]> git.sesse.net Git - ffmpeg/blob - libavcodec/h2645_parse.c
lavc: Drop deprecated VDPAU buffer fields
[ffmpeg] / 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/intmath.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28
29 #include "bytestream.h"
30 #include "h2645_parse.h"
31
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 static int get_bit_length(H2645NAL *nal, int skip_trailing_zeros)
132 {
133     int size = nal->size;
134     int v;
135
136     while (skip_trailing_zeros && size > 0 && nal->data[size - 1] == 0)
137         size--;
138
139     if (!size)
140         return 0;
141
142     v = nal->data[size - 1];
143
144     if (size > INT_MAX / 8)
145         return AVERROR(ERANGE);
146     size *= 8;
147
148     /* remove the stop bit and following trailing zeros,
149      * or nothing for damaged bitstreams */
150     if (v)
151         size -= av_ctz(v) + 1;
152
153     return size;
154 }
155
156 /**
157  * @return AVERROR_INVALIDDATA if the packet is not a valid NAL unit,
158  * 0 if the unit should be skipped, 1 otherwise
159  */
160 static int hevc_parse_nal_header(H2645NAL *nal, void *logctx)
161 {
162     GetBitContext *gb = &nal->gb;
163     int nuh_layer_id;
164
165     if (get_bits1(gb) != 0)
166         return AVERROR_INVALIDDATA;
167
168     nal->type = get_bits(gb, 6);
169
170     nuh_layer_id   = get_bits(gb, 6);
171     nal->temporal_id = get_bits(gb, 3) - 1;
172     if (nal->temporal_id < 0)
173         return AVERROR_INVALIDDATA;
174
175     av_log(logctx, AV_LOG_DEBUG,
176            "nal_unit_type: %d, nuh_layer_id: %dtemporal_id: %d\n",
177            nal->type, nuh_layer_id, nal->temporal_id);
178
179     return nuh_layer_id == 0;
180 }
181
182 static int h264_parse_nal_header(H2645NAL *nal, void *logctx)
183 {
184     GetBitContext *gb = &nal->gb;
185
186     if (get_bits1(gb) != 0)
187         return AVERROR_INVALIDDATA;
188
189     nal->ref_idc = get_bits(gb, 2);
190     nal->type    = get_bits(gb, 5);
191
192     av_log(logctx, AV_LOG_DEBUG,
193            "nal_unit_type: %d, nal_ref_idc: %d\n",
194            nal->type, nal->ref_idc);
195
196     return 1;
197 }
198
199 static int find_next_start_code(const uint8_t *buf, const uint8_t *next_avc)
200 {
201     int i = 0;
202
203     if (buf + 3 >= next_avc)
204         return next_avc - buf;
205
206     while (buf + i + 3 < next_avc) {
207         if (buf[i] == 0 && buf[i + 1] == 0 && buf[i + 2] == 1)
208             break;
209         i++;
210     }
211     return i + 3;
212 }
213
214 int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length,
215                           void *logctx, int is_nalff, int nal_length_size,
216                           enum AVCodecID codec_id)
217 {
218     GetByteContext bc;
219     int consumed, ret = 0;
220     size_t next_avc = is_nalff ? 0 : length;
221
222     bytestream2_init(&bc, buf, length);
223
224     pkt->nb_nals = 0;
225     while (bytestream2_get_bytes_left(&bc) >= 4) {
226         H2645NAL *nal;
227         int extract_length = 0;
228         int skip_trailing_zeros = 1;
229
230         /*
231          * Only parse an AVC1 length field if one is expected at the current
232          * buffer position. There are unfortunately streams with multiple
233          * NAL units covered by the length field. Those NAL units are delimited
234          * by Annex B start code prefixes. ff_h2645_extract_rbsp() detects it
235          * correctly and consumes only the first NAL unit. The additional NAL
236          * units are handled here in the Annex B parsing code.
237          */
238         if (bytestream2_tell(&bc) == next_avc) {
239             int i;
240             for (i = 0; i < nal_length_size; i++)
241                 extract_length = (extract_length << 8) | bytestream2_get_byte(&bc);
242
243             if (extract_length > bytestream2_get_bytes_left(&bc)) {
244                 av_log(logctx, AV_LOG_ERROR,
245                        "Invalid NAL unit size (%d > %d).\n",
246                        extract_length, bytestream2_get_bytes_left(&bc));
247                 return AVERROR_INVALIDDATA;
248             }
249             // keep track of the next AVC1 length field
250             next_avc = bytestream2_tell(&bc) + extract_length;
251         } else {
252             /*
253              * expected to return immediately except for streams with mixed
254              * NAL unit coding
255              */
256             int buf_index = find_next_start_code(bc.buffer, buf + next_avc);
257
258             bytestream2_skip(&bc, buf_index);
259
260             /*
261              * break if an AVC1 length field is expected at the current buffer
262              * position
263              */
264             if (bytestream2_tell(&bc) == next_avc)
265                 continue;
266
267             if (bytestream2_get_bytes_left(&bc) > 0) {
268                 extract_length = bytestream2_get_bytes_left(&bc);
269             } else if (pkt->nb_nals == 0) {
270                 av_log(logctx, AV_LOG_ERROR, "No NAL unit found\n");
271                 return AVERROR_INVALIDDATA;
272             } else {
273                 break;
274             }
275         }
276
277         if (pkt->nals_allocated < pkt->nb_nals + 1) {
278             int new_size = pkt->nals_allocated + 1;
279             H2645NAL *tmp = av_realloc_array(pkt->nals, new_size, sizeof(*tmp));
280             if (!tmp)
281                 return AVERROR(ENOMEM);
282
283             pkt->nals = tmp;
284             memset(pkt->nals + pkt->nals_allocated, 0,
285                    (new_size - pkt->nals_allocated) * sizeof(*tmp));
286             pkt->nals_allocated = new_size;
287         }
288         nal = &pkt->nals[pkt->nb_nals++];
289
290         consumed = ff_h2645_extract_rbsp(bc.buffer, extract_length, nal);
291         if (consumed < 0)
292             return consumed;
293
294         bytestream2_skip(&bc, consumed);
295
296         /* see commit 3566042a0 */
297         if (bytestream2_get_bytes_left(&bc) >= 4 &&
298             bytestream2_peek_be32(&bc) == 0x000001E0)
299             skip_trailing_zeros = 0;
300
301         nal->size_bits = get_bit_length(nal, skip_trailing_zeros);
302
303         ret = init_get_bits(&nal->gb, nal->data, nal->size_bits);
304         if (ret < 0)
305             return ret;
306
307         if (codec_id == AV_CODEC_ID_HEVC)
308             ret = hevc_parse_nal_header(nal, logctx);
309         else
310             ret = h264_parse_nal_header(nal, logctx);
311         if (ret <= 0) {
312             if (ret < 0) {
313                 av_log(logctx, AV_LOG_ERROR, "Invalid NAL unit %d, skipping.\n",
314                        nal->type);
315             }
316             pkt->nb_nals--;
317         }
318     }
319
320     return 0;
321 }
322
323 void ff_h2645_packet_uninit(H2645Packet *pkt)
324 {
325     int i;
326     for (i = 0; i < pkt->nals_allocated; i++)
327         av_freep(&pkt->nals[i].rbsp_buffer);
328     av_freep(&pkt->nals);
329     pkt->nals_allocated = 0;
330 }