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;
350 const vlc_fourcc_t chromas_default[] = {
357 if (vlc_fourcc_IsYUV (fmt.i_chroma))
358 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
360 chromas = chromas_default;
362 for (size_t i = 0; chromas[i]; i++)
364 vlc_fourcc_t chroma = chromas[i];
365 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
368 fmt.i_chroma = chroma;
376 /* TODO: grab port */
377 p_sys->port = a->base_id;
378 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
380 p_sys->id = xfmt->id;
381 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
382 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
384 fmt.i_rmask = xfmt->red_mask;
385 fmt.i_gmask = xfmt->green_mask;
386 fmt.i_bmask = xfmt->blue_mask;
389 if (xfmt->num_planes == 3
390 && !strcmp ((const char *)xfmt->vcomp_order, "YVU"))
391 fmt.i_chroma = VLC_CODEC_YV12;
393 found_adaptor = true;
398 msg_Err (vd, "no available XVideo adaptor");
405 const uint32_t mask =
406 /* XCB_CW_EVENT_MASK */
407 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
408 XCB_EVENT_MASK_POINTER_MOTION;
410 xcb_window_t window = xcb_generate_id (p_sys->conn);
412 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
413 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
414 XCB_WINDOW_CLASS_INPUT_OUTPUT,
416 XCB_CW_EVENT_MASK, &mask);
417 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
419 p_sys->window = window;
420 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
421 xcb_map_window (p_sys->conn, window);
423 vout_display_place_t place;
425 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
426 p_sys->width = place.width;
427 p_sys->height = place.height;
430 const uint32_t values[] = { place.x, place.y, place.width, place.height };
431 xcb_configure_window (p_sys->conn, p_sys->window,
432 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
433 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
437 /* Create graphic context */
438 p_sys->gc = xcb_generate_id (p_sys->conn);
439 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
440 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
446 vout_display_info_t info = vd->info;
447 info.has_pictures_invalid = false;
449 /* Setup vout_display_t once everything is fine */
455 vd->display = Display;
456 vd->control = Control;
460 unsigned width, height;
461 if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
462 vout_display_SendEventDisplaySize (vd, width, height);
463 vout_display_SendEventFullscreen (vd, false);
474 * Disconnect from the X server.
476 static void Close (vlc_object_t *obj)
478 vout_display_t *vd = (vout_display_t *)obj;
479 vout_display_sys_t *p_sys = vd->sys;
483 for (unsigned i = 0; i < MAX_PICTURES; i++)
485 picture_resource_t *res = &p_sys->resource[i];
487 if (!res->p->p_pixels)
489 PictureResourceFree (res, p_sys->conn);
491 picture_pool_Delete (p_sys->pool);
495 free (p_sys->adaptors);
496 vout_display_DeleteWindow (vd, p_sys->embed);
497 xcb_disconnect (p_sys->conn);
502 * Return a direct buffer
504 static picture_t *Get (vout_display_t *vd)
506 vout_display_sys_t *p_sys = vd->sys;
510 picture_t *pic = picture_New (vd->fmt.i_chroma,
511 p_sys->att->width, p_sys->att->height, 0);
515 memset (p_sys->resource, 0, sizeof(p_sys->resource));
517 const uint32_t *offsets =
518 xcb_xv_query_image_attributes_offsets (p_sys->att);
519 p_sys->data_size = p_sys->att->data_size;
522 picture_t *pic_array[MAX_PICTURES];
523 for (count = 0; count < MAX_PICTURES; count++)
525 picture_resource_t *res = &p_sys->resource[count];
527 for (int i = 0; i < pic->i_planes; i++)
529 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
530 res->p[i].i_pitch = pic->p[i].i_pitch;
532 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
533 p_sys->conn, p_sys->shm))
536 /* Allocate further planes as specified by XVideo */
537 /* We assume that offsets[0] is zero */
538 for (int i = 1; i < pic->i_planes; i++)
539 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
540 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
541 if (!pic_array[count])
543 PictureResourceFree (res, p_sys->conn);
544 memset (res, 0, sizeof(*res));
548 picture_Release (pic);
553 p_sys->pool = picture_pool_New (count, pic_array);
556 /* TODO release picture resources */
559 /* FIXME should also do it in case of error ? */
560 xcb_flush (p_sys->conn);
563 return picture_pool_Get (p_sys->pool);
567 * Sends an image to the X server.
569 static void Display (vout_display_t *vd, picture_t *pic)
571 vout_display_sys_t *p_sys = vd->sys;
572 xcb_shm_seg_t segment = pic->p_sys->segment;
575 xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
576 p_sys->gc, segment, p_sys->id, 0,
577 /* Src: */ vd->source.i_x_offset,
578 vd->source.i_y_offset,
579 vd->source.i_visible_width,
580 vd->source.i_visible_height,
581 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
582 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
583 pic->p->i_lines, false);
585 xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
586 p_sys->gc, p_sys->id,
587 vd->source.i_x_offset,
588 vd->source.i_y_offset,
589 vd->source.i_visible_width,
590 vd->source.i_visible_height,
591 0, 0, p_sys->width, p_sys->height,
592 vd->source.i_width, vd->source.i_height,
593 p_sys->data_size, pic->p->p_pixels);
595 xcb_flush (p_sys->conn);
596 picture_Release (pic);
599 static int Control (vout_display_t *vd, int query, va_list ap)
601 vout_display_sys_t *p_sys = vd->sys;
605 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
606 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
607 case VOUT_DISPLAY_CHANGE_ZOOM:
608 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
609 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
611 const vout_display_cfg_t *cfg;
612 const video_format_t *source;
614 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
615 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
617 source = (const video_format_t *)va_arg (ap, const video_format_t *);
622 source = &vd->source;
623 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
627 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
628 && (cfg->display.width != vd->cfg->display.width
629 ||cfg->display.height != vd->cfg->display.height)
630 && vout_window_SetSize (p_sys->embed,
632 cfg->display.height))
635 vout_display_place_t place;
636 vout_display_PlacePicture (&place, source, cfg, false);
637 p_sys->width = place.width;
638 p_sys->height = place.height;
640 /* Move the picture within the window */
641 const uint32_t values[] = { place.x, place.y,
642 place.width, place.height, };
643 xcb_configure_window (p_sys->conn, p_sys->window,
644 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
645 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
647 xcb_flush (p_sys->conn);
650 case VOUT_DISPLAY_CHANGE_ON_TOP:
652 int on_top = (int)va_arg (ap, int);
653 return vout_window_SetOnTop (p_sys->embed, on_top);
658 /* Hide the mouse. It will be send when
659 * vout_display_t::info.b_hide_mouse is false */
660 VOUT_DISPLAY_HIDE_MOUSE,
662 /* Ask the module to acknowledge/refuse the fullscreen state change after
663 * being requested (externaly or by VOUT_DISPLAY_EVENT_FULLSCREEN */
664 VOUT_DISPLAY_CHANGE_FULLSCREEN, /* const vout_display_cfg_t *p_cfg */
666 case VOUT_DISPLAY_RESET_PICTURES:
669 msg_Err (vd, "Unknown request in XCB vout display");
674 static void Manage (vout_display_t *vd)
676 vout_display_sys_t *p_sys = vd->sys;
678 ManageEvent (vd, p_sys->conn, p_sys->window);