Enabled pure CBR, though there's still some decoder issues.

This commit is contained in:
Jean-Marc Valin 2008-01-17 16:57:18 +11:00
parent 4fbd18d1f4
commit b35536a3b0
6 changed files with 50 additions and 17 deletions

View file

@ -252,8 +252,8 @@ void quant_bands(const CELTMode *m, float *X, float *P, float *W, struct alloc_d
{
int q;
float theta, n;
//q = pulses[i];
q = m->nbPulses[i];
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(q));
@ -298,8 +298,8 @@ void unquant_bands(const CELTMode *m, float *X, float *P, struct alloc_data *all
{
int q;
float theta, n;
//q = pulses[i];
q = m->nbPulses[i];
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(q));

View file

@ -55,6 +55,7 @@ struct CELTEncoder {
int channels;
int Fs;
int bytesPerFrame;
ec_byte_buffer buf;
ec_enc enc;
@ -95,7 +96,8 @@ CELTEncoder *celt_encoder_new(const CELTMode *mode)
N4 = (N-st->overlap)/2;
ec_byte_writeinit(&st->buf);
ec_enc_init(&st->enc,&st->buf);
st->bytesPerFrame = mode->defaultRate;
mdct_init(&st->mdct_lookup, 2*N);
st->fft = spx_fft_init(MAX_PERIOD*C);
@ -234,7 +236,15 @@ static void compute_inv_mdcts(mdct_lookup *mdct_lookup, float *window, float *X,
}
}
int celt_encode(CELTEncoder *st, short *pcm)
int celt_encoder_set_output_size(CELTEncoder *st, int bytesPerFrame)
{
if (bytesPerFrame<= 0)
return -1;
st->bytesPerFrame = bytesPerFrame;
return st->bytesPerFrame;
}
int celt_encode(CELTEncoder *st, short *pcm, char *compressed)
{
int i, c, N, B, C, N4;
N = st->block_size;
@ -347,7 +357,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->alloc, 770, &st->enc);
quant_bands(st->mode, X, P, mask, &st->alloc, st->bytesPerFrame*8, &st->enc);
time_idct(X, N, B, C);
if (C==2)
@ -378,7 +388,26 @@ int celt_encode(CELTEncoder *st, short *pcm)
}
}
}
return 0;
while (ec_enc_tell(&st->enc, 0) < st->bytesPerFrame*8)
ec_enc_bits(&st->enc, 1, 1);
ec_enc_done(&st->enc);
int nbBytes = ec_byte_bytes(&st->buf);
char *data = ec_byte_get_buffer(&st->buf);
for (i=0;i<nbBytes;i++)
compressed[i] = data[i];
/* Fill the last byte with the right pattern so the decoder doesn't get confused
if the encoder didn't return enough bytes */
/* FIXME: This isn't quite what the decoder expects, but it's the best we can do for now */
for (;i<st->bytesPerFrame;i++)
compressed[i] = 0x00;
//printf ("%d\n", *nbBytes);
/* Reset the packing for the next encoding */
ec_byte_reset(&st->buf);
ec_enc_init(&st->enc,&st->buf);
return st->bytesPerFrame;
}
char *celt_encoder_get_bytes(CELTEncoder *st, int *nbBytes)
@ -579,7 +608,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, &st->alloc, 770, &dec);
unquant_bands(st->mode, X, P, &st->alloc, len*8, &dec);
time_idct(X, N, B, C);
if (C==2)

View file

@ -49,9 +49,9 @@ CELTEncoder *celt_encoder_new(const CELTMode *mode);
void celt_encoder_destroy(CELTEncoder *st);
int celt_encode(CELTEncoder *st, short *pcm);
int celt_encoder_set_output_size(CELTEncoder *st, int bytesPerFrame);
char *celt_encoder_get_bytes(CELTEncoder *st, int *nbBytes);
int celt_encode(CELTEncoder *st, short *pcm, char *compressed);
/* Decoder stuff */

View file

@ -115,6 +115,7 @@ const CELTMode mode0 = {
means18, /**< eMeans */
decay18, /**< eDecay */
32, /**< defaultRate */
7, /**< nbAllocVectors */
bitalloc0, /**< allocVectors */
};
@ -139,6 +140,7 @@ const CELTMode mode1 = {
means, /**< eMeans */
decay, /**< eDecay */
32, /**< defaultRate */
7, /**< nbAllocVectors */
bitalloc0, /**< allocVectors */
};
@ -162,6 +164,7 @@ const CELTMode mode2 = {
means18, /**< eMeans */
decay18, /**< eDecay */
48, /**< defaultRate */
7, /**< nbAllocVectors */
bitalloc0, /**< allocVectors */
};
@ -184,6 +187,7 @@ const CELTMode mode3 = {
means, /**< eMeans */
decay, /**< eDecay */
32, /**< defaultRate */
7, /**< nbAllocVectors */
bitalloc0, /**< allocVectors */
};
@ -207,6 +211,7 @@ const CELTMode mode4 = {
means18, /**< eMeans */
decay18, /**< eDecay */
92, /**< defaultRate */
7, /**< nbAllocVectors */
bitalloc0, /**< allocVectors */
};

View file

@ -52,6 +52,7 @@ struct CELTMode {
const float *eMeans;
const int *eDecay;
int defaultRate;
int nbAllocVectors;
const int *allocVectors;
};

View file

@ -45,7 +45,7 @@ int main(int argc, char *argv[])
CELTEncoder *enc;
CELTDecoder *dec;
int len;
char *data;
char data[1024];
inFile = argv[1];
fin = fopen(inFile, "rb");
@ -55,14 +55,14 @@ int main(int argc, char *argv[])
/* Use mode4 for stereo and don't forget to change the value of CHANNEL above */
enc = celt_encoder_new(celt_mode0);
dec = celt_decoder_new(celt_mode0);
//celt_encoder_set_output_size(enc, 48);
while (!feof(fin))
{
fread(in, sizeof(short), FRAME_SIZE*CHANNELS, fin);
celt_encode(enc, in);
len = celt_encode(enc, in, data);
printf ("%d\n", len);
#if 1
data = celt_encoder_get_bytes(enc, &len);
//printf ("%d\n", len);
/* this is to simulate packet loss */
if (rand()%100==-1)
celt_decode(dec, NULL, len, in);
@ -71,8 +71,6 @@ int main(int argc, char *argv[])
#endif
fwrite(in, sizeof(short), FRAME_SIZE*CHANNELS, fout);
}
//data = celt_encoder_get_bytes(enc, &len);
//printf ("%d bytes total\n", len);
celt_encoder_destroy(enc);
celt_decoder_destroy(dec);