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_("XVideo output (XCB)"))
59 set_category (CAT_VIDEO)
60 set_subcategory (SUBCAT_VIDEO_VOUT)
61 set_capability ("vout display", 155)
62 set_callbacks (Open, Close)
64 add_string ("x11-display", NULL, NULL,
65 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
66 add_deprecated_alias ("xvideo-display")
67 add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
68 add_deprecated_alias ("xvideo-shm")
69 add_shortcut ("xcb-xv")
73 #define MAX_PICTURES (VOUT_MAX_PICTURES)
75 struct vout_display_sys_t
77 xcb_connection_t *conn;
78 xcb_xv_query_adaptors_reply_t *adaptors;
79 vout_window_t *embed;/* VLC window */
81 xcb_window_t window; /* drawable X window */
82 xcb_gcontext_t gc; /* context to put images */
83 xcb_xv_port_t port; /* XVideo port */
84 uint32_t id; /* XVideo format */
85 uint16_t width; /* display width */
86 uint16_t height; /* display height */
87 uint32_t data_size; /* picture byte size (for non-SHM) */
88 bool shm; /* whether to use MIT-SHM */
90 xcb_xv_query_image_attributes_reply_t *att;
91 picture_pool_t *pool; /* picture pool */
92 picture_resource_t resource[MAX_PICTURES];
95 static picture_t *Get (vout_display_t *);
96 static void Display (vout_display_t *, picture_t *);
97 static int Control (vout_display_t *, int, va_list);
98 static void Manage (vout_display_t *);
101 * Check that the X server supports the XVideo extension.
103 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
105 xcb_xv_query_extension_reply_t *r;
106 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
109 r = xcb_xv_query_extension_reply (conn, ck, NULL);
111 { /* We need XVideo 2.2 for PutImage */
112 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
114 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
119 msg_Dbg (vd, "XVideo extension too old (v%"PRIu8".%"PRIu8,
124 msg_Dbg (vd, "XVideo extension not available");
128 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
129 const xcb_xv_image_format_info_t *restrict f)
131 if (f->byte_order != ORDER && f->bpp != 8)
132 return 0; /* Argh! */
136 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
137 switch (f->num_planes)
144 return VLC_CODEC_RGB32;
148 return VLC_CODEC_RGB24;
152 return VLC_CODEC_RGB16;
154 return VLC_CODEC_RGB15;
158 return VLC_CODEC_RGB8;
163 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
165 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
166 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
169 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
170 if (f->u_sample_bits != f->v_sample_bits
171 || f->vhorz_u_period != f->vhorz_v_period
172 || f->vvert_u_period != f->vvert_v_period
173 || f->y_sample_bits != 8 || f->u_sample_bits != 8
174 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
176 switch (f->num_planes)
182 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
183 return VLC_CODEC_I444;
186 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
188 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
189 return VLC_CODEC_YUYV;
190 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
191 return VLC_CODEC_UYVY;
200 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
202 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
203 return VLC_CODEC_YV12;
204 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
205 return VLC_CODEC_I420;
211 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
213 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
214 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
215 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
216 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
217 "%"PRIu32"/%"PRIu32"/%"PRIu32,
218 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
219 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
220 msg_Warn (vd, " order: %.32s", f->vcomp_order);
227 static const xcb_xv_image_format_info_t *
228 FindFormat (vout_display_t *vd,
229 vlc_fourcc_t chroma, const video_format_t *fmt,
231 const xcb_xv_list_image_formats_reply_t *list,
232 xcb_xv_query_image_attributes_reply_t **restrict pa)
234 xcb_connection_t *conn = vd->sys->conn;
235 const xcb_xv_image_format_info_t *f, *end;
238 f = xcb_xv_list_image_formats_format (list);
240 f = (xcb_xv_image_format_info_t *) (list + 1);
242 end = f + xcb_xv_list_image_formats_format_length (list);
245 if (chroma != ParseFormat (vd, f))
248 xcb_xv_query_image_attributes_reply_t *i;
249 i = xcb_xv_query_image_attributes_reply (conn,
250 xcb_xv_query_image_attributes (conn, port, f->id,
251 fmt->i_width, fmt->i_height), NULL);
255 if (i->width != fmt->i_width
256 || i->height != fmt->i_height)
258 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
259 fmt->i_width, fmt->i_height,
260 i->width, i->height);
272 * Get a list of XVideo adaptors for a given window.
274 static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
275 xcb_connection_t *conn)
277 xcb_xv_query_adaptors_cookie_t ck;
279 ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
280 return xcb_xv_query_adaptors_reply (conn, ck, NULL);
284 * Probe the X server.
286 static int Open (vlc_object_t *obj)
288 vout_display_t *vd = (vout_display_t *)obj;
289 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
296 p_sys->conn = Connect (obj);
297 if (p_sys->conn == NULL)
303 if (!CheckXVideo (vd, p_sys->conn))
305 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
306 xcb_disconnect (p_sys->conn);
311 const xcb_screen_t *screen;
312 p_sys->embed = GetWindow (vd, p_sys->conn, &screen, &p_sys->shm);
313 if (p_sys->embed == NULL)
315 xcb_disconnect (p_sys->conn);
324 /* Cache adaptors infos */
325 p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
326 if (p_sys->adaptors == NULL)
330 video_format_t fmt = vd->fmt;
333 bool found_adaptor = false;
335 /* FIXME: check max image size */
336 xcb_xv_adaptor_info_iterator_t it;
337 for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
339 xcb_xv_adaptor_info_next (&it))
341 const xcb_xv_adaptor_info_t *a = it.data;
343 /* FIXME: Open() should fail if none of the ports are usable to VLC */
344 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
347 xcb_xv_list_image_formats_reply_t *r;
348 r = xcb_xv_list_image_formats_reply (p_sys->conn,
349 xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
353 const xcb_xv_image_format_info_t *xfmt;
356 const vlc_fourcc_t *chromas, chromas_default[] = {
363 if (vlc_fourcc_IsYUV (fmt.i_chroma))
364 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
366 chromas = chromas_default;
368 for (size_t i = 0; chromas[i]; i++)
370 vlc_fourcc_t chroma = chromas[i];
371 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
374 fmt.i_chroma = chroma;
382 /* TODO: grab port */
383 p_sys->port = a->base_id;
384 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
386 p_sys->id = xfmt->id;
387 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
388 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
390 fmt.i_rmask = xfmt->red_mask;
391 fmt.i_gmask = xfmt->green_mask;
392 fmt.i_bmask = xfmt->blue_mask;
395 if (xfmt->num_planes == 3
396 && !strcmp ((const char *)xfmt->vcomp_order, "YVU"))
397 fmt.i_chroma = VLC_CODEC_YV12;
399 found_adaptor = true;
404 msg_Err (vd, "no available XVideo adaptor");
411 const uint32_t mask =
412 /* XCB_CW_EVENT_MASK */
413 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
414 XCB_EVENT_MASK_POINTER_MOTION;
416 xcb_window_t window = xcb_generate_id (p_sys->conn);
418 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
419 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
420 XCB_WINDOW_CLASS_INPUT_OUTPUT,
422 XCB_CW_EVENT_MASK, &mask);
423 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
425 p_sys->window = window;
426 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
427 xcb_map_window (p_sys->conn, window);
429 vout_display_place_t place;
431 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
432 p_sys->width = place.width;
433 p_sys->height = place.height;
436 const uint32_t values[] = { place.x, place.y, place.width, place.height };
437 xcb_configure_window (p_sys->conn, p_sys->window,
438 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
439 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
443 /* Create graphic context */
444 p_sys->gc = xcb_generate_id (p_sys->conn);
445 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
446 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
452 vout_display_info_t info = vd->info;
453 info.has_pictures_invalid = false;
455 /* Setup vout_display_t once everything is fine */
461 vd->display = Display;
462 vd->control = Control;
466 unsigned width, height;
467 if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
468 vout_display_SendEventDisplaySize (vd, width, height);
469 vout_display_SendEventFullscreen (vd, false);
480 * Disconnect from the X server.
482 static void Close (vlc_object_t *obj)
484 vout_display_t *vd = (vout_display_t *)obj;
485 vout_display_sys_t *p_sys = vd->sys;
489 for (unsigned i = 0; i < MAX_PICTURES; i++)
491 picture_resource_t *res = &p_sys->resource[i];
493 if (!res->p->p_pixels)
495 PictureResourceFree (res, p_sys->conn);
497 picture_pool_Delete (p_sys->pool);
501 free (p_sys->adaptors);
502 vout_display_DeleteWindow (vd, p_sys->embed);
503 xcb_disconnect (p_sys->conn);
508 * Return a direct buffer
510 static picture_t *Get (vout_display_t *vd)
512 vout_display_sys_t *p_sys = vd->sys;
516 picture_t *pic = picture_New (vd->fmt.i_chroma,
517 p_sys->att->width, p_sys->att->height, 0);
521 memset (p_sys->resource, 0, sizeof(p_sys->resource));
523 const uint32_t *offsets =
524 xcb_xv_query_image_attributes_offsets (p_sys->att);
525 p_sys->data_size = p_sys->att->data_size;
528 picture_t *pic_array[MAX_PICTURES];
529 for (count = 0; count < MAX_PICTURES; count++)
531 picture_resource_t *res = &p_sys->resource[count];
533 for (int i = 0; i < pic->i_planes; i++)
535 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
536 res->p[i].i_pitch = pic->p[i].i_pitch;
538 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
539 p_sys->conn, p_sys->shm))
542 /* Allocate further planes as specified by XVideo */
543 /* We assume that offsets[0] is zero */
544 for (int i = 1; i < pic->i_planes; i++)
545 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
546 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
547 if (!pic_array[count])
549 PictureResourceFree (res, p_sys->conn);
550 memset (res, 0, sizeof(*res));
554 picture_Release (pic);
559 p_sys->pool = picture_pool_New (count, pic_array);
562 /* TODO release picture resources */
565 /* FIXME should also do it in case of error ? */
566 xcb_flush (p_sys->conn);
569 return picture_pool_Get (p_sys->pool);
573 * Sends an image to the X server.
575 static void Display (vout_display_t *vd, picture_t *pic)
577 vout_display_sys_t *p_sys = vd->sys;
578 xcb_shm_seg_t segment = pic->p_sys->segment;
581 xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
582 p_sys->gc, segment, p_sys->id, 0,
583 /* Src: */ vd->source.i_x_offset,
584 vd->source.i_y_offset,
585 vd->source.i_visible_width,
586 vd->source.i_visible_height,
587 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
588 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
589 pic->p->i_visible_lines, false);
591 xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
592 p_sys->gc, p_sys->id,
593 vd->source.i_x_offset,
594 vd->source.i_y_offset,
595 vd->source.i_visible_width,
596 vd->source.i_visible_height,
597 0, 0, p_sys->width, p_sys->height,
598 vd->source.i_width, vd->source.i_height,
599 p_sys->data_size, pic->p->p_pixels);
601 xcb_flush (p_sys->conn);
602 picture_Release (pic);
605 static int Control (vout_display_t *vd, int query, va_list ap)
607 vout_display_sys_t *p_sys = vd->sys;
611 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
613 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
614 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
617 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
618 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
619 case VOUT_DISPLAY_CHANGE_ZOOM:
620 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
621 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
623 const vout_display_cfg_t *cfg;
624 const video_format_t *source;
626 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
627 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
629 source = (const video_format_t *)va_arg (ap, const video_format_t *);
634 source = &vd->source;
635 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
639 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
640 && (cfg->display.width != vd->cfg->display.width
641 ||cfg->display.height != vd->cfg->display.height)
642 && vout_window_SetSize (p_sys->embed,
644 cfg->display.height))
647 vout_display_place_t place;
648 vout_display_PlacePicture (&place, source, cfg, false);
649 p_sys->width = place.width;
650 p_sys->height = place.height;
652 /* Move the picture within the window */
653 const uint32_t values[] = { place.x, place.y,
654 place.width, place.height, };
655 xcb_configure_window (p_sys->conn, p_sys->window,
656 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
657 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
659 xcb_flush (p_sys->conn);
662 case VOUT_DISPLAY_CHANGE_ON_TOP:
664 int on_top = (int)va_arg (ap, int);
665 return vout_window_SetOnTop (p_sys->embed, on_top);
670 /* Hide the mouse. It will be send when
671 * vout_display_t::info.b_hide_mouse is false */
672 VOUT_DISPLAY_HIDE_MOUSE,
674 case VOUT_DISPLAY_RESET_PICTURES:
677 msg_Err (vd, "Unknown request in XCB vout display");
682 static void Manage (vout_display_t *vd)
684 vout_display_sys_t *p_sys = vd->sys;
686 ManageEvent (vd, p_sys->conn, p_sys->window);