faac 和fdk-aac是2个不同的音频转换库。
头文件:
#ifndef __PCM_TO_AAC__
#define __PCM_TO_AAC__
#include <faac.h>
#include <string>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
class PCM_TO_AAC
{
public:
PCM_TO_AAC();
~PCM_TO_AAC();
public:
bool Init(int nSamplerate/*采样率*/, int nChannel/*通道号*/, int nSampleBit/*采样位数*/);
public:
//成功--返回aac数据大小,小于等于0时视为失败
int ConvertProcess(char* pcmbuf/*待转化的PCM数据,该数据大小必须等于InputSample * 采样位数 / 8,否则转化会失败*/, int PcmSize/*待转化pcm数据大小,该数据大小必须等于InputSample * 采样位数 / 8*/, unsigned char* aacBuf/*aac数据缓存buf*/);
public:
int GetPerPcmPacketSize() { return m_PCMBufSize; };
private:
faacEncHandle m_hEncoder;
private:
unsigned long m_nMaxOutputBytes;
int m_nPCMSampleBit;
int m_PCMBufSize;
private:
char* m_PtrPCMBuf;
char* m_PtrAACBuf;
private:
faacEncConfigurationPtr m_pConfiguration;
};
#endif
源文件:
#include "PCMToAAC.h"
PCM_TO_AAC::PCM_TO_AAC()
{
}
PCM_TO_AAC::~PCM_TO_AAC()
{
if (m_hEncoder){
faacEncClose(m_hEncoder);
m_hEncoder = 0;
}
if (m_PtrPCMBuf) {
delete[] m_PtrPCMBuf;
m_PtrPCMBuf = NULL;
}
if (m_PtrAACBuf) {
delete[] m_PtrAACBuf;
m_PtrAACBuf = NULL;
}
}
bool PCM_TO_AAC::Init(int nSamplerate/*采样率*/, int nChannel/*通道号*/, int nSampleBit/*采样位数*/)
{
m_nMaxOutputBytes = 0;
m_nPCMSampleBit = nSampleBit;
//
unsigned long nInputSample = 0;
m_hEncoder = faacEncOpen(nSamplerate, nChannel, &nInputSample, &m_nMaxOutputBytes);
if (!m_hEncoder){
return false;
}
m_PCMBufSize = nInputSample * m_nPCMSampleBit / 8;
//
try {
m_PtrPCMBuf = new char[m_PCMBufSize];
}
catch (...) {}
//
try {
m_PtrAACBuf = new char[m_nMaxOutputBytes];
}
catch (...) {}
//
m_pConfiguration = faacEncGetCurrentConfiguration(m_hEncoder);
m_pConfiguration->inputFormat = FAAC_INPUT_16BIT;
m_pConfiguration->outputFormat = 1; /* 0--RAW 1--ADTS header */
m_pConfiguration->aacObjectType = LOW;
m_pConfiguration->allowMidside = 0;
m_pConfiguration->useTns = 1;
faacEncSetConfiguration(m_hEncoder, m_pConfiguration);
return true;
}
int PCM_TO_AAC::ConvertProcess(char* pcmbuf, int PcmSize, unsigned char* aacBuf)
{
unsigned int inputSample = PcmSize / (m_nPCMSampleBit / 8);
return faacEncEncode(m_hEncoder, (int*)pcmbuf, inputSample, aacBuf, m_nMaxOutputBytes);
}