Preventing bands from being coded at a rate below (for now) 3/8 bit/sample

This commit is contained in:
Jean-Marc Valin 2010-09-28 14:56:20 -04:00
parent 78ea9fd024
commit b801da5e83
5 changed files with 53 additions and 26 deletions

View file

@ -873,7 +873,7 @@ static void quant_band(int encode, const CELTMode *m, int i, celt_norm *X, celt_
}
}
void quant_all_bands(int encode, const CELTMode *m, int start, int end, celt_norm *_X, celt_norm *_Y, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int *tf_res, int resynth, int total_bits, void *ec, int LM)
void quant_all_bands(int encode, const CELTMode *m, int start, int end, celt_norm *_X, celt_norm *_Y, const celt_ener *bandE, int *pulses, int shortBlocks, int fold, int *tf_res, int resynth, int total_bits, void *ec, int LM, int codedBands)
{
int i, balance;
celt_int32 remaining_bits;
@ -926,13 +926,18 @@ void quant_all_bands(int encode, const CELTMode *m, int start, int end, celt_nor
if (i != start)
balance -= tell;
remaining_bits = (total_bits<<BITRES)-tell-1;
curr_balance = (end-i);
if (curr_balance > 3)
curr_balance = 3;
curr_balance = balance / curr_balance;
b = IMIN(remaining_bits+1,pulses[i]+curr_balance);
if (b<0)
if (i <= codedBands-1)
{
curr_balance = (codedBands-i);
if (curr_balance > 3)
curr_balance = 3;
curr_balance = balance / curr_balance;
b = IMIN(remaining_bits+1,pulses[i]+curr_balance);
if (b<0)
b = 0;
} else {
b = 0;
}
/* Prevents ridiculous bit depths */
if (b > C*16*N<<BITRES)
b = C*16*N<<BITRES;

View file

@ -78,7 +78,7 @@ void measure_norm_mse(const CELTMode *m, float *X, float *X0, float *bandE, floa
* @param total_bits Total number of bits that can be used for the frame (including the ones already spent)
* @param enc Entropy encoder
*/
void quant_all_bands(int encode, const CELTMode *m, int start, int end, celt_norm * X, celt_norm * Y, const celt_ener *bandE, int *pulses, int time_domain, int fold, int *tf_res, int resynth, int total_bits, void *enc, int M);
void quant_all_bands(int encode, const CELTMode *m, int start, int end, celt_norm * X, celt_norm * Y, const celt_ener *bandE, int *pulses, int time_domain, int fold, int *tf_res, int resynth, int total_bits, void *enc, int M, int codedBands);
void stereo_decision(const CELTMode *m, celt_norm * restrict X, int *stereo_mode, int len, int M);

View file

@ -570,6 +570,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, c
int tf_select;
int nbFilledBytes, nbAvailableBytes;
int effEnd;
int codedBands;
SAVE_STACK;
if (nbCompressedBytes<0 || pcm==NULL)
@ -861,7 +862,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, c
for (i=0;i<st->mode->nbEBands;i++)
offsets[i] = 0;
bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1;
compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, LM);
codedBands = compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, LM);
quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C);
@ -876,7 +877,7 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, c
#endif
/* Residual quantisation */
quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, bandE, pulses, shortBlocks, has_fold, tf_res, resynth, nbCompressedBytes*8, enc, LM);
quant_all_bands(1, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, bandE, pulses, shortBlocks, has_fold, tf_res, resynth, nbCompressedBytes*8, enc, LM, codedBands);
quant_energy_finalise(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_enc_tell(enc, 0), enc, C);
@ -1424,6 +1425,7 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da
int LM, M;
int nbFilledBytes, nbAvailableBytes;
int effEnd;
int codedBands;
SAVE_STACK;
if (pcm==NULL)
@ -1535,14 +1537,14 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da
bits = len*8 - ec_dec_tell(dec, 0) - 1;
ALLOC(fine_quant, st->mode->nbEBands, int);
compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, LM);
codedBands = compute_allocation(st->mode, st->start, st->end, offsets, bits, pulses, fine_quant, fine_priority, C, LM);
/*bits = ec_dec_tell(dec, 0);
compute_fine_allocation(st->mode, fine_quant, (20*C+len*8/5-(ec_dec_tell(dec, 0)-bits))/C);*/
unquant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, fine_quant, dec, C);
/* Decode fixed codebook */
quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, NULL, pulses, shortBlocks, has_fold, tf_res, 1, len*8, dec, LM);
quant_all_bands(0, st->mode, st->start, st->end, X, C==2 ? X+N : NULL, NULL, pulses, shortBlocks, has_fold, tf_res, 1, len*8, dec, LM, codedBands);
unquant_energy_finalise(st->mode, st->start, st->end, bandE, oldBandE,
fine_quant, fine_priority, len*8-ec_dec_tell(dec, 0), dec, C);

View file

@ -137,29 +137,37 @@ void compute_pulse_cache(CELTMode *m, int LM)
#define ALLOC_STEPS 6
static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len, int _C, int LM)
static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int *bits1, int *bits2, int total, int *bits, int *ebits, int *fine_priority, int len, int _C, int LM)
{
int psum;
int lo, hi;
int i, j;
int logM;
int psum0;
const int C = CHANNELS(_C);
int codedBands=-1;
VARDECL(int, thresh);
SAVE_STACK;
psum0=0;
for (j=start;j<end;j++)
psum0 += bits1[j];
ALLOC(thresh, len, int);
/* Threshold: don't allow any band to go below 3/8 bit/sample */
for (j=start;j<end;j++)
thresh[j] = 3*(C*(m->eBands[j+1]-m->eBands[j])<<LM<<BITRES)>>3;
logM = LM<<BITRES;
lo = 0;
hi = 1<<ALLOC_STEPS;
for (i=0;i<ALLOC_STEPS;i++)
{
int mid = (lo+hi)>>1;
psum = psum0;
psum = 0;
for (j=start;j<end;j++)
psum += (mid*bits2[j]>>ALLOC_STEPS);
{
int tmp = bits1[j] + (mid*bits2[j]>>ALLOC_STEPS);
if (tmp >= thresh[j])
psum += tmp;
else if (tmp >= 1<<BITRES)
psum += 1<<BITRES;
}
if (psum > (total<<BITRES))
hi = mid;
else
@ -169,17 +177,26 @@ static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int
/*printf ("interp bisection gave %d\n", lo);*/
for (j=start;j<end;j++)
{
bits[j] = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS);
int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS);
if (tmp >= thresh[j])
{
bits[j] = tmp;
codedBands = j;
} else if (tmp >= 1<<BITRES)
bits[j] = 1<<BITRES;
else
bits[j] = 0;
psum += bits[j];
}
codedBands++;
/* Allocate the remaining bits */
{
int left, perband;
left = (total<<BITRES)-psum;
perband = left/(end-start);
for (j=start;j<end;j++)
perband = left/(codedBands-start);
for (j=start;j<codedBands;j++)
bits[j] += perband;
left = left-end*perband;
left = left-codedBands*perband;
for (j=start;j<start+left;j++)
bits[j]++;
}
@ -237,12 +254,14 @@ static inline void interp_bits2pulses(const CELTMode *m, int start, int end, int
bits[j] = 0;
}
RESTORE_STACK;
return codedBands;
}
void compute_allocation(const CELTMode *m, int start, int end, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM)
int compute_allocation(const CELTMode *m, int start, int end, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM)
{
int lo, hi, len, j;
const int C = CHANNELS(_C);
int codedBands;
VARDECL(int, bits1);
VARDECL(int, bits2);
SAVE_STACK;
@ -279,7 +298,8 @@ void compute_allocation(const CELTMode *m, int start, int end, int *offsets, int
bits2[j] = (C*N*m->allocVectors[hi*len+j]<<LM>>2) - bits1[j];
bits1[j] += offsets[j];
}
interp_bits2pulses(m, start, end, bits1, bits2, total, pulses, ebits, fine_priority, len, C, LM);
codedBands = interp_bits2pulses(m, start, end, bits1, bits2, total, pulses, ebits, fine_priority, len, C, LM);
RESTORE_STACK;
return codedBands;
}

View file

@ -104,7 +104,7 @@ celt_int16 **compute_alloc_cache(CELTMode *m, int M);
@param pulses Number of pulses per band (returned)
@return Total number of bits allocated
*/
void compute_allocation(const CELTMode *m, int start, int end, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM);
int compute_allocation(const CELTMode *m, int start, int end, int *offsets, int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM);
#endif