FFmpeg coverage


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