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.0
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 Lesser 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>
41 #define DISPLAY_TEXT N_("X11 display")
42 #define DISPLAY_LONGTEXT N_( \
43 "X11 hardware display to use. By default, VLC will " \
44 "use the value of the DISPLAY environment variable.")
46 #define ADAPTOR_TEXT N_("XVideo adaptor number")
47 #define ADAPTOR_LONGTEXT N_( \
48 "XVideo hardware adaptor to use. By default, VLC will " \
49 "use the first functional adaptor.")
51 #define SHM_TEXT N_("Use shared memory")
52 #define SHM_LONGTEXT N_( \
53 "Use shared memory to communicate between VLC and the X server.")
55 static int Open (vlc_object_t *);
56 static void Close (vlc_object_t *);
62 set_shortname (N_("XVideo"))
63 set_description (N_("XVideo output (XCB)"))
64 set_category (CAT_VIDEO)
65 set_subcategory (SUBCAT_VIDEO_VOUT)
66 set_capability ("vout display", 155)
67 set_callbacks (Open, Close)
69 add_string ("x11-display", NULL, NULL,
70 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
71 add_deprecated_alias ("xvideo-display")
72 add_integer ("xvideo-adaptor", -1, NULL,
73 ADAPTOR_TEXT, ADAPTOR_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")
78 add_shortcut ("xvideo")
81 #define MAX_PICTURES (VOUT_MAX_PICTURES)
83 struct vout_display_sys_t
85 xcb_connection_t *conn;
86 vout_window_t *embed;/* VLC window */
88 xcb_window_t window; /* drawable X window */
89 xcb_gcontext_t gc; /* context to put images */
90 xcb_xv_port_t port; /* XVideo port */
91 uint32_t id; /* XVideo format */
92 uint16_t width; /* display width */
93 uint16_t height; /* display height */
94 uint32_t data_size; /* picture byte size (for non-SHM) */
95 bool shm; /* whether to use MIT-SHM */
97 xcb_xv_query_image_attributes_reply_t *att;
98 picture_pool_t *pool; /* picture pool */
99 picture_resource_t resource[MAX_PICTURES];
102 static picture_t *Get (vout_display_t *);
103 static void Display (vout_display_t *, picture_t *);
104 static int Control (vout_display_t *, int, va_list);
105 static void Manage (vout_display_t *);
108 * Check that the X server supports the XVideo extension.
110 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
112 xcb_xv_query_extension_reply_t *r;
113 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
116 r = xcb_xv_query_extension_reply (conn, ck, NULL);
118 { /* We need XVideo 2.2 for PutImage */
119 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
121 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
126 msg_Dbg (vd, "XVideo extension too old (v%"PRIu8".%"PRIu8,
131 msg_Dbg (vd, "XVideo extension not available");
135 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
136 const xcb_xv_image_format_info_t *restrict f)
138 if (f->byte_order != ORDER && f->bpp != 8)
139 return 0; /* Argh! */
143 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
144 switch (f->num_planes)
151 return VLC_CODEC_RGB32;
155 return VLC_CODEC_RGB24;
159 return VLC_CODEC_RGB16;
161 return VLC_CODEC_RGB15;
165 return VLC_CODEC_RGB8;
170 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
172 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
173 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
176 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
177 if (f->u_sample_bits != f->v_sample_bits
178 || f->vhorz_u_period != f->vhorz_v_period
179 || f->vvert_u_period != f->vvert_v_period
180 || f->y_sample_bits != 8 || f->u_sample_bits != 8
181 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
183 switch (f->num_planes)
189 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
190 return VLC_CODEC_I444;
193 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
195 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
196 return VLC_CODEC_YUYV;
197 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
198 return VLC_CODEC_UYVY;
207 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
209 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
210 return VLC_CODEC_YV12;
211 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
212 return VLC_CODEC_I420;
218 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
220 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
221 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
222 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
223 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
224 "%"PRIu32"/%"PRIu32"/%"PRIu32,
225 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
226 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
227 msg_Warn (vd, " order: %.32s", f->vcomp_order);
234 static const xcb_xv_image_format_info_t *
235 FindFormat (vout_display_t *vd,
236 vlc_fourcc_t chroma, const video_format_t *fmt,
238 const xcb_xv_list_image_formats_reply_t *list,
239 xcb_xv_query_image_attributes_reply_t **restrict pa)
241 xcb_connection_t *conn = vd->sys->conn;
242 const xcb_xv_image_format_info_t *f, *end;
245 f = xcb_xv_list_image_formats_format (list);
247 f = (xcb_xv_image_format_info_t *) (list + 1);
249 end = f + xcb_xv_list_image_formats_format_length (list);
252 if (chroma != ParseFormat (vd, f))
255 xcb_xv_query_image_attributes_reply_t *i;
256 i = xcb_xv_query_image_attributes_reply (conn,
257 xcb_xv_query_image_attributes (conn, port, f->id,
258 fmt->i_width, fmt->i_height), NULL);
262 if (i->width != fmt->i_width
263 || i->height != fmt->i_height)
265 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
266 fmt->i_width, fmt->i_height,
267 i->width, i->height);
279 * Probe the X server.
281 static int Open (vlc_object_t *obj)
283 vout_display_t *vd = (vout_display_t *)obj;
284 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
291 xcb_connection_t *conn = Connect (obj);
299 if (!CheckXVideo (vd, conn))
301 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
302 xcb_disconnect (conn);
307 const xcb_screen_t *screen;
308 p_sys->embed = GetWindow (vd, conn, &screen, &p_sys->shm);
309 if (p_sys->embed == NULL)
311 xcb_disconnect (conn);
320 /* Cache adaptors infos */
321 xcb_xv_query_adaptors_reply_t *adaptors =
322 xcb_xv_query_adaptors_reply (conn,
323 xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
324 if (adaptors == NULL)
327 int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
330 video_format_t fmt = vd->fmt;
331 bool found_adaptor = false;
333 /* FIXME: check max image size */
334 xcb_xv_adaptor_info_iterator_t it;
335 for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
337 xcb_xv_adaptor_info_next (&it))
339 const xcb_xv_adaptor_info_t *a = it.data;
341 if (forced_adaptor != -1 && forced_adaptor != 0)
347 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
350 xcb_xv_list_image_formats_reply_t *r =
351 xcb_xv_list_image_formats_reply (conn,
352 xcb_xv_list_image_formats (conn, a->base_id), NULL);
356 const xcb_xv_image_format_info_t *xfmt;
359 const vlc_fourcc_t *chromas, chromas_default[] = {
368 if (vlc_fourcc_IsYUV (fmt.i_chroma))
369 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
371 chromas = chromas_default;
373 for (size_t i = 0; chromas[i]; i++)
375 vlc_fourcc_t chroma = chromas[i];
376 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
379 fmt.i_chroma = chroma;
388 /* XXX: assume all of an adapter's ports have the same formats */
389 for (unsigned i = 0; i < a->num_ports; i++)
391 xcb_xv_port_t port = a->base_id + i;
392 xcb_xv_grab_port_reply_t *gr =
393 xcb_xv_grab_port_reply (conn,
394 xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
395 uint8_t result = gr ? gr->result : 0xff;
403 msg_Dbg (vd, "cannot grab port %"PRIu32, port);
408 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
410 p_sys->id = xfmt->id;
411 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
412 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
414 fmt.i_rmask = xfmt->red_mask;
415 fmt.i_gmask = xfmt->green_mask;
416 fmt.i_bmask = xfmt->blue_mask;
419 found_adaptor = true;
425 msg_Err (vd, "no available XVideo adaptor");
431 const uint32_t mask =
432 /* XCB_CW_EVENT_MASK */
433 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
434 XCB_EVENT_MASK_POINTER_MOTION;
436 xcb_window_t window = xcb_generate_id (conn);
438 c = xcb_create_window_checked (conn, screen->root_depth, window,
439 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
440 XCB_WINDOW_CLASS_INPUT_OUTPUT,
442 XCB_CW_EVENT_MASK, &mask);
443 if (CheckError (vd, conn, "cannot create X11 window", c))
445 p_sys->window = window;
446 msg_Dbg (vd, "using X11 window %08"PRIx32, window);
447 xcb_map_window (conn, window);
449 vout_display_place_t place;
451 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
452 p_sys->width = place.width;
453 p_sys->height = place.height;
456 const uint32_t values[] = { place.x, place.y, place.width, place.height };
457 xcb_configure_window (conn, window,
458 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
459 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
463 /* Create graphic context */
464 p_sys->gc = xcb_generate_id (conn);
465 xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
466 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
472 vout_display_info_t info = vd->info;
473 info.has_pictures_invalid = false;
475 /* Setup vout_display_t once everything is fine */
481 vd->display = Display;
482 vd->control = Control;
486 unsigned width, height;
487 if (!GetWindowSize (p_sys->embed, conn, &width, &height))
488 vout_display_SendEventDisplaySize (vd, width, height);
489 vout_display_SendEventFullscreen (vd, false);
500 * Disconnect from the X server.
502 static void Close (vlc_object_t *obj)
504 vout_display_t *vd = (vout_display_t *)obj;
505 vout_display_sys_t *p_sys = vd->sys;
509 for (unsigned i = 0; i < MAX_PICTURES; i++)
511 picture_resource_t *res = &p_sys->resource[i];
513 if (!res->p->p_pixels)
515 PictureResourceFree (res, p_sys->conn);
517 picture_pool_Delete (p_sys->pool);
521 vout_display_DeleteWindow (vd, p_sys->embed);
522 xcb_disconnect (p_sys->conn);
527 * Return a direct buffer
529 static picture_t *Get (vout_display_t *vd)
531 vout_display_sys_t *p_sys = vd->sys;
535 picture_t *pic = picture_New (vd->fmt.i_chroma,
536 p_sys->att->width, p_sys->att->height, 0);
540 memset (p_sys->resource, 0, sizeof(p_sys->resource));
542 const uint32_t *offsets =
543 xcb_xv_query_image_attributes_offsets (p_sys->att);
544 p_sys->data_size = p_sys->att->data_size;
547 picture_t *pic_array[MAX_PICTURES];
548 for (count = 0; count < MAX_PICTURES; count++)
550 picture_resource_t *res = &p_sys->resource[count];
552 for (int i = 0; i < pic->i_planes; i++)
554 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
555 res->p[i].i_pitch = pic->p[i].i_pitch;
557 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
558 p_sys->conn, p_sys->shm))
561 /* Allocate further planes as specified by XVideo */
562 /* We assume that offsets[0] is zero */
563 for (int i = 1; i < pic->i_planes; i++)
564 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
565 if (vd->fmt.i_chroma == VLC_CODEC_YV12)
566 { /* YVU: swap U and V planes */
567 uint8_t *buf = res->p[2].p_pixels;
568 res->p[2].p_pixels = res->p[1].p_pixels;
569 res->p[1].p_pixels = buf;
572 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
573 if (!pic_array[count])
575 PictureResourceFree (res, p_sys->conn);
576 memset (res, 0, sizeof(*res));
580 picture_Release (pic);
585 p_sys->pool = picture_pool_New (count, pic_array);
588 /* TODO release picture resources */
591 /* FIXME should also do it in case of error ? */
592 xcb_flush (p_sys->conn);
595 return picture_pool_Get (p_sys->pool);
599 * Sends an image to the X server.
601 static void Display (vout_display_t *vd, picture_t *pic)
603 vout_display_sys_t *p_sys = vd->sys;
604 xcb_shm_seg_t segment = pic->p_sys->segment;
605 xcb_void_cookie_t ck;
608 ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
609 p_sys->window, p_sys->gc, segment, p_sys->id, 0,
610 /* Src: */ vd->source.i_x_offset,
611 vd->source.i_y_offset,
612 vd->source.i_visible_width,
613 vd->source.i_visible_height,
614 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
615 /* Memory: */ vd->source.i_width,
616 pic->p->i_visible_lines, false);
618 ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
619 p_sys->gc, p_sys->id,
620 vd->source.i_x_offset,
621 vd->source.i_y_offset,
622 vd->source.i_visible_width,
623 vd->source.i_visible_height,
624 0, 0, p_sys->width, p_sys->height,
625 vd->source.i_width, vd->source.i_height,
626 p_sys->data_size, pic->p->p_pixels);
628 /* Wait for reply. See x11.c for rationale. */
629 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
632 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
636 picture_Release (pic);
639 static int Control (vout_display_t *vd, int query, va_list ap)
641 vout_display_sys_t *p_sys = vd->sys;
645 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
647 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
648 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
651 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
652 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
653 case VOUT_DISPLAY_CHANGE_ZOOM:
654 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
655 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
657 const vout_display_cfg_t *cfg;
658 const video_format_t *source;
660 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
661 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
663 source = (const video_format_t *)va_arg (ap, const video_format_t *);
668 source = &vd->source;
669 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
673 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
674 && (cfg->display.width != vd->cfg->display.width
675 ||cfg->display.height != vd->cfg->display.height)
676 && vout_window_SetSize (p_sys->embed,
678 cfg->display.height))
681 vout_display_place_t place;
682 vout_display_PlacePicture (&place, source, cfg, false);
683 p_sys->width = place.width;
684 p_sys->height = place.height;
686 /* Move the picture within the window */
687 const uint32_t values[] = { place.x, place.y,
688 place.width, place.height, };
689 xcb_configure_window (p_sys->conn, p_sys->window,
690 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
691 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
693 xcb_flush (p_sys->conn);
696 case VOUT_DISPLAY_CHANGE_ON_TOP:
698 int on_top = (int)va_arg (ap, int);
699 return vout_window_SetOnTop (p_sys->embed, on_top);
704 /* Hide the mouse. It will be send when
705 * vout_display_t::info.b_hide_mouse is false */
706 VOUT_DISPLAY_HIDE_MOUSE,
708 case VOUT_DISPLAY_RESET_PICTURES:
711 msg_Err (vd, "Unknown request in XCB vout display");
716 static void Manage (vout_display_t *vd)
718 vout_display_sys_t *p_sys = vd->sys;
720 ManageEvent (vd, p_sys->conn, p_sys->window);