3 * @brief X C Bindings video output module for VLC media player
5 /*****************************************************************************
6 * Copyright © 2009 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This library 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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_picture_pool.h>
38 #include <vlc_dialog.h>
42 #define ADAPTOR_TEXT N_("XVideo adaptor number")
43 #define ADAPTOR_LONGTEXT N_( \
44 "XVideo hardware adaptor to use. By default, VLC will " \
45 "use the first functional adaptor.")
47 #define SHM_TEXT N_("Use shared memory")
48 #define SHM_LONGTEXT N_( \
49 "Use shared memory to communicate between VLC and the X server.")
51 static int Open (vlc_object_t *);
52 static void Close (vlc_object_t *);
58 set_shortname (N_("XVideo"))
59 set_description (N_("XVideo output (XCB)"))
60 set_category (CAT_VIDEO)
61 set_subcategory (SUBCAT_VIDEO_VOUT)
62 set_capability ("vout display", 155)
63 set_callbacks (Open, Close)
65 add_integer ("xvideo-adaptor", -1, NULL,
66 ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
67 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
68 add_deprecated_alias ("xvideo-shm")
69 add_shortcut ("xcb-xv")
71 add_shortcut ("xvideo")
74 #define MAX_PICTURES (128)
76 struct vout_display_sys_t
78 xcb_connection_t *conn;
79 vout_window_t *embed;/* VLC window */
81 xcb_cursor_t cursor; /* blank cursor */
82 xcb_window_t window; /* drawable X window */
83 xcb_gcontext_t gc; /* context to put images */
84 xcb_xv_port_t port; /* XVideo port */
85 uint32_t id; /* XVideo format */
86 uint16_t width; /* display width */
87 uint16_t height; /* display height */
88 uint32_t data_size; /* picture byte size (for non-SHM) */
89 bool swap_uv; /* U/V pointer must be swapped in a picture */
90 bool shm; /* whether to use MIT-SHM */
91 bool visible; /* whether it makes sense to draw at all */
93 xcb_xv_query_image_attributes_reply_t *att;
94 picture_pool_t *pool; /* picture pool */
95 picture_resource_t resource[MAX_PICTURES];
98 static picture_pool_t *Pool (vout_display_t *, unsigned);
99 static void Display (vout_display_t *, picture_t *);
100 static int Control (vout_display_t *, int, va_list);
101 static void Manage (vout_display_t *);
104 * Check that the X server supports the XVideo extension.
106 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
108 xcb_xv_query_extension_reply_t *r;
109 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
112 /* We need XVideo 2.2 for PutImage */
113 r = xcb_xv_query_extension_reply (conn, ck, NULL);
115 msg_Dbg (vd, "XVideo extension not available");
118 msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
122 msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
126 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
134 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
135 const xcb_xv_image_format_info_t *restrict f)
137 if (f->byte_order != ORDER && f->bpp != 8)
138 return 0; /* Argh! */
142 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
143 switch (f->num_planes)
150 return VLC_CODEC_RGB32;
152 return 0; /* ARGB -> VLC cannot do that currently */
156 return VLC_CODEC_RGB24;
160 return VLC_CODEC_RGB16;
162 return VLC_CODEC_RGB15;
166 return VLC_CODEC_RGB8;
171 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
173 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
174 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
177 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
178 if (f->u_sample_bits != f->v_sample_bits
179 || f->vhorz_u_period != f->vhorz_v_period
180 || f->vvert_u_period != f->vvert_v_period
181 || f->y_sample_bits != 8 || f->u_sample_bits != 8
182 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
184 switch (f->num_planes)
190 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
191 return VLC_CODEC_I444;
194 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
196 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
197 return VLC_CODEC_YUYV;
198 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
199 return VLC_CODEC_UYVY;
208 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
210 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
211 return VLC_CODEC_YV12;
212 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
213 return VLC_CODEC_I420;
219 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
221 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
222 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
223 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
224 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
225 "%"PRIu32"/%"PRIu32"/%"PRIu32,
226 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
227 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
228 msg_Warn (vd, " order: %.32s", f->vcomp_order);
235 static const xcb_xv_image_format_info_t *
236 FindFormat (vout_display_t *vd,
237 vlc_fourcc_t chroma, const video_format_t *fmt,
239 const xcb_xv_list_image_formats_reply_t *list,
240 xcb_xv_query_image_attributes_reply_t **restrict pa)
242 xcb_connection_t *conn = vd->sys->conn;
243 const xcb_xv_image_format_info_t *f, *end;
246 f = xcb_xv_list_image_formats_format (list);
248 f = (xcb_xv_image_format_info_t *) (list + 1);
250 end = f + xcb_xv_list_image_formats_format_length (list);
253 if (chroma != ParseFormat (vd, f))
256 /* VLC pads scanline to 16 pixels internally */
257 unsigned width = (fmt->i_width + 15) & ~15;
258 unsigned height = (fmt->i_height + 15) & ~15;
259 xcb_xv_query_image_attributes_reply_t *i;
260 i = xcb_xv_query_image_attributes_reply (conn,
261 xcb_xv_query_image_attributes (conn, port, f->id,
262 width, height), NULL);
266 if (i->width != width || i->height != height)
268 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
269 fmt->i_width, fmt->i_height,
270 i->width, i->height);
271 var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
272 if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
274 dialog_FatalWait (vd, _("Video acceleration not available"),
275 _("Your video output acceleration driver does not support "
276 "the required resolution: %ux%u pixels. The maximum "
277 "supported resolution is %"PRIu32"x%"PRIu32".\n"
278 "Video output acceleration will be disabled. However, "
279 "rendering videos with overly large resolution "
280 "may cause severe performance degration."),
281 width, height, i->width, i->height);
282 var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
295 * Probe the X server.
297 static int Open (vlc_object_t *obj)
299 vout_display_t *vd = (vout_display_t *)obj;
300 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
302 if (!var_CreateGetBool (obj, "overlay"))
310 xcb_connection_t *conn;
311 const xcb_screen_t *screen;
313 p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
314 if (p_sys->embed == NULL)
323 p_sys->swap_uv = false;
325 if (!CheckXVideo (vd, conn))
327 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
331 p_sys->window = xcb_generate_id (conn);
332 xcb_pixmap_t pixmap = xcb_generate_id (conn);
334 /* Cache adaptors infos */
335 xcb_xv_query_adaptors_reply_t *adaptors =
336 xcb_xv_query_adaptors_reply (conn,
337 xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
338 if (adaptors == NULL)
341 int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
344 video_format_t fmt = vd->fmt;
345 bool found_adaptor = false;
347 xcb_xv_adaptor_info_iterator_t it;
348 for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
349 it.rem > 0 && !found_adaptor;
350 xcb_xv_adaptor_info_next (&it))
352 const xcb_xv_adaptor_info_t *a = it.data;
355 if (forced_adaptor != -1 && forced_adaptor != 0)
361 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
364 xcb_xv_list_image_formats_reply_t *r =
365 xcb_xv_list_image_formats_reply (conn,
366 xcb_xv_list_image_formats (conn, a->base_id), NULL);
370 /* Look for an image format */
371 const xcb_xv_image_format_info_t *xfmt = NULL;
372 const vlc_fourcc_t *chromas, chromas_default[] = {
381 if (vlc_fourcc_IsYUV (fmt.i_chroma))
382 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
384 chromas = chromas_default;
387 for (size_t i = 0; chromas[i]; i++)
392 if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
394 && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
396 msg_Dbg (vd, "skipping slow I420 format");
397 continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
400 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
403 p_sys->id = xfmt->id;
404 p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma,
407 fmt.i_chroma = chroma;
408 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
410 fmt.i_rmask = xfmt->red_mask;
411 fmt.i_gmask = xfmt->green_mask;
412 fmt.i_bmask = xfmt->blue_mask;
418 if (xfmt == NULL) /* No acceptable image formats */
422 for (unsigned i = 0; i < a->num_ports; i++)
424 xcb_xv_port_t port = a->base_id + i;
425 xcb_xv_grab_port_reply_t *gr =
426 xcb_xv_grab_port_reply (conn,
427 xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
428 uint8_t result = gr ? gr->result : 0xff;
436 msg_Dbg (vd, "cannot grab port %"PRIu32, port);
438 continue; /* No usable port */
441 /* Found port - initialize selected format */
442 name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
445 msg_Dbg (vd, "using adaptor %s", name);
448 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
449 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
451 /* Look for an X11 visual, create a window */
452 xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
453 for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
455 if (f->depth != screen->root_depth)
456 continue; /* this would fail anyway */
461 XCB_CW_BORDER_PIXMAP |
462 XCB_CW_BORDER_PIXEL |
465 const uint32_t list[] = {
466 /* XCB_CW_BACK_PIXMAP */
468 /* XCB_CW_BACK_PIXEL */
470 /* XCB_CW_BORDER_PIXMAP */
472 /* XCB_CW_BORDER_PIXEL */
474 /* XCB_CW_EVENT_MASK */
475 XCB_EVENT_MASK_VISIBILITY_CHANGE,
476 /* XCB_CW_COLORMAP */
477 screen->default_colormap,
482 xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
483 c = xcb_create_window_checked (conn, f->depth, p_sys->window,
484 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
485 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
487 if (!CheckError (vd, conn, "cannot create X11 window", c))
489 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
490 " (depth: %"PRIu8")", f->visual, f->depth);
491 msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
495 xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
496 msg_Dbg (vd, "no usable X11 visual");
497 continue; /* No workable XVideo format (visual/depth) */
500 found_adaptor = true;
506 msg_Err (vd, "no available XVideo adaptor");
511 xcb_map_window (conn, p_sys->window);
513 vout_display_place_t place;
515 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
516 p_sys->width = place.width;
517 p_sys->height = place.height;
520 const uint32_t values[] = {
521 place.x, place.y, place.width, place.height };
522 xcb_configure_window (conn, p_sys->window,
523 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
524 XCB_CONFIG_WINDOW_WIDTH |
525 XCB_CONFIG_WINDOW_HEIGHT,
528 p_sys->visible = false;
530 /* Create graphic context */
531 p_sys->gc = xcb_generate_id (conn);
532 xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
533 msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
536 p_sys->cursor = CreateBlankCursor (conn, screen);
538 CheckSHM (obj, conn, &p_sys->shm);
541 vout_display_info_t info = vd->info;
542 info.has_pictures_invalid = false;
543 info.has_event_thread = true;
545 /* Setup vout_display_t once everything is fine */
551 vd->display = Display;
552 vd->control = Control;
556 bool is_fullscreen = vd->cfg->is_fullscreen;
557 if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
558 is_fullscreen = false;
559 vout_display_SendEventFullscreen (vd, is_fullscreen);
560 unsigned width, height;
561 if (!GetWindowSize (p_sys->embed, conn, &width, &height))
562 vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
573 * Disconnect from the X server.
575 static void Close (vlc_object_t *obj)
577 vout_display_t *vd = (vout_display_t *)obj;
578 vout_display_sys_t *p_sys = vd->sys;
582 for (unsigned i = 0; i < MAX_PICTURES; i++)
584 picture_resource_t *res = &p_sys->resource[i];
586 if (!res->p->p_pixels)
588 PictureResourceFree (res, NULL);
590 picture_pool_Delete (p_sys->pool);
593 /* show the default cursor */
594 xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
595 &(uint32_t) { XCB_CURSOR_NONE });
596 xcb_flush (p_sys->conn);
599 xcb_disconnect (p_sys->conn);
600 vout_display_DeleteWindow (vd, p_sys->embed);
605 * Return a direct buffer
607 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
609 vout_display_sys_t *p_sys = vd->sys;
613 memset (p_sys->resource, 0, sizeof(p_sys->resource));
615 const uint32_t *pitches =
616 xcb_xv_query_image_attributes_pitches (p_sys->att);
617 const uint32_t *offsets =
618 xcb_xv_query_image_attributes_offsets (p_sys->att);
619 p_sys->data_size = p_sys->att->data_size;
622 picture_t *pic_array[MAX_PICTURES];
623 for (count = 0; count < requested_count; count++)
625 if (count >= MAX_PICTURES)
627 picture_resource_t *res = &p_sys->resource[count];
629 for (int i = 0; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
632 ((i + 1 < p_sys->att->num_planes ? offsets[i+1] :
633 p_sys->data_size) - offsets[i]) / pitches[i];
634 res->p[i].i_pitch = pitches[i];
636 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
637 p_sys->conn, p_sys->shm))
640 /* Allocate further planes as specified by XVideo */
641 /* We assume that offsets[0] is zero */
642 for (int i = 1; i < __MIN (p_sys->att->num_planes, PICTURE_PLANE_MAX); i++)
643 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
645 { /* YVU: swap U and V planes */
646 uint8_t *buf = res->p[2].p_pixels;
647 res->p[2].p_pixels = res->p[1].p_pixels;
648 res->p[1].p_pixels = buf;
651 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
652 if (!pic_array[count])
654 PictureResourceFree (res, p_sys->conn);
655 memset (res, 0, sizeof(*res));
663 p_sys->pool = picture_pool_New (count, pic_array);
664 /* TODO release picture resources if NULL */
665 xcb_flush (p_sys->conn);
672 * Sends an image to the X server.
674 static void Display (vout_display_t *vd, picture_t *pic)
676 vout_display_sys_t *p_sys = vd->sys;
677 xcb_shm_seg_t segment = pic->p_sys->segment;
678 xcb_void_cookie_t ck;
683 ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
684 p_sys->window, p_sys->gc, segment, p_sys->id, 0,
685 /* Src: */ vd->source.i_x_offset,
686 vd->source.i_y_offset,
687 vd->source.i_visible_width,
688 vd->source.i_visible_height,
689 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
690 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
691 pic->p->i_lines, false);
693 ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
694 p_sys->gc, p_sys->id,
695 vd->source.i_x_offset,
696 vd->source.i_y_offset,
697 vd->source.i_visible_width,
698 vd->source.i_visible_height,
699 0, 0, p_sys->width, p_sys->height,
700 pic->p->i_pitch / pic->p->i_pixel_pitch,
702 p_sys->data_size, pic->p->p_pixels);
704 /* Wait for reply. See x11.c for rationale. */
705 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
708 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
712 picture_Release (pic);
715 static int Control (vout_display_t *vd, int query, va_list ap)
717 vout_display_sys_t *p_sys = vd->sys;
721 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
723 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
724 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
727 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
728 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
729 case VOUT_DISPLAY_CHANGE_ZOOM:
730 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
731 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
733 const vout_display_cfg_t *cfg;
734 const video_format_t *source;
735 bool is_forced = false;
737 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
738 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
740 source = (const video_format_t *)va_arg (ap, const video_format_t *);
745 source = &vd->source;
746 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
747 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
748 is_forced = (bool)va_arg (ap, int);
752 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
754 && (cfg->display.width != vd->cfg->display.width
755 ||cfg->display.height != vd->cfg->display.height)
756 && vout_window_SetSize (p_sys->embed,
758 cfg->display.height))
761 vout_display_place_t place;
762 vout_display_PlacePicture (&place, source, cfg, false);
763 p_sys->width = place.width;
764 p_sys->height = place.height;
766 /* Move the picture within the window */
767 const uint32_t values[] = { place.x, place.y,
768 place.width, place.height, };
769 xcb_configure_window (p_sys->conn, p_sys->window,
770 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
771 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
773 xcb_flush (p_sys->conn);
776 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
778 unsigned state = va_arg (ap, unsigned);
779 return vout_window_SetState (p_sys->embed, state);
782 /* Hide the mouse. It will be send when
783 * vout_display_t::info.b_hide_mouse is false */
784 case VOUT_DISPLAY_HIDE_MOUSE:
785 xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
786 XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
788 case VOUT_DISPLAY_RESET_PICTURES:
791 msg_Err (vd, "Unknown request in XCB vout display");
796 static void Manage (vout_display_t *vd)
798 vout_display_sys_t *p_sys = vd->sys;
800 ManageEvent (vd, p_sys->conn, &p_sys->visible);