近期需要做AAC编码,看了下几个开源的AAC编码库,综合考虑,最终选了fdk aac编码库。主要有以下几点原因:
1. 开源、音质好(目前认为是开源AAC编码器里音质最好的)
2. 音质,顺序大概是libfdk_aac>libfaac>aac>libvo_aacenc
3. 支持LC-AAC、HE-AAC、HE-AAC V2等多种编码
下载链接
http://www.linuxfromscratch.org/blfs/view/svn/multimedia/fdk-aac.html
编译安装
目前是在ubuntu下运行程序。我使用的是Ubuntu 14.04,其他linux发行版类似。将下载的文件解压,Terminal进入解压目录,“./configure”执行默认配置,然后“make”,最后“make install”即可。
如果需要在开发板上运行,可参考下面:
cd fdk-aac-0.1.6
export CC=arm-linux-gcc
export CXX=arm-linux-g++
./configure --prefix=/home/cwb/app/fdkaac --host=arm-linux
代码编写
使用FDK AAC库进行编码比较简单,主要是Encoder初始化和编码两大部分。
Encoder初始化:
1.调用aacEncOpen()来分配所需配置的编码器实例。函数原型:
aacEncOpen(HANDLE_AACENCODER *phAacEncoder, const UINT encModules, const UINT maxChannels)
2.调用aacEncoder_SetParam()来设置编码参数,AOT、采样率、通道模式、比特率和传输类型都是必需设置的。函数原型:
aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder, const AACENC_PARAM param, const UINT value)
部分代码示例:
if (aacEncoder_SetParam(paac_context->handle, AACENC_AOT, paac_context->aacenc_type) != AACENC_OK) {
printf("Unable to set the AOT\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_SBR_MODE, paac_context->sbr_enable) != AACENC_OK) {
printf("Unable to set the SBR MODE enable\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_SAMPLERATE, paac_context->sampleRate) != AACENC_OK) {
printf("Unable to set the AOT\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_CHANNELMODE, paac_context->enc_ch) != AACENC_OK) {
printf("Unable to set the channel mode\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_CHANNELORDER, paac_context->input_ch_order) != AACENC_OK) {
printf("Unable to set the channel order\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_BITRATEMODE, paac_context->bit_rate_mode) != AACENC_OK) {
printf("Unable to set the bit_rate_mode\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_BITRATE, paac_context->bit_rate) != AACENC_OK) {
printf("Unable to set the bitrate\n");
goto error;
}
if (aacEncoder_SetParam(paac_context->handle, AACENC_TRANSMUX, paac_context->aac_format) != AACENC_OK) {
printf("Unable to set the ADTS transmux\n");
goto error;
}
3.用NULL参数调用aacEncEncode()来初始化编码器实例和现有的参数集。函数原型:
aacEncEncode(const HANDLE_AACENCODER hAacEncoder, const AACENC_BufDesc *inBufDesc, const AACENC_BufDesc *outBufDesc, const AACENC_InArgs *inargs, AACENC_OutArgs *outargs)
部分代码示例:
if (aacEncEncode(paac_context->handle, NULL, NULL, NULL, NULL) != AACENC_OK) {
printf("Unable to initialize the encoder\n");
goto error;
}
4.调用aacEncInfo()获取关于当前编码器实例的信息。函数原型:
aacEncInfo(const HANDLE_AACENCODER hAacEncoder, AACENC_InfoStruct *pInfo)
部分代码示例:
if (aacEncInfo(paac_context->handle, &info) != AACENC_OK) {
printf("Unable to get the encoder info\n");
goto error;
}
编码部分:
4.在循环中对输入音频数据进行编码。并为aacEncEncode()提供输入/输出参数。将输出数据写入文件或音频设备。
5.调用aacEncClose()并销毁编码器实例。
部分代码示例:
if ((aacEncEncode(paac_context->handle, &in_buf, &out_buf, &in_args, &out_args)) != AACENC_OK) {
printf("Encoding aac failed\n");
return -2;
}
if (out_args.numOutBytes == 0)
{
printf("Encoding aac out size is 0\n");
return -3;
}
fwrite(m_aacOutbuf, 1, out_args.numOutBytes, outstream);
aacEncClose(&paac_context->handle);
我写的示例程序:
https://download.csdn.net/download/caiwenbin1101517886/10669383