converter_cvpx.c 6.53 KB
Newer Older
Thomas Guillem's avatar
Thomas Guillem committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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;
}