minor doc update
[opus.git] / libcelt / quant_bands.c
index ce7cb72..9927a18 100644 (file)
@@ -5,18 +5,14 @@
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:
-   
+
    - Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
-   
+
    - Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
-   
-   - Neither the name of the Xiph.org Foundation nor the names of its
-   contributors may be used to endorse or promote products derived from
-   this software without specific prior written permission.
-   
+
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -41,6 +37,7 @@
 #include "arch.h"
 #include "mathops.h"
 #include "stack_alloc.h"
+#include "rate.h"
 
 #ifdef FIXED_POINT
 /* Mean energy in each band quantized in Q6 */
@@ -53,7 +50,7 @@ static const signed char eMeans[25] = {
 };
 #else
 /* Mean energy in each band quantized in Q6 and converted back to float */
-static const celt_word16 eMeans[25] = {
+static const opus_val16 eMeans[25] = {
       6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f,
       4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f,
       4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f,
@@ -63,11 +60,13 @@ static const celt_word16 eMeans[25] = {
 #endif
 /* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */
 #ifdef FIXED_POINT
-static const celt_word16 pred_coef[4] = {29440, 26112, 21248, 16384};
-static const celt_word16 beta_coef[4] = {30147, 22282, 12124, 6554};
+static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384};
+static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554};
+static const opus_val16 beta_intra = 4915;
 #else
-static const celt_word16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.};
-static const celt_word16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.};
+static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.};
+static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.};
+static const opus_val16 beta_intra = 4915/32768.;
 #endif
 
 /*Parameters of the Laplace-like probability models used for the coarse energy.
@@ -138,36 +137,41 @@ static const unsigned char e_prob_model[4][2][42] = {
    }
 };
 
-static int intra_decision(const celt_word16 *eBands, celt_word16 *oldEBands, int start, int end, int len, int C)
+static const unsigned char small_energy_icdf[3]={2,1,0};
+
+static opus_val32 loss_distortion(const opus_val16 *eBands, opus_val16 *oldEBands, int start, int end, int len, int C)
 {
    int c, i;
-   celt_word32 dist = 0;
+   opus_val32 dist = 0;
    c=0; do {
       for (i=start;i<end;i++)
       {
-         celt_word16 d = SHR16(SUB16(eBands[i+c*len], oldEBands[i+c*len]),2);
+         opus_val16 d = SHR16(SUB16(eBands[i+c*len], oldEBands[i+c*len]),2);
          dist = MAC16_16(dist, d,d);
       }
    } while (++c<C);
-   return SHR32(dist,2*DB_SHIFT-4) > 2*C*(end-start);
+   return MIN32(200,SHR32(dist,2*DB_SHIFT-4));
 }
 
-static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
-      const celt_word16 *eBands, celt_word16 *oldEBands, int budget,
-      const unsigned char *prob_model, celt_word16 *error, ec_enc *enc,
-      int _C, int LM, int intra, celt_word16 max_decay)
+static int quant_coarse_energy_impl(const CELTMode *m, int start, int end,
+      const opus_val16 *eBands, opus_val16 *oldEBands,
+      opus_int32 budget, opus_int32 tell,
+      const unsigned char *prob_model, opus_val16 *error, ec_enc *enc,
+      int _C, int LM, int intra, opus_val16 max_decay)
 {
    const int C = CHANNELS(_C);
    int i, c;
-   celt_word32 prev[2] = {0,0};
-   celt_word16 coef;
-   celt_word16 beta;
+   int badness = 0;
+   opus_val32 prev[2] = {0,0};
+   opus_val16 coef;
+   opus_val16 beta;
 
-   ec_enc_bit_prob(enc, intra, 8192);
+   if (tell+3 <= budget)
+      ec_enc_bit_logp(enc, intra, 3);
    if (intra)
    {
       coef = 0;
-      beta = QCONST16(.15f,15);
+      beta = beta_intra;
    } else {
       beta = beta_coef[LM];
       coef = pred_coef[LM];
@@ -179,135 +183,176 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
       c=0;
       do {
          int bits_left;
-         int qi;
-         int pi;
-         celt_word16 q;
-         celt_word16 x;
-         celt_word32 f;
+         int qi, qi0;
+         opus_val32 q;
+         opus_val16 x;
+         opus_val32 f, tmp;
+         opus_val16 oldE;
+         opus_val16 decay_bound;
          x = eBands[i+c*m->nbEBands];
+         oldE = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
 #ifdef FIXED_POINT
-         f = SHL32(EXTEND32(x),15) -MULT16_16(coef,oldEBands[i+c*m->nbEBands])-prev[c];
+         f = SHL32(EXTEND32(x),7) - PSHR32(MULT16_16(coef,oldE), 8) - prev[c];
          /* Rounding to nearest integer here is really important! */
