vpx.c 7.05 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 37 38 39
/*****************************************************************************
 * 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>

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
Tristan Matthews's avatar
Tristan Matthews committed
40 41
static int OpenDecoder(vlc_object_t *);
static void CloseDecoder(vlc_object_t *);
Rafaël Carré's avatar
Rafaël Carré committed
42 43 44 45 46 47 48 49

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

vlc_module_begin ()
    set_shortname("vpx")
    set_description(N_("WebM video decoder"))
50
    set_capability("decoder", 60)
Tristan Matthews's avatar
Tristan Matthews committed
51
    set_callbacks(OpenDecoder, CloseDecoder)
Rafaël Carré's avatar
Rafaël Carré committed
52 53 54 55
    set_category(CAT_INPUT)
    set_subcategory(SUBCAT_INPUT_VCODEC)
vlc_module_end ()

56 57 58 59 60 61 62 63 64 65
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);
}

66
#define VPX_ERR(this, ctx, msg) vpx_err_msg(VLC_OBJECT(this), ctx, msg ": %s (%s)")
67

Rafaël Carré's avatar
Rafaël Carré committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/*****************************************************************************
 * decoder_sys_t: libvpx decoder descriptor
 *****************************************************************************/
struct decoder_sys_t
{
    struct vpx_codec_ctx ctx;
};

/****************************************************************************
 * Decode: the whole thing
 ****************************************************************************/
static picture_t *Decode(decoder_t *dec, block_t **pp_block)
{
    struct vpx_codec_ctx *ctx = &dec->p_sys->ctx;

    block_t *block = *pp_block;
    if (!block)
        return NULL;

87
    if (block->i_flags & (BLOCK_FLAG_CORRUPTED)) {
88
        block_Release(block);
Rafaël Carré's avatar
Rafaël Carré committed
89
        return NULL;
90
    }
Rafaël Carré's avatar
Rafaël Carré committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    /* 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;
    }

    *pkt_pts = block->i_pts;

    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);
110
        VPX_ERR(dec, ctx, "Failed to decode frame");
Rafaël Carré's avatar
Rafaël Carré committed
111 112 113 114 115
        return NULL;
    }

    const void *iter = NULL;
    struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
Tristan Matthews's avatar
Tristan Matthews committed
116 117
    if (!img) {
        free(pkt_pts);
Rafaël Carré's avatar
Rafaël Carré committed
118
        return NULL;
Tristan Matthews's avatar
Tristan Matthews committed
119
    }
Rafaël Carré's avatar
Rafaël Carré committed
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

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

    if (img->fmt != VPX_IMG_FMT_I420) {
        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;
    }

    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
163
 * OpenDecoder: probe the decoder
Rafaël Carré's avatar
Rafaël Carré committed
164
 *****************************************************************************/
Tristan Matthews's avatar
Tristan Matthews committed
165
static int OpenDecoder(vlc_object_t *p_this)
Rafaël Carré's avatar
Rafaël Carré committed
166 167 168 169 170 171 172
{
    decoder_t *dec = (decoder_t *)p_this;
    const struct vpx_codec_iface *iface;
    int vp_version;

    switch (dec->fmt_in.i_codec)
    {
173
#ifdef ENABLE_VP8_DECODER
Rafaël Carré's avatar
Rafaël Carré committed
174 175 176 177
    case VLC_CODEC_VP8:
        iface = &vpx_codec_vp8_dx_algo;
        vp_version = 8;
        break;
178
#endif
Rafaël Carré's avatar
Rafaël Carré committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
#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
198
    msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)",
Rafaël Carré's avatar
Rafaël Carré committed
199 200 201
        vp_version, vpx_codec_version_str(), vpx_codec_build_config());

    if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
202
        VPX_ERR(p_this, &sys->ctx, "Failed to initialize decoder");
Rafaël Carré's avatar
Rafaël Carré committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        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;
    dec->fmt_out.i_codec = VLC_CODEC_I420;

    return VLC_SUCCESS;
}

/*****************************************************************************
Tristan Matthews's avatar
Tristan Matthews committed
218
 * CloseDecoder: decoder destruction
Rafaël Carré's avatar
Rafaël Carré committed
219
 *****************************************************************************/
Tristan Matthews's avatar
Tristan Matthews committed
220
static void CloseDecoder(vlc_object_t *p_this)
Rafaël Carré's avatar
Rafaël Carré committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
{
    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);
}