edith_codecs
Clone
0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits
ec-mp3 against LAME on the real library — round 1
Why the lane opened
encodes_above_the_incumbent_bar was the crate's only quality gate, and it
measures against BAR — a table of correlations frozen from the encoder this
crate replaces, on synthetic fixtures. A frozen number cannot notice the
reference moving, and its thinnest row (mp3src-stereo-48000 at 128 kbit/s)
passed by +0.00014, so the gate was one rounding change away from meaningless
in either direction. The survey ranked it alongside ec-h264's missing
rate-quality reference: an unmeasured gap.
What the gate does now
real_library_sweep_vs_lame encodes the first 60 s of seven of the user's own
files twice at matched constant bitrate — once with Mp3Encoder, once with
ffmpeg's libmp3lame — decodes both with the same decoder, aligns each against
the samples that went in, and correlates. Matched CBR means the sizes agree to
within 0.2%, so the correlation difference is quality and not rate.
First measurement
| source | kbit/s | corr ours | corr LAME | gap | ours kbit/s | LAME kbit/s |
|---|---|---|---|---|---|---|
| nik | 128 | 0.99790 | 0.99888 | -0.00098 | 127.9 | 128.1 |
| nik | 192 | 0.99950 | 0.99982 | -0.00032 | 192.0 | 192.2 |
| reelo | 128 | 0.99718 | 0.99855 | -0.00137 | 127.9 | 128.1 |
| reelo | 192 | 0.99927 | 0.99976 | -0.00048 | 192.0 | 192.2 |
| her | 128 | 0.99680 | 0.99565 | +0.00115 | 127.9 | 128.2 |
| her | 192 | 0.99901 | 0.99883 | +0.00018 | 192.0 | 192.3 |
| reell | 128 | 0.99855 | 0.99849 | +0.00006 | 127.9 | 128.1 |
| reell | 192 | 0.99966 | 0.99969 | -0.00003 | 192.0 | 192.2 |
| reelm | 128 | 0.99816 | 0.99897 | -0.00080 | 127.9 | 128.1 |
| reelm | 192 | 0.99951 | 0.99994 | -0.00042 | 192.0 | 192.2 |
| dl8a | 128 | 0.99623 | 0.99737 | -0.00113 | 127.9 | 128.1 |
| dl8a | 192 | 0.99894 | 0.99940 | -0.00046 | 192.0 | 192.2 |
| hein | 128 | 0.99859 | 0.99958 | -0.00100 | 127.9 | 128.1 |
| hein | 192 | 0.99964 | 0.99996 | -0.00032 | 192.0 | 192.2 |
Fourteen rows, all seven library sources readable. The floor is set at -0.0020, just under the worst row.
What the numbers say
The gap closes with rate on every source: the mean 128 kbit/s gap is -0.00058 and the mean 192 kbit/s gap is -0.00027. That shape — behind where bits are scarce, level where they are not — is a bit-allocation difference, not a transform or quantiser defect. A defect in the filterbank or the quantiser would not politely halve itself when given more bits.
her is the one source where we are ahead at both rates, and it is also the
source with the widest gap in our favour. It is the sparsest material in the
list. reelo and dl8a are the two worst rows and are the densest. The
ordering is consistent with LAME's short-block switching being worth roughly a
thousandth of correlation on dense content at 128 kbit/s, and nothing at all on
sparse content — which is where the next round should look, rather than at the
psychoacoustic model.
Not measured in this round
VBR. encode_vbr exists and Mp3EncoderConfig::vbr_quality is honoured, but
LAME's -q/-V scale does not map onto ours, so a matched-rate comparison
needs a rate-matching search before it means anything. That is its own slice.
Round 2: the block switcher was inert, and the gate could not see it
The 128-versus-192 shape pointed at bit allocation, and the first constant on
that path is the one that decides a granule is a transient:
next_granule_energy > here * 8.0, on whole-granule totals. Sweeping it over
2, 4, 8, 16, 32 moved the correlation of the three worst sources by 0.00004
across the whole range, and 8 through 32 were bit-identical.
Counting block types in the coded streams explains why, and turns up something worse than a mistuned constant:
| stream | long | start | short | stop |
|---|---|---|---|---|
| ours, reelo @128 | 9193 | 1 | 1 | 1 |
| libmp3lame, same | 8988 | 58 | 88 | 58 |
| ours, her @128 | 9190 | 2 | 2 | 2 |
| libmp3lame, same | 8948 | 72 | 102 | 70 |
One short granule in 9196. A granule is 576 samples, 13 ms; requiring its total energy to grow eightfold is a condition music meets about once a minute. The switcher has been dead code since it was written.
Forcing it awake at ratio 2 produces 526 short granules and moves whole-file correlation by 0.00004, in the wrong direction. That is the finding: the crate's quality gate is structurally blind to the thing short blocks exist to fix. Pre-echo is a few milliseconds of noise smeared backwards before an attack; correlating over a minute divides it away. A gate like that would have accepted the switcher being deleted outright.
The metric the gate was missing
worst_window_db now reports the worst 20 ms error-to-signal ratio in each
file, for ours and for LAME, skipping windows under -60 dBFS so silence does
not produce a ratio out of nothing. Against it the constant is not flat at all:
| ratio | 1.3 | 1.6 | 2 | 3 | 4 | 8 |
|---|---|---|---|---|---|---|
| dl8a @128 | -2.3 | -3.3 | -3.3 | -3.3 | -3.3 | +1.1 |
| dl8a @192 | -6.7 | -6.7 | -6.7 | -7.8 | -7.8 | -4.2 |
At the shipped value one 20 ms window of dl8a carries more error energy than signal. The curve is flat from 1.6 to 4 and falls off a cliff between 4 and 8, so the shipped 8.0 sat just past the edge of a cliff nothing could see. Below 1.6 the extra short blocks cost correlation without buying back any more of the worst window. 4.0 lands, at the cheap end of the flat part.
Where the lane stands after round 2
| source | kbit/s | corr ours | corr LAME | gap | worst 20 ms ours | worst LAME |
|---|---|---|---|---|---|---|
| nik | 128 | 0.99789 | 0.99888 | -0.00099 | -11.4 | -11.2 |
| nik | 192 | 0.99950 | 0.99982 | -0.00032 | -17.7 | -17.2 |
| reelo | 128 | 0.99717 | 0.99855 | -0.00138 | -10.3 | -9.3 |
| reelo | 192 | 0.99927 | 0.99976 | -0.00049 | -16.3 | -16.1 |
| her | 128 | 0.99680 | 0.99565 | +0.00115 | -9.8 | -5.4 |
| her | 192 | 0.99901 | 0.99883 | +0.00018 | -14.7 | -11.1 |
| reell | 128 | 0.99855 | 0.99849 | +0.00006 | -9.3 | -6.1 |
| reell | 192 | 0.99966 | 0.99969 | -0.00003 | -15.2 | -11.7 |
| reelm | 128 | 0.99815 | 0.99897 | -0.00082 | -1.5 | -2.0 |
| reelm | 192 | 0.99951 | 0.99994 | -0.00042 | -3.4 | -13.2 |
| dl8a | 128 | 0.99625 | 0.99737 | -0.00111 | -3.3 | -6.0 |
| dl8a | 192 | 0.99894 | 0.99940 | -0.00046 | -7.8 | -12.2 |
| hein | 128 | 0.99858 | 0.99958 | -0.00101 | -0.1 | -7.6 |
| hein | 192 | 0.99964 | 0.99996 | -0.00032 | -5.4 | -19.4 |
We are ahead of LAME in the worst window on all four music sources and behind on all three where the material is speech or speech-dominated. The tell is what happens between the two rates: on hein, LAME's worst window improves by 11.8 dB for 50% more bits and ours improves by 5.3. A worst window that will not improve when handed more bits is not short of bits. It is the masking model letting a band go, or the distortion loop declining to buy it back.
Round 3, named
Three candidates, in order of what the round-2 evidence points at:
- The distortion loop's stop conditions --
for _round in 0..20, three bands amplified per round, and thespent * 20 > target_bits * 19budget break. The comment on that break already admits it is a corner cut against the ISO outer loop, and a budget break is exactly what would cap the worst window while bits go unspent. - The masking offset,
6.0 + 15.0 * (1.0 - flatness)dB. Speech is the material whose flatness estimate a 1024-point FFT gets most wrong. - The spreading leaks, 0.0032 upward and 0.1 downward, and the absolute
threshold's
1.0 + 400.0 * (rel - 0.35)^2shape.
None of the three has been swept. All of them predate the only metric that can price them.
Round 3: the distortion loop stopped amplifying too late
Round 2 left one question: on the speech sources the worst window barely improves when the bitrate rises by half, so something other than the bit supply is capping it. The distortion loop has three stop conditions and none had been swept.
Two of the three turn out not to be live at all. Raising the round cap from 20
to 60 produces bit-identical output, so the loop never reaches it. Amplifying
one band per round instead of three is a wash. The third is the budget break,
spent * 20 > target_bits * 19 -- stop amplifying once 95% of the frame's bits
are committed -- and it is the whole story:
| break at | reelm@192 worst | hein@192 worst | reelm@128 corr gap |
|---|---|---|---|
| 100% | -3.7 | — | -0.00483 |
| 95% (shipped) | -3.4 | -5.4 | -0.00082 |
| 85% | -6.2 | -6.8 | -0.00068 |
| 70% | -6.2 | -5.6 | -0.00067 |
| 55% | -6.2 | -5.6 | -0.00067 |
The 100% row is the shape of the mistake in the other direction: amplifying a band after the budget is gone does not buy that band anything, it makes the rate loop coarsen every other band to pay for it, and reelm loses 0.0048 correlation. Below 70% the condition stops binding, because the first round already spends more than that. 85% lands.
It is a uniform win -- all fourteen rows, both metrics:
| source | kbit/s | corr gap 95% → 85% | worst 20 ms 95% → 85% |
|---|---|---|---|
| nik | 128 | -0.00099 → -0.00073 | -11.4 → -12.1 |
| nik | 192 | -0.00032 → -0.00030 | -17.7 → -17.6 |
| reelo | 128 | -0.00138 → -0.00103 | -10.3 → -11.1 |
| reelo | 192 | -0.00049 → -0.00045 | -16.3 → -16.3 |
| her | 128 | +0.00115 → +0.00142 | -9.8 → -9.9 |
| her | 192 | +0.00018 → +0.00021 | -14.7 → -14.4 |
| reell | 128 | +0.00006 → +0.00021 | -9.3 → -9.7 |
| reell | 192 | -0.00003 → -0.00002 | -15.2 → -15.1 |
| reelm | 128 | -0.00082 → -0.00068 | -1.5 → -1.6 |
| reelm | 192 | -0.00042 → -0.00040 | -3.4 → -6.2 |
| dl8a | 128 | -0.00111 → -0.00062 | -3.3 → -3.2 |
| dl8a | 192 | -0.00046 → -0.00040 | -7.8 → -8.3 |
| hein | 128 | -0.00101 → -0.00087 | -0.1 → -0.1 |
| hein | 192 | -0.00032 → -0.00030 | -5.4 → -6.8 |
The mean 128 kbit/s correlation gap goes from -0.00058 to -0.00033 and the mean at 192 from -0.00027 to -0.00024. Both gate floors tighten onto the new worst rows: -0.0015 correlation and 13.5 dB of worst-window excess.
Round 4, named
The speech sources are still the trailing ones and the masking model is now the
only candidate left standing for them: hein at 192 kbit/s is 12.6 dB behind
LAME's worst window, and it no longer improves when the loop is allowed to
amplify more or when the bitrate rises. The unswept constants there are the
signal-to-mask offset 6.0 + 15.0 * (1.0 - flatness) dB, the spreading leaks
(0.0032 up, 0.1 down), and the absolute threshold's
1.0 + 400.0 * (rel - 0.35)^2 shape. Speech is exactly the material whose
tonality a 1024-point flatness estimate gets wrong.
Round 4: the encoder declared joint stereo and wrote plain left/right
Round 3 left the gap concentrated on the two spoken-word sources, and speech is
where the two channels are nearly the same signal. Parsing both streams' frame
headers found the reason: our mode_extension was hard-coded to zero on every
frame while the header's channel mode said joint stereo. libmp3lame wrote
mid/side on 2017 of ~2200 frames of the same audio. Our decoder already
implemented mid/side correctly — only the encoder never asked for it, so a
near-mono voice was coded twice, once in each channel, at half the bits each.
The fix is three pieces: both channels of a frame now take the same window
(adding a long block's lines to a short block's would smear one channel's
transient into the other), the frame picks mid/side when the difference of the
channels is quieter than their sum, and mode_ext is written per frame.
Masking thresholds for a mid/side pair are held to whichever channel masked
less, because an error in the side channel lands in both ears.
Both metrics, all fourteen rows, round 3 → round 4:
| source | kbps | corr gap | worst window (ours) |
|---|---|---|---|
| nik | 128 | -0.00073 → -0.00067 | -12.1 → -12.1 |
| nik | 192 | -0.00030 → -0.00028 | -17.6 → -17.1 |
| reelo | 128 | -0.00103 → -0.00103 | -11.1 → -11.3 |
| reelo | 192 | -0.00045 → -0.00044 | -16.3 → -16.1 |
| her | 128 | +0.00142 → +0.00154 | -9.9 → -10.0 |
| her | 192 | +0.00021 → +0.00025 | -14.4 → -14.7 |
| reell | 128 | +0.00021 → +0.00030 | -9.7 → -10.2 |
| reell | 192 | -0.00002 → +0.00001 | -15.1 → -15.7 |
| reelm | 128 | -0.00068 → +0.00076 | -1.6 → -9.2 |
| reelm | 192 | -0.00040 → +0.00001 | -6.2 → -16.5 |
| dl8a | 128 | -0.00062 → -0.00057 | -3.2 → -3.8 |
| dl8a | 192 | -0.00040 → -0.00037 | -8.3 → -8.1 |
| hein | 128 | -0.00087 → +0.00029 | -0.1 → -10.1 |
| hein | 192 | -0.00030 → +0.00001 | -6.8 → -16.1 |
Five rows now beat libmp3lame outright where three did before, and the two spoken-word sources moved from the worst rows in the table to ties at 192 kbit/s. hein at 192 went from 12.6 dB behind LAME's worst window to 3.3 dB; reelm at 128 is now 7 dB ahead. Both gate floors tightened onto the new worst rows: correlation -0.0015 → -0.0012 (reelo at 128), worst-window excess 13.5 → 5.0 dB (dl8a at 192, a wide music mix, which is the case mid/side cannot help).
The decision threshold itself was swept and is flat: 0.5, 1, 2 and "always"
differ by 0.00001 in correlation on the two sources mid/side helps least, while
"never" costs 0.0005. Real music is nowhere near the threshold, so it is a
guard rather than a knob — and what it guards is now a test rather than an
argument: mid_side_follows_the_channel_correlation encodes two channels
carrying the same waveform and two carrying opposite ones, and asserts the
decision goes each way.
What round 4 ruled out on the way
Before the stereo defect surfaced, four candidates were measured and rejected:
the masking offset and the absolute-threshold floor (bit-identical output
across 1e-9…1e-13), reservoir starvation (encoding the worst passage in
isolation gained 1.2 dB), and the distortion loop (amplifying scalefactors is a
net loss in both noise-to-mask and raw-noise orderings). Instrumenting the
loop's exit reasons explained why: 9498 of 9498 granules exit on the budget
break, 9484 of them at round 0. code_with already fills the frame budget, so
the outer loop never runs — the encoder is a pure rate loop and the
psychoacoustic model reaches the output only through the band ordering it never
gets to use. That is the standing structural finding for this crate; the gain
in this round came from a missing feature, not from the model.
Round 5: the frame budget was split evenly across channels that need
different amounts
Mid/side only pays if the quiet channel can give its bits away, and ours could
not: the CBR arm handed every granule-channel the same budget / (granules * channels), so a side channel that is nearly silent still held a quarter of the
frame it had no way to spend. The quantiser codes |xr|^(3/4), so the sum of
that over a granule's lines is the shape of its bit demand; the frame is now
split by it, blended with the even split.
| blend | reelo@128 gap | dl8a@128 gap | reelm@192 worst | reelm@192 vs LAME |
|---|---|---|---|---|
| 0 (even) | -0.00103 | -0.00057 | -16.5 | ahead |
| 0.25 | -0.00016 | +0.00025 | -20.9 | ahead |
| 0.5 | +0.00037 | +0.00077 | -18.2 | ahead |
| 0.7 | +0.00062 | +0.00103 | -15.0 | ahead |
| 1.0 (pure demand) | +0.00079 | +0.00120 | -11.4 | 1.8 dB behind |
Correlation keeps improving all the way to a pure demand split, but the worst window turns around before it: at 1.0 the loud granules take so much of the frame that a quiet one cannot code its own transient, and reelm at 192 falls behind the reference. 0.7 takes nearly all of the correlation gain and leaves every row's worst window ahead.
The result is that all fourteen rows now beat libmp3lame on both metrics:
| source | kbps | corr gap | worst window ours / LAME |
|---|---|---|---|
| nik | 128 | +0.00048 | -15.0 / -11.2 |
| nik | 192 | +0.00006 | -20.9 / -17.2 |
| reelo | 128 | +0.00062 | -15.3 / -9.3 |
| reelo | 192 | +0.00009 | -21.4 / -16.1 |
| her | 128 | +0.00193 | -9.4 / -5.4 |
| her | 192 | +0.00040 | -14.3 / -11.1 |
| reell | 128 | +0.00069 | -10.2 / -6.1 |
| reell | 192 | +0.00012 | -15.7 / -11.7 |
| reelm | 128 | +0.00086 | -10.2 / -2.0 |
| reelm | 192 | +0.00004 | -15.0 / -13.2 |
| dl8a | 128 | +0.00103 | -9.0 / -6.0 |
| dl8a | 192 | +0.00020 | -13.7 / -12.2 |
| hein | 128 | +0.00029 | -11.9 / -7.6 |
| hein | 192 | +0.00002 | -19.2 / -19.4 |
Both floors move onto that: the correlation floor is now zero — a row falling behind the reference at all is the regression — and the worst-window excess is 1.0 dB, set by hein at 192, the one row still (0.2 dB) behind.
The split is CBR only. VBR picks its bitrate from the noise-to-mask ratio a
candidate reaches, and that mapping is calibrated to what an even split
produces: with the demand split in the VBR arm, quality 0.5 settles at 154.6
kbit/s instead of 208 for the same noise-to-mask bar. That is plausibly the
encoder getting more quality per bit, but nothing here measures VBR against a
reference, so it is deferred — unblocked by a VBR reference gate against
lame -V, which is the next round of this lane.
Round 6: VBR gets a reference gate, and loses to it
Round 5 left the demand split out of the VBR arm because nothing could judge
it. This round builds the judge: real_library_vbr_vs_lame encodes each source
at quality 0.3 and 0.6, encodes every rung of libmp3lame's VBR ladder
(V0..V9) on the same audio, and compares against the rung that actually spent
what we spent.
Picking the rung by its nominal rate does not work, and the first attempt did: libmp3lame's V6 is nominally 115 kbit/s but lands near 60 on speech, because it drops the rate hard on easy material. Matching on the nominal rate compared us against a rung spending half what we did and made a losing table look winning.
Matched on the measured rate, the shipped VBR lost almost everywhere -- 11 of 14 rows behind on correlation, and 5 to 8 dB behind on the worst 20 ms window while spending the same bits. Two changes fixed it:
- The demand split, in the VBR arm too. Same change round 5 made to CBR; it took the table from 3 rows ahead to 10.
- The quality bar recalibrated. The knob's documented contract is a mean
rate, not a masking bar, so reaching the same bar for fewer bits has to be
spent rather than pocketed.
quality_thresholdnow tightens by1.6 - 12 qdB, fitted to two points on the library: quality 0.3 lands at 162 kbit/s against its promised 163, and quality 0.6 at 232 against 230.
| source | q | corr gap | worst window ours / LAME | ours / LAME kbit/s | rung |
|---|---|---|---|---|---|
| nik | 0.3 | +0.00046 | -11.2 / -14.0 | 170.6 / 169.6 | V2 |
| nik | 0.6 | +0.00019 | -18.5 / -19.7 | 238.1 / 224.7 | V0 |
| reelo | 0.3 | +0.00006 | -12.2 / -15.0 | 164.6 / 162.6 | V2 |
| reelo | 0.6 | +0.00012 | -21.3 / -20.2 | 232.7 / 210.0 | V0 |
| her | 0.3 | -0.00042 | -12.5 / -14.7 | 221.6 / 224.9 | V0 |
| her | 0.6 | +0.00032 | -19.6 / -14.7 | 303.2 / 224.9 | V0 |
| reell | 0.3 | +0.00017 | -11.3 / -12.6 | 197.5 / 194.2 | V1 |
| reell | 0.6 | +0.00022 | -18.5 / -14.4 | 276.2 / 219.6 | V0 |
| reelm | 0.3 | +0.00021 | -10.6 / -8.8 | 138.3 / 133.9 | V1 |
| reelm | 0.6 | +0.00016 | -16.0 / -10.8 | 207.9 / 154.0 | V0 |
| dl8a | 0.3 | +0.00062 | -8.3 / -9.3 | 150.9 / 158.0 | V3 |
| dl8a | 0.6 | +0.00012 | -17.0 / -13.7 | 226.8 / 227.1 | V0 |
| hein | 0.3 | +0.00019 | -7.5 / -9.3 | 129.8 / 135.3 | V1 |
| hein | 0.6 | +0.00019 | -16.5 / -12.0 | 189.2 / 153.7 | V0 |
Thirteen of fourteen rows are ahead. The floors: correlation -0.0005 (her at 0.3, which is behind while spending 1.5% less), worst-window excess 3.5 dB (nik and reelo at 0.3, both 2.8 dB behind — the frames our VBR calls easy are where it still under-spends against a transient), and a rate ceiling of 8% of the matched rung, with V0 rows exempt because libmp3lame cannot spend more there and the premium would measure its ceiling rather than our appetite.
The V0 rows are the standing weakness this gate cannot yet price: at quality 0.6 on four sources we spend 20-35% more than libmp3lame's top rung and buy quality with it, and nothing here says whether that trade is the one a listener would pick. Sitting off the top of the reference ladder is the next round's problem.
Closes the round 5 deferral: VBR now has a reference gate, and the split it was waiting on is in.