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