Commit graph

339 commits

Author SHA1 Message Date
Timothy B. Terriberry
9bac8c17d5 Eliminate the ec_int32 and ec_uint32 typedefs.
These were used because the entropy coder originally came from
 outside libcelt, and thus did not have a common type system.
It's now undergone enough modification that it's not ever likely to
 be used as-is in another codec without some porting effort, so
 there's no real reason to maintain the typedefs separately.
Hopefully we'll replace these all again somedate with a common set
 of Opus typedefs, but for now this will do.

This fixes an issue caused by commit 6c8acbf1, which moved the
 ec_ilog() prototype from entcode.h to ecintrin.h, where the
 ec_uint32 typedef was not yet available.
Thanks to John Ridges for the report.
2011-03-02 21:14:19 -05:00
Gregory Maxwell
5b236756a3 Correct a number of uninitialized value problems when end!=nbEBands. Now 8khz/20ms audio can be decoded at 48kHz and vice versa (with the PF turned off). 2011-02-14 15:44:31 -05:00
Jean-Marc Valin
cf89d2a60c Adding a tiny amount of noise when folding to prevent/reduce numerical issues 2011-02-10 10:50:26 -05:00
Jean-Marc Valin
3806c1d738 Relicensing under the simplified (2-clause) BSD license
Got authorization from all copyright holders
2011-02-09 22:37:41 -05:00
Gregory Maxwell
b8a6b31205 This removes some pointless/dead code. 2011-02-03 23:25:13 -05:00
Timothy B. Terriberry
a093f4df74 Refactor the entropy coder.
This unifies the byte buffer, encoder, and decoder into a single
 struct.
The common encoder and decoder functions (such as ec_tell()) can
 operate on either one, simplifying code which uses both.
The precision argument to ec_tell() has been removed.
It now comes in two precisions:
  ec_tell() gives 1 bit precision in two operations, and
  ec_tell_frac() gives 1/8th bit precision in... somewhat more.
ec_{enc|dec}_bit_prob() were removed (they are no longer needed).
Some of the byte buffer access functions were made static and
 removed from the cross-module API.
All of the code in rangeenc.c and rangedec.c was merged into
 entenc.c and entdec.c, respectively, as we are no longer
 considering alternative backends.
rangeenc.c and rangede.c have been removed entirely.

This passes make check, after disabling the modes that we removed
 support for in cf5d3a8c.
2011-02-03 21:48:52 -05:00
Jean-Marc Valin
ea93c01a24 Renormalize in anti-collapse only when needed 2011-02-03 06:42:54 -05:00
Timothy B. Terriberry
57854e816a Fix collapse mask tracking for recombine steps.
The recombine loop for cm was correct if one started at 1 block,
 but was wrong otherwise (for a test case, convert 2 recombined
 blocks back to 4 with an initial cm of 0x3; the result should be
 0xF, but instead you get 0x7).
The recombine loop for fill was always wrong (for a test case,
 combine 8 blocks down to 1 with an initial fill=0xFE; the low bit
 remains unset).
This now properly interleaves and deinterleaves bits for these
 steps, which avoids declaring collapses (and skipping folding)
 where none, in fact, occurred.
2011-02-03 06:31:51 -05:00
Jean-Marc Valin
263e271948 Tuning the split threshold 2011-02-01 11:20:09 -05:00
Timothy B. Terriberry
411a84faea Add a seprate qtheta offset for two-phase stereo.
9b34bd83 caused serious regressions for 240-sample frame stereo,
 because the previous qb limit was _always_ hit for two-phase
 stereo.
