Reorders some operations in anti-collapse to reuse values
[opus.git] / libcelt / bands.c
index 0031ce5..132c9d9 100644 (file)
@@ -217,42 +217,51 @@ void anti_collapse(const CELTMode *m, celt_norm *_X, unsigned char *collapse_mas
       celt_word16 *prev2logE, int *pulses, celt_uint32 seed)
 {
    int c, i, j, k;
-   c=0; do
+   for (i=start;i<end;i++)
    {
-      for (i=start;i<end;i++)
+      int N0;
+      celt_word16 thresh, sqrt_1;
+      int depth;
+#ifdef FIXED_POINT
+      int shift;
+#endif
+
+      N0 = m->eBands[i+1]-m->eBands[i];
+      depth = (1+(pulses[i]>>BITRES))/(m->eBands[i+1]-m->eBands[i]<<LM);
+
+#ifdef FIXED_POINT
+      thresh = MULT16_32_Q15(QCONST16(0.3f, 15), MIN32(32767,SHR32(celt_exp2(-SHL16(depth, 11)),1) ));
+      {
+         celt_word32 t;
+         t = N0<<LM;
+         shift = celt_ilog2(t)>>1;
+         t = SHL32(t, (7-shift)<<1);
+         sqrt_1 = celt_rsqrt_norm(t);
+      }
+#else
+      thresh = .3f*celt_exp2(-depth);
+      sqrt_1 = celt_rsqrt(N0<<LM);
+#endif
+
+      c=0; do
       {
          celt_norm *X;
-         int N0;
          celt_word16 Ediff;
          celt_word16 r;
-         celt_word16 thresh;
-         int depth;
-
-         N0 = m->eBands[i+1]-m->eBands[i];
          Ediff = logE[c*m->nbEBands+i]-MIN16(prev1logE[c*m->nbEBands+i],prev2logE[c*m->nbEBands+i]);
          Ediff = MAX16(0, Ediff);
-         depth = (1+(pulses[i]>>BITRES))/(m->eBands[i+1]-m->eBands[i]<<LM);
 
 #ifdef FIXED_POINT
-         thresh = MULT16_32_Q15(QCONST16(0.3f, 15), MIN32(32767,SHR32(celt_exp2(-SHL16(depth, 11)),1) ));
          if (Ediff < 16384)
             r = 2*MIN16(16383,SHR32(celt_exp2(-SHL16(Ediff, 11-DB_SHIFT)),1));
          else
             r = 0;
          r = SHR16(MIN16(thresh, r),1);
-         {
-            int shift;
-            celt_word32 t;
-            t = N0<<LM;
-            shift = celt_ilog2(t)>>1;
-            t = SHL32(t, (7-shift)<<1);
-            r = SHR32(MULT16_16_Q15(celt_rsqrt_norm(t), r),shift);
-         }
+         r = SHR32(MULT16_16_Q15(sqrt_1, r),shift);
 #else
-         thresh = .3f*celt_exp2(-depth);
          r = 2.f*celt_exp2(-Ediff);
          r = MIN16(thresh, r);
-         r = r*celt_rsqrt(N0<<LM);
+         r = r*sqrt_1;
 #endif
          X = _X+c*size+(m->eBands[i]<<LM);
          for (k=0;k<1<<LM;k++)
@@ -270,8 +279,8 @@ void anti_collapse(const CELTMode *m, celt_norm *_X, unsigned char *collapse_mas
          }
          /* We just added some energy, so we need to renormalise */
          renormalise_vector(X, N0<<LM, Q15ONE);
-      }
-   } while (++c<C);
+      } while (++c<C);
+   }
 
 }
 
@@ -605,7 +614,7 @@ static int compute_qn(int N, int b, int offset, int stereo)
 static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_norm *Y,
       int N, int b, int spread, int B, int intensity, 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 *seed, celt_word16 gain, celt_norm *lowband_scratch, int fill)
+      celt_uint32 *seed, celt_word16 gain, celt_norm *lowband_scratch, int fill)
 {
    int q;
    int curr_bits;
@@ -992,9 +1001,9 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c
 
          /* Finally do the actual quantization */
          if (encode)
-            cm = alg_quant(X, N, K, spread, B, lowband, resynth, (ec_enc*)ec, seed, gain);
+            cm = alg_quant(X, N, K, spread, B, lowband, resynth, (ec_enc*)ec, gain);
          else
-            cm = alg_unquant(X, N, K, spread, B, lowband, (ec_dec*)ec, seed, gain);
+            cm = alg_unquant(X, N, K, spread, B, lowband, (ec_dec*)ec, gain);
       } else {
          /* If there's no pulse, fill the band anyway */
          int j;
@@ -1086,7 +1095,7 @@ static unsigned quant_band(int encode, const CELTMode *m, int i, celt_norm *X, c
 void quant_all_bands(int encode, const CELTMode *m, int start, int end,
       celt_norm *_X, celt_norm *_Y, unsigned char *collapse_masks, const celt_ener *bandE, int *pulses,
       int shortBlocks, int spread, int dual_stereo, int intensity, int *tf_res, int resynth,
-      int total_bits, void *ec, int LM, int codedBands)
+      int total_bits, void *ec, int LM, int codedBands, ec_uint32 *seed)
 {
    int i;
    celt_int32 balance;
@@ -1097,7 +1106,6 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
    VARDECL(celt_norm, lowband_scratch);
    int B;
    int M;
-   celt_int32 seed;
    int lowband_offset;
    int update_lowband = 1;
    int C = _Y != NULL ? 2 : 1;
@@ -1110,10 +1118,6 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
    norm = _norm;
    norm2 = norm + M*eBands[m->nbEBands];
 
-   if (encode)
-      seed = ((ec_enc*)ec)->rng;
-   else
-      seed = ((ec_dec*)ec)->rng;
    balance = 0;
    lowband_offset = 0;
    for (i=start;i<end;i++)
@@ -1201,16 +1205,16 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end,
       {
          x_cm = quant_band(encode, m, i, X, NULL, N, b/2, spread, B, intensity, tf_change,
                effective_lowband != -1 ? norm+effective_lowband : NULL, resynth, ec, &remaining_bits, LM,
-               norm+M*eBands[i], bandE, 0, &seed, Q15ONE, lowband_scratch, x_cm);
+               norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm);
          y_cm = quant_band(encode, m, i, Y, NULL, N, b/2, spread, B, intensity, tf_change,
                effective_lowband != -1 ? norm2+effective_lowband : NULL, resynth, ec, &remaining_bits, LM,
-               norm2+M*eBands[i], bandE, 0, &seed, Q15ONE, lowband_scratch, y_cm);
+               norm2+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, y_cm);
          collapse_masks[i*2+0] = (unsigned char)(x_cm&(1<<B)-1);
          collapse_masks[i*2+1] = (unsigned char)(y_cm&(1<<B)-1);
       } else {
          x_cm = quant_band(encode, m, i, X, Y, N, b, spread, B, intensity, tf_change,
                effective_lowband != -1 ? norm+effective_lowband : NULL, resynth, ec, &remaining_bits, LM,
-               norm+M*eBands[i], bandE, 0, &seed, Q15ONE, lowband_scratch, x_cm|y_cm);
+               norm+M*eBands[i], bandE, 0, seed, Q15ONE, lowband_scratch, x_cm|y_cm);
          collapse_masks[i*C+1] = collapse_masks[i*C+0] = (unsigned char)(x_cm&(1<<B)-1);
       }
       balance += pulses[i] + tell;