mp3檔案格式(frame header )
MPEG Audio Layer I/II/III frame header
An MPEG audio file is built up from smaller parts called frames.MPEG audio檔案是由稱為frames的小部分組成的。 Generally, frames are independent items通常frames是由items決定的. Each frame has its own header and audio informations每一個frame有自己的頭和audio資訊. As there is no file header, you can cut any part of MPEG file and play it correctly (this should be done on frame boundaries but most applications will handle incorrect headers)如果沒有檔案頭,你可以刪減掉任何一個部分的MPEG檔案來正常播放(這個…..). However, for Layer III, this is not 100% correct但是在Layer III,就不是完全這樣了. Due to internal data organization in MPEG Layer III files, frames are often dependent of each other and they cannot be cut off just like that由於檔案內部組織,frames之間是相互關聯的,所以不能象前面一樣剪裁出單獨的部分播放.
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 當你想要讀MPEG檔案,通常先讀第一個頭,然後假設其他頭也是一樣的. But this may not be always the case但是情況並不總是如此. Variable bitrate MPEG files may use so called bitrate switching, which means that bitrate changes according to the content of each frame可變位元速率 MPEG檔案使用位元速率改變――位元速率隨著每一frame內容改變. This way lower bitrates may be used in frames where it will not reduce sound quality這樣就可以減小檔案尺寸,但是聲音的品質卻不會降低. This allows making better compression while keeping high quality of sound. 這樣就可以在更大的壓縮率下保持高音質。
The frame header is constituted by the very first four bytes (32bits) in a frame在frame開頭有4個位元組. The first eleven bits (or first twelve bits, see below about frame sync) of a frame header are always set and they are called "frame sync"開始11個位元位(或者是12位,見後面同步幀)被稱為同步幀. Therefore, you can search through the file for the first occurence of frame sync (meaning that you have to find a byte with a value of 255, and followed by a byte with its three (or four) most significant bits set)這樣你就可以搜尋檔案定位同步幀(). Then you read the whole header and check if the values are correct然後你就讀入整個header然後檢測是否有效. You will see in the following table the exact meaning of each bit in the header在後面的表你就可以看見每一位的意思. Each value that is specified as reserved, invalid, bad, or not allowed should indicate an invalid header. 每一個值分別表示保留、無效、壞、禁止
Frames may have a CRC check幀可能有CRC校正. The CRC is 16 bits long and, if it exists, it follows the frame header如果由,其長度為16bie,緊跟frame後. After the CRC comes the audio data.音頻檔案就在crc後面。 You may calculate the CRC of the frame你應該校正幀的CRC, and compare it with the one you read from the file然後與你讀的檔案校正. This is actually a very good method to check the MPEG frame validity. 這是檢查MGEG幀的好辦法
Here is a presentation of the header content一下是header的內容. Characters from A to M are used to indicate different fields字母A~M表示不同的Field. In the table below, you can see 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 ID
00 - MPEG Version 2.5 (unofficial)
01 - reserved
10 - MPEG Version 2 (ISO/IEC 13818-3)
11 - MPEG Version 1 (ISO/IEC 11172-3)
Note: MPEG Version 2.5 is not official standard. It is an extension of the standard used for very low bitrate files. If your decoder does not support this extension, it is recommended for you to use 12 bits for synchronization instead of 11 bits.
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 & L3
0000 free free free free free
0001 32 32 32 32 8
0010 64 48 40 48 16
0011 96 56 48 56 24
0100 128 64 56 64 32
0101 160 80 64 80 40
0110 192 96 80 96 48
0111 224 112 96 112 56
1000 256 128 112 128 64
1001 288 160 128 144 80
1010 320 192 160 160 96
1011 352 224 192 176 112
1100 384 256 224 192 128
1101 416 320 256 224 144
1110 448 384 320 256 160
1111 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 free format. If the correct fixed bitrate (such files cannot use variable bitrate) is different than those presented in upper table it must be determined by the application. This may be implemented only for internal purposes since third party applications have no means to findout correct bitrate. Howewer, this is not impossible to do but demands lots of efforts.
"bad" means that this is not an allowed value
MPEG files may have variable bitrate (VBR). Each frame may be created with different bitrate. It may be used in all layers. Layer III decoders must support this method. Layer I & II decoders may support it.
For Layer II there are some combinations of bitrate and mode which are not allowed. Here is a list of allowed combinations.
bitrate single channel stereo intensity stereo dual channel
free yes yes yes yes
32 yes no no no
48 yes no no no
56 yes no no no
64 yes yes yes yes
80 yes no no no
96 yes yes yes yes
112 yes yes yes yes
128 yes yes yes yes
160 yes yes yes yes
192 yes yes yes yes
224 no yes yes yes
256 no yes yes yes
320 no yes yes yes
384 no yes yes yes
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 slot
Padding is used to fit the bit rates exactly. For an example: 128k 44.1kHz layer II uses a lot of 418 bytes and some of 417 bytes long frames to get the exact 128k bitrate. For Layer I slot is 32 bits long, for Layer II and Layer III slot is 8 bits long.
H 1 (8) Private bit. It may be freely used for specific needs of an application.
I 2 (7,6) Channel Mode
00 - Stereo
01 - Joint stereo (Stereo)
10 - Dual channel (2 mono channels)
11 - Single channel (Mono)
Note: Dual channel files are made of two independant mono channel. Each one uses exactly half the bitrate of the file. Most decoders output them as stereo, but it might not always be the case.
????One example of use would be some speech in two different languages carried in the same bitstream, and and appropriate decoder would decode only the choosen language
另外一篇文章的描述
Mpeg 1.0/2.0 LayersI, II and III header and trailer formats
-----------------------------------------------------------
Laurent.Clevy@alcatel.fr
* HEADER
bits name comments
--------------------------------------------------
12 sync 0xFFF
1 version 1=mpeg1.0, 0=mpeg2.0
2 lay 4-lay = layerI, II or III
1 error protection 0=yes, 1=no
4 bitrate_index see table below
2 sampling_freq see table below
1 padding
1 extension see table below
2 mode see table below
2 mode_ext used with "joint stereo" mode
1 copyright 0=no 1=yes
1 original 0=no 1=yes
2 emphasis see table below
--------------------------------------------------
- bitrate_index
. mpeg1.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14
layer1 32 64 96 128 160 192 224 256 288 320 352 384 416 448
layer2 32 48 56 64 80 96 112 128 160 192 224 256 320 384
layer3 32 40 48 56 64 80 96 112 128 160 192 224 256 320
. mpeg2.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14
layer1 32 48 56 64 80 96 112 128 144 160 176 192 224 256
layer2 8 16 24 32 40 48 56 64 80 96 112 128 144 160
layer3 8 16 24 32 40 48 56 64 80 96 112 128 144 160
- sampling_freq
. mpeg1.0
0 1 2
44100 48000 32000
. mpeg2.0
0 1 2
22050 24000 16000
- mode:
0 "stereo"
1 "joint stereo"
2 "dual channel"
3 "single channel"
- mode extension:
0 MPG_MD_LR_LR
1 MPG_MD_LR_I
2 MPG_MD_MS_LR
3 MPG_MD_MS_I
jsbound :
mode_ext 0 1 2 3
layer
1 4 8 12 16
2 4 8 12 16
3 0 4 8 16
- emphasis:
0 "none"
1 "50/15 microsecs"
2 "reserved" must not be used !
3 "CCITT J 17"
* TRAILER
at end of file - 128 bytes
offset type len name
--------------------------------------------
0 char 3 "TAG"
3 char 30 title
33 char 30 artist
63 char 30 album
93 char 4 year
97 char 30 comments
127 byte 1 genre
--------------------------------------------
- genre :
0 "Blues"
1 "Classic Rock"
2 "Country"
3 "Dance"
4 "Disco"
5 "Funk"
6 "Grunge"
7 "Hip-Hop"
8 "Jazz"
9 "Metal"
10 "New Age"
11 "Oldies"
12 "Other"
13 "Pop"
14 "R&B"
15 "Rap"
16 "Reggae"
17 "Rock"
18 "Techno"
19 "Industrial"
20 "Alternative"
21 "Ska"
22 "Death Metal"
23 "Pranks"
24 "Soundtrack"
25 "Euro-Techno"
26 "Ambient"
27 "Trip-Hop"
28 "Vocal"
29 "Jazz+Funk"
30 "Fusion"
31 "Trance"
32 "Classical"
33 "Instrumental"
34 "Acid"
35 "House"
36 "Game"
37 "Sound Clip"
38 "Gospel"
39 "Noise"
40 "AlternRock"
41 "Bass"
42 "Soul"
43 "Punk"
44 "Space"
45 "Meditative"
46 "Instrumental Pop"
47 "Instrumental Rock"
48 "Ethnic"
49 "Gothic"
50 "Darkwave"
51 "Techno-Industrial"
52 "Electronic"
53 "Pop-Folk"
54 "Eurodance"
55 "Dream"
56 "Southern Rock"
57 "Comedy"
58 "Cult"
59 "Gangsta"
60 "Top 40"
61 "Christian Rap"
62 "Pop/Funk"
63 "Jungle"
64 "Native American"
65 "Cabaret"
66 "New Wave"
67 "Psychadelic"
68 "Rave"
69 "Showtunes"
70 "Trailer"
71 "Lo-Fi"
72 "Tribal"
73 "Acid Punk"
74 "Acid Jazz"
75 "Polka"
76 "Retro"
77 "Musical"
78 "Rock & Roll"
79 "Hard Rock"
80 "Unknown"
- frame length :
. 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