shithub: opus

Download patch

ref: bd0610d21b28b76095a49e601df44fb792b96369
parent: 5503f11a3d77b68c6f5b7b229fdd7d8ce2bff005
author: Jean-Marc Valin <[email protected]>
date: Sat Mar 13 05:04:19 EST 2010

Increasing log energy resolution

--- a/libcelt/arch.h
+++ b/libcelt/arch.h
@@ -96,7 +96,7 @@
 #define PGAIN_SCALING_1 (1.f/32768.f)
 #define PGAIN_SHIFT 15
 
-#define DB_SHIFT 8
+#define DB_SHIFT 10
 
 #define EPSILON 1
 #define VERY_SMALL 0
--- a/libcelt/mathops.h
+++ b/libcelt/mathops.h
@@ -315,7 +315,7 @@
    i = celt_ilog2(x);
    n = VSHR32(x,i-15)-32768-16384;
    frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2], MULT16_16_Q15(n, (C[3])))))));
-   return SHL16(i-13,8)+SHR16(frac,14-8);
+   return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT);
 }
 
 /*
--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -44,11 +44,7 @@
 
 #define E_MEANS_SIZE (5)
 
-#ifdef FIXED_POINT
-const celt_word16 eMeans[E_MEANS_SIZE] = {1920, -341, -512, -107, 43};
-#else
-const celt_word16 eMeans[E_MEANS_SIZE] = {7.5f, -1.33f, -2.f, -0.42f, 0.17f};
-#endif
+const celt_word16 eMeans[E_MEANS_SIZE] = {QCONST16(7.5f,DB_SHIFT), -QCONST16(1.33f,DB_SHIFT), -QCONST16(2.f,DB_SHIFT), -QCONST16(0.42f,DB_SHIFT), QCONST16(0.17f,DB_SHIFT)};
 
 /* FIXME: Implement for stereo */
 int intra_decision(celt_word16 *eBands, celt_word16 *oldEBands, int len)
@@ -60,7 +56,7 @@
       celt_word16 d = SUB16(eBands[i], oldEBands[i]);
       dist = MAC16_16(dist, d,d);
    }
-   return SHR32(dist,16) > 2*len;
+   return SHR32(dist,2*DB_SHIFT) > 2*len;
 }
 
 int *quant_prob_alloc(const CELTMode *m)
@@ -111,15 +107,15 @@
       c=0;
       do {
          int qi;
-         celt_word16 q;   /* dB */
-         celt_word16 x;   /* dB */
-         celt_word16 f;   /* Q8 */
+         celt_word16 q;
+         celt_word16 x;
+         celt_word16 f;
          celt_word16 mean =  (i-start < E_MEANS_SIZE) ? MULT16_16_P15(Q15ONE-coef,eMeans[i-start]) : 0;
          x = eBands[i+c*m->nbEBands];
 #ifdef FIXED_POINT
          f = x-mean -MULT16_16_P15(coef,oldEBands[i+c*m->nbEBands])-prev[c];
          /* Rounding to nearest integer here is really important! */
-         qi = (f+128)>>8;
+         qi = (f+QCONST16(.5,DB_SHIFT))>>DB_SHIFT;
 #else
          f = x-mean-coef*oldEBands[i+c*m->nbEBands]-prev[c];
          /* Rounding to nearest integer here is really important! */
@@ -131,10 +127,10 @@
          if (bits_used > budget)
          {
             qi = -1;
-            error[i+c*m->nbEBands] = 128;
+            error[i+c*m->nbEBands] = QCONST16(.5f,DB_SHIFT);
          } else {
             ec_laplace_encode_start(enc, &qi, prob[2*i], prob[2*i+1]);
-            error[i+c*m->nbEBands] = f - SHL16(qi,8);
+            error[i+c*m->nbEBands] = f - SHL16(qi,DB_SHIFT);
          }
          q = SHL16(qi,DB_SHIFT);
          
@@ -162,7 +158,7 @@
          celt_word16 offset;
 #ifdef FIXED_POINT
          /* Has to be without rounding */
-         q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,8))>>(8-fine_quant[i]);
+         q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]);
 #else
          q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac);
 #endif
@@ -170,7 +166,7 @@
             q2 = frac-1;
          ec_enc_bits(enc, q2, fine_quant[i]);
 #ifdef FIXED_POINT
-         offset = SUB16(SHR16(SHL16(q2,8)+QCONST16(.5,8),fine_quant[i]),QCONST16(.5f,8));
+         offset = SUB16(SHR16(SHL16(q2,DB_SHIFT)+QCONST16(.5,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
 #else
          offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
 #endif
@@ -203,7 +199,7 @@
             q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
             ec_enc_bits(enc, q2, 1);
 #ifdef FIXED_POINT
-            offset = SHR16(SHL16(q2,8)-QCONST16(.5,8),fine_quant[i]+1);
+            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5,DB_SHIFT),fine_quant[i]+1);
 #else
             offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
 #endif
@@ -215,8 +211,8 @@
    for (i=start;i<C*m->nbEBands;i++)
    {
       eBands[i] = log2Amp(oldEBands[i]);
-      if (oldEBands[i] < -QCONST16(7.f,8))
-         oldEBands[i] = -QCONST16(7.f,8);
+      if (oldEBands[i] < -QCONST16(7.f,DB_SHIFT))
+         oldEBands[i] = -QCONST16(7.f,DB_SHIFT);
    }
 }
 
@@ -253,7 +249,7 @@
          q = SHL16(qi,DB_SHIFT);
 
          oldEBands[i+c*m->nbEBands] = MULT16_16_P15(coef,oldEBands[i+c*m->nbEBands])+(mean+prev[c]+q);
-         prev[c] = mean+prev[c]+MULT16_16_Q15(Q15ONE-beta,q);
+         prev[c] = mean+prev[c]+MULT16_16_P15(Q15ONE-beta,q);
       } while (++c < C);
    }
 }
@@ -273,7 +269,7 @@
          celt_word16 offset;
          q2 = ec_dec_bits(dec, fine_quant[i]);
 #ifdef FIXED_POINT
-         offset = SUB16(SHR16(SHL16(q2,8)+QCONST16(.5,8),fine_quant[i]),QCONST16(.5f,8));
+         offset = SUB16(SHR16(SHL16(q2,DB_SHIFT)+QCONST16(.5,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
 #else
          offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
 #endif
@@ -302,7 +298,7 @@
             celt_word16 offset;
             q2 = ec_dec_bits(dec, 1);
 #ifdef FIXED_POINT
-            offset = SHR16(SHL16(q2,8)-QCONST16(.5,8),fine_quant[i]+1);
+            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5,DB_SHIFT),fine_quant[i]+1);
 #else
             offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
 #endif
@@ -314,7 +310,7 @@
    for (i=start;i<C*m->nbEBands;i++)
    {
       eBands[i] = log2Amp(oldEBands[i]);
-      if (oldEBands[i] < -QCONST16(7.f,8))
-         oldEBands[i] = -QCONST16(7.f,8);
+      if (oldEBands[i] < -QCONST16(7.f,DB_SHIFT))
+         oldEBands[i] = -QCONST16(7.f,DB_SHIFT);
    }
 }
--- a/libcelt/quant_bands.h
+++ b/libcelt/quant_bands.h
@@ -46,7 +46,7 @@
 
 static inline celt_word32 log2Amp(celt_word16 lg)
 {
-	return PSHR32(celt_exp2(SHL16(lg,3)),4);
+	return PSHR32(celt_exp2(SHL16(lg,11-DB_SHIFT)),4);
 }
 
 int *quant_prob_alloc(const CELTMode *m);