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