一、ADTS 格式
ADTS(Audio Data Transport Stream),通常被用于 MPEG TS 标准和流化音频格式为 AAC 的一种音频数据格式。
ADTS 数据格式:
Morphology | Length(Bits) | Description |
syncword | 12 | syncword 0xFFF, all bits must be 1 |
ID | 1 | MPEG Version: 0 for MPEG-4, 1 for MPEG-2 |
layer | 2 | Layer: always 0 |
protection_absent | 1 | protection absent, Warning, set to 1 if there is no CRC and 0 if there is CRC |
profile | 2 | profile, the MPEG-4 Audio Object Type minus 1 |
sampling_frequency_index | 4 | MPEG-4 Sampling Frequency Index (15 is forbidden) |
private_bit | 1 | private bit, guaranteed never to be used by MPEG, set to 0 when encoding, ignore when decoding |
channel_configuraion | 3 | MPEG-4 Channel Configuration (in the case of 0, the channel configuration is sent via an inband PCE) |
original_copy | 1 | originality, set to 0 when encoding, ignore when decoding |
home | 1 | home, set to 0 when encoding, ignore when decoding |
copyright_identification_bit | 1 | copyrighted id bit, the next bit of a centrally registered copyright identifier, set to 0 when encoding, ignore when decoding |
copyright_identification_start | 1 | copyright id start, signals that this frame's copyright id bit is the first bit of the copyright id, set to 0 when encoding, ignore when decoding |
aac_frame_length | 13 | frame length, this value must include 7 or 9 bytes of header length: FrameLength = (ProtectionAbsent == 1 ? 7 : 9) + size(AACFrame) |
adts_buffer_fullness | 11 | Buffer fullness |
number_of_raw_data_blocks_in_frame | 2 | Number of AAC frames (RDBs) in ADTS frame minus 1, for maximum compatibility always use 1 AAC frame per ADTS frame |
CRC | 16 | CRC if protection absent is 0 |
二、ADTS 解析
常用参数解析:
// profile [16-17]
fmt->profile = frm_buf[2] >> 6;
// sample index [18-21]
uint8_t sampling_index = (frm_buf[2] & 0x3c) >> 2;
fmt->sample_rate = sample_rate_for(sampling_index);
// channels [23-25]
fmt->channels = (frm_buf[2] & 0x01) << 7 | frm_buf[3] >> 6;
// frame size [30-42]
uint32_t frame_size = 0;
frame_size |= (((frm_buf[3] & 0x03)) << 11);//high 2 bit
frame_size |= (frm_buf[4] << 3);//middle 8 bit
frame_size |= ((frm_buf[5] & 0xe0) >> 5);//low 3bit
三、ADTS 生成
int sample_rate_index = sample_index_for(fmt->sample_rate);
if (sample_rate_index < 0) {
return sample_rate_index;
}
adts_buf[0] = 0xff;
adts_buf[1] = 0xf1;
adts_buf[2] = fmt->profile << 6 | sample_rate_index << 2 | 0x02 | fmt->channels >> 2;
int aac_frame_length = fmt->data_size + HP_AAC_ADTS_HEADER_SIZE;
adts_buf[3] = fmt->channels << 6 | aac_frame_length >> 11;
adts_buf[4] = aac_frame_length >> 3 & 0x000000ff;
adts_buf[5] = (aac_frame_length & 0x00000007) << 5 | 0x1f;
adts_buf[6] = 0xfc;
已验证,源码地址:https://github.com/cwyethism/hp-utils-adts