ref: b2f59009f6a1ef5b9faba6c9e256bbee65f8f9f7
parent: 7cbf168c01295c9574418d63c3c4ce04b8c433b4
author: Timothy B. Terriberry <[email protected]>
date: Wed Dec 15 00:12:43 EST 2010
Move skip coding into interp_bits2pulses(). This allows us to a) not pay a coding cost to avoid skipping bands that are stupid to skip (e.g., the first band, or bands that have so few bits that we wouldn't redistribute anything) and b) not reserve bits to pay that cost.
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -752,7 +752,6 @@
int intensity=0;
int dual_stereo=0;
int effectiveBytes;
- int skip;
SAVE_STACK;
if (nbCompressedBytes<0 || pcm==NULL)
@@ -1120,15 +1119,11 @@
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 - ec_enc_tell(enc, 0) - 1 - (1<<BITRES);
- skip=-1;
+ /* bits = packet size - where we are - safety */
+ bits = nbCompressedBytes*8 - ec_enc_tell(enc, 0) - 1;
codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
- alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, &skip, st->lastCodedBands);
+ alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands);
st->lastCodedBands = codedBands;
- for (i=0;i<skip;i++)
- ec_enc_bit_prob(enc, 0, 32768);
- ec_enc_bit_prob(enc, 1, 32768);
quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C);
@@ -1728,7 +1723,6 @@
celt_word16 postfilter_gain;
int intensity=0;
int dual_stereo=0;
- int skip;
SAVE_STACK;
if (pcm==NULL)
@@ -1862,19 +1856,9 @@
intensity = ec_dec_uint(dec, 1+st->end-st->start);
}
- bits = len*8 - ec_dec_tell(dec, 0) - 1 - (1<<BITRES);
- skip=0;
- while (ec_dec_bit_prob(dec, 32768)==0)
- {
- skip++;
- if (skip>21)
- {
- dec->error = 1;
- break;
- }
- }
+ bits = len*8 - ec_dec_tell(dec, 0) - 1;
codedBands = compute_allocation(st->mode, st->start, st->end, offsets,
- alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, &skip, 0);
+ alloc_trim, bits, pulses, fine_quant, fine_priority, C, LM, dec, 0, 0);
unquant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, fine_quant, dec, C);
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -142,7 +142,7 @@
static inline int interp_bits2pulses(const CELTMode *m, int start, int end,
int *bits1, int *bits2, const int *thresh, int total, int *bits,
- int *ebits, int *fine_priority, int len, int _C, int LM, int *skip, int prev)
+ int *ebits, int *fine_priority, int len, int _C, int LM, void *ec, int encode, int prev)
{
int psum;
int lo, hi;
@@ -153,7 +153,6 @@
int alloc_floor;
int left, percoeff;
int force_skipping;
- int unforced_skips;
int done;
SAVE_STACK;
@@ -206,8 +205,6 @@
}
force_skipping = 1;
- unforced_skips = *skip;
- *skip = 0;
codedBands=end;
for (j=end;j-->start;)
{
@@ -227,28 +224,32 @@
force_skipping = force_skipping && band_bits < thresh[j];
if (!force_skipping)
{
- if (unforced_skips == -1)
+ /*Never skip the first band: we'd be coding a bit to signal that we're
+ going to waste all of the other bits.*/
+ if (j<=start)break;
+ /*If we have enough for the fine energy, but not more than a full
+ bit beyond that, or no more than one bit total, then don't bother
+ skipping this band: there's no extra bits to redistribute.*/
+ if ((band_bits>=alloc_floor && band_bits<=alloc_floor+(1<<BITRES))
+ || band_bits<(1<<BITRES))
+ break;
+ if (encode)
{
/*This if() block is the only part of the allocation function that
is not a mandatory part of the bitstream: any bands we choose to
skip here must be explicitly signaled.*/
- /*If we have enough for the fine energy, but not more than a full
- bit beyond that, or no more than one bit total, then don't bother
- skipping this band: there's no extra bits to redistribute.*/
- if ((band_bits>=alloc_floor && band_bits<=alloc_floor+(1<<BITRES))
- || band_bits<(1<<BITRES))
- break;
- /*Never skip the first band: we'd be coding a bit to signal that
- we're going to waste all of the other bits.*/
- if (j==start)break;
/*Choose a threshold with some hysteresis to keep bands from
fluctuating in and out.*/
if (band_bits > ((j<prev?7:9)*band_width<<LM<<BITRES)>>4)
+ {
+ ec_enc_bit_prob((ec_enc *)ec, 1, 32768);
break;
- } else if(unforced_skips--<=0)
+ }
+ ec_enc_bit_prob((ec_enc *)ec, 0, 32768);
+ } else if (ec_dec_bit_prob((ec_dec *)ec, 32768)) {
break;
- (*skip)++;
- /*Use a bit to skip this band.*/
+ }
+ /*We used a bit to skip this band.*/
psum += 1<<BITRES;
band_bits -= 1<<BITRES;
}
@@ -336,7 +337,7 @@
}
int compute_allocation(const CELTMode *m, int start, int end, int *offsets, int alloc_trim,
- int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, int *skip, int prev)
+ int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev)
{
int lo, hi, len, j;
const int C = CHANNELS(_C);
@@ -404,7 +405,7 @@
bits1[j] += offsets[j];
}
codedBands = interp_bits2pulses(m, start, end, bits1, bits2, thresh,
- total, pulses, ebits, fine_priority, len, C, LM, skip, prev);
+ total, pulses, ebits, fine_priority, len, C, LM, ec, encode, prev);
RESTORE_STACK;
return codedBands;
}
--- a/libcelt/rate.h
+++ b/libcelt/rate.h
@@ -103,7 +103,7 @@
@return Total number of bits allocated
*/
int compute_allocation(const CELTMode *m, int start, int end, int *offsets, int alloc_trim,
- int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, int *skip, int prev);
+ int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev);
#endif