Commit graph

1197 commits

Author SHA1 Message Date
Gregory Maxwell
9b98aaa741 Eliminate some divisions from rate.c. 2010-12-19 07:53:04 -05:00
Gregory Maxwell
7c673cfcdc Fix for the allocation going negative. (bits[j] >= 0 assert)
The modeline-bisection and interpolator have used different criteria
for the minimum coding threshold since the introduction of the
"backwards done" in 405e6a99. This meant that a lower modeline could be
selected which the interpolator was never able to get under the maximum
allocation. This patch makes the modeline selection search use the same
criteria as the interpolator.
2010-12-19 07:53:03 -05:00
Timothy B. Terriberry
53454f4910 More simplifications to the decoder renormalization loop.
This removes an XOR, an ADD, and an AND, and replaces them with
 an AND NOT in ec_dec_normalize().
Also, simplify the loop structure of ec_dec_cdf() and eliminate a
 CMOV.
2010-12-18 09:06:06 -05:00
Timothy B. Terriberry
e86fb268b0 Replace ec_{enc|dec}_bit_prob() with ec_{enc|dec}_bit_logp().
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.
2010-12-18 09:06:06 -05:00
Timothy B. Terriberry
8c23a3a0fd Subtract one from dif in the range decoder.
It turns out to be more convenient to store dif=low+rng-code-1
 instead of dif=low+rng-code.
This gets rid of a decrement in the normal decode path, replaces a
 decrement and an "and" in the normalization loop with a single
 add, and makes it clear that the new ec_dec_cdf() will not result
 in an infinite loop.
This does not change the bitstream.
2010-12-18 09:06:06 -05:00
Timothy B. Terriberry
a0b664df3d Add a generic CDF decoding routine.
This decodes a value encoded with ec_encode_bin() without using any
 divisions.
It is only meant for small alphabets.
If a symbol can take on a large number of possible values, a binary
 search would be better.

This patch also converts spread_decision to use it, since it is
 faster and introduces less rounding error to encode a single
 decision for the entire value than to encode it a bit at a time.
2010-12-17 14:21:43 -05:00
Jean-Marc Valin
3fed34ae00 Giving less bits to single-bin bands.
This improves the allocation for 2.5 ms frames.
2010-12-17 14:17:27 -05:00
Jean-Marc Valin
f33a7fb8e0 Fixed the spreading probabilities (1-x) 2010-12-17 13:38:20 -05:00
Timothy B. Terriberry
320cf2e2cd Re-organize spreading/folding constants.
These were stored internally in one order and in the bitstream in a
 different order.
Both used bare constants, making it unclear what either actually
 meant.
This changes them to use the same order, gives them named constants,
 and renames all the "fold" decision stuff to "spread" instead,
 since that is what it is really controlling.
2010-12-17 10:35:51 -05:00
Jean-Marc Valin
cd84e3d0f4 Re-enabling post-filter on 2.5 ms frames
Also, now forcing MS stereo for 2.5 frames because the current
analysis isn't reliable.
2010-12-16 22:29:35 -05:00
Timothy B. Terriberry
428a77d6bd More cleanups to compute_allocation().
The bisection search in compute_allocation() was not using the same
 method to count psum as interp_bits2pulses, i.e., it did not
 include the 64*C<<BITRES<<LM allocation ceiling (this adds at most
 84 max operations/frame, and so should have a trivial CPU cost).
Again, I wouldn't want to try to explain why these are different in
 a spec, so let's make them the same.

In addition, the procedure used to fill in bits1 and bits2 after the
 bisection search was not the same as the one used during the
 bisection search.
I.e., the
      if (bits1[j] > 0)
               bits1[j] += trim_offset[j];
 step was not also done for bits2, so bits1[j] + bits2[j] would not
 be equal to what was computed earlier for the hi line, and would
 not be guaranteed to be larger than total.
We now compute both allocation lines in the same manner, and then
 obtain bits2 by subtracting them, instead of trying to compute the
 offset from bits1 up front.

Finally, there was nothing to stop a bitstream from boosting a band
 beyond the number of bits remaining, which means that bits1 would
 not produce an allocation less than or equal to total, which means
 that some bands would receive a negative allocation in the decoder
 when the "left over" negative bits were redistributed to other
 bands.
This patch only adds the dynalloc offset to allocation lines greater
 than 0, so that an all-zeros floor still exists; the effect is that
 a dynalloc boost gets linearly scaled between allocation lines 0 and
 1, and is constant (like it was before) after that.
We don't have to add the extra condition to the bisection search,
 because it never examines allocation line 0.
This re-writes the indexing in the search to make that explicit;
 it was tested and gives exactly the same results in exactly the
 same number of iterations as the old search.
