1 /* -----------------------------------------------------------------------------
2 Software License for The Fraunhofer FDK AAC Codec Library for Android
3
4 © Copyright 1995 - 2021 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 SUPPRESS_BUILD_DATE_INFO
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 /* Adjust bitrate mode in case given peak bitrate is lower than expected
1032 * VBR bitrate. */
1033 if ((INT)config->userPeakBitrate != -1) {
1034 hAacConfig->bitrateMode = FDKaacEnc_AdjustVBRBitrateMode(
1035 hAacConfig->bitrateMode, config->userPeakBitrate,
1036 hAacConfig->channelMode);
1037 }
1038 /* Get bitrate in VBR configuration */
1039 /* In VBR mode; SBR-modul depends on bitrate, core encoder on bitrateMode.
1040 */
1041 hAacConfig->bitRate = FDKaacEnc_GetVBRBitrate(hAacConfig->bitrateMode,
1042 hAacConfig->channelMode);
1043 break;
1044 default:
1045 return AACENC_INVALID_CONFIG;
1046 }
1047
1048 /* set bitreservoir size */
1049 switch (hAacConfig->bitrateMode) {
1050 case AACENC_BR_MODE_VBR_1:
1051 case AACENC_BR_MODE_VBR_2:
1052 case AACENC_BR_MODE_VBR_3:
1053 case AACENC_BR_MODE_VBR_4:
1054 case AACENC_BR_MODE_VBR_5:
1055 case AACENC_BR_MODE_CBR:
1056 if ((INT)config->userPeakBitrate != -1) {
1057 hAacConfig->maxBitsPerFrame =
1058 (FDKaacEnc_CalcBitsPerFrame(
1059 fMax(hAacConfig->bitRate, (INT)config->userPeakBitrate),
1060 hAacConfig->framelength, hAacConfig->sampleRate) +
1061 7) &
1062 ~7;
1063 } else {
1064 hAacConfig->maxBitsPerFrame = -1;
1065 }
1066 if (hAacConfig->audioMuxVersion == 2) {
1067 hAacConfig->minBitsPerFrame =
1068 fMin(32 * 8, FDKaacEnc_CalcBitsPerFrame(hAacConfig->bitRate,
1069 hAacConfig->framelength,
1070 hAacConfig->sampleRate)) &
1071 ~7;
1072 }
1073 break;
1074 default:
1075 return AACENC_INVALID_CONFIG;
1076 }
1077
1078 /* Max bits per frame limitation depending on transport format. */
1079 if ((config->userTpNsubFrames > 1)) {
1080 int maxFrameLength = 8 * hAacEncoder->outBufferInBytes;
1081 switch (config->userTpType) {
1082 case TT_MP4_LOAS:
1083 maxFrameLength =
1084 fMin(maxFrameLength, 8 * (1 << 13)) / config->userTpNsubFrames;
1085 break;
1086 case TT_MP4_ADTS:
1087 maxFrameLength = fMin(maxFrameLength, 8 * ((1 << 13) - 1)) /
1088 config->userTpNsubFrames;
1089 break;
1090 default:
1091 maxFrameLength = -1;
1092 }
1093 if (maxFrameLength != -1) {
1094 if (hAacConfig->maxBitsPerFrame > maxFrameLength) {
1095 return AACENC_INVALID_CONFIG;
1096 } else if (hAacConfig->maxBitsPerFrame == -1) {
1097 hAacConfig->maxBitsPerFrame = maxFrameLength;
1098 }
1099 }
1100 }
1101
1102 if ((hAacConfig->audioObjectType == AOT_ER_AAC_ELD) &&
1103 !(hAacConfig->syntaxFlags & AC_ELD_DOWNSCALE) &&
1104 (config->userSbrEnabled == (UCHAR)-1) && (config->userSbrRatio == 0) &&
1105 ((hAacConfig->syntaxFlags & AC_LD_MPS) == 0)) {
1106 const ELD_SBR_CONFIGURATOR *pConfig = NULL;
1107
1108 if (NULL !=
1109 (pConfig = eldSbrConfigurator(
1110 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1111 ->nChannels,
1112 hAacConfig->sampleRate, hAacConfig->bitRate))) {
1113 hAacConfig->syntaxFlags |= (pConfig->sbrMode == 0) ? 0 : AC_SBR_PRESENT;
1114 hAacConfig->syntaxFlags |= (pConfig->chMode == MODE_212) ? AC_LD_MPS : 0;
1115 hAacConfig->channelMode =
1116 GetCoreChannelMode(pConfig->chMode, hAacConfig->audioObjectType);
1117 hAacConfig->nChannels =
1118 FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1119 ->nChannels;
1120 hAacConfig->sbrRatio =
1121 (pConfig->sbrMode == 0) ? 0 : (pConfig->sbrMode == 1) ? 1 : 2;
1122 }
1123 }
1124
1125 {
1126 UCHAR tpSignaling =
1127 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType,
1128 config->userTpSignaling, hAacConfig->sbrRatio);
1129
1130 if ((hAacConfig->audioObjectType == AOT_AAC_LC ||
1131 hAacConfig->audioObjectType == AOT_SBR ||
1132 hAacConfig->audioObjectType == AOT_PS) &&
1133 (config->userTpType == TT_MP4_LATM_MCP1 ||
1134 config->userTpType == TT_MP4_LATM_MCP0 ||
1135 config->userTpType == TT_MP4_LOAS) &&
1136 (tpSignaling == 1) && (config->userTpAmxv == 0)) {
1137 /* For backward compatible explicit signaling, AMV1 has to be active */
1138 return AACENC_INVALID_CONFIG;
1139 }
1140
1141 if ((hAacConfig->audioObjectType == AOT_AAC_LC ||
1142 hAacConfig->audioObjectType == AOT_SBR ||
1143 hAacConfig->audioObjectType == AOT_PS) &&
1144 (tpSignaling == 0) && (hAacConfig->sbrRatio == 1)) {
1145 /* Downsampled SBR has to be signaled explicitely (for transmission of SBR
1146 * sampling fequency) */
1147 return AACENC_INVALID_CONFIG;
1148 }
1149 }
1150
1151 switch (hAacConfig->bitrateMode) {
1152 case AACENC_BR_MODE_CBR:
1153 case AACENC_BR_MODE_VBR_1:
1154 case AACENC_BR_MODE_VBR_2:
1155 case AACENC_BR_MODE_VBR_3:
1156 case AACENC_BR_MODE_VBR_4:
1157 case AACENC_BR_MODE_VBR_5:
1158 /* We need the frame length to call aacEncoder_LimitBitrate() */
1159 if (0 >= (hAacConfig->bitRate = aacEncoder_LimitBitrate(
1160 NULL, hAacConfig->sampleRate, hAacConfig->framelength,
1161 hAacConfig->nChannels, hAacConfig->channelMode,
1162 hAacConfig->bitRate, hAacConfig->nSubFrames,
1163 isSbrActive(hAacConfig), hAacConfig->sbrRatio,
1164 hAacConfig->syntaxFlags, hAacConfig->audioObjectType))) {
1165 return AACENC_INVALID_CONFIG;
1166 }
1167 break;
1168 default:
1169 break;
1170 }
1171
1172 /* Configure PNS */
1173 if (AACENC_BR_MODE_IS_VBR(hAacConfig->bitrateMode) /* VBR without PNS. */
1174 || (hAacConfig->useTns == 0)) /* TNS required. */
1175 {
1176 hAacConfig->usePns = 0;
1177 }
1178
1179 if (hAacConfig->epConfig >= 0) {
1180 hAacConfig->syntaxFlags |= AC_ER;
1181 if (((INT)hAacConfig->channelMode < 1) ||
1182 ((INT)hAacConfig->channelMode > 14)) {
1183 return AACENC_INVALID_CONFIG; /* Channel config 0 not supported. */
1184 }
1185 }
1186
1187 if ((hAacConfig->syntaxFlags & AC_LD_MPS) == 0) {
1188 if (FDKaacEnc_DetermineEncoderMode(&hAacConfig->channelMode,
1189 hAacConfig->nChannels) != AAC_ENC_OK) {
1190 return AACENC_INVALID_CONFIG; /* nChannels doesn't match chMode, this is
1191 just a check-up */
1192 }
1193 }
1194
1195 if ((hAacConfig->nChannels > hAacEncoder->nMaxAacChannels) ||
1196 ((FDKaacEnc_GetChannelModeConfiguration(hAacConfig->channelMode)
1197 ->nChannelsEff > hAacEncoder->nMaxSbrChannels) &&
1198 isSbrActive(hAacConfig))) {
1199 return AACENC_INVALID_CONFIG; /* not enough channels allocated */
1200 }
1201
1202 /* Meta data restriction. */
1203 switch (hAacConfig->audioObjectType) {
1204 /* Allow metadata support */
1205 case AOT_AAC_LC:
1206 case AOT_SBR:
1207 case AOT_PS:
1208 case AOT_MP2_AAC_LC:
1209 case AOT_MP2_SBR:
1210 hAacEncoder->metaDataAllowed = 1;
1211 if (!((((INT)hAacConfig->channelMode >= 1) &&
1212 ((INT)hAacConfig->channelMode <= 14)) ||
1213 (MODE_7_1_REAR_SURROUND == hAacConfig->channelMode) ||
1214 (MODE_7_1_FRONT_CENTER == hAacConfig->channelMode))) {
1215 config->userMetaDataMode = 0;
1216 }
1217 break;
1218 /* Prohibit metadata support */
1219 default:
1220 hAacEncoder->metaDataAllowed = 0;
1221 }
1222
1223 return err;
1224 }
1225
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)1226 static INT aacenc_SbrCallback(void *self, HANDLE_FDK_BITSTREAM hBs,
1227 const INT sampleRateIn, const INT sampleRateOut,
1228 const INT samplesPerFrame,
1229 const AUDIO_OBJECT_TYPE coreCodec,
1230 const MP4_ELEMENT_ID elementID,
1231 const INT elementIndex, const UCHAR harmonicSbr,
1232 const UCHAR stereoConfigIndex,
1233 const UCHAR configMode, UCHAR *configChanged,
1234 const INT downscaleFactor) {
1235 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self;
1236
1237 sbrEncoder_GetHeader(hAacEncoder->hEnvEnc, hBs, elementIndex, 0);
1238
1239 return 0;
1240 }
1241
aacenc_SscCallback(void * self,HANDLE_FDK_BITSTREAM hBs,const AUDIO_OBJECT_TYPE coreCodec,const INT samplingRate,const INT frameSize,const INT numChannels,const INT stereoConfigIndex,const INT coreSbrFrameLengthIndex,const INT configBytes,const UCHAR configMode,UCHAR * configChanged)1242 INT aacenc_SscCallback(void *self, HANDLE_FDK_BITSTREAM hBs,
1243 const AUDIO_OBJECT_TYPE coreCodec,
1244 const INT samplingRate, const INT frameSize,
1245 const INT numChannels, const INT stereoConfigIndex,
1246 const INT coreSbrFrameLengthIndex, const INT configBytes,
1247 const UCHAR configMode, UCHAR *configChanged) {
1248 HANDLE_AACENCODER hAacEncoder = (HANDLE_AACENCODER)self;
1249
1250 return (FDK_MpegsEnc_WriteSpatialSpecificConfig(hAacEncoder->hMpsEnc, hBs));
1251 }
1252
aacEncInit(HANDLE_AACENCODER hAacEncoder,ULONG InitFlags,USER_PARAM * config)1253 static AACENC_ERROR aacEncInit(HANDLE_AACENCODER hAacEncoder, ULONG InitFlags,
1254 USER_PARAM *config) {
1255 AACENC_ERROR err = AACENC_OK;
1256
1257 INT aacBufferOffset = 0;
1258 HANDLE_SBR_ENCODER *hSbrEncoder = &hAacEncoder->hEnvEnc;
1259 HANDLE_AACENC_CONFIG hAacConfig = &hAacEncoder->aacConfig;
1260
1261 hAacEncoder->nZerosAppended = 0; /* count appended zeros */
1262
1263 INT frameLength = hAacConfig->framelength;
1264
1265 if ((InitFlags & AACENC_INIT_CONFIG)) {
1266 CHANNEL_MODE prevChMode = hAacConfig->channelMode;
1267
1268 /* Verify settings and update: config -> heAacEncoder */
1269 if ((err = FDKaacEnc_AdjustEncSettings(hAacEncoder, config)) != AACENC_OK) {
1270 return err;
1271 }
1272 frameLength = hAacConfig->framelength; /* adapt temporal framelength */
1273
1274 /* Seamless channel reconfiguration in sbr not fully implemented */
1275 if ((prevChMode != hAacConfig->channelMode) && isSbrActive(hAacConfig)) {
1276 InitFlags |= AACENC_INIT_STATES;
1277 }
1278 }
1279
1280 /* Clear input buffer */
1281 if (InitFlags == AACENC_INIT_ALL) {
1282 FDKmemclear(hAacEncoder->inputBuffer,
1283 sizeof(INT_PCM) * hAacEncoder->inputBufferSize);
1284 }
1285
1286 if ((InitFlags & AACENC_INIT_CONFIG)) {
1287 aacBufferOffset = 0;
1288 switch (hAacConfig->audioObjectType) {
1289 case AOT_ER_AAC_LD:
1290 hAacEncoder->nDelay = DELAY_AACLD(hAacConfig->framelength);
1291 break;
1292 case AOT_ER_AAC_ELD:
1293 hAacEncoder->nDelay = DELAY_AACELD(hAacConfig->framelength);
1294 break;
1295 default:
1296 hAacEncoder->nDelay =
1297 DELAY_AAC(hAacConfig->framelength); /* AAC encoder delay */
1298 }
1299
1300 hAacConfig->ancDataBitRate = 0;
1301 }
1302
1303 if ((NULL != hAacEncoder->hEnvEnc) && isSbrActive(hAacConfig) &&
1304 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES))) {
1305 INT sbrError;
1306 UINT initFlag = 0;
1307 SBR_ELEMENT_INFO sbrElInfo[(8)];
1308 CHANNEL_MAPPING channelMapping;
1309 CHANNEL_MODE channelMode = isPsActive(hAacConfig->audioObjectType)
1310 ? config->userChannelMode
1311 : hAacConfig->channelMode;
1312 INT numChannels = isPsActive(hAacConfig->audioObjectType)
1313 ? config->nChannels
1314 : hAacConfig->nChannels;
1315
1316 if (FDKaacEnc_InitChannelMapping(channelMode, hAacConfig->channelOrder,
1317 &channelMapping) != AAC_ENC_OK) {
1318 return AACENC_INIT_ERROR;
1319 }
1320
1321 /* Check return value and if the SBR encoder can handle enough elements */
1322 if (channelMapping.nElements > (8)) {
1323 return AACENC_INIT_ERROR;
1324 }
1325
1326 aacEncDistributeSbrBits(&channelMapping, sbrElInfo, hAacConfig->bitRate);
1327
1328 initFlag += (InitFlags & AACENC_INIT_STATES) ? 1 : 0;
1329
1330 /* Let the SBR encoder take a look at the configuration and change if
1331 * required. */
1332 sbrError = sbrEncoder_Init(
1333 *hSbrEncoder, sbrElInfo, channelMapping.nElements,
1334 hAacEncoder->inputBuffer, hAacEncoder->inputBufferSizePerChannel,
1335 &hAacConfig->bandWidth, &aacBufferOffset, &numChannels,
1336 hAacConfig->syntaxFlags, &hAacConfig->sampleRate, &hAacConfig->sbrRatio,
1337 &frameLength, hAacConfig->audioObjectType, &hAacEncoder->nDelay,
1338 (hAacConfig->audioObjectType == AOT_ER_AAC_ELD) ? 1 : TRANS_FAC,
1339 (config->userTpHeaderPeriod != 0xFF)
1340 ? config->userTpHeaderPeriod
1341 : DEFAULT_HEADER_PERIOD_REPETITION_RATE,
1342 initFlag);
1343
1344 /* Suppress AOT reconfiguration and check error status. */
1345 if ((sbrError) || (numChannels != hAacConfig->nChannels)) {
1346 return AACENC_INIT_SBR_ERROR;
1347 }
1348
1349 if (numChannels == 1) {
1350 hAacConfig->channelMode = MODE_1;
1351 }
1352
1353 /* Never use PNS if SBR is active */
1354 if (hAacConfig->usePns) {
1355 hAacConfig->usePns = 0;
1356 }
1357
1358 /* estimated bitrate consumed by SBR or PS */
1359 hAacConfig->ancDataBitRate = sbrEncoder_GetEstimateBitrate(*hSbrEncoder);
1360
1361 } /* sbr initialization */
1362
1363 if ((hAacEncoder->hMpsEnc != NULL) && (hAacConfig->syntaxFlags & AC_LD_MPS)) {
1364 int coreCoderDelay = DELAY_AACELD(hAacConfig->framelength);
1365
1366 if (isSbrActive(hAacConfig)) {
1367 coreCoderDelay = hAacConfig->sbrRatio * coreCoderDelay +
1368 sbrEncoder_GetInputDataDelay(*hSbrEncoder);
1369 }
1370
1371 if (MPS_ENCODER_OK !=
1372 FDK_MpegsEnc_Init(hAacEncoder->hMpsEnc, hAacConfig->audioObjectType,
1373 config->userSamplerate, hAacConfig->bitRate,
1374 isSbrActive(hAacConfig) ? hAacConfig->sbrRatio : 0,
1375 frameLength, /* for dual rate sbr this value is
1376 already multiplied by 2 */
1377 hAacEncoder->inputBufferSizePerChannel,
1378 coreCoderDelay)) {
1379 return AACENC_INIT_MPS_ERROR;
1380 }
1381 }
1382 hAacEncoder->nDelay =
1383 fMax(FDK_MpegsEnc_GetDelay(hAacEncoder->hMpsEnc), hAacEncoder->nDelay);
1384
1385 /*
1386 * Initialize Transport - Module.
1387 */
1388 if ((InitFlags & AACENC_INIT_TRANSPORT)) {
1389 UINT flags = 0;
1390
1391 FDKaacEnc_MapConfig(
1392 &hAacEncoder->coderConfig, config,
1393 getSbrSignalingMode(hAacConfig->audioObjectType, config->userTpType,
1394 config->userTpSignaling, hAacConfig->sbrRatio),
1395 hAacConfig);
1396
1397 /* create flags for transport encoder */
1398 if (config->userTpAmxv != 0) {
1399 flags |= TP_FLAG_LATM_AMV;
1400 }
1401 /* Clear output buffer */
1402 FDKmemclear(hAacEncoder->outBuffer,
1403 hAacEncoder->outBufferInBytes * sizeof(UCHAR));
1404
1405 /* Initialize Bitstream encoder */
1406 if (transportEnc_Init(hAacEncoder->hTpEnc, hAacEncoder->outBuffer,
1407 hAacEncoder->outBufferInBytes, config->userTpType,
1408 &hAacEncoder->coderConfig, flags) != 0) {
1409 return AACENC_INIT_TP_ERROR;
1410 }
1411
1412 } /* transport initialization */
1413
1414 /*
1415 * Initialize AAC - Core.
1416 */
1417 if ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES)) {
1418 if (FDKaacEnc_Initialize(
1419 hAacEncoder->hAacEnc, hAacConfig, hAacEncoder->hTpEnc,
1420 (InitFlags & AACENC_INIT_STATES) ? 1 : 0) != AAC_ENC_OK) {
1421 return AACENC_INIT_AAC_ERROR;
1422 }
1423
1424 } /* aac initialization */
1425
1426 /*
1427 * Initialize Meta Data - Encoder.
1428 */
1429 if (hAacEncoder->hMetadataEnc && (hAacEncoder->metaDataAllowed != 0) &&
1430 ((InitFlags & AACENC_INIT_CONFIG) || (InitFlags & AACENC_INIT_STATES))) {
1431 INT inputDataDelay = DELAY_AAC(hAacConfig->framelength);
1432
1433 if (isSbrActive(hAacConfig) && hSbrEncoder != NULL) {
1434 inputDataDelay = hAacConfig->sbrRatio * inputDataDelay +
1435 sbrEncoder_GetInputDataDelay(*hSbrEncoder);
1436 }
1437
1438 if (FDK_MetadataEnc_Init(hAacEncoder->hMetadataEnc,
1439 ((InitFlags & AACENC_INIT_STATES) ? 1 : 0),
1440 config->userMetaDataMode, inputDataDelay,
1441 frameLength, config->userSamplerate,
1442 config->nChannels, config->userChannelMode,
1443 hAacConfig->channelOrder) != 0) {
1444 return AACENC_INIT_META_ERROR;
1445 }
1446
1447 hAacEncoder->nDelay += FDK_MetadataEnc_GetDelay(hAacEncoder->hMetadataEnc);
1448 }
1449
1450 /* Get custom delay, i.e. the codec delay w/o the decoder's SBR- or MPS delay
1451 */
1452 if ((hAacEncoder->hMpsEnc != NULL) && (hAacConfig->syntaxFlags & AC_LD_MPS)) {
1453 hAacEncoder->nDelayCore =
1454 hAacEncoder->nDelay -
1455 fMax(0, FDK_MpegsEnc_GetDecDelay(hAacEncoder->hMpsEnc));
1456 } else if (isSbrActive(hAacConfig) && hSbrEncoder != NULL) {
1457 hAacEncoder->nDelayCore =
1458 hAacEncoder->nDelay -
1459 fMax(0, sbrEncoder_GetSbrDecDelay(hAacEncoder->hEnvEnc));
1460 } else {
1461 hAacEncoder->nDelayCore = hAacEncoder->nDelay;
1462 }
1463
1464 /*
1465 * Update pointer to working buffer.
1466 */
1467 if ((InitFlags & AACENC_INIT_CONFIG)) {
1468 hAacEncoder->inputBufferOffset = aacBufferOffset;
1469
1470 hAacEncoder->nSamplesToRead = frameLength * config->nChannels;
1471
1472 } /* parameter changed */
1473
1474 return AACENC_OK;
1475 }
1476
aacEncOpen(HANDLE_AACENCODER * phAacEncoder,const UINT encModules,const UINT maxChannels)1477 AACENC_ERROR aacEncOpen(HANDLE_AACENCODER *phAacEncoder, const UINT encModules,
1478 const UINT maxChannels) {
1479 AACENC_ERROR err = AACENC_OK;
1480 HANDLE_AACENCODER hAacEncoder = NULL;
1481
1482 if (phAacEncoder == NULL) {
1483 err = AACENC_INVALID_HANDLE;
1484 goto bail;
1485 }
1486
1487 /* allocate memory */
1488 hAacEncoder = Get_AacEncoder();
1489
1490 if (hAacEncoder == NULL) {
1491 err = AACENC_MEMORY_ERROR;
1492 goto bail;
1493 }
1494
1495 FDKmemclear(hAacEncoder, sizeof(AACENCODER));
1496
1497 /* Specify encoder modules to be allocated. */
1498 if (encModules == 0) {
1499 C_ALLOC_SCRATCH_START(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1500 LIB_INFO(*pLibInfo)
1501 [FDK_MODULE_LAST] = (LIB_INFO(*)[FDK_MODULE_LAST])_pLibInfo;
1502 FDKinitLibInfo(*pLibInfo);
1503 aacEncGetLibInfo(*pLibInfo);
1504
1505 hAacEncoder->encoder_modis = ENC_MODE_FLAG_AAC;
1506 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_SBRENC) & CAPF_SBR_HQ) {
1507 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SBR;
1508 }
1509 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_SBRENC) & CAPF_SBR_PS_MPEG) {
1510 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_PS;
1511 }
1512 if (FDKlibInfo_getCapabilities(*pLibInfo, FDK_AACENC) & CAPF_AAC_DRC) {
1513 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_META;
1514 }
1515 hAacEncoder->encoder_modis |= ENC_MODE_FLAG_SAC;
1516
1517 C_ALLOC_SCRATCH_END(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1518 } else {
1519 hAacEncoder->encoder_modis = encModules;
1520 }
1521
1522 /* Determine max channel configuration. */
1523 if (maxChannels == 0) {
1524 hAacEncoder->nMaxAacChannels = (8);
1525 hAacEncoder->nMaxSbrChannels = (8);
1526 } else {
1527 hAacEncoder->nMaxAacChannels = (maxChannels & 0x00FF);
1528 if ((hAacEncoder->encoder_modis & ENC_MODE_FLAG_SBR)) {
1529 hAacEncoder->nMaxSbrChannels = (maxChannels & 0xFF00)
1530 ? (maxChannels >> 8)
1531 : hAacEncoder->nMaxAacChannels;
1532 }
1533
1534 if ((hAacEncoder->nMaxAacChannels > (8)) ||
1535 (hAacEncoder->nMaxSbrChannels > (8))) {
1536 err = AACENC_INVALID_CONFIG;
1537 goto bail;
1538 }
1539 } /* maxChannels==0 */
1540
1541 /* Max number of elements could be tuned any more. */
1542 hAacEncoder->nMaxAacElements = fixMin(((8)), hAacEncoder->nMaxAacChannels);
1543 hAacEncoder->nMaxSbrElements = fixMin((8), hAacEncoder->nMaxSbrChannels);
1544
1545 /* In case of memory overlay, allocate memory out of libraries */
1546
1547 if (hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR | ENC_MODE_FLAG_PS))
1548 hAacEncoder->inputBufferSizePerChannel = INPUTBUFFER_SIZE;
1549 else
1550 hAacEncoder->inputBufferSizePerChannel = (1024);
1551
1552 hAacEncoder->inputBufferSize =
1553 hAacEncoder->nMaxAacChannels * hAacEncoder->inputBufferSizePerChannel;
1554
1555 if (NULL == (hAacEncoder->inputBuffer = (INT_PCM *)FDKcalloc(
1556 hAacEncoder->inputBufferSize, sizeof(INT_PCM)))) {
1557 err = AACENC_MEMORY_ERROR;
1558 goto bail;
1559 }
1560
1561 /* Open SBR Encoder */
1562 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_SBR) {
1563 if (sbrEncoder_Open(
1564 &hAacEncoder->hEnvEnc, hAacEncoder->nMaxSbrElements,
1565 hAacEncoder->nMaxSbrChannels,
1566 (hAacEncoder->encoder_modis & ENC_MODE_FLAG_PS) ? 1 : 0)) {
1567 err = AACENC_MEMORY_ERROR;
1568 goto bail;
1569 }
1570
1571 if (NULL == (hAacEncoder->pSbrPayload = (SBRENC_EXT_PAYLOAD *)FDKcalloc(
1572 1, sizeof(SBRENC_EXT_PAYLOAD)))) {
1573 err = AACENC_MEMORY_ERROR;
1574 goto bail;
1575 }
1576 } /* (encoder_modis&ENC_MODE_FLAG_SBR) */
1577
1578 /* Open Aac Encoder */
1579 if (FDKaacEnc_Open(&hAacEncoder->hAacEnc, hAacEncoder->nMaxAacElements,
1580 hAacEncoder->nMaxAacChannels, (1)) != AAC_ENC_OK) {
1581 err = AACENC_MEMORY_ERROR;
1582 goto bail;
1583 }
1584
1585 /* Bitstream output buffer */
1586 hAacEncoder->outBufferInBytes =
1587 1 << (DFRACT_BITS - CntLeadingZeros(fixMax(
1588 1, ((1) * hAacEncoder->nMaxAacChannels * 6144) >>
1589 3))); /* buffer has to be 2^n */
1590 if (NULL == (hAacEncoder->outBuffer = (UCHAR *)FDKcalloc(
1591 hAacEncoder->outBufferInBytes, sizeof(UCHAR)))) {
1592 err = AACENC_MEMORY_ERROR;
1593 goto bail;
1594 }
1595
1596 /* Open Meta Data Encoder */
1597 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_META) {
1598 if (FDK_MetadataEnc_Open(&hAacEncoder->hMetadataEnc,
1599 (UINT)hAacEncoder->nMaxAacChannels)) {
1600 err = AACENC_MEMORY_ERROR;
1601 goto bail;
1602 }
1603 } /* (encoder_modis&ENC_MODE_FLAG_META) */
1604
1605 /* Open MPEG Surround Encoder */
1606 if (hAacEncoder->encoder_modis & ENC_MODE_FLAG_SAC) {
1607 if (MPS_ENCODER_OK != FDK_MpegsEnc_Open(&hAacEncoder->hMpsEnc)) {
1608 err = AACENC_MEMORY_ERROR;
1609 goto bail;
1610 }
1611 } /* (hAacEncoder->encoder_modis&ENC_MODE_FLAG_SAC) */
1612
1613 /* Open Transport Encoder */
1614 if (transportEnc_Open(&hAacEncoder->hTpEnc) != 0) {
1615 err = AACENC_MEMORY_ERROR;
1616 goto bail;
1617 } else {
1618 C_ALLOC_SCRATCH_START(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1619
1620 LIB_INFO(*pLibInfo)
1621 [FDK_MODULE_LAST] = (LIB_INFO(*)[FDK_MODULE_LAST])_pLibInfo;
1622
1623 FDKinitLibInfo(*pLibInfo);
1624 transportEnc_GetLibInfo(*pLibInfo);
1625
1626 /* Get capabilty flag for transport encoder. */
1627 hAacEncoder->CAPF_tpEnc = FDKlibInfo_getCapabilities(*pLibInfo, FDK_TPENC);
1628
1629 C_ALLOC_SCRATCH_END(_pLibInfo, LIB_INFO, FDK_MODULE_LAST)
1630 }
1631 if (transportEnc_RegisterSbrCallback(hAacEncoder->hTpEnc, aacenc_SbrCallback,
1632 hAacEncoder) != 0) {
1633 err = AACENC_INIT_TP_ERROR;
1634 goto bail;
1635 }
1636 if (transportEnc_RegisterSscCallback(hAacEncoder->hTpEnc, aacenc_SscCallback,
1637 hAacEncoder) != 0) {
1638 err = AACENC_INIT_TP_ERROR;
1639 goto bail;
1640 }
1641
1642 /* Initialize encoder instance with default parameters. */
1643 aacEncDefaultConfig(&hAacEncoder->aacConfig, &hAacEncoder->extParam);
1644
1645 /* Initialize headerPeriod in coderConfig for aacEncoder_GetParam(). */
1646 hAacEncoder->coderConfig.headerPeriod =
1647 hAacEncoder->extParam.userTpHeaderPeriod;
1648
1649 /* All encoder modules have to be initialized */
1650 hAacEncoder->InitFlags = AACENC_INIT_ALL;
1651
1652 /* Return encoder instance */
1653 *phAacEncoder = hAacEncoder;
1654
1655 return err;
1656
1657 bail:
1658 aacEncClose(&hAacEncoder);
1659
1660 return err;
1661 }
1662
aacEncClose(HANDLE_AACENCODER * phAacEncoder)1663 AACENC_ERROR aacEncClose(HANDLE_AACENCODER *phAacEncoder) {
1664 AACENC_ERROR err = AACENC_OK;
1665
1666 if (phAacEncoder == NULL) {
1667 err = AACENC_INVALID_HANDLE;
1668 goto bail;
1669 }
1670
1671 if (*phAacEncoder != NULL) {
1672 HANDLE_AACENCODER hAacEncoder = *phAacEncoder;
1673
1674 if (hAacEncoder->inputBuffer != NULL) {
1675 FDKfree(hAacEncoder->inputBuffer);
1676 hAacEncoder->inputBuffer = NULL;
1677 }
1678 if (hAacEncoder->outBuffer != NULL) {
1679 FDKfree(hAacEncoder->outBuffer);
1680 hAacEncoder->outBuffer = NULL;
1681 }
1682
1683 if (hAacEncoder->hEnvEnc) {
1684 sbrEncoder_Close(&hAacEncoder->hEnvEnc);
1685 }
1686 if (hAacEncoder->pSbrPayload != NULL) {
1687 FDKfree(hAacEncoder->pSbrPayload);
1688 hAacEncoder->pSbrPayload = NULL;
1689 }
1690 if (hAacEncoder->hAacEnc) {
1691 FDKaacEnc_Close(&hAacEncoder->hAacEnc);
1692 }
1693
1694 transportEnc_Close(&hAacEncoder->hTpEnc);
1695
1696 if (hAacEncoder->hMetadataEnc) {
1697 FDK_MetadataEnc_Close(&hAacEncoder->hMetadataEnc);
1698 }
1699 if (hAacEncoder->hMpsEnc) {
1700 FDK_MpegsEnc_Close(&hAacEncoder->hMpsEnc);
1701 }
1702
1703 Free_AacEncoder(phAacEncoder);
1704 }
1705
1706 bail:
1707 return err;
1708 }
1709
aacEncEncode(const HANDLE_AACENCODER hAacEncoder,const AACENC_BufDesc * inBufDesc,const AACENC_BufDesc * outBufDesc,const AACENC_InArgs * inargs,AACENC_OutArgs * outargs)1710 AACENC_ERROR aacEncEncode(const HANDLE_AACENCODER hAacEncoder,
1711 const AACENC_BufDesc *inBufDesc,
1712 const AACENC_BufDesc *outBufDesc,
1713 const AACENC_InArgs *inargs,
1714 AACENC_OutArgs *outargs) {
1715 AACENC_ERROR err = AACENC_OK;
1716 INT i, nBsBytes = 0;
1717 INT outBytes[(1)];
1718 int nExtensions = 0;
1719 int ancDataExtIdx = -1;
1720
1721 /* deal with valid encoder handle */
1722 if (hAacEncoder == NULL) {
1723 err = AACENC_INVALID_HANDLE;
1724 goto bail;
1725 }
1726
1727 /*
1728 * Adjust user settings and trigger reinitialization.
1729 */
1730 if (hAacEncoder->InitFlags != 0) {
1731 err =
1732 aacEncInit(hAacEncoder, hAacEncoder->InitFlags, &hAacEncoder->extParam);
1733
1734 if (err != AACENC_OK) {
1735 /* keep init flags alive! */
1736 goto bail;
1737 }
1738 hAacEncoder->InitFlags = AACENC_INIT_NONE;
1739 }
1740
1741 if (outargs != NULL) {
1742 FDKmemclear(outargs, sizeof(AACENC_OutArgs));
1743 }
1744
1745 if (outBufDesc != NULL) {
1746 for (i = 0; i < outBufDesc->numBufs; i++) {
1747 if (outBufDesc->bufs[i] != NULL) {
1748 FDKmemclear(outBufDesc->bufs[i], outBufDesc->bufSizes[i]);
1749 }
1750 }
1751 }
1752
1753 /*
1754 * If only encoder handle given, independent (re)initialization can be
1755 * triggered.
1756 */
1757 if ((inBufDesc == NULL) && (outBufDesc == NULL) && (inargs == NULL) &&
1758 (outargs == NULL)) {
1759 goto bail;
1760 }
1761
1762 /* check if buffer descriptors are filled out properly. */
1763 if ((inargs == NULL) || (outargs == NULL) ||
1764 ((AACENC_OK != validateBufDesc(inBufDesc)) &&
1765 (inargs->numInSamples > 0)) ||
1766 (AACENC_OK != validateBufDesc(outBufDesc))) {
1767 err = AACENC_UNSUPPORTED_PARAMETER;
1768 goto bail;
1769 }
1770
1771 /* reset buffer wich signals number of valid bytes in output bitstream buffer
1772 */
1773 FDKmemclear(outBytes, hAacEncoder->aacConfig.nSubFrames * sizeof(INT));
1774
1775 /*
1776 * Manage incoming audio samples.
1777 */
1778 if ((inBufDesc != NULL) && (inargs->numInSamples > 0) &&
1779 (getBufDescIdx(inBufDesc, IN_AUDIO_DATA) != -1)) {
1780 /* Fetch data until nSamplesToRead reached */
1781 INT idx = getBufDescIdx(inBufDesc, IN_AUDIO_DATA);
1782 INT newSamples =
1783 fixMax(0, fixMin(inargs->numInSamples, hAacEncoder->nSamplesToRead -
1784 hAacEncoder->nSamplesRead));
1785 INT_PCM *pIn =
1786 hAacEncoder->inputBuffer +
1787 hAacEncoder->inputBufferOffset / hAacEncoder->aacConfig.nChannels +
1788 hAacEncoder->nSamplesRead / hAacEncoder->extParam.nChannels;
1789 newSamples -=
1790 (newSamples %
1791 hAacEncoder->extParam
1792 .nChannels); /* process multiple samples of input channels */
1793
1794 /* Copy new input samples to internal buffer */
1795 if (inBufDesc->bufElSizes[idx] == (INT)sizeof(INT_PCM)) {
1796 FDK_deinterleave((INT_PCM *)inBufDesc->bufs[idx], pIn,
1797 hAacEncoder->extParam.nChannels,
1798 newSamples / hAacEncoder->extParam.nChannels,
1799 hAacEncoder->inputBufferSizePerChannel);
1800 } else if (inBufDesc->bufElSizes[idx] > (INT)sizeof(INT_PCM)) {
1801 FDK_deinterleave((LONG *)inBufDesc->bufs[idx], pIn,
1802 hAacEncoder->extParam.nChannels,
1803 newSamples / hAacEncoder->extParam.nChannels,
1804 hAacEncoder->inputBufferSizePerChannel);
1805 } else {
1806 FDK_deinterleave((SHORT *)inBufDesc->bufs[idx], pIn,
1807 hAacEncoder->extParam.nChannels,
1808 newSamples / hAacEncoder->extParam.nChannels,
1809 hAacEncoder->inputBufferSizePerChannel);
1810 }
1811 hAacEncoder->nSamplesRead += newSamples;
1812
1813 /* Number of fetched input buffer samples. */
1814 outargs->numInSamples = newSamples;
1815 }
1816
1817 /* input buffer completely filled ? */
1818 if (hAacEncoder->nSamplesRead < hAacEncoder->nSamplesToRead) {
1819 /* - eof reached and flushing enabled, or
1820 - return to main and wait for further incoming audio samples */
1821 if (inargs->numInSamples == -1) {
1822 if ((hAacEncoder->nZerosAppended < hAacEncoder->nDelay)) {
1823 int nZeros = (hAacEncoder->nSamplesToRead - hAacEncoder->nSamplesRead) /
1824 hAacEncoder->extParam.nChannels;
1825
1826 FDK_ASSERT(nZeros >= 0);
1827
1828 /* clear out until end-of-buffer */
1829 if (nZeros) {
1830 INT_PCM *pIn =
1831 hAacEncoder->inputBuffer +
1832 hAacEncoder->inputBufferOffset /
1833 hAacEncoder->aacConfig.nChannels +
1834 hAacEncoder->nSamplesRead / hAacEncoder->extParam.nChannels;
1835 for (i = 0; i < (int)hAacEncoder->extParam.nChannels; i++) {
1836 FDKmemclear(pIn + i * hAacEncoder->inputBufferSizePerChannel,
1837 sizeof(INT_PCM) * nZeros);
1838 }
1839 hAacEncoder->nZerosAppended += nZeros;
1840 hAacEncoder->nSamplesRead = hAacEncoder->nSamplesToRead;
1841 }
1842 } else { /* flushing completed */
1843 err = AACENC_ENCODE_EOF; /* eof reached */
1844 goto bail;
1845 }
1846 } else { /* inargs->numInSamples!= -1 */
1847 goto bail; /* not enough samples in input buffer and no flushing enabled
1848 */
1849 }
1850 }
1851
1852 /* init payload */
1853 FDKmemclear(hAacEncoder->extPayload,
1854 sizeof(AACENC_EXT_PAYLOAD) * MAX_TOTAL_EXT_PAYLOADS);
1855 for (i = 0; i < MAX_TOTAL_EXT_PAYLOADS; i++) {
1856 hAacEncoder->extPayload[i].associatedChElement = -1;
1857 }
1858 if (hAacEncoder->pSbrPayload != NULL) {
1859 FDKmemclear(hAacEncoder->pSbrPayload, sizeof(*hAacEncoder->pSbrPayload));
1860 }
1861
1862 /*
1863 * Calculate Meta Data info.
1864 */
1865 if ((hAacEncoder->hMetadataEnc != NULL) &&
1866 (hAacEncoder->metaDataAllowed != 0)) {
1867 const AACENC_MetaData *pMetaData = NULL;
1868 AACENC_EXT_PAYLOAD *pMetaDataExtPayload = NULL;
1869 UINT nMetaDataExtensions = 0;
1870 INT matrix_mixdown_idx = 0;
1871
1872 /* New meta data info available ? */
1873 if (getBufDescIdx(inBufDesc, IN_METADATA_SETUP) != -1) {
1874 pMetaData =
1875 (AACENC_MetaData *)
1876 inBufDesc->bufs[getBufDescIdx(inBufDesc, IN_METADATA_SETUP)];
1877 }
1878
1879 FDK_MetadataEnc_Process(
1880 hAacEncoder->hMetadataEnc,
1881 hAacEncoder->inputBuffer + hAacEncoder->inputBufferOffset /
1882 hAacEncoder->coderConfig.noChannels,
1883 hAacEncoder->inputBufferSizePerChannel, hAacEncoder->nSamplesRead,
1884 pMetaData, &pMetaDataExtPayload, &nMetaDataExtensions,
1885 &matrix_mixdown_idx);
1886
1887 for (i = 0; i < (INT)nMetaDataExtensions;
1888 i++) { /* Get meta data extension payload. */
1889 hAacEncoder->extPayload[nExtensions++] = pMetaDataExtPayload[i];
1890 }
1891
1892 if ((matrix_mixdown_idx != -1) &&
1893 ((hAacEncoder->extParam.userChannelMode == MODE_1_2_2) ||
1894 (hAacEncoder->extParam.userChannelMode == MODE_1_2_2_1))) {
1895 /* Set matrix mixdown coefficient. */
1896 UINT pceValue = (UINT)((0 << 3) | ((matrix_mixdown_idx & 0x3) << 1) | 1);
1897 if (hAacEncoder->extParam.userPceAdditions != pceValue) {
1898 hAacEncoder->extParam.userPceAdditions = pceValue;
1899 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
1900 }
1901 }
1902 }
1903
1904 /*
1905 * Encode MPS data.
1906 */
1907 if ((hAacEncoder->hMpsEnc != NULL) &&
1908 (hAacEncoder->aacConfig.syntaxFlags & AC_LD_MPS)) {
1909 AACENC_EXT_PAYLOAD mpsExtensionPayload;
1910 FDKmemclear(&mpsExtensionPayload, sizeof(AACENC_EXT_PAYLOAD));
1911
1912 if (MPS_ENCODER_OK !=
1913 FDK_MpegsEnc_Process(
1914 hAacEncoder->hMpsEnc,
1915 hAacEncoder->inputBuffer + hAacEncoder->inputBufferOffset /
1916 hAacEncoder->coderConfig.noChannels,
1917 hAacEncoder->nSamplesRead, &mpsExtensionPayload)) {
1918 err = AACENC_ENCODE_ERROR;
1919 goto bail;
1920 }
1921
1922 if ((mpsExtensionPayload.pData != NULL) &&
1923 ((mpsExtensionPayload.dataSize != 0))) {
1924 hAacEncoder->extPayload[nExtensions++] = mpsExtensionPayload;
1925 }
1926 }
1927
1928 if ((NULL != hAacEncoder->hEnvEnc) && (NULL != hAacEncoder->pSbrPayload) &&
1929 isSbrActive(&hAacEncoder->aacConfig)) {
1930 INT nPayload = 0;
1931
1932 /*
1933 * Encode SBR data.
1934 */
1935 if (sbrEncoder_EncodeFrame(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer,
1936 hAacEncoder->inputBufferSizePerChannel,
1937 hAacEncoder->pSbrPayload->dataSize[nPayload],
1938 hAacEncoder->pSbrPayload->data[nPayload])) {
1939 err = AACENC_ENCODE_ERROR;
1940 goto bail;
1941 } else {
1942 /* Add SBR extension payload */
1943 for (i = 0; i < (8); i++) {
1944 if (hAacEncoder->pSbrPayload->dataSize[nPayload][i] > 0) {
1945 hAacEncoder->extPayload[nExtensions].pData =
1946 hAacEncoder->pSbrPayload->data[nPayload][i];
1947 {
1948 hAacEncoder->extPayload[nExtensions].dataSize =
1949 hAacEncoder->pSbrPayload->dataSize[nPayload][i];
1950 hAacEncoder->extPayload[nExtensions].associatedChElement =
1951 getAssociatedChElement(
1952 &hAacEncoder->hEnvEnc->sbrElement[i]->elInfo,
1953 &hAacEncoder->hAacEnc->channelMapping);
1954 if (hAacEncoder->extPayload[nExtensions].associatedChElement ==
1955 -1) {
1956 err = AACENC_ENCODE_ERROR;
1957 goto bail;
1958 }
1959 }
1960 hAacEncoder->extPayload[nExtensions].dataType =
1961 EXT_SBR_DATA; /* Once SBR Encoder supports SBR CRC set
1962 EXT_SBR_DATA_CRC */
1963 nExtensions++; /* or EXT_SBR_DATA according to configuration. */
1964 FDK_ASSERT(nExtensions <= MAX_TOTAL_EXT_PAYLOADS);
1965 }
1966 }
1967 nPayload++;
1968 }
1969 } /* sbrEnabled */
1970
1971 if ((inargs->numAncBytes > 0) &&
1972 (getBufDescIdx(inBufDesc, IN_ANCILLRY_DATA) != -1)) {
1973 INT idx = getBufDescIdx(inBufDesc, IN_ANCILLRY_DATA);
1974 hAacEncoder->extPayload[nExtensions].dataSize = inargs->numAncBytes * 8;
1975 hAacEncoder->extPayload[nExtensions].pData = (UCHAR *)inBufDesc->bufs[idx];
1976 hAacEncoder->extPayload[nExtensions].dataType = EXT_DATA_ELEMENT;
1977 hAacEncoder->extPayload[nExtensions].associatedChElement = -1;
1978 ancDataExtIdx = nExtensions; /* store index */
1979 nExtensions++;
1980 }
1981
1982 /*
1983 * Encode AAC - Core.
1984 */
1985 if (FDKaacEnc_EncodeFrame(hAacEncoder->hAacEnc, hAacEncoder->hTpEnc,
1986 hAacEncoder->inputBuffer,
1987 hAacEncoder->inputBufferSizePerChannel, outBytes,
1988 hAacEncoder->extPayload) != AAC_ENC_OK) {
1989 err = AACENC_ENCODE_ERROR;
1990 goto bail;
1991 }
1992
1993 if (ancDataExtIdx >= 0) {
1994 outargs->numAncBytes =
1995 inargs->numAncBytes -
1996 (hAacEncoder->extPayload[ancDataExtIdx].dataSize >> 3);
1997 }
1998
1999 /* samples exhausted */
2000 hAacEncoder->nSamplesRead -= hAacEncoder->nSamplesToRead;
2001
2002 /*
2003 * Delay balancing buffer handling
2004 */
2005 if (isSbrActive(&hAacEncoder->aacConfig)) {
2006 sbrEncoder_UpdateBuffers(hAacEncoder->hEnvEnc, hAacEncoder->inputBuffer,
2007 hAacEncoder->inputBufferSizePerChannel);
2008 }
2009
2010 /*
2011 * Make bitstream public
2012 */
2013 if ((outBufDesc != NULL) && (outBufDesc->numBufs >= 1)) {
2014 INT bsIdx = getBufDescIdx(outBufDesc, OUT_BITSTREAM_DATA);
2015 INT auIdx = getBufDescIdx(outBufDesc, OUT_AU_SIZES);
2016
2017 for (i = 0, nBsBytes = 0; i < hAacEncoder->aacConfig.nSubFrames; i++) {
2018 nBsBytes += outBytes[i];
2019
2020 if (auIdx != -1) {
2021 ((INT *)outBufDesc->bufs[auIdx])[i] = outBytes[i];
2022 }
2023 }
2024
2025 if ((bsIdx != -1) && (outBufDesc->bufSizes[bsIdx] >= nBsBytes)) {
2026 FDKmemcpy(outBufDesc->bufs[bsIdx], hAacEncoder->outBuffer,
2027 sizeof(UCHAR) * nBsBytes);
2028 outargs->numOutBytes = nBsBytes;
2029 outargs->bitResState =
2030 FDKaacEnc_GetBitReservoirState(hAacEncoder->hAacEnc);
2031 } else {
2032 /* output buffer too small, can't write valid bitstream */
2033 err = AACENC_ENCODE_ERROR;
2034 goto bail;
2035 }
2036 }
2037
2038 bail:
2039 if (err == AACENC_ENCODE_ERROR) {
2040 /* All encoder modules have to be initialized */
2041 hAacEncoder->InitFlags = AACENC_INIT_ALL;
2042 }
2043
2044 return err;
2045 }
2046
aacEncGetConf(HANDLE_AACENCODER hAacEncoder,UINT * size,UCHAR * confBuffer)2047 static AAC_ENCODER_ERROR aacEncGetConf(HANDLE_AACENCODER hAacEncoder,
2048 UINT *size, UCHAR *confBuffer) {
2049 FDK_BITSTREAM tmpConf;
2050 UINT confType;
2051 UCHAR buf[64];
2052 int err;
2053
2054 /* Init bit buffer */
2055 FDKinitBitStream(&tmpConf, buf, 64, 0, BS_WRITER);
2056
2057 /* write conf in tmp buffer */
2058 err = transportEnc_GetConf(hAacEncoder->hTpEnc, &hAacEncoder->coderConfig,
2059 &tmpConf, &confType);
2060
2061 /* copy data to outbuffer: length in bytes */
2062 FDKbyteAlign(&tmpConf, 0);
2063
2064 /* Check buffer size */
2065 if (FDKgetValidBits(&tmpConf) > ((*size) << 3)) return AAC_ENC_UNKNOWN;
2066
2067 FDKfetchBuffer(&tmpConf, confBuffer, size);
2068
2069 if (err != 0)
2070 return AAC_ENC_UNKNOWN;
2071 else
2072 return AAC_ENC_OK;
2073 }
2074
aacEncGetLibInfo(LIB_INFO * info)2075 AACENC_ERROR aacEncGetLibInfo(LIB_INFO *info) {
2076 int i = 0;
2077
2078 if (info == NULL) {
2079 return AACENC_INVALID_HANDLE;
2080 }
2081
2082 FDK_toolsGetLibInfo(info);
2083 transportEnc_GetLibInfo(info);
2084 sbrEncoder_GetLibInfo(info);
2085 FDK_MpegsEnc_GetLibInfo(info);
2086
2087 /* search for next free tab */
2088 for (i = 0; i < FDK_MODULE_LAST; i++) {
2089 if (info[i].module_id == FDK_NONE) break;
2090 }
2091 if (i == FDK_MODULE_LAST) {
2092 return AACENC_INIT_ERROR;
2093 }
2094
2095 info[i].module_id = FDK_AACENC;
2096 info[i].build_date = AACENCODER_LIB_BUILD_DATE;
2097 info[i].build_time = AACENCODER_LIB_BUILD_TIME;
2098 info[i].title = AACENCODER_LIB_TITLE;
2099 info[i].version =
2100 LIB_VERSION(AACENCODER_LIB_VL0, AACENCODER_LIB_VL1, AACENCODER_LIB_VL2);
2101 ;
2102 LIB_VERSION_STRING(&info[i]);
2103
2104 /* Capability flags */
2105 info[i].flags = 0 | CAPF_AAC_1024 | CAPF_AAC_LC | CAPF_AAC_512 |
2106 CAPF_AAC_480 | CAPF_AAC_DRC | CAPF_AAC_ELD_DOWNSCALE;
2107 /* End of flags */
2108
2109 return AACENC_OK;
2110 }
2111
aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder,const AACENC_PARAM param,const UINT value)2112 AACENC_ERROR aacEncoder_SetParam(const HANDLE_AACENCODER hAacEncoder,
2113 const AACENC_PARAM param, const UINT value) {
2114 AACENC_ERROR err = AACENC_OK;
2115 USER_PARAM *settings = &hAacEncoder->extParam;
2116
2117 /* check encoder handle */
2118 if (hAacEncoder == NULL) {
2119 err = AACENC_INVALID_HANDLE;
2120 goto bail;
2121 }
2122
2123 /* apply param value */
2124 switch (param) {
2125 case AACENC_AOT:
2126 if (settings->userAOT != (AUDIO_OBJECT_TYPE)value) {
2127 /* check if AOT matches the allocated modules */
2128 switch (value) {
2129 case AOT_PS:
2130 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_PS))) {
2131 err = AACENC_INVALID_CONFIG;
2132 goto bail;
2133 }
2134 FDK_FALLTHROUGH;
2135 case AOT_SBR:
2136 case AOT_MP2_SBR:
2137 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_SBR))) {
2138 err = AACENC_INVALID_CONFIG;
2139 goto bail;
2140 }
2141 FDK_FALLTHROUGH;
2142 case AOT_AAC_LC:
2143 case AOT_MP2_AAC_LC:
2144 case AOT_ER_AAC_LD:
2145 case AOT_ER_AAC_ELD:
2146 if (!(hAacEncoder->encoder_modis & (ENC_MODE_FLAG_AAC))) {
2147 err = AACENC_INVALID_CONFIG;
2148 goto bail;
2149 }
2150 break;
2151 default:
2152 err = AACENC_INVALID_CONFIG;
2153 goto bail;
2154 } /* switch value */
2155 settings->userAOT = (AUDIO_OBJECT_TYPE)value;
2156 hAacEncoder->InitFlags |=
2157 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2158 }
2159 break;
2160 case AACENC_BITRATE:
2161 if (settings->userBitrate != value) {
2162 settings->userBitrate = value;
2163 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2164 }
2165 break;
2166 case AACENC_BITRATEMODE:
2167 if (settings->userBitrateMode != value) {
2168 switch (value) {
2169 case 0:
2170 case 1:
2171 case 2:
2172 case 3:
2173 case 4:
2174 case 5:
2175 settings->userBitrateMode = value;
2176 hAacEncoder->InitFlags |=
2177 AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2178 break;
2179 default:
2180 err = AACENC_INVALID_CONFIG;
2181 break;
2182 } /* switch value */
2183 }
2184 break;
2185 case AACENC_SAMPLERATE:
2186 if (settings->userSamplerate != value) {
2187 if (!((value == 8000) || (value == 11025) || (value == 12000) ||
2188 (value == 16000) || (value == 22050) || (value == 24000) ||
2189 (value == 32000) || (value == 44100) || (value == 48000) ||
2190 (value == 64000) || (value == 88200) || (value == 96000))) {
2191 err = AACENC_INVALID_CONFIG;
2192 break;
2193 }
2194 settings->userSamplerate = value;
2195 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2196 hAacEncoder->InitFlags |=
2197 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2198 }
2199 break;
2200 case AACENC_CHANNELMODE:
2201 if (settings->userChannelMode != (CHANNEL_MODE)value) {
2202 if (((CHANNEL_MODE)value == MODE_212) &&
2203 (NULL != hAacEncoder->hMpsEnc)) {
2204 settings->userChannelMode = (CHANNEL_MODE)value;
2205 settings->nChannels = 2;
2206 } else {
2207 const CHANNEL_MODE_CONFIG_TAB *pConfig =
2208 FDKaacEnc_GetChannelModeConfiguration((CHANNEL_MODE)value);
2209 if (pConfig == NULL) {
2210 err = AACENC_INVALID_CONFIG;
2211 break;
2212 }
2213 if ((pConfig->nElements > hAacEncoder->nMaxAacElements) ||
2214 (pConfig->nChannelsEff > hAacEncoder->nMaxAacChannels)) {
2215 err = AACENC_INVALID_CONFIG;
2216 break;
2217 }
2218
2219 settings->userChannelMode = (CHANNEL_MODE)value;
2220 settings->nChannels = pConfig->nChannels;
2221 }
2222 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2223 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2224 if (!((value >= 1) && (value <= 6))) {
2225 hAacEncoder->InitFlags |= AACENC_INIT_STATES;
2226 }
2227 }
2228 break;
2229 case AACENC_BANDWIDTH:
2230 if (settings->userBandwidth != value) {
2231 settings->userBandwidth = value;
2232 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2233 }
2234 break;
2235 case AACENC_CHANNELORDER:
2236 if (hAacEncoder->aacConfig.channelOrder != (CHANNEL_ORDER)value) {
2237 if (!((value == 0) || (value == 1))) {
2238 err = AACENC_INVALID_CONFIG;
2239 break;
2240 }
2241 hAacEncoder->aacConfig.channelOrder = (CHANNEL_ORDER)value;
2242 hAacEncoder->nSamplesRead = 0; /* reset internal inputbuffer */
2243 hAacEncoder->InitFlags |=
2244 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2245 }
2246 break;
2247 case AACENC_AFTERBURNER:
2248 if (settings->userAfterburner != value) {
2249 if (!((value == 0) || (value == 1))) {
2250 err = AACENC_INVALID_CONFIG;
2251 break;
2252 }
2253 settings->userAfterburner = value;
2254 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2255 }
2256 break;
2257 case AACENC_GRANULE_LENGTH:
2258 if (settings->userFramelength != value) {
2259 switch (value) {
2260 case 1024:
2261 case 512:
2262 case 480:
2263 case 256:
2264 case 240:
2265 case 128:
2266 case 120:
2267 if ((value << 1) == 480 || (value << 1) == 512) {
2268 settings->userDownscaleFactor = 2;
2269 } else if ((value << 2) == 480 || (value << 2) == 512) {
2270 settings->userDownscaleFactor = 4;
2271 }
2272 settings->userFramelength = value;
2273 hAacEncoder->InitFlags |=
2274 AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2275 break;
2276 default:
2277 err = AACENC_INVALID_CONFIG;
2278 break;
2279 }
2280 }
2281 break;
2282 case AACENC_SBR_RATIO:
2283 if (settings->userSbrRatio != value) {
2284 if (!((value == 0) || (value == 1) || (value == 2))) {
2285 err = AACENC_INVALID_CONFIG;
2286 break;
2287 }
2288 settings->userSbrRatio = value;
2289 hAacEncoder->InitFlags |=
2290 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2291 }
2292 break;
2293 case AACENC_SBR_MODE:
2294 if ((settings->userSbrEnabled != value) &&
2295 (NULL != hAacEncoder->hEnvEnc)) {
2296 settings->userSbrEnabled = value;
2297 hAacEncoder->InitFlags |=
2298 AACENC_INIT_CONFIG | AACENC_INIT_STATES | AACENC_INIT_TRANSPORT;
2299 }
2300 break;
2301 case AACENC_TRANSMUX:
2302 if (settings->userTpType != (TRANSPORT_TYPE)value) {
2303 TRANSPORT_TYPE type = (TRANSPORT_TYPE)value;
2304 UINT flags = hAacEncoder->CAPF_tpEnc;
2305
2306 if (!(((type == TT_MP4_ADIF) && (flags & CAPF_ADIF)) ||
2307 ((type == TT_MP4_ADTS) && (flags & CAPF_ADTS)) ||
2308 ((type == TT_MP4_LATM_MCP0) &&
2309 ((flags & CAPF_LATM) && (flags & CAPF_RAWPACKETS))) ||
2310 ((type == TT_MP4_LATM_MCP1) &&
2311 ((flags & CAPF_LATM) && (flags & CAPF_RAWPACKETS))) ||
2312 ((type == TT_MP4_LOAS) && (flags & CAPF_LOAS)) ||
2313 ((type == TT_MP4_RAW) && (flags & CAPF_RAWPACKETS)))) {
2314 err = AACENC_INVALID_CONFIG;
2315 break;
2316 }
2317 settings->userTpType = (TRANSPORT_TYPE)value;
2318 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2319 }
2320 break;
2321 case AACENC_SIGNALING_MODE:
2322 if (settings->userTpSignaling != value) {
2323 if (!((value == 0) || (value == 1) || (value == 2))) {
2324 err = AACENC_INVALID_CONFIG;
2325 break;
2326 }
2327 settings->userTpSignaling = value;
2328 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2329 }
2330 break;
2331 case AACENC_PROTECTION:
2332 if (settings->userTpProtection != value) {
2333 if (!((value == 0) || (value == 1))) {
2334 err = AACENC_INVALID_CONFIG;
2335 break;
2336 }
2337 settings->userTpProtection = value;
2338 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2339 }
2340 break;
2341 case AACENC_HEADER_PERIOD:
2342 if (settings->userTpHeaderPeriod != value) {
2343 if (!(((INT)value >= 0) && (value <= 255))) {
2344 err = AACENC_INVALID_CONFIG;
2345 break;
2346 }
2347 settings->userTpHeaderPeriod = value;
2348 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2349 }
2350 break;
2351 case AACENC_AUDIOMUXVER:
2352 if (settings->userTpAmxv != value) {
2353 if (!((value == 0) || (value == 1) || (value == 2))) {
2354 err = AACENC_INVALID_CONFIG;
2355 break;
2356 }
2357 settings->userTpAmxv = value;
2358 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2359 }
2360 break;
2361 case AACENC_TPSUBFRAMES:
2362 if (settings->userTpNsubFrames != value) {
2363 if (!((value >= 1) && (value <= 4))) {
2364 err = AACENC_INVALID_CONFIG;
2365 break;
2366 }
2367 settings->userTpNsubFrames = value;
2368 hAacEncoder->InitFlags |= AACENC_INIT_TRANSPORT;
2369 }
2370 break;
2371 case AACENC_ANCILLARY_BITRATE:
2372 if (settings->userAncDataRate != value) {
2373 settings->userAncDataRate = value;
2374 }
2375 break;
2376 case AACENC_CONTROL_STATE:
2377 if (hAacEncoder->InitFlags != value) {
2378 if (value & AACENC_RESET_INBUFFER) {
2379 hAacEncoder->nSamplesRead = 0;
2380 }
2381 hAacEncoder->InitFlags = value;
2382 }
2383 break;
2384 case AACENC_METADATA_MODE:
2385 if ((UINT)settings->userMetaDataMode != value) {
2386 if (!(((INT)value >= 0) && ((INT)value <= 3))) {
2387 err = AACENC_INVALID_CONFIG;
2388 break;
2389 }
2390 settings->userMetaDataMode = value;
2391 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG;
2392 }
2393 break;
2394 case AACENC_PEAK_BITRATE:
2395 if (settings->userPeakBitrate != value) {
2396 settings->userPeakBitrate = value;
2397 hAacEncoder->InitFlags |= AACENC_INIT_CONFIG | AACENC_INIT_TRANSPORT;
2398 }
2399 break;
2400 default:
2401 err = AACENC_UNSUPPORTED_PARAMETER;
2402 break;
2403 } /* switch(param) */
2404
2405 bail:
2406 return err;
2407 }
2408
aacEncoder_GetParam(const HANDLE_AACENCODER hAacEncoder,const AACENC_PARAM param)2409 UINT aacEncoder_GetParam(const HANDLE_AACENCODER hAacEncoder,
2410 const AACENC_PARAM param) {
2411 UINT value = 0;
2412 USER_PARAM *settings = &hAacEncoder->extParam;
2413
2414 /* check encoder handle */
2415 if (hAacEncoder == NULL) {
2416 goto bail;
2417 }
2418
2419 /* apply param value */
2420 switch (param) {
2421 case AACENC_AOT:
2422 value = (UINT)hAacEncoder->aacConfig.audioObjectType;
2423 break;
2424 case AACENC_BITRATE:
2425 switch (hAacEncoder->aacConfig.bitrateMode) {
2426 case AACENC_BR_MODE_CBR:
2427 value = (UINT)hAacEncoder->aacConfig.bitRate;
2428 break;
2429 default:
2430 value = (UINT)-1;
2431 }
2432 break;
2433 case AACENC_BITRATEMODE:
2434 value = (UINT)((hAacEncoder->aacConfig.bitrateMode != AACENC_BR_MODE_FF)
2435 ? hAacEncoder->aacConfig.bitrateMode
2436 : AACENC_BR_MODE_CBR);
2437 break;
2438 case AACENC_SAMPLERATE:
2439 value = (UINT)hAacEncoder->coderConfig.extSamplingRate;
2440 break;
2441 case AACENC_CHANNELMODE:
2442 if ((MODE_1 == hAacEncoder->aacConfig.channelMode) &&
2443 (hAacEncoder->aacConfig.syntaxFlags & AC_LD_MPS)) {
2444 value = MODE_212;
2445 } else {
2446 value = (UINT)hAacEncoder->aacConfig.channelMode;
2447 }
2448 break;
2449 case AACENC_BANDWIDTH:
2450 value = (UINT)hAacEncoder->aacConfig.bandWidth;
2451 break;
2452 case AACENC_CHANNELORDER:
2453 value = (UINT)hAacEncoder->aacConfig.channelOrder;
2454 break;
2455 case AACENC_AFTERBURNER:
2456 value = (UINT)hAacEncoder->aacConfig.useRequant;
2457 break;
2458 case AACENC_GRANULE_LENGTH:
2459 value = (UINT)hAacEncoder->aacConfig.framelength;
2460 break;
2461 case AACENC_SBR_RATIO:
2462 value = isSbrActive(&hAacEncoder->aacConfig)
2463 ? hAacEncoder->aacConfig.sbrRatio
2464 : 0;
2465 break;
2466 case AACENC_SBR_MODE:
2467 value =
2468 (UINT)(hAacEncoder->aacConfig.syntaxFlags & AC_SBR_PRESENT) ? 1 : 0;
2469 break;
2470 case AACENC_TRANSMUX:
2471 value = (UINT)settings->userTpType;
2472 break;
2473 case AACENC_SIGNALING_MODE:
2474 value = (UINT)getSbrSignalingMode(
2475 hAacEncoder->aacConfig.audioObjectType, settings->userTpType,
2476 settings->userTpSignaling, hAacEncoder->aacConfig.sbrRatio);
2477 break;
2478 case AACENC_PROTECTION:
2479 value = (UINT)settings->userTpProtection;
2480 break;
2481 case AACENC_HEADER_PERIOD:
2482 value = (UINT)hAacEncoder->coderConfig.headerPeriod;
2483 break;
2484 case AACENC_AUDIOMUXVER:
2485 value = (UINT)hAacEncoder->aacConfig.audioMuxVersion;
2486 break;
2487 case AACENC_TPSUBFRAMES:
2488 value = (UINT)settings->userTpNsubFrames;
2489 break;
2490 case AACENC_ANCILLARY_BITRATE:
2491 value = (UINT)hAacEncoder->aacConfig.anc_Rate;
2492 break;
2493 case AACENC_CONTROL_STATE:
2494 value = (UINT)hAacEncoder->InitFlags;
2495 break;
2496 case AACENC_METADATA_MODE:
2497 value = (hAacEncoder->metaDataAllowed == 0)
2498 ? 0
2499 : (UINT)settings->userMetaDataMode;
2500 break;
2501 case AACENC_PEAK_BITRATE:
2502 value = (UINT)-1; /* peak bitrate parameter is meaningless */
2503 if (((INT)hAacEncoder->extParam.userPeakBitrate != -1)) {
2504 value =
2505 (UINT)(fMax((INT)hAacEncoder->extParam.userPeakBitrate,
2506 hAacEncoder->aacConfig
2507 .bitRate)); /* peak bitrate parameter is in use */
2508 }
2509 break;
2510
2511 default:
2512 // err = MPS_INVALID_PARAMETER;
2513 break;
2514 } /* switch(param) */
2515
2516 bail:
2517 return value;
2518 }
2519
aacEncInfo(const HANDLE_AACENCODER hAacEncoder,AACENC_InfoStruct * pInfo)2520 AACENC_ERROR aacEncInfo(const HANDLE_AACENCODER hAacEncoder,
2521 AACENC_InfoStruct *pInfo) {
2522 AACENC_ERROR err = AACENC_OK;
2523
2524 if ((hAacEncoder == NULL) || (pInfo == NULL)) {
2525 err = AACENC_INVALID_HANDLE;
2526 goto bail;
2527 }
2528
2529 FDKmemclear(pInfo, sizeof(AACENC_InfoStruct));
2530 pInfo->confSize = 64; /* pre-initialize */
2531
2532 pInfo->maxOutBufBytes = ((hAacEncoder->nMaxAacChannels * 6144) + 7) >> 3;
2533 pInfo->maxAncBytes = hAacEncoder->aacConfig.maxAncBytesPerAU;
2534 pInfo->inBufFillLevel =
2535 hAacEncoder->nSamplesRead / hAacEncoder->extParam.nChannels;
2536 pInfo->inputChannels = hAacEncoder->extParam.nChannels;
2537 pInfo->frameLength =
2538 hAacEncoder->nSamplesToRead / hAacEncoder->extParam.nChannels;
2539 pInfo->nDelay = hAacEncoder->nDelay;
2540 pInfo->nDelayCore = hAacEncoder->nDelayCore;
2541
2542 /* Get encoder configuration */
2543 if (aacEncGetConf(hAacEncoder, &pInfo->confSize, &pInfo->confBuf[0]) !=
2544 AAC_ENC_OK) {
2545 err = AACENC_INIT_ERROR;
2546 goto bail;
2547 }
2548 bail:
2549 return err;
2550 }
2551