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