Skip to content

fix(audio): support full MPEG Layer III sync header detection for MP3 - #165

Open
AdamMagued wants to merge 1 commit into
h2non:masterfrom
AdamMagued:fix-mp3-layer-iii-syncword-detection
Open

AdamMagued wants to merge 1 commit into
h2non:masterfrom
AdamMagued:fix-mp3-layer-iii-syncword-detection

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Fixes #91

Root Cause Analysis

Previously, matchers.Mp3 only matched ID3 tags (0x49 0x44 0x33) and MPEG-1 Layer III audio frames without CRC protection (0xFF 0xFB). However, MP3 audio frames can use other valid MPEG versions (such as MPEG-2 Layer III used for lower sample rates like 16, 22.05, 24 kHz, or MPEG-2.5) as well as CRC-protected frames (where the protection bit is set to 0). When an MP3 without an ID3 container begins directly with an MPEG-2 Layer III frame header (e.g., 0xFF 0xF3 or 0xFF 0xF2), matchers.Mp3 returned false, causing the file to not be detected as MP3.

Changes

  • Updated matchers.Mp3 to validate all MPEG Layer III frame headers using standard bitmask checks:
    • First byte must be 0xFF.
    • Second byte sync word bits (0xE0) combined with Layer III description bits (0x02) are checked via (buf[1] & 0xE6) == 0xE2.
    • Audio version bits (buf[1] & 0x18) are verified not to equal 0x08 (the reserved version ID), properly admitting MPEG-1, MPEG-2, and MPEG-2.5 Layer III frames with or without CRC.
  • Added comprehensive unit tests in matchers/audio_test.go covering ID3 tags, MPEG-1, MPEG-2, and MPEG-2.5 Layer III sync headers with and without CRC, as well as invalid versions, other MPEG layers, and non-MPEG headers.
  • Added test cases in match_test.go verifying end-to-end detection.

Test Verification

  • All tests pass via go test -v ./... with 100% pass rate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Specific MP3 file not detected

1 participant