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 xcb_xv_query_adaptors_reply_t *adaptors;
87 vout_window_t *embed;/* VLC window */
89 xcb_window_t window; /* drawable X window */
90 xcb_gcontext_t gc; /* context to put images */
91 xcb_xv_port_t port; /* XVideo port */
92 uint32_t id; /* XVideo format */
93 uint16_t width; /* display width */
94 uint16_t height; /* display height */
95 uint32_t data_size; /* picture byte size (for non-SHM) */
96 bool shm; /* whether to use MIT-SHM */
98 xcb_xv_query_image_attributes_reply_t *att;
99 picture_pool_t *pool; /* picture pool */
100 picture_resource_t resource[MAX_PICTURES];
103 static picture_t *Get (vout_display_t *);
104 static void Display (vout_display_t *, picture_t *);
105 static int Control (vout_display_t *, int, va_list);
106 static void Manage (vout_display_t *);
109 * Check that the X server supports the XVideo extension.
111 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
113 xcb_xv_query_extension_reply_t *r;
114 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
117 r = xcb_xv_query_extension_reply (conn, ck, NULL);
119 { /* We need XVideo 2.2 for PutImage */
120 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
122 msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
127 msg_Dbg (vd, "XVideo extension too old (v%"PRIu8".%"PRIu8,
132 msg_Dbg (vd, "XVideo extension not available");
136 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
137 const xcb_xv_image_format_info_t *restrict f)
139 if (f->byte_order != ORDER && f->bpp != 8)
140 return 0; /* Argh! */
144 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
145 switch (f->num_planes)
152 return VLC_CODEC_RGB32;
156 return VLC_CODEC_RGB24;
160 return VLC_CODEC_RGB16;
162 return VLC_CODEC_RGB15;
166 return VLC_CODEC_RGB8;
171 msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
173 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
174 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
177 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
178 if (f->u_sample_bits != f->v_sample_bits
179 || f->vhorz_u_period != f->vhorz_v_period
180 || f->vvert_u_period != f->vvert_v_period
181 || f->y_sample_bits != 8 || f->u_sample_bits != 8
182 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
184 switch (f->num_planes)
190 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
191 return VLC_CODEC_I444;
194 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
196 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
197 return VLC_CODEC_YUYV;
198 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
199 return VLC_CODEC_UYVY;
208 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
210 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
211 return VLC_CODEC_YV12;
212 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
213 return VLC_CODEC_I420;
219 msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
221 msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
222 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
223 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
224 msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
225 "%"PRIu32"/%"PRIu32"/%"PRIu32,
226 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
227 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
228 msg_Warn (vd, " order: %.32s", f->vcomp_order);
235 static const xcb_xv_image_format_info_t *
236 FindFormat (vout_display_t *vd,
237 vlc_fourcc_t chroma, const video_format_t *fmt,
239 const xcb_xv_list_image_formats_reply_t *list,
240 xcb_xv_query_image_attributes_reply_t **restrict pa)
242 xcb_connection_t *conn = vd->sys->conn;
243 const xcb_xv_image_format_info_t *f, *end;
246 f = xcb_xv_list_image_formats_format (list);
248 f = (xcb_xv_image_format_info_t *) (list + 1);
250 end = f + xcb_xv_list_image_formats_format_length (list);
253 if (chroma != ParseFormat (vd, f))
256 xcb_xv_query_image_attributes_reply_t *i;
257 i = xcb_xv_query_image_attributes_reply (conn,
258 xcb_xv_query_image_attributes (conn, port, f->id,
259 fmt->i_width, fmt->i_height), NULL);
263 if (i->width != fmt->i_width
264 || i->height != fmt->i_height)
266 msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
267 fmt->i_width, fmt->i_height,
268 i->width, i->height);
280 * Get a list of XVideo adaptors for a given window.
282 static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
283 xcb_connection_t *conn)
285 xcb_xv_query_adaptors_cookie_t ck;
287 ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
288 return xcb_xv_query_adaptors_reply (conn, ck, NULL);
292 * Probe the X server.
294 static int Open (vlc_object_t *obj)
296 vout_display_t *vd = (vout_display_t *)obj;
297 vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
304 p_sys->conn = Connect (obj);
305 if (p_sys->conn == NULL)
311 if (!CheckXVideo (vd, p_sys->conn))
313 msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
314 xcb_disconnect (p_sys->conn);
319 const xcb_screen_t *screen;
320 p_sys->embed = GetWindow (vd, p_sys->conn, &screen, &p_sys->shm);
321 if (p_sys->embed == NULL)
323 xcb_disconnect (p_sys->conn);
332 /* Cache adaptors infos */
333 p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
334 if (p_sys->adaptors == NULL)
337 int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
340 video_format_t fmt = vd->fmt;
341 bool found_adaptor = false;
343 /* FIXME: check max image size */
344 xcb_xv_adaptor_info_iterator_t it;
345 for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
347 xcb_xv_adaptor_info_next (&it))
349 const xcb_xv_adaptor_info_t *a = it.data;
351 if (forced_adaptor != -1 && forced_adaptor != 0)
357 /* FIXME: Open() should fail if none of the ports are usable to VLC */
358 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
361 xcb_xv_list_image_formats_reply_t *r;
362 r = xcb_xv_list_image_formats_reply (p_sys->conn,
363 xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
367 const xcb_xv_image_format_info_t *xfmt;
370 const vlc_fourcc_t *chromas, chromas_default[] = {
377 if (vlc_fourcc_IsYUV (fmt.i_chroma))
378 chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
380 chromas = chromas_default;
382 for (size_t i = 0; chromas[i]; i++)
384 vlc_fourcc_t chroma = chromas[i];
385 xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
388 fmt.i_chroma = chroma;
397 /* XXX: assume all of an adapter's ports have the same formats */
398 for (unsigned i = 0; i < a->num_ports; i++)
400 xcb_xv_port_t port = a->base_id + i;
401 xcb_xv_grab_port_reply_t *gr =
402 xcb_xv_grab_port_reply (p_sys->conn,
403 xcb_xv_grab_port (p_sys->conn, port,
404 XCB_CURRENT_TIME), NULL);
405 uint8_t result = gr ? gr->result : 0xff;
413 msg_Dbg (vd, "cannot grab port %"PRIu32, port);
418 msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
420 p_sys->id = xfmt->id;
421 msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
422 if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
424 fmt.i_rmask = xfmt->red_mask;
425 fmt.i_gmask = xfmt->green_mask;
426 fmt.i_bmask = xfmt->blue_mask;
429 if (xfmt->num_planes == 3
430 && !strcmp ((const char *)xfmt->vcomp_order, "YVU"))
431 fmt.i_chroma = VLC_CODEC_YV12;
433 found_adaptor = true;
438 msg_Err (vd, "no available XVideo adaptor");
444 const uint32_t mask =
445 /* XCB_CW_EVENT_MASK */
446 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
447 XCB_EVENT_MASK_POINTER_MOTION;
449 xcb_window_t window = xcb_generate_id (p_sys->conn);
451 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
452 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
453 XCB_WINDOW_CLASS_INPUT_OUTPUT,
455 XCB_CW_EVENT_MASK, &mask);
456 if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
458 p_sys->window = window;
459 msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
460 xcb_map_window (p_sys->conn, window);
462 vout_display_place_t place;
464 vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
465 p_sys->width = place.width;
466 p_sys->height = place.height;
469 const uint32_t values[] = { place.x, place.y, place.width, place.height };
470 xcb_configure_window (p_sys->conn, p_sys->window,
471 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
472 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
476 /* Create graphic context */
477 p_sys->gc = xcb_generate_id (p_sys->conn);
478 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
479 msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
485 vout_display_info_t info = vd->info;
486 info.has_pictures_invalid = false;
488 /* Setup vout_display_t once everything is fine */
494 vd->display = Display;
495 vd->control = Control;
499 unsigned width, height;
500 if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
501 vout_display_SendEventDisplaySize (vd, width, height);
502 vout_display_SendEventFullscreen (vd, false);
513 * Disconnect from the X server.
515 static void Close (vlc_object_t *obj)
517 vout_display_t *vd = (vout_display_t *)obj;
518 vout_display_sys_t *p_sys = vd->sys;
522 for (unsigned i = 0; i < MAX_PICTURES; i++)
524 picture_resource_t *res = &p_sys->resource[i];
526 if (!res->p->p_pixels)
528 PictureResourceFree (res, p_sys->conn);
530 picture_pool_Delete (p_sys->pool);
534 free (p_sys->adaptors);
535 vout_display_DeleteWindow (vd, p_sys->embed);
536 xcb_disconnect (p_sys->conn);
541 * Return a direct buffer
543 static picture_t *Get (vout_display_t *vd)
545 vout_display_sys_t *p_sys = vd->sys;
549 picture_t *pic = picture_New (vd->fmt.i_chroma,
550 p_sys->att->width, p_sys->att->height, 0);
554 memset (p_sys->resource, 0, sizeof(p_sys->resource));
556 const uint32_t *offsets =
557 xcb_xv_query_image_attributes_offsets (p_sys->att);
558 p_sys->data_size = p_sys->att->data_size;
561 picture_t *pic_array[MAX_PICTURES];
562 for (count = 0; count < MAX_PICTURES; count++)
564 picture_resource_t *res = &p_sys->resource[count];
566 for (int i = 0; i < pic->i_planes; i++)
568 res->p[i].i_lines = pic->p[i].i_lines; /* FIXME seems wrong*/
569 res->p[i].i_pitch = pic->p[i].i_pitch;
571 if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
572 p_sys->conn, p_sys->shm))
575 /* Allocate further planes as specified by XVideo */
576 /* We assume that offsets[0] is zero */
577 for (int i = 1; i < pic->i_planes; i++)
578 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
579 pic_array[count] = picture_NewFromResource (&vd->fmt, res);
580 if (!pic_array[count])
582 PictureResourceFree (res, p_sys->conn);
583 memset (res, 0, sizeof(*res));
587 picture_Release (pic);
592 p_sys->pool = picture_pool_New (count, pic_array);
595 /* TODO release picture resources */
598 /* FIXME should also do it in case of error ? */
599 xcb_flush (p_sys->conn);
602 return picture_pool_Get (p_sys->pool);
606 * Sends an image to the X server.
608 static void Display (vout_display_t *vd, picture_t *pic)
610 vout_display_sys_t *p_sys = vd->sys;
611 xcb_shm_seg_t segment = pic->p_sys->segment;
612 xcb_void_cookie_t ck;
615 ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
616 p_sys->window, p_sys->gc, segment, p_sys->id, 0,
617 /* Src: */ vd->source.i_x_offset,
618 vd->source.i_y_offset,
619 vd->source.i_visible_width,
620 vd->source.i_visible_height,
621 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
622 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
623 pic->p->i_visible_lines, false);
625 ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
626 p_sys->gc, p_sys->id,
627 vd->source.i_x_offset,
628 vd->source.i_y_offset,
629 vd->source.i_visible_width,
630 vd->source.i_visible_height,
631 0, 0, p_sys->width, p_sys->height,
632 vd->source.i_width, vd->source.i_height,
633 p_sys->data_size, pic->p->p_pixels);
635 /* Wait for reply. See x11.c for rationale. */
636 xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
639 msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
643 picture_Release (pic);
646 static int Control (vout_display_t *vd, int query, va_list ap)
648 vout_display_sys_t *p_sys = vd->sys;
652 case VOUT_DISPLAY_CHANGE_FULLSCREEN:
654 const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
655 return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
658 case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
659 case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
660 case VOUT_DISPLAY_CHANGE_ZOOM:
661 case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
662 case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
664 const vout_display_cfg_t *cfg;
665 const video_format_t *source;
667 if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
668 || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
670 source = (const video_format_t *)va_arg (ap, const video_format_t *);
675 source = &vd->source;
676 cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
680 if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
681 && (cfg->display.width != vd->cfg->display.width
682 ||cfg->display.height != vd->cfg->display.height)
683 && vout_window_SetSize (p_sys->embed,
685 cfg->display.height))
688 vout_display_place_t place;
689 vout_display_PlacePicture (&place, source, cfg, false);
690 p_sys->width = place.width;
691 p_sys->height = place.height;
693 /* Move the picture within the window */
694 const uint32_t values[] = { place.x, place.y,
695 place.width, place.height, };
696 xcb_configure_window (p_sys->conn, p_sys->window,
697 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
698 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
700 xcb_flush (p_sys->conn);
703 case VOUT_DISPLAY_CHANGE_ON_TOP:
705 int on_top = (int)va_arg (ap, int);
706 return vout_window_SetOnTop (p_sys->embed, on_top);
711 /* Hide the mouse. It will be send when
712 * vout_display_t::info.b_hide_mouse is false */
713 VOUT_DISPLAY_HIDE_MOUSE,
715 case VOUT_DISPLAY_RESET_PICTURES:
718 msg_Err (vd, "Unknown request in XCB vout display");
723 static void Manage (vout_display_t *vd)
725 vout_display_sys_t *p_sys = vd->sys;
727 ManageEvent (vd, p_sys->conn, p_sys->window);