Merge remote-tracking branch 'wm4/window_title'
[mplayer.git] / libvo / vo_vdpau.c
blob42200d8f5dcf6bab46888a6854f1b4a77c03dd2b
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 "csputils.h"
45 #include "sub/sub.h"
46 #include "subopt-helper.h"
47 #include "libmpcodecs/vfcap.h"
48 #include "libmpcodecs/mp_image.h"
49 #include "osdep/timer.h"
51 #include "libavcodec/vdpau.h"
53 #include "sub/font_load.h"
55 #include "libavutil/common.h"
56 #include "libavutil/mathematics.h"
58 #include "sub/ass_mp.h"
60 #define WRAP_ADD(x, a, m) ((a) < 0 \
61 ? ((x)+(a)+(m) < (m) ? (x)+(a)+(m) : (x)+(a)) \
62 : ((x)+(a) < (m) ? (x)+(a) : (x)+(a)-(m)))
64 #define CHECK_ST_ERROR(message) \
65 do { \
66 if (vdp_st != VDP_STATUS_OK) { \
67 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
68 message, vdp->get_error_string(vdp_st)); \
69 return -1; \
70 } \
71 } while (0)
73 #define CHECK_ST_WARNING(message) \
74 do { \
75 if (vdp_st != VDP_STATUS_OK) \
76 mp_msg(MSGT_VO, MSGL_WARN, "[ vdpau] %s: %s\n", \
77 message, vdp->get_error_string(vdp_st)); \
78 } while (0)
80 /* number of video and output surfaces */
81 #define MAX_OUTPUT_SURFACES 15
82 #define MAX_VIDEO_SURFACES 50
83 #define NUM_BUFFERED_VIDEO 5
85 /* number of palette entries */
86 #define PALETTE_SIZE 256
88 /* Initial size of EOSD surface in pixels (x*x) */
89 #define EOSD_SURFACE_INITIAL_SIZE 256
91 /* Pixelformat used for output surfaces */
92 #define OUTPUT_RGBA_FORMAT VDP_RGBA_FORMAT_B8G8R8A8
95 * Global variable declaration - VDPAU specific
98 struct vdp_functions {
99 #define VDP_FUNCTION(vdp_type, _, mp_name) vdp_type *mp_name;
100 #include "vdpau_template.c"
101 #undef VDP_FUNCTION
104 struct vdpctx {
105 struct vdp_functions *vdp;
107 VdpDevice vdp_device;
108 bool is_preempted;
109 bool preemption_acked;
110 bool preemption_user_notified;
111 unsigned int last_preemption_retry_fail;
112 VdpGetProcAddress *vdp_get_proc_address;
114 VdpPresentationQueueTarget flip_target;
115 VdpPresentationQueue flip_queue;
116 uint64_t last_vdp_time;
117 unsigned int last_sync_update;
119 /* an extra last output surface is used for OSD and screenshots */
120 VdpOutputSurface output_surfaces[MAX_OUTPUT_SURFACES + 1];
121 int num_output_surfaces;
122 struct buffered_video_surface {
123 VdpVideoSurface surface;
124 double pts;
125 mp_image_t *mpi;
126 } buffered_video[NUM_BUFFERED_VIDEO];
127 int deint_queue_pos;
128 int output_surface_width, output_surface_height;
130 VdpVideoMixer video_mixer;
131 struct mp_csp_details colorspace;
132 int deint;
133 int deint_type;
134 int deint_counter;
135 int pullup;
136 float denoise;
137 float sharpen;
138 int hqscaling;
139 int chroma_deint;
140 int flip_offset_window;
141 int flip_offset_fs;
142 int top_field_first;
143 bool flip;
145 VdpDecoder decoder;
146 int decoder_max_refs;
148 VdpRect src_rect_vid;
149 VdpRect out_rect_vid;
150 int border_x, border_y;
152 struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES];
153 int surface_num;
154 int query_surface_num;
155 VdpTime recent_vsync_time;
156 float user_fps;
157 unsigned int vsync_interval;
158 uint64_t last_queue_time;
159 uint64_t queue_time[MAX_OUTPUT_SURFACES];
160 uint64_t last_ideal_time;
161 bool dropped_frame;
162 uint64_t dropped_time;
163 uint32_t vid_width, vid_height;
164 uint32_t vid_d_width, vid_d_height;
165 uint32_t image_format;
166 VdpChromaType vdp_chroma_type;
167 VdpYCbCrFormat vdp_pixel_format;
169 /* draw_osd */
170 unsigned char *index_data;
171 int index_data_size;
172 uint32_t palette[PALETTE_SIZE];
174 // EOSD
175 // Pool of surfaces
176 struct eosd_bitmap_surface {
177 VdpBitmapSurface surface;
178 int w;
179 int h;
180 uint32_t max_width;
181 uint32_t max_height;
182 } eosd_surface;
184 // List of surfaces to be rendered
185 struct eosd_target {
186 VdpRect source;
187 VdpRect dest;
188 VdpColor color;
189 } *eosd_targets;
190 int eosd_targets_size;
191 int *eosd_scratch;
193 int eosd_render_count;
195 // Video equalizer
196 struct mp_csp_equalizer video_eq;
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_DBG2, "[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 render_video_to_output_surface(struct vo *vo,
247 VdpOutputSurface output_surface,
248 VdpRect *output_rect)
250 struct vdpctx *vc = vo->priv;
251 struct vdp_functions *vdp = vc->vdp;
252 VdpTime dummy;
253 VdpStatus vdp_st;
254 if (vc->deint_queue_pos < 0)
255 return -1;
257 struct buffered_video_surface *bv = vc->buffered_video;
258 int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
259 unsigned int dp = vc->deint_queue_pos;
260 // dp==0 means last field of latest frame, 1 earlier field of latest frame,
261 // 2 last field of previous frame and so on
262 if (vc->deint) {
263 field = vc->top_field_first ^ (dp & 1) ?
264 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
265 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
267 const VdpVideoSurface *past_fields = (const VdpVideoSurface []){
268 bv[(dp+1)/2].surface, bv[(dp+2)/2].surface};
269 const VdpVideoSurface *future_fields = (const VdpVideoSurface []){
270 dp >= 1 ? bv[(dp-1)/2].surface : VDP_INVALID_HANDLE};
271 vdp_st = vdp->presentation_queue_block_until_surface_idle(vc->flip_queue,
272 output_surface,
273 &dummy);
274 CHECK_ST_WARNING("Error when calling "
275 "vdp_presentation_queue_block_until_surface_idle");
277 vdp_st = vdp->video_mixer_render(vc->video_mixer, VDP_INVALID_HANDLE,
278 0, field, 2, past_fields,
279 bv[dp/2].surface, 1, future_fields,
280 &vc->src_rect_vid, output_surface,
281 NULL, output_rect, 0, NULL);
282 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render");
283 return 0;
286 static int video_to_output_surface(struct vo *vo)
288 struct vdpctx *vc = vo->priv;
290 return render_video_to_output_surface(vo,
291 vc->output_surfaces[vc->surface_num],
292 &vc->out_rect_vid);
295 static int next_deint_queue_pos(struct vo *vo, bool eof)
297 struct vdpctx *vc = vo->priv;
299 int dqp = vc->deint_queue_pos;
300 if (dqp < 0)
301 dqp += 1000;
302 else
303 dqp = vc->deint >= 2 ? dqp - 1 : dqp - 2 | 1;
304 if (dqp < (eof ? 0 : 3))
305 return -1;
306 return dqp;
309 static void set_next_frame_info(struct vo *vo, bool eof)
311 struct vdpctx *vc = vo->priv;
313 vo->frame_loaded = false;
314 int dqp = next_deint_queue_pos(vo, eof);
315 if (dqp < 0)
316 return;
317 vo->frame_loaded = true;
319 // Set pts values
320 struct buffered_video_surface *bv = vc->buffered_video;
321 int idx = dqp >> 1;
322 if (idx == 0) { // no future frame/pts available
323 vo->next_pts = bv[0].pts;
324 vo->next_pts2 = MP_NOPTS_VALUE;
325 } else if (!(vc->deint >= 2)) { // no field-splitting deinterlace
326 vo->next_pts = bv[idx].pts;
327 vo->next_pts2 = bv[idx - 1].pts;
328 } else { // deinterlace with separate fields
329 double intermediate_pts;
330 double diff = bv[idx - 1].pts - bv[idx].pts;
331 if (diff > 0 && diff < 0.5)
332 intermediate_pts = (bv[idx].pts + bv[idx - 1].pts) / 2;
333 else
334 intermediate_pts = bv[idx].pts;
335 if (dqp & 1) { // first field
336 vo->next_pts = bv[idx].pts;
337 vo->next_pts2 = intermediate_pts;
338 } else {
339 vo->next_pts = intermediate_pts;
340 vo->next_pts2 = bv[idx - 1].pts;
345 static void add_new_video_surface(struct vo *vo, VdpVideoSurface surface,
346 struct mp_image *reserved_mpi, double pts)
348 struct vdpctx *vc = vo->priv;
349 struct buffered_video_surface *bv = vc->buffered_video;
351 if (reserved_mpi)
352 reserved_mpi->usage_count++;
353 if (bv[NUM_BUFFERED_VIDEO - 1].mpi)
354 bv[NUM_BUFFERED_VIDEO - 1].mpi->usage_count--;
356 for (int i = NUM_BUFFERED_VIDEO - 1; i > 0; i--)
357 bv[i] = bv[i - 1];
358 bv[0] = (struct buffered_video_surface){
359 .mpi = reserved_mpi,
360 .surface = surface,
361 .pts = pts,
364 vc->deint_queue_pos = FFMIN(vc->deint_queue_pos + 2,
365 NUM_BUFFERED_VIDEO * 2 - 3);
366 set_next_frame_info(vo, false);
369 static void forget_frames(struct vo *vo)
371 struct vdpctx *vc = vo->priv;
373 vc->deint_queue_pos = -1001;
374 vc->dropped_frame = false;
375 for (int i = 0; i < NUM_BUFFERED_VIDEO; i++) {
376 struct buffered_video_surface *p = vc->buffered_video + i;
377 if (p->mpi)
378 p->mpi->usage_count--;
379 *p = (struct buffered_video_surface){
380 .surface = VDP_INVALID_HANDLE,
385 static void resize(struct vo *vo)
387 struct vdpctx *vc = vo->priv;
388 struct vdp_functions *vdp = vc->vdp;
389 VdpStatus vdp_st;
390 int i;
391 struct vo_rect src_rect;
392 struct vo_rect dst_rect;
393 struct vo_rect borders;
394 calc_src_dst_rects(vo, vc->vid_width, vc->vid_height, &src_rect, &dst_rect,
395 &borders, NULL);
396 vc->out_rect_vid.x0 = dst_rect.left;
397 vc->out_rect_vid.x1 = dst_rect.right;
398 vc->out_rect_vid.y0 = dst_rect.top;
399 vc->out_rect_vid.y1 = dst_rect.bottom;
400 vc->src_rect_vid.x0 = src_rect.left;
401 vc->src_rect_vid.x1 = src_rect.right;
402 vc->src_rect_vid.y0 = vc->flip ? src_rect.bottom : src_rect.top;
403 vc->src_rect_vid.y1 = vc->flip ? src_rect.top : src_rect.bottom;
404 vc->border_x = borders.left;
405 vc->border_y = borders.top;
406 #ifdef CONFIG_FREETYPE
407 // adjust font size to display size
408 force_load_font = 1;
409 #endif
410 vo_osd_changed(OSDTYPE_OSD);
411 int flip_offset_ms = vo_fs ? vc->flip_offset_fs : vc->flip_offset_window;
412 vo->flip_queue_offset = flip_offset_ms / 1000.;
414 int min_output_width = FFMAX(vo->dwidth, vc->vid_width);
415 int min_output_height = FFMAX(vo->dheight, vc->vid_height);
417 if (vc->output_surface_width < min_output_width
418 || vc->output_surface_height < min_output_height) {
419 if (vc->output_surface_width < min_output_width) {
420 vc->output_surface_width += vc->output_surface_width >> 1;
421 vc->output_surface_width = FFMAX(vc->output_surface_width,
422 min_output_width);
424 if (vc->output_surface_height < min_output_height) {
425 vc->output_surface_height += vc->output_surface_height >> 1;
426 vc->output_surface_height = FFMAX(vc->output_surface_height,
427 min_output_height);
429 // Creation of output_surfaces
430 for (i = 0; i <= vc->num_output_surfaces; i++) {
431 if (vc->output_surfaces[i] != VDP_INVALID_HANDLE) {
432 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
433 CHECK_ST_WARNING("Error when calling "
434 "vdp_output_surface_destroy");
436 vdp_st = vdp->output_surface_create(vc->vdp_device,
437 OUTPUT_RGBA_FORMAT,
438 vc->output_surface_width,
439 vc->output_surface_height,
440 &vc->output_surfaces[i]);
441 CHECK_ST_WARNING("Error when calling vdp_output_surface_create");
442 mp_msg(MSGT_VO, MSGL_DBG2, "vdpau out create: %u\n",
443 vc->output_surfaces[i]);
446 vo->want_redraw = true;
449 static void preemption_callback(VdpDevice device, void *context)
451 struct vdpctx *vc = context;
452 vc->is_preempted = true;
453 vc->preemption_acked = false;
456 /* Initialize vdp_get_proc_address, called from preinit() */
457 static int win_x11_init_vdpau_procs(struct vo *vo)
459 struct vo_x11_state *x11 = vo->x11;
460 struct vdpctx *vc = vo->priv;
461 talloc_free(vc->vdp); // In case this is reinitialization after preemption
462 struct vdp_functions *vdp = talloc_zero(vc, struct vdp_functions);
463 vc->vdp = vdp;
464 VdpStatus vdp_st;
466 struct vdp_function {
467 const int id;
468 int offset;
471 const struct vdp_function *dsc;
473 static const struct vdp_function vdp_func[] = {
474 #define VDP_FUNCTION(_, macro_name, mp_name) {macro_name, offsetof(struct vdp_functions, mp_name)},
475 #include "vdpau_template.c"
476 #undef VDP_FUNCTION
477 {0, -1}
480 vdp_st = vdp_device_create_x11(x11->display, x11->screen, &vc->vdp_device,
481 &vc->vdp_get_proc_address);
482 if (vdp_st != VDP_STATUS_OK) {
483 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling "
484 "vdp_device_create_x11: %i\n", vdp_st);
485 return -1;
488 vdp->get_error_string = NULL;
489 for (dsc = vdp_func; dsc->offset >= 0; dsc++) {
490 vdp_st = vc->vdp_get_proc_address(vc->vdp_device, dsc->id,
491 (void **)((char *)vdp + dsc->offset));
492 if (vdp_st != VDP_STATUS_OK) {
493 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling "
494 "vdp_get_proc_address(function id %d): %s\n", dsc->id,
495 vdp->get_error_string ? vdp->get_error_string(vdp_st) : "?");
496 return -1;
499 vdp_st = vdp->preemption_callback_register(vc->vdp_device,
500 preemption_callback, vc);
501 return 0;
504 static int win_x11_init_vdpau_flip_queue(struct vo *vo)
506 struct vdpctx *vc = vo->priv;
507 struct vdp_functions *vdp = vc->vdp;
508 struct vo_x11_state *x11 = vo->x11;
509 VdpStatus vdp_st;
511 if (vc->flip_target == VDP_INVALID_HANDLE) {
512 vdp_st = vdp->presentation_queue_target_create_x11(vc->vdp_device,
513 x11->window,
514 &vc->flip_target);
515 CHECK_ST_ERROR("Error when calling "
516 "vdp_presentation_queue_target_create_x11");
519 /* Emperically this seems to be the first call which fails when we
520 * try to reinit after preemption while the user is still switched
521 * from X to a virtual terminal (creating the vdp_device initially
522 * succeeds, as does creating the flip_target above). This is
523 * probably not guaranteed behavior, but we'll assume it as a simple
524 * way to reduce warnings while trying to recover from preemption.
526 if (vc->flip_queue == VDP_INVALID_HANDLE) {
527 vdp_st = vdp->presentation_queue_create(vc->vdp_device, vc->flip_target,
528 &vc->flip_queue);
529 if (vc->is_preempted && vdp_st != VDP_STATUS_OK) {
530 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Failed to create flip queue "
531 "while preempted: %s\n", vdp->get_error_string(vdp_st));
532 return -1;
533 } else
534 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create");
537 VdpTime vdp_time;
538 vdp_st = vdp->presentation_queue_get_time(vc->flip_queue, &vdp_time);
539 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_get_time");
540 vc->last_vdp_time = vdp_time;
541 vc->last_sync_update = GetTimer();
543 vc->vsync_interval = 1;
544 if (vc->user_fps > 0) {
545 vc->vsync_interval = 1e9 / vc->user_fps;
546 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Assuming user-specified display "
547 "refresh rate of %.3f Hz.\n", vc->user_fps);
548 } else if (vc->user_fps == 0) {
549 #ifdef CONFIG_XF86VM
550 double fps = vo_vm_get_fps(vo);
551 if (!fps)
552 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Failed to get display FPS\n");
553 else {
554 vc->vsync_interval = 1e9 / fps;
555 // This is verbose, but I'm not yet sure how common wrong values are
556 mp_msg(MSGT_VO, MSGL_INFO,
557 "[vdpau] Got display refresh rate %.3f Hz.\n"
558 "[vdpau] If that value looks wrong give the "
559 "-vo vdpau:fps=X suboption manually.\n", fps);
561 #else
562 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] This binary has been compiled "
563 "without XF86VidMode support.\n");
564 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Can't use vsync-aware timing "
565 "without manually provided -vo vdpau:fps=X suboption.\n");
566 #endif
567 } else
568 mp_msg(MSGT_VO, MSGL_V, "[vdpau] framedrop/timing logic disabled by "
569 "user.\n");
571 return 0;
574 static int set_video_attribute(struct vdpctx *vc, VdpVideoMixerAttribute attr,
575 const void *value, char *attr_name)
577 struct vdp_functions *vdp = vc->vdp;
578 VdpStatus vdp_st;
580 vdp_st = vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, &attr,
581 &value);
582 if (vdp_st != VDP_STATUS_OK) {
583 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error setting video mixer "
584 "attribute %s: %s\n", attr_name, vdp->get_error_string(vdp_st));
585 return -1;
587 return 0;
590 static void update_csc_matrix(struct vo *vo)
592 struct vdpctx *vc = vo->priv;
594 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Updating CSC matrix\n");
596 // VdpCSCMatrix happens to be compatible with mplayer's CSC matrix type
597 // both are float[3][4]
598 VdpCSCMatrix matrix;
600 struct mp_csp_params cparams = { .colorspace = vc->colorspace };
601 mp_csp_copy_equalizer_values(&cparams, &vc->video_eq);
602 mp_get_yuv2rgb_coeffs(&cparams, matrix);
604 set_video_attribute(vc, VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX,
605 &matrix, "CSC matrix");
608 #define SET_VIDEO_ATTR(attr_name, attr_type, value) set_video_attribute(vc, \
609 VDP_VIDEO_MIXER_ATTRIBUTE_ ## attr_name, &(attr_type){value},\
610 # attr_name)
611 static int create_vdp_mixer(struct vo *vo, VdpChromaType vdp_chroma_type)
613 struct vdpctx *vc = vo->priv;
614 struct vdp_functions *vdp = vc->vdp;
615 #define VDP_NUM_MIXER_PARAMETER 3
616 #define MAX_NUM_FEATURES 6
617 int i;
618 VdpStatus vdp_st;
620 if (vc->video_mixer != VDP_INVALID_HANDLE)
621 return 0;
623 int feature_count = 0;
624 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
625 VdpBool feature_enables[MAX_NUM_FEATURES];
626 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
627 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
628 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
629 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE,
631 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
632 &vc->vid_width,
633 &vc->vid_height,
634 &vdp_chroma_type,
636 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
637 if (vc->deint_type == 4)
638 features[feature_count++] =
639 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
640 if (vc->pullup)
641 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
642 if (vc->denoise)
643 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
644 if (vc->sharpen)
645 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
646 if (vc->hqscaling) {
647 VdpVideoMixerFeature hqscaling_feature =
648 VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 + vc->hqscaling-1;
649 VdpBool hqscaling_available;
650 vdp_st = vdp->video_mixer_query_feature_support(vc->vdp_device,
651 hqscaling_feature,
652 &hqscaling_available);
653 CHECK_ST_ERROR("Error when calling video_mixer_query_feature_support");
654 if (hqscaling_available)
655 features[feature_count++] = hqscaling_feature;
656 else
657 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Your hardware or VDPAU "
658 "library does not support requested hqscaling.\n");
661 vdp_st = vdp->video_mixer_create(vc->vdp_device, feature_count, features,
662 VDP_NUM_MIXER_PARAMETER,
663 parameters, parameter_values,
664 &vc->video_mixer);
665 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create");
667 for (i = 0; i < feature_count; i++)
668 feature_enables[i] = VDP_TRUE;
669 if (vc->deint < 3)
670 feature_enables[0] = VDP_FALSE;
671 if (vc->deint_type == 4 && vc->deint < 4)
672 feature_enables[1] = VDP_FALSE;
673 if (feature_count) {
674 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
675 feature_count, features,
676 feature_enables);
677 CHECK_ST_WARNING("Error calling vdp_video_mixer_set_feature_enables");
679 if (vc->denoise)
680 SET_VIDEO_ATTR(NOISE_REDUCTION_LEVEL, float, vc->denoise);
681 if (vc->sharpen)
682 SET_VIDEO_ATTR(SHARPNESS_LEVEL, float, vc->sharpen);
683 if (!vc->chroma_deint)
684 SET_VIDEO_ATTR(SKIP_CHROMA_DEINTERLACE, uint8_t, 1);
686 update_csc_matrix(vo);
687 return 0;
690 // Free everything specific to a certain video file
691 static void free_video_specific(struct vo *vo)
693 struct vdpctx *vc = vo->priv;
694 struct vdp_functions *vdp = vc->vdp;
695 int i;
696 VdpStatus vdp_st;
698 if (vc->decoder != VDP_INVALID_HANDLE)
699 vdp->decoder_destroy(vc->decoder);
700 vc->decoder = VDP_INVALID_HANDLE;
701 vc->decoder_max_refs = -1;
703 forget_frames(vo);
705 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
706 if (vc->surface_render[i].surface != VDP_INVALID_HANDLE) {
707 vdp_st = vdp->video_surface_destroy(vc->surface_render[i].surface);
708 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy");
710 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
713 if (vc->video_mixer != VDP_INVALID_HANDLE) {
714 vdp_st = vdp->video_mixer_destroy(vc->video_mixer);
715 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy");
717 vc->video_mixer = VDP_INVALID_HANDLE;
720 static int create_vdp_decoder(struct vo *vo, int max_refs)
722 struct vdpctx *vc = vo->priv;
723 struct vdp_functions *vdp = vc->vdp;
724 VdpStatus vdp_st;
725 VdpDecoderProfile vdp_decoder_profile;
726 if (vc->decoder != VDP_INVALID_HANDLE)
727 vdp->decoder_destroy(vc->decoder);
728 switch (vc->image_format) {
729 case IMGFMT_VDPAU_MPEG1:
730 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
731 break;
732 case IMGFMT_VDPAU_MPEG2:
733 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
734 break;
735 case IMGFMT_VDPAU_H264:
736 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
737 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder "
738 "for %d reference frames.\n", max_refs);
739 break;
740 case IMGFMT_VDPAU_WMV3:
741 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
742 break;
743 case IMGFMT_VDPAU_VC1:
744 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
745 break;
746 case IMGFMT_VDPAU_MPEG4:
747 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
748 break;
749 default:
750 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Unknown image format!\n");
751 goto fail;
753 vdp_st = vdp->decoder_create(vc->vdp_device, vdp_decoder_profile,
754 vc->vid_width, vc->vid_height, max_refs,
755 &vc->decoder);
756 CHECK_ST_WARNING("Failed creating VDPAU decoder");
757 if (vdp_st != VDP_STATUS_OK) {
758 fail:
759 vc->decoder = VDP_INVALID_HANDLE;
760 vc->decoder_max_refs = 0;
761 return 0;
763 vc->decoder_max_refs = max_refs;
764 return 1;
767 static int initialize_vdpau_objects(struct vo *vo)
769 struct vdpctx *vc = vo->priv;
770 struct vdp_functions *vdp = vc->vdp;
771 VdpStatus vdp_st;
773 vc->vdp_chroma_type = VDP_CHROMA_TYPE_420;
774 switch (vc->image_format) {
775 case IMGFMT_YV12:
776 case IMGFMT_I420:
777 case IMGFMT_IYUV:
778 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YV12;
779 break;
780 case IMGFMT_NV12:
781 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_NV12;
782 break;
783 case IMGFMT_YUY2:
784 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV;
785 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
786 break;
787 case IMGFMT_UYVY:
788 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY;
789 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
791 if (win_x11_init_vdpau_flip_queue(vo) < 0)
792 return -1;
794 if (create_vdp_mixer(vo, vc->vdp_chroma_type) < 0)
795 return -1;
797 vdp_st = vdp->
798 bitmap_surface_query_capabilities(vc->vdp_device,
799 VDP_RGBA_FORMAT_A8,
800 &(VdpBool){0},
801 &vc->eosd_surface.max_width,
802 &vc->eosd_surface.max_height);
803 CHECK_ST_WARNING("Query to get max EOSD surface size failed");
804 forget_frames(vo);
805 resize(vo);
806 return 0;
809 static void mark_vdpau_objects_uninitialized(struct vo *vo)
811 struct vdpctx *vc = vo->priv;
813 vc->decoder = VDP_INVALID_HANDLE;
814 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
815 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
816 forget_frames(vo);
817 vc->video_mixer = VDP_INVALID_HANDLE;
818 vc->flip_queue = VDP_INVALID_HANDLE;
819 vc->flip_target = VDP_INVALID_HANDLE;
820 for (int i = 0; i <= MAX_OUTPUT_SURFACES; i++)
821 vc->output_surfaces[i] = VDP_INVALID_HANDLE;
822 vc->vdp_device = VDP_INVALID_HANDLE;
823 vc->eosd_surface = (struct eosd_bitmap_surface){
824 .surface = VDP_INVALID_HANDLE,
826 vc->output_surface_width = vc->output_surface_height = -1;
827 vc->eosd_render_count = 0;
830 static int handle_preemption(struct vo *vo)
832 struct vdpctx *vc = vo->priv;
834 if (!vc->is_preempted)
835 return 0;
836 if (!vc->preemption_acked)
837 mark_vdpau_objects_uninitialized(vo);
838 vc->preemption_acked = true;
839 if (!vc->preemption_user_notified) {
840 mp_tmsg(MSGT_VO, MSGL_ERR, "[vdpau] Got display preemption notice! "
841 "Will attempt to recover.\n");
842 vc->preemption_user_notified = true;
844 /* Trying to initialize seems to be quite slow, so only try once a
845 * second to avoid using 100% CPU. */
846 if (vc->last_preemption_retry_fail
847 && GetTimerMS() - vc->last_preemption_retry_fail < 1000)
848 return -1;
849 if (win_x11_init_vdpau_procs(vo) < 0 || initialize_vdpau_objects(vo) < 0) {
850 vc->last_preemption_retry_fail = GetTimerMS() | 1;
851 return -1;
853 vc->last_preemption_retry_fail = 0;
854 vc->is_preempted = false;
855 vc->preemption_user_notified = false;
856 mp_tmsg(MSGT_VO, MSGL_INFO, "[vdpau] Recovered from display preemption.\n");
857 return 1;
861 * connect to X server, create and map window, initialize all
862 * VDPAU objects, create different surfaces etc.
864 static int config(struct vo *vo, uint32_t width, uint32_t height,
865 uint32_t d_width, uint32_t d_height, uint32_t flags,
866 uint32_t format)
868 struct vdpctx *vc = vo->priv;
869 struct vo_x11_state *x11 = vo->x11;
870 XVisualInfo vinfo;
871 XSetWindowAttributes xswa;
872 XWindowAttributes attribs;
873 unsigned long xswamask;
874 int depth;
876 #ifdef CONFIG_XF86VM
877 int vm = flags & VOFLAG_MODESWITCHING;
878 #endif
880 if (handle_preemption(vo) < 0)
881 return -1;
883 vc->flip = flags & VOFLAG_FLIPPING;
884 vc->image_format = format;
885 vc->vid_width = width;
886 vc->vid_height = height;
887 vc->vid_d_width = d_width;
888 vc->vid_d_height = d_height;
890 free_video_specific(vo);
891 if (IMGFMT_IS_VDPAU(vc->image_format) && !create_vdp_decoder(vo, 2))
892 return -1;
894 #ifdef CONFIG_XF86VM
895 if (vm) {
896 vo_vm_switch(vo);
897 vc->mode_switched = true;
899 #endif
900 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
901 &attribs);
902 depth = attribs.depth;
903 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
904 depth = 24;
905 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
907 xswa.background_pixel = 0;
908 xswa.border_pixel = 0;
909 /* Do not use CWBackPixel: It leads to VDPAU errors after
910 * aspect ratio changes. */
911 xswamask = CWBorderPixel;
913 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, d_width, d_height,
914 flags, CopyFromParent, "vdpau");
915 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
917 #ifdef CONFIG_XF86VM
918 if (vm) {
919 /* Grab the mouse pointer in our window */
920 if (vo_grabpointer)
921 XGrabPointer(x11->display, x11->window, True, 0,
922 GrabModeAsync, GrabModeAsync,
923 x11->window, None, CurrentTime);
924 XSetInputFocus(x11->display, x11->window, RevertToNone, CurrentTime);
926 #endif
928 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
929 vo_fs = 1;
931 if (initialize_vdpau_objects(vo) < 0)
932 return -1;
934 return 0;
937 static void check_events(struct vo *vo)
939 if (handle_preemption(vo) < 0)
940 return;
942 int e = vo_x11_check_events(vo);
944 if (e & VO_EVENT_RESIZE)
945 resize(vo);
946 else if (e & VO_EVENT_EXPOSE) {
947 vo->want_redraw = true;
951 static void draw_osd_I8A8(void *ctx, int x0, int y0, int w, int h,
952 unsigned char *src, unsigned char *srca, int stride)
954 struct vo *vo = ctx;
955 struct vdpctx *vc = vo->priv;
956 struct vdp_functions *vdp = vc->vdp;
957 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
958 VdpStatus vdp_st;
959 int i;
960 int pitch;
961 int index_data_size_required;
962 VdpRect output_indexed_rect_vid;
964 if (!w || !h)
965 return;
967 index_data_size_required = 2*w*h;
968 if (vc->index_data_size < index_data_size_required) {
969 vc->index_data = talloc_realloc_size(vc, vc->index_data,
970 index_data_size_required);
971 vc->index_data_size = index_data_size_required;
974 // index_data creation, component order - I, A, I, A, .....
975 for (i = 0; i < h; i++)
976 for (int j = 0; j < w; j++) {
977 vc->index_data[i*2*w + j*2] = src [i*stride+j];
978 vc->index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
981 output_indexed_rect_vid.x0 = x0;
982 output_indexed_rect_vid.y0 = y0;
983 output_indexed_rect_vid.x1 = x0 + w;
984 output_indexed_rect_vid.y1 = y0 + h;
986 pitch = w*2;
988 // write source_data to osd_surface.
989 VdpOutputSurface osd_surface = vc->output_surfaces[vc->num_output_surfaces];
990 vdp_st = vdp->
991 output_surface_put_bits_indexed(osd_surface, VDP_INDEXED_FORMAT_I8A8,
992 (const void *const*)&vc->index_data,
993 &pitch, &output_indexed_rect_vid,
994 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
995 (void *)vc->palette);
996 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed");
998 VdpOutputSurfaceRenderBlendState blend_state = {
999 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1000 .blend_factor_source_color =
1001 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1002 .blend_factor_source_alpha =
1003 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1004 .blend_factor_destination_color =
1005 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1006 .blend_factor_destination_alpha =
1007 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1008 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1009 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1012 vdp_st = vdp->
1013 output_surface_render_output_surface(output_surface,
1014 &output_indexed_rect_vid,
1015 osd_surface,
1016 &output_indexed_rect_vid,
1017 NULL, &blend_state,
1018 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1019 CHECK_ST_WARNING("Error when calling "
1020 "vdp_output_surface_render_output_surface");
1023 static void draw_eosd(struct vo *vo)
1025 struct vdpctx *vc = vo->priv;
1026 struct vdp_functions *vdp = vc->vdp;
1027 VdpStatus vdp_st;
1028 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
1029 int i;
1031 if (handle_preemption(vo) < 0)
1032 return;
1034 VdpOutputSurfaceRenderBlendState blend_state = {
1035 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1036 .blend_factor_source_color =
1037 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1038 .blend_factor_source_alpha =
1039 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1040 .blend_factor_destination_color =
1041 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1042 .blend_factor_destination_alpha =
1043 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1044 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1045 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1048 for (i = 0; i < vc->eosd_render_count; i++) {
1049 vdp_st = vdp->
1050 output_surface_render_bitmap_surface(output_surface,
1051 &vc->eosd_targets[i].dest,
1052 vc->eosd_surface.surface,
1053 &vc->eosd_targets[i].source,
1054 &vc->eosd_targets[i].color,
1055 &blend_state,
1056 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1057 CHECK_ST_WARNING("EOSD: Error when rendering");
1061 #define HEIGHT_SORT_BITS 4
1062 static int size_index(struct eosd_target *r)
1064 unsigned int h = r->source.y1;
1065 int n = av_log2_16bit(h);
1066 return (n << HEIGHT_SORT_BITS)
1067 + (- 1 - (h << HEIGHT_SORT_BITS >> n) & (1 << HEIGHT_SORT_BITS) - 1);
1070 /* Pack the given rectangles into an area of size w * h.
1071 * The size of each rectangle is read from .source.x1/.source.y1.
1072 * The height of each rectangle must be at least 1 and less than 65536.
1073 * The .source rectangle is then set corresponding to the packed position.
1074 * 'scratch' must point to work memory for num_rects+16 ints.
1075 * Return 0 on success, -1 if the rectangles did not fit in w*h.
1077 * The rectangles are placed in rows in order approximately sorted by
1078 * height (the approximate sorting is simpler than a full one would be,
1079 * and allows the algorithm to work in linear time). Additionally, to
1080 * reduce wasted space when there are a few tall rectangles, empty
1081 * lower-right parts of rows are filled recursively when the size of
1082 * rectangles in the row drops past a power-of-two threshold. So if a
1083 * row starts with rectangles of size 3x50, 10x40 and 5x20 then the
1084 * free rectangle with corners (13, 20)-(w, 50) is filled recursively.
1086 static int pack_rectangles(struct eosd_target *rects, int num_rects,
1087 int w, int h, int *scratch)
1089 int bins[16 << HEIGHT_SORT_BITS];
1090 int sizes[16 << HEIGHT_SORT_BITS] = {};
1091 for (int i = 0; i < num_rects; i++)
1092 sizes[size_index(rects + i)]++;
1093 int idx = 0;
1094 for (int i = 0; i < 16 << HEIGHT_SORT_BITS; i += 1 << HEIGHT_SORT_BITS) {
1095 for (int j = 0; j < 1 << HEIGHT_SORT_BITS; j++) {
1096 bins[i + j] = idx;
1097 idx += sizes[i + j];
1099 scratch[idx++] = -1;
1101 for (int i = 0; i < num_rects; i++)
1102 scratch[bins[size_index(rects + i)]++] = i;
1103 for (int i = 0; i < 16; i++)
1104 bins[i] = bins[i << HEIGHT_SORT_BITS] - sizes[i << HEIGHT_SORT_BITS];
1105 struct {
1106 int size, x, bottom;
1107 } stack[16] = {{15, 0, h}}, s = {};
1108 int stackpos = 1;
1109 int y;
1110 while (stackpos) {
1111 y = s.bottom;
1112 s = stack[--stackpos];
1113 s.size++;
1114 while (s.size--) {
1115 int maxy = -1;
1116 int obj;
1117 while ((obj = scratch[bins[s.size]]) >= 0) {
1118 int bottom = y + rects[obj].source.y1;
1119 if (bottom > s.bottom)
1120 break;
1121 int right = s.x + rects[obj].source.x1;
1122 if (right > w)
1123 break;
1124 bins[s.size]++;
1125 rects[obj].source.x0 = s.x;
1126 rects[obj].source.x1 += s.x;
1127 rects[obj].source.y0 = y;
1128 rects[obj].source.y1 += y;
1129 num_rects--;
1130 if (maxy <= 0)
1131 stack[stackpos++] = s;
1132 s.x = right;
1133 maxy = FFMAX(maxy, bottom);
1135 if (maxy > 0)
1136 s.bottom = maxy;
1139 return num_rects ? -1 : 0;
1142 static void generate_eosd(struct vo *vo, mp_eosd_images_t *imgs)
1144 struct vdpctx *vc = vo->priv;
1145 struct vdp_functions *vdp = vc->vdp;
1146 VdpStatus vdp_st;
1147 int i;
1148 ASS_Image *img = imgs->imgs;
1149 ASS_Image *p;
1150 struct eosd_bitmap_surface *sfc = &vc->eosd_surface;
1151 bool need_upload = false;
1153 if (imgs->changed == 0)
1154 return; // Nothing changed, no need to redraw
1156 vc->eosd_render_count = 0;
1158 if (!img)
1159 return; // There's nothing to render!
1161 if (imgs->changed == 1)
1162 goto eosd_skip_upload;
1164 need_upload = true;
1165 bool reallocate = false;
1166 while (1) {
1167 for (p = img, i = 0; p; p = p->next) {
1168 if (p->w <= 0 || p->h <= 0)
1169 continue;
1170 // Allocate new space for surface/target arrays
1171 if (i >= vc->eosd_targets_size) {
1172 vc->eosd_targets_size = FFMAX(vc->eosd_targets_size * 2, 512);
1173 vc->eosd_targets =
1174 talloc_realloc_size(vc, vc->eosd_targets,
1175 vc->eosd_targets_size
1176 * sizeof(*vc->eosd_targets));
1177 vc->eosd_scratch =
1178 talloc_realloc_size(vc, vc->eosd_scratch,
1179 (vc->eosd_targets_size + 16)
1180 * sizeof(*vc->eosd_scratch));
1182 vc->eosd_targets[i].source.x1 = p->w;
1183 vc->eosd_targets[i].source.y1 = p->h;
1184 i++;
1186 if (pack_rectangles(vc->eosd_targets, i, sfc->w, sfc->h,
1187 vc->eosd_scratch) >= 0)
1188 break;
1189 int w = FFMIN(FFMAX(sfc->w * 2, EOSD_SURFACE_INITIAL_SIZE),
1190 sfc->max_width);
1191 int h = FFMIN(FFMAX(sfc->h * 2, EOSD_SURFACE_INITIAL_SIZE),
1192 sfc->max_height);
1193 if (w == sfc->w && h == sfc->h) {
1194 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] EOSD bitmaps do not fit on "
1195 "a surface with the maximum supported size\n");
1196 return;
1197 } else {
1198 sfc->w = w;
1199 sfc->h = h;
1201 reallocate = true;
1203 if (reallocate) {
1204 if (sfc->surface != VDP_INVALID_HANDLE) {
1205 vdp_st = vdp->bitmap_surface_destroy(sfc->surface);
1206 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1208 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Allocating a %dx%d surface for "
1209 "EOSD bitmaps.\n", sfc->w, sfc->h);
1210 vdp_st = vdp->bitmap_surface_create(vc->vdp_device, VDP_RGBA_FORMAT_A8,
1211 sfc->w, sfc->h, true,
1212 &sfc->surface);
1213 if (vdp_st != VDP_STATUS_OK)
1214 sfc->surface = VDP_INVALID_HANDLE;
1215 CHECK_ST_WARNING("EOSD: error when creating surface");
1218 eosd_skip_upload:
1219 if (sfc->surface == VDP_INVALID_HANDLE)
1220 return;
1221 for (p = img; p; p = p->next) {
1222 if (p->w <= 0 || p->h <= 0)
1223 continue;
1224 struct eosd_target *target = &vc->eosd_targets[vc->eosd_render_count];
1225 if (need_upload) {
1226 vdp_st = vdp->
1227 bitmap_surface_put_bits_native(sfc->surface,
1228 (const void *) &p->bitmap,
1229 &p->stride, &target->source);
1230 CHECK_ST_WARNING("EOSD: putbits failed");
1232 // Render dest, color, etc.
1233 target->color.alpha = 1.0 - ((p->color >> 0) & 0xff) / 255.0;
1234 target->color.blue = ((p->color >> 8) & 0xff) / 255.0;
1235 target->color.green = ((p->color >> 16) & 0xff) / 255.0;
1236 target->color.red = ((p->color >> 24) & 0xff) / 255.0;
1237 target->dest.x0 = p->dst_x;
1238 target->dest.y0 = p->dst_y;
1239 target->dest.x1 = p->w + p->dst_x;
1240 target->dest.y1 = p->h + p->dst_y;
1241 vc->eosd_render_count++;
1245 static void draw_osd(struct vo *vo, struct osd_state *osd)
1247 struct vdpctx *vc = vo->priv;
1249 if (handle_preemption(vo) < 0)
1250 return;
1252 osd_draw_text_ext(osd, vo->dwidth, vo->dheight, vc->border_x, vc->border_y,
1253 vc->border_x, vc->border_y, vc->vid_width,
1254 vc->vid_height, draw_osd_I8A8, vo);
1257 static int update_presentation_queue_status(struct vo *vo)
1259 struct vdpctx *vc = vo->priv;
1260 struct vdp_functions *vdp = vc->vdp;
1261 VdpStatus vdp_st;
1263 while (vc->query_surface_num != vc->surface_num) {
1264 VdpTime vtime;
1265 VdpPresentationQueueStatus status;
1266 VdpOutputSurface surface = vc->output_surfaces[vc->query_surface_num];
1267 vdp_st = vdp->presentation_queue_query_surface_status(vc->flip_queue,
1268 surface,
1269 &status, &vtime);
1270 CHECK_ST_WARNING("Error calling "
1271 "presentation_queue_query_surface_status");
1272 if (status == VDP_PRESENTATION_QUEUE_STATUS_QUEUED)
1273 break;
1274 if (vc->vsync_interval > 1) {
1275 uint64_t qtime = vc->queue_time[vc->query_surface_num];
1276 if (vtime < qtime + vc->vsync_interval / 2)
1277 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown too early\n");
1278 if (vtime > qtime + vc->vsync_interval)
1279 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown late\n");
1281 vc->query_surface_num = WRAP_ADD(vc->query_surface_num, 1,
1282 vc->num_output_surfaces);
1283 vc->recent_vsync_time = vtime;
1285 int num_queued = WRAP_ADD(vc->surface_num, -vc->query_surface_num,
1286 vc->num_output_surfaces);
1287 mp_msg(MSGT_VO, MSGL_DBG3, "[vdpau] Queued surface count (before add): "
1288 "%d\n", num_queued);
1289 return num_queued;
1292 static inline uint64_t prev_vs2(struct vdpctx *vc, uint64_t ts, int shift)
1294 uint64_t offset = ts - vc->recent_vsync_time;
1295 // Fix negative values for 1<<shift vsyncs before vc->recent_vsync_time
1296 offset += (uint64_t)vc->vsync_interval << shift;
1297 offset %= vc->vsync_interval;
1298 return ts - offset;
1301 static void flip_page_timed(struct vo *vo, unsigned int pts_us, int duration)
1303 struct vdpctx *vc = vo->priv;
1304 struct vdp_functions *vdp = vc->vdp;
1305 VdpStatus vdp_st;
1306 uint32_t vsync_interval = vc->vsync_interval;
1308 if (handle_preemption(vo) < 0)
1309 return;
1311 if (duration > INT_MAX / 1000)
1312 duration = -1;
1313 else
1314 duration *= 1000;
1316 if (vc->user_fps < 0)
1317 duration = -1; // Make sure drop logic is disabled
1319 uint64_t now = sync_vdptime(vo);
1320 uint64_t pts = pts_us ? convert_to_vdptime(vo, pts_us) : now;
1321 uint64_t ideal_pts = pts;
1322 uint64_t npts = duration >= 0 ? pts + duration : UINT64_MAX;
1324 #define PREV_VS2(ts, shift) prev_vs2(vc, ts, shift)
1325 // Only gives accurate results for ts >= vc->recent_vsync_time
1326 #define PREV_VSYNC(ts) PREV_VS2(ts, 0)
1328 /* We hope to be here at least one vsync before the frame should be shown.
1329 * If we are running late then don't drop the frame unless there is
1330 * already one queued for the next vsync; even if we _hope_ to show the
1331 * next frame soon enough to mean this one should be dropped we might
1332 * not make the target time in reality. Without this check we could drop
1333 * every frame, freezing the display completely if video lags behind.
1335 if (now > PREV_VSYNC(FFMAX(pts, vc->last_queue_time + vsync_interval)))
1336 npts = UINT64_MAX;
1338 /* Allow flipping a frame at a vsync if its presentation time is a
1339 * bit after that vsync and the change makes the flip time delta
1340 * from previous frame better match the target timestamp delta.
1341 * This avoids instability with frame timestamps falling near vsyncs.
1342 * For example if the frame timestamps were (with vsyncs at
1343 * integer values) 0.01, 1.99, 4.01, 5.99, 8.01, ... then
1344 * straightforward timing at next vsync would flip the frames at
1345 * 1, 2, 5, 6, 9; this changes it to 1, 2, 4, 6, 8 and so on with
1346 * regular 2-vsync intervals.
1348 * Also allow moving the frame forward if it looks like we dropped
1349 * the previous frame incorrectly (now that we know better after
1350 * having final exact timestamp information for this frame) and
1351 * there would unnecessarily be a vsync without a frame change.
1353 uint64_t vsync = PREV_VSYNC(pts);
1354 if (pts < vsync + vsync_interval / 4
1355 && (vsync - PREV_VS2(vc->last_queue_time, 16)
1356 > pts - vc->last_ideal_time + vsync_interval / 2
1357 || vc->dropped_frame && vsync > vc->dropped_time))
1358 pts -= vsync_interval / 2;
1360 vc->dropped_frame = true; // changed at end if false
1361 vc->dropped_time = ideal_pts;
1363 pts = FFMAX(pts, vc->last_queue_time + vsync_interval);
1364 pts = FFMAX(pts, now);
1365 if (npts < PREV_VSYNC(pts) + vsync_interval)
1366 return;
1368 int num_flips = update_presentation_queue_status(vo);
1369 vsync = vc->recent_vsync_time + num_flips * vc->vsync_interval;
1370 now = sync_vdptime(vo);
1371 pts = FFMAX(pts, now);
1372 pts = FFMAX(pts, vsync + (vsync_interval >> 2));
1373 vsync = PREV_VSYNC(pts);
1374 if (npts < vsync + vsync_interval)
1375 return;
1376 pts = vsync + (vsync_interval >> 2);
1377 vdp_st =
1378 vdp->presentation_queue_display(vc->flip_queue,
1379 vc->output_surfaces[vc->surface_num],
1380 vo->dwidth, vo->dheight, pts);
1381 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display");
1383 vc->last_queue_time = pts;
1384 vc->queue_time[vc->surface_num] = pts;
1385 vc->last_ideal_time = ideal_pts;
1386 vc->dropped_frame = false;
1387 vc->surface_num = WRAP_ADD(vc->surface_num, 1, vc->num_output_surfaces);
1390 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
1391 int h, int x, int y)
1393 struct vdpctx *vc = vo->priv;
1394 struct vdp_functions *vdp = vc->vdp;
1395 VdpStatus vdp_st;
1397 if (handle_preemption(vo) < 0)
1398 return VO_TRUE;
1400 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
1401 int max_refs = vc->image_format == IMGFMT_VDPAU_H264 ?
1402 rndr->info.h264.num_ref_frames : 2;
1403 if (!IMGFMT_IS_VDPAU(vc->image_format))
1404 return VO_FALSE;
1405 if ((vc->decoder == VDP_INVALID_HANDLE || vc->decoder_max_refs < max_refs)
1406 && !create_vdp_decoder(vo, max_refs))
1407 return VO_FALSE;
1409 vdp_st = vdp->decoder_render(vc->decoder, rndr->surface,
1410 (void *)&rndr->info,
1411 rndr->bitstream_buffers_used,
1412 rndr->bitstream_buffers);
1413 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
1414 return VO_TRUE;
1418 static struct vdpau_render_state *get_surface(struct vo *vo, int number)
1420 struct vdpctx *vc = vo->priv;
1421 struct vdp_functions *vdp = vc->vdp;
1423 if (number > MAX_VIDEO_SURFACES)
1424 return NULL;
1425 if (vc->surface_render[number].surface == VDP_INVALID_HANDLE
1426 && !vc->is_preempted) {
1427 VdpStatus vdp_st;
1428 vdp_st = vdp->video_surface_create(vc->vdp_device, vc->vdp_chroma_type,
1429 vc->vid_width, vc->vid_height,
1430 &vc->surface_render[number].surface);
1431 CHECK_ST_WARNING("Error when calling vdp_video_surface_create");
1433 mp_msg(MSGT_VO, MSGL_DBG3, "vdpau vid create: %u\n",
1434 vc->surface_render[number].surface);
1435 return &vc->surface_render[number];
1438 static void draw_image(struct vo *vo, mp_image_t *mpi, double pts)
1440 struct vdpctx *vc = vo->priv;
1441 struct vdp_functions *vdp = vc->vdp;
1442 struct mp_image *reserved_mpi = NULL;
1443 struct vdpau_render_state *rndr;
1445 if (vc->is_preempted) {
1446 vo->frame_loaded = true;
1447 return;
1450 if (IMGFMT_IS_VDPAU(vc->image_format)) {
1451 rndr = mpi->priv;
1452 reserved_mpi = mpi;
1453 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1454 VdpStatus vdp_st;
1455 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
1456 rndr = get_surface(vo, vc->deint_counter);
1457 vc->deint_counter = WRAP_ADD(vc->deint_counter, 1, NUM_BUFFERED_VIDEO);
1458 if (vc->image_format == IMGFMT_NV12)
1459 destdata[1] = destdata[2];
1460 vdp_st =
1461 vdp->video_surface_put_bits_y_cb_cr(rndr->surface,
1462 vc->vdp_pixel_format,
1463 (const void *const*)destdata,
1464 mpi->stride); // pitch
1465 CHECK_ST_WARNING("Error when calling "
1466 "vdp_video_surface_put_bits_y_cb_cr");
1467 } else
1468 // We don't support slice callbacks so this shouldn't occur -
1469 // I think the flags test above in pointless, but I'm adding
1470 // this instead of removing it just in case.
1471 abort();
1472 if (mpi->fields & MP_IMGFIELD_ORDERED)
1473 vc->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1474 else
1475 vc->top_field_first = 1;
1477 add_new_video_surface(vo, rndr->surface, reserved_mpi, pts);
1479 return;
1482 // warning: the size and pixel format of surface must match that of the
1483 // surfaces in vc->output_surfaces
1484 static struct mp_image *read_output_surface(struct vdpctx *vc,
1485 VdpOutputSurface surface)
1487 VdpStatus vdp_st;
1488 struct vdp_functions *vdp = vc->vdp;
1489 struct mp_image *image = alloc_mpi(vc->output_surface_width,
1490 vc->output_surface_height, IMGFMT_BGR32);
1492 void *dst_planes[] = { image->planes[0] };
1493 uint32_t dst_pitches[] = { image->stride[0] };
1494 vdp_st = vdp->output_surface_get_bits_native(surface, NULL, dst_planes,
1495 dst_pitches);
1496 CHECK_ST_WARNING("Error when calling vdp_output_surface_get_bits_native");
1498 return image;
1501 static struct mp_image *get_screenshot(struct vo *vo)
1503 struct vdpctx *vc = vo->priv;
1505 VdpOutputSurface screenshot_surface =
1506 vc->output_surfaces[vc->num_output_surfaces];
1508 VdpRect rc = { .x1 = vc->vid_width, .y1 = vc->vid_height };
1509 render_video_to_output_surface(vo, screenshot_surface, &rc);
1511 struct mp_image *image = read_output_surface(vc, screenshot_surface);
1513 image->width = vc->vid_width;
1514 image->height = vc->vid_height;
1515 image->w = vc->vid_d_width;
1516 image->h = vc->vid_d_height;
1518 return image;
1521 static struct mp_image *get_window_screenshot(struct vo *vo)
1523 struct vdpctx *vc = vo->priv;
1524 int last_surface = WRAP_ADD(vc->surface_num, -1, vc->num_output_surfaces);
1525 VdpOutputSurface screen = vc->output_surfaces[last_surface];
1526 struct mp_image *image = read_output_surface(vo->priv, screen);
1527 image->width = image->w = vo->dwidth;
1528 image->height = image->h = vo->dheight;
1529 return image;
1532 static uint32_t get_image(struct vo *vo, mp_image_t *mpi)
1534 struct vdpctx *vc = vo->priv;
1535 struct vdpau_render_state *rndr;
1537 // no dr for non-decoding for now
1538 if (!IMGFMT_IS_VDPAU(vc->image_format))
1539 return VO_FALSE;
1540 if (mpi->type != MP_IMGTYPE_NUMBERED)
1541 return VO_FALSE;
1543 rndr = get_surface(vo, mpi->number);
1544 if (!rndr) {
1545 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in "
1546 "get_image\n");
1547 // TODO: this probably breaks things forever, provide a dummy buffer?
1548 return VO_FALSE;
1550 mpi->flags |= MP_IMGFLAG_DIRECT;
1551 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1552 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1553 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1554 mpi->planes[0] = (void *)rndr;
1555 mpi->num_planes = 1;
1556 mpi->priv = rndr;
1557 return VO_TRUE;
1560 static int query_format(uint32_t format)
1562 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
1563 | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD
1564 | VFCAP_EOSD_UNSCALED | VFCAP_FLIP;
1565 switch (format) {
1566 case IMGFMT_YV12:
1567 case IMGFMT_I420:
1568 case IMGFMT_IYUV:
1569 case IMGFMT_NV12:
1570 case IMGFMT_YUY2:
1571 case IMGFMT_UYVY:
1572 return default_flags | VOCAP_NOSLICES;
1573 case IMGFMT_VDPAU_MPEG1:
1574 case IMGFMT_VDPAU_MPEG2:
1575 case IMGFMT_VDPAU_H264:
1576 case IMGFMT_VDPAU_WMV3:
1577 case IMGFMT_VDPAU_VC1:
1578 case IMGFMT_VDPAU_MPEG4:
1579 return default_flags;
1581 return 0;
1584 static void destroy_vdpau_objects(struct vo *vo)
1586 struct vdpctx *vc = vo->priv;
1587 struct vdp_functions *vdp = vc->vdp;
1589 int i;
1590 VdpStatus vdp_st;
1592 free_video_specific(vo);
1594 if (vc->flip_queue != VDP_INVALID_HANDLE) {
1595 vdp_st = vdp->presentation_queue_destroy(vc->flip_queue);
1596 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy");
1599 if (vc->flip_target != VDP_INVALID_HANDLE) {
1600 vdp_st = vdp->presentation_queue_target_destroy(vc->flip_target);
1601 CHECK_ST_WARNING("Error when calling "
1602 "vdp_presentation_queue_target_destroy");
1605 for (i = 0; i <= vc->num_output_surfaces; i++) {
1606 if (vc->output_surfaces[i] == VDP_INVALID_HANDLE)
1607 continue;
1608 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
1609 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy");
1612 if (vc->eosd_surface.surface != VDP_INVALID_HANDLE) {
1613 vdp_st = vdp->bitmap_surface_destroy(vc->eosd_surface.surface);
1614 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1617 vdp_st = vdp->device_destroy(vc->vdp_device);
1618 CHECK_ST_WARNING("Error when calling vdp_device_destroy");
1621 static void uninit(struct vo *vo)
1623 struct vdpctx *vc = vo->priv;
1625 /* Destroy all vdpau objects */
1626 destroy_vdpau_objects(vo);
1628 #ifdef CONFIG_XF86VM
1629 if (vc->mode_switched)
1630 vo_vm_close(vo);
1631 #endif
1632 vo_x11_uninit(vo);
1634 // Free bitstream buffers allocated by FFmpeg
1635 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
1636 av_freep(&vc->surface_render[i].bitstream_buffers);
1639 static int preinit(struct vo *vo, const char *arg)
1641 int i;
1642 int user_colorspace = 0;
1643 int studio_levels = 0;
1645 struct vdpctx *vc = talloc_zero(vo, struct vdpctx);
1646 vo->priv = vc;
1648 // Mark everything as invalid first so uninit() can tell what has been
1649 // allocated
1650 mark_vdpau_objects_uninitialized(vo);
1652 vc->colorspace = (struct mp_csp_details) MP_CSP_DETAILS_DEFAULTS;
1653 vc->deint_type = 3;
1654 vc->chroma_deint = 1;
1655 vc->flip_offset_window = 50;
1656 vc->flip_offset_fs = 50;
1657 vc->num_output_surfaces = 3;
1658 vc->video_eq.capabilities = MP_CSP_EQ_CAPS_COLORMATRIX;
1659 const opt_t subopts[] = {
1660 {"deint", OPT_ARG_INT, &vc->deint, NULL},
1661 {"chroma-deint", OPT_ARG_BOOL, &vc->chroma_deint, NULL},
1662 {"pullup", OPT_ARG_BOOL, &vc->pullup, NULL},
1663 {"denoise", OPT_ARG_FLOAT, &vc->denoise, NULL},
1664 {"sharpen", OPT_ARG_FLOAT, &vc->sharpen, NULL},
1665 {"colorspace", OPT_ARG_INT, &user_colorspace, NULL},
1666 {"studio", OPT_ARG_BOOL, &studio_levels, NULL},
1667 {"hqscaling", OPT_ARG_INT, &vc->hqscaling, NULL},
1668 {"fps", OPT_ARG_FLOAT, &vc->user_fps, NULL},
1669 {"queuetime_windowed", OPT_ARG_INT, &vc->flip_offset_window, NULL},
1670 {"queuetime_fs", OPT_ARG_INT, &vc->flip_offset_fs, NULL},
1671 {"output_surfaces", OPT_ARG_INT, &vc->num_output_surfaces, NULL},
1672 {NULL}
1674 if (subopt_parse(arg, subopts) != 0) {
1675 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Could not parse suboptions.\n");
1676 return -1;
1678 if (vc->hqscaling < 0 || vc->hqscaling > 9) {
1679 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid value for suboption "
1680 "hqscaling\n");
1681 return -1;
1683 if (vc->num_output_surfaces < 2) {
1684 mp_msg(MSGT_VO, MSGL_FATAL, "[vdpau] Invalid suboption "
1685 "output_surfaces: can't use less than 2 surfaces\n");
1686 return -1;
1688 if (user_colorspace != 0 || studio_levels != 0) {
1689 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] \"colorspace\" and \"studio\""
1690 " suboptions have been removed. Use options --colormatrix and"
1691 " --colormatrix-output-range=limited instead.\n");
1692 return -1;
1694 if (vc->num_output_surfaces > MAX_OUTPUT_SURFACES) {
1695 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Number of output surfaces "
1696 "is limited to %d.\n", MAX_OUTPUT_SURFACES);
1697 vc->num_output_surfaces = MAX_OUTPUT_SURFACES;
1699 if (vc->deint)
1700 vc->deint_type = FFABS(vc->deint);
1701 if (vc->deint < 0)
1702 vc->deint = 0;
1704 if (!vo_init(vo))
1705 return -1;
1707 // After this calling uninit() should work to free resources
1709 if (win_x11_init_vdpau_procs(vo) < 0) {
1710 if (vc->vdp->device_destroy)
1711 vc->vdp->device_destroy(vc->vdp_device);
1712 vo_x11_uninit(vo);
1713 return -1;
1716 // full grayscale palette.
1717 for (i = 0; i < PALETTE_SIZE; ++i)
1718 vc->palette[i] = (i << 16) | (i << 8) | i;
1720 return 0;
1723 static int get_equalizer(struct vo *vo, const char *name, int *value)
1725 struct vdpctx *vc = vo->priv;
1726 return mp_csp_equalizer_get(&vc->video_eq, name, value) >= 0 ?
1727 VO_TRUE : VO_NOTIMPL;
1730 static bool status_ok(struct vo *vo)
1732 if (!vo->config_ok || handle_preemption(vo) < 0)
1733 return false;
1734 return true;
1737 static int set_equalizer(struct vo *vo, const char *name, int value)
1739 struct vdpctx *vc = vo->priv;
1741 if (mp_csp_equalizer_set(&vc->video_eq, name, value) < 0)
1742 return VO_NOTIMPL;
1744 if (status_ok(vo))
1745 update_csc_matrix(vo);
1746 return true;
1749 static void checked_resize(struct vo *vo)
1751 if (!status_ok(vo))
1752 return;
1753 resize(vo);
1756 static int control(struct vo *vo, uint32_t request, void *data)
1758 struct vdpctx *vc = vo->priv;
1759 struct vdp_functions *vdp = vc->vdp;
1761 handle_preemption(vo);
1763 switch (request) {
1764 case VOCTRL_GET_DEINTERLACE:
1765 *(int *)data = vc->deint;
1766 return VO_TRUE;
1767 case VOCTRL_SET_DEINTERLACE:
1768 vc->deint = *(int *)data;
1769 if (vc->deint)
1770 vc->deint = vc->deint_type;
1771 if (vc->deint_type > 2 && status_ok(vo)) {
1772 VdpStatus vdp_st;
1773 VdpVideoMixerFeature features[1] =
1774 {vc->deint_type == 3 ?
1775 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1776 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1777 VdpBool feature_enables[1] = {vc->deint ? VDP_TRUE : VDP_FALSE};
1778 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
1779 1, features,
1780 feature_enables);
1781 CHECK_ST_WARNING("Error changing deinterlacing settings");
1783 vo->want_redraw = true;
1784 return VO_TRUE;
1785 case VOCTRL_PAUSE:
1786 if (vc->dropped_frame)
1787 flip_page_timed(vo, 0, -1);
1788 return true;
1789 case VOCTRL_QUERY_FORMAT:
1790 return query_format(*(uint32_t *)data);
1791 case VOCTRL_GET_IMAGE:
1792 return get_image(vo, data);
1793 case VOCTRL_DRAW_IMAGE:
1794 abort(); // draw_image() should get called directly
1795 case VOCTRL_BORDER:
1796 vo_x11_border(vo);
1797 checked_resize(vo);
1798 return VO_TRUE;
1799 case VOCTRL_FULLSCREEN:
1800 vo_x11_fullscreen(vo);
1801 checked_resize(vo);
1802 return VO_TRUE;
1803 case VOCTRL_GET_PANSCAN:
1804 return VO_TRUE;
1805 case VOCTRL_SET_PANSCAN:
1806 checked_resize(vo);
1807 return VO_TRUE;
1808 case VOCTRL_SET_EQUALIZER: {
1809 vo->want_redraw = true;
1810 struct voctrl_set_equalizer_args *args = data;
1811 return set_equalizer(vo, args->name, args->value);
1813 case VOCTRL_GET_EQUALIZER: {
1814 struct voctrl_get_equalizer_args *args = data;
1815 return get_equalizer(vo, args->name, args->valueptr);
1817 case VOCTRL_SET_YUV_COLORSPACE:
1818 vc->colorspace = *(struct mp_csp_details *)data;
1819 if (status_ok(vo))
1820 update_csc_matrix(vo);
1821 vo->want_redraw = true;
1822 return true;
1823 case VOCTRL_GET_YUV_COLORSPACE:
1824 *(struct mp_csp_details *)data = vc->colorspace;
1825 return true;
1826 case VOCTRL_ONTOP:
1827 vo_x11_ontop(vo);
1828 return VO_TRUE;
1829 case VOCTRL_UPDATE_SCREENINFO:
1830 update_xinerama_info(vo);
1831 return VO_TRUE;
1832 case VOCTRL_DRAW_EOSD:
1833 if (!data)
1834 return VO_FALSE;
1835 generate_eosd(vo, data);
1836 draw_eosd(vo);
1837 return VO_TRUE;
1838 case VOCTRL_GET_EOSD_RES: {
1839 struct mp_eosd_res *r = data;
1840 r->w = vo->dwidth;
1841 r->h = vo->dheight;
1842 r->ml = r->mr = vc->border_x;
1843 r->mt = r->mb = vc->border_y;
1844 return VO_TRUE;
1846 case VOCTRL_NEWFRAME:
1847 vc->deint_queue_pos = next_deint_queue_pos(vo, true);
1848 video_to_output_surface(vo);
1849 return true;
1850 case VOCTRL_SKIPFRAME:
1851 vc->deint_queue_pos = next_deint_queue_pos(vo, true);
1852 return true;
1853 case VOCTRL_REDRAW_FRAME:
1854 video_to_output_surface(vo);
1855 return true;
1856 case VOCTRL_RESET:
1857 forget_frames(vo);
1858 return true;
1859 case VOCTRL_SCREENSHOT: {
1860 struct voctrl_screenshot_args *args = data;
1861 if (args->full_window)
1862 args->out_image = get_window_screenshot(vo);
1863 else
1864 args->out_image = get_screenshot(vo);
1865 return true;
1868 return VO_NOTIMPL;
1871 const struct vo_driver video_out_vdpau = {
1872 .is_new = true,
1873 .buffer_frames = true,
1874 .info = &(const struct vo_info_s){
1875 "VDPAU with X11",
1876 "vdpau",
1877 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
1880 .preinit = preinit,
1881 .config = config,
1882 .control = control,
1883 .draw_image = draw_image,
1884 .get_buffered_frame = set_next_frame_info,
1885 .draw_slice = draw_slice,
1886 .draw_osd = draw_osd,
1887 .flip_page_timed = flip_page_timed,
1888 .check_events = check_events,
1889 .uninit = uninit,