一、AAC 裸流。
裸流无法播放,每一帧都要加上adts 头。
/**
* 添加ADTS头
* 一帧AAC+7 = packetLen
* @param packet
* @param packetLen
*/
private void addADTStoPacket(byte[] packet, int packetLen) {
int profile = 2; // AAC LC
int freqIdx = 8; // 16KHz
int chanCfg = 1; // CPE
// fill in ADTS data
packet[0] = (byte) 0xFF;
packet[1] = (byte) 0xF1;
packet[2] = (byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
packet[3] = (byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
packet[4] = (byte) ((packetLen & 0x7FF) >> 3);
packet[5] = (byte) (((packetLen & 7) << 5) + 0x1F);
packet[6] = (byte) 0xFC;
}
其中,
profile:表示使用哪个级别的AAC,如01 Low Complexity(LC) -- AAC LC
profile的值等于 Audio Object Type的值减1.
profile = MPEG-4 Audio Object Type - 1
sampling_frequency_index:采样率的下标
0x00 96000
0x01 88200
0x02 64000
0x03 48000
0x04 44100
0x05 32000
0x06 24000
0x07 22050
0x08 16000
0x09 12000
0x0A 11025
0x0B 8000
0x0C reserved
0x0D reserved
0x0E reserved
0x0F escape value
private int getFreqIndex(int SAMPLE_RATE) {
switch (SAMPLE_RATE) {
case 96000:
return 0;
case 88200:
return 1;
case 64000:
return 2;
case 48000:
return 3;
case 44100:
return 4;
case 32000:
return 5;
case 24000:
return 6;
case 22050:
return 7;
case 16000:
return 8;
case 12000:
return 9;
case 11025:
return 10;
case 8000:
return 11;
default:
return 11;
}
}
channel_configuration:声道数,比如2表示立体声双声道,
0x00 - defined in audioDecderSpecificConfig
0x01 单声道(center front speaker)
0x02 双声道(left, right front speakers)
0x03 三声道(center, left, right front speakers)
0x04 四声道(center, left, right front speakers, rear surround speakers)
0x05 五声道(center, left, right front speakers, left surround, right surround rear speakers)
0x06 5.1声道(center, left, right front speakers, left surround, right surround rear speakers, front low frequency effects speaker)
0x07 7.1声道(center, left, right center front speakers, left, right outside front speakers, left surround, right surround rear speakers, front low frequency effects speaker)
0x08-0x0F - reserved
二、AAC csd-0 参数意义
//用来标记AAC是否有adts头,1->有
mediaFormat.setInteger(MediaFormat.KEY_IS_ADTS, 1);
//ByteBuffer key
byte[] data = new byte[]{(byte) 0x14, (byte) 0x08};
ByteBuffer csd_0 = ByteBuffer.wrap(data);
//ADT头的解码信息
mediaFormat.setByteBuffer("csd-0", csd_0);
//解码器配置
audioDecoder.configure(mediaFormat, null, null, 0);
其中的data就是解码AAC的关键信息,该信息的格式:
AAC Profile 5bits | 采样率 4bits | 声道数 4bits | 其他 3bits |
如果是8K的采样率则换成:data = new byte[]{(byte) 0x15, (byte) 0x88};
网友评论