]> git.sesse.net Git - ffmpeg/blob - libavcodec/h2645_parse.c
Merge commit 'b667252a41fbf5a3f6ea8c67fdbc03db3d748977'
[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/intreadwrite.h"
26 #include "libavutil/mem.h"
27
28 #include "hevc.h"
29 #include "h2645_parse.h"
30
31 /* FIXME: This is adapted from ff_h264_decode_nal, avoiding duplication
32  * between these functions would be nice. */
33 int ff_h2645_extract_rbsp(const uint8_t *src, int length,
34                           H2645NAL *nal)
35 {
36     int i, si, di;
37     uint8_t *dst;
38
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 */             \
44                 length = i;                                             \
45             }                                                           \
46             break;                                                      \
47         }
48 #if HAVE_FAST_UNALIGNED
49 #define FIND_FIRST_ZERO                                                 \
50         if (i > 0 && !src[i])                                           \
51             i--;                                                        \
52         while (src[i])                                                  \
53             i++
54 #if HAVE_FAST_64BIT
55     for (i = 0; i + 1 < length; i += 9) {
56         if (!((~AV_RN64A(src + i) &
57                (AV_RN64A(src + i) - 0x0100010001000101ULL)) &
58               0x8000800080008080ULL))
59             continue;
60         FIND_FIRST_ZERO;
61         STARTCODE_TEST;
62         i -= 7;
63     }
64 #else
65     for (i = 0; i + 1 < length; i += 5) {
66         if (!((~AV_RN32A(src + i) &
67                (AV_RN32A(src + i) - 0x01000101U)) &
68               0x80008080U))
69             continue;
70         FIND_FIRST_ZERO;
71         STARTCODE_TEST;
72         i -= 3;
73     }
74 #endif /* HAVE_FAST_64BIT */
75 #else
76     for (i = 0; i + 1 < length; i += 2) {
77         if (src[i])
78             continue;
79         if (i > 0 && src[i - 1] == 0)
80             i--;
81         STARTCODE_TEST;
82     }
83 #endif /* HAVE_FAST_UNALIGNED */
84
85     if (i >= length - 1) { // no escaped 0
86         nal->data     =
87         nal->raw_data = src;
88         nal->size     =
89         nal->raw_size = length;
90         return length;
91     }
92
93     av_fast_malloc(&nal->rbsp_buffer, &nal->rbsp_buffer_size,
94                    length + AV_INPUT_BUFFER_PADDING_SIZE);
95     if (!nal->rbsp_buffer)
96         return AVERROR(ENOMEM);
97
98     dst = nal->rbsp_buffer;
99
100     memcpy(dst, src, i);
101     si = di = i;
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
109                 dst[di++] = 0;
110                 dst[di++] = 0;
111                 si       += 3;
112
113                 if (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);
124                         }
125                     }
126                     if (nal->skipped_bytes_pos)
127                         nal->skipped_bytes_pos[nal->skipped_bytes-1] = di - 1;
128                 }
129                 continue;
130             } else // next start code
131                 goto nsc;
132         }
133
134         dst[di++] = src[si++];
135     }
136     while (si < length)
137         dst[di++] = src[si++];
138
139 nsc:
140     memset(dst + di, 0, AV_INPUT_BUFFER_PADDING_SIZE);
141
142     nal->data = dst;
143     nal->size = di;
144     nal->raw_data = src;
145     nal->raw_size = si;
146     return si;
147 }
148
149 static const char *nal_unit_name(int nal_type)
150 {
151     switch(nal_type) {
152     case NAL_TRAIL_N    : return "TRAIL_N";
153     case NAL_TRAIL_R    : return "TRAIL_R";
154     case NAL_TSA_N      : return "TSA_N";
155     case NAL_TSA_R      : return "TSA_R";
156     case NAL_STSA_N     : return "STSA_N";
157     case NAL_STSA_R     : return "STSA_R";
158     case NAL_RADL_N     : return "RADL_N";
159     case NAL_RADL_R     : return "RADL_R";
160     case NAL_RASL_N     : return "RASL_N";
161     case NAL_RASL_R     : return "RASL_R";
162     case NAL_BLA_W_LP   : return "BLA_W_LP";
163     case NAL_BLA_W_RADL : return "BLA_W_RADL";
164     case NAL_BLA_N_LP   : return "BLA_N_LP";
165     case NAL_IDR_W_RADL : return "IDR_W_RADL";
166     case NAL_IDR_N_LP   : return "IDR_N_LP";
167     case NAL_CRA_NUT    : return "CRA_NUT";
168     case NAL_VPS        : return "VPS";
169     case NAL_SPS        : return "SPS";
170     case NAL_PPS        : return "PPS";
171     case NAL_AUD        : return "AUD";
172     case NAL_EOS_NUT    : return "EOS_NUT";
173     case NAL_EOB_NUT    : return "EOB_NUT";
174     case NAL_FD_NUT     : return "FD_NUT";
175     case NAL_SEI_PREFIX : return "SEI_PREFIX";
176     case NAL_SEI_SUFFIX : return "SEI_SUFFIX";
177     default : return "?";
178     }
179 }
180
181 /**
182  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
183  * 0 if the unit should be skipped, 1 otherwise
184  */
185 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
186 {
187     GetBitContext *gb = &nal->gb;
188     int nuh_layer_id;
189
190     if (get_bits1(gb) != 0)
191         return AVERROR_INVALIDDATA;
192
193     nal->type = get_bits(gb, 6);
194
195     nuh_layer_id   = get_bits(gb, 6);
196     nal->temporal_id = get_bits(gb, 3) - 1;
197     if (nal->temporal_id < 0)
198         return AVERROR_INVALIDDATA;
199
200     av_log(logctx, AV_LOG_DEBUG,
201            "nal_unit_type: %d(%s), nuh_layer_id: %d, temporal_id: %d\n",
202            nal->type, nal_unit_name(nal->type), nuh_layer_id, nal->temporal_id);
203
204     return nuh_layer_id == 0;
205 }
206
207 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
208 {
209     GetBitContext *gb = &nal->gb;
210
211     if (get_bits1(gb) != 0)
212         return AVERROR_INVALIDDATA;
213
214     nal->ref_idc = get_bits(gb, 2);
215     nal->type    = get_bits(gb, 5);
216
217     av_log(logctx, AV_LOG_DEBUG,
218            "nal_unit_type: %d, nal_ref_idc: %d\n",
219            nal->type, nal->ref_idc);
220
221     return 1;
222 }
223
224 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
225                           void *logctx, int is_nalff, int nal_length_size,
226                           enum AVCodecID codec_id)
227 {
228     int consumed, ret = 0;
229
230     pkt->nb_nals = 0;
231     while (length >= 4) {
232         H2645NAL *nal;
233         int extract_length = 0;
234
235         if (is_nalff) {
236             int i;
237             for (i = 0; i < nal_length_size; i++)
238                 extract_length = (extract_length << 8) | buf[i];
239             buf    += nal_length_size;
240             length -= nal_length_size;
241
242             if (extract_length > length) {
243                 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit size.\n");
244                 return AVERROR_INVALIDDATA;
245             }
246         } else {
247             /* search start code */
248             while (buf[0] != 0 || buf[1] != 0 || buf[2] != 1) {
249                 ++buf;
250                 --length;
251                 if (length < 4) {
252                     if (pkt->nb_nals > 0) {
253                         // No more start codes: we discarded some irrelevant
254                         // bytes at the end of the packet.
255                         return 0;
256                     } else {
257                         av_log(logctx, AV_LOG_ERROR, "No start code is found.\n");
258                         return AVERROR_INVALIDDATA;
259                     }
260                 }
261             }
262
263             buf           += 3;
264             length        -= 3;
265             extract_length = length;
266         }
267
268         if (pkt->nals_allocated < pkt->nb_nals + 1) {
269             int new_size = pkt->nals_allocated + 1;
270             void *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*pkt->nals));
271
272             if (!tmp)
273                 return AVERROR(ENOMEM);
274
275             pkt->nals = tmp;
276             memset(pkt->nals + pkt->nals_allocated, 0,
277                    (new_size - pkt->nals_allocated) * sizeof(*pkt->nals));
278
279             nal = &pkt->nals[pkt->nb_nals];
280             nal->skipped_bytes_pos_size = 1024; // initial buffer size
281             nal->skipped_bytes_pos = av_malloc_array(nal->skipped_bytes_pos_size, sizeof(*nal->skipped_bytes_pos));
282             if (!nal->skipped_bytes_pos)
283                 return AVERROR(ENOMEM);
284
285             pkt->nals_allocated = new_size;
286         }
287         nal = &pkt->nals[pkt->nb_nals];
288
289         consumed = ff_h2645_extract_rbsp(buf, extract_length, nal);
290         if (consumed < 0)
291             return consumed;
292
293         pkt->nb_nals++;
294
295         ret = init_get_bits8(&nal->gb, nal->data, nal->size);
296         if (ret < 0)
297             return ret;
298
299         if (codec_id == AV_CODEC_ID_HEVC)
300             ret = hevc_parse_nal_header(nal, logctx);
301         else
302             ret = h264_parse_nal_header(nal, logctx);
303         if (ret <= 0) {
304             if (ret < 0) {
305                 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
306                        nal->type);
307             }
308             pkt->nb_nals--;
309         }
310
311         buf    += consumed;
312         length -= consumed;
313     }
314
315     return 0;
316 }
317
318 void ff_h2645_packet_uninit(H2645Packet *pkt)
319 {
320     int i;
321     for (i = 0; i < pkt->nals_allocated; i++) {
322         av_freep(&pkt->nals[i].rbsp_buffer);
323         av_freep(&pkt->nals[i].skipped_bytes_pos);
324     }
325     av_freep(&pkt->nals);
326     pkt->nals_allocated = 0;
327 }