07c65389081d9b4b979c5c556dc5e1a9d4ce75f1
[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 #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])
16
17 #define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
18
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;
25         /** Bit counter. */
26         int index;
27 };
28
29 #define VLC_TYPE int16_t
30
31 struct vlc {
32         int bits;
33         VLC_TYPE(*table)[2];    ///< code, bits
34         int table_size, table_allocated;
35 };
36
37 /** Load \a gb into local variables. */
38 #define OPEN_READER(name, gb)\
39         int name##_index= (gb)->index;\
40         int name##_cache= 0;\
41
42 /** Store local vars in gb. */
43 #define CLOSE_READER(name, gb)\
44         (gb)->index= name##_index;\
45
46 /**
47  * Refill the internal cache from the bitstream.
48  */
49 #define UPDATE_CACHE(name, gb)\
50         name##_cache= AV_RB32( ((const uint8_t *)(gb)->buffer) \
51                 + (name##_index >> 3) ) << (name##_index & 0x07);\
52
53 /**
54  * Remove the next num bits from the cache.
55  *
56  * SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER).
57  */
58 #define SKIP_CACHE(name, gb, num)\
59         name##_cache <<= (num);
60
61 /**
62  * Increment the internal bit counter.
63  *
64  * \sa SKIP_CACHE, SKIP_BITS.
65  */
66 #define SKIP_COUNTER(name, gb, num)\
67         name##_index += (num);\
68
69 /**
70  * Skip over the next num bits.
71  *
72  * This is equivalent to SKIP_CACHE; SKIP_COUNTER.
73  */
74 #define SKIP_BITS(name, gb, num)\
75         {\
76                 SKIP_CACHE(name, gb, num)\
77                 SKIP_COUNTER(name, gb, num)\
78         }\
79
80 /** This is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER. */
81 #define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
82
83 /** Return the next num bits. */
84 #define SHOW_UBITS(name, gb, num)\
85         NEG_USR32(name##_cache, num)
86
87 static inline int get_bits_count(struct getbit_context *s)
88 {
89         return s->index;
90 }
91
92 /**
93  * Read 1-17 bits.
94  */
95 static inline unsigned int get_bits(struct getbit_context *s, int n)
96 {
97         register int tmp;
98         OPEN_READER(re, s)
99         UPDATE_CACHE(re, s)
100         tmp = SHOW_UBITS(re, s, n);
101         LAST_SKIP_BITS(re, s, n)
102         CLOSE_READER(re, s)
103         return tmp;
104 }
105
106 /**
107  * Show 1-17 bits.
108  */
109 static inline unsigned int show_bits(struct getbit_context *s, int n)
110 {
111         register int tmp;
112         OPEN_READER(re, s)
113         UPDATE_CACHE(re, s)
114         tmp = SHOW_UBITS(re, s, n);
115         return tmp;
116 }
117
118 static inline void skip_bits(struct getbit_context *s, int n)
119 {
120         /* gcc seems to optimize this to s->index+=n for the ALT_READER :)) */
121         OPEN_READER(re, s)
122         UPDATE_CACHE(re, s)
123         LAST_SKIP_BITS(re, s, n)
124         CLOSE_READER(re, s)
125 }
126
127 static inline unsigned int get_bits1(struct getbit_context *s)
128 {
129         int idx = s->index;
130         uint8_t result = s->buffer[idx >> 3];
131
132         result <<= (idx & 0x07);
133         result >>= 8 - 1;
134         idx++;
135         s->index = idx;
136         return result;
137 }
138
139 /**
140  * Initialize a getbit_context structure.
141  *
142  * \param buffer The bitstream buffer. It must be FF_INPUT_BUFFER_PADDING_SIZE
143  * bytes larger then the actual read bits * because some optimized bitstream
144  * readers read 32 or 64 bit at once and could read over the end.
145  *
146  * \param bit_size The size of the buffer in bits.
147  */
148 static inline void init_get_bits(struct getbit_context *s,
149                 const uint8_t *buffer, int bit_size)
150 {
151         int buffer_size = (bit_size + 7) >> 3;
152         if (buffer_size < 0 || bit_size < 0) {
153                 buffer_size = bit_size = 0;
154                 buffer = NULL;
155         }
156         s->buffer = buffer;
157         s->buffer_end = buffer + buffer_size;
158         s->index = 0;
159 }
160
161 void init_vlc(struct vlc *vlc, int nb_bits, int nb_codes, const void *bits,
162                 const void *codes, int codes_size);
163
164 void free_vlc(struct vlc *vlc);
165
166 /**
167  *
168  * if the vlc code is invalid and max_depth=1 than no bits will be removed
169  * if the vlc code is invalid and max_depth>1 than the number of bits removed
170  * is undefined
171  */
172 #define GET_VLC(code, name, gb, table, bits, max_depth)\
173 {\
174     int n, index, nb_bits;\
175 \
176     index= SHOW_UBITS(name, gb, bits);\
177     code = table[index][0];\
178     n    = table[index][1];\
179 \
180     if(max_depth > 1 && n < 0){\
181         LAST_SKIP_BITS(name, gb, bits)\
182         UPDATE_CACHE(name, gb)\
183 \
184         nb_bits = -n;\
185 \
186         index= SHOW_UBITS(name, gb, nb_bits) + code;\
187         code = table[index][0];\
188         n    = table[index][1];\
189         if(max_depth > 2 && n < 0){\
190             LAST_SKIP_BITS(name, gb, nb_bits)\
191             UPDATE_CACHE(name, gb)\
192 \
193             nb_bits = -n;\
194 \
195             index= SHOW_UBITS(name, gb, nb_bits) + code;\
196             code = table[index][0];\
197             n    = table[index][1];\
198         }\
199     }\
200     SKIP_BITS(name, gb, n)\
201 }