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  Frequency scale calculation
    106 */
    107 
    108 #include "sbrdec_freq_sca.h"
    109 
    110 #include "transcendent.h"
    111 #include "sbr_rom.h"
    112 #include "env_extr.h"
    113 
    114 #include "genericStds.h" /* need log() for debug-code only */
    115 
    116 #define MAX_OCTAVE 29
    117 #define MAX_SECOND_REGION 50
    118 
    119 static int numberOfBands(FIXP_SGL bpo_div16, int start, int stop, int warpFlag);
    120 static void CalcBands(UCHAR *diff, UCHAR start, UCHAR stop, UCHAR num_bands);
    121 static SBR_ERROR modifyBands(UCHAR max_band, UCHAR *diff, UCHAR length);
    122 static void cumSum(UCHAR start_value, UCHAR *diff, UCHAR length,
    123                    UCHAR *start_adress);
    124 
    125 /*!
    126   \brief     Retrieve QMF-band where the SBR range starts
    127 
    128   Convert startFreq which was read from the bitstream into a
    129   QMF-channel number.
    130 
    131   \return  Number of start band
    132 */
    133 static UCHAR getStartBand(
    134     UINT fs,              /*!< Output sampling frequency */
    135     UCHAR startFreq,      /*!< Index to table of possible start bands */
    136     UINT headerDataFlags) /*!< Info to SBR mode */
    137 {
    138   INT band;
    139   UINT fsMapped = fs;
    140   SBR_RATE rate = DUAL;
    141 
    142   if (headerDataFlags & (SBRDEC_SYNTAX_USAC | SBRDEC_SYNTAX_RSVD50)) {
    143     if (headerDataFlags & SBRDEC_QUAD_RATE) {
    144       rate = QUAD;
    145     }
    146     fsMapped = sbrdec_mapToStdSampleRate(fs, 1);
    147   }
    148 
    149   FDK_ASSERT(2 * (rate + 1) <= (4));
    150 
    151   switch (fsMapped) {
    152     case 192000:
    153       band = FDK_sbrDecoder_sbr_start_freq_192[startFreq];
    154       break;
    155     case 176400:
    156       band = FDK_sbrDecoder_sbr_start_freq_176[startFreq];
    157       break;
    158     case 128000:
    159       band = FDK_sbrDecoder_sbr_start_freq_128[startFreq];
    160       break;
    161     case 96000:
    162     case 88200:
    163       band = FDK_sbrDecoder_sbr_start_freq_88[rate][startFreq];
    164       break;
    165     case 64000:
    166       band = FDK_sbrDecoder_sbr_start_freq_64[rate][startFreq];
    167       break;
    168     case 48000:
    169       band = FDK_sbrDecoder_sbr_start_freq_48[rate][startFreq];
    170       break;
    171     case 44100:
    172       band = FDK_sbrDecoder_sbr_start_freq_44[rate][startFreq];
    173       break;
    174     case 40000:
    175       band = FDK_sbrDecoder_sbr_start_freq_40[rate][startFreq];
    176       break;
    177     case 32000:
    178       band = FDK_sbrDecoder_sbr_start_freq_32[rate][startFreq];
    179       break;
    180     case 24000:
    181       band = FDK_sbrDecoder_sbr_start_freq_24[rate][startFreq];
    182       break;
    183     case 22050:
    184       band = FDK_sbrDecoder_sbr_start_freq_22[rate][startFreq];
    185       break;
    186     case 16000:
    187       band = FDK_sbrDecoder_sbr_start_freq_16[rate][startFreq];
    188       break;
    189     default:
    190       band = 255;
    191   }
    192 
    193   return band;
    194 }
    195 
    196 /*!
    197   \brief     Retrieve QMF-band where the SBR range starts
    198 
    199   Convert startFreq which was read from the bitstream into a
    200   QMF-channel number.
    201 
    202   \return  Number of start band
    203 */
    204 static UCHAR getStopBand(
    205     UINT fs,              /*!< Output sampling frequency */
    206     UCHAR stopFreq,       /*!< Index to table of possible start bands */
    207     UINT headerDataFlags, /*!< Info to SBR mode */
    208     UCHAR k0)             /*!< Start freq index */
    209 {
    210   UCHAR k2;
    211 
    212   if (stopFreq < 14) {
    213     INT stopMin;
    214     INT num = 2 * (64);
    215     UCHAR diff_tot[MAX_OCTAVE + MAX_SECOND_REGION];
    216     UCHAR *diff0 = diff_tot;
    217     UCHAR *diff1 = diff_tot + MAX_OCTAVE;
    218 
    219     if (headerDataFlags & SBRDEC_QUAD_RATE) {
    220       num >>= 1;
    221     }
    222 
    223     if (fs < 32000) {
    224       stopMin = (((2 * 6000 * num) / fs) + 1) >> 1;
    225     } else {
    226       if (fs < 64000) {
    227         stopMin = (((2 * 8000 * num) / fs) + 1) >> 1;
    228       } else {
    229         stopMin = (((2 * 10000 * num) / fs) + 1) >> 1;
    230       }
    231     }
    232 
    233     /*
    234       Choose a stop band between k1 and 64 depending on stopFreq (0..13),
    235       based on a logarithmic scale.
    236       The vectors diff0 and diff1 are used temporarily here.
    237     */
    238     CalcBands(diff0, stopMin, 64, 13);
    239     shellsort(diff0, 13);
    240     cumSum(stopMin, diff0, 13, diff1);
    241     k2 = diff1[stopFreq];
    242   } else if (stopFreq == 14)
    243     k2 = 2 * k0;
    244   else
    245     k2 = 3 * k0;
    246 
    247   /* Limit to Nyquist */
    248   if (k2 > (64)) k2 = (64);
    249 
    250   /* Range checks */
    251   /* 1 <= difference <= 48; 1 <= fs <= 96000 */
    252   {
    253     UCHAR max_freq_coeffs = (headerDataFlags & SBRDEC_QUAD_RATE)
    254                                 ? MAX_FREQ_COEFFS_QUAD_RATE
    255                                 : MAX_FREQ_COEFFS;
    256     if (((k2 - k0) > max_freq_coeffs) || (k2 <= k0)) {
    257       return 255;
    258     }
    259   }
    260 
    261   if (headerDataFlags & SBRDEC_QUAD_RATE) {
    262     return k2; /* skip other checks: (k2 - k0) must be <=
    263                   MAX_FREQ_COEFFS_QUAD_RATE for all fs */
    264   }
    265   if (headerDataFlags & (SBRDEC_SYNTAX_USAC | SBRDEC_SYNTAX_RSVD50)) {
    266     /* 1 <= difference <= 35; 42000 <= fs <= 96000 */
    267     if ((fs >= 42000) && ((k2 - k0) > MAX_FREQ_COEFFS_FS44100)) {
    268       return 255;
    269     }
    270     /* 1 <= difference <= 32; 46009 <= fs <= 96000 */
    271     if ((fs >= 46009) && ((k2 - k0) > MAX_FREQ_COEFFS_FS48000)) {
    272       return 255;
    273     }
    274   } else {
    275     /* 1 <= difference <= 35; fs == 44100 */
    276     if ((fs == 44100) && ((k2 - k0) > MAX_FREQ_COEFFS_FS44100)) {
    277       return 255;
    278     }
    279     /* 1 <= difference <= 32; 48000 <= fs <= 96000 */
    280     if ((fs >= 48000) && ((k2 - k0) > MAX_FREQ_COEFFS_FS48000)) {
    281       return 255;
    282     }
    283   }
    284 
    285   return k2;
    286 }
    287 
    288 /*!
    289   \brief     Generates master frequency tables
    290 
    291   Frequency tables are calculated according to the selected domain
    292   (linear/logarithmic) and granularity.
    293   IEC 14496-3 4.6.18.3.2.1
    294 
    295   \return  errorCode, 0 if successful
    296 */
    297 SBR_ERROR
    298 sbrdecUpdateFreqScale(
    299     UCHAR *v_k_master, /*!< Master table to be created */
    300     UCHAR *numMaster,  /*!< Number of entries in master table */
    301     UINT fs,           /*!< SBR working sampling rate */
    302     HANDLE_SBR_HEADER_DATA hHeaderData, /*!< Control data from bitstream */
    303     UINT flags) {
    304   FIXP_SGL bpo_div16; /* bands_per_octave divided by 16 */
    305   INT dk = 0;
    306 
    307   /* Internal variables */
    308   UCHAR k0, k2, i;
    309   UCHAR num_bands0 = 0;
    310   UCHAR num_bands1 = 0;
    311   UCHAR diff_tot[MAX_OCTAVE + MAX_SECOND_REGION];
    312   UCHAR *diff0 = diff_tot;
    313   UCHAR *diff1 = diff_tot + MAX_OCTAVE;
    314   INT k2_achived;
    315   INT k2_diff;
    316   INT incr = 0;
    317 
    318   /*
    319     Determine start band
    320   */
    321   if (flags & SBRDEC_QUAD_RATE) {
    322     fs >>= 1;
    323   }
    324 
    325   k0 = getStartBand(fs, hHeaderData->bs_data.startFreq, flags);
    326   if (k0 == 255) {
    327     return SBRDEC_UNSUPPORTED_CONFIG;
    328   }
    329 
    330   /*
    331     Determine stop band
    332   */
    333   k2 = getStopBand(fs, hHeaderData->bs_data.stopFreq, flags, k0);
    334   if (k2 == 255) {
    335     return SBRDEC_UNSUPPORTED_CONFIG;
    336   }
    337 
    338   if (hHeaderData->bs_data.freqScale > 0) { /* Bark */
    339     INT k1;
    340 
    341     if (hHeaderData->bs_data.freqScale == 1) {
    342       bpo_div16 = FL2FXCONST_SGL(12.0f / 16.0f);
    343     } else if (hHeaderData->bs_data.freqScale == 2) {
    344       bpo_div16 = FL2FXCONST_SGL(10.0f / 16.0f);
    345     } else {
    346       bpo_div16 = FL2FXCONST_SGL(8.0f / 16.0f);
    347     }
    348 
    349     /* Ref: ISO/IEC 23003-3, Figure 12 - Flowchart calculation of fMaster for
    350      * 4:1 system when bs_freq_scale > 0 */
    351     if (flags & SBRDEC_QUAD_RATE) {
    352       if ((SHORT)k0 < (SHORT)(bpo_div16 >> ((FRACT_BITS - 1) - 4))) {
    353         bpo_div16 = (FIXP_SGL)(k0 & (UCHAR)0xfe)
    354                     << ((FRACT_BITS - 1) - 4); /* bpo_div16 = floor(k0/2)*2 */
    355       }
    356     }
    357 
    358     if (1000 * k2 > 2245 * k0) { /* Two or more regions */
    359       k1 = 2 * k0;
    360 
    361       num_bands0 = numberOfBands(bpo_div16, k0, k1, 0);
    362       num_bands1 =
    363           numberOfBands(bpo_div16, k1, k2, hHeaderData->bs_data.alterScale);
    364       if (num_bands0 < 1) {
    365         return SBRDEC_UNSUPPORTED_CONFIG;
    366       }
    367       if (num_bands1 < 1) {
    368         return SBRDEC_UNSUPPORTED_CONFIG;
    369       }
    370 
    371       CalcBands(diff0, k0, k1, num_bands0);
    372       shellsort(diff0, num_bands0);
    373       if (diff0[0] == 0) {
    374         return SBRDEC_UNSUPPORTED_CONFIG;
    375       }
    376 
    377       cumSum(k0, diff0, num_bands0, v_k_master);
    378 
    379       CalcBands(diff1, k1, k2, num_bands1);
    380       shellsort(diff1, num_bands1);
    381       if (diff0[num_bands0 - 1] > diff1[0]) {
    382         SBR_ERROR err;
    383 
    384         err = modifyBands(diff0[num_bands0 - 1], diff1, num_bands1);
    385         if (err) return SBRDEC_UNSUPPORTED_CONFIG;
    386       }
    387 
    388       /* Add 2nd region */
    389       cumSum(k1, diff1, num_bands1, &v_k_master[num_bands0]);
    390       *numMaster = num_bands0 + num_bands1; /* Output nr of bands */
    391 
    392     } else { /* Only one region */
    393       k1 = k2;
    394 
    395       num_bands0 = numberOfBands(bpo_div16, k0, k1, 0);
    396       if (num_bands0 < 1) {
    397         return SBRDEC_UNSUPPORTED_CONFIG;
    398       }
    399       CalcBands(diff0, k0, k1, num_bands0);
    400       shellsort(diff0, num_bands0);
    401       if (diff0[0] == 0) {
    402         return SBRDEC_UNSUPPORTED_CONFIG;
    403       }
    404 
    405       cumSum(k0, diff0, num_bands0, v_k_master);
    406       *numMaster = num_bands0; /* Output nr of bands */
    407     }
    408   } else { /* Linear mode */
    409     if (hHeaderData->bs_data.alterScale == 0) {
    410       dk = 1;
    411       /* FLOOR to get to few number of bands (next lower even number) */
    412       num_bands0 = (k2 - k0) & 254;
    413     } else {
    414       dk = 2;
    415       num_bands0 = (((k2 - k0) >> 1) + 1) & 254; /* ROUND to the closest fit */
    416     }
    417 
    418     if (num_bands0 < 1) {
    419       return SBRDEC_UNSUPPORTED_CONFIG;
    420       /* We must return already here because 'i' can become negative below. */
    421     }
    422 
    423     k2_achived = k0 + num_bands0 * dk;
    424     k2_diff = k2 - k2_achived;
    425 
    426     for (i = 0; i < num_bands0; i++) diff_tot[i] = dk;
    427 
    428     /* If linear scale wasn't achieved */
    429     /* and we got too wide SBR area */
    430     if (k2_diff < 0) {
    431       incr = 1;
    432       i = 0;
    433     }
    434 
    435     /* If linear scale wasn't achieved */
    436     /* and we got too small SBR area */
    437     if (k2_diff > 0) {
    438       incr = -1;
    439       i = num_bands0 - 1;
    440     }
    441 
    442     /* Adjust diff vector to get sepc. SBR range */
    443     while (k2_diff != 0) {
    444       diff_tot[i] = diff_tot[i] - incr;
    445       i = i + incr;
    446       k2_diff = k2_diff + incr;
    447     }
    448 
    449     cumSum(k0, diff_tot, num_bands0, v_k_master); /* cumsum */
    450     *numMaster = num_bands0;                      /* Output nr of bands */
    451   }
    452 
    453   if (*numMaster < 1) {
    454     return SBRDEC_UNSUPPORTED_CONFIG;
    455   }
    456 
    457   /* Ref: ISO/IEC 23003-3 Cor.3, "In 7.5.5.2, add to the requirements:"*/
    458   if (flags & SBRDEC_QUAD_RATE) {
    459     int k;
    460     for (k = 1; k < *numMaster; k++) {
    461       if (!(v_k_master[k] - v_k_master[k - 1] <= k0 - 2)) {
    462         return SBRDEC_UNSUPPORTED_CONFIG;
    463       }
    464     }
    465   }
    466 
    467   /*
    468     Print out the calculated table
    469   */
    470 
    471   return SBRDEC_OK;
    472 }
    473 
    474 /*!
    475   \brief     Calculate frequency ratio of one SBR band
    476 
    477   All SBR bands should span a constant frequency range in the logarithmic
    478   domain. This function calculates the ratio of any SBR band's upper and lower
    479   frequency.
    480 
    481  \return    num_band-th root of k_start/k_stop
    482 */
    483 static FIXP_SGL calcFactorPerBand(int k_start, int k_stop, int num_bands) {
    484   /* Scaled bandfactor and step 1 bit right to avoid overflow
    485    * use double data type */
    486   FIXP_DBL bandfactor = FL2FXCONST_DBL(0.25f); /* Start value */
    487   FIXP_DBL step = FL2FXCONST_DBL(0.125f); /* Initial increment for factor */
    488 
    489   int direction = 1;
    490 
    491   /* Because saturation can't be done in INT IIS,
    492    * changed start and stop data type from FIXP_SGL to FIXP_DBL */
    493   FIXP_DBL start = k_start << (DFRACT_BITS - 8);
    494   FIXP_DBL stop = k_stop << (DFRACT_BITS - 8);
    495 
    496   FIXP_DBL temp;
    497 
    498   int j, i = 0;
    499 
    500   while (step > FL2FXCONST_DBL(0.0f)) {
    501     i++;
    502     temp = stop;
    503 
    504     /* Calculate temp^num_bands: */
    505     for (j = 0; j < num_bands; j++)
    506       // temp = fMult(temp,bandfactor);
    507       temp = fMultDiv2(temp, bandfactor) << 2;
    508 
    509     if (temp < start) { /* Factor too strong, make it weaker */
    510       if (direction == 0)
    511         /* Halfen step. Right shift is not done as fract because otherwise the
    512            lowest bit cannot be cleared due to rounding */
    513         step = (FIXP_DBL)((LONG)step >> 1);
    514       direction = 1;
    515       bandfactor = bandfactor + step;
    516     } else { /* Factor is too weak: make it stronger */
    517       if (direction == 1) step = (FIXP_DBL)((LONG)step >> 1);
    518       direction = 0;
    519       bandfactor = bandfactor - step;
    520     }
    521 
    522     if (i > 100) {
    523       step = FL2FXCONST_DBL(0.0f);
    524     }
    525   }
    526   return FX_DBL2FX_SGL(bandfactor << 1);
    527 }
    528 
    529 /*!
    530   \brief     Calculate number of SBR bands between start and stop band
    531 
    532   Given the number of bands per octave, this function calculates how many
    533   bands fit in the given frequency range.
    534   When the warpFlag is set, the 'band density' is decreased by a factor
    535   of 1/1.3
    536 
    537   \return    number of bands
    538 */
    539 static int numberOfBands(
    540     FIXP_SGL bpo_div16, /*!< Input: number of bands per octave divided by 16 */
    541     int start,          /*!< First QMF band of SBR frequency range */
    542     int stop,           /*!< Last QMF band of SBR frequency range + 1 */
    543     int warpFlag)       /*!< Stretching flag */
    544 {
    545   FIXP_SGL num_bands_div128;
    546   int num_bands;
    547 
    548   num_bands_div128 =
    549       FX_DBL2FX_SGL(fMult(FDK_getNumOctavesDiv8(start, stop), bpo_div16));
    550 
    551   if (warpFlag) {
    552     /* Apply the warp factor of 1.3 to get wider bands.  We use a value
    553        of 32768/25200 instead of the exact value to avoid critical cases
    554        of rounding.
    555     */
    556     num_bands_div128 = FX_DBL2FX_SGL(
    557         fMult(num_bands_div128, FL2FXCONST_SGL(25200.0 / 32768.0)));
    558   }
    559 
    560   /* add scaled 1 for rounding to even numbers: */
    561   num_bands_div128 = num_bands_div128 + FL2FXCONST_SGL(1.0f / 128.0f);
    562   /* scale back to right aligned integer and double the value: */
    563   num_bands = 2 * ((LONG)num_bands_div128 >> (FRACT_BITS - 7));
    564 
    565   return (num_bands);
    566 }
    567 
    568 /*!
    569   \brief     Calculate width of SBR bands
    570 
    571   Given the desired number of bands within the SBR frequency range,
    572   this function calculates the width of each SBR band in QMF channels.
    573   The bands get wider from start to stop (bark scale).
    574 */
    575 static void CalcBands(UCHAR *diff,     /*!< Vector of widths to be calculated */
    576                       UCHAR start,     /*!< Lower end of subband range */
    577                       UCHAR stop,      /*!< Upper end of subband range */
    578                       UCHAR num_bands) /*!< Desired number of bands */
    579 {
    580   int i;
    581   int previous;
    582   int current;
    583   FIXP_SGL exact, temp;
    584   FIXP_SGL bandfactor = calcFactorPerBand(start, stop, num_bands);
    585 
    586   previous = stop; /* Start with highest QMF channel */
    587   exact = (FIXP_SGL)(
    588       stop << (FRACT_BITS - 8)); /* Shift left to gain some accuracy */
    589 
    590   for (i = num_bands - 1; i >= 0; i--) {
    591     /* Calculate border of next lower sbr band */
    592     exact = FX_DBL2FX_SGL(fMult(exact, bandfactor));
    593 
    594     /* Add scaled 0.5 for rounding:
    595        We use a value 128/256 instead of 0.5 to avoid some critical cases of
    596        rounding. */
    597     temp = exact + FL2FXCONST_SGL(128.0 / 32768.0);
    598 
    599     /* scale back to right alinged integer: */
    600     current = (LONG)temp >> (FRACT_BITS - 8);
    601 
    602     /* Save width of band i */
    603     diff[i] = previous - current;
    604     previous = current;
    605   }
    606 }
    607 
    608 /*!
    609   \brief     Calculate cumulated sum vector from delta vector
    610 */
    611 static void cumSum(UCHAR start_value, UCHAR *diff, UCHAR length,
    612                    UCHAR *start_adress) {
    613   int i;
    614   start_adress[0] = start_value;
    615   for (i = 1; i <= length; i++)
    616     start_adress[i] = start_adress[i - 1] + diff[i - 1];
    617 }
    618 
    619 /*!
    620   \brief     Adapt width of frequency bands in the second region
    621 
    622   If SBR spans more than 2 octaves, the upper part of a bark-frequency-scale
    623   is calculated separately. This function tries to avoid that the second region
    624   starts with a band smaller than the highest band of the first region.
    625 */
    626 static SBR_ERROR modifyBands(UCHAR max_band_previous, UCHAR *diff,
    627                              UCHAR length) {
    628   int change = max_band_previous - diff[0];
    629 
    630   /* Limit the change so that the last band cannot get narrower than the first
    631    * one */
    632   if (change > (diff[length - 1] - diff[0]) >> 1)
    633     change = (diff[length - 1] - diff[0]) >> 1;
    634 
    635   diff[0] += change;
    636   diff[length - 1] -= change;
    637   shellsort(diff, length);
    638 
    639   return SBRDEC_OK;
    640 }
    641 
    642 /*!
    643   \brief   Update high resolution frequency band table
    644 */
    645 static void sbrdecUpdateHiRes(UCHAR *h_hires, UCHAR *num_hires,
    646                               UCHAR *v_k_master, UCHAR num_bands,
    647                               UCHAR xover_band) {
    648   UCHAR i;
    649 
    650   *num_hires = num_bands - xover_band;
    651 
    652   for (i = xover_band; i <= num_bands; i++) {
    653     h_hires[i - xover_band] = v_k_master[i];
    654   }
    655 }
    656 
    657 /*!
    658   \brief  Build low resolution table out of high resolution table
    659 */
    660 static void sbrdecUpdateLoRes(UCHAR *h_lores, UCHAR *num_lores, UCHAR *h_hires,
    661                               UCHAR num_hires) {
    662   UCHAR i;
    663 
    664   if ((num_hires & 1) == 0) {
    665     /* If even number of hires bands */
    666     *num_lores = num_hires >> 1;
    667     /* Use every second lores=hires[0,2,4...] */
    668     for (i = 0; i <= *num_lores; i++) h_lores[i] = h_hires[i * 2];
    669   } else {
    670     /* Odd number of hires, which means xover is odd */
    671     *num_lores = (num_hires + 1) >> 1;
    672     /* Use lores=hires[0,1,3,5 ...] */
    673     h_lores[0] = h_hires[0];
    674     for (i = 1; i <= *num_lores; i++) {
    675       h_lores[i] = h_hires[i * 2 - 1];
    676     }
    677   }
    678 }
    679 
    680 /*!
    681   \brief   Derive a low-resolution frequency-table from the master frequency
    682   table
    683 */
    684 void sbrdecDownSampleLoRes(UCHAR *v_result, UCHAR num_result,
    685                            UCHAR *freqBandTableRef, UCHAR num_Ref) {
    686   int step;
    687   int i, j;
    688   int org_length, result_length;
    689   int v_index[MAX_FREQ_COEFFS >> 1];
    690 
    691   /* init */
    692   org_length = num_Ref;
    693   result_length = num_result;
    694 
    695   v_index[0] = 0; /* Always use left border */
    696   i = 0;
    697   while (org_length > 0) {
    698     /* Create downsample vector */
    699     i++;
    700     step = org_length / result_length;
    701     org_length = org_length - step;
    702     result_length--;
    703     v_index[i] = v_index[i - 1] + step;
    704   }
    705 
    706   for (j = 0; j <= i; j++) {
    707     /* Use downsample vector to index LoResolution vector */
    708     v_result[j] = freqBandTableRef[v_index[j]];
    709   }
    710 }
    711 
    712 /*!
    713   \brief   Sorting routine
    714 */
    715 void shellsort(UCHAR *in, UCHAR n) {
    716   int i, j, v, w;
    717   int inc = 1;
    718 
    719   do
    720     inc = 3 * inc + 1;
    721   while (inc <= n);
    722 
    723   do {
    724     inc = inc / 3;
    725     for (i = inc; i < n; i++) {
    726       v = in[i];
    727       j = i;
    728       while ((w = in[j - inc]) > v) {
    729         in[j] = w;
    730         j -= inc;
    731         if (j < inc) break;
    732       }
    733       in[j] = v;
    734     }
    735   } while (inc > 1);
    736 }
    737 
    738 /*!
    739   \brief   Reset frequency band tables
    740   \return  errorCode, 0 if successful
    741 */
    742 SBR_ERROR
    743 resetFreqBandTables(HANDLE_SBR_HEADER_DATA hHeaderData, const UINT flags) {
    744   SBR_ERROR err = SBRDEC_OK;
    745   int k2, kx, lsb, usb;
    746   int intTemp;
    747   UCHAR nBandsLo, nBandsHi;
    748   HANDLE_FREQ_BAND_DATA hFreq = &hHeaderData->freqBandData;
    749 
    750   /* Calculate master frequency function */
    751   err = sbrdecUpdateFreqScale(hFreq->v_k_master, &hFreq->numMaster,
    752                               hHeaderData->sbrProcSmplRate, hHeaderData, flags);
    753 
    754   if (err || (hHeaderData->bs_info.xover_band > hFreq->numMaster)) {
    755     return SBRDEC_UNSUPPORTED_CONFIG;
    756   }
    757 
    758   /* Derive Hiresolution from master frequency function */
    759   sbrdecUpdateHiRes(hFreq->freqBandTable[1], &nBandsHi, hFreq->v_k_master,
    760                     hFreq->numMaster, hHeaderData->bs_info.xover_band);
    761   /* Derive Loresolution from Hiresolution */
    762   sbrdecUpdateLoRes(hFreq->freqBandTable[0], &nBandsLo, hFreq->freqBandTable[1],
    763                     nBandsHi);
    764 
    765   hFreq->nSfb[0] = nBandsLo;
    766   hFreq->nSfb[1] = nBandsHi;
    767 
    768   /* Check index to freqBandTable[0] */
    769   if (!(nBandsLo > 0) ||
    770       (nBandsLo > (((hHeaderData->numberOfAnalysisBands == 16)
    771                         ? MAX_FREQ_COEFFS_QUAD_RATE
    772                         : MAX_FREQ_COEFFS_DUAL_RATE) >>
    773                    1))) {
    774     return SBRDEC_UNSUPPORTED_CONFIG;
    775   }
    776 
    777   lsb = hFreq->freqBandTable[0][0];
    778   usb = hFreq->freqBandTable[0][nBandsLo];
    779 
    780   /* Check for start frequency border k_x:
    781      - ISO/IEC 14496-3 4.6.18.3.6 Requirements
    782      - ISO/IEC 23003-3 7.5.5.2    Modifications and additions to the MPEG-4 SBR
    783      tool
    784   */
    785   /* Note that lsb > as hHeaderData->numberOfAnalysisBands is a valid SBR config
    786    * for 24 band QMF analysis. */
    787   if ((lsb > ((flags & SBRDEC_QUAD_RATE) ? 16 : (32))) || (lsb >= usb)) {
    788     return SBRDEC_UNSUPPORTED_CONFIG;
    789   }
    790 
    791   /* Calculate number of noise bands */
    792 
    793   k2 = hFreq->freqBandTable[1][nBandsHi];
    794   kx = hFreq->freqBandTable[1][0];
    795 
    796   if (hHeaderData->bs_data.noise_bands == 0) {
    797     hFreq->nNfb = 1;
    798   } else /* Calculate no of noise bands 1,2 or 3 bands/octave */
    799   {
    800     /* Fetch number of octaves divided by 32 */
    801     intTemp = (LONG)FDK_getNumOctavesDiv8(kx, k2) >> 2;
    802 
    803     /* Integer-Multiplication with number of bands: */
    804     intTemp = intTemp * hHeaderData->bs_data.noise_bands;
    805 
    806     /* Add scaled 0.5 for rounding: */
    807     intTemp = intTemp + (LONG)FL2FXCONST_SGL(0.5f / 32.0f);
    808 
    809     /* Convert to right-aligned integer: */
    810     intTemp = intTemp >> (FRACT_BITS - 1 /*sign*/ - 5 /* rescale */);
    811 
    812     if (intTemp == 0) intTemp = 1;
    813 
    814     hFreq->nNfb = intTemp;
    815   }
    816 
    817   hFreq->nInvfBands = hFreq->nNfb;
    818 
    819   if (hFreq->nNfb > MAX_NOISE_COEFFS) {
    820     return SBRDEC_UNSUPPORTED_CONFIG;
    821   }
    822 
    823   /* Get noise bands */
    824   sbrdecDownSampleLoRes(hFreq->freqBandTableNoise, hFreq->nNfb,
    825                         hFreq->freqBandTable[0], nBandsLo);
    826 
    827   /* save old highband; required for overlap in usac
    828      when headerchange occurs at XVAR and VARX frame; */
    829   hFreq->ov_highSubband = hFreq->highSubband;
    830 
    831   hFreq->lowSubband = lsb;
    832   hFreq->highSubband = usb;
    833 
    834   return SBRDEC_OK;
    835 }
    836