2 * AVS2 decoding using the davs2 library
4 * Copyright (C) 2018 Yiqun Xu, <yiqun.xu@vipl.ict.ac.cn>
5 * Falei Luo, <falei.luo@gmail.com>
6 * Huiwen Ren, <hwrenx@gmail.com>
8 * This file is part of FFmpeg.
10 * FFmpeg is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * FFmpeg is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with FFmpeg; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 typedef struct DAVS2Context {
33 davs2_param_t param; // decoding parameters
34 davs2_packet_t packet; // input bitstream
36 davs2_picture_t out_frame; // output data, frame data
37 davs2_seq_info_t headerset; // output data, sequence header
41 static av_cold int davs2_init(AVCodecContext *avctx)
43 DAVS2Context *cad = avctx->priv_data;
44 int cpu_flags = av_get_cpu_flags();
46 /* init the decoder */
47 cad->param.threads = avctx->thread_count;
48 cad->param.info_level = 0;
49 cad->param.disable_avx = !(cpu_flags & AV_CPU_FLAG_AVX &&
50 cpu_flags & AV_CPU_FLAG_AVX2);
51 cad->decoder = davs2_decoder_open(&cad->param);
54 av_log(avctx, AV_LOG_ERROR, "decoder created error.");
55 return AVERROR_EXTERNAL;
58 av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
62 static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic, int *got_frame,
63 davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
65 DAVS2Context *cad = avctx->priv_data;
66 int bytes_per_sample = pic->bytes_per_sample;
75 if (!pic || ret_type == DAVS2_GOT_HEADER) {
76 avctx->width = headerset->width;
77 avctx->height = headerset->height;
78 avctx->pix_fmt = headerset->output_bit_depth == 10 ?
79 AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P;
81 avctx->framerate = av_d2q(headerset->frame_rate,4096);
89 frame->pict_type = AV_PICTURE_TYPE_I;
93 frame->pict_type = AV_PICTURE_TYPE_P;
96 frame->pict_type = AV_PICTURE_TYPE_B;
99 frame->pict_type = AV_PICTURE_TYPE_S;
102 av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
103 return AVERROR_EXTERNAL;
106 for (plane = 0; plane < 3; ++plane) {
107 int size_line = pic->widths[plane] * bytes_per_sample;
108 frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
110 if (!frame->buf[plane]){
111 av_log(avctx, AV_LOG_ERROR, "Decoder error: allocation failure, can't dump frames.\n");
112 return AVERROR(ENOMEM);
115 frame->data[plane] = frame->buf[plane]->data;
116 frame->linesize[plane] = size_line;
118 for (line = 0; line < pic->lines[plane]; ++line)
119 memcpy(frame->data[plane] + line * size_line,
120 pic->planes[plane] + line * pic->strides[plane],
121 pic->widths[plane] * bytes_per_sample);
124 frame->width = cad->headerset.width;
125 frame->height = cad->headerset.height;
126 frame->pts = cad->out_frame.pts;
127 frame->format = avctx->pix_fmt;
133 static void davs2_flush(AVCodecContext *avctx)
135 DAVS2Context *cad = avctx->priv_data;
136 int ret = DAVS2_GOT_FRAME;
138 while (ret == DAVS2_GOT_FRAME) {
139 ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
140 davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
143 if (ret == DAVS2_ERROR) {
144 av_log(avctx, AV_LOG_WARNING, "Decoder flushing failed.\n");
148 static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
150 DAVS2Context *cad = avctx->priv_data;
151 int ret = DAVS2_DEFAULT;
153 ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
154 if (ret == DAVS2_ERROR) {
155 av_log(avctx, AV_LOG_ERROR, "Decoder error: can't flush delayed frame\n");
156 return AVERROR_EXTERNAL;
158 if (ret == DAVS2_GOT_FRAME) {
159 ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
160 davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
165 static av_cold int davs2_end(AVCodecContext *avctx)
167 DAVS2Context *cad = avctx->priv_data;
169 /* close the decoder */
171 davs2_decoder_close(cad->decoder);
178 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
179 int *got_frame, AVPacket *avpkt)
181 DAVS2Context *cad = avctx->priv_data;
182 int buf_size = avpkt->size;
183 uint8_t *buf_ptr = avpkt->data;
184 AVFrame *frame = data;
185 int ret = DAVS2_DEFAULT;
187 /* end of stream, output what is still in the buffers */
189 return send_delayed_frame(avctx, frame, got_frame);
192 cad->packet.data = buf_ptr;
193 cad->packet.len = buf_size;
194 cad->packet.pts = avpkt->pts;
195 cad->packet.dts = avpkt->dts;
197 ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
200 if (ret == DAVS2_ERROR) {
201 av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
202 return AVERROR_EXTERNAL;
205 ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
207 if (ret != DAVS2_DEFAULT) {
208 ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
209 davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
212 return ret == 0 ? buf_size : ret;
215 AVCodec ff_libdavs2_decoder = {
217 .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
218 .type = AVMEDIA_TYPE_VIDEO,
219 .id = AV_CODEC_ID_AVS2,
220 .priv_data_size = sizeof(DAVS2Context),
223 .decode = davs2_decode_frame,
224 .flush = davs2_flush,
225 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
226 .caps_internal = FF_CODEC_CAP_AUTO_THREADS,
227 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
229 .wrapper_name = "libdavs2",