Home | History | Annotate | Download | only in src
      1 /* -----------------------------------------------------------------------------
      2 Software License for The Fraunhofer FDK AAC Codec Library for Android
      3 
      4  Copyright  1995 - 2018 Fraunhofer-Gesellschaft zur Frderung der angewandten
      5 Forschung e.V. All rights reserved.
      6 
      7  1.    INTRODUCTION
      8 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software
      9 that implements the MPEG Advanced Audio Coding ("AAC") encoding and decoding
     10 scheme for digital audio. This FDK AAC Codec software is intended to be used on
     11 a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient
     14 general perceptual audio codecs. AAC-ELD is considered the best-performing
     15 full-bandwidth communications codec by independent studies and is widely
     16 deployed. AAC has been standardized by ISO and IEC as part of the MPEG
     17 specifications.
     18 
     19 Patent licenses for necessary patent claims for the FDK AAC Codec (including
     20 those of Fraunhofer) may be obtained through Via Licensing
     21 (www.vialicensing.com) or through the respective patent owners individually for
     22 the purpose of encoding or decoding bit streams in products that are compliant
     23 with the ISO/IEC MPEG audio standards. Please note that most manufacturers of
     24 Android devices already license these patent claims through Via Licensing or
     25 directly from the patent owners, and therefore FDK AAC Codec software may
     26 already be covered under those patent licenses when it is used for those
     27 licensed purposes only.
     28 
     29 Commercially-licensed AAC software libraries, including floating-point versions
     30 with enhanced sound quality, are also available from Fraunhofer. Users are
     31 encouraged to check the Fraunhofer website for additional applications
     32 information and documentation.
     33 
     34 2.    COPYRIGHT LICENSE
     35 
     36 Redistribution and use in source and binary forms, with or without modification,
     37 are permitted without payment of copyright license fees provided that you
     38 satisfy the following conditions:
     39 
     40 You must retain the complete text of this software license in redistributions of
     41 the FDK AAC Codec or your modifications thereto in source code form.
     42 
     43 You must retain the complete text of this software license in the documentation
     44 and/or other materials provided with redistributions of the FDK AAC Codec or
     45 your modifications thereto in binary form. You must make available free of
     46 charge copies of the complete source code of the FDK AAC Codec and your
     47 modifications thereto to recipients of copies in binary form.
     48 
     49 The name of Fraunhofer may not be used to endorse or promote products derived
     50 from this library without prior written permission.
     51 
     52 You may not charge copyright license fees for anyone to use, copy or distribute
     53 the FDK AAC Codec software or your modifications thereto.
     54 
     55 Your modified versions of the FDK AAC Codec must carry prominent notices stating
     56 that you changed the software and the date of any change. For modified versions
     57 of the FDK AAC Codec, the term "Fraunhofer FDK AAC Codec Library for Android"
     58 must be replaced by the term "Third-Party Modified Version of the Fraunhofer FDK
     59 AAC Codec Library for Android."
     60 
     61 3.    NO PATENT LICENSE
     62 
     63 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without
     64 limitation the patents of Fraunhofer, ARE GRANTED BY THIS SOFTWARE LICENSE.
     65 Fraunhofer provides no warranty of patent non-infringement with respect to this
     66 software.
     67 
     68 You may use this FDK AAC Codec software or modifications thereto only for
     69 purposes that are authorized by appropriate patent licenses.
     70 
     71 4.    DISCLAIMER
     72 
     73 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright
     74 holders and contributors "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
     75 including but not limited to the implied warranties of merchantability and
     76 fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     77 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary,
     78 or consequential damages, including but not limited to procurement of substitute
     79 goods or services; loss of use, data, or profits, or business interruption,
     80 however caused and on any theory of liability, whether in contract, strict
     81 liability, or tort (including negligence), arising in any way out of the use of
     82 this software, even if advised of the possibility of such damage.
     83 
     84 5.    CONTACT INFORMATION
     85 
     86 Fraunhofer Institute for Integrated Circuits IIS
     87 Attention: Audio and Multimedia Departments - FDK AAC LL
     88 Am Wolfsmantel 33
     89 91058 Erlangen, Germany
     90 
     91 www.iis.fraunhofer.de/amm
     92 amm-info (at) iis.fraunhofer.de
     93 ----------------------------------------------------------------------------- */
     94 
     95 /**************************** SBR decoder library ******************************
     96 
     97    Author(s):
     98 
     99    Description:
    100 
    101 *******************************************************************************/
    102 
    103 /*!
    104   \file
    105   \brief  Low Power Profile Transposer
    106   This module provides the transposer. The main entry point is lppTransposer().
    107   The function generates high frequency content by copying data from the low
    108   band (provided by core codec) into the high band. This process is also
    109   referred to as "patching". The function also implements spectral whitening by
    110   means of inverse filtering based on LPC coefficients.
    111 
    112   Together with the QMF filterbank the transposer can be tested using a supplied
    113   test program. See main_audio.cpp for details. This module does use fractional
    114   arithmetic and the accuracy of the computations has an impact on the overall
    115   sound quality. The module also needs to take into account the different
    116   scaling of spectral data.
    117 
    118   \sa lppTransposer(), main_audio.cpp, sbr_scale.h, \ref documentationOverview
    119 */
    120 
    121 #include "lpp_tran.h"
    122 
    123 #include "sbr_ram.h"
    124 #include "sbr_rom.h"
    125 
    126 #include "genericStds.h"
    127 #include "autocorr2nd.h"
    128 
    129 #include "HFgen_preFlat.h"
    130 
    131 #if defined(__arm__)
    132 #include "arm/lpp_tran_arm.cpp"
    133 #endif
    134 
    135 #define LPC_SCALE_FACTOR 2
    136 
    137 /*!
    138  *
    139  * \brief Get bandwidth expansion factor from filtering level
    140  *
    141  * Returns a filter parameter (bandwidth expansion factor) depending on
    142  * the desired filtering level signalled in the bitstream.
    143  * When switching the filtering level from LOW to OFF, an additional
    144  * level is being inserted to achieve a smooth transition.
    145  */
    146 
    147 static FIXP_DBL mapInvfMode(INVF_MODE mode, INVF_MODE prevMode,
    148                             WHITENING_FACTORS whFactors) {
    149   switch (mode) {
    150     case INVF_LOW_LEVEL:
    151       if (prevMode == INVF_OFF)
    152         return whFactors.transitionLevel;
    153       else
    154         return whFactors.lowLevel;
    155 
    156     case INVF_MID_LEVEL:
    157       return whFactors.midLevel;
    158 
    159     case INVF_HIGH_LEVEL:
    160       return whFactors.highLevel;
    161 
    162     default:
    163       if (prevMode == INVF_LOW_LEVEL)
    164         return whFactors.transitionLevel;
    165       else
    166         return whFactors.off;
    167   }
    168 }
    169 
    170 /*!
    171  *
    172  * \brief Perform inverse filtering level emphasis
    173  *
    174  * Retrieve bandwidth expansion factor and apply smoothing for each filter band
    175  *
    176  */
    177 
    178 static void inverseFilteringLevelEmphasis(
    179     HANDLE_SBR_LPP_TRANS hLppTrans, /*!< Handle of lpp transposer  */
    180     UCHAR nInvfBands,              /*!< Number of bands for inverse filtering */
    181     INVF_MODE *sbr_invf_mode,      /*!< Current inverse filtering modes */
    182     INVF_MODE *sbr_invf_mode_prev, /*!< Previous inverse filtering modes */
    183     FIXP_DBL *bwVector             /*!< Resulting filtering levels */
    184 ) {
    185   for (int i = 0; i < nInvfBands; i++) {
    186     FIXP_DBL accu;
    187     FIXP_DBL bwTmp = mapInvfMode(sbr_invf_mode[i], sbr_invf_mode_prev[i],
    188                                  hLppTrans->pSettings->whFactors);
    189 
    190     if (bwTmp < hLppTrans->bwVectorOld[i]) {
    191       accu = fMultDiv2(FL2FXCONST_DBL(0.75f), bwTmp) +
    192              fMultDiv2(FL2FXCONST_DBL(0.25f), hLppTrans->bwVectorOld[i]);
    193     } else {
    194       accu = fMultDiv2(FL2FXCONST_DBL(0.90625f), bwTmp) +
    195              fMultDiv2(FL2FXCONST_DBL(0.09375f), hLppTrans->bwVectorOld[i]);
    196     }
    197 
    198     if (accu<FL2FXCONST_DBL(0.015625f)>> 1) {
    199       bwVector[i] = FL2FXCONST_DBL(0.0f);
    200     } else {
    201       bwVector[i] = fixMin(accu << 1, FL2FXCONST_DBL(0.99609375f));
    202     }
    203   }
    204 }
    205 
    206 /* Resulting autocorrelation determinant exponent */
    207 #define ACDET_EXP \
    208   (2 * (DFRACT_BITS + sbrScaleFactor->lb_scale + 10 - ac.det_scale))
    209 #define AC_EXP (-sbrScaleFactor->lb_scale + LPC_SCALE_FACTOR)
    210 #define ALPHA_EXP (-sbrScaleFactor->lb_scale + LPC_SCALE_FACTOR + 1)
    211 /* Resulting transposed QMF values exponent 16 bit normalized samplebits
    212  * assumed. */
    213 #define QMFOUT_EXP ((SAMPLE_BITS - 15) - sbrScaleFactor->lb_scale)
    214 
    215 static inline void calc_qmfBufferReal(FIXP_DBL **qmfBufferReal,
    216                                       const FIXP_DBL *const lowBandReal,
    217                                       const int startSample,
    218                                       const int stopSample, const UCHAR hiBand,
    219                                       const int dynamicScale, const int descale,
    220                                       const FIXP_SGL a0r, const FIXP_SGL a1r) {
    221   FIXP_DBL accu1, accu2;
    222   int i;
    223 
    224   for (i = 0; i < stopSample - startSample; i++) {
    225     accu1 = fMultDiv2(a1r, lowBandReal[i]);
    226     accu1 = (fMultDiv2(a0r, lowBandReal[i + 1]) + accu1);
    227     accu1 = accu1 >> dynamicScale;
    228 
    229     accu1 <<= 1;
    230     accu2 = (lowBandReal[i + 2] >> descale);
    231     qmfBufferReal[i + startSample][hiBand] = accu1 + accu2;
    232   }
    233 }
    234 
    235 /*!
    236  *
    237  * \brief Perform transposition by patching of subband samples.
    238  * This function serves as the main entry point into the module. The function
    239  * determines the areas for the patching process (these are the source range as
    240  * well as the target range) and implements spectral whitening by means of
    241  * inverse filtering. The function autoCorrelation2nd() is an auxiliary function
    242  * for calculating the LPC coefficients for the filtering.  The actual
    243  * calculation of the LPC coefficients and the implementation of the filtering
    244  * are done as part of lppTransposer().
    245  *
    246  * Note that the filtering is done on all available QMF subsamples, whereas the
    247  * patching is only done on those QMF subsamples that will be used in the next
    248  * QMF synthesis. The filtering is also implemented before the patching includes
    249  * further dependencies on parameters from the SBR data.
    250  *
    251  */
    252 
    253 void lppTransposer(
    254     HANDLE_SBR_LPP_TRANS hLppTrans,   /*!< Handle of lpp transposer  */
    255     QMF_SCALE_FACTOR *sbrScaleFactor, /*!< Scaling factors */
    256     FIXP_DBL **qmfBufferReal, /*!< Pointer to pointer to real part of subband
    257                                  samples (source) */
    258 
    259     FIXP_DBL *degreeAlias,    /*!< Vector for results of aliasing estimation */
    260     FIXP_DBL **qmfBufferImag, /*!< Pointer to pointer to imaginary part of
    261                                  subband samples (source) */
    262     const int useLP, const int fPreWhitening, const int v_k_master0,
    263     const int timeStep,       /*!< Time step of envelope */
    264     const int firstSlotOffs,  /*!< Start position in time */
    265     const int lastSlotOffs,   /*!< Number of overlap-slots into next frame */
    266     const int nInvfBands,     /*!< Number of bands for inverse filtering */
    267     INVF_MODE *sbr_invf_mode, /*!< Current inverse filtering modes */
    268     INVF_MODE *sbr_invf_mode_prev /*!< Previous inverse filtering modes */
    269 ) {
    270   INT bwIndex[MAX_NUM_PATCHES];
    271   FIXP_DBL bwVector[MAX_NUM_PATCHES]; /*!< pole moving factors */
    272   FIXP_DBL preWhiteningGains[(64) / 2];
    273   int preWhiteningGains_exp[(64) / 2];
    274 
    275   int i;
    276   int loBand, start, stop;
    277   TRANSPOSER_SETTINGS *pSettings = hLppTrans->pSettings;
    278   PATCH_PARAM *patchParam = pSettings->patchParam;
    279   int patch;
    280 
    281   FIXP_SGL alphar[LPC_ORDER], a0r, a1r;
    282   FIXP_SGL alphai[LPC_ORDER], a0i = 0, a1i = 0;
    283   FIXP_SGL bw = FL2FXCONST_SGL(0.0f);
    284 
    285   int autoCorrLength;
    286 
    287   FIXP_DBL k1, k1_below = 0, k1_below2 = 0;
    288 
    289   ACORR_COEFS ac;
    290   int startSample;
    291   int stopSample;
    292   int stopSampleClear;
    293 
    294   int comLowBandScale;
    295   int ovLowBandShift;
    296   int lowBandShift;
    297   /*  int ovHighBandShift;*/
    298   int targetStopBand;
    299 
    300   alphai[0] = FL2FXCONST_SGL(0.0f);
    301   alphai[1] = FL2FXCONST_SGL(0.0f);
    302 
    303   startSample = firstSlotOffs * timeStep;
    304   stopSample = pSettings->nCols + lastSlotOffs * timeStep;
    305   FDK_ASSERT((lastSlotOffs * timeStep) <= pSettings->overlap);
    306 
    307   inverseFilteringLevelEmphasis(hLppTrans, nInvfBands, sbr_invf_mode,
    308                                 sbr_invf_mode_prev, bwVector);
    309 
    310   stopSampleClear = stopSample;
    311 
    312   autoCorrLength = pSettings->nCols + pSettings->overlap;
    313 
    314   /* Set upper subbands to zero:
    315      This is required in case that the patches do not cover the complete
    316      highband (because the last patch would be too short). Possible
    317      optimization: Clearing bands up to usb would be sufficient here. */
    318   targetStopBand = patchParam[pSettings->noOfPatches - 1].targetStartBand +
    319                    patchParam[pSettings->noOfPatches - 1].numBandsInPatch;
    320 
    321   int memSize = ((64) - targetStopBand) * sizeof(FIXP_DBL);
    322 
    323   if (!useLP) {
    324     for (i = startSample; i < stopSampleClear; i++) {
    325       FDKmemclear(&qmfBufferReal[i][targetStopBand], memSize);
    326       FDKmemclear(&qmfBufferImag[i][targetStopBand], memSize);
    327     }
    328   } else {
    329     for (i = startSample; i < stopSampleClear; i++) {
    330       FDKmemclear(&qmfBufferReal[i][targetStopBand], memSize);
    331     }
    332   }
    333 
    334   /* init bwIndex for each patch */
    335   FDKmemclear(bwIndex, sizeof(bwIndex));
    336 
    337   /*
    338     Calc common low band scale factor
    339   */
    340   comLowBandScale =
    341       fixMin(sbrScaleFactor->ov_lb_scale, sbrScaleFactor->lb_scale);
    342 
    343   ovLowBandShift = sbrScaleFactor->ov_lb_scale - comLowBandScale;
    344   lowBandShift = sbrScaleFactor->lb_scale - comLowBandScale;
    345   /*  ovHighBandShift = firstSlotOffs == 0 ? ovLowBandShift:0;*/
    346 
    347   if (fPreWhitening) {
    348     sbrDecoder_calculateGainVec(
    349         qmfBufferReal, qmfBufferImag,
    350         DFRACT_BITS - 1 - 16 -
    351             sbrScaleFactor->ov_lb_scale, /* convert scale to exponent */
    352         DFRACT_BITS - 1 - 16 -
    353             sbrScaleFactor->lb_scale, /* convert scale to exponent */
    354         pSettings->overlap, preWhiteningGains, preWhiteningGains_exp,
    355         v_k_master0, startSample, stopSample);
    356   }
    357 
    358   /* outer loop over bands to do analysis only once for each band */
    359 
    360   if (!useLP) {
    361     start = pSettings->lbStartPatching;
    362     stop = pSettings->lbStopPatching;
    363   } else {
    364     start = fixMax(1, pSettings->lbStartPatching - 2);
    365     stop = patchParam[0].targetStartBand;
    366   }
    367 
    368   for (loBand = start; loBand < stop; loBand++) {
    369     FIXP_DBL lowBandReal[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
    370     FIXP_DBL *plowBandReal = lowBandReal;
    371     FIXP_DBL **pqmfBufferReal =
    372         qmfBufferReal + firstSlotOffs * timeStep /* + pSettings->overlap */;
    373     FIXP_DBL lowBandImag[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
    374     FIXP_DBL *plowBandImag = lowBandImag;
    375     FIXP_DBL **pqmfBufferImag =
    376         qmfBufferImag + firstSlotOffs * timeStep /* + pSettings->overlap */;
    377     int resetLPCCoeffs = 0;
    378     int dynamicScale = DFRACT_BITS - 1 - LPC_SCALE_FACTOR;
    379     int acDetScale = 0; /* scaling of autocorrelation determinant */
    380 
    381     for (i = 0;
    382          i < LPC_ORDER + firstSlotOffs * timeStep /*+pSettings->overlap*/;
    383          i++) {
    384       *plowBandReal++ = hLppTrans->lpcFilterStatesRealLegSBR[i][loBand];
    385       if (!useLP)
    386         *plowBandImag++ = hLppTrans->lpcFilterStatesImagLegSBR[i][loBand];
    387     }
    388 
    389     /*
    390       Take old slope length qmf slot source values out of (overlap)qmf buffer
    391     */
    392     if (!useLP) {
    393       for (i = 0;
    394            i < pSettings->nCols + pSettings->overlap - firstSlotOffs * timeStep;
    395            i++) {
    396         *plowBandReal++ = (*pqmfBufferReal++)[loBand];
    397         *plowBandImag++ = (*pqmfBufferImag++)[loBand];
    398       }
    399     } else {
    400       /* pSettings->overlap is always even */
    401       FDK_ASSERT((pSettings->overlap & 1) == 0);
    402       for (i = 0; i < ((pSettings->nCols + pSettings->overlap -
    403                         firstSlotOffs * timeStep) >>
    404                        1);
    405            i++) {
    406         *plowBandReal++ = (*pqmfBufferReal++)[loBand];
    407         *plowBandReal++ = (*pqmfBufferReal++)[loBand];
    408       }
    409       if (pSettings->nCols & 1) {
    410         *plowBandReal++ = (*pqmfBufferReal++)[loBand];
    411       }
    412     }
    413 
    414     /*
    415       Determine dynamic scaling value.
    416      */
    417     dynamicScale =
    418         fixMin(dynamicScale,
    419                getScalefactor(lowBandReal, LPC_ORDER + pSettings->overlap) +
    420                    ovLowBandShift);
    421     dynamicScale =
    422         fixMin(dynamicScale,
    423                getScalefactor(&lowBandReal[LPC_ORDER + pSettings->overlap],
    424                               pSettings->nCols) +
    425                    lowBandShift);
    426     if (!useLP) {
    427       dynamicScale =
    428           fixMin(dynamicScale,
    429                  getScalefactor(lowBandImag, LPC_ORDER + pSettings->overlap) +
    430                      ovLowBandShift);
    431       dynamicScale =
    432           fixMin(dynamicScale,
    433                  getScalefactor(&lowBandImag[LPC_ORDER + pSettings->overlap],
    434                                 pSettings->nCols) +
    435                      lowBandShift);
    436     }
    437     dynamicScale = fixMax(
    438         0, dynamicScale - 1); /* one additional bit headroom to prevent -1.0 */
    439 
    440     /*
    441       Scale temporal QMF buffer.
    442      */
    443     scaleValues(&lowBandReal[0], LPC_ORDER + pSettings->overlap,
    444                 dynamicScale - ovLowBandShift);
    445     scaleValues(&lowBandReal[LPC_ORDER + pSettings->overlap], pSettings->nCols,
    446                 dynamicScale - lowBandShift);
    447 
    448     if (!useLP) {
    449       scaleValues(&lowBandImag[0], LPC_ORDER + pSettings->overlap,
    450                   dynamicScale - ovLowBandShift);
    451       scaleValues(&lowBandImag[LPC_ORDER + pSettings->overlap],
    452                   pSettings->nCols, dynamicScale - lowBandShift);
    453     }
    454 
    455     if (!useLP) {
    456       acDetScale += autoCorr2nd_cplx(&ac, lowBandReal + LPC_ORDER,
    457                                      lowBandImag + LPC_ORDER, autoCorrLength);
    458     } else {
    459       acDetScale +=
    460           autoCorr2nd_real(&ac, lowBandReal + LPC_ORDER, autoCorrLength);
    461     }
    462 
    463     /* Examine dynamic of determinant in autocorrelation. */
    464     acDetScale += 2 * (comLowBandScale + dynamicScale);
    465     acDetScale *= 2;            /* two times reflection coefficent scaling */
    466     acDetScale += ac.det_scale; /* ac scaling of determinant */
    467 
    468     /* In case of determinant < 10^-38, resetLPCCoeffs=1 has to be enforced. */
    469     if (acDetScale > 126) {
    470       resetLPCCoeffs = 1;
    471     }
    472 
    473     alphar[1] = FL2FXCONST_SGL(0.0f);
    474     if (!useLP) alphai[1] = FL2FXCONST_SGL(0.0f);
    475 
    476     if (ac.det != FL2FXCONST_DBL(0.0f)) {
    477       FIXP_DBL tmp, absTmp, absDet;
    478 
    479       absDet = fixp_abs(ac.det);
    480 
    481       if (!useLP) {
    482         tmp = (fMultDiv2(ac.r01r, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) -
    483               ((fMultDiv2(ac.r01i, ac.r12i) + fMultDiv2(ac.r02r, ac.r11r)) >>
    484                (LPC_SCALE_FACTOR - 1));
    485       } else {
    486         tmp = (fMultDiv2(ac.r01r, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) -
    487               (fMultDiv2(ac.r02r, ac.r11r) >> (LPC_SCALE_FACTOR - 1));
    488       }
    489       absTmp = fixp_abs(tmp);
    490 
    491       /*
    492         Quick check: is first filter coeff >= 1(4)
    493        */
    494       {
    495         INT scale;
    496         FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
    497         scale = scale + ac.det_scale;
    498 
    499         if ((scale > 0) && (result >= (FIXP_DBL)MAXVAL_DBL >> scale)) {
    500           resetLPCCoeffs = 1;
    501         } else {
    502           alphar[1] = FX_DBL2FX_SGL(scaleValue(result, scale));
    503           if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
    504             alphar[1] = -alphar[1];
    505           }
    506         }
    507       }
    508 
    509       if (!useLP) {
    510         tmp = (fMultDiv2(ac.r01i, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) +
    511               ((fMultDiv2(ac.r01r, ac.r12i) -
    512                 (FIXP_DBL)fMultDiv2(ac.r02i, ac.r11r)) >>
    513                (LPC_SCALE_FACTOR - 1));
    514 
    515         absTmp = fixp_abs(tmp);
    516 
    517         /*
    518         Quick check: is second filter coeff >= 1(4)
    519         */
    520         {
    521           INT scale;
    522           FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
    523           scale = scale + ac.det_scale;
    524 
    525           if ((scale > 0) &&
    526               (result >= /*FL2FXCONST_DBL(1.f)*/ (FIXP_DBL)MAXVAL_DBL >>
    527                scale)) {
    528             resetLPCCoeffs = 1;
    529           } else {
    530             alphai[1] = FX_DBL2FX_SGL(scaleValue(result, scale));
    531             if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
    532               alphai[1] = -alphai[1];
    533             }
    534           }
    535         }
    536       }
    537     }
    538 
    539     alphar[0] = FL2FXCONST_SGL(0.0f);
    540     if (!useLP) alphai[0] = FL2FXCONST_SGL(0.0f);
    541 
    542     if (ac.r11r != FL2FXCONST_DBL(0.0f)) {
    543       /* ac.r11r is always >=0 */
    544       FIXP_DBL tmp, absTmp;
    545 
    546       if (!useLP) {
    547         tmp = (ac.r01r >> (LPC_SCALE_FACTOR + 1)) +
    548               (fMultDiv2(alphar[1], ac.r12r) + fMultDiv2(alphai[1], ac.r12i));
    549       } else {
    550         if (ac.r01r >= FL2FXCONST_DBL(0.0f))
    551           tmp = (ac.r01r >> (LPC_SCALE_FACTOR + 1)) +
    552                 fMultDiv2(alphar[1], ac.r12r);
    553         else
    554           tmp = -((-ac.r01r) >> (LPC_SCALE_FACTOR + 1)) +
    555                 fMultDiv2(alphar[1], ac.r12r);
    556       }
    557 
    558       absTmp = fixp_abs(tmp);
    559 
    560       /*
    561         Quick check: is first filter coeff >= 1(4)
    562       */
    563 
    564       if (absTmp >= (ac.r11r >> 1)) {
    565         resetLPCCoeffs = 1;
    566       } else {
    567         INT scale;
    568         FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
    569         alphar[0] = FX_DBL2FX_SGL(scaleValue(result, scale + 1));
    570 
    571         if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))
    572           alphar[0] = -alphar[0];
    573       }
    574 
    575       if (!useLP) {
    576         tmp = (ac.r01i >> (LPC_SCALE_FACTOR + 1)) +
    577               (fMultDiv2(alphai[1], ac.r12r) - fMultDiv2(alphar[1], ac.r12i));
    578 
    579         absTmp = fixp_abs(tmp);
    580 
    581         /*
    582         Quick check: is second filter coeff >= 1(4)
    583         */
    584         if (absTmp >= (ac.r11r >> 1)) {
    585           resetLPCCoeffs = 1;
    586         } else {
    587           INT scale;
    588           FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
    589           alphai[0] = FX_DBL2FX_SGL(scaleValue(result, scale + 1));
    590           if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))
    591             alphai[0] = -alphai[0];
    592         }
    593       }
    594     }
    595 
    596     if (!useLP) {
    597       /* Now check the quadratic criteria */
    598       if ((fMultDiv2(alphar[0], alphar[0]) + fMultDiv2(alphai[0], alphai[0])) >=
    599           FL2FXCONST_DBL(0.5f))
    600         resetLPCCoeffs = 1;
    601       if ((fMultDiv2(alphar[1], alphar[1]) + fMultDiv2(alphai[1], alphai[1])) >=
    602           FL2FXCONST_DBL(0.5f))
    603         resetLPCCoeffs = 1;
    604     }
    605 
    606     if (resetLPCCoeffs) {
    607       alphar[0] = FL2FXCONST_SGL(0.0f);
    608       alphar[1] = FL2FXCONST_SGL(0.0f);
    609       if (!useLP) {
    610         alphai[0] = FL2FXCONST_SGL(0.0f);
    611         alphai[1] = FL2FXCONST_SGL(0.0f);
    612       }
    613     }
    614 
    615     if (useLP) {
    616       /* Aliasing detection */
    617       if (ac.r11r == FL2FXCONST_DBL(0.0f)) {
    618         k1 = FL2FXCONST_DBL(0.0f);
    619       } else {
    620         if (fixp_abs(ac.r01r) >= fixp_abs(ac.r11r)) {
    621           if (fMultDiv2(ac.r01r, ac.r11r) < FL2FX_DBL(0.0f)) {
    622             k1 = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_SGL(1.0f)*/;
    623           } else {
    624             /* Since this value is squared later, it must not ever become -1.0f.
    625              */
    626             k1 = (FIXP_DBL)(MINVAL_DBL + 1) /*FL2FXCONST_SGL(-1.0f)*/;
    627           }
    628         } else {
    629           INT scale;
    630           FIXP_DBL result =
    631               fDivNorm(fixp_abs(ac.r01r), fixp_abs(ac.r11r), &scale);
    632           k1 = scaleValue(result, scale);
    633 
    634           if (!((ac.r01r < FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))) {
    635             k1 = -k1;
    636           }
    637         }
    638       }
    639       if ((loBand > 1) && (loBand < v_k_master0)) {
    640         /* Check if the gain should be locked */
    641         FIXP_DBL deg =
    642             /*FL2FXCONST_DBL(1.0f)*/ (FIXP_DBL)MAXVAL_DBL - fPow2(k1_below);
    643         degreeAlias[loBand] = FL2FXCONST_DBL(0.0f);
    644         if (((loBand & 1) == 0) && (k1 < FL2FXCONST_DBL(0.0f))) {
    645           if (k1_below < FL2FXCONST_DBL(0.0f)) { /* 2-Ch Aliasing Detection */
    646             degreeAlias[loBand] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
    647             if (k1_below2 >
    648                 FL2FXCONST_DBL(0.0f)) { /* 3-Ch Aliasing Detection */
    649               degreeAlias[loBand - 1] = deg;
    650             }
    651           } else if (k1_below2 >
    652                      FL2FXCONST_DBL(0.0f)) { /* 3-Ch Aliasing Detection */
    653             degreeAlias[loBand] = deg;
    654           }
    655         }
    656         if (((loBand & 1) == 1) && (k1 > FL2FXCONST_DBL(0.0f))) {
    657           if (k1_below > FL2FXCONST_DBL(0.0f)) { /* 2-CH Aliasing Detection */
    658             degreeAlias[loBand] = (FIXP_DBL)MAXVAL_DBL /*FL2FXCONST_DBL(1.0f)*/;
    659             if (k1_below2 <
    660                 FL2FXCONST_DBL(0.0f)) { /* 3-CH Aliasing Detection */
    661               degreeAlias[loBand - 1] = deg;
    662             }
    663           } else if (k1_below2 <
    664                      FL2FXCONST_DBL(0.0f)) { /* 3-CH Aliasing Detection */
    665             degreeAlias[loBand] = deg;
    666           }
    667         }
    668       }
    669       /* remember k1 values of the 2 QMF channels below the current channel */
    670       k1_below2 = k1_below;
    671       k1_below = k1;
    672     }
    673 
    674     patch = 0;
    675 
    676     while (patch < pSettings->noOfPatches) { /* inner loop over every patch */
    677 
    678       int hiBand = loBand + patchParam[patch].targetBandOffs;
    679 
    680       if (loBand < patchParam[patch].sourceStartBand ||
    681           loBand >= patchParam[patch].sourceStopBand
    682           //|| hiBand >= hLppTrans->pSettings->noChannels
    683       ) {
    684         /* Lowband not in current patch - proceed */
    685         patch++;
    686         continue;
    687       }
    688 
    689       FDK_ASSERT(hiBand < (64));
    690 
    691       /* bwIndex[patch] is already initialized with value from previous band
    692        * inside this patch */
    693       while (hiBand >= pSettings->bwBorders[bwIndex[patch]] &&
    694              bwIndex[patch] < MAX_NUM_PATCHES - 1) {
    695         bwIndex[patch]++;
    696       }
    697 
    698       /*
    699         Filter Step 2: add the left slope with the current filter to the buffer
    700                        pure source values are already in there
    701       */
    702       bw = FX_DBL2FX_SGL(bwVector[bwIndex[patch]]);
    703 
    704       a0r = FX_DBL2FX_SGL(
    705           fMult(bw, alphar[0])); /* Apply current bandwidth expansion factor */
    706 
    707       if (!useLP) a0i = FX_DBL2FX_SGL(fMult(bw, alphai[0]));
    708       bw = FX_DBL2FX_SGL(fPow2(bw));
    709       a1r = FX_DBL2FX_SGL(fMult(bw, alphar[1]));
    710       if (!useLP) a1i = FX_DBL2FX_SGL(fMult(bw, alphai[1]));
    711 
    712       /*
    713         Filter Step 3: insert the middle part which won't be windowed
    714       */
    715       if (bw <= FL2FXCONST_SGL(0.0f)) {
    716         if (!useLP) {
    717           int descale =
    718               fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
    719           for (i = startSample; i < stopSample; i++) {
    720             FIXP_DBL accu1, accu2;
    721             accu1 = lowBandReal[LPC_ORDER + i] >> descale;
    722             accu2 = lowBandImag[LPC_ORDER + i] >> descale;
    723             if (fPreWhitening) {
    724               accu1 = scaleValueSaturate(
    725                   fMultDiv2(accu1, preWhiteningGains[loBand]),
    726                   preWhiteningGains_exp[loBand] + 1);
    727               accu2 = scaleValueSaturate(
    728                   fMultDiv2(accu2, preWhiteningGains[loBand]),
    729                   preWhiteningGains_exp[loBand] + 1);
    730             }
    731             qmfBufferReal[i][hiBand] = accu1;
    732             qmfBufferImag[i][hiBand] = accu2;
    733           }
    734         } else {
    735           int descale =
    736               fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
    737           for (i = startSample; i < stopSample; i++) {
    738             qmfBufferReal[i][hiBand] = lowBandReal[LPC_ORDER + i] >> descale;
    739           }
    740         }
    741       } else { /* bw <= 0 */
    742 
    743         if (!useLP) {
    744           int descale =
    745               fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
    746 #ifdef FUNCTION_LPPTRANSPOSER_func1
    747           lppTransposer_func1(
    748               lowBandReal + LPC_ORDER + startSample,
    749               lowBandImag + LPC_ORDER + startSample,
    750               qmfBufferReal + startSample, qmfBufferImag + startSample,
    751               stopSample - startSample, (int)hiBand, dynamicScale, descale, a0r,
    752               a0i, a1r, a1i, fPreWhitening, preWhiteningGains[loBand],
    753               preWhiteningGains_exp[loBand] + 1);
    754 #else
    755           for (i = startSample; i < stopSample; i++) {
    756             FIXP_DBL accu1, accu2;
    757 
    758             accu1 = (fMultDiv2(a0r, lowBandReal[LPC_ORDER + i - 1]) -
    759                      fMultDiv2(a0i, lowBandImag[LPC_ORDER + i - 1]) +
    760                      fMultDiv2(a1r, lowBandReal[LPC_ORDER + i - 2]) -
    761                      fMultDiv2(a1i, lowBandImag[LPC_ORDER + i - 2])) >>
    762                     dynamicScale;
    763             accu2 = (fMultDiv2(a0i, lowBandReal[LPC_ORDER + i - 1]) +
    764                      fMultDiv2(a0r, lowBandImag[LPC_ORDER + i - 1]) +
    765                      fMultDiv2(a1i, lowBandReal[LPC_ORDER + i - 2]) +
    766                      fMultDiv2(a1r, lowBandImag[LPC_ORDER + i - 2])) >>
    767                     dynamicScale;
    768 
    769             accu1 = (lowBandReal[LPC_ORDER + i] >> descale) + (accu1 << 1);
    770             accu2 = (lowBandImag[LPC_ORDER + i] >> descale) + (accu2 << 1);
    771             if (fPreWhitening) {
    772               accu1 = scaleValueSaturate(
    773                   fMultDiv2(accu1, preWhiteningGains[loBand]),
    774                   preWhiteningGains_exp[loBand] + 1);
    775               accu2 = scaleValueSaturate(
    776                   fMultDiv2(accu2, preWhiteningGains[loBand]),
    777                   preWhiteningGains_exp[loBand] + 1);
    778             }
    779             qmfBufferReal[i][hiBand] = accu1;
    780             qmfBufferImag[i][hiBand] = accu2;
    781           }
    782 #endif
    783         } else {
    784           FDK_ASSERT(dynamicScale >= 0);
    785           calc_qmfBufferReal(
    786               qmfBufferReal, &(lowBandReal[LPC_ORDER + startSample - 2]),
    787               startSample, stopSample, hiBand, dynamicScale,
    788               fMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale)), a0r,
    789               a1r);
    790         }
    791       } /* bw <= 0 */
    792 
    793       patch++;
    794 
    795     } /* inner loop over patches */
    796 
    797     /*
    798      * store the unmodified filter coefficients if there is
    799      * an overlapping envelope
    800      *****************************************************************/
    801 
    802   } /* outer loop over bands (loBand) */
    803 
    804   if (useLP) {
    805     for (loBand = pSettings->lbStartPatching;
    806          loBand < pSettings->lbStopPatching; loBand++) {
    807       patch = 0;
    808       while (patch < pSettings->noOfPatches) {
    809         UCHAR hiBand = loBand + patchParam[patch].targetBandOffs;
    810 
    811         if (loBand < patchParam[patch].sourceStartBand ||
    812             loBand >= patchParam[patch].sourceStopBand ||
    813             hiBand >= (64) /* Highband out of range (biterror) */
    814         ) {
    815           /* Lowband not in current patch or highband out of range (might be
    816            * caused by biterrors)- proceed */
    817           patch++;
    818           continue;
    819         }
    820 
    821         if (hiBand != patchParam[patch].targetStartBand)
    822           degreeAlias[hiBand] = degreeAlias[loBand];
    823 
    824         patch++;
    825       }
    826     } /* end  for loop */
    827   }
    828 
    829   for (i = 0; i < nInvfBands; i++) {
    830     hLppTrans->bwVectorOld[i] = bwVector[i];
    831   }
    832 
    833   /*
    834     set high band scale factor
    835   */
    836   sbrScaleFactor->hb_scale = comLowBandScale - (LPC_SCALE_FACTOR);
    837 }
    838 
    839 void lppTransposerHBE(
    840     HANDLE_SBR_LPP_TRANS hLppTrans, /*!< Handle of lpp transposer  */
    841     HANDLE_HBE_TRANSPOSER hQmfTransposer,
    842     QMF_SCALE_FACTOR *sbrScaleFactor, /*!< Scaling factors */
    843     FIXP_DBL **qmfBufferReal, /*!< Pointer to pointer to real part of subband
    844                                  samples (source) */
    845     FIXP_DBL **qmfBufferImag, /*!< Pointer to pointer to imaginary part of
    846                                  subband samples (source) */
    847     const int timeStep,       /*!< Time step of envelope */
    848     const int firstSlotOffs,  /*!< Start position in time */
    849     const int lastSlotOffs,   /*!< Number of overlap-slots into next frame */
    850     const int nInvfBands,     /*!< Number of bands for inverse filtering */
    851     INVF_MODE *sbr_invf_mode, /*!< Current inverse filtering modes */
    852     INVF_MODE *sbr_invf_mode_prev /*!< Previous inverse filtering modes */
    853 ) {
    854   INT bwIndex;
    855   FIXP_DBL bwVector[MAX_NUM_PATCHES_HBE]; /*!< pole moving factors */
    856 
    857   int i;
    858   int loBand, start, stop;
    859   TRANSPOSER_SETTINGS *pSettings = hLppTrans->pSettings;
    860   PATCH_PARAM *patchParam = pSettings->patchParam;
    861 
    862   FIXP_SGL alphar[LPC_ORDER], a0r, a1r;
    863   FIXP_SGL alphai[LPC_ORDER], a0i = 0, a1i = 0;
    864   FIXP_SGL bw = FL2FXCONST_SGL(0.0f);
    865 
    866   int autoCorrLength;
    867 
    868   ACORR_COEFS ac;
    869   int startSample;
    870   int stopSample;
    871   int stopSampleClear;
    872 
    873   int comBandScale;
    874   int ovLowBandShift;
    875   int lowBandShift;
    876   /*  int ovHighBandShift;*/
    877   int targetStopBand;
    878 
    879   alphai[0] = FL2FXCONST_SGL(0.0f);
    880   alphai[1] = FL2FXCONST_SGL(0.0f);
    881 
    882   startSample = firstSlotOffs * timeStep;
    883   stopSample = pSettings->nCols + lastSlotOffs * timeStep;
    884 
    885   inverseFilteringLevelEmphasis(hLppTrans, nInvfBands, sbr_invf_mode,
    886                                 sbr_invf_mode_prev, bwVector);
    887 
    888   stopSampleClear = stopSample;
    889 
    890   autoCorrLength = pSettings->nCols + pSettings->overlap;
    891 
    892   /* Set upper subbands to zero:
    893      This is required in case that the patches do not cover the complete
    894      highband (because the last patch would be too short). Possible
    895      optimization: Clearing bands up to usb would be sufficient here. */
    896   targetStopBand = patchParam[pSettings->noOfPatches - 1].targetStartBand +
    897                    patchParam[pSettings->noOfPatches - 1].numBandsInPatch;
    898 
    899   int memSize = ((64) - targetStopBand) * sizeof(FIXP_DBL);
    900 
    901   for (i = startSample; i < stopSampleClear; i++) {
    902     FDKmemclear(&qmfBufferReal[i][targetStopBand], memSize);
    903     FDKmemclear(&qmfBufferImag[i][targetStopBand], memSize);
    904   }
    905 
    906   /*
    907   Calc common low band scale factor
    908   */
    909   comBandScale = sbrScaleFactor->hb_scale;
    910 
    911   ovLowBandShift = sbrScaleFactor->hb_scale - comBandScale;
    912   lowBandShift = sbrScaleFactor->hb_scale - comBandScale;
    913   /*  ovHighBandShift = firstSlotOffs == 0 ? ovLowBandShift:0;*/
    914 
    915   /* outer loop over bands to do analysis only once for each band */
    916 
    917   start = hQmfTransposer->startBand;
    918   stop = hQmfTransposer->stopBand;
    919 
    920   for (loBand = start; loBand < stop; loBand++) {
    921     bwIndex = 0;
    922 
    923     FIXP_DBL lowBandReal[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
    924     FIXP_DBL lowBandImag[(((1024) / (32) * (4) / 2) + (3 * (4))) + LPC_ORDER];
    925 
    926     int resetLPCCoeffs = 0;
    927     int dynamicScale = DFRACT_BITS - 1 - LPC_SCALE_FACTOR;
    928     int acDetScale = 0; /* scaling of autocorrelation determinant */
    929 
    930     for (i = 0; i < LPC_ORDER; i++) {
    931       lowBandReal[i] = hLppTrans->lpcFilterStatesRealHBE[i][loBand];
    932       lowBandImag[i] = hLppTrans->lpcFilterStatesImagHBE[i][loBand];
    933     }
    934 
    935     for (; i < LPC_ORDER + firstSlotOffs * timeStep; i++) {
    936       lowBandReal[i] = hLppTrans->lpcFilterStatesRealHBE[i][loBand];
    937       lowBandImag[i] = hLppTrans->lpcFilterStatesImagHBE[i][loBand];
    938     }
    939 
    940     /*
    941     Take old slope length qmf slot source values out of (overlap)qmf buffer
    942     */
    943     for (i = firstSlotOffs * timeStep;
    944          i < pSettings->nCols + pSettings->overlap; i++) {
    945       lowBandReal[i + LPC_ORDER] = qmfBufferReal[i][loBand];
    946       lowBandImag[i + LPC_ORDER] = qmfBufferImag[i][loBand];
    947     }
    948 
    949     /* store unmodified values to buffer */
    950     for (i = 0; i < LPC_ORDER + pSettings->overlap; i++) {
    951       hLppTrans->lpcFilterStatesRealHBE[i][loBand] =
    952           qmfBufferReal[pSettings->nCols - LPC_ORDER + i][loBand];
    953       hLppTrans->lpcFilterStatesImagHBE[i][loBand] =
    954           qmfBufferImag[pSettings->nCols - LPC_ORDER + i][loBand];
    955     }
    956 
    957     /*
    958     Determine dynamic scaling value.
    959     */
    960     dynamicScale =
    961         fixMin(dynamicScale,
    962                getScalefactor(lowBandReal, LPC_ORDER + pSettings->overlap) +
    963                    ovLowBandShift);
    964     dynamicScale =
    965         fixMin(dynamicScale,
    966                getScalefactor(&lowBandReal[LPC_ORDER + pSettings->overlap],
    967                               pSettings->nCols) +
    968                    lowBandShift);
    969     dynamicScale =
    970         fixMin(dynamicScale,
    971                getScalefactor(lowBandImag, LPC_ORDER + pSettings->overlap) +
    972                    ovLowBandShift);
    973     dynamicScale =
    974         fixMin(dynamicScale,
    975                getScalefactor(&lowBandImag[LPC_ORDER + pSettings->overlap],
    976                               pSettings->nCols) +
    977                    lowBandShift);
    978 
    979     dynamicScale = fixMax(
    980         0, dynamicScale - 1); /* one additional bit headroom to prevent -1.0 */
    981 
    982     /*
    983     Scale temporal QMF buffer.
    984     */
    985     scaleValues(&lowBandReal[0], LPC_ORDER + pSettings->overlap,
    986                 dynamicScale - ovLowBandShift);
    987     scaleValues(&lowBandReal[LPC_ORDER + pSettings->overlap], pSettings->nCols,
    988                 dynamicScale - lowBandShift);
    989     scaleValues(&lowBandImag[0], LPC_ORDER + pSettings->overlap,
    990                 dynamicScale - ovLowBandShift);
    991     scaleValues(&lowBandImag[LPC_ORDER + pSettings->overlap], pSettings->nCols,
    992                 dynamicScale - lowBandShift);
    993 
    994     acDetScale += autoCorr2nd_cplx(&ac, lowBandReal + LPC_ORDER,
    995                                    lowBandImag + LPC_ORDER, autoCorrLength);
    996 
    997     /* Examine dynamic of determinant in autocorrelation. */
    998     acDetScale += 2 * (comBandScale + dynamicScale);
    999     acDetScale *= 2;            /* two times reflection coefficent scaling */
   1000     acDetScale += ac.det_scale; /* ac scaling of determinant */
   1001 
   1002     /* In case of determinant < 10^-38, resetLPCCoeffs=1 has to be enforced. */
   1003     if (acDetScale > 126) {
   1004       resetLPCCoeffs = 1;
   1005     }
   1006 
   1007     alphar[1] = FL2FXCONST_SGL(0.0f);
   1008     alphai[1] = FL2FXCONST_SGL(0.0f);
   1009 
   1010     if (ac.det != FL2FXCONST_DBL(0.0f)) {
   1011       FIXP_DBL tmp, absTmp, absDet;
   1012 
   1013       absDet = fixp_abs(ac.det);
   1014 
   1015       tmp = (fMultDiv2(ac.r01r, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) -
   1016             ((fMultDiv2(ac.r01i, ac.r12i) + fMultDiv2(ac.r02r, ac.r11r)) >>
   1017              (LPC_SCALE_FACTOR - 1));
   1018       absTmp = fixp_abs(tmp);
   1019 
   1020       /*
   1021       Quick check: is first filter coeff >= 1(4)
   1022       */
   1023       {
   1024         INT scale;
   1025         FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
   1026         scale = scale + ac.det_scale;
   1027 
   1028         if ((scale > 0) && (result >= (FIXP_DBL)MAXVAL_DBL >> scale)) {
   1029           resetLPCCoeffs = 1;
   1030         } else {
   1031           alphar[1] = FX_DBL2FX_SGL(scaleValue(result, scale));
   1032           if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
   1033             alphar[1] = -alphar[1];
   1034           }
   1035         }
   1036       }
   1037 
   1038       tmp = (fMultDiv2(ac.r01i, ac.r12r) >> (LPC_SCALE_FACTOR - 1)) +
   1039             ((fMultDiv2(ac.r01r, ac.r12i) -
   1040               (FIXP_DBL)fMultDiv2(ac.r02i, ac.r11r)) >>
   1041              (LPC_SCALE_FACTOR - 1));
   1042 
   1043       absTmp = fixp_abs(tmp);
   1044 
   1045       /*
   1046       Quick check: is second filter coeff >= 1(4)
   1047       */
   1048       {
   1049         INT scale;
   1050         FIXP_DBL result = fDivNorm(absTmp, absDet, &scale);
   1051         scale = scale + ac.det_scale;
   1052 
   1053         if ((scale > 0) &&
   1054             (result >= /*FL2FXCONST_DBL(1.f)*/ (FIXP_DBL)MAXVAL_DBL >> scale)) {
   1055           resetLPCCoeffs = 1;
   1056         } else {
   1057           alphai[1] = FX_DBL2FX_SGL(scaleValue(result, scale));
   1058           if ((tmp < FL2FX_DBL(0.0f)) ^ (ac.det < FL2FX_DBL(0.0f))) {
   1059             alphai[1] = -alphai[1];
   1060           }
   1061         }
   1062       }
   1063     }
   1064 
   1065     alphar[0] = FL2FXCONST_SGL(0.0f);
   1066     alphai[0] = FL2FXCONST_SGL(0.0f);
   1067 
   1068     if (ac.r11r != FL2FXCONST_DBL(0.0f)) {
   1069       /* ac.r11r is always >=0 */
   1070       FIXP_DBL tmp, absTmp;
   1071 
   1072       tmp = (ac.r01r >> (LPC_SCALE_FACTOR + 1)) +
   1073             (fMultDiv2(alphar[1], ac.r12r) + fMultDiv2(alphai[1], ac.r12i));
   1074 
   1075       absTmp = fixp_abs(tmp);
   1076 
   1077       /*
   1078       Quick check: is first filter coeff >= 1(4)
   1079       */
   1080 
   1081       if (absTmp >= (ac.r11r >> 1)) {
   1082         resetLPCCoeffs = 1;
   1083       } else {
   1084         INT scale;
   1085         FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
   1086         alphar[0] = FX_DBL2FX_SGL(scaleValue(result, scale + 1));
   1087 
   1088         if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f)))
   1089           alphar[0] = -alphar[0];
   1090       }
   1091 
   1092       tmp = (ac.r01i >> (LPC_SCALE_FACTOR + 1)) +
   1093             (fMultDiv2(alphai[1], ac.r12r) - fMultDiv2(alphar[1], ac.r12i));
   1094 
   1095       absTmp = fixp_abs(tmp);
   1096 
   1097       /*
   1098       Quick check: is second filter coeff >= 1(4)
   1099       */
   1100       if (absTmp >= (ac.r11r >> 1)) {
   1101         resetLPCCoeffs = 1;
   1102       } else {
   1103         INT scale;
   1104         FIXP_DBL result = fDivNorm(absTmp, fixp_abs(ac.r11r), &scale);
   1105         alphai[0] = FX_DBL2FX_SGL(scaleValue(result, scale + 1));
   1106         if ((tmp > FL2FX_DBL(0.0f)) ^ (ac.r11r < FL2FX_DBL(0.0f))) {
   1107           alphai[0] = -alphai[0];
   1108         }
   1109       }
   1110     }
   1111 
   1112     /* Now check the quadratic criteria */
   1113     if ((fMultDiv2(alphar[0], alphar[0]) + fMultDiv2(alphai[0], alphai[0])) >=
   1114         FL2FXCONST_DBL(0.5f)) {
   1115       resetLPCCoeffs = 1;
   1116     }
   1117     if ((fMultDiv2(alphar[1], alphar[1]) + fMultDiv2(alphai[1], alphai[1])) >=
   1118         FL2FXCONST_DBL(0.5f)) {
   1119       resetLPCCoeffs = 1;
   1120     }
   1121 
   1122     if (resetLPCCoeffs) {
   1123       alphar[0] = FL2FXCONST_SGL(0.0f);
   1124       alphar[1] = FL2FXCONST_SGL(0.0f);
   1125       alphai[0] = FL2FXCONST_SGL(0.0f);
   1126       alphai[1] = FL2FXCONST_SGL(0.0f);
   1127     }
   1128 
   1129     while (bwIndex < MAX_NUM_PATCHES - 1 &&
   1130            loBand >= pSettings->bwBorders[bwIndex]) {
   1131       bwIndex++;
   1132     }
   1133 
   1134     /*
   1135     Filter Step 2: add the left slope with the current filter to the buffer
   1136     pure source values are already in there
   1137     */
   1138     bw = FX_DBL2FX_SGL(bwVector[bwIndex]);
   1139 
   1140     a0r = FX_DBL2FX_SGL(
   1141         fMult(bw, alphar[0])); /* Apply current bandwidth expansion factor */
   1142     a0i = FX_DBL2FX_SGL(fMult(bw, alphai[0]));
   1143     bw = FX_DBL2FX_SGL(fPow2(bw));
   1144     a1r = FX_DBL2FX_SGL(fMult(bw, alphar[1]));
   1145     a1i = FX_DBL2FX_SGL(fMult(bw, alphai[1]));
   1146 
   1147     /*
   1148     Filter Step 3: insert the middle part which won't be windowed
   1149     */
   1150     if (bw <= FL2FXCONST_SGL(0.0f)) {
   1151       int descale = fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
   1152       for (i = startSample; i < stopSample; i++) {
   1153         qmfBufferReal[i][loBand] = lowBandReal[LPC_ORDER + i] >> descale;
   1154         qmfBufferImag[i][loBand] = lowBandImag[LPC_ORDER + i] >> descale;
   1155       }
   1156     } else { /* bw <= 0 */
   1157 
   1158       int descale = fixMin(DFRACT_BITS - 1, (LPC_SCALE_FACTOR + dynamicScale));
   1159 
   1160       for (i = startSample; i < stopSample; i++) {
   1161         FIXP_DBL accu1, accu2;
   1162 
   1163         accu1 = (fMultDiv2(a0r, lowBandReal[LPC_ORDER + i - 1]) -
   1164                  fMultDiv2(a0i, lowBandImag[LPC_ORDER + i - 1]) +
   1165                  fMultDiv2(a1r, lowBandReal[LPC_ORDER + i - 2]) -
   1166                  fMultDiv2(a1i, lowBandImag[LPC_ORDER + i - 2])) >>
   1167                 dynamicScale;
   1168         accu2 = (fMultDiv2(a0i, lowBandReal[LPC_ORDER + i - 1]) +
   1169                  fMultDiv2(a0r, lowBandImag[LPC_ORDER + i - 1]) +
   1170                  fMultDiv2(a1i, lowBandReal[LPC_ORDER + i - 2]) +
   1171                  fMultDiv2(a1r, lowBandImag[LPC_ORDER + i - 2])) >>
   1172                 dynamicScale;
   1173 
   1174         qmfBufferReal[i][loBand] =
   1175             (lowBandReal[LPC_ORDER + i] >> descale) + (accu1 << 1);
   1176         qmfBufferImag[i][loBand] =
   1177             (lowBandImag[LPC_ORDER + i] >> descale) + (accu2 << 1);
   1178       }
   1179     } /* bw <= 0 */
   1180 
   1181     /*
   1182      * store the unmodified filter coefficients if there is
   1183      * an overlapping envelope
   1184      *****************************************************************/
   1185 
   1186   } /* outer loop over bands (loBand) */
   1187 
   1188   for (i = 0; i < nInvfBands; i++) {
   1189     hLppTrans->bwVectorOld[i] = bwVector[i];
   1190   }
   1191 
   1192   /*
   1193   set high band scale factor
   1194   */
   1195   sbrScaleFactor->hb_scale = comBandScale - (LPC_SCALE_FACTOR);
   1196 }
   1197 
   1198 /*!
   1199  *
   1200  * \brief Initialize one low power transposer instance
   1201  *
   1202  *
   1203  */
   1204 SBR_ERROR
   1205 createLppTransposer(
   1206     HANDLE_SBR_LPP_TRANS hs,        /*!< Handle of low power transposer  */
   1207     TRANSPOSER_SETTINGS *pSettings, /*!< Pointer to settings */
   1208     const int highBandStartSb,      /*!< ? */
   1209     UCHAR *v_k_master,              /*!< Master table */
   1210     const int numMaster,            /*!< Valid entries in master table */
   1211     const int usb,                  /*!< Highband area stop subband */
   1212     const int timeSlots,            /*!< Number of time slots */
   1213     const int nCols,                /*!< Number of colums (codec qmf bank) */
   1214     UCHAR *noiseBandTable,  /*!< Mapping of SBR noise bands to QMF bands */
   1215     const int noNoiseBands, /*!< Number of noise bands */
   1216     UINT fs,                /*!< Sample Frequency */
   1217     const int chan,         /*!< Channel number */
   1218     const int overlap) {
   1219   /* FB inverse filtering settings */
   1220   hs->pSettings = pSettings;
   1221 
   1222   pSettings->nCols = nCols;
   1223   pSettings->overlap = overlap;
   1224 
   1225   switch (timeSlots) {
   1226     case 15:
   1227     case 16:
   1228       break;
   1229 
   1230     default:
   1231       return SBRDEC_UNSUPPORTED_CONFIG; /* Unimplemented */
   1232   }
   1233 
   1234   if (chan == 0) {
   1235     /* Init common data only once */
   1236     hs->pSettings->nCols = nCols;
   1237 
   1238     return resetLppTransposer(hs, highBandStartSb, v_k_master, numMaster,
   1239                               noiseBandTable, noNoiseBands, usb, fs);
   1240   }
   1241   return SBRDEC_OK;
   1242 }
   1243 
   1244 static int findClosestEntry(UCHAR goalSb, UCHAR *v_k_master, UCHAR numMaster,
   1245                             UCHAR direction) {
   1246   int index;
   1247 
   1248   if (goalSb <= v_k_master[0]) return v_k_master[0];
   1249 
   1250   if (goalSb >= v_k_master[numMaster]) return v_k_master[numMaster];
   1251 
   1252   if (direction) {
   1253     index = 0;
   1254     while (v_k_master[index] < goalSb) {
   1255       index++;
   1256     }
   1257   } else {
   1258     index = numMaster;
   1259     while (v_k_master[index] > goalSb) {
   1260       index--;
   1261     }
   1262   }
   1263 
   1264   return v_k_master[index];
   1265 }
   1266 
   1267 /*!
   1268  *
   1269  * \brief Reset memory for one lpp transposer instance
   1270  *
   1271  * \return SBRDEC_OK on success, SBRDEC_UNSUPPORTED_CONFIG on error
   1272  */
   1273 SBR_ERROR
   1274 resetLppTransposer(
   1275     HANDLE_SBR_LPP_TRANS hLppTrans, /*!< Handle of lpp transposer  */
   1276     UCHAR highBandStartSb,          /*!< High band area: start subband */
   1277     UCHAR *v_k_master,              /*!< Master table */
   1278     UCHAR numMaster,                /*!< Valid entries in master table */
   1279     UCHAR *noiseBandTable, /*!< Mapping of SBR noise bands to QMF bands */
   1280     UCHAR noNoiseBands,    /*!< Number of noise bands */
   1281     UCHAR usb,             /*!< High band area: stop subband */
   1282     UINT fs                /*!< SBR output sampling frequency */
   1283 ) {
   1284   TRANSPOSER_SETTINGS *pSettings = hLppTrans->pSettings;
   1285   PATCH_PARAM *patchParam = pSettings->patchParam;
   1286 
   1287   int i, patch;
   1288   int targetStopBand;
   1289   int sourceStartBand;
   1290   int patchDistance;
   1291   int numBandsInPatch;
   1292 
   1293   int lsb = v_k_master[0]; /* Start subband expressed in "non-critical" sampling
   1294                               terms*/
   1295   int xoverOffset = highBandStartSb -
   1296                     lsb; /* Calculate distance in QMF bands between k0 and kx */
   1297   int startFreqHz;
   1298 
   1299   int desiredBorder;
   1300 
   1301   usb = fixMin(usb, v_k_master[numMaster]); /* Avoid endless loops (compare with
   1302                                                float code). */
   1303 
   1304   /*
   1305    * Plausibility check
   1306    */
   1307 
   1308   if (pSettings->nCols == 64) {
   1309     if (lsb < 4) {
   1310       /* 4:1 SBR Requirement k0 >= 4 missed! */
   1311       return SBRDEC_UNSUPPORTED_CONFIG;
   1312     }
   1313   } else if (lsb - SHIFT_START_SB < 4) {
   1314     return SBRDEC_UNSUPPORTED_CONFIG;
   1315   }
   1316 
   1317   /*
   1318    * Initialize the patching parameter
   1319    */
   1320   /* ISO/IEC 14496-3 (Figure 4.48): goalSb = round( 2.048e6 / fs ) */
   1321   desiredBorder = (((2048000 * 2) / fs) + 1) >> 1;
   1322 
   1323   desiredBorder = findClosestEntry(desiredBorder, v_k_master, numMaster,
   1324                                    1); /* Adapt region to master-table */
   1325 
   1326   /* First patch */
   1327   sourceStartBand = SHIFT_START_SB + xoverOffset;
   1328   targetStopBand = lsb + xoverOffset; /* upperBand */
   1329 
   1330   /* Even (odd) numbered channel must be patched to even (odd) numbered channel
   1331    */
   1332   patch = 0;
   1333   while (targetStopBand < usb) {
   1334     /* Too many patches?
   1335        Allow MAX_NUM_PATCHES+1 patches here.
   1336        we need to check later again, since patch might be the highest patch
   1337        AND contain less than 3 bands => actual number of patches will be reduced
   1338        by 1.
   1339     */
   1340     if (patch > MAX_NUM_PATCHES) {
   1341       return SBRDEC_UNSUPPORTED_CONFIG;
   1342     }
   1343 
   1344     patchParam[patch].guardStartBand = targetStopBand;
   1345     patchParam[patch].targetStartBand = targetStopBand;
   1346 
   1347     numBandsInPatch =
   1348         desiredBorder - targetStopBand; /* Get the desired range of the patch */
   1349 
   1350     if (numBandsInPatch >= lsb - sourceStartBand) {
   1351       /* Desired number bands are not available -> patch whole source range */
   1352       patchDistance =
   1353           targetStopBand - sourceStartBand; /* Get the targetOffset */
   1354       patchDistance =
   1355           patchDistance & ~1; /* Rounding off odd numbers and make all even */
   1356       numBandsInPatch =
   1357           lsb - (targetStopBand -
   1358                  patchDistance); /* Update number of bands to be patched */
   1359       numBandsInPatch = findClosestEntry(targetStopBand + numBandsInPatch,
   1360                                          v_k_master, numMaster, 0) -
   1361                         targetStopBand; /* Adapt region to master-table */
   1362     }
   1363 
   1364     if (pSettings->nCols == 64) {
   1365       if (numBandsInPatch == 0 && sourceStartBand == SHIFT_START_SB) {
   1366         return SBRDEC_UNSUPPORTED_CONFIG;
   1367       }
   1368     }
   1369 
   1370     /* Desired number bands are available -> get the minimal even patching
   1371      * distance */
   1372     patchDistance =
   1373         numBandsInPatch + targetStopBand - lsb; /* Get minimal distance */
   1374     patchDistance = (patchDistance + 1) &
   1375                     ~1; /* Rounding up odd numbers and make all even */
   1376 
   1377     if (numBandsInPatch > 0) {
   1378       patchParam[patch].sourceStartBand = targetStopBand - patchDistance;
   1379       patchParam[patch].targetBandOffs = patchDistance;
   1380       patchParam[patch].numBandsInPatch = numBandsInPatch;
   1381       patchParam[patch].sourceStopBand =
   1382           patchParam[patch].sourceStartBand + numBandsInPatch;
   1383 
   1384       targetStopBand += patchParam[patch].numBandsInPatch;
   1385       patch++;
   1386     }
   1387 
   1388     /* All patches but first */
   1389     sourceStartBand = SHIFT_START_SB;
   1390 
   1391     /* Check if we are close to desiredBorder */
   1392     if (desiredBorder - targetStopBand < 3) /* MPEG doc */
   1393     {
   1394       desiredBorder = usb;
   1395     }
   1396   }
   1397 
   1398   patch--;
   1399 
   1400   /* If highest patch contains less than three subband: skip it */
   1401   if ((patch > 0) && (patchParam[patch].numBandsInPatch < 3)) {
   1402     patch--;
   1403     targetStopBand =
   1404         patchParam[patch].targetStartBand + patchParam[patch].numBandsInPatch;
   1405   }
   1406 
   1407   /* now check if we don't have one too many */
   1408   if (patch >= MAX_NUM_PATCHES) {
   1409     return SBRDEC_UNSUPPORTED_CONFIG;
   1410   }
   1411 
   1412   pSettings->noOfPatches = patch + 1;
   1413 
   1414   /* Check lowest and highest source subband */
   1415   pSettings->lbStartPatching = targetStopBand;
   1416   pSettings->lbStopPatching = 0;
   1417   for (patch = 0; patch < pSettings->noOfPatches; patch++) {
   1418     pSettings->lbStartPatching =
   1419         fixMin(pSettings->lbStartPatching, patchParam[patch].sourceStartBand);
   1420     pSettings->lbStopPatching =
   1421         fixMax(pSettings->lbStopPatching, patchParam[patch].sourceStopBand);
   1422   }
   1423 
   1424   for (i = 0; i < noNoiseBands; i++) {
   1425     pSettings->bwBorders[i] = noiseBandTable[i + 1];
   1426   }
   1427   for (; i < MAX_NUM_NOISE_VALUES; i++) {
   1428     pSettings->bwBorders[i] = 255;
   1429   }
   1430 
   1431   /*
   1432    * Choose whitening factors
   1433    */
   1434 
   1435   startFreqHz =
   1436       ((lsb + xoverOffset) * fs) >> 7; /* Shift does a division by 2*(64) */
   1437 
   1438   for (i = 1; i < NUM_WHFACTOR_TABLE_ENTRIES; i++) {
   1439     if (startFreqHz < FDK_sbrDecoder_sbr_whFactorsIndex[i]) break;
   1440   }
   1441   i--;
   1442 
   1443   pSettings->whFactors.off = FDK_sbrDecoder_sbr_whFactorsTable[i][0];
   1444   pSettings->whFactors.transitionLevel =
   1445       FDK_sbrDecoder_sbr_whFactorsTable[i][1];
   1446   pSettings->whFactors.lowLevel = FDK_sbrDecoder_sbr_whFactorsTable[i][2];
   1447   pSettings->whFactors.midLevel = FDK_sbrDecoder_sbr_whFactorsTable[i][3];
   1448   pSettings->whFactors.highLevel = FDK_sbrDecoder_sbr_whFactorsTable[i][4];
   1449 
   1450   return SBRDEC_OK;
   1451 }
   1452