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 SHM_TEXT N_("Use shared memory")
47 #define SHM_LONGTEXT N_( \
48 "Use shared memory to communicate between VLC and the X server.")
50 static int Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
57 set_shortname (N_("XVideo"))
58 set_description (N_("(Experimental) XVideo output"))
59 set_category (CAT_VIDEO)
60 set_subcategory (SUBCAT_VIDEO_VOUT)
61 set_capability ("vout display", 0)
62 set_callbacks (Open, Close)
64 add_string ("x11-display", NULL, NULL,
65 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
66 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
67 add_shortcut ("xcb-xv")
70 #define MAX_PICTURES (VOUT_MAX_PICTURES)
72 struct vout_display_sys_t
74 xcb_connection_t *conn;
75 xcb_xv_query_adaptors_reply_t *adaptors;
76 vout_window_t *embed;/* VLC window */
78 xcb_window_t window; /* drawable X window */
79 xcb_gcontext_t gc; /* context to put images */
80 xcb_xv_port_t port; /* XVideo port */
81 uint32_t id; /* XVideo format */
82 uint16_t width; /* display width */
83 uint16_t height; /* display height */
84 uint32_t data_size; /* picture byte size (for non-SHM) */
85 bool shm; /* whether to use MIT-SHM */
87 xcb_xv_query_image_attributes_reply_t *att;
88 picture_pool_t *pool; /* picture pool */
89 picture_resource_t resource[MAX_PICTURES];
92 static picture_t *Get (vout_display_t *);
93 static void Display (vout_display_t *, picture_t *);
94 static int Control (vout_display_t *, int, va_list);
95 static void Manage (vout_display_t *);
98 * Check that the X server supports the XVideo extension.
100 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
102 xcb_xv_query_extension_reply_t *r;
103 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
106 r = xcb_xv_query_extension_reply (conn, ck, NULL);
108 { /* We need XVideo 2.2 for PutImage */
109 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
111 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
116 msg_Dbg (vd, "XVideo extension too old (v%"PRIu8".%"PRIu8,
121 msg_Dbg (vd, "XVideo extension not available");
125 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
126 const xcb_xv_image_format_info_t *restrict f)
128 if (f->byte_order != ORDER && f->bpp != 8)
129 return 0; /* Argh! */
133 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
134 switch (f->num_planes)
141 return VLC_CODEC_RGB32;
145 return VLC_CODEC_RGB24;
149 return VLC_CODEC_RGB16;
151 return VLC_CODEC_RGB15;
155 return VLC_CODEC_RGB8;
160 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
162 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
163 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
166 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
167 if (f->u_sample_bits != f->v_sample_bits
168 || f->vhorz_u_period != f->vhorz_v_period
169 || f->vvert_u_period != f->vvert_v_period
170 || f->y_sample_bits != 8 || f->u_sample_bits != 8
171 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
173 switch (f->num_planes)
179 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
180 return VLC_CODEC_I444;
183 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
185 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
186 return VLC_CODEC_YUYV;
187 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
188 return VLC_CODEC_UYVY;
197 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
199 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
200 return VLC_CODEC_YV12;
201 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
202 return VLC_CODEC_I420;
208 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
210 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
211 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
212 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
213 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
214 "%"PRIu32"/%"PRIu32"/%"PRIu32,
215 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
216 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
217 msg_Warn (vd, " order: %.32s", f->vcomp_order);
224 static const xcb_xv_image_format_info_t *
225 FindFormat (vout_display_t *vd,
226 vlc_fourcc_t chroma, const video_format_t *fmt,
228 const xcb_xv_list_image_formats_reply_t *list,
229 xcb_xv_query_image_attributes_reply_t **restrict pa)
231 xcb_connection_t *conn = vd->sys->conn;
232 const xcb_xv_image_format_info_t *f, *end;
235 f = xcb_xv_list_image_formats_format (list);
237 f = (xcb_xv_image_format_info_t *) (list + 1);
239 end = f + xcb_xv_list_image_formats_format_length (list);
242 if (chroma != ParseFormat (vd, f))
245 xcb_xv_query_image_attributes_reply_t *i;
246 i = xcb_xv_query_image_attributes_reply (conn,
247 xcb_xv_query_image_attributes (conn, port, f->id,
248 fmt->i_width, fmt->i_height), NULL);
252 if (i->width != fmt->i_width
253 || i->height != fmt->i_height)
255 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
256 fmt->i_width, fmt->i_height,
257 i->width, i->height);
269 * Get a list of XVideo adaptors for a given window.
271 static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
272 xcb_connection_t *conn)
274 xcb_xv_query_adaptors_cookie_t ck;
276 ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
277 return xcb_xv_query_adaptors_reply (conn, ck, NULL);
281 * Probe the X server.
283 static int Open (vlc_object_t *obj)
285 vout_display_t *vd = (vout_display_t *)obj;
286 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
293 p_sys->conn = Connect (obj);
294 if (p_sys->conn == NULL)
300 if (!CheckXVideo (vd, p_sys->conn))
302 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
303 xcb_disconnect (p_sys->conn);
308 const xcb_screen_t *screen;
309 p_sys->embed = GetWindow (vd, p_sys->conn, &screen, &p_sys->shm);
310 if (p_sys->embed == NULL)
312 xcb_disconnect (p_sys->conn);
317 /* Cache adaptors infos */
318 p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
319 if (p_sys->adaptors == NULL)
323 video_format_t fmt = vd->fmt;
327 bool found_adaptor = false;
329 /* FIXME: check max image size */
330 xcb_xv_adaptor_info_iterator_t it;
331 for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
333 xcb_xv_adaptor_info_next (&it))
335 const xcb_xv_adaptor_info_t *a = it.data;
337 /* FIXME: Open() should fail if none of the ports are usable to VLC */
338 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
341 xcb_xv_list_image_formats_reply_t *r;
342 r = xcb_xv_list_image_formats_reply (p_sys->conn,
343 xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
347 const xcb_xv_image_format_info_t *xfmt;
349 /* Video chroma in preference order */
350 const vlc_fourcc_t chromas[] = {
356 for (size_t i = 0; i < sizeof (chromas) / sizeof (chromas[0]); i++)
358 vlc_fourcc_t chroma = chromas[i];
359 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
362 fmt.i_chroma = chroma;
370 /* TODO: grab port */
371 p_sys->port = a->base_id;
372 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
374 p_sys->id = xfmt->id;
375 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
376 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
378 fmt.i_rmask = xfmt->red_mask;
379 fmt.i_gmask = xfmt->green_mask;
380 fmt.i_bmask = xfmt->blue_mask;
383 if (xfmt->num_planes == 3
384 && !strcmp ((const char *)xfmt->vcomp_order, "YVU"))
385 fmt.i_chroma = VLC_CODEC_YV12;
387 found_adaptor = true;
392 msg_Err (vd, "no available XVideo adaptor");
399 const uint32_t mask =
400 /* XCB_CW_EVENT_MASK */
401 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
402 XCB_EVENT_MASK_POINTER_MOTION;
404 xcb_window_t window = xcb_generate_id (p_sys->conn);
406 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
407 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
408 XCB_WINDOW_CLASS_INPUT_OUTPUT,
410 XCB_CW_EVENT_MASK, &mask);
411 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
413 p_sys->window = window;
414 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
415 xcb_map_window (p_sys->conn, window);
417 vout_display_place_t place;
419 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
420 p_sys->width = place.width;
421 p_sys->height = place.height;
424 const uint32_t values[] = { place.x, place.y, place.width, place.height };
425 xcb_configure_window (p_sys->conn, p_sys->window,
426 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
427 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
431 /* Create graphic context */
432 p_sys->gc = xcb_generate_id (p_sys->conn);
433 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
434 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
440 vout_display_info_t info = vd->info;
441 info.has_pictures_invalid = false;
443 /* Setup vout_display_t once everything is fine */
449 vd->display = Display;
450 vd->control = Control;
454 unsigned width, height;
455 if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
456 vout_display_SendEventDisplaySize (vd, width, height);
457 vout_display_SendEventFullscreen (vd, false);
468 * Disconnect from the X server.
470 static void Close (vlc_object_t *obj)
472 vout_display_t *vd = (vout_display_t *)obj;
473 vout_display_sys_t *p_sys = vd->sys;
477 for (unsigned i = 0; i < MAX_PICTURES; i++)
479 picture_resource_t *res = &p_sys->resource[i];
481 if (!res->p->p_pixels)
483 PictureResourceFree (res, p_sys->conn);
485 picture_pool_Delete (p_sys->pool);
489 free (p_sys->adaptors);
490 vout_display_DeleteWindow (vd, p_sys->embed);
491 xcb_disconnect (p_sys->conn);
496 * Return a direct buffer
498 static picture_t *Get (vout_display_t *vd)
500 vout_display_sys_t *p_sys = vd->sys;
504 picture_t *pic = picture_New (vd->fmt.i_chroma,
505 p_sys->att->width, p_sys->att->height, 0);
509 memset (p_sys->resource, 0, sizeof(p_sys->resource));
511 const uint32_t *offsets =
512 xcb_xv_query_image_attributes_offsets (p_sys->att);
513 p_sys->data_size = p_sys->att->data_size;
516 picture_t *pic_array[MAX_PICTURES];
517 for (count = 0; count < MAX_PICTURES; count++)
519 picture_resource_t *res = &p_sys->resource[count];
521 for (int i = 0; i < pic->i_planes; i++)
523 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
524 res->p[i].i_pitch = pic->p[i].i_pitch;
526 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
527 p_sys->conn, p_sys->shm))
530 /* Allocate further planes as specified by XVideo */
531 /* We assume that offsets[0] is zero */
532 for (int i = 1; i < pic->i_planes; i++)
533 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
534 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
535 if (!pic_array[count])
537 PictureResourceFree (res, p_sys->conn);
538 memset (res, 0, sizeof(*res));
542 picture_Release (pic);
547 p_sys->pool = picture_pool_New (count, pic_array);
550 /* TODO release picture resources */
553 /* FIXME should also do it in case of error ? */
554 xcb_flush (p_sys->conn);
557 return picture_pool_Get (p_sys->pool);
561 * Sends an image to the X server.
563 static void Display (vout_display_t *vd, picture_t *pic)
565 vout_display_sys_t *p_sys = vd->sys;
566 xcb_shm_seg_t segment = pic->p_sys->segment;
569 xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
570 p_sys->gc, segment, p_sys->id, 0,
571 /* Src: */ vd->source.i_x_offset,
572 vd->source.i_y_offset,
573 vd->source.i_visible_width,
574 vd->source.i_visible_height,
575 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
576 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
577 pic->p->i_lines, false);
579 xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
580 p_sys->gc, p_sys->id,
581 vd->source.i_x_offset,
582 vd->source.i_y_offset,
583 vd->source.i_visible_width,
584 vd->source.i_visible_height,
585 0, 0, p_sys->width, p_sys->height,
586 vd->source.i_width, vd->source.i_height,
587 p_sys->data_size, pic->p->p_pixels);
589 xcb_flush (p_sys->conn);
590 picture_Release (pic);
593 static int Control (vout_display_t *vd, int query, va_list ap)
595 vout_display_sys_t *p_sys = vd->sys;
599 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
600 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
601 case VOUT_DISPLAY_CHANGE_ZOOM:
602 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
603 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
605 const vout_display_cfg_t *cfg;
606 const video_format_t *source;
608 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
609 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
611 source = (const video_format_t *)va_arg (ap, const video_format_t *);
616 source = &vd->source;
617 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
621 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
622 && (cfg->display.width != vd->cfg->display.width
623 ||cfg->display.height != vd->cfg->display.height)
624 && vout_window_SetSize (p_sys->embed,
626 cfg->display.height))
629 vout_display_place_t place;
630 vout_display_PlacePicture (&place, source, cfg, false);
631 p_sys->width = place.width;
632 p_sys->height = place.height;
634 /* Move the picture within the window */
635 const uint32_t values[] = { place.x, place.y,
636 place.width, place.height, };
637 xcb_configure_window (p_sys->conn, p_sys->window,
638 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
639 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
641 xcb_flush (p_sys->conn);
644 case VOUT_DISPLAY_CHANGE_ON_TOP:
646 int on_top = (int)va_arg (ap, int);
647 return vout_window_SetOnTop (p_sys->embed, on_top);
652 /* Hide the mouse. It will be send when
653 * vout_display_t::info.b_hide_mouse is false */
654 VOUT_DISPLAY_HIDE_MOUSE,
656 /* Ask the module to acknowledge/refuse the fullscreen state change after
657 * being requested (externaly or by VOUT_DISPLAY_EVENT_FULLSCREEN */
658 VOUT_DISPLAY_CHANGE_FULLSCREEN, /* const vout_display_cfg_t *p_cfg */
660 case VOUT_DISPLAY_RESET_PICTURES:
663 msg_Err (vd, "Unknown request in XCB vout display");
668 static void Manage (vout_display_t *vd)
670 vout_display_sys_t *p_sys = vd->sys;
672 ManageEvent (vd, p_sys->conn, p_sys->window);