This renames ec_dec_cdf() to ec_dec_icdf(), and changes the
functionality to use an "inverse" CDF table, where
icdf[i]=ft-cdf[i+1].
The first entry is omitted entirely.
It also adds a corresonding ec_enc_icdf() to the encoder, which uses
the same table.
One could use ec_encode_bin() by converting the values in the tables
back to normal CDF values, but the icdf[] table already has them in
the form ec_encode_bin() wants to use them, so there's no reason to
translate them and then translate them back.
This is done primarily to allow SILK to use the range coder with
8-bit probability tables containing cumulative frequencies that
span the full range 0...256.
With an 8-bit table, the final 256 of a normal CDF becomes 0 in the
"inverse" CDF.
It's the 0 at the start of a normal CDF which would become 256, but
this is the value we omit, as it already has to be special-cased in
the encoder, and is not used at all in the decoder.
This simplifies a good bit of the error handling, and should make it
impossible to overrun the buffer in the encoder or decoder, while
still allowing tell() to operate correctly after a bust.
The encoder now tries to keep the range coder data intact after a
bust instead of corrupting it with extra bits data, though this is
not a guarantee (too many extra bits may have already been flushed).
It also now correctly reports errors when the bust occurs merging the
last byte of range coder and extra bits.
A number of abstraction barrier violations were cleaned up, as well.
This patch also includes a number of minor performance improvements:
ec_{enc|dec}_bits() in particular should be much faster.
Finally, tf_select was changed to be coded with the range coder
rather than extra bits, so that it is at the front of the packet
(for unequal error protection robustness).
All of our usage of ec_{enc|dec}_bit_prob had the probability of a
"one" being a power of two.
This adds a new ec_{enc|dec}_bit_logp() function that takes this
explicitly into account.
It introduces less rounding error than the bit_prob version, does not
require 17-bit integers to be emulated by ec_{encode|decode}_bin(),
and does not require any multiplies or divisions at all.
It is exactly equivalent to
ec_encode_bin(enc,_val?0:(1<<_logp)-1,(1<<_logp)-(_val?1:0),1<<_logp)
The old ec_{enc|dec}_bit_prob functions are left in place for now,
because I am not sure if SILK is still using them or not when
combined in Opus.
Making it so all the information encoded directly with ec_enc_bits() gets
stored at the end of the stream, without going through the range coder. This
should be both faster and reduce the effects of bit errors.
Conflicts:
tests/ectest.c
Instead of trying to maximize the number of trailing zeros (minimize the number
of bits encoded), we try to maximize the number of trailing bits that can
contain arbitrary data.
Note that this requires ec_enc_tell() and ec_dec_tell() to reserve an extra
bit, since depending on the exact final codeword, as little as half the final
range might be available for storing arbitrary data.
This is the first step needed to start packing literal bits outside the range
coder (for speed and robustness purposes).