blob: b0d9ff66f00f305af17e9382c5dc6e612eaab057
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 "hevc.h" |
30 | #include "h2645_parse.h" |
31 | |
32 | int ff_h2645_extract_rbsp(const uint8_t *src, int length, |
33 | H2645NAL *nal, int small_padding) |
34 | { |
35 | int i, si, di; |
36 | uint8_t *dst; |
37 | int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE; |
38 | |
39 | nal->skipped_bytes = 0; |
40 | #define STARTCODE_TEST \ |
41 | if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \ |
42 | if (src[i + 2] != 3 && src[i + 2] != 0) { \ |
43 | /* startcode, so we must be past the end */ \ |
44 | length = i; \ |
45 | } \ |
46 | break; \ |
47 | } |
48 | #if HAVE_FAST_UNALIGNED |
49 | #define FIND_FIRST_ZERO \ |
50 | if (i > 0 && !src[i]) \ |
51 | i--; \ |
52 | while (src[i]) \ |
53 | i++ |
54 | #if HAVE_FAST_64BIT |
55 | for (i = 0; i + 1 < length; i += 9) { |
56 | if (!((~AV_RN64A(src + i) & |
57 | (AV_RN64A(src + i) - 0x0100010001000101ULL)) & |
58 | 0x8000800080008080ULL)) |
59 | continue; |
60 | FIND_FIRST_ZERO; |
61 | STARTCODE_TEST; |
62 | i -= 7; |
63 | } |
64 | #else |
65 | for (i = 0; i + 1 < length; i += 5) { |
66 | if (!((~AV_RN32A(src + i) & |
67 | (AV_RN32A(src + i) - 0x01000101U)) & |
68 | 0x80008080U)) |
69 | continue; |
70 | FIND_FIRST_ZERO; |
71 | STARTCODE_TEST; |
72 | i -= 3; |
73 | } |
74 | #endif /* HAVE_FAST_64BIT */ |
75 | #else |
76 | for (i = 0; i + 1 < length; i += 2) { |
77 | if (src[i]) |
78 | continue; |
79 | if (i > 0 && src[i - 1] == 0) |
80 | i--; |
81 | STARTCODE_TEST; |
82 | } |
83 | #endif /* HAVE_FAST_UNALIGNED */ |
84 | |
85 | if (i >= length - 1 && small_padding) { // no escaped 0 |
86 | nal->data = |
87 | nal->raw_data = src; |
88 | nal->size = |
89 | nal->raw_size = length; |
90 | return length; |
91 | } else if (i > length) |
92 | i = length; |
93 | |
94 | av_fast_padded_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size, |
95 | length + padding); |
96 | if (!nal->rbsp_buffer) |
97 | return AVERROR(ENOMEM); |
98 | |
99 | dst = nal->rbsp_buffer; |
100 | |
101 | memcpy(dst, src, i); |
102 | si = di = i; |
103 | while (si + 2 < length) { |
104 | // remove escapes (very rare 1:2^22) |
105 | if (src[si + 2] > 3) { |
106 | dst[di++] = src[si++]; |
107 | dst[di++] = src[si++]; |
108 | } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) { |
109 | if (src[si + 2] == 3) { // escape |
110 | dst[di++] = 0; |
111 | dst[di++] = 0; |
112 | si += 3; |
113 | |
114 | if (nal->skipped_bytes_pos) { |
115 | nal->skipped_bytes++; |
116 | if (nal->skipped_bytes_pos_size < nal->skipped_bytes) { |
117 | nal->skipped_bytes_pos_size *= 2; |
118 | av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes); |
119 | av_reallocp_array(&nal->skipped_bytes_pos, |
120 | nal->skipped_bytes_pos_size, |
121 | sizeof(*nal->skipped_bytes_pos)); |
122 | if (!nal->skipped_bytes_pos) { |
123 | nal->skipped_bytes_pos_size = 0; |
124 | return AVERROR(ENOMEM); |
125 | } |
126 | } |
127 | if (nal->skipped_bytes_pos) |
128 | nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1; |
129 | } |
130 | continue; |
131 | } else // next start code |
132 | goto nsc; |
133 | } |
134 | |
135 | dst[di++] = src[si++]; |
136 | } |
137 | while (si < length) |
138 | dst[di++] = src[si++]; |
139 | |
140 | nsc: |
141 | memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE); |
142 | |
143 | nal->data = dst; |
144 | nal->size = di; |
145 | nal->raw_data = src; |
146 | nal->raw_size = si; |
147 | return si; |
148 | } |
149 | |
150 | static const char *nal_unit_name(int nal_type) |
151 | { |
152 | switch(nal_type) { |
153 | case HEVC_NAL_TRAIL_N : return "TRAIL_N"; |
154 | case HEVC_NAL_TRAIL_R : return "TRAIL_R"; |
155 | case HEVC_NAL_TSA_N : return "TSA_N"; |
156 | case HEVC_NAL_TSA_R : return "TSA_R"; |
157 | case HEVC_NAL_STSA_N : return "STSA_N"; |
158 | case HEVC_NAL_STSA_R : return "STSA_R"; |
159 | case HEVC_NAL_RADL_N : return "RADL_N"; |
160 | case HEVC_NAL_RADL_R : return "RADL_R"; |
161 | case HEVC_NAL_RASL_N : return "RASL_N"; |
162 | case HEVC_NAL_RASL_R : return "RASL_R"; |
163 | case HEVC_NAL_BLA_W_LP : return "BLA_W_LP"; |
164 | case HEVC_NAL_BLA_W_RADL : return "BLA_W_RADL"; |
165 | case HEVC_NAL_BLA_N_LP : return "BLA_N_LP"; |
166 | case HEVC_NAL_IDR_W_RADL : return "IDR_W_RADL"; |
167 | case HEVC_NAL_IDR_N_LP : return "IDR_N_LP"; |
168 | case HEVC_NAL_CRA_NUT : return "CRA_NUT"; |
169 | case HEVC_NAL_VPS : return "VPS"; |
170 | case HEVC_NAL_SPS : return "SPS"; |
171 | case HEVC_NAL_PPS : return "PPS"; |
172 | case HEVC_NAL_AUD : return "AUD"; |
173 | case HEVC_NAL_EOS_NUT : return "EOS_NUT"; |
174 | case HEVC_NAL_EOB_NUT : return "EOB_NUT"; |
175 | case HEVC_NAL_FD_NUT : return "FD_NUT"; |
176 | case HEVC_NAL_SEI_PREFIX : return "SEI_PREFIX"; |
177 | case HEVC_NAL_SEI_SUFFIX : return "SEI_SUFFIX"; |
178 | default : return "?"; |
179 | } |
180 | } |
181 | |
182 | static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros) |
183 | { |
184 | int size = nal->size; |
185 | int v; |
186 | |
187 | while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0) |
188 | size--; |
189 | |
190 | if (!size) |
191 | return 0; |
192 | |
193 | v = nal->data[size - 1]; |
194 | |
195 | if (size > INT_MAX / 8) |
196 | return AVERROR(ERANGE); |
197 | size *= 8; |
198 | |
199 | /* remove the stop bit and following trailing zeros, |
200 | * or nothing for damaged bitstreams */ |
201 | if (v) |
202 | size -= ff_ctz(v) + 1; |
203 | |
204 | return size; |
205 | } |
206 | |
207 | /** |
208 | * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit, |
209 | * 0 if the unit should be skipped, 1 otherwise |
210 | */ |
211 | static int hevc_parse_nal_header(H2645NAL *nal, void *logctx) |
212 | { |
213 | GetBitContext *gb = &nal->gb; |
214 | int nuh_layer_id; |
215 | |
216 | if (get_bits1(gb) != 0) |
217 | return AVERROR_INVALIDDATA; |
218 | |
219 | nal->type = get_bits(gb, 6); |
220 | |
221 | nuh_layer_id = get_bits(gb, 6); |
222 | nal->temporal_id = get_bits(gb, 3) - 1; |
223 | if (nal->temporal_id < 0) |
224 | return AVERROR_INVALIDDATA; |
225 | |
226 | av_log(logctx, AV_LOG_DEBUG, |
227 | "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n", |
228 | nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id); |
229 | |
230 | return nuh_layer_id == 0; |
231 | } |
232 | |
233 | static int h264_parse_nal_header(H2645NAL *nal, void *logctx) |
234 | { |
235 | GetBitContext *gb = &nal->gb; |
236 | |
237 | if (get_bits1(gb) != 0) |
238 | return AVERROR_INVALIDDATA; |
239 | |
240 | nal->ref_idc = get_bits(gb, 2); |
241 | nal->type = get_bits(gb, 5); |
242 | |
243 | av_log(logctx, AV_LOG_DEBUG, |
244 | "nal_unit_type: %d, nal_ref_idc: %d\n", |
245 | nal->type, nal->ref_idc); |
246 | |
247 | return 1; |
248 | } |
249 | |
250 | int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, |
251 | void *logctx, int is_nalff, int nal_length_size, |
252 | enum AVCodecID codec_id, int small_padding) |
253 | { |
254 | int consumed, ret = 0; |
255 | const uint8_t *next_avc = is_nalff ? buf : buf + length; |
256 | |
257 | pkt->nb_nals = 0; |
258 | while (length >= 4) { |
259 | H2645NAL *nal; |
260 | int extract_length = 0; |
261 | int skip_trailing_zeros = 1; |
262 | |
263 | if (buf == next_avc) { |
264 | int i = 0; |
265 | extract_length = get_nalsize(nal_length_size, |
266 | buf, length, &i, logctx); |
267 | if (extract_length < 0) |
268 | return extract_length; |
269 | |
270 | buf += nal_length_size; |
271 | length -= nal_length_size; |
272 | |
273 | next_avc = buf + extract_length; |
274 | } else { |
275 | if (buf > next_avc) |
276 | av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n"); |
277 | |
278 | /* search start code */ |
279 | while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) { |
280 | ++buf; |
281 | --length; |
282 | if (length < 4) { |
283 | if (pkt->nb_nals > 0) { |
284 | // No more start codes: we discarded some irrelevant |
285 | // bytes at the end of the packet. |
286 | return 0; |
287 | } else { |
288 | av_log(logctx, AV_LOG_ERROR, "No start code is found.\n"); |
289 | return AVERROR_INVALIDDATA; |
290 | } |
291 | } else if (buf >= (next_avc - 3)) |
292 | break; |
293 | } |
294 | |
295 | buf += 3; |
296 | length -= 3; |
297 | extract_length = FFMIN(length, next_avc - buf); |
298 | |
299 | if (buf >= next_avc) { |
300 | /* skip to the start of the next NAL */ |
301 | int offset = next_avc - buf; |
302 | buf += offset; |
303 | length -= offset; |
304 | continue; |
305 | } |
306 | } |
307 | |
308 | if (pkt->nals_allocated < pkt->nb_nals + 1) { |
309 | int new_size = pkt->nals_allocated + 1; |
310 | void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals)); |
311 | |
312 | if (!tmp) |
313 | return AVERROR(ENOMEM); |
314 | |
315 | pkt->nals = tmp; |
316 | memset(pkt->nals + pkt->nals_allocated, 0, |
317 | (new_size - pkt->nals_allocated) * sizeof(*pkt->nals)); |
318 | |
319 | nal = &pkt->nals[pkt->nb_nals]; |
320 | nal->skipped_bytes_pos_size = 1024; // initial buffer size |
321 | nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos)); |
322 | if (!nal->skipped_bytes_pos) |
323 | return AVERROR(ENOMEM); |
324 | |
325 | pkt->nals_allocated = new_size; |
326 | } |
327 | nal = &pkt->nals[pkt->nb_nals]; |
328 | |
329 | consumed = ff_h2645_extract_rbsp(buf, extract_length, nal, small_padding); |
330 | if (consumed < 0) |
331 | return consumed; |
332 | |
333 | if (is_nalff && (extract_length != consumed) && extract_length) |
334 | av_log(logctx, AV_LOG_DEBUG, |
335 | "NALFF: Consumed only %d bytes instead of %d\n", |
336 | consumed, extract_length); |
337 | |
338 | pkt->nb_nals++; |
339 | |
340 | /* see commit 3566042a0 */ |
341 | if (consumed < length - 3 && |
342 | buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 && |
343 | buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0) |
344 | skip_trailing_zeros = 0; |
345 | |
346 | nal->size_bits = get_bit_length(nal, skip_trailing_zeros); |
347 | |
348 | ret = init_get_bits(&nal->gb, nal->data, nal->size_bits); |
349 | if (ret < 0) |
350 | return ret; |
351 | |
352 | if (codec_id == AV_CODEC_ID_HEVC) |
353 | ret = hevc_parse_nal_header(nal, logctx); |
354 | else |
355 | ret = h264_parse_nal_header(nal, logctx); |
356 | if (ret <= 0 || nal->size <= 0) { |
357 | if (ret < 0) { |
358 | av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n", |
359 | nal->type); |
360 | } |
361 | pkt->nb_nals--; |
362 | } |
363 | |
364 | buf += consumed; |
365 | length -= consumed; |
366 | } |
367 | |
368 | return 0; |
369 | } |
370 | |
371 | void ff_h2645_packet_uninit(H2645Packet *pkt) |
372 | { |
373 | int i; |
374 | for (i = 0; i < pkt->nals_allocated; i++) { |
375 | av_freep(&pkt->nals[i].rbsp_buffer); |
376 | av_freep(&pkt->nals[i].skipped_bytes_pos); |
377 | } |
378 | av_freep(&pkt->nals); |
379 | pkt->nals_allocated = 0; |
380 | } |
381 |