FLV视频和音频解析学习(二)

3.2 Audio Tag

  如果TAG包中的TagType==8时,就表示这个TAGaudio

StreamID之后的数据就表示是AudioTagHeaderAudioTagHeader结构如下:

Field

Type

Comment

SoundFormat

UB [4]

Format of SoundData. The following values are defined:
0 = Linear PCM, platform endian
1 = ADPCM
2 = MP3
3 = Linear PCM, little endian
4 = Nellymoser 16 kHz mono
5 = Nellymoser 8 kHz mono
6 = Nellymoser
7 = G.711 A-law logarithmic PCM
8 = G.711 mu-law logarithmic PCM
9 = reserved
10 = AAC
11 = Speex
14 = MP3 8 kHz
15 = Device-specific sound
Formats 7, 8, 14, and 15 are reserved.
AAC is supported in Flash Player 9,0,115,0 and higher.
Speex is supported in Flash Player 10 and higher.

SoundRate

UB [2]

Sampling rate. The following values are defined:
0 = 5.5 kHz
1 = 11 kHz
2 = 22 kHz
3 = 44 kHz

SoundSize

UB [1]

Size of each audio sample. This parameter only pertains to
uncompressed formats. Compressed formats always decode
to 16 bits internally.
0 = 8-bit samples
1 = 16-bit samples

SoundType

UB [1]

Mono or stereo sound
0 = Mono sound
1 = Stereo sound

AACPacketType

IF SoundFormat == 10
UI8

The following values are defined:
0 = AAC sequence header
1 = AAC raw

 

AudioTagHeader的头1个字节,也就是接跟着StreamID1个字节包含着音频类型、采样率等的基本信息.表里列的十分清楚.

AudioTagHeader之后跟着的就是AUDIODATA数据了,也就是audio payload 但是这里有个特例,如果音频格式(SoundFormat)是10 = AACAudioTagHeader中会多出1个字节的数据AACPacketType,这个字段来表示AACAUDIODATA的类型:0 = AAC sequence header1 = AAC raw

Field

Type

Comment

Data

IF AACPacketType ==0 AudioSpecificConfig

The AudioSpecificConfig is defined in ISO14496-3. Note that this is not the same as the contents of the esds box from an MP4/F4V file.

 

ELSE IF AACPacketType == 1 Raw AAC frame data in UI8 [ ]

audio payload

AAC sequence header也就是包含了AudioSpecificConfigAudioSpecificConfig包含着一些更加详细音频的信息,AudioSpecificConfig的定义在ISO14496-31.6.2.1 AudioSpecificConfig,这里就不详细贴了。而且在ffmpeg中有对AudioSpecificConfig解析的函数,ff_mpeg4audio_get_config(),可以对比的看一下,理解更深刻。

AAC raw 这种包含的就是音频ES流了,也就是audio payload.

FLV的文件中,一般情况下 AAC sequence header 这种包只出现1次,而且是第一个audio tag,为什么要提到这种tag,因为当时在做FLVdemux的时候,如果是AAC的音频,需要在每帧AAC ES流前边添加7个字节ADST,ADST在音频的格式中会详细解读,这是解码器通用的格式,就是AAC的纯ES流要打包成ADST格式的AAC文件,解码器才能正常播放.就是在打包ADST的时候,需要samplingFrequencyIndex这个信息,samplingFrequencyIndex最准确的信息是在AudioSpecificConfig中,所以就对AudioSpecificConfig进行解析并得到了samplingFrequencyIndex

到这步你就完全可以把FLV 文件中的音频信息及数据提取出来,送给音频解码器正常播放了。


AudioSpecificConfig

 

