]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/xvideo.c
XCB: handle planar YVU (YV12) properly (untested)
[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.0
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 Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  ****************************************************************************/
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.h>
37 #include <vlc_window.h>
38
39 #include "xcb_vlc.h"
40
41 #define DISPLAY_TEXT N_("X11 display")
42 #define DISPLAY_LONGTEXT N_( \
43     "X11 hardware display to use. By default VLC will " \
44     "use the value of the DISPLAY environment variable.")
45
46 #define SHM_TEXT N_("Use shared memory")
47 #define SHM_LONGTEXT N_( \
48     "Use shared memory to communicate between VLC and the X server.")
49
50 static int  Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
52
53 /*
54  * Module descriptor
55  */
56 vlc_module_begin ()
57     set_shortname (N_("XVideo"))
58     set_description (N_("(Experimental) XVideo output"))
59     set_category (CAT_VIDEO)
60     set_subcategory (SUBCAT_VIDEO_VOUT)
61     set_capability ("video output", 0)
62     set_callbacks (Open, Close)
63
64     add_string ("x11-display", NULL, NULL,
65                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
66     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
67     add_shortcut ("xcb-xv")
68 vlc_module_end ()
69
70 struct vout_sys_t
71 {
72     xcb_connection_t *conn;
73     xcb_xv_query_adaptors_reply_t *adaptors;
74     vout_window_t *embed;/* VLC window */
75
76     xcb_window_t window; /* drawable X window */
77     xcb_gcontext_t gc;   /* context to put images */
78     xcb_xv_port_t port;  /* XVideo port */
79     uint32_t id;         /* XVideo format */
80     uint16_t width;      /* display width */
81     uint16_t height;     /* display height */
82     bool shm;            /* whether to use MIT-SHM */
83 };
84
85 static int Init (vout_thread_t *);
86 static void Deinit (vout_thread_t *);
87 static void Display (vout_thread_t *, picture_t *);
88 static int Manage (vout_thread_t *);
89
90 int CheckError (vout_thread_t *vout, const char *str, xcb_void_cookie_t ck)
91 {
92     xcb_generic_error_t *err;
93
94     err = xcb_request_check (vout->p_sys->conn, ck);
95     if (err)
96     {
97         msg_Err (vout, "%s: X11 error %d", str, err->error_code);
98         return VLC_EGENERIC;
99     }
100     return VLC_SUCCESS;
101 }
102
103 /**
104  * Check that the X server supports the XVideo extension.
105  */
106 static bool CheckXVideo (vout_thread_t *vout, xcb_connection_t *conn)
107 {
108     xcb_xv_query_extension_reply_t *r;
109     xcb_xv_query_extension_cookie_t ck = xcb_xv_query_extension (conn);
110     bool ok = false;
111
112     r = xcb_xv_query_extension_reply (conn, ck, NULL);
113     if (r != NULL)
114     {   /* We need XVideo 2.2 for PutImage */
115         if ((r->major > 2) || (r->major == 2 && r->minor >= 2))
116         {
117             msg_Dbg (vout, "using XVideo extension v%"PRIu8".%"PRIu8,
118                      r->major, r->minor);
119             ok = true;
120         }
121         else
122             msg_Dbg (vout, "XVideo extension too old (v%"PRIu8".%"PRIu8,
123                      r->major, r->minor);
124         free (r);
125     }
126     else
127         msg_Dbg (vout, "XVideo extension not available");
128     return ok;
129 }
130
131 /**
132  * Get a list of XVideo adaptors for a given window.
133  */
134 static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
135                                                    xcb_connection_t *conn)
136 {
137     xcb_xv_query_adaptors_cookie_t ck;
138
139     ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
140     return xcb_xv_query_adaptors_reply (conn, ck, NULL);
141 }
142
143 #define p_vout vout
144
145 /**
146  * Probe the X server.
147  */
148 static int Open (vlc_object_t *obj)
149 {
150     vout_thread_t *vout = (vout_thread_t *)obj;
151     vout_sys_t *p_sys = malloc (sizeof (*p_sys));
152     if (p_sys == NULL)
153         return VLC_ENOMEM;
154
155     vout->p_sys = p_sys;
156
157     /* Connect to X */
158     p_sys->conn = Connect (obj);
159     if (p_sys->conn == NULL)
160         return VLC_EGENERIC;
161
162     if (!CheckXVideo (vout, p_sys->conn))
163     {
164         msg_Warn (vout, "Please enable XVideo 2.2 for faster video display");
165         xcb_disconnect (p_sys->conn);
166         return VLC_EGENERIC;
167     }
168
169     const xcb_screen_t *screen;
170     p_sys->embed = GetWindow (vout, p_sys->conn, &screen, &p_sys->shm);
171     if (p_sys->embed == NULL)
172     {
173         xcb_disconnect (p_sys->conn);
174         return VLC_EGENERIC;
175     }
176
177     /* Cache adaptors infos */
178     p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
179     if (p_sys->adaptors == NULL)
180         goto error;
181
182     /* Create window */
183     {
184         const uint32_t mask =
185             /* XCB_CW_EVENT_MASK */
186             XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
187             XCB_EVENT_MASK_POINTER_MOTION;
188         xcb_void_cookie_t c;
189         xcb_window_t window = xcb_generate_id (p_sys->conn);
190
191         c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
192                                        p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
193                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
194                                        screen->root_visual,
195                                        XCB_CW_EVENT_MASK, &mask);
196         if (CheckError (vout, "cannot create X11 window", c))
197             goto error;
198         p_sys->window = window;
199         msg_Dbg (vout, "using X11 window %08"PRIx32, p_sys->window);
200         xcb_map_window (p_sys->conn, window);
201     }
202
203     p_sys->gc = xcb_generate_id (p_sys->conn);
204     xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
205     msg_Dbg (vout, "using X11 graphic context %08"PRIx32, p_sys->gc);
206
207     vout->pf_init = Init;
208     vout->pf_end = Deinit;
209     vout->pf_display = Display;
210     vout->pf_manage = Manage;
211     return VLC_SUCCESS;
212
213 error:
214     Close (obj);
215     return VLC_EGENERIC;
216 }
217
218
219 /**
220  * Disconnect from the X server.
221  */
222 static void Close (vlc_object_t *obj)
223 {
224     vout_thread_t *vout = (vout_thread_t *)obj;
225     vout_sys_t *p_sys = vout->p_sys;
226
227     free (p_sys->adaptors);
228     vout_ReleaseWindow (p_sys->embed);
229     xcb_disconnect (p_sys->conn);
230     free (p_sys);
231 }
232
233 static vlc_fourcc_t ParseFormat (vout_thread_t *vout,
234                                  const xcb_xv_image_format_info_t *restrict f)
235 {
236     if (f->byte_order != ORDER && f->bpp != 8)
237         return 0; /* Argh! */
238
239     switch (f->type)
240     {
241       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB:
242         switch (f->num_planes)
243         {
244           case 1:
245             switch (f->bpp)
246             {
247               case 32:
248                 if (f->depth == 24)
249                     return VLC_FOURCC ('R', 'V', '3', '2');
250                 break;
251               case 24:
252                 if (f->depth == 24)
253                     return VLC_FOURCC ('R', 'V', '2', '4');
254                 break;
255               case 16:
256                 if (f->depth == 16)
257                     return VLC_FOURCC ('R', 'V', '1', '6');
258                 if (f->depth == 15)
259                     return VLC_FOURCC ('R', 'V', '1', '5');
260                 break;
261               case 8:
262                 if (f->depth == 8)
263                     return VLC_FOURCC ('R', 'G', 'B', '2');
264                 break;
265             }
266             break;
267         }
268         msg_Err (vout, "unknown XVideo RGB format %"PRIx32" (%.4s)",
269                  f->id, f->guid);
270         msg_Dbg (vout, " %"PRIu8" planes, %"PRIu8" bits/pixel, "
271                  "depth %"PRIu8, f->num_planes, f->bpp, f->depth);
272         break;
273
274       case XCB_XV_IMAGE_FORMAT_INFO_TYPE_YUV:
275         if (f->u_sample_bits != f->v_sample_bits
276          || f->vhorz_u_period != f->vhorz_v_period
277          || f->vvert_u_period != f->vvert_v_period
278          || f->y_sample_bits != 8 || f->u_sample_bits != 8
279          || f->vhorz_y_period != 1 || f->vvert_y_period != 1)
280             goto bad;
281         switch (f->num_planes)
282         {
283           case 1:
284             switch (f->bpp)
285             {
286               /*untested: case 24:
287                 if (f->vhorz_u_period == 1 && f->vvert_u_period == 1)
288                     return VLC_FOURCC ('I', '4', '4', '4');
289                 break;*/
290               case 16:
291                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 1)
292                 {
293                     if (!strcmp ((const char *)f->vcomp_order, "YUYV"))
294                         return VLC_FOURCC ('Y', 'U', 'Y', '2');
295                     if (!strcmp ((const char *)f->vcomp_order, "UYVY"))
296                         return VLC_FOURCC ('U', 'Y', 'V', 'Y');
297                 }
298                 break;
299             }
300             break;
301           case 3:
302             switch (f->bpp)
303             {
304               case 12:
305                 if (f->vhorz_u_period == 2 && f->vvert_u_period == 2)
306                 {
307                     if (!strcmp ((const char *)f->vcomp_order, "YVU"))
308                         return VLC_FOURCC ('Y', 'V', '1', '2');
309                     if (!strcmp ((const char *)f->vcomp_order, "YUV"))
310                         return VLC_FOURCC ('I', '4', '2', '0');
311                 }
312             }
313             break;
314         }
315     bad:
316         msg_Err (vout, "unknown XVideo YUV format %"PRIx32" (%.4s)", f->id,
317                  f->guid);
318         msg_Dbg (vout, " %"PRIu8" planes, %"PRIu32" bits/pixel, "
319                  "%"PRIu32"/%"PRIu32"/%"PRIu32" bits/sample", f->num_planes,
320                  f->bpp, f->y_sample_bits, f->u_sample_bits, f->v_sample_bits);
321         msg_Dbg (vout, " period: %"PRIu32"/%"PRIu32"/%"PRIu32"x"
322                  "%"PRIu32"/%"PRIu32"/%"PRIu32,
323                  f->vhorz_y_period, f->vhorz_u_period, f->vhorz_v_period,
324                  f->vvert_y_period, f->vvert_u_period, f->vvert_v_period);
325         msg_Warn (vout, " order: %.32s", f->vcomp_order);
326         break;
327     }
328     return 0;
329 }
330
331
332 static const xcb_xv_image_format_info_t *
333 FindFormat (vout_thread_t *vout, vlc_fourcc_t chroma, xcb_xv_port_t port,
334             const xcb_xv_list_image_formats_reply_t *list,
335             xcb_xv_query_image_attributes_reply_t **restrict pa)
336 {
337     xcb_connection_t *conn = vout->p_sys->conn;
338     const xcb_xv_image_format_info_t *f, *end;
339
340     f = xcb_xv_list_image_formats_format (list);
341     end = f + xcb_xv_list_image_formats_format_length (list);
342     for (; f < end; f++)
343     {
344         if (chroma != ParseFormat (vout, f))
345             continue;
346
347         xcb_xv_query_image_attributes_reply_t *i;
348         i = xcb_xv_query_image_attributes_reply (conn,
349             xcb_xv_query_image_attributes (conn, port, f->id,
350                 vout->fmt_in.i_width, vout->fmt_in.i_height), NULL);
351         if (i == NULL)
352             continue;
353
354         if (i->width != vout->fmt_in.i_width
355          || i->height != vout->fmt_in.i_height)
356         {
357             msg_Warn (vout, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
358                       vout->fmt_in.i_width, vout->fmt_in.i_height,
359                       i->width, i->height);
360             free (i);
361             continue;
362         }
363         *pa = i;
364         return f;
365     }
366     return NULL;
367 }
368
369 /**
370  * Allocate drawable window and picture buffers.
371  */
372 static int Init (vout_thread_t *vout)
373 {
374     vout_sys_t *p_sys = vout->p_sys;
375     xcb_xv_query_image_attributes_reply_t *att = NULL;
376     bool swap_planes = false; /* whether X wants V before U */
377
378     /* FIXME: check max image size */
379     xcb_xv_adaptor_info_iterator_t it;
380     for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
381          it.rem > 0;
382          xcb_xv_adaptor_info_next (&it))
383     {
384         const xcb_xv_adaptor_info_t *a = it.data;
385
386         /* FIXME: Open() should fail if none of the ports are usable to VLC */
387         if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
388             continue;
389
390         xcb_xv_list_image_formats_reply_t *r;
391         r = xcb_xv_list_image_formats_reply (p_sys->conn,
392             xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
393         if (r == NULL)
394             continue;
395
396         const xcb_xv_image_format_info_t *fmt;
397
398         /* Video chroma in preference order */
399         const vlc_fourcc_t chromas[] = {
400             vout->fmt_in.i_chroma,
401             VLC_FOURCC ('Y', 'U', 'Y', '2'),
402             VLC_FOURCC ('R', 'V', '2', '4'),
403             VLC_FOURCC ('R', 'V', '1', '5'),
404         };
405         for (size_t i = 0; i < sizeof (chromas) / sizeof (chromas[0]); i++)
406         {
407             vlc_fourcc_t chroma = chromas[i];
408             fmt = FindFormat (vout, chroma, a->base_id, r, &att);
409             if (fmt != NULL)
410             {
411                 vout->output.i_chroma = chroma;
412                 goto found_format;
413             }
414         }
415         free (r);
416         continue;
417
418     found_format:
419         /* TODO: grab port */
420         p_sys->port = a->base_id;
421         msg_Dbg (vout, "using port %"PRIu32, p_sys->port);
422
423         p_sys->id = fmt->id;
424         msg_Dbg (vout, "using image format 0x%"PRIx32, p_sys->id);
425         if (fmt->type == XCB_XV_IMAGE_FORMAT_INFO_TYPE_RGB)
426         {
427             vout->fmt_out.i_rmask = vout->output.i_rmask = fmt->red_mask;
428             vout->fmt_out.i_gmask = vout->output.i_gmask = fmt->green_mask;
429             vout->fmt_out.i_bmask = vout->output.i_bmask = fmt->blue_mask;
430         }
431         else
432         if (fmt->num_planes == 3)
433             swap_planes = !strcmp ((const char *)fmt->vcomp_order, "YVU");
434         free (r);
435         goto found_adaptor;
436     }
437     msg_Err (vout, "no available XVideo adaptor");
438     return VLC_EGENERIC; /* no usable adaptor */
439
440     /* Allocate picture buffers */
441     const uint32_t *offsets;
442 found_adaptor:
443     offsets = xcb_xv_query_image_attributes_offsets (att);
444
445     I_OUTPUTPICTURES = 0;
446     for (size_t index = 0; I_OUTPUTPICTURES < 2; index++)
447     {
448         picture_t *pic = vout->p_picture + index;
449
450         if (index > sizeof (vout->p_picture) / sizeof (pic))
451             break;
452         if (pic->i_status != FREE_PICTURE)
453             continue;
454
455         vout_InitPicture (vout, pic, vout->output.i_chroma,
456                           att->width, att->height,
457                           vout->fmt_in.i_aspect);
458         if (PictureAlloc (vout, pic, att->data_size,
459                           p_sys->shm ? p_sys->conn : NULL))
460             break;
461         /* Allocate further planes as specified by XVideo */
462         /* We assume that offsets[0] is zero */
463         for (int i = 1; i < pic->i_planes; i++)
464              pic->p[i].p_pixels =
465                  pic->p->p_pixels + offsets[swap_planes ? (3 - i) : i];
466         PP_OUTPUTPICTURE[I_OUTPUTPICTURES++] = pic;
467     }
468     free (att);
469
470     unsigned x, y, width, height;
471
472     if (GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
473         return VLC_EGENERIC;
474     vout_PlacePicture (vout, width, height, &x, &y, &width, &height);
475
476     const uint32_t values[] = { x, y, width, height, };
477     xcb_configure_window (p_sys->conn, p_sys->window,
478                           XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
479                           XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
480                           values);
481     xcb_flush (p_sys->conn);
482     p_sys->height = height;
483     p_sys->width = width;
484
485     vout->fmt_out.i_chroma = vout->output.i_chroma;
486     vout->fmt_out.i_visible_width = vout->fmt_in.i_visible_width;
487     vout->fmt_out.i_visible_height = vout->fmt_in.i_visible_height;
488     vout->fmt_out.i_sar_num = vout->fmt_out.i_sar_den = 1;
489
490     vout->output.i_width = vout->fmt_out.i_width = vout->fmt_in.i_width;
491     vout->output.i_height = vout->fmt_out.i_height = vout->fmt_in.i_height;
492     vout->fmt_out.i_x_offset = vout->fmt_in.i_x_offset;
493     p_vout->fmt_out.i_y_offset = vout->fmt_in.i_y_offset;
494
495     assert (height > 0);
496     vout->output.i_aspect = vout->fmt_out.i_aspect =
497         width * VOUT_ASPECT_FACTOR / height;
498
499     return VLC_SUCCESS;
500 }
501
502 /**
503  * Free picture buffers.
504  */
505 static void Deinit (vout_thread_t *vout)
506 {
507     vout_sys_t *p_sys = vout->p_sys;
508
509     for (int i = 0; i < I_OUTPUTPICTURES; i++)
510         PictureFree (PP_OUTPUTPICTURE[i], p_sys->conn);
511 }
512
513 /**
514  * Sends an image to the X server.
515  */
516 static void Display (vout_thread_t *vout, picture_t *pic)
517 {
518     vout_sys_t *p_sys = vout->p_sys;
519     xcb_shm_seg_t segment = (uintptr_t)pic->p_sys;
520
521     if (segment)
522         xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
523                               p_sys->gc, segment, p_sys->id, 0,
524                               /* Src: */ 0, 0,
525                               pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
526                               pic->p->i_visible_lines,
527                               /* Dst: */ 0, 0, p_sys->width, p_sys->height,
528                               /* Memory: */
529                               pic->p->i_pitch / pic->p->i_pixel_pitch,
530                               pic->p->i_lines, false);
531     else
532         xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
533                           p_sys->gc, p_sys->id,
534                           0, 0,
535                           pic->p->i_visible_pitch / pic->p->i_pixel_pitch,
536                           pic->p->i_visible_lines,
537                           0, 0, p_sys->width, p_sys->height,
538                           pic->p->i_pitch / pic->p->i_pixel_pitch,
539                           pic->p->i_lines,
540                           pic->p->i_pitch * pic->p->i_lines, pic->p->p_pixels);
541     xcb_flush (p_sys->conn);
542 }
543
544 /**
545  * Process incoming X events.
546  */
547 static int Manage (vout_thread_t *vout)
548 {
549     vout_sys_t *p_sys = vout->p_sys;
550     xcb_generic_event_t *ev;
551
552     while ((ev = xcb_poll_for_event (p_sys->conn)) != NULL)
553         ProcessEvent (vout, p_sys->conn, p_sys->window, ev);
554
555     if (xcb_connection_has_error (p_sys->conn))
556     {
557         msg_Err (vout, "X server failure");
558         return VLC_EGENERIC;
559     }
560     return VLC_SUCCESS;
561 }
562
563 void
564 HandleParentStructure (vout_thread_t *vout, xcb_connection_t *conn,
565                        xcb_window_t xid, xcb_configure_notify_event_t *ev)
566 {
567     unsigned width, height, x, y;
568
569     vout_PlacePicture (vout, ev->width, ev->height, &x, &y, &width, &height);
570
571     /* Move the picture within the window */
572     const uint32_t values[] = { x, y, width, height, };
573     xcb_configure_window (conn, xid,
574                           XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y
575                         | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
576                           values);
577     vout->p_sys->width = width;
578     vout->p_sys->height = height;
579 }