amr格式解析

来源:互联网 发布:怎样打造淘宝爆款 编辑:程序博客网 时间:2024/05/22 03:59

AMR File Format:

AMR( AdaptiveMultiRate ) is an audio format which is extensively used in mobile devices in various applications ranging from normal audio player/recorder to VoIP kind of applications. AMR can be further categorized as

1) AMR-NB( NarrowBand )

2) AMR-WB( WideBand )

But in general terms, AMR refers to AMR-NB. The AMR file format (*.amr) has the following structure:

Each AMR file consists of a 6-byte header that identifies the file as AMR audio. This header is always set to: 0x23, 0x21, 0x41, 0x4D, 0x52, 0x0A. This is common across all AMR-NB files. If the header is not as it should be, the file is probably corrupted and should not be used.

From here( ie. from the 7th byte ) the AMR file consists of a whole number of packed frames of audio (into bandwidth inefficient, octet-aligned mode ). These frames each constitute 20ms of audio. Each frame can be encoded using any of the valid AMR-NB modes (0-7, 8 SID in DTX mode). Because the frames can be encoded at different rates, this method of encoding is called Adaptive Multi-Rate(AMR).

Each frame can be encoded using one of 8 varying levels of compression using various bitrates(AMR modes 0-7). Following are the various AMR modes and their corresponding bitrates:

MODE               BIT RATES0           - AMR 4.75 - Encodes at 4.75kbit/s1           - AMR 5.15 - Encodes at 5.15kbit/s2           - AMR 5.9  - Encodes at 5.9kbit/s3           - AMR 6.7  - Encodes at 6.7kbit/s4           - AMR 7.4  - Encodes at 7.4kbit/s5           - AMR 7.95 - Encodes at 7.95kbit/s6           - AMR 10.2 - Encodes at 10.2kbit/s7           - AMR 12.2 - Encodes at 12.2kbit/s

Each frame consists of a 1-byte header, then the rest of the frame is audio data. The entire frame is fed into the AMR decoder (header too). The frame size can be deduced from the frame header.

The 2nd bit through 5th bit (counting the most significant bit as the first bit) comprise the CMR (Codec Mode Request), values 0-7 being valid for AMR. The top bit of the CMR can actually be ignored, though it is used when AMR forms RTP payloads. The lower 3-bits of the header are reserved and are not used. Viewing the header from most significant bit to least significant bit, the encoding is XCCCCXXX, where Xs are reserved (typically 0) and the Cs are the CMR.

Frame size of AMR modes in bytes (including the header byte) are shown below:

CMR      MODE        FRAME SIZE( in bytes )0 AMR 4.75 131 AMR 5.15 142 AMR 5.9 163 AMR 6.7 184 AMR 7.4 205 AMR 7.95 216 AMR 10.2 277 AMR 12.2 32

目录
1.    概述
2.    AMR编码方式
3.    AMR文件结构示范图
4.    AMR帧头格式分析
5.    异常帧分析
6.    AMR帧读取算法
7.    参考资料

1.      概述

现在很多智能手机都支持多媒体功能,特别是音频和视频播放功能,而AMR文件格式是手机端普遍支持的音频文件格式。

 

AMR,全称是:Adaptive Multi-Rate,自适应多速率,是一种音频编码文件格式,专用于有效地压缩语音频率。

 

AMR音频主要用于移动设备的音频压缩,压缩比非常高,但是音质比较差,主要用于语音类的音频压缩,不适合对音质要求较高的音乐类音频的压缩。

 

下面是两个AMR示范文件,一个是新闻类,一个是歌曲,可以听出来,音乐类压缩成AMR格式后,音质大大降低。

编号

文件名

文件长度

时长

1

h.amr              (歌曲)

409KB

04:21

2

zbc1217.amr

242KB

02:36

 

 


2.        AMR编码方式

AMR有多种编码方式,每种编码方式的采样频率不同:

 

 

规格

