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文件大体分为三部分:TAG_V2(ID3V2),Frame, TAG_V1(ID3V1)
ID3V2
包含了作者,作曲,专辑等信息,长度不固定,扩展了ID3V1的信息量。
Frame
.
.
.
Frame
一系列的帧,个数由文件大小和帧长决定
每个FRAME的长度可能不固定,也可能固定,由位率bitrate决定
每个FRAME又分为帧头和数据实体两部分
帧头记录了mp3的位率,采样率,版本等信息,每个帧之间相互独立
ID3V1
包含了作者,作曲,专辑等信息,长度为128BYTE。
帧头长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
|
MAIN_DATA 部分长度是否变化决定于 FRAMEHEADER 的 bitrate 是否变化,一首 MP3 歌曲,它有三个版
本:96Kbps(96 千比特位每秒)、128Kbps 和 192Kbps。Kbps(比特位速率),表明了音乐每秒的数据量,
Kbps 值越高,音质越好,文件也越大,MP3 标准规定,不变的 bitrate 的 MP3 文件称作 CBR,大多数 MP3
文件都是 CBR 的,而变化的 bitrate 的 MP3 文件称作 VBR,每个 FRAME 的长度都可能是变化的。下面是 CBR
和 VBR 的不同点:
1)CBR:固定位率的 FRAME 的大小是固定的(公式如上所述),只要知道文件总长度,和帧长即可由播放每帧需 26ms 计算得出 mp3 播放的总时间,也可通过计数帧的个数控制快进、快退慢放等操作。注:有些时候,并不是所有的帧都是等长的,有的帧可能多一个或几个字节。
2)VBR:VBR 是 XING 公司推出的算法,所以在 MP3 的 FRAME 里会有“XING"这个关键字(现在很多流行的
小软件也可以进行 VBR 压缩,它们是否遵守这个约定,那就不得而知了),它存放在 MP3 文件中的第一个有效 FRAME 里,它标识了这个 MP3 文件是 VBR 的。同时第一个 FRAME 里存放了 MP3 文件的 FRAME 的总个数,这就很容易获得了播放总时间,同时还有 100 个字节存放了播放总时间的 100 个时间分段的 FRAME 的 INDEX,假设 4 分钟的 MP3 歌曲,240S,分成 100 段,每两个相邻 INDEX 的时间差就是 2.4S,所以通过这个 INDEX,只要前后处理少数的 FRAME,就能快速找出我们需要快进的 FRAME 头。
表2 VBR文件第一帧结构
|
字节
|
说 明
|
|
1-4
|
与CBR相同的标准声音帧头
|
|
5-40
|
存放VBR文件标识“Xing”(58 69 6E 67),此标识具体位置视采用的MPEG标准和声道模式而定。标识的前后字节没有使用。
|
|
36-39
|
MPEG-1和非单声道(常见)
|
|
21-24
|
MPEG-1和单声道
|
|
21-24
|
MPEG-2和非单声道
|
|
13-16
|
MPEG-2和单声道
|
|
41-44
|
标志,说明是否存储了帧数、文件长度、目录表和VBR规模信息,如果存储了,则01 02 04 08。
|
|
45-48
|
帧数(包括第一帧)
|
|
49-52
|
文件长度
|
|
53-152
|
目录表,用来按时间进行字节定位。
|
|
153-156
|
VBR规模,用于位率变动
|
另可参考下文:
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.
MP3帧头中除了存储一些象private、copyright、original的简单音乐说明信息以外,没有考虑存放歌名、作者、专辑名、年份等复杂信息,而这些信息在MP3应用中非常必要。1996年,FricKemp在“Studio 3”项目中提出了在MP3文件尾增加一块用于存放歌曲的说明信息,形成了ID3标准,至今已制定出ID3 V1.0,V1.1,V2.0,V2.3和V2.4标准。版本越高,记录的相关信息就越丰富详尽。
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
|
作者
|
|