FFmpeg
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 "davs2.h"
27 
28 typedef struct DAVS2Context {
29  void *decoder;
30 
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 
39 
40 static av_cold int davs2_init(AVCodecContext *avctx)
41 {
42  DAVS2Context *cad = avctx->priv_data;
44 
45  /* init the decoder */
46  cad->param.threads = avctx->thread_count;
47  cad->param.info_level = 0;
48  cad->param.disable_avx = !(cpu_flags & AV_CPU_FLAG_AVX &&
49  cpu_flags & AV_CPU_FLAG_AVX2);
50  cad->decoder = davs2_decoder_open(&cad->param);
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, int *got_frame,
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  *got_frame = 0;
71  return 0;
72  }
73 
74  if (!pic || ret_type == DAVS2_GOT_HEADER) {
75  avctx->width = headerset->width;
76  avctx->height = headerset->height;
77  avctx->pix_fmt = headerset->output_bit_depth == 10 ?
79 
80  avctx->framerate = av_d2q(headerset->frame_rate,4096);
81  *got_frame = 0;
82  return 0;
83  }
84 
85  switch (pic->type) {
86  case DAVS2_PIC_I:
87  case DAVS2_PIC_G:
89  break;
90  case DAVS2_PIC_P:
91  case DAVS2_PIC_S:
93  break;
94  case DAVS2_PIC_B:
96  break;
97  case DAVS2_PIC_F:
99  break;
100  default:
101  av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
102  return AVERROR_EXTERNAL;
103  }
104 
105  for (plane = 0; plane < 3; ++plane) {
106  int size_line = pic->widths[plane] * bytes_per_sample;
107  frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
108 
109  if (!frame->buf[plane]){
110  av_log(avctx, AV_LOG_ERROR, "Decoder error: allocation failure, can't dump frames.\n");
111  return AVERROR(ENOMEM);
112  }
113 
114  frame->data[plane] = frame->buf[plane]->data;
115  frame->linesize[plane] = size_line;
116 
117  for (line = 0; line < pic->lines[plane]; ++line)
118  memcpy(frame->data[plane] + line * size_line,
119  pic->planes[plane] + line * pic->strides[plane],
120  pic->widths[plane] * bytes_per_sample);
121  }
122 
123  frame->width = cad->headerset.width;
124  frame->height = cad->headerset.height;
125  frame->pts = cad->out_frame.pts;
126  frame->format = avctx->pix_fmt;
127 
128  *got_frame = 1;
129  return 0;
130 }
131 
132 static void davs2_flush(AVCodecContext *avctx)
133 {
134  DAVS2Context *cad = avctx->priv_data;
135  int ret = DAVS2_GOT_FRAME;
136 
137  while (ret == DAVS2_GOT_FRAME) {
138  ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
139  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
140  }
141 
142  if (ret == DAVS2_ERROR) {
143  av_log(avctx, AV_LOG_WARNING, "Decoder flushing failed.\n");
144  }
145 }
146 
147 static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
148 {
149  DAVS2Context *cad = avctx->priv_data;
150  int ret = DAVS2_DEFAULT;
151 
152  ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
153  if (ret == DAVS2_ERROR) {
154  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't flush delayed frame\n");
155  return AVERROR_EXTERNAL;
156  }
157  if (ret == DAVS2_GOT_FRAME) {
158  ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
159  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
160  }
161  return ret;
162 }
163 
164 static av_cold int davs2_end(AVCodecContext *avctx)
165 {
166  DAVS2Context *cad = avctx->priv_data;
167 
168  /* close the decoder */
169  if (cad->decoder) {
170  davs2_decoder_close(cad->decoder);
171  cad->decoder = NULL;
172  }
173 
174  return 0;
175 }
176 
177 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
178  int *got_frame, AVPacket *avpkt)
179 {
180  DAVS2Context *cad = avctx->priv_data;
181  int buf_size = avpkt->size;
182  uint8_t *buf_ptr = avpkt->data;
183  AVFrame *frame = data;
184  int ret = DAVS2_DEFAULT;
185 
186  /* end of stream, output what is still in the buffers */
187  if (!buf_size) {
188  return send_delayed_frame(avctx, frame, got_frame);
189  }
190 
191  cad->packet.data = buf_ptr;
192  cad->packet.len = buf_size;
193  cad->packet.pts = avpkt->pts;
194  cad->packet.dts = avpkt->dts;
195 
196  ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
197 
198 
199  if (ret == DAVS2_ERROR) {
200  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
201  return AVERROR_EXTERNAL;
202  }
203 
204  ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
205 
206  if (ret != DAVS2_DEFAULT) {
207  ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
208  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
209  }
210 
211  return ret == 0 ? buf_size : ret;
212 }
213 
215  .name = "libdavs2",
216  .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
217  .type = AVMEDIA_TYPE_VIDEO,
218  .id = AV_CODEC_ID_AVS2,
219  .priv_data_size = sizeof(DAVS2Context),
220  .init = davs2_init,
221  .close = davs2_end,
223  .flush = davs2_flush,
225  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
226  AV_PIX_FMT_NONE },
227  .wrapper_name = "libdavs2",
228 };
int plane
Definition: avisynth_c.h:384
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:49
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3101
S(GMC)-VOP MPEG-4.
Definition: avutil.h:277
This structure describes decoded (raw) audio or video data.
Definition: frame.h:268
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
davs2_picture_t out_frame
Definition: libdavs2.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVFrame * frame
Definition: libdavs2.c:31
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:459
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
davs2_packet_t packet
Definition: libdavs2.c:33
int size
Definition: avcodec.h:1478
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
static atomic_int cpu_flags
Definition: cpu.c:50
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1049
AVCodec.
Definition: avcodec.h:3477
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: libdavs2.c:147
#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: avcodec.h:1006
uint8_t
#define av_cold
Definition: attributes.h:82
AVCodec ff_libdavs2_decoder
Definition: libdavs2.c:214
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:361
uint8_t * data
Definition: avcodec.h:1477
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren&#39;t used.
Definition: cpu.h:54
#define av_log(a,...)
int width
Definition: frame.h:326
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_cold int davs2_end(AVCodecContext *avctx)
Definition: libdavs2.c:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
Definition: graph2dot.c:48
const char * name
Name of the codec implementation.
Definition: avcodec.h:3484
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:351
int width
picture width / height.
Definition: avcodec.h:1738
static av_cold int davs2_init(AVCodecContext *avctx)
Definition: libdavs2.c:40
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2820
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:341
Libavcodec external API header.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:299
main external API structure.
Definition: avcodec.h:1565
uint8_t * data
The data buffer.
Definition: buffer.h:89
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:387
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:61
static void davs2_flush(AVCodecContext *avctx)
Definition: libdavs2.c:132
void * decoder
Definition: libdavs2.c:29
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:93
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
davs2_seq_info_t headerset
Definition: libdavs2.c:36
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:282
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:33
static int davs2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libdavs2.c:177
Bi-dir predicted.
Definition: avutil.h:276
void * priv_data
Definition: avcodec.h:1592
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1476
int height
Definition: frame.h:326
davs2_param_t param
Definition: libdavs2.c:32
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1454
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
Predicted.
Definition: avutil.h:275