]> git.sesse.net Git - ffmpeg/blob - libavcodec/h2645_parse.c
Merge commit '1bd986ed4b0e95ded368a8eeb5c044853c090f9b'
[ffmpeg] / libavcodec / h2645_parse.c
1 /*
2  * H.264/HEVC common parsing code
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <string.h>
22
23 #include "config.h"
24
25 #include "libavutil/intmath.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28
29 #include "bytestream.h"
30 #include "hevc.h"
31 #include "h2645_parse.h"
32
33 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
34                           H2645NAL *nal, int small_padding)
35 {
36     int i, si, di;
37     uint8_t *dst;
38     int64_t padding = small_padding ? 0 : MAX_MBPAIR_SIZE;
39
40     nal->skipped_bytes = 0;
41 #define STARTCODE_TEST                                                  \
42         if (i + 2 < length && src[i + 1] == 0 && src[i + 2] <= 3) {     \
43             if (src[i + 2] != 3 && src[i + 2] != 0) {                   \
44                 /* startcode, so we must be past the end */             \
45                 length = i;                                             \
46             }                                                           \
47             break;                                                      \
48         }
49 #if HAVE_FAST_UNALIGNED
50 #define FIND_FIRST_ZERO                                                 \
51         if (i > 0 && !src[i])                                           \
52             i--;                                                        \
53         while (src[i])                                                  \
54             i++
55 #if HAVE_FAST_64BIT
56     for (i = 0; i + 1 < length; i += 9) {
57         if (!((~AV_RN64A(src + i) &
58                (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
59               0x8000800080008080ULL))
60             continue;
61         FIND_FIRST_ZERO;
62         STARTCODE_TEST;
63         i -= 7;
64     }
65 #else
66     for (i = 0; i + 1 < length; i += 5) {
67         if (!((~AV_RN32A(src + i) &
68                (AV_RN32A(src + i) - 0x01000101U)) &
69               0x80008080U))
70             continue;
71         FIND_FIRST_ZERO;
72         STARTCODE_TEST;
73         i -= 3;
74     }
75 #endif /* HAVE_FAST_64BIT */
76 #else
77     for (i = 0; i + 1 < length; i += 2) {
78         if (src[i])
79             continue;
80         if (i > 0 && src[i - 1] == 0)
81             i--;
82         STARTCODE_TEST;
83     }
84 #endif /* HAVE_FAST_UNALIGNED */
85
86     if (i >= length - 1 && small_padding) { // no escaped 0
87         nal->data     =
88         nal->raw_data = src;
89         nal->size     =
90         nal->raw_size = length;
91         return length;
92     } else if (i > length)
93         i = length;
94
95     av_fast_padded_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
96                           length + padding);
97     if (!nal->rbsp_buffer)
98         return AVERROR(ENOMEM);
99
100     dst = nal->rbsp_buffer;
101
102     memcpy(dst, src, i);
103     si = di = i;
104     while (si + 2 < length) {
105         // remove escapes (very rare 1:2^22)
106         if (src[si + 2] > 3) {
107             dst[di++] = src[si++];
108             dst[di++] = src[si++];
109         } else if (src[si] == 0 && src[si + 1] == 0 && src[si + 2] != 0) {
110             if (src[si + 2] == 3) { // escape
111                 dst[di++] = 0;
112                 dst[di++] = 0;
113                 si       += 3;
114
115                 if (nal->skipped_bytes_pos) {
116                     nal->skipped_bytes++;
117                     if (nal->skipped_bytes_pos_size < nal->skipped_bytes) {
118                         nal->skipped_bytes_pos_size *= 2;
119                         av_assert0(nal->skipped_bytes_pos_size >= nal->skipped_bytes);
120                         av_reallocp_array(&nal->skipped_bytes_pos,
121                                 nal->skipped_bytes_pos_size,
122                                 sizeof(*nal->skipped_bytes_pos));
123                         if (!nal->skipped_bytes_pos) {
124                             nal->skipped_bytes_pos_size = 0;
125                             return AVERROR(ENOMEM);
126                         }
127                     }
128                     if (nal->skipped_bytes_pos)
129                         nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
130                 }
131                 continue;
132             } else // next start code
133                 goto nsc;
134         }
135
136         dst[di++] = src[si++];
137     }
138     while (si < length)
139         dst[di++] = src[si++];
140
141 nsc:
142     memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
143
144     nal->data = dst;
145     nal->size = di;
146     nal->raw_data = src;
147     nal->raw_size = si;
148     return si;
149 }
150
151 static const char *nal_unit_name(int nal_type)
152 {
153     switch(nal_type) {
154     case HEVC_NAL_TRAIL_N    : return "TRAIL_N";
155     case HEVC_NAL_TRAIL_R    : return "TRAIL_R";
156     case HEVC_NAL_TSA_N      : return "TSA_N";
157     case HEVC_NAL_TSA_R      : return "TSA_R";
158     case HEVC_NAL_STSA_N     : return "STSA_N";
159     case HEVC_NAL_STSA_R     : return "STSA_R";
160     case HEVC_NAL_RADL_N     : return "RADL_N";
161     case HEVC_NAL_RADL_R     : return "RADL_R";
162     case HEVC_NAL_RASL_N     : return "RASL_N";
163     case HEVC_NAL_RASL_R     : return "RASL_R";
164     case HEVC_NAL_BLA_W_LP   : return "BLA_W_LP";
165     case HEVC_NAL_BLA_W_RADL : return "BLA_W_RADL";
166     case HEVC_NAL_BLA_N_LP   : return "BLA_N_LP";
167     case HEVC_NAL_IDR_W_RADL : return "IDR_W_RADL";
168     case HEVC_NAL_IDR_N_LP   : return "IDR_N_LP";
169     case HEVC_NAL_CRA_NUT    : return "CRA_NUT";
170     case HEVC_NAL_VPS        : return "VPS";
171     case HEVC_NAL_SPS        : return "SPS";
172     case HEVC_NAL_PPS        : return "PPS";
173     case HEVC_NAL_AUD        : return "AUD";
174     case HEVC_NAL_EOS_NUT    : return "EOS_NUT";
175     case HEVC_NAL_EOB_NUT    : return "EOB_NUT";
176     case HEVC_NAL_FD_NUT     : return "FD_NUT";
177     case HEVC_NAL_SEI_PREFIX : return "SEI_PREFIX";
178     case HEVC_NAL_SEI_SUFFIX : return "SEI_SUFFIX";
179     default : return "?";
180     }
181 }
182
183 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
184 {
185     int size = nal->size;
186     int v;
187
188     while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
189         size--;
190
191     if (!size)
192         return 0;
193
194     v = nal->data[size - 1];
195
196     if (size > INT_MAX / 8)
197         return AVERROR(ERANGE);
198     size *= 8;
199
200     /* remove the stop bit and following trailing zeros,
201      * or nothing for damaged bitstreams */
202     if (v)
203         size -= ff_ctz(v) + 1;
204
205     return size;
206 }
207
208 /**
209  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
210  * 0 if the unit should be skipped, 1 otherwise
211  */
212 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
213 {
214     GetBitContext *gb = &nal->gb;
215     int nuh_layer_id;
216
217     if (get_bits1(gb) != 0)
218         return AVERROR_INVALIDDATA;
219
220     nal->type = get_bits(gb, 6);
221
222     nuh_layer_id   = get_bits(gb, 6);
223     nal->temporal_id = get_bits(gb, 3) - 1;
224     if (nal->temporal_id < 0)
225         return AVERROR_INVALIDDATA;
226
227     av_log(logctx, AV_LOG_DEBUG,
228            "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
229            nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
230
231     return nuh_layer_id == 0;
232 }
233
234 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
235 {
236     GetBitContext *gb = &nal->gb;
237
238     if (get_bits1(gb) != 0)
239         return AVERROR_INVALIDDATA;
240
241     nal->ref_idc = get_bits(gb, 2);
242     nal->type    = get_bits(gb, 5);
243
244     av_log(logctx, AV_LOG_DEBUG,
245            "nal_unit_type: %d, nal_ref_idc: %d\n",
246            nal->type, nal->ref_idc);
247
248     return 1;
249 }
250
251 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
252 {
253     int i = 0;
254
255     if (buf + 3 >= next_avc)
256         return next_avc - buf;
257
258     while (buf + i + 3 < next_avc) {
259         if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
260             break;
261         i++;
262     }
263     return i + 3;
264 }
265
266 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
267                           void *logctx, int is_nalff, int nal_length_size,
268                           enum AVCodecID codec_id, int small_padding)
269 {
270     GetByteContext bc;
271     int consumed, ret = 0;
272     int next_avc = is_nalff ? 0 : length;
273
274     bytestream2_init(&bc, buf, length);
275
276     pkt->nb_nals = 0;
277     while (bytestream2_get_bytes_left(&bc) >= 4) {
278         H2645NAL *nal;
279         int extract_length = 0;
280         int skip_trailing_zeros = 1;
281
282         if (bytestream2_tell(&bc) == next_avc) {
283             int i = 0;
284             extract_length = get_nalsize(nal_length_size,
285                                          bc.buffer, bytestream2_get_bytes_left(&bc), &i, logctx);
286             if (extract_length < 0)
287                 return extract_length;
288
289             bytestream2_skip(&bc, nal_length_size);
290
291             next_avc = bytestream2_tell(&bc) + extract_length;
292         } else {
293             int buf_index;
294
295             if (bytestream2_tell(&bc) > next_avc)
296                 av_log(logctx, AV_LOG_WARNING, "Exceeded next NALFF position, re-syncing.\n");
297
298             /* search start code */
299             buf_index = find_next_start_code(bc.buffer, buf + next_avc);
300
301             bytestream2_skip(&bc, buf_index);
302
303             if (!bytestream2_get_bytes_left(&bc)) {
304                 if (pkt->nb_nals > 0) {
305                     // No more start codes: we discarded some irrelevant
306                     // bytes at the end of the packet.
307                     return 0;
308                 } else {
309                     av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
310                     return AVERROR_INVALIDDATA;
311                 }
312             }
313
314             extract_length = FFMIN(bytestream2_get_bytes_left(&bc), next_avc - bytestream2_tell(&bc));
315
316             if (bytestream2_tell(&bc) >= next_avc) {
317                 /* skip to the start of the next NAL */
318                 bytestream2_skip(&bc, next_avc - bytestream2_tell(&bc));
319                 continue;
320             }
321         }
322
323         if (pkt->nals_allocated < pkt->nb_nals + 1) {
324             int new_size = pkt->nals_allocated + 1;
325             void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
326
327             if (!tmp)
328                 return AVERROR(ENOMEM);
329
330             pkt->nals = tmp;
331             memset(pkt->nals + pkt->nals_allocated, 0,
332                    (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
333
334             nal = &pkt->nals[pkt->nb_nals];
335             nal->skipped_bytes_pos_size = 1024; // initial buffer size
336             nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
337             if (!nal->skipped_bytes_pos)
338                 return AVERROR(ENOMEM);
339
340             pkt->nals_allocated = new_size;
341         }
342         nal = &pkt->nals[pkt->nb_nals];
343
344         consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, nal, small_padding);
345         if (consumed < 0)
346             return consumed;
347
348         if (is_nalff && (extract_length != consumed) && extract_length)
349             av_log(logctx, AV_LOG_DEBUG,
350                    "NALFF: Consumed only %d bytes instead of %d\n",
351                    consumed, extract_length);
352
353         pkt->nb_nals++;
354
355         bytestream2_skip(&bc, consumed);
356
357         /* see commit 3566042a0 */
358         if (bytestream2_get_bytes_left(&bc) >= 4 &&
359             bytestream2_peek_be32(&bc) == 0x000001E0)
360             skip_trailing_zeros = 0;
361
362         nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
363
364         ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
365         if (ret < 0)
366             return ret;
367
368         if (codec_id == AV_CODEC_ID_HEVC)
369             ret = hevc_parse_nal_header(nal, logctx);
370         else
371             ret = h264_parse_nal_header(nal, logctx);
372         if (ret <= 0 || nal->size <= 0) {
373             if (ret < 0) {
374                 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
375                        nal->type);
376             }
377             pkt->nb_nals--;
378         }
379     }
380
381     return 0;
382 }
383
384 void ff_h2645_packet_uninit(H2645Packet *pkt)
385 {
386     int i;
387     for (i = 0; i < pkt->nals_allocated; i++) {
388         av_freep(&pkt->nals[i].rbsp_buffer);
389         av_freep(&pkt->nals[i].skipped_bytes_pos);
390     }
391     av_freep(&pkt->nals);
392     pkt->nals_allocated = 0;
393 }