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 | |||
97 | #define init_get_bits8_le(s, buffer, byte_size) bits_init8_le((BitstreamContextLE*)s, buffer, byte_size) | ||
98 | #define get_bits_le(s, n) bits_read_le((BitstreamContextLE*)s, n) | ||
99 | |||
100 | #define show_bits1(s) bits_peek(s, 1) | ||
101 | #define skip_bits1(s) bits_skip(s, 1) | ||
102 | |||
103 | #define skip_1stop_8data_bits bits_skip_1stop_8data | ||
104 | |||
105 | #else // CACHED_BITSTREAM_READER | ||
106 | |||
107 | typedef struct GetBitContext { | ||
108 | const uint8_t *buffer, *buffer_end; | ||
109 | int index; | ||
110 | int size_in_bits; | ||
111 | int size_in_bits_plus8; | ||
112 | } GetBitContext; | ||
113 | |||
114 | static inline unsigned int get_bits(GetBitContext *s, int n); | ||
115 | static inline void skip_bits(GetBitContext *s, int n); | ||
116 | static inline unsigned int show_bits(GetBitContext *s, int n); | ||
117 | |||
118 | /* Bitstream reader API docs: | ||
119 | * name | ||
120 | * arbitrary name which is used as prefix for the internal variables | ||
121 | * | ||
122 | * gb | ||
123 | * getbitcontext | ||
124 | * | ||
125 | * OPEN_READER(name, gb) | ||
126 | * load gb into local variables | ||
127 | * | ||
128 | * CLOSE_READER(name, gb) | ||
129 | * store local vars in gb | ||
130 | * | ||
131 | * UPDATE_CACHE(name, gb) | ||
132 | * Refill the internal cache from the bitstream. | ||
133 | * After this call at least MIN_CACHE_BITS will be available. | ||
134 | * | ||
135 | * GET_CACHE(name, gb) | ||
136 | * Will output the contents of the internal cache, | ||
137 | * next bit is MSB of 32 or 64 bits (FIXME 64 bits). | ||
138 | * | ||
139 | * SHOW_UBITS(name, gb, num) | ||
140 | * Will return the next num bits. | ||
141 | * | ||
142 | * SHOW_SBITS(name, gb, num) | ||
143 | * Will return the next num bits and do sign extension. | ||
144 | * | ||
145 | * SKIP_BITS(name, gb, num) | ||
146 | * Will skip over the next num bits. | ||
147 | * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER. | ||
148 | * | ||
149 | * SKIP_CACHE(name, gb, num) | ||
150 | * Will remove the next num bits from the cache (note SKIP_COUNTER | ||
151 | * MUST be called before UPDATE_CACHE / CLOSE_READER). | ||
152 | * | ||
153 | * SKIP_COUNTER(name, gb, num) | ||
154 | * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS). | ||
155 | * | ||
156 | * LAST_SKIP_BITS(name, gb, num) | ||
157 | * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER. | ||
158 | * | ||
159 | * BITS_LEFT(name, gb) | ||
160 | * Return the number of bits left | ||
161 | * | ||
162 | * For examples see get_bits, show_bits, skip_bits, get_vlc. | ||
163 | */ | ||
164 | |||
165 | #if defined LONG_BITSTREAM_READER | ||
166 | # define MIN_CACHE_BITS 32 | ||
167 | #else | ||
168 | # define MIN_CACHE_BITS 25 | ||
169 | #endif | ||
170 | |||
171 | #define OPEN_READER_NOSIZE(name, gb) \ | ||
172 | unsigned int name ## _index = (gb)->index; \ | ||
173 | unsigned int av_unused name ## _cache | ||
174 | |||
175 | #if UNCHECKED_BITSTREAM_READER | ||
176 | #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb) | ||
177 | |||
178 | #define BITS_AVAILABLE(name, gb) 1 | ||
179 | #else | ||
180 | #define OPEN_READER(name, gb) \ | ||
181 | OPEN_READER_NOSIZE(name, gb); \ | ||
182 | unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8 | ||
183 | |||
184 | #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8 | ||
185 | #endif | ||
186 | |||
187 | #define CLOSE_READER(name, gb) (gb)->index = name ## _index | ||
188 | |||
189 | # ifdef LONG_BITSTREAM_READER | ||
190 | |||
191 | # define UPDATE_CACHE_LE(name, gb) name ## _cache = \ | ||
192 | AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) | ||
193 | |||
194 | # define UPDATE_CACHE_BE(name, gb) name ## _cache = \ | ||
195 | AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7)) | ||
196 | |||
197 | #else | ||
198 | |||
199 | # define UPDATE_CACHE_LE(name, gb) name ## _cache = \ | ||
200 | AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) | ||
201 | |||
202 | # define UPDATE_CACHE_BE(name, gb) name ## _cache = \ | ||
203 | AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) | ||
204 | |||
205 | #endif | ||
206 | |||
207 | |||
208 | #ifdef BITSTREAM_READER_LE | ||
209 | |||
210 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb) | ||
211 | |||
212 | # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num) | ||
213 | |||
214 | #else | ||
215 | |||
216 | # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb) | ||
217 | |||
218 | # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num) | ||
219 | |||
220 | #endif | ||
221 | |||
222 | #if UNCHECKED_BITSTREAM_READER | ||
223 | # define SKIP_COUNTER(name, gb, num) name ## _index += (num) | ||
224 | #else | ||
225 | # define SKIP_COUNTER(name, gb, num) \ | ||
226 | name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num)) | ||
227 | #endif | ||
228 | |||
229 | #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index)) | ||
230 | |||
231 | #define SKIP_BITS(name, gb, num) \ | ||
232 | do { \ | ||
233 | SKIP_CACHE(name, gb, num); \ | ||
234 | SKIP_COUNTER(name, gb, num); \ | ||
235 | } while (0) | ||
236 | |||
237 | #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) | ||
238 | |||
239 | #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num) | ||
240 | #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num) | ||
241 | |||
242 | #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num) | ||
243 | #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num) | ||
244 | |||
245 | #ifdef BITSTREAM_READER_LE | ||
246 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num) | ||
247 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num) | ||
248 | #else | ||
249 | # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num) | ||
250 | # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num) | ||
251 | #endif | ||
252 | |||
253 | #define GET_CACHE(name, gb) ((uint32_t) name ## _cache) | ||
254 | |||
255 | |||
256 | 309021189 | static inline int get_bits_count(const GetBitContext *s) | |
257 | { | ||
258 | 309021189 | return s->index; | |
259 | } | ||
260 | |||
261 | /** | ||
262 | * Skips the specified number of bits. | ||
263 | * @param n the number of bits to skip, | ||
264 | * For the UNCHECKED_BITSTREAM_READER this must not cause the distance | ||
265 | * from the start to overflow int32_t. Staying within the bitstream + padding | ||
266 | * is sufficient, too. | ||
267 | */ | ||
268 | 3390151 | static inline void skip_bits_long(GetBitContext *s, int n) | |
269 | { | ||
270 | #if UNCHECKED_BITSTREAM_READER | ||
271 | 2755860 | s->index += n; | |
272 | #else | ||
273 | 634291 | s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index); | |
274 | #endif | ||
275 | 3390151 | } | |
276 | |||
277 | /** | ||
278 | * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB). | ||
279 | * if MSB not set it is negative | ||
280 | * @param n length in bits | ||
281 | */ | ||
282 | 26836244 | static inline int get_xbits(GetBitContext *s, int n) | |
283 | { | ||
284 | register int sign; | ||
285 | register int32_t cache; | ||
286 | 26836244 | OPEN_READER(re, s); | |
287 | av_assert2(n>0 && n<=25); | ||
288 | 26836244 | UPDATE_CACHE(re, s); | |
289 | 26836244 | cache = GET_CACHE(re, s); | |
290 | 26836244 | sign = ~cache >> 31; | |
291 | 26836244 | LAST_SKIP_BITS(re, s, n); | |
292 | 26836244 | CLOSE_READER(re, s); | |
293 | 26836244 | return (NEG_USR32(sign ^ cache, n) ^ sign) - sign; | |
294 | } | ||
295 | |||
296 | 1534760 | static inline int get_xbits_le(GetBitContext *s, int n) | |
297 | { | ||
298 | register int sign; | ||
299 | register int32_t cache; | ||
300 | 1534760 | OPEN_READER(re, s); | |
301 | av_assert2(n>0 && n<=25); | ||
302 | 1534760 | UPDATE_CACHE_LE(re, s); | |
303 | 1534760 | cache = GET_CACHE(re, s); | |
304 | 1534760 | sign = sign_extend(~cache, n) >> 31; | |
305 | 1534760 | LAST_SKIP_BITS(re, s, n); | |
306 | 1534760 | CLOSE_READER(re, s); | |
307 | 1534760 | return (zero_extend(sign ^ cache, n) ^ sign) - sign; | |
308 | } | ||
309 | |||
310 | 33301656 | static inline int get_sbits(GetBitContext *s, int n) | |
311 | { | ||
312 | register int tmp; | ||
313 | 33301656 | OPEN_READER(re, s); | |
314 | av_assert2(n>0 && n<=25); | ||
315 | 33301656 | UPDATE_CACHE(re, s); | |
316 | 33301656 | tmp = SHOW_SBITS(re, s, n); | |
317 | 33301656 | LAST_SKIP_BITS(re, s, n); | |
318 | 33301656 | CLOSE_READER(re, s); | |
319 | 33301656 | return tmp; | |
320 | } | ||
321 | |||
322 | /** | ||
323 | * Read 1-25 bits. | ||
324 | */ | ||
325 | 323780091 | static inline unsigned int get_bits(GetBitContext *s, int n) | |
326 | { | ||
327 | register unsigned int tmp; | ||
328 | 323780091 | OPEN_READER(re, s); | |
329 | av_assert2(n>0 && n<=25); | ||
330 | 323780091 | UPDATE_CACHE(re, s); | |
331 | 323780091 | tmp = SHOW_UBITS(re, s, n); | |
332 | 323780091 | LAST_SKIP_BITS(re, s, n); | |
333 | 323780091 | CLOSE_READER(re, s); | |
334 | av_assert2(tmp < UINT64_C(1) << n); | ||
335 | 323780091 | return tmp; | |
336 | } | ||
337 | |||
338 | /** | ||
339 | * Read 0-25 bits. | ||
340 | */ | ||
341 | 762341 | static av_always_inline int get_bitsz(GetBitContext *s, int n) | |
342 | { | ||
343 |
2/2✓ Branch 0 taken 685680 times.
✓ Branch 1 taken 76661 times.
|
762341 | return n ? get_bits(s, n) : 0; |
344 | } | ||
345 | |||
346 | 718813 | static inline unsigned int get_bits_le(GetBitContext *s, int n) | |
347 | { | ||
348 | register int tmp; | ||
349 | 718813 | OPEN_READER(re, s); | |
350 | av_assert2(n>0 && n<=25); | ||
351 | 718813 | UPDATE_CACHE_LE(re, s); | |
352 | 718813 | tmp = SHOW_UBITS_LE(re, s, n); | |
353 | 718813 | LAST_SKIP_BITS(re, s, n); | |
354 | 718813 | CLOSE_READER(re, s); | |
355 | 718813 | return tmp; | |
356 | } | ||
357 | |||
358 | /** | ||
359 | * Show 1-25 bits. | ||
360 | */ | ||
361 | 114200007 | static inline unsigned int show_bits(GetBitContext *s, int n) | |
362 | { | ||
363 | register unsigned int tmp; | ||
364 | 114200007 | OPEN_READER_NOSIZE(re, s); | |
365 | av_assert2(n>0 && n<=25); | ||
366 | 114200007 | UPDATE_CACHE(re, s); | |
367 | 114200007 | tmp = SHOW_UBITS(re, s, n); | |
368 | 114200007 | return tmp; | |
369 | } | ||
370 | |||
371 | 111390826 | static inline void skip_bits(GetBitContext *s, int n) | |
372 | { | ||
373 | 111390826 | OPEN_READER(re, s); | |
374 | 111390826 | LAST_SKIP_BITS(re, s, n); | |
375 | 111390826 | CLOSE_READER(re, s); | |
376 | 111390826 | } | |
377 | |||
378 | 446929580 | static inline unsigned int get_bits1(GetBitContext *s) | |
379 | { | ||
380 | 446929580 | unsigned int index = s->index; | |
381 | 446929580 | uint8_t result = s->buffer[index >> 3]; | |
382 | #ifdef BITSTREAM_READER_LE | ||
383 | 261126126 | result >>= index & 7; | |
384 | 261126126 | result &= 1; | |
385 | #else | ||
386 | 185803454 | result <<= index & 7; | |
387 | 185803454 | result >>= 8 - 1; | |
388 | #endif | ||
389 | #if !UNCHECKED_BITSTREAM_READER | ||
390 |
2/2✓ Branch 0 taken 404259685 times.
✓ Branch 1 taken 15377 times.
|
404275062 | if (s->index < s->size_in_bits_plus8) |
391 | #endif | ||
392 | 446914203 | index++; | |
393 | 446929580 | s->index = index; | |
394 | |||
395 | 446929580 | return result; | |
396 | } | ||
397 | |||
398 | 43150 | static inline unsigned int show_bits1(GetBitContext *s) | |
399 | { | ||
400 | 43150 | return show_bits(s, 1); | |
401 | } | ||
402 | |||
403 | 282734 | static inline void skip_bits1(GetBitContext *s) | |
404 | { | ||
405 | 282734 | skip_bits(s, 1); | |
406 | 282734 | } | |
407 | |||
408 | /** | ||
409 | * Read 0-32 bits. | ||
410 | */ | ||
411 | 54943175 | static inline unsigned int get_bits_long(GetBitContext *s, int n) | |
412 | { | ||
413 | av_assert2(n>=0 && n<=32); | ||
414 |
2/2✓ Branch 0 taken 3404960 times.
✓ Branch 1 taken 51538215 times.
|
54943175 | if (!n) { |
415 | 3404960 | return 0; | |
416 |
2/2✓ Branch 0 taken 48140627 times.
✓ Branch 1 taken 3397588 times.
|
51538215 | } else if (n <= MIN_CACHE_BITS) { |
417 | 48140627 | return get_bits(s, n); | |
418 | } else { | ||
419 | #ifdef BITSTREAM_READER_LE | ||
420 | 2417 | unsigned ret = get_bits(s, 16); | |
421 | 2417 | return ret | (get_bits(s, n - 16) << 16); | |
422 | #else | ||
423 | 3395171 | unsigned ret = get_bits(s, 16) << (n - 16); | |
424 | 3395171 | return ret | get_bits(s, n - 16); | |
425 | #endif | ||
426 | } | ||
427 | } | ||
428 | |||
429 | /** | ||
430 | * Read 0-64 bits. | ||
431 | */ | ||
432 | 11094 | static inline uint64_t get_bits64(GetBitContext *s, int n) | |
433 | { | ||
434 |
2/2✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 4719 times.
|
11094 | if (n <= 32) { |
435 | 6375 | return get_bits_long(s, n); | |
436 | } else { | ||
437 | #ifdef BITSTREAM_READER_LE | ||
438 | 1 | uint64_t ret = get_bits_long(s, 32); | |
439 | 1 | return ret | (uint64_t) get_bits_long(s, n - 32) << 32; | |
440 | #else | ||
441 | 4718 | uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32; | |
442 | 4718 | return ret | get_bits_long(s, 32); | |
443 | #endif | ||
444 | } | ||
445 | } | ||
446 | |||
447 | /** | ||
448 | * Read 0-32 bits as a signed integer. | ||
449 | */ | ||
450 | 4766298 | static inline int get_sbits_long(GetBitContext *s, int n) | |
451 | { | ||
452 | // sign_extend(x, 0) is undefined | ||
453 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4766298 times.
|
4766298 | if (!n) |
454 | ✗ | return 0; | |
455 | |||
456 | 4766298 | return sign_extend(get_bits_long(s, n), n); | |
457 | } | ||
458 | |||
459 | /** | ||
460 | * Read 0-64 bits as a signed integer. | ||
461 | */ | ||
462 | 4624 | static inline int64_t get_sbits64(GetBitContext *s, int n) | |
463 | { | ||
464 | // sign_extend(x, 0) is undefined | ||
465 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4624 times.
|
4624 | if (!n) |
466 | ✗ | return 0; | |
467 | |||
468 | 4624 | return sign_extend64(get_bits64(s, n), n); | |
469 | } | ||
470 | |||
471 | /** | ||
472 | * Show 0-32 bits. | ||
473 | */ | ||
474 | 3153015 | static inline unsigned int show_bits_long(GetBitContext *s, int n) | |
475 | { | ||
476 |
2/2✓ Branch 0 taken 6799 times.
✓ Branch 1 taken 3146216 times.
|
3153015 | if (n <= MIN_CACHE_BITS) { |
477 | 6799 | return show_bits(s, n); | |
478 | } else { | ||
479 | 3146216 | GetBitContext gb = *s; | |
480 | 3146216 | return get_bits_long(&gb, n); | |
481 | } | ||
482 | } | ||
483 | |||
484 | |||
485 | /** | ||
486 | * Initialize GetBitContext. | ||
487 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
488 | * larger than the actual read bits because some optimized bitstream | ||
489 | * readers read 32 or 64 bit at once and could read over the end | ||
490 | * @param bit_size the size of the buffer in bits | ||
491 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
492 | */ | ||
493 | 28784111 | static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer, | |
494 | int bit_size) | ||
495 | { | ||
496 | int buffer_size; | ||
497 | 28784111 | int ret = 0; | |
498 | |||
499 |
4/6✓ Branch 0 taken 28784111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28784111 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 28784107 times.
|
28784111 | if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) { |
500 | 4 | bit_size = 0; | |
501 | 4 | buffer = NULL; | |
502 | 4 | ret = AVERROR_INVALIDDATA; | |
503 | } | ||
504 | |||
505 | 28784111 | buffer_size = (bit_size + 7) >> 3; | |
506 | |||
507 | 28784111 | s->buffer = buffer; | |
508 | 28784111 | s->size_in_bits = bit_size; | |
509 | 28784111 | s->size_in_bits_plus8 = bit_size + 8; | |
510 | 28784111 | s->buffer_end = buffer + buffer_size; | |
511 | 28784111 | s->index = 0; | |
512 | |||
513 | 28784111 | return ret; | |
514 | } | ||
515 | |||
516 | /** | ||
517 | * Initialize GetBitContext. | ||
518 | * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes | ||
519 | * larger than the actual read bits because some optimized bitstream | ||
520 | * readers read 32 or 64 bit at once and could read over the end | ||
521 | * @param byte_size the size of the buffer in bytes | ||
522 | * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow. | ||
523 | */ | ||
524 | 590689 | static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer, | |
525 | int byte_size) | ||
526 | { | ||
527 |
2/4✓ Branch 0 taken 590689 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 590689 times.
|
590689 | if (byte_size > INT_MAX / 8 || byte_size < 0) |
528 | ✗ | byte_size = -1; | |
529 | 590689 | return init_get_bits(s, buffer, byte_size * 8); | |
530 | } | ||
531 | |||
532 | 200 | static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, | |
533 | int byte_size) | ||
534 | { | ||
535 |
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) |
536 | ✗ | byte_size = -1; | |
537 | 200 | return init_get_bits(s, buffer, byte_size * 8); | |
538 | } | ||
539 | |||
540 | 277905 | static inline const uint8_t *align_get_bits(GetBitContext *s) | |
541 | { | ||
542 | 277905 | int n = -get_bits_count(s) & 7; | |
543 |
2/2✓ Branch 0 taken 92549 times.
✓ Branch 1 taken 185356 times.
|
277905 | if (n) |
544 | 92549 | skip_bits(s, n); | |
545 | 277905 | return s->buffer + (s->index >> 3); | |
546 | } | ||
547 | |||
548 | /** | ||
549 | * If the vlc code is invalid and max_depth=1, then no bits will be removed. | ||
550 | * If the vlc code is invalid and max_depth>1, then the number of bits removed | ||
551 | * is undefined. | ||
552 | */ | ||
553 | #define GET_VLC(code, name, gb, table, bits, max_depth) \ | ||
554 | do { \ | ||
555 | int n, nb_bits; \ | ||
556 | unsigned int index; \ | ||
557 | \ | ||
558 | index = SHOW_UBITS(name, gb, bits); \ | ||
559 | code = table[index].sym; \ | ||
560 | n = table[index].len; \ | ||
561 | \ | ||
562 | if (max_depth > 1 && n < 0) { \ | ||
563 | LAST_SKIP_BITS(name, gb, bits); \ | ||
564 | UPDATE_CACHE(name, gb); \ | ||
565 | \ | ||
566 | nb_bits = -n; \ | ||
567 | \ | ||
568 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ | ||
569 | code = table[index].sym; \ | ||
570 | n = table[index].len; \ | ||
571 | if (max_depth > 2 && n < 0) { \ | ||
572 | LAST_SKIP_BITS(name, gb, nb_bits); \ | ||
573 | UPDATE_CACHE(name, gb); \ | ||
574 | \ | ||
575 | nb_bits = -n; \ | ||
576 | \ | ||
577 | index = SHOW_UBITS(name, gb, nb_bits) + code; \ | ||
578 | code = table[index].sym; \ | ||
579 | n = table[index].len; \ | ||
580 | } \ | ||
581 | } \ | ||
582 | SKIP_BITS(name, gb, n); \ | ||
583 | } while (0) | ||
584 | |||
585 | #define GET_RL_VLC(level, run, name, gb, table, bits, \ | ||
586 | max_depth, need_update) \ | ||
587 | do { \ | ||
588 | int n, nb_bits; \ | ||
589 | unsigned int index; \ | ||
590 | \ | ||
591 | index = SHOW_UBITS(name, gb, bits); \ | ||
592 | level = table[index].level; \ | ||
593 | n = table[index].len; \ | ||
594 | \ | ||
595 | if (max_depth > 1 && n < 0) { \ | ||
596 | SKIP_BITS(name, gb, bits); \ | ||
597 | if (need_update) { \ | ||
598 | UPDATE_CACHE(name, gb); \ | ||
599 | } \ | ||
600 | \ | ||
601 | nb_bits = -n; \ | ||
602 | \ | ||
603 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ | ||
604 | level = table[index].level; \ | ||
605 | n = table[index].len; \ | ||
606 | if (max_depth > 2 && n < 0) { \ | ||
607 | LAST_SKIP_BITS(name, gb, nb_bits); \ | ||
608 | if (need_update) { \ | ||
609 | UPDATE_CACHE(name, gb); \ | ||
610 | } \ | ||
611 | nb_bits = -n; \ | ||
612 | \ | ||
613 | index = SHOW_UBITS(name, gb, nb_bits) + level; \ | ||
614 | level = table[index].level; \ | ||
615 | n = table[index].len; \ | ||
616 | } \ | ||
617 | } \ | ||
618 | run = table[index].run; \ | ||
619 | SKIP_BITS(name, gb, n); \ | ||
620 | } while (0) | ||
621 | |||
622 | /** | ||
623 | * Parse a vlc code. | ||
624 | * @param bits is the number of bits which will be read at once, must be | ||
625 | * identical to nb_bits in init_vlc() | ||
626 | * @param max_depth is the number of times bits bits must be read to completely | ||
627 | * read the longest vlc code | ||
628 | * = (max_vlc_length + bits - 1) / bits | ||
629 | * @returns the code parsed or -1 if no vlc matches | ||
630 | */ | ||
631 | 289227123 | static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, | |
632 | int bits, int max_depth) | ||
633 | { | ||
634 | int code; | ||
635 | |||
636 | 289227123 | OPEN_READER(re, s); | |
637 | 289227123 | UPDATE_CACHE(re, s); | |
638 | |||
639 |
10/10✓ Branch 0 taken 7683300 times.
✓ Branch 1 taken 179252459 times.
✓ Branch 2 taken 102645455 times.
✓ Branch 3 taken 32616712 times.
✓ Branch 4 taken 144822908 times.
✓ Branch 5 taken 103091 times.
✓ Branch 6 taken 22987166 times.
✓ Branch 7 taken 2551337 times.
✓ Branch 8 taken 630505 times.
✓ Branch 9 taken 22356091 times.
|
289227123 | GET_VLC(code, re, s, table, bits, max_depth); |
640 | |||
641 | 289227123 | CLOSE_READER(re, s); | |
642 | |||
643 | 289227123 | return code; | |
644 | } | ||
645 | |||
646 | 378513 | static inline int decode012(GetBitContext *gb) | |
647 | { | ||
648 | int n; | ||
649 | 378513 | n = get_bits1(gb); | |
650 |
2/2✓ Branch 0 taken 263658 times.
✓ Branch 1 taken 114855 times.
|
378513 | if (n == 0) |
651 | 263658 | return 0; | |
652 | else | ||
653 | 114855 | return get_bits1(gb) + 1; | |
654 | } | ||
655 | |||
656 | 180374 | static inline int decode210(GetBitContext *gb) | |
657 | { | ||
658 |
2/2✓ Branch 1 taken 131153 times.
✓ Branch 2 taken 49221 times.
|
180374 | if (get_bits1(gb)) |
659 | 131153 | return 0; | |
660 | else | ||
661 | 49221 | return 2 - get_bits1(gb); | |
662 | } | ||
663 | |||
664 | 274260151 | static inline int get_bits_left(GetBitContext *gb) | |
665 | { | ||
666 | 274260151 | return gb->size_in_bits - get_bits_count(gb); | |
667 | } | ||
668 | |||
669 | 130058 | static inline int skip_1stop_8data_bits(GetBitContext *gb) | |
670 | { | ||
671 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 130058 times.
|
130058 | if (get_bits_left(gb) <= 0) |
672 | ✗ | return AVERROR_INVALIDDATA; | |
673 | |||
674 |
2/2✓ Branch 1 taken 1557 times.
✓ Branch 2 taken 130058 times.
|
131615 | while (get_bits1(gb)) { |
675 | 1557 | skip_bits(gb, 8); | |
676 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1557 times.
|
1557 | if (get_bits_left(gb) <= 0) |
677 | ✗ | return AVERROR_INVALIDDATA; | |
678 | } | ||
679 | |||
680 | 130058 | return 0; | |
681 | } | ||
682 | |||
683 | #endif // CACHED_BITSTREAM_READER | ||
684 | |||
685 | #endif /* AVCODEC_GET_BITS_H */ | ||
686 |