作者:咕唧咕唧liukun321
来自:http://blog.csdn.net/liukun321
Advanced Audio Coding。一种专为声音数据设计的文件压缩格式,与Mp3不同,它採用了全新的算法进行编码。更加高效,具有更高的“性价比”。
利用AAC格式,可使人感觉声音质量没有明显减少的前提下。更加小巧。
FAAC是在嵌入式系统中经常使用的AAC音频编码开源库。关于AAC音频格式能够看一下这篇博文作简单了解:AAC音频编码格式简析
FAAC开源project源代码下载链接:FAAC源代码下载
得到FAACproject源代码后首先运行 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() "); return -1; } printf("nInputSamples = %d ",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 ",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; }