2 * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.h.
4 * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
6 * Licensed under the GNU Lesser General Public License.
7 * For licencing details see COPYING.LIB.
10 /** \file bitstream.h Bitstream structures and inline functions. */
12 #define AV_RB32(x) ((((const uint8_t*)(x))[0] << 24) | \
13 (((const uint8_t*)(x))[1] << 16) | \
14 (((const uint8_t*)(x))[2] << 8) | \
15 ((const uint8_t*)(x))[3])
17 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
20 * Structure for bistream I/O.
22 * The buffer, buffer_end and size_in_bits fields must be present and are used
25 struct getbit_context {
26 const uint8_t *buffer, *buffer_end;
31 #define VLC_TYPE int16_t
35 VLC_TYPE(*table)[2]; ///< code, bits
36 int table_size, table_allocated;
39 /** Load \a gb into local variables. */
40 #define OPEN_READER(name, gb)\
41 int name##_index= (gb)->index;\
44 /** Store local vars in gb. */
45 #define CLOSE_READER(name, gb)\
46 (gb)->index= name##_index;\
49 * Refill the internal cache from the bitstream.
51 #define UPDATE_CACHE(name, gb)\
52 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer) \
53 + (name##_index >> 3) ) << (name##_index & 0x07);\
56 * Remove the next num bits from the cache.
58 * SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER).
60 #define SKIP_CACHE(name, gb, num)\
61 name##_cache <<= (num);
64 * Increment the internal bit counter.
66 * \sa SKIP_CACHE, SKIP_BITS.
68 #define SKIP_COUNTER(name, gb, num)\
69 name##_index += (num);\
72 * Skip over the next num bits.
74 * This is equivalent to SKIP_CACHE; SKIP_COUNTER.
76 #define SKIP_BITS(name, gb, num)\
78 SKIP_CACHE(name, gb, num)\
79 SKIP_COUNTER(name, gb, num)\
82 /** This is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER. */
83 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
85 /** Return the next num bits. */
86 #define SHOW_UBITS(name, gb, num)\
87 NEG_USR32(name##_cache, num)
89 static inline int get_bits_count(struct getbit_context *s)
97 static inline unsigned int get_bits(struct getbit_context *s, int n)
102 tmp = SHOW_UBITS(re, s, n);
103 LAST_SKIP_BITS(re, s, n)
111 static inline unsigned int show_bits(struct getbit_context *s, int n)
116 tmp = SHOW_UBITS(re, s, n);
120 static inline void skip_bits(struct getbit_context *s, int n)
122 /* gcc seems to optimize this to s->index+=n for the ALT_READER :)) */
125 LAST_SKIP_BITS(re, s, n)
129 static inline unsigned int get_bits1(struct getbit_context *s)
132 uint8_t result = s->buffer[idx >> 3];
134 result <<= (idx & 0x07);
142 * Initialize a getbit_context structure.
144 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
145 * bytes larger then the actual read bits * because some optimized bitstream
146 * readers read 32 or 64 bit at once and could read over the end.
148 * \param bit_size The size of the buffer in bits.
150 static inline void init_get_bits(struct getbit_context *s,
151 const uint8_t * buffer, int bit_size)
153 int buffer_size = (bit_size + 7) >> 3;
154 if (buffer_size < 0 || bit_size < 0) {
155 buffer_size = bit_size = 0;
159 s->size_in_bits = bit_size;
160 s->buffer_end = buffer + buffer_size;
164 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
165 const void *codes, int codes_size);
167 void free_vlc(struct vlc *vlc);
171 * if the vlc code is invalid and max_depth=1 than no bits will be removed
172 * if the vlc code is invalid and max_depth>1 than the number of bits removed
175 #define GET_VLC(code, name, gb, table, bits, max_depth)\
177 int n, index, nb_bits;\
179 index= SHOW_UBITS(name, gb, bits);\
180 code = table[index][0];\
181 n = table[index][1];\
183 if(max_depth > 1 && n < 0){\
184 LAST_SKIP_BITS(name, gb, bits)\
185 UPDATE_CACHE(name, gb)\
189 index= SHOW_UBITS(name, gb, nb_bits) + code;\
190 code = table[index][0];\
191 n = table[index][1];\
192 if(max_depth > 2 && n < 0){\
193 LAST_SKIP_BITS(name, gb, nb_bits)\
194 UPDATE_CACHE(name, gb)\
198 index= SHOW_UBITS(name, gb, nb_bits) + code;\
199 code = table[index][0];\
200 n = table[index][1];\
203 SKIP_BITS(name, gb, n)\