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 /** Structure for bistream I/O. */
13 struct getbit_context {
14 /** Start of the internal buffer. */
15 const uint8_t *buffer;
16 /** End of the internal buffer. */
17 const uint8_t *buffer_end;
22 #define VLC_TYPE int16_t
24 /** A variable length code table. */
26 /** Number of bits of the table. */
28 /** The code and the bits table. */
30 /** The size of the table. */
32 /** Amount of memory allocated so far. */
36 static inline uint32_t show_bits(struct getbit_context *gbc, int num)
39 const uint8_t *p = gbc->buffer + (idx >> 3);
40 uint32_t x = ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
41 return (x << (idx & 7)) >> (32 - num);
44 static inline int get_bits_count(struct getbit_context *gbc)
49 static inline void skip_bits(struct getbit_context *gbc, int n)
54 static inline unsigned int get_bits(struct getbit_context *gbc, int n)
56 unsigned int ret = show_bits(gbc, n);
61 /* This is rather hot, we can do better than get_bits(gbc, 1). */
62 static inline unsigned int get_bit(struct getbit_context *gbc)
64 int idx = gbc->index++;
65 uint8_t tmp = gbc->buffer[idx >> 3], mask = (1 << (7 - (idx & 7)));
66 return !!(tmp & mask);
70 * Initialize a getbit_context structure.
72 * \param buffer The bitstream buffer. It must be 4 bytes larger then the
73 * actual read bits because the bitstream reader might read 32 bits at once and
74 * could read over the end.
76 * \param bit_size The size of the buffer in bytes.
78 static inline void init_get_bits(struct getbit_context *gbc,
79 const uint8_t *buffer, int size)
82 gbc->buffer_end = buffer + size;
86 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
87 const void *codes, int codes_size);
88 void free_vlc(struct vlc *vlc);
89 int get_vlc(struct getbit_context *gbc, VLC_TYPE(*table)[2], int bits,