faac编解码移植和优化

1、下载、编译和安装过程:

wget http://downloads.sourceforge.net/faac/faac-1.28.tar.gz
tar zxvf faac-1.28.tar.gz
cd faac-1.28
./configure
make
sudo make install

2、aac配置

1. 打开faac编码器引擎。
   faacEncHandle FAACAPI faacEncOpen(   
        unsigned long sampleRate,      // pcm音频采样率,8k,16k,44100等
        unsigned int numChannels,      // pcm音频通道,mono = 1 / stereo = 2 
        unsigned long *inputSamples,   // 一次输入的样本数
        unsigned long *maxOutputBytes);// 输出aac buffer的最大size   
        函数调用成功会return一个编码器faacEncHandle,同时确定输入样本数和输出aac buffer最大size;

    申请输入buffer及输出buffer
    int     nPCMBufferSize = inputSamples * nPCMBitSize / 8; //nPCMBitSize 单次样本位数
    unsinged char*   pbPCMBuffer = new BYTE[nPCMBufferSize];
    unsigned char*   pbAACBuffer = new BYTE[maxOutputBytes];


2. 获取当前编码器配置。
   faacEncConfigurationPtr FAACAPI faacEncGetCurrentConfiguration( 
        faacEncHandle hEncoder); //编码器handle
    函数调用成功会返回一个faacEncConfigurationPtr用来查看及设置编码器配置。

3. 修改当前编码器的配置并设置。
   //copy this struct from headfile
   typedef struct faacEncConfiguration{
    /* config version - 配置版本,可以默认不设置*/ 
    int version;
    /* library version - 库版本,可以默认不设置*/     
    char *name;
    /* copyright string - 版权,可以默认不设置*/   
    char *copyright;       
    /* MPEG version, 2 or 4 - MPEG版本,MPEG2 or MPEG4*/ 
    unsigned int mpegVersion;
    /* AAC object type - AAC对象类型,详细见补充说明1,取值:1-MAIN 2-LOW 3-SSR 4-LTP*/
    unsigned int aacObjectType;
    /* Allow mid/side coding - 是否允许mid/side coding, 详细见补充说明2,取值:0-NO 1-YES*/
    unsigned int allowMidside;
    /* Use one of the channels as LFE channel - 是否允许一个通道为低频通道,取值:0-NO 1-YES*/
    /* LFE(low-frequencyeffects) */
    unsigned int useLfe;
    /* Use Temporal Noise Shaping - 瞬时噪声定形(temporal noise shaping,TNS)滤波器,取值:0-NO 1-YES*/
    unsigned int useTns;
    /* bitrate / channel of AAC file - AAC文件的bitrate / channel 取值:0和48000都可以,暂时不清楚这个参数作用*/
    unsigned long bitRate;
    /* AAC file frequency bandwidth - 频宽 取值:0, 32000,64000都可以,暂时不清楚参数作用*/
    unsigned int bandWidth;
    /* Quantizer quality - 编码质量,取值:efault=100 LOWER<100 HIGHER>100*/
    /* 默认100,值越大音质越高 */
    unsigned long quantqual;
    /* Bitstream output format (取值:0 = Raw; 1 = ADTS) - 输出数据类型(是否包包含adts头),录制MP4文件时,要用raw流,ADTS详细见补充说明3*/
    unsigned int outputFormat;
    /* psychoacoustic model list*/
    psymodellist_t *psymodellist;
    /* selected index in psymodellist*/
    unsigned int psymodelidx;
    /*
    PCM Sample Input Format  - 输入pcm数据类型
    0 FAAC_INPUT_NULL    invalid, signifies a misconfigured config
    1 FAAC_INPUT_16BIT native endian 16bit
    2 FAAC_INPUT_24BIT native endian 24bit in 24 bits(not implemented)
    3 FAAC_INPUT_32BIT native endian 24bit in 32 bits (DEFAULT)
    4 FAAC_INPUT_FLOAT 32bit floating point
    */
    unsigned int inputFormat;
    /* block type enforcing - 
     * (SHORTCTL_NORMAL/SHORTCTL_NOSHORT/SHORTCTL_NOLONG) 
     */
    int shortctl;
    /*
        Channel Remapping
        Default         0, 1, 2, 3 ... 63  (64 is MAX_CHANNELS in coder.h)
        WAVE 4.0        2, 0, 1, 3
        WAVE 5.0        2, 0, 1, 3, 4
        WAVE 5.1        2, 0, 1, 4, 5, 3
        AIFF 5.1        2, 0, 3, 1, 4, 5 
    */
    int channel_map[64];    
} faacEncConfiguration, *faacEncConfigurationPtr;   

