]> git.sesse.net Git - ffmpeg/blob - libavcodec/libdavs2.c
lavc/libdavs2: output delayed frames
[ffmpeg] / libavcodec / libdavs2.c
1 /*
2  * AVS2 decoding using the davs2 library
3  *
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>
7  *
8  * This file is part of FFmpeg.
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "avcodec.h"
26 #include "davs2.h"
27
28 typedef struct DAVS2Context {
29     void *decoder;
30
31     AVFrame *frame;
32     davs2_param_t    param;      // decoding parameters
33     davs2_packet_t   packet;     // input bitstream
34
35     davs2_picture_t  out_frame;  // output data, frame data
36     davs2_seq_info_t headerset;  // output data, sequence header
37
38 }DAVS2Context;
39
40 static av_cold int davs2_init(AVCodecContext *avctx)
41 {
42     DAVS2Context *cad = avctx->priv_data;
43     int cpu_flags = av_get_cpu_flags();
44
45     /* init the decoder */
46     cad->param.threads      = avctx->thread_count;
47     cad->param.info_level   = 0;
48     cad->decoder            = davs2_decoder_open(&cad->param);
49     cad->param.disable_avx  = !(cpu_flags & AV_CPU_FLAG_AVX &&
50                                 cpu_flags & AV_CPU_FLAG_AVX2);
51
52     if (!cad->decoder) {
53         av_log(avctx, AV_LOG_ERROR, "decoder created error.");
54         return AVERROR_EXTERNAL;
55     }
56
57     av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
58     return 0;
59 }
60
61 static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic,
62                              davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
63 {
64     DAVS2Context *cad    = avctx->priv_data;
65     int bytes_per_sample = pic->bytes_per_sample;
66     int plane = 0;
67     int line  = 0;
68
69     if (!headerset)
70         return 0;
71
72     if (!pic || ret_type == DAVS2_GOT_HEADER) {
73         avctx->width     = headerset->width;
74         avctx->height    = headerset->height;
75         avctx->pix_fmt   = headerset->output_bit_depth == 10 ?
76                            AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P;
77
78         avctx->framerate = av_d2q(headerset->frame_rate,4096);
79         return 0;
80     }
81
82     switch (pic->type) {
83         case DAVS2_PIC_I:
84         case DAVS2_PIC_G:
85             frame->pict_type = AV_PICTURE_TYPE_I;
86             break;
87         case DAVS2_PIC_P:
88         case DAVS2_PIC_S:
89             frame->pict_type = AV_PICTURE_TYPE_P;
90             break;
91         case DAVS2_PIC_B:
92             frame->pict_type = AV_PICTURE_TYPE_B;
93             break;
94         case DAVS2_PIC_F:
95             frame->pict_type = AV_PICTURE_TYPE_S;
96             break;
97         default:
98             av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
99             return AVERROR_EXTERNAL;
100     }
101
102     for (plane = 0; plane < 3; ++plane) {
103         int size_line = pic->widths[plane] * bytes_per_sample;
104         frame->buf[plane]  = av_buffer_alloc(size_line * pic->lines[plane]);
105
106         if (!frame->buf[plane]){
107             av_log(avctx, AV_LOG_ERROR, "dump error: alloc failed.\n");
108             return AVERROR(ENOMEM);
109         }
110
111         frame->data[plane]     = frame->buf[plane]->data;
112         frame->linesize[plane] = size_line;
113
114         for (line = 0; line < pic->lines[plane]; ++line)
115             memcpy(frame->data[plane] + line * size_line,
116                    pic->planes[plane] + line * pic->strides[plane],
117                    pic->widths[plane] * bytes_per_sample);
118     }
119
120     frame->width     = cad->headerset.width;
121     frame->height    = cad->headerset.height;
122     frame->pts       = cad->out_frame.pts;
123     frame->format    = avctx->pix_fmt;
124
125     return 1;
126 }
127
128 static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
129 {
130     DAVS2Context *cad      = avctx->priv_data;
131     int           ret      = DAVS2_DEFAULT;
132
133     ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
134     if (ret == DAVS2_ERROR) {
135         av_log(avctx, AV_LOG_ERROR, "Decoder error: can't flush delayed frame\n");
136         return AVERROR_EXTERNAL;
137     }
138     if (ret == DAVS2_GOT_FRAME) {
139         *got_frame = davs2_dump_frames(avctx, &cad->out_frame, &cad->headerset, ret, frame);
140         davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
141     }
142     return ret;
143 }
144
145 static av_cold int davs2_end(AVCodecContext *avctx)
146 {
147     DAVS2Context *cad = avctx->priv_data;
148
149     /* close the decoder */
150     if (cad->decoder) {
151         davs2_decoder_close(cad->decoder);
152         cad->decoder = NULL;
153     }
154
155     return 0;
156 }
157
158 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
159                               int *got_frame, AVPacket *avpkt)
160 {
161     DAVS2Context *cad      = avctx->priv_data;
162     int           buf_size = avpkt->size;
163     uint8_t      *buf_ptr  = avpkt->data;
164     AVFrame      *frame    = data;
165     int           ret      = DAVS2_DEFAULT;
166
167     /* end of stream, output what is still in the buffers */
168     if (!buf_size) {
169         return send_delayed_frame(avctx, frame, got_frame);
170     }
171
172     cad->packet.data = buf_ptr;
173     cad->packet.len  = buf_size;
174     cad->packet.pts  = avpkt->pts;
175     cad->packet.dts  = avpkt->dts;
176
177     ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
178
179
180     if (ret == DAVS2_ERROR) {
181         av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
182         return AVERROR_EXTERNAL;
183     }
184
185     ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
186
187     if (ret != DAVS2_DEFAULT) {
188         *got_frame = davs2_dump_frames(avctx, &cad->out_frame, &cad->headerset, ret, frame);
189         davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
190     }
191
192     return buf_size;
193 }
194
195 AVCodec ff_libdavs2_decoder = {
196     .name           = "libdavs2",
197     .long_name      = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
198     .type           = AVMEDIA_TYPE_VIDEO,
199     .id             = AV_CODEC_ID_AVS2,
200     .priv_data_size = sizeof(DAVS2Context),
201     .init           = davs2_init,
202     .close          = davs2_end,
203     .decode         = davs2_decode_frame,
204     .capabilities   =  AV_CODEC_CAP_DELAY,//AV_CODEC_CAP_DR1 |
205     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
206                                                      AV_PIX_FMT_NONE },
207     .wrapper_name   = "libdavs2",
208 };