]> git.sesse.net Git - vlc/blobdiff - modules/video_output/xcb/xvideo.c
vdr: clarify
[vlc] / modules / video_output / xcb / xvideo.c
index 843ae3544013a4d845f7d4bc6b19b2adc837c985..cf1353fae3c481d499f8bdb9b33ed2194c957d71 100644 (file)
@@ -5,20 +5,20 @@
 /*****************************************************************************
  * Copyright © 2009 Rémi Denis-Courmont
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- ****************************************************************************/
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -38,7 +38,8 @@
 #include <vlc_picture_pool.h>
 #include <vlc_dialog.h>
 
-#include "xcb_vlc.h"
+#include "pictures.h"
+#include "events.h"
 
 #define ADAPTOR_TEXT N_("XVideo adaptor number")
 #define ADAPTOR_LONGTEXT N_( \
@@ -52,6 +53,7 @@
 
 static int  Open (vlc_object_t *);
 static void Close (vlc_object_t *);
+static int EnumAdaptors (vlc_object_t *, const char *, int64_t **, char ***);
 
 /*
  * Module descriptor
@@ -66,6 +68,7 @@ vlc_module_begin ()
 
     add_integer ("xvideo-adaptor", -1,
                  ADAPTOR_TEXT, ADAPTOR_LONGTEXT, true)
+        change_integer_cb (EnumAdaptors)
     add_integer ("xvideo-format-id", 0,
                  FORMAT_TEXT, FORMAT_LONGTEXT, true)
     add_obsolete_bool ("xvideo-shm") /* removed in 2.0.0 */
@@ -93,7 +96,6 @@ struct vout_display_sys_t
 
     xcb_xv_query_image_attributes_reply_t *att;
     picture_pool_t *pool; /* picture pool */
-    picture_resource_t resource[MAX_PICTURES];
 };
 
 static picture_pool_t *Pool (vout_display_t *, unsigned);