-         qi = (f+QCONST32(.5,DB_SHIFT+15))>>(DB_SHIFT+15);
+         qi = (f+QCONST32(.5f,DB_SHIFT+7))>>(DB_SHIFT+7);
+         decay_bound = EXTRACT16(MAX32(-QCONST16(28.f,DB_SHIFT),
+               SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay)));
 #else
-         f = x-coef*oldEBands[i+c*m->nbEBands]-prev[c];
+         f = x-coef*oldE-prev[c];
          /* Rounding to nearest integer here is really important! */
          qi = (int)floor(.5f+f);
+         decay_bound = MAX16(-QCONST16(28.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]) - max_decay;
 #endif
          /* Prevent the energy from going down too quickly (e.g. for bands
             that have just one bin) */
-         if (qi < 0 && x < oldEBands[i+c*m->nbEBands]-max_decay)
+         if (qi < 0 && x < decay_bound)
          {
-            qi += (int)SHR16(oldEBands[i+c*m->nbEBands]-max_decay-x, DB_SHIFT);
+            qi += (int)SHR16(SUB16(decay_bound,x), DB_SHIFT);
             if (qi > 0)
                qi = 0;
          }
-         /* If we don't have enough bits to encode all the energy, just assume something safe.
-            We allow slightly busting the budget here */
-         bits_left = budget-(int)ec_enc_tell(enc, 0)-3*C*(end-i);
+         qi0 = qi;
+         /* If we don't have enough bits to encode all the energy, just assume
+             something safe. */
+         tell = ec_tell(enc);
+         bits_left = budget-tell-3*C*(end-i);
          if (i!=start && bits_left < 30)
          {
-            qi = IMAX(-1,qi);
             if (bits_left < 24)
                qi = IMIN(1, qi);
             if (bits_left < 16)
                qi = IMAX(-1, qi);
-            if (bits_left<8)
-               qi = 0;
          }
-         pi = 2*IMIN(i,20);
-         ec_laplace_encode(enc, &qi,
-               prob_model[pi]<<7, prob_model[pi+1]<<6);
-         error[i+c*m->nbEBands] = PSHR32(f,15) - SHL16(qi,DB_SHIFT);
-         q = SHL16(qi,DB_SHIFT);
-         
-         oldEBands[i+c*m->nbEBands] = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + SHL32(EXTEND32(q),15), 15);
-         prev[c] = prev[c] + SHL32(EXTEND32(q),15) - MULT16_16(beta,q);
+         if (budget-tell >= 15)
+         {
+            int pi;
+            pi = 2*IMIN(i,20);
+            ec_laplace_encode(enc, &qi,
+                  prob_model[pi]<<7, prob_model[pi+1]<<6);
+         }
+         else if(budget-tell >= 2)
+         {
+            qi = IMAX(-1, IMIN(qi, 1));
+            ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
+         }
+         else if(budget-tell >= 1)
+         {
+            qi = IMIN(0, qi);
+            ec_enc_bit_logp(enc, -qi, 1);
+         }
+         else
+            qi = -1;
+         error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT);
+         badness += abs(qi0-qi);
+         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
+
+         tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7);
+#ifdef FIXED_POINT
+         tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
+#endif
+         oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
+         prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
       } while (++c < C);
    }
+   return badness;
 }
 
 void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
