开源Faac实现PCM编码AAC

来源:互联网 发布:淘宝店怎么免费推广 编辑:程序博客网 时间:2024/06/05 02:57

目的:Faac实现PCM编码AAC

环境:

系统:Windows 环境:VS2015 64bit

步骤:

1. 下载及编译Faac

1. 从官网(http://www.audiocoding.com/downloads.html)下载Faac最新版本。2. 解压zip文件以后直接进入文件夹libfaac,在里面找到libfaac_dll.sln,点击启动vs并升级项目,切换到release模式,build即可生成windows下for vs的动态库。ps: 其实可以直接把源码里面的faac的.c文件和include 拷贝到项目中,直接加载使用。

2. 使用Faac步骤

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出来的缓冲区。补充说明:1. 1997年,AAC第一次出现在标准MPEG-2 Part 7,(ISO/IEC 13818-7:1997)。和视频CODEC标准类似,AAC在MPEG-2 Part 7就有三个profiles他们分别是。l Low-Complexity profile (AAC-LC / LC-AAC)l Main profile (AAC Main)l Scalable Sampling Rate profile (AAC-SSR)从此可知AAC-LC出现最早,所以AAC-LC的应用最广泛,兼容性最好。1999年, AAC从原有标准升级并且合入标准MPEG-4 Part 3(ISO/IEC 14496-3:1999)这次升级一个重要变化是引入Audio Object Types(AOT)并且把AOT概念合并到profiles中。这时profile也变成4个。l Main (which includes most of the MPEG-4 Audio Object Types)l Scalable (AAC LC, AAC LTP, CELP, HVXC, TwinVQ, Wavetable Synthesis, TTSI),l Speech (CELP, HVXC, TTSI)l Low Rate Synthesis (Wavetable Synthesis, TTSI)合成语音。 2. 在MPEG-2 AAC 系统中,M/S(Mid/Side) Stereo coding被提供在多声道信号中,每个声道对(channel pair)的组合,也就是每个通道对,是对称地排列在人耳听觉的左右两边,其方式简单,且对位串不会引起较显著的负担。 一般其在左右声道数据相似度大时常被用到,并需记载每一频带的四种能量临界组合,分别为左、右、左右声道音频合并(L+R)及相减(L-R)的两种新的能量。一般,若所转换的Sid声道的能量较小时,M/S Stereo coding 可以节省此通道的位数,而将多余的位应用于另一个所转换的声道,即Mid 声道,进而可提高此编码效率。3. 一般的AAC解码器都需要把AAC的ES流打包成ADTS的格式,一般是在AAC ES流前添加7个字节的ADTS header。也就是说你可以吧ADTS这个头看作是AAC的frameheader。ADTS 头中相对有用的信息 采样率、声道数、帧长度。每一个带ADTS头信息的AAC流会清晰的告送解码器他需要的这些信息。一般情况下ADTS的头信息都是7个字节

3. 注意事项

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

FaacDemo(来自转载)下载链接:http://download.csdn.net/detail/alger_magic/9671263

1 0
原创粉丝点击