ref: c1c40a76c24fd918bc2db9171d1c01794d7ab94e
parent: 6ac26ecf2df52748b3fd8991a424ef42462364ae
author: Timothy B. Terriberry <tterribe@xiph.org>
date: Wed Aug 11 04:35:24 EDT 2010
Rework coarse energy coding.
This changes how the PDF used to code coarse energy.
New features:
1) The probability of 0 (p0) is now indepedent of the decay rate
of the remaining values; this additional flexibility will allow
us to model the actual distribution better, though that
improvement is not part of this patch.
2) There is a guaranteed minimum number of encodable energy
deltas.
This ensures that even the most extreme sudden volume changes
can be accurately represented.
3) The tail end of the distribution has an adjustable (through a
constant in the code) minimum probability.
This allows us to lower the worst-case bit cost of a single
delta.
4) The codebook is interleaved as 0, -1, +1, -2, +2, ... instead
of the 0, +1, -1, +2, -2, ... order used before (see 5).
5) There is no restriction that p0 be even.
Any remaining, unused part of the code is assigned to an
additional negative value (collected inter data suggests that
very large negative deltas are more common than very large
positive ones).
If the minimum probability is greater than 1, then an
additional positive delta with a smaller probablity may also
be added.
6) Once the tail of the distribution is reached, the energy delta
is computed directly, instead of continuing to loop through
the codebook.
This reduces the worst-case computational cost.
--- a/libcelt/laplace.c
+++ b/libcelt/laplace.c
@@ -35,56 +35,69 @@
#endif
#include "laplace.h"
+#include "mathops.h"
+/* The minimum probability of an energy delta (out of 32768). */
+#define LAPLACE_LOG_MINP (0)
+#define LAPLACE_MINP (1<<LAPLACE_LOG_MINP)
+/* The minimum number of guaranteed representable energy deltas (in one
+ direction). */
+#define LAPLACE_NMIN (16)
+
int ec_laplace_get_start_freq(int decay)
{- int fs = (((ec_uint32)32768)*(16384-decay))/(16384+decay);
- /* Making fs even so we're sure that all the range is used for +/- values */
- fs -= (fs&1);
- return fs;
+ celt_uint32 ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN+1);
+ int fs = (ft*(16384-decay))/(16384+decay);
+ return fs+LAPLACE_MINP;
}
+static int ec_laplace_get_freq1(int fs0, int decay)
+{+ celt_int32 ft;
+ ft = 32768 - LAPLACE_MINP*(2*LAPLACE_NMIN) - fs0;
+ return ft*(16384-decay)>>15;
+}
+
void ec_laplace_encode_start(ec_enc *enc, int *value, int decay, int fs)
{- int i;
- int fl;
- unsigned int ft;
- int s = 0;
+ unsigned fl;
int val = *value;
- if (val < 0)
+ fl = 0;
+ if (val)
{- s = 1;
- val = -val;
- }
- ft = 32768;
- fl = -fs;
- for (i=0;i<val;i++)
- {- int tmp_l, tmp_s;
- tmp_l = fl;
- tmp_s = fs;
- fl += fs*2;
- fs = (fs*(ec_int32)decay)>>14;
- if (fs == 0)
+ int s;
+ int i;
+ s = -(val<0);
+ val = val+s^s;
+ fl = fs;
+ fs = ec_laplace_get_freq1(fs, decay);
+ /* Search the decaying part of the PDF.*/
+ for (i=1; fs > 0 && i < val; i++)
{- if (fl+2 <= ft)
- {- fs = 1;
- } else {- fs = tmp_s;
- fl = tmp_l;
- if (s)
- *value = -i;
- else
- *value = i;
- break;
- }
+ fs *= 2;
+ fl += fs+2*LAPLACE_MINP;
+ fs = (fs*(celt_int32)decay)>>15;
}
+ /* Everything beyond that has probability LAPLACE_MINP. */
+ if (fs <= 0)
+ {+ int di;
+ int di_max;
+ di_max = (32768-fl+LAPLACE_MINP-1)>>LAPLACE_LOG_MINP;
+ di_max = (di_max-s)>>1;
+ di = IMIN(val - i, di_max);
+ fl += (2*di+1+s)*LAPLACE_MINP;
+ fs = IMIN(LAPLACE_MINP, 32768-fl);
+ *value = i+di+s^s;
+ }
+ else
+ {+ fs += LAPLACE_MINP;
+ fl += fs&~s;
+ }
+ celt_assert(fl+fs<=32768);
+ celt_assert(fs>0);
}
- if (fl < 0)
- fl = 0;
- if (s)
- fl += fs;
ec_encode_bin(enc, fl, fl+fs, 15);
}
@@ -99,37 +112,42 @@
int ec_laplace_decode_start(ec_dec *dec, int decay, int fs)
{int val=0;
- int fl, fh, fm;
- unsigned int ft;
- fl = 0;
- ft = 32768;
- fh = fs;
+ unsigned fl;
+ int fm;
fm = ec_decode_bin(dec, 15);
- while (fm >= fh && fs != 0)
+ fl = 0;
+ if (fm >= fs)
{- fl = fh;
- fs = (fs*(ec_int32)decay)>>14;
- if (fs == 0 && fh+2 <= ft)
+ val++;
+ fl = fs;
+ fs = ec_laplace_get_freq1(fs, decay)+LAPLACE_MINP;
+ /* Search the decaying part of the PDF.*/
+ while(fs > LAPLACE_MINP && fm >= fl+2*fs)
{- fs = 1;
+ fs *= 2;
+ fl += fs;
+ fs = ((fs-2*LAPLACE_MINP)*(celt_int32)decay)>>15;
+ fs += LAPLACE_MINP;
+ val++;
}
- fh += fs*2;
- val++;
- }
- if (fl>0)
- {- if (fm >= fl+fs)
+ /* Everything beyond that has probability LAPLACE_MINP. */
+ if (fs <= LAPLACE_MINP)
{+ int di;
+ di = (fm-fl)>>(LAPLACE_LOG_MINP+1);
+ val += di;
+ fl += 2*di*LAPLACE_MINP;
+ }
+ if (fm < fl+fs)
val = -val;
+ else
fl += fs;
- } else {- fh -= fs;
- }
}
- /* Preventing an infinite loop in case something screws up in the decoding */
- if (fl==fh)
- fl--;
- ec_dec_update(dec, fl, fh, ft);
+ celt_assert(fl<32768);
+ celt_assert(fs>0);
+ celt_assert(fl<=fm);
+ celt_assert(fm<IMIN(fl+fs,32768));
+ ec_dec_update(dec, fl, IMIN(fl+fs,32768), 32768);
return val;
}
--
⑨