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.
41 #include "video_out.h"
42 #include "x11_common.h"
45 #include "subopt-helper.h"
46 #include "libmpcodecs/vfcap.h"
47 #include "libmpcodecs/mp_image.h"
48 #include "osdep/timer.h"
50 #include "libavcodec/vdpau.h"
52 #include "font_load.h"
54 #include "libavutil/common.h"
55 #include "libavutil/mathematics.h"
59 #define WRAP_ADD(x, a, m) ((a) < 0 \
60 ? ((x)+(a)+(m) < (m) ? (x)+(a)+(m) : (x)+(a)) \
61 : ((x)+(a) < (m) ? (x)+(a) : (x)+(a)-(m)))
63 #define CHECK_ST_ERROR(message) \
65 if (vdp_st != VDP_STATUS_OK) { \
66 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
67 message, vdp->get_error_string(vdp_st)); \
72 #define CHECK_ST_WARNING(message) \
74 if (vdp_st != VDP_STATUS_OK) \
75 mp_msg(MSGT_VO, MSGL_WARN, "[ vdpau] %s: %s\n", \
76 message, vdp->get_error_string(vdp_st)); \
79 /* number of video and output surfaces */
80 #define MAX_OUTPUT_SURFACES 15
81 #define MAX_VIDEO_SURFACES 50
82 #define NUM_BUFFERED_VIDEO 4
84 /* number of palette entries */
85 #define PALETTE_SIZE 256
87 /* Initial size of EOSD surface in pixels (x*x) */
88 #define EOSD_SURFACE_INITIAL_SIZE 256
91 * Global variable declaration - VDPAU specific
94 struct vdp_functions
{
95 #define VDP_FUNCTION(vdp_type, _, mp_name) vdp_type *mp_name;
96 #include "vdpau_template.c"
101 struct vdp_functions
*vdp
;
103 VdpDevice vdp_device
;
105 bool preemption_acked
;
106 bool preemption_user_notified
;
107 unsigned int last_preemption_retry_fail
;
108 VdpGetProcAddress
*vdp_get_proc_address
;
110 VdpPresentationQueueTarget flip_target
;
111 VdpPresentationQueue flip_queue
;
112 uint64_t last_vdp_time
;
113 unsigned int last_sync_update
;
115 /* an extra last output surface is misused for OSD. */
116 VdpOutputSurface output_surfaces
[MAX_OUTPUT_SURFACES
+ 1];
117 int num_output_surfaces
;
118 struct buffered_video_surface
{
119 VdpVideoSurface surface
;
122 } buffered_video
[NUM_BUFFERED_VIDEO
];
124 int output_surface_width
, output_surface_height
;
126 VdpVideoMixer video_mixer
;
138 int flip_offset_window
;
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
];
152 int query_surface_num
;
153 VdpTime recent_vsync_time
;
155 unsigned int vsync_interval
;
156 uint64_t last_queue_time
;
157 uint64_t queue_time
[MAX_OUTPUT_SURFACES
];
158 uint64_t last_ideal_time
;
160 uint64_t dropped_time
;
161 uint32_t vid_width
, vid_height
;
162 uint32_t image_format
;
163 VdpChromaType vdp_chroma_type
;
164 VdpYCbCrFormat vdp_pixel_format
;
167 unsigned char *index_data
;
169 uint32_t palette
[PALETTE_SIZE
];
173 struct eosd_bitmap_surface
{
174 VdpBitmapSurface surface
;
181 // List of surfaces to be rendered
187 int eosd_targets_size
;
190 int eosd_render_count
;
195 int num_shown_frames
;
198 // These tell what's been initialized and uninit() should free/uninitialize
202 static int change_vdptime_sync(struct vdpctx
*vc
, unsigned int *t
)
204 struct vdp_functions
*vdp
= vc
->vdp
;
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;
213 if (vdp_time
> old
+ (t2
- t1
) * 1000ULL)
214 vdp_time
-= (t2
- t1
) * 1000ULL;
217 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] adjusting VdpTime offset by %f µs\n",
218 (int64_t)(vdp_time
- old
) / 1000.);
219 vc
->last_vdp_time
= vdp_time
;
220 vc
->last_sync_update
= t1
;
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
);
238 static uint64_t convert_to_vdptime(struct vo
*vo
, unsigned int t
)
240 struct vdpctx
*vc
= vo
->priv
;
241 return (int)(t
- vc
->last_sync_update
) * 1000LL + vc
->last_vdp_time
;
244 static void flip_page_timed(struct vo
*vo
, unsigned int pts_us
, int duration
);
246 static int video_to_output_surface(struct vo
*vo
)
248 struct vdpctx
*vc
= vo
->priv
;
249 struct vdp_functions
*vdp
= vc
->vdp
;
252 if (vc
->deint_queue_pos
< 0)
255 struct buffered_video_surface
*bv
= vc
->buffered_video
;
256 int field
= VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME
;
257 unsigned int dp
= vc
->deint_queue_pos
;
258 // dp==0 means last field of latest frame, 1 earlier field of latest frame,
259 // 2 last field of previous frame and so on
261 field
= vc
->top_field_first
^ (dp
& 1) ?
262 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD
:
263 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD
;
265 const VdpVideoSurface
*past_fields
= (const VdpVideoSurface
[]){
266 bv
[(dp
+1)/2].surface
, bv
[(dp
+2)/2].surface
};
267 const VdpVideoSurface
*future_fields
= (const VdpVideoSurface
[]){
268 dp
>= 1 ? bv
[(dp
-1)/2].surface
: VDP_INVALID_HANDLE
};
269 VdpOutputSurface output_surface
= vc
->output_surfaces
[vc
->surface_num
];
270 vdp_st
= vdp
->presentation_queue_block_until_surface_idle(vc
->flip_queue
,
273 CHECK_ST_WARNING("Error when calling "
274 "vdp_presentation_queue_block_until_surface_idle");
276 vdp_st
= vdp
->video_mixer_render(vc
->video_mixer
, VDP_INVALID_HANDLE
,
277 0, field
, 2, past_fields
,
278 bv
[dp
/2].surface
, 1, future_fields
,
279 &vc
->src_rect_vid
, output_surface
,
280 NULL
, &vc
->out_rect_vid
, 0, NULL
);
281 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render");
285 static void get_buffered_frame(struct vo
*vo
, bool eof
)
287 struct vdpctx
*vc
= vo
->priv
;
289 int dqp
= vc
->deint_queue_pos
;
293 dqp
= vc
->deint
>= 2 ? dqp
- 1 : dqp
- 2 | 1;
294 if (dqp
< (eof
? 0 : 3))
298 vc
->deint_queue_pos
= dqp
;
299 vo
->frame_loaded
= true;
302 struct buffered_video_surface
*bv
= vc
->buffered_video
;
303 int idx
= vc
->deint_queue_pos
>> 1;
304 if (idx
== 0) { // no future frame/pts available
305 vo
->next_pts
= bv
[0].pts
;
306 vo
->next_pts2
= MP_NOPTS_VALUE
;
307 } else if (!(vc
->deint
>= 2)) { // no field-splitting deinterlace
308 vo
->next_pts
= bv
[idx
].pts
;
309 vo
->next_pts2
= bv
[idx
- 1].pts
;
310 } else { // deinterlace with separate fields
311 double intermediate_pts
;
312 double diff
= bv
[idx
- 1].pts
- bv
[idx
].pts
;
313 if (diff
> 0 && diff
< 0.5)
314 intermediate_pts
= (bv
[idx
].pts
+ bv
[idx
- 1].pts
) / 2;
316 intermediate_pts
= bv
[idx
].pts
;
317 if (vc
->deint_queue_pos
& 1) { // first field
318 vo
->next_pts
= bv
[idx
].pts
;
319 vo
->next_pts2
= intermediate_pts
;
321 vo
->next_pts
= intermediate_pts
;
322 vo
->next_pts2
= bv
[idx
- 1].pts
;
326 video_to_output_surface(vo
);
329 static void add_new_video_surface(struct vo
*vo
, VdpVideoSurface surface
,
330 struct mp_image
*reserved_mpi
, double pts
)
332 struct vdpctx
*vc
= vo
->priv
;
333 struct buffered_video_surface
*bv
= vc
->buffered_video
;
336 reserved_mpi
->usage_count
++;
337 if (bv
[NUM_BUFFERED_VIDEO
- 1].mpi
)
338 bv
[NUM_BUFFERED_VIDEO
- 1].mpi
->usage_count
--;
340 for (int i
= NUM_BUFFERED_VIDEO
- 1; i
> 0; i
--)
342 bv
[0] = (struct buffered_video_surface
){
348 vc
->deint_queue_pos
+= 2;
349 get_buffered_frame(vo
, false);
352 static void forget_frames(struct vo
*vo
)
354 struct vdpctx
*vc
= vo
->priv
;
356 vc
->deint_queue_pos
= -1001;
357 vc
->dropped_frame
= false;
358 for (int i
= 0; i
< NUM_BUFFERED_VIDEO
; i
++) {
359 struct buffered_video_surface
*p
= vc
->buffered_video
+ i
;
361 p
->mpi
->usage_count
--;
362 *p
= (struct buffered_video_surface
){
363 .surface
= VDP_INVALID_HANDLE
,
368 static void resize(struct vo
*vo
)
370 struct vdpctx
*vc
= vo
->priv
;
371 struct vdp_functions
*vdp
= vc
->vdp
;
374 struct vo_rect src_rect
;
375 struct vo_rect dst_rect
;
376 struct vo_rect borders
;
377 calc_src_dst_rects(vo
, vc
->vid_width
, vc
->vid_height
, &src_rect
, &dst_rect
,
379 vc
->out_rect_vid
.x0
= dst_rect
.left
;
380 vc
->out_rect_vid
.x1
= dst_rect
.right
;
381 vc
->out_rect_vid
.y0
= dst_rect
.top
;
382 vc
->out_rect_vid
.y1
= dst_rect
.bottom
;
383 vc
->src_rect_vid
.x0
= src_rect
.left
;
384 vc
->src_rect_vid
.x1
= src_rect
.right
;
385 vc
->src_rect_vid
.y0
= vc
->flip
? src_rect
.bottom
: src_rect
.top
;
386 vc
->src_rect_vid
.y1
= vc
->flip
? src_rect
.top
: src_rect
.bottom
;
387 vc
->border_x
= borders
.left
;
388 vc
->border_y
= borders
.top
;
389 #ifdef CONFIG_FREETYPE
390 // adjust font size to display size
393 vo_osd_changed(OSDTYPE_OSD
);
394 int flip_offset_ms
= vo_fs
? vc
->flip_offset_fs
: vc
->flip_offset_window
;
395 vo
->flip_queue_offset
= flip_offset_ms
/ 1000.;
397 bool had_frames
= vc
->num_shown_frames
;
398 if (vc
->output_surface_width
< vo
->dwidth
399 || vc
->output_surface_height
< vo
->dheight
) {
400 if (vc
->output_surface_width
< vo
->dwidth
) {
401 vc
->output_surface_width
+= vc
->output_surface_width
>> 1;
402 vc
->output_surface_width
= FFMAX(vc
->output_surface_width
,
405 if (vc
->output_surface_height
< vo
->dheight
) {
406 vc
->output_surface_height
+= vc
->output_surface_height
>> 1;
407 vc
->output_surface_height
= FFMAX(vc
->output_surface_height
,
410 // Creation of output_surfaces
411 for (i
= 0; i
<= vc
->num_output_surfaces
; i
++) {
412 if (vc
->output_surfaces
[i
] != VDP_INVALID_HANDLE
) {
413 vdp_st
= vdp
->output_surface_destroy(vc
->output_surfaces
[i
]);
414 CHECK_ST_WARNING("Error when calling "
415 "vdp_output_surface_destroy");
417 vdp_st
= vdp
->output_surface_create(vc
->vdp_device
,
418 VDP_RGBA_FORMAT_B8G8R8A8
,
419 vc
->output_surface_width
,
420 vc
->output_surface_height
,
421 &vc
->output_surfaces
[i
]);
422 CHECK_ST_WARNING("Error when calling vdp_output_surface_create");
423 mp_msg(MSGT_VO
, MSGL_DBG2
, "OUT CREATE: %u\n",
424 vc
->output_surfaces
[i
]);
426 vc
->num_shown_frames
= 0;
428 if (vc
->paused
&& had_frames
)
429 if (video_to_output_surface(vo
) >= 0)
430 flip_page_timed(vo
, 0, -1);
433 static void preemption_callback(VdpDevice device
, void *context
)
435 struct vdpctx
*vc
= context
;
436 vc
->is_preempted
= true;
437 vc
->preemption_acked
= false;
440 /* Initialize vdp_get_proc_address, called from preinit() */
441 static int win_x11_init_vdpau_procs(struct vo
*vo
)
443 struct vo_x11_state
*x11
= vo
->x11
;
444 struct vdpctx
*vc
= vo
->priv
;
445 talloc_free(vc
->vdp
); // In case this is reinitialization after preemption
446 struct vdp_functions
*vdp
= talloc_zero(vc
, struct vdp_functions
);
450 struct vdp_function
{
455 const struct vdp_function
*dsc
;
457 static const struct vdp_function vdp_func
[] = {
458 #define VDP_FUNCTION(_, macro_name, mp_name) {macro_name, offsetof(struct vdp_functions, mp_name)},
459 #include "vdpau_template.c"
464 vdp_st
= vdp_device_create_x11(x11
->display
, x11
->screen
,&vc
->vdp_device
,
465 &vc
->vdp_get_proc_address
);
466 if (vdp_st
!= VDP_STATUS_OK
) {
467 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Error when calling "
468 "vdp_device_create_x11: %i\n", vdp_st
);
472 vdp
->get_error_string
= NULL
;
473 for (dsc
= vdp_func
; dsc
->offset
>= 0; dsc
++) {
474 vdp_st
= vc
->vdp_get_proc_address(vc
->vdp_device
, dsc
->id
,
475 (void **)((char *)vdp
+ dsc
->offset
));
476 if (vdp_st
!= VDP_STATUS_OK
) {
477 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Error when calling "
478 "vdp_get_proc_address(function id %d): %s\n", dsc
->id
,
479 vdp
->get_error_string
? vdp
->get_error_string(vdp_st
) : "?");
483 vdp_st
= vdp
->preemption_callback_register(vc
->vdp_device
,
484 preemption_callback
, vc
);
488 static int win_x11_init_vdpau_flip_queue(struct vo
*vo
)
490 struct vdpctx
*vc
= vo
->priv
;
491 struct vdp_functions
*vdp
= vc
->vdp
;
492 struct vo_x11_state
*x11
= vo
->x11
;
495 if (vc
->flip_target
== VDP_INVALID_HANDLE
) {
496 vdp_st
= vdp
->presentation_queue_target_create_x11(vc
->vdp_device
,
499 CHECK_ST_ERROR("Error when calling "
500 "vdp_presentation_queue_target_create_x11");
503 /* Emperically this seems to be the first call which fails when we
504 * try to reinit after preemption while the user is still switched
505 * from X to a virtual terminal (creating the vdp_device initially
506 * succeeds, as does creating the flip_target above). This is
507 * probably not guaranteed behavior, but we'll assume it as a simple
508 * way to reduce warnings while trying to recover from preemption.
510 if (vc
->flip_queue
== VDP_INVALID_HANDLE
) {
511 vdp_st
= vdp
->presentation_queue_create(vc
->vdp_device
, vc
->flip_target
,
513 if (vc
->is_preempted
&& vdp_st
!= VDP_STATUS_OK
) {
514 mp_msg(MSGT_VO
, MSGL_DBG2
, "[vdpau] Failed to create flip queue "
515 "while preempted: %s\n", vdp
->get_error_string(vdp_st
));
518 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create");
522 vdp_st
= vdp
->presentation_queue_get_time(vc
->flip_queue
, &vdp_time
);
523 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_get_time");
524 vc
->last_vdp_time
= vdp_time
;
525 vc
->last_sync_update
= GetTimer();
527 vc
->vsync_interval
= 1;
528 if (vc
->user_fps
> 0) {
529 vc
->vsync_interval
= 1e9
/ vc
->user_fps
;
530 mp_msg(MSGT_VO
, MSGL_INFO
, "[vdpau] Assuming user-specified display "
531 "refresh rate of %.3f Hz.\n", vc
->user_fps
);
532 } else if (vc
->user_fps
== 0) {
534 double fps
= vo_vm_get_fps(vo
);
536 mp_msg(MSGT_VO
, MSGL_WARN
, "[vdpau] Failed to get display FPS\n");
538 vc
->vsync_interval
= 1e9
/ fps
;
539 // This is verbose, but I'm not yet sure how common wrong values are
540 mp_msg(MSGT_VO
, MSGL_INFO
,
541 "[vdpau] Got display refresh rate %.3f Hz.\n"
542 "[vdpau] If that value looks wrong give the "
543 "-vo vdpau:fps=X suboption manually.\n", fps
);
546 mp_msg(MSGT_VO
, MSGL_INFO
, "[vdpau] This binary has been compiled "
547 "without XF86VidMode support.\n");
548 mp_msg(MSGT_VO
, MSGL_INFO
, "[vdpau] Can't use vsync-aware timing "
549 "without manually provided -vo vdpau:fps=X suboption.\n");
552 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] framedrop/timing logic disabled by "
558 static int set_video_attribute(struct vdpctx
*vc
, VdpVideoMixerAttribute attr
,
559 const void *value
, char *attr_name
)
561 struct vdp_functions
*vdp
= vc
->vdp
;
564 vdp_st
= vdp
->video_mixer_set_attribute_values(vc
->video_mixer
, 1, &attr
,
566 if (vdp_st
!= VDP_STATUS_OK
) {
567 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Error setting video mixer "
568 "attribute %s: %s\n", attr_name
, vdp
->get_error_string(vdp_st
));
574 static void update_csc_matrix(struct vo
*vo
)
576 struct vdpctx
*vc
= vo
->priv
;
577 struct vdp_functions
*vdp
= vc
->vdp
;
580 const VdpColorStandard vdp_colors
[] = {VDP_COLOR_STANDARD_ITUR_BT_601
,
581 VDP_COLOR_STANDARD_ITUR_BT_709
,
582 VDP_COLOR_STANDARD_SMPTE_240M
};
583 char * const vdp_names
[] = {"BT.601", "BT.709", "SMPTE-240M"};
584 int csp
= vc
->colorspace
;
585 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] Updating CSC matrix for %s\n",
589 vdp_st
= vdp
->generate_csc_matrix(&vc
->procamp
, vdp_colors
[csp
], &matrix
);
590 CHECK_ST_WARNING("Error when generating CSC matrix");
592 if (vc
->studio_levels
) {
593 /* Modify matrix to change output range from 0..255 to 16..235.
594 * Clipping limits can't be changed, so out-of-range results that
595 * would have been clipped to 0 or 255 before can still go below
598 for (int i
= 0; i
< 3; i
++) {
599 for (int j
= 0; j
< 4; j
++)
600 matrix
[i
][j
] *= 220. / 256;
601 matrix
[i
][3] += 16. / 256;
605 set_video_attribute(vc
, VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX
,
606 &matrix
, "CSC matrix");
609 #define SET_VIDEO_ATTR(attr_name, attr_type, value) set_video_attribute(vc, \
610 VDP_VIDEO_MIXER_ATTRIBUTE_ ## attr_name, &(attr_type){value},\
612 static int create_vdp_mixer(struct vo
*vo
, VdpChromaType vdp_chroma_type
)
614 struct vdpctx
*vc
= vo
->priv
;
615 struct vdp_functions
*vdp
= vc
->vdp
;
616 #define VDP_NUM_MIXER_PARAMETER 3
617 #define MAX_NUM_FEATURES 6
621 if (vc
->video_mixer
!= VDP_INVALID_HANDLE
)
624 int feature_count
= 0;
625 VdpVideoMixerFeature features
[MAX_NUM_FEATURES
];
626 VdpBool feature_enables
[MAX_NUM_FEATURES
];
627 static const VdpVideoMixerParameter parameters
[VDP_NUM_MIXER_PARAMETER
] = {
628 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH
,
629 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT
,
630 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
,
632 const void *const parameter_values
[VDP_NUM_MIXER_PARAMETER
] = {
637 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL
;
639 features
[feature_count
++] =
640 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL
;
642 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE
;
644 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION
;
646 features
[feature_count
++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS
;
648 VdpVideoMixerFeature hqscaling_feature
=
649 VDP_VIDEO_MIXER_FEATURE_HIGH_QUALITY_SCALING_L1
+ vc
->hqscaling
-1;
650 VdpBool hqscaling_available
;
651 vdp_st
= vdp
->video_mixer_query_feature_support(vc
->vdp_device
,
653 &hqscaling_available
);
654 CHECK_ST_ERROR("Error when calling video_mixer_query_feature_support");
655 if (hqscaling_available
)
656 features
[feature_count
++] = hqscaling_feature
;
658 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Your hardware or VDPAU "
659 "library does not support requested hqscaling.\n");
662 vdp_st
= vdp
->video_mixer_create(vc
->vdp_device
, feature_count
, features
,
663 VDP_NUM_MIXER_PARAMETER
,
664 parameters
, parameter_values
,
666 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create");
668 for (i
= 0; i
< feature_count
; i
++)
669 feature_enables
[i
] = VDP_TRUE
;
671 feature_enables
[0] = VDP_FALSE
;
673 vdp_st
= vdp
->video_mixer_set_feature_enables(vc
->video_mixer
,
674 feature_count
, features
,
676 CHECK_ST_WARNING("Error calling vdp_video_mixer_set_feature_enables");
679 SET_VIDEO_ATTR(NOISE_REDUCTION_LEVEL
, float, vc
->denoise
);
681 SET_VIDEO_ATTR(SHARPNESS_LEVEL
, float, vc
->sharpen
);
682 if (!vc
->chroma_deint
)
683 SET_VIDEO_ATTR(SKIP_CHROMA_DEINTERLACE
, uint8_t, 1);
685 update_csc_matrix(vo
);
689 // Free everything specific to a certain video file
690 static void free_video_specific(struct vo
*vo
)
692 struct vdpctx
*vc
= vo
->priv
;
693 struct vdp_functions
*vdp
= vc
->vdp
;
697 if (vc
->decoder
!= VDP_INVALID_HANDLE
)
698 vdp
->decoder_destroy(vc
->decoder
);
699 vc
->decoder
= VDP_INVALID_HANDLE
;
700 vc
->decoder_max_refs
= -1;
704 for (i
= 0; i
< MAX_VIDEO_SURFACES
; i
++) {
705 if (vc
->surface_render
[i
].surface
!= VDP_INVALID_HANDLE
) {
706 vdp_st
= vdp
->video_surface_destroy(vc
->surface_render
[i
].surface
);
707 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy");
709 vc
->surface_render
[i
].surface
= VDP_INVALID_HANDLE
;
712 if (vc
->video_mixer
!= VDP_INVALID_HANDLE
) {
713 vdp_st
= vdp
->video_mixer_destroy(vc
->video_mixer
);
714 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy");
716 vc
->video_mixer
= VDP_INVALID_HANDLE
;
719 static int create_vdp_decoder(struct vo
*vo
, int max_refs
)
721 struct vdpctx
*vc
= vo
->priv
;
722 struct vdp_functions
*vdp
= vc
->vdp
;
724 VdpDecoderProfile vdp_decoder_profile
;
725 if (vc
->decoder
!= VDP_INVALID_HANDLE
)
726 vdp
->decoder_destroy(vc
->decoder
);
727 switch (vc
->image_format
) {
728 case IMGFMT_VDPAU_MPEG1
:
729 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG1
;
731 case IMGFMT_VDPAU_MPEG2
:
732 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG2_MAIN
;
734 case IMGFMT_VDPAU_H264
:
735 vdp_decoder_profile
= VDP_DECODER_PROFILE_H264_HIGH
;
736 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] Creating H264 hardware decoder "
737 "for %d reference frames.\n", max_refs
);
739 case IMGFMT_VDPAU_WMV3
:
740 vdp_decoder_profile
= VDP_DECODER_PROFILE_VC1_MAIN
;
742 case IMGFMT_VDPAU_VC1
:
743 vdp_decoder_profile
= VDP_DECODER_PROFILE_VC1_ADVANCED
;
745 case IMGFMT_VDPAU_MPEG4
:
746 vdp_decoder_profile
= VDP_DECODER_PROFILE_MPEG4_PART2_ASP
;
749 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] Unknown image format!\n");
752 vdp_st
= vdp
->decoder_create(vc
->vdp_device
, vdp_decoder_profile
,
753 vc
->vid_width
, vc
->vid_height
, max_refs
,
755 CHECK_ST_WARNING("Failed creating VDPAU decoder");
756 if (vdp_st
!= VDP_STATUS_OK
) {
758 vc
->decoder
= VDP_INVALID_HANDLE
;
759 vc
->decoder_max_refs
= 0;
762 vc
->decoder_max_refs
= max_refs
;
766 static int initialize_vdpau_objects(struct vo
*vo
)
768 struct vdpctx
*vc
= vo
->priv
;
769 struct vdp_functions
*vdp
= vc
->vdp
;
772 vc
->vdp_chroma_type
= VDP_CHROMA_TYPE_420
;
773 switch (vc
->image_format
) {
777 vc
->vdp_pixel_format
= VDP_YCBCR_FORMAT_YV12
;
780 vc
->vdp_pixel_format
= VDP_YCBCR_FORMAT_NV12
;
783 vc
->vdp_pixel_format
= VDP_YCBCR_FORMAT_YUYV
;
784 vc
->vdp_chroma_type
= VDP_CHROMA_TYPE_422
;
787 vc
->vdp_pixel_format
= VDP_YCBCR_FORMAT_UYVY
;
788 vc
->vdp_chroma_type
= VDP_CHROMA_TYPE_422
;
790 if (win_x11_init_vdpau_flip_queue(vo
) < 0)
793 if (create_vdp_mixer(vo
, vc
->vdp_chroma_type
) < 0)
797 bitmap_surface_query_capabilities(vc
->vdp_device
,
800 &vc
->eosd_surface
.max_width
,
801 &vc
->eosd_surface
.max_height
);
802 CHECK_ST_WARNING("Query to get max EOSD surface size failed");
808 static void mark_vdpau_objects_uninitialized(struct vo
*vo
)
810 struct vdpctx
*vc
= vo
->priv
;
812 vc
->decoder
= VDP_INVALID_HANDLE
;
813 for (int i
= 0; i
< MAX_VIDEO_SURFACES
; i
++)
814 vc
->surface_render
[i
].surface
= VDP_INVALID_HANDLE
;
816 vc
->video_mixer
= VDP_INVALID_HANDLE
;
817 vc
->flip_queue
= VDP_INVALID_HANDLE
;
818 vc
->flip_target
= VDP_INVALID_HANDLE
;
819 for (int i
= 0; i
<= MAX_OUTPUT_SURFACES
; i
++)
820 vc
->output_surfaces
[i
] = VDP_INVALID_HANDLE
;
821 vc
->vdp_device
= VDP_INVALID_HANDLE
;
822 vc
->eosd_surface
= (struct eosd_bitmap_surface
){
823 .surface
= VDP_INVALID_HANDLE
,
825 vc
->output_surface_width
= vc
->output_surface_height
= -1;
826 vc
->eosd_render_count
= 0;
827 vc
->num_shown_frames
= 0;
830 static int handle_preemption(struct vo
*vo
)
832 struct vdpctx
*vc
= vo
->priv
;
834 if (!vc
->is_preempted
)
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)
849 if (win_x11_init_vdpau_procs(vo
) < 0 || initialize_vdpau_objects(vo
) < 0) {
850 vc
->last_preemption_retry_fail
= GetTimerMS() | 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");
861 * connect to X server, create and map window, initialize all
862 * VDPAU objects, create different surfaces etc.
864 static int config(struct vo
*vo
, uint32_t width
, uint32_t height
,
865 uint32_t d_width
, uint32_t d_height
, uint32_t flags
,
866 char *title
, uint32_t format
)
868 struct vdpctx
*vc
= vo
->priv
;
869 struct vo_x11_state
*x11
= vo
->x11
;
871 XSetWindowAttributes xswa
;
872 XWindowAttributes attribs
;
873 unsigned long xswamask
;
877 int vm
= flags
& VOFLAG_MODESWITCHING
;
880 if (handle_preemption(vo
) < 0)
883 vc
->flip
= flags
& VOFLAG_FLIPPING
;
884 vc
->image_format
= format
;
885 vc
->vid_width
= width
;
886 vc
->vid_height
= height
;
887 if (vc
->user_colorspace
== 0)
888 vc
->colorspace
= width
>= 1280 || height
> 576 ? 1 : 0;
890 vc
->colorspace
= vc
->user_colorspace
- 1;
891 free_video_specific(vo
);
892 if (IMGFMT_IS_VDPAU(vc
->image_format
) && !create_vdp_decoder(vo
, 2))
898 vc
->mode_switched
= true;
901 XGetWindowAttributes(x11
->display
, DefaultRootWindow(x11
->display
),
903 depth
= attribs
.depth
;
904 if (depth
!= 15 && depth
!= 16 && depth
!= 24 && depth
!= 32)
906 XMatchVisualInfo(x11
->display
, x11
->screen
, depth
, TrueColor
, &vinfo
);
908 xswa
.background_pixel
= 0;
909 xswa
.border_pixel
= 0;
910 /* Do not use CWBackPixel: It leads to VDPAU errors after
911 * aspect ratio changes. */
912 xswamask
= CWBorderPixel
;
914 vo_x11_create_vo_window(vo
, &vinfo
, vo
->dx
, vo
->dy
, d_width
, d_height
,
915 flags
, CopyFromParent
, "vdpau", title
);
916 XChangeWindowAttributes(x11
->display
, x11
->window
, xswamask
, &xswa
);
920 /* Grab the mouse pointer in our window */
922 XGrabPointer(x11
->display
, x11
->window
, True
, 0,
923 GrabModeAsync
, GrabModeAsync
,
924 x11
->window
, None
, CurrentTime
);
925 XSetInputFocus(x11
->display
, x11
->window
, RevertToNone
, CurrentTime
);
929 if ((flags
& VOFLAG_FULLSCREEN
) && WinID
<= 0)
932 if (initialize_vdpau_objects(vo
) < 0)
938 static void check_events(struct vo
*vo
)
940 struct vdpctx
*vc
= vo
->priv
;
941 struct vdp_functions
*vdp
= vc
->vdp
;
943 if (handle_preemption(vo
) < 0)
946 int e
= vo_x11_check_events(vo
);
948 if (e
& VO_EVENT_RESIZE
)
950 else if (e
& VO_EVENT_EXPOSE
&& vc
->paused
) {
951 /* did we already draw a buffer */
952 if (vc
->num_shown_frames
) {
953 /* redraw the last visible buffer */
955 int last_surface
= WRAP_ADD(vc
->surface_num
, -1,
956 vc
->num_output_surfaces
);
957 vdp_st
= vdp
->presentation_queue_display(vc
->flip_queue
,
958 vc
->output_surfaces
[last_surface
],
959 vo
->dwidth
, vo
->dheight
, 0);
960 CHECK_ST_WARNING("Error when calling "
961 "vdp_presentation_queue_display");
966 static void draw_osd_I8A8(void *ctx
, int x0
, int y0
, int w
, int h
,
967 unsigned char *src
, unsigned char *srca
, int stride
)
970 struct vdpctx
*vc
= vo
->priv
;
971 struct vdp_functions
*vdp
= vc
->vdp
;
972 VdpOutputSurface output_surface
= vc
->output_surfaces
[vc
->surface_num
];
976 int index_data_size_required
;
977 VdpRect output_indexed_rect_vid
;
982 index_data_size_required
= 2*w
*h
;
983 if (vc
->index_data_size
< index_data_size_required
) {
984 vc
->index_data
= talloc_realloc_size(vc
, vc
->index_data
,
985 index_data_size_required
);
986 vc
->index_data_size
= index_data_size_required
;
989 // index_data creation, component order - I, A, I, A, .....
990 for (i
= 0; i
< h
; i
++)
991 for (int j
= 0; j
< w
; j
++) {
992 vc
->index_data
[i
*2*w
+ j
*2] = src
[i
*stride
+j
];
993 vc
->index_data
[i
*2*w
+ j
*2 + 1] = -srca
[i
*stride
+j
];
996 output_indexed_rect_vid
.x0
= x0
;
997 output_indexed_rect_vid
.y0
= y0
;
998 output_indexed_rect_vid
.x1
= x0
+ w
;
999 output_indexed_rect_vid
.y1
= y0
+ h
;
1003 // write source_data to osd_surface.
1004 VdpOutputSurface osd_surface
= vc
->output_surfaces
[vc
->num_output_surfaces
];
1006 output_surface_put_bits_indexed(osd_surface
, VDP_INDEXED_FORMAT_I8A8
,
1007 (const void *const*)&vc
->index_data
,
1008 &pitch
, &output_indexed_rect_vid
,
1009 VDP_COLOR_TABLE_FORMAT_B8G8R8X8
,
1010 (void *)vc
->palette
);
1011 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed");
1013 VdpOutputSurfaceRenderBlendState blend_state
= {
1014 .struct_version
= VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION
,
1015 .blend_factor_source_color
=
1016 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
,
1017 .blend_factor_source_alpha
=
1018 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
,
1019 .blend_factor_destination_color
=
1020 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
,
1021 .blend_factor_destination_alpha
=
1022 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
,
1023 .blend_equation_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
,
1024 .blend_equation_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
,
1028 output_surface_render_output_surface(output_surface
,
1029 &output_indexed_rect_vid
,
1031 &output_indexed_rect_vid
,
1033 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0
);
1034 CHECK_ST_WARNING("Error when calling "
1035 "vdp_output_surface_render_output_surface");
1038 static void draw_eosd(struct vo
*vo
)
1040 struct vdpctx
*vc
= vo
->priv
;
1041 struct vdp_functions
*vdp
= vc
->vdp
;
1043 VdpOutputSurface output_surface
= vc
->output_surfaces
[vc
->surface_num
];
1046 if (handle_preemption(vo
) < 0)
1049 VdpOutputSurfaceRenderBlendState blend_state
= {
1050 .struct_version
= VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION
,
1051 .blend_factor_source_color
=
1052 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
,
1053 .blend_factor_source_alpha
=
1054 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE
,
1055 .blend_factor_destination_color
=
1056 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
,
1057 .blend_factor_destination_alpha
=
1058 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA
,
1059 .blend_equation_color
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
,
1060 .blend_equation_alpha
= VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD
,
1063 for (i
= 0; i
< vc
->eosd_render_count
; i
++) {
1065 output_surface_render_bitmap_surface(output_surface
,
1066 &vc
->eosd_targets
[i
].dest
,
1067 vc
->eosd_surface
.surface
,
1068 &vc
->eosd_targets
[i
].source
,
1069 &vc
->eosd_targets
[i
].color
,
1071 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0
);
1072 CHECK_ST_WARNING("EOSD: Error when rendering");
1076 #define HEIGHT_SORT_BITS 4
1077 static int size_index(struct eosd_target
*r
)
1079 unsigned int h
= r
->source
.y1
;
1080 int n
= av_log2_16bit(h
);
1081 return (n
<< HEIGHT_SORT_BITS
)
1082 + (- 1 - (h
<< HEIGHT_SORT_BITS
>> n
) & (1 << HEIGHT_SORT_BITS
) - 1);
1085 /* Pack the given rectangles into an area of size w * h.
1086 * The size of each rectangle is read from .source.x1/.source.y1.
1087 * The height of each rectangle must be at least 1 and less than 65536.
1088 * The .source rectangle is then set corresponding to the packed position.
1089 * 'scratch' must point to work memory for num_rects+16 ints.
1090 * Return 0 on success, -1 if the rectangles did not fit in w*h.
1092 * The rectangles are placed in rows in order approximately sorted by
1093 * height (the approximate sorting is simpler than a full one would be,
1094 * and allows the algorithm to work in linear time). Additionally, to
1095 * reduce wasted space when there are a few tall rectangles, empty
1096 * lower-right parts of rows are filled recursively when the size of
1097 * rectangles in the row drops past a power-of-two threshold. So if a
1098 * row starts with rectangles of size 3x50, 10x40 and 5x20 then the
1099 * free rectangle with corners (13, 20)-(w, 50) is filled recursively.
1101 static int pack_rectangles(struct eosd_target
*rects
, int num_rects
,
1102 int w
, int h
, int *scratch
)
1104 int bins
[16 << HEIGHT_SORT_BITS
];
1105 int sizes
[16 << HEIGHT_SORT_BITS
] = {};
1106 for (int i
= 0; i
< num_rects
; i
++)
1107 sizes
[size_index(rects
+ i
)]++;
1109 for (int i
= 0; i
< 16 << HEIGHT_SORT_BITS
; i
+= 1 << HEIGHT_SORT_BITS
) {
1110 for (int j
= 0; j
< 1 << HEIGHT_SORT_BITS
; j
++) {
1112 idx
+= sizes
[i
+ j
];
1114 scratch
[idx
++] = -1;
1116 for (int i
= 0; i
< num_rects
; i
++)
1117 scratch
[bins
[size_index(rects
+ i
)]++] = i
;
1118 for (int i
= 0; i
< 16; i
++)
1119 bins
[i
] = bins
[i
<< HEIGHT_SORT_BITS
] - sizes
[i
<< HEIGHT_SORT_BITS
];
1121 int size
, x
, bottom
;
1122 } stack
[16] = {{15, 0, h
}}, s
= {};
1127 s
= stack
[--stackpos
];
1132 while ((obj
= scratch
[bins
[s
.size
]]) >= 0) {
1133 int bottom
= y
+ rects
[obj
].source
.y1
;
1134 if (bottom
> s
.bottom
)
1136 int right
= s
.x
+ rects
[obj
].source
.x1
;
1140 rects
[obj
].source
.x0
= s
.x
;
1141 rects
[obj
].source
.x1
+= s
.x
;
1142 rects
[obj
].source
.y0
= y
;
1143 rects
[obj
].source
.y1
+= y
;
1146 stack
[stackpos
++] = s
;
1148 maxy
= FFMAX(maxy
, bottom
);
1154 return num_rects
? -1 : 0;
1157 static void generate_eosd(struct vo
*vo
, mp_eosd_images_t
*imgs
)
1159 struct vdpctx
*vc
= vo
->priv
;
1160 struct vdp_functions
*vdp
= vc
->vdp
;
1163 ASS_Image
*img
= imgs
->imgs
;
1165 struct eosd_bitmap_surface
*sfc
= &vc
->eosd_surface
;
1166 bool need_upload
= false;
1168 if (imgs
->changed
== 0)
1169 return; // Nothing changed, no need to redraw
1171 vc
->eosd_render_count
= 0;
1174 return; // There's nothing to render!
1176 if (imgs
->changed
== 1)
1177 goto eosd_skip_upload
;
1180 bool reallocate
= false;
1182 for (p
= img
, i
= 0; p
; p
= p
->next
) {
1183 if (p
->w
<= 0 || p
->h
<= 0)
1185 // Allocate new space for surface/target arrays
1186 if (i
>= vc
->eosd_targets_size
) {
1187 vc
->eosd_targets_size
= FFMAX(vc
->eosd_targets_size
* 2, 512);
1189 talloc_realloc_size(vc
, vc
->eosd_targets
,
1190 vc
->eosd_targets_size
1191 * sizeof(*vc
->eosd_targets
));
1193 talloc_realloc_size(vc
, vc
->eosd_scratch
,
1194 (vc
->eosd_targets_size
+ 16)
1195 * sizeof(*vc
->eosd_scratch
));
1197 vc
->eosd_targets
[i
].source
.x1
= p
->w
;
1198 vc
->eosd_targets
[i
].source
.y1
= p
->h
;
1201 if (pack_rectangles(vc
->eosd_targets
, i
, sfc
->w
, sfc
->h
,
1202 vc
->eosd_scratch
) >= 0)
1204 int w
= FFMIN(FFMAX(sfc
->w
* 2, EOSD_SURFACE_INITIAL_SIZE
),
1206 int h
= FFMIN(FFMAX(sfc
->h
* 2, EOSD_SURFACE_INITIAL_SIZE
),
1208 if (w
== sfc
->w
&& h
== sfc
->h
) {
1209 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] EOSD bitmaps do not fit on "
1210 "a surface with the maximum supported size\n");
1219 if (sfc
->surface
!= VDP_INVALID_HANDLE
) {
1220 vdp_st
= vdp
->bitmap_surface_destroy(sfc
->surface
);
1221 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1223 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] Allocating a %dx%d surface for "
1224 "EOSD bitmaps.\n", sfc
->w
, sfc
->h
);
1225 vdp_st
= vdp
->bitmap_surface_create(vc
->vdp_device
, VDP_RGBA_FORMAT_A8
,
1226 sfc
->w
, sfc
->h
, true,
1228 if (vdp_st
!= VDP_STATUS_OK
)
1229 sfc
->surface
= VDP_INVALID_HANDLE
;
1230 CHECK_ST_WARNING("EOSD: error when creating surface");
1234 if (sfc
->surface
== VDP_INVALID_HANDLE
)
1236 for (p
= img
; p
; p
= p
->next
) {
1237 if (p
->w
<= 0 || p
->h
<= 0)
1239 struct eosd_target
*target
= &vc
->eosd_targets
[vc
->eosd_render_count
];
1242 bitmap_surface_put_bits_native(sfc
->surface
,
1243 (const void *) &p
->bitmap
,
1244 &p
->stride
, &target
->source
);
1245 CHECK_ST_WARNING("EOSD: putbits failed");
1247 // Render dest, color, etc.
1248 target
->color
.alpha
= 1.0 - ((p
->color
>> 0) & 0xff) / 255.0;
1249 target
->color
.blue
= ((p
->color
>> 8) & 0xff) / 255.0;
1250 target
->color
.green
= ((p
->color
>> 16) & 0xff) / 255.0;
1251 target
->color
.red
= ((p
->color
>> 24) & 0xff) / 255.0;
1252 target
->dest
.x0
= p
->dst_x
;
1253 target
->dest
.y0
= p
->dst_y
;
1254 target
->dest
.x1
= p
->w
+ p
->dst_x
;
1255 target
->dest
.y1
= p
->h
+ p
->dst_y
;
1256 vc
->eosd_render_count
++;
1260 static void draw_osd(struct vo
*vo
, struct osd_state
*osd
)
1262 struct vdpctx
*vc
= vo
->priv
;
1263 mp_msg(MSGT_VO
, MSGL_DBG2
, "DRAW_OSD\n");
1265 if (handle_preemption(vo
) < 0)
1268 osd_draw_text_ext(osd
, vo
->dwidth
, vo
->dheight
, vc
->border_x
, vc
->border_y
,
1269 vc
->border_x
, vc
->border_y
, vc
->vid_width
,
1270 vc
->vid_height
, draw_osd_I8A8
, vo
);
1273 static int update_presentation_queue_status(struct vo
*vo
)
1275 struct vdpctx
*vc
= vo
->priv
;
1276 struct vdp_functions
*vdp
= vc
->vdp
;
1279 while (vc
->query_surface_num
!= vc
->surface_num
) {
1281 VdpPresentationQueueStatus status
;
1282 VdpOutputSurface surface
= vc
->output_surfaces
[vc
->query_surface_num
];
1283 vdp_st
= vdp
->presentation_queue_query_surface_status(vc
->flip_queue
,
1286 CHECK_ST_WARNING("Error calling "
1287 "presentation_queue_query_surface_status");
1288 if (status
== VDP_PRESENTATION_QUEUE_STATUS_QUEUED
)
1290 if (vc
->vsync_interval
> 1) {
1291 uint64_t qtime
= vc
->queue_time
[vc
->query_surface_num
];
1292 if (vtime
< qtime
+ vc
->vsync_interval
/ 2)
1293 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] Frame shown too early\n");
1294 if (vtime
> qtime
+ vc
->vsync_interval
)
1295 mp_msg(MSGT_VO
, MSGL_V
, "[vdpau] Frame shown late\n");
1297 vc
->query_surface_num
= WRAP_ADD(vc
->query_surface_num
, 1,
1298 vc
->num_output_surfaces
);
1299 vc
->recent_vsync_time
= vtime
;
1301 int num_queued
= WRAP_ADD(vc
->surface_num
, -vc
->query_surface_num
,
1302 vc
->num_output_surfaces
);
1303 mp_msg(MSGT_VO
, MSGL_DBG2
, "[vdpau] Queued surface count (before add): "
1304 "%d\n", num_queued
);
1308 static inline uint64_t prev_vs2(struct vdpctx
*vc
, uint64_t ts
, int shift
)
1310 uint64_t offset
= ts
- vc
->recent_vsync_time
;
1311 // Fix negative values for 1<<shift vsyncs before vc->recent_vsync_time
1312 offset
+= (uint64_t)vc
->vsync_interval
<< shift
;
1313 offset
%= vc
->vsync_interval
;
1317 static void flip_page_timed(struct vo
*vo
, unsigned int pts_us
, int duration
)
1319 struct vdpctx
*vc
= vo
->priv
;
1320 struct vdp_functions
*vdp
= vc
->vdp
;
1322 uint32_t vsync_interval
= vc
->vsync_interval
;
1324 if (handle_preemption(vo
) < 0)
1327 if (duration
> INT_MAX
/ 1000)
1332 if (vc
->user_fps
< 0)
1333 duration
= -1; // Make sure drop logic is disabled
1335 uint64_t now
= sync_vdptime(vo
);
1336 uint64_t pts
= pts_us
? convert_to_vdptime(vo
, pts_us
) : now
;
1337 uint64_t ideal_pts
= pts
;
1338 uint64_t npts
= duration
>= 0 ? pts
+ duration
: UINT64_MAX
;
1340 #define PREV_VS2(ts, shift) prev_vs2(vc, ts, shift)
1341 // Only gives accurate results for ts >= vc->recent_vsync_time
1342 #define PREV_VSYNC(ts) PREV_VS2(ts, 0)
1344 /* We hope to be here at least one vsync before the frame should be shown.
1345 * If we are running late then don't drop the frame unless there is
1346 * already one queued for the next vsync; even if we _hope_ to show the
1347 * next frame soon enough to mean this one should be dropped we might
1348 * not make the target time in reality. Without this check we could drop
1349 * every frame, freezing the display completely if video lags behind.
1351 if (now
> PREV_VSYNC(FFMAX(pts
, vc
->last_queue_time
+ vsync_interval
)))
1354 /* Allow flipping a frame at a vsync if its presentation time is a
1355 * bit after that vsync and the change makes the flip time delta
1356 * from previous frame better match the target timestamp delta.
1357 * This avoids instability with frame timestamps falling near vsyncs.
1358 * For example if the frame timestamps were (with vsyncs at
1359 * integer values) 0.01, 1.99, 4.01, 5.99, 8.01, ... then
1360 * straightforward timing at next vsync would flip the frames at
1361 * 1, 2, 5, 6, 9; this changes it to 1, 2, 4, 6, 8 and so on with
1362 * regular 2-vsync intervals.
1364 * Also allow moving the frame forward if it looks like we dropped
1365 * the previous frame incorrectly (now that we know better after
1366 * having final exact timestamp information for this frame) and
1367 * there would unnecessarily be a vsync without a frame change.
1369 uint64_t vsync
= PREV_VSYNC(pts
);
1370 if (pts
< vsync
+ vsync_interval
/ 4
1371 && (vsync
- PREV_VS2(vc
->last_queue_time
, 16)
1372 > pts
- vc
->last_ideal_time
+ vsync_interval
/ 2
1373 || vc
->dropped_frame
&& vsync
> vc
->dropped_time
))
1374 pts
-= vsync_interval
/ 2;
1376 vc
->dropped_frame
= true; // changed at end if false
1377 vc
->dropped_time
= ideal_pts
;
1379 pts
= FFMAX(pts
, vc
->last_queue_time
+ vsync_interval
);
1380 pts
= FFMAX(pts
, now
);
1381 if (npts
< PREV_VSYNC(pts
) + vsync_interval
)
1384 int num_flips
= update_presentation_queue_status(vo
);
1385 vsync
= vc
->recent_vsync_time
+ num_flips
* vc
->vsync_interval
;
1386 now
= sync_vdptime(vo
);
1387 pts
= FFMAX(pts
, now
);
1388 pts
= FFMAX(pts
, vsync
+ (vsync_interval
>> 2));
1389 vsync
= PREV_VSYNC(pts
);
1390 if (npts
< vsync
+ vsync_interval
)
1392 pts
= vsync
+ (vsync_interval
>> 2);
1394 vdp
->presentation_queue_display(vc
->flip_queue
,
1395 vc
->output_surfaces
[vc
->surface_num
],
1396 vo
->dwidth
, vo
->dheight
, pts
);
1397 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display");
1399 vc
->last_queue_time
= pts
;
1400 vc
->queue_time
[vc
->surface_num
] = pts
;
1401 vc
->last_ideal_time
= ideal_pts
;
1402 vc
->dropped_frame
= false;
1403 vc
->surface_num
= WRAP_ADD(vc
->surface_num
, 1, vc
->num_output_surfaces
);
1404 vc
->num_shown_frames
= FFMIN(vc
->num_shown_frames
+ 1, 1000);
1407 static int draw_slice(struct vo
*vo
, uint8_t *image
[], int stride
[], int w
,
1408 int h
, int x
, int y
)
1410 struct vdpctx
*vc
= vo
->priv
;
1411 struct vdp_functions
*vdp
= vc
->vdp
;
1414 if (handle_preemption(vo
) < 0)
1417 struct vdpau_render_state
*rndr
= (struct vdpau_render_state
*)image
[0];
1418 int max_refs
= vc
->image_format
== IMGFMT_VDPAU_H264
?
1419 rndr
->info
.h264
.num_ref_frames
: 2;
1420 if (!IMGFMT_IS_VDPAU(vc
->image_format
))
1422 if ((vc
->decoder
== VDP_INVALID_HANDLE
|| vc
->decoder_max_refs
< max_refs
)
1423 && !create_vdp_decoder(vo
, max_refs
))
1426 vdp_st
= vdp
->decoder_render(vc
->decoder
, rndr
->surface
,
1427 (void *)&rndr
->info
,
1428 rndr
->bitstream_buffers_used
,
1429 rndr
->bitstream_buffers
);
1430 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
1435 static struct vdpau_render_state
*get_surface(struct vo
*vo
, int number
)
1437 struct vdpctx
*vc
= vo
->priv
;
1438 struct vdp_functions
*vdp
= vc
->vdp
;
1440 if (number
> MAX_VIDEO_SURFACES
)
1442 if (vc
->surface_render
[number
].surface
== VDP_INVALID_HANDLE
1443 && !vc
->is_preempted
) {
1445 vdp_st
= vdp
->video_surface_create(vc
->vdp_device
, vc
->vdp_chroma_type
,
1446 vc
->vid_width
, vc
->vid_height
,
1447 &vc
->surface_render
[number
].surface
);
1448 CHECK_ST_WARNING("Error when calling vdp_video_surface_create");
1450 mp_msg(MSGT_VO
, MSGL_DBG2
, "VID CREATE: %u\n",
1451 vc
->surface_render
[number
].surface
);
1452 return &vc
->surface_render
[number
];
1455 static void draw_image(struct vo
*vo
, mp_image_t
*mpi
, double pts
)
1457 struct vdpctx
*vc
= vo
->priv
;
1458 struct vdp_functions
*vdp
= vc
->vdp
;
1459 struct mp_image
*reserved_mpi
= NULL
;
1460 struct vdpau_render_state
*rndr
;
1462 if (vc
->is_preempted
) {
1463 vo
->frame_loaded
= true;
1467 if (IMGFMT_IS_VDPAU(vc
->image_format
)) {
1470 } else if (!(mpi
->flags
& MP_IMGFLAG_DRAW_CALLBACK
)) {
1472 void *destdata
[3] = {mpi
->planes
[0], mpi
->planes
[2], mpi
->planes
[1]};
1473 rndr
= get_surface(vo
, vc
->deint_counter
);
1474 vc
->deint_counter
= WRAP_ADD(vc
->deint_counter
, 1, NUM_BUFFERED_VIDEO
);
1475 if (vc
->image_format
== IMGFMT_NV12
)
1476 destdata
[1] = destdata
[2];
1478 vdp
->video_surface_put_bits_y_cb_cr(rndr
->surface
,
1479 vc
->vdp_pixel_format
,
1480 (const void *const*)destdata
,
1481 mpi
->stride
); // pitch
1482 CHECK_ST_WARNING("Error when calling "
1483 "vdp_video_surface_put_bits_y_cb_cr");
1485 // We don't support slice callbacks so this shouldn't occur -
1486 // I think the flags test above in pointless, but I'm adding
1487 // this instead of removing it just in case.
1489 if (mpi
->fields
& MP_IMGFIELD_ORDERED
)
1490 vc
->top_field_first
= !!(mpi
->fields
& MP_IMGFIELD_TOP_FIRST
);
1492 vc
->top_field_first
= 1;
1494 add_new_video_surface(vo
, rndr
->surface
, mpi
, pts
);
1499 static uint32_t get_image(struct vo
*vo
, mp_image_t
*mpi
)
1501 struct vdpctx
*vc
= vo
->priv
;
1502 struct vdpau_render_state
*rndr
;
1504 // no dr for non-decoding for now
1505 if (!IMGFMT_IS_VDPAU(vc
->image_format
))
1507 if (mpi
->type
!= MP_IMGTYPE_NUMBERED
)
1510 rndr
= get_surface(vo
, mpi
->number
);
1512 mp_msg(MSGT_VO
, MSGL_ERR
, "[vdpau] no surfaces available in "
1514 // TODO: this probably breaks things forever, provide a dummy buffer?
1517 mpi
->flags
|= MP_IMGFLAG_DIRECT
;
1518 mpi
->stride
[0] = mpi
->stride
[1] = mpi
->stride
[2] = 0;
1519 mpi
->planes
[0] = mpi
->planes
[1] = mpi
->planes
[2] = NULL
;
1520 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
1521 mpi
->planes
[0] = (void *)rndr
;
1522 mpi
->num_planes
= 1;
1527 static int query_format(uint32_t format
)
1529 int default_flags
= VFCAP_CSP_SUPPORTED
| VFCAP_CSP_SUPPORTED_BY_HW
1530 | VFCAP_HWSCALE_UP
| VFCAP_HWSCALE_DOWN
| VFCAP_OSD
| VFCAP_EOSD
1531 | VFCAP_EOSD_UNSCALED
| VFCAP_FLIP
;
1539 return default_flags
| VOCAP_NOSLICES
;
1540 case IMGFMT_VDPAU_MPEG1
:
1541 case IMGFMT_VDPAU_MPEG2
:
1542 case IMGFMT_VDPAU_H264
:
1543 case IMGFMT_VDPAU_WMV3
:
1544 case IMGFMT_VDPAU_VC1
:
1545 case IMGFMT_VDPAU_MPEG4
:
1546 return default_flags
;
1551 static void destroy_vdpau_objects(struct vo
*vo
)
1553 struct vdpctx
*vc
= vo
->priv
;
1554 struct vdp_functions
*vdp
= vc
->vdp
;
1559 free_video_specific(vo
);
1561 if (vc
->flip_queue
!= VDP_INVALID_HANDLE
) {
1562 vdp_st
= vdp
->presentation_queue_destroy(vc
->flip_queue
);
1563 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy");
1566 if (vc
->flip_target
!= VDP_INVALID_HANDLE
) {
1567 vdp_st
= vdp
->presentation_queue_target_destroy(vc
->flip_target
);
1568 CHECK_ST_WARNING("Error when calling "
1569 "vdp_presentation_queue_target_destroy");
1572 for (i
= 0; i
<= vc
->num_output_surfaces
; i
++) {
1573 if (vc
->output_surfaces
[i
] == VDP_INVALID_HANDLE
)
1575 vdp_st
= vdp
->output_surface_destroy(vc
->output_surfaces
[i
]);
1576 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy");
1579 if (vc
->eosd_surface
.surface
!= VDP_INVALID_HANDLE
) {
1580 vdp_st
= vdp
->bitmap_surface_destroy(vc
->eosd_surface
.surface
);
1581 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy");
1584 vdp_st
= vdp
->device_destroy(vc
->vdp_device
);
1585 CHECK_ST_WARNING("Error when calling vdp_device_destroy");
1588 static void uninit(struct vo
*vo
)
1590 struct vdpctx
*vc
= vo
->priv
;
1592 /* Destroy all vdpau objects */
1593 destroy_vdpau_objects(vo
);
1595 #ifdef CONFIG_XF86VM
1596 if (vc
->mode_switched
)
1601 // Free bitstream buffers allocated by FFmpeg
1602 for (int i
= 0; i
< MAX_VIDEO_SURFACES
; i
++)
1603 av_freep(&vc
->surface_render
[i
].bitstream_buffers
);
1606 static int preinit(struct vo
*vo
, const char *arg
)
1610 struct vdpctx
*vc
= talloc_zero(vo
, struct vdpctx
);
1613 // Mark everything as invalid first so uninit() can tell what has been
1615 mark_vdpau_objects_uninitialized(vo
);
1618 vc
->chroma_deint
= 1;
1619 vc
->user_colorspace
= 1;
1620 vc
->flip_offset_window
= 50;
1621 vc
->flip_offset_fs
= 50;
1622 vc
->num_output_surfaces
= 3;
1623 const opt_t subopts
[] = {
1624 {"deint", OPT_ARG_INT
, &vc
->deint
, (opt_test_f
)int_non_neg
},
1625 {"chroma-deint", OPT_ARG_BOOL
, &vc
->chroma_deint
, NULL
},
1626 {"pullup", OPT_ARG_BOOL
, &vc
->pullup
, NULL
},
1627 {"denoise", OPT_ARG_FLOAT
, &vc
->denoise
, NULL
},
1628 {"sharpen", OPT_ARG_FLOAT
, &vc
->sharpen
, NULL
},
1629 {"colorspace", OPT_ARG_INT
, &vc
->user_colorspace
, NULL
},
1630 {"studio", OPT_ARG_BOOL
, &vc
->studio_levels
, NULL
},
1631 {"hqscaling", OPT_ARG_INT
, &vc
->hqscaling
, NULL
},
1632 {"fps", OPT_ARG_FLOAT
, &vc
->user_fps
, NULL
},
1633 {"queuetime_windowed", OPT_ARG_INT
, &vc
->flip_offset_window
, NULL
},
1634 {"queuetime_fs", OPT_ARG_INT
, &vc
->flip_offset_fs
, NULL
},
1635 {"output_surfaces", OPT_ARG_INT
, &vc
->num_output_surfaces
, NULL
},
1638 if (subopt_parse(arg
, subopts
) != 0) {
1639 mp_msg(MSGT_VO
, MSGL_FATAL
, "[vdpau] Could not parse suboptions.\n");
1642 if (vc
->hqscaling
< 0 || vc
->hqscaling
> 9) {
1643 mp_msg(MSGT_VO
, MSGL_FATAL
, "[vdpau] Invalid value for suboption "
1647 if (vc
->num_output_surfaces
< 2) {
1648 mp_msg(MSGT_VO
, MSGL_FATAL
, "[vdpau] Invalid suboption "
1649 "output_surfaces: can't use less than 2 surfaces\n");
1652 if (vc
->num_output_surfaces
> MAX_OUTPUT_SURFACES
) {
1653 mp_msg(MSGT_VO
, MSGL_WARN
, "[vdpau] Number of output surfaces "
1654 "is limited to %d.\n", MAX_OUTPUT_SURFACES
);
1655 vc
->num_output_surfaces
= MAX_OUTPUT_SURFACES
;
1658 vc
->deint_type
= vc
->deint
;
1663 // After this calling uninit() should work to free resources
1665 if (win_x11_init_vdpau_procs(vo
) < 0) {
1666 if (vc
->vdp
->device_destroy
)
1667 vc
->vdp
->device_destroy(vc
->vdp_device
);
1672 // full grayscale palette.
1673 for (i
= 0; i
< PALETTE_SIZE
; ++i
)
1674 vc
->palette
[i
] = (i
<< 16) | (i
<< 8) | i
;
1676 vc
->procamp
.struct_version
= VDP_PROCAMP_VERSION
;
1677 vc
->procamp
.brightness
= 0.0;
1678 vc
->procamp
.contrast
= 1.0;
1679 vc
->procamp
.saturation
= 1.0;
1680 vc
->procamp
.hue
= 0.0;
1685 static int get_equalizer(struct vo
*vo
, const char *name
, int *value
)
1687 struct vdpctx
*vc
= vo
->priv
;
1689 if (!strcasecmp(name
, "brightness"))
1690 *value
= vc
->procamp
.brightness
* 100;
1691 else if (!strcasecmp(name
, "contrast"))
1692 *value
= (vc
->procamp
.contrast
- 1.0) * 100;
1693 else if (!strcasecmp(name
, "saturation"))
1694 *value
= (vc
->procamp
.saturation
- 1.0) * 100;
1695 else if (!strcasecmp(name
, "hue"))
1696 *value
= vc
->procamp
.hue
* 100 / M_PI
;
1702 static int set_equalizer(struct vo
*vo
, const char *name
, int value
)
1704 struct vdpctx
*vc
= vo
->priv
;
1706 if (!strcasecmp(name
, "brightness"))
1707 vc
->procamp
.brightness
= value
/ 100.0;
1708 else if (!strcasecmp(name
, "contrast"))
1709 vc
->procamp
.contrast
= value
/ 100.0 + 1.0;
1710 else if (!strcasecmp(name
, "saturation"))
1711 vc
->procamp
.saturation
= value
/ 100.0 + 1.0;
1712 else if (!strcasecmp(name
, "hue"))
1713 vc
->procamp
.hue
= value
/ 100.0 * M_PI
;
1717 update_csc_matrix(vo
);
1721 static int control(struct vo
*vo
, uint32_t request
, void *data
)
1723 struct vdpctx
*vc
= vo
->priv
;
1724 struct vdp_functions
*vdp
= vc
->vdp
;
1726 handle_preemption(vo
);
1729 case VOCTRL_GET_DEINTERLACE
:
1730 *(int*)data
= vc
->deint
;
1732 case VOCTRL_SET_DEINTERLACE
:
1733 vc
->deint
= *(int*)data
;
1735 vc
->deint
= vc
->deint_type
;
1736 if (vc
->deint_type
> 2) {
1738 VdpVideoMixerFeature features
[1] =
1739 {vc
->deint_type
== 3 ?
1740 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL
:
1741 VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL
};
1742 VdpBool feature_enables
[1] = {vc
->deint
? VDP_TRUE
: VDP_FALSE
};
1743 vdp_st
= vdp
->video_mixer_set_feature_enables(vc
->video_mixer
,
1746 CHECK_ST_WARNING("Error changing deinterlacing settings");
1750 if (vc
->dropped_frame
)
1751 flip_page_timed(vo
, 0, -1);
1752 return (vc
->paused
= true);
1754 return (vc
->paused
= false);
1755 case VOCTRL_QUERY_FORMAT
:
1756 return query_format(*(uint32_t *)data
);
1757 case VOCTRL_GET_IMAGE
:
1758 return get_image(vo
, data
);
1759 case VOCTRL_DRAW_IMAGE
:
1760 abort(); // draw_image() should get called directly
1765 case VOCTRL_FULLSCREEN
:
1766 vo_x11_fullscreen(vo
);
1769 case VOCTRL_GET_PANSCAN
:
1771 case VOCTRL_SET_PANSCAN
:
1774 case VOCTRL_SET_EQUALIZER
: {
1775 struct voctrl_set_equalizer_args
*args
= data
;
1776 return set_equalizer(vo
, args
->name
, args
->value
);
1778 case VOCTRL_GET_EQUALIZER
: {
1779 struct voctrl_get_equalizer_args
*args
= data
;
1780 return get_equalizer(vo
, args
->name
, args
->valueptr
);
1782 case VOCTRL_SET_YUV_COLORSPACE
:
1783 vc
->colorspace
= *(int *)data
% 3;
1784 update_csc_matrix(vo
);
1786 case VOCTRL_GET_YUV_COLORSPACE
:
1787 *(int *)data
= vc
->colorspace
;
1792 case VOCTRL_UPDATE_SCREENINFO
:
1793 update_xinerama_info(vo
);
1795 case VOCTRL_DRAW_EOSD
:
1798 generate_eosd(vo
, data
);
1801 case VOCTRL_GET_EOSD_RES
: {
1802 mp_eosd_res_t
*r
= data
;
1805 r
->ml
= r
->mr
= vc
->border_x
;
1806 r
->mt
= r
->mb
= vc
->border_y
;
1809 case VOCTRL_REDRAW_OSD
:
1810 video_to_output_surface(vo
);
1813 flip_page_timed(vo
, 0, -1);
1822 const struct vo_driver video_out_vdpau
= {
1824 .buffer_frames
= true,
1825 .info
= &(const struct vo_info_s
){
1828 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
1834 .draw_image
= draw_image
,
1835 .get_buffered_frame
= get_buffered_frame
,
1836 .draw_slice
= draw_slice
,
1837 .draw_osd
= draw_osd
,
1838 .flip_page_timed
= flip_page_timed
,
1839 .check_events
= check_events
,