分享

MPEG音频帧头

 xrzs 2009-09-09

MPEG音频帧头

MPEG Audio Frame Header

更新日期:2008年09月30日
Konrad Windszus
开发工具:VC7.1, C++, Windows, MFC, VS, VS.NET2003, Dev
摘要:这个文章介绍MPEG音频帧头的格式。
所含源码列表:
Download demo project (binary and source, V2.2) - 225.4 KB
Download source (V2.2) - 21.9 KB

内容

  1. Introduction
  2. MPEG Audio Frame
    1. MPEG Audio Frame Header
    2. CRC Checksum
    3. VBR Headers
      1. XING Header
      2. VBRI Header
  3. Additional Tags
  4. Using The Code
  5. Links And Miscellaneous
  6. History

 

1 。简介

这个文章是关于结构的MPEG音频帧头包括XING和VBRI头。其目的是估计的期限MPEG音频文章件的准确和尽快。本文章不包括任何提示就如何解码/编码实际的音频数据。 MPEG音频文章件存在于不同层次。最常见的是MPEG 1层III (也称为MP3 ) ,因为它最复杂的压缩技术。

我知道有其他文章的MPEG音频帧头(甚至CodeProject ) ,但我将进入一个更深一点。

2 。 MPEG音频帧

一个MPEG音频文件组成的框架。每个帧包含一个头开始在其遵循的音频数据。这音频数据总是包含一个fixed number of samples 。当前存在三个层次的MPEG音频,其中的方式有所不同的音频数据进行编码的框架内,但他们都有同样的头格式。该框架本身由插槽。在层一,一个插槽始终是第4字节长,在所有其他层次的插槽是1字节长。

附加层也有三个版本的MPEG音频,不同的采样率,他们可以处理(见table 2.1.2 ) 。 MPEG 1 ( ISO/IEC 13818-3 )和MPEG 2 ( ISO/IEC 11172-3 )是ISO标准。 MPEG 2.5是一个非官方扩展MPEG 2支持甚至更低的采样率。 MPEG 2/2.5也被称为下LSF的缩写,它的采样频率下。每个版本可以处理三个层次。如果你想知道更多有关技术细节的MPEG音频文件,请看看规格。你可以找到他们和其他许多有用信息MPEG在www.MP3-Tech.org

文件可与编码要么常数码率( CBR )或可变量比特率( VBR ) ,这意味着每帧可以有不同的比特率。因此,质量是这些文件往往高于文件编码常数a码率模式,因为他们可以使用较高的比特率的音乐需要它。

2.1 。 MPEG音频帧头

头开始时每帧32位是长期和具有以下格式。 0位头中是最重要的位( MSB )完整的头。请注意,位置是零基;位置,长度和实例都在位格式。

2.1.1 MPEG Audio Frame Header
Position Length Meaning Example
0 11 Frame sync to find the header (all bits are always set) 1111 1111 111
11 2 Audio version ID (see table 3.2 also)

00 - MPEG Version 2.5 (unofficial extension of MPEG 2)
01 - reserved
10 - MPEG Version 2 (ISO/IEC 13818-3)
11 - MPEG Version 1 (ISO/IEC 11172-3)

11
13 2 Layer index

00 - reserved
01 - Layer III
10 - Layer II
11 - Layer I

01
15 1 Protection bit

0 - protected by 16 bit CRC following header
1 - no CRC

1
16 4 Bitrate index (see table 2.1.3) 1001
20 2 Sampling rate index (see table 2.1.2) 11
22 1 Padding bit

If it is set, data is padded with with one slot (important for frame size calculation)

0
23 1 Private bit (only informative) 1
24 2 Channel mode

00 - Stereo
01 - Joint Stereo (Stereo)
10 - Dual channel (Two mono channels)
11 - Single channel (Mono)

Note: Dual channel files are made of two independent mono channels. Each one uses exactly half the bitrate of the file. Most decoders output them as stereo, but it might not always be the case.

01
26 2

Mode extension (Only used in Joint Stereo)

(see table 2.1.6)

00
28 1 Copyright bit (only informative) 1
29 1 Original bit (only informative) 1
30 2 Emphasis

00 - none
01 - 50/15 ms
10 - reserved
11 - CCIT J.17

The emphasis indication is here to tell the decoder that the file must be de-emphasized, that means the decoder must 're-equalize' the sound after a Dolby-like noise suppression. It is rarely used.

00

在指定的采样率有多少样品每秒记录。每个MPEG版本可以处理不同的采样率。

