Some work towards being able to encode a 48 kHz stream from 32 kHz audio (incomplete)
[opus.git] / libcelt / quant_bands.c
index c687a26..af2b7e1 100644 (file)
@@ -1,5 +1,6 @@
-/* (C) 2007 Jean-Marc Valin, CSIRO
-*/
+/* Copyright (c) 2007-2008 CSIRO
+   Copyright (c) 2007-2009 Xiph.Org Foundation
+   Written by Jean-Marc Valin */
 /*
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include "quant_bands.h"
 #include "laplace.h"
 #include <math.h>
 #include "os_support.h"
+#include "arch.h"
+#include "mathops.h"
+#include "stack_alloc.h"
 
-const float eMeans[24] = {45.f, -8.f, -12.f, -2.5f, 1.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
+#define E_MEANS_SIZE (5)
 
-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};
+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)
+{
+   int i;
+   celt_word32 dist = 0;
+   for (i=0;i<len;i++)
+   {
+      celt_word16 d = SUB16(eBands[i], oldEBands[i]);
+      dist = MAC16_16(dist, d,d);
+   }
+   return SHR32(dist,2*DB_SHIFT) > 2*len;
+}
 
-static void quant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands, ec_enc *enc)
+int *quant_prob_alloc(const CELTMode *m)
 {
    int i;
-   float prev = 0;
-   float coef = m->ePredCoef;
-   float error[m->nbEBands];
-   /* The .7 is a heuristic */
-   float beta = .7*coef;
+   int *prob;
+   prob = celt_alloc(4*m->nbEBands*sizeof(int));
+   if (prob==NULL)
+     return NULL;
    for (i=0;i<m->nbEBands;i++)
    {
-      int qi;
-      float q;
-      float res;
-      float x;
-      float f;
-      float mean = (1-coef)*eMeans[i];
-      x = 20*log10(.3+eBands[i]);
-      res = 6.;
-      //res = 1;
-      f = (x-mean-coef*oldEBands[i]-prev)/res;
-      qi = (int)floor(.5+f);
-      //if (i> 4 && qi<-2)
-      //   qi = -2;
-      //ec_laplace_encode(enc, qi, i==0?11192:6192);
-      //ec_laplace_encode(enc, qi, 8500-i*200);
-      ec_laplace_encode(enc, qi, 6000-i*200);
-      q = qi*res;
-      error[i] = f - qi;
-      
-      //printf("%d ", qi);
-      //printf("%f %f ", pred+prev+q, x);
-      //printf("%f ", x-pred);
-      
-      oldEBands[i] = mean+coef*oldEBands[i]+prev+q;
-      
-      prev = mean+prev+(1-beta)*q;
+      prob[2*i] = 6000-i*200;
+      prob[2*i+1] = ec_laplace_get_start_freq(prob[2*i]);
    }
    for (i=0;i<m->nbEBands;i++)
    {
-      int q2;
-      float offset = (error[i]+.5)*frac[i];
-      q2 = (int)floor(offset);
-      if (q2 > frac[i]-1)
-         q2 = frac[i]-1;
-      ec_enc_uint(enc, q2, frac[i]);
-      offset = ((q2+.5)/frac[i])-.5;
-      oldEBands[i] += 6.*offset;
-      //printf ("%f ", error[i] - offset);
-      eBands[i] = pow(10, .05*oldEBands[i])-.3;
-      if (eBands[i] < 0)
-         eBands[i] = 0;
+      prob[2*m->nbEBands+2*i] = 9000-i*240;
+      prob[2*m->nbEBands+2*i+1] = ec_laplace_get_start_freq(prob[2*m->nbEBands+2*i]);
    }
-   //printf ("%d\n", ec_enc_tell(enc, 0)-9);
+   return prob;
+}
 
-   //printf ("\n");
+void quant_prob_free(int *freq)
+{
+   celt_free(freq);
 }
 
