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