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