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 1
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
getAssociatedChElement(SBR_ELEMENT_INFO * elInfoSbr,CHANNEL_MAPPING * channelMapping)449 static inline INT getAssociatedChElement(SBR_ELEMENT_INFO *elInfoSbr,
450 CHANNEL_MAPPING *channelMapping) {
451 ELEMENT_INFO *elInfo = channelMapping->elInfo;
452 INT nElements = channelMapping->nElements;
453 INT associatedChElement = -1;
454 int i;
455
456 for (i = 0; i < nElements; i++) {
457 if (elInfoSbr->elType == elInfo[i].elType &&
458 elInfoSbr->instanceTag == elInfo[i].instanceTag) {
459 associatedChElement = i;
460 break;
461 }
462 }
463
464 return associatedChElement;
465 }
466
467 /****************************************************************************
468 Allocate Encoder
469 ****************************************************************************/
470
H_ALLOC_MEM(_AacEncoder,AACENCODER)471 H_ALLOC_MEM(_AacEncoder, AACENCODER)
472 C_ALLOC_MEM(_AacEncoder, struct AACENCODER, 1)
473
474 /*
475 * Map Encoder specific config structures to CODER_CONFIG.
476 */
477 static void FDKaacEnc_MapConfig(CODER_CONFIG *const cc,
478 const USER_PARAM *const extCfg,
479 const SBR_PS_SIGNALING sbrSignaling,
480 const HANDLE_AACENC_CONFIG hAacConfig) {
481 AUDIO_OBJECT_TYPE transport_AOT = AOT_NULL_OBJECT;
482 FDKmemclear(cc, sizeof(CODER_CONFIG));
483
484 cc->flags = 0;
485
486 cc->samplesPerFrame = hAacConfig->framelength;
487 cc->samplingRate = hAacConfig->sampleRate;
488 cc->extSamplingRate = extCfg->userSamplerate;
489
490 /* Map virtual aot to transport aot. */
491 switch (hAacConfig->audioObjectType) {
492 case AOT_MP2_AAC_LC:
493 transport_AOT = AOT_AAC_LC;
494 break;
495 case AOT_MP2_SBR:
496 transport_AOT = AOT_SBR;
497 cc->flags |= CC_SBR;
498 break;
499 default:
500 transport_AOT = hAacConfig->audioObjectType;
501 }
502
503 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) {
504 cc->flags |= (hAacConfig->syntaxFlags & AC_SBR_PRESENT) ? CC_SBR : 0;
505 cc->flags |= (hAacConfig->syntaxFlags & AC_LD_MPS) ? CC_SAC : 0;
506 }
507
508 /* transport type is usually AAC-LC. */
509 if ((transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS)) {
510 cc->aot = AOT_AAC_LC;
511 } else {
512 cc->aot = transport_AOT;
513 }
514
515 /* Configure extension aot. */
516 if (sbrSignaling == SIG_IMPLICIT) {
517 cc->extAOT = AOT_NULL_OBJECT; /* implicit */
518 } else {
519 if ((sbrSignaling == SIG_EXPLICIT_BW_COMPATIBLE) &&
520 ((transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS))) {
521 cc->extAOT = AOT_SBR; /* explicit backward compatible */
522 } else {
523 cc->extAOT = transport_AOT; /* explicit hierarchical */
524 }
525 }
526
527 if ((transport_AOT == AOT_SBR) || (transport_AOT == AOT_PS)) {
528 cc->sbrPresent = 1;
529 if (transport_AOT == AOT_PS) {
530 cc->psPresent = 1;
531 }
532 }
533 cc->sbrSignaling = sbrSignaling;
534
535 if (hAacConfig->downscaleFactor > 1) {
536 cc->downscaleSamplingRate = cc->samplingRate;
537 cc->samplingRate *= hAacConfig->downscaleFactor;
538 cc->extSamplingRate *= hAacConfig->downscaleFactor;
539 }
540
541 cc->bitRate = hAacConfig->bitRate;
542 cc->noChannels = hAacConfig->nChannels;
543 cc->flags |= CC_IS_BASELAYER;
544 cc->channelMode = hAacConfig->channelMode;
545
546 cc->nSubFrames = (hAacConfig->nSubFrames > 1 && extCfg->userTpNsubFrames == 1)
547 ? hAacConfig->nSubFrames
548 : extCfg->userTpNsubFrames;
549
550 cc->flags |= (extCfg->userTpProtection) ? CC_PROTECTION : 0;
551
552 if (extCfg->userTpHeaderPeriod != 0xFF) {
553 cc->headerPeriod = extCfg->userTpHeaderPeriod;
554 } else { /* auto-mode */
555 switch (extCfg->userTpType) {
556 case TT_MP4_ADTS:
557 case TT_MP4_LOAS:
558 case TT_MP4_LATM_MCP1:
559 cc->headerPeriod = DEFAULT_HEADER_PERIOD_REPETITION_RATE;
560 break;
561 default:
562 cc->headerPeriod = 0;
563 }
564 }
565
566 /* Mpeg-4 signaling for transport library. */
567 switch (hAacConfig->audioObjectType) {
568 case AOT_MP2_AAC_LC:
569 case AOT_MP2_SBR:
570 cc->flags &= ~CC_MPEG_ID; /* Required for ADTS. */
571 cc->extAOT = AOT_NULL_OBJECT;
572 break;
573 default:
574 cc->flags |= CC_MPEG_ID;
575 }
576
577 /* ER-tools signaling. */
578 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_VCB11) ? CC_VCB11 : 0;
579 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_HCR) ? CC_HCR : 0;
580 cc->flags |= (hAacConfig->syntaxFlags & AC_ER_RVLC) ? CC_RVLC : 0;
581
582 /* Matrix mixdown coefficient configuration. */
583 if ((extCfg->userPceAdditions & 0x1) && (hAacConfig->epConfig == -1) &&
584 ((cc->channelMode == MODE_1_2_2) || (cc->channelMode == MODE_1_2_2_1))) {
585 cc->matrixMixdownA = ((extCfg->userPceAdditions >> 1) & 0x3) + 1;
586 cc->flags |= (extCfg->userPceAdditions >> 3) & 0x1 ? CC_PSEUDO_SURROUND : 0;
587 } else {
588 cc->matrixMixdownA = 0;
589 }
590
591 cc->channelConfigZero = 0;
592 }
593
594 /*
595 * Validate prefilled pointers within buffer descriptor.
596 *
597 * \param pBufDesc Pointer to buffer descriptor
598
599 * \return - AACENC_OK, all fine.
600 * - AACENC_INVALID_HANDLE, on missing pointer initializiation.
601 * - AACENC_UNSUPPORTED_PARAMETER, on incorrect buffer descriptor
602 initialization.
603 */
validateBufDesc(const AACENC_BufDesc * pBufDesc)604 static AACENC_ERROR validateBufDesc(const AACENC_BufDesc *pBufDesc) {
605 AACENC_ERROR err = AACENC_OK;
606
607 if (pBufDesc != NULL) {
608 int i;
609 if ((pBufDesc->bufferIdentifiers == NULL) || (pBufDesc->bufSizes == NULL) ||
610 (pBufDesc->bufElSizes == NULL) || (pBufDesc->bufs == NULL)) {
611 err = AACENC_UNSUPPORTED_PARAMETER;
612 goto bail;
613 }
614 for (i = 0; i < pBufDesc->numBufs; i++) {
615 if (pBufDesc->bufs[i] == NULL) {
616 err = AACENC_UNSUPPORTED_PARAMETER;
617 goto bail;
618 }
619 }
620 } else {
621 err = AACENC_INVALID_HANDLE;
622 }
623 bail:
624 return err;
625 }
626
627 /*
628 * Examine buffer descriptor regarding choosen identifier.
629 *
630 * \param pBufDesc Pointer to buffer descriptor
631 * \param identifier Buffer identifier to look for.
632
633 * \return - Buffer descriptor index.
634 * -1, if there is no entry available.
635 */
getBufDescIdx(const AACENC_BufDesc * pBufDesc,const AACENC_BufferIdentifier identifier)636 static INT getBufDescIdx(const AACENC_BufDesc *pBufDesc,
637 const AACENC_BufferIdentifier identifier) {
638 INT i, idx = -1;
639
640 if (pBufDesc != NULL) {
641 for (i = 0; i < pBufDesc->numBufs; i++) {
642 if ((AACENC_BufferIdentifier)pBufDesc->bufferIdentifiers[i] ==
643 identifier) {
644 idx = i;
645 break;
646 }
647 }
648 }
649 return idx;
650 }
651
652 /****************************************************************************
653 Function Declarations
654 ****************************************************************************/
655
aacEncDefaultConfig(HANDLE_AACENC_CONFIG hAacConfig,USER_PARAM * config)656 AAC_ENCODER_ERROR aacEncDefaultConfig(HANDLE_AACENC_CONFIG hAacConfig,
657 USER_PARAM *config) {
658 /* make reasonable default settings */
659 FDKaacEnc_AacInitDefaultConfig(hAacConfig);
660
661 /* clear configuration structure and copy default settings */
662 FDKmemclear(config, sizeof(USER_PARAM));
663
664 /* copy encoder configuration settings */
665 config->nChannels = hAacConfig->nChannels;
666 config->userAOT = hAacConfig->audioObjectType = AOT_AAC_LC;
667 config->userSamplerate = hAacConfig->sampleRate;
668 config->userChannelMode = hAacConfig->channelMode;
669 config->userBitrate = hAacConfig->bitRate;
670 config->userBitrateMode = hAacConfig->bitrateMode;
671 config->userPeakBitrate = (UINT)-1;
672 config->userBandwidth = hAacConfig->bandWidth;
673 config->userTns = hAacConfig->useTns;
674 config->userPns = hAacConfig->usePns;
675 config->userIntensity = hAacConfig->useIS;
676 config->userAfterburner = hAacConfig->useRequant;
677 config->userFramelength = (UINT)-1;
678
679 config->userDownscaleFactor = 1;
680
681 /* initialize transport parameters */
682 config->userTpType = TT_UNKNOWN;
683 config->userTpAmxv = 0;
684 config->userTpSignaling = 0xFF; /* choose signaling automatically */
685 config->userTpNsubFrames = 1;
686 config->userTpProtection = 0; /* not crc protected*/
687 config->userTpHeaderPeriod = 0xFF; /* header period in auto mode */
688 config->userPceAdditions = 0; /* no matrix mixdown coefficient */
689 config->userMetaDataMode = 0; /* do not embed any meta data info */
690
691 config->userAncDataRate = 0;
692
693 /* SBR rate is set to 0 here, which means it should be set automatically
694 in FDKaacEnc_AdjustEncSettings() if the user did not set a rate
695 expilicitely. */
696 config->userSbrRatio = 0;
697
698 /* SBR enable set to -1 means to inquire ELD audio configurator for reasonable
699 * configuration. */
700 config->userSbrEnabled = (UCHAR)-1;
701
702 return AAC_ENC_OK;
703 }
704
aacEncDistributeSbrBits(CHANNEL_MAPPING * channelMapping,SBR_ELEMENT_INFO * sbrElInfo,INT bitRate)705 static void aacEncDistributeSbrBits(CHANNEL_MAPPING *channelMapping,
706 SBR_ELEMENT_INFO *sbrElInfo, INT bitRate) {
707 INT codebits = bitRate;
708 int el;
709
710 /* Copy Element info */
711 for (el = 0; el < channelMapping->nElements; el++) {
712 sbrElInfo[el].ChannelIndex[0] = channelMapping->elInfo[el].ChannelIndex[0];
713 sbrElInfo[el].ChannelIndex[1] = channelMapping->elInfo[el].ChannelIndex[1];
714 sbrElInfo[el].elType = channelMapping->elInfo[el].elType;
715 sbrElInfo[el].bitRate =
716 fMultIfloor(channelMapping->elInfo[el].relativeBits, bitRate);
717 sbrElInfo[el].instanceTag = channelMapping->elInfo[el].instanceTag;
718 sbrElInfo[el].nChannelsInEl = channelMapping->elInfo[el].nChannelsInEl;
719 sbrElInfo[el].fParametricStereo = 0;
720 sbrElInfo[el].fDualMono = 0;
721
722 codebits -= sbrElInfo[el].bitRate;
723 }
724 sbrElInfo[0].bitRate += codebits;
725 }
726
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)727 static INT aacEncoder_LimitBitrate(const HANDLE_TRANSPORTENC hTpEnc,
728 const INT samplingRate,
729 const INT frameLength, const INT nChannels,
730 const CHANNEL_MODE channelMode, INT bitRate,
731 const INT nSubFrames, const INT sbrActive,
732 const INT sbrDownSampleRate,
733 const UINT syntaxFlags,
734 const AUDIO_OBJECT_TYPE aot) {
735 INT coreSamplingRate;
736 CHANNEL_MAPPING cm;
737
738 FDKaacEnc_InitChannelMapping(channelMode, CH_ORDER_MPEG, &cm);
739
740 if (sbrActive) {
741 coreSamplingRate =
742 samplingRate >>
743 (sbrEncoder_IsSingleRatePossible(aot) ? (sbrDownSampleRate - 1) : 1);
744 } else {
745 coreSamplingRate = samplingRate;
746 }
747
748 /* Limit bit rate in respect to the core coder */
749 bitRate = FDKaacEnc_LimitBitrate(hTpEnc, aot, coreSamplingRate, frameLength,
750 nChannels, cm.nChannelsEff, bitRate, -1,
751 NULL, AACENC_BR_MODE_INVALID, nSubFrames);
752
753 /* Limit bit rate in respect to available SBR modes if active */
754 if (sbrActive) {
755 int numIterations = 0;
756 INT initialBitrate, adjustedBitrate;
757 adjustedBitrate = bitRate;
758
759 /* Find total bitrate which provides valid configuration for each SBR
760 * element. */
761 do {
762 int e;
763 SBR_ELEMENT_INFO sbrElInfo[((8))];
764 FDK_ASSERT(cm.nElements <= ((8)));
765
766 initialBitrate = adjustedBitrate;
767
768 /* Get bit rate for each SBR element */
769 aacEncDistributeSbrBits(&cm, sbrElInfo, initialBitrate);
770
771 for (e = 0; e < cm.nElements; e++) {
772 INT sbrElementBitRateIn, sbrBitRateOut;
773
774 if (cm.elInfo[e].elType != ID_SCE && cm.elInfo[e].elType != ID_CPE) {
775 continue;
776 }
777 sbrElementBitRateIn = sbrElInfo[e].bitRate;
778
779 sbrBitRateOut = sbrEncoder_LimitBitRate(sbrElementBitRateIn,
780 cm.elInfo[e].nChannelsInEl,
781 coreSamplingRate, aot);
782
783 if (sbrBitRateOut == 0) {
784 return 0;
785 }
786
787 /* If bitrates don't match, distribution and limiting needs to be
788 determined again. Abort element loop and restart with adapted
789 bitrate. */
790 if (sbrElementBitRateIn != sbrBitRateOut) {
791 if (sbrElementBitRateIn < sbrBitRateOut) {
792 adjustedBitrate = fMax(initialBitrate,
793 (INT)fDivNorm((FIXP_DBL)(sbrBitRateOut + 8),
794 cm.elInfo[e].relativeBits));
795 break;
796 }
797
798 if (sbrElementBitRateIn > sbrBitRateOut) {
799 adjustedBitrate = fMin(initialBitrate,
800 (INT)fDivNorm((FIXP_DBL)(sbrBitRateOut - 8),
801 cm.elInfo[e].relativeBits));
802 break;
803 }
804
805 } /* sbrElementBitRateIn != sbrBitRateOut */
806
807 } /* elements */
808
809 numIterations++; /* restrict iteration to worst case of num elements */
810
811 } while ((initialBitrate != adjustedBitrate) &&
812 (numIterations <= cm.nElements));
813
814 /* Unequal bitrates mean that no reasonable bitrate configuration found. */
815 bitRate = (initialBitrate == adjustedBitrate) ? adjustedBitrate : 0;
816 }
817
818 /* Limit bit rate in respect to available MPS modes if active */
819 if ((aot == AOT_ER_AAC_ELD) && (syntaxFlags & AC_LD_MPS) &&
820 (channelMode == MODE_1)) {
821 bitRate = FDK_MpegsEnc_GetClosestBitRate(
822 aot, MODE_212, samplingRate, (sbrActive) ? sbrDownSampleRate : 0,
823 bitRate);
824 }
825
826 return bitRate;
827 }
828
829 /*
830 * \brief Get CBR bitrate
831 *
832 * \hAacConfig Internal encoder config
833 * \return Bitrate
834 */
FDKaacEnc_GetCBRBitrate(const HANDLE_AACENC_CONFIG hAacConfig,const INT userSbrRatio)835 static INT FDKaacEnc_GetCBRBitrate(const HANDLE_AACENC_CONFIG hAacConfig,
836 const INT userSbrRatio) {
837 INT bitrate = FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
838 ->nChannelsEff *
839 hAacConfig->sampleRate;
840
841 if (isPsActive(hAacConfig->audioObjectType)) {
842 bitrate = 1 * bitrate; /* 0.5 bit per sample */
843 } else if (isSbrActive(hAacConfig)) {
844 if ((userSbrRatio == 2) ||
845 ((userSbrRatio == 0) &&
846 (hAacConfig->audioObjectType != AOT_ER_AAC_ELD))) {
847 bitrate = (bitrate + (bitrate >> 2)) >> 1; /* 0.625 bits per sample */
848 }
849 if ((userSbrRatio == 1) ||
850 ((userSbrRatio == 0) &&
851 (hAacConfig->audioObjectType == AOT_ER_AAC_ELD))) {
852 bitrate = (bitrate + (bitrate >> 3)); /* 1.125 bits per sample */
853 }
854 } else {
855 bitrate = bitrate + (bitrate >> 1); /* 1.5 bits per sample */
856 }
857
858 return bitrate;
859 }
860
861 /*
862 * \brief Consistency check of given USER_PARAM struct and
863 * copy back configuration from public struct into internal
864 * encoder configuration struct.
865 *
866 * \hAacEncoder Internal encoder config which is to be updated
867 * \param config User provided config (public struct)
868 * \return returns always AAC_ENC_OK
869 */
FDKaacEnc_AdjustEncSettings(HANDLE_AACENCODER hAacEncoder,USER_PARAM * config)870 static AACENC_ERROR FDKaacEnc_AdjustEncSettings(HANDLE_AACENCODER hAacEncoder,
871 USER_PARAM *config) {
872 AACENC_ERROR err = AACENC_OK;
873
874 /* Get struct pointers. */
875 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig;
876
877 /* Encoder settings update. */
878 hAacConfig->sampleRate = config->userSamplerate;
879 if (config->userDownscaleFactor > 1) {
880 hAacConfig->useTns = 0;
881 hAacConfig->usePns = 0;
882 hAacConfig->useIS = 0;
883 } else {
884 hAacConfig->useTns = config->userTns;
885 hAacConfig->usePns = config->userPns;
886 hAacConfig->useIS = config->userIntensity;
887 }
888
889 hAacConfig->audioObjectType = config->userAOT;
890 hAacConfig->channelMode =
891 GetCoreChannelMode(config->userChannelMode, hAacConfig->audioObjectType);
892 hAacConfig->nChannels =
893 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)->nChannels;
894 hAacConfig->bitrateMode = (AACENC_BITRATE_MODE)config->userBitrateMode;
895 hAacConfig->bandWidth = config->userBandwidth;
896 hAacConfig->useRequant = config->userAfterburner;
897
898 hAacConfig->anc_Rate = config->userAncDataRate;
899 hAacConfig->syntaxFlags = 0;
900 hAacConfig->epConfig = -1;
901
902 if (hAacConfig->audioObjectType != AOT_ER_AAC_ELD &&
903 config->userDownscaleFactor > 1) {
904 return AACENC_INVALID_CONFIG; /* downscaling only allowed for AOT_ER_AAC_ELD
905 */
906 }
907 if (config->userDownscaleFactor > 1 && config->userSbrEnabled == 1) {
908 return AACENC_INVALID_CONFIG; /* downscaling only allowed for AOT_ER_AAC_ELD
909 w/o SBR */
910 }
911 if (config->userDownscaleFactor > 1 && config->userChannelMode == 128) {
912 return AACENC_INVALID_CONFIG; /* disallow downscaling for AAC-ELDv2 */
913 }
914
915 if (config->userTpType == TT_MP4_LATM_MCP1 ||
916 config->userTpType == TT_MP4_LATM_MCP0 ||
917 config->userTpType == TT_MP4_LOAS) {
918 hAacConfig->audioMuxVersion = config->userTpAmxv;
919 } else {
920 hAacConfig->audioMuxVersion = -1;
921 }
922
923 /* Adapt internal AOT when necessary. */
924 switch (config->userAOT) {
925 case AOT_MP2_AAC_LC:
926 case AOT_MP2_SBR:
927 hAacConfig->usePns = 0;
928 FDK_FALLTHROUGH;
929 case AOT_AAC_LC:
930 case AOT_SBR:
931 case AOT_PS:
932 config->userTpType =
933 (config->userTpType != TT_UNKNOWN) ? config->userTpType : TT_MP4_ADTS;
934 hAacConfig->framelength = (config->userFramelength != (UINT)-1)
935 ? config->userFramelength
936 : 1024;
937 if (hAacConfig->framelength != 1024 && hAacConfig->framelength != 960) {
938 return AACENC_INVALID_CONFIG;
939 }
940 break;
941 case AOT_ER_AAC_LD:
942 hAacConfig->epConfig = 0;
943 hAacConfig->syntaxFlags |= AC_ER | AC_LD;
944 hAacConfig->syntaxFlags |=
945 ((config->userErTools & 0x1) ? AC_ER_VCB11 : 0);
946 hAacConfig->syntaxFlags |= ((config->userErTools & 0x2) ? AC_ER_HCR : 0);
947 hAacConfig->syntaxFlags |= ((config->userErTools & 0x4) ? AC_ER_RVLC : 0);
948 config->userTpType =
949 (config->userTpType != TT_UNKNOWN) ? config->userTpType : TT_MP4_LOAS;
950 hAacConfig->framelength =
951 (config->userFramelength != (UINT)-1) ? config->userFramelength : 512;
952 if (hAacConfig->framelength != 512 && hAacConfig->framelength != 480) {
953 return AACENC_INVALID_CONFIG;
954 }
955 break;
956 case AOT_ER_AAC_ELD:
957 hAacConfig->epConfig = 0;
958 hAacConfig->syntaxFlags |= AC_ER | AC_ELD;
959 hAacConfig->syntaxFlags |=
960 ((config->userErTools & 0x1) ? AC_ER_VCB11 : 0);
961 hAacConfig->syntaxFlags |= ((config->userErTools & 0x2) ? AC_ER_HCR : 0);
962 hAacConfig->syntaxFlags |= ((config->userErTools & 0x4) ? AC_ER_RVLC : 0);
963 hAacConfig->syntaxFlags |=
964 ((config->userSbrEnabled == 1) ? AC_SBR_PRESENT : 0);
965 hAacConfig->syntaxFlags |=
966 ((config->userChannelMode == MODE_212) ? AC_LD_MPS : 0);
967 config->userTpType =
968 (config->userTpType != TT_UNKNOWN) ? config->userTpType : TT_MP4_LOAS;
969 hAacConfig->framelength =
970 (config->userFramelength != (UINT)-1) ? config->userFramelength : 512;
971
972 hAacConfig->downscaleFactor = config->userDownscaleFactor;
973
974 switch (config->userDownscaleFactor) {
975 case 1:
976 break;
977 case 2:
978 case 4:
979 hAacConfig->syntaxFlags |= AC_ELD_DOWNSCALE;
980 break;
981 default:
982 return AACENC_INVALID_CONFIG;
983 }
984
985 if (hAacConfig->framelength != 512 && hAacConfig->framelength != 480 &&
986 hAacConfig->framelength != 256 && hAacConfig->framelength != 240 &&
987 hAacConfig->framelength != 128 && hAacConfig->framelength != 120) {
988 return AACENC_INVALID_CONFIG;
989 }
990 break;
991 default:
992 break;
993 }
994
995 /* Initialize SBR parameters */
996 if ((config->userSbrRatio == 0) && (isSbrActive(hAacConfig))) {
997 /* Automatic SBR ratio configuration
998 * - downsampled SBR for ELD
999 * - otherwise always dualrate SBR
1000 */
1001 if (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) {
1002 hAacConfig->sbrRatio = ((hAacConfig->syntaxFlags & AC_LD_MPS) &&
1003 (hAacConfig->sampleRate >= 27713))
1004 ? 2
1005 : 1;
1006 } else {
1007 hAacConfig->sbrRatio = 2;
1008 }
1009 } else {
1010 /* SBR ratio has been set by the user, so use it. */
1011 hAacConfig->sbrRatio = isSbrActive(hAacConfig) ? config->userSbrRatio : 0;
1012 }
1013
1014 /* Set default bitrate */
1015 hAacConfig->bitRate = config->userBitrate;
1016
1017 switch (hAacConfig->bitrateMode) {
1018 case AACENC_BR_MODE_CBR:
1019 /* Set default bitrate if no external bitrate declared. */
1020 if (config->userBitrate == (UINT)-1) {
1021 hAacConfig->bitRate =
1022 FDKaacEnc_GetCBRBitrate(hAacConfig, config->userSbrRatio);
1023 }
1024 hAacConfig->averageBits = -1;
1025 break;
1026 case AACENC_BR_MODE_VBR_1:
1027 case AACENC_BR_MODE_VBR_2:
1028 case AACENC_BR_MODE_VBR_3:
1029 case AACENC_BR_MODE_VBR_4:
1030 case AACENC_BR_MODE_VBR_5:
1031 /* Get bitrate in VBR configuration */
1032 /* In VBR mode; SBR-modul depends on bitrate, core encoder on bitrateMode.
1033 */
1034 hAacConfig->bitRate = FDKaacEnc_GetVBRBitrate(hAacConfig->bitrateMode,
1035 hAacConfig->channelMode);
1036 break;
1037 default:
1038 return AACENC_INVALID_CONFIG;
1039 }
1040
1041 /* set bitreservoir size */
1042 switch (hAacConfig->bitrateMode) {
1043 case AACENC_BR_MODE_VBR_1:
1044 case AACENC_BR_MODE_VBR_2:
1045 case AACENC_BR_MODE_VBR_3:
1046 case AACENC_BR_MODE_VBR_4:
1047 case AACENC_BR_MODE_VBR_5:
1048 case AACENC_BR_MODE_CBR:
1049 if ((INT)config->userPeakBitrate != -1) {
1050 hAacConfig->maxBitsPerFrame =
1051 (FDKaacEnc_CalcBitsPerFrame(
1052 fMax(hAacConfig->bitRate, (INT)config->userPeakBitrate),
1053 hAacConfig->framelength, hAacConfig->sampleRate) +
1054 7) &
1055 ~7;
1056 } else {
1057 hAacConfig->maxBitsPerFrame = -1;
1058 }
1059 if (hAacConfig->audioMuxVersion == 2) {
1060 hAacConfig->minBitsPerFrame =
1061 fMin(32 * 8, FDKaacEnc_CalcBitsPerFrame(hAacConfig->bitRate,
1062 hAacConfig->framelength,
1063 hAacConfig->sampleRate)) &
1064 ~7;
1065 }
1066 break;
1067 default:
1068 return AACENC_INVALID_CONFIG;
1069 }
1070
1071 /* Max bits per frame limitation depending on transport format. */
1072 if ((config->userTpNsubFrames > 1)) {
1073 int maxFrameLength = 8 * hAacEncoder->outBufferInBytes;
1074 switch (config->userTpType) {
1075 case TT_MP4_LOAS:
1076 maxFrameLength =
1077 fMin(maxFrameLength, 8 * (1 << 13)) / config->userTpNsubFrames;
1078 break;
1079 case TT_MP4_ADTS:
1080 maxFrameLength = fMin(maxFrameLength, 8 * ((1 << 13) - 1)) /
1081 config->userTpNsubFrames;
1082 break;
1083 default:
1084 maxFrameLength = -1;
1085 }
1086 if (maxFrameLength != -1) {
1087 if (hAacConfig->maxBitsPerFrame > maxFrameLength) {
1088 return AACENC_INVALID_CONFIG;
1089 } else if (hAacConfig->maxBitsPerFrame == -1) {
1090 hAacConfig->maxBitsPerFrame = maxFrameLength;
1091 }
1092 }
1093 }
1094
1095 if ((hAacConfig->audioObjectType == AOT_ER_AAC_ELD) &&
1096 !(hAacConfig->syntaxFlags & AC_ELD_DOWNSCALE) &&
1097 (config->userSbrEnabled == (UCHAR)-1) && (config->userSbrRatio == 0) &&
1098 ((hAacConfig->syntaxFlags & AC_LD_MPS) == 0)) {
1099 const ELD_SBR_CONFIGURATOR *pConfig = NULL;
1100
1101 if (NULL !=
1102 (pConfig = eldSbrConfigurator(
1103 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1104 ->nChannels,
1105 hAacConfig->sampleRate, hAacConfig->bitRate))) {
1106 hAacConfig->syntaxFlags |= (pConfig->sbrMode == 0) ? 0 : AC_SBR_PRESENT;
1107 hAacConfig->syntaxFlags |= (pConfig->chMode == MODE_212) ? AC_LD_MPS : 0;
1108 hAacConfig->channelMode =
1109 GetCoreChannelMode(pConfig->chMode, hAacConfig->audioObjectType);
1110 hAacConfig->nChannels =
1111 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1112 ->nChannels;
1113 hAacConfig->sbrRatio =
1114 (pConfig->sbrMode == 0) ? 0 : (pConfig->sbrMode == 1) ? 1 : 2;
1115 }
1116 }
1117
1118 {
1119 UCHAR tpSignaling =
1120 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType,
1121 config->userTpSignaling, hAacConfig->sbrRatio);
1122
1123 if ((hAacConfig->audioObjectType == AOT_AAC_LC ||
1124 hAacConfig->audioObjectType == AOT_SBR ||
1125 hAacConfig->audioObjectType == AOT_PS) &&
1126 (config->userTpType == TT_MP4_LATM_MCP1 ||
1127 config->userTpType == TT_MP4_LATM_MCP0 ||
1128 config->userTpType == TT_MP4_LOAS) &&
1129 (tpSignaling == 1) && (config->userTpAmxv == 0)) {
1130 /* For backward compatible explicit signaling, AMV1 has to be active */
1131 return AACENC_INVALID_CONFIG;
1132 }
1133
1134 if ((hAacConfig->audioObjectType == AOT_AAC_LC ||
1135 hAacConfig->audioObjectType == AOT_SBR ||
1136 hAacConfig->audioObjectType == AOT_PS) &&
1137 (tpSignaling == 0) && (hAacConfig->sbrRatio == 1)) {
1138 /* Downsampled SBR has to be signaled explicitely (for transmission of SBR
1139 * sampling fequency) */
1140 return AACENC_INVALID_CONFIG;
1141 }
1142 }
1143
1144 switch (hAacConfig->bitrateMode) {
1145 case AACENC_BR_MODE_CBR:
1146 case AACENC_BR_MODE_VBR_1:
1147 case AACENC_BR_MODE_VBR_2:
1148 case AACENC_BR_MODE_VBR_3:
1149 case AACENC_BR_MODE_VBR_4:
1150 case AACENC_BR_MODE_VBR_5:
1151 /* We need the frame length to call aacEncoder_LimitBitrate() */
1152 if (0 >= (hAacConfig->bitRate = aacEncoder_LimitBitrate(
1153 NULL, hAacConfig->sampleRate, hAacConfig->framelength,
1154 hAacConfig->nChannels, hAacConfig->channelMode,
1155 hAacConfig->bitRate, hAacConfig->nSubFrames,
1156 isSbrActive(hAacConfig), hAacConfig->sbrRatio,
1157 hAacConfig->syntaxFlags, hAacConfig->audioObjectType))) {
1158 return AACENC_INVALID_CONFIG;
1159 }
1160 break;
1161 default:
1162 break;
1163 }
1164
1165 /* Configure PNS */
1166 if (AACENC_BR_MODE_IS_VBR(hAacConfig->bitrateMode) /* VBR without PNS. */
1167 || (hAacConfig->useTns == 0)) /* TNS required. */
1168 {
1169 hAacConfig->usePns = 0;
1170 }
1171
1172 if (hAacConfig->epConfig >= 0) {
1173 hAacConfig->syntaxFlags |= AC_ER;
1174 if (((INT)hAacConfig->channelMode < 1) ||
1175 ((INT)hAacConfig->channelMode > 14)) {
1176 return AACENC_INVALID_CONFIG; /* Channel config 0 not supported. */
1177 }
1178 }
1179
1180 if ((hAacConfig->syntaxFlags & AC_LD_MPS) == 0) {
1181 if (FDKaacEnc_DetermineEncoderMode(&hAacConfig->channelMode,
1182 hAacConfig->nChannels) != AAC_ENC_OK) {
1183 return AACENC_INVALID_CONFIG; /* nChannels doesn't match chMode, this is
1184 just a check-up */
1185 }
1186 }
1187
1188 if ((hAacConfig->nChannels > hAacEncoder->nMaxAacChannels) ||
1189 ((FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1190 ->nChannelsEff > hAacEncoder->nMaxSbrChannels) &&
1191 isSbrActive(hAacConfig))) {
1192 return AACENC_INVALID_CONFIG; /* not enough channels allocated */
1193 }
1194
1195 /* Meta data restriction. */
1196 switch (hAacConfig->audioObjectType) {
1197 /* Allow metadata support */
1198 case AOT_AAC_LC:
1199 case AOT_SBR:
1200 case AOT_PS:
1201 case AOT_MP2_AAC_LC:
1202 case AOT_MP2_SBR:
1203 hAacEncoder->metaDataAllowed = 1;
1204 if (!((((INT)hAacConfig->channelMode >= 1) &&
1205 ((INT)hAacConfig->channelMode <= 14)) ||
1206 (MODE_7_1_REAR_SURROUND == hAacConfig->channelMode) ||
1207 (MODE_7_1_FRONT_CENTER == hAacConfig->channelMode))) {
1208 config->userMetaDataMode = 0;
1209 }
1210 break;
1211 /* Prohibit metadata support */
1212 default:
1213 hAacEncoder->metaDataAllowed = 0;
1214 }
1215
1216 return err;
1217 }
1218
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)1219 static INT aacenc_SbrCallback(void *self, HANDLE_FDK_BITSTREAM hBs,
1220 const INT sampleRateIn, const INT sampleRateOut,
1221 const INT samplesPerFrame,
1222 const AUDIO_OBJECT_TYPE coreCodec,
1223 const MP4_ELEMENT_ID elementID,
1224 const INT elementIndex, const UCHAR harmonicSbr,
1225 const UCHAR stereoConfigIndex,
1226 const UCHAR configMode, UCHAR *configChanged,
1227 const INT downscaleFactor) {
1228 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self;
1229
1230 sbrEncoder_GetHeader(hAacEncoder->hEnvEnc, hBs, elementIndex, 0);
1231
1232 return 0;
1233 }
1234
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)1235 INT aacenc_SscCallback(void *self, HANDLE_FDK_BITSTREAM hBs,
1236 const AUDIO_OBJECT_TYPE coreCodec,
1237 const INT samplingRate, const INT frameSize,
1238 const INT stereoConfigIndex,
1239 const INT coreSbrFrameLengthIndex, const INT configBytes,
1240 const UCHAR configMode, UCHAR *configChanged) {
1241 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self;
1242
1243 return (FDK_MpegsEnc_WriteSpatialSpecificConfig(hAacEncoder->hMpsEnc, hBs));
1244 }
1245
aacEncInit(HANDLE_AACENCODER hAacEncoder,ULONG InitFlags,USER_PARAM * config)1246 static AACENC_ERROR aacEncInit(HANDLE_AACENCODER hAacEncoder, ULONG InitFlags,
1247 USER_PARAM *config) {
1248 AACENC_ERROR err = AACENC_OK;
1249
1250 INT aacBufferOffset = 0;
1251 HANDLE_SBR_ENCODER *hSbrEncoder = &hAacEncoder->hEnvEnc;
1252 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig;
1253
1254 hAacEncoder->nZerosAppended = 0; /* count appended zeros */
1255
1256 INT frameLength = hAacConfig->framelength;
1257
1258 if ((InitFlags & AACENC_INIT_CONFIG)) {
1259 CHANNEL_MODE prevChMode = hAacConfig->channelMode;
1260
1261 /* Verify settings and update: config -> heAacEncoder */
1262 if ((err = FDKaacEnc_AdjustEncSettings(hAacEncoder, config)) != AACENC_OK) {
1263 return err;
1264 }
1265 frameLength = hAacConfig->framelength; /* adapt temporal framelength */
1266
1267 /* Seamless channel reconfiguration in sbr not fully implemented */
1268 if ((prevChMode != hAacConfig->channelMode) && isSbrActive(hAacConfig)) {
1269 InitFlags |= AACENC_INIT_STATES;
1270 }
1271 }
1272
1273 /* Clear input buffer */
1274 if (InitFlags == AACENC_INIT_ALL) {
1275 FDKmemclear(hAacEncoder->inputBuffer,
1276 sizeof(INT_PCM) * hAacEncoder->inputBufferSize);
1277 }
1278
1279 if ((InitFlags & AACENC_INIT_CONFIG)) {
1280 aacBufferOffset = 0;
1281 switch (hAacConfig->audioObjectType) {
1282 case AOT_ER_AAC_LD:
1283 hAacEncoder->nDelay = DELAY_AACLD(hAacConfig->framelength);
1284 break;
1285 case AOT_ER_AAC_ELD:
1286 hAacEncoder->nDelay = DELAY_AACELD(hAacConfig->framelength);
1287 break;
1288 default:
1289 hAacEncoder->nDelay =
1290 DELAY_AAC(hAacConfig->framelength); /* AAC encoder delay */
1291 }
1292
1293 hAacConfig->ancDataBitRate = 0;
1294 }
1295
1296 if ((NULL != hAacEncoder->hEnvEnc) && isSbrActive(hAacConfig) &&
1297 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES))) {
1298 INT sbrError;
1299 UINT initFlag = 0;
1300 SBR_ELEMENT_INFO sbrElInfo[(8)];
1301 CHANNEL_MAPPING channelMapping;
1302 CHANNEL_MODE channelMode = isPsActive(hAacConfig->audioObjectType)
1303 ? config->userChannelMode
1304 : hAacConfig->channelMode;
1305 INT numChannels = isPsActive(hAacConfig->audioObjectType)
1306 ? config->nChannels
1307 : hAacConfig->nChannels;
1308
1309 if (FDKaacEnc_InitChannelMapping(channelMode, hAacConfig->channelOrder,
1310 &channelMapping) != AAC_ENC_OK) {
1311 return AACENC_INIT_ERROR;
1312 }
1313
1314 /* Check return value and if the SBR encoder can handle enough elements */
1315 if (channelMapping.nElements > (8)) {
1316 return AACENC_INIT_ERROR;
1317 }
1318
1319 aacEncDistributeSbrBits(&channelMapping, sbrElInfo, hAacConfig->bitRate);
1320
1321 initFlag += (InitFlags & AACENC_INIT_STATES) ? 1 : 0;
1322
1323 /* Let the SBR encoder take a look at the configuration and change if
1324 * required. */
1325 sbrError = sbrEncoder_Init(
1326 *hSbrEncoder, sbrElInfo, channelMapping.nElements,
1327 hAacEncoder->inputBuffer, hAacEncoder->inputBufferSizePerChannel,
1328 &hAacConfig->bandWidth, &aacBufferOffset, &numChannels,
1329 hAacConfig->syntaxFlags, &hAacConfig->sampleRate, &hAacConfig->sbrRatio,
1330 &frameLength, hAacConfig->audioObjectType, &hAacEncoder->nDelay,
1331 (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) ? 1 : TRANS_FAC,
1332 (config->userTpHeaderPeriod != 0xFF)
1333 ? config->userTpHeaderPeriod
1334 : DEFAULT_HEADER_PERIOD_REPETITION_RATE,
1335 initFlag);
1336
1337 /* Suppress AOT reconfiguration and check error status. */
1338 if ((sbrError) || (numChannels != hAacConfig->nChannels)) {
1339 return AACENC_INIT_SBR_ERROR;
1340 }
1341
1342 if (numChannels == 1) {
1343 hAacConfig->channelMode = MODE_1;
1344 }
1345
1346 /* Never use PNS if SBR is active */
1347 if (hAacConfig->usePns) {
1348 hAacConfig->usePns = 0;
1349 }
1350
1351 /* estimated bitrate consumed by SBR or PS */
1352 hAacConfig->ancDataBitRate = sbrEncoder_GetEstimateBitrate(*hSbrEncoder);
1353
1354 } /* sbr initialization */
1355
1356 if ((hAacEncoder->hMpsEnc != NULL) && (hAacConfig->syntaxFlags & AC_LD_MPS)) {
1357 int coreCoderDelay = DELAY_AACELD(hAacConfig->framelength);
1358
1359 if (isSbrActive(hAacConfig)) {
1360 coreCoderDelay = hAacConfig->sbrRatio * coreCoderDelay +
1361 sbrEncoder_GetInputDataDelay(*hSbrEncoder);
1362 }
1363
1364 if (MPS_ENCODER_OK !=
1365 FDK_MpegsEnc_Init(hAacEncoder->hMpsEnc, hAacConfig->audioObjectType,
1366 config->userSamplerate, hAacConfig->bitRate,
1367 isSbrActive(hAacConfig) ? hAacConfig->sbrRatio : 0,
1368 frameLength, /* for dual rate sbr this value is
1369 already multiplied by 2 */
1370 hAacEncoder->inputBufferSizePerChannel,
1371 coreCoderDelay)) {
1372 return AACENC_INIT_MPS_ERROR;
1373 }
1374 }
1375 hAacEncoder->nDelay =
1376 fMax(FDK_MpegsEnc_GetDelay(hAacEncoder->hMpsEnc), hAacEncoder->nDelay);
1377
1378 /*
1379 * Initialize Transport - Module.
1380 */
1381 if ((InitFlags & AACENC_INIT_TRANSPORT)) {
1382 UINT flags = 0;
1383
1384 FDKaacEnc_MapConfig(
1385 &hAacEncoder->coderConfig, config,
1386 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType,
1387 config->userTpSignaling, hAacConfig->sbrRatio),
1388 hAacConfig);
1389
1390 /* create flags for transport encoder */
1391 if (config->userTpAmxv != 0) {
1392 flags |= TP_FLAG_LATM_AMV;
1393 }
1394 /* Clear output buffer */
1395 FDKmemclear(hAacEncoder->outBuffer,
1396 hAacEncoder->outBufferInBytes * sizeof(UCHAR));
1397
1398 /* Initialize Bitstream encoder */
1399 if (transportEnc_Init(hAacEncoder->hTpEnc, hAacEncoder->outBuffer,
1400 hAacEncoder->outBufferInBytes, config->userTpType,
1401 &hAacEncoder->coderConfig, flags) != 0) {
1402 return AACENC_INIT_TP_ERROR;
1403 }
1404
1405 } /* transport initialization */
1406
1407 /*
1408 * Initialize AAC - Core.
1409 */
1410 if ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES)) {
1411 if (FDKaacEnc_Initialize(
1412 hAacEncoder->hAacEnc, hAacConfig, hAacEncoder->hTpEnc,
1413 (InitFlags & AACENC_INIT_STATES) ? 1 : 0) != AAC_ENC_OK) {
1414 return AACENC_INIT_AAC_ERROR;
1415 }
1416
1417 } /* aac initialization */
1418
1419 /*
1420 * Initialize Meta Data - Encoder.
1421 */
1422 if (hAacEncoder->hMetadataEnc && (hAacEncoder->metaDataAllowed != 0) &&
1423 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES))) {
1424 INT inputDataDelay = DELAY_AAC(hAacConfig->framelength);
1425
1426 if (isSbrActive(hAacConfig) && hSbrEncoder != NULL) {
1427 inputDataDelay = hAacConfig->sbrRatio * inputDataDelay +
1428 sbrEncoder_GetInputDataDelay(*hSbrEncoder);
1429 }
1430
1431 if (FDK_MetadataEnc_Init(hAacEncoder->hMetadataEnc,
1432 ((InitFlags & AACENC_INIT_STATES) ? 1 : 0),
1433 config->userMetaDataMode, inputDataDelay,
1434 frameLength, config->userSamplerate,
1435 config->nChannels, config->userChannelMode,
1436 hAacConfig->channelOrder) != 0) {
1437 return AACENC_INIT_META_ERROR;
1438 }
1439
1440 hAacEncoder->nDelay += FDK_MetadataEnc_GetDelay(hAacEncoder->hMetadataEnc);
1441 }
1442
1443 /* Get custom delay, i.e. the codec delay w/o the decoder's SBR- or MPS delay
1444 */
1445 if ((hAacEncoder->hMpsEnc != NULL) && (hAacConfig->syntaxFlags & AC_LD_MPS)) {
1446 hAacEncoder->nDelayCore =
1447 hAacEncoder->nDelay -
1448 fMax(0, FDK_MpegsEnc_GetDecDelay(hAacEncoder->hMpsEnc));
1449 } else if (isSbrActive(hAacConfig) && hSbrEncoder != NULL) {
1450 hAacEncoder->nDelayCore =
1451 hAacEncoder->nDelay -
1452 fMax(0, sbrEncoder_GetSbrDecDelay(hAacEncoder->hEnvEnc));
1453 } else {
1454 hAacEncoder->nDelayCore = hAacEncoder->nDelay;
1455 }
1456
1457 /*
1458 * Update pointer to working buffer.
1459 */
1460 if ((InitFlags & AACENC_INIT_CONFIG)) {
1461 hAacEncoder->inputBufferOffset = aacBufferOffset;
1462
1463 hAacEncoder->nSamplesToRead = frameLength * config->nChannels;
1464
1465 } /* parameter changed */
1466
1467 return AACENC_OK;
1468 }
1469
aacEncOpen(HANDLE_AACENCODER * phAacEncoder,const UINT encModules,const UINT maxChannels)1470 AACENC_ERROR aacEncOpen(HANDLE_AACENCODER *phAacEncoder, const UINT encModules,
1471 const UINT maxChannels) {
1472 AACENC_ERROR err = AACENC_OK;
1473 HANDLE_AACENCODER hAacEncoder = NULL;
1474
1475 if (phAacEncoder == NULL) {
1476 err = AACENC_INVALID_HANDLE;
1477 goto bail;
1478 }
1479
1480 /* allocate memory */
1481 hAacEncoder = Get_AacEncoder();
1482
1483 if (hAacEncoder == NULL) {
1484 err = AACENC_MEMORY_ERROR;
1485 goto bail;
1486 }
1487
1488 FDKmemclear(hAacEncoder, sizeof(AACENCODER));
1489
1490 /* Specify encoder modules to be allocated. */
1491 if (encModules == 0) {
1492 C_ALLOC_SCRATCH_START(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1493 LIB_INFO(*pLibInfo)
1494 [FDK_MODULE_LAST] = (LIB_INFO(*)[FDK_MODULE_LAST])_pLibInfo;
1495 FDKinitLibInfo(*pLibInfo);
1496 aacEncGetLibInfo(*pLibInfo);
1497
1498 hAacEncoder->encoder_modis = ENC_MODE_FLAG_AAC;
1499 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_SBRENC) & CAPF_SBR_HQ) {
1500 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SBR;
1501 }
1502 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_SBRENC) & CAPF_SBR_PS_MPEG) {
1503 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_PS;
1504 }
1505 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_AACENC) & CAPF_AAC_DRC) {
1506 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_META;
1507 }
1508 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SAC;
1509
1510 C_ALLOC_SCRATCH_END(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1511 } else {
1512 hAacEncoder->encoder_modis = encModules;
1513 }
1514
1515 /* Determine max channel configuration. */
1516 if (maxChannels == 0) {
1517 hAacEncoder->nMaxAacChannels = (8);
1518 hAacEncoder->nMaxSbrChannels = (8);
1519 } else {
1520 hAacEncoder->nMaxAacChannels = (maxChannels & 0x00FF);
1521 if ((hAacEncoder->encoder_modis & ENC_MODE_FLAG_SBR)) {
1522 hAacEncoder->nMaxSbrChannels = (maxChannels & 0xFF00)
1523 ? (maxChannels >> 8)
1524 : hAacEncoder->nMaxAacChannels;
1525 }
1526
1527 if ((hAacEncoder->nMaxAacChannels > (8)) ||
1528 (hAacEncoder->nMaxSbrChannels > (8))) {
1529 err = AACENC_INVALID_CONFIG;
1530 goto bail;
1531 }
1532 } /* maxChannels==0 */
1533
1534 /* Max number of elements could be tuned any more. */
1535 hAacEncoder->nMaxAacElements = fixMin(((8)), hAacEncoder->nMaxAacChannels);
1536 hAacEncoder->nMaxSbrElements = fixMin((8), hAacEncoder->nMaxSbrChannels);
1537
1538 /* In case of memory overlay, allocate memory out of libraries */
1539
1540 if (hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR | ENC_MODE_FLAG_PS))
1541 hAacEncoder->inputBufferSizePerChannel = INPUTBUFFER_SIZE;
1542 else
1543 hAacEncoder->inputBufferSizePerChannel = (1024);
1544
1545 hAacEncoder->inputBufferSize =
1546 hAacEncoder->nMaxAacChannels * hAacEncoder->inputBufferSizePerChannel;
1547
1548 if (NULL == (hAacEncoder->inputBuffer = (INT_PCM *)FDKcalloc(
1549 hAacEncoder->inputBufferSize, sizeof(INT_PCM)))) {
1550 err = AACENC_MEMORY_ERROR;
1551 goto bail;
1552 }
1553
1554 /* Open SBR Encoder */
1555 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_SBR) {
1556 if (sbrEncoder_Open(
1557 &hAacEncoder->hEnvEnc, hAacEncoder->nMaxSbrElements,
1558 hAacEncoder->nMaxSbrChannels,
1559 (hAacEncoder->encoder_modis & ENC_MODE_FLAG_PS) ? 1 : 0)) {
1560 err = AACENC_MEMORY_ERROR;
1561 goto bail;
1562 }
1563
1564 if (NULL == (hAacEncoder->pSbrPayload = (SBRENC_EXT_PAYLOAD *)FDKcalloc(
1565 1, sizeof(SBRENC_EXT_PAYLOAD)))) {
1566 err = AACENC_MEMORY_ERROR;
1567 goto bail;
1568 }
1569 } /* (encoder_modis&ENC_MODE_FLAG_SBR) */
1570
1571 /* Open Aac Encoder */
1572 if (FDKaacEnc_Open(&hAacEncoder->hAacEnc, hAacEncoder->nMaxAacElements,
1573 hAacEncoder->nMaxAacChannels, (1)) != AAC_ENC_OK) {
1574 err = AACENC_MEMORY_ERROR;
1575 goto bail;
1576 }
1577
1578 /* Bitstream output buffer */
1579 hAacEncoder->outBufferInBytes =
1580 1 << (DFRACT_BITS - CntLeadingZeros(fixMax(
1581 1, ((1) * hAacEncoder->nMaxAacChannels * 6144) >>
1582 3))); /* buffer has to be 2^n */
1583 if (NULL == (hAacEncoder->outBuffer = (UCHAR *)FDKcalloc(
1584 hAacEncoder->outBufferInBytes, sizeof(UCHAR)))) {
1585 err = AACENC_MEMORY_ERROR;
1586 goto bail;
1587 }
1588
1589 /* Open Meta Data Encoder */
1590 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_META) {
1591 if (FDK_MetadataEnc_Open(&hAacEncoder->hMetadataEnc,
1592 (UINT)hAacEncoder->nMaxAacChannels)) {
1593 err = AACENC_MEMORY_ERROR;
1594 goto bail;
1595 }
1596 } /* (encoder_modis&ENC_MODE_FLAG_META) */
1597
1598 /* Open MPEG Surround Encoder */
1599 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_SAC) {
1600 if (MPS_ENCODER_OK != FDK_MpegsEnc_Open(&hAacEncoder->hMpsEnc)) {
1601 err = AACENC_MEMORY_ERROR;
1602 goto bail;
1603 }
1604 } /* (hAacEncoder->encoder_modis&ENC_MODE_FLAG_SAC) */
1605
1606 /* Open Transport Encoder */
1607 if (transportEnc_Open(&hAacEncoder->hTpEnc) != 0) {
1608 err = AACENC_MEMORY_ERROR;
1609 goto bail;
1610 } else {
1611 C_ALLOC_SCRATCH_START(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1612
1613 LIB_INFO(*pLibInfo)
1614 [FDK_MODULE_LAST] = (LIB_INFO(*)[FDK_MODULE_LAST])_pLibInfo;
1615
1616 FDKinitLibInfo(*pLibInfo);
1617 transportEnc_GetLibInfo(*pLibInfo);
1618
1619 /* Get capabilty flag for transport encoder. */
1620 hAacEncoder->CAPF_tpEnc = FDKlibInfo_getCapabilities(*pLibInfo, FDK_TPENC);
1621
1622 C_ALLOC_SCRATCH_END(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1623 }
1624 if (transportEnc_RegisterSbrCallback(hAacEncoder->hTpEnc, aacenc_SbrCallback,
1625 hAacEncoder) != 0) {
1626 err = AACENC_INIT_TP_ERROR;
1627 goto bail;
1628 }
1629 if (transportEnc_RegisterSscCallback(hAacEncoder->hTpEnc, aacenc_SscCallback,
1630 hAacEncoder) != 0) {
1631 err = AACENC_INIT_TP_ERROR;
1632 goto bail;
1633 }
1634
1635 /* Initialize encoder instance with default parameters. */
1636 aacEncDefaultConfig(&hAacEncoder->aacConfig, &hAacEncoder->extParam);
1637
1638 /* Initialize headerPeriod in coderConfig for aacEncoder_GetParam(). */
1639 hAacEncoder->coderConfig.headerPeriod =
1640 hAacEncoder->extParam.userTpHeaderPeriod;
1641
1642 /* All encoder modules have to be initialized */
1643 hAacEncoder->InitFlags = AACENC_INIT_ALL;
1644
1645 /* Return encoder instance */
1646 *phAacEncoder = hAacEncoder;
1647
1648 return err;
1649
1650 bail:
1651 aacEncClose(&hAacEncoder);
1652
1653 return err;
1654 }
1655
aacEncClose(HANDLE_AACENCODER * phAacEncoder)1656 AACENC_ERROR aacEncClose(HANDLE_AACENCODER *phAacEncoder) {
1657 AACENC_ERROR err = AACENC_OK;
1658
1659 if (phAacEncoder == NULL) {
1660 err = AACENC_INVALID_HANDLE;
1661 goto bail;
1662 }
1663
1664 if (*phAacEncoder != NULL) {
1665 HANDLE_AACENCODER hAacEncoder = *phAacEncoder;
1666
1667 if (hAacEncoder->inputBuffer != NULL) {
1668 FDKfree(hAacEncoder->inputBuffer);
1669 hAacEncoder->inputBuffer = NULL;
1670 }
1671 if (hAacEncoder->outBuffer != NULL) {
1672 FDKfree(hAacEncoder->outBuffer);
1673 hAacEncoder->outBuffer = NULL;
1674 }
1675
1676 if (hAacEncoder->hEnvEnc) {
1677 sbrEncoder_Close(&hAacEncoder->hEnvEnc);
1678 }
1679 if (hAacEncoder->pSbrPayload != NULL) {
1680 FDKfree(hAacEncoder->pSbrPayload);
1681 hAacEncoder->pSbrPayload = NULL;
1682 }
1683 if (hAacEncoder->hAacEnc) {
1684 FDKaacEnc_Close(&hAacEncoder->hAacEnc);
1685 }
1686
1687 transportEnc_Close(&hAacEncoder->hTpEnc);
1688
1689 if (hAacEncoder->hMetadataEnc) {
1690 FDK_MetadataEnc_Close(&hAacEncoder->hMetadataEnc);
1691 }
1692 if (hAacEncoder->hMpsEnc) {
1693 FDK_MpegsEnc_Close(&hAacEncoder->hMpsEnc);
1694 }
1695
1696 Free_AacEncoder(phAacEncoder);
1697 }
1698
1699 bail:
1700 return err;
1701 }
1702
aacEncEncode(const HANDLE_AACENCODER hAacEncoder,const AACENC_BufDesc * inBufDesc,const AACENC_BufDesc * outBufDesc,const AACENC_InArgs * inargs,AACENC_OutArgs * outargs)1703 AACENC_ERROR aacEncEncode(const HANDLE_AACENCODER hAacEncoder,
1704 const AACENC_BufDesc *inBufDesc,
1705 const AACENC_BufDesc *outBufDesc,
1706 const AACENC_InArgs *inargs,
1707 AACENC_OutArgs *outargs) {
1708 AACENC_ERROR err = AACENC_OK;
1709 INT i, nBsBytes = 0;
1710 INT outBytes[(1)];
1711 int nExtensions = 0;
1712 int ancDataExtIdx = -1;
1713
1714 /* deal with valid encoder handle */
1715 if (hAacEncoder == NULL) {
1716 err = AACENC_INVALID_HANDLE;
1717 goto bail;
1718 }
1719
1720 /*
1721 * Adjust user settings and trigger reinitialization.
1722 */
1723 if (hAacEncoder->InitFlags != 0) {
1724 err =
1725 aacEncInit(hAacEncoder, hAacEncoder->InitFlags, &hAacEncoder->extParam);
1726
1727 if (err != AACENC_OK) {
1728 /* keep init flags alive! */
1729 goto bail;
1730 }
1731 hAacEncoder->InitFlags = AACENC_INIT_NONE;
1732 }
1733
1734 if (outargs != NULL) {
1735 FDKmemclear(outargs, sizeof(AACENC_OutArgs));
1736 }
1737
1738 if (outBufDesc != NULL) {
1739 for (i = 0; i < outBufDesc->numBufs; i++) {
1740 if (outBufDesc->bufs[i] != NULL) {
1741 FDKmemclear(outBufDesc->bufs[i], outBufDesc->bufSizes[i]);
1742 }
1743 }
1744 }
1745
1746 /*
1747 * If only encoder handle given, independent (re)initialization can be
1748 * triggered.
1749 */
1750 if ((inBufDesc == NULL) && (outBufDesc == NULL) && (inargs == NULL) &&
1751 (outargs == NULL)) {
1752 goto bail;
1753 }
1754
1755 /* check if buffer descriptors are filled out properly. */
1756 if ((inargs == NULL) || (outargs == NULL) ||
1757 ((AACENC_OK != validateBufDesc(inBufDesc)) &&
1758 (inargs->numInSamples > 0)) ||
1759 (AACENC_OK != validateBufDesc(outBufDesc))) {
1760 err = AACENC_UNSUPPORTED_PARAMETER;
1761 goto bail;
1762 }
1763
1764 /* reset buffer wich signals number of valid bytes in output bitstream buffer
1765 */
1766 FDKmemclear(outBytes, hAacEncoder->aacConfig.nSubFrames * sizeof(INT));
1767
1768 /*
1769 * Manage incoming audio samples.
1770 */
1771 if ((inBufDesc != NULL) && (inargs->numInSamples > 0) &&
1772 (getBufDescIdx(inBufDesc, IN_AUDIO_DATA) != -1)) {
1773 /* Fetch data until nSamplesToRead reached */
1774 INT idx = getBufDescIdx(inBufDesc, IN_AUDIO_DATA);
1775 INT newSamples =
1776 fixMax(0, fixMin(inargs->numInSamples, hAacEncoder->nSamplesToRead -
1777 hAacEncoder->nSamplesRead));
1778 INT_PCM *pIn =
1779 hAacEncoder->inputBuffer +
1780 (hAacEncoder->inputBufferOffset + hAacEncoder->nSamplesRead) /
1781 hAacEncoder->aacConfig.nChannels;
1782 newSamples -=
1783 (newSamples %
1784 hAacEncoder->extParam
1785 .nChannels); /* process multiple samples of input channels */
1786
1787 /* Copy new input samples to internal buffer */
1788 if (inBufDesc->bufElSizes[idx] == (INT)sizeof(INT_PCM)) {
1789 FDK_deinterleave((INT_PCM *)inBufDesc->bufs[idx], pIn,
1790 hAacEncoder->extParam.nChannels,
1791 newSamples / hAacEncoder->extParam.nChannels,
1792 hAacEncoder->inputBufferSizePerChannel);
1793 } else if (inBufDesc->bufElSizes[idx] > (INT)sizeof(INT_PCM)) {
1794 FDK_deinterleave((LONG *)inBufDesc->bufs[idx], pIn,
1795 hAacEncoder->extParam.nChannels,
1796 newSamples / hAacEncoder->extParam.nChannels,
1797 hAacEncoder->inputBufferSizePerChannel);
1798 } else {
1799 FDK_deinterleave((SHORT *)inBufDesc->bufs[idx], pIn,
1800 hAacEncoder->extParam.nChannels,
1801 newSamples / hAacEncoder->extParam.nChannels,
1802 hAacEncoder->inputBufferSizePerChannel);
1803 }
1804 hAacEncoder->nSamplesRead += newSamples;
1805
1806 /* Number of fetched input buffer samples. */
1807 outargs->numInSamples = newSamples;
1808 }
1809
1810 /* input buffer completely filled ? */
1811 if (hAacEncoder->nSamplesRead < hAacEncoder->nSamplesToRead) {
1812 /* - eof reached and flushing enabled, or
1813 - return to main and wait for further incoming audio samples */
1814 if (inargs->numInSamples == -1) {
1815 if ((hAacEncoder->nZerosAppended < hAacEncoder->nDelay)) {
1816 int nZeros = (hAacEncoder->nSamplesToRead - hAacEncoder->nSamplesRead) /
1817 hAacEncoder->extParam.nChannels;
1818
1819 FDK_ASSERT(nZeros >= 0);
1820
1821 /* clear out until end-of-buffer */
1822 if (nZeros) {
1823 for (i = 0; i < (int)hAacEncoder->extParam.nChannels; i++) {
1824 FDKmemclear(hAacEncoder->inputBuffer +
1825 i * hAacEncoder->inputBufferSizePerChannel +
1826 (hAacEncoder->inputBufferOffset +
1827 hAacEncoder->nSamplesRead) /
1828 hAacEncoder->extParam.nChannels,
1829 sizeof(INT_PCM) * nZeros);
1830 }
1831 hAacEncoder->nZerosAppended += nZeros;
1832 hAacEncoder->nSamplesRead = hAacEncoder->nSamplesToRead;
1833 }
1834 } else { /* flushing completed */
1835 err = AACENC_ENCODE_EOF; /* eof reached */
1836 goto bail;
1837 }
1838 } else { /* inargs->numInSamples!= -1 */
1839 goto bail; /* not enough samples in input buffer and no flushing enabled
1840 */
1841 }
1842 }
1843
1844 /* init payload */
1845 FDKmemclear(hAacEncoder->extPayload,
1846 sizeof(AACENC_EXT_PAYLOAD) * MAX_TOTAL_EXT_PAYLOADS);
1847 for (i = 0; i < MAX_TOTAL_EXT_PAYLOADS; i++) {
1848 hAacEncoder->extPayload[i].associatedChElement = -1;
1849 }
1850 if (hAacEncoder->pSbrPayload != NULL) {
1851 FDKmemclear(hAacEncoder->pSbrPayload, sizeof(*hAacEncoder->pSbrPayload));
1852 }
1853
1854 /*
1855 * Calculate Meta Data info.
1856 */
1857 if ((hAacEncoder->hMetadataEnc != NULL) &&
1858 (hAacEncoder->metaDataAllowed != 0)) {
1859 const AACENC_MetaData *pMetaData = NULL;
1860 AACENC_EXT_PAYLOAD *pMetaDataExtPayload = NULL;
1861 UINT nMetaDataExtensions = 0;
1862 INT matrix_mixdown_idx = 0;
1863
1864 /* New meta data info available ? */
1865 if (getBufDescIdx(inBufDesc, IN_METADATA_SETUP) != -1) {
1866 pMetaData =
1867 (AACENC_MetaData *)
1868 inBufDesc->bufs[getBufDescIdx(inBufDesc, IN_METADATA_SETUP)];
1869 }
1870
1871 FDK_MetadataEnc_Process(
1872 hAacEncoder->hMetadataEnc,
1873 hAacEncoder->inputBuffer + hAacEncoder->inputBufferOffset /
1874 hAacEncoder->coderConfig.noChannels,
1875 hAacEncoder->inputBufferSizePerChannel, hAacEncoder->nSamplesRead,
1876 pMetaData, &pMetaDataExtPayload, &nMetaDataExtensions,
1877 &matrix_mixdown_idx);
1878
1879 for (i = 0; i < (INT)nMetaDataExtensions;
1880 i++) { /* Get meta data extension payload. */
1881 hAacEncoder->extPayload[nExtensions++] = pMetaDataExtPayload[i];
1882 }
1883
1884 if ((matrix_mixdown_idx != -1) &&
1885 ((hAacEncoder->extParam.userChannelMode == MODE_1_2_2) ||
1886 (hAacEncoder->extParam.userChannelMode == MODE_1_2_2_1))) {
1887 /* Set matrix mixdown coefficient. */
1888 UINT pceValue = (UINT)((0 << 3) | ((matrix_mixdown_idx & 0x3) << 1) | 1);
1889 if (hAacEncoder->extParam.userPceAdditions != pceValue) {
1890 hAacEncoder->extParam.userPceAdditions = pceValue;
1891 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
1892 }
1893 }
1894 }
1895
1896 /*
1897 * Encode MPS data.
1898 */
1899 if ((hAacEncoder->hMpsEnc != NULL) &&
1900 (hAacEncoder->aacConfig.syntaxFlags & AC_LD_MPS)) {
1901 AACENC_EXT_PAYLOAD mpsExtensionPayload;
1902 FDKmemclear(&mpsExtensionPayload, sizeof(AACENC_EXT_PAYLOAD));
1903
1904 if (MPS_ENCODER_OK !=
1905 FDK_MpegsEnc_Process(
1906 hAacEncoder->hMpsEnc,
1907 hAacEncoder->inputBuffer + hAacEncoder->inputBufferOffset /
1908 hAacEncoder->coderConfig.noChannels,
1909 hAacEncoder->nSamplesRead, &mpsExtensionPayload)) {
1910 err = AACENC_ENCODE_ERROR;
1911 goto bail;
1912 }
1913
1914 if ((mpsExtensionPayload.pData != NULL) &&
1915 ((mpsExtensionPayload.dataSize != 0))) {
1916 hAacEncoder->extPayload[nExtensions++] = mpsExtensionPayload;
1917 }
1918 }
1919
1920 if ((NULL != hAacEncoder->hEnvEnc) && (NULL != hAacEncoder->pSbrPayload) &&
1921 isSbrActive(&hAacEncoder->aacConfig)) {
1922 INT nPayload = 0;
1923
1924 /*
1925 * Encode SBR data.
1926 */
1927 if (sbrEncoder_EncodeFrame(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer,
1928 hAacEncoder->inputBufferSizePerChannel,
1929 hAacEncoder->pSbrPayload->dataSize[nPayload],
1930 hAacEncoder->pSbrPayload->data[nPayload])) {
1931 err = AACENC_ENCODE_ERROR;
1932 goto bail;
1933 } else {
1934 /* Add SBR extension payload */
1935 for (i = 0; i < (8); i++) {
1936 if (hAacEncoder->pSbrPayload->dataSize[nPayload][i] > 0) {
1937 hAacEncoder->extPayload[nExtensions].pData =
1938 hAacEncoder->pSbrPayload->data[nPayload][i];
1939 {
1940 hAacEncoder->extPayload[nExtensions].dataSize =
1941 hAacEncoder->pSbrPayload->dataSize[nPayload][i];
1942 hAacEncoder->extPayload[nExtensions].associatedChElement =
1943 getAssociatedChElement(
1944 &hAacEncoder->hEnvEnc->sbrElement[i]->elInfo,
1945 &hAacEncoder->hAacEnc->channelMapping);
1946 if (hAacEncoder->extPayload[nExtensions].associatedChElement ==
1947 -1) {
1948 err = AACENC_ENCODE_ERROR;
1949 goto bail;
1950 }
1951 }
1952 hAacEncoder->extPayload[nExtensions].dataType =
1953 EXT_SBR_DATA; /* Once SBR Encoder supports SBR CRC set
1954 EXT_SBR_DATA_CRC */
1955 nExtensions++; /* or EXT_SBR_DATA according to configuration. */
1956 FDK_ASSERT(nExtensions <= MAX_TOTAL_EXT_PAYLOADS);
1957 }
1958 }
1959 nPayload++;
1960 }
1961 } /* sbrEnabled */
1962
1963 if ((inargs->numAncBytes > 0) &&
1964 (getBufDescIdx(inBufDesc, IN_ANCILLRY_DATA) != -1)) {
1965 INT idx = getBufDescIdx(inBufDesc, IN_ANCILLRY_DATA);
1966 hAacEncoder->extPayload[nExtensions].dataSize = inargs->numAncBytes * 8;
1967 hAacEncoder->extPayload[nExtensions].pData = (UCHAR *)inBufDesc->bufs[idx];
1968 hAacEncoder->extPayload[nExtensions].dataType = EXT_DATA_ELEMENT;
1969 hAacEncoder->extPayload[nExtensions].associatedChElement = -1;
1970 ancDataExtIdx = nExtensions; /* store index */
1971 nExtensions++;
1972 }
1973
1974 /*
1975 * Encode AAC - Core.
1976 */
1977 if (FDKaacEnc_EncodeFrame(hAacEncoder->hAacEnc, hAacEncoder->hTpEnc,
1978 hAacEncoder->inputBuffer,
1979 hAacEncoder->inputBufferSizePerChannel, outBytes,
1980 hAacEncoder->extPayload) != AAC_ENC_OK) {
1981 err = AACENC_ENCODE_ERROR;
1982 goto bail;
1983 }
1984
1985 if (ancDataExtIdx >= 0) {
1986 outargs->numAncBytes =
1987 inargs->numAncBytes -
1988 (hAacEncoder->extPayload[ancDataExtIdx].dataSize >> 3);
1989 }
1990
1991 /* samples exhausted */
1992 hAacEncoder->nSamplesRead -= hAacEncoder->nSamplesToRead;
1993
1994 /*
1995 * Delay balancing buffer handling
1996 */
1997 if (isSbrActive(&hAacEncoder->aacConfig)) {
1998 sbrEncoder_UpdateBuffers(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer,
1999 hAacEncoder->inputBufferSizePerChannel);
2000 }
2001
2002 /*
2003 * Make bitstream public
2004 */
2005 if ((outBufDesc != NULL) && (outBufDesc->numBufs >= 1)) {
2006 INT bsIdx = getBufDescIdx(outBufDesc, OUT_BITSTREAM_DATA);
2007 INT auIdx = getBufDescIdx(outBufDesc, OUT_AU_SIZES);
2008
2009 for (i = 0, nBsBytes = 0; i < hAacEncoder->aacConfig.nSubFrames; i++) {
2010 nBsBytes += outBytes[i];
2011
2012 if (auIdx != -1) {
2013 ((INT *)outBufDesc->bufs[auIdx])[i] = outBytes[i];
2014 }
2015 }
2016
2017 if ((bsIdx != -1) && (outBufDesc->bufSizes[bsIdx] >= nBsBytes)) {
2018 FDKmemcpy(outBufDesc->bufs[bsIdx], hAacEncoder->outBuffer,
2019 sizeof(UCHAR) * nBsBytes);
2020 outargs->numOutBytes = nBsBytes;
2021 outargs->bitResState =
2022 FDKaacEnc_GetBitReservoirState(hAacEncoder->hAacEnc);
2023 } else {
2024 /* output buffer too small, can't write valid bitstream */
2025 err = AACENC_ENCODE_ERROR;
2026 goto bail;
2027 }
2028 }
2029
2030 bail:
2031 if (err == AACENC_ENCODE_ERROR) {
2032 /* All encoder modules have to be initialized */
2033 hAacEncoder->InitFlags = AACENC_INIT_ALL;
2034 }
2035
2036 return err;
2037 }
2038
aacEncGetConf(HANDLE_AACENCODER hAacEncoder,UINT * size,UCHAR * confBuffer)2039 static AAC_ENCODER_ERROR aacEncGetConf(HANDLE_AACENCODER hAacEncoder,
2040 UINT *size, UCHAR *confBuffer) {
2041 FDK_BITSTREAM tmpConf;
2042 UINT confType;
2043 UCHAR buf[64];
2044 int err;
2045
2046 /* Init bit buffer */
2047 FDKinitBitStream(&tmpConf, buf, 64, 0, BS_WRITER);
2048
2049 /* write conf in tmp buffer */
2050 err = transportEnc_GetConf(hAacEncoder->hTpEnc, &hAacEncoder->coderConfig,
2051 &tmpConf, &confType);
2052
2053 /* copy data to outbuffer: length in bytes */
2054 FDKbyteAlign(&tmpConf, 0);
2055
2056 /* Check buffer size */
2057 if (FDKgetValidBits(&tmpConf) > ((*size) << 3)) return AAC_ENC_UNKNOWN;
2058
2059 FDKfetchBuffer(&tmpConf, confBuffer, size);
2060
2061 if (err != 0)
2062 return AAC_ENC_UNKNOWN;
2063 else
2064 return AAC_ENC_OK;
2065 }
2066
aacEncGetLibInfo(LIB_INFO * info)2067 AACENC_ERROR aacEncGetLibInfo(LIB_INFO *info) {
2068 int i = 0;
2069
2070 if (info == NULL) {
2071 return AACENC_INVALID_HANDLE;
2072 }
2073
2074 FDK_toolsGetLibInfo(info);
2075 transportEnc_GetLibInfo(info);
2076 sbrEncoder_GetLibInfo(info);
2077 FDK_MpegsEnc_GetLibInfo(info);
2078
2079 /* search for next free tab */
2080 for (i = 0; i < FDK_MODULE_LAST; i++) {
2081 if (info[i].module_id == FDK_NONE) break;
2082 }
2083 if (i == FDK_MODULE_LAST) {
2084 return AACENC_INIT_ERROR;
2085 }
2086
2087 info[i].module_id = FDK_AACENC;
2088 info[i].build_date = AACENCODER_LIB_BUILD_DATE;
2089 info[i].build_time = AACENCODER_LIB_BUILD_TIME;
2090 info[i].title = AACENCODER_LIB_TITLE;
2091 info[i].version =
2092 LIB_VERSION(AACENCODER_LIB_VL0, AACENCODER_LIB_VL1, AACENCODER_LIB_VL2);
2093 ;
2094 LIB_VERSION_STRING(&info[i]);
2095
2096 /* Capability flags */
2097 info[i].flags = 0 | CAPF_AAC_1024 | CAPF_AAC_LC | CAPF_AAC_512 |
2098 CAPF_AAC_480 | CAPF_AAC_DRC | CAPF_AAC_ELD_DOWNSCALE;
2099 /* End of flags */
2100
2101 return AACENC_OK;
2102 }
2103
aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder,const AACENC_PARAM param,const UINT value)2104 AACENC_ERROR aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder,
2105 const AACENC_PARAM param, const UINT value) {
2106 AACENC_ERROR err = AACENC_OK;
2107 USER_PARAM *settings = &hAacEncoder->extParam;
2108
2109 /* check encoder handle */
2110 if (hAacEncoder == NULL) {
2111 err = AACENC_INVALID_HANDLE;
2112 goto bail;
2113 }
2114
2115 /* apply param value */
2116 switch (param) {
2117 case AACENC_AOT:
2118 if (settings->userAOT != (AUDIO_OBJECT_TYPE)value) {
2119 /* check if AOT matches the allocated modules */
2120 switch (value) {
2121 case AOT_PS:
2122 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_PS))) {
2123 err = AACENC_INVALID_CONFIG;
2124 goto bail;
2125 }
2126 FDK_FALLTHROUGH;
2127 case AOT_SBR:
2128 case AOT_MP2_SBR:
2129 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR))) {
2130 err = AACENC_INVALID_CONFIG;
2131 goto bail;
2132 }
2133 FDK_FALLTHROUGH;
2134 case AOT_AAC_LC:
2135 case AOT_MP2_AAC_LC:
2136 case AOT_ER_AAC_LD:
2137 case AOT_ER_AAC_ELD:
2138 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_AAC))) {
2139 err = AACENC_INVALID_CONFIG;
2140 goto bail;
2141 }
2142 break;
2143 default:
2144 err = AACENC_INVALID_CONFIG;
2145 goto bail;
2146 } /* switch value */
2147 settings->userAOT = (AUDIO_OBJECT_TYPE)value;
2148 hAacEncoder->InitFlags |=
2149 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2150 }
2151 break;
2152 case AACENC_BITRATE:
2153 if (settings->userBitrate != value) {
2154 settings->userBitrate = value;
2155 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2156 }
2157 break;
2158 case AACENC_BITRATEMODE:
2159 if (settings->userBitrateMode != value) {
2160 switch (value) {
2161 case 0:
2162 case 1:
2163 case 2:
2164 case 3:
2165 case 4:
2166 case 5:
2167 settings->userBitrateMode = value;
2168 hAacEncoder->InitFlags |=
2169 AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2170 break;
2171 default:
2172 err = AACENC_INVALID_CONFIG;
2173 break;
2174 } /* switch value */
2175 }
2176 break;
2177 case AACENC_SAMPLERATE:
2178 if (settings->userSamplerate != value) {
2179 if (!((value == 8000) || (value == 11025) || (value == 12000) ||
2180 (value == 16000) || (value == 22050) || (value == 24000) ||
2181 (value == 32000) || (value == 44100) || (value == 48000) ||
2182 (value == 64000) || (value == 88200) || (value == 96000))) {
2183 err = AACENC_INVALID_CONFIG;
2184 break;
2185 }
2186 settings->userSamplerate = value;
2187 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2188 hAacEncoder->InitFlags |=
2189 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2190 }
2191 break;
2192 case AACENC_CHANNELMODE:
2193 if (settings->userChannelMode != (CHANNEL_MODE)value) {
2194 if (((CHANNEL_MODE)value == MODE_212) &&
2195 (NULL != hAacEncoder->hMpsEnc)) {
2196 settings->userChannelMode = (CHANNEL_MODE)value;
2197 settings->nChannels = 2;
2198 } else {
2199 const CHANNEL_MODE_CONFIG_TAB *pConfig =
2200 FDKaacEnc_GetChannelModeConfiguration((CHANNEL_MODE)value);
2201 if (pConfig == NULL) {
2202 err = AACENC_INVALID_CONFIG;
2203 break;
2204 }
2205 if ((pConfig->nElements > hAacEncoder->nMaxAacElements) ||
2206 (pConfig->nChannelsEff > hAacEncoder->nMaxAacChannels)) {
2207 err = AACENC_INVALID_CONFIG;
2208 break;
2209 }
2210
2211 settings->userChannelMode = (CHANNEL_MODE)value;
2212 settings->nChannels = pConfig->nChannels;
2213 }
2214 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2215 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2216 if (!((value >= 1) && (value <= 6))) {
2217 hAacEncoder->InitFlags |= AACENC_INIT_STATES;
2218 }
2219 }
2220 break;
2221 case AACENC_BANDWIDTH:
2222 if (settings->userBandwidth != value) {
2223 settings->userBandwidth = value;
2224 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2225 }
2226 break;
2227 case AACENC_CHANNELORDER:
2228 if (hAacEncoder->aacConfig.channelOrder != (CHANNEL_ORDER)value) {
2229 if (!((value == 0) || (value == 1))) {
2230 err = AACENC_INVALID_CONFIG;
2231 break;
2232 }
2233 hAacEncoder->aacConfig.channelOrder = (CHANNEL_ORDER)value;
2234 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2235 hAacEncoder->InitFlags |=
2236 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2237 }
2238 break;
2239 case AACENC_AFTERBURNER:
2240 if (settings->userAfterburner != value) {
2241 if (!((value == 0) || (value == 1))) {
2242 err = AACENC_INVALID_CONFIG;
2243 break;
2244 }
2245 settings->userAfterburner = value;
2246 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2247 }
2248 break;
2249 case AACENC_GRANULE_LENGTH:
2250 if (settings->userFramelength != value) {
2251 switch (value) {
2252 case 1024:
2253 case 512:
2254 case 480:
2255 case 256:
2256 case 240:
2257 case 128:
2258 case 120:
2259 if ((value << 1) == 480 || (value << 1) == 512) {
2260 settings->userDownscaleFactor = 2;
2261 } else if ((value << 2) == 480 || (value << 2) == 512) {
2262 settings->userDownscaleFactor = 4;
2263 }
2264 settings->userFramelength = value;
2265 hAacEncoder->InitFlags |=
2266 AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2267 break;
2268 default:
2269 err = AACENC_INVALID_CONFIG;
2270 break;
2271 }
2272 }
2273 break;
2274 case AACENC_SBR_RATIO:
2275 if (settings->userSbrRatio != value) {
2276 if (!((value == 0) || (value == 1) || (value == 2))) {
2277 err = AACENC_INVALID_CONFIG;
2278 break;
2279 }
2280 settings->userSbrRatio = value;
2281 hAacEncoder->InitFlags |=
2282 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2283 }
2284 break;
2285 case AACENC_SBR_MODE:
2286 if ((settings->userSbrEnabled != value) &&
2287 (NULL != hAacEncoder->hEnvEnc)) {
2288 settings->userSbrEnabled = value;
2289 hAacEncoder->InitFlags |=
2290 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2291 }
2292 break;
2293 case AACENC_TRANSMUX:
2294 if (settings->userTpType != (TRANSPORT_TYPE)value) {
2295 TRANSPORT_TYPE type = (TRANSPORT_TYPE)value;
2296 UINT flags = hAacEncoder->CAPF_tpEnc;
2297
2298 if (!(((type == TT_MP4_ADIF) && (flags & CAPF_ADIF)) ||
2299 ((type == TT_MP4_ADTS) && (flags & CAPF_ADTS)) ||
2300 ((type == TT_MP4_LATM_MCP0) &&
2301 ((flags & CAPF_LATM) && (flags & CAPF_RAWPACKETS))) ||
2302 ((type == TT_MP4_LATM_MCP1) &&
2303 ((flags & CAPF_LATM) && (flags & CAPF_RAWPACKETS))) ||
2304 ((type == TT_MP4_LOAS) && (flags & CAPF_LOAS)) ||
2305 ((type == TT_MP4_RAW) && (flags & CAPF_RAWPACKETS)))) {
2306 err = AACENC_INVALID_CONFIG;
2307 break;
2308 }
2309 settings->userTpType = (TRANSPORT_TYPE)value;
2310 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2311 }
2312 break;
2313 case AACENC_SIGNALING_MODE:
2314 if (settings->userTpSignaling != value) {
2315 if (!((value == 0) || (value == 1) || (value == 2))) {
2316 err = AACENC_INVALID_CONFIG;
2317 break;
2318 }
2319 settings->userTpSignaling = value;
2320 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2321 }
2322 break;
2323 case AACENC_PROTECTION:
2324 if (settings->userTpProtection != value) {
2325 if (!((value == 0) || (value == 1))) {
2326 err = AACENC_INVALID_CONFIG;
2327 break;
2328 }
2329 settings->userTpProtection = value;
2330 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2331 }
2332 break;
2333 case AACENC_HEADER_PERIOD:
2334 if (settings->userTpHeaderPeriod != value) {
2335 if (!(((INT)value >= 0) && (value <= 255))) {
2336 err = AACENC_INVALID_CONFIG;
2337 break;
2338 }
2339 settings->userTpHeaderPeriod = value;
2340 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2341 }
2342 break;
2343 case AACENC_AUDIOMUXVER:
2344 if (settings->userTpAmxv != value) {
2345 if (!((value == 0) || (value == 1) || (value == 2))) {
2346 err = AACENC_INVALID_CONFIG;
2347 break;
2348 }
2349 settings->userTpAmxv = value;
2350 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2351 }
2352 break;
2353 case AACENC_TPSUBFRAMES:
2354 if (settings->userTpNsubFrames != value) {
2355 if (!((value >= 1) && (value <= 4))) {
2356 err = AACENC_INVALID_CONFIG;
2357 break;
2358 }
2359 settings->userTpNsubFrames = value;
2360 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2361 }
2362 break;
2363 case AACENC_ANCILLARY_BITRATE:
2364 if (settings->userAncDataRate != value) {
2365 settings->userAncDataRate = value;
2366 }
2367 break;
2368 case AACENC_CONTROL_STATE:
2369 if (hAacEncoder->InitFlags != value) {
2370 if (value & AACENC_RESET_INBUFFER) {
2371 hAacEncoder->nSamplesRead = 0;
2372 }
2373 hAacEncoder->InitFlags = value;
2374 }
2375 break;
2376 case AACENC_METADATA_MODE:
2377 if ((UINT)settings->userMetaDataMode != value) {
2378 if (!(((INT)value >= 0) && ((INT)value <= 3))) {
2379 err = AACENC_INVALID_CONFIG;
2380 break;
2381 }
2382 settings->userMetaDataMode = value;
2383 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2384 }
2385 break;
2386 case AACENC_PEAK_BITRATE:
2387 if (settings->userPeakBitrate != value) {
2388 settings->userPeakBitrate = value;
2389 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2390 }
2391 break;
2392 default:
2393 err = AACENC_UNSUPPORTED_PARAMETER;
2394 break;
2395 } /* switch(param) */
2396
2397 bail:
2398 return err;
2399 }
2400
aacEncoder_GetParam(const HANDLE_AACENCODER hAacEncoder,const AACENC_PARAM param)2401 UINT aacEncoder_GetParam(const HANDLE_AACENCODER hAacEncoder,
2402 const AACENC_PARAM param) {
2403 UINT value = 0;
2404 USER_PARAM *settings = &hAacEncoder->extParam;
2405
2406 /* check encoder handle */
2407 if (hAacEncoder == NULL) {
2408 goto bail;
2409 }
2410
2411 /* apply param value */
2412 switch (param) {
2413 case AACENC_AOT:
2414 value = (UINT)hAacEncoder->aacConfig.audioObjectType;
2415 break;
2416 case AACENC_BITRATE:
2417 switch (hAacEncoder->aacConfig.bitrateMode) {
2418 case AACENC_BR_MODE_CBR:
2419 value = (UINT)hAacEncoder->aacConfig.bitRate;
2420 break;
2421 default:
2422 value = (UINT)-1;
2423 }
2424 break;
2425 case AACENC_BITRATEMODE:
2426 value = (UINT)((hAacEncoder->aacConfig.bitrateMode != AACENC_BR_MODE_FF)
2427 ? hAacEncoder->aacConfig.bitrateMode
2428 : AACENC_BR_MODE_CBR);
2429 break;
2430 case AACENC_SAMPLERATE:
2431 value = (UINT)hAacEncoder->coderConfig.extSamplingRate;
2432 break;
2433 case AACENC_CHANNELMODE:
2434 if ((MODE_1 == hAacEncoder->aacConfig.channelMode) &&
2435 (hAacEncoder->aacConfig.syntaxFlags & AC_LD_MPS)) {
2436 value = MODE_212;
2437 } else {
2438 value = (UINT)hAacEncoder->aacConfig.channelMode;
2439 }
2440 break;
2441 case AACENC_BANDWIDTH:
2442 value = (UINT)hAacEncoder->aacConfig.bandWidth;
2443 break;
2444 case AACENC_CHANNELORDER:
2445 value = (UINT)hAacEncoder->aacConfig.channelOrder;
2446 break;
2447 case AACENC_AFTERBURNER:
2448 value = (UINT)hAacEncoder->aacConfig.useRequant;
2449 break;
2450 case AACENC_GRANULE_LENGTH:
2451 value = (UINT)hAacEncoder->aacConfig.framelength;
2452 break;
2453 case AACENC_SBR_RATIO:
2454 value = isSbrActive(&hAacEncoder->aacConfig)
2455 ? hAacEncoder->aacConfig.sbrRatio
2456 : 0;
2457 break;
2458 case AACENC_SBR_MODE:
2459 value =
2460 (UINT)(hAacEncoder->aacConfig.syntaxFlags & AC_SBR_PRESENT) ? 1 : 0;
2461 break;
2462 case AACENC_TRANSMUX:
2463 value = (UINT)settings->userTpType;
2464 break;
2465 case AACENC_SIGNALING_MODE:
2466 value = (UINT)getSbrSignalingMode(
2467 hAacEncoder->aacConfig.audioObjectType, settings->userTpType,
2468 settings->userTpSignaling, hAacEncoder->aacConfig.sbrRatio);
2469 break;
2470 case AACENC_PROTECTION:
2471 value = (UINT)settings->userTpProtection;
2472 break;
2473 case AACENC_HEADER_PERIOD:
2474 value = (UINT)hAacEncoder->coderConfig.headerPeriod;
2475 break;
2476 case AACENC_AUDIOMUXVER:
2477 value = (UINT)hAacEncoder->aacConfig.audioMuxVersion;
2478 break;
2479 case AACENC_TPSUBFRAMES:
2480 value = (UINT)settings->userTpNsubFrames;
2481 break;
2482 case AACENC_ANCILLARY_BITRATE:
2483 value = (UINT)hAacEncoder->aacConfig.anc_Rate;
2484 break;
2485 case AACENC_CONTROL_STATE:
2486 value = (UINT)hAacEncoder->InitFlags;
2487 break;
2488 case AACENC_METADATA_MODE:
2489 value = (hAacEncoder->metaDataAllowed == 0)
2490 ? 0
2491 : (UINT)settings->userMetaDataMode;
2492 break;
2493 case AACENC_PEAK_BITRATE:
2494 value = (UINT)-1; /* peak bitrate parameter is meaningless */
2495 if (((INT)hAacEncoder->extParam.userPeakBitrate != -1)) {
2496 value =
2497 (UINT)(fMax((INT)hAacEncoder->extParam.userPeakBitrate,
2498 hAacEncoder->aacConfig
2499 .bitRate)); /* peak bitrate parameter is in use */
2500 }
2501 break;
2502
2503 default:
2504 // err = MPS_INVALID_PARAMETER;
2505 break;
2506 } /* switch(param) */
2507
2508 bail:
2509 return value;
2510 }
2511
aacEncInfo(const HANDLE_AACENCODER hAacEncoder,AACENC_InfoStruct * pInfo)2512 AACENC_ERROR aacEncInfo(const HANDLE_AACENCODER hAacEncoder,
2513 AACENC_InfoStruct *pInfo) {
2514 AACENC_ERROR err = AACENC_OK;
2515
2516 FDKmemclear(pInfo, sizeof(AACENC_InfoStruct));
2517 pInfo->confSize = 64; /* pre-initialize */
2518
2519 pInfo->maxOutBufBytes = ((hAacEncoder->nMaxAacChannels * 6144) + 7) >> 3;
2520 pInfo->maxAncBytes = hAacEncoder->aacConfig.maxAncBytesPerAU;
2521 pInfo->inBufFillLevel =
2522 hAacEncoder->nSamplesRead / hAacEncoder->extParam.nChannels;
2523 pInfo->inputChannels = hAacEncoder->extParam.nChannels;
2524 pInfo->frameLength =
2525 hAacEncoder->nSamplesToRead / hAacEncoder->extParam.nChannels;
2526 pInfo->nDelay = hAacEncoder->nDelay;
2527 pInfo->nDelayCore = hAacEncoder->nDelayCore;
2528
2529 /* Get encoder configuration */
2530 if (aacEncGetConf(hAacEncoder, &pInfo->confSize, &pInfo->confBuf[0]) !=
2531 AAC_ENC_OK) {
2532 err = AACENC_INIT_ERROR;
2533 goto bail;
2534 }
2535 bail:
2536 return err;
2537 }
2538