您的位置:首页 > 其它

MP3文件格式与编码原理解码流程详解

2014-10-12 17:32 513 查看
1 文件格式

MP3文件格式四部分,按顺序排列如下:

ID3V2

包含了作者,作曲,专辑等信息,长度不固定,扩展了ID3V1的信息量

Frame

音频帧序列

APEV2

包含了作者,作曲,专辑等信息,长度不固定,扩展了ID3V1的信息量

ID3V1

包含了作者,作曲,专辑等信息,长度为128BYTE

1.1 ID3 V1

ID3 V1内容按顺序排列如下:

名称

位置(byte)

标签标志'TAG'

0~2

标题

3~32

艺术家

33~62

专辑

63~92

年代

93~96

注释

97~126

流派

127

//note:因ID3 V1未规定填充内容的格式,在某些情况下会引起问题。如台湾常常采用big5编码,如果当成ASCII码处理就会出错。

1.2 ID3 V2

Tag Header

10 bytes

Tag Frame 序列

长度不定

1.2.1 Tag Header格式

标识

3 bytes

必须为“ID3”

Version

1 byte

ID3V2.3 就记录3

Revision

1 byte

记录为0

Flag

1 byte

标志是否存在extended Header等

Size

4 bytes

标签大小,包括标签头的10 个字节

1.2.2 Tag Frame格式





Frame ID对照表如下:

AENC Audio encryption

APIC Attached picture

COMM Comments

COMR Commercial frame

ENCR Encryption method registration

EQUA Equalization

ETCO Event timing codes

GEOB General encapsulated object

GRID Group identification registration

IPLS Involved people list

LINK Linked information

MCDI Music CD identifier

MLLT MPEG location lookup table

OWNE Ownership frame

PRIV Private frame

PCNT Play counter

POPM Popularimeter

POSS Position synchronisation frame

RBUF Recommended buffer size

RVAD Relative volume adjustment

RVRB Reverb

SYLT Synchronized lyric/text

SYTC Synchronized tempo codes

TALB Album/Movie/Show title

TBPM BPM (beats per minute)

TCOM Composer

TCON Content type

TCOP Copyright message

TDAT Date

TDLY Playlist delay

TENC Encoded by

TEXT Lyricist/Text writer

TFLT File type

TIME Time

TIT1 Content group description

TIT2 Title/songname/content description

TIT3 Subtitle/Description refinement

TKEY Initial key

TLAN Language(s)

TLEN Length

TMED Media type

TOAL Original album/movie/show title

TOFN Original filename

TOLY Original lyricist(s)/text writer(s)

TOPE Original artist(s)/performer(s)

TORY Original release year

TOWN File owner/licensee

TPE1 Lead performer(s)/Soloist(s)

TPE2 Band/orchestra/accompaniment

TPE3 Conductor/performer refinement

TPE4 Interpreted, remixed, or otherwise modified by

TPOS Part of a set

TPUB Publisher

TRCK Track number/Position in set

TRDA Recording dates

TRSN Internet radio station name

TRSO Internet radio station owner

TSIZ Size

TSRC ISRC (international standard recording code)

TSSE Software/Hardware and settings used for encoding

TYER Year

TXXX User defined text information frame

UFID Unique file identifier

USER Terms of use

USLT Unsychronized lyric/text transcription

WCOM Commercial information

WCOP Copyright/Legal information

WOAF Official audio file webpage

WOAR Official artist/performer webpage

WOAS Official audio source webpage

WORS Official internet radio station homepage

WPAY Payment

WPUB Publishers official webpage

WXXX User defined URL link frame

1.3 APE V2

APE Tags Header

32 bytes

APE Tag Item 1

10 bytes

APE Tag Item 2

10 bytes



10 bytes

APE Tag Item n-1

10 bytes

APE Tag Item n

10 bytes

APE Tags Footer

32 bytes

APE tag items should be sorted ascending by size. When streaming, parts of the APE tags can be dropped to reduce danger of drop outs between titles. This is not a must , but strongly recommended. Actually the items should be sorted by importance/byte,
but this is not feasible. Only break this rule if you add less important small items and you don't want to rewrite the whole tag. An APE tag at the end of a file (strongly recommended) must have at least a footer, an APE tag in the beginning of a file (strongly
unrecommended) must have at least a header. When located at the end of an MP3 file, an APE tag should be placed after the the
last frame, just before the ID3v1 tag (if any).

1.4 Frame

Frame Header

4 bytes

Side Information

单声道17bytes,双声道32bytes

Frame Data

不定长

1.4.1 Frame Header

AAAAAAAA AAABBCCD EEEEFFGH IIJJKLMM





























1.4.2 Side information





main_data_begin: 表示一帧数据main_data开始的位置。表示main data相对于该帧同步头的负偏移。这里涉及到bit reservior技术:即把当前帧未使用完的bit数,留给后面需要的帧使用,这就导致每一帧的main data开始的位置,可能在它的Header和side information之前。

Private_bits:留作私用。

Scfsi:表明两个granule是否使用相同的缩放因子。

scalefac_compress:被编码的缩放因子所占的比特数。

Part_2_3_length:表示main data中scalefactor和Huffman数据所占的比特数。

Global_gain:全局量化步长。

window_switching_flag、 block_bype和mixed_block_type:当window_switching_flag未置位时,block_type为0;当其置位时, block_type由mixed_block_type[gr][ch]指定,如下图所示:





当block_type为0、1、3时,为长块;为2时,为短块。mixed_block_flag[gr][ch]为1时,为混合块。

Table_select、Big_value、count1_table_select、Region0_count和region1_count:参考Huffman码表选择。

subblock_gain:短窗量化时所用的增益偏移量。

preflag和scalefac_scale:反量化时用到的变量。

2 编码原理及流程





以单声道而言,MP3一帧包含1152个声音取样,一帧分为2节(granule)。mp3编码时,首先将原始的PCM数据送入滤波器组,分成32个等频宽的子频带,然后再通过MDCT(modified discrete coding transform),将每个子带,转换成18个次频带。然后根据第二声学模型提供的SMR(signal-to-mask ratio),对每一个子频带信号,做位元分配和编码。

Step 1:prediction

根据第二声学模型做预测,预测结果得到的SMR会作为位元分配的依据。第二声学模型中几个重要依据如下:

静音阈值曲线、时域掩蔽效应、频域掩蔽效应、临界频带。

Step 2:子带分离(Analysis Subband Filter)

首先将原始的PCM数据送入滤波器组,分成32个等频宽的子频带。



Step
3:MDCT

MDCT滤波器将32个子带中每一个子带的的信号在频域上进一步划分,长块进行18点(18个频域采样点)的MDCT变换,短块进行6点(6个频域采样点)的MDCT变换,以窗为单位分3次进行。MDCT包括三部分:MDCT窗框、MDCT、长窗假象处理。

(a)MDCT加窗

4种窗及使用情形如下:







窗框的选择依据第二声学模型,规则通常如下:子频带音频讯号稳定时,采用长窗来提供最细的频谱解析度;子频带变动较大时,采用短窗提供较大的时域解析度。决定好窗框以后,就可以以窗框为单位进行MDCT运算。如果是长窗,需要针对混跌做假象处理。

(b)MDCT

作用:将时域信号转换成频域信号

In the case of long blocks ( block_type 0,1,3 ) there are 36 coefficents in the time domain and 18 in the frequency domain.

In the case of short blocks (block_type 2 ) there are 3 transformations with short length. This leads to 12 coefficents in the time and 6 in the frequency domain.

(c)长窗假象处理(混叠消除)

什么是长窗假象:使用长窗时,频谱上可见邻近的子频带间有明显的重叠现象,处于重叠区间的讯号将会同时影响两个子频带。

假象处理的方式:将处在相对应位置的频线之能量做一定比例的增减,蝶形运算。

附:Step2和Step3的综合图为:





Step 4: Joint Stereo(联合立体声)

