annotate libfaad2/bits.h @ 13435:8ec040ae2f30

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