2 * H.264/HEVC common parsing code
4 * This file is part of Libav.
6 * Libav 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 * Libav 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 Libav; 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"
30 #include "h2645_parse.h"
32 int ff_h2645_extract_rbsp(const uint8_t *src
, int length
,
38 #define STARTCODE_TEST \
39 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
40 if (src[i + 2] != 3) { \
41 /* startcode, so we must be past the end */ \
46 #if HAVE_FAST_UNALIGNED
47 #define FIND_FIRST_ZERO \
48 if (i > 0 && !src[i]) \
53 for (i
= 0; i
+ 1 < length
; i
+= 9) {
54 if (!((~AV_RN64A(src
+ i
) &
55 (AV_RN64A(src
+ i
) - 0x0100010001000101ULL
)) &
56 0x8000800080008080ULL
))
63 for (i
= 0; i
+ 1 < length
; i
+= 5) {
64 if (!((~AV_RN32A(src
+ i
) &
65 (AV_RN32A(src
+ i
) - 0x01000101U
)) &
72 #endif /* HAVE_FAST_64BIT */
74 for (i
= 0; i
+ 1 < length
; i
+= 2) {
77 if (i
> 0 && src
[i
- 1] == 0)
81 #endif /* HAVE_FAST_UNALIGNED */
83 if (i
>= length
- 1) { // no escaped 0
87 nal
->raw_size
= length
;
91 av_fast_malloc(&nal
->rbsp_buffer
, &nal
->rbsp_buffer_size
,
92 length
+ AV_INPUT_BUFFER_PADDING_SIZE
);
93 if (!nal
->rbsp_buffer
)
94 return AVERROR(ENOMEM
);
96 dst
= nal
->rbsp_buffer
;
100 while (si
+ 2 < length
) {
101 // remove escapes (very rare 1:2^22)
102 if (src
[si
+ 2] > 3) {
103 dst
[di
++] = src
[si
++];
104 dst
[di
++] = src
[si
++];
105 } else if (src
[si
] == 0 && src
[si
+ 1] == 0) {
106 if (src
[si
+ 2] == 3) { // escape
112 } else // next start code
116 dst
[di
++] = src
[si
++];
119 dst
[di
++] = src
[si
++];
122 memset(dst
+ di
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
131 static int get_bit_length(H2645NAL
*nal
, int skip_trailing_zeros
)
133 int size
= nal
->size
;
136 while (skip_trailing_zeros
&& size
> 0 && nal
->data
[size
- 1] == 0)
142 v
= nal
->data
[size
- 1];
144 if (size
> INT_MAX
/ 8)
145 return AVERROR(ERANGE
);
148 /* remove the stop bit and following trailing zeros,
149 * or nothing for damaged bitstreams */
151 size
-= av_ctz(v
) + 1;
157 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
158 * 0 if the unit should be skipped, 1 otherwise
160 static int hevc_parse_nal_header(H2645NAL
*nal
, void *logctx
)
162 GetBitContext
*gb
= &nal
->gb
;
165 if (get_bits1(gb
) != 0)
166 return AVERROR_INVALIDDATA
;
168 nal
->type
= get_bits(gb
, 6);
170 nuh_layer_id
= get_bits(gb
, 6);
171 nal
->temporal_id
= get_bits(gb
, 3) - 1;
172 if (nal
->temporal_id
< 0)
173 return AVERROR_INVALIDDATA
;
175 av_log(logctx
, AV_LOG_DEBUG
,
176 "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
177 nal
->type
, nuh_layer_id
, nal
->temporal_id
);
179 return nuh_layer_id
== 0;
182 static int h264_parse_nal_header(H2645NAL
*nal
, void *logctx
)
184 GetBitContext
*gb
= &nal
->gb
;
186 if (get_bits1(gb
) != 0)
187 return AVERROR_INVALIDDATA
;
189 nal
->ref_idc
= get_bits(gb
, 2);
190 nal
->type
= get_bits(gb
, 5);
192 av_log(logctx
, AV_LOG_DEBUG
,
193 "nal_unit_type: %d, nal_ref_idc: %d\n",
194 nal
->type
, nal
->ref_idc
);
199 static int find_next_start_code(const uint8_t *buf
, const uint8_t *next_avc
)
203 if (buf
+ 3 >= next_avc
)
204 return next_avc
- buf
;
206 while (buf
+ i
+ 3 < next_avc
) {
207 if (buf
[i
] == 0 && buf
[i
+ 1] == 0 && buf
[i
+ 2] == 1)
214 int ff_h2645_packet_split(H2645Packet
*pkt
, const uint8_t *buf
, int length
,
215 void *logctx
, int is_nalff
, int nal_length_size
,
216 enum AVCodecID codec_id
)
219 int consumed
, ret
= 0;
220 size_t next_avc
= is_nalff
? 0 : length
;
222 bytestream2_init(&bc
, buf
, length
);
225 while (bytestream2_get_bytes_left(&bc
) >= 4) {
227 int extract_length
= 0;
228 int skip_trailing_zeros
= 1;
231 * Only parse an AVC1 length field if one is expected at the current
232 * buffer position. There are unfortunately streams with multiple
233 * NAL units covered by the length field. Those NAL units are delimited
234 * by Annex B start code prefixes. ff_h2645_extract_rbsp() detects it
235 * correctly and consumes only the first NAL unit. The additional NAL
236 * units are handled here in the Annex B parsing code.
238 if (bytestream2_tell(&bc
) == next_avc
) {
240 for (i
= 0; i
< nal_length_size
; i
++)
241 extract_length
= (extract_length
<< 8) | bytestream2_get_byte(&bc
);
243 if (extract_length
> bytestream2_get_bytes_left(&bc
)) {
244 av_log(logctx
, AV_LOG_ERROR
,
245 "Invalid NAL unit size (%d > %d).\n",
246 extract_length
, bytestream2_get_bytes_left(&bc
));
247 return AVERROR_INVALIDDATA
;
249 // keep track of the next AVC1 length field
250 next_avc
= bytestream2_tell(&bc
) + extract_length
;
253 * expected to return immediately except for streams with mixed
256 int buf_index
= find_next_start_code(bc
.buffer
, buf
+ next_avc
);
258 bytestream2_skip(&bc
, buf_index
);
261 * break if an AVC1 length field is expected at the current buffer
264 if (bytestream2_tell(&bc
) == next_avc
)
267 if (bytestream2_get_bytes_left(&bc
) > 0) {
268 extract_length
= bytestream2_get_bytes_left(&bc
);
269 } else if (pkt
->nb_nals
== 0) {
270 av_log(logctx
, AV_LOG_ERROR
, "No NAL unit found\n");
271 return AVERROR_INVALIDDATA
;
277 if (pkt
->nals_allocated
< pkt
->nb_nals
+ 1) {
278 int new_size
= pkt
->nals_allocated
+ 1;
279 H2645NAL
*tmp
= av_realloc_array(pkt
->nals
, new_size
, sizeof(*tmp
));
281 return AVERROR(ENOMEM
);
284 memset(pkt
->nals
+ pkt
->nals_allocated
, 0,
285 (new_size
- pkt
->nals_allocated
) * sizeof(*tmp
));
286 pkt
->nals_allocated
= new_size
;
288 nal
= &pkt
->nals
[pkt
->nb_nals
++];
290 consumed
= ff_h2645_extract_rbsp(bc
.buffer
, extract_length
, nal
);
294 bytestream2_skip(&bc
, consumed
);
296 /* see commit 3566042a0 */
297 if (bytestream2_get_bytes_left(&bc
) >= 4 &&
298 bytestream2_peek_be32(&bc
) == 0x000001E0)
299 skip_trailing_zeros
= 0;
301 nal
->size_bits
= get_bit_length(nal
, skip_trailing_zeros
);
303 ret
= init_get_bits(&nal
->gb
, nal
->data
, nal
->size_bits
);
307 if (codec_id
== AV_CODEC_ID_HEVC
)
308 ret
= hevc_parse_nal_header(nal
, logctx
);
310 ret
= h264_parse_nal_header(nal
, logctx
);
313 av_log(logctx
, AV_LOG_ERROR
, "Invalid NAL unit %d, skipping.\n",
323 void ff_h2645_packet_uninit(H2645Packet
*pkt
)
326 for (i
= 0; i
< pkt
->nals_allocated
; i
++)
327 av_freep(&pkt
->nals
[i
].rbsp_buffer
);
328 av_freep(&pkt
->nals
);
329 pkt
->nals_allocated
= 0;