128 lines
3.3 KiB
C++
128 lines
3.3 KiB
C++
/*
|
|
*******************************************************************************
|
|
* INCLUDE
|
|
*******************************************************************************
|
|
*/
|
|
#include "AudioEncoder.h"
|
|
#include "Logger.h"
|
|
#include "../core/System.h"
|
|
#include "fdk-aac/FDK_audio.h"
|
|
|
|
namespace Video
|
|
{
|
|
namespace Fdk
|
|
{
|
|
/*
|
|
*******************************************************************************
|
|
* CLASS DEFINE
|
|
*******************************************************************************
|
|
*/
|
|
AudioEncoder::AudioEncoder(Core::CodecID codecID)
|
|
: Core::AudioEncoder(Poco::format("Fdk::AudioEncoder(%s)", Core::codecIDToString(codecID)), codecID),
|
|
_pEncoder(nullptr)
|
|
{
|
|
}
|
|
|
|
AudioEncoder::~AudioEncoder()
|
|
{
|
|
}
|
|
|
|
void AudioEncoder::initialize()
|
|
{
|
|
setSampleFormat(Core::SampleFormat::S16);
|
|
|
|
int channels = Core::channelLayoutChannels(getChannelLayout());
|
|
int samplerate = getSampleRate();
|
|
|
|
if (aacEncOpen(&_pEncoder, 0, channels) != AACENC_OK)
|
|
{
|
|
logger().error("Unable to open _pEncoder");
|
|
return;
|
|
}
|
|
|
|
if (aacEncoder_SetParam(_pEncoder, AACENC_CHANNELMODE, (channels == 1) ? MODE_1 : MODE_2) != AACENC_OK)
|
|
{
|
|
logger().error("Unable to set the channel mode");
|
|
return;
|
|
}
|
|
|
|
if (aacEncoder_SetParam(_pEncoder, AACENC_AOT, 2) != AACENC_OK)
|
|
{
|
|
logger().error("Unable to set the AOT");
|
|
return;
|
|
}
|
|
|
|
if (aacEncoder_SetParam(_pEncoder, AACENC_SAMPLERATE, samplerate) != AACENC_OK)
|
|
{
|
|
logger().error("Unable to set the SAMPLERATE");
|
|
return;
|
|
}
|
|
|
|
if (aacEncoder_SetParam(_pEncoder, AACENC_BITRATE, getBitrate()) != AACENC_OK)
|
|
{
|
|
logger().error("Unable to set the BITRATE");
|
|
return;
|
|
}
|
|
}
|
|
|
|
void AudioEncoder::deinitialize()
|
|
{
|
|
if (_pEncoder)
|
|
{
|
|
aacEncClose(&_pEncoder);
|
|
_pEncoder = nullptr;
|
|
}
|
|
}
|
|
|
|
Core::Packet::Ptr AudioEncoder::encode()
|
|
{
|
|
if (!_pEncoder)
|
|
return nullptr;
|
|
|
|
auto pIn = readFrame();
|
|
|
|
AACENC_BufDesc inBuf = {0}, outBuf = {0};
|
|
AACENC_InArgs inArgs = {0};
|
|
AACENC_OutArgs outArgs = {0};
|
|
uint8_t* pBuffer = new uint8_t[2048];
|
|
|
|
int inIdentifier = IN_AUDIO_DATA;
|
|
int inSize = pIn->length;
|
|
int inElemSize = 2;
|
|
int outIdentifier = OUT_BITSTREAM_DATA;
|
|
int outEemSize = 1;
|
|
int outSize = 2048;
|
|
|
|
inBuf.numBufs = 1;
|
|
uint8_t* data = pIn->data[0].get();
|
|
inBuf.bufs = (void**)&data;
|
|
inBuf.bufferIdentifiers = &inIdentifier;
|
|
inBuf.bufSizes = &inSize;
|
|
inBuf.bufElSizes = &inElemSize;
|
|
|
|
outBuf.numBufs = 1;
|
|
outBuf.bufs = (void**)&pBuffer;
|
|
outBuf.bufferIdentifiers = &outIdentifier;
|
|
outBuf.bufSizes = &outSize;
|
|
outBuf.bufElSizes = &outEemSize;
|
|
|
|
inArgs.numInSamples = pIn->length / inElemSize;
|
|
|
|
inArgs.numAncBytes = 0;
|
|
|
|
if (AACENC_OK != aacEncEncode(_pEncoder, &inBuf, &outBuf, &inArgs, &outArgs))
|
|
{
|
|
delete[] pBuffer;
|
|
return nullptr;
|
|
}
|
|
else
|
|
return new Core::Packet(serialCodeManager().code(), 0, pBuffer, outArgs.numOutBytes, pIn->timestampEncode,
|
|
pIn->timestampEncode);
|
|
}
|
|
|
|
void AudioEncoder::onConfigChanged(ParameterNames& names)
|
|
{
|
|
Core::AudioEncoder::onConfigChanged(names);
|
|
}
|
|
}
|
|
}
|