LCOV - code coverage report
Current view: top level - lib_enc - swb_bwe_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 641 703 91.2 %
Date: 2025-05-23 08:37:30 Functions: 17 18 94.4 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "rom_enc.h"
      47             : #include "basop_util.h"
      48             : #include "basop_proto_func.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*---------------------------------------------------------------------*
      52             :  * Local function prototypes
      53             :  *---------------------------------------------------------------------*/
      54             : 
      55             : static int16_t SWB_BWE_encoding( Encoder_State *st, const float *insig, const float *insig_lp, const float *insig_hp, const float *synth, const float *yos, float *SWB_fenv, const float tilt_nb, const int16_t st_offset );
      56             : static void MSVQ_Interpol_Tran( float *SWB_env_energy, int16_t *indice );
      57             : static void calculate_tonality( const float *org, const float *gen, float *SFM_org, float *SFM_gen, const int16_t length );
      58             : static int16_t WB_BWE_encoding( Encoder_State *st, const float *yos, float *WB_fenv );
      59             : static void energy_control( Encoder_State *st, const int16_t core, const int16_t mode, const int16_t coder_type, const float *org, const int16_t offset, float *energy_factor );
      60             : static int16_t decision_hq_generic_class( const float *coefs, const int16_t hq_generic_offset );
      61             : 
      62             : 
      63             : /*-------------------------------------------------------------------*
      64             :  * wb_bwe_enc()
      65             :  *
      66             :  * WB BWE encoder
      67             :  *-------------------------------------------------------------------*/
      68             : 
      69       11573 : void wb_bwe_enc(
      70             :     Encoder_State *st,         /* i/o: encoder state structure                  */
      71             :     const float *new_wb_speech /* i  : original input signal at 16kHz           */
      72             : )
      73             : {
      74             :     FD_BWE_ENC_HANDLE hBWE_FD;
      75             :     float old_input[NS2SA( 16000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k];
      76             :     float *new_input;        /* pointer to original input signal         */
      77             :     float yorig[L_FRAME16k]; /* MDCT spectrum of weighted original       */
      78       11573 :     int16_t mode = 0;
      79             :     float wtda_old_input[2 * L_FRAME16k];
      80             :     float WB_fenv[SWB_FENV];
      81             :     int16_t Sample_Delay_WB_BWE;
      82             : 
      83       11573 :     hBWE_FD = st->hBWE_FD;
      84             : 
      85       11573 :     if ( st->extl_brate > 0 )
      86             :     {
      87             :         /*---------------------------------------------------------------------*
      88             :          * Delay the original input signal to be synchronized with ACELP core synthesis
      89             :          *---------------------------------------------------------------------*/
      90             : 
      91        4100 :         set_f( old_input, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k );
      92             : 
      93        4100 :         Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS );
      94             : 
      95        4100 :         new_input = old_input + Sample_Delay_WB_BWE;
      96        4100 :         mvr2r( hBWE_FD->old_input_wb, old_input, Sample_Delay_WB_BWE );
      97        4100 :         mvr2r( new_wb_speech, new_input, L_FRAME16k );
      98        4100 :         mvr2r( old_input + L_FRAME16k, hBWE_FD->old_input_wb, Sample_Delay_WB_BWE );
      99             : 
     100             :         /*---------------------------------------------------------------------*
     101             :          * WB BWE encoding
     102             :          *---------------------------------------------------------------------*/
     103             : 
     104             :         /* MDCT of the original signal */
     105        4100 :         wtda( old_input, wtda_old_input, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, L_FRAME16k );
     106             : 
     107        4100 :         direct_transform( wtda_old_input, yorig, 0, L_FRAME16k, st->element_mode );
     108             : 
     109        4100 :         mode = WB_BWE_encoding( st, yorig, WB_fenv );
     110             : 
     111        4100 :         push_indice( st->hBstr, IND_WB_CLASS, mode - 2, 1 );
     112             :     }
     113             : 
     114       11573 :     hBWE_FD->prev_mode = mode;
     115             : 
     116       11573 :     return;
     117             : }
     118             : 
     119             : /*-------------------------------------------------------------------*
     120             :  * get_normalize_spec()
     121             :  *
     122             :  *-------------------------------------------------------------------*/
     123             : 
     124       19010 : static void get_normalize_spec(
     125             :     const int16_t core,       /* i  : core selected            */
     126             :     const int16_t extl,       /* i  : extension layer selected */
     127             :     const int16_t mode,       /* i  : SHB BWE class            */
     128             :     const int16_t core_type,  /* i  : coding type              */
     129             :     const float *org,         /* i  : input spectrum           */
     130             :     float *SWB_signal,        /* o  : output spectrum          */
     131             :     int16_t *prev_L_swb_norm, /* i  : previous norm. len       */
     132             :     const int16_t offset      /* i  : frequency offset         */
     133             : )
     134             : {
     135             :     int16_t n_freq, L_swb_norm;
     136             :     float envelope[L_FRAME32k];
     137             :     int16_t frq_end;
     138             : 
     139       19010 :     set_f( SWB_signal, 0, HQ_GENERIC_HIGH0 + offset );
     140       19010 :     calc_normal_length( core, org, mode, extl, &L_swb_norm, prev_L_swb_norm );
     141             : 
     142       19010 :     if ( extl == SWB_BWE || extl == FB_BWE )
     143             :     {
     144       11841 :         if ( mode == HARMONIC )
     145             :         {
     146         270 :             mvr2r( org, &SWB_signal[240 + offset], 240 );
     147         270 :             mvr2r( &org[128], &SWB_signal[480 + offset], 80 );
     148             :         }
     149             :         else
     150             :         {
     151       11571 :             mvr2r( &org[112], &SWB_signal[240 + offset], 128 );
     152       11571 :             mvr2r( &org[112], &SWB_signal[368 + offset], 128 );
     153       11571 :             mvr2r( &org[176], &SWB_signal[496 + offset], 64 );
     154             :         }
     155       11841 :         frq_end = 560 + offset;
     156             :     }
     157        7169 :     else if ( extl == WB_BWE )
     158             :     {
     159        4100 :         if ( core_type == 0 )
     160             :         {
     161          11 :             mvr2r( &org[160], &SWB_signal[240], 80 );
     162             :         }
     163             :         else
     164             :         {
     165        4089 :             mvr2r( &org[80], &SWB_signal[240], 80 );
     166             :         }
     167        4100 :         frq_end = L_FRAME16k;
     168             :     }
     169             :     else
     170             :     {
     171        3069 :         mvr2r( org + HQ_GENERIC_OFFSET, SWB_signal + HQ_GENERIC_HIGH0 + offset, HQ_GENERIC_LEN0 );
     172        3069 :         mvr2r( org + HQ_GENERIC_OFFSET, SWB_signal + HQ_GENERIC_HIGH1 + offset, HQ_GENERIC_LEN0 );
     173        3069 :         if ( offset == HQ_GENERIC_FOFFSET_24K4 )
     174             :         {
     175        3069 :             mvr2r( org + HQ_GENERIC_LOW0, SWB_signal + HQ_GENERIC_HIGH2 + offset, HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
     176             :         }
     177        3069 :         frq_end = L_FRAME32k;
     178             :     }
     179             : 
     180             :     /* calculate envelope */
     181       19010 :     calc_norm_envelop( SWB_signal, envelope, L_swb_norm, frq_end - offset, offset );
     182             : 
     183             :     /* Normalize with envelope */
     184     5118210 :     for ( n_freq = swb_bwe_subband[0] + offset; n_freq < frq_end; n_freq++ )
     185             :     {
     186     5099200 :         SWB_signal[n_freq] /= envelope[n_freq];
     187             :     }
     188             : 
     189       19010 :     return;
     190             : }
     191             : 
     192             : /*-------------------------------------------------------------------*
     193             :  * WB_BWE_fenv_q()
     194             :  *
     195             :  * Scalar quantizer routine
     196             :  *-------------------------------------------------------------------*/
     197             : 
     198             : /*! r: quantized gain index */
     199        4100 : static int16_t WB_BWE_fenv_q(
     200             :     float *x,                /* i/o: energy of WB envelop  */
     201             :     const float *cb,         /* i  : quantizer codebook    */
     202             :     const int16_t cb_length, /* i  : length of codebook    */
     203             :     const int16_t cb_dim     /* i  : dimension of codebook */
     204             : )
     205             : {
     206        4100 :     int16_t i, j, indx = 0;
     207             :     float dist, min_dist;
     208        4100 :     const float *pit = cb;
     209             : 
     210        4100 :     min_dist = FLT_MAX;
     211      135300 :     for ( i = 0; i < cb_length; i++ )
     212             :     {
     213      131200 :         dist = 0.0f;
     214      393600 :         for ( j = 0; j < cb_dim; j++ )
     215             :         {
     216      262400 :             dist += ( x[j] - ( *pit ) ) * ( x[j] - ( *pit ) );
     217      262400 :             pit++;
     218             :         }
     219             : 
     220      131200 :         if ( dist < min_dist )
     221             :         {
     222       17644 :             min_dist = dist;
     223       17644 :             indx = i;
     224             :         }
     225             :     }
     226             : 
     227       12300 :     for ( j = 0; j < cb_dim; j++ )
     228             :     {
     229        8200 :         x[j] = cb[cb_dim * indx + j];
     230             :     }
     231             : 
     232        4100 :     return ( indx );
     233             : }
     234             : 
     235             : /*-------------------------------------------------------------------*
     236             :  * FD_BWE_class()
     237             :  *
     238             :  * classify signal of above 6.4kHz, can be used for WB/SWB switch
     239             :  *-------------------------------------------------------------------*/
     240             : 
     241             : /*! r: FD BWE class */
     242       15941 : static int16_t FD_BWE_class(
     243             :     const float *fSpectrum, /* i  : input spectrum      */
     244             :     const float fGain,      /* i  : global gain         */
     245             :     const float tilt_nb,    /* i  : BWE tilt            */
     246             :     Encoder_State *st       /* i/o: Encoder structure   */
     247             : )
     248             : {
     249       15941 :     int16_t i, j, k, noise, sharpMod = 0;
     250             :     float peak, mean[20], mag;
     251             :     float sharpPeak;
     252       15941 :     const float *input_hi = 0;
     253             :     float sharp;
     254       15941 :     float gain_tmp = 0;
     255             :     int16_t mode;
     256       15941 :     float meanH, mean_d = 0;
     257             :     int16_t sharplimit;
     258             :     int16_t numsharp;
     259             :     int16_t numharmonic;
     260             : 
     261       15941 :     mode = NORMAL;
     262       15941 :     k = 0;
     263       15941 :     noise = 0;
     264       15941 :     sharpPeak = 0;
     265       15941 :     numsharp = 0;
     266       15941 :     numharmonic = 4;
     267       15941 :     sharplimit = 10;
     268             : 
     269       15941 :     if ( st->extl == SWB_BWE || st->extl == FB_BWE )
     270             :     {
     271       11841 :         input_hi = &fSpectrum[256];
     272       11841 :         numsharp = NUM_SHARP;
     273       11841 :         if ( ( st->last_extl == SWB_BWE && st->extl == SWB_BWE ) || ( st->last_extl == FB_BWE && st->extl == FB_BWE ) )
     274             :         {
     275        9773 :             gain_tmp = fGain / ( st->hBWE_FD->prev_global_gain + EPSILON );
     276        9773 :             if ( st->hBWE_FD->prev_mode == TRANSIENT )
     277             :             {
     278          24 :                 numharmonic = numharmonic * 2;
     279             :             }
     280        9749 :             else if ( st->hBWE_FD->prev_mode == NORMAL || st->hBWE_FD->prev_mode == NOISE )
     281             :             {
     282        9567 :                 numharmonic = 3 * numharmonic / 2;
     283             :             }
     284             :         }
     285             :         else
     286             :         {
     287        2068 :             gain_tmp = 1;
     288        2068 :             if ( st->hBWE_FD->prev_mode == HARMONIC )
     289             :             {
     290           0 :                 numharmonic = numharmonic / 2;
     291           0 :                 sharplimit = sharplimit / 2;
     292             :             }
     293             :             else
     294             :             {
     295        2068 :                 numharmonic = numharmonic * 2;
     296        2068 :                 sharplimit = sharplimit * 2;
     297             :             }
     298             :         }
     299             :     }
     300        4100 :     else if ( st->extl == WB_BWE )
     301             :     {
     302        4100 :         input_hi = &fSpectrum[224];
     303        4100 :         numsharp = NUM_SHARP / 3;
     304        4100 :         if ( st->hBWE_FD->prev_mode == HARMONIC )
     305             :         {
     306        1462 :             numharmonic = numharmonic / 4;
     307             :         }
     308             :         else
     309             :         {
     310        2638 :             numharmonic = numharmonic / 2;
     311             :         }
     312        4100 :         if ( st->last_extl != WB_BWE )
     313             :         {
     314         110 :             if ( st->hBWE_FD->prev_mode == HARMONIC )
     315             :             {
     316           0 :                 sharplimit = sharplimit / 2;
     317             :             }
     318             :             else
     319             :             {
     320         110 :                 sharplimit = sharplimit * 2;
     321             :             }
     322             :         }
     323             :     }
     324             : 
     325       15941 :     meanH = EPSILON;
     326      134810 :     for ( i = 0; i < numsharp; i++ )
     327             :     {
     328      118869 :         peak = 0.0f;
     329      118869 :         mean[i] = 0;
     330             : 
     331     3922677 :         for ( j = 0; j < SHARP_WIDTH; j++ )
     332             :         {
     333     3803808 :             mag = (float) fabs( *input_hi );
     334     3803808 :             if ( mag > peak )
     335             :             {
     336      485986 :                 peak = mag;
     337             :             }
     338     3803808 :             mean[i] += mag;
     339     3803808 :             input_hi++;
     340             :         }
     341      118869 :         meanH += mean[i];
     342             : 
     343      118869 :         if ( mean[i] != peak )
     344             :         {
     345      118869 :             sharp = (float) ( peak * ( SHARP_WIDTH - 1 ) / ( mean[i] - peak ) );
     346             :         }
     347             :         else
     348             :         {
     349           0 :             sharp = 0.0f;
     350             :         }
     351             : 
     352      118869 :         if ( sharp > 4.5 && peak > 8 )
     353             :         {
     354       15630 :             k += 1;
     355             :         }
     356      103239 :         else if ( sharp < 3.0 )
     357             :         {
     358       43657 :             noise += 1;
     359             :         }
     360             : 
     361      118869 :         if ( sharp > sharpPeak )
     362             :         {
     363       39747 :             sharpPeak = sharp;
     364             :         }
     365             :     }
     366             : 
     367       15941 :     if ( st->extl == SWB_BWE || st->extl == FB_BWE )
     368             :     {
     369       11841 :         if ( k >= numharmonic && gain_tmp > 0.5f && gain_tmp < 1.8f && sharpPeak > sharplimit )
     370             :         {
     371         244 :             sharpMod = 1;
     372             :         }
     373             :         else
     374             :         {
     375       11597 :             sharpMod = 0;
     376             :         }
     377       11841 :         meanH /= 288;
     378       11841 :         mean_d = 0.0f;
     379      118410 :         for ( i = 0; i < NUM_SHARP; i++ )
     380             :         {
     381      106569 :             mean_d += (float) fabs( mean[i] / 32 - meanH );
     382             :         }
     383             :     }
     384        4100 :     else if ( st->extl == WB_BWE )
     385             :     {
     386        4100 :         if ( k >= numharmonic && sharpPeak > sharplimit )
     387             :         {
     388         996 :             sharpMod = 1;
     389             :         }
     390             :         else
     391             :         {
     392        3104 :             sharpMod = 0;
     393             :         }
     394             :     }
     395             : 
     396       15941 :     if ( sharpMod && st->hBWE_FD->modeCount < 12 )
     397             :     {
     398         894 :         st->hBWE_FD->modeCount++;
     399             :     }
     400       15047 :     else if ( sharpMod == 0 && st->hBWE_FD->modeCount > 0 )
     401             :     {
     402         686 :         st->hBWE_FD->modeCount--;
     403             :     }
     404             : 
     405       15941 :     if ( st->hBWE_FD->modeCount >= 2 )
     406             :     {
     407        1533 :         sharpMod = 1;
     408             :     }
     409             : 
     410       15941 :     if ( sharpMod )
     411             :     {
     412        1738 :         mode = HARMONIC;
     413             :     }
     414       14203 :     else if ( st->extl == SWB_BWE || st->extl == FB_BWE )
     415             :     {
     416       11571 :         if ( noise > 4 && mean_d < 4.8f * meanH && tilt_nb < 5 )
     417             :         {
     418         811 :             mode = NOISE;
     419             :         }
     420             :     }
     421             : 
     422       15941 :     return mode;
     423             : }
     424             : 
     425             : /*-------------------------------------------------------------------*
     426             :  * WB_BWE_encoding()
     427             :  *
     428             :  * WB BWE main encoder
     429             :  *-------------------------------------------------------------------*/
     430             : 
     431             : /*! r: classification of wb signal */
     432        4100 : static int16_t WB_BWE_encoding(
     433             :     Encoder_State *st, /* i/o: Encoder structure                      */
     434             :     const float *yos,  /* i  : MDCT coefficients of weighted original */
     435             :     float *WB_fenv     /* i/o: energy of WB envelope                  */
     436             : )
     437             : {
     438             :     int16_t mode;
     439             :     float global_gain;
     440             :     float energy;
     441             :     int16_t i, n_coeff, n_band;
     442             :     int16_t index;
     443             :     float energy_factor[4];
     444             : 
     445             :     /* Energy for the different bands and global energies */
     446        4100 :     global_gain = EPSILON;
     447             : 
     448       12300 :     for ( i = 0, n_band = 0; i < 2; i++ )
     449             :     {
     450        8200 :         energy = EPSILON;
     451      336200 :         for ( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ )
     452             :         {
     453      328000 :             energy += yos[n_coeff] * yos[n_coeff];
     454             :         }
     455             : 
     456        8200 :         WB_fenv[i] = energy;
     457        8200 :         n_band += 2;
     458        8200 :         global_gain += energy;
     459             :     }
     460             : 
     461        4100 :     mode = FD_BWE_class( yos, global_gain, 0, st );
     462             : 
     463        4100 :     energy_control( st, ACELP_CORE, mode, st->coder_type, yos, 0, energy_factor );
     464             : 
     465       12300 :     for ( i = 0; i < 2; i++ )
     466             :     {
     467        8200 :         WB_fenv[i] = (float) ( log10( WB_fenv[i] * energy_factor[i << 1] / 40 ) * FAC_LOG2 );
     468             :     }
     469             : 
     470        4100 :     index = WB_BWE_fenv_q( WB_fenv, F_2_5, 32, 2 );
     471             : 
     472        4100 :     push_indice( st->hBstr, IND_WB_FENV, index, 5 );
     473             : 
     474             : 
     475        4100 :     return ( mode );
     476             : }
     477             : 
     478             : 
     479             : /*-------------------------------------------------------------------*
     480             :  * swb_bwe_enc()
     481             :  *
     482             :  * SWB BWE encoder (only for 32kHz signals)
     483             :  *-------------------------------------------------------------------*/
     484             : 
     485       11922 : void swb_bwe_enc(
     486             :     Encoder_State *st,               /* i/o: encoder state structure                 */
     487             :     const int16_t last_element_mode, /* i  : last element mode                       */
     488             :     const float *old_input_12k8,     /* i  : input signal @12.8kHz for SWB BWE       */
     489             :     const float *old_input_16k,      /* i  : input signal @16kHz for SWB BWE         */
     490             :     const float *old_syn_12k8_16k,   /* i  : ACELP core synthesis at 12.8kHz or 16kHz*/
     491             :     const float *new_swb_speech,     /* i  : original input signal at 32kHz          */
     492             :     const float *shb_speech          /* i  : SHB target signal (6-14kHz) at 16kHz    */
     493             : )
     494             : {
     495             :     int16_t i, inner_frame, idxGain;
     496             :     TD_BWE_ENC_HANDLE hBWE_TD;
     497             :     FD_BWE_ENC_HANDLE hBWE_FD;
     498             :     float *new_input;
     499             :     int32_t inner_Fs;
     500             :     float old_input[NS2SA( 48000, DELAY_FD_BWE_ENC_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME48k];
     501             :     float old_input_lp[L_FRAME16k], new_input_hp[L_FRAME16k];
     502             :     float yorig[L_FRAME48k];
     503             :     float wtda_old_input[2 * L_FRAME48k];
     504             :     float SWB_fenv[SWB_FENV];
     505             :     float tilt_nb;
     506             :     int16_t Sample_Delay_SWB_BWE, Sample_Delay_HP, Sample_Delay_LP;
     507             :     float ener_low, energy_fbe_fb, fb_ener_adjust, ener_adjust_quan;
     508             :     int16_t fb_band_begin;
     509             : 
     510       11922 :     hBWE_TD = st->hBWE_TD;
     511       11922 :     hBWE_FD = st->hBWE_FD;
     512             : 
     513       11922 :     ener_adjust_quan = 0.0f;
     514       11922 :     idxGain = 0;
     515             : 
     516             :     /*---------------------------------------------------------------------*
     517             :      * Delay the original input signal to be synchronized with ACELP core synthesis
     518             :      *---------------------------------------------------------------------*/
     519             : 
     520       11922 :     if ( st->extl == FB_BWE )
     521             :     {
     522        4771 :         inner_frame = L_FRAME48k;
     523        4771 :         inner_Fs = 48000;
     524             :     }
     525             :     else
     526             :     {
     527        7151 :         inner_frame = L_FRAME32k;
     528        7151 :         inner_Fs = 32000;
     529             :     }
     530             : 
     531       11922 :     set_f( old_input, 0.0f, NS2SA( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + inner_frame );
     532             : 
     533       11922 :     if ( st->L_frame == L_FRAME )
     534             :     {
     535        7399 :         Sample_Delay_SWB_BWE = NS2SA( inner_Fs, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS );
     536        7399 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     537        7399 :         if ( st->element_mode == IVAS_CPE_TD )
     538             :         {
     539          17 :             Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS );
     540             :         }
     541        7399 :         Sample_Delay_LP = NS2SA( 12800, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS );
     542             : 
     543        7399 :         if ( st->element_mode > EVS_MONO )
     544             :         {
     545        7393 :             Sample_Delay_SWB_BWE -= NS2SA( inner_Fs, DELAY_FIR_RESAMPL_NS );
     546        7393 :             Sample_Delay_HP -= NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
     547             : 
     548        7393 :             if ( st->element_mode == IVAS_CPE_DFT )
     549             :             {
     550        2119 :                 mvr2r( old_input_12k8 + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
     551        2119 :                 mvr2r( hBWE_TD->old_speech_shb + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp, Sample_Delay_HP );
     552             :             }
     553             :         }
     554             : 
     555        7399 :         mvr2r( hBWE_FD->old_input_lp, old_input_lp, Sample_Delay_LP );
     556        7399 :         mvr2r( old_input_12k8 + L_INP_MEM, &old_input_lp[Sample_Delay_LP], L_FRAME - Sample_Delay_LP );
     557        7399 :         mvr2r( old_input_12k8 + L_INP_MEM + L_FRAME - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
     558             :     }
     559             :     else
     560             :     {
     561        4523 :         Sample_Delay_SWB_BWE = NS2SA( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS );
     562        4523 :         Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS );
     563        4523 :         if ( st->element_mode == IVAS_CPE_TD )
     564             :         {
     565           0 :             Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS );
     566             :         }
     567        4523 :         Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS );
     568             : 
     569        4523 :         if ( st->element_mode > EVS_MONO )
     570             :         {
     571        4523 :             Sample_Delay_SWB_BWE -= NS2SA( inner_Fs, DELAY_FIR_RESAMPL_NS );
     572        4523 :             Sample_Delay_HP -= NS2SA( 16000, DELAY_FIR_RESAMPL_NS );
     573             : 
     574        4523 :             if ( st->element_mode == IVAS_CPE_DFT )
     575             :             {
     576         523 :                 mvr2r( old_input_16k + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
     577         523 :                 mvr2r( hBWE_TD->old_speech_shb + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp, Sample_Delay_HP );
     578             :             }
     579             :         }
     580             : 
     581        4523 :         mvr2r( hBWE_FD->old_input_lp, old_input_lp, Sample_Delay_LP );
     582        4523 :         mvr2r( old_input_16k + L_INP_MEM, &old_input_lp[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP );
     583        4523 :         mvr2r( old_input_16k + L_INP_MEM + L_FRAME16k - Sample_Delay_LP, hBWE_FD->old_input_lp, Sample_Delay_LP );
     584             :     }
     585             : 
     586       11922 :     mvr2r( hBWE_FD->new_input_hp, new_input_hp, Sample_Delay_HP );
     587       11922 :     mvr2r( shb_speech, new_input_hp + Sample_Delay_HP, L_FRAME16k - Sample_Delay_HP );
     588       11922 :     mvr2r( shb_speech + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp, Sample_Delay_HP );
     589             : 
     590       11922 :     new_input = old_input + Sample_Delay_SWB_BWE;
     591       11922 :     mvr2r( hBWE_FD->old_input, old_input, Sample_Delay_SWB_BWE );
     592       11922 :     mvr2r( new_swb_speech, new_input, inner_frame );
     593       11922 :     mvr2r( old_input + inner_frame, hBWE_FD->old_input, Sample_Delay_SWB_BWE );
     594             : 
     595             :     /*----------------------------------------------------------------------*
     596             :      * Calculate tilt of the input signal and the ACELP core synthesis
     597             :      *----------------------------------------------------------------------*/
     598             : 
     599       11922 :     calc_tilt_bwe( old_input_lp, &tilt_nb, L_FRAME );
     600             : 
     601             :     /*---------------------------------------------------------------------*
     602             :      * SWB BWE encoding
     603             :      * FB BWE encoding
     604             :      *---------------------------------------------------------------------*/
     605             : 
     606       11922 :     if ( st->idchan == 1 && last_element_mode == IVAS_CPE_DFT )
     607             :     {
     608           0 :         for ( i = 0; i < inner_frame; i++ )
     609             :         {
     610           0 :             hBWE_FD->old_wtda_swb[i] *= (float) i / (float) inner_frame;
     611             :         }
     612             :     }
     613             : 
     614             :     /* windowing of the original input signal */
     615       11922 :     wtda( old_input, wtda_old_input, hBWE_FD->old_wtda_swb, ALDO_WINDOW, ALDO_WINDOW, inner_frame );
     616             : 
     617             :     /* DCT of the original input signal */
     618       11922 :     direct_transform( wtda_old_input, yorig, 0, inner_frame, st->element_mode );
     619             : 
     620             :     /* high-band gain control in case of BWS */
     621       11922 :     if ( st->bwidth_sw_cnt > 0 )
     622             :     {
     623          10 :         v_multc( yorig + L_FRAME16k, (float) ( st->bwidth_sw_cnt ) / (float) BWS_TRAN_PERIOD, yorig + L_FRAME16k, inner_frame - L_FRAME16k );
     624             :     }
     625             : 
     626             :     /* FB BWE encoding */
     627       11922 :     if ( st->extl == FB_BWE )
     628             :     {
     629        4771 :         fb_band_begin = FB_BAND_BEGIN;
     630        4771 :         if ( st->L_frame == L_FRAME )
     631             :         {
     632         743 :             fb_band_begin = FB_BAND_BEGIN_12k8;
     633             :         }
     634             : 
     635        4771 :         energy_fbe_fb = sum2_f( yorig + fb_band_begin, FB_BAND_WIDTH ) + EPSILON;
     636        4771 :         ener_low = EPSILON;
     637      863551 :         for ( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ )
     638             :         {
     639      858780 :             ener_low += yorig[i] * yorig[i];
     640             :         }
     641             : 
     642        4771 :         fb_ener_adjust = (float) sqrt( energy_fbe_fb / ener_low );
     643        4771 :         fb_ener_adjust = min( fb_ener_adjust, FB_MAX_GAIN_VAR );
     644        4771 :         idxGain = (int16_t) usquant( fb_ener_adjust, &ener_adjust_quan, FB_GAIN_QLOW, FB_GAIN_QDELTA, 1 << NUM_BITS_FB_FRAMEGAIN );
     645             :     }
     646             : 
     647             :     /* SWB BWE encoding */
     648       11922 :     if ( st->L_frame == L_FRAME16k )
     649             :     {
     650        4523 :         SWB_BWE_encoding( st, old_input, old_input_lp, new_input_hp, old_syn_12k8_16k, yorig, SWB_fenv, tilt_nb, 80 );
     651             :     }
     652             :     else
     653             :     {
     654        7399 :         SWB_BWE_encoding( st, old_input, old_input_lp, new_input_hp, old_syn_12k8_16k, yorig, SWB_fenv, tilt_nb, 6 );
     655             :     }
     656             : 
     657             :     /* write FB BWE frame gain to the bitstream */
     658       11922 :     if ( st->extl == FB_BWE )
     659             :     {
     660        4771 :         push_indice( st->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN );
     661             :     }
     662             : 
     663       11922 :     return;
     664             : }
     665             : 
     666             : 
     667             : /*-------------------------------------------------------------------*
     668             :  * Freq_weights()
     669             :  *
     670             :  *-------------------------------------------------------------------*/
     671             : 
     672       14910 : static void freq_weights(
     673             :     const float Band_Ener[],   /* i  : Band energy                 */
     674             :     const float f_weighting[], /* i  : weigting coefs.             */
     675             :     float w_env[],             /* o  : Freq. weighting             */
     676             :     const int16_t Nbands       /* i  : Number of bands             */
     677             : )
     678             : {
     679             :     int16_t i;
     680             :     float tmp, w1[SWB_FENV], w2[SWB_FENV];
     681             :     float min_b, max_b;
     682             : 
     683             :     /* Find Max band energy */
     684       14910 :     min_b = Band_Ener[0];
     685       14910 :     max_b = Band_Ener[0];
     686      208740 :     for ( i = 1; i < Nbands; i++ )
     687             :     {
     688      193830 :         if ( Band_Ener[i] < min_b )
     689             :         {
     690       60733 :             min_b = Band_Ener[i];
     691             :         }
     692      193830 :         if ( Band_Ener[i] > max_b )
     693             :         {
     694       20608 :             max_b = Band_Ener[i];
     695             :         }
     696             :     }
     697             : 
     698             :     /* Find weighting function */
     699       14910 :     tmp = 1.f / ( max_b - min_b );
     700      223650 :     for ( i = 0; i < Nbands; i++ )
     701             :     {
     702      208740 :         w1[i] = ( Band_Ener[i] - min_b ) * tmp + 1.f; /*1<= var <=2 */
     703      208740 :         w2[i] = f_weighting[i];                       /*1~0.75*/
     704      208740 :         w_env[i] = w1[i] * w2[i];
     705             :     }
     706             : 
     707       14910 :     return;
     708             : }
     709             : 
     710             : 
     711             : /*-------------------------------------------------------------------*
     712             :  * VQwithCand_w()
     713             :  *
     714             :  *-------------------------------------------------------------------*/
     715             : 
     716       14991 : static void vqWithCand_w(
     717             :     const float *x,                /* i  : input vector                                */
     718             :     const float *E_ROM_dico,       /* i  : codebook                                    */
     719             :     const int16_t dim,             /* i  : codebook dimension                          */
     720             :     const int16_t E_ROM_dico_size, /* i  : codebook size                               */
     721             :     int16_t *index,                /* o  : survivors indices                           */
     722             :     const int16_t surv,            /* i  : survivor number                             */
     723             :     float dist_min[],              /* o  : minimum distortion                          */
     724             :     const float *w,                /* i  : weighting                                   */
     725             :     const int16_t flag             /* i  : flag indicationg weighted distortion metric */
     726             : )
     727             : {
     728             :     int16_t i, j, k, l;
     729             :     const float *p_E_ROM_dico;
     730             :     float dist, temp1;
     731             : 
     732       14991 :     if ( flag )
     733             :     {
     734       14910 :         set_f( dist_min, 3.402823466e+38F, surv ); /* FLT_MAX */
     735             : 
     736       44730 :         for ( i = 0; i < surv; i++ )
     737             :         {
     738       29820 :             index[i] = i;
     739             :         }
     740             : 
     741       14910 :         p_E_ROM_dico = E_ROM_dico;
     742             : 
     743      492030 :         for ( i = 0; i < E_ROM_dico_size; i++ )
     744             :         {
     745      477120 :             dist = x[0] - *p_E_ROM_dico++;
     746      477120 :             dist *= ( dist * w[0] );
     747             : 
     748     3339840 :             for ( j = 1; j < dim; j++ )
     749             :             {
     750     2862720 :                 temp1 = x[j] - *p_E_ROM_dico++;
     751     2862720 :                 dist += temp1 * temp1 * w[j];
     752             :             }
     753             : 
     754     1277784 :             for ( k = 0; k < surv; k++ )
     755             :             {
     756      897962 :                 if ( dist < dist_min[k] )
     757             :                 {
     758      153576 :                     for ( l = surv - 1; l > k; l-- )
     759             :                     {
     760       56278 :                         dist_min[l] = dist_min[l - 1];
     761       56278 :                         index[l] = index[l - 1];
     762             :                     }
     763       97298 :                     dist_min[k] = dist;
     764       97298 :                     index[k] = i;
     765       97298 :                     break;
     766             :                 }
     767             :             }
     768             :         }
     769             :     }
     770             :     else
     771             :     {
     772          81 :         set_f( dist_min, 3.402823466e+38F, surv ); /* FLT_MAX */
     773             : 
     774         324 :         for ( i = 0; i < surv; i++ )
     775             :         {
     776         243 :             index[i] = i;
     777             :         }
     778             : 
     779          81 :         p_E_ROM_dico = E_ROM_dico;
     780             : 
     781       10449 :         for ( i = 0; i < E_ROM_dico_size; i++ )
     782             :         {
     783       10368 :             dist = x[0] - *p_E_ROM_dico++;
     784       10368 :             dist *= dist;
     785             : 
     786       20736 :             for ( j = 1; j < dim; j++ )
     787             :             {
     788       10368 :                 temp1 = x[j] - *p_E_ROM_dico++;
     789       10368 :                 dist += temp1 * temp1;
     790             :             }
     791             : 
     792       39276 :             for ( k = 0; k < surv; k++ )
     793             :             {
     794       29888 :                 if ( dist < dist_min[k] )
     795             :                 {
     796        2196 :                     for ( l = surv - 1; l > k; l-- )
     797             :                     {
     798        1216 :                         dist_min[l] = dist_min[l - 1];
     799        1216 :                         index[l] = index[l - 1];
     800             :                     }
     801         980 :                     dist_min[k] = dist;
     802         980 :                     index[k] = i;
     803         980 :                     break;
     804             :                 }
     805             :             }
     806             :         }
     807             :     }
     808             : 
     809       14991 :     return;
     810             : }
     811             : 
     812             : /*-------------------------------------------------------------------*
     813             :  * vqSimple_w()
     814             :  *
     815             :  *-------------------------------------------------------------------*/
     816             : 
     817      121832 : static int16_t vqSimple_w(
     818             :     const float *x,    /* i  : input for quantizer                         */
     819             :     float *y,          /* i  : quantized value                             */
     820             :     const float *cb,   /* i  : codebooks                                   */
     821             :     const float *w,    /* i  : weight                                      */
     822             :     const int16_t dim, /* i  : dimension                                   */
     823             :     const int16_t l,   /* i  : number of candidates                        */
     824             :     const int16_t flag /* i  : flag indicationg weighted distortion metric */
     825             : )
     826             : {
     827             :     int16_t i, j, index;
     828             :     const float *cbP;
     829             :     float dist_min, dist, temp;
     830             : 
     831      121832 :     index = 0;
     832      121832 :     dist_min = FLT_MAX;
     833      121832 :     cbP = cb;
     834             : 
     835      121832 :     if ( flag )
     836             :     {
     837     8707440 :         for ( i = 0; i < l; i++ )
     838             :         {
     839     8588160 :             dist = x[0] - *cbP++;
     840     8588160 :             dist *= ( dist * w[0] );
     841    29581440 :             for ( j = 1; j < dim; j++ )
     842             :             {
     843    20993280 :                 temp = x[j] - *cbP++;
     844    20993280 :                 dist += temp * temp * w[j];
     845             :             }
     846             : 
     847     8588160 :             if ( dist < dist_min )
     848             :             {
     849      545800 :                 dist_min = dist;
     850      545800 :                 index = i;
     851             :             }
     852             :         }
     853             :     }
     854             :     else
     855             :     {
     856       91992 :         for ( i = 0; i < l; i++ )
     857             :         {
     858       89440 :             dist = x[0] - *cbP++;
     859       89440 :             dist *= dist;
     860      252768 :             for ( j = 1; j < dim; j++ )
     861             :             {
     862      163328 :                 temp = x[j] - *cbP++;
     863      163328 :                 dist += temp * temp;
     864             :             }
     865             : 
     866       89440 :             if ( dist < dist_min )
     867             :             {
     868        9082 :                 dist_min = dist;
     869        9082 :                 index = i;
     870             :             }
     871             :         }
     872             :     }
     873             : 
     874             :     /* Reading the selected vector */
     875      121832 :     mvr2r( &cb[index * dim], y, dim );
     876             : 
     877      121832 :     return ( index );
     878             : }
     879             : 
     880             : /*-------------------------------------------------------------------*
     881             :  * MSVQ_Interpol_Tran()
     882             :  *
     883             :  *-------------------------------------------------------------------*/
     884             : 
     885          81 : static void MSVQ_Interpol_Tran(
     886             :     float *SWB_env_energy, /* i/o: (original/quantized) energy   */
     887             :     int16_t *indice        /* o  : quantized index               */
     888             : )
     889             : {
     890             :     int16_t k, n_band, candInd[N_CAND_TR], ind_tmp[2];
     891             :     float env_temp11[SWB_FENV_TRANS / 2], env_temp12[SWB_FENV_TRANS / 2];
     892             :     float dist, minDist, tmp_q;
     893             :     float quant_tmp[SWB_FENV_TRANS], quant_tmp2[SWB_FENV_TRANS];
     894             :     float distCand[N_CAND_TR], quant_select[SWB_FENV_TRANS];
     895             : 
     896             :     /* Extract target vector */
     897         243 :     for ( n_band = 0; n_band < DIM_TR1; n_band++ )
     898             :     {
     899         162 :         env_temp11[n_band] = SWB_env_energy[2 * n_band];
     900         162 :         env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
     901             :     }
     902             : 
     903          81 :     vqWithCand_w( env_temp11, Env_TR_Cdbk1, DIM_TR1, N_CB_TR1, candInd, N_CAND_TR, distCand, NULL, 0 );
     904             : 
     905          81 :     minDist = FLT_MAX;
     906         324 :     for ( k = 0; k < N_CAND_TR; k++ )
     907             :     {
     908         729 :         for ( n_band = 0; n_band < DIM_TR1; n_band++ )
     909             :         {
     910         486 :             quant_tmp[n_band] = Env_TR_Cdbk1[candInd[k] * DIM_TR1 + n_band];
     911             :         }
     912             : 
     913         486 :         for ( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
     914             :         {
     915         243 :             quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f );
     916             :         }
     917         243 :         quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band];
     918         243 :         ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, Env_TR_Cdbk2, NULL, DIM_TR2, N_CB_TR2, 0 );
     919             : 
     920         729 :         for ( n_band = 0; n_band < DIM_TR1; n_band++ )
     921             :         {
     922         486 :             quant_select[n_band * 2] = quant_tmp[n_band];
     923             :         }
     924             : 
     925         486 :         for ( n_band = 0; n_band < DIM_TR2 - 1; n_band++ )
     926             :         {
     927         243 :             quant_select[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
     928             :         }
     929         243 :         quant_select[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
     930             : 
     931         243 :         dist = 0.f;
     932        1215 :         for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
     933             :         {
     934         972 :             tmp_q = SWB_env_energy[n_band] - quant_select[n_band];
     935         972 :             dist += tmp_q * tmp_q;
     936             :         }
     937             : 
     938             :         /* Check optimal candidate */
     939         243 :         if ( dist < minDist )
     940             :         {
     941          83 :             minDist = dist;
     942          83 :             indice[0] = candInd[k];
     943          83 :             indice[1] = ind_tmp[0];
     944             :         }
     945             :     }
     946             : 
     947          81 :     return;
     948             : }
     949             : 
     950             : /*-------------------------------------------------------------------*
     951             :  * MSVQ_Interpol()
     952             :  *
     953             :  *-------------------------------------------------------------------*/
     954       14910 : static void msvq_interpol(
     955             :     float *SWB_env_energy, /* i/o: (original/quantized) energy */
     956             :     float *w_env,          /* i/o: weighting coffecients       */
     957             :     int16_t *indice        /* o  : quantized index             */
     958             : )
     959             : {
     960             :     int16_t k, n_band, candInd[N_CAND], ind_tmp[4];
     961             :     float dist, minDist, tmp_q;
     962             :     float env_temp11[SWB_FENV / 2], env_temp12[SWB_FENV / 2];
     963             :     float quant_tmp[SWB_FENV], quant_tmp1[SWB_FENV], quant_tmp2[SWB_FENV], distCand[N_CAND];
     964             :     float quant_select[SWB_FENV], w_env11[SWB_FENV / 2], w_env12[SWB_FENV / 2];
     965             :     float synth_energy[SWB_FENV];
     966             : 
     967             :     /* Extract target vector */
     968      119280 :     for ( n_band = 0; n_band < DIM11; n_band++ )
     969             :     {
     970      104370 :         env_temp11[n_band] = SWB_env_energy[2 * n_band];
     971      104370 :         env_temp12[n_band] = SWB_env_energy[2 * n_band + 1];
     972             : 
     973      104370 :         w_env11[n_band] = w_env[2 * n_band];
     974      104370 :         w_env12[n_band] = w_env[2 * n_band + 1];
     975             :     }
     976             : 
     977       14910 :     vqWithCand_w( env_temp11, EnvCdbk11, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
     978             : 
     979       14910 :     minDist = FLT_MAX;
     980       44730 :     for ( k = 0; k < N_CAND; k++ )
     981             :     {
     982      238560 :         for ( n_band = 0; n_band < DIM11; n_band++ )
     983             :         {
     984      208740 :             quant_tmp1[n_band] = EnvCdbk11[candInd[k] * DIM11 + n_band];
     985      208740 :             quant_tmp2[n_band] = env_temp11[n_band] - quant_tmp1[n_band];
     986             :         }
     987             : 
     988       29820 :         ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk1st, w_env11, DIM1ST, N_CB1ST, 1 );
     989       29820 :         ind_tmp[1] = vqSimple_w( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
     990             : 
     991             :         /* Extract vector for odd position */
     992      238560 :         for ( n_band = 0; n_band < DIM11; n_band++ )
     993             :         {
     994      208740 :             quant_tmp[n_band] = quant_tmp1[n_band] + quant_tmp2[n_band];
     995             :         }
     996             : 
     997      208740 :         for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
     998             :         {
     999      178920 :             quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f );
    1000             :         }
    1001             : 
    1002       29820 :         quant_tmp2[n_band] = env_temp12[n_band] - quant_tmp[n_band];
    1003             : 
    1004       29820 :         ind_tmp[2] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk3rd, w_env12, DIM3RD, N_CB3RD, 1 );
    1005       29820 :         ind_tmp[3] = vqSimple_w( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk4th, w_env12 + DIM3RD, DIM4TH, N_CB4TH, 1 );
    1006             : 
    1007      238560 :         for ( n_band = 0; n_band < DIM11; n_band++ )
    1008             :         {
    1009      208740 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1010             :         }
    1011             : 
    1012      208740 :         for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1013             :         {
    1014      178920 :             quant_select[n_band * 2 + 1] = ( ( quant_tmp[n_band] + quant_tmp[n_band + 1] ) / 2.f ) + quant_tmp2[n_band];
    1015             :         }
    1016       29820 :         quant_select[n_band * 2 + 1] = quant_tmp[n_band] + quant_tmp2[n_band];
    1017             : 
    1018       29820 :         dist = 0.f;
    1019      447300 :         for ( n_band = 0; n_band < SWB_FENV; n_band++ )
    1020             :         {
    1021      417480 :             tmp_q = SWB_env_energy[n_band] - quant_select[n_band];
    1022      417480 :             tmp_q = tmp_q * tmp_q;
    1023      417480 :             dist += tmp_q * w_env[n_band];
    1024             :         }
    1025             : 
    1026             :         /* Check optimal candidate */
    1027       29820 :         if ( dist < minDist )
    1028             :         {
    1029       18582 :             minDist = dist;
    1030       18582 :             mvr2r( quant_select, synth_energy, SWB_FENV );
    1031             : 
    1032       18582 :             indice[0] = candInd[k];
    1033       18582 :             indice[1] = ind_tmp[0];
    1034       18582 :             indice[2] = ind_tmp[1];
    1035       18582 :             indice[3] = ind_tmp[2];
    1036       18582 :             indice[4] = ind_tmp[3];
    1037             :         }
    1038             :     }
    1039             : 
    1040       14910 :     mvr2r( synth_energy, SWB_env_energy, SWB_FENV );
    1041             : 
    1042       14910 :     return;
    1043             : }
    1044             : 
    1045             : /*-------------------------------------------------------------------*
    1046             :  * msvq_interpol_2()
    1047             :  *
    1048             :  *
    1049             :  *-------------------------------------------------------------------*/
    1050             : 
    1051           0 : static void msvq_interpol_2(
    1052             :     float *hq_generic_fenv, /* i/o: (original/quantized) energy */
    1053             :     const float *w_env,     /* i  : weighting coffecients       */
    1054             :     int16_t *indice,        /* o  : quantized index             */
    1055             :     const int16_t nenv      /* i  : the number of envelopes     */
    1056             : )
    1057             : {
    1058             :     int16_t k, n_band, candInd[N_CAND], ind_tmp[4];
    1059             :     float dist, minDist, tmp_q;
    1060             :     float env_temp11[SWB_FENV / 2], env_temp12[SWB_FENV / 2];
    1061             :     float quant_tmp[SWB_FENV], quant_tmp1[SWB_FENV], quant_tmp2[SWB_FENV], distCand[N_CAND];
    1062             :     float quant_select[SWB_FENV], w_env11[SWB_FENV / 2], w_env12[SWB_FENV / 2];
    1063             :     float synth_energy[SWB_FENV];
    1064             : 
    1065             :     /* Extract target vector */
    1066           0 :     for ( n_band = 0; n_band < DIM11 - 1; n_band++ )
    1067             :     {
    1068           0 :         env_temp11[n_band] = hq_generic_fenv[2 * n_band];
    1069           0 :         w_env11[n_band] = w_env[2 * n_band];
    1070             :     }
    1071           0 :     env_temp11[DIM11 - 1] = hq_generic_fenv[2 * ( DIM11 - 2 ) + 1];
    1072           0 :     w_env11[DIM11 - 1] = w_env[2 * ( DIM11 - 2 ) + 1];
    1073             : 
    1074           0 :     env_temp12[0] = hq_generic_fenv[0];
    1075           0 :     w_env12[0] = w_env[0];
    1076           0 :     for ( n_band = 1; n_band < DIM11 - 1; n_band++ )
    1077             :     {
    1078           0 :         env_temp12[n_band] = hq_generic_fenv[2 * n_band - 1];
    1079           0 :         w_env12[n_band] = w_env[2 * n_band - 1];
    1080             :     }
    1081             : 
    1082           0 :     vqWithCand_w( env_temp11, EnvCdbk11, DIM11, N_CB11, candInd, N_CAND, distCand, w_env11, 1 );
    1083             : 
    1084           0 :     minDist = FLT_MAX;
    1085           0 :     for ( k = 0; k < N_CAND; k++ )
    1086             :     {
    1087           0 :         for ( n_band = 0; n_band < DIM11; n_band++ )
    1088             :         {
    1089           0 :             quant_tmp1[n_band] = EnvCdbk11[candInd[k] * DIM11 + n_band];
    1090           0 :             quant_tmp2[n_band] = env_temp11[n_band] - quant_tmp1[n_band];
    1091             :         }
    1092             : 
    1093           0 :         ind_tmp[0] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk1st, w_env11, DIM1ST, N_CB1ST, 1 );
    1094           0 :         ind_tmp[1] = vqSimple_w( quant_tmp2 + DIM1ST, quant_tmp2 + DIM1ST, EnvCdbk2nd, w_env11 + DIM1ST, DIM2ND, N_CB2ND, 1 );
    1095             : 
    1096             :         /* Extract vector for odd position */
    1097           0 :         for ( n_band = 0; n_band < DIM11; n_band++ )
    1098             :         {
    1099           0 :             quant_tmp[n_band] = quant_tmp1[n_band] + quant_tmp2[n_band];
    1100             :         }
    1101             : 
    1102           0 :         quant_tmp2[0] = env_temp12[0] - quant_tmp[0];
    1103           0 :         for ( n_band = 1; n_band < DIM12 - 1; n_band++ )
    1104             :         {
    1105           0 :             quant_tmp2[n_band] = env_temp12[n_band] - ( ( quant_tmp[n_band - 1] + quant_tmp[n_band] ) / 2.f );
    1106             :         }
    1107             : 
    1108           0 :         ind_tmp[2] = vqSimple_w( quant_tmp2, quant_tmp2, EnvCdbk3rd, w_env12, DIM3RD, N_CB3RD, 1 );
    1109           0 :         ind_tmp[3] = vqSimple_w( quant_tmp2 + DIM3RD, quant_tmp2 + DIM3RD, EnvCdbk3rd, w_env12 + DIM3RD, DIM3RD, N_CB3RD, 1 );
    1110             : 
    1111           0 :         for ( n_band = 0; n_band < DIM12 - 1; n_band++ )
    1112             :         {
    1113           0 :             quant_select[n_band * 2] = quant_tmp[n_band];
    1114             :         }
    1115           0 :         quant_select[11] = quant_tmp[DIM12 - 1];
    1116             : 
    1117           0 :         quant_select[0] += quant_tmp2[0];
    1118           0 :         for ( n_band = 1; n_band < DIM12 - 1; n_band++ )
    1119             :         {
    1120           0 :             quant_select[n_band * 2 - 1] = ( ( quant_tmp[n_band - 1] + quant_tmp[n_band] ) / 2.f ) + quant_tmp2[n_band];
    1121             :         }
    1122             : 
    1123           0 :         dist = 0.f;
    1124           0 :         for ( n_band = 0; n_band < SWB_FENV - 2; n_band++ )
    1125             :         {
    1126           0 :             tmp_q = hq_generic_fenv[n_band] - quant_select[n_band];
    1127           0 :             tmp_q = tmp_q * tmp_q;
    1128           0 :             dist += tmp_q * w_env[n_band];
    1129             :         }
    1130             : 
    1131             :         /* Check optimal candidate */
    1132           0 :         if ( dist < minDist )
    1133             :         {
    1134           0 :             minDist = dist;
    1135           0 :             mvr2r( quant_select, synth_energy, SWB_FENV - 2 );
    1136           0 :             synth_energy[SWB_FENV - 2] = 0;
    1137           0 :             synth_energy[SWB_FENV - 1] = 0;
    1138             : 
    1139           0 :             indice[0] = candInd[k];
    1140           0 :             indice[1] = ind_tmp[0];
    1141           0 :             indice[2] = ind_tmp[1];
    1142           0 :             indice[3] = ind_tmp[2];
    1143           0 :             indice[4] = ind_tmp[3];
    1144             :         }
    1145             :     }
    1146             : 
    1147           0 :     mvr2r( synth_energy, hq_generic_fenv, nenv );
    1148             : 
    1149           0 :     return;
    1150             : }
    1151             : 
    1152             : /*-------------------------------------------------------------------*
    1153             :  * calculate_tonality()
    1154             :  *
    1155             :  * Calculate tonality
    1156             :  *-------------------------------------------------------------------*/
    1157             : 
    1158      216940 : static void calculate_tonality(
    1159             :     const float *org,    /* i  : MDCT coefficients of original           */
    1160             :     const float *gen,    /* i  : MDCT coefficients of generated signal   */
    1161             :     float *SFM_org,      /* o  : Spectral Flatness results               */
    1162             :     float *SFM_gen,      /* o  : Spectral Flatness results               */
    1163             :     const int16_t length /* i  : length for calculating tonality         */
    1164             : )
    1165             : {
    1166             :     int16_t n_coeff;
    1167             :     float am_org, am_gen, gm_org, gm_gen;
    1168             :     float inv_len, max_val, mult;
    1169             :     float org_spec[80], gen_spec[80];
    1170             : 
    1171             :     /* to reduce dynamic range of original spectrum */
    1172      216940 :     max_val = EPSILON;
    1173     5316140 :     for ( n_coeff = 0; n_coeff < length; n_coeff++ )
    1174             :     {
    1175     5099200 :         org_spec[n_coeff] = (float) fabs( org[n_coeff] );
    1176             : 
    1177     5099200 :         if ( max_val < org_spec[n_coeff] )
    1178             :         {
    1179      809033 :             max_val = org_spec[n_coeff];
    1180             :         }
    1181             :     }
    1182      216940 :     mult = 25.f / max_val;
    1183             : 
    1184     5316140 :     for ( n_coeff = 0; n_coeff < length; n_coeff++ )
    1185             :     {
    1186     5099200 :         org_spec[n_coeff] *= mult;
    1187             :     }
    1188             : 
    1189      216940 :     max_val = EPSILON;
    1190     5316140 :     for ( n_coeff = 0; n_coeff < length; n_coeff++ )
    1191             :     {
    1192     5099200 :         gen_spec[n_coeff] = (float) fabs( gen[n_coeff] );
    1193             : 
    1194     5099200 :         if ( max_val < gen_spec[n_coeff] )
    1195             :         {
    1196      774047 :             max_val = gen_spec[n_coeff];
    1197             :         }
    1198             :     }
    1199      216940 :     mult = 25.f / max_val;
    1200             : 
    1201     5316140 :     for ( n_coeff = 0; n_coeff < length; n_coeff++ )
    1202             :     {
    1203     5099200 :         gen_spec[n_coeff] *= mult;
    1204             :     }
    1205             : 
    1206      216940 :     inv_len = 1.f / (float) length;
    1207             : 
    1208      216940 :     am_org = EPSILON;
    1209      216940 :     am_gen = EPSILON;
    1210      216940 :     gm_org = 1.f;
    1211      216940 :     gm_gen = 1.f;
    1212             : 
    1213     5316140 :     for ( n_coeff = 0; n_coeff < length; n_coeff++ )
    1214             :     {
    1215     5099200 :         am_org += org_spec[n_coeff];
    1216     5099200 :         am_gen += gen_spec[n_coeff];
    1217     5099200 :         gm_org *= org_spec[n_coeff];
    1218     5099200 :         gm_gen *= gen_spec[n_coeff];
    1219             :     }
    1220             : 
    1221      216940 :     *SFM_org = 10.f * ( (float) log10( am_org * inv_len ) - inv_len * (float) log10( gm_org ) );
    1222      216940 :     *SFM_org = max( 0.0001f, min( *SFM_org, 5.993f ) );
    1223      216940 :     *SFM_gen = 10.f * ( (float) log10( am_gen * inv_len ) - inv_len * (float) log10( gm_gen ) );
    1224      216940 :     *SFM_gen = max( 0.0001f, min( *SFM_gen, 5.993f ) );
    1225             : 
    1226      216940 :     return;
    1227             : }
    1228             : 
    1229             : /*-------------------------------------------------------------------*
    1230             :  * energy_control()
    1231             :  *
    1232             :  *-------------------------------------------------------------------*/
    1233             : 
    1234       19010 : static void energy_control(
    1235             :     Encoder_State *st,        /* i/o: encoder structure   */
    1236             :     const int16_t core,       /* i  : core                */
    1237             :     const int16_t mode,       /* i  : SHB BWE class       */
    1238             :     const int16_t coder_type, /* i  : SHB BWE class       */
    1239             :     const float *org,         /* i  : input spectrum      */
    1240             :     const int16_t offset,     /* i  : frequency offset    */
    1241             :     float *energy_factor      /* o  : energy factor       */
    1242             : )
    1243             : {
    1244             :     int16_t n_band;
    1245             :     float gamma;
    1246             :     int16_t core_type;
    1247             :     float SWB_signal[L_FRAME32k], SFM_org[SWB_FENV], SFM_gen[SWB_FENV];
    1248       19010 :     int16_t max_band = SWB_FENV, band_step = 1;
    1249             : 
    1250       19010 :     if ( core == ACELP_CORE )
    1251             :     {
    1252       15941 :         gamma = 0.35f;
    1253       15941 :         if ( coder_type != AUDIO && st->total_brate <= ACELP_8k00 )
    1254             :         {
    1255          21 :             core_type = 0;
    1256             :         }
    1257             :         else
    1258             :         {
    1259       15920 :             core_type = 1;
    1260             :         }
    1261             : 
    1262       15941 :         get_normalize_spec( core, st->extl, mode, core_type, org, SWB_signal, &( st->hBWE_FD->prev_L_swb_norm1 ), offset );
    1263             : 
    1264       15941 :         if ( st->extl == WB_BWE )
    1265             :         {
    1266        4100 :             max_band = 4;
    1267        4100 :             band_step = 2;
    1268             :         }
    1269             :     }
    1270             :     else /* HQ core */
    1271             :     {
    1272        3069 :         gamma = 0.55f;
    1273        3069 :         get_normalize_spec( core, -1, mode, -1, org, SWB_signal, &( st->hBWE_FD->prev_L_swb_norm1 ), offset );
    1274             : 
    1275        3069 :         if ( offset == HQ_GENERIC_FOFFSET_32K )
    1276             :         {
    1277           0 :             max_band = 12;
    1278             :         }
    1279             :     }
    1280             : 
    1281      235950 :     for ( n_band = 0; n_band < max_band; n_band += band_step )
    1282             :     {
    1283      216940 :         calculate_tonality( org + swb_bwe_subband[n_band] + offset, SWB_signal + swb_bwe_subband[n_band] + offset, &SFM_org[n_band], &SFM_gen[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] );
    1284             : 
    1285      216940 :         if ( SFM_gen[n_band] < 0.75 * SFM_org[n_band] )
    1286             :         {
    1287       58376 :             energy_factor[n_band] = ( SFM_gen[n_band] / SFM_org[n_band] );
    1288             : 
    1289       58376 :             if ( energy_factor[n_band] < gamma )
    1290             :             {
    1291        6208 :                 energy_factor[n_band] = gamma;
    1292             :             }
    1293             :         }
    1294             :         else
    1295             :         {
    1296      158564 :             energy_factor[n_band] = 1.0f;
    1297             :         }
    1298             :     }
    1299             : 
    1300       19010 :     return;
    1301             : }
    1302             : 
    1303             : /*-------------------------------------------------------------------*
    1304             :  * SWB_BWE_encoding()
    1305             :  *
    1306             :  * SWB BWE encoder
    1307             :  *-------------------------------------------------------------------*/
    1308             : 
    1309       11922 : static int16_t SWB_BWE_encoding(
    1310             :     Encoder_State *st,      /* i/o: Encoder state structure                         */
    1311             :     const float *insig,     /* i  : delayed original input signal at 32kHz          */
    1312             :     const float *insig_lp,  /* i  : delayed original lowband input signal at 16kHz  */
    1313             :     const float *insig_hp,  /* i  : delayed original highband input signal at 16kHz */
    1314             :     const float *synth,     /* i  : delayed ACELP core synthesis at 12.8kHz         */
    1315             :     const float *yos,       /* i  : MDCT coefficients of the windowed original input signal at 32kHz */
    1316             :     float *SWB_fenv,        /* o  : frequency-domain quantized BWE envelope         */
    1317             :     const float tilt_nb,    /* i  : SWB tilt                                        */
    1318             :     const int16_t st_offset /* i  : start frequency offset for BWE envelope         */
    1319             : )
    1320             : {
    1321             :     int16_t IsTransient, mode;
    1322             :     int16_t index;
    1323             :     float SWB_tenv_tmp[SWB_TENV];
    1324             :     float SWB_tenv[SWB_TENV];
    1325             :     float global_gain;
    1326             :     float energy;
    1327             :     float max_val;
    1328             :     int16_t i, n_coeff, n_band, pos, indice[6];
    1329             :     float tilt, WB_tenv_orig, WB_tenv_syn, Rat_tenv;
    1330             :     float energy_factor[SWB_FENV], w_env[SWB_FENV];
    1331             :     int16_t L;
    1332             :     int16_t IsTransient_LF;
    1333       11922 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
    1334             : 
    1335       11922 :     if ( st->L_frame == L_FRAME )
    1336             :     {
    1337        7399 :         L = L_SUBFR;
    1338             :     }
    1339             :     else
    1340             :     {
    1341        4523 :         L = L_SUBFR16k;
    1342             :     }
    1343             : 
    1344             :     /* HF transient detect */
    1345       11922 :     IsTransient = detect_transient( st, insig_hp, L_FRAME16k );
    1346             : 
    1347             :     /* LF transient detect */
    1348       11922 :     IsTransient_LF = 0;
    1349       59610 :     for ( n_band = 0; n_band < 4; n_band++ )
    1350             :     {
    1351       47688 :         energy = EPSILON;
    1352     3389192 :         for ( i = 0; i < L; i++ )
    1353             :         {
    1354     3341504 :             energy += insig_lp[i + n_band * L] * insig_lp[i + n_band * L];
    1355             :         }
    1356             : 
    1357       47688 :         if ( energy > 5.5f * st->hBWE_FD->EnergyLF )
    1358             :         {
    1359        2488 :             IsTransient_LF = 1;
    1360             :         }
    1361             : 
    1362       47688 :         st->hBWE_FD->EnergyLF = energy;
    1363             :     }
    1364             : 
    1365       11922 :     calc_tilt_bwe( insig, &tilt, L_FRAME32k );
    1366             : 
    1367       11922 :     if ( IsTransient == 1 && ( tilt > 8.0 || st->clas > 1 ) )
    1368             :     {
    1369          74 :         IsTransient = 0;
    1370          74 :         st->TransientHangOver = 0;
    1371             :     }
    1372             : 
    1373       11922 :     if ( IsTransient == 1 )
    1374             :     {
    1375          81 :         mode = IsTransient;
    1376          81 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    1377             : 
    1378             :         /* Energy for the different bands and global energies */
    1379          81 :         global_gain = 0;
    1380         405 :         for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1381             :         {
    1382         324 :             energy = EPSILON;
    1383       26244 :             for ( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ )
    1384             :             {
    1385       25920 :                 energy += yos[n_coeff] * yos[n_coeff];
    1386             :             }
    1387         324 :             global_gain += energy;
    1388         324 :             SWB_fenv[n_band] = energy;
    1389             :         }
    1390          81 :         global_gain *= 0.5f;
    1391             : 
    1392         405 :         for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1393             :         {
    1394         324 :             SWB_fenv[n_band] = 10.0f * (float) log10( SWB_fenv[n_band] / swb_bwe_trans_subband_width[n_band] ) - Mean_env_tr[n_band];
    1395             :         }
    1396             : 
    1397          81 :         WB_tenv_orig = EPSILON;
    1398          81 :         WB_tenv_syn = EPSILON;
    1399         405 :         for ( n_band = 0; n_band < SWB_TENV; n_band++ )
    1400             :         {
    1401         324 :             SWB_tenv[n_band] = EPSILON;
    1402             : 
    1403       26244 :             for ( i = 0; i < L_SUBFR16k; i++ )
    1404             :             {
    1405       25920 :                 SWB_tenv[n_band] += insig_hp[i + n_band * L_SUBFR16k] * insig_hp[i + n_band * L_SUBFR16k];
    1406             :             }
    1407             : 
    1408       21124 :             for ( i = 0; i < L; i++ )
    1409             :             {
    1410       20800 :                 WB_tenv_syn += synth[i + n_band * L] * synth[i + n_band * L];
    1411       20800 :                 WB_tenv_orig += insig_lp[i + n_band * L] * insig_lp[i + n_band * L];
    1412             :             }
    1413             : 
    1414         324 :             SWB_tenv[n_band] = (float) ( sqrt( SWB_tenv[n_band] * INV_L_SUBFR16k ) );
    1415             :         }
    1416             : 
    1417          81 :         Rat_tenv = (float) sqrt( WB_tenv_syn / WB_tenv_orig );
    1418             : 
    1419          81 :         if ( Rat_tenv < 0.5 )
    1420             :         {
    1421           8 :             Rat_tenv *= 1.2f;
    1422             :         }
    1423          73 :         else if ( Rat_tenv > 1 )
    1424             :         {
    1425          55 :             Rat_tenv = 1.0f;
    1426             :         }
    1427             : 
    1428         405 :         for ( n_band = 0; n_band < SWB_TENV; n_band++ )
    1429             :         {
    1430         324 :             SWB_tenv[n_band] *= Rat_tenv;
    1431             :         }
    1432             : 
    1433          81 :         max_val = SWB_tenv[0];
    1434          81 :         pos = 0;
    1435         324 :         for ( n_band = 1; n_band < SWB_TENV; n_band++ )
    1436             :         {
    1437         243 :             if ( SWB_tenv[n_band] > max_val )
    1438             :             {
    1439         104 :                 max_val = SWB_tenv[n_band];
    1440         104 :                 pos = n_band;
    1441             :             }
    1442             :         }
    1443             : 
    1444         290 :         for ( n_band = 1; n_band < SWB_TENV; n_band++ )
    1445             :         {
    1446         227 :             if ( SWB_tenv[n_band] > 5.0f * SWB_tenv[n_band - 1] )
    1447             :             {
    1448          18 :                 break;
    1449             :             }
    1450             :         }
    1451             : 
    1452          81 :         if ( n_band < SWB_TENV )
    1453             :         {
    1454          18 :             energy = 0.0f;
    1455          29 :             for ( n_band = ( pos + 1 ); n_band < SWB_TENV; n_band++ )
    1456             :             {
    1457          11 :                 energy += SWB_tenv[n_band];
    1458             :             }
    1459          18 :             if ( pos == SWB_TENV - 1 )
    1460             :             {
    1461           9 :                 energy = 0.0f;
    1462             :             }
    1463             :             else
    1464             :             {
    1465           9 :                 energy /= ( SWB_TENV - pos - 1 );
    1466             :             }
    1467             : 
    1468          61 :             for ( n_band = 0; n_band < pos; n_band++ )
    1469             :             {
    1470          43 :                 SWB_tenv[n_band] *= 0.5f;
    1471             :             }
    1472             : 
    1473          18 :             SWB_tenv[pos] *= 1.005f;
    1474          18 :             if ( energy < SWB_tenv[pos] )
    1475             :             {
    1476          29 :                 for ( n_band = pos + 1; n_band < SWB_TENV; n_band++ )
    1477             :                 {
    1478          11 :                     SWB_tenv[n_band] *= 0.9f;
    1479             :                 }
    1480             :             }
    1481             :         }
    1482             :         else
    1483             :         {
    1484         252 :             for ( n_band = 1; n_band < SWB_TENV; n_band++ )
    1485             :             {
    1486         189 :                 if ( SWB_tenv[n_band - 1] > SWB_tenv[n_band] )
    1487             :                 {
    1488          77 :                     SWB_tenv[n_band - 1] = 0.5f * ( SWB_tenv[n_band - 1] + SWB_tenv[n_band] );
    1489             :                 }
    1490             :                 else
    1491             :                 {
    1492         112 :                     SWB_tenv[n_band] = 0.5f * ( SWB_tenv[n_band - 1] + SWB_tenv[n_band] );
    1493             :                 }
    1494             :             }
    1495             : 
    1496         315 :             for ( n_band = 0; n_band < SWB_TENV; n_band++ )
    1497             :             {
    1498         252 :                 SWB_tenv[n_band] *= 0.9f;
    1499             :             }
    1500             :         }
    1501             : 
    1502          81 :         if ( IsTransient_LF == 0 && st->coder_type == INACTIVE && st->TransientHangOver == 1 )
    1503             :         {
    1504           5 :             for ( n_band = 0; n_band < SWB_TENV; n_band++ )
    1505             :             {
    1506           4 :                 SWB_tenv[n_band] *= 0.5f;
    1507             :             }
    1508           5 :             for ( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1509             :             {
    1510           4 :                 SWB_fenv[n_band] *= 0.05f;
    1511             :             }
    1512             :         }
    1513             :         else
    1514             :         {
    1515          80 :             SWB_fenv[2] *= 0.1f;
    1516          80 :             SWB_fenv[3] *= 0.05f;
    1517             :         }
    1518             : 
    1519         405 :         for ( n_band = 0; n_band < SWB_TENV; n_band++ )
    1520             :         {
    1521         324 :             SWB_tenv_tmp[n_band] = (float) log10( SWB_tenv[n_band] + EPSILON ) * FAC_LOG2;
    1522         324 :             if ( SWB_tenv_tmp[n_band] > 15 )
    1523             :             {
    1524           0 :                 index = 15;
    1525             :             }
    1526         324 :             else if ( SWB_tenv_tmp[n_band] < 0 )
    1527             :             {
    1528           5 :                 index = 0;
    1529             :             }
    1530             :             else
    1531             :             {
    1532         319 :                 index = (int16_t) ( SWB_tenv_tmp[n_band] + 0.5f );
    1533             :             }
    1534             : 
    1535         324 :             push_indice( hBstr, IND_SWB_TENV, index, 4 );
    1536             :         }
    1537             : 
    1538          81 :         MSVQ_Interpol_Tran( SWB_fenv, indice );
    1539             : 
    1540          81 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 7 );
    1541          81 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 6 );
    1542             :     }
    1543             :     else
    1544             :     {
    1545             :         /* Energy for the different bands and global energies */
    1546       11841 :         global_gain = 0;
    1547      177615 :         for ( n_band = 0; n_band < SWB_FENV; n_band++ )
    1548             :         {
    1549      165774 :             energy = EPSILON;
    1550     3954894 :             for ( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ )
    1551             :             {
    1552     3789120 :                 energy += yos[n_coeff] * yos[n_coeff];
    1553             :             }
    1554             : 
    1555      165774 :             if ( n_band < SWB_FENV - 2 )
    1556             :             {
    1557      142092 :                 global_gain += energy;
    1558             :             }
    1559      165774 :             SWB_fenv[n_band] = energy;
    1560             :         }
    1561             : 
    1562       11841 :         global_gain *= 0.5f;
    1563             : 
    1564       11841 :         mode = FD_BWE_class( yos, global_gain, tilt_nb, st );
    1565             : 
    1566       11841 :         push_indice( hBstr, IND_SWB_CLASS, mode, 2 );
    1567             : 
    1568       11841 :         energy_control( st, ACELP_CORE, mode, -1, yos, st_offset, energy_factor );
    1569             : 
    1570      177615 :         for ( n_band = 0; n_band < SWB_FENV; n_band++ )
    1571             :         {
    1572      165774 :             SWB_fenv[n_band] *= energy_factor[n_band];
    1573      165774 :             SWB_fenv[n_band] = 10.0f * (float) log10( SWB_fenv[n_band] * swb_inv_bwe_subband_width[n_band] );
    1574             :         }
    1575             : 
    1576       11841 :         freq_weights( SWB_fenv, w_NOR, w_env, SWB_FENV );
    1577             : 
    1578      177615 :         for ( n_band = 0; n_band < SWB_FENV; n_band++ )
    1579             :         {
    1580      165774 :             SWB_fenv[n_band] -= Mean_env[n_band];
    1581             :         }
    1582             : 
    1583             :         /* Energy VQ */
    1584       11841 :         msvq_interpol( SWB_fenv, w_env, indice );
    1585             : 
    1586       11841 :         push_indice( hBstr, IND_SWB_FENV, indice[0], 5 );
    1587       11841 :         push_indice( hBstr, IND_SWB_FENV, indice[1], 7 );
    1588       11841 :         push_indice( hBstr, IND_SWB_FENV, indice[2], 6 );
    1589       11841 :         push_indice( hBstr, IND_SWB_FENV, indice[3], 5 );
    1590       11841 :         push_indice( hBstr, IND_SWB_FENV, indice[4], 6 );
    1591             :     }
    1592             : 
    1593       11922 :     st->hBWE_FD->prev_mode = mode;
    1594       11922 :     st->hBWE_FD->prev_global_gain = global_gain;
    1595             : 
    1596       11922 :     return mode;
    1597             : }
    1598             : 
    1599             : 
    1600             : /*-------------------------------------------------------------------*
    1601             :  * decision_hq_generic_class()
    1602             :  *
    1603             :  *-------------------------------------------------------------------*/
    1604             : 
    1605        2925 : static int16_t decision_hq_generic_class(
    1606             :     const float *coefs,             /* i  : original MDCT spectrum                      */
    1607             :     const int16_t hq_generic_offset /* i  : frequency offset of high frequency spectrum */
    1608             : )
    1609             : {
    1610             :     int16_t i, k;
    1611             :     float p, a, e;
    1612             :     float p2a;
    1613             :     float avgp2a;
    1614             :     int16_t nband;
    1615             : 
    1616        2925 :     if ( hq_generic_offset == HQ_GENERIC_FOFFSET_24K4 )
    1617             :     {
    1618        2925 :         nband = 10;
    1619             :     }
    1620             :     else
    1621             :     {
    1622           0 :         nband = 8;
    1623             :     }
    1624             : 
    1625        2925 :     avgp2a = 0.f;
    1626       32175 :     for ( k = 0; k < nband; k++ )
    1627             :     {
    1628       29250 :         a = 0.0f;
    1629       29250 :         p = 0.0f;
    1630      637650 :         for ( i = swb_bwe_subband[k] + hq_generic_offset; i < swb_bwe_subband[k + 1] + hq_generic_offset; i++ )
    1631             :         {
    1632      608400 :             e = coefs[i] * coefs[i];
    1633             : 
    1634      608400 :             if ( e > p )
    1635             :             {
    1636      105369 :                 p = e;
    1637             :             }
    1638             : 
    1639      608400 :             a += e;
    1640             :         }
    1641             : 
    1642       29250 :         if ( a > 0.0f )
    1643             :         {
    1644       29250 :             a *= swb_inv_bwe_subband_width[k];
    1645       29250 :             p2a = 10.0f * (float) log10( p / a );
    1646       29250 :             avgp2a += p2a;
    1647             :         }
    1648             :     }
    1649             : 
    1650        2925 :     avgp2a /= (float) ( nband );
    1651             : 
    1652        2925 :     if ( avgp2a > 8.6f )
    1653             :     {
    1654          15 :         return HQ_GENERIC_EXC1;
    1655             :     }
    1656             :     else
    1657             :     {
    1658        2910 :         return HQ_GENERIC_EXC0;
    1659             :     }
    1660             : }
    1661             : 
    1662             : /*-------------------------------------------------------------------*
    1663             :  * hq_generic_hf_encoding()
    1664             :  *
    1665             :  *-------------------------------------------------------------------*/
    1666             : 
    1667        3069 : void hq_generic_hf_encoding(
    1668             :     const float *coefs,              /* i  : MDCT coefficients of weighted original      */
    1669             :     float *hq_generic_fenv,          /* i/o: energy of SWB envelope                      */
    1670             :     const int16_t hq_generic_offset, /* i  : frequency offset for extracting energy      */
    1671             :     Encoder_State *st,               /* i/o: encoder state structure                     */
    1672             :     int16_t *hq_generic_exc_clas,    /* o  : HF excitation class                         */
    1673             :     const int16_t length             /* i  : Length of spectrum                          */
    1674             : )
    1675             : {
    1676             :     int16_t n_coeff, n_band;
    1677             :     float energy;
    1678             :     float energy_factor[SWB_FENV], w_env[SWB_FENV];
    1679             :     int16_t indice[HQ_GENERIC_NVQIDX];
    1680             :     int16_t nenv;
    1681        3069 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
    1682             : 
    1683        3069 :     if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
    1684             :     {
    1685        3069 :         nenv = SWB_FENV;
    1686             :     }
    1687             :     else
    1688             :     {
    1689           0 :         nenv = SWB_FENV - 2;
    1690             :     }
    1691             : 
    1692       46035 :     for ( n_band = 0; n_band < nenv; n_band++ )
    1693             :     {
    1694       42966 :         energy = EPSILON;
    1695     1025046 :         for ( n_coeff = swb_bwe_subband[n_band] + hq_generic_offset; n_coeff < swb_bwe_subband[n_band + 1] + hq_generic_offset; n_coeff++ )
    1696             :         {
    1697      982080 :             energy += coefs[n_coeff] * coefs[n_coeff];
    1698             :         }
    1699             : 
    1700       42966 :         hq_generic_fenv[n_band] = energy;
    1701             :     }
    1702             : 
    1703        3069 :     if ( length == L_SPEC48k )
    1704             :     {
    1705        9236 :         for ( n_band = 0; n_band < DIM_FB; n_band++ )
    1706             :         {
    1707        6927 :             energy = EPSILON;
    1708      376367 :             for ( n_coeff = fb_bwe_subband[n_band]; n_coeff < fb_bwe_subband[n_band + 1]; n_coeff++ )
    1709             :             {
    1710      369440 :                 energy += coefs[n_coeff] * coefs[n_coeff];
    1711             :             }
    1712             : 
    1713        6927 :             hq_generic_fenv[n_band + nenv] = energy;
    1714             :         }
    1715             :     }
    1716             : 
    1717        3069 :     energy_control( st, HQ_CORE, -1, -1, coefs, hq_generic_offset, energy_factor );
    1718             : 
    1719        3069 :     if ( st->hHQ_core->hq_generic_speech_class == 1 )
    1720             :     {
    1721         144 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 1, 1 );
    1722         144 :         *hq_generic_exc_clas = HQ_GENERIC_SP_EXC;
    1723             :     }
    1724             :     else
    1725             :     {
    1726        2925 :         *hq_generic_exc_clas = decision_hq_generic_class( coefs, hq_generic_offset );
    1727        2925 :         push_indice( hBstr, IND_HQ_SWB_EXC_SP_CLAS, 0, 1 );
    1728        2925 :         push_indice( hBstr, IND_HQ_SWB_EXC_CLAS, *hq_generic_exc_clas, 1 );
    1729             :     }
    1730             : 
    1731       46035 :     for ( n_band = 0; n_band < nenv; n_band++ )
    1732             :     {
    1733       42966 :         hq_generic_fenv[n_band] *= energy_factor[n_band];
    1734       42966 :         hq_generic_fenv[n_band] = 10.0f * (float) log10( hq_generic_fenv[n_band] * swb_inv_bwe_subband_width[n_band] );
    1735             :     }
    1736             : 
    1737        3069 :     if ( length == L_SPEC48k )
    1738             :     {
    1739        9236 :         for ( n_band = 0; n_band < DIM_FB; n_band++ )
    1740             :         {
    1741        6927 :             hq_generic_fenv[n_band + nenv] = 10.0f * (float) log10( hq_generic_fenv[n_band + nenv] * fb_inv_bwe_subband_width[n_band] );
    1742             :         }
    1743             :     }
    1744             : 
    1745        3069 :     freq_weights( hq_generic_fenv, w_NOR, w_env, nenv );
    1746             : 
    1747       46035 :     for ( n_band = 0; n_band < nenv; n_band++ )
    1748             :     {
    1749       42966 :         hq_generic_fenv[n_band] -= Mean_env[n_band];
    1750             :     }
    1751             : 
    1752        3069 :     if ( length == L_SPEC48k )
    1753             :     {
    1754        9236 :         for ( n_band = 0; n_band < DIM_FB; n_band++ )
    1755             :         {
    1756        6927 :             hq_generic_fenv[n_band + nenv] -= Mean_env_fb[n_band];
    1757             :         }
    1758             :     }
    1759             : 
    1760             :     /* Energy VQ */
    1761        3069 :     if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
    1762             :     {
    1763        3069 :         msvq_interpol( hq_generic_fenv, w_env, indice );
    1764             :     }
    1765             :     else
    1766             :     {
    1767           0 :         msvq_interpol_2( hq_generic_fenv, w_env, indice, nenv );
    1768             :     }
    1769             : 
    1770        3069 :     if ( length == L_SPEC48k )
    1771             :     {
    1772        2309 :         indice[5] = vqSimple_w( hq_generic_fenv + nenv, hq_generic_fenv + nenv, EnvCdbkFB, NULL, DIM_FB, N_CB_FB, 0 );
    1773             :     }
    1774             : 
    1775        3069 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[0], 5 );
    1776        3069 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[1], 7 );
    1777        3069 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[2], 6 );
    1778        3069 :     push_indice( hBstr, IND_SWB_FENV_HQ, indice[3], 5 );
    1779             : 
    1780        3069 :     if ( hq_generic_offset <= HQ_GENERIC_FOFFSET_24K4 )
    1781             :     {
    1782        3069 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 6 );
    1783             :     }
    1784             :     else
    1785             :     {
    1786           0 :         push_indice( hBstr, IND_SWB_FENV_HQ, indice[4], 5 );
    1787             :     }
    1788             : 
    1789        3069 :     if ( length == L_SPEC48k )
    1790             :     {
    1791        2309 :         push_indice( hBstr, IND_FB_FENV_HQ, indice[5], 5 );
    1792             :     }
    1793             : 
    1794       46035 :     for ( n_band = 0; n_band < nenv; n_band++ )
    1795             :     {
    1796             :         Word16 tmp, frac, exp;
    1797             :         Word32 L_tmp;
    1798             : #ifdef BASOP_NOGLOB
    1799             :         Flag Overflow;
    1800             : 
    1801       42966 :         Overflow = 0;
    1802             : #endif
    1803       42966 :         tmp = add( (int16_t) ( hq_generic_fenv[n_band] * 256 ), (int16_t) ( Mean_env[n_band] * 256 ) ); /*Q8 */
    1804             : 
    1805       42966 :         L_tmp = L_mult( tmp, 21771 );       /* 0.166096 in Q17 -> Q26 */
    1806       42966 :         L_tmp = L_shr( L_tmp, 10 );         /* From Q26 to Q16 */
    1807       42966 :         frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */
    1808             : 
    1809       42966 :         tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
    1810             :         /* output of Pow2() will be: */
    1811             :         /* 16384 < Pow2() <= 32767 */
    1812       42966 :         exp = sub( exp, 13 );
    1813             : #ifdef BASOP_NOGLOB
    1814       42966 :         tmp = shl_o( tmp, add( exp, 1 ), &Overflow ); /*Q1 */
    1815             : #else
    1816             :         tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
    1817             : #endif
    1818       42966 :         hq_generic_fenv[n_band] = (float) tmp * 0.5f; /*Q1 */
    1819             :     }
    1820             : 
    1821        3069 :     if ( length == L_SPEC48k )
    1822             :     {
    1823        9236 :         for ( n_band = 0; n_band < DIM_FB; n_band++ )
    1824             :         {
    1825             :             Word16 tmp, frac, exp;
    1826             :             Word32 L_tmp;
    1827             : 
    1828        6927 :             tmp = add( (int16_t) ( hq_generic_fenv[n_band + nenv] * 128 ), (int16_t) ( Mean_env_fb[n_band] * 128 ) ); /*Q7 */
    1829        6927 :             L_tmp = L_mult( tmp, 21771 );                                                                             /* 0.166096 in Q17 -> Q25 */
    1830        6927 :             L_tmp = L_shr( L_tmp, 9 );                                                                                /* From Q25 to Q16 */
    1831        6927 :             frac = L_Extract_lc( L_tmp, &exp );                                                                       /* Extract exponent of L_tmp */
    1832             : 
    1833        6927 :             tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */
    1834             :             /* output of Pow2() will be: */
    1835             :             /* 16384 < Pow2() <= 32767 */
    1836        6927 :             exp = sub( exp, 13 );
    1837        6927 :             tmp = shl( tmp, add( exp, 1 ) ); /*Q1 */
    1838        6927 :             hq_generic_fenv[add( n_band, nenv )] = (float) tmp * 0.5f;
    1839             :         }
    1840             :     }
    1841             : 
    1842        3069 :     return;
    1843             : }
    1844             : 
    1845             : 
    1846             : /*-------------------------------------------------------------------*
    1847             :  * fd_bwe_enc_init()
    1848             :  *
    1849             :  * Initialize FD BWE state structure at the encoder
    1850             :  *-------------------------------------------------------------------*/
    1851             : 
    1852        3897 : void fd_bwe_enc_init(
    1853             :     FD_BWE_ENC_HANDLE hBWE_FD /* i/o: FD BWE data handle  */
    1854             : )
    1855             : {
    1856        3897 :     set_f( hBWE_FD->new_input_hp, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS ) );
    1857        3897 :     set_f( hBWE_FD->old_input, 0, NS2SA( 48000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) );
    1858        3897 :     set_f( hBWE_FD->old_input_wb, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS ) );
    1859        3897 :     set_f( hBWE_FD->old_input_lp, 0, NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ) );
    1860        3897 :     set_f( hBWE_FD->old_syn_12k8_16k, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) );
    1861             : 
    1862        3897 :     hBWE_FD->prev_mode = NORMAL;
    1863        3897 :     set_f( hBWE_FD->old_wtda_swb, 0, L_FRAME48k );
    1864        3897 :     hBWE_FD->prev_L_swb_norm1 = 8;
    1865        3897 :     hBWE_FD->prev_global_gain = 0.0f;
    1866        3897 :     hBWE_FD->modeCount = 0;
    1867        3897 :     hBWE_FD->EnergyLF = 0.0f;
    1868        3897 :     hBWE_FD->mem_old_wtda_swb = 0.0f;
    1869             : 
    1870        3897 :     set_f( hBWE_FD->old_fdbwe_speech, 0.0f, L_FRAME48k );
    1871        3897 :     hBWE_FD->mem_deemph_old_syn = 0.0f;
    1872             : 
    1873        3897 :     return;
    1874             : }

Generated by: LCOV version 1.14