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>
37 #include <vlc_window.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 ("video output", 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")
72 xcb_connection_t *conn;
73 xcb_xv_query_adaptors_reply_t *adaptors;
74 vout_window_t *embed;/* VLC window */
76 xcb_window_t window; /* drawable X window */
77 xcb_gcontext_t gc; /* context to put images */
78 xcb_xv_port_t port; /* XVideo port */
79 uint32_t id; /* XVideo format */
80 uint16_t width; /* display width */
81 uint16_t height; /* display height */
82 bool shm; /* whether to use MIT-SHM */
85 static int Init (vout_thread_t *);
86 static void Deinit (vout_thread_t *);
87 static void Display (vout_thread_t *, picture_t *);
88 static int Manage (vout_thread_t *);
90 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
92 xcb_generic_error_t *err;
94 err = xcb_request_check (vout->p_sys->conn, ck);
97 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
104 * Check that the X server supports the XVideo extension.
106 static bool CheckXVideo (vout_thread_t *vout, xcb_connection_t *conn)
108 xcb_xv_query_extension_reply_t *r;
109 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
112 r = xcb_xv_query_extension_reply (conn, ck, NULL);
114 { /* We need XVideo 2.2 for PutImage */
115 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
117 msg_Dbg (vout, "using XVideo extension v%"PRIu8".%"PRIu8,
122 msg_Dbg (vout, "XVideo extension too old (v%"PRIu8".%"PRIu8,
127 msg_Dbg (vout, "XVideo extension not available");
132 * Get a list of XVideo adaptors for a given window.
134 static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
135 xcb_connection_t *conn)
137 xcb_xv_query_adaptors_cookie_t ck;
139 ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
140 return xcb_xv_query_adaptors_reply (conn, ck, NULL);
146 * Probe the X server.
148 static int Open (vlc_object_t *obj)
150 vout_thread_t *vout = (vout_thread_t *)obj;
151 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
158 p_sys->conn = Connect (obj);
159 if (p_sys->conn == NULL)
162 if (!CheckXVideo (vout, p_sys->conn))
164 msg_Warn (vout, "Please enable XVideo 2.2 for faster video display");
165 xcb_disconnect (p_sys->conn);
169 const xcb_screen_t *screen;
170 p_sys->embed = GetWindow (vout, p_sys->conn, &screen, &p_sys->shm);
171 if (p_sys->embed == NULL)
173 xcb_disconnect (p_sys->conn);
177 /* Cache adaptors infos */
178 p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
179 if (p_sys->adaptors == NULL)
184 const uint32_t mask =
185 /* XCB_CW_EVENT_MASK */
186 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
187 XCB_EVENT_MASK_POINTER_MOTION;
189 xcb_window_t window = xcb_generate_id (p_sys->conn);
191 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
192 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
193 XCB_WINDOW_CLASS_INPUT_OUTPUT,
195 XCB_CW_EVENT_MASK, &mask);
196 if (CheckError (vout, "cannot create X11 window", c))
198 p_sys->window = window;
199 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
200 xcb_map_window (p_sys->conn, window);
203 p_sys->gc = xcb_generate_id (p_sys->conn);
204 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
205 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
207 vout->pf_init = Init;
208 vout->pf_end = Deinit;
209 vout->pf_display = Display;
210 vout->pf_manage = Manage;
220 * Disconnect from the X server.
222 static void Close (vlc_object_t *obj)
224 vout_thread_t *vout = (vout_thread_t *)obj;
225 vout_sys_t *p_sys = vout->p_sys;
227 free (p_sys->adaptors);
228 vout_ReleaseWindow (p_sys->embed);
229 xcb_disconnect (p_sys->conn);
233 static int QueryBestSize (vout_thread_t *vout,
234 unsigned *restrict width, unsigned *restrict height)
236 vout_sys_t *p_sys = vout->p_sys;
237 xcb_xv_query_best_size_reply_t *r;
238 xcb_xv_query_best_size_cookie_t ck;
240 ck = xcb_xv_query_best_size (p_sys->conn, p_sys->port,
241 vout->fmt_in.i_visible_width,
242 vout->fmt_in.i_visible_height,
243 *width, *height, false);
244 r = xcb_xv_query_best_size_reply (p_sys->conn, ck, NULL);
248 msg_Dbg (vout, "best size: %ux%u -> %ux%u", *width, *height,
249 r->actual_width, r->actual_height);
250 *width = r->actual_width;
251 *height = r->actual_height;
257 static vlc_fourcc_t ParseFormat (vout_thread_t *vout,
258 const xcb_xv_image_format_info_t *restrict f)
260 if (f->byte_order != ORDER && f->bpp != 8)
261 return 0; /* Argh! */
265 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
266 switch (f->num_planes)
273 return VLC_FOURCC ('R', 'V', '3', '2');
277 return VLC_FOURCC ('R', 'V', '2', '4');
281 return VLC_FOURCC ('R', 'V', '1', '6');
283 return VLC_FOURCC ('R', 'V', '1', '5');
287 return VLC_FOURCC ('R', 'G', 'B', '2');
292 msg_Err (vout, "unknown XVideo RGB format %"PRIx32" (%.4s)",
294 msg_Dbg (vout, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
295 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
298 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
299 if (f->u_sample_bits != f->v_sample_bits
300 || f->vhorz_u_period != f->vhorz_v_period
301 || f->vvert_u_period != f->vvert_v_period
302 || f->y_sample_bits != 8 || f->u_sample_bits != 8
303 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
305 switch (f->num_planes)
311 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
312 return VLC_FOURCC ('I', '4', '4', '4');
315 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
317 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
318 return VLC_FOURCC ('Y', 'U', 'Y', '2');
319 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
320 return VLC_FOURCC ('U', 'Y', 'V', 'Y');
329 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
331 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
332 return VLC_FOURCC ('Y', 'V', '1', '2');
333 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
334 return VLC_FOURCC ('I', '4', '2', '0');
340 msg_Err (vout, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
342 msg_Dbg (vout, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
343 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
344 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
345 msg_Dbg (vout, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
346 "%"PRIu32"/%"PRIu32"/%"PRIu32,
347 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
348 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
349 msg_Warn (vout, " order: %.32s", f->vcomp_order);
357 * Allocate drawable window and picture buffers.
359 static int Init (vout_thread_t *vout)
361 vout_sys_t *p_sys = vout->p_sys;
362 unsigned x, y, width, height;
364 /* FIXME: check max image size */
365 xcb_xv_adaptor_info_iterator_t it;
366 for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
368 xcb_xv_adaptor_info_next (&it))
370 const xcb_xv_adaptor_info_t *a = it.data;
372 /* FIXME: Open() should fail if none of the ports are usable to VLC */
373 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
376 xcb_xv_list_image_formats_reply_t *r;
377 r = xcb_xv_list_image_formats_reply (p_sys->conn,
378 xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
382 const xcb_xv_image_format_info_t *f, *end;
383 f = xcb_xv_list_image_formats_format (r);
384 end = f + xcb_xv_list_image_formats_format_length (r);
387 vlc_fourcc_t chroma = ParseFormat (vout, f);
391 if (chroma == vout->fmt_in.i_chroma)
393 vout->output.i_chroma = chroma;
397 /* TODO: RGB masks */
401 /* TODO: grab port */
403 msg_Dbg (vout, "using image format 0x%"PRIx32, p_sys->id);
404 p_sys->port = a->base_id;
405 msg_Dbg (vout, "using port %"PRIu32, p_sys->port);
409 /* TODO: fallback to RV24 or I420 */
410 if (!vout->output.i_chroma)
411 return VLC_EGENERIC; /* no usable adaptor */
413 if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
415 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
416 if (QueryBestSize (vout, &width, &height))
419 const uint32_t values[] = { x, y, width, height, };
420 xcb_configure_window (p_sys->conn, p_sys->window,
421 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
422 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
424 p_sys->height = height;
425 p_sys->width = width;
427 vout->fmt_out.i_chroma = vout->output.i_chroma;
428 vout->fmt_out.i_visible_width = vout->fmt_in.i_visible_width;
429 vout->fmt_out.i_visible_height = vout->fmt_in.i_visible_height;
430 vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
432 vout->output.i_width = vout->fmt_out.i_width = vout->fmt_in.i_width;
433 vout->output.i_height = vout->fmt_out.i_height = vout->fmt_in.i_height;
434 vout->fmt_out.i_x_offset = vout->fmt_in.i_x_offset;
435 p_vout->fmt_out.i_y_offset = vout->fmt_in.i_y_offset;
438 vout->output.i_aspect = vout->fmt_out.i_aspect =
439 width * VOUT_ASPECT_FACTOR / height;
441 /* Allocate picture buffers */
442 I_OUTPUTPICTURES = 0;
443 for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
445 picture_t *pic = vout->p_picture + index;
447 if (index > sizeof (vout->p_picture) / sizeof (pic))
449 if (pic->i_status != FREE_PICTURE)
452 vout_InitPicture (vout, pic, vout->output.i_chroma,
453 vout->output.i_width, vout->output.i_height,
454 vout->output.i_aspect);
455 if (PictureAlloc (vout, pic, pic->p->i_pitch * pic->p->i_lines,
456 p_sys->shm ? p_sys->conn : NULL))
458 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
460 xcb_flush (p_sys->conn);
465 * Free picture buffers.
467 static void Deinit (vout_thread_t *vout)
469 vout_sys_t *p_sys = vout->p_sys;
471 for (int i = 0; i < I_OUTPUTPICTURES; i++)
472 PictureFree (PP_OUTPUTPICTURE[i], p_sys->conn);
476 * Sends an image to the X server.
478 static void Display (vout_thread_t *vout, picture_t *pic)
480 vout_sys_t *p_sys = vout->p_sys;
481 xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
484 xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
485 p_sys->gc, segment, p_sys->id, 0,
487 pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
488 pic->p->i_visible_lines,
489 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
491 pic->p->i_pitch / pic->p->i_pixel_pitch,
492 pic->p->i_lines, false);
494 xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
495 p_sys->gc, p_sys->id,
497 pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
498 pic->p->i_visible_lines,
499 0, 0, p_sys->width, p_sys->height,
500 pic->p->i_pitch / pic->p->i_pixel_pitch,
502 pic->p->i_pitch * pic->p->i_lines, pic->p->p_pixels);
503 xcb_flush (p_sys->conn);
507 * Process incoming X events.
509 static int Manage (vout_thread_t *vout)
511 vout_sys_t *p_sys = vout->p_sys;
512 xcb_generic_event_t *ev;
514 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
515 ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
517 if (xcb_connection_has_error (p_sys->conn))
519 msg_Err (vout, "X server failure");
526 HandleParentStructure (vout_thread_t *vout, xcb_connection_t *conn,
527 xcb_window_t xid, xcb_configure_notify_event_t *ev)
529 unsigned width, height, x, y;
531 vout_PlacePicture (vout, ev->width, ev->height, &x, &y, &width, &height);
533 /* Move the picture within the window */
534 const uint32_t values[] = { x, y, width, height, };
535 xcb_configure_window (conn, xid,
536 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
537 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
539 vout->p_sys->width = width;
540 vout->p_sys->height = height;