configure: fix disabling of (x)mga without swscale internals
[mplayer/glamo.git] / libvo / vo_vdpau.c
blobaf4943ed1bc06f46e6c06a210baa393df5152db2
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 #ifdef VDP_DECODER_PROFILE_MPEG4_PART2_ASP
746 case IMGFMT_VDPAU_MPEG4:
747 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
748 break;
749 #endif
751 vdp_st = vdp->decoder_create(vc->vdp_device, vdp_decoder_profile,
752 vc->vid_width, vc->vid_height, max_refs,
753 &vc->decoder);
754 CHECK_ST_WARNING("Failed creating VDPAU decoder");
755 if (vdp_st != VDP_STATUS_OK) {
756 vc->decoder = VDP_INVALID_HANDLE;
757 vc->decoder_max_refs = 0;
758 return 0;
760 vc->decoder_max_refs = max_refs;
761 return 1;
764 static int initialize_vdpau_objects(struct vo *vo)
766 struct vdpctx *vc = vo->priv;
767 struct vdp_functions *vdp = vc->vdp;
768 VdpStatus vdp_st;
770 vc->vdp_chroma_type = VDP_CHROMA_TYPE_420;
771 switch (vc->image_format) {
772 case IMGFMT_YV12:
773 case IMGFMT_I420:
774 case IMGFMT_IYUV:
775 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YV12;
776 break;
777 case IMGFMT_NV12:
778 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_NV12;
779 break;
780 case IMGFMT_YUY2:
781 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV;
782 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
783 break;
784 case IMGFMT_UYVY:
785 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY;
786 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
788 if (win_x11_init_vdpau_flip_queue(vo) < 0)
789 return -1;
791 if (create_vdp_mixer(vo, vc->vdp_chroma_type) < 0)
792 return -1;
794 vdp_st = vdp->
795 bitmap_surface_query_capabilities(vc->vdp_device,
796 VDP_RGBA_FORMAT_A8,
797 &(VdpBool){0},
798 &vc->eosd_surface.max_width,
799 &vc->eosd_surface.max_height);
800 CHECK_ST_WARNING("Query to get max EOSD surface size failed");
801 forget_frames(vo);
802 resize(vo);
803 return 0;
806 static void mark_vdpau_objects_uninitialized(struct vo *vo)
808 struct vdpctx *vc = vo->priv;
810 vc->decoder = VDP_INVALID_HANDLE;
811 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
812 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
813 forget_frames(vo);
814 vc->video_mixer = VDP_INVALID_HANDLE;
815 vc->flip_queue = VDP_INVALID_HANDLE;
816 vc->flip_target = VDP_INVALID_HANDLE;
817 for (int i = 0; i <= MAX_OUTPUT_SURFACES; i++)
818 vc->output_surfaces[i] = VDP_INVALID_HANDLE;
819 vc->vdp_device = VDP_INVALID_HANDLE;
820 vc->eosd_surface = (struct eosd_bitmap_surface){
821 .surface = VDP_INVALID_HANDLE,
823 vc->output_surface_width = vc->output_surface_height = -1;
824 vc->eosd_render_count = 0;
825 vc->num_shown_frames = 0;
828 static int handle_preemption(struct vo *vo)
830 struct vdpctx *vc = vo->priv;
832 if (!vc->is_preempted)
833 return 0;
834 if (!vc->preemption_acked)
835 mark_vdpau_objects_uninitialized(vo);
836 vc->preemption_acked = true;
837 if (!vc->preemption_user_notified) {
838 mp_tmsg(MSGT_VO, MSGL_ERR, "[vdpau] Got display preemption notice! "
839 "Will attempt to recover.\n");
840 vc->preemption_user_notified = true;
842 /* Trying to initialize seems to be quite slow, so only try once a
843 * second to avoid using 100% CPU. */
844 if (vc->last_preemption_retry_fail
845 && GetTimerMS() - vc->last_preemption_retry_fail < 1000)
846 return -1;
847 if (win_x11_init_vdpau_procs(vo) < 0 || initialize_vdpau_objects(vo) < 0) {
848 vc->last_preemption_retry_fail = GetTimerMS() | 1;
849 return -1;
851 vc->last_preemption_retry_fail = 0;
852 vc->is_preempted = false;
853 vc->preemption_user_notified = false;
854 mp_tmsg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n");
855 return 1;
859 * connect to X server, create and map window, initialize all
860 * VDPAU objects, create different surfaces etc.
862 static int config(struct vo *vo, uint32_t width, uint32_t height,
863 uint32_t d_width, uint32_t d_height, uint32_t flags,
864 char *title, uint32_t format)
866 struct vdpctx *vc = vo->priv;
867 struct vo_x11_state *x11 = vo->x11;
868 XVisualInfo vinfo;
869 XSetWindowAttributes xswa;
870 XWindowAttributes attribs;
871 unsigned long xswamask;
872 int depth;
874 #ifdef CONFIG_XF86VM
875 int vm = flags & VOFLAG_MODESWITCHING;
876 #endif
878 if (handle_preemption(vo) < 0)
879 return -1;
881 vc->flip = flags & VOFLAG_FLIPPING;
882 vc->image_format = format;
883 vc->vid_width = width;
884 vc->vid_height = height;
885 if (vc->user_colorspace == 0)
886 vc->colorspace = width >= 1280 || height > 576 ? 1 : 0;
887 else
888 vc->colorspace = vc->user_colorspace - 1;
889 free_video_specific(vo);
890 if (IMGFMT_IS_VDPAU(vc->image_format) && !create_vdp_decoder(vo, 2))
891 return -1;
893 #ifdef CONFIG_XF86VM
894 if (vm) {
895 vo_vm_switch(vo);
896 vc->mode_switched = true;
898 #endif
899 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
900 &attribs);
901 depth = attribs.depth;
902 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
903 depth = 24;
904 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
906 xswa.background_pixel = 0;
907 xswa.border_pixel = 0;
908 /* Do not use CWBackPixel: It leads to VDPAU errors after
909 * aspect ratio changes. */
910 xswamask = CWBorderPixel;
912 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, d_width, d_height,
913 flags, CopyFromParent, "vdpau", title);
914 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
916 #ifdef CONFIG_XF86VM
917 if (vm) {
918 /* Grab the mouse pointer in our window */
919 if (vo_grabpointer)
920 XGrabPointer(x11->display, x11->window, True, 0,
921 GrabModeAsync, GrabModeAsync,
922 x11->window, None, CurrentTime);
923 XSetInputFocus(x11->display, x11->window, RevertToNone, CurrentTime);
925 #endif
927 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
928 vo_fs = 1;
930 if (initialize_vdpau_objects(vo) < 0)
931 return -1;
933 return 0;
936 static void check_events(struct vo *vo)
938 struct vdpctx *vc = vo->priv;
939 struct vdp_functions *vdp = vc->vdp;
941 if (handle_preemption(vo) < 0)
942 return;
944 int e = vo_x11_check_events(vo);
946 if (e & VO_EVENT_RESIZE)
947 resize(vo);
948 else if (e & VO_EVENT_EXPOSE && vc->paused) {
949 /* did we already draw a buffer */
950 if (vc->num_shown_frames) {
951 /* redraw the last visible buffer */
952 VdpStatus vdp_st;
953 int last_surface = WRAP_ADD(vc->surface_num, -1,
954 vc->num_output_surfaces);
955 vdp_st = vdp->presentation_queue_display(vc->flip_queue,
956 vc->output_surfaces[last_surface],
957 vo->dwidth, vo->dheight, 0);
958 CHECK_ST_WARNING("Error when calling "
959 "vdp_presentation_queue_display");
964 static void draw_osd_I8A8(void *ctx, int x0, int y0, int w, int h,
965 unsigned char *src, unsigned char *srca, int stride)
967 struct vo *vo = ctx;
968 struct vdpctx *vc = vo->priv;
969 struct vdp_functions *vdp = vc->vdp;
970 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
971 VdpStatus vdp_st;
972 int i;
973 int pitch;
974 int index_data_size_required;
975 VdpRect output_indexed_rect_vid;
977 if (!w || !h)
978 return;
980 index_data_size_required = 2*w*h;
981 if (vc->index_data_size < index_data_size_required) {
982 vc->index_data = talloc_realloc_size(vc, vc->index_data,
983 index_data_size_required);
984 vc->index_data_size = index_data_size_required;
987 // index_data creation, component order - I, A, I, A, .....
988 for (i = 0; i < h; i++)
989 for (int j = 0; j < w; j++) {
990 vc->index_data[i*2*w + j*2] = src [i*stride+j];
991 vc->index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
994 output_indexed_rect_vid.x0 = x0;
995 output_indexed_rect_vid.y0 = y0;
996 output_indexed_rect_vid.x1 = x0 + w;
997 output_indexed_rect_vid.y1 = y0 + h;
999 pitch = w*2;
1001 // write source_data to osd_surface.
1002 VdpOutputSurface osd_surface = vc->output_surfaces[vc->num_output_surfaces];
1003 vdp_st = vdp->
1004 output_surface_put_bits_indexed(osd_surface, VDP_INDEXED_FORMAT_I8A8,
1005 (const void *const*)&vc->index_data,
1006 &pitch, &output_indexed_rect_vid,
1007 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
1008 (void *)vc->palette);
1009 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed");
1011 VdpOutputSurfaceRenderBlendState blend_state = {
1012 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1013 .blend_factor_source_color =
1014 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1015 .blend_factor_source_alpha =
1016 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1017 .blend_factor_destination_color =
1018 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1019 .blend_factor_destination_alpha =
1020 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1021 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1022 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1025 vdp_st = vdp->
1026 output_surface_render_output_surface(output_surface,
1027 &output_indexed_rect_vid,
1028 osd_surface,
1029 &output_indexed_rect_vid,
1030 NULL, &blend_state,
1031 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1032 CHECK_ST_WARNING("Error when calling "
1033 "vdp_output_surface_render_output_surface");
1036 static void draw_eosd(struct vo *vo)
1038 struct vdpctx *vc = vo->priv;
1039 struct vdp_functions *vdp = vc->vdp;
1040 VdpStatus vdp_st;
1041 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
1042 int i;
1044 if (handle_preemption(vo) < 0)
1045 return;
1047 VdpOutputSurfaceRenderBlendState blend_state = {
1048 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1049 .blend_factor_source_color =
1050 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1051 .blend_factor_source_alpha =
1052 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1053 .blend_factor_destination_color =
1054 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1055 .blend_factor_destination_alpha =
1056 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1057 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1058 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1061 for (i = 0; i < vc->eosd_render_count; i++) {
1062 vdp_st = vdp->
1063 output_surface_render_bitmap_surface(output_surface,
1064 &vc->eosd_targets[i].dest,
1065 vc->eosd_surface.surface,
1066 &vc->eosd_targets[i].source,
1067 &vc->eosd_targets[i].color,
1068 &blend_state,
1069 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1070 CHECK_ST_WARNING("EOSD: Error when rendering");
1074 #define HEIGHT_SORT_BITS 4
1075 static int size_index(struct eosd_target *r)
1077 unsigned int h = r->source.y1;
1078 int n = av_log2_16bit(h);
1079 return (n << HEIGHT_SORT_BITS)
1080 + (- 1 - (h << HEIGHT_SORT_BITS >> n) & (1 << HEIGHT_SORT_BITS) - 1);
1083 /* Pack the given rectangles into an area of size w * h.
1084 * The size of each rectangle is read from .source.x1/.source.y1.
1085 * The height of each rectangle must be at least 1 and less than 65536.
1086 * The .source rectangle is then set corresponding to the packed position.
1087 * 'scratch' must point to work memory for num_rects+16 ints.
1088 * Return 0 on success, -1 if the rectangles did not fit in w*h.
1090 * The rectangles are placed in rows in order approximately sorted by
1091 * height (the approximate sorting is simpler than a full one would be,
1092 * and allows the algorithm to work in linear time). Additionally, to
1093 * reduce wasted space when there are a few tall rectangles, empty
1094 * lower-right parts of rows are filled recursively when the size of
1095 * rectangles in the row drops past a power-of-two threshold. So if a
1096 * row starts with rectangles of size 3x50, 10x40 and 5x20 then the
1097 * free rectangle with corners (13, 20)-(w, 50) is filled recursively.
1099 static int pack_rectangles(struct eosd_target *rects, int num_rects,
1100 int w, int h, int *scratch)
1102 int bins[16 << HEIGHT_SORT_BITS];
1103 int sizes[16 << HEIGHT_SORT_BITS] = {};
1104 for (int i = 0; i < num_rects; i++)
1105 sizes[size_index(rects + i)]++;
1106 int idx = 0;
1107 for (int i = 0; i < 16 << HEIGHT_SORT_BITS; i += 1 << HEIGHT_SORT_BITS) {
1108 for (int j = 0; j < 1 << HEIGHT_SORT_BITS; j++) {
1109 bins[i + j] = idx;
1110 idx += sizes[i + j];
1112 scratch[idx++] = -1;
1114 for (int i = 0; i < num_rects; i++)
1115 scratch[bins[size_index(rects + i)]++] = i;
1116 for (int i = 0; i < 16; i++)
1117 bins[i] = bins[i << HEIGHT_SORT_BITS] - sizes[i << HEIGHT_SORT_BITS];
1118 struct {
1119 int size, x, bottom;
1120 } stack[16] = {{15, 0, h}}, s = {};
1121 int stackpos = 1;
1122 int y;
1123 while (stackpos) {
1124 y = s.bottom;
1125 s = stack[--stackpos];
1126 s.size++;
1127 while (s.size--) {
1128 int maxy = -1;
1129 int obj;
1130 while ((obj = scratch[bins[s.size]]) >= 0) {
1131 int bottom = y + rects[obj].source.y1;
1132 if (bottom > s.bottom)
1133 break;
1134 int right = s.x + rects[obj].source.x1;
1135 if (right > w)
1136 break;
1137 bins[s.size]++;
1138 rects[obj].source.x0 = s.x;
1139 rects[obj].source.x1 += s.x;
1140 rects[obj].source.y0 = y;
1141 rects[obj].source.y1 += y;
1142 num_rects--;
1143 if (maxy <= 0)
1144 stack[stackpos++] = s;
1145 s.x = right;
1146 maxy = FFMAX(maxy, bottom);
1148 if (maxy > 0)
1149 s.bottom = maxy;
1152 return num_rects ? -1 : 0;
1155 static void generate_eosd(struct vo *vo, mp_eosd_images_t *imgs)
1157 struct vdpctx *vc = vo->priv;
1158 struct vdp_functions *vdp = vc->vdp;
1159 VdpStatus vdp_st;
1160 int i;
1161 ASS_Image *img = imgs->imgs;
1162 ASS_Image *p;
1163 struct eosd_bitmap_surface *sfc = &vc->eosd_surface;
1164 bool need_upload = false;
1166 if (imgs->changed == 0)
1167 return; // Nothing changed, no need to redraw
1169 vc->eosd_render_count = 0;
1171 if (!img)
1172 return; // There's nothing to render!
1174 if (imgs->changed == 1)
1175 goto eosd_skip_upload;
1177 need_upload = true;
1178 bool reallocate = false;
1179 while (1) {
1180 for (p = img, i = 0; p; p = p->next) {
1181 if (p->w <= 0 || p->h <= 0)
1182 continue;
1183 // Allocate new space for surface/target arrays
1184 if (i >= vc->eosd_targets_size) {
1185 vc->eosd_targets_size = FFMAX(vc->eosd_targets_size * 2, 512);
1186 vc->eosd_targets =
1187 talloc_realloc_size(vc, vc->eosd_targets,
1188 vc->eosd_targets_size
1189 * sizeof(*vc->eosd_targets));
1190 vc->eosd_scratch =
1191 talloc_realloc_size(vc, vc->eosd_scratch,
1192 (vc->eosd_targets_size + 16)
1193 * sizeof(*vc->eosd_scratch));
1195 vc->eosd_targets[i].source.x1 = p->w;
1196 vc->eosd_targets[i].source.y1 = p->h;
1197 i++;
1199 if (pack_rectangles(vc->eosd_targets, i, sfc->w, sfc->h,
1200 vc->eosd_scratch) >= 0)
1201 break;
1202 int w = FFMIN(FFMAX(sfc->w * 2, EOSD_SURFACE_INITIAL_SIZE),
1203 sfc->max_width);
1204 int h = FFMIN(FFMAX(sfc->h * 2, EOSD_SURFACE_INITIAL_SIZE),
1205 sfc->max_height);
1206 if (w == sfc->w && h == sfc->h) {
1207 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] EOSD bitmaps do not fit on "
1208 "a surface with the maximum supported size\n");
1209 return;
1210 } else {
1211 sfc->w = w;
1212 sfc->h = h;
1214 reallocate = true;
1216 if (reallocate) {
1217 if (sfc->surface != VDP_INVALID_HANDLE) {
1218 vdp_st = vdp->bitmap_surface_destroy(sfc->surface);
1219 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1221 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Allocating a %dx%d surface for "
1222 "EOSD bitmaps.\n", sfc->w, sfc->h);
1223 vdp_st = vdp->bitmap_surface_create(vc->vdp_device, VDP_RGBA_FORMAT_A8,
1224 sfc->w, sfc->h, true,
1225 &sfc->surface);
1226 if (vdp_st != VDP_STATUS_OK)
1227 sfc->surface = VDP_INVALID_HANDLE;
1228 CHECK_ST_WARNING("EOSD: error when creating surface");
1231 eosd_skip_upload:
1232 if (sfc->surface == VDP_INVALID_HANDLE)
1233 return;
1234 for (p = img; p; p = p->next) {
1235 if (p->w <= 0 || p->h <= 0)
1236 continue;
1237 struct eosd_target *target = &vc->eosd_targets[vc->eosd_render_count];
1238 if (need_upload) {
1239 vdp_st = vdp->
1240 bitmap_surface_put_bits_native(sfc->surface,
1241 (const void *) &p->bitmap,
1242 &p->stride, &target->source);
1243 CHECK_ST_WARNING("EOSD: putbits failed");
1245 // Render dest, color, etc.
1246 target->color.alpha = 1.0 - ((p->color >> 0) & 0xff) / 255.0;
1247 target->color.blue = ((p->color >> 8) & 0xff) / 255.0;
1248 target->color.green = ((p->color >> 16) & 0xff) / 255.0;
1249 target->color.red = ((p->color >> 24) & 0xff) / 255.0;
1250 target->dest.x0 = p->dst_x;
1251 target->dest.y0 = p->dst_y;
1252 target->dest.x1 = p->w + p->dst_x;
1253 target->dest.y1 = p->h + p->dst_y;
1254 vc->eosd_render_count++;
1258 static void draw_osd(struct vo *vo, struct osd_state *osd)
1260 struct vdpctx *vc = vo->priv;
1261 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
1263 if (handle_preemption(vo) < 0)
1264 return;
1266 osd_draw_text_ext(osd, vo->dwidth, vo->dheight, vc->border_x, vc->border_y,
1267 vc->border_x, vc->border_y, vc->vid_width,
1268 vc->vid_height, draw_osd_I8A8, vo);
1271 static int update_presentation_queue_status(struct vo *vo)
1273 struct vdpctx *vc = vo->priv;
1274 struct vdp_functions *vdp = vc->vdp;
1275 VdpStatus vdp_st;
1277 while (vc->query_surface_num != vc->surface_num) {
1278 VdpTime vtime;
1279 VdpPresentationQueueStatus status;
1280 VdpOutputSurface surface = vc->output_surfaces[vc->query_surface_num];
1281 vdp_st = vdp->presentation_queue_query_surface_status(vc->flip_queue,
1282 surface,
1283 &status, &vtime);
1284 CHECK_ST_WARNING("Error calling "
1285 "presentation_queue_query_surface_status");
1286 if (status == VDP_PRESENTATION_QUEUE_STATUS_QUEUED)
1287 break;
1288 if (vc->vsync_interval > 1) {
1289 uint64_t qtime = vc->queue_time[vc->query_surface_num];
1290 if (vtime < qtime + vc->vsync_interval / 2)
1291 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown too early\n");
1292 if (vtime > qtime + vc->vsync_interval)
1293 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown late\n");
1295 vc->query_surface_num = WRAP_ADD(vc->query_surface_num, 1,
1296 vc->num_output_surfaces);
1297 vc->recent_vsync_time = vtime;
1299 int num_queued = WRAP_ADD(vc->surface_num, -vc->query_surface_num,
1300 vc->num_output_surfaces);
1301 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Queued surface count (before add): "
1302 "%d\n", num_queued);
1303 return num_queued;
1306 static inline uint64_t prev_vs2(struct vdpctx *vc, uint64_t ts, int shift)
1308 uint64_t offset = ts - vc->recent_vsync_time;
1309 // Fix negative values for 1<<shift vsyncs before vc->recent_vsync_time
1310 offset += (uint64_t)vc->vsync_interval << shift;
1311 offset %= vc->vsync_interval;
1312 return ts - offset;
1315 static void flip_page_timed(struct vo *vo, unsigned int pts_us, int duration)
1317 struct vdpctx *vc = vo->priv;
1318 struct vdp_functions *vdp = vc->vdp;
1319 VdpStatus vdp_st;
1320 uint32_t vsync_interval = vc->vsync_interval;
1322 if (handle_preemption(vo) < 0)
1323 return;
1325 if (duration > INT_MAX / 1000)
1326 duration = -1;
1327 else
1328 duration *= 1000;
1330 if (vc->user_fps < 0)
1331 duration = -1; // Make sure drop logic is disabled
1333 uint64_t now = sync_vdptime(vo);
1334 uint64_t pts = pts_us ? convert_to_vdptime(vo, pts_us) : now;
1335 uint64_t ideal_pts = pts;
1336 uint64_t npts = duration >= 0 ? pts + duration : UINT64_MAX;
1338 #define PREV_VS2(ts, shift) prev_vs2(vc, ts, shift)
1339 // Only gives accurate results for ts >= vc->recent_vsync_time
1340 #define PREV_VSYNC(ts) PREV_VS2(ts, 0)
1342 /* We hope to be here at least one vsync before the frame should be shown.
1343 * If we are running late then don't drop the frame unless there is
1344 * already one queued for the next vsync; even if we _hope_ to show the
1345 * next frame soon enough to mean this one should be dropped we might
1346 * not make the target time in reality. Without this check we could drop
1347 * every frame, freezing the display completely if video lags behind.
1349 if (now > PREV_VSYNC(FFMAX(pts, vc->last_queue_time + vsync_interval)))
1350 npts = UINT64_MAX;
1352 /* Allow flipping a frame at a vsync if its presentation time is a
1353 * bit after that vsync and the change makes the flip time delta
1354 * from previous frame better match the target timestamp delta.
1355 * This avoids instability with frame timestamps falling near vsyncs.
1356 * For example if the frame timestamps were (with vsyncs at
1357 * integer values) 0.01, 1.99, 4.01, 5.99, 8.01, ... then
1358 * straightforward timing at next vsync would flip the frames at
1359 * 1, 2, 5, 6, 9; this changes it to 1, 2, 4, 6, 8 and so on with
1360 * regular 2-vsync intervals.
1362 * Also allow moving the frame forward if it looks like we dropped
1363 * the previous frame incorrectly (now that we know better after
1364 * having final exact timestamp information for this frame) and
1365 * there would unnecessarily be a vsync without a frame change.
1367 uint64_t vsync = PREV_VSYNC(pts);
1368 if (pts < vsync + vsync_interval / 4
1369 && (vsync - PREV_VS2(vc->last_queue_time, 16)
1370 > pts - vc->last_ideal_time + vsync_interval / 2
1371 || vc->dropped_frame && vsync > vc->dropped_time))
1372 pts -= vsync_interval / 2;
1374 vc->dropped_frame = true; // changed at end if false
1375 vc->dropped_time = ideal_pts;
1377 pts = FFMAX(pts, vc->last_queue_time + vsync_interval);
1378 pts = FFMAX(pts, now);
1379 if (npts < PREV_VSYNC(pts) + vsync_interval)
1380 return;
1382 int num_flips = update_presentation_queue_status(vo);
1383 vsync = vc->recent_vsync_time + num_flips * vc->vsync_interval;
1384 now = sync_vdptime(vo);
1385 pts = FFMAX(pts, now);
1386 pts = FFMAX(pts, vsync + (vsync_interval >> 2));
1387 vsync = PREV_VSYNC(pts);
1388 if (npts < vsync + vsync_interval)
1389 return;
1390 pts = vsync + (vsync_interval >> 2);
1391 vdp_st =
1392 vdp->presentation_queue_display(vc->flip_queue,
1393 vc->output_surfaces[vc->surface_num],
1394 vo->dwidth, vo->dheight, pts);
1395 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display");
1397 vc->last_queue_time = pts;
1398 vc->queue_time[vc->surface_num] = pts;
1399 vc->last_ideal_time = ideal_pts;
1400 vc->dropped_frame = false;
1401 vc->surface_num = WRAP_ADD(vc->surface_num, 1, vc->num_output_surfaces);
1402 vc->num_shown_frames = FFMIN(vc->num_shown_frames + 1, 1000);
1405 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
1406 int h, int x, int y)
1408 struct vdpctx *vc = vo->priv;
1409 struct vdp_functions *vdp = vc->vdp;
1410 VdpStatus vdp_st;
1412 if (handle_preemption(vo) < 0)
1413 return VO_TRUE;
1415 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
1416 int max_refs = vc->image_format == IMGFMT_VDPAU_H264 ?
1417 rndr->info.h264.num_ref_frames : 2;
1418 if (!IMGFMT_IS_VDPAU(vc->image_format))
1419 return VO_FALSE;
1420 if ((vc->decoder == VDP_INVALID_HANDLE || vc->decoder_max_refs < max_refs)
1421 && !create_vdp_decoder(vo, max_refs))
1422 return VO_FALSE;
1424 vdp_st = vdp->decoder_render(vc->decoder, rndr->surface,
1425 (void *)&rndr->info,
1426 rndr->bitstream_buffers_used,
1427 rndr->bitstream_buffers);
1428 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
1429 return VO_TRUE;
1433 static struct vdpau_render_state *get_surface(struct vo *vo, int number)
1435 struct vdpctx *vc = vo->priv;
1436 struct vdp_functions *vdp = vc->vdp;
1438 if (number > MAX_VIDEO_SURFACES)
1439 return NULL;
1440 if (vc->surface_render[number].surface == VDP_INVALID_HANDLE
1441 && !vc->is_preempted) {
1442 VdpStatus vdp_st;
1443 vdp_st = vdp->video_surface_create(vc->vdp_device, vc->vdp_chroma_type,
1444 vc->vid_width, vc->vid_height,
1445 &vc->surface_render[number].surface);
1446 CHECK_ST_WARNING("Error when calling vdp_video_surface_create");
1448 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n",
1449 vc->surface_render[number].surface);
1450 return &vc->surface_render[number];
1453 static void draw_image(struct vo *vo, mp_image_t *mpi, double pts)
1455 struct vdpctx *vc = vo->priv;
1456 struct vdp_functions *vdp = vc->vdp;
1457 struct mp_image *reserved_mpi = NULL;
1458 struct vdpau_render_state *rndr;
1460 if (vc->is_preempted) {
1461 vo->frame_loaded = true;
1462 return;
1465 if (IMGFMT_IS_VDPAU(vc->image_format)) {
1466 rndr = mpi->priv;
1467 reserved_mpi = mpi;
1468 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1469 VdpStatus vdp_st;
1470 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
1471 rndr = get_surface(vo, vc->deint_counter);
1472 vc->deint_counter = WRAP_ADD(vc->deint_counter, 1, NUM_BUFFERED_VIDEO);
1473 if (vc->image_format == IMGFMT_NV12)
1474 destdata[1] = destdata[2];
1475 vdp_st =
1476 vdp->video_surface_put_bits_y_cb_cr(rndr->surface,
1477 vc->vdp_pixel_format,
1478 (const void *const*)destdata,
1479 mpi->stride); // pitch
1480 CHECK_ST_WARNING("Error when calling "
1481 "vdp_video_surface_put_bits_y_cb_cr");
1482 } else
1483 // We don't support slice callbacks so this shouldn't occur -
1484 // I think the flags test above in pointless, but I'm adding
1485 // this instead of removing it just in case.
1486 abort();
1487 if (mpi->fields & MP_IMGFIELD_ORDERED)
1488 vc->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1489 else
1490 vc->top_field_first = 1;
1492 add_new_video_surface(vo, rndr->surface, mpi, pts);
1494 return;
1497 static uint32_t get_image(struct vo *vo, mp_image_t *mpi)
1499 struct vdpctx *vc = vo->priv;
1500 struct vdpau_render_state *rndr;
1502 // no dr for non-decoding for now
1503 if (!IMGFMT_IS_VDPAU(vc->image_format))
1504 return VO_FALSE;
1505 if (mpi->type != MP_IMGTYPE_NUMBERED)
1506 return VO_FALSE;
1508 rndr = get_surface(vo, mpi->number);
1509 if (!rndr) {
1510 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in "
1511 "get_image\n");
1512 // TODO: this probably breaks things forever, provide a dummy buffer?
1513 return VO_FALSE;
1515 mpi->flags |= MP_IMGFLAG_DIRECT;
1516 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1517 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1518 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1519 mpi->planes[0] = (void *)rndr;
1520 mpi->num_planes = 1;
1521 mpi->priv = rndr;
1522 return VO_TRUE;
1525 static int query_format(uint32_t format)
1527 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
1528 | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD
1529 | VFCAP_EOSD_UNSCALED | VFCAP_FLIP;
1530 switch (format) {
1531 case IMGFMT_YV12:
1532 case IMGFMT_I420:
1533 case IMGFMT_IYUV:
1534 case IMGFMT_NV12:
1535 case IMGFMT_YUY2:
1536 case IMGFMT_UYVY:
1537 return default_flags | VOCAP_NOSLICES;
1538 case IMGFMT_VDPAU_MPEG1:
1539 case IMGFMT_VDPAU_MPEG2:
1540 case IMGFMT_VDPAU_H264:
1541 case IMGFMT_VDPAU_WMV3:
1542 case IMGFMT_VDPAU_VC1:
1543 case IMGFMT_VDPAU_MPEG4:
1544 return default_flags;
1546 return 0;
1549 static void destroy_vdpau_objects(struct vo *vo)
1551 struct vdpctx *vc = vo->priv;
1552 struct vdp_functions *vdp = vc->vdp;
1554 int i;
1555 VdpStatus vdp_st;
1557 free_video_specific(vo);
1559 if (vc->flip_queue != VDP_INVALID_HANDLE) {
1560 vdp_st = vdp->presentation_queue_destroy(vc->flip_queue);
1561 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy");
1564 if (vc->flip_target != VDP_INVALID_HANDLE) {
1565 vdp_st = vdp->presentation_queue_target_destroy(vc->flip_target);
1566 CHECK_ST_WARNING("Error when calling "
1567 "vdp_presentation_queue_target_destroy");
1570 for (i = 0; i <= vc->num_output_surfaces; i++) {
1571 if (vc->output_surfaces[i] == VDP_INVALID_HANDLE)
1572 continue;
1573 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
1574 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy");
1577 if (vc->eosd_surface.surface != VDP_INVALID_HANDLE) {
1578 vdp_st = vdp->bitmap_surface_destroy(vc->eosd_surface.surface);
1579 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1582 vdp_st = vdp->device_destroy(vc->vdp_device);
1583 CHECK_ST_WARNING("Error when calling vdp_device_destroy");
1586 static void uninit(struct vo *vo)
1588 struct vdpctx *vc = vo->priv;
1590 /* Destroy all vdpau objects */
1591 destroy_vdpau_objects(vo);
1593 #ifdef CONFIG_XF86VM
1594 if (vc->mode_switched)
1595 vo_vm_close(vo);
1596 #endif
1597 vo_x11_uninit(vo);
1599 // Free bitstream buffers allocated by FFmpeg
1600 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
1601 av_freep(&vc->surface_render[i].bitstream_buffers);
1604 static int preinit(struct vo *vo, const char *arg)
1606 int i;
1608 struct vdpctx *vc = talloc_zero(vo, struct vdpctx);
1609 vo->priv = vc;
1611 // Mark everything as invalid first so uninit() can tell what has been
1612 // allocated
1613 mark_vdpau_objects_uninitialized(vo);
1615 vc->deint_type = 3;
1616 vc->chroma_deint = 1;
1617 vc->user_colorspace = 1;
1618 vc->flip_offset_window = 50;
1619 vc->flip_offset_fs = 50;
1620 vc->num_output_surfaces = 3;
1621 const opt_t subopts[] = {
1622 {"deint", OPT_ARG_INT, &vc->deint, (opt_test_f)int_non_neg},
1623 {"chroma-deint", OPT_ARG_BOOL, &vc->chroma_deint, NULL},
1624 {"pullup", OPT_ARG_BOOL, &vc->pullup, NULL},
1625 {"denoise", OPT_ARG_FLOAT, &vc->denoise, NULL},
1626 {"sharpen", OPT_ARG_FLOAT, &vc->sharpen, NULL},
1627 {"colorspace", OPT_ARG_INT, &vc->user_colorspace, NULL},
1628 {"studio", OPT_ARG_BOOL, &vc->studio_levels, NULL},
1629 {"hqscaling", OPT_ARG_INT, &vc->hqscaling, NULL},
1630 {"fps", OPT_ARG_FLOAT, &vc->user_fps, NULL},
1631 {"queuetime_windowed", OPT_ARG_INT, &vc->flip_offset_window, NULL},
1632 {"queuetime_fs", OPT_ARG_INT, &vc->flip_offset_fs, NULL},
1633 {"output_surfaces", OPT_ARG_INT, &vc->num_output_surfaces, NULL},
1634 {NULL}
1636 if (subopt_parse(arg, subopts) != 0) {
1637 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Could not parse suboptions.\n");
1638 return -1;
1640 if (vc->hqscaling < 0 || vc->hqscaling > 9) {
1641 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid value for suboption "
1642 "hqscaling\n");
1643 return -1;
1645 if (vc->num_output_surfaces < 2) {
1646 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid suboption "
1647 "output_surfaces: can't use less than 2 surfaces\n");
1648 return -1;
1650 if (vc->num_output_surfaces > MAX_OUTPUT_SURFACES) {
1651 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Number of output surfaces "
1652 "is limited to %d.\n", MAX_OUTPUT_SURFACES);
1653 vc->num_output_surfaces = MAX_OUTPUT_SURFACES;
1655 if (vc->deint)
1656 vc->deint_type = vc->deint;
1658 if (!vo_init(vo))
1659 return -1;
1661 // After this calling uninit() should work to free resources
1663 if (win_x11_init_vdpau_procs(vo) < 0) {
1664 if (vc->vdp->device_destroy)
1665 vc->vdp->device_destroy(vc->vdp_device);
1666 vo_x11_uninit(vo);
1667 return -1;
1670 // full grayscale palette.
1671 for (i = 0; i < PALETTE_SIZE; ++i)
1672 vc->palette[i] = (i << 16) | (i << 8) | i;
1674 vc->procamp.struct_version = VDP_PROCAMP_VERSION;
1675 vc->procamp.brightness = 0.0;
1676 vc->procamp.contrast = 1.0;
1677 vc->procamp.saturation = 1.0;
1678 vc->procamp.hue = 0.0;
1680 return 0;
1683 static int get_equalizer(struct vo *vo, const char *name, int *value)
1685 struct vdpctx *vc = vo->priv;
1687 if (!strcasecmp(name, "brightness"))
1688 *value = vc->procamp.brightness * 100;
1689 else if (!strcasecmp(name, "contrast"))
1690 *value = (vc->procamp.contrast - 1.0) * 100;
1691 else if (!strcasecmp(name, "saturation"))
1692 *value = (vc->procamp.saturation - 1.0) * 100;
1693 else if (!strcasecmp(name, "hue"))
1694 *value = vc->procamp.hue * 100 / M_PI;
1695 else
1696 return VO_NOTIMPL;
1697 return VO_TRUE;
1700 static int set_equalizer(struct vo *vo, const char *name, int value)
1702 struct vdpctx *vc = vo->priv;
1704 if (!strcasecmp(name, "brightness"))
1705 vc->procamp.brightness = value / 100.0;
1706 else if (!strcasecmp(name, "contrast"))
1707 vc->procamp.contrast = value / 100.0 + 1.0;
1708 else if (!strcasecmp(name, "saturation"))
1709 vc->procamp.saturation = value / 100.0 + 1.0;
1710 else if (!strcasecmp(name, "hue"))
1711 vc->procamp.hue = value / 100.0 * M_PI;
1712 else
1713 return VO_NOTIMPL;
1715 update_csc_matrix(vo);
1716 return true;
1719 static int control(struct vo *vo, uint32_t request, void *data)
1721 struct vdpctx *vc = vo->priv;
1722 struct vdp_functions *vdp = vc->vdp;
1724 handle_preemption(vo);
1726 switch (request) {
1727 case VOCTRL_GET_DEINTERLACE:
1728 *(int*)data = vc->deint;
1729 return VO_TRUE;
1730 case VOCTRL_SET_DEINTERLACE:
1731 vc->deint = *(int*)data;
1732 if (vc->deint)
1733 vc->deint = vc->deint_type;
1734 if (vc->deint_type > 2) {
1735 VdpStatus vdp_st;
1736 VdpVideoMixerFeature features[1] =
1737 {vc->deint_type == 3 ?
1738 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1739 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1740 VdpBool feature_enables[1] = {vc->deint ? VDP_TRUE : VDP_FALSE};
1741 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
1742 1, features,
1743 feature_enables);
1744 CHECK_ST_WARNING("Error changing deinterlacing settings");
1746 return VO_TRUE;
1747 case VOCTRL_PAUSE:
1748 if (vc->dropped_frame)
1749 flip_page_timed(vo, 0, -1);
1750 return (vc->paused = true);
1751 case VOCTRL_RESUME:
1752 return (vc->paused = false);
1753 case VOCTRL_QUERY_FORMAT:
1754 return query_format(*(uint32_t *)data);
1755 case VOCTRL_GET_IMAGE:
1756 return get_image(vo, data);
1757 case VOCTRL_DRAW_IMAGE:
1758 abort(); // draw_image() should get called directly
1759 case VOCTRL_BORDER:
1760 vo_x11_border(vo);
1761 resize(vo);
1762 return VO_TRUE;
1763 case VOCTRL_FULLSCREEN:
1764 vo_x11_fullscreen(vo);
1765 resize(vo);
1766 return VO_TRUE;
1767 case VOCTRL_GET_PANSCAN:
1768 return VO_TRUE;
1769 case VOCTRL_SET_PANSCAN:
1770 resize(vo);
1771 return VO_TRUE;
1772 case VOCTRL_SET_EQUALIZER: {
1773 struct voctrl_set_equalizer_args *args = data;
1774 return set_equalizer(vo, args->name, args->value);
1776 case VOCTRL_GET_EQUALIZER: {
1777 struct voctrl_get_equalizer_args *args = data;
1778 return get_equalizer(vo, args->name, args->valueptr);
1780 case VOCTRL_SET_YUV_COLORSPACE:
1781 vc->colorspace = *(int *)data % 3;
1782 update_csc_matrix(vo);
1783 return true;
1784 case VOCTRL_GET_YUV_COLORSPACE:
1785 *(int *)data = vc->colorspace;
1786 return true;
1787 case VOCTRL_ONTOP:
1788 vo_x11_ontop(vo);
1789 return VO_TRUE;
1790 case VOCTRL_UPDATE_SCREENINFO:
1791 update_xinerama_info(vo);
1792 return VO_TRUE;
1793 case VOCTRL_DRAW_EOSD:
1794 if (!data)
1795 return VO_FALSE;
1796 generate_eosd(vo, data);
1797 draw_eosd(vo);
1798 return VO_TRUE;
1799 case VOCTRL_GET_EOSD_RES: {
1800 mp_eosd_res_t *r = data;
1801 r->w = vo->dwidth;
1802 r->h = vo->dheight;
1803 r->ml = r->mr = vc->border_x;
1804 r->mt = r->mb = vc->border_y;
1805 return VO_TRUE;
1807 case VOCTRL_REDRAW_OSD:
1808 video_to_output_surface(vo);
1809 draw_eosd(vo);
1810 draw_osd(vo, data);
1811 flip_page_timed(vo, 0, -1);
1812 return true;
1813 case VOCTRL_RESET:
1814 forget_frames(vo);
1815 return true;
1817 return VO_NOTIMPL;
1820 const struct vo_driver video_out_vdpau = {
1821 .is_new = true,
1822 .buffer_frames = true,
1823 .info = &(const struct vo_info_s){
1824 "VDPAU with X11",
1825 "vdpau",
1826 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
1829 .preinit = preinit,
1830 .config = config,
1831 .control = control,
1832 .draw_image = draw_image,
1833 .get_buffered_frame = get_buffered_frame,
1834 .draw_slice = draw_slice,
1835 .draw_osd = draw_osd,
1836 .flip_page_timed = flip_page_timed,
1837 .check_events = check_events,
1838 .uninit = uninit,