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 "h2645_parse.h"
31 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
37 #define STARTCODE_TEST \
38 if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) { \
39 if (src[i + 2] != 3) { \
40 /* startcode, so we must be past the end */ \
45 #if HAVE_FAST_UNALIGNED
46 #define FIND_FIRST_ZERO \
47 if (i > 0 && !src[i]) \
52 for (i = 0; i + 1 < length; i += 9) {
53 if (!((~AV_RN64A(src + i) &
54 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
55 0x8000800080008080ULL))
62 for (i = 0; i + 1 < length; i += 5) {
63 if (!((~AV_RN32A(src + i) &
64 (AV_RN32A(src + i) - 0x01000101U)) &
71 #endif /* HAVE_FAST_64BIT */
73 for (i = 0; i + 1 < length; i += 2) {
76 if (i > 0 && src[i - 1] == 0)
80 #endif /* HAVE_FAST_UNALIGNED */
82 if (i >= length - 1) { // no escaped 0
86 nal->raw_size = length;
90 av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
91 length + AV_INPUT_BUFFER_PADDING_SIZE);
92 if (!nal->rbsp_buffer)
93 return AVERROR(ENOMEM);
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) {
105 if (src[si + 2] == 3) { // escape
111 } else // next start code
115 dst[di++] = src[si++];
118 dst[di++] = src[si++];
121 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
130 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
132 int size = nal->size;
135 while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
141 v = nal->data[size - 1];
143 if (size > INT_MAX / 8)
144 return AVERROR(ERANGE);
147 /* remove the stop bit and following trailing zeros,
148 * or nothing for damaged bitstreams */
150 size -= av_ctz(v) + 1;
156 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
157 * 0 if the unit should be skipped, 1 otherwise
159 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
161 GetBitContext *gb = &nal->gb;
164 if (get_bits1(gb) != 0)
165 return AVERROR_INVALIDDATA;
167 nal->type = get_bits(gb, 6);
169 nuh_layer_id = get_bits(gb, 6);
170 nal->temporal_id = get_bits(gb, 3) - 1;
171 if (nal->temporal_id < 0)
172 return AVERROR_INVALIDDATA;
174 av_log(logctx, AV_LOG_DEBUG,
175 "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
176 nal->type, nuh_layer_id, nal->temporal_id);
178 return nuh_layer_id == 0;
181 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
183 GetBitContext *gb = &nal->gb;
185 if (get_bits1(gb) != 0)
186 return AVERROR_INVALIDDATA;
188 nal->ref_idc = get_bits(gb, 2);
189 nal->type = get_bits(gb, 5);
191 av_log(logctx, AV_LOG_DEBUG,
192 "nal_unit_type: %d, nal_ref_idc: %d\n",
193 nal->type, nal->ref_idc);
198 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
199 void *logctx, int is_nalff, int nal_length_size,
200 enum AVCodecID codec_id)
202 int consumed, ret = 0;
205 while (length >= 4) {
207 int extract_length = 0;
208 int skip_trailing_zeros = 1;
212 for (i = 0; i < nal_length_size; i++)
213 extract_length = (extract_length << 8) | buf[i];
214 buf += nal_length_size;
215 length -= nal_length_size;
217 if (extract_length > length) {
218 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
219 return AVERROR_INVALIDDATA;
227 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
228 return AVERROR_INVALIDDATA;
232 extract_length = length;
235 if (pkt->nals_allocated < pkt->nb_nals + 1) {
236 int new_size = pkt->nals_allocated + 1;
237 H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
239 return AVERROR(ENOMEM);
242 memset(pkt->nals + pkt->nals_allocated, 0,
243 (new_size - pkt->nals_allocated) * sizeof(*tmp));
244 pkt->nals_allocated = new_size;
246 nal = &pkt->nals[pkt->nb_nals++];
248 consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);
252 /* see commit 3566042a0 */
253 if (consumed < length - 3 &&
254 buf[consumed] == 0x00 && buf[consumed + 1] == 0x00 &&
255 buf[consumed + 2] == 0x01 && buf[consumed + 3] == 0xE0)
256 skip_trailing_zeros = 0;
258 nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
260 ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
264 if (codec_id == AV_CODEC_ID_HEVC)
265 ret = hevc_parse_nal_header(nal, logctx);
267 ret = h264_parse_nal_header(nal, logctx);
270 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
283 void ff_h2645_packet_uninit(H2645Packet *pkt)
286 for (i = 0; i < pkt->nals_allocated; i++)
287 av_freep(&pkt->nals[i].rbsp_buffer);
288 av_freep(&pkt->nals);
289 pkt->nals_allocated = 0;