ref: 76ea41e17fb702fa86cfaf498579a421beaa0e0b
parent: 8e31ab3def98b4bf4988f1029bf5ed8251cd68fa
author: Timothy B. Terriberry <[email protected]>
date: Thu Dec 16 09:39:58 EST 2010
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).
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -1119,8 +1119,8 @@
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<<BITRES) - ec_enc_tell(enc, BITRES) - 1 - (1<<BITRES);
+ /* bits = packet size - where we are - safety */
+ bits = (nbCompressedBytes*8<<BITRES) - ec_enc_tell(enc, BITRES) - 1;
codedBands = compute_allocation(st->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 @@
intensity = ec_dec_uint(dec, 1+st->end-st->start);
}
- bits = (len*8<<BITRES) - ec_dec_tell(dec, BITRES) - 1 - (1<<BITRES);
+ bits = (len*8<<BITRES) - ec_dec_tell(dec, BITRES) - 1;
codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, dec, 0, 0);
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -141,7 +141,7 @@
#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 @@
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 @@
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 @@
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<<BITRES ? 1<<BITRES : 0;
+ total -= skip_rsv;
ALLOC(bits1, len, int);
ALLOC(bits2, len, int);
ALLOC(thresh, len, int);
@@ -430,7 +439,7 @@
skip_start = j;
}
codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh,
- total, pulses, ebits, fine_priority, len, C, LM, ec, encode, prev);
+ total, skip_rsv, pulses, ebits, fine_priority, len, C, LM, ec, encode, prev);
RESTORE_STACK;
return codedBands;
}