[FFmpeg-devel] [PATCH 2/3] lavc, configure: add avs2 video decoder via libdavs2

hwren hwrenx at 126.com
Mon May 28 13:14:56 EEST 2018


Signed-off-by: hwren <hwrenx at 126.com>
---
 configure               |   4 +
 libavcodec/Makefile     |   1 +
 libavcodec/codec_desc.c |   7 ++
 libavcodec/libdavs2.c   | 212 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 224 insertions(+)
 create mode 100644 libavcodec/libdavs2.c

diff --git a/configure b/configure
index 09ff0c5..e2ac9f6 100755
--- a/configure
+++ b/configure
@@ -226,6 +226,7 @@ External library support:
   --enable-libcelt         enable CELT decoding via libcelt [no]
   --enable-libcdio         enable audio CD grabbing with libcdio [no]
   --enable-libcodec2       enable codec2 en/decoding using libcodec2 [no]
+  --enable-libdavs2        enable AVS2 decoding via libdavs2 [no]
   --enable-libdc1394       enable IIDC-1394 grabbing using libdc1394
                            and libraw1394 [no]
   --enable-libfdk-aac      enable AAC de/encoding via libfdk-aac [no]
@@ -1636,6 +1637,7 @@ EXTERNAL_LIBRARY_GPL_LIST="
     avisynth
     frei0r
     libcdio
+    libdavs2
     librubberband
     libvidstab
     libx264
@@ -3042,6 +3044,7 @@ libaom_av1_encoder_deps="libaom"
 libcelt_decoder_deps="libcelt"
 libcodec2_decoder_deps="libcodec2"
 libcodec2_encoder_deps="libcodec2"
+libdavs2_decoder_deps="libdavs2 gpl"
 libfdk_aac_decoder_deps="libfdk_aac"
 libfdk_aac_encoder_deps="libfdk_aac"
 libfdk_aac_encoder_select="audio_frame_queue"
@@ -5992,6 +5995,7 @@ enabled libcelt           && require libcelt celt/celt.h celt_decode -lcelt0 &&
                                die "ERROR: libcelt must be installed and version must be >= 0.11.0."; }
 enabled libcaca           && require_pkg_config libcaca caca caca.h caca_create_canvas
 enabled libcodec2         && require libcodec2 codec2/codec2.h codec2_create -lcodec2
+enabled libdavs2          && require_pkg_config libdavs2 "davs2 >= 1.2.34" davs2.h davs2_decoder_decode
 enabled libdc1394         && require_pkg_config libdc1394 libdc1394-2 dc1394/dc1394.h dc1394_new
 enabled libdrm            && require_pkg_config libdrm libdrm xf86drm.h drmGetVersion
 enabled libfdk_aac        && { check_pkg_config libfdk_aac fdk-aac "fdk-aac/aacenc_lib.h" aacEncOpen ||
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 3ab071a..2a845f1 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -944,6 +944,7 @@ OBJS-$(CONFIG_LIBAOM_AV1_ENCODER)         += libaomenc.o
 OBJS-$(CONFIG_LIBCELT_DECODER)            += libcelt_dec.o
 OBJS-$(CONFIG_LIBCODEC2_DECODER)          += libcodec2.o codec2utils.o
 OBJS-$(CONFIG_LIBCODEC2_ENCODER)          += libcodec2.o codec2utils.o
+OBJS-$(CONFIG_LIBDAVS2_DECODER)           += libdavs2.o
 OBJS-$(CONFIG_LIBFDK_AAC_DECODER)         += libfdk-aacdec.o
 OBJS-$(CONFIG_LIBFDK_AAC_ENCODER)         += libfdk-aacenc.o
 OBJS-$(CONFIG_LIBGSM_DECODER)             += libgsmdec.o
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index 79552a9..48cb413 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -1395,6 +1395,13 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .props     = AV_CODEC_PROP_LOSSLESS,
     },
     {
+        .id        = AV_CODEC_ID_AVS2,
+        .type      = AVMEDIA_TYPE_VIDEO,
+        .name      = "avs2",
+        .long_name = NULL_IF_CONFIG_SMALL("Chinese AVS2 (Audio Video Standar) (AVS2-P2, JiZhun profile)"),
+        .props     = AV_CODEC_PROP_LOSSY,
+    },
+    {
         .id        = AV_CODEC_ID_Y41P,
         .type      = AVMEDIA_TYPE_VIDEO,
         .name      = "y41p",
diff --git a/libavcodec/libdavs2.c b/libavcodec/libdavs2.c
new file mode 100644
index 0000000..4fa8790
--- /dev/null
+++ b/libavcodec/libdavs2.c
@@ -0,0 +1,212 @@
+/*
+ * AVS2 decoding using the davs2 library
+ *
+ * Copyright (C) 2018 Yiqun Xu, <yiqun.xu at vipl.ict.ac.cn>
+ *                    Falei Luo, <falei.luo at gmail.com>
+ *                    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/common.h"
+#include "libavutil/avutil.h"
+#include "avcodec.h"
+#include "libavutil/imgutils.h"
+#include "internal.h"
+
+#include <davs2.h>
+
+typedef struct DAVS2Context {
+    AVCodecContext *avctx;
+
+    void *decoder;
+
+    AVFrame *frame;
+    davs2_param_t    param;      // decoding parameters
+    davs2_packet_t   packet;     // input bitstream
+
+    int decoded_frames;
+
+    davs2_picture_t  out_frame;  // output data, frame data
+    davs2_seq_info_t headerset;  // output data, sequence header
+
+}DAVS2Context;
+
+static int ff_davs2_init(AVCodecContext *avctx)
+{
+    DAVS2Context *cad = avctx->priv_data;
+
+    /* init the decoder */
+    cad->param.threads      = 0;
+    cad->param.i_info_level = 0;
+    cad->decoder = davs2_decoder_open(&cad->param);
+    avctx->flags |= AV_CODEC_FLAG_TRUNCATED;
+
+    av_log(avctx, AV_LOG_VERBOSE, "[davs2] decoder created. 0x%p\n", cad->decoder);
+    return 0;
+}
+
+/* ---------------------------------------------------------------------------
+ */
+static int DumpFrames(AVCodecContext *avctx, davs2_picture_t *pic, davs2_seq_info_t *headerset, AVFrame *frm)
+{
+    DAVS2Context *cad = avctx->priv_data;
+    avctx->flags |= AV_CODEC_FLAG_TRUNCATED; // we do not send complete frames.
+
+    if (!headerset) {
+        return 0;
+    }
+
+    if (!pic || pic->ret_type == DAVS2_GOT_HEADER) {
+        avctx->frame_size   = (headerset->horizontal_size * headerset->vertical_size * 3 * headerset->bytes_per_sample) >> 1;
+        avctx->width        = headerset->horizontal_size;
+        avctx->height       = headerset->vertical_size;
+        avctx->pix_fmt      = (headerset->output_bitdepth == 10 ? AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P);
+
+        AVRational r = av_d2q(headerset->frame_rate,4096);
+        avctx->framerate.num = r.num;
+        avctx->framerate.den = r.den;
+        return 0;
+    }
+
+    const int bytes_per_sample = pic->bytes_per_sample;
+
+    for (int i = 0; i < 3; ++i) {
+        int size_plane = pic->width[i] * pic->lines[i] * bytes_per_sample;
+        frm->buf[i]      = av_buffer_alloc(size_plane);
+        frm->data[i]     = frm->buf[i]->data;
+        frm->linesize[i] = pic->width[i] * bytes_per_sample;
+        if(!frm->buf[i] || !frm->data[i] || !frm->linesize[i]){
+            av_log(avctx, AV_LOG_ERROR, "[davs2] dump error: alloc failed.\n");
+            return AVERROR(EINVAL);
+        }
+        memcpy(frm->data[i], pic->planes[i], size_plane);
+    }
+
+    frm->width     = cad->headerset.horizontal_size;
+    frm->height    = cad->headerset.vertical_size;
+    frm->pts       = cad->out_frame.pts;
+    frm->pict_type = pic->type;
+    frm->format    = avctx->pix_fmt;
+
+    cad->decoded_frames++;
+    return 1;
+}
+
+static int ff_davs2_end(AVCodecContext *avctx)
+{
+    DAVS2Context *cad = avctx->priv_data;
+
+    /* close the decoder */
+    if (cad->decoder) {
+        davs2_decoder_close(cad->decoder);
+        av_log(avctx, AV_LOG_VERBOSE, "[davs2] decoder destroyed. 0x%p; frames %d\n", cad->decoder, cad->decoded_frames);
+        cad->decoder = NULL;
+    }
+
+    return 0;
+}
+
+static int davs2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
+{
+    DAVS2Context *cad = avctx->priv_data;
+    int buf_size       = avpkt->size;
+    const uint8_t *buf_ptr = avpkt->data;
+    AVFrame *frm = data;
+    int ret = 0;
+
+    *got_frame = 0;
+    cad->frame = frm;
+    avctx->flags |= AV_CODEC_FLAG_TRUNCATED;
+
+    if (buf_size == 0) {
+        cad->packet.data = buf_ptr;
+        cad->packet.len  = buf_size;
+        cad->packet.pts  = avpkt->pts;
+        cad->packet.dts  = avpkt->dts;
+
+        for (;;) {
+            ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
+
+            if (ret < 0) {
+                return 0;
+            }
+
+            if (cad->out_frame.ret_type != DAVS2_DEFAULT) {
+                *got_frame = DumpFrames(avctx, &cad->out_frame, &cad->headerset, frm);
+                davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
+            }
+            if(*got_frame==1) {
+                break;
+            }
+        }
+        return 0;
+    } else {
+        for(; buf_size > 0;) {
+            int len = buf_size;   // for API-3, pass all data in
+
+            cad->packet.marker = 0;
+            cad->packet.data = buf_ptr;
+            cad->packet.len  = len;
+            cad->packet.pts  = avpkt->pts;
+            cad->packet.dts  = avpkt->dts;
+
+            len = davs2_decoder_decode(cad->decoder, &cad->packet, &cad->headerset, &cad->out_frame);
+
+            if (cad->out_frame.ret_type != DAVS2_DEFAULT) {
+                *got_frame = DumpFrames(avctx, &cad->out_frame, &cad->headerset, frm);
+                davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
+            }
+
+            if (len < 0) {
+                av_log(NULL, AV_LOG_ERROR, "An decoder error counted\n");
+                if (cad->decoder) {
+                    davs2_decoder_close(cad->decoder);
+                    av_log(avctx, AV_LOG_VERBOSE, "[davs2] decoder destroyed. 0x%p; frames %d\n", cad->decoder, cad->decoded_frames);
+                    cad->decoder = NULL;
+                }
+                return AVERROR(EINVAL);
+            }
+
+            buf_ptr     += len;
+            buf_size    -= len;
+
+            if(*got_frame==1) {
+                break;
+            }
+        }
+    }
+
+    buf_size = (buf_ptr - avpkt->data);
+
+    return buf_size;
+}
+
+AVCodec ff_libdavs2_decoder = {
+    .name           = "libdavs2",
+    .long_name      = NULL_IF_CONFIG_SMALL("Decoder for Chinese AVS2"),
+    .type           = AVMEDIA_TYPE_VIDEO,
+    .id             = AV_CODEC_ID_AVS2,
+    .priv_data_size = sizeof(DAVS2Context),
+    .init           = ff_davs2_init,
+    .close          = ff_davs2_end,
+    .decode         = davs2_decode_frame,
+    .capabilities   =  AV_CODEC_CAP_DELAY,//AV_CODEC_CAP_DR1 |
+    .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10,
+                                                     AV_PIX_FMT_NONE },
+};
-- 
2.7.4



More information about the ffmpeg-devel mailing list