[vlc-commits] opengl: add converter_cvpx

Thomas Guillem git at videolan.org
Thu Feb 2 09:52:51 CET 2017


vlc | branch: master | Thomas Guillem <thomas at gllm.fr> | Fri Jan 27 14:35:28 2017 +0100| [0982d2bed9105cfbd1992a5e0baf18a9af4a5dbd] | committer: Thomas Guillem

opengl: add converter_cvpx

This converter can render CVPixel buffers directly by binding the IOSurface of
the buffer to an OpenGL texture.

This works only on Macos since IOSurface API is not allowed on iOS. This
improve significantly performances when using videotoolbox.

TODO: implement iOS conversion using
CVOpenGLESTextureCacheCreateTextureFromImage().

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=0982d2bed9105cfbd1992a5e0baf18a9af4a5dbd
---

 modules/video_output/Makefile.am             |   9 +-
 modules/video_output/opengl/converter_cvpx.c | 221 +++++++++++++++++++++++++++
 modules/video_output/opengl/internal.h       |   9 ++
 modules/video_output/opengl/vout_helper.c    |   3 +
 4 files changed, 239 insertions(+), 3 deletions(-)

diff --git a/modules/video_output/Makefile.am b/modules/video_output/Makefile.am
index 9c8956e..c04974e 100644
--- a/modules/video_output/Makefile.am
+++ b/modules/video_output/Makefile.am
@@ -14,9 +14,12 @@ vout_LTLIBRARIES += libdecklinkoutput_plugin.la
 endif
 
 if HAVE_OSX
-libvout_macosx_plugin_la_SOURCES = video_output/macosx.m $(OPENGL_DISPLAY_SRCS)
-libvout_macosx_plugin_la_CFLAGS = $(AM_CFLAGS)
-libvout_macosx_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(voutdir)' -Wl,-framework,OpenGL,-framework,Cocoa
+libvout_macosx_plugin_la_SOURCES = video_output/macosx.m $(OPENGL_DISPLAY_SRCS) \
+	video_output/opengl/converter_cvpx.c
+libvout_macosx_plugin_la_CFLAGS = $(AM_CFLAGS) -DVLCGL_CONV_CVPX
+libvout_macosx_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(voutdir)' \
+	-Wl,-framework,OpenGL,-framework,Cocoa -Wl,-framework,IOSurface \
+	-Wl,-framework,CoreVideo
 
 libcaopengllayer_plugin_la_SOURCES = video_output/caopengllayer.m \
 	$(OPENGL_DISPLAY_SRCS)
