FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libffmpegFLAC / bitstream.h
blob19f2b6e01c582270f93d74ee26c6efbb0a2965eb
1 /**
2 * @file bitstream.h
3 * bitstream api header.
4 */
6 #ifndef BITSTREAM_H
7 #define BITSTREAM_H
9 #include <inttypes.h>
11 #ifndef BUILD_STANDALONE
12 #include <config.h>
13 #include <system.h>
14 #else
15 #include <stdio.h>
16 #define IBSS_ATTR
17 #define ICONST_ATTR
18 #define ICODE_ATTR
19 #endif
21 #ifndef ICODE_ATTR_FLAC
22 #define ICODE_ATTR_FLAC ICODE_ATTR
23 #endif
25 #ifndef IBSS_ATTR_FLAC_DECODED0
26 #define IBSS_ATTR_FLAC_DECODED0 IBSS_ATTR
27 #endif
29 /* Endian conversion routines for standalone compilation */
30 #ifdef BUILD_STANDALONE
31 #ifdef BUILD_BIGENDIAN
32 #define betoh32(x) (x)
33 #define letoh32(x) swap32(x)
34 #else
35 #define letoh32(x) (x)
36 #define betoh32(x) swap32(x)
37 #endif
39 /* Taken from rockbox/firmware/export/system.h */
41 static inline unsigned short swap16(unsigned short value)
43 result[15..8] = value[ 7..0];
44 result[ 7..0] = value[15..8];
47 return (value >> 8) | (value << 8);
50 static inline unsigned long swap32(unsigned long value)
52 result[31..24] = value[ 7.. 0];
53 result[23..16] = value[15.. 8];
54 result[15.. 8] = value[23..16];
55 result[ 7.. 0] = value[31..24];
58 unsigned long hi = swap16(value >> 16);
59 unsigned long lo = swap16(value & 0xffff);
60 return (lo << 16) | hi;
62 #endif
64 /* FLAC files are big-endian */
65 #define ALT_BITSTREAM_READER_BE
67 #define NEG_SSR32(a,s) (((int32_t)(a))>>(32-(s)))
68 #define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
70 /* bit input */
71 /* buffer, buffer_end and size_in_bits must be present and used by every reader */
72 typedef struct GetBitContext {
73 const uint8_t *buffer, *buffer_end;
74 int index;
75 int size_in_bits;
76 } GetBitContext;
78 #define VLC_TYPE int16_t
80 typedef struct VLC {
81 int bits;
82 VLC_TYPE (*table)[2]; ///< code, bits
83 int table_size, table_allocated;
84 } VLC;
86 typedef struct RL_VLC_ELEM {
87 int16_t level;
88 int8_t len;
89 uint8_t run;
90 } RL_VLC_ELEM;
92 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
93 #define UNALIGNED_STORES_ARE_BAD
94 #endif
96 /* used to avoid missaligned exceptions on some archs (alpha, ...) */
97 #if defined(ARCH_X86) || defined(ARCH_X86_64)
98 # define unaligned32(a) (*(const uint32_t*)(a))
99 #else
100 # ifdef __GNUC__
101 static inline uint32_t unaligned32(const void *v) {
102 struct Unaligned {
103 uint32_t i;
104 } __attribute__((packed));
106 return ((const struct Unaligned *) v)->i;
108 # elif defined(__DECC)
109 static inline uint32_t unaligned32(const void *v) {
110 return *(const __unaligned uint32_t *) v;
112 # else
113 static inline uint32_t unaligned32(const void *v) {
114 return *(const uint32_t *) v;
116 # endif
117 #endif //!ARCH_X86
120 /* Bitstream reader API docs:
121 name
122 abritary name which is used as prefix for the internal variables
125 getbitcontext
127 OPEN_READER(name, gb)
128 loads gb into local variables
130 CLOSE_READER(name, gb)
131 stores local vars in gb
133 UPDATE_CACHE(name, gb)
134 refills the internal cache from the bitstream
135 after this call at least MIN_CACHE_BITS will be available,
137 GET_CACHE(name, gb)
138 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
140 SHOW_UBITS(name, gb, num)
141 will return the next num bits
143 SHOW_SBITS(name, gb, num)
144 will return the next num bits and do sign extension
146 SKIP_BITS(name, gb, num)
147 will skip over the next num bits
148 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
150 SKIP_CACHE(name, gb, num)
151 will remove the next num bits from the cache (note SKIP_COUNTER MUST be called before UPDATE_CACHE / CLOSE_READER)
153 SKIP_COUNTER(name, gb, num)
154 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
156 LAST_SKIP_CACHE(name, gb, num)
157 will remove the next num bits from the cache if it is needed for UPDATE_CACHE otherwise it will do nothing
159 LAST_SKIP_BITS(name, gb, num)
160 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
162 for examples see get_bits, show_bits, skip_bits, get_vlc
165 static inline int unaligned32_be(const void *v)
167 #ifdef CONFIG_ALIGN
168 const uint8_t *p=v;
169 return (((p[0]<<8) | p[1])<<16) | (p[2]<<8) | (p[3]);
170 #else
171 return betoh32( unaligned32(v)); //original
172 #endif
175 static inline int unaligned32_le(const void *v)
177 #ifdef CONFIG_ALIGN
178 const uint8_t *p=v;
179 return (((p[3]<<8) | p[2])<<16) | (p[1]<<8) | (p[0]);
180 #else
181 return letoh32( unaligned32(v)); //original
182 #endif
185 # define MIN_CACHE_BITS 25
187 # define OPEN_READER(name, gb)\
188 int name##_index= (gb)->index;\
189 int name##_cache= 0;\
191 # define CLOSE_READER(name, gb)\
192 (gb)->index= name##_index;\
194 # ifdef ALT_BITSTREAM_READER_LE
195 # define UPDATE_CACHE(name, gb)\
196 name##_cache= unaligned32_le( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) >> (name##_index&0x07);\
198 # define SKIP_CACHE(name, gb, num)\
199 name##_cache >>= (num);
200 # else
201 # define UPDATE_CACHE(name, gb)\
202 name##_cache= unaligned32_be( ((const uint8_t *)(gb)->buffer)+(name##_index>>3) ) << (name##_index&0x07);\
204 # define SKIP_CACHE(name, gb, num)\
205 name##_cache <<= (num);
206 # endif
208 // FIXME name?
209 # define SKIP_COUNTER(name, gb, num)\
210 name##_index += (num);\
212 # define SKIP_BITS(name, gb, num)\
214 SKIP_CACHE(name, gb, num)\
215 SKIP_COUNTER(name, gb, num)\
218 # define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
219 # define LAST_SKIP_CACHE(name, gb, num) ;
221 # ifdef ALT_BITSTREAM_READER_LE
222 # define SHOW_UBITS(name, gb, num)\
223 ((name##_cache) & (NEG_USR32(0xffffffff,num)))
224 # else
225 # define SHOW_UBITS(name, gb, num)\
226 NEG_USR32(name##_cache, num)
227 # endif
229 # define SHOW_SBITS(name, gb, num)\
230 NEG_SSR32(name##_cache, num)
232 # define GET_CACHE(name, gb)\
233 ((uint32_t)name##_cache)
235 static inline int get_bits_count(GetBitContext *s){
236 return s->index;
239 static inline int get_sbits(GetBitContext *s, int n){
240 register int tmp;
241 OPEN_READER(re, s)
242 UPDATE_CACHE(re, s)
243 tmp= SHOW_SBITS(re, s, n);
244 LAST_SKIP_BITS(re, s, n)
245 CLOSE_READER(re, s)
246 return tmp;
250 * reads 0-17 bits.
251 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
253 static inline unsigned int get_bits(GetBitContext *s, int n){
254 register int tmp;
255 OPEN_READER(re, s)
256 UPDATE_CACHE(re, s)
257 tmp= SHOW_UBITS(re, s, n);
258 LAST_SKIP_BITS(re, s, n)
259 CLOSE_READER(re, s)
260 return tmp;
263 unsigned int get_bits_long(GetBitContext *s, int n) ICODE_ATTR_FLAC;
266 * shows 0-17 bits.
267 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
269 static inline unsigned int show_bits(GetBitContext *s, int n){
270 register int tmp;
271 OPEN_READER(re, s)
272 UPDATE_CACHE(re, s)
273 tmp= SHOW_UBITS(re, s, n);
274 // CLOSE_READER(re, s)
275 return tmp;
278 unsigned int show_bits_long(GetBitContext *s, int n) ICODE_ATTR_FLAC;
280 static inline void skip_bits(GetBitContext *s, int n){
281 //Note gcc seems to optimize this to s->index+=n for the ALT_READER :))
282 OPEN_READER(re, s)
283 UPDATE_CACHE(re, s)
284 LAST_SKIP_BITS(re, s, n)
285 CLOSE_READER(re, s)
288 static inline unsigned int get_bits1(GetBitContext *s){
289 int index= s->index;
290 uint8_t result= s->buffer[ index>>3 ];
291 #ifdef ALT_BITSTREAM_READER_LE
292 result>>= (index&0x07);
293 result&= 1;
294 #else
295 result<<= (index&0x07);
296 result>>= 8 - 1;
297 #endif
298 index++;
299 s->index= index;
301 return result;
304 static inline unsigned int show_bits1(GetBitContext *s){
305 return show_bits(s, 1);
308 static inline void skip_bits1(GetBitContext *s){
309 skip_bits(s, 1);
313 * init GetBitContext.
314 * @param buffer bitstream buffer, must be FF_INPUT_BUFFER_PADDING_SIZE bytes larger then the actual read bits
315 * because some optimized bitstream readers read 32 or 64 bit at once and could read over the end
316 * @param bit_size the size of the buffer in bits
318 static inline void init_get_bits(GetBitContext *s,
319 const uint8_t *buffer, int bit_size)
321 int buffer_size= (bit_size+7)>>3;
322 if(buffer_size < 0 || bit_size < 0) {
323 buffer_size = bit_size = 0;
324 buffer = 0;
327 s->buffer= buffer;
328 s->size_in_bits= bit_size;
329 s->buffer_end= buffer + buffer_size;
330 s->index=0;
332 OPEN_READER(re, s)
333 UPDATE_CACHE(re, s)
334 UPDATE_CACHE(re, s)
335 CLOSE_READER(re, s)
339 void align_get_bits(GetBitContext *s) ICODE_ATTR_FLAC;
341 #endif /* BITSTREAM_H */