FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/av1dec.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 154 220 70.0%
Functions: 9 11 81.8%
Branches: 95 162 58.6%

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/av1_parse.h"
27 #include "libavcodec/bsf.h"
28 #include "avformat.h"
29 #include "avio_internal.h"
30 #include "demux.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 st->avg_frame_rate = 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 16229 static int leb(AVIOContext *pb, uint32_t *len, int eof) {
128 16229 int more, i = 0;
129 16229 *len = 0;
130 do {
131 unsigned bits;
132 17175 int byte = avio_r8(pb);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17175 times.
17175 if (pb->error)
134 return pb->error;
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17174 times.
17175 if (pb->eof_reached)
136
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 return (eof && !i) ? AVERROR_EOF : AVERROR_INVALIDDATA;
137 17174 more = byte & 0x80;
138 17174 bits = byte & 0x7f;
139
5/6
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 17122 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 30 times.
17174 if (i <= 3 || (i == 4 && bits < (1 << 4)))
140 17144 *len |= bits << (i * 7);
141
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 else if (bits)
142 30 return AVERROR_INVALIDDATA;
143
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 17144 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17144 if (++i == 8 && more)
144 return AVERROR_INVALIDDATA;
145
2/2
✓ Branch 0 taken 946 times.
✓ Branch 1 taken 16198 times.
17144 } while (more);
146 16198 return i;
147 }
148
149 1258 static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
150 {
151 int start_pos, temporal_id, spatial_id;
152 int len;
153
154 1258 len = parse_obu_header(buf, size, obu_size, &start_pos,
155 type, &temporal_id, &spatial_id);
156
2/2
✓ Branch 0 taken 735 times.
✓ Branch 1 taken 523 times.
1258 if (len < 0)
157 735 return len;
158
159 523 return 0;
160 }
161
162 7186 static int annexb_probe(const AVProbeData *p)
163 {
164 FFIOContext ctx;
165 7186 AVIOContext *const pb = &ctx.pub;
166 int64_t obu_size;
167 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
168 7186 int seq = 0;
169 7186 int ret, type, cnt = 0;
170
171 7186 ffio_init_read_context(&ctx, p->buf, p->buf_size);
172
173 7186 ret = leb(pb, &temporal_unit_size, 1);
174
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7173 times.
7186 if (ret < 0)
175 13 return 0;
176 7173 cnt += ret;
177 7173 ret = leb(pb, &frame_unit_size, 0);
178
4/4
✓ Branch 0 taken 7156 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 5340 times.
✓ Branch 3 taken 1816 times.
7173 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
179 5357 return 0;
180 1816 cnt += ret;
181 1816 ret = leb(pb, &obu_unit_size, 0);
182
3/4
✓ Branch 0 taken 1816 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 563 times.
✓ Branch 3 taken 1253 times.
1816 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
183 563 return 0;
184 1253 cnt += ret;
185
186 1253 frame_unit_size -= obu_unit_size + ret;
187
188 1253 avio_skip(pb, obu_unit_size);
189
3/4
✓ Branch 0 taken 1251 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1251 times.
1253 if (pb->eof_reached || pb->error)
190 2 return 0;
191
192 // Check that the first OBU is a Temporal Delimiter.
193 1251 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
194
6/6
✓ Branch 0 taken 516 times.
✓ Branch 1 taken 735 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 487 times.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 6 times.
1251 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
195 1245 return 0;
196 6 cnt += obu_unit_size;
197
198 do {
199 10 ret = leb(pb, &obu_unit_size, 0);
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_INVALIDDATA;
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, 1);
238
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (len == AVERROR_EOF) goto end;
239
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 else if (len < 0) return len;
240 }
241
242
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 if (!c->frame_unit_size) {
243 11 len = leb(s->pb, &c->frame_unit_size, 0);
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (len < 0)
245 return len;
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
247 return AVERROR_INVALIDDATA;
248 11 c->temporal_unit_size -= len;
249 }
250
251 22 len = leb(s->pb, &obu_unit_size, 0);
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (len < 0)
253 return len;
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (((int64_t)obu_unit_size + len) > c->frame_unit_size)
255 return AVERROR_INVALIDDATA;
256
257 22 ret = av_get_packet(s->pb, pkt, obu_unit_size);
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret < 0)
259 return ret;
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (ret != obu_unit_size)
261 return AVERROR_INVALIDDATA;
262
263 22 c->temporal_unit_size -= obu_unit_size + len;
264 22 c->frame_unit_size -= obu_unit_size + len;
265
266 25 end:
267 25 ret = av_bsf_send_packet(c->bsf, pkt);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (ret < 0) {
269 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
270 "av1_frame_merge filter\n");
271 return ret;
272 }
273
274 25 ret = av_bsf_receive_packet(c->bsf, pkt);
275
5/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
25 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
276 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
277 "send output packet\n");
278
279
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 12 times.
25 if (ret == AVERROR(EAGAIN))
280 13 goto retry;
281
282 12 return ret;
283 }
284
285 const FFInputFormat ff_av1_demuxer = {
286 .p.name = "av1",
287 .p.long_name = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
288 .p.extensions = "obu",
289 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
290 .p.priv_class = &av1_demuxer_class,
291 .priv_data_size = sizeof(AV1DemuxContext),
292 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
293 .read_probe = annexb_probe,
294 .read_header = av1_read_header,
295 .read_packet = annexb_read_packet,
296 .read_close = av1_read_close,
297 };
298 #endif
299
300 #if CONFIG_OBU_DEMUXER
301 //For low overhead obu, we can't foresee the obu size before we parsed the header.
302 //So, we can't use parse_obu_header here, since it will check size <= buf_size
303 //see c27c7b49dc for more details
304 7190 static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
305 {
306 GetBitContext gb;
307 int ret, extension_flag, start_pos;
308 int64_t size;
309
310 7190 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7190 times.
7190 if (ret < 0)
312 return ret;
313
314
2/2
✓ Branch 1 taken 250 times.
✓ Branch 2 taken 6940 times.
7190 if (get_bits1(&gb) != 0) // obu_forbidden_bit
315 250 return AVERROR_INVALIDDATA;
316
317 6940 *type = get_bits(&gb, 4);
318 6940 extension_flag = get_bits1(&gb);
319
2/2
✓ Branch 1 taken 4781 times.
✓ Branch 2 taken 2159 times.
6940 if (!get_bits1(&gb)) // has_size_flag
320 4781 return AVERROR_INVALIDDATA;
321 2159 skip_bits1(&gb); // obu_reserved_1bit
322
323
2/2
✓ Branch 0 taken 552 times.
✓ Branch 1 taken 1607 times.
2159 if (extension_flag) {
324 552 get_bits(&gb, 3); // temporal_id
325 552 get_bits(&gb, 2); // spatial_id
326 552 skip_bits(&gb, 3); // extension_header_reserved_3bits
327 }
328
329 2159 *obu_size = get_leb128(&gb);
330
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2156 times.
2159 if (*obu_size > INT_MAX)
331 3 return AVERROR_INVALIDDATA;
332
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2156 times.
2156 if (get_bits_left(&gb) < 0)
334 return AVERROR_INVALIDDATA;
335
336 2156 start_pos = get_bits_count(&gb) / 8;
337
338 2156 size = *obu_size + start_pos;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2156 times.
2156 if (size > INT_MAX)
340 return AVERROR_INVALIDDATA;
341 2156 return size;
342 }
343
344 7186 static int obu_probe(const AVProbeData *p)
345 {
346 int64_t obu_size;
347 7186 int seq = 0;
348 int ret, type, cnt;
349
350 // Check that the first OBU is a Temporal Delimiter.
351 7186 cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
352
6/6
✓ Branch 0 taken 2156 times.
✓ Branch 1 taken 5030 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 2140 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 12 times.
7186 if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
353 7182 return 0;
354
355 while (1) {
356 4 ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
357
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)
358 4 return 0;
359 cnt += FFMIN(ret, p->buf_size - cnt);
360
361 ret = get_score(type, &seq);
362 if (ret >= 0)
363 return ret;
364 }
365 return 0;
366 }
367
368 static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
369 {
370 AV1DemuxContext *const c = s->priv_data;
371 uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
372 int64_t obu_size;
373 int size;
374 int ret, len, type;
375
376 if ((ret = ffio_ensure_seekback(s->pb, MAX_OBU_HEADER_SIZE)) < 0)
377 return ret;
378 size = avio_read(s->pb, header, MAX_OBU_HEADER_SIZE);
379 if (size < 0)
380 return size;
381
382 memset(header + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
383 len = read_obu_with_size(header, size, &obu_size, &type);
384 if (len < 0) {
385 av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
386 return len;
387 }
388 avio_seek(s->pb, -size, SEEK_CUR);
389
390 ret = av_get_packet(s->pb, pkt, len);
391 if (ret != len) {
392 av_log(c, AV_LOG_ERROR, "Failed to get packet for obu\n");
393 return ret < 0 ? ret : AVERROR_INVALIDDATA;
394 }
395 return 0;
396 }
397
398 static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
399 {
400 AV1DemuxContext *const c = s->priv_data;
401 int ret;
402
403 if (s->io_repositioned) {
404 av_bsf_flush(c->bsf);
405 s->io_repositioned = 0;
406 }
407 while (1) {
408 ret = obu_get_packet(s, pkt);
409 /* In case of AVERROR_EOF we need to flush the BSF. Conveniently
410 * obu_get_packet() returns a blank pkt in this case which
411 * can be used to signal that the BSF should be flushed. */
412 if (ret < 0 && ret != AVERROR_EOF)
413 return ret;
414 ret = av_bsf_send_packet(c->bsf, pkt);
415 if (ret < 0) {
416 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
417 "av1_frame_merge filter\n");
418 return ret;
419 }
420 ret = av_bsf_receive_packet(c->bsf, pkt);
421 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
422 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
423 "send output packet\n");
424 if (ret != AVERROR(EAGAIN))
425 break;
426 }
427
428 return ret;
429 }
430
431 const FFInputFormat ff_obu_demuxer = {
432 .p.name = "obu",
433 .p.long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
434 .p.extensions = "obu",
435 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS,
436 .p.priv_class = &av1_demuxer_class,
437 .priv_data_size = sizeof(AV1DemuxContext),
438 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
439 .read_probe = obu_probe,
440 .read_header = av1_read_header,
441 .read_packet = obu_read_packet,
442 .read_close = av1_read_close,
443 };
444 #endif
445