shithub: opus

Download patch

ref: 2eaba8ac1fdfadbcc9c4c357c60408c861c66937
parent: 98193470ecbc53c4699a1ca97421e898808f8b35
author: Jean-Marc Valin <[email protected]>
date: Thu Feb 28 17:14:54 EST 2008

fixed-point: log approximation

--- a/libcelt/mathops.h
+++ b/libcelt/mathops.h
@@ -54,6 +54,11 @@
 
 #include "entcode.h"
 
+static inline celt_int16_t celt_ilog2(celt_word32_t x)
+{
+   return EC_ILOG(x)-1;
+}
+
 #define C0 3634
 #define C1 21173
 #define C2 -12627
@@ -116,6 +121,19 @@
    }
 }
 
+static inline celt_word16_t celt_log2(celt_word32_t x)
+{
+   int i;
+   /*-0.41446   0.96093  -0.33981   0.15600 */
+   const celt_word16_t C[4] = {-6791, 7872, -1392, 319};
+   if (x==0)
+      return -32767;
+   i = celt_ilog2(x);
+   celt_word16_t n = VSHR32(x,i-15)-32768-16384;
+   celt_word16_t ret = ADD16(C[0], MULT16_16_Q14(n, ADD16(C[1], MULT16_16_Q14(n, ADD16(C[2], MULT16_16_Q14(n, (C[3])))))));
+   /*printf ("%d %d %d %d\n", x, n, ret, SHL16(i-13,8)+SHR16(ret,14-8));*/
+   return SHL16(i-13,8)+SHR16(ret,14-8);
+}
 
 #endif
 
--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -38,6 +38,7 @@
 #include <math.h>
 #include "os_support.h"
 #include "arch.h"
+#include "mathops.h"
 
 #ifdef FIXED_POINT
 const celt_word16_t eMeans[24] = {11520, -2048, -3072, -640, 256, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@@ -49,6 +50,37 @@
 const int frac[24] = {8, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
 
 #ifdef FIXED_POINT
+static inline celt_ener_t dB2Amp( dB)
+{
+   celt_ener_t amp;
+   amp = pow(10, .05*dB)-.3;
+   if (amp < 0)
+      amp = 0;
+}
+
+#define DBofTWO 24661
+static inline celt_word16_t amp2dB(celt_ener_t amp)
+{
+   /* equivalent to return 6.0207*log2(.3+amp) */
+   return ROUND(MULT16_16(24661,celt_log2(ADD32(QCONST32(.3f,14),amp))),12);
+   /* return DB_SCALING*20*log10(.3+ENER_SCALING_1*amp); */
+}
+#else
+static inline celt_ener_t dB2Amp( dB)
+{
+   celt_ener_t amp;
+   amp = pow(10, .05*dB)-.3;
+   if (amp < 0)
+      amp = 0;
+}
+static inline celt_word16_t amp2dB(celt_ener_t amp)
+{
+   return 20*log10(.3+amp);
+}
+#endif
+
+
+#ifdef FIXED_POINT
 #define Q8 256.f
 #define Q8_1 (1.f/256.f)
 #else
@@ -75,7 +107,7 @@
       celt_word16_t x;   /* dB */
       celt_word16_t f;   /* Q8 */
       celt_word16_t mean = (1-coef)*eMeans[i];
-      x = DB_SCALING*20*log10(.3+ENER_SCALING_1*eBands[i]);
+      x = amp2dB(eBands[i]);
       res = DB_SCALING*6;
       f = QCONST16(1.f,8)*(x-mean-coef*oldEBands[i]-prev*1.f)/res;
 #ifdef FIXED_POINT