MPEG Audio Frame
Header
An MPEG audio file is separated in smaller parts called frames. Each frame
is independent. It has its own header and audio informations. There is no file
header. Therefore, you can cut any part of MPEG file and play it correctly.
When you want to read info about an MPEG file, it is usually enough to find
the first frame, read its header and assume that the other frames are the same
(which may not be always the case).
The frame header is constituated by the very first four bytes (32bits) in a
frame. The first eleven bits of a frame header are always set and they are
called "frame sync". Therefore, you can search through the file for
the first occurence of eleven bits set (meaning that you have to find a byte
with a value of 255, and followed by a byte with its three most significant
bits set). Then you read the whole header and check if the values are correct.
You will see in next table the exact meaning of each bits in the header, and
which values may be checked for validity. Each value that is specified as
reserved, invalid, bad, or not allowed should indicate an invalid header.
Frames may have a CRC check, but it's pretty rare. The CRC is 16 bits long
and, if it exists, it follows the frame header. After the CRC comes the audio
data. You may calculate the length of the frame and use it if you need to read
other headers too or just want to calculate the CRC of the frame, to compare
it with the one you read from the file. This is actually a very good method to
check the MPEG header validity.
Here is "graphical" presentation of the header content. The
letters are used to indicate the different fields. In the table, you can see
the details about the content of each field.
AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM
Sign | Length (bits) | Position (bits) | Description |
A | 11 | (31-21) | Frame sync (all bits set) |
B | 2 | (20,19) | MPEG Audio version
00 - MPEG Version 2.5
01 - reserved
10 - MPEG Version 2
11 - MPEG Version 1 |
C | 2 | (18,17) | Layer description
00 - reserved
01 - Layer III
10 - Layer II
11 - Layer I |
D | 1 | (16) | Protection bit
0 - Protected by CRC (16bit crc follows header)
1 - Not protected |
E | 4 | (15,12) | Bitrate index
bits | V1,L1 | V1,L2 | V1,L3 | V2,L1 | V2,L2 | V2,L3 |
0000 | free | free | free | free | free | free |
0001 | 32 | 32 | 32 | 32 | 32 | 8 (8) |
0010 | 64 | 48 | 40 | 64 | 48 | 16 (16) |
0011 | 96 | 56 | 48 | 96 | 56 | 24 (24) |
0100 | 128 | 64 | 56 | 128 | 64 | 32 (32) |
0101 | 160 | 80 | 64 | 160 | 80 | 64 (40) |
0110 | 192 | 96 | 80 | 192 | 96 | 80 (48) |
0111 | 224 | 112 | 96 | 224 | 112 | 56 (56) |
1000 | 256 | 128 | 112 | 256 | 128 | 64 (64) |
1001 | 288 | 160 | 128 | 288 | 160 | 128 (80) |
1010 | 320 | 192 | 160 | 320 | 192 | 160 (96) |
1011 | 352 | 224 | 192 | 352 | 224 | 112 (112) |
1100 | 384 | 256 | 224 | 384 | 256 | 128 (128) |
1101 | 416 | 320 | 256 | 416 | 320 | 256 (144) |
1110 | 448 | 384 | 320 | 448 | 384 | 320 (160) |
1111 | bad | bad | bad | bad | bad | bad |
NOTES: All values are in kbps
V1 - MPEG Version 1
V2 - MPEG Version 2 and Version 2.5
L1 - Layer I
L2 - Layer II
L3 - Layer III
"free" means variable bitrate.
"bad" means that this is not an allowed value
The values in parentheses are from different sources which claim that those
values are valid for V2,L2 and V2,L3. If anyone can confirm please let me
know.
|
F |
2 |
(11,10) |
Sampling rate frequency index (values are in Hz)
bits | MPEG1 | MPEG2 | MPEG2.5 |
00 | 44100 | 22050 | 11025 |
01 | 48000 | 24000 | 12000 |
10 | 32000 | 16000 | 8000 |
11 | reserv. | reserv. | reserv. |
|
G | 1 | (9) | Padding bit
0 - frame is not padded
1 - frame is padded with one extra bit |
H | 1 | (8) | Private bit (unknown purpose) |
I | 2 | (7,6) | Channel Mode
00 - Stereo
01 - Joint stereo (Stereo)
10 - Dual channel (Stereo)
11 - Single channel (Mono) |
J | 2 | (5,4) | Mode extension (Only if Joint stereo)
value | Intensity stereo | MS stereo |
00 | off | off |
01 | on | off |
10 | off | on |
11 | on | on |
|
K | 1 | (3) | Copyright
0 - Audio is not copyrighted
1 - Audio is copyrighted |
L | 1 | (2) | Original
0 - Copy of original media
1 - Original media |
M | 2 | (1,0) | Emphasis
00 - none
01 - 50/15 ms
10 - reserved
11 - CCIT J.17 |
How to calculate frame size
Read the BitRate, SampleRate and Padding (as value of one or zero) of the
frame header and use the formula:
FrameSize = 144 * BitRate / SampleRate + Padding
Example: BitRate = 128000, SampleRate=441000, Padding=0 ==>
FrameSize=417 bytes
MPEG Audio
Tag MP3v1
The TAG is used to describe the MPEG Audio file. It contains information
about artist, title, album, publishing year and genre. There is some extra
space for comments. It is exactly 128 bytes long and is located at very end of
the audio data. You can get it by reading the last 128 bytes of the MPEG audio
file.
AAABBBBB BBBBBBBB BBBBBBBB BBBBBBBB
BCCCCCCC CCCCCCCC CCCCCCCC CCCCCCCD
DDDDDDDD DDDDDDDD DDDDDDDD DDDDDEEE
EFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFG
Sign | Length (bytes) | Position (bytes) | Description |
A | 3 | (0-2) | Tag identification.
Must contain 'TAG' if tag exists and is correct. |
B | 30 | (3-32) | Title |
C | 30 | (33-62) | Artist |
D | 30 | (63-92) | Album |
E | 4 | (93-96) | Year |
F | 30 | (97-126) | Comment |
G | 1 | (127) | Genre |
The specification asks for all fields to be padded with null character
(ASCII 0). However, not all applications respect this (an example is WinAmp
which pads fields with <space>, ASCII 32).
There is a small change proposed in MP3v1.1 structure. The last byte
of the Comment field may be used to specify the track number of a song in an
album. It should contain a null character (ASCII 0) if the information is
unknown.
Genre is a numeric field which may have one of the following values:
0 | 'Blues' | 20 | 'Alternative' | 40 | 'AlternRock' | 60 | 'Top 40' |
1 | 'Classic Rock' | 21 | 'Ska' | 41 | 'Bass' | 61 | 'Christian Rap' |
2 | 'Country' | 22 | 'Death Metal' | 42 | 'Soul' | 62 | 'Pop/Funk' |
3 | 'Dance' | 23 | 'Pranks' | 43 | 'Punk' | 63 | 'Jungle' |
4 | 'Disco' | 24 | 'Soundtrack' | 44 | 'Space' | 64 | 'Native American' |
5 | 'Funk' | 25 | 'Euro-Techno' | 45 | 'Meditative' | 65 | 'Cabaret' |
6 | 'Grunge' | 26 | 'Ambient' | 46 | 'Instrumental Pop' | 66 | 'New Wave' |
7 | 'Hip-Hop' | 27 | 'Trip-Hop' | 47 | 'Instrumental Rock' | 67 | 'Psychadelic' |
8 | 'Jazz' | 28 | 'Vocal' | 48 | 'Ethnic' | 68 | 'Rave' |
9 | 'Metal' | 29 | 'Jazz+Funk' | 49 | 'Gothic' | 69 | 'Showtunes' |
10 | 'New Age' | 30 | 'Fusion' | 50 | 'Darkwave' | 70 | 'Trailer' |
11 | 'Oldies' | 31 | 'Trance' | 51 | 'Techno-Industrial' | 71 | 'Lo-Fi' |
12 | 'Other' | 32 | 'Classical' | 52 | 'Electronic' | 72 | 'Tribal' |
13 | 'Pop' | 33 | 'Instrumental' | 53 | 'Pop-Folk' | 73 |
'Acid Punk' |
14 | 'R&B' | 34 | 'Acid' | 54 | 'Eurodance' | 74 | 'Acid Jazz' |
15 | 'Rap' | 35 | 'House' | 55 | 'Dream' | 75 | 'Polka' |
16 | 'Reggae' | 36 | 'Game' | 56 | 'Southern Rock' | 76 | 'Retro' |
17 | 'Rock' | 37 | 'Sound Clip' | 57 | 'Comedy' | 77 | 'Musical' |
18 | 'Techno' | 38 | 'Gospel' | 58 | 'Cult' | 78 | 'Rock & Roll' |
19 | 'Industrial' | 39 | 'Noise' | 59 | 'Gangsta' | 79 | 'Hard Rock' |
Any other value should be considered as 'Unknown'
|
MPEG Audio
Tag MP3v2
This is new proposed TAG format which is different than MP3v1 and MP3v1.1.
Complete tech specs for it may be found at http://www.id3.com/.
Created on September 1998. by Predrag
Supurovic.
Thanks to Jean
Nicolle for debugging and polishing of this document.
This document may be changed. Check http://www.dv.co.yu/mp3list/mpeghdr.htm
for updates.
You may use it freely. If you can help us make it more accurate, please do.