1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4 © Copyright 1995 - 2019 Fraunhofer-Gesellschaft zur Förderung der angewandten
5 Forschung e.V. All rights reserved.
6
7 1. INTRODUCTION
8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
11 a wide variety of Android devices.
12
13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
14 general perceptual audio codecs. AAC-ELD is considered the best-performing
15 full-bandwidth communications codec by independent studies and is widely
16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
17 specifications.
18
19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
20 those of Fraunhofer) may be obtained through Via Licensing
21 (www.vialicensing.com) or through the respective patent owners individually for
22 the purpose of encoding or decoding bit streams in products that are compliant
23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
24 Android devices already license these patent claims through Via Licensing or
25 directly from the patent owners, and therefore FDK AAC Codec software may
26 already be covered under those patent licenses when it is used for those
27 licensed purposes only.
28
29 Commercially-licensed AAC software libraries, including floating-point versions
30 with enhanced sound quality, are also available from Fraunhofer. Users are
31 encouraged to check the Fraunhofer website for additional applications
32 information and documentation.
33
34 2. COPYRIGHT LICENSE
35
36 Redistribution and use in source and binary forms, with or without modification,
37 are permitted without payment of copyright license fees provided that you
38 satisfy the following conditions:
39
40 You must retain the complete text of this software license in redistributions of
41 the FDK AAC Codec or your modifications thereto in source code form.
42
43 You must retain the complete text of this software license in the documentation
44 and/or other materials provided with redistributions of the FDK AAC Codec or
45 your modifications thereto in binary form. You must make available free of
46 charge copies of the complete source code of the FDK AAC Codec and your
47 modifications thereto to recipients of copies in binary form.
48
49 The name of Fraunhofer may not be used to endorse or promote products derived
50 from this library without prior written permission.
51
52 You may not charge copyright license fees for anyone to use, copy or distribute
53 the FDK AAC Codec software or your modifications thereto.
54
55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
56 that you changed the software and the date of any change. For modified versions
57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
59 AAC Codec Library for Android."
60
61 3. NO PATENT LICENSE
62
63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
65 Fraunhofer provides no warranty of patent non-infringement with respect to this
66 software.
67
68 You may use this FDK AAC Codec software or modifications thereto only for
69 purposes that are authorized by appropriate patent licenses.
70
71 4. DISCLAIMER
72
73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
75 including but not limited to the implied warranties of merchantability and
76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
78 or consequential damages, including but not limited to procurement of substitute
79 goods or services; loss of use, data, or profits, or business interruption,
80 however caused and on any theory of liability, whether in contract, strict
81 liability, or tort (including negligence), arising in any way out of the use of
82 this software, even if advised of the possibility of such damage.
83
84 5. CONTACT INFORMATION
85
86 Fraunhofer Institute for Integrated Circuits IIS
87 Attention: Audio and Multimedia Departments - FDK AAC LL
88 Am Wolfsmantel 33
89 91058 Erlangen, Germany
90
91 www.iis.fraunhofer.de/amm
92 amm-info@iis.fraunhofer.de
93 ----------------------------------------------------------------------------- */
94
95 /**************************** AAC encoder library ******************************
96
97 Author(s): M. Lohwasser
98
99 Description: FDK HE-AAC Encoder interface library functions
100
101 *******************************************************************************/
102
103 #include "aacenc_lib.h"
104 #include "FDK_audio.h"
105 #include "aacenc.h"
106
107 #include "aacEnc_ram.h"
108 #include "FDK_core.h" /* FDK_tools versioning info */
109
110 /* Encoder library info */
111 #define AACENCODER_LIB_VL0 4
112 #define AACENCODER_LIB_VL1 0
113 #define AACENCODER_LIB_VL2 0
114 #define AACENCODER_LIB_TITLE "AAC Encoder"
115 #ifdef __ANDROID__
116 #define AACENCODER_LIB_BUILD_DATE ""
117 #define AACENCODER_LIB_BUILD_TIME ""
118 #else
119 #define AACENCODER_LIB_BUILD_DATE __DATE__
120 #define AACENCODER_LIB_BUILD_TIME __TIME__
121 #endif
122
123 #include "pcm_utils.h"
124
125 #include "sbr_encoder.h"
126 #include "../src/sbrenc_ram.h"
127 #include "channel_map.h"
128
129 #include "psy_const.h"
130 #include "bitenc.h"
131
132 #include "tpenc_lib.h"
133
134 #include "metadata_main.h"
135 #include "mps_main.h"
136 #include "sacenc_lib.h"
137
138 #define SBL(fl) \
139 (fl / \
140 8) /*!< Short block length (hardcoded to 8 short blocks per long block) */
141 #define BSLA(fl) \
142 (4 * SBL(fl) + SBL(fl) / 2) /*!< AAC block switching look-ahead */
143 #define DELAY_AAC(fl) (fl + BSLA(fl)) /*!< MDCT + blockswitching */
144 #define DELAY_AACLD(fl) (fl) /*!< MDCT delay (no framing delay included) */
145 #define DELAY_AACELD(fl) \
146 ((fl) / 2) /*!< ELD FB delay (no framing delay included) */
147
148 #define MAX_DS_DELAY (100) /*!< Maximum downsampler delay in SBR. */
149 #define INPUTBUFFER_SIZE \
150 (2 * (1024) + MAX_DS_DELAY + 1537) /*!< Audio input samples + downsampler \
151 delay + sbr/aac delay compensation */
152
153 #define DEFAULT_HEADER_PERIOD_REPETITION_RATE \
154 10 /*!< Default header repetition rate used in transport library and for SBR \
155 header. */
156
157 ////////////////////////////////////////////////////////////////////////////////////
158 /**
159 * Flags to characterize encoder modules to be supported in present instance.
160 */
161 enum {
162 ENC_MODE_FLAG_AAC = 0x0001,
163 ENC_MODE_FLAG_SBR = 0x0002,
164 ENC_MODE_FLAG_PS = 0x0004,
165 ENC_MODE_FLAG_SAC = 0x0008,
166 ENC_MODE_FLAG_META = 0x0010
167 };
168
169 ////////////////////////////////////////////////////////////////////////////////////
170 typedef struct {
171 AUDIO_OBJECT_TYPE userAOT; /*!< Audio Object Type. */
172 UINT userSamplerate; /*!< Sampling frequency. */
173 UINT nChannels; /*!< will be set via channelMode. */
174 CHANNEL_MODE userChannelMode;
175 UINT userBitrate;
176 UINT userBitrateMode;
177 UINT userBandwidth;
178 UINT userAfterburner;
179 UINT userFramelength;
180 UINT userAncDataRate;
181 UINT userPeakBitrate;
182
183 UCHAR userTns; /*!< Use TNS coding. */
184 UCHAR userPns; /*!< Use PNS coding. */
185 UCHAR userIntensity; /*!< Use Intensity coding. */
186
187 TRANSPORT_TYPE userTpType; /*!< Transport type */
188 UCHAR userTpSignaling; /*!< Extension AOT signaling mode. */
189 UCHAR userTpNsubFrames; /*!< Number of sub frames in a transport frame for
190 LOAS/LATM or ADTS (default 1). */
191 UCHAR userTpAmxv; /*!< AudioMuxVersion to be used for LATM (default 0). */
192 UCHAR userTpProtection;
193 UCHAR userTpHeaderPeriod; /*!< Parameter used to configure LATM/LOAS SMC rate.
194 Moreover this parameters is used to configure
195 repetition rate of PCE in raw_data_block. */
196
197 UCHAR userErTools; /*!< Use VCB11, HCR and/or RVLC ER tool. */
198 UINT userPceAdditions; /*!< Configure additional bits in PCE. */
199
200 UCHAR userMetaDataMode; /*!< Meta data library configuration. */
201
202 UCHAR userSbrEnabled; /*!< Enable SBR for ELD. */
203 UINT userSbrRatio; /*!< SBR sampling rate ratio. Dual- or single-rate. */
204
205 UINT userDownscaleFactor;
206
207 } USER_PARAM;
208
209 /**
210 * SBR extenxion payload struct provides buffers to be filled in SBR encoder
211 * library.
212 */
213 typedef struct {
214 UCHAR data[(1)][(8)][MAX_PAYLOAD_SIZE]; /*!< extension payload data buffer */
215 UINT dataSize[(1)][(8)]; /*!< extension payload data size in bits */
216 } SBRENC_EXT_PAYLOAD;
217
218 ////////////////////////////////////////////////////////////////////////////////////
219
220 /****************************************************************************
221 Structure Definitions
222 ****************************************************************************/
223
224 typedef struct AACENC_CONFIG *HANDLE_AACENC_CONFIG;
225
226 struct AACENCODER {
227 USER_PARAM extParam;
228 CODER_CONFIG coderConfig;
229
230 /* AAC */
231 AACENC_CONFIG aacConfig;
232 HANDLE_AAC_ENC hAacEnc;
233
234 /* SBR */
235 HANDLE_SBR_ENCODER hEnvEnc; /* SBR encoder */
236 SBRENC_EXT_PAYLOAD *pSbrPayload; /* SBR extension payload */
237
238 /* Meta Data */
239 HANDLE_FDK_METADATA_ENCODER hMetadataEnc;
240 INT metaDataAllowed; /* Signal whether chosen configuration allows metadata.
241 Necessary for delay compensation. Metadata mode is a
242 separate parameter. */
243
244 HANDLE_MPS_ENCODER hMpsEnc;
245
246 /* Transport */
247 HANDLE_TRANSPORTENC hTpEnc;
248
249 INT_PCM
250 *inputBuffer; /* Internal input buffer. Input source for AAC encoder */
251 UCHAR *outBuffer; /* Internal bitstream buffer */
252
253 INT inputBufferSize; /* Size of internal input buffer */
254 INT inputBufferSizePerChannel; /* Size of internal input buffer per channel */
255 INT outBufferInBytes; /* Size of internal bitstream buffer*/
256
257 INT inputBufferOffset; /* Where to write new input samples. */
258
259 INT nSamplesToRead; /* number of input samples neeeded for encoding one frame
260 */
261 INT nSamplesRead; /* number of input samples already in input buffer */
262 INT nZerosAppended; /* appended zeros at end of file*/
263 INT nDelay; /* codec delay */
264 INT nDelayCore; /* codec delay, w/o the SBR decoder delay */
265
266 AACENC_EXT_PAYLOAD extPayload[MAX_TOTAL_EXT_PAYLOADS];
267
268 ULONG InitFlags; /* internal status to treggier re-initialization */
269
270 /* Memory allocation info. */
271 INT nMaxAacElements;
272 INT nMaxAacChannels;
273 INT nMaxSbrElements;
274 INT nMaxSbrChannels;
275
276 UINT encoder_modis;
277
278 /* Capability flags */
279 UINT CAPF_tpEnc;
280 };
281
282 typedef struct {
283 /* input */
284 ULONG nChannels; /*!< Number of audio channels. */
285 ULONG samplingRate; /*!< Encoder output sampling rate. */
286 ULONG bitrateRange; /*!< Lower bitrate range for config entry. */
287
288 /* output*/
289 UCHAR sbrMode; /*!< 0: ELD sbr off,
290 1: ELD with downsampled sbr,
291 2: ELD with dualrate sbr. */
292 CHANNEL_MODE chMode; /*!< Channel mode. */
293
294 } ELD_SBR_CONFIGURATOR;
295
296 /**
297 * \brief This table defines ELD/SBR default configurations.
298 */
299 static const ELD_SBR_CONFIGURATOR eldSbrAutoConfigTab[] = {
300 {1, 48000, 0, 2, MODE_1}, {1, 48000, 64000, 0, MODE_1},
301
302 {1, 44100, 0, 2, MODE_1}, {1, 44100, 64000, 0, MODE_1},
303
304 {1, 32000, 0, 2, MODE_1}, {1, 32000, 28000, 1, MODE_1},
305 {1, 32000, 56000, 0, MODE_1},
306
307 {1, 24000, 0, 1, MODE_1}, {1, 24000, 40000, 0, MODE_1},
308
309 {1, 16000, 0, 1, MODE_1}, {1, 16000, 28000, 0, MODE_1},
310
311 {1, 15999, 0, 0, MODE_1},
312
313 {2, 48000, 0, 2, MODE_2}, {2, 48000, 44000, 2, MODE_2},
314 {2, 48000, 128000, 0, MODE_2},
315
316 {2, 44100, 0, 2, MODE_2}, {2, 44100, 44000, 2, MODE_2},
317 {2, 44100, 128000, 0, MODE_2},
318
319 {2, 32000, 0, 2, MODE_2}, {2, 32000, 32000, 2, MODE_2},
320 {2, 32000, 68000, 1, MODE_2}, {2, 32000, 96000, 0, MODE_2},
321
322 {2, 24000, 0, 1, MODE_2}, {2, 24000, 48000, 1, MODE_2},
323 {2, 24000, 80000, 0, MODE_2},
324
325 {2, 16000, 0, 1, MODE_2}, {2, 16000, 32000, 1, MODE_2},
326 {2, 16000, 64000, 0, MODE_2},
327
328 {2, 15999, 0, 0, MODE_2}
329
330 };
331
332 /*
333 * \brief Configure SBR for ELD configuration.
334 *
335 * This function finds default SBR configuration for ELD based on number of
336 * channels, sampling rate and bitrate.
337 *
338 * \param nChannels Number of audio channels.
339 * \param samplingRate Audio signal sampling rate.
340 * \param bitrate Encoder bitrate.
341 *
342 * \return - pointer to eld sbr configuration.
343 * - NULL, on failure.
344 */
eldSbrConfigurator(const ULONG nChannels,const ULONG samplingRate,const ULONG bitrate)345 static const ELD_SBR_CONFIGURATOR *eldSbrConfigurator(const ULONG nChannels,
346 const ULONG samplingRate,
347 const ULONG bitrate) {
348 int i;
349 const ELD_SBR_CONFIGURATOR *pSetup = NULL;
350
351 for (i = 0;
352 i < (int)(sizeof(eldSbrAutoConfigTab) / sizeof(ELD_SBR_CONFIGURATOR));
353 i++) {
354 if ((nChannels == eldSbrAutoConfigTab[i].nChannels) &&
355 (samplingRate <= eldSbrAutoConfigTab[i].samplingRate) &&
356 (bitrate >= eldSbrAutoConfigTab[i].bitrateRange)) {
357 pSetup = &eldSbrAutoConfigTab[i];
358 }
359 }
360
361 return pSetup;
362 }
363
isSbrActive(const HANDLE_AACENC_CONFIG hAacConfig)364 static inline INT isSbrActive(const HANDLE_AACENC_CONFIG hAacConfig) {
365 INT sbrUsed = 0;
366
367 /* Note: Even if implicit signalling was selected, The AOT itself here is not
368 * AOT_AAC_LC */
369 if ((hAacConfig->audioObjectType == AOT_SBR) ||
370 (hAacConfig->audioObjectType == AOT_PS) ||
371 (hAacConfig->audioObjectType == AOT_MP2_SBR)) {
372 sbrUsed = 1;
373 }
374 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD &&
375 (hAacConfig->syntaxFlags & AC_SBR_PRESENT)) {
376 sbrUsed = 1;
377 }
378
379 return (sbrUsed);
380 }
381
isPsActive(const AUDIO_OBJECT_TYPE audioObjectType)382 static inline INT isPsActive(const AUDIO_OBJECT_TYPE audioObjectType) {
383 INT psUsed = 0;
384
385 if (audioObjectType == AOT_PS) {
386 psUsed = 1;
387 }
388
389 return (psUsed);
390 }
391
GetCoreChannelMode(const CHANNEL_MODE channelMode,const AUDIO_OBJECT_TYPE audioObjectType)392 static CHANNEL_MODE GetCoreChannelMode(
393 const CHANNEL_MODE channelMode, const AUDIO_OBJECT_TYPE audioObjectType) {
394 CHANNEL_MODE mappedChannelMode = channelMode;
395 if ((isPsActive(audioObjectType) && (channelMode == MODE_2)) ||
396 (channelMode == MODE_212)) {
397 mappedChannelMode = MODE_1;
398 }
399 return mappedChannelMode;
400 }
401
getSbrSignalingMode(const AUDIO_OBJECT_TYPE audioObjectType,const TRANSPORT_TYPE transportType,const UCHAR transportSignaling,const UINT sbrRatio)402 static SBR_PS_SIGNALING getSbrSignalingMode(
403 const AUDIO_OBJECT_TYPE audioObjectType, const TRANSPORT_TYPE transportType,
404 const UCHAR transportSignaling, const UINT sbrRatio)
405
406 {
407 SBR_PS_SIGNALING sbrSignaling;
408
409 if (transportType == TT_UNKNOWN || sbrRatio == 0) {
410 sbrSignaling = SIG_UNKNOWN; /* Needed parameters have not been set */
411 return sbrSignaling;
412 } else {
413 sbrSignaling =
414 SIG_EXPLICIT_HIERARCHICAL; /* default: explicit hierarchical signaling
415 */
416 }
417
418 if ((audioObjectType == AOT_AAC_LC) || (audioObjectType == AOT_SBR) ||
419 (audioObjectType == AOT_PS) || (audioObjectType == AOT_MP2_AAC_LC) ||
420 (audioObjectType == AOT_MP2_SBR)) {
421 switch (transportType) {
422 case TT_MP4_ADIF:
423 case TT_MP4_ADTS:
424 sbrSignaling = SIG_IMPLICIT; /* For MPEG-2 transport types, only
425 implicit signaling is possible */
426 break;
427
428 case TT_MP4_RAW:
429 case TT_MP4_LATM_MCP1:
430 case TT_MP4_LATM_MCP0:
431 case TT_MP4_LOAS:
432 default:
433 if (transportSignaling == 0xFF) {
434 /* Defaults */
435 sbrSignaling = SIG_EXPLICIT_HIERARCHICAL;
436 } else {
437 /* User set parameters */
438 /* Attention: Backward compatible explicit signaling does only work
439 * with AMV1 for LATM/LOAS */
440 sbrSignaling = (SBR_PS_SIGNALING)transportSignaling;
441 }
442 break;
443 }
444 }
445
446 return sbrSignaling;
447 }
448
449 /****************************************************************************
450 Allocate Encoder
451 ****************************************************************************/
452
H_ALLOC_MEM(_AacEncoder,AACENCODER)453 H_ALLOC_MEM(_AacEncoder, AACENCODER)
454 C_ALLOC_MEM(_AacEncoder, struct AACENCODER, 1)
455
456 /*
457 * Map Encoder specific config structures to CODER_CONFIG.
458 */
459 static void FDKaacEnc_MapConfig(CODER_CONFIG *const cc,
460 const USER_PARAM *const extCfg,
461 const SBR_PS_SIGNALING sbrSignaling,
462 const HANDLE_AACENC_CONFIG hAacConfig) {
463 AUDIO_OBJECT_TYPE transport_AOT = AOT_NULL_OBJECT;
464 FDKmemclear(cc, sizeof(CODER_CONFIG));
465
466 cc->flags = 0;
467
468 cc->samplesPerFrame = hAacConfig->framelength;
469 cc->samplingRate = hAacConfig->sampleRate;
470 cc->extSamplingRate = extCfg->userSamplerate;
471
472 /* Map virtual aot to transport aot. */
473 switch (hAacConfig->audioObjectType) {
474 case AOT_MP2_AAC_LC:
475 transport_AOT = AOT_AAC_LC;
476 break;
477 case AOT_MP2_SBR:
478 transport_AOT = AOT_SBR;
479 cc->flags |= CC_SBR;
480 break;
481 default:
482 transport_AOT = hAacConfig->audioObjectType;
483 }
484
485 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) {
486 cc->flags |= (hAacConfig->syntaxFlags & AC_SBR_PRESENT) ? CC_SBR : 0;
487 cc->flags |= (hAacConfig->syntaxFlags & AC_LD_MPS) ? CC_SAC : 0;
488 }
489
490 /* transport type is usually AAC-LC. */
491 if ((transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS)) {
492 cc->aot = AOT_AAC_LC;
493 } else {
494 cc->aot = transport_AOT;
495 }
496
497 /* Configure extension aot. */
498 if (sbrSignaling == SIG_IMPLICIT) {
499 cc->extAOT = AOT_NULL_OBJECT; /* implicit */
500 } else {
501 if ((sbrSignaling == SIG_EXPLICIT_BW_COMPATIBLE) &&
502 ((transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS))) {
503 cc->extAOT = AOT_SBR; /* explicit backward compatible */
504 } else {
505 cc->extAOT = transport_AOT; /* explicit hierarchical */
506 }
507 }
508
509 if ((transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS)) {
510 cc->sbrPresent = 1;
511 if (transport_AOT == AOT_PS) {
512 cc->psPresent = 1;
513 }
514 }
515 cc->sbrSignaling = sbrSignaling;
516
517 if (hAacConfig->downscaleFactor > 1) {
518 cc->downscaleSamplingRate = cc->samplingRate;
519 cc->samplingRate *= hAacConfig->downscaleFactor;
520 cc->extSamplingRate *= hAacConfig->downscaleFactor;
521 }
522
523 cc->bitRate = hAacConfig->bitRate;
524 cc->noChannels = hAacConfig->nChannels;
525 cc->flags |= CC_IS_BASELAYER;
526 cc->channelMode = hAacConfig->channelMode;
527
528 cc->nSubFrames = (hAacConfig->nSubFrames > 1 && extCfg->userTpNsubFrames == 1)
529 ? hAacConfig->nSubFrames
530 : extCfg->userTpNsubFrames;
531
532 cc->flags |= (extCfg->userTpProtection) ? CC_PROTECTION : 0;
533
534 if (extCfg->userTpHeaderPeriod != 0xFF) {
535 cc->headerPeriod = extCfg->userTpHeaderPeriod;
536 } else { /* auto-mode */
537 switch (extCfg->userTpType) {
538 case TT_MP4_ADTS:
539 case TT_MP4_LOAS:
540 case TT_MP4_LATM_MCP1:
541 cc->headerPeriod = DEFAULT_HEADER_PERIOD_REPETITION_RATE;
542 break;
543 default:
544 cc->headerPeriod = 0;
545 }
546 }
547
548 /* Mpeg-4 signaling for transport library. */
549 switch (hAacConfig->audioObjectType) {
550 case AOT_MP2_AAC_LC:
551 case AOT_MP2_SBR:
552 cc->flags &= ~CC_MPEG_ID; /* Required for ADTS. */
553 cc->extAOT = AOT_NULL_OBJECT;
554 break;
555 default:
556 cc->flags |= CC_MPEG_ID;
557 }
558
559 /* ER-tools signaling. */
560 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_VCB11) ? CC_VCB11 : 0;
561 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_HCR) ? CC_HCR : 0;
562 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_RVLC) ? CC_RVLC : 0;
563
564 /* Matrix mixdown coefficient configuration. */
565 if ((extCfg->userPceAdditions & 0x1) && (hAacConfig->epConfig == -1) &&
566 ((cc->channelMode == MODE_1_2_2) || (cc->channelMode == MODE_1_2_2_1))) {
567 cc->matrixMixdownA = ((extCfg->userPceAdditions >> 1) & 0x3) + 1;
568 cc->flags |= (extCfg->userPceAdditions >> 3) & 0x1 ? CC_PSEUDO_SURROUND : 0;
569 } else {
570 cc->matrixMixdownA = 0;
571 }
572
573 cc->channelConfigZero = 0;
574 }
575
576 /*
577 * Validate prefilled pointers within buffer descriptor.
578 *
579 * \param pBufDesc Pointer to buffer descriptor
580
581 * \return - AACENC_OK, all fine.
582 * - AACENC_INVALID_HANDLE, on missing pointer initializiation.
583 * - AACENC_UNSUPPORTED_PARAMETER, on incorrect buffer descriptor
584 initialization.
585 */
validateBufDesc(const AACENC_BufDesc * pBufDesc)586 static AACENC_ERROR validateBufDesc(const AACENC_BufDesc *pBufDesc) {
587 AACENC_ERROR err = AACENC_OK;
588
589 if (pBufDesc != NULL) {
590 int i;
591 if ((pBufDesc->bufferIdentifiers == NULL) || (pBufDesc->bufSizes == NULL) ||
592 (pBufDesc->bufElSizes == NULL) || (pBufDesc->bufs == NULL)) {
593 err = AACENC_UNSUPPORTED_PARAMETER;
594 goto bail;
595 }
596 for (i = 0; i < pBufDesc->numBufs; i++) {
597 if (pBufDesc->bufs[i] == NULL) {
598 err = AACENC_UNSUPPORTED_PARAMETER;
599 goto bail;
600 }
601 }
602 } else {
603 err = AACENC_INVALID_HANDLE;
604 }
605 bail:
606 return err;
607 }
608
609 /*
610 * Examine buffer descriptor regarding choosen identifier.
611 *
612 * \param pBufDesc Pointer to buffer descriptor
613 * \param identifier Buffer identifier to look for.
614
615 * \return - Buffer descriptor index.
616 * -1, if there is no entry available.
617 */
getBufDescIdx(const AACENC_BufDesc * pBufDesc,const AACENC_BufferIdentifier identifier)618 static INT getBufDescIdx(const AACENC_BufDesc *pBufDesc,
619 const AACENC_BufferIdentifier identifier) {
620 INT i, idx = -1;
621
622 if (pBufDesc != NULL) {
623 for (i = 0; i < pBufDesc->numBufs; i++) {
624 if ((AACENC_BufferIdentifier)pBufDesc->bufferIdentifiers[i] ==
625 identifier) {
626 idx = i;
627 break;
628 }
629 }
630 }
631 return idx;
632 }
633
634 /****************************************************************************
635 Function Declarations
636 ****************************************************************************/
637
aacEncDefaultConfig(HANDLE_AACENC_CONFIG hAacConfig,USER_PARAM * config)638 AAC_ENCODER_ERROR aacEncDefaultConfig(HANDLE_AACENC_CONFIG hAacConfig,
639 USER_PARAM *config) {
640 /* make reasonable default settings */
641 FDKaacEnc_AacInitDefaultConfig(hAacConfig);
642
643 /* clear configuration structure and copy default settings */
644 FDKmemclear(config, sizeof(USER_PARAM));
645
646 /* copy encoder configuration settings */
647 config->nChannels = hAacConfig->nChannels;
648 config->userAOT = hAacConfig->audioObjectType = AOT_AAC_LC;
649 config->userSamplerate = hAacConfig->sampleRate;
650 config->userChannelMode = hAacConfig->channelMode;
651 config->userBitrate = hAacConfig->bitRate;
652 config->userBitrateMode = hAacConfig->bitrateMode;
653 config->userPeakBitrate = (UINT)-1;
654 config->userBandwidth = hAacConfig->bandWidth;
655 config->userTns = hAacConfig->useTns;
656 config->userPns = hAacConfig->usePns;
657 config->userIntensity = hAacConfig->useIS;
658 config->userAfterburner = hAacConfig->useRequant;
659 config->userFramelength = (UINT)-1;
660
661 config->userDownscaleFactor = 1;
662
663 /* initialize transport parameters */
664 config->userTpType = TT_UNKNOWN;
665 config->userTpAmxv = 0;
666 config->userTpSignaling = 0xFF; /* choose signaling automatically */
667 config->userTpNsubFrames = 1;
668 config->userTpProtection = 0; /* not crc protected*/
669 config->userTpHeaderPeriod = 0xFF; /* header period in auto mode */
670 config->userPceAdditions = 0; /* no matrix mixdown coefficient */
671 config->userMetaDataMode = 0; /* do not embed any meta data info */
672
673 config->userAncDataRate = 0;
674
675 /* SBR rate is set to 0 here, which means it should be set automatically
676 in FDKaacEnc_AdjustEncSettings() if the user did not set a rate
677 expilicitely. */
678 config->userSbrRatio = 0;
679
680 /* SBR enable set to -1 means to inquire ELD audio configurator for reasonable
681 * configuration. */
682 config->userSbrEnabled = (UCHAR)-1;
683
684 return AAC_ENC_OK;
685 }
686
aacEncDistributeSbrBits(CHANNEL_MAPPING * channelMapping,SBR_ELEMENT_INFO * sbrElInfo,INT bitRate)687 static void aacEncDistributeSbrBits(CHANNEL_MAPPING *channelMapping,
688 SBR_ELEMENT_INFO *sbrElInfo, INT bitRate) {
689 INT codebits = bitRate;
690 int el;
691
692 /* Copy Element info */
693 for (el = 0; el < channelMapping->nElements; el++) {
694 sbrElInfo[el].ChannelIndex[0] = channelMapping->elInfo[el].ChannelIndex[0];
695 sbrElInfo[el].ChannelIndex[1] = channelMapping->elInfo[el].ChannelIndex[1];
696 sbrElInfo[el].elType = channelMapping->elInfo[el].elType;
697 sbrElInfo[el].bitRate =
698 fMultIfloor(channelMapping->elInfo[el].relativeBits, bitRate);
699 sbrElInfo[el].instanceTag = channelMapping->elInfo[el].instanceTag;
700 sbrElInfo[el].nChannelsInEl = channelMapping->elInfo[el].nChannelsInEl;
701 sbrElInfo[el].fParametricStereo = 0;
702 sbrElInfo[el].fDualMono = 0;
703
704 codebits -= sbrElInfo[el].bitRate;
705 }
706 sbrElInfo[0].bitRate += codebits;
707 }
708
aacEncoder_LimitBitrate(const HANDLE_TRANSPORTENC hTpEnc,const INT samplingRate,const INT frameLength,const INT nChannels,const CHANNEL_MODE channelMode,INT bitRate,const INT nSubFrames,const INT sbrActive,const INT sbrDownSampleRate,const UINT syntaxFlags,const AUDIO_OBJECT_TYPE aot)709 static INT aacEncoder_LimitBitrate(const HANDLE_TRANSPORTENC hTpEnc,
710 const INT samplingRate,
711 const INT frameLength, const INT nChannels,
712 const CHANNEL_MODE channelMode, INT bitRate,
713 const INT nSubFrames, const INT sbrActive,
714 const INT sbrDownSampleRate,
715 const UINT syntaxFlags,
716 const AUDIO_OBJECT_TYPE aot) {
717 INT coreSamplingRate;
718 CHANNEL_MAPPING cm;
719
720 FDKaacEnc_InitChannelMapping(channelMode, CH_ORDER_MPEG, &cm);
721
722 if (sbrActive) {
723 coreSamplingRate =
724 samplingRate >>
725 (sbrEncoder_IsSingleRatePossible(aot) ? (sbrDownSampleRate - 1) : 1);
726 } else {
727 coreSamplingRate = samplingRate;
728 }
729
730 /* Limit bit rate in respect to the core coder */
731 bitRate = FDKaacEnc_LimitBitrate(hTpEnc, aot, coreSamplingRate, frameLength,
732 nChannels, cm.nChannelsEff, bitRate, -1,
733 NULL, AACENC_BR_MODE_INVALID, nSubFrames);
734
735 /* Limit bit rate in respect to available SBR modes if active */
736 if (sbrActive) {
737 int numIterations = 0;
738 INT initialBitrate, adjustedBitrate;
739 adjustedBitrate = bitRate;
740
741 /* Find total bitrate which provides valid configuration for each SBR
742 * element. */
743 do {
744 int e;
745 SBR_ELEMENT_INFO sbrElInfo[((8))];
746 FDK_ASSERT(cm.nElements <= ((8)));
747
748 initialBitrate = adjustedBitrate;
749
750 /* Get bit rate for each SBR element */
751 aacEncDistributeSbrBits(&cm, sbrElInfo, initialBitrate);
752
753 for (e = 0; e < cm.nElements; e++) {
754 INT sbrElementBitRateIn, sbrBitRateOut;
755
756 if (cm.elInfo[e].elType != ID_SCE && cm.elInfo[e].elType != ID_CPE) {
757 continue;
758 }
759 sbrElementBitRateIn = sbrElInfo[e].bitRate;
760
761 sbrBitRateOut = sbrEncoder_LimitBitRate(sbrElementBitRateIn,
762 cm.elInfo[e].nChannelsInEl,
763 coreSamplingRate, aot);
764
765 if (sbrBitRateOut == 0) {
766 return 0;
767 }
768
769 /* If bitrates don't match, distribution and limiting needs to be
770 determined again. Abort element loop and restart with adapted
771 bitrate. */
772 if (sbrElementBitRateIn != sbrBitRateOut) {
773 if (sbrElementBitRateIn < sbrBitRateOut) {
774 adjustedBitrate = fMax(initialBitrate,
775 (INT)fDivNorm((FIXP_DBL)(sbrBitRateOut + 8),
776 cm.elInfo[e].relativeBits));
777 break;
778 }
779
780 if (sbrElementBitRateIn > sbrBitRateOut) {
781 adjustedBitrate = fMin(initialBitrate,
782 (INT)fDivNorm((FIXP_DBL)(sbrBitRateOut - 8),
783 cm.elInfo[e].relativeBits));
784 break;
785 }
786
787 } /* sbrElementBitRateIn != sbrBitRateOut */
788
789 } /* elements */
790
791 numIterations++; /* restrict iteration to worst case of num elements */
792
793 } while ((initialBitrate != adjustedBitrate) &&
794 (numIterations <= cm.nElements));
795
796 /* Unequal bitrates mean that no reasonable bitrate configuration found. */
797 bitRate = (initialBitrate == adjustedBitrate) ? adjustedBitrate : 0;
798 }
799
800 /* Limit bit rate in respect to available MPS modes if active */
801 if ((aot == AOT_ER_AAC_ELD) && (syntaxFlags & AC_LD_MPS) &&
802 (channelMode == MODE_1)) {
803 bitRate = FDK_MpegsEnc_GetClosestBitRate(
804 aot, MODE_212, samplingRate, (sbrActive) ? sbrDownSampleRate : 0,
805 bitRate);
806 }
807
808 return bitRate;
809 }
810
811 /*
812 * \brief Get CBR bitrate
813 *
814 * \hAacConfig Internal encoder config
815 * \return Bitrate
816 */
FDKaacEnc_GetCBRBitrate(const HANDLE_AACENC_CONFIG hAacConfig,const INT userSbrRatio)817 static INT FDKaacEnc_GetCBRBitrate(const HANDLE_AACENC_CONFIG hAacConfig,
818 const INT userSbrRatio) {
819 INT bitrate = FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
820 ->nChannelsEff *
821 hAacConfig->sampleRate;
822
823 if (isPsActive(hAacConfig->audioObjectType)) {
824 bitrate = 1 * bitrate; /* 0.5 bit per sample */
825 } else if (isSbrActive(hAacConfig)) {
826 if ((userSbrRatio == 2) ||
827 ((userSbrRatio == 0) &&
828 (hAacConfig->audioObjectType != AOT_ER_AAC_ELD))) {
829 bitrate = (bitrate + (bitrate >> 2)) >> 1; /* 0.625 bits per sample */
830 }
831 if ((userSbrRatio == 1) ||
832 ((userSbrRatio == 0) &&
833 (hAacConfig->audioObjectType == AOT_ER_AAC_ELD))) {
834 bitrate = (bitrate + (bitrate >> 3)); /* 1.125 bits per sample */
835 }
836 } else {
837 bitrate = bitrate + (bitrate >> 1); /* 1.5 bits per sample */
838 }
839
840 return bitrate;
841 }
842
843 /*
844 * \brief Consistency check of given USER_PARAM struct and
845 * copy back configuration from public struct into internal
846 * encoder configuration struct.
847 *
848 * \hAacEncoder Internal encoder config which is to be updated
849 * \param config User provided config (public struct)
850 * \return returns always AAC_ENC_OK
851 */
FDKaacEnc_AdjustEncSettings(HANDLE_AACENCODER hAacEncoder,USER_PARAM * config)852 static AACENC_ERROR FDKaacEnc_AdjustEncSettings(HANDLE_AACENCODER hAacEncoder,
853 USER_PARAM *config) {
854 AACENC_ERROR err = AACENC_OK;
855
856 /* Get struct pointers. */
857 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig;
858
859 /* Encoder settings update. */
860 hAacConfig->sampleRate = config->userSamplerate;
861 if (config->userDownscaleFactor > 1) {
862 hAacConfig->useTns = 0;
863 hAacConfig->usePns = 0;
864 hAacConfig->useIS = 0;
865 } else {
866 hAacConfig->useTns = config->userTns;
867 hAacConfig->usePns = config->userPns;
868 hAacConfig->useIS = config->userIntensity;
869 }
870
871 hAacConfig->audioObjectType = config->userAOT;
872 hAacConfig->channelMode =
873 GetCoreChannelMode(config->userChannelMode, hAacConfig->audioObjectType);
874 hAacConfig->nChannels =
875 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)->nChannels;
876 hAacConfig->bitrateMode = (AACENC_BITRATE_MODE)config->userBitrateMode;
877 hAacConfig->bandWidth = config->userBandwidth;
878 hAacConfig->useRequant = config->userAfterburner;
879
880 hAacConfig->anc_Rate = config->userAncDataRate;
881 hAacConfig->syntaxFlags = 0;
882 hAacConfig->epConfig = -1;
883
884 if (hAacConfig->audioObjectType != AOT_ER_AAC_ELD &&
885 config->userDownscaleFactor > 1) {
886 return AACENC_INVALID_CONFIG; /* downscaling only allowed for AOT_ER_AAC_ELD
887 */
888 }
889 if (config->userDownscaleFactor > 1 && config->userSbrEnabled == 1) {
890 return AACENC_INVALID_CONFIG; /* downscaling only allowed for AOT_ER_AAC_ELD
891 w/o SBR */
892 }
893 if (config->userDownscaleFactor > 1 && config->userChannelMode == 128) {
894 return AACENC_INVALID_CONFIG; /* disallow downscaling for AAC-ELDv2 */
895 }
896
897 if (config->userTpType == TT_MP4_LATM_MCP1 ||
898 config->userTpType == TT_MP4_LATM_MCP0 ||
899 config->userTpType == TT_MP4_LOAS) {
900 hAacConfig->audioMuxVersion = config->userTpAmxv;
901 } else {
902 hAacConfig->audioMuxVersion = -1;
903 }
904
905 /* Adapt internal AOT when necessary. */
906 switch (config->userAOT) {
907 case AOT_MP2_AAC_LC:
908 case AOT_MP2_SBR:
909 hAacConfig->usePns = 0;
910 FDK_FALLTHROUGH;
911 case AOT_AAC_LC:
912 case AOT_SBR:
913 case AOT_PS:
914 config->userTpType =
915 (config->userTpType != TT_UNKNOWN) ? config->userTpType : TT_MP4_ADTS;
916 hAacConfig->framelength = (config->userFramelength != (UINT)-1)
917 ? config->userFramelength
918 : 1024;
919 if (hAacConfig->framelength != 1024 && hAacConfig->framelength != 960) {
920 return AACENC_INVALID_CONFIG;
921 }
922 break;
923 case AOT_ER_AAC_LD:
924 hAacConfig->epConfig = 0;
925 hAacConfig->syntaxFlags |= AC_ER | AC_LD;
926 hAacConfig->syntaxFlags |=
927 ((config->userErTools & 0x1) ? AC_ER_VCB11 : 0);
928 hAacConfig->syntaxFlags |= ((config->userErTools & 0x2) ? AC_ER_HCR : 0);
929 hAacConfig->syntaxFlags |= ((config->userErTools & 0x4) ? AC_ER_RVLC : 0);
930 config->userTpType =
931 (config->userTpType != TT_UNKNOWN) ? config->userTpType : TT_MP4_LOAS;
932 hAacConfig->framelength =
933 (config->userFramelength != (UINT)-1) ? config->userFramelength : 512;
934 if (hAacConfig->framelength != 512 && hAacConfig->framelength != 480) {
935 return AACENC_INVALID_CONFIG;
936 }
937 break;
938 case AOT_ER_AAC_ELD:
939 hAacConfig->epConfig = 0;
940 hAacConfig->syntaxFlags |= AC_ER | AC_ELD;
941 hAacConfig->syntaxFlags |=
942 ((config->userErTools & 0x1) ? AC_ER_VCB11 : 0);
943 hAacConfig->syntaxFlags |= ((config->userErTools & 0x2) ? AC_ER_HCR : 0);
944 hAacConfig->syntaxFlags |= ((config->userErTools & 0x4) ? AC_ER_RVLC : 0);
945 hAacConfig->syntaxFlags |=
946 ((config->userSbrEnabled == 1) ? AC_SBR_PRESENT : 0);
947 hAacConfig->syntaxFlags |=
948 ((config->userChannelMode == MODE_212) ? AC_LD_MPS : 0);
949 config->userTpType =
950 (config->userTpType != TT_UNKNOWN) ? config->userTpType : TT_MP4_LOAS;
951 hAacConfig->framelength =
952 (config->userFramelength != (UINT)-1) ? config->userFramelength : 512;
953
954 hAacConfig->downscaleFactor = config->userDownscaleFactor;
955
956 switch (config->userDownscaleFactor) {
957 case 1:
958 break;
959 case 2:
960 case 4:
961 hAacConfig->syntaxFlags |= AC_ELD_DOWNSCALE;
962 break;
963 default:
964 return AACENC_INVALID_CONFIG;
965 }
966
967 if (hAacConfig->framelength != 512 && hAacConfig->framelength != 480 &&
968 hAacConfig->framelength != 256 && hAacConfig->framelength != 240 &&
969 hAacConfig->framelength != 128 && hAacConfig->framelength != 120) {
970 return AACENC_INVALID_CONFIG;
971 }
972 break;
973 default:
974 break;
975 }
976
977 /* Initialize SBR parameters */
978 if ((config->userSbrRatio == 0) && (isSbrActive(hAacConfig))) {
979 /* Automatic SBR ratio configuration
980 * - downsampled SBR for ELD
981 * - otherwise always dualrate SBR
982 */
983 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) {
984 hAacConfig->sbrRatio = ((hAacConfig->syntaxFlags & AC_LD_MPS) &&
985 (hAacConfig->sampleRate >= 27713))
986 ? 2
987 : 1;
988 } else {
989 hAacConfig->sbrRatio = 2;
990 }
991 } else {
992 /* SBR ratio has been set by the user, so use it. */
993 hAacConfig->sbrRatio = isSbrActive(hAacConfig) ? config->userSbrRatio : 0;
994 }
995
996 /* Set default bitrate */
997 hAacConfig->bitRate = config->userBitrate;
998
999 switch (hAacConfig->bitrateMode) {
1000 case AACENC_BR_MODE_CBR:
1001 /* Set default bitrate if no external bitrate declared. */
1002 if (config->userBitrate == (UINT)-1) {
1003 hAacConfig->bitRate =
1004 FDKaacEnc_GetCBRBitrate(hAacConfig, config->userSbrRatio);
1005 }
1006 hAacConfig->averageBits = -1;
1007 break;
1008 case AACENC_BR_MODE_VBR_1:
1009 case AACENC_BR_MODE_VBR_2:
1010 case AACENC_BR_MODE_VBR_3:
1011 case AACENC_BR_MODE_VBR_4:
1012 case AACENC_BR_MODE_VBR_5:
1013 /* Get bitrate in VBR configuration */
1014 /* In VBR mode; SBR-modul depends on bitrate, core encoder on bitrateMode.
1015 */
1016 hAacConfig->bitRate = FDKaacEnc_GetVBRBitrate(hAacConfig->bitrateMode,
1017 hAacConfig->channelMode);
1018 break;
1019 default:
1020 return AACENC_INVALID_CONFIG;
1021 }
1022
1023 /* set bitreservoir size */
1024 switch (hAacConfig->bitrateMode) {
1025 case AACENC_BR_MODE_VBR_1:
1026 case AACENC_BR_MODE_VBR_2:
1027 case AACENC_BR_MODE_VBR_3:
1028 case AACENC_BR_MODE_VBR_4:
1029 case AACENC_BR_MODE_VBR_5:
1030 case AACENC_BR_MODE_CBR:
1031 if ((INT)config->userPeakBitrate != -1) {
1032 hAacConfig->maxBitsPerFrame =
1033 (FDKaacEnc_CalcBitsPerFrame(
1034 fMax(hAacConfig->bitRate, (INT)config->userPeakBitrate),
1035 hAacConfig->framelength, hAacConfig->sampleRate) +
1036 7) &
1037 ~7;
1038 } else {
1039 hAacConfig->maxBitsPerFrame = -1;
1040 }
1041 if (hAacConfig->audioMuxVersion == 2) {
1042 hAacConfig->minBitsPerFrame =
1043 fMin(32 * 8, FDKaacEnc_CalcBitsPerFrame(hAacConfig->bitRate,
1044 hAacConfig->framelength,
1045 hAacConfig->sampleRate)) &
1046 ~7;
1047 }
1048 break;
1049 default:
1050 return AACENC_INVALID_CONFIG;
1051 }
1052
1053 /* Max bits per frame limitation depending on transport format. */
1054 if ((config->userTpNsubFrames > 1)) {
1055 int maxFrameLength = 8 * hAacEncoder->outBufferInBytes;
1056 switch (config->userTpType) {
1057 case TT_MP4_LOAS:
1058 maxFrameLength =
1059 fMin(maxFrameLength, 8 * (1 << 13)) / config->userTpNsubFrames;
1060 break;
1061 case TT_MP4_ADTS:
1062 maxFrameLength = fMin(maxFrameLength, 8 * ((1 << 13) - 1)) /
1063 config->userTpNsubFrames;
1064 break;
1065 default:
1066 maxFrameLength = -1;
1067 }
1068 if (maxFrameLength != -1) {
1069 if (hAacConfig->maxBitsPerFrame > maxFrameLength) {
1070 return AACENC_INVALID_CONFIG;
1071 } else if (hAacConfig->maxBitsPerFrame == -1) {
1072 hAacConfig->maxBitsPerFrame = maxFrameLength;
1073 }
1074 }
1075 }
1076
1077 if ((hAacConfig->audioObjectType == AOT_ER_AAC_ELD) &&
1078 !(hAacConfig->syntaxFlags & AC_ELD_DOWNSCALE) &&
1079 (config->userSbrEnabled == (UCHAR)-1) && (config->userSbrRatio == 0) &&
1080 ((hAacConfig->syntaxFlags & AC_LD_MPS) == 0)) {
1081 const ELD_SBR_CONFIGURATOR *pConfig = NULL;
1082
1083 if (NULL !=
1084 (pConfig = eldSbrConfigurator(
1085 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1086 ->nChannels,
1087 hAacConfig->sampleRate, hAacConfig->bitRate))) {
1088 hAacConfig->syntaxFlags |= (pConfig->sbrMode == 0) ? 0 : AC_SBR_PRESENT;
1089 hAacConfig->syntaxFlags |= (pConfig->chMode == MODE_212) ? AC_LD_MPS : 0;
1090 hAacConfig->channelMode =
1091 GetCoreChannelMode(pConfig->chMode, hAacConfig->audioObjectType);
1092 hAacConfig->nChannels =
1093 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1094 ->nChannels;
1095 hAacConfig->sbrRatio =
1096 (pConfig->sbrMode == 0) ? 0 : (pConfig->sbrMode == 1) ? 1 : 2;
1097 }
1098 }
1099
1100 {
1101 UCHAR tpSignaling =
1102 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType,
1103 config->userTpSignaling, hAacConfig->sbrRatio);
1104
1105 if ((hAacConfig->audioObjectType == AOT_AAC_LC ||
1106 hAacConfig->audioObjectType == AOT_SBR ||
1107 hAacConfig->audioObjectType == AOT_PS) &&
1108 (config->userTpType == TT_MP4_LATM_MCP1 ||
1109 config->userTpType == TT_MP4_LATM_MCP0 ||
1110 config->userTpType == TT_MP4_LOAS) &&
1111 (tpSignaling == 1) && (config->userTpAmxv == 0)) {
1112 /* For backward compatible explicit signaling, AMV1 has to be active */
1113 return AACENC_INVALID_CONFIG;
1114 }
1115
1116 if ((hAacConfig->audioObjectType == AOT_AAC_LC ||
1117 hAacConfig->audioObjectType == AOT_SBR ||
1118 hAacConfig->audioObjectType == AOT_PS) &&
1119 (tpSignaling == 0) && (hAacConfig->sbrRatio == 1)) {
1120 /* Downsampled SBR has to be signaled explicitely (for transmission of SBR
1121 * sampling fequency) */
1122 return AACENC_INVALID_CONFIG;
1123 }
1124 }
1125
1126 switch (hAacConfig->bitrateMode) {
1127 case AACENC_BR_MODE_CBR:
1128 case AACENC_BR_MODE_VBR_1:
1129 case AACENC_BR_MODE_VBR_2:
1130 case AACENC_BR_MODE_VBR_3:
1131 case AACENC_BR_MODE_VBR_4:
1132 case AACENC_BR_MODE_VBR_5:
1133 /* We need the frame length to call aacEncoder_LimitBitrate() */
1134 if (0 >= (hAacConfig->bitRate = aacEncoder_LimitBitrate(
1135 NULL, hAacConfig->sampleRate, hAacConfig->framelength,
1136 hAacConfig->nChannels, hAacConfig->channelMode,
1137 hAacConfig->bitRate, hAacConfig->nSubFrames,
1138 isSbrActive(hAacConfig), hAacConfig->sbrRatio,
1139 hAacConfig->syntaxFlags, hAacConfig->audioObjectType))) {
1140 return AACENC_INVALID_CONFIG;
1141 }
1142 break;
1143 default:
1144 break;
1145 }
1146
1147 /* Configure PNS */
1148 if (AACENC_BR_MODE_IS_VBR(hAacConfig->bitrateMode) /* VBR without PNS. */
1149 || (hAacConfig->useTns == 0)) /* TNS required. */
1150 {
1151 hAacConfig->usePns = 0;
1152 }
1153
1154 if (hAacConfig->epConfig >= 0) {
1155 hAacConfig->syntaxFlags |= AC_ER;
1156 if (((INT)hAacConfig->channelMode < 1) ||
1157 ((INT)hAacConfig->channelMode > 14)) {
1158 return AACENC_INVALID_CONFIG; /* Channel config 0 not supported. */
1159 }
1160 }
1161
1162 if ((hAacConfig->syntaxFlags & AC_LD_MPS) == 0) {
1163 if (FDKaacEnc_DetermineEncoderMode(&hAacConfig->channelMode,
1164 hAacConfig->nChannels) != AAC_ENC_OK) {
1165 return AACENC_INVALID_CONFIG; /* nChannels doesn't match chMode, this is
1166 just a check-up */
1167 }
1168 }
1169
1170 if ((hAacConfig->nChannels > hAacEncoder->nMaxAacChannels) ||
1171 ((FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1172 ->nChannelsEff > hAacEncoder->nMaxSbrChannels) &&
1173 isSbrActive(hAacConfig))) {
1174 return AACENC_INVALID_CONFIG; /* not enough channels allocated */
1175 }
1176
1177 /* Meta data restriction. */
1178 switch (hAacConfig->audioObjectType) {
1179 /* Allow metadata support */
1180 case AOT_AAC_LC:
1181 case AOT_SBR:
1182 case AOT_PS:
1183 case AOT_MP2_AAC_LC:
1184 case AOT_MP2_SBR:
1185 hAacEncoder->metaDataAllowed = 1;
1186 if (!((((INT)hAacConfig->channelMode >= 1) &&
1187 ((INT)hAacConfig->channelMode <= 14)) ||
1188 (MODE_7_1_REAR_SURROUND == hAacConfig->channelMode) ||
1189 (MODE_7_1_FRONT_CENTER == hAacConfig->channelMode))) {
1190 config->userMetaDataMode = 0;
1191 }
1192 break;
1193 /* Prohibit metadata support */
1194 default:
1195 hAacEncoder->metaDataAllowed = 0;
1196 }
1197
1198 return err;
1199 }
1200
aacenc_SbrCallback(void * self,HANDLE_FDK_BITSTREAM hBs,const INT sampleRateIn,const INT sampleRateOut,const INT samplesPerFrame,const AUDIO_OBJECT_TYPE coreCodec,const MP4_ELEMENT_ID elementID,const INT elementIndex,const UCHAR harmonicSbr,const UCHAR stereoConfigIndex,const UCHAR configMode,UCHAR * configChanged,const INT downscaleFactor)1201 static INT aacenc_SbrCallback(void *self, HANDLE_FDK_BITSTREAM hBs,
1202 const INT sampleRateIn, const INT sampleRateOut,
1203 const INT samplesPerFrame,
1204 const AUDIO_OBJECT_TYPE coreCodec,
1205 const MP4_ELEMENT_ID elementID,
1206 const INT elementIndex, const UCHAR harmonicSbr,
1207 const UCHAR stereoConfigIndex,
1208 const UCHAR configMode, UCHAR *configChanged,
1209 const INT downscaleFactor) {
1210 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self;
1211
1212 sbrEncoder_GetHeader(hAacEncoder->hEnvEnc, hBs, elementIndex, 0);
1213
1214 return 0;
1215 }
1216
aacenc_SscCallback(void * self,HANDLE_FDK_BITSTREAM hBs,const AUDIO_OBJECT_TYPE coreCodec,const INT samplingRate,const INT frameSize,const INT stereoConfigIndex,const INT coreSbrFrameLengthIndex,const INT configBytes,const UCHAR configMode,UCHAR * configChanged)1217 INT aacenc_SscCallback(void *self, HANDLE_FDK_BITSTREAM hBs,
1218 const AUDIO_OBJECT_TYPE coreCodec,
1219 const INT samplingRate, const INT frameSize,
1220 const INT stereoConfigIndex,
1221 const INT coreSbrFrameLengthIndex, const INT configBytes,
1222 const UCHAR configMode, UCHAR *configChanged) {
1223 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self;
1224
1225 return (FDK_MpegsEnc_WriteSpatialSpecificConfig(hAacEncoder->hMpsEnc, hBs));
1226 }
1227
aacEncInit(HANDLE_AACENCODER hAacEncoder,ULONG InitFlags,USER_PARAM * config)1228 static AACENC_ERROR aacEncInit(HANDLE_AACENCODER hAacEncoder, ULONG InitFlags,
1229 USER_PARAM *config) {
1230 AACENC_ERROR err = AACENC_OK;
1231
1232 INT aacBufferOffset = 0;
1233 HANDLE_SBR_ENCODER *hSbrEncoder = &hAacEncoder->hEnvEnc;
1234 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig;
1235
1236 hAacEncoder->nZerosAppended = 0; /* count appended zeros */
1237
1238 INT frameLength = hAacConfig->framelength;
1239
1240 if ((InitFlags & AACENC_INIT_CONFIG)) {
1241 CHANNEL_MODE prevChMode = hAacConfig->channelMode;
1242
1243 /* Verify settings and update: config -> heAacEncoder */
1244 if ((err = FDKaacEnc_AdjustEncSettings(hAacEncoder, config)) != AACENC_OK) {
1245 return err;
1246 }
1247 frameLength = hAacConfig->framelength; /* adapt temporal framelength */
1248
1249 /* Seamless channel reconfiguration in sbr not fully implemented */
1250 if ((prevChMode != hAacConfig->channelMode) && isSbrActive(hAacConfig)) {
1251 InitFlags |= AACENC_INIT_STATES;
1252 }
1253 }
1254
1255 /* Clear input buffer */
1256 if (InitFlags == AACENC_INIT_ALL) {
1257 FDKmemclear(hAacEncoder->inputBuffer,
1258 sizeof(INT_PCM) * hAacEncoder->inputBufferSize);
1259 }
1260
1261 if ((InitFlags & AACENC_INIT_CONFIG)) {
1262 aacBufferOffset = 0;
1263 switch (hAacConfig->audioObjectType) {
1264 case AOT_ER_AAC_LD:
1265 hAacEncoder->nDelay = DELAY_AACLD(hAacConfig->framelength);
1266 break;
1267 case AOT_ER_AAC_ELD:
1268 hAacEncoder->nDelay = DELAY_AACELD(hAacConfig->framelength);
1269 break;
1270 default:
1271 hAacEncoder->nDelay =
1272 DELAY_AAC(hAacConfig->framelength); /* AAC encoder delay */
1273 }
1274
1275 hAacConfig->ancDataBitRate = 0;
1276 }
1277
1278 if ((NULL != hAacEncoder->hEnvEnc) && isSbrActive(hAacConfig) &&
1279 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES))) {
1280 INT sbrError;
1281 UINT initFlag = 0;
1282 SBR_ELEMENT_INFO sbrElInfo[(8)];
1283 CHANNEL_MAPPING channelMapping;
1284 CHANNEL_MODE channelMode = isPsActive(hAacConfig->audioObjectType)
1285 ? config->userChannelMode
1286 : hAacConfig->channelMode;
1287 INT numChannels = isPsActive(hAacConfig->audioObjectType)
1288 ? config->nChannels
1289 : hAacConfig->nChannels;
1290
1291 if (FDKaacEnc_InitChannelMapping(channelMode, hAacConfig->channelOrder,
1292 &channelMapping) != AAC_ENC_OK) {
1293 return AACENC_INIT_ERROR;
1294 }
1295
1296 /* Check return value and if the SBR encoder can handle enough elements */
1297 if (channelMapping.nElements > (8)) {
1298 return AACENC_INIT_ERROR;
1299 }
1300
1301 aacEncDistributeSbrBits(&channelMapping, sbrElInfo, hAacConfig->bitRate);
1302
1303 initFlag += (InitFlags & AACENC_INIT_STATES) ? 1 : 0;
1304
1305 /* Let the SBR encoder take a look at the configuration and change if
1306 * required. */
1307 sbrError = sbrEncoder_Init(
1308 *hSbrEncoder, sbrElInfo, channelMapping.nElements,
1309 hAacEncoder->inputBuffer, hAacEncoder->inputBufferSizePerChannel,
1310 &hAacConfig->bandWidth, &aacBufferOffset, &numChannels,
1311 hAacConfig->syntaxFlags, &hAacConfig->sampleRate, &hAacConfig->sbrRatio,
1312 &frameLength, hAacConfig->audioObjectType, &hAacEncoder->nDelay,
1313 (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) ? 1 : TRANS_FAC,
1314 (config->userTpHeaderPeriod != 0xFF)
1315 ? config->userTpHeaderPeriod
1316 : DEFAULT_HEADER_PERIOD_REPETITION_RATE,
1317 initFlag);
1318
1319 /* Suppress AOT reconfiguration and check error status. */
1320 if ((sbrError) || (numChannels != hAacConfig->nChannels)) {
1321 return AACENC_INIT_SBR_ERROR;
1322 }
1323
1324 if (numChannels == 1) {
1325 hAacConfig->channelMode = MODE_1;
1326 }
1327
1328 /* Never use PNS if SBR is active */
1329 if (hAacConfig->usePns) {
1330 hAacConfig->usePns = 0;
1331 }
1332
1333 /* estimated bitrate consumed by SBR or PS */
1334 hAacConfig->ancDataBitRate = sbrEncoder_GetEstimateBitrate(*hSbrEncoder);
1335
1336 } /* sbr initialization */
1337
1338 if ((hAacEncoder->hMpsEnc != NULL) && (hAacConfig->syntaxFlags & AC_LD_MPS)) {
1339 int coreCoderDelay = DELAY_AACELD(hAacConfig->framelength);
1340
1341 if (isSbrActive(hAacConfig)) {
1342 coreCoderDelay = hAacConfig->sbrRatio * coreCoderDelay +
1343 sbrEncoder_GetInputDataDelay(*hSbrEncoder);
1344 }
1345
1346 if (MPS_ENCODER_OK !=
1347 FDK_MpegsEnc_Init(hAacEncoder->hMpsEnc, hAacConfig->audioObjectType,
1348 config->userSamplerate, hAacConfig->bitRate,
1349 isSbrActive(hAacConfig) ? hAacConfig->sbrRatio : 0,
1350 frameLength, /* for dual rate sbr this value is
1351 already multiplied by 2 */
1352 hAacEncoder->inputBufferSizePerChannel,
1353 coreCoderDelay)) {
1354 return AACENC_INIT_MPS_ERROR;
1355 }
1356 }
1357 hAacEncoder->nDelay =
1358 fMax(FDK_MpegsEnc_GetDelay(hAacEncoder->hMpsEnc), hAacEncoder->nDelay);
1359
1360 /*
1361 * Initialize Transport - Module.
1362 */
1363 if ((InitFlags & AACENC_INIT_TRANSPORT)) {
1364 UINT flags = 0;
1365
1366 FDKaacEnc_MapConfig(
1367 &hAacEncoder->coderConfig, config,
1368 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType,
1369 config->userTpSignaling, hAacConfig->sbrRatio),
1370 hAacConfig);
1371
1372 /* create flags for transport encoder */
1373 if (config->userTpAmxv != 0) {
1374 flags |= TP_FLAG_LATM_AMV;
1375 }
1376 /* Clear output buffer */
1377 FDKmemclear(hAacEncoder->outBuffer,
1378 hAacEncoder->outBufferInBytes * sizeof(UCHAR));
1379
1380 /* Initialize Bitstream encoder */
1381 if (transportEnc_Init(hAacEncoder->hTpEnc, hAacEncoder->outBuffer,
1382 hAacEncoder->outBufferInBytes, config->userTpType,
1383 &hAacEncoder->coderConfig, flags) != 0) {
1384 return AACENC_INIT_TP_ERROR;
1385 }
1386
1387 } /* transport initialization */
1388
1389 /*
1390 * Initialize AAC - Core.
1391 */
1392 if ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES)) {
1393 if (FDKaacEnc_Initialize(
1394 hAacEncoder->hAacEnc, hAacConfig, hAacEncoder->hTpEnc,
1395 (InitFlags & AACENC_INIT_STATES) ? 1 : 0) != AAC_ENC_OK) {
1396 return AACENC_INIT_AAC_ERROR;
1397 }
1398
1399 } /* aac initialization */
1400
1401 /*
1402 * Initialize Meta Data - Encoder.
1403 */
1404 if (hAacEncoder->hMetadataEnc && (hAacEncoder->metaDataAllowed != 0) &&
1405 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES))) {
1406 INT inputDataDelay = DELAY_AAC(hAacConfig->framelength);
1407
1408 if (isSbrActive(hAacConfig) && hSbrEncoder != NULL) {
1409 inputDataDelay = hAacConfig->sbrRatio * inputDataDelay +
1410 sbrEncoder_GetInputDataDelay(*hSbrEncoder);
1411 }
1412
1413 if (FDK_MetadataEnc_Init(hAacEncoder->hMetadataEnc,
1414 ((InitFlags & AACENC_INIT_STATES) ? 1 : 0),
1415 config->userMetaDataMode, inputDataDelay,
1416 frameLength, config->userSamplerate,
1417 config->nChannels, config->userChannelMode,
1418 hAacConfig->channelOrder) != 0) {
1419 return AACENC_INIT_META_ERROR;
1420 }
1421
1422 hAacEncoder->nDelay += FDK_MetadataEnc_GetDelay(hAacEncoder->hMetadataEnc);
1423 }
1424
1425 /* Get custom delay, i.e. the codec delay w/o the decoder's SBR- or MPS delay
1426 */
1427 if ((hAacEncoder->hMpsEnc != NULL) && (hAacConfig->syntaxFlags & AC_LD_MPS)) {
1428 hAacEncoder->nDelayCore =
1429 hAacEncoder->nDelay -
1430 fMax(0, FDK_MpegsEnc_GetDecDelay(hAacEncoder->hMpsEnc));
1431 } else if (isSbrActive(hAacConfig) && hSbrEncoder != NULL) {
1432 hAacEncoder->nDelayCore =
1433 hAacEncoder->nDelay -
1434 fMax(0, sbrEncoder_GetSbrDecDelay(hAacEncoder->hEnvEnc));
1435 } else {
1436 hAacEncoder->nDelayCore = hAacEncoder->nDelay;
1437 }
1438
1439 /*
1440 * Update pointer to working buffer.
1441 */
1442 if ((InitFlags & AACENC_INIT_CONFIG)) {
1443 hAacEncoder->inputBufferOffset = aacBufferOffset;
1444
1445 hAacEncoder->nSamplesToRead = frameLength * config->nChannels;
1446
1447 } /* parameter changed */
1448
1449 return AACENC_OK;
1450 }
1451
aacEncOpen(HANDLE_AACENCODER * phAacEncoder,const UINT encModules,const UINT maxChannels)1452 AACENC_ERROR aacEncOpen(HANDLE_AACENCODER *phAacEncoder, const UINT encModules,
1453 const UINT maxChannels) {
1454 AACENC_ERROR err = AACENC_OK;
1455 HANDLE_AACENCODER hAacEncoder = NULL;
1456
1457 if (phAacEncoder == NULL) {
1458 err = AACENC_INVALID_HANDLE;
1459 goto bail;
1460 }
1461
1462 /* allocate memory */
1463 hAacEncoder = Get_AacEncoder();
1464
1465 if (hAacEncoder == NULL) {
1466 err = AACENC_MEMORY_ERROR;
1467 goto bail;
1468 }
1469
1470 FDKmemclear(hAacEncoder, sizeof(AACENCODER));
1471
1472 /* Specify encoder modules to be allocated. */
1473 if (encModules == 0) {
1474 C_ALLOC_SCRATCH_START(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1475 LIB_INFO(*pLibInfo)
1476 [FDK_MODULE_LAST] = (LIB_INFO(*)[FDK_MODULE_LAST])_pLibInfo;
1477 FDKinitLibInfo(*pLibInfo);
1478 aacEncGetLibInfo(*pLibInfo);
1479
1480 hAacEncoder->encoder_modis = ENC_MODE_FLAG_AAC;
1481 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_SBRENC) & CAPF_SBR_HQ) {
1482 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SBR;
1483 }
1484 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_SBRENC) & CAPF_SBR_PS_MPEG) {
1485 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_PS;
1486 }
1487 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_AACENC) & CAPF_AAC_DRC) {
1488 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_META;
1489 }
1490 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SAC;
1491
1492 C_ALLOC_SCRATCH_END(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1493 } else {
1494 hAacEncoder->encoder_modis = encModules;
1495 }
1496
1497 /* Determine max channel configuration. */
1498 if (maxChannels == 0) {
1499 hAacEncoder->nMaxAacChannels = (8);
1500 hAacEncoder->nMaxSbrChannels = (8);
1501 } else {
1502 hAacEncoder->nMaxAacChannels = (maxChannels & 0x00FF);
1503 if ((hAacEncoder->encoder_modis & ENC_MODE_FLAG_SBR)) {
1504 hAacEncoder->nMaxSbrChannels = (maxChannels & 0xFF00)
1505 ? (maxChannels >> 8)
1506 : hAacEncoder->nMaxAacChannels;
1507 }
1508
1509 if ((hAacEncoder->nMaxAacChannels > (8)) ||
1510 (hAacEncoder->nMaxSbrChannels > (8))) {
1511 err = AACENC_INVALID_CONFIG;
1512 goto bail;
1513 }
1514 } /* maxChannels==0 */
1515
1516 /* Max number of elements could be tuned any more. */
1517 hAacEncoder->nMaxAacElements = fixMin(((8)), hAacEncoder->nMaxAacChannels);
1518 hAacEncoder->nMaxSbrElements = fixMin((8), hAacEncoder->nMaxSbrChannels);
1519
1520 /* In case of memory overlay, allocate memory out of libraries */
1521
1522 if (hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR | ENC_MODE_FLAG_PS))
1523 hAacEncoder->inputBufferSizePerChannel = INPUTBUFFER_SIZE;
1524 else
1525 hAacEncoder->inputBufferSizePerChannel = (1024);
1526
1527 hAacEncoder->inputBufferSize =
1528 hAacEncoder->nMaxAacChannels * hAacEncoder->inputBufferSizePerChannel;
1529
1530 if (NULL == (hAacEncoder->inputBuffer = (INT_PCM *)FDKcalloc(
1531 hAacEncoder->inputBufferSize, sizeof(INT_PCM)))) {
1532 err = AACENC_MEMORY_ERROR;
1533 goto bail;
1534 }
1535
1536 /* Open SBR Encoder */
1537 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_SBR) {
1538 if (sbrEncoder_Open(
1539 &hAacEncoder->hEnvEnc, hAacEncoder->nMaxSbrElements,
1540 hAacEncoder->nMaxSbrChannels,
1541 (hAacEncoder->encoder_modis & ENC_MODE_FLAG_PS) ? 1 : 0)) {
1542 err = AACENC_MEMORY_ERROR;
1543 goto bail;
1544 }
1545
1546 if (NULL == (hAacEncoder->pSbrPayload = (SBRENC_EXT_PAYLOAD *)FDKcalloc(
1547 1, sizeof(SBRENC_EXT_PAYLOAD)))) {
1548 err = AACENC_MEMORY_ERROR;
1549 goto bail;
1550 }
1551 } /* (encoder_modis&ENC_MODE_FLAG_SBR) */
1552
1553 /* Open Aac Encoder */
1554 if (FDKaacEnc_Open(&hAacEncoder->hAacEnc, hAacEncoder->nMaxAacElements,
1555 hAacEncoder->nMaxAacChannels, (1)) != AAC_ENC_OK) {
1556 err = AACENC_MEMORY_ERROR;
1557 goto bail;
1558 }
1559
1560 /* Bitstream output buffer */
1561 hAacEncoder->outBufferInBytes =
1562 1 << (DFRACT_BITS - CntLeadingZeros(fixMax(
1563 1, ((1) * hAacEncoder->nMaxAacChannels * 6144) >>
1564 3))); /* buffer has to be 2^n */
1565 if (NULL == (hAacEncoder->outBuffer = (UCHAR *)FDKcalloc(
1566 hAacEncoder->outBufferInBytes, sizeof(UCHAR)))) {
1567 err = AACENC_MEMORY_ERROR;
1568 goto bail;
1569 }
1570
1571 /* Open Meta Data Encoder */
1572 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_META) {
1573 if (FDK_MetadataEnc_Open(&hAacEncoder->hMetadataEnc,
1574 (UINT)hAacEncoder->nMaxAacChannels)) {
1575 err = AACENC_MEMORY_ERROR;
1576 goto bail;
1577 }
1578 } /* (encoder_modis&ENC_MODE_FLAG_META) */
1579
1580 /* Open MPEG Surround Encoder */
1581 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_SAC) {
1582 if (MPS_ENCODER_OK != FDK_MpegsEnc_Open(&hAacEncoder->hMpsEnc)) {
1583 err = AACENC_MEMORY_ERROR;
1584 goto bail;
1585 }
1586 } /* (hAacEncoder->encoder_modis&ENC_MODE_FLAG_SAC) */
1587
1588 /* Open Transport Encoder */
1589 if (transportEnc_Open(&hAacEncoder->hTpEnc) != 0) {
1590 err = AACENC_MEMORY_ERROR;
1591 goto bail;
1592 } else {
1593 C_ALLOC_SCRATCH_START(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1594
1595 LIB_INFO(*pLibInfo)
1596 [FDK_MODULE_LAST] = (LIB_INFO(*)[FDK_MODULE_LAST])_pLibInfo;
1597
1598 FDKinitLibInfo(*pLibInfo);
1599 transportEnc_GetLibInfo(*pLibInfo);
1600
1601 /* Get capabilty flag for transport encoder. */
1602 hAacEncoder->CAPF_tpEnc = FDKlibInfo_getCapabilities(*pLibInfo, FDK_TPENC);
1603
1604 C_ALLOC_SCRATCH_END(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1605 }
1606 if (transportEnc_RegisterSbrCallback(hAacEncoder->hTpEnc, aacenc_SbrCallback,
1607 hAacEncoder) != 0) {
1608 err = AACENC_INIT_TP_ERROR;
1609 goto bail;
1610 }
1611 if (transportEnc_RegisterSscCallback(hAacEncoder->hTpEnc, aacenc_SscCallback,
1612 hAacEncoder) != 0) {
1613 err = AACENC_INIT_TP_ERROR;
1614 goto bail;
1615 }
1616
1617 /* Initialize encoder instance with default parameters. */
1618 aacEncDefaultConfig(&hAacEncoder->aacConfig, &hAacEncoder->extParam);
1619
1620 /* Initialize headerPeriod in coderConfig for aacEncoder_GetParam(). */
1621 hAacEncoder->coderConfig.headerPeriod =
1622 hAacEncoder->extParam.userTpHeaderPeriod;
1623
1624 /* All encoder modules have to be initialized */
1625 hAacEncoder->InitFlags = AACENC_INIT_ALL;
1626
1627 /* Return encoder instance */
1628 *phAacEncoder = hAacEncoder;
1629
1630 return err;
1631
1632 bail:
1633 aacEncClose(&hAacEncoder);
1634
1635 return err;
1636 }
1637
aacEncClose(HANDLE_AACENCODER * phAacEncoder)1638 AACENC_ERROR aacEncClose(HANDLE_AACENCODER *phAacEncoder) {
1639 AACENC_ERROR err = AACENC_OK;
1640
1641 if (phAacEncoder == NULL) {
1642 err = AACENC_INVALID_HANDLE;
1643 goto bail;
1644 }
1645
1646 if (*phAacEncoder != NULL) {
1647 HANDLE_AACENCODER hAacEncoder = *phAacEncoder;
1648
1649 if (hAacEncoder->inputBuffer != NULL) {
1650 FDKfree(hAacEncoder->inputBuffer);
1651 hAacEncoder->inputBuffer = NULL;
1652 }
1653 if (hAacEncoder->outBuffer != NULL) {
1654 FDKfree(hAacEncoder->outBuffer);
1655 hAacEncoder->outBuffer = NULL;
1656 }
1657
1658 if (hAacEncoder->hEnvEnc) {
1659 sbrEncoder_Close(&hAacEncoder->hEnvEnc);
1660 }
1661 if (hAacEncoder->pSbrPayload != NULL) {
1662 FDKfree(hAacEncoder->pSbrPayload);
1663 hAacEncoder->pSbrPayload = NULL;
1664 }
1665 if (hAacEncoder->hAacEnc) {
1666 FDKaacEnc_Close(&hAacEncoder->hAacEnc);
1667 }
1668
1669 transportEnc_Close(&hAacEncoder->hTpEnc);
1670
1671 if (hAacEncoder->hMetadataEnc) {
1672 FDK_MetadataEnc_Close(&hAacEncoder->hMetadataEnc);
1673 }
1674 if (hAacEncoder->hMpsEnc) {
1675 FDK_MpegsEnc_Close(&hAacEncoder->hMpsEnc);
1676 }
1677
1678 Free_AacEncoder(phAacEncoder);
1679 }
1680
1681 bail:
1682 return err;
1683 }
1684
aacEncEncode(const HANDLE_AACENCODER hAacEncoder,const AACENC_BufDesc * inBufDesc,const AACENC_BufDesc * outBufDesc,const AACENC_InArgs * inargs,AACENC_OutArgs * outargs)1685 AACENC_ERROR aacEncEncode(const HANDLE_AACENCODER hAacEncoder,
1686 const AACENC_BufDesc *inBufDesc,
1687 const AACENC_BufDesc *outBufDesc,
1688 const AACENC_InArgs *inargs,
1689 AACENC_OutArgs *outargs) {
1690 AACENC_ERROR err = AACENC_OK;
1691 INT i, nBsBytes = 0;
1692 INT outBytes[(1)];
1693 int nExtensions = 0;
1694 int ancDataExtIdx = -1;
1695
1696 /* deal with valid encoder handle */
1697 if (hAacEncoder == NULL) {
1698 err = AACENC_INVALID_HANDLE;
1699 goto bail;
1700 }
1701
1702 /*
1703 * Adjust user settings and trigger reinitialization.
1704 */
1705 if (hAacEncoder->InitFlags != 0) {
1706 err =
1707 aacEncInit(hAacEncoder, hAacEncoder->InitFlags, &hAacEncoder->extParam);
1708
1709 if (err != AACENC_OK) {
1710 /* keep init flags alive! */
1711 goto bail;
1712 }
1713 hAacEncoder->InitFlags = AACENC_INIT_NONE;
1714 }
1715
1716 if (outargs != NULL) {
1717 FDKmemclear(outargs, sizeof(AACENC_OutArgs));
1718 }
1719
1720 if (outBufDesc != NULL) {
1721 for (i = 0; i < outBufDesc->numBufs; i++) {
1722 if (outBufDesc->bufs[i] != NULL) {
1723 FDKmemclear(outBufDesc->bufs[i], outBufDesc->bufSizes[i]);
1724 }
1725 }
1726 }
1727
1728 /*
1729 * If only encoder handle given, independent (re)initialization can be
1730 * triggered.
1731 */
1732 if ((inBufDesc == NULL) && (outBufDesc == NULL) && (inargs == NULL) &&
1733 (outargs == NULL)) {
1734 goto bail;
1735 }
1736
1737 /* check if buffer descriptors are filled out properly. */
1738 if ((inargs == NULL) || (outargs == NULL) ||
1739 ((AACENC_OK != validateBufDesc(inBufDesc)) &&
1740 (inargs->numInSamples > 0)) ||
1741 (AACENC_OK != validateBufDesc(outBufDesc))) {
1742 err = AACENC_UNSUPPORTED_PARAMETER;
1743 goto bail;
1744 }
1745
1746 /* reset buffer wich signals number of valid bytes in output bitstream buffer
1747 */
1748 FDKmemclear(outBytes, hAacEncoder->aacConfig.nSubFrames * sizeof(INT));
1749
1750 /*
1751 * Manage incoming audio samples.
1752 */
1753 if ((inBufDesc != NULL) && (inargs->numInSamples > 0) &&
1754 (getBufDescIdx(inBufDesc, IN_AUDIO_DATA) != -1)) {
1755 /* Fetch data until nSamplesToRead reached */
1756 INT idx = getBufDescIdx(inBufDesc, IN_AUDIO_DATA);
1757 INT newSamples =
1758 fixMax(0, fixMin(inargs->numInSamples, hAacEncoder->nSamplesToRead -
1759 hAacEncoder->nSamplesRead));
1760 INT_PCM *pIn =
1761 hAacEncoder->inputBuffer +
1762 (hAacEncoder->inputBufferOffset + hAacEncoder->nSamplesRead) /
1763 hAacEncoder->aacConfig.nChannels;
1764 newSamples -=
1765 (newSamples %
1766 hAacEncoder->extParam
1767 .nChannels); /* process multiple samples of input channels */
1768
1769 /* Copy new input samples to internal buffer */
1770 if (inBufDesc->bufElSizes[idx] == (INT)sizeof(INT_PCM)) {
1771 FDK_deinterleave((INT_PCM *)inBufDesc->bufs[idx], pIn,
1772 hAacEncoder->extParam.nChannels,
1773 newSamples / hAacEncoder->extParam.nChannels,
1774 hAacEncoder->inputBufferSizePerChannel);
1775 } else if (inBufDesc->bufElSizes[idx] > (INT)sizeof(INT_PCM)) {
1776 FDK_deinterleave((LONG *)inBufDesc->bufs[idx], pIn,
1777 hAacEncoder->extParam.nChannels,
1778 newSamples / hAacEncoder->extParam.nChannels,
1779 hAacEncoder->inputBufferSizePerChannel);
1780 } else {
1781 FDK_deinterleave((SHORT *)inBufDesc->bufs[idx], pIn,
1782 hAacEncoder->extParam.nChannels,
1783 newSamples / hAacEncoder->extParam.nChannels,
1784 hAacEncoder->inputBufferSizePerChannel);
1785 }
1786 hAacEncoder->nSamplesRead += newSamples;
1787
1788 /* Number of fetched input buffer samples. */
1789 outargs->numInSamples = newSamples;
1790 }
1791
1792 /* input buffer completely filled ? */
1793 if (hAacEncoder->nSamplesRead < hAacEncoder->nSamplesToRead) {
1794 /* - eof reached and flushing enabled, or
1795 - return to main and wait for further incoming audio samples */
1796 if (inargs->numInSamples == -1) {
1797 if ((hAacEncoder->nZerosAppended < hAacEncoder->nDelay)) {
1798 int nZeros = (hAacEncoder->nSamplesToRead - hAacEncoder->nSamplesRead) /
1799 hAacEncoder->extParam.nChannels;
1800
1801 FDK_ASSERT(nZeros >= 0);
1802
1803 /* clear out until end-of-buffer */
1804 if (nZeros) {
1805 for (i = 0; i < (int)hAacEncoder->extParam.nChannels; i++) {
1806 FDKmemclear(hAacEncoder->inputBuffer +
1807 i * hAacEncoder->inputBufferSizePerChannel +
1808 (hAacEncoder->inputBufferOffset +
1809 hAacEncoder->nSamplesRead) /
1810 hAacEncoder->extParam.nChannels,
1811 sizeof(INT_PCM) * nZeros);
1812 }
1813 hAacEncoder->nZerosAppended += nZeros;
1814 hAacEncoder->nSamplesRead = hAacEncoder->nSamplesToRead;
1815 }
1816 } else { /* flushing completed */
1817 err = AACENC_ENCODE_EOF; /* eof reached */
1818 goto bail;
1819 }
1820 } else { /* inargs->numInSamples!= -1 */
1821 goto bail; /* not enough samples in input buffer and no flushing enabled
1822 */
1823 }
1824 }
1825
1826 /* init payload */
1827 FDKmemclear(hAacEncoder->extPayload,
1828 sizeof(AACENC_EXT_PAYLOAD) * MAX_TOTAL_EXT_PAYLOADS);
1829 for (i = 0; i < MAX_TOTAL_EXT_PAYLOADS; i++) {
1830 hAacEncoder->extPayload[i].associatedChElement = -1;
1831 }
1832 if (hAacEncoder->pSbrPayload != NULL) {
1833 FDKmemclear(hAacEncoder->pSbrPayload, sizeof(*hAacEncoder->pSbrPayload));
1834 }
1835
1836 /*
1837 * Calculate Meta Data info.
1838 */
1839 if ((hAacEncoder->hMetadataEnc != NULL) &&
1840 (hAacEncoder->metaDataAllowed != 0)) {
1841 const AACENC_MetaData *pMetaData = NULL;
1842 AACENC_EXT_PAYLOAD *pMetaDataExtPayload = NULL;
1843 UINT nMetaDataExtensions = 0;
1844 INT matrix_mixdown_idx = 0;
1845
1846 /* New meta data info available ? */
1847 if (getBufDescIdx(inBufDesc, IN_METADATA_SETUP) != -1) {
1848 pMetaData =
1849 (AACENC_MetaData *)
1850 inBufDesc->bufs[getBufDescIdx(inBufDesc, IN_METADATA_SETUP)];
1851 }
1852
1853 FDK_MetadataEnc_Process(
1854 hAacEncoder->hMetadataEnc,
1855 hAacEncoder->inputBuffer + hAacEncoder->inputBufferOffset /
1856 hAacEncoder->coderConfig.noChannels,
1857 hAacEncoder->inputBufferSizePerChannel, hAacEncoder->nSamplesRead,
1858 pMetaData, &pMetaDataExtPayload, &nMetaDataExtensions,
1859 &matrix_mixdown_idx);
1860
1861 for (i = 0; i < (INT)nMetaDataExtensions;
1862 i++) { /* Get meta data extension payload. */
1863 hAacEncoder->extPayload[nExtensions++] = pMetaDataExtPayload[i];
1864 }
1865
1866 if ((matrix_mixdown_idx != -1) &&
1867 ((hAacEncoder->extParam.userChannelMode == MODE_1_2_2) ||
1868 (hAacEncoder->extParam.userChannelMode == MODE_1_2_2_1))) {
1869 /* Set matrix mixdown coefficient. */
1870 UINT pceValue = (UINT)((0 << 3) | ((matrix_mixdown_idx & 0x3) << 1) | 1);
1871 if (hAacEncoder->extParam.userPceAdditions != pceValue) {
1872 hAacEncoder->extParam.userPceAdditions = pceValue;
1873 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
1874 }
1875 }
1876 }
1877
1878 /*
1879 * Encode MPS data.
1880 */
1881 if ((hAacEncoder->hMpsEnc != NULL) &&
1882 (hAacEncoder->aacConfig.syntaxFlags & AC_LD_MPS)) {
1883 AACENC_EXT_PAYLOAD mpsExtensionPayload;
1884 FDKmemclear(&mpsExtensionPayload, sizeof(AACENC_EXT_PAYLOAD));
1885
1886 if (MPS_ENCODER_OK !=
1887 FDK_MpegsEnc_Process(
1888 hAacEncoder->hMpsEnc,
1889 hAacEncoder->inputBuffer + hAacEncoder->inputBufferOffset /
1890 hAacEncoder->coderConfig.noChannels,
1891 hAacEncoder->nSamplesRead, &mpsExtensionPayload)) {
1892 err = AACENC_ENCODE_ERROR;
1893 goto bail;
1894 }
1895
1896 if ((mpsExtensionPayload.pData != NULL) &&
1897 ((mpsExtensionPayload.dataSize != 0))) {
1898 hAacEncoder->extPayload[nExtensions++] = mpsExtensionPayload;
1899 }
1900 }
1901
1902 if ((NULL != hAacEncoder->hEnvEnc) && (NULL != hAacEncoder->pSbrPayload) &&
1903 isSbrActive(&hAacEncoder->aacConfig)) {
1904 INT nPayload = 0;
1905
1906 /*
1907 * Encode SBR data.
1908 */
1909 if (sbrEncoder_EncodeFrame(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer,
1910 hAacEncoder->inputBufferSizePerChannel,
1911 hAacEncoder->pSbrPayload->dataSize[nPayload],
1912 hAacEncoder->pSbrPayload->data[nPayload])) {
1913 err = AACENC_ENCODE_ERROR;
1914 goto bail;
1915 } else {
1916 /* Add SBR extension payload */
1917 for (i = 0; i < (8); i++) {
1918 if (hAacEncoder->pSbrPayload->dataSize[nPayload][i] > 0) {
1919 hAacEncoder->extPayload[nExtensions].pData =
1920 hAacEncoder->pSbrPayload->data[nPayload][i];
1921 {
1922 hAacEncoder->extPayload[nExtensions].dataSize =
1923 hAacEncoder->pSbrPayload->dataSize[nPayload][i];
1924 hAacEncoder->extPayload[nExtensions].associatedChElement = i;
1925 }
1926 hAacEncoder->extPayload[nExtensions].dataType =
1927 EXT_SBR_DATA; /* Once SBR Encoder supports SBR CRC set
1928 EXT_SBR_DATA_CRC */
1929 nExtensions++; /* or EXT_SBR_DATA according to configuration. */
1930 FDK_ASSERT(nExtensions <= MAX_TOTAL_EXT_PAYLOADS);
1931 }
1932 }
1933 nPayload++;
1934 }
1935 } /* sbrEnabled */
1936
1937 if ((inargs->numAncBytes > 0) &&
1938 (getBufDescIdx(inBufDesc, IN_ANCILLRY_DATA) != -1)) {
1939 INT idx = getBufDescIdx(inBufDesc, IN_ANCILLRY_DATA);
1940 hAacEncoder->extPayload[nExtensions].dataSize = inargs->numAncBytes * 8;
1941 hAacEncoder->extPayload[nExtensions].pData = (UCHAR *)inBufDesc->bufs[idx];
1942 hAacEncoder->extPayload[nExtensions].dataType = EXT_DATA_ELEMENT;
1943 hAacEncoder->extPayload[nExtensions].associatedChElement = -1;
1944 ancDataExtIdx = nExtensions; /* store index */
1945 nExtensions++;
1946 }
1947
1948 /*
1949 * Encode AAC - Core.
1950 */
1951 if (FDKaacEnc_EncodeFrame(hAacEncoder->hAacEnc, hAacEncoder->hTpEnc,
1952 hAacEncoder->inputBuffer,
1953 hAacEncoder->inputBufferSizePerChannel, outBytes,
1954 hAacEncoder->extPayload) != AAC_ENC_OK) {
1955 err = AACENC_ENCODE_ERROR;
1956 goto bail;
1957 }
1958
1959 if (ancDataExtIdx >= 0) {
1960 outargs->numAncBytes =
1961 inargs->numAncBytes -
1962 (hAacEncoder->extPayload[ancDataExtIdx].dataSize >> 3);
1963 }
1964
1965 /* samples exhausted */
1966 hAacEncoder->nSamplesRead -= hAacEncoder->nSamplesToRead;
1967
1968 /*
1969 * Delay balancing buffer handling
1970 */
1971 if (isSbrActive(&hAacEncoder->aacConfig)) {
1972 sbrEncoder_UpdateBuffers(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer,
1973 hAacEncoder->inputBufferSizePerChannel);
1974 }
1975
1976 /*
1977 * Make bitstream public
1978 */
1979 if ((outBufDesc != NULL) && (outBufDesc->numBufs >= 1)) {
1980 INT bsIdx = getBufDescIdx(outBufDesc, OUT_BITSTREAM_DATA);
1981 INT auIdx = getBufDescIdx(outBufDesc, OUT_AU_SIZES);
1982
1983 for (i = 0, nBsBytes = 0; i < hAacEncoder->aacConfig.nSubFrames; i++) {
1984 nBsBytes += outBytes[i];
1985
1986 if (auIdx != -1) {
1987 ((INT *)outBufDesc->bufs[auIdx])[i] = outBytes[i];
1988 }
1989 }
1990
1991 if ((bsIdx != -1) && (outBufDesc->bufSizes[bsIdx] >= nBsBytes)) {
1992 FDKmemcpy(outBufDesc->bufs[bsIdx], hAacEncoder->outBuffer,
1993 sizeof(UCHAR) * nBsBytes);
1994 outargs->numOutBytes = nBsBytes;
1995 outargs->bitResState =
1996 FDKaacEnc_GetBitReservoirState(hAacEncoder->hAacEnc);
1997 } else {
1998 /* output buffer too small, can't write valid bitstream */
1999 err = AACENC_ENCODE_ERROR;
2000 goto bail;
2001 }
2002 }
2003
2004 bail:
2005 if (err == AACENC_ENCODE_ERROR) {
2006 /* All encoder modules have to be initialized */
2007 hAacEncoder->InitFlags = AACENC_INIT_ALL;
2008 }
2009
2010 return err;
2011 }
2012
aacEncGetConf(HANDLE_AACENCODER hAacEncoder,UINT * size,UCHAR * confBuffer)2013 static AAC_ENCODER_ERROR aacEncGetConf(HANDLE_AACENCODER hAacEncoder,
2014 UINT *size, UCHAR *confBuffer) {
2015 FDK_BITSTREAM tmpConf;
2016 UINT confType;
2017 UCHAR buf[64];
2018 int err;
2019
2020 /* Init bit buffer */
2021 FDKinitBitStream(&tmpConf, buf, 64, 0, BS_WRITER);
2022
2023 /* write conf in tmp buffer */
2024 err = transportEnc_GetConf(hAacEncoder->hTpEnc, &hAacEncoder->coderConfig,
2025 &tmpConf, &confType);
2026
2027 /* copy data to outbuffer: length in bytes */
2028 FDKbyteAlign(&tmpConf, 0);
2029
2030 /* Check buffer size */
2031 if (FDKgetValidBits(&tmpConf) > ((*size) << 3)) return AAC_ENC_UNKNOWN;
2032
2033 FDKfetchBuffer(&tmpConf, confBuffer, size);
2034
2035 if (err != 0)
2036 return AAC_ENC_UNKNOWN;
2037 else
2038 return AAC_ENC_OK;
2039 }
2040
aacEncGetLibInfo(LIB_INFO * info)2041 AACENC_ERROR aacEncGetLibInfo(LIB_INFO *info) {
2042 int i = 0;
2043
2044 if (info == NULL) {
2045 return AACENC_INVALID_HANDLE;
2046 }
2047
2048 FDK_toolsGetLibInfo(info);
2049 transportEnc_GetLibInfo(info);
2050 sbrEncoder_GetLibInfo(info);
2051 FDK_MpegsEnc_GetLibInfo(info);
2052
2053 /* search for next free tab */
2054 for (i = 0; i < FDK_MODULE_LAST; i++) {
2055 if (info[i].module_id == FDK_NONE) break;
2056 }
2057 if (i == FDK_MODULE_LAST) {
2058 return AACENC_INIT_ERROR;
2059 }
2060
2061 info[i].module_id = FDK_AACENC;
2062 info[i].build_date = AACENCODER_LIB_BUILD_DATE;
2063 info[i].build_time = AACENCODER_LIB_BUILD_TIME;
2064 info[i].title = AACENCODER_LIB_TITLE;
2065 info[i].version =
2066 LIB_VERSION(AACENCODER_LIB_VL0, AACENCODER_LIB_VL1, AACENCODER_LIB_VL2);
2067 ;
2068 LIB_VERSION_STRING(&info[i]);
2069
2070 /* Capability flags */
2071 info[i].flags = 0 | CAPF_AAC_1024 | CAPF_AAC_LC | CAPF_AAC_512 |
2072 CAPF_AAC_480 | CAPF_AAC_DRC | CAPF_AAC_ELD_DOWNSCALE;
2073 /* End of flags */
2074
2075 return AACENC_OK;
2076 }
2077
aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder,const AACENC_PARAM param,const UINT value)2078 AACENC_ERROR aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder,
2079 const AACENC_PARAM param, const UINT value) {
2080 AACENC_ERROR err = AACENC_OK;
2081 USER_PARAM *settings = &hAacEncoder->extParam;
2082
2083 /* check encoder handle */
2084 if (hAacEncoder == NULL) {
2085 err = AACENC_INVALID_HANDLE;
2086 goto bail;
2087 }
2088
2089 /* apply param value */
2090 switch (param) {
2091 case AACENC_AOT:
2092 if (settings->userAOT != (AUDIO_OBJECT_TYPE)value) {
2093 /* check if AOT matches the allocated modules */
2094 switch (value) {
2095 case AOT_PS:
2096 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_PS))) {
2097 err = AACENC_INVALID_CONFIG;
2098 goto bail;
2099 }
2100 FDK_FALLTHROUGH;
2101 case AOT_SBR:
2102 case AOT_MP2_SBR:
2103 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR))) {
2104 err = AACENC_INVALID_CONFIG;
2105 goto bail;
2106 }
2107 FDK_FALLTHROUGH;
2108 case AOT_AAC_LC:
2109 case AOT_MP2_AAC_LC:
2110 case AOT_ER_AAC_LD:
2111 case AOT_ER_AAC_ELD:
2112 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_AAC))) {
2113 err = AACENC_INVALID_CONFIG;
2114 goto bail;
2115 }
2116 break;
2117 default:
2118 err = AACENC_INVALID_CONFIG;
2119 goto bail;
2120 } /* switch value */
2121 settings->userAOT = (AUDIO_OBJECT_TYPE)value;
2122 hAacEncoder->InitFlags |=
2123 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2124 }
2125 break;
2126 case AACENC_BITRATE:
2127 if (settings->userBitrate != value) {
2128 settings->userBitrate = value;
2129 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2130 }
2131 break;
2132 case AACENC_BITRATEMODE:
2133 if (settings->userBitrateMode != value) {
2134 switch (value) {
2135 case 0:
2136 case 1:
2137 case 2:
2138 case 3:
2139 case 4:
2140 case 5:
2141 settings->userBitrateMode = value;
2142 hAacEncoder->InitFlags |=
2143 AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2144 break;
2145 default:
2146 err = AACENC_INVALID_CONFIG;
2147 break;
2148 } /* switch value */
2149 }
2150 break;
2151 case AACENC_SAMPLERATE:
2152 if (settings->userSamplerate != value) {
2153 if (!((value == 8000) || (value == 11025) || (value == 12000) ||
2154 (value == 16000) || (value == 22050) || (value == 24000) ||
2155 (value == 32000) || (value == 44100) || (value == 48000) ||
2156 (value == 64000) || (value == 88200) || (value == 96000))) {
2157 err = AACENC_INVALID_CONFIG;
2158 break;
2159 }
2160 settings->userSamplerate = value;
2161 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2162 hAacEncoder->InitFlags |=
2163 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2164 }
2165 break;
2166 case AACENC_CHANNELMODE:
2167 if (settings->userChannelMode != (CHANNEL_MODE)value) {
2168 if (((CHANNEL_MODE)value == MODE_212) &&
2169 (NULL != hAacEncoder->hMpsEnc)) {
2170 settings->userChannelMode = (CHANNEL_MODE)value;
2171 settings->nChannels = 2;
2172 } else {
2173 const CHANNEL_MODE_CONFIG_TAB *pConfig =
2174 FDKaacEnc_GetChannelModeConfiguration((CHANNEL_MODE)value);
2175 if (pConfig == NULL) {
2176 err = AACENC_INVALID_CONFIG;
2177 break;
2178 }
2179 if ((pConfig->nElements > hAacEncoder->nMaxAacElements) ||
2180 (pConfig->nChannelsEff > hAacEncoder->nMaxAacChannels)) {
2181 err = AACENC_INVALID_CONFIG;
2182 break;
2183 }
2184
2185 settings->userChannelMode = (CHANNEL_MODE)value;
2186 settings->nChannels = pConfig->nChannels;
2187 }
2188 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2189 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2190 if (!((value >= 1) && (value <= 6))) {
2191 hAacEncoder->InitFlags |= AACENC_INIT_STATES;
2192 }
2193 }
2194 break;
2195 case AACENC_BANDWIDTH:
2196 if (settings->userBandwidth != value) {
2197 settings->userBandwidth = value;
2198 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2199 }
2200 break;
2201 case AACENC_CHANNELORDER:
2202 if (hAacEncoder->aacConfig.channelOrder != (CHANNEL_ORDER)value) {
2203 if (!((value == 0) || (value == 1))) {
2204 err = AACENC_INVALID_CONFIG;
2205 break;
2206 }
2207 hAacEncoder->aacConfig.channelOrder = (CHANNEL_ORDER)value;
2208 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2209 hAacEncoder->InitFlags |=
2210 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2211 }
2212 break;
2213 case AACENC_AFTERBURNER:
2214 if (settings->userAfterburner != value) {
2215 if (!((value == 0) || (value == 1))) {
2216 err = AACENC_INVALID_CONFIG;
2217 break;
2218 }
2219 settings->userAfterburner = value;
2220 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2221 }
2222 break;
2223 case AACENC_GRANULE_LENGTH:
2224 if (settings->userFramelength != value) {
2225 switch (value) {
2226 case 1024:
2227 case 512:
2228 case 480:
2229 case 256:
2230 case 240:
2231 case 128:
2232 case 120:
2233 if ((value << 1) == 480 || (value << 1) == 512) {
2234 settings->userDownscaleFactor = 2;
2235 } else if ((value << 2) == 480 || (value << 2) == 512) {
2236 settings->userDownscaleFactor = 4;
2237 }
2238 settings->userFramelength = value;
2239 hAacEncoder->InitFlags |=
2240 AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2241 break;
2242 default:
2243 err = AACENC_INVALID_CONFIG;
2244 break;
2245 }
2246 }
2247 break;
2248 case AACENC_SBR_RATIO:
2249 if (settings->userSbrRatio != value) {
2250 if (!((value == 0) || (value == 1) || (value == 2))) {
2251 err = AACENC_INVALID_CONFIG;
2252 break;
2253 }
2254 settings->userSbrRatio = value;
2255 hAacEncoder->InitFlags |=
2256 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2257 }
2258 break;
2259 case AACENC_SBR_MODE:
2260 if ((settings->userSbrEnabled != value) &&
2261 (NULL != hAacEncoder->hEnvEnc)) {
2262 settings->userSbrEnabled = value;
2263 hAacEncoder->InitFlags |=
2264 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2265 }
2266 break;
2267 case AACENC_TRANSMUX:
2268 if (settings->userTpType != (TRANSPORT_TYPE)value) {
2269 TRANSPORT_TYPE type = (TRANSPORT_TYPE)value;
2270 UINT flags = hAacEncoder->CAPF_tpEnc;
2271
2272 if (!(((type == TT_MP4_ADIF) && (flags & CAPF_ADIF)) ||
2273 ((type == TT_MP4_ADTS) && (flags & CAPF_ADTS)) ||
2274 ((type == TT_MP4_LATM_MCP0) &&
2275 ((flags & CAPF_LATM) && (flags & CAPF_RAWPACKETS))) ||
2276 ((type == TT_MP4_LATM_MCP1) &&
2277 ((flags & CAPF_LATM) && (flags & CAPF_RAWPACKETS))) ||
2278 ((type == TT_MP4_LOAS) && (flags & CAPF_LOAS)) ||
2279 ((type == TT_MP4_RAW) && (flags & CAPF_RAWPACKETS)))) {
2280 err = AACENC_INVALID_CONFIG;
2281 break;
2282 }
2283 settings->userTpType = (TRANSPORT_TYPE)value;
2284 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2285 }
2286 break;
2287 case AACENC_SIGNALING_MODE:
2288 if (settings->userTpSignaling != value) {
2289 if (!((value == 0) || (value == 1) || (value == 2))) {
2290 err = AACENC_INVALID_CONFIG;
2291 break;
2292 }
2293 settings->userTpSignaling = value;
2294 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2295 }
2296 break;
2297 case AACENC_PROTECTION:
2298 if (settings->userTpProtection != value) {
2299 if (!((value == 0) || (value == 1))) {
2300 err = AACENC_INVALID_CONFIG;
2301 break;
2302 }
2303 settings->userTpProtection = value;
2304 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2305 }
2306 break;
2307 case AACENC_HEADER_PERIOD:
2308 if (settings->userTpHeaderPeriod != value) {
2309 if (!(((INT)value >= 0) && (value <= 255))) {
2310 err = AACENC_INVALID_CONFIG;
2311 break;
2312 }
2313 settings->userTpHeaderPeriod = value;
2314 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2315 }
2316 break;
2317 case AACENC_AUDIOMUXVER:
2318 if (settings->userTpAmxv != value) {
2319 if (!((value == 0) || (value == 1) || (value == 2))) {
2320 err = AACENC_INVALID_CONFIG;
2321 break;
2322 }
2323 settings->userTpAmxv = value;
2324 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2325 }
2326 break;
2327 case AACENC_TPSUBFRAMES:
2328 if (settings->userTpNsubFrames != value) {
2329 if (!((value >= 1) && (value <= 4))) {
2330 err = AACENC_INVALID_CONFIG;
2331 break;
2332 }
2333 settings->userTpNsubFrames = value;
2334 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2335 }
2336 break;
2337 case AACENC_ANCILLARY_BITRATE:
2338 if (settings->userAncDataRate != value) {
2339 settings->userAncDataRate = value;
2340 }
2341 break;
2342 case AACENC_CONTROL_STATE:
2343 if (hAacEncoder->InitFlags != value) {
2344 if (value & AACENC_RESET_INBUFFER) {
2345 hAacEncoder->nSamplesRead = 0;
2346 }
2347 hAacEncoder->InitFlags = value;
2348 }
2349 break;
2350 case AACENC_METADATA_MODE:
2351 if ((UINT)settings->userMetaDataMode != value) {
2352 if (!(((INT)value >= 0) && ((INT)value <= 3))) {
2353 err = AACENC_INVALID_CONFIG;
2354 break;
2355 }
2356 settings->userMetaDataMode = value;
2357 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2358 }
2359 break;
2360 case AACENC_PEAK_BITRATE:
2361 if (settings->userPeakBitrate != value) {
2362 settings->userPeakBitrate = value;
2363 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2364 }
2365 break;
2366 default:
2367 err = AACENC_UNSUPPORTED_PARAMETER;
2368 break;
2369 } /* switch(param) */
2370
2371 bail:
2372 return err;
2373 }
2374
aacEncoder_GetParam(const HANDLE_AACENCODER hAacEncoder,const AACENC_PARAM param)2375 UINT aacEncoder_GetParam(const HANDLE_AACENCODER hAacEncoder,
2376 const AACENC_PARAM param) {
2377 UINT value = 0;
2378 USER_PARAM *settings = &hAacEncoder->extParam;
2379
2380 /* check encoder handle */
2381 if (hAacEncoder == NULL) {
2382 goto bail;
2383 }
2384
2385 /* apply param value */
2386 switch (param) {
2387 case AACENC_AOT:
2388 value = (UINT)hAacEncoder->aacConfig.audioObjectType;
2389 break;
2390 case AACENC_BITRATE:
2391 switch (hAacEncoder->aacConfig.bitrateMode) {
2392 case AACENC_BR_MODE_CBR:
2393 value = (UINT)hAacEncoder->aacConfig.bitRate;
2394 break;
2395 default:
2396 value = (UINT)-1;
2397 }
2398 break;
2399 case AACENC_BITRATEMODE:
2400 value = (UINT)((hAacEncoder->aacConfig.bitrateMode != AACENC_BR_MODE_FF)
2401 ? hAacEncoder->aacConfig.bitrateMode
2402 : AACENC_BR_MODE_CBR);
2403 break;
2404 case AACENC_SAMPLERATE:
2405 value = (UINT)hAacEncoder->coderConfig.extSamplingRate;
2406 break;
2407 case AACENC_CHANNELMODE:
2408 if ((MODE_1 == hAacEncoder->aacConfig.channelMode) &&
2409 (hAacEncoder->aacConfig.syntaxFlags & AC_LD_MPS)) {
2410 value = MODE_212;
2411 } else {
2412 value = (UINT)hAacEncoder->aacConfig.channelMode;
2413 }
2414 break;
2415 case AACENC_BANDWIDTH:
2416 value = (UINT)hAacEncoder->aacConfig.bandWidth;
2417 break;
2418 case AACENC_CHANNELORDER:
2419 value = (UINT)hAacEncoder->aacConfig.channelOrder;
2420 break;
2421 case AACENC_AFTERBURNER:
2422 value = (UINT)hAacEncoder->aacConfig.useRequant;
2423 break;
2424 case AACENC_GRANULE_LENGTH:
2425 value = (UINT)hAacEncoder->aacConfig.framelength;
2426 break;
2427 case AACENC_SBR_RATIO:
2428 value = isSbrActive(&hAacEncoder->aacConfig)
2429 ? hAacEncoder->aacConfig.sbrRatio
2430 : 0;
2431 break;
2432 case AACENC_SBR_MODE:
2433 value =
2434 (UINT)(hAacEncoder->aacConfig.syntaxFlags & AC_SBR_PRESENT) ? 1 : 0;
2435 break;
2436 case AACENC_TRANSMUX:
2437 value = (UINT)settings->userTpType;
2438 break;
2439 case AACENC_SIGNALING_MODE:
2440 value = (UINT)getSbrSignalingMode(
2441 hAacEncoder->aacConfig.audioObjectType, settings->userTpType,
2442 settings->userTpSignaling, hAacEncoder->aacConfig.sbrRatio);
2443 break;
2444 case AACENC_PROTECTION:
2445 value = (UINT)settings->userTpProtection;
2446 break;
2447 case AACENC_HEADER_PERIOD:
2448 value = (UINT)hAacEncoder->coderConfig.headerPeriod;
2449 break;
2450 case AACENC_AUDIOMUXVER:
2451 value = (UINT)hAacEncoder->aacConfig.audioMuxVersion;
2452 break;
2453 case AACENC_TPSUBFRAMES:
2454 value = (UINT)settings->userTpNsubFrames;
2455 break;
2456 case AACENC_ANCILLARY_BITRATE:
2457 value = (UINT)hAacEncoder->aacConfig.anc_Rate;
2458 break;
2459 case AACENC_CONTROL_STATE:
2460 value = (UINT)hAacEncoder->InitFlags;
2461 break;
2462 case AACENC_METADATA_MODE:
2463 value = (hAacEncoder->metaDataAllowed == 0)
2464 ? 0
2465 : (UINT)settings->userMetaDataMode;
2466 break;
2467 case AACENC_PEAK_BITRATE:
2468 value = (UINT)-1; /* peak bitrate parameter is meaningless */
2469 if (((INT)hAacEncoder->extParam.userPeakBitrate != -1)) {
2470 value =
2471 (UINT)(fMax((INT)hAacEncoder->extParam.userPeakBitrate,
2472 hAacEncoder->aacConfig
2473 .bitRate)); /* peak bitrate parameter is in use */
2474 }
2475 break;
2476
2477 default:
2478 // err = MPS_INVALID_PARAMETER;
2479 break;
2480 } /* switch(param) */
2481
2482 bail:
2483 return value;
2484 }
2485
aacEncInfo(const HANDLE_AACENCODER hAacEncoder,AACENC_InfoStruct * pInfo)2486 AACENC_ERROR aacEncInfo(const HANDLE_AACENCODER hAacEncoder,
2487 AACENC_InfoStruct *pInfo) {
2488 AACENC_ERROR err = AACENC_OK;
2489
2490 FDKmemclear(pInfo, sizeof(AACENC_InfoStruct));
2491 pInfo->confSize = 64; /* pre-initialize */
2492
2493 pInfo->maxOutBufBytes = ((hAacEncoder->nMaxAacChannels * 6144) + 7) >> 3;
2494 pInfo->maxAncBytes = hAacEncoder->aacConfig.maxAncBytesPerAU;
2495 pInfo->inBufFillLevel =
2496 hAacEncoder->nSamplesRead / hAacEncoder->extParam.nChannels;
2497 pInfo->inputChannels = hAacEncoder->extParam.nChannels;
2498 pInfo->frameLength =
2499 hAacEncoder->nSamplesToRead / hAacEncoder->extParam.nChannels;
2500 pInfo->nDelay = hAacEncoder->nDelay;
2501 pInfo->nDelayCore = hAacEncoder->nDelayCore;
2502
2503 /* Get encoder configuration */
2504 if (aacEncGetConf(hAacEncoder, &pInfo->confSize, &pInfo->confBuf[0]) !=
2505 AAC_ENC_OK) {
2506 err = AACENC_INIT_ERROR;
2507 goto bail;
2508 }
2509 bail:
2510 return err;
2511 }
2512