forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on 6828b152fbb3de7dbfdfdce189e9b72de86552f7Filtered to crates/ec-mp3/tests/encode_matrix.rsClear filter

2026-08-21

fix(mp3): speed up VBR silenceAhmet Kaan Gümüş · f0cad7a
fix(mp3): speed up VBR silence

Add a zero-granule fast path, make VBR candidate search local to the previous frame, and recalibrate the quality threshold curve on music clips.

2026-08-20

fix(mp3): state the masking threshold in MDCT power units so NMR means somethingAhmet Kaan Gümüş · 60c1bbb
fix(mp3): state the masking threshold in MDCT power units so NMR means something

psy_threshold summed an unnormalised 1024-point FFT while code_with measures
noise as summed squared MDCT lines; the two differ by exactly 2^17 (512 P per
bin from the sine-windowed FFT, P/288 per line from the unity-gain filterbank
and the 1/9-scaled 36-point MDCT, 512/576 bins per line), so every band read
as "under its mask" and the model was inert. Derivation is in the doc comment;
a unit test checks white noise lands the signal-to-mask offset over the mask.

The -48 dB corner-cut in quality_threshold goes; the q mapping is 24 dB across
the range, centred 4 dB over the mask (documented calibration: q 0.5 lands
210-225 kbit/s on mp3src-stereo-48000 and two real tracks, above the CBR-192
bar). The outer loop now takes the worst over-mask bands first; it still stops
at the budget, because letting it run past (ISO style) costs 12-36% RMS under
a model this coarse and the bars are an RMS metric (corner-cut noted inline).

cbr_output_is_unchanged pinned bytes of the inert-model CBR; CBR bytes
legitimately move now, so it is replaced by cbr_quality_holds_its_floor,
gating corr >= 0.99999 and RMS <= 0.025% (the pinned stream measured
0.999999 / 0.020%).
feat(mp3): true VBR — per-frame bitrate index chosen by mean noise-to-mask ratioAhmet Kaan Gümüş · 252530f
feat(mp3): true VBR — per-frame bitrate index chosen by mean noise-to-mask ratio

vbr_quality now varies the bitrate index per frame: the frame's granules are
coded against each candidate's share with the CBR distortion loop, and the
cheapest candidate whose mean noise-to-mask ratio is <= 1 wins (bisection
over the table). quantise_granule reports that ratio. The VBR threshold
scale absorbs the FFT-vs-MDCT unit gap (-48 dB at quality 0.5); CBR output
is byte-identical (hash pinned in cbr_output_is_unchanged).

2026-08-14

fix(mp3): gapless Info tag, and the region split a switched granule must useAhmet Kaan Gümüş · 09040eb
fix(mp3): gapless Info tag, and the region split a switched granule must use

The encoder now writes a real Xing/Info header — frame and byte counts plus
the encoder delay and padding — so a decoder that honours it hands back
exactly the samples that went in. ffmpeg does: a two-second timeline clip
encodes and decodes back to 88200 samples per channel at lag zero, where
before it came back 1728 samples late with no way to know it, and 72 samples
short because the granules held for block-switching lookahead were dropped at
the end instead of flushed.

Chasing that alignment surfaced two real defects, both invisible until the
comparison was made at lag zero over the whole stream:

- A granule that switches windows does not transmit its region boundaries;
  the decoder derives them (three short bands for a short block, eight long
  bands for a start or stop one). The encoder was choosing its own split and
  writing the tables for it, so every switched granule parsed as different
  tables. Bit accounting still landed exactly on part2_3_length, every
  fixture still decoded, and only the switched granules came back as noise --
  47x the intended energy in the worst one measured.
- Short-block spectra are coded band by band and window by window, which is
  not the order the transform produces them in. The decoder's reorder had no
  inverse in the encoder.

Both now have tests that fail without the fix: one drives an attack that
forces the switch and checks the granules around it through our own decoder,
one runs the whole window sequence through the transform pair directly.

Also: scalefactors for short blocks (they had none, so their noise was flat
across the spectrum); a ceiling on scalefactor amplification, since the
second partition's length is at most three bits and a band above the split
cannot carry more than 7 -- the quantiser used what it wanted and the decoder
rebuilt the band with zero; the window-type sequence now carries across frame
boundaries, where it used to restart and leave a start window facing a normal
one; and a frame of priming silence, so the transform's startup lands before
the audio rather than on top of it.

Measured on W2's timeline content, ours at lag zero against the incumbent
rusty_mp3 0.6.1 at its own lag, RMS error against the source:

  128k  1.173e-4 vs 1.143e-4    256k  3.986e-5 vs 3.985e-5
  192k  6.027e-5 vs 6.110e-5    320k  2.535e-5 vs 2.567e-5

Correlation is 1.000000 at 256k and above. The encode fixture matrix is
unchanged in shape and every entry still beats its bar, now measured at lag
zero rather than at the best shift.
feat(mp3): Layer III decoder and encoder, tables measured not copiedAhmet Kaan Gümüş · 1325107
feat(mp3): Layer III decoder and encoder, tables measured not copied

ec-mp3 decodes MPEG-1, MPEG-2 LSF and MPEG-2.5 Layer III and encodes CBR
MPEG-1 Layer III, and shims/rusty_mp3 carries the incumbent's name, version
and call shape over it.

Layer III's Huffman code tables and its 512-tap polyphase window are
normative data no formula produces. Instead of copying them out of an
existing implementation, scripts/mp3-tables measures them against a
reference decoder: it writes legal frames whose main-data bits it chooses,
decodes them with ffmpeg, and reads the answer back through a model of the
decode chain. Every code tree is walked to its leaves and checked for a
Kraft sum of exactly 1, complete coverage of its value grid and no
duplicates; the window is least-squares fitted to a 1.6e-6 residual, the
f32 noise floor of the decoder measured. The same rig produced the MPEG-1
long scalefactor band widths, which came out equal to the published
layouts, and it corrected two things a description would not have: the
MPEG-2.5 11.025 and 12 kHz short-block layouts are the 16 kHz ones, and the
short-block region boundary is three scalefactor bands rather than the
fixed 36 lines that only holds where those bands are four wide.

Decoder: header and CRC, side info, scalefactors (MPEG-1 with scfsi and the
LSF partition scheme), all Huffman tables, requantisation, mid/side and
intensity stereo, short-block reorder, alias reduction, IMDCT with the four
block windows, and a DCT-based polyphase bank from ec-dsp. ID3v2 and the
Xing/LAME/VBRI header frame are skipped, junk is resynced over, and
free-format is refused by name rather than guessed at.

Encoder: polyphase analysis (the synthesis bank's adjoint, reconstructing
to its -85 dB design limit), MDCT with block switching, an FFT-based
masking model whose gaps are stated in the module docs, inner rate and
outer distortion loops, Huffman table and region selection, and a bit
reservoir that emits frames one behind so a granule can start inside the
previous frame's payload.

Measured, all against ffmpeg:
- decode: 47 fixtures (nine sample rates, mono/stereo/joint/simple, CBR and
  VBR) at correlation 1.000000, 333-3232x realtime
- encode: at or above rusty_mp3 0.6.1 on the same corpus and metric at
  128/192/320 kbit/s, mono and stereo, every stream decoding without an
  ffmpeg warning