shithub: opus

Download patch

ref: 8d367029a7b39d5365a3a6034a01a107c75be88a
parent: dfa847a25de8f75370a10b12533d8f6fc87ff733
author: Jean-Marc Valin <[email protected]>
date: Mon Jan 17 11:37:51 EST 2011

Adding tapset decision logic

Based on spreading_decision()'s logic. We choose tapsets
with less roll-off when we think the HF are tonal.

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -300,7 +300,9 @@
 }
 
 /* Decide whether we should spread the pulses in the current frame */
-int spreading_decision(const CELTMode *m, celt_norm *X, int *average, int last_decision, int end, int _C, int M)
+int spreading_decision(const CELTMode *m, celt_norm *X, int *average,
+      int last_decision, int *hf_average, int *tapset_decision, int update_hf,
+      int end, int _C, int M)
 {
    int i, c, N0;
    int sum = 0, nbBands=0;
@@ -307,6 +309,7 @@
    const int C = CHANNELS(_C);
    const celt_int16 * restrict eBands = m->eBands;
    int decision;
+   int hf_sum=0;
    
    N0 = M*m->shortMdctSize;
 
@@ -335,11 +338,33 @@
                tcount[2]++;
          }
 
+         /* Only include four last bands (8 kHz and up) */
+         if (i>m->nbEBands-4)
+            hf_sum += 32*(tcount[1]+tcount[0])/N;
          tmp = (2*tcount[2] >= N) + (2*tcount[1] >= N) + (2*tcount[0] >= N);
          sum += tmp*256;
          nbBands++;
       }
    } while (++c<C);
+
+   if (update_hf)
+   {
+      if (hf_sum)
+         hf_sum /= C*(4-m->nbEBands+end);
+      *hf_average = (*hf_average+hf_sum)>>1;
+      hf_sum = *hf_average;
+      if (*tapset_decision==2)
+         hf_sum += 4;
+      else if (*tapset_decision==0)
+         hf_sum -= 4;
+      if (hf_sum > 22)
+         *tapset_decision=2;
+      else if (hf_sum > 18)
+         *tapset_decision=1;
+      else
+         *tapset_decision=0;
+   }
+   /*printf("%d %d %d\n", hf_sum, *hf_average, *tapset_decision);*/
    sum /= nbBands;
    /* Recursive averaging */
    sum = (sum+*average)>>1;
--- a/libcelt/bands.h
+++ b/libcelt/bands.h
@@ -69,7 +69,9 @@
 #define SPREAD_NORMAL     (2)
 #define SPREAD_AGGRESSIVE (3)
 
-int spreading_decision(const CELTMode *m, celt_norm *X, int *average, int last_decision, int end, int _C, int M);
+int spreading_decision(const CELTMode *m, celt_norm *X, int *average,
+      int last_decision, int *hf_average, int *tapset_decision, int update_hf,
+      int end, int _C, int M);
 
 #ifdef MEASURE_NORM_MSE
 void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, float *bandE0, int M, int N, int C);
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -83,6 +83,8 @@
    int delayedIntra;
    int tonal_average;
    int lastCodedBands;
+   int hf_average;
+   int tapset_decision;
 
    int prefilter_period;
    celt_word16 prefilter_gain;
@@ -160,6 +162,8 @@
    st->delayedIntra = 1;
    st->tonal_average = 256;
    st->spread_decision = SPREAD_NORMAL;
+   st->hf_average = 0;
+   st->tapset_decision = 0;
    st->complexity = 5;
 
    if (error)
@@ -809,7 +813,8 @@
    celt_int32 total_bits;
    celt_int32 total_boost;
    celt_int32 tell;
-   int prefilter_tapset;
+   int prefilter_tapset=0;
+   int pf_on;
    SAVE_STACK;
 
    if (nbCompressedBytes<0 || pcm==NULL)
@@ -927,7 +932,7 @@
          if (pitch_index > COMBFILTER_MAXPERIOD-2)
             pitch_index = COMBFILTER_MAXPERIOD-2;
          gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
-         prefilter_tapset = 0;
+         prefilter_tapset = st->tapset_decision;
       } else {
          gain1 = 0;
       }
@@ -954,6 +959,7 @@
          if(tell+15<=total_bits)
             ec_enc_bit_logp(enc, 0, 1);
          gain1 = 0;
+         pf_on = 0;
       } else {
          int qg;
          int octave;
@@ -979,11 +985,13 @@
          ec_enc_bits(enc, prefilter_tapset!=0, 1);
          if (prefilter_tapset!=0)
             ec_enc_bits(enc, prefilter_tapset>1, 1);
+         pf_on = 1;
       }
       /*printf("%d %f\n", pitch_index, gain1);*/
 #else /* ENABLE_POSTFILTER */
-      if(tell+15<=total_bits)
+      if(tell+17<=total_bits)
          ec_enc_bit_logp(enc, 0, 1);
+      pf_on = 0;
 #endif /* ENABLE_POSTFILTER */
 
       c=0; do {
@@ -1068,7 +1076,8 @@
             st->spread_decision = SPREAD_NONE;
       } else {
          st->spread_decision = spreading_decision(st->mode, X,
-               &st->tonal_average, st->spread_decision, effEnd, C, M);
+               &st->tonal_average, st->spread_decision, &st->hf_average,
+               &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
       }
       ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
    }
@@ -1934,7 +1943,7 @@
    postfilter_gain = 0;
    postfilter_pitch = 0;
    postfilter_tapset = 0;
-   if (tell+15 <= total_bits)
+   if (tell+17 <= total_bits)
    {
       if(ec_dec_bit_logp(dec, 1))
       {