Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libvo / vo_vdpau.c
blob8b6fec44ef1742aa82c442b5c032814f94d070c1
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>
37 #include "config.h"
38 #include "mp_msg.h"
39 #include "video_out.h"
40 #include "video_out_internal.h"
41 #include "x11_common.h"
42 #include "aspect.h"
43 #include "sub.h"
44 #include "subopt-helper.h"
46 #include "libavcodec/vdpau.h"
48 #include "gui/interface.h"
50 #include "libavutil/common.h"
51 #include "libavutil/mathematics.h"
53 #include "libass/ass_mp.h"
55 static vo_info_t info = {
56 "VDPAU with X11",
57 "vdpau",
58 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
62 const LIBVO_EXTERN(vdpau)
64 #define CHECK_ST_ERROR(message) \
65 if (vdp_st != VDP_STATUS_OK) { \
66 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
67 message, vdp_get_error_string(vdp_st)); \
68 return -1; \
71 #define CHECK_ST_WARNING(message) \
72 if (vdp_st != VDP_STATUS_OK) \
73 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] %s: %s\n", \
74 message, vdp_get_error_string(vdp_st));
76 /* number of video and output surfaces */
77 #define NUM_OUTPUT_SURFACES 3
78 #define MAX_VIDEO_SURFACES 50
80 /* number of palette entries */
81 #define PALETTE_SIZE 256
83 /* Initial maximum number of EOSD surfaces */
84 #define EOSD_SURFACES_INITIAL 512
87 * Global variable declaration - VDPAU specific
90 /* Declaration for all variables of win_x11_init_vdpau_procs() and
91 * win_x11_init_vdpau_flip_queue() functions
93 static VdpDevice vdp_device;
94 static VdpGetProcAddress *vdp_get_proc_address;
96 static VdpPresentationQueueTarget vdp_flip_target;
97 static VdpPresentationQueue vdp_flip_queue;
99 static VdpDeviceDestroy *vdp_device_destroy;
100 static VdpVideoSurfaceCreate *vdp_video_surface_create;
101 static VdpVideoSurfaceDestroy *vdp_video_surface_destroy;
103 static VdpGetErrorString *vdp_get_error_string;
105 /* May be used in software filtering/postprocessing options
106 * in MPlayer (./mplayer -vf ..) if we copy video_surface data to
107 * system memory.
109 static VdpVideoSurfacePutBitsYCbCr *vdp_video_surface_put_bits_y_cb_cr;
110 static VdpOutputSurfacePutBitsNative *vdp_output_surface_put_bits_native;
112 static VdpOutputSurfaceCreate *vdp_output_surface_create;
113 static VdpOutputSurfaceDestroy *vdp_output_surface_destroy;
115 /* VideoMixer puts video_surface data on displayable output_surface. */
116 static VdpVideoMixerCreate *vdp_video_mixer_create;
117 static VdpVideoMixerDestroy *vdp_video_mixer_destroy;
118 static VdpVideoMixerRender *vdp_video_mixer_render;
119 static VdpVideoMixerSetFeatureEnables *vdp_video_mixer_set_feature_enables;
120 static VdpVideoMixerSetAttributeValues *vdp_video_mixer_set_attribute_values;
122 static VdpPresentationQueueTargetDestroy *vdp_presentation_queue_target_destroy;
123 static VdpPresentationQueueCreate *vdp_presentation_queue_create;
124 static VdpPresentationQueueDestroy *vdp_presentation_queue_destroy;
125 static VdpPresentationQueueDisplay *vdp_presentation_queue_display;
126 static VdpPresentationQueueBlockUntilSurfaceIdle *vdp_presentation_queue_block_until_surface_idle;
127 static VdpPresentationQueueTargetCreateX11 *vdp_presentation_queue_target_create_x11;
129 static VdpOutputSurfaceRenderOutputSurface *vdp_output_surface_render_output_surface;
130 static VdpOutputSurfacePutBitsIndexed *vdp_output_surface_put_bits_indexed;
131 static VdpOutputSurfaceRenderBitmapSurface *vdp_output_surface_render_bitmap_surface;
133 static VdpBitmapSurfaceCreate *vdp_bitmap_surface_create;
134 static VdpBitmapSurfaceDestroy *vdp_bitmap_surface_destroy;
135 static VdpBitmapSurfacePutBitsNative *vdp_bitmap_surface_putbits_native;
137 static VdpDecoderCreate *vdp_decoder_create;
138 static VdpDecoderDestroy *vdp_decoder_destroy;
139 static VdpDecoderRender *vdp_decoder_render;
141 static VdpGenerateCSCMatrix *vdp_generate_csc_matrix;
142 static VdpPreemptionCallbackRegister *vdp_preemption_callback_register;
144 /* output_surfaces[NUM_OUTPUT_SURFACES] is misused for OSD. */
145 #define osd_surface output_surfaces[NUM_OUTPUT_SURFACES]
146 static VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES + 1];
147 static VdpVideoSurface deint_surfaces[3];
148 static mp_image_t *deint_mpi[2];
149 static int output_surface_width, output_surface_height;
151 static VdpVideoMixer video_mixer;
152 static int deint;
153 static int deint_type;
154 static int deint_counter;
155 static int pullup;
156 static float denoise;
157 static float sharpen;
158 static int colorspace;
159 static int chroma_deint;
160 static int force_mixer;
161 static int top_field_first;
162 static int flip;
163 static int hqscaling;
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 VdpChromaType vdp_chroma_type;
178 static VdpYCbCrFormat vdp_pixel_format;
180 static volatile int is_preempted;
182 /* draw_osd */
183 static unsigned char *index_data;
184 static int index_data_size;
185 static uint32_t palette[PALETTE_SIZE];
187 // EOSD
188 // Pool of surfaces
189 struct {
190 VdpBitmapSurface surface;
191 int w;
192 int h;
193 char in_use;
194 } *eosd_surfaces;
196 // List of surfaces to be rendered
197 struct {
198 VdpBitmapSurface surface;
199 VdpRect source;
200 VdpRect dest;
201 VdpColor color;
202 } *eosd_targets;
204 static int eosd_render_count;
205 static int eosd_surface_count;
207 // Video equalizer
208 static VdpProcamp procamp;
211 * X11 specific
213 static int visible_buf;
214 static int int_pause;
216 static void draw_eosd(void);
218 static void push_deint_surface(VdpVideoSurface surface)
220 deint_surfaces[2] = deint_surfaces[1];
221 deint_surfaces[1] = deint_surfaces[0];
222 deint_surfaces[0] = surface;
225 static void video_to_output_surface(void)
227 VdpTime dummy;
228 VdpStatus vdp_st;
229 int i;
230 if (vid_surface_num < 0)
231 return;
233 if (deint < 2 || deint_surfaces[0] == VDP_INVALID_HANDLE)
234 push_deint_surface(surface_render[vid_surface_num].surface);
236 for (i = 0; i <= !!(deint > 1); i++) {
237 int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
238 VdpOutputSurface output_surface;
239 if (i) {
240 draw_eosd();
241 draw_osd();
242 flip_page();
244 if (deint)
245 field = (top_field_first == i) ^ (deint > 1) ?
246 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
247 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
248 output_surface = output_surfaces[surface_num];
249 vdp_st = vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue,
250 output_surface,
251 &dummy);
252 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
254 vdp_st = vdp_video_mixer_render(video_mixer, VDP_INVALID_HANDLE, 0,
255 field, 2, deint_surfaces + 1,
256 deint_surfaces[0],
257 1, &surface_render[vid_surface_num].surface,
258 &src_rect_vid,
259 output_surface,
260 NULL, &out_rect_vid, 0, NULL);
261 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
262 push_deint_surface(surface_render[vid_surface_num].surface);
266 static void resize(void)
268 VdpStatus vdp_st;
269 int i;
270 struct vo_rect src_rect;
271 struct vo_rect dst_rect;
272 struct vo_rect borders;
273 calc_src_dst_rects(vid_width, vid_height, &src_rect, &dst_rect, &borders, NULL);
274 out_rect_vid.x0 = dst_rect.left;
275 out_rect_vid.x1 = dst_rect.right;
276 out_rect_vid.y0 = dst_rect.top;
277 out_rect_vid.y1 = dst_rect.bottom;
278 src_rect_vid.x0 = src_rect.left;
279 src_rect_vid.x1 = src_rect.right;
280 src_rect_vid.y0 = flip ? src_rect.bottom : src_rect.top;
281 src_rect_vid.y1 = flip ? src_rect.top : src_rect.bottom;
282 border_x = borders.left;
283 border_y = borders.top;
284 #ifdef CONFIG_FREETYPE
285 // adjust font size to display size
286 force_load_font = 1;
287 #endif
288 vo_osd_changed(OSDTYPE_OSD);
290 if (output_surface_width < vo_dwidth || output_surface_height < vo_dheight) {
291 if (output_surface_width < vo_dwidth) {
292 output_surface_width += output_surface_width >> 1;
293 output_surface_width = FFMAX(output_surface_width, vo_dwidth);
295 if (output_surface_height < vo_dheight) {
296 output_surface_height += output_surface_height >> 1;
297 output_surface_height = FFMAX(output_surface_height, vo_dheight);
299 // Creation of output_surfaces
300 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) {
301 if (output_surfaces[i] != VDP_INVALID_HANDLE)
302 vdp_output_surface_destroy(output_surfaces[i]);
303 vdp_st = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8,
304 output_surface_width, output_surface_height,
305 &output_surfaces[i]);
306 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
307 mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", output_surfaces[i]);
310 if (image_format == IMGFMT_BGRA) {
311 vdp_st = vdp_output_surface_render_output_surface(output_surfaces[surface_num],
312 NULL, VDP_INVALID_HANDLE,
313 NULL, NULL, NULL,
314 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
315 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
316 vdp_st = vdp_output_surface_render_output_surface(output_surfaces[1 - 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 } else
322 video_to_output_surface();
323 if (visible_buf)
324 flip_page();
327 static void preemption_callback(VdpDevice device, void *context)
329 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Display preemption detected\n");
330 is_preempted = 1;
333 /* Initialize vdp_get_proc_address, called from preinit() */
334 static int win_x11_init_vdpau_procs(void)
336 VdpStatus vdp_st;
338 struct vdp_function {
339 const int id;
340 void *pointer;
343 const struct vdp_function *dsc;
345 static const struct vdp_function vdp_func[] = {
346 {VDP_FUNC_ID_GET_ERROR_STRING, &vdp_get_error_string},
347 {VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy},
348 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE, &vdp_video_surface_create},
349 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, &vdp_video_surface_destroy},
350 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
351 &vdp_video_surface_put_bits_y_cb_cr},
352 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE,
353 &vdp_output_surface_put_bits_native},
354 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create},
355 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy},
356 {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create},
357 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy},
358 {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render},
359 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
360 &vdp_video_mixer_set_feature_enables},
361 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
362 &vdp_video_mixer_set_attribute_values},
363 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
364 &vdp_presentation_queue_target_destroy},
365 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, &vdp_presentation_queue_create},
366 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
367 &vdp_presentation_queue_destroy},
368 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
369 &vdp_presentation_queue_display},
370 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
371 &vdp_presentation_queue_block_until_surface_idle},
372 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
373 &vdp_presentation_queue_target_create_x11},
374 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE,
375 &vdp_output_surface_render_output_surface},
376 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED,
377 &vdp_output_surface_put_bits_indexed},
378 {VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create},
379 {VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render},
380 {VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy},
381 {VDP_FUNC_ID_BITMAP_SURFACE_CREATE, &vdp_bitmap_surface_create},
382 {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY, &vdp_bitmap_surface_destroy},
383 {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE,
384 &vdp_bitmap_surface_putbits_native},
385 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE,
386 &vdp_output_surface_render_bitmap_surface},
387 {VDP_FUNC_ID_GENERATE_CSC_MATRIX, &vdp_generate_csc_matrix},
388 {VDP_FUNC_ID_PREEMPTION_CALLBACK_REGISTER,
389 &vdp_preemption_callback_register},
390 {0, NULL}
393 vdp_st = vdp_device_create_x11(mDisplay, mScreen,
394 &vdp_device, &vdp_get_proc_address);
395 if (vdp_st != VDP_STATUS_OK) {
396 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st);
397 return -1;
400 vdp_get_error_string = NULL;
401 for (dsc = vdp_func; dsc->pointer; dsc++) {
402 vdp_st = vdp_get_proc_address(vdp_device, dsc->id, dsc->pointer);
403 if (vdp_st != VDP_STATUS_OK) {
404 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_get_proc_address(function id %d): %s\n",
405 dsc->id, vdp_get_error_string ? vdp_get_error_string(vdp_st) : "?");
406 return -1;
409 vdp_st = vdp_preemption_callback_register(vdp_device,
410 preemption_callback, NULL);
411 CHECK_ST_ERROR("Error when calling vdp_preemption_callback_register")
413 return 0;
416 /* Initialize vdpau_flip_queue, called from config() */
417 static int win_x11_init_vdpau_flip_queue(void)
419 VdpStatus vdp_st;
421 vdp_st = vdp_presentation_queue_target_create_x11(vdp_device, vo_window,
422 &vdp_flip_target);
423 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
425 vdp_st = vdp_presentation_queue_create(vdp_device, vdp_flip_target,
426 &vdp_flip_queue);
427 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
429 return 0;
432 static int update_csc_matrix(void)
434 VdpStatus vdp_st;
435 VdpCSCMatrix matrix;
436 static const VdpVideoMixerAttribute attributes[] = {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX};
437 const void *attribute_values[] = {&matrix};
438 static const VdpColorStandard vdp_colors[] = {0, VDP_COLOR_STANDARD_ITUR_BT_601, VDP_COLOR_STANDARD_ITUR_BT_709, VDP_COLOR_STANDARD_SMPTE_240M};
439 static const char * const vdp_names[] = {NULL, "BT.601", "BT.709", "SMPTE-240M"};
440 int csp = colorspace;
442 if (!csp)
443 csp = vid_width >= 1280 || vid_height > 576 ? 2 : 1;
445 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Updating CSC matrix for %s\n",
446 vdp_names[csp]);
448 vdp_st = vdp_generate_csc_matrix(&procamp, vdp_colors[csp], &matrix);
449 CHECK_ST_WARNING("Error when generating CSC matrix")
451 vdp_st = vdp_video_mixer_set_attribute_values(video_mixer, 1, attributes,
452 attribute_values);
453 CHECK_ST_WARNING("Error when setting CSC matrix")
454 return VO_TRUE;
457 static int create_vdp_mixer(VdpChromaType vdp_chroma_type)
459 #define VDP_NUM_MIXER_PARAMETER 3
460 #define MAX_NUM_FEATURES 6
461 int i;
462 VdpStatus vdp_st;
463 int feature_count = 0;
464 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
465 VdpBool feature_enables[MAX_NUM_FEATURES];
466 static const VdpVideoMixerAttribute denoise_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL};
467 const void * const denoise_value[] = {&denoise};
468 static const VdpVideoMixerAttribute sharpen_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL};
469 const void * const sharpen_value[] = {&sharpen};
470 static const VdpVideoMixerAttribute skip_chroma_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SKIP_CHROMA_DEINTERLACE};
471 const uint8_t skip_chroma_value = 1;
472 const void * const skip_chroma_value_ptr[] = {&skip_chroma_value};
473 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
474 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
475 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
476 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
478 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
479 &vid_width,
480 &vid_height,
481 &vdp_chroma_type
483 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
484 if (deint == 4)
485 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
486 if (pullup)
487 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
488 if (denoise)
489 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
490 if (sharpen)
491 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
492 if (hqscaling)
493 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 + (hqscaling - 1);
495 vdp_st = vdp_video_mixer_create(vdp_device, feature_count, features,
496 VDP_NUM_MIXER_PARAMETER,
497 parameters, parameter_values,
498 &video_mixer);
499 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
501 for (i = 0; i < feature_count; i++)
502 feature_enables[i] = VDP_TRUE;
503 if (deint < 3)
504 feature_enables[0] = VDP_FALSE;
505 if (feature_count)
506 vdp_video_mixer_set_feature_enables(video_mixer, feature_count, features, feature_enables);
507 if (denoise)
508 vdp_video_mixer_set_attribute_values(video_mixer, 1, denoise_attrib, denoise_value);
509 if (sharpen)
510 vdp_video_mixer_set_attribute_values(video_mixer, 1, sharpen_attrib, sharpen_value);
511 if (!chroma_deint)
512 vdp_video_mixer_set_attribute_values(video_mixer, 1, skip_chroma_attrib, skip_chroma_value_ptr);
514 update_csc_matrix();
515 return 0;
518 // Free everything specific to a certain video file
519 static void free_video_specific(void)
521 int i;
522 VdpStatus vdp_st;
524 if (decoder != VDP_INVALID_HANDLE)
525 vdp_decoder_destroy(decoder);
526 decoder = VDP_INVALID_HANDLE;
527 decoder_max_refs = -1;
529 for (i = 0; i < 3; i++)
530 deint_surfaces[i] = VDP_INVALID_HANDLE;
532 for (i = 0; i < 2; i++)
533 if (deint_mpi[i]) {
534 deint_mpi[i]->usage_count--;
535 deint_mpi[i] = NULL;
538 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
539 if (surface_render[i].surface != VDP_INVALID_HANDLE) {
540 vdp_st = vdp_video_surface_destroy(surface_render[i].surface);
541 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
543 surface_render[i].surface = VDP_INVALID_HANDLE;
546 if (video_mixer != VDP_INVALID_HANDLE) {
547 vdp_st = vdp_video_mixer_destroy(video_mixer);
548 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
550 video_mixer = VDP_INVALID_HANDLE;
553 static int create_vdp_decoder(uint32_t format, uint32_t width, uint32_t height,
554 int max_refs)
556 VdpStatus vdp_st;
557 VdpDecoderProfile vdp_decoder_profile;
558 if (decoder != VDP_INVALID_HANDLE)
559 vdp_decoder_destroy(decoder);
560 switch (format) {
561 case IMGFMT_VDPAU_MPEG1:
562 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
563 break;
564 case IMGFMT_VDPAU_MPEG2:
565 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
566 break;
567 case IMGFMT_VDPAU_H264:
568 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
569 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder for %d reference frames.\n", max_refs);
570 break;
571 case IMGFMT_VDPAU_WMV3:
572 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
573 break;
574 case IMGFMT_VDPAU_VC1:
575 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
576 break;
577 case IMGFMT_VDPAU_MPEG4:
578 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
579 break;
580 default:
581 goto err_out;
583 vdp_st = vdp_decoder_create(vdp_device, vdp_decoder_profile,
584 width, height, max_refs, &decoder);
585 CHECK_ST_WARNING("Failed creating VDPAU decoder");
586 if (vdp_st != VDP_STATUS_OK) {
587 err_out:
588 decoder = VDP_INVALID_HANDLE;
589 decoder_max_refs = 0;
590 return 0;
592 decoder_max_refs = max_refs;
593 return 1;
596 static void mark_vdpau_objects_uninitialized(void)
598 int i;
600 decoder = VDP_INVALID_HANDLE;
601 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
602 surface_render[i].surface = VDP_INVALID_HANDLE;
603 for (i = 0; i < 3; i++) {
604 deint_surfaces[i] = VDP_INVALID_HANDLE;
605 if (i < 2 && deint_mpi[i])
606 deint_mpi[i]->usage_count--;
607 deint_mpi[i] = NULL;
609 video_mixer = VDP_INVALID_HANDLE;
610 vdp_flip_queue = VDP_INVALID_HANDLE;
611 vdp_flip_target = VDP_INVALID_HANDLE;
612 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++)
613 output_surfaces[i] = VDP_INVALID_HANDLE;
614 vdp_device = VDP_INVALID_HANDLE;
615 for (i = 0; i < eosd_surface_count; i++)
616 eosd_surfaces[i].surface = VDP_INVALID_HANDLE;
617 output_surface_width = output_surface_height = -1;
618 eosd_render_count = 0;
619 visible_buf = 0;
622 static int handle_preemption(void)
624 if (!is_preempted)
625 return 0;
626 is_preempted = 0;
627 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Attempting to recover from preemption.\n");
628 mark_vdpau_objects_uninitialized();
629 if (win_x11_init_vdpau_procs() < 0 ||
630 win_x11_init_vdpau_flip_queue() < 0 ||
631 create_vdp_mixer(vdp_chroma_type) < 0) {
632 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Recovering from preemption failed\n");
633 is_preempted = 1;
634 return -1;
636 resize();
637 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n");
638 return 1;
642 * connect to X server, create and map window, initialize all
643 * VDPAU objects, create different surfaces etc.
645 static int config(uint32_t width, uint32_t height, uint32_t d_width,
646 uint32_t d_height, uint32_t flags, char *title,
647 uint32_t format)
649 XVisualInfo vinfo;
650 XSetWindowAttributes xswa;
651 XWindowAttributes attribs;
652 unsigned long xswamask;
653 int depth;
655 #ifdef CONFIG_XF86VM
656 int vm = flags & VOFLAG_MODESWITCHING;
657 #endif
658 flip = flags & VOFLAG_FLIPPING;
660 image_format = format;
661 vid_width = width;
662 vid_height = height;
663 free_video_specific();
664 if (IMGFMT_IS_VDPAU(image_format)
665 && !create_vdp_decoder(image_format, vid_width, vid_height, 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(image_format, vid_width, vid_height, 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 mpi->usage_count++;
1026 if (deint_mpi[1])
1027 deint_mpi[1]->usage_count--;
1028 deint_mpi[1] = deint_mpi[0];
1029 deint_mpi[0] = mpi;
1030 } else if (image_format == IMGFMT_BGRA) {
1031 VdpStatus vdp_st;
1032 VdpRect r = {0, 0, vid_width, vid_height};
1033 vdp_st = vdp_output_surface_put_bits_native(output_surfaces[2],
1034 (void const*const*)mpi->planes,
1035 mpi->stride, &r);
1036 CHECK_ST_ERROR("Error when calling vdp_output_surface_put_bits_native")
1037 vdp_st = vdp_output_surface_render_output_surface(output_surfaces[surface_num],
1038 &out_rect_vid,
1039 output_surfaces[2],
1040 &src_rect_vid, NULL, NULL,
1041 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1042 CHECK_ST_ERROR("Error when calling vdp_output_surface_render_output_surface")
1043 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1044 VdpStatus vdp_st;
1045 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
1046 struct vdpau_render_state *rndr = get_surface(deint_counter);
1047 deint_counter = (deint_counter + 1) % 3;
1048 vid_surface_num = rndr - surface_render;
1049 if (image_format == IMGFMT_NV12)
1050 destdata[1] = destdata[2];
1051 vdp_st = vdp_video_surface_put_bits_y_cb_cr(rndr->surface,
1052 vdp_pixel_format,
1053 (const void *const*)destdata,
1054 mpi->stride); // pitch
1055 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
1057 if (mpi->fields & MP_IMGFIELD_ORDERED)
1058 top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1059 else
1060 top_field_first = 1;
1062 video_to_output_surface();
1063 return VO_TRUE;
1066 static uint32_t get_image(mp_image_t *mpi)
1068 struct vdpau_render_state *rndr;
1070 // no dr for non-decoding for now
1071 if (!IMGFMT_IS_VDPAU(image_format))
1072 return VO_FALSE;
1073 if (mpi->type != MP_IMGTYPE_NUMBERED)
1074 return VO_FALSE;
1076 rndr = get_surface(mpi->number);
1077 if (!rndr) {
1078 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in get_image\n");
1079 // TODO: this probably breaks things forever, provide a dummy buffer?
1080 return VO_FALSE;
1082 mpi->flags |= MP_IMGFLAG_DIRECT;
1083 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1084 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1085 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1086 mpi->planes[0] = (void *)rndr;
1087 mpi->num_planes = 1;
1088 mpi->priv = rndr;
1089 return VO_TRUE;
1092 static int query_format(uint32_t format)
1094 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;
1095 switch (format) {
1096 case IMGFMT_BGRA:
1097 if (force_mixer)
1098 return 0;
1099 case IMGFMT_YV12:
1100 case IMGFMT_I420:
1101 case IMGFMT_IYUV:
1102 case IMGFMT_NV12:
1103 case IMGFMT_YUY2:
1104 case IMGFMT_UYVY:
1105 return default_flags | VOCAP_NOSLICES;
1106 case IMGFMT_VDPAU_MPEG1:
1107 case IMGFMT_VDPAU_MPEG2:
1108 case IMGFMT_VDPAU_H264:
1109 case IMGFMT_VDPAU_WMV3:
1110 case IMGFMT_VDPAU_VC1:
1111 case IMGFMT_VDPAU_MPEG4:
1112 if (create_vdp_decoder(format, 48, 48, 2))
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();
1180 static const opt_t subopts[] = {
1181 {"deint", OPT_ARG_INT, &deint, (opt_test_f)int_non_neg},
1182 {"chroma-deint", OPT_ARG_BOOL, &chroma_deint, NULL},
1183 {"pullup", OPT_ARG_BOOL, &pullup, NULL},
1184 {"denoise", OPT_ARG_FLOAT, &denoise, NULL},
1185 {"sharpen", OPT_ARG_FLOAT, &sharpen, NULL},
1186 {"colorspace", OPT_ARG_INT, &colorspace, NULL},
1187 {"force-mixer", OPT_ARG_BOOL, &force_mixer, NULL},
1188 {"hqscaling", OPT_ARG_INT, &hqscaling, (opt_test_f)int_non_neg},
1189 {NULL}
1192 static const char help_msg[] =
1193 "\n-vo vdpau command line help:\n"
1194 "Example: mplayer -vo vdpau:deint=2\n"
1195 "\nOptions:\n"
1196 " deint (all modes > 0 respect -field-dominance)\n"
1197 " 0: no deinterlacing\n"
1198 " 1: only show first field\n"
1199 " 2: bob deinterlacing\n"
1200 " 3: temporal deinterlacing (resource-hungry)\n"
1201 " 4: temporal-spatial deinterlacing (very resource-hungry)\n"
1202 " chroma-deint\n"
1203 " Operate on luma and chroma when using temporal deinterlacing (default)\n"
1204 " Use nochroma-deint to speed up temporal deinterlacing\n"
1205 " pullup\n"
1206 " Try to skip deinterlacing for progressive frames\n"
1207 " (needs at least temporal deinterlacing, useful for telecined content)\n"
1208 " denoise\n"
1209 " Apply denoising, argument is strength from 0.0 to 1.0\n"
1210 " sharpen\n"
1211 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
1212 " colorspace\n"
1213 " 0: guess based on video resolution\n"
1214 " 1: ITU-R BT.601 (default)\n"
1215 " 2: ITU-R BT.709\n"
1216 " 3: SMPTE-240M\n"
1217 " hqscaling\n"
1218 " 0: default VDPAU scaler\n"
1219 " 1-9: high quality VDPAU scaler (needs capable hardware)\n"
1220 " force-mixer\n"
1221 " Use the VDPAU mixer (default)\n"
1222 " Use noforce-mixer to allow BGRA output (disables all above options)\n"
1225 static int preinit(const char *arg)
1227 int i;
1229 deint = 0;
1230 deint_type = 3;
1231 deint_counter = 0;
1232 deint_mpi[0] = deint_mpi[1] = NULL;
1233 chroma_deint = 1;
1234 pullup = 0;
1235 denoise = 0;
1236 sharpen = 0;
1237 colorspace = 1;
1238 force_mixer = 1;
1239 hqscaling = 0;
1240 if (subopt_parse(arg, subopts) != 0) {
1241 mp_msg(MSGT_VO, MSGL_FATAL, help_msg);
1242 return -1;
1244 if (deint)
1245 deint_type = deint;
1246 if (colorspace < 0 || colorspace > 3) {
1247 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Invalid color space specified, "
1248 "using BT.601\n");
1249 colorspace = 1;
1252 if (!vo_init() || win_x11_init_vdpau_procs())
1253 return -1;
1255 decoder = VDP_INVALID_HANDLE;
1256 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
1257 surface_render[i].surface = VDP_INVALID_HANDLE;
1258 video_mixer = VDP_INVALID_HANDLE;
1259 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++)
1260 output_surfaces[i] = VDP_INVALID_HANDLE;
1261 vdp_flip_queue = VDP_INVALID_HANDLE;
1262 output_surface_width = output_surface_height = -1;
1264 // full grayscale palette.
1265 for (i = 0; i < PALETTE_SIZE; ++i)
1266 palette[i] = (i << 16) | (i << 8) | i;
1267 index_data = NULL;
1268 index_data_size = 0;
1270 eosd_surface_count = eosd_render_count = 0;
1271 eosd_surfaces = NULL;
1272 eosd_targets = NULL;
1274 procamp.struct_version = VDP_PROCAMP_VERSION;
1275 procamp.brightness = 0.0;
1276 procamp.contrast = 1.0;
1277 procamp.saturation = 1.0;
1278 procamp.hue = 0.0;
1280 return 0;
1283 static int get_equalizer(char *name, int *value)
1285 if (!strcasecmp(name, "brightness"))
1286 *value = procamp.brightness * 100;
1287 else if (!strcasecmp(name, "contrast"))
1288 *value = (procamp.contrast-1.0) * 100;
1289 else if (!strcasecmp(name, "saturation"))
1290 *value = (procamp.saturation-1.0) * 100;
1291 else if (!strcasecmp(name, "hue"))
1292 *value = procamp.hue * 100 / M_PI;
1293 else
1294 return VO_NOTIMPL;
1295 return VO_TRUE;
1298 static int set_equalizer(char *name, int value)
1300 if (!strcasecmp(name, "brightness"))
1301 procamp.brightness = value / 100.0;
1302 else if (!strcasecmp(name, "contrast"))
1303 procamp.contrast = value / 100.0 + 1.0;
1304 else if (!strcasecmp(name, "saturation"))
1305 procamp.saturation = value / 100.0 + 1.0;
1306 else if (!strcasecmp(name, "hue"))
1307 procamp.hue = value / 100.0 * M_PI;
1308 else
1309 return VO_NOTIMPL;
1311 return update_csc_matrix();
1314 static int control(uint32_t request, void *data, ...)
1316 if (handle_preemption() < 0)
1317 return VO_FALSE;
1319 switch (request) {
1320 case VOCTRL_GET_DEINTERLACE:
1321 *(int*)data = deint;
1322 return VO_TRUE;
1323 case VOCTRL_SET_DEINTERLACE:
1324 if (image_format == IMGFMT_BGRA)
1325 return VO_NOTIMPL;
1326 deint = *(int*)data;
1327 if (deint)
1328 deint = deint_type;
1329 if (deint_type > 2) {
1330 VdpStatus vdp_st;
1331 VdpVideoMixerFeature features[1] =
1332 {deint_type == 3 ?
1333 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1334 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1335 VdpBool feature_enables[1] = {deint ? VDP_TRUE : VDP_FALSE};
1336 vdp_st = vdp_video_mixer_set_feature_enables(video_mixer, 1,
1337 features,
1338 feature_enables);
1339 CHECK_ST_WARNING("Error changing deinterlacing settings")
1341 return VO_TRUE;
1342 case VOCTRL_PAUSE:
1343 return int_pause = 1;
1344 case VOCTRL_RESUME:
1345 return int_pause = 0;
1346 case VOCTRL_QUERY_FORMAT:
1347 return query_format(*(uint32_t *)data);
1348 case VOCTRL_GET_IMAGE:
1349 return get_image(data);
1350 case VOCTRL_DRAW_IMAGE:
1351 return draw_image(data);
1352 case VOCTRL_GUISUPPORT:
1353 return VO_TRUE;
1354 case VOCTRL_BORDER:
1355 vo_x11_border();
1356 resize();
1357 return VO_TRUE;
1358 case VOCTRL_FULLSCREEN:
1359 vo_x11_fullscreen();
1360 resize();
1361 return VO_TRUE;
1362 case VOCTRL_GET_PANSCAN:
1363 return VO_TRUE;
1364 case VOCTRL_SET_PANSCAN:
1365 resize();
1366 return VO_TRUE;
1367 case VOCTRL_SET_EQUALIZER: {
1368 va_list ap;
1369 int value;
1370 if (image_format == IMGFMT_BGRA)
1371 return VO_NOTIMPL;
1373 va_start(ap, data);
1374 value = va_arg(ap, int);
1376 va_end(ap);
1377 return set_equalizer(data, value);
1379 case VOCTRL_GET_EQUALIZER: {
1380 va_list ap;
1381 int *value;
1383 va_start(ap, data);
1384 value = va_arg(ap, int *);
1386 va_end(ap);
1387 return get_equalizer(data, value);
1389 case VOCTRL_ONTOP:
1390 vo_x11_ontop();
1391 return VO_TRUE;
1392 case VOCTRL_UPDATE_SCREENINFO:
1393 update_xinerama_info();
1394 return VO_TRUE;
1395 case VOCTRL_DRAW_EOSD:
1396 if (!data)
1397 return VO_FALSE;
1398 generate_eosd(data);
1399 draw_eosd();
1400 return VO_TRUE;
1401 case VOCTRL_GET_EOSD_RES: {
1402 mp_eosd_res_t *r = data;
1403 r->mt = r->mb = r->ml = r->mr = 0;
1404 r->srcw = vid_width; r->srch = vid_height;
1405 if (vo_fs) {
1406 r->w = vo_screenwidth;
1407 r->h = vo_screenheight;
1408 r->ml = r->mr = border_x;
1409 r->mt = r->mb = border_y;
1410 } else {
1411 r->w = vo_dwidth;
1412 r->h = vo_dheight;
1414 return VO_TRUE;
1417 return VO_NOTIMPL;
1420 /* @} */