2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2019-05-08 15:40:07 +08:00
|
|
|
|
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
2017-09-27 16:20:30 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#ifdef ENABLE_FAAC
|
2017-04-25 11:35:41 +08:00
|
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "AACEncoder.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
#include <faac.h>
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
AACEncoder::AACEncoder() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AACEncoder::~AACEncoder() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_hEncoder != nullptr) {
|
|
|
|
|
faacEncClose(_hEncoder);
|
|
|
|
|
_hEncoder = nullptr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_pucAacBuf != nullptr) {
|
|
|
|
|
delete[] _pucAacBuf;
|
|
|
|
|
_pucAacBuf = nullptr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_pucPcmBuf != nullptr) {
|
|
|
|
|
delete[] _pucPcmBuf;
|
|
|
|
|
_pucPcmBuf = nullptr;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AACEncoder::init(int iSampleRate, int iChannels, int iSampleBit) {
|
|
|
|
|
if (iSampleBit != 16) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// (1) Open FAAC engine
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_hEncoder = faacEncOpen(iSampleRate, iChannels, &_ulInputSamples,
|
|
|
|
|
&_ulMaxOutputBytes);
|
|
|
|
|
if (_hEncoder == NULL) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_pucAacBuf = new unsigned char[_ulMaxOutputBytes];
|
|
|
|
|
_ulMaxInputBytes = _ulInputSamples * iSampleBit / 8;
|
|
|
|
|
_pucPcmBuf = new unsigned char[_ulMaxInputBytes * 4];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
// (2.1) Get current encoding configuration
|
2018-10-24 15:43:52 +08:00
|
|
|
|
faacEncConfigurationPtr pConfiguration = faacEncGetCurrentConfiguration(_hEncoder);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (pConfiguration == NULL) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
faacEncClose(_hEncoder);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
pConfiguration->aacObjectType =LOW;
|
|
|
|
|
pConfiguration->mpegVersion = 4;
|
|
|
|
|
pConfiguration->useTns = 1;
|
|
|
|
|
pConfiguration->shortctl = SHORTCTL_NORMAL;
|
|
|
|
|
pConfiguration->useLfe = 1;
|
|
|
|
|
pConfiguration->allowMidside = 1;
|
|
|
|
|
pConfiguration->bitRate = 0;
|
|
|
|
|
pConfiguration->bandWidth = 0;
|
|
|
|
|
pConfiguration->quantqual = 50;
|
|
|
|
|
pConfiguration->outputFormat = 1;
|
|
|
|
|
pConfiguration->inputFormat = FAAC_INPUT_16BIT;
|
|
|
|
|
|
|
|
|
|
// (2.2) Set encoding configuration
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(!faacEncSetConfiguration(_hEncoder, pConfiguration)){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
ErrorL << "faacEncSetConfiguration failed";
|
2018-10-24 15:43:52 +08:00
|
|
|
|
faacEncClose(_hEncoder);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AACEncoder::inputData(char *pcPcmBufr, int iLen, unsigned char **ppucOutBuffer) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
memcpy(_pucPcmBuf + _uiPcmLen, pcPcmBufr, iLen);
|
|
|
|
|
_uiPcmLen += iLen;
|
|
|
|
|
if (_uiPcmLen < _ulMaxInputBytes) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
int nRet = faacEncEncode(_hEncoder, (int32_t *) (_pucPcmBuf), _ulInputSamples, _pucAacBuf, _ulMaxOutputBytes);
|
|
|
|
|
_uiPcmLen -= _ulMaxInputBytes;
|
|
|
|
|
memmove(_pucPcmBuf, _pucPcmBuf + _ulMaxInputBytes, _uiPcmLen);
|
|
|
|
|
*ppucOutBuffer = _pucAacBuf;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
return nRet;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#endif //ENABLE_FAAC
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|