FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/get_bits.h
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 193 236 81.8%
Branches: 55 88 62.5%

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