编码依据:2个声道存在相干性,方法有:

Intensity Stereo(IS):Human hearing is predominantly less acute at perceiving the direction of certain audio frequencies

Mid/Side (M/S) stereo :The mid channel is L + R. The side channel is L − R

Step 5:位元分配(bit allocation)

根据第二声学模型的预测结果,进行位元分配;位元分配是一个反复调整的过程。

Step 6:量化

以缩放因子频带为单位,进行量化。缩放因子频带内使用相同的缩放因子。

mp3中一帧数据含有1152个PCM数据,分成2节(granual)。每节含有576个PCM数据,这576个值在不同的节类型有不同的定义。如下所述:

(a)该节为长块:这576个值代表576条频率线上的值,是时域上的576个pcm值经过时频变换的结果。这576条频率线从低到高分成32个子带,每个子带含18条频率线。同时,也将这576个数据分成若干个缩放因子带,每个缩放因子带共用一个缩放因子。长块的缩放因子带在44kHz按如下表格划分,其中,418-575不属于任何一个缩放因子带,使用系统提供的默认缩放因子:





(b)该节为短块:这576个值代表192条频率线的值,192条频率线分32个子带,每条子带包括6条频率线。每条频率线有3个值,分别属于3个窗 (windows_0,windows_1,windows_2)。192条频率线也被分成若干缩放因子带,在44.1kHz时划分如下图,其中,136-191使用默认缩放因子。







这576个值得排列顺序为,先是按缩放因子带从低到高排列;缩放因子带内,按windows_0,windows_2,windows_3排列;每一个window中,频率线从低到高排列。

(c)该节为混合块: 解出来的值分2个部分,第一部分(前36个值)是长块,代表36条频率线;第二部分(后540个值)为短块,代表180个频率线。2部分的排列方式分别于长块和短块相同。

综上,有:





附: 缩放因子

缩放因子带在逆量化时共用的缩放因子,缩放因子被编码于main_data中,欲解码缩放因子,首先得知道缩放因子所占的比特数,在side information的scale_compress[gr][ch]提供这样的信息,所用的bit数通过查如下表才能得到。slen 1和slen 2针对那些缩放因子带,由块类型决定。



Step 7:Huffman编码

(a)Huffman码表选择

当从一个缩放因子频带过渡到另一个缩放因子频带时,Huffman码表可能发生改变;需要进行编码的576个值分为大值区、小值区、零值区:





不同的区域使用不同的Huffman表编码,大值区每2个值一起编码,小值区每4个值一起编码,零值区无需编码,大值区以缩放因子频带为单位,分为3个region。每个region使用不同的Huffman表。一共有32个Huffman表供选择。

(b)huffman编码(略)

注:大值区的Huffman表有一个参数linbits,用来指定Huffman表是否能用来编码大于15的数。

Step 8:生成帧

加上Frame Header和Side Information,生成帧。

3 解码流程





preprocessing:主要完成Header和side information的解码。

Huffman decoding:选择Huffman table,进行解码。

Requantization:逆量化,短块和长块使用不同的公式。公式如下:





Reodering:由于编码时对短块和混合块中的短块进行了重排,具体见编码部分,故解码时需要重新排序

Stero decoding:立体声解码。

Alias reduction:长块间需要消除混跌。

IMDCT:每做一次MDCT产生36个输出。昨晚IMDCT后,需再做加窗运算。IMDCT做完后,再无长块、短块概念,得到的结果是从低到高的32个子带,每个子带18个值。

子带合成滤波:先是把32个子带中,每个子带取一个数据,组成的32个值送入一个1024的FIFO中;接着把这1024个值中取出一半,对其做加窗运算,加窗系数由MP3官方协议的表格提供;最后对加窗结果进行叠加得到32个时域PCM输出。





4 附录

ID3 V2官网:http://www.id3.org/id3v2.3.0

APE V2官网:http://wiki.hydrogenaudio.org/index.php?title=APEv2_specification

ISO/IEC 11172-3、ISO/IEC 13818-3:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: