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. php下关于Cannot use a scalar value as an array的解决办法

    今天在测试php程序的时候,出现了一个错误提示:Cannot use a scalar value as an array,这个错误提示前几天也出过,当时好像稍微调了一下就好了,也没深究,今天却又出现 ...

  2. 爬虫进阶之Selenium和chromedriver,动态网页(Ajax)数据抓取

    什么是Ajax: Ajax(Asynchronouse JavaScript And XML)异步JavaScript和XML.过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新.这意 ...

  3. Spark架构与作业执行流程简介(scala版)

    在讲spark之前,不得不详细介绍一下RDD(Resilient Distributed Dataset),打开RDD的源码,一开始的介绍如此: 字面意思就是弹性分布式数据集,是spark中最基本的数 ...

  4. 4 Values whose Sum is 0 POJ - 2785

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 29243   Accep ...

  5. BFS:HDU2054-A==B?(字符串的比较)

    A == B ? Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. [NOIP2015]运输计划(树上差分+LCA+二分)

    Description 公元 2044 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n−1 条双向航道,每条航道建立在两个星球之间,这 n−1 条航道连通了 L 国的所有星球. 小 P 掌管 ...

  7. Selenium与PhantomJS【转】

    爬虫(Spider),反爬虫(Anti-Spider),反反爬虫(Anti-Anti-Spider) 之间恢宏壮阔的斗争... Day 1 小莫想要某站上所有的电影,写了标准的爬虫(基于HttpCli ...

  8. C# Params的应用

    为了将方法声明为可以接受可变数量参数的方法,我们可以使用params关键字来声明数组,如下所示: public static Int32Add(params Int32[] values) { Int ...

  9. Careercup - Microsoft面试题 - 24313662

    2014-05-12 07:27 题目链接 原题: Convert a number to a number 题目:把二进制数转化成四进制数. 解法:四是二的倍数,所以两位变一位就可以了. 代码: / ...

  10. [转]核函数K(kernel function)

    1 核函数K(kernel function)定义 核函数K(kernel function)就是指K(x, y) = <f(x), f(y)>,其中x和y是n维的输入值,f(·) 是从n ...