2010-12-16 20:20:09 -05:00
Timothy B. Terriberry
76ea41e17f Give the bit we reserved to end skipping back when we don't use it.
Commit 8e447678 increased the number of cases where we end skipping
 without explicit signaling.
Before, this would cause the bit we reserved for this purpose to
 either a) get grabbed by some N=1 band to code its sign bits or
 b) wind up as part of the fine energy at the end.
This patch gives it back to the band where we stopped skipping,
 which is either the first band, or a band that was boosted by
 dynalloc.
This allows the bit to be used for shape coding in that band, and
 allows the better computation of the fine offset, since the band
 knows it will get that bit in advance.

With this change, we now guarantee that the number of bits allocated
 by compute_allocation() is exactly equal to the input total, less
 the bits consumed by skip flags during allocation itself (assuming
 total was non-negative; for negative total, no bits are emitted,
 and no bits are allocated).
2010-12-16 20:20:04 -05:00
Jean-Marc Valin
8e31ab3def Making lower sampling rates use the same allocation table 2010-12-16 16:45:35 -05:00
Gregory Maxwell
8e4476789c Terminate the coding of skip bits at the last dynalloc boosted band. Otherwise the bitstream allows non-sensible behavior by the encoder (dynallocing bits into a band and then skipping it). This reduces skip bit overhead by about 2-3% at moderate bitrates with the current encoder. 2010-12-16 14:45:52 -05:00
Jean-Marc Valin
034172baa6 Making the encoder fail less often at stupidly low bit-rates 2010-12-16 14:40:14 -05:00
Jean-Marc Valin
5c80391b35 Comments, low bit-rate busting avoidance 2010-12-16 14:11:48 -05:00
Jean-Marc Valin
df6620eb0b Setting fine_priority for skipped bands 2010-12-16 13:07:29 -05:00
Timothy B. Terriberry
66c5ab4f07 Rebalance N=1 allocations during interp_bits2pulses().
Excess fractions of a bit can't be re-used in N=1 bands during
 quant_all_bands() because there's no shape, only a sign bit.
This meant that all the fractional bits in these bands accumulated,
 often up to 5 or 6 bits for stereo, until the first band with N>1,
 where they were dumped all at once.
This patch moves the rebalancing for N=1 bands to
 interp_bits2pulses() instead, where excess bits still have a
 chance to be moved into fine energy.
2010-12-16 12:47:08 -05:00
Timothy B. Terriberry
405e6a99e3 Change strategies for allocation hole prevention.
In commit ffe10574 JM added a "done" flag to the allocation
 interpolation loop: whenver a band did not have enough bits to
 pass its threshold for receiving PVQ pulses, all of the rest of
 band were given just enough bits for fine energy only.
This patch implements JM's "backwards done" idea: instead work
 backwards, dropping bands until the first band that is over the
 threshold is encountered, and don't artificially reduce the
 allocation any more after that.
This is much more stable: we can continue to signal manual skips if
 we want to, but we aren't forced to skip a large number of bands
 because of an isolated hole in he allocation.

This makes low-bitrate 120-sample frames much less rough.
It also reduces the force skip threshold from
 alloc_floor+(1<<BITRES)+1 to just alloc_floor+(1<<BITRES), because
 the former can now cascade to cause many bands to be skipped.
The difference here is subtle, and increases signaling overhead by
 0.11% of the total bitrate, but Monty confirmed that removing the
 +1 reduces noise in the bass (i.e., in N=1 bands where such a skip
 could cascade).

Finally the 64*C<<BITRES<<LM ceiling is moved into the bisection
 search, instead of just being imposed afterwards, again because I
 wouldn't want to try to explain in a spec why they're different.
2010-12-16 11:14:24 -05:00
Timothy B. Terriberry
7627b9f660 More compute_allocation() fixes.
1) Continue to update left and percoeff if we skip all the way to the
    first band.
   This doesn't actually matter for correctness, but I don't want to
    try to explain in a spec why we aren't doing this.
2) Force all the bits in skipped bands to go to fine energy.
   Before some of them could continue to be given to pulses, even though no
    pulses would actually be allocate for them.
2010-12-15 11:33:54 -05:00
Timothy B. Terriberry
4777f06910 Store the total budget of compute_allocation in BITRES units.
The margin of safety was supposed to be 1/8th bit, not 1 bit, and the
 bit we reserved to terminate skip signalling before was actually 8
 bits.
This patch updates the margin of safety to the correct value and
 accounts for the one bit (not 8) needed for skip signalling.
It also fixes the remainder calculation in the skip loop to work
 correctly when start>0.
2010-12-15 10:04:45 -05:00
Timothy B. Terriberry
283a9b606d Don't stop force-skipping on the first manually skipped band.
Now that manual skipping is in the same loop as forced skipping, there
 is no reason to do all of one, then all of the other.
