转自:http://blog.csdn.net/sunshine1314/article/details/251432
MP3文件格式解析
Peter Lee 2008-06-05
目录
一、概述
二、整个MP3文件结构
三、MP3帧格式
1. 帧头格式
2. MAIN_DATA
四、ID3标准
1. ID3V1
2. ID3V2
五、MP3文件实例剖析
六、资料
一、 概述
MP3 文件是由帧(frame)构成的,帧是 MP3 文件最小的组成单位。MP3 的全称应为 MPEG1 Layer-3 音频
文件,MPEG(Moving Picture Experts Group)在汉语中译为活动图像专家组,特指活动影音压缩标准,MPEG
音频文件是 MPEG1 标准中的声音部分,也叫 MPEG 音频层,它根据压缩质量和编码复杂程度划分为三层,即
Layer-1、Layer2、Layer3,且分别对应 MP1、MP2、MP3 这三种声音文件,并根据不同的用途,使用不同层
次的编码。MPEG 音频编码的层次越高,编码器越复杂,压缩率也越高,MP1 和 MP2 的压缩率分别为 4:1 和
6:1-8:1,而 MP3 的压缩率则高达 10:1-12:1,也就是说,一分钟 CD 音质的音乐,未经压缩需要 10MB
的存储空间,而经过 MP3 压缩编码后只有 1MB 左右。不过 MP3 对音频信号采用的是有损压缩方式,为了降
低声音失真度,MP3 采取了“感官编码技术”,即编码时先对音频文件进行频谱分析,然后用过滤器滤掉
噪音电平,接着通过量化的方式将剩下的每一位打散排列,最后形成具有较高压缩比的 MP3 文件,并使压
缩后的文件在回放时能够达到比较接近原音源的声音效果。
二、整个MP3文件结构
MP3文件大体分为三部分:TAG_V2(ID3V2),Frame, TAG_V1(ID3V1)
ID3V2
包含了作者,作曲,专辑等信息,长度不固定,扩展了ID3V1的信息量。
Frame
.
.
.
Frame
一系列的帧,个数由文件大小和帧长决定
每个FRAME的长度可能不固定,也可能固定,由位率bitrate决定
每个FRAME又分为帧头和数据实体两部分
帧头记录了mp3的位率,采样率,版本等信息,每个帧之间相互独立
ID3V1
包含了作者,作曲,专辑等信息,长度为128BYTE。
三、MP3帧格式
1. 帧头格式
帧头长4字节,对于固定位率的MP3文件,所有帧的帧头格式一样其数据结构如下:
typedef FrameHeader {
unsigned int sync: 11; //同步信息
unsigned int version: 2; //版本
unsigned int layer: 2; //层
unsigned int error protection: 1; // CRC校验
unsigned int bitrate_index: 4; //位率
unsigned int sampling_frequency: 2; //采样频率
unsigned int padding: 1; //帧长调节
unsigned int private: 1; //保留字
unsigned int mode: 2; //声道模式
unsigned int mode extension: 2; //扩充模式
unsigned int copyright: 1; // 版权
unsigned int original: 1; //原版标志
unsigned int emphasis: 2; //强调模式
}HEADER, *LPHEADER;
帧头4字节使用说明见表1。
MP3帧长取决于位率和频率,计算公式为:
. mpeg1.0 layer1 : 帧长= (48000*bitrate)/sampling_freq + padding
layer2&3: 帧长= (144000*bitrate)/sampling_freq + padding
. mpeg2.0 layer1 : 帧长= (24000*bitrate)/sampling_freq + padding
layer2&3 : 帧长= (72000*bitrate)/sampling_freq + padding
例如:位率为64kbps,采样频率为44.1kHz,padding(帧长调节)为1时,帧长为210字节。
帧头后面是可变长度的附加信息,对于标准的MP3文件来说,其长度是32字节,紧接其后的是压缩的声音数据,当解码器读到此处时就进行解码了。
表1 MP3帧头字节使用说明
名称
位长
说 明
同步信息
11
第1、2字节
所有位均为1,第1字节恒为FF。
版本
2
00-MPEG 2.5 01-未定义 10-MPEG 2 11-MPEG 1
层
2
00-未定义 01-Layer 3 10-Layer 2 11-Layer 1
CRC校验
1
0-校验 1-不校验
位率
4
第3字节
取样率,单位是kbps,例如采用MPEG-1 Layer 3,64kbps是,值为0101。
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)
32(8)
8 (8)
0010
64
48
40
64(48)
48(16)
16 (16)
0011
96
56
48
96(56)
56(24)
24 (24)
0100
128
64
56
128(64)
64(32)
32 (32)
0101
160
80
64
160(80)
80(40)
64 (40)
0110
192
96
80
192(96)
96(48)
80 (48)
0111
224
112
96
224(112)
112(56)
56 (56)
1000
256
128
112
256(128)
128(64)
64 (64)
1001
288
160
128
288(144)
160(80)
128 (80)
1010
320
192
160
320(160)
192(96)
160 (96)
1011
352
224
192
352(176)
224(112)
112 (112)
1100
384
256
224
384(192)
256(128)
128 (128)
1101
416
320
256
416(224)
320(144)
256 (144)
1110
448
384
320
448(256)
384(160)
320 (160)
1111
bad
bad
bad
bad
bad
bad
V1 - MPEG 1 V2 - MPEG 2 and MPEG 2.5
L1 - Layer 1 L2 - Layer 2 L3 - Layer 3
“free” 表示位率可变 “bad” 表示不允许值
采样频率
2
采样频率,对于MPEG-1: 00-44.1kHz 01-48kHz 10-32kHz 11-未定义
对于MPEG-2: 00-22.05kHz 01-24kHz 10-16kHz 11-未定义
对于MPEG-2.5: 00-11.025kHz 01-12kHz 10-8kHz 11-未定义
帧长调节
1
用来调整文件头长度,0-无需调整,1-调整,具体调整计算方法见下文。
保留字
1
没有使用。
声道模式
2
第4字节
表示声道, 00-立体声Stereo 01-Joint Stereo 10-双声道 11-单声道
扩充模式
2
当声道模式为01是才使用。
Value
强度立体声
MS立体声
00
off
off
01
on
off
10
off
on
11
on
on
版权
1
文件是否合法,0-不合法 1-合法
原版标志
1
是否原版, 0-非原版 1-原版
强调方式
2
用于声音经降噪压缩后再补偿的分类,很少用到,今后也可能不会用。
00-未定义 01-50/15ms 10-保留 11-CCITT J.17
另可参考下文:
This system was created to minimize file lengths and to preserve sound quality.
Higher frequencies generally needs more space for encoding (thats why many codecs cut all
frequencies above cca 16kHz) and lower tones requires less. So if some part of song doesnt consistof higher tones then using eg. 192kbps is wasting of space. It should be enough to use only eg.96kbps.
And it is the principle of VBR. Codec looks over frame and then choose bitrate suitable for itssound quality.
It sounds perfect but it brings some problems:
If you want to jump over 2 minutes in song, it is not a problem with CBR because you are able
simply count amount of Bytes which is necessary to skip. But it is impossible with VBR. Frame
lengths should be arbitrary so you have to either go frame by frame and counts (time consuming
and very unpractical) or use another mechanism for approximate count.
If you want to cut 5 minutes from the middle of VBR file (all we know CDs where last song takes
10 minutes but 5 minutes is a pure silence, HELL!) problems are the same.
Result? VBR files are more difficult for controlling and adjusting. And I dont like feeling that
sound quality changes in every moment. And AFAIK many codecs have problems with creation VBR ingood quality.
Personally I cant see any reason why to use VBR - I dont give a fuck if size of one CD in MP3is 55 MB with CBR or 51 MB with VBR. But everybody has a different taste… some people preferVBR.
VBR File Structureis the same as for CBR. But the first frame doesnt contain audio data and it is used for special
information about VBR file.
Structure of the first frame:
Byte Content
0-3 Standard audio frame header (as descripted above). Mostly it contains values FF
FB 30 4C, from which you can count FrameLen = 156 Bytes. And thats exactly enough
space for storing VBR info.
This header contains some important information valid for the whole file:
- MPEG (MPEG1 or MPEG2)
- SAMPLING rate frequency index
- CHANNEL (JointStereo etc.)
4-x Not used till string “Xing” (58 69 6E 67). This string is used as a main VBR file
identifier. If it is not found, file is supposed to be CBR. This string can be placed
at different locations according to values of MPEG and CHANNEL (ya, these from a
few lines upwards):
36-39 “Xing” for MPEG1 and CHANNEL != mono (mostly used)
21-24 “Xing” for MPEG1 and CHANNEL == mono
21-24 “Xing” for MPEG2 and CHANNEL != mono
13-16 “Xing” for MPEG2 and CHANNEL == mono
After “Xing” string there are placed flags, number of frames in file and a size
of file in Bytes. Each of these items has 4 Bytes and it is stored as ‘int’ number
in memory. The first is the most significant Byte and the last is the least.
Following schema is for MPEG1 and CHANNEL != mono:
40-43 Flags
Value Name Description
00 00 00 01 Frames Flag set if value for number of frames in file is stored
00 00 00 02 Bytes Flag set if value for filesize in Bytes is stored
00 00 00 04 TOC Flag set if values for TOC (see below) are stored
00 00 00 08 VBR Scale Flag set if values for VBR scale are stored
All these values can be stored simultaneously.
44-47 Frames
Number of frames in file (including the first info one)
48-51 Bytes
File length in Bytes
52-151 TOC (Table of Contents)
Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately
solves problem with moving inside file.
Each Byte has a value according this formula:
(TOC[i] / 256) * fileLenInBytes
So if song lasts eg. 240 sec. and you want to jump to 60. sec. (and file is 5 000
000 Bytes length) you can use:
TOC[(60/240)*100] = TOC[25]
and corresponding Byte in file is then approximately at:
(TOC[25]/256) * 5000000
If you want to trim VBR file you should also reconstruct Frames, Bytes and TOC
properly.
152-155 VBR Scale
I dont know exactly system of storing of this values but this item probably doesnt
have deeper meaning.
四、ID3标准
MP3帧头中除了存储一些象private、copyright、original的简单音乐说明信息以外,没有考虑存放歌名、作者、专辑名、年份等复杂信息,而这些信息在MP3应用中非常必要。1996年,FricKemp在“Studio 3”项目中提出了在MP3文件尾增加一块用于存放歌曲的说明信息,形成了ID3标准,至今已制定出ID3 V1.0,V1.1,V2.0,V2.3和V2.4标准。版本越高,记录的相关信息就越丰富详尽。
1. ID3V1
ID3 V1.0标准并不周全,存放的信息少,无法存放歌词,无法录入专辑封面、图片等。V2.0是一个相当完备的标准,但给编写软件带来困难,虽然赞成此格式的人很多,在软件中真正实现的却极少。绝大多数MP3仍使用ID3 V1.0标准。此标准是将MP3文件尾的最后128个字节用来存放ID3信息,这128个字节使用说明见表3。
表3 ID3 V1.0文件尾说明
字节
长度 (字节)
说 明
1-3
3
存放“TAG”字符,表示ID3 V1.0标准,紧接其后的是歌曲信息。
4-33
30
歌名
34-63
30
作者
64-93
30
专辑名
94-97
4
年份
98-127
30
附注
128
1
MP3音乐类别,共147种。
表4 MP3音乐类别:
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’
80
Folk
81
Folk/Rock
82
National Folk
83
Swing
84
Fast-Fusion
85
Bebob
86
Latin
87
Revival
88
Celtic
89
Bluegrass
90
Advantgarde
91
Gothic Rock
92
Progressive Rock
93
Psychadelic Rock
94
Symphonic Rock
95
Slow Rock
96
Big Band
97
Chorus
98
Easy Listening
99
Acoustic
100
Humour
101
Speech
102
Chanson
103
Opera
104
Chamber Music
105
Sonata
106
Symphony
107
Booty Bass
108
Primus
109
Porn Groove
110
Satire
111
Slow Jam
112
Club
113
Tango
114
Samba
115
Folklore
Any other value should be considered as ‘Unknown’
五、MP3文件实例剖析
在VC++中打开一个名为test.mp3文件,其内容如下:
000000 FF FB 52 8C 00 00 01 49 09 C5 05 24 60 00 2A C1
000010 19 40 A6 00 00 05 96 41 34 18 20 80 08 26 48 29
000020 83 04 00 01 61 41 40 50 10 04 00 C1 21 41 50 64
……
0000D0 FE FF FB 52 8C 11 80 01 EE 90 65 6E 08 20 02 30
0000E0 32 0C CD C0 04 00 46 16 41 89 B8 01 00 08 36 48
0000F033 B7 00 00 01 02 FF FF FF F4 E1 2F FF FF FF FF
……
0001A0 DF FF FF FB 52 8C 12 00 01 FE 90 58 6E 09 A0 02
0001B0 33 B0 CA 85 E1 50 01 45 F6 19 61 BC 26 80 28 7C
0001C0 05 AC B4 20 28 94 FF FF FF FF FF FF FF FF FF FF
……
001390 7F FF FF FF FD 4E 00 54 41 47 54 45 53 54 00 00
0013A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
……
0013F000 00 00 00 04 19 14 03 00 00 00 00 00 00 00 00
001400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
001410 00 00 00 00 00 00 4E
该文件长度1416H(5.142K),帧头为:FF FB 52 8C,转换成二进制为:
11111111 11111011
01010010 10001100
对照表1可知,test.mp3帧头信息见表5。
表5 test.mp3文件帧头信息
名称
位值
说 明
同步信息
11111111111
第1字节恒为FF,11位均为1。
版本
11
MPEG 1
层
01
Layer 3
CRC校验
1
不校验
位率
0101
64kbps
频率
00
44.1kHz
帧长调节
1
调整,帧长是210字节。
保留字
0
没有使用。
声道模式
10
双声道
扩充模式
00
未使用。
版权
1
合法
原版标志
1
原版
强调方式
00
未定义
第1397H开始的三个字节是54 41 47,存放的是字符“TAG”,表示此文件有ID3 V1.0信息。
139AH开始的30个字节存放歌名,前4个非00字节是54 45 53 54,表示“TEST”;
13F4H开始的4个字节是04 19 14 03,存放年份“04/25/2003”;
最后1个字节是4E,表示音乐类别,代号为78,即“Rock&Roll”;
其它字节均为00,未存储信息。
六、资料
www.id3.org