From 76ea41e17fb702fa86cfaf498579a421beaa0e0b Mon Sep 17 00:00:00 2001 From: "Timothy B. Terriberry" Date: Thu, 16 Dec 2010 14:39:58 -0800 Subject: [PATCH] Give the bit we reserved to end skipping back when we don't use it. Commit 8e447678 increased the number of cases where we end skipping without explicit signaling. Before, this would cause the bit we reserved for this purpose to either a) get grabbed by some N=1 band to code its sign bits or b) wind up as part of the fine energy at the end. This patch gives it back to the band where we stopped skipping, which is either the first band, or a band that was boosted by dynalloc. This allows the bit to be used for shape coding in that band, and allows the better computation of the fine offset, since the band knows it will get that bit in advance. With this change, we now guarantee that the number of bits allocated by compute_allocation() is exactly equal to the input total, less the bits consumed by skip flags during allocation itself (assuming total was non-negative; for negative total, no bits are emitted, and no bits are allocated). --- libcelt/celt.c | 6 +++--- libcelt/rate.c | 23 ++++++++++++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libcelt/celt.c b/libcelt/celt.c index 26849ac2..94cacce5 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -1119,8 +1119,8 @@ int celt_encode_with_ec_float(CELTEncoder * restrict st, const celt_sig * pcm, i ALLOC(pulses, st->mode->nbEBands, int); ALLOC(fine_priority, st->mode->nbEBands, int); - /* bits = packet size - where we are - safety - skip signalling*/ - bits = (nbCompressedBytes*8<mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands); st->lastCodedBands = codedBands; @@ -1856,7 +1856,7 @@ int celt_decode_with_ec_float(CELTDecoder * restrict st, const unsigned char *da intensity = ec_dec_uint(dec, 1+st->end-st->start); } - bits = (len*8<mode, st->start, st->end, offsets, alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, dec, 0, 0); diff --git a/libcelt/rate.c b/libcelt/rate.c index bd228651..c711fb4f 100644 --- a/libcelt/rate.c +++ b/libcelt/rate.c @@ -141,7 +141,7 @@ void compute_pulse_cache(CELTMode *m, int LM) #define ALLOC_STEPS 6 static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int skip_start, - const int *bits1, const int *bits2, const int *thresh, int total, int *bits, + const int *bits1, const int *bits2, const int *thresh, int total, int skip_rsv,int *bits, int *ebits, int *fine_priority, int len, int _C, int LM, void *ec, int encode, int prev) { int psum; @@ -216,13 +216,18 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int left = total-psum; percoeff = left/(m->eBands[codedBands]-m->eBands[start]); left -= (m->eBands[codedBands]-m->eBands[start])*percoeff; - /* Never skip the first band: we'd be coding a bit to signal that we're - going to waste all the other bits. - This means we won't be using the extra bit we reserved to signal the - end of manual skipping, but that will get added back in by - quant_all_bands().*/ + /* Never skip the first band, nor a band that has been boosted by + dynalloc. + In the first case, we'd be coding a bit to signal we're going to waste + all the other bits. + In the second case, we'd be coding a bit to redistribute all the bits + we just signaled should be cocentrated in this band. */ if (j<=skip_start) + { + /* Give the bit we reserved to end skipping back to this band. */ + bits[j] += skip_rsv; break; + } rem = IMAX(left-(m->eBands[j]-m->eBands[start]),0); band_width = m->eBands[codedBands]-m->eBands[j]; band_bits = bits[j] + percoeff*band_width + rem; @@ -364,6 +369,7 @@ int compute_allocation(const CELTMode *m, int start, int end, const int *offsets const int C = CHANNELS(_C); int codedBands; int skip_start; + int skip_rsv; VARDECL(int, bits1); VARDECL(int, bits2); VARDECL(int, thresh); @@ -373,6 +379,9 @@ int compute_allocation(const CELTMode *m, int start, int end, const int *offsets total = IMAX(total, 0); len = m->nbEBands; skip_start = start; + /* Reserve a bit to signal the end of manually skipped bands. */ + skip_rsv = total >= 1<