cvpx.c 11.3 KB
Newer Older
1
/*****************************************************************************
2
 * cvpx.c: core video buffer to picture converter
3
 *****************************************************************************
4
 * Copyright (C) 2015-2017 VLC authors, VideoLAN and VideoLabs
5 6
 *
 * Authors: Felix Paul Kühne <fkuehne at videolan dot org>
7 8
 *          Thomas Guillem <thomas@gllm.fr>
 *          Victorien Le Couviour--Tuffet <victorien.lecouiour.tuffet@gmail.com>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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.
 *****************************************************************************/

#include <QuartzCore/QuartzCore.h>

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31 32
#include <TargetConditionals.h>

33 34 35
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_filter.h>
36
#include <vlc_picture.h>
37
#include <vlc_modules.h>
38
#include "../codec/vt_utils.h"
39

40 41
static int Open(vlc_object_t *);
static void Close(vlc_object_t *);
42

43
#if !TARGET_OS_IPHONE
44 45
static int Open_CVPX_to_CVPX(vlc_object_t *);
static void Close_CVPX_to_CVPX(vlc_object_t *);
46
#endif
47

48
vlc_module_begin ()
49 50 51
    set_description("Conversions from/to CoreVideo buffers")
    set_capability("video converter", 10)
    set_callbacks(Open, Close)
52

53 54 55 56 57 58 59 60 61 62
#if TARGET_OS_IPHONE
vlc_module_end ()

struct filter_sys_t
{
    filter_t *p_sw_filter;
    CVPixelBufferPoolRef pool;
};

#else
63 64 65
    add_submodule()
    set_description("Conversions between CoreVideo buffers")
    set_callbacks(Open_CVPX_to_CVPX, Close_CVPX_to_CVPX)
66 67
vlc_module_end ()

68
struct filter_sys_t
69
{
70 71 72 73 74 75 76 77 78 79
    union
    {
        struct
        {
            filter_t *p_sw_filter;
            CVPixelBufferPoolRef pool;
        };

        VTPixelTransferSessionRef vttransfer;
    };
80
};
81
#endif
82

83 84 85 86
/********************************
 * CVPX to/from I420 conversion *
 ********************************/

87 88 89 90 91
static picture_t *CVPX_TO_SW_Filter(filter_t *p_filter, picture_t *src)
{
    picture_t *src_sw =
        cvpxpic_create_mapped(&p_filter->fmt_out.video, cvpxpic_get_ref(src),
                              true);
92 93 94 95 96
    if (!src_sw)
    {
        picture_Release(src);
        return NULL;
    }
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
    picture_CopyProperties(src_sw, src);
    picture_Release(src);
    return src_sw;
}

static picture_t *SW_TO_CVPX_Filter(filter_t *p_filter, picture_t *src)
{
    filter_sys_t *p_sys = p_filter->p_sys;

    CVPixelBufferRef cvpx = cvpxpool_new_cvpx(p_sys->pool);
    if (cvpx == NULL)
    {
        picture_Release(src);
        return NULL;
    }

    /* Allocate a CPVX backed picture mapped for read/write */
    picture_t *mapped_dst =
        cvpxpic_create_mapped(&p_filter->fmt_in.video, cvpx, false);
    CFRelease(cvpx);
    if (!mapped_dst)
    {
        picture_Release(src);
        return NULL;
    }

    /* Allocate a CVPX picture without any context */
    picture_t *dst = picture_NewFromFormat(&p_filter->fmt_out.video);
    if (!dst)
    {
        picture_Release(src);
        picture_Release(mapped_dst);
        return NULL;
    }

    /* Copy pixels to the CVPX backed picture */
    picture_CopyPixels(mapped_dst, src);

    /* Attach the CVPX to a new opaque picture */
    cvpxpic_attach(dst, (void *)mapped_dst->p_sys);

    /* Unlock and unmap the dst picture */
    picture_Release(mapped_dst);

    picture_CopyProperties(dst, src);
    picture_Release(src);
    return dst;
}


147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
static picture_t *CVPX_TO_I420_Filter(filter_t *p_filter, picture_t *src)
{
    filter_sys_t *p_sys = p_filter->p_sys;
    filter_t *p_sw_filter = p_sys->p_sw_filter;
    assert(p_sw_filter != NULL);
    picture_t *dst = NULL;

    picture_t *src_sw =
        cvpxpic_create_mapped(&p_sw_filter->fmt_in.video, cvpxpic_get_ref(src),
                              true);

    if (!src_sw)
    {
        picture_Release(src);
        return NULL;
    }
    picture_CopyProperties(src_sw, src);
    picture_Release(src);
165

166
    dst = p_sw_filter->pf_video_filter(p_sw_filter, src_sw);
167

168 169
    return dst;
}
170

