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 struct vout_display_sys_t
57 xcb_connection_t *conn; /**< XCB connection */
58 vout_window_t *embed; /**< parent window */
59 vdp_t *vdp; /**< VDPAU back-end */
60 picture_t *current; /**< Currently visible picture */
62 xcb_window_t window; /**< target window (owned by VDPAU back-end) */
63 xcb_cursor_t cursor; /**< blank cursor */
64 VdpDevice device; /**< VDPAU device handle */
65 VdpPresentationQueueTarget target; /**< VDPAU presentation queue target */
66 VdpPresentationQueue queue; /**< VDPAU presentation queue */
67 VdpRGBAFormat rgb_fmt; /**< Output surface format */
69 picture_pool_t *pool; /**< pictures pool */
72 static void pictureSys_DestroyVDPAU(picture_sys_t *psys)
74 vdp_output_surface_destroy(psys->vdp, psys->surface);
75 vdp_release_x11(psys->vdp);
79 static void PictureDestroyVDPAU(picture_t *pic)
81 pictureSys_DestroyVDPAU(pic->p_sys);
85 static VdpStatus picture_NewVDPAU(vdp_t *vdp, VdpRGBAFormat rgb_fmt,
86 const video_format_t *restrict fmt,
87 picture_t **restrict picp)
89 picture_sys_t *psys = malloc(sizeof (*psys));
90 if (unlikely(psys == NULL))
91 return VDP_STATUS_RESOURCES;
93 psys->vdp = vdp_hold_x11(vdp, &psys->device);
95 VdpStatus err = vdp_output_surface_create(psys->vdp, psys->device,
96 rgb_fmt, fmt->i_visible_width, fmt->i_visible_height,
98 if (err != VDP_STATUS_OK)
100 vdp_release_x11(psys->vdp);
105 picture_resource_t res = {
107 .pf_destroy = PictureDestroyVDPAU,
110 picture_t *pic = picture_NewFromResource(fmt, &res);
111 if (unlikely(pic == NULL))
113 pictureSys_DestroyVDPAU(psys);
114 return VDP_STATUS_RESOURCES;
117 return VDP_STATUS_OK;
120 static picture_pool_t *PoolAlloc(vout_display_t *vd, unsigned requested_count)
122 vout_display_sys_t *sys = vd->sys;
123 picture_t *pics[requested_count];
126 while (count < requested_count)
128 VdpStatus err = picture_NewVDPAU(sys->vdp, sys->rgb_fmt, &vd->fmt,
130 if (err != VDP_STATUS_OK)
132 msg_Err(vd, "%s creation failure: %s", "output surface",
133 vdp_get_error_string(sys->vdp, err));
139 return count ? picture_pool_New(count, pics) : NULL;
142 static void PoolFree(vout_display_t *vd, picture_pool_t *pool)
144 vout_display_sys_t *sys = vd->sys;
146 if (sys->current != NULL)
147 picture_Release(sys->current);
148 picture_pool_Delete(pool);
151 static picture_pool_t *Pool(vout_display_t *vd, unsigned requested_count)
153 vout_display_sys_t *sys = vd->sys;
155 if (sys->pool == NULL)
156 sys->pool = PoolAlloc(vd, requested_count);
160 static void RenderRegion(vout_display_t *vd, VdpOutputSurface target,
161 const subpicture_region_t *reg, int alpha)
163 vout_display_sys_t *sys = vd->sys;
164 VdpBitmapSurface surface;
165 VdpRGBAFormat fmt = VDP_RGBA_FORMAT_R8G8B8A8; /* TODO? YUVA */
168 /* Create GPU surface for sub-picture */
169 err = vdp_bitmap_surface_create(sys->vdp, sys->device, fmt,
170 reg->fmt.i_visible_width, reg->fmt.i_visible_height, VDP_FALSE,
172 if (err != VDP_STATUS_OK)
174 msg_Err(vd, "%s creation failure: %s", "bitmap surface",
175 vdp_get_error_string(sys->vdp, err));
179 /* Upload sub-picture to GPU surface */
180 picture_t *subpic = reg->p_picture;
181 const void *data = subpic->p[0].p_pixels;
182 uint32_t pitch = subpic->p[0].i_pitch;
184 err = vdp_bitmap_surface_put_bits_native(sys->vdp, surface, &data, &pitch,
186 if (err != VDP_STATUS_OK)
188 msg_Err(vd, "subpicture upload failure: %s",
189 vdp_get_error_string(sys->vdp, err));
193 /* Render onto main surface */
197 reg->i_x + reg->fmt.i_visible_width,
198 reg->i_y + reg->fmt.i_visible_height,
200 VdpColor color = { 1.f, 1.f, 1.f, reg->i_alpha * alpha / 65535.f };
201 VdpOutputSurfaceRenderBlendState state = {
202 .struct_version = VDP_OUTPUT_SURFACE_RENDER_BLEND_STATE_VERSION,
203 .blend_factor_source_color =
204 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_SRC_ALPHA,
205 .blend_factor_destination_color =
206 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
207 .blend_factor_source_alpha =
208 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ZERO,
209 .blend_factor_destination_alpha =
210 VDP_OUTPUT_SURFACE_RENDER_BLEND_FACTOR_ONE,
211 .blend_equation_color = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
212 .blend_equation_alpha = VDP_OUTPUT_SURFACE_RENDER_BLEND_EQUATION_ADD,
213 .blend_constant = { 0.f, 0.f, 0.f, 0.f },
216 err = vdp_output_surface_render_bitmap_surface(sys->vdp, target, &area,
217 surface, NULL, &color, &state, 0);
218 if (err != VDP_STATUS_OK)
219 msg_Err(vd, "blending failure: %s",
220 vdp_get_error_string(sys->vdp, err));
222 out:/* Destroy GPU surface */
223 vdp_bitmap_surface_destroy(sys->vdp, surface);
226 static void Queue(vout_display_t *vd, picture_t *pic, subpicture_t *subpic)
228 vout_display_sys_t *sys = vd->sys;
229 VdpOutputSurface surface = pic->p_sys->surface;
232 VdpPresentationQueueStatus status;
234 err = vdp_presentation_queue_query_surface_status(sys->vdp, sys->queue,
235 surface, &status, &ts);
236 if (err == VDP_STATUS_OK && status != VDP_PRESENTATION_QUEUE_STATUS_IDLE)
237 msg_Dbg(vd, "surface status: %u", status);
240 for (subpicture_region_t *r = subpic->p_region; r != NULL;
242 RenderRegion(vd, surface, r, subpic->i_alpha);
244 /* Compute picture presentation time */
245 mtime_t now = mdate();
248 err = vdp_presentation_queue_get_time(sys->vdp, sys->queue, &pts);
249 if (err != VDP_STATUS_OK)
251 msg_Err(vd, "presentation queue time failure: %s",
252 vdp_get_error_string(sys->vdp, err));
253 if (err == VDP_STATUS_DISPLAY_PREEMPTED)
254 vout_display_SendEventPicturesInvalid(vd);
258 mtime_t delay = pic->date - now;
260 delay = 0; /* core bug: date is not updated during pause */
261 if (unlikely(delay > CLOCK_FREQ))
262 { /* We would get stuck if the delay was too long. */
263 msg_Dbg(vd, "picture date corrupt: delay of %"PRId64" us", delay);
264 delay = CLOCK_FREQ / 50;
269 err = vdp_presentation_queue_display(sys->vdp, sys->queue, surface, 0, 0,
271 if (err != VDP_STATUS_OK)
272 msg_Err(vd, "presentation queue display failure: %s",
273 vdp_get_error_string(sys->vdp, err));
276 static void Wait(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
278 vout_display_sys_t *sys = vd->sys;
280 picture_t *current = sys->current;
283 picture_sys_t *psys = current->p_sys;
287 err = vdp_presentation_queue_block_until_surface_idle(sys->vdp,
288 sys->queue, psys->surface, &pts);
289 if (err != VDP_STATUS_OK)
291 msg_Err(vd, "presentation queue blocking error: %s",
292 vdp_get_error_string(sys->vdp, err));
293 picture_Release(pic);
296 picture_Release(current);
303 static int Control(vout_display_t *vd, int query, va_list ap)
305 vout_display_sys_t *sys = vd->sys;
309 case VOUT_DISPLAY_HIDE_MOUSE:
310 xcb_change_window_attributes(sys->conn, sys->embed->handle.xid,
311 XCB_CW_CURSOR, &(uint32_t){ sys->cursor });
313 case VOUT_DISPLAY_RESET_PICTURES:
315 msg_Dbg(vd, "resetting pictures");
316 if (sys->pool != NULL)
318 PoolFree(vd, sys->pool);
322 const video_format_t *src= &vd->source;
323 video_format_t *fmt = &vd->fmt;
324 vout_display_place_t place;
326 vout_display_PlacePicture(&place, src, vd->cfg, false);
328 fmt->i_width = src->i_width * place.width / src->i_visible_width;
329 fmt->i_height = src->i_height * place.height / src->i_visible_height;
330 fmt->i_visible_width = place.width;
331 fmt->i_visible_height = place.height;
332 fmt->i_x_offset = src->i_x_offset * place.width / src->i_visible_width;
333 fmt->i_y_offset = src->i_y_offset * place.height / src->i_visible_height;
335 const uint32_t values[] = { place.x, place.y,
336 place.width, place.height, };
337 xcb_configure_window(sys->conn, sys->window,
338 XCB_CONFIG_WINDOW_X|XCB_CONFIG_WINDOW_Y|
339 XCB_CONFIG_WINDOW_WIDTH|XCB_CONFIG_WINDOW_HEIGHT,
343 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
345 const vout_display_cfg_t *c = va_arg(ap, const vout_display_cfg_t *);
346 return vout_window_SetFullScreen(sys->embed, c->is_fullscreen);
348 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
350 unsigned state = va_arg(ap, unsigned);
351 return vout_window_SetState(sys->embed, state);
353 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
355 const vout_display_cfg_t *cfg = va_arg(ap, const vout_display_cfg_t *);
356 bool forced = va_arg(ap, int);
359 vout_window_SetSize(sys->embed,
360 cfg->display.width, cfg->display.height);
361 return VLC_EGENERIC; /* Always fail. See x11.c for rationale. */
364 vout_display_place_t place;
365 vout_display_PlacePicture(&place, &vd->source, cfg, false);
366 if (place.width != vd->fmt.i_visible_width
367 || place.height != vd->fmt.i_visible_height)
369 vout_display_SendEventPicturesInvalid(vd);
373 const uint32_t values[] = { place.x, place.y,
374 place.width, place.height, };
375 xcb_configure_window(sys->conn, sys->window,
376 XCB_CONFIG_WINDOW_X|XCB_CONFIG_WINDOW_Y|
377 XCB_CONFIG_WINDOW_WIDTH|XCB_CONFIG_WINDOW_HEIGHT,
381 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
382 case VOUT_DISPLAY_CHANGE_ZOOM:
383 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
384 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
385 vout_display_SendEventPicturesInvalid (vd);
388 msg_Err(vd, "unknown control request %d", query);
391 xcb_flush (sys->conn);
395 static void Manage(vout_display_t *vd)
397 vout_display_sys_t *sys = vd->sys;
400 XCB_Manage(vd, sys->conn, &visible);
403 static int xcb_screen_num(xcb_connection_t *conn, const xcb_screen_t *screen)
405 const xcb_setup_t *setup = xcb_get_setup(conn);
408 for (xcb_screen_iterator_t i = xcb_setup_roots_iterator(setup);
409 i.rem > 0; xcb_screen_next(&i))
411 if (i.data->root == screen->root)
418 static int Open(vlc_object_t *obj)
420 if (!vlc_xlib_init(obj))
423 vout_display_t *vd = (vout_display_t *)obj;
424 vout_display_sys_t *sys = malloc(sizeof (*sys));
425 if (unlikely(sys == NULL))
428 const xcb_screen_t *screen;
429 uint16_t width, height;
430 sys->embed = XCB_parent_Create(vd, &sys->conn, &screen, &width, &height);
431 if (sys->embed == NULL)
437 /* Load the VDPAU back-end and create a device instance */
438 VdpStatus err = vdp_get_x11(sys->embed->display.x11,
439 xcb_screen_num(sys->conn, screen),
440 &sys->vdp, &sys->device);
441 if (err != VDP_STATUS_OK)
443 msg_Dbg(obj, "device creation failure: error %d", (int)err);
444 xcb_disconnect(sys->conn);
445 vout_display_DeleteWindow(vd, sys->embed);
451 if (vdp_get_information_string(sys->vdp, &info) == VDP_STATUS_OK)
452 msg_Dbg(vd, "using back-end %s", info);
454 /* Check source format */
455 VdpChromaType chroma;
456 VdpYCbCrFormat format;
457 if (vd->fmt.i_chroma == VLC_CODEC_VDPAU_VIDEO_420
458 || vd->fmt.i_chroma == VLC_CODEC_VDPAU_VIDEO_422)
461 if (vlc_fourcc_to_vdp_ycc(vd->fmt.i_chroma, &chroma, &format))
466 err = vdp_video_surface_query_capabilities(sys->vdp, sys->device,
467 chroma, &ok, &w, &h);
468 if (err != VDP_STATUS_OK)
470 msg_Err(vd, "%s capabilities query failure: %s", "video surface",
471 vdp_get_error_string(sys->vdp, err));
474 if (!ok || w < vd->fmt.i_width || h < vd->fmt.i_height)
476 msg_Err(vd, "source video %s not supported", "chroma type");
480 err = vdp_video_surface_query_get_put_bits_y_cb_cr_capabilities(
481 sys->vdp, sys->device, chroma, format, &ok);
482 if (err != VDP_STATUS_OK)
484 msg_Err(vd, "%s capabilities query failure: %s", "video surface",
485 vdp_get_error_string(sys->vdp, err));
490 msg_Err(vd, "source video %s not supported", "YCbCr format");
497 /* Check video mixer capabilities */
501 err = vdp_video_mixer_query_parameter_value_range(sys->vdp,
502 sys->device, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_WIDTH,
504 if (err != VDP_STATUS_OK)
506 msg_Err(vd, "%s capabilities query failure: %s",
507 "video mixer surface width",
508 vdp_get_error_string(sys->vdp, err));
511 if (min > vd->fmt.i_width || vd->fmt.i_width > max)
513 msg_Err(vd, "source video %s not supported", "width");
517 err = vdp_video_mixer_query_parameter_value_range(sys->vdp,
518 sys->device, VDP_VIDEO_MIXER_PARAMETER_VIDEO_SURFACE_HEIGHT,
520 if (err != VDP_STATUS_OK)
522 msg_Err(vd, "%s capabilities query failure: %s",
523 "video mixer surface width",
524 vdp_get_error_string(sys->vdp, err));
527 if (min > vd->fmt.i_height || vd->fmt.i_height > max)
529 msg_Err(vd, "source video %s not supported", "height");
534 /* Select surface format */
535 static const VdpRGBAFormat rgb_fmts[] = {
536 VDP_RGBA_FORMAT_R10G10B10A2, VDP_RGBA_FORMAT_B10G10R10A2,
537 VDP_RGBA_FORMAT_B8G8R8A8, VDP_RGBA_FORMAT_R8G8B8A8,
541 for (i = 0; i < sizeof (rgb_fmts) / sizeof (rgb_fmts[0]); i++)
546 err = vdp_output_surface_query_capabilities(sys->vdp, sys->device,
547 rgb_fmts[i], &ok, &w, &h);
548 if (err != VDP_STATUS_OK)
550 msg_Err(vd, "%s capabilities query failure: %s", "output surface",
551 vdp_get_error_string(sys->vdp, err));
554 /* NOTE: Wrong! No warranties that zoom <= 100%! */
555 if (!ok || w < vd->fmt.i_width || h < vd->fmt.i_height)
558 sys->rgb_fmt = rgb_fmts[i];
559 msg_Dbg(vd, "using RGBA format %u", sys->rgb_fmt);
562 if (i == sizeof (rgb_fmts) / sizeof (rgb_fmts[0]))
564 msg_Err(vd, "no supported output surface format");
568 /* VDPAU-X11 requires a window dedicated to the back-end */
570 xcb_pixmap_t pix = xcb_generate_id(sys->conn);
571 xcb_create_pixmap(sys->conn, screen->root_depth, pix,
575 XCB_CW_BACK_PIXMAP | XCB_CW_BACK_PIXEL |
576 XCB_CW_BORDER_PIXMAP | XCB_CW_BORDER_PIXEL |
577 XCB_CW_EVENT_MASK | XCB_CW_COLORMAP;
578 const uint32_t values[] = {
579 pix, screen->black_pixel, pix, screen->black_pixel,
580 XCB_EVENT_MASK_VISIBILITY_CHANGE, screen->default_colormap
582 vout_display_place_t place;
584 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
585 sys->window = xcb_generate_id(sys->conn);
587 xcb_void_cookie_t c =
588 xcb_create_window_checked(sys->conn, screen->root_depth,
589 sys->window, sys->embed->handle.xid, place.x, place.y,
590 place.width, place.height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
591 screen->root_visual, mask, values);
592 if (XCB_error_Check(vd, sys->conn, "window creation failure", c))
594 msg_Dbg(vd, "using X11 window 0x%08"PRIx32, sys->window);
595 xcb_map_window(sys->conn, sys->window);
598 /* Check bitmap capabilities (for SPU) */
599 const vlc_fourcc_t *spu_chromas = NULL;
601 static const vlc_fourcc_t subpicture_chromas[] = { VLC_CODEC_RGBA, 0 };
605 err = vdp_bitmap_surface_query_capabilities(sys->vdp, sys->device,
606 VDP_RGBA_FORMAT_R8G8B8A8, &ok, &w, &h);
607 if (err != VDP_STATUS_OK)
609 msg_Err(vd, "%s capabilities query failure: %s", "output surface",
610 vdp_get_error_string(sys->vdp, err));
614 spu_chromas = subpicture_chromas;
617 /* Initialize VDPAU queue */
618 err = vdp_presentation_queue_target_create_x11(sys->vdp, sys->device,
619 sys->window, &sys->target);
620 if (err != VDP_STATUS_OK)
622 msg_Err(vd, "%s creation failure: %s", "presentation queue target",
623 vdp_get_error_string(sys->vdp, err));
627 err = vdp_presentation_queue_create(sys->vdp, sys->device, sys->target,
629 if (err != VDP_STATUS_OK)
631 msg_Err(vd, "%s creation failure: %s", "presentation queue",
632 vdp_get_error_string(sys->vdp, err));
633 vdp_presentation_queue_target_destroy(sys->vdp, sys->target);
637 VdpColor black = { 0.f, 0.f, 0.f, 1.f };
638 vdp_presentation_queue_set_background_color(sys->vdp, sys->queue, &black);
640 sys->cursor = XCB_cursor_Create(sys->conn, screen);
645 vd->info.has_pictures_invalid = true;
646 vd->info.has_event_thread = true;
647 vd->info.subpicture_chromas = spu_chromas;
648 vd->fmt.i_chroma = VLC_CODEC_VDPAU_OUTPUT;
653 vd->control = Control;
657 bool is_fullscreen = vd->cfg->is_fullscreen;
658 if (is_fullscreen && vout_window_SetFullScreen(sys->embed, true))
659 is_fullscreen = false;
660 vout_display_SendEventFullscreen(vd, is_fullscreen);
661 vout_display_SendEventDisplaySize(vd, width, height, is_fullscreen);
666 vdp_release_x11(sys->vdp);
667 xcb_disconnect(sys->conn);
668 vout_display_DeleteWindow(vd, sys->embed);
673 static void Close(vlc_object_t *obj)
675 vout_display_t *vd = (vout_display_t *)obj;
676 vout_display_sys_t *sys = vd->sys;
678 /* Restore cursor explicitly (parent window connection will survive) */
679 xcb_change_window_attributes(sys->conn, sys->embed->handle.xid,
680 XCB_CW_CURSOR, &(uint32_t) { XCB_CURSOR_NONE });
681 xcb_flush(sys->conn);
683 vdp_presentation_queue_destroy(sys->vdp, sys->queue);
684 vdp_presentation_queue_target_destroy(sys->vdp, sys->target);
686 if (sys->pool != NULL)
687 PoolFree(vd, sys->pool);
689 vdp_release_x11(sys->vdp);
690 xcb_disconnect(sys->conn);
691 vout_display_DeleteWindow(vd, sys->embed);