h2645_parse: add support for parsing h264
[libav.git] / libavcodec / h2645_parse.c
1 /*
2 * H.264/HEVC common parsing code
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <string.h>
22
23 #include "config.h"
24
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mem.h"
27
28 #include "h2645_parse.h"
29
30 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
31 * between these functions would be nice. */
32 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
33 H2645NAL *nal)
34 {
35 int i, si, di;
36 uint8_t *dst;
37
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 */ \
42 length = i; \
43 } \
44 break; \
45 }
46 #if HAVE_FAST_UNALIGNED
47 #define FIND_FIRST_ZERO \
48 if (i > 0 && !src[i]) \
49 i--; \
50 while (src[i]) \
51 i++
52 #if HAVE_FAST_64BIT
53 for (i = 0; i + 1 < length; i += 9) {
54 if (!((~AV_RN64A(src + i) &
55 (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
56 0x8000800080008080ULL))
57 continue;
58 FIND_FIRST_ZERO;
59 STARTCODE_TEST;
60 i -= 7;
61 }
62 #else
63 for (i = 0; i + 1 < length; i += 5) {
64 if (!((~AV_RN32A(src + i) &
65 (AV_RN32A(src + i) - 0x01000101U)) &
66 0x80008080U))
67 continue;
68 FIND_FIRST_ZERO;
69 STARTCODE_TEST;
70 i -= 3;
71 }
72 #endif /* HAVE_FAST_64BIT */
73 #else
74 for (i = 0; i + 1 < length; i += 2) {
75 if (src[i])
76 continue;
77 if (i > 0 && src[i - 1] == 0)
78 i--;
79 STARTCODE_TEST;
80 }
81 #endif /* HAVE_FAST_UNALIGNED */
82
83 if (i >= length - 1) { // no escaped 0
84 nal->data =
85 nal->raw_data = src;
86 nal->size =
87 nal->raw_size = length;
88 return length;
89 }
90
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);
95
96 dst = nal->rbsp_buffer;
97
98 memcpy(dst, src, i);
99 si = di = i;
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
107 dst[di++] = 0;
108 dst[di++] = 0;
109 si += 3;
110
111 continue;
112 } else // next start code
113 goto nsc;
114 }
115
116 dst[di++] = src[si++];
117 }
118 while (si < length)
119 dst[di++] = src[si++];
120
121 nsc:
122 memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
123
124 nal->data = dst;
125 nal->size = di;
126 nal->raw_data = src;
127 nal->raw_size = si;
128 return si;
129 }
130
131 /**
132 * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
133 * 0 if the unit should be skipped, 1 otherwise
134 */
135 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
136 {
137 GetBitContext *gb = &nal->gb;
138 int nuh_layer_id;
139
140 if (get_bits1(gb) != 0)
141 return AVERROR_INVALIDDATA;
142
143 nal->type = get_bits(gb, 6);
144
145 nuh_layer_id = get_bits(gb, 6);
146 nal->temporal_id = get_bits(gb, 3) - 1;
147 if (nal->temporal_id < 0)
148 return AVERROR_INVALIDDATA;
149
150 av_log(logctx, AV_LOG_DEBUG,
151 "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
152 nal->type, nuh_layer_id, nal->temporal_id);
153
154 return nuh_layer_id == 0;
155 }
156
157 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
158 {
159 GetBitContext *gb = &nal->gb;
160
161 if (get_bits1(gb) != 0)
162 return AVERROR_INVALIDDATA;
163
164 nal->ref_idc = get_bits(gb, 2);
165 nal->type = get_bits(gb, 5);
166
167 av_log(logctx, AV_LOG_DEBUG,
168 "nal_unit_type: %d, nal_ref_idc: %d\n",
169 nal->type, nal->ref_idc);
170
171 return 1;
172 }
173
174 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
175 void *logctx, int is_nalff, int nal_length_size,
176 enum AVCodecID codec_id)
177 {
178 int consumed, ret = 0;
179
180 pkt->nb_nals = 0;
181 while (length >= 4) {
182 H2645NAL *nal;
183 int extract_length = 0;
184
185 if (is_nalff) {
186 int i;
187 for (i = 0; i < nal_length_size; i++)
188 extract_length = (extract_length << 8) | buf[i];
189 buf += nal_length_size;
190 length -= nal_length_size;
191
192 if (extract_length > length) {
193 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
194 return AVERROR_INVALIDDATA;
195 }
196 } else {
197 if (buf[2] == 0) {
198 length--;
199 buf++;
200 continue;
201 }
202 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
203 return AVERROR_INVALIDDATA;
204
205 buf += 3;
206 length -= 3;
207 extract_length = length;
208 }
209
210 if (pkt->nals_allocated < pkt->nb_nals + 1) {
211 int new_size = pkt->nals_allocated + 1;
212 H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
213 if (!tmp)
214 return AVERROR(ENOMEM);
215
216 pkt->nals = tmp;
217 memset(pkt->nals + pkt->nals_allocated, 0,
218 (new_size - pkt->nals_allocated) * sizeof(*tmp));
219 pkt->nals_allocated = new_size;
220 }
221 nal = &pkt->nals[pkt->nb_nals++];
222
223 consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);
224 if (consumed < 0)
225 return consumed;
226
227 ret = init_get_bits8(&nal->gb, nal->data, nal->size);
228 if (ret < 0)
229 return ret;
230
231 if (codec_id == AV_CODEC_ID_HEVC)
232 ret = hevc_parse_nal_header(nal, logctx);
233 else
234 ret = h264_parse_nal_header(nal, logctx);
235 if (ret <= 0) {
236 if (ret < 0) {
237 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
238 nal->type);
239 }
240 pkt->nb_nals--;
241 }
242
243 buf += consumed;
244 length -= consumed;
245 }
246
247 return 0;
248 }
249
250 void ff_h2645_packet_uninit(H2645Packet *pkt)
251 {
252 int i;
253 for (i = 0; i < pkt->nals_allocated; i++)
254 av_freep(&pkt->nals[i].rbsp_buffer);
255 av_freep(&pkt->nals);
256 pkt->nals_allocated = 0;
257 }