osd: rewrite OSD rendering to use libass
[mplayer.git] / libvo / vo_vdpau.c
blob407ecdff3c0ede451d32a7e1aeb6d88f3e125bae
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 "m_option.h"
47 #include "libmpcodecs/vfcap.h"
48 #include "libmpcodecs/mp_image.h"
49 #include "osdep/timer.h"
51 #include "libavcodec/vdpau.h"
53 #include "libavutil/common.h"
54 #include "libavutil/mathematics.h"
56 #include "sub/ass_mp.h"
58 #define WRAP_ADD(x, a, m) ((a) < 0 \
59 ? ((x)+(a)+(m) < (m) ? (x)+(a)+(m) : (x)+(a)) \
60 : ((x)+(a) < (m) ? (x)+(a) : (x)+(a)-(m)))
62 #define CHECK_ST_ERROR(message) \
63 do { \
64 if (vdp_st != VDP_STATUS_OK) { \
65 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
66 message, vdp->get_error_string(vdp_st)); \
67 return -1; \
68 } \
69 } while (0)
71 #define CHECK_ST_WARNING(message) \
72 do { \
73 if (vdp_st != VDP_STATUS_OK) \
74 mp_msg(MSGT_VO, MSGL_WARN, "[ vdpau] %s: %s\n", \
75 message, vdp->get_error_string(vdp_st)); \
76 } while (0)
78 /* number of video and output surfaces */
79 #define MAX_OUTPUT_SURFACES 15
80 #define MAX_VIDEO_SURFACES 50
81 #define NUM_BUFFERED_VIDEO 5
83 /* number of palette entries */
84 #define PALETTE_SIZE 256
86 /* Initial size of EOSD surface in pixels (x*x) */
87 #define EOSD_SURFACE_INITIAL_SIZE 256
89 /* Pixelformat used for output surfaces */
90 #define OUTPUT_RGBA_FORMAT VDP_RGBA_FORMAT_B8G8R8A8
93 * Global variable declaration - VDPAU specific
96 struct vdp_functions {
97 #define VDP_FUNCTION(vdp_type, _, mp_name) vdp_type *mp_name;
98 #include "vdpau_template.c"
99 #undef VDP_FUNCTION
102 struct vdpctx {
103 struct vdp_functions *vdp;
105 VdpDevice vdp_device;
106 bool is_preempted;
107 bool preemption_acked;
108 bool preemption_user_notified;
109 unsigned int last_preemption_retry_fail;
110 VdpGetProcAddress *vdp_get_proc_address;
112 VdpPresentationQueueTarget flip_target;
113 VdpPresentationQueue flip_queue;
114 uint64_t last_vdp_time;
115 unsigned int last_sync_update;
117 /* an extra last output surface is used for OSD and screenshots */
118 VdpOutputSurface output_surfaces[MAX_OUTPUT_SURFACES + 1];
119 int num_output_surfaces;
120 struct buffered_video_surface {
121 VdpVideoSurface surface;
122 double pts;
123 mp_image_t *mpi;
124 } buffered_video[NUM_BUFFERED_VIDEO];
125 int deint_queue_pos;
126 int output_surface_width, output_surface_height;
128 VdpVideoMixer video_mixer;
129 struct mp_csp_details colorspace;
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 int composite_detect;
156 unsigned int vsync_interval;
157 uint64_t last_queue_time;
158 uint64_t queue_time[MAX_OUTPUT_SURFACES];
159 uint64_t last_ideal_time;
160 bool dropped_frame;
161 uint64_t dropped_time;
162 uint32_t vid_width, vid_height;
163 uint32_t vid_d_width, vid_d_height;
164 uint32_t image_format;
165 VdpChromaType vdp_chroma_type;
166 VdpYCbCrFormat vdp_pixel_format;
168 /* draw_osd */
169 unsigned char *index_data;
170 int index_data_size;
171 uint32_t palette[PALETTE_SIZE];
173 // EOSD
174 // Pool of surfaces
175 struct eosd_bitmap_surface {
176 VdpBitmapSurface surface;
177 int w;
178 int h;
179 uint32_t max_width;
180 uint32_t max_height;
181 } eosd_surface;
183 // List of surfaces to be rendered
184 struct eosd_target {
185 VdpRect source;
186 VdpRect dest;
187 VdpColor color;
188 } *eosd_targets;
189 int eosd_targets_size;
190 int *eosd_scratch;
192 int eosd_render_count;
194 // Video equalizer
195 struct mp_csp_equalizer video_eq;
197 // These tell what's been initialized and uninit() should free/uninitialize
198 bool mode_switched;
201 static int change_vdptime_sync(struct vdpctx *vc, unsigned int *t)
203 struct vdp_functions *vdp = vc->vdp;
204 VdpStatus vdp_st;
205 VdpTime vdp_time;
206 vdp_st = vdp->presentation_queue_get_time(vc->flip_queue, &vdp_time);
207 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_get_time");
208 unsigned int t1 = *t;
209 unsigned int t2 = GetTimer();
210 uint64_t old = vc->last_vdp_time + (t1 - vc->last_sync_update) * 1000ULL;
211 if (vdp_time > old)
212 if (vdp_time > old + (t2 - t1) * 1000ULL)
213 vdp_time -= (t2 - t1) * 1000ULL;
214 else
215 vdp_time = old;
216 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] adjusting VdpTime offset by %f µs\n",
217 (int64_t)(vdp_time - old) / 1000.);
218 vc->last_vdp_time = vdp_time;
219 vc->last_sync_update = t1;
220 *t = t2;
221 return 0;
224 static uint64_t sync_vdptime(struct vo *vo)
226 struct vdpctx *vc = vo->priv;
228 unsigned int t = GetTimer();
229 if (t - vc->last_sync_update > 5000000)
230 change_vdptime_sync(vc, &t);
231 uint64_t now = (t - vc->last_sync_update) * 1000ULL + vc->last_vdp_time;
232 // Make sure nanosecond inaccuracies don't make things inconsistent
233 now = FFMAX(now, vc->recent_vsync_time);
234 return now;
237 static uint64_t convert_to_vdptime(struct vo *vo, unsigned int t)
239 struct vdpctx *vc = vo->priv;
240 return (int)(t - vc->last_sync_update) * 1000LL + vc->last_vdp_time;
243 static int render_video_to_output_surface(struct vo *vo,
244 VdpOutputSurface output_surface,
245 VdpRect *output_rect)
247 struct vdpctx *vc = vo->priv;
248 struct vdp_functions *vdp = vc->vdp;
249 VdpTime dummy;
250 VdpStatus vdp_st;
251 if (vc->deint_queue_pos < 0)
252 return -1;
254 struct buffered_video_surface *bv = vc->buffered_video;
255 int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
256 unsigned int dp = vc->deint_queue_pos;
257 // dp==0 means last field of latest frame, 1 earlier field of latest frame,
258 // 2 last field of previous frame and so on
259 if (vc->deint) {
260 field = vc->top_field_first ^ (dp & 1) ?
261 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
262 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
264 const VdpVideoSurface *past_fields = (const VdpVideoSurface []){
265 bv[(dp+1)/2].surface, bv[(dp+2)/2].surface};
266 const VdpVideoSurface *future_fields = (const VdpVideoSurface []){
267 dp >= 1 ? bv[(dp-1)/2].surface : VDP_INVALID_HANDLE};
268 vdp_st = vdp->presentation_queue_block_until_surface_idle(vc->flip_queue,
269 output_surface,
270 &dummy);
271 CHECK_ST_WARNING("Error when calling "
272 "vdp_presentation_queue_block_until_surface_idle");
274 vdp_st = vdp->video_mixer_render(vc->video_mixer, VDP_INVALID_HANDLE,
275 0, field, 2, past_fields,
276 bv[dp/2].surface, 1, future_fields,
277 &vc->src_rect_vid, output_surface,
278 NULL, output_rect, 0, NULL);
279 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render");
280 return 0;
283 static int video_to_output_surface(struct vo *vo)
285 struct vdpctx *vc = vo->priv;
287 return render_video_to_output_surface(vo,
288 vc->output_surfaces[vc->surface_num],
289 &vc->out_rect_vid);
292 static int next_deint_queue_pos(struct vo *vo, bool eof)
294 struct vdpctx *vc = vo->priv;
296 int dqp = vc->deint_queue_pos;
297 if (dqp < 0)
298 dqp += 1000;
299 else
300 dqp = vc->deint >= 2 ? dqp - 1 : dqp - 2 | 1;
301 if (dqp < (eof ? 0 : 3))
302 return -1;
303 return dqp;
306 static void set_next_frame_info(struct vo *vo, bool eof)
308 struct vdpctx *vc = vo->priv;
310 vo->frame_loaded = false;
311 int dqp = next_deint_queue_pos(vo, eof);
312 if (dqp < 0)
313 return;
314 vo->frame_loaded = true;
316 // Set pts values
317 struct buffered_video_surface *bv = vc->buffered_video;
318 int idx = dqp >> 1;
319 if (idx == 0) { // no future frame/pts available
320 vo->next_pts = bv[0].pts;
321 vo->next_pts2 = MP_NOPTS_VALUE;
322 } else if (!(vc->deint >= 2)) { // no field-splitting deinterlace
323 vo->next_pts = bv[idx].pts;
324 vo->next_pts2 = bv[idx - 1].pts;
325 } else { // deinterlace with separate fields
326 double intermediate_pts;
327 double diff = bv[idx - 1].pts - bv[idx].pts;
328 if (diff > 0 && diff < 0.5)
329 intermediate_pts = (bv[idx].pts + bv[idx - 1].pts) / 2;
330 else
331 intermediate_pts = bv[idx].pts;
332 if (dqp & 1) { // first field
333 vo->next_pts = bv[idx].pts;
334 vo->next_pts2 = intermediate_pts;
335 } else {
336 vo->next_pts = intermediate_pts;
337 vo->next_pts2 = bv[idx - 1].pts;
342 static void add_new_video_surface(struct vo *vo, VdpVideoSurface surface,
343 struct mp_image *reserved_mpi, double pts)
345 struct vdpctx *vc = vo->priv;
346 struct buffered_video_surface *bv = vc->buffered_video;
348 if (reserved_mpi)
349 reserved_mpi->usage_count++;
350 if (bv[NUM_BUFFERED_VIDEO - 1].mpi)
351 bv[NUM_BUFFERED_VIDEO - 1].mpi->usage_count--;
353 for (int i = NUM_BUFFERED_VIDEO - 1; i > 0; i--)
354 bv[i] = bv[i - 1];
355 bv[0] = (struct buffered_video_surface){
356 .mpi = reserved_mpi,
357 .surface = surface,
358 .pts = pts,
361 vc->deint_queue_pos = FFMIN(vc->deint_queue_pos + 2,
362 NUM_BUFFERED_VIDEO * 2 - 3);
363 set_next_frame_info(vo, false);
366 static void forget_frames(struct vo *vo)
368 struct vdpctx *vc = vo->priv;
370 vc->deint_queue_pos = -1001;
371 vc->dropped_frame = false;
372 for (int i = 0; i < NUM_BUFFERED_VIDEO; i++) {
373 struct buffered_video_surface *p = vc->buffered_video + i;
374 if (p->mpi)
375 p->mpi->usage_count--;
376 *p = (struct buffered_video_surface){
377 .surface = VDP_INVALID_HANDLE,
382 static void resize(struct vo *vo)
384 struct vdpctx *vc = vo->priv;
385 struct vdp_functions *vdp = vc->vdp;
386 VdpStatus vdp_st;
387 int i;
388 struct vo_rect src_rect;
389 struct vo_rect dst_rect;
390 struct vo_rect borders;
391 calc_src_dst_rects(vo, vc->vid_width, vc->vid_height, &src_rect, &dst_rect,
392 &borders, NULL);
393 vc->out_rect_vid.x0 = dst_rect.left;
394 vc->out_rect_vid.x1 = dst_rect.right;
395 vc->out_rect_vid.y0 = dst_rect.top;
396 vc->out_rect_vid.y1 = dst_rect.bottom;
397 vc->src_rect_vid.x0 = src_rect.left;
398 vc->src_rect_vid.x1 = src_rect.right;
399 vc->src_rect_vid.y0 = vc->flip ? src_rect.bottom : src_rect.top;
400 vc->src_rect_vid.y1 = vc->flip ? src_rect.top : src_rect.bottom;
401 vc->border_x = borders.left;
402 vc->border_y = borders.top;
403 int flip_offset_ms = vo_fs ? vc->flip_offset_fs : vc->flip_offset_window;
404 vo->flip_queue_offset = flip_offset_ms / 1000.;
406 int min_output_width = FFMAX(vo->dwidth, vc->vid_width);
407 int min_output_height = FFMAX(vo->dheight, vc->vid_height);
409 if (vc->output_surface_width < min_output_width
410 || vc->output_surface_height < min_output_height) {
411 if (vc->output_surface_width < min_output_width) {
412 vc->output_surface_width += vc->output_surface_width >> 1;
413 vc->output_surface_width = FFMAX(vc->output_surface_width,
414 min_output_width);
416 if (vc->output_surface_height < min_output_height) {
417 vc->output_surface_height += vc->output_surface_height >> 1;
418 vc->output_surface_height = FFMAX(vc->output_surface_height,
419 min_output_height);
421 // Creation of output_surfaces
422 for (i = 0; i <= vc->num_output_surfaces; i++) {
423 if (vc->output_surfaces[i] != VDP_INVALID_HANDLE) {
424 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
425 CHECK_ST_WARNING("Error when calling "
426 "vdp_output_surface_destroy");
428 vdp_st = vdp->output_surface_create(vc->vdp_device,
429 OUTPUT_RGBA_FORMAT,
430 vc->output_surface_width,
431 vc->output_surface_height,
432 &vc->output_surfaces[i]);
433 CHECK_ST_WARNING("Error when calling vdp_output_surface_create");
434 mp_msg(MSGT_VO, MSGL_DBG2, "vdpau out create: %u\n",
435 vc->output_surfaces[i]);
438 vo->want_redraw = true;
441 static void preemption_callback(VdpDevice device, void *context)
443 struct vdpctx *vc = context;
444 vc->is_preempted = true;
445 vc->preemption_acked = false;
448 /* Initialize vdp_get_proc_address, called from preinit() */
449 static int win_x11_init_vdpau_procs(struct vo *vo)
451 struct vo_x11_state *x11 = vo->x11;
452 struct vdpctx *vc = vo->priv;
453 if (vc->vdp) // reinitialization after preemption
454 memset(vc->vdp, 0, sizeof(*vc->vdp));
455 else
456 vc->vdp = talloc_zero(vc, struct vdp_functions);
457 struct vdp_functions *vdp = vc->vdp;
458 VdpStatus vdp_st;
460 struct vdp_function {
461 const int id;
462 int offset;
465 const struct vdp_function *dsc;
467 static const struct vdp_function vdp_func[] = {
468 #define VDP_FUNCTION(_, macro_name, mp_name) {macro_name, offsetof(struct vdp_functions, mp_name)},
469 #include "vdpau_template.c"
470 #undef VDP_FUNCTION
471 {0, -1}
474 vdp_st = vdp_device_create_x11(x11->display, x11->screen, &vc->vdp_device,
475 &vc->vdp_get_proc_address);
476 if (vdp_st != VDP_STATUS_OK) {
477 if (vc->is_preempted)
478 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Error calling "
479 "vdp_device_create_x11 while preempted: %d\n", vdp_st);
480 else
481 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling "
482 "vdp_device_create_x11: %d\n", vdp_st);
483 return -1;
486 vdp->get_error_string = NULL;
487 for (dsc = vdp_func; dsc->offset >= 0; dsc++) {
488 vdp_st = vc->vdp_get_proc_address(vc->vdp_device, dsc->id,
489 (void **)((char *)vdp + dsc->offset));
490 if (vdp_st != VDP_STATUS_OK) {
491 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling "
492 "vdp_get_proc_address(function id %d): %s\n", dsc->id,
493 vdp->get_error_string ? vdp->get_error_string(vdp_st) : "?");
494 return -1;
497 vdp_st = vdp->preemption_callback_register(vc->vdp_device,
498 preemption_callback, vc);
499 return 0;
502 static int win_x11_init_vdpau_flip_queue(struct vo *vo)
504 struct vdpctx *vc = vo->priv;
505 struct vdp_functions *vdp = vc->vdp;
506 struct vo_x11_state *x11 = vo->x11;
507 VdpStatus vdp_st;
509 if (vc->flip_target == VDP_INVALID_HANDLE) {
510 vdp_st = vdp->presentation_queue_target_create_x11(vc->vdp_device,
511 x11->window,
512 &vc->flip_target);
513 CHECK_ST_ERROR("Error when calling "
514 "vdp_presentation_queue_target_create_x11");
517 /* Emperically this seems to be the first call which fails when we
518 * try to reinit after preemption while the user is still switched
519 * from X to a virtual terminal (creating the vdp_device initially
520 * succeeds, as does creating the flip_target above). This is
521 * probably not guaranteed behavior, but we'll assume it as a simple
522 * way to reduce warnings while trying to recover from preemption.
524 if (vc->flip_queue == VDP_INVALID_HANDLE) {
525 vdp_st = vdp->presentation_queue_create(vc->vdp_device, vc->flip_target,
526 &vc->flip_queue);
527 if (vc->is_preempted && vdp_st != VDP_STATUS_OK) {
528 mp_msg(MSGT_VO, MSGL_DBG2, "[vdpau] Failed to create flip queue "
529 "while preempted: %s\n", vdp->get_error_string(vdp_st));
530 return -1;
531 } else
532 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create");
535 VdpTime vdp_time;
536 vdp_st = vdp->presentation_queue_get_time(vc->flip_queue, &vdp_time);
537 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_get_time");
538 vc->last_vdp_time = vdp_time;
539 vc->last_sync_update = GetTimer();
541 vc->vsync_interval = 1;
542 if (vc->composite_detect && vo_x11_screen_is_composited(vo)) {
543 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Compositing window manager "
544 "detected. Assuming timing info is inaccurate.\n");
545 } else if (vc->user_fps > 0) {
546 vc->vsync_interval = 1e9 / vc->user_fps;
547 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Assuming user-specified display "
548 "refresh rate of %.3f Hz.\n", vc->user_fps);
549 } else if (vc->user_fps == 0) {
550 #ifdef CONFIG_XF86VM
551 double fps = vo_vm_get_fps(vo);
552 if (!fps)
553 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] Failed to get display FPS\n");
554 else {
555 vc->vsync_interval = 1e9 / fps;
556 // This is verbose, but I'm not yet sure how common wrong values are
557 mp_msg(MSGT_VO, MSGL_INFO,
558 "[vdpau] Got display refresh rate %.3f Hz.\n"
559 "[vdpau] If that value looks wrong give the "
560 "-vo vdpau:fps=X suboption manually.\n", fps);
562 #else
563 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] This binary has been compiled "
564 "without XF86VidMode support.\n");
565 mp_msg(MSGT_VO, MSGL_INFO, "[vdpau] Can't use vsync-aware timing "
566 "without manually provided -vo vdpau:fps=X suboption.\n");
567 #endif
568 } else
569 mp_msg(MSGT_VO, MSGL_V, "[vdpau] framedrop/timing logic disabled by "
570 "user.\n");
572 return 0;
575 static int set_video_attribute(struct vdpctx *vc, VdpVideoMixerAttribute attr,
576 const void *value, char *attr_name)
578 struct vdp_functions *vdp = vc->vdp;
579 VdpStatus vdp_st;
581 vdp_st = vdp->video_mixer_set_attribute_values(vc->video_mixer, 1, &attr,
582 &value);
583 if (vdp_st != VDP_STATUS_OK) {
584 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error setting video mixer "
585 "attribute %s: %s\n", attr_name, vdp->get_error_string(vdp_st));
586 return -1;
588 return 0;
591 static void update_csc_matrix(struct vo *vo)
593 struct vdpctx *vc = vo->priv;
595 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Updating CSC matrix\n");
597 // VdpCSCMatrix happens to be compatible with mplayer's CSC matrix type
598 // both are float[3][4]
599 VdpCSCMatrix matrix;
601 struct mp_csp_params cparams = {
602 .colorspace = vc->colorspace, .input_bits = 8, .texture_bits = 8 };
603 mp_csp_copy_equalizer_values(&cparams, &vc->video_eq);
604 mp_get_yuv2rgb_coeffs(&cparams, matrix);
606 set_video_attribute(vc, VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX,
607 &matrix, "CSC matrix");
610 #define SET_VIDEO_ATTR(attr_name, attr_type, value) set_video_attribute(vc, \
611 VDP_VIDEO_MIXER_ATTRIBUTE_ ## attr_name, &(attr_type){value},\
612 # attr_name)
613 static int create_vdp_mixer(struct vo *vo, VdpChromaType vdp_chroma_type)
615 struct vdpctx *vc = vo->priv;
616 struct vdp_functions *vdp = vc->vdp;
617 #define VDP_NUM_MIXER_PARAMETER 3
618 #define MAX_NUM_FEATURES 6
619 int i;
620 VdpStatus vdp_st;
622 if (vc->video_mixer != VDP_INVALID_HANDLE)
623 return 0;
625 int feature_count = 0;
626 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
627 VdpBool feature_enables[MAX_NUM_FEATURES];
628 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
629 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
630 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
631 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE,
633 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
634 &vc->vid_width,
635 &vc->vid_height,
636 &vdp_chroma_type,
638 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
639 if (vc->deint_type == 4)
640 features[feature_count++] =
641 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
642 if (vc->pullup)
643 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
644 if (vc->denoise)
645 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
646 if (vc->sharpen)
647 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
648 if (vc->hqscaling) {
649 VdpVideoMixerFeature hqscaling_feature =
650 VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1 + vc->hqscaling-1;
651 VdpBool hqscaling_available;
652 vdp_st = vdp->video_mixer_query_feature_support(vc->vdp_device,
653 hqscaling_feature,
654 &hqscaling_available);
655 CHECK_ST_ERROR("Error when calling video_mixer_query_feature_support");
656 if (hqscaling_available)
657 features[feature_count++] = hqscaling_feature;
658 else
659 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Your hardware or VDPAU "
660 "library does not support requested hqscaling.\n");
663 vdp_st = vdp->video_mixer_create(vc->vdp_device, feature_count, features,
664 VDP_NUM_MIXER_PARAMETER,
665 parameters, parameter_values,
666 &vc->video_mixer);
667 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create");
669 for (i = 0; i < feature_count; i++)
670 feature_enables[i] = VDP_TRUE;
671 if (vc->deint < 3)
672 feature_enables[0] = VDP_FALSE;
673 if (vc->deint_type == 4 && vc->deint < 4)
674 feature_enables[1] = VDP_FALSE;
675 if (feature_count) {
676 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
677 feature_count, features,
678 feature_enables);
679 CHECK_ST_WARNING("Error calling vdp_video_mixer_set_feature_enables");
681 if (vc->denoise)
682 SET_VIDEO_ATTR(NOISE_REDUCTION_LEVEL, float, vc->denoise);
683 if (vc->sharpen)
684 SET_VIDEO_ATTR(SHARPNESS_LEVEL, float, vc->sharpen);
685 if (!vc->chroma_deint)
686 SET_VIDEO_ATTR(SKIP_CHROMA_DEINTERLACE, uint8_t, 1);
688 update_csc_matrix(vo);
689 return 0;
692 // Free everything specific to a certain video file
693 static void free_video_specific(struct vo *vo)
695 struct vdpctx *vc = vo->priv;
696 struct vdp_functions *vdp = vc->vdp;
697 int i;
698 VdpStatus vdp_st;
700 if (vc->decoder != VDP_INVALID_HANDLE)
701 vdp->decoder_destroy(vc->decoder);
702 vc->decoder = VDP_INVALID_HANDLE;
703 vc->decoder_max_refs = -1;
705 forget_frames(vo);
707 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
708 if (vc->surface_render[i].surface != VDP_INVALID_HANDLE) {
709 vdp_st = vdp->video_surface_destroy(vc->surface_render[i].surface);
710 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy");
712 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
715 if (vc->video_mixer != VDP_INVALID_HANDLE) {
716 vdp_st = vdp->video_mixer_destroy(vc->video_mixer);
717 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy");
719 vc->video_mixer = VDP_INVALID_HANDLE;
722 static int create_vdp_decoder(struct vo *vo, int max_refs)
724 struct vdpctx *vc = vo->priv;
725 struct vdp_functions *vdp = vc->vdp;
726 VdpStatus vdp_st;
727 VdpDecoderProfile vdp_decoder_profile;
728 if (vc->decoder != VDP_INVALID_HANDLE)
729 vdp->decoder_destroy(vc->decoder);
730 switch (vc->image_format) {
731 case IMGFMT_VDPAU_MPEG1:
732 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
733 break;
734 case IMGFMT_VDPAU_MPEG2:
735 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
736 break;
737 case IMGFMT_VDPAU_H264:
738 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
739 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Creating H264 hardware decoder "
740 "for %d reference frames.\n", max_refs);
741 break;
742 case IMGFMT_VDPAU_WMV3:
743 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
744 break;
745 case IMGFMT_VDPAU_VC1:
746 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
747 break;
748 case IMGFMT_VDPAU_MPEG4:
749 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG4_PART2_ASP;
750 break;
751 default:
752 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Unknown image format!\n");
753 goto fail;
755 vdp_st = vdp->decoder_create(vc->vdp_device, vdp_decoder_profile,
756 vc->vid_width, vc->vid_height, max_refs,
757 &vc->decoder);
758 CHECK_ST_WARNING("Failed creating VDPAU decoder");
759 if (vdp_st != VDP_STATUS_OK) {
760 fail:
761 vc->decoder = VDP_INVALID_HANDLE;
762 vc->decoder_max_refs = 0;
763 return 0;
765 vc->decoder_max_refs = max_refs;
766 return 1;
769 static int initialize_vdpau_objects(struct vo *vo)
771 struct vdpctx *vc = vo->priv;
772 struct vdp_functions *vdp = vc->vdp;
773 VdpStatus vdp_st;
775 vc->vdp_chroma_type = VDP_CHROMA_TYPE_420;
776 switch (vc->image_format) {
777 case IMGFMT_YV12:
778 case IMGFMT_I420:
779 case IMGFMT_IYUV:
780 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YV12;
781 break;
782 case IMGFMT_NV12:
783 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_NV12;
784 break;
785 case IMGFMT_YUY2:
786 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_YUYV;
787 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
788 break;
789 case IMGFMT_UYVY:
790 vc->vdp_pixel_format = VDP_YCBCR_FORMAT_UYVY;
791 vc->vdp_chroma_type = VDP_CHROMA_TYPE_422;
793 if (win_x11_init_vdpau_flip_queue(vo) < 0)
794 return -1;
796 if (create_vdp_mixer(vo, vc->vdp_chroma_type) < 0)
797 return -1;
799 vdp_st = vdp->
800 bitmap_surface_query_capabilities(vc->vdp_device,
801 VDP_RGBA_FORMAT_A8,
802 &(VdpBool){0},
803 &vc->eosd_surface.max_width,
804 &vc->eosd_surface.max_height);
805 CHECK_ST_WARNING("Query to get max EOSD surface size failed");
806 forget_frames(vo);
807 resize(vo);
808 return 0;
811 static void mark_vdpau_objects_uninitialized(struct vo *vo)
813 struct vdpctx *vc = vo->priv;
815 vc->decoder = VDP_INVALID_HANDLE;
816 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
817 vc->surface_render[i].surface = VDP_INVALID_HANDLE;
818 forget_frames(vo);
819 vc->video_mixer = VDP_INVALID_HANDLE;
820 vc->flip_queue = VDP_INVALID_HANDLE;
821 vc->flip_target = VDP_INVALID_HANDLE;
822 for (int i = 0; i <= MAX_OUTPUT_SURFACES; i++)
823 vc->output_surfaces[i] = VDP_INVALID_HANDLE;
824 vc->vdp_device = VDP_INVALID_HANDLE;
825 vc->eosd_surface = (struct eosd_bitmap_surface){
826 .surface = VDP_INVALID_HANDLE,
828 vc->output_surface_width = vc->output_surface_height = -1;
829 vc->eosd_render_count = 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 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 vc->vid_d_width = d_width;
890 vc->vid_d_height = d_height;
892 free_video_specific(vo);
893 if (IMGFMT_IS_VDPAU(vc->image_format) && !create_vdp_decoder(vo, 2))
894 return -1;
896 #ifdef CONFIG_XF86VM
897 if (vm) {
898 vo_vm_switch(vo);
899 vc->mode_switched = true;
901 #endif
902 XGetWindowAttributes(x11->display, DefaultRootWindow(x11->display),
903 &attribs);
904 depth = attribs.depth;
905 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
906 depth = 24;
907 XMatchVisualInfo(x11->display, x11->screen, depth, TrueColor, &vinfo);
909 xswa.background_pixel = 0;
910 xswa.border_pixel = 0;
911 /* Do not use CWBackPixel: It leads to VDPAU errors after
912 * aspect ratio changes. */
913 xswamask = CWBorderPixel;
915 vo_x11_create_vo_window(vo, &vinfo, vo->dx, vo->dy, d_width, d_height,
916 flags, CopyFromParent, "vdpau");
917 XChangeWindowAttributes(x11->display, x11->window, xswamask, &xswa);
919 #ifdef CONFIG_XF86VM
920 if (vm) {
921 /* Grab the mouse pointer in our window */
922 if (vo_grabpointer)
923 XGrabPointer(x11->display, x11->window, True, 0,
924 GrabModeAsync, GrabModeAsync,
925 x11->window, None, CurrentTime);
926 XSetInputFocus(x11->display, x11->window, RevertToNone, CurrentTime);
928 #endif
930 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
931 vo_fs = 1;
933 if (initialize_vdpau_objects(vo) < 0)
934 return -1;
936 return 0;
939 static void check_events(struct vo *vo)
941 if (handle_preemption(vo) < 0)
942 return;
944 int e = vo_x11_check_events(vo);
946 if (e & VO_EVENT_RESIZE)
947 resize(vo);
948 else if (e & VO_EVENT_EXPOSE) {
949 vo->want_redraw = true;
953 static void draw_osd_I8A8(void *ctx, int x0, int y0, int w, int h,
954 unsigned char *src, unsigned char *srca, int stride)
956 struct vo *vo = ctx;
957 struct vdpctx *vc = vo->priv;
958 struct vdp_functions *vdp = vc->vdp;
959 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
960 VdpStatus vdp_st;
961 int i;
962 int pitch;
963 int index_data_size_required;
964 VdpRect output_indexed_rect_vid;
966 if (!w || !h)
967 return;
969 index_data_size_required = 2*w*h;
970 if (vc->index_data_size < index_data_size_required) {
971 vc->index_data = talloc_realloc_size(vc, vc->index_data,
972 index_data_size_required);
973 vc->index_data_size = index_data_size_required;
976 // index_data creation, component order - I, A, I, A, .....
977 for (i = 0; i < h; i++)
978 for (int j = 0; j < w; j++) {
979 vc->index_data[i*2*w + j*2] = src [i*stride+j];
980 vc->index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
983 output_indexed_rect_vid.x0 = x0;
984 output_indexed_rect_vid.y0 = y0;
985 output_indexed_rect_vid.x1 = x0 + w;
986 output_indexed_rect_vid.y1 = y0 + h;
988 pitch = w*2;
990 // write source_data to osd_surface.
991 VdpOutputSurface osd_surface = vc->output_surfaces[vc->num_output_surfaces];
992 vdp_st = vdp->
993 output_surface_put_bits_indexed(osd_surface, VDP_INDEXED_FORMAT_I8A8,
994 (const void *const*)&vc->index_data,
995 &pitch, &output_indexed_rect_vid,
996 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
997 (void *)vc->palette);
998 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed");
1000 VdpOutputSurfaceRenderBlendState blend_state = {
1001 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1002 .blend_factor_source_color =
1003 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1004 .blend_factor_source_alpha =
1005 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1006 .blend_factor_destination_color =
1007 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1008 .blend_factor_destination_alpha =
1009 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1010 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1011 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1014 vdp_st = vdp->
1015 output_surface_render_output_surface(output_surface,
1016 &output_indexed_rect_vid,
1017 osd_surface,
1018 &output_indexed_rect_vid,
1019 NULL, &blend_state,
1020 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1021 CHECK_ST_WARNING("Error when calling "
1022 "vdp_output_surface_render_output_surface");
1025 static void draw_eosd(struct vo *vo)
1027 struct vdpctx *vc = vo->priv;
1028 struct vdp_functions *vdp = vc->vdp;
1029 VdpStatus vdp_st;
1030 VdpOutputSurface output_surface = vc->output_surfaces[vc->surface_num];
1031 int i;
1033 VdpOutputSurfaceRenderBlendState blend_state = {
1034 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
1035 .blend_factor_source_color =
1036 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1037 .blend_factor_source_alpha =
1038 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
1039 .blend_factor_destination_color =
1040 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
1041 .blend_factor_destination_alpha =
1042 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
1043 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1044 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
1047 for (i = 0; i < vc->eosd_render_count; i++) {
1048 vdp_st = vdp->
1049 output_surface_render_bitmap_surface(output_surface,
1050 &vc->eosd_targets[i].dest,
1051 vc->eosd_surface.surface,
1052 &vc->eosd_targets[i].source,
1053 &vc->eosd_targets[i].color,
1054 &blend_state,
1055 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
1056 CHECK_ST_WARNING("EOSD: Error when rendering");
1060 #define HEIGHT_SORT_BITS 4
1061 static int size_index(struct eosd_target *r)
1063 unsigned int h = r->source.y1;
1064 int n = av_log2_16bit(h);
1065 return (n << HEIGHT_SORT_BITS)
1066 + (- 1 - (h << HEIGHT_SORT_BITS >> n) & (1 << HEIGHT_SORT_BITS) - 1);
1069 /* Pack the given rectangles into an area of size w * h.
1070 * The size of each rectangle is read from .source.x1/.source.y1.
1071 * The height of each rectangle must be at least 1 and less than 65536.
1072 * The .source rectangle is then set corresponding to the packed position.
1073 * 'scratch' must point to work memory for num_rects+16 ints.
1074 * Return 0 on success, -1 if the rectangles did not fit in w*h.
1076 * The rectangles are placed in rows in order approximately sorted by
1077 * height (the approximate sorting is simpler than a full one would be,
1078 * and allows the algorithm to work in linear time). Additionally, to
1079 * reduce wasted space when there are a few tall rectangles, empty
1080 * lower-right parts of rows are filled recursively when the size of
1081 * rectangles in the row drops past a power-of-two threshold. So if a
1082 * row starts with rectangles of size 3x50, 10x40 and 5x20 then the
1083 * free rectangle with corners (13, 20)-(w, 50) is filled recursively.
1085 static int pack_rectangles(struct eosd_target *rects, int num_rects,
1086 int w, int h, int *scratch)
1088 int bins[16 << HEIGHT_SORT_BITS];
1089 int sizes[16 << HEIGHT_SORT_BITS] = {};
1090 for (int i = 0; i < num_rects; i++)
1091 sizes[size_index(rects + i)]++;
1092 int idx = 0;
1093 for (int i = 0; i < 16 << HEIGHT_SORT_BITS; i += 1 << HEIGHT_SORT_BITS) {
1094 for (int j = 0; j < 1 << HEIGHT_SORT_BITS; j++) {
1095 bins[i + j] = idx;
1096 idx += sizes[i + j];
1098 scratch[idx++] = -1;
1100 for (int i = 0; i < num_rects; i++)
1101 scratch[bins[size_index(rects + i)]++] = i;
1102 for (int i = 0; i < 16; i++)
1103 bins[i] = bins[i << HEIGHT_SORT_BITS] - sizes[i << HEIGHT_SORT_BITS];
1104 struct {
1105 int size, x, bottom;
1106 } stack[16] = {{15, 0, h}}, s = {};
1107 int stackpos = 1;
1108 int y;
1109 while (stackpos) {
1110 y = s.bottom;
1111 s = stack[--stackpos];
1112 s.size++;
1113 while (s.size--) {
1114 int maxy = -1;
1115 int obj;
1116 while ((obj = scratch[bins[s.size]]) >= 0) {
1117 int bottom = y + rects[obj].source.y1;
1118 if (bottom > s.bottom)
1119 break;
1120 int right = s.x + rects[obj].source.x1;
1121 if (right > w)
1122 break;
1123 bins[s.size]++;
1124 rects[obj].source.x0 = s.x;
1125 rects[obj].source.x1 += s.x;
1126 rects[obj].source.y0 = y;
1127 rects[obj].source.y1 += y;
1128 num_rects--;
1129 if (maxy <= 0)
1130 stack[stackpos++] = s;
1131 s.x = right;
1132 maxy = FFMAX(maxy, bottom);
1134 if (maxy > 0)
1135 s.bottom = maxy;
1138 return num_rects ? -1 : 0;
1141 static void generate_eosd(struct vo *vo, mp_eosd_images_t *imgs)
1143 struct vdpctx *vc = vo->priv;
1144 struct vdp_functions *vdp = vc->vdp;
1145 VdpStatus vdp_st;
1146 int i;
1147 ASS_Image *img = imgs->imgs;
1148 ASS_Image *p;
1149 struct eosd_bitmap_surface *sfc = &vc->eosd_surface;
1150 bool need_upload = false;
1152 if (imgs->changed == 0)
1153 return; // Nothing changed, no need to redraw
1155 vc->eosd_render_count = 0;
1157 if (!img)
1158 return; // There's nothing to render!
1160 if (imgs->changed == 1)
1161 goto eosd_skip_upload;
1163 need_upload = true;
1164 bool reallocate = false;
1165 while (1) {
1166 for (p = img, i = 0; p; p = p->next) {
1167 if (p->w <= 0 || p->h <= 0)
1168 continue;
1169 // Allocate new space for surface/target arrays
1170 if (i >= vc->eosd_targets_size) {
1171 vc->eosd_targets_size = FFMAX(vc->eosd_targets_size * 2, 512);
1172 vc->eosd_targets =
1173 talloc_realloc_size(vc, vc->eosd_targets,
1174 vc->eosd_targets_size
1175 * sizeof(*vc->eosd_targets));
1176 vc->eosd_scratch =
1177 talloc_realloc_size(vc, vc->eosd_scratch,
1178 (vc->eosd_targets_size + 16)
1179 * sizeof(*vc->eosd_scratch));
1181 vc->eosd_targets[i].source.x1 = p->w;
1182 vc->eosd_targets[i].source.y1 = p->h;
1183 i++;
1185 if (pack_rectangles(vc->eosd_targets, i, sfc->w, sfc->h,
1186 vc->eosd_scratch) >= 0)
1187 break;
1188 int w = FFMIN(FFMAX(sfc->w * 2, EOSD_SURFACE_INITIAL_SIZE),
1189 sfc->max_width);
1190 int h = FFMIN(FFMAX(sfc->h * 2, EOSD_SURFACE_INITIAL_SIZE),
1191 sfc->max_height);
1192 if (w == sfc->w && h == sfc->h) {
1193 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] EOSD bitmaps do not fit on "
1194 "a surface with the maximum supported size\n");
1195 return;
1196 } else {
1197 sfc->w = w;
1198 sfc->h = h;
1200 reallocate = true;
1202 if (reallocate) {
1203 if (sfc->surface != VDP_INVALID_HANDLE) {
1204 vdp_st = vdp->bitmap_surface_destroy(sfc->surface);
1205 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1207 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Allocating a %dx%d surface for "
1208 "EOSD bitmaps.\n", sfc->w, sfc->h);
1209 vdp_st = vdp->bitmap_surface_create(vc->vdp_device, VDP_RGBA_FORMAT_A8,
1210 sfc->w, sfc->h, true,
1211 &sfc->surface);
1212 if (vdp_st != VDP_STATUS_OK)
1213 sfc->surface = VDP_INVALID_HANDLE;
1214 CHECK_ST_WARNING("EOSD: error when creating surface");
1217 eosd_skip_upload:
1218 if (sfc->surface == VDP_INVALID_HANDLE)
1219 return;
1220 for (p = img; p; p = p->next) {
1221 if (p->w <= 0 || p->h <= 0)
1222 continue;
1223 struct eosd_target *target = &vc->eosd_targets[vc->eosd_render_count];
1224 if (need_upload) {
1225 vdp_st = vdp->
1226 bitmap_surface_put_bits_native(sfc->surface,
1227 (const void *) &p->bitmap,
1228 &p->stride, &target->source);
1229 CHECK_ST_WARNING("EOSD: putbits failed");
1231 // Render dest, color, etc.
1232 target->color.alpha = 1.0 - ((p->color >> 0) & 0xff) / 255.0;
1233 target->color.blue = ((p->color >> 8) & 0xff) / 255.0;
1234 target->color.green = ((p->color >> 16) & 0xff) / 255.0;
1235 target->color.red = ((p->color >> 24) & 0xff) / 255.0;
1236 target->dest.x0 = p->dst_x;
1237 target->dest.y0 = p->dst_y;
1238 target->dest.x1 = p->w + p->dst_x;
1239 target->dest.y1 = p->h + p->dst_y;
1240 vc->eosd_render_count++;
1244 static void draw_osd(struct vo *vo, struct osd_state *osd)
1246 struct vdpctx *vc = vo->priv;
1248 if (handle_preemption(vo) < 0)
1249 return;
1251 osd_draw_text_ext(osd, vo->dwidth, vo->dheight, vc->border_x, vc->border_y,
1252 vc->border_x, vc->border_y, vc->vid_width,
1253 vc->vid_height, draw_osd_I8A8, vo);
1256 static int update_presentation_queue_status(struct vo *vo)
1258 struct vdpctx *vc = vo->priv;
1259 struct vdp_functions *vdp = vc->vdp;
1260 VdpStatus vdp_st;
1262 while (vc->query_surface_num != vc->surface_num) {
1263 VdpTime vtime;
1264 VdpPresentationQueueStatus status;
1265 VdpOutputSurface surface = vc->output_surfaces[vc->query_surface_num];
1266 vdp_st = vdp->presentation_queue_query_surface_status(vc->flip_queue,
1267 surface,
1268 &status, &vtime);
1269 CHECK_ST_WARNING("Error calling "
1270 "presentation_queue_query_surface_status");
1271 if (status == VDP_PRESENTATION_QUEUE_STATUS_QUEUED)
1272 break;
1273 if (vc->vsync_interval > 1) {
1274 uint64_t qtime = vc->queue_time[vc->query_surface_num];
1275 if (vtime < qtime + vc->vsync_interval / 2)
1276 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown too early\n");
1277 if (vtime > qtime + vc->vsync_interval)
1278 mp_msg(MSGT_VO, MSGL_V, "[vdpau] Frame shown late\n");
1280 vc->query_surface_num = WRAP_ADD(vc->query_surface_num, 1,
1281 vc->num_output_surfaces);
1282 vc->recent_vsync_time = vtime;
1284 int num_queued = WRAP_ADD(vc->surface_num, -vc->query_surface_num,
1285 vc->num_output_surfaces);
1286 mp_msg(MSGT_VO, MSGL_DBG3, "[vdpau] Queued surface count (before add): "
1287 "%d\n", num_queued);
1288 return num_queued;
1291 static inline uint64_t prev_vs2(struct vdpctx *vc, uint64_t ts, int shift)
1293 uint64_t offset = ts - vc->recent_vsync_time;
1294 // Fix negative values for 1<<shift vsyncs before vc->recent_vsync_time
1295 offset += (uint64_t)vc->vsync_interval << shift;
1296 offset %= vc->vsync_interval;
1297 return ts - offset;
1300 static void flip_page_timed(struct vo *vo, unsigned int pts_us, int duration)
1302 struct vdpctx *vc = vo->priv;
1303 struct vdp_functions *vdp = vc->vdp;
1304 VdpStatus vdp_st;
1305 uint32_t vsync_interval = vc->vsync_interval;
1307 if (handle_preemption(vo) < 0)
1308 return;
1310 if (duration > INT_MAX / 1000)
1311 duration = -1;
1312 else
1313 duration *= 1000;
1315 if (vc->vsync_interval == 1)
1316 duration = -1; // Make sure drop logic is disabled
1318 uint64_t now = sync_vdptime(vo);
1319 uint64_t pts = pts_us ? convert_to_vdptime(vo, pts_us) : now;
1320 uint64_t ideal_pts = pts;
1321 uint64_t npts = duration >= 0 ? pts + duration : UINT64_MAX;
1323 #define PREV_VS2(ts, shift) prev_vs2(vc, ts, shift)
1324 // Only gives accurate results for ts >= vc->recent_vsync_time
1325 #define PREV_VSYNC(ts) PREV_VS2(ts, 0)
1327 /* We hope to be here at least one vsync before the frame should be shown.
1328 * If we are running late then don't drop the frame unless there is
1329 * already one queued for the next vsync; even if we _hope_ to show the
1330 * next frame soon enough to mean this one should be dropped we might
1331 * not make the target time in reality. Without this check we could drop
1332 * every frame, freezing the display completely if video lags behind.
1334 if (now > PREV_VSYNC(FFMAX(pts, vc->last_queue_time + vsync_interval)))
1335 npts = UINT64_MAX;
1337 /* Allow flipping a frame at a vsync if its presentation time is a
1338 * bit after that vsync and the change makes the flip time delta
1339 * from previous frame better match the target timestamp delta.
1340 * This avoids instability with frame timestamps falling near vsyncs.
1341 * For example if the frame timestamps were (with vsyncs at
1342 * integer values) 0.01, 1.99, 4.01, 5.99, 8.01, ... then
1343 * straightforward timing at next vsync would flip the frames at
1344 * 1, 2, 5, 6, 9; this changes it to 1, 2, 4, 6, 8 and so on with
1345 * regular 2-vsync intervals.
1347 * Also allow moving the frame forward if it looks like we dropped
1348 * the previous frame incorrectly (now that we know better after
1349 * having final exact timestamp information for this frame) and
1350 * there would unnecessarily be a vsync without a frame change.
1352 uint64_t vsync = PREV_VSYNC(pts);
1353 if (pts < vsync + vsync_interval / 4
1354 && (vsync - PREV_VS2(vc->last_queue_time, 16)
1355 > pts - vc->last_ideal_time + vsync_interval / 2
1356 || vc->dropped_frame && vsync > vc->dropped_time))
1357 pts -= vsync_interval / 2;
1359 vc->dropped_frame = true; // changed at end if false
1360 vc->dropped_time = ideal_pts;
1362 pts = FFMAX(pts, vc->last_queue_time + vsync_interval);
1363 pts = FFMAX(pts, now);
1364 if (npts < PREV_VSYNC(pts) + vsync_interval)
1365 return;
1367 int num_flips = update_presentation_queue_status(vo);
1368 vsync = vc->recent_vsync_time + num_flips * vc->vsync_interval;
1369 now = sync_vdptime(vo);
1370 pts = FFMAX(pts, now);
1371 pts = FFMAX(pts, vsync + (vsync_interval >> 2));
1372 vsync = PREV_VSYNC(pts);
1373 if (npts < vsync + vsync_interval)
1374 return;
1375 pts = vsync + (vsync_interval >> 2);
1376 vdp_st =
1377 vdp->presentation_queue_display(vc->flip_queue,
1378 vc->output_surfaces[vc->surface_num],
1379 vo->dwidth, vo->dheight, pts);
1380 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display");
1382 vc->last_queue_time = pts;
1383 vc->queue_time[vc->surface_num] = pts;
1384 vc->last_ideal_time = ideal_pts;
1385 vc->dropped_frame = false;
1386 vc->surface_num = WRAP_ADD(vc->surface_num, 1, vc->num_output_surfaces);
1389 static int draw_slice(struct vo *vo, uint8_t *image[], int stride[], int w,
1390 int h, int x, int y)
1392 struct vdpctx *vc = vo->priv;
1393 struct vdp_functions *vdp = vc->vdp;
1394 VdpStatus vdp_st;
1396 if (handle_preemption(vo) < 0)
1397 return VO_TRUE;
1399 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
1400 int max_refs = vc->image_format == IMGFMT_VDPAU_H264 ?
1401 rndr->info.h264.num_ref_frames : 2;
1402 if (!IMGFMT_IS_VDPAU(vc->image_format))
1403 return VO_FALSE;
1404 if ((vc->decoder == VDP_INVALID_HANDLE || vc->decoder_max_refs < max_refs)
1405 && !create_vdp_decoder(vo, max_refs))
1406 return VO_FALSE;
1408 vdp_st = vdp->decoder_render(vc->decoder, rndr->surface,
1409 (void *)&rndr->info,
1410 rndr->bitstream_buffers_used,
1411 rndr->bitstream_buffers);
1412 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
1413 return VO_TRUE;
1417 static struct vdpau_render_state *get_surface(struct vo *vo, int number)
1419 struct vdpctx *vc = vo->priv;
1420 struct vdp_functions *vdp = vc->vdp;
1422 if (number > MAX_VIDEO_SURFACES)
1423 return NULL;
1424 if (vc->surface_render[number].surface == VDP_INVALID_HANDLE
1425 && !vc->is_preempted) {
1426 VdpStatus vdp_st;
1427 vdp_st = vdp->video_surface_create(vc->vdp_device, vc->vdp_chroma_type,
1428 vc->vid_width, vc->vid_height,
1429 &vc->surface_render[number].surface);
1430 CHECK_ST_WARNING("Error when calling vdp_video_surface_create");
1432 mp_msg(MSGT_VO, MSGL_DBG3, "vdpau vid create: %u\n",
1433 vc->surface_render[number].surface);
1434 return &vc->surface_render[number];
1437 static void draw_image(struct vo *vo, mp_image_t *mpi, double pts)
1439 struct vdpctx *vc = vo->priv;
1440 struct vdp_functions *vdp = vc->vdp;
1441 struct mp_image *reserved_mpi = NULL;
1442 struct vdpau_render_state *rndr;
1444 if (IMGFMT_IS_VDPAU(vc->image_format)) {
1445 rndr = mpi->priv;
1446 reserved_mpi = mpi;
1447 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
1448 rndr = get_surface(vo, vc->deint_counter);
1449 vc->deint_counter = WRAP_ADD(vc->deint_counter, 1, NUM_BUFFERED_VIDEO);
1450 if (handle_preemption(vo) >= 0) {
1451 VdpStatus vdp_st;
1452 const void *destdata[3] = {mpi->planes[0], mpi->planes[2],
1453 mpi->planes[1]};
1454 if (vc->image_format == IMGFMT_NV12)
1455 destdata[1] = destdata[2];
1456 vdp_st = vdp->video_surface_put_bits_y_cb_cr(rndr->surface,
1457 vc->vdp_pixel_format, destdata, mpi->stride);
1458 CHECK_ST_WARNING("Error when calling "
1459 "vdp_video_surface_put_bits_y_cb_cr");
1461 } else
1462 // We don't support slice callbacks so this shouldn't occur -
1463 // I think the flags test above in pointless, but I'm adding
1464 // this instead of removing it just in case.
1465 abort();
1466 if (mpi->fields & MP_IMGFIELD_ORDERED)
1467 vc->top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
1468 else
1469 vc->top_field_first = 1;
1471 add_new_video_surface(vo, rndr->surface, reserved_mpi, pts);
1473 return;
1476 // warning: the size and pixel format of surface must match that of the
1477 // surfaces in vc->output_surfaces
1478 static struct mp_image *read_output_surface(struct vdpctx *vc,
1479 VdpOutputSurface surface)
1481 VdpStatus vdp_st;
1482 struct vdp_functions *vdp = vc->vdp;
1483 struct mp_image *image = alloc_mpi(vc->output_surface_width,
1484 vc->output_surface_height, IMGFMT_BGR32);
1486 void *dst_planes[] = { image->planes[0] };
1487 uint32_t dst_pitches[] = { image->stride[0] };
1488 vdp_st = vdp->output_surface_get_bits_native(surface, NULL, dst_planes,
1489 dst_pitches);
1490 CHECK_ST_WARNING("Error when calling vdp_output_surface_get_bits_native");
1492 return image;
1495 static struct mp_image *get_screenshot(struct vo *vo)
1497 struct vdpctx *vc = vo->priv;
1499 VdpOutputSurface screenshot_surface =
1500 vc->output_surfaces[vc->num_output_surfaces];
1502 VdpRect rc = { .x1 = vc->vid_width, .y1 = vc->vid_height };
1503 render_video_to_output_surface(vo, screenshot_surface, &rc);
1505 struct mp_image *image = read_output_surface(vc, screenshot_surface);
1507 image->width = vc->vid_width;
1508 image->height = vc->vid_height;
1509 image->w = vc->vid_d_width;
1510 image->h = vc->vid_d_height;
1512 return image;
1515 static struct mp_image *get_window_screenshot(struct vo *vo)
1517 struct vdpctx *vc = vo->priv;
1518 int last_surface = WRAP_ADD(vc->surface_num, -1, vc->num_output_surfaces);
1519 VdpOutputSurface screen = vc->output_surfaces[last_surface];
1520 struct mp_image *image = read_output_surface(vo->priv, screen);
1521 image->width = image->w = vo->dwidth;
1522 image->height = image->h = vo->dheight;
1523 return image;
1526 static uint32_t get_image(struct vo *vo, mp_image_t *mpi)
1528 struct vdpctx *vc = vo->priv;
1529 struct vdpau_render_state *rndr;
1531 // no dr for non-decoding for now
1532 if (!IMGFMT_IS_VDPAU(vc->image_format))
1533 return VO_FALSE;
1534 if (mpi->type != MP_IMGTYPE_NUMBERED)
1535 return VO_FALSE;
1537 rndr = get_surface(vo, mpi->number);
1538 if (!rndr) {
1539 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in "
1540 "get_image\n");
1541 // TODO: this probably breaks things forever, provide a dummy buffer?
1542 return VO_FALSE;
1544 mpi->flags |= MP_IMGFLAG_DIRECT;
1545 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
1546 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
1547 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1548 mpi->planes[0] = (void *)rndr;
1549 mpi->num_planes = 1;
1550 mpi->priv = rndr;
1551 return VO_TRUE;
1554 static int query_format(uint32_t format)
1556 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW
1557 | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD
1558 | VFCAP_EOSD_UNSCALED | VFCAP_FLIP;
1559 switch (format) {
1560 case IMGFMT_YV12:
1561 case IMGFMT_I420:
1562 case IMGFMT_IYUV:
1563 case IMGFMT_NV12:
1564 case IMGFMT_YUY2:
1565 case IMGFMT_UYVY:
1566 return default_flags | VOCAP_NOSLICES;
1567 case IMGFMT_VDPAU_MPEG1:
1568 case IMGFMT_VDPAU_MPEG2:
1569 case IMGFMT_VDPAU_H264:
1570 case IMGFMT_VDPAU_WMV3:
1571 case IMGFMT_VDPAU_VC1:
1572 case IMGFMT_VDPAU_MPEG4:
1573 return default_flags;
1575 return 0;
1578 static void destroy_vdpau_objects(struct vo *vo)
1580 struct vdpctx *vc = vo->priv;
1581 struct vdp_functions *vdp = vc->vdp;
1583 int i;
1584 VdpStatus vdp_st;
1586 free_video_specific(vo);
1588 if (vc->flip_queue != VDP_INVALID_HANDLE) {
1589 vdp_st = vdp->presentation_queue_destroy(vc->flip_queue);
1590 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy");
1593 if (vc->flip_target != VDP_INVALID_HANDLE) {
1594 vdp_st = vdp->presentation_queue_target_destroy(vc->flip_target);
1595 CHECK_ST_WARNING("Error when calling "
1596 "vdp_presentation_queue_target_destroy");
1599 for (i = 0; i <= vc->num_output_surfaces; i++) {
1600 if (vc->output_surfaces[i] == VDP_INVALID_HANDLE)
1601 continue;
1602 vdp_st = vdp->output_surface_destroy(vc->output_surfaces[i]);
1603 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy");
1606 if (vc->eosd_surface.surface != VDP_INVALID_HANDLE) {
1607 vdp_st = vdp->bitmap_surface_destroy(vc->eosd_surface.surface);
1608 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1611 vdp_st = vdp->device_destroy(vc->vdp_device);
1612 CHECK_ST_WARNING("Error when calling vdp_device_destroy");
1615 static void uninit(struct vo *vo)
1617 struct vdpctx *vc = vo->priv;
1619 /* Destroy all vdpau objects */
1620 destroy_vdpau_objects(vo);
1622 #ifdef CONFIG_XF86VM
1623 if (vc->mode_switched)
1624 vo_vm_close(vo);
1625 #endif
1626 vo_x11_uninit(vo);
1628 // Free bitstream buffers allocated by FFmpeg
1629 for (int i = 0; i < MAX_VIDEO_SURFACES; i++)
1630 av_freep(&vc->surface_render[i].bitstream_buffers);
1633 static int preinit(struct vo *vo, const char *arg)
1635 struct vdpctx *vc = vo->priv;
1637 // Mark everything as invalid first so uninit() can tell what has been
1638 // allocated
1639 mark_vdpau_objects_uninitialized(vo);
1641 vc->colorspace = (struct mp_csp_details) MP_CSP_DETAILS_DEFAULTS;
1642 vc->video_eq.capabilities = MP_CSP_EQ_CAPS_COLORMATRIX;
1644 vc->deint_type = vc->deint ? FFABS(vc->deint) : 3;
1645 if (vc->deint < 0)
1646 vc->deint = 0;
1648 if (!vo_init(vo))
1649 return -1;
1651 // After this calling uninit() should work to free resources
1653 if (win_x11_init_vdpau_procs(vo) < 0) {
1654 if (vc->vdp->device_destroy)
1655 vc->vdp->device_destroy(vc->vdp_device);
1656 vo_x11_uninit(vo);
1657 return -1;
1660 // full grayscale palette.
1661 for (int i = 0; i < PALETTE_SIZE; ++i)
1662 vc->palette[i] = (i << 16) | (i << 8) | i;
1664 return 0;
1667 static int get_equalizer(struct vo *vo, const char *name, int *value)
1669 struct vdpctx *vc = vo->priv;
1670 return mp_csp_equalizer_get(&vc->video_eq, name, value) >= 0 ?
1671 VO_TRUE : VO_NOTIMPL;
1674 static bool status_ok(struct vo *vo)
1676 if (!vo->config_ok || handle_preemption(vo) < 0)
1677 return false;
1678 return true;
1681 static int set_equalizer(struct vo *vo, const char *name, int value)
1683 struct vdpctx *vc = vo->priv;
1685 if (mp_csp_equalizer_set(&vc->video_eq, name, value) < 0)
1686 return VO_NOTIMPL;
1688 if (status_ok(vo))
1689 update_csc_matrix(vo);
1690 return true;
1693 static void checked_resize(struct vo *vo)
1695 if (!status_ok(vo))
1696 return;
1697 resize(vo);
1700 static int control(struct vo *vo, uint32_t request, void *data)
1702 struct vdpctx *vc = vo->priv;
1703 struct vdp_functions *vdp = vc->vdp;
1705 handle_preemption(vo);
1707 switch (request) {
1708 case VOCTRL_GET_DEINTERLACE:
1709 *(int *)data = vc->deint;
1710 return VO_TRUE;
1711 case VOCTRL_SET_DEINTERLACE:
1712 vc->deint = *(int *)data;
1713 if (vc->deint)
1714 vc->deint = vc->deint_type;
1715 if (vc->deint_type > 2 && status_ok(vo)) {
1716 VdpStatus vdp_st;
1717 VdpVideoMixerFeature features[1] =
1718 {vc->deint_type == 3 ?
1719 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL :
1720 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL};
1721 VdpBool feature_enables[1] = {vc->deint ? VDP_TRUE : VDP_FALSE};
1722 vdp_st = vdp->video_mixer_set_feature_enables(vc->video_mixer,
1723 1, features,
1724 feature_enables);
1725 CHECK_ST_WARNING("Error changing deinterlacing settings");
1727 vo->want_redraw = true;
1728 return VO_TRUE;
1729 case VOCTRL_PAUSE:
1730 if (vc->dropped_frame)
1731 vo->want_redraw = true;
1732 return true;
1733 case VOCTRL_QUERY_FORMAT:
1734 return query_format(*(uint32_t *)data);
1735 case VOCTRL_GET_IMAGE:
1736 return get_image(vo, data);
1737 case VOCTRL_DRAW_IMAGE:
1738 abort(); // draw_image() should get called directly
1739 case VOCTRL_BORDER:
1740 vo_x11_border(vo);
1741 checked_resize(vo);
1742 return VO_TRUE;
1743 case VOCTRL_FULLSCREEN:
1744 vo_x11_fullscreen(vo);
1745 checked_resize(vo);
1746 return VO_TRUE;
1747 case VOCTRL_GET_PANSCAN:
1748 return VO_TRUE;
1749 case VOCTRL_SET_PANSCAN:
1750 checked_resize(vo);
1751 return VO_TRUE;
1752 case VOCTRL_SET_EQUALIZER: {
1753 vo->want_redraw = true;
1754 struct voctrl_set_equalizer_args *args = data;
1755 return set_equalizer(vo, args->name, args->value);
1757 case VOCTRL_GET_EQUALIZER: {
1758 struct voctrl_get_equalizer_args *args = data;
1759 return get_equalizer(vo, args->name, args->valueptr);
1761 case VOCTRL_SET_YUV_COLORSPACE:
1762 vc->colorspace = *(struct mp_csp_details *)data;
1763 if (status_ok(vo))
1764 update_csc_matrix(vo);
1765 vo->want_redraw = true;
1766 return true;
1767 case VOCTRL_GET_YUV_COLORSPACE:
1768 *(struct mp_csp_details *)data = vc->colorspace;
1769 return true;
1770 case VOCTRL_ONTOP:
1771 vo_x11_ontop(vo);
1772 return VO_TRUE;
1773 case VOCTRL_UPDATE_SCREENINFO:
1774 update_xinerama_info(vo);
1775 return VO_TRUE;
1776 case VOCTRL_DRAW_EOSD:
1777 if (!data)
1778 return VO_FALSE;
1779 if (status_ok(vo)) {
1780 generate_eosd(vo, data);
1781 draw_eosd(vo);
1783 return VO_TRUE;
1784 case VOCTRL_GET_EOSD_RES: {
1785 struct mp_eosd_res *r = data;
1786 r->w = vo->dwidth;
1787 r->h = vo->dheight;
1788 r->ml = r->mr = vc->border_x;
1789 r->mt = r->mb = vc->border_y;
1790 return VO_TRUE;
1792 case VOCTRL_NEWFRAME:
1793 vc->deint_queue_pos = next_deint_queue_pos(vo, true);
1794 if (status_ok(vo))
1795 video_to_output_surface(vo);
1796 return true;
1797 case VOCTRL_SKIPFRAME:
1798 vc->deint_queue_pos = next_deint_queue_pos(vo, true);
1799 return true;
1800 case VOCTRL_REDRAW_FRAME:
1801 if (status_ok(vo))
1802 video_to_output_surface(vo);
1803 return true;
1804 case VOCTRL_RESET:
1805 forget_frames(vo);
1806 return true;
1807 case VOCTRL_SCREENSHOT: {
1808 if (!status_ok(vo))
1809 return false;
1810 struct voctrl_screenshot_args *args = data;
1811 if (args->full_window)
1812 args->out_image = get_window_screenshot(vo);
1813 else
1814 args->out_image = get_screenshot(vo);
1815 return true;
1818 return VO_NOTIMPL;
1821 #undef OPT_BASE_STRUCT
1822 #define OPT_BASE_STRUCT struct vdpctx
1824 const struct vo_driver video_out_vdpau = {
1825 .is_new = true,
1826 .buffer_frames = true,
1827 .info = &(const struct vo_info_s){
1828 "VDPAU with X11",
1829 "vdpau",
1830 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
1833 .preinit = preinit,
1834 .config = config,
1835 .control = control,
1836 .draw_image = draw_image,
1837 .get_buffered_frame = set_next_frame_info,
1838 .draw_slice = draw_slice,
1839 .draw_osd = draw_osd,
1840 .flip_page_timed = flip_page_timed,
1841 .check_events = check_events,
1842 .uninit = uninit,
1843 .privsize = sizeof(struct vdpctx),
1844 .options = (const struct m_option []){
1845 OPT_INTRANGE("deint", deint, 0, -4, 4),
1846 OPT_FLAG_ON("chroma-deint", chroma_deint, 0, OPTDEF_INT(1)),
1847 OPT_FLAG_OFF("nochroma-deint", chroma_deint, 0),
1848 OPT_MAKE_FLAGS("pullup", pullup, 0),
1849 OPT_FLOATRANGE("denoise", denoise, 0, 0, 1),
1850 OPT_FLOATRANGE("sharpen", sharpen, 0, -1, 1),
1851 OPT_ERRORMESSAGE("colorspace", "vo_vdpau suboption \"colorspace\" has "
1852 "been removed. Use --colormatrix instead.\n"),
1853 OPT_ERRORMESSAGE("studio", "vo_vdpau suboption \"studio\" has been "
1854 "removed. Use --colormatrix-output-range=limited "
1855 "instead.\n"),
1856 OPT_INTRANGE("hqscaling", hqscaling, 0, 0, 9),
1857 OPT_FLOAT("fps", user_fps, 0),
1858 OPT_FLAG_ON("composite-detect", composite_detect, 0, OPTDEF_INT(1)),
1859 OPT_INT("queuetime_windowed", flip_offset_window, 0, OPTDEF_INT(50)),
1860 OPT_INT("queuetime_fs", flip_offset_fs, 0, OPTDEF_INT(50)),
1861 OPT_INTRANGE("output_surfaces", num_output_surfaces, 0,
1862 2, MAX_OUTPUT_SURFACES, OPTDEF_INT(3)),
1863 {NULL},