comparison libfaad2/common.c @ 13453:6d50ef45a058

Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12. patch by adland <adland123 at yahoo dot com>
author diego
date Fri, 24 Sep 2004 17:31:36 +0000
parents d81145997036
children 2ae5ab4331ca
comparison
equal deleted inserted replaced
13452:c364b7c13dd8 13453:6d50ef45a058
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 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30
26 ** $Id: common.c,v 1.3 2004/06/02 22:59:02 diego Exp $ 26 ** $Id: common.c,v 1.4 2004/06/23 13:50:49 diego Exp $
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ 27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
28 **/ 28 **/
29 29
30 /* just some common functions that could be used anywhere */ 30 /* just some common functions that could be used anywhere */
31 31
231 231
232 return -1; 232 return -1;
233 } 233 }
234 234
235 /* common malloc function */ 235 /* common malloc function */
236 void *faad_malloc(int32_t size) 236 void *faad_malloc(size_t size)
237 { 237 {
238 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE) 238 #if 0 // defined(_WIN32) && !defined(_WIN32_WCE)
239 return _aligned_malloc(size, 16); 239 return _aligned_malloc(size, 16);
240 #else 240 #else
241 return malloc(size); 241 return malloc(size);
302 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations. 302 t1 = Parity [t1]; t2 &= 0x63; // jumps and slow rotate through the carry flag operations.
303 t1 <<= 31; t2 = Parity [t2]; 303 t1 <<= 31; t2 = Parity [t2];
304 304
305 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 ); 305 return (__r1 = (t3 >> 1) | t1 ) ^ (__r2 = (t4 + t4) | t2 );
306 } 306 }
307
308 uint32_t ones32(uint32_t x)
309 {
310 x -= ((x >> 1) & 0x55555555);
311 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
312 x = (((x >> 4) + x) & 0x0f0f0f0f);
313 x += (x >> 8);
314 x += (x >> 16);
315
316 return (x & 0x0000003f);
317 }
318
319 uint32_t floor_log2(uint32_t x)
320 {
321 #if 1
322 x |= (x >> 1);
323 x |= (x >> 2);
324 x |= (x >> 4);
325 x |= (x >> 8);
326 x |= (x >> 16);
327
328 return (ones32(x) - 1);
329 #else
330 uint32_t count = 0;
331
332 while (x >>= 1)
333 count++;
334
335 return count;
336 #endif
337 }
338
339 /* returns position of first bit that is not 0 from msb,
340 * starting count at lsb */
341 uint32_t wl_min_lzc(uint32_t x)
342 {
343 #if 1
344 x |= (x >> 1);
345 x |= (x >> 2);
346 x |= (x >> 4);
347 x |= (x >> 8);
348 x |= (x >> 16);
349
350 return (ones32(x));
351 #else
352 uint32_t count = 0;
353
354 while (x >>= 1)
355 count++;
356
357 return (count + 1);
358 #endif
359 }
360
361 #ifdef FIXED_POINT
362
363 #define TABLE_BITS 6
364 /* just take the maximum number of bits for interpolation */
365 #define INTERP_BITS (REAL_BITS-TABLE_BITS)
366
367 static const real_t pow2_tab[] = {
368 REAL_CONST(1.000000000000000), REAL_CONST(1.010889286051701), REAL_CONST(1.021897148654117),
369 REAL_CONST(1.033024879021228), REAL_CONST(1.044273782427414), REAL_CONST(1.055645178360557),
370 REAL_CONST(1.067140400676824), REAL_CONST(1.078760797757120), REAL_CONST(1.090507732665258),
371 REAL_CONST(1.102382583307841), REAL_CONST(1.114386742595892), REAL_CONST(1.126521618608242),
372 REAL_CONST(1.138788634756692), REAL_CONST(1.151189229952983), REAL_CONST(1.163724858777578),
373 REAL_CONST(1.176396991650281), REAL_CONST(1.189207115002721), REAL_CONST(1.202156731452703),
374 REAL_CONST(1.215247359980469), REAL_CONST(1.228480536106870), REAL_CONST(1.241857812073484),
375 REAL_CONST(1.255380757024691), REAL_CONST(1.269050957191733), REAL_CONST(1.282870016078778),
376 REAL_CONST(1.296839554651010), REAL_CONST(1.310961211524764), REAL_CONST(1.325236643159741),
377 REAL_CONST(1.339667524053303), REAL_CONST(1.354255546936893), REAL_CONST(1.369002422974591),
378 REAL_CONST(1.383909881963832), REAL_CONST(1.398979672538311), REAL_CONST(1.414213562373095),
379 REAL_CONST(1.429613338391970), REAL_CONST(1.445180806977047), REAL_CONST(1.460917794180647),
380 REAL_CONST(1.476826145939499), REAL_CONST(1.492907728291265), REAL_CONST(1.509164427593423),
381 REAL_CONST(1.525598150744538), REAL_CONST(1.542210825407941), REAL_CONST(1.559004400237837),
382 REAL_CONST(1.575980845107887), REAL_CONST(1.593142151342267), REAL_CONST(1.610490331949254),
383 REAL_CONST(1.628027421857348), REAL_CONST(1.645755478153965), REAL_CONST(1.663676580326736),
384 REAL_CONST(1.681792830507429), REAL_CONST(1.700106353718524), REAL_CONST(1.718619298122478),
385 REAL_CONST(1.737333835273706), REAL_CONST(1.756252160373300), REAL_CONST(1.775376492526521),
386 REAL_CONST(1.794709075003107), REAL_CONST(1.814252175500399), REAL_CONST(1.834008086409342),
387 REAL_CONST(1.853979125083386), REAL_CONST(1.874167634110300), REAL_CONST(1.894575981586966),
388 REAL_CONST(1.915206561397147), REAL_CONST(1.936061793492294), REAL_CONST(1.957144124175400),
389 REAL_CONST(1.978456026387951), REAL_CONST(2.000000000000000)
390 };
391
392 static const real_t log2_tab[] = {
393 REAL_CONST(0.000000000000000), REAL_CONST(0.022367813028455), REAL_CONST(0.044394119358453),
394 REAL_CONST(0.066089190457772), REAL_CONST(0.087462841250339), REAL_CONST(0.108524456778169),
395 REAL_CONST(0.129283016944966), REAL_CONST(0.149747119504682), REAL_CONST(0.169925001442312),
396 REAL_CONST(0.189824558880017), REAL_CONST(0.209453365628950), REAL_CONST(0.228818690495881),
397 REAL_CONST(0.247927513443585), REAL_CONST(0.266786540694901), REAL_CONST(0.285402218862248),
398 REAL_CONST(0.303780748177103), REAL_CONST(0.321928094887362), REAL_CONST(0.339850002884625),
399 REAL_CONST(0.357552004618084), REAL_CONST(0.375039431346925), REAL_CONST(0.392317422778760),
400 REAL_CONST(0.409390936137702), REAL_CONST(0.426264754702098), REAL_CONST(0.442943495848728),
401 REAL_CONST(0.459431618637297), REAL_CONST(0.475733430966398), REAL_CONST(0.491853096329675),
402 REAL_CONST(0.507794640198696), REAL_CONST(0.523561956057013), REAL_CONST(0.539158811108031),
403 REAL_CONST(0.554588851677637), REAL_CONST(0.569855608330948), REAL_CONST(0.584962500721156),
404 REAL_CONST(0.599912842187128), REAL_CONST(0.614709844115208), REAL_CONST(0.629356620079610),
405 REAL_CONST(0.643856189774725), REAL_CONST(0.658211482751795), REAL_CONST(0.672425341971496),
406 REAL_CONST(0.686500527183218), REAL_CONST(0.700439718141092), REAL_CONST(0.714245517666123),
407 REAL_CONST(0.727920454563199), REAL_CONST(0.741466986401147), REAL_CONST(0.754887502163469),
408 REAL_CONST(0.768184324776926), REAL_CONST(0.781359713524660), REAL_CONST(0.794415866350106),
409 REAL_CONST(0.807354922057604), REAL_CONST(0.820178962415188), REAL_CONST(0.832890014164742),
410 REAL_CONST(0.845490050944375), REAL_CONST(0.857980995127572), REAL_CONST(0.870364719583405),
411 REAL_CONST(0.882643049361841), REAL_CONST(0.894817763307943), REAL_CONST(0.906890595608519),
412 REAL_CONST(0.918863237274595), REAL_CONST(0.930737337562886), REAL_CONST(0.942514505339240),
413 REAL_CONST(0.954196310386875), REAL_CONST(0.965784284662087), REAL_CONST(0.977279923499917),
414 REAL_CONST(0.988684686772166), REAL_CONST(1.000000000000000)
415 };
416
417 real_t pow2_fix(real_t val)
418 {
419 uint32_t x1, x2;
420 uint32_t errcorr;
421 uint32_t index_frac;
422 real_t retval;
423 int32_t whole = (val >> REAL_BITS);
424
425 /* rest = [0..1] */
426 int32_t rest = val - (whole << REAL_BITS);
427
428 /* index into pow2_tab */
429 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
430
431
432 if (val == 0)
433 return (1<<REAL_BITS);
434
435 /* leave INTERP_BITS bits */
436 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
437 index_frac = index_frac & ((1<<INTERP_BITS)-1);
438
439 if (whole > 0)
440 {
441 retval = 1 << whole;
442 } else {
443 retval = REAL_CONST(1) >> -whole;
444 }
445
446 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
447 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
448 errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
449
450 if (whole > 0)
451 {
452 retval = retval * (errcorr + x1);
453 } else {
454 retval = MUL_R(retval, (errcorr + x1));
455 }
456
457 return retval;
458 }
459
460 int32_t pow2_int(real_t val)
461 {
462 uint32_t x1, x2;
463 uint32_t errcorr;
464 uint32_t index_frac;
465 real_t retval;
466 int32_t whole = (val >> REAL_BITS);
467
468 /* rest = [0..1] */
469 int32_t rest = val - (whole << REAL_BITS);
470
471 /* index into pow2_tab */
472 int32_t index = rest >> (REAL_BITS-TABLE_BITS);
473
474
475 if (val == 0)
476 return 1;
477
478 /* leave INTERP_BITS bits */
479 index_frac = rest >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
480 index_frac = index_frac & ((1<<INTERP_BITS)-1);
481
482 if (whole > 0)
483 retval = 1 << whole;
484 else
485 retval = 0;
486
487 x1 = pow2_tab[index & ((1<<TABLE_BITS)-1)];
488 x2 = pow2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
489 errcorr = ( (index_frac*(x2-x1))) >> INTERP_BITS;
490
491 retval = MUL_R(retval, (errcorr + x1));
492
493 return retval;
494 }
495
496 /* ld(x) = ld(x*y/y) = ld(x/y) + ld(y), with y=2^N and [1 <= (x/y) < 2] */
497 int32_t log2_int(uint32_t val)
498 {
499 uint32_t frac;
500 uint32_t whole = (val);
501 int8_t exp = 0;
502 uint32_t index;
503 uint32_t index_frac;
504 uint32_t x1, x2;
505 uint32_t errcorr;
506
507 /* error */
508 if (val == 0)
509 return -10000;
510
511 exp = floor_log2(val);
512 exp -= REAL_BITS;
513
514 /* frac = [1..2] */
515 if (exp >= 0)
516 frac = val >> exp;
517 else
518 frac = val << -exp;
519
520 /* index in the log2 table */
521 index = frac >> (REAL_BITS-TABLE_BITS);
522
523 /* leftover part for linear interpolation */
524 index_frac = frac & ((1<<(REAL_BITS-TABLE_BITS))-1);
525
526 /* leave INTERP_BITS bits */
527 index_frac = index_frac >> (REAL_BITS-TABLE_BITS-INTERP_BITS);
528
529 x1 = log2_tab[index & ((1<<TABLE_BITS)-1)];
530 x2 = log2_tab[(index & ((1<<TABLE_BITS)-1)) + 1];
531
532 /* linear interpolation */
533 /* retval = exp + ((index_frac)*x2 + (1-index_frac)*x1) */
534
535 errcorr = (index_frac * (x2-x1)) >> INTERP_BITS;
536
537 return ((exp+REAL_BITS) << REAL_BITS) + errcorr + x1;
538 }
539 #endif