比特率(kbps

音频帧大小(字节)

帧头(字节)

FT

0

AMR 4.75

 

13

04 00000100

0000

1

AMR 5.15

 

14

0C 00001100

0001

2

AMR 5.9

 

16

14 00010100

0010

3

AMR 6.7

 

18

1C 00011100

0011

4

AMR 7.4

 

20

24 00100100

0100

5

AMR 7.95

 

21

2C 00101100

0101

6

AMR 10.2

 

27

34 00110100

0110

7

AMR 12.2

 

32

3C 00111100

0111

 

说明:

l         比特率是指将数字声音由模拟格式转化成数字格式的采样率,采样率越高,还原后的音质就越好。

 

l         比特率值与现实音频对照:

16kbps=电话音质

24kbps=增加电话音质、短波广播、长波广播、欧洲制式中波广播

40kbps=美国制式中波广播

56kbps=话音

64kbps=增加话音(手机铃声最佳比特率设定值、手机单声道MP3播放器最佳设定值)

112kbps=FM调频立体声广播

128kbps=磁带(手机立体声MP3播放器最佳设定值、低档MP3播放器最佳设定值)

160kbps=HIFI高保真(中高档MP3播放器最佳设定值)

192kbps=CD(高档MP3播放器最佳设定值)

256kbps=Studio音乐工作室(音乐发烧友适用)

 

l         音频数据帧大小的计算:

amr 一帧对应20ms,那么一秒有50帧的音频数据。由于比特率不同,每帧的数据大小也不同。

如果比特率是12.2kbs,那么每秒采样的音频数据位数为:

12200 / 50 = 244bit = 30.5byte,取整为31字节。

取整要四舍五入。

再加上一个字节的帧头,这样数据帧的大小为32字节。

 


3.      AMR文件结构示范图

下面是一个AMR文件示范:

从图中可以看出,所有AMR文件头标志是6个字节。(最后一个字节是换行符“/n”)

后面就紧跟的是音频帧。这个文件是每帧32字节。

 

文件头

语音帧 1

语音帧 2

 

 


4.      AMR帧头格式分析

AMR语音帧格式为帧头和语音数据组成。

帧头

语音数据

 

AMR语音帧头占1个字节,如下图所示:

0

1

2

3

4

5

6

7

P

FT

Q

P

P

0

 

 

 

 

0

0

0

 

其中:

P = 0

FTFrame Type,对应不同编码模式。(参见“2. AMR编码方式”中表格中的说明)

Q:帧质量指示器,0:表示为坏帧。

后面的P0

 


5.      异常帧分析

下图是一个出现异常帧的示例:(zbc1217.amr

 

从上图可以看见,本amr音频帧头都是0x3C,但是在0x000091e6处,帧头字节为0x44。与0x3C不一致。而在0x00009243处,才恢复到0x3C

 

对异常帧的简单处理办法是,遇到异常帧就跳过异常帧。后面的读帧算法有描述。

 


6.      AMR帧读取算法

因为可能存在异常帧,所以不一定所有的语音帧大小一致,对于跟正常帧大小不一致的,或者帧头跟正常帧头不一致的,就不交给解码器,直接抛弃该坏帧。

 

读取帧的算法,用C语言来编写,readAMRFrame.cJAVA可以用类似的方法。

下面是算法描述流程图。



7.      参考资料

l         rfc3267

http://www.rfc-editor.org/rfc/rfc3267.txt

http://ietfreport.isoc.org/rfc/PDF/rfc3267.pdf

l         3GPP TS 26.104 V 6.1.0 (2004-03)

http://www.3gpp.org/ftp/Specs/html-info/26104-CRs.htm

l         3GPP AMR Floating-point Speech Codec

http://www.3gpp.org/ftp/Specs/html-info/26104.htm

l         amr编程汇总”

http://blog.csdn.net/windcao/archive/2006/01/04/570348.aspx

l         关于AMR文件格式的解释

http://www.mcublog.com/blog/user1/11409/archives/2006/16832.html

0 0