171 172 173 174 175
static picture_t *SW_buffer_new(filter_t *p_filter)
{
    return picture_NewFromFormat( &p_filter->fmt_out.video );
}

176 177 178 179
static picture_t *CVPX_buffer_new(filter_t *p_sw_filter)
{
    filter_t *p_filter = p_sw_filter->owner.sys;
    filter_sys_t *p_sys = p_filter->p_sys;
180

181
    CVPixelBufferRef cvpx = cvpxpool_new_cvpx(p_sys->pool);
182 183
    if (cvpx == NULL)
        return NULL;
184

185 186 187 188 189
    picture_t *pic =
        cvpxpic_create_mapped(&p_sw_filter->fmt_out.video, cvpx, false);
    CFRelease(cvpx);
    return pic;
}
190

191 192 193 194
static picture_t *I420_TO_CVPX_Filter(filter_t *p_filter, picture_t *src)
{
    filter_sys_t *p_sys = p_filter->p_sys;
    filter_t *p_sw_filter = p_sys->p_sw_filter;
195

196 197 198
    picture_t *sw_dst = p_sw_filter->pf_video_filter(p_sw_filter, src);
    if (sw_dst == NULL)
        return NULL;
199

200 201 202 203 204 205 206
    return cvpxpic_unmap(sw_dst);
}

static void Close(vlc_object_t *obj)
{
    filter_t *p_filter = (filter_t *)obj;
    filter_sys_t *p_sys = p_filter->p_sys;
207

208 209 210 211 212
    if (p_sys->p_sw_filter != NULL)
    {
        module_unneed(p_sys->p_sw_filter, p_sys->p_sw_filter->p_module);
        vlc_object_release(p_sys->p_sw_filter);
    }
213

214 215 216
    if (p_sys->pool != NULL)
        CVPixelBufferPoolRelease(p_sys->pool);
    free(p_sys);
217
}
218

219
static int Open(vlc_object_t *obj)
220 221 222 223 224 225 226
{
    filter_t *p_filter = (filter_t *)obj;

    if (p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height
        || p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width)
        return VLC_EGENERIC;

227 228
#define CASE_CVPX_INPUT(x) \
    case VLC_CODEC_CVPX_##x: \
229 230 231 232 233 234 235 236
        if (p_filter->fmt_out.video.i_chroma == VLC_CODEC_##x) { \
            p_filter->pf_video_filter = CVPX_TO_SW_Filter; \
        } else if (p_filter->fmt_out.video.i_chroma == VLC_CODEC_I420) {\
            p_filter->pf_video_filter = CVPX_TO_I420_Filter; \
            i_sw_filter_in_chroma = VLC_CODEC_##x; \
            i_sw_filter_out_chroma = VLC_CODEC_I420; \
            sw_filter_owner.video.buffer_new = SW_buffer_new; \
        } else return VLC_EGENERIC; \
237 238 239 240
        b_need_pool = false;

#define CASE_CVPX_OUTPUT(x) \
    case VLC_CODEC_CVPX_##x: \
241 242 243 244 245 246 247 248 249
        if (p_filter->fmt_in.video.i_chroma == VLC_CODEC_##x) { \
            p_filter->pf_video_filter = SW_TO_CVPX_Filter; \
        } else if (p_filter->fmt_in.video.i_chroma == VLC_CODEC_I420) {\
            p_filter->pf_video_filter = I420_TO_CVPX_Filter; \
            i_sw_filter_in_chroma = VLC_CODEC_I420; \
            i_sw_filter_out_chroma = VLC_CODEC_##x; \
            sw_filter_owner.sys = p_filter; \
            sw_filter_owner.video.buffer_new = CVPX_buffer_new; \
        } else return VLC_EGENERIC; \
250 251 252 253 254 255 256 257 258 259 260 261 262
        b_need_pool = true;

    bool b_need_pool;
    vlc_fourcc_t i_sw_filter_in_chroma = 0, i_sw_filter_out_chroma = 0;
    filter_owner_t sw_filter_owner = {};
    switch (p_filter->fmt_in.video.i_chroma)
    {
        CASE_CVPX_INPUT(NV12)
            break;
        CASE_CVPX_INPUT(UYVY)
            break;
        CASE_CVPX_INPUT(I420)
            break;
263 264
        CASE_CVPX_INPUT(BGRA)
            break;
265 266 267 268 269 270 271 272 273
        default:
            switch (p_filter->fmt_out.video.i_chroma)
            {
                CASE_CVPX_OUTPUT(NV12)
                    break;
                CASE_CVPX_OUTPUT(UYVY)
                    break;
                CASE_CVPX_OUTPUT(I420)
                    break;
274 275
                CASE_CVPX_OUTPUT(BGRA)
                    break;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
                default:
                    return VLC_EGENERIC;
            }
    }

    filter_sys_t *p_sys = p_filter->p_sys = malloc(sizeof(filter_sys_t));

    if (unlikely(!p_sys))
        return VLC_ENOMEM;

    p_sys->p_sw_filter = NULL;
    p_sys->pool = NULL;

    if (b_need_pool
     && (p_sys->pool = cvpxpool_create(&p_filter->fmt_out.video, 3)) == NULL)
        goto error;

293
    if (i_sw_filter_in_chroma != 0)
294
    {
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
        filter_t *p_sw_filter = vlc_object_create(p_filter, sizeof(filter_t));
        if (unlikely(p_sw_filter == NULL))
            goto error;

        p_sw_filter->fmt_in = p_filter->fmt_in;
        p_sw_filter->fmt_out = p_filter->fmt_out;
        p_sw_filter->fmt_in.i_codec = p_sw_filter->fmt_in.video.i_chroma
                                    = i_sw_filter_in_chroma;
        p_sw_filter->fmt_out.i_codec = p_sw_filter->fmt_out.video.i_chroma
                                     = i_sw_filter_out_chroma;

        p_sw_filter->owner = sw_filter_owner;
        p_sw_filter->p_module = module_need(p_sw_filter, "video converter",
                                            NULL, false);
        if (p_sw_filter->p_module == NULL)
        {
            vlc_object_release(p_sw_filter);
            goto error;
        }
        p_sys->p_sw_filter = p_sw_filter;
315
    }
316 317 318

    return VLC_SUCCESS;

319 320 321 322 323 324
error:
    Close(obj);
    return VLC_EGENERIC;
#undef CASE_CVPX_INPUT
#undef CASE_CVPX_OUTPUT
}
325 326 327 328 329

