vpx.c 15.4 KB
Newer Older
Rafaël Carré's avatar
Rafaël Carré 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
/*****************************************************************************
 * vpx.c: libvpx decoder (VP8/VP9) module
 *****************************************************************************
 * Copyright (C) 2013 Rafaël Carré
 *
 * Authors: Rafaël Carré <funman@videolanorg>
 *
 * 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.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_codec.h>

#include <vpx/vpx_decoder.h>
#include <vpx/vp8dx.h>

37 38 39 40 41
#ifdef ENABLE_SOUT
# include <vpx/vpx_encoder.h>
# include <vpx/vp8cx.h>
#endif

Rafaël Carré's avatar
Rafaël Carré committed
42 43 44
/****************************************************************************
 * Local prototypes
 ****************************************************************************/
Tristan Matthews's avatar
Tristan Matthews committed
45 46
static int OpenDecoder(vlc_object_t *);
static void CloseDecoder(vlc_object_t *);
47
#ifdef ENABLE_SOUT
48
static const char *const ppsz_sout_options[] = { "quality-mode", NULL };
49 50 51 52 53
static int OpenEncoder(vlc_object_t *);
static void CloseEncoder(vlc_object_t *);
static block_t *Encode(encoder_t *p_enc, picture_t *p_pict);

#define QUALITY_MODE_TEXT N_("Quality mode")
54
#define QUALITY_MODE_LONGTEXT N_("Quality setting which will determine max encoding time\n" \
55 56 57 58
        " - 0: Good quality\n"\
        " - 1: Realtime\n"\
        " - 2: Best quality")
#endif
Rafaël Carré's avatar
Rafaël Carré committed
59 60 61 62 63 64 65 66

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/

vlc_module_begin ()
    set_shortname("vpx")
    set_description(N_("WebM video decoder"))
67
    set_capability("decoder", 100)
Tristan Matthews's avatar
Tristan Matthews committed
68
    set_callbacks(OpenDecoder, CloseDecoder)
Rafaël Carré's avatar
Rafaël Carré committed
69 70
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
71 72 73 74 75 76 77 78 79 80 81
#ifdef ENABLE_SOUT
    add_submodule()
    set_shortname("vpx")
    set_capability("encoder", 60)
    set_description(N_("WebM video encoder"))
    set_callbacks(OpenEncoder, CloseEncoder)
#   define ENC_CFG_PREFIX "sout-vpx-"
    add_integer( ENC_CFG_PREFIX "quality-mode", VPX_DL_GOOD_QUALITY, QUALITY_MODE_TEXT,
                 QUALITY_MODE_LONGTEXT, true )
        change_integer_range( 0, 2 )
#endif
Rafaël Carré's avatar
Rafaël Carré committed
82 83
vlc_module_end ()

84 85 86 87 88 89 90 91 92 93
static void vpx_err_msg(vlc_object_t *this, struct vpx_codec_ctx *ctx,
                        const char *msg)
{
    const char *error  = vpx_codec_error(ctx);
    const char *detail = vpx_codec_error_detail(ctx);
    if (!detail)
        detail = "no specific information";
    msg_Err(this, msg, error, detail);
}

94
#define VPX_ERR(this, ctx, msg) vpx_err_msg(VLC_OBJECT(this), ctx, msg ": %s (%s)")
95

