summaryrefslogtreecommitdiff
path: root/apps/codecs/libffmpegFLAC/bitstream.h
diff options
context:
space:
mode:
authorNils Wallménius <nils@rockbox.org>2010-07-15 16:19:17 +0000
committerNils Wallménius <nils@rockbox.org>2010-07-15 16:19:17 +0000
commita87c61854ef614b258ca7d4d0b40db017884e63e (patch)
tree4f0129350a8a2d25ee5e5d218aa787ae2dbbeca3 /apps/codecs/libffmpegFLAC/bitstream.h
parent328f2f9c285dd9ccec4ddabe4d64a508b0e498fa (diff)
downloadrockbox-a87c61854ef614b258ca7d4d0b40db017884e63e.tar.gz
rockbox-a87c61854ef614b258ca7d4d0b40db017884e63e.zip
Sync codeclib bitstream code with upstream ffmpeg code. Build ffmpeg_bitstream.c as a part of the codec lib. Use this codeclib implementation in libffmpegFLAC. Implement adapted version of the unaligned longword reading optimization for coldfire from the libwma version of this code. Speeds up cook decoding by 2-3% on h300 and flac by 25% on h300, also speeds up flac decoding by 2% on c200 (decoding speed of cook on c200 is unchanged).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27430 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/codecs/libffmpegFLAC/bitstream.h')
-rw-r--r--apps/codecs/libffmpegFLAC/bitstream.h278
1 files changed, 1 insertions, 277 deletions
diff --git a/apps/codecs/libffmpegFLAC/bitstream.h b/apps/codecs/libffmpegFLAC/bitstream.h
index 9a8c548d95..1333b9f4b8 100644
--- a/apps/codecs/libffmpegFLAC/bitstream.h
+++ b/apps/codecs/libffmpegFLAC/bitstream.h
@@ -7,6 +7,7 @@
7#define BITSTREAM_H 7#define BITSTREAM_H
8 8
9#include <inttypes.h> 9#include <inttypes.h>
10#include "ffmpeg_get_bits.h"
10 11
11#ifndef BUILD_STANDALONE 12#ifndef BUILD_STANDALONE
12 #include <config.h> 13 #include <config.h>
@@ -61,281 +62,4 @@
61 } 62 }
62#endif 63#endif
63 64
64/* FLAC files are big-endian */
65#define ALT_BITSTREAM_READER_BE
66
67#define NEG_SSR32(a,s) (((int32_t)(a))>>(32-(s)))
68#define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
69
70/* bit input */
71/* buffer, buffer_end and size_in_bits must be present and used by every reader */
72typedef struct GetBitContext {
73 const uint8_t *buffer, *buffer_end;
74 int index;
75 int size_in_bits;
76} GetBitContext;
77
78#define VLC_TYPE int16_t
79
80typedef struct VLC {
81 int bits;
82 VLC_TYPE (*table)[2]; ///< code, bits
83 int table_size, table_allocated;
84} VLC;
85
86typedef struct RL_VLC_ELEM {
87 int16_t level;
88 int8_t len;
89 uint8_t run;
90} RL_VLC_ELEM;
91
92#if defined(ARCH_SPARC) || defined(ARCH_ARMV4L)
93#define UNALIGNED_STORES_ARE_BAD
94#endif
95
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__
101static inline uint32_t unaligned32(const void *v) {
102 struct Unaligned {
103 uint32_t i;
104 } __attribute__((packed));
105
106 return ((const struct Unaligned *) v)->i;
107}
108# elif defined(__DECC)
109static inline uint32_t unaligned32(const void *v) {
110 return *(const __unaligned uint32_t *) v;
111}
112# else
113static inline uint32_t unaligned32(const void *v) {
114 return *(const uint32_t *) v;
115}
116# endif
117#endif //!ARCH_X86
118
119
120/* Bitstream reader API docs:
121name
122 abritary name which is used as prefix for the internal variables
123
124gb
125 getbitcontext
126
127OPEN_READER(name, gb)
128 loads gb into local variables
129
130CLOSE_READER(name, gb)
131 stores local vars in gb
132
133UPDATE_CACHE(name, gb)
134 refills the internal cache from the bitstream
135 after this call at least MIN_CACHE_BITS will be available,
136
137GET_CACHE(name, gb)
138 will output the contents of the internal cache, next bit is MSB of 32 or 64 bit (FIXME 64bit)
139
140SHOW_UBITS(name, gb, num)
141 will return the next num bits
142
143SHOW_SBITS(name, gb, num)
144 will return the next num bits and do sign extension
145
146SKIP_BITS(name, gb, num)
147 will skip over the next num bits
148 note, this is equivalent to SKIP_CACHE; SKIP_COUNTER
149
150SKIP_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)
152
153SKIP_COUNTER(name, gb, num)
154 will increment the internal bit counter (see SKIP_CACHE & SKIP_BITS)
155
156LAST_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
158
159LAST_SKIP_BITS(name, gb, num)
160 is equivalent to SKIP_LAST_CACHE; SKIP_COUNTER
161
162for examples see get_bits, show_bits, skip_bits, get_vlc
163*/
164
165static inline int unaligned32_be(const void *v)
166{
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
173}
174
175static inline int unaligned32_le(const void *v)
176{
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
183}
184
185# define MIN_CACHE_BITS 25
186
187# define OPEN_READER(name, gb)\
188 int name##_index= (gb)->index;\
189 int name##_cache= 0;\
190
191# define CLOSE_READER(name, gb)\
192 (gb)->index= name##_index;\
193
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);\
197
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);\
203
204# define SKIP_CACHE(name, gb, num)\
205 name##_cache <<= (num);
206# endif
207
208// FIXME name?
209# define SKIP_COUNTER(name, gb, num)\
210 name##_index += (num);\
211
212# define SKIP_BITS(name, gb, num)\
213 {\
214 SKIP_CACHE(name, gb, num)\
215 SKIP_COUNTER(name, gb, num)\
216 }\
217
218# define LAST_SKIP_BITS(name, gb, num) SKIP_COUNTER(name, gb, num)
219# define LAST_SKIP_CACHE(name, gb, num) ;
220
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
228
229# define SHOW_SBITS(name, gb, num)\
230 NEG_SSR32(name##_cache, num)
231
232# define GET_CACHE(name, gb)\
233 ((uint32_t)name##_cache)
234
235static inline int get_bits_count(GetBitContext *s){
236 return s->index;
237}
238
239static 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;
247}
248
249/**
250 * reads 0-17 bits.
251 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
252 */
253static 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;
261}
262
263unsigned int get_bits_long(GetBitContext *s, int n) ICODE_ATTR_FLAC;
264
265/**
266 * shows 0-17 bits.
267 * Note, the alt bitstream reader can read up to 25 bits, but the libmpeg2 reader can't
268 */
269static 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;
276}
277
278unsigned int show_bits_long(GetBitContext *s, int n) ICODE_ATTR_FLAC;
279
280static 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)
286}
287
288static 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;
300
301 return result;
302}
303
304static inline unsigned int show_bits1(GetBitContext *s){
305 return show_bits(s, 1);
306}
307
308static inline void skip_bits1(GetBitContext *s){
309 skip_bits(s, 1);
310}
311
312/**
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
317 */
318static inline void init_get_bits(GetBitContext *s,
319 const uint8_t *buffer, int bit_size)
320{
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;
325 }
326
327 s->buffer= buffer;
328 s->size_in_bits= bit_size;
329 s->buffer_end= buffer + buffer_size;
330 s->index=0;
331 {
332 OPEN_READER(re, s)
333 UPDATE_CACHE(re, s)
334 UPDATE_CACHE(re, s)
335 CLOSE_READER(re, s)
336 }
337}
338
339void align_get_bits(GetBitContext *s) ICODE_ATTR_FLAC;
340
341#endif /* BITSTREAM_H */ 65#endif /* BITSTREAM_H */