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_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
18 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
21 * Structure for bistream I/O.
23 * The buffer, buffer_end and size_in_bits fields must be present and are used
26 struct getbit_context {
27 const uint8_t *buffer, *buffer_end;
32 #define VLC_TYPE int16_t
36 VLC_TYPE(*table)[2]; ///< code, bits
37 int table_size, table_allocated;
40 #define MIN_CACHE_BITS 25
42 /** Load \a gb into local variables. */
43 #define OPEN_READER(name, gb)\
44 int name##_index= (gb)->index;\
47 /** Store local vars in gb. */
48 #define CLOSE_READER(name, gb)\
49 (gb)->index= name##_index;\
52 * Refill the internal cache from the bitstream.
54 * After this call at least MIN_CACHE_BITS will be available.
56 #define UPDATE_CACHE(name, gb)\
57 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer) \
58 + (name##_index >> 3) ) << (name##_index & 0x07);\
61 * Remove the next num bits from the cache.
63 * SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER).
65 #define SKIP_CACHE(name, gb, num)\
66 name##_cache <<= (num);
69 * Increment the internal bit counter.
71 * \sa SKIP_CACHE, SKIP_BITS.
73 #define SKIP_COUNTER(name, gb, num)\
74 name##_index += (num);\
77 * Skip over the next num bits.
79 * This is equivalent to SKIP_CACHE; SKIP_COUNTER.
81 #define SKIP_BITS(name, gb, num)\
83 SKIP_CACHE(name, gb, num)\
84 SKIP_COUNTER(name, gb, num)\
87 /** This is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER. */
88 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
90 /** Return the next num bits. */
91 #define SHOW_UBITS(name, gb, num)\
92 NEG_USR32(name##_cache, num)
94 static inline int get_bits_count(struct getbit_context *s)
99 static inline void skip_bits_long(struct getbit_context *s, int n)
106 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
108 static inline unsigned int get_bits(struct getbit_context *s, int n)
113 tmp = SHOW_UBITS(re, s, n);
114 LAST_SKIP_BITS(re, s, n)
122 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2
125 static inline unsigned int show_bits(struct getbit_context *s, int n)
130 tmp = SHOW_UBITS(re, s, n);
131 // CLOSE_READER(re, s)
135 static inline void skip_bits(struct getbit_context *s, int n)
137 /* gcc seems to optimize this to s->index+=n for the ALT_READER :)) */
140 LAST_SKIP_BITS(re, s, n)
144 static inline unsigned int get_bits1(struct getbit_context *s)
147 uint8_t result = s->buffer[idx >> 3];
149 result <<= (idx & 0x07);
159 static inline unsigned int get_bits_long(struct getbit_context *s, int n)
162 return get_bits(s, n);
164 int ret = get_bits(s, 16) << (n - 16);
165 return ret | get_bits(s, n - 16);
170 * Initialize a getbit_context structure.
172 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
173 * bytes larger then the actual read bits * because some optimized bitstream
174 * readers read 32 or 64 bit at once and could read over the end.
176 * \param bit_size The size of the buffer in bits.
178 static inline void init_get_bits(struct getbit_context *s,
179 const uint8_t * buffer, int bit_size)
181 int buffer_size = (bit_size + 7) >> 3;
182 if (buffer_size < 0 || bit_size < 0) {
183 buffer_size = bit_size = 0;
187 s->size_in_bits = bit_size;
188 s->buffer_end = buffer + buffer_size;
192 int init_vlc(struct vlc *vlc, int nb_bits, int nb_codes,
193 const void *bits, int bits_wrap, int bits_size,
194 const void *codes, int codes_wrap, int codes_size);
196 void free_vlc(struct vlc *vlc);
200 * if the vlc code is invalid and max_depth=1 than no bits will be removed
201 * if the vlc code is invalid and max_depth>1 than the number of bits removed
204 #define GET_VLC(code, name, gb, table, bits, max_depth)\
206 int n, index, nb_bits;\
208 index= SHOW_UBITS(name, gb, bits);\
209 code = table[index][0];\
210 n = table[index][1];\
212 if(max_depth > 1 && n < 0){\
213 LAST_SKIP_BITS(name, gb, bits)\
214 UPDATE_CACHE(name, gb)\
218 index= SHOW_UBITS(name, gb, nb_bits) + code;\
219 code = table[index][0];\
220 n = table[index][1];\
221 if(max_depth > 2 && n < 0){\
222 LAST_SKIP_BITS(name, gb, nb_bits)\
223 UPDATE_CACHE(name, gb)\
227 index= SHOW_UBITS(name, gb, nb_bits) + code;\
228 code = table[index][0];\
229 n = table[index][1];\
232 SKIP_BITS(name, gb, n)\