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 "libavutil/cpu.h"
26 #include "avcodec.h"
27 #include "internal.h"
28 #include "davs2.h"
29 
30 typedef struct DAVS2Context {
31  void *decoder;
32 
34  davs2_param_t param; // decoding parameters
35  davs2_packet_t packet; // input bitstream
36 
37  davs2_picture_t out_frame; // output data, frame data
38  davs2_seq_info_t headerset; // output data, sequence header
39 
41 
42 static av_cold int davs2_init(AVCodecContext *avctx)
43 {
44  DAVS2Context *cad = avctx->priv_data;
46 
47  /* init the decoder */
48  cad->param.threads = avctx->thread_count;
49  cad->param.info_level = 0;
50  cad->param.disable_avx = !(cpu_flags & AV_CPU_FLAG_AVX &&
52  cad->decoder = davs2_decoder_open(&cad->param);
53 
54  if (!cad->decoder) {
55  av_log(avctx, AV_LOG_ERROR, "decoder created error.");
56  return AVERROR_EXTERNAL;
57  }
58 
59  av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
60  return 0;
61 }
62 
63 static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic, int *got_frame,
64  davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
65 {
66  DAVS2Context *cad = avctx->priv_data;
67  int bytes_per_sample = pic->bytes_per_sample;
68  int plane = 0;
69  int line = 0;
70 
71  if (!headerset) {
72  *got_frame = 0;
73  return 0;
74  }
75 
76  if (!pic || ret_type == DAVS2_GOT_HEADER) {
77  avctx->width = headerset->width;
78  avctx->height = headerset->height;
79  avctx->pix_fmt = headerset->output_bit_depth == 10 ?
81 
82  avctx->framerate = av_d2q(headerset->frame_rate,4096);
83  *got_frame = 0;
84  return 0;
85  }
86 
87  switch (pic->type) {
88  case DAVS2_PIC_I:
89  case DAVS2_PIC_G:
90  frame->pict_type = AV_PICTURE_TYPE_I;
91  break;
92  case DAVS2_PIC_P:
93  case DAVS2_PIC_S:
94  frame->pict_type = AV_PICTURE_TYPE_P;
95  break;
96  case DAVS2_PIC_B:
97  frame->pict_type = AV_PICTURE_TYPE_B;
98  break;
99  case DAVS2_PIC_F:
100  frame->pict_type = AV_PICTURE_TYPE_S;
101  break;
102  default:
103  av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
104  return AVERROR_EXTERNAL;
105  }
106 
107  for (plane = 0; plane < 3; ++plane) {
108  int size_line = pic->widths[plane] * bytes_per_sample;
109  frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
110 
111  if (!frame->buf[plane]){
112  av_log(avctx, AV_LOG_ERROR, "Decoder error: allocation failure, can't dump frames.\n");
113  return AVERROR(ENOMEM);
114  }
115 
116  frame->data[plane] = frame->buf[plane]->data;
117  frame->linesize[plane] = size_line;
118 
119  for (line = 0; line < pic->lines[plane]; ++line)
120  memcpy(frame->data[plane] + line * size_line,
121  pic->planes[plane] + line * pic->strides[plane],
122  pic->widths[plane] * bytes_per_sample);
123  }
124 
125  frame->width = cad->headerset.width;
126  frame->height = cad->headerset.height;
127  frame->pts = cad->out_frame.pts;
128  frame->format = avctx->pix_fmt;
129 
130  *got_frame = 1;
131  return 0;
132 }
133 
134 static void davs2_flush(AVCodecContext *avctx)
135 {
136  DAVS2Context *cad = avctx->priv_data;
137  int ret = DAVS2_GOT_FRAME;
138 
139  while (ret == DAVS2_GOT_FRAME) {
140  ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
141  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
142  }
143 
144  if (ret == DAVS2_ERROR) {
145  av_log(avctx, AV_LOG_WARNING, "Decoder flushing failed.\n");
146  }
147 }
148 
149 static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
150 {
151  DAVS2Context *cad = avctx->priv_data;
152  int ret = DAVS2_DEFAULT;
153 
154  ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
155  if (ret == DAVS2_ERROR) {
156  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't flush delayed frame\n");
157  return AVERROR_EXTERNAL;
158  }
159  if (ret == DAVS2_GOT_FRAME) {
160  ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
161  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
162  }
163  return ret;
164 }
165 
166 static av_cold int davs2_end(AVCodecContext *avctx)
167 {
168  DAVS2Context *cad = avctx->priv_data;
169 
170  /* close the decoder */
171  if (cad->decoder) {
172  davs2_decoder_close(cad->decoder);
173  cad->decoder = NULL;
174  }
175 
176  return 0;
177 }
178 
179 static int davs2_decode_frame(AVCodecContext *avctx, void *data,
180  int *got_frame, AVPacket *avpkt)
181 {
182  DAVS2Context *cad = avctx->priv_data;
183  int buf_size = avpkt->size;
184  uint8_t *buf_ptr = avpkt->data;
185  AVFrame *frame = data;
186  int ret = DAVS2_DEFAULT;
187 
188  /* end of stream, output what is still in the buffers */
189  if (!buf_size) {
190  return send_delayed_frame(avctx, frame, got_frame);
191  }
192 
193  cad->packet.data = buf_ptr;
194  cad->packet.len = buf_size;
195  cad->packet.pts = avpkt->pts;
196  cad->packet.dts = avpkt->dts;
197 
198  ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
199 
200 
201  if (ret == DAVS2_ERROR) {
202  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
203  return AVERROR_EXTERNAL;
204  }
205 
206  ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
207 
208  if (ret != DAVS2_DEFAULT) {
209  ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
210  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
211  }
212 
213  return ret == 0 ? buf_size : ret;
214 }
215 
217  .name = "libdavs2",
218  .long_name = NULL_IF_CONFIG_SMALL("libdavs2 AVS2-P2/IEEE1857.4"),
219  .type = AVMEDIA_TYPE_VIDEO,
220  .id = AV_CODEC_ID_AVS2,
221  .priv_data_size = sizeof(DAVS2Context),
222  .init = davs2_init,
223  .close = davs2_end,
225  .flush = davs2_flush,
227  .caps_internal = FF_CODEC_CAP_AUTO_THREADS,
228  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
229  AV_PIX_FMT_NONE },
230  .wrapper_name = "libdavs2",
231 };
AVCodec
AVCodec.
Definition: codec.h:202
davs2_decode_frame
static int davs2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: libdavs2.c:179
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
AVERROR
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
ff_libdavs2_decoder
const AVCodec ff_libdavs2_decoder
Definition: libdavs2.c:216
DAVS2Context::headerset
davs2_seq_info_t headerset
Definition: libdavs2.c:38
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
davs2_init
static av_cold int davs2_init(AVCodecContext *avctx)
Definition: libdavs2.c:42
AV_CODEC_ID_AVS2
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:244
data
const char data[16]
Definition: mxf.c:143
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:404
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:98
cpu_flags
static atomic_int cpu_flags
Definition: cpu.c:50
init
static int init
Definition: av_tx.c:47
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1710
davs2_dump_frames
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:63
DAVS2Context::packet
davs2_packet_t packet
Definition: libdavs2.c:35
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1440
DAVS2Context::out_frame
davs2_picture_t out_frame
Definition: libdavs2.c:37
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
DAVS2Context::frame
AVFrame * frame
Definition: libdavs2.c:33
av_cold
#define av_cold
Definition: attributes.h:90
decode
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
AV_CODEC_CAP_OTHER_THREADS
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:127
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:296
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:593
NULL
#define NULL
Definition: coverity.c:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: internal.h:81
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_CPU_FLAG_AVX
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren't used.
Definition: cpu.h:47
cpu.h
AV_CPU_FLAG_AVX2
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren't used.
Definition: cpu.h:52
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
line
Definition: graph2dot.c:48
send_delayed_frame
static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
Definition: libdavs2.c:149
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
davs2_flush
static void davs2_flush(AVCodecContext *avctx)
Definition: libdavs2.c:134
av_d2q
AVRational av_d2q(double d, int max)
Convert a double precision floating point number to a rational.
Definition: rational.c:106
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
DAVS2Context::param
davs2_param_t param
Definition: libdavs2.c:34
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
avcodec.h
davs2_end
static av_cold int davs2_end(AVCodecContext *avctx)
Definition: libdavs2.c:166
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_CODEC_CAP_DELAY
#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:82
DAVS2Context::decoder
void * decoder
Definition: libdavs2.c:31
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:277
DAVS2Context
Definition: libdavs2.c:30
line
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:40