shithub: opus

Download patch

ref: 65ee67ac55b8752b63bc37ed56690fd1689fedd7
parent: be8d1259af0a91d0a0f7eff5a262e2c1745ba48b
author: Jean-Marc Valin <[email protected]>
date: Mon Apr 26 03:08:44 EDT 2010

Making the band definition the same at all frame sizes.

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -48,7 +48,7 @@
 
 #ifdef FIXED_POINT
 /* Compute the amplitude (sqrt energy) in each of the bands */
-void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int _C)
+void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int _C, int M)
 {
    int i, c, N;
    const celt_int16 *eBands = m->eBands;
@@ -62,18 +62,18 @@
          celt_word32 maxval=0;
          celt_word32 sum = 0;
          
-         j=eBands[i]; do {
+         j=M*eBands[i]; do {
             maxval = MAX32(maxval, X[j+c*N]);
             maxval = MAX32(maxval, -X[j+c*N]);
-         } while (++j<eBands[i+1]);
+         } while (++j<M*eBands[i+1]);
          
          if (maxval > 0)
          {
             int shift = celt_ilog2(maxval)-10;
-            j=eBands[i]; do {
+            j=M*eBands[i]; do {
                sum = MAC16_16(sum, EXTRACT16(VSHR32(X[j+c*N],shift)),
                                    EXTRACT16(VSHR32(X[j+c*N],shift)));
-            } while (++j<eBands[i+1]);
+            } while (++j<M*eBands[i+1]);
             /* We're adding one here to make damn sure we never end up with a pitch vector that's
                larger than unity norm */
             bank[i+c*m->nbEBands] = EPSILON+VSHR32(EXTEND32(celt_sqrt(sum)),-shift);
@@ -87,7 +87,7 @@
 }
 
 /* Normalise each band such that the energy is one. */
-void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int _C)
+void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int _C, int M)
 {
    int i, c, N;
    const celt_int16 *eBands = m->eBands;
@@ -102,9 +102,9 @@
          shift = celt_zlog2(bank[i+c*m->nbEBands])-13;
          E = VSHR32(bank[i+c*m->nbEBands], shift);
          g = EXTRACT16(celt_rcp(SHL32(E,3)));
-         j=eBands[i]; do {
+         j=M*eBands[i]; do {
             X[j+c*N] = MULT16_16_Q15(VSHR32(freq[j+c*N],shift-1),g);
-         } while (++j<eBands[i+1]);
+         } while (++j<M*eBands[i+1]);
       } while (++i<m->nbEBands);
    }
 }
@@ -111,7 +111,7 @@
 
 #else /* FIXED_POINT */
 /* Compute the amplitude (sqrt energy) in each of the bands */