-static void unquant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands, ec_dec *dec)
+unsigned quant_coarse_energy(const CELTMode *m, int start, int end, const celt_word16 *eBands, celt_word16 *oldEBands, int budget, int intra, int *prob, celt_word16 *error, ec_enc *enc, int _C, celt_word16 max_decay)
 {
-   int i;
-   float prev = 0;
-   float coef = m->ePredCoef;
-   float error[m->nbEBands];
-   /* The .7 is a heuristic */
-   float beta = .7*coef;
-   for (i=0;i<m->nbEBands;i++)
+   int i, c;
+   unsigned bits_used = 0;
+   celt_word32 prev[2] = {0,0};
+   celt_word16 coef = m->ePredCoef;
+   celt_word16 beta;
+   const int C = CHANNELS(_C);
+
+   if (intra)
    {
-      int qi;
-      float q;
-      float res;
-      float mean = (1-coef)*eMeans[i];
-      res = 6.;
-      qi = ec_laplace_decode(dec, 6000-i*200);
-      q = qi*res;
-      
-      oldEBands[i] = mean+coef*oldEBands[i]+prev+q;
-      
-      prev = mean+prev+(1-beta)*q;
+      coef = 0;
+      prob += 2*m->nbEBands;
    }
-   for (i=0;i<m->nbEBands;i++)
+   /* The .8 is a heuristic */
+   beta = MULT16_16_P15(QCONST16(.8f,15),coef);
+
+   /* Encode at a fixed coarse resolution */
+   for (i=start;i<end;i++)
    {
-      int q2;
-      float offset;
-      q2 = ec_dec_uint(dec, frac[i]);
-      offset = ((q2+.5)/frac[i])-.5;
-      oldEBands[i] += 6.*offset;
-      //printf ("%f ", error[i] - offset);
-      eBands[i] = pow(10, .05*oldEBands[i])-.3;
-      if (eBands[i] < 0)
-         eBands[i] = 0;
+      c=0;
+      do {
+         int qi;
+         celt_word16 q;
+         celt_word16 x;
+         celt_word32 f;
+         celt_word32 mean =  (i-start < E_MEANS_SIZE) ? SUB32(SHL32(EXTEND32(eMeans[i-start]),15), MULT16_16(coef,eMeans[i-start])) : 0;
+         x = eBands[i+c*m->nbEBands];
+#ifdef FIXED_POINT
+         f = SHL32(EXTEND32(x),15)-mean -MULT16_16(coef,oldEBands[i+c*m->nbEBands])-prev[c];
+         /* Rounding to nearest integer here is really important! */
+         qi = (f+QCONST32(.5,DB_SHIFT+15))>>(DB_SHIFT+15);
+#else
+         f = x-mean-coef*oldEBands[i+c*m->nbEBands]-prev[c];
+         /* Rounding to nearest integer here is really important! */
+         qi = (int)floor(.5f+f);
+#endif
+         if (qi < 0 && x < oldEBands[i+c*m->nbEBands]-max_decay)
+         {
+            qi += SHR16(oldEBands[i+c*m->nbEBands]-max_decay-x, DB_SHIFT);
+            if (qi > 0)
+               qi = 0;
+         }
+         /* If we don't have enough bits to encode all the energy, just assume something safe.
+            We allow slightly busting the budget here */
+         bits_used=ec_enc_tell(enc, 0);
+         if (bits_used > budget)
+         {
+            qi = -1;
+            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] = PSHR32(f,15) - SHL16(qi,DB_SHIFT);
+         }
+         q = SHL16(qi,DB_SHIFT);
+         
+         oldEBands[i+c*m->nbEBands] = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]) + mean + prev[c] + SHL32(EXTEND32(q),15), 15);
+         prev[c] = mean + prev[c] + SHL32(EXTEND32(q),15) - MULT16_16(beta,q);
+      } while (++c < C);
    }
-   //printf ("\n");
+   return bits_used;
 }
 
