shithub: opus

Download patch

ref: c890b58b69a340d33b6ee8eceaa617b9535b0bdf
parent: 7364e758ed15fa499021d2823e0efc6024c8dd66
author: Jean-Marc Valin <[email protected]>
date: Fri Aug 1 18:26:49 EDT 2008

Completed the separation of coarse and fine energy quantisation

--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -361,6 +361,7 @@
    int i, c, N, N4;
    int has_pitch;
    int pitch_index;
+   int bits;
    celt_word32_t curr_power, pitch_power;
    VARDECL(celt_sig_t, in);
    VARDECL(celt_sig_t, freq);
@@ -369,6 +370,8 @@
    VARDECL(celt_ener_t, bandE);
    VARDECL(celt_pgain_t, gains);
    VARDECL(int, stereo_mode);
+   VARDECL(celt_int16_t, fine_quant);
+   VARDECL(celt_word16_t, error);
 #ifdef EXP_PSY
    VARDECL(celt_word32_t, mask);
 #endif
@@ -525,8 +528,14 @@
       for (i=0;i<C*N;i++)
          P[i] = 0;
    }
-   quant_energy(st->mode, bandE, st->oldBandE, 20*C+nbCompressedBytes*8/5, st->mode->prob, &st->enc);
 
+   ALLOC(fine_quant, st->mode->nbEBands, celt_int16_t);
+   ALLOC(error, C*st->mode->nbEBands, celt_word16_t);
+   bits = ec_enc_tell(&st->enc, 0);
+   quant_coarse_energy(st->mode, bandE, st->oldBandE, 20*C+nbCompressedBytes*8, st->mode->prob, error, &st->enc);
+   compute_fine_allocation(st->mode, fine_quant, (20*C+nbCompressedBytes*8/5-(ec_enc_tell(&st->enc, 0)-bits))/C);
+   quant_fine_energy(st->mode, bandE, st->oldBandE, error, fine_quant, &st->enc);
+
    ALLOC(stereo_mode, st->mode->nbEBands, int);
    stereo_decision(st->mode, X, stereo_mode, st->mode->nbEBands);
 
@@ -735,6 +744,7 @@
    int c, N, N4;
    int has_pitch;
    int pitch_index;
+   int bits;
    ec_dec dec;
    ec_byte_buffer buf;
    VARDECL(celt_sig_t, freq);
@@ -743,6 +753,8 @@
    VARDECL(celt_ener_t, bandE);
    VARDECL(celt_pgain_t, gains);
    VARDECL(int, stereo_mode);
+   VARDECL(celt_int16_t, fine_quant);
+
    int shortBlocks;
    int transient_time;
    int transient_shift;
@@ -808,8 +820,12 @@
       pitch_index = 0;
    }
 
+   ALLOC(fine_quant, st->mode->nbEBands, celt_int16_t);
+   bits = ec_dec_tell(&dec, 0);
    /* Get band energies */
-   unquant_energy(st->mode, bandE, st->oldBandE, 20*C+len*8/5, st->mode->prob, &dec);
+   unquant_coarse_energy(st->mode, bandE, st->oldBandE, 20*C+len*8, st->mode->prob, &dec);
+   compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(&dec, 0)-bits))/C);
+   unquant_fine_energy(st->mode, bandE, st->oldBandE, fine_quant, &dec);
 
    /* Pitch MDCT */
    compute_mdcts(st->mode, 0, st->out_mem+pitch_index*C, freq);
--- a/libcelt/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -50,7 +50,7 @@
 /*const int frac[24] = {4, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};*/
 /*const int frac[24] = {8, 6, 5, 4, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};*/
 
