From 4fbd18d1f45fd5ecdeb2244e6f5bbd88d585cf8d Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Thu, 17 Jan 2008 14:07:55 +1100 Subject: [PATCH] Close to getting CBR working --- libcelt/bands.c | 32 ++++++++++++++++++++++++++------ libcelt/bands.h | 5 +++-- libcelt/celt.c | 16 ++++++++++++++-- libcelt/modes.c | 25 +++++++++++++++---------- libcelt/rate.c | 1 - 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/libcelt/bands.c b/libcelt/bands.c index 513e8cb7..f995200d 100644 --- a/libcelt/bands.c +++ b/libcelt/bands.c @@ -229,21 +229,33 @@ void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains) /* 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;inbEBands;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;inbEBands;i++) + printf ("%d ", pulses[i]); + printf ("\n");*/ /*printf ("%d %d\n", ec_enc_tell(enc, 0), compute_allocation(m, m->nbPulses));*/ for (i=0;inbEBands;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 @@ void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc) } /* 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;inbEBands;i++) + offsets[i] = 0; + bits = total_bits - ec_dec_tell(dec, 0) - 1; + compute_allocation(alloc, offsets, bits, pulses); + for (i=0;inbEBands;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; diff --git a/libcelt/bands.h b/libcelt/bands.h index a7603e3e..914ef96d 100644 --- 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 @@ void pitch_quant_bands(const CELTMode *m, float *X, float *P, float *gains); * @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 @@ void quant_bands(const CELTMode *m, float *X, float *P, float *W, ec_enc *enc); * @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); diff --git a/libcelt/celt.c b/libcelt/celt.c index e887428a..0907ac32 100644 --- 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 @@ struct CELTEncoder { float *out_mem; float *oldBandE; + + struct alloc_data alloc; }; @@ -113,6 +116,7 @@ CELTEncoder *celt_encoder_new(const CELTMode *mode) 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 @@ void celt_encoder_destroy(CELTEncoder *st) celt_free(st->out_mem); celt_free(st->oldBandE); + alloc_clear(&st->alloc); + celt_free(st); } @@ -341,7 +347,7 @@ int celt_encode(CELTEncoder *st, short *pcm) 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 @@ struct CELTDecoder { float *oldBandE; int last_pitch_index; + + struct alloc_data alloc; }; CELTDecoder *celt_decoder_new(const CELTMode *mode) @@ -459,6 +467,8 @@ CELTDecoder *celt_decoder_new(const CELTMode *mode) 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 @@ void celt_decoder_destroy(CELTDecoder *st) celt_free(st->out_mem); celt_free(st->oldBandE); + alloc_clear(&st->alloc); + celt_free(st); } @@ -567,7 +579,7 @@ int celt_decode(CELTDecoder *st, char *data, int len, short *pcm) 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) diff --git a/libcelt/modes.c b/libcelt/modes.c index 54a4b071..f0b71804 100644 --- a/libcelt/modes.c +++ b/libcelt/modes.c @@ -78,13 +78,18 @@ const int qpulses4s[NBANDS] ={38,31,25,21,18,16, 14, 12, 14, 12,14,15, 14, 15, 1 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 @@ const CELTMode mode0 = { means18, /**< eMeans */ decay18, /**< eDecay */ - 5, /**< nbAllocVectors */ + 7, /**< nbAllocVectors */ bitalloc0, /**< allocVectors */ }; @@ -134,7 +139,7 @@ const CELTMode mode1 = { means, /**< eMeans */ decay, /**< eDecay */ - 5, /**< nbAllocVectors */ + 7, /**< nbAllocVectors */ bitalloc0, /**< allocVectors */ }; @@ -157,7 +162,7 @@ const CELTMode mode2 = { means18, /**< eMeans */ decay18, /**< eDecay */ - 5, /**< nbAllocVectors */ + 7, /**< nbAllocVectors */ bitalloc0, /**< allocVectors */ }; @@ -179,7 +184,7 @@ const CELTMode mode3 = { means, /**< eMeans */ decay, /**< eDecay */ - 5, /**< nbAllocVectors */ + 7, /**< nbAllocVectors */ bitalloc0, /**< allocVectors */ }; @@ -202,7 +207,7 @@ const CELTMode mode4 = { means18, /**< eMeans */ decay18, /**< eDecay */ - 5, /**< nbAllocVectors */ + 7, /**< nbAllocVectors */ bitalloc0, /**< allocVectors */ }; diff --git a/libcelt/rate.c b/libcelt/rate.c index 2d2b6a73..9594417e 100644 --- a/libcelt/rate.c +++ b/libcelt/rate.c @@ -185,7 +185,6 @@ int interp_bits2pulses(const struct alloc_data *alloc, int *bits1, int *bits2, i int lo, hi, out; int j; int bits[len]; - int used_bits[len]; const int *bands = alloc->bands; lo = 0; hi = 1<