]> git.sesse.net Git - ffmpeg/blob - libavcodec/libdavs2.c
lavc/libdavs2: correct frame type setting
[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
44     /* init the decoder */
45     cad->param.threads      = avctx->thread_count;
46     cad->param.info_level   = 0;
47     cad->decoder            = davs2_decoder_open(&cad->param);
48
49     if (!cad->decoder) {
50         av_log(avctx, AV_LOG_ERROR, "decoder created error.");
51         return AVERROR_EXTERNAL;
52     }
53
54     av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
55     return 0;
56 }
57
58 static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic,
59                              davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
60 {
61     DAVS2Context *cad    = avctx->priv_data;
62     int bytes_per_sample = pic->bytes_per_sample;
63     int plane = 0;
64     int line  = 0;
65
66     if (!headerset)
67         return 0;
68
69     if (!pic || ret_type == DAVS2_GOT_HEADER) {
70         avctx->width     = headerset->width;
71         avctx->height    = headerset->height;
72         avctx->pix_fmt   = headerset->output_bit_depth == 10 ?
73                            AV_PIX_FMT_YUV420P10 : AV_PIX_FMT_YUV420P;
74
75         avctx->framerate = av_d2q(headerset->frame_rate,4096);
76         return 0;
77     }
78
79     switch (pic->type) {
80         case DAVS2_PIC_I:
81         case DAVS2_PIC_G:
82             frame->pict_type = AV_PICTURE_TYPE_I;
83             break;
84         case DAVS2_PIC_P:
85         case DAVS2_PIC_S:
86             frame->pict_type = AV_PICTURE_TYPE_P;
87             break;
88         case DAVS2_PIC_B:
89             frame->pict_type = AV_PICTURE_TYPE_B;
90             break;
91         case DAVS2_PIC_F:
92             frame->pict_type = AV_PICTURE_TYPE_S;
93             break;
94         default:
95             av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
96             return AVERROR_EXTERNAL;
97     }
98
99     for (plane = 0; plane < 3; ++plane) {
100         int size_line = pic->widths[plane] * bytes_per_sample;
101         frame->buf[plane]  = av_buffer_alloc(size_line * pic->lines[plane]);
102
103         if (!frame->buf[plane]){
104             av_log(avctx, AV_LOG_ERROR, "dump error: alloc failed.\n");
105             return AVERROR(ENOMEM);
106         }
107
108         frame->data[plane]     = frame->buf[plane]->data;
109         frame->linesize[plane] = pic->widths[plane];
110
111         for (line = 0; line < pic->lines[plane]; ++line)
112             memcpy(frame->data[plane] + line * size_line,
113                    pic->planes[plane] + line * pic->strides[plane],
114                    pic->widths[plane] * bytes_per_sample);
115     }
116
117     frame->width     = cad->headerset.width;
118     frame->height    = cad->headerset.height;
119     frame->pts       = cad->out_frame.pts;
120     frame->format    = avctx->pix_fmt;
121
122     return 1;
123 }
124
125 static av_cold int davs2_end(AVCodecContext *avctx)
126 {
127     DAVS2Context *cad = avctx->priv_data;
128
129     /* close the decoder */
130     if (cad->decoder) {
131         davs2_decoder_close(cad->decoder);
132         cad->decoder = NULL;
133     }
134
135     return 0;
136 }
137
138 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
139                               int *got_frame, AVPacket *avpkt)
140 {
141     DAVS2Context *cad      = avctx->priv_data;
142     int           buf_size = avpkt->size;
143     uint8_t      *buf_ptr  = avpkt->data;
144     AVFrame      *frame    = data;
145     int           ret      = DAVS2_DEFAULT;
146
147     if (!buf_size) {
148         return 0;
149     }
150
151     cad->packet.data = buf_ptr;
152     cad->packet.len  = buf_size;
153     cad->packet.pts  = avpkt->pts;
154     cad->packet.dts  = avpkt->dts;
155
156     ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
157
158
159     if (ret == DAVS2_ERROR) {
160         av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
161         return AVERROR_EXTERNAL;
162     }
163
164     ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
165
166     if (ret != DAVS2_DEFAULT) {
167         *got_frame = davs2_dump_frames(avctx, &cad->out_frame, &cad->headerset, ret, frame);
168         davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
169     }
170
171     return buf_size;
172 }
173
174 AVCodec ff_libdavs2_decoder = {
175     .name           = "libdavs2",
176     .long_name      = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
177     .type           = AVMEDIA_TYPE_VIDEO,
178     .id             = AV_CODEC_ID_AVS2,
179     .priv_data_size = sizeof(DAVS2Context),
180     .init           = davs2_init,
181     .close          = davs2_end,
182     .decode         = davs2_decode_frame,
183     .capabilities   =  AV_CODEC_CAP_DELAY,//AV_CODEC_CAP_DR1 |
184     .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
185                                                      AV_PIX_FMT_NONE },
186     .wrapper_name   = "libdavs2",
187 };