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 FORMAT_TEXT N_("XVideo format id")
48 #define FORMAT_LONGTEXT N_( \
49 "XVideo image format id to use. By default, VLC will " \
50 "try to use the best match for the video being played.")
52 #define SHM_TEXT N_("Use shared memory")
53 #define SHM_LONGTEXT N_( \
54 "Use shared memory to communicate between VLC and the X server.")
56 static int Open (vlc_object_t *);
57 static void Close (vlc_object_t *);
63 set_shortname (N_("XVideo"))
64 set_description (N_("XVideo output (XCB)"))
65 set_category (CAT_VIDEO)
66 set_subcategory (SUBCAT_VIDEO_VOUT)
67 set_capability ("vout display", 155)
68 set_callbacks (Open, Close)
70 add_integer ("xvideo-adaptor", -1, NULL,
71 ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
72 add_integer ("xvideo-format-id", 0, NULL,
73 FORMAT_TEXT, FORMAT_LONGTEXT, true)
74 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
75 add_deprecated_alias ("xvideo-shm")
76 add_shortcut ("xcb-xv", "xv", "xvideo", "xid")
79 #define MAX_PICTURES (128)
81 struct vout_display_sys_t
83 xcb_connection_t *conn;
84 vout_window_t *embed;/* VLC window */
86 xcb_cursor_t cursor; /* blank cursor */
87 xcb_window_t window; /* drawable X window */
88 xcb_gcontext_t gc; /* context to put images */
89 xcb_xv_port_t port; /* XVideo port */
90 uint32_t id; /* XVideo format */
91 uint16_t width; /* display width */
92 uint16_t height; /* display height */
93 uint32_t data_size; /* picture byte size (for non-SHM) */
94 bool swap_uv; /* U/V pointer must be swapped in a picture */
95 bool shm; /* whether to use MIT-SHM */
96 bool visible; /* whether it makes sense to draw at all */
98 xcb_xv_query_image_attributes_reply_t *att;
99 picture_pool_t *pool; /* picture pool */
100 picture_resource_t resource[MAX_PICTURES];
103 static picture_pool_t *Pool (vout_display_t *, unsigned);
104 static void Display (vout_display_t *, picture_t *);
105 static int Control (vout_display_t *, int, va_list);
106 static void Manage (vout_display_t *);
109 * Check that the X server supports the XVideo extension.
111 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
113 xcb_xv_query_extension_reply_t *r;
114 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
117 /* We need XVideo 2.2 for PutImage */
118 r = xcb_xv_query_extension_reply (conn, ck, NULL);
120 msg_Dbg (vd, "XVideo extension not available");
123 msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
127 msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
131 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
139 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
140 const xcb_xv_image_format_info_t *restrict f)
142 if (f->byte_order != ORDER && f->bpp != 8)
143 return 0; /* Argh! */
147 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
148 switch (f->num_planes)
155 return VLC_CODEC_RGB32;
157 return 0; /* ARGB -> VLC cannot do that currently */
161 return VLC_CODEC_RGB24;
165 return VLC_CODEC_RGB16;
167 return VLC_CODEC_RGB15;
171 return VLC_CODEC_RGB8;
176 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
178 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
179 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
182 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
183 if (f->u_sample_bits != f->v_sample_bits
184 || f->vhorz_u_period != f->vhorz_v_period
185 || f->vvert_u_period != f->vvert_v_period
186 || f->y_sample_bits != 8 || f->u_sample_bits != 8
187 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
189 switch (f->num_planes)
195 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
196 return VLC_CODEC_I444;
199 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
201 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
202 return VLC_CODEC_YUYV;
203 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
204 return VLC_CODEC_UYVY;
213 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
215 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
216 return VLC_CODEC_YV12;
217 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
218 return VLC_CODEC_I420;
224 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
226 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
227 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
228 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
229 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
230 "%"PRIu32"/%"PRIu32"/%"PRIu32,
231 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
232 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
233 msg_Warn (vd, " order: %.32s", f->vcomp_order);
240 static const xcb_xv_image_format_info_t *
241 FindFormat (vout_display_t *vd,
242 vlc_fourcc_t chroma, const video_format_t *fmt,
244 const xcb_xv_list_image_formats_reply_t *list,
245 xcb_xv_query_image_attributes_reply_t **restrict pa)
247 xcb_connection_t *conn = vd->sys->conn;
248 const xcb_xv_image_format_info_t *f, *end;
251 f = xcb_xv_list_image_formats_format (list);
253 f = (xcb_xv_image_format_info_t *) (list + 1);
255 end = f + xcb_xv_list_image_formats_format_length (list);
258 if (chroma != ParseFormat (vd, f))
261 /* VLC pads scanline to 16 pixels internally */
262 unsigned width = fmt->i_width;
263 unsigned height = fmt->i_height;
264 xcb_xv_query_image_attributes_reply_t *i;
265 i = xcb_xv_query_image_attributes_reply (conn,
266 xcb_xv_query_image_attributes (conn, port, f->id,
267 width, height), NULL);
271 if (i->width != width || i->height != height)
273 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
274 fmt->i_width, fmt->i_height,
275 i->width, i->height);
276 var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
277 if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
279 dialog_FatalWait (vd, _("Video acceleration not available"),
280 _("Your video output acceleration driver does not support "
281 "the required resolution: %ux%u pixels. The maximum "
282 "supported resolution is %"PRIu32"x%"PRIu32".\n"
283 "Video output acceleration will be disabled. However, "
284 "rendering videos with overly large resolution "
285 "may cause severe performance degration."),
286 width, height, i->width, i->height);
287 var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
300 * Probe the X server.
302 static int Open (vlc_object_t *obj)
304 vout_display_t *vd = (vout_display_t *)obj;
305 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
307 if (!var_InheritBool (obj, "overlay"))
315 xcb_connection_t *conn;
316 const xcb_screen_t *screen;
318 p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
319 if (p_sys->embed == NULL)
328 p_sys->swap_uv = false;
330 if (!CheckXVideo (vd, conn))
332 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
336 p_sys->window = xcb_generate_id (conn);
337 xcb_pixmap_t pixmap = xcb_generate_id (conn);
339 /* Cache adaptors infos */
340 xcb_xv_query_adaptors_reply_t *adaptors =
341 xcb_xv_query_adaptors_reply (conn,
342 xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
343 if (adaptors == NULL)
346 int forced_adaptor = var_InheritInteger (obj, "xvideo-adaptor");
349 video_format_t fmt = vd->fmt;
352 xcb_xv_adaptor_info_iterator_t it;
353 for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
355 xcb_xv_adaptor_info_next (&it))
357 const xcb_xv_adaptor_info_t *a = it.data;
360 if (forced_adaptor != -1 && forced_adaptor != 0)
366 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
369 xcb_xv_list_image_formats_reply_t *r =
370 xcb_xv_list_image_formats_reply (conn,
371 xcb_xv_list_image_formats (conn, a->base_id), NULL);
375 /* Look for an image format */
376 const xcb_xv_image_format_info_t *xfmt = NULL;
377 const vlc_fourcc_t *chromas, chromas_default[] = {
386 if (vlc_fourcc_IsYUV (fmt.i_chroma))
387 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
389 chromas = chromas_default;
391 int forced_format_id = var_CreateGetInteger (obj, "xvideo-format-id");
392 vlc_fourcc_t chroma = forced_format_id;
393 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
394 for (size_t i = 0; !xfmt && chromas[i]; i++)
399 if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
401 && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
403 msg_Dbg (vd, "skipping slow I420 format");
404 continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
407 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
408 if (xfmt != NULL) break;
411 if (xfmt == NULL) /* No acceptable image formats */
414 p_sys->id = xfmt->id;
415 p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma, chroma);
417 fmt.i_chroma = chroma;
418 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
420 fmt.i_rmask = xfmt->red_mask;
421 fmt.i_gmask = xfmt->green_mask;
422 fmt.i_bmask = xfmt->blue_mask;
426 for (unsigned i = 0; i < a->num_ports; i++)
428 xcb_xv_port_t port = a->base_id + i;
429 xcb_xv_grab_port_reply_t *gr =
430 xcb_xv_grab_port_reply (conn,
431 xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
432 uint8_t result = gr ? gr->result : 0xff;
440 msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
443 continue; /* No usable port */
446 /* Found port - initialize selected format */
447 name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
450 msg_Dbg (vd, "using adaptor %s", name);
453 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
454 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
456 /* Look for an X11 visual, create a window */
457 xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
458 for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
460 if (f->depth != screen->root_depth)
461 continue; /* this would fail anyway */
466 XCB_CW_BORDER_PIXMAP |
467 XCB_CW_BORDER_PIXEL |
470 const uint32_t list[] = {
471 /* XCB_CW_BACK_PIXMAP */
473 /* XCB_CW_BACK_PIXEL */
475 /* XCB_CW_BORDER_PIXMAP */
477 /* XCB_CW_BORDER_PIXEL */
479 /* XCB_CW_EVENT_MASK */
480 XCB_EVENT_MASK_VISIBILITY_CHANGE,
481 /* XCB_CW_COLORMAP */
482 screen->default_colormap,
487 xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
488 c = xcb_create_window_checked (conn, f->depth, p_sys->window,
489 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
490 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
492 if (!CheckError (vd, conn, "cannot create X11 window", c))
494 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
495 " (depth: %"PRIu8")", f->visual, f->depth);
496 msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
500 xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
502 msg_Dbg (vd, "no usable X11 visual");
503 continue; /* No workable XVideo format (visual/depth) */
511 msg_Err (vd, "no available XVideo adaptor");
516 xcb_map_window (conn, p_sys->window);
518 vout_display_place_t place;
520 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
521 p_sys->width = place.width;
522 p_sys->height = place.height;
525 const uint32_t values[] = {
526 place.x, place.y, place.width, place.height };
527 xcb_configure_window (conn, p_sys->window,
528 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
529 XCB_CONFIG_WINDOW_WIDTH |
530 XCB_CONFIG_WINDOW_HEIGHT,
533 p_sys->visible = false;
535 /* Create graphic context */
536 p_sys->gc = xcb_generate_id (conn);
537 xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
538 msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
541 p_sys->cursor = CreateBlankCursor (conn, screen);
543 CheckSHM (obj, conn, &p_sys->shm);
546 vout_display_info_t info = vd->info;
547 info.has_pictures_invalid = false;
548 info.has_event_thread = true;
550 /* Setup vout_display_t once everything is fine */
556 vd->display = Display;
557 vd->control = Control;
561 bool is_fullscreen = vd->cfg->is_fullscreen;
562 if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
563 is_fullscreen = false;
564 vout_display_SendEventFullscreen (vd, is_fullscreen);
565 unsigned width, height;
566 if (!GetWindowSize (p_sys->embed, conn, &width, &height))
567 vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
578 * Disconnect from the X server.
580 static void Close (vlc_object_t *obj)
582 vout_display_t *vd = (vout_display_t *)obj;
583 vout_display_sys_t *p_sys = vd->sys;
587 for (unsigned i = 0; i < MAX_PICTURES; i++)
589 picture_resource_t *res = &p_sys->resource[i];
591 if (!res->p->p_pixels)
593 PictureResourceFree (res, NULL);
595 picture_pool_Delete (p_sys->pool);
598 /* show the default cursor */
599 xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
600 &(uint32_t) { XCB_CURSOR_NONE });
601 xcb_flush (p_sys->conn);
604 xcb_disconnect (p_sys->conn);
605 vout_display_DeleteWindow (vd, p_sys->embed);
609 static void PoolAlloc (vout_display_t *vd, unsigned requested_count)
611 vout_display_sys_t *p_sys = vd->sys;
613 memset (p_sys->resource, 0, sizeof(p_sys->resource));
615 const uint32_t *pitches= xcb_xv_query_image_attributes_pitches (p_sys->att);
616 const uint32_t *offsets= xcb_xv_query_image_attributes_offsets (p_sys->att);
617 const unsigned num_planes= __MIN(p_sys->att->num_planes, PICTURE_PLANE_MAX);
618 p_sys->data_size = p_sys->att->data_size;
620 picture_t *pic_array[MAX_PICTURES];
621 requested_count = __MIN(requested_count, MAX_PICTURES);
624 for (count = 0; count < requested_count; count++)
626 picture_resource_t *res = &p_sys->resource[count];
628 for (unsigned i = 0; i < num_planes; i++)
631 data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
633 res->p[i].i_lines = (data_size - offsets[i]) / pitches[i];
634 res->p[i].i_pitch = pitches[i];
637 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
638 p_sys->conn, p_sys->shm))
641 /* Allocate further planes as specified by XVideo */
642 /* We assume that offsets[0] is zero */
643 for (unsigned i = 1; i < num_planes; i++)
644 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
647 { /* YVU: swap U and V planes */
648 uint8_t *buf = res->p[2].p_pixels;
649 res->p[2].p_pixels = res->p[1].p_pixels;
650 res->p[1].p_pixels = buf;
653 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
654 if (!pic_array[count])
656 PictureResourceFree (res, p_sys->conn);
657 memset (res, 0, sizeof(*res));
665 p_sys->pool = picture_pool_New (count, pic_array);
666 /* TODO release picture resources if NULL */
667 xcb_flush (p_sys->conn);
671 * Return a direct buffer
673 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
675 vout_display_sys_t *p_sys = vd->sys;
678 PoolAlloc (vd, requested_count);
684 * Sends an image to the X server.
686 static void Display (vout_display_t *vd, picture_t *pic)
688 vout_display_sys_t *p_sys = vd->sys;
689 xcb_shm_seg_t segment = pic->p_sys->segment;
690 xcb_void_cookie_t ck;
694 xcb_force_screen_saver (p_sys->conn, XCB_SCREEN_SAVER_RESET);
697 ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
698 p_sys->window, p_sys->gc, segment, p_sys->id, 0,
699 /* Src: */ vd->source.i_x_offset,
700 vd->source.i_y_offset,
701 vd->source.i_visible_width,
702 vd->source.i_visible_height,
703 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
704 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
705 pic->p->i_lines, false);
707 ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
708 p_sys->gc, p_sys->id,
709 vd->source.i_x_offset,
710 vd->source.i_y_offset,
711 vd->source.i_visible_width,
712 vd->source.i_visible_height,
713 0, 0, p_sys->width, p_sys->height,
714 pic->p->i_pitch / pic->p->i_pixel_pitch,
716 p_sys->data_size, pic->p->p_pixels);
718 /* Wait for reply. See x11.c for rationale. */
719 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
722 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
726 picture_Release (pic);
729 static int Control (vout_display_t *vd, int query, va_list ap)
731 vout_display_sys_t *p_sys = vd->sys;
735 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
737 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
738 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
741 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
742 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
743 case VOUT_DISPLAY_CHANGE_ZOOM:
744 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
745 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
747 const vout_display_cfg_t *cfg;
748 const video_format_t *source;
749 bool is_forced = false;
751 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
752 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
754 source = (const video_format_t *)va_arg (ap, const video_format_t *);
759 source = &vd->source;
760 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
761 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
762 is_forced = (bool)va_arg (ap, int);
766 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
768 && (cfg->display.width != vd->cfg->display.width
769 ||cfg->display.height != vd->cfg->display.height)
770 && vout_window_SetSize (p_sys->embed,
772 cfg->display.height))
775 vout_display_place_t place;
776 vout_display_PlacePicture (&place, source, cfg, false);
777 p_sys->width = place.width;
778 p_sys->height = place.height;
780 /* Move the picture within the window */
781 const uint32_t values[] = { place.x, place.y,
782 place.width, place.height, };
783 xcb_configure_window (p_sys->conn, p_sys->window,
784 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
785 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
787 xcb_flush (p_sys->conn);
790 case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
792 unsigned state = va_arg (ap, unsigned);
793 return vout_window_SetState (p_sys->embed, state);
796 /* Hide the mouse. It will be send when
797 * vout_display_t::info.b_hide_mouse is false */
798 case VOUT_DISPLAY_HIDE_MOUSE:
799 xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
800 XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
802 case VOUT_DISPLAY_RESET_PICTURES:
805 msg_Err (vd, "Unknown request in XCB vout display");
810 static void Manage (vout_display_t *vd)
812 vout_display_sys_t *p_sys = vd->sys;
814 ManageEvent (vd, p_sys->conn, &p_sys->visible);