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;
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 (p_sys->adaptors);
337 xcb_xv_adaptor_info_next (&it))
339 const xcb_xv_adaptor_info_t *a = it.data;
341 /* FIXME: Open() should fail if none of the ports are usable to VLC */
342 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
345 xcb_xv_list_image_formats_reply_t *r;
346 r = xcb_xv_list_image_formats_reply (p_sys->conn,
347 xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
351 const xcb_xv_image_format_info_t *xfmt;
354 const vlc_fourcc_t *chromas, chromas_default[] = {
361 if (vlc_fourcc_IsYUV (fmt.i_chroma))
362 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
364 chromas = chromas_default;
366 for (size_t i = 0; chromas[i]; i++)
368 vlc_fourcc_t chroma = chromas[i];
369 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
372 fmt.i_chroma = chroma;
381 /* XXX: assume all of an adapter's ports have the same formats */
382 for (unsigned i = 0; i < a->num_ports; i++)
384 xcb_xv_port_t port = a->base_id + i;
385 xcb_xv_grab_port_reply_t *gr =
386 xcb_xv_grab_port_reply (p_sys->conn,
387 xcb_xv_grab_port (p_sys->conn, port,
388 XCB_CURRENT_TIME), NULL);
389 uint8_t result = gr ? gr->result : 0xff;
397 msg_Dbg (vd, "cannot grab port %"PRIu32, port);
402 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
404 p_sys->id = xfmt->id;
405 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
406 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
408 fmt.i_rmask = xfmt->red_mask;
409 fmt.i_gmask = xfmt->green_mask;
410 fmt.i_bmask = xfmt->blue_mask;
413 if (xfmt->num_planes == 3
414 && !strcmp ((const char *)xfmt->vcomp_order, "YVU"))
415 fmt.i_chroma = VLC_CODEC_YV12;
417 found_adaptor = true;
422 msg_Err (vd, "no available XVideo adaptor");
428 const uint32_t mask =
429 /* XCB_CW_EVENT_MASK */
430 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
431 XCB_EVENT_MASK_POINTER_MOTION;
433 xcb_window_t window = xcb_generate_id (p_sys->conn);
435 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
436 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
437 XCB_WINDOW_CLASS_INPUT_OUTPUT,
439 XCB_CW_EVENT_MASK, &mask);
440 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
442 p_sys->window = window;
443 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
444 xcb_map_window (p_sys->conn, window);
446 vout_display_place_t place;
448 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
449 p_sys->width = place.width;
450 p_sys->height = place.height;
453 const uint32_t values[] = { place.x, place.y, place.width, place.height };
454 xcb_configure_window (p_sys->conn, p_sys->window,
455 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
456 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
460 /* Create graphic context */
461 p_sys->gc = xcb_generate_id (p_sys->conn);
462 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
463 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
469 vout_display_info_t info = vd->info;
470 info.has_pictures_invalid = false;
472 /* Setup vout_display_t once everything is fine */
478 vd->display = Display;
479 vd->control = Control;
483 unsigned width, height;
484 if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
485 vout_display_SendEventDisplaySize (vd, width, height);
486 vout_display_SendEventFullscreen (vd, false);
497 * Disconnect from the X server.
499 static void Close (vlc_object_t *obj)
501 vout_display_t *vd = (vout_display_t *)obj;
502 vout_display_sys_t *p_sys = vd->sys;
506 for (unsigned i = 0; i < MAX_PICTURES; i++)
508 picture_resource_t *res = &p_sys->resource[i];
510 if (!res->p->p_pixels)
512 PictureResourceFree (res, p_sys->conn);
514 picture_pool_Delete (p_sys->pool);
518 free (p_sys->adaptors);
519 vout_display_DeleteWindow (vd, p_sys->embed);
520 xcb_disconnect (p_sys->conn);
525 * Return a direct buffer
527 static picture_t *Get (vout_display_t *vd)
529 vout_display_sys_t *p_sys = vd->sys;
533 picture_t *pic = picture_New (vd->fmt.i_chroma,
534 p_sys->att->width, p_sys->att->height, 0);
538 memset (p_sys->resource, 0, sizeof(p_sys->resource));
540 const uint32_t *offsets =
541 xcb_xv_query_image_attributes_offsets (p_sys->att);
542 p_sys->data_size = p_sys->att->data_size;
545 picture_t *pic_array[MAX_PICTURES];
546 for (count = 0; count < MAX_PICTURES; count++)
548 picture_resource_t *res = &p_sys->resource[count];
550 for (int i = 0; i < pic->i_planes; i++)
552 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
553 res->p[i].i_pitch = pic->p[i].i_pitch;
555 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
556 p_sys->conn, p_sys->shm))
559 /* Allocate further planes as specified by XVideo */
560 /* We assume that offsets[0] is zero */
561 for (int i = 1; i < pic->i_planes; i++)
562 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
563 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
564 if (!pic_array[count])
566 PictureResourceFree (res, p_sys->conn);
567 memset (res, 0, sizeof(*res));
571 picture_Release (pic);
576 p_sys->pool = picture_pool_New (count, pic_array);
579 /* TODO release picture resources */
582 /* FIXME should also do it in case of error ? */
583 xcb_flush (p_sys->conn);
586 return picture_pool_Get (p_sys->pool);
590 * Sends an image to the X server.
592 static void Display (vout_display_t *vd, picture_t *pic)
594 vout_display_sys_t *p_sys = vd->sys;
595 xcb_shm_seg_t segment = pic->p_sys->segment;
598 xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
599 p_sys->gc, segment, p_sys->id, 0,
600 /* Src: */ vd->source.i_x_offset,
601 vd->source.i_y_offset,
602 vd->source.i_visible_width,
603 vd->source.i_visible_height,
604 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
605 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
606 pic->p->i_visible_lines, false);
608 xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
609 p_sys->gc, p_sys->id,
610 vd->source.i_x_offset,
611 vd->source.i_y_offset,
612 vd->source.i_visible_width,
613 vd->source.i_visible_height,
614 0, 0, p_sys->width, p_sys->height,
615 vd->source.i_width, vd->source.i_height,
616 p_sys->data_size, pic->p->p_pixels);
618 xcb_flush (p_sys->conn);
619 picture_Release (pic);
622 static int Control (vout_display_t *vd, int query, va_list ap)
624 vout_display_sys_t *p_sys = vd->sys;
628 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
630 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
631 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
634 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
635 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
636 case VOUT_DISPLAY_CHANGE_ZOOM:
637 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
638 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
640 const vout_display_cfg_t *cfg;
641 const video_format_t *source;
643 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
644 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
646 source = (const video_format_t *)va_arg (ap, const video_format_t *);
651 source = &vd->source;
652 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
656 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
657 && (cfg->display.width != vd->cfg->display.width
658 ||cfg->display.height != vd->cfg->display.height)
659 && vout_window_SetSize (p_sys->embed,
661 cfg->display.height))
664 vout_display_place_t place;
665 vout_display_PlacePicture (&place, source, cfg, false);
666 p_sys->width = place.width;
667 p_sys->height = place.height;
669 /* Move the picture within the window */
670 const uint32_t values[] = { place.x, place.y,
671 place.width, place.height, };
672 xcb_configure_window (p_sys->conn, p_sys->window,
673 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
674 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
676 xcb_flush (p_sys->conn);
679 case VOUT_DISPLAY_CHANGE_ON_TOP:
681 int on_top = (int)va_arg (ap, int);
682 return vout_window_SetOnTop (p_sys->embed, on_top);
687 /* Hide the mouse. It will be send when
688 * vout_display_t::info.b_hide_mouse is false */
689 VOUT_DISPLAY_HIDE_MOUSE,
691 case VOUT_DISPLAY_RESET_PICTURES:
694 msg_Err (vd, "Unknown request in XCB vout display");
699 static void Manage (vout_display_t *vd)
701 vout_display_sys_t *p_sys = vd->sys;
703 ManageEvent (vd, p_sys->conn, p_sys->window);