pcm2aac

一、下載faac源代碼:http://downloads.sourceforge.net/faac/faac-1.28.ziplinux

二、在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的可執行文件。.net

三、建立頭文件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

四、在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();code

至此,編譯運行程序sample_audio,選1,軟件錄音到文件audio_ch0.pcm的同時,生成audio_ch0.aac文件,可在vlc中播放。orm

不足之處:faac轉碼時CPU佔用高達99%,效率低。ip

相關文章
相關標籤/搜索