lookup.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH *
  6. * THIS SOURCE. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and the XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************
  13. function: lookup based functions
  14. last mod: $Id: lookup.c,v 1.2.2.2 2000/11/04 06:21:44 xiphmont Exp $
  15. ********************************************************************/
  16. #include <math.h>
  17. #include "lookup.h"
  18. #include "lookup_data.h"
  19. #include "os.h"
  20. #ifdef FLOAT_LOOKUP
  21. /* interpolated lookup based cos function, domain 0 to PI only */
  22. float vorbis_coslook(float a){
  23. double d=a*(.31830989*(float)COS_LOOKUP_SZ);
  24. int i=vorbis_ftoi(d-.5);
  25. return COS_LOOKUP[i]+ (d-i)*(COS_LOOKUP[i+1]-COS_LOOKUP[i]);
  26. }
  27. /* interpolated 1./sqrt(p) where .5 <= p < 1. */
  28. float vorbis_invsqlook(float a){
  29. double d=a*(2.*(float)INVSQ_LOOKUP_SZ)-(float)INVSQ_LOOKUP_SZ;
  30. int i=vorbis_ftoi(d-.5);
  31. return INVSQ_LOOKUP[i]+ (d-i)*(INVSQ_LOOKUP[i+1]-INVSQ_LOOKUP[i]);
  32. }
  33. /* interpolated 1./sqrt(p) where .5 <= p < 1. */
  34. float vorbis_invsq2explook(int a){
  35. return INVSQ2EXP_LOOKUP[a-INVSQ2EXP_LOOKUP_MIN];
  36. }
  37. #include <stdio.h>
  38. /* interpolated lookup based fromdB function, domain -140dB to 0dB only */
  39. float vorbis_fromdBlook(float a){
  40. int i=vorbis_ftoi(a*((float)(-(1<<FROMdB2_SHIFT)))-5.);
  41. return (i<0)?1.:
  42. ((i>=(FROMdB_LOOKUP_SZ<<FROMdB_SHIFT))?0.:
  43. FROMdB_LOOKUP[i>>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]);
  44. }
  45. #endif
  46. #ifdef INT_LOOKUP
  47. /* interpolated 1./sqrt(p) where .5 <= a < 1. (.100000... to .111111...) in
  48. 16.16 format
  49. returns in m.8 format */
  50. long vorbis_invsqlook_i(long a,long e){
  51. long i=(a&0x7fff)>>(INVSQ_LOOKUP_I_SHIFT-1);
  52. long d=(a&INVSQ_LOOKUP_I_MASK)<<(16-INVSQ_LOOKUP_I_SHIFT); /* 0.16 */
  53. long val=INVSQ_LOOKUP_I[i]- /* 1.16 */
  54. (((INVSQ_LOOKUP_I[i]-INVSQ_LOOKUP_I[i+1])* /* 0.16 */
  55. d)>>16); /* result 1.16 */
  56. e+=32;
  57. if(e&1)val=(val*5792)>>13; /* multiply val by 1/sqrt(2) */
  58. e=(e>>1)-8;
  59. return(val>>e);
  60. }
  61. /* interpolated lookup based fromdB function, domain -140dB to 0dB only */
  62. /* a is in n.12 format */
  63. float vorbis_fromdBlook_i(long a){
  64. int i=(-a)>>(12-FROMdB2_SHIFT);
  65. return (i<0)?1.:
  66. ((i>=(FROMdB_LOOKUP_SZ<<FROMdB_SHIFT))?0.:
  67. FROMdB_LOOKUP[i>>FROMdB_SHIFT]*FROMdB2_LOOKUP[i&FROMdB2_MASK]);
  68. }
  69. /* interpolated lookup based cos function, domain 0 to PI only */
  70. /* a is in 0.16 format, where 0==0, 2^^16-1==PI, return 0.14 */
  71. long vorbis_coslook_i(long a){
  72. int i=a>>COS_LOOKUP_I_SHIFT;
  73. int d=a&COS_LOOKUP_I_MASK;
  74. return COS_LOOKUP_I[i]- ((d*(COS_LOOKUP_I[i]-COS_LOOKUP_I[i+1]))>>
  75. COS_LOOKUP_I_SHIFT);
  76. }
  77. #endif