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