-void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int _C)
+void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bank, int _C, int M)
 {
    int i, c, N;
    const celt_int16 *eBands = m->eBands;
@@ -123,7 +123,7 @@
       {
          int j;
          celt_word32 sum = 1e-10;
-         for (j=eBands[i];j<eBands[i+1];j++)
+         for (j=M*eBands[i];j<M*eBands[i+1];j++)
             sum += X[j+c*N]*X[j+c*N];
          bank[i+c*m->nbEBands] = sqrt(sum);
          /*printf ("%f ", bank[i+c*m->nbEBands]);*/
@@ -133,7 +133,7 @@
 }
 
 #ifdef EXP_PSY
-void compute_noise_energies(const CELTMode *m, const celt_sig *X, const celt_word16 *tonality, celt_ener *bank, int _C)
+void compute_noise_energies(const CELTMode *m, const celt_sig *X, const celt_word16 *tonality, celt_ener *bank, int _C, int M)
 {
    int i, c, N;
    const celt_int16 *eBands = m->eBands;
@@ -145,7 +145,7 @@
       {
          int j;
          celt_word32 sum = 1e-10;
-         for (j=eBands[i];j<eBands[i+1];j++)
+         for (j=M*eBands[i];j<M*eBands[i+1];j++)
             sum += X[j*C+c]*X[j+c*N]*tonality[j];
          bank[i+c*m->nbEBands] = sqrt(sum);
          /*printf ("%f ", bank[i+c*m->nbEBands]);*/
@@ -156,7 +156,7 @@
 #endif
 
 /* Normalise each band such that the energy is one. */
-void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int _C)
+void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bank, int _C, int M)
 {
    int i, c, N;
    const celt_int16 *eBands = m->eBands;
@@ -168,7 +168,7 @@
       {
          int j;
          celt_word16 g = 1.f/(1e-10f+bank[i+c*m->nbEBands]);
-         for (j=eBands[i];j<eBands[i+1];j++)
+         for (j=M*eBands[i];j<M*eBands[i+1];j++)
             X[j+c*N] = freq[j+c*N]*g;
       }
    }
@@ -176,7 +176,7 @@
 
 #endif /* FIXED_POINT */
 
-void renormalise_bands(const CELTMode *m, celt_norm * restrict X, int _C)
+void renormalise_bands(const CELTMode *m, celt_norm * restrict X, int _C, int M)
 {
    int i, c;
    const celt_int16 *eBands = m->eBands;
@@ -184,13 +184,13 @@
    for (c=0;c<C;c++)
    {
       i=0; do {
-         renormalise_vector(X+eBands[i]+c*m->eBands[m->nbEBands+1], Q15ONE, eBands[i+1]-eBands[i], 1);
+         renormalise_vector(X+M*eBands[i]+c*M*eBands[m->nbEBands+1], Q15ONE, M*eBands[i+1]-M*eBands[i], 1);
       } while (++i<m->nbEBands);
    }
 }
 
 /* De-normalise the energy to produce the synthesis from the unit-energy bands */
-void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig * restrict freq, const celt_ener *bank, int _C)
+void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig * restrict freq, const celt_ener *bank, int _C, int M)
 {
    int i, c, N;
    const celt_int16 *eBands = m->eBands;
@@ -208,14 +208,14 @@
       {
          int j, end;
          celt_word32 g = SHR32(bank[i+c*m->nbEBands],1);
-         j=eBands[i];
-         end = eBands[i+1];
+         j=M*eBands[i];
+         end = M*eBands[i+1];
          do {
             *f++ = SHL32(MULT16_32_Q15(*x, g),2);
             x++;
          } while (++j<end);
       }
-      for (i=eBands[m->nbEBands];i<eBands[m->nbEBands+1];i++)
+      for (i=M*eBands[m->nbEBands];i<M*eBands[m->nbEBands+1];i++)
          *f++ = 0;
    }
 }
@@ -341,7 +341,7 @@
 
 #ifndef DISABLE_STEREO
 
-static void stereo_band_mix(const CELTMode *m, celt_norm *X, celt_norm *Y, const celt_ener *bank, int stereo_mode, int bandID, int dir)
+static void stereo_band_mix(const CELTMode *m, celt_norm *X, celt_norm *Y, const celt_ener *bank, int stereo_mode, int bandID, int dir, int M)
 {
    int i = bandID;
    const celt_int16 *eBands = m->eBands;
@@ -364,7 +364,7 @@
       a1 = DIV32_16(SHL32(EXTEND32(left),14),norm);
       a2 = dir*DIV32_16(SHL32(EXTEND32(right),14),norm);
    }
-   for (j=0;j<eBands[i+1]-eBands[i];j++)
+   for (j=0;j<M*eBands[i+1]-M*eBands[i];j++)
    {
       celt_norm r, l;
       l = X[j];
@@ -377,7 +377,7 @@
 
 #endif /* DISABLE_STEREO */
 
-int folding_decision(const CELTMode *m, celt_norm *X, celt_word16 *average, int *last_decision, int _C)
+int folding_decision(const CELTMode *m, celt_norm *X, celt_word16 *average, int *last_decision, int _C, int M)
 {
    int i, c, N0;
    int NR=0;
@@ -395,8 +395,8 @@
       int max_i=0;
       celt_word16 max_val=EPSILON;
       celt_word32 floor_ener=EPSILON;
-      celt_norm * restrict x = X+eBands[i]+c*N0;
-      N = eBands[i+1]-eBands[i];
+      celt_norm * restrict x = X+M*eBands[i]+c*N0;
+      N = M*eBands[i+1]-M*eBands[i];
       for (j=0;j<N;j++)
       {
          if (ABS16(x[j])>max_val)
@@ -448,7 +448,7 @@
 }
 
 /* Quantisation of the residual */
-void quant_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_dec)
+void quant_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_dec, int M)
 {
    int i, j, remaining_bits, balance;
    const celt_int16 * restrict eBands = m->eBands;
@@ -458,7 +458,7 @@
    SAVE_STACK;
 
    B = shortBlocks ? m->nbShortMdcts : 1;
-   ALLOC(_norm, eBands[m->nbEBands+1], celt_norm);
+   ALLOC(_norm, M*eBands[m->nbEBands+1], celt_norm);
    norm = _norm;
 
    balance = 0;
@@ -471,7 +471,7 @@
       
       int curr_balance, curr_bits;
       
-      N = eBands[i+1]-eBands[i];
+      N = M*eBands[i+1]-M*eBands[i];
       BPbits = m->bits;
 
       if (encode)
@@ -502,18 +502,18 @@
       {
          int spread = fold ? B : 0;
          if (encode)
-            alg_quant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, resynth, enc_dec);
+            alg_quant(X+M*eBands[i], N, q, spread, resynth, enc_dec);
          else
-            alg_unquant(X+eBands[i], eBands[i+1]-eBands[i], q, spread, enc_dec);
+            alg_unquant(X+M*eBands[i], N, q, spread, enc_dec);
       } else {
          if (resynth)
-            intra_fold(m, start, eBands[i+1]-eBands[i], norm, X+eBands[i], eBands[i], B);
+            intra_fold(m, start, N, norm, X+M*eBands[i], M*eBands[i], B, M);
       }
       if (resynth)
       {
          celt_word16 n;
-         n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22));
-         for (j=eBands[i];j<eBands[i+1];j++)
+         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]);
       }
    }
