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