comparison Plugins/Input/aac/libfaad2/common.c @ 1021:1e6c0a3f2d15 trunk

[svn] - 2.1 beta
author nenolod
date Wed, 10 May 2006 14:44:20 -0700
parents 29feaace84d0
children f12d7e208b43
comparison
equal deleted inserted replaced
1020:d70514b3b436 1021:1e6c0a3f2d15
1 /* 1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 ** 4 **
5 ** This program is free software; you can redistribute it and/or modify 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 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 7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version. 8 ** (at your option) any later version.
9 ** 9 **
10 ** This program is distributed in the hope that it will be useful, 10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details. 13 ** GNU General Public License for more details.
14 ** 14 **
15 ** You should have received a copy of the GNU General Public License 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 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. 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 ** 18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly 19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden. 20 ** forbidden.
21 ** 21 **
22 ** Commercial non-GPL licensing of this software is possible. 22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 ** 24 **
25 ** $Id: common.c,v 1.15 2004/01/05 14:05:11 menno Exp $ 25 ** $Id: common.c,v 1.22 2004/09/08 09:43:11 gcp Exp $
26 **/ 26 **/
27 27
28 /* just some common functions that could be used anywhere */ 28 /* just some common functions that could be used anywhere */
29 29
30 #include "common.h" 30 #include "common.h"
31 #include "structs.h" 31 #include "structs.h"
32 32
33 #include <stdlib.h> 33 #include <stdlib.h>
34 #include "syntax.h" 34 #include "syntax.h"
35 35
36 #ifdef USE_SSE
37 __declspec(naked) static int32_t __fastcall test_cpuid(void)
38 {
39 __asm
40 {
41 pushf
42 pop eax
43 mov ecx,eax
44 xor eax,(1<<21)
45 push eax
46 popf
47 pushf
48 pop eax
49 push ecx
50 popf
51 cmp eax,ecx
52 mov eax,0
53 setne al
54 ret
55 }
56 }
57
58 __declspec(naked) static void __fastcall run_cpuid(int32_t param, int32_t out[4])
59 {
60 __asm
61 {
62 pushad
63 push edx
64 mov eax,ecx
65 cpuid
66 pop edi
67 mov [edi+0],eax
68 mov [edi+4],ebx
69 mov [edi+8],ecx
70 mov [edi+12],edx
71 popad
72 ret
73 }
74 }
75
76 uint8_t cpu_has_sse()
77 {
78 int32_t features[4];
79
80 if (test_cpuid())
81 {
82 run_cpuid(1, features);
83 }
84
85 /* check for SSE */
86 if (features[3] & 0x02000000)
87 return 1;
88
89 return 0;
90 }
91 #else
92 uint8_t cpu_has_sse()
93 {
94 return 0;
95 }
96 #endif
97 36
98 /* Returns the sample rate index based on the samplerate */ 37 /* Returns the sample rate index based on the samplerate */
99 uint8_t get_sr_index(const uint32_t samplerate) 38 uint8_t get_sr_index(const uint32_t samplerate)
100 { 39 {
101 if (92017 <= samplerate) return 0; 40 if (92017 <= samplerate) return 0;
144 } 83 }
145 84
146 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type, 85 uint8_t max_tns_sfb(const uint8_t sr_index, const uint8_t object_type,
147 const uint8_t is_short) 86 const uint8_t is_short)
148 { 87 {
149 /* entry for each sampling rate 88 /* entry for each sampling rate
150 * 1 Main/LC long window 89 * 1 Main/LC long window
151 * 2 Main/LC short window 90 * 2 Main/LC short window
152 * 3 SSR long window 91 * 3 SSR long window
153 * 4 SSR short window 92 * 4 SSR short window
154 */ 93 */
228 } 167 }
229 168
230 return -1; 169 return -1;
231 } 170 }
232 171
233 /* common malloc function */ 172 void *faad_malloc(size_t size)
234 void *faad_malloc(int32_t size)
235 { 173 {
236 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) 174 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
237 return _aligned_malloc(size, 16); 175 return _aligned_malloc(size, 16);
238 #else 176 #else // #ifdef 0
239 return malloc(size); 177 return malloc(size);
240 #endif 178 #endif // #ifdef 0
241 } 179 }
242 180
243 /* common free function */ 181 /* common free function */
244 void faad_free(void *b) 182 void faad_free(void *b)
245 { 183 {
246 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) 184 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
247 _aligned_free(b); 185 _aligned_free(b);
248 #else 186 #else
249 free(b); 187 free(b);
250 #endif 188 }
251 } 189 #endif
252 190
253 static const uint8_t Parity [256] = { // parity 191 static const uint8_t Parity [256] = { // parity
254 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 192 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
255 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 193 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
256 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 194 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
257 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 195 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
258 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 196 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,
259 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 197 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
260 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1, 198 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,
261 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0 199 1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0
262 }; 200 };
263 201
264 static uint32_t __r1 = 1; 202 static uint32_t __r1 = 1;
265 static uint32_t __r2 = 1; 203 static uint32_t __r2 = 1;
266 204
291 * which gives a period of 18.410.713.077.675.721.215. The result is the 229 * which gives a period of 18.410.713.077.675.721.215. The result is the
292 * XORed values of both generators. 230 * XORed values of both generators.
293 */ 231 */
294 uint32_t random_int(void) 232 uint32_t random_int(void)
295 { 233 {
296 uint32_t t1, t2, t3, t4; 234 uint32_t t1, t2, t3, t4;
297 235
298 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available 236 t3 = t1 = __r1; t4 = t2 = __r2; // Parity calculation is done via table lookup, this is also available
299 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable 237 t1 &= 0xF5; t2 >>= 25; // on CPUs without parity, can be implemented in C and avoid unpredictable
300 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations. 238 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
301 t1 <<= 31; t2 = Parity [t2]; 239 t1 <<= 31; t2 = Parity [t2];
302 240
303 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 ); 241 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
304 } 242 }
243
244 uint32_t ones32(uint32_t x)
245 {
246 x -= ((x >> 1) & 0x55555555);
247 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
248 x = (((x >> 4) + x) & 0x0f0f0f0f);
249 x += (x >> 8);
250 x += (x >> 16);
251
252 return (x & 0x0000003f);
253 }
254
255 uint32_t floor_log2(uint32_t x)
256 {
257 #if 1
258 x |= (x >> 1);
259 x |= (x >> 2);
260 x |= (x >> 4);
261 x |= (x >> 8);
262 x |= (x >> 16);
263
264 return (ones32(x) - 1);
265 #else
266 uint32_t count = 0;
267
268 while (x >>= 1)
269 count++;
270
271 return count;
272 #endif
273 }
274
275 /* returns position of first bit that is not 0 from msb,
276 * starting count at lsb */
277 uint32_t wl_min_lzc(uint32_t x)
278 {
279 #if 1
280 x |= (x >> 1);
281 x |= (x >> 2);
282 x |= (x >> 4);
283 x |= (x >> 8);
284 x |= (x >> 16);
285
286 return (ones32(x));
287 #else
288 uint32_t count = 0;
289
290 while (x >>= 1)
291 count++;
292
293 return (count + 1);
294 #endif
295 }
296
297 #ifdef FIXED_POINT
298
299 #define TABLE_BITS 6
300 /* just take the maximum number of bits for interpolation */
301 #define INTERP_BITS (REAL_BITS-TABLE_BITS)
302
303 static const real_t pow2_tab[] = {
304 REAL_CONST(1.000000000000000), REAL_CONST(1.010889286051701), REAL_CONST(1.021897148654117),
305 REAL_CONST(1.033024879021228), REAL_CONST(1.044273782427414), REAL_CONST(1.055645178360557),
306 REAL_CONST(1.067140400676824), REAL_CONST(1.078760797757120), REAL_CONST(1.090507732665258),
307 REAL_CONST(1.102382583307841), REAL_CONST(1.114386742595892), REAL_CONST(1.126521618608242),
308 REAL_CONST(1.138788634756692), REAL_CONST(1.151189229952983), REAL_CONST(1.163724858777578),
309 REAL_CONST(1.176396991650281), REAL_CONST(1.189207115002721), REAL_CONST(1.202156731452703),
310 REAL_CONST(1.215247359980469), REAL_CONST(1.228480536106870), REAL_CONST(1.241857812073484),
311 REAL_CONST(1.255380757024691), REAL_CONST(1.269050957191733), REAL_CONST(1.282870016078778),
312 REAL_CONST(1.296839554651010), REAL_CONST(1.310961211524764), REAL_CONST(1.325236643159741),
313 REAL_CONST(1.339667524053303), REAL_CONST(1.354255546936893), REAL_CONST(1.369002422974591),
314 REAL_CONST(1.383909881963832), REAL_CONST(1.398979672538311), REAL_CONST(1.414213562373095),
315 REAL_CONST(1.429613338391970), REAL_CONST(1.445180806977047), REAL_CONST(1.460917794180647),
316 REAL_CONST(1.476826145939499), REAL_CONST(1.492907728291265), REAL_CONST(1.509164427593423),
317 REAL_CONST(1.525598150744538), REAL_CONST(1.542210825407941), REAL_CONST(1.559004400237837),
318 REAL_CONST(1.575980845107887), REAL_CONST(1.593142151342267), REAL_CONST(1.610490331949254),
319 REAL_CONST(1.628027421857348), REAL_CONST(1.645755478153965), REAL_CONST(1.663676580326736),
320 REAL_CONST(1.681792830507429), REAL_CONST(1.700106353718524), REAL_CONST(1.718619298122478),
321 REAL_CONST(1.737333835273706), REAL_CONST(1.756252160373300), REAL_CONST(1.775376492526521),
322 REAL_CONST(1.794709075003107), REAL_CONST(1.814252175500399), REAL_CONST(1.834008086409342),
323 REAL_CONST(1.853979125083386), REAL_CONST(1.874167634110300), REAL_CONST(1.894575981586966),
324 REAL_CONST(1.915206561397147), REAL_CONST(1.936061793492294), REAL_CONST(1.957144124175400),
325 REAL_CONST(1.978456026387951), REAL_CONST(2.000000000000000)
326 };
327
328 static const real_t log2_tab[] = {
329 REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453),
330 REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169),
331 REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312),
332 REAL_CONST(0.189824558880017), REAL_CONST(0.209453365628950), REAL_CONST(0.228818690495881),
333 REAL_CONST(0.247927513443585), REAL_CONST(0.266786540694901), REAL_CONST(0.285402218862248),
334 REAL_CONST(0.303780748177103), REAL_CONST(0.321928094887362), REAL_CONST(0.339850002884625),
335 REAL_CONST(0.357552004618084), REAL_CONST(0.375039431346925), REAL_CONST(0.392317422778760),
336 REAL_CONST(0.409390936137702), REAL_CONST(0.426264754702098), REAL_CONST(0.442943495848728),
337 REAL_CONST(0.459431618637297), REAL_CONST(0.475733430966398), REAL_CONST(0.491853096329675),
338 REAL_CONST(0.507794640198696), REAL_CONST(0.523561956057013), REAL_CONST(0.539158811108031),
339 REAL_CONST(0.554588851677637), REAL_CONST(0.569855608330948), REAL_CONST(0.584962500721156),
340 REAL_CONST(0.599912842187128), REAL_CONST(0.614709844115208), REAL_CONST(0.629356620079610),
341 REAL_CONST(0.643856189774725), REAL_CONST(0.658211482751795), REAL_CONST(0.672425341971496),
342 REAL_CONST(0.686500527183218), REAL_CONST(0.700439718141092), REAL_CONST(0.714245517666123),
343 REAL_CONST(0.727920454563199), REAL_CONST(0.741466986401147), REAL_CONST(0.754887502163469),
344 REAL_CONST(0.768184324776926), REAL_CONST(0.781359713524660), REAL_CONST(0.794415866350106),
345 REAL_CONST(0.807354922057604), REAL_CONST(0.820178962415188), REAL_CONST(0.832890014164742),
346 REAL_CONST(0.845490050944375), REAL_CONST(0.857980995127572), REAL_CONST(0.870364719583405),
347 REAL_CONST(0.882643049361841), REAL_CONST(0.894817763307943), REAL_CONST(0.906890595608519),
348 REAL_CONST(0.918863237274595), REAL_CONST(0.930737337562886), REAL_CONST(0.942514505339240),
349 REAL_CONST(0.954196310386875), REAL_CONST(0.965784284662087), REAL_CONST(0.977279923499917),
350 REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000)
351 };
352
353 real_t pow2_fix(real_t val)
354 {
355 uint32_t x1, x2;
356 uint32_t errcorr;
357 uint32_t index_frac;
358 real_t retval;
359 int32_t whole = (val >> REAL_BITS);
360
361 /* rest = [0..1] */
362 int32_t rest = val - (whole << REAL_BITS);
363
364 /* index into pow2_tab */
365 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
366
367
368 if (val == 0)
369 return (1<<REAL_BITS);
370
371 /* leave INTERP_BITS bits */
372 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
373 index_frac = index_frac & ((1<<INTERP_BITS)-1);
374
375 if (whole > 0)
376 {
377 retval = 1 << whole;
378 } else {
379 retval = REAL_CONST(1) >> -whole;
380 }
381
382 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
383 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
384 errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
385
386 if (whole > 0)
387 {
388 retval = retval * (errcorr + x1);
389 } else {
390 retval = MUL_R(retval, (errcorr + x1));
391 }
392
393 return retval;
394 }
395
396 int32_t pow2_int(real_t val)
397 {
398 uint32_t x1, x2;
399 uint32_t errcorr;
400 uint32_t index_frac;
401 real_t retval;
402 int32_t whole = (val >> REAL_BITS);
403
404 /* rest = [0..1] */
405 int32_t rest = val - (whole << REAL_BITS);
406
407 /* index into pow2_tab */
408 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
409
410
411 if (val == 0)
412 return 1;
413
414 /* leave INTERP_BITS bits */
415 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
416 index_frac = index_frac & ((1<<INTERP_BITS)-1);
417
418 if (whole > 0)
419 retval = 1 << whole;
420 else
421 retval = 0;
422
423 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
424 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
425 errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
426
427 retval = MUL_R(retval, (errcorr + x1));
428
429 return retval;
430 }
431
432 /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */
433 int32_t log2_int(uint32_t val)
434 {
435 uint32_t frac;
436 uint32_t whole = (val);
437 int32_t exp = 0;
438 uint32_t index;
439 uint32_t index_frac;
440 uint32_t x1, x2;
441 uint32_t errcorr;
442
443 /* error */
444 if (val == 0)
445 return -10000;
446
447 exp = floor_log2(val);
448 exp -= REAL_BITS;
449
450 /* frac = [1..2] */
451 if (exp >= 0)
452 frac = val >> exp;
453 else
454 frac = val << -exp;
455
456 /* index in the log2 table */
457 index = frac >> (REAL_BITS-TABLE_BITS);
458
459 /* leftover part for linear interpolation */
460 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
461
462 /* leave INTERP_BITS bits */
463 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
464
465 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
466 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
467
468 /* linear interpolation */
469 /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */
470
471 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
472
473 return ((exp+REAL_BITS) << REAL_BITS) + errcorr + x1;
474 }
475
476 /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */
477 real_t log2_fix(uint32_t val)
478 {
479 uint32_t frac;
480 uint32_t whole = (val >> REAL_BITS);
481 int8_t exp = 0;
482 uint32_t index;
483 uint32_t index_frac;
484 uint32_t x1, x2;
485 uint32_t errcorr;
486
487 /* error */
488 if (val == 0)
489 return -100000;
490
491 exp = floor_log2(val);
492 exp -= REAL_BITS;
493
494 /* frac = [1..2] */
495 if (exp >= 0)
496 frac = val >> exp;
497 else
498 frac = val << -exp;
499
500 /* index in the log2 table */
501 index = frac >> (REAL_BITS-TABLE_BITS);
502
503 /* leftover part for linear interpolation */
504 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
505
506 /* leave INTERP_BITS bits */
507 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
508
509 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
510 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
511
512 /* linear interpolation */
513 /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */
514
515 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
516
517 return (exp << REAL_BITS) + errcorr + x1;
518 }
519 #endif