• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* -----------------------------------------------------------------------------------------------------------
3 Software License for The Fraunhofer FDK AAC Codec Library for Android
4 
5 � Copyright  1995 - 2013 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 /***************************  Fraunhofer IIS FDK Tools  ***********************
85 
86    Author(s): Andreas Ehret, Tobias Chalupka
87    Description: SBR encoder top level processing.
88 
89 ******************************************************************************/
90 
91 #include "sbr_encoder.h"
92 
93 #include "sbr_ram.h"
94 #include "sbr_rom.h"
95 #include "sbrenc_freq_sca.h"
96 #include "env_bit.h"
97 #include "cmondata.h"
98 #include "sbr_misc.h"
99 #include "sbr.h"
100 #include "qmf.h"
101 
102 #include "ps_main.h"
103 
104 #define SBRENCODER_LIB_VL0 3
105 #define SBRENCODER_LIB_VL1 3
106 #define SBRENCODER_LIB_VL2 4
107 
108 
109 
110 /***************************************************************************/
111 /*
112  * SBR Delay balancing definitions.
113  */
114 
115 /*
116       input buffer (1ch)
117 
118       |------------ 1537   -------------|-----|---------- 2048 -------------|
119            (core2sbr delay     )          ds     (read, core and ds area)
120 */
121 
122 #define SFB(dwnsmp)        (32 << (dwnsmp-1))     /* SBR Frequency bands: 64 for dual-rate, 32 for single-rate */
123 #define STS(fl)            (((fl)==1024)?32:30)   /* SBR Time Slots: 32 for core frame length 1024, 30 for core frame length 960 */
124 
125 #define DELAY_QMF_ANA(dwnsmp) ((320<<((dwnsmp)-1)) - (32<<((dwnsmp)-1))) /* Full bandwidth */
126 #define DELAY_HYB_ANA         (10*64)       /* + 0.5 */              /*  */
127 #define DELAY_HYB_SYN         (6*64 - 32)                            /*  */
128 #define DELAY_QMF_POSTPROC(dwnsmp) (32*(dwnsmp))                     /* QMF postprocessing delay */
129 #define DELAY_DEC_QMF(dwnsmp) (6 * SFB(dwnsmp) )                     /* Decoder QMF overlap */
130 #define DELAY_QMF_SYN         (2)                                    /* NO_POLY/2=2.5, rounded down to 2 */
131 #define DELAY_QMF_DS          (32)                                   /* QMF synthesis for downsampled time signal */
132 
133 /* Delay in QMF paths */
134 #define DELAY_SBR(fl,dwnsmp)     (DELAY_QMF_ANA(dwnsmp) + (SFB(dwnsmp)*STS(fl) - 1) + DELAY_QMF_SYN)
135 #define DELAY_PS(fl,dwnsmp)      (DELAY_QMF_ANA(dwnsmp) + DELAY_HYB_ANA + DELAY_DEC_QMF(dwnsmp) + (SFB(dwnsmp)*STS(fl)-1) + DELAY_HYB_SYN + DELAY_QMF_SYN)
136 #define DELAY_ELDSBR(fl,dwnsmp)  ( ( ((fl)/2)*(dwnsmp) ) - 1 + DELAY_QMF_POSTPROC(dwnsmp) )
137 
138 /* Delay differences for SBR and SBR+PS */
139 #define MAX_DS_FILTER_DELAY (5)                                           /* the additional max downsampler filter delay (source fs) */
140 #define DELAY_AAC2SBR(fl,dwnsmp) ((DELAY_QMF_ANA(dwnsmp) + DELAY_DEC_QMF(dwnsmp) + DELAY_QMF_SYN) - DELAY_SBR((fl),(dwnsmp)))
141 #define DELAY_ELD2SBR(fl,dwnsmp) ((DELAY_QMF_POSTPROC(dwnsmp)) - DELAY_ELDSBR(fl,dwnsmp))
142 #define DELAY_AAC2PS(fl,dwnsmp) ((DELAY_QMF_ANA(dwnsmp) + DELAY_QMF_DS + /*(DELAY_AAC(fl)*2) + */ DELAY_QMF_ANA(dwnsmp) + DELAY_DEC_QMF(dwnsmp) + DELAY_HYB_SYN + DELAY_QMF_SYN) - DELAY_PS(fl,dwnsmp)) /* 2048 - 463*2 */
143 
144 /* Assumption: The sample delay resulting of of DELAY_AAC2PS is always smaller than the sample delay implied by DELAY_AAC2SBR */
145 #define MAX_SAMPLE_DELAY       (DELAY_AAC2SBR(1024,2) + MAX_DS_FILTER_DELAY) /* maximum delay: frame length of 1024 and dual-rate sbr */
146 
147 /***************************************************************************/
148 
149 
150 
151 #define INVALID_TABLE_IDX -1
152 
153 /***************************************************************************/
154 /*!
155 
156   \brief  Selects the SBR tuning settings to use dependent on number of
157           channels, bitrate, sample rate and core coder
158 
159   \return Index to the appropriate table
160 
161 ****************************************************************************/
162 #define DISTANCE_CEIL_VALUE 5000000
163 static INT
getSbrTuningTableIndex(UINT bitrate,UINT numChannels,UINT sampleRate,AUDIO_OBJECT_TYPE core,UINT * pBitRateClosest)164 getSbrTuningTableIndex(UINT bitrate,    /*! the total bitrate in bits/sec */
165                        UINT numChannels,/*! the number of channels for the core coder */
166                        UINT sampleRate,  /*! the sampling rate of the core coder */
167                        AUDIO_OBJECT_TYPE core,
168                        UINT *pBitRateClosest
169                        )
170 {
171   int i, bitRateClosestLowerIndex=-1, bitRateClosestUpperIndex=-1, found = 0;
172   UINT bitRateClosestUpper = 0, bitRateClosestLower=DISTANCE_CEIL_VALUE;
173   int isforThisCodec=0;
174 
175   #define isForThisCore(i) \
176     ( ( sbrTuningTable[i].coreCoder == CODEC_AACLD && core == AOT_ER_AAC_ELD ) || \
177       ( sbrTuningTable[i].coreCoder == CODEC_AAC   && core != AOT_ER_AAC_ELD ) )
178 
179   for (i=0; i < sbrTuningTableSize ; i++) {
180     if ( isForThisCore(i) ) /* tuning table is for this core codec */
181     {
182       if ( numChannels == sbrTuningTable [i].numChannels
183         && sampleRate == sbrTuningTable [i].sampleRate )
184       {
185         found = 1;
186         if ((bitrate >= sbrTuningTable [i].bitrateFrom) &&
187             (bitrate < sbrTuningTable [i].bitrateTo)) {
188               bitRateClosestLower = bitrate;
189               bitRateClosestUpper = bitrate;
190               //FDKprintf("entry %d\n", i);
191           return i ;
192         } else {
193           if ( sbrTuningTable [i].bitrateFrom > bitrate ) {
194             if (sbrTuningTable [i].bitrateFrom < bitRateClosestLower) {
195               bitRateClosestLower = sbrTuningTable [i].bitrateFrom;
196               bitRateClosestLowerIndex = i;
197             }
198           }
199           if ( sbrTuningTable [i].bitrateTo <= bitrate ) {
200             if (sbrTuningTable [i].bitrateTo > bitRateClosestUpper) {
201               bitRateClosestUpper = sbrTuningTable [i].bitrateTo-1;
202               bitRateClosestUpperIndex = i;
203             }
204           }
205         }
206       }
207     }
208   }
209 
210   if (pBitRateClosest != NULL)
211   {
212     /* If there was at least one matching tuning entry found then pick the least distance bit rate */
213     if (found)
214     {
215       int distanceUpper=DISTANCE_CEIL_VALUE, distanceLower=DISTANCE_CEIL_VALUE;
216       if (bitRateClosestLowerIndex >= 0) {
217         distanceLower = sbrTuningTable [bitRateClosestLowerIndex].bitrateFrom - bitrate;
218       }
219       if (bitRateClosestUpperIndex >= 0) {
220         distanceUpper = bitrate - sbrTuningTable [bitRateClosestUpperIndex].bitrateTo;
221       }
222       if ( distanceUpper < distanceLower )
223       {
224         *pBitRateClosest = bitRateClosestUpper;
225       } else {
226         *pBitRateClosest = bitRateClosestLower;
227       }
228     } else {
229       *pBitRateClosest = 0;
230     }
231   }
232 
233   return INVALID_TABLE_IDX;
234 }
235 
236 /***************************************************************************/
237 /*!
238 
239   \brief  Selects the PS tuning settings to use dependent on bitrate
240   and core coder
241 
242   \return Index to the appropriate table
243 
244 ****************************************************************************/
245 static INT
getPsTuningTableIndex(UINT bitrate,UINT * pBitRateClosest)246 getPsTuningTableIndex(UINT bitrate, UINT *pBitRateClosest){
247 
248   INT i, paramSets = sizeof (psTuningTable) / sizeof (psTuningTable [0]);
249   int bitRateClosestLowerIndex=-1, bitRateClosestUpperIndex=-1;
250   UINT bitRateClosestUpper = 0, bitRateClosestLower=DISTANCE_CEIL_VALUE;
251 
252   for (i = 0 ; i < paramSets ; i++)  {
253     if ((bitrate >= psTuningTable [i].bitrateFrom) &&
254         (bitrate < psTuningTable [i].bitrateTo)) {
255       return i ;
256     } else {
257       if ( psTuningTable [i].bitrateFrom > bitrate ) {
258         if (psTuningTable [i].bitrateFrom < bitRateClosestLower) {
259           bitRateClosestLower = psTuningTable [i].bitrateFrom;
260           bitRateClosestLowerIndex = i;
261         }
262       }
263       if ( psTuningTable [i].bitrateTo <= bitrate ) {
264         if (psTuningTable [i].bitrateTo > bitRateClosestUpper) {
265           bitRateClosestUpper = psTuningTable [i].bitrateTo-1;
266           bitRateClosestUpperIndex = i;
267         }
268       }
269     }
270   }
271 
272   if (pBitRateClosest != NULL)
273   {
274     int distanceUpper=DISTANCE_CEIL_VALUE, distanceLower=DISTANCE_CEIL_VALUE;
275     if (bitRateClosestLowerIndex >= 0) {
276       distanceLower = sbrTuningTable [bitRateClosestLowerIndex].bitrateFrom - bitrate;
277     }
278     if (bitRateClosestUpperIndex >= 0) {
279       distanceUpper = bitrate - sbrTuningTable [bitRateClosestUpperIndex].bitrateTo;
280     }
281     if ( distanceUpper < distanceLower )
282     {
283       *pBitRateClosest = bitRateClosestUpper;
284     } else {
285       *pBitRateClosest = bitRateClosestLower;
286     }
287   }
288 
289   return INVALID_TABLE_IDX;
290 }
291 
292 /***************************************************************************/
293 /*!
294 
295   \brief  In case of downsampled SBR we may need to lower the stop freq
296           of a tuning setting to fit into the lower half of the
297           spectrum ( which is sampleRate/4 )
298 
299   \return the adapted stop frequency index (-1 -> error)
300 
301   \ingroup SbrEncCfg
302 
303 ****************************************************************************/
304 static INT
FDKsbrEnc_GetDownsampledStopFreq(const INT sampleRateCore,const INT startFreq,INT stopFreq,const INT downSampleFactor)305 FDKsbrEnc_GetDownsampledStopFreq (
306         const INT sampleRateCore,
307         const INT startFreq,
308               INT stopFreq,
309         const INT downSampleFactor
310         )
311 {
312   INT maxStopFreqRaw = sampleRateCore / 2;
313   INT startBand, stopBand;
314   HANDLE_ERROR_INFO err;
315 
316   while (stopFreq > 0 && FDKsbrEnc_getSbrStopFreqRAW(stopFreq, sampleRateCore) > maxStopFreqRaw) {
317     stopFreq--;
318   }
319 
320   if (FDKsbrEnc_getSbrStopFreqRAW( stopFreq, sampleRateCore) > maxStopFreqRaw)
321     return -1;
322 
323   err = FDKsbrEnc_FindStartAndStopBand (
324                 sampleRateCore<<(downSampleFactor-1),
325                 sampleRateCore,
326                 32<<(downSampleFactor-1),
327                 startFreq,
328                 stopFreq,
329                &startBand,
330                &stopBand
331                 );
332   if (err)
333     return -1;
334 
335   return stopFreq;
336 }
337 
338 
339 /***************************************************************************/
340 /*!
341 
342   \brief  tells us, if for the given coreCoder, bitrate, number of channels
343           and input sampling rate an SBR setting is available. If yes, it
344           tells us also the core sampling rate we would need to run with
345 
346   \return a flag indicating success: yes (1) or no (0)
347 
348 ****************************************************************************/
349 static UINT
FDKsbrEnc_IsSbrSettingAvail(UINT bitrate,UINT vbrMode,UINT numOutputChannels,UINT sampleRateInput,UINT sampleRateCore,AUDIO_OBJECT_TYPE core)350 FDKsbrEnc_IsSbrSettingAvail (
351         UINT bitrate,           /*! the total bitrate in bits/sec */
352         UINT vbrMode,           /*! the vbr paramter, 0 means constant bitrate */
353         UINT numOutputChannels, /*! the number of channels for the core coder */
354         UINT sampleRateInput,   /*! the input sample rate [in Hz] */
355         UINT sampleRateCore,    /*! the core's sampling rate */
356         AUDIO_OBJECT_TYPE core
357         )
358 {
359   INT idx = INVALID_TABLE_IDX;
360 
361   if (sampleRateInput < 16000)
362     return 0;
363 
364   if (bitrate==0) {
365     /* map vbr quality to bitrate */
366     if (vbrMode < 30)
367       bitrate = 24000;
368     else if (vbrMode < 40)
369       bitrate = 28000;
370     else if (vbrMode < 60)
371       bitrate = 32000;
372     else if (vbrMode < 75)
373       bitrate = 40000;
374     else
375       bitrate = 48000;
376     bitrate *= numOutputChannels;
377   }
378 
379   idx = getSbrTuningTableIndex(bitrate, numOutputChannels, sampleRateCore, core, NULL);
380 
381   return (idx == INVALID_TABLE_IDX ? 0 : 1);
382 }
383 
384 
385 /***************************************************************************/
386 /*!
387 
388   \brief  Adjusts the SBR settings according to the chosen core coder
389           settings which are accessible via config->codecSettings
390 
391   \return A flag indicating success: yes (1) or no (0)
392 
393 ****************************************************************************/
394 static UINT
FDKsbrEnc_AdjustSbrSettings(const sbrConfigurationPtr config,UINT bitRate,UINT numChannels,UINT sampleRateCore,UINT sampleRateSbr,UINT transFac,UINT standardBitrate,UINT vbrMode,UINT useSpeechConfig,UINT lcsMode,UINT bParametricStereo,AUDIO_OBJECT_TYPE core)395 FDKsbrEnc_AdjustSbrSettings (const sbrConfigurationPtr config, /*! output, modified */
396                    UINT bitRate,             /*! the total bitrate in bits/sec */
397                    UINT numChannels,         /*! the core coder number of channels */
398                    UINT sampleRateCore,      /*! the core coder sampling rate in Hz */
399                    UINT sampleRateSbr,       /*! the sbr coder sampling rate in Hz */
400                    UINT transFac,            /*! the short block to long block ratio */
401                    UINT standardBitrate,     /*! the standard bitrate per channel in bits/sec */
402                    UINT vbrMode,             /*! the vbr paramter, 0 poor quality .. 100 high quality*/
403                    UINT useSpeechConfig,     /*!< adapt tuning parameters for speech ? */
404                    UINT lcsMode,             /*! the low complexity stereo mode */
405                    UINT bParametricStereo,   /*!< use parametric stereo */
406                    AUDIO_OBJECT_TYPE core)   /* Core audio codec object type */
407 {
408   INT idx = INVALID_TABLE_IDX;
409   /* set the core codec settings */
410   config->codecSettings.bitRate         = bitRate;
411   config->codecSettings.nChannels       = numChannels;
412   config->codecSettings.sampleFreq      = sampleRateCore;
413   config->codecSettings.transFac        = transFac;
414   config->codecSettings.standardBitrate = standardBitrate;
415 
416   if (bitRate==0) {
417     /* map vbr quality to bitrate */
418     if (vbrMode < 30)
419       bitRate = 24000;
420     else if (vbrMode < 40)
421       bitRate = 28000;
422     else if (vbrMode < 60)
423       bitRate = 32000;
424     else if (vbrMode < 75)
425       bitRate = 40000;
426     else
427       bitRate = 48000;
428     bitRate *= numChannels;
429     /* fix to enable mono vbrMode<40 @ 44.1 of 48kHz */
430     if (numChannels==1) {
431       if (sampleRateSbr==44100 || sampleRateSbr==48000) {
432         if (vbrMode<40) bitRate = 32000;
433       }
434     }
435   }
436 
437   idx = getSbrTuningTableIndex(bitRate,numChannels,sampleRateCore, core, NULL);
438 
439   if (idx != INVALID_TABLE_IDX) {
440     config->startFreq       = sbrTuningTable[idx].startFreq ;
441     config->stopFreq        = sbrTuningTable[idx].stopFreq ;
442     if (useSpeechConfig) {
443       config->startFreq     = sbrTuningTable[idx].startFreqSpeech;
444       config->stopFreq      = sbrTuningTable[idx].stopFreqSpeech;
445     }
446 
447     /* Adapt stop frequency in case of downsampled SBR - only 32 bands then */
448     if (1 == config->downSampleFactor) {
449       INT dsStopFreq = FDKsbrEnc_GetDownsampledStopFreq(
450                                sampleRateCore,
451                                config->startFreq,
452                                config->stopFreq,
453                                config->downSampleFactor
454                                );
455       if (dsStopFreq < 0) {
456         return 0;
457       }
458 
459       config->stopFreq = dsStopFreq;
460     }
461 
462     config->sbr_noise_bands = sbrTuningTable[idx].numNoiseBands ;
463     if (core == AOT_ER_AAC_ELD)
464       config->init_amp_res_FF = SBR_AMP_RES_1_5;
465     config->noiseFloorOffset= sbrTuningTable[idx].noiseFloorOffset;
466 
467     config->ana_max_level   = sbrTuningTable[idx].noiseMaxLevel ;
468     config->stereoMode      = sbrTuningTable[idx].stereoMode ;
469     config->freqScale       = sbrTuningTable[idx].freqScale ;
470 
471     /* adjust usage of parametric coding dependent on bitrate and speech config flag */
472     if (useSpeechConfig)
473       config->parametricCoding  = 0;
474 
475     if (core == AOT_ER_AAC_ELD) {
476       if (bitRate < 28000)
477         config->init_amp_res_FF = SBR_AMP_RES_3_0;
478       config->SendHeaderDataTime = -1;
479     }
480 
481     if (numChannels == 1) {
482       if (bitRate < 16000) {
483         config->parametricCoding  = 0;
484       }
485     }
486     else {
487       if (bitRate < 20000) {
488         config->parametricCoding  = 0;
489       }
490     }
491 
492     config->useSpeechConfig = useSpeechConfig;
493 
494     /* PS settings */
495     config->bParametricStereo = bParametricStereo;
496 
497     return 1 ;
498   }
499   else {
500     return 0 ;
501   }
502 }
503 
504 /*****************************************************************************
505 
506  functionname: FDKsbrEnc_InitializeSbrDefaults
507  description:  initializes the SBR confifuration
508  returns:      error status
509  input:        - core codec type,
510                - factor of SBR to core frame length,
511                - core frame length
512  output:       initialized SBR configuration
513 
514 *****************************************************************************/
515 static UINT
FDKsbrEnc_InitializeSbrDefaults(sbrConfigurationPtr config,INT downSampleFactor,UINT codecGranuleLen)516 FDKsbrEnc_InitializeSbrDefaults (sbrConfigurationPtr config,
517                                  INT                 downSampleFactor,
518                                  UINT                codecGranuleLen
519                                  )
520 {
521     if ( (downSampleFactor < 1 || downSampleFactor > 2) ||
522          (codecGranuleLen*downSampleFactor > QMF_CHANNELS*QMF_MAX_TIME_SLOTS) )
523        return(0); /* error */
524 
525     config->SendHeaderDataTime     = 1000;
526     config->useWaveCoding          = 0;
527     config->crcSbr                 = 0;
528     config->dynBwSupported         = 1;
529     config->tran_thr               = 13000;
530     config->parametricCoding       = 1;
531 
532     config->sbrFrameSize           = codecGranuleLen * downSampleFactor;
533     config->downSampleFactor       = downSampleFactor;
534 
535     /* sbr default parameters */
536     config->sbr_data_extra         = 0;
537     config->amp_res                = SBR_AMP_RES_3_0 ;
538     config->tran_fc                = 0 ;
539     config->tran_det_mode          = 1 ;
540     config->spread                 = 1 ;
541     config->stat                   = 0 ;
542     config->e                      = 1 ;
543     config->deltaTAcrossFrames     = 1 ;
544     config->dF_edge_1stEnv         = FL2FXCONST_DBL(0.3f) ;
545     config->dF_edge_incr           = FL2FXCONST_DBL(0.3f) ;
546 
547     config->sbr_invf_mode   = INVF_SWITCHED;
548     config->sbr_xpos_mode   = XPOS_LC;
549     config->sbr_xpos_ctrl   = SBR_XPOS_CTRL_DEFAULT;
550     config->sbr_xpos_level  = 0;
551     config->useSaPan        = 0;
552     config->dynBwEnabled    = 0;
553 
554 
555     /* the following parameters are overwritten by the FDKsbrEnc_AdjustSbrSettings() function since
556        they are included in the tuning table */
557     config->stereoMode             = SBR_SWITCH_LRC;
558     config->ana_max_level          = 6;
559     config->noiseFloorOffset       = 0;
560     config->startFreq              = 5; /*  5.9 respectively  6.0 kHz at fs = 44.1/48 kHz */
561     config->stopFreq               = 9; /* 16.2 respectively 16.8 kHz at fs = 44.1/48 kHz */
562 
563 
564     /* header_extra_1 */
565     config->freqScale       = SBR_FREQ_SCALE_DEFAULT;
566     config->alterScale      = SBR_ALTER_SCALE_DEFAULT;
567     config->sbr_noise_bands = SBR_NOISE_BANDS_DEFAULT;
568 
569     /* header_extra_2 */
570     config->sbr_limiter_bands    = SBR_LIMITER_BANDS_DEFAULT;
571     config->sbr_limiter_gains    = SBR_LIMITER_GAINS_DEFAULT;
572     config->sbr_interpol_freq    = SBR_INTERPOL_FREQ_DEFAULT;
573     config->sbr_smoothing_length = SBR_SMOOTHING_LENGTH_DEFAULT;
574 
575     return 1;
576 }
577 
578 
579 /*****************************************************************************
580 
581  functionname: DeleteEnvChannel
582  description:  frees memory of one SBR channel
583  returns:      -
584  input:        handle of channel
585  output:       released handle
586 
587 *****************************************************************************/
588 static void
deleteEnvChannel(HANDLE_ENV_CHANNEL hEnvCut)589 deleteEnvChannel (HANDLE_ENV_CHANNEL hEnvCut)
590 {
591   if (hEnvCut) {
592 
593     FDKsbrEnc_DeleteTonCorrParamExtr(&hEnvCut->TonCorr);
594 
595     FDKsbrEnc_deleteExtractSbrEnvelope (&hEnvCut->sbrExtractEnvelope);
596   }
597 
598 }
599 
600 
601 /*****************************************************************************
602 
603  functionname: sbrEncoder_ChannelClose
604  description:  close the channel coding handle
605  returns:
606  input:        phSbrChannel
607  output:
608 
609 *****************************************************************************/
610 static void
sbrEncoder_ChannelClose(HANDLE_SBR_CHANNEL hSbrChannel)611 sbrEncoder_ChannelClose(HANDLE_SBR_CHANNEL hSbrChannel)
612 {
613   if (hSbrChannel != NULL)
614   {
615     deleteEnvChannel (&hSbrChannel->hEnvChannel);
616   }
617 }
618 
619 /*****************************************************************************
620 
621  functionname: sbrEncoder_ElementClose
622  description:  close the channel coding handle
623  returns:
624  input:        phSbrChannel
625  output:
626 
627 *****************************************************************************/
628 static void
sbrEncoder_ElementClose(HANDLE_SBR_ELEMENT * phSbrElement)629 sbrEncoder_ElementClose(HANDLE_SBR_ELEMENT *phSbrElement)
630 {
631   HANDLE_SBR_ELEMENT hSbrElement = *phSbrElement;
632 
633   if (hSbrElement!=NULL) {
634     if (hSbrElement->sbrConfigData.v_k_master)
635       FreeRam_Sbr_v_k_master(&hSbrElement->sbrConfigData.v_k_master);
636     if (hSbrElement->sbrConfigData.freqBandTable[LO])
637       FreeRam_Sbr_freqBandTableLO(&hSbrElement->sbrConfigData.freqBandTable[LO]);
638     if (hSbrElement->sbrConfigData.freqBandTable[HI])
639       FreeRam_Sbr_freqBandTableHI(&hSbrElement->sbrConfigData.freqBandTable[HI]);
640 
641     FreeRam_SbrElement(phSbrElement);
642   }
643   return ;
644 
645 }
646 
647 
sbrEncoder_Close(HANDLE_SBR_ENCODER * phSbrEncoder)648 void sbrEncoder_Close (HANDLE_SBR_ENCODER *phSbrEncoder)
649 {
650   HANDLE_SBR_ENCODER hSbrEncoder = *phSbrEncoder;
651 
652   if (hSbrEncoder != NULL)
653   {
654     int el, ch;
655 
656     for (el=0; el<(8); el++)
657     {
658       if (hSbrEncoder->sbrElement[el]!=NULL) {
659         sbrEncoder_ElementClose(&hSbrEncoder->sbrElement[el]);
660       }
661     }
662 
663     /* Close sbr Channels */
664     for (ch=0; ch<(8); ch++)
665     {
666       if (hSbrEncoder->pSbrChannel[ch]) {
667         sbrEncoder_ChannelClose(hSbrEncoder->pSbrChannel[ch]);
668         FreeRam_SbrChannel(&hSbrEncoder->pSbrChannel[ch]);
669       }
670 
671       if (hSbrEncoder->QmfAnalysis[ch].FilterStates)
672         FreeRam_Sbr_QmfStatesAnalysis((FIXP_QAS**)&hSbrEncoder->QmfAnalysis[ch].FilterStates);
673 
674 
675     }
676 
677     if (hSbrEncoder->hParametricStereo)
678       PSEnc_Destroy(&hSbrEncoder->hParametricStereo);
679     if (hSbrEncoder->qmfSynthesisPS.FilterStates)
680       FreeRam_PsQmfStatesSynthesis((FIXP_DBL**)&hSbrEncoder->qmfSynthesisPS.FilterStates);
681 
682     /* Release Overlay */
683     FreeRam_SbrDynamic_RAM((FIXP_DBL**)&hSbrEncoder->pSBRdynamic_RAM);
684 
685 
686     FreeRam_SbrEncoder(phSbrEncoder);
687   }
688 
689 }
690 
691 /*****************************************************************************
692 
693  functionname: updateFreqBandTable
694  description:  updates vk_master
695  returns:      -
696  input:        config handle
697  output:       error info
698 
699 *****************************************************************************/
updateFreqBandTable(HANDLE_SBR_CONFIG_DATA sbrConfigData,HANDLE_SBR_HEADER_DATA sbrHeaderData,const INT downSampleFactor)700 static INT updateFreqBandTable(
701         HANDLE_SBR_CONFIG_DATA  sbrConfigData,
702         HANDLE_SBR_HEADER_DATA  sbrHeaderData,
703         const INT               downSampleFactor
704         )
705 {
706   INT k0, k2;
707 
708   if( FDKsbrEnc_FindStartAndStopBand (
709               sbrConfigData->sampleFreq,
710               sbrConfigData->sampleFreq >> (downSampleFactor-1),
711               sbrConfigData->noQmfBands,
712               sbrHeaderData->sbr_start_frequency,
713               sbrHeaderData->sbr_stop_frequency,
714              &k0,
715              &k2
716               )
717     )
718     return(1);
719 
720 
721   if( FDKsbrEnc_UpdateFreqScale(
722               sbrConfigData->v_k_master,
723              &sbrConfigData->num_Master,
724               k0,
725               k2,
726               sbrHeaderData->freqScale,
727               sbrHeaderData->alterScale
728               )
729     )
730     return(1);
731 
732 
733   sbrHeaderData->sbr_xover_band=0;
734 
735 
736   if( FDKsbrEnc_UpdateHiRes(
737               sbrConfigData->freqBandTable[HI],
738               &sbrConfigData->nSfb[HI],
739               sbrConfigData->v_k_master,
740               sbrConfigData->num_Master,
741               &sbrHeaderData->sbr_xover_band
742               )
743     )
744     return(1);
745 
746 
747   FDKsbrEnc_UpdateLoRes(
748           sbrConfigData->freqBandTable[LO],
749           &sbrConfigData->nSfb[LO],
750           sbrConfigData->freqBandTable[HI],
751           sbrConfigData->nSfb[HI]
752           );
753 
754 
755   sbrConfigData->xOverFreq = (sbrConfigData->freqBandTable[LOW_RES][0] * sbrConfigData->sampleFreq / sbrConfigData->noQmfBands+1)>>1;
756 
757   return (0);
758 }
759 
760 
761 /*****************************************************************************
762 
763  functionname: resetEnvChannel
764  description:  resets parameters and allocates memory
765  returns:      error status
766  input:
767  output:       hEnv
768 
769 *****************************************************************************/
resetEnvChannel(HANDLE_SBR_CONFIG_DATA sbrConfigData,HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_ENV_CHANNEL hEnv)770 static INT resetEnvChannel (HANDLE_SBR_CONFIG_DATA  sbrConfigData,
771                             HANDLE_SBR_HEADER_DATA  sbrHeaderData,
772                             HANDLE_ENV_CHANNEL      hEnv)
773 {
774   /* note !!! hEnv->encEnvData.noOfnoisebands will be updated later in function FDKsbrEnc_extractSbrEnvelope !!!*/
775   hEnv->TonCorr.sbrNoiseFloorEstimate.noiseBands =  sbrHeaderData->sbr_noise_bands;
776 
777 
778   if(FDKsbrEnc_ResetTonCorrParamExtr(&hEnv->TonCorr,
779                            sbrConfigData->xposCtrlSwitch,
780                            sbrConfigData->freqBandTable[HI][0],
781                            sbrConfigData->v_k_master,
782                            sbrConfigData->num_Master,
783                            sbrConfigData->sampleFreq,
784                            sbrConfigData->freqBandTable,
785                            sbrConfigData->nSfb,
786                            sbrConfigData->noQmfBands))
787     return(1);
788 
789   hEnv->sbrCodeNoiseFloor.nSfb[LO] = hEnv->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
790   hEnv->sbrCodeNoiseFloor.nSfb[HI] = hEnv->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
791 
792   hEnv->sbrCodeEnvelope.nSfb[LO] = sbrConfigData->nSfb[LO];
793   hEnv->sbrCodeEnvelope.nSfb[HI] = sbrConfigData->nSfb[HI];
794 
795   hEnv->encEnvData.noHarmonics = sbrConfigData->nSfb[HI];
796 
797   hEnv->sbrCodeEnvelope.upDate = 0;
798   hEnv->sbrCodeNoiseFloor.upDate = 0;
799 
800   return (0);
801 }
802 
803 /* ****************************** FDKsbrEnc_SbrGetXOverFreq ******************************/
804 /**
805  * @fn
806  * @brief       calculates the closest possible crossover frequency
807  * @return      the crossover frequency SBR accepts
808  *
809  */
810 static INT
FDKsbrEnc_SbrGetXOverFreq(HANDLE_SBR_ELEMENT hEnv,INT xoverFreq)811 FDKsbrEnc_SbrGetXOverFreq(HANDLE_SBR_ELEMENT  hEnv,        /*!< handle to SBR encoder instance */
812                           INT                 xoverFreq)   /*!< from core coder suggested crossover frequency */
813 {
814   INT band;
815   INT lastDiff, newDiff;
816   INT cutoffSb;
817 
818   UCHAR *RESTRICT pVKMaster = hEnv->sbrConfigData.v_k_master;
819 
820   /* Check if there is a matching cutoff frequency in the master table */
821   cutoffSb = (4*xoverFreq * hEnv->sbrConfigData.noQmfBands / hEnv->sbrConfigData.sampleFreq + 1)>>1;
822   lastDiff = cutoffSb;
823   for (band = 0; band < hEnv->sbrConfigData.num_Master; band++) {
824 
825     newDiff = fixp_abs((INT)pVKMaster[band] - cutoffSb);
826 
827     if(newDiff >= lastDiff) {
828       band--;
829       break;
830     }
831 
832     lastDiff = newDiff;
833   }
834 
835   return ((pVKMaster[band] * hEnv->sbrConfigData.sampleFreq/hEnv->sbrConfigData.noQmfBands+1)>>1);
836 }
837 
838 /*****************************************************************************
839 
840  functionname: FDKsbrEnc_EnvEncodeFrame
841  description: performs the sbr envelope calculation for one element
842  returns:
843  input:
844  output:
845 
846 *****************************************************************************/
847 INT
FDKsbrEnc_EnvEncodeFrame(HANDLE_SBR_ENCODER hEnvEncoder,int iElement,INT_PCM * samples,UINT timeInStride,UINT * sbrDataBits,UCHAR * sbrData,int clearOutput)848 FDKsbrEnc_EnvEncodeFrame(HANDLE_SBR_ENCODER   hEnvEncoder,
849                          int                  iElement,
850                          INT_PCM             *samples,                 /*!< time samples, always interleaved */
851                          UINT                 timeInStride,            /*!< time buffer channel interleaving stride */
852                          UINT                *sbrDataBits,             /*!< Size of SBR payload  */
853                          UCHAR               *sbrData,                 /*!< SBR payload  */
854                          int                  clearOutput              /*!< Do not consider any input signal */
855                         )
856 {
857   HANDLE_SBR_ELEMENT hSbrElement = hEnvEncoder->sbrElement[iElement];
858   FDK_CRCINFO  crcInfo;
859   INT    crcReg;
860   INT    ch;
861   INT    band;
862   INT    cutoffSb;
863   INT    newXOver;
864 
865   if (hEnvEncoder == NULL)
866     return -1;
867 
868   hSbrElement = hEnvEncoder->sbrElement[iElement];
869 
870   if (hSbrElement == NULL)
871     return -1;
872 
873 
874   /* header bitstream handling */
875   HANDLE_SBR_BITSTREAM_DATA sbrBitstreamData = &hSbrElement->sbrBitstreamData;
876 
877   INT psHeaderActive = 0;
878   sbrBitstreamData->HeaderActive = 0;
879 
880   /* Anticipate PS header because of internal PS bitstream delay in order to be in sync with SBR header. */
881   if ( sbrBitstreamData->CountSendHeaderData==(sbrBitstreamData->NrSendHeaderData-1) )
882   {
883       psHeaderActive = 1;
884   }
885 
886   /* Signal SBR header to be written into bitstream */
887   if ( sbrBitstreamData->CountSendHeaderData==0  )
888   {
889      sbrBitstreamData->HeaderActive = 1;
890   }
891 
892   /* Increment header interval counter */
893   if (sbrBitstreamData->NrSendHeaderData == 0) {
894     sbrBitstreamData->CountSendHeaderData = 1;
895   }
896   else {
897     if (sbrBitstreamData->CountSendHeaderData >= 0) {
898       sbrBitstreamData->CountSendHeaderData++;
899       sbrBitstreamData->CountSendHeaderData %= sbrBitstreamData->NrSendHeaderData;
900     }
901   }
902 
903   if (hSbrElement->CmonData.dynBwEnabled ) {
904     INT i;
905     for ( i = 4; i > 0; i-- )
906       hSbrElement->dynXOverFreqDelay[i] = hSbrElement->dynXOverFreqDelay[i-1];
907 
908     hSbrElement->dynXOverFreqDelay[0] = hSbrElement->CmonData.dynXOverFreqEnc;
909     if (hSbrElement->dynXOverFreqDelay[1] > hSbrElement->dynXOverFreqDelay[2])
910       newXOver = hSbrElement->dynXOverFreqDelay[2];
911     else
912       newXOver = hSbrElement->dynXOverFreqDelay[1];
913 
914     /* has the crossover frequency changed? */
915     if ( hSbrElement->sbrConfigData.dynXOverFreq != newXOver ) {
916 
917       /* get corresponding master band */
918       cutoffSb = ((4* newXOver * hSbrElement->sbrConfigData.noQmfBands
919                   / hSbrElement->sbrConfigData.sampleFreq)+1)>>1;
920 
921       for ( band = 0; band < hSbrElement->sbrConfigData.num_Master; band++ ) {
922         if ( cutoffSb == hSbrElement->sbrConfigData.v_k_master[band] )
923           break;
924       }
925       FDK_ASSERT( band < hSbrElement->sbrConfigData.num_Master );
926 
927       hSbrElement->sbrConfigData.dynXOverFreq = newXOver;
928       hSbrElement->sbrHeaderData.sbr_xover_band = band;
929       hSbrElement->sbrBitstreamData.HeaderActive=1;
930       psHeaderActive = 1; /* ps header is one frame delayed */
931 
932       /*
933         update vk_master table
934       */
935       if(updateFreqBandTable(&hSbrElement->sbrConfigData,
936                              &hSbrElement->sbrHeaderData,
937                              hEnvEncoder->downSampleFactor
938                              ))
939         return(1);
940 
941 
942       /* reset SBR channels */
943       INT nEnvCh = hSbrElement->sbrConfigData.nChannels;
944       for ( ch = 0; ch < nEnvCh; ch++ ) {
945         if(resetEnvChannel (&hSbrElement->sbrConfigData,
946                             &hSbrElement->sbrHeaderData,
947                             &hSbrElement->sbrChannel[ch]->hEnvChannel))
948           return(1);
949 
950       }
951     }
952   }
953 
954   /*
955     allocate space for dummy header and crc
956   */
957   crcReg = FDKsbrEnc_InitSbrBitstream(&hSbrElement->CmonData,
958                                        hSbrElement->payloadDelayLine[hEnvEncoder->nBitstrDelay],
959                                        MAX_PAYLOAD_SIZE*sizeof(UCHAR),
960                                       &crcInfo,
961                                        hSbrElement->sbrConfigData.sbrSyntaxFlags);
962 
963   /* Temporal Envelope Data */
964   SBR_FRAME_TEMP_DATA _fData;
965   SBR_FRAME_TEMP_DATA *fData = &_fData;
966   SBR_ENV_TEMP_DATA eData[MAX_NUM_CHANNELS];
967 
968   /* Init Temporal Envelope Data */
969   {
970     int i;
971 
972     FDKmemclear(&eData[0], sizeof(SBR_ENV_TEMP_DATA));
973     FDKmemclear(&eData[1], sizeof(SBR_ENV_TEMP_DATA));
974     FDKmemclear(fData, sizeof(SBR_FRAME_TEMP_DATA));
975 
976     for(i=0; i<MAX_NUM_NOISE_VALUES; i++)
977       fData->res[i] = FREQ_RES_HIGH;
978   }
979 
980 
981   if (!clearOutput)
982   {
983     /*
984      * Transform audio data into QMF domain
985      */
986     for(ch = 0; ch < hSbrElement->sbrConfigData.nChannels; ch++)
987     {
988       HANDLE_ENV_CHANNEL h_envChan = &hSbrElement->sbrChannel[ch]->hEnvChannel;
989       HANDLE_SBR_EXTRACT_ENVELOPE sbrExtrEnv = &h_envChan->sbrExtractEnvelope;
990 
991       if(hSbrElement->elInfo.fParametricStereo == 0)
992       {
993         QMF_SCALE_FACTOR tmpScale;
994         FIXP_DBL **pQmfReal, **pQmfImag;
995         C_AALLOC_SCRATCH_START(qmfWorkBuffer, FIXP_DBL, QMF_CHANNELS*2)
996 
997 
998         /* Obtain pointers to QMF buffers. */
999         pQmfReal = sbrExtrEnv->rBuffer;
1000         pQmfImag = sbrExtrEnv->iBuffer;
1001 
1002         qmfAnalysisFiltering( hSbrElement->hQmfAnalysis[ch],
1003                                pQmfReal,
1004                                pQmfImag,
1005                               &tmpScale,
1006                                samples + hSbrElement->elInfo.ChannelIndex[ch],
1007                                timeInStride,
1008                                qmfWorkBuffer );
1009 
1010         h_envChan->qmfScale = tmpScale.lb_scale + 7;
1011 
1012 
1013         C_AALLOC_SCRATCH_END(qmfWorkBuffer, FIXP_DBL, QMF_CHANNELS*2)
1014 
1015       } /* fParametricStereo == 0 */
1016 
1017 
1018       /*
1019         Parametric Stereo processing
1020       */
1021       if (hSbrElement->elInfo.fParametricStereo)
1022       {
1023         INT error = noError;
1024 
1025 
1026         /* Limit Parametric Stereo to one instance */
1027         FDK_ASSERT(ch == 0);
1028 
1029 
1030         if(error == noError){
1031           /* parametric stereo processing:
1032              - input:
1033                o left and right time domain samples
1034              - processing:
1035                o stereo qmf analysis
1036                o stereo hybrid analysis
1037                o ps parameter extraction
1038                o downmix + hybrid synthesis
1039              - output:
1040                o downmixed qmf data is written to sbrExtrEnv->rBuffer and sbrExtrEnv->iBuffer
1041           */
1042           SCHAR qmfScale;
1043           INT_PCM* pSamples[2] = {samples + hSbrElement->elInfo.ChannelIndex[0],samples + hSbrElement->elInfo.ChannelIndex[1]};
1044           error = FDKsbrEnc_PSEnc_ParametricStereoProcessing( hEnvEncoder->hParametricStereo,
1045                                                               pSamples,
1046                                                               timeInStride,
1047                                                               hSbrElement->hQmfAnalysis,
1048                                                               sbrExtrEnv->rBuffer,
1049                                                               sbrExtrEnv->iBuffer,
1050                                                               samples + hSbrElement->elInfo.ChannelIndex[ch],
1051                                                              &hEnvEncoder->qmfSynthesisPS,
1052                                                              &qmfScale,
1053                                                               psHeaderActive );
1054           if (noError != error)
1055           {
1056             error = handBack(error);
1057           }
1058           h_envChan->qmfScale = (int)qmfScale;
1059         }
1060 
1061 
1062       } /* if (hEnvEncoder->hParametricStereo) */
1063 
1064       /*
1065 
1066          Extract Envelope relevant things from QMF data
1067 
1068       */
1069       FDKsbrEnc_extractSbrEnvelope1(
1070                                 &hSbrElement->sbrConfigData,
1071                                 &hSbrElement->sbrHeaderData,
1072                                 &hSbrElement->sbrBitstreamData,
1073                                  h_envChan,
1074                                 &hSbrElement->CmonData,
1075                                 &eData[ch],
1076                                  fData
1077                                  );
1078 
1079     } /* hEnvEncoder->sbrConfigData.nChannels */
1080  }
1081 
1082   /*
1083      Process Envelope relevant things and calculate envelope data and write payload
1084   */
1085   FDKsbrEnc_extractSbrEnvelope2(
1086                                 &hSbrElement->sbrConfigData,
1087                                 &hSbrElement->sbrHeaderData,
1088                                 (hSbrElement->elInfo.fParametricStereo) ? hEnvEncoder->hParametricStereo : NULL,
1089                                 &hSbrElement->sbrBitstreamData,
1090                                 &hSbrElement->sbrChannel[0]->hEnvChannel,
1091                                 &hSbrElement->sbrChannel[1]->hEnvChannel,
1092                                 &hSbrElement->CmonData,
1093                                  eData,
1094                                  fData,
1095                                  clearOutput
1096                                );
1097 
1098   /*
1099     format payload, calculate crc
1100   */
1101   FDKsbrEnc_AssembleSbrBitstream(&hSbrElement->CmonData, &crcInfo, crcReg, hSbrElement->sbrConfigData.sbrSyntaxFlags);
1102 
1103   /*
1104     save new payload, set to zero length if greater than MAX_PAYLOAD_SIZE
1105   */
1106   hSbrElement->payloadDelayLineSize[hEnvEncoder->nBitstrDelay] = FDKgetValidBits(&hSbrElement->CmonData.sbrBitbuf);
1107 
1108   if(hSbrElement->payloadDelayLineSize[hEnvEncoder->nBitstrDelay] > (MAX_PAYLOAD_SIZE<<3))
1109     hSbrElement->payloadDelayLineSize[hEnvEncoder->nBitstrDelay]=0;
1110 
1111   /* While filling the Delay lines, sbrData is NULL */
1112   if (sbrData) {
1113     *sbrDataBits = hSbrElement->payloadDelayLineSize[0];
1114     FDKmemcpy(sbrData, hSbrElement->payloadDelayLine[0], (hSbrElement->payloadDelayLineSize[0]+7)>>3);
1115 
1116 
1117   }
1118 
1119 
1120 /*******************************/
1121 
1122   if (hEnvEncoder->fTimeDomainDownsampling)
1123   {
1124     int ch;
1125     int nChannels = hSbrElement->sbrConfigData.nChannels;
1126 
1127     for (ch=0; ch < nChannels; ch++)
1128     {
1129       INT nOutSamples;
1130 
1131       FDKaacEnc_Downsample(&hSbrElement->sbrChannel[ch]->downSampler,
1132                             samples + hSbrElement->elInfo.ChannelIndex[ch] + hEnvEncoder->bufferOffset,
1133                             hSbrElement->sbrConfigData.frameSize,
1134                             timeInStride,
1135                             samples + hSbrElement->elInfo.ChannelIndex[ch],
1136                            &nOutSamples,
1137                             hEnvEncoder->nChannels);
1138     }
1139   } /* downsample */
1140 
1141 
1142   return (0);
1143 }
1144 
1145 /*****************************************************************************
1146 
1147  functionname: createEnvChannel
1148  description:  initializes parameters and allocates memory
1149  returns:      error status
1150  input:
1151  output:       hEnv
1152 
1153 *****************************************************************************/
1154 
1155 static INT
createEnvChannel(HANDLE_ENV_CHANNEL hEnv,INT channel,UCHAR * dynamic_RAM)1156 createEnvChannel (HANDLE_ENV_CHANNEL     hEnv,
1157                   INT                    channel
1158                  ,UCHAR*                 dynamic_RAM
1159                  )
1160 {
1161   FDKmemclear(hEnv,sizeof (struct ENV_CHANNEL));
1162 
1163   if ( FDKsbrEnc_CreateTonCorrParamExtr(&hEnv->TonCorr,
1164                                          channel) )
1165   {
1166     return(1);
1167   }
1168 
1169   if ( FDKsbrEnc_CreateExtractSbrEnvelope (&hEnv->sbrExtractEnvelope,
1170                                channel
1171                               ,/*chan*/0
1172                               ,dynamic_RAM
1173                               ) )
1174   {
1175     return(1);
1176   }
1177 
1178   return 0;
1179 }
1180 
1181 /*****************************************************************************
1182 
1183  functionname: initEnvChannel
1184  description:  initializes parameters
1185  returns:      error status
1186  input:
1187  output:
1188 
1189 *****************************************************************************/
1190 static INT
initEnvChannel(HANDLE_SBR_CONFIG_DATA sbrConfigData,HANDLE_SBR_HEADER_DATA sbrHeaderData,HANDLE_ENV_CHANNEL hEnv,sbrConfigurationPtr params,ULONG statesInitFlag,INT chanInEl,UCHAR * dynamic_RAM)1191 initEnvChannel (  HANDLE_SBR_CONFIG_DATA sbrConfigData,
1192                   HANDLE_SBR_HEADER_DATA sbrHeaderData,
1193                   HANDLE_ENV_CHANNEL     hEnv,
1194                   sbrConfigurationPtr    params,
1195                   ULONG                  statesInitFlag
1196                  ,INT                    chanInEl
1197                  ,UCHAR*                 dynamic_RAM
1198                  )
1199 {
1200   int frameShift, tran_off=0;
1201   INT e;
1202   INT tran_fc;
1203   INT timeSlots, timeStep, startIndex;
1204   INT noiseBands[2] = { 3, 3 };
1205 
1206   e = 1 << params->e;
1207 
1208   FDK_ASSERT(params->e >= 0);
1209 
1210   hEnv->encEnvData.freq_res_fixfix = 1;
1211   hEnv->fLevelProtect = 0;
1212 
1213   hEnv->encEnvData.ldGrid = (sbrConfigData->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) ? 1 : 0;
1214 
1215   hEnv->encEnvData.sbr_xpos_mode = (XPOS_MODE)params->sbr_xpos_mode;
1216 
1217   if (hEnv->encEnvData.sbr_xpos_mode == XPOS_SWITCHED) {
1218     /*
1219        no other type than XPOS_MDCT or XPOS_SPEECH allowed,
1220        but enable switching
1221     */
1222     sbrConfigData->switchTransposers = TRUE;
1223     hEnv->encEnvData.sbr_xpos_mode = XPOS_MDCT;
1224   }
1225   else {
1226     sbrConfigData->switchTransposers = FALSE;
1227   }
1228 
1229   hEnv->encEnvData.sbr_xpos_ctrl = params->sbr_xpos_ctrl;
1230 
1231 
1232   /* extended data */
1233   if(params->parametricCoding) {
1234     hEnv->encEnvData.extended_data = 1;
1235   }
1236   else {
1237     hEnv->encEnvData.extended_data = 0;
1238   }
1239 
1240   hEnv->encEnvData.extension_size = 0;
1241 
1242   startIndex = QMF_FILTER_PROTOTYPE_SIZE - sbrConfigData->noQmfBands;
1243 
1244   switch (params->sbrFrameSize) {
1245   case 2304:
1246     timeSlots = 18;
1247     break;
1248   case 2048:
1249   case 1024:
1250   case 512:
1251     timeSlots = 16;
1252     break;
1253   case 1920:
1254   case 960:
1255   case 480:
1256     timeSlots = 15;
1257     break;
1258   case 1152:
1259     timeSlots = 9;
1260     break;
1261   default:
1262     return (1); /* Illegal frame size */
1263   }
1264 
1265   timeStep = sbrConfigData->noQmfSlots / timeSlots;
1266 
1267   if ( FDKsbrEnc_InitTonCorrParamExtr(params->sbrFrameSize,
1268                                      &hEnv->TonCorr,
1269                                       sbrConfigData,
1270                                       timeSlots,
1271                                       params->sbr_xpos_ctrl,
1272                                       params->ana_max_level,
1273                                       sbrHeaderData->sbr_noise_bands,
1274                                       params->noiseFloorOffset,
1275                                       params->useSpeechConfig) )
1276     return(1);
1277 
1278   hEnv->encEnvData.noOfnoisebands = hEnv->TonCorr.sbrNoiseFloorEstimate.noNoiseBands;
1279 
1280   noiseBands[0] = hEnv->encEnvData.noOfnoisebands;
1281   noiseBands[1] = hEnv->encEnvData.noOfnoisebands;
1282 
1283   hEnv->encEnvData.sbr_invf_mode = (INVF_MODE)params->sbr_invf_mode;
1284 
1285   if (hEnv->encEnvData.sbr_invf_mode == INVF_SWITCHED) {
1286     hEnv->encEnvData.sbr_invf_mode = INVF_MID_LEVEL;
1287     hEnv->TonCorr.switchInverseFilt = TRUE;
1288   }
1289   else {
1290     hEnv->TonCorr.switchInverseFilt = FALSE;
1291   }
1292 
1293 
1294   tran_fc  = params->tran_fc;
1295 
1296   if (tran_fc == 0) {
1297     tran_fc = fixMin (5000, FDKsbrEnc_getSbrStartFreqRAW (sbrHeaderData->sbr_start_frequency,params->codecSettings.sampleFreq));
1298   }
1299 
1300   tran_fc = (tran_fc*4*sbrConfigData->noQmfBands/sbrConfigData->sampleFreq + 1)>>1;
1301 
1302   if (sbrConfigData->sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) {
1303     frameShift = LD_PRETRAN_OFF;
1304     tran_off = LD_PRETRAN_OFF + FRAME_MIDDLE_SLOT_512LD*timeStep;
1305   } else
1306   {
1307     frameShift = 0;
1308     switch (timeSlots) {
1309       /* The factor of 2 is by definition. */
1310       case NUMBER_TIME_SLOTS_2048: tran_off = 8 + FRAME_MIDDLE_SLOT_2048 * timeStep; break;
1311       case NUMBER_TIME_SLOTS_1920: tran_off = 7 + FRAME_MIDDLE_SLOT_1920 * timeStep; break;
1312       default: return 1;
1313     }
1314   }
1315   if ( FDKsbrEnc_InitExtractSbrEnvelope (&hEnv->sbrExtractEnvelope,
1316                                sbrConfigData->noQmfSlots,
1317                                sbrConfigData->noQmfBands, startIndex,
1318                                timeSlots, timeStep, tran_off,
1319                                statesInitFlag
1320                               ,chanInEl
1321                               ,dynamic_RAM
1322                               ,sbrConfigData->sbrSyntaxFlags
1323                                ) )
1324     return(1);
1325 
1326   if(FDKsbrEnc_InitSbrCodeEnvelope (&hEnv->sbrCodeEnvelope,
1327                             sbrConfigData->nSfb,
1328                             params->deltaTAcrossFrames,
1329                             params->dF_edge_1stEnv,
1330                             params->dF_edge_incr))
1331     return(1);
1332 
1333   if(FDKsbrEnc_InitSbrCodeEnvelope (&hEnv->sbrCodeNoiseFloor,
1334                             noiseBands,
1335                             params->deltaTAcrossFrames,
1336                             0,0))
1337     return(1);
1338 
1339   sbrConfigData->initAmpResFF = params->init_amp_res_FF;
1340 
1341   if(FDKsbrEnc_InitSbrHuffmanTables (&hEnv->encEnvData,
1342                            &hEnv->sbrCodeEnvelope,
1343                            &hEnv->sbrCodeNoiseFloor,
1344                            sbrHeaderData->sbr_amp_res))
1345    return(1);
1346 
1347   FDKsbrEnc_initFrameInfoGenerator (&hEnv->SbrEnvFrame,
1348                             params->spread,
1349                             e,
1350                             params->stat,
1351                             timeSlots,
1352                             hEnv->encEnvData.freq_res_fixfix
1353                             ,hEnv->encEnvData.ldGrid
1354                             );
1355 
1356   if(FDKsbrEnc_InitSbrTransientDetector (&hEnv->sbrTransientDetector,
1357                                           sbrConfigData->frameSize,
1358                                           sbrConfigData->sampleFreq,
1359                                           params,
1360                                           tran_fc,
1361                                           sbrConfigData->noQmfSlots,
1362                                           sbrConfigData->noQmfBands,
1363                                           hEnv->sbrExtractEnvelope.YBufferWriteOffset,
1364                                           hEnv->sbrExtractEnvelope.YBufferSzShift,
1365                                           frameShift,
1366                                           tran_off
1367                                           ))
1368   return(1);
1369 
1370 
1371   sbrConfigData->xposCtrlSwitch = params->sbr_xpos_ctrl;
1372 
1373   hEnv->encEnvData.noHarmonics = sbrConfigData->nSfb[HI];
1374   hEnv->encEnvData.addHarmonicFlag = 0;
1375 
1376   return (0);
1377 }
1378 
sbrEncoder_Open(HANDLE_SBR_ENCODER * phSbrEncoder,INT nElements,INT nChannels,INT supportPS)1379 INT sbrEncoder_Open(
1380         HANDLE_SBR_ENCODER  *phSbrEncoder,
1381         INT                  nElements,
1382         INT                  nChannels,
1383         INT                  supportPS
1384         )
1385 {
1386   INT i;
1387   INT errorStatus = 1;
1388   HANDLE_SBR_ENCODER hSbrEncoder = NULL;
1389 
1390   if (phSbrEncoder==NULL
1391      )
1392   {
1393     goto bail;
1394   }
1395 
1396   hSbrEncoder = GetRam_SbrEncoder();
1397   if (hSbrEncoder==NULL) {
1398     goto bail;
1399   }
1400   FDKmemclear(hSbrEncoder, sizeof(SBR_ENCODER));
1401 
1402       hSbrEncoder->pSBRdynamic_RAM = (UCHAR*)GetRam_SbrDynamic_RAM();
1403       hSbrEncoder->dynamicRam = hSbrEncoder->pSBRdynamic_RAM;
1404 
1405   for (i=0; i<nElements; i++) {
1406     hSbrEncoder->sbrElement[i] = GetRam_SbrElement(i);
1407     if (hSbrEncoder->sbrElement[i]==NULL) {
1408         goto bail;
1409     }
1410     FDKmemclear(hSbrEncoder->sbrElement[i], sizeof(SBR_ELEMENT));
1411     hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[LO] =  GetRam_Sbr_freqBandTableLO(i);
1412     hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[HI] =  GetRam_Sbr_freqBandTableHI(i);
1413     hSbrEncoder->sbrElement[i]->sbrConfigData.v_k_master        =  GetRam_Sbr_v_k_master(i);
1414     if ( (hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[LO]==NULL) ||
1415          (hSbrEncoder->sbrElement[i]->sbrConfigData.freqBandTable[HI]==NULL) ||
1416          (hSbrEncoder->sbrElement[i]->sbrConfigData.v_k_master==NULL) )
1417     {
1418         goto bail;
1419     }
1420   }
1421 
1422   for (i=0; i<nChannels; i++) {
1423     hSbrEncoder->pSbrChannel[i] = GetRam_SbrChannel(i);
1424     if (hSbrEncoder->pSbrChannel[i]==NULL) {
1425         goto bail;
1426     }
1427 
1428     if ( createEnvChannel(&hSbrEncoder->pSbrChannel[i]->hEnvChannel,
1429                            i
1430                           ,hSbrEncoder->dynamicRam
1431                            ) )
1432     {
1433         goto bail;
1434     }
1435 
1436   }
1437 
1438   for (i=0; i<fixMax(nChannels,(supportPS)?2:0); i++) {
1439     hSbrEncoder->QmfAnalysis[i].FilterStates = GetRam_Sbr_QmfStatesAnalysis(i);
1440     if (hSbrEncoder->QmfAnalysis[i].FilterStates==NULL) {
1441         goto bail;
1442     }
1443   }
1444 
1445   if (supportPS) {
1446     if (PSEnc_Create(&hSbrEncoder->hParametricStereo))
1447     {
1448       goto bail;
1449     }
1450 
1451     hSbrEncoder->qmfSynthesisPS.FilterStates = GetRam_PsQmfStatesSynthesis();
1452     if (hSbrEncoder->qmfSynthesisPS.FilterStates==NULL) {
1453       goto bail;
1454     }
1455   }  /* supportPS */
1456 
1457   *phSbrEncoder = hSbrEncoder;
1458 
1459   errorStatus = 0;
1460   return errorStatus;
1461 
1462 bail:
1463   /* Close SBR encoder instance */
1464   sbrEncoder_Close(&hSbrEncoder);
1465   return errorStatus;
1466 }
1467 
1468 static
FDKsbrEnc_Reallocate(HANDLE_SBR_ENCODER hSbrEncoder,SBR_ELEMENT_INFO elInfo[(8)],const INT noElements)1469 INT FDKsbrEnc_Reallocate(
1470                     HANDLE_SBR_ENCODER   hSbrEncoder,
1471                     SBR_ELEMENT_INFO     elInfo[(8)],
1472                     const INT            noElements)
1473 {
1474   INT totalCh = 0;
1475   INT totalQmf = 0;
1476   INT coreEl;
1477   INT el=-1;
1478 
1479   hSbrEncoder->lfeChIdx = -1; /* default value, until lfe found */
1480 
1481   for (coreEl=0; coreEl<noElements; coreEl++)
1482   {
1483     /* SBR only handles SCE and CPE's */
1484     if (elInfo[coreEl].elType == ID_SCE || elInfo[coreEl].elType == ID_CPE) {
1485       el++;
1486     } else {
1487       if (elInfo[coreEl].elType == ID_LFE) {
1488           hSbrEncoder->lfeChIdx = elInfo[coreEl].ChannelIndex[0];
1489       }
1490       continue;
1491     }
1492 
1493     SBR_ELEMENT_INFO    *pelInfo = &elInfo[coreEl];
1494     HANDLE_SBR_ELEMENT  hSbrElement = hSbrEncoder->sbrElement[el];
1495 
1496     int ch;
1497     for ( ch = 0; ch < pelInfo->nChannelsInEl; ch++ ) {
1498       hSbrElement->sbrChannel[ch] = hSbrEncoder->pSbrChannel[totalCh];
1499       totalCh++;
1500     }
1501     /* analysis QMF */
1502     for ( ch = 0; ch < ((pelInfo->fParametricStereo)?2:pelInfo->nChannelsInEl); ch++ ) {
1503       hSbrElement->elInfo.ChannelIndex[ch] = pelInfo->ChannelIndex[ch];
1504       hSbrElement->hQmfAnalysis[ch] = &hSbrEncoder->QmfAnalysis[totalQmf++];
1505     }
1506 
1507     /* Copy Element info */
1508     hSbrElement->elInfo.elType            = pelInfo->elType;
1509     hSbrElement->elInfo.instanceTag       = pelInfo->instanceTag;
1510     hSbrElement->elInfo.nChannelsInEl     = pelInfo->nChannelsInEl;
1511     hSbrElement->elInfo.fParametricStereo = pelInfo->fParametricStereo;
1512   } /* coreEl */
1513 
1514   return 0;
1515 }
1516 
1517 
1518 
1519 /*****************************************************************************
1520 
1521  functionname: FDKsbrEnc_EnvInit
1522  description:  initializes parameters
1523  returns:      error status
1524  input:
1525  output:       hEnv
1526 
1527 *****************************************************************************/
1528 static
FDKsbrEnc_EnvInit(HANDLE_SBR_ELEMENT hSbrElement,sbrConfigurationPtr params,INT * coreBandWith,AUDIO_OBJECT_TYPE aot,int nBitstrDelay,int nElement,const int headerPeriod,ULONG statesInitFlag,int fTimeDomainDownsampling,UCHAR * dynamic_RAM)1529 INT FDKsbrEnc_EnvInit (
1530                        HANDLE_SBR_ELEMENT   hSbrElement,
1531                        sbrConfigurationPtr params,
1532                        INT      *coreBandWith,
1533                        AUDIO_OBJECT_TYPE aot,
1534                        int       nBitstrDelay,
1535                        int       nElement,
1536                        const int headerPeriod,
1537                        ULONG     statesInitFlag,
1538                        int       fTimeDomainDownsampling
1539                       ,UCHAR    *dynamic_RAM
1540                       )
1541 {
1542   UCHAR    *bitstreamBuffer;
1543   int ch, i;
1544 
1545   if ((params->codecSettings.nChannels < 1) || (params->codecSettings.nChannels > MAX_NUM_CHANNELS)){
1546     return(1);
1547   }
1548 
1549   /* initialize the encoder handle  and structs*/
1550   bitstreamBuffer = hSbrElement->payloadDelayLine[nBitstrDelay];
1551 
1552   /* init and set syntax flags */
1553   hSbrElement->sbrConfigData.sbrSyntaxFlags = 0;
1554 
1555   switch (aot) {
1556   case AOT_DRM_MPEG_PS:
1557   case AOT_DRM_SBR:
1558     hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_SCALABLE;
1559     hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_DRM_CRC;
1560     hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_CRC;
1561     break;
1562   case AOT_ER_AAC_ELD:
1563     hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_LOW_DELAY;
1564     break;
1565   default:
1566     break;
1567   }
1568   if (params->crcSbr) {
1569     hSbrElement->sbrConfigData.sbrSyntaxFlags |= SBR_SYNTAX_CRC;
1570   }
1571 
1572   hSbrElement->sbrConfigData.noQmfBands = QMF_CHANNELS>>(2-params->downSampleFactor);
1573   switch (hSbrElement->sbrConfigData.noQmfBands)
1574   {
1575     case 64: hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize>>6;
1576              break;
1577     case 32: hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize>>5;
1578              break;
1579     default: hSbrElement->sbrConfigData.noQmfSlots = params->sbrFrameSize>>6;
1580              return(2);
1581   }
1582 
1583   FDKinitBitStream(&hSbrElement->CmonData.sbrBitbuf, bitstreamBuffer, MAX_PAYLOAD_SIZE*sizeof(UCHAR), 0, BS_WRITER);
1584 
1585   /*
1586     now initialize sbrConfigData, sbrHeaderData and sbrBitstreamData,
1587   */
1588   hSbrElement->sbrConfigData.nChannels = params->codecSettings.nChannels;
1589 
1590   if(params->codecSettings.nChannels == 2)
1591      hSbrElement->sbrConfigData.stereoMode = params->stereoMode;
1592   else
1593      hSbrElement->sbrConfigData.stereoMode = SBR_MONO;
1594 
1595   hSbrElement->sbrConfigData.frameSize   = params->sbrFrameSize;
1596 
1597   hSbrElement->sbrConfigData.sampleFreq = params->downSampleFactor * params->codecSettings.sampleFreq;
1598 
1599   hSbrElement->sbrBitstreamData.CountSendHeaderData = 0;
1600   if (params->SendHeaderDataTime > 0 ) {
1601 
1602     if (headerPeriod==-1) {
1603 
1604       hSbrElement->sbrBitstreamData.NrSendHeaderData = (INT)(params->SendHeaderDataTime * hSbrElement->sbrConfigData.sampleFreq
1605                                                / (1000 * hSbrElement->sbrConfigData.frameSize));
1606       hSbrElement->sbrBitstreamData.NrSendHeaderData = fixMax(hSbrElement->sbrBitstreamData.NrSendHeaderData,1);
1607     }
1608     else {
1609       /* assure header period at least once per second */
1610       hSbrElement->sbrBitstreamData.NrSendHeaderData = fixMin(fixMax(headerPeriod,1),(hSbrElement->sbrConfigData.sampleFreq/hSbrElement->sbrConfigData.frameSize));
1611     }
1612   }
1613   else {
1614    hSbrElement->sbrBitstreamData.NrSendHeaderData = 0;
1615   }
1616 
1617   hSbrElement->sbrHeaderData.sbr_data_extra = params->sbr_data_extra;
1618   hSbrElement->sbrBitstreamData.HeaderActive = 0;
1619   hSbrElement->sbrHeaderData.sbr_start_frequency = params->startFreq;
1620   hSbrElement->sbrHeaderData.sbr_stop_frequency  = params->stopFreq;
1621   hSbrElement->sbrHeaderData.sbr_xover_band = 0;
1622   hSbrElement->sbrHeaderData.sbr_lc_stereo_mode = 0;
1623 
1624   /* data_extra */
1625   if (params->sbr_xpos_ctrl!= SBR_XPOS_CTRL_DEFAULT)
1626      hSbrElement->sbrHeaderData.sbr_data_extra = 1;
1627 
1628   hSbrElement->sbrHeaderData.sbr_amp_res = (AMP_RES)params->amp_res;
1629 
1630   /* header_extra_1 */
1631   hSbrElement->sbrHeaderData.freqScale  = params->freqScale;
1632   hSbrElement->sbrHeaderData.alterScale = params->alterScale;
1633   hSbrElement->sbrHeaderData.sbr_noise_bands = params->sbr_noise_bands;
1634   hSbrElement->sbrHeaderData.header_extra_1 = 0;
1635 
1636   if ((params->freqScale != SBR_FREQ_SCALE_DEFAULT) ||
1637       (params->alterScale != SBR_ALTER_SCALE_DEFAULT) ||
1638       (params->sbr_noise_bands != SBR_NOISE_BANDS_DEFAULT))
1639   {
1640    hSbrElement->sbrHeaderData.header_extra_1 = 1;
1641   }
1642 
1643   /* header_extra_2 */
1644   hSbrElement->sbrHeaderData.sbr_limiter_bands = params->sbr_limiter_bands;
1645   hSbrElement->sbrHeaderData.sbr_limiter_gains = params->sbr_limiter_gains;
1646 
1647   if ((hSbrElement->sbrConfigData.sampleFreq > 48000) &&
1648       (hSbrElement->sbrHeaderData.sbr_start_frequency >= 9))
1649   {
1650     hSbrElement->sbrHeaderData.sbr_limiter_gains = SBR_LIMITER_GAINS_INFINITE;
1651   }
1652 
1653   hSbrElement->sbrHeaderData.sbr_interpol_freq = params->sbr_interpol_freq;
1654   hSbrElement->sbrHeaderData.sbr_smoothing_length = params->sbr_smoothing_length;
1655   hSbrElement->sbrHeaderData.header_extra_2 = 0;
1656 
1657   if ((params->sbr_limiter_bands != SBR_LIMITER_BANDS_DEFAULT) ||
1658       (params->sbr_limiter_gains != SBR_LIMITER_GAINS_DEFAULT) ||
1659       (params->sbr_interpol_freq != SBR_INTERPOL_FREQ_DEFAULT) ||
1660       (params->sbr_smoothing_length != SBR_SMOOTHING_LENGTH_DEFAULT))
1661   {
1662      hSbrElement->sbrHeaderData.header_extra_2 = 1;
1663   }
1664 
1665    /* other switches */
1666   hSbrElement->sbrConfigData.useWaveCoding             = params->useWaveCoding;
1667   hSbrElement->sbrConfigData.useParametricCoding       = params->parametricCoding;
1668 
1669   /* init freq band table */
1670   if(updateFreqBandTable(&hSbrElement->sbrConfigData,
1671                          &hSbrElement->sbrHeaderData,
1672                          params->downSampleFactor
1673                          ))
1674   {
1675     return(1);
1676   }
1677 
1678   /* now create envelope ext and QMF for each available channel */
1679   for ( ch = 0; ch < hSbrElement->sbrConfigData.nChannels; ch++ ) {
1680 
1681      if ( initEnvChannel(&hSbrElement->sbrConfigData,
1682                          &hSbrElement->sbrHeaderData,
1683                          &hSbrElement->sbrChannel[ch]->hEnvChannel,
1684                           params,
1685                           statesInitFlag
1686                         ,ch
1687                         ,dynamic_RAM
1688                          ) )
1689      {
1690        return(1);
1691      }
1692 
1693 
1694   } /* nChannels */
1695 
1696   /* reset and intialize analysis qmf */
1697   for ( ch = 0; ch < ((hSbrElement->elInfo.fParametricStereo)?2:hSbrElement->sbrConfigData.nChannels); ch++ )
1698   {
1699     int err;
1700     UINT qmfFlags = (hSbrElement->sbrConfigData.sbrSyntaxFlags & SBR_SYNTAX_LOW_DELAY) ? QMF_FLAG_CLDFB : 0;
1701     if (statesInitFlag)
1702       qmfFlags &= ~QMF_FLAG_KEEP_STATES;
1703     else
1704       qmfFlags |=  QMF_FLAG_KEEP_STATES;
1705 
1706     err = qmfInitAnalysisFilterBank( hSbrElement->hQmfAnalysis[ch],
1707                                      (FIXP_QAS*)hSbrElement->hQmfAnalysis[ch]->FilterStates,
1708                                      hSbrElement->sbrConfigData.noQmfSlots,
1709                                      hSbrElement->sbrConfigData.noQmfBands,
1710                                      hSbrElement->sbrConfigData.noQmfBands,
1711                                      hSbrElement->sbrConfigData.noQmfBands,
1712                                      qmfFlags );
1713     if (0!=err) {
1714       return err;
1715     }
1716   }
1717 
1718   /*  */
1719   hSbrElement->CmonData.xOverFreq = hSbrElement->sbrConfigData.xOverFreq;
1720   hSbrElement->CmonData.dynBwEnabled = (params->dynBwSupported && params->dynBwEnabled);
1721   hSbrElement->CmonData.dynXOverFreqEnc = FDKsbrEnc_SbrGetXOverFreq( hSbrElement, hSbrElement->CmonData.xOverFreq);
1722   for ( i = 0; i < 5; i++ )
1723       hSbrElement->dynXOverFreqDelay[i] = hSbrElement->CmonData.dynXOverFreqEnc;
1724   hSbrElement->CmonData.sbrNumChannels  = hSbrElement->sbrConfigData.nChannels;
1725   hSbrElement->sbrConfigData.dynXOverFreq = hSbrElement->CmonData.xOverFreq;
1726 
1727   /* Update Bandwith to be passed to the core encoder */
1728   *coreBandWith = hSbrElement->CmonData.xOverFreq;
1729 
1730   return(0);
1731  }
1732 
sbrEncoder_GetInBufferSize(int noChannels)1733 INT sbrEncoder_GetInBufferSize(int noChannels)
1734 {
1735   INT temp;
1736 
1737   temp = (2048);
1738   temp += 1024 + MAX_SAMPLE_DELAY;
1739   temp *= noChannels;
1740   temp *= sizeof(INT_PCM);
1741   return temp;
1742 }
1743 
1744 /*
1745  * Encode Dummy SBR payload frames to fill the delay lines.
1746  */
1747 static
FDKsbrEnc_DelayCompensation(HANDLE_SBR_ENCODER hEnvEnc,INT_PCM * timeBuffer)1748 INT FDKsbrEnc_DelayCompensation (
1749                                   HANDLE_SBR_ENCODER hEnvEnc,
1750                                   INT_PCM *timeBuffer
1751                                  )
1752 {
1753     int n, el;
1754 
1755     for (n=hEnvEnc->nBitstrDelay; n>0; n--)
1756     {
1757       for (el=0; el<hEnvEnc->noElements; el++)
1758       {
1759         if (FDKsbrEnc_EnvEncodeFrame(
1760                                      hEnvEnc,
1761                                      el,
1762                                      timeBuffer + hEnvEnc->downsampledOffset,
1763                                      hEnvEnc->sbrElement[el]->sbrConfigData.nChannels,
1764                                      NULL,
1765                                      NULL,
1766                                      1
1767                                     ))
1768           return -1;
1769       }
1770       sbrEncoder_UpdateBuffers(hEnvEnc, timeBuffer);
1771     }
1772 	  return 0;
1773 }
1774 
sbrEncoder_LimitBitRate(UINT bitRate,UINT numChannels,UINT coreSampleRate,AUDIO_OBJECT_TYPE aot)1775 UINT sbrEncoder_LimitBitRate(UINT bitRate, UINT numChannels, UINT coreSampleRate, AUDIO_OBJECT_TYPE aot)
1776 {
1777   UINT newBitRate;
1778   INT index;
1779 
1780   FDK_ASSERT(numChannels > 0 && numChannels <= 2);
1781   if (aot == AOT_PS) {
1782     if (numChannels == 2) {
1783       index = getPsTuningTableIndex(bitRate, &newBitRate);
1784       if (index == INVALID_TABLE_IDX) {
1785         bitRate = newBitRate;
1786       }
1787       /* Set numChannels to 1 because for PS we need a SBR SCE (mono) element. */
1788       numChannels = 1;
1789     } else {
1790       return 0;
1791     }
1792   }
1793   index = getSbrTuningTableIndex(bitRate, numChannels, coreSampleRate, aot, &newBitRate);
1794   if (index != INVALID_TABLE_IDX) {
1795     newBitRate = bitRate;
1796   }
1797 
1798   return newBitRate;
1799 }
1800 
sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot)1801 UINT sbrEncoder_IsSingleRatePossible(AUDIO_OBJECT_TYPE aot)
1802 {
1803   UINT isPossible=(AOT_PS==aot)?0:1;
1804   return isPossible;
1805 }
1806 
sbrEncoder_Init(HANDLE_SBR_ENCODER hSbrEncoder,SBR_ELEMENT_INFO elInfo[(8)],int noElements,INT_PCM * inputBuffer,INT * coreBandwidth,INT * inputBufferOffset,INT * numChannels,INT * coreSampleRate,UINT * downSampleFactor,INT * frameLength,AUDIO_OBJECT_TYPE aot,int * delay,int transformFactor,const int headerPeriod,ULONG statesInitFlag)1807 INT sbrEncoder_Init(
1808         HANDLE_SBR_ENCODER   hSbrEncoder,
1809         SBR_ELEMENT_INFO     elInfo[(8)],
1810         int                  noElements,
1811         INT_PCM             *inputBuffer,
1812         INT                 *coreBandwidth,
1813         INT                 *inputBufferOffset,
1814         INT                 *numChannels,
1815         INT                 *coreSampleRate,
1816         UINT                *downSampleFactor,
1817         INT                 *frameLength,
1818         AUDIO_OBJECT_TYPE    aot,
1819         int                 *delay,
1820         int                  transformFactor,
1821         const int            headerPeriod,
1822         ULONG                statesInitFlag
1823         )
1824 {
1825     HANDLE_ERROR_INFO errorInfo = noError;
1826     sbrConfiguration sbrConfig[(8)];
1827     INT error = 0;
1828     INT lowestBandwidth;
1829     /* Save input parameters */
1830     INT inputSampleRate = *coreSampleRate;
1831     int coreFrameLength = *frameLength;
1832     int inputBandWidth = *coreBandwidth;
1833     int inputChannels = *numChannels;
1834 
1835     int downsampledOffset = 0;
1836     int sbrOffset = 0;
1837     int downsamplerDelay = 0;
1838     int timeDomainDownsample = 0;
1839     int nBitstrDelay = 0;
1840     int highestSbrStartFreq, highestSbrStopFreq;
1841     int lowDelay = 0;
1842     int usePs = 0;
1843 
1844     /* check whether SBR setting is available for the current encoder configuration (bitrate, samplerate) */
1845     if (!sbrEncoder_IsSingleRatePossible(aot)) {
1846       *downSampleFactor = 2;
1847     }
1848 
1849 
1850 
1851     if ( (aot==AOT_PS) || (aot==AOT_MP2_PS) || (aot==AOT_DABPLUS_PS) || (aot==AOT_DRM_MPEG_PS) ) {
1852         usePs = 1;
1853     }
1854     if ( (aot==AOT_ER_AAC_ELD) ) {
1855         lowDelay = 1;
1856     }
1857     else if ( (aot==AOT_ER_AAC_LD) ) {
1858         error = 1;
1859         goto bail;
1860     }
1861 
1862     /* Parametric Stereo */
1863     if ( usePs ) {
1864       if ( *numChannels == 2 && noElements == 1) {
1865         /* Override Element type in case of Parametric stereo */
1866         elInfo[0].elType = ID_SCE;
1867         elInfo[0].fParametricStereo = 1;
1868         elInfo[0].nChannelsInEl = 1;
1869         /* core encoder gets downmixed mono signal */
1870         *numChannels  = 1;
1871       } else {
1872         error = 1;
1873         goto bail;
1874       }
1875     } /* usePs */
1876 
1877     /* set the core's sample rate */
1878     switch (*downSampleFactor) {
1879     case 1:
1880       *coreSampleRate = inputSampleRate;
1881       break;
1882     case 2:
1883       *coreSampleRate = inputSampleRate>>1;
1884       break;
1885     default:
1886       *coreSampleRate = inputSampleRate>>1;
1887       return 0; /* return error */
1888     }
1889 
1890     /* check whether SBR setting is available for the current encoder configuration (bitrate, coreSampleRate) */
1891     {
1892       int delayDiff = 0;
1893       int el, coreEl;
1894 
1895       /* Check if every element config is feasible */
1896       for (coreEl=0; coreEl<noElements; coreEl++)
1897       {
1898         /* SBR only handles SCE and CPE's */
1899         if (elInfo[coreEl].elType != ID_SCE && elInfo[coreEl].elType != ID_CPE) {
1900           continue;
1901         }
1902         /* check if desired configuration is available */
1903         if ( !FDKsbrEnc_IsSbrSettingAvail (elInfo[coreEl].bitRate, 0, elInfo[coreEl].nChannelsInEl, inputSampleRate, *coreSampleRate, aot) )
1904         {
1905             error = 1;
1906             goto bail;
1907         }
1908       }
1909 
1910       /* Determine Delay balancing and new encoder delay */
1911       if (lowDelay) {
1912         {
1913         delayDiff = (*delay * *downSampleFactor) + DELAY_ELD2SBR(coreFrameLength,*downSampleFactor);
1914         *delay = DELAY_ELDSBR(coreFrameLength,*downSampleFactor);
1915         }
1916       }
1917       else if (usePs) {
1918         delayDiff = (*delay * *downSampleFactor) + DELAY_AAC2PS(coreFrameLength,*downSampleFactor);
1919         *delay = DELAY_PS(coreFrameLength,*downSampleFactor);
1920       }
1921       else {
1922         delayDiff = DELAY_AAC2SBR(coreFrameLength,*downSampleFactor);
1923         delayDiff += (*delay * *downSampleFactor);
1924         *delay = DELAY_SBR(coreFrameLength,*downSampleFactor);
1925       }
1926 
1927       if (!usePs) {
1928           timeDomainDownsample = *downSampleFactor-1;  /* activate time domain downsampler when downSampleFactor is != 1 */
1929       }
1930 
1931 
1932       /* Take care about downsampled data bound to the SBR path */
1933       if (!timeDomainDownsample && delayDiff > 0) {
1934         /*
1935          * We must tweak the balancing into a situation where the downsampled path
1936          * is the one to be delayed, because delaying the QMF domain input, also delays
1937          * the downsampled audio, counteracting to the purpose of delay balancing.
1938          */
1939         while ( delayDiff > 0 )
1940         {
1941           /* Encoder delay increases */
1942           {
1943             *delay += coreFrameLength * *downSampleFactor;
1944             /* Add one frame delay to SBR path */
1945             delayDiff -= coreFrameLength * *downSampleFactor;
1946           }
1947           nBitstrDelay += 1;
1948         }
1949       } else
1950       {
1951         *delay += fixp_abs(delayDiff);
1952       }
1953 
1954       if (delayDiff < 0) {
1955         /* Delay AAC data */
1956         delayDiff = -delayDiff;
1957         /* Multiply downsampled offset by AAC core channels. Divide by 2 because of half samplerate of downsampled data. */
1958         FDK_ASSERT(*downSampleFactor>0 && *downSampleFactor<=2);
1959         downsampledOffset = (delayDiff*(*numChannels))>>(*downSampleFactor-1);
1960         sbrOffset = 0;
1961       } else {
1962         /* Delay SBR input */
1963         if ( delayDiff > (int)coreFrameLength * (int)*downSampleFactor )
1964         {
1965           /* Do bitstream frame-wise delay balancing if we have more than SBR framelength samples delay difference */
1966           delayDiff -= coreFrameLength * *downSampleFactor;
1967           nBitstrDelay = 1;
1968         }
1969         /* Multiply input offset by input channels */
1970         sbrOffset = delayDiff*(*numChannels);
1971         downsampledOffset = 0;
1972       }
1973       hSbrEncoder->nBitstrDelay            = nBitstrDelay;
1974       hSbrEncoder->nChannels               = *numChannels;
1975       hSbrEncoder->frameSize               = coreFrameLength * *downSampleFactor;
1976       hSbrEncoder->fTimeDomainDownsampling = timeDomainDownsample;
1977       hSbrEncoder->downSampleFactor        = *downSampleFactor;
1978       hSbrEncoder->estimateBitrate         = 0;
1979       hSbrEncoder->inputDataDelay          = 0;
1980 
1981 
1982       /* Open SBR elements */
1983       el = -1;
1984       highestSbrStartFreq = highestSbrStopFreq = 0;
1985       lowestBandwidth = 99999;
1986 
1987       /* Loop through each core encoder element and get a matching SBR element config */
1988       for (coreEl=0; coreEl<noElements; coreEl++)
1989       {
1990         /* SBR only handles SCE and CPE's */
1991         if (elInfo[coreEl].elType == ID_SCE || elInfo[coreEl].elType == ID_CPE) {
1992           el++;
1993         } else {
1994           continue;
1995         }
1996 
1997         /* Set parametric Stereo Flag. */
1998         if (usePs) {
1999           elInfo[coreEl].fParametricStereo = 1;
2000         } else {
2001           elInfo[coreEl].fParametricStereo = 0;
2002         }
2003 
2004         /*
2005          * Init sbrConfig structure
2006          */
2007         if ( ! FDKsbrEnc_InitializeSbrDefaults ( &sbrConfig[el],
2008                                                  *downSampleFactor,
2009                                                   coreFrameLength
2010                                                   ) )
2011         {
2012           error = 1;
2013           goto bail;
2014         }
2015 
2016         /*
2017          * Modify sbrConfig structure according to Element parameters
2018          */
2019         if ( ! FDKsbrEnc_AdjustSbrSettings (&sbrConfig[el],
2020                                              elInfo[coreEl].bitRate,
2021                                              elInfo[coreEl].nChannelsInEl,
2022                                             *coreSampleRate,
2023                                              inputSampleRate,
2024                                              transformFactor,
2025                                              24000,
2026                                              0,
2027                                              0,     /* useSpeechConfig */
2028                                              0,     /* lcsMode */
2029                                              usePs, /* bParametricStereo */
2030                                              aot) )
2031         {
2032           error = 1;
2033           goto bail;
2034         }
2035 
2036         /* Find common frequency border for all SBR elements */
2037         highestSbrStartFreq = fixMax(highestSbrStartFreq, sbrConfig[el].startFreq);
2038         highestSbrStopFreq = fixMax(highestSbrStopFreq, sbrConfig[el].stopFreq);
2039 
2040       } /* first element loop */
2041 
2042       /* Set element count (can be less than core encoder element count) */
2043       hSbrEncoder->noElements = el+1;
2044 
2045       FDKsbrEnc_Reallocate(hSbrEncoder,
2046                            elInfo,
2047                            noElements);
2048 
2049       for (el=0; el<hSbrEncoder->noElements; el++) {
2050 
2051         int bandwidth = *coreBandwidth;
2052 
2053         /* Use lowest common bandwidth */
2054         sbrConfig[el].startFreq = highestSbrStartFreq;
2055         sbrConfig[el].stopFreq = highestSbrStopFreq;
2056 
2057         /* initialize SBR element, and get core bandwidth */
2058         error = FDKsbrEnc_EnvInit(hSbrEncoder->sbrElement[el],
2059                                   &sbrConfig[el],
2060                                   &bandwidth,
2061                                    aot,
2062                                    nBitstrDelay,
2063                                    el,
2064                                    headerPeriod,
2065                                    statesInitFlag,
2066                                    hSbrEncoder->fTimeDomainDownsampling
2067                                   ,hSbrEncoder->dynamicRam
2068                                   );
2069 
2070         if (error != 0) {
2071           error = 2;
2072           goto bail;
2073         }
2074 
2075         /* Get lowest core encoder bandwidth to be returned later. */
2076         lowestBandwidth = fixMin(lowestBandwidth, bandwidth);
2077 
2078       } /* second element loop */
2079 
2080       /* Initialize a downsampler for each channel in each SBR element */
2081       if (hSbrEncoder->fTimeDomainDownsampling)
2082       {
2083         for (el=0; el<hSbrEncoder->noElements; el++)
2084         {
2085           HANDLE_SBR_ELEMENT hSbrEl = hSbrEncoder->sbrElement[el];
2086           INT Wc, ch;
2087 
2088           /* Calculated required normalized cutoff frequency (Wc = 1.0 -> lowestBandwidth = inputSampleRate/2) */
2089           Wc = (2*lowestBandwidth)*1000 / inputSampleRate;
2090 
2091           for (ch=0; ch<hSbrEl->elInfo.nChannelsInEl; ch++)
2092           {
2093             FDKaacEnc_InitDownsampler (&hSbrEl->sbrChannel[ch]->downSampler, Wc, *downSampleFactor);
2094             FDK_ASSERT (hSbrEl->sbrChannel[ch]->downSampler.delay <=MAX_DS_FILTER_DELAY);
2095           }
2096 
2097           downsamplerDelay = hSbrEl->sbrChannel[0]->downSampler.delay;
2098         } /* third element loop */
2099 
2100         /* lfe */
2101         FDKaacEnc_InitDownsampler (&hSbrEncoder->lfeDownSampler, 0, *downSampleFactor);
2102 
2103         /* Add the resampler additional delay to get the final delay and buffer offset values. */
2104         if (sbrOffset > 0 || downsampledOffset <= ((downsamplerDelay * (*numChannels))>>(*downSampleFactor-1))) {
2105           sbrOffset += (downsamplerDelay - downsampledOffset) * (*numChannels) ;
2106           *delay += downsamplerDelay - downsampledOffset;
2107           downsampledOffset = 0;
2108         } else {
2109           downsampledOffset -= (downsamplerDelay * (*numChannels))>>(*downSampleFactor-1);
2110           sbrOffset = 0;
2111         }
2112 
2113         hSbrEncoder->inputDataDelay = downsamplerDelay;
2114       }
2115 
2116       /* Assign core encoder Bandwidth */
2117       *coreBandwidth = lowestBandwidth;
2118 
2119       /* Estimate sbr bitrate, 2.5 kBit/s per sbr channel */
2120       hSbrEncoder->estimateBitrate += 2500 * (*numChannels);
2121 
2122       /* initialize parametric stereo */
2123       if (usePs)
2124       {
2125         PSENC_CONFIG psEncConfig;
2126         FDK_ASSERT(hSbrEncoder->noElements == 1);
2127         INT psTuningTableIdx = getPsTuningTableIndex(elInfo[0].bitRate, NULL);
2128 
2129         psEncConfig.frameSize           = coreFrameLength; //sbrConfig.sbrFrameSize;
2130         psEncConfig.qmfFilterMode       = 0;
2131         psEncConfig.sbrPsDelay          = 0;
2132 
2133         /* tuning parameters */
2134         if (psTuningTableIdx  != INVALID_TABLE_IDX) {
2135           psEncConfig.nStereoBands           = psTuningTable[psTuningTableIdx].nStereoBands;
2136           psEncConfig.maxEnvelopes           = psTuningTable[psTuningTableIdx].nEnvelopes;
2137           psEncConfig.iidQuantErrorThreshold = (FIXP_DBL)psTuningTable[psTuningTableIdx].iidQuantErrorThreshold;
2138 
2139           /* calculation is not quite linear, increased number of envelopes causes more bits */
2140           /* assume avg. 50 bits per frame for 10 stereo bands / 1 envelope configuration */
2141           hSbrEncoder->estimateBitrate += ( (((*coreSampleRate) * 5 * psEncConfig.nStereoBands * psEncConfig.maxEnvelopes) / hSbrEncoder->frameSize));
2142 
2143         } else {
2144           error = ERROR(CDI, "Invalid ps tuning table index.");
2145           goto bail;
2146         }
2147 
2148         qmfInitSynthesisFilterBank(&hSbrEncoder->qmfSynthesisPS,
2149                                     (FIXP_DBL*)hSbrEncoder->qmfSynthesisPS.FilterStates,
2150                                     hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfSlots,
2151                                     hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands>>1,
2152                                     hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands>>1,
2153                                     hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands>>1,
2154                                     (statesInitFlag) ? 0 : QMF_FLAG_KEEP_STATES);
2155 
2156         if(errorInfo == noError){
2157           /* update delay */
2158           psEncConfig.sbrPsDelay = FDKsbrEnc_GetEnvEstDelay(&hSbrEncoder->sbrElement[0]->sbrChannel[0]->hEnvChannel.sbrExtractEnvelope);
2159 
2160           if(noError != (errorInfo = PSEnc_Init( hSbrEncoder->hParametricStereo,
2161                                                 &psEncConfig,
2162                                                  hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfSlots,
2163                                                  hSbrEncoder->sbrElement[0]->sbrConfigData.noQmfBands
2164                                                 ,hSbrEncoder->dynamicRam
2165                                                  )))
2166           {
2167             errorInfo = handBack(errorInfo);
2168           }
2169         }
2170 
2171         /* QMF analysis + Hybrid analysis + Hybrid synthesis + QMF synthesis + downsampled input buffer delay */
2172         hSbrEncoder->inputDataDelay = (64*10/2) + (6*64) +  (0) + (64*10/2-64+1) + ((*downSampleFactor)*downsampledOffset);
2173       }
2174 
2175       hSbrEncoder->downsampledOffset = downsampledOffset;
2176       {
2177         hSbrEncoder->downmixSize = coreFrameLength*(*numChannels);
2178       }
2179 
2180       hSbrEncoder->bufferOffset = sbrOffset;
2181       /* Delay Compensation: fill bitstream delay buffer with zero input signal */
2182       if ( hSbrEncoder->nBitstrDelay > 0 )
2183       {
2184         error = FDKsbrEnc_DelayCompensation (hSbrEncoder, inputBuffer);
2185         if (error != 0)
2186           goto bail;
2187       }
2188 
2189       /* Set Output frame length */
2190       *frameLength = coreFrameLength * *downSampleFactor;
2191       /* Input buffer offset */
2192       *inputBufferOffset = fixMax(sbrOffset, downsampledOffset);
2193 
2194 
2195     }
2196 
2197     return error;
2198 
2199 bail:
2200     /* Restore input settings */
2201     *coreSampleRate = inputSampleRate;
2202     *frameLength = coreFrameLength;
2203     *numChannels = inputChannels;
2204     *coreBandwidth = inputBandWidth;
2205 
2206     return error;
2207  }
2208 
2209 
2210 INT
sbrEncoder_EncodeFrame(HANDLE_SBR_ENCODER hSbrEncoder,INT_PCM * samples,UINT timeInStride,UINT sbrDataBits[(8)],UCHAR sbrData[(8)][MAX_PAYLOAD_SIZE])2211 sbrEncoder_EncodeFrame(  HANDLE_SBR_ENCODER   hSbrEncoder,
2212                          INT_PCM             *samples,
2213                          UINT                 timeInStride,
2214                          UINT                 sbrDataBits[(8)],
2215                          UCHAR                sbrData[(8)][MAX_PAYLOAD_SIZE]
2216                         )
2217 {
2218   INT error;
2219   int el;
2220 
2221   for (el=0; el<hSbrEncoder->noElements; el++)
2222   {
2223     if (hSbrEncoder->sbrElement[el] != NULL)
2224     {
2225       error = FDKsbrEnc_EnvEncodeFrame(
2226                                         hSbrEncoder,
2227                                         el,
2228                                         samples + hSbrEncoder->downsampledOffset,
2229                                         timeInStride,
2230                                        &sbrDataBits[el],
2231                                         sbrData[el],
2232                                         0
2233                                        );
2234       if (error)
2235         return error;
2236     }
2237   }
2238 
2239   if ( ( hSbrEncoder->lfeChIdx!=-1) && (hSbrEncoder->downSampleFactor > 1) )
2240   {   /* lfe downsampler */
2241       INT nOutSamples;
2242 
2243       FDKaacEnc_Downsample(&hSbrEncoder->lfeDownSampler,
2244                             samples + hSbrEncoder->downsampledOffset + hSbrEncoder->bufferOffset + hSbrEncoder->lfeChIdx,
2245                             hSbrEncoder->frameSize,
2246                             timeInStride,
2247                             samples + hSbrEncoder->downsampledOffset + hSbrEncoder->lfeChIdx,
2248                            &nOutSamples,
2249                             hSbrEncoder->nChannels);
2250 
2251 
2252   }
2253 
2254   return 0;
2255 }
2256 
2257 
sbrEncoder_UpdateBuffers(HANDLE_SBR_ENCODER hSbrEncoder,INT_PCM * timeBuffer)2258 INT sbrEncoder_UpdateBuffers(
2259                             HANDLE_SBR_ENCODER hSbrEncoder,
2260                             INT_PCM *timeBuffer
2261                             )
2262  {
2263     if ( hSbrEncoder->downsampledOffset > 0 ) {
2264       /* Move delayed downsampled data */
2265       FDKmemcpy ( timeBuffer,
2266                   timeBuffer + hSbrEncoder->downmixSize,
2267                   sizeof(INT_PCM) * (hSbrEncoder->downsampledOffset) );
2268     } else {
2269       /* Move delayed input data */
2270       FDKmemcpy ( timeBuffer,
2271         timeBuffer + hSbrEncoder->nChannels * hSbrEncoder->frameSize,
2272                   sizeof(INT_PCM) * hSbrEncoder->bufferOffset );
2273     }
2274     if ( hSbrEncoder->nBitstrDelay > 0  )
2275     {
2276       int el;
2277 
2278       for (el=0; el<hSbrEncoder->noElements; el++)
2279       {
2280         FDKmemmove ( hSbrEncoder->sbrElement[el]->payloadDelayLine[0],
2281                      hSbrEncoder->sbrElement[el]->payloadDelayLine[1],
2282                      sizeof(UCHAR) * (hSbrEncoder->nBitstrDelay*MAX_PAYLOAD_SIZE) );
2283 
2284         FDKmemmove( &hSbrEncoder->sbrElement[el]->payloadDelayLineSize[0],
2285                     &hSbrEncoder->sbrElement[el]->payloadDelayLineSize[1],
2286                     sizeof(UINT) * (hSbrEncoder->nBitstrDelay) );
2287       }
2288     }
2289     return 0;
2290  }
2291 
2292 
sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder)2293 INT sbrEncoder_GetEstimateBitrate(HANDLE_SBR_ENCODER hSbrEncoder)
2294 {
2295   INT estimateBitrate = 0;
2296 
2297   if(hSbrEncoder) {
2298     estimateBitrate += hSbrEncoder->estimateBitrate;
2299   }
2300 
2301   return estimateBitrate;
2302 }
2303 
sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder)2304 INT sbrEncoder_GetInputDataDelay(HANDLE_SBR_ENCODER hSbrEncoder)
2305 {
2306   INT delay = -1;
2307 
2308   if(hSbrEncoder) {
2309     delay = hSbrEncoder->inputDataDelay;
2310   }
2311   return delay;
2312 }
2313 
2314 
sbrEncoder_GetLibInfo(LIB_INFO * info)2315 INT sbrEncoder_GetLibInfo( LIB_INFO *info )
2316 {
2317   int i;
2318 
2319   if (info == NULL) {
2320     return -1;
2321   }
2322   /* search for next free tab */
2323   for (i = 0; i < FDK_MODULE_LAST; i++) {
2324     if (info[i].module_id == FDK_NONE) break;
2325   }
2326   if (i == FDK_MODULE_LAST) {
2327     return -1;
2328   }
2329   info += i;
2330 
2331   info->module_id = FDK_SBRENC;
2332   info->version = LIB_VERSION(SBRENCODER_LIB_VL0, SBRENCODER_LIB_VL1, SBRENCODER_LIB_VL2);
2333   LIB_VERSION_STRING(info);
2334   info->build_date = __DATE__;
2335   info->build_time = __TIME__;
2336   info->title = "SBR Encoder";
2337 
2338   /* Set flags */
2339   info->flags = 0
2340     | CAPF_SBR_HQ
2341     | CAPF_SBR_PS_MPEG
2342     ;
2343   /* End of flags */
2344 
2345   return 0;
2346 }
2347