4 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
7 #ifndef FAAD_INPUT_BUFFER_SIZE
8 #define FAAD_INPUT_BUFFER_SIZE 1024*2
12 #ifndef FAAD_UNDERFLOW_LIMIT
13 #define FAAD_UNDERFLOW_LIMIT 500
42 unsigned long cap = NeAACDecGetCapabilities();
45 if (!cap & FIXED_POINT_CAP) {
51 hAac = NeAACDecOpen();
54 conf = NeAACDecGetCurrentConfiguration(hAac);
57 conf->outputFormat = FAAD_FMT_16BIT;
60 conf->downMatrix =
false;
61 conf->useOldADTSFormat =
false;
62 conf->dontUpSampleImplicitSBR =
false;
65 if (!NeAACDecSetConfiguration(hAac, conf)) {
66 LOGE(
"NeAACDecSetConfiguration");
71 if (input_buffer.size() != buffer_size_input){
72 input_buffer.resize(buffer_size_input);
82 if (hAac !=
nullptr) {
89 size_t write(
const uint8_t *data,
size_t len) {
91 size_t result = input_buffer.
writeArray((uint8_t *)data, len);
93 decode(underflow_limit);
104 buffer_size_input = len;
109 underflow_limit = len;
113 virtual operator bool() {
return hAac !=
nullptr; }
116 int buffer_size_input = FAAD_INPUT_BUFFER_SIZE;
117 int underflow_limit = FAAD_UNDERFLOW_LIMIT;
118 NeAACDecHandle hAac =
nullptr;
119 NeAACDecConfigurationPtr conf;
121 bool is_init =
false;
123 void init(uint8_t *data,
size_t len) {
127 unsigned char channels = info.
channels;
129 if (NeAACDecInit(hAac, data, len, &samplerate, &channels)==-1) {
130 LOGE(
"NeAACDecInit");
137 void decode(
int minBufferSize) {
139 NeAACDecFrameInfo hInfo;
142 while (input_buffer.
available()>minBufferSize) {
146 init(input_buffer.
data(), eff_len);
149 uint8_t *sample_buffer=(uint8_t *)NeAACDecDecode(hAac, &hInfo, input_buffer.
address(), eff_len);
151 LOGD(
"bytesconsumed: %d of %d", (
int)hInfo.bytesconsumed, (
int)eff_len);
152 if (hInfo.error != 0) {
153 LOGW(
"Decoding error: %s", NeAACDecGetErrorMessage(hInfo.error));
156 if (hInfo.bytesconsumed == 0 ) {
160 LOGD(
"Decoded %lu samples", hInfo.samples);
161 LOGD(
" bytesconsumed: %lu", hInfo.bytesconsumed);
162 LOGD(
" channels: %d", hInfo.channels);
163 LOGD(
" samplerate: %lu", hInfo.samplerate);
164 LOGD(
" sbr: %u", hInfo.sbr);
165 LOGD(
" object_type: %u", hInfo.object_type);
166 LOGD(
" header_type: %u", hInfo.header_type);
167 LOGD(
" num_front_channels: %u", hInfo.num_front_channels);
168 LOGD(
" num_side_channels: %u", hInfo.num_side_channels);
169 LOGD(
" num_back_channels: %u", hInfo.num_back_channels);
170 LOGD(
" num_lfe_channels: %u", hInfo.num_lfe_channels);
171 LOGD(
" ps: %u", hInfo.ps);
177 AudioInfo tmp{(sample_rate_t)hInfo.samplerate, hInfo.channels, 16};
182 int bytes = hInfo.samples *
sizeof(int16_t);
183 size_t len = p_print->write(sample_buffer, bytes);