vo_glamo: sub.h was moved to sub directory in c9026cb3210205b07e2e068467a18ee40f9259a3
[mplayer/glamo.git] / libvo / vo_vdpau.c
blob210fe797dc0680b9a2b5d418a3cd546e42b9b676
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/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 "sub/font_load.h"
54 #include "libavutil/common.h"
55 #include "libavutil/mathematics.h"
57 #include "sub/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_type == 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 (vc->deint_type == 4 && vc->deint < 4)
673 feature_enables[1] = VDP_FALSE;
674 if (feature_count) {
675 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
676 feature_count, features,
677 feature_enables);
678 CHECK_ST_WARNING("Error calling vdp_video_mixer_set_feature_enables");
680 if (vc->denoise)
681 SET_VIDEO_ATTR(NOISE_REDUCTION_LEVEL, float, vc->denoise);
682 if (vc->sharpen)
683 SET_VIDEO_ATTR(SHARPNESS_LEVEL, float, vc->sharpen);
684 if (!vc->chroma_deint)
685 SET_VIDEO_ATTR(SKIP_CHROMA_DEINTERLACE, uint8_t, 1);
687 update_csc_matrix(vo);
688 return 0;
691 // Free everything specific to a certain video file
692 static void free_video_specific(struct vo *vo)
694 struct vdpctx *vc = vo->priv;
695 struct vdp_functions *vdp = vc->vdp;
696 int i;
697 VdpStatus vdp_st;
699 if (vc->decoder != VDP_INVALID_HANDLE)
700 vdp->decoder_destroy(vc->decoder);
701 vc->decoder = VDP_INVALID_HANDLE;
702 vc->decoder_max_refs = -1;
704 forget_frames(vo);
706 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
707 if (vc->surface_render[i].surface != VDP_INVALID_HANDLE) {
708 vdp_st = vdp->video_surface_destroy(vc->surface_render[i].surface);
709 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy");
711 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
714 if (vc->video_mixer != VDP_INVALID_HANDLE) {
715 vdp_st = vdp->video_mixer_destroy(vc->video_mixer);
716 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy");
718 vc->video_mixer = VDP_INVALID_HANDLE;
721 static int create_vdp_decoder(struct vo *vo, int max_refs)
723 struct vdpctx *vc = vo->priv;
724 struct vdp_functions *vdp = vc->vdp;
725 VdpStatus vdp_st;
726 VdpDecoderProfile vdp_decoder_profile;
727 if (vc->decoder != VDP_INVALID_HANDLE)
728 vdp->decoder_destroy(vc->decoder);
729 switch (vc->image_format) {
730 case IMGFMT_VDPAU_MPEG1:
731 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
732 break;
733 case IMGFMT_VDPAU_MPEG2:
734 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
735 break;
736 case IMGFMT_VDPAU_H264:
737 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
738 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder "
739 "for %d reference frames.\n", max_refs);
740 break;
741 case IMGFMT_VDPAU_WMV3:
742 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
743 break;
744 case IMGFMT_VDPAU_VC1:
745 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
746 break;
747 case IMGFMT_VDPAU_MPEG4:
748 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
749 break;
750 default:
751 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Unknown image format!\n");
752 goto fail;
754 vdp_st = vdp->decoder_create(vc->vdp_device, vdp_decoder_profile,
755 vc->vid_width, vc->vid_height, max_refs,
756 &vc->decoder);
757 CHECK_ST_WARNING("Failed creating VDPAU decoder");
758 if (vdp_st != VDP_STATUS_OK) {
759 fail:
760 vc->decoder = VDP_INVALID_HANDLE;
761 vc->decoder_max_refs = 0;
762 return 0;
764 vc->decoder_max_refs = max_refs;
765 return 1;
768 static int initialize_vdpau_objects(struct vo *vo)
770 struct vdpctx *vc = vo->priv;
771 struct vdp_functions *vdp = vc->vdp;
772 VdpStatus vdp_st;
774 vc->vdp_chroma_type = VDP_CHROMA_TYPE_420;
775 switch (vc->image_format) {
776 case IMGFMT_YV12:
777 case IMGFMT_I420:
778 case IMGFMT_IYUV:
779 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YV12;
780 break;
781 case IMGFMT_NV12:
782 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_NV12;
783 break;
784 case IMGFMT_YUY2:
785 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV;
786 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
787 break;
788 case IMGFMT_UYVY:
789 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY;
790 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
792 if (win_x11_init_vdpau_flip_queue(vo) < 0)
793 return -1;
795 if (create_vdp_mixer(vo, vc->vdp_chroma_type) < 0)
796 return -1;
798 vdp_st = vdp->
799 bitmap_surface_query_capabilities(vc->vdp_device,
800 VDP_RGBA_FORMAT_A8,
801 &(VdpBool){0},
802 &vc->eosd_surface.max_width,
803 &vc->eosd_surface.max_height);
804 CHECK_ST_WARNING("Query to get max EOSD surface size failed");
805 forget_frames(vo);
806 resize(vo);
807 return 0;
810 static void mark_vdpau_objects_uninitialized(struct vo *vo)
812 struct vdpctx *vc = vo->priv;
814 vc->decoder = VDP_INVALID_HANDLE;
815 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
816 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
817 forget_frames(vo);
818 vc->video_mixer = VDP_INVALID_HANDLE;
819 vc->flip_queue = VDP_INVALID_HANDLE;
820 vc->flip_target = VDP_INVALID_HANDLE;
821 for (int i = 0; i <= MAX_OUTPUT_SURFACES; i++)
822 vc->output_surfaces[i] = VDP_INVALID_HANDLE;
823 vc->vdp_device = VDP_INVALID_HANDLE;
824 vc->eosd_surface = (struct eosd_bitmap_surface){
825 .surface = VDP_INVALID_HANDLE,
827 vc->output_surface_width = vc->output_surface_height = -1;
828 vc->eosd_render_count = 0;
829 vc->num_shown_frames = 0;
832 static int handle_preemption(struct vo *vo)
834 struct vdpctx *vc = vo->priv;
836 if (!vc->is_preempted)
837 return 0;
838 if (!vc->preemption_acked)
839 mark_vdpau_objects_uninitialized(vo);
840 vc->preemption_acked = true;
841 if (!vc->preemption_user_notified) {
842 mp_tmsg(MSGT_VO, MSGL_ERR, "[vdpau] Got display preemption notice! "
843 "Will attempt to recover.\n");
844 vc->preemption_user_notified = true;
846 /* Trying to initialize seems to be quite slow, so only try once a
847 * second to avoid using 100% CPU. */
848 if (vc->last_preemption_retry_fail
849 && GetTimerMS() - vc->last_preemption_retry_fail < 1000)
850 return -1;
851 if (win_x11_init_vdpau_procs(vo) < 0 || initialize_vdpau_objects(vo) < 0) {
852 vc->last_preemption_retry_fail = GetTimerMS() | 1;
853 return -1;
855 vc->last_preemption_retry_fail = 0;
856 vc->is_preempted = false;
857 vc->preemption_user_notified = false;
858 mp_tmsg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n");
859 return 1;
863 * connect to X server, create and map window, initialize all
864 * VDPAU objects, create different surfaces etc.
866 static int config(struct vo *vo, uint32_t width, uint32_t height,
867 uint32_t d_width, uint32_t d_height, uint32_t flags,
868 char *title, uint32_t format)
870 struct vdpctx *vc = vo->priv;
871 struct vo_x11_state *x11 = vo->x11;
872 XVisualInfo vinfo;
873 XSetWindowAttributes xswa;
874 XWindowAttributes attribs;
875 unsigned long xswamask;
876 int depth;
878 #ifdef CONFIG_XF86VM
879 int vm = flags & VOFLAG_MODESWITCHING;
880 #endif
882 if (handle_preemption(vo) < 0)
883 return -1;
885 vc->flip = flags & VOFLAG_FLIPPING;
886 vc->image_format = format;
887 vc->vid_width = width;
888 vc->vid_height = height;
889 if (vc->user_colorspace == 0)
890 vc->colorspace = width >= 1280 || height > 576 ? 1 : 0;
891 else
892 vc->colorspace = vc->user_colorspace - 1;
893 free_video_specific(vo);
894 if (IMGFMT_IS_VDPAU(vc->image_format) && !create_vdp_decoder(vo, 2))
895 return -1;
897 #ifdef CONFIG_XF86VM
898 if (vm) {
899 vo_vm_switch(vo);
900 vc->mode_switched = true;
902 #endif
903 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
904 &attribs);
905 depth = attribs.depth;
906 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
907 depth = 24;
908 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
910 xswa.background_pixel = 0;
911 xswa.border_pixel = 0;
912 /* Do not use CWBackPixel: It leads to VDPAU errors after
913 * aspect ratio changes. */
914 xswamask = CWBorderPixel;
916 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, d_width, d_height,
917 flags, CopyFromParent, "vdpau", title);
918 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
920 #ifdef CONFIG_XF86VM
921 if (vm) {
922 /* Grab the mouse pointer in our window */
923 if (vo_grabpointer)
924 XGrabPointer(x11->display, x11->window, True, 0,
925 GrabModeAsync, GrabModeAsync,
926 x11->window, None, CurrentTime);
927 XSetInputFocus(x11->display, x11->window, RevertToNone, CurrentTime);
929 #endif
931 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
932 vo_fs = 1;
934 if (initialize_vdpau_objects(vo) < 0)
935 return -1;
937 return 0;
940 static void check_events(struct vo *vo)
942 struct vdpctx *vc = vo->priv;
943 struct vdp_functions *vdp = vc->vdp;
945 if (handle_preemption(vo) < 0)
946 return;
948 int e = vo_x11_check_events(vo);
950 if (e & VO_EVENT_RESIZE)
951 resize(vo);
952 else if (e & VO_EVENT_EXPOSE && vc->paused) {
953 /* did we already draw a buffer */
954 if (vc->num_shown_frames) {
955 /* redraw the last visible buffer */
956 VdpStatus vdp_st;
957 int last_surface = WRAP_ADD(vc->surface_num, -1,
958 vc->num_output_surfaces);
959 vdp_st = vdp->presentation_queue_display(vc->flip_queue,
960 vc->output_surfaces[last_surface],
961 vo->dwidth, vo->dheight, 0);
962 CHECK_ST_WARNING("Error when calling "
963 "vdp_presentation_queue_display");
968 static void draw_osd_I8A8(void *ctx, int x0, int y0, int w, int h,
969 unsigned char *src, unsigned char *srca, int stride)
971 struct vo *vo = ctx;
972 struct vdpctx *vc = vo->priv;
973 struct vdp_functions *vdp = vc->vdp;
974 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
975 VdpStatus vdp_st;
976 int i;
977 int pitch;
978 int index_data_size_required;
979 VdpRect output_indexed_rect_vid;
981 if (!w || !h)
982 return;
984 index_data_size_required = 2*w*h;
985 if (vc->index_data_size < index_data_size_required) {
986 vc->index_data = talloc_realloc_size(vc, vc->index_data,
987 index_data_size_required);
988 vc->index_data_size = index_data_size_required;
991 // index_data creation, component order - I, A, I, A, .....
992 for (i = 0; i < h; i++)
993 for (int j = 0; j < w; j++) {
994 vc->index_data[i*2*w + j*2] = src [i*stride+j];
995 vc->index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
998 output_indexed_rect_vid.x0 = x0;
999 output_indexed_rect_vid.y0 = y0;
1000 output_indexed_rect_vid.x1 = x0 + w;
1001 output_indexed_rect_vid.y1 = y0 + h;
1003 pitch = w*2;
1005 // write source_data to osd_surface.
1006 VdpOutputSurface osd_surface = vc->output_surfaces[vc->num_output_surfaces];
1007 vdp_st = vdp->
1008 output_surface_put_bits_indexed(osd_surface, VDP_INDEXED_FORMAT_I8A8,
1009 (const void *const*)&vc->index_data,
1010 &pitch, &output_indexed_rect_vid,
1011 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
1012 (void *)vc->palette);
1013 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed");
1015 VdpOutputSurfaceRenderBlendState blend_state = {
1016 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1017 .blend_factor_source_color =
1018 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1019 .blend_factor_source_alpha =
1020 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1021 .blend_factor_destination_color =
1022 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1023 .blend_factor_destination_alpha =
1024 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1025 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1026 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1029 vdp_st = vdp->
1030 output_surface_render_output_surface(output_surface,
1031 &output_indexed_rect_vid,
1032 osd_surface,
1033 &output_indexed_rect_vid,
1034 NULL, &blend_state,
1035 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1036 CHECK_ST_WARNING("Error when calling "
1037 "vdp_output_surface_render_output_surface");
1040 static void draw_eosd(struct vo *vo)
1042 struct vdpctx *vc = vo->priv;
1043 struct vdp_functions *vdp = vc->vdp;
1044 VdpStatus vdp_st;
1045 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
1046 int i;
1048 if (handle_preemption(vo) < 0)
1049 return;
1051 VdpOutputSurfaceRenderBlendState blend_state = {
1052 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1053 .blend_factor_source_color =
1054 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1055 .blend_factor_source_alpha =
1056 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1057 .blend_factor_destination_color =
1058 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1059 .blend_factor_destination_alpha =
1060 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1061 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1062 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1065 for (i = 0; i < vc->eosd_render_count; i++) {
1066 vdp_st = vdp->
1067 output_surface_render_bitmap_surface(output_surface,
1068 &vc->eosd_targets[i].dest,
1069 vc->eosd_surface.surface,
1070 &vc->eosd_targets[i].source,
1071 &vc->eosd_targets[i].color,
1072 &blend_state,
1073 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1074 CHECK_ST_WARNING("EOSD: Error when rendering");
1078 #define HEIGHT_SORT_BITS 4
1079 static int size_index(struct eosd_target *r)
1081 unsigned int h = r->source.y1;
1082 int n = av_log2_16bit(h);
1083 return (n << HEIGHT_SORT_BITS)
1084 + (- 1 - (h << HEIGHT_SORT_BITS >> n) & (1 << HEIGHT_SORT_BITS) - 1);
1087 /* Pack the given rectangles into an area of size w * h.
1088 * The size of each rectangle is read from .source.x1/.source.y1.
1089 * The height of each rectangle must be at least 1 and less than 65536.
1090 * The .source rectangle is then set corresponding to the packed position.
1091 * 'scratch' must point to work memory for num_rects+16 ints.
1092 * Return 0 on success, -1 if the rectangles did not fit in w*h.
1094 * The rectangles are placed in rows in order approximately sorted by
1095 * height (the approximate sorting is simpler than a full one would be,
1096 * and allows the algorithm to work in linear time). Additionally, to
1097 * reduce wasted space when there are a few tall rectangles, empty
1098 * lower-right parts of rows are filled recursively when the size of
1099 * rectangles in the row drops past a power-of-two threshold. So if a
1100 * row starts with rectangles of size 3x50, 10x40 and 5x20 then the
1101 * free rectangle with corners (13, 20)-(w, 50) is filled recursively.
1103 static int pack_rectangles(struct eosd_target *rects, int num_rects,
1104 int w, int h, int *scratch)
1106 int bins[16 << HEIGHT_SORT_BITS];
1107 int sizes[16 << HEIGHT_SORT_BITS] = {};
1108 for (int i = 0; i < num_rects; i++)
1109 sizes[size_index(rects + i)]++;
1110 int idx = 0;
1111 for (int i = 0; i < 16 << HEIGHT_SORT_BITS; i += 1 << HEIGHT_SORT_BITS) {
1112 for (int j = 0; j < 1 << HEIGHT_SORT_BITS; j++) {
1113 bins[i + j] = idx;
1114 idx += sizes[i + j];
1116 scratch[idx++] = -1;
1118 for (int i = 0; i < num_rects; i++)
1119 scratch[bins[size_index(rects + i)]++] = i;
1120 for (int i = 0; i < 16; i++)
1121 bins[i] = bins[i << HEIGHT_SORT_BITS] - sizes[i << HEIGHT_SORT_BITS];
1122 struct {
1123 int size, x, bottom;
1124 } stack[16] = {{15, 0, h}}, s = {};
1125 int stackpos = 1;
1126 int y;
1127 while (stackpos) {
1128 y = s.bottom;
1129 s = stack[--stackpos];
1130 s.size++;
1131 while (s.size--) {
1132 int maxy = -1;
1133 int obj;
1134 while ((obj = scratch[bins[s.size]]) >= 0) {
1135 int bottom = y + rects[obj].source.y1;
1136 if (bottom > s.bottom)
1137 break;
1138 int right = s.x + rects[obj].source.x1;
1139 if (right > w)
1140 break;
1141 bins[s.size]++;
1142 rects[obj].source.x0 = s.x;
1143 rects[obj].source.x1 += s.x;
1144 rects[obj].source.y0 = y;
1145 rects[obj].source.y1 += y;
1146 num_rects--;
1147 if (maxy <= 0)
1148 stack[stackpos++] = s;
1149 s.x = right;
1150 maxy = FFMAX(maxy, bottom);
1152 if (maxy > 0)
1153 s.bottom = maxy;
1156 return num_rects ? -1 : 0;
1159 static void generate_eosd(struct vo *vo, mp_eosd_images_t *imgs)
1161 struct vdpctx *vc = vo->priv;
1162 struct vdp_functions *vdp = vc->vdp;
1163 VdpStatus vdp_st;
1164 int i;
1165 ASS_Image *img = imgs->imgs;
1166 ASS_Image *p;
1167 struct eosd_bitmap_surface *sfc = &vc->eosd_surface;
1168 bool need_upload = false;
1170 if (imgs->changed == 0)
1171 return; // Nothing changed, no need to redraw
1173 vc->eosd_render_count = 0;
1175 if (!img)
1176 return; // There's nothing to render!
1178 if (imgs->changed == 1)
1179 goto eosd_skip_upload;
1181 need_upload = true;
1182 bool reallocate = false;
1183 while (1) {
1184 for (p = img, i = 0; p; p = p->next) {
1185 if (p->w <= 0 || p->h <= 0)
1186 continue;
1187 // Allocate new space for surface/target arrays
1188 if (i >= vc->eosd_targets_size) {
1189 vc->eosd_targets_size = FFMAX(vc->eosd_targets_size * 2, 512);
1190 vc->eosd_targets =
1191 talloc_realloc_size(vc, vc->eosd_targets,
1192 vc->eosd_targets_size
1193 * sizeof(*vc->eosd_targets));
1194 vc->eosd_scratch =
1195 talloc_realloc_size(vc, vc->eosd_scratch,
1196 (vc->eosd_targets_size + 16)
1197 * sizeof(*vc->eosd_scratch));
1199 vc->eosd_targets[i].source.x1 = p->w;
1200 vc->eosd_targets[i].source.y1 = p->h;
1201 i++;
1203 if (pack_rectangles(vc->eosd_targets, i, sfc->w, sfc->h,
1204 vc->eosd_scratch) >= 0)
1205 break;
1206 int w = FFMIN(FFMAX(sfc->w * 2, EOSD_SURFACE_INITIAL_SIZE),
1207 sfc->max_width);
1208 int h = FFMIN(FFMAX(sfc->h * 2, EOSD_SURFACE_INITIAL_SIZE),
1209 sfc->max_height);
1210 if (w == sfc->w && h == sfc->h) {
1211 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] EOSD bitmaps do not fit on "
1212 "a surface with the maximum supported size\n");
1213 return;
1214 } else {
1215 sfc->w = w;
1216 sfc->h = h;
1218 reallocate = true;
1220 if (reallocate) {
1221 if (sfc->surface != VDP_INVALID_HANDLE) {
1222 vdp_st = vdp->bitmap_surface_destroy(sfc->surface);
1223 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1225 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Allocating a %dx%d surface for "
1226 "EOSD bitmaps.\n", sfc->w, sfc->h);
1227 vdp_st = vdp->bitmap_surface_create(vc->vdp_device, VDP_RGBA_FORMAT_A8,
1228 sfc->w, sfc->h, true,
1229 &sfc->surface);
1230 if (vdp_st != VDP_STATUS_OK)
1231 sfc->surface = VDP_INVALID_HANDLE;
1232 CHECK_ST_WARNING("EOSD: error when creating surface");
1235 eosd_skip_upload:
1236 if (sfc->surface == VDP_INVALID_HANDLE)
1237 return;
1238 for (p = img; p; p = p->next) {
1239 if (p->w <= 0 || p->h <= 0)
1240 continue;
1241 struct eosd_target *target = &vc->eosd_targets[vc->eosd_render_count];
1242 if (need_upload) {
1243 vdp_st = vdp->
1244 bitmap_surface_put_bits_native(sfc->surface,
1245 (const void *) &p->bitmap,
1246 &p->stride, &target->source);
1247 CHECK_ST_WARNING("EOSD: putbits failed");
1249 // Render dest, color, etc.
1250 target->color.alpha = 1.0 - ((p->color >> 0) & 0xff) / 255.0;
1251 target->color.blue = ((p->color >> 8) & 0xff) / 255.0;
1252 target->color.green = ((p->color >> 16) & 0xff) / 255.0;
1253 target->color.red = ((p->color >> 24) & 0xff) / 255.0;
1254 target->dest.x0 = p->dst_x;
1255 target->dest.y0 = p->dst_y;
1256 target->dest.x1 = p->w + p->dst_x;
1257 target->dest.y1 = p->h + p->dst_y;
1258 vc->eosd_render_count++;
1262 static void draw_osd(struct vo *vo, struct osd_state *osd)
1264 struct vdpctx *vc = vo->priv;
1265 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
1267 if (handle_preemption(vo) < 0)
1268 return;
1270 osd_draw_text_ext(osd, vo->dwidth, vo->dheight, vc->border_x, vc->border_y,
1271 vc->border_x, vc->border_y, vc->vid_width,
1272 vc->vid_height, draw_osd_I8A8, vo);
1275 static int update_presentation_queue_status(struct vo *vo)
1277 struct vdpctx *vc = vo->priv;
1278 struct vdp_functions *vdp = vc->vdp;
1279 VdpStatus vdp_st;
1281 while (vc->query_surface_num != vc->surface_num) {
1282 VdpTime vtime;
1283 VdpPresentationQueueStatus status;
1284 VdpOutputSurface surface = vc->output_surfaces[vc->query_surface_num];
1285 vdp_st = vdp->presentation_queue_query_surface_status(vc->flip_queue,
1286 surface,
1287 &status, &vtime);
1288 CHECK_ST_WARNING("Error calling "
1289 "presentation_queue_query_surface_status");
1290 if (status == VDP_PRESENTATION_QUEUE_STATUS_QUEUED)
1291 break;
1292 if (vc->vsync_interval > 1) {
1293 uint64_t qtime = vc->queue_time[vc->query_surface_num];
1294 if (vtime < qtime + vc->vsync_interval / 2)
1295 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown too early\n");
1296 if (vtime > qtime + vc->vsync_interval)
1297 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown late\n");
1299 vc->query_surface_num = WRAP_ADD(vc->query_surface_num, 1,
1300 vc->num_output_surfaces);
1301 vc->recent_vsync_time = vtime;
1303 int num_queued = WRAP_ADD(vc->surface_num, -vc->query_surface_num,
1304 vc->num_output_surfaces);
1305 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Queued surface count (before add): "
1306 "%d\n", num_queued);
1307 return num_queued;
1310 static inline uint64_t prev_vs2(struct vdpctx *vc, uint64_t ts, int shift)
1312 uint64_t offset = ts - vc->recent_vsync_time;
1313 // Fix negative values for 1<<shift vsyncs before vc->recent_vsync_time
1314 offset += (uint64_t)vc->vsync_interval << shift;
1315 offset %= vc->vsync_interval;
1316 return ts - offset;
1319 static void flip_page_timed(struct vo *vo, unsigned int pts_us, int duration)
1321 struct vdpctx *vc = vo->priv;
1322 struct vdp_functions *vdp = vc->vdp;
1323 VdpStatus vdp_st;
1324 uint32_t vsync_interval = vc->vsync_interval;
1326 if (handle_preemption(vo) < 0)
1327 return;
1329 if (duration > INT_MAX / 1000)
1330 duration = -1;
1331 else
1332 duration *= 1000;
1334 if (vc->user_fps < 0)
1335 duration = -1; // Make sure drop logic is disabled
1337 uint64_t now = sync_vdptime(vo);
1338 uint64_t pts = pts_us ? convert_to_vdptime(vo, pts_us) : now;
1339 uint64_t ideal_pts = pts;
1340 uint64_t npts = duration >= 0 ? pts + duration : UINT64_MAX;
1342 #define PREV_VS2(ts, shift) prev_vs2(vc, ts, shift)
1343 // Only gives accurate results for ts >= vc->recent_vsync_time
1344 #define PREV_VSYNC(ts) PREV_VS2(ts, 0)
1346 /* We hope to be here at least one vsync before the frame should be shown.
1347 * If we are running late then don't drop the frame unless there is
1348 * already one queued for the next vsync; even if we _hope_ to show the
1349 * next frame soon enough to mean this one should be dropped we might
1350 * not make the target time in reality. Without this check we could drop
1351 * every frame, freezing the display completely if video lags behind.
1353 if (now > PREV_VSYNC(FFMAX(pts, vc->last_queue_time + vsync_interval)))
1354 npts = UINT64_MAX;
1356 /* Allow flipping a frame at a vsync if its presentation time is a
1357 * bit after that vsync and the change makes the flip time delta
1358 * from previous frame better match the target timestamp delta.
1359 * This avoids instability with frame timestamps falling near vsyncs.
1360 * For example if the frame timestamps were (with vsyncs at
1361 * integer values) 0.01, 1.99, 4.01, 5.99, 8.01, ... then
1362 * straightforward timing at next vsync would flip the frames at
1363 * 1, 2, 5, 6, 9; this changes it to 1, 2, 4, 6, 8 and so on with
1364 * regular 2-vsync intervals.
1366 * Also allow moving the frame forward if it looks like we dropped
1367 * the previous frame incorrectly (now that we know better after
1368 * having final exact timestamp information for this frame) and
1369 * there would unnecessarily be a vsync without a frame change.
1371 uint64_t vsync = PREV_VSYNC(pts);
1372 if (pts < vsync + vsync_interval / 4
1373 && (vsync - PREV_VS2(vc->last_queue_time, 16)
1374 > pts - vc->last_ideal_time + vsync_interval / 2
1375 || vc->dropped_frame && vsync > vc->dropped_time))
1376 pts -= vsync_interval / 2;
1378 vc->dropped_frame = true; // changed at end if false
1379 vc->dropped_time = ideal_pts;
1381 pts = FFMAX(pts, vc->last_queue_time + vsync_interval);
1382 pts = FFMAX(pts, now);
1383 if (npts < PREV_VSYNC(pts) + vsync_interval)
1384 return;
1386 int num_flips = update_presentation_queue_status(vo);
1387 vsync = vc->recent_vsync_time + num_flips * vc->vsync_interval;
1388 now = sync_vdptime(vo);
1389 pts = FFMAX(pts, now);
1390 pts = FFMAX(pts, vsync + (vsync_interval >> 2));
1391 vsync = PREV_VSYNC(pts);
1392 if (npts < vsync + vsync_interval)
1393 return;
1394 pts = vsync + (vsync_interval >> 2);
1395 vdp_st =
1396 vdp->presentation_queue_display(vc->flip_queue,
1397 vc->output_surfaces[vc->surface_num],
1398 vo->dwidth, vo->dheight, pts);
1399 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display");
1401 vc->last_queue_time = pts;
1402 vc->queue_time[vc->surface_num] = pts;
1403 vc->last_ideal_time = ideal_pts;
1404 vc->dropped_frame = false;
1405 vc->surface_num = WRAP_ADD(vc->surface_num, 1, vc->num_output_surfaces);
1406 vc->num_shown_frames = FFMIN(vc->num_shown_frames + 1, 1000);
1409 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
1410 int h, int x, int y)
1412 struct vdpctx *vc = vo->priv;
1413 struct vdp_functions *vdp = vc->vdp;
1414 VdpStatus vdp_st;
1416 if (handle_preemption(vo) < 0)
1417 return VO_TRUE;
1419 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
1420 int max_refs = vc->image_format == IMGFMT_VDPAU_H264 ?
1421 rndr->info.h264.num_ref_frames : 2;
1422 if (!IMGFMT_IS_VDPAU(vc->image_format))
1423 return VO_FALSE;
1424 if ((vc->decoder == VDP_INVALID_HANDLE || vc->decoder_max_refs < max_refs)
1425 && !create_vdp_decoder(vo, max_refs))
1426 return VO_FALSE;
1428 vdp_st = vdp->decoder_render(vc->decoder, rndr->surface,
1429 (void *)&rndr->info,
1430 rndr->bitstream_buffers_used,
1431 rndr->bitstream_buffers);
1432 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
1433 return VO_TRUE;
1437 static struct vdpau_render_state *get_surface(struct vo *vo, int number)
1439 struct vdpctx *vc = vo->priv;
1440 struct vdp_functions *vdp = vc->vdp;
1442 if (number > MAX_VIDEO_SURFACES)
1443 return NULL;
1444 if (vc->surface_render[number].surface == VDP_INVALID_HANDLE
1445 && !vc->is_preempted) {
1446 VdpStatus vdp_st;
1447 vdp_st = vdp->video_surface_create(vc->vdp_device, vc->vdp_chroma_type,
1448 vc->vid_width, vc->vid_height,
1449 &vc->surface_render[number].surface);
1450 CHECK_ST_WARNING("Error when calling vdp_video_surface_create");
1452 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n",
1453 vc->surface_render[number].surface);
1454 return &vc->surface_render[number];
1457 static void draw_image(struct vo *vo, mp_image_t *mpi, double pts)
1459 struct vdpctx *vc = vo->priv;
1460 struct vdp_functions *vdp = vc->vdp;
1461 struct mp_image *reserved_mpi = NULL;
1462 struct vdpau_render_state *rndr;
1464 if (vc->is_preempted) {
1465 vo->frame_loaded = true;
1466 return;
1469 if (IMGFMT_IS_VDPAU(vc->image_format)) {
1470 rndr = mpi->priv;
1471 reserved_mpi = mpi;
1472 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1473 VdpStatus vdp_st;
1474 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
1475 rndr = get_surface(vo, vc->deint_counter);
1476 vc->deint_counter = WRAP_ADD(vc->deint_counter, 1, NUM_BUFFERED_VIDEO);
1477 if (vc->image_format == IMGFMT_NV12)
1478 destdata[1] = destdata[2];
1479 vdp_st =
1480 vdp->video_surface_put_bits_y_cb_cr(rndr->surface,
1481 vc->vdp_pixel_format,
1482 (const void *const*)destdata,
1483 mpi->stride); // pitch
1484 CHECK_ST_WARNING("Error when calling "
1485 "vdp_video_surface_put_bits_y_cb_cr");
1486 } else
1487 // We don't support slice callbacks so this shouldn't occur -
1488 // I think the flags test above in pointless, but I'm adding
1489 // this instead of removing it just in case.
1490 abort();
1491 if (mpi->fields & MP_IMGFIELD_ORDERED)
1492 vc->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1493 else
1494 vc->top_field_first = 1;
1496 add_new_video_surface(vo, rndr->surface, reserved_mpi, pts);
1498 return;
1501 static uint32_t get_image(struct vo *vo, mp_image_t *mpi)
1503 struct vdpctx *vc = vo->priv;
1504 struct vdpau_render_state *rndr;
1506 // no dr for non-decoding for now
1507 if (!IMGFMT_IS_VDPAU(vc->image_format))
1508 return VO_FALSE;
1509 if (mpi->type != MP_IMGTYPE_NUMBERED)
1510 return VO_FALSE;
1512 rndr = get_surface(vo, mpi->number);
1513 if (!rndr) {
1514 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in "
1515 "get_image\n");
1516 // TODO: this probably breaks things forever, provide a dummy buffer?
1517 return VO_FALSE;
1519 mpi->flags |= MP_IMGFLAG_DIRECT;
1520 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1521 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1522 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1523 mpi->planes[0] = (void *)rndr;
1524 mpi->num_planes = 1;
1525 mpi->priv = rndr;
1526 return VO_TRUE;
1529 static int query_format(uint32_t format)
1531 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
1532 | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD
1533 | VFCAP_EOSD_UNSCALED | VFCAP_FLIP;
1534 switch (format) {
1535 case IMGFMT_YV12:
1536 case IMGFMT_I420:
1537 case IMGFMT_IYUV:
1538 case IMGFMT_NV12:
1539 case IMGFMT_YUY2:
1540 case IMGFMT_UYVY:
1541 return default_flags | VOCAP_NOSLICES;
1542 case IMGFMT_VDPAU_MPEG1:
1543 case IMGFMT_VDPAU_MPEG2:
1544 case IMGFMT_VDPAU_H264:
1545 case IMGFMT_VDPAU_WMV3:
1546 case IMGFMT_VDPAU_VC1:
1547 case IMGFMT_VDPAU_MPEG4:
1548 return default_flags;
1550 return 0;
1553 static void destroy_vdpau_objects(struct vo *vo)
1555 struct vdpctx *vc = vo->priv;
1556 struct vdp_functions *vdp = vc->vdp;
1558 int i;
1559 VdpStatus vdp_st;
1561 free_video_specific(vo);
1563 if (vc->flip_queue != VDP_INVALID_HANDLE) {
1564 vdp_st = vdp->presentation_queue_destroy(vc->flip_queue);
1565 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy");
1568 if (vc->flip_target != VDP_INVALID_HANDLE) {
1569 vdp_st = vdp->presentation_queue_target_destroy(vc->flip_target);
1570 CHECK_ST_WARNING("Error when calling "
1571 "vdp_presentation_queue_target_destroy");
1574 for (i = 0; i <= vc->num_output_surfaces; i++) {
1575 if (vc->output_surfaces[i] == VDP_INVALID_HANDLE)
1576 continue;
1577 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
1578 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy");
1581 if (vc->eosd_surface.surface != VDP_INVALID_HANDLE) {
1582 vdp_st = vdp->bitmap_surface_destroy(vc->eosd_surface.surface);
1583 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1586 vdp_st = vdp->device_destroy(vc->vdp_device);
1587 CHECK_ST_WARNING("Error when calling vdp_device_destroy");
1590 static void uninit(struct vo *vo)
1592 struct vdpctx *vc = vo->priv;
1594 /* Destroy all vdpau objects */
1595 destroy_vdpau_objects(vo);
1597 #ifdef CONFIG_XF86VM
1598 if (vc->mode_switched)
1599 vo_vm_close(vo);
1600 #endif
1601 vo_x11_uninit(vo);
1603 // Free bitstream buffers allocated by FFmpeg
1604 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
1605 av_freep(&vc->surface_render[i].bitstream_buffers);
1608 static int preinit(struct vo *vo, const char *arg)
1610 int i;
1612 struct vdpctx *vc = talloc_zero(vo, struct vdpctx);
1613 vo->priv = vc;
1615 // Mark everything as invalid first so uninit() can tell what has been
1616 // allocated
1617 mark_vdpau_objects_uninitialized(vo);
1619 vc->deint_type = 3;
1620 vc->chroma_deint = 1;
1621 vc->user_colorspace = 1;
1622 vc->flip_offset_window = 50;
1623 vc->flip_offset_fs = 50;
1624 vc->num_output_surfaces = 3;
1625 const opt_t subopts[] = {
1626 {"deint", OPT_ARG_INT, &vc->deint, NULL},
1627 {"chroma-deint", OPT_ARG_BOOL, &vc->chroma_deint, NULL},
1628 {"pullup", OPT_ARG_BOOL, &vc->pullup, NULL},
1629 {"denoise", OPT_ARG_FLOAT, &vc->denoise, NULL},
1630 {"sharpen", OPT_ARG_FLOAT, &vc->sharpen, NULL},
1631 {"colorspace", OPT_ARG_INT, &vc->user_colorspace, NULL},
1632 {"studio", OPT_ARG_BOOL, &vc->studio_levels, NULL},
1633 {"hqscaling", OPT_ARG_INT, &vc->hqscaling, NULL},
1634 {"fps", OPT_ARG_FLOAT, &vc->user_fps, NULL},
1635 {"queuetime_windowed", OPT_ARG_INT, &vc->flip_offset_window, NULL},
1636 {"queuetime_fs", OPT_ARG_INT, &vc->flip_offset_fs, NULL},
1637 {"output_surfaces", OPT_ARG_INT, &vc->num_output_surfaces, NULL},
1638 {NULL}
1640 if (subopt_parse(arg, subopts) != 0) {
1641 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Could not parse suboptions.\n");
1642 return -1;
1644 if (vc->hqscaling < 0 || vc->hqscaling > 9) {
1645 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid value for suboption "
1646 "hqscaling\n");
1647 return -1;
1649 if (vc->num_output_surfaces < 2) {
1650 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid suboption "
1651 "output_surfaces: can't use less than 2 surfaces\n");
1652 return -1;
1654 if (vc->num_output_surfaces > MAX_OUTPUT_SURFACES) {
1655 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Number of output surfaces "
1656 "is limited to %d.\n", MAX_OUTPUT_SURFACES);
1657 vc->num_output_surfaces = MAX_OUTPUT_SURFACES;
1659 if (vc->deint)
1660 vc->deint_type = FFABS(vc->deint);
1661 if (vc->deint < 0)
1662 vc->deint = 0;
1664 if (!vo_init(vo))
1665 return -1;
1667 // After this calling uninit() should work to free resources
1669 if (win_x11_init_vdpau_procs(vo) < 0) {
1670 if (vc->vdp->device_destroy)
1671 vc->vdp->device_destroy(vc->vdp_device);
1672 vo_x11_uninit(vo);
1673 return -1;
1676 // full grayscale palette.
1677 for (i = 0; i < PALETTE_SIZE; ++i)
1678 vc->palette[i] = (i << 16) | (i << 8) | i;
1680 vc->procamp.struct_version = VDP_PROCAMP_VERSION;
1681 vc->procamp.brightness = 0.0;
1682 vc->procamp.contrast = 1.0;
1683 vc->procamp.saturation = 1.0;
1684 vc->procamp.hue = 0.0;
1686 return 0;
1689 static int get_equalizer(struct vo *vo, const char *name, int *value)
1691 struct vdpctx *vc = vo->priv;
1693 if (!strcasecmp(name, "brightness"))
1694 *value = vc->procamp.brightness * 100;
1695 else if (!strcasecmp(name, "contrast"))
1696 *value = (vc->procamp.contrast - 1.0) * 100;
1697 else if (!strcasecmp(name, "saturation"))
1698 *value = (vc->procamp.saturation - 1.0) * 100;
1699 else if (!strcasecmp(name, "hue"))
1700 *value = vc->procamp.hue * 100 / M_PI;
1701 else
1702 return VO_NOTIMPL;
1703 return VO_TRUE;
1706 static int set_equalizer(struct vo *vo, const char *name, int value)
1708 struct vdpctx *vc = vo->priv;
1710 if (!strcasecmp(name, "brightness"))
1711 vc->procamp.brightness = value / 100.0;
1712 else if (!strcasecmp(name, "contrast"))
1713 vc->procamp.contrast = value / 100.0 + 1.0;
1714 else if (!strcasecmp(name, "saturation"))
1715 vc->procamp.saturation = value / 100.0 + 1.0;
1716 else if (!strcasecmp(name, "hue"))
1717 vc->procamp.hue = value / 100.0 * M_PI;
1718 else
1719 return VO_NOTIMPL;
1721 update_csc_matrix(vo);
1722 return true;
1725 static int control(struct vo *vo, uint32_t request, void *data)
1727 struct vdpctx *vc = vo->priv;
1728 struct vdp_functions *vdp = vc->vdp;
1730 handle_preemption(vo);
1732 switch (request) {
1733 case VOCTRL_GET_DEINTERLACE:
1734 *(int *)data = vc->deint;
1735 return VO_TRUE;
1736 case VOCTRL_SET_DEINTERLACE:
1737 vc->deint = *(int *)data;
1738 if (vc->deint)
1739 vc->deint = vc->deint_type;
1740 if (vc->deint_type > 2) {
1741 VdpStatus vdp_st;
1742 VdpVideoMixerFeature features[1] =
1743 {vc->deint_type == 3 ?
1744 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1745 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1746 VdpBool feature_enables[1] = {vc->deint ? VDP_TRUE : VDP_FALSE};
1747 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
1748 1, features,
1749 feature_enables);
1750 CHECK_ST_WARNING("Error changing deinterlacing settings");
1752 return VO_TRUE;
1753 case VOCTRL_PAUSE:
1754 if (vc->dropped_frame)
1755 flip_page_timed(vo, 0, -1);
1756 return (vc->paused = true);
1757 case VOCTRL_RESUME:
1758 return (vc->paused = false);
1759 case VOCTRL_QUERY_FORMAT:
1760 return query_format(*(uint32_t *)data);
1761 case VOCTRL_GET_IMAGE:
1762 return get_image(vo, data);
1763 case VOCTRL_DRAW_IMAGE:
1764 abort(); // draw_image() should get called directly
1765 case VOCTRL_BORDER:
1766 vo_x11_border(vo);
1767 resize(vo);
1768 return VO_TRUE;
1769 case VOCTRL_FULLSCREEN:
1770 vo_x11_fullscreen(vo);
1771 resize(vo);
1772 return VO_TRUE;
1773 case VOCTRL_GET_PANSCAN:
1774 return VO_TRUE;
1775 case VOCTRL_SET_PANSCAN:
1776 resize(vo);
1777 return VO_TRUE;
1778 case VOCTRL_SET_EQUALIZER: {
1779 struct voctrl_set_equalizer_args *args = data;
1780 return set_equalizer(vo, args->name, args->value);
1782 case VOCTRL_GET_EQUALIZER: {
1783 struct voctrl_get_equalizer_args *args = data;
1784 return get_equalizer(vo, args->name, args->valueptr);
1786 case VOCTRL_SET_YUV_COLORSPACE:
1787 vc->colorspace = *(int *)data % 3;
1788 update_csc_matrix(vo);
1789 return true;
1790 case VOCTRL_GET_YUV_COLORSPACE:
1791 *(int *)data = vc->colorspace;
1792 return true;
1793 case VOCTRL_ONTOP:
1794 vo_x11_ontop(vo);
1795 return VO_TRUE;
1796 case VOCTRL_UPDATE_SCREENINFO:
1797 update_xinerama_info(vo);
1798 return VO_TRUE;
1799 case VOCTRL_DRAW_EOSD:
1800 if (!data)
1801 return VO_FALSE;
1802 generate_eosd(vo, data);
1803 draw_eosd(vo);
1804 return VO_TRUE;
1805 case VOCTRL_GET_EOSD_RES: {
1806 struct mp_eosd_res *r = data;
1807 r->w = vo->dwidth;
1808 r->h = vo->dheight;
1809 r->ml = r->mr = vc->border_x;
1810 r->mt = r->mb = vc->border_y;
1811 return VO_TRUE;
1813 case VOCTRL_REDRAW_OSD:
1814 video_to_output_surface(vo);
1815 draw_eosd(vo);
1816 draw_osd(vo, data);
1817 flip_page_timed(vo, 0, -1);
1818 return true;
1819 case VOCTRL_RESET:
1820 forget_frames(vo);
1821 return true;
1823 return VO_NOTIMPL;
1826 const struct vo_driver video_out_vdpau = {
1827 .is_new = true,
1828 .buffer_frames = true,
1829 .info = &(const struct vo_info_s){
1830 "VDPAU with X11",
1831 "vdpau",
1832 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
1835 .preinit = preinit,
1836 .config = config,
1837 .control = control,
1838 .draw_image = draw_image,
1839 .get_buffered_frame = get_buffered_frame,
1840 .draw_slice = draw_slice,
1841 .draw_osd = draw_osd,
1842 .flip_page_timed = flip_page_timed,
1843 .check_events = check_events,
1844 .uninit = uninit,