2.1.2 MPEG Versions and Sampling Rates
Sampling Rate Index MPEG 1 MPEG 2 (LSF) MPEG 2.5 (LSF)
00 44100 Hz 22050 Hz 11025 Hz
01 48000 Hz 24000 Hz 12000 Hz
10 32000 Hz 16000 Hz 8000 Hz
11 reserved

该比特率总是显示在每秒。请注意,前缀kilo(简称小'k')并不意味着1024个,但千比特每秒!该比特率索引1111则保留,并应永远不再使用。在MPEG音频标准有一个免费的格式描述。这个免费的格式是指该文件编码恒定比特率,这不是一个预先确定的比特率。只有极少数的解码器能够处理这些文件。

2.1.3 Bitrates (in kilobits per second)
Bitrate Index MPEG 1 MPEG 2, 2.5 (LSF)
Layer I Layer II Layer III Layer I Layer II & III
0000 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 reserved

在MPEG 1层II ,只有一些组合的比特率和模式允许的。在MPEG 2/2.5 ,就没有这种限制。

2.1.4 Allowed bitrate and mode combinations
Bitrate Allowed modes
free all
32 single channel
48 single channel
56 single channel
64 all
80 single channel
96 all
112 all
128 all
160 all
192 all
224 stereo, intensity stereo, dual channel
256 stereo, intensity stereo, dual channel
320 stereo, intensity stereo, dual channel
384 stereo, intensity stereo, dual channel

计算帧大小,您需要的样本数量每MPEG音频帧。因此,您可以使用如下表:

2.1.5 Samples Per Frame
MPEG 1 MPEG 2 (LSF) MPEG 2.5 (LSF)
Layer I 384 384 384
Layer II 1152 1152 1152
Layer III 1152 576 576

然后,你就可以计算出帧大小这样的:

 

Frame Size = ( (Samples Per Frame / 8 * Bitrate) / Sampling Rate) + Padding Size

 

由于四舍五入的错误,官方计算公式帧大小是一个有点不同。据ISO标准,你必须计算在帧大小插槽(见2. MPEG Audio Format ) ,然后截断这一数字为整数,之后乘它与插槽的大小。你可以找到正确的方式计算帧大小的类CMPAHeader在我的代码。

你的时间的文件在几秒钟内采用的公式如下:

 

Duration = File Size / Bitrate * 8

 

该方法获得的第一个个帧头中的文件,然后计算期限由上述公式正常工作只为CBR文件。

延伸的模式是用来加入信息,是没有用的立体声效果,从而减少了所需的位。这些位是动态确定的编码在联合立体声模式,联合立体声可以改变从一帧到另一国,或什至开启或关闭。对于所有其他渠道模式,推广模式领域是无效的。

完整的频率范围MPEG音频文件分为子带。有32子带。对于层I和II ,这两个位在头中确定的频率范围(频带)的强度立体声应用。在这个频率范围内,只有一个通道存储。所有其他带包含的信息在两个不同的渠道。对于层III ,这两个位确定哪些类型的联合立体声是用来(强度立体声和/或米/秒的立体声) 。

2.1.6 Mode Extension
Value Layer I & II Layer III
M/S stereo Intensity stereo
00 bands 4 to 31 off off
01 bands 8 to 31 off on
10 bands 12 to 31 on off
11 bands 16 to 31 on on

2.2 。验证CRC

如果保护位在头中没有设置,框架包含了16位CRC (循环冗余校验) 。这直接如下校验帧头和是一个很大的尾端WORD 。要验证这一点校验你必须计算的框架和比较CRC计算与存储CRC 。如果他们不等于转让可能出现的错误。这也是有益的CRC检查,以确认你真的发现的开始帧,因为这样做同步位在相同的情况下也发生在数据节的一帧。

该CRC计算方法是,通过运用CRC - 16算法(与生成器polynom 0x8005 )的部分范围。以下数据被认为是为CRC :最后两个字节的头和若干位的音频数据如下校验后的头。校验本身必须跳过CRC计算。不幸的是有没有简便的方法来计算的数目帧所必需的校验计算层II 。因此,我离开了代码。您需要其他信息除了头计算必要位。然而,它是有可能的数量计算的保护层位在我和层III只有在信息的头。

对于层III ,您认为完整的一侧信息CRC计算。在如下方面信息的头或CRC层III文件。它包含的信息一般解码框架,但并不包含实际的编码录音样品。下表显示的大小方面信息的所有层III文件。

