shithub: opus

Download patch

ref: f276b23415bad8a5197133b87aa1a1d1b4b74a1d
parent: ac1da4f42b36e08e2cda9793b50818cfad1ec437
author: Jean-Marc Valin <[email protected]>
date: Sat Jul 24 13:13:17 EDT 2010

Using random noise in upper bands when signal is "normal"

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -485,7 +485,7 @@
    can be called recursively so bands can end up being split in 8 parts. */
 static void quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y,
       int N, int b, int spread, int tf_change, celt_norm *lowband, int resynth, void *ec,
-      celt_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level)
+      celt_int32 *remaining_bits, int LM, celt_norm *lowband_out, const celt_ener *bandE, int level, celt_int32 *seed)
 {
    int q;
    int curr_bits;
@@ -754,7 +754,7 @@
             else
                sign = -1;
          }
-         quant_band(encode, m, i, v, NULL, N, mbits, spread, tf_change, lowband, resynth, ec, remaining_bits, LM, lowband_out, NULL, level+1);
+         quant_band(encode, m, i, v, NULL, N, mbits, spread, tf_change, lowband, resynth, ec, remaining_bits, LM, lowband_out, NULL, level+1, seed);
          if (sbits)
          {
             if (encode)
@@ -806,8 +806,8 @@
          else
             next_level = level+1;
 
-         quant_band(encode, m, i, X, NULL, N, mbits, spread, tf_change, lowband, resynth, ec, remaining_bits, LM, next_lowband_out1, NULL, next_level);
-         quant_band(encode, m, i, Y, NULL, N, sbits, spread, tf_change, next_lowband2, resynth, ec, remaining_bits, LM, NULL, NULL, level);
+         quant_band(encode, m, i, X, NULL, N, mbits, spread, tf_change, lowband, resynth, ec, remaining_bits, LM, next_lowband_out1, NULL, next_level, seed);
+         quant_band(encode, m, i, Y, NULL, N, sbits, spread, tf_change, next_lowband2, resynth, ec, remaining_bits, LM, NULL, NULL, level, seed);
       }
 
    } else {
@@ -826,9 +826,9 @@
       }
 
       if (encode)
-         alg_quant(X, N, q, spread, lowband, resynth, (ec_enc*)ec);
+         alg_quant(X, N, q, spread, lowband, resynth, (ec_enc*)ec, seed);
       else
-         alg_unquant(X, N, q, spread, lowband, (ec_dec*)ec);
+         alg_unquant(X, N, q, spread, lowband, (ec_dec*)ec, seed);
    }
 
    /* This code is used by the decoder and by the resynthesis-enabled encoder */
@@ -909,6 +909,7 @@
    int B;
    int M;
    int spread;
+   celt_int32 seed;
    celt_norm *lowband;
    int update_lowband = 1;
    int C = _Y != NULL ? 2 : 1;
@@ -920,6 +921,10 @@
    ALLOC(_norm, M*eBands[m->nbEBands], celt_norm);
    norm = _norm;
 
+   if (encode)
+      seed = ((ec_enc*)ec)->rng;
+   else
+      seed = ((ec_dec*)ec)->rng;
    balance = 0;
    lowband = NULL;
    for (i=start;i<end;i++)
@@ -930,6 +935,7 @@
       int curr_balance;
       celt_norm * restrict X, * restrict Y;
       int tf_change=0;
+      celt_norm *effective_lowband;
       
       X = _X+M*eBands[i];
       if (_Y!=NULL)
@@ -956,12 +962,8 @@
       if (b > C*16*N<<BITRES)
          b = C*16*N<<BITRES;
 
-      if (M*eBands[i]-N >= M*eBands[start])
-      {
-         if (update_lowband || lowband==NULL)
+      if (M*eBands[i]-N >= M*eBands[start] && (update_lowband || lowband==NULL))
             lowband = norm+M*eBands[i]-N;
-      } else
-         lowband = NULL;
 
       tf_change = tf_res[i];
       if (i>=m->effEBands)
@@ -970,7 +972,12 @@
          if (_Y!=NULL)
             Y = norm;
       }
-      quant_band(encode, m, i, X, Y, N, b, spread, tf_change, lowband, resynth, ec, &remaining_bits, LM, norm+M*eBands[i], bandE, 0);
+
+      if (tf_change==0 && !shortBlocks && fold)
+         effective_lowband = NULL;
+      else
+         effective_lowband = lowband;
+      quant_band(encode, m, i, X, Y, N, b, spread, tf_change, effective_lowband, resynth, ec, &remaining_bits, LM, norm+M*eBands[i], bandE, 0, &seed);
 
       balance += pulses[i] + tell;
 
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -45,6 +45,10 @@
 #define M_PI 3.141592653
 #endif
 
+static celt_uint32 lcg_rand(celt_uint32 seed)
+{
+   return 1664525 * seed + 1013904223;
+}
 
 static void exp_rotation1(celt_norm *X, int len, int dir, int stride, celt_word16 c, celt_word16 s)
 {
@@ -150,7 +154,7 @@
    while (++i < N);
 }
 
-void alg_quant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, int resynth, ec_enc *enc)
+void alg_quant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, int resynth, ec_enc *enc, celt_int32 *seed)
 {
    VARDECL(celt_norm, y);
    VARDECL(int, iy);
@@ -172,12 +176,15 @@
       {
          for (j=0;j<N;j++)
             X[j] = lowband[j];
-         renormalise_vector(X, Q15ONE, N, 1);
       } else {
          /* This is important for encoding the side in stereo mode */
          for (j=0;j<N;j++)
-            X[j] = 0;
+         {
+            *seed = lcg_rand(*seed);
+            X[j] = (int)(*seed)>>20;
+         }
       }
+      renormalise_vector(X, Q15ONE, N, 1);
       return;
    }
    K = get_pulses(K);
@@ -333,7 +340,7 @@
 
 /** Decode pulse vector and combine the result with the pitch vector to produce
     the final normalised signal in the current band. */
-void alg_unquant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, ec_dec *dec)
+void alg_unquant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, ec_dec *dec, celt_int32 *seed)
 {
    int i;
    celt_word32 Ryy;
@@ -345,12 +352,15 @@
       {
          for (i=0;i<N;i++)
             X[i] = lowband[i];
-         renormalise_vector(X, Q15ONE, N, 1);
       } else {
          /* This is important for encoding the side in stereo mode */
          for (i=0;i<N;i++)
-            X[i] = 0;
+         {
+            *seed = lcg_rand(*seed);
+            X[i] = (int)(*seed)>>20;
+         }
       }
+      renormalise_vector(X, Q15ONE, N, 1);
       return;
    }
    K = get_pulses(K);
--- a/libcelt/vq.h
+++ b/libcelt/vq.h
@@ -51,7 +51,7 @@
  * @param p Pitch vector (it is assumed that p+x is a unit vector)
  * @param enc Entropy encoder state
 */
-void alg_quant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, int resynth, ec_enc *enc);
+void alg_quant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, int resynth, ec_enc *enc, celt_int32 *seed);
 
 /** Algebraic pulse decoder
  * @param x Decoded normalised spectrum (returned)
@@ -60,7 +60,7 @@
  * @param p Pitch vector (automatically added to x)
  * @param dec Entropy decoder state
  */
-void alg_unquant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, ec_dec *dec);
+void alg_unquant(celt_norm *X, int N, int K, int spread, celt_norm *lowband, ec_dec *dec, celt_int32 *seed);
 
 celt_word16 renormalise_vector(celt_norm *X, celt_word16 value, int N, int stride);