Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/av1_parse.h |
Date: | 2022-07-04 00:18:54 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 45 | 49 | 91.8% |
Branches: | 28 | 34 | 82.4% |
Line | Branch | Exec | 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 | |||
34 | // OBU header fields + max leb128 length | ||
35 | #define MAX_OBU_HEADER_SIZE (2 + 8) | ||
36 | |||
37 | typedef struct AV1OBU { | ||
38 | /** Size of payload */ | ||
39 | int size; | ||
40 | const uint8_t *data; | ||
41 | |||
42 | /** | ||
43 | * Size, in bits, of just the data, excluding the trailing_one_bit and | ||
44 | * any trailing padding. | ||
45 | */ | ||
46 | int size_bits; | ||
47 | |||
48 | /** Size of entire OBU, including header */ | ||
49 | int raw_size; | ||
50 | const uint8_t *raw_data; | ||
51 | |||
52 | /** GetBitContext initialized to the start of the payload */ | ||
53 | GetBitContext gb; | ||
54 | |||
55 | int type; | ||
56 | |||
57 | int temporal_id; | ||
58 | int spatial_id; | ||
59 | } AV1OBU; | ||
60 | |||
61 | /** An input packet split into OBUs */ | ||
62 | typedef struct AV1Packet { | ||
63 | AV1OBU *obus; | ||
64 | int nb_obus; | ||
65 | int obus_allocated; | ||
66 | unsigned obus_allocated_size; | ||
67 | } AV1Packet; | ||
68 | |||
69 | /** | ||
70 | * Extract an OBU from a raw bitstream. | ||
71 | * | ||
72 | * @note This function does not copy or store any bitstream data. All | ||
73 | * the pointers in the AV1OBU structure will be valid as long | ||
74 | * as the input buffer also is. | ||
75 | */ | ||
76 | int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, | ||
77 | void *logctx); | ||
78 | |||
79 | /** | ||
80 | * Split an input packet into OBUs. | ||
81 | * | ||
82 | * @note This function does not copy or store any bitstream data. All | ||
83 | * the pointers in the AV1Packet structure will be valid as | ||
84 | * long as the input buffer also is. | ||
85 | */ | ||
86 | int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, | ||
87 | void *logctx); | ||
88 | |||
89 | /** | ||
90 | * Free all the allocated memory in the packet. | ||
91 | */ | ||
92 | void ff_av1_packet_uninit(AV1Packet *pkt); | ||
93 | |||
94 | 3109 | static inline int64_t leb128(GetBitContext *gb) { | |
95 | 3109 | int64_t ret = 0; | |
96 | int i; | ||
97 | |||
98 |
2/2✓ Branch 0 taken 3685 times.
✓ Branch 1 taken 6 times.
|
3691 | for (i = 0; i < 8; i++) { |
99 | 3685 | int byte = get_bits(gb, 8); | |
100 | 3685 | ret |= (int64_t)(byte & 0x7f) << (i * 7); | |
101 |
2/2✓ Branch 0 taken 3103 times.
✓ Branch 1 taken 582 times.
|
3685 | if (!(byte & 0x80)) |
102 | 3103 | break; | |
103 | } | ||
104 | 3109 | return ret; | |
105 | } | ||
106 | |||
107 | 1287 | static inline int parse_obu_header(const uint8_t *buf, int buf_size, | |
108 | int64_t *obu_size, int *start_pos, int *type, | ||
109 | int *temporal_id, int *spatial_id) | ||
110 | { | ||
111 | GetBitContext gb; | ||
112 | int ret, extension_flag, has_size_flag; | ||
113 | int64_t size; | ||
114 | |||
115 | 1287 | ret = init_get_bits8(&gb, buf, FFMIN(buf_size, MAX_OBU_HEADER_SIZE)); | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1287 times.
|
1287 | if (ret < 0) |
117 | ✗ | return ret; | |
118 | |||
119 |
2/2✓ Branch 1 taken 34 times.
✓ Branch 2 taken 1253 times.
|
1287 | if (get_bits1(&gb) != 0) // obu_forbidden_bit |
120 | 34 | return AVERROR_INVALIDDATA; | |
121 | |||
122 | 1253 | *type = get_bits(&gb, 4); | |
123 | 1253 | extension_flag = get_bits1(&gb); | |
124 | 1253 | has_size_flag = get_bits1(&gb); | |
125 | 1253 | skip_bits1(&gb); // obu_reserved_1bit | |
126 | |||
127 |
2/2✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 236 times.
|
1253 | if (extension_flag) { |
128 | 1017 | *temporal_id = get_bits(&gb, 3); | |
129 | 1017 | *spatial_id = get_bits(&gb, 2); | |
130 | 1017 | skip_bits(&gb, 3); // extension_header_reserved_3bits | |
131 | } else { | ||
132 | 236 | *temporal_id = *spatial_id = 0; | |
133 | } | ||
134 | |||
135 | 1113 | *obu_size = has_size_flag ? leb128(&gb) | |
136 |
2/2✓ Branch 0 taken 1113 times.
✓ Branch 1 taken 140 times.
|
1253 | : buf_size - 1 - extension_flag; |
137 | |||
138 |
2/2✓ Branch 1 taken 29 times.
✓ Branch 2 taken 1224 times.
|
1253 | if (get_bits_left(&gb) < 0) |
139 | 29 | return AVERROR_INVALIDDATA; | |
140 | |||
141 | 1224 | *start_pos = get_bits_count(&gb) / 8; | |
142 | |||
143 | 1224 | size = *obu_size + *start_pos; | |
144 | |||
145 |
2/2✓ Branch 0 taken 623 times.
✓ Branch 1 taken 601 times.
|
1224 | if (size > buf_size) |
146 | 623 | return AVERROR_INVALIDDATA; | |
147 | |||
148 | 601 | return size; | |
149 | } | ||
150 | |||
151 | 78 | static inline int get_obu_bit_length(const uint8_t *buf, int size, int type) | |
152 | { | ||
153 | int v; | ||
154 | |||
155 | /* There are no trailing bits on these */ | ||
156 |
3/4✓ Branch 0 taken 65 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
|
78 | if (type == AV1_OBU_TILE_GROUP || |
157 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 49 times.
|
65 | type == AV1_OBU_TILE_LIST || |
158 | type == AV1_OBU_FRAME) { | ||
159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29 | if (size > INT_MAX / 8) |
160 | ✗ | return AVERROR(ERANGE); | |
161 | else | ||
162 | 29 | return size * 8; | |
163 | } | ||
164 | |||
165 |
3/4✓ Branch 0 taken 30 times.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
49 | while (size > 0 && buf[size - 1] == 0) |
166 | ✗ | size--; | |
167 | |||
168 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 30 times.
|
49 | if (!size) |
169 | 19 | return 0; | |
170 | |||
171 | 30 | v = buf[size - 1]; | |
172 | |||
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (size > INT_MAX / 8) |
174 | ✗ | return AVERROR(ERANGE); | |
175 | 30 | size *= 8; | |
176 | |||
177 | /* Remove the trailing_one_bit and following trailing zeros */ | ||
178 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (v) |
179 | 30 | size -= ff_ctz(v) + 1; | |
180 | |||
181 | 30 | return size; | |
182 | } | ||
183 | |||
184 | #endif /* AVCODEC_AV1_PARSE_H */ | ||
185 |