pcm2aac
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的更多相关文章
随机推荐
- oracle 事务 第一弹
一.事务概念 概念:在数据库中事务是工作的逻辑单元,一个事务是由一个或多个完成一组的相关行为的SQL语句组成,通过事务机制确保这一组SQL语句所作的操作要么完全成功执行,完成整个工作单元操作,要么一点 ...
- IIC简介(转载)
来自:https://www.cnblogs.com/zalebool/p/4214599.html IIC简介: IIC 即Inter-Integrated Circuit(集成电路总线),这种总线 ...
- 下载速度更加快的 SourceForge 镜像
http://www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/是 University of Kent的镜像, ...
- mysql同步故障解决
故障现象:Slave_SQL_Running: No Slave状态:mysql> show slave status\GSlave_IO_Running: YesSlave_SQL_Runni ...
- (转) Redis哨兵的详解
1 哨兵的作用 哨兵是redis集群架构中非常重要的一个组件,主要功能如下: 1. 集群监控:负责监控redis master和slave进程是否正常工作 2. 消息通知:如果某个redis实例有故障 ...
- Python虚拟机类机制之instance对象(六)
instance对象中的__dict__ 在Python虚拟机类机制之从class对象到instance对象(五)这一章中最后的属性访问算法中,我们看到“a.__dict__”这样的形式. # 首先寻 ...
- hadoop ha集群搭建
集群配置: jdk1.8.0_161 hadoop-2.6.1 zookeeper-3.4.8 linux系统环境:Centos6.5 3台主机:master.slave01.slave02 Hado ...
- php和js中数组的总结
php中数组的表示方法:array()或者[] js中数组的表示方法:new array()或者[] 一.php中初始化命名数组 在PHP中声明数组的方式主要有两种:一是应用array()函数声明 ...
- JS一个非常经典的问题:在遍历数组时对DOM监听事件,索引值将始终等于遍历结束后的值
一个简单的Tab选项卡点击事件. <style type="text/css"> ul{padding:0;margin:0;} .tab{width:400px;} ...
- Couchbase II( View And Index)
Couchbase II( View And Index) Views view的作用是从没有结构和半结构的数据对象中抽取过滤需要的信息,并生成相关的index信息,通常生成json数据. vie ...