@@ -522,7 +522,7 @@
 
 #ifndef DISABLE_STEREO
 
-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)
+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 M)
 {
    int i, j, remaining_bits, balance;
    const celt_int16 * restrict eBands = m->eBands;
@@ -533,7 +533,7 @@
    SAVE_STACK;
 
    B = shortBlocks ? m->nbShortMdcts : 1;
-   ALLOC(_norm, eBands[m->nbEBands+1], celt_norm);
+   ALLOC(_norm, M*eBands[m->nbEBands+1], celt_norm);
    norm = _norm;
 
    balance = 0;
@@ -550,11 +550,11 @@
       int qalloc;
       celt_norm * restrict X, * restrict Y;
       
-      X = _X+eBands[i];
-      Y = X+eBands[m->nbEBands+1];
+      X = _X+M*eBands[i];
+      Y = X+M*eBands[m->nbEBands+1];
       BPbits = m->bits;
 
-      N = eBands[i+1]-eBands[i];
+      N = M*eBands[i+1]-M*eBands[i];
       tell = ec_enc_tell(enc, BITRES);
       if (i != start)
          balance -= tell;
@@ -575,7 +575,7 @@
       if (qb>14)
          qb = 14;
 
-      stereo_band_mix(m, X, Y, bandE, qb==0, i, 1);
+      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);
@@ -715,7 +715,7 @@
             alg_quant(X, N, q1, spread, resynth, enc);
          } else {
             if (resynth)
-               intra_fold(m, start, eBands[i+1]-eBands[i], norm, X, eBands[i], B);
+               intra_fold(m, start, N, norm, X, M*eBands[i], B, M);
          }
          if (q2 > 0) {
             int spread = fold ? B : 0;
@@ -737,9 +737,9 @@
          mid = (1.f/32768)*imid;
          side = (1.f/32768)*iside;
 #endif
-         n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22));
+         n = celt_sqrt(SHL32(EXTEND32(N),22));
          for (j=0;j<N;j++)
-            norm[eBands[i]+j] = MULT16_16_Q15(n,X[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);
@@ -746,7 +746,7 @@
          for (j=0;j<N;j++)
             Y[j] = MULT16_16_Q15(Y[j], side);
 
-         stereo_band_mix(m, X, Y, bandE, 0, i, -1);
+         stereo_band_mix(m, X, Y, bandE, 0, i, -1, M);
          renormalise_vector(X, Q15ONE, N, 1);
          renormalise_vector(Y, Q15ONE, N, 1);
       }
@@ -758,7 +758,7 @@
 
 #ifndef DISABLE_STEREO
 