+void quant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, celt_word16 *error, int *fine_quant, ec_enc *enc, int _C)
+{
+   int i, c;
+   const int C = CHANNELS(_C);
 
+   /* Encode finer resolution */
+   for (i=start;i<end;i++)
+   {
+      celt_int16 frac = 1<<fine_quant[i];
+      if (fine_quant[i] <= 0)
+         continue;
+      c=0;
+      do {
+         int q2;
+         celt_word16 offset;
+#ifdef FIXED_POINT
+         /* Has to be without rounding */
+         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
+         if (q2 > frac-1)
+            q2 = frac-1;
+         if (q2<0)
+            q2 = 0;
+         ec_enc_bits(enc, q2, fine_quant[i]);
+#ifdef FIXED_POINT
+         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
+         oldEBands[i+c*m->nbEBands] += offset;
+         error[i+c*m->nbEBands] -= offset;
+         /*printf ("%f ", error[i] - offset);*/
+      } while (++c < C);
+   }
+}
 
-void quant_energy(const CELTMode *m, float *eBands, float *oldEBands, ec_enc *enc)
+void quant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, celt_word16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int _C)
 {
-   int C;
-   
-   C = m->nbChannels;
+   int i, prio, c;
+   const int C = CHANNELS(_C);
 
-   if (C==1)
-      quant_energy_mono(m, eBands, oldEBands, enc);
-   else 
-#if 1
+   /* Use up the remaining bits */
+   for (prio=0;prio<2;prio++)
    {
-      int c;
-      for (c=0;c<C;c++)
+      for (i=start;i<end && bits_left>=C ;i++)
       {
-         int i;
-         float E[m->nbEBands];
-         for (i=0;i<m->nbEBands;i++)
-            E[i] = eBands[C*i+c];
-         quant_energy_mono(m, E, oldEBands+c*m->nbEBands, enc);
-         for (i=0;i<m->nbEBands;i++)
-            eBands[C*i+c] = E[i];
-      }
-   }
+         if (fine_quant[i] >= 7 || fine_priority[i]!=prio)
+            continue;
+         c=0;
+         do {
+            int q2;
+            celt_word16 offset;
+            q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
+            ec_enc_bits(enc, q2, 1);
+#ifdef FIXED_POINT
+            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5,DB_SHIFT),fine_quant[i]+1);
 #else
-      if (C==2)
-   {
-      int i;
-      int NB = m->nbEBands;
-      float mid[NB];
-      float side[NB];
-      float left;
-      float right;
-      for (i=0;i<NB;i++)
-      {
-         //left = eBands[C*i];
-         //right = eBands[C*i+1];
-         mid[i] = sqrt(eBands[C*i]*eBands[C*i] + eBands[C*i+1]*eBands[C*i+1]);
-         side[i] = 20*log10((eBands[2*i]+.3)/(eBands[2*i+1]+.3));
-         //printf ("%f %f ", mid[i], side[i]);
+            offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
+#endif
+            oldEBands[i+c*m->nbEBands] += offset;
+            bits_left--;
+         } while (++c < C);
       }
-      //printf ("\n");
-      quant_energy_mono(m, mid, oldEBands, enc);
-      for (i=0;i<NB;i++)
-         side[i] = pow(10.f,floor(.5f+side[i])/10.f);
-         
-      //quant_energy_side(m, side, oldEBands+NB, enc);
-      for (i=0;i<NB;i++)
+   }
+   c=0;
+   do {
+      for (i=start;i<m->nbEBands;i++)
       {
-         eBands[C*i] = mid[i]*sqrt(side[i]/(1.f+side[i]));
-         eBands[C*i+1] = mid[i]*sqrt(1.f/(1.f+side[i]));
-         //printf ("%f %f ", mid[i], side[i]);
+         eBands[i+c*m->nbEBands] = log2Amp(oldEBands[i+c*m->nbEBands]);
+         if (oldEBands[i+c*m->nbEBands] < -QCONST16(7.f,DB_SHIFT))
+            oldEBands[i+c*m->nbEBands] = -QCONST16(7.f,DB_SHIFT);
       }
+   } while (++c < C);
+}
 