参数设置示例:
第一步:    
    faacEncConfigurationPtr pConfiguration;
    pConfiguration->outputFormat = 1;
    pConfiguration->aacObjectType = LOW;
    pConfiguration->bitRate = 48000;    // or 0
    pConfiguration->bandWidth = 64000;  //or 0 or 32000
    pConfiguration->inputFormat = FAAC_INPUT_16BIT;

    /*下面可以选择设置*/
    pConfiguration->allowMidside = 1;
    pConfiguration->useLfe = 0;
    pConfiguration->useTns = 0;        
    pConfiguration->quantqual = 100;
    pConfiguration->outputFormat = 1;
    pConfiguration->shortctl = SHORTCTL_NORMAL;  

第二步:
    int FAACAPI faacEncSetConfiguration( //设置编码器配置
        faacEncHandle hEncoder,
        faacEncConfigurationPtr config);

4.进行编码操作(PCM to AAC)
    /* 请见步骤1中这部分
    int nPCMBufferSize = inputSamples * nPCMBitSize / 8;
    unsinged char*   pbPCMBuffer = new BYTE[nPCMBufferSize];
    unsigned char*   pbAACBuffer = new BYTE[maxOutputBytes];
    */
    先获取PCM数据,填充到pbPCMBuffer,单次获取长度为nPCMBufferSize。

    int FAACAPI faacEncEncode(
        faacEncHandle hEncoder,
        int32_t * inputBuffer,       //pcm输入buffer, pbPCMBuffer 
        unsigned int samplesInput,   //一次输入的样本数(注意不是数据长度 ),samplesInput
        unsigned char *outputBuffer, //AAC输出buffer, pbAACBuffer 
        unsigned int bufferSize);
        函数调用成功会返回实际AAC数据大小,从pbAACBuffer中读出即可。

5. 结束关闭编码器退出。
    int FAACAPI faacEncClose(faacEncHandle hEncoder);

    tips:释放new出来的缓冲区。

3、优化

在不修改源码的情况下,faac的内存占用非常高,每路音频在13M左右。如果多路音频的话,内存将很快耗尽。

搜索MAX_CHANNELS的定义,默认是6 和64,全部改成1(一般都是单声道)。

重新编译,运行,内存占用降为2.5M左右。

编译faac库
cd faac-1.28
make clean
#./configure CXX=arm-hisiv300-linux-g++ CC=arm-hisiv300-linux-gcc --host=arm-linux --with-mp4v2=no 
./configure CXX=arm-hisiv300-linux-g++ CC=arm-hisiv300-linux-gcc --host=arm-linux --with-mp4v2=no CFLAGS="-mfpu=vfp -mfloat-abi=softfp" CXXFLAGS="-mfpu=vfp -mfloat-abi=softfp"
 
make
make install

编译业务源码
OBJ_BIN=./aac_lib_v300/faac_v300
SRC_FILE=faac_test.cpp
arm-hisiv300-linux-gcc $SRC_FILE -o $OBJ_BIN -L/usr/local/lib -lfaac -I/usr/local/include -lsupc++ -mfpu=vfp -mfloat-abi=softfp

4、注意事项

 1)PCM规格,pcm_s16be(motorola PCM)   pcm_s16le(intel PCM), 两者区别在于高低位,测试faac直接编码pcm_s16le ok,不需要转。
 2)faacEncEncode()编码函数中的第三个参数是一次输入的样本数samplesInput,不是第二个参数输入buffer的实际大小,而是通过faacEncOpen()获取的。
 3)Faac是free的,但是音频格式AAC是需要授权的。

猜你喜欢

转载自blog.csdn.net/qinglongzhan/article/details/81315532