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