Add a default to switch(image_format), suppresses a warning after a future patch.
[mplayer/glamo.git] / libvo / vo_vdpau.c
blob6276ef02fd517259a534a8ca1aa7e69f2768da60
1 /*
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.
23 /**
24 * \defgroup VDPAU_Presentation VDPAU Presentation
25 * \ingroup Decoder
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.
32 * @{
35 #include <stdio.h>
36 #include <dlfcn.h>
38 #include "config.h"
39 #include "mp_msg.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "x11_common.h"
43 #include "aspect.h"
44 #include "sub.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 = {
58 "VDPAU with X11",
59 "vdpau",
60 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
64 LIBVO_EXTERN(vdpau)
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)); \
70 return -1; \
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
110 * system memory.
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;
145 static VdpPreemptionCallbackRegister *vdp_preemption_callback_register;
147 static void *vdpau_lib_handle;
148 /* output_surfaces[NUM_OUTPUT_SURFACES] is misused for OSD. */
149 #define osd_surface output_surfaces[NUM_OUTPUT_SURFACES]
150 static VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES + 1];
151 static VdpVideoSurface deint_surfaces[3];
152 static mp_image_t *deint_mpi[2];
153 static int output_surface_width, output_surface_height;
155 static VdpVideoMixer video_mixer;
156 static int deint;
157 static int deint_type;
158 static int deint_counter;
159 static int deint_buffer_past_frames;
160 static int pullup;
161 static float denoise;
162 static float sharpen;
163 static int colorspace;
164 static int chroma_deint;
165 static int force_mixer;
166 static int top_field_first;
167 static int flip;
168 static int hqscaling;
170 static VdpDecoder decoder;
171 static int decoder_max_refs;
173 static VdpRect src_rect_vid;
174 static VdpRect out_rect_vid;
175 static int border_x, border_y;
177 static struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES];
178 static int surface_num;
179 static int vid_surface_num;
180 static uint32_t vid_width, vid_height;
181 static uint32_t image_format;
182 static VdpChromaType vdp_chroma_type;
183 static VdpYCbCrFormat vdp_pixel_format;
185 static volatile int is_preempted;
187 /* draw_osd */
188 static unsigned char *index_data;
189 static int index_data_size;
190 static uint32_t palette[PALETTE_SIZE];
192 // EOSD
193 // Pool of surfaces
194 struct {
195 VdpBitmapSurface surface;
196 int w;
197 int h;
198 char in_use;
199 } *eosd_surfaces;
201 // List of surfaces to be rendered
202 struct {
203 VdpBitmapSurface surface;
204 VdpRect source;
205 VdpRect dest;
206 VdpColor color;
207 } *eosd_targets;
209 static int eosd_render_count;
210 static int eosd_surface_count;
212 // Video equalizer
213 static VdpProcamp procamp;
216 * X11 specific
218 static int visible_buf;
219 static int int_pause;
221 static void draw_eosd(void);
223 static void push_deint_surface(VdpVideoSurface surface)
225 deint_surfaces[2] = deint_surfaces[1];
226 deint_surfaces[1] = deint_surfaces[0];
227 deint_surfaces[0] = surface;
230 static void video_to_output_surface(void)
232 VdpTime dummy;
233 VdpStatus vdp_st;
234 int i;
235 if (vid_surface_num < 0)
236 return;
238 if (deint < 2 || deint_surfaces[0] == VDP_INVALID_HANDLE)
239 push_deint_surface(surface_render[vid_surface_num].surface);
241 for (i = 0; i <= !!(deint > 1); i++) {
242 int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
243 VdpOutputSurface output_surface;
244 if (i) {
245 draw_eosd();
246 draw_osd();
247 flip_page();
249 if (deint)
250 field = (top_field_first == i) ^ (deint > 1) ?
251 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
252 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
253 output_surface = output_surfaces[surface_num];
254 vdp_st = vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue,
255 output_surface,
256 &dummy);
257 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
259 vdp_st = vdp_video_mixer_render(video_mixer, VDP_INVALID_HANDLE, 0,
260 field, 2, deint_surfaces + 1,
261 deint_surfaces[0],
262 1, &surface_render[vid_surface_num].surface,
263 &src_rect_vid,
264 output_surface,
265 NULL, &out_rect_vid, 0, NULL);
266 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
267 push_deint_surface(surface_render[vid_surface_num].surface);
271 static void resize(void)
273 VdpStatus vdp_st;
274 int i;
275 struct vo_rect src_rect;
276 struct vo_rect dst_rect;
277 struct vo_rect borders;
278 calc_src_dst_rects(vid_width, vid_height, &src_rect, &dst_rect, &borders, NULL);
279 out_rect_vid.x0 = dst_rect.left;
280 out_rect_vid.x1 = dst_rect.right;
281 out_rect_vid.y0 = dst_rect.top;
282 out_rect_vid.y1 = dst_rect.bottom;
283 src_rect_vid.x0 = src_rect.left;
284 src_rect_vid.x1 = src_rect.right;
285 src_rect_vid.y0 = flip ? src_rect.bottom : src_rect.top;
286 src_rect_vid.y1 = flip ? src_rect.top : src_rect.bottom;
287 border_x = borders.left;
288 border_y = borders.top;
289 #ifdef CONFIG_FREETYPE
290 // adjust font size to display size
291 force_load_font = 1;
292 #endif
293 vo_osd_changed(OSDTYPE_OSD);
295 if (output_surface_width < vo_dwidth || output_surface_height < vo_dheight) {
296 if (output_surface_width < vo_dwidth) {
297 output_surface_width += output_surface_width >> 1;
298 output_surface_width = FFMAX(output_surface_width, vo_dwidth);
300 if (output_surface_height < vo_dheight) {
301 output_surface_height += output_surface_height >> 1;
302 output_surface_height = FFMAX(output_surface_height, vo_dheight);
304 // Creation of output_surfaces
305 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) {
306 if (output_surfaces[i] != VDP_INVALID_HANDLE)
307 vdp_output_surface_destroy(output_surfaces[i]);
308 vdp_st = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8,
309 output_surface_width, output_surface_height,
310 &output_surfaces[i]);
311 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
312 mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", output_surfaces[i]);
315 if (image_format == IMGFMT_BGRA) {
316 vdp_st = vdp_output_surface_render_output_surface(output_surfaces[surface_num],
317 NULL, VDP_INVALID_HANDLE,
318 NULL, NULL, NULL,
319 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
320 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
321 vdp_st = vdp_output_surface_render_output_surface(output_surfaces[1 - surface_num],
322 NULL, VDP_INVALID_HANDLE,
323 NULL, NULL, NULL,
324 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
325 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
326 } else
327 video_to_output_surface();
328 if (visible_buf)
329 flip_page();
332 static void preemption_callback(VdpDevice device, void *context)
334 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Display preemption detected\n");
335 is_preempted = 1;
338 /* Initialize vdp_get_proc_address, called from preinit() */
339 static int win_x11_init_vdpau_procs(void)
341 VdpStatus vdp_st;
343 struct vdp_function {
344 const int id;
345 void *pointer;
348 const struct vdp_function *dsc;
350 static const struct vdp_function vdp_func[] = {
351 {VDP_FUNC_ID_GET_ERROR_STRING, &vdp_get_error_string},
352 {VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy},
353 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE, &vdp_video_surface_create},
354 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, &vdp_video_surface_destroy},
355 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
356 &vdp_video_surface_put_bits_y_cb_cr},
357 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE,
358 &vdp_output_surface_put_bits_native},
359 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create},
360 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy},
361 {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create},
362 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy},
363 {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render},
364 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
365 &vdp_video_mixer_set_feature_enables},
366 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
367 &vdp_video_mixer_set_attribute_values},
368 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
369 &vdp_presentation_queue_target_destroy},
370 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, &vdp_presentation_queue_create},
371 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
372 &vdp_presentation_queue_destroy},
373 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
374 &vdp_presentation_queue_display},
375 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
376 &vdp_presentation_queue_block_until_surface_idle},
377 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
378 &vdp_presentation_queue_target_create_x11},
379 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE,
380 &vdp_output_surface_render_output_surface},
381 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED,
382 &vdp_output_surface_put_bits_indexed},
383 {VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create},
384 {VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render},
385 {VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy},
386 {VDP_FUNC_ID_BITMAP_SURFACE_CREATE, &vdp_bitmap_surface_create},
387 {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY, &vdp_bitmap_surface_destroy},
388 {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE,
389 &vdp_bitmap_surface_putbits_native},
390 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE,
391 &vdp_output_surface_render_bitmap_surface},
392 {VDP_FUNC_ID_GENERATE_CSC_MATRIX, &vdp_generate_csc_matrix},
393 {VDP_FUNC_ID_PREEMPTION_CALLBACK_REGISTER,
394 &vdp_preemption_callback_register},
395 {0, NULL}
398 vdp_st = vdp_device_create(mDisplay, mScreen,
399 &vdp_device, &vdp_get_proc_address);
400 if (vdp_st != VDP_STATUS_OK) {
401 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st);
402 return -1;
405 vdp_get_error_string = NULL;
406 for (dsc = vdp_func; dsc->pointer; dsc++) {
407 vdp_st = vdp_get_proc_address(vdp_device, dsc->id, dsc->pointer);
408 if (vdp_st != VDP_STATUS_OK) {
409 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_get_proc_address(function id %d): %s\n",
410 dsc->id, vdp_get_error_string ? vdp_get_error_string(vdp_st) : "?");
411 return -1;
414 vdp_st = vdp_preemption_callback_register(vdp_device,
415 preemption_callback, NULL);
416 CHECK_ST_ERROR("Error when calling vdp_preemption_callback_register")
418 return 0;
421 /* Initialize vdpau_flip_queue, called from config() */
422 static int win_x11_init_vdpau_flip_queue(void)
424 VdpStatus vdp_st;
426 vdp_st = vdp_presentation_queue_target_create_x11(vdp_device, vo_window,
427 &vdp_flip_target);
428 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
430 vdp_st = vdp_presentation_queue_create(vdp_device, vdp_flip_target,
431 &vdp_flip_queue);
432 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
434 return 0;
437 static int update_csc_matrix(void)
439 VdpStatus vdp_st;
440 VdpCSCMatrix matrix;
441 static const VdpVideoMixerAttribute attributes[] = {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX};
442 const void *attribute_values[] = {&matrix};
443 static const VdpColorStandard vdp_colors[] = {0, VDP_COLOR_STANDARD_ITUR_BT_601, VDP_COLOR_STANDARD_ITUR_BT_709, VDP_COLOR_STANDARD_SMPTE_240M};
444 static const char * const vdp_names[] = {NULL, "BT.601", "BT.709", "SMPTE-240M"};
445 int csp = colorspace;
447 if (!csp)
448 csp = vid_width >= 1280 || vid_height > 576 ? 2 : 1;
450 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Updating CSC matrix for %s\n",
451 vdp_names[csp]);
453 vdp_st = vdp_generate_csc_matrix(&procamp, vdp_colors[csp], &matrix);
454 CHECK_ST_WARNING("Error when generating CSC matrix")
456 vdp_st = vdp_video_mixer_set_attribute_values(video_mixer, 1, attributes,
457 attribute_values);
458 CHECK_ST_WARNING("Error when setting CSC matrix")
459 return VO_TRUE;
462 static int create_vdp_mixer(VdpChromaType vdp_chroma_type)
464 #define VDP_NUM_MIXER_PARAMETER 3
465 #define MAX_NUM_FEATURES 6
466 int i;
467 VdpStatus vdp_st;
468 int feature_count = 0;
469 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
470 VdpBool feature_enables[MAX_NUM_FEATURES];
471 static const VdpVideoMixerAttribute denoise_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL};
472 const void * const denoise_value[] = {&denoise};
473 static const VdpVideoMixerAttribute sharpen_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL};
474 const void * const sharpen_value[] = {&sharpen};
475 static const VdpVideoMixerAttribute skip_chroma_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
476 const uint8_t skip_chroma_value = 1;
477 const void * const skip_chroma_value_ptr[] = {&skip_chroma_value};
478 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
479 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
480 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
481 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
483 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
484 &vid_width,
485 &vid_height,
486 &vdp_chroma_type
488 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
489 if (deint == 4)
490 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
491 if (pullup)
492 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
493 if (denoise)
494 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
495 if (sharpen)
496 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
497 if (hqscaling)
498 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 + (hqscaling - 1);
500 vdp_st = vdp_video_mixer_create(vdp_device, feature_count, features,
501 VDP_NUM_MIXER_PARAMETER,
502 parameters, parameter_values,
503 &video_mixer);
504 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
506 for (i = 0; i < feature_count; i++)
507 feature_enables[i] = VDP_TRUE;
508 if (deint < 3)
509 feature_enables[0] = VDP_FALSE;
510 if (feature_count)
511 vdp_video_mixer_set_feature_enables(video_mixer, feature_count, features, feature_enables);
512 if (denoise)
513 vdp_video_mixer_set_attribute_values(video_mixer, 1, denoise_attrib, denoise_value);
514 if (sharpen)
515 vdp_video_mixer_set_attribute_values(video_mixer, 1, sharpen_attrib, sharpen_value);
516 if (!chroma_deint)
517 vdp_video_mixer_set_attribute_values(video_mixer, 1, skip_chroma_attrib, skip_chroma_value_ptr);
519 update_csc_matrix();
520 return 0;
523 // Free everything specific to a certain video file
524 static void free_video_specific(void)
526 int i;
527 VdpStatus vdp_st;
529 if (decoder != VDP_INVALID_HANDLE)
530 vdp_decoder_destroy(decoder);
531 decoder = VDP_INVALID_HANDLE;
532 decoder_max_refs = -1;
534 for (i = 0; i < 3; i++)
535 deint_surfaces[i] = VDP_INVALID_HANDLE;
537 for (i = 0; i < 2; i++)
538 if (deint_mpi[i]) {
539 deint_mpi[i]->usage_count--;
540 deint_mpi[i] = NULL;
543 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
544 if (surface_render[i].surface != VDP_INVALID_HANDLE) {
545 vdp_st = vdp_video_surface_destroy(surface_render[i].surface);
546 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
548 surface_render[i].surface = VDP_INVALID_HANDLE;
551 if (video_mixer != VDP_INVALID_HANDLE) {
552 vdp_st = vdp_video_mixer_destroy(video_mixer);
553 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
555 video_mixer = VDP_INVALID_HANDLE;
558 static int create_vdp_decoder(int max_refs)
560 VdpStatus vdp_st;
561 VdpDecoderProfile vdp_decoder_profile;
562 if (decoder != VDP_INVALID_HANDLE)
563 vdp_decoder_destroy(decoder);
564 switch (image_format) {
565 case IMGFMT_VDPAU_MPEG1:
566 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
567 break;
568 case IMGFMT_VDPAU_MPEG2:
569 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
570 break;
571 case IMGFMT_VDPAU_H264:
572 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
573 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder for %d reference frames.\n", max_refs);
574 break;
575 case IMGFMT_VDPAU_WMV3:
576 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
577 break;
578 case IMGFMT_VDPAU_VC1:
579 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
580 break;
581 default:
582 goto err_out;
584 vdp_st = vdp_decoder_create(vdp_device, vdp_decoder_profile,
585 vid_width, vid_height, max_refs, &decoder);
586 CHECK_ST_WARNING("Failed creating VDPAU decoder");
587 if (vdp_st != VDP_STATUS_OK) {
588 err_out:
589 decoder = VDP_INVALID_HANDLE;
590 decoder_max_refs = 0;
591 return 0;
593 decoder_max_refs = max_refs;
594 return 1;
597 static void mark_vdpau_objects_uninitialized(void)
599 int i;
601 decoder = VDP_INVALID_HANDLE;
602 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
603 surface_render[i].surface = VDP_INVALID_HANDLE;
604 for (i = 0; i < 3; i++) {
605 deint_surfaces[i] = VDP_INVALID_HANDLE;
606 if (i < 2 && deint_mpi[i])
607 deint_mpi[i]->usage_count--;
608 deint_mpi[i] = NULL;
610 video_mixer = VDP_INVALID_HANDLE;
611 vdp_flip_queue = VDP_INVALID_HANDLE;
612 vdp_flip_target = VDP_INVALID_HANDLE;
613 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++)
614 output_surfaces[i] = VDP_INVALID_HANDLE;
615 vdp_device = VDP_INVALID_HANDLE;
616 for (i = 0; i < eosd_surface_count; i++)
617 eosd_surfaces[i].surface = VDP_INVALID_HANDLE;
618 output_surface_width = output_surface_height = -1;
619 eosd_render_count = 0;
620 visible_buf = 0;
623 static int handle_preemption(void)
625 if (!is_preempted)
626 return 0;
627 is_preempted = 0;
628 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Attempting to recover from preemption.\n");
629 mark_vdpau_objects_uninitialized();
630 if (win_x11_init_vdpau_procs() < 0 ||
631 win_x11_init_vdpau_flip_queue() < 0 ||
632 create_vdp_mixer(vdp_chroma_type) < 0) {
633 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Recovering from preemption failed\n");
634 is_preempted = 1;
635 return -1;
637 resize();
638 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n");
639 return 1;
643 * connect to X server, create and map window, initialize all
644 * VDPAU objects, create different surfaces etc.
646 static int config(uint32_t width, uint32_t height, uint32_t d_width,
647 uint32_t d_height, uint32_t flags, char *title,
648 uint32_t format)
650 XVisualInfo vinfo;
651 XSetWindowAttributes xswa;
652 XWindowAttributes attribs;
653 unsigned long xswamask;
654 int depth;
656 #ifdef CONFIG_XF86VM
657 int vm = flags & VOFLAG_MODESWITCHING;
658 #endif
659 flip = flags & VOFLAG_FLIPPING;
661 image_format = format;
662 vid_width = width;
663 vid_height = height;
664 free_video_specific();
665 if (IMGFMT_IS_VDPAU(image_format) && !create_vdp_decoder(2))
666 return -1;
668 int_pause = 0;
669 visible_buf = 0;
671 #ifdef CONFIG_GUI
672 if (use_gui)
673 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize our window
674 else
675 #endif
677 #ifdef CONFIG_XF86VM
678 if (vm)
679 vo_vm_switch();
680 else
681 #endif
682 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
683 depth = attribs.depth;
684 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
685 depth = 24;
686 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
688 xswa.background_pixel = 0;
689 xswa.border_pixel = 0;
690 /* Do not use CWBackPixel: It leads to VDPAU errors after
691 aspect ratio changes. */
692 xswamask = CWBorderPixel;
694 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height,
695 flags, CopyFromParent, "vdpau", title);
696 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
698 #ifdef CONFIG_XF86VM
699 if (vm) {
700 /* Grab the mouse pointer in our window */
701 if (vo_grabpointer)
702 XGrabPointer(mDisplay, vo_window, True, 0,
703 GrabModeAsync, GrabModeAsync,
704 vo_window, None, CurrentTime);
705 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
707 #endif
710 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
711 vo_fs = 1;
713 /* -----VDPAU related code here -------- */
714 if (vdp_flip_queue == VDP_INVALID_HANDLE && win_x11_init_vdpau_flip_queue())
715 return -1;
717 vdp_chroma_type = VDP_CHROMA_TYPE_420;
718 switch (image_format) {
719 case IMGFMT_YV12:
720 case IMGFMT_I420:
721 case IMGFMT_IYUV:
722 vdp_pixel_format = VDP_YCBCR_FORMAT_YV12;
723 break;
724 case IMGFMT_NV12:
725 vdp_pixel_format = VDP_YCBCR_FORMAT_NV12;
726 break;
727 case IMGFMT_YUY2:
728 vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV;
729 vdp_chroma_type = VDP_CHROMA_TYPE_422;
730 break;
731 case IMGFMT_UYVY:
732 vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY;
733 vdp_chroma_type = VDP_CHROMA_TYPE_422;
735 if (create_vdp_mixer(vdp_chroma_type))
736 return -1;
738 surface_num = 0;
739 vid_surface_num = -1;
740 resize();
742 return 0;
745 static void check_events(void)
747 int e = vo_x11_check_events(mDisplay);
749 if (handle_preemption() < 0)
750 return;
752 if (e & VO_EVENT_RESIZE)
753 resize();
755 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && int_pause) {
756 /* did we already draw a buffer */
757 if (visible_buf) {
758 /* redraw the last visible buffer */
759 VdpStatus vdp_st;
760 vdp_st = vdp_presentation_queue_display(vdp_flip_queue,
761 output_surfaces[surface_num],
762 vo_dwidth, vo_dheight,
764 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
769 static void draw_osd_I8A8(int x0,int y0, int w,int h, unsigned char *src,
770 unsigned char *srca, int stride)
772 VdpOutputSurface output_surface = output_surfaces[surface_num];
773 VdpStatus vdp_st;
774 int i, j;
775 int pitch;
776 int index_data_size_required;
777 VdpRect output_indexed_rect_vid;
778 VdpOutputSurfaceRenderBlendState blend_state;
780 if (!w || !h)
781 return;
783 index_data_size_required = 2*w*h;
784 if (index_data_size < index_data_size_required) {
785 index_data = realloc(index_data, index_data_size_required);
786 index_data_size = index_data_size_required;
789 // index_data creation, component order - I, A, I, A, .....
790 for (i = 0; i < h; i++)
791 for (j = 0; j < w; j++) {
792 index_data[i*2*w + j*2] = src [i*stride + j];
793 index_data[i*2*w + j*2 + 1] = -srca[i*stride + j];
796 output_indexed_rect_vid.x0 = x0;
797 output_indexed_rect_vid.y0 = y0;
798 output_indexed_rect_vid.x1 = x0 + w;
799 output_indexed_rect_vid.y1 = y0 + h;
801 pitch = w*2;
803 // write source_data to osd_surface.
804 vdp_st = vdp_output_surface_put_bits_indexed(osd_surface,
805 VDP_INDEXED_FORMAT_I8A8,
806 (const void *const*)&index_data,
807 &pitch,
808 &output_indexed_rect_vid,
809 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
810 (void *)palette);
811 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed")
813 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
814 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
815 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
816 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
817 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
818 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
819 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
821 vdp_st = vdp_output_surface_render_output_surface(output_surface,
822 &output_indexed_rect_vid,
823 osd_surface,
824 &output_indexed_rect_vid,
825 NULL,
826 &blend_state,
827 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
828 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
831 static void draw_eosd(void)
833 VdpStatus vdp_st;
834 VdpOutputSurface output_surface = output_surfaces[surface_num];
835 VdpOutputSurfaceRenderBlendState blend_state;
836 int i;
838 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
839 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA;
840 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
841 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
842 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA;
843 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
844 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
846 for (i = 0; i < eosd_render_count; i++) {
847 vdp_st = vdp_output_surface_render_bitmap_surface(
848 output_surface, &eosd_targets[i].dest,
849 eosd_targets[i].surface, &eosd_targets[i].source,
850 &eosd_targets[i].color, &blend_state,
851 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
852 CHECK_ST_WARNING("EOSD: Error when rendering")
856 static void generate_eosd(mp_eosd_images_t *imgs)
858 VdpStatus vdp_st;
859 VdpRect destRect;
860 int j, found;
861 ass_image_t *img = imgs->imgs;
862 ass_image_t *i;
864 // Nothing changed, no need to redraw
865 if (imgs->changed == 0)
866 return;
867 eosd_render_count = 0;
868 // There's nothing to render!
869 if (!img)
870 return;
872 if (imgs->changed == 1)
873 goto eosd_skip_upload;
875 for (j = 0; j < eosd_surface_count; j++)
876 eosd_surfaces[j].in_use = 0;
878 for (i = img; i; i = i->next) {
879 // Try to reuse a suitable surface
880 found = -1;
881 for (j = 0; j < eosd_surface_count; j++) {
882 if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE && !eosd_surfaces[j].in_use &&
883 eosd_surfaces[j].w >= i->w && eosd_surfaces[j].h >= i->h) {
884 found = j;
885 break;
888 // None found, allocate a new surface
889 if (found < 0) {
890 for (j = 0; j < eosd_surface_count; j++) {
891 if (!eosd_surfaces[j].in_use) {
892 if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE)
893 vdp_bitmap_surface_destroy(eosd_surfaces[j].surface);
894 found = j;
895 break;
898 // Allocate new space for surface/target arrays
899 if (found < 0) {
900 j = found = eosd_surface_count;
901 eosd_surface_count = eosd_surface_count ? eosd_surface_count*2 : EOSD_SURFACES_INITIAL;
902 eosd_surfaces = realloc(eosd_surfaces, eosd_surface_count * sizeof(*eosd_surfaces));
903 eosd_targets = realloc(eosd_targets, eosd_surface_count * sizeof(*eosd_targets));
904 for (j = found; j < eosd_surface_count; j++) {
905 eosd_surfaces[j].surface = VDP_INVALID_HANDLE;
906 eosd_surfaces[j].in_use = 0;
909 vdp_st = vdp_bitmap_surface_create(vdp_device, VDP_RGBA_FORMAT_A8,
910 i->w, i->h, VDP_TRUE, &eosd_surfaces[found].surface);
911 CHECK_ST_WARNING("EOSD: error when creating surface")
912 eosd_surfaces[found].w = i->w;
913 eosd_surfaces[found].h = i->h;
915 eosd_surfaces[found].in_use = 1;
916 eosd_targets[eosd_render_count].surface = eosd_surfaces[found].surface;
917 destRect.x0 = 0;
918 destRect.y0 = 0;
919 destRect.x1 = i->w;
920 destRect.y1 = i->h;
921 vdp_st = vdp_bitmap_surface_putbits_native(eosd_targets[eosd_render_count].surface,
922 (const void *) &i->bitmap, &i->stride, &destRect);
923 CHECK_ST_WARNING("EOSD: putbits failed")
924 eosd_render_count++;
927 eosd_skip_upload:
928 eosd_render_count = 0;
929 for (i = img; i; i = i->next) {
930 // Render dest, color, etc.
931 eosd_targets[eosd_render_count].color.alpha = 1.0 - ((i->color >> 0) & 0xff) / 255.0;
932 eosd_targets[eosd_render_count].color.blue = ((i->color >> 8) & 0xff) / 255.0;
933 eosd_targets[eosd_render_count].color.green = ((i->color >> 16) & 0xff) / 255.0;
934 eosd_targets[eosd_render_count].color.red = ((i->color >> 24) & 0xff) / 255.0;
935 eosd_targets[eosd_render_count].dest.x0 = i->dst_x;
936 eosd_targets[eosd_render_count].dest.y0 = i->dst_y;
937 eosd_targets[eosd_render_count].dest.x1 = i->w + i->dst_x;
938 eosd_targets[eosd_render_count].dest.y1 = i->h + i->dst_y;
939 eosd_targets[eosd_render_count].source.x0 = 0;
940 eosd_targets[eosd_render_count].source.y0 = 0;
941 eosd_targets[eosd_render_count].source.x1 = i->w;
942 eosd_targets[eosd_render_count].source.y1 = i->h;
943 eosd_render_count++;
947 static void draw_osd(void)
949 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
951 if (handle_preemption() < 0)
952 return;
954 vo_draw_text_ext(vo_dwidth, vo_dheight, border_x, border_y, border_x, border_y,
955 vid_width, vid_height, draw_osd_I8A8);
958 static void flip_page(void)
960 VdpStatus vdp_st;
961 mp_msg(MSGT_VO, MSGL_DBG2, "\nFLIP_PAGE VID:%u -> OUT:%u\n",
962 surface_render[vid_surface_num].surface, output_surfaces[surface_num]);
964 if (handle_preemption() < 0)
965 return;
967 vdp_st = vdp_presentation_queue_display(vdp_flip_queue, output_surfaces[surface_num],
968 vo_dwidth, vo_dheight,
970 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
972 surface_num = (surface_num + 1) % NUM_OUTPUT_SURFACES;
973 visible_buf = 1;
976 static int draw_slice(uint8_t *image[], int stride[], int w, int h,
977 int x, int y)
979 VdpStatus vdp_st;
980 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
981 int max_refs = image_format == IMGFMT_VDPAU_H264 ? rndr->info.h264.num_ref_frames : 2;
983 if (handle_preemption() < 0)
984 return VO_TRUE;
986 if (!IMGFMT_IS_VDPAU(image_format))
987 return VO_FALSE;
988 if ((decoder == VDP_INVALID_HANDLE || decoder_max_refs < max_refs)
989 && !create_vdp_decoder(max_refs))
990 return VO_FALSE;
992 vdp_st = vdp_decoder_render(decoder, rndr->surface, (void *)&rndr->info, rndr->bitstream_buffers_used, rndr->bitstream_buffers);
993 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
994 return VO_TRUE;
998 static int draw_frame(uint8_t *src[])
1000 return VO_ERROR;
1003 static struct vdpau_render_state *get_surface(int number)
1005 if (number > MAX_VIDEO_SURFACES)
1006 return NULL;
1007 if (surface_render[number].surface == VDP_INVALID_HANDLE) {
1008 VdpStatus vdp_st;
1009 vdp_st = vdp_video_surface_create(vdp_device, vdp_chroma_type,
1010 vid_width, vid_height,
1011 &surface_render[number].surface);
1012 CHECK_ST_WARNING("Error when calling vdp_video_surface_create")
1013 if (vdp_st != VDP_STATUS_OK)
1014 return NULL;
1016 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n", surface_render[number].surface);
1017 return &surface_render[number];
1020 static uint32_t draw_image(mp_image_t *mpi)
1022 if (IMGFMT_IS_VDPAU(image_format)) {
1023 struct vdpau_render_state *rndr = mpi->priv;
1024 vid_surface_num = rndr - surface_render;
1025 if (deint_buffer_past_frames) {
1026 mpi->usage_count++;
1027 if (deint_mpi[1])
1028 deint_mpi[1]->usage_count--;
1029 deint_mpi[1] = deint_mpi[0];
1030 deint_mpi[0] = mpi;
1032 } else if (image_format == IMGFMT_BGRA) {
1033 VdpStatus vdp_st;
1034 VdpRect r = {0, 0, vid_width, vid_height};
1035 vdp_st = vdp_output_surface_put_bits_native(output_surfaces[2],
1036 (void const*const*)mpi->planes,
1037 mpi->stride, &r);
1038 CHECK_ST_ERROR("Error when calling vdp_output_surface_put_bits_native")
1039 vdp_st = vdp_output_surface_render_output_surface(output_surfaces[surface_num],
1040 &out_rect_vid,
1041 output_surfaces[2],
1042 &src_rect_vid, NULL, NULL,
1043 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1044 CHECK_ST_ERROR("Error when calling vdp_output_surface_render_output_surface")
1045 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1046 VdpStatus vdp_st;
1047 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
1048 struct vdpau_render_state *rndr = get_surface(deint_counter);
1049 deint_counter = (deint_counter + 1) % 3;
1050 vid_surface_num = rndr - surface_render;
1051 if (image_format == IMGFMT_NV12)
1052 destdata[1] = destdata[2];
1053 vdp_st = vdp_video_surface_put_bits_y_cb_cr(rndr->surface,
1054 vdp_pixel_format,
1055 (const void *const*)destdata,
1056 mpi->stride); // pitch
1057 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
1059 if (mpi->fields & MP_IMGFIELD_ORDERED)
1060 top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1061 else
1062 top_field_first = 1;
1064 video_to_output_surface();
1065 return VO_TRUE;
1068 static uint32_t get_image(mp_image_t *mpi)
1070 struct vdpau_render_state *rndr;
1072 // no dr for non-decoding for now
1073 if (!IMGFMT_IS_VDPAU(image_format))
1074 return VO_FALSE;
1075 if (mpi->type != MP_IMGTYPE_NUMBERED)
1076 return VO_FALSE;
1078 rndr = get_surface(mpi->number);
1079 if (!rndr) {
1080 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in get_image\n");
1081 // TODO: this probably breaks things forever, provide a dummy buffer?
1082 return VO_FALSE;
1084 mpi->flags |= MP_IMGFLAG_DIRECT;
1085 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1086 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1087 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1088 mpi->planes[0] = (void *)rndr;
1089 mpi->num_planes = 1;
1090 mpi->priv = rndr;
1091 return VO_TRUE;
1094 static int query_format(uint32_t format)
1096 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD | VFCAP_EOSD_UNSCALED | VFCAP_FLIP;
1097 switch (format) {
1098 case IMGFMT_BGRA:
1099 if (force_mixer)
1100 return 0;
1101 case IMGFMT_YV12:
1102 case IMGFMT_I420:
1103 case IMGFMT_IYUV:
1104 case IMGFMT_NV12:
1105 case IMGFMT_YUY2:
1106 case IMGFMT_UYVY:
1107 return default_flags | VOCAP_NOSLICES;
1108 case IMGFMT_VDPAU_MPEG1:
1109 case IMGFMT_VDPAU_MPEG2:
1110 case IMGFMT_VDPAU_H264:
1111 case IMGFMT_VDPAU_WMV3:
1112 case IMGFMT_VDPAU_VC1:
1113 return default_flags;
1115 return 0;
1118 static void DestroyVdpauObjects(void)
1120 int i;
1121 VdpStatus vdp_st;
1123 free_video_specific();
1125 vdp_st = vdp_presentation_queue_destroy(vdp_flip_queue);
1126 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy")
1128 vdp_st = vdp_presentation_queue_target_destroy(vdp_flip_target);
1129 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy")
1131 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) {
1132 vdp_st = vdp_output_surface_destroy(output_surfaces[i]);
1133 output_surfaces[i] = VDP_INVALID_HANDLE;
1134 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy")
1137 for (i = 0; i<eosd_surface_count; i++) {
1138 if (eosd_surfaces[i].surface != VDP_INVALID_HANDLE) {
1139 vdp_st = vdp_bitmap_surface_destroy(eosd_surfaces[i].surface);
1140 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy")
1142 eosd_surfaces[i].surface = VDP_INVALID_HANDLE;
1145 vdp_st = vdp_device_destroy(vdp_device);
1146 CHECK_ST_WARNING("Error when calling vdp_device_destroy")
1149 static void uninit(void)
1151 int i;
1153 if (!vo_config_count)
1154 return;
1155 visible_buf = 0;
1157 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
1158 // Allocated in ff_vdpau_add_data_chunk()
1159 av_freep(&surface_render[i].bitstream_buffers);
1160 surface_render[i].bitstream_buffers_allocated = 0;
1163 /* Destroy all vdpau objects */
1164 DestroyVdpauObjects();
1166 free(index_data);
1167 index_data = NULL;
1169 free(eosd_surfaces);
1170 eosd_surfaces = NULL;
1171 free(eosd_targets);
1172 eosd_targets = NULL;
1174 #ifdef CONFIG_XF86VM
1175 vo_vm_close();
1176 #endif
1177 vo_x11_uninit();
1179 dlclose(vdpau_lib_handle);
1182 static const opt_t subopts[] = {
1183 {"deint", OPT_ARG_INT, &deint, (opt_test_f)int_non_neg},
1184 {"chroma-deint", OPT_ARG_BOOL, &chroma_deint, NULL},
1185 {"pullup", OPT_ARG_BOOL, &pullup, NULL},
1186 {"denoise", OPT_ARG_FLOAT, &denoise, NULL},
1187 {"sharpen", OPT_ARG_FLOAT, &sharpen, NULL},
1188 {"colorspace", OPT_ARG_INT, &colorspace, NULL},
1189 {"force-mixer", OPT_ARG_BOOL, &force_mixer, NULL},
1190 {"hqscaling", OPT_ARG_INT, &hqscaling, (opt_test_f)int_non_neg},
1191 {NULL}
1194 static const char help_msg[] =
1195 "\n-vo vdpau command line help:\n"
1196 "Example: mplayer -vo vdpau:deint=2\n"
1197 "\nOptions:\n"
1198 " deint (all modes > 0 respect -field-dominance)\n"
1199 " 0: no deinterlacing\n"
1200 " 1: only show first field\n"
1201 " 2: bob deinterlacing\n"
1202 " 3: temporal deinterlacing (resource-hungry)\n"
1203 " 4: temporal-spatial deinterlacing (very resource-hungry)\n"
1204 " chroma-deint\n"
1205 " Operate on luma and chroma when using temporal deinterlacing (default)\n"
1206 " Use nochroma-deint to speed up temporal deinterlacing\n"
1207 " pullup\n"
1208 " Try to apply inverse-telecine (needs temporal deinterlacing)\n"
1209 " denoise\n"
1210 " Apply denoising, argument is strength from 0.0 to 1.0\n"
1211 " sharpen\n"
1212 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
1213 " colorspace\n"
1214 " 0: guess based on video resolution\n"
1215 " 1: ITU-R BT.601 (default)\n"
1216 " 2: ITU-R BT.709\n"
1217 " 3: SMPTE-240M\n"
1218 " hqscaling\n"
1219 " 0: default VDPAU scaler\n"
1220 " 1-9: high quality VDPAU scaler (needs capable hardware)\n"
1221 " force-mixer\n"
1222 " Use the VDPAU mixer (default)\n"
1223 " Use noforce-mixer to allow BGRA output (disables all above options)\n"
1226 static int preinit(const char *arg)
1228 int i;
1229 static const char *vdpaulibrary = "libvdpau.so.1";
1230 static const char *vdpau_device_create = "vdp_device_create_x11";
1232 deint = 0;
1233 deint_type = 3;
1234 deint_counter = 0;
1235 deint_buffer_past_frames = 0;
1236 deint_mpi[0] = deint_mpi[1] = NULL;
1237 chroma_deint = 1;
1238 pullup = 0;
1239 denoise = 0;
1240 sharpen = 0;
1241 colorspace = 1;
1242 force_mixer = 1;
1243 hqscaling = 0;
1244 if (subopt_parse(arg, subopts) != 0) {
1245 mp_msg(MSGT_VO, MSGL_FATAL, help_msg);
1246 return -1;
1248 if (deint)
1249 deint_type = deint;
1250 if (deint > 1)
1251 deint_buffer_past_frames = 1;
1252 if (colorspace < 0 || colorspace > 3) {
1253 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Invalid color space specified, "
1254 "using BT.601\n");
1255 colorspace = 1;
1258 vdpau_lib_handle = dlopen(vdpaulibrary, RTLD_LAZY);
1259 if (!vdpau_lib_handle) {
1260 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not open dynamic library %s\n",
1261 vdpaulibrary);
1262 return -1;
1264 vdp_device_create = dlsym(vdpau_lib_handle, vdpau_device_create);
1265 if (!vdp_device_create) {
1266 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not find function %s in %s\n",
1267 vdpau_device_create, vdpaulibrary);
1268 return -1;
1270 if (!vo_init() || win_x11_init_vdpau_procs())
1271 return -1;
1273 decoder = VDP_INVALID_HANDLE;
1274 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
1275 surface_render[i].surface = VDP_INVALID_HANDLE;
1276 video_mixer = VDP_INVALID_HANDLE;
1277 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++)
1278 output_surfaces[i] = VDP_INVALID_HANDLE;
1279 vdp_flip_queue = VDP_INVALID_HANDLE;
1280 output_surface_width = output_surface_height = -1;
1282 // full grayscale palette.
1283 for (i = 0; i < PALETTE_SIZE; ++i)
1284 palette[i] = (i << 16) | (i << 8) | i;
1285 index_data = NULL;
1286 index_data_size = 0;
1288 eosd_surface_count = eosd_render_count = 0;
1289 eosd_surfaces = NULL;
1290 eosd_targets = NULL;
1292 procamp.struct_version = VDP_PROCAMP_VERSION;
1293 procamp.brightness = 0.0;
1294 procamp.contrast = 1.0;
1295 procamp.saturation = 1.0;
1296 procamp.hue = 0.0;
1298 return 0;
1301 static int get_equalizer(char *name, int *value)
1303 if (!strcasecmp(name, "brightness"))
1304 *value = procamp.brightness * 100;
1305 else if (!strcasecmp(name, "contrast"))
1306 *value = (procamp.contrast-1.0) * 100;
1307 else if (!strcasecmp(name, "saturation"))
1308 *value = (procamp.saturation-1.0) * 100;
1309 else if (!strcasecmp(name, "hue"))
1310 *value = procamp.hue * 100 / M_PI;
1311 else
1312 return VO_NOTIMPL;
1313 return VO_TRUE;
1316 static int set_equalizer(char *name, int value)
1318 if (!strcasecmp(name, "brightness"))
1319 procamp.brightness = value / 100.0;
1320 else if (!strcasecmp(name, "contrast"))
1321 procamp.contrast = value / 100.0 + 1.0;
1322 else if (!strcasecmp(name, "saturation"))
1323 procamp.saturation = value / 100.0 + 1.0;
1324 else if (!strcasecmp(name, "hue"))
1325 procamp.hue = value / 100.0 * M_PI;
1326 else
1327 return VO_NOTIMPL;
1329 return update_csc_matrix();
1332 static int control(uint32_t request, void *data, ...)
1334 if (handle_preemption() < 0)
1335 return VO_FALSE;
1337 switch (request) {
1338 case VOCTRL_GET_DEINTERLACE:
1339 *(int*)data = deint;
1340 return VO_TRUE;
1341 case VOCTRL_SET_DEINTERLACE:
1342 if (image_format == IMGFMT_BGRA)
1343 return VO_NOTIMPL;
1344 deint = *(int*)data;
1345 if (deint)
1346 deint = deint_type;
1347 if (deint_type > 2) {
1348 VdpStatus vdp_st;
1349 VdpVideoMixerFeature features[1] =
1350 {deint_type == 3 ?
1351 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1352 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1353 VdpBool feature_enables[1] = {deint ? VDP_TRUE : VDP_FALSE};
1354 vdp_st = vdp_video_mixer_set_feature_enables(video_mixer, 1,
1355 features,
1356 feature_enables);
1357 CHECK_ST_WARNING("Error changing deinterlacing settings")
1358 deint_buffer_past_frames = 1;
1360 return VO_TRUE;
1361 case VOCTRL_PAUSE:
1362 return int_pause = 1;
1363 case VOCTRL_RESUME:
1364 return int_pause = 0;
1365 case VOCTRL_QUERY_FORMAT:
1366 return query_format(*(uint32_t *)data);
1367 case VOCTRL_GET_IMAGE:
1368 return get_image(data);
1369 case VOCTRL_DRAW_IMAGE:
1370 return draw_image(data);
1371 case VOCTRL_GUISUPPORT:
1372 return VO_TRUE;
1373 case VOCTRL_BORDER:
1374 vo_x11_border();
1375 resize();
1376 return VO_TRUE;
1377 case VOCTRL_FULLSCREEN:
1378 vo_x11_fullscreen();
1379 resize();
1380 return VO_TRUE;
1381 case VOCTRL_GET_PANSCAN:
1382 return VO_TRUE;
1383 case VOCTRL_SET_PANSCAN:
1384 resize();
1385 return VO_TRUE;
1386 case VOCTRL_SET_EQUALIZER: {
1387 va_list ap;
1388 int value;
1389 if (image_format == IMGFMT_BGRA)
1390 return VO_NOTIMPL;
1392 va_start(ap, data);
1393 value = va_arg(ap, int);
1395 va_end(ap);
1396 return set_equalizer(data, value);
1398 case VOCTRL_GET_EQUALIZER: {
1399 va_list ap;
1400 int *value;
1402 va_start(ap, data);
1403 value = va_arg(ap, int *);
1405 va_end(ap);
1406 return get_equalizer(data, value);
1408 case VOCTRL_ONTOP:
1409 vo_x11_ontop();
1410 return VO_TRUE;
1411 case VOCTRL_UPDATE_SCREENINFO:
1412 update_xinerama_info();
1413 return VO_TRUE;
1414 case VOCTRL_DRAW_EOSD:
1415 if (!data)
1416 return VO_FALSE;
1417 generate_eosd(data);
1418 draw_eosd();
1419 return VO_TRUE;
1420 case VOCTRL_GET_EOSD_RES: {
1421 mp_eosd_res_t *r = data;
1422 r->mt = r->mb = r->ml = r->mr = 0;
1423 if (vo_fs) {
1424 r->w = vo_screenwidth;
1425 r->h = vo_screenheight;
1426 r->ml = r->mr = border_x;
1427 r->mt = r->mb = border_y;
1428 } else {
1429 r->w = vo_dwidth;
1430 r->h = vo_dheight;
1432 return VO_TRUE;
1435 return VO_NOTIMPL;
1438 /* @} */