Two-phase stereo really does operate with a different model (for
 example, the single bit allocated to the side should really
 probably be thought of as a sign bit for qtheta, but we don't
 count it as part of qtheta's allocation).
The old code was equivalent to a separate two-phase offset of 12,
 however Greg Maxwell's testing demonstrates that 16 performs
 best.
2011-02-01 06:35:14 -05:00
Timothy B. Terriberry
4499263b44 Adjust the splitting threshold.
Previously, we would only split a band if it was allocated more than
 32 bits.
However, the N=4 codebook can only produce about 22.5 bits, and two
 N=2 bands combined can only produce 26 bits, including 8 bits for
 qtheta, so if we wait until we allocate 32, we're guaranteed to fall
 short.
Several of the larger bands come pretty far from filling 32 bits as
 well, though their split versions will.

Greg Maxwell also suggested adding an offset to the threshold to
 account for the inefficiency of using qtheta compared to another
 VQ dimension.
This patch uses 1 bit as a placeholder, as it's a clear
 improvement, but we may adjust this later after collecting data on
 more possibilities over more files.
2011-01-31 22:42:12 -05:00
Timothy B. Terriberry
682b6cf1ad Don't destroy stereo history when switching to mono.
The first version of the mono decoder with stereo output collapsed
 the historic energy values stored for anti-collapse down to one
 channel (by taking the max).
This means that a subsequent switch back would continue on using
 the the maximum of the two values instead of the original history,
 which would make anti-collapse produce louder noise (and
 potentially more pre-echo than otherwise).

This patch moves the max into the anti_collapse function itself,
 and does not store the values back into the source array, so the
 full stereo history is maintained if subsequent frames switch
 back.
It also fixes an encoder mismatch, which never took the max
 (assuming, apparently, that the output channel count would never
 change).
2011-01-31 17:26:02 -05:00
Timothy B. Terriberry
948d27c9bc Propagate balance from compute_allocation() to quant_all_bands().
Instead of just dumping excess bits into the first band after
 allocation, use them to initialize the rebalancing loop in
 quant_all_bands().
This allows these bits to be redistributed over several bands, like
 normal.
2011-01-31 15:37:01 -05:00
Jean-Marc Valin
aaca4a713f Making sure that itheta=0 or 16384 really cuts allocation to one band 2011-01-30 21:19:06 -05:00
Timothy B. Terriberry
9b34bd835c Change qb cap to prevent side-fold collapses.
Previously, in a stereo split with itheta==16384, but without
 enough bits left over to actually code a pulse, the target band
 would completely collapse, because the mid gain would be zero and
 we don't fold the side.
This changes the limit to ensure that we never set qn>1 unless we
 know we'll have enough bits for at least one pulse.
This should eliminate the last possible whole-band collapse.
2011-01-29 01:31:09 -05:00
Jean-Marc Valin
09213de91c Don't rebalance bits for itheta=0 or 16384 2011-01-27 21:43:24 -05:00
Jean-Marc Valin
a928572055 Making rebalance a celt_int32 2011-01-27 18:44:26 -05:00
Jean-Marc Valin
8ca161212b Reducing waste at high bit-rate
We now encode the highest bitrate part of the split first and transfer
any unused bits to the other part. We use a dead zone of three bits
to prevent redistributing in cases of random fluctuation (or else
we will statistically lower the allocation of higher frequencies at
low-mid bitrates).
2011-01-27 16:28:16 -05:00
Jean-Marc Valin
79afa9c605 Fixes fixed-point overflow on really low energy
Makes celt_exp2() use Q10 input to avoid problems on very low energy.
Also makes the pitch downsampling more conservative on gain to avoid
problems later.
2011-01-27 10:46:01 -05:00
Jean-Marc Valin
02ad779f5f Fixes a fixed-point overflow in anti-collapse 2011-01-27 09:53:09 -05:00
Timothy B. Terriberry
045e47cede Improve accuracy of update_lowband test. 2011-01-26 23:52:27 -05:00
Timothy B. Terriberry
a396e153b9 More anti-collapse fixes, as well as a fold fix.
This changes folding so that the LCG is never used on transients
 (either short blocks or long blocks with increased time
 resolution), except in the case that there's not enough decoded
 spectrum to fold yet.

It also now only subtracts the anti-collapse bit from the total
 allocation in quant_all_bands() when space has actually been
 reserved for it.

Finally, it cleans up some of the fill and collapse_mask tracking
 (this tracking was originally made intentionally sloppy to save
 work, but then converted to replace the existing fill flag at the
 last minute, which can have a number of logical implications).
The changes, in particular:
1) Splits of less than a block now correctly mark the second half
    as filled only if the whole block was filled (previously it
    would also mark it filled if the next block was filled).
