wma: Remove pointless VLC_TYPE define.
[paraslash.git] / bitstream.h
1 /*
2  * Extracted 2009 from mplayer 2009-02-10 libavcodec/bitstream.h.
3  *
4  * copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * Licensed under the GNU Lesser General Public License.
7  * For licencing details see COPYING.LIB.
8  */
9
10 /** \file bitstream.h Bitstream structures and inline functions. */
11
12 /** Structure for bistream I/O. */
13 struct getbit_context {
14         /** Start of the internal buffer. */
15         const uint8_t *buffer;
16         /** Length of buffer in bits (always a multiple of 8). */
17         uint32_t num_bits;
18         /** Bit counter. */
19         int index;
20 };
21
22 /** A variable length code table. */
23 struct vlc {
24         /** Number of bits of the table. */
25         int bits;
26         /** The code and the bits table. */
27         int16_t (*table)[2];
28         /** The size of the table. */
29         int table_size;
30         /** Amount of memory allocated so far. */
31         int table_allocated;
32 };
33
34 static inline uint32_t show_bits(struct getbit_context *gbc, int num)
35 {
36         int idx = gbc->index;
37         const char *p;
38         uint32_t x;
39
40         assert(idx + num <= gbc->num_bits);
41         p = (const char *)gbc->buffer + (idx >> 3);
42         x = read_u32_be(p);
43         return (x << (idx & 7)) >> (32 - num);
44 }
45
46 static inline int get_bits_count(struct getbit_context *gbc)
47 {
48         return gbc->index;
49 }
50
51 static inline void skip_bits(struct getbit_context *gbc, int n)
52 {
53         assert(gbc->index + n <= gbc->num_bits);
54         gbc->index += n;
55 }
56
57 static inline unsigned int get_bits(struct getbit_context *gbc, int n)
58 {
59         unsigned int ret = show_bits(gbc, n); /* checks n */
60         skip_bits(gbc, n);
61         return ret;
62 }
63
64 /* This is rather hot, we can do better than get_bits(gbc, 1). */
65 static inline unsigned int get_bit(struct getbit_context *gbc)
66 {
67         int idx;
68         uint8_t tmp, mask;
69
70         assert(gbc->index < gbc->num_bits);
71         idx = gbc->index++;
72         tmp = gbc->buffer[idx >> 3];
73         mask = 1 << (7 - (idx & 7));
74         return !!(tmp & mask);
75 }
76
77 /**
78  * Initialize a getbit_context structure.
79  *
80  * \param gbc The structure to initialize.
81  * \param buffer The bitstream buffer.
82  * \param size The size of the buffer in bytes.
83  *
84  * The bitstream buffer must be 4 bytes larger then the actual read bits
85  * because the bitstream reader might read 32 bits at once and could read over
86  * the end.
87  */
88 static inline void init_get_bits(struct getbit_context *gbc,
89                 const uint8_t *buffer, int size)
90 {
91         gbc->buffer = buffer;
92         gbc->num_bits = size * 8;
93         gbc->index = 0;
94 }
95
96 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
97                 const void *codes, int codes_size);
98 void free_vlc(struct vlc *vlc);
99 int get_vlc(struct getbit_context *gbc, int16_t (*table)[2], int bits);