-void unquant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_dec *dec)
+void unquant_bands_stereo(const CELTMode *m, int start, celt_norm *_X, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int total_bits, ec_dec *dec, int M)
 {
    int i, j, remaining_bits, balance;
    const celt_int16 * restrict eBands = m->eBands;
@@ -769,7 +769,7 @@
    SAVE_STACK;
 
    B = shortBlocks ? m->nbShortMdcts : 1;
-   ALLOC(_norm, eBands[m->nbEBands+1], celt_norm);
+   ALLOC(_norm, M*eBands[m->nbEBands+1], celt_norm);
    norm = _norm;
 
    balance = 0;
@@ -787,11 +787,11 @@
       int qalloc;
       celt_norm * restrict X, * restrict Y;
       
-      X = _X+eBands[i];
-      Y = X+eBands[m->nbEBands+1];
+      X = _X+M*eBands[i];
+      Y = X+M*eBands[m->nbEBands+1];
       BPbits = m->bits;
 
-      N = eBands[i+1]-eBands[i];
+      N = M*eBands[i+1]-M*eBands[i];
       tell = ec_dec_tell(dec, BITRES);
       if (i != start)
          balance -= tell;
@@ -836,7 +836,7 @@
          iside = bitexact_cos(16384-itheta);
          delta = (N-1)*(log2_frac(iside,BITRES+2)-log2_frac(imid,BITRES+2))>>2;
       }
-      n = celt_sqrt(SHL32(EXTEND32(eBands[i+1]-eBands[i]),22));
+      n = celt_sqrt(SHL32(EXTEND32(N),22));
 
 #if 1
       if (N==2)
@@ -928,7 +928,7 @@
             int spread = fold ? B : 0;
             alg_unquant(X, N, q1, spread, dec);
          } else
-            intra_fold(m, start, eBands[i+1]-eBands[i], norm, X, eBands[i], B);
+            intra_fold(m, start, N, norm, X, M*eBands[i], B, M);
          if (q2 > 0)
          {
             int spread = fold ? B : 0;
@@ -936,7 +936,7 @@
          } else
             for (j=0;j<N;j++)
                Y[j] = 0;
-            /*orthogonalize(X+C*eBands[i], X+C*eBands[i]+N, N);*/
+            /*orthogonalize(X+C*M*eBands[i], X+C*M*eBands[i]+N, N);*/
       }
       balance += pulses[i] + tell;
       
@@ -948,7 +948,7 @@
       side = (1.f/32768)*iside;
 #endif
       for (j=0;j<N;j++)
-         norm[eBands[i]+j] = MULT16_16_Q15(n,X[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);
@@ -955,7 +955,7 @@
       for (j=0;j<N;j++)
          Y[j] = MULT16_16_Q15(Y[j], side);
 
-      stereo_band_mix(m, X, Y, bandE, 0, i, -1);
+      stereo_band_mix(m, X, Y, bandE, 0, i, -1, M);
       renormalise_vector(X, Q15ONE, N, 1);
       renormalise_vector(Y, Q15ONE, N, 1);
    }
--- a/libcelt/bands.h
+++ b/libcelt/bands.h
@@ -45,7 +45,7 @@
  * @param X Spectrum
  * @param bands Square root of the energy for each band (returned)
  */
-void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bands, int _C);
+void compute_band_energies(const CELTMode *m, const celt_sig *X, celt_ener *bands, int _C, int M);
 
 /*void compute_noise_energies(const CELTMode *m, const celt_sig *X, const celt_word16 *tonality, celt_ener *bank);*/
 
@@ -55,9 +55,9 @@
  * @param X Spectrum (returned normalised)
  * @param bands Square root of the energy for each band
  */
-void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bands, int _C);
+void normalise_bands(const CELTMode *m, const celt_sig * restrict freq, celt_norm * restrict X, const celt_ener *bands, int _C, int M);
 
-void renormalise_bands(const CELTMode *m, celt_norm * restrict X, int _C);
+void renormalise_bands(const CELTMode *m, celt_norm * restrict X, int _C, int M);
 
 /** Denormalise each band of X to restore full amplitude
  * @param m Mode data 
@@ -64,7 +64,7 @@
  * @param X Spectrum (returned de-normalised)
  * @param bands Square root of the energy for each band
  */
