2 * VDPAU video output driver
4 * Copyright (C) 2008 NVIDIA
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * \defgroup VDPAU_Presentation VDPAU Presentation
27 * Actual decoding and presentation are implemented here.
28 * All necessary frame information is collected through
29 * the "vdpau_render_state" structure after parsing all headers
30 * etc. in libavcodec for different codecs.
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "x11_common.h"
45 #include "subopt-helper.h"
47 #include "libavcodec/vdpau.h"
49 #include "gui/interface.h"
51 #include "libavutil/common.h"
52 #include "libavutil/mathematics.h"
54 #include "libass/ass.h"
55 #include "libass/ass_mp.h"
57 static vo_info_t info
= {
60 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
66 #define CHECK_ST_ERROR(message) \
67 if (vdp_st != VDP_STATUS_OK) { \
68 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
69 message, vdp_get_error_string(vdp_st)); \
73 #define CHECK_ST_WARNING(message) \
74 if (vdp_st != VDP_STATUS_OK) \
75 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] %s: %s\n", \
76 message, vdp_get_error_string(vdp_st));
78 /* number of video and output surfaces */
79 #define NUM_OUTPUT_SURFACES 2
80 #define MAX_VIDEO_SURFACES 50
82 /* number of palette entries */
83 #define PALETTE_SIZE 256
85 /* Initial maximum number of EOSD surfaces */
86 #define EOSD_SURFACES_INITIAL 512
89 * Global variable declaration - VDPAU specific
92 /* Declaration for all variables of win_x11_init_vdpau_procs() and
93 * win_x11_init_vdpau_flip_queue() functions
95 static VdpDevice vdp_device
;
96 static VdpDeviceCreateX11
*vdp_device_create
;
97 static VdpGetProcAddress
*vdp_get_proc_address
;
99 static VdpPresentationQueueTarget vdp_flip_target
;
100 static VdpPresentationQueue vdp_flip_queue
;
102 static VdpDeviceDestroy
*vdp_device_destroy
;
103 static VdpVideoSurfaceCreate
*vdp_video_surface_create
;
104 static VdpVideoSurfaceDestroy
*vdp_video_surface_destroy
;
106 static VdpGetErrorString
*vdp_get_error_string
;
108 /* May be used in software filtering/postprocessing options
109 * in MPlayer (./mplayer -vf ..) if we copy video_surface data to
112 static VdpVideoSurfacePutBitsYCbCr
*vdp_video_surface_put_bits_y_cb_cr
;
113 static VdpOutputSurfacePutBitsNative
*vdp_output_surface_put_bits_native
;
115 static VdpOutputSurfaceCreate
*vdp_output_surface_create
;
116 static VdpOutputSurfaceDestroy
*vdp_output_surface_destroy
;
118 /* VideoMixer puts video_surface data on displayable output_surface. */
119 static VdpVideoMixerCreate
*vdp_video_mixer_create
;
120 static VdpVideoMixerDestroy
*vdp_video_mixer_destroy
;
121 static VdpVideoMixerRender
*vdp_video_mixer_render
;
122 static VdpVideoMixerSetFeatureEnables
*vdp_video_mixer_set_feature_enables
;
123 static VdpVideoMixerSetAttributeValues
*vdp_video_mixer_set_attribute_values
;
125 static VdpPresentationQueueTargetDestroy
*vdp_presentation_queue_target_destroy
;
126 static VdpPresentationQueueCreate
*vdp_presentation_queue_create
;
127 static VdpPresentationQueueDestroy
*vdp_presentation_queue_destroy
;
128 static VdpPresentationQueueDisplay
*vdp_presentation_queue_display
;
129 static VdpPresentationQueueBlockUntilSurfaceIdle
*vdp_presentation_queue_block_until_surface_idle
;
130 static VdpPresentationQueueTargetCreateX11
*vdp_presentation_queue_target_create_x11
;
132 static VdpOutputSurfaceRenderOutputSurface
*vdp_output_surface_render_output_surface
;
133 static VdpOutputSurfacePutBitsIndexed
*vdp_output_surface_put_bits_indexed
;
134 static VdpOutputSurfaceRenderBitmapSurface
*vdp_output_surface_render_bitmap_surface
;
136 static VdpBitmapSurfaceCreate
*vdp_bitmap_surface_create
;
137 static VdpBitmapSurfaceDestroy
*vdp_bitmap_surface_destroy
;
138 static VdpBitmapSurfacePutBitsNative
*vdp_bitmap_surface_putbits_native
;
140 static VdpDecoderCreate
*vdp_decoder_create
;
141 static VdpDecoderDestroy
*vdp_decoder_destroy
;
142 static VdpDecoderRender
*vdp_decoder_render
;
144 static VdpGenerateCSCMatrix
*vdp_generate_csc_matrix
;
146 static void *vdpau_lib_handle
;
147 /* output_surfaces[NUM_OUTPUT_SURFACES] is misused for OSD. */
148 #define osd_surface output_surfaces[NUM_OUTPUT_SURFACES]
149 static VdpOutputSurface output_surfaces
[NUM_OUTPUT_SURFACES
+ 1];
150 static int output_surface_width
, output_surface_height
;
152 static VdpVideoMixer video_mixer
;
154 static int deint_type
;
156 static float denoise
;
157 static float sharpen
;
158 static int top_field_first
;
160 static VdpDecoder decoder
;
161 static int decoder_max_refs
;
163 static VdpRect src_rect_vid
;
164 static VdpRect out_rect_vid
;
165 static int border_x
, border_y
;
167 static struct vdpau_render_state surface_render
[MAX_VIDEO_SURFACES
];
168 static int surface_num
;
169 static int vid_surface_num
;
170 static uint32_t vid_width
, vid_height
;
171 static uint32_t image_format
;
172 static const VdpChromaType vdp_chroma_type
= VDP_CHROMA_TYPE_420
;
175 static unsigned char *index_data
;
176 static int index_data_size
;
177 static uint32_t palette
[PALETTE_SIZE
];
182 VdpBitmapSurface surface
;
188 // List of surfaces to be rendered
190 VdpBitmapSurface surface
;
196 static int eosd_render_count
;
197 static int eosd_surface_count
;
200 static VdpProcamp procamp
;
205 static int visible_buf
;
206 static int int_pause
;
208 static void draw_eosd(void);
210 static void video_to_output_surface(void)
215 if (vid_surface_num
< 0)
218 // we would need to provide 2 past and 1 future frames to allow advanced
219 // deinterlacing, which is not really possible currently.
220 for (i
= 0; i
<= !!(deint
> 1); i
++) {
221 int field
= VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME
;
222 VdpOutputSurface output_surface
;
229 field
= top_field_first
== i
?
230 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD
:
231 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD
;
232 output_surface
= output_surfaces
[surface_num
];
233 vdp_st
= vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue
,
236 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
238 vdp_st
= vdp_video_mixer_render(video_mixer
, VDP_INVALID_HANDLE
, 0,
240 surface_render
[vid_surface_num
].surface
,
241 0, NULL
, &src_rect_vid
,
243 NULL
, &out_rect_vid
, 0, NULL
);
244 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
248 static void resize(void)
252 struct vo_rect src_rect
;
253 struct vo_rect dst_rect
;
254 struct vo_rect borders
;
255 calc_src_dst_rects(vid_width
, vid_height
, &src_rect
, &dst_rect
, &borders
, NULL
);
256 out_rect_vid
.x0
= dst_rect
.left
;
257 out_rect_vid
.x1
= dst_rect
.right
;
258 out_rect_vid
.y0
= dst_rect
.top
;
259 out_rect_vid
.y1
= dst_rect
.bottom
;
260 src_rect_vid
.x0
= src_rect
.left
;
261 src_rect_vid
.x1
= src_rect
.right
;
262 src_rect_vid
.y0
= src_rect
.top
;
263 src_rect_vid
.y1
= src_rect
.bottom
;
264 border_x
= borders
.left
;
265 border_y
= borders
.top
;
266 #ifdef CONFIG_FREETYPE
267 // adjust font size to display size
270 vo_osd_changed(OSDTYPE_OSD
);
272 if (output_surface_width
< vo_dwidth
|| output_surface_height
< vo_dheight
) {
273 if (output_surface_width
< vo_dwidth
) {
274 output_surface_width
+= output_surface_width
>> 1;
275 output_surface_width
= FFMAX(output_surface_width
, vo_dwidth
);
277 if (output_surface_height
< vo_dheight
) {
278 output_surface_height
+= output_surface_height
>> 1;
279 output_surface_height
= FFMAX(output_surface_height
, vo_dheight
);
281 // Creation of output_surfaces
282 for (i
= 0; i
<= NUM_OUTPUT_SURFACES
; i
++) {
283 if (output_surfaces
[i
] != VDP_INVALID_HANDLE
)
284 vdp_output_surface_destroy(output_surfaces
[i
]);
285 vdp_st
= vdp_output_surface_create(vdp_device
, VDP_RGBA_FORMAT_B8G8R8A8
,
286 output_surface_width
, output_surface_height
,
287 &output_surfaces
[i
]);
288 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
289 mp_msg(MSGT_VO
, MSGL_DBG2
, "OUT CREATE: %u\n", output_surfaces
[i
]);
292 video_to_output_surface();
297 /* Initialize vdp_get_proc_address, called from preinit() */
298 static int win_x11_init_vdpau_procs(void)
302 struct vdp_function
{
307 const struct vdp_function
*dsc
;
309 static const struct vdp_function vdp_func
[] = {
310 {VDP_FUNC_ID_GET_ERROR_STRING
, &vdp_get_error_string
},
311 {VDP_FUNC_ID_DEVICE_DESTROY
, &vdp_device_destroy
},
312 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE
, &vdp_video_surface_create
},
313 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY
, &vdp_video_surface_destroy
},
314 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR
,
315 &vdp_video_surface_put_bits_y_cb_cr
},
316 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE
,
317 &vdp_output_surface_put_bits_native
},
318 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE
, &vdp_output_surface_create
},
319 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY
, &vdp_output_surface_destroy
},
320 {VDP_FUNC_ID_VIDEO_MIXER_CREATE
, &vdp_video_mixer_create
},
321 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY
, &vdp_video_mixer_destroy
},
322 {VDP_FUNC_ID_VIDEO_MIXER_RENDER
, &vdp_video_mixer_render
},
323 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES
,
324 &vdp_video_mixer_set_feature_enables
},
325 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES
,
326 &vdp_video_mixer_set_attribute_values
},
327 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY
,
328 &vdp_presentation_queue_target_destroy
},
329 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE
, &vdp_presentation_queue_create
},
330 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY
,
331 &vdp_presentation_queue_destroy
},
332 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY
,
333 &vdp_presentation_queue_display
},
334 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE
,
335 &vdp_presentation_queue_block_until_surface_idle
},
336 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11
,
337 &vdp_presentation_queue_target_create_x11
},
338 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE
,
339 &vdp_output_surface_render_output_surface
},
340 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED
,
341 &vdp_output_surface_put_bits_indexed
},
342 {VDP_FUNC_ID_DECODER_CREATE
, &vdp_decoder_create
},
343 {VDP_FUNC_ID_DECODER_RENDER
, &vdp_decoder_render
},
344 {VDP_FUNC_ID_DECODER_DESTROY
, &vdp_decoder_destroy
},
345 {VDP_FUNC_ID_BITMAP_SURFACE_CREATE
, &vdp_bitmap_surface_create
},
346 {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY
, &vdp_bitmap_surface_destroy
},
347 {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE
,
348 &vdp_bitmap_surface_putbits_native
},
349 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE
,
350 &vdp_output_surface_render_bitmap_surface
},
351 {VDP_FUNC_ID_GENERATE_CSC_MATRIX
, &vdp_generate_csc_matrix
},
355 vdp_st
= vdp_device_create(mDisplay
, mScreen
,
356 &vdp_device
, &vdp_get_proc_address
);
357 if (vdp_st
!= VDP_STATUS_OK
) {
358 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st
);
362 for (dsc
= vdp_func
; dsc
->pointer
; dsc
++) {
363 vdp_st
= vdp_get_proc_address(vdp_device
, dsc
->id
, dsc
->pointer
);
364 if (vdp_st
!= VDP_STATUS_OK
) {
365 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Error when calling vdp_get_proc_address(function id %d): %s\n", dsc
->id
, vdp_get_error_string
? vdp_get_error_string(vdp_st
) : "?");
372 /* Initialize vdpau_flip_queue, called from config() */
373 static int win_x11_init_vdpau_flip_queue(void)
377 vdp_st
= vdp_presentation_queue_target_create_x11(vdp_device
, vo_window
,
379 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
381 vdp_st
= vdp_presentation_queue_create(vdp_device
, vdp_flip_target
,
383 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
388 static int create_vdp_mixer(VdpChromaType vdp_chroma_type
) {
389 #define VDP_NUM_MIXER_PARAMETER 3
390 #define MAX_NUM_FEATURES 5
393 int feature_count
= 0;
394 VdpVideoMixerFeature features
[MAX_NUM_FEATURES
];
395 VdpBool feature_enables
[MAX_NUM_FEATURES
];
396 static const VdpVideoMixerAttribute denoise_attrib
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL
};
397 const void * const denoise_value
[] = {&denoise
};
398 static const VdpVideoMixerAttribute sharpen_attrib
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL
};
399 const void * const sharpen_value
[] = {&sharpen
};
400 static const VdpVideoMixerParameter parameters
[VDP_NUM_MIXER_PARAMETER
] = {
401 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH
,
402 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT
,
403 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
405 const void *const parameter_values
[VDP_NUM_MIXER_PARAMETER
] = {
411 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL
;
413 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL
;
415 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE
;
417 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION
;
419 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS
;
421 vdp_st
= vdp_video_mixer_create(vdp_device
, feature_count
, features
,
422 VDP_NUM_MIXER_PARAMETER
,
423 parameters
, parameter_values
,
425 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
427 for (i
= 0; i
< feature_count
; i
++) feature_enables
[i
] = VDP_TRUE
;
429 vdp_video_mixer_set_feature_enables(video_mixer
, feature_count
, features
, feature_enables
);
431 vdp_video_mixer_set_attribute_values(video_mixer
, 1, denoise_attrib
, denoise_value
);
433 vdp_video_mixer_set_attribute_values(video_mixer
, 1, sharpen_attrib
, sharpen_value
);
438 // Free everything specific to a certain video file
439 static void free_video_specific(void) {
443 if (decoder
!= VDP_INVALID_HANDLE
)
444 vdp_decoder_destroy(decoder
);
445 decoder
= VDP_INVALID_HANDLE
;
446 decoder_max_refs
= -1;
448 for (i
= 0; i
< MAX_VIDEO_SURFACES
; i
++) {
449 if (surface_render
[i
].surface
!= VDP_INVALID_HANDLE
) {
450 vdp_st
= vdp_video_surface_destroy(surface_render
[i
].surface
);
451 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
453 surface_render
[i
].surface
= VDP_INVALID_HANDLE
;
456 if (video_mixer
!= VDP_INVALID_HANDLE
) {
457 vdp_st
= vdp_video_mixer_destroy(video_mixer
);
458 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
460 video_mixer
= VDP_INVALID_HANDLE
;
464 * connect to X server, create and map window, initialize all
465 * VDPAU objects, create different surfaces etc.
467 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
468 uint32_t d_height
, uint32_t flags
, char *title
,
472 XSetWindowAttributes xswa
;
473 XWindowAttributes attribs
;
474 unsigned long xswamask
;
478 int vm
= flags
& VOFLAG_MODESWITCHING
;
481 image_format
= format
;
488 guiGetEvent(guiSetShVideo
, 0); // the GUI will set up / resize our window
497 XGetWindowAttributes(mDisplay
, DefaultRootWindow(mDisplay
), &attribs
);
498 depth
= attribs
.depth
;
499 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32)
501 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
503 xswa
.background_pixel
= 0;
504 xswa
.border_pixel
= 0;
505 xswamask
= CWBackPixel
| CWBorderPixel
;
507 vo_x11_create_vo_window(&vinfo
, vo_dx
, vo_dy
, d_width
, d_height
,
508 flags
, CopyFromParent
, "vdpau", title
);
509 XChangeWindowAttributes(mDisplay
, vo_window
, xswamask
, &xswa
);
513 /* Grab the mouse pointer in our window */
515 XGrabPointer(mDisplay
, vo_window
, True
, 0,
516 GrabModeAsync
, GrabModeAsync
,
517 vo_window
, None
, CurrentTime
);
518 XSetInputFocus(mDisplay
, vo_window
, RevertToNone
, CurrentTime
);
523 if ((flags
& VOFLAG_FULLSCREEN
) && WinID
<= 0)
526 /* -----VDPAU related code here -------- */
528 free_video_specific();
530 if (vdp_flip_queue
== VDP_INVALID_HANDLE
&& win_x11_init_vdpau_flip_queue())
533 // video width and height
537 if (create_vdp_mixer(vdp_chroma_type
))
541 vid_surface_num
= -1;
547 static void check_events(void)
549 int e
= vo_x11_check_events(mDisplay
);
551 if (e
& VO_EVENT_RESIZE
)
554 if ((e
& VO_EVENT_EXPOSE
|| e
& VO_EVENT_RESIZE
) && int_pause
) {
555 /* did we already draw a buffer */
557 /* redraw the last visible buffer */
559 vdp_st
= vdp_presentation_queue_display(vdp_flip_queue
,
560 output_surfaces
[surface_num
],
561 vo_dwidth
, vo_dheight
,
563 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
568 static void draw_osd_I8A8(int x0
,int y0
, int w
,int h
, unsigned char *src
,
569 unsigned char *srca
, int stride
)
571 VdpOutputSurface output_surface
= output_surfaces
[surface_num
];
575 int index_data_size_required
;
576 VdpRect output_indexed_rect_vid
;
577 VdpOutputSurfaceRenderBlendState blend_state
;
582 index_data_size_required
= 2*w
*h
;
583 if (index_data_size
< index_data_size_required
) {
584 index_data
= realloc(index_data
, index_data_size_required
);
585 index_data_size
= index_data_size_required
;
588 // index_data creation, component order - I, A, I, A, .....
589 for (i
= 0; i
< h
; i
++)
590 for (j
= 0; j
< w
; j
++) {
591 index_data
[i
*2*w
+ j
*2] = src
[i
*stride
+j
];
592 index_data
[i
*2*w
+ j
*2 + 1] = -srca
[i
*stride
+j
];
595 output_indexed_rect_vid
.x0
= x0
;
596 output_indexed_rect_vid
.y0
= y0
;
597 output_indexed_rect_vid
.x1
= x0
+ w
;
598 output_indexed_rect_vid
.y1
= y0
+ h
;
602 // write source_data to osd_surface.
603 vdp_st
= vdp_output_surface_put_bits_indexed(osd_surface
,
604 VDP_INDEXED_FORMAT_I8A8
,
605 (const void *const*)&index_data
,
607 &output_indexed_rect_vid
,
608 VDP_COLOR_TABLE_FORMAT_B8G8R8X8
,
610 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed")
612 blend_state
.struct_version
= VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION
;
613 blend_state
.blend_factor_source_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
;
614 blend_state
.blend_factor_source_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
;
615 blend_state
.blend_factor_destination_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
;
616 blend_state
.blend_factor_destination_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
;
617 blend_state
.blend_equation_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
618 blend_state
.blend_equation_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
620 vdp_st
= vdp_output_surface_render_output_surface(output_surface
,
621 &output_indexed_rect_vid
,
623 &output_indexed_rect_vid
,
626 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0
);
627 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
630 static void draw_eosd(void) {
632 VdpOutputSurface output_surface
= output_surfaces
[surface_num
];
633 VdpOutputSurfaceRenderBlendState blend_state
;
636 blend_state
.struct_version
= VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION
;
637 blend_state
.blend_factor_source_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
;
638 blend_state
.blend_factor_source_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
;
639 blend_state
.blend_factor_destination_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
;
640 blend_state
.blend_factor_destination_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
;
641 blend_state
.blend_equation_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
642 blend_state
.blend_equation_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
644 for (i
=0; i
<eosd_render_count
; i
++) {
645 vdp_st
= vdp_output_surface_render_bitmap_surface(
646 output_surface
, &eosd_targets
[i
].dest
,
647 eosd_targets
[i
].surface
, &eosd_targets
[i
].source
,
648 &eosd_targets
[i
].color
, &blend_state
,
649 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0
);
650 CHECK_ST_WARNING("EOSD: Error when rendering")
654 static void generate_eosd(mp_eosd_images_t
*imgs
) {
658 ass_image_t
*img
= imgs
->imgs
;
661 // Nothing changed, no need to redraw
662 if (imgs
->changed
== 0)
664 eosd_render_count
= 0;
665 // There's nothing to render!
669 if (imgs
->changed
== 1)
670 goto eosd_skip_upload
;
672 for (j
=0; j
<eosd_surface_count
; j
++)
673 eosd_surfaces
[j
].in_use
= 0;
675 for (i
= img
; i
; i
= i
->next
) {
676 // Try to reuse a suitable surface
678 for (j
=0; j
<eosd_surface_count
; j
++) {
679 if (eosd_surfaces
[j
].surface
!= VDP_INVALID_HANDLE
&& !eosd_surfaces
[j
].in_use
&&
680 eosd_surfaces
[j
].w
>= i
->w
&& eosd_surfaces
[j
].h
>= i
->h
) {
685 // None found, allocate a new surface
687 for (j
=0; j
<eosd_surface_count
; j
++) {
688 if (!eosd_surfaces
[j
].in_use
) {
689 if (eosd_surfaces
[j
].surface
!= VDP_INVALID_HANDLE
)
690 vdp_bitmap_surface_destroy(eosd_surfaces
[j
].surface
);
695 // Allocate new space for surface/target arrays
697 j
= found
= eosd_surface_count
;
698 eosd_surface_count
= eosd_surface_count
? eosd_surface_count
*2 : EOSD_SURFACES_INITIAL
;
699 eosd_surfaces
= realloc(eosd_surfaces
, eosd_surface_count
* sizeof(*eosd_surfaces
));
700 eosd_targets
= realloc(eosd_targets
, eosd_surface_count
* sizeof(*eosd_targets
));
701 for(j
=found
; j
<eosd_surface_count
; j
++) {
702 eosd_surfaces
[j
].surface
= VDP_INVALID_HANDLE
;
703 eosd_surfaces
[j
].in_use
= 0;
706 vdp_st
= vdp_bitmap_surface_create(vdp_device
, VDP_RGBA_FORMAT_A8
,
707 i
->w
, i
->h
, VDP_TRUE
, &eosd_surfaces
[found
].surface
);
708 CHECK_ST_WARNING("EOSD: error when creating surface")
709 eosd_surfaces
[found
].w
= i
->w
;
710 eosd_surfaces
[found
].h
= i
->h
;
712 eosd_surfaces
[found
].in_use
= 1;
713 eosd_targets
[eosd_render_count
].surface
= eosd_surfaces
[found
].surface
;
718 vdp_st
= vdp_bitmap_surface_putbits_native(eosd_targets
[eosd_render_count
].surface
,
719 (const void *) &i
->bitmap
, &i
->stride
, &destRect
);
720 CHECK_ST_WARNING("EOSD: putbits failed")
725 eosd_render_count
= 0;
726 for (i
= img
; i
; i
= i
->next
) {
727 // Render dest, color, etc.
728 eosd_targets
[eosd_render_count
].color
.alpha
= 1.0 - ((i
->color
>> 0) & 0xff) / 255.0;
729 eosd_targets
[eosd_render_count
].color
.blue
= ((i
->color
>> 8) & 0xff) / 255.0;
730 eosd_targets
[eosd_render_count
].color
.green
= ((i
->color
>> 16) & 0xff) / 255.0;
731 eosd_targets
[eosd_render_count
].color
.red
= ((i
->color
>> 24) & 0xff) / 255.0;
732 eosd_targets
[eosd_render_count
].dest
.x0
= i
->dst_x
;
733 eosd_targets
[eosd_render_count
].dest
.y0
= i
->dst_y
;
734 eosd_targets
[eosd_render_count
].dest
.x1
= i
->w
+ i
->dst_x
;
735 eosd_targets
[eosd_render_count
].dest
.y1
= i
->h
+ i
->dst_y
;
736 eosd_targets
[eosd_render_count
].source
.x0
= 0;
737 eosd_targets
[eosd_render_count
].source
.y0
= 0;
738 eosd_targets
[eosd_render_count
].source
.x1
= i
->w
;
739 eosd_targets
[eosd_render_count
].source
.y1
= i
->h
;
744 static void draw_osd(void)
746 mp_msg(MSGT_VO
, MSGL_DBG2
, "DRAW_OSD\n");
748 vo_draw_text_ext(vo_dwidth
, vo_dheight
, border_x
, border_y
, border_x
, border_y
,
749 vid_width
, vid_height
, draw_osd_I8A8
);
752 static void flip_page(void)
755 mp_msg(MSGT_VO
, MSGL_DBG2
, "\nFLIP_PAGE VID:%u -> OUT:%u\n",
756 surface_render
[vid_surface_num
].surface
, output_surfaces
[surface_num
]);
758 vdp_st
= vdp_presentation_queue_display(vdp_flip_queue
, output_surfaces
[surface_num
],
759 vo_dwidth
, vo_dheight
,
761 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
763 surface_num
= (surface_num
+ 1) % NUM_OUTPUT_SURFACES
;
767 static int draw_slice(uint8_t *image
[], int stride
[], int w
, int h
,
771 struct vdpau_render_state
*rndr
= (struct vdpau_render_state
*)image
[0];
772 int max_refs
= image_format
== IMGFMT_VDPAU_H264
? rndr
->info
.h264
.num_ref_frames
: 2;
773 if (!IMGFMT_IS_VDPAU(image_format
))
775 if (decoder
== VDP_INVALID_HANDLE
|| decoder_max_refs
< max_refs
) {
776 VdpDecoderProfile vdp_decoder_profile
;
777 if (decoder
!= VDP_INVALID_HANDLE
)
778 vdp_decoder_destroy(decoder
);
779 decoder
= VDP_INVALID_HANDLE
;
780 switch (image_format
) {
781 case IMGFMT_VDPAU_MPEG1
:
782 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG1
;
784 case IMGFMT_VDPAU_MPEG2
:
785 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG2_MAIN
;
787 case IMGFMT_VDPAU_H264
:
788 vdp_decoder_profile
= VDP_DECODER_PROFILE_H264_HIGH
;
790 case IMGFMT_VDPAU_WMV3
:
791 vdp_decoder_profile
= VDP_DECODER_PROFILE_VC1_MAIN
;
793 case IMGFMT_VDPAU_VC1
:
794 vdp_decoder_profile
= VDP_DECODER_PROFILE_VC1_ADVANCED
;
797 vdp_st
= vdp_decoder_create(vdp_device
, vdp_decoder_profile
, vid_width
, vid_height
, max_refs
, &decoder
);
798 CHECK_ST_WARNING("Failed creating VDPAU decoder");
799 if (vdp_st
!= VDP_STATUS_OK
) {
800 decoder
= VDP_INVALID_HANDLE
;
801 decoder_max_refs
= 0;
804 decoder_max_refs
= max_refs
;
806 vdp_st
= vdp_decoder_render(decoder
, rndr
->surface
, (void *)&rndr
->info
, rndr
->bitstream_buffers_used
, rndr
->bitstream_buffers
);
807 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
812 static int draw_frame(uint8_t *src
[])
817 static struct vdpau_render_state
*get_surface(int number
)
819 if (number
> MAX_VIDEO_SURFACES
)
821 if (surface_render
[number
].surface
== VDP_INVALID_HANDLE
) {
823 vdp_st
= vdp_video_surface_create(vdp_device
, vdp_chroma_type
,
824 vid_width
, vid_height
,
825 &surface_render
[number
].surface
);
826 CHECK_ST_WARNING("Error when calling vdp_video_surface_create")
827 if (vdp_st
!= VDP_STATUS_OK
)
830 mp_msg(MSGT_VO
, MSGL_DBG2
, "VID CREATE: %u\n", surface_render
[number
].surface
);
831 return &surface_render
[number
];
834 static uint32_t draw_image(mp_image_t
*mpi
)
836 if (IMGFMT_IS_VDPAU(image_format
)) {
837 struct vdpau_render_state
*rndr
= mpi
->priv
;
838 vid_surface_num
= rndr
- surface_render
;
839 } else if (!(mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)) {
841 void *destdata
[3] = {mpi
->planes
[0], mpi
->planes
[2], mpi
->planes
[1]};
842 struct vdpau_render_state
*rndr
= get_surface(0);
843 vid_surface_num
= rndr
- surface_render
;
844 vdp_st
= vdp_video_surface_put_bits_y_cb_cr(rndr
->surface
,
845 VDP_YCBCR_FORMAT_YV12
,
846 (const void *const*)destdata
,
847 mpi
->stride
); // pitch
848 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
850 top_field_first
= !!(mpi
->fields
& MP_IMGFIELD_TOP_FIRST
);
852 video_to_output_surface();
856 static uint32_t get_image(mp_image_t
*mpi
)
858 struct vdpau_render_state
*rndr
;
860 // no dr for non-decoding for now
861 if (!IMGFMT_IS_VDPAU(image_format
)) return VO_FALSE
;
862 if (mpi
->type
!= MP_IMGTYPE_NUMBERED
) return VO_FALSE
;
864 rndr
= get_surface(mpi
->number
);
866 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] no surfaces available in get_image\n");
867 // TODO: this probably breaks things forever, provide a dummy buffer?
870 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
871 mpi
->stride
[0] = mpi
->stride
[1] = mpi
->stride
[2] = 0;
872 mpi
->planes
[0] = mpi
->planes
[1] = mpi
->planes
[2] = NULL
;
873 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
874 mpi
->planes
[0] = (void *)rndr
;
880 static int query_format(uint32_t format
)
882 int default_flags
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_OSD
| VFCAP_EOSD
| VFCAP_EOSD_UNSCALED
;
885 return default_flags
| VOCAP_NOSLICES
;
886 case IMGFMT_VDPAU_MPEG1
:
887 case IMGFMT_VDPAU_MPEG2
:
888 case IMGFMT_VDPAU_H264
:
889 case IMGFMT_VDPAU_WMV3
:
890 case IMGFMT_VDPAU_VC1
:
891 return default_flags
;
896 static void DestroyVdpauObjects(void)
901 free_video_specific();
903 vdp_st
= vdp_presentation_queue_destroy(vdp_flip_queue
);
904 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy")
906 vdp_st
= vdp_presentation_queue_target_destroy(vdp_flip_target
);
907 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy")
909 for (i
= 0; i
<= NUM_OUTPUT_SURFACES
; i
++) {
910 vdp_st
= vdp_output_surface_destroy(output_surfaces
[i
]);
911 output_surfaces
[i
] = VDP_INVALID_HANDLE
;
912 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy")
915 for (i
= 0; i
<eosd_surface_count
; i
++) {
916 if (eosd_surfaces
[i
].surface
!= VDP_INVALID_HANDLE
) {
917 vdp_st
= vdp_bitmap_surface_destroy(eosd_surfaces
[i
].surface
);
918 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy")
920 eosd_surfaces
[i
].surface
= VDP_INVALID_HANDLE
;
923 vdp_st
= vdp_device_destroy(vdp_device
);
924 CHECK_ST_WARNING("Error when calling vdp_device_destroy")
927 static void uninit(void)
929 if (!vo_config_count
)
933 /* Destroy all vdpau objects */
934 DestroyVdpauObjects();
940 eosd_surfaces
= NULL
;
949 dlclose(vdpau_lib_handle
);
952 static const opt_t subopts
[] = {
953 {"deint", OPT_ARG_INT
, &deint
, (opt_test_f
)int_non_neg
},
954 {"pullup", OPT_ARG_BOOL
, &pullup
, NULL
},
955 {"denoise", OPT_ARG_FLOAT
, &denoise
, NULL
},
956 {"sharpen", OPT_ARG_FLOAT
, &sharpen
, NULL
},
960 static const char help_msg
[] =
961 "\n-vo vdpau command line help:\n"
962 "Example: mplayer -vo vdpau:deint=2\n"
964 " deint (all modes > 0 respect -field-dominance)\n"
965 " 0: no deinterlacing\n"
966 " 1: only show first field\n"
967 " 2: bob deinterlacing (current fallback)\n"
968 " 3: temporal deinterlacing (not yet working)\n"
969 " 4: temporal-spatial deinterlacing (not yet working)\n"
971 " Try to apply inverse-telecine (needs deinterlacing, not working)\n"
973 " Apply denoising, argument is strength from 0.0 to 1.0\n"
975 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
978 static int preinit(const char *arg
)
981 static const char *vdpaulibrary
= "libvdpau.so.1";
982 static const char *vdpau_device_create
= "vdp_device_create_x11";
989 if (subopt_parse(arg
, subopts
) != 0) {
990 mp_msg(MSGT_VO
, MSGL_FATAL
, help_msg
);
996 vdpau_lib_handle
= dlopen(vdpaulibrary
, RTLD_LAZY
);
997 if (!vdpau_lib_handle
) {
998 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Could not open dynamic library %s\n",
1002 vdp_device_create
= dlsym(vdpau_lib_handle
, vdpau_device_create
);
1003 if (!vdp_device_create
) {
1004 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Could not find function %s in %s\n",
1005 vdpau_device_create
, vdpaulibrary
);
1008 if (!vo_init() || win_x11_init_vdpau_procs())
1011 decoder
= VDP_INVALID_HANDLE
;
1012 for (i
= 0; i
< MAX_VIDEO_SURFACES
; i
++)
1013 surface_render
[i
].surface
= VDP_INVALID_HANDLE
;
1014 video_mixer
= VDP_INVALID_HANDLE
;
1015 for (i
= 0; i
<= NUM_OUTPUT_SURFACES
; i
++)
1016 output_surfaces
[i
] = VDP_INVALID_HANDLE
;
1017 vdp_flip_queue
= VDP_INVALID_HANDLE
;
1018 output_surface_width
= output_surface_height
= -1;
1020 // full grayscale palette.
1021 for (i
= 0; i
< PALETTE_SIZE
; ++i
)
1022 palette
[i
] = (i
<< 16) | (i
<< 8) | i
;
1024 index_data_size
= 0;
1026 eosd_surface_count
= eosd_render_count
= 0;
1027 eosd_surfaces
= NULL
;
1028 eosd_targets
= NULL
;
1030 procamp
.struct_version
= VDP_PROCAMP_VERSION
;
1031 procamp
.brightness
= 0.0;
1032 procamp
.contrast
= 1.0;
1033 procamp
.saturation
= 1.0;
1039 static int get_equalizer(char *name
, int *value
) {
1040 if (!strcasecmp(name
, "brightness"))
1041 *value
= procamp
.brightness
* 100;
1042 else if (!strcasecmp(name
, "contrast"))
1043 *value
= (procamp
.contrast
-1.0) * 100;
1044 else if (!strcasecmp(name
, "saturation"))
1045 *value
= (procamp
.saturation
-1.0) * 100;
1046 else if (!strcasecmp(name
, "hue"))
1047 *value
= procamp
.hue
* 100 / M_PI
;
1053 static int set_equalizer(char *name
, int value
) {
1055 VdpCSCMatrix matrix
;
1056 static const VdpVideoMixerAttribute attributes
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX
};
1057 const void *attribute_values
[] = {&matrix
};
1059 if (!strcasecmp(name
, "brightness"))
1060 procamp
.brightness
= value
/ 100.0;
1061 else if (!strcasecmp(name
, "contrast"))
1062 procamp
.contrast
= value
/ 100.0 + 1.0;
1063 else if (!strcasecmp(name
, "saturation"))
1064 procamp
.saturation
= value
/ 100.0 + 1.0;
1065 else if (!strcasecmp(name
, "hue"))
1066 procamp
.hue
= value
/ 100.0 * M_PI
;
1070 vdp_st
= vdp_generate_csc_matrix(&procamp
, VDP_COLOR_STANDARD_ITUR_BT_601
,
1072 CHECK_ST_WARNING("Error when generating CSC matrix")
1073 vdp_st
= vdp_video_mixer_set_attribute_values(video_mixer
, 1, attributes
,
1075 CHECK_ST_WARNING("Error when setting CSC matrix")
1079 static int control(uint32_t request
, void *data
, ...)
1082 case VOCTRL_GET_DEINTERLACE
:
1083 *(int*)data
= deint
;
1085 case VOCTRL_SET_DEINTERLACE
:
1086 deint
= *(int*)data
;
1091 return (int_pause
= 1);
1093 return (int_pause
= 0);
1094 case VOCTRL_QUERY_FORMAT
:
1095 return query_format(*(uint32_t *)data
);
1096 case VOCTRL_GET_IMAGE
:
1097 return get_image(data
);
1098 case VOCTRL_DRAW_IMAGE
:
1099 return draw_image(data
);
1100 case VOCTRL_GUISUPPORT
:
1106 case VOCTRL_FULLSCREEN
:
1107 vo_x11_fullscreen();
1110 case VOCTRL_GET_PANSCAN
:
1112 case VOCTRL_SET_PANSCAN
:
1115 case VOCTRL_SET_EQUALIZER
: {
1120 value
= va_arg(ap
, int);
1123 return set_equalizer(data
, value
);
1125 case VOCTRL_GET_EQUALIZER
: {
1130 value
= va_arg(ap
, int *);
1133 return get_equalizer(data
, value
);
1138 case VOCTRL_UPDATE_SCREENINFO
:
1139 update_xinerama_info();
1141 case VOCTRL_DRAW_EOSD
:
1144 generate_eosd(data
);
1147 case VOCTRL_GET_EOSD_RES
: {
1148 mp_eosd_res_t
*r
= data
;
1149 r
->mt
= r
->mb
= r
->ml
= r
->mr
= 0;
1151 r
->w
= vo_screenwidth
;
1152 r
->h
= vo_screenheight
;
1153 r
->ml
= r
->mr
= border_x
;
1154 r
->mt
= r
->mb
= border_y
;