Add support for VDPAU deinterlacing, pullup, denoise and sharpening.
[mplayer/glamo.git] / libvo / vo_vdpau.c
blob0cb9d682f0706cc113203288cd82171425bb2d46
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"
53 static vo_info_t info = {
54 "VDPAU with X11",
55 "vdpau",
56 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
60 LIBVO_EXTERN(vdpau)
62 #define CHECK_ST_ERROR(message) \
63 if (vdp_st != VDP_STATUS_OK) { \
64 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
65 message, vdp_get_error_string(vdp_st)); \
66 return -1; \
69 #define CHECK_ST_WARNING(message) \
70 if (vdp_st != VDP_STATUS_OK) \
71 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] %s: %s\n", \
72 message, vdp_get_error_string(vdp_st));
74 /* number of video and output surfaces */
75 #define NUM_OUTPUT_SURFACES 3
76 #define MAX_VIDEO_SURFACES 50
78 /* number of palette entries */
79 #define PALETTE_SIZE 256
82 * Global variable declaration - VDPAU specific
85 /* Declaration for all variables of win_x11_init_vdpau_procs() and
86 * win_x11_init_vdpau_flip_queue() functions
88 static VdpDevice vdp_device;
89 static VdpDeviceCreateX11 *vdp_device_create;
90 static VdpGetProcAddress *vdp_get_proc_address;
92 static VdpPresentationQueueTarget vdp_flip_target;
93 static VdpPresentationQueue vdp_flip_queue;
95 static VdpDeviceDestroy *vdp_device_destroy;
96 static VdpVideoSurfaceCreate *vdp_video_surface_create;
97 static VdpVideoSurfaceDestroy *vdp_video_surface_destroy;
99 static VdpGetErrorString *vdp_get_error_string;
101 /* May be used in software filtering/postprocessing options
102 * in MPlayer (./mplayer -vf ..) if we copy video_surface data to
103 * system memory.
105 static VdpVideoSurfacePutBitsYCbCr *vdp_video_surface_put_bits_y_cb_cr;
106 static VdpOutputSurfacePutBitsNative *vdp_output_surface_put_bits_native;
108 static VdpOutputSurfaceCreate *vdp_output_surface_create;
109 static VdpOutputSurfaceDestroy *vdp_output_surface_destroy;
111 /* VideoMixer puts video_surface data on displayable output_surface. */
112 static VdpVideoMixerCreate *vdp_video_mixer_create;
113 static VdpVideoMixerDestroy *vdp_video_mixer_destroy;
114 static VdpVideoMixerRender *vdp_video_mixer_render;
115 static VdpVideoMixerSetFeatureEnables *vdp_video_mixer_set_feature_enables;
116 static VdpVideoMixerSetAttributeValues *vdp_video_mixer_set_attribute_values;
118 static VdpPresentationQueueTargetDestroy *vdp_presentation_queue_target_destroy;
119 static VdpPresentationQueueCreate *vdp_presentation_queue_create;
120 static VdpPresentationQueueDestroy *vdp_presentation_queue_destroy;
121 static VdpPresentationQueueDisplay *vdp_presentation_queue_display;
122 static VdpPresentationQueueBlockUntilSurfaceIdle *vdp_presentation_queue_block_until_surface_idle;
123 static VdpPresentationQueueTargetCreateX11 *vdp_presentation_queue_target_create_x11;
125 /* output_surfaces[2] is used in composite-picture. */
126 static VdpOutputSurfaceRenderOutputSurface *vdp_output_surface_render_output_surface;
127 static VdpOutputSurfacePutBitsIndexed *vdp_output_surface_put_bits_indexed;
129 static VdpDecoderCreate *vdp_decoder_create;
130 static VdpDecoderDestroy *vdp_decoder_destroy;
131 static VdpDecoderRender *vdp_decoder_render;
133 static void *vdpau_lib_handle;
134 static VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES];
135 static int output_surface_width, output_surface_height;
137 static VdpVideoMixer video_mixer;
138 static int deint;
139 static int pullup;
140 static float denoise;
141 static float sharpen;
143 static VdpDecoder decoder;
144 static int decoder_max_refs;
146 static VdpRect src_rect_vid;
147 static VdpRect out_rect_vid;
148 static int border_x, border_y;
150 static struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES];
151 static int surface_num;
152 static int vid_surface_num;
153 static uint32_t vid_width, vid_height;
154 static uint32_t image_format;
155 static const VdpChromaType vdp_chroma_type = VDP_CHROMA_TYPE_420;
157 /* draw_osd */
158 static unsigned char *index_data;
159 static int index_data_size;
160 static uint32_t palette[PALETTE_SIZE];
163 * X11 specific
165 static int visible_buf;
166 static int int_pause;
168 static void video_to_output_surface(void)
170 VdpTime dummy;
171 VdpStatus vdp_st;
172 VdpOutputSurface output_surface = output_surfaces[surface_num];
173 if (vid_surface_num < 0)
174 return;
176 vdp_st = vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue,
177 output_surface,
178 &dummy);
179 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
181 vdp_st = vdp_video_mixer_render(video_mixer, VDP_INVALID_HANDLE, 0,
182 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME,
183 0, NULL, surface_render[vid_surface_num].surface, 0, NULL, &src_rect_vid,
184 output_surface,
185 NULL, &out_rect_vid, 0, NULL);
186 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
189 static void resize(void)
191 VdpStatus vdp_st;
192 int i;
193 struct vo_rect src_rect;
194 struct vo_rect dst_rect;
195 struct vo_rect borders;
196 calc_src_dst_rects(vid_width, vid_height, &src_rect, &dst_rect, &borders, NULL);
197 out_rect_vid.x0 = dst_rect.left;
198 out_rect_vid.x1 = dst_rect.right;
199 out_rect_vid.y0 = dst_rect.top;
200 out_rect_vid.y1 = dst_rect.bottom;
201 src_rect_vid.x0 = src_rect.left;
202 src_rect_vid.x1 = src_rect.right;
203 src_rect_vid.y0 = src_rect.top;
204 src_rect_vid.y1 = src_rect.bottom;
205 border_x = borders.left;
206 border_y = borders.top;
207 #ifdef CONFIG_FREETYPE
208 // adjust font size to display size
209 force_load_font = 1;
210 #endif
211 vo_osd_changed(OSDTYPE_OSD);
213 if (output_surface_width < vo_dwidth || output_surface_height < vo_dheight) {
214 if (output_surface_width < vo_dwidth) {
215 output_surface_width += output_surface_width >> 1;
216 output_surface_width = FFMAX(output_surface_width, vo_dwidth);
218 if (output_surface_height < vo_dheight) {
219 output_surface_height += output_surface_height >> 1;
220 output_surface_height = FFMAX(output_surface_height, vo_dheight);
222 // Creation of output_surfaces
223 for (i = 0; i < NUM_OUTPUT_SURFACES; i++) {
224 if (output_surfaces[i] != VDP_INVALID_HANDLE)
225 vdp_output_surface_destroy(output_surfaces[i]);
226 vdp_st = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8,
227 output_surface_width, output_surface_height,
228 &output_surfaces[i]);
229 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
230 mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", output_surfaces[i]);
233 video_to_output_surface();
234 if (visible_buf)
235 flip_page();
238 /* Initialize vdp_get_proc_address, called from preinit() */
239 static int win_x11_init_vdpau_procs(void)
241 VdpStatus vdp_st;
243 struct vdp_function {
244 const int id;
245 void *pointer;
248 const struct vdp_function *dsc;
250 static const struct vdp_function vdp_func[] = {
251 {VDP_FUNC_ID_GET_ERROR_STRING, &vdp_get_error_string},
252 {VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy},
253 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE, &vdp_video_surface_create},
254 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, &vdp_video_surface_destroy},
255 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
256 &vdp_video_surface_put_bits_y_cb_cr},
257 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE,
258 &vdp_output_surface_put_bits_native},
259 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create},
260 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy},
261 {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create},
262 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy},
263 {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render},
264 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
265 &vdp_video_mixer_set_feature_enables},
266 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
267 &vdp_video_mixer_set_attribute_values},
268 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
269 &vdp_presentation_queue_target_destroy},
270 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, &vdp_presentation_queue_create},
271 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
272 &vdp_presentation_queue_destroy},
273 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
274 &vdp_presentation_queue_display},
275 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
276 &vdp_presentation_queue_block_until_surface_idle},
277 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
278 &vdp_presentation_queue_target_create_x11},
279 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE,
280 &vdp_output_surface_render_output_surface},
281 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED,
282 &vdp_output_surface_put_bits_indexed},
283 {VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create},
284 {VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render},
285 {VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy},
286 {0, NULL}
289 vdp_st = vdp_device_create(mDisplay, mScreen,
290 &vdp_device, &vdp_get_proc_address);
291 if (vdp_st != VDP_STATUS_OK) {
292 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st);
293 return -1;
296 for (dsc = vdp_func; dsc->pointer; dsc++) {
297 vdp_st = vdp_get_proc_address(vdp_device, dsc->id, dsc->pointer);
298 if (vdp_st != VDP_STATUS_OK) {
299 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) : "?");
300 return -1;
303 return 0;
306 /* Initialize vdpau_flip_queue, called from config() */
307 static int win_x11_init_vdpau_flip_queue(void)
309 VdpStatus vdp_st;
311 vdp_st = vdp_presentation_queue_target_create_x11(vdp_device, vo_window,
312 &vdp_flip_target);
313 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
315 vdp_st = vdp_presentation_queue_create(vdp_device, vdp_flip_target,
316 &vdp_flip_queue);
317 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
319 return 0;
322 static int create_vdp_mixer(VdpChromaType vdp_chroma_type) {
323 #define VDP_NUM_MIXER_PARAMETER 3
324 #define MAX_NUM_FEATURES 5
325 int i;
326 VdpStatus vdp_st;
327 int feature_count = 0;
328 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
329 VdpBool feature_enables[MAX_NUM_FEATURES];
330 static const denoise_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL};
331 const void * const denoise_value[] = {&denoise};
332 static const sharpen_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL};
333 const void * const sharpen_value[] = {&sharpen};
334 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
335 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
336 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
337 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
339 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
340 &vid_width,
341 &vid_height,
342 &vdp_chroma_type
344 if (deint == 1)
345 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
346 if (deint == 2)
347 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
348 if (pullup)
349 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
350 if (denoise)
351 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
352 if (sharpen)
353 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
355 vdp_st = vdp_video_mixer_create(vdp_device, feature_count, features,
356 VDP_NUM_MIXER_PARAMETER,
357 parameters, parameter_values,
358 &video_mixer);
359 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
361 for (i = 0; i < feature_count; i++) feature_enables[i] = VDP_TRUE;
362 if (feature_count)
363 vdp_video_mixer_set_feature_enables(video_mixer, feature_count, features, feature_enables);
364 if (denoise)
365 vdp_video_mixer_set_attribute_values(video_mixer, 1, denoise_attrib, denoise_value);
366 if (sharpen)
367 vdp_video_mixer_set_attribute_values(video_mixer, 1, sharpen_attrib, sharpen_value);
369 return 0;
372 // Free everything specific to a certain video file
373 static void free_video_specific(void) {
374 int i;
375 VdpStatus vdp_st;
377 if (decoder != VDP_INVALID_HANDLE)
378 vdp_decoder_destroy(decoder);
379 decoder = VDP_INVALID_HANDLE;
380 decoder_max_refs = -1;
382 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
383 if (surface_render[i].surface != VDP_INVALID_HANDLE) {
384 vdp_st = vdp_video_surface_destroy(surface_render[i].surface);
385 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
387 surface_render[i].surface = VDP_INVALID_HANDLE;
390 if (video_mixer != VDP_INVALID_HANDLE)
391 vdp_st = vdp_video_mixer_destroy(video_mixer);
392 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
393 video_mixer = VDP_INVALID_HANDLE;
397 * connect to X server, create and map window, initialize all
398 * VDPAU objects, create different surfaces etc.
400 static int config(uint32_t width, uint32_t height, uint32_t d_width,
401 uint32_t d_height, uint32_t flags, char *title,
402 uint32_t format)
404 XVisualInfo vinfo;
405 XSetWindowAttributes xswa;
406 XWindowAttributes attribs;
407 unsigned long xswamask;
408 int depth;
410 #ifdef CONFIG_XF86VM
411 int vm = flags & VOFLAG_MODESWITCHING;
412 #endif
414 image_format = format;
416 int_pause = 0;
417 visible_buf = 0;
419 #ifdef CONFIG_GUI
420 if (use_gui)
421 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize our window
422 else
423 #endif
425 #ifdef CONFIG_XF86VM
426 if (vm)
427 vo_vm_switch();
428 else
429 #endif
430 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
431 depth = attribs.depth;
432 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
433 depth = 24;
434 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
436 xswa.background_pixel = 0;
437 xswa.border_pixel = 0;
438 xswamask = CWBackPixel | CWBorderPixel;
440 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height,
441 flags, CopyFromParent, "vdpau", title);
442 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
444 #ifdef CONFIG_XF86VM
445 if (vm) {
446 /* Grab the mouse pointer in our window */
447 if (vo_grabpointer)
448 XGrabPointer(mDisplay, vo_window, True, 0,
449 GrabModeAsync, GrabModeAsync,
450 vo_window, None, CurrentTime);
451 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
453 #endif
456 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
457 vo_fs = 1;
459 /* -----VDPAU related code here -------- */
461 free_video_specific();
463 if (vdp_flip_queue == VDP_INVALID_HANDLE && win_x11_init_vdpau_flip_queue())
464 return -1;
466 // video width and height
467 vid_width = width;
468 vid_height = height;
470 if (create_vdp_mixer(vdp_chroma_type))
471 return -1;
473 surface_num = 0;
474 vid_surface_num = -1;
475 resize();
477 return 0;
480 static void check_events(void)
482 int e = vo_x11_check_events(mDisplay);
484 if (e & VO_EVENT_RESIZE)
485 resize();
487 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && int_pause) {
488 /* did we already draw a buffer */
489 if (visible_buf) {
490 /* redraw the last visible buffer */
491 VdpStatus vdp_st;
492 vdp_st = vdp_presentation_queue_display(vdp_flip_queue,
493 output_surfaces[surface_num],
494 vo_dwidth, vo_dheight,
496 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
501 static void draw_osd_I8A8(int x0,int y0, int w,int h, unsigned char *src,
502 unsigned char *srca, int stride)
504 VdpOutputSurface output_surface = output_surfaces[surface_num];
505 VdpStatus vdp_st;
506 int i, j;
507 int pitch;
508 int index_data_size_required;
509 VdpRect output_indexed_rect_vid;
510 VdpOutputSurfaceRenderBlendState blend_state;
512 if (!w || !h)
513 return;
515 index_data_size_required = 2*w*h;
516 if (index_data_size < index_data_size_required) {
517 index_data = realloc(index_data, index_data_size_required);
518 index_data_size = index_data_size_required;
521 // index_data creation, component order - I, A, I, A, .....
522 for (i = 0; i < h; i++)
523 for (j = 0; j < w; j++) {
524 index_data[i*2*w + j*2] = src [i*stride+j];
525 index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
528 output_indexed_rect_vid.x0 = x0;
529 output_indexed_rect_vid.y0 = y0;
530 output_indexed_rect_vid.x1 = x0 + w;
531 output_indexed_rect_vid.y1 = y0 + h;
533 pitch = w*2;
535 // write source_data to output_surfaces[2].
536 vdp_st = vdp_output_surface_put_bits_indexed(output_surfaces[2],
537 VDP_INDEXED_FORMAT_I8A8,
538 (const void *const*)&index_data,
539 &pitch,
540 &output_indexed_rect_vid,
541 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
542 (void *)palette);
543 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed")
545 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
546 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
547 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
548 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
549 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
550 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
551 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
553 vdp_st = vdp_output_surface_render_output_surface(output_surface,
554 &output_indexed_rect_vid,
555 output_surfaces[2],
556 &output_indexed_rect_vid,
557 NULL,
558 &blend_state,
559 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
560 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
563 static void draw_osd(void)
565 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
567 vo_draw_text_ext(vo_dwidth, vo_dheight, border_x, border_y, border_x, border_y,
568 vid_width, vid_height, draw_osd_I8A8);
571 static void flip_page(void)
573 VdpStatus vdp_st;
574 mp_msg(MSGT_VO, MSGL_DBG2, "\nFLIP_PAGE VID:%u -> OUT:%u\n",
575 surface_render[vid_surface_num].surface, output_surfaces[surface_num]);
577 vdp_st = vdp_presentation_queue_display(vdp_flip_queue, output_surfaces[surface_num],
578 vo_dwidth, vo_dheight,
580 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
582 surface_num = !surface_num;
583 visible_buf = 1;
586 static int draw_slice(uint8_t *image[], int stride[], int w, int h,
587 int x, int y)
589 VdpStatus vdp_st;
590 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
591 int max_refs = image_format == IMGFMT_VDPAU_H264 ? rndr->info.h264.num_ref_frames : 2;
592 if (!IMGFMT_IS_VDPAU(image_format))
593 return VO_FALSE;
594 if (decoder == VDP_INVALID_HANDLE || decoder_max_refs < max_refs) {
595 VdpDecoderProfile vdp_decoder_profile;
596 if (decoder != VDP_INVALID_HANDLE)
597 vdp_decoder_destroy(decoder);
598 decoder = VDP_INVALID_HANDLE;
599 switch (image_format) {
600 case IMGFMT_VDPAU_MPEG1:
601 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
602 break;
603 case IMGFMT_VDPAU_MPEG2:
604 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
605 break;
606 case IMGFMT_VDPAU_H264:
607 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
608 break;
609 case IMGFMT_VDPAU_WMV3:
610 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
611 break;
612 case IMGFMT_VDPAU_VC1:
613 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
614 break;
616 vdp_st = vdp_decoder_create(vdp_device, vdp_decoder_profile, vid_width, vid_height, max_refs, &decoder);
617 CHECK_ST_WARNING("Failed creating VDPAU decoder");
618 decoder_max_refs = max_refs;
620 vdp_st = vdp_decoder_render(decoder, rndr->surface, (void *)&rndr->info, rndr->bitstream_buffers_used, rndr->bitstream_buffers);
621 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
622 return VO_TRUE;
626 static int draw_frame(uint8_t *src[])
628 return VO_ERROR;
631 static struct vdpau_render_state *get_surface(int number)
633 if (number > MAX_VIDEO_SURFACES)
634 return NULL;
635 if (surface_render[number].surface == VDP_INVALID_HANDLE) {
636 VdpStatus vdp_st;
637 vdp_st = vdp_video_surface_create(vdp_device, vdp_chroma_type,
638 vid_width, vid_height,
639 &surface_render[number].surface);
640 CHECK_ST_WARNING("Error when calling vdp_video_surface_create")
641 if (vdp_st != VDP_STATUS_OK)
642 return NULL;
644 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n", surface_render[number].surface);
645 return &surface_render[number];
648 static uint32_t draw_image(mp_image_t *mpi)
650 if (IMGFMT_IS_VDPAU(image_format)) {
651 struct vdpau_render_state *rndr = mpi->priv;
652 vid_surface_num = rndr - surface_render;
653 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
654 VdpStatus vdp_st;
655 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
656 struct vdpau_render_state *rndr = get_surface(0);
657 vid_surface_num = rndr - surface_render;
658 vdp_st = vdp_video_surface_put_bits_y_cb_cr(rndr->surface,
659 VDP_YCBCR_FORMAT_YV12,
660 (const void *const*)destdata,
661 mpi->stride); // pitch
662 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
665 video_to_output_surface();
666 return VO_TRUE;
669 static uint32_t get_image(mp_image_t *mpi)
671 struct vdpau_render_state *rndr;
673 // no dr for non-decoding for now
674 if (!IMGFMT_IS_VDPAU(image_format)) return VO_FALSE;
675 if (mpi->type != MP_IMGTYPE_NUMBERED) return VO_FALSE;
677 rndr = get_surface(mpi->number);
678 if (!rndr) {
679 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in get_image\n");
680 // TODO: this probably breaks things forever, provide a dummy buffer?
681 return VO_FALSE;
683 mpi->flags |= MP_IMGFLAG_DIRECT;
684 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
685 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
686 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
687 mpi->planes[0] = (void *)rndr;
688 mpi->num_planes = 1;
689 mpi->priv = rndr;
690 return VO_TRUE;
693 static int query_format(uint32_t format)
695 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD;
696 switch (format) {
697 case IMGFMT_YV12:
698 return default_flags | VOCAP_NOSLICES;
699 case IMGFMT_VDPAU_MPEG1:
700 case IMGFMT_VDPAU_MPEG2:
701 case IMGFMT_VDPAU_H264:
702 case IMGFMT_VDPAU_WMV3:
703 case IMGFMT_VDPAU_VC1:
704 return default_flags;
706 return 0;
709 static void DestroyVdpauObjects(void)
711 int i;
712 VdpStatus vdp_st;
714 free_video_specific();
716 vdp_st = vdp_presentation_queue_destroy(vdp_flip_queue);
717 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy")
719 vdp_st = vdp_presentation_queue_target_destroy(vdp_flip_target);
720 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy")
722 for (i = 0; i < NUM_OUTPUT_SURFACES; i++) {
723 vdp_st = vdp_output_surface_destroy(output_surfaces[i]);
724 output_surfaces[i] = VDP_INVALID_HANDLE;
725 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy")
728 vdp_st = vdp_device_destroy(vdp_device);
729 CHECK_ST_WARNING("Error when calling vdp_device_destroy")
732 static void uninit(void)
734 if (!vo_config_count)
735 return;
736 visible_buf = 0;
738 /* Destroy all vdpau objects */
739 DestroyVdpauObjects();
741 free(index_data);
742 index_data = NULL;
744 #ifdef CONFIG_XF86VM
745 vo_vm_close();
746 #endif
747 vo_x11_uninit();
749 dlclose(vdpau_lib_handle);
752 static opt_t subopts[] = {
753 {"deint", OPT_ARG_INT, &deint, (opt_test_f)int_non_neg},
754 {"pullup", OPT_ARG_BOOL, &pullup, NULL},
755 {"denoise", OPT_ARG_FLOAT, &denoise, NULL},
756 {"sharpen", OPT_ARG_FLOAT, &sharpen, NULL},
757 {NULL}
760 static const char help_msg[] =
761 "\n-vo vdpau command line help:\n"
762 "Example: mplayer -vo vdpau:deint=2\n"
763 "\nOptions:\n"
764 " deint\n"
765 " 0: no deinterlacing\n"
766 " 1: temporal deinterlacing\n"
767 " 2: temporal-spatial deinterlacing\n"
768 " pullup\n"
769 " Try to apply inverse-telecine\n"
770 " denoise\n"
771 " Apply denoising, argument is strength from 0.0 to 1.0\n"
772 " sharpen\n"
773 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
776 static int preinit(const char *arg)
778 int i;
779 static const char *vdpaulibrary = "libvdpau.so.1";
780 static const char *vdpau_device_create = "vdp_device_create_x11";
782 deint = 0;
783 pullup = 0;
784 denoise = 0;
785 sharpen = 0;
786 if (subopt_parse(arg, subopts) != 0) {
787 mp_msg(MSGT_VO, MSGL_FATAL, help_msg);
788 return -1;
791 vdpau_lib_handle = dlopen(vdpaulibrary, RTLD_LAZY);
792 if (!vdpau_lib_handle) {
793 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not open dynamic library %s\n",
794 vdpaulibrary);
795 return -1;
797 vdp_device_create = dlsym(vdpau_lib_handle, vdpau_device_create);
798 if (!vdp_device_create) {
799 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not find function %s in %s\n",
800 vdpau_device_create, vdpaulibrary);
801 return -1;
803 if (!vo_init() || win_x11_init_vdpau_procs())
804 return -1;
806 decoder = VDP_INVALID_HANDLE;
807 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
808 surface_render[i].surface = VDP_INVALID_HANDLE;
809 video_mixer = VDP_INVALID_HANDLE;
810 for (i = 0; i < NUM_OUTPUT_SURFACES; i++)
811 output_surfaces[i] = VDP_INVALID_HANDLE;
812 vdp_flip_queue = VDP_INVALID_HANDLE;
813 output_surface_width = output_surface_height = -1;
815 // full grayscale palette.
816 for (i = 0; i < PALETTE_SIZE; ++i)
817 palette[i] = (i << 16) | (i << 8) | i;
818 index_data = NULL;
819 index_data_size = 0;
821 return 0;
824 static int control(uint32_t request, void *data, ...)
826 switch (request) {
827 case VOCTRL_PAUSE:
828 return (int_pause = 1);
829 case VOCTRL_RESUME:
830 return (int_pause = 0);
831 case VOCTRL_QUERY_FORMAT:
832 return query_format(*(uint32_t *)data);
833 case VOCTRL_GET_IMAGE:
834 return get_image(data);
835 case VOCTRL_DRAW_IMAGE:
836 return draw_image(data);
837 case VOCTRL_GUISUPPORT:
838 return VO_TRUE;
839 case VOCTRL_BORDER:
840 vo_x11_border();
841 resize();
842 return VO_TRUE;
843 case VOCTRL_FULLSCREEN:
844 vo_x11_fullscreen();
845 resize();
846 return VO_TRUE;
847 case VOCTRL_GET_PANSCAN:
848 return VO_TRUE;
849 case VOCTRL_SET_PANSCAN:
850 resize();
851 return VO_TRUE;
852 case VOCTRL_SET_EQUALIZER: {
853 va_list ap;
854 int value;
856 va_start(ap, data);
857 value = va_arg(ap, int);
859 va_end(ap);
860 return vo_x11_set_equalizer(data, value);
862 case VOCTRL_GET_EQUALIZER: {
863 va_list ap;
864 int *value;
866 va_start(ap, data);
867 value = va_arg(ap, int *);
869 va_end(ap);
870 return vo_x11_get_equalizer(data, value);
872 case VOCTRL_ONTOP:
873 vo_x11_ontop();
874 return VO_TRUE;
875 case VOCTRL_UPDATE_SCREENINFO:
876 update_xinerama_info();
877 return VO_TRUE;
879 return VO_NOTIMPL;
882 /* @} */