-void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig * restrict freq, const celt_ener *bands, int _C);
+void denormalise_bands(const CELTMode *m, const celt_norm * restrict X, celt_sig * restrict freq, const celt_ener *bands, int _C, int M);
 
 /** Compute the pitch predictor gain for each pitch band
  * @param m Mode data 
@@ -77,7 +77,7 @@
 
 void apply_pitch(const CELTMode *m, celt_sig *X, const celt_sig *P, int gain_id, int pred, int _C);
 
-int folding_decision(const CELTMode *m, celt_norm *X, celt_word16 *average, int *last_decision, int _C);
+int folding_decision(const CELTMode *m, celt_norm *X, celt_word16 *average, int *last_decision, int _C, int M);
 
 /** Quantisation/encoding of the residual spectrum
  * @param m Mode data 
@@ -85,9 +85,9 @@
  * @param total_bits Total number of bits that can be used for the frame (including the ones already spent)
  * @param enc Entropy encoder
  */
-void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int resynth, int total_bits, int encode, void *enc_dec);
+void quant_bands(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int resynth, int total_bits, int encode, void *enc_dec, int M);
 
-void quant_bands_stereo(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int resynth, int total_bits, ec_enc *enc);
+void quant_bands_stereo(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int resynth, int total_bits, ec_enc *enc, int M);
 
 /** Decoding of the residual spectrum
  * @param m Mode data 
@@ -95,8 +95,8 @@
  * @param total_bits Total number of bits that can be used for the frame (including the ones already spent)
  * @param dec Entropy decoder
 */
-void unquant_bands_stereo(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int total_bits, ec_dec *dec);
+void unquant_bands_stereo(const CELTMode *m, int start, celt_norm * restrict X, const celt_ener *bandE, int *pulses, int time_domain, int fold, int total_bits, ec_dec *dec, int M);
 
-void stereo_decision(const CELTMode *m, celt_norm * restrict X, int *stereo_mode, int len);
+void stereo_decision(const CELTMode *m, celt_norm * restrict X, int *stereo_mode, int len, int M);
 
 #endif /* BANDS_H */
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -531,7 +531,7 @@
 
 static void mdct_shape(const CELTMode *mode, celt_norm *X, int start,
                        int end, int N, int nbShortMdcts,
-                       int mdct_weight_shift, int _C, int renorm)
+                       int mdct_weight_shift, int _C, int renorm, int M)
 {
    int m, i, c;
    const int C = CHANNELS(_C);
@@ -544,7 +544,7 @@
             X[i] = (1.f/(1<<mdct_weight_shift))*X[i];
 #endif
    if (renorm)
-      renormalise_bands(mode, X, C);
+      renormalise_bands(mode, X, C, M);
 }
 
 
@@ -585,6 +585,7 @@
    int gain_id=0;
    int norm_rate;
    int start=0;
+   const int M=st->mode->nbShortMdcts;
    SAVE_STACK;
 
    if (check_encoder(st) != CELT_OK)
@@ -697,13 +698,13 @@
    if (has_pitch)
       apply_pitch(st->mode, freq, pitch_freq, gain_id, 1, C);
 
-   compute_band_energies(st->mode, freq, bandE, C);
+   compute_band_energies(st->mode, freq, bandE, C, M);
    for (i=0;i<st->mode->nbEBands*C;i++)
       bandLogE[i] = amp2Log(bandE[i]);
 
    /* Band normalisation */
-   normalise_bands(st->mode, freq, X, bandE, C);
-   if (!shortBlocks && !folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, C))
+   normalise_bands(st->mode, freq, X, bandE, C, M);
+   if (!shortBlocks && !folding_decision(st->mode, X, &st->tonal_average, &st->fold_decision, C, M))
       has_fold = 0;
 
    /* Don't use intra energy when we're operating at low bit-rate */
@@ -713,7 +714,7 @@
    else
       st->delayedIntra = 0;
 
-   NN = st->mode->eBands[st->mode->nbEBands];
+   NN = M*st->mode->eBands[st->mode->nbEBands];
    if (shortBlocks && !transient_shift) 
    {
       celt_word32 sum[8]={1,1,1,1,1,1,1,1};
@@ -757,7 +758,7 @@
       } while (m<st->mode->nbShortMdcts-1);
 #endif
       if (mdct_weight_shift)
-         mdct_shape(st->mode, X, mdct_weight_pos+1, st->mode->nbShortMdcts, N, st->mode->nbShortMdcts, mdct_weight_shift, C, 0);
+         mdct_shape(st->mode, X, mdct_weight_pos+1, st->mode->nbShortMdcts, N, st->mode->nbShortMdcts, mdct_weight_shift, C, 0, M);
    }
 
 
@@ -865,16 +866,16 @@
    for (i=0;i<st->mode->nbEBands;i++)
       offsets[i] = 0;
    bits = nbCompressedBytes*8 - ec_enc_tell(&enc, 0) - 1;
