shithub: opus

Download patch

ref: a4833ffadaee1d293f230eb9e5fa5308128f0db8
parent: 021478e25218b6c0fc62a1705eea6ec510293be4
author: Jean-Marc Valin <[email protected]>
date: Thu Jan 10 10:34:00 EST 2008

Stereo decoding working again (fixed a few issues in the encoder at the same
time)

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -107,7 +107,7 @@
             = sin(.5*M_PI* sin(.5*M_PI*(i+.5)/st->overlap) * sin(.5*M_PI*(i+.5)/st->overlap));
    for (i=0;i<2*N4;i++)
       st->window[N-N4+i] = 1;
-   st->oldBandE = celt_alloc(mode->nbEBands*sizeof(float));
+   st->oldBandE = celt_alloc(C*mode->nbEBands*sizeof(float));
 
    st->preemph = 0.8;
    st->preemph_memE = celt_alloc(C*sizeof(float));;
@@ -318,7 +318,6 @@
    time_dct(X, N, B, C);
    time_dct(P, N, B, C);
 
-
    quant_energy(st->mode, bandE, st->oldBandE, &st->enc);
 
    /* Pitch prediction */
@@ -447,7 +446,7 @@
    for (i=0;i<2*N4;i++)
       st->window[N-N4+i] = 1;
    
-   st->oldBandE = celt_alloc(mode->nbEBands*sizeof(float));
+   st->oldBandE = celt_alloc(C*mode->nbEBands*sizeof(float));
 
    st->preemph = 0.8;
    st->preemph_memD = celt_alloc(C*sizeof(float));;
@@ -518,7 +517,7 @@
    
    float X[C*B*N];         /**< Interleaved signal MDCTs */
    float P[C*B*N];         /**< Interleaved pitch MDCTs*/
-   float bandE[st->mode->nbEBands];
+   float bandE[st->mode->nbEBands*C];
    float gains[st->mode->nbPBands];
    int pitch_index;
    ec_dec dec;
@@ -543,10 +542,6 @@
    /* Pitch MDCT */
    compute_mdcts(&st->mdct_lookup, st->window, st->out_mem+pitch_index*C, P, N, B, C);
 
-   if (C==2)
-      haar1(P, B*N*C, 1);
-   time_dct(P, N, B, C);
-
    {
       float bandEp[st->mode->nbEBands];
       compute_band_energies(st->mode, P, bandEp);
@@ -553,6 +548,10 @@
       normalise_bands(st->mode, P, bandEp);
    }
 
+   if (C==2)
+      haar1(P, B*N*C, 1);
+   time_dct(P, N, B, C);
+
    /* Get the pitch gains */
    unquant_pitch(gains, st->mode->nbPBands, &dec);
 
@@ -562,12 +561,15 @@
    /* Decode fixed codebook and merge with pitch */
    unquant_bands(st->mode, X, P, &dec);
 
-   /* Synthesis */
-   denormalise_bands(st->mode, X, bandE);
-
    time_idct(X, N, B, C);
    if (C==2)
       haar1(X, B*N*C, 1);
+
+   renormalise_bands(st->mode, X);
+   
+   /* Synthesis */
+   denormalise_bands(st->mode, X, bandE);
+
 
    CELT_MOVE(st->out_mem, st->out_mem+C*B*N, C*(MAX_PERIOD-B*N));
    /* Compute inverse MDCTs */
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -77,9 +77,9 @@
 #define NBANDS256 15
 #define PBANDS256 8
 #define PITCH_END256 88
-const int qbank3[NBANDS256+2] = {0, 4, 8, 12, 16, 24, 32, 40, 48, 56, 72, 88, 104, 126, 168, 232, 256};
+const int qbank3[NBANDS256+2] = {0, 4, 8, 12, 16, 24, 32, 40, 48, 56, 72, 88, 104, 136, 168, 232, 256};
 //const int pbank3[PBANDS256+2] = {0, 8, 16, 24, 40, PITCH_END256, 256};
-const int pbank3[PBANDS256+2] = {0, 4, 8, 12, 16, 24, 40, 56, PITCH_END128, 128};
+const int pbank3[PBANDS256+2] = {0, 4, 8, 12, 16, 24, 40, 56, PITCH_END256, 256};
 
 const CELTMode mode0 = {
    128,         /**< overlap */
--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -35,6 +35,38 @@
 #include <math.h>
 #include "os_support.h"
 
+static void quant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands, ec_enc *enc)
+{
+   int i;
+   float prev = 0;
+   for (i=0;i<m->nbEBands;i++)
+   {
+      int qi;
+      float q;
+      float res;
+      float x;
+      float pred = m->ePredCoef*oldEBands[i]+m->eMeans[i];
+      
+      x = 20*log10(.3+eBands[i]);
+      res = .25f*(i+3.f);
+      //res = 1;
+      qi = (int)floor(.5+(x-pred-prev)/res);
+      ec_laplace_encode(enc, qi, m->eDecay[i]);
+      q = qi*res;
+      
+      //printf("%d ", qi);
+      //printf("%f %f ", pred+prev+q, x);
+      //printf("%f ", x-pred);
+      
+      oldEBands[i] = pred+prev+q;
+      eBands[i] = pow(10, .05*oldEBands[i])-.3;
+      if (eBands[i] < 0)
+         eBands[i] = 0;
+      prev = (prev + .5*q);
+   }
+   //printf ("\n");
+}
+
 void quant_energy(const CELTMode *m, float *eBands, float *oldEBands, ec_enc *enc)
 {
    int C;
@@ -43,8 +75,24 @@
 
    if (C==1)
       quant_energy_mono(m, eBands, oldEBands, enc);
-   else if (C==2)
+   else 
+#if 1
    {
+      int c;
+      for (c=0;c<C;c++)
+      {
+         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];
+      }
+   }
+#else
+      if (C==2)
+   {
       int i;
       int NB = m->nbEBands;
       float mid[NB];
@@ -75,9 +123,11 @@
    } else {
       celt_fatal("more than 2 channels not supported");
    }
+#endif
 }
 
-void quant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands, ec_enc *enc)
+
+static void unquant_energy_mono(const CELTMode *m, float *eBands, float *oldEBands, ec_dec *dec)
 {
    int i;
    float prev = 0;
@@ -86,19 +136,14 @@
       int qi;
       float q;
       float res;
-      float x;
       float pred = m->ePredCoef*oldEBands[i]+m->eMeans[i];
       
-      x = 20*log10(.3+eBands[i]);
       res = .25f*(i+3.f);
-      //res = 1;
-      qi = (int)floor(.5+(x-pred-prev)/res);
-      ec_laplace_encode(enc, qi, m->eDecay[i]);
+      qi = ec_laplace_decode(dec, m->eDecay[i]);
       q = qi*res;
-      
-      //printf("%d ", qi);
       //printf("%f %f ", pred+prev+q, x);
-      //printf("%f ", x-pred);
+      //printf("%d ", qi);
+      //printf("%f ", x-pred-prev);
       
       oldEBands[i] = pred+prev+q;
       eBands[i] = pow(10, .05*oldEBands[i])-.3;
@@ -111,27 +156,20 @@
 
 void unquant_energy(const CELTMode *m, float *eBands, float *oldEBands, ec_dec *dec)
 {
-   int i;
-   float prev = 0;
-   for (i=0;i<m->nbEBands;i++)
-   {
-      int qi;
-      float q;
-      float res;
-      float pred = m->ePredCoef*oldEBands[i]+m->eMeans[i];
-      
-      res = .25f*(i+3.f);
-      qi = ec_laplace_decode(dec, m->eDecay[i]);
-      q = qi*res;
-      //printf("%f %f ", pred+prev+q, x);
-      //printf("%d ", qi);
-      //printf("%f ", x-pred-prev);
-      
-      oldEBands[i] = pred+prev+q;
-      eBands[i] = pow(10, .05*oldEBands[i])-.3;
-      if (eBands[i] < 0)
-         eBands[i] = 0;
-      prev = (prev + .5*q);
+   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;
+         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];
+      }
    }
-   //printf ("\n");
 }
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -188,8 +188,9 @@
    pulse2comb(N, K, comb, signs, iy[0]); 
    ec_enc_uint64(enc,icwrs64(N, K, comb, signs),ncwrs64(N, K));
    
-   /* Recompute the gain in one pass (to reduce errors) */
-   if (0) {
+   /* Recompute the gain in one pass to reduce the encoder-decoder mismatch
+      due to the recursive computation used in quantisation */
+   if (1) {
       float Ryp=0;
       float Rpp=0;
       float Ryy=0;
@@ -203,7 +204,6 @@
       for (i=0;i<N;i++)
          y[0][i] = iy[0][i] - alpha*Ryp*p[i];
       
-      /* Recompute after the projection (I think it's right) */
       Ryp = 0;
       for (i=0;i<N;i++)
          Ryp += y[0][i]*p[i];