Rafaël Carré's avatar
Rafaël Carré committed
96 97 98 99 100 101 102 103
/*****************************************************************************
 * decoder_sys_t: libvpx decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    struct vpx_codec_ctx ctx;
};

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
static const struct
{
    vlc_fourcc_t     i_chroma;
    enum vpx_img_fmt i_chroma_id;
    uint8_t          i_bitdepth;
    uint8_t          i_needs_hack;

} chroma_table[] =
{
    { VLC_CODEC_I420, VPX_IMG_FMT_I420, 8, 0 },
    { VLC_CODEC_I422, VPX_IMG_FMT_I422, 8, 0 },
    { VLC_CODEC_I444, VPX_IMG_FMT_I444, 8, 0 },
    { VLC_CODEC_I440, VPX_IMG_FMT_I440, 8, 0 },

    { VLC_CODEC_YV12, VPX_IMG_FMT_YV12, 8, 0 },
    { VLC_CODEC_YUVA, VPX_IMG_FMT_444A, 8, 0 },
    { VLC_CODEC_YUYV, VPX_IMG_FMT_YUY2, 8, 0 },
    { VLC_CODEC_UYVY, VPX_IMG_FMT_UYVY, 8, 0 },
    { VLC_CODEC_YVYU, VPX_IMG_FMT_YVYU, 8, 0 },

    { VLC_CODEC_RGB15, VPX_IMG_FMT_RGB555, 8, 0 },
    { VLC_CODEC_RGB16, VPX_IMG_FMT_RGB565, 8, 0 },
    { VLC_CODEC_RGB24, VPX_IMG_FMT_RGB24, 8, 0 },
    { VLC_CODEC_RGB32, VPX_IMG_FMT_RGB32, 8, 0 },

    { VLC_CODEC_ARGB, VPX_IMG_FMT_ARGB, 8, 0 },
    { VLC_CODEC_BGRA, VPX_IMG_FMT_ARGB_LE, 8, 0 },

    { VLC_CODEC_GBR_PLANAR, VPX_IMG_FMT_I444, 8, 1 },
    { VLC_CODEC_GBR_PLANAR_10L, VPX_IMG_FMT_I44416, 10, 1 },

    { VLC_CODEC_I420_10L, VPX_IMG_FMT_I42016, 10, 0 },
    { VLC_CODEC_I422_10L, VPX_IMG_FMT_I42216, 10, 0 },
    { VLC_CODEC_I444_10L, VPX_IMG_FMT_I44416, 10, 0 },

    { VLC_CODEC_I420_12L, VPX_IMG_FMT_I42016, 12, 0 },
    { VLC_CODEC_I422_12L, VPX_IMG_FMT_I42216, 12, 0 },
    { VLC_CODEC_I444_12L, VPX_IMG_FMT_I44416, 12, 0 },

    { VLC_CODEC_I444_16L, VPX_IMG_FMT_I44416, 16, 0 },
};

146
static vlc_fourcc_t FindVlcChroma( struct vpx_image *img )
147 148 149
{
    uint8_t hack = (img->fmt & VPX_IMG_FMT_I444) && (img->cs == VPX_CS_SRGB);

150
    for( unsigned int i = 0; i < ARRAY_SIZE(chroma_table); i++ )
151 152 153 154 155 156 157 158
        if( chroma_table[i].i_chroma_id == img->fmt &&
            chroma_table[i].i_bitdepth == img->bit_depth &&
            chroma_table[i].i_needs_hack == hack )
            return chroma_table[i].i_chroma;

    return 0;
}

Rafaël Carré's avatar
Rafaël Carré committed
159 160 161 162 163 164 165
/****************************************************************************
 * Decode: the whole thing
 ****************************************************************************/