-static void compute_fine_allocation(const CELTMode *m, celt_int16_t *bits, int budget)
+void compute_fine_allocation(const CELTMode *m, celt_int16_t *bits, int budget)
 {
    int i,j;
    int len;
@@ -274,22 +274,38 @@
 
 
 
-void quant_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, ec_enc *enc)
+void quant_coarse_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, celt_word16_t *error, ec_enc *enc)
 {
    int C;
-   VARDECL(celt_int16_t, fine_quant);
-   VARDECL(celt_word16_t, error);
-   SAVE_STACK;
-
-   ALLOC(fine_quant, m->nbEBands, celt_int16_t);
-   ALLOC(error, m->nbEBands, celt_word16_t);
    C = m->nbChannels;
 
    if (C==1)
    {
-      int bits = ec_enc_tell(enc, 0);
       quant_coarse_energy_mono(m, eBands, oldEBands, budget, prob, error, enc);
-      compute_fine_allocation(m, fine_quant, budget-(ec_enc_tell(enc, 0)-bits));
+
+   } else {
+      int c;
+      for (c=0;c<C;c++)
+      {
+         int i;
+         VARDECL(celt_ener_t, E);
+         SAVE_STACK;
+         ALLOC(E, m->nbEBands, celt_ener_t);
+         for (i=0;i<m->nbEBands;i++)
+            E[i] = eBands[C*i+c];
+         quant_coarse_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, prob, error+c*m->nbEBands, enc);
+      }
+      RESTORE_STACK;
+   }
+}
+
+void quant_fine_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, celt_word16_t *error, celt_int16_t *fine_quant, ec_enc *enc)
+{
+   int C;
+   C = m->nbChannels;
+
+   if (C==1)
+   {
       quant_fine_energy_mono(m, eBands, oldEBands, error, fine_quant, enc);
 
    } else {
@@ -298,53 +314,61 @@
       ALLOC(E, m->nbEBands, celt_ener_t);
       for (c=0;c<C;c++)
       {
-         int i, bits;
+         int i;
+         SAVE_STACK;
+         quant_fine_energy_mono(m, E, oldEBands+c*m->nbEBands, error+c*m->nbEBands, fine_quant, enc);
          for (i=0;i<m->nbEBands;i++)
-            E[i] = eBands[C*i+c];
-         bits = ec_enc_tell(enc, 0);
-         quant_coarse_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, prob, error, enc);
-         compute_fine_allocation(m, fine_quant, budget/C-(ec_enc_tell(enc, 0)-bits));
-         quant_fine_energy_mono(m, E, oldEBands+c*m->nbEBands, error, fine_quant, enc);
-         for (i=0;i<m->nbEBands;i++)
             eBands[C*i+c] = E[i];
       }
+      RESTORE_STACK;
    }
-   RESTORE_STACK;
 }
 
 
+void unquant_coarse_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, ec_dec *dec)
+{
+   int C;   
 
-void unquant_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, ec_dec *dec)
+   C = m->nbChannels;
+   if (C==1)
+   {
+      unquant_coarse_energy_mono(m, eBands, oldEBands, budget, prob, dec);
+   }
+   else {
+      int c;
+      VARDECL(celt_ener_t, E);
+      SAVE_STACK;
+      ALLOC(E, m->nbEBands, celt_ener_t);
+      for (c=0;c<C;c++)
+      {
+         unquant_coarse_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, prob, dec);
+      }
+      RESTORE_STACK;
+   }
+}
+
+void unquant_fine_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, celt_int16_t *fine_quant, ec_dec *dec)
 {
    int C;   
-   VARDECL(celt_int16_t, fine_quant);
-   SAVE_STACK;
 
    C = m->nbChannels;
-   ALLOC(fine_quant, m->nbEBands, celt_int16_t);
 
    if (C==1)
    {
-      int bits = ec_dec_tell(dec, 0);
-      unquant_coarse_energy_mono(m, eBands, oldEBands, budget, prob, dec);
-      compute_fine_allocation(m, fine_quant, budget-(ec_dec_tell(dec, 0)-bits));
       unquant_fine_energy_mono(m, eBands, oldEBands, fine_quant, dec);
    }
    else {
       int c;
       VARDECL(celt_ener_t, E);
+      SAVE_STACK;
       ALLOC(E, m->nbEBands, celt_ener_t);
       for (c=0;c<C;c++)
       {
          int i;
-         int bits = ec_dec_tell(dec, 0);
-         unquant_coarse_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, prob, dec);
-         compute_fine_allocation(m, fine_quant, budget/C-(ec_dec_tell(dec, 0)-bits));
          unquant_fine_energy_mono(m, E, oldEBands+c*m->nbEBands, fine_quant, dec);
-         //unquant_energy_mono(m, E, oldEBands+c*m->nbEBands, budget/C, prob, dec);
          for (i=0;i<m->nbEBands;i++)
             eBands[C*i+c] = E[i];
       }
+      RESTORE_STACK;
    }
-   RESTORE_STACK;
 }
--- a/libcelt/quant_bands.h
+++ b/libcelt/quant_bands.h
@@ -40,8 +40,14 @@
 int *quant_prob_alloc(const CELTMode *m);
 void quant_prob_free(int *freq);
 
-void quant_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, ec_enc *enc);
+void compute_fine_allocation(const CELTMode *m, celt_int16_t *bits, int budget);
 
-void unquant_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, ec_dec *dec);
+void quant_coarse_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, celt_word16_t *error, ec_enc *enc);
+
+void quant_fine_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, celt_word16_t *error, celt_int16_t *fine_quant, ec_enc *enc);
+
+void unquant_coarse_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, int budget, int *prob, ec_dec *dec);
+
+void unquant_fine_energy(const CELTMode *m, celt_ener_t *eBands, celt_word16_t *oldEBands, celt_int16_t *fine_quant, ec_dec *dec);
 
 #endif /* QUANT_BANDS */