Taking into account the percentage of packet loss for intra decision
[opus.git] / libcelt / quant_bands.c
index 937692f..2641a1b 100644 (file)
    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
@@ -66,9 +62,11 @@ static const celt_word16 eMeans[25] = {
 #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 celt_word16 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 celt_word16 beta_intra = 4915/32768.;
 #endif
 
 /*Parameters of the Laplace-like probability models used for the coarse energy.
@@ -141,7 +139,7 @@ static const unsigned char e_prob_model[4][2][42] = {
 
 static const unsigned char small_energy_icdf[3]={2,1,0};
 
-static int intra_decision(const celt_word16 *eBands, celt_word16 *oldEBands, int start, int end, int len, int C)
+static celt_word32 intra_decision(const celt_word16 *eBands, celt_word16 *oldEBands, int start, int end, int len, int C)
 {
    int c, i;
    celt_word32 dist = 0;
@@ -152,17 +150,18 @@ static int intra_decision(const celt_word16 *eBands, celt_word16 *oldEBands, int
          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,
+static int quant_coarse_energy_impl(const CELTMode *m, int start, int end,
       const celt_word16 *eBands, celt_word16 *oldEBands,
-      ec_int32 budget, ec_int32 tell,
+      celt_int32 budget, celt_int32 tell,
       const unsigned char *prob_model, celt_word16 *error, ec_enc *enc,
       int _C, int LM, int intra, celt_word16 max_decay)
 {
    const int C = CHANNELS(_C);
    int i, c;
+   int badness = 0;
    celt_word32 prev[2] = {0,0};
    celt_word16 coef;
    celt_word16 beta;
@@ -172,7 +171,7 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
    if (intra)
    {
       coef = 0;
-      beta = QCONST16(.15f,15);
+      beta = beta_intra;
    } else {
       beta = beta_coef[LM];
       coef = pred_coef[LM];
@@ -184,31 +183,38 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
       c=0;
       do {
          int bits_left;
-         int qi;
-         celt_word16 q;
+         int qi, qi0;
+         celt_word32 q;
          celt_word16 x;
-         celt_word32 f;
+         celt_word32 f, tmp;
+         celt_word16 oldE;
+         celt_word16 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((celt_word32)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;
          }
+         qi0 = qi;
          /* If we don't have enough bits to encode all the energy, just assume
              something safe. */
-         tell = ec_enc_tell(enc, 0);
+         tell = ec_tell(enc);
          bits_left = budget-tell-3*C*(end-i);
          if (i!=start && bits_left < 30)
          {
@@ -236,19 +242,25 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
          }
          else
             qi = -1;
-         error[i+c*m->nbEBands] = PSHR32(f,15) - SHL16(qi,DB_SHIFT);
-         q = SHL16(qi,DB_SHIFT);
+         error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT);
+         badness += abs(qi0-qi);
+         q = SHL32(EXTEND32(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);
+         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, ec_uint32 budget,
+      const celt_word16 *eBands, celt_word16 *oldEBands, celt_uint32 budget,
       celt_word16 *error, ec_enc *enc, int _C, int LM, int nbAvailableBytes,
-      int force_intra, int *delayedIntra, int two_pass)
+      int force_intra, celt_word32 *delayedIntra, int two_pass, int loss_rate)
 {
    const int C = CHANNELS(_C);
    int intra;
@@ -256,17 +268,16 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
    VARDECL(celt_word16, oldEBands_intra);
    VARDECL(celt_word16, error_intra);
    ec_enc enc_start_state;
-   ec_byte_buffer buf_start_state;
-   ec_uint32 tell;
+   celt_uint32 tell;
+   int badness1=0;
+   celt_int32 intra_bias;
    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 = ((budget**delayedIntra*loss_rate)/(C*512));
+   *delayedIntra = intra_decision(eBands, oldEBands, start, effEnd, m->nbEBands, C);
 
-   tell = ec_enc_tell(enc, 0);
+   tell = ec_tell(enc);
    if (tell+3 > budget)
       two_pass = intra = 0;
 
@@ -274,70 +285,66 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
       (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);
+   CELT_COPY(oldEBands_intra, oldEBands, C*m->nbEBands);
 
    if (two_pass || intra)
    {
-      quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
+      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)
    {
       ec_enc enc_intra_state;
-      ec_byte_buffer buf_intra_state;
       int tell_intra;
-      ec_uint32 nstart_bytes;
-      ec_uint32 nintra_bytes;
+      celt_uint32 nstart_bytes;
+      celt_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);
 
-      nstart_bytes = ec_byte_bytes(&buf_start_state);
-      nintra_bytes = ec_byte_bytes(&buf_intra_state);
+      nstart_bytes = ec_range_bytes(&enc_start_state);
+      nintra_bytes = ec_range_bytes(&enc_intra_state);
       ALLOC(intra_bits, nintra_bytes-nstart_bytes, unsigned char);
       /* Copy bits from intra bit-stream */
       CELT_COPY(intra_bits,
-            ec_byte_get_buffer(&buf_intra_state) + nstart_bytes,
+            ec_get_buffer(&enc_intra_state) + nstart_bytes,
             nintra_bytes - nstart_bytes);
 
       *enc = enc_start_state;
-      *(enc->buf) = buf_start_state;
 
-      quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
+      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 && ec_tell_frac(enc)+intra_bias > tell_intra)))
       {
          *enc = enc_intra_state;
-         *(enc->buf) = buf_intra_state;
          /* Copy intra bits to bit-stream */
-         CELT_COPY(ec_byte_get_buffer(&buf_intra_state) + nstart_bytes,
+         CELT_COPY(ec_get_buffer(&enc_intra_state) + nstart_bytes,
                intra_bits, nintra_bytes - nstart_bytes);
-         CELT_COPY(oldEBands, oldEBands_intra, C*end);
-         CELT_COPY(error, error_intra, C*end);
+         CELT_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
+         CELT_COPY(error, error_intra, C*m->nbEBands);
       }
    } else {
-      CELT_COPY(oldEBands, oldEBands_intra, C*end);
-      CELT_COPY(error, error_intra, C*end);
+      CELT_COPY(oldEBands, oldEBands_intra, C*m->nbEBands);
+      CELT_COPY(error, error_intra, C*m->nbEBands);
    }
    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, celt_word16 *oldEBands, celt_word16 *error, int *fine_quant, ec_enc *enc, int _C)
 {
    int i, c;
    const int C = CHANNELS(_C);
@@ -364,7 +371,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
@@ -375,7 +382,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, celt_word16 *oldEBands, celt_word16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int _C)
 {
    int i, prio, c;
    const int C = CHANNELS(_C);
@@ -394,7 +401,7 @@ void quant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *eBa
             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
@@ -405,7 +412,7 @@ 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, celt_word16 *oldEBands, int intra, ec_dec *dec, int _C, int LM)
 {
    const unsigned char *prob_model = e_prob_model[LM][intra];
    int i, c;
@@ -413,20 +420,20 @@ void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBa
    celt_word16 coef;
    celt_word16 beta;
    const int C = CHANNELS(_C);
-   ec_int32 budget;
-   ec_int32 tell;
+   celt_int32 budget;
+   celt_int32 tell;
 
 
    if (intra)
    {
       coef = 0;
-      beta = QCONST16(.15f,15);
+      beta = beta_intra;
    } else {
       beta = beta_coef[LM];
       coef = pred_coef[LM];
    }
 
-   budget = dec->buf->storage*8;
+   budget = dec->storage*8;
 
    /* Decode at a fixed coarse resolution */
    for (i=start;i<end;i++)
@@ -434,8 +441,9 @@ void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBa
       c=0;
       do {
          int qi;
-         celt_word16 q;
-         tell = ec_dec_tell(dec, 0);
+         celt_word32 q;
+         celt_word32 tmp;
+         tell = ec_tell(dec);
          if(budget-tell>=15)
          {
             int pi;
@@ -454,15 +462,20 @@ void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBa
          }
          else
             qi = -1;
-         q = SHL16(qi,DB_SHIFT);
+         q = SHL32(EXTEND32(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);
+         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, celt_word16 *oldEBands, int *fine_quant, ec_dec *dec, int _C)
 {
    int i, c;
    const int C = CHANNELS(_C);
@@ -477,7 +490,7 @@ void unquant_fine_energy(const CELTMode *m, int start, int end, celt_ener *eBand
          celt_word16 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
@@ -486,7 +499,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, celt_word16 *oldEBands, int *fine_quant,  int *fine_priority, int bits_left, ec_dec *dec, int _C)
 {
    int i, prio, c;
    const int C = CHANNELS(_C);
@@ -504,7 +517,7 @@ void unquant_energy_finalise(const CELTMode *m, int start, int end, celt_ener *e
             celt_word16 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
@@ -522,14 +535,16 @@ void log2Amp(const CELTMode *m, int start, int end,
    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);
+         celt_word16 lg = ADD16(oldEBands[i+c*m->nbEBands],
+                         SHL16((celt_word16)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);
 }
 
@@ -542,7 +557,7 @@ 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)))
+               celt_log2(SHL32(bandE[i+c*m->nbEBands],2))
                - SHL16((celt_word16)eMeans[i],6);
       for (i=effEnd;i<end;i++)
          bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT);