27 #ifndef AVCODEC_GET_BITS_H 28 #define AVCODEC_GET_BITS_H 33 #include "libavutil/intreadwrite.h" 34 #include "libavutil/log.h" 53 #ifndef UNCHECKED_BITSTREAM_READER 54 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER 57 #ifndef CACHED_BITSTREAM_READER 58 #define CACHED_BITSTREAM_READER 0 62 const uint8_t *buffer, *buffer_end;
63 #if CACHED_BITSTREAM_READER 69 int size_in_bits_plus8;
74 static inline unsigned int show_bits(
GetBitContext *s,
int n);
123 #if CACHED_BITSTREAM_READER 124 # define MIN_CACHE_BITS 64 125 #elif defined LONG_BITSTREAM_READER 126 # define MIN_CACHE_BITS 32 128 # define MIN_CACHE_BITS 25 131 #if !CACHED_BITSTREAM_READER 133 #define OPEN_READER_NOSIZE(name, gb) \ 134 unsigned int name ## _index = (gb)->index; \ 135 unsigned int av_unused name ## _cache 137 #if UNCHECKED_BITSTREAM_READER 138 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb) 140 #define BITS_AVAILABLE(name, gb) 1 142 #define OPEN_READER(name, gb) \ 143 OPEN_READER_NOSIZE(name, gb); \ 144 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8 146 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8 149 #define CLOSE_READER(name, gb) (gb)->index = name ## _index 151 # ifdef LONG_BITSTREAM_READER 153 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \ 154 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) 156 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \ 157 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7)) 161 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \ 162 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7) 164 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \ 165 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7) 170 #ifdef BITSTREAM_READER_LE 172 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb) 174 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num) 178 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb) 180 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num) 184 #if UNCHECKED_BITSTREAM_READER 185 # define SKIP_COUNTER(name, gb, num) name ## _index += (num) 187 # define SKIP_COUNTER(name, gb, num) \ 188 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num)) 191 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index)) 193 #define SKIP_BITS(name, gb, num) \ 195 SKIP_CACHE(name, gb, num); \ 196 SKIP_COUNTER(name, gb, num); \ 199 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num) 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) 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) 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) 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) 215 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache) 221 #if CACHED_BITSTREAM_READER 222 return s->index - s->bits_left;
228 #if CACHED_BITSTREAM_READER 231 #if !UNCHECKED_BITSTREAM_READER 232 if (s->index >> 3 >= s->buffer_end - s->buffer)
236 #ifdef BITSTREAM_READER_LE 237 s->cache = (uint64_t)AV_RL32(s->buffer + (s->index >> 3)) << s->bits_left | s->cache;
239 s->cache = s->cache | (uint64_t)AV_RB32(s->buffer + (s->index >> 3)) << (32 - s->bits_left);
247 #if !UNCHECKED_BITSTREAM_READER 248 if (s->index >> 3 >= s->buffer_end - s->buffer)
252 #ifdef BITSTREAM_READER_LE 253 s->cache = AV_RL64(s->buffer + (s->index >> 3));
255 s->cache = AV_RB64(s->buffer + (s->index >> 3));
261 static inline uint64_t get_val(
GetBitContext *s,
unsigned n,
int is_le)
266 ret = s->cache & ((UINT64_C(1) << n) - 1);
269 ret = s->cache >> (64 - n);
276 static inline unsigned show_val(
const GetBitContext *s,
unsigned n)
278 #ifdef BITSTREAM_READER_LE 279 return s->cache & ((UINT64_C(1) << n) - 1);
281 return s->cache >> (64 - n);
295 #if CACHED_BITSTREAM_READER 298 #if UNCHECKED_BITSTREAM_READER 301 s->index += av_clip(n, -s->index, s->size_in_bits_plus8 - s->index);
306 #if CACHED_BITSTREAM_READER 307 static inline void skip_remaining(
GetBitContext *s,
unsigned n)
309 #ifdef BITSTREAM_READER_LE 325 #if CACHED_BITSTREAM_READER 326 int32_t cache = show_bits(s, 32);
327 int sign = ~cache >> 31;
328 skip_remaining(s, n);
330 return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
333 register int32_t cache;
337 cache = GET_CACHE(re, s);
339 LAST_SKIP_BITS(re, s, n);
341 return (NEG_USR32(sign ^ cache, n) ^ sign) - sign;
345 #if !CACHED_BITSTREAM_READER 349 register int32_t cache;
352 UPDATE_CACHE_LE(re, s);
353 cache = GET_CACHE(re, s);
354 sign = sign_extend(~cache, n) >> 31;
355 LAST_SKIP_BITS(re, s, n);
357 return (zero_extend(sign ^ cache, n) ^ sign) - sign;
364 #if CACHED_BITSTREAM_READER 366 tmp = sign_extend(get_bits(s, n), n);
371 tmp = SHOW_SBITS(re, s, n);
372 LAST_SKIP_BITS(re, s, n);
383 register unsigned int tmp;
384 #if CACHED_BITSTREAM_READER 387 if (n > s->bits_left) {
389 if (s->bits_left < 32)
393 #ifdef BITSTREAM_READER_LE 394 tmp = get_val(s, n, 1);
396 tmp = get_val(s, n, 0);
402 tmp = SHOW_UBITS(re, s, n);
403 LAST_SKIP_BITS(re, s, n);
413 static av_always_inline
int get_bitsz(
GetBitContext *s,
int n)
415 return n ? get_bits(s, n) : 0;
418 static inline unsigned int get_bits_le(
GetBitContext *s,
int n)
420 #if CACHED_BITSTREAM_READER 422 if (n > s->bits_left) {
424 if (s->bits_left < 32)
428 return get_val(s, n, 1);
433 UPDATE_CACHE_LE(re, s);
434 tmp = SHOW_UBITS_LE(re, s, n);
435 LAST_SKIP_BITS(re, s, n);
444 static inline unsigned int show_bits(
GetBitContext *s,
int n)
446 register unsigned int tmp;
447 #if CACHED_BITSTREAM_READER 448 if (n > s->bits_left)
451 tmp = show_val(s, n);
453 OPEN_READER_NOSIZE(re, s);
456 tmp = SHOW_UBITS(re, s, n);
463 #if CACHED_BITSTREAM_READER 464 if (n < s->bits_left)
465 skip_remaining(s, n);
472 unsigned skip = (n / 8) * 8;
479 skip_remaining(s, n);
483 LAST_SKIP_BITS(re, s, n);
490 #if CACHED_BITSTREAM_READER 494 #ifdef BITSTREAM_READER_LE 495 return get_val(s, 1, 1);
497 return get_val(s, 1, 0);
500 unsigned int index = s->index;
501 uint8_t result = s->buffer[index >> 3];
502 #ifdef BITSTREAM_READER_LE 503 result >>= index & 7;
506 result <<= index & 7;
509 #if !UNCHECKED_BITSTREAM_READER 510 if (s->index < s->size_in_bits_plus8)
521 return show_bits(s, 1);
532 static inline unsigned int get_bits_long(
GetBitContext *s,
int n)
537 #if CACHED_BITSTREAM_READER 539 return get_bits(s, n);
541 }
else if (n <= MIN_CACHE_BITS) {
542 return get_bits(s, n);
544 #ifdef BITSTREAM_READER_LE 545 unsigned ret = get_bits(s, 16);
546 return ret | (get_bits(s, n - 16) << 16);
548 unsigned ret = get_bits(s, 16) << (n - 16);
549 return ret | get_bits(s, n - 16);
561 return get_bits_long(s, n);
563 #ifdef BITSTREAM_READER_LE 564 uint64_t ret = get_bits_long(s, 32);
565 return ret | (uint64_t) get_bits_long(s, n - 32) << 32;
567 uint64_t ret = (uint64_t) get_bits_long(s, n - 32) << 32;
568 return ret | get_bits_long(s, 32);
582 return sign_extend(get_bits_long(s, n), n);
588 static inline unsigned int show_bits_long(
GetBitContext *s,
int n)
590 if (n <= MIN_CACHE_BITS) {
591 return show_bits(s, n);
594 return get_bits_long(&gb, n);
598 static inline int check_marker(
void *logctx,
GetBitContext *s,
const char *msg)
600 int bit = get_bits1(s);
603 get_bits_count(s) - 1, s->size_in_bits, msg);
616 static inline int init_get_bits(
GetBitContext *s,
const uint8_t *buffer,
628 buffer_size = (bit_size + 7) >> 3;
631 s->size_in_bits = bit_size;
632 s->size_in_bits_plus8 = bit_size + 8;
633 s->buffer_end = buffer + buffer_size;
636 #if CACHED_BITSTREAM_READER 651 static inline int init_get_bits8(
GetBitContext *s,
const uint8_t *buffer,
654 if (byte_size > INT_MAX / 8 || byte_size < 0)
656 return init_get_bits(s, buffer, byte_size * 8);
659 static inline const uint8_t *align_get_bits(
GetBitContext *s)
661 int n = -get_bits_count(s) & 7;
664 return s->buffer + (s->index >> 3);
672 #define GET_VLC(code, name, gb, table, bits, max_depth) \ 675 unsigned int index; \ 677 index = SHOW_UBITS(name, gb, bits); \ 678 code = table[index][0]; \ 679 n = table[index][1]; \ 681 if (max_depth > 1 && n < 0) { \ 682 LAST_SKIP_BITS(name, gb, bits); \ 683 UPDATE_CACHE(name, gb); \ 687 index = SHOW_UBITS(name, gb, nb_bits) + code; \ 688 code = table[index][0]; \ 689 n = table[index][1]; \ 690 if (max_depth > 2 && n < 0) { \ 691 LAST_SKIP_BITS(name, gb, nb_bits); \ 692 UPDATE_CACHE(name, gb); \ 696 index = SHOW_UBITS(name, gb, nb_bits) + code; \ 697 code = table[index][0]; \ 698 n = table[index][1]; \ 701 SKIP_BITS(name, gb, n); \ 704 #define GET_RL_VLC(level, run, name, gb, table, bits, \ 705 max_depth, need_update) \ 708 unsigned int index; \ 710 index = SHOW_UBITS(name, gb, bits); \ 711 level = table[index].level; \ 712 n = table[index].len; \ 714 if (max_depth > 1 && n < 0) { \ 715 SKIP_BITS(name, gb, bits); \ 717 UPDATE_CACHE(name, gb); \ 722 index = SHOW_UBITS(name, gb, nb_bits) + level; \ 723 level = table[index].level; \ 724 n = table[index].len; \ 725 if (max_depth > 2 && n < 0) { \ 726 LAST_SKIP_BITS(name, gb, nb_bits); \ 728 UPDATE_CACHE(name, gb); \ 732 index = SHOW_UBITS(name, gb, nb_bits) + level; \ 733 level = table[index].level; \ 734 n = table[index].len; \ 737 run = table[index].run; \ 738 SKIP_BITS(name, gb, n); \ 742 static inline int set_idx(
GetBitContext *s,
int code,
int *n,
int *nb_bits,
743 VLC_TYPE (*table)[2])
748 idx = show_bits(s, *nb_bits) + code;
751 return table[idx][0];
763 static av_always_inline
int get_vlc2(
GetBitContext *s, VLC_TYPE (*table)[2],
764 int bits,
int max_depth)
766 #if CACHED_BITSTREAM_READER 768 unsigned idx = show_bits(s, bits);
769 int code = table[idx][0];
770 int n = table[idx][1];
772 if (max_depth > 1 && n < 0) {
773 skip_remaining(s, bits);
774 code = set_idx(s, code, &n, &nb_bits, table);
775 if (max_depth > 2 && n < 0) {
776 skip_remaining(s, nb_bits);
777 code = set_idx(s, code, &n, &nb_bits, table);
780 skip_remaining(s, n);
789 GET_VLC(code, re, s, table, bits, max_depth);
804 return get_bits1(gb) + 1;
812 return 2 - get_bits1(gb);
817 return gb->size_in_bits - get_bits_count(gb);
822 if (get_bits_left(gb) <= 0)
825 while (get_bits1(gb)) {
827 if (get_bits_left(gb) <= 0)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
simple assert() macros that are a bit more flexible than ISO C assert().
#define GET_VLC(code, name, gb, table, bits, max_depth)
If the vlc code is invalid and max_depth=1, then no bits will be removed.
Definition: get_bits.h:672
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
Definition: get_bits.h:61
common internal and external API header
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:786