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 /** Structure for bistream I/O. */
18 struct getbit_context
{
19 /* Start of the internal buffer. */
20 const uint8_t *buffer
;
21 /* End of the internal buffer. */
22 const uint8_t *buffer_end
;
27 #define VLC_TYPE int16_t
31 VLC_TYPE(*table
)[2]; ///< code, bits
32 int table_size
, table_allocated
;
35 static inline uint32_t show_bits(struct getbit_context
*gbc
, int num
)
38 uint32_t x
= AV_RB32(gbc
->buffer
+ (idx
>> 3)) << (idx
& 7);
39 return x
>> (32 - num
);
42 static inline int get_bits_count(struct getbit_context
*gbc
)
47 static inline void skip_bits(struct getbit_context
*gbc
, int n
)
52 static inline unsigned int get_bits(struct getbit_context
*gbc
, int n
)
54 unsigned int ret
= show_bits(gbc
, n
);
59 static inline unsigned int get_bits1(struct getbit_context
*gbc
)
62 uint8_t result
= gbc
->buffer
[idx
>> 3];
64 result
<<= (idx
& 0x07);
72 * Initialize a getbit_context structure.
74 * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
75 * bytes larger then the actual read bits * because some optimized bitstream
76 * readers read 32 or 64 bit at once and could read over the end.
78 * \param bit_size The size of the buffer in bytes.
80 static inline void init_get_bits(struct getbit_context
*gbc
,
81 const uint8_t *buffer
, int size
)
84 gbc
->buffer_end
= buffer
+ size
;
88 void init_vlc(struct vlc
*vlc
, int nb_bits
, int nb_codes
, const void *bits
,
89 const void *codes
, int codes_size
);
91 void free_vlc(struct vlc
*vlc
);
96 * \param bits The number of bits which will be read at once, must be
97 * identical to nb_bits in init_vlc()
99 * \param max_depth The number of times bits bits must be read to completely
100 * read the longest vlc code = (max_vlc_length + bits - 1) / bits.
102 static inline int get_vlc(struct getbit_context
*gbc
, VLC_TYPE(*table
)[2],
103 int bits
, int max_depth
)
105 int n
, idx
, nb_bits
, code
;
107 idx
= show_bits(gbc
, bits
);
108 code
= table
[idx
][0];
110 if (max_depth
> 1 && n
< 0) {
111 skip_bits(gbc
, bits
);
113 idx
= show_bits(gbc
, nb_bits
) + code
;
114 code
= table
[idx
][0];
116 if (max_depth
> 2 && n
< 0) {
117 skip_bits(gbc
, nb_bits
);
119 idx
= show_bits(gbc
, nb_bits
) + code
;
120 code
= table
[idx
][0];