shithub: opus

Download patch

ref: 45018cbfa263a1bd1c44f445c0f63de2c122c080
parent: a2482aceb4e0d63ec0bfc01e97624cef86180577
author: Jean-Marc Valin <[email protected]>
date: Fri Jan 11 09:02:07 EST 2008

Doing stereo mixing adaptively based on amplitude difference.

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -298,3 +298,32 @@
    for (i=B*eBands[m->nbEBands];i<B*eBands[m->nbEBands+1];i++)
       X[i] = 0;
 }
+
+void stereo_mix(const CELTMode *m, float *X, float *bank, int dir)
+{
+   int i, B, C;
+   const int *eBands = m->eBands;
+   B = m->nbMdctBlocks;
+   C = m->nbChannels;
+   for (i=0;i<m->nbEBands;i++)
+   {
+      int j;
+      float left, right;
+      float a1, a2;
+      left = bank[i*C];
+      right = bank[i*C+1];
+      a1 = left/sqrt(.01+left*left+right*right);
+      a2 = dir*right/sqrt(.01+left*left+right*right);
+      for (j=B*eBands[i];j<B*eBands[i+1];j++)
+      {
+         float r, l;
+         l = X[j*C];
+         r = X[j*C+1];         
+         X[j*C] = a1*l + a2*r;
+         X[j*C+1] = a1*r - a2*l;
+      }
+   }
+   for (i=B*C*eBands[m->nbEBands];i<B*C*eBands[m->nbEBands+1];i++)
+      X[i] = 0;
+
+}
\ No newline at end of file
--- a/libcelt/bands.h
+++ b/libcelt/bands.h
@@ -89,4 +89,6 @@
 */
 void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec);
 
+void stereo_mix(const CELTMode *m, float *X, float *bank, int dir);
+
 #endif /* BANDS_H */
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -309,16 +309,22 @@
       normalise_bands(st->mode, P, bandEp);
    }
 
+   quant_energy(st->mode, bandE, st->oldBandE, &st->enc);
+
    if (C==2)
    {
-      haar1(X, B*N*C, 1);
-      haar1(P, B*N*C, 1);
+      stereo_mix(st->mode, X, bandE, 1);
+      stereo_mix(st->mode, P, bandE, 1);
+      //haar1(X, B*N*C, 1);
+      //haar1(P, B*N*C, 1);
    }
+   /* Simulates intensity stereo */
+   //for (i=30;i<N*B;i++)
+   //   X[i*C+1] = P[i*C+1] = 0;
    /* Get a tiny bit more frequency resolution and prevent unstable energy when quantising */
    time_dct(X, N, B, C);
    time_dct(P, N, B, C);
 
-   quant_energy(st->mode, bandE, st->oldBandE, &st->enc);
 
    /* Pitch prediction */
    compute_pitch_gain(st->mode, X, P, gains, bandE);
@@ -339,7 +345,8 @@
    
    time_idct(X, N, B, C);
    if (C==2)
-      haar1(X, B*N*C, 1);
+      //haar1(X, B*N*C, 1);
+      stereo_mix(st->mode, X, bandE, -1);
 
    renormalise_bands(st->mode, X);
    /* Synthesis */
@@ -549,7 +556,8 @@
    }
 
    if (C==2)
-      haar1(P, B*N*C, 1);
+      //haar1(P, B*N*C, 1);
+      stereo_mix(st->mode, P, bandE, 1);
    time_dct(P, N, B, C);
 
    /* Get the pitch gains */
@@ -563,7 +571,8 @@
 
    time_idct(X, N, B, C);
    if (C==2)
-      haar1(X, B*N*C, 1);
+      //haar1(X, B*N*C, 1);
+      stereo_mix(st->mode, X, bandE, -1);
 
    renormalise_bands(st->mode, X);