lookup.c 3.2 KB

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