]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/xvideo.c
video_output/xcb: fix description strings for --xvideo-format-id config option
[vlc] / modules / video_output / xcb / xvideo.c
1 /**
2  * @file xvideo.c
3  * @brief X C Bindings video output module for VLC media player
4  */
5 /*****************************************************************************
6  * Copyright © 2009 Rémi Denis-Courmont
7  *
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
11  * of the License, or (at your option) any later version.
12  *
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.
17  *
18  * You should have received a copy of the GNU 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  ****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <xcb/xcb.h>
31 #include <xcb/shm.h>
32 #include <xcb/xv.h>
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_vout_display.h>
37 #include <vlc_picture_pool.h>
38 #include <vlc_dialog.h>
39
40 #include "xcb_vlc.h"
41
42 #define ADAPTOR_TEXT N_("XVideo adaptor number")
43 #define ADAPTOR_LONGTEXT N_( \
44     "XVideo hardware adaptor to use. By default, VLC will " \
45     "use the first functional adaptor.")
46
47 #define FORMAT_TEXT N_("XVideo format id")
48 #define FORMAT_LONGTEXT N_( \
49     "XVideo image format id to use. By default, VLC will " \
50     "try to use the best match for the video being played.")
51
52 #define SHM_TEXT N_("Use shared memory")
53 #define SHM_LONGTEXT N_( \
54     "Use shared memory to communicate between VLC and the X server.")
55
56 static int  Open (vlc_object_t *);
57 static void Close (vlc_object_t *);
58
59 /*
60  * Module descriptor
61  */
62 vlc_module_begin ()
63     set_shortname (N_("XVideo"))
64     set_description (N_("XVideo output (XCB)"))
65     set_category (CAT_VIDEO)
66     set_subcategory (SUBCAT_VIDEO_VOUT)
67     set_capability ("vout display", 155)
68     set_callbacks (Open, Close)
69
70     add_integer ("xvideo-adaptor", -1, NULL,
71                  ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
72     add_integer ("xvideo-format-id", 0, NULL,
73                  FORMAT_TEXT, FORMAT_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", "xv", "xvideo", "xid")
77 vlc_module_end ()
78
79 #define MAX_PICTURES (128)
80
81 struct vout_display_sys_t
82 {
83     xcb_connection_t *conn;
84     vout_window_t *embed;/* VLC window */
85
86     xcb_cursor_t cursor; /* blank cursor */
87     xcb_window_t window; /* drawable X window */
88     xcb_gcontext_t gc;   /* context to put images */
89     xcb_xv_port_t port;  /* XVideo port */
90     uint32_t id;         /* XVideo format */
91     uint16_t width;      /* display width */
92     uint16_t height;     /* display height */
93     uint32_t data_size;  /* picture byte size (for non-SHM) */
94     bool     swap_uv;    /* U/V pointer must be swapped in a picture */
95     bool shm;            /* whether to use MIT-SHM */
96     bool visible;        /* whether it makes sense to draw at all */
97
98     xcb_xv_query_image_attributes_reply_t *att;
99     picture_pool_t *pool; /* picture pool */
100     picture_resource_t resource[MAX_PICTURES];
101 };
102
103 static picture_pool_t *Pool (vout_display_t *, unsigned);
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 *);
107
108 /**
109  * Check that the X server supports the XVideo extension.
110  */
111 static bool CheckXVideo (vout_display_t *vd, xcb_connection_t *conn)
112 {
113     xcb_xv_query_extension_reply_t *r;
114     xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
115     bool ok = false;
116
117     /* We need XVideo 2.2 for PutImage */
118     r = xcb_xv_query_extension_reply (conn, ck, NULL);
119     if (r == NULL)
120         msg_Dbg (vd, "XVideo extension not available");
121     else
122     if (r->major != 2)
123         msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" unknown",
124                  r->major, r->minor);
125     else
126     if (r->minor < 2)
127         msg_Dbg (vd, "XVideo extension v%"PRIu8".%"PRIu8" too old",
128                  r->major, r->minor);
129     else
130     {
131         msg_Dbg (vd, "using XVideo extension v%"PRIu8".%"PRIu8,
132                  r->major, r->minor);
133         ok = true;
134     }
135     free (r);
136     return ok;
137 }
138
139 static vlc_fourcc_t ParseFormat (vout_display_t *vd,
140                                  const xcb_xv_image_format_info_t *restrict f)
141 {
142     if (f->byte_order != ORDER && f->bpp != 8)
143         return 0; /* Argh! */
144
145     switch (f->type)
146     {
147       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
148         switch (f->num_planes)
149         {
150           case 1:
151             switch (f->bpp)
152             {
153               case 32:
154                 if (f->depth == 24)
155                     return VLC_CODEC_RGB32;
156                 if (f->depth == 32)
157                     return 0; /* ARGB -> VLC cannot do that currently */
158                 break;
159               case 24:
160                 if (f->depth == 24)
161                     return VLC_CODEC_RGB24;
162                 break;
163               case 16:
164                 if (f->depth == 16)
165                     return VLC_CODEC_RGB16;
166                 if (f->depth == 15)
167                     return VLC_CODEC_RGB15;
168                 break;
169               case 8:
170                 if (f->depth == 8)
171                     return VLC_CODEC_RGB8;
172                 break;
173             }
174             break;
175         }
176         msg_Err (vd, "unknown XVideo RGB format %"PRIx32" (%.4s)",
177                  f->id, f->guid);
178         msg_Dbg (vd, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
179                  "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
180         break;
181
182       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
183         if (f->u_sample_bits != f->v_sample_bits
184          || f->vhorz_u_period != f->vhorz_v_period
185          || f->vvert_u_period != f->vvert_v_period
186          || f->y_sample_bits != 8 || f->u_sample_bits != 8
187          || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
188             goto bad;
189         switch (f->num_planes)
190         {
191           case 1:
192             switch (f->bpp)
193             {
194               /*untested: case 24:
195                 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
196                     return VLC_CODEC_I444;
197                 break;*/
198               case 16:
199                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
200                 {
201                     if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
202                         return VLC_CODEC_YUYV;
203                     if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
204                         return VLC_CODEC_UYVY;
205                 }
206                 break;
207             }
208             break;
209           case 3:
210             switch (f->bpp)
211             {
212               case 12:
213                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
214                 {
215                     if (!strcmp ((const char *)f->vcomp_order, "YVU"))
216                         return VLC_CODEC_YV12;
217                     if (!strcmp ((const char *)f->vcomp_order, "YUV"))
218                         return VLC_CODEC_I420;
219                 }
220             }
221             break;
222         }
223     bad:
224         msg_Err (vd, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
225                  f->guid);
226         msg_Dbg (vd, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
227                  "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
228                  f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
229         msg_Dbg (vd, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
230                  "%"PRIu32"/%"PRIu32"/%"PRIu32,
231                  f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
232                  f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
233         msg_Warn (vd, " order: %.32s", f->vcomp_order);
234         break;
235     }
236     return 0;
237 }
238
239
240 static const xcb_xv_image_format_info_t *
241 FindFormat (vout_display_t *vd,
242             vlc_fourcc_t chroma, const video_format_t *fmt,
243             xcb_xv_port_t port,
244             const xcb_xv_list_image_formats_reply_t *list,
245             xcb_xv_query_image_attributes_reply_t **restrict pa)
246 {
247     xcb_connection_t *conn = vd->sys->conn;
248     const xcb_xv_image_format_info_t *f, *end;
249
250 #ifndef XCB_XV_OLD
251     f = xcb_xv_list_image_formats_format (list);
252 #else
253     f = (xcb_xv_image_format_info_t *) (list + 1);
254 #endif
255     end = f + xcb_xv_list_image_formats_format_length (list);
256     for (; f < end; f++)
257     {
258         if (chroma != ParseFormat (vd, f))
259             continue;
260
261         /* VLC pads scanline to 16 pixels internally */
262         unsigned width = fmt->i_width;
263         unsigned height = fmt->i_height;
264         xcb_xv_query_image_attributes_reply_t *i;
265         i = xcb_xv_query_image_attributes_reply (conn,
266             xcb_xv_query_image_attributes (conn, port, f->id,
267                                            width, height), NULL);
268         if (i == NULL)
269             continue;
270
271         if (i->width != width || i->height != height)
272         {
273             msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
274                       fmt->i_width, fmt->i_height,
275                       i->width, i->height);
276             var_Create (vd->p_libvlc, "xvideo-resolution-error", VLC_VAR_BOOL);
277             if (!var_GetBool (vd->p_libvlc, "xvideo-resolution-error"))
278             {
279                 dialog_FatalWait (vd, _("Video acceleration not available"),
280                     _("Your video output acceleration driver does not support "
281                       "the required resolution: %ux%u pixels. The maximum "
282                       "supported resolution is %"PRIu32"x%"PRIu32".\n"
283                       "Video output acceleration will be disabled. However, "
284                       "rendering videos with overly large resolution "
285                       "may cause severe performance degration."),
286                                   width, height, i->width, i->height);
287                 var_SetBool (vd->p_libvlc, "xvideo-resolution-error", true);
288             }
289             free (i);
290             continue;
291         }
292         *pa = i;
293         return f;
294     }
295     return NULL;
296 }
297
298
299 /**
300  * Probe the X server.
301  */
302 static int Open (vlc_object_t *obj)
303 {
304     vout_display_t *vd = (vout_display_t *)obj;
305     vout_display_sys_t *p_sys = malloc (sizeof (*p_sys));
306
307     if (!var_InheritBool (obj, "overlay"))
308         return VLC_EGENERIC;
309     if (p_sys == NULL)
310         return VLC_ENOMEM;
311
312     vd->sys = p_sys;
313
314     /* Connect to X */
315     xcb_connection_t *conn;
316     const xcb_screen_t *screen;
317     uint8_t depth;
318     p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
319     if (p_sys->embed == NULL)
320     {
321         free (p_sys);
322         return VLC_EGENERIC;
323     }
324
325     p_sys->conn = conn;
326     p_sys->att = NULL;
327     p_sys->pool = NULL;
328     p_sys->swap_uv = false;
329
330     if (!CheckXVideo (vd, conn))
331     {
332         msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
333         goto error;
334     }
335
336     p_sys->window = xcb_generate_id (conn);
337     xcb_pixmap_t pixmap = xcb_generate_id (conn);
338
339     /* Cache adaptors infos */
340     xcb_xv_query_adaptors_reply_t *adaptors =
341         xcb_xv_query_adaptors_reply (conn,
342             xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
343     if (adaptors == NULL)
344         goto error;
345
346     int forced_adaptor = var_InheritInteger (obj, "xvideo-adaptor");
347
348     /* */
349     video_format_t fmt = vd->fmt;
350     p_sys->port = 0;
351
352     xcb_xv_adaptor_info_iterator_t it;
353     for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
354          it.rem > 0;
355          xcb_xv_adaptor_info_next (&it))
356     {
357         const xcb_xv_adaptor_info_t *a = it.data;
358         char *name;
359
360         if (forced_adaptor != -1 && forced_adaptor != 0)
361         {
362             forced_adaptor--;
363             continue;
364         }
365
366         if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
367             continue;
368
369         xcb_xv_list_image_formats_reply_t *r =
370             xcb_xv_list_image_formats_reply (conn,
371                 xcb_xv_list_image_formats (conn, a->base_id), NULL);
372         if (r == NULL)
373             continue;
374
375         /* Look for an image format */
376         const xcb_xv_image_format_info_t *xfmt = NULL;
377         const vlc_fourcc_t *chromas, chromas_default[] = {
378             fmt.i_chroma,
379             VLC_CODEC_RGB32,
380             VLC_CODEC_RGB24,
381             VLC_CODEC_RGB16,
382             VLC_CODEC_RGB15,
383             VLC_CODEC_YUYV,
384             0
385         };
386         if (vlc_fourcc_IsYUV (fmt.i_chroma))
387             chromas = vlc_fourcc_GetYUVFallback (fmt.i_chroma);
388         else
389             chromas = chromas_default;
390
391         int forced_format_id = var_CreateGetInteger (obj, "xvideo-format-id");
392         vlc_fourcc_t chroma = forced_format_id;
393         xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
394         for (size_t i = 0; !xfmt && chromas[i]; i++)
395         {
396             chroma = chromas[i];
397
398             /* Oink oink! */
399             if ((chroma == VLC_CODEC_I420 || chroma == VLC_CODEC_YV12)
400              && a->name_size >= 4
401              && !memcmp ("OMAP", xcb_xv_adaptor_info_name (a), 4))
402             {
403                 msg_Dbg (vd, "skipping slow I420 format");
404                 continue; /* OMAP framebuffer sucks at YUV 4:2:0 */
405             }
406
407             xfmt = FindFormat (vd, chroma, &fmt, a->base_id, r, &p_sys->att);
408             if (xfmt != NULL) break;
409         }
410         free (r);
411         if (xfmt == NULL) /* No acceptable image formats */
412             continue;
413
414         p_sys->id = xfmt->id;
415         p_sys->swap_uv = vlc_fourcc_AreUVPlanesSwapped (fmt.i_chroma, chroma);
416         if (!p_sys->swap_uv)
417             fmt.i_chroma = chroma;
418         if (xfmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
419         {
420             fmt.i_rmask = xfmt->red_mask;
421             fmt.i_gmask = xfmt->green_mask;
422             fmt.i_bmask = xfmt->blue_mask;
423         }
424
425         /* Grab a port */
426         for (unsigned i = 0; i < a->num_ports; i++)
427         {
428              xcb_xv_port_t port = a->base_id + i;
429              xcb_xv_grab_port_reply_t *gr =
430                  xcb_xv_grab_port_reply (conn,
431                      xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
432              uint8_t result = gr ? gr->result : 0xff;
433
434              free (gr);
435              if (result == 0)
436              {
437                  p_sys->port = port;
438                  goto grabbed_port;
439              }
440              msg_Dbg (vd, "cannot grab port %"PRIu32": Xv error %"PRIu8, port,
441                       result);
442         }
443         continue; /* No usable port */
444
445     grabbed_port:
446         /* Found port - initialize selected format */
447         name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
448         if (name != NULL)
449         {
450             msg_Dbg (vd, "using adaptor %s", name);
451             free (name);
452         }
453         msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
454         msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
455
456         /* Look for an X11 visual, create a window */
457         xcb_xv_format_t *f = xcb_xv_adaptor_info_formats (a);
458         for (uint_fast16_t i = a->num_formats; i > 0; i--, f++)
459         {
460             if (f->depth != screen->root_depth)
461                 continue; /* this would fail anyway */
462
463             uint32_t mask =
464                 XCB_CW_BACK_PIXMAP |
465                 XCB_CW_BACK_PIXEL |
466                 XCB_CW_BORDER_PIXMAP |
467                 XCB_CW_BORDER_PIXEL |
468                 XCB_CW_EVENT_MASK |
469                 XCB_CW_COLORMAP;
470             const uint32_t list[] = {
471                 /* XCB_CW_BACK_PIXMAP */
472                 pixmap,
473                 /* XCB_CW_BACK_PIXEL */
474                 screen->black_pixel,
475                 /* XCB_CW_BORDER_PIXMAP */
476                 pixmap,
477                 /* XCB_CW_BORDER_PIXEL */
478                 screen->black_pixel,
479                 /* XCB_CW_EVENT_MASK */
480                 XCB_EVENT_MASK_VISIBILITY_CHANGE,
481                 /* XCB_CW_COLORMAP */
482                 screen->default_colormap,
483             };
484
485             xcb_void_cookie_t c;
486
487             xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
488             c = xcb_create_window_checked (conn, f->depth, p_sys->window,
489                  p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
490                  XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
491
492             if (!CheckError (vd, conn, "cannot create X11 window", c))
493             {
494                 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
495                          " (depth: %"PRIu8")", f->visual, f->depth);
496                 msg_Dbg (vd, "using X11 window 0x%08"PRIx32, p_sys->window);
497                 goto created_window;
498             }
499         }
500         xcb_xv_ungrab_port (conn, p_sys->port, XCB_CURRENT_TIME);
501         p_sys->port = 0;
502         msg_Dbg (vd, "no usable X11 visual");
503         continue; /* No workable XVideo format (visual/depth) */
504
505     created_window:
506         break;
507     }
508     free (adaptors);
509     if (!p_sys->port)
510     {
511         msg_Err (vd, "no available XVideo adaptor");
512         goto error;
513     }
514     else
515     {
516         xcb_map_window (conn, p_sys->window);
517
518         vout_display_place_t place;
519
520         vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
521         p_sys->width  = place.width;
522         p_sys->height = place.height;
523
524         /* */
525         const uint32_t values[] = {
526             place.x, place.y, place.width, place.height };
527         xcb_configure_window (conn, p_sys->window,
528                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
529                               XCB_CONFIG_WINDOW_WIDTH |
530                               XCB_CONFIG_WINDOW_HEIGHT,
531                               values);
532     }
533     p_sys->visible = false;
534
535     /* Create graphic context */
536     p_sys->gc = xcb_generate_id (conn);
537     xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
538     msg_Dbg (vd, "using X11 graphic context 0x%08"PRIx32, p_sys->gc);
539
540     /* Create cursor */
541     p_sys->cursor = CreateBlankCursor (conn, screen);
542
543     CheckSHM (obj, conn, &p_sys->shm);
544
545     /* */
546     vout_display_info_t info = vd->info;
547     info.has_pictures_invalid = false;
548     info.has_event_thread = true;
549
550     /* Setup vout_display_t once everything is fine */
551     vd->fmt = fmt;
552     vd->info = info;
553
554     vd->pool = Pool;
555     vd->prepare = NULL;
556     vd->display = Display;
557     vd->control = Control;
558     vd->manage = Manage;
559
560     /* */
561     bool is_fullscreen = vd->cfg->is_fullscreen;
562     if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
563         is_fullscreen = false;
564     vout_display_SendEventFullscreen (vd, is_fullscreen);
565     unsigned width, height;
566     if (!GetWindowSize (p_sys->embed, conn, &width, &height))
567         vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
568
569     return VLC_SUCCESS;
570
571 error:
572     Close (obj);
573     return VLC_EGENERIC;
574 }
575
576
577 /**
578  * Disconnect from the X server.
579  */
580 static void Close (vlc_object_t *obj)
581 {
582     vout_display_t *vd = (vout_display_t *)obj;
583     vout_display_sys_t *p_sys = vd->sys;
584
585     if (p_sys->pool)
586     {
587         for (unsigned i = 0; i < MAX_PICTURES; i++)
588         {
589             picture_resource_t *res = &p_sys->resource[i];
590
591             if (!res->p->p_pixels)
592                 break;
593             PictureResourceFree (res, NULL);
594         }
595         picture_pool_Delete (p_sys->pool);
596     }
597
598     /* show the default cursor */
599     xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
600                                   &(uint32_t) { XCB_CURSOR_NONE });
601     xcb_flush (p_sys->conn);
602
603     free (p_sys->att);
604     xcb_disconnect (p_sys->conn);
605     vout_display_DeleteWindow (vd, p_sys->embed);
606     free (p_sys);
607 }
608
609 static void PoolAlloc (vout_display_t *vd, unsigned requested_count)
610 {
611     vout_display_sys_t *p_sys = vd->sys;
612
613     memset (p_sys->resource, 0, sizeof(p_sys->resource));
614
615     const uint32_t *pitches= xcb_xv_query_image_attributes_pitches (p_sys->att);
616     const uint32_t *offsets= xcb_xv_query_image_attributes_offsets (p_sys->att);
617     const unsigned num_planes= __MIN(p_sys->att->num_planes, PICTURE_PLANE_MAX);
618     p_sys->data_size = p_sys->att->data_size;
619
620     picture_t *pic_array[MAX_PICTURES];
621     requested_count = __MIN(requested_count, MAX_PICTURES);
622
623     unsigned count;
624     for (count = 0; count < requested_count; count++)
625     {
626         picture_resource_t *res = &p_sys->resource[count];
627
628         for (unsigned i = 0; i < num_planes; i++)
629         {
630             uint32_t data_size;
631             data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
632
633             res->p[i].i_lines = (data_size - offsets[i]) / pitches[i];
634             res->p[i].i_pitch = pitches[i];
635         }
636
637         if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
638                                   p_sys->conn, p_sys->shm))
639             break;
640
641         /* Allocate further planes as specified by XVideo */
642         /* We assume that offsets[0] is zero */
643         for (unsigned i = 1; i < num_planes; i++)
644             res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
645
646         if (p_sys->swap_uv)
647         {   /* YVU: swap U and V planes */
648             uint8_t *buf = res->p[2].p_pixels;
649             res->p[2].p_pixels = res->p[1].p_pixels;
650             res->p[1].p_pixels = buf;
651         }
652
653         pic_array[count] = picture_NewFromResource (&vd->fmt, res);
654         if (!pic_array[count])
655         {
656             PictureResourceFree (res, p_sys->conn);
657             memset (res, 0, sizeof(*res));
658             break;
659         }
660     }
661
662     if (count == 0)
663         return;
664
665     p_sys->pool = picture_pool_New (count, pic_array);
666     /* TODO release picture resources if NULL */
667     xcb_flush (p_sys->conn);
668 }
669
670 /**
671  * Return a direct buffer
672  */
673 static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
674 {
675     vout_display_sys_t *p_sys = vd->sys;
676
677     if (!p_sys->pool)
678         PoolAlloc (vd, requested_count);
679
680     return p_sys->pool;
681 }
682
683 /**
684  * Sends an image to the X server.
685  */
686 static void Display (vout_display_t *vd, picture_t *pic)
687 {
688     vout_display_sys_t *p_sys = vd->sys;
689     xcb_shm_seg_t segment = pic->p_sys->segment;
690     xcb_void_cookie_t ck;
691
692     if (!p_sys->visible)
693         goto out;
694     xcb_force_screen_saver (p_sys->conn, XCB_SCREEN_SAVER_RESET);
695
696     if (segment)
697         ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
698                               p_sys->window, p_sys->gc, segment, p_sys->id, 0,
699                    /* Src: */ vd->source.i_x_offset,
700                               vd->source.i_y_offset,
701                               vd->source.i_visible_width,
702                               vd->source.i_visible_height,
703                    /* Dst: */ 0, 0, p_sys->width, p_sys->height,
704                 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
705                               pic->p->i_lines, false);
706     else
707         ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
708                           p_sys->gc, p_sys->id,
709                           vd->source.i_x_offset,
710                           vd->source.i_y_offset,
711                           vd->source.i_visible_width,
712                           vd->source.i_visible_height,
713                           0, 0, p_sys->width, p_sys->height,
714                           pic->p->i_pitch / pic->p->i_pixel_pitch,
715                           pic->p->i_lines,
716                           p_sys->data_size, pic->p->p_pixels);
717
718     /* Wait for reply. See x11.c for rationale. */
719     xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
720     if (e != NULL)
721     {
722         msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
723         free (e);
724     }
725 out:
726     picture_Release (pic);
727 }
728
729 static int Control (vout_display_t *vd, int query, va_list ap)
730 {
731     vout_display_sys_t *p_sys = vd->sys;
732
733     switch (query)
734     {
735     case VOUT_DISPLAY_CHANGE_FULLSCREEN:
736     {
737         const vout_display_cfg_t *c = va_arg (ap, const vout_display_cfg_t *);
738         return vout_window_SetFullScreen (p_sys->embed, c->is_fullscreen);
739     }
740
741     case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
742     case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
743     case VOUT_DISPLAY_CHANGE_ZOOM:
744     case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
745     case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
746     {
747         const vout_display_cfg_t *cfg;
748         const video_format_t *source;
749         bool is_forced = false;
750
751         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
752          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
753         {
754             source = (const video_format_t *)va_arg (ap, const video_format_t *);
755             cfg = vd->cfg;
756         }
757         else
758         {
759             source = &vd->source;
760             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
761             if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
762                 is_forced = (bool)va_arg (ap, int);
763         }
764
765         /* */
766         if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
767          && is_forced
768          && (cfg->display.width  != vd->cfg->display.width
769            ||cfg->display.height != vd->cfg->display.height)
770          && vout_window_SetSize (p_sys->embed,
771                                   cfg->display.width,
772                                   cfg->display.height))
773             return VLC_EGENERIC;
774
775         vout_display_place_t place;
776         vout_display_PlacePicture (&place, source, cfg, false);
777         p_sys->width  = place.width;
778         p_sys->height = place.height;
779
780         /* Move the picture within the window */
781         const uint32_t values[] = { place.x, place.y,
782                                     place.width, place.height, };
783         xcb_configure_window (p_sys->conn, p_sys->window,
784                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
785                             | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
786                               values);
787         xcb_flush (p_sys->conn);
788         return VLC_SUCCESS;
789     }
790     case VOUT_DISPLAY_CHANGE_WINDOW_STATE:
791     {
792         unsigned state = va_arg (ap, unsigned);
793         return vout_window_SetState (p_sys->embed, state);
794     }
795
796     /* Hide the mouse. It will be send when
797      * vout_display_t::info.b_hide_mouse is false */
798     case VOUT_DISPLAY_HIDE_MOUSE:
799         xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid,
800                                   XCB_CW_CURSOR, &(uint32_t){ p_sys->cursor });
801         return VLC_SUCCESS;
802     case VOUT_DISPLAY_RESET_PICTURES:
803         assert(0);
804     default:
805         msg_Err (vd, "Unknown request in XCB vout display");
806         return VLC_EGENERIC;
807     }
808 }
809
810 static void Manage (vout_display_t *vd)
811 {
812     vout_display_sys_t *p_sys = vd->sys;
813
814     ManageEvent (vd, p_sys->conn, &p_sys->visible);
815 }
816