2) Splits of less than a block now correctly mark a block as
    un-collapsed if either half was un-collapsed, instead of marking
    the next block as un-collapsed when the high half was.
3) The N=2 stereo special case now keeps its fill mask even when
    itheta==16384; previously this would have gotten cleared,
    despite the fact that we fold into the side in this case.
4) The test against fill for folding now only considers the bits
    corresponding to the current set of blocks.
   Previously it would still fold if any later block was filled.
5) The collapse mask used for the LCG fold data is now correctly
    initialized when B=16 on platforms with a 16-bit int.
6) The high bits on a collapse mask are now cleared after the TF
    resolution changes and interleaving at level 0, instead of
    waiting until the very end.
   This prevents extraneous high flags set on mid from being mixed
    into the side flags for mid-side stereo.
2011-01-26 20:54:13 -05:00
Jean-Marc Valin
9ce95e0bd0 anti-collapse tuning
Using the min energy of the two last non-transient frames rather
than the min of just the two last frames. Also slightly increasing
the "thresh" upper bound coefficient to 0.5.
2011-01-25 19:12:06 -05:00
Jean-Marc Valin
72a554775f Oops, fixed the sqrt(2) constant for anti_collapse() 2011-01-25 16:01:27 -05:00
Jean-Marc Valin
01fa338985 Taking LM into account for anti-collapse 2011-01-24 17:21:12 -05:00
Jean-Marc Valin
eafd8a7f17 Simple DTX/CNG implementation 2011-01-23 00:24:45 -05:00
Jean-Marc Valin
ad3d813f01 Stop uselessly calling haar1() when resynth=0
This was computing on uninitialized data (though the result was never
used)
2011-01-22 21:22:03 -05:00
Gregory Maxwell
568326bfe2 Prevent out of bounds access to collapse_masks. 2011-01-22 20:37:14 -05:00
Gregory Maxwell
8f02c482ba Correct an encoder/decoder mismatch at low volume levels. Relax some low level clamps so that the dynamic range can extend further below the 16bit floor. 2011-01-22 19:50:36 -05:00
Jean-Marc Valin
620e716b76 Defining merge_stereo() when S==-M or S==M 2011-01-21 18:41:48 -05:00
Timothy B. Terriberry
6bdc53b86f Fix collapse_masks overflow for mono.
We were always storing collapse_masks[i*C+1], which could have
 overflowed the buffer on the last band in mono.
This also moves the stores outside the conditional, which allows
 common code to be used for masking the high bits, address
 generation, etc.
2011-01-21 06:53:03 -05:00
Jean-Marc Valin
5da938b2db Reorders some operations in anti-collapse to reuse values 2011-01-21 00:07:03 -05:00
Jean-Marc Valin
63fb61f176 Using previous range coder state for PRNG
This provides more entropy and allows some more flexibility on the
encoder side.
2011-01-20 23:29:05 -05:00
Jean-Marc Valin
e8a373fdbd Fixes an irrelevant uninitialized bug 2011-01-19 20:47:01 -05:00
Timothy B. Terriberry
21af73eb21 Make collapse-detection bitexact.
Jean-Marc's original anti-collapse patch used a threshold on the
 content of a decoded band to determine whether or not it should
 be filled with random noise.
Since this is highly sensitive to the accuracy of the
 implementation, it could lead to significant decoder output
 differences even if decoding error up to that point was relatively
 small.

This patch detects collapsed bands from the output of the vector
 quantizer, using exact integer arithmetic.
It makes two simplifying assumptions:
 a) If either input to haar1() is non-zero during TF resolution
     adjustments, then the output will be non-zero.
 b) If the content of a block is non-zero in any of the bands that
     are used for folding, then the folded output will be non-zero.
b) in particular is likely to be false when SPREAD_NONE is used.
It also ignores the case where mid and side are orthogonal in
 stereo_merge, but this is relatively unlikely.
