ref: 76469c64b4a6fb67946e58b6d2f32a5bfa18fcac
parent: 051e044d1466bfc566d188426abdd6c6d7232ed1
author: Timothy B. Terriberry <[email protected]>
date: Fri Jan 7 04:18:34 EST 2011
Prevent busts at low bitrates. This patch makes all symbols conditional on whether or not there's enough space left in the buffer to code them, and eliminates much of the redundancy in the side information. A summary of the major changes: * The isTransient flag is moved up to before the the coarse energy. If there are not enough bits to code the coarse energy, the flag would get forced to 0, meaning what energy values were coded would get interpreted incorrectly. This might not be the end of the world, and I'd be willing to move it back given a compelling argument. * Coarse energy switches coding schemes when there are less than 15 bits left in the packet: - With at least 2 bits remaining, the change in energy is forced to the range [-1...1] and coded with 1 bit (for 0) or 2 bits (for +/-1). - With only 1 bit remaining, the change in energy is forced to the range [-1...0] and coded with one bit. - If there is less than 1 bit remaining, the change in energy is forced to -1. This effectively low-passes bands whose energy is consistently starved; this might be undesirable, but letting the default be zero is unstable, which is worse. * The tf_select flag gets moved back after the per-band tf_res flags again, and is now skipped entirely when none of the tf_res flags are set, and the default value is the same for either alternative. * dynalloc boosting is now limited so that it stops once it's given a band all the remaining bits in the frame, or when it hits the "stupid cap" of (64<<LM)*(C<<BITRES) used during allocation. * If dynalloc boosing has allocated all the remaining bits in the frame, the alloc trim parameter does not get encoded (it would have no effect). * The intensity stereo offset is now limited to the range [start...codedBands], and thus doesn't get coded until after all of the skip decisions. Some space is reserved for it up front, and gradually given back as each band is skipped. * The dual stereo flag is coded only if intensity>start, since otherwise it has no effect. It is now coded after the intensity flag. * The space reserved for the final skip flag, the intensity stereo offset, and the dual stereo flag is now redistributed to all bands equally if it is unused. Before, the skip flag's bit was given to the band that stopped skipping without it (usually a dynalloc boosted band). In order to enable simple interaction between VBR and these packet-size enforced limits, many of which are encountered before VBR is run, the maximum packet size VBR will allow is computed at the beginning of the encoding function, and the buffer reduced to that size immediately. Later, when it is time to make the VBR decision, the minimum packet size is set high enough to ensure that no decision made thus far will have been affected by the packet size. As long as this is smaller than the up-front maximum, all of the encoder's decisions will remain in-sync with the decoder. If it is larger than the up-front maximum, the packet size is kept at that maximum, also ensuring sync. The minimum used now is slightly larger than it used to be, because it also includes the bits added for dynalloc boosting. Such boosting is shut off by the encoder at low rates, and so should not cause any serious issues at the rates where we would actually run out of room before compute_allocation().
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -591,15 +591,38 @@
static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
{
int curr, i;
- if (LM!=0)
- ec_enc_bit_logp(enc, tf_select, 1);
- ec_enc_bit_logp(enc, tf_res[start], isTransient ? 2 : 4);
- curr = tf_res[start];
- for (i=start+1;i<end;i++)
+ int tf_select_rsv;
+ int tf_changed;
+ int logp;
+ ec_uint32 budget;
+ ec_uint32 tell;
+ budget = enc->buf->storage*8;
+ tell = ec_enc_tell(enc, 0);
+ logp = isTransient ? 2 : 4;
+ /* Reserve space to code the tf_select decision. */
+ tf_select_rsv = LM>0 && tell+logp+1 <= budget;
+ budget -= tf_select_rsv;
+ curr = tf_changed = 0;
+ for (i=start;i<end;i++)
{
- ec_enc_bit_logp(enc, tf_res[i] ^ curr, isTransient ? 4 : 5);
- curr = tf_res[i];
+ if (tell+logp<=budget)
+ {
+ ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
+ tell = ec_enc_tell(enc, 0);
+ curr = tf_res[i];
+ tf_changed |= curr;
+ }
+ else
+ tf_res[i] = curr;
+ logp = isTransient ? 4 : 5;
}
+ /* Only code tf_select if it would actually make a difference. */
+ if (tf_select_rsv &&
+ tf_select_table[LM][4*isTransient+0+tf_changed]!=
+ tf_select_table[LM][4*isTransient+2+tf_changed])
+ ec_enc_bit_logp(enc, tf_select, 1);
+ else
+ tf_select = 0;
for (i=start;i<end;i++)
tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
/*printf("%d %d ", isTransient, tf_select); for(i=0;i<end;i++)printf("%d ", tf_res[i]);printf("\n");*/
@@ -608,16 +631,39 @@
static void tf_decode(int start, int end, int C, int isTransient, int *tf_res, int LM, ec_dec *dec)
{
int i, curr, tf_select;
- if (LM!=0)
+ int tf_select_rsv;
+ int tf_changed;
+ int logp;
+ ec_uint32 budget;
+ ec_uint32 tell;
+
+ budget = dec->buf->storage*8;
+ tell = ec_dec_tell(dec, 0);
+ logp = isTransient ? 2 : 4;
+ tf_select_rsv = LM>0 && tell+logp+1<=budget;
+ budget -= tf_select_rsv;
+ tf_changed = curr = 0;
+ for (i=start;i<end;i++)
+ {
+ if (tell+logp<=budget)
+ {
+ curr ^= ec_dec_bit_logp(dec, logp);
+ tell = ec_dec_tell(dec, 0);
+ tf_changed |= curr;
+ }
+ tf_res[i] = curr;
+ logp = isTransient ? 4 : 5;
+ }
+ tf_select = 0;
+ if (tf_select_rsv &&
+ tf_select_table[LM][4*isTransient+0+tf_changed] !=
+ tf_select_table[LM][4*isTransient+2+tf_changed])
+ {
tf_select = ec_dec_bit_logp(dec, 1);
- else
- tf_select = 0;
- curr = ec_dec_bit_logp(dec, isTransient ? 2 : 4);
- tf_res[start] = tf_select_table[LM][4*isTransient+2*tf_select+curr];
- for (i=start+1;i<end;i++)
+ }
+ for (i=start;i<end;i++)
{
- curr = ec_dec_bit_logp(dec, isTransient ? 4 : 5) ^ curr;
- tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+curr];
+ tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
}
}
@@ -750,7 +796,11 @@
int dual_stereo=0;
int effectiveBytes;
celt_word16 pf_threshold;
- int dynalloc_prob;
+ int dynalloc_logp;
+ celt_int32 vbr_rate;
+ celt_int32 total_bits;
+ celt_int32 total_boost;
+ celt_int32 tell;
SAVE_STACK;
if (nbCompressedBytes<0 || pcm==NULL)
@@ -773,16 +823,44 @@
ec_byte_writeinit_buffer(&buf, compressed, nbCompressedBytes);
ec_enc_init(&_enc,&buf);
enc = &_enc;
+ tell=1;
nbFilledBytes=0;
} else {
- nbFilledBytes=(ec_enc_tell(enc, 0)+4)>>3;
+ tell=ec_enc_tell(enc, 0);
+ nbFilledBytes=(tell+4)>>3;
}
nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
- if (st->vbr_rate_norm>0)
+ vbr_rate = st->vbr_rate_norm<<LM;
+ if (vbr_rate>0)
+ {
effectiveBytes = st->vbr_rate_norm>>BITRES<<LM>>3;
- else
+ /* Computes the max bit-rate allowed in VBR mode to avoid violating the
+ target rate and buffering.
+ We must do this up front so that bust-prevention logic triggers
+ correctly if we don't have enough bits. */
+ if (st->constrained_vbr)
+ {
+ celt_int32 vbr_bound;
+ celt_int32 max_allowed;
+ /* We could use any multiple of vbr_rate as bound (depending on the
+ delay).
+ This is clamped to ensure we use at least one byte if the encoder
+ was entirely empty, but to allow 0 in hybrid mode. */
+ vbr_bound = vbr_rate;
+ max_allowed = IMIN(IMAX((tell+7>>3)-nbFilledBytes,
+ vbr_rate+vbr_bound-st->vbr_reservoir>>(BITRES+3)),
+ nbAvailableBytes);
+ if(max_allowed < nbAvailableBytes)
+ {
+ nbCompressedBytes = nbFilledBytes+max_allowed;
+ nbAvailableBytes = max_allowed;
+ ec_byte_shrink(&buf, nbCompressedBytes);
+ }
+ }
+ } else
effectiveBytes = nbCompressedBytes;
+ total_bits = nbCompressedBytes*8;
effEnd = st->end;
if (effEnd > st->mode->effEBands)
@@ -863,7 +941,8 @@
pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
if (gain1<pf_threshold)
{
- ec_enc_bit_logp(enc, 0, 1);
+ if(tell+15<=total_bits)
+ ec_enc_bit_logp(enc, 0, 1);
gain1 = 0;
} else {
int qg;
@@ -888,7 +967,8 @@
}
/*printf("%d %f\n", pitch_index, gain1);*/
#else /* ENABLE_POSTFILTER */
- ec_enc_bit_logp(enc, 0, 1);
+ if(tell+15<=total_bits)
+ ec_enc_bit_logp(enc, 0, 1);
#endif /* ENABLE_POSTFILTER */
c=0; do {
@@ -920,19 +1000,20 @@
resynth = 0;
#endif
- if (st->complexity > 1 && LM>0)
+ isTransient = 0;
+ shortBlocks = 0;
+ if (LM>0 && ec_enc_tell(enc, 0)+3<=total_bits)
{
- isTransient = M > 1 &&
- transient_analysis(in, N+st->overlap, C, &st->frame_max, st->overlap);
- } else {
- isTransient = 0;
+ if (st->complexity > 1)
+ {
+ isTransient = transient_analysis(in, N+st->overlap, C,
+ &st->frame_max, st->overlap);
+ if (isTransient)
+ shortBlocks = M;
+ }
+ ec_enc_bit_logp(enc, isTransient, 3);
}
- if (isTransient)
- shortBlocks = M;
- else
- shortBlocks = 0;
-
ALLOC(freq, C*N, celt_sig); /**< Interleaved signal MDCTs */
ALLOC(bandE,st->mode->nbEBands*C, celt_ener);
ALLOC(bandLogE,st->mode->nbEBands*C, celt_word16);
@@ -956,27 +1037,25 @@
ALLOC(error, C*st->mode->nbEBands, celt_word16);
quant_coarse_energy(st->mode, st->start, st->end, effEnd, bandLogE,
- oldBandE, nbCompressedBytes*8, error, enc,
+ oldBandE, total_bits, error, enc,
C, LM, nbAvailableBytes, st->force_intra,
&st->delayedIntra, st->complexity >= 4);
- if (LM > 0)
- ec_enc_bit_logp(enc, shortBlocks!=0, 3);
-
tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
- if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
+ st->spread_decision = SPREAD_NORMAL;
+ if (ec_enc_tell(enc, 0)+4<=total_bits)
{
- if (st->complexity == 0)
+ if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
{
- st->spread_decision = SPREAD_NONE;
+ if (st->complexity == 0)
+ st->spread_decision = SPREAD_NONE;
} else {
- st->spread_decision = SPREAD_NORMAL;
+ st->spread_decision = spreading_decision(st->mode, X,
+ &st->tonal_average, st->spread_decision, effEnd, C, M);
}
- } else {
- st->spread_decision = spreading_decision(st->mode, X, &st->tonal_average, st->spread_decision, effEnd, C, M);
+ ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
}
- ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
ALLOC(offsets, st->mode->nbEBands, int);
@@ -1008,43 +1087,60 @@
offsets[i] += 1;
}
}
- dynalloc_prob = 6;
+ dynalloc_logp = 6;
+ total_bits<<=BITRES;
+ total_boost = 0;
+ tell = ec_enc_tell(enc, BITRES);
for (i=st->start;i<st->end;i++)
{
+ int width, quanta;
+ int dynalloc_loop_logp;
+ int boost;
int j;
- ec_enc_bit_logp(enc, offsets[i]!=0, dynalloc_prob);
- if (offsets[i]!=0)
+ width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
+ /* quanta is 6 bits, but no more than 1 bit/sample
+ and no less than 1/8 bit/sample */
+ quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
+ dynalloc_loop_logp = dynalloc_logp;
+ boost = 0;
+ for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
+ && boost < (64<<LM)*(C<<BITRES); j++)
{
- int width, quanta;
- width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
- /* quanta is 6 bits, but no more than 1 bit/sample
- and no less than 1/8 bit/sample */
- quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
- for (j=0;j<offsets[i]-1;j++)
- ec_enc_bit_logp(enc, 1, 1);
- ec_enc_bit_logp(enc, 0, 1);
- offsets[i] *= quanta;
- /* Making dynalloc more likely */
- dynalloc_prob = IMAX(2, dynalloc_prob-1);
+ int flag;
+ flag = j<offsets[i];
+ ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
+ tell = ec_enc_tell(enc, BITRES);
+ if (!flag)
+ break;
+ boost += quanta;
+ total_boost += quanta;
+ dynalloc_loop_logp = 1;
}
+ /* Making dynalloc more likely */
+ if (j)
+ dynalloc_logp = IMAX(2, dynalloc_logp-1);
+ offsets[i] = boost;
}
- alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE, st->mode->nbEBands, LM, C, N);
- ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
+ alloc_trim = 5;
+ if (tell+(6<<BITRES) <= total_bits - total_boost)
+ {
+ alloc_trim = alloc_trim_analysis(st->mode, X, bandLogE,
+ st->mode->nbEBands, LM, C, N);
+ ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
+ tell = ec_enc_tell(enc, BITRES);
+ }
/* Variable bitrate */
- if (st->vbr_rate_norm>0)
+ if (vbr_rate>0)
{
celt_word16 alpha;
- celt_int32 delta, tell;
+ celt_int32 delta;
/* The target rate in 8th bits per frame */
- celt_int32 vbr_rate;
celt_int32 target;
- celt_int32 vbr_bound, max_allowed, min_allowed;
+ celt_int32 min_allowed;
- target = vbr_rate = M*st->vbr_rate_norm;
+ target = vbr_rate + st->vbr_offset - ((40*C+20)<<BITRES);
- target = target + st->vbr_offset - ((40*C+20)<<BITRES);
-
/* Shortblocks get a large boost in bitrate, but since they
are uncommon long blocks are not greatly affected */
if (shortBlocks || tf_sum < -2*(st->end-st->start))
@@ -1054,8 +1150,6 @@
else if (M > 1)
target-=(target+14)/28;
- tell = ec_enc_tell(enc, BITRES);
-
/* The current offset is removed from the target and the space used
so far is added*/
target=target+tell;
@@ -1062,17 +1156,15 @@
/* By how much did we "miss" the target on that frame */
delta = target - vbr_rate;
- /* Computes the max bit-rate allowed in VBR more to avoid violating the target rate and buffering */
- vbr_bound = vbr_rate;
- if (st->constrained_vbr)
- max_allowed = IMIN(vbr_rate+vbr_bound-st->vbr_reservoir>>(BITRES+3),nbAvailableBytes);
- else
- max_allowed = nbAvailableBytes;
- min_allowed = (tell>>(BITRES+3)) + 2 - nbFilledBytes;
+ /* In VBR mode the frame size must not be reduced so much that it would
+ result in the encoder running out of bits.
+ The margin of 2 bytes ensures that none of the bust-prevention logic
+ in the decoder will have triggered so far. */
+ min_allowed = (tell+total_boost+(1<<BITRES+3)-1>>(BITRES+3)) + 2 - nbFilledBytes;
- /* In VBR mode the frame size must not be reduced so much that it would result in the encoder running out of bits */
nbAvailableBytes = target+(1<<(BITRES+2))>>(BITRES+3);
- nbAvailableBytes=IMAX(min_allowed,IMIN(max_allowed,nbAvailableBytes));
+ nbAvailableBytes=IMAX(min_allowed,nbAvailableBytes);
+ /* TODO: if we're busting, this will increase the reservoir by too much */
target=nbAvailableBytes<<(BITRES+3);
if (st->vbr_count < 970)
@@ -1091,7 +1183,6 @@
st->vbr_offset = -st->vbr_drift;
/*printf ("%d\n", st->vbr_drift);*/
- /* We could use any multiple of vbr_rate as bound (depending on the delay) */
if (st->constrained_vbr && st->vbr_reservoir < 0)
{
/* We're under the min value -- increase rate */
@@ -1108,16 +1199,11 @@
if (C==2)
{
+ int effectiveRate;
+
/* Always use MS for 2.5 ms frames until we can do a better analysis */
- if (LM==0)
- dual_stereo = 0;
- else
+ if (LM!=0)
dual_stereo = stereo_analysis(st->mode, X, st->mode->nbEBands, LM, C, N);
- ec_enc_bit_logp(enc, dual_stereo, 1);
- }
- if (C==2)
- {
- int effectiveRate;
/* Account for coarse energy */
effectiveRate = (8*effectiveBytes - 80)>>LM;
@@ -1139,7 +1225,6 @@
else
intensity = 100;
intensity = IMIN(st->end,IMAX(st->start, intensity));
- ec_enc_uint(enc, intensity-st->start, 1+st->end-st->start);
}
/* Bit allocation */
@@ -1150,7 +1235,8 @@
/* 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);
+ alloc_trim, &intensity, &dual_stereo, bits, pulses, fine_quant,
+ fine_priority, C, LM, enc, 1, st->lastCodedBands);
st->lastCodedBands = codedBands;
quant_fine_energy(st->mode, st->start, st->end, bandE, oldBandE, error, fine_quant, enc, C);
@@ -1743,7 +1829,6 @@
int intra_ener;
const int C = CHANNELS(st->channels);
int LM, M;
- int nbFilledBytes, nbAvailableBytes;
int effEnd;
int codedBands;
int alloc_trim;
@@ -1751,7 +1836,9 @@
celt_word16 postfilter_gain;
int intensity=0;
int dual_stereo=0;
- int dynalloc_prob;
+ celt_int32 total_bits;
+ celt_int32 tell;
+ int dynalloc_logp;
SAVE_STACK;
if (pcm==NULL)
@@ -1806,38 +1893,37 @@
ec_byte_readinit(&buf,(unsigned char*)data,len);
ec_dec_init(&_dec,&buf);
dec = &_dec;
- nbFilledBytes = 0;
- } else {
- nbFilledBytes = (ec_dec_tell(dec, 0)+4)>>3;
}
- nbAvailableBytes = len-nbFilledBytes;
- if (ec_dec_bit_logp(dec, 1))
+ total_bits = len*8;
+ tell = ec_dec_tell(dec, 0);
+
+ postfilter_gain = 0;
+ postfilter_pitch = 0;
+ if (tell+15 <= total_bits)
{
+ if(ec_dec_bit_logp(dec, 1))
+ {
#ifdef ENABLE_POSTFILTER
- int qg, octave;
- octave = ec_dec_uint(dec, 6);
- postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave);
- qg = ec_dec_bits(dec, 2);
- postfilter_gain = QCONST16(.125f,15)*(qg+2);
+ int qg, octave;
+ octave = ec_dec_uint(dec, 6);
+ postfilter_pitch = (16<<octave)+ec_dec_bits(dec, 4+octave);
+ qg = ec_dec_bits(dec, 2);
+ postfilter_gain = QCONST16(.125f,15)*(qg+2);
+ tell = ec_dec_tell(dec, 0);
#else /* ENABLE_POSTFILTER */
- RESTORE_STACK;
- return CELT_CORRUPTED_DATA;
+ RESTORE_STACK;
+ return CELT_CORRUPTED_DATA;
#endif /* ENABLE_POSTFILTER */
-
- } else {
- postfilter_gain = 0;
- postfilter_pitch = 0;
+ }
+ tell = ec_dec_tell(dec, 0);
}
- /* Decode the global flags (first symbols in the stream) */
- intra_ener = ec_dec_bit_logp(dec, 3);
- /* Get band energies */
- unquant_coarse_energy(st->mode, st->start, st->end, bandE, oldBandE,
- intra_ener, dec, C, LM);
-
- if (LM > 0)
+ if (LM > 0 && tell+3 <= total_bits)
+ {
isTransient = ec_dec_bit_logp(dec, 3);
+ tell = ec_dec_tell(dec, 0);
+ }
else
isTransient = 0;
@@ -1846,48 +1932,64 @@
else
shortBlocks = 0;
+ /* Decode the global flags (first symbols in the stream) */
+ intra_ener = tell+3<=total_bits ? ec_dec_bit_logp(dec, 3) : 0;
+ /* Get band energies */
+ unquant_coarse_energy(st->mode, st->start, st->end, bandE, oldBandE,
+ intra_ener, dec, C, LM);
+
ALLOC(tf_res, st->mode->nbEBands, int);
tf_decode(st->start, st->end, C, isTransient, tf_res, LM, dec);
- spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
+ tell = ec_dec_tell(dec, 0);
+ spread_decision = SPREAD_NORMAL;
+ if (tell+4 <= total_bits)
+ spread_decision = ec_dec_icdf(dec, spread_icdf, 5);
ALLOC(pulses, st->mode->nbEBands, int);
ALLOC(offsets, st->mode->nbEBands, int);
ALLOC(fine_priority, st->mode->nbEBands, int);
- for (i=0;i<st->mode->nbEBands;i++)
- offsets[i] = 0;
- dynalloc_prob = 6;
+ dynalloc_logp = 6;
+ total_bits<<=BITRES;
+ tell = ec_dec_tell(dec, BITRES);
for (i=st->start;i<st->end;i++)
{
- if (ec_dec_bit_logp(dec, dynalloc_prob))
+ int width, quanta;
+ int dynalloc_loop_logp;
+ int boost;
+ width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
+ /* quanta is 6 bits, but no more than 1 bit/sample
+ and no less than 1/8 bit/sample */
+ quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
+ dynalloc_loop_logp = dynalloc_logp;
+ boost = 0;
+ while (tell+(dynalloc_loop_logp<<BITRES) < total_bits &&
+ boost < (64<<LM)*(C<<BITRES))
{
- int width, quanta;
- width = C*(st->mode->eBands[i+1]-st->mode->eBands[i])<<LM;
- /* quanta is 6 bits, but no more than 1 bit/sample
- and no less than 1/8 bit/sample */
- quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
- while (ec_dec_bit_logp(dec, 1))
- offsets[i]++;
- offsets[i]++;
- offsets[i] *= quanta;
- /* Making dynalloc more likely */
- dynalloc_prob = IMAX(2, dynalloc_prob-1);
+ int flag;
+ flag = ec_dec_bit_logp(dec, dynalloc_loop_logp);
+ tell = ec_dec_tell(dec, BITRES);
+ if (!flag)
+ break;
+ boost += quanta;
+ total_bits -= quanta;
+ dynalloc_loop_logp = 1;
}
+ offsets[i] = boost;
+ /* Making dynalloc more likely */
+ if (boost>0)
+ dynalloc_logp = IMAX(2, dynalloc_logp-1);
}
ALLOC(fine_quant, st->mode->nbEBands, int);
- alloc_trim = ec_dec_icdf(dec, trim_icdf, 7);
+ alloc_trim = tell+(6<<BITRES) <= total_bits ?
+ ec_dec_icdf(dec, trim_icdf, 7) : 5;
- if (C==2)
- {
- dual_stereo = ec_dec_bit_logp(dec, 1);
- intensity = st->start + ec_dec_uint(dec, 1+st->end-st->start);
- }
-
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);
+ alloc_trim, &intensity, &dual_stereo, 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/quant_bands.c
+++ b/libcelt/quant_bands.c
@@ -138,6 +138,8 @@
}
};
+static const unsigned char small_energy_icdf[3]={2,1,0};
+
static int intra_decision(const celt_word16 *eBands, celt_word16 *oldEBands, int start, int end, int len, int C)
{
int c, i;
@@ -153,7 +155,8 @@
}
static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
- const celt_word16 *eBands, celt_word16 *oldEBands, int budget,
+ const celt_word16 *eBands, celt_word16 *oldEBands,
+ ec_int32 budget, ec_int32 tell,
const unsigned char *prob_model, celt_word16 *error, ec_enc *enc,
int _C, int LM, int intra, celt_word16 max_decay)
{
@@ -163,7 +166,8 @@
celt_word16 coef;
celt_word16 beta;
- ec_enc_bit_logp(enc, intra, 3);
+ if (tell+3 <= budget)
+ ec_enc_bit_logp(enc, intra, 3);
if (intra)
{
coef = 0;
@@ -180,7 +184,6 @@
do {
int bits_left;
int qi;
- int pi;
celt_word16 q;
celt_word16 x;
celt_word32 f;
@@ -202,9 +205,10 @@
if (qi > 0)
qi = 0;
}
- /* If we don't have enough bits to encode all the energy, just assume something safe.
- We allow slightly busting the budget here */
- bits_left = budget-(int)ec_enc_tell(enc, 0)-3*C*(end-i);
+ /* If we don't have enough bits to encode all the energy, just assume
+ something safe. */
+ tell = ec_enc_tell(enc, 0);
+ bits_left = budget-tell-3*C*(end-i);
if (i!=start && bits_left < 30)
{
if (bits_left < 24)
@@ -211,12 +215,26 @@
qi = IMIN(1, qi);
if (bits_left < 16)
qi = IMAX(-1, qi);
- if (bits_left<8)
- qi = 0;
}
- pi = 2*IMIN(i,20);
- ec_laplace_encode(enc, &qi,
- prob_model[pi]<<7, prob_model[pi+1]<<6);
+ if (budget-tell >= 15)
+ {
+ int pi;
+ pi = 2*IMIN(i,20);
+ ec_laplace_encode(enc, &qi,
+ prob_model[pi]<<7, prob_model[pi+1]<<6);
+ }
+ else if(budget-tell >= 2)
+ {
+ qi = IMAX(-1, IMIN(qi, 1));
+ ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2);
+ }
+ else if(budget-tell >= 1)
+ {
+ qi = IMIN(0, qi);
+ ec_enc_bit_logp(enc, -qi, 1);
+ }
+ else
+ qi = -1;
error[i+c*m->nbEBands] = PSHR32(f,15) - SHL16(qi,DB_SHIFT);
q = SHL16(qi,DB_SHIFT);
@@ -227,7 +245,7 @@
}
void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
- const celt_word16 *eBands, celt_word16 *oldEBands, int budget,
+ const celt_word16 *eBands, celt_word16 *oldEBands, ec_uint32 budget,
celt_word16 *error, ec_enc *enc, int _C, int LM, int nbAvailableBytes,
int force_intra, int *delayedIntra, int two_pass)
{
@@ -238,6 +256,7 @@
VARDECL(celt_word16, error_intra);
ec_enc enc_start_state;
ec_byte_buffer buf_start_state;
+ ec_uint32 tell;
SAVE_STACK;
intra = force_intra || (*delayedIntra && nbAvailableBytes > end*C);
@@ -246,6 +265,10 @@
else
*delayedIntra = 0;
+ tell = ec_enc_tell(enc, 0);
+ if (tell+3 > budget)
+ two_pass = intra = 0;
+
/* Encode the global flags using a simple probability model
(first symbols in the stream) */
@@ -265,7 +288,7 @@
if (two_pass || intra)
{
quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget,
- e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay);
+ tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay);
}
if (!intra)
@@ -294,7 +317,7 @@
*(enc->buf) = buf_start_state;
quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget,
- e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay);
+ tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay);
if (two_pass && ec_enc_tell(enc, 3) > tell_intra)
{
@@ -389,6 +412,8 @@
celt_word16 coef;
celt_word16 beta;
const int C = CHANNELS(_C);
+ ec_int32 budget;
+ ec_int32 tell;
if (intra)
@@ -400,6 +425,8 @@
coef = pred_coef[LM];
}
+ budget = dec->buf->storage*8;
+
/* Decode at a fixed coarse resolution */
for (i=start;i<end;i++)
{
@@ -406,11 +433,26 @@
c=0;
do {
int qi;
- int pi;
celt_word16 q;
- pi = 2*IMIN(i,20);
- qi = ec_laplace_decode(dec,
- prob_model[pi]<<7, prob_model[pi+1]<<6);
+ tell = ec_dec_tell(dec, 0);
+ if(budget-tell>=15)
+ {
+ int pi;
+ pi = 2*IMIN(i,20);
+ qi = ec_laplace_decode(dec,
+ prob_model[pi]<<7, prob_model[pi+1]<<6);
+ }
+ else if(budget-tell>=2)
+ {
+ qi = ec_dec_icdf(dec, small_energy_icdf, 2);
+ qi = (qi>>1)^-(qi&1);
+ }
+ else if(budget-tell>=1)
+ {
+ qi = -ec_dec_bit_logp(dec, 1);
+ }
+ else
+ qi = -1;
q = SHL16(qi,DB_SHIFT);
oldEBands[i+c*m->nbEBands] = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]) + prev[c] + SHL32(EXTEND32(q),15), 15);
--- a/libcelt/quant_bands.h
+++ b/libcelt/quant_bands.h
@@ -49,7 +49,7 @@
void quant_prob_free(const celt_int16 *freq);
void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
- const celt_word16 *eBands, celt_word16 *oldEBands, int budget,
+ const celt_word16 *eBands, celt_word16 *oldEBands, ec_uint32 budget,
celt_word16 *error, ec_enc *enc, int _C, int LM,
int nbAvailableBytes, int force_intra, int *delayedIntra, int two_pass);
--- a/libcelt/rate.c
+++ b/libcelt/rate.c
@@ -44,6 +44,14 @@
#include "rate.h"
+static const unsigned char LOG2_FRAC_TABLE[24]={
+ 0,
+ 8,13,
+ 16,19,21,23,
+ 24,26,27,28,29,30,31,32,
+ 32,33,34,34,35,36,36,37,37
+};
+
#ifndef STATIC_MODES
/*Determines if V(N,K) fits in a 32-bit unsigned integer.
@@ -141,7 +149,8 @@
#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 skip_rsv,int *bits,
+ const int *bits1, const int *bits2, const int *thresh, int total, int skip_rsv,
+ int *intensity, int intensity_rsv, int *dual_stereo, int dual_stereo_rsv, int *bits,
int *ebits, int *fine_priority, int len, int _C, int LM, void *ec, int encode, int prev)
{
int psum;
@@ -213,11 +222,6 @@
int band_bits;
int rem;
j = codedBands-1;
- /*Figure out how many left-over bits we would be adding to this band.
- This can include bits we've stolen back from higher, skipped bands.*/
- left = total-psum;
- percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
- left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
/* 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
@@ -226,10 +230,15 @@
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;
+ /* Give the bit we reserved to end skipping back. */
+ total += skip_rsv;
break;
}
+ /*Figure out how many left-over bits we would be adding to this band.
+ This can include bits we've stolen back from higher, skipped bands.*/
+ left = total-psum;
+ percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
+ left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
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;
@@ -259,7 +268,10 @@
band_bits -= 1<<BITRES;
}
/*Reclaim the bits originally allocated to this band.*/
- psum -= bits[j];
+ psum -= bits[j]+intensity_rsv;
+ if (intensity_rsv > 0)
+ intensity_rsv = LOG2_FRAC_TABLE[j-start];
+ psum += intensity_rsv;
if (band_bits >= alloc_floor)
{
/*If we have enough for a fine energy bit per channel, use it.*/
@@ -271,17 +283,47 @@
}
}
- /* Allocate the remaining bits */
- if (codedBands>start) {
- for (j=start;j<codedBands;j++)
- bits[j] += percoeff*(m->eBands[j+1]-m->eBands[j]);
- for (j=start;j<codedBands;j++)
+ celt_assert(codedBands > start);
+ /* Code the intensity and dual stereo parameters. */
+ if (intensity_rsv > 0)
+ {
+ if (encode)
{
- int tmp = IMIN(left, m->eBands[j+1]-m->eBands[j]);
- bits[j] += tmp;
- left -= tmp;
+ *intensity = IMIN(*intensity, codedBands);
+ ec_enc_uint((ec_enc *)ec, *intensity-start, codedBands+1-start);
}
+ else
+ *intensity = start+ec_dec_uint((ec_dec *)ec, codedBands+1-start);
}
+ else
+ *intensity = 0;
+ if (*intensity <= start)
+ {
+ total += dual_stereo_rsv;
+ dual_stereo_rsv = 0;
+ }
+ if (dual_stereo_rsv > 0)
+ {
+ if (encode)
+ ec_enc_bit_logp((ec_enc *)ec, *dual_stereo, 1);
+ else
+ *dual_stereo = ec_dec_bit_logp((ec_dec *)ec, 1);
+ }
+ else
+ *dual_stereo = 0;
+
+ /* Allocate the remaining bits */
+ left = total-psum;
+ percoeff = left/(m->eBands[codedBands]-m->eBands[start]);
+ left -= (m->eBands[codedBands]-m->eBands[start])*percoeff;
+ for (j=start;j<codedBands;j++)
+ bits[j] += percoeff*(m->eBands[j+1]-m->eBands[j]);
+ for (j=start;j<codedBands;j++)
+ {
+ int tmp = IMIN(left, m->eBands[j+1]-m->eBands[j]);
+ bits[j] += tmp;
+ left -= tmp;
+ }
/*for (j=0;j<end;j++)printf("%d ", bits[j]);printf("\n");*/
balance = 0;
@@ -364,7 +406,7 @@
return codedBands;
}
-int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim,
+int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim, int *intensity, int *dual_stereo,
int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev)
{
int lo, hi, len, j;
@@ -372,6 +414,8 @@
int codedBands;
int skip_start;
int skip_rsv;
+ int intensity_rsv;
+ int dual_stereo_rsv;
VARDECL(int, bits1);
VARDECL(int, bits2);
VARDECL(int, thresh);
@@ -384,6 +428,20 @@
/* Reserve a bit to signal the end of manually skipped bands. */
skip_rsv = total >= 1<<BITRES ? 1<<BITRES : 0;
total -= skip_rsv;
+ /* Reserve bits for the intensity and dual stereo parameters. */
+ intensity_rsv = dual_stereo_rsv = 0;
+ if (C==2)
+ {
+ intensity_rsv = LOG2_FRAC_TABLE[end-start];
+ if (intensity_rsv>total)
+ intensity_rsv = 0;
+ else
+ {
+ total -= intensity_rsv;
+ dual_stereo_rsv = total>=1<<BITRES ? 1<<BITRES : 0;
+ total -= dual_stereo_rsv;
+ }
+ }
ALLOC(bits1, len, int);
ALLOC(bits2, len, int);
ALLOC(thresh, len, int);
@@ -451,7 +509,8 @@
bits2[j] -= bits1[j];
}
codedBands = interp_bits2pulses(m, start, end, skip_start, bits1, bits2, thresh,
- total, skip_rsv, pulses, ebits, fine_priority, len, C, LM, ec, encode, prev);
+ total, skip_rsv, intensity, intensity_rsv, dual_stereo, dual_stereo_rsv,
+ 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 @@
@param pulses Number of pulses per band (returned)
@return Total number of bits allocated
*/
-int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim,
+int compute_allocation(const CELTMode *m, int start, int end, const int *offsets, int alloc_trim, int *intensity, int *dual_stero,
int total, int *pulses, int *ebits, int *fine_priority, int _C, int LM, void *ec, int encode, int prev);