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 VdpVideoSurface deint_surfaces
[3];
151 static mp_image_t
*deint_mpi
[3];
152 static int output_surface_width
, output_surface_height
;
154 static VdpVideoMixer video_mixer
;
156 static int deint_type
;
157 static int deint_counter
;
158 static int deint_buffer_past_frames
;
160 static float denoise
;
161 static float sharpen
;
162 static int chroma_deint
;
163 static int top_field_first
;
165 static VdpDecoder decoder
;
166 static int decoder_max_refs
;
168 static VdpRect src_rect_vid
;
169 static VdpRect out_rect_vid
;
170 static int border_x
, border_y
;
172 static struct vdpau_render_state surface_render
[MAX_VIDEO_SURFACES
];
173 static int surface_num
;
174 static int vid_surface_num
;
175 static uint32_t vid_width
, vid_height
;
176 static uint32_t image_format
;
177 static const VdpChromaType vdp_chroma_type
= VDP_CHROMA_TYPE_420
;
180 static unsigned char *index_data
;
181 static int index_data_size
;
182 static uint32_t palette
[PALETTE_SIZE
];
187 VdpBitmapSurface surface
;
193 // List of surfaces to be rendered
195 VdpBitmapSurface surface
;
201 static int eosd_render_count
;
202 static int eosd_surface_count
;
205 static VdpProcamp procamp
;
210 static int visible_buf
;
211 static int int_pause
;
213 static void draw_eosd(void);
215 static void video_to_output_surface(void)
220 if (vid_surface_num
< 0 || deint_surfaces
[0] == VDP_INVALID_HANDLE
)
223 for (i
= 0; i
<= !!(deint
> 1); i
++) {
224 int field
= VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME
;
225 VdpOutputSurface output_surface
;
232 field
= top_field_first
== i
?
233 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD
:
234 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD
;
235 output_surface
= output_surfaces
[surface_num
];
236 vdp_st
= vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue
,
239 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
241 vdp_st
= vdp_video_mixer_render(video_mixer
, VDP_INVALID_HANDLE
, 0,
242 field
, 2, deint_surfaces
+ 1,
244 1, &surface_render
[vid_surface_num
].surface
,
247 NULL
, &out_rect_vid
, 0, NULL
);
248 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
252 static void resize(void)
256 struct vo_rect src_rect
;
257 struct vo_rect dst_rect
;
258 struct vo_rect borders
;
259 calc_src_dst_rects(vid_width
, vid_height
, &src_rect
, &dst_rect
, &borders
, NULL
);
260 out_rect_vid
.x0
= dst_rect
.left
;
261 out_rect_vid
.x1
= dst_rect
.right
;
262 out_rect_vid
.y0
= dst_rect
.top
;
263 out_rect_vid
.y1
= dst_rect
.bottom
;
264 src_rect_vid
.x0
= src_rect
.left
;
265 src_rect_vid
.x1
= src_rect
.right
;
266 src_rect_vid
.y0
= src_rect
.top
;
267 src_rect_vid
.y1
= src_rect
.bottom
;
268 border_x
= borders
.left
;
269 border_y
= borders
.top
;
270 #ifdef CONFIG_FREETYPE
271 // adjust font size to display size
274 vo_osd_changed(OSDTYPE_OSD
);
276 if (output_surface_width
< vo_dwidth
|| output_surface_height
< vo_dheight
) {
277 if (output_surface_width
< vo_dwidth
) {
278 output_surface_width
+= output_surface_width
>> 1;
279 output_surface_width
= FFMAX(output_surface_width
, vo_dwidth
);
281 if (output_surface_height
< vo_dheight
) {
282 output_surface_height
+= output_surface_height
>> 1;
283 output_surface_height
= FFMAX(output_surface_height
, vo_dheight
);
285 // Creation of output_surfaces
286 for (i
= 0; i
<= NUM_OUTPUT_SURFACES
; i
++) {
287 if (output_surfaces
[i
] != VDP_INVALID_HANDLE
)
288 vdp_output_surface_destroy(output_surfaces
[i
]);
289 vdp_st
= vdp_output_surface_create(vdp_device
, VDP_RGBA_FORMAT_B8G8R8A8
,
290 output_surface_width
, output_surface_height
,
291 &output_surfaces
[i
]);
292 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
293 mp_msg(MSGT_VO
, MSGL_DBG2
, "OUT CREATE: %u\n", output_surfaces
[i
]);
296 video_to_output_surface();
301 /* Initialize vdp_get_proc_address, called from preinit() */
302 static int win_x11_init_vdpau_procs(void)
306 struct vdp_function
{
311 const struct vdp_function
*dsc
;
313 static const struct vdp_function vdp_func
[] = {
314 {VDP_FUNC_ID_GET_ERROR_STRING
, &vdp_get_error_string
},
315 {VDP_FUNC_ID_DEVICE_DESTROY
, &vdp_device_destroy
},
316 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE
, &vdp_video_surface_create
},
317 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY
, &vdp_video_surface_destroy
},
318 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR
,
319 &vdp_video_surface_put_bits_y_cb_cr
},
320 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE
,
321 &vdp_output_surface_put_bits_native
},
322 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE
, &vdp_output_surface_create
},
323 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY
, &vdp_output_surface_destroy
},
324 {VDP_FUNC_ID_VIDEO_MIXER_CREATE
, &vdp_video_mixer_create
},
325 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY
, &vdp_video_mixer_destroy
},
326 {VDP_FUNC_ID_VIDEO_MIXER_RENDER
, &vdp_video_mixer_render
},
327 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES
,
328 &vdp_video_mixer_set_feature_enables
},
329 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES
,
330 &vdp_video_mixer_set_attribute_values
},
331 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY
,
332 &vdp_presentation_queue_target_destroy
},
333 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE
, &vdp_presentation_queue_create
},
334 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY
,
335 &vdp_presentation_queue_destroy
},
336 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY
,
337 &vdp_presentation_queue_display
},
338 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE
,
339 &vdp_presentation_queue_block_until_surface_idle
},
340 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11
,
341 &vdp_presentation_queue_target_create_x11
},
342 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE
,
343 &vdp_output_surface_render_output_surface
},
344 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED
,
345 &vdp_output_surface_put_bits_indexed
},
346 {VDP_FUNC_ID_DECODER_CREATE
, &vdp_decoder_create
},
347 {VDP_FUNC_ID_DECODER_RENDER
, &vdp_decoder_render
},
348 {VDP_FUNC_ID_DECODER_DESTROY
, &vdp_decoder_destroy
},
349 {VDP_FUNC_ID_BITMAP_SURFACE_CREATE
, &vdp_bitmap_surface_create
},
350 {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY
, &vdp_bitmap_surface_destroy
},
351 {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE
,
352 &vdp_bitmap_surface_putbits_native
},
353 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE
,
354 &vdp_output_surface_render_bitmap_surface
},
355 {VDP_FUNC_ID_GENERATE_CSC_MATRIX
, &vdp_generate_csc_matrix
},
359 vdp_st
= vdp_device_create(mDisplay
, mScreen
,
360 &vdp_device
, &vdp_get_proc_address
);
361 if (vdp_st
!= VDP_STATUS_OK
) {
362 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st
);
366 for (dsc
= vdp_func
; dsc
->pointer
; dsc
++) {
367 vdp_st
= vdp_get_proc_address(vdp_device
, dsc
->id
, dsc
->pointer
);
368 if (vdp_st
!= VDP_STATUS_OK
) {
369 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
) : "?");
376 /* Initialize vdpau_flip_queue, called from config() */
377 static int win_x11_init_vdpau_flip_queue(void)
381 vdp_st
= vdp_presentation_queue_target_create_x11(vdp_device
, vo_window
,
383 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
385 vdp_st
= vdp_presentation_queue_create(vdp_device
, vdp_flip_target
,
387 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
392 static int create_vdp_mixer(VdpChromaType vdp_chroma_type
) {
393 #define VDP_NUM_MIXER_PARAMETER 3
394 #define MAX_NUM_FEATURES 5
397 int feature_count
= 0;
398 VdpVideoMixerFeature features
[MAX_NUM_FEATURES
];
399 VdpBool feature_enables
[MAX_NUM_FEATURES
];
400 static const VdpVideoMixerAttribute denoise_attrib
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL
};
401 const void * const denoise_value
[] = {&denoise
};
402 static const VdpVideoMixerAttribute sharpen_attrib
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL
};
403 const void * const sharpen_value
[] = {&sharpen
};
404 static const VdpVideoMixerAttribute skip_chroma_attrib
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE
};
405 const uint8_t skip_chroma_value
= 1;
406 const void * const skip_chroma_value_ptr
[] = {&skip_chroma_value
};
407 static const VdpVideoMixerParameter parameters
[VDP_NUM_MIXER_PARAMETER
] = {
408 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH
,
409 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT
,
410 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
412 const void *const parameter_values
[VDP_NUM_MIXER_PARAMETER
] = {
417 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL
;
419 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL
;
421 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE
;
423 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION
;
425 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS
;
427 vdp_st
= vdp_video_mixer_create(vdp_device
, feature_count
, features
,
428 VDP_NUM_MIXER_PARAMETER
,
429 parameters
, parameter_values
,
431 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
433 for (i
= 0; i
< feature_count
; i
++) feature_enables
[i
] = VDP_TRUE
;
435 feature_enables
[0] = VDP_FALSE
;
437 vdp_video_mixer_set_feature_enables(video_mixer
, feature_count
, features
, feature_enables
);
439 vdp_video_mixer_set_attribute_values(video_mixer
, 1, denoise_attrib
, denoise_value
);
441 vdp_video_mixer_set_attribute_values(video_mixer
, 1, sharpen_attrib
, sharpen_value
);
443 vdp_video_mixer_set_attribute_values(video_mixer
, 1, skip_chroma_attrib
, skip_chroma_value_ptr
);
448 // Free everything specific to a certain video file
449 static void free_video_specific(void) {
453 if (decoder
!= VDP_INVALID_HANDLE
)
454 vdp_decoder_destroy(decoder
);
455 decoder
= VDP_INVALID_HANDLE
;
456 decoder_max_refs
= -1;
458 for (i
= 0; i
< 3; i
++)
459 deint_surfaces
[i
] = VDP_INVALID_HANDLE
;
461 for (i
= 0; i
< 3; i
++)
463 deint_mpi
[i
]->usage_count
--;
467 for (i
= 0; i
< MAX_VIDEO_SURFACES
; i
++) {
468 if (surface_render
[i
].surface
!= VDP_INVALID_HANDLE
) {
469 vdp_st
= vdp_video_surface_destroy(surface_render
[i
].surface
);
470 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
472 surface_render
[i
].surface
= VDP_INVALID_HANDLE
;
475 if (video_mixer
!= VDP_INVALID_HANDLE
) {
476 vdp_st
= vdp_video_mixer_destroy(video_mixer
);
477 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
479 video_mixer
= VDP_INVALID_HANDLE
;
483 * connect to X server, create and map window, initialize all
484 * VDPAU objects, create different surfaces etc.
486 static int config(uint32_t width
, uint32_t height
, uint32_t d_width
,
487 uint32_t d_height
, uint32_t flags
, char *title
,
491 XSetWindowAttributes xswa
;
492 XWindowAttributes attribs
;
493 unsigned long xswamask
;
497 int vm
= flags
& VOFLAG_MODESWITCHING
;
500 image_format
= format
;
507 guiGetEvent(guiSetShVideo
, 0); // the GUI will set up / resize our window
516 XGetWindowAttributes(mDisplay
, DefaultRootWindow(mDisplay
), &attribs
);
517 depth
= attribs
.depth
;
518 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32)
520 XMatchVisualInfo(mDisplay
, mScreen
, depth
, TrueColor
, &vinfo
);
522 xswa
.background_pixel
= 0;
523 xswa
.border_pixel
= 0;
524 xswamask
= CWBackPixel
| CWBorderPixel
;
526 vo_x11_create_vo_window(&vinfo
, vo_dx
, vo_dy
, d_width
, d_height
,
527 flags
, CopyFromParent
, "vdpau", title
);
528 XChangeWindowAttributes(mDisplay
, vo_window
, xswamask
, &xswa
);
532 /* Grab the mouse pointer in our window */
534 XGrabPointer(mDisplay
, vo_window
, True
, 0,
535 GrabModeAsync
, GrabModeAsync
,
536 vo_window
, None
, CurrentTime
);
537 XSetInputFocus(mDisplay
, vo_window
, RevertToNone
, CurrentTime
);
542 if ((flags
& VOFLAG_FULLSCREEN
) && WinID
<= 0)
545 /* -----VDPAU related code here -------- */
547 free_video_specific();
549 if (vdp_flip_queue
== VDP_INVALID_HANDLE
&& win_x11_init_vdpau_flip_queue())
552 // video width and height
556 if (create_vdp_mixer(vdp_chroma_type
))
560 vid_surface_num
= -1;
566 static void check_events(void)
568 int e
= vo_x11_check_events(mDisplay
);
570 if (e
& VO_EVENT_RESIZE
)
573 if ((e
& VO_EVENT_EXPOSE
|| e
& VO_EVENT_RESIZE
) && int_pause
) {
574 /* did we already draw a buffer */
576 /* redraw the last visible buffer */
578 vdp_st
= vdp_presentation_queue_display(vdp_flip_queue
,
579 output_surfaces
[surface_num
],
580 vo_dwidth
, vo_dheight
,
582 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
587 static void draw_osd_I8A8(int x0
,int y0
, int w
,int h
, unsigned char *src
,
588 unsigned char *srca
, int stride
)
590 VdpOutputSurface output_surface
= output_surfaces
[surface_num
];
594 int index_data_size_required
;
595 VdpRect output_indexed_rect_vid
;
596 VdpOutputSurfaceRenderBlendState blend_state
;
601 index_data_size_required
= 2*w
*h
;
602 if (index_data_size
< index_data_size_required
) {
603 index_data
= realloc(index_data
, index_data_size_required
);
604 index_data_size
= index_data_size_required
;
607 // index_data creation, component order - I, A, I, A, .....
608 for (i
= 0; i
< h
; i
++)
609 for (j
= 0; j
< w
; j
++) {
610 index_data
[i
*2*w
+ j
*2] = src
[i
*stride
+j
];
611 index_data
[i
*2*w
+ j
*2 + 1] = -srca
[i
*stride
+j
];
614 output_indexed_rect_vid
.x0
= x0
;
615 output_indexed_rect_vid
.y0
= y0
;
616 output_indexed_rect_vid
.x1
= x0
+ w
;
617 output_indexed_rect_vid
.y1
= y0
+ h
;
621 // write source_data to osd_surface.
622 vdp_st
= vdp_output_surface_put_bits_indexed(osd_surface
,
623 VDP_INDEXED_FORMAT_I8A8
,
624 (const void *const*)&index_data
,
626 &output_indexed_rect_vid
,
627 VDP_COLOR_TABLE_FORMAT_B8G8R8X8
,
629 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed")
631 blend_state
.struct_version
= VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION
;
632 blend_state
.blend_factor_source_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
;
633 blend_state
.blend_factor_source_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
;
634 blend_state
.blend_factor_destination_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
;
635 blend_state
.blend_factor_destination_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
;
636 blend_state
.blend_equation_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
637 blend_state
.blend_equation_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
639 vdp_st
= vdp_output_surface_render_output_surface(output_surface
,
640 &output_indexed_rect_vid
,
642 &output_indexed_rect_vid
,
645 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0
);
646 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
649 static void draw_eosd(void) {
651 VdpOutputSurface output_surface
= output_surfaces
[surface_num
];
652 VdpOutputSurfaceRenderBlendState blend_state
;
655 blend_state
.struct_version
= VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION
;
656 blend_state
.blend_factor_source_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
;
657 blend_state
.blend_factor_source_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
;
658 blend_state
.blend_factor_destination_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
;
659 blend_state
.blend_factor_destination_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
;
660 blend_state
.blend_equation_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
661 blend_state
.blend_equation_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
;
663 for (i
=0; i
<eosd_render_count
; i
++) {
664 vdp_st
= vdp_output_surface_render_bitmap_surface(
665 output_surface
, &eosd_targets
[i
].dest
,
666 eosd_targets
[i
].surface
, &eosd_targets
[i
].source
,
667 &eosd_targets
[i
].color
, &blend_state
,
668 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0
);
669 CHECK_ST_WARNING("EOSD: Error when rendering")
673 static void generate_eosd(mp_eosd_images_t
*imgs
) {
677 ass_image_t
*img
= imgs
->imgs
;
680 // Nothing changed, no need to redraw
681 if (imgs
->changed
== 0)
683 eosd_render_count
= 0;
684 // There's nothing to render!
688 if (imgs
->changed
== 1)
689 goto eosd_skip_upload
;
691 for (j
=0; j
<eosd_surface_count
; j
++)
692 eosd_surfaces
[j
].in_use
= 0;
694 for (i
= img
; i
; i
= i
->next
) {
695 // Try to reuse a suitable surface
697 for (j
=0; j
<eosd_surface_count
; j
++) {
698 if (eosd_surfaces
[j
].surface
!= VDP_INVALID_HANDLE
&& !eosd_surfaces
[j
].in_use
&&
699 eosd_surfaces
[j
].w
>= i
->w
&& eosd_surfaces
[j
].h
>= i
->h
) {
704 // None found, allocate a new surface
706 for (j
=0; j
<eosd_surface_count
; j
++) {
707 if (!eosd_surfaces
[j
].in_use
) {
708 if (eosd_surfaces
[j
].surface
!= VDP_INVALID_HANDLE
)
709 vdp_bitmap_surface_destroy(eosd_surfaces
[j
].surface
);
714 // Allocate new space for surface/target arrays
716 j
= found
= eosd_surface_count
;
717 eosd_surface_count
= eosd_surface_count
? eosd_surface_count
*2 : EOSD_SURFACES_INITIAL
;
718 eosd_surfaces
= realloc(eosd_surfaces
, eosd_surface_count
* sizeof(*eosd_surfaces
));
719 eosd_targets
= realloc(eosd_targets
, eosd_surface_count
* sizeof(*eosd_targets
));
720 for(j
=found
; j
<eosd_surface_count
; j
++) {
721 eosd_surfaces
[j
].surface
= VDP_INVALID_HANDLE
;
722 eosd_surfaces
[j
].in_use
= 0;
725 vdp_st
= vdp_bitmap_surface_create(vdp_device
, VDP_RGBA_FORMAT_A8
,
726 i
->w
, i
->h
, VDP_TRUE
, &eosd_surfaces
[found
].surface
);
727 CHECK_ST_WARNING("EOSD: error when creating surface")
728 eosd_surfaces
[found
].w
= i
->w
;
729 eosd_surfaces
[found
].h
= i
->h
;
731 eosd_surfaces
[found
].in_use
= 1;
732 eosd_targets
[eosd_render_count
].surface
= eosd_surfaces
[found
].surface
;
737 vdp_st
= vdp_bitmap_surface_putbits_native(eosd_targets
[eosd_render_count
].surface
,
738 (const void *) &i
->bitmap
, &i
->stride
, &destRect
);
739 CHECK_ST_WARNING("EOSD: putbits failed")
744 eosd_render_count
= 0;
745 for (i
= img
; i
; i
= i
->next
) {
746 // Render dest, color, etc.
747 eosd_targets
[eosd_render_count
].color
.alpha
= 1.0 - ((i
->color
>> 0) & 0xff) / 255.0;
748 eosd_targets
[eosd_render_count
].color
.blue
= ((i
->color
>> 8) & 0xff) / 255.0;
749 eosd_targets
[eosd_render_count
].color
.green
= ((i
->color
>> 16) & 0xff) / 255.0;
750 eosd_targets
[eosd_render_count
].color
.red
= ((i
->color
>> 24) & 0xff) / 255.0;
751 eosd_targets
[eosd_render_count
].dest
.x0
= i
->dst_x
;
752 eosd_targets
[eosd_render_count
].dest
.y0
= i
->dst_y
;
753 eosd_targets
[eosd_render_count
].dest
.x1
= i
->w
+ i
->dst_x
;
754 eosd_targets
[eosd_render_count
].dest
.y1
= i
->h
+ i
->dst_y
;
755 eosd_targets
[eosd_render_count
].source
.x0
= 0;
756 eosd_targets
[eosd_render_count
].source
.y0
= 0;
757 eosd_targets
[eosd_render_count
].source
.x1
= i
->w
;
758 eosd_targets
[eosd_render_count
].source
.y1
= i
->h
;
763 static void draw_osd(void)
765 mp_msg(MSGT_VO
, MSGL_DBG2
, "DRAW_OSD\n");
767 vo_draw_text_ext(vo_dwidth
, vo_dheight
, border_x
, border_y
, border_x
, border_y
,
768 vid_width
, vid_height
, draw_osd_I8A8
);
771 static void flip_page(void)
774 mp_msg(MSGT_VO
, MSGL_DBG2
, "\nFLIP_PAGE VID:%u -> OUT:%u\n",
775 surface_render
[vid_surface_num
].surface
, output_surfaces
[surface_num
]);
777 vdp_st
= vdp_presentation_queue_display(vdp_flip_queue
, output_surfaces
[surface_num
],
778 vo_dwidth
, vo_dheight
,
780 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
782 surface_num
= (surface_num
+ 1) % NUM_OUTPUT_SURFACES
;
786 static int draw_slice(uint8_t *image
[], int stride
[], int w
, int h
,
790 struct vdpau_render_state
*rndr
= (struct vdpau_render_state
*)image
[0];
791 int max_refs
= image_format
== IMGFMT_VDPAU_H264
? rndr
->info
.h264
.num_ref_frames
: 2;
792 if (!IMGFMT_IS_VDPAU(image_format
))
794 if (decoder
== VDP_INVALID_HANDLE
|| decoder_max_refs
< max_refs
) {
795 VdpDecoderProfile vdp_decoder_profile
;
796 if (decoder
!= VDP_INVALID_HANDLE
)
797 vdp_decoder_destroy(decoder
);
798 decoder
= VDP_INVALID_HANDLE
;
799 switch (image_format
) {
800 case IMGFMT_VDPAU_MPEG1
:
801 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG1
;
803 case IMGFMT_VDPAU_MPEG2
:
804 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG2_MAIN
;
806 case IMGFMT_VDPAU_H264
:
807 vdp_decoder_profile
= VDP_DECODER_PROFILE_H264_HIGH
;
808 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] Creating H264 hardware decoder for %d reference frames.\n", max_refs
);
810 case IMGFMT_VDPAU_WMV3
:
811 vdp_decoder_profile
= VDP_DECODER_PROFILE_VC1_MAIN
;
813 case IMGFMT_VDPAU_VC1
:
814 vdp_decoder_profile
= VDP_DECODER_PROFILE_VC1_ADVANCED
;
817 vdp_st
= vdp_decoder_create(vdp_device
, vdp_decoder_profile
, vid_width
, vid_height
, max_refs
, &decoder
);
818 CHECK_ST_WARNING("Failed creating VDPAU decoder");
819 if (vdp_st
!= VDP_STATUS_OK
) {
820 decoder
= VDP_INVALID_HANDLE
;
821 decoder_max_refs
= 0;
824 decoder_max_refs
= max_refs
;
826 vdp_st
= vdp_decoder_render(decoder
, rndr
->surface
, (void *)&rndr
->info
, rndr
->bitstream_buffers_used
, rndr
->bitstream_buffers
);
827 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
832 static int draw_frame(uint8_t *src
[])
837 static struct vdpau_render_state
*get_surface(int number
)
839 if (number
> MAX_VIDEO_SURFACES
)
841 if (surface_render
[number
].surface
== VDP_INVALID_HANDLE
) {
843 vdp_st
= vdp_video_surface_create(vdp_device
, vdp_chroma_type
,
844 vid_width
, vid_height
,
845 &surface_render
[number
].surface
);
846 CHECK_ST_WARNING("Error when calling vdp_video_surface_create")
847 if (vdp_st
!= VDP_STATUS_OK
)
850 mp_msg(MSGT_VO
, MSGL_DBG2
, "VID CREATE: %u\n", surface_render
[number
].surface
);
851 return &surface_render
[number
];
854 static uint32_t draw_image(mp_image_t
*mpi
)
856 if (IMGFMT_IS_VDPAU(image_format
)) {
857 struct vdpau_render_state
*rndr
= mpi
->priv
;
858 vid_surface_num
= rndr
- surface_render
;
859 if (deint_buffer_past_frames
) {
862 deint_mpi
[2]->usage_count
--;
863 deint_mpi
[2] = deint_mpi
[1];
864 deint_mpi
[1] = deint_mpi
[0];
867 } else if (!(mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)) {
869 void *destdata
[3] = {mpi
->planes
[0], mpi
->planes
[2], mpi
->planes
[1]};
870 struct vdpau_render_state
*rndr
= get_surface(deint_counter
);
871 deint_counter
= (deint_counter
+ 1) & 3;
872 vid_surface_num
= rndr
- surface_render
;
873 vdp_st
= vdp_video_surface_put_bits_y_cb_cr(rndr
->surface
,
874 VDP_YCBCR_FORMAT_YV12
,
875 (const void *const*)destdata
,
876 mpi
->stride
); // pitch
877 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
879 top_field_first
= !!(mpi
->fields
& MP_IMGFIELD_TOP_FIRST
);
882 deint_surfaces
[0] = surface_render
[vid_surface_num
].surface
;
883 video_to_output_surface();
884 deint_surfaces
[2] = deint_surfaces
[1];
885 deint_surfaces
[1] = deint_surfaces
[0];
886 deint_surfaces
[0] = surface_render
[vid_surface_num
].surface
;
890 static uint32_t get_image(mp_image_t
*mpi
)
892 struct vdpau_render_state
*rndr
;
894 // no dr for non-decoding for now
895 if (!IMGFMT_IS_VDPAU(image_format
)) return VO_FALSE
;
896 if (mpi
->type
!= MP_IMGTYPE_NUMBERED
) return VO_FALSE
;
898 rndr
= get_surface(mpi
->number
);
900 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] no surfaces available in get_image\n");
901 // TODO: this probably breaks things forever, provide a dummy buffer?
904 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
905 mpi
->stride
[0] = mpi
->stride
[1] = mpi
->stride
[2] = 0;
906 mpi
->planes
[0] = mpi
->planes
[1] = mpi
->planes
[2] = NULL
;
907 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
908 mpi
->planes
[0] = (void *)rndr
;
914 static int query_format(uint32_t format
)
916 int default_flags
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
| VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_OSD
| VFCAP_EOSD
| VFCAP_EOSD_UNSCALED
;
919 return default_flags
| VOCAP_NOSLICES
;
920 case IMGFMT_VDPAU_MPEG1
:
921 case IMGFMT_VDPAU_MPEG2
:
922 case IMGFMT_VDPAU_H264
:
923 case IMGFMT_VDPAU_WMV3
:
924 case IMGFMT_VDPAU_VC1
:
925 return default_flags
;
930 static void DestroyVdpauObjects(void)
935 free_video_specific();
937 vdp_st
= vdp_presentation_queue_destroy(vdp_flip_queue
);
938 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy")
940 vdp_st
= vdp_presentation_queue_target_destroy(vdp_flip_target
);
941 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy")
943 for (i
= 0; i
<= NUM_OUTPUT_SURFACES
; i
++) {
944 vdp_st
= vdp_output_surface_destroy(output_surfaces
[i
]);
945 output_surfaces
[i
] = VDP_INVALID_HANDLE
;
946 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy")
949 for (i
= 0; i
<eosd_surface_count
; i
++) {
950 if (eosd_surfaces
[i
].surface
!= VDP_INVALID_HANDLE
) {
951 vdp_st
= vdp_bitmap_surface_destroy(eosd_surfaces
[i
].surface
);
952 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy")
954 eosd_surfaces
[i
].surface
= VDP_INVALID_HANDLE
;
957 vdp_st
= vdp_device_destroy(vdp_device
);
958 CHECK_ST_WARNING("Error when calling vdp_device_destroy")
961 static void uninit(void)
963 if (!vo_config_count
)
967 /* Destroy all vdpau objects */
968 DestroyVdpauObjects();
974 eosd_surfaces
= NULL
;
983 dlclose(vdpau_lib_handle
);
986 static const opt_t subopts
[] = {
987 {"deint", OPT_ARG_INT
, &deint
, (opt_test_f
)int_non_neg
},
988 {"chroma-deint", OPT_ARG_BOOL
, &chroma_deint
, NULL
},
989 {"pullup", OPT_ARG_BOOL
, &pullup
, NULL
},
990 {"denoise", OPT_ARG_FLOAT
, &denoise
, NULL
},
991 {"sharpen", OPT_ARG_FLOAT
, &sharpen
, NULL
},
995 static const char help_msg
[] =
996 "\n-vo vdpau command line help:\n"
997 "Example: mplayer -vo vdpau:deint=2\n"
999 " deint (all modes > 0 respect -field-dominance)\n"
1000 " 0: no deinterlacing\n"
1001 " 1: only show first field\n"
1002 " 2: bob deinterlacing\n"
1003 " 3: temporal deinterlacing (resource-hungry)\n"
1004 " 4: temporal-spatial deinterlacing (very resource-hungry)\n"
1006 " Operate on luma and chroma when using temporal deinterlacing (default)\n"
1007 " Use nochroma-deint to speed up temporal deinterlacing\n"
1009 " Try to apply inverse-telecine (needs temporal deinterlacing)\n"
1011 " Apply denoising, argument is strength from 0.0 to 1.0\n"
1013 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
1016 static int preinit(const char *arg
)
1019 static const char *vdpaulibrary
= "libvdpau.so.1";
1020 static const char *vdpau_device_create
= "vdp_device_create_x11";
1025 deint_buffer_past_frames
= 0;
1026 deint_mpi
[0] = deint_mpi
[1] = deint_mpi
[2] = NULL
;
1031 if (subopt_parse(arg
, subopts
) != 0) {
1032 mp_msg(MSGT_VO
, MSGL_FATAL
, help_msg
);
1038 deint_buffer_past_frames
= 1;
1040 vdpau_lib_handle
= dlopen(vdpaulibrary
, RTLD_LAZY
);
1041 if (!vdpau_lib_handle
) {
1042 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Could not open dynamic library %s\n",
1046 vdp_device_create
= dlsym(vdpau_lib_handle
, vdpau_device_create
);
1047 if (!vdp_device_create
) {
1048 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Could not find function %s in %s\n",
1049 vdpau_device_create
, vdpaulibrary
);
1052 if (!vo_init() || win_x11_init_vdpau_procs())
1055 decoder
= VDP_INVALID_HANDLE
;
1056 for (i
= 0; i
< MAX_VIDEO_SURFACES
; i
++)
1057 surface_render
[i
].surface
= VDP_INVALID_HANDLE
;
1058 video_mixer
= VDP_INVALID_HANDLE
;
1059 for (i
= 0; i
<= NUM_OUTPUT_SURFACES
; i
++)
1060 output_surfaces
[i
] = VDP_INVALID_HANDLE
;
1061 vdp_flip_queue
= VDP_INVALID_HANDLE
;
1062 output_surface_width
= output_surface_height
= -1;
1064 // full grayscale palette.
1065 for (i
= 0; i
< PALETTE_SIZE
; ++i
)
1066 palette
[i
] = (i
<< 16) | (i
<< 8) | i
;
1068 index_data_size
= 0;
1070 eosd_surface_count
= eosd_render_count
= 0;
1071 eosd_surfaces
= NULL
;
1072 eosd_targets
= NULL
;
1074 procamp
.struct_version
= VDP_PROCAMP_VERSION
;
1075 procamp
.brightness
= 0.0;
1076 procamp
.contrast
= 1.0;
1077 procamp
.saturation
= 1.0;
1083 static int get_equalizer(char *name
, int *value
) {
1084 if (!strcasecmp(name
, "brightness"))
1085 *value
= procamp
.brightness
* 100;
1086 else if (!strcasecmp(name
, "contrast"))
1087 *value
= (procamp
.contrast
-1.0) * 100;
1088 else if (!strcasecmp(name
, "saturation"))
1089 *value
= (procamp
.saturation
-1.0) * 100;
1090 else if (!strcasecmp(name
, "hue"))
1091 *value
= procamp
.hue
* 100 / M_PI
;
1097 static int set_equalizer(char *name
, int value
) {
1099 VdpCSCMatrix matrix
;
1100 static const VdpVideoMixerAttribute attributes
[] = {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX
};
1101 const void *attribute_values
[] = {&matrix
};
1103 if (!strcasecmp(name
, "brightness"))
1104 procamp
.brightness
= value
/ 100.0;
1105 else if (!strcasecmp(name
, "contrast"))
1106 procamp
.contrast
= value
/ 100.0 + 1.0;
1107 else if (!strcasecmp(name
, "saturation"))
1108 procamp
.saturation
= value
/ 100.0 + 1.0;
1109 else if (!strcasecmp(name
, "hue"))
1110 procamp
.hue
= value
/ 100.0 * M_PI
;
1114 vdp_st
= vdp_generate_csc_matrix(&procamp
, VDP_COLOR_STANDARD_ITUR_BT_601
,
1116 CHECK_ST_WARNING("Error when generating CSC matrix")
1117 vdp_st
= vdp_video_mixer_set_attribute_values(video_mixer
, 1, attributes
,
1119 CHECK_ST_WARNING("Error when setting CSC matrix")
1123 static int control(uint32_t request
, void *data
, ...)
1126 case VOCTRL_GET_DEINTERLACE
:
1127 *(int*)data
= deint
;
1129 case VOCTRL_SET_DEINTERLACE
:
1130 deint
= *(int*)data
;
1133 if (deint_type
> 2) {
1135 VdpVideoMixerFeature features
[1] =
1137 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL
:
1138 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL
};
1139 VdpBool feature_enables
[1] = {deint
? VDP_TRUE
: VDP_FALSE
};
1140 vdp_st
= vdp_video_mixer_set_feature_enables(video_mixer
, 1,
1143 CHECK_ST_WARNING("Error changing deinterlacing settings")
1144 deint_buffer_past_frames
= 1;
1148 return (int_pause
= 1);
1150 return (int_pause
= 0);
1151 case VOCTRL_QUERY_FORMAT
:
1152 return query_format(*(uint32_t *)data
);
1153 case VOCTRL_GET_IMAGE
:
1154 return get_image(data
);
1155 case VOCTRL_DRAW_IMAGE
:
1156 return draw_image(data
);
1157 case VOCTRL_GUISUPPORT
:
1163 case VOCTRL_FULLSCREEN
:
1164 vo_x11_fullscreen();
1167 case VOCTRL_GET_PANSCAN
:
1169 case VOCTRL_SET_PANSCAN
:
1172 case VOCTRL_SET_EQUALIZER
: {
1177 value
= va_arg(ap
, int);
1180 return set_equalizer(data
, value
);
1182 case VOCTRL_GET_EQUALIZER
: {
1187 value
= va_arg(ap
, int *);
1190 return get_equalizer(data
, value
);
1195 case VOCTRL_UPDATE_SCREENINFO
:
1196 update_xinerama_info();
1198 case VOCTRL_DRAW_EOSD
:
1201 generate_eosd(data
);
1204 case VOCTRL_GET_EOSD_RES
: {
1205 mp_eosd_res_t
*r
= data
;
1206 r
->mt
= r
->mb
= r
->ml
= r
->mr
= 0;
1208 r
->w
= vo_screenwidth
;
1209 r
->h
= vo_screenheight
;
1210 r
->ml
= r
->mr
= border_x
;
1211 r
->mt
= r
->mb
= border_y
;