3 * @brief VDPAU video display module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009-2013 Rémi Denis-Courmont
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout_display.h>
35 #include <vlc_picture_pool.h>
38 #include "vlc_vdpau.h"
41 static int Open(vlc_object_t *);
42 static void Close(vlc_object_t *);
45 set_shortname(N_("VDPAU"))
46 set_description(N_("VDPAU output"))
47 set_category(CAT_VIDEO)
48 set_subcategory(SUBCAT_VIDEO_VOUT)
49 set_capability("vout display", 300)
50 set_callbacks(Open, Close)
52 add_shortcut("vdpau", "xid")
55 #define MAX_PICTURES (32)
57 struct vout_display_sys_t
59 xcb_connection_t *conn; /**< XCB connection */
60 vout_window_t *embed; /**< parent window */
61 vdp_t *vdp; /**< VDPAU back-end */
62 picture_t *current; /**< Currently visible picture */
64 xcb_window_t window; /**< target window (owned by VDPAU back-end) */
65 xcb_cursor_t cursor; /**< blank cursor */
66 VdpDevice device; /**< VDPAU device handle */
67 VdpPresentationQueueTarget target; /**< VDPAU presentation queue target */
68 VdpPresentationQueue queue; /**< VDPAU presentation queue */
69 VdpRGBAFormat rgb_fmt; /**< Output surface format */
71 picture_pool_t *pool; /**< pictures pool */
72 picture_sys_t *pics[MAX_PICTURES];
75 static picture_pool_t *PoolAlloc(vout_display_t *vd, unsigned requested_count)
77 vout_display_sys_t *sys = vd->sys;
78 const video_format_t *fmt = &vd->fmt;
79 picture_t *pics[MAX_PICTURES];
80 picture_resource_t res = { .p_sys = NULL };
82 if (requested_count > MAX_PICTURES)
83 requested_count = MAX_PICTURES;
86 while (count < requested_count)
88 picture_sys_t *psys = malloc(sizeof (*psys));
89 if (unlikely(psys == NULL))
92 VdpStatus err = vdp_output_surface_create(sys->vdp, sys->device,
93 sys->rgb_fmt, fmt->i_visible_width, fmt->i_visible_height,
95 if (err != VDP_STATUS_OK)
98 msg_Err(vd, "%s creation failure: %s", "output surface",
99 vdp_get_error_string(sys->vdp, err));
102 psys->device = sys->device;
103 psys->vdp = sys->vdp;
106 pics[count] = picture_NewFromResource(&vd->fmt, &res);
107 if (pics[count] == NULL)
112 sys->pics[count++] = res.p_sys;
115 return count ? picture_pool_New(count, pics) : NULL;
118 static void PoolFree(vout_display_t *vd, picture_pool_t *pool)
120 vout_display_sys_t *sys = vd->sys;
122 for (unsigned count = 0; count < MAX_PICTURES; count++)
124 picture_sys_t *psys = sys->pics[count];
128 VdpStatus err = vdp_output_surface_destroy(sys->vdp, psys->surface);
129 if (err != VDP_STATUS_OK)
130 msg_Err(vd, "%s destruction failure: %s", "output surface",
131 vdp_get_error_string(sys->vdp, err));
133 if (sys->current != NULL)
134 picture_Release(sys->current);
135 picture_pool_Delete(pool);
138 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
140 vout_display_sys_t *sys = vd->sys;
142 if (sys->pool == NULL)
143 sys->pool = PoolAlloc(vd, requested_count);
147 static void RenderRegion(vout_display_t *vd, VdpOutputSurface target,
148 const subpicture_region_t *reg, int alpha)
150 vout_display_sys_t *sys = vd->sys;
151 VdpBitmapSurface surface;
152 VdpRGBAFormat fmt = VDP_RGBA_FORMAT_R8G8B8A8; /* TODO? YUVA */
155 /* Create GPU surface for sub-picture */
156 err = vdp_bitmap_surface_create(sys->vdp, sys->device, fmt,
157 reg->fmt.i_visible_width, reg->fmt.i_visible_height, VDP_FALSE,
159 if (err != VDP_STATUS_OK)
161 msg_Err(vd, "%s creation failure: %s", "bitmap surface",
162 vdp_get_error_string(sys->vdp, err));
166 /* Upload sub-picture to GPU surface */
167 picture_t *subpic = reg->p_picture;
168 const void *data = subpic->p[0].p_pixels;
169 uint32_t pitch = subpic->p[0].i_pitch;
171 err = vdp_bitmap_surface_put_bits_native(sys->vdp, surface, &data, &pitch,
173 if (err != VDP_STATUS_OK)
175 msg_Err(vd, "subpicture upload failure: %s",
176 vdp_get_error_string(sys->vdp, err));
180 /* Render onto main surface */
184 reg->i_x + reg->fmt.i_visible_width,
185 reg->i_y + reg->fmt.i_visible_height,
187 VdpColor color = { 1.f, 1.f, 1.f, reg->i_alpha * alpha / 65535.f };
188 VdpOutputSurfaceRenderBlendState state = {
189 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
190 .blend_factor_source_color =
191 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
192 .blend_factor_destination_color =
193 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
194 .blend_factor_source_alpha =
195 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ZERO,
196 .blend_factor_destination_alpha =
197 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
198 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
199 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
200 .blend_constant = { 0.f, 0.f, 0.f, 0.f },
203 err = vdp_output_surface_render_bitmap_surface(sys->vdp, target, &area,
204 surface, NULL, &color, &state, 0);
205 if (err != VDP_STATUS_OK)
206 msg_Err(vd, "blending failure: %s",
207 vdp_get_error_string(sys->vdp, err));
209 out:/* Destroy GPU surface */
210 vdp_bitmap_surface_destroy(sys->vdp, surface);
213 static void Queue(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
215 vout_display_sys_t *sys = vd->sys;
216 VdpOutputSurface surface = pic->p_sys->surface;
219 VdpPresentationQueueStatus status;
221 err = vdp_presentation_queue_query_surface_status(sys->vdp, sys->queue,
222 surface, &status, &ts);
223 if (err == VDP_STATUS_OK && status != VDP_PRESENTATION_QUEUE_STATUS_IDLE)
224 msg_Dbg(vd, "surface status: %u", status);
227 for (subpicture_region_t *r = subpic->p_region; r != NULL;
229 RenderRegion(vd, surface, r, subpic->i_alpha);
231 /* Compute picture presentation time */
232 mtime_t now = mdate();
235 err = vdp_presentation_queue_get_time(sys->vdp, sys->queue, &pts);
236 if (err != VDP_STATUS_OK)
238 msg_Err(vd, "presentation queue time failure: %s",
239 vdp_get_error_string(sys->vdp, err));
243 mtime_t delay = pic->date - now;
245 delay = 0; /* core bug: date is not updated during pause */
246 if (unlikely(delay > CLOCK_FREQ))
247 { /* We would get stuck if the delay was too long. */
248 msg_Dbg(vd, "picture date corrupt: delay of %"PRId64" us", delay);
249 delay = CLOCK_FREQ / 50;
254 err = vdp_presentation_queue_display(sys->vdp, sys->queue, surface, 0, 0,
256 if (err != VDP_STATUS_OK)
257 msg_Err(vd, "presentation queue display failure: %s",
258 vdp_get_error_string(sys->vdp, err));
261 static void Wait(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
263 vout_display_sys_t *sys = vd->sys;
265 picture_t *current = sys->current;
268 picture_sys_t *psys = current->p_sys;
272 err = vdp_presentation_queue_block_until_surface_idle(sys->vdp,
273 sys->queue, psys->surface, &pts);
274 if (err != VDP_STATUS_OK)
276 msg_Err(vd, "presentation queue blocking error: %s",
277 vdp_get_error_string(sys->vdp, err));
278 picture_Release(pic);
281 picture_Release(current);
288 static int Control(vout_display_t *vd, int query, va_list ap)
290 vout_display_sys_t *sys = vd->sys;
294 case VOUT_DISPLAY_HIDE_MOUSE:
295 xcb_change_window_attributes(sys->conn, sys->embed->handle.xid,
296 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
298 case VOUT_DISPLAY_RESET_PICTURES:
300 msg_Dbg(vd, "resetting pictures");
301 if (sys->pool != NULL)
303 PoolFree(vd, sys->pool);
307 const video_format_t *src= &vd->source;
308 video_format_t *fmt = &vd->fmt;
309 vout_display_place_t place;
311 vout_display_PlacePicture(&place, src, vd->cfg, false);
313 fmt->i_width = src->i_width * place.width / src->i_visible_width;
314 fmt->i_height = src->i_height * place.height / src->i_visible_height;
315 fmt->i_visible_width = place.width;
316 fmt->i_visible_height = place.height;
317 fmt->i_x_offset = src->i_x_offset * place.width / src->i_visible_width;
318 fmt->i_y_offset = src->i_y_offset * place.height / src->i_visible_height;
320 const uint32_t values[] = { place.x, place.y,
321 place.width, place.height, };
322 xcb_configure_window(sys->conn, sys->window,
323 XCB_CONFIG_WINDOW_X|XCB_CONFIG_WINDOW_Y|
324 XCB_CONFIG_WINDOW_WIDTH|XCB_CONFIG_WINDOW_HEIGHT,
328 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
330 const vout_display_cfg_t *c = va_arg(ap, const vout_display_cfg_t *);
331 return vout_window_SetFullScreen(sys->embed, c->is_fullscreen);
333 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
335 unsigned state = va_arg(ap, unsigned);
336 return vout_window_SetState(sys->embed, state);
338 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
340 const vout_display_cfg_t *cfg = va_arg(ap, const vout_display_cfg_t *);
341 bool forced = va_arg(ap, int);
344 vout_window_SetSize(sys->embed,
345 cfg->display.width, cfg->display.height);
346 return VLC_EGENERIC; /* Always fail. See x11.c for rationale. */
349 vout_display_place_t place;
350 vout_display_PlacePicture(&place, &vd->source, cfg, false);
351 if (place.width != vd->fmt.i_visible_width
352 || place.height != vd->fmt.i_visible_height)
354 vout_display_SendEventPicturesInvalid(vd);
358 const uint32_t values[] = { place.x, place.y,
359 place.width, place.height, };
360 xcb_configure_window(sys->conn, sys->window,
361 XCB_CONFIG_WINDOW_X|XCB_CONFIG_WINDOW_Y|
362 XCB_CONFIG_WINDOW_WIDTH|XCB_CONFIG_WINDOW_HEIGHT,
366 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
367 case VOUT_DISPLAY_CHANGE_ZOOM:
368 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
369 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
370 vout_display_SendEventPicturesInvalid (vd);
373 msg_Err(vd, "unknown control request %d", query);
376 xcb_flush (sys->conn);
380 static void Manage(vout_display_t *vd)
382 vout_display_sys_t *sys = vd->sys;
385 XCB_Manage(vd, sys->conn, &visible);
388 static int xcb_screen_num(xcb_connection_t *conn, const xcb_screen_t *screen)
390 const xcb_setup_t *setup = xcb_get_setup(conn);
393 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator(setup);
394 i.rem > 0; xcb_screen_next(&i))
396 if (i.data->root == screen->root)
403 static int Open(vlc_object_t *obj)
405 if (!vlc_xlib_init(obj))
408 vout_display_t *vd = (vout_display_t *)obj;
409 vout_display_sys_t *sys = malloc(sizeof (*sys));
410 if (unlikely(sys == NULL))
413 const xcb_screen_t *screen;
414 uint16_t width, height;
415 sys->embed = XCB_parent_Create(vd, &sys->conn, &screen, &width, &height);
416 if (sys->embed == NULL)
422 /* Load the VDPAU back-end and create a device instance */
423 VdpStatus err = vdp_get_x11(sys->embed->display.x11,
424 xcb_screen_num(sys->conn, screen),
425 &sys->vdp, &sys->device);
426 if (err != VDP_STATUS_OK)
428 msg_Dbg(obj, "device creation failure: error %d", (int)err);
429 xcb_disconnect(sys->conn);
430 vout_display_DeleteWindow(vd, sys->embed);
436 if (vdp_get_information_string(sys->vdp, &info) == VDP_STATUS_OK)
437 msg_Dbg(vd, "using back-end %s", info);
439 /* Check source format */
440 VdpChromaType chroma;
441 VdpYCbCrFormat format;
442 if (vd->fmt.i_chroma == VLC_CODEC_VDPAU_VIDEO_420
443 || vd->fmt.i_chroma == VLC_CODEC_VDPAU_VIDEO_422)
446 if (vlc_fourcc_to_vdp_ycc(vd->fmt.i_chroma, &chroma, &format))
451 err = vdp_video_surface_query_capabilities(sys->vdp, sys->device,
452 chroma, &ok, &w, &h);
453 if (err != VDP_STATUS_OK)
455 msg_Err(vd, "%s capabilities query failure: %s", "video surface",
456 vdp_get_error_string(sys->vdp, err));
459 if (!ok || w < vd->fmt.i_width || h < vd->fmt.i_height)
461 msg_Err(vd, "source video %s not supported", "chroma type");
465 err = vdp_video_surface_query_get_put_bits_y_cb_cr_capabilities(
466 sys->vdp, sys->device, chroma, format, &ok);
467 if (err != VDP_STATUS_OK)
469 msg_Err(vd, "%s capabilities query failure: %s", "video surface",
470 vdp_get_error_string(sys->vdp, err));
475 msg_Err(vd, "source video %s not supported", "YCbCr format");
482 /* Check video mixer capabilities */
486 err = vdp_video_mixer_query_parameter_value_range(sys->vdp,
487 sys->device, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
489 if (err != VDP_STATUS_OK)
491 msg_Err(vd, "%s capabilities query failure: %s",
492 "video mixer surface width",
493 vdp_get_error_string(sys->vdp, err));
496 if (min > vd->fmt.i_width || vd->fmt.i_width > max)
498 msg_Err(vd, "source video %s not supported", "width");
502 err = vdp_video_mixer_query_parameter_value_range(sys->vdp,
503 sys->device, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
505 if (err != VDP_STATUS_OK)
507 msg_Err(vd, "%s capabilities query failure: %s",
508 "video mixer surface width",
509 vdp_get_error_string(sys->vdp, err));
512 if (min > vd->fmt.i_height || vd->fmt.i_height > max)
514 msg_Err(vd, "source video %s not supported", "height");
519 /* Select surface format */
520 static const VdpRGBAFormat rgb_fmts[] = {
521 VDP_RGBA_FORMAT_R10G10B10A2, VDP_RGBA_FORMAT_B10G10R10A2,
522 VDP_RGBA_FORMAT_B8G8R8A8, VDP_RGBA_FORMAT_R8G8B8A8,
526 for (i = 0; i < sizeof (rgb_fmts) / sizeof (rgb_fmts[0]); i++)
531 err = vdp_output_surface_query_capabilities(sys->vdp, sys->device,
532 rgb_fmts[i], &ok, &w, &h);
533 if (err != VDP_STATUS_OK)
535 msg_Err(vd, "%s capabilities query failure: %s", "output surface",
536 vdp_get_error_string(sys->vdp, err));
539 /* NOTE: Wrong! No warranties that zoom <= 100%! */
540 if (!ok || w < vd->fmt.i_width || h < vd->fmt.i_height)
543 sys->rgb_fmt = rgb_fmts[i];
544 msg_Dbg(vd, "using RGBA format %u", sys->rgb_fmt);
547 if (i == sizeof (rgb_fmts) / sizeof (rgb_fmts[0]))
549 msg_Err(vd, "no supported output surface format");
553 /* VDPAU-X11 requires a window dedicated to the back-end */
555 xcb_pixmap_t pix = xcb_generate_id(sys->conn);
556 xcb_create_pixmap(sys->conn, screen->root_depth, pix,
560 XCB_CW_BACK_PIXMAP | XCB_CW_BACK_PIXEL |
561 XCB_CW_BORDER_PIXMAP | XCB_CW_BORDER_PIXEL |
562 XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
563 const uint32_t values[] = {
564 pix, screen->black_pixel, pix, screen->black_pixel,
565 XCB_EVENT_MASK_VISIBILITY_CHANGE, screen->default_colormap
567 vout_display_place_t place;
569 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
570 sys->window = xcb_generate_id(sys->conn);
572 xcb_void_cookie_t c =
573 xcb_create_window_checked(sys->conn, screen->root_depth,
574 sys->window, sys->embed->handle.xid, place.x, place.y,
575 place.width, place.height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
576 screen->root_visual, mask, values);
577 if (XCB_error_Check(vd, sys->conn, "window creation failure", c))
579 msg_Dbg(vd, "using X11 window 0x%08"PRIx32, sys->window);
580 xcb_map_window(sys->conn, sys->window);
583 /* Check bitmap capabilities (for SPU) */
584 const vlc_fourcc_t *spu_chromas = NULL;
586 static const vlc_fourcc_t subpicture_chromas[] = { VLC_CODEC_RGBA, 0 };
590 err = vdp_bitmap_surface_query_capabilities(sys->vdp, sys->device,
591 VDP_RGBA_FORMAT_R8G8B8A8, &ok, &w, &h);
592 if (err != VDP_STATUS_OK)
594 msg_Err(vd, "%s capabilities query failure: %s", "output surface",
595 vdp_get_error_string(sys->vdp, err));
599 spu_chromas = subpicture_chromas;
602 /* Initialize VDPAU queue */
603 err = vdp_presentation_queue_target_create_x11(sys->vdp, sys->device,
604 sys->window, &sys->target);
605 if (err != VDP_STATUS_OK)
607 msg_Err(vd, "%s creation failure: %s", "presentation queue target",
608 vdp_get_error_string(sys->vdp, err));
612 err = vdp_presentation_queue_create(sys->vdp, sys->device, sys->target,
614 if (err != VDP_STATUS_OK)
616 msg_Err(vd, "%s creation failure: %s", "presentation queue",
617 vdp_get_error_string(sys->vdp, err));
618 vdp_presentation_queue_target_destroy(sys->vdp, sys->target);
622 VdpColor black = { 0.f, 0.f, 0.f, 1.f };
623 vdp_presentation_queue_set_background_color(sys->vdp, sys->queue, &black);
625 sys->cursor = XCB_cursor_Create(sys->conn, screen);
627 for (unsigned count = 0; count < MAX_PICTURES; count++)
628 sys->pics[count] = NULL;
632 vd->info.has_pictures_invalid = true;
633 vd->info.has_event_thread = true;
634 vd->info.subpicture_chromas = spu_chromas;
635 vd->fmt.i_chroma = VLC_CODEC_VDPAU_OUTPUT;
640 vd->control = Control;
644 bool is_fullscreen = vd->cfg->is_fullscreen;
645 if (is_fullscreen && vout_window_SetFullScreen(sys->embed, true))
646 is_fullscreen = false;
647 vout_display_SendEventFullscreen(vd, is_fullscreen);
648 vout_display_SendEventDisplaySize(vd, width, height, is_fullscreen);
653 vdp_release_x11(sys->vdp);
654 xcb_disconnect(sys->conn);
655 vout_display_DeleteWindow(vd, sys->embed);
660 static void Close(vlc_object_t *obj)
662 vout_display_t *vd = (vout_display_t *)obj;
663 vout_display_sys_t *sys = vd->sys;
665 /* Restore cursor explicitly (parent window connection will survive) */
666 xcb_change_window_attributes(sys->conn, sys->embed->handle.xid,
667 XCB_CW_CURSOR, &(uint32_t) { XCB_CURSOR_NONE });
668 xcb_flush(sys->conn);
670 vdp_presentation_queue_destroy(sys->vdp, sys->queue);
671 vdp_presentation_queue_target_destroy(sys->vdp, sys->target);
673 if (sys->pool != NULL)
674 PoolFree(vd, sys->pool);
676 vdp_release_x11(sys->vdp);
677 xcb_disconnect(sys->conn);
678 vout_display_DeleteWindow(vd, sys->embed);