]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/xvideo.c
XCB-XVideo: pad scanlines like VLC does
[vlc] / modules / video_output / xcb / xvideo.c
index 5c07cde937bd88bd19f8e3a221db91ac68ebbf06..1239c076efeb1e4725933fb1ce6209c4794f2dbf 100644 (file)
 
 #define DISPLAY_TEXT N_("X11 display")
 #define DISPLAY_LONGTEXT N_( \
-    "X11 hardware display to use. By default VLC will " \
+    "X11 hardware display to use. By default, VLC will " \
     "use the value of the DISPLAY environment variable.")
 
+#define ADAPTOR_TEXT N_("XVideo adaptor number")
+#define ADAPTOR_LONGTEXT N_( \
+    "XVideo hardware adaptor to use. By default, VLC will " \
+    "use the first functional adaptor.")
+
 #define SHM_TEXT N_("Use shared memory")
 #define SHM_LONGTEXT N_( \
     "Use shared memory to communicate between VLC and the X server.")
@@ -64,10 +69,13 @@ vlc_module_begin ()
     add_string ("x11-display", NULL, NULL,
                 DISPLAY_TEXT, DISPLAY_LONGTEXT, true)
         add_deprecated_alias ("xvideo-display")
+    add_integer ("xvideo-adaptor", -1, NULL,
+                 ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
     add_bool ("x11-shm", true, NULL, SHM_TEXT, SHM_LONGTEXT, true)
         add_deprecated_alias ("xvideo-shm")
     add_shortcut ("xcb-xv")
     add_shortcut ("xv")
+    add_shortcut ("xvideo")
 vlc_module_end ()
 
 #define MAX_PICTURES (VOUT_MAX_PICTURES)
@@ -75,7 +83,6 @@ vlc_module_end ()
 struct vout_display_sys_t
 {
     xcb_connection_t *conn;
-    xcb_xv_query_adaptors_reply_t *adaptors;
     vout_window_t *embed;/* VLC window */
 
     xcb_window_t window; /* drawable X window */
@@ -245,15 +252,16 @@ FindFormat (vout_display_t *vd,
         if (chroma != ParseFormat (vd, f))
             continue;
 
+        /* VLC pads scanline to 16 pixels internally */
+        unsigned width = (fmt->i_width + 15) & ~15;
         xcb_xv_query_image_attributes_reply_t *i;
         i = xcb_xv_query_image_attributes_reply (conn,
             xcb_xv_query_image_attributes (conn, port, f->id,
-                fmt->i_width, fmt->i_height), NULL);
+                width, fmt->i_height), NULL);
         if (i == NULL)
             continue;
 
-        if (i->width != fmt->i_width
-         || i->height != fmt->i_height)
+        if (i->width != width || i->height != fmt->i_height)
         {
             msg_Warn (vd, "incompatible size %ux%u -> %"PRIu32"x%"PRIu32,
                       fmt->i_width, fmt->i_height,
@@ -268,18 +276,6 @@ FindFormat (vout_display_t *vd,
 }
 
 
-/**
- * Get a list of XVideo adaptors for a given window.
- */
-static xcb_xv_query_adaptors_reply_t *GetAdaptors (vout_window_t *wnd,
-                                                   xcb_connection_t *conn)
-{
-    xcb_xv_query_adaptors_cookie_t ck;
-
-    ck = xcb_xv_query_adaptors (conn, wnd->handle.xid);
-    return xcb_xv_query_adaptors_reply (conn, ck, NULL);
-}
-
 /**
  * Probe the X server.
  */
@@ -293,26 +289,27 @@ static int Open (vlc_object_t *obj)
     vd->sys = p_sys;
 
     /* Connect to X */
-    p_sys->conn = Connect (obj);
-    if (p_sys->conn == NULL)
+    xcb_connection_t *conn = Connect (obj);
+    if (conn == NULL)
     {
         free (p_sys);
         return VLC_EGENERIC;
     }
+    p_sys->conn = conn;
 
-    if (!CheckXVideo (vd, p_sys->conn))
+    if (!CheckXVideo (vd, conn))
     {
         msg_Warn (vd, "Please enable XVideo 2.2 for faster video display");
-        xcb_disconnect (p_sys->conn);
+        xcb_disconnect (conn);
         free (p_sys);
         return VLC_EGENERIC;
     }
 
     const xcb_screen_t *screen;
-    p_sys->embed = GetWindow (vd, p_sys->conn, &screen, &p_sys->shm);
+    p_sys->embed = GetWindow (vd, conn, &screen, &p_sys->shm);
     if (p_sys->embed == NULL)
     {
-        xcb_disconnect (p_sys->conn);
+        xcb_disconnect (conn);
         free (p_sys);
         return VLC_EGENERIC;
     }
@@ -322,29 +319,38 @@ static int Open (vlc_object_t *obj)
     p_sys->pool = NULL;
 
     /* Cache adaptors infos */
-    p_sys->adaptors = GetAdaptors (p_sys->embed, p_sys->conn);
-    if (p_sys->adaptors == NULL)
+    xcb_xv_query_adaptors_reply_t *adaptors =
+        xcb_xv_query_adaptors_reply (conn,
+            xcb_xv_query_adaptors (conn, p_sys->embed->handle.xid), NULL);
+    if (adaptors == NULL)
         goto error;
 
+    int forced_adaptor = var_CreateGetInteger (obj, "xvideo-adaptor");
+
     /* */
     video_format_t fmt = vd->fmt;
     bool found_adaptor = false;
 
     /* FIXME: check max image size */
     xcb_xv_adaptor_info_iterator_t it;
-    for (it = xcb_xv_query_adaptors_info_iterator (p_sys->adaptors);
+    for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
          it.rem > 0;
          xcb_xv_adaptor_info_next (&it))
     {
         const xcb_xv_adaptor_info_t *a = it.data;
 
-        /* FIXME: Open() should fail if none of the ports are usable to VLC */
+        if (forced_adaptor != -1 && forced_adaptor != 0)
+        {
+            forced_adaptor--;
+            continue;
+        }
+
         if (!(a->type & XCB_XV_TYPE_IMAGE_MASK))
             continue;
 
-        xcb_xv_list_image_formats_reply_t *r;
-        r = xcb_xv_list_image_formats_reply (p_sys->conn,
-            xcb_xv_list_image_formats (p_sys->conn, a->base_id), NULL);
+        xcb_xv_list_image_formats_reply_t *r =
+            xcb_xv_list_image_formats_reply (conn,
+                xcb_xv_list_image_formats (conn, a->base_id), NULL);
         if (r == NULL)
             continue;
 
@@ -353,7 +359,9 @@ static int Open (vlc_object_t *obj)
         /* */
         const vlc_fourcc_t *chromas, chromas_default[] = {
             fmt.i_chroma,
+            VLC_CODEC_RGB32,
             VLC_CODEC_RGB24,
+            VLC_CODEC_RGB16,
             VLC_CODEC_RGB15,
             VLC_CODEC_YUYV,
             0
@@ -383,9 +391,8 @@ static int Open (vlc_object_t *obj)
         {
              xcb_xv_port_t port = a->base_id + i;
              xcb_xv_grab_port_reply_t *gr =
-                 xcb_xv_grab_port_reply (p_sys->conn,
-                     xcb_xv_grab_port (p_sys->conn, port,
-                                       XCB_CURRENT_TIME), NULL);
+                 xcb_xv_grab_port_reply (conn,
+                     xcb_xv_grab_port (conn, port, XCB_CURRENT_TIME), NULL);
              uint8_t result = gr ? gr->result : 0xff;
 
              free (gr);
@@ -409,14 +416,11 @@ static int Open (vlc_object_t *obj)
             fmt.i_gmask = xfmt->green_mask;
             fmt.i_bmask = xfmt->blue_mask;
         }
-        else
-        if (xfmt->num_planes == 3
-         && !strcmp ((const char *)xfmt->vcomp_order, "YVU"))
-            fmt.i_chroma = VLC_CODEC_YV12;
         free (r);
         found_adaptor = true;
         break;
     }
+    free (adaptors);
     if (!found_adaptor)
     {
         msg_Err (vd, "no available XVideo adaptor");
@@ -430,18 +434,18 @@ static int Open (vlc_object_t *obj)
             XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
             XCB_EVENT_MASK_POINTER_MOTION;
         xcb_void_cookie_t c;
-        xcb_window_t window = xcb_generate_id (p_sys->conn);
+        xcb_window_t window = xcb_generate_id (conn);
 
-        c = xcb_create_window_checked (p_sys->conn, screen->root_depth, window,
+        c = xcb_create_window_checked (conn, screen->root_depth, window,
                                        p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
                                        XCB_WINDOW_CLASS_INPUT_OUTPUT,
                                        screen->root_visual,
                                        XCB_CW_EVENT_MASK, &mask);
-        if (CheckError (vd, p_sys->conn, "cannot create X11 window", c))
+        if (CheckError (vd, conn, "cannot create X11 window", c))
             goto error;
         p_sys->window = window;
-        msg_Dbg (vd, "using X11 window %08"PRIx32, p_sys->window);
-        xcb_map_window (p_sys->conn, window);
+        msg_Dbg (vd, "using X11 window %08"PRIx32, window);
+        xcb_map_window (conn, window);
 
         vout_display_place_t place;
 
@@ -451,15 +455,15 @@ static int Open (vlc_object_t *obj)
 
         /* */
         const uint32_t values[] = { place.x, place.y, place.width, place.height };
-        xcb_configure_window (p_sys->conn, p_sys->window,
+        xcb_configure_window (conn, window,
                               XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
                               XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
                               values);
     }
 
     /* Create graphic context */
-    p_sys->gc = xcb_generate_id (p_sys->conn);
-    xcb_create_gc (p_sys->conn, p_sys->gc, p_sys->window, 0, NULL);
+    p_sys->gc = xcb_generate_id (conn);
+    xcb_create_gc (conn, p_sys->gc, p_sys->window, 0, NULL);
     msg_Dbg (vd, "using X11 graphic context %08"PRIx32, p_sys->gc);
 
     /* */
@@ -481,7 +485,7 @@ static int Open (vlc_object_t *obj)
 
     /* */
     unsigned width, height;
-    if (!GetWindowSize (p_sys->embed, p_sys->conn, &width, &height))
+    if (!GetWindowSize (p_sys->embed, conn, &width, &height))
         vout_display_SendEventDisplaySize (vd, width, height);
     vout_display_SendEventFullscreen (vd, false);
 
@@ -515,7 +519,6 @@ static void Close (vlc_object_t *obj)
     }
 
     free (p_sys->att);
-    free (p_sys->adaptors);
     vout_display_DeleteWindow (vd, p_sys->embed);
     xcb_disconnect (p_sys->conn);
     free (p_sys);
@@ -560,6 +563,13 @@ static picture_t *Get (vout_display_t *vd)
             /* We assume that offsets[0] is zero */
             for (int i = 1; i < pic->i_planes; i++)
                 res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
+            if (vd->fmt.i_chroma == VLC_CODEC_YV12)
+            {   /* YVU: swap U and V planes */
+                uint8_t *buf = res->p[2].p_pixels;
+                res->p[2].p_pixels = res->p[1].p_pixels;
+                res->p[1].p_pixels = buf;
+            }
+
             pic_array[count] = picture_NewFromResource (&vd->fmt, res);
             if (!pic_array[count])
             {
@@ -593,10 +603,11 @@ static void Display (vout_display_t *vd, picture_t *pic)
 {
     vout_display_sys_t *p_sys = vd->sys;
     xcb_shm_seg_t segment = pic->p_sys->segment;
+    xcb_void_cookie_t ck;
 
     if (segment)
-        xcb_xv_shm_put_image (p_sys->conn, p_sys->port, p_sys->window,
-                              p_sys->gc, segment, p_sys->id, 0,
+        ck = xcb_xv_shm_put_image_checked (p_sys->conn, p_sys->port,
+                              p_sys->window, p_sys->gc, segment, p_sys->id, 0,
                    /* Src: */ vd->source.i_x_offset,
                               vd->source.i_y_offset,
                               vd->source.i_visible_width,
@@ -605,17 +616,25 @@ static void Display (vout_display_t *vd, picture_t *pic)
                 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
                               pic->p->i_visible_lines, false);
     else
-        xcb_xv_put_image (p_sys->conn, p_sys->port, p_sys->window,
+        ck = xcb_xv_put_image_checked (p_sys->conn, p_sys->port, p_sys->window,
                           p_sys->gc, p_sys->id,
                           vd->source.i_x_offset,
                           vd->source.i_y_offset,
                           vd->source.i_visible_width,
                           vd->source.i_visible_height,
                           0, 0, p_sys->width, p_sys->height,
-                          vd->source.i_width, vd->source.i_height,
+                          pic->p->i_pitch / pic->p->i_pixel_pitch,
+                          pic->p->i_visible_lines,
                           p_sys->data_size, pic->p->p_pixels);
 
-    xcb_flush (p_sys->conn);
+    /* Wait for reply. See x11.c for rationale. */
+    xcb_generic_error_t *e = xcb_request_check (p_sys->conn, ck);
+    if (e != NULL)
+    {
+        msg_Dbg (vd, "%s: X11 error %d", "cannot put image", e->error_code);
+        free (e);
+    }
+
     picture_Release (pic);
 }