-      const celt_word16 *eBands, celt_word16 *oldEBands, int budget,
-      celt_word16 *error, ec_enc *enc, int _C, int LM, int nbAvailableBytes,
-      int force_intra, int *delayedIntra, int two_pass)
+      const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget,
+      opus_val16 *error, ec_enc *enc, int _C, int LM, int nbAvailableBytes,
+      int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate)
 {
    const int C = CHANNELS(_C);
    int intra;
-   celt_word16 max_decay;
-   VARDECL(celt_word16, oldEBands_intra);
-   VARDECL(celt_word16, error_intra);
+   opus_val16 max_decay;
+   VARDECL(opus_val16, oldEBands_intra);
+   VARDECL(opus_val16, error_intra);
    ec_enc enc_start_state;
-   ec_byte_buffer buf_start_state;
+   opus_uint32 tell;
+   int badness1=0;
+   opus_int32 intra_bias;
+   opus_val32 new_distortion;
    SAVE_STACK;
 
-   intra = force_intra || (*delayedIntra && nbAvailableBytes > end*C);
-   if (/*shortBlocks || */intra_decision(eBands, oldEBands, start, effEnd, m->nbEBands, C))
-      *delayedIntra = 1;
-   else
-      *delayedIntra = 0;
+   intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C);
+   intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512));
+   new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C);
+
+   tell = ec_tell(enc);
+   if (tell+3 > budget)
+      two_pass = intra = 0;
 
    /* Encode the global flags using a simple probability model
       (first symbols in the stream) */
 
 #ifdef FIXED_POINT
-      max_decay = MIN32(QCONST16(16,DB_SHIFT), SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3));
+      max_decay = MIN32(QCONST16(16.f,DB_SHIFT), SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3));
 #else
    max_decay = MIN32(16.f, .125f*nbAvailableBytes);
 #endif
 
    enc_start_state = *enc;
-   buf_start_state = *(enc->buf);
 
-   ALLOC(oldEBands_intra, C*m->nbEBands, celt_word16);
-   ALLOC(error_intra, C*m->nbEBands, celt_word16);
-   CELT_COPY(oldEBands_intra, oldEBands, C*end);
+   ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16);
+   ALLOC(error_intra, C*m->nbEBands, opus_val16);
+   OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
 
    if (two_pass || intra)
    {
-      quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
-            e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay);
+      badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
+            tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay);
    }
 
    if (!intra)
    {
+      unsigned char *intra_buf;
       ec_enc enc_intra_state;
-      ec_byte_buffer buf_intra_state;
-      int tell_intra;
+      opus_int32 tell_intra;
+      opus_uint32 nstart_bytes;
+      opus_uint32 nintra_bytes;
+      int badness2;
       VARDECL(unsigned char, intra_bits);
 
-      tell_intra = ec_enc_tell(enc, 3);
+      tell_intra = ec_tell_frac(enc);
 
       enc_intra_state = *enc;
-      buf_intra_state = *(enc->buf);
 
-      ALLOC(intra_bits, buf_intra_state.ptr-buf_start_state.ptr, unsigned char);
+      nstart_bytes = ec_range_bytes(&enc_start_state);
+      nintra_bytes = ec_range_bytes(&enc_intra_state);
+      intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes;
+      ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char);
       /* Copy bits from intra bit-stream */
-      CELT_COPY(intra_bits, buf_start_state.ptr, buf_intra_state.ptr-buf_start_state.ptr);
+      OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes);
 
       *enc = enc_start_state;
-      *(enc->buf) = buf_start_state;
 
-      quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
-            e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay);
+      badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
+            tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay);
 
-      if (two_pass && ec_enc_tell(enc, 3) > tell_intra)
+      if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra)))
       {
          *enc = enc_intra_state;
-         *(enc->buf) = buf_intra_state;
-         /* Copy bits from to bit-stream */
-         CELT_COPY(buf_start_state.ptr, intra_bits, buf_intra_state.ptr-buf_start_state.ptr);
-         CELT_COPY(oldEBands, oldEBands_intra, C*end);
-         CELT_COPY(error, error_intra, C*end);
+         /* Copy intra bits to bit-stream */
+         OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes);
+         OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
+         OPUS_COPY(error, error_intra, C*m->nbEBands);
+         intra = 1;
       }
    } else {
-      CELT_COPY(oldEBands, oldEBands_intra, C*end);
-      CELT_COPY(error, error_intra, C*end);
+      OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
+      OPUS_COPY(error, error_intra, C*m->nbEBands);
    }