diff --git a/modules/video_output/opengl/converter_cvpx.c b/modules/video_output/opengl/converter_cvpx.c
new file mode 100644
index 0000000..c4ece0a
--- /dev/null
+++ b/modules/video_output/opengl/converter_cvpx.c
@@ -0,0 +1,221 @@
+/*****************************************************************************
+ * converter_cvpx.c: OpenGL Apple CVPX opaque converter
+ *****************************************************************************
+ * Copyright (C) 2017 VLC authors and VideoLAN
+ *
+ * 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 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 Lesser General Public License for more details.
+ *
+ * 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"
+#endif
+
+#include "internal.h"
+#include <IOSurface/IOSurface.h>
+#include <VideoToolbox/VideoToolbox.h>
+
+struct picture_sys_t
+{
+    CVPixelBufferRef pixelBuffer;
+};
+
+struct gl_sys
+{
+    CGLContextObj locked_ctx;
+};
+
+struct priv
+{
+    picture_t *last_pic;
+};
+
+static void
+pic_destroy_cb(picture_t *pic)
+{
+    if (pic->p_sys->pixelBuffer != NULL)
+        CFRelease(pic->p_sys->pixelBuffer);
+
+    free(pic->p_sys);
+    free(pic);
+}
+
+static picture_pool_t *
+tc_cvpx_get_pool(const opengl_tex_converter_t *tc, const video_format_t *fmt,
+                 unsigned requested_count, GLuint *textures)
+{
+    (void) tc; (void) textures;
+    picture_t *picture[VLCGL_PICTURE_MAX] = {NULL, };
+    unsigned count;
+
+    for (count = 0; count < requested_count; count++)
+    {
+        picture_sys_t *p_picsys = calloc(1, sizeof(*p_picsys));
+        if (unlikely(p_picsys == NULL))
+            goto error;
+        picture_resource_t rsc = {
+            .p_sys = p_picsys,
+            .pf_destroy = pic_destroy_cb,
+        };
+
+        picture[count] = picture_NewFromResource(fmt, &rsc);
+        if (!picture[count])
+        {
+            free(p_picsys);
+            goto error;
+        }
+    }
+
+    /* Wrap the pictures into a pool */
+    picture_pool_configuration_t pool_cfg = {
+        .picture_count = requested_count,
+        .picture       = picture,
+    };
+    picture_pool_t *pool = picture_pool_NewExtended(&pool_cfg);
+    if (!pool)
+        goto error;
+
+    return pool;
+error:
+    for (unsigned i = 0; i < count; i++)
+        picture_Release(picture[i]);
+    return NULL;
+}
+
+static int
+tc_cvpx_update(const opengl_tex_converter_t *tc, GLuint *textures,
+               const GLsizei *tex_width, const GLsizei *tex_height,
+               picture_t *pic, const size_t *plane_offset)
+{
+    (void) plane_offset;
+    struct priv *priv = tc->priv;
+    struct gl_sys *glsys = tc->gl->sys;
+    picture_sys_t *picsys = pic->p_sys;
+
+    assert(picsys->pixelBuffer != NULL);
+
+    IOSurfaceRef surface = CVPixelBufferGetIOSurface(picsys->pixelBuffer);
+
+    for (unsigned i = 0; i < tc->tex_count; ++i)
+    {
+        glActiveTexture(GL_TEXTURE0 + i);
+        glClientActiveTexture(GL_TEXTURE0 + i);
+        glBindTexture(tc->tex_target, textures[i]);
+
+        CGLError err =
+            CGLTexImageIOSurface2D(glsys->locked_ctx, tc->tex_target,
+                                   tc->texs[i].internal,
+                                   tex_width[i], tex_height[i],
+                                   tc->texs[i].format,
+                                   tc->texs[i].type,
+                                   surface, i);
+        if (err != kCGLNoError)
+        {
+            msg_Err(tc->gl, "CGLTexImageIOSurface2D error: %d: %s\n", i,
+                    CGLErrorString(err));
+            return VLC_EGENERIC;
+        }
+    }
+
+    if (priv->last_pic != pic)
+    {
+        if (priv->last_pic != NULL)
+        {
+            picture_sys_t *picsys = priv->last_pic->p_sys;
+            assert(picsys->pixelBuffer != NULL);
+            CFRelease(picsys->pixelBuffer);
+            picsys->pixelBuffer = NULL;
+            picture_Release(priv->last_pic);
+        }
+        priv->last_pic = picture_Hold(pic);
+    }
+
+    return VLC_SUCCESS;
+}
+
+static void
+tc_cvpx_release(const opengl_tex_converter_t *tc)
+{
+    struct priv *priv = tc->priv;
+
+    if (priv->last_pic != NULL)
+        picture_Release(priv->last_pic);
+    free(tc->priv);
+}
+
+GLuint
+opengl_tex_converter_cvpx_init(const video_format_t *fmt,
+                               opengl_tex_converter_t *tc)
+{
+    if (fmt->i_chroma != VLC_CODEC_CVPX_UYVY
+     && fmt->i_chroma != VLC_CODEC_CVPX_NV12
+     && fmt->i_chroma != VLC_CODEC_CVPX_I420
+     && fmt->i_chroma != VLC_CODEC_CVPX_BGRA)
+        return VLC_EGENERIC;
+
+    struct priv *priv = calloc(1, sizeof(struct priv));
+    if (unlikely(priv == NULL))
+        return VLC_ENOMEM;
+
+    GLenum tex_target = GL_TEXTURE_RECTANGLE;
+
+    GLuint fragment_shader;
+    switch (fmt->i_chroma)
+    {
+        case VLC_CODEC_CVPX_UYVY:
+            fragment_shader =
+                opengl_fragment_shader_init(tc, tex_target, VLC_CODEC_RGB32,
+                                            COLOR_SPACE_UNDEF);
+            tc->texs[0].internal = GL_RGB;
+            tc->texs[0].format = GL_YCBCR_422_APPLE;
+            tc->texs[0].type = GL_UNSIGNED_SHORT_8_8_APPLE;
+            break;
+        case VLC_CODEC_CVPX_NV12:
+        {
+            fragment_shader =
+                opengl_fragment_shader_init(tc, tex_target, VLC_CODEC_NV12,
+                                            fmt->space);
+            break;
+        }
+        case VLC_CODEC_CVPX_I420:
+            fragment_shader =
+                opengl_fragment_shader_init(tc, tex_target, VLC_CODEC_I420,
+                                            fmt->space);
+            break;
+        case VLC_CODEC_CVPX_BGRA:
+            fragment_shader =
+                opengl_fragment_shader_init(tc, tex_target, VLC_CODEC_RGB32,
+                                            COLOR_SPACE_UNDEF);
+            tc->texs[0].internal = GL_RGBA;
+            tc->texs[0].format = GL_BGRA;
+            tc->texs[0].type = GL_UNSIGNED_INT_8_8_8_8_REV;
+            break;
+        default:
+            vlc_assert_unreachable();
+    }
+
+    if (fragment_shader == 0)
+    {
+        free(priv);
+        return 0;
+    }
+
+    tc->priv              = priv;
+    tc->chroma            = fmt->i_chroma;
+    tc->pf_get_pool       = tc_cvpx_get_pool;
+    tc->pf_update         = tc_cvpx_update;
+    tc->pf_release        = tc_cvpx_release;
+
+    return fragment_shader;
+}
diff --git a/modules/video_output/opengl/internal.h b/modules/video_output/opengl/internal.h
index 1557674..2658833 100644
--- a/modules/video_output/opengl/internal.h
+++ b/modules/video_output/opengl/internal.h
@@ -35,6 +35,9 @@
 #       define VLCGL_HAS_PBO
 #   endif
 #   define PRECISION ""
+#   if defined(__APPLE__)
+#       define GL_TEXTURE_RECTANGLE 0x84F5
+#   endif
 #endif
 
 #if defined(USE_OPENGL_ES2) || defined(__APPLE__)
@@ -332,4 +335,10 @@ opengl_tex_converter_anop_init(const video_format_t *,
                                opengl_tex_converter_t *);
 #endif
 
+#ifdef __APPLE__
+GLuint
+opengl_tex_converter_cvpx_init(const video_format_t *fmt,
+                               opengl_tex_converter_t *tc);
+#endif
+
 #endif /* include-guard */
diff --git a/modules/video_output/opengl/vout_helper.c b/modules/video_output/opengl/vout_helper.c
index d82c46f..aa7775a 100644
--- a/modules/video_output/opengl/vout_helper.c
+++ b/modules/video_output/opengl/vout_helper.c
@@ -55,6 +55,9 @@ static opengl_tex_converter_init_cb opengl_tex_converter_init_cbs[] =
 #ifdef __ANDROID__
     opengl_tex_converter_anop_init,
 #endif
+#ifdef VLCGL_CONV_CVPX
+    opengl_tex_converter_cvpx_init,
+#endif
 };
 
 typedef struct {



More information about the vlc-commits mailing list