summaryrefslogtreecommitdiff
path: root/apps/codecs/libfaad/bits.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libfaad/bits.h')
-rw-r--r--apps/codecs/libfaad/bits.h382
1 files changed, 382 insertions, 0 deletions
diff --git a/apps/codecs/libfaad/bits.h b/apps/codecs/libfaad/bits.h
new file mode 100644
index 0000000000..56d413e0c8
--- /dev/null
+++ b/apps/codecs/libfaad/bits.h
@@ -0,0 +1,382 @@
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
59#if defined (_WIN32) && !defined(_WIN32_WCE) && !defined(__MINGW32__)
60#define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
61#elif defined(LINUX) || defined(DJGPP) || defined(__MINGW32__)
62#define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) )
63#else
64#define BSWAP(a) \
65 ((a) = ( ((a)&0xff)<<24) | (((a)&0xff00)<<8) | (((a)>>8)&0xff00) | (((a)>>24)&0xff))
66#endif
67
68static uint32_t bitmask[] = {
69 0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF,
70 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF,
71 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF,
72 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF,
73 0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF
74 /* added bitmask 32, correct?!?!?! */
75 , 0xFFFFFFFF
76};
77
78void faad_initbits(bitfile *ld, const void *buffer, const uint32_t buffer_size);
79void faad_endbits(bitfile *ld);
80void faad_initbits_rev(bitfile *ld, void *buffer,
81 uint32_t bits_in_buffer);
82uint8_t faad_byte_align(bitfile *ld);
83uint32_t faad_get_processed_bits(bitfile *ld);
84void faad_flushbits_ex(bitfile *ld, uint32_t bits);
85void faad_rewindbits(bitfile *ld);
86uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits
87 DEBUGDEC);
88#ifdef DRM
89void *faad_origbitbuffer(bitfile *ld);
90uint32_t faad_origbitbuffer_size(bitfile *ld);
91#endif
92
93/* circumvent memory alignment errors on ARM */
94static INLINE uint32_t getdword(void *mem)
95{
96#ifdef ARM
97 uint32_t tmp;
98#ifndef ARCH_IS_BIG_ENDIAN
99 ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[3];
100 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[2];
101 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[1];
102 ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[0];
103#else
104 ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0];
105 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1];
106 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2];
107 ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[3];
108#endif
109
110 return tmp;
111#else
112 uint32_t tmp;
113 tmp = *(uint32_t*)mem;
114#ifndef ARCH_IS_BIG_ENDIAN
115 BSWAP(tmp);
116#endif
117 return tmp;
118#endif
119}
120
121static INLINE uint32_t faad_showbits(bitfile *ld, uint32_t bits)
122{
123 if (bits <= ld->bits_left)
124 {
125 return (ld->bufa >> (ld->bits_left - bits)) & bitmask[bits];
126 }
127
128 bits -= ld->bits_left;
129 return ((ld->bufa & bitmask[ld->bits_left]) << bits) | (ld->bufb >> (32 - bits));
130}
131
132static INLINE void faad_flushbits(bitfile *ld, uint32_t bits)
133{
134 /* do nothing if error */
135 if (ld->error != 0)
136 return;
137
138 if (bits < ld->bits_left)
139 {
140 ld->bits_left -= bits;
141 } else {
142 faad_flushbits_ex(ld, bits);
143 }
144}
145
146/* return next n bits (right adjusted) */
147static INLINE uint32_t faad_getbits(bitfile *ld, uint32_t n DEBUGDEC)
148{
149 uint32_t ret;
150
151 if (ld->no_more_reading || n == 0)
152 return 0;
153
154 ret = faad_showbits(ld, n);
155 faad_flushbits(ld, n);
156
157#ifdef ANALYSIS
158 if (print)
159 fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg);
160#endif
161
162 return ret;
163}
164
165static INLINE uint8_t faad_get1bit(bitfile *ld DEBUGDEC)
166{
167 uint8_t r;
168
169 if (ld->bits_left > 0)
170 {
171 ld->bits_left--;
172 r = (uint8_t)((ld->bufa >> ld->bits_left) & 1);
173 return r;
174 }
175
176 /* bits_left == 0 */
177#if 0
178 r = (uint8_t)(ld->bufb >> 31);
179 faad_flushbits_ex(ld, 1);
180#else
181 r = (uint8_t)faad_getbits(ld, 1);
182#endif
183 return r;
184}
185
186/* reversed bitreading routines */
187static INLINE uint32_t faad_showbits_rev(bitfile *ld, uint32_t bits)
188{
189 uint8_t i;
190 uint32_t B = 0;
191
192 if (bits <= ld->bits_left)
193 {
194 for (i = 0; i < bits; i++)
195 {
196 if (ld->bufa & (1 << (i + (32 - ld->bits_left))))
197 B |= (1 << (bits - i - 1));
198 }
199 return B;
200 } else {
201 for (i = 0; i < ld->bits_left; i++)
202 {
203 if (ld->bufa & (1 << (i + (32 - ld->bits_left))))
204 B |= (1 << (bits - i - 1));
205 }
206 for (i = 0; i < bits - ld->bits_left; i++)
207 {
208 if (ld->bufb & (1 << (i + (32-ld->bits_left))))
209 B |= (1 << (bits - ld->bits_left - i - 1));
210 }
211 return B;
212 }
213}
214
215static INLINE void faad_flushbits_rev(bitfile *ld, uint32_t bits)
216{
217 /* do nothing if error */
218 if (ld->error != 0)
219 return;
220
221 if (bits < ld->bits_left)
222 {
223 ld->bits_left -= bits;
224 } else {
225 uint32_t tmp;
226
227 ld->bufa = ld->bufb;
228 tmp = getdword(ld->start);
229 ld->bufb = tmp;
230 ld->start--;
231 ld->bits_left += (32 - bits);
232
233 ld->bytes_used += 4;
234 if (ld->bytes_used == ld->buffer_size)
235 ld->no_more_reading = 1;
236 if (ld->bytes_used > ld->buffer_size)
237 ld->error = 1;
238 }
239}
240
241static INLINE uint32_t faad_getbits_rev(bitfile *ld, uint32_t n
242 DEBUGDEC)
243{
244 uint32_t ret;
245
246 if (ld->no_more_reading)
247 return 0;
248
249 if (n == 0)
250 return 0;
251
252 ret = faad_showbits_rev(ld, n);
253 faad_flushbits_rev(ld, n);
254
255#ifdef ANALYSIS
256 if (print)
257 fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg);
258#endif
259
260 return ret;
261}
262
263#ifdef DRM
264static uint8_t faad_check_CRC(bitfile *ld, uint16_t len)
265{
266 uint8_t CRC;
267 uint16_t r=255; /* Initialize to all ones */
268
269 /* CRC polynome used x^8 + x^4 + x^3 + x^2 +1 */
270#define GPOLY 0435
271
272 faad_rewindbits(ld);
273
274 CRC = (uint8_t) ~faad_getbits(ld, 8
275 DEBUGVAR(1,999,"faad_check_CRC(): CRC")); /* CRC is stored inverted */
276
277 for (; len>0; len--)
278 {
279 r = ( (r << 1) ^ (( ( faad_get1bit(ld
280 DEBUGVAR(1,998,"")) & 1) ^ ((r >> 7) & 1)) * GPOLY )) & 0xFF;
281 }
282
283 if (r != CRC)
284 {
285 return 8;
286 } else {
287 return 0;
288 }
289}
290
291static uint8_t tabFlipbits[256] = {
292 0,128,64,192,32,160,96,224,16,144,80,208,48,176,112,240,
293 8,136,72,200,40,168,104,232,24,152,88,216,56,184,120,248,
294 4,132,68,196,36,164,100,228,20,148,84,212,52,180,116,244,
295 12,140,76,204,44,172,108,236,28,156,92,220,60,188,124,252,
296 2,130,66,194,34,162,98,226,18,146,82,210,50,178,114,242,
297 10,138,74,202,42,170,106,234,26,154,90,218,58,186,122,250,
298 6,134,70,198,38,166,102,230,22,150,86,214,54,182,118,246,
299 14,142,78,206,46,174,110,238,30,158,94,222,62,190,126,254,
300 1,129,65,193,33,161,97,225,17,145,81,209,49,177,113,241,
301 9,137,73,201,41,169,105,233,25,153,89,217,57,185,121,249,
302 5,133,69,197,37,165,101,229,21,149,85,213,53,181,117,245,
303 13,141,77,205,45,173,109,237,29,157,93,221,61,189,125,253,
304 3,131,67,195,35,163,99,227,19,147,83,211,51,179,115,243,
305 11,139,75,203,43,171,107,235,27,155,91,219,59,187,123,251,
306 7,135,71,199,39,167,103,231,23,151,87,215,55,183,119,247,
307 15,143,79,207,47,175,111,239,31,159,95,223,63,191,127,255
308};
309#endif
310
311#ifdef ERROR_RESILIENCE
312
313/* Modified bit reading functions for HCR */
314
315typedef struct
316{
317 /* bit input */
318 uint32_t bufa;
319 uint32_t bufb;
320 int8_t len;
321} bits_t;
322
323
324static INLINE uint32_t showbits_hcr(bits_t *ld, uint8_t bits)
325{
326 if (bits == 0) return 0;
327 if (ld->len <= 32)
328 {
329 /* huffman_spectral_data_2 needs to read more than may be available, bits maybe
330 > ld->len, deliver 0 than */
331 if (ld->len >= bits)
332 return ((ld->bufa >> (ld->len - bits)) & (0xFFFFFFFF >> (32 - bits)));
333 else
334 return ((ld->bufa << (bits - ld->len)) & (0xFFFFFFFF >> (32 - bits)));
335 } else {
336 if ((ld->len - bits) < 32)
337 {
338 return ( (ld->bufb & (0xFFFFFFFF >> (64 - ld->len))) << (bits - ld->len + 32)) |
339 (ld->bufa >> (ld->len - bits));
340 } else {
341 return ((ld->bufb >> (ld->len - bits - 32)) & (0xFFFFFFFF >> (32 - bits)));
342 }
343 }
344}
345
346/* return 1 if position is outside of buffer, 0 otherwise */
347static INLINE int8_t flushbits_hcr( bits_t *ld, uint8_t bits)
348{
349 ld->len -= bits;
350
351 if (ld->len <0)
352 {
353 ld->len = 0;
354 return 1;
355 } else {
356 return 0;
357 }
358}
359
360static INLINE int8_t getbits_hcr(bits_t *ld, uint8_t n, uint32_t *result)
361{
362 *result = showbits_hcr(ld, n);
363 return flushbits_hcr(ld, n);
364}
365
366static INLINE int8_t get1bit_hcr(bits_t *ld, uint8_t *result)
367{
368 uint32_t res;
369 int8_t ret;
370
371 ret = getbits_hcr(ld, 1, &res);
372 *result = (int8_t)(res & 1);
373 return ret;
374}
375
376#endif
377
378
379#ifdef __cplusplus
380}
381#endif
382#endif