static picture_t *Decode(decoder_t *dec, block_t **pp_block)
{
    struct vpx_codec_ctx *ctx = &dec->p_sys->ctx;

Tristan Matthews's avatar
Tristan Matthews committed
166
    if( !pp_block || !*pp_block )
Rafaël Carré's avatar
Rafaël Carré committed
167
        return NULL;
Tristan Matthews's avatar
Tristan Matthews committed
168
    block_t *block = *pp_block;
Rafaël Carré's avatar
Rafaël Carré committed
169

170
    if (block->i_flags & (BLOCK_FLAG_CORRUPTED)) {
171
        block_Release(block);
Rafaël Carré's avatar
Rafaël Carré committed
172
        return NULL;
173
    }
Rafaël Carré's avatar
Rafaël Carré committed
174 175 176 177 178 179 180 181 182

    /* Associate packet PTS with decoded frame */
    mtime_t *pkt_pts = malloc(sizeof(*pkt_pts));
    if (!pkt_pts) {
        block_Release(block);
        *pp_block = NULL;
        return NULL;
    }

183
    *pkt_pts = block->i_pts ? block->i_pts : block->i_dts;
Rafaël Carré's avatar
Rafaël Carré committed
184 185 186 187 188 189 190 191 192

    vpx_codec_err_t err;
    err = vpx_codec_decode(ctx, block->p_buffer, block->i_buffer, pkt_pts, 0);

    block_Release(block);
    *pp_block = NULL;

    if (err != VPX_CODEC_OK) {
        free(pkt_pts);
193
        VPX_ERR(dec, ctx, "Failed to decode frame");
Rafaël Carré's avatar
Rafaël Carré committed
194 195 196 197 198
        return NULL;
    }

    const void *iter = NULL;
    struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
Tristan Matthews's avatar
Tristan Matthews committed
199 200
    if (!img) {
        free(pkt_pts);
Rafaël Carré's avatar
Rafaël Carré committed
201
        return NULL;
Tristan Matthews's avatar
Tristan Matthews committed
202
    }
Rafaël Carré's avatar
Rafaël Carré committed
203 204 205 206 207 208

    /* fetches back the PTS */
    pkt_pts = img->user_priv;
    mtime_t pts = *pkt_pts;
    free(pkt_pts);

209
    dec->fmt_out.i_codec = FindVlcChroma(img);
210 211

    if( dec->fmt_out.i_codec == 0 ) {
Rafaël Carré's avatar
Rafaël Carré committed
212 213 214 215 216 217 218 219 220 221 222
        msg_Err(dec, "Unsupported output colorspace %d", img->fmt);
        return NULL;
    }

    video_format_t *v = &dec->fmt_out.video;

    if (img->d_w != v->i_visible_width || img->d_h != v->i_visible_height) {
        v->i_visible_width = img->d_w;
        v->i_visible_height = img->d_h;
    }

223
    if( !dec->fmt_out.video.i_sar_num || !dec->fmt_out.video.i_sar_den )
224
    {
225 226
        dec->fmt_out.video.i_sar_num = 1;
        dec->fmt_out.video.i_sar_den = 1;
227 228
    }

229 230
    v->b_color_range_full = img->range == VPX_CR_FULL_RANGE;

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    switch( img->cs )
    {
        case VPX_CS_SRGB:
        case VPX_CS_BT_709:
            v->space = COLOR_SPACE_BT709;
            break;
        case VPX_CS_BT_601:
        case VPX_CS_SMPTE_170:
        case VPX_CS_SMPTE_240:
            v->space = COLOR_SPACE_BT601;
            break;
        case VPX_CS_BT_2020:
            v->space = COLOR_SPACE_BT2020;
            break;
        default:
            break;
    }

249 250
    if (decoder_UpdateVideoFormat(dec))
        return NULL;
Rafaël Carré's avatar
Rafaël Carré committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    picture_t *pic = decoder_NewPicture(dec);
    if (!pic)
        return NULL;

    for (int plane = 0; plane < pic->i_planes; plane++ ) {
        uint8_t *src = img->planes[plane];
        uint8_t *dst = pic->p[plane].p_pixels;
        int src_stride = img->stride[plane];
        int dst_stride = pic->p[plane].i_pitch;

        int size = __MIN( src_stride, dst_stride );
        for( int line = 0; line < pic->p[plane].i_visible_lines; line++ ) {
            memcpy( dst, src, size );
            src += src_stride;
            dst += dst_stride;
        }
    }

    pic->b_progressive = true; /* codec does not support interlacing */
    pic->date = pts;

    return pic;
}

/*****************************************************************************
Tristan Matthews's avatar
Tristan Matthews committed
276
 * OpenDecoder: probe the decoder
Rafaël Carré's avatar
Rafaël Carré committed
277
 *****************************************************************************/