-   compute_allocation(st->mode, start, offsets, bits, pulses, fine_quant, fine_priority, C);
+   compute_allocation(st->mode, start, offsets, bits, pulses, fine_quant, fine_priority, C, M);
 
    quant_fine_energy(st->mode, start, bandE, st->oldBandE, error, fine_quant, &enc, C);
 
    /* Residual quantisation */
    if (C==1)
-      quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, resynth, nbCompressedBytes*8, 1, &enc);
+      quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, resynth, nbCompressedBytes*8, 1, &enc, M);
 #ifndef DISABLE_STEREO
    else
-      quant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, resynth, nbCompressedBytes*8, &enc);
+      quant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, resynth, nbCompressedBytes*8, &enc, M);
 #endif
 
    quant_energy_finalise(st->mode, start, bandE, st->oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_enc_tell(&enc, 0), &enc, C);
@@ -887,11 +888,11 @@
 
       if (mdct_weight_shift)
       {
-         mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, st->mode->nbShortMdcts, mdct_weight_shift, C, 1);
+         mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, st->mode->nbShortMdcts, mdct_weight_shift, C, 1, M);
       }
 
       /* Synthesis */
-      denormalise_bands(st->mode, X, freq, bandE, C);
+      denormalise_bands(st->mode, X, freq, bandE, C, M);
 
       CELT_MOVE(st->out_mem, st->out_mem+C*N, C*(MAX_PERIOD+st->overlap-N));
 
@@ -1459,6 +1460,7 @@
    int mdct_weight_pos=0;
    int gain_id=0;
    int start=0;
+   const int M=st->mode->nbShortMdcts;
    SAVE_STACK;
 
    if (check_decoder(st) != CELT_OK)
@@ -1530,7 +1532,7 @@
       offsets[i] = 0;
 
    bits = len*8 - ec_dec_tell(&dec, 0) - 1;
-   compute_allocation(st->mode, start, offsets, bits, pulses, fine_quant, fine_priority, C);
+   compute_allocation(st->mode, start, offsets, bits, pulses, fine_quant, fine_priority, C, M);
    /*bits = ec_dec_tell(&dec, 0);
    compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(&dec, 0)-bits))/C);*/
    
@@ -1545,20 +1547,20 @@
 
    /* Decode fixed codebook and merge with pitch */
    if (C==1)
-      quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, 1, len*8, 0, &dec);
+      quant_bands(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, 1, len*8, 0, &dec, M);
 #ifndef DISABLE_STEREO
    else
-      unquant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, len*8, &dec);
+      unquant_bands_stereo(st->mode, start, X, bandE, pulses, shortBlocks, has_fold, len*8, &dec, M);
 #endif
    unquant_energy_finalise(st->mode, start, bandE, st->oldBandE, fine_quant, fine_priority, len*8-ec_dec_tell(&dec, 0), &dec, C);
    
    if (mdct_weight_shift)
    {
-      mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, st->mode->nbShortMdcts, mdct_weight_shift, C, 1);
+      mdct_shape(st->mode, X, 0, mdct_weight_pos+1, N, st->mode->nbShortMdcts, mdct_weight_shift, C, 1, M);
    }
 
    /* Synthesis */
-   denormalise_bands(st->mode, X, freq, bandE, C);
+   denormalise_bands(st->mode, X, freq, bandE, C, M);
 
 
    CELT_MOVE(st->decode_mem, st->decode_mem+C*N, C*(DECODE_BUFFER_SIZE+st->overlap-N));
@@ -1566,7 +1568,7 @@
    if (has_pitch)
       apply_pitch(st->mode, freq, pitch_freq, gain_id, 0, C);
 
