Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at> | ||
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 | /** | ||
22 | * @file | ||
23 | * bitstream reader API header. | ||
24 | */ | ||
25 | |||
26 | #ifndef AVCODEC_GET_BITS_H | ||
27 | #define AVCODEC_GET_BITS_H | ||
28 | |||
29 | #include <stdint.h> | ||
30 | |||
31 | #include "libavutil/common.h" | ||
32 | #include "libavutil/intreadwrite.h" | ||
33 | #include "libavutil/avassert.h" | ||
34 | |||
35 | #include "defs.h" | ||
36 | #include "mathops.h" | ||
37 | #include "vlc.h" | ||
38 | |||
39 | /* | ||
40 | * Safe bitstream reading: | ||
41 | * optionally, the get_bits API can check to ensure that we | ||
42 | * don't read past input buffer boundaries. This is protected | ||
43 | * with CONFIG_SAFE_BITSTREAM_READER at the global level, and | ||
44 | * then below that with UNCHECKED_BITSTREAM_READER at the per- | ||
45 | * decoder level. This means that decoders that check internally | ||
46 | * can "#define UNCHECKED_BITSTREAM_READER 1" to disable | ||
47 | * overread checks. | ||
48 | * Boundary checking causes a minor performance penalty so for | ||
49 | * applications that won't want/need this, it can be disabled | ||
50 | * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0". | ||
51 | */ | ||
52 | #ifndef UNCHECKED_BITSTREAM_READER | ||
53 | #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER | ||
54 | #endif | ||
55 | |||
56 | #ifndef CACHED_BITSTREAM_READER | ||
57 | #define CACHED_BITSTREAM_READER 0 | ||
58 | #endif | ||
59 | |||
60 | #if CACHED_BITSTREAM_READER | ||
61 | |||
62 | // we always want the LE implementation, to provide get_bits_le() | ||
63 | #define BITSTREAM_LE | ||
64 | |||
65 | #ifndef BITSTREAM_READER_LE | ||
66 | # define BITSTREAM_BE | ||
67 | # define BITSTREAM_DEFAULT_BE | ||
68 | #endif | ||
69 | |||
70 | #include "bitstream.h" | ||
71 | |||
72 | #undef BITSTREAM_LE | ||
73 | #undef BITSTREAM_BE | ||
74 | #undef BITSTREAM_DEFAULT_BE | ||
75 | |||
76 | typedef BitstreamContext GetBitContext; | ||
77 | |||
78 | #define get_bits_count bits_tell | ||
79 | #define get_bits_left bits_left | ||
80 | #define skip_bits_long bits_skip | ||
81 | #define skip_bits bits_skip | ||
82 | #define get_bits bits_read_nz | ||
83 | #define get_bitsz bits_read | ||
84 | #define get_bits_long bits_read | ||
85 | #define get_bits1 bits_read_bit | ||
86 | #define get_bits64 bits_read_64 | ||
87 | #define get_xbits bits_read_xbits | ||
88 | #define get_sbits bits_read_signed_nz | ||
89 | #define get_sbits_long bits_read_signed | ||
90 | #define show_bits bits_peek | ||
91 | #define show_bits_long bits_peek | ||
92 | #define init_get_bits bits_init | ||
93 | #define init_get_bits8 bits_init8 | ||
94 | #define align_get_bits bits_align | ||
95 | #define get_vlc2 bits_read_vlc | ||
96 | #define get_vlc_multi bits_read_vlc_multi | ||
97 | |||
98 | #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size) | ||
99 | #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n) | ||
100 | |||
101 | #define show_bits1(s) bits_peek(s, 1) | ||
102 | #define skip_bits1(s) bits_skip(s, 1) | ||
103 | |||
104 | #define skip_1stop_8data_bits bits_skip_1stop_8data | ||
105 | |||
106 | #else // CACHED_BITSTREAM_READER | ||
107 | |||
108 | typedef struct GetBitContext { | ||
109 | const uint8_t *buffer, *buffer_end; | ||
110 | int index; | ||
111 | int size_in_bits; | ||
112 | int size_in_bits_plus8; | ||
113 | } GetBitContext; | ||
114 | |||
115 | static inline unsigned int get_bits(GetBitContext *s, int n); | ||
116 | static inline void skip_bits(GetBitContext *s, int n); | ||
117 | static inline unsigned int show_bits(GetBitContext *s, int n); | ||
118 | |||
119 | /* Bitstream reader API docs: | ||
120 | * name | ||
121 | * arbitrary name which is used as prefix for the internal variables | ||
122 | * | ||
123 | * gb | ||
124 | * getbitcontext | ||
125 | * | ||
126 | * OPEN_READER(name, gb) | ||
127 | * load gb into local variables | ||
128 | * | ||
129 | * CLOSE_READER(name, gb) | ||
130 | * store local vars in gb | ||
131 | * | ||
132 | * UPDATE_CACHE(name, gb) | ||
133 | * Refill the internal cache from the bitstream. | ||
134 | * After this call at least MIN_CACHE_BITS will be available. | ||
135 | * | ||
136 | * GET_CACHE(name, gb) | ||
137 | * Will output the contents of the internal cache, | ||
138 | * next bit is MSB of 32 or 64 bits (FIXME 64 bits). | ||
139 | * | ||
140 | * SHOW_UBITS(name, gb, num) | ||
141 | * Will return the next num bits. | ||
142 | * | ||
143 | * SHOW_SBITS(name, gb, num) | ||
144 | * Will return the next num bits and do sign extension. | ||
145 | * | ||
146 | * SKIP_BITS(name, gb, num) | ||
147 | * Will skip over the next num bits. | ||
148 | * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER. | ||
149 | * | ||
150 | * SKIP_CACHE(name, gb, num) | ||
151 | * Will remove the next num bits from the cache (note SKIP_COUNTER | ||
152 | * MUST be called before UPDATE_CACHE / CLOSE_READER). | ||
153 | * | ||
154 | * SKIP_COUNTER(name, gb, num) | ||
155 | * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS). | ||
156 | * | ||
157 | * LAST_SKIP_BITS(name, gb, num) | ||
158 | * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER. | ||
159 | * | ||
160 | * BITS_LEFT(name, gb) | ||
161 | * Return the number of bits left | ||
162 | * | ||
163 | * For examples see get_bits, show_bits, skip_bits, get_vlc. | ||
164 | */ | ||
165 | |||
166 | #define MIN_CACHE_BITS 25 | ||
167 | |||
168 | #define OPEN_READER_NOSIZE(name, gb) \ | ||
169 | unsigned int name ## _index = (gb)->index; \ | ||
170 | unsigned int av_unused name ## _cache | ||
171 | |||
172 | #if UNCHECKED_BITSTREAM_READER | ||
173 | #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb) | ||
174 | |||
175 | #define BITS_AVAILABLE(name, gb) 1 | ||
176 | #else | ||
177 | #define OPEN_READER(name, gb) \ | ||
178 | OPEN_READER_NOSIZE(name, gb); \ | ||
179 | unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8 | ||
180 | |||
181 | #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8 | ||
182 | #endif | ||
183 | |||
184 | #define CLOSE_READER(name, gb) (gb)->index = name ## _index | ||
185 | |||
186 | #define UPDATE_CACHE_BE_EXT(name, gb, bits, dst_bits) name ## _cache = \ | ||
187 | AV_RB ## bits((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) >> (bits - dst_bits) | ||
188 | |||
189 | #define UPDATE_CACHE_LE_EXT(name, gb, bits, dst_bits) name ## _cache = \ | ||
190 | (uint ## dst_bits ## _t)(AV_RL ## bits((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)) | ||
191 | |||
192 | /* Using these two macros ensures that 32 bits are available. */ | ||
193 | # define UPDATE_CACHE_LE_32(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 64, 32) | ||
194 | # define UPDATE_CACHE_BE_32(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 64, 32) | ||
195 | |||
196 | # define UPDATE_CACHE_LE(name, gb) UPDATE_CACHE_LE_EXT(name, (gb), 32, 32) | ||
197 | # define UPDATE_CACHE_BE(name, gb) UPDATE_CACHE_BE_EXT(name, (gb), 32, 32) | ||
198 | |||
199 | #ifdef BITSTREAM_READER_LE | ||
200 | |||
201 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb) | ||
202 | # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_LE_32(name, (gb)) | ||
203 | |||
204 | # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num) | ||
205 | |||
206 | #else | ||
207 | |||
208 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb) | ||
209 | # define UPDATE_CACHE_32(name, gb) UPDATE_CACHE_BE_32(name, (gb)) | ||
210 | |||
211 | # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num) | ||
212 | |||
213 | #endif | ||
214 | |||
215 | #if UNCHECKED_BITSTREAM_READER | ||
216 | # define SKIP_COUNTER(name, gb, num) name ## _index += (num) | ||
217 | #else | ||
218 | # define SKIP_COUNTER(name, gb, num) \ | ||
219 | name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num)) | ||
220 | #endif | ||
221 | |||
222 | #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index)) | ||
223 | |||
224 | #define SKIP_BITS(name, gb, num) \ | ||
225 | do { \ | ||
226 | SKIP_CACHE(name, gb, num); \ | ||
227 | SKIP_COUNTER(name, gb, num); \ | ||
228 | } while (0) | ||
229 | |||
230 | #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) | ||
231 | |||
232 | #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num) | ||
233 | #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num) | ||
234 | |||
235 | #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num) | ||
236 | #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num) | ||
237 | |||
238 | #ifdef BITSTREAM_READER_LE | ||
239 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num) | ||
240 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num) | ||
241 | #else | ||
242 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num) | ||
243 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num) | ||
244 | #endif | ||
245 | |||
246 | #define GET_CACHE(name, gb) ((uint32_t) name ## _cache) | ||
247 | |||
248 | |||
249 | 306969153 | static inline int get_bits_count(const GetBitContext *s) | |
250 | { | ||
251 | 306969153 | return s->index; | |
252 | } | ||
253 | |||
254 | /** | ||
255 | * Skips the specified number of bits. | ||
256 | * @param n the number of bits to skip, | ||
257 | * For the UNCHECKED_BITSTREAM_READER this must not cause the distance | ||
258 | * from the start to overflow int32_t. Staying within the bitstream + padding | ||
259 | * is sufficient, too. | ||
260 | */ | ||
261 | 4658068 | static inline void skip_bits_long(GetBitContext *s, int n) | |
262 | { | ||
263 | #if UNCHECKED_BITSTREAM_READER | ||
264 | 2784862 | s->index += n; | |
265 | #else | ||
266 | 1873206 | s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index); | |
267 | #endif | ||
268 | 4658068 | } | |
269 | |||
270 | /** | ||
271 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). | ||
272 | * if MSB not set it is negative | ||
273 | * @param n length in bits | ||
274 | */ | ||
275 | 26897633 | static inline int get_xbits(GetBitContext *s, int n) | |
276 | { | ||
277 | register int sign; | ||
278 | register int32_t cache; | ||
279 | 26897633 | OPEN_READER(re, s); | |
280 | av_assert2(n>0 && n<=25); | ||
281 | 26897633 | UPDATE_CACHE(re, s); | |
282 | 26897633 | cache = GET_CACHE(re, s); | |
283 | 26897633 | sign = ~cache >> 31; | |
284 | 26897633 | LAST_SKIP_BITS(re, s, n); | |
285 | 26897633 | CLOSE_READER(re, s); | |
286 | 26897633 | return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; | |
287 | } | ||
288 | |||
289 | 1534760 | static inline int get_xbits_le(GetBitContext *s, int n) | |
290 | { | ||
291 | register int sign; | ||
292 | register int32_t cache; | ||
293 | 1534760 | OPEN_READER(re, s); | |
294 | av_assert2(n>0 && n<=25); | ||
295 | 1534760 | UPDATE_CACHE_LE(re, s); | |
296 | 1534760 | cache = GET_CACHE(re, s); | |
297 | 1534760 | sign = sign_extend(~cache, n) >> 31; | |
298 | 1534760 | LAST_SKIP_BITS(re, s, n); | |
299 | 1534760 | CLOSE_READER(re, s); | |
300 | 1534760 | return (zero_extend(sign ^ cache, n) ^ sign) - sign; | |
301 | } | ||
302 | |||
303 | 33303623 | static inline int get_sbits(GetBitContext *s, int n) | |
304 | { | ||
305 | register int tmp; | ||
306 | 33303623 | OPEN_READER(re, s); | |
307 | av_assert2(n>0 && n<=25); | ||
308 | 33303623 | UPDATE_CACHE(re, s); | |
309 | 33303623 | tmp = SHOW_SBITS(re, s, n); | |
310 | 33303623 | LAST_SKIP_BITS(re, s, n); | |
311 | 33303623 | CLOSE_READER(re, s); | |
312 | 33303623 | return tmp; | |
313 | } | ||
314 | |||
315 | /** | ||
316 | * Read 1-25 bits. | ||
317 | */ | ||
318 | 269981353 | static inline unsigned int get_bits(GetBitContext *s, int n) | |
319 | { | ||
320 | register unsigned int tmp; | ||
321 | 269981353 | OPEN_READER(re, s); | |
322 | av_assert2(n>0 && n<=25); | ||
323 | 269981353 | UPDATE_CACHE(re, s); | |
324 | 269981353 | tmp = SHOW_UBITS(re, s, n); | |
325 | 269981353 | LAST_SKIP_BITS(re, s, n); | |
326 | 269981353 | CLOSE_READER(re, s); | |
327 | av_assert2(tmp < UINT64_C(1) << n); | ||
328 | 269981353 | return tmp; | |
329 | } | ||
330 | |||
331 | /** | ||
332 | * Read 0-25 bits. | ||
333 | */ | ||
334 | 746532 | static av_always_inline int get_bitsz(GetBitContext *s, int n) | |
335 | { | ||
336 |
2/2✓ Branch 0 taken 668627 times.
✓ Branch 1 taken 77905 times.
|
746532 | return n ? get_bits(s, n) : 0; |
337 | } | ||
338 | |||
339 | 718813 | static inline unsigned int get_bits_le(GetBitContext *s, int n) | |
340 | { | ||
341 | register int tmp; | ||
342 | 718813 | OPEN_READER(re, s); | |
343 | av_assert2(n>0 && n<=25); | ||
344 | 718813 | UPDATE_CACHE_LE(re, s); | |
345 | 718813 | tmp = SHOW_UBITS_LE(re, s, n); | |
346 | 718813 | LAST_SKIP_BITS(re, s, n); | |
347 | 718813 | CLOSE_READER(re, s); | |
348 | 718813 | return tmp; | |
349 | } | ||
350 | |||
351 | /** | ||
352 | * Show 1-25 bits. | ||
353 | */ | ||
354 | 113644741 | static inline unsigned int show_bits(GetBitContext *s, int n) | |
355 | { | ||
356 | register unsigned int tmp; | ||
357 | 113644741 | OPEN_READER_NOSIZE(re, s); | |
358 | av_assert2(n>0 && n<=25); | ||
359 | 113644741 | UPDATE_CACHE(re, s); | |
360 | 113644741 | tmp = SHOW_UBITS(re, s, n); | |
361 | 113644741 | return tmp; | |
362 | } | ||
363 | |||
364 | 110729633 | static inline void skip_bits(GetBitContext *s, int n) | |
365 | { | ||
366 | 110729633 | OPEN_READER(re, s); | |
367 | 110729633 | LAST_SKIP_BITS(re, s, n); | |
368 | 110729633 | CLOSE_READER(re, s); | |
369 | 110729633 | } | |
370 | |||
371 | 459572215 | static inline unsigned int get_bits1(GetBitContext *s) | |
372 | { | ||
373 | 459572215 | unsigned int index = s->index; | |
374 | 459572215 | uint8_t result = s->buffer[index >> 3]; | |
375 | #ifdef BITSTREAM_READER_LE | ||
376 | 265269732 | result >>= index & 7; | |
377 | 265269732 | result &= 1; | |
378 | #else | ||
379 | 194302483 | result <<= index & 7; | |
380 | 194302483 | result >>= 8 - 1; | |
381 | #endif | ||
382 | #if !UNCHECKED_BITSTREAM_READER | ||
383 |
2/2✓ Branch 0 taken 415863394 times.
✓ Branch 1 taken 15231 times.
|
415878625 | if (s->index < s->size_in_bits_plus8) |
384 | #endif | ||
385 | 459556984 | index++; | |
386 | 459572215 | s->index = index; | |
387 | |||
388 | 459572215 | return result; | |
389 | } | ||
390 | |||
391 | 43299 | static inline unsigned int show_bits1(GetBitContext *s) | |
392 | { | ||
393 | 43299 | return show_bits(s, 1); | |
394 | } | ||
395 | |||
396 | 297329 | static inline void skip_bits1(GetBitContext *s) | |
397 | { | ||
398 | 297329 | skip_bits(s, 1); | |
399 | 297329 | } | |
400 | |||
401 | /** | ||
402 | * Read 0-32 bits. | ||
403 | */ | ||
404 | 57202225 | static inline unsigned int get_bits_long(GetBitContext *s, int n) | |
405 | { | ||
406 | av_assert2(n>=0 && n<=32); | ||
407 |
2/2✓ Branch 0 taken 3404960 times.
✓ Branch 1 taken 53797265 times.
|
57202225 | if (!n) { |
408 | 3404960 | return 0; | |
409 | } else if ((!HAVE_FAST_64BIT || av_builtin_constant_p(n <= MIN_CACHE_BITS)) | ||
410 | && n <= MIN_CACHE_BITS) { | ||
411 | return get_bits(s, n); | ||
412 | } else { | ||
413 | #if HAVE_FAST_64BIT | ||
414 | unsigned tmp; | ||
415 | 53797265 | OPEN_READER(re, s); | |
416 | 53797265 | UPDATE_CACHE_32(re, s); | |
417 | 53797265 | tmp = SHOW_UBITS(re, s, n); | |
418 | 53797265 | LAST_SKIP_BITS(re, s, n); | |
419 | 53797265 | CLOSE_READER(re, s); | |
420 | 53797265 | return tmp; | |
421 | #else | ||
422 | #ifdef BITSTREAM_READER_LE | ||
423 | unsigned ret = get_bits(s, 16); | ||
424 | return ret | (get_bits(s, n - 16) << 16); | ||
425 | #else | ||
426 | unsigned ret = get_bits(s, 16) << (n - 16); | ||
427 | return ret | get_bits(s, n - 16); | ||
428 | #endif | ||
429 | #endif | ||
430 | } | ||
431 | } | ||
432 | |||
433 | /** | ||
434 | * Read 0-64 bits. | ||
435 | */ | ||
436 | 11437 | static inline uint64_t get_bits64(GetBitContext *s, int n) | |
437 | { | ||
438 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 5062 times.
|
11437 | if (n <= 32) { |
439 | 6375 | return get_bits_long(s, n); | |
440 | } else { | ||
441 | #ifdef BITSTREAM_READER_LE | ||
442 | 1 | uint64_t ret = get_bits_long(s, 32); | |
443 | 1 | return ret | (uint64_t) get_bits_long(s, n - 32) << 32; | |
444 | #else | ||
445 | 5061 | uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32; | |
446 | 5061 | return ret | get_bits_long(s, 32); | |
447 | #endif | ||
448 | } | ||
449 | } | ||
450 | |||
451 | /** | ||
452 | * Read 0-32 bits as a signed integer. | ||
453 | */ | ||
454 | 4766758 | static inline int get_sbits_long(GetBitContext *s, int n) | |
455 | { | ||
456 | // sign_extend(x, 0) is undefined | ||
457 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4766758 times.
|
4766758 | if (!n) |
458 | ✗ | return 0; | |
459 | |||
460 | 4766758 | return sign_extend(get_bits_long(s, n), n); | |
461 | } | ||
462 | |||
463 | /** | ||
464 | * Read 0-64 bits as a signed integer. | ||
465 | */ | ||
466 | 4624 | static inline int64_t get_sbits64(GetBitContext *s, int n) | |
467 | { | ||
468 | // sign_extend(x, 0) is undefined | ||
469 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4624 times.
|
4624 | if (!n) |
470 | ✗ | return 0; | |
471 | |||
472 | 4624 | return sign_extend64(get_bits64(s, n), n); | |
473 | } | ||
474 | |||
475 | /** | ||
476 | * Show 0-32 bits. | ||
477 | */ | ||
478 | 3464405 | static inline unsigned int show_bits_long(GetBitContext *s, int n) | |
479 | { | ||
480 |
2/2✓ Branch 0 taken 18473 times.
✓ Branch 1 taken 3445932 times.
|
3464405 | if (n <= MIN_CACHE_BITS) { |
481 | 18473 | return show_bits(s, n); | |
482 | } else { | ||
483 | 3445932 | GetBitContext gb = *s; | |
484 | 3445932 | return get_bits_long(&gb, n); | |
485 | } | ||
486 | } | ||
487 | |||
488 | |||
489 | /** | ||
490 | * Initialize GetBitContext. | ||
491 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
492 | * larger than the actual read bits because some optimized bitstream | ||
493 | * readers read 32 or 64 bit at once and could read over the end | ||
494 | * @param bit_size the size of the buffer in bits | ||
495 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
496 | */ | ||
497 | 29831398 | static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, | |
498 | int bit_size) | ||
499 | { | ||
500 | int buffer_size; | ||
501 | 29831398 | int ret = 0; | |
502 | |||
503 |
4/6✓ Branch 0 taken 29831398 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29831398 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 29831394 times.
|
29831398 | if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) { |
504 | 4 | bit_size = 0; | |
505 | 4 | buffer = NULL; | |
506 | 4 | ret = AVERROR_INVALIDDATA; | |
507 | } | ||
508 | |||
509 | 29831398 | buffer_size = (bit_size + 7) >> 3; | |
510 | |||
511 | 29831398 | s->buffer = buffer; | |
512 | 29831398 | s->size_in_bits = bit_size; | |
513 | 29831398 | s->size_in_bits_plus8 = bit_size + 8; | |
514 | 29831398 | s->buffer_end = buffer + buffer_size; | |
515 | 29831398 | s->index = 0; | |
516 | |||
517 | 29831398 | return ret; | |
518 | } | ||
519 | |||
520 | /** | ||
521 | * Initialize GetBitContext. | ||
522 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
523 | * larger than the actual read bits because some optimized bitstream | ||
524 | * readers read 32 or 64 bit at once and could read over the end | ||
525 | * @param byte_size the size of the buffer in bytes | ||
526 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
527 | */ | ||
528 | 684454 | static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer, | |
529 | int byte_size) | ||
530 | { | ||
531 |
2/4✓ Branch 0 taken 684454 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 684454 times.
|
684454 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
532 | ✗ | byte_size = -1; | |
533 | 684454 | return init_get_bits(s, buffer, byte_size * 8); | |
534 | } | ||
535 | |||
536 | 200 | static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, | |
537 | int byte_size) | ||
538 | { | ||
539 |
2/4✓ Branch 0 taken 200 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 200 times.
|
200 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
540 | ✗ | byte_size = -1; | |
541 | 200 | return init_get_bits(s, buffer, byte_size * 8); | |
542 | } | ||
543 | |||
544 | 254067 | static inline const uint8_t *align_get_bits(GetBitContext *s) | |
545 | { | ||
546 | 254067 | int n = -get_bits_count(s) & 7; | |
547 |
2/2✓ Branch 0 taken 70152 times.
✓ Branch 1 taken 183915 times.
|
254067 | if (n) |
548 | 70152 | skip_bits(s, n); | |
549 | 254067 | return s->buffer + (s->index >> 3); | |
550 | } | ||
551 | |||
552 | /** | ||
553 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
554 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
555 | * is undefined. | ||
556 | */ | ||
557 | #define GET_VLC(code, name, gb, table, bits, max_depth) \ | ||
558 | do { \ | ||
559 | int n, nb_bits; \ | ||
560 | unsigned int index; \ | ||
561 | \ | ||
562 | index = SHOW_UBITS(name, gb, bits); \ | ||
563 | code = table[index].sym; \ | ||
564 | n = table[index].len; \ | ||
565 | \ | ||
566 | if (max_depth > 1 && n < 0) { \ | ||
567 | LAST_SKIP_BITS(name, gb, bits); \ | ||
568 | UPDATE_CACHE(name, gb); \ | ||
569 | \ | ||
570 | nb_bits = -n; \ | ||
571 | \ | ||
572 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ | ||
573 | code = table[index].sym; \ | ||
574 | n = table[index].len; \ | ||
575 | if (max_depth > 2 && n < 0) { \ | ||
576 | LAST_SKIP_BITS(name, gb, nb_bits); \ | ||
577 | UPDATE_CACHE(name, gb); \ | ||
578 | \ | ||
579 | nb_bits = -n; \ | ||
580 | \ | ||
581 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ | ||
582 | code = table[index].sym; \ | ||
583 | n = table[index].len; \ | ||
584 | } \ | ||
585 | } \ | ||
586 | SKIP_BITS(name, gb, n); \ | ||
587 | } while (0) | ||
588 | |||
589 | #define GET_RL_VLC(level, run, name, gb, table, bits, \ | ||
590 | max_depth, need_update) \ | ||
591 | do { \ | ||
592 | int n, nb_bits; \ | ||
593 | unsigned int index; \ | ||
594 | \ | ||
595 | index = SHOW_UBITS(name, gb, bits); \ | ||
596 | level = table[index].level; \ | ||
597 | n = table[index].len; \ | ||
598 | \ | ||
599 | if (max_depth > 1 && n < 0) { \ | ||
600 | SKIP_BITS(name, gb, bits); \ | ||
601 | if (need_update) { \ | ||
602 | UPDATE_CACHE(name, gb); \ | ||
603 | } \ | ||
604 | \ | ||
605 | nb_bits = -n; \ | ||
606 | \ | ||
607 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ | ||
608 | level = table[index].level; \ | ||
609 | n = table[index].len; \ | ||
610 | if (max_depth > 2 && n < 0) { \ | ||
611 | LAST_SKIP_BITS(name, gb, nb_bits); \ | ||
612 | if (need_update) { \ | ||
613 | UPDATE_CACHE(name, gb); \ | ||
614 | } \ | ||
615 | nb_bits = -n; \ | ||
616 | \ | ||
617 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ | ||
618 | level = table[index].level; \ | ||
619 | n = table[index].len; \ | ||
620 | } \ | ||
621 | } \ | ||
622 | run = table[index].run; \ | ||
623 | SKIP_BITS(name, gb, n); \ | ||
624 | } while (0) | ||
625 | |||
626 | /** | ||
627 | * Parse a vlc code. | ||
628 | * @param bits is the number of bits which will be read at once, must be | ||
629 | * identical to nb_bits in vlc_init() | ||
630 | * @param max_depth is the number of times bits bits must be read to completely | ||
631 | * read the longest vlc code | ||
632 | * = (max_vlc_length + bits - 1) / bits | ||
633 | * @returns the code parsed or -1 if no vlc matches | ||
634 | */ | ||
635 | 291792456 | static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, | |
636 | int bits, int max_depth) | ||
637 | { | ||
638 | int code; | ||
639 | |||
640 | 291792456 | OPEN_READER(re, s); | |
641 | 291792456 | UPDATE_CACHE(re, s); | |
642 | |||
643 |
10/10✓ Branch 0 taken 7684008 times.
✓ Branch 1 taken 181305067 times.
✓ Branch 2 taken 103157472 times.
✓ Branch 3 taken 32534987 times.
✓ Branch 4 taken 146957050 times.
✓ Branch 5 taken 103091 times.
✓ Branch 6 taken 22893750 times.
✓ Branch 7 taken 2562320 times.
✓ Branch 8 taken 630124 times.
✓ Branch 9 taken 22263056 times.
|
291792456 | GET_VLC(code, re, s, table, bits, max_depth); |
644 | |||
645 | 291792456 | CLOSE_READER(re, s); | |
646 | |||
647 | 291792456 | return code; | |
648 | } | ||
649 | |||
650 | static inline int get_vlc_multi(GetBitContext *s, uint8_t *dst, | ||
651 | const VLC_MULTI_ELEM *const Jtable, | ||
652 | const VLCElem *const table, | ||
653 | const int bits, const int max_depth, | ||
654 | const int symbols_size) | ||
655 | { | ||
656 | dst[0] = get_vlc2(s, table, bits, max_depth); | ||
657 | return 1; | ||
658 | } | ||
659 | |||
660 | 378881 | static inline int decode012(GetBitContext *gb) | |
661 | { | ||
662 | int n; | ||
663 | 378881 | n = get_bits1(gb); | |
664 |
2/2✓ Branch 0 taken 263934 times.
✓ Branch 1 taken 114947 times.
|
378881 | if (n == 0) |
665 | 263934 | return 0; | |
666 | else | ||
667 | 114947 | return get_bits1(gb) + 1; | |
668 | } | ||
669 | |||
670 | 191971 | static inline int decode210(GetBitContext *gb) | |
671 | { | ||
672 |
2/2✓ Branch 1 taken 135836 times.
✓ Branch 2 taken 56135 times.
|
191971 | if (get_bits1(gb)) |
673 | 135836 | return 0; | |
674 | else | ||
675 | 56135 | return 2 - get_bits1(gb); | |
676 | } | ||
677 | |||
678 | 271263629 | static inline int get_bits_left(GetBitContext *gb) | |
679 | { | ||
680 | 271263629 | return gb->size_in_bits - get_bits_count(gb); | |
681 | } | ||
682 | |||
683 | 134771 | static inline int skip_1stop_8data_bits(GetBitContext *gb) | |
684 | { | ||
685 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 134771 times.
|
134771 | if (get_bits_left(gb) <= 0) |
686 | ✗ | return AVERROR_INVALIDDATA; | |
687 | |||
688 |
2/2✓ Branch 1 taken 3114 times.
✓ Branch 2 taken 134771 times.
|
137885 | while (get_bits1(gb)) { |
689 | 3114 | skip_bits(gb, 8); | |
690 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3114 times.
|
3114 | if (get_bits_left(gb) <= 0) |
691 | ✗ | return AVERROR_INVALIDDATA; | |
692 | } | ||
693 | |||
694 | 134771 | return 0; | |
695 | } | ||
696 | |||
697 | #endif // CACHED_BITSTREAM_READER | ||
698 | |||
699 | #endif /* AVCODEC_GET_BITS_H */ | ||
700 |