FFmpeg  4.4.6
libdavs2.c
Go to the documentation of this file.
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 "internal.h"
27 #include "davs2.h"
28 
29 typedef struct DAVS2Context {
30  void *decoder;
31 
33  davs2_param_t param; // decoding parameters
34  davs2_packet_t packet; // input bitstream
35 
36  davs2_picture_t out_frame; // output data, frame data
37  davs2_seq_info_t headerset; // output data, sequence header
38 
40 
41 static av_cold int davs2_init(AVCodecContext *avctx)
42 {
43  DAVS2Context *cad = avctx->priv_data;
45 
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 &&
51  cad->decoder = davs2_decoder_open(&cad->param);
52 
53  if (!cad->decoder) {
54  av_log(avctx, AV_LOG_ERROR, "decoder created error.");
55  return AVERROR_EXTERNAL;
56  }
57 
58  av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
59  return 0;
60 }
61 
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)
64 {
65  DAVS2Context *cad = avctx->priv_data;
66  int bytes_per_sample = pic->bytes_per_sample;
67  int plane = 0;
68  int line = 0;
69 
70  if (!headerset) {
71  *got_frame = 0;
72  return 0;
73  }
74 
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 ?
80 
81  avctx->framerate = av_d2q(headerset->frame_rate,4096);
82  *got_frame = 0;
83  return 0;
84  }
85 
86  switch (pic->type) {
87  case DAVS2_PIC_I:
88  case DAVS2_PIC_G:
90  break;
91  case DAVS2_PIC_P:
92  case DAVS2_PIC_S:
94  break;
95  case DAVS2_PIC_B:
97  break;
98  case DAVS2_PIC_F:
100  break;
101  default:
102  av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
103  return AVERROR_EXTERNAL;
104  }
105 
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]);
109 
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);
113  }
114 
115  frame->data[plane] = frame->buf[plane]->data;
116  frame->linesize[plane] = size_line;
117 
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);
122  }
123 
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;
128 
129  *got_frame = 1;
130  return 0;
131 }
132 
133 static void davs2_flush(AVCodecContext *avctx)
134 {
135  DAVS2Context *cad = avctx->priv_data;
136  int ret = DAVS2_GOT_FRAME;
137 
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);
141  }
142 
143  if (ret == DAVS2_ERROR) {
144  av_log(avctx, AV_LOG_WARNING, "Decoder flushing failed.\n");
145  }
146 }
147 
148 static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
149 {
150  DAVS2Context *cad = avctx->priv_data;
151  int ret = DAVS2_DEFAULT;
152 
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;
157  }
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);
161  }
162  return ret;
163 }
164 
165 static av_cold int davs2_end(AVCodecContext *avctx)
166 {
167  DAVS2Context *cad = avctx->priv_data;
168 
169  /* close the decoder */
170  if (cad->decoder) {
171  davs2_decoder_close(cad->decoder);
172  cad->decoder = NULL;
173  }
174 
175  return 0;
176 }
177 
178 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
179  int *got_frame, AVPacket *avpkt)
180 {
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;
186 
187  /* end of stream, output what is still in the buffers */
188  if (!buf_size) {
189  return send_delayed_frame(avctx, frame, got_frame);
190  }
191 
192  cad->packet.data = buf_ptr;
193  cad->packet.len = buf_size;
194  cad->packet.pts = avpkt->pts;
195  cad->packet.dts = avpkt->dts;
196 
197  ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
198 
199 
200  if (ret == DAVS2_ERROR) {
201  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
202  return AVERROR_EXTERNAL;
203  }
204 
205  ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
206 
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);
210  }
211 
212  return ret == 0 ? buf_size : ret;
213 }
214 
216  .name = "libdavs2",
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),
221  .init = davs2_init,
222  .close = davs2_end,
224  .flush = davs2_flush,
226  .caps_internal = FF_CODEC_CAP_AUTO_THREADS,
227  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
228  AV_PIX_FMT_NONE },
229  .wrapper_name = "libdavs2",
230 };
static void flush(AVCodecContext *avctx)
#define av_cold
Definition: attributes.h:88
uint8_t
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define NULL
Definition: coverity.c:32
static atomic_int cpu_flags
Definition: cpu.c:50
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:95
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren't used.
Definition: cpu.h:54
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren't used.
Definition: cpu.h:49
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static AVFrame * frame
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:122
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:243
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:277
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: internal.h:80
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static int davs2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libdavs2.c:178
static av_cold int davs2_init(AVCodecContext *avctx)
Definition: libdavs2.c:41
AVCodec ff_libdavs2_decoder
Definition: libdavs2.c:215
static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic, int *got_frame, davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
Definition: libdavs2.c:62
static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: libdavs2.c:148
static void davs2_flush(AVCodecContext *avctx)
Definition: libdavs2.c:133
static av_cold int davs2_end(AVCodecContext *avctx)
Definition: libdavs2.c:165
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
const char data[16]
Definition: mxf.c:142
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
uint8_t * data
The data buffer.
Definition: buffer.h:92
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
AVRational framerate
Definition: avcodec.h:2075
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1777
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
AVFrame * frame
Definition: libdavs2.c:32
davs2_picture_t out_frame
Definition: libdavs2.c:36
davs2_param_t param
Definition: libdavs2.c:33
davs2_seq_info_t headerset
Definition: libdavs2.c:37
void * decoder
Definition: libdavs2.c:30
davs2_packet_t packet
Definition: libdavs2.c:34
Definition: graph2dot.c:48
#define av_log(a,...)