[FFmpeg-devel] [PATCH v4 4/4] lavc, doc: add libuavs3d video decoder wrapper

Paul B Mahol onemda at gmail.com
Tue Aug 18 19:49:12 EEST 2020


On 8/18/20, hwrenx at 126.com <hwrenx at 126.com> wrote:
> From: hwren <hwrenx at 126.com>
>
> Signed-off-by: hbj <hanbj at pku.edu.cn>
> Signed-off-by: hwren <hwrenx at 126.com>
> ---
>  Changelog              |   1 +
>  configure              |   4 +
>  doc/decoders.texi      |  21 +++
>  doc/general.texi       |   8 ++
>  libavcodec/Makefile    |   1 +
>  libavcodec/allcodecs.c |   1 +
>  libavcodec/libuavs3d.c | 294 +++++++++++++++++++++++++++++++++++++++++
>  7 files changed, 330 insertions(+)
>  create mode 100644 libavcodec/libuavs3d.c
>
> diff --git a/Changelog b/Changelog
> index 1efc768387..91794629e5 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -14,6 +14,7 @@ version <next>:
>  - ADPCM Argonaut Games encoder
>  - Argonaut Games ASF muxer
>  - AV1 Low overhead bitstream format demuxer
> +- AVS3 video decoder via libuavs3d
>
>
>  version 4.3:
> diff --git a/configure b/configure
> index 6faff9bc7b..0fde821742 100755
> --- a/configure
> +++ b/configure
> @@ -277,6 +277,7 @@ External library support:
>    --enable-libtls          enable LibreSSL (via libtls), needed for https
> support
>                             if openssl, gnutls or mbedtls is not used [no]
>    --enable-libtwolame      enable MP2 encoding via libtwolame [no]
> +  --enable-libuavs3d       enable AVS3 decoding via libuavs3d [no]
>    --enable-libv4l2         enable libv4l2/v4l-utils [no]
>    --enable-libvidstab      enable video stabilization using vid.stab [no]
>    --enable-libvmaf         enable vmaf filter via libvmaf [no]
> @@ -1811,6 +1812,7 @@ EXTERNAL_LIBRARY_LIST="
>      libtesseract
>      libtheora
>      libtwolame
> +    libuavs3d
>      libv4l2
>      libvmaf
>      libvorbis
> @@ -3253,6 +3255,7 @@ libspeex_encoder_select="audio_frame_queue"
>  libsvtav1_encoder_deps="libsvtav1"
>  libtheora_encoder_deps="libtheora"
>  libtwolame_encoder_deps="libtwolame"
> +libuavs3d_decoder_deps="libuavs3d"
>  libvo_amrwbenc_encoder_deps="libvo_amrwbenc"
>  libvorbis_decoder_deps="libvorbis"
>  libvorbis_encoder_deps="libvorbis libvorbisenc"
> @@ -6416,6 +6419,7 @@ enabled libtls            && require_pkg_config libtls
> libtls tls.h tls_configur
>  enabled libtwolame        && require libtwolame twolame.h twolame_init
> -ltwolame &&
>                               { check_lib libtwolame twolame.h
> twolame_encode_buffer_float32_interleaved -ltwolame ||
>                                 die "ERROR: libtwolame must be installed and
> version must be >= 0.3.10"; }
> +enabled libuavs3d         && require_pkg_config libuavs3d "uavs3d >=
> 1.1.41" uavs3d.h uavs3d_decode
>  enabled libv4l2           && require_pkg_config libv4l2 libv4l2 libv4l2.h
> v4l2_ioctl
>  enabled libvidstab        && require_pkg_config libvidstab "vidstab >=
> 0.98" vid.stab/libvidstab.h vsMotionDetectInit
>  enabled libvmaf           && require_pkg_config libvmaf "libvmaf >= 1.5.2"
> libvmaf.h compute_vmaf
> diff --git a/doc/decoders.texi b/doc/decoders.texi
> index 9005714e3c..42b46fe153 100644
> --- a/doc/decoders.texi
> +++ b/doc/decoders.texi
> @@ -88,6 +88,27 @@ This decoder allows libavcodec to decode AVS2 streams
> with davs2 library.
>
>  @c man end VIDEO DECODERS
>
> + at section libuavs3d
> +
> +AVS3-P2/IEEE1857.10 video decoder.
> +
> +libuavs3d allows libavcodec to decode AVS3 streams.
> +Requires the presence of the libuavs3d headers and library during
> configuration.
> +You need to explicitly configure the build with @code{--enable-libuavs3d}.
> +
> + at subsection Options
> +
> +The following option is supported by the libuavs3d wrapper.
> +
> + at table @option
> +
> + at item frame_threads
> +Set amount of frame threads to use during decoding. The default value is 0
> (autodetect).
> +
> + at end table
> +
> + at c man end VIDEO DECODERS
> +
>  @chapter Audio Decoders
>  @c man begin AUDIO DECODERS
>
> diff --git a/doc/general.texi b/doc/general.texi
> index fac5377504..233f69d349 100644
> --- a/doc/general.texi
> +++ b/doc/general.texi
> @@ -125,6 +125,14 @@ Go to @url{https://github.com/pkuvcl/davs2} and follow
> the instructions for
>  installing the library. Then pass @code{--enable-libdavs2} to configure to
>  enable it.
>
> + at section uavs3d
> +
> +FFmpeg can make use of the uavs3d library for AVS3-P2/IEEE1857.10 video
> decoding.
> +
> +Go to @url{https://github.com/uavs3/uavs3d} and follow the instructions for
> +installing the library. Then pass @code{--enable-libuavs3d} to configure to
> +enable it.
> +
>  @float NOTE
>  libdavs2 is under the GNU Public License Version 2 or later
>  (see @url{http://www.gnu.org/licenses/old-licenses/gpl-2.0.html} for
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index 5c031f7777..31d5f39bcd 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1032,6 +1032,7 @@ OBJS-$(CONFIG_LIBSPEEX_ENCODER)           +=
> libspeexenc.o
>  OBJS-$(CONFIG_LIBSVTAV1_ENCODER)          += libsvtav1.o
>  OBJS-$(CONFIG_LIBTHEORA_ENCODER)          += libtheoraenc.o
>  OBJS-$(CONFIG_LIBTWOLAME_ENCODER)         += libtwolame.o
> +OBJS-$(CONFIG_LIBUAVS3D_DECODER)          += libuavs3d.o
>  OBJS-$(CONFIG_LIBVO_AMRWBENC_ENCODER)     += libvo-amrwbenc.o
>  OBJS-$(CONFIG_LIBVORBIS_DECODER)          += libvorbisdec.o
>  OBJS-$(CONFIG_LIBVORBIS_ENCODER)          += libvorbisenc.o \
> diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
> index 4bd830e5d0..04b990dd37 100644
> --- a/libavcodec/allcodecs.c
> +++ b/libavcodec/allcodecs.c
> @@ -732,6 +732,7 @@ extern AVCodec ff_libspeex_decoder;
>  extern AVCodec ff_libsvtav1_encoder;
>  extern AVCodec ff_libtheora_encoder;
>  extern AVCodec ff_libtwolame_encoder;
> +extern AVCodec ff_libuavs3d_decoder;
>  extern AVCodec ff_libvo_amrwbenc_encoder;
>  extern AVCodec ff_libvorbis_encoder;
>  extern AVCodec ff_libvorbis_decoder;
> diff --git a/libavcodec/libuavs3d.c b/libavcodec/libuavs3d.c
> new file mode 100644
> index 0000000000..9f82dbde69
> --- /dev/null
> +++ b/libavcodec/libuavs3d.c
> @@ -0,0 +1,294 @@
> +/*
> + * RAW AVS3-P2/IEEE1857.10 video demuxer
> + * Copyright (c) 2020 Zhenyu Wang <wangzhenyu at pkusz.edu.cn>
> + *                    Bingjie Han <hanbj at pkusz.edu.cn>
> + *                    Huiwen Ren  <hwrenx at gmail.com>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg 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.
> + *
> + * FFmpeg 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 FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
> USA
> + */
> +
> +#include "libavutil/avassert.h"
> +#include "libavutil/avutil.h"
> +#include "libavutil/common.h"
> +#include "libavutil/imgutils.h"
> +#include "libavutil/intreadwrite.h"
> +#include "libavutil/opt.h"
> +#include "avcodec.h"
> +#include "avs3_parser.h"
> +#include "internal.h"
> +#include "uavs3d.h"
> +
> +static const int uavs3d_color_primaries_tab[10] = {
> +    AVCOL_PRI_RESERVED0   ,    // 0
> +    AVCOL_PRI_BT709       ,    // 1
> +    AVCOL_PRI_UNSPECIFIED ,    // 2
> +    AVCOL_PRI_RESERVED    ,    // 3
> +    AVCOL_PRI_BT470M      ,    // 4
> +    AVCOL_PRI_BT470BG     ,    // 5
> +    AVCOL_PRI_SMPTE170M   ,    // 6
> +    AVCOL_PRI_SMPTE240M   ,    // 7
> +    AVCOL_PRI_FILM        ,    // 8
> +    AVCOL_PRI_BT2020           // 9
> +};
> +
> +static const int uavs3d_color_transfer_tab[15] = {
> +    AVCOL_TRC_RESERVED0    , // 0
> +    AVCOL_TRC_BT709        , // 1
> +    AVCOL_TRC_UNSPECIFIED  , // 2
> +    AVCOL_TRC_RESERVED     , // 3
> +    AVCOL_TRC_GAMMA22      , // 4
> +    AVCOL_TRC_GAMMA28      , // 5
> +    AVCOL_TRC_SMPTE170M    , // 6
> +    AVCOL_TRC_SMPTE240M    , // 7
> +    AVCOL_TRC_LINEAR       , // 8
> +    AVCOL_TRC_LOG          , // 9
> +    AVCOL_TRC_LOG_SQRT     , // 10
> +    AVCOL_TRC_BT2020_12    , // 11
> +    AVCOL_TRC_SMPTE2084    , // 12
> +    AVCOL_TRC_UNSPECIFIED  , // 13
> +    AVCOL_TRC_ARIB_STD_B67   // 14
> +};
> +
> +static const int uavs3d_color_matrix_tab[12] = {
> +    AVCOL_SPC_RESERVED     , // 0
> +    AVCOL_SPC_BT709        , // 1
> +    AVCOL_SPC_UNSPECIFIED  , // 2
> +    AVCOL_SPC_RESERVED     , // 3
> +    AVCOL_SPC_FCC          , // 4
> +    AVCOL_SPC_BT470BG      , // 5
> +    AVCOL_SPC_SMPTE170M    , // 6
> +    AVCOL_SPC_SMPTE240M    , // 7
> +    AVCOL_SPC_BT2020_NCL   , // 8
> +    AVCOL_SPC_BT2020_CL    , // 9
> +    AVCOL_SPC_UNSPECIFIED  , // 10
> +    AVCOL_SPC_UNSPECIFIED    // 11
> +};
> +
> +static const enum AVPictureType uavs3d_image_type[4] = {
> +    AV_PICTURE_TYPE_NONE,
> +    AV_PICTURE_TYPE_I,
> +    AV_PICTURE_TYPE_P,
> +    AV_PICTURE_TYPE_B
> +};
> +
> +typedef struct uavs3d_context {
> +    AVCodecContext  *avctx;
> +    void            *dec_handle;
> +    int              frame_threads;
> +    int              got_seqhdr;
> +    uavs3d_io_frm_t  dec_frame;
> +} uavs3d_context;
> +
> +#define UAVS3D_CHECK_START_CODE(data_ptr, PIC_START_CODE) \
> +        (AV_RL32(data_ptr) != (PIC_START_CODE << 24) + AVS3_NAL_START_CODE)
> +static int uavs3d_find_next_start_code(const unsigned char *bs_data, int
> bs_len, int *left)
> +{
> +    const unsigned char *data_ptr = bs_data + 4;
> +    int count = bs_len - 4;
> +
> +    while (count >= 4 &&
> +           UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTER_PIC_START_CODE) &&
> +           UAVS3D_CHECK_START_CODE(data_ptr, AVS3_INTRA_PIC_START_CODE) &&
> +           UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_START_CODE) &&
> +           UAVS3D_CHECK_START_CODE(data_ptr, AVS3_FIRST_SLICE_START_CODE)
> &&
> +           UAVS3D_CHECK_START_CODE(data_ptr, AVS3_SEQ_END_CODE)) {
> +        data_ptr++;
> +        count--;
> +    }
> +
> +    if (count >= 4) {
> +        *left = count;
> +        return 1;
> +    }
> +
> +    return 0;
> +}
> +
> +static void uavs3d_output_callback(uavs3d_io_frm_t *dec_frame) {
> +    uavs3d_io_frm_t frm_out;
> +    AVFrame *frm = (AVFrame *)dec_frame->priv;
> +    int i;
> +
> +    if (!frm || !frm->data[0]) {
> +        dec_frame->got_pic = 0;
> +        av_log(NULL, AV_LOG_ERROR, "Invalid AVFrame in uavs3d output.\n");
> +        return;
> +    }
> +
> +    frm->pts       = dec_frame->pts;
> +    frm->pkt_dts   = dec_frame->dts;
> +
> +    if (dec_frame->type < 0 || dec_frame->type >= 4) {
> +        av_log(NULL, AV_LOG_WARNING, "Error frame type in uavs3d: %d.\n",
> dec_frame->type);
> +    }
> +
> +    frm->pict_type = uavs3d_image_type[dec_frame->type];
> +    frm->key_frame = (frm->pict_type == AV_PICTURE_TYPE_I);
> +
> +    for (i = 0; i < 3; i++) {
> +        frm_out.width [i] = dec_frame->width[i];
> +        frm_out.height[i] = dec_frame->height[i];
> +        frm_out.stride[i] = frm->linesize[i];
> +        frm_out.buffer[i] = frm->data[i];
> +    }
> +
> +    uavs3d_img_cpy_cvt(&frm_out, dec_frame, dec_frame->bit_depth);
> +}
> +
> +static av_cold int libuavs3d_init(AVCodecContext *avctx)
> +{
> +    uavs3d_context *h = avctx->priv_data;
> +    uavs3d_cfg_t cdsc;
> +
> +    cdsc.frm_threads = avctx->thread_count > 0 ? avctx->thread_count :
> av_cpu_count();
> +    cdsc.check_md5 = 0;
> +    h->dec_handle = uavs3d_create(&cdsc, uavs3d_output_callback, NULL);
> +    h->got_seqhdr = 0;
> +
> +    if (!h->dec_handle) {
> +        return AVERROR(ENOMEM);
> +    }
> +
> +    return 0;
> +}
> +
> +static av_cold int libuavs3d_end(AVCodecContext *avctx)
> +{
> +    uavs3d_context *h = avctx->priv_data;
> +
> +    if (h->dec_handle) {
> +        uavs3d_flush(h->dec_handle, NULL);
> +        uavs3d_delete(h->dec_handle);
> +        h->dec_handle = NULL;
> +    }
> +    h->got_seqhdr = 0;
> +
> +    return 0;
> +}
> +
> +static void libuavs3d_flush(AVCodecContext * avctx)
> +{
> +    uavs3d_context *h = avctx->priv_data;
> +
> +    if (h->dec_handle) {
> +        uavs3d_reset(h->dec_handle);
> +    }
> +}
> +
> +static int libuavs3d_decode_frame(AVCodecContext *avctx, void *data, int
> *got_frame, AVPacket *avpkt)
> +{
> +    uavs3d_context *h = avctx->priv_data;
> +    const uint8_t *buf = avpkt->data;
> +    int buf_size = avpkt->size;
> +    const uint8_t *buf_end;
> +    const uint8_t *buf_ptr;
> +    AVFrame *frm = data;
> +    int left_bytes;
> +    int ret, finish = 0;
> +
> +    *got_frame = 0;
> +    frm->pts = -1;
> +    frm->pict_type = AV_PICTURE_TYPE_NONE;
> +
> +    if (!buf_size) {
> +        if (h->got_seqhdr) {
> +            if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) < 0)
> {
> +                return ret;
> +            }
> +            h->dec_frame.priv = data;   // AVFrame
> +        }
> +        do {
> +            ret = uavs3d_flush(h->dec_handle, &h->dec_frame);
> +        } while (ret > 0 && !h->dec_frame.got_pic);
> +    } else {
> +        buf_ptr = buf;
> +        buf_end = buf + buf_size;
> +
> +        while (!finish) {
> +            int bs_len;
> +            uavs3d_io_frm_t *frm_dec = &h->dec_frame;
> +
> +            if (h->got_seqhdr) {
> +                if (!frm->data[0] && (ret = ff_get_buffer(avctx, frm, 0)) <
> 0) {
> +                    return ret;
> +                }
> +                h->dec_frame.priv = data;   // AVFrame
> +            }
> +
> +            if (uavs3d_find_next_start_code(buf_ptr, buf_end - buf_ptr,
> &left_bytes)) {
> +                bs_len = buf_end - buf_ptr - left_bytes;
> +            } else {
> +                bs_len = buf_end - buf_ptr;
> +                finish = 1;
> +            }
> +            frm_dec->bs = (unsigned char *)buf_ptr;
> +            frm_dec->bs_len = bs_len;
> +            frm_dec->pts = avpkt->pts;
> +            frm_dec->dts = avpkt->dts;
> +            uavs3d_decode(h->dec_handle, frm_dec);
> +            buf_ptr += bs_len;
> +
> +            if (frm_dec->nal_type == NAL_SEQ_HEADER) {
> +                struct uavs3d_com_seqh_t *seqh = frm_dec->seqhdr;
> +                if (seqh->frame_rate_code >= 16) {
> +                    av_log(avctx, AV_LOG_WARNING, "Invalid frame rate code:
> %d.\n", seqh->frame_rate_code);
> +                    seqh->frame_rate_code = 3; // default 25 fps
> +                }
> +                avctx->framerate.num =
> avs3_frame_rate_tab[seqh->frame_rate_code].num;
> +                avctx->framerate.den =
> avs3_frame_rate_tab[seqh->frame_rate_code].den;
> +                avctx->has_b_frames  = !seqh->low_delay;
> +                avctx->pix_fmt = seqh->bit_depth_internal == 8 ?
> AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV420P10LE;
> +                ff_set_dimensions(avctx, seqh->horizontal_size,
> seqh->vertical_size);
> +                h->got_seqhdr = 1;
> +
> +                if (seqh->colour_description) {
> +                    avctx->color_primaries =
> uavs3d_color_primaries_tab[seqh->colour_primaries];
> +                    avctx->color_trc       = uavs3d_color_transfer_tab
> [seqh->transfer_characteristics];
> +                    avctx->colorspace      = uavs3d_color_matrix_tab
> [seqh->matrix_coefficients];
> +                }
> +            }
> +            if (frm_dec->got_pic) {
> +                break;
> +            }
> +        }
> +    }
> +
> +    *got_frame = h->dec_frame.got_pic;
> +
> +    if (!(*got_frame)) {
> +        av_frame_unref(frm);
> +    }
> +
> +    return buf_ptr - buf;
> +}
> +
> +AVCodec ff_libuavs3d_decoder = {
> +    .name           = "libuavs3d",
> +    .long_name      = NULL_IF_CONFIG_SMALL("libuavs3d AVS3-P2/IEEE1857.10
> decoder"),

Please remove "decoder" word from long description.
Its unnecessary.

> +    .type           = AVMEDIA_TYPE_VIDEO,
> +    .id             = AV_CODEC_ID_AVS3,
> +    .priv_data_size = sizeof(uavs3d_context),
> +    .init           = libuavs3d_init,
> +    .close          = libuavs3d_end,
> +    .decode         = libuavs3d_decode_frame,
> +    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
> AV_CODEC_CAP_AUTO_THREADS,
> +    .flush          = libuavs3d_flush,
> +    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
> +
> AV_PIX_FMT_YUV420P10LE,
> +                                                     AV_PIX_FMT_NONE },
> +    .wrapper_name   = "libuavs3d",
> +};
> --
> 2.23.0.windows.1
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request at ffmpeg.org with subject "unsubscribe".


More information about the ffmpeg-devel mailing list