FFmpeg
Loading...
Searching...
No Matches
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_components.h"
23
24#include "libavutil/common.h"
25#include "libavutil/opt.h"
27#include "libavcodec/bsf.h"
28#include "avformat.h"
29#include "avio_internal.h"
30#include "demux.h"
31#include "internal.h"
32
41
42//return < 0 if we need more data
43static int get_score(int type, int *seq)
44{
45 switch (type) {
47 *seq = 1;
48 return -1;
49 case AV1_OBU_FRAME:
51 return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
53 case AV1_OBU_PADDING:
54 return -1;
55 default:
56 break;
57 }
58 return 0;
59}
60
62{
63 AV1DemuxContext *const c = s->priv_data;
64 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
65 AVStream *st;
66 FFStream *sti;
67 int ret;
68
69 if (!filter) {
70 av_log(s, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
71 "not found. This is a bug, please report it.\n");
72 return AVERROR_BUG;
73 }
74
76 if (!st)
77 return AVERROR(ENOMEM);
78 sti = ffstream(st);
79
83
84 st->avg_frame_rate = c->framerate;
85 // taken from rawvideo demuxers
86 avpriv_set_pts_info(st, 64, 1, 1200000);
87
88 ret = av_bsf_alloc(filter, &c->bsf);
89 if (ret < 0)
90 return ret;
91
92 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
93 if (ret < 0)
94 return ret;
95
96 ret = av_bsf_init(c->bsf);
97 if (ret < 0)
98 return ret;
99
100 return 0;
101}
102
104{
105 AV1DemuxContext *const c = s->priv_data;
106
107 av_bsf_free(&c->bsf);
108 return 0;
109}
110
111#define DEC AV_OPT_FLAG_DECODING_PARAM
112#define OFFSET(x) offsetof(AV1DemuxContext, x)
113static const AVOption av1_options[] = {
114 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
115 { NULL },
116};
117#undef OFFSET
118
120 .class_name = "AV1 Annex B/low overhead OBU demuxer",
121 .item_name = av_default_item_name,
122 .option = av1_options,
123 .version = LIBAVUTIL_VERSION_INT,
124};
125
126#if CONFIG_AV1_DEMUXER
127
128static int leb(AVIOContext *pb, uint32_t *len, int eof) {
129 int more, i = 0;
130 *len = 0;
131 do {
132 unsigned bits;
133 int byte = avio_r8(pb);
134 if (pb->error)
135 return pb->error;
136 if (pb->eof_reached)
137 return (eof && !i) ? AVERROR_EOF : AVERROR_INVALIDDATA;
138 more = byte & 0x80;
139 bits = byte & 0x7f;
140 if (i <= 3 || (i == 4 && bits < (1 << 4)))
141 *len |= bits << (i * 7);
142 else if (bits)
143 return AVERROR_INVALIDDATA;
144 if (++i == 8 && more)
145 return AVERROR_INVALIDDATA;
146 } while (more);
147 return i;
148}
149
150static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
151{
152 int start_pos, temporal_id, spatial_id;
153 int len;
154
155 len = parse_obu_header(buf, size, obu_size, &start_pos,
156 type, &temporal_id, &spatial_id);
157 if (len < 0)
158 return len;
159
160 return 0;
161}
162
163static int annexb_probe(const AVProbeData *p)
164{
166 AVIOContext *const pb = &ctx.pub;
167 int64_t obu_size;
168 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
169 int seq = 0;
170 int ret, type, cnt = 0;
171
172 ffio_init_read_context(&ctx, p->buf, p->buf_size);
173
174 ret = leb(pb, &temporal_unit_size, 1);
175 if (ret < 0)
176 return 0;
177 cnt += ret;
178 ret = leb(pb, &frame_unit_size, 0);
179 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
180 return 0;
181 cnt += ret;
182 ret = leb(pb, &obu_unit_size, 0);
183 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
184 return 0;
185 cnt += ret;
186
187 frame_unit_size -= obu_unit_size + ret;
188
189 avio_skip(pb, obu_unit_size);
190 if (pb->eof_reached || pb->error)
191 return 0;
192
193 // Check that the first OBU is a Temporal Delimiter.
194 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
195 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
196 return 0;
197 cnt += obu_unit_size;
198
199 do {
200 ret = leb(pb, &obu_unit_size, 0);
201 if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
202 return 0;
203 cnt += ret;
204
205 avio_skip(pb, obu_unit_size);
206 if (pb->eof_reached || pb->error)
207 return 0;
208
209 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
210 if (ret < 0)
211 return 0;
212 cnt += obu_unit_size;
213
214 ret = get_score(type, &seq);
215 if (ret >= 0)
216 return ret;
217
218 frame_unit_size -= obu_unit_size + ret;
219 } while (frame_unit_size);
220
221 return 0;
222}
223
224static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
225{
226 AV1DemuxContext *const c = s->priv_data;
227 uint32_t obu_unit_size;
228 int64_t pos = c->pos;
229 int ret, len;
230
231retry:
232 if (avio_feof(s->pb)) {
233 if (c->temporal_unit_size || c->frame_unit_size)
234 return AVERROR_INVALIDDATA;
235 goto end;
236 }
237
238 if (!c->temporal_unit_size) {
239 c->pos = avio_tell(s->pb);
240 len = leb(s->pb, &c->temporal_unit_size, 1);
241 if (len == AVERROR_EOF) goto end;
242 else if (len < 0) return len;
243 }
244
245 if (!c->frame_unit_size) {
246 len = leb(s->pb, &c->frame_unit_size, 0);
247 if (len < 0)
248 return len;
249 if (((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
250 return AVERROR_INVALIDDATA;
251 c->temporal_unit_size -= len;
252 }
253
254 len = leb(s->pb, &obu_unit_size, 0);
255 if (len < 0)
256 return len;
257 if (((int64_t)obu_unit_size + len) > c->frame_unit_size)
258 return AVERROR_INVALIDDATA;
259
260 ret = av_get_packet(s->pb, pkt, obu_unit_size);
261 if (ret < 0)
262 return ret;
263 if (ret != obu_unit_size)
264 return AVERROR_INVALIDDATA;
265
266 c->temporal_unit_size -= obu_unit_size + len;
267 c->frame_unit_size -= obu_unit_size + len;
268
269end:
270 ret = av_bsf_send_packet(c->bsf, pkt);
271 if (ret < 0) {
272 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
273 "av1_frame_merge filter\n");
274 return ret;
275 }
276
277 ret = av_bsf_receive_packet(c->bsf, pkt);
278 if (ret < 0) {
279 if (ret == AVERROR(EAGAIN))
280 goto retry;
281 if (ret != AVERROR_EOF)
282 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
283 "send output packet\n");
284 return ret;
285 }
286
287 pkt->pos = pos;
288
289 return 0;
290}
291
293 .p.name = "av1",
294 .p.long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
295 .p.extensions = "obu",
297 .p.priv_class = &av1_demuxer_class,
298 .priv_data_size = sizeof(AV1DemuxContext),
299 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
302 .read_packet = annexb_read_packet,
304};
305#endif
306
307#if CONFIG_OBU_DEMUXER
308//For low overhead obu, we can't foresee the obu size before we parsed the header.
309//So, we can't use parse_obu_header here, since it will check size <= buf_size
310//see c27c7b49dc for more details
311static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
312{
313 GetBitContext gb;
314 int ret, extension_flag, start_pos;
316
317 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
318 if (ret < 0)
319 return ret;
320
321 if (get_bits1(&gb) != 0) // obu_forbidden_bit
322 return AVERROR_INVALIDDATA;
323
324 *type = get_bits(&gb, 4);
325 extension_flag = get_bits1(&gb);
326 if (!get_bits1(&gb)) // has_size_flag
327 return AVERROR_INVALIDDATA;
328 skip_bits1(&gb); // obu_reserved_1bit
329
330 if (extension_flag) {
331 get_bits(&gb, 3); // temporal_id
332 get_bits(&gb, 2); // spatial_id
333 skip_bits(&gb, 3); // extension_header_reserved_3bits
334 }
335
336 *obu_size = get_leb128(&gb);
337 if (*obu_size > INT_MAX)
338 return AVERROR_INVALIDDATA;
339
340 if (get_bits_left(&gb) < 0)
341 return AVERROR_INVALIDDATA;
342
343 start_pos = get_bits_count(&gb) / 8;
344
345 size = *obu_size + start_pos;
346 if (size > INT_MAX)
347 return AVERROR_INVALIDDATA;
348 return size;
349}
350
351static int obu_probe(const AVProbeData *p)
352{
353 int64_t obu_size;
354 int seq = 0;
355 int ret, type, cnt;
356
357 // Check that the first OBU is a Temporal Delimiter.
358 cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
359 if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
360 return 0;
361
362 while (1) {
363 ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
364 if (ret < 0 || obu_size <= 0)
365 return 0;
366 cnt += FFMIN(ret, p->buf_size - cnt);
367
368 ret = get_score(type, &seq);
369 if (ret >= 0)
370 return ret;
371 }
372 return 0;
373}
374
375static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
376{
377 AV1DemuxContext *const c = s->priv_data;
379 int64_t obu_size;
380 int size;
381 int ret, len, type;
382
383 if ((ret = ffio_ensure_seekback(s->pb, MAX_OBU_HEADER_SIZE)) < 0)
384 return ret;
386 if (size < 0)
387 return size;
388
390 len = read_obu_with_size(header, size, &obu_size, &type);
391 if (len < 0) {
392 av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
393 return len;
394 }
395 avio_seek(s->pb, -size, SEEK_CUR);
396
397 ret = av_get_packet(s->pb, pkt, len);
398 if (ret != len) {
399 av_log(c, AV_LOG_ERROR, "Failed to get packet for obu\n");
400 return ret < 0 ? ret : AVERROR_INVALIDDATA;
401 }
402 return 0;
403}
404
405static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
406{
407 AV1DemuxContext *const c = s->priv_data;
408 int ret;
409
410 if (s->io_repositioned) {
411 av_bsf_flush(c->bsf);
412 s->io_repositioned = 0;
413 }
414 while (1) {
415 ret = obu_get_packet(s, pkt);
416 /* In case of AVERROR_EOF we need to flush the BSF. Conveniently
417 * obu_get_packet() returns a blank pkt in this case which
418 * can be used to signal that the BSF should be flushed. */
419 if (ret < 0 && ret != AVERROR_EOF)
420 return ret;
421 ret = av_bsf_send_packet(c->bsf, pkt);
422 if (ret < 0) {
423 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
424 "av1_frame_merge filter\n");
425 return ret;
426 }
427 ret = av_bsf_receive_packet(c->bsf, pkt);
428 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
429 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
430 "send output packet\n");
431 if (ret != AVERROR(EAGAIN))
432 break;
433 }
434
435 return ret;
436}
437
439 .p.name = "obu",
440 .p.long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
441 .p.extensions = "obu",
443 .p.priv_class = &av1_demuxer_class,
444 .priv_data_size = sizeof(AV1DemuxContext),
445 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
446 .read_probe = obu_probe,
448 .read_packet = obu_read_packet,
450};
451#endif
const FFInputFormat ff_av1_demuxer
const FFInputFormat ff_obu_demuxer
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:92
#define MAX_OBU_HEADER_SIZE
Definition av1_parse.h:36
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition avformat.h:506
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition avformat.h:498
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition avformat.h:612
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_ensure_seekback(AVIOContext *s, int64_t buf_size)
Ensures that the requested seekback buffer size will be available.
Definition aviobuf.c:1026
void ffio_init_read_context(FFIOContext *s, const uint8_t *buffer, int buffer_size)
Wrap a buffer in an AVIOContext for reading.
Definition aviobuf.c:99
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Definition codec_par.c:107
common internal and external API header
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int annexb_probe(const AVProbeData *p)
Definition evcdec.c:60
static const uint8_t bits[8]
Definition fastaudio.c:100
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static void skip_bits(GetBitContext *s, int n)
Definition get_bits.h:383
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static int get_bits_count(const GetBitContext *s)
Definition get_bits.h:254
static void skip_bits1(GetBitContext *s)
Definition get_bits.h:416
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
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:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
void av_bsf_flush(AVBSFContext *ctx)
Reset the internal bitstream filter state.
Definition bsf.c:188
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition bsf.c:99
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
cl_device_type type
static int64_t get_leb128(GetBitContext *gb)
Read a unsigned integer coded as a variable number of up to eight little-endian bytes,...
Definition leb.h:57
@ AV1_OBU_TEMPORAL_DELIMITER
Definition av1.h:31
@ AV1_OBU_METADATA
Definition av1.h:34
@ AV1_OBU_FRAME_HEADER
Definition av1.h:32
@ AV1_OBU_PADDING
Definition av1.h:39
@ AV1_OBU_FRAME
Definition av1.h:35
@ AV1_OBU_SEQUENCE_HEADER
Definition av1.h:30
static const AVOption av1_options[]
Definition av1dec.c:1533
#define OFFSET(x)
Definition av1dec.c:1531
static int get_score(int type, int *seq)
Definition av1dec.c:43
static const AVClass av1_demuxer_class
Definition av1dec.c:119
static int av1_read_header(AVFormatContext *s)
Definition av1dec.c:61
static int av1_read_close(AVFormatContext *s)
Definition av1dec.c:103
static av_always_inline FFStream * ffstream(AVStream *st)
Definition internal.h:365
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
#define DEC
Definition librsvgdec.c:149
#define FFMIN(a, b)
Definition macros.h:49
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
AVOptions.
static const uint8_t header[24]
Definition sdr2.c:68
unsigned int pos
Definition spdifenc.c:431
uint32_t frame_unit_size
Definition av1dec.c:38
AVBSFContext * bsf
Definition av1dec.c:35
AVRational framerate
Definition av1dec.c:36
int64_t pos
Definition av1dec.c:39
uint32_t temporal_unit_size
Definition av1dec.c:37
The bitstream filter state.
Definition bsf.h:68
Describe the class of an AVClass context structure.
Definition log.h:76
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
int eof_reached
true if was unable to read due to error or eof
Definition avio.h:238
int error
contains the error code or 0 if no error happened
Definition avio.h:239
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
enum AVStreamParseType need_parsing
Definition internal.h:321
#define av_log(a,...)
float framerate
Definition av1_levels.c:29
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
static AVFormatContext * ctx
Definition movenc.c:49
int size
int len
static double c[64]