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
232 if (
s->index >> 3 >=
s->buffer_end -
s->buffer)
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);
246 #if !UNCHECKED_BITSTREAM_READER
247 if (
s->index >> 3 >=
s->buffer_end -
s->buffer)
252 s->cache =
AV_RL64(
s->buffer + (
s->index >> 3));
254 s->cache =
AV_RB64(
s->buffer + (
s->index >> 3));
259 static inline uint64_t get_val(
GetBitContext *
s,
unsigned n,
int is_le)
264 ret =
s->cache & ((UINT64_C(1) << n) - 1);
267 ret =
s->cache >> (64 - n);
274 static inline unsigned show_val(
const GetBitContext *
s,
unsigned n)
276 #ifdef BITSTREAM_READER_LE
277 return s->cache & ((UINT64_C(1) << n) - 1);
279 return s->cache >> (64 - n);
293 #if CACHED_BITSTREAM_READER
296 #if UNCHECKED_BITSTREAM_READER
299 s->index +=
av_clip(n, -
s->index,
s->size_in_bits_plus8 -
s->index);
304 #if CACHED_BITSTREAM_READER
307 #ifdef BITSTREAM_READER_LE
323 #if CACHED_BITSTREAM_READER
325 int sign = ~cache >> 31;
326 skip_remaining(
s, n);
328 return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
339 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
343 #if !CACHED_BITSTREAM_READER
355 return (
zero_extend(sign ^ cache, n) ^ sign) - sign;
362 #if CACHED_BITSTREAM_READER
381 register unsigned int tmp;
382 #if CACHED_BITSTREAM_READER
385 if (n >
s->bits_left) {
386 #ifdef BITSTREAM_READER_LE
391 if (
s->bits_left < 32)
395 #ifdef BITSTREAM_READER_LE
396 tmp = get_val(
s, n, 1);
398 tmp = get_val(
s, n, 0);
422 #if CACHED_BITSTREAM_READER
424 if (n >
s->bits_left) {
426 if (
s->bits_left < 32)
430 return get_val(
s, n, 1);
448 register unsigned int tmp;
449 #if CACHED_BITSTREAM_READER
450 if (n >
s->bits_left)
451 #ifdef BITSTREAM_READER_LE
457 tmp = show_val(
s, n);
469 #if CACHED_BITSTREAM_READER
470 if (n < s->bits_left)
471 skip_remaining(
s, n);
478 unsigned skip = (n / 8) * 8;
483 #ifdef BITSTREAM_READER_LE
489 skip_remaining(
s, n);
500 #if CACHED_BITSTREAM_READER
502 #ifdef BITSTREAM_READER_LE
508 #ifdef BITSTREAM_READER_LE
509 return get_val(
s, 1, 1);
511 return get_val(
s, 1, 0);
514 unsigned int index =
s->index;
516 #ifdef BITSTREAM_READER_LE
517 result >>=
index & 7;
520 result <<=
index & 7;
523 #if !UNCHECKED_BITSTREAM_READER
524 if (
s->index <
s->size_in_bits_plus8)
551 #if CACHED_BITSTREAM_READER
558 #ifdef BITSTREAM_READER_LE
560 return ret | (
get_bits(
s, n - 16) << 16);
562 unsigned ret =
get_bits(
s, 16) << (n - 16);
577 #ifdef BITSTREAM_READER_LE
623 int bit_size,
int is_le)
634 buffer_size = (bit_size + 7) >> 3;
637 s->size_in_bits = bit_size;
638 s->size_in_bits_plus8 = bit_size + 8;
639 s->buffer_end =
buffer + buffer_size;
642 #if CACHED_BITSTREAM_READER
662 #ifdef BITSTREAM_READER_LE
680 if (byte_size > INT_MAX / 8 || byte_size < 0)
688 if (byte_size > INT_MAX / 8 || byte_size < 0)
698 return s->buffer + (
s->index >> 3);
706 #define GET_VLC(code, name, gb, table, bits, max_depth) \
709 unsigned int index; \
711 index = SHOW_UBITS(name, gb, bits); \
712 code = table[index][0]; \
713 n = table[index][1]; \
715 if (max_depth > 1 && n < 0) { \
716 LAST_SKIP_BITS(name, gb, bits); \
717 UPDATE_CACHE(name, gb); \
721 index = SHOW_UBITS(name, gb, nb_bits) + code; \
722 code = table[index][0]; \
723 n = table[index][1]; \
724 if (max_depth > 2 && n < 0) { \
725 LAST_SKIP_BITS(name, gb, nb_bits); \
726 UPDATE_CACHE(name, gb); \
730 index = SHOW_UBITS(name, gb, nb_bits) + code; \
731 code = table[index][0]; \
732 n = table[index][1]; \
735 SKIP_BITS(name, gb, n); \
738 #define GET_RL_VLC(level, run, name, gb, table, bits, \
739 max_depth, need_update) \
742 unsigned int index; \
744 index = SHOW_UBITS(name, gb, bits); \
745 level = table[index].level; \
746 n = table[index].len; \
748 if (max_depth > 1 && n < 0) { \
749 SKIP_BITS(name, gb, bits); \
751 UPDATE_CACHE(name, gb); \
756 index = SHOW_UBITS(name, gb, nb_bits) + level; \
757 level = table[index].level; \
758 n = table[index].len; \
759 if (max_depth > 2 && n < 0) { \
760 LAST_SKIP_BITS(name, gb, nb_bits); \
762 UPDATE_CACHE(name, gb); \
766 index = SHOW_UBITS(name, gb, nb_bits) + level; \
767 level = table[index].level; \
768 n = table[index].len; \
771 run = table[index].run; \
772 SKIP_BITS(name, gb, n); \
785 return table[idx][0];
798 int bits,
int max_depth)
800 #if CACHED_BITSTREAM_READER
804 int n =
table[idx][1];
806 if (max_depth > 1 && n < 0) {
807 skip_remaining(
s,
bits);
809 if (max_depth > 2 && n < 0) {
810 skip_remaining(
s, nb_bits);
814 skip_remaining(
s, n);
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Libavcodec external API header.
#define bit(string, value)
common internal and external API header
static unsigned int get_bits_le(GetBitContext *s, int n)
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
#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.
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, int byte_size)
static int decode012(GetBitContext *gb)
#define GET_CACHE(name, gb)
static int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer, int bit_size, int is_le)
static int get_sbits(GetBitContext *s, int n)
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
#define CLOSE_READER(name, gb)
static int get_bits_left(GetBitContext *gb)
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static int get_xbits_le(GetBitContext *s, int n)
#define SHOW_UBITS(name, gb, num)
static unsigned int get_bits1(GetBitContext *s)
#define SHOW_SBITS(name, gb, num)
static int decode210(GetBitContext *gb)
#define OPEN_READER_NOSIZE(name, gb)
#define OPEN_READER(name, gb)
static void skip_bits(GetBitContext *s, int n)
static int skip_1stop_8data_bits(GetBitContext *gb)
#define UPDATE_CACHE(name, gb)
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
#define SHOW_UBITS_LE(name, gb, num)
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define LAST_SKIP_BITS(name, gb, num)
static int set_idx(GetBitContext *s, int code, int *n, int *nb_bits, VLC_TYPE(*table)[2])
static int get_bits_count(const GetBitContext *s)
static void skip_bits1(GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define UPDATE_CACHE_LE(name, gb)
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
static const uint8_t * align_get_bits(GetBitContext *s)
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static unsigned int show_bits1(GetBitContext *s)
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AV_LOG_INFO
Standard information.
static av_const unsigned zero_extend(unsigned val, unsigned bits)
static av_const int sign_extend(int val, unsigned bits)
static const uint16_t table[]
const uint8_t * buffer_end