@@ -145,7 +147,7 @@ static vlc_fourcc_t ParseFormat (vlc_object_t *obj,
             {
               case 24:
                 if (f->bpp == 32 && f->depth == 32)
-                    return VLC_CODEC_RGBA;
+                    return VLC_CODEC_ARGB;
                 if (f->bpp == 32 && f->depth == 24)
                     return VLC_CODEC_RGB32;
                 if (f->bpp == 24 && f->depth == 24)
@@ -160,16 +162,13 @@ static vlc_fourcc_t ParseFormat (vlc_object_t *obj,
               case 15:
                 if (f->byte_order != ORDER)
                     return 0; /* Mixed endian! */
-                if (f->bpp == 16 && f->depth == 16)
-                    return VLC_CODEC_RGBT;
                 if (f->bpp == 16 && f->depth == 15)
                     return VLC_CODEC_RGB15;
                 break;
               case 12:
-                if (f->bpp == 16 && f->depth == 16)
-                    return VLC_CODEC_RGBA16;
                 if (f->bpp == 16 && f->depth == 12)
                     return VLC_CODEC_RGB12;
+                break;
               case 8:
                 if (f->bpp == 8 && f->depth == 8)
                     return VLC_CODEC_RGB8;
@@ -316,8 +315,8 @@ FindFormat (vlc_object_t *obj, xcb_connection_t *conn, video_format_t *fmt,
             continue;
 
         /* VLC pads scanline to 16 pixels internally */
-        unsigned width = fmt->i_width;
-        unsigned height = fmt->i_height;
+        unsigned width = (fmt->i_width + 31) & ~31;
+        unsigned height = (fmt->i_height + 15) & ~15;
         xcb_xv_query_image_attributes_reply_t *i;
         i = xcb_xv_query_image_attributes_reply (conn,
             xcb_xv_query_image_attributes (conn, a->base_id, f->id,
@@ -334,12 +333,11 @@ FindFormat (vlc_object_t *obj, xcb_connection_t *conn, video_format_t *fmt,
             if (!var_GetBool (obj->p_libvlc, "xvideo-res-error"))
             {
                 dialog_FatalWait (obj, _("Video acceleration not available"),
-                    _("Your video output acceleration driver does not support "
-                      "the required resolution of %ux%u pixels. The supported "
-                      "resolution is %"PRIu32"x%"PRIu32".\n"
-                      "Video output acceleration will be disabled. However, "
-                      "rendering videos with overly large resolution "
-                      "may cause severe performance degration."),
+                    _("The XVideo rendering acceleration driver does not "
+                      "support the required resolution of %ux%u pixels but "
+                      "%"PRIu32"x%"PRIu32" pixels instead.\n"
+                      "Acceleration will thus be disabled. Performance may "
+                      "be degraded severely if the resolution is large."),
                                   width, height, i->width, i->height);
                 var_SetBool (obj->p_libvlc, "xvideo-res-error", true);
             }
@@ -385,8 +383,8 @@ static int Open (vlc_object_t *obj)
     /* Connect to X */
     xcb_connection_t *conn;
     const xcb_screen_t *screen;
-    uint8_t depth;
-    p_sys->embed = GetWindow (vd, &conn, &screen, &depth);
+    uint16_t width, height;
+    p_sys->embed = XCB_parent_Create (vd, &conn, &screen, &width, &height);
     if (p_sys->embed == NULL)
     {
         free (p_sys);
@@ -413,11 +411,17 @@ static int Open (vlc_object_t *obj)
     if (adaptors == NULL)
         goto error;
 
-    int forced_adaptor = var_InheritInteger (obj, "xvideo-adaptor");
+    int adaptor_selected = var_InheritInteger (obj, "xvideo-adaptor");
+    int adaptor_current = -1;
 
     /* */
     video_format_t fmt;
+    vout_display_place_t place;
+
     p_sys->port = 0;
+    vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
+    p_sys->width  = place.width;
+    p_sys->height = place.height;
 
     xcb_xv_adaptor_info_iterator_t it;
     for (it = xcb_xv_query_adaptors_info_iterator (adaptors);
@@ -425,20 +429,16 @@ static int Open (vlc_object_t *obj)
          xcb_xv_adaptor_info_next (&it))
     {
         const xcb_xv_adaptor_info_t *a = it.data;
-        char *name;
 
-        if (forced_adaptor != -1 && forced_adaptor != 0)
-        {
-            forced_adaptor--;
+        adaptor_current++;
+        if (adaptor_selected != -1 && adaptor_selected != adaptor_current)
             continue;
-        }
-
         if (!(a->type & XCB_XV_TYPE_INPUT_MASK)
          || !(a->type & XCB_XV_TYPE_IMAGE_MASK))
             continue;
 
         /* Look for an image format */
-        fmt = vd->fmt;
+        video_format_ApplyRotation(&fmt, &vd->fmt);
         free (p_sys->att);
         p_sys->att = FindFormat (obj, conn, &fmt, a, &p_sys->id);
         if (p_sys->att == NULL) /* No acceptable image formats */
@@ -466,12 +466,8 @@ static int Open (vlc_object_t *obj)
 
     grabbed_port:
         /* Found port - initialize selected format */
-        name = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
-        if (name != NULL)
-        {
-            msg_Dbg (vd, "using adaptor %s", name);
-            free (name);
-        }
+        msg_Dbg (vd, "using adaptor %.*s", (int)a->name_size,
+                 xcb_xv_adaptor_info_name (a));
         msg_Dbg (vd, "using port %"PRIu32, p_sys->port);
         msg_Dbg (vd, "using image format 0x%"PRIx32, p_sys->id);
 
@@ -503,15 +499,16 @@ static int Open (vlc_object_t *obj)
                 /* XCB_CW_COLORMAP */
                 screen->default_colormap,
             };
-
             xcb_void_cookie_t c;
 
             xcb_create_pixmap (conn, f->depth, pixmap, screen->root, 1, 1);
             c = xcb_create_window_checked (conn, f->depth, p_sys->window,
-                 p_sys->embed->handle.xid, 0, 0, 1, 1, 0,
-                 XCB_WINDOW_CLASS_INPUT_OUTPUT, f->visual, mask, list);
+                 p_sys->embed->handle.xid, place.x, place.y,
+                 place.width, place.height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
+                 f->visual, mask, list);
+            xcb_map_window (conn, p_sys->window);
 
-            if (!CheckError (vd, conn, "cannot create X11 window", c))
+            if (!XCB_error_Check (vd, conn, "cannot create X11 window", c))
             {
                 msg_Dbg (vd, "using X11 visual ID 0x%"PRIx32
                          " (depth: %"PRIu8")", f->visual, f->depth);
@@ -533,26 +530,6 @@ static int Open (vlc_object_t *obj)
         msg_Err (vd, "no available XVideo adaptor");
         goto error;
     }
-    /* Compute video (window) placement within the parent window */
-    {
-        xcb_map_window (conn, p_sys->window);
-
-        vout_display_place_t place;
-
-        vout_display_PlacePicture (&place, &vd->source, vd->cfg, false);
-        p_sys->width  = place.width;
-        p_sys->height = place.height;
-
-        /* */
-        const uint32_t values[] = {
-            place.x, place.y, place.width, place.height };
-        xcb_configure_window (conn, p_sys->window,
-                              XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
-                              XCB_CONFIG_WINDOW_WIDTH |
-                              XCB_CONFIG_WINDOW_HEIGHT,
-                              values);
-    }
-    p_sys->visible = false;
 
     /* Create graphic context */
     p_sys->gc = xcb_generate_id (conn);
@@ -566,12 +543,25 @@ static int Open (vlc_object_t *obj)
                 xcb_intern_atom (conn, 1, 21, "XV_AUTOPAINT_COLORKEY"), NULL);
         if (r != NULL && r->atom != 0)
             xcb_xv_set_port_attribute(conn, p_sys->port, r->atom, 1);
+        free(r);
+    }
+
+    /* Colour space */
+    {
+        xcb_intern_atom_reply_t *r =
+            xcb_intern_atom_reply (conn,
+                xcb_intern_atom (conn, 1, 13, "XV_ITURBT_709"), NULL);
+        if (r != NULL && r->atom != 0)
+            xcb_xv_set_port_attribute(conn, p_sys->port, r->atom,
+                                      fmt.i_height > 576);
+        free(r);
     }
 
     /* Create cursor */
-    p_sys->cursor = CreateBlankCursor (conn, screen);
+    p_sys->cursor = XCB_cursor_Create (conn, screen);
 
-    p_sys->shm = CheckSHM (obj, conn);
+    p_sys->shm = XCB_shm_Check (obj, conn);
+    p_sys->visible = false;
 
     /* */
     vout_display_info_t info = vd->info;
@@ -597,9 +587,7 @@ static int Open (vlc_object_t *obj)
     if (is_fullscreen && vout_window_SetFullScreen (p_sys->embed, true))
         is_fullscreen = false;
     vout_display_SendEventFullscreen (vd, is_fullscreen);
-    unsigned width, height;
-    if (!GetWindowSize (p_sys->embed, conn, &width, &height))
-        vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
+    vout_display_SendEventDisplaySize (vd, width, height, is_fullscreen);
 
     return VLC_SUCCESS;
 
@@ -618,17 +606,7 @@ static void Close (vlc_object_t *obj)
     vout_display_sys_t *p_sys = vd->sys;
 
     if (p_sys->pool)
-    {
-        for (unsigned i = 0; i < MAX_PICTURES; i++)
-        {
-            picture_resource_t *res = &p_sys->resource[i];
-
-            if (!res->p->p_pixels)
-                break;
-            PictureResourceFree (res, NULL);
-        }
         picture_pool_Delete (p_sys->pool);
-    }
 
     /* show the default cursor */
     xcb_change_window_attributes (p_sys->conn, p_sys->embed->handle.xid, XCB_CW_CURSOR,
@@ -645,61 +623,57 @@ static void PoolAlloc (vout_display_t *vd, unsigned requested_count)
 {
     vout_display_sys_t *p_sys = vd->sys;
 
-    memset (p_sys->resource, 0, sizeof(p_sys->resource));
-
     const uint32_t *pitches= xcb_xv_query_image_attributes_pitches (p_sys->att);
     const uint32_t *offsets= xcb_xv_query_image_attributes_offsets (p_sys->att);
     const unsigned num_planes= __MIN(p_sys->att->num_planes, PICTURE_PLANE_MAX);
     p_sys->data_size = p_sys->att->data_size;
 
+    picture_resource_t res = { NULL };
+    for (unsigned i = 0; i < num_planes; i++)
+    {
+        uint32_t data_size;
+        data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
+
+        res.p[i].i_lines = (data_size - offsets[i]) / pitches[i];
+        res.p[i].i_pitch = pitches[i];
+    }
+
     picture_t *pic_array[MAX_PICTURES];
     requested_count = __MIN(requested_count, MAX_PICTURES);
 
     unsigned count;
     for (count = 0; count < requested_count; count++)
     {
-        picture_resource_t *res = &p_sys->resource[count];
+        xcb_shm_seg_t seg = p_sys->shm ? xcb_generate_id (p_sys->conn) : 0;
 
-        for (unsigned i = 0; i < num_planes; i++)
-        {
-            uint32_t data_size;
-            data_size = (i < num_planes - 1) ? offsets[i+1] : p_sys->data_size;
-
-            res->p[i].i_lines = (data_size - offsets[i]) / pitches[i];
-            res->p[i].i_pitch = pitches[i];
-        }
-
-        if (PictureResourceAlloc (vd, res, p_sys->att->data_size,
-                                  p_sys->conn, p_sys->shm))
+        if (XCB_picture_Alloc (vd, &res, p_sys->data_size, p_sys->conn, seg))
             break;
 
         /* Allocate further planes as specified by XVideo */
         /* We assume that offsets[0] is zero */
         for (unsigned i = 1; i < num_planes; i++)
-            res->p[i].p_pixels = res->p[0].p_pixels + offsets[i];
+            res.p[i].p_pixels = res.p[0].p_pixels + offsets[i];
 
         if (p_sys->swap_uv)
         {   /* 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;
+            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])
-        {
-            PictureResourceFree (res, p_sys->conn);
-            memset (res, 0, sizeof(*res));
+        pic_array[count] = XCB_picture_NewFromResource (&vd->fmt, &res);
+        if (unlikely(pic_array[count] == NULL))
             break;
-        }
     }
+    xcb_flush (p_sys->conn);
 
     if (count == 0)
         return;
 
     p_sys->pool = picture_pool_New (count, pic_array);
-    /* TODO release picture resources if NULL */
-    xcb_flush (p_sys->conn);
+    if (unlikely(p_sys->pool == NULL))
+        while (count > 0)
+            picture_Release(pic_array[--count]);
 }
 
 /**
@@ -721,28 +695,28 @@ static picture_pool_t *Pool (vout_display_t *vd, unsigned requested_count)
 static void Display (vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
 {
     vout_display_sys_t *p_sys = vd->sys;
-    xcb_shm_seg_t segment = pic->p_sys->segment;
+    xcb_shm_seg_t segment = XCB_picture_GetSegment(pic);
     xcb_void_cookie_t ck;
+    video_format_t fmt;
 
     if (!p_sys->visible)
         goto out;
+
+    video_format_ApplyRotation(&fmt, &vd->source);
+
     if (segment)
         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,
-                              vd->source.i_visible_height,
+                   /* Src: */ fmt.i_x_offset, fmt.i_y_offset,
+                              fmt.i_visible_width, fmt.i_visible_height,
                    /* Dst: */ 0, 0, p_sys->width, p_sys->height,
                 /* Memory: */ pic->p->i_pitch / pic->p->i_pixel_pitch,
                               pic->p->i_lines, false);
     else
         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,
+                          fmt.i_x_offset, fmt.i_y_offset,
+                          fmt.i_visible_width, fmt.i_visible_height,
                           0, 0, p_sys->width, p_sys->height,
                           pic->p->i_pitch / pic->p->i_pixel_pitch,
                           pic->p->i_lines,
@@ -780,7 +754,6 @@ static int Control (vout_display_t *vd, int query, va_list ap)
     {
         const vout_display_cfg_t *cfg;
         const video_format_t *source;
-        bool is_forced = false;
 
         if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT
          || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP)
@@ -792,19 +765,14 @@ static int Control (vout_display_t *vd, int query, va_list ap)
         {
             source = &vd->source;
             cfg = (const vout_display_cfg_t*)va_arg (ap, const vout_display_cfg_t *);
-            if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
-                is_forced = (bool)va_arg (ap, int);
         }
 
-        /* */
-        if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE
-         && is_forced
-         && (cfg->display.width  != vd->cfg->display.width
-           ||cfg->display.height != vd->cfg->display.height)
-         && vout_window_SetSize (p_sys->embed,
-                                  cfg->display.width,
-                                  cfg->display.height))
-            return VLC_EGENERIC;
+        if (query == VOUT_DISPLAY_CHANGE_DISPLAY_SIZE && va_arg (ap, int))
+        {
+            vout_window_SetSize (p_sys->embed,
+                                 cfg->display.width, cfg->display.height);
+            return VLC_EGENERIC; /* Always fail. See x11.c for rationale. */
+        }
 
         vout_display_place_t place;
         vout_display_PlacePicture (&place, source, cfg, false);
@@ -846,6 +814,87 @@ static void Manage (vout_display_t *vd)
 {
     vout_display_sys_t *p_sys = vd->sys;
 
-    ManageEvent (vd, p_sys->conn, &p_sys->visible);
+    XCB_Manage (vd, p_sys->conn, &p_sys->visible);
 }
 
+static int EnumAdaptors (vlc_object_t *obj, const char *var,
+                         int64_t **vp, char ***tp)
+{
+    /* Connect to X */
+    char *display = var_InheritString (obj, "x11-display");
+    xcb_connection_t *conn;
+    int snum;
+
+    conn = xcb_connect (display, &snum);
+    free (display);
+    if (xcb_connection_has_error (conn) /*== NULL*/)
+        return -1;
+
+    /* Find configured screen */
+    const xcb_setup_t *setup = xcb_get_setup (conn);
+    const xcb_screen_t *scr = NULL;
+    for (xcb_screen_iterator_t i = xcb_setup_roots_iterator (setup);
+         i.rem > 0; xcb_screen_next (&i))
+    {
+        if (snum == 0)
+        {
+            scr = i.data;
+            break;
+        }
+        snum--;
+    }
+
+    if (scr == NULL)
+    {
+        xcb_disconnect (conn);
+        return -1;
+    }
+
+    xcb_xv_query_adaptors_reply_t *adaptors =
+        xcb_xv_query_adaptors_reply (conn,
+            xcb_xv_query_adaptors (conn, scr->root), NULL);
+    xcb_disconnect (conn);
+    if (adaptors == NULL)
+        return -1;
+
+    xcb_xv_adaptor_info_iterator_t it;
+    size_t n = 0;
+
+    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;
+
+        if ((a->type & XCB_XV_TYPE_INPUT_MASK)
+         && (a->type & XCB_XV_TYPE_IMAGE_MASK))
+            n++;
+    }
+
+    int64_t *values = xmalloc ((n + 1) * sizeof (*values));
+    char **texts = xmalloc ((n + 1) * sizeof (*texts));
+    *vp = values;
+    *tp = texts;
+
+    *(values++) = -1;
+    *(texts++) = strdup (N_("Auto"));
+
+    for (it = xcb_xv_query_adaptors_info_iterator (adaptors), n = -1;
+         it.rem > 0;
+         xcb_xv_adaptor_info_next (&it))
+    {
+        const xcb_xv_adaptor_info_t *a = it.data;
+
+        n++;
+
+        if (!(a->type & XCB_XV_TYPE_INPUT_MASK)
+         || !(a->type & XCB_XV_TYPE_IMAGE_MASK))
+            continue;
+
+        *(values++) = n;
+        *(texts++) = strndup (xcb_xv_adaptor_info_name (a), a->name_size);
+    }
+    free (adaptors);
+    (void) obj; (void) var;
+    return values - *vp;
+}