2.2.1 Layer III side information size (in bytes)
MPEG 1 MPEG 2/2.5 (LSF)
Stereo, Joint Stereo, Dual Channel 32 17
Mono 17 9

对于层我的文件,您必须考虑的模式扩展(见表2.1.6 )的头。然后,你就可以计算number of bits所必需的CRC计算采用的公式如下:

 

4 * (number of channels * bound of intensity stereo +
(32 - bound of intensity stereo));

 

这可以被理解为2次的数量加上立体声子带的数目和单子带的结果乘以4 。对于简单单框架,这等于128 ,因为一些渠道之一和约束的强度立体声是32 ,也就是说,没有强度立体声。立体框架,这是256 。欲了解更多信息看一下CRC代码类CMPAFrame

2.3 。 VBR标题

有些文件编码与可变量比特率模式( VBR ) 。要估计的时间这些文件,你必须知道average bitrate整个文件。它常常很多不同的比特率的第一个个帧,因为最低的比特率可用于沉默的音乐书籍(尤其是在开头) 。要获得此平均码率,你必须通过所有帧中的文件和计算,通过总结比特率每帧的划分是通过一些帧。因为这不是一个很好的做法(非常缓慢) ,存在着更多的VBR头内的数据节的第一个个帧(帧后头) 。它们包含的总数范围内的文件,从中可以计算时间以秒计算公式为:

 

Duration = Number of Frames * Samples Per Frame / Sampling Rate

 

附加的是, VBR头往往载有一份表格,这是必须寻求职位文件。

2.3.1 XING头

这往往是头(但不幸的是,并非总是)添加到文件的编码与可变量比特率模式。这头站第一个个后,音频头MPEG在某一特定的位置。整个第一个个帧其中包含XING头是一个有效的,但空的音频帧,所以即使解码器,它不认为这头可以解码文件。 XING头的看台一侧后,信息层III文件。所以,你可以计算的开始XING头相对的开始帧增加4字节(为MPEG音频头)的值从table 2.2.1 。抵销计算不考虑16位CRC以下的头,等于为所有层,虽然只有层III有一个方面的信息。

宣读了这一头,你就必须找到第一个个MPEG音频头,然后进入这一特定位置的框架内。该XING头这个身具有以下格式。 (请注意,位置是零基;位置,长度和例子是每个字节的格式。 )

2.3.1.1 XING Header
Position Length Meaning Example

0

4 VBR header ID in 4 ASCII chars, either 'Xing' or 'Info', not NULL-terminated 'Xing'

4

4

Flags which indicate what fields are present, flags are combined with a logical OR. Field is mandatory.

0x0001 - Frames field is present
0x0002 - Bytes field is present
0x0004 - TOC field is present
0x0008 - Quality indicator field is present

0x0007
(means Frames, Bytes & TOC valid)
8 4 Number of Frames as Big-Endian DWORD (optional) 7344
8 or 12 4 Number of Bytes in file as Big-Endian DWORD (optional) 45000
8, 12 or 16 100 100 TOC entries for seeking as integral BYTE (optional)
8, 12, 16, 108, 112 or 116 4 Quality indicator as Big-Endian DWORD
from 0 - best quality to 100 - worst quality (optional)
0

根据这一格式,头必须XING只包含ID和旗帜。所有其他字段是可选的,取决于它的旗帜设置。有时,这种头也增加CBR文件。然后它往往具有ID 'Info' 而不是'Xing' 。

存在的LAME extension这个头,这是用普通LAME编码,但我并没有考虑到这一点,因为这是没有必要的时间估计。尽管如此,这里是连结documentation of the MP3 Info Tag

2.3.2 VBRI头

这头只用MPEG音频文件编码与编码弗劳恩霍夫据我所知。这是不同于XING头。你会发现它完全32 bytes结束后的第一个个头MPEG音频文件中的。 (请注意,位置是零基;位置,长度和例子是每个字节的格式。 )

2.3.2.1 VBRI Header
Position Length Meaning Example

0

4 VBR header ID in 4 ASCII chars, always 'VBRI', not NULL-terminated 'VBRI'

4

2 Version ID as Big-Endian WORD 1
6 2 Delay as Big-Endian float 7344
8 2 Quality indicator 75
10 4 Number of Bytes as Big-Endian DWORD 45000
14 4 Number of Frames as Big-Endian DWORD 7344
18 2 Number of entries within TOC table as Big-Endian WORD 100
20 2 Scale factor of TOC table entries as Big-Endian DWORD 1
22 2 Size per table entry in bytes (max 4) as Big-Endian WORD 2
24 2 Frames per table entry as Big-Endian WORD 845
26 TOC entries for seeking as Big-Endian integral. From size per table entry and number of entries, you can calculate the length of this field.

