codecs.conf: add matchware screen codec fourcc MWSC
[mplayer/glamo.git] / libvo / vo_vdpau.c
blob2793e99c314e68517a984c6a30ab1d6ee0cad80b
1 /*
2 * VDPAU video output driver
4 * Copyright (C) 2008 NVIDIA
5 * Copyright (C) 2009 Uoti Urpala
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Actual decoding and presentation are implemented here.
26 * All necessary frame information is collected through
27 * the "vdpau_render_state" structure after parsing all headers
28 * etc. in libavcodec for different codecs.
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdint.h>
34 #include <stdbool.h>
35 #include <limits.h>
37 #include "config.h"
38 #include "mp_msg.h"
39 #include "options.h"
40 #include "talloc.h"
41 #include "video_out.h"
42 #include "x11_common.h"
43 #include "aspect.h"
44 #include "sub.h"
45 #include "subopt-helper.h"
46 #include "libmpcodecs/vfcap.h"
47 #include "libmpcodecs/mp_image.h"
48 #include "osdep/timer.h"
50 #include "libavcodec/vdpau.h"
52 #include "font_load.h"
54 #include "libavutil/common.h"
55 #include "libavutil/mathematics.h"
57 #include "ass_mp.h"
59 #define WRAP_ADD(x, a, m) ((a) < 0 \
60 ? ((x)+(a)+(m) < (m) ? (x)+(a)+(m) : (x)+(a)) \
61 : ((x)+(a) < (m) ? (x)+(a) : (x)+(a)-(m)))
63 #define CHECK_ST_ERROR(message) \
64 do { \
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; \
69 } \
70 } while (0)
72 #define CHECK_ST_WARNING(message) \
73 do { \
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)); \
77 } while (0)
79 /* number of video and output surfaces */
80 #define MAX_OUTPUT_SURFACES 15
81 #define MAX_VIDEO_SURFACES 50
82 #define NUM_BUFFERED_VIDEO 4
84 /* number of palette entries */
85 #define PALETTE_SIZE 256
87 /* Initial size of EOSD surface in pixels (x*x) */
88 #define EOSD_SURFACE_INITIAL_SIZE 256
91 * Global variable declaration - VDPAU specific
94 struct vdp_functions {
95 #define VDP_FUNCTION(vdp_type, _, mp_name) vdp_type *mp_name;
96 #include "vdpau_template.c"
97 #undef VDP_FUNCTION
100 struct vdpctx {
101 struct vdp_functions *vdp;
103 VdpDevice vdp_device;
104 bool is_preempted;
105 bool preemption_acked;
106 bool preemption_user_notified;
107 unsigned int last_preemption_retry_fail;
108 VdpGetProcAddress *vdp_get_proc_address;
110 VdpPresentationQueueTarget flip_target;
111 VdpPresentationQueue flip_queue;
112 uint64_t last_vdp_time;
113 unsigned int last_sync_update;
115 /* an extra last output surface is misused for OSD. */
116 VdpOutputSurface output_surfaces[MAX_OUTPUT_SURFACES + 1];
117 int num_output_surfaces;
118 struct buffered_video_surface {
119 VdpVideoSurface surface;
120 double pts;
121 mp_image_t *mpi;
122 } buffered_video[NUM_BUFFERED_VIDEO];
123 int deint_queue_pos;
124 int output_surface_width, output_surface_height;
126 VdpVideoMixer video_mixer;
127 int user_colorspace;
128 int colorspace;
129 int studio_levels;
130 int deint;
131 int deint_type;
132 int deint_counter;
133 int pullup;
134 float denoise;
135 float sharpen;
136 int hqscaling;
137 int chroma_deint;
138 int flip_offset_window;
139 int flip_offset_fs;
140 int top_field_first;
141 bool flip;
143 VdpDecoder decoder;
144 int decoder_max_refs;
146 VdpRect src_rect_vid;
147 VdpRect out_rect_vid;
148 int border_x, border_y;
150 struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES];
151 int surface_num;
152 int query_surface_num;
153 VdpTime recent_vsync_time;
154 float user_fps;
155 unsigned int vsync_interval;
156 uint64_t last_queue_time;
157 uint64_t queue_time[MAX_OUTPUT_SURFACES];
158 uint64_t last_ideal_time;
159 bool dropped_frame;
160 uint64_t dropped_time;
161 uint32_t vid_width, vid_height;
162 uint32_t image_format;
163 VdpChromaType vdp_chroma_type;
164 VdpYCbCrFormat vdp_pixel_format;
166 /* draw_osd */
167 unsigned char *index_data;
168 int index_data_size;
169 uint32_t palette[PALETTE_SIZE];
171 // EOSD
172 // Pool of surfaces
173 struct eosd_bitmap_surface {
174 VdpBitmapSurface surface;
175 int w;
176 int h;
177 uint32_t max_width;
178 uint32_t max_height;
179 } eosd_surface;
181 // List of surfaces to be rendered
182 struct eosd_target {
183 VdpRect source;
184 VdpRect dest;
185 VdpColor color;
186 } *eosd_targets;
187 int eosd_targets_size;
188 int *eosd_scratch;
190 int eosd_render_count;
192 // Video equalizer
193 VdpProcamp procamp;
195 int num_shown_frames;
196 bool paused;
198 // These tell what's been initialized and uninit() should free/uninitialize
199 bool mode_switched;
202 static int change_vdptime_sync(struct vdpctx *vc, unsigned int *t)
204 struct vdp_functions *vdp = vc->vdp;
205 VdpStatus vdp_st;
206 VdpTime vdp_time;
207 vdp_st = vdp->presentation_queue_get_time(vc->flip_queue, &vdp_time);
208 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_get_time");
209 unsigned int t1 = *t;
210 unsigned int t2 = GetTimer();
211 uint64_t old = vc->last_vdp_time + (t1 - vc->last_sync_update) * 1000ULL;
212 if (vdp_time > old)
213 if (vdp_time > old + (t2 - t1) * 1000ULL)
214 vdp_time -= (t2 - t1) * 1000ULL;
215 else
216 vdp_time = old;
217 mp_msg(MSGT_VO, MSGL_V, "[vdpau] adjusting VdpTime offset by %f µs\n",
218 (int64_t)(vdp_time - old) / 1000.);
219 vc->last_vdp_time = vdp_time;
220 vc->last_sync_update = t1;
221 *t = t2;
222 return 0;
225 static uint64_t sync_vdptime(struct vo *vo)
227 struct vdpctx *vc = vo->priv;
229 unsigned int t = GetTimer();
230 if (t - vc->last_sync_update > 5000000)
231 change_vdptime_sync(vc, &t);
232 uint64_t now = (t - vc->last_sync_update) * 1000ULL + vc->last_vdp_time;
233 // Make sure nanosecond inaccuracies don't make things inconsistent
234 now = FFMAX(now, vc->recent_vsync_time);
235 return now;
238 static uint64_t convert_to_vdptime(struct vo *vo, unsigned int t)
240 struct vdpctx *vc = vo->priv;
241 return (int)(t - vc->last_sync_update) * 1000LL + vc->last_vdp_time;
244 static void flip_page_timed(struct vo *vo, unsigned int pts_us, int duration);
246 static int video_to_output_surface(struct vo *vo)
248 struct vdpctx *vc = vo->priv;
249 struct vdp_functions *vdp = vc->vdp;
250 VdpTime dummy;
251 VdpStatus vdp_st;
252 if (vc->deint_queue_pos < 0)
253 return -1;
255 struct buffered_video_surface *bv = vc->buffered_video;
256 int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
257 unsigned int dp = vc->deint_queue_pos;
258 // dp==0 means last field of latest frame, 1 earlier field of latest frame,
259 // 2 last field of previous frame and so on
260 if (vc->deint) {
261 field = vc->top_field_first ^ (dp & 1) ?
262 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
263 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
265 const VdpVideoSurface *past_fields = (const VdpVideoSurface []){
266 bv[(dp+1)/2].surface, bv[(dp+2)/2].surface};
267 const VdpVideoSurface *future_fields = (const VdpVideoSurface []){
268 dp >= 1 ? bv[(dp-1)/2].surface : VDP_INVALID_HANDLE};
269 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
270 vdp_st = vdp->presentation_queue_block_until_surface_idle(vc->flip_queue,
271 output_surface,
272 &dummy);
273 CHECK_ST_WARNING("Error when calling "
274 "vdp_presentation_queue_block_until_surface_idle");
276 vdp_st = vdp->video_mixer_render(vc->video_mixer, VDP_INVALID_HANDLE,
277 0, field, 2, past_fields,
278 bv[dp/2].surface, 1, future_fields,
279 &vc->src_rect_vid, output_surface,
280 NULL, &vc->out_rect_vid, 0, NULL);
281 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render");
282 return 0;
285 static void get_buffered_frame(struct vo *vo, bool eof)
287 struct vdpctx *vc = vo->priv;
289 int dqp = vc->deint_queue_pos;
290 if (dqp < 0)
291 dqp += 1000;
292 else
293 dqp = vc->deint >= 2 ? dqp - 1 : dqp - 2 | 1;
294 if (dqp < (eof ? 0 : 3))
295 return;
297 dqp = FFMIN(dqp, 4);
298 vc->deint_queue_pos = dqp;
299 vo->frame_loaded = true;
301 // Set pts values
302 struct buffered_video_surface *bv = vc->buffered_video;
303 int idx = vc->deint_queue_pos >> 1;
304 if (idx == 0) { // no future frame/pts available
305 vo->next_pts = bv[0].pts;
306 vo->next_pts2 = MP_NOPTS_VALUE;
307 } else if (!(vc->deint >= 2)) { // no field-splitting deinterlace
308 vo->next_pts = bv[idx].pts;
309 vo->next_pts2 = bv[idx - 1].pts;
310 } else { // deinterlace with separate fields
311 double intermediate_pts;
312 double diff = bv[idx - 1].pts - bv[idx].pts;
313 if (diff > 0 && diff < 0.5)
314 intermediate_pts = (bv[idx].pts + bv[idx - 1].pts) / 2;
315 else
316 intermediate_pts = bv[idx].pts;
317 if (vc->deint_queue_pos & 1) { // first field
318 vo->next_pts = bv[idx].pts;
319 vo->next_pts2 = intermediate_pts;
320 } else {
321 vo->next_pts = intermediate_pts;
322 vo->next_pts2 = bv[idx - 1].pts;
326 video_to_output_surface(vo);
329 static void add_new_video_surface(struct vo *vo, VdpVideoSurface surface,
330 struct mp_image *reserved_mpi, double pts)
332 struct vdpctx *vc = vo->priv;
333 struct buffered_video_surface *bv = vc->buffered_video;
335 if (reserved_mpi)
336 reserved_mpi->usage_count++;
337 if (bv[NUM_BUFFERED_VIDEO - 1].mpi)
338 bv[NUM_BUFFERED_VIDEO - 1].mpi->usage_count--;
340 for (int i = NUM_BUFFERED_VIDEO - 1; i > 0; i--)
341 bv[i] = bv[i - 1];
342 bv[0] = (struct buffered_video_surface){
343 .mpi = reserved_mpi,
344 .surface = surface,
345 .pts = pts,
348 vc->deint_queue_pos += 2;
349 get_buffered_frame(vo, false);
352 static void forget_frames(struct vo *vo)
354 struct vdpctx *vc = vo->priv;
356 vc->deint_queue_pos = -1001;
357 vc->dropped_frame = false;
358 for (int i = 0; i < NUM_BUFFERED_VIDEO; i++) {
359 struct buffered_video_surface *p = vc->buffered_video + i;
360 if (p->mpi)
361 p->mpi->usage_count--;
362 *p = (struct buffered_video_surface){
363 .surface = VDP_INVALID_HANDLE,
368 static void resize(struct vo *vo)
370 struct vdpctx *vc = vo->priv;
371 struct vdp_functions *vdp = vc->vdp;
372 VdpStatus vdp_st;
373 int i;
374 struct vo_rect src_rect;
375 struct vo_rect dst_rect;
376 struct vo_rect borders;
377 calc_src_dst_rects(vo, vc->vid_width, vc->vid_height, &src_rect, &dst_rect,
378 &borders, NULL);
379 vc->out_rect_vid.x0 = dst_rect.left;
380 vc->out_rect_vid.x1 = dst_rect.right;
381 vc->out_rect_vid.y0 = dst_rect.top;
382 vc->out_rect_vid.y1 = dst_rect.bottom;
383 vc->src_rect_vid.x0 = src_rect.left;
384 vc->src_rect_vid.x1 = src_rect.right;
385 vc->src_rect_vid.y0 = vc->flip ? src_rect.bottom : src_rect.top;
386 vc->src_rect_vid.y1 = vc->flip ? src_rect.top : src_rect.bottom;
387 vc->border_x = borders.left;
388 vc->border_y = borders.top;
389 #ifdef CONFIG_FREETYPE
390 // adjust font size to display size
391 force_load_font = 1;
392 #endif
393 vo_osd_changed(OSDTYPE_OSD);
394 int flip_offset_ms = vo_fs ? vc->flip_offset_fs : vc->flip_offset_window;
395 vo->flip_queue_offset = flip_offset_ms / 1000.;
397 bool had_frames = vc->num_shown_frames;
398 if (vc->output_surface_width < vo->dwidth
399 || vc->output_surface_height < vo->dheight) {
400 if (vc->output_surface_width < vo->dwidth) {
401 vc->output_surface_width += vc->output_surface_width >> 1;
402 vc->output_surface_width = FFMAX(vc->output_surface_width,
403 vo->dwidth);
405 if (vc->output_surface_height < vo->dheight) {
406 vc->output_surface_height += vc->output_surface_height >> 1;
407 vc->output_surface_height = FFMAX(vc->output_surface_height,
408 vo->dheight);
410 // Creation of output_surfaces
411 for (i = 0; i <= vc->num_output_surfaces; i++) {
412 if (vc->output_surfaces[i] != VDP_INVALID_HANDLE) {
413 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
414 CHECK_ST_WARNING("Error when calling "
415 "vdp_output_surface_destroy");
417 vdp_st = vdp->output_surface_create(vc->vdp_device,
418 VDP_RGBA_FORMAT_B8G8R8A8,
419 vc->output_surface_width,
420 vc->output_surface_height,
421 &vc->output_surfaces[i]);
422 CHECK_ST_WARNING("Error when calling vdp_output_surface_create");
423 mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n",
424 vc->output_surfaces[i]);
426 vc->num_shown_frames = 0;
428 if (vc->paused && had_frames)
429 if (video_to_output_surface(vo) >= 0)
430 flip_page_timed(vo, 0, -1);
433 static void preemption_callback(VdpDevice device, void *context)
435 struct vdpctx *vc = context;
436 vc->is_preempted = true;
437 vc->preemption_acked = false;
440 /* Initialize vdp_get_proc_address, called from preinit() */
441 static int win_x11_init_vdpau_procs(struct vo *vo)
443 struct vo_x11_state *x11 = vo->x11;
444 struct vdpctx *vc = vo->priv;
445 talloc_free(vc->vdp); // In case this is reinitialization after preemption
446 struct vdp_functions *vdp = talloc_zero(vc, struct vdp_functions);
447 vc->vdp = vdp;
448 VdpStatus vdp_st;
450 struct vdp_function {
451 const int id;
452 int offset;
455 const struct vdp_function *dsc;
457 static const struct vdp_function vdp_func[] = {
458 #define VDP_FUNCTION(_, macro_name, mp_name) {macro_name, offsetof(struct vdp_functions, mp_name)},
459 #include "vdpau_template.c"
460 #undef VDP_FUNCTION
461 {0, -1}
464 vdp_st = vdp_device_create_x11(x11->display, x11->screen,&vc->vdp_device,
465 &vc->vdp_get_proc_address);
466 if (vdp_st != VDP_STATUS_OK) {
467 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling "
468 "vdp_device_create_x11: %i\n", vdp_st);
469 return -1;
472 vdp->get_error_string = NULL;
473 for (dsc = vdp_func; dsc->offset >= 0; dsc++) {
474 vdp_st = vc->vdp_get_proc_address(vc->vdp_device, dsc->id,
475 (void **)((char *)vdp + dsc->offset));
476 if (vdp_st != VDP_STATUS_OK) {
477 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling "
478 "vdp_get_proc_address(function id %d): %s\n", dsc->id,
479 vdp->get_error_string ? vdp->get_error_string(vdp_st) : "?");
480 return -1;
483 vdp_st = vdp->preemption_callback_register(vc->vdp_device,
484 preemption_callback, vc);
485 return 0;
488 static int win_x11_init_vdpau_flip_queue(struct vo *vo)
490 struct vdpctx *vc = vo->priv;
491 struct vdp_functions *vdp = vc->vdp;
492 struct vo_x11_state *x11 = vo->x11;
493 VdpStatus vdp_st;
495 if (vc->flip_target == VDP_INVALID_HANDLE) {
496 vdp_st = vdp->presentation_queue_target_create_x11(vc->vdp_device,
497 x11->window,
498 &vc->flip_target);
499 CHECK_ST_ERROR("Error when calling "
500 "vdp_presentation_queue_target_create_x11");
503 /* Emperically this seems to be the first call which fails when we
504 * try to reinit after preemption while the user is still switched
505 * from X to a virtual terminal (creating the vdp_device initially
506 * succeeds, as does creating the flip_target above). This is
507 * probably not guaranteed behavior, but we'll assume it as a simple
508 * way to reduce warnings while trying to recover from preemption.
510 if (vc->flip_queue == VDP_INVALID_HANDLE) {
511 vdp_st = vdp->presentation_queue_create(vc->vdp_device, vc->flip_target,
512 &vc->flip_queue);
513 if (vc->is_preempted && vdp_st != VDP_STATUS_OK) {
514 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Failed to create flip queue "
515 "while preempted: %s\n", vdp->get_error_string(vdp_st));
516 return -1;
517 } else
518 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create");
521 VdpTime vdp_time;
522 vdp_st = vdp->presentation_queue_get_time(vc->flip_queue, &vdp_time);
523 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_get_time");
524 vc->last_vdp_time = vdp_time;
525 vc->last_sync_update = GetTimer();
527 vc->vsync_interval = 1;
528 if (vc->user_fps > 0) {
529 vc->vsync_interval = 1e9 / vc->user_fps;
530 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Assuming user-specified display "
531 "refresh rate of %.3f Hz.\n", vc->user_fps);
532 } else if (vc->user_fps == 0) {
533 #ifdef CONFIG_XF86VM
534 double fps = vo_vm_get_fps(vo);
535 if (!fps)
536 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Failed to get display FPS\n");
537 else {
538 vc->vsync_interval = 1e9 / fps;
539 // This is verbose, but I'm not yet sure how common wrong values are
540 mp_msg(MSGT_VO, MSGL_INFO,
541 "[vdpau] Got display refresh rate %.3f Hz.\n"
542 "[vdpau] If that value looks wrong give the "
543 "-vo vdpau:fps=X suboption manually.\n", fps);
545 #else
546 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] This binary has been compiled "
547 "without XF86VidMode support.\n");
548 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Can't use vsync-aware timing "
549 "without manually provided -vo vdpau:fps=X suboption.\n");
550 #endif
551 } else
552 mp_msg(MSGT_VO, MSGL_V, "[vdpau] framedrop/timing logic disabled by "
553 "user.\n");
555 return 0;
558 static int set_video_attribute(struct vdpctx *vc, VdpVideoMixerAttribute attr,
559 const void *value, char *attr_name)
561 struct vdp_functions *vdp = vc->vdp;
562 VdpStatus vdp_st;
564 vdp_st = vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, &attr,
565 &value);
566 if (vdp_st != VDP_STATUS_OK) {
567 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error setting video mixer "
568 "attribute %s: %s\n", attr_name, vdp->get_error_string(vdp_st));
569 return -1;
571 return 0;
574 static void update_csc_matrix(struct vo *vo)
576 struct vdpctx *vc = vo->priv;
577 struct vdp_functions *vdp = vc->vdp;
578 VdpStatus vdp_st;
580 const VdpColorStandard vdp_colors[] = {VDP_COLOR_STANDARD_ITUR_BT_601,
581 VDP_COLOR_STANDARD_ITUR_BT_709,
582 VDP_COLOR_STANDARD_SMPTE_240M};
583 char * const vdp_names[] = {"BT.601", "BT.709", "SMPTE-240M"};
584 int csp = vc->colorspace;
585 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Updating CSC matrix for %s\n",
586 vdp_names[csp]);
588 VdpCSCMatrix matrix;
589 vdp_st = vdp->generate_csc_matrix(&vc->procamp, vdp_colors[csp], &matrix);
590 CHECK_ST_WARNING("Error when generating CSC matrix");
592 if (vc->studio_levels) {
593 /* Modify matrix to change output range from 0..255 to 16..235.
594 * Clipping limits can't be changed, so out-of-range results that
595 * would have been clipped to 0 or 255 before can still go below
596 * 16 or above 235.
598 for (int i = 0; i < 3; i++) {
599 for (int j = 0; j < 4; j++)
600 matrix[i][j] *= 220. / 256;
601 matrix[i][3] += 16. / 256;
605 set_video_attribute(vc, VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX,
606 &matrix, "CSC matrix");
609 #define SET_VIDEO_ATTR(attr_name, attr_type, value) set_video_attribute(vc, \
610 VDP_VIDEO_MIXER_ATTRIBUTE_ ## attr_name, &(attr_type){value},\
611 # attr_name)
612 static int create_vdp_mixer(struct vo *vo, VdpChromaType vdp_chroma_type)
614 struct vdpctx *vc = vo->priv;
615 struct vdp_functions *vdp = vc->vdp;
616 #define VDP_NUM_MIXER_PARAMETER 3
617 #define MAX_NUM_FEATURES 6
618 int i;
619 VdpStatus vdp_st;
621 if (vc->video_mixer != VDP_INVALID_HANDLE)
622 return 0;
624 int feature_count = 0;
625 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
626 VdpBool feature_enables[MAX_NUM_FEATURES];
627 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
628 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
629 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
630 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE,
632 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
633 &vc->vid_width,
634 &vc->vid_height,
635 &vdp_chroma_type,
637 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
638 if (vc->deint == 4)
639 features[feature_count++] =
640 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
641 if (vc->pullup)
642 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
643 if (vc->denoise)
644 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
645 if (vc->sharpen)
646 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
647 if (vc->hqscaling) {
648 VdpVideoMixerFeature hqscaling_feature =
649 VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 + vc->hqscaling-1;
650 VdpBool hqscaling_available;
651 vdp_st = vdp->video_mixer_query_feature_support(vc->vdp_device,
652 hqscaling_feature,
653 &hqscaling_available);
654 CHECK_ST_ERROR("Error when calling video_mixer_query_feature_support");
655 if (hqscaling_available)
656 features[feature_count++] = hqscaling_feature;
657 else
658 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Your hardware or VDPAU "
659 "library does not support requested hqscaling.\n");
662 vdp_st = vdp->video_mixer_create(vc->vdp_device, feature_count, features,
663 VDP_NUM_MIXER_PARAMETER,
664 parameters, parameter_values,
665 &vc->video_mixer);
666 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create");
668 for (i = 0; i < feature_count; i++)
669 feature_enables[i] = VDP_TRUE;
670 if (vc->deint < 3)
671 feature_enables[0] = VDP_FALSE;
672 if (feature_count) {
673 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
674 feature_count, features,
675 feature_enables);
676 CHECK_ST_WARNING("Error calling vdp_video_mixer_set_feature_enables");
678 if (vc->denoise)
679 SET_VIDEO_ATTR(NOISE_REDUCTION_LEVEL, float, vc->denoise);
680 if (vc->sharpen)
681 SET_VIDEO_ATTR(SHARPNESS_LEVEL, float, vc->sharpen);
682 if (!vc->chroma_deint)
683 SET_VIDEO_ATTR(SKIP_CHROMA_DEINTERLACE, uint8_t, 1);
685 update_csc_matrix(vo);
686 return 0;
689 // Free everything specific to a certain video file
690 static void free_video_specific(struct vo *vo)
692 struct vdpctx *vc = vo->priv;
693 struct vdp_functions *vdp = vc->vdp;
694 int i;
695 VdpStatus vdp_st;
697 if (vc->decoder != VDP_INVALID_HANDLE)
698 vdp->decoder_destroy(vc->decoder);
699 vc->decoder = VDP_INVALID_HANDLE;
700 vc->decoder_max_refs = -1;
702 forget_frames(vo);
704 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
705 if (vc->surface_render[i].surface != VDP_INVALID_HANDLE) {
706 vdp_st = vdp->video_surface_destroy(vc->surface_render[i].surface);
707 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy");
709 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
712 if (vc->video_mixer != VDP_INVALID_HANDLE) {
713 vdp_st = vdp->video_mixer_destroy(vc->video_mixer);
714 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy");
716 vc->video_mixer = VDP_INVALID_HANDLE;
719 static int create_vdp_decoder(struct vo *vo, int max_refs)
721 struct vdpctx *vc = vo->priv;
722 struct vdp_functions *vdp = vc->vdp;
723 VdpStatus vdp_st;
724 VdpDecoderProfile vdp_decoder_profile;
725 if (vc->decoder != VDP_INVALID_HANDLE)
726 vdp->decoder_destroy(vc->decoder);
727 switch (vc->image_format) {
728 case IMGFMT_VDPAU_MPEG1:
729 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
730 break;
731 case IMGFMT_VDPAU_MPEG2:
732 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
733 break;
734 case IMGFMT_VDPAU_H264:
735 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
736 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder "
737 "for %d reference frames.\n", max_refs);
738 break;
739 case IMGFMT_VDPAU_WMV3:
740 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
741 break;
742 case IMGFMT_VDPAU_VC1:
743 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
744 break;
745 case IMGFMT_VDPAU_MPEG4:
746 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
747 break;
748 default:
749 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Unknown image format!\n");
750 goto fail;
752 vdp_st = vdp->decoder_create(vc->vdp_device, vdp_decoder_profile,
753 vc->vid_width, vc->vid_height, max_refs,
754 &vc->decoder);
755 CHECK_ST_WARNING("Failed creating VDPAU decoder");
756 if (vdp_st != VDP_STATUS_OK) {
757 fail:
758 vc->decoder = VDP_INVALID_HANDLE;
759 vc->decoder_max_refs = 0;
760 return 0;
762 vc->decoder_max_refs = max_refs;
763 return 1;
766 static int initialize_vdpau_objects(struct vo *vo)
768 struct vdpctx *vc = vo->priv;
769 struct vdp_functions *vdp = vc->vdp;
770 VdpStatus vdp_st;
772 vc->vdp_chroma_type = VDP_CHROMA_TYPE_420;
773 switch (vc->image_format) {
774 case IMGFMT_YV12:
775 case IMGFMT_I420:
776 case IMGFMT_IYUV:
777 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YV12;
778 break;
779 case IMGFMT_NV12:
780 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_NV12;
781 break;
782 case IMGFMT_YUY2:
783 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV;
784 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
785 break;
786 case IMGFMT_UYVY:
787 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY;
788 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
790 if (win_x11_init_vdpau_flip_queue(vo) < 0)
791 return -1;
793 if (create_vdp_mixer(vo, vc->vdp_chroma_type) < 0)
794 return -1;
796 vdp_st = vdp->
797 bitmap_surface_query_capabilities(vc->vdp_device,
798 VDP_RGBA_FORMAT_A8,
799 &(VdpBool){0},
800 &vc->eosd_surface.max_width,
801 &vc->eosd_surface.max_height);
802 CHECK_ST_WARNING("Query to get max EOSD surface size failed");
803 forget_frames(vo);
804 resize(vo);
805 return 0;
808 static void mark_vdpau_objects_uninitialized(struct vo *vo)
810 struct vdpctx *vc = vo->priv;
812 vc->decoder = VDP_INVALID_HANDLE;
813 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
814 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
815 forget_frames(vo);
816 vc->video_mixer = VDP_INVALID_HANDLE;
817 vc->flip_queue = VDP_INVALID_HANDLE;
818 vc->flip_target = VDP_INVALID_HANDLE;
819 for (int i = 0; i <= MAX_OUTPUT_SURFACES; i++)
820 vc->output_surfaces[i] = VDP_INVALID_HANDLE;
821 vc->vdp_device = VDP_INVALID_HANDLE;
822 vc->eosd_surface = (struct eosd_bitmap_surface){
823 .surface = VDP_INVALID_HANDLE,
825 vc->output_surface_width = vc->output_surface_height = -1;
826 vc->eosd_render_count = 0;
827 vc->num_shown_frames = 0;
830 static int handle_preemption(struct vo *vo)
832 struct vdpctx *vc = vo->priv;
834 if (!vc->is_preempted)
835 return 0;
836 if (!vc->preemption_acked)
837 mark_vdpau_objects_uninitialized(vo);
838 vc->preemption_acked = true;
839 if (!vc->preemption_user_notified) {
840 mp_tmsg(MSGT_VO, MSGL_ERR, "[vdpau] Got display preemption notice! "
841 "Will attempt to recover.\n");
842 vc->preemption_user_notified = true;
844 /* Trying to initialize seems to be quite slow, so only try once a
845 * second to avoid using 100% CPU. */
846 if (vc->last_preemption_retry_fail
847 && GetTimerMS() - vc->last_preemption_retry_fail < 1000)
848 return -1;
849 if (win_x11_init_vdpau_procs(vo) < 0 || initialize_vdpau_objects(vo) < 0) {
850 vc->last_preemption_retry_fail = GetTimerMS() | 1;
851 return -1;
853 vc->last_preemption_retry_fail = 0;
854 vc->is_preempted = false;
855 vc->preemption_user_notified = false;
856 mp_tmsg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n");
857 return 1;
861 * connect to X server, create and map window, initialize all
862 * VDPAU objects, create different surfaces etc.
864 static int config(struct vo *vo, uint32_t width, uint32_t height,
865 uint32_t d_width, uint32_t d_height, uint32_t flags,
866 char *title, uint32_t format)
868 struct vdpctx *vc = vo->priv;
869 struct vo_x11_state *x11 = vo->x11;
870 XVisualInfo vinfo;
871 XSetWindowAttributes xswa;
872 XWindowAttributes attribs;
873 unsigned long xswamask;
874 int depth;
876 #ifdef CONFIG_XF86VM
877 int vm = flags & VOFLAG_MODESWITCHING;
878 #endif
880 if (handle_preemption(vo) < 0)
881 return -1;
883 vc->flip = flags & VOFLAG_FLIPPING;
884 vc->image_format = format;
885 vc->vid_width = width;
886 vc->vid_height = height;
887 if (vc->user_colorspace == 0)
888 vc->colorspace = width >= 1280 || height > 576 ? 1 : 0;
889 else
890 vc->colorspace = vc->user_colorspace - 1;
891 free_video_specific(vo);
892 if (IMGFMT_IS_VDPAU(vc->image_format) && !create_vdp_decoder(vo, 2))
893 return -1;
895 #ifdef CONFIG_XF86VM
896 if (vm) {
897 vo_vm_switch(vo);
898 vc->mode_switched = true;
900 #endif
901 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
902 &attribs);
903 depth = attribs.depth;
904 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
905 depth = 24;
906 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
908 xswa.background_pixel = 0;
909 xswa.border_pixel = 0;
910 /* Do not use CWBackPixel: It leads to VDPAU errors after
911 * aspect ratio changes. */
912 xswamask = CWBorderPixel;
914 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, d_width, d_height,
915 flags, CopyFromParent, "vdpau", title);
916 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
918 #ifdef CONFIG_XF86VM
919 if (vm) {
920 /* Grab the mouse pointer in our window */
921 if (vo_grabpointer)
922 XGrabPointer(x11->display, x11->window, True, 0,
923 GrabModeAsync, GrabModeAsync,
924 x11->window, None, CurrentTime);
925 XSetInputFocus(x11->display, x11->window, RevertToNone, CurrentTime);
927 #endif
929 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
930 vo_fs = 1;
932 if (initialize_vdpau_objects(vo) < 0)
933 return -1;
935 return 0;
938 static void check_events(struct vo *vo)
940 struct vdpctx *vc = vo->priv;
941 struct vdp_functions *vdp = vc->vdp;
943 if (handle_preemption(vo) < 0)
944 return;
946 int e = vo_x11_check_events(vo);
948 if (e & VO_EVENT_RESIZE)
949 resize(vo);
950 else if (e & VO_EVENT_EXPOSE && vc->paused) {
951 /* did we already draw a buffer */
952 if (vc->num_shown_frames) {
953 /* redraw the last visible buffer */
954 VdpStatus vdp_st;
955 int last_surface = WRAP_ADD(vc->surface_num, -1,
956 vc->num_output_surfaces);
957 vdp_st = vdp->presentation_queue_display(vc->flip_queue,
958 vc->output_surfaces[last_surface],
959 vo->dwidth, vo->dheight, 0);
960 CHECK_ST_WARNING("Error when calling "
961 "vdp_presentation_queue_display");
966 static void draw_osd_I8A8(void *ctx, int x0, int y0, int w, int h,
967 unsigned char *src, unsigned char *srca, int stride)
969 struct vo *vo = ctx;
970 struct vdpctx *vc = vo->priv;
971 struct vdp_functions *vdp = vc->vdp;
972 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
973 VdpStatus vdp_st;
974 int i;
975 int pitch;
976 int index_data_size_required;
977 VdpRect output_indexed_rect_vid;
979 if (!w || !h)
980 return;
982 index_data_size_required = 2*w*h;
983 if (vc->index_data_size < index_data_size_required) {
984 vc->index_data = talloc_realloc_size(vc, vc->index_data,
985 index_data_size_required);
986 vc->index_data_size = index_data_size_required;
989 // index_data creation, component order - I, A, I, A, .....
990 for (i = 0; i < h; i++)
991 for (int j = 0; j < w; j++) {
992 vc->index_data[i*2*w + j*2] = src [i*stride+j];
993 vc->index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
996 output_indexed_rect_vid.x0 = x0;
997 output_indexed_rect_vid.y0 = y0;
998 output_indexed_rect_vid.x1 = x0 + w;
999 output_indexed_rect_vid.y1 = y0 + h;
1001 pitch = w*2;
1003 // write source_data to osd_surface.
1004 VdpOutputSurface osd_surface = vc->output_surfaces[vc->num_output_surfaces];
1005 vdp_st = vdp->
1006 output_surface_put_bits_indexed(osd_surface, VDP_INDEXED_FORMAT_I8A8,
1007 (const void *const*)&vc->index_data,
1008 &pitch, &output_indexed_rect_vid,
1009 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
1010 (void *)vc->palette);
1011 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed");
1013 VdpOutputSurfaceRenderBlendState blend_state = {
1014 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1015 .blend_factor_source_color =
1016 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1017 .blend_factor_source_alpha =
1018 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1019 .blend_factor_destination_color =
1020 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1021 .blend_factor_destination_alpha =
1022 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1023 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1024 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1027 vdp_st = vdp->
1028 output_surface_render_output_surface(output_surface,
1029 &output_indexed_rect_vid,
1030 osd_surface,
1031 &output_indexed_rect_vid,
1032 NULL, &blend_state,
1033 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1034 CHECK_ST_WARNING("Error when calling "
1035 "vdp_output_surface_render_output_surface");
1038 static void draw_eosd(struct vo *vo)
1040 struct vdpctx *vc = vo->priv;
1041 struct vdp_functions *vdp = vc->vdp;
1042 VdpStatus vdp_st;
1043 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
1044 int i;
1046 if (handle_preemption(vo) < 0)
1047 return;
1049 VdpOutputSurfaceRenderBlendState blend_state = {
1050 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1051 .blend_factor_source_color =
1052 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1053 .blend_factor_source_alpha =
1054 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1055 .blend_factor_destination_color =
1056 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1057 .blend_factor_destination_alpha =
1058 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1059 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1060 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1063 for (i = 0; i < vc->eosd_render_count; i++) {
1064 vdp_st = vdp->
1065 output_surface_render_bitmap_surface(output_surface,
1066 &vc->eosd_targets[i].dest,
1067 vc->eosd_surface.surface,
1068 &vc->eosd_targets[i].source,
1069 &vc->eosd_targets[i].color,
1070 &blend_state,
1071 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1072 CHECK_ST_WARNING("EOSD: Error when rendering");
1076 #define HEIGHT_SORT_BITS 4
1077 static int size_index(struct eosd_target *r)
1079 unsigned int h = r->source.y1;
1080 int n = av_log2_16bit(h);
1081 return (n << HEIGHT_SORT_BITS)
1082 + (- 1 - (h << HEIGHT_SORT_BITS >> n) & (1 << HEIGHT_SORT_BITS) - 1);
1085 /* Pack the given rectangles into an area of size w * h.
1086 * The size of each rectangle is read from .source.x1/.source.y1.
1087 * The height of each rectangle must be at least 1 and less than 65536.
1088 * The .source rectangle is then set corresponding to the packed position.
1089 * 'scratch' must point to work memory for num_rects+16 ints.
1090 * Return 0 on success, -1 if the rectangles did not fit in w*h.
1092 * The rectangles are placed in rows in order approximately sorted by
1093 * height (the approximate sorting is simpler than a full one would be,
1094 * and allows the algorithm to work in linear time). Additionally, to
1095 * reduce wasted space when there are a few tall rectangles, empty
1096 * lower-right parts of rows are filled recursively when the size of
1097 * rectangles in the row drops past a power-of-two threshold. So if a
1098 * row starts with rectangles of size 3x50, 10x40 and 5x20 then the
1099 * free rectangle with corners (13, 20)-(w, 50) is filled recursively.
1101 static int pack_rectangles(struct eosd_target *rects, int num_rects,
1102 int w, int h, int *scratch)
1104 int bins[16 << HEIGHT_SORT_BITS];
1105 int sizes[16 << HEIGHT_SORT_BITS] = {};
1106 for (int i = 0; i < num_rects; i++)
1107 sizes[size_index(rects + i)]++;
1108 int idx = 0;
1109 for (int i = 0; i < 16 << HEIGHT_SORT_BITS; i += 1 << HEIGHT_SORT_BITS) {
1110 for (int j = 0; j < 1 << HEIGHT_SORT_BITS; j++) {
1111 bins[i + j] = idx;
1112 idx += sizes[i + j];
1114 scratch[idx++] = -1;
1116 for (int i = 0; i < num_rects; i++)
1117 scratch[bins[size_index(rects + i)]++] = i;
1118 for (int i = 0; i < 16; i++)
1119 bins[i] = bins[i << HEIGHT_SORT_BITS] - sizes[i << HEIGHT_SORT_BITS];
1120 struct {
1121 int size, x, bottom;
1122 } stack[16] = {{15, 0, h}}, s = {};
1123 int stackpos = 1;
1124 int y;
1125 while (stackpos) {
1126 y = s.bottom;
1127 s = stack[--stackpos];
1128 s.size++;
1129 while (s.size--) {
1130 int maxy = -1;
1131 int obj;
1132 while ((obj = scratch[bins[s.size]]) >= 0) {
1133 int bottom = y + rects[obj].source.y1;
1134 if (bottom > s.bottom)
1135 break;
1136 int right = s.x + rects[obj].source.x1;
1137 if (right > w)
1138 break;
1139 bins[s.size]++;
1140 rects[obj].source.x0 = s.x;
1141 rects[obj].source.x1 += s.x;
1142 rects[obj].source.y0 = y;
1143 rects[obj].source.y1 += y;
1144 num_rects--;
1145 if (maxy <= 0)
1146 stack[stackpos++] = s;
1147 s.x = right;
1148 maxy = FFMAX(maxy, bottom);
1150 if (maxy > 0)
1151 s.bottom = maxy;
1154 return num_rects ? -1 : 0;
1157 static void generate_eosd(struct vo *vo, mp_eosd_images_t *imgs)
1159 struct vdpctx *vc = vo->priv;
1160 struct vdp_functions *vdp = vc->vdp;
1161 VdpStatus vdp_st;
1162 int i;
1163 ASS_Image *img = imgs->imgs;
1164 ASS_Image *p;
1165 struct eosd_bitmap_surface *sfc = &vc->eosd_surface;
1166 bool need_upload = false;
1168 if (imgs->changed == 0)
1169 return; // Nothing changed, no need to redraw
1171 vc->eosd_render_count = 0;
1173 if (!img)
1174 return; // There's nothing to render!
1176 if (imgs->changed == 1)
1177 goto eosd_skip_upload;
1179 need_upload = true;
1180 bool reallocate = false;
1181 while (1) {
1182 for (p = img, i = 0; p; p = p->next) {
1183 if (p->w <= 0 || p->h <= 0)
1184 continue;
1185 // Allocate new space for surface/target arrays
1186 if (i >= vc->eosd_targets_size) {
1187 vc->eosd_targets_size = FFMAX(vc->eosd_targets_size * 2, 512);
1188 vc->eosd_targets =
1189 talloc_realloc_size(vc, vc->eosd_targets,
1190 vc->eosd_targets_size
1191 * sizeof(*vc->eosd_targets));
1192 vc->eosd_scratch =
1193 talloc_realloc_size(vc, vc->eosd_scratch,
1194 (vc->eosd_targets_size + 16)
1195 * sizeof(*vc->eosd_scratch));
1197 vc->eosd_targets[i].source.x1 = p->w;
1198 vc->eosd_targets[i].source.y1 = p->h;
1199 i++;
1201 if (pack_rectangles(vc->eosd_targets, i, sfc->w, sfc->h,
1202 vc->eosd_scratch) >= 0)
1203 break;
1204 int w = FFMIN(FFMAX(sfc->w * 2, EOSD_SURFACE_INITIAL_SIZE),
1205 sfc->max_width);
1206 int h = FFMIN(FFMAX(sfc->h * 2, EOSD_SURFACE_INITIAL_SIZE),
1207 sfc->max_height);
1208 if (w == sfc->w && h == sfc->h) {
1209 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] EOSD bitmaps do not fit on "
1210 "a surface with the maximum supported size\n");
1211 return;
1212 } else {
1213 sfc->w = w;
1214 sfc->h = h;
1216 reallocate = true;
1218 if (reallocate) {
1219 if (sfc->surface != VDP_INVALID_HANDLE) {
1220 vdp_st = vdp->bitmap_surface_destroy(sfc->surface);
1221 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1223 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Allocating a %dx%d surface for "
1224 "EOSD bitmaps.\n", sfc->w, sfc->h);
1225 vdp_st = vdp->bitmap_surface_create(vc->vdp_device, VDP_RGBA_FORMAT_A8,
1226 sfc->w, sfc->h, true,
1227 &sfc->surface);
1228 if (vdp_st != VDP_STATUS_OK)
1229 sfc->surface = VDP_INVALID_HANDLE;
1230 CHECK_ST_WARNING("EOSD: error when creating surface");
1233 eosd_skip_upload:
1234 if (sfc->surface == VDP_INVALID_HANDLE)
1235 return;
1236 for (p = img; p; p = p->next) {
1237 if (p->w <= 0 || p->h <= 0)
1238 continue;
1239 struct eosd_target *target = &vc->eosd_targets[vc->eosd_render_count];
1240 if (need_upload) {
1241 vdp_st = vdp->
1242 bitmap_surface_put_bits_native(sfc->surface,
1243 (const void *) &p->bitmap,
1244 &p->stride, &target->source);
1245 CHECK_ST_WARNING("EOSD: putbits failed");
1247 // Render dest, color, etc.
1248 target->color.alpha = 1.0 - ((p->color >> 0) & 0xff) / 255.0;
1249 target->color.blue = ((p->color >> 8) & 0xff) / 255.0;
1250 target->color.green = ((p->color >> 16) & 0xff) / 255.0;
1251 target->color.red = ((p->color >> 24) & 0xff) / 255.0;
1252 target->dest.x0 = p->dst_x;
1253 target->dest.y0 = p->dst_y;
1254 target->dest.x1 = p->w + p->dst_x;
1255 target->dest.y1 = p->h + p->dst_y;
1256 vc->eosd_render_count++;
1260 static void draw_osd(struct vo *vo, struct osd_state *osd)
1262 struct vdpctx *vc = vo->priv;
1263 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
1265 if (handle_preemption(vo) < 0)
1266 return;
1268 osd_draw_text_ext(osd, vo->dwidth, vo->dheight, vc->border_x, vc->border_y,
1269 vc->border_x, vc->border_y, vc->vid_width,
1270 vc->vid_height, draw_osd_I8A8, vo);
1273 static int update_presentation_queue_status(struct vo *vo)
1275 struct vdpctx *vc = vo->priv;
1276 struct vdp_functions *vdp = vc->vdp;
1277 VdpStatus vdp_st;
1279 while (vc->query_surface_num != vc->surface_num) {
1280 VdpTime vtime;
1281 VdpPresentationQueueStatus status;
1282 VdpOutputSurface surface = vc->output_surfaces[vc->query_surface_num];
1283 vdp_st = vdp->presentation_queue_query_surface_status(vc->flip_queue,
1284 surface,
1285 &status, &vtime);
1286 CHECK_ST_WARNING("Error calling "
1287 "presentation_queue_query_surface_status");
1288 if (status == VDP_PRESENTATION_QUEUE_STATUS_QUEUED)
1289 break;
1290 if (vc->vsync_interval > 1) {
1291 uint64_t qtime = vc->queue_time[vc->query_surface_num];
1292 if (vtime < qtime + vc->vsync_interval / 2)
1293 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown too early\n");
1294 if (vtime > qtime + vc->vsync_interval)
1295 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown late\n");
1297 vc->query_surface_num = WRAP_ADD(vc->query_surface_num, 1,
1298 vc->num_output_surfaces);
1299 vc->recent_vsync_time = vtime;
1301 int num_queued = WRAP_ADD(vc->surface_num, -vc->query_surface_num,
1302 vc->num_output_surfaces);
1303 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Queued surface count (before add): "
1304 "%d\n", num_queued);
1305 return num_queued;
1308 static inline uint64_t prev_vs2(struct vdpctx *vc, uint64_t ts, int shift)
1310 uint64_t offset = ts - vc->recent_vsync_time;
1311 // Fix negative values for 1<<shift vsyncs before vc->recent_vsync_time
1312 offset += (uint64_t)vc->vsync_interval << shift;
1313 offset %= vc->vsync_interval;
1314 return ts - offset;
1317 static void flip_page_timed(struct vo *vo, unsigned int pts_us, int duration)
1319 struct vdpctx *vc = vo->priv;
1320 struct vdp_functions *vdp = vc->vdp;
1321 VdpStatus vdp_st;
1322 uint32_t vsync_interval = vc->vsync_interval;
1324 if (handle_preemption(vo) < 0)
1325 return;
1327 if (duration > INT_MAX / 1000)
1328 duration = -1;
1329 else
1330 duration *= 1000;
1332 if (vc->user_fps < 0)
1333 duration = -1; // Make sure drop logic is disabled
1335 uint64_t now = sync_vdptime(vo);
1336 uint64_t pts = pts_us ? convert_to_vdptime(vo, pts_us) : now;
1337 uint64_t ideal_pts = pts;
1338 uint64_t npts = duration >= 0 ? pts + duration : UINT64_MAX;
1340 #define PREV_VS2(ts, shift) prev_vs2(vc, ts, shift)
1341 // Only gives accurate results for ts >= vc->recent_vsync_time
1342 #define PREV_VSYNC(ts) PREV_VS2(ts, 0)
1344 /* We hope to be here at least one vsync before the frame should be shown.
1345 * If we are running late then don't drop the frame unless there is
1346 * already one queued for the next vsync; even if we _hope_ to show the
1347 * next frame soon enough to mean this one should be dropped we might
1348 * not make the target time in reality. Without this check we could drop
1349 * every frame, freezing the display completely if video lags behind.
1351 if (now > PREV_VSYNC(FFMAX(pts, vc->last_queue_time + vsync_interval)))
1352 npts = UINT64_MAX;
1354 /* Allow flipping a frame at a vsync if its presentation time is a
1355 * bit after that vsync and the change makes the flip time delta
1356 * from previous frame better match the target timestamp delta.
1357 * This avoids instability with frame timestamps falling near vsyncs.
1358 * For example if the frame timestamps were (with vsyncs at
1359 * integer values) 0.01, 1.99, 4.01, 5.99, 8.01, ... then
1360 * straightforward timing at next vsync would flip the frames at
1361 * 1, 2, 5, 6, 9; this changes it to 1, 2, 4, 6, 8 and so on with
1362 * regular 2-vsync intervals.
1364 * Also allow moving the frame forward if it looks like we dropped
1365 * the previous frame incorrectly (now that we know better after
1366 * having final exact timestamp information for this frame) and
1367 * there would unnecessarily be a vsync without a frame change.
1369 uint64_t vsync = PREV_VSYNC(pts);
1370 if (pts < vsync + vsync_interval / 4
1371 && (vsync - PREV_VS2(vc->last_queue_time, 16)
1372 > pts - vc->last_ideal_time + vsync_interval / 2
1373 || vc->dropped_frame && vsync > vc->dropped_time))
1374 pts -= vsync_interval / 2;
1376 vc->dropped_frame = true; // changed at end if false
1377 vc->dropped_time = ideal_pts;
1379 pts = FFMAX(pts, vc->last_queue_time + vsync_interval);
1380 pts = FFMAX(pts, now);
1381 if (npts < PREV_VSYNC(pts) + vsync_interval)
1382 return;
1384 int num_flips = update_presentation_queue_status(vo);
1385 vsync = vc->recent_vsync_time + num_flips * vc->vsync_interval;
1386 now = sync_vdptime(vo);
1387 pts = FFMAX(pts, now);
1388 pts = FFMAX(pts, vsync + (vsync_interval >> 2));
1389 vsync = PREV_VSYNC(pts);
1390 if (npts < vsync + vsync_interval)
1391 return;
1392 pts = vsync + (vsync_interval >> 2);
1393 vdp_st =
1394 vdp->presentation_queue_display(vc->flip_queue,
1395 vc->output_surfaces[vc->surface_num],
1396 vo->dwidth, vo->dheight, pts);
1397 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display");
1399 vc->last_queue_time = pts;
1400 vc->queue_time[vc->surface_num] = pts;
1401 vc->last_ideal_time = ideal_pts;
1402 vc->dropped_frame = false;
1403 vc->surface_num = WRAP_ADD(vc->surface_num, 1, vc->num_output_surfaces);
1404 vc->num_shown_frames = FFMIN(vc->num_shown_frames + 1, 1000);
1407 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
1408 int h, int x, int y)
1410 struct vdpctx *vc = vo->priv;
1411 struct vdp_functions *vdp = vc->vdp;
1412 VdpStatus vdp_st;
1414 if (handle_preemption(vo) < 0)
1415 return VO_TRUE;
1417 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
1418 int max_refs = vc->image_format == IMGFMT_VDPAU_H264 ?
1419 rndr->info.h264.num_ref_frames : 2;
1420 if (!IMGFMT_IS_VDPAU(vc->image_format))
1421 return VO_FALSE;
1422 if ((vc->decoder == VDP_INVALID_HANDLE || vc->decoder_max_refs < max_refs)
1423 && !create_vdp_decoder(vo, max_refs))
1424 return VO_FALSE;
1426 vdp_st = vdp->decoder_render(vc->decoder, rndr->surface,
1427 (void *)&rndr->info,
1428 rndr->bitstream_buffers_used,
1429 rndr->bitstream_buffers);
1430 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
1431 return VO_TRUE;
1435 static struct vdpau_render_state *get_surface(struct vo *vo, int number)
1437 struct vdpctx *vc = vo->priv;
1438 struct vdp_functions *vdp = vc->vdp;
1440 if (number > MAX_VIDEO_SURFACES)
1441 return NULL;
1442 if (vc->surface_render[number].surface == VDP_INVALID_HANDLE
1443 && !vc->is_preempted) {
1444 VdpStatus vdp_st;
1445 vdp_st = vdp->video_surface_create(vc->vdp_device, vc->vdp_chroma_type,
1446 vc->vid_width, vc->vid_height,
1447 &vc->surface_render[number].surface);
1448 CHECK_ST_WARNING("Error when calling vdp_video_surface_create");
1450 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n",
1451 vc->surface_render[number].surface);
1452 return &vc->surface_render[number];
1455 static void draw_image(struct vo *vo, mp_image_t *mpi, double pts)
1457 struct vdpctx *vc = vo->priv;
1458 struct vdp_functions *vdp = vc->vdp;
1459 struct mp_image *reserved_mpi = NULL;
1460 struct vdpau_render_state *rndr;
1462 if (vc->is_preempted) {
1463 vo->frame_loaded = true;
1464 return;
1467 if (IMGFMT_IS_VDPAU(vc->image_format)) {
1468 rndr = mpi->priv;
1469 reserved_mpi = mpi;
1470 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1471 VdpStatus vdp_st;
1472 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
1473 rndr = get_surface(vo, vc->deint_counter);
1474 vc->deint_counter = WRAP_ADD(vc->deint_counter, 1, NUM_BUFFERED_VIDEO);
1475 if (vc->image_format == IMGFMT_NV12)
1476 destdata[1] = destdata[2];
1477 vdp_st =
1478 vdp->video_surface_put_bits_y_cb_cr(rndr->surface,
1479 vc->vdp_pixel_format,
1480 (const void *const*)destdata,
1481 mpi->stride); // pitch
1482 CHECK_ST_WARNING("Error when calling "
1483 "vdp_video_surface_put_bits_y_cb_cr");
1484 } else
1485 // We don't support slice callbacks so this shouldn't occur -
1486 // I think the flags test above in pointless, but I'm adding
1487 // this instead of removing it just in case.
1488 abort();
1489 if (mpi->fields & MP_IMGFIELD_ORDERED)
1490 vc->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1491 else
1492 vc->top_field_first = 1;
1494 add_new_video_surface(vo, rndr->surface, mpi, pts);
1496 return;
1499 static uint32_t get_image(struct vo *vo, mp_image_t *mpi)
1501 struct vdpctx *vc = vo->priv;
1502 struct vdpau_render_state *rndr;
1504 // no dr for non-decoding for now
1505 if (!IMGFMT_IS_VDPAU(vc->image_format))
1506 return VO_FALSE;
1507 if (mpi->type != MP_IMGTYPE_NUMBERED)
1508 return VO_FALSE;
1510 rndr = get_surface(vo, mpi->number);
1511 if (!rndr) {
1512 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in "
1513 "get_image\n");
1514 // TODO: this probably breaks things forever, provide a dummy buffer?
1515 return VO_FALSE;
1517 mpi->flags |= MP_IMGFLAG_DIRECT;
1518 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1519 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1520 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1521 mpi->planes[0] = (void *)rndr;
1522 mpi->num_planes = 1;
1523 mpi->priv = rndr;
1524 return VO_TRUE;
1527 static int query_format(uint32_t format)
1529 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
1530 | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD
1531 | VFCAP_EOSD_UNSCALED | VFCAP_FLIP;
1532 switch (format) {
1533 case IMGFMT_YV12:
1534 case IMGFMT_I420:
1535 case IMGFMT_IYUV:
1536 case IMGFMT_NV12:
1537 case IMGFMT_YUY2:
1538 case IMGFMT_UYVY:
1539 return default_flags | VOCAP_NOSLICES;
1540 case IMGFMT_VDPAU_MPEG1:
1541 case IMGFMT_VDPAU_MPEG2:
1542 case IMGFMT_VDPAU_H264:
1543 case IMGFMT_VDPAU_WMV3:
1544 case IMGFMT_VDPAU_VC1:
1545 case IMGFMT_VDPAU_MPEG4:
1546 return default_flags;
1548 return 0;
1551 static void destroy_vdpau_objects(struct vo *vo)
1553 struct vdpctx *vc = vo->priv;
1554 struct vdp_functions *vdp = vc->vdp;
1556 int i;
1557 VdpStatus vdp_st;
1559 free_video_specific(vo);
1561 if (vc->flip_queue != VDP_INVALID_HANDLE) {
1562 vdp_st = vdp->presentation_queue_destroy(vc->flip_queue);
1563 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy");
1566 if (vc->flip_target != VDP_INVALID_HANDLE) {
1567 vdp_st = vdp->presentation_queue_target_destroy(vc->flip_target);
1568 CHECK_ST_WARNING("Error when calling "
1569 "vdp_presentation_queue_target_destroy");
1572 for (i = 0; i <= vc->num_output_surfaces; i++) {
1573 if (vc->output_surfaces[i] == VDP_INVALID_HANDLE)
1574 continue;
1575 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
1576 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy");
1579 if (vc->eosd_surface.surface != VDP_INVALID_HANDLE) {
1580 vdp_st = vdp->bitmap_surface_destroy(vc->eosd_surface.surface);
1581 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1584 vdp_st = vdp->device_destroy(vc->vdp_device);
1585 CHECK_ST_WARNING("Error when calling vdp_device_destroy");
1588 static void uninit(struct vo *vo)
1590 struct vdpctx *vc = vo->priv;
1592 /* Destroy all vdpau objects */
1593 destroy_vdpau_objects(vo);
1595 #ifdef CONFIG_XF86VM
1596 if (vc->mode_switched)
1597 vo_vm_close(vo);
1598 #endif
1599 vo_x11_uninit(vo);
1601 // Free bitstream buffers allocated by FFmpeg
1602 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
1603 av_freep(&vc->surface_render[i].bitstream_buffers);
1606 static int preinit(struct vo *vo, const char *arg)
1608 int i;
1610 struct vdpctx *vc = talloc_zero(vo, struct vdpctx);
1611 vo->priv = vc;
1613 // Mark everything as invalid first so uninit() can tell what has been
1614 // allocated
1615 mark_vdpau_objects_uninitialized(vo);
1617 vc->deint_type = 3;
1618 vc->chroma_deint = 1;
1619 vc->user_colorspace = 1;
1620 vc->flip_offset_window = 50;
1621 vc->flip_offset_fs = 50;
1622 vc->num_output_surfaces = 3;
1623 const opt_t subopts[] = {
1624 {"deint", OPT_ARG_INT, &vc->deint, (opt_test_f)int_non_neg},
1625 {"chroma-deint", OPT_ARG_BOOL, &vc->chroma_deint, NULL},
1626 {"pullup", OPT_ARG_BOOL, &vc->pullup, NULL},
1627 {"denoise", OPT_ARG_FLOAT, &vc->denoise, NULL},
1628 {"sharpen", OPT_ARG_FLOAT, &vc->sharpen, NULL},
1629 {"colorspace", OPT_ARG_INT, &vc->user_colorspace, NULL},
1630 {"studio", OPT_ARG_BOOL, &vc->studio_levels, NULL},
1631 {"hqscaling", OPT_ARG_INT, &vc->hqscaling, NULL},
1632 {"fps", OPT_ARG_FLOAT, &vc->user_fps, NULL},
1633 {"queuetime_windowed", OPT_ARG_INT, &vc->flip_offset_window, NULL},
1634 {"queuetime_fs", OPT_ARG_INT, &vc->flip_offset_fs, NULL},
1635 {"output_surfaces", OPT_ARG_INT, &vc->num_output_surfaces, NULL},
1636 {NULL}
1638 if (subopt_parse(arg, subopts) != 0) {
1639 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Could not parse suboptions.\n");
1640 return -1;
1642 if (vc->hqscaling < 0 || vc->hqscaling > 9) {
1643 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid value for suboption "
1644 "hqscaling\n");
1645 return -1;
1647 if (vc->num_output_surfaces < 2) {
1648 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid suboption "
1649 "output_surfaces: can't use less than 2 surfaces\n");
1650 return -1;
1652 if (vc->num_output_surfaces > MAX_OUTPUT_SURFACES) {
1653 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Number of output surfaces "
1654 "is limited to %d.\n", MAX_OUTPUT_SURFACES);
1655 vc->num_output_surfaces = MAX_OUTPUT_SURFACES;
1657 if (vc->deint)
1658 vc->deint_type = vc->deint;
1660 if (!vo_init(vo))
1661 return -1;
1663 // After this calling uninit() should work to free resources
1665 if (win_x11_init_vdpau_procs(vo) < 0) {
1666 if (vc->vdp->device_destroy)
1667 vc->vdp->device_destroy(vc->vdp_device);
1668 vo_x11_uninit(vo);
1669 return -1;
1672 // full grayscale palette.
1673 for (i = 0; i < PALETTE_SIZE; ++i)
1674 vc->palette[i] = (i << 16) | (i << 8) | i;
1676 vc->procamp.struct_version = VDP_PROCAMP_VERSION;
1677 vc->procamp.brightness = 0.0;
1678 vc->procamp.contrast = 1.0;
1679 vc->procamp.saturation = 1.0;
1680 vc->procamp.hue = 0.0;
1682 return 0;
1685 static int get_equalizer(struct vo *vo, const char *name, int *value)
1687 struct vdpctx *vc = vo->priv;
1689 if (!strcasecmp(name, "brightness"))
1690 *value = vc->procamp.brightness * 100;
1691 else if (!strcasecmp(name, "contrast"))
1692 *value = (vc->procamp.contrast - 1.0) * 100;
1693 else if (!strcasecmp(name, "saturation"))
1694 *value = (vc->procamp.saturation - 1.0) * 100;
1695 else if (!strcasecmp(name, "hue"))
1696 *value = vc->procamp.hue * 100 / M_PI;
1697 else
1698 return VO_NOTIMPL;
1699 return VO_TRUE;
1702 static int set_equalizer(struct vo *vo, const char *name, int value)
1704 struct vdpctx *vc = vo->priv;
1706 if (!strcasecmp(name, "brightness"))
1707 vc->procamp.brightness = value / 100.0;
1708 else if (!strcasecmp(name, "contrast"))
1709 vc->procamp.contrast = value / 100.0 + 1.0;
1710 else if (!strcasecmp(name, "saturation"))
1711 vc->procamp.saturation = value / 100.0 + 1.0;
1712 else if (!strcasecmp(name, "hue"))
1713 vc->procamp.hue = value / 100.0 * M_PI;
1714 else
1715 return VO_NOTIMPL;
1717 update_csc_matrix(vo);
1718 return true;
1721 static int control(struct vo *vo, uint32_t request, void *data)
1723 struct vdpctx *vc = vo->priv;
1724 struct vdp_functions *vdp = vc->vdp;
1726 handle_preemption(vo);
1728 switch (request) {
1729 case VOCTRL_GET_DEINTERLACE:
1730 *(int*)data = vc->deint;
1731 return VO_TRUE;
1732 case VOCTRL_SET_DEINTERLACE:
1733 vc->deint = *(int*)data;
1734 if (vc->deint)
1735 vc->deint = vc->deint_type;
1736 if (vc->deint_type > 2) {
1737 VdpStatus vdp_st;
1738 VdpVideoMixerFeature features[1] =
1739 {vc->deint_type == 3 ?
1740 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1741 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1742 VdpBool feature_enables[1] = {vc->deint ? VDP_TRUE : VDP_FALSE};
1743 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
1744 1, features,
1745 feature_enables);
1746 CHECK_ST_WARNING("Error changing deinterlacing settings");
1748 return VO_TRUE;
1749 case VOCTRL_PAUSE:
1750 if (vc->dropped_frame)
1751 flip_page_timed(vo, 0, -1);
1752 return (vc->paused = true);
1753 case VOCTRL_RESUME:
1754 return (vc->paused = false);
1755 case VOCTRL_QUERY_FORMAT:
1756 return query_format(*(uint32_t *)data);
1757 case VOCTRL_GET_IMAGE:
1758 return get_image(vo, data);
1759 case VOCTRL_DRAW_IMAGE:
1760 abort(); // draw_image() should get called directly
1761 case VOCTRL_BORDER:
1762 vo_x11_border(vo);
1763 resize(vo);
1764 return VO_TRUE;
1765 case VOCTRL_FULLSCREEN:
1766 vo_x11_fullscreen(vo);
1767 resize(vo);
1768 return VO_TRUE;
1769 case VOCTRL_GET_PANSCAN:
1770 return VO_TRUE;
1771 case VOCTRL_SET_PANSCAN:
1772 resize(vo);
1773 return VO_TRUE;
1774 case VOCTRL_SET_EQUALIZER: {
1775 struct voctrl_set_equalizer_args *args = data;
1776 return set_equalizer(vo, args->name, args->value);
1778 case VOCTRL_GET_EQUALIZER: {
1779 struct voctrl_get_equalizer_args *args = data;
1780 return get_equalizer(vo, args->name, args->valueptr);
1782 case VOCTRL_SET_YUV_COLORSPACE:
1783 vc->colorspace = *(int *)data % 3;
1784 update_csc_matrix(vo);
1785 return true;
1786 case VOCTRL_GET_YUV_COLORSPACE:
1787 *(int *)data = vc->colorspace;
1788 return true;
1789 case VOCTRL_ONTOP:
1790 vo_x11_ontop(vo);
1791 return VO_TRUE;
1792 case VOCTRL_UPDATE_SCREENINFO:
1793 update_xinerama_info(vo);
1794 return VO_TRUE;
1795 case VOCTRL_DRAW_EOSD:
1796 if (!data)
1797 return VO_FALSE;
1798 generate_eosd(vo, data);
1799 draw_eosd(vo);
1800 return VO_TRUE;
1801 case VOCTRL_GET_EOSD_RES: {
1802 mp_eosd_res_t *r = data;
1803 r->w = vo->dwidth;
1804 r->h = vo->dheight;
1805 r->ml = r->mr = vc->border_x;
1806 r->mt = r->mb = vc->border_y;
1807 return VO_TRUE;
1809 case VOCTRL_REDRAW_OSD:
1810 video_to_output_surface(vo);
1811 draw_eosd(vo);
1812 draw_osd(vo, data);
1813 flip_page_timed(vo, 0, -1);
1814 return true;
1815 case VOCTRL_RESET:
1816 forget_frames(vo);
1817 return true;
1819 return VO_NOTIMPL;
1822 const struct vo_driver video_out_vdpau = {
1823 .is_new = true,
1824 .buffer_frames = true,
1825 .info = &(const struct vo_info_s){
1826 "VDPAU with X11",
1827 "vdpau",
1828 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
1831 .preinit = preinit,
1832 .config = config,
1833 .control = control,
1834 .draw_image = draw_image,
1835 .get_buffered_frame = get_buffered_frame,
1836 .draw_slice = draw_slice,
1837 .draw_osd = draw_osd,
1838 .flip_page_timed = flip_page_timed,
1839 .check_events = check_events,
1840 .uninit = uninit,