2 * H.264/HEVC common parsing code
4 * This file is part of FFmpeg.
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.
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.
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
25 #include "libavutil/intmath.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
29 #include "bytestream.h"
31 #include "h2645_parse.h"
33 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
34 H2645RBSP *rbsp, H2645NAL *nal, int small_padding)
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 */ \
48 #if HAVE_FAST_UNALIGNED
49 #define FIND_FIRST_ZERO \
50 if (i > 0 && !src[i]) \
55 for (i = 0; i + 1 < length; i += 9) {
56 if (!((~AV_RN64(src + i) &
57 (AV_RN64(src + i) - 0x0100010001000101ULL)) &
58 0x8000800080008080ULL))
65 for (i = 0; i + 1 < length; i += 5) {
66 if (!((~AV_RN32(src + i) &
67 (AV_RN32(src + i) - 0x01000101U)) &
74 #endif /* HAVE_FAST_64BIT */
76 for (i = 0; i + 1 < length; i += 2) {
79 if (i > 0 && src[i - 1] == 0)
83 #endif /* HAVE_FAST_UNALIGNED */
85 if (i >= length - 1 && small_padding) { // no escaped 0
89 nal->raw_size = length;
91 } else if (i > length)
94 nal->rbsp_buffer = &rbsp->rbsp_buffer[rbsp->rbsp_buffer_size];
95 dst = nal->rbsp_buffer;
99 while (si + 2 < length) {
100 // remove escapes (very rare 1:2^22)
101 if (src[si + 2] > 3) {
102 dst[di++] = src[si++];
103 dst[di++] = src[si++];
104 } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
105 if (src[si + 2] == 3) { // escape
110 if (nal->skipped_bytes_pos) {
111 nal->skipped_bytes++;
112 if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
113 nal->skipped_bytes_pos_size *= 2;
114 av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
115 av_reallocp_array(&nal->skipped_bytes_pos,
116 nal->skipped_bytes_pos_size,
117 sizeof(*nal->skipped_bytes_pos));
118 if (!nal->skipped_bytes_pos) {
119 nal->skipped_bytes_pos_size = 0;
120 return AVERROR(ENOMEM);
123 if (nal->skipped_bytes_pos)
124 nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
127 } else // next start code
131 dst[di++] = src[si++];
134 dst[di++] = src[si++];
137 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
143 rbsp->rbsp_buffer_size += si;
148 static const char *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 "IRAP_IRAP_VCL22", // HEVC_NAL_IRAP_VCL22
172 "IRAP_IRAP_VCL23", // HEVC_NAL_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
215 static const char *nal_unit_name(int nal_type)
217 av_assert0(nal_type >= 0 && nal_type < 64);
218 return hevc_nal_type_name[nal_type];
221 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
223 int size = nal->size;
226 while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
232 v = nal->data[size - 1];
234 if (size > INT_MAX / 8)
235 return AVERROR(ERANGE);
238 /* remove the stop bit and following trailing zeros,
239 * or nothing for damaged bitstreams */
241 size -= ff_ctz(v) + 1;
247 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
248 * 0 if the unit should be skipped, 1 otherwise
250 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
252 GetBitContext *gb = &nal->gb;
255 if (get_bits1(gb) != 0)
256 return AVERROR_INVALIDDATA;
258 nal->type = get_bits(gb, 6);
260 nuh_layer_id = get_bits(gb, 6);
261 nal->temporal_id = get_bits(gb, 3) - 1;
262 if (nal->temporal_id < 0)
263 return AVERROR_INVALIDDATA;
265 av_log(logctx, AV_LOG_DEBUG,
266 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
267 nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
269 return nuh_layer_id == 0;
272 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
274 GetBitContext *gb = &nal->gb;
276 if (get_bits1(gb) != 0)
277 return AVERROR_INVALIDDATA;
279 nal->ref_idc = get_bits(gb, 2);
280 nal->type = get_bits(gb, 5);
282 av_log(logctx, AV_LOG_DEBUG,
283 "nal_unit_type: %d, nal_ref_idc: %d\n",
284 nal->type, nal->ref_idc);
289 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
293 if (buf + 3 >= next_avc)
294 return next_avc - buf;
296 while (buf + i + 3 < next_avc) {
297 if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
304 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
305 void *logctx, int is_nalff, int nal_length_size,
306 enum AVCodecID codec_id, int small_padding)
309 int consumed, ret = 0;
310 int next_avc = is_nalff ? 0 : length;
311 int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
313 bytestream2_init(&bc, buf, length);
314 av_fast_padded_malloc(&pkt->rbsp.rbsp_buffer, &pkt->rbsp.rbsp_buffer_alloc_size, length + padding);
315 if (!pkt->rbsp.rbsp_buffer)
316 return AVERROR(ENOMEM);
318 pkt->rbsp.rbsp_buffer_size = 0;
320 while (bytestream2_get_bytes_left(&bc) >= 4) {
322 int extract_length = 0;
323 int skip_trailing_zeros = 1;
325 if (bytestream2_tell(&bc) == next_avc) {
327 extract_length = get_nalsize(nal_length_size,
328 bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
329 if (extract_length < 0)
330 return extract_length;
332 bytestream2_skip(&bc, nal_length_size);
334 next_avc = bytestream2_tell(&bc) + extract_length;
338 if (bytestream2_tell(&bc) > next_avc)
339 av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
341 /* search start code */
342 buf_index = find_next_start_code(bc.buffer, buf + next_avc);
344 bytestream2_skip(&bc, buf_index);
346 if (!bytestream2_get_bytes_left(&bc)) {
347 if (pkt->nb_nals > 0) {
348 // No more start codes: we discarded some irrelevant
349 // bytes at the end of the packet.
352 av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
353 return AVERROR_INVALIDDATA;
357 extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
359 if (bytestream2_tell(&bc) >= next_avc) {
360 /* skip to the start of the next NAL */
361 bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
366 if (pkt->nals_allocated < pkt->nb_nals + 1) {
367 int new_size = pkt->nals_allocated + 1;
368 void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
371 return AVERROR(ENOMEM);
374 memset(pkt->nals + pkt->nals_allocated, 0,
375 (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
377 nal = &pkt->nals[pkt->nb_nals];
378 nal->skipped_bytes_pos_size = 1024; // initial buffer size
379 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
380 if (!nal->skipped_bytes_pos)
381 return AVERROR(ENOMEM);
383 pkt->nals_allocated = new_size;
385 nal = &pkt->nals[pkt->nb_nals];
387 consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
391 if (is_nalff && (extract_length != consumed) && extract_length)
392 av_log(logctx, AV_LOG_DEBUG,
393 "NALFF: Consumed only %d bytes instead of %d\n",
394 consumed, extract_length);
398 bytestream2_skip(&bc, consumed);
400 /* see commit 3566042a0 */
401 if (bytestream2_get_bytes_left(&bc) >= 4 &&
402 bytestream2_peek_be32(&bc) == 0x000001E0)
403 skip_trailing_zeros = 0;
405 nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
407 ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
411 if (codec_id == AV_CODEC_ID_HEVC)
412 ret = hevc_parse_nal_header(nal, logctx);
414 ret = h264_parse_nal_header(nal, logctx);
415 if (ret <= 0 || nal->size <= 0 || nal->size_bits <= 0) {
417 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
427 void ff_h2645_packet_uninit(H2645Packet *pkt)
430 for (i = 0; i < pkt->nals_allocated; i++) {
431 av_freep(&pkt->nals[i].skipped_bytes_pos);
433 av_freep(&pkt->nals);
434 pkt->nals_allocated = 0;
435 av_freep(&pkt->rbsp.rbsp_buffer);
436 pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;