1、下载faac源代码:http://downloads.sourceforge.net/faac/faac-1.28.zip

2、在VAWARE上进行交叉编译,安装。

./configure --target=arm-linux --host=arm-hisiv300-linux
    make
    make install

之后默认安装在/usr/locol下,头文件faac.h在/usr/locol/include下,静态库libfaac.a在/usr/locol/lib下,/usr/locol/bin下有faac的可执行文件。

3、创建头文件pcm2aac.h

#ifndef PCM_TO_AAC_H
#define PCM_TO_AAC_H

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

#include <faac.h>
#include <stdio.h>

typedef unsigned long   ULONG;
typedef unsigned int    UINT;
typedef unsigned char   BYTE;
typedef char            _TCHAR;
//-----------------------------------------------------------------------------
ULONG nSampleRate;
UINT nChannels;
UINT nPCMBitSize;
ULONG nInputSamples;
ULONG nMaxOutputBytes;

int nRet;
faacEncHandle hEncoder;
faacEncConfigurationPtr pConfiguration;

int nBytesRead;
int nPCMBufferSize;
int iPcmBytes;
BYTE* pbPCMBuffer;
BYTE* pbAACBuffer;
FILE *fhaac;//写aac文件流句柄
//----------------------------------------------------------------------------
//extern "C" int pcm2aac_init(void);
//-----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif // PCM_TO_AAC_H

4、在sample_audio.c中添加如下代码
#include "pcm2aac.h"
HI_BOOL pcm2aac_init(void)
{
    nSampleRate = AUDIO_SAMPLE_RATE_8000;  //
    nChannels = 1;         //pcm编码时选择MONO,这里就要nChannels = 1
    nPCMBitSize = 16;      // λ
    nInputSamples = 0;
    iPcmBytes = 0;//pcm帧计数器,每16帧处理一次,转换成5帧aac
    nMaxOutputBytes = 0;

nRet = 0;
    hEncoder = NULL;
    pConfiguration = NULL;

nBytesRead = -1;
    nPCMBufferSize = -1;
    pbPCMBuffer = NULL;
    pbAACBuffer = NULL;

hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);
    if(hEncoder == NULL)
    {
        printf("[ERROR] Failed to call faacEncOpen()\n");
        return HI_FALSE;
    }
    printf("----------nSampleRate=%d, nChannels=%d, nInputSamples=%d, nMaxOutputBytes=%d",nSampleRate, nChannels, nInputSamples, nMaxOutputBytes);

nPCMBufferSize = nInputSamples * nPCMBitSize / 8;
    pbPCMBuffer = (BYTE *)malloc(nPCMBufferSize*2);
    pbAACBuffer = (BYTE *)malloc(nMaxOutputBytes);
    if(pbPCMBuffer == NULL || pbAACBuffer == NULL)
    {
        printf("----------[ERROR] Failed to call malloc(pbPCMBuffer pbAACBuffer) \n");
    }
    memset(pbPCMBuffer,0,nPCMBufferSize*2);
    memset(pbAACBuffer,0,nMaxOutputBytes);

// (2.1) Get current encoding configuration
    pConfiguration = faacEncGetCurrentConfiguration(hEncoder);
    pConfiguration->inputFormat = FAAC_INPUT_16BIT;
    pConfiguration->outputFormat = 1;//0 Raw;1 ATDS
    pConfiguration->aacObjectType = 2;//LC编码

// (2.2) Set encoding configuration
    nRet = faacEncSetConfiguration(hEncoder, pConfiguration);
    if(nRet < 0)
    {
        printf("----------[ERROR] Failed to call faacEncSetConfiguration()\n");
        return HI_FALSE;
    }

fhaac = fopen("audio_chn0.aac","w+");
    if (NULL == fhaac)
    {
        printf("----------[ERROR] Failed to open file audio_chn0.aac \n");
        return HI_FALSE;
    }
    return HI_TRUE;

}
HI_BOOL pcm2aac_exit(void)
{
    nRet = faacEncClose(hEncoder);

free(pbPCMBuffer);
    free(pbAACBuffer);
    fclose(fhaac);
    //fclose(fhmsg);

return HI_TRUE;
}

