Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/av1_parse.h
Line
Count
Source
1
/*
2
 * AV1 common parsing code
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
#ifndef AVCODEC_AV1_PARSE_H
22
#define AVCODEC_AV1_PARSE_H
23
24
#include <limits.h>
25
#include <stdint.h>
26
27
#include "libavutil/error.h"
28
#include "libavutil/intmath.h"
29
#include "libavutil/macros.h"
30
31
#include "av1.h"
32
#include "get_bits.h"
33
#include "leb.h"
34
35
// OBU header fields + max leb128 length
36
7.27M
#define MAX_OBU_HEADER_SIZE (2 + 8)
37
38
typedef struct AV1OBU {
39
    /** Size of payload */
40
    int size;
41
    const uint8_t *data;
42
43
    /**
44
     * Size, in bits, of just the data, excluding the trailing_one_bit and
45
     * any trailing padding.
46
     */
47
    int size_bits;
48
49
    /** Size of entire OBU, including header */
50
    int raw_size;
51
    const uint8_t *raw_data;
52
53
    int type;
54
55
    int temporal_id;
56
    int spatial_id;
57
} AV1OBU;
58
59
/** An input packet split into OBUs */
60
typedef struct AV1Packet {
61
    AV1OBU *obus;
62
    int nb_obus;
63
    int obus_allocated;
64
    unsigned obus_allocated_size;
65
} AV1Packet;
66
67
/**
68
 * Extract an OBU from a raw bitstream.
69
 *
70
 * @note This function does not copy or store any bitstream data. All
71
 *       the pointers in the AV1OBU structure will be valid as long
72
 *       as the input buffer also is.
73
 */
74
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length,
75
                       void *logctx);
76
77
/**
78
 * Split an input packet into OBUs.
79
 *
80
 * @note This function does not copy or store any bitstream data. All
81
 *       the pointers in the AV1Packet structure will be valid as
82
 *       long as the input buffer also is.
83
 */
84
int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length,
85
                        void *logctx);
86
87
/**
88
 * Free all the allocated memory in the packet.
89
 */
90
void ff_av1_packet_uninit(AV1Packet *pkt);
91
92
static inline int parse_obu_header(const uint8_t *buf, int buf_size,
93
                                   int64_t *obu_size, int *start_pos, int *type,
94
                                   int *temporal_id, int *spatial_id)
95
1.21M
{
96
1.21M
    GetBitContext gb;
97
1.21M
    int ret, extension_flag, has_size_flag;
98
1.21M
    int64_t size;
99
100
1.21M
    ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
101
1.21M
    if (ret < 0)
102
0
        return ret;
103
104
1.21M
    if (get_bits1(&gb) != 0) // obu_forbidden_bit
105
13.9k
        return AVERROR_INVALIDDATA;
106
107
1.20M
    *type      = get_bits(&gb, 4);
108
1.20M
    extension_flag = get_bits1(&gb);
109
1.20M
    has_size_flag  = get_bits1(&gb);
110
1.20M
    skip_bits1(&gb); // obu_reserved_1bit
111
112
1.20M
    if (extension_flag) {
113
75.3k
        *temporal_id = get_bits(&gb, 3);
114
75.3k
        *spatial_id  = get_bits(&gb, 2);
115
75.3k
        skip_bits(&gb, 3); // extension_header_reserved_3bits
116
1.13M
    } else {
117
1.13M
        *temporal_id = *spatial_id = 0;
118
1.13M
    }
119
120
1.20M
    *obu_size  = has_size_flag ? get_leb128(&gb)
121
1.20M
                               : buf_size - 1 - extension_flag;
122
123
1.20M
    if (get_bits_left(&gb) < 0)
124
27.7k
        return AVERROR_INVALIDDATA;
125
126
1.17M
    *start_pos = get_bits_count(&gb) / 8;
127
128
1.17M
    size = *obu_size + *start_pos;
129
130
1.17M
    if (size > buf_size)
131
23.3k
        return AVERROR_INVALIDDATA;
132
133
1.15M
    return size;
134
1.17M
}
Unexecuted instantiation: extract_extradata.c:parse_obu_header
Unexecuted instantiation: remove_extradata.c:parse_obu_header
av1_parse.c:parse_obu_header
Line
Count
Source
95
1.04M
{
96
1.04M
    GetBitContext gb;
97
1.04M
    int ret, extension_flag, has_size_flag;
98
1.04M
    int64_t size;
99
100
1.04M
    ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
101
1.04M
    if (ret < 0)
102
0
        return ret;
103
104
1.04M
    if (get_bits1(&gb) != 0) // obu_forbidden_bit
105
644
        return AVERROR_INVALIDDATA;
106
107
1.04M
    *type      = get_bits(&gb, 4);
108
1.04M
    extension_flag = get_bits1(&gb);
109
1.04M
    has_size_flag  = get_bits1(&gb);
110
1.04M
    skip_bits1(&gb); // obu_reserved_1bit
111
112
1.04M
    if (extension_flag) {
113
3.22k
        *temporal_id = get_bits(&gb, 3);
114
3.22k
        *spatial_id  = get_bits(&gb, 2);
115
3.22k
        skip_bits(&gb, 3); // extension_header_reserved_3bits
116
1.04M
    } else {
117
1.04M
        *temporal_id = *spatial_id = 0;
118
1.04M
    }
119
120
1.04M
    *obu_size  = has_size_flag ? get_leb128(&gb)
121
1.04M
                               : buf_size - 1 - extension_flag;
122
123
1.04M
    if (get_bits_left(&gb) < 0)
124
533
        return AVERROR_INVALIDDATA;
125
126
1.04M
    *start_pos = get_bits_count(&gb) / 8;
127
128
1.04M
    size = *obu_size + *start_pos;
129
130
1.04M
    if (size > buf_size)
131
918
        return AVERROR_INVALIDDATA;
132
133
1.04M
    return size;
134
1.04M
}
Unexecuted instantiation: av1_parser.c:parse_obu_header
av1dec.c:parse_obu_header
Line
Count
Source
95
173k
{
96
173k
    GetBitContext gb;
97
173k
    int ret, extension_flag, has_size_flag;
98
173k
    int64_t size;
99
100
173k
    ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE));
