Inside a MP3 File: Signature, Structure and MIME Type
Learn how MP3 is identified and what its internal components mean.
Inside an MP3 File: Signature, Structure and MIME Type
The Sync Word as a Validation Gate
A .mp3 extension is advisory. The only reliable identifier is the sync word occupying the first 11 bits of a valid MPEG Audio frame. For MPEG-1 Audio Layer III, this is the constant bit pattern 1111 1111 111 (eleven consecutive 1-bits), which produces the byte-aligned value 0xFFE0 when masked against the frame header's first two bytes. A VBR file with a CRC check present will show 0xFFFB in the same position.
Any validation routine must test for this pattern at offset zero. If the masked first two bytes do not match 0xFFE0 or 0xFFFB, the file is not a valid MP3 stream regardless of its extension or reported MIME type. This check is specified in the Library of Congress format description for MPEG Audio Layer III (FDD000012) as the definitive structural entry point.
Frame Anatomy and Hard Limits
An MP3 file is a concatenation of independent frames. Each frame is a self-describing packet containing a 4-byte header, an optional 16-bit CRC, and a compressed audio data payload. The header encodes five fields that determine the frame's size and playback parameters:
- Sync word (11 bits):
0x7FFmasked to the upper bits of the first two bytes. - MPEG version (2 bits):
11for MPEG-1,10for MPEG-2,00for MPEG-2.5. - Layer (2 bits):
01for Layer III. Values00,10, and11are reserved or indicate Layer I/II. - Bitrate index (4 bits): An index into a fixed lookup table. For MPEG-1 Layer III, valid indices map to 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, and 320 kbps. Index
0means "free format" (rare, decoder-dependent). Index15is forbidden. - Sampling rate frequency (2 bits):
00= 44.1 kHz,01= 48 kHz,10= 32 kHz for MPEG-1. MPEG-2 and MPEG-2.5 use different mappings (22.05, 24, 16 kHz and 11.025, 12, 8 kHz respectively). - Padding bit (1 bit): If set, one extra slot of data is appended to the frame.
- Mode (2 bits): Stereo (
00), Joint Stereo (01), Dual Channel (10), Mono (11).
Frame length in bytes is computed as:
frame_length = (144 * bitrate_in_bps / sample_rate) + padding
This formula is deterministic. A frame header claiming a length that, when added to the frame's start offset, exceeds the file's total byte count is a frame length overflow — an unrecoverable structural corruption.
Frame Boundary Verification
Because frames are independent, a decoder can seek by scanning for sync words and jumping forward by computed frame lengths. The validation sequence is:
- Locate the next
0xFFE0or0xFFFBpattern. - Parse the 4-byte header to extract bitrate index, sample rate, and padding bit.
- Calculate
frame_lengthusing the formula above. - Advance the read pointer by
frame_lengthbytes. - Verify that the next two bytes at the new position form a valid sync word.
If step 5 fails, the file contains a frame boundary error. The decoder must either resynchronize by scanning for the next valid sync word (producing a gap in playback) or halt. The LOC specification treats inconsistent frame boundaries as a malformed stream. A file with zero valid frames — no sync words at all, or sync words that never align at computed boundaries — is not an MP3 file and cannot be repaired.
The Xing Header in VBR Streams
Variable bitrate MP3 files vary the bitrate index per frame. This breaks the simple duration calculation file_size / bitrate that works for constant bitrate (CBR) streams. To provide seek-table data and accurate duration without scanning the entire file, many VBR encoders insert a Xing header (also called an Info header) into the first frame's ancillary data region.
The Xing header is located at byte offset 36 or 37 within the first frame (depending on the mode field) and begins with the ASCII tag Xing or Info. Its structure includes:
- Flags (4 bytes): Bitmask indicating which fields are present.
- Number of frames (4 bytes, if flag
0x01set): Total frame count in the stream. - File size in bytes (4 bytes, if flag
0x02set): Total stream size. - TOC (table of contents) (100 bytes, if flag
0x04set): A seek index dividing the file into 100 equal-duration segments, each stored as a byte-scaled offset. - VBR scale (4 bytes, if flag
0x08set): Encoder-specific quality metric.
This header is not part of the MPEG standard. It originated with the Xing encoder and was adopted by LAME and others as a de facto convention. A VBR file without a Xing header remains structurally valid per the MPEG specification, but players must scan the entire stream to determine duration and enable seeking. The LOC format description does not require the Xing header for conformance.
MIME Type and Delivery
The IANA-registered MIME type for MPEG Audio Layer III is audio/mpeg, defined in RFC 3003. This is the canonical value for HTTP Content-Type headers, SMTP attachments, and any context requiring MIME typing.
Non-standard types that appear in practice:
audio/mp3: Historically used but never registered. Some older user agents accept it, but strict validators will reject it.audio/mpeg3: A misnomer conflating MPEG-1 Layer III with the unrelated MPEG-3 standard (which was abandoned). Not reliable.audio/x-mpeg: An experimental prefix variant. Deprecated.
For HTTP delivery, the correct header is:
Content-Type: audio/mpeg
For forced download rather than inline playback, add:
Content-Disposition: attachment; filename="file.mp3"
A file served as application/octet-stream with a .mp3 extension is treated as an opaque binary blob. This is a strong signal of misconfiguration or corruption and should trigger a sync-word validation before any playback attempt.
Unrecoverable Structural Conditions
Three structural states are irrecoverable without re-encoding from a lossy source:
- Zero valid frames: No sync words exist, or every sync word fails the boundary-alignment check. The file is not an MP3 stream.
- Frame length overflow: A parsed frame header specifies a length exceeding the remaining file size. This typically indicates a corrupted bitrate index or padding bit. The frame and all subsequent data are unreadable.
- Inconsistent sample rate across frames: The MPEG specification permits per-frame sample rate changes, but virtually all decoders and hardware players expect a single sample rate for the entire stream. Mixed-rate files will cause decoder resets, clicks, or outright rejection.
Partial recovery is possible by scanning for aligned sync words, extracting contiguous valid frames, and re-encoding the result. This process is lossy and degrades audio quality further.
Reference
Library of Congress, Sustainability of Digital Formats: MPEG Audio Layer III (MP3).
https://www.loc.gov/preservation/digital/formats/fdd/fdd000012.shtml