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)))
19 /** Structure for bistream I/O. */
20 struct getbit_context
{
21 /* Start of the internal buffer. */
22 const uint8_t *buffer
;
23 /* End of the internal buffer. */
24 const uint8_t *buffer_end
;
29 #define VLC_TYPE int16_t
33 VLC_TYPE(*table
)[2]; ///< code, bits
34 int table_size
, table_allocated
;
37 /** Load \a gb into local variables. */
38 #define OPEN_READER(name, gb)\
39 int name##_index= (gb)->index;\
42 /** Store local vars in gb. */
43 #define CLOSE_READER(name, gb)\
44 (gb)->index= name##_index;\
47 * Refill the internal cache from the bitstream.
49 #define UPDATE_CACHE(name, gb)\
50 name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer) \
51 + (name##_index >> 3) ) << (name##_index & 0x07);\
54 * Remove the next num bits from the cache.
56 * SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER).
58 #define SKIP_CACHE(name, gb, num)\
59 name##_cache <<= (num);
62 * Increment the internal bit counter.
64 * \sa SKIP_CACHE, SKIP_BITS.
66 #define SKIP_COUNTER(name, gb, num)\
67 name##_index += (num);\
70 * Skip over the next num bits.
72 * This is equivalent to SKIP_CACHE; SKIP_COUNTER.
74 #define SKIP_BITS(name, gb, num)\
76 SKIP_CACHE(name, gb, num)\
77 SKIP_COUNTER(name, gb, num)\
80 /** This is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER. */
81 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
83 /** Return the next num bits. */
84 #define SHOW_UBITS(name, gb, num)\
85 NEG_USR32(name##_cache, num)
87 static inline int get_bits_count(struct getbit_context
*s
)
95 static inline unsigned int get_bits(struct getbit_context
*s
, int n
)
100 tmp
= SHOW_UBITS(re
, s
, n
);
101 LAST_SKIP_BITS(re
, s
, n
)
106 static inline void skip_bits(struct getbit_context
*s
, int n
)
108 /* gcc seems to optimize this to s->index+=n for the ALT_READER :)) */
111 LAST_SKIP_BITS(re
, s
, n
)
115 static inline unsigned int get_bits1(struct getbit_context
*s
)
118 uint8_t result
= s
->buffer
[idx
>> 3];
120 result
<<= (idx
& 0x07);
128 * Initialize a getbit_context structure.
130 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
131 * bytes larger then the actual read bits * because some optimized bitstream
132 * readers read 32 or 64 bit at once and could read over the end.
134 * \param bit_size The size of the buffer in bits.
136 static inline void init_get_bits(struct getbit_context
*s
,
137 const uint8_t *buffer
, int bit_size
)
139 int buffer_size
= (bit_size
+ 7) >> 3;
140 if (buffer_size
< 0 || bit_size
< 0) {
141 buffer_size
= bit_size
= 0;
145 s
->buffer_end
= buffer
+ buffer_size
;
149 void init_vlc(struct vlc
*vlc
, int nb_bits
, int nb_codes
, const void *bits
,
150 const void *codes
, int codes_size
);
152 void free_vlc(struct vlc
*vlc
);
157 * \param bits The number of bits which will be read at once, must be
158 * identical to nb_bits in init_vlc()
160 * \param max_depth The number of times bits bits must be read to completely
161 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
163 static inline int get_vlc(struct getbit_context
*gb
, VLC_TYPE(*table
)[2],
164 int bits
, int max_depth
)
166 int n
, idx
, nb_bits
, code
;
170 idx
= SHOW_UBITS(re
, gb
, bits
);
171 code
= table
[idx
][0];
173 if (max_depth
> 1 && n
< 0) {
174 LAST_SKIP_BITS(re
, gb
, bits
)
177 idx
= SHOW_UBITS(re
, gb
, nb_bits
) + code
;
178 code
= table
[idx
][0];
180 if (max_depth
> 2 && n
< 0) {
181 LAST_SKIP_BITS(re
, gb
, nb_bits
)
184 idx
= SHOW_UBITS(re
, gb
, nb_bits
) + code
;
185 code
= table
[idx
][0];