shithub: opus

Download patch

ref: 4fbd18d1f45fd5ecdeb2244e6f5bbd88d585cf8d
parent: f51ca493fb26b83dfbabc9cbba4f5ab76b273f01
author: Jean-Marc Valin <[email protected]>
date: Thu Jan 17 09:07:55 EST 2008

Close to getting CBR working

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -229,21 +229,33 @@
 
 
 /* Quantisation of the residual */
-void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc)
+void quant_bands(const CELTMode *m, float *X, float *P, float *W, struct alloc_data *alloc, int total_bits, ec_enc *enc)
 {
-   int i, j, B;
+   int i, j, B, bits;
    const int *eBands = m->eBands;
    B = m->nbMdctBlocks*m->nbChannels;
    float norm[B*eBands[m->nbEBands+1]];
+   int pulses[m->nbEBands];
+   int offsets[m->nbEBands];
    
+   for (i=0;i<m->nbEBands;i++)
+      offsets[i] = 0;
+   bits = total_bits - ec_enc_tell(enc, 0) - 1;
+   compute_allocation(alloc, offsets, bits, pulses);
+   
+   /*printf("bits left: %d\n", bits);
+   for (i=0;i<m->nbEBands;i++)
+      printf ("%d ", pulses[i]);
+   printf ("\n");*/
    /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/
    for (i=0;i<m->nbEBands;i++)
    {
       int q;
       float theta, n;
+      //q = pulses[i];
       q = m->nbPulses[i];
       n = sqrt(B*(eBands[i+1]-eBands[i]));
-      theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(m->nbPulses[i]));
+      theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(q));
          
       if (q<=0) {
          q = -q;
@@ -268,20 +280,28 @@
 }
 
 /* Decoding of the residual */
-void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec)
+void unquant_bands(const CELTMode *m, float *X, float *P, struct alloc_data *alloc, int total_bits, ec_dec *dec)
 {
-   int i, j, B;
+   int i, j, B, bits;
    const int *eBands = m->eBands;
    B = m->nbMdctBlocks*m->nbChannels;
    float norm[B*eBands[m->nbEBands+1]];
+   int pulses[m->nbEBands];
+   int offsets[m->nbEBands];
    
    for (i=0;i<m->nbEBands;i++)
+      offsets[i] = 0;
+   bits = total_bits - ec_dec_tell(dec, 0) - 1;
+   compute_allocation(alloc, offsets, bits, pulses);
+
+   for (i=0;i<m->nbEBands;i++)
    {
       int q;
       float theta, n;
+      //q = pulses[i];
       q = m->nbPulses[i];
       n = sqrt(B*(eBands[i+1]-eBands[i]));
-      theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(m->nbPulses[i]));
+      theta = .007*(B*(eBands[i+1]-eBands[i]))/(.1f+abs(q));
 
       if (q<=0) {
          q = -q;
--- a/libcelt/bands.h
+++ b/libcelt/bands.h
@@ -36,6 +36,7 @@
 #include "modes.h"
 #include "entenc.h"
 #include "entdec.h"
+#include "rate.h"
 
 /** Compute the amplitude (sqrt energy) in each of the bands 
  * @param m Mode data 
@@ -79,7 +80,7 @@
  * @param W Perceptual weighting
  * @param enc Entropy encoder
  */
-void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc);
+void quant_bands(const CELTMode *m, float *X, float *P, float *W, struct alloc_data *alloc, int total_bits, ec_enc *enc);
 
 /** Decoding of the residual spectrum
  * @param m Mode data 
@@ -87,7 +88,7 @@
  * @param P Pitch vector (normalised)
  * @param dec Entropy decoder
 */
-void unquant_bands(const CELTMode *m, float *X, float *P, ec_dec *dec);
+void unquant_bands(const CELTMode *m, float *X, float *P, struct alloc_data *alloc, int total_bits, ec_dec *dec);
 
 void stereo_mix(const CELTMode *m, float *X, float *bank, int dir);
 
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -41,6 +41,7 @@
 #include "quant_pitch.h"
 #include "quant_bands.h"
 #include "psy.h"
+#include "rate.h"
 
 #define MAX_PERIOD 1024
 
@@ -70,6 +71,8 @@
    float *out_mem;
 
    float *oldBandE;
+   
+   struct alloc_data alloc;
 };
 
 
@@ -113,6 +116,7 @@
    st->preemph_memE = celt_alloc(C*sizeof(float));;
    st->preemph_memD = celt_alloc(C*sizeof(float));;
 
+   alloc_init(&st->alloc, st->mode);
    return st;
 }
 
@@ -134,6 +138,8 @@
    celt_free(st->out_mem);
    
    celt_free(st->oldBandE);
+   alloc_clear(&st->alloc);
+
    celt_free(st);
 }
 
