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