shithub: opus

Download patch

ref: abe40f02a93968c2a2e4f8a19c1deceaff9567c3
parent: 8256ed4cf745436f4c9b9cd5857917614193f22c
author: Gregory Maxwell <[email protected]>
date: Fri Dec 12 20:31:32 EST 2008

Minor pitch handling cleanups.

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -372,6 +372,7 @@
 #endif
    int i, c, N, N4;
    int has_pitch;
+   int id;
    int pitch_index;
    int bits;
    int has_fold=1;
@@ -554,27 +555,24 @@
 
    /* Check if we can safely use the pitch (i.e. effective gain isn't too high) */
    curr_power = bandE[0]+bandE[1]+bandE[2];
+   has_pitch = 0;
    if (st->pitch_enabled && !shortBlocks && (MULT16_32_Q15(QCONST16(.1f, 15),curr_power) + QCONST32(10.f,ENER_SHIFT) < pitch_power))
    {
-      int id;
-
       /* Pitch prediction */
       compute_pitch_gain(st->mode, X, P, gains);
-      id = quant_pitch(gains, st->mode->nbPBands, &enc);
-      if (id != -1)
+      id = quant_pitch(gains, st->mode->nbPBands);
+      if (id > -1)
          has_pitch = 1;
-      else
-         has_pitch = 0;
+   }
+   
+   if (has_pitch) 
+   {  
+      unquant_pitch(id, gains, st->mode->nbPBands);
       ec_enc_bits(&enc, has_pitch, 1); /* Pitch flag */
-      if (has_pitch)
-      {
-         ec_enc_bits(&enc, has_fold, 1); /* Folding flag */
-         ec_enc_bits(&enc, id, 7);
-         ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4));
-      } else if (st->mode->nbShortMdcts > 1) {
-         ec_enc_bits(&enc, 0, 1); /* Transient off */
-         has_fold = 1;
-      }
+      ec_enc_bits(&enc, has_fold, 1); /* Folding flag */
+      ec_enc_bits(&enc, id, 7);
+      ec_enc_uint(&enc, pitch_index, MAX_PERIOD-(2*N-2*N4));
+      pitch_quant_bands(st->mode, P, gains);
    } else {
       if (!shortBlocks)
       {
@@ -624,8 +622,6 @@
 
    quant_fine_energy(st->mode, bandE, st->oldBandE, error, fine_quant, &enc);
 
-   pitch_quant_bands(st->mode, P, gains);
-
    /* Residual quantisation */
    quant_bands(st->mode, X, P, NULL, bandE, stereo_mode, pulses, shortBlocks, has_fold, nbCompressedBytes*8, &enc);
 
@@ -972,16 +968,16 @@
       transient_time = -1;
       transient_shift = 0;
    }
-   /* Get the pitch gains */
    
-   /* Get the pitch index */
    if (has_pitch)
    {
-      has_pitch = unquant_pitch(gains, st->mode->nbPBands, &dec);
+      int id;
+      /* Get the pitch gains and index */
+      id = ec_dec_bits(&dec, 7);
+      unquant_pitch(id, gains, st->mode->nbPBands);
       pitch_index = ec_dec_uint(&dec, MAX_PERIOD-(2*N-2*N4));
       st->last_pitch_index = pitch_index;
    } else {
-      /* FIXME: We could be more intelligent here and just not compute the MDCT */
       pitch_index = 0;
       for (i=0;i<st->mode->nbPBands;i++)
          gains[i] = 0;
@@ -1016,13 +1012,12 @@
       ALLOC(bandEp, st->mode->nbEBands*C, celt_ener_t);
       compute_band_energies(st->mode, freq, bandEp);
       normalise_bands(st->mode, freq, P, bandEp);
+      /* Apply pitch gains */
+      pitch_quant_bands(st->mode, P, gains);
    } else {
       for (i=0;i<C*N;i++)
          P[i] = 0;
    }
-
-   /* Apply pitch gains */
-   pitch_quant_bands(st->mode, P, gains);
 
    /* Decode fixed codebook and merge with pitch */
    unquant_bands(st->mode, X, P, bandE, stereo_mode, pulses, shortBlocks, has_fold, len*8, &dec);
--- a/libcelt/quant_pitch.c
+++ b/libcelt/quant_pitch.c
@@ -84,16 +84,8 @@
    return best_index;
 }
 
-/** Returns the pitch gain vector corresponding to a certain id */
-static void id2gains(int id, celt_pgain_t *gains, int len)
+int quant_pitch(celt_pgain_t *gains, int len)
 {
-   int i;
-   for (i=0;i<len;i++)
-      gains[i] = celt_sqrt(Q1515ONE-MULT16_16(Q15ONE-PGAIN(pgain_table,id*len+i),Q15ONE-PGAIN(pgain_table,id*len+i)));
-}
-
-int quant_pitch(celt_pgain_t *gains, int len, ec_enc *enc)
-{
    int i, id;
    celt_word32_t gain_sum = 0;
    /*for (i=0;i<len;i++) printf ("%f ", gains[i]);printf ("\n");*/
@@ -107,21 +99,19 @@
    if (gain_sum > QCONST32(.3f,15))
    {
       id = vq_index(gains, pgain_table, len, 128);
+      /* FIXME: Remove when we're not waisting a transmitted index on 0 gains */
+      if (id==0)
+        id = -1;
    } else {
-      id = 0;
+      id = -1;
    }
-   /*for (i=0;i<len;i++) printf ("%f ", pgain_table[id*len+i]);printf ("\n");*/
-   id2gains(id, gains, len);
-   if (id != 0)
-      return id;
-   else
-      return -1;
+   return id;
 }
 
-int unquant_pitch(celt_pgain_t *gains, int len, ec_dec *dec)
+/** Returns the pitch gain vector corresponding to a certain id */
+void unquant_pitch(int id, celt_pgain_t *gains, int len)
 {
-   int id;
-   id = ec_dec_bits(dec, 7);
-   id2gains(id, gains, len);
-   return id!=0;
+   int i;
+   for (i=0;i<len;i++)
+      gains[i] = celt_sqrt(Q1515ONE-MULT16_16(Q15ONE-PGAIN(pgain_table,id*len+i),Q15ONE-PGAIN(pgain_table,id*len+i)));
 }
--- a/libcelt/quant_pitch.h
+++ b/libcelt/quant_pitch.h
@@ -36,10 +36,9 @@
 #include "entenc.h"
 #include "entdec.h"
 
-/** If this returns 0, the gain is zero (don't encode the pitch index) */
-int quant_pitch(celt_pgain_t *gains, int len, ec_enc *enc);
+/** If this returns -1, the gain is zero (don't encode the pitch index) */
+int quant_pitch(celt_pgain_t *gains, int len);
 
-/** If this returns 0, the gain is zero (don't decode the pitch index) */
-int unquant_pitch(celt_pgain_t *gains, int len, ec_dec *dec);
+void unquant_pitch(int id, celt_pgain_t *gains, int len);
 
 #endif /* QUANT_PITCH_H */