27 #ifndef AVCODEC_GET_BITS_H
28 #define AVCODEC_GET_BITS_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
63 #if CACHED_BITSTREAM_READER
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
236 #ifdef BITSTREAM_READER_LE
239 s->cache = s->cache | (uint64_t)
AV_RB32(s->
buffer + (s->
index >> 3)) << (32 - s->bits_left);
247 #if !UNCHECKED_BITSTREAM_READER
252 #ifdef BITSTREAM_READER_LE
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
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
327 int sign = ~cache >> 31;
328 skip_remaining(s, n);
330 return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
341 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
345 #if !CACHED_BITSTREAM_READER
357 return (
zero_extend(sign ^ cache, n) ^ sign) - sign;
364 #if CACHED_BITSTREAM_READER
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);
419 #if CACHED_BITSTREAM_READER
421 if (n > s->bits_left) {
423 if (s->bits_left < 32)
427 return get_val(s, n, 1);
446 #if CACHED_BITSTREAM_READER
447 if (n > s->bits_left)
450 tmp = show_val(s, n);
462 #if CACHED_BITSTREAM_READER
463 if (n < s->bits_left)
464 skip_remaining(s, n);
471 unsigned skip = (n / 8) * 8;
478 skip_remaining(s, n);
489 #if CACHED_BITSTREAM_READER
493 #ifdef BITSTREAM_READER_LE
494 return get_val(s, 1, 1);
496 return get_val(s, 1, 0);
501 #ifdef BITSTREAM_READER_LE
502 result >>= index & 7;
505 result <<= index & 7;
508 #if !UNCHECKED_BITSTREAM_READER
536 #if CACHED_BITSTREAM_READER
543 #ifdef BITSTREAM_READER_LE
545 return ret | (
get_bits(s, n - 16) << 16);
547 unsigned ret =
get_bits(s, 16) << (n - 16);
562 #ifdef BITSTREAM_READER_LE
627 buffer_size = (bit_size + 7) >> 3;
635 #if CACHED_BITSTREAM_READER
653 if (byte_size > INT_MAX / 8 || byte_size < 0)
671 #define GET_VLC(code, name, gb, table, bits, max_depth) \
674 unsigned int index; \
676 index = SHOW_UBITS(name, gb, bits); \
677 code = table[index][0]; \
678 n = table[index][1]; \
680 if (max_depth > 1 && n < 0) { \
681 LAST_SKIP_BITS(name, gb, bits); \
682 UPDATE_CACHE(name, gb); \
686 index = SHOW_UBITS(name, gb, nb_bits) + code; \
687 code = table[index][0]; \
688 n = table[index][1]; \
689 if (max_depth > 2 && n < 0) { \
690 LAST_SKIP_BITS(name, gb, nb_bits); \
691 UPDATE_CACHE(name, gb); \
695 index = SHOW_UBITS(name, gb, nb_bits) + code; \
696 code = table[index][0]; \
697 n = table[index][1]; \
700 SKIP_BITS(name, gb, n); \
703 #define GET_RL_VLC(level, run, name, gb, table, bits, \
704 max_depth, need_update) \
707 unsigned int index; \
709 index = SHOW_UBITS(name, gb, bits); \
710 level = table[index].level; \
711 n = table[index].len; \
713 if (max_depth > 1 && n < 0) { \
714 SKIP_BITS(name, gb, bits); \
716 UPDATE_CACHE(name, gb); \
721 index = SHOW_UBITS(name, gb, nb_bits) + level; \
722 level = table[index].level; \
723 n = table[index].len; \
724 if (max_depth > 2 && n < 0) { \
725 LAST_SKIP_BITS(name, gb, nb_bits); \
727 UPDATE_CACHE(name, gb); \
731 index = SHOW_UBITS(name, gb, nb_bits) + level; \
732 level = table[index].level; \
733 n = table[index].len; \
736 run = table[index].run; \
737 SKIP_BITS(name, gb, n); \
750 return table[idx][0];
763 int bits,
int max_depth)
765 #if CACHED_BITSTREAM_READER
768 int code =
table[idx][0];
769 int n =
table[idx][1];
771 if (max_depth > 1 && n < 0) {
772 skip_remaining(s, bits);
774 if (max_depth > 2 && n < 0) {
775 skip_remaining(s, nb_bits);
779 skip_remaining(s, n);
static int get_xbits_le(GetBitContext *s, int n)
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static unsigned int show_bits1(GetBitContext *s)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static int set_idx(GetBitContext *s, int code, int *n, int *nb_bits, VLC_TYPE(*table)[2])
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
static av_const unsigned zero_extend(unsigned val, unsigned bits)
static int get_sbits(GetBitContext *s, int n)
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
static int get_bits_count(const GetBitContext *s)
#define SHOW_UBITS_LE(name, gb, num)
static const uint16_t table[]
static int get_bits_left(GetBitContext *gb)
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
#define UPDATE_CACHE(name, gb)
simple assert() macros that are a bit more flexible than ISO C assert().
#define CLOSE_READER(name, gb)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
#define LAST_SKIP_BITS(name, gb, num)
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
#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.
#define SHOW_UBITS(name, gb, num)
static int decode210(GetBitContext *gb)
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
#define OPEN_READER(name, gb)
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
#define UPDATE_CACHE_LE(name, gb)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define GET_CACHE(name, gb)
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static av_const int sign_extend(int val, unsigned bits)
static unsigned int get_bits_le(GetBitContext *s, int n)
#define SHOW_SBITS(name, gb, num)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_RB64
#define OPEN_READER_NOSIZE(name, gb)
common internal and external API header
const uint8_t * buffer_end
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
static int decode012(GetBitContext *gb)
static const uint8_t * align_get_bits(GetBitContext *s)
static int skip_1stop_8data_bits(GetBitContext *gb)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.