作者:咕唧咕唧liukun321
來自:http://blog.csdn.net/liukun321
Advanced Audio Coding。一種專爲聲音數據設計的文件壓縮格式,與Mp3不同,它採用了全新的算法進行編碼,更加高效,具有更高的“性價比”。利用AAC格式,可使人感覺聲音質量沒有明顯降低的前提下,更加小巧。
FAAC是在嵌入式系統中常用的AAC音頻編碼開源庫,關於AAC音頻格式可以看一下這篇博文作簡單瞭解:AAC音頻編碼格式簡析
FAAC開源工程源碼下載鏈接:FAAC源碼下載
得到FAAC工程源碼後首先執行 configure獲得Makefile,並指定目標平臺和交叉工具鏈
./configure--target=arm-linux--host=arm-none-linux-gnueabi
編譯:
make
安裝:
make install
最終會在指定安裝目錄獲得如下動態及靜態庫:
libfaac.a
libfaac.la
libfaac.so
libfaac.so.0
libfaac.so.0.0.0
將獲得的動態鏈接庫放入開發板/usr/lib目錄即可
下面順帶附上一個將PCM 16bit 原始音頻數據編碼成AAC格式音頻數據的C++類,下面的代碼是從一個項目中抽取的,沒有單獨測試,僅做參考:
class AudioProcess {
public:
AudioProcess (void)
{
nSampleRate = RATE; // 採樣率
nChannels = CHANNELS; // 聲道數
nPCMBitSize = SIZE;
nInputSamples = 0;
nMaxOutputBytes = 0;
AACDecoderInitFlag = 0;
DecoderHandle = 0;
ADTSFrameInBuf = NULL;
PCMData = NULL;
ppBuffer = NULL;
}// var init
~AudioProcess(void)
{
}// var init
private:
ULONG nInputSamples ;
ULONG nMaxOutputBytes ;
faacEncHandle hEncoder;
faacEncConfigurationPtr pConfiguration;
BYTE* pbAACBuffer;
int nRet;
public:
int OutAACLength;
ULONG nSampleRate; // 採樣率
UINT nChannels; // 聲道數
UINT nPCMBitSize;
unsigned char* ppBuffer;
unsigned long pSizeOfDecoderSpecificInfo;
int nBytesRead;
int nPCMBufferSize;
int nAACBufferSize;
BYTE* pbPCMBuffer;
BYTE* OutAACBuffer;
public:
int AACEncoderInit();
int AACEncoding();
int AACEncoderDestory();
};
int AudioProcess ::AACEncoderInit()
{
hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);
if(hEncoder == NULL)
{
printf("[ERROR] Failed to call faacEncOpen()\n");
return -1;
}
printf("nInputSamples = %d\n",nInputSamples);
nPCMBufferSize = nInputSamples * nPCMBitSize / 8;
pbPCMBuffer = new BYTE [nPCMBufferSize];
pbAACBuffer = new BYTE [nMaxOutputBytes];
// Get current encoding configuration
pConfiguration = faacEncGetCurrentConfiguration(hEncoder);
pConfiguration->inputFormat = FAAC_INPUT_16BIT;//_16BIT;
pConfiguration->mpegVersion = MPEG4;
pConfiguration->version = MPEG4; // 1
pConfiguration->outputFormat =1;// ADTS_STREAM;
pConfiguration->aacObjectType = 2;//LOW;
pConfiguration->useTns = 0;//DEFAULT_TNS;
pConfiguration->shortctl = 0;//SHORTCTL_NORMAL;
pConfiguration->allowMidside = 1 ;
// Set encoding configuration
nRet = faacEncSetConfiguration(hEncoder, pConfiguration);
faacEncGetDecoderSpecificInfo(hEncoder,&(ppBuffer), &(pSizeOfDecoderSpecificInfo));
}
int AudioProcess ::AACEncoding()
{
// 輸入樣本數,用實際讀入字節數計算,一般只有讀到文件尾時纔不是 //nPCMBufferSize/(nPCMBitSize/8);
nBytesRead = length;
nInputSamples = nBytesRead / (nPCMBitSize / 8);
printf("nInputSamples = %d\n",nInputSamples);
//Encode
nRet = faacEncEncode(hEncoder, (int*) pbPCMBuffer, nInputSamples, pbAACBuffer,nMaxOutputBytes);
OutAACBuffer = pbAACBuffer;
OutAACLength = nRet;
return nRet;
}
void AudioProcess::AACEncoderDestroy()
{
nRet = faacEncClose(hEncoder);
delete[] pbPCMBuffer;
delete[] pbAACBuffer;
}