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/intreadwrite.h"
26 #include "libavutil/mem.h"
30 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
31 * between these functions would be nice. */
32 int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length,
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) { \
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) { // no escaped 0
89 nal->raw_size = length;
93 av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
94 length + FF_INPUT_BUFFER_PADDING_SIZE);
95 if (!nal->rbsp_buffer)
96 return AVERROR(ENOMEM);
98 dst = nal->rbsp_buffer;
102 while (si + 2 < length) {
103 // remove escapes (very rare 1:2^22)
104 if (src[si + 2] > 3) {
105 dst[di++] = src[si++];
106 dst[di++] = src[si++];
107 } else if (src[si] == 0 && src[si + 1] == 0) {
108 if (src[si + 2] == 3) { // escape
113 if (s && nal->skipped_bytes_pos) {
114 nal->skipped_bytes++;
115 if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
116 nal->skipped_bytes_pos_size *= 2;
117 av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
118 av_reallocp_array(&nal->skipped_bytes_pos,
119 nal->skipped_bytes_pos_size,
120 sizeof(*nal->skipped_bytes_pos));
121 if (!nal->skipped_bytes_pos) {
122 nal->skipped_bytes_pos_size = 0;
123 return AVERROR(ENOMEM);
126 if (nal->skipped_bytes_pos)
127 nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
130 } else // next start code
134 dst[di++] = src[si++];
137 dst[di++] = src[si++];
140 memset(dst + di, 0, FF_INPUT_BUFFER_PADDING_SIZE);
150 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
151 * 0 if the unit should be skipped, 1 otherwise
153 static int hls_nal_unit(HEVCNAL *nal, AVCodecContext *avctx)
155 GetBitContext *gb = &nal->gb;
158 if (get_bits1(gb) != 0)
159 return AVERROR_INVALIDDATA;
161 nal->type = get_bits(gb, 6);
163 nuh_layer_id = get_bits(gb, 6);
164 nal->temporal_id = get_bits(gb, 3) - 1;
165 if (nal->temporal_id < 0)
166 return AVERROR_INVALIDDATA;
168 av_log(avctx, AV_LOG_DEBUG,
169 "nal_unit_type: %d, nuh_layer_id: %d, temporal_id: %d\n",
170 nal->type, nuh_layer_id, nal->temporal_id);
172 return nuh_layer_id == 0;
176 int ff_hevc_split_packet(HEVCContext *s, HEVCPacket *pkt, const uint8_t *buf, int length,
177 AVCodecContext *avctx, int is_nalff, int nal_length_size)
179 int consumed, ret = 0;
182 while (length >= 4) {
184 int extract_length = 0;
188 for (i = 0; i < nal_length_size; i++)
189 extract_length = (extract_length << 8) | buf[i];
190 buf += nal_length_size;
191 length -= nal_length_size;
193 if (extract_length > length) {
194 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
195 return AVERROR_INVALIDDATA;
198 /* search start code */
199 while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
203 av_log(avctx, AV_LOG_ERROR, "No start code is found.\n");
204 return AVERROR_INVALIDDATA;
210 extract_length = length;
213 if (pkt->nals_allocated < pkt->nb_nals + 1) {
214 int new_size = pkt->nals_allocated + 1;
215 void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
218 return AVERROR(ENOMEM);
221 memset(pkt->nals + pkt->nals_allocated, 0,
222 (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
224 nal = &pkt->nals[pkt->nb_nals];
225 nal->skipped_bytes_pos_size = 1024; // initial buffer size
226 nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
227 if (!nal->skipped_bytes_pos)
228 return AVERROR(ENOMEM);
230 pkt->nals_allocated = new_size;
232 nal = &pkt->nals[pkt->nb_nals];
234 consumed = ff_hevc_extract_rbsp(s, buf, extract_length, nal);
240 ret = init_get_bits8(&nal->gb, nal->data, nal->size);
244 ret = hls_nal_unit(nal, avctx);
247 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",