Commit 76469c64 authored by Timothy B. Terriberry's avatar Timothy B. Terriberry Committed by Jean-Marc Valin
Browse files

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().
parent 051e044d
This diff is collapsed.
......@@ -138,6 +138,8 @@ static const unsigned char e_prob_model[4][2][42] = {
}
};
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 int intra_decision(const celt_word16 *eBands, celt_word16 *oldEBands, int
}
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 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
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 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
do {
int bits_left;
int qi;
int pi;
celt_word16 q;
celt_word16 x;
celt_word32 f;
......@@ -202,21 +205,36 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
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)
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 @@ static void quant_coarse_energy_impl(const CELTMode *m, int start, int end,
}
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 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
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 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
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 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
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 @@ void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd,
*(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 @@ void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBa
celt_word16 coef;
celt_word16 beta;
const int C = CHANNELS(_C);
ec_int32 budget;
ec_int32 tell;
if (intra)
......@@ -400,17 +425,34 @@ void unquant_coarse_energy(const CELTMode *m, int start, int end, celt_ener *eBa
coef = pred_coef[LM];
}
budget = dec->buf->storage*8;
/* Decode at a fixed coarse resolution */
for (i=start;i<end;i++)
{
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);
......
......@@ -49,7 +49,7 @@ unsigned char *quant_prob_alloc(const CELTMode *m);
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);
......
......@@ -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 @@ 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 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 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
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 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
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 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
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,16 +283,46 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
}
}
/* 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");*/
......@@ -364,7 +406,7 @@ static inline int interp_bits2pulses(const CELTMode *m, int start, int end, int
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 compute_allocation(const CELTMode *m, int start, int end, const int *offsets
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 @@ int compute_allocation(const CELTMode *m, int start, int end, const int *offsets
/* 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 @@ int compute_allocation(const CELTMode *m, int start, int end, const int *offsets
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;
}
......
......@@ -103,7 +103,7 @@ celt_int16 **compute_alloc_cache(CELTMode *m, int M);
@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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment