Skip to content

fix(mp3): decoder emits the Xing/LAME header frame and encoder delay as audio #4129

Closed

Description

@zackees

What happens

fl::Mp3Decoder decodes MP3 frames with a raw mp3dec_decode_frame_r loop over the byte stream. That loop does not understand container metadata, so for a LAME- or Xing-encoded file it:

  1. decodes the VBR tag frame as audio. The Xing/LAME header occupies a real, valid MPEG frame at the start of the stream. It contains no music; decoding it yields a frame of noise-adjacent output.
  2. does not apply the encoder delay/padding the LAME header specifies, so the first ~576–1152 samples of genuine encoder priming are emitted rather than trimmed.

Practically: playing a LAME-encoded MP3 through FastLED starts with a short burst of garbage before the music. Most MP3s in the wild are LAME-encoded.

Evidence

ci/codec_conformance/run.py carries this as its single explicit exception:

EXCEPTED l3-nonstandard-sin1k0db_lame_vbrtag: 2.85 dB

2.85 dB against a reference that assumes gapless trimming, where every other Layer III vector scores above 100 dB.

It is not an arithmetic defect. The upstream float build scores identically — 2.85 dB — so this is not something the fixed-point port introduced. Frame-shifting the comparison does not rescue it either (best alignment reaches 30.15 dB at a 3-frame skip, still far below the floor), which is consistent with a leading garbage frame plus an untrimmed delay rather than a simple constant offset.

Where the missing logic lives

Upstream minimp3 handles this in minimp3_ex.h — mp3dec_ex_open / mp3dec_load parse the ID3/APE tags, read the Xing/LAME header, and apply delay/padding for gapless playback. FastLED vendors only minimp3.h and calls the raw frame API, deliberately, because minimp3_ex.h brings a stdio/file layer that does not belong on an MCU.

So the fix is to implement the small part of that we actually need against the byte stream we already have, not to vendor minimp3_ex.h.

Related: tags are not skipped either

The same conformance run shows four vectors decoding zero frames:

  • l3-nonstandard-id3v1
  • l3-nonstandard-id3v2
  • l3-nonstandard-apetag
  • l3-nonstandard-id3v1-apetag

These are the tag-handling conformance cases. A file whose leading bytes are an ID3v2 tag is not decoded at all by the raw loop. That is arguably more serious than the delay problem, since ID3v2 tags are near-universal in real files — worth confirming against a real tagged MP3 before sizing the work, as the sync-word scan may recover on many real files where it fails on these deliberately-shaped ones.

Acceptance criteria

  • Skip a leading ID3v2 tag (ID3 magic, syncsafe size) and a leading APE tag before the first sync-word search.
  • Detect the Xing/Info/VBRI header in the first frame and not emit it as audio.
  • Apply the LAME header's encoder delay and padding so playback starts at the first real sample.
  • l3-nonstandard-sin1k0db_lame_vbrtag clears the 60 dB ISO floor and moves out of KNOWN_EXCEPTIONS in ci/codec_conformance/run.py.
  • The four tag vectors above decode a non-zero number of frames.
  • Memory ledger unchanged: this is parsing over the existing stream buffer, and must not add per-frame allocation.

Found while auditing whether the MP3 decoder is actually verified against a gold reference (#4127). The conformance suite added there is what makes this measurable.

Refs #4127

Activity

  1. zackees commented on Sep 1, 2026

    @zackees
    MemberAuthor

    Correction and quantification

    I filed this with two claims. Having measured both: one is confirmed and now has exact numbers, the other is wrong and I am withdrawing it.

    Confirmed: the Xing tag frame and encoder delay are emitted as audio

    A sample-level offset scan against the reference pins it exactly:

    decoded 730368   reference 725760   excess 4608
    best alignment at offset 4514 samples -> PSNR 108.18 dB
    

    108.18 dB is right in line with every other Layer III vector. So the decode is correct; it is simply offset. And the offset decomposes precisely:

    component samples/channel
    Xing/LAME tag frame emitted as audio 1152
    LAME encoder delay (576 + 528 + 1) 1105
    total 2257

    2257 x 2 channels = 4514 interleaved samples, which is the measured best offset to the sample. That is as clean a confirmation as this kind of thing gets: skip the tag frame and honour the delay, and the vector passes at 108 dB instead of 2.85.

    Withdrawn: ID3v2/APE tags do not break real files

    I wrote that a file whose leading bytes are an ID3v2 tag "is not decoded at all", and flagged it as possibly more serious than the delay. I hedged that it needed confirming against a real tagged MP3 before sizing. I have now confirmed it, and it is not true.

    Taking a real multi-frame file (l3-hecommon, 30 frames) and attaching tags:

    input frames PSNR
    plain 30 123.24 dB
    + 2058-byte ID3v2 header 30 123.24 dB
    + APE tag header 30 123.24 dB
    + ID3v1 trailer 29 123.24 dB

    Byte-identical output. The sync-word scan walks past leading tags without help.

    (The ID3v1 trailer case is a small bonus: it suppresses the spurious 30th frame, so the decoded length matches the reference exactly.)

    Why the four tag vectors decode zero frames is unrelated to tags. They are single-frame files of 274-2048 bytes, and minimp3 will not decode a lone frame without lookahead to validate it. Upstream's float build produces zero frames on all four as well, so it is neither a regression nor specific to the fixed-point port. Whether a one-frame stream should decode is a separate question, and a much smaller one than "tags break playback" — which is what I incorrectly implied.

    Revised scope

    • Detect Xing/Info/VBRI in the first frame and do not emit it as audio.
    • Apply the LAME header's encoder delay and padding.
    • l3-nonstandard-sin1k0db_lame_vbrtag clears the ISO floor and leaves KNOWN_EXCEPTIONS in ci/codec_conformance/run.py.
    • No new per-frame allocation; this is parsing over the existing stream buffer.

    Dropped from scope: ID3v2/APE skipping (unnecessary -- the sync scan already handles it) and the four single-frame tag vectors (upstream behaviour, not tag-related).

  2. moved this from Triage to Done in FastLED Trackeron Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions