shithub: opus

Download patch

ref: 1520f8924e717fb621ee1e3ac04cc37d37891593
parent: 80e56f1a2e1065f29587a2c338a5a05b3419e520
author: Jean-Marc Valin <[email protected]>
date: Tue May 11 19:38:23 EDT 2010

Stereo quantization also made more generic

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -447,12 +447,138 @@
    return *last_decision;
 }
 
-void quant_band(const CELTMode *m, int i, celt_norm *X, int N, int bits, int spread, celt_norm *lowband, int resynth, ec_enc *enc, celt_int32 *remaining_bits, int LM)
+void quant_band(const CELTMode *m, int i, celt_norm *X, celt_norm *Y, int N, int b, int spread, celt_norm *lowband, int resynth, ec_enc *enc, celt_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE)
 {
    int q;
    int curr_bits;
+   int stereo;
+   int imid=0, iside=0;
 
-   q = bits2pulses(m, m->bits[LM][i], N, bits);
+   stereo = Y != NULL;
+
+   if (stereo)
+   {
+      int qb;
+      int itheta;
+      int mbits, sbits, delta;
+      int qalloc;
+      celt_word16 mid, side;
+      qb = (b-2*(N-1)*(QTHETA_OFFSET-m->logN[i]-(LM<<BITRES)))/(32*(N-1));
+      if (qb > (b>>BITRES)-1)
+         qb = (b>>BITRES)-1;
+      if (qb<0)
+         qb = 0;
+      if (qb>14)
+         qb = 14;
+
+      stereo_band_mix(m, X, Y, bandE, qb==0, i, 1, 1<<LM);
+
+      mid = renormalise_vector(X, Q15ONE, N, 1);
+      side = renormalise_vector(Y, Q15ONE, N, 1);
+      /* 0.63662 = 2/pi */
+#ifdef FIXED_POINT
+      itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
+#else
+      itheta = floor(.5f+16384*0.63662f*atan2(side,mid));
+#endif
+      qalloc = log2_frac((1<<qb)+1,BITRES);
+      if (qb==0)
+      {
+         itheta=0;
+      } else {
+         int shift;
+         shift = 14-qb;
+         itheta = (itheta+(1<<shift>>1))>>shift;
+         ec_enc_uint(enc, itheta, (1<<qb)+1);
+         itheta <<= shift;
+      }
+      if (itheta == 0)
+      {
+         imid = 32767;
+         iside = 0;
+         delta = -10000;
+      } else if (itheta == 16384)
+      {
+         imid = 0;
+         iside = 32767;
+         delta = 10000;
+      } else {
+         imid = bitexact_cos(itheta);
+         iside = bitexact_cos(16384-itheta);
+         delta = (N-1)*(log2_frac(iside,BITRES+2)-log2_frac(imid,BITRES+2))>>2;
+      }
+#if 1
+      if (N==2)
+      {
+         int c, c2;
+         int sign=1;
+         celt_norm v[2], w[2];
+         celt_norm *x2, *y2;
+         mbits = b-qalloc;
+         sbits = 0;
+         if (itheta != 0 && itheta != 16384)
+            sbits = 1<<BITRES;
+         mbits -= sbits;
+         c = itheta > 8192 ? 1 : 0;
+         c2 = 1-c;
+
+         x2 = X;
+         y2 = Y;
+         if (c==0)
+         {
+            v[0] = x2[0];
+            v[1] = x2[1];
+            w[0] = y2[0];
+            w[1] = y2[1];
+         } else {
+            v[0] = y2[0];
+            v[1] = y2[1];
+            w[0] = x2[0];
+            w[1] = x2[1];
+         }
+         *remaining_bits -= qalloc+sbits;
+         quant_band(m, i, v, NULL, N, mbits, spread, lowband, resynth, enc, remaining_bits, LM, lowband_out, NULL);
+         if (sbits)
+         {
+            if (v[0]*w[1] - v[1]*w[0] > 0)
+               sign = 1;
+            else
+               sign = -1;
+            ec_enc_bits(enc, sign==1, 1);
+         } else {
+            sign = 1;
+         }
+         w[0] = -sign*v[1];
+         w[1] = sign*v[0];
+         if (c==0)
+         {
+            x2[0] = v[0];
+            x2[1] = v[1];
+            y2[0] = w[0];
+            y2[1] = w[1];
+         } else {
+            x2[0] = w[0];
+            x2[1] = w[1];
+            y2[0] = v[0];
+            y2[1] = v[1];
+         }
+      } else
+#endif
+      {
+
+         mbits = (b-qalloc/2-delta)/2;
+         if (mbits > b-qalloc)
+            mbits = b-qalloc;
+         if (mbits<0)
+            mbits=0;
+         sbits = b-qalloc-mbits;
+         *remaining_bits -= qalloc;
+         quant_band(m, i, X, NULL, N, mbits, spread, lowband, resynth, enc, remaining_bits, LM, lowband_out, NULL);
+         quant_band(m, i, Y, NULL, N, sbits, spread, NULL, resynth, enc, remaining_bits, LM, NULL, NULL);
+      }
+
+   } else {
+   q = bits2pulses(m, m->bits[LM][i], N, b);
    curr_bits = pulses2bits(m->bits[LM][i], N, q);
    *remaining_bits -= curr_bits;
    while (*remaining_bits < 0 && q > 0)
@@ -463,6 +589,34 @@
       *remaining_bits -= curr_bits;
    }
    alg_quant(X, N, q, spread, lowband, resynth, enc);
+   }
+
+   if (resynth && lowband_out)
+   {
+      int j;
+      celt_word16 n;
+      n = celt_sqrt(SHL32(EXTEND32(N),22));
+      for (j=0;j<N;j++)
+         lowband_out[j] = MULT16_16_Q15(n,X[j]);
+   }
+
+   if (stereo && resynth)
+   {
+      int j;
+      celt_word16 mid, side;
+#ifdef FIXED_POINT
+      mid = imid;
+      side = iside;
+#else
+      mid = (1.f/32768)*imid;
+      side = (1.f/32768)*iside;
+#endif
+      for (j=0;j<N;j++)
+         X[j] = MULT16_16_Q15(X[j], mid);
+      for (j=0;j<N;j++)
+         Y[j] = MULT16_16_Q15(Y[j], side);
+
+   }
 }
 
 void unquant_band(const CELTMode *m, int i, celt_norm *X, celt_norm *Y, int N, int b,
@@ -608,7 +762,7 @@
 /* Quantisation of the residual */
 void quant_all_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int resynth, int total_bits, int encode, void *enc, int LM)
 {
-   int i, j, remaining_bits, balance;
+   int i, remaining_bits, balance;
    const celt_int16 * restrict eBands = m->eBands;
    celt_norm * restrict norm;
    VARDECL(celt_norm, _norm);
@@ -644,16 +798,9 @@
          curr_balance = 3;
       curr_balance = balance / curr_balance;
 
-      quant_band(m, i, X+M*eBands[i], N, pulses[i]+curr_balance, spread, norm+M*eBands[start], resynth, enc, &remaining_bits, LM);
+      quant_band(m, i, X+M*eBands[i], NULL, N, pulses[i]+curr_balance, spread, norm+M*eBands[start], resynth, enc, &remaining_bits, LM, norm+M*eBands[i], NULL);
 
       balance += pulses[i] + tell;
-      if (resynth)
-      {
-         celt_word16 n;
-         n = celt_sqrt(SHL32(EXTEND32(N),22));
-         for (j=M*eBands[i];j<M*eBands[i+1];j++)
-            norm[j] = MULT16_16_Q15(n,X[j]);
-      }
    }
    RESTORE_STACK;
 }