+
+   if (intra)
+      *delayedIntra = new_distortion;
+   else
+      *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra),
+            new_distortion);
+
    RESTORE_STACK;
 }
 
-void quant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, celt_word16 *error, int *fine_quant, ec_enc *enc, int _C)
+void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int _C)
 {
    int i, c;
    const int C = CHANNELS(_C);
@@ -315,13 +360,13 @@ void quant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands,
    /* Encode finer resolution */
    for (i=start;i<end;i++)
    {
-      celt_int16 frac = 1<<fine_quant[i];
+      opus_int16 frac = 1<<fine_quant[i];
       if (fine_quant[i] <= 0)
          continue;
       c=0;
       do {
          int q2;
-         celt_word16 offset;
+         opus_val16 offset;
 #ifdef FIXED_POINT
          /* Has to be without rounding */
          q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]);
@@ -334,7 +379,7 @@ void quant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands,
             q2 = 0;
          ec_enc_bits(enc, q2, fine_quant[i]);
 #ifdef FIXED_POINT
-         offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
+         offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
 #else
          offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
 #endif
@@ -345,7 +390,7 @@ void quant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands,
    }
 }
 
-void quant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, celt_word16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int _C)
+void quant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int _C)
 {
    int i, prio, c;
    const int C = CHANNELS(_C);
@@ -355,16 +400,16 @@ void quant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBa
    {
       for (i=start;i<end && bits_left>=C ;i++)
       {
-         if (fine_quant[i] >= 7 || fine_priority[i]!=prio)
+         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
             continue;
          c=0;
          do {
             int q2;
-            celt_word16 offset;
+            opus_val16 offset;
             q2 = error[i+c*m->nbEBands]<0 ? 0 : 1;
             ec_enc_bits(enc, q2, 1);
 #ifdef FIXED_POINT
-            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5,DB_SHIFT),fine_quant[i]+1);
+            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
 #else
             offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
 #endif
@@ -375,45 +420,69 @@ void quant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBa
    }
 }
 
-void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, int intra, ec_dec *dec, int _C, int LM)
+void unquant_coarse_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int intra, ec_dec *dec, int _C, int LM)
 {
    const unsigned char *prob_model = e_prob_model[LM][intra];
    int i, c;
-   celt_word32 prev[2] = {0, 0};
-   celt_word16 coef;
-   celt_word16 beta;
+   opus_val32 prev[2] = {0, 0};
+   opus_val16 coef;
+   opus_val16 beta;
    const int C = CHANNELS(_C);
-
+   opus_int32 budget;
+   opus_int32 tell;
 
    if (intra)
    {
       coef = 0;
-      beta = QCONST16(.15f,15);
+      beta = beta_intra;
    } else {
       beta = beta_coef[LM];
       coef = pred_coef[LM];
    }
 
+   budget = dec->storage*8;
+
    /* Decode at a fixed coarse resolution */
    for (i=start;i<end;i++)
    {
       c=0;
       do {
          int qi;
-         int pi;
-         celt_word16 q;
-         pi = 2*IMIN(i,20);
-         qi = ec_laplace_decode(dec,
-               prob_model[pi]<<7, prob_model[pi+1]<<6);
-         q = SHL16(qi,DB_SHIFT);
-
-         oldEBands[i+c*m->nbEBands] = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + SHL32(EXTEND32(q),15), 15);
-         prev[c] = prev[c] + SHL32(EXTEND32(q),15) - MULT16_16(beta,q);
+         opus_val32 q;
+         opus_val32 tmp;
+         tell = ec_tell(dec);
+         if(budget-tell>=15)
+         {
+            int pi;
+            pi = 2*IMIN(i,20);
+            qi = ec_laplace_decode(dec,
+                  prob_model[pi]<<7, prob_model[pi+1]<<6);
+         }
+         else if(budget-tell>=2)
+         {
+            qi = ec_dec_icdf(dec, small_energy_icdf, 2);
+            qi = (qi>>1)^-(qi&1);
+         }
+         else if(budget-tell>=1)
+         {
+            qi = -ec_dec_bit_logp(dec, 1);
+         }
+         else
+            qi = -1;
+         q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT);
+
+         oldEBands[i+c*m->nbEBands] = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]);
+         tmp = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]),8) + prev[c] + SHL32(q,7);
+#ifdef FIXED_POINT
+         tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp);
+#endif
+         oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7);
+         prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8));
       } while (++c < C);
    }
 }
 