/***************************
 * CVPX to CVPX conversion *
 ***************************/

330 331
#if !TARGET_OS_IPHONE

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
static picture_t *
Filter(filter_t *filter, picture_t *src)
{
    CVPixelBufferRef src_cvpx = cvpxpic_get_ref(src);
    assert(src_cvpx);

    picture_t *dst = filter_NewPicture(filter);
    if (!dst)
    {
        picture_Release(src);
        return NULL;
    }

    CVPixelBufferRef dst_cvpx = cvpxpic_get_ref(dst);
    assert(dst_cvpx);

    if (VTPixelTransferSessionTransferImage(filter->p_sys->vttransfer,
                                            src_cvpx, dst_cvpx) != noErr)
    {
        picture_Release(dst);
        picture_Release(src);
        return NULL;
    }

    picture_CopyProperties(dst, src);
    picture_Release(src);
    return dst;
}

static vlc_fourcc_t const supported_chromas[] = { VLC_CODEC_CVPX_BGRA,
                                                  VLC_CODEC_CVPX_I420,
                                                  VLC_CODEC_CVPX_NV12,
                                                  VLC_CODEC_CVPX_UYVY };

static int
Open_CVPX_to_CVPX(vlc_object_t *obj)
{
    filter_t *filter = (filter_t *)obj;

    unsigned int i;
#define CHECK_CHROMA(fourcc) \
    i = 0; \
    while (i < ARRAY_SIZE(supported_chromas) && \
           fourcc != supported_chromas[i]) \
        ++i; \
    if (i == ARRAY_SIZE(supported_chromas)) \
        return VLC_EGENERIC; \

    CHECK_CHROMA(filter->fmt_in.video.i_chroma)
    CHECK_CHROMA(filter->fmt_out.video.i_chroma)
#undef CHECK_CHROMA

    filter->p_sys = calloc(1, sizeof(filter_sys_t));
    if (!filter->p_sys)
        return VLC_ENOMEM;

    if (VTPixelTransferSessionCreate(NULL, &filter->p_sys->vttransfer)
        != noErr)
    {
        free(filter->p_sys);
        return VLC_EGENERIC;
    }

    filter->pf_video_filter = Filter;

    return VLC_SUCCESS;
}

static void
Close_CVPX_to_CVPX(vlc_object_t *obj)
{
    filter_t *filter = (filter_t *)obj;

    VTPixelTransferSessionInvalidate(filter->p_sys->vttransfer);
    CFRelease(filter->p_sys->vttransfer);
    free(filter->p_sys);
}
409 410

#endif