This ensures we won't propagate bits to bands that have almost nothing
 later in quant_all_bands() because we didn't have enough bits to
 signal them skipped.
2010-12-15 08:42:55 -05:00
Timothy B. Terriberry
b2f59009f6 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.
2010-12-15 08:35:22 -05:00
Timothy B. Terriberry
7cbf168c01 More allocation function updates.
This moves more of the decisions about when to stop skipping bands into the
 encoder-specific branch, so they are not forced in the decoder (because there
 is currently no bit-savings from forcing them).
It also no longer requires an extra bit to code the fine energy in a skipped
 band: that was meant to account for the skip flag, but we already subtracted
 that.
2010-12-15 01:09:35 -05:00
Jean-Marc Valin
ffe1057419 Tim just rewrote half of the bit allocator -- hope it works now
Making sure we never waste bits due to band skip and also making
sure we don't skip bands "in the middle".
2010-12-15 00:36:41 -05:00
Jean-Marc Valin
9651ffdbb5 This fixes a bunch of bit allocation bugs
Now properly (I think) handling thresh[] and skipping
2010-12-14 16:41:03 -05:00
Jean-Marc Valin
fa50d3ac34 More negative bits prevention 2010-12-14 15:02:21 -05:00
Jean-Marc Valin
dcacb73c33 Preventing negative bit allocation 2010-12-14 13:39:30 -05:00
Jean-Marc Valin
6cbfbc383a Tuning for 2.5 ms frames 2010-12-14 11:53:39 -05:00
Jean-Marc Valin
425cfd3e15 Fix 2-phase stereo in the encoder 2010-12-13 16:56:35 -05:00
Jean-Marc Valin
70d30ffc09 Using overlap=shortMdctSize even for 2.5 ms frames.
And fixed a post-filter bug for that special case.
2010-12-13 13:52:06 -05:00
Jean-Marc Valin
546dfa1959 Adapting the allocation trim based on the spectral tilt 2010-12-10 17:18:17 -05:00
Jean-Marc Valin
dfd6e714f9 Adding some hysteresis on the folding threshold frequency
This adds some side-information that can be used to change the
threshold freq arbitrarily.
2010-12-09 23:23:34 -05:00
Jean-Marc Valin
85bbab0b4a Fixes a fixed-point overflow in stereo angle calculation 2010-12-09 15:23:45 -05:00
Jean-Marc Valin
fddc521a5c Completely new transient analysis algorithm
Should be more robust to closely-spaced transients
2010-12-09 14:48:02 -05:00
Jean-Marc Valin
2b747c9817 Simplifying the allocation code and making sure both channels get a bit 2010-12-08 11:06:09 -05:00
Jean-Marc Valin
52dc66b887 Take into accound the 32-bit limit in the VQ
This reduces waste at high bit-rate
2010-12-06 21:31:15 -05:00
Jean-Marc Valin
79b34eb83e Add API support for unconstrained VBR
celtenc now defaults to unconstrained VBR.
2010-12-05 17:22:06 -05:00
Jean-Marc Valin
9faf740882 Keeping the allocation of the intensity-codec bands
Also some code to select between constrained and unconstrained VBR
2010-12-04 10:27:22 -05:00
Jean-Marc Valin
a4badac92e Making VBR slightly exceed the budget rather than fail 2010-12-03 15:20:11 -05:00
Jean-Marc Valin
30165bbae0 Fixing the most obvious problems with the VBR code 2010-12-03 14:35:59 -05:00
Jean-Marc Valin
e5e9aa7985 Fixes some side-information rate control issues in VBR mode 2010-12-02 16:09:51 -05:00
Jean-Marc Valin
e65978fea7 Adding a dual stereo option.
Left and right are coded independently.
2010-12-02 13:52:20 -05:00
Jean-Marc Valin
100ae8ce34 nothing to see here 2010-12-01 18:45:45 -05:00
Jean-Marc Valin
1bfa18cb92 Fix totally broken bit allocation for non-mainstream modes (e.g. powers of two).
Also, making per-band dynamic allocation less aggressive.
2010-12-01 16:11:38 -05:00
Jean-Marc Valin
bad42a7d05 Better tuning for 5 ms frames 2010-12-01 14:54:10 -05:00
Jean-Marc Valin
4b087df592 Increasing resolution of the alloc trim 2010-11-30 21:08:31 -05:00
Jean-Marc Valin
dd4bf63ae3 Tuning higher bit-rate to behave a bit more like Vorbis. 2010-11-30 15:31:45 -05:00
Jean-Marc Valin
4f177e8510 Intensity stereo now in the bit-stream
Bands that are intensity-coded also get less bits than the others
2010-11-26 10:32:03 -05:00
Jean-Marc Valin
fb031119d3 Allocate remaining bits with constant SNR offset 2010-11-25 16:32:54 -05:00