FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_bits.h
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 189 230 82.2%
Branches: 54 86 62.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2016 Alexandra Hájková
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * bitstream reader API header.
25 */
26
27 #ifndef AVCODEC_GET_BITS_H
28 #define AVCODEC_GET_BITS_H
29
30 #include <stdint.h>
31
32 #include "libavutil/common.h"
33 #include "libavutil/intreadwrite.h"
34 #include "libavutil/avassert.h"
35
36 #include "defs.h"
37 #include "mathops.h"
38 #include "vlc.h"
39
40 /*
41 * Safe bitstream reading:
42 * optionally, the get_bits API can check to ensure that we
43 * don't read past input buffer boundaries. This is protected
44 * with CONFIG_SAFE_BITSTREAM_READER at the global level, and
45 * then below that with UNCHECKED_BITSTREAM_READER at the per-
46 * decoder level. This means that decoders that check internally
47 * can "#define UNCHECKED_BITSTREAM_READER 1" to disable
48 * overread checks.
49 * Boundary checking causes a minor performance penalty so for
50 * applications that won't want/need this, it can be disabled
51 * globally using "#define CONFIG_SAFE_BITSTREAM_READER 0".
52 */
53 #ifndef UNCHECKED_BITSTREAM_READER
54 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
55 #endif
56
57 #ifndef CACHED_BITSTREAM_READER
58 #define CACHED_BITSTREAM_READER 0
59 #endif
60
61 typedef struct GetBitContext {
62 const uint8_t *buffer, *buffer_end;
63 #if CACHED_BITSTREAM_READER
64 uint64_t cache;
65 unsigned bits_left;
66 #endif
67 int index;
68 int size_in_bits;
69 int size_in_bits_plus8;
70 } GetBitContext;
71
72 static inline unsigned int get_bits(GetBitContext *s, int n);
73 static inline void skip_bits(GetBitContext *s, int n);
74 static inline unsigned int show_bits(GetBitContext *s, int n);
75
76 /* Bitstream reader API docs:
77 * name
78 * arbitrary name which is used as prefix for the internal variables
79 *
80 * gb
81 * getbitcontext
82 *
83 * OPEN_READER(name, gb)
84 * load gb into local variables
85 *
86 * CLOSE_READER(name, gb)
87 * store local vars in gb
88 *
89 * UPDATE_CACHE(name, gb)
90 * Refill the internal cache from the bitstream.
91 * After this call at least MIN_CACHE_BITS will be available.
92 *
93 * GET_CACHE(name, gb)
94 * Will output the contents of the internal cache,
95 * next bit is MSB of 32 or 64 bits (FIXME 64 bits).
96 *
97 * SHOW_UBITS(name, gb, num)
98 * Will return the next num bits.
99 *
100 * SHOW_SBITS(name, gb, num)
101 * Will return the next num bits and do sign extension.
102 *
103 * SKIP_BITS(name, gb, num)
104 * Will skip over the next num bits.
105 * Note, this is equivalent to SKIP_CACHE; SKIP_COUNTER.
106 *
107 * SKIP_CACHE(name, gb, num)
108 * Will remove the next num bits from the cache (note SKIP_COUNTER
109 * MUST be called before UPDATE_CACHE / CLOSE_READER).
110 *
111 * SKIP_COUNTER(name, gb, num)
112 * Will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS).
113 *
114 * LAST_SKIP_BITS(name, gb, num)
115 * Like SKIP_BITS, to be used if next call is UPDATE_CACHE or CLOSE_READER.
116 *
117 * BITS_LEFT(name, gb)
118 * Return the number of bits left
119 *
120 * For examples see get_bits, show_bits, skip_bits, get_vlc.
121 */
122
123 #if CACHED_BITSTREAM_READER
124 # define MIN_CACHE_BITS 64
125 #elif defined LONG_BITSTREAM_READER
126 # define MIN_CACHE_BITS 32
127 #else
128 # define MIN_CACHE_BITS 25
129 #endif
130
131 #if !CACHED_BITSTREAM_READER
132
133 #define OPEN_READER_NOSIZE(name, gb) \
134 unsigned int name ## _index = (gb)->index; \
135 unsigned int av_unused name ## _cache
136
137 #if UNCHECKED_BITSTREAM_READER
138 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
139
140 #define BITS_AVAILABLE(name, gb) 1
141 #else
142 #define OPEN_READER(name, gb) \
143 OPEN_READER_NOSIZE(name, gb); \
144 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
145
146 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
147 #endif
148
149 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
150
151 # ifdef LONG_BITSTREAM_READER
152
153 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
154 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
155
156 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
157 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
158
159 #else
160
161 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
162 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
163
164 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
165 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
166
167 #endif
168
169
170 #ifdef BITSTREAM_READER_LE
171
172 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
173
174 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
175
176 #else
177
178 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
179
180 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
181
182 #endif
183
184 #if UNCHECKED_BITSTREAM_READER
185 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
186 #else
187 # define SKIP_COUNTER(name, gb, num) \
188 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
189 #endif
190
191 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
192
193 #define SKIP_BITS(name, gb, num) \
194 do { \
195 SKIP_CACHE(name, gb, num); \
196 SKIP_COUNTER(name, gb, num); \
197 } while (0)
198
199 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
200
201 #define SHOW_UBITS_LE(name, gb, num) zero_extend(name ## _cache, num)
202 #define SHOW_SBITS_LE(name, gb, num) sign_extend(name ## _cache, num)
203
204 #define SHOW_UBITS_BE(name, gb, num) NEG_USR32(name ## _cache, num)
205 #define SHOW_SBITS_BE(name, gb, num) NEG_SSR32(name ## _cache, num)
206
207 #ifdef BITSTREAM_READER_LE
208 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_LE(name, gb, num)
209 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_LE(name, gb, num)
210 #else
211 # define SHOW_UBITS(name, gb, num) SHOW_UBITS_BE(name, gb, num)
212 # define SHOW_SBITS(name, gb, num) SHOW_SBITS_BE(name, gb, num)
213 #endif
214
215 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
216
217 #endif
218
219 338758411 static inline int get_bits_count(const GetBitContext *s)
220 {
221 #if CACHED_BITSTREAM_READER
222 31369264 return s->index - s->bits_left;
223 #else
224 307389147 return s->index;
225 #endif
226 }
227
228 #if CACHED_BITSTREAM_READER
229 3984175 static inline void refill_32(GetBitContext *s, int is_le)
230 {
231 #if !UNCHECKED_BITSTREAM_READER
232
2/2
✓ Branch 0 taken 274 times.
✓ Branch 1 taken 191800 times.
192074 if (s->index >> 3 >= s->buffer_end - s->buffer)
233 274 return;
234 #endif
235
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3983901 times.
3983901 if (is_le)
237 s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache;
238 else
239 3983901 s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left);
240 3983901 s->index += 32;
241 3983901 s->bits_left += 32;
242 3792101 }
243
244 1511 static inline void refill_64(GetBitContext *s, int is_le)
245 {
246 #if !UNCHECKED_BITSTREAM_READER
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 378 times.
378 if (s->index >> 3 >= s->buffer_end - s->buffer)
248 return;
249 #endif
250
251
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1511 times.
1511 if (is_le)
252 s->cache = AV_RL64(s->buffer + (s->index >> 3));
253 else
254 1511 s->cache = AV_RB64(s->buffer + (s->index >> 3));
255 1511 s->index += 64;
256 1511 s->bits_left = 64;
257 1133 }
258
259 static inline uint64_t get_val(GetBitContext *s, unsigned n, int is_le)
260 {
261 uint64_t ret;
262 av_assert2(n>0 && n<=63);
263 if (is_le) {
264 ret = s->cache & ((UINT64_C(1) << n) - 1);
265 s->cache >>= n;
266 } else {
267 ret = s->cache >> (64 - n);
268 s->cache <<= n;
269 }
270 s->bits_left -= n;
271 return ret;
272 }
273
274 66850117 static inline unsigned show_val(const GetBitContext *s, unsigned n)
275 {
276 #ifdef BITSTREAM_READER_LE
277 return s->cache & ((UINT64_C(1) << n) - 1);
278 #else
279 66850117 return s->cache >> (64 - n);
280 #endif
281 }
282 #endif
283
284 /**
285 * Skips the specified number of bits.
286 * @param n the number of bits to skip,
287 * For the UNCHECKED_BITSTREAM_READER this must not cause the distance
288 * from the start to overflow int32_t. Staying within the bitstream + padding
289 * is sufficient, too.
290 */
291 3299807 static inline void skip_bits_long(GetBitContext *s, int n)
292 {
293 #if CACHED_BITSTREAM_READER
294 skip_bits(s, n);
295 #else
296 #if UNCHECKED_BITSTREAM_READER
297 2664286 s->index += n;
298 #else
299 635521 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
300 #endif
301 #endif
302 3299807 }
303
304 #if CACHED_BITSTREAM_READER
305 66850117 static inline void skip_remaining(GetBitContext *s, unsigned n)
306 {
307 #ifdef BITSTREAM_READER_LE
308 s->cache >>= n;
309 #else
310 66850117 s->cache <<= n;
311 #endif
312 66850117 s->bits_left -= n;
313 66850117 }
314 #endif
315
316 /**
317 * Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
318 * if MSB not set it is negative
319 * @param n length in bits
320 */
321 26622536 static inline int get_xbits(GetBitContext *s, int n)
322 {
323 #if CACHED_BITSTREAM_READER
324 int32_t cache = show_bits(s, 32);
325 int sign = ~cache >> 31;
326 skip_remaining(s, n);
327
328 return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
329 #else
330 register int sign;
331 register int32_t cache;
332 26622536 OPEN_READER(re, s);
333 av_assert2(n>0 && n<=25);
334 26622536 UPDATE_CACHE(re, s);
335 26622536 cache = GET_CACHE(re, s);
336 26622536 sign = ~cache >> 31;
337 26622536 LAST_SKIP_BITS(re, s, n);
338 26622536 CLOSE_READER(re, s);
339 26622536 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
340 #endif
341 }
342
343 #if !CACHED_BITSTREAM_READER
344 336 static inline int get_xbits_le(GetBitContext *s, int n)
345 {
346 register int sign;
347 register int32_t cache;
348 336 OPEN_READER(re, s);
349 av_assert2(n>0 && n<=25);
350 336 UPDATE_CACHE_LE(re, s);
351 336 cache = GET_CACHE(re, s);
352 336 sign = sign_extend(~cache, n) >> 31;
353 336 LAST_SKIP_BITS(re, s, n);
354 336 CLOSE_READER(re, s);
355 336 return (zero_extend(sign ^ cache, n) ^ sign) - sign;
356 }
357 #endif
358
359 32018621 static inline int get_sbits(GetBitContext *s, int n)
360 {
361 register int tmp;
362 #if CACHED_BITSTREAM_READER
363 av_assert2(n>0 && n<=25);
364 tmp = sign_extend(get_bits(s, n), n);
365 #else
366 32018621 OPEN_READER(re, s);
367 av_assert2(n>0 && n<=25);
368 32018621 UPDATE_CACHE(re, s);
369 32018621 tmp = SHOW_SBITS(re, s, n);
370 32018621 LAST_SKIP_BITS(re, s, n);
371 32018621 CLOSE_READER(re, s);
372 #endif
373 32018621 return tmp;
374 }
375
376 /**
377 * Read 1-25 bits.
378 */
379 317915059 static inline unsigned int get_bits(GetBitContext *s, int n)
380 {
381 register unsigned int tmp;
382 #if CACHED_BITSTREAM_READER
383
384 av_assert2(n>0 && n<=32);
385 if (n > s->bits_left) {
386 #ifdef BITSTREAM_READER_LE
387 refill_32(s, 1);
388 #else
389 refill_32(s, 0);
390 #endif
391 if (s->bits_left < 32)
392 s->bits_left = n;
393 }
394
395 #ifdef BITSTREAM_READER_LE
396 tmp = get_val(s, n, 1);
397 #else
398 tmp = get_val(s, n, 0);
399 #endif
400 #else
401 317915059 OPEN_READER(re, s);
402 av_assert2(n>0 && n<=25);
403 317915059 UPDATE_CACHE(re, s);
404 317915059 tmp = SHOW_UBITS(re, s, n);
405 317915059 LAST_SKIP_BITS(re, s, n);
406 317915059 CLOSE_READER(re, s);
407 #endif
408 av_assert2(tmp < UINT64_C(1) << n);
409 317915059 return tmp;
410 }
411
412 /**
413 * Read 0-25 bits.
414 */
415 25961009 static av_always_inline int get_bitsz(GetBitContext *s, int n)
416 {
417
2/2
✓ Branch 0 taken 22724418 times.
✓ Branch 1 taken 3236591 times.
25961009 return n ? get_bits(s, n) : 0;
418 }
419
420 718813 static inline unsigned int get_bits_le(GetBitContext *s, int n)
421 {
422 #if CACHED_BITSTREAM_READER
423 av_assert2(n>0 && n<=32);
424 if (n > s->bits_left) {
425 refill_32(s, 1);
426 if (s->bits_left < 32)
427 s->bits_left = n;
428 }
429
430 return get_val(s, n, 1);
431 #else
432 register int tmp;
433 718813 OPEN_READER(re, s);
434 av_assert2(n>0 && n<=25);
435 718813 UPDATE_CACHE_LE(re, s);
436 718813 tmp = SHOW_UBITS_LE(re, s, n);
437 718813 LAST_SKIP_BITS(re, s, n);
438 718813 CLOSE_READER(re, s);
439 718813 return tmp;
440 #endif
441 }
442
443 /**
444 * Show 1-25 bits.
445 */
446 179656263 static inline unsigned int show_bits(GetBitContext *s, int n)
447 {
448 register unsigned int tmp;
449 #if CACHED_BITSTREAM_READER
450
2/2
✓ Branch 0 taken 3984175 times.
✓ Branch 1 taken 62865942 times.
66850117 if (n > s->bits_left)
451 #ifdef BITSTREAM_READER_LE
452 refill_32(s, 1);
453 #else
454 3984175 refill_32(s, 0);
455 #endif
456
457 66850117 tmp = show_val(s, n);
458 #else
459 112806146 OPEN_READER_NOSIZE(re, s);
460 av_assert2(n>0 && n<=25);
461 112806146 UPDATE_CACHE(re, s);
462 112806146 tmp = SHOW_UBITS(re, s, n);
463 #endif
464 179656263 return tmp;
465 }
466
467 110067957 static inline void skip_bits(GetBitContext *s, int n)
468 {
469 #if CACHED_BITSTREAM_READER
470 if (n < s->bits_left)
471 skip_remaining(s, n);
472 else {
473 n -= s->bits_left;
474 s->cache = 0;
475 s->bits_left = 0;
476
477 if (n >= 64) {
478 unsigned skip = (n / 8) * 8;
479
480 n -= skip;
481 s->index += skip;
482 }
483 #ifdef BITSTREAM_READER_LE
484 refill_64(s, 1);
485 #else
486 refill_64(s, 0);
487 #endif
488 if (n)
489 skip_remaining(s, n);
490 }
491 #else
492 110067957 OPEN_READER(re, s);
493 110067957 LAST_SKIP_BITS(re, s, n);
494 110067957 CLOSE_READER(re, s);
495 #endif
496 110067957 }
497
498 444608544 static inline unsigned int get_bits1(GetBitContext *s)
499 {
500 #if CACHED_BITSTREAM_READER
501 if (!s->bits_left)
502 #ifdef BITSTREAM_READER_LE
503 refill_64(s, 1);
504 #else
505 refill_64(s, 0);
506 #endif
507
508 #ifdef BITSTREAM_READER_LE
509 return get_val(s, 1, 1);
510 #else
511 return get_val(s, 1, 0);
512 #endif
513 #else
514 444608544 unsigned int index = s->index;
515 444608544 uint8_t result = s->buffer[index >> 3];
516 #ifdef BITSTREAM_READER_LE
517 261812561 result >>= index & 7;
518 261812561 result &= 1;
519 #else
520 182795983 result <<= index & 7;
521 182795983 result >>= 8 - 1;
522 #endif
523 #if !UNCHECKED_BITSTREAM_READER
524
2/2
✓ Branch 0 taken 403359457 times.
✓ Branch 1 taken 15212 times.
403374669 if (s->index < s->size_in_bits_plus8)
525 #endif
526 444593332 index++;
527 444608544 s->index = index;
528
529 444608544 return result;
530 #endif
531 }
532
533 783 static inline unsigned int show_bits1(GetBitContext *s)
534 {
535 783 return show_bits(s, 1);
536 }
537
538 236708 static inline void skip_bits1(GetBitContext *s)
539 {
540 236708 skip_bits(s, 1);
541 236708 }
542
543 /**
544 * Read 0-32 bits.
545 */
546 29476602 static inline unsigned int get_bits_long(GetBitContext *s, int n)
547 {
548 av_assert2(n>=0 && n<=32);
549
2/2
✓ Branch 0 taken 240222 times.
✓ Branch 1 taken 29236380 times.
29476602 if (!n) {
550 240222 return 0;
551 #if CACHED_BITSTREAM_READER
552 }
553 return get_bits(s, n);
554 #else
555
2/2
✓ Branch 0 taken 26041868 times.
✓ Branch 1 taken 3194512 times.
29236380 } else if (n <= MIN_CACHE_BITS) {
556 26041868 return get_bits(s, n);
557 } else {
558 #ifdef BITSTREAM_READER_LE
559 2440 unsigned ret = get_bits(s, 16);
560 2440 return ret | (get_bits(s, n - 16) << 16);
561 #else
562 3192072 unsigned ret = get_bits(s, 16) << (n - 16);
563 3192072 return ret | get_bits(s, n - 16);
564 #endif
565 }
566 #endif
567 }
568
569 /**
570 * Read 0-64 bits.
571 */
572 6485 static inline uint64_t get_bits64(GetBitContext *s, int n)
573 {
574
2/2
✓ Branch 0 taken 6375 times.
✓ Branch 1 taken 110 times.
6485 if (n <= 32) {
575 6375 return get_bits_long(s, n);
576 } else {
577 #ifdef BITSTREAM_READER_LE
578 24 uint64_t ret = get_bits_long(s, 32);
579 24 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
580 #else
581 86 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
582 86 return ret | get_bits_long(s, 32);
583 #endif
584 }
585 }
586
587 /**
588 * Read 0-32 bits as a signed integer.
589 */
590 4760809 static inline int get_sbits_long(GetBitContext *s, int n)
591 {
592 // sign_extend(x, 0) is undefined
593
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4760809 times.
4760809 if (!n)
594 return 0;
595
596 4760809 return sign_extend(get_bits_long(s, n), n);
597 }
598
599 /**
600 * Show 0-32 bits.
601 */
602 2957494 static inline unsigned int show_bits_long(GetBitContext *s, int n)
603 {
604
2/2
✓ Branch 0 taken 6799 times.
✓ Branch 1 taken 2950695 times.
2957494 if (n <= MIN_CACHE_BITS) {
605 6799 return show_bits(s, n);
606 } else {
607 2950695 GetBitContext gb = *s;
608 2950695 return get_bits_long(&gb, n);
609 }
610 }
611
612 27168168 static inline int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer,
613 int bit_size, int is_le)
614 {
615 int buffer_size;
616 27168168 int ret = 0;
617
618
4/6
✓ Branch 0 taken 27168168 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27168168 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✓ Branch 5 taken 27168156 times.
27168168 if (bit_size >= INT_MAX - FFMAX(7, AV_INPUT_BUFFER_PADDING_SIZE*8) || bit_size < 0 || !buffer) {
619 12 bit_size = 0;
620 12 buffer = NULL;
621 12 ret = AVERROR_INVALIDDATA;
622 }
623
624 27168168 buffer_size = (bit_size + 7) >> 3;
625
626 27168168 s->buffer = buffer;
627 27168168 s->size_in_bits = bit_size;
628 27168168 s->size_in_bits_plus8 = bit_size + 8;
629 27168168 s->buffer_end = buffer + buffer_size;
630 27168168 s->index = 0;
631
632 #if CACHED_BITSTREAM_READER
633 1511 s->cache = 0;
634 1511 s->bits_left = 0;
635 1511 refill_64(s, is_le);
636 #endif
637
638 27168168 return ret;
639 }
640
641 /**
642 * Initialize GetBitContext.
643 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
644 * larger than the actual read bits because some optimized bitstream
645 * readers read 32 or 64 bit at once and could read over the end
646 * @param bit_size the size of the buffer in bits
647 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
648 */
649 27167968 static inline int init_get_bits(GetBitContext *s, const uint8_t *buffer,
650 int bit_size)
651 {
652 #ifdef BITSTREAM_READER_LE
653 64351 return init_get_bits_xe(s, buffer, bit_size, 1);
654 #else
655 27103617 return init_get_bits_xe(s, buffer, bit_size, 0);
656 #endif
657 }
658
659 /**
660 * Initialize GetBitContext.
661 * @param buffer bitstream buffer, must be AV_INPUT_BUFFER_PADDING_SIZE bytes
662 * larger than the actual read bits because some optimized bitstream
663 * readers read 32 or 64 bit at once and could read over the end
664 * @param byte_size the size of the buffer in bytes
665 * @return 0 on success, AVERROR_INVALIDDATA if the buffer_size would overflow.
666 */
667 340022 static inline int init_get_bits8(GetBitContext *s, const uint8_t *buffer,
668 int byte_size)
669 {
670
2/4
✓ Branch 0 taken 340022 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 340022 times.
340022 if (byte_size > INT_MAX / 8 || byte_size < 0)
671 byte_size = -1;
672 340022 return init_get_bits(s, buffer, byte_size * 8);
673 }
674
675 200 static inline int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer,
676 int byte_size)
677 {
678
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)
679 byte_size = -1;
680 200 return init_get_bits_xe(s, buffer, byte_size * 8, 1);
681 }
682
683 276266 static inline const uint8_t *align_get_bits(GetBitContext *s)
684 {
685 276266 int n = -get_bits_count(s) & 7;
686
2/2
✓ Branch 0 taken 91132 times.
✓ Branch 1 taken 185134 times.
276266 if (n)
687 91132 skip_bits(s, n);
688 276266 return s->buffer + (s->index >> 3);
689 }
690
691 /**
692 * If the vlc code is invalid and max_depth=1, then no bits will be removed.
693 * If the vlc code is invalid and max_depth>1, then the number of bits removed
694 * is undefined.
695 */
696 #define GET_VLC(code, name, gb, table, bits, max_depth) \
697 do { \
698 int n, nb_bits; \
699 unsigned int index; \
700 \
701 index = SHOW_UBITS(name, gb, bits); \
702 code = table[index].sym; \
703 n = table[index].len; \
704 \
705 if (max_depth > 1 && n < 0) { \
706 LAST_SKIP_BITS(name, gb, bits); \
707 UPDATE_CACHE(name, gb); \
708 \
709 nb_bits = -n; \
710 \
711 index = SHOW_UBITS(name, gb, nb_bits) + code; \
712 code = table[index].sym; \
713 n = table[index].len; \
714 if (max_depth > 2 && n < 0) { \
715 LAST_SKIP_BITS(name, gb, nb_bits); \
716 UPDATE_CACHE(name, gb); \
717 \
718 nb_bits = -n; \
719 \
720 index = SHOW_UBITS(name, gb, nb_bits) + code; \
721 code = table[index].sym; \
722 n = table[index].len; \
723 } \
724 } \
725 SKIP_BITS(name, gb, n); \
726 } while (0)
727
728 #define GET_RL_VLC(level, run, name, gb, table, bits, \
729 max_depth, need_update) \
730 do { \
731 int n, nb_bits; \
732 unsigned int index; \
733 \
734 index = SHOW_UBITS(name, gb, bits); \
735 level = table[index].level; \
736 n = table[index].len; \
737 \
738 if (max_depth > 1 && n < 0) { \
739 SKIP_BITS(name, gb, bits); \
740 if (need_update) { \
741 UPDATE_CACHE(name, gb); \
742 } \
743 \
744 nb_bits = -n; \
745 \
746 index = SHOW_UBITS(name, gb, nb_bits) + level; \
747 level = table[index].level; \
748 n = table[index].len; \
749 if (max_depth > 2 && n < 0) { \
750 LAST_SKIP_BITS(name, gb, nb_bits); \
751 if (need_update) { \
752 UPDATE_CACHE(name, gb); \
753 } \
754 nb_bits = -n; \
755 \
756 index = SHOW_UBITS(name, gb, nb_bits) + level; \
757 level = table[index].level; \
758 n = table[index].len; \
759 } \
760 } \
761 run = table[index].run; \
762 SKIP_BITS(name, gb, n); \
763 } while (0)
764
765 /* Return the LUT element for the given bitstream configuration. */
766 282167 static inline int set_idx(GetBitContext *s, int code, int *n, int *nb_bits,
767 const VLCElem *table)
768 {
769 unsigned idx;
770
771 282167 *nb_bits = -*n;
772 282167 idx = show_bits(s, *nb_bits) + code;
773 282167 *n = table[idx].len;
774
775 282167 return table[idx].sym;
776 }
777
778 /**
779 * Parse a vlc code.
780 * @param bits is the number of bits which will be read at once, must be
781 * identical to nb_bits in init_vlc()
782 * @param max_depth is the number of times bits bits must be read to completely
783 * read the longest vlc code
784 * = (max_vlc_length + bits - 1) / bits
785 * @returns the code parsed or -1 if no vlc matches
786 */
787 349563008 static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table,
788 int bits, int max_depth)
789 {
790 #if CACHED_BITSTREAM_READER
791 int nb_bits;
792 66567950 unsigned idx = show_bits(s, bits);
793 66567950 int code = table[idx].sym;
794 66567950 int n = table[idx].len;
795
796
3/4
✓ Branch 0 taken 66567950 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 282110 times.
✓ Branch 3 taken 66285840 times.
66567950 if (max_depth > 1 && n < 0) {
797 282110 skip_remaining(s, bits);
798 282110 code = set_idx(s, code, &n, &nb_bits, table);
799
3/4
✓ Branch 0 taken 282110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 282053 times.
282110 if (max_depth > 2 && n < 0) {
800 57 skip_remaining(s, nb_bits);
801 57 code = set_idx(s, code, &n, &nb_bits, table);
802 }
803 }
804 66567950 skip_remaining(s, n);
805
806 66567950 return code;
807 #else
808 int code;
809
810 282995058 OPEN_READER(re, s);
811 282995058 UPDATE_CACHE(re, s);
812
813
10/10
✓ Branch 0 taken 6138900 times.
✓ Branch 1 taken 177294920 times.
✓ Branch 2 taken 99890100 times.
✓ Branch 3 taken 31101189 times.
✓ Branch 4 taken 142861721 times.
✓ Branch 5 taken 77862 times.
✓ Branch 6 taken 22990384 times.
✓ Branch 7 taken 2551767 times.
✓ Branch 8 taken 630500 times.
✓ Branch 9 taken 22359314 times.
282995058 GET_VLC(code, re, s, table, bits, max_depth);
814
815 282995058 CLOSE_READER(re, s);
816
817 282995058 return code;
818 #endif
819 }
820
821 378508 static inline int decode012(GetBitContext *gb)
822 {
823 int n;
824 378508 n = get_bits1(gb);
825
2/2
✓ Branch 0 taken 263653 times.
✓ Branch 1 taken 114855 times.
378508 if (n == 0)
826 263653 return 0;
827 else
828 114855 return get_bits1(gb) + 1;
829 }
830
831 180238 static inline int decode210(GetBitContext *gb)
832 {
833
2/2
✓ Branch 1 taken 131041 times.
✓ Branch 2 taken 49197 times.
180238 if (get_bits1(gb))
834 131041 return 0;
835 else
836 49197 return 2 - get_bits1(gb);
837 }
838
839 304131159 static inline int get_bits_left(GetBitContext *gb)
840 {
841 304131159 return gb->size_in_bits - get_bits_count(gb);
842 }
843
844 120518 static inline int skip_1stop_8data_bits(GetBitContext *gb)
845 {
846
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120518 times.
120518 if (get_bits_left(gb) <= 0)
847 return AVERROR_INVALIDDATA;
848
849
2/2
✓ Branch 1 taken 1557 times.
✓ Branch 2 taken 120518 times.
122075 while (get_bits1(gb)) {
850 1557 skip_bits(gb, 8);
851
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1557 times.
1557 if (get_bits_left(gb) <= 0)
852 return AVERROR_INVALIDDATA;
853 }
854
855 120518 return 0;
856 }
857
858 #endif /* AVCODEC_GET_BITS_H */
859