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_RN64A(src + i) &
57 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
58 0x8000800080008080ULL))
65 for (i = 0; i + 1 < length; i += 5) {
66 if (!((~AV_RN32A(src + i) &
67 (AV_RN32A(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 *nal_unit_name(int nal_type)
151 case HEVC_NAL_TRAIL_N : return "TRAIL_N";
152 case HEVC_NAL_TRAIL_R : return "TRAIL_R";
153 case HEVC_NAL_TSA_N : return "TSA_N";
154 case HEVC_NAL_TSA_R : return "TSA_R";
155 case HEVC_NAL_STSA_N : return "STSA_N";
156 case HEVC_NAL_STSA_R : return "STSA_R";
157 case HEVC_NAL_RADL_N : return "RADL_N";
158 case HEVC_NAL_RADL_R : return "RADL_R";
159 case HEVC_NAL_RASL_N : return "RASL_N";
160 case HEVC_NAL_RASL_R : return "RASL_R";
161 case HEVC_NAL_BLA_W_LP : return "BLA_W_LP";
162 case HEVC_NAL_BLA_W_RADL : return "BLA_W_RADL";
163 case HEVC_NAL_BLA_N_LP : return "BLA_N_LP";
164 case HEVC_NAL_IDR_W_RADL : return "IDR_W_RADL";
165 case HEVC_NAL_IDR_N_LP : return "IDR_N_LP";
166 case HEVC_NAL_CRA_NUT : return "CRA_NUT";
167 case HEVC_NAL_VPS : return "VPS";
168 case HEVC_NAL_SPS : return "SPS";
169 case HEVC_NAL_PPS : return "PPS";
170 case HEVC_NAL_AUD : return "AUD";
171 case HEVC_NAL_EOS_NUT : return "EOS_NUT";
172 case HEVC_NAL_EOB_NUT : return "EOB_NUT";
173 case HEVC_NAL_FD_NUT : return "FD_NUT";
174 case HEVC_NAL_SEI_PREFIX : return "SEI_PREFIX";
175 case HEVC_NAL_SEI_SUFFIX : return "SEI_SUFFIX";
176 default : return "?";
180 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
182 int size = nal->size;
185 while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
191 v = nal->data[size - 1];
193 if (size > INT_MAX / 8)
194 return AVERROR(ERANGE);
197 /* remove the stop bit and following trailing zeros,
198 * or nothing for damaged bitstreams */
200 size -= ff_ctz(v) + 1;
206 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
207 * 0 if the unit should be skipped, 1 otherwise
209 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
211 GetBitContext *gb = &nal->gb;
214 if (get_bits1(gb) != 0)
215 return AVERROR_INVALIDDATA;
217 nal->type = get_bits(gb, 6);
219 nuh_layer_id = get_bits(gb, 6);
220 nal->temporal_id = get_bits(gb, 3) - 1;
221 if (nal->temporal_id < 0)
222 return AVERROR_INVALIDDATA;
224 av_log(logctx, AV_LOG_DEBUG,
225 "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
226 nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
228 return nuh_layer_id == 0;
231 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
233 GetBitContext *gb = &nal->gb;
235 if (get_bits1(gb) != 0)
236 return AVERROR_INVALIDDATA;
238 nal->ref_idc = get_bits(gb, 2);
239 nal->type = get_bits(gb, 5);
241 av_log(logctx, AV_LOG_DEBUG,
242 "nal_unit_type: %d, nal_ref_idc: %d\n",
243 nal->type, nal->ref_idc);
248 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
252 if (buf + 3 >= next_avc)
253 return next_avc - buf;
255 while (buf + i + 3 < next_avc) {
256 if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
263 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
264 void *logctx, int is_nalff, int nal_length_size,
265 enum AVCodecID codec_id, int small_padding)
268 int consumed, ret = 0;
269 int next_avc = is_nalff ? 0 : length;
270 int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
272 bytestream2_init(&bc, buf, length);
273 av_fast_padded_malloc(&pkt->rbsp.rbsp_buffer, &pkt->rbsp.rbsp_buffer_alloc_size, length + padding);
274 if (!pkt->rbsp.rbsp_buffer)
275 return AVERROR(ENOMEM);
277 pkt->rbsp.rbsp_buffer_size = 0;
279 while (bytestream2_get_bytes_left(&bc) >= 4) {
281 int extract_length = 0;
282 int skip_trailing_zeros = 1;
284 if (bytestream2_tell(&bc) == next_avc) {
286 extract_length = get_nalsize(nal_length_size,
287 bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
288 if (extract_length < 0)
289 return extract_length;
291 bytestream2_skip(&bc, nal_length_size);
293 next_avc = bytestream2_tell(&bc) + extract_length;
297 if (bytestream2_tell(&bc) > next_avc)
298 av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
300 /* search start code */
301 buf_index = find_next_start_code(bc.buffer, buf + next_avc);
303 bytestream2_skip(&bc, buf_index);
305 if (!bytestream2_get_bytes_left(&bc)) {
306 if (pkt->nb_nals > 0) {
307 // No more start codes: we discarded some irrelevant
308 // bytes at the end of the packet.
311 av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
312 return AVERROR_INVALIDDATA;
316 extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
318 if (bytestream2_tell(&bc) >= next_avc) {
319 /* skip to the start of the next NAL */
320 bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
325 if (pkt->nals_allocated < pkt->nb_nals + 1) {
326 int new_size = pkt->nals_allocated + 1;
327 void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
330 return AVERROR(ENOMEM);
333 memset(pkt->nals + pkt->nals_allocated, 0,
334 (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
336 nal = &pkt->nals[pkt->nb_nals];
337 nal->skipped_bytes_pos_size = 1024; // initial buffer size
338 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
339 if (!nal->skipped_bytes_pos)
340 return AVERROR(ENOMEM);
342 pkt->nals_allocated = new_size;
344 nal = &pkt->nals[pkt->nb_nals];
346 consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, &pkt->rbsp, nal, small_padding);
350 if (is_nalff && (extract_length != consumed) && extract_length)
351 av_log(logctx, AV_LOG_DEBUG,
352 "NALFF: Consumed only %d bytes instead of %d\n",
353 consumed, extract_length);
357 bytestream2_skip(&bc, consumed);
359 /* see commit 3566042a0 */
360 if (bytestream2_get_bytes_left(&bc) >= 4 &&
361 bytestream2_peek_be32(&bc) == 0x000001E0)
362 skip_trailing_zeros = 0;
364 nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
366 ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
370 if (codec_id == AV_CODEC_ID_HEVC)
371 ret = hevc_parse_nal_header(nal, logctx);
373 ret = h264_parse_nal_header(nal, logctx);
374 if (ret <= 0 || nal->size <= 0) {
376 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
386 void ff_h2645_packet_uninit(H2645Packet *pkt)
389 for (i = 0; i < pkt->nals_allocated; i++) {
390 av_freep(&pkt->nals[i].skipped_bytes_pos);
392 av_freep(&pkt->nals);
393 pkt->nals_allocated = 0;
394 av_freep(&pkt->rbsp.rbsp_buffer);
395 pkt->rbsp.rbsp_buffer_alloc_size = pkt->rbsp.rbsp_buffer_size = 0;