ADTS的全称是Audio Data Transport Stream。是AAC音频的传输流格式。

       AAC音频格式在MPEG-2ISO-13318-7 2003)中有定义。AAC后来又被采用到MPEG-4标准中。

       1. adts_sequence()

       {

              while (nextbits() == syncword)

              {

                     adts_frame();

              }

       }

 

       2. adts_frame()

       {

              adts_fixed_header();

              adts_variable_header();

              if (number_of_raw_data_blocks_in_frame == 0)

              {

                     adts_error_check();

                     raw_data_block();

              } else

              {

                     adts_header_error_check();

                     for (i = 0; i <= number_of_raw_data_blocks_in_frame; i++)

                     {

                             raw_data_block();

                             adts_raw_data_block_error_check();

                     }

              }

       }

 

       3. adts_fixed_header()

       {

              syncword: 12 bslbf

              ID: 1 bslbf

              layer: 2 uimsbf

              protection_absent: 1 bslbf

              profile: 2 uimsbf

              sampling_frequency_index: 4 uimsbf

              private_bit: 1 bslbf

              channel_configuration: 3 uimsbf

              original/copy: 1 bslbf

              home: 1 bslbf

       }

      

       adts_variable_header()

       {

              copyright_identification_bit: 1 bslbf

              copyright_identification_start: 1 bslbf

              frame_length: 13 bslbf

              adts_buffer_fullness: 11 bslbf

              number_of_raw_data_blocks_in_frame: 2 uimsfb

       }

 

       详细说明下ADTS头的重要数据部分:

       syncword 同步字The bit string 1111 1111 1111’,说明一个ADTS帧的开始。

       ID    MPEG 标示符设置为1.

       layer Indicates which layer is used. Set to ‘00’

       protection_absent 表示是否误码校验

       profile 表示使用哪个级别的AAC,如01 Low Complexity(LC)--- AACLC

       sampling_frequency_index 表示使用的采样率下标

       sampling_frequency_index sampling frequeny [Hz]

       0x0       96000

       0x1           88200

       0x2          64000

       0x3          48000

       0x4          44100

       0x5          32000

       0x6            24000

       0x7          22050

       0x8          16000

       0x9          2000

       0xa          11025

       0xb          8000

       0xc          reserved

       0xd          reserved

       0xe          reserved

       0xf           reserved

       channel_configuration 表示声道数

       frame_length 一个ADTS帧的长度包括ADTS头和raw data block.

       adts_buffer_fullness  0x7FF 说明是码率可变的码流

       number_of_raw_data_blocks_in_frame

      表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1AAC原始帧.

       所以说number_of_raw_data_blocks_in_frame == 0 表示说ADTS帧中有一个AAC数据块并不是说没有。(一个AAC原始帧包含一段时间内1024个采样及相关数据)

封装AAC为ADTS帧 

       一个AAC原始数据块长度是可变的,对原始帧加上ADTS头进行ADTS 的封装,就形成了ADTS帧。通常我们将得到的AAC原始帧进行封装后写入文件,用常用的播放器如千千静听即可播放,这是个验证AAC数据是否正确的方法。   

       进行封装前,需要了解相关参数,如采样率,声道数,原始数据块的长度等。下面把AAC原始数据帧加工为ADTS帧,据相关参数填写组成7字节的ADTS头。

       The ADTS header is defined below -    

       unsigned int obj_type = 0;    

       unsigned int num_data_block = frame_length / 1024; 

       // include the header length also     

       frame_length += 7;   

       /* We want the same metadata */    

       /* Generate ADTS header */    

       if(adts_header == NULL) return;    

       /* Sync point over a full byte */    

       adts_header[0] = 0xFF;    

       /* Sync point continued over first 4 bits + static 4 bits    

      * (ID, layer, protection)*/    

       adts_header[1] = 0xF9;    

       /* Object type over first 2 bits */    

       adts_header[2] = obj_type << 6;//    

       /* rate index over next 4 bits */    

       adts_header[2] |= (rate_idx << 2);    

       /* channels over last 2 bits */    

       adts_header[2] |= (channels & 0x4) >> 2;   

       /* channels continued over next 2 bits + 4 bits at zero */    

       adts_header[3] = (channels & 0x3) << 6;    

       /* frame size over last 2 bits */    

       adts_header[3] |= (frame_length & 0x1800) >> 11;    

       /* frame size continued over full byte */    

       adts_header[4] = (frame_length & 0x1FF8) >> 3;    

       /* frame size continued first 3 bits */    

       adts_header[5] = (frame_length & 0x7) << 5;    

       /* buffer fullness (0x7FF for VBR) over 5 last bits*/    

       adts_header[5] |= 0x1F;    

       /* buffer fullness (0x7FF for VBR) continued over 6 first bits + 2 zeros     

       * number of raw data blocks */

       adts_header[6] = 0xFC;// one raw data blocks .

       adts_header[6] |= num_data_block & 0x03; //Set raw Data blocks.



参考:

http://blog.csdn.net/jwybobo2007/article/details/9221657#0-qzone-1-75775-d020d2d2a4e8d1a374a433f596ad1440


http://blog.csdn.net/bsplover/article/details/7426476

AAC ADTS格式分析

http://blog.sina.com.cn/s/blog_47fefca50102w9zy.html

http://blog.163.com/zhujiatc@126/blog/static/1834638201342432628426/

https://www.cnblogs.com/cslunatic/p/6042875.html

http://blog.csdn.net/jzjhome/article/details/75270727


猜你喜欢

转载自blog.csdn.net/sunshine_505/article/details/80912075