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