forge
Explore
Log in

Don't you dare go Hollow

Commits

Commits on b07f07aeecbefcfa032bccd431e82dd62a52ac44Filtered to crates/ec-mp3/src/lib.rsClear filter

2026-08-21

2026-08-20

fix(mp3): propagate real decode errors from Mp3Reader::take, and fix a test comparison that never saw themAhmet Kaan Gümüş · cc3e0e3
fix(mp3): propagate real decode errors from Mp3Reader::take, and fix a test comparison that never saw them

Root cause of the "open: NeedMore" symptom on this file was never in
ec-mp3's per-packet frame/reservoir bookkeeping — that already decodes
this cover-art-tagged, CRC-less 192kbps/48kHz stream correctly one
frame per packet, matching ffmpeg sample-for-sample once the LAME
encoder's lead-in is accounted for (measured lag 2210 interleaved
samples = 1105 stereo frames = exactly StreamInfo::initial_padding).

Two real bugs, both now fixed:

- Mp3Decoder::take() used `while let Ok(frame) = self.reader.next_frame()`,
  so any real decode error (corrupt CRC, unsupported layer) — not just
  the expected "nothing more yet" NeedMore/Eof — was silently dropped
  instead of reaching send_packet's caller. take() now returns Result
  and only swallows NeedMore/Eof.

- The new real-file regression test compared decoded audio against
  ffmpeg without accounting for initial_padding: ffmpeg trims the
  LAME/Xing encoder's lead-in from its PCM output; ec-mp3's raw decoder
  intentionally does not (registry.rs: callers must count and drop it
  themselves). The mismatch alone produced corr=0.53; trimming
  `initial_padding * channels` off the front before comparing gets
  corr>0.999.

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