101
173k
    if (ret < 0)
102
0
        return ret;
103
104
173k
    if (get_bits1(&gb) != 0) // obu_forbidden_bit
105
13.2k
        return AVERROR_INVALIDDATA;
106
107
160k
    *type      = get_bits(&gb, 4);
108
160k
    extension_flag = get_bits1(&gb);
109
160k
    has_size_flag  = get_bits1(&gb);
110
160k
    skip_bits1(&gb); // obu_reserved_1bit
111
112
160k
    if (extension_flag) {
113
72.0k
        *temporal_id = get_bits(&gb, 3);
114
72.0k
        *spatial_id  = get_bits(&gb, 2);
115
72.0k
        skip_bits(&gb, 3); // extension_header_reserved_3bits
116
88.2k
    } else {
117
88.2k
        *temporal_id = *spatial_id = 0;
118
88.2k
    }
119
120
160k
    *obu_size  = has_size_flag ? get_leb128(&gb)
121
160k
                               : buf_size - 1 - extension_flag;
122
123
160k
    if (get_bits_left(&gb) < 0)
124
27.1k
        return AVERROR_INVALIDDATA;
125
126
133k
    *start_pos = get_bits_count(&gb) / 8;
127
128
133k
    size = *obu_size + *start_pos;
129
130
133k
    if (size > buf_size)
131
22.4k
        return AVERROR_INVALIDDATA;
132
133
110k
    return size;
134
133k
}
135
136
static inline int get_obu_bit_length(const uint8_t *buf, int size, int type)
137
1.04M
{
138
1.04M
    int v;
139
140
    /* There are no trailing bits on these */
141
1.04M
    if (type == AV1_OBU_TILE_GROUP ||
142
1.04M
        type == AV1_OBU_TILE_LIST ||
143
1.04M
        type == AV1_OBU_FRAME) {
144
1.91k
        if (size > INT_MAX / 8)
145
0
            return AVERROR(ERANGE);
146
1.91k
        else
147
1.91k
            return size * 8;
148
1.91k
    }
149
150
1.08M
    while (size > 0 && buf[size - 1] == 0)
151
47.1k
        size--;
152
153
1.04M
    if (!size)
154
2.06k
        return 0;
155
156
1.03M
    v = buf[size - 1];
157
158
1.03M
    if (size > INT_MAX / 8)
159
0
        return AVERROR(ERANGE);
160
1.03M
    size *= 8;
161
162
    /* Remove the trailing_one_bit and following trailing zeros */
163
1.03M
    if (v)
164
1.03M
        size -= ff_ctz(v) + 1;
165
166
1.03M
    return size;
167
1.03M
}
Unexecuted instantiation: extract_extradata.c:get_obu_bit_length
Unexecuted instantiation: remove_extradata.c:get_obu_bit_length
av1_parse.c:get_obu_bit_length
Line
Count
Source
137
1.04M
{
138
1.04M
    int v;
139
140
    /* There are no trailing bits on these */
141
1.04M
    if (type == AV1_OBU_TILE_GROUP ||
142
1.04M
        type == AV1_OBU_TILE_LIST ||
143
1.04M
        type == AV1_OBU_FRAME) {
144
1.91k
        if (size > INT_MAX / 8)
145
0
            return AVERROR(ERANGE);
146
1.91k
        else
147
1.91k
            return size * 8;
148
1.91k
    }
149
150
1.08M
    while (size > 0 && buf[size - 1] == 0)
151
47.1k
        size--;
152
153
1.04M
    if (!size)
154
2.06k
        return 0;
155
156
1.03M
    v = buf[size - 1];
157
158
1.03M
    if (size > INT_MAX / 8)
159
0
        return AVERROR(ERANGE);
160
1.03M
    size *= 8;
161
162
    /* Remove the trailing_one_bit and following trailing zeros */
163
1.03M
    if (v)
164
1.03M
        size -= ff_ctz(v) + 1;
165
166
1.03M
    return size;
167
1.03M
}
Unexecuted instantiation: av1_parser.c:get_obu_bit_length
Unexecuted instantiation: av1dec.c:get_obu_bit_length
168
169
AVRational ff_av1_framerate(int64_t ticks_per_frame, int64_t units_per_tick,
170
                            int64_t time_scale);
171
172
#endif /* AVCODEC_AV1_PARSE_H */