AAC
常见flv文件封装的音频用的都是aac编码,aac文件有两种格式,一个是ADIF(Audio Data Interchange Format, 音频数据交换格式 ),一个是ADTS(Audio Data Transport Stream, 音频数据传输流)。其中ADIF多用于磁盘存储,仅在文件首部保存解码信息,而ADTS格式用于流式传输,因此每一帧都包含头信息,可以从任一帧开始解码,故用于流式传输。
ADTS
ADTS AAC文件格式如下:
ADTS_header | AAC ES | ADTS_header | AAC ES | ······· |
---|
ADTS header
Field | Type | Commet |
---|---|---|
Syncword | 12 bits | The bit string ‘1111 1111 1111’. |
ID | 1 bit | MPEG identifier, 0 for MPEG-4, 1 for MPEG-2 |
layer | 2 bits | Indicates which layer is used. Set to ‘00’. |
protection_absent | 1 bit | Set to 1 if there is no CRC and 0 if there is CRC |
profile_ObjectType | 2 bits | audio_object_type - 1 |
sampling_frequency_index | 4 bits | Integer value of the sampling frequency used. |
private_bit | 1 bit | Set to 0 when encoding, ignore when decoding |
channel_configuration | 3 bits | Indicates the channel configuration used. |
original | 1 bit | set to 0 when encoding , ignore when decoding |
home | 1 bit | set to 0 when encoding , ignore when decoding |
copyright id bit | 1 bit | set to 0 when encoding , ignore when decoding |
copyright id start | 1 bit | set to 0 when encoding , ignore when decoding |
frame length | 13 bits | this value is 7 or 9 bytes of ADTS + size(AAVFrame) |
buffer_fullness | 11 bits | State of the bit reservoir after encoding the first raw data |
number of AAC frames | 2 bits | always use 1 AAC frame per ADTS frame |
CRC | 16 bits | if 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;