Only check for vdp_video_mixer_destroy failure when we actually executed that function.
[mplayer/glamo.git] / libvo / vo_vdpau.c
blobe60c36c3ab6e70abc624e7b0b6daabe43f505dc1
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 #include "libass/ass.h"
54 #include "libass/ass_mp.h"
56 static vo_info_t info = {
57 "VDPAU with X11",
58 "vdpau",
59 "Rajib Mahapatra <rmahapatra@nvidia.com> and others",
63 LIBVO_EXTERN(vdpau)
65 #define CHECK_ST_ERROR(message) \
66 if (vdp_st != VDP_STATUS_OK) { \
67 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] %s: %s\n", \
68 message, vdp_get_error_string(vdp_st)); \
69 return -1; \
72 #define CHECK_ST_WARNING(message) \
73 if (vdp_st != VDP_STATUS_OK) \
74 mp_msg(MSGT_VO, MSGL_WARN, "[vdpau] %s: %s\n", \
75 message, vdp_get_error_string(vdp_st));
77 /* number of video and output surfaces */
78 #define NUM_OUTPUT_SURFACES 3
79 #define MAX_VIDEO_SURFACES 50
81 /* number of palette entries */
82 #define PALETTE_SIZE 256
84 /* Initial maximum number of EOSD surfaces */
85 #define EOSD_SURFACES_INITIAL 512
88 * Global variable declaration - VDPAU specific
91 /* Declaration for all variables of win_x11_init_vdpau_procs() and
92 * win_x11_init_vdpau_flip_queue() functions
94 static VdpDevice vdp_device;
95 static VdpDeviceCreateX11 *vdp_device_create;
96 static VdpGetProcAddress *vdp_get_proc_address;
98 static VdpPresentationQueueTarget vdp_flip_target;
99 static VdpPresentationQueue vdp_flip_queue;
101 static VdpDeviceDestroy *vdp_device_destroy;
102 static VdpVideoSurfaceCreate *vdp_video_surface_create;
103 static VdpVideoSurfaceDestroy *vdp_video_surface_destroy;
105 static VdpGetErrorString *vdp_get_error_string;
107 /* May be used in software filtering/postprocessing options
108 * in MPlayer (./mplayer -vf ..) if we copy video_surface data to
109 * system memory.
111 static VdpVideoSurfacePutBitsYCbCr *vdp_video_surface_put_bits_y_cb_cr;
112 static VdpOutputSurfacePutBitsNative *vdp_output_surface_put_bits_native;
114 static VdpOutputSurfaceCreate *vdp_output_surface_create;
115 static VdpOutputSurfaceDestroy *vdp_output_surface_destroy;
117 /* VideoMixer puts video_surface data on displayable output_surface. */
118 static VdpVideoMixerCreate *vdp_video_mixer_create;
119 static VdpVideoMixerDestroy *vdp_video_mixer_destroy;
120 static VdpVideoMixerRender *vdp_video_mixer_render;
121 static VdpVideoMixerSetFeatureEnables *vdp_video_mixer_set_feature_enables;
122 static VdpVideoMixerSetAttributeValues *vdp_video_mixer_set_attribute_values;
124 static VdpPresentationQueueTargetDestroy *vdp_presentation_queue_target_destroy;
125 static VdpPresentationQueueCreate *vdp_presentation_queue_create;
126 static VdpPresentationQueueDestroy *vdp_presentation_queue_destroy;
127 static VdpPresentationQueueDisplay *vdp_presentation_queue_display;
128 static VdpPresentationQueueBlockUntilSurfaceIdle *vdp_presentation_queue_block_until_surface_idle;
129 static VdpPresentationQueueTargetCreateX11 *vdp_presentation_queue_target_create_x11;
131 /* output_surfaces[2] is used in composite-picture. */
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 void *vdpau_lib_handle;
145 static VdpOutputSurface output_surfaces[NUM_OUTPUT_SURFACES];
146 static int output_surface_width, output_surface_height;
148 static VdpVideoMixer video_mixer;
149 static int deint;
150 static int pullup;
151 static float denoise;
152 static float sharpen;
154 static VdpDecoder decoder;
155 static int decoder_max_refs;
157 static VdpRect src_rect_vid;
158 static VdpRect out_rect_vid;
159 static int border_x, border_y;
161 static struct vdpau_render_state surface_render[MAX_VIDEO_SURFACES];
162 static int surface_num;
163 static int vid_surface_num;
164 static uint32_t vid_width, vid_height;
165 static uint32_t image_format;
166 static const VdpChromaType vdp_chroma_type = VDP_CHROMA_TYPE_420;
168 /* draw_osd */
169 static unsigned char *index_data;
170 static int index_data_size;
171 static uint32_t palette[PALETTE_SIZE];
173 // EOSD
174 // Pool of surfaces
175 struct {
176 VdpBitmapSurface surface;
177 int w;
178 int h;
179 char in_use;
180 } *eosd_surfaces;
182 // List of surfaces to be rendered
183 struct {
184 VdpBitmapSurface surface;
185 VdpRect source;
186 VdpRect dest;
187 VdpColor color;
188 } *eosd_targets;
190 static int eosd_render_count;
191 static int eosd_surface_count;
194 * X11 specific
196 static int visible_buf;
197 static int int_pause;
199 static void video_to_output_surface(void)
201 VdpTime dummy;
202 VdpStatus vdp_st;
203 VdpOutputSurface output_surface = output_surfaces[surface_num];
204 if (vid_surface_num < 0)
205 return;
207 vdp_st = vdp_presentation_queue_block_until_surface_idle(vdp_flip_queue,
208 output_surface,
209 &dummy);
210 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_block_until_surface_idle")
212 // we would need to provide past and future frames to allow deinterlacing,
213 // which is not really possible currently. Deinterlacing is supposed to fall
214 // back to bob deinterlacing, but that seems not to work either.
215 vdp_st = vdp_video_mixer_render(video_mixer, VDP_INVALID_HANDLE, 0,
216 VDP_VIDEO_MIXER_PICTURE_STRUCTURE_FRAME,
217 0, NULL, surface_render[vid_surface_num].surface, 0, NULL, &src_rect_vid,
218 output_surface,
219 NULL, &out_rect_vid, 0, NULL);
220 CHECK_ST_WARNING("Error when calling vdp_video_mixer_render")
223 static void resize(void)
225 VdpStatus vdp_st;
226 int i;
227 struct vo_rect src_rect;
228 struct vo_rect dst_rect;
229 struct vo_rect borders;
230 calc_src_dst_rects(vid_width, vid_height, &src_rect, &dst_rect, &borders, NULL);
231 out_rect_vid.x0 = dst_rect.left;
232 out_rect_vid.x1 = dst_rect.right;
233 out_rect_vid.y0 = dst_rect.top;
234 out_rect_vid.y1 = dst_rect.bottom;
235 src_rect_vid.x0 = src_rect.left;
236 src_rect_vid.x1 = src_rect.right;
237 src_rect_vid.y0 = src_rect.top;
238 src_rect_vid.y1 = src_rect.bottom;
239 border_x = borders.left;
240 border_y = borders.top;
241 #ifdef CONFIG_FREETYPE
242 // adjust font size to display size
243 force_load_font = 1;
244 #endif
245 vo_osd_changed(OSDTYPE_OSD);
247 if (output_surface_width < vo_dwidth || output_surface_height < vo_dheight) {
248 if (output_surface_width < vo_dwidth) {
249 output_surface_width += output_surface_width >> 1;
250 output_surface_width = FFMAX(output_surface_width, vo_dwidth);
252 if (output_surface_height < vo_dheight) {
253 output_surface_height += output_surface_height >> 1;
254 output_surface_height = FFMAX(output_surface_height, vo_dheight);
256 // Creation of output_surfaces
257 for (i = 0; i < NUM_OUTPUT_SURFACES; i++) {
258 if (output_surfaces[i] != VDP_INVALID_HANDLE)
259 vdp_output_surface_destroy(output_surfaces[i]);
260 vdp_st = vdp_output_surface_create(vdp_device, VDP_RGBA_FORMAT_B8G8R8A8,
261 output_surface_width, output_surface_height,
262 &output_surfaces[i]);
263 CHECK_ST_WARNING("Error when calling vdp_output_surface_create")
264 mp_msg(MSGT_VO, MSGL_DBG2, "OUT CREATE: %u\n", output_surfaces[i]);
267 video_to_output_surface();
268 if (visible_buf)
269 flip_page();
272 /* Initialize vdp_get_proc_address, called from preinit() */
273 static int win_x11_init_vdpau_procs(void)
275 VdpStatus vdp_st;
277 struct vdp_function {
278 const int id;
279 void *pointer;
282 const struct vdp_function *dsc;
284 static const struct vdp_function vdp_func[] = {
285 {VDP_FUNC_ID_GET_ERROR_STRING, &vdp_get_error_string},
286 {VDP_FUNC_ID_DEVICE_DESTROY, &vdp_device_destroy},
287 {VDP_FUNC_ID_VIDEO_SURFACE_CREATE, &vdp_video_surface_create},
288 {VDP_FUNC_ID_VIDEO_SURFACE_DESTROY, &vdp_video_surface_destroy},
289 {VDP_FUNC_ID_VIDEO_SURFACE_PUT_BITS_Y_CB_CR,
290 &vdp_video_surface_put_bits_y_cb_cr},
291 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_NATIVE,
292 &vdp_output_surface_put_bits_native},
293 {VDP_FUNC_ID_OUTPUT_SURFACE_CREATE, &vdp_output_surface_create},
294 {VDP_FUNC_ID_OUTPUT_SURFACE_DESTROY, &vdp_output_surface_destroy},
295 {VDP_FUNC_ID_VIDEO_MIXER_CREATE, &vdp_video_mixer_create},
296 {VDP_FUNC_ID_VIDEO_MIXER_DESTROY, &vdp_video_mixer_destroy},
297 {VDP_FUNC_ID_VIDEO_MIXER_RENDER, &vdp_video_mixer_render},
298 {VDP_FUNC_ID_VIDEO_MIXER_SET_FEATURE_ENABLES,
299 &vdp_video_mixer_set_feature_enables},
300 {VDP_FUNC_ID_VIDEO_MIXER_SET_ATTRIBUTE_VALUES,
301 &vdp_video_mixer_set_attribute_values},
302 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_DESTROY,
303 &vdp_presentation_queue_target_destroy},
304 {VDP_FUNC_ID_PRESENTATION_QUEUE_CREATE, &vdp_presentation_queue_create},
305 {VDP_FUNC_ID_PRESENTATION_QUEUE_DESTROY,
306 &vdp_presentation_queue_destroy},
307 {VDP_FUNC_ID_PRESENTATION_QUEUE_DISPLAY,
308 &vdp_presentation_queue_display},
309 {VDP_FUNC_ID_PRESENTATION_QUEUE_BLOCK_UNTIL_SURFACE_IDLE,
310 &vdp_presentation_queue_block_until_surface_idle},
311 {VDP_FUNC_ID_PRESENTATION_QUEUE_TARGET_CREATE_X11,
312 &vdp_presentation_queue_target_create_x11},
313 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_OUTPUT_SURFACE,
314 &vdp_output_surface_render_output_surface},
315 {VDP_FUNC_ID_OUTPUT_SURFACE_PUT_BITS_INDEXED,
316 &vdp_output_surface_put_bits_indexed},
317 {VDP_FUNC_ID_DECODER_CREATE, &vdp_decoder_create},
318 {VDP_FUNC_ID_DECODER_RENDER, &vdp_decoder_render},
319 {VDP_FUNC_ID_DECODER_DESTROY, &vdp_decoder_destroy},
320 {VDP_FUNC_ID_BITMAP_SURFACE_CREATE, &vdp_bitmap_surface_create},
321 {VDP_FUNC_ID_BITMAP_SURFACE_DESTROY, &vdp_bitmap_surface_destroy},
322 {VDP_FUNC_ID_BITMAP_SURFACE_PUT_BITS_NATIVE,
323 &vdp_bitmap_surface_putbits_native},
324 {VDP_FUNC_ID_OUTPUT_SURFACE_RENDER_BITMAP_SURFACE,
325 &vdp_output_surface_render_bitmap_surface},
326 {0, NULL}
329 vdp_st = vdp_device_create(mDisplay, mScreen,
330 &vdp_device, &vdp_get_proc_address);
331 if (vdp_st != VDP_STATUS_OK) {
332 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Error when calling vdp_device_create_x11: %i\n", vdp_st);
333 return -1;
336 for (dsc = vdp_func; dsc->pointer; dsc++) {
337 vdp_st = vdp_get_proc_address(vdp_device, dsc->id, dsc->pointer);
338 if (vdp_st != VDP_STATUS_OK) {
339 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) : "?");
340 return -1;
343 return 0;
346 /* Initialize vdpau_flip_queue, called from config() */
347 static int win_x11_init_vdpau_flip_queue(void)
349 VdpStatus vdp_st;
351 vdp_st = vdp_presentation_queue_target_create_x11(vdp_device, vo_window,
352 &vdp_flip_target);
353 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_target_create_x11")
355 vdp_st = vdp_presentation_queue_create(vdp_device, vdp_flip_target,
356 &vdp_flip_queue);
357 CHECK_ST_ERROR("Error when calling vdp_presentation_queue_create")
359 return 0;
362 static int create_vdp_mixer(VdpChromaType vdp_chroma_type) {
363 #define VDP_NUM_MIXER_PARAMETER 3
364 #define MAX_NUM_FEATURES 5
365 int i;
366 VdpStatus vdp_st;
367 int feature_count = 0;
368 VdpVideoMixerFeature features[MAX_NUM_FEATURES];
369 VdpBool feature_enables[MAX_NUM_FEATURES];
370 static const VdpVideoMixerAttribute denoise_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_NOISE_REDUCTION_LEVEL};
371 const void * const denoise_value[] = {&denoise};
372 static const VdpVideoMixerAttribute sharpen_attrib[] = {VDP_VIDEO_MIXER_ATTRIBUTE_SHARPNESS_LEVEL};
373 const void * const sharpen_value[] = {&sharpen};
374 static const VdpVideoMixerParameter parameters[VDP_NUM_MIXER_PARAMETER] = {
375 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
376 VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
377 VDP_VIDEO_MIXER_PARAMETER_CHROMA_TYPE
379 const void *const parameter_values[VDP_NUM_MIXER_PARAMETER] = {
380 &vid_width,
381 &vid_height,
382 &vdp_chroma_type
384 if (deint == 1)
385 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL;
386 if (deint == 2)
387 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_DEINTERLACE_TEMPORAL_SPATIAL;
388 if (pullup)
389 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_INVERSE_TELECINE;
390 if (denoise)
391 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_NOISE_REDUCTION;
392 if (sharpen)
393 features[feature_count++] = VDP_VIDEO_MIXER_FEATURE_SHARPNESS;
395 vdp_st = vdp_video_mixer_create(vdp_device, feature_count, features,
396 VDP_NUM_MIXER_PARAMETER,
397 parameters, parameter_values,
398 &video_mixer);
399 CHECK_ST_ERROR("Error when calling vdp_video_mixer_create")
401 for (i = 0; i < feature_count; i++) feature_enables[i] = VDP_TRUE;
402 if (feature_count)
403 vdp_video_mixer_set_feature_enables(video_mixer, feature_count, features, feature_enables);
404 if (denoise)
405 vdp_video_mixer_set_attribute_values(video_mixer, 1, denoise_attrib, denoise_value);
406 if (sharpen)
407 vdp_video_mixer_set_attribute_values(video_mixer, 1, sharpen_attrib, sharpen_value);
409 return 0;
412 // Free everything specific to a certain video file
413 static void free_video_specific(void) {
414 int i;
415 VdpStatus vdp_st;
417 if (decoder != VDP_INVALID_HANDLE)
418 vdp_decoder_destroy(decoder);
419 decoder = VDP_INVALID_HANDLE;
420 decoder_max_refs = -1;
422 for (i = 0; i < MAX_VIDEO_SURFACES; i++) {
423 if (surface_render[i].surface != VDP_INVALID_HANDLE) {
424 vdp_st = vdp_video_surface_destroy(surface_render[i].surface);
425 CHECK_ST_WARNING("Error when calling vdp_video_surface_destroy")
427 surface_render[i].surface = VDP_INVALID_HANDLE;
430 if (video_mixer != VDP_INVALID_HANDLE) {
431 vdp_st = vdp_video_mixer_destroy(video_mixer);
432 CHECK_ST_WARNING("Error when calling vdp_video_mixer_destroy")
434 video_mixer = VDP_INVALID_HANDLE;
438 * connect to X server, create and map window, initialize all
439 * VDPAU objects, create different surfaces etc.
441 static int config(uint32_t width, uint32_t height, uint32_t d_width,
442 uint32_t d_height, uint32_t flags, char *title,
443 uint32_t format)
445 XVisualInfo vinfo;
446 XSetWindowAttributes xswa;
447 XWindowAttributes attribs;
448 unsigned long xswamask;
449 int depth;
451 #ifdef CONFIG_XF86VM
452 int vm = flags & VOFLAG_MODESWITCHING;
453 #endif
455 image_format = format;
457 int_pause = 0;
458 visible_buf = 0;
460 #ifdef CONFIG_GUI
461 if (use_gui)
462 guiGetEvent(guiSetShVideo, 0); // the GUI will set up / resize our window
463 else
464 #endif
466 #ifdef CONFIG_XF86VM
467 if (vm)
468 vo_vm_switch();
469 else
470 #endif
471 XGetWindowAttributes(mDisplay, DefaultRootWindow(mDisplay), &attribs);
472 depth = attribs.depth;
473 if (depth != 15 && depth != 16 && depth != 24 && depth != 32)
474 depth = 24;
475 XMatchVisualInfo(mDisplay, mScreen, depth, TrueColor, &vinfo);
477 xswa.background_pixel = 0;
478 xswa.border_pixel = 0;
479 xswamask = CWBackPixel | CWBorderPixel;
481 vo_x11_create_vo_window(&vinfo, vo_dx, vo_dy, d_width, d_height,
482 flags, CopyFromParent, "vdpau", title);
483 XChangeWindowAttributes(mDisplay, vo_window, xswamask, &xswa);
485 #ifdef CONFIG_XF86VM
486 if (vm) {
487 /* Grab the mouse pointer in our window */
488 if (vo_grabpointer)
489 XGrabPointer(mDisplay, vo_window, True, 0,
490 GrabModeAsync, GrabModeAsync,
491 vo_window, None, CurrentTime);
492 XSetInputFocus(mDisplay, vo_window, RevertToNone, CurrentTime);
494 #endif
497 if ((flags & VOFLAG_FULLSCREEN) && WinID <= 0)
498 vo_fs = 1;
500 /* -----VDPAU related code here -------- */
502 free_video_specific();
504 if (vdp_flip_queue == VDP_INVALID_HANDLE && win_x11_init_vdpau_flip_queue())
505 return -1;
507 // video width and height
508 vid_width = width;
509 vid_height = height;
511 if (create_vdp_mixer(vdp_chroma_type))
512 return -1;
514 surface_num = 0;
515 vid_surface_num = -1;
516 resize();
518 return 0;
521 static void check_events(void)
523 int e = vo_x11_check_events(mDisplay);
525 if (e & VO_EVENT_RESIZE)
526 resize();
528 if ((e & VO_EVENT_EXPOSE || e & VO_EVENT_RESIZE) && int_pause) {
529 /* did we already draw a buffer */
530 if (visible_buf) {
531 /* redraw the last visible buffer */
532 VdpStatus vdp_st;
533 vdp_st = vdp_presentation_queue_display(vdp_flip_queue,
534 output_surfaces[surface_num],
535 vo_dwidth, vo_dheight,
537 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
542 static void draw_osd_I8A8(int x0,int y0, int w,int h, unsigned char *src,
543 unsigned char *srca, int stride)
545 VdpOutputSurface output_surface = output_surfaces[surface_num];
546 VdpStatus vdp_st;
547 int i, j;
548 int pitch;
549 int index_data_size_required;
550 VdpRect output_indexed_rect_vid;
551 VdpOutputSurfaceRenderBlendState blend_state;
553 if (!w || !h)
554 return;
556 index_data_size_required = 2*w*h;
557 if (index_data_size < index_data_size_required) {
558 index_data = realloc(index_data, index_data_size_required);
559 index_data_size = index_data_size_required;
562 // index_data creation, component order - I, A, I, A, .....
563 for (i = 0; i < h; i++)
564 for (j = 0; j < w; j++) {
565 index_data[i*2*w + j*2] = src [i*stride+j];
566 index_data[i*2*w + j*2 + 1] = -srca[i*stride+j];
569 output_indexed_rect_vid.x0 = x0;
570 output_indexed_rect_vid.y0 = y0;
571 output_indexed_rect_vid.x1 = x0 + w;
572 output_indexed_rect_vid.y1 = y0 + h;
574 pitch = w*2;
576 // write source_data to output_surfaces[2].
577 vdp_st = vdp_output_surface_put_bits_indexed(output_surfaces[2],
578 VDP_INDEXED_FORMAT_I8A8,
579 (const void *const*)&index_data,
580 &pitch,
581 &output_indexed_rect_vid,
582 VDP_COLOR_TABLE_FORMAT_B8G8R8X8,
583 (void *)palette);
584 CHECK_ST_WARNING("Error when calling vdp_output_surface_put_bits_indexed")
586 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
587 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
588 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
589 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
590 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
591 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
592 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
594 vdp_st = vdp_output_surface_render_output_surface(output_surface,
595 &output_indexed_rect_vid,
596 output_surfaces[2],
597 &output_indexed_rect_vid,
598 NULL,
599 &blend_state,
600 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
601 CHECK_ST_WARNING("Error when calling vdp_output_surface_render_output_surface")
604 static void draw_eosd(void) {
605 VdpStatus vdp_st;
606 VdpOutputSurface output_surface = output_surfaces[surface_num];
607 VdpOutputSurfaceRenderBlendState blend_state;
608 int i;
610 blend_state.struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION;
611 blend_state.blend_factor_source_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA;
612 blend_state.blend_factor_source_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE;
613 blend_state.blend_factor_destination_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
614 blend_state.blend_factor_destination_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA;
615 blend_state.blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
616 blend_state.blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD;
618 for (i=0; i<eosd_render_count; i++) {
619 vdp_st = vdp_output_surface_render_bitmap_surface(
620 output_surface, &eosd_targets[i].dest,
621 eosd_targets[i].surface, &eosd_targets[i].source,
622 &eosd_targets[i].color, &blend_state,
623 VDP_OUTPUT_SURFACE_RENDER_ROTATE_0);
624 CHECK_ST_WARNING("EOSD: Error when rendering")
628 static void generate_eosd(mp_eosd_images_t *imgs) {
629 VdpStatus vdp_st;
630 VdpRect destRect;
631 int j, found;
632 ass_image_t *img = imgs->imgs;
633 ass_image_t *i;
635 // Nothing changed, no need to redraw
636 if (imgs->changed == 0)
637 return;
638 eosd_render_count = 0;
639 // There's nothing to render!
640 if (!img)
641 return;
643 if (imgs->changed == 1)
644 goto eosd_skip_upload;
646 for (j=0; j<eosd_surface_count; j++)
647 eosd_surfaces[j].in_use = 0;
649 for (i = img; i; i = i->next) {
650 // Try to reuse a suitable surface
651 found = -1;
652 for (j=0; j<eosd_surface_count; j++) {
653 if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE && !eosd_surfaces[j].in_use &&
654 eosd_surfaces[j].w >= i->w && eosd_surfaces[j].h >= i->h) {
655 found = j;
656 break;
659 // None found, allocate a new surface
660 if (found < 0) {
661 for (j=0; j<eosd_surface_count; j++) {
662 if (!eosd_surfaces[j].in_use) {
663 if (eosd_surfaces[j].surface != VDP_INVALID_HANDLE)
664 vdp_bitmap_surface_destroy(eosd_surfaces[j].surface);
665 found = j;
666 break;
669 // Allocate new space for surface/target arrays
670 if (found < 0) {
671 j = found = eosd_surface_count;
672 eosd_surface_count = eosd_surface_count ? eosd_surface_count*2 : EOSD_SURFACES_INITIAL;
673 eosd_surfaces = realloc(eosd_surfaces, eosd_surface_count * sizeof(*eosd_surfaces));
674 eosd_targets = realloc(eosd_targets, eosd_surface_count * sizeof(*eosd_targets));
675 for(j=found; j<eosd_surface_count; j++) {
676 eosd_surfaces[j].surface = VDP_INVALID_HANDLE;
677 eosd_surfaces[j].in_use = 0;
680 vdp_st = vdp_bitmap_surface_create(vdp_device, VDP_RGBA_FORMAT_A8,
681 i->w, i->h, VDP_TRUE, &eosd_surfaces[found].surface);
682 CHECK_ST_WARNING("EOSD: error when creating surface")
683 eosd_surfaces[found].w = i->w;
684 eosd_surfaces[found].h = i->h;
686 eosd_surfaces[found].in_use = 1;
687 eosd_targets[eosd_render_count].surface = eosd_surfaces[found].surface;
688 destRect.x0 = 0;
689 destRect.y0 = 0;
690 destRect.x1 = i->w;
691 destRect.y1 = i->h;
692 vdp_st = vdp_bitmap_surface_putbits_native(eosd_targets[eosd_render_count].surface,
693 (const void *) &i->bitmap, &i->stride, &destRect);
694 CHECK_ST_WARNING("EOSD: putbits failed")
695 eosd_render_count++;
698 eosd_skip_upload:
699 eosd_render_count = 0;
700 for (i = img; i; i = i->next) {
701 // Render dest, color, etc.
702 eosd_targets[eosd_render_count].color.alpha = 1.0 - ((i->color >> 0) & 0xff) / 255.0;
703 eosd_targets[eosd_render_count].color.blue = ((i->color >> 8) & 0xff) / 255.0;
704 eosd_targets[eosd_render_count].color.green = ((i->color >> 16) & 0xff) / 255.0;
705 eosd_targets[eosd_render_count].color.red = ((i->color >> 24) & 0xff) / 255.0;
706 eosd_targets[eosd_render_count].dest.x0 = i->dst_x;
707 eosd_targets[eosd_render_count].dest.y0 = i->dst_y;
708 eosd_targets[eosd_render_count].dest.x1 = i->w + i->dst_x;
709 eosd_targets[eosd_render_count].dest.y1 = i->h + i->dst_y;
710 eosd_targets[eosd_render_count].source.x0 = 0;
711 eosd_targets[eosd_render_count].source.y0 = 0;
712 eosd_targets[eosd_render_count].source.x1 = i->w;
713 eosd_targets[eosd_render_count].source.y1 = i->h;
714 eosd_render_count++;
718 static void draw_osd(void)
720 mp_msg(MSGT_VO, MSGL_DBG2, "DRAW_OSD\n");
722 draw_eosd();
723 vo_draw_text_ext(vo_dwidth, vo_dheight, border_x, border_y, border_x, border_y,
724 vid_width, vid_height, draw_osd_I8A8);
727 static void flip_page(void)
729 VdpStatus vdp_st;
730 mp_msg(MSGT_VO, MSGL_DBG2, "\nFLIP_PAGE VID:%u -> OUT:%u\n",
731 surface_render[vid_surface_num].surface, output_surfaces[surface_num]);
733 vdp_st = vdp_presentation_queue_display(vdp_flip_queue, output_surfaces[surface_num],
734 vo_dwidth, vo_dheight,
736 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_display")
738 surface_num = !surface_num;
739 visible_buf = 1;
742 static int draw_slice(uint8_t *image[], int stride[], int w, int h,
743 int x, int y)
745 VdpStatus vdp_st;
746 struct vdpau_render_state *rndr = (struct vdpau_render_state *)image[0];
747 int max_refs = image_format == IMGFMT_VDPAU_H264 ? rndr->info.h264.num_ref_frames : 2;
748 if (!IMGFMT_IS_VDPAU(image_format))
749 return VO_FALSE;
750 if (decoder == VDP_INVALID_HANDLE || decoder_max_refs < max_refs) {
751 VdpDecoderProfile vdp_decoder_profile;
752 if (decoder != VDP_INVALID_HANDLE)
753 vdp_decoder_destroy(decoder);
754 decoder = VDP_INVALID_HANDLE;
755 switch (image_format) {
756 case IMGFMT_VDPAU_MPEG1:
757 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG1;
758 break;
759 case IMGFMT_VDPAU_MPEG2:
760 vdp_decoder_profile = VDP_DECODER_PROFILE_MPEG2_MAIN;
761 break;
762 case IMGFMT_VDPAU_H264:
763 vdp_decoder_profile = VDP_DECODER_PROFILE_H264_HIGH;
764 break;
765 case IMGFMT_VDPAU_WMV3:
766 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_MAIN;
767 break;
768 case IMGFMT_VDPAU_VC1:
769 vdp_decoder_profile = VDP_DECODER_PROFILE_VC1_ADVANCED;
770 break;
772 vdp_st = vdp_decoder_create(vdp_device, vdp_decoder_profile, vid_width, vid_height, max_refs, &decoder);
773 CHECK_ST_WARNING("Failed creating VDPAU decoder");
774 decoder_max_refs = max_refs;
776 vdp_st = vdp_decoder_render(decoder, rndr->surface, (void *)&rndr->info, rndr->bitstream_buffers_used, rndr->bitstream_buffers);
777 CHECK_ST_WARNING("Failed VDPAU decoder rendering");
778 return VO_TRUE;
782 static int draw_frame(uint8_t *src[])
784 return VO_ERROR;
787 static struct vdpau_render_state *get_surface(int number)
789 if (number > MAX_VIDEO_SURFACES)
790 return NULL;
791 if (surface_render[number].surface == VDP_INVALID_HANDLE) {
792 VdpStatus vdp_st;
793 vdp_st = vdp_video_surface_create(vdp_device, vdp_chroma_type,
794 vid_width, vid_height,
795 &surface_render[number].surface);
796 CHECK_ST_WARNING("Error when calling vdp_video_surface_create")
797 if (vdp_st != VDP_STATUS_OK)
798 return NULL;
800 mp_msg(MSGT_VO, MSGL_DBG2, "VID CREATE: %u\n", surface_render[number].surface);
801 return &surface_render[number];
804 static uint32_t draw_image(mp_image_t *mpi)
806 if (IMGFMT_IS_VDPAU(image_format)) {
807 struct vdpau_render_state *rndr = mpi->priv;
808 vid_surface_num = rndr - surface_render;
809 } else if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) {
810 VdpStatus vdp_st;
811 void *destdata[3] = {mpi->planes[0], mpi->planes[2], mpi->planes[1]};
812 struct vdpau_render_state *rndr = get_surface(0);
813 vid_surface_num = rndr - surface_render;
814 vdp_st = vdp_video_surface_put_bits_y_cb_cr(rndr->surface,
815 VDP_YCBCR_FORMAT_YV12,
816 (const void *const*)destdata,
817 mpi->stride); // pitch
818 CHECK_ST_ERROR("Error when calling vdp_video_surface_put_bits_y_cb_cr")
821 video_to_output_surface();
822 return VO_TRUE;
825 static uint32_t get_image(mp_image_t *mpi)
827 struct vdpau_render_state *rndr;
829 // no dr for non-decoding for now
830 if (!IMGFMT_IS_VDPAU(image_format)) return VO_FALSE;
831 if (mpi->type != MP_IMGTYPE_NUMBERED) return VO_FALSE;
833 rndr = get_surface(mpi->number);
834 if (!rndr) {
835 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] no surfaces available in get_image\n");
836 // TODO: this probably breaks things forever, provide a dummy buffer?
837 return VO_FALSE;
839 mpi->flags |= MP_IMGFLAG_DIRECT;
840 mpi->stride[0] = mpi->stride[1] = mpi->stride[2] = 0;
841 mpi->planes[0] = mpi->planes[1] = mpi->planes[2] = NULL;
842 // hack to get around a check and to avoid a special-case in vd_ffmpeg.c
843 mpi->planes[0] = (void *)rndr;
844 mpi->num_planes = 1;
845 mpi->priv = rndr;
846 return VO_TRUE;
849 static int query_format(uint32_t format)
851 int default_flags = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP | VFCAP_HWSCALE_DOWN | VFCAP_OSD | VFCAP_EOSD | VFCAP_EOSD_UNSCALED;
852 switch (format) {
853 case IMGFMT_YV12:
854 return default_flags | VOCAP_NOSLICES;
855 case IMGFMT_VDPAU_MPEG1:
856 case IMGFMT_VDPAU_MPEG2:
857 case IMGFMT_VDPAU_H264:
858 case IMGFMT_VDPAU_WMV3:
859 case IMGFMT_VDPAU_VC1:
860 return default_flags;
862 return 0;
865 static void DestroyVdpauObjects(void)
867 int i;
868 VdpStatus vdp_st;
870 free_video_specific();
872 vdp_st = vdp_presentation_queue_destroy(vdp_flip_queue);
873 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_destroy")
875 vdp_st = vdp_presentation_queue_target_destroy(vdp_flip_target);
876 CHECK_ST_WARNING("Error when calling vdp_presentation_queue_target_destroy")
878 for (i = 0; i < NUM_OUTPUT_SURFACES; i++) {
879 vdp_st = vdp_output_surface_destroy(output_surfaces[i]);
880 output_surfaces[i] = VDP_INVALID_HANDLE;
881 CHECK_ST_WARNING("Error when calling vdp_output_surface_destroy")
884 for (i = 0; i<eosd_surface_count; i++) {
885 if (eosd_surfaces[i].surface != VDP_INVALID_HANDLE) {
886 vdp_st = vdp_bitmap_surface_destroy(eosd_surfaces[i].surface);
887 CHECK_ST_WARNING("Error when calling vdp_bitmap_surface_destroy")
889 eosd_surfaces[i].surface = VDP_INVALID_HANDLE;
892 vdp_st = vdp_device_destroy(vdp_device);
893 CHECK_ST_WARNING("Error when calling vdp_device_destroy")
896 static void uninit(void)
898 if (!vo_config_count)
899 return;
900 visible_buf = 0;
902 /* Destroy all vdpau objects */
903 DestroyVdpauObjects();
905 free(index_data);
906 index_data = NULL;
908 free(eosd_surfaces);
909 eosd_surfaces = NULL;
910 free(eosd_targets);
911 eosd_targets = NULL;
913 #ifdef CONFIG_XF86VM
914 vo_vm_close();
915 #endif
916 vo_x11_uninit();
918 dlclose(vdpau_lib_handle);
921 static opt_t subopts[] = {
922 {"deint", OPT_ARG_INT, &deint, (opt_test_f)int_non_neg},
923 {"pullup", OPT_ARG_BOOL, &pullup, NULL},
924 {"denoise", OPT_ARG_FLOAT, &denoise, NULL},
925 {"sharpen", OPT_ARG_FLOAT, &sharpen, NULL},
926 {NULL}
929 static const char help_msg[] =
930 "\n-vo vdpau command line help:\n"
931 "Example: mplayer -vo vdpau:deint=2\n"
932 "\nOptions:\n"
933 " deint\n"
934 " 0: no deinterlacing\n"
935 " 1: temporal deinterlacing (not yet working)\n"
936 " 2: temporal-spatial deinterlacing (not yet working)\n"
937 " pullup\n"
938 " Try to apply inverse-telecine (needs deinterlacing, not working)\n"
939 " denoise\n"
940 " Apply denoising, argument is strength from 0.0 to 1.0\n"
941 " sharpen\n"
942 " Apply sharpening or softening, argument is strength from -1.0 to 1.0\n"
945 static int preinit(const char *arg)
947 int i;
948 static const char *vdpaulibrary = "libvdpau.so.1";
949 static const char *vdpau_device_create = "vdp_device_create_x11";
951 deint = 0;
952 pullup = 0;
953 denoise = 0;
954 sharpen = 0;
955 if (subopt_parse(arg, subopts) != 0) {
956 mp_msg(MSGT_VO, MSGL_FATAL, help_msg);
957 return -1;
960 vdpau_lib_handle = dlopen(vdpaulibrary, RTLD_LAZY);
961 if (!vdpau_lib_handle) {
962 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not open dynamic library %s\n",
963 vdpaulibrary);
964 return -1;
966 vdp_device_create = dlsym(vdpau_lib_handle, vdpau_device_create);
967 if (!vdp_device_create) {
968 mp_msg(MSGT_VO, MSGL_ERR, "[vdpau] Could not find function %s in %s\n",
969 vdpau_device_create, vdpaulibrary);
970 return -1;
972 if (!vo_init() || win_x11_init_vdpau_procs())
973 return -1;
975 decoder = VDP_INVALID_HANDLE;
976 for (i = 0; i < MAX_VIDEO_SURFACES; i++)
977 surface_render[i].surface = VDP_INVALID_HANDLE;
978 video_mixer = VDP_INVALID_HANDLE;
979 for (i = 0; i < NUM_OUTPUT_SURFACES; i++)
980 output_surfaces[i] = VDP_INVALID_HANDLE;
981 vdp_flip_queue = VDP_INVALID_HANDLE;
982 output_surface_width = output_surface_height = -1;
984 // full grayscale palette.
985 for (i = 0; i < PALETTE_SIZE; ++i)
986 palette[i] = (i << 16) | (i << 8) | i;
987 index_data = NULL;
988 index_data_size = 0;
990 eosd_surface_count = eosd_render_count = 0;
991 eosd_surfaces = NULL;
992 eosd_targets = NULL;
994 return 0;
997 static int control(uint32_t request, void *data, ...)
999 switch (request) {
1000 case VOCTRL_PAUSE:
1001 return (int_pause = 1);
1002 case VOCTRL_RESUME:
1003 return (int_pause = 0);
1004 case VOCTRL_QUERY_FORMAT:
1005 return query_format(*(uint32_t *)data);
1006 case VOCTRL_GET_IMAGE:
1007 return get_image(data);
1008 case VOCTRL_DRAW_IMAGE:
1009 return draw_image(data);
1010 case VOCTRL_GUISUPPORT:
1011 return VO_TRUE;
1012 case VOCTRL_BORDER:
1013 vo_x11_border();
1014 resize();
1015 return VO_TRUE;
1016 case VOCTRL_FULLSCREEN:
1017 vo_x11_fullscreen();
1018 resize();
1019 return VO_TRUE;
1020 case VOCTRL_GET_PANSCAN:
1021 return VO_TRUE;
1022 case VOCTRL_SET_PANSCAN:
1023 resize();
1024 return VO_TRUE;
1025 case VOCTRL_SET_EQUALIZER: {
1026 va_list ap;
1027 int value;
1029 va_start(ap, data);
1030 value = va_arg(ap, int);
1032 va_end(ap);
1033 return vo_x11_set_equalizer(data, value);
1035 case VOCTRL_GET_EQUALIZER: {
1036 va_list ap;
1037 int *value;
1039 va_start(ap, data);
1040 value = va_arg(ap, int *);
1042 va_end(ap);
1043 return vo_x11_get_equalizer(data, value);
1045 case VOCTRL_ONTOP:
1046 vo_x11_ontop();
1047 return VO_TRUE;
1048 case VOCTRL_UPDATE_SCREENINFO:
1049 update_xinerama_info();
1050 return VO_TRUE;
1051 case VOCTRL_DRAW_EOSD:
1052 if (!data)
1053 return VO_FALSE;
1054 generate_eosd(data);
1055 return VO_TRUE;
1056 case VOCTRL_GET_EOSD_RES: {
1057 mp_eosd_res_t *r = data;
1058 r->mt = r->mb = r->ml = r->mr = 0;
1059 if (vo_fs) {
1060 r->w = vo_screenwidth;
1061 r->h = vo_screenheight;
1062 r->ml = r->mr = border_x;
1063 r->mt = r->mb = border_y;
1064 } else
1065 r->w = vo_dwidth; r->h = vo_dheight;
1066 return VO_TRUE;
1069 return VO_NOTIMPL;
1072 /* @} */