-void unquant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, int *fine_quant, ec_dec *dec, int _C)
+void unquant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, ec_dec *dec, int _C)
 {
    int i, c;
    const int C = CHANNELS(_C);
@@ -422,13 +491,13 @@ void unquant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBand
    {
       if (fine_quant[i] <= 0)
          continue;
-      c=0; 
+      c=0;
       do {
          int q2;
-         celt_word16 offset;
+         opus_val16 offset;
          q2 = ec_dec_bits(dec, fine_quant[i]);
 #ifdef FIXED_POINT
-         offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
+         offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT));
 #else
          offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f;
 #endif
@@ -437,7 +506,7 @@ void unquant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBand
    }
 }
 
-void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBands, celt_word16 *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec, int _C)
+void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec, int _C)
 {
    int i, prio, c;
    const int C = CHANNELS(_C);
@@ -447,15 +516,15 @@ void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *e
    {
       for (i=start;i<end && bits_left>=C ;i++)
       {
-         if (fine_quant[i] >= 7 || fine_priority[i]!=prio)
+         if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio)
             continue;
          c=0;
          do {
             int q2;
-            celt_word16 offset;
+            opus_val16 offset;
             q2 = ec_dec_bits(dec, 1);
 #ifdef FIXED_POINT
-            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5,DB_SHIFT),fine_quant[i]+1);
+            offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1);
 #else
             offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384);
 #endif
@@ -467,25 +536,27 @@ void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *e
 }
 
 void log2Amp(const CELTMode *m, int start, int end,
-      celt_ener *eBands, celt_word16 *oldEBands, int _C)
+      celt_ener *eBands, opus_val16 *oldEBands, int _C)
 {
    int c, i;
    const int C = CHANNELS(_C);
    c=0;
    do {
-      for (i=start;i<m->nbEBands;i++)
+      for (i=0;i<start;i++)
+         eBands[i+c*m->nbEBands] = 0;
+      for (;i<end;i++)
       {
-         celt_word16 lg = oldEBands[i+c*m->nbEBands]
-                        + SHL16((celt_word16)eMeans[i],6);
-         eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(SHL16(lg,11-DB_SHIFT)),4);
-         if (oldEBands[i+c*m->nbEBands] < -QCONST16(14.f,DB_SHIFT))
-            oldEBands[i+c*m->nbEBands] = -QCONST16(14.f,DB_SHIFT);
+         opus_val16 lg = ADD16(oldEBands[i+c*m->nbEBands],
+                         SHL16((opus_val16)eMeans[i],6));
+         eBands[i+c*m->nbEBands] = PSHR32(celt_exp2(lg),4);
       }
+      for (;i<m->nbEBands;i++)
+         eBands[i+c*m->nbEBands] = 0;
    } while (++c < C);
 }
 
 void amp2Log2(const CELTMode *m, int effEnd, int end,
-      celt_ener *bandE, celt_word16 *bandLogE, int _C)
+      celt_ener *bandE, opus_val16 *bandLogE, int _C)
 {
    int c, i;
    const int C = CHANNELS(_C);
@@ -493,8 +564,8 @@ void amp2Log2(const CELTMode *m, int effEnd, int end,
    do {
       for (i=0;i<effEnd;i++)
          bandLogE[i+c*m->nbEBands] =
-               celt_log2(MAX32(QCONST32(.001f,14),SHL32(bandE[i+c*m->nbEBands],2)))
-               - SHL16((celt_word16)eMeans[i],6);
+               celt_log2(SHL32(bandE[i+c*m->nbEBands],2))
+               - SHL16((opus_val16)eMeans[i],6);
       for (i=effEnd;i<end;i++)
          bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT);
    } while (++c < C);