FFmpeg
av1dec.c
Go to the documentation of this file.
1 /*
2  * AV1 Annex B demuxer
3  * Copyright (c) 2019 James Almer <jamrial@gmail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include "config.h"
23 
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavcodec/av1_parse.h"
27 #include "avformat.h"
28 #include "avio_internal.h"
29 #include "internal.h"
30 
31 typedef struct AnnexBContext {
32  const AVClass *class;
35  uint32_t frame_unit_size;
38 
39 static int leb(AVIOContext *pb, uint32_t *len) {
40  int more, i = 0;
41  uint8_t byte;
42  *len = 0;
43  do {
44  unsigned bits;
45  byte = avio_r8(pb);
46  more = byte & 0x80;
47  bits = byte & 0x7f;
48  if (i <= 3 || (i == 4 && bits < (1 << 4)))
49  *len |= bits << (i * 7);
50  else if (bits)
51  return AVERROR_INVALIDDATA;
52  if (++i == 8 && more)
53  return AVERROR_INVALIDDATA;
54  if (pb->eof_reached || pb->error)
55  return pb->error ? pb->error : AVERROR(EIO);
56  } while (more);
57  return i;
58 }
59 
60 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
61 {
62  int start_pos, temporal_id, spatial_id;
63  int len;
64 
65  len = parse_obu_header(buf, size, obu_size, &start_pos,
66  type, &temporal_id, &spatial_id);
67  if (len < 0)
68  return len;
69 
70  return 0;
71 }
72 
73 static int annexb_probe(const AVProbeData *p)
74 {
75  AVIOContext pb;
76  int64_t obu_size;
77  uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
78  int seq = 0;
79  int ret, type, cnt = 0;
80 
81  ffio_init_context(&pb, p->buf, p->buf_size, 0,
82  NULL, NULL, NULL, NULL);
83 
84  ret = leb(&pb, &temporal_unit_size);
85  if (ret < 0)
86  return 0;
87  cnt += ret;
88  ret = leb(&pb, &frame_unit_size);
89  if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
90  return 0;
91  cnt += ret;
92  temporal_unit_size -= ret;
93  ret = leb(&pb, &obu_unit_size);
94  if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
95  return 0;
96  cnt += ret;
97 
98  temporal_unit_size -= obu_unit_size + ret;
99  frame_unit_size -= obu_unit_size + ret;
100 
101  avio_skip(&pb, obu_unit_size);
102  if (pb.eof_reached || pb.error)
103  return 0;
104 
105  // Check that the first OBU is a Temporal Delimiter.
106  ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
107  if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
108  return 0;
109  cnt += obu_unit_size;
110 
111  do {
112  ret = leb(&pb, &obu_unit_size);
113  if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
114  return 0;
115  cnt += ret;
116 
117  avio_skip(&pb, obu_unit_size);
118  if (pb.eof_reached || pb.error)
119  return 0;
120 
121  ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
122  if (ret < 0)
123  return 0;
124  cnt += obu_unit_size;
125 
126  switch (type) {
128  seq = 1;
129  break;
130  case AV1_OBU_FRAME:
132  return seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
133  case AV1_OBU_TILE_GROUP:
135  return 0;
136  default:
137  break;
138  }
139 
140  temporal_unit_size -= obu_unit_size + ret;
141  frame_unit_size -= obu_unit_size + ret;
142  } while (frame_unit_size);
143 
144  return 0;
145 }
146 
148 {
149  AnnexBContext *c = s->priv_data;
150  const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
151  AVStream *st;
152  int ret;
153 
154  if (!filter) {
155  av_log(c, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
156  "not found. This is a bug, please report it.\n");
157  return AVERROR_BUG;
158  }
159 
160  st = avformat_new_stream(s, NULL);
161  if (!st)
162  return AVERROR(ENOMEM);
163 
167 
168  st->internal->avctx->framerate = c->framerate;
169  // taken from rawvideo demuxers
170  avpriv_set_pts_info(st, 64, 1, 1200000);
171 
172  ret = av_bsf_alloc(filter, &c->bsf);
173  if (ret < 0)
174  return ret;
175 
176  ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
177  if (ret < 0) {
178  av_bsf_free(&c->bsf);
179  return ret;
180  }
181 
182  ret = av_bsf_init(c->bsf);
183  if (ret < 0)
184  av_bsf_free(&c->bsf);
185 
186  return ret;
187 }
188 
190 {
191  AnnexBContext *c = s->priv_data;
192  uint32_t obu_unit_size;
193  int ret, len;
194 
195 retry:
196  if (avio_feof(s->pb)) {
197  if (c->temporal_unit_size || c->frame_unit_size)
198  return AVERROR(EIO);
199  goto end;
200  }
201 
202  if (!c->temporal_unit_size) {
203  len = leb(s->pb, &c->temporal_unit_size);
204  if (len < 0) return AVERROR_INVALIDDATA;
205  }
206 
207  if (!c->frame_unit_size) {
208  len = leb(s->pb, &c->frame_unit_size);
209  if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
210  return AVERROR_INVALIDDATA;
211  c->temporal_unit_size -= len;
212  }
213 
214  len = leb(s->pb, &obu_unit_size);
215  if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size)
216  return AVERROR_INVALIDDATA;
217 
218  ret = av_get_packet(s->pb, pkt, obu_unit_size);
219  if (ret < 0)
220  return ret;
221  if (ret != obu_unit_size)
222  return AVERROR(EIO);
223 
224  c->temporal_unit_size -= obu_unit_size + len;
225  c->frame_unit_size -= obu_unit_size + len;
226 
227 end:
228  ret = av_bsf_send_packet(c->bsf, pkt);
229  if (ret < 0) {
230  av_log(s, AV_LOG_ERROR, "Failed to send packet to "
231  "av1_frame_merge filter\n");
232  return ret;
233  }
234 
235  ret = av_bsf_receive_packet(c->bsf, pkt);
236  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
237  av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
238  "send output packet\n");
239 
240  if (ret == AVERROR(EAGAIN))
241  goto retry;
242 
243  return ret;
244 }
245 
247 {
248  AnnexBContext *c = s->priv_data;
249 
250  av_bsf_free(&c->bsf);
251  return 0;
252 }
253 
254 #define OFFSET(x) offsetof(AnnexBContext, x)
255 #define DEC AV_OPT_FLAG_DECODING_PARAM
256 static const AVOption annexb_options[] = {
257  { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
258  { NULL },
259 };
260 
262  .class_name = "AV1 Annex B demuxer",
263  .item_name = av_default_item_name,
264  .option = annexb_options,
265  .version = LIBAVUTIL_VERSION_INT,
266 };
267 
269  .name = "av1",
270  .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
271  .priv_data_size = sizeof(AnnexBContext),
276  .extensions = "obu",
278  .priv_class = &annexb_demuxer_class,
279 };
annexb_read_header
static int annexb_read_header(AVFormatContext *s)
Definition: av1dec.c:147
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
opt.h
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
AnnexBContext::framerate
AVRational framerate
Definition: av1dec.c:36
AVStreamInternal::avctx
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:169
OFFSET
#define OFFSET(x)
Definition: av1dec.c:254
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
annexb_probe
static int annexb_probe(const AVProbeData *p)
Definition: av1dec.c:73
AV_OPT_TYPE_VIDEO_RATE
@ AV_OPT_TYPE_VIDEO_RATE
offset must point to AVRational
Definition: opt.h:236
av_bsf_init
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:144
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
AVStream::internal
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1220
AVOption
AVOption.
Definition: opt.h:246
AVIOContext::error
int error
contains the error code or 0 if no error happened
Definition: avio.h:245
filter
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
Definition: filter_design.txt:228
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
annexb_options
static const AVOption annexb_options[]
Definition: av1dec.c:256
AV1_OBU_TEMPORAL_DELIMITER
@ AV1_OBU_TEMPORAL_DELIMITER
Definition: av1.h:31
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
av_bsf_get_by_name
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
Definition: bitstream_filters.c:83
ff_av1_demuxer
AVInputFormat ff_av1_demuxer
Definition: av1dec.c:268
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:2069
framerate
int framerate
Definition: h264_levels.c:65
av1_parse.h
AV1_OBU_FRAME_HEADER
@ AV1_OBU_FRAME_HEADER
Definition: av1.h:32
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
annexb_read_packet
static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: av1dec.c:189
AVInputFormat
Definition: avformat.h:636
parse_obu_header
static int parse_obu_header(const uint8_t *buf, int buf_size, int64_t *obu_size, int *start_pos, int *type, int *temporal_id, int *spatial_id)
Definition: av1_parse.h:100
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:641
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
bits
uint8_t bits
Definition: vp3data.h:202
AVStream::need_parsing
enum AVStreamParseType need_parsing
Definition: avformat.h:1083
AVFormatContext
Format I/O context.
Definition: avformat.h:1335
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1012
AnnexBContext
Definition: av1dec.c:31
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:274
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
leb
static int leb(AVIOContext *pb, uint32_t *len)
Definition: av1dec.c:39
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
AnnexBContext::frame_unit_size
uint32_t frame_unit_size
Definition: av1dec.c:35
AV1_OBU_TILE_GROUP
@ AV1_OBU_TILE_GROUP
Definition: av1.h:33
annexb_read_close
static int annexb_read_close(AVFormatContext *s)
Definition: av1dec.c:246
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
AnnexBContext::bsf
AVBSFContext * bsf
Definition: av1dec.c:33
AV1_OBU_FRAME
@ AV1_OBU_FRAME
Definition: av1.h:35
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
ffio_init_context
int ffio_init_context(AVIOContext *s, unsigned char *buffer, int buffer_size, int write_flag, void *opaque, int(*read_packet)(void *opaque, uint8_t *buf, int buf_size), int(*write_packet)(void *opaque, uint8_t *buf, int buf_size), int64_t(*seek)(void *opaque, int64_t offset, int whence))
Definition: aviobuf.c:76
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:188
byte
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:95
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4948
size
int size
Definition: twinvq_data.h:11134
AV1_OBU_SEQUENCE_HEADER
@ AV1_OBU_SEQUENCE_HEADER
Definition: av1.h:30
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:616
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
av_bsf_receive_packet
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:223
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
avio_internal.h
common.h
uint8_t
uint8_t
Definition: audio_convert.c:194
annexb_demuxer_class
static const AVClass annexb_demuxer_class
Definition: av1dec.c:261
len
int len
Definition: vorbis_enc_data.h:452
read_obu
static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
Definition: av1dec.c:60
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:307
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:865
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:787
avformat.h
av_bsf_send_packet
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:197
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: utils.c:2109
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
AVBitStreamFilter
Definition: bsf.h:98
AVIOContext::eof_reached
int eof_reached
true if was unable to read due to error or eof
Definition: avio.h:239
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
DEC
#define DEC
Definition: av1dec.c:255
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_bsf_free
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:40
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
AnnexBContext::temporal_unit_size
uint32_t temporal_unit_size
Definition: av1dec.c:34
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
av_bsf_alloc
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:91
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:356