27 #include <fdk-aac/aacdecoder_lib.h>
28 #include <fdk-aac/aacenc_lib.h>
30 #include "dsp_fdk_impl.h"
39 static const char* enc_err_str(AACENC_ERROR err)
45 case AACENC_INVALID_HANDLE:
46 return "AACENC_INVALID_HANDLE";
47 case AACENC_MEMORY_ERROR:
48 return "AACENC_MEMORY_ERROR";
49 case AACENC_UNSUPPORTED_PARAMETER:
50 return "AACENC_UNSUPPORTED_PARAMETER";
51 case AACENC_INVALID_CONFIG:
52 return "AACENC_INVALID_CONFIG";
53 case AACENC_INIT_ERROR:
54 return "AACENC_INIT_ERROR";
55 case AACENC_INIT_AAC_ERROR:
56 return "AACENC_INIT_AAC_ERROR";
57 case AACENC_INIT_SBR_ERROR:
58 return "AACENC_INIT_SBR_ERROR";
59 case AACENC_INIT_TP_ERROR:
60 return "AACENC_INIT_TP_ERROR";
61 case AACENC_INIT_META_ERROR:
62 return "AACENC_INIT_META_ERROR";
63 #ifdef AACENC_INIT_MPS_ERROR
64 case AACENC_INIT_MPS_ERROR:
65 return "AACENC_INIT_MPS_ERROR";
67 case AACENC_ENCODE_ERROR:
68 return "AACENC_ENCODE_ERROR";
69 case AACENC_ENCODE_EOF:
70 return "AACENC_ENCODE_EOF";
72 return "AACENC_UNKNOWN";
76 static const char* dec_err_str(AAC_DECODER_ERROR err)
82 case AAC_DEC_OUT_OF_MEMORY:
83 return "AAC_DEC_OUT_OF_MEMORY";
85 return "AAC_DEC_UNKNOWN";
86 case aac_dec_sync_error_start:
87 return "aac_dec_sync_error_start";
88 case AAC_DEC_TRANSPORT_SYNC_ERROR:
89 return "AAC_DEC_TRANSPORT_SYNC_ERROR";
90 case AAC_DEC_NOT_ENOUGH_BITS:
91 return "AAC_DEC_NOT_ENOUGH_BITS";
92 case aac_dec_sync_error_end:
93 return "aac_dec_sync_error_end";
94 case aac_dec_init_error_start:
95 return "aac_dec_init_error_start";
96 case AAC_DEC_INVALID_HANDLE:
97 return "AAC_DEC_INVALID_HANDLE";
98 case AAC_DEC_UNSUPPORTED_FORMAT:
99 return "AAC_DEC_UNSUPPORTED_FORMAT";
100 case AAC_DEC_UNSUPPORTED_ER_FORMAT:
101 return "AAC_DEC_UNSUPPORTED_ER_FORMAT";
102 case AAC_DEC_UNSUPPORTED_EPCONFIG:
103 return "AAC_DEC_UNSUPPORTED_EPCONFIG";
104 case AAC_DEC_UNSUPPORTED_MULTILAYER:
105 return "AAC_DEC_UNSUPPORTED_MULTILAYER";
106 case AAC_DEC_UNSUPPORTED_CHANNELCONFIG:
107 return "AAC_DEC_UNSUPPORTED_CHANNELCONFIG";
108 case AAC_DEC_UNSUPPORTED_SAMPLINGRATE:
109 return "AAC_DEC_UNSUPPORTED_SAMPLINGRATE";
110 case AAC_DEC_INVALID_SBR_CONFIG:
111 return "AAC_DEC_INVALID_SBR_CONFIG";
112 case AAC_DEC_SET_PARAM_FAIL:
113 return "AAC_DEC_SET_PARAM_FAIL";
114 case AAC_DEC_NEED_TO_RESTART:
115 return "AAC_DEC_NEED_TO_RESTART";
116 case AAC_DEC_OUTPUT_BUFFER_TOO_SMALL:
117 return "AAC_DEC_OUTPUT_BUFFER_TOO_SMALL";
118 case aac_dec_init_error_end:
119 return "aac_dec_init_error_end";
120 case aac_dec_decode_error_start:
121 return "aac_dec_decode_error_start";
122 case AAC_DEC_TRANSPORT_ERROR:
123 return "AAC_DEC_TRANSPORT_ERROR";
124 case AAC_DEC_PARSE_ERROR:
125 return "AAC_DEC_PARSE_ERROR";
126 case AAC_DEC_UNSUPPORTED_EXTENSION_PAYLOAD:
127 return "AAC_DEC_UNSUPPORTED_EXTENSION_PAYLOAD";
128 case AAC_DEC_DECODE_FRAME_ERROR:
129 return "AAC_DEC_DECODE_FRAME_ERROR";
130 case AAC_DEC_CRC_ERROR:
131 return "AAC_DEC_CRC_ERROR";
132 case AAC_DEC_INVALID_CODE_BOOK:
133 return "AAC_DEC_INVALID_CODE_BOOK";
134 case AAC_DEC_UNSUPPORTED_PREDICTION:
135 return "AAC_DEC_UNSUPPORTED_PREDICTION";
136 case AAC_DEC_UNSUPPORTED_CCE:
137 return "AAC_DEC_UNSUPPORTED_CCE";
138 case AAC_DEC_UNSUPPORTED_LFE:
139 return "AAC_DEC_UNSUPPORTED_LFE";
140 case AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA:
141 return "AAC_DEC_UNSUPPORTED_GAIN_CONTROL_DATA";
142 case AAC_DEC_UNSUPPORTED_SBA:
143 return "AAC_DEC_UNSUPPORTED_SBA";
144 case AAC_DEC_TNS_READ_ERROR:
145 return "AAC_DEC_TNS_READ_ERROR";
146 case AAC_DEC_RVLC_ERROR:
147 return "AAC_DEC_RVLC_ERROR";
148 case aac_dec_decode_error_end:
149 return "aac_dec_decode_error_end";
150 case aac_dec_anc_data_error_start:
151 return "aac_dec_anc_data_error_start";
152 case AAC_DEC_ANC_DATA_ERROR:
153 return "AAC_DEC_ANC_DATA_ERROR";
154 case AAC_DEC_TOO_SMALL_ANC_BUFFER:
155 return "AAC_DEC_TOO_SMALL_ANC_BUFFER";
156 case AAC_DEC_TOO_MANY_ANC_ELEMENTS:
157 return "AAC_DEC_TOO_MANY_ANC_ELEMENTS";
158 case aac_dec_anc_data_error_end:
159 return "aac_dec_anc_data_error_end";
161 return "AAC_DEC unknown value";
165 static void log_dec_info(
const CStreamInfo* info,
void (*log)(
const char* fmt, ...))
171 "aacSampleRate: %d, "
175 "pChannelIndices: %p, "
176 "aacSampleRate: %d, "
179 "channelConfig: %d, "
181 "aacSamplesPerFrame: %d, "
182 "aacNumChannels: %d, "
184 "extSamplingRate: %d, "
188 "numLostAccessUnits: %d, "
189 "numTotalBytes: %" PRIu64
", "
190 "numBadBytes: %" PRIu64
", "
191 "numTotalAccessUnits: %" PRIu64
", "
192 "numBadAccessUnits: %" PRIu64
", "
193 "drcProgRefLev: %d, "
195 info->aacSampleRate, info->frameSize, info->numChannels, info->pChannelType,
196 info->pChannelIndices, info->aacSampleRate, info->profile, info->aot, info->channelConfig,
197 info->bitRate, info->aacSamplesPerFrame, info->aacNumChannels, info->extAot,
198 info->extSamplingRate, info->outputDelay, info->flags, (
int)info->epConfig,
199 info->numLostAccessUnits,
201 info->numTotalBytes, info->numBadBytes, info->numTotalAccessUnits, info->numBadAccessUnits,
203 (
int)info->drcProgRefLev, (
int)info->drcPresMode);
206 static void log_enc_info(
const AACENC_InfoStruct* info, fdk_log_fkt_t log)
208 char confBuf[1024] = { 0 };
214 size_t remain =
sizeof(confBuf) - 1;
215 int rc = snprintf(confBuf, remain,
"{");
218 offset += (size_t)rc;
220 for (
size_t x = 0; x < 64; x++)
222 rc = snprintf(&confBuf[offset], remain - offset,
"0x%02x%s", (
int)info->confBuf[x],
223 (x > 0) ?
", " :
"");
228 rc = snprintf(confBuf, remain - offset,
"}");
234 "maxOutBufBytes : %u, "
236 "inBufFillLevel : %u, "
237 "inputChannels : %u, "
245 info->maxOutBufBytes, info->maxAncBytes, info->inBufFillLevel, info->inputChannels,
248 info->nDelay, info->nDelayCore,
250 confBuf, info->confSize);
253 static const char* aac_enc_param_str(AACENC_PARAM param)
260 return "AACENC_BITRATE";
261 case AACENC_BITRATEMODE:
262 return "AACENC_BITRATEMODE";
263 case AACENC_SAMPLERATE:
264 return "AACENC_SAMPLERATE";
265 case AACENC_SBR_MODE:
266 return "AACENC_SBR_MODE";
267 case AACENC_GRANULE_LENGTH:
268 return "AACENC_GRANULE_LENGTH";
269 case AACENC_CHANNELMODE:
270 return "AACENC_CHANNELMODE";
271 case AACENC_CHANNELORDER:
272 return "AACENC_CHANNELORDER";
273 case AACENC_SBR_RATIO:
274 return "AACENC_SBR_RATIO";
275 case AACENC_AFTERBURNER:
276 return "AACENC_AFTERBURNER";
277 case AACENC_BANDWIDTH:
278 return "AACENC_BANDWIDTH";
279 case AACENC_PEAK_BITRATE:
280 return "AACENC_PEAK_BITRATE";
281 case AACENC_TRANSMUX:
282 return "AACENC_TRANSMUX";
283 case AACENC_HEADER_PERIOD:
284 return "AACENC_HEADER_PERIOD";
285 case AACENC_SIGNALING_MODE:
286 return "AACENC_SIGNALING_MODE";
287 case AACENC_TPSUBFRAMES:
288 return "AACENC_TPSUBFRAMES";
289 case AACENC_AUDIOMUXVER:
290 return "AACENC_AUDIOMUXVER";
291 case AACENC_PROTECTION:
292 return "AACENC_PROTECTION";
293 case AACENC_ANCILLARY_BITRATE:
294 return "AACENC_ANCILLARY_BITRATE";
295 case AACENC_METADATA_MODE:
296 return "AACENC_METADATA_MODE";
297 case AACENC_CONTROL_STATE:
298 return "AACENC_CONTROL_STATE";
300 return "AACENC_UNKNOWN";
304 int fdk_aac_dsp_impl_init(
void** handle,
int encoder, fdk_log_fkt_t log)
311 HANDLE_AACENCODER* h = (HANDLE_AACENCODER*)handle;
312 AACENC_ERROR err = aacEncOpen(h, 0, 0);
313 if (err != AACENC_OK)
315 log(WLOG_ERROR,
"aacEncOpen failed with %s", enc_err_str(err));
321 HANDLE_AACDECODER* h = (HANDLE_AACDECODER*)handle;
324 *h = aacDecoder_Open(TT_MP4_RAW, 1);
327 log(WLOG_ERROR,
"aacDecoder_Open failed");
334 void fdk_aac_dsp_impl_uninit(
void** handle,
int encoder, fdk_log_fkt_t log)
341 HANDLE_AACENCODER* h = (HANDLE_AACENCODER*)handle;
342 AACENC_ERROR err = aacEncClose(h);
343 if (err != AACENC_OK)
344 log(WLOG_ERROR,
"aacEncClose failed with %s", enc_err_str(err));
348 HANDLE_AACDECODER* h = (HANDLE_AACDECODER*)handle;
350 aacDecoder_Close(*h);
356 ssize_t fdk_aac_dsp_impl_decode_read(
void* handle,
void* dst,
size_t dstSize, fdk_log_fkt_t log)
359 assert((dstSize /
sizeof(INT_PCM)) <= INT_MAX);
361 const INT nrsamples = (INT)(dstSize /
sizeof(INT_PCM));
363 HANDLE_AACDECODER
self = (HANDLE_AACDECODER)handle;
364 AAC_DECODER_ERROR err = aacDecoder_DecodeFrame(
self, dst, nrsamples, flags);
368 return fdk_aac_dsp_impl_stream_info(handle, 0, log);
369 case AAC_DEC_NOT_ENOUGH_BITS:
372 log(WLOG_ERROR,
"aacDecoder_DecodeFrame failed with %s", dec_err_str(err));
377 static unsigned get_channelmode(
unsigned channels)
394 return MODE_1_2_2_2_1;
397 return MODE_7_1_BACK;
405 int fdk_aac_dsp_impl_config(
void* handle,
size_t* pbuffersize,
int encoder,
unsigned samplerate,
406 unsigned channels,
unsigned bytes_per_second,
407 unsigned frames_per_packet, fdk_log_fkt_t log)
414 "fdk_aac_dsp_impl_config: samplerate: %ld, channels: %ld, bytes_pers_second: %ld",
415 samplerate, channels, bytes_per_second);
423 const struct t_param_pair params[] = { { AACENC_AOT, 2 },
424 { AACENC_SAMPLERATE, samplerate },
425 { AACENC_CHANNELMODE, get_channelmode(channels) },
426 { AACENC_CHANNELORDER, 0 },
427 { AACENC_BITRATE, bytes_per_second * 8 },
428 { AACENC_TRANSMUX, 0 },
429 { AACENC_AFTERBURNER, 1 } };
430 HANDLE_AACENCODER
self = NULL;
432 self = (HANDLE_AACENCODER)handle;
435 AACENC_ERROR err = aacEncOpen(&
self, 0, channels);
436 if (err != AACENC_OK)
438 log(WLOG_ERROR,
"aacEncOpen failed with %s", enc_err_str(err));
443 for (
size_t x = 0; x <
sizeof(params) /
sizeof(params[0]); x++)
445 const struct t_param_pair* param = ¶ms[x];
447 AACENC_ERROR err = aacEncoder_SetParam(
self, param->param, param->value);
448 if (err != AACENC_OK)
450 log(WLOG_ERROR,
"aacEncoder_SetParam(%s, %d) failed with %s",
451 aac_enc_param_str(param->param), param->value, enc_err_str(err));
456 AACENC_ERROR err = aacEncEncode(
self, NULL, NULL, NULL, NULL);
457 if (err != AACENC_OK)
459 log(WLOG_ERROR,
"aacEncEncode failed with %s", enc_err_str(err));
463 AACENC_InfoStruct info = { 0 };
464 err = aacEncInfo(
self, &info);
465 if (err != AACENC_OK)
467 log(WLOG_ERROR,
"aacEncInfo failed with %s", enc_err_str(err));
473 *pbuffersize = info.maxOutBufBytes;
474 log_enc_info(&info, log);
479 err = aacEncClose(&
self);
480 if (err != AACENC_OK)
481 log(WLOG_WARN,
"aacEncClose failed with %s", enc_err_str(err));
483 *pbuffersize = info.frameLength * info.inputChannels *
sizeof(INT_PCM);
485 HANDLE_AACDECODER aacdec = (HANDLE_AACDECODER)handle;
487 UCHAR* asc[] = { info.confBuf };
488 UINT ascSize[] = { info.confSize };
492 AAC_DECODER_ERROR decerr = aacDecoder_ConfigRaw(aacdec, asc, ascSize);
493 if (decerr != AAC_DEC_OK)
495 log(WLOG_ERROR,
"aacDecoder_ConfigRaw failed with %s", dec_err_str(decerr));
502 ssize_t fdk_aac_dsp_impl_decode_fill(
void* handle,
const void* data,
size_t size, fdk_log_fkt_t log)
507 UINT leftBytes = size;
508 HANDLE_AACDECODER
self = (HANDLE_AACDECODER)handle;
516 UCHAR* pBuffer[] = { cnv.puc };
517 const UINT bufferSize[] = { size };
520 assert(data || (size == 0));
522 AAC_DECODER_ERROR err = aacDecoder_Fill(
self, pBuffer, bufferSize, &leftBytes);
523 if (err != AAC_DEC_OK)
525 log(WLOG_ERROR,
"aacDecoder_Fill failed with %s", dec_err_str(err));
531 ssize_t fdk_aac_dsp_impl_stream_info(
void* handle,
int encoder, fdk_log_fkt_t log)
538 AACENC_InfoStruct info = { 0 };
539 HANDLE_AACENCODER
self = (HANDLE_AACENCODER)handle;
540 AACENC_ERROR err = aacEncInfo(
self, &info);
541 if (err != AACENC_OK)
543 log(WLOG_ERROR,
"aacEncInfo failed with %s", enc_err_str(err));
546 return info.maxOutBufBytes;
550 HANDLE_AACDECODER
self = (HANDLE_AACDECODER)handle;
551 CStreamInfo* info = aacDecoder_GetStreamInfo(
self);
554 log(WLOG_ERROR,
"aacDecoder_GetStreamInfo failed");
558 return sizeof(INT_PCM) * info->numChannels * info->frameSize;
562 ssize_t fdk_aac_dsp_impl_encode(
void* handle,
const void* data,
size_t size,
void* dst,
563 size_t dstSize, fdk_log_fkt_t log)
565 INT inSizes[] = { size };
566 INT inElSizes[] = {
sizeof(INT_PCM) };
567 INT inIdentifiers[] = { IN_AUDIO_DATA };
574 void* inBuffers[] = { cnv.pv };
576 const AACENC_BufDesc inBufDesc = {
579 .bufferIdentifiers = inIdentifiers,
581 .bufElSizes = inElSizes
584 INT outSizes[] = { dstSize };
585 INT outElSizes[] = { 1 };
586 INT outIdentifiers[] = { OUT_BITSTREAM_DATA };
587 void* outBuffers[] = { dst };
588 const AACENC_BufDesc outBufDesc = { .numBufs = 1,
590 .bufferIdentifiers = outIdentifiers,
591 .bufSizes = outSizes,
592 .bufElSizes = outElSizes };
594 const AACENC_InArgs inArgs = { .numInSamples =
595 size /
sizeof(INT_PCM),
597 AACENC_OutArgs outArgs = { 0 };
599 HANDLE_AACENCODER
self = (HANDLE_AACENCODER)handle;
604 AACENC_ERROR err = aacEncEncode(
self, &inBufDesc, &outBufDesc, &inArgs, &outArgs);
605 if (err != AACENC_OK)
607 log(WLOG_ERROR,
"aacEncEncode failed with %s", enc_err_str(err));
610 return outArgs.numOutBytes;