summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libfaad/bits.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/bits.h')
-rw-r--r--lib/rbcodec/codecs/libfaad/bits.h381
1 files changed, 381 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/bits.h b/lib/rbcodec/codecs/libfaad/bits.h
new file mode 100644
index 0000000000..f9417b8670
--- /dev/null
+++ b/lib/rbcodec/codecs/libfaad/bits.h
@@ -0,0 +1,381 @@
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4**
5** This program is free software; you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation; either version 2 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
22** Commercial non-GPL licensing of this software is possible.
23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24**
25** $Id$
26**/
27
28#ifndef __BITS_H__
29#define __BITS_H__
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#include "analysis.h"
36#ifdef ANALYSIS
37#include <stdio.h>
38#endif
39
40#define BYTE_NUMBIT 8
41#define bit2byte(a) ((a+7)/BYTE_NUMBIT)
42
43typedef struct _bitfile
44{
45 /* bit input */
46 uint32_t bufa;
47 uint32_t bufb;
48 uint32_t bits_left;
49 uint32_t buffer_size; /* size of the buffer in bytes */
50 uint32_t bytes_used;
51 uint8_t no_more_reading;
52 uint8_t error;
53 uint32_t *tail;
54 uint32_t *start;
55 void *buffer;
56} bitfile;
57
58/* rockbox: use asm optimized swap32()
59#define BSWAP(a) \
60 ((a) = ( ((a)&0xff)<<24) | (((a)&0xff00)<<8) | (((a)>>8)&0xff00) | (((a)>>24)&0xff))
61*/
62#define BSWAP(a) swap32(a)
63
64static uint32_t bitmask[] = {
65 0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF,
66 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,
67 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF,
68 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF,
69 0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF
70 /* added bitmask 32, correct?!?!?! */
71 , 0xFFFFFFFF
72};
73
74void faad_initbits(bitfile *ld, const void *buffer, const uint32_t buffer_size);
75void faad_endbits(bitfile *ld);
76/* not used
77void faad_initbits_rev(bitfile *ld, void *buffer,
78 uint32_t bits_in_buffer);
79*/
80uint8_t faad_byte_align(bitfile *ld);
81uint32_t faad_get_processed_bits(bitfile *ld);
82void faad_rewindbits(bitfile *ld);
83uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
84 DEBUGDEC);
85#ifdef DRM
86void *faad_origbitbuffer(bitfile *ld);
87uint32_t faad_origbitbuffer_size(bitfile *ld);
88#endif
89
90static INLINE uint32_t getdword(void *mem)
91{
92#ifndef ARCH_IS_BIG_ENDIAN
93 return BSWAP(*(uint32_t*)mem);
94#else
95 return *(uint32_t*)mem;
96#endif
97}
98
99static INLINE void faad_flushbits_ex(bitfile *ld, uint32_t bits)
100{
101 uint32_t tmp;
102
103 ld->bufa = ld->bufb;
104 if (ld->no_more_reading == 0)
105 {
106 tmp = getdword(ld->tail);
107 ld->tail++;
108 } else {
109 tmp = 0;
110 }
111 ld->bufb = tmp;
112 ld->bits_left += (32 - bits);
113 ld->bytes_used += 4;
114 if (ld->bytes_used == ld->buffer_size)
115 ld->no_more_reading = 1;
116 if (ld->bytes_used > ld->buffer_size)
117 ld->error = 1;
118}
119
120static INLINE uint32_t faad_showbits(bitfile *ld, uint32_t bits)
121{
122 if (bits <= ld->bits_left)
123 {
124 return (ld->bufa >> (ld->bits_left - bits)) & bitmask[bits];
125 }
126
127 bits -= ld->bits_left;
128 return ((ld->bufa & bitmask[ld->bits_left]) << bits) | (ld->bufb >> (32 - bits));
129}
130
131static INLINE void faad_flushbits(bitfile *ld, uint32_t bits)
132{
133 /* do nothing if error */
134 if (ld->error != 0)
135 return;
136
137 if (bits < ld->bits_left)
138 {
139 ld->bits_left -= bits;
140 } else {
141 faad_flushbits_ex(ld, bits);
142 }
143}
144
145/* return next n bits (right adjusted) */
146static uint32_t faad_getbits(bitfile *ld, uint32_t n DEBUGDEC)
147{
148 uint32_t ret;
149
150 if (ld->no_more_reading || n == 0)
151 return 0;
152
153 ret = faad_showbits(ld, n);
154 faad_flushbits(ld, n);
155
156#ifdef ANALYSIS
157 if (print)
158 fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg);
159#endif
160
161 return ret;
162}
163
164static INLINE uint8_t faad_get1bit(bitfile *ld DEBUGDEC)
165{
166 uint8_t r;
167
168 if (ld->bits_left > 0)
169 {
170 ld->bits_left--;
171 r = (uint8_t)((ld->bufa >> ld->bits_left) & 1);
172 return r;
173 }
174
175 /* bits_left == 0 */
176#if 0
177 r = (uint8_t)(ld->bufb >> 31);
178 faad_flushbits_ex(ld, 1);
179#else
180 r = (uint8_t)faad_getbits(ld, 1);
181#endif
182 return r;
183}
184
185/* reversed bitreading routines */
186static INLINE uint32_t faad_showbits_rev(bitfile *ld, uint32_t bits)
187{
188 uint8_t i;
189 uint32_t B = 0;
190
191 if (bits <= ld->bits_left)
192 {
193 for (i = 0; i < bits; i++)
194 {
195 if (ld->bufa & (1 << (i + (32 - ld->bits_left))))
196 B |= (1 << (bits - i - 1));
197 }
198 return B;
199 } else {
200 for (i = 0; i < ld->bits_left; i++)
201 {
202 if (ld->bufa & (1 << (i + (32 - ld->bits_left))))
203 B |= (1 << (bits - i - 1));
204 }
205 for (i = 0; i < bits - ld->bits_left; i++)
206 {
207 if (ld->bufb & (1 << (i + (32-ld->bits_left))))
208 B |= (1 << (bits - ld->bits_left - i - 1));
209 }
210 return B;
211 }
212}
213
214static INLINE void faad_flushbits_rev(bitfile *ld, uint32_t bits)
215{
216 /* do nothing if error */
217 if (ld->error != 0)
218 return;
219
220 if (bits < ld->bits_left)
221 {
222 ld->bits_left -= bits;
223 } else {
224 uint32_t tmp;
225
226 ld->bufa = ld->bufb;
227 tmp = getdword(ld->start);
228 ld->bufb = tmp;
229 ld->start--;
230 ld->bits_left += (32 - bits);
231
232 ld->bytes_used += 4;
233 if (ld->bytes_used == ld->buffer_size)
234 ld->no_more_reading = 1;
235 if (ld->bytes_used > ld->buffer_size)
236 ld->error = 1;
237 }
238}
239
240static INLINE uint32_t faad_getbits_rev(bitfile *ld, uint32_t n
241 DEBUGDEC)
242{
243 uint32_t ret;
244
245 if (ld->no_more_reading)
246 return 0;
247
248 if (n == 0)
249 return 0;
250
251 ret = faad_showbits_rev(ld, n);
252 faad_flushbits_rev(ld, n);
253
254#ifdef ANALYSIS
255 if (print)
256 fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg);
257#endif
258
259 return ret;
260}
261
262#ifdef DRM
263static uint8_t faad_check_CRC(bitfile *ld, uint16_t len)
264{
265 uint8_t CRC;
266 uint16_t r=255; /* Initialize to all ones */
267
268 /* CRC polynome used x^8 + x^4 + x^3 + x^2 +1 */
269#define GPOLY 0435
270
271 faad_rewindbits(ld);
272
273 CRC = (uint8_t) ~faad_getbits(ld, 8
274 DEBUGVAR(1,999,"faad_check_CRC(): CRC")); /* CRC is stored inverted */
275
276 for (; len>0; len--)
277 {
278 r = ( (r << 1) ^ (( ( faad_get1bit(ld
279 DEBUGVAR(1,998,"")) & 1) ^ ((r >> 7) & 1)) * GPOLY )) & 0xFF;
280 }
281
282 if (r != CRC)
283 {
284 return 8;
285 } else {
286 return 0;
287 }
288}
289
290static uint8_t tabFlipbits[256] = {
291 0,128,64,192,32,160,96,224,16,144,80,208,48,176,112,240,
292 8,136,72,200,40,168,104,232,24,152,88,216,56,184,120,248,
293 4,132,68,196,36,164,100,228,20,148,84,212,52,180,116,244,
294 12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,
295 2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,
296 10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,
297 6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,
298 14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,
299 1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241,
300 9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249,
301 5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245,
302 13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253,
303 3,131,67,195,35,163,99,227,19,147,83,211,51,179,115,243,
304 11,139,75,203,43,171,107,235,27,155,91,219,59,187,123,251,
305 7,135,71,199,39,167,103,231,23,151,87,215,55,183,119,247,
306 15,143,79,207,47,175,111,239,31,159,95,223,63,191,127,255
307};
308#endif
309
310#ifdef ERROR_RESILIENCE
311
312/* Modified bit reading functions for HCR */
313
314typedef struct
315{
316 /* bit input */
317 uint32_t bufa;
318 uint32_t bufb;
319 int8_t len;
320} bits_t;
321
322
323static INLINE uint32_t showbits_hcr(bits_t *ld, uint8_t bits)
324{
325 if (bits == 0) return 0;
326 if (ld->len <= 32)
327 {
328 /* huffman_spectral_data_2 needs to read more than may be available, bits maybe
329 > ld->len, deliver 0 than */
330 if (ld->len >= bits)
331 return ((ld->bufa >> (ld->len - bits)) & (0xFFFFFFFF >> (32 - bits)));
332 else
333 return ((ld->bufa << (bits - ld->len)) & (0xFFFFFFFF >> (32 - bits)));
334 } else {
335 if ((ld->len - bits) < 32)
336 {
337 return ( (ld->bufb & (0xFFFFFFFF >> (64 - ld->len))) << (bits - ld->len + 32)) |
338 (ld->bufa >> (ld->len - bits));
339 } else {
340 return ((ld->bufb >> (ld->len - bits - 32)) & (0xFFFFFFFF >> (32 - bits)));
341 }
342 }
343}
344
345/* return 1 if position is outside of buffer, 0 otherwise */
346static INLINE int8_t flushbits_hcr( bits_t *ld, uint8_t bits)
347{
348 ld->len -= bits;
349
350 if (ld->len <0)
351 {
352 ld->len = 0;
353 return 1;
354 } else {
355 return 0;
356 }
357}
358
359static INLINE int8_t getbits_hcr(bits_t *ld, uint8_t n, uint32_t *result)
360{
361 *result = showbits_hcr(ld, n);
362 return flushbits_hcr(ld, n);
363}
364
365static INLINE int8_t get1bit_hcr(bits_t *ld, uint8_t *result)
366{
367 uint32_t res;
368 int8_t ret;
369
370 ret = getbits_hcr(ld, 1, &res);
371 *result = (int8_t)(res & 1);
372 return ret;
373}
374
375#endif
376
377
378#ifdef __cplusplus
379}
380#endif
381#endif