st/vdpau: recreate video buffer if format doesn't match
[mesa.git] / src / gallium / state_trackers / vdpau / vdpau_private.h
1 /**************************************************************************
2 *
3 * Copyright 2010 Younes Manton & Thomas Balling Sørensen.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef VDPAU_PRIVATE_H
29 #define VDPAU_PRIVATE_H
30
31 #include <assert.h>
32
33 #include <vdpau/vdpau.h>
34 #include <vdpau/vdpau_x11.h>
35
36 #include "pipe/p_compiler.h"
37 #include "pipe/p_video_decoder.h"
38
39 #include "util/u_debug.h"
40 #include "util/u_rect.h"
41 #include "vl/vl_compositor.h"
42
43 #include "vl_winsys.h"
44
45 /* Full VDPAU API documentation available at :
46 * ftp://download.nvidia.com/XFree86/vdpau/doxygen/html/index.html */
47
48 #define INFORMATION G3DVL VDPAU Driver Shared Library version VER_MAJOR.VER_MINOR
49 #define QUOTEME(x) #x
50 #define TOSTRING(x) QUOTEME(x)
51 #define INFORMATION_STRING TOSTRING(INFORMATION)
52 #define VL_HANDLES
53
54 static inline enum pipe_video_chroma_format
55 ChromaToPipe(VdpChromaType vdpau_type)
56 {
57 switch (vdpau_type) {
58 case VDP_CHROMA_TYPE_420:
59 return PIPE_VIDEO_CHROMA_FORMAT_420;
60 case VDP_CHROMA_TYPE_422:
61 return PIPE_VIDEO_CHROMA_FORMAT_422;
62 case VDP_CHROMA_TYPE_444:
63 return PIPE_VIDEO_CHROMA_FORMAT_444;
64 default:
65 assert(0);
66 }
67
68 return -1;
69 }
70
71 static inline VdpChromaType
72 PipeToChroma(enum pipe_video_chroma_format pipe_type)
73 {
74 switch (pipe_type) {
75 case PIPE_VIDEO_CHROMA_FORMAT_420:
76 return VDP_CHROMA_TYPE_420;
77 case PIPE_VIDEO_CHROMA_FORMAT_422:
78 return VDP_CHROMA_TYPE_422;
79 case PIPE_VIDEO_CHROMA_FORMAT_444:
80 return VDP_CHROMA_TYPE_444;
81 default:
82 assert(0);
83 }
84
85 return -1;
86 }
87
88 static inline enum pipe_format
89 FormatYCBCRToPipe(VdpYCbCrFormat vdpau_format)
90 {
91 switch (vdpau_format) {
92 case VDP_YCBCR_FORMAT_NV12:
93 return PIPE_FORMAT_NV12;
94 case VDP_YCBCR_FORMAT_YV12:
95 return PIPE_FORMAT_YV12;
96 case VDP_YCBCR_FORMAT_UYVY:
97 return PIPE_FORMAT_UYVY;
98 case VDP_YCBCR_FORMAT_YUYV:
99 return PIPE_FORMAT_YUYV;
100 case VDP_YCBCR_FORMAT_Y8U8V8A8: /* Not defined in p_format.h */
101 return PIPE_FORMAT_NONE;
102 case VDP_YCBCR_FORMAT_V8U8Y8A8:
103 return PIPE_FORMAT_VUYA;
104 default:
105 assert(0);
106 }
107
108 return PIPE_FORMAT_NONE;
109 }
110
111 static inline VdpYCbCrFormat
112 PipeToFormatYCBCR(enum pipe_format p_format)
113 {
114 switch (p_format) {
115 case PIPE_FORMAT_NV12:
116 return VDP_YCBCR_FORMAT_NV12;
117 case PIPE_FORMAT_YV12:
118 return VDP_YCBCR_FORMAT_YV12;
119 case PIPE_FORMAT_UYVY:
120 return VDP_YCBCR_FORMAT_UYVY;
121 case PIPE_FORMAT_YUYV:
122 return VDP_YCBCR_FORMAT_YUYV;
123 //case PIPE_FORMAT_YUVA:
124 // return VDP_YCBCR_FORMAT_Y8U8V8A8;
125 case PIPE_FORMAT_VUYA:
126 return VDP_YCBCR_FORMAT_V8U8Y8A8;
127 default:
128 assert(0);
129 }
130
131 return -1;
132 }
133
134 static inline enum pipe_format
135 FormatRGBAToPipe(VdpRGBAFormat vdpau_format)
136 {
137 switch (vdpau_format) {
138 case VDP_RGBA_FORMAT_A8:
139 return PIPE_FORMAT_A8_UNORM;
140 case VDP_RGBA_FORMAT_B10G10R10A2:
141 return PIPE_FORMAT_B10G10R10A2_UNORM;
142 case VDP_RGBA_FORMAT_B8G8R8A8:
143 return PIPE_FORMAT_B8G8R8A8_UNORM;
144 case VDP_RGBA_FORMAT_R10G10B10A2:
145 return PIPE_FORMAT_R10G10B10A2_UNORM;
146 case VDP_RGBA_FORMAT_R8G8B8A8:
147 return PIPE_FORMAT_R8G8B8A8_UNORM;
148 default:
149 assert(0);
150 }
151
152 return PIPE_FORMAT_NONE;
153 }
154
155 static inline VdpRGBAFormat
156 PipeToFormatRGBA(enum pipe_format p_format)
157 {
158 switch (p_format) {
159 case PIPE_FORMAT_A8_UNORM:
160 return VDP_RGBA_FORMAT_A8;
161 case PIPE_FORMAT_B10G10R10A2_UNORM:
162 return VDP_RGBA_FORMAT_B10G10R10A2;
163 case PIPE_FORMAT_B8G8R8A8_UNORM:
164 return VDP_RGBA_FORMAT_B8G8R8A8;
165 case PIPE_FORMAT_R10G10B10A2_UNORM:
166 return VDP_RGBA_FORMAT_R10G10B10A2;
167 case PIPE_FORMAT_R8G8B8A8_UNORM:
168 return VDP_RGBA_FORMAT_R8G8B8A8;
169 default:
170 assert(0);
171 }
172
173 return -1;
174 }
175
176 static inline enum pipe_format
177 FormatIndexedToPipe(VdpRGBAFormat vdpau_format)
178 {
179 switch (vdpau_format) {
180 case VDP_INDEXED_FORMAT_A4I4:
181 return PIPE_FORMAT_A4R4_UNORM;
182 case VDP_INDEXED_FORMAT_I4A4:
183 return PIPE_FORMAT_R4A4_UNORM;
184 case VDP_INDEXED_FORMAT_A8I8:
185 return PIPE_FORMAT_A8R8_UNORM;
186 case VDP_INDEXED_FORMAT_I8A8:
187 return PIPE_FORMAT_R8A8_UNORM;
188 default:
189 assert(0);
190 }
191
192 return PIPE_FORMAT_NONE;
193 }
194
195 static inline enum pipe_format
196 FormatColorTableToPipe(VdpColorTableFormat vdpau_format)
197 {
198 switch(vdpau_format) {
199 case VDP_COLOR_TABLE_FORMAT_B8G8R8X8:
200 return PIPE_FORMAT_B8G8R8X8_UNORM;
201 default:
202 assert(0);
203 }
204
205 return PIPE_FORMAT_NONE;
206 }
207
208 static inline enum pipe_video_profile
209 ProfileToPipe(VdpDecoderProfile vdpau_profile)
210 {
211 switch (vdpau_profile) {
212 case VDP_DECODER_PROFILE_MPEG1:
213 return PIPE_VIDEO_PROFILE_MPEG1;
214 case VDP_DECODER_PROFILE_MPEG2_SIMPLE:
215 return PIPE_VIDEO_PROFILE_MPEG2_SIMPLE;
216 case VDP_DECODER_PROFILE_MPEG2_MAIN:
217 return PIPE_VIDEO_PROFILE_MPEG2_MAIN;
218 case VDP_DECODER_PROFILE_H264_BASELINE:
219 return PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE;
220 case VDP_DECODER_PROFILE_H264_MAIN:
221 return PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN;
222 case VDP_DECODER_PROFILE_H264_HIGH:
223 return PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH;
224 case VDP_DECODER_PROFILE_MPEG4_PART2_SP:
225 return PIPE_VIDEO_PROFILE_MPEG4_SIMPLE;
226 case VDP_DECODER_PROFILE_MPEG4_PART2_ASP:
227 return PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE;
228 case VDP_DECODER_PROFILE_VC1_SIMPLE:
229 return PIPE_VIDEO_PROFILE_VC1_SIMPLE;
230 case VDP_DECODER_PROFILE_VC1_MAIN:
231 return PIPE_VIDEO_PROFILE_VC1_MAIN;
232 case VDP_DECODER_PROFILE_VC1_ADVANCED:
233 return PIPE_VIDEO_PROFILE_VC1_ADVANCED;
234 default:
235 return PIPE_VIDEO_PROFILE_UNKNOWN;
236 }
237 }
238
239 static inline VdpDecoderProfile
240 PipeToProfile(enum pipe_video_profile p_profile)
241 {
242 switch (p_profile) {
243 case PIPE_VIDEO_PROFILE_MPEG1:
244 return VDP_DECODER_PROFILE_MPEG1;
245 case PIPE_VIDEO_PROFILE_MPEG2_SIMPLE:
246 return VDP_DECODER_PROFILE_MPEG2_SIMPLE;
247 case PIPE_VIDEO_PROFILE_MPEG2_MAIN:
248 return VDP_DECODER_PROFILE_MPEG2_MAIN;
249 case PIPE_VIDEO_PROFILE_MPEG4_AVC_BASELINE:
250 return VDP_DECODER_PROFILE_H264_BASELINE;
251 case PIPE_VIDEO_PROFILE_MPEG4_AVC_MAIN:
252 return VDP_DECODER_PROFILE_H264_MAIN;
253 case PIPE_VIDEO_PROFILE_MPEG4_AVC_HIGH:
254 return VDP_DECODER_PROFILE_H264_HIGH;
255 case PIPE_VIDEO_PROFILE_MPEG4_SIMPLE:
256 return VDP_DECODER_PROFILE_MPEG4_PART2_SP;
257 case PIPE_VIDEO_PROFILE_MPEG4_ADVANCED_SIMPLE:
258 return VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
259 case PIPE_VIDEO_PROFILE_VC1_SIMPLE:
260 return VDP_DECODER_PROFILE_VC1_SIMPLE;
261 case PIPE_VIDEO_PROFILE_VC1_MAIN:
262 return VDP_DECODER_PROFILE_VC1_MAIN;
263 case PIPE_VIDEO_PROFILE_VC1_ADVANCED:
264 return VDP_DECODER_PROFILE_VC1_ADVANCED;
265 default:
266 assert(0);
267 return -1;
268 }
269 }
270
271 static inline struct pipe_video_rect *
272 RectToPipe(const VdpRect *src, struct pipe_video_rect *dst)
273 {
274 if (src) {
275 dst->x = MIN2(src->x1, src->x0);
276 dst->y = MIN2(src->y1, src->y0);
277 dst->w = abs(src->x1 - src->x0);
278 dst->h = abs(src->y1 - src->y0);
279 return dst;
280 }
281 return NULL;
282 }
283
284 typedef struct
285 {
286 struct vl_screen *vscreen;
287 struct vl_context *context;
288 struct vl_compositor compositor;
289 } vlVdpDevice;
290
291 typedef struct
292 {
293 vlVdpDevice *device;
294 Drawable drawable;
295 } vlVdpPresentationQueueTarget;
296
297 typedef struct
298 {
299 vlVdpDevice *device;
300 Drawable drawable;
301 struct vl_compositor compositor;
302 struct u_rect dirty_area;
303 } vlVdpPresentationQueue;
304
305 typedef struct
306 {
307 vlVdpDevice *device;
308 struct vl_compositor compositor;
309 unsigned video_width, video_height;
310 enum pipe_video_chroma_format chroma_format;
311 unsigned max_layers, skip_chroma_deint, custom_csc;
312 float luma_key_min, luma_key_max, sharpness, noise_reduction_level;
313 float csc[16];
314 } vlVdpVideoMixer;
315
316 typedef struct
317 {
318 vlVdpDevice *device;
319 struct pipe_video_buffer templat, *video_buffer;
320 } vlVdpSurface;
321
322 typedef uint64_t vlVdpTime;
323
324 typedef struct
325 {
326 vlVdpTime timestamp;
327 vlVdpDevice *device;
328 struct pipe_surface *surface;
329 struct pipe_sampler_view *sampler_view;
330 struct pipe_fence_handle *fence;
331 struct u_rect dirty_area;
332 } vlVdpOutputSurface;
333
334 typedef struct
335 {
336 vlVdpDevice *device;
337 struct pipe_video_decoder *decoder;
338 } vlVdpDecoder;
339
340 typedef uint32_t vlHandle;
341
342 boolean vlCreateHTAB(void);
343 void vlDestroyHTAB(void);
344 vlHandle vlAddDataHTAB(void *data);
345 void* vlGetDataHTAB(vlHandle handle);
346 void vlRemoveDataHTAB(vlHandle handle);
347
348 boolean vlGetFuncFTAB(VdpFuncId function_id, void **func);
349
350 /* Public functions */
351 VdpDeviceCreateX11 vdp_imp_device_create_x11;
352 VdpPresentationQueueTargetCreateX11 vlVdpPresentationQueueTargetCreateX11;
353
354 /* Internal function pointers */
355 VdpGetErrorString vlVdpGetErrorString;
356 VdpDeviceDestroy vlVdpDeviceDestroy;
357 VdpGetProcAddress vlVdpGetProcAddress;
358 VdpGetApiVersion vlVdpGetApiVersion;
359 VdpGetInformationString vlVdpGetInformationString;
360 VdpVideoSurfaceQueryCapabilities vlVdpVideoSurfaceQueryCapabilities;
361 VdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities vlVdpVideoSurfaceQueryGetPutBitsYCbCrCapabilities;
362 VdpDecoderQueryCapabilities vlVdpDecoderQueryCapabilities;
363 VdpOutputSurfaceQueryCapabilities vlVdpOutputSurfaceQueryCapabilities;
364 VdpOutputSurfaceQueryGetPutBitsNativeCapabilities vlVdpOutputSurfaceQueryGetPutBitsNativeCapabilities;
365 VdpOutputSurfaceQueryPutBitsIndexedCapabilities vlVdpOutputSurfaceQueryPutBitsIndexedCapabilities;
366 VdpOutputSurfaceQueryPutBitsYCbCrCapabilities vlVdpOutputSurfaceQueryPutBitsYCbCrCapabilities;
367 VdpBitmapSurfaceQueryCapabilities vlVdpBitmapSurfaceQueryCapabilities;
368 VdpVideoMixerQueryFeatureSupport vlVdpVideoMixerQueryFeatureSupport;
369 VdpVideoMixerQueryParameterSupport vlVdpVideoMixerQueryParameterSupport;
370 VdpVideoMixerQueryParameterValueRange vlVdpVideoMixerQueryParameterValueRange;
371 VdpVideoMixerQueryAttributeSupport vlVdpVideoMixerQueryAttributeSupport;
372 VdpVideoMixerQueryAttributeValueRange vlVdpVideoMixerQueryAttributeValueRange;
373 VdpVideoSurfaceCreate vlVdpVideoSurfaceCreate;
374 VdpVideoSurfaceDestroy vlVdpVideoSurfaceDestroy;
375 VdpVideoSurfaceGetParameters vlVdpVideoSurfaceGetParameters;
376 VdpVideoSurfaceGetBitsYCbCr vlVdpVideoSurfaceGetBitsYCbCr;
377 VdpVideoSurfacePutBitsYCbCr vlVdpVideoSurfacePutBitsYCbCr;
378 VdpDecoderCreate vlVdpDecoderCreate;
379 VdpDecoderDestroy vlVdpDecoderDestroy;
380 VdpDecoderGetParameters vlVdpDecoderGetParameters;
381 VdpDecoderRender vlVdpDecoderRender;
382 VdpOutputSurfaceCreate vlVdpOutputSurfaceCreate;
383 VdpOutputSurfaceDestroy vlVdpOutputSurfaceDestroy;
384 VdpOutputSurfaceGetParameters vlVdpOutputSurfaceGetParameters;
385 VdpOutputSurfaceGetBitsNative vlVdpOutputSurfaceGetBitsNative;
386 VdpOutputSurfacePutBitsNative vlVdpOutputSurfacePutBitsNative;
387 VdpOutputSurfacePutBitsIndexed vlVdpOutputSurfacePutBitsIndexed;
388 VdpOutputSurfacePutBitsYCbCr vlVdpOutputSurfacePutBitsYCbCr;
389 VdpOutputSurfaceRenderOutputSurface vlVdpOutputSurfaceRenderOutputSurface;
390 VdpOutputSurfaceRenderBitmapSurface vlVdpOutputSurfaceRenderBitmapSurface;
391 VdpBitmapSurfaceCreate vlVdpBitmapSurfaceCreate;
392 VdpBitmapSurfaceDestroy vlVdpBitmapSurfaceDestroy;
393 VdpBitmapSurfaceGetParameters vlVdpBitmapSurfaceGetParameters;
394 VdpBitmapSurfacePutBitsNative vlVdpBitmapSurfacePutBitsNative;
395 VdpPresentationQueueTargetDestroy vlVdpPresentationQueueTargetDestroy;
396 VdpPresentationQueueCreate vlVdpPresentationQueueCreate;
397 VdpPresentationQueueDestroy vlVdpPresentationQueueDestroy;
398 VdpPresentationQueueSetBackgroundColor vlVdpPresentationQueueSetBackgroundColor;
399 VdpPresentationQueueGetBackgroundColor vlVdpPresentationQueueGetBackgroundColor;
400 VdpPresentationQueueGetTime vlVdpPresentationQueueGetTime;
401 VdpPresentationQueueDisplay vlVdpPresentationQueueDisplay;
402 VdpPresentationQueueBlockUntilSurfaceIdle vlVdpPresentationQueueBlockUntilSurfaceIdle;
403 VdpPresentationQueueQuerySurfaceStatus vlVdpPresentationQueueQuerySurfaceStatus;
404 VdpPreemptionCallback vlVdpPreemptionCallback;
405 VdpPreemptionCallbackRegister vlVdpPreemptionCallbackRegister;
406 VdpVideoMixerSetFeatureEnables vlVdpVideoMixerSetFeatureEnables;
407 VdpVideoMixerCreate vlVdpVideoMixerCreate;
408 VdpVideoMixerRender vlVdpVideoMixerRender;
409 VdpVideoMixerSetAttributeValues vlVdpVideoMixerSetAttributeValues;
410 VdpVideoMixerGetFeatureSupport vlVdpVideoMixerGetFeatureSupport;
411 VdpVideoMixerGetFeatureEnables vlVdpVideoMixerGetFeatureEnables;
412 VdpVideoMixerGetParameterValues vlVdpVideoMixerGetParameterValues;
413 VdpVideoMixerGetAttributeValues vlVdpVideoMixerGetAttributeValues;
414 VdpVideoMixerDestroy vlVdpVideoMixerDestroy;
415 VdpGenerateCSCMatrix vlVdpGenerateCSCMatrix;
416
417 #define VDPAU_OUT 0
418 #define VDPAU_ERR 1
419 #define VDPAU_WARN 2
420 #define VDPAU_TRACE 3
421
422 static inline void VDPAU_MSG(unsigned int level, const char *fmt, ...)
423 {
424 static int debug_level = -1;
425
426 if (debug_level == -1) {
427 debug_level = MAX2(debug_get_num_option("VDPAU_DEBUG", 0), 0);
428 }
429
430 if (level <= debug_level) {
431 va_list ap;
432 va_start(ap, fmt);
433 _debug_vprintf(fmt, ap);
434 va_end(ap);
435 }
436 }
437
438 #endif /* VDPAU_PRIVATE_H */