3 * bitstream api header.
11 #ifndef BUILD_STANDALONE
21 #ifndef ICODE_ATTR_FLAC
22 #define ICODE_ATTR_FLAC ICODE_ATTR
25 #ifndef IBSS_ATTR_FLAC_DECODED0
26 #define IBSS_ATTR_FLAC_DECODED0 IBSS_ATTR
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)
35 #define letoh32(x) (x)
36 #define betoh32(x) swap32(x)
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
;
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)))
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
;
78 #define VLC_TYPE int16_t
82 VLC_TYPE (*table
)[2]; ///< code, bits
83 int table_size
, table_allocated
;
86 typedef struct RL_VLC_ELEM
{
92 #if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
93 #define UNALIGNED_STORES_ARE_BAD
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))
101 static inline uint32_t unaligned32(const void *v
) {
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
;
113 static inline uint32_t unaligned32(const void *v
) {
114 return *(const uint32_t *) v
;
120 /* Bitstream reader API docs:
122 abritary name which is used as prefix for the internal variables
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,
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
)
169 return (((p
[0]<<8) | p
[1])<<16) | (p
[2]<<8) | (p
[3]);
171 return betoh32( unaligned32(v
)); //original
175 static inline int unaligned32_le(const void *v
)
179 return (((p
[3]<<8) | p
[2])<<16) | (p
[1]<<8) | (p
[0]);
181 return letoh32( unaligned32(v
)); //original
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);
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);
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)))
225 # define SHOW_UBITS(name, gb, num)\
226 NEG_USR32(name##_cache, num)
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
){
239 static inline int get_sbits(GetBitContext
*s
, int n
){
243 tmp
= SHOW_SBITS(re
, s
, n
);
244 LAST_SKIP_BITS(re
, s
, n
)
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
){
257 tmp
= SHOW_UBITS(re
, s
, n
);
258 LAST_SKIP_BITS(re
, s
, n
)
263 unsigned int get_bits_long(GetBitContext
*s
, int n
) ICODE_ATTR_FLAC
;
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
){
273 tmp
= SHOW_UBITS(re
, s
, n
);
274 // CLOSE_READER(re, s)
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 :))
284 LAST_SKIP_BITS(re
, s
, n
)
288 static inline unsigned int get_bits1(GetBitContext
*s
){
290 uint8_t result
= s
->buffer
[ index
>>3 ];
291 #ifdef ALT_BITSTREAM_READER_LE
292 result
>>= (index
&0x07);
295 result
<<= (index
&0x07);
304 static inline unsigned int show_bits1(GetBitContext
*s
){
305 return show_bits(s
, 1);
308 static inline void skip_bits1(GetBitContext
*s
){
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;
328 s
->size_in_bits
= bit_size
;
329 s
->buffer_end
= buffer
+ buffer_size
;
339 void align_get_bits(GetBitContext
*s
) ICODE_ATTR_FLAC
;
341 #endif /* BITSTREAM_H */