更新时间:2022-05-13 08:29:31
通常来说AAC的头信息在编解码过程中是可以获取到的,但今天需要根据音频参数生成相应的AAC头。项目中使用的是AAC_LC,今天先对它的结构进行分析。
项目中使用ffmpeg进行音频编码,音频编码库为FAAC,好吧,直接看代码吧。
ffmpeg调用Faac_encode_init()初始化编码器;在Faac_encode_init()调用faacEncGetDecoderSpecificInfo()获取AAC_LC头信息内容及长度。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
int FAACAPI faacEncGetDecoderSpecificInfo(faacEncHandle hEncoder,unsigned char ** ppBuffer,unsigned long * pSizeOfDecoderSpecificInfo)
{ BitStream* pBitStream = NULL; <span style= "font-family:Times New Roman;" ></span> if ((hEncoder == NULL) || (ppBuffer == NULL) || (pSizeOfDecoderSpecificInfo == NULL)) {
return -1;
}
if (hEncoder->config.mpegVersion == MPEG2){
return -2; /* not supported */
}
*pSizeOfDecoderSpecificInfo = 2;
*ppBuffer = malloc (2);
if (*ppBuffer != NULL){
memset (*ppBuffer,0,*pSizeOfDecoderSpecificInfo);
pBitStream = OpenBitStream(*pSizeOfDecoderSpecificInfo, *ppBuffer);
PutBit(pBitStream, hEncoder->config.aacObjectType, 5);
PutBit(pBitStream, hEncoder->sampleRateIdx, 4);
PutBit(pBitStream, hEncoder->numChannels, 4);
CloseBitStream(pBitStream);
return 0;
} else {
return -3;
}
} |
从代码中可以看出,头数据长度固定为2;
数据内容由高位到低位依次为:aacObjectType(5bits),sampleRateIdx(4bits),numChannels(4bits)
例如:音频编码参数为:
aacObjectType:AAC_LC,对应值为2,用5bit二进制表示为00010;
sampleRate:44100KHz, 对应的IDX值为4, 用4bit二进制表示为0100;
numChannels:2,对应的值为2,用4bit二进制表示为0010;
将它们由高位到低位串起来:0001,0010,0001,0000,
则,对应的十六进制值为:0x1220
引一些参考资料:http://wiki.multimedia.cx/index.php?title=MPEG-4_Audio
The Audio Specific Config is the global header for MPEG-4 Audio:
5 bits: object type if (object type == 31) 6 bits + 32: object type 4 bits: frequency index if (frequency index == 15) 24 bits: frequency 4 bits: channel configuration var bits: AOT Specific Config
MPEG-4 Audio Object Types:
There are 13 supported frequencies:
These are the channel configurations: