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