-   for (i=0;i<st->mode->eBands[start];i++)
+   for (i=0;i<M*st->mode->eBands[start];i++)
       freq[i] = 0;
 
    /* Compute inverse MDCTs */
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -119,14 +119,13 @@
 
 static celt_int16 *compute_ebands(celt_int32 Fs, int frame_size, int nbShortMdcts, int *nbEBands)
 {
-   int min_bins = 2;
    celt_int16 *eBands;
    int i, res, min_width, lin, low, high, nBark, offset=0;
 
-   if (min_bins < nbShortMdcts)
-      min_bins = nbShortMdcts;
+   frame_size /= nbShortMdcts;
+   nbShortMdcts = 1;
    res = (Fs+frame_size)/(2*frame_size);
-   min_width = min_bins*res;
+   min_width = res;
 
    /* Find the number of critical bands supported by our sampling rate */
    for (nBark=1;nBark<BARK_BANDS;nBark++)
@@ -138,7 +137,7 @@
       if (bark_freq[lin+1]-bark_freq[lin] >= min_width)
          break;
 
-   low = (bark_freq[lin]+res*min_bins/2)/(res*min_bins);
+   low = (bark_freq[lin]+res/2)/res;
    high = nBark-lin;
    *nbEBands = low+high;
    eBands = celt_alloc(sizeof(celt_int16)*(*nbEBands+2));
@@ -148,7 +147,7 @@
    
    /* Linear spacing (min_width) */
    for (i=0;i<low;i++)
-      eBands[i] = min_bins*i;
+      eBands[i] = i;
    if (low>0)
       offset = eBands[low-1]*res - bark_freq[lin-1];
    /* Spacing follows critical bands */
@@ -155,14 +154,14 @@
    for (i=0;i<high;i++)
    {
       int target = bark_freq[lin+i];
-      eBands[i+low] = (target+(offset+res*nbShortMdcts)/2)/(res*nbShortMdcts)*nbShortMdcts;
+      eBands[i+low] = (target+(offset+res)/2)/res;
       offset = eBands[i+low]*res - target;
    }
    /* Enforce the minimum spacing at the boundary */
    for (i=0;i<*nbEBands;i++)
-      if (eBands[i] < min_bins*i)
-         eBands[i] = min_bins*i;
-   eBands[*nbEBands] = (bark_freq[nBark]+res*nbShortMdcts/2)/(res*nbShortMdcts)*nbShortMdcts;
+      if (eBands[i] < i)
+         eBands[i] = i;
+   eBands[*nbEBands] = (bark_freq[nBark]+res/2)/res;
    eBands[*nbEBands+1] = frame_size;
    if (eBands[*nbEBands] > eBands[*nbEBands+1])
       eBands[*nbEBands] = eBands[*nbEBands+1];
@@ -170,10 +169,10 @@
    {
       if (eBands[i+1]-eBands[i] < eBands[i]-eBands[i-1])
       {
-         eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1]+nbShortMdcts)/(2*nbShortMdcts)*nbShortMdcts;
+         eBands[i] -= (2*eBands[i]-eBands[i-1]-eBands[i+1])/2;
       }
    }
-   /*for (i=0;i<*nbEBands+1;i++)
+   /*for (i=0;i<=*nbEBands+1;i++)
       printf ("%d ", eBands[i]);
    printf ("\n");
    exit(1);*/
@@ -181,7 +180,7 @@
    return eBands;
 }
 
-static void compute_allocation_table(CELTMode *mode, int res)
+static void compute_allocation_table(CELTMode *mode, int res, int M)
 {
    int i, j, nBark;
    celt_int16 *allocVectors;
@@ -209,7 +208,7 @@
          low = bark_freq[j];
          high = bark_freq[j+1];
 
-         edge = mode->eBands[eband+1]*res;
+         edge = M*mode->eBands[eband+1]*res;
          while (edge <= high && eband < mode->nbEBands)
          {
             celt_int32 num;
@@ -227,7 +226,7 @@
             /* Move to next eband */
             current = 0;
             eband++;
-            edge = mode->eBands[eband+1]*res;
+            edge = M*mode->eBands[eband+1]*res;
          }
          current += alloc;
       }
@@ -361,7 +360,7 @@
    else
       mode->overlap = (frame_size>>3)<<2;
 
-   compute_allocation_table(mode, res);
+   compute_allocation_table(mode, res, mode->nbShortMdcts);
    if (mode->allocVectors==NULL)
       goto failure;
    
@@ -378,7 +377,7 @@
 #endif
    mode->window = window;
 
-   mode->bits = (const celt_int16 **)compute_alloc_cache(mode, 1);
+   mode->bits = (const celt_int16 **)compute_alloc_cache(mode, 1, mode->nbShortMdcts);
    if (mode->bits==NULL)
       goto failure;
 
@@ -387,7 +386,7 @@
       goto failure;
 
    for (i=0;i<mode->nbEBands;i++)
