Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * H.264/HEVC 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 | #include <string.h> | ||
22 | |||
23 | #include "config.h" | ||
24 | |||
25 | #include "libavutil/intmath.h" | ||
26 | #include "libavutil/intreadwrite.h" | ||
27 | #include "libavutil/mem.h" | ||
28 | |||
29 | #include "bytestream.h" | ||
30 | #include "hevc.h" | ||
31 | #include "h264.h" | ||
32 | #include "h2645_parse.h" | ||
33 | #include "vvc.h" | ||
34 | |||
35 | 227380 | int ff_h2645_extract_rbsp(const uint8_t *src, int length, | |
36 | H2645RBSP *rbsp, H2645NAL *nal, int small_padding) | ||
37 | { | ||
38 | int i, si, di; | ||
39 | uint8_t *dst; | ||
40 | |||
41 | 227380 | nal->skipped_bytes = 0; | |
42 | #define STARTCODE_TEST \ | ||
43 | if (i + 2 < length && src[i + 1] == 0 && \ | ||
44 | (src[i + 2] == 3 || src[i + 2] == 1)) { \ | ||
45 | if (src[i + 2] == 1) { \ | ||
46 | /* startcode, so we must be past the end */ \ | ||
47 | length = i; \ | ||
48 | } \ | ||
49 | break; \ | ||
50 | } | ||
51 | #if HAVE_FAST_UNALIGNED | ||
52 | #define FIND_FIRST_ZERO \ | ||
53 | if (i > 0 && !src[i]) \ | ||
54 | i--; \ | ||
55 | while (src[i]) \ | ||
56 | i++ | ||
57 | #if HAVE_FAST_64BIT | ||
58 |
2/2✓ Branch 0 taken 50220905 times.
✓ Branch 1 taken 102360 times.
|
50323265 | for (i = 0; i + 1 < length; i += 9) { |
59 | 98933561 | if (!((~AV_RN64(src + i) & | |
60 |
2/2✓ Branch 0 taken 48712656 times.
✓ Branch 1 taken 1508249 times.
|
50220905 | (AV_RN64(src + i) - 0x0100010001000101ULL)) & |
61 | 0x8000800080008080ULL)) | ||
62 | 48712656 | continue; | |
63 |
6/6✓ Branch 0 taken 1424185 times.
✓ Branch 1 taken 84064 times.
✓ Branch 2 taken 326539 times.
✓ Branch 3 taken 1097646 times.
✓ Branch 4 taken 4670778 times.
✓ Branch 5 taken 1508249 times.
|
6179027 | FIND_FIRST_ZERO; |
64 |
10/10✓ Branch 0 taken 1460682 times.
✓ Branch 1 taken 47567 times.
✓ Branch 2 taken 209725 times.
✓ Branch 3 taken 1250957 times.
✓ Branch 4 taken 201527 times.
✓ Branch 5 taken 8198 times.
✓ Branch 6 taken 116822 times.
✓ Branch 7 taken 84705 times.
✓ Branch 8 taken 116822 times.
✓ Branch 9 taken 8198 times.
|
1508249 | STARTCODE_TEST; |
65 | 1383229 | i -= 7; | |
66 | } | ||
67 | #else | ||
68 | for (i = 0; i + 1 < length; i += 5) { | ||
69 | if (!((~AV_RN32(src + i) & | ||
70 | (AV_RN32(src + i) - 0x01000101U)) & | ||
71 | 0x80008080U)) | ||
72 | continue; | ||
73 | FIND_FIRST_ZERO; | ||
74 | STARTCODE_TEST; | ||
75 | i -= 3; | ||
76 | } | ||
77 | #endif /* HAVE_FAST_64BIT */ | ||
78 | #else | ||
79 | for (i = 0; i + 1 < length; i += 2) { | ||
80 | if (src[i]) | ||
81 | continue; | ||
82 | if (i > 0 && src[i - 1] == 0) | ||
83 | i--; | ||
84 | STARTCODE_TEST; | ||
85 | } | ||
86 | #endif /* HAVE_FAST_UNALIGNED */ | ||
87 | |||
88 |
4/4✓ Branch 0 taken 219182 times.
✓ Branch 1 taken 8198 times.
✓ Branch 2 taken 169303 times.
✓ Branch 3 taken 49879 times.
|
227380 | if (i >= length - 1 && small_padding) { // no escaped 0 |
89 | 169303 | nal->data = | |
90 | 169303 | nal->raw_data = src; | |
91 | 169303 | nal->size = | |
92 | 169303 | nal->raw_size = length; | |
93 | 169303 | return length; | |
94 |
2/2✓ Branch 0 taken 23986 times.
✓ Branch 1 taken 34091 times.
|
58077 | } else if (i > length) |
95 | 23986 | i = length; | |
96 | |||
97 | 58077 | dst = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size]; | |
98 | |||
99 | 58077 | memcpy(dst, src, i); | |
100 | 58077 | si = di = i; | |
101 |
2/2✓ Branch 0 taken 9826156 times.
✓ Branch 1 taken 52151 times.
|
9878307 | while (si + 2 < length) { |
102 | // remove escapes (very rare 1:2^22) | ||
103 |
2/2✓ Branch 0 taken 9543220 times.
✓ Branch 1 taken 282936 times.
|
9826156 | if (src[si + 2] > 3) { |
104 | 9543220 | dst[di++] = src[si++]; | |
105 | 9543220 | dst[di++] = src[si++]; | |
106 |
6/6✓ Branch 0 taken 49226 times.
✓ Branch 1 taken 233710 times.
✓ Branch 2 taken 38293 times.
✓ Branch 3 taken 10933 times.
✓ Branch 4 taken 32317 times.
✓ Branch 5 taken 5976 times.
|
282936 | } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) { |
107 |
2/2✓ Branch 0 taken 26391 times.
✓ Branch 1 taken 5926 times.
|
32317 | if (src[si + 2] == 3) { // escape |
108 | 26391 | dst[di++] = 0; | |
109 | 26391 | dst[di++] = 0; | |
110 | 26391 | si += 3; | |
111 | |||
112 |
2/2✓ Branch 0 taken 22195 times.
✓ Branch 1 taken 4196 times.
|
26391 | if (nal->skipped_bytes_pos) { |
113 | 22195 | nal->skipped_bytes++; | |
114 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 22173 times.
|
22195 | if (nal->skipped_bytes_pos_size < nal->skipped_bytes) { |
115 | 22 | nal->skipped_bytes_pos_size *= 2; | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes); |
117 | 22 | av_reallocp_array(&nal->skipped_bytes_pos, | |
118 | 22 | nal->skipped_bytes_pos_size, | |
119 | sizeof(*nal->skipped_bytes_pos)); | ||
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
|
22 | if (!nal->skipped_bytes_pos) { |
121 | ✗ | nal->skipped_bytes_pos_size = 0; | |
122 | ✗ | return AVERROR(ENOMEM); | |
123 | } | ||
124 | } | ||
125 |
1/2✓ Branch 0 taken 22195 times.
✗ Branch 1 not taken.
|
22195 | if (nal->skipped_bytes_pos) |
126 | 22195 | nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1; | |
127 | } | ||
128 | 26391 | continue; | |
129 | } else // next start code | ||
130 | 5926 | goto nsc; | |
131 | } | ||
132 | |||
133 | 9793839 | dst[di++] = src[si++]; | |
134 | } | ||
135 |
2/2✓ Branch 0 taken 5412 times.
✓ Branch 1 taken 52151 times.
|
57563 | while (si < length) |
136 | 5412 | dst[di++] = src[si++]; | |
137 | |||
138 | 52151 | nsc: | |
139 | 58077 | memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
140 | |||
141 | 58077 | nal->data = dst; | |
142 | 58077 | nal->size = di; | |
143 | 58077 | nal->raw_data = src; | |
144 | 58077 | nal->raw_size = si; | |
145 | 58077 | rbsp->rbsp_buffer_size += si; | |
146 | |||
147 | 58077 | return si; | |
148 | } | ||
149 | |||
150 | static const char *const vvc_nal_type_name[32] = { | ||
151 | "TRAIL_NUT", // VVC_TRAIL_NUT | ||
152 | "STSA_NUT", // VVC_STSA_NUT | ||
153 | "RADL_NUT", // VVC_RADL_NUT | ||
154 | "RASL_NUT", // VVC_RASL_NUT | ||
155 | "RSV_VCL4", // VVC_RSV_VCL_4 | ||
156 | "RSV_VCL5", // VVC_RSV_VCL_5 | ||
157 | "RSV_VCL6", // VVC_RSV_VCL_6 | ||
158 | "IDR_W_RADL", // VVC_IDR_W_RADL | ||
159 | "IDR_N_LP", // VVC_IDR_N_LP | ||
160 | "CRA_NUT", // VVC_CRA_NUT | ||
161 | "GDR_NUT", // VVC_GDR_NUT | ||
162 | "RSV_IRAP_11", // VVC_RSV_IRAP_11 | ||
163 | "OPI_NUT", // VVC_OPI_NUT | ||
164 | "DCI_NUT", // VVC_DCI_NUT | ||
165 | "VPS_NUT", // VVC_VPS_NUT | ||
166 | "SPS_NUT", // VVC_SPS_NUT | ||
167 | "PPS_NUT", // VVC_PPS_NUT | ||
168 | "APS_PREFIX", // VVC_PREFIX_APS_NUT | ||
169 | "APS_SUFFIX", // VVC_SUFFIX_APS_NUT | ||
170 | "PH_NUT", // VVC_PH_NUT | ||
171 | "AUD_NUT", // VVC_AUD_NUT | ||
172 | "EOS_NUT", // VVC_EOS_NUT | ||
173 | "EOB_NUT", // VVC_EOB_NUT | ||
174 | "SEI_PREFIX", // VVC_PREFIX_SEI_NUT | ||
175 | "SEI_SUFFIX", // VVC_SUFFIX_SEI_NUT | ||
176 | "FD_NUT", // VVC_FD_NUT | ||
177 | "RSV_NVCL26", // VVC_RSV_NVCL_26 | ||
178 | "RSV_NVCL27", // VVC_RSV_NVCL_27 | ||
179 | "UNSPEC28", // VVC_UNSPEC_28 | ||
180 | "UNSPEC29", // VVC_UNSPEC_29 | ||
181 | "UNSPEC30", // VVC_UNSPEC_30 | ||
182 | "UNSPEC31", // VVC_UNSPEC_31 | ||
183 | }; | ||
184 | |||
185 | 17471 | static const char *vvc_nal_unit_name(int nal_type) | |
186 | { | ||
187 |
2/4✓ Branch 0 taken 17471 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17471 times.
|
17471 | av_assert0(nal_type >= 0 && nal_type < 32); |
188 | 17471 | return vvc_nal_type_name[nal_type]; | |
189 | } | ||
190 | |||
191 | static const char *const hevc_nal_type_name[64] = { | ||
192 | "TRAIL_N", // HEVC_NAL_TRAIL_N | ||
193 | "TRAIL_R", // HEVC_NAL_TRAIL_R | ||
194 | "TSA_N", // HEVC_NAL_TSA_N | ||
195 | "TSA_R", // HEVC_NAL_TSA_R | ||
196 | "STSA_N", // HEVC_NAL_STSA_N | ||
197 | "STSA_R", // HEVC_NAL_STSA_R | ||
198 | "RADL_N", // HEVC_NAL_RADL_N | ||
199 | "RADL_R", // HEVC_NAL_RADL_R | ||
200 | "RASL_N", // HEVC_NAL_RASL_N | ||
201 | "RASL_R", // HEVC_NAL_RASL_R | ||
202 | "RSV_VCL_N10", // HEVC_NAL_VCL_N10 | ||
203 | "RSV_VCL_R11", // HEVC_NAL_VCL_R11 | ||
204 | "RSV_VCL_N12", // HEVC_NAL_VCL_N12 | ||
205 | "RSV_VLC_R13", // HEVC_NAL_VCL_R13 | ||
206 | "RSV_VCL_N14", // HEVC_NAL_VCL_N14 | ||
207 | "RSV_VCL_R15", // HEVC_NAL_VCL_R15 | ||
208 | "BLA_W_LP", // HEVC_NAL_BLA_W_LP | ||
209 | "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL | ||
210 | "BLA_N_LP", // HEVC_NAL_BLA_N_LP | ||
211 | "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL | ||
212 | "IDR_N_LP", // HEVC_NAL_IDR_N_LP | ||
213 | "CRA_NUT", // HEVC_NAL_CRA_NUT | ||
214 | "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22 | ||
215 | "RSV_IRAP_VCL23", // HEVC_NAL_RSV_IRAP_VCL23 | ||
216 | "RSV_VCL24", // HEVC_NAL_RSV_VCL24 | ||
217 | "RSV_VCL25", // HEVC_NAL_RSV_VCL25 | ||
218 | "RSV_VCL26", // HEVC_NAL_RSV_VCL26 | ||
219 | "RSV_VCL27", // HEVC_NAL_RSV_VCL27 | ||
220 | "RSV_VCL28", // HEVC_NAL_RSV_VCL28 | ||
221 | "RSV_VCL29", // HEVC_NAL_RSV_VCL29 | ||
222 | "RSV_VCL30", // HEVC_NAL_RSV_VCL30 | ||
223 | "RSV_VCL31", // HEVC_NAL_RSV_VCL31 | ||
224 | "VPS", // HEVC_NAL_VPS | ||
225 | "SPS", // HEVC_NAL_SPS | ||
226 | "PPS", // HEVC_NAL_PPS | ||
227 | "AUD", // HEVC_NAL_AUD | ||
228 | "EOS_NUT", // HEVC_NAL_EOS_NUT | ||
229 | "EOB_NUT", // HEVC_NAL_EOB_NUT | ||
230 | "FD_NUT", // HEVC_NAL_FD_NUT | ||
231 | "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX | ||
232 | "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX | ||
233 | "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41 | ||
234 | "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42 | ||
235 | "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43 | ||
236 | "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44 | ||
237 | "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45 | ||
238 | "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46 | ||
239 | "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47 | ||
240 | "UNSPEC48", // HEVC_NAL_UNSPEC48 | ||
241 | "UNSPEC49", // HEVC_NAL_UNSPEC49 | ||
242 | "UNSPEC50", // HEVC_NAL_UNSPEC50 | ||
243 | "UNSPEC51", // HEVC_NAL_UNSPEC51 | ||
244 | "UNSPEC52", // HEVC_NAL_UNSPEC52 | ||
245 | "UNSPEC53", // HEVC_NAL_UNSPEC53 | ||
246 | "UNSPEC54", // HEVC_NAL_UNSPEC54 | ||
247 | "UNSPEC55", // HEVC_NAL_UNSPEC55 | ||
248 | "UNSPEC56", // HEVC_NAL_UNSPEC56 | ||
249 | "UNSPEC57", // HEVC_NAL_UNSPEC57 | ||
250 | "UNSPEC58", // HEVC_NAL_UNSPEC58 | ||
251 | "UNSPEC59", // HEVC_NAL_UNSPEC59 | ||
252 | "UNSPEC60", // HEVC_NAL_UNSPEC60 | ||
253 | "UNSPEC61", // HEVC_NAL_UNSPEC61 | ||
254 | "UNSPEC62", // HEVC_NAL_UNSPEC62 | ||
255 | "UNSPEC63", // HEVC_NAL_UNSPEC63 | ||
256 | }; | ||
257 | |||
258 | 97152 | static const char *hevc_nal_unit_name(int nal_type) | |
259 | { | ||
260 |
2/4✓ Branch 0 taken 97152 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 97152 times.
|
97152 | av_assert0(nal_type >= 0 && nal_type < 64); |
261 | 97152 | return hevc_nal_type_name[nal_type]; | |
262 | } | ||
263 | |||
264 | static const char *const h264_nal_type_name[32] = { | ||
265 | "Unspecified 0", //H264_NAL_UNSPECIFIED | ||
266 | "Coded slice of a non-IDR picture", // H264_NAL_SLICE | ||
267 | "Coded slice data partition A", // H264_NAL_DPA | ||
268 | "Coded slice data partition B", // H264_NAL_DPB | ||
269 | "Coded slice data partition C", // H264_NAL_DPC | ||
270 | "IDR", // H264_NAL_IDR_SLICE | ||
271 | "SEI", // H264_NAL_SEI | ||
272 | "SPS", // H264_NAL_SPS | ||
273 | "PPS", // H264_NAL_PPS | ||
274 | "AUD", // H264_NAL_AUD | ||
275 | "End of sequence", // H264_NAL_END_SEQUENCE | ||
276 | "End of stream", // H264_NAL_END_STREAM | ||
277 | "Filler data", // H264_NAL_FILLER_DATA | ||
278 | "SPS extension", // H264_NAL_SPS_EXT | ||
279 | "Prefix", // H264_NAL_PREFIX | ||
280 | "Subset SPS", // H264_NAL_SUB_SPS | ||
281 | "Depth parameter set", // H264_NAL_DPS | ||
282 | "Reserved 17", // H264_NAL_RESERVED17 | ||
283 | "Reserved 18", // H264_NAL_RESERVED18 | ||
284 | "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE | ||
285 | "Slice extension", // H264_NAL_EXTEN_SLICE | ||
286 | "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE | ||
287 | "Reserved 22", // H264_NAL_RESERVED22 | ||
288 | "Reserved 23", // H264_NAL_RESERVED23 | ||
289 | "Unspecified 24", // H264_NAL_UNSPECIFIED24 | ||
290 | "Unspecified 25", // H264_NAL_UNSPECIFIED25 | ||
291 | "Unspecified 26", // H264_NAL_UNSPECIFIED26 | ||
292 | "Unspecified 27", // H264_NAL_UNSPECIFIED27 | ||
293 | "Unspecified 28", // H264_NAL_UNSPECIFIED28 | ||
294 | "Unspecified 29", // H264_NAL_UNSPECIFIED29 | ||
295 | "Unspecified 30", // H264_NAL_UNSPECIFIED30 | ||
296 | "Unspecified 31", // H264_NAL_UNSPECIFIED31 | ||
297 | }; | ||
298 | |||
299 | 63131 | static const char *h264_nal_unit_name(int nal_type) | |
300 | { | ||
301 |
2/4✓ Branch 0 taken 63131 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 63131 times.
|
63131 | av_assert0(nal_type >= 0 && nal_type < 32); |
302 | 63131 | return h264_nal_type_name[nal_type]; | |
303 | } | ||
304 | |||
305 | 177756 | static int get_bit_length(H2645NAL *nal, int min_size, int skip_trailing_zeros) | |
306 | { | ||
307 | 177756 | int size = nal->size; | |
308 | 177756 | int trailing_padding = 0; | |
309 | |||
310 |
5/6✓ Branch 0 taken 222695 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 222695 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44943 times.
✓ Branch 5 taken 177752 times.
|
222699 | while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0) |
311 | 44943 | size--; | |
312 | |||
313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 177756 times.
|
177756 | if (!size) |
314 | ✗ | return 0; | |
315 | |||
316 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 177693 times.
|
177756 | if (size <= min_size) { |
317 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
|
63 | if (nal->size < min_size) |
318 | ✗ | return AVERROR_INVALIDDATA; | |
319 | 63 | size = min_size; | |
320 | } else { | ||
321 | 177693 | int v = nal->data[size - 1]; | |
322 | /* remove the stop bit and following trailing zeros, | ||
323 | * or nothing for damaged bitstreams */ | ||
324 |
1/2✓ Branch 0 taken 177693 times.
✗ Branch 1 not taken.
|
177693 | if (v) |
325 | 177693 | trailing_padding = ff_ctz(v) + 1; | |
326 | } | ||
327 | |||
328 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 177756 times.
|
177756 | if (size > INT_MAX / 8) |
329 | ✗ | return AVERROR(ERANGE); | |
330 | 177756 | size *= 8; | |
331 | |||
332 | 177756 | return size - trailing_padding; | |
333 | } | ||
334 | |||
335 | /** | ||
336 | * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit, | ||
337 | * 0 otherwise | ||
338 | */ | ||
339 | 17471 | static int vvc_parse_nal_header(H2645NAL *nal, void *logctx) | |
340 | { | ||
341 | 17471 | GetBitContext *gb = &nal->gb; | |
342 | |||
343 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 17471 times.
|
17471 | if (get_bits1(gb) != 0) //forbidden_zero_bit |
344 | ✗ | return AVERROR_INVALIDDATA; | |
345 | |||
346 | 17471 | skip_bits1(gb); //nuh_reserved_zero_bit | |
347 | |||
348 | 17471 | nal->nuh_layer_id = get_bits(gb, 6); | |
349 | 17471 | nal->type = get_bits(gb, 5); | |
350 | 17471 | nal->temporal_id = get_bits(gb, 3) - 1; | |
351 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 17471 times.
|
17471 | if (nal->temporal_id < 0) |
352 | ✗ | return AVERROR_INVALIDDATA; | |
353 | |||
354 |
5/6✓ Branch 0 taken 10839 times.
✓ Branch 1 taken 6632 times.
✓ Branch 2 taken 1287 times.
✓ Branch 3 taken 9552 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1287 times.
|
17471 | if ((nal->type >= VVC_IDR_W_RADL && nal->type <= VVC_RSV_IRAP_11) && nal->temporal_id) |
355 | ✗ | return AVERROR_INVALIDDATA; | |
356 | |||
357 | 17471 | av_log(logctx, AV_LOG_DEBUG, | |
358 | "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n", | ||
359 | nal->type, vvc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id); | ||
360 | |||
361 | 17471 | return 0; | |
362 | } | ||
363 | |||
364 | 97154 | static int hevc_parse_nal_header(H2645NAL *nal, void *logctx) | |
365 | { | ||
366 | 97154 | GetBitContext *gb = &nal->gb; | |
367 | |||
368 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 97152 times.
|
97154 | if (get_bits1(gb) != 0) |
369 | 2 | return AVERROR_INVALIDDATA; | |
370 | |||
371 | 97152 | nal->type = get_bits(gb, 6); | |
372 | |||
373 | 97152 | nal->nuh_layer_id = get_bits(gb, 6); | |
374 | 97152 | nal->temporal_id = get_bits(gb, 3) - 1; | |
375 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 97152 times.
|
97152 | if (nal->temporal_id < 0) |
376 | ✗ | return AVERROR_INVALIDDATA; | |
377 | |||
378 | 97152 | av_log(logctx, AV_LOG_DEBUG, | |
379 | "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n", | ||
380 | nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id); | ||
381 | |||
382 | 97152 | return 0; | |
383 | } | ||
384 | |||
385 | 63131 | static int h264_parse_nal_header(H2645NAL *nal, void *logctx) | |
386 | { | ||
387 | 63131 | GetBitContext *gb = &nal->gb; | |
388 | |||
389 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 63131 times.
|
63131 | if (get_bits1(gb) != 0) |
390 | ✗ | return AVERROR_INVALIDDATA; | |
391 | |||
392 | 63131 | nal->ref_idc = get_bits(gb, 2); | |
393 | 63131 | nal->type = get_bits(gb, 5); | |
394 | |||
395 | 63131 | av_log(logctx, AV_LOG_DEBUG, | |
396 | "nal_unit_type: %d(%s), nal_ref_idc: %d\n", | ||
397 | nal->type, h264_nal_unit_name(nal->type), nal->ref_idc); | ||
398 | |||
399 | 63131 | return 0; | |
400 | } | ||
401 | |||
402 | 168630 | static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc) | |
403 | { | ||
404 | 168630 | int i = 0; | |
405 | |||
406 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 168630 times.
|
168630 | if (buf + 3 >= next_avc) |
407 | ✗ | return next_avc - buf; | |
408 | |||
409 |
1/2✓ Branch 0 taken 225617 times.
✗ Branch 1 not taken.
|
225617 | while (buf + i + 3 < next_avc) { |
410 |
4/6✓ Branch 0 taken 225617 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 225617 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 168630 times.
✓ Branch 5 taken 56987 times.
|
225617 | if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) |
411 | 168630 | break; | |
412 | 56987 | i++; | |
413 | } | ||
414 | 168630 | return i + 3; | |
415 | } | ||
416 | |||
417 | 64334 | static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref) | |
418 | { | ||
419 | 64334 | int min_size = size; | |
420 | |||
421 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64334 times.
|
64334 | if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
422 | ✗ | goto fail; | |
423 | 64334 | size += AV_INPUT_BUFFER_PADDING_SIZE; | |
424 | |||
425 |
2/2✓ Branch 0 taken 59992 times.
✓ Branch 1 taken 4342 times.
|
64334 | if (rbsp->rbsp_buffer_alloc_size >= size && |
426 |
4/4✓ Branch 0 taken 10604 times.
✓ Branch 1 taken 49388 times.
✓ Branch 3 taken 10014 times.
✓ Branch 4 taken 590 times.
|
59992 | (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) { |
427 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 59402 times.
|
59402 | av_assert0(rbsp->rbsp_buffer); |
428 | 59402 | memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
429 | 59402 | return; | |
430 | } | ||
431 | |||
432 |
1/2✓ Branch 0 taken 4932 times.
✗ Branch 1 not taken.
|
4932 | size = FFMIN(size + size / 16 + 32, INT_MAX); |
433 | |||
434 |
2/2✓ Branch 0 taken 1537 times.
✓ Branch 1 taken 3395 times.
|
4932 | if (rbsp->rbsp_buffer_ref) |
435 | 1537 | av_buffer_unref(&rbsp->rbsp_buffer_ref); | |
436 | else | ||
437 | 3395 | av_free(rbsp->rbsp_buffer); | |
438 | |||
439 | 4932 | rbsp->rbsp_buffer = av_mallocz(size); | |
440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4932 times.
|
4932 | if (!rbsp->rbsp_buffer) |
441 | ✗ | goto fail; | |
442 | 4932 | rbsp->rbsp_buffer_alloc_size = size; | |
443 | |||
444 |
2/2✓ Branch 0 taken 1712 times.
✓ Branch 1 taken 3220 times.
|
4932 | if (use_ref) { |
445 | 1712 | rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size, | |
446 | NULL, NULL, 0); | ||
447 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1712 times.
|
1712 | if (!rbsp->rbsp_buffer_ref) |
448 | ✗ | goto fail; | |
449 | } | ||
450 | |||
451 | 4932 | return; | |
452 | |||
453 | ✗ | fail: | |
454 | ✗ | rbsp->rbsp_buffer_alloc_size = 0; | |
455 | ✗ | if (rbsp->rbsp_buffer_ref) { | |
456 | ✗ | av_buffer_unref(&rbsp->rbsp_buffer_ref); | |
457 | ✗ | rbsp->rbsp_buffer = NULL; | |
458 | } else | ||
459 | ✗ | av_freep(&rbsp->rbsp_buffer); | |
460 | |||
461 | ✗ | return; | |
462 | } | ||
463 | |||
464 | 64334 | int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, | |
465 | void *logctx, int is_nalff, int nal_length_size, | ||
466 | enum AVCodecID codec_id, int small_padding, int use_ref) | ||
467 | { | ||
468 | GetByteContext bc; | ||
469 | 64334 | int consumed, ret = 0; | |
470 |
2/2✓ Branch 0 taken 59384 times.
✓ Branch 1 taken 4950 times.
|
64334 | int next_avc = is_nalff ? 0 : length; |
471 |
2/2✓ Branch 0 taken 35191 times.
✓ Branch 1 taken 29143 times.
|
64334 | int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE; |
472 | |||
473 | 64334 | bytestream2_init(&bc, buf, length); | |
474 | 64334 | alloc_rbsp_buffer(&pkt->rbsp, length + padding, use_ref); | |
475 | |||
476 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64334 times.
|
64334 | if (!pkt->rbsp.rbsp_buffer) |
477 | ✗ | return AVERROR(ENOMEM); | |
478 | |||
479 | 64334 | pkt->rbsp.rbsp_buffer_size = 0; | |
480 | 64334 | pkt->nb_nals = 0; | |
481 |
2/2✓ Branch 1 taken 177757 times.
✓ Branch 2 taken 64333 times.
|
242090 | while (bytestream2_get_bytes_left(&bc) >= 4) { |
482 | H2645NAL *nal; | ||
483 | 177757 | int extract_length = 0; | |
484 | 177757 | int skip_trailing_zeros = 1; | |
485 | |||
486 |
2/2✓ Branch 1 taken 9127 times.
✓ Branch 2 taken 168630 times.
|
177757 | if (bytestream2_tell(&bc) == next_avc) { |
487 | 9127 | int i = 0; | |
488 | 9127 | extract_length = get_nalsize(nal_length_size, | |
489 | bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx); | ||
490 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9126 times.
|
9127 | if (extract_length < 0) |
491 | 1 | return extract_length; | |
492 | |||
493 | 9126 | bytestream2_skip(&bc, nal_length_size); | |
494 | |||
495 | 9126 | next_avc = bytestream2_tell(&bc) + extract_length; | |
496 | } else { | ||
497 | int buf_index; | ||
498 | |||
499 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 168630 times.
|
168630 | if (bytestream2_tell(&bc) > next_avc) |
500 | ✗ | av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n"); | |
501 | |||
502 | /* search start code */ | ||
503 | 168630 | buf_index = find_next_start_code(bc.buffer, buf + next_avc); | |
504 | |||
505 | 168630 | bytestream2_skip(&bc, buf_index); | |
506 | |||
507 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 168630 times.
|
168630 | if (!bytestream2_get_bytes_left(&bc)) { |
508 | ✗ | if (pkt->nb_nals > 0) { | |
509 | // No more start codes: we discarded some irrelevant | ||
510 | // bytes at the end of the packet. | ||
511 | ✗ | return 0; | |
512 | } else { | ||
513 | ✗ | av_log(logctx, AV_LOG_ERROR, "No start code is found.\n"); | |
514 | ✗ | return AVERROR_INVALIDDATA; | |
515 | } | ||
516 | } | ||
517 | |||
518 |
2/2✓ Branch 2 taken 6 times.
✓ Branch 3 taken 168624 times.
|
168630 | extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc)); |
519 | |||
520 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 168630 times.
|
168630 | if (bytestream2_tell(&bc) >= next_avc) { |
521 | /* skip to the start of the next NAL */ | ||
522 | ✗ | bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc)); | |
523 | ✗ | continue; | |
524 | } | ||
525 | } | ||
526 | |||
527 |
2/2✓ Branch 0 taken 13240 times.
✓ Branch 1 taken 164516 times.
|
177756 | if (pkt->nals_allocated < pkt->nb_nals + 1) { |
528 | 13240 | int new_size = pkt->nals_allocated + 1; | |
529 | void *tmp; | ||
530 | |||
531 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13240 times.
|
13240 | if (new_size >= INT_MAX / sizeof(*pkt->nals)) |
532 | ✗ | return AVERROR(ENOMEM); | |
533 | |||
534 | 13240 | tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals)); | |
535 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13240 times.
|
13240 | if (!tmp) |
536 | ✗ | return AVERROR(ENOMEM); | |
537 | |||
538 | 13240 | pkt->nals = tmp; | |
539 | 13240 | memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals)); | |
540 | |||
541 | 13240 | nal = &pkt->nals[pkt->nb_nals]; | |
542 |
2/2✓ Branch 0 taken 5610 times.
✓ Branch 1 taken 7630 times.
|
13240 | nal->skipped_bytes_pos_size = FFMIN(1024, extract_length/3+1); // initial buffer size |
543 | 13240 | nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); | |
544 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13240 times.
|
13240 | if (!nal->skipped_bytes_pos) |
545 | ✗ | return AVERROR(ENOMEM); | |
546 | |||
547 | 13240 | pkt->nals_allocated = new_size; | |
548 | } | ||
549 | 177756 | nal = &pkt->nals[pkt->nb_nals]; | |
550 | |||
551 | 177756 | consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding); | |
552 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 177756 times.
|
177756 | if (consumed < 0) |
553 | ✗ | return consumed; | |
554 | |||
555 |
5/6✓ Branch 0 taken 9138 times.
✓ Branch 1 taken 168618 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 9126 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
177756 | if (is_nalff && (extract_length != consumed) && extract_length) |
556 | 12 | av_log(logctx, AV_LOG_DEBUG, | |
557 | "NALFF: Consumed only %d bytes instead of %d\n", | ||
558 | consumed, extract_length); | ||
559 | |||
560 | 177756 | bytestream2_skip(&bc, consumed); | |
561 | |||
562 | /* see commit 3566042a0 */ | ||
563 |
4/4✓ Branch 1 taken 113425 times.
✓ Branch 2 taken 64331 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 113421 times.
|
291181 | if (bytestream2_get_bytes_left(&bc) >= 4 && |
564 | 113425 | bytestream2_peek_be32(&bc) == 0x000001E0) | |
565 | 4 | skip_trailing_zeros = 0; | |
566 | |||
567 |
2/2✓ Branch 0 taken 97154 times.
✓ Branch 1 taken 80602 times.
|
177756 | nal->size_bits = get_bit_length(nal, 1 + (codec_id == AV_CODEC_ID_HEVC), |
568 | skip_trailing_zeros); | ||
569 | |||
570 |
2/4✓ Branch 0 taken 177756 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 177756 times.
|
177756 | if (nal->size <= 0 || nal->size_bits <= 0) |
571 | ✗ | continue; | |
572 | |||
573 | 177756 | ret = init_get_bits(&nal->gb, nal->data, nal->size_bits); | |
574 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 177756 times.
|
177756 | if (ret < 0) |
575 | ✗ | return ret; | |
576 | |||
577 | /* Reset type in case it contains a stale value from a previously parsed NAL */ | ||
578 | 177756 | nal->type = 0; | |
579 | |||
580 |
2/2✓ Branch 0 taken 17471 times.
✓ Branch 1 taken 160285 times.
|
177756 | if (codec_id == AV_CODEC_ID_VVC) |
581 | 17471 | ret = vvc_parse_nal_header(nal, logctx); | |
582 |
2/2✓ Branch 0 taken 97154 times.
✓ Branch 1 taken 63131 times.
|
160285 | else if (codec_id == AV_CODEC_ID_HEVC) |
583 | 97154 | ret = hevc_parse_nal_header(nal, logctx); | |
584 | else | ||
585 | 63131 | ret = h264_parse_nal_header(nal, logctx); | |
586 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 177754 times.
|
177756 | if (ret < 0) { |
587 | 2 | av_log(logctx, AV_LOG_WARNING, "Invalid NAL unit %d, skipping.\n", | |
588 | nal->type); | ||
589 | 2 | continue; | |
590 | } | ||
591 | |||
592 | 177754 | pkt->nb_nals++; | |
593 | } | ||
594 | |||
595 | 64333 | return 0; | |
596 | } | ||
597 | |||
598 | 3765 | void ff_h2645_packet_uninit(H2645Packet *pkt) | |
599 | { | ||
600 | int i; | ||
601 |
2/2✓ Branch 0 taken 13240 times.
✓ Branch 1 taken 3765 times.
|
17005 | for (i = 0; i < pkt->nals_allocated; i++) { |
602 | 13240 | av_freep(&pkt->nals[i].skipped_bytes_pos); | |
603 | } | ||
604 | 3765 | av_freep(&pkt->nals); | |
605 | 3765 | pkt->nals_allocated = pkt->nal_buffer_size = 0; | |
606 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 3590 times.
|
3765 | if (pkt->rbsp.rbsp_buffer_ref) { |
607 | 175 | av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref); | |
608 | 175 | pkt->rbsp.rbsp_buffer = NULL; | |
609 | } else | ||
610 | 3590 | av_freep(&pkt->rbsp.rbsp_buffer); | |
611 | 3765 | pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0; | |
612 | 3765 | } | |
613 |