Go to the documentation of this file.
27 #ifndef AVCODEC_GET_BITS_H
28 #define AVCODEC_GET_BITS_H
54 #ifndef UNCHECKED_BITSTREAM_READER
55 #define UNCHECKED_BITSTREAM_READER !CONFIG_SAFE_BITSTREAM_READER
58 #ifndef CACHED_BITSTREAM_READER
59 #define CACHED_BITSTREAM_READER 0
64 #if CACHED_BITSTREAM_READER
124 #if CACHED_BITSTREAM_READER
125 # define MIN_CACHE_BITS 64
126 #elif defined LONG_BITSTREAM_READER
127 # define MIN_CACHE_BITS 32
129 # define MIN_CACHE_BITS 25
132 #if !CACHED_BITSTREAM_READER
134 #define OPEN_READER_NOSIZE(name, gb) \
135 unsigned int name ## _index = (gb)->index; \
136 unsigned int av_unused name ## _cache
138 #if UNCHECKED_BITSTREAM_READER
139 #define OPEN_READER(name, gb) OPEN_READER_NOSIZE(name, gb)
141 #define BITS_AVAILABLE(name, gb) 1
143 #define OPEN_READER(name, gb) \
144 OPEN_READER_NOSIZE(name, gb); \
145 unsigned int name ## _size_plus8 = (gb)->size_in_bits_plus8
147 #define BITS_AVAILABLE(name, gb) name ## _index < name ## _size_plus8
150 #define CLOSE_READER(name, gb) (gb)->index = name ## _index
152 # ifdef LONG_BITSTREAM_READER
154 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
155 AV_RL64((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
157 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
158 AV_RB64((gb)->buffer + (name ## _index >> 3)) >> (32 - (name ## _index & 7))
162 # define UPDATE_CACHE_LE(name, gb) name ## _cache = \
163 AV_RL32((gb)->buffer + (name ## _index >> 3)) >> (name ## _index & 7)
165 # define UPDATE_CACHE_BE(name, gb) name ## _cache = \
166 AV_RB32((gb)->buffer + (name ## _index >> 3)) << (name ## _index & 7)
171 #ifdef BITSTREAM_READER_LE
173 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_LE(name, gb)
175 # define SKIP_CACHE(name, gb, num) name ## _cache >>= (num)
179 # define UPDATE_CACHE(name, gb) UPDATE_CACHE_BE(name, gb)
181 # define SKIP_CACHE(name, gb, num) name ## _cache <<= (num)
185 #if UNCHECKED_BITSTREAM_READER
186 # define SKIP_COUNTER(name, gb, num) name ## _index += (num)
188 # define SKIP_COUNTER(name, gb, num) \
189 name ## _index = FFMIN(name ## _size_plus8, name ## _index + (num))
192 #define BITS_LEFT(name, gb) ((int)((gb)->size_in_bits - name ## _index))
194 #define SKIP_BITS(name, gb, num) \
196 SKIP_CACHE(name, gb, num); \
197 SKIP_COUNTER(name, gb, num); \
200 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
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)
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)
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)
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)
216 #define GET_CACHE(name, gb) ((uint32_t) name ## _cache)
222 #if CACHED_BITSTREAM_READER
223 return s->index -
s->bits_left;
229 #if CACHED_BITSTREAM_READER
232 #if !UNCHECKED_BITSTREAM_READER
233 if (
s->index >> 3 >=
s->buffer_end -
s->buffer)
238 s->cache = (uint64_t)
AV_RL32(
s->buffer + (
s->index >> 3)) <<
s->bits_left |
s->cache;
240 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)
253 s->cache =
AV_RL64(
s->buffer + (
s->index >> 3));
255 s->cache =
AV_RB64(
s->buffer + (
s->index >> 3));
260 static inline uint64_t get_val(
GetBitContext *
s,
unsigned n,
int is_le)
265 ret =
s->cache & ((UINT64_C(1) << n) - 1);
268 ret =
s->cache >> (64 - n);
275 static inline unsigned show_val(
const GetBitContext *
s,
unsigned n)
277 #ifdef BITSTREAM_READER_LE
278 return s->cache & ((UINT64_C(1) << n) - 1);
280 return s->cache >> (64 - n);
294 #if CACHED_BITSTREAM_READER
297 #if UNCHECKED_BITSTREAM_READER
300 s->index +=
av_clip(n, -
s->index,
s->size_in_bits_plus8 -
s->index);
305 #if CACHED_BITSTREAM_READER
308 #ifdef BITSTREAM_READER_LE
324 #if CACHED_BITSTREAM_READER
326 int sign = ~cache >> 31;
327 skip_remaining(
s, n);
329 return ((((uint32_t)(sign ^ cache)) >> (32 - n)) ^ sign) - sign;
340 return (
NEG_USR32(sign ^ cache, n) ^ sign) - sign;
344 #if !CACHED_BITSTREAM_READER
356 return (
zero_extend(sign ^ cache, n) ^ sign) - sign;
363 #if CACHED_BITSTREAM_READER
382 register unsigned int tmp;
383 #if CACHED_BITSTREAM_READER
386 if (n >
s->bits_left) {
387 #ifdef BITSTREAM_READER_LE
392 if (
s->bits_left < 32)
396 #ifdef BITSTREAM_READER_LE
397 tmp = get_val(
s, n, 1);
399 tmp = get_val(
s, n, 0);
423 #if CACHED_BITSTREAM_READER
425 if (n >
s->bits_left) {
427 if (
s->bits_left < 32)
431 return get_val(
s, n, 1);
449 register unsigned int tmp;
450 #if CACHED_BITSTREAM_READER
451 if (n >
s->bits_left)
452 #ifdef BITSTREAM_READER_LE
458 tmp = show_val(
s, n);
470 #if CACHED_BITSTREAM_READER
471 if (n < s->bits_left)
472 skip_remaining(
s, n);
479 unsigned skip = (n / 8) * 8;
484 #ifdef BITSTREAM_READER_LE
490 skip_remaining(
s, n);
501 #if CACHED_BITSTREAM_READER
503 #ifdef BITSTREAM_READER_LE
509 #ifdef BITSTREAM_READER_LE
510 return get_val(
s, 1, 1);
512 return get_val(
s, 1, 0);
515 unsigned int index =
s->index;
517 #ifdef BITSTREAM_READER_LE
524 #if !UNCHECKED_BITSTREAM_READER
525 if (
s->index <
s->size_in_bits_plus8)
552 #if CACHED_BITSTREAM_READER
559 #ifdef BITSTREAM_READER_LE
578 #ifdef BITSTREAM_READER_LE
624 int bit_size,
int is_le)
635 buffer_size = (bit_size + 7) >> 3;
638 s->size_in_bits = bit_size;
639 s->size_in_bits_plus8 = bit_size + 8;
640 s->buffer_end =
buffer + buffer_size;
643 #if CACHED_BITSTREAM_READER
663 #ifdef BITSTREAM_READER_LE
681 if (byte_size > INT_MAX / 8 || byte_size < 0)
689 if (byte_size > INT_MAX / 8 || byte_size < 0)
699 return s->buffer + (
s->index >> 3);
707 #define GET_VLC(code, name, gb, table, bits, max_depth) \
710 unsigned int index; \
712 index = SHOW_UBITS(name, gb, bits); \
713 code = table[index][0]; \
714 n = table[index][1]; \
716 if (max_depth > 1 && n < 0) { \
717 LAST_SKIP_BITS(name, gb, bits); \
718 UPDATE_CACHE(name, gb); \
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); \
731 index = SHOW_UBITS(name, gb, nb_bits) + code; \
732 code = table[index][0]; \
733 n = table[index][1]; \
736 SKIP_BITS(name, gb, n); \
739 #define GET_RL_VLC(level, run, name, gb, table, bits, \
740 max_depth, need_update) \
743 unsigned int index; \
745 index = SHOW_UBITS(name, gb, bits); \
746 level = table[index].level; \
747 n = table[index].len; \
749 if (max_depth > 1 && n < 0) { \
750 SKIP_BITS(name, gb, bits); \
752 UPDATE_CACHE(name, gb); \
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); \
763 UPDATE_CACHE(name, gb); \
767 index = SHOW_UBITS(name, gb, nb_bits) + level; \
768 level = table[index].level; \
769 n = table[index].len; \
772 run = table[index].run; \
773 SKIP_BITS(name, gb, n); \
786 return table[idx][0];
799 int bits,
int max_depth)
801 #if CACHED_BITSTREAM_READER
805 int n =
table[idx][1];
807 if (max_depth > 1 && n < 0) {
808 skip_remaining(
s,
bits);
810 if (max_depth > 2 && n < 0) {
811 skip_remaining(
s, nb_bits);
815 skip_remaining(
s, n);
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
static unsigned int show_bits1(GetBitContext *s)
static int get_bits_left(GetBitContext *gb)
#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 get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
static int get_bits_count(const GetBitContext *s)
static const uint16_t table[]
static av_const unsigned zero_extend(unsigned val, unsigned bits)
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
#define UPDATE_CACHE(name, gb)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define OPEN_READER_NOSIZE(name, gb)
#define bit(string, value)
#define GET_CACHE(name, gb)
static void skip_bits(GetBitContext *s, int n)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
#define CLOSE_READER(name, gb)
#define SHOW_SBITS(name, gb, num)
static unsigned int get_bits_le(GetBitContext *s, int n)
static int get_sbits(GetBitContext *s, int n)
static int decode012(GetBitContext *gb)
#define SHOW_UBITS_LE(name, gb, num)
and forward the result(frame or status change) to the corresponding input. If nothing is possible
static unsigned int get_bits1(GetBitContext *s)
#define LAST_SKIP_BITS(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_WB64 unsigned int_TMPL AV_RB32
#define OPEN_READER(name, gb)
static int get_xbits(GetBitContext *s, int n)
Read MPEG-1 dc-style VLC (sign bit + mantissa with no MSB).
static void skip_bits1(GetBitContext *s)
#define AV_LOG_INFO
Standard information.
static uint64_t get_bits64(GetBitContext *s, int n)
Read 0-64 bits.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
const uint8_t * buffer_end
static int get_xbits_le(GetBitContext *s, int n)
static const uint8_t * align_get_bits(GetBitContext *s)
#define AV_INPUT_BUFFER_PADDING_SIZE
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
static int skip_1stop_8data_bits(GetBitContext *gb)
#define SHOW_UBITS(name, gb, num)
#define UPDATE_CACHE_LE(name, gb)
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
static int init_get_bits_xe(GetBitContext *s, const uint8_t *buffer, int bit_size, int is_le)
static av_const int sign_extend(int val, unsigned bits)
static int decode210(GetBitContext *gb)
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
static int check_marker(void *logctx, GetBitContext *s, const char *msg)
static int init_get_bits8_le(GetBitContext *s, const uint8_t *buffer, int byte_size)
static int set_idx(GetBitContext *s, int code, int *n, int *nb_bits, VLC_TYPE(*table)[2])
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
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