FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/av1dec.c
Date: 2024-04-27 00:58:15
Exec Total Coverage
Lines: 154 219 70.3%
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 16098 static int leb(AVIOContext *pb, uint32_t *len, int eof) {
128 16098 int more, i = 0;
129 16098 *len = 0;
130 do {
131 unsigned bits;
132 17039 int byte = avio_r8(pb);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17039 times.
17039 if (pb->error)
134 return pb->error;
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 17038 times.
17039 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 17038 more = byte & 0x80;
138 17038 bits = byte & 0x7f;
139
5/6
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 16986 times.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✓ Branch 5 taken 30 times.
17038 if (i <= 3 || (i == 4 && bits < (1 << 4)))
140 17008 *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 17008 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
17008 if (++i == 8 && more)
144 return AVERROR_INVALIDDATA;
145
2/2
✓ Branch 0 taken 941 times.
✓ Branch 1 taken 16067 times.
17008 } while (more);
146 16067 return i;
147 }
148
149 1244 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 1244 len = parse_obu_header(buf, size, obu_size, &start_pos,
155 type, &temporal_id, &spatial_id);
156
2/2
✓ Branch 0 taken 731 times.
✓ Branch 1 taken 513 times.
1244 if (len < 0)
157 731 return len;
158
159 513 return 0;
160 }
161
162 7128 static int annexb_probe(const AVProbeData *p)
163 {
164 FFIOContext ctx;
165 7128 AVIOContext *const pb = &ctx.pub;
166 int64_t obu_size;
167 uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
168 7128 int seq = 0;
169 7128 int ret, type, cnt = 0;
170
171 7128 ffio_init_read_context(&ctx, p->buf, p->buf_size);
172
173 7128 ret = leb(pb, &temporal_unit_size, 1);
174
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7115 times.
7128 if (ret < 0)
175 13 return 0;
176 7115 cnt += ret;
177 7115 ret = leb(pb, &frame_unit_size, 0);
178
4/4
✓ Branch 0 taken 7098 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 5297 times.
✓ Branch 3 taken 1801 times.
7115 if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
179 5314 return 0;
180 1801 cnt += ret;
181 1801 ret = leb(pb, &obu_unit_size, 0);
182
3/4
✓ Branch 0 taken 1801 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 562 times.
✓ Branch 3 taken 1239 times.
1801 if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
183 562 return 0;
184 1239 cnt += ret;
185
186 1239 frame_unit_size -= obu_unit_size + ret;
187
188 1239 avio_skip(pb, obu_unit_size);
189
3/4
✓ Branch 0 taken 1237 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1237 times.
1239 if (pb->eof_reached || pb->error)
190 2 return 0;
191
192 // Check that the first OBU is a Temporal Delimiter.
193 1237 ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
194
6/6
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 731 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 477 times.
✓ Branch 4 taken 23 times.
✓ Branch 5 taken 6 times.
1237 if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
195 1231 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 7132 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 7132 ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7132 times.
7132 if (ret < 0)
312 return ret;
313
314
2/2
✓ Branch 1 taken 246 times.
✓ Branch 2 taken 6886 times.
7132 if (get_bits1(&gb) != 0) // obu_forbidden_bit
315 246 return AVERROR_INVALIDDATA;
316
317 6886 *type = get_bits(&gb, 4);
318 6886 extension_flag = get_bits1(&gb);
319
2/2
✓ Branch 1 taken 4741 times.
✓ Branch 2 taken 2145 times.
6886 if (!get_bits1(&gb)) // has_size_flag
320 4741 return AVERROR_INVALIDDATA;
321 2145 skip_bits1(&gb); // obu_reserved_1bit
322
323
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 1596 times.
2145 if (extension_flag) {
324 549 get_bits(&gb, 3); // temporal_id
325 549 get_bits(&gb, 2); // spatial_id
326 549 skip_bits(&gb, 3); // extension_header_reserved_3bits
327 }
328
329 2145 *obu_size = get_leb128(&gb);
330
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2142 times.
2145 if (*obu_size > INT_MAX)
331 3 return AVERROR_INVALIDDATA;
332
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2142 times.
2142 if (get_bits_left(&gb) < 0)
334 return AVERROR_INVALIDDATA;
335
336 2142 start_pos = get_bits_count(&gb) / 8;
337
338 2142 size = *obu_size + start_pos;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2142 times.
2142 if (size > INT_MAX)
340 return AVERROR_INVALIDDATA;
341 2142 return size;
342 }
343
344 7128 static int obu_probe(const AVProbeData *p)
345 {
346 int64_t obu_size;
347 7128 int seq = 0;
348 int ret, type, cnt;
349
350 // Check that the first OBU is a Temporal Delimiter.
351 7128 cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
352
6/6
✓ Branch 0 taken 2142 times.
✓ Branch 1 taken 4986 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 2126 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 12 times.
7128 if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
353 7124 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 len = read_obu_with_size(header, size, &obu_size, &type);
383 if (len < 0) {
384 av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
385 return len;
386 }
387 avio_seek(s->pb, -size, SEEK_CUR);
388
389 ret = av_get_packet(s->pb, pkt, len);
390 if (ret != len) {
391 av_log(c, AV_LOG_ERROR, "Failed to get packet for obu\n");
392 return ret < 0 ? ret : AVERROR_INVALIDDATA;
393 }
394 return 0;
395 }
396
397 static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
398 {
399 AV1DemuxContext *const c = s->priv_data;
400 int ret;
401
402 if (s->io_repositioned) {
403 av_bsf_flush(c->bsf);
404 s->io_repositioned = 0;
405 }
406 while (1) {
407 ret = obu_get_packet(s, pkt);
408 /* In case of AVERROR_EOF we need to flush the BSF. Conveniently
409 * obu_get_packet() returns a blank pkt in this case which
410 * can be used to signal that the BSF should be flushed. */
411 if (ret < 0 && ret != AVERROR_EOF)
412 return ret;
413 ret = av_bsf_send_packet(c->bsf, pkt);
414 if (ret < 0) {
415 av_log(s, AV_LOG_ERROR, "Failed to send packet to "
416 "av1_frame_merge filter\n");
417 return ret;
418 }
419 ret = av_bsf_receive_packet(c->bsf, pkt);
420 if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
421 av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
422 "send output packet\n");
423 if (ret != AVERROR(EAGAIN))
424 break;
425 }
426
427 return ret;
428 }
429
430 const FFInputFormat ff_obu_demuxer = {
431 .p.name = "obu",
432 .p.long_name = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
433 .p.extensions = "obu",
434 .p.flags = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS,
435 .p.priv_class = &av1_demuxer_class,
436 .priv_data_size = sizeof(AV1DemuxContext),
437 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
438 .read_probe = obu_probe,
439 .read_header = av1_read_header,
440 .read_packet = obu_read_packet,
441 .read_close = av1_read_close,
442 };
443 #endif
444