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 "codec_internal.h"
28 #include "avs2.h"
29 #include "davs2.h"
30 
31 typedef struct DAVS2Context {
32  void *decoder;
33 
35  davs2_param_t param; // decoding parameters
36  davs2_packet_t packet; // input bitstream
37 
38  davs2_picture_t out_frame; // output data, frame data
39  davs2_seq_info_t headerset; // output data, sequence header
40 
42 
43 static av_cold int davs2_init(AVCodecContext *avctx)
44 {
45  DAVS2Context *cad = avctx->priv_data;
47 
48  /* init the decoder */
49  cad->param.threads = avctx->thread_count;
50  cad->param.info_level = 0;
51  cad->param.disable_avx = !(cpu_flags & AV_CPU_FLAG_AVX &&
53  cad->decoder = davs2_decoder_open(&cad->param);
54 
55  if (!cad->decoder) {
56  av_log(avctx, AV_LOG_ERROR, "decoder created error.");
57  return AVERROR_EXTERNAL;
58  }
59 
60  av_log(avctx, AV_LOG_VERBOSE, "decoder created. %p\n", cad->decoder);
61  return 0;
62 }
63 
64 static int davs2_dump_frames(AVCodecContext *avctx, davs2_picture_t *pic, int *got_frame,
65  davs2_seq_info_t *headerset, int ret_type, AVFrame *frame)
66 {
67  DAVS2Context *cad = avctx->priv_data;
68  int bytes_per_sample = pic->bytes_per_sample;
69  int plane = 0;
70  int line = 0;
71 
72  if (!headerset) {
73  *got_frame = 0;
74  return 0;
75  }
76 
77  if (!pic || ret_type == DAVS2_GOT_HEADER) {
78  avctx->width = headerset->width;
79  avctx->height = headerset->height;
80  avctx->pix_fmt = headerset->output_bit_depth == 10 ?
82  /* It should be picture_reorder_delay, but libdavs2 doesn't export that
83  * info.
84  * Use FFMAX since has_b_frames could be set by AVS2 parser in theory,
85  * which doesn't do it yet.
86  */
87  avctx->has_b_frames = FFMAX(avctx->has_b_frames, !headerset->low_delay);
88 
89  if (headerset->frame_rate_id < 16)
90  avctx->framerate = ff_avs2_frame_rate_tab[headerset->frame_rate_id];
91  *got_frame = 0;
92  return 0;
93  }
94 
95  switch (pic->type) {
96  case DAVS2_PIC_I:
97  case DAVS2_PIC_G:
99  break;
100  case DAVS2_PIC_P:
101  case DAVS2_PIC_S:
103  break;
104  case DAVS2_PIC_B:
106  break;
107  case DAVS2_PIC_F:
109  break;
110  default:
111  av_log(avctx, AV_LOG_ERROR, "Decoder error: unknown frame type\n");
112  return AVERROR_EXTERNAL;
113  }
114 
115  for (plane = 0; plane < 3; ++plane) {
116  int size_line = pic->widths[plane] * bytes_per_sample;
117  frame->buf[plane] = av_buffer_alloc(size_line * pic->lines[plane]);
118 
119  if (!frame->buf[plane]){
120  av_log(avctx, AV_LOG_ERROR, "Decoder error: allocation failure, can't dump frames.\n");
121  return AVERROR(ENOMEM);
122  }
123 
124  frame->data[plane] = frame->buf[plane]->data;
125  frame->linesize[plane] = size_line;
126 
127  for (line = 0; line < pic->lines[plane]; ++line)
128  memcpy(frame->data[plane] + line * size_line,
129  pic->planes[plane] + line * pic->strides[plane],
130  pic->widths[plane] * bytes_per_sample);
131  }
132 
133  frame->width = cad->headerset.width;
134  frame->height = cad->headerset.height;
135  frame->pts = cad->out_frame.pts;
136  frame->format = avctx->pix_fmt;
137 
138  *got_frame = 1;
139  return 0;
140 }
141 
142 static void davs2_flush(AVCodecContext *avctx)
143 {
144  DAVS2Context *cad = avctx->priv_data;
145  int ret = DAVS2_GOT_FRAME;
146 
147  while (ret == DAVS2_GOT_FRAME) {
148  ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
149  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
150  }
151 
152  if (ret == DAVS2_ERROR) {
153  av_log(avctx, AV_LOG_WARNING, "Decoder flushing failed.\n");
154  }
155 }
156 
157 static int send_delayed_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame)
158 {
159  DAVS2Context *cad = avctx->priv_data;
160  int ret = DAVS2_DEFAULT;
161 
162  ret = davs2_decoder_flush(cad->decoder, &cad->headerset, &cad->out_frame);
163  if (ret == DAVS2_ERROR) {
164  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't flush delayed frame\n");
165  return AVERROR_EXTERNAL;
166  }
167  if (ret == DAVS2_GOT_FRAME) {
168  ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
169  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
170  }
171  return ret;
172 }
173 
174 static av_cold int davs2_end(AVCodecContext *avctx)
175 {
176  DAVS2Context *cad = avctx->priv_data;
177 
178  /* close the decoder */
179  if (cad->decoder) {
180  davs2_flush(avctx);
181  davs2_decoder_close(cad->decoder);
182  cad->decoder = NULL;
183  }
184 
185  return 0;
186 }
187 
189  int *got_frame, AVPacket *avpkt)
190 {
191  DAVS2Context *cad = avctx->priv_data;
192  int buf_size = avpkt->size;
193  const uint8_t *buf_ptr = avpkt->data;
194  int ret = DAVS2_DEFAULT;
195 
196  /* end of stream, output what is still in the buffers */
197  if (!buf_size) {
198  return send_delayed_frame(avctx, frame, got_frame);
199  }
200 
201  cad->packet.data = buf_ptr;
202  cad->packet.len = buf_size;
203  cad->packet.pts = avpkt->pts;
204  cad->packet.dts = avpkt->dts;
205 
206  ret = davs2_decoder_send_packet(cad->decoder, &cad->packet);
207 
208 
209  if (ret == DAVS2_ERROR) {
210  av_log(avctx, AV_LOG_ERROR, "Decoder error: can't read packet\n");
211  return AVERROR_EXTERNAL;
212  }
213 
214  ret = davs2_decoder_recv_frame(cad->decoder, &cad->headerset, &cad->out_frame);
215 
216  if (ret != DAVS2_DEFAULT) {
217  ret = davs2_dump_frames(avctx, &cad->out_frame, got_frame, &cad->headerset, ret, frame);
218  davs2_decoder_frame_unref(cad->decoder, &cad->out_frame);
219  }
220 
221  return ret == 0 ? buf_size : ret;
222 }
223 
225  .p.name = "libdavs2",
226  CODEC_LONG_NAME("libdavs2 AVS2-P2/IEEE1857.4"),
227  .p.type = AVMEDIA_TYPE_VIDEO,
228  .p.id = AV_CODEC_ID_AVS2,
229  .priv_data_size = sizeof(DAVS2Context),
230  .init = davs2_init,
231  .close = davs2_end,
233  .flush = davs2_flush,
235  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
237  .p.pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
238  AV_PIX_FMT_NONE },
239  .p.wrapper_name = "libdavs2",
240 };
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
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
DAVS2Context::headerset
davs2_seq_info_t headerset
Definition: libdavs2.c:39
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVFrame::width
int width
Definition: frame.h:412
AVPacket::data
uint8_t * data
Definition: packet.h:491
davs2_init
static av_cold int davs2_init(AVCodecContext *avctx)
Definition: libdavs2.c:43
AV_CODEC_ID_AVS2
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:246
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:468
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:103
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
cpu_flags
static atomic_int cpu_flags
Definition: cpu.c:52
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1803
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:64
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
DAVS2Context::packet
davs2_packet_t packet
Definition: libdavs2.c:36
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1532
DAVS2Context::out_frame
davs2_picture_t out_frame
Definition: libdavs2.c:38
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:34
av_cold
#define av_cold
Definition: attributes.h:90
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:744
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
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:124
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:365
ff_libdavs2_decoder
const FFCodec ff_libdavs2_decoder
Definition: libdavs2.c:224
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
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_avs2_frame_rate_tab
const AVRational ff_avs2_frame_rate_tab[16]
Definition: avs2.c:25
NULL
#define NULL
Definition: coverity.c:32
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:442
AVPacket::size
int size
Definition: packet.h:492
codec_internal.h
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
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:427
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:490
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:157
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:484
davs2_flush
static void davs2_flush(AVCodecContext *avctx)
Definition: libdavs2.c:142
davs2_decode_frame
static int davs2_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *avpkt)
Definition: libdavs2.c:188
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
DAVS2Context::param
davs2_param_t param
Definition: libdavs2.c:35
AVCodecContext::height
int height
Definition: avcodec.h:621
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:658
avcodec.h
davs2_end
static av_cold int davs2_end(AVCodecContext *avctx)
Definition: libdavs2.c:174
ret
ret
Definition: filter_design.txt:187
AVCodecContext
main external API structure.
Definition: avcodec.h:441
AVFrame::height
int height
Definition: frame.h:412
avs2.h
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
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:76
DAVS2Context::decoder
void * decoder
Definition: libdavs2.c:32
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:73
AVPacket
This structure stores compressed data.
Definition: packet.h:468
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:468
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:621
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:385
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AV_PICTURE_TYPE_S
@ AV_PICTURE_TYPE_S
S(GMC)-VOP MPEG-4.
Definition: avutil.h:282
DAVS2Context
Definition: libdavs2.c:31
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