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 DISPLAY_TEXT N_("X11 display")
43 #define DISPLAY_LONGTEXT N_( \
44 "X11 hardware display to use. By default, VLC will " \
45 "use the value of the DISPLAY environment variable.")
47 #define ADAPTOR_TEXT N_("XVideo adaptor number")
48 #define ADAPTOR_LONGTEXT N_( \
49 "XVideo hardware adaptor to use. By default, VLC will " \
50 "use the first functional adaptor.")
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_string ("x11-display", NULL, NULL,
71 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
72 add_deprecated_alias ("xvideo-display")
73 add_integer ("xvideo-adaptor", -1, NULL,
74 ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
75 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
76 add_deprecated_alias ("xvideo-shm")
77 add_shortcut ("xcb-xv")
79 add_shortcut ("xvideo")
82 #define MAX_PICTURES (VOUT_MAX_PICTURES)
84 struct vout_display_sys_t
86 xcb_connection_t *conn;
87 vout_window_t *embed;/* VLC window */
89 xcb_cursor_t cursor; /* blank cursor */
90 xcb_window_t window; /* drawable X window */
91 xcb_gcontext_t gc; /* context to put images */
92 xcb_xv_port_t port; /* XVideo port */
93 uint32_t id; /* XVideo format */
94 uint16_t width; /* display width */
95 uint16_t height; /* display height */
96 uint32_t data_size; /* picture byte size (for non-SHM) */
97 bool shm; /* whether to use MIT-SHM */
98 bool visible; /* whether it makes sense to draw at all */
100 xcb_xv_query_image_attributes_reply_t *att;
101 picture_pool_t *pool; /* picture pool */
102 picture_resource_t resource[MAX_PICTURES];
105 static picture_t *Get (vout_display_t *);
106 static void Display (vout_display_t *, picture_t *);
107 static int Control (vout_display_t *, int, va_list);
108 static void Manage (vout_display_t *);
111 * Check that the X server supports the XVideo extension.
113 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
115 xcb_xv_query_extension_reply_t *r;
116 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
119 r = xcb_xv_query_extension_reply (conn, ck, NULL);
121 { /* We need XVideo 2.2 for PutImage */
122 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
124 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
129 msg_Dbg (vd, "XVideo extension too old (v%"PRIu8".%"PRIu8,
134 msg_Dbg (vd, "XVideo extension not available");
138 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
139 const xcb_xv_image_format_info_t *restrict f)
141 if (f->byte_order != ORDER && f->bpp != 8)
142 return 0; /* Argh! */
146 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
147 switch (f->num_planes)
154 return VLC_CODEC_RGB32;
158 return VLC_CODEC_RGB24;
162 return VLC_CODEC_RGB16;
164 return VLC_CODEC_RGB15;
168 return VLC_CODEC_RGB8;
173 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
175 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
176 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
179 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
180 if (f->u_sample_bits != f->v_sample_bits
181 || f->vhorz_u_period != f->vhorz_v_period
182 || f->vvert_u_period != f->vvert_v_period
183 || f->y_sample_bits != 8 || f->u_sample_bits != 8
184 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
186 switch (f->num_planes)
192 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
193 return VLC_CODEC_I444;
196 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
198 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
199 return VLC_CODEC_YUYV;
200 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
201 return VLC_CODEC_UYVY;
210 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
212 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
213 return VLC_CODEC_YV12;
214 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
215 return VLC_CODEC_I420;
221 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
223 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
224 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
225 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
226 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
227 "%"PRIu32"/%"PRIu32"/%"PRIu32,
228 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
229 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
230 msg_Warn (vd, " order: %.32s", f->vcomp_order);
237 static const xcb_xv_image_format_info_t *
238 FindFormat (vout_display_t *vd,
239 vlc_fourcc_t chroma, const video_format_t *fmt,
241 const xcb_xv_list_image_formats_reply_t *list,
242 xcb_xv_query_image_attributes_reply_t **restrict pa)
244 xcb_connection_t *conn = vd->sys->conn;
245 const xcb_xv_image_format_info_t *f, *end;
248 f = xcb_xv_list_image_formats_format (list);
250 f = (xcb_xv_image_format_info_t *) (list + 1);
252 end = f + xcb_xv_list_image_formats_format_length (list);
255 if (chroma != ParseFormat (vd, f))
258 /* VLC pads scanline to 16 pixels internally */
259 unsigned width = (fmt->i_width + 15) & ~15;
260 unsigned height = (fmt->i_height + 15) & ~15;
261 xcb_xv_query_image_attributes_reply_t *i;
262 i = xcb_xv_query_image_attributes_reply (conn,
263 xcb_xv_query_image_attributes (conn, port, f->id,
264 width, height), NULL);
268 if (i->width != width || i->height != height)
270 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
271 fmt->i_width, fmt->i_height,
272 i->width, i->height);
273 var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
274 if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
276 dialog_FatalWait (vd, _("Video acceleration not available"),
277 _("Your video output acceleration driver does not support "
278 "the required resolution: %ux%u pixels. The maximum "
279 "supported resolution is %"PRIu32"x%"PRIu32".\n"
280 "Video output acceleration will be disabled. However, "
281 "rendering videos with overly large resolution "
282 "may cause severe performance degration."),
283 width, height, i->width, i->height);
284 var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
297 * Probe the X server.
299 static int Open (vlc_object_t *obj)
301 vout_display_t *vd = (vout_display_t *)obj;
302 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
309 xcb_connection_t *conn;
310 const xcb_screen_t *screen;
312 p_sys->embed = GetWindow (vd, &conn, &screen, &depth, &p_sys->shm);
313 if (p_sys->embed == NULL)
323 if (!CheckXVideo (vd, conn))
325 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
329 p_sys->window = xcb_generate_id (conn);
331 /* Cache adaptors infos */
332 xcb_xv_query_adaptors_reply_t *adaptors =
333 xcb_xv_query_adaptors_reply (conn,
334 xcb_xv_query_adaptors (conn, p_sys->embed->xid), NULL);
335 if (adaptors == NULL)
338 int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
341 video_format_t fmt = vd->fmt;
342 bool found_adaptor = false;
344 xcb_xv_adaptor_info_iterator_t it;
345 for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
346 it.rem > 0 && !found_adaptor;
347 xcb_xv_adaptor_info_next (&it))
349 const xcb_xv_adaptor_info_t *a = it.data;
352 if (forced_adaptor != -1 && forced_adaptor != 0)
358 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
361 xcb_xv_list_image_formats_reply_t *r =
362 xcb_xv_list_image_formats_reply (conn,
363 xcb_xv_list_image_formats (conn, a->base_id), NULL);
367 /* Look for an image format */
368 const xcb_xv_image_format_info_t *xfmt = NULL;
369 const vlc_fourcc_t *chromas, chromas_default[] = {
378 if (vlc_fourcc_IsYUV (fmt.i_chroma))
379 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
381 chromas = chromas_default;
384 for (size_t i = 0; chromas[i]; i++)
389 if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
391 && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
393 msg_Dbg (vd, "skipping slow I420 format");
394 continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
397 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
400 p_sys->id = xfmt->id;
401 fmt.i_chroma = chroma;
402 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
404 fmt.i_rmask = xfmt->red_mask;
405 fmt.i_gmask = xfmt->green_mask;
406 fmt.i_bmask = xfmt->blue_mask;
412 if (xfmt == NULL) /* No acceptable image formats */
416 for (unsigned i = 0; i < a->num_ports; i++)
418 xcb_xv_port_t port = a->base_id + i;
419 xcb_xv_grab_port_reply_t *gr =
420 xcb_xv_grab_port_reply (conn,
421 xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
422 uint8_t result = gr ? gr->result : 0xff;
430 msg_Dbg (vd, "cannot grab port %"PRIu32, port);
432 continue; /* No usable port */
435 /* Found port - initialize selected format */
436 name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
439 msg_Dbg (vd, "using adaptor %s", name);
442 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
443 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
445 /* Look for an X11 visual, create a window */
446 xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
447 for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
449 if (f->depth != depth)
450 continue; /* this would fail anyway */
452 const uint32_t mask =
453 /* XCB_CW_EVENT_MASK */
454 XCB_EVENT_MASK_VISIBILITY_CHANGE;
457 c = xcb_create_window_checked (conn, f->depth, p_sys->window,
458 p_sys->embed->xid, 0, 0, 1, 1, 0,
459 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual,
460 XCB_CW_EVENT_MASK, &mask);
462 if (!CheckError (vd, conn, "cannot create X11 window", c))
464 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
465 " (depth: %"PRIu8")", f->visual, f->depth);
466 msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
470 xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
471 continue; /* No workable XVideo format (visual/depth) */
474 found_adaptor = true;
480 msg_Err (vd, "no available XVideo adaptor");
485 xcb_map_window (conn, p_sys->window);
487 vout_display_place_t place;
489 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
490 p_sys->width = place.width;
491 p_sys->height = place.height;
494 const uint32_t values[] = {
495 place.x, place.y, place.width, place.height };
496 xcb_configure_window (conn, p_sys->window,
497 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
498 XCB_CONFIG_WINDOW_WIDTH |
499 XCB_CONFIG_WINDOW_HEIGHT,
502 p_sys->visible = false;
504 /* Create graphic context */
505 p_sys->gc = xcb_generate_id (conn);
506 xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
507 msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
510 p_sys->cursor = CreateBlankCursor (conn, screen);
516 vout_display_info_t info = vd->info;
517 info.has_pictures_invalid = false;
519 /* Setup vout_display_t once everything is fine */
525 vd->display = Display;
526 vd->control = Control;
530 vout_display_SendEventFullscreen (vd, false);
531 unsigned width, height;
532 if (!GetWindowSize (p_sys->embed, conn, &width, &height))
533 vout_display_SendEventDisplaySize (vd, width, height, false);
544 * Disconnect from the X server.
546 static void Close (vlc_object_t *obj)
548 vout_display_t *vd = (vout_display_t *)obj;
549 vout_display_sys_t *p_sys = vd->sys;
553 for (unsigned i = 0; i < MAX_PICTURES; i++)
555 picture_resource_t *res = &p_sys->resource[i];
557 if (!res->p->p_pixels)
559 PictureResourceFree (res, NULL);
561 picture_pool_Delete (p_sys->pool);
565 xcb_disconnect (p_sys->conn);
566 vout_display_DeleteWindow (vd, p_sys->embed);
571 * Return a direct buffer
573 static picture_t *Get (vout_display_t *vd)
575 vout_display_sys_t *p_sys = vd->sys;
579 picture_t *pic = picture_New (vd->fmt.i_chroma, p_sys->att->width,
580 p_sys->att->height, 0);
584 memset (p_sys->resource, 0, sizeof(p_sys->resource));
586 const uint32_t *offsets =
587 xcb_xv_query_image_attributes_offsets (p_sys->att);
588 p_sys->data_size = p_sys->att->data_size;
591 picture_t *pic_array[MAX_PICTURES];
592 for (count = 0; count < MAX_PICTURES; count++)
594 picture_resource_t *res = &p_sys->resource[count];
596 for (int i = 0; i < pic->i_planes; i++)
598 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
599 res->p[i].i_pitch = pic->p[i].i_pitch;
601 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
602 p_sys->conn, p_sys->shm))
605 /* Allocate further planes as specified by XVideo */
606 /* We assume that offsets[0] is zero */
607 for (int i = 1; i < pic->i_planes; i++)
608 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
609 if (vd->fmt.i_chroma == VLC_CODEC_YV12)
610 { /* YVU: swap U and V planes */
611 uint8_t *buf = res->p[2].p_pixels;
612 res->p[2].p_pixels = res->p[1].p_pixels;
613 res->p[1].p_pixels = buf;
616 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
617 if (!pic_array[count])
619 PictureResourceFree (res, p_sys->conn);
620 memset (res, 0, sizeof(*res));
624 picture_Release (pic);
629 p_sys->pool = picture_pool_New (count, pic_array);
632 /* TODO release picture resources */
635 /* FIXME should also do it in case of error ? */
636 xcb_flush (p_sys->conn);
639 return picture_pool_Get (p_sys->pool);
643 * Sends an image to the X server.
645 static void Display (vout_display_t *vd, picture_t *pic)
647 vout_display_sys_t *p_sys = vd->sys;
648 xcb_shm_seg_t segment = pic->p_sys->segment;
649 xcb_void_cookie_t ck;
654 ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
655 p_sys->window, p_sys->gc, segment, p_sys->id, 0,
656 /* Src: */ vd->source.i_x_offset,
657 vd->source.i_y_offset,
658 vd->source.i_visible_width,
659 vd->source.i_visible_height,
660 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
661 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
662 pic->p->i_lines, false);
664 ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
665 p_sys->gc, p_sys->id,
666 vd->source.i_x_offset,
667 vd->source.i_y_offset,
668 vd->source.i_visible_width,
669 vd->source.i_visible_height,
670 0, 0, p_sys->width, p_sys->height,
671 pic->p->i_pitch / pic->p->i_pixel_pitch,
673 p_sys->data_size, pic->p->p_pixels);
675 /* Wait for reply. See x11.c for rationale. */
676 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
679 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
683 picture_Release (pic);
686 static int Control (vout_display_t *vd, int query, va_list ap)
688 vout_display_sys_t *p_sys = vd->sys;
692 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
694 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
695 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
698 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
699 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
700 case VOUT_DISPLAY_CHANGE_ZOOM:
701 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
702 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
704 const vout_display_cfg_t *cfg;
705 const video_format_t *source;
706 bool is_forced = false;
708 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
709 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
711 source = (const video_format_t *)va_arg (ap, const video_format_t *);
716 source = &vd->source;
717 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
718 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
719 is_forced = (bool)va_arg (ap, int);
723 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
725 && (cfg->display.width != vd->cfg->display.width
726 ||cfg->display.height != vd->cfg->display.height)
727 && vout_window_SetSize (p_sys->embed,
729 cfg->display.height))
732 vout_display_place_t place;
733 vout_display_PlacePicture (&place, source, cfg, false);
734 p_sys->width = place.width;
735 p_sys->height = place.height;
737 /* Move the picture within the window */
738 const uint32_t values[] = { place.x, place.y,
739 place.width, place.height, };
740 xcb_configure_window (p_sys->conn, p_sys->window,
741 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
742 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
744 xcb_flush (p_sys->conn);
747 case VOUT_DISPLAY_CHANGE_ON_TOP:
749 int on_top = (int)va_arg (ap, int);
750 return vout_window_SetOnTop (p_sys->embed, on_top);
753 /* Hide the mouse. It will be send when
754 * vout_display_t::info.b_hide_mouse is false */
755 case VOUT_DISPLAY_HIDE_MOUSE:
756 xcb_change_window_attributes (p_sys->conn, p_sys->embed->xid,
757 XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
759 case VOUT_DISPLAY_RESET_PICTURES:
762 msg_Err (vd, "Unknown request in XCB vout display");
767 static void Manage (vout_display_t *vd)
769 vout_display_sys_t *p_sys = vd->sys;
771 ManageEvent (vd, p_sys->conn, &p_sys->visible);