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 | |||
34 | 199687 | int ff_h2645_extract_rbsp(const uint8_t *src, int length, | |
35 | H2645RBSP *rbsp, H2645NAL *nal, int small_padding) | ||
36 | { | ||
37 | int i, si, di; | ||
38 | uint8_t *dst; | ||
39 | |||
40 | 199687 | nal->skipped_bytes = 0; | |
41 | #define STARTCODE_TEST \ | ||
42 | if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \ | ||
43 | if (src[i + 2] != 3 && src[i + 2] != 0) { \ | ||
44 | /* startcode, so we must be past the end */ \ | ||
45 | length = i; \ | ||
46 | } \ | ||
47 | break; \ | ||
48 | } | ||
49 | #if HAVE_FAST_UNALIGNED | ||
50 | #define FIND_FIRST_ZERO \ | ||
51 | if (i > 0 && !src[i]) \ | ||
52 | i--; \ | ||
53 | while (src[i]) \ | ||
54 | i++ | ||
55 | #if HAVE_FAST_64BIT | ||
56 |
2/2✓ Branch 0 taken 47232949 times.
✓ Branch 1 taken 88856 times.
|
47321805 | for (i = 0; i + 1 < length; i += 9) { |
57 | 93123152 | if (!((~AV_RN64(src + i) & | |
58 |
2/2✓ Branch 0 taken 45890203 times.
✓ Branch 1 taken 1342746 times.
|
47232949 | (AV_RN64(src + i) - 0x0100010001000101ULL)) & |
59 | 0x8000800080008080ULL)) | ||
60 | 45890203 | continue; | |
61 |
6/6✓ Branch 0 taken 1280325 times.
✓ Branch 1 taken 62421 times.
✓ Branch 2 taken 257853 times.
✓ Branch 3 taken 1022472 times.
✓ Branch 4 taken 4358942 times.
✓ Branch 5 taken 1342746 times.
|
5701688 | FIND_FIRST_ZERO; |
62 |
10/10✓ Branch 0 taken 1304153 times.
✓ Branch 1 taken 38593 times.
✓ Branch 2 taken 137255 times.
✓ Branch 3 taken 1166898 times.
✓ Branch 4 taken 110831 times.
✓ Branch 5 taken 26424 times.
✓ Branch 6 taken 103929 times.
✓ Branch 7 taken 6902 times.
✓ Branch 8 taken 61891 times.
✓ Branch 9 taken 42038 times.
|
1342746 | STARTCODE_TEST; |
63 | 1231915 | i -= 7; | |
64 | } | ||
65 | #else | ||
66 | for (i = 0; i + 1 < length; i += 5) { | ||
67 | if (!((~AV_RN32(src + i) & | ||
68 | (AV_RN32(src + i) - 0x01000101U)) & | ||
69 | 0x80008080U)) | ||
70 | continue; | ||
71 | FIND_FIRST_ZERO; | ||
72 | STARTCODE_TEST; | ||
73 | i -= 3; | ||
74 | } | ||
75 | #endif /* HAVE_FAST_64BIT */ | ||
76 | #else | ||
77 | for (i = 0; i + 1 < length; i += 2) { | ||
78 | if (src[i]) | ||
79 | continue; | ||
80 | if (i > 0 && src[i - 1] == 0) | ||
81 | i--; | ||
82 | STARTCODE_TEST; | ||
83 | } | ||
84 | #endif /* HAVE_FAST_UNALIGNED */ | ||
85 | |||
86 |
4/4✓ Branch 0 taken 150747 times.
✓ Branch 1 taken 48940 times.
✓ Branch 2 taken 118554 times.
✓ Branch 3 taken 32193 times.
|
199687 | if (i >= length - 1 && small_padding) { // no escaped 0 |
87 | 118554 | nal->data = | |
88 | 118554 | nal->raw_data = src; | |
89 | 118554 | nal->size = | |
90 | 118554 | nal->raw_size = length; | |
91 | 118554 | return length; | |
92 |
2/2✓ Branch 0 taken 22581 times.
✓ Branch 1 taken 58552 times.
|
81133 | } else if (i > length) |
93 | 22581 | i = length; | |
94 | |||
95 | 81133 | dst = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size]; | |
96 | |||
97 | 81133 | memcpy(dst, src, i); | |
98 | 81133 | si = di = i; | |
99 |
2/2✓ Branch 0 taken 9823263 times.
✓ Branch 1 taken 34451 times.
|
9857714 | while (si + 2 < length) { |
100 | // remove escapes (very rare 1:2^22) | ||
101 |
2/2✓ Branch 0 taken 9457668 times.
✓ Branch 1 taken 365595 times.
|
9823263 | if (src[si + 2] > 3) { |
102 | 9457668 | dst[di++] = src[si++]; | |
103 | 9457668 | dst[di++] = src[si++]; | |
104 |
6/6✓ Branch 0 taken 135258 times.
✓ Branch 1 taken 230337 times.
✓ Branch 2 taken 124804 times.
✓ Branch 3 taken 10454 times.
✓ Branch 4 taken 70256 times.
✓ Branch 5 taken 54548 times.
|
365595 | } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) { |
105 |
2/2✓ Branch 0 taken 23574 times.
✓ Branch 1 taken 46682 times.
|
70256 | if (src[si + 2] == 3) { // escape |
106 | 23574 | dst[di++] = 0; | |
107 | 23574 | dst[di++] = 0; | |
108 | 23574 | si += 3; | |
109 | |||
110 |
2/2✓ Branch 0 taken 19563 times.
✓ Branch 1 taken 4011 times.
|
23574 | if (nal->skipped_bytes_pos) { |
111 | 19563 | nal->skipped_bytes++; | |
112 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 19543 times.
|
19563 | if (nal->skipped_bytes_pos_size < nal->skipped_bytes) { |
113 | 20 | nal->skipped_bytes_pos_size *= 2; | |
114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes); |
115 | 20 | av_reallocp_array(&nal->skipped_bytes_pos, | |
116 | 20 | nal->skipped_bytes_pos_size, | |
117 | sizeof(*nal->skipped_bytes_pos)); | ||
118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (!nal->skipped_bytes_pos) { |
119 | ✗ | nal->skipped_bytes_pos_size = 0; | |
120 | ✗ | return AVERROR(ENOMEM); | |
121 | } | ||
122 | } | ||
123 |
1/2✓ Branch 0 taken 19563 times.
✗ Branch 1 not taken.
|
19563 | if (nal->skipped_bytes_pos) |
124 | 19563 | nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1; | |
125 | } | ||
126 | 23574 | continue; | |
127 | } else // next start code | ||
128 | 46682 | goto nsc; | |
129 | } | ||
130 | |||
131 | 9753007 | dst[di++] = src[si++]; | |
132 | } | ||
133 |
2/2✓ Branch 0 taken 5189 times.
✓ Branch 1 taken 34451 times.
|
39640 | while (si < length) |
134 | 5189 | dst[di++] = src[si++]; | |
135 | |||
136 | 34451 | nsc: | |
137 | 81133 | memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
138 | |||
139 | 81133 | nal->data = dst; | |
140 | 81133 | nal->size = di; | |
141 | 81133 | nal->raw_data = src; | |
142 | 81133 | nal->raw_size = si; | |
143 | 81133 | rbsp->rbsp_buffer_size += si; | |
144 | |||
145 | 81133 | return si; | |
146 | } | ||
147 | |||
148 | static const char *const hevc_nal_type_name[64] = { | ||
149 | "TRAIL_N", // HEVC_NAL_TRAIL_N | ||
150 | "TRAIL_R", // HEVC_NAL_TRAIL_R | ||
151 | "TSA_N", // HEVC_NAL_TSA_N | ||
152 | "TSA_R", // HEVC_NAL_TSA_R | ||
153 | "STSA_N", // HEVC_NAL_STSA_N | ||
154 | "STSA_R", // HEVC_NAL_STSA_R | ||
155 | "RADL_N", // HEVC_NAL_RADL_N | ||
156 | "RADL_R", // HEVC_NAL_RADL_R | ||
157 | "RASL_N", // HEVC_NAL_RASL_N | ||
158 | "RASL_R", // HEVC_NAL_RASL_R | ||
159 | "RSV_VCL_N10", // HEVC_NAL_VCL_N10 | ||
160 | "RSV_VCL_R11", // HEVC_NAL_VCL_R11 | ||
161 | "RSV_VCL_N12", // HEVC_NAL_VCL_N12 | ||
162 | "RSV_VLC_R13", // HEVC_NAL_VCL_R13 | ||
163 | "RSV_VCL_N14", // HEVC_NAL_VCL_N14 | ||
164 | "RSV_VCL_R15", // HEVC_NAL_VCL_R15 | ||
165 | "BLA_W_LP", // HEVC_NAL_BLA_W_LP | ||
166 | "BLA_W_RADL", // HEVC_NAL_BLA_W_RADL | ||
167 | "BLA_N_LP", // HEVC_NAL_BLA_N_LP | ||
168 | "IDR_W_RADL", // HEVC_NAL_IDR_W_RADL | ||
169 | "IDR_N_LP", // HEVC_NAL_IDR_N_LP | ||
170 | "CRA_NUT", // HEVC_NAL_CRA_NUT | ||
171 | "RSV_IRAP_VCL22", // HEVC_NAL_RSV_IRAP_VCL22 | ||
172 | "RSV_IRAP_VCL23", // HEVC_NAL_RSV_IRAP_VCL23 | ||
173 | "RSV_VCL24", // HEVC_NAL_RSV_VCL24 | ||
174 | "RSV_VCL25", // HEVC_NAL_RSV_VCL25 | ||
175 | "RSV_VCL26", // HEVC_NAL_RSV_VCL26 | ||
176 | "RSV_VCL27", // HEVC_NAL_RSV_VCL27 | ||
177 | "RSV_VCL28", // HEVC_NAL_RSV_VCL28 | ||
178 | "RSV_VCL29", // HEVC_NAL_RSV_VCL29 | ||
179 | "RSV_VCL30", // HEVC_NAL_RSV_VCL30 | ||
180 | "RSV_VCL31", // HEVC_NAL_RSV_VCL31 | ||
181 | "VPS", // HEVC_NAL_VPS | ||
182 | "SPS", // HEVC_NAL_SPS | ||
183 | "PPS", // HEVC_NAL_PPS | ||
184 | "AUD", // HEVC_NAL_AUD | ||
185 | "EOS_NUT", // HEVC_NAL_EOS_NUT | ||
186 | "EOB_NUT", // HEVC_NAL_EOB_NUT | ||
187 | "FD_NUT", // HEVC_NAL_FD_NUT | ||
188 | "SEI_PREFIX", // HEVC_NAL_SEI_PREFIX | ||
189 | "SEI_SUFFIX", // HEVC_NAL_SEI_SUFFIX | ||
190 | "RSV_NVCL41", // HEVC_NAL_RSV_NVCL41 | ||
191 | "RSV_NVCL42", // HEVC_NAL_RSV_NVCL42 | ||
192 | "RSV_NVCL43", // HEVC_NAL_RSV_NVCL43 | ||
193 | "RSV_NVCL44", // HEVC_NAL_RSV_NVCL44 | ||
194 | "RSV_NVCL45", // HEVC_NAL_RSV_NVCL45 | ||
195 | "RSV_NVCL46", // HEVC_NAL_RSV_NVCL46 | ||
196 | "RSV_NVCL47", // HEVC_NAL_RSV_NVCL47 | ||
197 | "UNSPEC48", // HEVC_NAL_UNSPEC48 | ||
198 | "UNSPEC49", // HEVC_NAL_UNSPEC49 | ||
199 | "UNSPEC50", // HEVC_NAL_UNSPEC50 | ||
200 | "UNSPEC51", // HEVC_NAL_UNSPEC51 | ||
201 | "UNSPEC52", // HEVC_NAL_UNSPEC52 | ||
202 | "UNSPEC53", // HEVC_NAL_UNSPEC53 | ||
203 | "UNSPEC54", // HEVC_NAL_UNSPEC54 | ||
204 | "UNSPEC55", // HEVC_NAL_UNSPEC55 | ||
205 | "UNSPEC56", // HEVC_NAL_UNSPEC56 | ||
206 | "UNSPEC57", // HEVC_NAL_UNSPEC57 | ||
207 | "UNSPEC58", // HEVC_NAL_UNSPEC58 | ||
208 | "UNSPEC59", // HEVC_NAL_UNSPEC59 | ||
209 | "UNSPEC60", // HEVC_NAL_UNSPEC60 | ||
210 | "UNSPEC61", // HEVC_NAL_UNSPEC61 | ||
211 | "UNSPEC62", // HEVC_NAL_UNSPEC62 | ||
212 | "UNSPEC63", // HEVC_NAL_UNSPEC63 | ||
213 | }; | ||
214 | |||
215 | 93676 | static const char *hevc_nal_unit_name(int nal_type) | |
216 | { | ||
217 |
2/4✓ Branch 0 taken 93676 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 93676 times.
|
93676 | av_assert0(nal_type >= 0 && nal_type < 64); |
218 | 93676 | return hevc_nal_type_name[nal_type]; | |
219 | } | ||
220 | |||
221 | static const char *const h264_nal_type_name[32] = { | ||
222 | "Unspecified 0", //H264_NAL_UNSPECIFIED | ||
223 | "Coded slice of a non-IDR picture", // H264_NAL_SLICE | ||
224 | "Coded slice data partition A", // H264_NAL_DPA | ||
225 | "Coded slice data partition B", // H264_NAL_DPB | ||
226 | "Coded slice data partition C", // H264_NAL_DPC | ||
227 | "IDR", // H264_NAL_IDR_SLICE | ||
228 | "SEI", // H264_NAL_SEI | ||
229 | "SPS", // H264_NAL_SPS | ||
230 | "PPS", // H264_NAL_PPS | ||
231 | "AUD", // H264_NAL_AUD | ||
232 | "End of sequence", // H264_NAL_END_SEQUENCE | ||
233 | "End of stream", // H264_NAL_END_STREAM | ||
234 | "Filler data", // H264_NAL_FILLER_DATA | ||
235 | "SPS extension", // H264_NAL_SPS_EXT | ||
236 | "Prefix", // H264_NAL_PREFIX | ||
237 | "Subset SPS", // H264_NAL_SUB_SPS | ||
238 | "Depth parameter set", // H264_NAL_DPS | ||
239 | "Reserved 17", // H264_NAL_RESERVED17 | ||
240 | "Reserved 18", // H264_NAL_RESERVED18 | ||
241 | "Auxiliary coded picture without partitioning", // H264_NAL_AUXILIARY_SLICE | ||
242 | "Slice extension", // H264_NAL_EXTEN_SLICE | ||
243 | "Slice extension for a depth view or a 3D-AVC texture view", // H264_NAL_DEPTH_EXTEN_SLICE | ||
244 | "Reserved 22", // H264_NAL_RESERVED22 | ||
245 | "Reserved 23", // H264_NAL_RESERVED23 | ||
246 | "Unspecified 24", // H264_NAL_UNSPECIFIED24 | ||
247 | "Unspecified 25", // H264_NAL_UNSPECIFIED25 | ||
248 | "Unspecified 26", // H264_NAL_UNSPECIFIED26 | ||
249 | "Unspecified 27", // H264_NAL_UNSPECIFIED27 | ||
250 | "Unspecified 28", // H264_NAL_UNSPECIFIED28 | ||
251 | "Unspecified 29", // H264_NAL_UNSPECIFIED29 | ||
252 | "Unspecified 30", // H264_NAL_UNSPECIFIED30 | ||
253 | "Unspecified 31", // H264_NAL_UNSPECIFIED31 | ||
254 | }; | ||
255 | |||
256 | 59633 | static const char *h264_nal_unit_name(int nal_type) | |
257 | { | ||
258 |
2/4✓ Branch 0 taken 59633 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 59633 times.
|
59633 | av_assert0(nal_type >= 0 && nal_type < 32); |
259 | 59633 | return h264_nal_type_name[nal_type]; | |
260 | } | ||
261 | |||
262 | 153311 | static int get_bit_length(H2645NAL *nal, int min_size, int skip_trailing_zeros) | |
263 | { | ||
264 | 153311 | int size = nal->size; | |
265 | 153311 | int trailing_padding = 0; | |
266 | |||
267 |
5/6✓ Branch 0 taken 219222 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 219222 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 65915 times.
✓ Branch 5 taken 153307 times.
|
219226 | while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0) |
268 | 65915 | size--; | |
269 | |||
270 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153311 times.
|
153311 | if (!size) |
271 | ✗ | return 0; | |
272 | |||
273 |
2/2✓ Branch 0 taken 63 times.
✓ Branch 1 taken 153248 times.
|
153311 | if (size <= min_size) { |
274 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 63 times.
|
63 | if (nal->size < min_size) |
275 | ✗ | return AVERROR_INVALIDDATA; | |
276 | 63 | size = min_size; | |
277 | } else { | ||
278 | 153248 | int v = nal->data[size - 1]; | |
279 | /* remove the stop bit and following trailing zeros, | ||
280 | * or nothing for damaged bitstreams */ | ||
281 |
1/2✓ Branch 0 taken 153248 times.
✗ Branch 1 not taken.
|
153248 | if (v) |
282 | 153248 | trailing_padding = ff_ctz(v) + 1; | |
283 | } | ||
284 | |||
285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153311 times.
|
153311 | if (size > INT_MAX / 8) |
286 | ✗ | return AVERROR(ERANGE); | |
287 | 153311 | size *= 8; | |
288 | |||
289 | 153311 | return size - trailing_padding; | |
290 | } | ||
291 | |||
292 | /** | ||
293 | * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit, | ||
294 | * 0 otherwise | ||
295 | */ | ||
296 | 93678 | static int hevc_parse_nal_header(H2645NAL *nal, void *logctx) | |
297 | { | ||
298 | 93678 | GetBitContext *gb = &nal->gb; | |
299 | |||
300 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 93676 times.
|
93678 | if (get_bits1(gb) != 0) |
301 | 2 | return AVERROR_INVALIDDATA; | |
302 | |||
303 | 93676 | nal->type = get_bits(gb, 6); | |
304 | |||
305 | 93676 | nal->nuh_layer_id = get_bits(gb, 6); | |
306 | 93676 | nal->temporal_id = get_bits(gb, 3) - 1; | |
307 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 93676 times.
|
93676 | if (nal->temporal_id < 0) |
308 | ✗ | return AVERROR_INVALIDDATA; | |
309 | |||
310 | 93676 | av_log(logctx, AV_LOG_DEBUG, | |
311 | "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n", | ||
312 | nal->type, hevc_nal_unit_name(nal->type), nal->nuh_layer_id, nal->temporal_id); | ||
313 | |||
314 | 93676 | return 0; | |
315 | } | ||
316 | |||
317 | 59633 | static int h264_parse_nal_header(H2645NAL *nal, void *logctx) | |
318 | { | ||
319 | 59633 | GetBitContext *gb = &nal->gb; | |
320 | |||
321 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 59633 times.
|
59633 | if (get_bits1(gb) != 0) |
322 | ✗ | return AVERROR_INVALIDDATA; | |
323 | |||
324 | 59633 | nal->ref_idc = get_bits(gb, 2); | |
325 | 59633 | nal->type = get_bits(gb, 5); | |
326 | |||
327 | 59633 | av_log(logctx, AV_LOG_DEBUG, | |
328 | "nal_unit_type: %d(%s), nal_ref_idc: %d\n", | ||
329 | nal->type, h264_nal_unit_name(nal->type), nal->ref_idc); | ||
330 | |||
331 | 59633 | return 0; | |
332 | } | ||
333 | |||
334 | 146782 | static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc) | |
335 | { | ||
336 | 146782 | int i = 0; | |
337 | |||
338 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 146782 times.
|
146782 | if (buf + 3 >= next_avc) |
339 | ✗ | return next_avc - buf; | |
340 | |||
341 |
1/2✓ Branch 0 taken 173883 times.
✗ Branch 1 not taken.
|
173883 | while (buf + i + 3 < next_avc) { |
342 |
4/6✓ Branch 0 taken 173883 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 173883 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 146782 times.
✓ Branch 5 taken 27101 times.
|
173883 | if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1) |
343 | 146782 | break; | |
344 | 27101 | i++; | |
345 | } | ||
346 | 146782 | return i + 3; | |
347 | } | ||
348 | |||
349 | 55647 | static void alloc_rbsp_buffer(H2645RBSP *rbsp, unsigned int size, int use_ref) | |
350 | { | ||
351 | 55647 | int min_size = size; | |
352 | |||
353 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55647 times.
|
55647 | if (size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE) |
354 | ✗ | goto fail; | |
355 | 55647 | size += AV_INPUT_BUFFER_PADDING_SIZE; | |
356 | |||
357 |
2/2✓ Branch 0 taken 52631 times.
✓ Branch 1 taken 3016 times.
|
55647 | if (rbsp->rbsp_buffer_alloc_size >= size && |
358 |
4/4✓ Branch 0 taken 4891 times.
✓ Branch 1 taken 47740 times.
✓ Branch 3 taken 4889 times.
✓ Branch 4 taken 2 times.
|
52631 | (!rbsp->rbsp_buffer_ref || av_buffer_is_writable(rbsp->rbsp_buffer_ref))) { |
359 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 52629 times.
|
52629 | av_assert0(rbsp->rbsp_buffer); |
360 | 52629 | memset(rbsp->rbsp_buffer + min_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
361 | 52629 | return; | |
362 | } | ||
363 | |||
364 |
1/2✓ Branch 0 taken 3018 times.
✗ Branch 1 not taken.
|
3018 | size = FFMIN(size + size / 16 + 32, INT_MAX); |
365 | |||
366 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 2968 times.
|
3018 | if (rbsp->rbsp_buffer_ref) |
367 | 50 | av_buffer_unref(&rbsp->rbsp_buffer_ref); | |
368 | else | ||
369 | 2968 | av_free(rbsp->rbsp_buffer); | |
370 | |||
371 | 3018 | rbsp->rbsp_buffer = av_mallocz(size); | |
372 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3018 times.
|
3018 | if (!rbsp->rbsp_buffer) |
373 | ✗ | goto fail; | |
374 | 3018 | rbsp->rbsp_buffer_alloc_size = size; | |
375 | |||
376 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 2928 times.
|
3018 | if (use_ref) { |
377 | 90 | rbsp->rbsp_buffer_ref = av_buffer_create(rbsp->rbsp_buffer, size, | |
378 | NULL, NULL, 0); | ||
379 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 90 times.
|
90 | if (!rbsp->rbsp_buffer_ref) |
380 | ✗ | goto fail; | |
381 | } | ||
382 | |||
383 | 3018 | return; | |
384 | |||
385 | ✗ | fail: | |
386 | ✗ | rbsp->rbsp_buffer_alloc_size = 0; | |
387 | ✗ | if (rbsp->rbsp_buffer_ref) { | |
388 | ✗ | av_buffer_unref(&rbsp->rbsp_buffer_ref); | |
389 | ✗ | rbsp->rbsp_buffer = NULL; | |
390 | } else | ||
391 | ✗ | av_freep(&rbsp->rbsp_buffer); | |
392 | |||
393 | ✗ | return; | |
394 | } | ||
395 | |||
396 | 55647 | int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, | |
397 | void *logctx, int is_nalff, int nal_length_size, | ||
398 | enum AVCodecID codec_id, int small_padding, int use_ref) | ||
399 | { | ||
400 | GetByteContext bc; | ||
401 | 55647 | int consumed, ret = 0; | |
402 |
2/2✓ Branch 0 taken 51612 times.
✓ Branch 1 taken 4035 times.
|
55647 | int next_avc = is_nalff ? 0 : length; |
403 |
2/2✓ Branch 0 taken 27862 times.
✓ Branch 1 taken 27785 times.
|
55647 | int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE; |
404 | |||
405 | 55647 | bytestream2_init(&bc, buf, length); | |
406 | 55647 | alloc_rbsp_buffer(&pkt->rbsp, length + padding, use_ref); | |
407 | |||
408 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 55647 times.
|
55647 | if (!pkt->rbsp.rbsp_buffer) |
409 | ✗ | return AVERROR(ENOMEM); | |
410 | |||
411 | 55647 | pkt->rbsp.rbsp_buffer_size = 0; | |
412 | 55647 | pkt->nb_nals = 0; | |
413 |
2/2✓ Branch 1 taken 153312 times.
✓ Branch 2 taken 55646 times.
|
208958 | while (bytestream2_get_bytes_left(&bc) >= 4) { |
414 | H2645NAL *nal; | ||
415 | 153312 | int extract_length = 0; | |
416 | 153312 | int skip_trailing_zeros = 1; | |
417 | |||
418 |
2/2✓ Branch 1 taken 6530 times.
✓ Branch 2 taken 146782 times.
|
153312 | if (bytestream2_tell(&bc) == next_avc) { |
419 | 6530 | int i = 0; | |
420 | 6530 | extract_length = get_nalsize(nal_length_size, | |
421 | bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx); | ||
422 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6529 times.
|
6530 | if (extract_length < 0) |
423 | 1 | return extract_length; | |
424 | |||
425 | 6529 | bytestream2_skip(&bc, nal_length_size); | |
426 | |||
427 | 6529 | next_avc = bytestream2_tell(&bc) + extract_length; | |
428 | } else { | ||
429 | int buf_index; | ||
430 | |||
431 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 146782 times.
|
146782 | if (bytestream2_tell(&bc) > next_avc) |
432 | ✗ | av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n"); | |
433 | |||
434 | /* search start code */ | ||
435 | 146782 | buf_index = find_next_start_code(bc.buffer, buf + next_avc); | |
436 | |||
437 | 146782 | bytestream2_skip(&bc, buf_index); | |
438 | |||
439 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 146782 times.
|
146782 | if (!bytestream2_get_bytes_left(&bc)) { |
440 | ✗ | if (pkt->nb_nals > 0) { | |
441 | // No more start codes: we discarded some irrelevant | ||
442 | // bytes at the end of the packet. | ||
443 | ✗ | return 0; | |
444 | } else { | ||
445 | ✗ | av_log(logctx, AV_LOG_ERROR, "No start code is found.\n"); | |
446 | ✗ | return AVERROR_INVALIDDATA; | |
447 | } | ||
448 | } | ||
449 | |||
450 |
2/2✓ Branch 2 taken 6 times.
✓ Branch 3 taken 146776 times.
|
146782 | extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc)); |
451 | |||
452 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 146782 times.
|
146782 | if (bytestream2_tell(&bc) >= next_avc) { |
453 | /* skip to the start of the next NAL */ | ||
454 | ✗ | bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc)); | |
455 | ✗ | continue; | |
456 | } | ||
457 | } | ||
458 | |||
459 |
2/2✓ Branch 0 taken 11378 times.
✓ Branch 1 taken 141933 times.
|
153311 | if (pkt->nals_allocated < pkt->nb_nals + 1) { |
460 | 11378 | int new_size = pkt->nals_allocated + 1; | |
461 | void *tmp; | ||
462 | |||
463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11378 times.
|
11378 | if (new_size >= INT_MAX / sizeof(*pkt->nals)) |
464 | ✗ | return AVERROR(ENOMEM); | |
465 | |||
466 | 11378 | tmp = av_fast_realloc(pkt->nals, &pkt->nal_buffer_size, new_size * sizeof(*pkt->nals)); | |
467 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11378 times.
|
11378 | if (!tmp) |
468 | ✗ | return AVERROR(ENOMEM); | |
469 | |||
470 | 11378 | pkt->nals = tmp; | |
471 | 11378 | memset(pkt->nals + pkt->nals_allocated, 0, sizeof(*pkt->nals)); | |
472 | |||
473 | 11378 | nal = &pkt->nals[pkt->nb_nals]; | |
474 |
2/2✓ Branch 0 taken 4659 times.
✓ Branch 1 taken 6719 times.
|
11378 | nal->skipped_bytes_pos_size = FFMIN(1024, extract_length/3+1); // initial buffer size |
475 | 11378 | nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); | |
476 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11378 times.
|
11378 | if (!nal->skipped_bytes_pos) |
477 | ✗ | return AVERROR(ENOMEM); | |
478 | |||
479 | 11378 | pkt->nals_allocated = new_size; | |
480 | } | ||
481 | 153311 | nal = &pkt->nals[pkt->nb_nals]; | |
482 | |||
483 | 153311 | consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding); | |
484 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153311 times.
|
153311 | if (consumed < 0) |
485 | ✗ | return consumed; | |
486 | |||
487 |
5/6✓ Branch 0 taken 6541 times.
✓ Branch 1 taken 146770 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 6529 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
|
153311 | if (is_nalff && (extract_length != consumed) && extract_length) |
488 | 12 | av_log(logctx, AV_LOG_DEBUG, | |
489 | "NALFF: Consumed only %d bytes instead of %d\n", | ||
490 | consumed, extract_length); | ||
491 | |||
492 | 153311 | bytestream2_skip(&bc, consumed); | |
493 | |||
494 | /* see commit 3566042a0 */ | ||
495 |
4/4✓ Branch 1 taken 97667 times.
✓ Branch 2 taken 55644 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 97663 times.
|
250978 | if (bytestream2_get_bytes_left(&bc) >= 4 && |
496 | 97667 | bytestream2_peek_be32(&bc) == 0x000001E0) | |
497 | 4 | skip_trailing_zeros = 0; | |
498 | |||
499 |
2/2✓ Branch 0 taken 93678 times.
✓ Branch 1 taken 59633 times.
|
153311 | nal->size_bits = get_bit_length(nal, 1 + (codec_id == AV_CODEC_ID_HEVC), |
500 | skip_trailing_zeros); | ||
501 | |||
502 |
2/4✓ Branch 0 taken 153311 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 153311 times.
|
153311 | if (nal->size <= 0 || nal->size_bits <= 0) |
503 | ✗ | continue; | |
504 | |||
505 | 153311 | ret = init_get_bits(&nal->gb, nal->data, nal->size_bits); | |
506 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 153311 times.
|
153311 | if (ret < 0) |
507 | ✗ | return ret; | |
508 | |||
509 | /* Reset type in case it contains a stale value from a previously parsed NAL */ | ||
510 | 153311 | nal->type = 0; | |
511 | |||
512 |
2/2✓ Branch 0 taken 93678 times.
✓ Branch 1 taken 59633 times.
|
153311 | if (codec_id == AV_CODEC_ID_HEVC) |
513 | 93678 | ret = hevc_parse_nal_header(nal, logctx); | |
514 | else | ||
515 | 59633 | ret = h264_parse_nal_header(nal, logctx); | |
516 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 153309 times.
|
153311 | if (ret < 0) { |
517 | 2 | av_log(logctx, AV_LOG_WARNING, "Invalid NAL unit %d, skipping.\n", | |
518 | nal->type); | ||
519 | 2 | continue; | |
520 | } | ||
521 | |||
522 | 153309 | pkt->nb_nals++; | |
523 | } | ||
524 | |||
525 | 55646 | return 0; | |
526 | } | ||
527 | |||
528 | 3250 | void ff_h2645_packet_uninit(H2645Packet *pkt) | |
529 | { | ||
530 | int i; | ||
531 |
2/2✓ Branch 0 taken 11378 times.
✓ Branch 1 taken 3250 times.
|
14628 | for (i = 0; i < pkt->nals_allocated; i++) { |
532 | 11378 | av_freep(&pkt->nals[i].skipped_bytes_pos); | |
533 | } | ||
534 | 3250 | av_freep(&pkt->nals); | |
535 | 3250 | pkt->nals_allocated = pkt->nal_buffer_size = 0; | |
536 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 3210 times.
|
3250 | if (pkt->rbsp.rbsp_buffer_ref) { |
537 | 40 | av_buffer_unref(&pkt->rbsp.rbsp_buffer_ref); | |
538 | 40 | pkt->rbsp.rbsp_buffer = NULL; | |
539 | } else | ||
540 | 3210 | av_freep(&pkt->rbsp.rbsp_buffer); | |
541 | 3250 | pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0; | |
542 | 3250 | } | |
543 |