AAC/ADTS格式

AAC

​ 常见flv文件封装的音频用的都是aac编码,aac文件有两种格式,一个是ADIF(Audio Data Interchange Format, 音频数据交换格式 ),一个是ADTS(Audio Data Transport Stream, 音频数据传输流)。其中ADIF多用于磁盘存储,仅在文件首部保存解码信息,而ADTS格式用于流式传输,因此每一帧都包含头信息,可以从任一帧开始解码,故用于流式传输。

ADTS

ADTS AAC文件格式如下:

ADTS_headerAAC ESADTS_headerAAC ES·······

ADTS header

FieldTypeCommet
Syncword12 bitsThe bit string ‘1111 1111 1111’.
ID1 bitMPEG identifier, 0 for MPEG-4, 1 for MPEG-2
layer2 bitsIndicates which layer is used. Set to ‘00’.
protection_absent1 bitSet to 1 if there is no CRC and 0 if there is CRC
profile_ObjectType2 bitsaudio_object_type - 1
sampling_frequency_index4 bitsInteger value of the sampling frequency used.
private_bit1 bitSet to 0 when encoding, ignore when decoding
channel_configuration3 bitsIndicates the channel configuration used.
original1 bitset to 0 when encoding , ignore when decoding
home1 bitset to 0 when encoding , ignore when decoding
copyright id bit1 bitset to 0 when encoding , ignore when decoding
copyright id start1 bitset to 0 when encoding , ignore when decoding
frame length13 bitsthis value is 7 or 9 bytes of ADTS + size(AAVFrame)
buffer_fullness11 bitsState of the bit reservoir after encoding the first raw data
number of AAC frames2 bitsalways use 1 AAC frame per ADTS frame
CRC16 bitsif protection_absent is 0

​ 在flv文件中,上述Audio Object Type,Channel Configrartion等信息存储在AudioSpecificConfig结构中,该AudioSpecificConfig结构通常存储在第一个Audio Tag数据中。如下:

5 bits: audio object type
4 bits: sampling frequency index
if (frequency index == 15)
    24 bits: frequency index
4 bits: channel configuration
other bits: audio object type specific config

利用AudioSpecificConfig信息获取audio_object_type、sampling_frequency_index、channel_config

uint8_t audio_object_type = 0;
uint8_t sampling_frequency_index = 0;
uint8_t channel_config = 0;
 
//audio object type:5bit
audio_object_type = szAudioSpecificConfig[0] & 0xf8;
audio_object_type >>= 3;
 
//sampling frequency index:4bit
//高3bits
sampling_frequency_index = szAudioSpecificConfig[0] & 0x07;
sampling_frequency_index <<= 1;
//低1bit
uint8_t tmp = szAudioSpecificConfig[1] & 0x80;
tmp >>= 7;
sampling_frequency_index |= tmp;
 
//channel config:4bits
channel_config = szAudioSpecificConfig[1] & 0x78;
channel_config >>= 3;

计算ADTS header

char Adtsheader[7] = {0};

AdtsHeader[0] = 0xff;         //syncword:0xfff                          高8bits
AdtsHeader[1] = 0xf0;         //syncword:0xfff                          低4bits
AdtsHeader[1] |= (0 << 3);    //MPEG Version:0 for MPEG-4,1 for MPEG-2  1bit
AdtsHeader[1] |= (0 << 1);    //Layer:0                                 2bits 
AdtsHeader[1] |= 1;           //protection absent:1                     1bit

//profile:audio_object_type - 1                      2bits
AdtsHeader[2] = (audio_object_type - 1)<<6;        
//sampling frequency index:sampling_frequency_index  4bits 
AdtsHeader[2] |= (sampling_frequency_index & 0x0f)<<2; 
//private bit:0                                      1bit
AdtsHeader[2] |= (0 << 1);         
//channel configuration:channel_config               高1bit
AdtsHeader[2] |= (channel_config & 0x04)>>2;           
//channel configuration:channel_config      		 低2bits
AdtsHeader[3] = (channel_config & 0x03)<<6;     
//original:0                              			 1bit
AdtsHeader[3] |= (0 << 5);               
//home:0                                			 1bit
AdtsHeader[3] |= (0 << 4);                
//copyright id bit:0                                1bit  
AdtsHeader[3] |= (0 << 3);               
//copyright id start:0                              1bit	
AdtsHeader[3] |= (0 << 2);                      

// 处理frame length,tagheader_datasize为flv tag size有效字段
int AdtsLen = tagheader_datasize - 2 + 7;
AdtsHeader[3] |= ((AdtsLen & 0x1800) >> 11);           //frame length:value     高2bits
AdtsHeader[4] = (uint8_t)((AdtsLen & 0x7f8) >> 3);     //frame length:value     中间8bits 
AdtsHeader[5] = (uint8_t)((AdtsLen & 0x7) << 5);       //frame length:value     低3bits
AdtsHeader[5] |= 0x1f;                                 //buffer fullness:0x7ff  高5bits 
AdtsHeader[6] = 0xfc;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值