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, see file COPYING.LIB.
9 /** \file bitstream.h Bitstream structures and inline functions. */
11 /** Structure for bistream I/O. */
12 struct getbit_context {
13 /** Start of the internal buffer. */
14 const uint8_t *buffer;
15 /** Length of buffer in bits (always a multiple of 8). */
21 /** A variable length code table. */
23 /** Number of bits of the table. */
25 /** The code and the bits table. */
27 /** The size of the table. */
29 /** Amount of memory allocated so far. */
33 static inline uint32_t show_bits(struct getbit_context *gbc, int num)
39 assert(idx + num <= gbc->num_bits);
40 p = (const char *)gbc->buffer + (idx >> 3);
42 return (x << (idx & 7)) >> (32 - num);
45 static inline int get_bits_count(struct getbit_context *gbc)
50 static inline void skip_bits(struct getbit_context *gbc, int n)
52 assert(gbc->index + n <= gbc->num_bits);
56 static inline unsigned int get_bits(struct getbit_context *gbc, int n)
58 unsigned int ret = show_bits(gbc, n); /* checks n */
63 /* This is rather hot, we can do better than get_bits(gbc, 1). */
64 static inline unsigned int get_bit(struct getbit_context *gbc)
69 assert(gbc->index < gbc->num_bits);
71 tmp = gbc->buffer[idx >> 3];
72 mask = 1 << (7 - (idx & 7));
73 return !!(tmp & mask);
77 * Initialize a getbit_context structure.
79 * \param gbc The structure to initialize.
80 * \param buffer The bitstream buffer.
81 * \param size The size of the buffer in bytes.
83 * The bitstream buffer must be 4 bytes larger then the actual read bits
84 * because the bitstream reader might read 32 bits at once and could read over
87 static inline void init_get_bits(struct getbit_context *gbc,
88 const uint8_t *buffer, int size)
91 gbc->num_bits = size * 8;
95 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
96 const void *codes, int codes_size);
97 void free_vlc(struct vlc *vlc);
98 int get_vlc(struct getbit_context *gbc, const struct vlc *vlc);