From b801da5e831ea5eba0dd592a9711f05fcd97c9b4 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Tue, 28 Sep 2010 14:56:20 -0400 Subject: [PATCH] Preventing bands from being coded at a rate below (for now) 3/8 bit/sample --- libcelt/bands.c | 19 ++++++++++++------- libcelt/bands.h | 2 +- libcelt/celt.c | 10 ++++++---- libcelt/rate.c | 46 +++++++++++++++++++++++++++++++++------------- libcelt/rate.h | 2 +- 5 files changed, 53 insertions(+), 26 deletions(-) diff --git a/libcelt/bands.c b/libcelt/bands.c index 70e8bcdc..22045dc2 100644 --- a/libcelt/bands.c +++ b/libcelt/bands.c @@ -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< 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<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); diff --git a/libcelt/rate.c b/libcelt/rate.c index 64f7a60d..7ae5f59b 100644 --- a/libcelt/rate.c +++ b/libcelt/rate.c @@ -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;jeBands[j+1]-m->eBands[j])<>3; logM = LM<>1; - psum = psum0; + psum = 0; for (j=start;j>ALLOC_STEPS); + { + int tmp = bits1[j] + (mid*bits2[j]>>ALLOC_STEPS); + if (tmp >= thresh[j]) + psum += tmp; + else if (tmp >= 1< (total<>ALLOC_STEPS); + int tmp = bits1[j] + (lo*bits2[j]>>ALLOC_STEPS); + if (tmp >= thresh[j]) + { + bits[j] = tmp; + codedBands = j; + } else if (tmp >= 1<allocVectors[hi*len+j]<>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; } diff --git a/libcelt/rate.h b/libcelt/rate.h index a6767994..153b5304 100644 --- a/libcelt/rate.h +++ b/libcelt/rate.h @@ -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