@@ -708,12 +855,11 @@
 
 void quant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int resynth, int total_bits, ec_enc *enc, int LM)
 {
-   int i, j, remaining_bits, balance;
+   int i, remaining_bits, balance;
    const celt_int16 * restrict eBands = m->eBands;
    celt_norm * restrict norm;
    VARDECL(celt_norm, _norm);
    int B;
-   celt_word16 mid, side;
    int M;
    int spread;
    SAVE_STACK;
@@ -731,12 +877,9 @@
    for (i=start;i<m->nbEBands;i++)
    {
       int tell;
-      int b, qb;
+      int b;
       int N;
       int curr_balance;
-      int imid, iside, itheta;
-      int mbits, sbits, delta;
-      int qalloc;
       celt_norm * restrict X, * restrict Y;
       
       X = _X+M*eBands[i];
@@ -755,141 +898,12 @@
       if (b<0)
          b = 0;
 
-      qb = (b-2*(N-1)*(QTHETA_OFFSET-m->logN[i]-(LM<<BITRES)))/(32*(N-1));
-      if (qb > (b>>BITRES)-1)
-         qb = (b>>BITRES)-1;
-      if (qb<0)
-         qb = 0;
-      if (qb>14)
-         qb = 14;
+      quant_band(m, i, X, Y, N, b, spread, norm+M*eBands[start], resynth, enc, &remaining_bits, LM, norm+M*eBands[i], bandE);
 
-      stereo_band_mix(m, X, Y, bandE, qb==0, i, 1, M);
-
-      mid = renormalise_vector(X, Q15ONE, N, 1);
-      side = renormalise_vector(Y, Q15ONE, N, 1);
-      /* 0.63662 = 2/pi */
-#ifdef FIXED_POINT
-      itheta = MULT16_16_Q15(QCONST16(0.63662f,15),celt_atan2p(side, mid));
-#else
-      itheta = floor(.5f+16384*0.63662f*atan2(side,mid));
-#endif
-      qalloc = log2_frac((1<<qb)+1,BITRES);
-      if (qb==0)
-      {
-         itheta=0;
-      } else {
-         int shift;
-         shift = 14-qb;
-         itheta = (itheta+(1<<shift>>1))>>shift;
-         ec_enc_uint(enc, itheta, (1<<qb)+1);
-         itheta <<= shift;
-      }
-      if (itheta == 0)
-      {
-         imid = 32767;
-         iside = 0;
-         delta = -10000;
-      } else if (itheta == 16384)
-      {
-         imid = 0;
-         iside = 32767;
-         delta = 10000;
-      } else {
-         imid = bitexact_cos(itheta);
-         iside = bitexact_cos(16384-itheta);
-         delta = (N-1)*(log2_frac(iside,BITRES+2)-log2_frac(imid,BITRES+2))>>2;
-      }
-#if 1
-      if (N==2)
-      {
-         int c, c2;
-         int sign=1;
-         celt_norm v[2], w[2];
-         celt_norm *x2, *y2;
-         mbits = b-qalloc;
-         sbits = 0;
-         if (itheta != 0 && itheta != 16384)
-            sbits = 1<<BITRES;
-         mbits -= sbits;
-         c = itheta > 8192 ? 1 : 0;
-         c2 = 1-c;
-
-         x2 = X;
-         y2 = Y;
-         if (c==0)
-         {
-            v[0] = x2[0];
-            v[1] = x2[1];
-            w[0] = y2[0];
-            w[1] = y2[1];
-         } else {
-            v[0] = y2[0];
-            v[1] = y2[1];
-            w[0] = x2[0];
-            w[1] = x2[1];
-         }
-         remaining_bits -= qalloc+sbits;
-         quant_band(m, i, v, N, mbits, spread, norm+M*eBands[start], resynth, enc, &remaining_bits, LM);
-         if (sbits)
-         {
-            if (v[0]*w[1] - v[1]*w[0] > 0)
-               sign = 1;
-            else
-               sign = -1;
-            ec_enc_bits(enc, sign==1, 1);
-         } else {
-            sign = 1;
-         }
-         w[0] = -sign*v[1];
-         w[1] = sign*v[0];
-         if (c==0)
-         {
-            x2[0] = v[0];
-            x2[1] = v[1];
-            y2[0] = w[0];
-            y2[1] = w[1];
-         } else {
-            x2[0] = w[0];
-            x2[1] = w[1];
-            y2[0] = v[0];
-            y2[1] = v[1];
-         }
-      } else 
-#endif
-      {
-         
-         mbits = (b-qalloc/2-delta)/2;
-         if (mbits > b-qalloc)
-            mbits = b-qalloc;
-         if (mbits<0)
-            mbits=0;
-         sbits = b-qalloc-mbits;
-         remaining_bits -= qalloc;
-         quant_band(m, i, X, N, mbits, spread, norm+M*eBands[start], resynth, enc, &remaining_bits, LM);
-         quant_band(m, i, Y, N, sbits, spread, NULL, resynth, enc, &remaining_bits, LM);
-      }
-      
       balance += pulses[i] + tell;
 
       if (resynth)
       {
-         celt_word16 n;
-#ifdef FIXED_POINT
-         mid = imid;
-         side = iside;
-#else
-         mid = (1.f/32768)*imid;
-         side = (1.f/32768)*iside;
-#endif
-         n = celt_sqrt(SHL32(EXTEND32(N),22));
-         for (j=0;j<N;j++)
-            norm[M*eBands[i]+j] = MULT16_16_Q15(n,X[j]);
-
-         for (j=0;j<N;j++)
-            X[j] = MULT16_16_Q15(X[j], mid);
-         for (j=0;j<N;j++)
-            Y[j] = MULT16_16_Q15(Y[j], side);
-
          stereo_band_mix(m, X, Y, bandE, 0, i, -1, M);
          renormalise_vector(X, Q15ONE, N, 1);
          renormalise_vector(Y, Q15ONE, N, 1);