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 uint32_t data_size; /* picture byte size (for non-SHM) */
83 bool shm; /* whether to use MIT-SHM */
86 static int Init (vout_thread_t *);
87 static void Deinit (vout_thread_t *);
88 static void Display (vout_thread_t *, picture_t *);
89 static int Manage (vout_thread_t *);
90 static int Control (vout_thread_t *, int, va_list);
92 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
94 xcb_generic_error_t *err;
96 err = xcb_request_check (vout->p_sys->conn, ck);
99 msg_Err (vout, "%s: X11 error %d", str, err->error_code);
106 * Check that the X server supports the XVideo extension.
108 static bool CheckXVideo (vout_thread_t *vout, xcb_connection_t *conn)
110 xcb_xv_query_extension_reply_t *r;
111 xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
114 r = xcb_xv_query_extension_reply (conn, ck, NULL);
116 { /* We need XVideo 2.2 for PutImage */
117 if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
119 msg_Dbg (vout, "using XVideo extension v%"PRIu8".%"PRIu8,
124 msg_Dbg (vout, "XVideo extension too old (v%"PRIu8".%"PRIu8,
129 msg_Dbg (vout, "XVideo extension not available");
134 * Get a list of XVideo adaptors for a given window.
136 static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
137 xcb_connection_t *conn)
139 xcb_xv_query_adaptors_cookie_t ck;
141 ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
142 return xcb_xv_query_adaptors_reply (conn, ck, NULL);
148 * Probe the X server.
150 static int Open (vlc_object_t *obj)
152 vout_thread_t *vout = (vout_thread_t *)obj;
153 vout_sys_t *p_sys = malloc (sizeof (*p_sys));
160 p_sys->conn = Connect (obj);
161 if (p_sys->conn == NULL)
167 if (!CheckXVideo (vout, p_sys->conn))
169 msg_Warn (vout, "Please enable XVideo 2.2 for faster video display");
170 xcb_disconnect (p_sys->conn);
175 const xcb_screen_t *screen;
176 p_sys->embed = GetWindow (vout, p_sys->conn, &screen, &p_sys->shm);
177 if (p_sys->embed == NULL)
179 xcb_disconnect (p_sys->conn);
184 /* Cache adaptors infos */
185 p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
186 if (p_sys->adaptors == NULL)
191 const uint32_t mask =
192 /* XCB_CW_EVENT_MASK */
193 XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
194 XCB_EVENT_MASK_POINTER_MOTION;
196 xcb_window_t window = xcb_generate_id (p_sys->conn);
198 c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
199 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
200 XCB_WINDOW_CLASS_INPUT_OUTPUT,
202 XCB_CW_EVENT_MASK, &mask);
203 if (CheckError (vout, "cannot create X11 window", c))
205 p_sys->window = window;
206 msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
207 xcb_map_window (p_sys->conn, window);
210 p_sys->gc = xcb_generate_id (p_sys->conn);
211 xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
212 msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
214 vout->pf_init = Init;
215 vout->pf_end = Deinit;
216 vout->pf_display = Display;
217 vout->pf_manage = Manage;
218 vout->pf_control = Control;
228 * Disconnect from the X server.
230 static void Close (vlc_object_t *obj)
232 vout_thread_t *vout = (vout_thread_t *)obj;
233 vout_sys_t *p_sys = vout->p_sys;
235 free (p_sys->adaptors);
236 vout_ReleaseWindow (p_sys->embed);
237 xcb_disconnect (p_sys->conn);
241 static vlc_fourcc_t ParseFormat (vout_thread_t *vout,
242 const xcb_xv_image_format_info_t *restrict f)
244 if (f->byte_order != ORDER && f->bpp != 8)
245 return 0; /* Argh! */
249 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
250 switch (f->num_planes)
257 return VLC_CODEC_RGB32;
261 return VLC_CODEC_RGB24;
265 return VLC_CODEC_RGB16;
267 return VLC_CODEC_RGB15;
271 return VLC_CODEC_RGB8;
276 msg_Err (vout, "unknown XVideo RGB format %"PRIx32" (%.4s)",
278 msg_Dbg (vout, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
279 "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
282 case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
283 if (f->u_sample_bits != f->v_sample_bits
284 || f->vhorz_u_period != f->vhorz_v_period
285 || f->vvert_u_period != f->vvert_v_period
286 || f->y_sample_bits != 8 || f->u_sample_bits != 8
287 || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
289 switch (f->num_planes)
295 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
296 return VLC_CODEC_I444;
299 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
301 if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
302 return VLC_CODEC_YUYV;
303 if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
304 return VLC_CODEC_UYVY;
313 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
315 if (!strcmp ((const char *)f->vcomp_order, "YVU"))
316 return VLC_CODEC_YV12;
317 if (!strcmp ((const char *)f->vcomp_order, "YUV"))
318 return VLC_CODEC_I420;
324 msg_Err (vout, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
326 msg_Dbg (vout, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
327 "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
328 f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
329 msg_Dbg (vout, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
330 "%"PRIu32"/%"PRIu32"/%"PRIu32,
331 f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
332 f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
333 msg_Warn (vout, " order: %.32s", f->vcomp_order);
340 static const xcb_xv_image_format_info_t *
341 FindFormat (vout_thread_t *vout, vlc_fourcc_t chroma, xcb_xv_port_t port,
342 const xcb_xv_list_image_formats_reply_t *list,
343 xcb_xv_query_image_attributes_reply_t **restrict pa)
345 xcb_connection_t *conn = vout->p_sys->conn;
346 const xcb_xv_image_format_info_t *f, *end;
349 f = xcb_xv_list_image_formats_format (list);
351 f = (xcb_xv_image_format_info_t *) (list + 1);
353 end = f + xcb_xv_list_image_formats_format_length (list);
356 if (chroma != ParseFormat (vout, f))
359 xcb_xv_query_image_attributes_reply_t *i;
360 i = xcb_xv_query_image_attributes_reply (conn,
361 xcb_xv_query_image_attributes (conn, port, f->id,
362 vout->fmt_in.i_width, vout->fmt_in.i_height), NULL);
366 if (i->width != vout->fmt_in.i_width
367 || i->height != vout->fmt_in.i_height)
369 msg_Warn (vout, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
370 vout->fmt_in.i_width, vout->fmt_in.i_height,
371 i->width, i->height);
382 * Allocate drawable window and picture buffers.
384 static int Init (vout_thread_t *vout)
386 vout_sys_t *p_sys = vout->p_sys;
387 xcb_xv_query_image_attributes_reply_t *att = NULL;
388 bool swap_planes = false; /* whether X wants V before U */
390 /* FIXME: check max image size */
391 xcb_xv_adaptor_info_iterator_t it;
392 for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
394 xcb_xv_adaptor_info_next (&it))
396 const xcb_xv_adaptor_info_t *a = it.data;
398 /* FIXME: Open() should fail if none of the ports are usable to VLC */
399 if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
402 xcb_xv_list_image_formats_reply_t *r;
403 r = xcb_xv_list_image_formats_reply (p_sys->conn,
404 xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
408 const xcb_xv_image_format_info_t *fmt;
410 /* Video chroma in preference order */
411 const vlc_fourcc_t chromas[] = {
412 vout->fmt_in.i_chroma,
417 for (size_t i = 0; i < sizeof (chromas) / sizeof (chromas[0]); i++)
419 vlc_fourcc_t chroma = chromas[i];
420 fmt = FindFormat (vout, chroma, a->base_id, r, &att);
423 vout->output.i_chroma = chroma;
431 /* TODO: grab port */
432 p_sys->port = a->base_id;
433 msg_Dbg (vout, "using port %"PRIu32, p_sys->port);
436 msg_Dbg (vout, "using image format 0x%"PRIx32, p_sys->id);
437 if (fmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
439 vout->fmt_out.i_rmask = vout->output.i_rmask = fmt->red_mask;
440 vout->fmt_out.i_gmask = vout->output.i_gmask = fmt->green_mask;
441 vout->fmt_out.i_bmask = vout->output.i_bmask = fmt->blue_mask;
444 if (fmt->num_planes == 3)
445 swap_planes = !strcmp ((const char *)fmt->vcomp_order, "YVU");
449 msg_Err (vout, "no available XVideo adaptor");
450 return VLC_EGENERIC; /* no usable adaptor */
452 /* Allocate picture buffers */
453 const uint32_t *offsets;
455 offsets = xcb_xv_query_image_attributes_offsets (att);
456 p_sys->data_size = att->data_size;
458 I_OUTPUTPICTURES = 0;
459 for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
461 picture_t *pic = vout->p_picture + index;
463 if (index > sizeof (vout->p_picture) / sizeof (pic))
465 if (pic->i_status != FREE_PICTURE)
468 picture_Setup (pic, vout->output.i_chroma,
469 att->width, att->height,
470 vout->fmt_in.i_aspect);
471 if (PictureAlloc (vout, pic, att->data_size,
472 p_sys->shm ? p_sys->conn : NULL))
474 /* Allocate further planes as specified by XVideo */
475 /* We assume that offsets[0] is zero */
476 for (int i = 1; i < pic->i_planes; i++)
478 pic->p->p_pixels + offsets[swap_planes ? (3 - i) : i];
479 PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
483 unsigned x, y, width, height;
485 if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
487 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
489 const uint32_t values[] = { x, y, width, height, };
490 xcb_configure_window (p_sys->conn, p_sys->window,
491 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
492 XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
494 xcb_flush (p_sys->conn);
495 p_sys->height = height;
496 p_sys->width = width;
498 vout->fmt_out.i_chroma = vout->output.i_chroma;
499 vout->fmt_out.i_visible_width = vout->fmt_in.i_visible_width;
500 vout->fmt_out.i_visible_height = vout->fmt_in.i_visible_height;
501 vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
503 vout->output.i_width = vout->fmt_out.i_width = vout->fmt_in.i_width;
504 vout->output.i_height = vout->fmt_out.i_height = vout->fmt_in.i_height;
505 vout->fmt_out.i_x_offset = vout->fmt_in.i_x_offset;
506 vout->fmt_out.i_y_offset = vout->fmt_in.i_y_offset;
509 vout->output.i_aspect = vout->fmt_out.i_aspect =
510 width * VOUT_ASPECT_FACTOR / height;
516 * Free picture buffers.
518 static void Deinit (vout_thread_t *vout)
520 vout_sys_t *p_sys = vout->p_sys;
522 for (int i = 0; i < I_OUTPUTPICTURES; i++)
523 PictureFree (PP_OUTPUTPICTURE[i], p_sys->conn);
527 * Sends an image to the X server.
529 static void Display (vout_thread_t *vout, picture_t *pic)
531 vout_sys_t *p_sys = vout->p_sys;
532 xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
535 xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
536 p_sys->gc, segment, p_sys->id, 0,
537 /* Src: */ vout->fmt_out.i_x_offset,
538 vout->fmt_out.i_y_offset,
539 vout->fmt_out.i_visible_width,
540 vout->fmt_out.i_visible_height,
541 /* Dst: */ 0, 0, p_sys->width, p_sys->height,
543 pic->p->i_pitch / pic->p->i_pixel_pitch,
544 pic->p->i_lines, false);
546 xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
547 p_sys->gc, p_sys->id,
548 vout->fmt_out.i_x_offset, vout->fmt_out.i_y_offset,
549 vout->fmt_out.i_visible_width,
550 vout->fmt_out.i_visible_height,
551 0, 0, p_sys->width, p_sys->height,
552 vout->fmt_out.i_width, vout->fmt_out.i_height,
553 p_sys->data_size, pic->p->p_pixels);
554 xcb_flush (p_sys->conn);
558 * Process incoming X events.
560 static int Manage (vout_thread_t *vout)
562 vout_sys_t *p_sys = vout->p_sys;
563 xcb_generic_event_t *ev;
565 while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
566 ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
568 if (xcb_connection_has_error (p_sys->conn))
570 msg_Err (vout, "X server failure");
575 if (vout->i_changes & VOUT_SIZE_CHANGE)
576 { /* TODO: factor this code with XV and X11 Init() */
577 unsigned x, y, width, height;
579 if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
581 vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
583 const uint32_t values[] = { x, y, width, height, };
584 xcb_configure_window (p_sys->conn, p_sys->window, XCB_CONFIG_WINDOW_X |
585 XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH |
586 XCB_CONFIG_WINDOW_HEIGHT, values);
587 vout->p_sys->width = width; // XXX: <-- this is useless, as the zoom is
588 vout->p_sys->height = height; // handled with VOUT_SET_SIZE anyway.
589 vout->i_changes &= ~VOUT_SIZE_CHANGE;
595 HandleParentStructure (vout_thread_t *vout, xcb_connection_t *conn,
596 xcb_window_t xid, xcb_configure_notify_event_t *ev)
598 unsigned width, height, x, y;
600 vout_PlacePicture (vout, ev->width, ev->height, &x, &y, &width, &height);
602 /* Move the picture within the window */
603 const uint32_t values[] = { x, y, width, height, };
604 xcb_configure_window (conn, xid,
605 XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
606 | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
608 vout->p_sys->width = width;
609 vout->p_sys->height = height;
612 static int Control (vout_thread_t *vout, int query, va_list ap)
614 return vout_ControlWindow (vout->p_sys->embed, query, ap);