Home | History | Annotate | Download | only in src
      1 
      2 /* -----------------------------------------------------------------------------------------------------------
      3 Software License for The Fraunhofer FDK AAC Codec Library for Android
      4 
      5  Copyright  1995 - 2013 Fraunhofer-Gesellschaft zur Frderung der angewandten Forschung e.V.
      6   All rights reserved.
      7 
      8  1.    INTRODUCTION
      9 The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
     10 the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
     11 This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
     12 
     13 AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
     14 audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
     15 independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
     16 of the MPEG specifications.
     17 
     18 Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
     19 may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
     20 individually for the purpose of encoding or decoding bit streams in products that are compliant with
     21 the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
     22 these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
     23 software may already be covered under those patent licenses when it is used for those licensed purposes only.
     24 
     25 Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
     26 are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
     27 applications information and documentation.
     28 
     29 2.    COPYRIGHT LICENSE
     30 
     31 Redistribution and use in source and binary forms, with or without modification, are permitted without
     32 payment of copyright license fees provided that you satisfy the following conditions:
     33 
     34 You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
     35 your modifications thereto in source code form.
     36 
     37 You must retain the complete text of this software license in the documentation and/or other materials
     38 provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
     39 You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
     40 modifications thereto to recipients of copies in binary form.
     41 
     42 The name of Fraunhofer may not be used to endorse or promote products derived from this library without
     43 prior written permission.
     44 
     45 You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
     46 software or your modifications thereto.
     47 
     48 Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
     49 and the date of any change. For modified versions of the FDK AAC Codec, the term
     50 "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
     51 "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
     52 
     53 3.    NO PATENT LICENSE
     54 
     55 NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
     56 ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
     57 respect to this software.
     58 
     59 You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
     60 by appropriate patent licenses.
     61 
     62 4.    DISCLAIMER
     63 
     64 This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
     65 "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
     66 of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     67 CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
     68 including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
     69 or business interruption, however caused and on any theory of liability, whether in contract, strict
     70 liability, or tort (including negligence), arising in any way out of the use of this software, even if
     71 advised of the possibility of such damage.
     72 
     73 5.    CONTACT INFORMATION
     74 
     75 Fraunhofer Institute for Integrated Circuits IIS
     76 Attention: Audio and Multimedia Departments - FDK AAC LL
     77 Am Wolfsmantel 33
     78 91058 Erlangen, Germany
     79 
     80 www.iis.fraunhofer.de/amm
     81 amm-info (at) iis.fraunhofer.de
     82 ----------------------------------------------------------------------------------------------------------- */
     83 
     84 #include "code_env.h"
     85 #include "sbr_rom.h"
     86 
     87 /*****************************************************************************
     88 
     89  functionname: FDKsbrEnc_InitSbrHuffmanTables
     90  description:  initializes Huffman Tables dependent on chosen amp_res
     91  returns:      error handle
     92  input:
     93  output:
     94 
     95 *****************************************************************************/
     96 INT
     97 FDKsbrEnc_InitSbrHuffmanTables (HANDLE_SBR_ENV_DATA       sbrEnvData,
     98                       HANDLE_SBR_CODE_ENVELOPE  henv,
     99                       HANDLE_SBR_CODE_ENVELOPE  hnoise,
    100                       AMP_RES                   amp_res)
    101 {
    102   if ( (!henv)  ||  (!hnoise)  || (!sbrEnvData) )
    103     return (1); /* not init. */
    104 
    105   sbrEnvData->init_sbr_amp_res = amp_res;
    106 
    107   switch (amp_res) {
    108   case  SBR_AMP_RES_3_0:
    109     /*envelope data*/
    110 
    111     /*Level/Pan - coding */
    112     sbrEnvData->hufftableLevelTimeC   = v_Huff_envelopeLevelC11T;
    113     sbrEnvData->hufftableLevelTimeL   = v_Huff_envelopeLevelL11T;
    114     sbrEnvData->hufftableBalanceTimeC = bookSbrEnvBalanceC11T;
    115     sbrEnvData->hufftableBalanceTimeL = bookSbrEnvBalanceL11T;
    116 
    117     sbrEnvData->hufftableLevelFreqC   = v_Huff_envelopeLevelC11F;
    118     sbrEnvData->hufftableLevelFreqL   = v_Huff_envelopeLevelL11F;
    119     sbrEnvData->hufftableBalanceFreqC = bookSbrEnvBalanceC11F;
    120     sbrEnvData->hufftableBalanceFreqL = bookSbrEnvBalanceL11F;
    121 
    122     /*Right/Left - coding */
    123     sbrEnvData->hufftableTimeC        = v_Huff_envelopeLevelC11T;
    124     sbrEnvData->hufftableTimeL        = v_Huff_envelopeLevelL11T;
    125     sbrEnvData->hufftableFreqC        = v_Huff_envelopeLevelC11F;
    126     sbrEnvData->hufftableFreqL        = v_Huff_envelopeLevelL11F;
    127 
    128     sbrEnvData->codeBookScfLavBalance  = CODE_BOOK_SCF_LAV_BALANCE11;
    129     sbrEnvData->codeBookScfLav         = CODE_BOOK_SCF_LAV11;
    130 
    131     sbrEnvData->si_sbr_start_env_bits           = SI_SBR_START_ENV_BITS_AMP_RES_3_0;
    132     sbrEnvData->si_sbr_start_env_bits_balance   = SI_SBR_START_ENV_BITS_BALANCE_AMP_RES_3_0;
    133     break;
    134 
    135   case SBR_AMP_RES_1_5:
    136    /*envelope data*/
    137 
    138     /*Level/Pan - coding */
    139     sbrEnvData->hufftableLevelTimeC   = v_Huff_envelopeLevelC10T;
    140     sbrEnvData->hufftableLevelTimeL   = v_Huff_envelopeLevelL10T;
    141     sbrEnvData->hufftableBalanceTimeC = bookSbrEnvBalanceC10T;
    142     sbrEnvData->hufftableBalanceTimeL = bookSbrEnvBalanceL10T;
    143 
    144     sbrEnvData->hufftableLevelFreqC   = v_Huff_envelopeLevelC10F;
    145     sbrEnvData->hufftableLevelFreqL   = v_Huff_envelopeLevelL10F;
    146     sbrEnvData->hufftableBalanceFreqC = bookSbrEnvBalanceC10F;
    147     sbrEnvData->hufftableBalanceFreqL = bookSbrEnvBalanceL10F;
    148 
    149     /*Right/Left - coding */
    150     sbrEnvData->hufftableTimeC        = v_Huff_envelopeLevelC10T;
    151     sbrEnvData->hufftableTimeL        = v_Huff_envelopeLevelL10T;
    152     sbrEnvData->hufftableFreqC        = v_Huff_envelopeLevelC10F;
    153     sbrEnvData->hufftableFreqL        = v_Huff_envelopeLevelL10F;
    154 
    155     sbrEnvData->codeBookScfLavBalance = CODE_BOOK_SCF_LAV_BALANCE10;
    156     sbrEnvData->codeBookScfLav = CODE_BOOK_SCF_LAV10;
    157 
    158     sbrEnvData->si_sbr_start_env_bits           = SI_SBR_START_ENV_BITS_AMP_RES_1_5;
    159     sbrEnvData->si_sbr_start_env_bits_balance   = SI_SBR_START_ENV_BITS_BALANCE_AMP_RES_1_5;
    160     break;
    161 
    162   default:
    163     return (1); /* undefined amp_res mode */
    164   }
    165 
    166   /* these are common to both amp_res values */
    167   /*Noise data*/
    168 
    169   /*Level/Pan - coding */
    170   sbrEnvData->hufftableNoiseLevelTimeC   = v_Huff_NoiseLevelC11T;
    171   sbrEnvData->hufftableNoiseLevelTimeL   = v_Huff_NoiseLevelL11T;
    172   sbrEnvData->hufftableNoiseBalanceTimeC = bookSbrNoiseBalanceC11T;
    173   sbrEnvData->hufftableNoiseBalanceTimeL = bookSbrNoiseBalanceL11T;
    174 
    175   sbrEnvData->hufftableNoiseLevelFreqC   = v_Huff_envelopeLevelC11F;
    176   sbrEnvData->hufftableNoiseLevelFreqL   = v_Huff_envelopeLevelL11F;
    177   sbrEnvData->hufftableNoiseBalanceFreqC = bookSbrEnvBalanceC11F;
    178   sbrEnvData->hufftableNoiseBalanceFreqL = bookSbrEnvBalanceL11F;
    179 
    180 
    181   /*Right/Left - coding */
    182   sbrEnvData->hufftableNoiseTimeC        = v_Huff_NoiseLevelC11T;
    183   sbrEnvData->hufftableNoiseTimeL        = v_Huff_NoiseLevelL11T;
    184   sbrEnvData->hufftableNoiseFreqC        = v_Huff_envelopeLevelC11F;
    185   sbrEnvData->hufftableNoiseFreqL        = v_Huff_envelopeLevelL11F;
    186 
    187   sbrEnvData->si_sbr_start_noise_bits         = SI_SBR_START_NOISE_BITS_AMP_RES_3_0;
    188   sbrEnvData->si_sbr_start_noise_bits_balance = SI_SBR_START_NOISE_BITS_BALANCE_AMP_RES_3_0;
    189 
    190 
    191   /* init envelope tables and codebooks */
    192   henv->codeBookScfLavBalanceTime = sbrEnvData->codeBookScfLavBalance;
    193   henv->codeBookScfLavBalanceFreq = sbrEnvData->codeBookScfLavBalance;
    194   henv->codeBookScfLavLevelTime = sbrEnvData->codeBookScfLav;
    195   henv->codeBookScfLavLevelFreq = sbrEnvData->codeBookScfLav;
    196   henv->codeBookScfLavTime = sbrEnvData->codeBookScfLav;
    197   henv->codeBookScfLavFreq = sbrEnvData->codeBookScfLav;
    198 
    199   henv->hufftableLevelTimeL = sbrEnvData->hufftableLevelTimeL;
    200   henv->hufftableBalanceTimeL = sbrEnvData->hufftableBalanceTimeL;
    201   henv->hufftableTimeL = sbrEnvData->hufftableTimeL;
    202   henv->hufftableLevelFreqL = sbrEnvData->hufftableLevelFreqL;
    203   henv->hufftableBalanceFreqL = sbrEnvData->hufftableBalanceFreqL;
    204   henv->hufftableFreqL = sbrEnvData->hufftableFreqL;
    205 
    206   henv->codeBookScfLavFreq = sbrEnvData->codeBookScfLav;
    207   henv->codeBookScfLavTime = sbrEnvData->codeBookScfLav;
    208 
    209   henv->start_bits = sbrEnvData->si_sbr_start_env_bits;
    210   henv->start_bits_balance = sbrEnvData->si_sbr_start_env_bits_balance;
    211 
    212 
    213   /* init noise tables and codebooks */
    214 
    215   hnoise->codeBookScfLavBalanceTime = CODE_BOOK_SCF_LAV_BALANCE11;
    216   hnoise->codeBookScfLavBalanceFreq = CODE_BOOK_SCF_LAV_BALANCE11;
    217   hnoise->codeBookScfLavLevelTime = CODE_BOOK_SCF_LAV11;
    218   hnoise->codeBookScfLavLevelFreq = CODE_BOOK_SCF_LAV11;
    219   hnoise->codeBookScfLavTime = CODE_BOOK_SCF_LAV11;
    220   hnoise->codeBookScfLavFreq = CODE_BOOK_SCF_LAV11;
    221 
    222   hnoise->hufftableLevelTimeL = sbrEnvData->hufftableNoiseLevelTimeL;
    223   hnoise->hufftableBalanceTimeL = sbrEnvData->hufftableNoiseBalanceTimeL;
    224   hnoise->hufftableTimeL = sbrEnvData->hufftableNoiseTimeL;
    225   hnoise->hufftableLevelFreqL = sbrEnvData->hufftableNoiseLevelFreqL;
    226   hnoise->hufftableBalanceFreqL = sbrEnvData->hufftableNoiseBalanceFreqL;
    227   hnoise->hufftableFreqL = sbrEnvData->hufftableNoiseFreqL;
    228 
    229 
    230   hnoise->start_bits = sbrEnvData->si_sbr_start_noise_bits;
    231   hnoise->start_bits_balance = sbrEnvData->si_sbr_start_noise_bits_balance;
    232 
    233   /* No delta coding in time from the previous frame due to 1.5dB FIx-FIX rule */
    234   henv->upDate = 0;
    235   hnoise->upDate = 0;
    236   return  (0);
    237 }
    238 
    239 /*******************************************************************************
    240  Functionname:  indexLow2High
    241  *******************************************************************************
    242 
    243  Description:   Nice small patch-functions in order to cope with non-factor-2
    244                 ratios between high-res and low-res
    245 
    246  Arguments:     INT offset, INT index, FREQ_RES res
    247 
    248  Return:        INT
    249 
    250 *******************************************************************************/
    251 static INT indexLow2High(INT offset, INT index, FREQ_RES res)
    252 {
    253 
    254   if(res == FREQ_RES_LOW)
    255   {
    256     if (offset >= 0)
    257     {
    258         if (index < offset)
    259           return(index);
    260         else
    261           return(2*index - offset);
    262     }
    263     else
    264     {
    265         offset = -offset;
    266         if (index < offset)
    267           return(2*index+index);
    268         else
    269           return(2*index + offset);
    270     }
    271   }
    272   else
    273     return(index);
    274 }
    275 
    276 
    277 
    278 /*******************************************************************************
    279  Functionname:  mapLowResEnergyVal
    280  *******************************************************************************
    281 
    282  Description:
    283 
    284  Arguments:     INT currVal,INT* prevData, INT offset, INT index, FREQ_RES res
    285 
    286  Return:        none
    287 
    288 *******************************************************************************/
    289 static void mapLowResEnergyVal(SCHAR currVal, SCHAR* prevData, INT offset, INT index, FREQ_RES res)
    290 {
    291 
    292   if(res == FREQ_RES_LOW)
    293   {
    294     if (offset >= 0)
    295     {
    296         if(index < offset)
    297             prevData[index] = currVal;
    298         else
    299         {
    300             prevData[2*index - offset] = currVal;
    301             prevData[2*index+1 - offset] = currVal;
    302         }
    303     }
    304     else
    305     {
    306         offset = -offset;
    307         if (index < offset)
    308         {
    309             prevData[3*index] = currVal;
    310             prevData[3*index+1] = currVal;
    311             prevData[3*index+2] = currVal;
    312         }
    313         else
    314         {
    315             prevData[2*index + offset] = currVal;
    316             prevData[2*index + 1 + offset] = currVal;
    317         }
    318     }
    319   }
    320   else
    321     prevData[index] = currVal;
    322 }
    323 
    324 
    325 
    326 /*******************************************************************************
    327  Functionname:  computeBits
    328  *******************************************************************************
    329 
    330  Description:
    331 
    332  Arguments:     INT delta,
    333                 INT codeBookScfLavLevel,
    334                 INT codeBookScfLavBalance,
    335                 const UCHAR * hufftableLevel,
    336                 const UCHAR * hufftableBalance, INT coupling, INT channel)
    337 
    338  Return:        INT
    339 
    340 *******************************************************************************/
    341 static INT
    342 computeBits (SCHAR *delta,
    343              INT codeBookScfLavLevel,
    344              INT codeBookScfLavBalance,
    345              const UCHAR * hufftableLevel,
    346              const UCHAR * hufftableBalance, INT coupling, INT channel)
    347 {
    348   INT index;
    349   INT delta_bits = 0;
    350 
    351   if (coupling) {
    352     if (channel == 1)
    353       {
    354         if (*delta < 0)
    355           index = fixMax(*delta, -codeBookScfLavBalance);
    356         else
    357           index = fixMin(*delta,  codeBookScfLavBalance);
    358 
    359         if (index != *delta) {
    360           *delta = index;
    361           return (10000);
    362         }
    363 
    364         delta_bits = hufftableBalance[index + codeBookScfLavBalance];
    365       }
    366     else {
    367       if (*delta < 0)
    368         index = fixMax(*delta, -codeBookScfLavLevel);
    369       else
    370         index = fixMin(*delta,  codeBookScfLavLevel);
    371 
    372       if (index != *delta) {
    373         *delta = index;
    374         return (10000);
    375       }
    376       delta_bits = hufftableLevel[index + codeBookScfLavLevel];
    377     }
    378   }
    379   else {
    380     if (*delta < 0)
    381       index = fixMax(*delta, -codeBookScfLavLevel);
    382     else
    383       index = fixMin(*delta,  codeBookScfLavLevel);
    384 
    385     if (index != *delta) {
    386       *delta = index;
    387       return (10000);
    388     }
    389     delta_bits = hufftableLevel[index + codeBookScfLavLevel];
    390   }
    391 
    392   return (delta_bits);
    393 }
    394 
    395 
    396 
    397 
    398 /*******************************************************************************
    399  Functionname:  FDKsbrEnc_codeEnvelope
    400  *******************************************************************************
    401 
    402  Description:
    403 
    404  Arguments:     INT *sfb_nrg,
    405                 const FREQ_RES *freq_res,
    406                 SBR_CODE_ENVELOPE * h_sbrCodeEnvelope,
    407                 INT *directionVec, INT scalable, INT nEnvelopes, INT channel,
    408                 INT headerActive)
    409 
    410  Return:        none
    411                 h_sbrCodeEnvelope->sfb_nrg_prev is modified !
    412                 sfb_nrg is modified
    413                 h_sbrCodeEnvelope->update is modfied !
    414                 *directionVec is modified
    415 
    416 *******************************************************************************/
    417 void
    418 FDKsbrEnc_codeEnvelope(SCHAR             *sfb_nrg,
    419                        const FREQ_RES    *freq_res,
    420                        SBR_CODE_ENVELOPE *h_sbrCodeEnvelope,
    421                        INT               *directionVec,
    422                        INT                coupling,
    423                        INT                nEnvelopes,
    424                        INT                channel,
    425                        INT                headerActive)
    426 {
    427   INT i, no_of_bands, band;
    428   FIXP_DBL tmp1,tmp2,tmp3,dF_edge_1stEnv;
    429   SCHAR *ptr_nrg;
    430 
    431   INT codeBookScfLavLevelTime;
    432   INT codeBookScfLavLevelFreq;
    433   INT codeBookScfLavBalanceTime;
    434   INT codeBookScfLavBalanceFreq;
    435   const UCHAR *hufftableLevelTimeL;
    436   const UCHAR *hufftableBalanceTimeL;
    437   const UCHAR *hufftableLevelFreqL;
    438   const UCHAR *hufftableBalanceFreqL;
    439 
    440   INT offset = h_sbrCodeEnvelope->offset;
    441   INT envDataTableCompFactor;
    442 
    443   INT delta_F_bits = 0, delta_T_bits = 0;
    444   INT use_dT;
    445 
    446   SCHAR delta_F[MAX_FREQ_COEFFS];
    447   SCHAR delta_T[MAX_FREQ_COEFFS];
    448   SCHAR last_nrg, curr_nrg;
    449 
    450   tmp1 = FL2FXCONST_DBL(0.5f) >> (DFRACT_BITS-16-1);
    451   tmp2 = h_sbrCodeEnvelope->dF_edge_1stEnv >> (DFRACT_BITS-16);
    452   tmp3 = (FIXP_DBL)(((INT)(LONG)h_sbrCodeEnvelope->dF_edge_incr*h_sbrCodeEnvelope->dF_edge_incr_fac) >> (DFRACT_BITS-16));
    453 
    454   dF_edge_1stEnv = tmp1 + tmp2 + tmp3;
    455 
    456   if (coupling) {
    457     codeBookScfLavLevelTime = h_sbrCodeEnvelope->codeBookScfLavLevelTime;
    458     codeBookScfLavLevelFreq = h_sbrCodeEnvelope->codeBookScfLavLevelFreq;
    459     codeBookScfLavBalanceTime = h_sbrCodeEnvelope->codeBookScfLavBalanceTime;
    460     codeBookScfLavBalanceFreq = h_sbrCodeEnvelope->codeBookScfLavBalanceFreq;
    461     hufftableLevelTimeL = h_sbrCodeEnvelope->hufftableLevelTimeL;
    462     hufftableBalanceTimeL = h_sbrCodeEnvelope->hufftableBalanceTimeL;
    463     hufftableLevelFreqL = h_sbrCodeEnvelope->hufftableLevelFreqL;
    464     hufftableBalanceFreqL = h_sbrCodeEnvelope->hufftableBalanceFreqL;
    465   }
    466   else {
    467     codeBookScfLavLevelTime = h_sbrCodeEnvelope->codeBookScfLavTime;
    468     codeBookScfLavLevelFreq = h_sbrCodeEnvelope->codeBookScfLavFreq;
    469     codeBookScfLavBalanceTime = h_sbrCodeEnvelope->codeBookScfLavTime;
    470     codeBookScfLavBalanceFreq = h_sbrCodeEnvelope->codeBookScfLavFreq;
    471     hufftableLevelTimeL = h_sbrCodeEnvelope->hufftableTimeL;
    472     hufftableBalanceTimeL = h_sbrCodeEnvelope->hufftableTimeL;
    473     hufftableLevelFreqL = h_sbrCodeEnvelope->hufftableFreqL;
    474     hufftableBalanceFreqL = h_sbrCodeEnvelope->hufftableFreqL;
    475   }
    476 
    477   if(coupling == 1 && channel == 1)
    478     envDataTableCompFactor = 1;       /*should be one when the new huffman-tables are ready*/
    479   else
    480     envDataTableCompFactor = 0;
    481 
    482 
    483   if (h_sbrCodeEnvelope->deltaTAcrossFrames == 0)
    484     h_sbrCodeEnvelope->upDate = 0;
    485 
    486   /* no delta coding in time in case of a header */
    487   if (headerActive)
    488     h_sbrCodeEnvelope->upDate = 0;
    489 
    490 
    491   for (i = 0; i < nEnvelopes; i++)
    492   {
    493     if (freq_res[i] == FREQ_RES_HIGH)
    494       no_of_bands = h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH];
    495     else
    496       no_of_bands = h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW];
    497 
    498 
    499     ptr_nrg = sfb_nrg;
    500     curr_nrg = *ptr_nrg;
    501 
    502     delta_F[0] = curr_nrg >> envDataTableCompFactor;
    503 
    504     if (coupling && channel == 1)
    505       delta_F_bits = h_sbrCodeEnvelope->start_bits_balance;
    506     else
    507       delta_F_bits = h_sbrCodeEnvelope->start_bits;
    508 
    509 
    510     if(h_sbrCodeEnvelope->upDate != 0)
    511     {
    512       delta_T[0] = (curr_nrg - h_sbrCodeEnvelope->sfb_nrg_prev[0]) >> envDataTableCompFactor;
    513 
    514       delta_T_bits = computeBits (&delta_T[0],
    515                                   codeBookScfLavLevelTime,
    516                                   codeBookScfLavBalanceTime,
    517                                   hufftableLevelTimeL,
    518                                   hufftableBalanceTimeL, coupling, channel);
    519     }
    520 
    521 
    522     mapLowResEnergyVal(curr_nrg, h_sbrCodeEnvelope->sfb_nrg_prev, offset, 0, freq_res[i]);
    523 
    524     /* ensure that nrg difference is not higher than codeBookScfLavXXXFreq */
    525     if ( coupling && channel == 1 ) {
    526       for (band = no_of_bands - 1; band > 0; band--) {
    527         if ( ptr_nrg[band] - ptr_nrg[band-1] > codeBookScfLavBalanceFreq ) {
    528           ptr_nrg[band-1] = ptr_nrg[band] - codeBookScfLavBalanceFreq;
    529         }
    530       }
    531       for (band = 1; band < no_of_bands; band++) {
    532         if ( ptr_nrg[band-1] - ptr_nrg[band] > codeBookScfLavBalanceFreq ) {
    533           ptr_nrg[band] = ptr_nrg[band-1] - codeBookScfLavBalanceFreq;
    534         }
    535       }
    536     }
    537     else {
    538       for (band = no_of_bands - 1; band > 0; band--) {
    539         if ( ptr_nrg[band] - ptr_nrg[band-1] > codeBookScfLavLevelFreq ) {
    540           ptr_nrg[band-1] = ptr_nrg[band] - codeBookScfLavLevelFreq;
    541         }
    542       }
    543       for (band = 1; band < no_of_bands; band++) {
    544         if ( ptr_nrg[band-1] - ptr_nrg[band] > codeBookScfLavLevelFreq ) {
    545           ptr_nrg[band] = ptr_nrg[band-1] - codeBookScfLavLevelFreq;
    546         }
    547       }
    548     }
    549 
    550 
    551     /* Coding loop*/
    552     for (band = 1; band < no_of_bands; band++)
    553     {
    554       last_nrg = (*ptr_nrg);
    555       ptr_nrg++;
    556       curr_nrg = (*ptr_nrg);
    557 
    558       delta_F[band] = (curr_nrg - last_nrg) >> envDataTableCompFactor;
    559 
    560       delta_F_bits += computeBits (&delta_F[band],
    561                                    codeBookScfLavLevelFreq,
    562                                    codeBookScfLavBalanceFreq,
    563                                    hufftableLevelFreqL,
    564                                    hufftableBalanceFreqL, coupling, channel);
    565 
    566       if(h_sbrCodeEnvelope->upDate != 0)
    567       {
    568         delta_T[band] = curr_nrg - h_sbrCodeEnvelope->sfb_nrg_prev[indexLow2High(offset, band, freq_res[i])];
    569         delta_T[band] = delta_T[band] >> envDataTableCompFactor;
    570       }
    571 
    572       mapLowResEnergyVal(curr_nrg, h_sbrCodeEnvelope->sfb_nrg_prev, offset, band, freq_res[i]);
    573 
    574       if(h_sbrCodeEnvelope->upDate != 0)
    575       {
    576         delta_T_bits += computeBits (&delta_T[band],
    577                                      codeBookScfLavLevelTime,
    578                                      codeBookScfLavBalanceTime,
    579                                      hufftableLevelTimeL,
    580                                      hufftableBalanceTimeL, coupling, channel);
    581       }
    582     }
    583 
    584     /* Replace sfb_nrg with deltacoded samples and set flag */
    585     if (i == 0) {
    586       INT tmp_bits;
    587       tmp_bits = (((delta_T_bits * dF_edge_1stEnv) >> (DFRACT_BITS-18)) + (FIXP_DBL)1) >> 1;
    588       use_dT = (h_sbrCodeEnvelope->upDate != 0 && (delta_F_bits > tmp_bits));
    589     }
    590     else
    591       use_dT = (delta_T_bits < delta_F_bits && h_sbrCodeEnvelope->upDate != 0);
    592 
    593     if (use_dT)
    594     {
    595       directionVec[i] = TIME;
    596       FDKmemcpy (sfb_nrg, delta_T, no_of_bands * sizeof (SCHAR));
    597     }
    598     else {
    599       h_sbrCodeEnvelope->upDate = 0;
    600       directionVec[i] = FREQ;
    601       FDKmemcpy (sfb_nrg, delta_F, no_of_bands * sizeof (SCHAR));
    602     }
    603     sfb_nrg += no_of_bands;
    604     h_sbrCodeEnvelope->upDate = 1;
    605   }
    606 
    607 }
    608 
    609 
    610 /*******************************************************************************
    611  Functionname:  FDKsbrEnc_InitSbrCodeEnvelope
    612  *******************************************************************************
    613 
    614  Description:
    615 
    616  Arguments:
    617 
    618  Return:
    619 
    620 *******************************************************************************/
    621 INT
    622 FDKsbrEnc_InitSbrCodeEnvelope (HANDLE_SBR_CODE_ENVELOPE  h_sbrCodeEnvelope,
    623                        INT *nSfb,
    624                        INT deltaTAcrossFrames,
    625                        FIXP_DBL dF_edge_1stEnv,
    626                        FIXP_DBL dF_edge_incr)
    627 {
    628 
    629   FDKmemclear(h_sbrCodeEnvelope,sizeof(SBR_CODE_ENVELOPE));
    630 
    631   h_sbrCodeEnvelope->deltaTAcrossFrames = deltaTAcrossFrames;
    632   h_sbrCodeEnvelope->dF_edge_1stEnv = dF_edge_1stEnv;
    633   h_sbrCodeEnvelope->dF_edge_incr = dF_edge_incr;
    634   h_sbrCodeEnvelope->dF_edge_incr_fac = 0;
    635   h_sbrCodeEnvelope->upDate = 0;
    636   h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW] = nSfb[FREQ_RES_LOW];
    637   h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH] = nSfb[FREQ_RES_HIGH];
    638   h_sbrCodeEnvelope->offset = 2*h_sbrCodeEnvelope->nSfb[FREQ_RES_LOW] - h_sbrCodeEnvelope->nSfb[FREQ_RES_HIGH];
    639 
    640   return (0);
    641 }
    642