Coverage Report

Created: 2026-02-14 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavformat/av1dec.c
Line
Count
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
    int64_t pos;
40
} AV1DemuxContext;
41
42
//return < 0 if we need more data
43
static int get_score(int type, int *seq)
44
42.3k
{
45
42.3k
    switch (type) {
46
21.2k
    case AV1_OBU_SEQUENCE_HEADER:
47
21.2k
        *seq = 1;
48
21.2k
        return -1;
49
9.42k
    case AV1_OBU_FRAME:
50
10.8k
    case AV1_OBU_FRAME_HEADER:
51
10.8k
        return *seq ? AVPROBE_SCORE_EXTENSION + 1 : 0;
52
5.15k
    case AV1_OBU_METADATA:
53
9.19k
    case AV1_OBU_PADDING:
54
9.19k
        return -1;
55
1.12k
    default:
56
1.12k
        break;
57
42.3k
    }
58
1.12k
    return 0;
59
42.3k
}
60
61
static int av1_read_header(AVFormatContext *s)
62
17.7k
{
63
17.7k
    AV1DemuxContext *const c = s->priv_data;
64
17.7k
    const AVBitStreamFilter *filter = av_bsf_get_by_name("av1_frame_merge");
65
17.7k
    AVStream *st;
66
17.7k
    FFStream *sti;
67
17.7k
    int ret;
68
69
17.7k
    if (!filter) {
70
0
        av_log(s, AV_LOG_ERROR, "av1_frame_merge bitstream filter "
71
0
               "not found. This is a bug, please report it.\n");
72
0
        return AVERROR_BUG;
73
0
    }
74
75
17.7k
    st = avformat_new_stream(s, NULL);
76
17.7k
    if (!st)
77
0
        return AVERROR(ENOMEM);
78
17.7k
    sti = ffstream(st);
79
80
17.7k
    st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
81
17.7k
    st->codecpar->codec_id = AV_CODEC_ID_AV1;
82
17.7k
    sti->need_parsing = AVSTREAM_PARSE_HEADERS;
83
84
17.7k
    st->avg_frame_rate = c->framerate;
85
    // taken from rawvideo demuxers
86
17.7k
    avpriv_set_pts_info(st, 64, 1, 1200000);
87
88
17.7k
    ret = av_bsf_alloc(filter, &c->bsf);
89
17.7k
    if (ret < 0)
90
0
        return ret;
91
92
17.7k
    ret = avcodec_parameters_copy(c->bsf->par_in, st->codecpar);
93
17.7k
    if (ret < 0)
94
0
        return ret;
95
96
17.7k
    ret = av_bsf_init(c->bsf);
97
17.7k
    if (ret < 0)
98
0
        return ret;
99
100
17.7k
    return 0;
101
17.7k
}
102
103
static int av1_read_close(AVFormatContext *s)
104
17.7k
{
105
17.7k
    AV1DemuxContext *const c = s->priv_data;
106
107
17.7k
    av_bsf_free(&c->bsf);
108
17.7k
    return 0;
109
17.7k
}
110
111
#define DEC AV_OPT_FLAG_DECODING_PARAM
112
#define OFFSET(x) offsetof(AV1DemuxContext, x)
113
static 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
119
static const AVClass av1_demuxer_class = {
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
128
2.74M
static int leb(AVIOContext *pb, uint32_t *len, int eof) {
129
2.74M
    int more, i = 0;
130
2.74M
    *len = 0;
131
3.49M
    do {
132
3.49M
        unsigned bits;
133
3.49M
        int byte = avio_r8(pb);
134
3.49M
        if (pb->error)
135
0
            return pb->error;
136
3.49M
        if (pb->eof_reached)
137
28.4k
            return (eof && !i) ? AVERROR_EOF : AVERROR_INVALIDDATA;
138
3.46M
        more = byte & 0x80;
139
3.46M
        bits = byte & 0x7f;
140
3.46M
        if (i <= 3 || (i == 4 && bits < (1 << 4)))
141
3.39M
            *len |= bits << (i * 7);
142
71.0k
        else if (bits)
143
55.2k
            return AVERROR_INVALIDDATA;
144
3.41M
        if (++i == 8 && more)
145
1.18k
            return AVERROR_INVALIDDATA;
146
3.41M
    } while (more);
147
2.66M
    return i;
148
2.74M
}
149
150
static int read_obu(const uint8_t *buf, int size, int64_t *obu_size, int *type)
151
159k
{
152
159k
    int start_pos, temporal_id, spatial_id;
153
159k
    int len;
154
155
159k
    len = parse_obu_header(buf, size, obu_size, &start_pos,
156
159k
                           type, &temporal_id, &spatial_id);
157
159k
    if (len < 0)
158
61.5k
        return len;
159
160
98.2k
    return 0;
161
159k
}
162
163
static int annexb_probe(const AVProbeData *p)
164
936k
{
165
936k
    FFIOContext ctx;
166
936k
    AVIOContext *const pb = &ctx.pub;
167
936k
    int64_t obu_size;
168
936k
    uint32_t temporal_unit_size, frame_unit_size, obu_unit_size;
169
936k
    int seq = 0;
170
936k
    int ret, type, cnt = 0;
171
172
936k
    ffio_init_read_context(&ctx, p->buf, p->buf_size);
173
174
936k
    ret = leb(pb, &temporal_unit_size, 1);
175
936k
    if (ret < 0)
176
46.7k
        return 0;
177
889k
    cnt += ret;
178
889k
    ret = leb(pb, &frame_unit_size, 0);
179
889k
    if (ret < 0 || ((int64_t)frame_unit_size + ret) > temporal_unit_size)
180
549k
        return 0;
181
340k
    cnt += ret;
182
340k
    ret = leb(pb, &obu_unit_size, 0);
183
340k
    if (ret < 0 || ((int64_t)obu_unit_size + ret) >= frame_unit_size)
184
187k
        return 0;
185
153k
    cnt += ret;
186
187
153k
    frame_unit_size -= obu_unit_size + ret;
188
189
153k
    avio_skip(pb, obu_unit_size);
190
153k
    if (pb->eof_reached || pb->error)
191
25.1k
        return 0;
192
193
    // Check that the first OBU is a Temporal Delimiter.
194
127k
    ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
195
127k
    if (ret < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size > 0)
196
111k
        return 0;
197
16.3k
    cnt += obu_unit_size;
198
199
37.1k
    do {
200
37.1k
        ret = leb(pb, &obu_unit_size, 0);
201
37.1k
        if (ret < 0 || ((int64_t)obu_unit_size + ret) > frame_unit_size)
202
1.95k
            return 0;
203
35.1k
        cnt += ret;
204
205
35.1k
        avio_skip(pb, obu_unit_size);
206
35.1k
        if (pb->eof_reached || pb->error)
207
3.27k
            return 0;
208
209
31.8k
        ret = read_obu(p->buf + cnt, FFMIN(p->buf_size - cnt, obu_unit_size), &obu_size, &type);
210
31.8k
        if (ret < 0)
211
1.38k
            return 0;
212
30.4k
        cnt += obu_unit_size;
213
214
30.4k
        ret = get_score(type, &seq);
215
30.4k
        if (ret >= 0)
216
9.73k
            return ret;
217
218
20.7k
        frame_unit_size -= obu_unit_size + ret;
219
20.7k
    } while (frame_unit_size);
220
221
0
    return 0;
222
16.3k
}
223
224
static int annexb_read_packet(AVFormatContext *s, AVPacket *pkt)
225
158k
{
226
158k
    AV1DemuxContext *const c = s->priv_data;
227
158k
    uint32_t obu_unit_size;
228
158k
    int64_t pos = c->pos;
229
158k
    int ret, len;
230
231
530k
retry:
232
530k
    if (avio_feof(s->pb)) {
233
2.42k
        if (c->temporal_unit_size || c->frame_unit_size)
234
2.38k
            return AVERROR_INVALIDDATA;
235
38
        goto end;
236
2.42k
    }
237
238
528k
    if (!c->temporal_unit_size) {
239
9.21k
        c->pos = avio_tell(s->pb);
240
9.21k
        len = leb(s->pb, &c->temporal_unit_size, 1);
241
9.21k
        if (len == AVERROR_EOF) goto end;
242
9.12k
        else if (len < 0) return len;
243
9.21k
    }
244
245
527k
    if (!c->frame_unit_size) {
246
9.18k
        len = leb(s->pb, &c->frame_unit_size, 0);
247
9.18k
        if (len < 0)
248
373
            return len;
249
8.81k
        if (((int64_t)c->frame_unit_size + len) > c->temporal_unit_size)
250
2.92k
            return AVERROR_INVALIDDATA;
251
5.88k
        c->temporal_unit_size -= len;
252
5.88k
    }
253
254
524k
    len = leb(s->pb, &obu_unit_size, 0);
255
524k
    if (len < 0)
256
4.58k
        return len;
257
519k
    if (((int64_t)obu_unit_size + len) > c->frame_unit_size)
258
354
        return AVERROR_INVALIDDATA;
259
260
519k
    ret = av_get_packet(s->pb, pkt, obu_unit_size);
261
519k
    if (ret < 0)
262
641
        return ret;
263
518k
    if (ret != obu_unit_size)
264
870
        return AVERROR_INVALIDDATA;
265
266
518k
    c->temporal_unit_size -= obu_unit_size + len;
267
518k
    c->frame_unit_size -= obu_unit_size + len;
268
269
518k
end:
270
518k
    ret = av_bsf_send_packet(c->bsf, pkt);
271
518k
    if (ret < 0) {
272
118
        av_log(s, AV_LOG_ERROR, "Failed to send packet to "
273
118
                                "av1_frame_merge filter\n");
274
118
        return ret;
275
118
    }
276
277
518k
    ret = av_bsf_receive_packet(c->bsf, pkt);
278
518k
    if (ret < 0) {
279
376k
        if (ret == AVERROR(EAGAIN))
280
372k
            goto retry;
281
4.36k
        if (ret != AVERROR_EOF)
282
3.93k
            av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
283
3.93k
                                    "send output packet\n");
284
4.36k
        return ret;
285
376k
    }
286
287
141k
    pkt->pos = pos;
288
289
141k
    return 0;
290
518k
}
291
292
const FFInputFormat ff_av1_demuxer = {
293
    .p.name         = "av1",
294
    .p.long_name    = NULL_IF_CONFIG_SMALL("AV1 Annex B"),
295
    .p.extensions   = "obu",
296
    .p.flags        = AVFMT_GENERIC_INDEX | AVFMT_NOTIMESTAMPS,
297
    .p.priv_class   = &av1_demuxer_class,
298
    .priv_data_size = sizeof(AV1DemuxContext),
299
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
300
    .read_probe     = annexb_probe,
301
    .read_header    = av1_read_header,
302
    .read_packet    = annexb_read_packet,
303
    .read_close     = av1_read_close,
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
311
static int read_obu_with_size(const uint8_t *buf, int buf_size, int64_t *obu_size, int *type)
312
3.39M
{
313
3.39M
    GetBitContext gb;
314
3.39M
    int ret, extension_flag, start_pos;
315
3.39M
    int64_t size;
316
317
3.39M
    ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
318
3.39M
    if (ret < 0)
319
0
        return ret;
320
321
3.39M
    if (get_bits1(&gb) != 0) // obu_forbidden_bit
322
193k
        return AVERROR_INVALIDDATA;
323
324
3.20M
    *type      = get_bits(&gb, 4);
325
3.20M
    extension_flag = get_bits1(&gb);
326
3.20M
    if (!get_bits1(&gb))    // has_size_flag
327
538k
        return AVERROR_INVALIDDATA;
328
2.66M
    skip_bits1(&gb);        // obu_reserved_1bit
329
330
2.66M
    if (extension_flag) {
331
253k
        get_bits(&gb, 3);   // temporal_id
332
253k
        get_bits(&gb, 2);   // spatial_id
333
253k
        skip_bits(&gb, 3);  // extension_header_reserved_3bits
334
253k
    }
335
336
2.66M
    *obu_size  = get_leb128(&gb);
337
2.66M
    if (*obu_size > INT_MAX)
338
2.75k
        return AVERROR_INVALIDDATA;
339
340
2.66M
    if (get_bits_left(&gb) < 0)
341
2.20k
        return AVERROR_INVALIDDATA;
342
343
2.65M
    start_pos = get_bits_count(&gb) / 8;
344
345
2.65M
    size = *obu_size + start_pos;
346
2.65M
    if (size > INT_MAX)
347
190
        return AVERROR_INVALIDDATA;
348
2.65M
    return size;
349
2.65M
}
350
351
static int obu_probe(const AVProbeData *p)
352
936k
{
353
936k
    int64_t obu_size;
354
936k
    int seq = 0;
355
936k
    int ret, type, cnt;
356
357
    // Check that the first OBU is a Temporal Delimiter.
358
936k
    cnt = read_obu_with_size(p->buf, p->buf_size, &obu_size, &type);
359
936k
    if (cnt < 0 || type != AV1_OBU_TEMPORAL_DELIMITER || obu_size != 0)
360
931k
        return 0;
361
362
14.5k
    while (1) {
363
14.5k
        ret = read_obu_with_size(p->buf + cnt, p->buf_size - cnt, &obu_size, &type);
364
14.5k
        if (ret < 0 || obu_size <= 0)
365
2.67k
            return 0;
366
11.8k
        cnt += FFMIN(ret, p->buf_size - cnt);
367
368
11.8k
        ret = get_score(type, &seq);
369
11.8k
        if (ret >= 0)
370
2.22k
            return ret;
371
11.8k
    }
372
0
    return 0;
373
4.89k
}
374
375
static int obu_get_packet(AVFormatContext *s, AVPacket *pkt)
376
2.46M
{
377
2.46M
    AV1DemuxContext *const c = s->priv_data;
378
2.46M
    uint8_t header[MAX_OBU_HEADER_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
379
2.46M
    int64_t obu_size;
380
2.46M
    int size;
381
2.46M
    int ret, len, type;
382
383
2.46M
    if ((ret = ffio_ensure_seekback(s->pb, MAX_OBU_HEADER_SIZE)) < 0)
384
0
        return ret;
385
2.46M
    size = avio_read(s->pb, header, MAX_OBU_HEADER_SIZE);
386
2.46M
    if (size < 0)
387
14.4k
        return size;
388
389
2.44M
    memset(header + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
390
2.44M
    len = read_obu_with_size(header, size, &obu_size, &type);
391
2.44M
    if (len < 0) {
392
1.95k
        av_log(c, AV_LOG_ERROR, "Failed to read obu\n");
393
1.95k
        return len;
394
1.95k
    }
395
2.44M
    avio_seek(s->pb, -size, SEEK_CUR);
396
397
2.44M
    ret = av_get_packet(s->pb, pkt, len);
398
2.44M
    if (ret != len) {
399
1.26k
        av_log(c, AV_LOG_ERROR, "Failed to get packet for obu\n");
400
1.26k
        return ret < 0 ? ret : AVERROR_INVALIDDATA;
401
1.26k
    }
402
2.44M
    return 0;
403
2.44M
}
404
405
static int obu_read_packet(AVFormatContext *s, AVPacket *pkt)
406
185k
{
407
185k
    AV1DemuxContext *const c = s->priv_data;
408
185k
    int ret;
409
410
185k
    if (s->io_repositioned) {
411
0
        av_bsf_flush(c->bsf);
412
0
        s->io_repositioned = 0;
413
0
    }
414
2.46M
    while (1) {
415
2.46M
        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
2.46M
        if (ret < 0 && ret != AVERROR_EOF)
420
3.16k
            return ret;
421
2.45M
        ret = av_bsf_send_packet(c->bsf, pkt);
422
2.45M
        if (ret < 0) {
423
0
            av_log(s, AV_LOG_ERROR, "Failed to send packet to "
424
0
                                    "av1_frame_merge filter\n");
425
0
            return ret;
426
0
        }
427
2.45M
        ret = av_bsf_receive_packet(c->bsf, pkt);
428
2.45M
        if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
429
5.14k
            av_log(s, AV_LOG_ERROR, "av1_frame_merge filter failed to "
430
5.14k
                                    "send output packet\n");
431
2.45M
        if (ret != AVERROR(EAGAIN))
432
182k
            break;
433
2.45M
    }
434
435
182k
    return ret;
436
185k
}
437
438
const FFInputFormat ff_obu_demuxer = {
439
    .p.name         = "obu",
440
    .p.long_name    = NULL_IF_CONFIG_SMALL("AV1 low overhead OBU"),
441
    .p.extensions   = "obu",
442
    .p.flags        = AVFMT_GENERIC_INDEX | AVFMT_NO_BYTE_SEEK | AVFMT_NOTIMESTAMPS,
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,
447
    .read_header    = av1_read_header,
448
    .read_packet    = obu_read_packet,
449
    .read_close     = av1_read_close,
450
};
451
#endif