FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/av1dec.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 150 212 70.8%
Branches: 93 158 58.9%

Line Branch Exec Source
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"
26 #include "libavcodec/avcodec.h"
27 #include "libavcodec/av1_parse.h"
28 #include "libavcodec/bsf.h"
29 #include "avformat.h"
30 #include "avio_internal.h"
31 #include "internal.h"
32
33 typedef struct AV1DemuxContext {
34 const AVClass *class;
35 AVBSFContext *bsf;
36 AVRational framerate;
37 uint32_t temporal_unit_size;
38 uint32_t frame_unit_size;
39 } AV1DemuxContext;
40
41 //return < 0 if we need more data
42 7 static int get_score(int type, int *seq)
43 {
44
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
7 switch (type) {
45 4 case AV1_OBU_SEQUENCE_HEADER:
46 4 *seq = 1;
47 4 return -1;
48 1 case AV1_OBU_FRAME:
49 case AV1_OBU_FRAME_HEADER:
50
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
51 case AV1_OBU_METADATA:
52 case AV1_OBU_PADDING:
53 return -1;
54 2 default:
55 2 break;
56 }
57 2 return 0;
58 }
59
60 1 static int av1_read_header(AVFormatContext *s)
61 {
62 1 AV1DemuxContext *const c = s->priv_data;
63 1 const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
64 AVStream *st;
65 FFStream *sti;
66 int ret;
67
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!filter) {
69 av_log(s, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
70 "not found. This is a bug, please report it.\n");
71 return AVERROR_BUG;
72 }
73
74 1 st = avformat_new_stream(s, NULL);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!st)
76 return AVERROR(ENOMEM);
77 1 sti = ffstream(st);
78
79 1 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
80 1 st->codecpar->codec_id = AV_CODEC_ID_AV1;
81 1 sti->need_parsing = AVSTREAM_PARSE_HEADERS;
82
83 1 sti->avctx->framerate = c->framerate;
84 // taken from rawvideo demuxers
85 1 avpriv_set_pts_info(st, 64, 1, 1200000);
86
87 1 ret = av_bsf_alloc(filter, &c->bsf);
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
89 return ret;
90
91 1 ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
93 return ret;
94
95 1 ret = av_bsf_init(c->bsf);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
97 return ret;
98
99 1 return 0;
100 }
101
102 1 static int av1_read_close(AVFormatContext *s)
103 {
104 1 AV1DemuxContext *const c = s->priv_data;
105
106 1 av_bsf_free(&c->bsf);
107 1 return 0;
108 }
109
110 #define DEC AV_OPT_FLAG_DECODING_PARAM
111 #define OFFSET(x) offsetof(AV1DemuxContext, x)
112 static const AVOption av1_options[] = {
113 { "framerate", "", OFFSET(framerate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, DEC},
114 { NULL },
115 };
116 #undef OFFSET
117
118 static const AVClass av1_demuxer_class = {
119 .class_name = "AV1 Annex B/low overhead OBU demuxer",
120 .item_name = av_default_item_name,
121 .option = av1_options,
122 .version = LIBAVUTIL_VERSION_INT,
123 };
124
125 #if CONFIG_AV1_DEMUXER
126
127 15344 static int leb(AVIOContext *pb, uint32_t *len) {
128 15344 int more, i = 0;
129 uint8_t byte;
130 15344 *len = 0;
131 do {
132 unsigned bits;
133 16178 byte = avio_r8(pb);
134 16178 more = byte & 0x80;
135 16178 bits = byte & 0x7f;
136
5/6
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 16135 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 29 times.
16178 if (i <= 3 || (i == 4 && bits < (1 << 4)))
137 16149 *len |= bits << (i * 7);
138
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 else if (bits)
139 29 return AVERROR_INVALIDDATA;
140
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16149 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16149 if (++i == 8 && more)
141 return AVERROR_INVALIDDATA;
142
3/4
✓ Branch 0 taken 16148 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16148 times.
16149 if (pb->eof_reached || pb->error)
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 return pb->error ? pb->error : AVERROR(EIO);
144
2/2
✓ Branch 0 taken 834 times.
✓ Branch 1 taken 15314 times.
16148 } while (more);
145 15314 return i;
146 }
147
148 1202 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
149 {
150 int start_pos, temporal_id, spatial_id;
151 int len;
152
153 1202 len = parse_obu_header(buf, size, obu_size, &start_pos,
154 type, &temporal_id, &spatial_id);
155
2/2
✓ Branch 0 taken 709 times.
✓ Branch 1 taken 493 times.
1202 if (len < 0)
156 709 return len;
157
158 493 return 0;
159 }
160
161 6780 static int annexb_probe(const AVProbeData *p)
162 {
163 FFIOContext ctx;
164 6780 AVIOContext *const pb = &ctx.pub;
165 int64_t obu_size;
166 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
167 6780 int seq = 0;
168 6780 int ret, type, cnt = 0;
169
170 6780 ffio_init_context(&ctx, p->buf, p->buf_size, 0,
171 NULL, NULL, NULL, NULL);
172
173 6780 ret = leb(pb, &temporal_unit_size);
174
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 6767 times.
6780 if (ret < 0)
175 13 return 0;
176 6767 cnt += ret;
177 6767 ret = leb(pb, &frame_unit_size);
178
4/4
✓ Branch 0 taken 6751 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 5008 times.
✓ Branch 3 taken 1743 times.
6767 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
179 5024 return 0;
180 1743 cnt += ret;
181 1743 ret = leb(pb, &obu_unit_size);
182
3/4
✓ Branch 0 taken 1743 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 546 times.
✓ Branch 3 taken 1197 times.
1743 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
183 546 return 0;
184 1197 cnt += ret;
185
186 1197 frame_unit_size -= obu_unit_size + ret;
187
188 1197 avio_skip(pb, obu_unit_size);
189
3/4
✓ Branch 0 taken 1195 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1195 times.
1197 if (pb->eof_reached || pb->error)
190 2 return 0;
191
192 // Check that the first OBU is a Temporal Delimiter.
193 1195 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
194
6/6
✓ Branch 0 taken 486 times.
✓ Branch 1 taken 709 times.
✓ Branch 2 taken 27 times.
✓ Branch 3 taken 459 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 6 times.
1195 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
195 1189 return 0;
196 6 cnt += obu_unit_size;
197
198 do {
199 10 ret = leb(pb, &obu_unit_size);
200
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
10 if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
201 return 0;
202 10 cnt += ret;
203
204 10 avio_skip(pb, obu_unit_size);
205
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
10 if (pb->eof_reached || pb->error)
206 3 return 0;
207
208 7 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (ret < 0)
210 return 0;
211 7 cnt += obu_unit_size;
212
213 7 ret = get_score(type, &seq);
214
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 if (ret >= 0)
215 3 return ret;
216
217 4 frame_unit_size -= obu_unit_size + ret;
218
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 } while (frame_unit_size);
219
220 return 0;
221 }
222
223 12 static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
224 {
225 12 AV1DemuxContext *const c = s->priv_data;
226 uint32_t obu_unit_size;
227 int ret, len;
228
229 25 retry:
230
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 23 times.
25 if (avio_feof(s->pb)) {
231
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (c->temporal_unit_size || c->frame_unit_size)
232 return AVERROR(EIO);
233 2 goto end;
234 }
235
236
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 12 times.
23 if (!c->temporal_unit_size) {
237 11 len = leb(s->pb, &c->temporal_unit_size);
238
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (len < 0) return AVERROR_INVALIDDATA;
239 }
240
241
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 if (!c->frame_unit_size) {
242 11 len = leb(s->pb, &c->frame_unit_size);
243
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
11 if (len < 0 || ((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
244 return AVERROR_INVALIDDATA;
245 11 c->temporal_unit_size -= len;
246 }
247
248 22 len = leb(s->pb, &obu_unit_size);
249
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 22 times.
22 if (len < 0 || ((int64_t)obu_unit_size + len) > c->frame_unit_size)
250 return AVERROR_INVALIDDATA;
251
252 22 ret = av_get_packet(s->pb, pkt, obu_unit_size);
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
254 return ret;
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret != obu_unit_size)
256 return AVERROR(EIO);
257
258 22 c->temporal_unit_size -= obu_unit_size + len;
259 22 c->frame_unit_size -= obu_unit_size + len;
260
261 24 end:
262 24 ret = av_bsf_send_packet(c->bsf, pkt);
263
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (ret < 0) {
264 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
265 "av1_frame_merge filter\n");
266 return ret;
267 }
268
269 24 ret = av_bsf_receive_packet(c->bsf, pkt);
270
5/6
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
24 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
271 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
272 "send output packet\n");
273
274
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 11 times.
24 if (ret == AVERROR(EAGAIN))
275 13 goto retry;
276
277 11 return ret;
278 }
279
280 const AVInputFormat ff_av1_demuxer = {
281 .name = "av1",
282 .long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
283 .priv_data_size = sizeof(AV1DemuxContext),
284 .flags_internal = FF_FMT_INIT_CLEANUP,
285 .read_probe = annexb_probe,
286 .read_header = av1_read_header,
287 .read_packet = annexb_read_packet,
288 .read_close = av1_read_close,
289 .extensions = "obu",
290 .flags = AVFMT_GENERIC_INDEX,
291 .priv_class = &av1_demuxer_class,
292 };
293 #endif
294
295 #if CONFIG_OBU_DEMUXER
296 //For low overhead obu, we can't foresee the obu size before we parsed the header.
297 //So, we can't use parse_obu_header here, since it will check size <= buf_size
298 //see c27c7b49dc for more details
299 6784 static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
300 {
301 GetBitContext gb;
302 int ret, extension_flag, start_pos;
303 int64_t size;
304
305 6784 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6784 times.
6784 if (ret < 0)
307 return ret;
308
309
2/2
✓ Branch 1 taken 208 times.
✓ Branch 2 taken 6576 times.
6784 if (get_bits1(&gb) != 0) // obu_forbidden_bit
310 208 return AVERROR_INVALIDDATA;
311
312 6576 *type = get_bits(&gb, 4);
313 6576 extension_flag = get_bits1(&gb);
314
2/2
✓ Branch 1 taken 4501 times.
✓ Branch 2 taken 2075 times.
6576 if (!get_bits1(&gb)) // has_size_flag
315 4501 return AVERROR_INVALIDDATA;
316 2075 skip_bits1(&gb); // obu_reserved_1bit
317
318
2/2
✓ Branch 0 taken 523 times.
✓ Branch 1 taken 1552 times.
2075 if (extension_flag) {
319 523 get_bits(&gb, 3); // temporal_id
320 523 get_bits(&gb, 2); // spatial_id
321 523 skip_bits(&gb, 3); // extension_header_reserved_3bits
322 }
323
324 2075 *obu_size = leb128(&gb);
325
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2073 times.
2075 if (*obu_size > INT_MAX)
326 2 return AVERROR_INVALIDDATA;
327
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2073 times.
2073 if (get_bits_left(&gb) < 0)
329 return AVERROR_INVALIDDATA;
330
331 2073 start_pos = get_bits_count(&gb) / 8;
332
333 2073 size = *obu_size + start_pos;
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2073 times.
2073 if (size > INT_MAX)
335 return AVERROR_INVALIDDATA;
336 2073 return size;
337 }
338
339 6780 static int obu_probe(const AVProbeData *p)
340 {
341 int64_t obu_size;
342 6780 int seq = 0;
343 int ret, type, cnt;
344
345 // Check that the first OBU is a Temporal Delimiter.
346 6780 cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
347
6/6
✓ Branch 0 taken 2073 times.
✓ Branch 1 taken 4707 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 2056 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 4 times.
6780 if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
348 6776 return 0;
349
350 while (1) {
351 4 ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
352
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (ret < 0 || obu_size <= 0)
353 4 return 0;
354 cnt += FFMIN(ret, p->buf_size - cnt);
355
356 ret = get_score(type, &seq);
357 if (ret >= 0)
358 return ret;
359 }
360 return 0;
361 }
362
363 static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
364 {
365 AV1DemuxContext *const c = s->priv_data;
366 uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
367 int64_t obu_size;
368 int size;
369 int ret, len, type;
370
371 if ((ret = ffio_ensure_seekback(s->pb, MAX_OBU_HEADER_SIZE)) < 0)
372 return ret;
373 size = avio_read(s->pb, header, MAX_OBU_HEADER_SIZE);
374 if (size < 0)
375 return size;
376
377 len = read_obu_with_size(header, size, &obu_size, &type);
378 if (len < 0) {
379 av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
380 return len;
381 }
382 avio_seek(s->pb, -size, SEEK_CUR);
383
384 ret = av_get_packet(s->pb, pkt, len);
385 if (ret != len) {
386 av_log(c, AV_LOG_ERROR, "Failed to get packet for obu\n");
387 return ret < 0 ? ret : AVERROR_INVALIDDATA;
388 }
389 return 0;
390 }
391
392 static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
393 {
394 AV1DemuxContext *const c = s->priv_data;
395 int ret;
396
397 if (s->io_repositioned) {
398 av_bsf_flush(c->bsf);
399 s->io_repositioned = 0;
400 }
401 while (1) {
402 ret = obu_get_packet(s, pkt);
403 /* In case of AVERROR_EOF we need to flush the BSF. Conveniently
404 * obu_get_packet() returns a blank pkt in this case which
405 * can be used to signal that the BSF should be flushed. */
406 if (ret < 0 && ret != AVERROR_EOF)
407 return ret;
408 ret = av_bsf_send_packet(c->bsf, pkt);
409 if (ret < 0) {
410 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
411 "av1_frame_merge filter\n");
412 return ret;
413 }
414 ret = av_bsf_receive_packet(c->bsf, pkt);
415 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
416 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
417 "send output packet\n");
418 if (ret != AVERROR(EAGAIN))
419 break;
420 }
421
422 return ret;
423 }
424
425 const AVInputFormat ff_obu_demuxer = {
426 .name = "obu",
427 .long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
428 .priv_data_size = sizeof(AV1DemuxContext),
429 .flags_internal = FF_FMT_INIT_CLEANUP,
430 .read_probe = obu_probe,
431 .read_header = av1_read_header,
432 .read_packet = obu_read_packet,
433 .read_close = av1_read_close,
434 .extensions = "obu",
435 .flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK,
436 .priv_class = &av1_demuxer_class,
437 };
438 #endif
439