[vlc-devel] [PATCH 2/2] libvpx decoder

Jean-Baptiste Kempf jb at videolan.org
Sun Oct 20 17:11:00 CEST 2013


On 20 Oct, Rafaël Carré wrote :
> diff --git a/NEWS b/NEWS
> index 888cf22..ead97df 100644
> --- a/NEWS
> +++ b/NEWS
> @@ -7,6 +7,7 @@ Access:
>  
>  Decoders:
>   * Partial support for Voxware MetaSound
> + * libvpx decoder for VP8 and VP9
>  
>  Encoder:

OK.

> diff --git a/configure.ac b/configure.ac
> index d5e2d81..dca45c8 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -2497,6 +2497,25 @@ if test "${enable_faad}" != "no"; then
>  fi
>  
>  dnl
> +dnl libvpx decoder plugin
> +dnl
> +AC_ARG_ENABLE(vpx,
> +    AS_HELP_STRING([--enable-vpx],[libvpx VP8/VP9 decoder (default auto)]))
> +AS_IF([test "${enable_vpx}" != "no"],[
> +    PKG_CHECK_MODULES([VPX], [vpx] , [
> +        VLC_ADD_PLUGIN([vpx])
> +        VLC_ADD_CPPFLAGS([vpx], [${VPX_CFLAGS}])
> +        VLC_ADD_LIBS([vpx], [${VPX_LIBS}])
> +        AC_CHECK_LIB([vpx],[vpx_codec_vp9_dx], [
> +            VLC_ADD_CPPFLAGS([vpx], [-DENABLE_VP9_DECODER])
> +        ], [], [${VPX_LIBS}])
> +    ], [
> +    AS_IF([test "${enable_vpx}" = "yes"],[
> +        AC_MSG_ERROR([libvpx was not found])
> +    ])])
> +])
> +
> +dnl

Unfortunately, OK :)

> diff --git a/modules/MODULES_LIST b/modules/MODULES_LIST
> index b2288aa..ca28d32 100644
> --- a/modules/MODULES_LIST
> +++ b/modules/MODULES_LIST
> @@ -382,6 +382,7 @@ $Id$
>   * vout_ios2: iOS video provider using OpenGL ES 2
>   * vout_macosx: Mac OS X OpenGL provider
>   * vout_sdl: video output module using the SDL library
> + * vpx: libvpx decoder (VP8/VP9)
>   * vsxu: audio visualization using Vovoid VSXu
>   * wall: image wall filter
>   * wav: Wav demuxer

OK.

> diff --git a/modules/codec/Makefile.am b/modules/codec/Makefile.am
> index bf73069..ad9f797b 100644
> --- a/modules/codec/Makefile.am
> +++ b/modules/codec/Makefile.am
> @@ -398,6 +398,14 @@ libshine_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(codecdir)'
>  EXTRA_LTLIBRARIES += libshine_plugin.la
>  codec_LTLIBRARIES += $(LTLIBshine)
>  
> +libvpx_plugin_la_SOURCES = codec/vpx.c
> +libvpx_plugin_la_CPPFLAGS = $(AM_CPPFLAGS)
> +libvpx_plugin_la_CFLAGS = $(AM_CFLAGS) $(VPX_CFLAGS) $(CPPFLAGS_vpx)
> +libvpx_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(codecdir)'
> +libvpx_plugin_la_LIBADD = $(VPX_LIBS)
> +EXTRA_LTLIBRARIES += libvpx_plugin.la
> +codec_LTLIBRARIES += $(LTLIBvpx)
> +
>  libtwolame_plugin_la_SOURCES = codec/twolame.c
>  libtwolame_plugin_la_CPPFLAGS = $(AM_CPPFLAGS) -DLIBTWOLAME_STATIC

LGTM.

> +vlc_module_begin ()
> +    set_shortname("vpx")
> +    set_description(N_("libvpx video decoder"))

Not really useful for users, if I may say. VP8/VP9 would be more
interesting.

> +    set_capability("decoder", 80)
> +    set_callbacks(Open, Close)
> +    set_category(CAT_INPUT)
> +    set_subcategory(SUBCAT_INPUT_VCODEC)
> +vlc_module_end ()
> +
> +/*****************************************************************************
> + * 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;
> +
> +    if (block->i_flags & (BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED))
> +        return NULL;
> +
> +    /* 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);
> +        const char *error  = vpx_codec_error(ctx);
> +        const char *detail = vpx_codec_error_detail(ctx);
> +        if (!detail)
> +            detail = "no specific information";
> +        msg_Err(dec, "Failed to decode frame: %s (%s)", error, detail);
> +        return NULL;
> +    }
> +
> +    const void *iter = NULL;
> +    struct vpx_image *img = vpx_codec_get_frame(ctx, &iter);
> +    if (!img)
> +        return NULL;
> +
> +    /* 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;
> +}
> +
> +/*****************************************************************************
> + * Open: probe the decoder
> + *****************************************************************************/
> +static int Open(vlc_object_t *p_this)
> +{
> +    decoder_t *dec = (decoder_t *)p_this;
> +    const struct vpx_codec_iface *iface;
> +    int vp;

I find this variable to verbose enough :)

> +
> +    switch (dec->fmt_in.i_codec)
> +    {
> +    case VLC_CODEC_VP8:
> +        iface = &vpx_codec_vp8_dx_algo;
> +        vp = 8;
> +        break;
> +#ifdef ENABLE_VP9_DECODER
> +    case VLC_CODEC_VP9:
> +        iface = &vpx_codec_vp9_dx_algo;
> +        vp = 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)
> +    };
> +
> +    msg_Dbg(p_this, "VP%d: using libvpx version %s (build options %s)", vp, vpx_codec_version_str(), vpx_codec_build_config());
> +
> +    if (vpx_codec_dec_init(&sys->ctx, iface, &deccfg, 0) != VPX_CODEC_OK) {
> +        const char *error = vpx_codec_error(&sys->ctx);
> +        msg_Err(p_this, "Failed to initialize decoder: %s\n", error);
> +        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;
> +    dec->b_need_packetized = true;
> +
> +    return VLC_SUCCESS;
> +}
> +
> +/*****************************************************************************
> + * Close: decoder destruction
> + *****************************************************************************/
> +static void Close(vlc_object_t *p_this)
> +{
> +    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);
> +}
> diff --git a/po/POTFILES.in b/po/POTFILES.in
> index e66e8ee..4e38143 100644
> --- a/po/POTFILES.in
> +++ b/po/POTFILES.in
> @@ -404,6 +404,7 @@ modules/codec/theora.c
>  modules/codec/twolame.c
>  modules/codec/uleaddvaudio.c
>  modules/codec/vorbis.c
> +modules/codec/vpx.c
>  modules/codec/wmafixed/wma.c
>  modules/codec/x264.c
>  modules/codec/x265.c


Apart those minor things, LTGM. Nice.

Best regards,

-- 
Jean-Baptiste Kempf
http://www.jbkempf.com/ - +33 672 704 734
Sent from my Electronic Device



More information about the vlc-devel mailing list