-      logN[i] = log2_frac(mode->eBands[i+1]-mode->eBands[i], BITRES);
+      logN[i] = log2_frac(mode->nbShortMdcts*(mode->eBands[i+1]-mode->eBands[i]), BITRES);
    mode->logN = logN;
 #endif /* !STATIC_MODES */
 
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -46,7 +46,7 @@
 
 #ifndef STATIC_MODES
 
-celt_int16 **compute_alloc_cache(CELTMode *m, int C)
+celt_int16 **compute_alloc_cache(CELTMode *m, int C, int M)
 {
    int i, prevN;
    int error = 0;
@@ -60,8 +60,8 @@
    prevN = -1;
    for (i=0;i<m->nbEBands;i++)
    {
-      int N = C*(eBands[i+1]-eBands[i]);
-      if (N == prevN && eBands[i] < m->pitchEnd)
+      int N = C*M*(eBands[i+1]-eBands[i]);
+      if (N == prevN && M*eBands[i] < m->pitchEnd)
       {
          bits[i] = bits[i-1];
       } else {
@@ -102,7 +102,7 @@
 
 
 
-static inline void interp_bits2pulses(const CELTMode *m, int start, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len, int _C)
+static inline void interp_bits2pulses(const CELTMode *m, int start, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len, int _C, int M)
 {
    int psum;
    int lo, hi;
@@ -145,7 +145,7 @@
       int N, d;
       int offset;
 
-      N=m->eBands[j+1]-m->eBands[j]; 
+      N=M*(m->eBands[j+1]-m->eBands[j]);
       /* Compensate for the extra DoF in stereo */
       d=(C*N+ ((C==2 && N>2) ? 1 : 0))<<BITRES; 
       offset = FINE_OFFSET - m->logN[j];
@@ -173,7 +173,7 @@
    RESTORE_STACK;
 }
 
-void compute_allocation(const CELTMode *m, int start, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C)
+void compute_allocation(const CELTMode *m, int start, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int M)
 {
    int lo, hi, len, j;
    const int C = CHANNELS(_C);
@@ -216,7 +216,7 @@
       if (bits2[j] < 0)
          bits2[j] = 0;
    }
-   interp_bits2pulses(m, start, bits1, bits2, total, pulses, ebits, fine_priority, len, C);
+   interp_bits2pulses(m, start, bits1, bits2, total, pulses, ebits, fine_priority, len, C, M);
    RESTORE_STACK;
 }
 
--- a/libcelt/rate.h
+++ b/libcelt/rate.h
@@ -155,7 +155,7 @@
 }
 
 /** Computes a cache of the pulses->bits mapping in each band */
-celt_int16 **compute_alloc_cache(CELTMode *m, int C);
+celt_int16 **compute_alloc_cache(CELTMode *m, int C, int M);
 
 /** Compute the pulse allocation, i.e. how many pulses will go in each
   * band. 
@@ -166,7 +166,7 @@
  @param pulses Number of pulses per band (returned)
  @return Total number of bits allocated
 */
-void compute_allocation(const CELTMode *m, int start, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C);
+void compute_allocation(const CELTMode *m, int start, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int M);
 
 
 #endif
--- a/libcelt/vq.c
+++ b/libcelt/vq.c
@@ -473,11 +473,11 @@
    return celt_sqrt(E);
 }
 
-static void fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B)
+static void fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B, int M)
 {
    int j;
    int id = N0 % B;
-   while (id < m->eBands[start])
+   while (id < M*m->eBands[start])
       id += B;
    /* Here, we assume that id will never be greater than N0, i.e. that 
       no band is wider than N0. In the unlikely case it happens, we set
@@ -500,9 +500,9 @@
          P[j] = Y[id++];
 }
 
-void intra_fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B)
+void intra_fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B, int M)
 {
-   fold(m, start, N, Y, P, N0, B);
+   fold(m, start, N, Y, P, N0, B, M);
    renormalise_vector(P, Q15ONE, N, 1);
 }
 
--- a/libcelt/vq.h
+++ b/libcelt/vq.h
@@ -75,6 +75,6 @@
  * @param B Stride (number of channels multiplied by the number of MDCTs per frame)
  * @param N0 Number of valid offsets
  */
-void intra_fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B);
+void intra_fold(const CELTMode *m, int start, int N, const celt_norm * restrict Y, celt_norm * restrict P, int N0, int B, int M);
 
 #endif /* VQ_H */