@@ -341,7 +347,7 @@
       sum += X[i]*X[i];
    printf ("%f\n", sum);*/
    /* Residual quantisation */
-   quant_bands(st->mode, X, P, mask, &st->enc);
+   quant_bands(st->mode, X, P, mask, &st->alloc, 770, &st->enc);
    
    time_idct(X, N, B, C);
    if (C==2)
@@ -421,6 +427,8 @@
    float *oldBandE;
    
    int last_pitch_index;
+   
+   struct alloc_data alloc;
 };
 
 CELTDecoder *celt_decoder_new(const CELTMode *mode)
@@ -459,6 +467,8 @@
    st->preemph_memD = celt_alloc(C*sizeof(float));;
 
    st->last_pitch_index = 0;
+   alloc_init(&st->alloc, st->mode);
+
    return st;
 }
 
@@ -477,6 +487,8 @@
    celt_free(st->out_mem);
    
    celt_free(st->oldBandE);
+   alloc_clear(&st->alloc);
+
    celt_free(st);
 }
 
@@ -567,7 +579,7 @@
    pitch_quant_bands(st->mode, X, P, gains);
 
    /* Decode fixed codebook and merge with pitch */
-   unquant_bands(st->mode, X, P, &dec);
+   unquant_bands(st->mode, X, P, &st->alloc, 770, &dec);
 
    time_idct(X, N, B, C);
    if (C==2)
--- a/libcelt/modes.c
+++ b/libcelt/modes.c
@@ -78,13 +78,18 @@
 const int pbank1[PBANDS128+2] = {0, 2, 4, 6, 8, 12, 20, 28, PITCH_END128, 128};
 //const int pbank1[PBANDS128+2] = {0, 4, 8, 12, 20, PITCH_END128, 128};
 
-int bitalloc0[NBANDS*5] = { 5,  4,  4,  4,  3,  3,  2,  2,  2,  2,  1,  1,  1,  1,  0,  0,  0,  0,
-                            8,  7,  7,  6,  6,  6,  5,  4,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
-                           11, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
-                           16, 15, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
-                           26, 25, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
+#define NALLOCS 7
+int bitalloc0[NBANDS*NALLOCS] = 
+   { 5,  4,  4,  4,  3,  3,  2,  2,  2,  2,  1,  1,  1,  1,  0,  0,  0,  0,
+     8,  7,  7,  6,  6,  6,  5,  4,  5,  5,  5,  5,  5,  5,  5,  5,  5,  5,
+    10,  9,  9,  8,  8,  8,  8,  8,  8,  8,  9, 10, 11, 12, 17, 15,  6,  7,
+    16, 15, 14, 14, 14, 13, 13, 13, 13, 13, 15, 16, 17, 18, 20, 18, 11, 12,
+    26, 25, 24, 22, 20, 18, 19, 19, 25, 22, 25, 30, 30, 35, 35, 35, 35, 25,
+    32, 30, 28, 27, 25, 24, 23, 21, 29, 27, 35, 40, 42, 50, 59, 54, 51, 36,
+    42, 40, 38, 37, 35, 34, 33, 31, 39, 37, 45, 50, 52, 60, 60, 60, 60, 46,
 };
 
+
 #define NBANDS256 15
 #define PBANDS256 8
 #define PITCH_END256 88
@@ -110,7 +115,7 @@
    means18,     /**< eMeans */
    decay18,     /**< eDecay */
    
-   5,           /**< nbAllocVectors */
+   7,           /**< nbAllocVectors */
    bitalloc0,   /**< allocVectors */
 };
 
@@ -134,7 +139,7 @@
    means,       /**< eMeans */
    decay,       /**< eDecay */
    
-   5,           /**< nbAllocVectors */
+   7,           /**< nbAllocVectors */
    bitalloc0,   /**< allocVectors */
 };
 
@@ -157,7 +162,7 @@
    means18,       /**< eMeans */
    decay18,       /**< eDecay */
    
-   5,           /**< nbAllocVectors */
+   7,           /**< nbAllocVectors */
    bitalloc0,   /**< allocVectors */
 };
 
@@ -179,7 +184,7 @@
    means,       /**< eMeans */
    decay,       /**< eDecay */
    
-   5,           /**< nbAllocVectors */
+   7,           /**< nbAllocVectors */
    bitalloc0,   /**< allocVectors */
 };
 
@@ -202,7 +207,7 @@
    means18,       /**< eMeans */
    decay18,       /**< eDecay */
    
-   5,           /**< nbAllocVectors */
+   7,           /**< nbAllocVectors */
    bitalloc0,   /**< allocVectors */
 };
 
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -185,7 +185,6 @@
    int lo, hi, out;
    int j;
    int bits[len];
-   int used_bits[len];
    const int *bands = alloc->bands;
    lo = 0;
    hi = 1<<BITRES;