Commit 388f1d6c authored by Jean-Marc Valin's avatar Jean-Marc Valin
Browse files

Tweaks for slightly improving the handling of the last byte (still not

perfect)
parent e5ec0f8d
...@@ -234,7 +234,7 @@ static void compute_inv_mdcts(mdct_lookup *mdct_lookup, float *window, float *X, ...@@ -234,7 +234,7 @@ static void compute_inv_mdcts(mdct_lookup *mdct_lookup, float *window, float *X,
} }
} }
int celt_encode(CELTEncoder *st, short *pcm, char *compressed, int nbCompressedBytes) int celt_encode(CELTEncoder *st, short *pcm, unsigned char *compressed, int nbCompressedBytes)
{ {
int i, c, N, B, C, N4; int i, c, N, B, C, N4;
N = st->block_size; N = st->block_size;
...@@ -379,46 +379,29 @@ int celt_encode(CELTEncoder *st, short *pcm, char *compressed, int nbCompressedB ...@@ -379,46 +379,29 @@ int celt_encode(CELTEncoder *st, short *pcm, char *compressed, int nbCompressedB
} }
} }
/* Not sure why, but filling the rest with zeros tends to help */
while (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8) while (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8)
ec_enc_uint(&st->enc, 1, 2); ec_enc_uint(&st->enc, 0, 2);
ec_enc_done(&st->enc); ec_enc_done(&st->enc);
{ {
unsigned char *data; unsigned char *data;
int nbBytes = ec_byte_bytes(&st->buf); int nbBytes = ec_byte_bytes(&st->buf);
if (nbBytes > nbCompressedBytes)
{
celt_warning("got too many bytes");
return CELT_INTERNAL_ERROR;
}
//printf ("%d\n", *nbBytes); //printf ("%d\n", *nbBytes);
data = ec_byte_get_buffer(&st->buf); data = ec_byte_get_buffer(&st->buf);
for (i=0;i<nbBytes;i++) for (i=0;i<nbBytes;i++)
compressed[i] = data[i]; compressed[i] = data[i];
/* Fill the last byte with the right pattern so the decoder doesn't get confused /* Fill the last byte with the right pattern so the decoder doesn't get confused
if the encoder didn't return enough bytes */ 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 */ /* FIXME: This isn't quite what the decoder expects, but it's the best we can do for now */
if (nbBytes < nbCompressedBytes) for (;i<nbCompressedBytes;i++)
{ compressed[i] = 0x00;
//fprintf (stderr, "smaller: %d\n", compressed[nbBytes-1]); }
if (compressed[nbBytes-1] == 0x00)
{
compressed[i++] = 0x80;
//fprintf (stderr, "put 0x00\n");
} else if (compressed[nbBytes-1] == 0x80)
{
int k = nbBytes-1;
while (compressed[k-1] == 0x80)
{
k--;
}
if (compressed[k-1] == 0x00)
{
compressed[i++] = 0x80;
//fprintf (stderr, "special 0x00\n");
}
}
for (;i<nbCompressedBytes;i++)
compressed[i] = 0x00;
} else if (nbBytes < nbCompressedBytes)
{
//fprintf (stderr, "ERROR: too many bits\n");
}
}
/* Reset the packing for the next encoding */ /* Reset the packing for the next encoding */
ec_byte_reset(&st->buf); ec_byte_reset(&st->buf);
ec_enc_init(&st->enc,&st->buf); ec_enc_init(&st->enc,&st->buf);
...@@ -656,7 +639,7 @@ int celt_decode(CELTDecoder *st, char *data, int len, short *pcm) ...@@ -656,7 +639,7 @@ int celt_decode(CELTDecoder *st, char *data, int len, short *pcm)
} }
} }
} }
return 0; return 0;
//printf ("\n"); //printf ("\n");
} }
......
...@@ -52,7 +52,7 @@ CELTEncoder *celt_encoder_new(const CELTMode *mode); ...@@ -52,7 +52,7 @@ CELTEncoder *celt_encoder_new(const CELTMode *mode);
void celt_encoder_destroy(CELTEncoder *st); void celt_encoder_destroy(CELTEncoder *st);
int celt_encode(CELTEncoder *st, short *pcm, char *compressed, int nbCompressedBytes); int celt_encode(CELTEncoder *st, short *pcm, unsigned char *compressed, int nbCompressedBytes);
/* Decoder stuff */ /* Decoder stuff */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment