Merge svn changes up to r28862
[mplayer.git] / libvo / vo_vdpau.c
blobfec166c78e1dff627ea5c59fea678c83c1d47bea
1 /*
2 * VDPAU video output driver
4 * Copyright (C) 2008 NVIDIA
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * \defgroup VDPAU_Presentation VDPAU Presentation
25 * \ingroup Decoder
27 * Actual decoding and presentation are implemented here.
28 * All necessary frame information is collected through
29 * the "vdpau_render_state" structure after parsing all headers
30 * etc. in libavcodec for different codecs.
32 * @{
35 #include <stdio.h>
36 #include <dlfcn.h>
38 #include "config.h"
39 #include "mp_msg.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "x11_common.h"
43 #include "aspect.h"
44 #include "sub.h"
45 #include "subopt-helper.h"
47 #include "libavcodec/vdpau.h"
49 #include "gui/interface.h"
51 #include "libavutil/common.h"
52 #include "libavutil/mathematics.h"
54 #include "libass/ass.h"
55 #include "libass/ass_mp.h"
57 static vo_info_t info = {
58 "VDPAU with X11",
59 "vdpau",
60 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
64 LIBVO_EXTERN(vdpau)
66 #define CHECK_ST_ERROR(message) \
67 if (vdp_st != VDP_STATUS_OK) { \
68 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
69 message, vdp_get_error_string(vdp_st)); \
70 return -1; \
73 #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));
78 /* number of video and output surfaces */
79 #define NUM_OUTPUT_SURFACES 2
80 #define MAX_VIDEO_SURFACES 50
82 /* number of palette entries */
83 #define PALETTE_SIZE 256
85 /* Initial maximum number of EOSD surfaces */
86 #define EOSD_SURFACES_INITIAL 512
89 * Global variable declaration - VDPAU specific
92 /* Declaration for all variables of win_x11_init_vdpau_procs() and
93 * win_x11_init_vdpau_flip_queue() functions
95 static VdpDevice vdp_device;
96 static VdpDeviceCreateX11 *vdp_device_create;
97 static VdpGetProcAddress *vdp_get_proc_address;
99 static VdpPresentationQueueTarget vdp_flip_target;
100 static VdpPresentationQueue vdp_flip_queue;
102 static VdpDeviceDestroy *vdp_device_destroy;
103 static VdpVideoSurfaceCreate *vdp_video_surface_create;
104 static VdpVideoSurfaceDestroy *vdp_video_surface_destroy;
106 static VdpGetErrorString *vdp_get_error_string;
108 /* May be used in software filtering/postprocessing options
109 * in MPlayer (./mplayer -vf ..) if we copy video_surface data to
110 * system memory.
112 static VdpVideoSurfacePutBitsYCbCr *vdp_video_surface_put_bits_y_cb_cr;
113 static VdpOutputSurfacePutBitsNative *vdp_output_surface_put_bits_native;
115 static VdpOutputSurfaceCreate *vdp_output_surface_create;
116 static VdpOutputSurfaceDestroy *vdp_output_surface_destroy;
118 /* VideoMixer puts video_surface data on displayable output_surface. */
119 static VdpVideoMixerCreate *vdp_video_mixer_create;
120 static VdpVideoMixerDestroy *vdp_video_mixer_destroy;
121 static VdpVideoMixerRender *vdp_video_mixer_render;
122 static VdpVideoMixerSetFeatureEnables *vdp_video_mixer_set_feature_enables;
123 static VdpVideoMixerSetAttributeValues *vdp_video_mixer_set_attribute_values;
125 static VdpPresentationQueueTargetDestroy *vdp_presentation_queue_target_destroy;
126 static VdpPresentationQueueCreate *vdp_presentation_queue_create;
127 static VdpPresentationQueueDestroy *vdp_presentation_queue_destroy;
128 static VdpPresentationQueueDisplay *vdp_presentation_queue_display;
129 static VdpPresentationQueueBlockUntilSurfaceIdle *vdp_presentation_queue_block_until_surface_idle;
130 static VdpPresentationQueueTargetCreateX11 *vdp_presentation_queue_target_create_x11;
132 static VdpOutputSurfaceRenderOutputSurface *vdp_output_surface_render_output_surface;
133 static VdpOutputSurfacePutBitsIndexed *vdp_output_surface_put_bits_indexed;
134 static VdpOutputSurfaceRenderBitmapSurface *vdp_output_surface_render_bitmap_surface;
136 static VdpBitmapSurfaceCreate *vdp_bitmap_surface_create;
137 static VdpBitmapSurfaceDestroy *vdp_bitmap_surface_destroy;
138 static VdpBitmapSurfacePutBitsNative *vdp_bitmap_surface_putbits_native;
140 static VdpDecoderCreate *vdp_decoder_create;
141 static VdpDecoderDestroy *vdp_decoder_destroy;
142 static VdpDecoderRender *vdp_decoder_render;
144 static VdpGenerateCSCMatrix *vdp_generate_csc_matrix;
146 static void *vdpau_lib_handle;
147 /* output_surfaces[NUM_OUTPUT_SURFACES] is misused for OSD. */
148 #define osd_surface output_surfaces[NUM_OUTPUT_SURFACES]
149 static VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES + 1];
150 static int output_surface_width, output_surface_height;
152 static VdpVideoMixer video_mixer;
153 static int deint;
154 static int deint_type;
155 static int pullup;
156 static float denoise;
157 static float sharpen;
158 static int top_field_first;
160 static VdpDecoder decoder;
161 static int decoder_max_refs;
163 static VdpRect src_rect_vid;
164 static VdpRect out_rect_vid;
165 static int border_x, border_y;
167 static struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES];
168 static int surface_num;
169 static int vid_surface_num;
170 static uint32_t vid_width, vid_height;
171 static uint32_t image_format;
172 static const VdpChromaType vdp_chroma_type = VDP_CHROMA_TYPE_420;
174 /* draw_osd */
175 static unsigned char *index_data;
176 static int index_data_size;
177 static uint32_t palette[PALETTE_SIZE];
179 // EOSD
180 // Pool of surfaces
181 struct {
182 VdpBitmapSurface surface;
183 int w;
184 int h;
185 char in_use;
186 } *eosd_surfaces;
188 // List of surfaces to be rendered
189 struct {
190 VdpBitmapSurface surface;
191 VdpRect source;
192 VdpRect dest;
193 VdpColor color;
194 } *eosd_targets;
196 static int eosd_render_count;
197 static int eosd_surface_count;
199 // Video equalizer
200 static VdpProcamp procamp;
203 * X11 specific
205 static int visible_buf;
206 static int int_pause;
208 static void draw_eosd(void);
210 static void video_to_output_surface(void)
212 VdpTime dummy;
213 VdpStatus vdp_st;
214 int i;
215 if (vid_surface_num < 0)
216 return;
218 // we would need to provide 2 past and 1 future frames to allow advanced
219 // deinterlacing, which is not really possible currently.
220 for (i = 0; i <= !!(deint > 1); i++) {
221 int field = VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME;
222 VdpOutputSurface output_surface;
223 if (i) {
224 draw_eosd();
225 draw_osd();
226 flip_page();
228 if (deint)
229 field = top_field_first == i ?
230 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_BOTTOM_FIELD:
231 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_TOP_FIELD;
232 output_surface = output_surfaces[surface_num];
233 vdp_st = vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue,
234 output_surface,
235 &dummy);
236 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
238 vdp_st = vdp_video_mixer_render(video_mixer, VDP_INVALID_HANDLE, 0,
239 field, 0, NULL,
240 surface_render[vid_surface_num].surface,
241 0, NULL, &src_rect_vid,
242 output_surface,
243 NULL, &out_rect_vid, 0, NULL);
244 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
248 static void resize(void)
250 VdpStatus vdp_st;
251 int i;
252 struct vo_rect src_rect;
253 struct vo_rect dst_rect;
254 struct vo_rect borders;
255 calc_src_dst_rects(vid_width, vid_height, &src_rect, &dst_rect, &borders, NULL);
256 out_rect_vid.x0 = dst_rect.left;
257 out_rect_vid.x1 = dst_rect.right;
258 out_rect_vid.y0 = dst_rect.top;
259 out_rect_vid.y1 = dst_rect.bottom;
260 src_rect_vid.x0 = src_rect.left;
261 src_rect_vid.x1 = src_rect.right;
262 src_rect_vid.y0 = src_rect.top;
263 src_rect_vid.y1 = src_rect.bottom;
264 border_x = borders.left;
265 border_y = borders.top;
266 #ifdef CONFIG_FREETYPE
267 // adjust font size to display size
268 force_load_font = 1;
269 #endif
270 vo_osd_changed(OSDTYPE_OSD);
272 if (output_surface_width < vo_dwidth || output_surface_height < vo_dheight) {
273 if (output_surface_width < vo_dwidth) {
274 output_surface_width += output_surface_width >> 1;
275 output_surface_width = FFMAX(output_surface_width, vo_dwidth);
277 if (output_surface_height < vo_dheight) {
278 output_surface_height += output_surface_height >> 1;
279 output_surface_height = FFMAX(output_surface_height, vo_dheight);
281 // Creation of output_surfaces
282 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) {
283 if (output_surfaces[i] != VDP_INVALID_HANDLE)
284 vdp_output_surface_destroy(output_surfaces[i]);
285 vdp_st = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8,
286 output_surface_width, output_surface_height,
287 &output_surfaces[i]);
288 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
289 mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", output_surfaces[i]);
292 video_to_output_surface();
293 if (visible_buf)
294 flip_page();
297 /* Initialize vdp_get_proc_address, called from preinit() */
298 static int win_x11_init_vdpau_procs(void)
300 VdpStatus vdp_st;
302 struct vdp_function {
303 const int id;
304 void *pointer;
307 const struct vdp_function *dsc;
309 static const struct vdp_function vdp_func[] = {
310 {VDP_FUNC_ID_GET_ERROR_STRING, &vdp_get_error_string},
311 {VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy},
312 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE, &vdp_video_surface_create},
313 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, &vdp_video_surface_destroy},
314 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
315 &vdp_video_surface_put_bits_y_cb_cr},
316 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE,
317 &vdp_output_surface_put_bits_native},
318 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create},
319 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy},
320 {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create},
321 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy},
322 {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render},
323 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
324 &vdp_video_mixer_set_feature_enables},
325 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
326 &vdp_video_mixer_set_attribute_values},
327 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
328 &vdp_presentation_queue_target_destroy},
329 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, &vdp_presentation_queue_create},
330 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
331 &vdp_presentation_queue_destroy},
332 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
333 &vdp_presentation_queue_display},
334 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
335 &vdp_presentation_queue_block_until_surface_idle},
336 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
337 &vdp_presentation_queue_target_create_x11},
338 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE,
339 &vdp_output_surface_render_output_surface},
340 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED,
341 &vdp_output_surface_put_bits_indexed},
342 {VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create},
343 {VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render},
344 {VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy},
345 {VDP_FUNC_ID_BITMAP_SURFACE_CREATE, &vdp_bitmap_surface_create},
346 {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY, &vdp_bitmap_surface_destroy},
347 {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE,
348 &vdp_bitmap_surface_putbits_native},
349 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE,
350 &vdp_output_surface_render_bitmap_surface},
351 {VDP_FUNC_ID_GENERATE_CSC_MATRIX, &vdp_generate_csc_matrix},
352 {0, NULL}
355 vdp_st = vdp_device_create(mDisplay, mScreen,
356 &vdp_device, &vdp_get_proc_address);
357 if (vdp_st != VDP_STATUS_OK) {
358 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st);
359 return -1;
362 for (dsc = vdp_func; dsc->pointer; dsc++) {
363 vdp_st = vdp_get_proc_address(vdp_device, dsc->id, dsc->pointer);
364 if (vdp_st != VDP_STATUS_OK) {
365 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_get_proc_address(function id %d): %s\n", dsc->id, vdp_get_error_string ? vdp_get_error_string(vdp_st) : "?");
366 return -1;
369 return 0;
372 /* Initialize vdpau_flip_queue, called from config() */
373 static int win_x11_init_vdpau_flip_queue(void)
375 VdpStatus vdp_st;
377 vdp_st = vdp_presentation_queue_target_create_x11(vdp_device, vo_window,
378 &vdp_flip_target);
379 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
381 vdp_st = vdp_presentation_queue_create(vdp_device, vdp_flip_target,
382 &vdp_flip_queue);
383 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
385 return 0;
388 static int create_vdp_mixer(VdpChromaType vdp_chroma_type) {
389 #define VDP_NUM_MIXER_PARAMETER 3
390 #define MAX_NUM_FEATURES 5
391 int i;
392 VdpStatus vdp_st;
393 int feature_count = 0;
394 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
395 VdpBool feature_enables[MAX_NUM_FEATURES];
396 static const VdpVideoMixerAttribute denoise_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL};
397 const void * const denoise_value[] = {&denoise};
398 static const VdpVideoMixerAttribute sharpen_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL};
399 const void * const sharpen_value[] = {&sharpen};
400 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
401 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
402 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
403 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
405 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
406 &vid_width,
407 &vid_height,
408 &vdp_chroma_type
410 if (deint == 3)
411 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
412 if (deint == 4)
413 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
414 if (pullup)
415 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
416 if (denoise)
417 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
418 if (sharpen)
419 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
421 vdp_st = vdp_video_mixer_create(vdp_device, feature_count, features,
422 VDP_NUM_MIXER_PARAMETER,
423 parameters, parameter_values,
424 &video_mixer);
425 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
427 for (i = 0; i < feature_count; i++) feature_enables[i] = VDP_TRUE;
428 if (feature_count)
429 vdp_video_mixer_set_feature_enables(video_mixer, feature_count, features, feature_enables);
430 if (denoise)
431 vdp_video_mixer_set_attribute_values(video_mixer, 1, denoise_attrib, denoise_value);
432 if (sharpen)
433 vdp_video_mixer_set_attribute_values(video_mixer, 1, sharpen_attrib, sharpen_value);
435 return 0;
438 // Free everything specific to a certain video file
439 static void free_video_specific(void) {
440 int i;
441 VdpStatus vdp_st;
443 if (decoder != VDP_INVALID_HANDLE)
444 vdp_decoder_destroy(decoder);
445 decoder = VDP_INVALID_HANDLE;
446 decoder_max_refs = -1;
448 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
449 if (surface_render[i].surface != VDP_INVALID_HANDLE) {
450 vdp_st = vdp_video_surface_destroy(surface_render[i].surface);
451 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
453 surface_render[i].surface = VDP_INVALID_HANDLE;
456 if (video_mixer != VDP_INVALID_HANDLE) {
457 vdp_st = vdp_video_mixer_destroy(video_mixer);
458 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
460 video_mixer = VDP_INVALID_HANDLE;
464 * connect to X server, create and map window, initialize all
465 * VDPAU objects, create different surfaces etc.
467 static int config(uint32_t width, uint32_t height, uint32_t d_width,
468 uint32_t d_height, uint32_t flags, char *title,
469 uint32_t format)
471 XVisualInfo vinfo;
472 XSetWindowAttributes xswa;
473 XWindowAttributes attribs;
474 unsigned long xswamask;
475 int depth;
477 #ifdef CONFIG_XF86VM
478 int vm = flags & VOFLAG_MODESWITCHING;
479 #endif
481 image_format = format;
483 int_pause = 0;
484 visible_buf = 0;
486 #ifdef CONFIG_GUI
487 if (use_gui)
488 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize our window
489 else
490 #endif
492 #ifdef CONFIG_XF86VM
493 if (vm)
494 vo_vm_switch();
495 else
496 #endif
497 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
498 depth = attribs.depth;
499 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
500 depth = 24;
501 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
503 xswa.background_pixel = 0;
504 xswa.border_pixel = 0;
505 xswamask = CWBackPixel | CWBorderPixel;
507 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height,
508 flags, CopyFromParent, "vdpau", title);
509 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
511 #ifdef CONFIG_XF86VM
512 if (vm) {
513 /* Grab the mouse pointer in our window */
514 if (vo_grabpointer)
515 XGrabPointer(mDisplay, vo_window, True, 0,
516 GrabModeAsync, GrabModeAsync,
517 vo_window, None, CurrentTime);
518 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
520 #endif
523 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
524 vo_fs = 1;
526 /* -----VDPAU related code here -------- */
528 free_video_specific();
530 if (vdp_flip_queue == VDP_INVALID_HANDLE && win_x11_init_vdpau_flip_queue())
531 return -1;
533 // video width and height
534 vid_width = width;
535 vid_height = height;
537 if (create_vdp_mixer(vdp_chroma_type))
538 return -1;
540 surface_num = 0;
541 vid_surface_num = -1;
542 resize();
544 return 0;
547 static void check_events(void)
549 int e = vo_x11_check_events(mDisplay);
551 if (e & VO_EVENT_RESIZE)
552 resize();
554 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && int_pause) {
555 /* did we already draw a buffer */
556 if (visible_buf) {
557 /* redraw the last visible buffer */
558 VdpStatus vdp_st;
559 vdp_st = vdp_presentation_queue_display(vdp_flip_queue,
560 output_surfaces[surface_num],
561 vo_dwidth, vo_dheight,
563 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
568 static void draw_osd_I8A8(int x0,int y0, int w,int h, unsigned char *src,
569 unsigned char *srca, int stride)
571 VdpOutputSurface output_surface = output_surfaces[surface_num];
572 VdpStatus vdp_st;
573 int i, j;
574 int pitch;
575 int index_data_size_required;
576 VdpRect output_indexed_rect_vid;
577 VdpOutputSurfaceRenderBlendState blend_state;
579 if (!w || !h)
580 return;
582 index_data_size_required = 2*w*h;
583 if (index_data_size < index_data_size_required) {
584 index_data = realloc(index_data, index_data_size_required);
585 index_data_size = index_data_size_required;
588 // index_data creation, component order - I, A, I, A, .....
589 for (i = 0; i < h; i++)
590 for (j = 0; j < w; j++) {
591 index_data[i*2*w + j*2] = src [i*stride+j];
592 index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
595 output_indexed_rect_vid.x0 = x0;
596 output_indexed_rect_vid.y0 = y0;
597 output_indexed_rect_vid.x1 = x0 + w;
598 output_indexed_rect_vid.y1 = y0 + h;
600 pitch = w*2;
602 // write source_data to osd_surface.
603 vdp_st = vdp_output_surface_put_bits_indexed(osd_surface,
604 VDP_INDEXED_FORMAT_I8A8,
605 (const void *const*)&index_data,
606 &pitch,
607 &output_indexed_rect_vid,
608 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
609 (void *)palette);
610 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed")
612 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
613 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
614 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
615 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
616 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
617 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
618 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
620 vdp_st = vdp_output_surface_render_output_surface(output_surface,
621 &output_indexed_rect_vid,
622 osd_surface,
623 &output_indexed_rect_vid,
624 NULL,
625 &blend_state,
626 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
627 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
630 static void draw_eosd(void) {
631 VdpStatus vdp_st;
632 VdpOutputSurface output_surface = output_surfaces[surface_num];
633 VdpOutputSurfaceRenderBlendState blend_state;
634 int i;
636 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
637 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA;
638 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
639 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
640 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA;
641 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
642 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
644 for (i=0; i<eosd_render_count; i++) {
645 vdp_st = vdp_output_surface_render_bitmap_surface(
646 output_surface, &eosd_targets[i].dest,
647 eosd_targets[i].surface, &eosd_targets[i].source,
648 &eosd_targets[i].color, &blend_state,
649 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
650 CHECK_ST_WARNING("EOSD: Error when rendering")
654 static void generate_eosd(mp_eosd_images_t *imgs) {
655 VdpStatus vdp_st;
656 VdpRect destRect;
657 int j, found;
658 ass_image_t *img = imgs->imgs;
659 ass_image_t *i;
661 // Nothing changed, no need to redraw
662 if (imgs->changed == 0)
663 return;
664 eosd_render_count = 0;
665 // There's nothing to render!
666 if (!img)
667 return;
669 if (imgs->changed == 1)
670 goto eosd_skip_upload;
672 for (j=0; j<eosd_surface_count; j++)
673 eosd_surfaces[j].in_use = 0;
675 for (i = img; i; i = i->next) {
676 // Try to reuse a suitable surface
677 found = -1;
678 for (j=0; j<eosd_surface_count; j++) {
679 if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE && !eosd_surfaces[j].in_use &&
680 eosd_surfaces[j].w >= i->w && eosd_surfaces[j].h >= i->h) {
681 found = j;
682 break;
685 // None found, allocate a new surface
686 if (found < 0) {
687 for (j=0; j<eosd_surface_count; j++) {
688 if (!eosd_surfaces[j].in_use) {
689 if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE)
690 vdp_bitmap_surface_destroy(eosd_surfaces[j].surface);
691 found = j;
692 break;
695 // Allocate new space for surface/target arrays
696 if (found < 0) {
697 j = found = eosd_surface_count;
698 eosd_surface_count = eosd_surface_count ? eosd_surface_count*2 : EOSD_SURFACES_INITIAL;
699 eosd_surfaces = realloc(eosd_surfaces, eosd_surface_count * sizeof(*eosd_surfaces));
700 eosd_targets = realloc(eosd_targets, eosd_surface_count * sizeof(*eosd_targets));
701 for(j=found; j<eosd_surface_count; j++) {
702 eosd_surfaces[j].surface = VDP_INVALID_HANDLE;
703 eosd_surfaces[j].in_use = 0;
706 vdp_st = vdp_bitmap_surface_create(vdp_device, VDP_RGBA_FORMAT_A8,
707 i->w, i->h, VDP_TRUE, &eosd_surfaces[found].surface);
708 CHECK_ST_WARNING("EOSD: error when creating surface")
709 eosd_surfaces[found].w = i->w;
710 eosd_surfaces[found].h = i->h;
712 eosd_surfaces[found].in_use = 1;
713 eosd_targets[eosd_render_count].surface = eosd_surfaces[found].surface;
714 destRect.x0 = 0;
715 destRect.y0 = 0;
716 destRect.x1 = i->w;
717 destRect.y1 = i->h;
718 vdp_st = vdp_bitmap_surface_putbits_native(eosd_targets[eosd_render_count].surface,
719 (const void *) &i->bitmap, &i->stride, &destRect);
720 CHECK_ST_WARNING("EOSD: putbits failed")
721 eosd_render_count++;
724 eosd_skip_upload:
725 eosd_render_count = 0;
726 for (i = img; i; i = i->next) {
727 // Render dest, color, etc.
728 eosd_targets[eosd_render_count].color.alpha = 1.0 - ((i->color >> 0) & 0xff) / 255.0;
729 eosd_targets[eosd_render_count].color.blue = ((i->color >> 8) & 0xff) / 255.0;
730 eosd_targets[eosd_render_count].color.green = ((i->color >> 16) & 0xff) / 255.0;
731 eosd_targets[eosd_render_count].color.red = ((i->color >> 24) & 0xff) / 255.0;
732 eosd_targets[eosd_render_count].dest.x0 = i->dst_x;
733 eosd_targets[eosd_render_count].dest.y0 = i->dst_y;
734 eosd_targets[eosd_render_count].dest.x1 = i->w + i->dst_x;
735 eosd_targets[eosd_render_count].dest.y1 = i->h + i->dst_y;
736 eosd_targets[eosd_render_count].source.x0 = 0;
737 eosd_targets[eosd_render_count].source.y0 = 0;
738 eosd_targets[eosd_render_count].source.x1 = i->w;
739 eosd_targets[eosd_render_count].source.y1 = i->h;
740 eosd_render_count++;
744 static void draw_osd(void)
746 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
748 vo_draw_text_ext(vo_dwidth, vo_dheight, border_x, border_y, border_x, border_y,
749 vid_width, vid_height, draw_osd_I8A8);
752 static void flip_page(void)
754 VdpStatus vdp_st;
755 mp_msg(MSGT_VO, MSGL_DBG2, "\nFLIP_PAGE VID:%u -> OUT:%u\n",
756 surface_render[vid_surface_num].surface, output_surfaces[surface_num]);
758 vdp_st = vdp_presentation_queue_display(vdp_flip_queue, output_surfaces[surface_num],
759 vo_dwidth, vo_dheight,
761 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
763 surface_num = (surface_num + 1) % NUM_OUTPUT_SURFACES;
764 visible_buf = 1;
767 static int draw_slice(uint8_t *image[], int stride[], int w, int h,
768 int x, int y)
770 VdpStatus vdp_st;
771 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
772 int max_refs = image_format == IMGFMT_VDPAU_H264 ? rndr->info.h264.num_ref_frames : 2;
773 if (!IMGFMT_IS_VDPAU(image_format))
774 return VO_FALSE;
775 if (decoder == VDP_INVALID_HANDLE || decoder_max_refs < max_refs) {
776 VdpDecoderProfile vdp_decoder_profile;
777 if (decoder != VDP_INVALID_HANDLE)
778 vdp_decoder_destroy(decoder);
779 decoder = VDP_INVALID_HANDLE;
780 switch (image_format) {
781 case IMGFMT_VDPAU_MPEG1:
782 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
783 break;
784 case IMGFMT_VDPAU_MPEG2:
785 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
786 break;
787 case IMGFMT_VDPAU_H264:
788 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
789 break;
790 case IMGFMT_VDPAU_WMV3:
791 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
792 break;
793 case IMGFMT_VDPAU_VC1:
794 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
795 break;
797 vdp_st = vdp_decoder_create(vdp_device, vdp_decoder_profile, vid_width, vid_height, max_refs, &decoder);
798 CHECK_ST_WARNING("Failed creating VDPAU decoder");
799 if (vdp_st != VDP_STATUS_OK) {
800 decoder = VDP_INVALID_HANDLE;
801 decoder_max_refs = 0;
802 return VO_FALSE;
804 decoder_max_refs = max_refs;
806 vdp_st = vdp_decoder_render(decoder, rndr->surface, (void *)&rndr->info, rndr->bitstream_buffers_used, rndr->bitstream_buffers);
807 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
808 return VO_TRUE;
812 static int draw_frame(uint8_t *src[])
814 return VO_ERROR;
817 static struct vdpau_render_state *get_surface(int number)
819 if (number > MAX_VIDEO_SURFACES)
820 return NULL;
821 if (surface_render[number].surface == VDP_INVALID_HANDLE) {
822 VdpStatus vdp_st;
823 vdp_st = vdp_video_surface_create(vdp_device, vdp_chroma_type,
824 vid_width, vid_height,
825 &surface_render[number].surface);
826 CHECK_ST_WARNING("Error when calling vdp_video_surface_create")
827 if (vdp_st != VDP_STATUS_OK)
828 return NULL;
830 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n", surface_render[number].surface);
831 return &surface_render[number];
834 static uint32_t draw_image(mp_image_t *mpi)
836 if (IMGFMT_IS_VDPAU(image_format)) {
837 struct vdpau_render_state *rndr = mpi->priv;
838 vid_surface_num = rndr - surface_render;
839 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
840 VdpStatus vdp_st;
841 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
842 struct vdpau_render_state *rndr = get_surface(0);
843 vid_surface_num = rndr - surface_render;
844 vdp_st = vdp_video_surface_put_bits_y_cb_cr(rndr->surface,
845 VDP_YCBCR_FORMAT_YV12,
846 (const void *const*)destdata,
847 mpi->stride); // pitch
848 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
850 top_field_first = !!(mpi->fields & MP_IMGFIELD_TOP_FIRST);
852 video_to_output_surface();
853 return VO_TRUE;
856 static uint32_t get_image(mp_image_t *mpi)
858 struct vdpau_render_state *rndr;
860 // no dr for non-decoding for now
861 if (!IMGFMT_IS_VDPAU(image_format)) return VO_FALSE;
862 if (mpi->type != MP_IMGTYPE_NUMBERED) return VO_FALSE;
864 rndr = get_surface(mpi->number);
865 if (!rndr) {
866 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in get_image\n");
867 // TODO: this probably breaks things forever, provide a dummy buffer?
868 return VO_FALSE;
870 mpi->flags |= MP_IMGFLAG_DIRECT;
871 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
872 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
873 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
874 mpi->planes[0] = (void *)rndr;
875 mpi->num_planes = 1;
876 mpi->priv = rndr;
877 return VO_TRUE;
880 static int query_format(uint32_t format)
882 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD | VFCAP_EOSD_UNSCALED;
883 switch (format) {
884 case IMGFMT_YV12:
885 return default_flags | VOCAP_NOSLICES;
886 case IMGFMT_VDPAU_MPEG1:
887 case IMGFMT_VDPAU_MPEG2:
888 case IMGFMT_VDPAU_H264:
889 case IMGFMT_VDPAU_WMV3:
890 case IMGFMT_VDPAU_VC1:
891 return default_flags;
893 return 0;
896 static void DestroyVdpauObjects(void)
898 int i;
899 VdpStatus vdp_st;
901 free_video_specific();
903 vdp_st = vdp_presentation_queue_destroy(vdp_flip_queue);
904 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy")
906 vdp_st = vdp_presentation_queue_target_destroy(vdp_flip_target);
907 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy")
909 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++) {
910 vdp_st = vdp_output_surface_destroy(output_surfaces[i]);
911 output_surfaces[i] = VDP_INVALID_HANDLE;
912 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy")
915 for (i = 0; i<eosd_surface_count; i++) {
916 if (eosd_surfaces[i].surface != VDP_INVALID_HANDLE) {
917 vdp_st = vdp_bitmap_surface_destroy(eosd_surfaces[i].surface);
918 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy")
920 eosd_surfaces[i].surface = VDP_INVALID_HANDLE;
923 vdp_st = vdp_device_destroy(vdp_device);
924 CHECK_ST_WARNING("Error when calling vdp_device_destroy")
927 static void uninit(void)
929 if (!vo_config_count)
930 return;
931 visible_buf = 0;
933 /* Destroy all vdpau objects */
934 DestroyVdpauObjects();
936 free(index_data);
937 index_data = NULL;
939 free(eosd_surfaces);
940 eosd_surfaces = NULL;
941 free(eosd_targets);
942 eosd_targets = NULL;
944 #ifdef CONFIG_XF86VM
945 vo_vm_close();
946 #endif
947 vo_x11_uninit();
949 dlclose(vdpau_lib_handle);
952 static opt_t subopts[] = {
953 {"deint", OPT_ARG_INT, &deint, (opt_test_f)int_non_neg},
954 {"pullup", OPT_ARG_BOOL, &pullup, NULL},
955 {"denoise", OPT_ARG_FLOAT, &denoise, NULL},
956 {"sharpen", OPT_ARG_FLOAT, &sharpen, NULL},
957 {NULL}
960 static const char help_msg[] =
961 "\n-vo vdpau command line help:\n"
962 "Example: mplayer -vo vdpau:deint=2\n"
963 "\nOptions:\n"
964 " deint (all modes > 0 respect -field-dominance)\n"
965 " 0: no deinterlacing\n"
966 " 1: only show first field\n"
967 " 2: bob deinterlacing (current fallback)\n"
968 " 3: temporal deinterlacing (not yet working)\n"
969 " 4: temporal-spatial deinterlacing (not yet working)\n"
970 " pullup\n"
971 " Try to apply inverse-telecine (needs deinterlacing, not working)\n"
972 " denoise\n"
973 " Apply denoising, argument is strength from 0.0 to 1.0\n"
974 " sharpen\n"
975 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
978 static int preinit(const char *arg)
980 int i;
981 static const char *vdpaulibrary = "libvdpau.so.1";
982 static const char *vdpau_device_create = "vdp_device_create_x11";
984 deint = 0;
985 deint_type = 3;
986 pullup = 0;
987 denoise = 0;
988 sharpen = 0;
989 if (subopt_parse(arg, subopts) != 0) {
990 mp_msg(MSGT_VO, MSGL_FATAL, help_msg);
991 return -1;
993 if (deint)
994 deint_type = deint;
996 vdpau_lib_handle = dlopen(vdpaulibrary, RTLD_LAZY);
997 if (!vdpau_lib_handle) {
998 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not open dynamic library %s\n",
999 vdpaulibrary);
1000 return -1;
1002 vdp_device_create = dlsym(vdpau_lib_handle, vdpau_device_create);
1003 if (!vdp_device_create) {
1004 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not find function %s in %s\n",
1005 vdpau_device_create, vdpaulibrary);
1006 return -1;
1008 if (!vo_init() || win_x11_init_vdpau_procs())
1009 return -1;
1011 decoder = VDP_INVALID_HANDLE;
1012 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
1013 surface_render[i].surface = VDP_INVALID_HANDLE;
1014 video_mixer = VDP_INVALID_HANDLE;
1015 for (i = 0; i <= NUM_OUTPUT_SURFACES; i++)
1016 output_surfaces[i] = VDP_INVALID_HANDLE;
1017 vdp_flip_queue = VDP_INVALID_HANDLE;
1018 output_surface_width = output_surface_height = -1;
1020 // full grayscale palette.
1021 for (i = 0; i < PALETTE_SIZE; ++i)
1022 palette[i] = (i << 16) | (i << 8) | i;
1023 index_data = NULL;
1024 index_data_size = 0;
1026 eosd_surface_count = eosd_render_count = 0;
1027 eosd_surfaces = NULL;
1028 eosd_targets = NULL;
1030 procamp.struct_version = VDP_PROCAMP_VERSION;
1031 procamp.brightness = 0.0;
1032 procamp.contrast = 1.0;
1033 procamp.saturation = 1.0;
1034 procamp.hue = 0.0;
1036 return 0;
1039 static int get_equalizer(char *name, int *value) {
1040 if (!strcasecmp(name, "brightness"))
1041 *value = procamp.brightness * 100;
1042 else if (!strcasecmp(name, "contrast"))
1043 *value = (procamp.contrast-1.0) * 100;
1044 else if (!strcasecmp(name, "saturation"))
1045 *value = (procamp.saturation-1.0) * 100;
1046 else if (!strcasecmp(name, "hue"))
1047 *value = procamp.hue * 100 / M_PI;
1048 else
1049 return VO_NOTIMPL;
1050 return VO_TRUE;
1053 static int set_equalizer(char *name, int value) {
1054 VdpStatus vdp_st;
1055 VdpCSCMatrix matrix;
1056 static const VdpVideoMixerAttribute attributes[] = {VDP_VIDEO_MIXER_ATTRIBUTE_CSC_MATRIX};
1057 const void *attribute_values[] = {&matrix};
1059 if (!strcasecmp(name, "brightness"))
1060 procamp.brightness = value / 100.0;
1061 else if (!strcasecmp(name, "contrast"))
1062 procamp.contrast = value / 100.0 + 1.0;
1063 else if (!strcasecmp(name, "saturation"))
1064 procamp.saturation = value / 100.0 + 1.0;
1065 else if (!strcasecmp(name, "hue"))
1066 procamp.hue = value / 100.0 * M_PI;
1067 else
1068 return VO_NOTIMPL;
1070 vdp_st = vdp_generate_csc_matrix(&procamp, VDP_COLOR_STANDARD_ITUR_BT_601,
1071 &matrix);
1072 CHECK_ST_WARNING("Error when generating CSC matrix")
1073 vdp_st = vdp_video_mixer_set_attribute_values(video_mixer, 1, attributes,
1074 attribute_values);
1075 CHECK_ST_WARNING("Error when setting CSC matrix")
1076 return VO_TRUE;
1079 static int control(uint32_t request, void *data)
1081 switch (request) {
1082 case VOCTRL_GET_DEINTERLACE:
1083 *(int*)data = deint;
1084 return VO_TRUE;
1085 case VOCTRL_SET_DEINTERLACE:
1086 deint = *(int*)data;
1087 if (deint)
1088 deint = deint_type;
1089 return VO_TRUE;
1090 case VOCTRL_PAUSE:
1091 return (int_pause = 1);
1092 case VOCTRL_RESUME:
1093 return (int_pause = 0);
1094 case VOCTRL_QUERY_FORMAT:
1095 return query_format(*(uint32_t *)data);
1096 case VOCTRL_GET_IMAGE:
1097 return get_image(data);
1098 case VOCTRL_DRAW_IMAGE:
1099 return draw_image(data);
1100 case VOCTRL_GUISUPPORT:
1101 return VO_TRUE;
1102 case VOCTRL_BORDER:
1103 vo_x11_border();
1104 resize();
1105 return VO_TRUE;
1106 case VOCTRL_FULLSCREEN:
1107 vo_x11_fullscreen();
1108 resize();
1109 return VO_TRUE;
1110 case VOCTRL_GET_PANSCAN:
1111 return VO_TRUE;
1112 case VOCTRL_SET_PANSCAN:
1113 resize();
1114 return VO_TRUE;
1115 case VOCTRL_SET_EQUALIZER: {
1116 struct voctrl_set_equalizer_args *args = data;
1117 return set_equalizer(args->name, args->value);
1119 case VOCTRL_GET_EQUALIZER:
1121 struct voctrl_get_equalizer_args *args = data;
1122 return get_equalizer(args->name, args->valueptr);
1124 case VOCTRL_ONTOP:
1125 vo_x11_ontop();
1126 return VO_TRUE;
1127 case VOCTRL_UPDATE_SCREENINFO:
1128 update_xinerama_info();
1129 return VO_TRUE;
1130 case VOCTRL_DRAW_EOSD:
1131 if (!data)
1132 return VO_FALSE;
1133 generate_eosd(data);
1134 draw_eosd();
1135 return VO_TRUE;
1136 case VOCTRL_GET_EOSD_RES: {
1137 mp_eosd_res_t *r = data;
1138 r->mt = r->mb = r->ml = r->mr = 0;
1139 if (vo_fs) {
1140 r->w = vo_screenwidth;
1141 r->h = vo_screenheight;
1142 r->ml = r->mr = border_x;
1143 r->mt = r->mb = border_y;
1144 } else {
1145 r->w = vo_dwidth;
1146 r->h = vo_dheight;
1148 return VO_TRUE;
1151 return VO_NOTIMPL;
1154 /* @} */