。。。。
。。。。
。。。。

在SAMPLE_COMM_AUDIO_AencProc函数里添加下面的代码(这里需要说明一下,我把mpp/sample/common下的所有c代码都加到了sample_audio.c中,合成了一个文件)

/* save audio stream to file */
fwrite(stStream.pStream,1,stStream.u32Len, pstAencCtl->pfd);//在这一句之后添加:

//转码成aac
            memcpy(&pbPCMBuffer[iPcmBytes],stStream.pStream,stStream.u32Len);//pcm流数据保存到转换缓冲区
            iPcmBytes +=stStream.u32Len;
            if(iPcmBytes >= nPCMBufferSize)
            {               
                nRet = faacEncEncode(hEncoder, (int *) pbPCMBuffer, nInputSamples, pbAACBuffer, nMaxOutputBytes);//nInputSamples音频片数量
                memcpy(pbPCMBuffer,&pbPCMBuffer[nPCMBufferSize],nPCMBufferSize);//后半部分拷贝到前半部分
                iPcmBytes -= nPCMBufferSize;//未处理数据指针复位
                fwrite(pbAACBuffer, 1, nRet, fhaac);
                //fprintf(fhmsg,"nInputSamples=%d nRet=%d nMaxOutputBytes=%d \r\n",nInputSamples,nRet,nMaxOutputBytes);
                //转码并写aac文件................................结束
            }

在main函数里添加:
//在SAMPLE_AUDIO_Usage(); 这一句代码之前添加:
//初始化aac环境
    if(pcm2aac_init() == HI_FALSE)
    {
        printf("--------[ERORR]: pcm2aac_init() failed \n");
        return HI_FAILURE;
    }
   
在main函数最后,“SAMPLE_COMM_SYS_Exit();”这一行之后添加:
//aac环境退出
    printf("----------Begin exit pcm2aac........");
    pcm2aac_exit();

至此,编译运行程序sample_audio,选1,软件录音到文件audio_ch0.pcm的同时,生成audio_ch0.aac文件,可在vlc中播放。

不足之处:faac转码时CPU占用高达99%,效率低。

pcm2aac的更多相关文章

随机推荐

  1. CodeForces:#448 div2 a Pizza Separation

    传送门:http://codeforces.com/contest/895/problem/A A. Pizza Separation time limit per test1 second memo ...

  2. 动态规划:POJ2576-Tug of War(二维费用的背包问题)

    Tug of War Time Limit: 3000MS Memory Limit: 65536K Description A tug of war is to be arranged at the ...

  3. [BZOJ1208]宠物收养所(Splay)

    Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...

  4. 新手用WPF山寨QQ管家7.6(三)

    由于一直忙工作,没有更新完博客,更可恨的是...在清理资料的时候不小心删除了之前自己做的各种效果的DEMO....好在项目中用到了大部分,也算有所保留,以后可不敢随便删东西了....太可怕了! 在 新 ...

  5. MVC中Spring.net 对基类控制器无效 过滤器控制器无效

    比如现在我又一个BaseController作为基类控制器,用于过滤权限.登录判断等作用,其它控制由原本的继承Controller,改为继承BaseController.然后BaseControlle ...

  6. 【Trapping Rain Water】cpp

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  7. leetcode 【 Container With Most Water 】python 实现

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  8. 想进BAT?这些面试题助你一臂之力

    1 软性热身题 这种题目,考的就是你的软性能力,比如表达能力,理解能力,协调能力,一个词概括就是套路.这类题目会在面试开始热身的时候,问一道两题,不会多,但是如果你能回答的有条不紊,清晰达意,那么就会 ...

  9. 使用 htaccess 重写 url,隐藏查询字符串

    例如我们有如下 URL: http://example.com/users.php?name=tania 但是我们想要让 URL 变成如下: http://example.com/users/tani ...

  10. icpc南昌邀请赛 比赛总结

    上周末,我参加了icpc南昌区域赛邀请赛,这也是我的第一次外出参赛. 星期五晚上,在6个小时的火车和1个小时的公交后,我们终于抵达了江西师范大学,这次的比赛场地.江西师范大学周围的设施很齐全,各种烧烤 ...