AACEncoder.cpp 3.45 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * MIT License
xzl committed
3
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
xzl committed
25
 */
xiongziliang committed
26

xzl committed
27
#ifdef ENABLE_FAAC
xiongzilaing committed
28 29

#include <cstdlib>
xzl committed
30 31 32 33 34 35 36 37 38 39 40
#include "AACEncoder.h"
#include "Util/logger.h"

#ifdef __cplusplus
extern "C" {
#endif
#include <faac.h>
#ifdef __cplusplus
}
#endif

xiongziliang committed
41
using namespace toolkit;
xzl committed
42

xiongziliang committed
43
namespace mediakit {
xzl committed
44 45 46 47 48 49

AACEncoder::AACEncoder() {

}

AACEncoder::~AACEncoder() {
50 51 52
	if (_hEncoder != nullptr) {
		faacEncClose(_hEncoder);
		_hEncoder = nullptr;
xzl committed
53
	}
54 55 56
	if (_pucAacBuf != nullptr) {
		delete[] _pucAacBuf;
		_pucAacBuf = nullptr;
xzl committed
57
	}
58 59 60
	if (_pucPcmBuf != nullptr) {
		delete[] _pucPcmBuf;
		_pucPcmBuf = nullptr;
xzl committed
61 62 63 64 65 66 67 68
	}
}

bool AACEncoder::init(int iSampleRate, int iChannels, int iSampleBit) {
	if (iSampleBit != 16) {
		return false;
	}
	// (1) Open FAAC engine
69 70 71
	_hEncoder = faacEncOpen(iSampleRate, iChannels, &_ulInputSamples,
			&_ulMaxOutputBytes);
	if (_hEncoder == NULL) {
xzl committed
72 73
		return false;
	}
74 75 76
	_pucAacBuf = new unsigned char[_ulMaxOutputBytes];
	_ulMaxInputBytes = _ulInputSamples * iSampleBit / 8;
	_pucPcmBuf = new unsigned char[_ulMaxInputBytes * 4];
xzl committed
77 78

	// (2.1) Get current encoding configuration
79
	faacEncConfigurationPtr pConfiguration = faacEncGetCurrentConfiguration(_hEncoder);
xzl committed
80
	if (pConfiguration == NULL) {
81
		faacEncClose(_hEncoder);
xzl committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
		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
97
	if(!faacEncSetConfiguration(_hEncoder, pConfiguration)){
xzl committed
98
		ErrorL << "faacEncSetConfiguration failed";
99
		faacEncClose(_hEncoder);
xzl committed
100 101 102 103 104 105
		return false;
	}
	return true;
}

int AACEncoder::inputData(char *pcPcmBufr, int iLen, unsigned char **ppucOutBuffer) {
106 107 108
	memcpy(_pucPcmBuf + _uiPcmLen, pcPcmBufr, iLen);
	_uiPcmLen += iLen;
	if (_uiPcmLen < _ulMaxInputBytes) {
xzl committed
109 110 111
		return 0;
	}

112 113 114 115
	int nRet = faacEncEncode(_hEncoder, (int32_t *) (_pucPcmBuf), _ulInputSamples, _pucAacBuf, _ulMaxOutputBytes);
	_uiPcmLen -= _ulMaxInputBytes;
	memmove(_pucPcmBuf, _pucPcmBuf + _ulMaxInputBytes, _uiPcmLen);
	*ppucOutBuffer = _pucAacBuf;
xzl committed
116 117 118
	return nRet;
}

xiongziliang committed
119
} /* namespace mediakit */
xzl committed
120 121 122 123 124 125 126 127

#endif //ENABLE_FAAC