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"
30 #include "h2645_parse.h"
32 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
38 nal->skipped_bytes = 0;
39 #define STARTCODE_TEST \
40 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
41 if (src[i + 2] != 3) { \
42 /* startcode, so we must be past the end */ \
47 #if HAVE_FAST_UNALIGNED
48 #define FIND_FIRST_ZERO \
49 if (i > 0 && !src[i]) \
54 for (i = 0; i + 1 < length; i += 9) {
55 if (!((~AV_RN64A(src + i) &
56 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
57 0x8000800080008080ULL))
64 for (i = 0; i + 1 < length; i += 5) {
65 if (!((~AV_RN32A(src + i) &
66 (AV_RN32A(src + i) - 0x01000101U)) &
73 #endif /* HAVE_FAST_64BIT */
75 for (i = 0; i + 1 < length; i += 2) {
78 if (i > 0 && src[i - 1] == 0)
82 #endif /* HAVE_FAST_UNALIGNED */
84 if (i >= length - 1) { // no escaped 0
88 nal->raw_size = length;
92 av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
93 length + AV_INPUT_BUFFER_PADDING_SIZE);
94 if (!nal->rbsp_buffer)
95 return AVERROR(ENOMEM);
97 dst = nal->rbsp_buffer;
101 while (si + 2 < length) {
102 // remove escapes (very rare 1:2^22)
103 if (src[si + 2] > 3) {
104 dst[di++] = src[si++];
105 dst[di++] = src[si++];
106 } else if (src[si] == 0 && src[si + 1] == 0) {
107 if (src[si + 2] == 3) { // escape
112 if (nal->skipped_bytes_pos) {
113 nal->skipped_bytes++;
114 if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
115 nal->skipped_bytes_pos_size *= 2;
116 av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
117 av_reallocp_array(&nal->skipped_bytes_pos,
118 nal->skipped_bytes_pos_size,
119 sizeof(*nal->skipped_bytes_pos));
120 if (!nal->skipped_bytes_pos) {
121 nal->skipped_bytes_pos_size = 0;
122 return AVERROR(ENOMEM);
125 if (nal->skipped_bytes_pos)
126 nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
129 } else // next start code
133 dst[di++] = src[si++];
136 dst[di++] = src[si++];
139 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
148 static const char *nal_unit_name(int nal_type)
151 case NAL_TRAIL_N : return "TRAIL_N";
152 case NAL_TRAIL_R : return "TRAIL_R";
153 case NAL_TSA_N : return "TSA_N";
154 case NAL_TSA_R : return "TSA_R";
155 case NAL_STSA_N : return "STSA_N";
156 case NAL_STSA_R : return "STSA_R";
157 case NAL_RADL_N : return "RADL_N";
158 case NAL_RADL_R : return "RADL_R";
159 case NAL_RASL_N : return "RASL_N";
160 case NAL_RASL_R : return "RASL_R";
161 case NAL_BLA_W_LP : return "BLA_W_LP";
162 case NAL_BLA_W_RADL : return "BLA_W_RADL";
163 case NAL_BLA_N_LP : return "BLA_N_LP";
164 case NAL_IDR_W_RADL : return "IDR_W_RADL";
165 case NAL_IDR_N_LP : return "IDR_N_LP";
166 case NAL_CRA_NUT : return "CRA_NUT";
167 case NAL_VPS : return "VPS";
168 case NAL_SPS : return "SPS";
169 case NAL_PPS : return "PPS";
170 case NAL_AUD : return "AUD";
171 case NAL_EOS_NUT : return "EOS_NUT";
172 case NAL_EOB_NUT : return "EOB_NUT";
173 case NAL_FD_NUT : return "FD_NUT";
174 case NAL_SEI_PREFIX : return "SEI_PREFIX";
175 case 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 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
249 void *logctx, int is_nalff, int nal_length_size,
250 enum AVCodecID codec_id)
252 int consumed, ret = 0;
255 while (length >= 4) {
257 int extract_length = 0;
258 int skip_trailing_zeros = 1;
262 for (i = 0; i < nal_length_size; i++)
263 extract_length = (extract_length << 8) | buf[i];
264 buf += nal_length_size;
265 length -= nal_length_size;
267 if (extract_length > length) {
268 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
269 return AVERROR_INVALIDDATA;
272 /* search start code */
273 while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
277 if (pkt->nb_nals > 0) {
278 // No more start codes: we discarded some irrelevant
279 // bytes at the end of the packet.
282 av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
283 return AVERROR_INVALIDDATA;
290 extract_length = length;
293 if (pkt->nals_allocated < pkt->nb_nals + 1) {
294 int new_size = pkt->nals_allocated + 1;
295 void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
298 return AVERROR(ENOMEM);
301 memset(pkt->nals + pkt->nals_allocated, 0,
302 (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
304 nal = &pkt->nals[pkt->nb_nals];
305 nal->skipped_bytes_pos_size = 1024; // initial buffer size
306 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
307 if (!nal->skipped_bytes_pos)
308 return AVERROR(ENOMEM);
310 pkt->nals_allocated = new_size;
312 nal = &pkt->nals[pkt->nb_nals];
314 consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);
320 /* see commit 3566042a0 */
321 if (consumed < length - 3 &&
322 buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&
323 buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)
324 skip_trailing_zeros = 0;
326 nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
328 ret = init_get_bits8(&nal->gb, nal->data, nal->size_bits);
332 if (codec_id == AV_CODEC_ID_HEVC)
333 ret = hevc_parse_nal_header(nal, logctx);
335 ret = h264_parse_nal_header(nal, logctx);
338 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
351 void ff_h2645_packet_uninit(H2645Packet *pkt)
354 for (i = 0; i < pkt->nals_allocated; i++) {
355 av_freep(&pkt->nals[i].rbsp_buffer);
356 av_freep(&pkt->nals[i].skipped_bytes_pos);
358 av_freep(&pkt->nals);
359 pkt->nals_allocated = 0;