Mercurial > mplayer.hg
annotate libfaad2/bits.h @ 10911:a2b9b13af73f
Update readme's. Remove incorrect bug :)
author | lumag |
---|---|
date | Sun, 21 Sep 2003 13:01:25 +0000 |
parents | 125a35fc47a5 |
children | 3185f64f6350 |
rev | line source |
---|---|
10725 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
3 ** Copyright (C) 2003 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 | |
32 extern "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 | |
43 typedef 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 | |
10805
125a35fc47a5
fixed win32 compile problems and broken bigendian support
alex
parents:
10725
diff
changeset
|
59 #if defined (_WIN32) && !defined(_WIN32_WCE) && !defined(__GNUC__) |
10725 | 60 #define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax |
10805
125a35fc47a5
fixed win32 compile problems and broken bigendian support
alex
parents:
10725
diff
changeset
|
61 #elif defined(ARCH_X86) && (defined(DJGPP) || defined(__GNUC__)) |
10725 | 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 | |
68 static 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 }; | |
75 | |
76 void faad_initbits(bitfile *ld, void *buffer, uint32_t buffer_size); | |
77 void faad_endbits(bitfile *ld); | |
78 void faad_initbits_rev(bitfile *ld, void *buffer, | |
79 uint32_t bits_in_buffer); | |
80 uint8_t faad_byte_align(bitfile *ld); | |
81 uint32_t faad_get_processed_bits(bitfile *ld); | |
82 void faad_rewindbits(bitfile *ld); | |
83 uint8_t *faad_getbitbuffer(bitfile *ld, uint32_t bits | |
84 DEBUGDEC); | |
85 | |
86 /* circumvent memory alignment errors on ARM */ | |
87 static INLINE uint32_t getdword(void *mem) | |
88 { | |
89 #ifdef ARM | |
90 uint32_t tmp; | |
91 ((uint8_t*)&tmp)[0] = ((uint8_t*)mem)[0]; | |
92 ((uint8_t*)&tmp)[1] = ((uint8_t*)mem)[1]; | |
93 ((uint8_t*)&tmp)[2] = ((uint8_t*)mem)[2]; | |
94 ((uint8_t*)&tmp)[3] = ((uint8_t*)mem)[3]; | |
95 | |
96 return tmp; | |
97 #else | |
98 return *(uint32_t*)mem; | |
99 #endif | |
100 } | |
101 | |
102 static INLINE uint32_t faad_showbits(bitfile *ld, uint32_t bits) | |
103 { | |
104 if (bits <= ld->bits_left) | |
105 { | |
106 return (ld->bufa >> (ld->bits_left - bits)) & bitmask[bits]; | |
107 } else { | |
108 bits -= ld->bits_left; | |
109 return ((ld->bufa & bitmask[ld->bits_left]) << bits) | (ld->bufb >> (32 - bits)); | |
110 } | |
111 } | |
112 | |
113 static INLINE void faad_flushbits(bitfile *ld, uint32_t bits) | |
114 { | |
115 /* do nothing if error */ | |
116 if (ld->error != 0) | |
117 return; | |
118 | |
119 if (bits < ld->bits_left) | |
120 { | |
121 ld->bits_left -= bits; | |
122 } else { | |
123 uint32_t tmp; | |
124 | |
125 ld->bufa = ld->bufb; | |
126 tmp = getdword(ld->tail); | |
127 ld->tail++; | |
128 #ifndef ARCH_IS_BIG_ENDIAN | |
129 BSWAP(tmp); | |
130 #endif | |
131 ld->bufb = tmp; | |
132 ld->bits_left += (32 - bits); | |
133 ld->bytes_used += 4; | |
134 if (ld->bytes_used == ld->buffer_size) | |
135 ld->no_more_reading = 1; | |
136 if (ld->bytes_used > ld->buffer_size) | |
137 ld->error = 1; | |
138 } | |
139 } | |
140 | |
141 /* return next n bits (right adjusted) */ | |
142 static INLINE uint32_t faad_getbits(bitfile *ld, uint32_t n DEBUGDEC) | |
143 { | |
144 uint32_t ret; | |
145 | |
146 if (ld->no_more_reading) | |
147 return 0; | |
148 | |
149 if (n == 0) | |
150 return 0; | |
151 | |
152 ret = faad_showbits(ld, n); | |
153 faad_flushbits(ld, n); | |
154 | |
155 #ifdef ANALYSIS | |
156 if (print) | |
157 fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg); | |
158 #endif | |
159 | |
160 return ret; | |
161 } | |
162 | |
163 static INLINE uint8_t faad_get1bit(bitfile *ld DEBUGDEC) | |
164 { | |
165 uint8_t r; | |
166 | |
167 if (ld->bits_left == 0) | |
168 return (uint8_t)faad_getbits(ld, 1 DEBUGVAR(print,var,dbg)); | |
169 | |
170 ld->bits_left--; | |
171 r = (uint8_t)((ld->bufa >> ld->bits_left) & 1); | |
172 | |
173 return r; | |
174 } | |
175 | |
176 /* reversed bitreading routines */ | |
177 static INLINE uint32_t faad_showbits_rev(bitfile *ld, uint32_t bits) | |
178 { | |
179 uint8_t i; | |
180 uint32_t B = 0; | |
181 | |
182 if (bits <= ld->bits_left) | |
183 { | |
184 for (i = 0; i < bits; i++) | |
185 { | |
186 if (ld->bufa & (1 << (i + (32 - ld->bits_left)))) | |
187 B |= (1 << (bits - i - 1)); | |
188 } | |
189 return B; | |
190 } else { | |
191 for (i = 0; i < ld->bits_left; i++) | |
192 { | |
193 if (ld->bufa & (1 << (i + (32 - ld->bits_left)))) | |
194 B |= (1 << (bits - i - 1)); | |
195 } | |
196 for (i = 0; i < bits - ld->bits_left; i++) | |
197 { | |
198 if (ld->bufb & (1 << (i + (32-ld->bits_left)))) | |
199 B |= (1 << (bits - ld->bits_left - i - 1)); | |
200 } | |
201 return B; | |
202 } | |
203 } | |
204 | |
205 static INLINE void faad_flushbits_rev(bitfile *ld, uint32_t bits) | |
206 { | |
207 /* do nothing if error */ | |
208 if (ld->error != 0) | |
209 return; | |
210 | |
211 if (bits < ld->bits_left) | |
212 { | |
213 ld->bits_left -= bits; | |
214 } else { | |
215 uint32_t tmp; | |
216 | |
217 ld->bufa = ld->bufb; | |
218 tmp = getdword(ld->start); | |
219 #ifndef ARCH_IS_BIG_ENDIAN | |
220 BSWAP(tmp); | |
221 #endif | |
222 ld->bufb = tmp; | |
223 ld->start--; | |
224 ld->bits_left += (32 - bits); | |
225 | |
226 ld->bytes_used += 4; | |
227 if (ld->bytes_used == ld->buffer_size) | |
228 ld->no_more_reading = 1; | |
229 if (ld->bytes_used > ld->buffer_size) | |
230 ld->error = 1; | |
231 } | |
232 } | |
233 | |
234 static INLINE uint32_t faad_getbits_rev(bitfile *ld, uint32_t n | |
235 DEBUGDEC) | |
236 { | |
237 uint32_t ret; | |
238 | |
239 if (ld->no_more_reading) | |
240 return 0; | |
241 | |
242 if (n == 0) | |
243 return 0; | |
244 | |
245 ret = faad_showbits_rev(ld, n); | |
246 faad_flushbits_rev(ld, n); | |
247 | |
248 #ifdef ANALYSIS | |
249 if (print) | |
250 fprintf(stdout, "%4d %2d bits, val: %4d, variable: %d %s\n", dbg_count++, n, ret, var, dbg); | |
251 #endif | |
252 | |
253 return ret; | |
254 } | |
255 | |
256 | |
257 #ifdef __cplusplus | |
258 } | |
259 #endif | |
260 #endif |