This misses just over 3% of the cases that Jean-Marc's anti-collapse
 detection strategy would catch, but does not mis-classify any (all
 detected collapses are true collapses).

This patch overloads the "fill" parameter to mark which blocks have
 non-zero content for folding.
As a consequence, if a set of blocks on one side of a split has
 collapsed, _no_ folding is done: the result would be zero anyway,
 except for short blocks with SPREAD_AGGRESSIVE that are split down
 to a single block, but a) that means a lot of bits were available
 so a collapse is unlikely and b) anti-collapse can fill the block
 anyway, if it's used.
This also means that if itheta==0 or itheta==16384, we no longer
 fold at all on that side (even with long blocks), since we'd be
 multiplying the result by zero anyway.
2011-01-19 19:43:08 -05:00
Jean-Marc Valin
87efe1df00 Adds an anti-collapse mechanism for transients
This looks for bands in each short block that have no energy. For
each of these "collapsed" bands, noise is injected to have an
energy equal to the minimum of the two previous frames for that band.
The mechanism can be used whenever there are 4 or more MDCTs (otherwise
no complete collapse is possible) and is signalled with one bit just
before the final fine energy bits.
2011-01-18 14:44:04 -05:00
Jean-Marc Valin
8d367029a7 Adding tapset decision logic
Based on spreading_decision()'s logic. We choose tapsets
with less roll-off when we think the HF are tonal.
2011-01-17 16:37:51 -05:00
Jean-Marc Valin
f0d828fc14 Minor code cleanup, nothing to see here 2011-01-13 15:12:20 -05:00
Jean-Marc Valin
a387ebfc13 Proper scaling for the mid folding
Nor scaling the mid only after we've been able to store it
for folding.
2011-01-13 13:22:02 -05:00
Timothy B. Terriberry
173774bbf9 Replace log2_frac in the delta offset calculation.
Adds a new bitexact_log2tan() function which is much simpler, and
 more accurate.
The new approximation has an RMS error of 0.0038 bits from the
 correctly rounded result over the range of inputs we use, compared
 to an RMS error of 0.013 for the old log2_frac() method.
The actual computation of delta is also changed to use FRAC_MUL16,
 since this allows us to keep the full accuracy of the new method
 while avoiding 16-bit overflow.
The old delta computation actually could overflow 16 bits: it needed
 8 for the log2_frac() result, 1 for the sign of the difference, and
 8 more for N.
2011-01-12 21:21:35 -05:00
Jean-Marc Valin
235c64b94d Using a step pdf for the stereo itheta encoding 2011-01-11 17:05:41 -05:00
Jean-Marc Valin
ecefde3d4a Fixes the recombining stride and the deinterleaving stride
Previously, recombining only worked when going all the way back to
frequency domain.
2011-01-11 09:20:39 -05:00
Jean-Marc Valin
8cfda4a301 Using intensity_stereo() when itheta==0 2011-01-11 06:56:18 -05:00
Jean-Marc Valin
a66b7574f6 Defines MAX_FINE_BITS to ensure that we're using the same value everywhere 2011-01-10 13:21:04 -05:00
Jean-Marc Valin
9d2d0510a1 Using tell() rather than log2_frac() to compute qalloc 2011-01-10 12:45:01 -05:00
Jean-Marc Valin
d9f6ec3f04 Changes the N=2 stereo case to use the same sign convention as N=1 2011-01-10 10:52:51 -05:00
Jean-Marc Valin
c2095a2930 Max delta: +/- 16384 2011-01-10 10:35:31 -05:00
Jean-Marc Valin
d9127edb1f 32-bit fixes 2011-01-10 10:34:20 -05:00
Jean-Marc Valin
75f99bc51a Make LCG 16-bit clean 2011-01-10 10:02:58 -05:00
Timothy B. Terriberry
051e044d14 Fix Jean-Marc's sqrt(0.5) constants.
There were two different ones in use, one with less precision than
 a float, and the other missing a digit in the middle.
2011-01-09 01:40:05 -05:00