-   } else {
-      celt_fatal("more than 2 channels not supported");
+void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, int budget, int intra, int *prob, ec_dec *dec, int _C)
+{
+   int i, c;
+   celt_word32 prev[2] = {0, 0};
+   celt_word16 coef = m->ePredCoef;
+   celt_word16 beta;
+   const int C = CHANNELS(_C);
+
+   if (intra)
+   {
+      coef = 0;
+      prob += 2*m->nbEBands;
    }
-#endif
-}
+   /* The .8 is a heuristic */
+   beta = MULT16_16_P15(QCONST16(.8f,15),coef);
+
+   /* Decode at a fixed coarse resolution */
+   for (i=start;i<end;i++)
+   {
+      c=0;
+      do {
+         int qi;
+         celt_word16 q;
+         celt_word32 mean =  (i-start < E_MEANS_SIZE) ? SUB32(SHL32(EXTEND32(eMeans[i-start]),15), MULT16_16(coef,eMeans[i-start])) : 0;
+         /* If we didn't have enough bits to encode all the energy, just assume something safe.
+            We allow slightly busting the budget here */
+         if (ec_dec_tell(dec, 0) > budget)
+            qi = -1;
+         else
+            qi = ec_laplace_decode_start(dec, prob[2*i], prob[2*i+1]);
+         q = SHL16(qi,DB_SHIFT);
 
+         oldEBands[i+c*m->nbEBands] = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]) + mean + prev[c] + SHL32(EXTEND32(q),15), 15);
+         prev[c] = mean + prev[c] + SHL32(EXTEND32(q),15) - MULT16_16(beta,q);
+      } while (++c < C);
+   }
+}
 
+void unquant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, int *fine_quant, ec_dec *dec, int _C)
+{
+   int i, c;
+   const int C = CHANNELS(_C);
+   /* Decode finer resolution */
+   for (i=start;i<end;i++)
+   {
+      if (fine_quant[i] <= 0)
+         continue;
+      c=0; 
+      do {
+         int q2;
+         celt_word16 offset;
+         q2 = ec_dec_bits(dec, fine_quant[i]);
+#ifdef FIXED_POINT
+         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
+         oldEBands[i+c*m->nbEBands] += offset;
+      } while (++c < C);
+   }
+}
 
-void unquant_energy(const CELTMode *m, float *eBands, float *oldEBands, ec_dec *dec)
+void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec, int _C)
 {
-   int C;   
-   C = m->nbChannels;
-
-   if (C==1)
-      unquant_energy_mono(m, eBands, oldEBands, dec);
-   else {
-      int c;
-      for (c=0;c<C;c++)
+   int i, prio, c;
+   const int C = CHANNELS(_C);
+
+   /* Use up the remaining bits */
+   for (prio=0;prio<2;prio++)
+   {
+      for (i=start;i<end && bits_left>=C ;i++)
       {
-         int i;
-         float E[m->nbEBands];
-         unquant_energy_mono(m, E, oldEBands+c*m->nbEBands, dec);
-         for (i=0;i<m->nbEBands;i++)
-            eBands[C*i+c] = E[i];
+         if (fine_quant[i] >= 7 || fine_priority[i]!=prio)
+            continue;
+         c=0;
+         do {
+            int q2;
+            celt_word16 offset;
+            q2 = ec_dec_bits(dec, 1);
+#ifdef FIXED_POINT
+            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
+            oldEBands[i+c*m->nbEBands] += offset;
+            bits_left--;
+         } while (++c < C);
       }
    }
+   c=0;
+   do {
+      for (i=start;i<m->nbEBands;i++)
+      {
+         eBands[i+c*m->nbEBands] = log2Amp(oldEBands[i+c*m->nbEBands]);
+         if (oldEBands[i+c*m->nbEBands] < -QCONST16(7.f,DB_SHIFT))
+            oldEBands[i+c*m->nbEBands] = -QCONST16(7.f,DB_SHIFT);
+      }
+   } while (++c < C);
 }