3 。附加标签

请考虑,在结尾或开头的文件,可能有更多的数据是不属于MPEG音频帧。这种数据被调用标记,因为它包含的数据文件,如标题,艺术家,田径,年等您必须考虑这些标记,因为只有MPEG音频数据计数的时间估计..在结束一个文件有可能是一个ID3V1标记,一个标记Lyrics3或Musicmatch的标记。在开始或结束时,该文件可能有ID3V2标签和/或APE标记。您找到有关ID3和Lyrics3标签www. 。 APE最初是为无损压缩音频文件的APE ( Monkey's Audio )格式。但是,现在这个标记也可用于MPEG音频文件。在Musicmatch的标记是用旧版这个的Musicmatch的编码。不幸的是很少有网页上的信息有关此标记,因为它是一个专有格式Musicmatch的。

4 。使用代码

我写的一些c++类处理MPEG音频帧头和VBR头。类CMPAFile代表整个文件,并提供方法访问一个特定范围内( CMPAFrame类) 。这个类实例一类CMPAHeader代表MPEG音频帧头框架这个身。继承各个领域的头可以通过类变量。此外,还有CVBRHeader类是概括CXINGHeaderCVBRIHeader 。所有的标签都继承类CTag 。这些类是完全独立于任何库一样MFC或ATL 。他们只使用Win32 API 。所有课程将抛出例外情况下的错误。所有例外的类型CMPAException 。您可以显示的信息除了使用ShowError()方法。所有课程使用CMPAStream的文件访问。这个类包含一个简单的缓冲区。

下面是一个代码段,这表明你可以使用这些类。请注意,您必须至少包括mpafile.h头:

 

try {
// open file and look for first header
    CMPAFile MPAFile(_T("C:\\test.mp3")) ;
cout << MPAFile.GetLengthSec() << _T(" seconds");
}
catch(CMPAException& Exc)
{
// show error message
    Exc.ShowError();
}

 

欲了解更多信息,看看源代码的演示项目MPEG Audio Info ,这是一个简单的MFC对话框应用程序,它使用类CMPAFile获取信息MPEG的音频文件。它也可以执行检查整个文件的错误的框架结构。

5 。链接和杂项

在这里,你可以找到的来源,我用于此文章:

 

如果您发现任何错误代码或文章,或有改进建议,只要他们后论坛低于或写箱电子邮件至webmaster@wincd.de

6 。历史

  • 2004-09-15
    • Version 1.0 of MPEG Audio Info released.
    • First version of the article published.
  • 2004-11-01
    • Version 2.0 of MPEG Audio Info released.
      • added possibility to get information about arbitrary frames (not just the first one).
      • added checking of MPEG audio files.
      • better handling of faulty MPEG files (tolerance range +/-3 bytes to look for the next frame).
      • padding is detected correctly.
      • improved buffer management.
      • fixed some other small issues.
      • added a readme file.
    • Article updated:
      • added information about mode extension field.
      • XING header offsets fixed (Mono and Stereo inverted).
      • frames and bytes in VBRI fixed (they were inverted).
      • position of quality indicator in VBRI header fixed.
      • corrected some typos.
  • 2005-11-17
    • Version 2.1 of MPEG Audio Info released.
      • cleaned up class architecture.
      • better navigation through frames.
      • improved checking of files.
      • fixed original bit checking.
      • fixed frame size calculation for all layers (correct truncation).
      • fixed skipping of small frames in VBR files.
      • new cache management.
      • added CRC check.
      • added detection of ID3V1/V2, APE and Lyrics3 tags.
    • Article updated:
      • added information about CRC.
      • fixed sampling rate table.
      • some minor enhancements.
  • 2007-04-09
    • Version 2.2 of MPEG Audio Info released.
      • added Musicmatch-Tag detection
      • better exception handling
      • improved design of CMPAStream
      • added Drag&Drop functionality to dialog
      • fixed memory leaks
      • updated solution to Visual Studio 2005
      • better performance for finding frames
    • Article updated:
      • fixed broken links
      • added link with MPEG Audio Specifications
      • added link with informations about Musicmatch

 

所含源码列表:
Download demo project (binary and source, V2.2) - 225.4 KB
Download source (V2.2) - 21.9 KB

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多