Tristan Matthews's avatar
Tristan Matthews committed
278
static int OpenDecoder(vlc_object_t *p_this)
Rafaël Carré's avatar
Rafaël Carré committed
279 280 281 282 283 284 285
{
    decoder_t *dec = (decoder_t *)p_this;
    const struct vpx_codec_iface *iface;
    int vp_version;

    switch (dec->fmt_in.i_codec)
    {
286
#ifdef ENABLE_VP8_DECODER
Rafaël Carré's avatar
Rafaël Carré committed
287 288 289 290
    case VLC_CODEC_VP8:
        iface = &vpx_codec_vp8_dx_algo;
        vp_version = 8;
        break;
291
#endif
Rafaël Carré's avatar
Rafaël Carré committed
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
#ifdef ENABLE_VP9_DECODER
    case VLC_CODEC_VP9:
        iface = &vpx_codec_vp9_dx_algo;
        vp_version = 9;
        break;
#endif
    default:
        return VLC_EGENERIC;
    }

    decoder_sys_t *sys = malloc(sizeof(*sys));
    if (!sys)
        return VLC_ENOMEM;
    dec->p_sys = sys;

    struct vpx_codec_dec_cfg deccfg = {
        .threads = __MIN(vlc_GetCPUCount(), 16)
    };

Tristan Matthews's avatar
Tristan Matthews committed
311
    msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
Rafaël Carré's avatar
Rafaël Carré committed
312 313 314
        vp_version, vpx_codec_version_str(), vpx_codec_build_config());

    if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
315
        VPX_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
Rafaël Carré's avatar
Rafaël Carré committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329
        free(sys);
        return VLC_EGENERIC;;
    }

    dec->pf_decode_video = Decode;

    dec->fmt_out.i_cat = VIDEO_ES;
    dec->fmt_out.video.i_width = dec->fmt_in.video.i_width;
    dec->fmt_out.video.i_height = dec->fmt_in.video.i_height;

    return VLC_SUCCESS;
}

/*****************************************************************************
Tristan Matthews's avatar
Tristan Matthews committed
330
 * CloseDecoder: decoder destruction
Rafaël Carré's avatar
Rafaël Carré committed
331
 *****************************************************************************/
Tristan Matthews's avatar
Tristan Matthews committed
332
static void CloseDecoder(vlc_object_t *p_this)
Rafaël Carré's avatar
Rafaël Carré committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
    decoder_t *dec = (decoder_t *)p_this;
    decoder_sys_t *sys = dec->p_sys;

    /* Free our PTS */
    const void *iter = NULL;
    for (;;) {
        struct vpx_image *img = vpx_codec_get_frame(&sys->ctx, &iter);
        if (!img)
            break;
        free(img->user_priv);
    }

    vpx_codec_destroy(&sys->ctx);

    free(sys);
}
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

#ifdef ENABLE_SOUT

/*****************************************************************************
 * encoder_sys_t: libvpx encoder descriptor
 *****************************************************************************/
struct encoder_sys_t
{
    struct vpx_codec_ctx ctx;
};

/*****************************************************************************
 * OpenEncoder: probe the encoder
 *****************************************************************************/
static int OpenEncoder(vlc_object_t *p_this)
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys;

    /* Allocate the memory needed to store the encoder's structure */
    p_sys = malloc(sizeof(*p_sys));
    if (p_sys == NULL)
        return VLC_ENOMEM;
    p_enc->p_sys = p_sys;

    const struct vpx_codec_iface *iface;
    int vp_version;

    switch (p_enc->fmt_out.i_codec)
    {
#ifdef ENABLE_VP8_ENCODER
    case VLC_CODEC_VP8:
        iface = &vpx_codec_vp8_cx_algo;
        vp_version = 8;
        break;
#endif
Pere Orga's avatar
Pere Orga committed
386
#ifdef ENABLE_VP9_ENCODER
387 388 389 390 391 392
    case VLC_CODEC_VP9:
        iface = &vpx_codec_vp9_cx_algo;
        vp_version = 9;
        break;
#endif
    default:
Hannes Domani's avatar
Hannes Domani committed
393
        free(p_sys);
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
        return VLC_EGENERIC;
    }

    struct vpx_codec_enc_cfg enccfg = {};
    vpx_codec_enc_config_default(iface, &enccfg, 0);
    enccfg.g_threads = __MIN(vlc_GetCPUCount(), 4);
    enccfg.g_w = p_enc->fmt_in.video.i_visible_width;
    enccfg.g_h = p_enc->fmt_in.video.i_visible_height;

    msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
        vp_version, vpx_codec_version_str(), vpx_codec_build_config());

    struct vpx_codec_ctx *ctx = &p_sys->ctx;
    if (vpx_codec_enc_init(ctx, iface, &enccfg, 0) != VPX_CODEC_OK) {
        VPX_ERR(p_this, ctx, "Failed to initialize encoder");
        free(p_sys);
        return VLC_EGENERIC;
    }

    p_enc->pf_encode_video = Encode;
    p_enc->fmt_in.i_codec = VLC_CODEC_I420;
    config_ChainParse(p_enc, ENC_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg);

    return VLC_SUCCESS;
}

/****************************************************************************
 * Encode: the whole thing
 ****************************************************************************/
static block_t *Encode(encoder_t *p_enc, picture_t *p_pict)
{
    encoder_sys_t *p_sys = p_enc->p_sys;
    struct vpx_codec_ctx *ctx = &p_sys->ctx;

    if (!p_pict) return NULL;

    vpx_image_t img = {};
    unsigned i_w = p_enc->fmt_in.video.i_visible_width;
    unsigned i_h = p_enc->fmt_in.video.i_visible_height;

    /* Create and initialize the vpx_image */
    if (!vpx_img_alloc(&img, VPX_IMG_FMT_I420, i_w, i_h, 1)) {
        VPX_ERR(p_enc, ctx, "Failed to allocate image");
        return NULL;
    }
    for (int plane = 0; plane < p_pict->i_planes; plane++) {
        uint8_t *src = p_pict->p[plane].p_pixels;
        uint8_t *dst = img.planes[plane];
        int src_stride = p_pict->p[plane].i_pitch;
        int dst_stride = img.stride[plane];

        int size = __MIN(src_stride, dst_stride);
        for (int line = 0; line < p_pict->p[plane].i_visible_lines; line++)
        {
            memcpy(dst, src, size);
            src += src_stride;
            dst += dst_stride;
        }
    }

    int flags = 0;
    /* Deadline (in ms) to spend in encoder */
    int quality = VPX_DL_GOOD_QUALITY;
    switch (var_GetInteger(p_enc, ENC_CFG_PREFIX "quality-mode")) {
        case 1:
            quality = VPX_DL_REALTIME;
            break;
        case 2:
            quality = VPX_DL_BEST_QUALITY;
            break;
        default:
            break;
    }

    vpx_codec_err_t res = vpx_codec_encode(ctx, &img, p_pict->date, 1,
     flags, quality);
    if (res != VPX_CODEC_OK) {
        VPX_ERR(p_enc, ctx, "Failed to encode frame");
        return NULL;
    }

    const vpx_codec_cx_pkt_t *pkt = NULL;
    vpx_codec_iter_t iter = NULL;
    block_t *p_out = NULL;
    while ((pkt = vpx_codec_get_cx_data(ctx, &iter)) != NULL)
    {
        if (pkt->kind == VPX_CODEC_CX_FRAME_PKT)
        {
            int keyframe = pkt->data.frame.flags & VPX_FRAME_IS_KEY;
            block_t *p_block = block_Alloc(pkt->data.frame.sz);

            memcpy(p_block->p_buffer, pkt->data.frame.buf, pkt->data.frame.sz);
            p_block->i_dts = p_block->i_pts = pkt->data.frame.pts;
            if (keyframe)
                p_block->i_flags |= BLOCK_FLAG_TYPE_I;
            block_ChainAppend(&p_out, p_block);
        }
    }
    vpx_img_free(&img);
    return p_out;
}

/*****************************************************************************
 * CloseEncoder: encoder destruction
 *****************************************************************************/
static void CloseEncoder(vlc_object_t *p_this)
{
    encoder_t *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys = p_enc->p_sys;
    if (vpx_codec_destroy(&p_sys->ctx))
        VPX_ERR(p_this, &p_sys->ctx, "Failed to destroy codec");
    free(p_sys);
}

#endif  /* ENABLE_SOUT */