LCOV - code coverage report
Current view: top level - lib_dec - swb_tbe_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 4c82f1d24d39d0296b18d775f18a006f4c7d024b Lines: 2323 3624 64.1 %
Date: 2025-05-17 01:59:02 Functions: 25 29 86.2 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : 
       6             : #include <stdlib.h>
       7             : #include "options.h"
       8             : #include "rom_com.h"
       9             : #include "prot_fx.h"
      10             : #include "rom_dec.h"
      11             : #include "stl.h"
      12             : 
      13             : #include "ivas_prot_fx.h"
      14             : 
      15             : /*-----------------------------------------------------------------*
      16             :  * Local functions
      17             :  *-----------------------------------------------------------------*/
      18             : 
      19             : static void dequantizeSHBparams_fx_9_1( Decoder_State *st_fx, const Word16 extl, Word32 extl_brate, Word16 *Q_lsf, Word16 *Q_subgain, Word32 *Q_framegrain, Word16 *uv_flag, Word32 *Q_shb_ener_sf_32, Word16 *Q_shb_res_gshape, Word16 *Q_mixFactors );
      20             : 
      21             : static void find_max_mem_dec( Decoder_State *st_fx, Word16 *n_mem, Word16 *n_mem2, Word16 *n_mem3 );
      22             : static void rescale_genSHB_mem_dec( Decoder_State *st_fx, Word16 sf );
      23             : static void find_max_mem_wb( Decoder_State *st_fx, Word16 *n_mem );
      24             : static void rescale_genWB_mem( Decoder_State *st_fx, Word16 sf );
      25             : static void Dequant_lower_LSF_fx( const Word16 lsf_idx[], Word16 lsf_q[] );
      26             : static void Map_higher_LSF_fx( Word16 lsf_q[], const Word16 m, const Word16 grid_in[] );
      27             : static void Dequant_mirror_point_fx( const Word16 lsf_q[], const Word16 m_idx, Word16 *m );
      28             : static Word16 dotp_loc( const Word16 x[], const Word32 y[], const Word16 n );
      29             : static void find_max_mem_dec_m3( Decoder_State *st, Word16 *n_mem3 );
      30             : 
      31             : /* gain shape concealment code */
      32             : static void gradientGainShape( Decoder_State *st_fx, Word16 *GainShape, Word32 *GainFrame );
      33             : 
      34             : /*-------------------------------------------------------------------*
      35             :  * find_max_mem_dec()
      36             :  *
      37             :  * Find norm and max in TBE memories and past buffers
      38             :  *-------------------------------------------------------------------*/
      39      103912 : static void find_max_mem_dec(
      40             :     Decoder_State *st_fx,
      41             :     Word16 *n_mem,
      42             :     Word16 *n_mem2,
      43             :     Word16 *n_mem3 )
      44             : {
      45             :     Word16 i;
      46             :     Word16 n_mem_32;
      47      103912 :     Word16 max = 0;
      48      103912 :     move16();
      49      103912 :     Word32 Lmax = 0;
      50      103912 :     move32();
      51      103912 :     Word16 tempQ15, max2 = 0;
      52      103912 :     move16();
      53             :     Word16 max3;
      54             :     Word32 tempQ32, Lmax3;
      55             :     TD_BWE_DEC_HANDLE hBWE_TD;
      56      103912 :     hBWE_TD = st_fx->hBWE_TD;
      57             : 
      58             :     /* old BWE exc max */
      59     1350856 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
      60             :     {
      61     1246944 :         tempQ15 = abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] );
      62     1246944 :         max = s_max( max, tempQ15 );
      63             :     }
      64             : 
      65             :     /* decimate all-pass steep memory */
      66      831296 :     FOR( i = 0; i < 7; i++ )
      67             :     {
      68      727384 :         tempQ15 = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
      69      727384 :         max = s_max( max, tempQ15 );
      70             :     }
      71             : 
      72             :     /*  -- keep norm of state_lpc_syn_fx, state_syn_shbexc_fx,
      73             :            and mem_stp_swb_fx separately for 24.4 and 32kbps ----*/
      74             :     /* findMaxMem2() inside tbe com */
      75     1143032 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
      76             :     {
      77     1039120 :         tempQ15 = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
      78     1039120 :         max2 = s_max( max2, tempQ15 );
      79             :     }
      80             : 
      81             :     /* findMaxMem2() inside tbe com */
      82     2182152 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
      83             :     {
      84     2078240 :         tempQ15 = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
      85     2078240 :         max2 = s_max( max2, tempQ15 );
      86             :     }
      87             : 
      88             :     /* findMaxMem2() inside tbe com */
      89     1143032 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
      90             :     {
      91     1039120 :         tempQ15 = abs_s( hBWE_TD->mem_stp_swb_fx[i] );
      92     1039120 :         max2 = s_max( max2, tempQ15 );
      93             :     }
      94             : 
      95             :     /* for total_brate > 16.4kbps, use n_mem2; else account for the max2 for n_mem calculation */
      96      103912 :     *n_mem2 = norm_s( max2 );
      97      103912 :     move16();
      98      103912 :     if ( max2 == 0 )
      99             :     {
     100       83240 :         *n_mem2 = 15;
     101       83240 :         move16();
     102             :     }
     103             : 
     104      103912 :     if ( LT_32( st_fx->total_brate, ACELP_24k40 ) )
     105             :     {
     106       81346 :         max = s_max( max, max2 );
     107             :     }
     108             : 
     109             :     /* de-emph and pre-emph memory */
     110      103912 :     tempQ15 = abs_s( hBWE_TD->tbe_demph_fx );
     111      103912 :     max = s_max( max, tempQ15 );
     112             : 
     113      103912 :     tempQ15 = abs_s( hBWE_TD->tbe_premph_fx );
     114      103912 :     max = s_max( max, tempQ15 );
     115             : 
     116      103912 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
     117             :     {
     118      439890 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     119             :         {
     120      399900 :             tempQ15 = abs_s( hBWE_TD->fb_state_lpc_syn_fx[i] );
     121      399900 :             max = s_max( max, tempQ15 );
     122             :         }
     123             :         /* FB de-emph memory */
     124       39990 :         tempQ15 = abs_s( hBWE_TD->fb_tbe_demph_fx );
     125       39990 :         max = s_max( max, tempQ15 );
     126             :     }
     127             :     /* estimate the norm for 16-bit memories  */
     128      103912 :     *n_mem = norm_s( max );
     129      103912 :     move16();
     130      103912 :     if ( max == 0 )
     131             :     {
     132         129 :         *n_mem = 15;
     133         129 :         move16();
     134             :     }
     135             : 
     136             :     /* estimate the norm for 32-bit memories */
     137      103912 :     Lmax = L_abs( hBWE_TD->mem_csfilt_fx[0] ); /* only element [0] is used in env. shaping */
     138             : 
     139      103912 :     n_mem_32 = norm_l( Lmax );
     140      103912 :     if ( Lmax == 0 )
     141             :     {
     142       34895 :         n_mem_32 = 31;
     143       34895 :         move16();
     144             :     }
     145             : 
     146      103912 :     tempQ15 = sub( s_min( *n_mem, n_mem_32 ), 1 );
     147      103912 :     *n_mem = s_max( tempQ15, 0 );
     148      103912 :     move16();
     149             : 
     150             :     /* --------------------------------------------------------------*/
     151             :     /* Find headroom for synthesis stage associated with these memories:
     152             :           1. st_fx->syn_overlap_fx
     153             :           2. st_fx->genSHBsynth_state_lsyn_filt_shb_local
     154             :           3. st_fx->genSHBsynth_Hilbert_Mem_fx (32-bit) */
     155      103912 :     max3 = 0;
     156      103912 :     move16();
     157             :     /* find max in prev overlapSyn */
     158     2182152 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
     159             :     {
     160     2078240 :         tempQ15 = abs_s( hBWE_TD->syn_overlap_fx[i] );
     161     2078240 :         max3 = s_max( max3, tempQ15 );
     162             :     }
     163             :     /* find max in prev genSHBsynth_state_lsyn_filt_shb_local_fx */
     164      727384 :     FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
     165             :     {
     166      623472 :         tempQ15 = abs_s( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i] );
     167      623472 :         max3 = s_max( max3, tempQ15 );
     168             :     }
     169             :     /* find max in prev int_3_over_2_tbemem_dec_fx */
     170      103912 :     IF( EQ_32( st_fx->output_Fs, 48000 ) )
     171             :     {
     172     1140560 :         FOR( i = 0; i < INTERP_3_2_MEM_LEN; i++ )
     173             :         {
     174     1069275 :             tempQ15 = abs_s( hBWE_TD->int_3_over_2_tbemem_dec_fx[i] );
     175     1069275 :             max3 = s_max( max3, tempQ15 );
     176             :         }
     177             :     }
     178      103912 :     IF( EQ_32( st_fx->output_Fs, 16000 ) )
     179             :     {
     180       34208 :         FOR( i = 0; i < ( 2 * ALLPASSSECTIONS_STEEP + 1 ); i++ )
     181             :         {
     182       29932 :             tempQ15 = abs_s( hBWE_TD->mem_resamp_HB_32k_fx[i] );
     183       29932 :             max3 = s_max( max3, tempQ15 );
     184             :         }
     185             :     }
     186             :     /* estimate the norm for 16-bit memories  */
     187      103912 :     *n_mem3 = norm_s( max3 );
     188      103912 :     move16();
     189      103912 :     if ( max3 == 0 )
     190             :     {
     191        8822 :         *n_mem3 = 15;
     192        8822 :         move16();
     193             :     }
     194             : 
     195      103912 :     Lmax3 = 0;
     196      103912 :     move32();
     197      103912 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     198             :     {
     199             :         /* find max in prev genSHBsynth_Hilbert_Mem_fx */
     200     1084996 :         FOR( i = 0; i < HILBERT_MEM_SIZE; i++ )
     201             :         {
     202     1035678 :             tempQ32 = L_abs( hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i] );
     203     1035678 :             Lmax3 = L_max( Lmax3, tempQ32 );
     204             :         }
     205             :     }
     206             : 
     207             :     /* estimate the norm for 32-bit memories  */
     208      103912 :     n_mem_32 = norm_l( Lmax3 );
     209      103912 :     if ( Lmax3 == 0 )
     210             :     {
     211       59103 :         n_mem_32 = 31;
     212       59103 :         move16();
     213             :     }
     214             : 
     215      103912 :     tempQ15 = sub( s_min( *n_mem3, n_mem_32 ), 2 ); /* very important leave at least 2 bit head room
     216             :                                                        because of the Hilber transform and Q14 coeffs */
     217      103912 :     *n_mem3 = s_max( tempQ15, 0 );
     218      103912 :     move16();
     219             :     /* --------------------------------------------------------------*/
     220      103912 : }
     221             : 
     222             : /*-------------------------------------------------------------------*
     223             :  * rescale_genSHB_mem_dec()
     224             :  *
     225             :  * Rescale genSHB memories
     226             :  *-------------------------------------------------------------------*/
     227         699 : static void rescale_genSHB_mem_dec(
     228             :     Decoder_State *st_fx,
     229             :     Word16 sf )
     230             : {
     231             :     Word16 i;
     232             :     TD_BWE_DEC_HANDLE hBWE_TD;
     233         699 :     hBWE_TD = st_fx->hBWE_TD;
     234             : 
     235        9087 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     236             :     {
     237        8388 :         hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
     238        8388 :         move16();
     239             :     }
     240             : 
     241        5592 :     FOR( i = 0; i < 7; i++ )
     242             :     {
     243        4893 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
     244        4893 :         move16();
     245             :     }
     246             : 
     247             :     /*  -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
     248         699 :     IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
     249             :     {
     250        4147 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     251             :         {
     252        3770 :             hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
     253        3770 :             move16();
     254             :         }
     255             : 
     256        7917 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
     257             :         {
     258        7540 :             hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
     259        7540 :             move16();
     260             :         }
     261             :     }
     262             : 
     263         699 :     if ( EQ_16( st_fx->extl, FB_TBE ) )
     264             :     {
     265             :     }
     266         699 :     hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
     267         699 :     move32();
     268             : 
     269         699 :     hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
     270         699 :     move16();
     271         699 :     hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
     272         699 :     move16();
     273         699 : }
     274             : 
     275        6870 : void find_max_mem_wb( Decoder_State *st_fx, Word16 *n_mem )
     276             : {
     277             :     Word16 i;
     278        6870 :     Word16 max = 0;
     279        6870 :     move16();
     280        6870 :     Word32 Lmax = 0;
     281        6870 :     move32();
     282             :     Word16 n_mem_32;
     283             :     TD_BWE_DEC_HANDLE hBWE_TD;
     284        6870 :     hBWE_TD = st_fx->hBWE_TD;
     285             : 
     286       89310 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     287             :     {
     288       82440 :         max = s_max( max, abs_s( hBWE_TD->old_bwe_exc_extended_fx[i] ) );
     289             :     }
     290       54960 :     FOR( i = 0; i < 7; i++ )
     291             :     {
     292             : 
     293       48090 :         if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] ), max ) )
     294        5491 :             max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] );
     295             :     }
     296             : 
     297       54960 :     FOR( i = 0; i < 7; i++ )
     298             :     {
     299       48090 :         if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] ), max ) )
     300        3954 :             max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] );
     301             :     }
     302             : 
     303       54960 :     FOR( i = 0; i < 7; i++ )
     304             :     {
     305             : 
     306       48090 :         if ( GT_16( abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] ), max ) )
     307        1204 :             max = abs_s( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] );
     308             :     }
     309             : 
     310       75570 :     FOR( i = 0; i < 10; i++ )
     311             :     {
     312             : 
     313       68700 :         if ( GT_16( abs_s( hBWE_TD->state_lpc_syn_fx[i] ), max ) )
     314        1409 :             max = abs_s( hBWE_TD->state_lpc_syn_fx[i] );
     315             :     }
     316             : 
     317       41220 :     FOR( i = 0; i < 5; i++ )
     318             :     {
     319             : 
     320       34350 :         if ( GT_16( abs_s( hBWE_TD->state_syn_shbexc_fx[i] ), max ) )
     321         259 :             max = abs_s( hBWE_TD->state_syn_shbexc_fx[i] );
     322             :     }
     323             : 
     324        6870 :     IF( max == 0 )
     325             :     {
     326           0 :         *n_mem = 15;
     327           0 :         move16();
     328             :     }
     329             :     ELSE
     330             :     {
     331        6870 :         *n_mem = norm_s( max );
     332        6870 :         move16();
     333             :     }
     334             : 
     335             : 
     336       20610 :     FOR( i = 0; i < 2; i++ )
     337             :     {
     338             : 
     339       13740 :         if ( GT_32( L_abs( hBWE_TD->mem_csfilt_fx[i] ), Lmax ) )
     340        6548 :             Lmax = L_abs( hBWE_TD->mem_csfilt_fx[i] );
     341             :     }
     342             : 
     343        6870 :     IF( Lmax == 0 )
     344             :     {
     345         322 :         n_mem_32 = 31;
     346         322 :         move16();
     347             :     }
     348             :     ELSE
     349             :     {
     350        6548 :         n_mem_32 = norm_l( Lmax );
     351             :     }
     352             : 
     353        6870 :     *n_mem = sub( s_min( *n_mem, n_mem_32 ), 1 );
     354        6870 :     move16();
     355        6870 :     *n_mem = s_max( *n_mem, 0 );
     356        6870 :     move16();
     357        6870 : }
     358             : 
     359        6870 : void rescale_genWB_mem( Decoder_State *st_fx, Word16 sf )
     360             : {
     361             :     Word16 i;
     362             :     TD_BWE_DEC_HANDLE hBWE_TD;
     363        6870 :     hBWE_TD = st_fx->hBWE_TD;
     364             : 
     365       89310 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
     366             :     {
     367       82440 :         hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
     368       82440 :         move16();
     369             :     }
     370             : 
     371       75570 :     FOR( i = 0; i < 10; i++ )
     372             :     {
     373       68700 :         hBWE_TD->state_lpc_syn_fx[i] = shl( hBWE_TD->state_lpc_syn_fx[i], sf );
     374       68700 :         move16();
     375             :     }
     376             : 
     377       41220 :     FOR( i = 0; i < 5; i++ )
     378             :     {
     379       34350 :         hBWE_TD->state_syn_shbexc_fx[i] = shl( hBWE_TD->state_syn_shbexc_fx[i], sf );
     380       34350 :         move16();
     381             :     }
     382             : 
     383       54960 :     FOR( i = 0; i < 7; i++ )
     384             :     {
     385       48090 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
     386       48090 :         move16();
     387             :     }
     388             : 
     389       54960 :     FOR( i = 0; i < 7; i++ )
     390             :     {
     391       48090 :         hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx[i], sf );
     392       48090 :         move16();
     393             :     }
     394             : 
     395       54960 :     FOR( i = 0; i < 7; i++ )
     396             :     {
     397       48090 :         hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx[i], sf );
     398       48090 :         move16();
     399             :     }
     400             : 
     401       20610 :     FOR( i = 0; i < 2; i++ )
     402             :     {
     403       13740 :         hBWE_TD->mem_csfilt_fx[i] = L_shl( hBWE_TD->mem_csfilt_fx[i], sf );
     404       13740 :         move32();
     405             :     }
     406             : 
     407        6870 :     return;
     408             : }
     409             : 
     410             : 
     411        3085 : static void InitSWBdecBuffer_fx(
     412             :     TD_BWE_DEC_HANDLE hBWE_TD /* TD BWE data handle */ )
     413             : {
     414        3085 :     set16_fx( hBWE_TD->old_bwe_exc_fx, 0, ( PIT16k_MAX * 2 ) );
     415        3085 :     hBWE_TD->bwe_seed[0] = 23;
     416        3085 :     move16();
     417        3085 :     hBWE_TD->bwe_seed[1] = 59;
     418        3085 :     move16();
     419             : 
     420        3085 :     set16_fx( hBWE_TD->old_bwe_exc_extended_fx, 0, NL_BUFF_OFFSET );
     421        3085 :     hBWE_TD->bwe_non_lin_prev_scale_fx = 0;
     422        3085 :     move16();
     423             : 
     424        3085 :     set32_fx( hBWE_TD->genSHBsynth_Hilbert_Mem_fx, 0, HILBERT_MEM_SIZE );
     425        3085 :     set16_fx( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, 0, 2 * ALLPASSSECTIONS_STEEP );    /* Interp all pass memory */
     426        3085 :     set32_fx( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP ); /* Interp all pass memory */
     427             : 
     428        3085 :     hBWE_TD->syn_dm_phase = 0;
     429        3085 :     move16();
     430        3085 :     hBWE_TD->prev_fbbwe_ratio_fx = 32767 /*1.0f Q15*/;
     431        3085 :     move16();
     432             : 
     433             :     /* these are fd-bwe constants */
     434        3085 :     hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
     435        3085 :     move32();
     436        3085 :     hBWE_TD->prev_swb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
     437        3085 :     move32();
     438        3085 :     hBWE_TD->prev_ener_fx_Q = 31;
     439        3085 :     move16();
     440        3085 :     hBWE_TD->prev_Qx = 0;
     441        3085 :     move16();
     442        3085 :     hBWE_TD->prev_frame_pow_exp = 0;
     443        3085 :     move16();
     444        3085 :     hBWE_TD->prev_Q_bwe_syn = 0;
     445        3085 :     move16();
     446        3085 :     hBWE_TD->prev_Q_bwe_syn2 = 0;
     447        3085 :     move16();
     448        3085 :     hBWE_TD->prev_hb_synth_fx_exp = 31;
     449        3085 :     move16();
     450             : 
     451        3085 :     return;
     452             : }
     453             : 
     454             : 
     455        4873 : void ResetSHBbuffer_Dec_fx(
     456             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
     457             :     const Word16 extl          /* i  : BWE extension layer     */
     458             : )
     459             : {
     460             :     Word16 i;
     461             :     Word16 f;
     462             :     Word16 inc;
     463             : 
     464        4873 :     IF( NE_16( extl, WB_TBE ) )
     465             :     {
     466        4873 :         f = 1489;
     467        4873 :         move16(); /* Q15 */
     468        4873 :         inc = 1489;
     469        4873 :         move16(); /* Q15 */
     470             :     }
     471             :     ELSE
     472             :     {
     473           0 :         f = 5461;
     474           0 :         move16(); /* Q15 */
     475           0 :         inc = 5461;
     476           0 :         move16(); /* Q15 */
     477             :     }
     478             : 
     479             :     /* states for the filters used in generating SHB excitation from WB excitation*/
     480        4873 :     set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
     481             : 
     482             :     /* states for the filters used in generating SHB signal from SHB excitation*/
     483        4873 :     set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD );
     484        4873 :     set16_fx( hBWE_TD->state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     485             : 
     486        4873 :     IF( EQ_16( extl, FB_TBE ) )
     487             :     {
     488           0 :         set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
     489           0 :         hBWE_TD->fb_tbe_demph_fx = 0;
     490           0 :         move16();
     491           0 :         fb_tbe_reset_synth_fx( hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, &hBWE_TD->prev_fbbwe_ratio_fx );
     492             :     }
     493             :     /* states for the filters used in generating SHB signal from SHB excitation in wideband*/
     494        4873 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     495        4873 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     496        4873 :     set16_fx( hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
     497        4873 :     set16_fx( hBWE_TD->state_lsyn_filt_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
     498        4873 :     set32_fx( hBWE_TD->state_lsyn_filt_shb_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP );
     499        4873 :     set16_fx( hBWE_TD->state_lsyn_filt_dwn_shb_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
     500        4873 :     set32_fx( hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP );
     501        4873 :     set16_fx( hBWE_TD->state_32and48k_WB_upsample_fx, 0, 2 * ALLPASSSECTIONS_STEEP );
     502             : 
     503             :     /* States for the local synthesis filters */
     504        4873 :     set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
     505        4873 :     set32_fx( hBWE_TD->syn_overlap_fx_32, 0, L_SHB_LAHEAD );
     506             : 
     507             :     /* States for FEC */
     508             : 
     509        4873 :     IF( NE_16( extl, WB_TBE ) )
     510             :     {
     511       53603 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
     512             :         {
     513       48730 :             hBWE_TD->lsp_prevfrm_fx[i] = f;
     514       48730 :             move16(); /*Q15*/
     515       48730 :             f = add( f, inc );
     516       48730 :             move16();
     517             :         }
     518             :     }
     519             :     ELSE
     520             :     {
     521           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     522             :         {
     523           0 :             hBWE_TD->lsp_prevfrm_fx[i] = f;
     524           0 :             move16(); /*Q15*/
     525           0 :             f = add( f, inc );
     526           0 :             move16();
     527             :         }
     528           0 :         FOR( ; i < LPC_SHB_ORDER; i++ )
     529             :         {
     530           0 :             hBWE_TD->lsp_prevfrm_fx[i] = 0;
     531           0 :             move16();
     532             :         }
     533             :     }
     534        4873 :     hBWE_TD->GainFrame_prevfrm_fx = 0;
     535        4873 :     move16(); /*Q18*/
     536        4873 :     hBWE_TD->GainAttn_fx = 32767;
     537        4873 :     move16(); /*Q15*/
     538        4873 :     hBWE_TD->tbe_demph_fx = 0;
     539        4873 :     move16();
     540        4873 :     hBWE_TD->tbe_premph_fx = 0;
     541        4873 :     move16();
     542        4873 :     set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
     543        4873 :     hBWE_TD->gain_prec_swb_fx = 16384; /*Q14 =1*/
     544        4873 :     move16();
     545        4873 :     set16_fx( hBWE_TD->GainShape_Delay_fx, 0, NUM_SHB_SUBFR / 2 );
     546        4873 :     hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /* Q0 1.f */
     547        4873 :     move32();
     548        4873 :     hBWE_TD->prev_mix_factor_fx = 32767; /*Q15 1.f*/
     549        4873 :     move16();
     550             : 
     551        4873 :     set16_fx( hBWE_TD->old_core_synth_fx, 0, L_FRAME16k );
     552        4873 :     set16_fx( hBWE_TD->old_tbe_synth_fx, 0, L_SHB_TRANSITION_LENGTH );
     553        4873 :     set32_fx( hBWE_TD->old_tbe_synth_fx_32, 0, L_SHB_TRANSITION_LENGTH );
     554        4873 :     hBWE_TD->tilt_swb_fec_fx = 0;
     555        4873 :     move16();
     556             : 
     557        4873 :     return;
     558             : }
     559             : 
     560             : 
     561             : /*==========================================================================*/
     562             : /* FUNCTION      : void wb_tbe_dec_fx ()                                    */
     563             : /*--------------------------------------------------------------------------*/
     564             : /* PURPOSE       : WB TBE decoder, 6 - 8 kHz band decoding module           */
     565             : /*--------------------------------------------------------------------------*/
     566             : /* INPUT ARGUMENTS  :                                                       */
     567             : /* _Word16 coder_type             i  : coding type                          */
     568             : /* _Word32 *bwe_exc_extended      i  : bandwidth extended exciatation 2*Q_exc*/
     569             : /* _Word16 Q_exc                  i  : Q format                             */
     570             : /* _Word16 voice_factors[]        i  : voicing factors          Q15         */
     571             : /*--------------------------------------------------------------------------*/
     572             : /* OUTPUT ARGUMENTS :                                                       */
     573             : /* _Word16 *synth               o  : WB synthesis/final synthesis  Q_synth  */
     574             : /*--------------------------------------------------------------------------*/
     575             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
     576             : /* Decoder_State *st_fx,       i/o: decoder state structure              */
     577             : /*--------------------------------------------------------------------------*/
     578             : /* RETURN ARGUMENTS :                                                       */
     579             : /*           _ None                                                         */
     580             : /*--------------------------------------------------------------------------*/
     581             : /* CALLED FROM :                                                            */
     582             : /*==========================================================================*/
     583        6950 : void ivas_wb_tbe_dec_fx(
     584             :     Decoder_State *st_fx,     /* i/o: decoder state structure         */
     585             :     const Word16 coder_type,  /* i  : coding type                 Q0     */
     586             :     Word32 *bwe_exc_extended, /* i  : bandwidth extended exciatation 2*Q_exc*/
     587             :     const Word16 Q_exc,
     588             :     const Word16 voice_factors[], /* i  : voicing factors              Q15   */
     589             :     Word16 *synth,                /* o  : WB synthesis/final synthesis  Q_synth  */
     590             :     Word16 *Q_synth )
     591             : {
     592             :     Word16 i;
     593             :     Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
     594             :     Word16 shaped_wb_excitation_frac[L_FRAME16k / 4];
     595             :     Word16 bwe_exc_extended_16[L_FRAME32k + 40];
     596             :     Word16 exc4kWhtnd[L_FRAME16k / 4];
     597             :     Word16 lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR];
     598             :     Word32 GainFrame;
     599             :     Word16 error[L_FRAME16k];
     600             :     Word16 synth_frac[L_FRAME16k];
     601             :     Word16 upsampled_synth[L_FRAME48k];
     602             :     Word32 prev_pow, curr_pow, curr_frame_pow;
     603             :     Word16 curr_frame_pow_exp;
     604             :     Word16 temp, scale, n;
     605             :     Word16 j;
     606             : 
     607             :     Word16 Q_bwe_exc, Q_bwe_exc_ext, Qx;
     608             :     Word16 n_mem, cnt;
     609        6950 :     Word16 max = 0;
     610        6950 :     move16();
     611        6950 :     Word32 L_tmp, Lacc, Lscale, Lmax = 0;
     612        6950 :     move32();
     613             :     Word16 tmp, exp, sc;
     614             :     Word16 vf_modified[NB_SUBFR16k];
     615        6950 :     Word16 uv_flag = 0;
     616        6950 :     move16();
     617        6950 :     Word16 dummy = 0;
     618        6950 :     move16();
     619             :     Word32 dummy2[HILBERT_MEM_SIZE];
     620             :     Word16 f, inc;
     621             :     Word64 W_tmp;
     622             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     623        6950 :     Flag Overflow = 0;
     624        6950 :     move32();
     625             : #endif
     626             :     TD_BWE_DEC_HANDLE hBWE_TD;
     627        6950 :     hBWE_TD = st_fx->hBWE_TD;
     628        6950 :     set32_fx( &dummy2[0], 0, HILBERT_MEM_SIZE );
     629        6950 :     IF( st_fx->bws_cnt == 0 )
     630             :     {
     631             :         /* Initialization */
     632        6870 :         set16_fx( GainShape, 11469 /*0.35f Q15*/, NUM_SHB_SUBFR );
     633        6870 :         GainFrame = 1;
     634        6870 :         move32();
     635             : 
     636        6870 :         IF( !st_fx->bfi )
     637             :         {
     638        6839 :             IF( EQ_16( st_fx->use_partial_copy, 1 ) )
     639             :             {
     640           0 :                 IF( NE_16( st_fx->last_extl, WB_TBE ) )
     641             :                 {
     642           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
     643           0 :                     move32();
     644           0 :                     hBWE_TD->lsp_prevfrm_fx[0] = 3277 /*0.1f Q15*/;
     645           0 :                     move16();
     646           0 :                     FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
     647             :                     {
     648           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = add( hBWE_TD->lsp_prevfrm_fx[i - i], 3277 /*0.1f Q15*/ );
     649           0 :                         move16();
     650             :                     }
     651             :                 }
     652           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     653           0 :                 set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
     654             : 
     655           0 :                 IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
     656             :                 {
     657             :                     /* Frame gain */
     658           0 :                     st_fx->rf_indx_tbeGainFr = s_and( st_fx->rf_indx_tbeGainFr, 0xF ); /* only four LSBs are valid */
     659           0 :                     move16();
     660           0 :                     Copy32( SHBCB_FrameGain16_fx + st_fx->rf_indx_tbeGainFr, &GainFrame, 1 );
     661           0 :                     test();
     662           0 :                     test();
     663           0 :                     test();
     664           0 :                     test();
     665           0 :                     test();
     666           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && EQ_16( st_fx->last_extl, WB_TBE ) )
     667             :                     {
     668             :                         /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
     669           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx /*Q18*/, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame /*Q18*/, 6553 /*0.2f in Q15*/ ) /*Q18+Q15 - 15*/ ); /*Q18*/
     670             :                     }
     671             :                 }
     672             :                 ELSE
     673             :                 {
     674           0 :                     temp = 0;
     675           0 :                     move16();
     676             :                     /* Frame gain */
     677           0 :                     SWITCH( st_fx->rf_indx_tbeGainFr )
     678             :                     {
     679           0 :                         case 0:
     680           0 :                             GainFrame = 131072; /* 0.5f in Q18 */
     681           0 :                             move32();
     682           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
     683             :                             {
     684           0 :                                 temp = 26214 /*0.8 Q15*/;
     685           0 :                                 move16();
     686             :                             }
     687           0 :                             BREAK;
     688           0 :                         case 1:
     689           0 :                             GainFrame = 524288; /* 2.0f in Q18 */
     690           0 :                             move32();
     691           0 :                             test();
     692           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
     693             :                             {
     694           0 :                                 temp = 26214 /*0.8 Q15*/;
     695           0 :                                 move16();
     696             :                             }
     697           0 :                             BREAK;
     698           0 :                         case 2:
     699           0 :                             GainFrame = 1048576; /* 4.0f in Q18 */
     700           0 :                             move32();
     701           0 :                             test();
     702           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
     703             :                             {
     704           0 :                                 temp = 26214 /*0.8 Q15*/;
     705           0 :                                 move16();
     706             :                             }
     707           0 :                             BREAK;
     708           0 :                         case 3:
     709           0 :                             GainFrame = 2097152; /* 8.0f in Q18 */
     710           0 :                             move32();
     711           0 :                             test();
     712           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16 Q18*/ ) )
     713             :                             {
     714           0 :                                 temp = 26214 /*0.8 Q15*/;
     715           0 :                                 move16();
     716             :                             }
     717           0 :                             BREAK;
     718           0 :                         default:
     719           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
     720             :                     }
     721           0 :                     IF( EQ_16( st_fx->last_extl, WB_TBE ) )
     722             :                     {
     723             : 
     724           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
     725             :                     }
     726           0 :                     test();
     727           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
     728             :                     {
     729           0 :                         test();
     730           0 :                         test();
     731           0 :                         test();
     732           0 :                         test();
     733           0 :                         IF( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && LT_16( st_fx->prev_tilt_code_dec_fx, 1497 ) && GT_16( st_fx->prev_tilt_code_dec_fx, 200 ) )
     734             :                         {
     735           0 :                             GainFrame = Mult_32_16( GainFrame, 9830 /*0.3f in Q15*/ ); /*Q18*/
     736             :                         }
     737             :                     }
     738             :                 }
     739             :             }
     740             :             ELSE
     741             :             {
     742             :                 /* de-quantization */
     743             :                 Word16 ignore;
     744        6839 :                 ivas_dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0, &ignore );
     745             :             }
     746             :         }
     747             :         ELSE
     748             :         {
     749          31 :             IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
     750             :             {
     751           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB );
     752             :             }
     753             :             ELSE
     754             :             {
     755          31 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_WB );
     756             :             }
     757          31 :             set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
     758             : 
     759          31 :             hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27853 /*0.85f in Q15*/ ); /*Q15*/
     760          31 :             move16();
     761             : 
     762          31 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
     763             :             {
     764          31 :                 GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx /*Q18*/, hBWE_TD->GainAttn_fx /*Q15*/ ); /*Q15+Q18-15*/
     765             :             }
     766             :             ELSE
     767             :             {
     768           0 :                 GainFrame = hBWE_TD->GainFrame_prevfrm_fx; /*Q18*/
     769           0 :                 move32();
     770             :             }
     771             :         }
     772             : 
     773        6870 :         IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
     774             :         {
     775             :             /* convert LSPs back into LP coeffs */
     776        2427 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
     777        2427 :             set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, ( LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB ) );
     778       16989 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     779             :             {
     780       14562 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
     781       14562 :                 move16();
     782             :             }
     783       12135 :             FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
     784             :             {
     785        9708 :                 lpc_wb[i] = negate( lpc_wb[i] );
     786        9708 :                 move16();
     787             :             }
     788        2427 :             lpc_wb[0] = 4096; /*1.0f in Q12*/
     789        2427 :             move16();
     790             :         }
     791             :         ELSE
     792             :         {
     793             :             /* convert LSPs back into LP coeffs */
     794        4443 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
     795       31101 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
     796             :             {
     797       26658 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
     798       26658 :                 move16();
     799             :             }
     800       31101 :             FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
     801             :             {
     802       26658 :                 lpc_wb[i] = negate( lpc_wb[i] );
     803       26658 :                 move16();
     804             :             }
     805        4443 :             lpc_wb[0] = 4096; /*1.0f in Q12*/
     806        4443 :             move16();
     807             :         }
     808             : 
     809        6870 :         Copy( voice_factors, vf_modified, NB_SUBFR16k );
     810        6870 :         IF( EQ_16( coder_type, VOICED ) )
     811             :         {
     812        4680 :             FOR( i = 1; i < NB_SUBFR; i++ )
     813             :             {
     814        3510 :                 vf_modified[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6554, voice_factors[i - 1] ) ); /* Q15 */
     815        3510 :                 move16();
     816             :             }
     817        1170 :             IF( NE_16( st_fx->L_frame, L_FRAME ) )
     818             :             {
     819           0 :                 vf_modified[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6554, voice_factors[3] ) ); /* Q15 */
     820           0 :                 move16();
     821             :             }
     822             :         }
     823             : 
     824             :         /* From low band excitation, generate highband excitation */
     825        6870 :         Lmax = 0;
     826        6870 :         move32();
     827     4486110 :         FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
     828             :         {
     829     4479240 :             Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
     830             :         }
     831        6870 :         IF( Lmax == 0 )
     832             :         {
     833           0 :             Q_bwe_exc = 31;
     834           0 :             move16();
     835             :         }
     836             :         ELSE
     837             :         {
     838        6870 :             Q_bwe_exc = norm_l( Lmax );
     839             :         }
     840        6870 :         Q_bwe_exc = sub( Q_bwe_exc, 3 );
     841        6870 :         Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
     842             : 
     843        6870 :         find_max_mem_wb( st_fx, &n_mem );
     844             : 
     845        6870 :         if ( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
     846             :         {
     847          65 :             Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
     848             :         }
     849             : 
     850        6870 :         test();
     851        6870 :         if ( uv_flag && GT_16( Q_bwe_exc, 20 ) )
     852             :         {
     853         185 :             Q_bwe_exc = 20;
     854         185 :             move16(); /* restrict this to 21 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
     855             :         }
     856             : 
     857        6870 :         W_tmp = 0;
     858        6870 :         move64();
     859        6870 :         IF( st_fx->element_mode > EVS_MONO )
     860             :         {
     861        6870 :             tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
     862        6870 :             W_tmp = L_shl( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
     863             :         }
     864       41220 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
     865             :         {
     866       34350 :             W_tmp = W_mac_16_16( W_tmp, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /*Q(2*(st_fx->prev_Q_bwe_exc-16 + 1))*/
     867             :         }
     868        6870 :         exp = W_norm( W_tmp );
     869        6870 :         prev_pow = W_extract_h( W_shl( W_tmp, exp ) );
     870        6870 :         exp = sub( add( add( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 1 ), exp ), 32 );
     871             : 
     872        6870 :         rescale_genWB_mem( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
     873             : 
     874        6870 :         sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
     875     4486110 :         FOR( cnt = 0; cnt < L_FRAME32k + NL_BUFF_OFFSET; cnt++ )
     876             :         {
     877     4479240 :             bwe_exc_extended_16[cnt] = round_fx( L_shl( bwe_exc_extended[cnt], sc ) );
     878     4479240 :             move16();
     879             :         }
     880             : 
     881        6870 :         Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
     882             : 
     883        6870 :         Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
     884             : 
     885        6870 :         GenShapedWBExcitation_ivas_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
     886        6870 :                                        hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx,
     887        6870 :                                        hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, hBWE_TD->state_lpc_syn_fx, coder_type,
     888        6870 :                                        bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, vf_modified, uv_flag, st_fx->igf );
     889             : 
     890        6870 :         curr_pow = 0;
     891        6870 :         move32();
     892        6870 :         IF( st_fx->element_mode > EVS_MONO )
     893             :         {
     894        6870 :             tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
     895        6870 :             curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
     896             :         }
     897       41220 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
     898             :         {
     899       34350 :             curr_pow = L_mac0_sat( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
     900             :         }
     901             : 
     902        6870 :         if ( GT_16( voice_factors[0], 24576 ) )
     903             :         {
     904         243 :             curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
     905             :         }
     906             : 
     907        6870 :         Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow,
     908             :                                    exp, &exp );
     909             : 
     910       34350 :         FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
     911             :         {
     912       27480 :             L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] );             /* Q(16-exp+Q_bwe_exc_ext) */
     913       27480 :             shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
     914       27480 :             move16();
     915             :         }
     916        6870 :         Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
     917        6870 :         L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] );     /* Q(16-exp+Q_bwe_exc_ext) */
     918        6870 :         shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
     919        6870 :         move16();
     920             : 
     921             :         /* Update SHB excitation */
     922        6870 :         Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
     923             : 
     924             : 
     925             :         /* Adjust the subframe and frame gain of the synthesized shb signal */
     926             :         /* Scale the shaped excitation */
     927        6870 :         ScaleShapedWB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame,
     928             :                           window_wb_fx, subwin_wb_fx,
     929        6870 :                           Q_bwe_exc_ext, st_fx->L_frame, 0, &dummy, dummy, dummy2 );
     930             : 
     931        6870 :         max = 0;
     932        6870 :         move16();
     933      556470 :         FOR( i = 0; i < L_FRAME16k / 4; i++ )
     934             :         {
     935      549600 :             max = s_max( max, abs_s( shaped_wb_excitation[i] ) );
     936             :         }
     937             : 
     938        6870 :         IF( max == 0 )
     939             :         {
     940         191 :             curr_frame_pow = 1;
     941         191 :             move16();
     942         191 :             n = 0;
     943         191 :             move16();
     944             :         }
     945             :         ELSE
     946             :         {
     947        6679 :             n = norm_s( max );
     948      540999 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
     949             :             {
     950      534320 :                 shaped_wb_excitation_frac[i] = shl_o( shaped_wb_excitation[i], n, &Overflow ); /*Q14*/
     951      534320 :                 move16();
     952             :             }
     953        6679 :             n = sub( 14, n );
     954        6679 :             curr_frame_pow = 1;
     955        6679 :             move32();
     956      540999 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
     957             :             {
     958      534320 :                 L_tmp = L_mult_o( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i], &Overflow ); /*Q29*/
     959      534320 :                 curr_frame_pow = L_add_o( curr_frame_pow, L_shr( L_tmp, 7 ), &Overflow );                  /*Q22*/
     960             :             }
     961             :         }
     962        6870 :         curr_frame_pow_exp = add( n, n );
     963             : 
     964        6870 :         IF( GT_16( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) )
     965             :         {
     966        6085 :             curr_frame_pow = L_shr( curr_frame_pow, sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) );
     967        6085 :             curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
     968        6085 :             move16();
     969             :         }
     970             :         ELSE
     971             :         {
     972         785 :             hBWE_TD->prev_wb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_wb_bwe_frame_pow_fx, sub( curr_frame_pow_exp, hBWE_TD->prev_frame_pow_exp ) );
     973         785 :             move32();
     974             :         }
     975             : 
     976        6870 :         test();
     977        6870 :         test();
     978        6870 :         IF( !st_fx->bfi && ( st_fx->prev_bfi || st_fx->prev_use_partial_copy ) )
     979             :         {
     980          29 :             IF( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_wb_bwe_frame_pow_fx ) )
     981             :             {
     982          25 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_wb_bwe_frame_pow_fx, 22, curr_frame_pow, 22, &exp );
     983          25 :                 scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
     984             : 
     985          25 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
     986          25 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
     987          25 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
     988          25 :                 temp = round_fx( L_shl( L_tmp, exp ) ); /* Q15 */
     989             :             }
     990             :             ELSE
     991             :             {
     992           4 :                 scale = temp = 32767;
     993           4 :                 move16(); /* Q15 */
     994           4 :                 move16();
     995             :             }
     996             : 
     997         261 :             FOR( j = 0; j < 8; j++ )
     998             :             {
     999         232 :                 GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
    1000         232 :                 GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
    1001        2552 :                 FOR( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
    1002             :                 {
    1003        2320 :                     shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )] = mult_r( shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )], scale );
    1004        2320 :                     move16();
    1005             :                 }
    1006         232 :                 IF( temp > 0 )
    1007             :                 {
    1008         232 :                     IF( LT_16( scale, temp ) )
    1009             :                     {
    1010         200 :                         scale = div_s( scale, temp );
    1011             :                     }
    1012             :                     ELSE
    1013             :                     {
    1014          32 :                         scale = 32767;
    1015          32 :                         move16();
    1016             :                     }
    1017             :                 }
    1018             :                 ELSE
    1019             :                 {
    1020           0 :                     scale = 0;
    1021           0 :                     move16();
    1022             :                 }
    1023             :             }
    1024             :         }
    1025             : 
    1026        6870 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = curr_frame_pow;
    1027        6870 :         move32();
    1028        6870 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    1029        6870 :         move16();
    1030             : 
    1031             :         /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
    1032        6870 :         max = 0;
    1033        6870 :         move16();
    1034      590820 :         FOR( cnt = 0; cnt < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; cnt++ )
    1035             :         {
    1036      583950 :             if ( GT_16( abs_s( shaped_wb_excitation[cnt] ), max ) )
    1037             :             {
    1038       44544 :                 max = abs_s( shaped_wb_excitation[cnt] );
    1039             :             }
    1040             :         }
    1041        6870 :         Qx = norm_s( max );
    1042        6870 :         if ( max == 0 )
    1043             :         {
    1044           0 :             Qx = 15;
    1045           0 :             move16();
    1046             :         }
    1047             : 
    1048        6870 :         Qx = sub( Qx, 1 ); /* 1 bit space for saturation */
    1049             : 
    1050        6870 :         max = 0;
    1051        6870 :         move16();
    1052       48090 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1053             :         {
    1054       41220 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] ), max ) )
    1055       14913 :                 max = abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] );
    1056             :         }
    1057             : 
    1058       48090 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1059             :         {
    1060       41220 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] ), max ) )
    1061        4046 :                 max = abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] );
    1062             :         }
    1063             : 
    1064        6870 :         IF( EQ_32( st_fx->output_Fs, 32000 ) )
    1065             :         {
    1066        3626 :             FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1067             :             {
    1068        3108 :                 max = s_max( max, abs_s( hBWE_TD->state_32and48k_WB_upsample_fx[i] ) );
    1069             :             }
    1070             :         }
    1071        6870 :         IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1072             :         {
    1073       46130 :             FOR( i = 0; i < INTERP_3_1_MEM_LEN; i++ )
    1074             :             {
    1075       42835 :                 max = s_max( max, abs_s( hBWE_TD->mem_resamp_HB_fx[i] ) );
    1076             :             }
    1077             :         }
    1078        6870 :         n_mem = 15;
    1079        6870 :         move16();
    1080        6870 :         if ( max != 0 )
    1081             :         {
    1082        6446 :             n_mem = norm_s( max );
    1083             :         }
    1084        6870 :         n_mem = s_max( n_mem, 0 );
    1085             : 
    1086        6870 :         if ( GT_16( sub( Qx, hBWE_TD->prev_Qx ), n_mem ) )
    1087             :         {
    1088           1 :             Qx = add( hBWE_TD->prev_Qx, n_mem );
    1089             :         }
    1090             : 
    1091      590820 :         FOR( i = 0; i < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; i++ )
    1092             :         {
    1093      583950 :             shaped_wb_excitation[i] = shl( shaped_wb_excitation[i], Qx );
    1094      583950 :             move16();
    1095             :         }
    1096             : 
    1097       48090 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1098             :         {
    1099       41220 :             hBWE_TD->state_lsyn_filt_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1100       41220 :             move16();
    1101             :         }
    1102             : 
    1103       48090 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1104             :         {
    1105       41220 :             hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1106       41220 :             move16();
    1107             :         }
    1108             : 
    1109        6870 :         GenWBSynth_fx( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx );
    1110             : 
    1111        6870 :         Copy( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
    1112             : 
    1113     2205270 :         FOR( i = 0; i < L_FRAME16k; i++ )
    1114             :         {
    1115     2198400 :             synth[i] = mult_r( error[i], 21299 /*0.65f in Q15*/ );
    1116     2198400 :             move16();
    1117             :         }
    1118             : 
    1119        6870 :         IF( st_fx->hBWE_FD != NULL )
    1120             :         {
    1121        6870 :             max = 0;
    1122        6870 :             move16();
    1123     2205270 :             FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1124             :             {
    1125     2198400 :                 max = s_max( max, abs_s( synth[cnt] ) );
    1126             :             }
    1127             : 
    1128        6870 :             IF( max == 0 )
    1129             :             {
    1130         158 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = 0;
    1131         158 :                 move16();
    1132             :             }
    1133             :             ELSE
    1134             :             {
    1135        6712 :                 n = norm_s( max );
    1136     2154552 :                 FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1137             :                 {
    1138     2147840 :                     synth_frac[cnt] = shl( synth[cnt], n ); /*Q14*/
    1139     2147840 :                     move16();
    1140             :                 }
    1141        6712 :                 n = sub( sub( 14, n ), Qx );
    1142             : 
    1143        6712 :                 Lacc = 0;
    1144        6712 :                 move32();
    1145     2154552 :                 FOR( i = 0; i < L_FRAME16k; i++ )
    1146             :                 {
    1147     2147840 :                     L_tmp = L_mult( synth_frac[i], synth_frac[i] ); /* Q29 */
    1148     2147840 :                     Lacc = L_add( Lacc, L_shr( L_tmp, 7 ) );        /* Q22 */
    1149             :                 }
    1150             : 
    1151        6712 :                 L_tmp = Mult_32_16( Lacc, 102 ); /* Q22 */
    1152        6712 :                 exp = norm_l( L_tmp );
    1153        6712 :                 tmp = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow );
    1154        6712 :                 exp = sub( add( exp, 22 ), 30 );
    1155        6712 :                 tmp = div_s( 16384, tmp );
    1156        6712 :                 L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                                                       /* Q(31-exp) */
    1157        6712 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = round_fx_sat( L_shl_sat( L_tmp, add( exp, sub( n, 12 ) ) ) ); /*  Q3 */
    1158        6712 :                 move16();
    1159             :             }
    1160             :         }
    1161             : 
    1162        6870 :         IF( EQ_32( st_fx->output_Fs, 32000 ) ) /* 32kHz sampling rate, but only WB output - interpolate */
    1163             :         {
    1164         518 :             Scale_sig( hBWE_TD->state_32and48k_WB_upsample_fx, 2 * ALLPASSSECTIONS_STEEP, sub( Qx, hBWE_TD->prev_Qx ) );
    1165         518 :             Interpolate_allpass_steep_fx( synth, hBWE_TD->state_32and48k_WB_upsample_fx, L_FRAME16k, upsampled_synth );
    1166         518 :             Copy( upsampled_synth, synth, L_FRAME32k );
    1167             :         }
    1168        6352 :         ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1169             :         {
    1170        3295 :             Scale_sig( hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( Qx, hBWE_TD->prev_Qx ) );
    1171        3295 :             ivas_interpolate_3_over_1_allpass_fx( synth, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
    1172        3295 :             Copy( upsampled_synth, synth, L_FRAME48k );
    1173             :         }
    1174             :     }
    1175             :     ELSE
    1176             :     {
    1177          80 :         f = 5461;
    1178          80 :         move16(); /* Q15 */
    1179          80 :         inc = 5461;
    1180          80 :         move16(); /* Q15 */
    1181         560 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1182             :         {
    1183         480 :             lsf_wb[i] = f;
    1184         480 :             move16(); /*Q15*/
    1185         480 :             f = add_sat( f, inc );
    1186         480 :             move16();
    1187             :         }
    1188          80 :         GainFrame = 0; /* Q18 */
    1189          80 :         move32();
    1190          80 :         Qx = 0;
    1191          80 :         move16();
    1192          80 :         Q_bwe_exc = 31;
    1193          80 :         move16();
    1194          80 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
    1195          80 :         move32();
    1196          80 :         hBWE_TD->prev_frame_pow_exp = 0;
    1197          80 :         move16();
    1198             :     }
    1199             : 
    1200             :     /* Update previous frame parameters for FEC */
    1201        6950 :     IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1202             :     {
    1203        2435 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_LBR_WB );
    1204             :     }
    1205             :     ELSE
    1206             :     {
    1207        4515 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_WB );
    1208             :     }
    1209        6950 :     hBWE_TD->GainFrame_prevfrm_fx = GainFrame; /* Q18 */
    1210        6950 :     move32();
    1211             : 
    1212        6950 :     if ( !st_fx->bfi )
    1213             :     {
    1214        6919 :         hBWE_TD->GainAttn_fx = 32767;
    1215        6919 :         move16();
    1216             :     }
    1217             : 
    1218        6950 :     *Q_synth = Qx;
    1219        6950 :     move16();
    1220             : 
    1221        6950 :     st_fx->prev_Q_bwe_exc = Q_bwe_exc;
    1222        6950 :     move16();
    1223        6950 :     hBWE_TD->prev_Qx = Qx;
    1224        6950 :     move16();
    1225             : 
    1226        6950 :     return;
    1227             : }
    1228             : 
    1229             : 
    1230           0 : void wb_tbe_dec_fx(
    1231             :     Decoder_State *st_fx,     /* i/o: decoder state structure         */
    1232             :     const Word16 coder_type,  /* i  : coding type                     */
    1233             :     Word32 *bwe_exc_extended, /* i  : bandwidth extended exciatation 2*Q_exc*/
    1234             :     const Word16 Q_exc,
    1235             :     const Word16 voice_factors[], /* i  : voicing factors                 */
    1236             :     Word16 *synth,                /* o  : WB synthesis/final synthesis    */
    1237             :     Word16 *Q_synth )
    1238             : {
    1239             :     Word16 i;
    1240             :     Word16 shaped_wb_excitation[( L_FRAME16k + L_SHB_LAHEAD ) / 4];
    1241             :     Word16 shaped_wb_excitation_frac[L_FRAME16k / 4];
    1242             :     Word16 bwe_exc_extended_16[L_FRAME32k + 40];
    1243             :     Word16 exc4kWhtnd[L_FRAME16k / 4];
    1244             :     Word16 lsf_wb[LPC_SHB_ORDER_WB], lpc_wb[LPC_SHB_ORDER_WB + 1], GainShape[NUM_SHB_SUBFR];
    1245             :     Word32 GainFrame;
    1246             :     Word16 error[L_FRAME16k];
    1247             :     Word16 synth_frac[L_FRAME16k];
    1248             :     Word16 upsampled_synth[L_FRAME48k];
    1249             :     Word32 prev_pow, curr_pow, curr_frame_pow;
    1250             :     Word16 curr_frame_pow_exp;
    1251             :     Word16 temp, scale, n;
    1252             :     Word16 j;
    1253             : 
    1254             :     Word16 Q_bwe_exc, Q_bwe_exc_ext, Qx;
    1255             :     Word16 n_mem, cnt;
    1256           0 :     Word16 max = 0;
    1257           0 :     move16();
    1258           0 :     Word32 L_tmp, Lacc, Lscale, Lmax = 0;
    1259           0 :     move32();
    1260             :     Word16 tmp, exp, sc;
    1261             :     Word16 vf_modified[NB_SUBFR16k];
    1262           0 :     Word16 uv_flag = 0;
    1263           0 :     move16();
    1264           0 :     Word16 dummy = 0;
    1265           0 :     move16();
    1266             :     Word32 dummy2[HILBERT_MEM_SIZE];
    1267             :     Word16 f, inc;
    1268             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1269           0 :     Flag Overflow = 0;
    1270           0 :     move32();
    1271             : #endif
    1272             :     TD_BWE_DEC_HANDLE hBWE_TD;
    1273           0 :     hBWE_TD = st_fx->hBWE_TD;
    1274           0 :     set32_fx( &dummy2[0], 0, HILBERT_MEM_SIZE );
    1275           0 :     IF( st_fx->bws_cnt == 0 )
    1276             :     {
    1277             :         /* Initialization */
    1278           0 :         set16_fx( GainShape, 11469 /*0.35f Q15*/, NUM_SHB_SUBFR );
    1279           0 :         GainFrame = 1;
    1280           0 :         move32();
    1281             : 
    1282           0 :         IF( !st_fx->bfi )
    1283             :         {
    1284           0 :             IF( EQ_16( st_fx->use_partial_copy, 1 ) )
    1285             :             {
    1286           0 :                 IF( NE_16( st_fx->last_extl, WB_TBE ) )
    1287             :                 {
    1288           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
    1289           0 :                     move32();
    1290           0 :                     hBWE_TD->lsp_prevfrm_fx[0] = 3277 /*0.1f Q15*/;
    1291           0 :                     move16();
    1292           0 :                     FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB; i++ )
    1293             :                     {
    1294           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = add( hBWE_TD->lsp_prevfrm_fx[i - i], 3277 /*0.1f Q15*/ ); /*Q15*/
    1295           0 :                         move16();
    1296             :                     }
    1297             :                 }
    1298           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
    1299           0 :                 set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    1300             : 
    1301           0 :                 IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
    1302             :                 {
    1303             :                     /* Frame gain */
    1304           0 :                     st_fx->rf_indx_tbeGainFr = s_and( st_fx->rf_indx_tbeGainFr, 0xF ); /* only four LSBs are valid */
    1305           0 :                     move16();
    1306           0 :                     Copy32( SHBCB_FrameGain16_fx + st_fx->rf_indx_tbeGainFr, &GainFrame, 1 );
    1307           0 :                     test();
    1308           0 :                     test();
    1309           0 :                     test();
    1310           0 :                     test();
    1311           0 :                     test();
    1312           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && EQ_16( st_fx->last_extl, WB_TBE ) )
    1313             :                     {
    1314             :                         /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
    1315           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6553 ) ); /*Q18*/
    1316             :                     }
    1317             :                 }
    1318             :                 ELSE
    1319             :                 {
    1320           0 :                     temp = 0;
    1321           0 :                     move16();
    1322             :                     /* Frame gain */
    1323           0 :                     SWITCH( st_fx->rf_indx_tbeGainFr )
    1324             :                     {
    1325           0 :                         case 0:
    1326           0 :                             GainFrame = 131072; /* 0.5f in Q18 */
    1327           0 :                             move32();
    1328           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
    1329             :                             {
    1330           0 :                                 temp = 26214 /*0.8 Q15*/;
    1331           0 :                                 move16();
    1332             :                             }
    1333           0 :                             BREAK;
    1334           0 :                         case 1:
    1335           0 :                             GainFrame = 524288; /* 2.0f in Q18 */
    1336           0 :                             move32();
    1337           0 :                             test();
    1338           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
    1339             :                             {
    1340           0 :                                 temp = 26214 /*0.8 Q15*/;
    1341           0 :                                 move16();
    1342             :                             }
    1343           0 :                             BREAK;
    1344           0 :                         case 2:
    1345           0 :                             GainFrame = 1048576; /* 4.0f in Q18 */
    1346           0 :                             move32();
    1347           0 :                             test();
    1348           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
    1349             :                             {
    1350           0 :                                 temp = 26214 /*0.8 Q15*/;
    1351           0 :                                 move16();
    1352             :                             }
    1353           0 :                             BREAK;
    1354           0 :                         case 3:
    1355           0 :                             GainFrame = 2097152; /* 8.0f in Q18 */
    1356           0 :                             move32();
    1357           0 :                             test();
    1358           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16 Q18*/ ) )
    1359             :                             {
    1360           0 :                                 temp = 26214 /*0.8 Q15*/;
    1361           0 :                                 move16();
    1362             :                             }
    1363           0 :                             BREAK;
    1364           0 :                         default:
    1365           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
    1366             :                     }
    1367           0 :                     IF( EQ_16( st_fx->last_extl, WB_TBE ) )
    1368             :                     {
    1369             : 
    1370           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
    1371             :                     }
    1372           0 :                     test();
    1373           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
    1374             :                     {
    1375           0 :                         test();
    1376           0 :                         test();
    1377           0 :                         test();
    1378           0 :                         test();
    1379           0 :                         IF( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && LT_16( st_fx->prev_tilt_code_dec_fx, 1497 ) && GT_16( st_fx->prev_tilt_code_dec_fx, 200 ) )
    1380             :                         {
    1381           0 :                             GainFrame = Mult_32_16( GainFrame, 9830 ); /*Q18*/
    1382             :                         }
    1383             :                     }
    1384             :                 }
    1385             :             }
    1386             :             ELSE
    1387             :             {
    1388             :                 /* de-quantization */
    1389           0 :                 dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_wb, GainShape, &GainFrame, &uv_flag, 0, 0, 0 );
    1390             :             }
    1391             :         }
    1392             :         ELSE
    1393             :         {
    1394           0 :             IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1395             :             {
    1396           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
    1397             :             }
    1398             :             ELSE
    1399             :             {
    1400           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_wb, LPC_SHB_ORDER_WB ); /*Q15*/
    1401             :             }
    1402           0 :             set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    1403             : 
    1404           0 :             hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27853 );
    1405           0 :             move16();
    1406             : 
    1407           0 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    1408             :             {
    1409           0 :                 GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, hBWE_TD->GainAttn_fx ); /*Q18*/
    1410             :             }
    1411             :             ELSE
    1412             :             {
    1413           0 :                 GainFrame = hBWE_TD->GainFrame_prevfrm_fx; /*Q18*/
    1414           0 :                 move32();
    1415             :             }
    1416             :         }
    1417             : 
    1418           0 :         IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1419             :         {
    1420             :             /* convert LSPs back into LP coeffs */
    1421           0 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_LBR_WB );
    1422           0 :             set16_fx( lpc_wb + LPC_SHB_ORDER_LBR_WB + 1, 0, LPC_SHB_ORDER_WB - LPC_SHB_ORDER_LBR_WB );
    1423           0 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1424             :             {
    1425           0 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
    1426           0 :                 move16();
    1427             :             }
    1428           0 :             FOR( i = 1; i < LPC_SHB_ORDER_LBR_WB + 1; i++ )
    1429             :             {
    1430           0 :                 lpc_wb[i] = negate( lpc_wb[i] );
    1431           0 :                 move16();
    1432             :             }
    1433           0 :             lpc_wb[0] = 4096;
    1434           0 :             move16();
    1435             :         }
    1436             :         ELSE
    1437             :         {
    1438             :             /* convert LSPs back into LP coeffs */
    1439           0 :             lsp2lpc_fx( lpc_wb + 1, lsf_wb, hBWE_TD->prev_lpc_wb_fx, LPC_SHB_ORDER_WB );
    1440           0 :             FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1441             :             {
    1442           0 :                 hBWE_TD->prev_lpc_wb_fx[i] = lpc_wb[i + 1];
    1443           0 :                 move16();
    1444             :             }
    1445           0 :             FOR( i = 1; i < LPC_SHB_ORDER_WB + 1; i++ )
    1446             :             {
    1447           0 :                 lpc_wb[i] = negate( lpc_wb[i] );
    1448           0 :                 move16();
    1449             :             }
    1450           0 :             lpc_wb[0] = 4096;
    1451           0 :             move16();
    1452             :         }
    1453             : 
    1454           0 :         Copy( voice_factors, vf_modified, NB_SUBFR16k );
    1455           0 :         IF( EQ_16( coder_type, VOICED ) )
    1456             :         {
    1457           0 :             FOR( i = 1; i < NB_SUBFR; i++ )
    1458             :             {
    1459           0 :                 vf_modified[i] = add( mult_r( 26214, voice_factors[i] ), mult_r( 6554, voice_factors[i - 1] ) ); /* Q15 */
    1460           0 :                 move16();
    1461             :             }
    1462           0 :             IF( NE_16( st_fx->L_frame, L_FRAME ) )
    1463             :             {
    1464           0 :                 vf_modified[4] = add( mult_r( 26214, voice_factors[4] ), mult_r( 6554, voice_factors[3] ) ); /* Q15 */
    1465           0 :                 move16();
    1466             :             }
    1467             :         }
    1468             : 
    1469             :         /* From low band excitation, generate highband excitation */
    1470           0 :         Lmax = 0;
    1471           0 :         move32();
    1472           0 :         FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    1473             :         {
    1474           0 :             Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
    1475             :         }
    1476           0 :         IF( Lmax == 0 )
    1477             :         {
    1478           0 :             Q_bwe_exc = 31;
    1479           0 :             move16();
    1480             :         }
    1481             :         ELSE
    1482             :         {
    1483           0 :             Q_bwe_exc = norm_l( Lmax );
    1484             :         }
    1485           0 :         Q_bwe_exc = sub( Q_bwe_exc, 1 );
    1486           0 :         Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
    1487             : 
    1488           0 :         find_max_mem_wb( st_fx, &n_mem );
    1489             : 
    1490           0 :         if ( GT_16( sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ), n_mem ) )
    1491             :         {
    1492           0 :             Q_bwe_exc = add( st_fx->prev_Q_bwe_exc, n_mem );
    1493             :         }
    1494             : 
    1495           0 :         test();
    1496           0 :         if ( uv_flag && GT_16( Q_bwe_exc, 20 ) )
    1497             :         {
    1498           0 :             Q_bwe_exc = 20;
    1499           0 :             move16(); /* restrict this to 21 due to the Q factor requireemnt of the random number generator (keep 1 bit headroom) */
    1500             :         }
    1501             : 
    1502           0 :         prev_pow = 0;
    1503           0 :         move32();
    1504           0 :         IF( st_fx->element_mode > EVS_MONO )
    1505             :         {
    1506           0 :             tmp = sub( shl( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), 31 + 16 );
    1507           0 :             prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
    1508             :         }
    1509           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1510             :         {
    1511           0 :             prev_pow = L_mac0_sat( prev_pow, hBWE_TD->state_syn_shbexc_fx[i], hBWE_TD->state_syn_shbexc_fx[i] ); /*Q(2*(st_fx->prev_Q_bwe_exc-16))*/
    1512             :         }
    1513             : 
    1514           0 :         rescale_genWB_mem( st_fx, sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc ) );
    1515             : 
    1516           0 :         Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
    1517           0 :         sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
    1518           0 :         FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    1519             :         {
    1520           0 :             bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
    1521             :         }
    1522           0 :         Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
    1523             : 
    1524           0 :         Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 );
    1525             : 
    1526           0 :         Q_bwe_exc_ext = sub( Q_bwe_exc, 16 );
    1527             : 
    1528           0 :         GenShapedWBExcitation_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx,
    1529           0 :                                   hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx,
    1530           0 :                                   hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, hBWE_TD->state_lpc_syn_fx, coder_type,
    1531           0 :                                   bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, vf_modified, uv_flag, st_fx->igf );
    1532             : 
    1533           0 :         curr_pow = 0;
    1534           0 :         move32();
    1535           0 :         IF( st_fx->element_mode > EVS_MONO )
    1536             :         {
    1537           0 :             tmp = sub( shl( Q_bwe_exc_ext, 1 ), 31 + 16 );
    1538           0 :             curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /*Q(2*(Q_bwe_exc_ext))*/
    1539             :         }
    1540           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4; i++ )
    1541             :         {
    1542           0 :             curr_pow = L_mac0_sat( curr_pow, shaped_wb_excitation[i + L_SHB_LAHEAD / 4], shaped_wb_excitation[i + L_SHB_LAHEAD / 4] ); /* Q(2*Q_bwe_exc_ext) */
    1543             :         }
    1544             : 
    1545           0 :         if ( GT_16( voice_factors[0], 24576 ) )
    1546             :         {
    1547           0 :             curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc_ext) */
    1548             :         }
    1549             : 
    1550           0 :         Lscale = root_a_over_b_fx( curr_pow, shl_r( Q_bwe_exc_ext, 1 ), prev_pow,
    1551           0 :                                    shl_r( sub( st_fx->prev_Q_bwe_exc, 16 ), 1 ), &exp );
    1552             : 
    1553           0 :         FOR( i = 0; i < L_SHB_LAHEAD / 4 - 1; i++ )
    1554             :         {
    1555           0 :             L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[i] );             /* Q(16-exp+Q_bwe_exc_ext) */
    1556           0 :             shaped_wb_excitation[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1557           0 :             move16();
    1558             :         }
    1559           0 :         Lscale = root_a_fx( Lscale, sub( 31, exp ), &exp );
    1560           0 :         L_tmp = Mult_32_16( Lscale, shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] );     /* Q(16-exp+Q_bwe_exc_ext) */
    1561           0 :         shaped_wb_excitation[L_SHB_LAHEAD / 4 - 1] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc_ext */
    1562             : 
    1563             :         /* Update SHB excitation */
    1564           0 :         Copy( shaped_wb_excitation + L_FRAME16k / 4, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD / 4 );
    1565             : 
    1566             : 
    1567             :         /* Adjust the subframe and frame gain of the synthesized shb signal */
    1568             :         /* Scale the shaped excitation */
    1569           0 :         ScaleShapedWB_fx( SHB_OVERLAP_LEN / 2, shaped_wb_excitation, hBWE_TD->syn_overlap_fx, GainShape, GainFrame,
    1570             :                           window_wb_fx, subwin_wb_fx,
    1571           0 :                           Q_bwe_exc_ext, st_fx->L_frame, 0, &dummy, dummy, dummy2 );
    1572             : 
    1573           0 :         max = 0;
    1574           0 :         move16();
    1575           0 :         FOR( i = 0; i < L_FRAME16k / 4; i++ )
    1576             :         {
    1577           0 :             max = s_max( max, shaped_wb_excitation[i] ); /*Q0*/
    1578             :         }
    1579             : 
    1580           0 :         IF( max == 0 )
    1581             :         {
    1582           0 :             curr_frame_pow = 1;
    1583           0 :             move32();
    1584           0 :             n = 0;
    1585           0 :             move16();
    1586             :         }
    1587             :         ELSE
    1588             :         {
    1589           0 :             n = norm_s( max );
    1590           0 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
    1591             :             {
    1592           0 :                 shaped_wb_excitation_frac[i] = shl_o( shaped_wb_excitation[i], n, &Overflow ); /*Q14*/
    1593           0 :                 move16();
    1594             :             }
    1595           0 :             n = sub( 14, n );
    1596           0 :             curr_frame_pow = 1;
    1597           0 :             move32();
    1598           0 :             FOR( i = 0; i < L_FRAME16k / 4; i++ )
    1599             :             {
    1600           0 :                 L_tmp = L_mult_o( shaped_wb_excitation_frac[i], shaped_wb_excitation_frac[i], &Overflow ); /*Q29*/
    1601           0 :                 curr_frame_pow = L_add_o( curr_frame_pow, L_shr( L_tmp, 7 ), &Overflow );                  /*Q22*/
    1602             :             }
    1603             :         }
    1604           0 :         curr_frame_pow_exp = add( n, n );
    1605             : 
    1606           0 :         IF( GT_16( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) )
    1607             :         {
    1608           0 :             curr_frame_pow = L_shr( curr_frame_pow, sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp ) );
    1609           0 :             curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
    1610           0 :             move16();
    1611             :         }
    1612             :         ELSE
    1613             :         {
    1614           0 :             hBWE_TD->prev_wb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_wb_bwe_frame_pow_fx, sub( curr_frame_pow_exp, hBWE_TD->prev_frame_pow_exp ) );
    1615           0 :             move32();
    1616             :         }
    1617             : 
    1618           0 :         test();
    1619           0 :         IF( !st_fx->bfi && st_fx->prev_bfi )
    1620             :         {
    1621           0 :             IF( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_wb_bwe_frame_pow_fx ) )
    1622             :             {
    1623           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_wb_bwe_frame_pow_fx, 22, curr_frame_pow, 22, &exp );
    1624           0 :                 scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    1625             : 
    1626           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    1627           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    1628           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    1629           0 :                 temp = round_fx( L_shl( L_tmp, exp ) ); /* Q15 */
    1630             :             }
    1631             :             ELSE
    1632             :             {
    1633           0 :                 scale = temp = 32767;
    1634           0 :                 move16(); /* Q15 */
    1635           0 :                 move16();
    1636             :             }
    1637             : 
    1638           0 :             FOR( j = 0; j < 8; j++ )
    1639             :             {
    1640           0 :                 GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
    1641           0 :                 GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
    1642           0 :                 FOR( i = 0; i < L_FRAME16k / ( 4 * 8 ); i++ )
    1643             :                 {
    1644           0 :                     shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )] = mult_r( shaped_wb_excitation[add( i, j * ( L_FRAME16k / ( 4 * 8 ) ) )], scale );
    1645             :                 }
    1646           0 :                 IF( temp > 0 )
    1647             :                 {
    1648           0 :                     IF( LT_16( scale, temp ) )
    1649             :                     {
    1650           0 :                         scale = div_s( scale, temp );
    1651             :                     }
    1652             :                     ELSE
    1653             :                     {
    1654           0 :                         scale = 32767;
    1655           0 :                         move16();
    1656             :                     }
    1657             :                 }
    1658             :                 ELSE
    1659             :                 {
    1660           0 :                     scale = 0;
    1661           0 :                     move16();
    1662             :                 }
    1663             :             }
    1664             :         }
    1665             : 
    1666           0 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = curr_frame_pow;
    1667           0 :         move32();
    1668           0 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    1669           0 :         move16();
    1670             : 
    1671             :         /* generate 16kHz SHB signal (6 - 8 kHz) from 2kHz signal */
    1672           0 :         max = 0;
    1673           0 :         move16();
    1674           0 :         FOR( cnt = 0; cnt < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; cnt++ )
    1675             :         {
    1676           0 :             if ( GT_16( abs_s( shaped_wb_excitation[cnt] ), max ) )
    1677             :             {
    1678           0 :                 max = abs_s( shaped_wb_excitation[cnt] );
    1679             :             }
    1680             :         }
    1681           0 :         Qx = norm_s( max );
    1682           0 :         if ( max == 0 )
    1683             :         {
    1684           0 :             Qx = 15;
    1685           0 :             move16();
    1686             :         }
    1687             : 
    1688           0 :         Qx = sub( Qx, 1 ); /* 1 bit space for saturation */
    1689             : 
    1690           0 :         max = 0;
    1691           0 :         move16();
    1692           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1693             :         {
    1694           0 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] ), max ) )
    1695             :             {
    1696           0 :                 max = abs_s( hBWE_TD->state_lsyn_filt_shb_fx[i] );
    1697             :             }
    1698             :         }
    1699             : 
    1700           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1701             :         {
    1702           0 :             if ( GT_16( abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] ), max ) )
    1703             :             {
    1704           0 :                 max = abs_s( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] );
    1705             :             }
    1706             :         }
    1707             : 
    1708           0 :         IF( EQ_32( st_fx->output_Fs, 32000 ) )
    1709             :         {
    1710           0 :             FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1711             :             {
    1712           0 :                 max = s_max( max, abs_s( hBWE_TD->state_32and48k_WB_upsample_fx[i] ) );
    1713             :             }
    1714             :         }
    1715           0 :         IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1716             :         {
    1717           0 :             FOR( i = 0; i < INTERP_3_1_MEM_LEN; i++ )
    1718             :             {
    1719           0 :                 max = s_max( max, abs_s( hBWE_TD->mem_resamp_HB_fx[i] ) );
    1720             :             }
    1721             :         }
    1722           0 :         n_mem = 15;
    1723           0 :         move16();
    1724           0 :         if ( max != 0 )
    1725             :         {
    1726           0 :             n_mem = norm_s( max );
    1727             :         }
    1728           0 :         n_mem = s_max( n_mem, 0 );
    1729             : 
    1730           0 :         if ( GT_16( sub( Qx, hBWE_TD->prev_Qx ), n_mem ) )
    1731             :         {
    1732           0 :             Qx = add( hBWE_TD->prev_Qx, n_mem );
    1733             :         }
    1734             : 
    1735           0 :         FOR( i = 0; i < ( L_FRAME16k + L_SHB_LAHEAD ) / 4; i++ )
    1736             :         {
    1737           0 :             shaped_wb_excitation[i] = shl( shaped_wb_excitation[i], Qx );
    1738           0 :             move16();
    1739             :         }
    1740             : 
    1741           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1742             :         {
    1743           0 :             hBWE_TD->state_lsyn_filt_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1744           0 :             move16();
    1745             :         }
    1746             : 
    1747           0 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    1748             :         {
    1749           0 :             hBWE_TD->state_lsyn_filt_dwn_shb_fx[i] = shl( hBWE_TD->state_lsyn_filt_dwn_shb_fx[i], sub( Qx, hBWE_TD->prev_Qx ) );
    1750           0 :             move16();
    1751             :         }
    1752             : 
    1753           0 :         GenWBSynth_fx( shaped_wb_excitation, error, hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx );
    1754             : 
    1755           0 :         Copy( error + L_FRAME16k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
    1756             : 
    1757           0 :         FOR( i = 0; i < L_FRAME16k; i++ )
    1758             :         {
    1759           0 :             synth[i] = mult_r( error[i], 21299 );
    1760           0 :             move16();
    1761             :         }
    1762             : 
    1763           0 :         IF( st_fx->hBWE_FD != NULL )
    1764             :         {
    1765           0 :             max = 0;
    1766           0 :             move16();
    1767           0 :             FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1768             :             {
    1769           0 :                 max = s_max( max, abs_s( synth[cnt] ) );
    1770             :             }
    1771             : 
    1772           0 :             IF( max == 0 )
    1773             :             {
    1774           0 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = 0;
    1775           0 :                 move16();
    1776             :             }
    1777             :             ELSE
    1778             :             {
    1779           0 :                 n = norm_s( max );
    1780           0 :                 FOR( cnt = 0; cnt < L_FRAME16k; cnt++ )
    1781             :                 {
    1782           0 :                     synth_frac[cnt] = shl( synth[cnt], n ); /*Q14*/
    1783           0 :                     move16();
    1784             :                 }
    1785           0 :                 n = sub( sub( 14, n ), Qx );
    1786             : 
    1787           0 :                 Lacc = 0;
    1788           0 :                 move32();
    1789           0 :                 FOR( i = 0; i < L_FRAME16k; i++ )
    1790             :                 {
    1791           0 :                     L_tmp = L_mult( synth_frac[i], synth_frac[i] ); /* Q29 */
    1792           0 :                     Lacc = L_add( Lacc, L_shr( L_tmp, 7 ) );        /* Q22 */
    1793             :                 }
    1794             : 
    1795           0 :                 L_tmp = Mult_32_16( Lacc, 102 ); /* Q22 */
    1796           0 :                 exp = norm_l( L_tmp );
    1797           0 :                 tmp = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow );
    1798           0 :                 exp = sub( add( exp, 22 ), 30 );
    1799           0 :                 tmp = div_s( 16384, tmp );
    1800           0 :                 L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                                                       /* Q(31-exp) */
    1801           0 :                 st_fx->hBWE_FD->last_wb_bwe_ener_fx = round_fx_sat( L_shl_sat( L_tmp, add( exp, sub( n, 12 ) ) ) ); /*  Q3 */
    1802           0 :                 move16();
    1803             :             }
    1804             :         }
    1805             : 
    1806           0 :         IF( EQ_32( st_fx->output_Fs, 32000 ) ) /* 32kHz sampling rate, but only WB output - interpolate */
    1807             :         {
    1808           0 :             Scale_sig( hBWE_TD->state_32and48k_WB_upsample_fx, 2 * ALLPASSSECTIONS_STEEP, sub( Qx, hBWE_TD->prev_Qx ) );
    1809           0 :             Interpolate_allpass_steep_fx( synth, hBWE_TD->state_32and48k_WB_upsample_fx, L_FRAME16k, upsampled_synth );
    1810           0 :             Copy( upsampled_synth, synth, L_FRAME32k );
    1811             :         }
    1812           0 :         ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) )
    1813             :         {
    1814           0 :             Scale_sig( hBWE_TD->mem_resamp_HB_fx, INTERP_3_1_MEM_LEN, sub( Qx, hBWE_TD->prev_Qx ) );
    1815           0 :             interpolate_3_over_1_allpass_fx( synth, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
    1816           0 :             Copy( upsampled_synth, synth, L_FRAME48k );
    1817             :         }
    1818             :     }
    1819             :     ELSE
    1820             :     {
    1821           0 :         f = 5461;
    1822           0 :         move16(); /* Q15 */
    1823           0 :         inc = 5461;
    1824           0 :         move16(); /* Q15 */
    1825           0 :         FOR( i = 0; i < LPC_SHB_ORDER_WB; i++ )
    1826             :         {
    1827           0 :             lsf_wb[i] = f;
    1828           0 :             move16(); /*Q15*/
    1829           0 :             f = add_sat( f, inc );
    1830           0 :             move16();
    1831             :         }
    1832           0 :         GainFrame = 0; /* Q18 */
    1833           0 :         move32();
    1834           0 :         Qx = 0;
    1835           0 :         move16();
    1836           0 :         Q_bwe_exc = 31;
    1837           0 :         move16();
    1838           0 :         hBWE_TD->prev_wb_bwe_frame_pow_fx = 4194l /*0.001f Q22*/; /* Q22 */
    1839           0 :         move32();
    1840           0 :         hBWE_TD->prev_frame_pow_exp = 0;
    1841           0 :         move16();
    1842             :     }
    1843             : 
    1844             :     /* Update previous frame parameters for FEC */
    1845           0 :     IF( EQ_32( st_fx->extl_brate, WB_TBE_0k35 ) )
    1846             :     {
    1847           0 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_LBR_WB ); /*Q15*/
    1848             :     }
    1849             :     ELSE
    1850             :     {
    1851           0 :         Copy( lsf_wb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER_WB ); /*Q15*/
    1852             :     }
    1853           0 :     hBWE_TD->GainFrame_prevfrm_fx = GainFrame; /* Q18 */
    1854           0 :     move32();
    1855             : 
    1856           0 :     IF( !st_fx->bfi )
    1857             :     {
    1858           0 :         hBWE_TD->GainAttn_fx = 32767; /*Q15*/
    1859           0 :         move16();
    1860             :     }
    1861             : 
    1862           0 :     *Q_synth = Qx;
    1863           0 :     move16();
    1864             : 
    1865           0 :     st_fx->prev_Q_bwe_exc = Q_bwe_exc;
    1866           0 :     move16();
    1867           0 :     hBWE_TD->prev_Qx = Qx;
    1868           0 :     move16();
    1869             : 
    1870           0 :     return;
    1871             : }
    1872             : 
    1873             : 
    1874             : /*======================================================================================*/
    1875             : /* FUNCTION      : void swb_tbe_dec_fx ()                                               */
    1876             : /*--------------------------------------------------------------------------------------*/
    1877             : /* PURPOSE       : SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module */
    1878             : /*--------------------------------------------------------------------------------------*/
    1879             : /* INPUT ARGUMENTS  :                                                                   */
    1880             : /*  _(Word16) coder_type         : coding type                                          */
    1881             : /*  _(Word16*) bwe_exc_extended  :bandwidth extended exciatation  Q0                    */
    1882             : /*  _(Word16[]) voice_factors    :voicing factors          Q15                          */
    1883             : /*  _(Word16*) Q_white_exc       :Q Format of White Exc                                 */
    1884             : /*  _(Word16*) Q_synth           :Q Format of Synthesis                                 */
    1885             : /*--------------------------------------------------------------------------------------*/
    1886             : /* OUTPUT ARGUMENTS :                                                                   */
    1887             : /*   _(Word16*)synth            : SHB synthesis/final synthesis          Q_white_exc    */
    1888             : /*   _(Word16*)White_exc16k     : shaped white excitation for the FB TBE  Q_synth       */
    1889             : /*--------------------------------------------------------------------------------------*/
    1890             : /* INPUT/OUTPUT ARGUMENTS :                                                             */
    1891             : /*   _ Decoder_State *st_fx:      : Decoder state structure                          */
    1892             : /*--------------------------------------------------------------------------------------*/
    1893             : /* RETURN ARGUMENTS :                                                                   */
    1894             : /*           _ None                                                                     */
    1895             : /*--------------------------------------------------------------------------------------*/
    1896             : /* CALLED FROM : RX                                                                     */
    1897             : /*======================================================================================*/
    1898        1358 : void swb_tbe_dec_fx(
    1899             :     Decoder_State *st_fx,     /* i/o: decoder state structure                 */
    1900             :     const Word16 coder_type,  /* i  : coding type                             */
    1901             :     Word32 *bwe_exc_extended, /* i  : bandwidth extended excitation  2*Q_exc  */
    1902             :     Word16 Q_exc,
    1903             :     const Word16 voice_factors[],    /* i  : voicing factors                         */
    1904             :     const Word16 old_syn_12k8_16k[], /* i  : low band synthesis                      */
    1905             :     Word16 *White_exc16k,            /* o  : shaped white excitation for the FB TBE  */
    1906             :     Word16 *Q_white_exc,
    1907             :     Word16 *synth, /* o  : SHB synthesis/final synthesis           */
    1908             :     Word16 *Q_synth,
    1909             :     Word16 *pitch_buf /* i  : pitch buffer Q6                         */
    1910             : )
    1911             : {
    1912             :     Word16 i;
    1913             :     Word16 shaped_shb_excitation[L_FRAME16k + L_SHB_LAHEAD];
    1914             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    1915             :     Word16 lsf_shb[LPC_SHB_ORDER], lpc_shb[LPC_SHB_ORDER + 1], GainShape[NUM_SHB_SUBFR];
    1916             :     Word32 GainFrame;
    1917             :     Word16 error[L_FRAME32k];
    1918             :     Word32 L_ener;
    1919             :     Word16 ener;
    1920             :     Word16 is_fractive;
    1921             :     Word32 prev_pow, curr_pow, Lscale;
    1922             :     Word16 scale;
    1923             :     Word16 exp, tmp;
    1924             :     Word16 j, cnt;
    1925             :     Word16 n_mem, n_mem2, Qx, sc;
    1926             :     Word16 n_mem3;
    1927             :     Word32 Lmax, L_tmp;
    1928             :     Word16 frac;
    1929             : 
    1930             :     Word32 L_tmp1, L_tmp2;
    1931             :     Word16 expa, expb;
    1932             :     Word16 fraca, fracb;
    1933             :     Word16 GainShape_tmp[NUM_SHB_SUBGAINS];
    1934             :     Word16 Q_bwe_exc;
    1935             :     Word16 Q_shb;
    1936             :     Word16 vf_modified[NB_SUBFR16k];
    1937             :     Word16 stemp;
    1938             : 
    1939             :     Word16 tilt_swb_fec;
    1940             :     Word16 Q_bwe_exc_fb;
    1941             : 
    1942             :     Word16 lsp_shb_1[LPC_SHB_ORDER], lsp_shb_2[LPC_SHB_ORDER], lsp_temp[LPC_SHB_ORDER];
    1943             :     Word16 lpc_shb_sf[4 * ( LPC_SHB_ORDER + 1 )];
    1944             :     const Word16 *ptr_lsp_interp_coef;
    1945             :     Word32 shb_ener_sf_32;
    1946             :     Word16 shb_res_gshape[NB_SUBFR16k];
    1947             :     Word16 mixFactors;
    1948             :     Word16 vind;
    1949             :     Word16 shb_res_dummy[L_FRAME16k];
    1950             :     Word16 shaped_shb_excitationTemp[L_FRAME16k];
    1951             :     Word32 ener_tmp[NUM_SHB_SUBGAINS];
    1952             :     Word16 pitch_fx;
    1953             :     Word16 l_subframe_fx;
    1954             :     Word16 formant_fac;
    1955             :     Word16 lsf_diff[LPC_SHB_ORDER], w[LPC_SHB_ORDER];
    1956             :     Word16 refl[M];
    1957             :     Word16 tilt_para;
    1958             :     Word16 tmp1, tmp2;
    1959             :     Word16 f_fx, inc_fx;
    1960             :     Word32 GainFrame_prevfrm_fx;
    1961             : 
    1962             :     Word16 synth_scale_fx;
    1963             :     Word16 mean_vf;
    1964             :     Word16 exp_ener, inv_ener;
    1965        1358 :     Word32 prev_ener_ratio_fx = 0; /* initialize just to avoid compiler warning */
    1966        1358 :     move32();
    1967             :     Word16 max, n, temp, shaped_shb_excitation_frac[L_FRAME16k + L_SHB_LAHEAD];
    1968             :     Word32 curr_frame_pow;
    1969             :     Word16 curr_frame_pow_exp;
    1970             :     Word32 L_prev_ener_shb;
    1971             :     TD_BWE_DEC_HANDLE hBWE_TD;
    1972        1358 :     hBWE_TD = st_fx->hBWE_TD;
    1973             : 
    1974             :     /* initializations */
    1975        1358 :     GainFrame = L_deposit_l( 0 );
    1976        1358 :     mixFactors = 0;
    1977        1358 :     move16();
    1978        1358 :     shb_ener_sf_32 = L_deposit_l( 0 );
    1979        1358 :     set16_fx( shaped_shb_excitationTemp, 0, L_FRAME16k );
    1980        1358 :     st_fx->hTdCngDec->shb_dtx_count_fx = 0;
    1981        1358 :     move16();
    1982        1358 :     is_fractive = 0;
    1983        1358 :     move16();
    1984        1358 :     set16_fx( shb_res_gshape, 1638 /*0.1f Q14*/, NB_SUBFR16k ); /* Q14 */
    1985        1358 :     Q_shb = 0;                                                  /* high band target Q factor set to zero */
    1986        1358 :     move16();
    1987        1358 :     L_tmp = calc_tilt_bwe_fx( old_syn_12k8_16k, st_fx->Q_syn2, st_fx->L_frame );
    1988        1358 :     tilt_swb_fec = round_fx_sat( L_shl_sat( L_tmp, 3 ) );
    1989             :     /* i: old_syn_12k8_16k in st_fx->Q_syn2 */
    1990             :     /* o: tilt_swb_fec in Q11 */
    1991        1358 :     test();
    1992        1358 :     if ( st_fx->bfi && st_fx->clas_dec != UNVOICED_CLAS )
    1993             :     {
    1994           0 :         tilt_swb_fec = hBWE_TD->tilt_swb_fec_fx;
    1995           0 :         move16();
    1996             :     }
    1997             : 
    1998             :     /* WB/SWB bandwidth switching */
    1999        1358 :     test();
    2000        1358 :     test();
    2001        1358 :     IF( ( GT_16( st_fx->tilt_wb_fx, 10240 ) && ( st_fx->clas_dec == UNVOICED_CLAS ) ) || GT_16( st_fx->tilt_wb_fx, 20480 ) )
    2002             :     {
    2003          57 :         test();
    2004          57 :         test();
    2005          57 :         test();
    2006          57 :         test();
    2007          57 :         test();
    2008          57 :         test();
    2009          57 :         test();
    2010          57 :         test();
    2011          57 :         IF( ( ( st_fx->prev_fractive == 0 ) &&
    2012             :               ( LT_32( st_fx->prev_enerLH_fx, L_shl( st_fx->enerLH_fx, 1 ) ) && GT_32( st_fx->prev_enerLH_fx, L_shr( st_fx->enerLH_fx, 1 ) ) && LT_32( st_fx->prev_enerLL_fx, L_shl( st_fx->enerLL_fx, 1 ) ) && GT_32( st_fx->prev_enerLL_fx, L_shr( st_fx->enerLL_fx, 1 ) ) ) ) ||
    2013             :             ( EQ_16( st_fx->prev_fractive, 1 ) &&
    2014             :               GT_32( L_shr( st_fx->prev_enerLH_fx, 2 ), Mult_32_16( st_fx->enerLH_fx, 24576 ) ) ) /* 24576 in Q13*/
    2015             :             || ( GT_32( L_shr( st_fx->enerLL_fx, 1 ), Mult_32_16( st_fx->enerLH_fx, 24576 ) ) &&  /*24576 = 1.5 in Q14*/
    2016             :                  LT_16( st_fx->tilt_wb_fx, 20480 ) )                                              /* 20480 = 10 in Q11*/
    2017             :         )
    2018             :         {
    2019          41 :             is_fractive = 0;
    2020          41 :             move16();
    2021             :         }
    2022             :         ELSE
    2023             :         {
    2024          16 :             is_fractive = 1;
    2025          16 :             move16();
    2026             :         }
    2027             :     }
    2028             : 
    2029             :     /* WB/SWB bandwidth switching */
    2030        1358 :     IF( st_fx->bws_cnt > 0 )
    2031             :     {
    2032           0 :         f_fx = 1489; /*Q15*/
    2033           0 :         move16();
    2034           0 :         inc_fx = 1489; /*Q15*/
    2035           0 :         move16();
    2036           0 :         IF( EQ_16( is_fractive, 1 ) )
    2037             :         {
    2038           0 :             Copy( lsf_tab_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    2039             :         }
    2040             :         ELSE
    2041             :         {
    2042           0 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2043             :             {
    2044           0 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
    2045           0 :                 move16();
    2046           0 :                 f_fx = add( f_fx, inc_fx );
    2047             :             }
    2048             :         }
    2049           0 :         test();
    2050           0 :         test();
    2051           0 :         test();
    2052           0 :         test();
    2053           0 :         test();
    2054           0 :         test();
    2055           0 :         test();
    2056           0 :         IF( ( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) &&
    2057             :               !( ( L_sub( L_shr( st_fx->prev_enerLH_fx, 1 ), st_fx->enerLH_fx ) < 0 ) && L_sub( st_fx->prev_enerLH_fx, ( L_shr( st_fx->enerLH_fx, 1 ) > 0 ) ) ) ) ||
    2058             :             ( sub( st_fx->last_core, ACELP_CORE ) != 0 ) || ( ( sub( st_fx->last_core, ACELP_CORE ) == 0 ) && ( L_sub( L_abs( L_sub( st_fx->last_core_brate, st_fx->core_brate ) ), 3600 ) > 0 ) ) || ( sub( s_xor( is_fractive, st_fx->prev_fractive ), 1 ) == 0 ) )
    2059             :         {
    2060           0 :             set16_fx( GainShape, 11587, NUM_SHB_SUBFR );
    2061             :         }
    2062             :         ELSE
    2063             :         {
    2064           0 :             if ( GT_16( hBWE_TD->prev_GainShape_fx, 11587 ) )
    2065             :             {
    2066           0 :                 hBWE_TD->prev_GainShape_fx = 11587;
    2067           0 :                 move16();
    2068             :             }
    2069           0 :             set16_fx( GainShape, hBWE_TD->prev_GainShape_fx, NUM_SHB_SUBFR );
    2070             :         }
    2071             : 
    2072           0 :         Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
    2073           0 :         set16_fx( shb_res_gshape, 3277 /*0.2f Q14*/, NB_SUBFR16k ); /* Q14 */
    2074             :     }
    2075             :     ELSE /* No bandwidth switching */
    2076             :     {
    2077        1358 :         test();
    2078        1358 :         IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) )
    2079             :         {
    2080          50 :             f_fx = 1489; /*Q15*/
    2081          50 :             move16();
    2082          50 :             inc_fx = 1489; /*Q15*/
    2083          50 :             move16();
    2084         550 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2085             :             {
    2086         500 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
    2087         500 :                 move16();
    2088         500 :                 f_fx = add( f_fx, inc_fx );
    2089             :             }
    2090             :         }
    2091             : 
    2092        1358 :         IF( !st_fx->bfi )
    2093             :         {
    2094        1358 :             IF( st_fx->use_partial_copy )
    2095             :             {
    2096           0 :                 IF( NE_16( st_fx->last_extl, SWB_TBE ) )
    2097             :                 {
    2098           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
    2099           0 :                     move16();
    2100           0 :                     f_fx = 1489 /*0.045454f Q15*/;
    2101           0 :                     move16();
    2102           0 :                     inc_fx = 1489 /*0.045454f Q15*/;
    2103           0 :                     move16();
    2104           0 :                     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2105             :                     {
    2106           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = f_fx;
    2107           0 :                         move16();
    2108           0 :                         f_fx = add( f_fx, inc_fx );
    2109             :                     }
    2110             :                 }
    2111           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
    2112           0 :                 set16_fx( GainShape, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR );
    2113             : 
    2114           0 :                 IF( EQ_16( st_fx->rf_frame_type, RF_NELP ) )
    2115             :                 {
    2116             :                     /* Frame gain */
    2117           0 :                     GainFrame = L_mac( SHB_GAIN_QLOW_FX, st_fx->rf_indx_tbeGainFr, SHB_GAIN_QDELTA_FX );
    2118           0 :                     move32();                                       /*Q18*/
    2119           0 :                     L_tmp = Mult_32_16( GainFrame, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    2120             : 
    2121           0 :                     frac = L_Extract_lc( L_tmp, &exp );
    2122           0 :                     L_tmp = Pow2( 30, frac );
    2123           0 :                     GainFrame = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    2124           0 :                     test();
    2125           0 :                     test();
    2126           0 :                     test();
    2127           0 :                     test();
    2128           0 :                     test();
    2129           0 :                     test();
    2130           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) && !st_fx->prev_use_partial_copy && EQ_16( st_fx->prev_coder_type, UNVOICED ) && NE_32( GainFrame, hBWE_TD->GainFrame_prevfrm_fx ) && NE_16( st_fx->next_coder_type, GENERIC ) && EQ_16( st_fx->last_extl, SWB_TBE ) )
    2131             :                     {
    2132             :                         /*GainFrame = 0.2f*GainFrame + 0.8f*st_fx->GainFrame_prevfrm_fx;*/
    2133           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6553 ) );
    2134             :                     }
    2135             :                 }
    2136             :                 ELSE
    2137             :                 {
    2138           0 :                     temp = 0;
    2139           0 :                     move16();
    2140             :                     /* Frame gain */
    2141           0 :                     SWITCH( st_fx->rf_indx_tbeGainFr )
    2142             :                     {
    2143           0 :                         case 0:
    2144           0 :                             GainFrame = 131072; /* 0.5f in Q18 */
    2145           0 :                             move32();
    2146           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
    2147             :                             {
    2148           0 :                                 temp = 26214 /*0.8 Q15*/;
    2149           0 :                                 move16();
    2150             :                             }
    2151           0 :                             BREAK;
    2152           0 :                         case 1:
    2153           0 :                             GainFrame = 524288; /* 2.0f in Q18 */
    2154           0 :                             move32();
    2155           0 :                             test();
    2156           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
    2157             :                             {
    2158           0 :                                 temp = 26214 /*0.8 Q15*/;
    2159           0 :                                 move16();
    2160             :                             }
    2161           0 :                             BREAK;
    2162           0 :                         case 2:
    2163           0 :                             GainFrame = 1048576; /* 4.0f in Q18 */
    2164           0 :                             move32();
    2165           0 :                             test();
    2166           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
    2167             :                             {
    2168           0 :                                 temp = 26214 /*0.8 Q15*/;
    2169           0 :                                 move16();
    2170             :                             }
    2171           0 :                             BREAK;
    2172           0 :                         case 3:
    2173           0 :                             GainFrame = 2097152; /* 8.0f in Q18 */
    2174           0 :                             move32();
    2175           0 :                             test();
    2176           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16Q18*/ ) )
    2177             :                             {
    2178           0 :                                 temp = 26214 /*0.8 Q15*/;
    2179           0 :                                 move16();
    2180             :                             }
    2181           0 :                             BREAK;
    2182           0 :                         default:
    2183           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
    2184             :                     }
    2185           0 :                     IF( EQ_16( st_fx->last_extl, SWB_TBE ) )
    2186             :                     {
    2187           0 :                         GainFrame = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp ), Mult_32_16( GainFrame, sub( 32767, temp ) ) );
    2188             :                     }
    2189           0 :                     test();
    2190           0 :                     IF( ( st_fx->core == ACELP_CORE ) && ( st_fx->last_core == ACELP_CORE ) )
    2191             :                     {
    2192           0 :                         test();
    2193           0 :                         test();
    2194           0 :                         test();
    2195           0 :                         test();
    2196           0 :                         if ( !st_fx->prev_use_partial_copy && EQ_16( st_fx->last_coder_type, VOICED ) && EQ_16( st_fx->rf_frame_type, RF_GENPRED ) && GT_32( GainFrame, 2097152 ) && LT_32( GainFrame, 3059606 ) )
    2197             :                         {
    2198           0 :                             GainFrame = Mult_32_16( GainFrame, 9830 );
    2199             :                         }
    2200             :                     }
    2201             :                 }
    2202             :             }
    2203             :             ELSE
    2204             :             {
    2205             : 
    2206             :                 /* de-quantization */
    2207        1358 :                 dequantizeSHBparams_fx_9_1( st_fx, st_fx->extl, st_fx->extl_brate, lsf_shb, GainShape, &GainFrame, &stemp,
    2208             :                                             &shb_ener_sf_32, shb_res_gshape, &mixFactors );
    2209        1358 :                 Q_shb = 0;
    2210        1358 :                 move16();
    2211             :                 /* o: shb_ener_sf_32 in (2*Q_shb) */
    2212             :                 /* o: shb_res_gshape in Q14 */
    2213             :                 /* o: GainShape Q15 */
    2214             :                 /* o: GainFrame Q18 */
    2215             :             }
    2216             :         }
    2217             :         ELSE /* FER concealment of TBE parameters */
    2218             :         {
    2219           0 :             Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb, LPC_SHB_ORDER );
    2220             : 
    2221             :             /* Gain shape concealment */
    2222           0 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    2223             :             {
    2224             :                 /* Gradient based GS estimation */
    2225           0 :                 gradientGainShape( st_fx, GainShape, &GainFrame );
    2226             :                 /* o: GainShape[16] in Q15 */
    2227             :                 /* o: GainFrame in Q18 */
    2228             :             }
    2229             :             ELSE
    2230             :             {
    2231           0 :                 FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    2232             :                 {
    2233           0 :                     FOR( j = 0; j < 4; j++ )
    2234             :                     {
    2235           0 :                         GainShape[add( i * 4, j )] = mult_r( st_fx->cummulative_damping, hBWE_TD->GainShape_Delay_fx[4 + i] );
    2236           0 :                         move16();
    2237             :                     }
    2238             :                 }
    2239           0 :                 IF( GT_16( tilt_swb_fec, ( 8 << 11 ) ) ) /* tilt_swb_fec in Q11 */
    2240             :                 {
    2241           0 :                     IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
    2242             :                     {
    2243           0 :                         GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 19661 /*0.6f Q15*/ );
    2244             :                     }
    2245           0 :                     ELSE IF( EQ_16( st_fx->nbLostCmpt, 2 ) )
    2246             :                     {
    2247           0 :                         GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 11469 /*0.35f Q15*/ );
    2248             :                     }
    2249             :                     ELSE
    2250             :                     {
    2251           0 :                         GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 6554 /*0.2f Q15*/ );
    2252             :                     }
    2253           0 :                     GainFrame = Mult_32_16( GainFrame, st_fx->cummulative_damping );
    2254             :                 }
    2255             :                 ELSE
    2256             :                 {
    2257           0 :                     GainFrame = hBWE_TD->GainFrame_prevfrm_fx;
    2258           0 :                     move16(); /* gain locking */
    2259             :                 }
    2260             :             }
    2261             : 
    2262             :             /* FER concealment for 24.4kbps and 32kbps */
    2263           0 :             test();
    2264           0 :             IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    2265             :             {
    2266           0 :                 IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    2267             :                 {
    2268             :                     /*scale = st->prev1_shb_ener_sf/root_a(st->prev2_shb_ener_sf * st->prev3_shb_ener_sf); */
    2269           0 :                     L_tmp = L_mult( extract_l( hBWE_TD->prev2_shb_ener_sf_fx ), extract_l( hBWE_TD->prev3_shb_ener_sf_fx ) ); /*Q1*/
    2270           0 :                     tmp = round_fx( root_a_fx( L_tmp, 1, &exp ) );                                                            /* Q = 15-exp */
    2271           0 :                     tmp1 = extract_l( hBWE_TD->prev1_shb_ener_sf_fx );                                                        /*Q0*/
    2272           0 :                     i = sub( norm_s( tmp1 ), 1 );
    2273           0 :                     tmp1 = shl( tmp1, i ); /* Qi */
    2274           0 :                     IF( tmp == 0 )
    2275             :                     {
    2276           0 :                         tmp = 32767 /*1.0f Q15*/;
    2277           0 :                         move16(); /*Q15*/
    2278             :                     }
    2279             :                     ELSE
    2280             :                     {
    2281           0 :                         scale = div_s( tmp1, tmp ); /* Q15 - Q(15-exp) + Qi  = Qexp+i */
    2282           0 :                         scale = s_max( scale, 0 );
    2283             :                         /*scale = st->prev_res_shb_gshape * min(scale, 1.0f); */
    2284           0 :                         tmp = shl_sat( scale, sub( sub( 15, exp ), i ) ); /*Q15*/
    2285             :                     }
    2286           0 :                     scale = mult_r( hBWE_TD->prev_res_shb_gshape_fx, tmp ); /* Q14 */
    2287             : 
    2288           0 :                     test();
    2289           0 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    2290             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    2291             :                     {
    2292             :                         /* shb_ener_sf_32 = 0.5f * scale * st_fx->prev1_shb_ener_sf_fx; */
    2293           0 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, scale );
    2294             : 
    2295           0 :                         if ( GT_16( st_fx->nbLostCmpt, 1 ) )
    2296             :                         {
    2297             :                             /* shb_ener_sf_32 *= 0.5f; */
    2298           0 :                             shb_ener_sf_32 = L_shr( shb_ener_sf_32, 1 );
    2299             :                         }
    2300             :                     }
    2301             :                     ELSE
    2302             :                     {
    2303             :                         /* shb_ener_sf = scale * scale * st_fx->prev1_shb_ener_sf_fx; */
    2304           0 :                         L_tmp = L_mult( scale, scale ); /* Q29 */
    2305           0 :                         shb_ener_sf_32 = L_shl( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, round_fx( L_tmp ) ), 2 );
    2306             :                     }
    2307             :                 }
    2308             :                 ELSE
    2309             :                 {
    2310           0 :                     test();
    2311           0 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    2312             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    2313             :                     {
    2314             :                         /* shb_ener_sf_32 = 0.5f * st->cummulative_damping * st_fx->prev1_shb_ener_sf_fx; */
    2315           0 :                         shb_ener_sf_32 = L_shr( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st_fx->cummulative_damping ), 1 );
    2316             :                     }
    2317             :                     ELSE
    2318             :                     {
    2319           0 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st_fx->cummulative_damping );
    2320             :                     }
    2321             :                 }
    2322             :             }
    2323             : 
    2324           0 :             shb_ener_sf_32 = L_max( shb_ener_sf_32, 1l /*1.0f Q0*/ );
    2325           0 :             mixFactors = hBWE_TD->prev_mixFactors_fx;
    2326           0 :             move16();
    2327             : 
    2328           0 :             IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    2329             :             {
    2330           0 :                 set16_fx( shb_res_gshape, 3277 /*0.2f Q14*/, 5 ); /* Q14 */
    2331             :             }
    2332             :             ELSE
    2333             :             {
    2334           0 :                 set16_fx( shb_res_gshape, 16384 /*1.0f Q14*/, 5 ); /* Q14 */
    2335             :             }
    2336             :         }
    2337             :     }
    2338             : 
    2339             :     /* get the gainshape delay */
    2340        1358 :     Copy( &hBWE_TD->GainShape_Delay_fx[4], &hBWE_TD->GainShape_Delay_fx[0], NUM_SHB_SUBFR / 4 );
    2341        1358 :     test();
    2342        1358 :     IF( ( st_fx->rf_flag != 0 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) )
    2343             :     {
    2344           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    2345             :         {
    2346           0 :             hBWE_TD->GainShape_Delay_fx[i + 4] = s_min( s_max( GainShape[i * 4], 3277 /*0.1f Q15*/ ), 16384 /*0.5f Q15*/ );
    2347           0 :             move16();
    2348             :         }
    2349             :     }
    2350             :     ELSE
    2351             :     {
    2352        6790 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    2353             :         {
    2354        5432 :             hBWE_TD->GainShape_Delay_fx[i + 4] = GainShape[i * 4];
    2355        5432 :             move16();
    2356             :         }
    2357             :     }
    2358             : 
    2359             :     /* voice factor modification to limit any spurious jumps in the middle of voiced subframes*/
    2360             :     /* mean(voice_factors[i], 4); */
    2361        1358 :     L_tmp = L_mult( voice_factors[0], 8192 );
    2362        1358 :     L_tmp = L_mac( L_tmp, voice_factors[1], 8192 );
    2363        1358 :     L_tmp = L_mac( L_tmp, voice_factors[2], 8192 );
    2364        1358 :     mean_vf = mac_r( L_tmp, voice_factors[3], 8192 );
    2365             : 
    2366        1358 :     Copy( voice_factors, vf_modified, NB_SUBFR16k );
    2367             : 
    2368        1358 :     test();
    2369        1358 :     IF( EQ_16( coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
    2370             :     {
    2371        2088 :         FOR( i = 1; i < NB_SUBFR; i++ )
    2372             :         {
    2373        1566 :             L_tmp = L_mult( voice_factors[i], 26214 /*0.8f Q15*/ );
    2374        1566 :             vf_modified[i] = mac_r( L_tmp, voice_factors[i - 1], 6554 /*0.2f Q15*/ );
    2375        1566 :             move16();
    2376             :         }
    2377         522 :         IF( NE_16( st_fx->L_frame, L_FRAME ) )
    2378             :         {
    2379         272 :             L_tmp = L_mult( voice_factors[4], 26214 /*0.8f Q15*/ );
    2380         272 :             vf_modified[4] = mac_r( L_tmp, voice_factors[3], 6554 /*0.2f Q15*/ );
    2381         272 :             move16();
    2382             :         }
    2383             :     }
    2384             : 
    2385             :     /* convert quantized LSFs to LSPs for interpolation */
    2386        1358 :     E_LPC_lsf_lsp_conversion( lsf_shb, lsp_shb_2, LPC_SHB_ORDER );
    2387             : 
    2388        1358 :     test();
    2389        1358 :     IF( EQ_16( st_fx->last_extl, SWB_TBE ) || EQ_16( st_fx->last_extl, FB_TBE ) )
    2390             :     {
    2391             :         /* SHB LSP values from prev. frame for interpolation */
    2392        1308 :         Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1, LPC_SHB_ORDER );
    2393             :     }
    2394             :     ELSE
    2395             :     {
    2396             :         /* Use current frame's LSPs; in effect no interpolation */
    2397          50 :         Copy( lsp_shb_2, lsp_shb_1, LPC_SHB_ORDER );
    2398             :     }
    2399             : 
    2400        1358 :     test();
    2401        1358 :     test();
    2402        1358 :     test();
    2403        1358 :     IF( ( st_fx->bws_cnt == 0 ) && ( st_fx->bws_cnt1 == 0 ) && ( st_fx->prev_use_partial_copy == 0 ) && ( st_fx->use_partial_copy == 0 ) )
    2404             :     {
    2405        1358 :         lsf_diff[0] = 16384;
    2406        1358 :         move16(); /*Q15*/
    2407        1358 :         lsf_diff[LPC_SHB_ORDER - 1] = 16384;
    2408        1358 :         move16(); /*Q15*/
    2409       12222 :         FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2410             :         {
    2411       10864 :             lsf_diff[i] = sub( lsf_shb[i], lsf_shb[i - 1] );
    2412       10864 :             move16();
    2413             :         }
    2414             : 
    2415        1358 :         a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl, M );
    2416        1358 :         tmp = add( 16384, shr( refl[0], 1 ) ); /*Q14*/
    2417        1358 :         tmp1 = mult( 27425, tmp );
    2418        1358 :         tmp1 = mult( tmp1, tmp );                   /*Q10*/
    2419        1358 :         tmp2 = shr( mult( 31715, tmp ), 2 );        /*Q10*/
    2420        1358 :         tilt_para = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
    2421             : 
    2422        1358 :         test();
    2423        1358 :         IF( NE_16( st_fx->last_extl, SWB_TBE ) && NE_16( st_fx->last_extl, FB_TBE ) )
    2424             :         {
    2425         450 :             FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2426             :             {
    2427         400 :                 hBWE_TD->prev_lsf_diff_fx[i - 1] = shr( lsf_diff[i], 1 );
    2428         400 :                 move16();
    2429             :             }
    2430             :         }
    2431             : 
    2432        1358 :         IF( LE_32( st_fx->total_brate, ACELP_16k40 ) )
    2433             :         {
    2434         748 :             test();
    2435         748 :             test();
    2436         748 :             test();
    2437         748 :             test();
    2438         748 :             test();
    2439         748 :             IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( coder_type, TRANSITION ) || LT_16( tilt_para, 1024 ) ) ) &&
    2440             :                 !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( st_fx->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para, 5120 ) ) )
    2441             :             {
    2442        6552 :                 FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    2443             :                 {
    2444        5824 :                     IF( LT_16( lsf_diff[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
    2445             :                     {
    2446        2519 :                         tmp = mult( 26214, lsf_diff[i] );
    2447             : 
    2448        2519 :                         test();
    2449        2519 :                         IF( ( hBWE_TD->prev_lsf_diff_fx[i - 1] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    2450             :                         {
    2451           0 :                             st_fx->BER_detect = 1;
    2452           0 :                             move16();
    2453           0 :                             tmp = 0;
    2454           0 :                             move16();
    2455             :                         }
    2456             :                         ELSE
    2457             :                         {
    2458        2519 :                             tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    2459             :                         }
    2460             : 
    2461        2519 :                         tmp = s_max( tmp, 16384 );
    2462        2519 :                         w[i] = s_min( tmp, 32767 );
    2463        2519 :                         move16();
    2464             :                     }
    2465             :                     ELSE
    2466             :                     {
    2467        3305 :                         tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    2468             : 
    2469        3305 :                         test();
    2470        3305 :                         IF( ( lsf_diff[i] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    2471             :                         {
    2472           0 :                             st_fx->BER_detect = 1;
    2473           0 :                             move16();
    2474           0 :                             tmp = 0;
    2475           0 :                             move16();
    2476             :                         }
    2477             :                         ELSE
    2478             :                         {
    2479        3305 :                             tmp = div_s( tmp, lsf_diff[i] );
    2480             :                         }
    2481             : 
    2482        3305 :                         tmp = s_max( tmp, 16384 );
    2483        3305 :                         w[i] = s_min( tmp, 32767 );
    2484        3305 :                         move16();
    2485             :                     }
    2486             :                 }
    2487         728 :                 w[0] = w[1];
    2488         728 :                 move16();
    2489         728 :                 w[LPC_SHB_ORDER - 1] = w[LPC_SHB_ORDER - 2];
    2490         728 :                 move16();
    2491             : 
    2492        8008 :                 FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2493             :                 {
    2494        7280 :                     tmp1 = mult( lsp_shb_1[i], sub( 32767, w[i] ) );
    2495        7280 :                     tmp2 = mult( lsp_shb_2[i], w[i] );
    2496        7280 :                     lsp_temp[i] = add( tmp1, tmp2 );
    2497        7280 :                     move16();
    2498             :                 }
    2499             :             }
    2500             :             ELSE
    2501             :             {
    2502          20 :                 Copy( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
    2503             :             }
    2504             :         }
    2505             : 
    2506        1358 :         Copy( lsf_diff + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
    2507        1358 :         hBWE_TD->prev_tilt_para_fx = tilt_para;
    2508        1358 :         move16();
    2509             :     }
    2510             :     ELSE
    2511             :     {
    2512           0 :         Copy( lsp_shb_2, lsp_temp, LPC_SHB_ORDER );
    2513             :     }
    2514             : 
    2515        1358 :     test();
    2516        1358 :     IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    2517             :     {
    2518             :         /* ---------- SHB LSP interpolation ---------- */
    2519         610 :         ptr_lsp_interp_coef = interpol_frac_shb; /*Q15*/
    2520        3050 :         FOR( j = 0; j < 4; j++ )
    2521             :         {
    2522       26840 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2523             :             {
    2524             :                 /*lsp_temp_fx[i] =  lsp_shb_1_fx[i]*(*ptr_lsp_interp_coef_fx) */
    2525             :                 /*                + lsp_shb_2_fx[i]*(*(ptr_lsp_interp_coef_fx+1));*/
    2526       24400 :                 L_tmp = L_mult( lsp_shb_1[i], ( *ptr_lsp_interp_coef ) );
    2527       24400 :                 lsp_temp[i] = mac_r( L_tmp, lsp_shb_2[i], ( *( ptr_lsp_interp_coef + 1 ) ) );
    2528       24400 :                 move16();
    2529             :             }
    2530        2440 :             ptr_lsp_interp_coef += 2;
    2531             : 
    2532             :             /* convert from lsp to lsf */
    2533             :             /*old code: lsp2lsf_fx(lsp_temp, lsp_temp, LPC_SHB_ORDER, INT_FS_FX); */ /* input lsp_temp_fx in Q15*/
    2534             : 
    2535        2440 :             tmp = i_mult( j, LPC_SHB_ORDER + 1 );
    2536             :             /* convert LSPs to LP coefficients */
    2537        2440 :             E_LPC_f_lsp_a_conversion( lsp_temp, lpc_shb_sf + tmp, LPC_SHB_ORDER );
    2538             :             /* Bring the LPCs to Q12 */
    2539        2440 :             Copy_Scale_sig( lpc_shb_sf + tmp, lpc_shb_sf + tmp, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_sf[tmp] ), 2 ) );
    2540             :         }
    2541             :     }
    2542             :     /*ELSE*/
    2543             :     {
    2544             :         /* for 13.2 and 16.4kbps */
    2545        1358 :         E_LPC_f_lsp_a_conversion( lsp_temp, lpc_shb, LPC_SHB_ORDER );
    2546        1358 :         Copy_Scale_sig( lpc_shb, lpc_shb, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb[0] ), 2 ) ); /* Q12 */
    2547             :     }
    2548             : 
    2549             :     /* Save the SWB LSP values from current frame for interpolation */
    2550        1358 :     Copy( lsp_shb_2, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER );
    2551             :     /* lsp_shb_2_fx in Q15 */
    2552             : 
    2553             :     /* save the shb_ener Q18, prev_resgainshape Q14,  and mixFactor Q15 values */
    2554        1358 :     hBWE_TD->prev3_shb_ener_sf_fx = hBWE_TD->prev2_shb_ener_sf_fx;
    2555        1358 :     move32();
    2556        1358 :     hBWE_TD->prev2_shb_ener_sf_fx = hBWE_TD->prev1_shb_ener_sf_fx;
    2557        1358 :     move32();
    2558        1358 :     hBWE_TD->prev1_shb_ener_sf_fx = shb_ener_sf_32;
    2559        1358 :     move32();
    2560        1358 :     hBWE_TD->prev_res_shb_gshape_fx = shb_res_gshape[4];
    2561        1358 :     move16();
    2562        1358 :     hBWE_TD->prev_mixFactors_fx = mixFactors;
    2563        1358 :     move16();
    2564             : 
    2565             :     /* SWB CNG/DTX - update memories */
    2566        1358 :     if ( st_fx->hTdCngDec != NULL )
    2567             :     {
    2568        1358 :         Copy( st_fx->hTdCngDec->lsp_shb_prev_fx, st_fx->hTdCngDec->lsp_shb_prev_prev_fx, LPC_SHB_ORDER ); /* Q15 */
    2569        1358 :         Copy( lsf_shb, st_fx->hTdCngDec->lsp_shb_prev_fx, LPC_SHB_ORDER );                                /* Q15 */
    2570             :     }
    2571             : 
    2572             :     /* vind = (short)(mixFactors*8.0f); */
    2573        1358 :     vind = shl( mixFactors, 3 - 15 ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */
    2574             :     /* i: mixFactors in Q15 */
    2575             :     /* o: vind in Q0        */
    2576             : 
    2577             :     /* Determine formant PF strength */
    2578        1358 :     formant_fac = swb_formant_fac_fx( lpc_shb[1], &hBWE_TD->tilt_mem_fx );
    2579             :     /* o: formant_fac in Q15 */
    2580             : 
    2581             :     /* -------- start of  memory rescaling  -------- */
    2582             :     /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
    2583        1358 :     Lmax = 0;
    2584        1358 :     move32();
    2585      870478 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    2586             :     {
    2587      869120 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended[cnt] ) );
    2588             :     }
    2589        1358 :     Q_bwe_exc = norm_l( Lmax );
    2590        1358 :     if ( Lmax == 0 )
    2591             :     {
    2592           0 :         Q_bwe_exc = 31;
    2593           0 :         move16();
    2594             :     }
    2595        1358 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
    2596             : 
    2597             :     /* Account for any outliers in the memories from previous frame for rescaling to avoid saturation */
    2598        1358 :     find_max_mem_dec( st_fx, &n_mem, &n_mem2, &n_mem3 ); /* for >=24.4, use n_mem2 lpc_syn, shb_20sample, and mem_stp_swb_fx memory */
    2599             : 
    2600        1358 :     tmp = add( st_fx->prev_Q_bwe_exc, n_mem );
    2601        1358 :     if ( GT_16( Q_bwe_exc, tmp ) )
    2602             :     {
    2603          78 :         Q_bwe_exc = tmp;
    2604          78 :         move16();
    2605             :     }
    2606             : 
    2607             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    2608        1358 :     sc = sub( Q_bwe_exc, st_fx->prev_Q_bwe_exc );
    2609        1358 :     IF( sc != 0 )
    2610             :     {
    2611         699 :         rescale_genSHB_mem_dec( st_fx, sc );
    2612             :     }
    2613             : 
    2614             :     /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm  */
    2615        1358 :     Copy( hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_16, NL_BUFF_OFFSET );
    2616        1358 :     sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
    2617             : 
    2618      870478 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    2619             :     {
    2620      869120 :         bwe_exc_extended_16[cnt + NL_BUFF_OFFSET] = round_fx_sat( L_shl_sat( bwe_exc_extended[cnt], sc ) );
    2621             :     }
    2622        1358 :     Copy( bwe_exc_extended_16 + L_FRAME32k, hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET );
    2623             : 
    2624             :     /* state_syn_shbexc_fx is kept at (st_fx->prev_Q_bwe_syn) for 24.4/32kbps or is kept at Q_bwe_exc for 13.2/16.4kbps */
    2625        1358 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation, L_SHB_LAHEAD );
    2626             : 
    2627             :     /* save the previous Q factor (32-bit) of the buffer */
    2628        1358 :     st_fx->prev_Q_bwe_exc = Q_bwe_exc;
    2629        1358 :     move16();
    2630             : 
    2631        1358 :     Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
    2632             : 
    2633             :     /* -------- end of rescaling memories -------- */
    2634        1358 :     Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
    2635        1358 :     move16();
    2636             : 
    2637        1358 :     IF( GT_32( st_fx->total_brate, ACELP_32k ) )
    2638             :     {
    2639           0 :         FOR( j = 0; j < 4; j++ )
    2640             :         {
    2641           0 :             Copy( lpc_shb, &lpc_shb_sf[i_mult( j, LPC_SHB_ORDER + 1 )], LPC_SHB_ORDER + 1 );
    2642             :         }
    2643             :     }
    2644             : 
    2645             :     /* Calculate the 6 to 14 kHz (or 7.5 - 15.5 kHz) SHB excitation signal from the low band ACELP core excitation */
    2646        1358 :     GenShapedSHBExcitation_fx( shaped_shb_excitation + L_SHB_LAHEAD, lpc_shb, White_exc16k,
    2647        1358 :                                hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    2648        1358 :                                coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified, st_fx->extl,
    2649             :                                &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf, shb_ener_sf_32,
    2650        1358 :                                shb_res_gshape, shb_res_dummy, &vind, formant_fac, hBWE_TD->fb_state_lpc_syn_fx,
    2651        1358 :                                &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st_fx->total_brate, st_fx->prev_bfi );
    2652             : 
    2653        1358 :     *Q_white_exc = Q_bwe_exc_fb;
    2654        1358 :     move16();
    2655        1358 :     IF( EQ_16( st_fx->extl, FB_TBE ) )
    2656             :     {
    2657           0 :         hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
    2658           0 :         move16();
    2659             :     }
    2660             :     ELSE
    2661             :     {
    2662             :         /*Indirectly a memory reset of FB memories for next frame such that rescaling of memories would lead to 0 due to such high prev. Q value.
    2663             :         51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
    2664        1358 :         hBWE_TD->prev_Q_bwe_exc_fb = 51;
    2665        1358 :         move16();
    2666             :     }
    2667             :     /* rescale the TBE post proc memory */
    2668       14938 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    2669             :     {
    2670       13580 :         hBWE_TD->mem_stp_swb_fx[i] = shl( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn ) );
    2671       13580 :         move16();
    2672             :     }
    2673             : 
    2674        6790 :     FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    2675             :     {
    2676             :         /* TD BWE post-processing */
    2677        5432 :         PostShortTerm_fx( &shaped_shb_excitation[L_SHB_LAHEAD + i], lpc_shb, &shaped_shb_excitationTemp[i], hBWE_TD->mem_stp_swb_fx,
    2678        5432 :                           hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac );
    2679             :     }
    2680        1358 :     Copy( shaped_shb_excitationTemp, &shaped_shb_excitation[L_SHB_LAHEAD], L_FRAME16k ); /* Q_bwe_exc */
    2681             : 
    2682        1358 :     tmp = sub( shl( Q_bwe_exc, 1 ), 31 + 16 );
    2683        1358 :     prev_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    2684        1358 :     curr_pow = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    2685       42098 :     FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
    2686             :     {
    2687       40740 :         prev_pow = L_mac0_sat( prev_pow, shaped_shb_excitation[i], shaped_shb_excitation[i] );                                         /*2*Q_bwe_exc*/
    2688       40740 :         curr_pow = L_mac0_sat( curr_pow, shaped_shb_excitation[i + L_SHB_LAHEAD + 10], shaped_shb_excitation[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
    2689             :     }
    2690             : 
    2691        1358 :     if ( GT_16( voice_factors[0], 24576 /*0.75f Q15*/ ) )
    2692             :     {
    2693          54 :         curr_pow = L_shr( curr_pow, 2 ); /* Q(2*Q_bwe_exc) */
    2694             :     }
    2695             : 
    2696        1358 :     Lscale = root_a_over_b_fx( curr_pow, shl( Q_bwe_exc, 1 ), prev_pow, shl( Q_bwe_exc, 1 ), &exp );
    2697             : 
    2698       28518 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    2699             :     {
    2700       27160 :         L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] );     /* Q_bwe_exc + (31-exp) - 15 */
    2701       27160 :         shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
    2702       27160 :         move16();
    2703             :     }
    2704        1358 :     IF( exp < 0 )
    2705             :     {
    2706         137 :         Lscale = L_shl( Lscale, exp );
    2707         137 :         exp = 0;
    2708         137 :         move16();
    2709             :     }
    2710       14938 :     FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    2711             :     {
    2712       13580 :         temp = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ );        /* Q15 */
    2713       13580 :         L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp ); /* Q31-exp */
    2714       13580 :         temp = sub( 32767 /*1.0f Q15*/, temp );
    2715       13580 :         Lscale = L_add( Mult_32_16( Lscale, temp ), L_tmp1 );
    2716       13580 :         L_tmp = Mult_32_16( Lscale, shaped_shb_excitation[i] );     /* Q_bwe_exc + (31-exp) - 15 */
    2717       13580 :         shaped_shb_excitation[i] = round_fx( L_shl( L_tmp, exp ) ); /* Q_bwe_exc */
    2718       13580 :         move16();
    2719             :     }
    2720             : 
    2721             :     /* Update SHB excitation */
    2722        1358 :     Copy( shaped_shb_excitation + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
    2723             : 
    2724        1358 :     l_subframe_fx = L_FRAME16k / NUM_SHB_SUBGAINS;
    2725        1358 :     L_ener = 0;
    2726        1358 :     move32();
    2727        6790 :     FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    2728             :     {
    2729        5432 :         L_tmp = 0;
    2730        5432 :         move32();
    2731        5432 :         ener_tmp[i] = 0;
    2732        5432 :         move32();
    2733             :         {
    2734        5432 :             Word64 tmp64 = 0;
    2735        5432 :             move64();
    2736      439992 :             FOR( j = 0; j < l_subframe_fx; j++ )
    2737             :             {
    2738      434560 :                 tmp64 = W_mac0_16_16( tmp64, shaped_shb_excitation[add( i_mult( i, l_subframe_fx ), j )], shaped_shb_excitation[add( i_mult( i, l_subframe_fx ), j )] ); /* 2*Q_bwe_exc */
    2739             :             }
    2740        5432 :             L_tmp = W_sat_l( tmp64 );
    2741             :         }
    2742             : 
    2743        5432 :         L_tmp = Mult_32_16( L_tmp, 410 /*0.0125 Q15*/ ); /* 2*Q_bwe_exc: ener_tmp_fx in (2*Q_bwe_exc) */
    2744        5432 :         IF( L_tmp != 0 )
    2745             :         {
    2746        5424 :             exp = norm_l( L_tmp );
    2747        5424 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    2748        5424 :             exp = sub( exp, sub( 30, i_mult( 2, Q_bwe_exc ) ) );
    2749             : 
    2750        5424 :             tmp = div_s( 16384, tmp );
    2751        5424 :             L_tmp = L_deposit_h( tmp );
    2752        5424 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    2753        5424 :             ener_tmp[i] = L_shl_sat( L_tmp, sub( add( exp, shl( Q_bwe_exc, 1 ) ), 31 ) ); /*2 * Q_bwe_exc: Q31 -exp +exp +2 * Q_bwe_exc -31  */
    2754        5424 :             move32();
    2755        5424 :             L_ener = L_add_sat( L_ener, L_shr( ener_tmp[i], 2 ) ); /* 2*Q_bwe_exc */
    2756             :         }
    2757             :     }
    2758        1358 :     ener = s_max( 1, round_fx_sat( L_shl_sat( L_ener, sub( 19, shl( Q_bwe_exc, 1 ) ) ) ) ); /* Q3: 2*Q_bwe_exc+19-2*Q_bwe_exc-16 */
    2759             :     /* WB/SWB bandwidth switching */
    2760        1358 :     IF( st_fx->bws_cnt > 0 )
    2761             :     {
    2762           0 :         ener = mult( ener, 11587 );
    2763             :         /*bandwidth switching should be updated*/
    2764           0 :         if ( GT_16( st_fx->tilt_swb_fx, 16384 ) )
    2765             :         {
    2766           0 :             st_fx->prev_fractive = 1;
    2767           0 :             move16();
    2768             :         }
    2769             : 
    2770           0 :         IF( is_fractive == 0 )
    2771             :         {
    2772           0 :             IF( GT_16( st_fx->tilt_wb_fx, 2048 ) ) /*assuming st_fx->tilt_wb_fx in Q11*/
    2773             :             {
    2774           0 :                 st_fx->tilt_wb_fx = 2048;
    2775           0 :                 move16();
    2776             :             }
    2777           0 :             ELSE IF( LT_16( st_fx->tilt_wb_fx, 1024 ) )
    2778             :             {
    2779           0 :                 st_fx->tilt_wb_fx = 1024;
    2780           0 :                 move16();
    2781             :             }
    2782           0 :             test();
    2783           0 :             if ( EQ_16( st_fx->prev_fractive, 1 ) && GT_16( st_fx->tilt_wb_fx, 1024 ) )
    2784             :             {
    2785           0 :                 st_fx->tilt_wb_fx = 1024;
    2786           0 :                 move16();
    2787             :             }
    2788             :         }
    2789             :         ELSE
    2790             :         {
    2791           0 :             IF( GT_16( st_fx->tilt_wb_fx, 8192 ) )
    2792             :             {
    2793           0 :                 IF( st_fx->prev_fractive == 0 )
    2794             :                 {
    2795           0 :                     st_fx->tilt_wb_fx = 8192;
    2796           0 :                     move16();
    2797             :                 }
    2798             :                 ELSE
    2799             :                 {
    2800           0 :                     st_fx->tilt_wb_fx = 16384;
    2801           0 :                     move16();
    2802             :                 }
    2803             :             }
    2804             :             ELSE
    2805             :             {
    2806           0 :                 st_fx->tilt_wb_fx = shl( st_fx->tilt_wb_fx, 2 );
    2807           0 :                 move16();
    2808             :             }
    2809             :         }
    2810             : 
    2811           0 :         IF( ener != 0 )
    2812             :         {
    2813           0 :             L_tmp = L_shl( L_mult0( ener, st_fx->tilt_wb_fx ), sub( st_fx->Q_syn2, 14 ) ); /* 3+11 +st_fx->Q_syn2 -14 = st_fx->Q_syn2*/
    2814           0 :             exp_ener = norm_s( ener );
    2815           0 :             tmp = shl( ener, exp_ener );    /*Q(3+exp)*/
    2816           0 :             inv_ener = div_s( 16384, tmp ); /*Q(15+14-3-exp) = 26- exp*/
    2817             : 
    2818           0 :             test();
    2819           0 :             IF( GT_32( L_tmp, st_fx->enerLH_fx ) ) /*st_fx->Q_syn2*/
    2820             :             {
    2821           0 :                 st_fx->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st_fx->enerLH_fx, inv_ener ), sub( sub( st_fx->Q_syn2, exp_ener ), 16 ) ) ); /*Q11*/
    2822           0 :                 move16();
    2823             :                 /*st_fx->Q_syn2 -1 + 26- exp_ener -15 -(st_fx->Q_syn2 -exp_ener -16 ) -16    +1 -1 = (11) *0.5*/
    2824             :             }
    2825           0 :             ELSE IF( LT_32( L_tmp, Mult_32_16( st_fx->enerLH_fx, 1638 ) ) && EQ_16( is_fractive, 1 ) )
    2826             :             {
    2827           0 :                 st_fx->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st_fx->enerLH_fx, inv_ener ), sub( sub( st_fx->Q_syn2, exp_ener ), 15 ) ) ); /*Q11*/
    2828           0 :                 move16();
    2829             :                 /*st_fx->Q_syn2 -1 + 26- exp_ener -15 -(st_fx->Q_syn2 -exp_ener -15 ) -16 = (11) 0.25*/
    2830             :             }
    2831           0 :             L_tmp = L_mult0( st_fx->prev_ener_shb_fx, inv_ener );          /*Q(1+15+14-3-exp_ener) = 27 -exp_ener*/
    2832           0 :             GainFrame_prevfrm_fx = L_shr_sat( L_tmp, sub( 9, exp_ener ) ); /*27 -exp_ener -(9-exp_ener )= Q18*/
    2833             :         }
    2834             :         ELSE
    2835             :         {
    2836           0 :             GainFrame_prevfrm_fx = 0;
    2837           0 :             move32();
    2838             :         }
    2839             : 
    2840           0 :         IF( EQ_16( is_fractive, 1 ) )
    2841             :         {
    2842           0 :             GainFrame = L_shl( L_deposit_l( st_fx->tilt_wb_fx ), 10 );
    2843             :         }
    2844             :         ELSE
    2845             :         {
    2846           0 :             GainFrame = L_shl( L_deposit_l( st_fx->tilt_wb_fx ), 8 );
    2847             :         }
    2848             : 
    2849           0 :         test();
    2850           0 :         IF( EQ_16( s_and( is_fractive, st_fx->prev_fractive ), 1 ) && GT_32( GainFrame, GainFrame_prevfrm_fx ) )
    2851             :         {
    2852           0 :             GainFrame = L_add( Mult_32_16( GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) ); /* 18 +15 -15 = 18*/
    2853             :         }
    2854             :         ELSE
    2855             :         {
    2856           0 :             test();
    2857           0 :             test();
    2858           0 :             test();
    2859           0 :             test();
    2860           0 :             IF( ( LT_32( L_shr( st_fx->prev_enerLH_fx, 1 ), st_fx->enerLH_fx ) && GT_32( st_fx->prev_enerLH_fx, L_shr( st_fx->enerLH_fx, 1 ) ) ) && ( LT_32( L_shr( st_fx->prev_enerLL_fx, 1 ), st_fx->enerLL_fx ) && GT_32( st_fx->prev_enerLL_fx, L_shr( st_fx->enerLL_fx, 1 ) ) ) && ( s_xor( is_fractive, st_fx->prev_fractive ) == 0 ) )
    2861             :             {
    2862           0 :                 GainFrame = L_add( L_shr( GainFrame, 1 ), L_shr( GainFrame_prevfrm_fx, 1 ) );
    2863             :             }
    2864             :             ELSE
    2865             :             {
    2866           0 :                 test();
    2867           0 :                 IF( ( is_fractive == 0 ) && EQ_16( st_fx->prev_fractive, 1 ) )
    2868             :                 {
    2869           0 :                     L_tmp1 = L_shl( Mult_32_16( GainFrame, 3277 ), 13 );                                             /* 31 */
    2870           0 :                     L_tmp = L_sub( 2147483647, L_tmp1 );                                                             /* 31 */
    2871           0 :                     GainFrame = L_add( Mult_32_32( GainFrame, L_tmp ), Mult_32_32( GainFrame_prevfrm_fx, L_tmp1 ) ); /* 18 */
    2872             :                 }
    2873             :                 ELSE
    2874             :                 {
    2875           0 :                     GainFrame = L_add( L_shr( GainFrame, 1 ), L_shr( L_min( GainFrame_prevfrm_fx, GainFrame ), 1 ) ); /* 18 */
    2876             :                 }
    2877             :             }
    2878             :         }
    2879             : 
    2880           0 :         GainFrame = Mult_32_16( GainFrame, i_mult( sub( N_WS2N_FRAMES, st_fx->bws_cnt ), 819 ) ); /*Q18*/
    2881             :     }
    2882             :     ELSE
    2883             :     {
    2884        1358 :         if ( st_fx->bws_cnt1 > 0 )
    2885             :         {
    2886           0 :             GainFrame = Mult_32_16( GainFrame, i_mult( st_fx->bws_cnt1, 819 ) ); /*Q18*/
    2887             :         }
    2888        1358 :         IF( GE_16( st_fx->nbLostCmpt, 1 ) )
    2889             :         {
    2890           0 :             ener = s_max( 1, ener );
    2891           0 :             exp_ener = norm_s( ener );
    2892           0 :             tmp = shl( ener, exp_ener );                                                                        /*Q(3+exp)*/
    2893           0 :             inv_ener = div_s( 16384, tmp );                                                                     /*Q(15+14-3-exp)*/
    2894           0 :             prev_ener_ratio_fx = L_shr_sat( L_mult0( st_fx->prev_ener_shb_fx, inv_ener ), sub( 9, exp_ener ) ); /*Q: 1+26-exp-9+exp = 18 */
    2895             :         }
    2896             : 
    2897        1358 :         IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
    2898             :         {
    2899           0 :             test();
    2900           0 :             test();
    2901           0 :             test();
    2902           0 :             test();
    2903           0 :             test();
    2904           0 :             test();
    2905           0 :             test();
    2906           0 :             test();
    2907           0 :             test();
    2908           0 :             test();
    2909           0 :             IF( ( st_fx->clas_dec != UNVOICED_CLAS ) && NE_16( st_fx->clas_dec, UNVOICED_TRANSITION ) && LT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) &&
    2910             :                 ( ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) && LT_32( L_shr( st_fx->enerLL_fx, 1 ), st_fx->prev_enerLL_fx ) ) || ( GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) && LT_32( L_shr( st_fx->enerLH_fx, 1 ), st_fx->prev_enerLH_fx ) ) ) )
    2911             :             {
    2912           0 :                 IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) ) /*18*/
    2913             :                 {
    2914           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 13107 ), Mult_32_16( GainFrame, 19661 ) ); /*18*/
    2915             :                 }
    2916           0 :                 ELSE IF( GT_32( L_shr( prev_ener_ratio_fx, 1 ), GainFrame ) )
    2917             :                 {
    2918           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) );
    2919             :                 }
    2920             :                 ELSE
    2921             :                 {
    2922           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
    2923             :                 }
    2924             : 
    2925           0 :                 test();
    2926           0 :                 IF( GT_16( tilt_swb_fec, hBWE_TD->tilt_swb_fec_fx ) && ( hBWE_TD->tilt_swb_fec_fx > 0 ) )
    2927             :                 {
    2928           0 :                     exp = norm_s( hBWE_TD->tilt_swb_fec_fx );
    2929           0 :                     tmp = shl( hBWE_TD->tilt_swb_fec_fx, exp );                                   /*Q(11+exp)*/
    2930           0 :                     tmp = div_s( 16384, tmp );                                                    /*Q(15+14-11-exp)*/
    2931           0 :                     tmp = extract_h( L_shl( L_mult0( tmp, st_fx->tilt_wb_fx ), sub( exp, 1 ) ) ); /*18 -exp +11  + exp -1 -16  =12;          */
    2932           0 :                     GainFrame = L_shl( Mult_32_16( GainFrame, s_min( tmp, 20480 ) ), 3 );         /*Q18 = 18 +12 -15 +3 */
    2933             :                 }
    2934             :             }
    2935           0 :             ELSE IF( ( ( st_fx->clas_dec != UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) ) && GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) &&
    2936             :                      ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) )
    2937             :             {
    2938           0 :                 GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
    2939             :             }
    2940             :         }
    2941        1358 :         ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
    2942             :         {
    2943           0 :             test();
    2944           0 :             test();
    2945           0 :             test();
    2946           0 :             test();
    2947           0 :             test();
    2948           0 :             test();
    2949           0 :             test();
    2950           0 :             test();
    2951           0 :             IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame ) && ( ( EQ_16( st_fx->codec_mode, MODE1 ) && GT_32( st_fx->enerLL_fx, st_fx->prev_enerLL_fx ) && GT_32( st_fx->enerLH_fx, st_fx->prev_enerLH_fx ) ) || EQ_16( st_fx->codec_mode, MODE2 ) ) )
    2952             :             {
    2953           0 :                 test();
    2954           0 :                 IF( GT_16( tilt_swb_fec, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
    2955             :                 {
    2956           0 :                     GainFrame = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame, 6554 ) ), L_shl( Mult_32_16( GainFrame, 16384 ), 3 ) ); /*Q18*/
    2957             :                 }
    2958             :                 ELSE
    2959             :                 {
    2960           0 :                     GainFrame = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame, 16384 ) ), L_shl( Mult_32_16( GainFrame, 16384 ), 3 ) ); /*Q18*/
    2961             :                 }
    2962             :             }
    2963           0 :             ELSE IF( GT_32( prev_ener_ratio_fx, GainFrame ) && ( ( EQ_16( st_fx->codec_mode, MODE1 ) && GT_32( st_fx->enerLL_fx, st_fx->prev_enerLL_fx ) && GT_32( st_fx->enerLH_fx, st_fx->prev_enerLH_fx ) ) || EQ_16( st_fx->codec_mode, MODE2 ) ) )
    2964             :             {
    2965           0 :                 test();
    2966           0 :                 IF( GT_16( tilt_swb_fec, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
    2967             :                 {
    2968           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame, 16384 ) );
    2969             :                 }
    2970             :                 ELSE
    2971             :                 {
    2972           0 :                     GainFrame = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame, 26214 ) );
    2973             :                 }
    2974             :             }
    2975             :         }
    2976             :     }
    2977        1358 :     st_fx->prev_fractive = is_fractive;
    2978        1358 :     move16();
    2979             : 
    2980             :     /* Adjust the subframe and frame gain of the synthesized shb signal */
    2981        1358 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    2982             :     {
    2983             :         /* pitch = 0.25f*sum_s(pitch_buf, 4); */
    2984         748 :         L_tmp = L_mult( pitch_buf[0], 8192 );
    2985        2992 :         FOR( i = 1; i < NB_SUBFR; i++ )
    2986             :         {
    2987        2244 :             L_tmp = L_mac( L_tmp, pitch_buf[i], 8192 ); /* pitch_buf in Q6 x 0.25 in Q15 */
    2988             :         }
    2989         748 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    2990             :     }
    2991             :     ELSE
    2992             :     {
    2993             :         /* pitch_fx = 0.2f*sum_s(pitch_buf, 5); */
    2994         610 :         L_tmp = L_mult( pitch_buf[0], 6554 );
    2995        3050 :         FOR( i = 1; i < NB_SUBFR16k; i++ )
    2996             :         {
    2997        2440 :             L_tmp = L_mac( L_tmp, pitch_buf[i], 6554 ); /* pitch_buf in Q6 x 0.2 in Q15 */
    2998             :         }
    2999         610 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    3000             :     }
    3001             : 
    3002        1358 :     test();
    3003        1358 :     test();
    3004        1358 :     test();
    3005        1358 :     test();
    3006        1358 :     test();
    3007        1358 :     test();
    3008        1358 :     test();
    3009        1358 :     test();
    3010        1358 :     test();
    3011        1358 :     test();
    3012        1358 :     IF( ( ( GE_32( st_fx->total_brate, ACELP_24k40 ) && EQ_16( st_fx->prev_coder_type, coder_type ) && NE_16( coder_type, UNVOICED ) ) || ( LE_32( st_fx->total_brate, ACELP_16k40 ) && ( EQ_16( st_fx->prev_coder_type, coder_type ) || ( EQ_16( st_fx->prev_coder_type, VOICED ) && EQ_16( coder_type, GENERIC ) ) || ( EQ_16( st_fx->prev_coder_type, GENERIC ) && EQ_16( coder_type, VOICED ) ) ) ) ) && GT_16( pitch_fx, 4480 /*70 in Q6*/ ) && LT_16( st_fx->extl, FB_TBE ) )
    3013             :     {
    3014        2760 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3015             :         {
    3016        2208 :             GainShape_tmp[i] = GainShape[i * 4]; /* Q15 */
    3017        2208 :             move16();
    3018             :         }
    3019             : 
    3020        2760 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    3021             :         {
    3022             :             /* if( ener_tmp_fx[i]*GainShape_tmp_fx[i] > st_fx->prev_ener_fx*st_fx->prev_GainShape_fx ) */
    3023        2208 :             L_tmp1 = Mult_32_16( ener_tmp[i], GainShape_tmp[i] );                     /* (2*Q_bwe_exc) */
    3024        2208 :             L_tmp2 = Mult_32_16( hBWE_TD->prev_ener_fx, hBWE_TD->prev_GainShape_fx ); /* (2*st_fx->prev_ener_fx_Q) */
    3025        2208 :             tmp = sub( shl( Q_bwe_exc, 1 ), shl( hBWE_TD->prev_ener_fx_Q, 1 ) );
    3026        2208 :             L_tmp2 = L_shl_sat( L_tmp2, tmp ); /* new Q = (2*Q_bwe_exc) */
    3027        2208 :             IF( GT_32( L_tmp1, L_tmp2 ) )
    3028             :             {
    3029             :                 /*GainShape_tmp_fx[i] = 0.5f*(L_tmp2/ener_tmp_fx[i] + GainShape_tmp_fx[i]);*/
    3030             :                 /* tmp = L_tmp2/ener_tmp_fx[i]*/
    3031        1271 :                 L_tmp = L_tmp2;
    3032        1271 :                 move32();
    3033        1271 :                 if ( L_tmp2 < 0 )
    3034             :                 {
    3035           0 :                     L_tmp = L_negate( L_tmp2 );
    3036             :                 }
    3037             : 
    3038        1271 :                 expb = norm_l( L_tmp );
    3039        1271 :                 fracb = round_fx_sat( L_shl_sat( L_tmp, expb ) );
    3040        1271 :                 expb = sub( 30, expb ); /* - (2*Q_bwe_exc_ext);                    */
    3041             : 
    3042        1271 :                 expa = norm_l( ener_tmp[i] );
    3043        1271 :                 fraca = extract_h( L_shl( ener_tmp[i], expa ) );
    3044        1271 :                 expa = sub( 30, expa );
    3045             : 
    3046        1271 :                 scale = shr( sub( fraca, fracb ), 15 );
    3047        1271 :                 fracb = shl( fracb, scale );
    3048        1271 :                 expb = sub( expb, scale );
    3049             : 
    3050        1271 :                 tmp = div_s( fracb, fraca );
    3051        1271 :                 exp = sub( sub( expb, expa ), 1 );
    3052        1271 :                 tmp = shl( tmp, exp );
    3053        1271 :                 GainShape_tmp[i] = add( tmp, shr( GainShape_tmp[i], 1 ) ); /* Q15 */
    3054        1271 :                 move16();
    3055             :             }
    3056             : 
    3057        2208 :             hBWE_TD->prev_ener_fx = ener_tmp[i];
    3058        2208 :             move32();
    3059        2208 :             hBWE_TD->prev_GainShape_fx = GainShape_tmp[i];
    3060        2208 :             move16();
    3061        2208 :             hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    3062        2208 :             move16();
    3063             :         }
    3064        9384 :         FOR( i = 0; i < NUM_SHB_SUBFR; i++ )
    3065             :         {
    3066        8832 :             Word16 idx = 0;
    3067        8832 :             move16();
    3068        8832 :             IF( i != 0 )
    3069             :             {
    3070        8280 :                 idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
    3071             :             }
    3072        8832 :             GainShape[i] = GainShape_tmp[idx];
    3073        8832 :             move16();
    3074             :         }
    3075             :     }
    3076             :     ELSE
    3077             :     {
    3078         806 :         hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    3079         806 :         move16();
    3080             :     }
    3081             : 
    3082             : 
    3083             :     /* Back up the Q_bwe_exc associated with shaped_shb_excitation for the next frame*/
    3084        1358 :     hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
    3085        1358 :     move16();
    3086             : 
    3087             :     /* Scale the shaped excitation */
    3088        1358 :     ScaleShapedSHB_fx( SHB_OVERLAP_LEN,
    3089             :                        shaped_shb_excitation, /* i/o: Q_bwe_exc */
    3090        1358 :                        hBWE_TD->syn_overlap_fx,
    3091             :                        GainShape, /* Q15  */
    3092             :                        GainFrame, /* Q18  */
    3093             :                        window_shb_fx,
    3094             :                        subwin_shb_fx,
    3095        1358 :                        &Q_bwe_exc, &Qx, n_mem3, hBWE_TD->prev_Q_bwe_syn2 );
    3096             : 
    3097        1358 :     max = 0;
    3098        1358 :     move16();
    3099      435918 :     FOR( i = 0; i < L_FRAME16k; i++ )
    3100             :     {
    3101      434560 :         max = s_max( max, shaped_shb_excitation[i] ); /* Q0 */
    3102             :     }
    3103             : 
    3104        1358 :     IF( max == 0 )
    3105             :     {
    3106           0 :         curr_frame_pow = 0;
    3107           0 :         move16();
    3108           0 :         n = 0;
    3109           0 :         move16();
    3110             :     }
    3111             :     ELSE
    3112             :     {
    3113        1358 :         n = norm_s( max );
    3114        1358 :         max = 0;
    3115        1358 :         move16();
    3116      435918 :         FOR( i = 0; i < L_FRAME16k; i++ )
    3117             :         {
    3118      434560 :             shaped_shb_excitation_frac[i] = shl_sat( shaped_shb_excitation[i], n ); /*Q_bwe_exc+n*/
    3119      434560 :             move16();
    3120             :         }
    3121             : 
    3122        1358 :         curr_frame_pow = 0;
    3123        1358 :         move32();
    3124      435918 :         FOR( i = 0; i < L_FRAME16k; i++ )
    3125             :         {
    3126      434560 :             L_tmp = L_mult0( shaped_shb_excitation_frac[i], shaped_shb_excitation_frac[i] ); /*2*(Q_bwe_exc+n)*/
    3127      434560 :             curr_frame_pow = L_add( curr_frame_pow, L_shr( L_tmp, 9 ) );                     /*2*(Q_bwe_exc+n)-9*/
    3128             :         }
    3129             :     }
    3130        1358 :     curr_frame_pow_exp = sub( shl( add( Q_bwe_exc, n ), 1 ), 9 );
    3131             : 
    3132             : 
    3133        1358 :     tmp = sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp );
    3134        1358 :     IF( tmp > 0 ) /* shifting prev */
    3135             :     {
    3136           8 :         IF( GT_16( tmp, 32 ) )
    3137             :         {
    3138           0 :             hBWE_TD->prev_frame_pow_exp = add( curr_frame_pow_exp, 32 );
    3139           0 :             move16();
    3140           0 :             tmp = 32;
    3141           0 :             move16();
    3142             :         }
    3143           8 :         hBWE_TD->prev_swb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, tmp );
    3144           8 :         move32();
    3145           8 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    3146           8 :         move16();
    3147             :     }
    3148             :     ELSE /* shifting curr */
    3149             :     {
    3150        1350 :         IF( LT_16( tmp, -32 ) )
    3151             :         {
    3152           0 :             curr_frame_pow_exp = sub( hBWE_TD->prev_frame_pow_exp, 32 );
    3153           0 :             tmp = -32;
    3154           0 :             move16();
    3155             :         }
    3156        1350 :         curr_frame_pow = L_shr( curr_frame_pow, -tmp );
    3157        1350 :         curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
    3158        1350 :         move16();
    3159             :     }
    3160        1358 :     test();
    3161        1358 :     IF( !st_fx->bfi && st_fx->prev_bfi )
    3162             :     {
    3163           0 :         L_tmp = L_shr( curr_frame_pow, 4 );
    3164           0 :         L_tmp = Mult_32_16( L_tmp, 17476 );
    3165             : 
    3166           0 :         test();
    3167           0 :         test();
    3168           0 :         IF( ( GT_32( L_shr( curr_frame_pow, 1 ), hBWE_TD->prev_swb_bwe_frame_pow_fx ) ) &&
    3169             :             ( GT_32( hBWE_TD->prev_swb_bwe_frame_pow_fx, L_tmp ) ) && EQ_16( st_fx->prev_coder_type, UNVOICED ) )
    3170             :         {
    3171           0 :             L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3172           0 :             scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    3173             : 
    3174           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3175           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3176           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3177           0 :             temp = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    3178             :         }
    3179             :         ELSE
    3180             :         {
    3181           0 :             scale = temp = 32767;
    3182           0 :             move16(); /*Q15*/
    3183             :         }
    3184           0 :         FOR( j = 0; j < 8; j++ )
    3185             :         {
    3186           0 :             GainShape[2 * j] = mult_r( GainShape[2 * j], scale );
    3187           0 :             move16();
    3188           0 :             GainShape[add( 2 * j, 1 )] = mult_r( GainShape[add( 2 * j, 1 )], scale );
    3189           0 :             move16();
    3190           0 :             FOR( i = 0; i < L_FRAME16k / 8; i++ )
    3191             :             {
    3192           0 :                 shaped_shb_excitation[add( i, j * ( L_FRAME16k / 8 ) )] = mult_r( shaped_shb_excitation[add( i, j * ( L_FRAME16k / 8 ) )], scale );
    3193           0 :                 move16();
    3194             :             }
    3195             : 
    3196           0 :             IF( temp > 0 )
    3197             :             {
    3198             :                 /* scale <= temp, due to scale = sqrt( st->prev_swb_bwe_frame_pow_fx/curr_frame_pow ), temp = sqrt( scale, 1.f/8.f )
    3199             :                    and curr_frame_pow > st->prev_swb_bwe_frame_pow_fx -> scale <= 1.0, sqrt(scale, 1.f/8.f) >= scale  */
    3200           0 :                 IF( LT_16( scale, temp ) )
    3201             :                 {
    3202           0 :                     scale = div_s( scale, temp );
    3203             :                 }
    3204             :                 ELSE
    3205             :                 {
    3206           0 :                     scale = 32767;
    3207           0 :                     move16();
    3208             :                 }
    3209             :             }
    3210             :             ELSE
    3211             :             {
    3212           0 :                 scale = 0;
    3213           0 :                 move16();
    3214             :             }
    3215             :         }
    3216             :     }
    3217             : 
    3218             :     /* adjust the FEC frame energy */
    3219        1358 :     IF( st_fx->bfi )
    3220             :     {
    3221           0 :         scale = temp = 4096;
    3222           0 :         move16(); /*Q12*/
    3223             : 
    3224           0 :         IF( EQ_16( st_fx->nbLostCmpt, 1 ) )
    3225             :         {
    3226           0 :             test();
    3227           0 :             test();
    3228           0 :             test();
    3229           0 :             test();
    3230           0 :             test();
    3231           0 :             test();
    3232           0 :             test();
    3233           0 :             test();
    3234           0 :             IF( GT_32( curr_frame_pow, hBWE_TD->prev_swb_bwe_frame_pow_fx ) &&
    3235             :                 NE_16( st_fx->prev_coder_type, UNVOICED ) &&
    3236             :                 ( st_fx->last_good != UNVOICED_CLAS ) )
    3237             :             {
    3238           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp ); /*31 - exp*/
    3239           0 :                 scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );                                                                            /*Q12*/
    3240           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3241           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3242           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3243           0 :                 temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3244             :             }
    3245           0 :             ELSE IF( LT_32( curr_frame_pow, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) && EQ_16( st_fx->nbLostCmpt, 1 ) &&
    3246             :                      ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) &&
    3247             :                      ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    3248             :             {
    3249           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3250           0 :                 scale = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3251           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3252           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3253           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3254           0 :                 temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3255             :             }
    3256             :         }
    3257           0 :         ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
    3258             :         {
    3259           0 :             test();
    3260           0 :             test();
    3261           0 :             test();
    3262           0 :             test();
    3263           0 :             test();
    3264           0 :             IF( GT_32( curr_frame_pow, hBWE_TD->prev_swb_bwe_frame_pow_fx ) )
    3265             :             {
    3266           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3267           0 :                 scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3268           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3269           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3270           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3271           0 :                 temp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3272             :             }
    3273           0 :             ELSE IF( LT_32( curr_frame_pow, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) &&
    3274             :                      ( GT_32( st_fx->enerLL_fx, L_shr( st_fx->prev_enerLL_fx, 1 ) ) || GT_32( st_fx->enerLH_fx, L_shr( st_fx->prev_enerLH_fx, 1 ) ) ) &&
    3275             :                      ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    3276             :             {
    3277           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow, curr_frame_pow_exp, &exp );
    3278           0 :                 L_tmp = L_min( L_tmp, L_shl_sat( 2, sub( 31, exp ) ) ); /*31 - exp*/
    3279           0 :                 scale = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );      /*Q12*/
    3280           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3281           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3282           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    3283           0 :                 temp = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    3284             :             }
    3285             :         }
    3286           0 :         FOR( j = 0; j < 8; j++ )
    3287             :         {
    3288           0 :             GainShape[2 * j] = shl_sat( mult_r( GainShape[2 * j], scale ), 3 );
    3289           0 :             move16(); /* 15 +12 +3-15 =15*/
    3290           0 :             GainShape[add( 2 * j, 1 )] = shl_sat( mult_r( GainShape[add( 2 * j, 1 )], scale ), 3 );
    3291           0 :             move16();
    3292           0 :             FOR( i = 0; i < 40; i++ )
    3293             :             {
    3294           0 :                 shaped_shb_excitation[add( i, j * 40 )] = shl_sat( mult_r( shaped_shb_excitation[add( i, j * 40 )], scale ), 3 );
    3295           0 :                 move16(); /* Q_bwe_exc +12+3 -15  =Q_bwe_exc*/
    3296             :             }
    3297             : 
    3298           0 :             IF( temp > 0 )
    3299             :             {
    3300           0 :                 IF( LT_16( scale, temp ) )
    3301             :                 {
    3302           0 :                     scale = shr( div_s( scale, temp ), 3 );
    3303             :                 }
    3304             :                 ELSE
    3305             :                 {
    3306           0 :                     tmp1 = sub( norm_s( scale ), 1 );
    3307           0 :                     tmp2 = norm_s( temp );
    3308           0 :                     scale = div_s( shl( scale, tmp1 ), shl( temp, tmp2 ) );
    3309           0 :                     scale = shr( scale, add( sub( tmp1, tmp2 ), 3 ) );
    3310             :                 }
    3311             :             }
    3312             :             ELSE
    3313             :             {
    3314           0 :                 scale = 0;
    3315           0 :                 move16();
    3316             :             }
    3317             :         }
    3318             :     }
    3319             : 
    3320        1358 :     hBWE_TD->prev_swb_bwe_frame_pow_fx = curr_frame_pow;
    3321        1358 :     move32();
    3322        1358 :     hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    3323        1358 :     move16();
    3324             : 
    3325             :     {
    3326        1358 :         Word64 prev_ener_shb64 = 0;
    3327        1358 :         move64();
    3328      435918 :         FOR( i = 0; i < L_FRAME16k; i++ )
    3329             :         {
    3330      434560 :             prev_ener_shb64 = W_mac0_16_16( prev_ener_shb64, shaped_shb_excitation[i], shaped_shb_excitation[i] ); /* Q0 */
    3331             :         }
    3332        1358 :         L_prev_ener_shb = W_sat_l( prev_ener_shb64 );
    3333             :     }
    3334             : 
    3335             :     /* st->prev_ener_shb = sqrt(st->prev_ener_shb/L_FRAME16k) */
    3336        1358 :     L_prev_ener_shb = Mult_32_16( L_prev_ener_shb, 26214 ); /* 2*Q_bwe_exc_mod+8; 26214=(1/L_FRAME16k) in Q23 */
    3337        1358 :     st_fx->prev_ener_shb_fx = 0;
    3338        1358 :     move16();
    3339             : 
    3340        1358 :     IF( L_prev_ener_shb != 0 )
    3341             :     {
    3342        1358 :         exp = norm_l( L_prev_ener_shb );
    3343        1358 :         tmp = extract_h( L_shl( L_prev_ener_shb, exp ) );
    3344        1358 :         exp = sub( exp, sub( 30, ( add( i_mult( 2, Q_bwe_exc ), 8 ) ) ) );
    3345             : 
    3346        1358 :         tmp = div_s( 16384, tmp );
    3347        1358 :         L_tmp = L_deposit_h( tmp );
    3348        1358 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    3349        1358 :         st_fx->prev_ener_shb_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    3350        1358 :         move16();
    3351             :     }
    3352             :     /* st->prev_SWB_fenv[i] = sqrt(curr_frame_pow/L_FRAME16k); */
    3353        1358 :     L_tmp = Mult_32_16( curr_frame_pow, 26214 ); /* curr_frame_pow_exp+8; 26214=(1/L_FRAME16k) in Q23 */
    3354        1358 :     tmp = 0;
    3355        1358 :     move16();
    3356        1358 :     IF( L_tmp != 0 )
    3357             :     {
    3358        1318 :         exp = norm_l( L_tmp );
    3359        1318 :         tmp = extract_h( L_shl( L_tmp, exp ) );
    3360        1318 :         exp = sub( exp, sub( 30, add( curr_frame_pow_exp, 8 ) ) );
    3361             : 
    3362        1318 :         tmp = div_s( 16384, tmp );
    3363        1318 :         L_tmp = L_deposit_h( tmp );
    3364        1318 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    3365        1318 :         tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    3366             :     }
    3367        1358 :     set16_fx( st_fx->hBWE_FD->prev_SWB_fenv_fx, tmp, SWB_FENV ); /* Q1 */
    3368             : 
    3369             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    3370        1358 :     sc = sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn2 );
    3371        1358 :     IF( sc != 0 )
    3372             :     {
    3373        5971 :         FOR( i = 0; i < 2 * ALLPASSSECTIONS_STEEP; i++ )
    3374             :         {
    3375        5118 :             hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i] = shl( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx[i], sc );
    3376        5118 :             move16();
    3377             :         }
    3378             : 
    3379         853 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    3380             :         {
    3381        9482 :             FOR( i = 0; i < HILBERT_MEM_SIZE; i++ )
    3382             :             {
    3383        9051 :                 hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i] = L_shl( hBWE_TD->genSHBsynth_Hilbert_Mem_fx[i], sc );
    3384        9051 :                 move32();
    3385             :             }
    3386             :         }
    3387         853 :         IF( EQ_32( st_fx->output_Fs, 48000 ) )
    3388             :         {
    3389         422 :             Scale_sig( hBWE_TD->int_3_over_2_tbemem_dec_fx, INTERP_3_2_MEM_LEN, sc );
    3390             :         }
    3391         853 :         IF( EQ_32( st_fx->output_Fs, 16000 ) )
    3392             :         {
    3393           0 :             Scale_sig( hBWE_TD->mem_resamp_HB_32k_fx, 2 * ALLPASSSECTIONS_STEEP + 1, sc );
    3394             :         }
    3395             :     }
    3396             :     /* i: shaped_shb_excitation[320] in (Q_bwe_exc)            */
    3397             :     /* i/o: st_fx->genSHBsynth_Hilbert_Mem_fx in (Q_bwe_exc)   */
    3398             :     /* i/o: st_fx->genSHBsynth_state_lsyn_filt_shb_local_fx in (Q_bwe_exc)   */
    3399             :     /* o: error in (Qx)                                 */
    3400        1358 :     GenSHBSynth_fx( shaped_shb_excitation, error, hBWE_TD->genSHBsynth_Hilbert_Mem_fx,
    3401        1358 :                     hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, st_fx->L_frame, &( hBWE_TD->syn_dm_phase ) );
    3402             : 
    3403        1358 :     Copy( error + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx, L_SHB_TRANSITION_LENGTH );
    3404             : 
    3405             :     /* resample SHB synthesis (if needed) and scale down */
    3406        1358 :     synth_scale_fx = 32767;
    3407        1358 :     move16(); /* 1.0 in Q15 */
    3408        1358 :     if ( EQ_16( st_fx->codec_mode, MODE1 ) )
    3409             :     {
    3410         748 :         synth_scale_fx = 29491;
    3411         748 :         move16(); /* 0.9 in Q15 */
    3412             :     }
    3413             : 
    3414        1358 :     IF( EQ_32( st_fx->output_Fs, 48000 ) )
    3415             :     {
    3416         610 :         IF( EQ_32( st_fx->extl, FB_TBE ) )
    3417             :         {
    3418           0 :             tmp = norm_l( GainFrame );
    3419           0 :             if ( GainFrame == 0 )
    3420             :             {
    3421           0 :                 tmp = 31;
    3422           0 :                 move16();
    3423             :             }
    3424           0 :             L_tmp = L_shl( GainFrame, tmp ); /* 18 + tmp */
    3425             : 
    3426           0 :             tmp1 = 0;
    3427           0 :             move16();
    3428           0 :             FOR( i = 0; i < L_FRAME16k; i++ )
    3429             :             {
    3430           0 :                 Word16 idx = 0;
    3431           0 :                 IF( i != 0 )
    3432             :                 {
    3433           0 :                     idx = idiv1616( i_mult( NUM_SHB_SUBFR, i ), L_FRAME16k );
    3434             :                 }
    3435           0 :                 L_tmp1 = Mult_32_16( L_tmp, GainShape[idx] );                        /* Q : 18 + tmp +15 -15*/
    3436           0 :                 White_exc16k[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
    3437           0 :                 move16();
    3438           0 :                 tmp1 = s_max( tmp1, abs_s( White_exc16k[i] ) );
    3439             :             }
    3440             : 
    3441           0 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
    3442           0 :             move16();
    3443           0 :             tmp = norm_s( tmp1 );
    3444           0 :             if ( tmp1 == 0 )
    3445             :             {
    3446           0 :                 tmp = 15;
    3447           0 :                 move16();
    3448             :             }
    3449             : 
    3450           0 :             FOR( i = 0; i < L_FRAME16k; i++ )
    3451             :             {
    3452           0 :                 White_exc16k[i] = shl( White_exc16k[i], sub( tmp, 1 ) );
    3453           0 :                 move16();
    3454             :             }
    3455           0 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
    3456           0 :             move16();
    3457             :         }
    3458             : 
    3459         610 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    3460             :         {
    3461           0 :             FOR( i = 0; i < L_FRAME32k; i++ )
    3462             :             {
    3463           0 :                 error[i] = mult_r( error[i], synth_scale_fx );
    3464           0 :                 move16();
    3465             :             }
    3466             :         }
    3467             : 
    3468         610 :         interpolate_3_over_2_allpass_fx( error, L_FRAME32k, synth, hBWE_TD->int_3_over_2_tbemem_dec_fx, allpass_poles_3_ov_2 );
    3469             :     }
    3470         748 :     ELSE IF( EQ_32( st_fx->output_Fs, 32000 ) )
    3471             :     {
    3472         748 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    3473             :         {
    3474      479468 :             FOR( i = 0; i < L_FRAME32k; i++ )
    3475             :             {
    3476      478720 :                 synth[i] = mult_r( synth_scale_fx, error[i] );
    3477      478720 :                 move16(); /*Qx*/
    3478             :             }
    3479             :         }
    3480             :         ELSE
    3481             :         {
    3482           0 :             Copy( error, synth, L_FRAME32k );
    3483             :         }
    3484             :     }
    3485           0 :     ELSE IF( EQ_32( st_fx->output_Fs, 16000 ) )
    3486             :     {
    3487           0 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    3488             :         {
    3489           0 :             FOR( i = 0; i < L_FRAME32k; i++ )
    3490             :             {
    3491           0 :                 error[i] = mult_r( error[i], synth_scale_fx );
    3492           0 :                 move16();
    3493             :             }
    3494             :         }
    3495             : 
    3496           0 :         Decimate_allpass_steep_fx( error, hBWE_TD->mem_resamp_HB_32k_fx, L_FRAME32k, synth );
    3497             :     }
    3498             : 
    3499             :     /* Update previous frame parameters for FEC */
    3500        1358 :     Copy( lsf_shb, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    3501        1358 :     IF( EQ_16( st_fx->codec_mode, MODE1 ) )
    3502             :     {
    3503         748 :         hBWE_TD->GainFrame_prevfrm_fx = GainFrame;
    3504         748 :         move16(); /*Q18*/
    3505         748 :         hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec;
    3506         748 :         move16();
    3507             : 
    3508         748 :         if ( !st_fx->bfi )
    3509             :         {
    3510         748 :             hBWE_TD->GainAttn_fx = 32767; /*Q15*/
    3511         748 :             move16();
    3512             :         }
    3513             :     }
    3514             :     ELSE
    3515             :     {
    3516         610 :         IF( !st_fx->bfi )
    3517             :         {
    3518         610 :             hBWE_TD->GainFrame_prevfrm_fx = GainFrame;
    3519         610 :             move16(); /*Q18*/
    3520         610 :             hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec;
    3521         610 :             move16();
    3522         610 :             hBWE_TD->GainAttn_fx = 32767; /*Q15*/
    3523         610 :             move16();
    3524             :         }
    3525             :     }
    3526             : 
    3527        1358 :     hBWE_TD->prev_ener_fx = ener_tmp[NUM_SHB_SUBGAINS - 1];
    3528        1358 :     move32();
    3529        1358 :     hBWE_TD->prev_GainShape_fx = GainShape[NUM_SHB_SUBFR - 1];
    3530        1358 :     move32();
    3531        1358 :     *Q_synth = Q_bwe_exc;
    3532        1358 :     move16();
    3533        1358 :     hBWE_TD->prev_Q_bwe_syn2 = Q_bwe_exc;
    3534        1358 :     move16();
    3535        1358 :     hBWE_TD->prev_Qx = Q_bwe_exc;
    3536        1358 :     move16();
    3537             : 
    3538        1358 :     return;
    3539             : }
    3540             : 
    3541           0 : static void gradientGainShape(
    3542             :     Decoder_State *st_fx,
    3543             :     Word16 *GainShape,
    3544             :     Word32 *GainFrame )
    3545             : {
    3546             :     Word16 i, j, tmp;
    3547             :     Word16 GainShapeTemp[NUM_SHB_SUBFR / 4];
    3548             :     Word16 GainGrad0[3];
    3549             :     Word16 GainGrad1[3];
    3550             :     Word16 GainGradFEC[4];
    3551             :     TD_BWE_DEC_HANDLE hBWE_TD;
    3552           0 :     hBWE_TD = st_fx->hBWE_TD;
    3553             : 
    3554             :     /* the previous frame gainshape gradient and the gainshape gradient pattern for the current frame */
    3555           0 :     FOR( j = 0; j < 3; j++ )
    3556             :     {
    3557           0 :         GainGrad0[j] = sub( shr( hBWE_TD->GainShape_Delay_fx[j + 1], 1 ), shr( hBWE_TD->GainShape_Delay_fx[j], 1 ) );
    3558           0 :         move16(); /* Q14 */
    3559           0 :         GainGrad1[j] = sub( shr( hBWE_TD->GainShape_Delay_fx[j + 5], 1 ), shr( hBWE_TD->GainShape_Delay_fx[j + 4], 1 ) );
    3560           0 :         move16(); /* Q14 */
    3561           0 :         GainGradFEC[j + 1] = add( mult_r( GainGrad0[j], 13107 ), mult_r( GainGrad1[j], 19660 ) );
    3562           0 :         move16(); /* Q14 */
    3563             :     }
    3564             : 
    3565             :     /* gradient for the first gainshape */
    3566           0 :     test();
    3567           0 :     test();
    3568           0 :     test();
    3569           0 :     IF( ( ( GT_16( shr( GainGrad1[2], 1 ), GainGrad1[1] ) ) && ( GT_16( shr( GainGrad1[1], 1 ), GainGrad1[0] ) ) ) ||
    3570             :         ( ( LT_16( shr( GainGrad1[2], 1 ), GainGrad1[1] ) ) && ( LT_16( shr( GainGrad1[1], 1 ), GainGrad1[0] ) ) ) )
    3571             :     {
    3572           0 :         GainGradFEC[0] = add( mult_r( GainGrad1[1], 3277 ), mult_r( GainGrad1[2], 29490 ) );
    3573           0 :         move16(); /* Q14 */
    3574             :     }
    3575             :     ELSE
    3576             :     {
    3577           0 :         GainGradFEC[0] = add( mult_r( GainGrad1[0], 6553 ), mult_r( GainGrad1[1], 9830 ) );
    3578           0 :         move16();
    3579           0 :         GainGradFEC[0] = add( GainGradFEC[0], mult_r( GainGrad1[2], 16384 ) );
    3580           0 :         move16(); /* Q14 */
    3581             :     }
    3582             : 
    3583             :     /* get the first gainshape template */
    3584           0 :     test();
    3585           0 :     test();
    3586           0 :     IF( ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) ) && ( GainGradFEC[0] > 0 ) )
    3587             :     {
    3588           0 :         GainShapeTemp[0] = add( shr( hBWE_TD->GainShape_Delay_fx[7], 1 ), GainGradFEC[0] );
    3589           0 :         move16();
    3590             :     }
    3591           0 :     ELSE IF( GainGradFEC[0] > 0 )
    3592             :     {
    3593           0 :         GainShapeTemp[0] = add( shr( hBWE_TD->GainShape_Delay_fx[7], 1 ), mult_r( GainGradFEC[0], 16384 ) );
    3594           0 :         move16(); /* Q14 */
    3595             :     }
    3596             :     ELSE
    3597             :     {
    3598           0 :         GainShapeTemp[0] = shr( hBWE_TD->GainShape_Delay_fx[7], 1 );
    3599           0 :         move16(); /* Q14 */
    3600             :     }
    3601             : 
    3602             :     /*Get the second the third and the fourth gainshape template*/
    3603             : 
    3604           0 :     tmp = shr( GainGrad1[2], 3 ); /* GainGrad1[2]/8 */
    3605           0 :     tmp = mult_r( tmp, 26214 );   /* 0.8 in Q15 tmp*(8/10) */
    3606             : 
    3607           0 :     test();
    3608           0 :     IF( ( GT_16( tmp, GainGrad1[1] ) ) && ( GainGrad1[1] > 0 ) )
    3609             :     {
    3610           0 :         FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    3611             :         {
    3612           0 :             GainShapeTemp[i] = add( GainShapeTemp[i - 1], mult_r( GainGradFEC[i], 26214 ) );
    3613           0 :             move16(); /* GainShapeTemp[i-1] + 0.8* GainShapeTemp[i] */
    3614           0 :             GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
    3615           0 :             move16();
    3616             :         }
    3617             :     }
    3618             :     ELSE
    3619             :     {
    3620           0 :         test();
    3621           0 :         IF( ( GT_16( tmp, GainGrad1[1] ) ) && ( GainGrad1[1] < 0 ) )
    3622             :         {
    3623           0 :             FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    3624             :             {
    3625           0 :                 GainShapeTemp[i] = add( GainShapeTemp[i - 1], mult_r( GainGradFEC[i], 6553 ) );
    3626           0 :                 move16(); /* GainShapeTemp[i-1] + 0.8* GainShapeTemp[i] */
    3627           0 :                 GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
    3628           0 :                 move16(); /* Q14 */
    3629             :             }
    3630             :         }
    3631             :         ELSE
    3632             :         {
    3633           0 :             FOR( i = 1; i < NUM_SHB_SUBFR / 4; i++ )
    3634             :             {
    3635           0 :                 GainShapeTemp[i] = add( GainShapeTemp[i - 1], GainGradFEC[i] );
    3636           0 :                 move16();
    3637           0 :                 GainShapeTemp[i] = s_max( GainShapeTemp[i], 328 /*0.01f Q15*/ );
    3638           0 :                 move16();
    3639             :             }
    3640             :         }
    3641             :     }
    3642             : 
    3643             :     /* Get the gainshape and gain frame for the current frame*/
    3644           0 :     test();
    3645           0 :     test();
    3646           0 :     test();
    3647           0 :     IF( ( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) ) && EQ_16( st_fx->nbLostCmpt, 1 ) )
    3648             :     {
    3649           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3650             :         {
    3651           0 :             FOR( j = 0; j < 4; j++ )
    3652             :             {
    3653           0 :                 tmp = mult_r( GainShapeTemp[i], 19660 ); /* GainShapeTemp[i]*0.6 */
    3654             : 
    3655           0 :                 IF( GT_16( 8192, tmp ) )
    3656             :                 {
    3657           0 :                     GainShape[i * 4 + j] = shl( tmp, 2 );
    3658           0 :                     move16(); /* (GainShapeTemp[i]*0.6)>>1 */
    3659             :                 }
    3660             :                 ELSE
    3661             :                 {
    3662           0 :                     GainShape[i * 4 + j] = 32767;
    3663           0 :                     move16(); /* Clipping here to avoid the a huge change of the code due to gain shape change */
    3664             :                 }
    3665             :             }
    3666             :         }
    3667           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 31129 );
    3668           0 :         move16();
    3669             :     }
    3670           0 :     ELSE IF( EQ_16( st_fx->prev_coder_type, UNVOICED ) || ( st_fx->last_good == UNVOICED_CLAS ) )
    3671             :     {
    3672           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3673             :         {
    3674           0 :             FOR( j = 0; j < 4; j++ )
    3675             :             {
    3676           0 :                 IF( LT_16( GainShapeTemp[i], 16384 ) )
    3677             :                 {
    3678           0 :                     GainShape[i * 4 + j] = shl( GainShapeTemp[i], 1 );
    3679           0 :                     move16();
    3680             :                 }
    3681             :                 ELSE
    3682             :                 {
    3683           0 :                     GainShape[i * 4 + j] = 32767;
    3684           0 :                     move16();
    3685             :                 }
    3686             :             }
    3687             :         }
    3688           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 31129 );
    3689           0 :         move16();
    3690             :     }
    3691           0 :     ELSE IF( GT_16( st_fx->nbLostCmpt, 1 ) )
    3692             :     {
    3693           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3694             :         {
    3695           0 :             FOR( j = 0; j < 4; j++ )
    3696             :             {
    3697           0 :                 GainShape[add( i * 4, j )] = GainShapeTemp[i];
    3698           0 :                 move16();
    3699             :             }
    3700             :         }
    3701           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 16384 );
    3702           0 :         move16();
    3703             :     }
    3704             :     ELSE
    3705             :     {
    3706           0 :         FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3707             :         {
    3708           0 :             FOR( j = 0; j < 4; j++ )
    3709             :             {
    3710           0 :                 IF( LT_16( GainShapeTemp[i], 16384 ) )
    3711             :                 {
    3712           0 :                     GainShape[i * 4 + j] = shl( GainShapeTemp[i], 1 );
    3713           0 :                     move16();
    3714             :                 }
    3715             :                 ELSE
    3716             :                 {
    3717           0 :                     GainShape[i * 4 + j] = 32767;
    3718           0 :                     move16();
    3719             :                 }
    3720             :             }
    3721             :         }
    3722           0 :         hBWE_TD->GainAttn_fx = mult_r( hBWE_TD->GainAttn_fx, 27852 );
    3723           0 :         move16();
    3724             :     }
    3725             : 
    3726           0 :     *GainFrame = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, hBWE_TD->GainAttn_fx ); /* Q18 */
    3727           0 :     move32();
    3728           0 : }
    3729             : 
    3730             : /*-------------------------------------------------------------------*
    3731             :  * Dequant_lower_LSF()
    3732             :  *
    3733             :  * Dequantized the lower LSFs
    3734             :  *-------------------------------------------------------------------*/
    3735             : 
    3736             : 
    3737       72248 : static void Dequant_lower_LSF_fx(
    3738             :     const Word16 lsf_idx[], /* i  : LSF indices */
    3739             :     Word16 lsf_q[]          /* o  : Quantized LSFs Q15*/
    3740             : )
    3741             : {
    3742             :     Word16 i;
    3743             : 
    3744       72248 :     lsf_q[0] = lsf_q_cb_fx[0][lsf_idx[0]];
    3745       72248 :     move16();
    3746      361240 :     FOR( i = 1; i < NUM_Q_LSF; i++ )
    3747             :     {
    3748      288992 :         lsf_q[i] = add( lsf_q_cb_fx[i][lsf_idx[i]], lsf_q[i - 1] ); /*Q15*/
    3749      288992 :         move16();
    3750             :     }
    3751             : 
    3752       72248 :     return;
    3753             : }
    3754             : 
    3755             : /*-------------------------------------------------------------------*
    3756             :  * Map_higher_LSF()
    3757             :  *
    3758             :  * Map the higher LSFs from the lower LSFs
    3759             :  *-------------------------------------------------------------------*/
    3760             : 
    3761       72248 : static void Map_higher_LSF_fx(
    3762             :     Word16 lsf_q[],        /* i/o : Quantized lower LSFs               Q15*/
    3763             :     const Word16 m,        /* i   : Mirroring point                    Q15*/
    3764             :     const Word16 grid_in[] /* i   : Input LSF smoohthing grid   Q15*/
    3765             : )
    3766             : {
    3767             :     Word16 lsf_map[NUM_MAP_LSF];
    3768             :     Word16 grid[NUM_MAP_LSF];
    3769             :     Word16 last_q_lsf;
    3770             :     Word16 lsf_smooth[NUM_MAP_LSF];
    3771             :     Word16 offset;
    3772             :     Word16 i;
    3773             :     Word16 scale;
    3774             : 
    3775      433488 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3776             :     {
    3777      361240 :         lsf_map[i] = sub( shl( m, 1 ), lsf_q[NUM_MAP_LSF - 1 - i] ); /*Q15*/
    3778      361240 :         move16();
    3779             :     }
    3780             : 
    3781       72248 :     IF( GT_16( m, MAX_LSF_FX_BY_2 ) )
    3782             :     {
    3783       14383 :         offset = lsf_map[0]; /*Q15*/
    3784       14383 :         move16();
    3785       14383 :         scale = div_s( sub( MAX_LSF_FX, m ), m );
    3786       86298 :         FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3787             :         {
    3788       71915 :             lsf_map[i] = add( mult_r( sub( lsf_map[i], offset ), scale ), offset ); /*Q15*/
    3789       71915 :             move16();
    3790             :         }
    3791             :     }
    3792             : 
    3793       72248 :     last_q_lsf = lsf_q[NUM_Q_LSF - 1]; /*Q15*/
    3794       72248 :     move16();
    3795       72248 :     scale = sub( MAX_LSF_FX, last_q_lsf );
    3796             : 
    3797      433488 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3798             :     {
    3799      361240 :         grid[i] = add( mult_r( grid_in[i], scale ), last_q_lsf ); /*Q15*/
    3800      361240 :         move16();
    3801             :     }
    3802             : 
    3803      433488 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3804             :     {
    3805      361240 :         lsf_smooth[i] = sub( mult_r( grid_smoothing_fx[i], grid[i] ) /*Q15*/,
    3806      361240 :                              mult_r( lsf_map[i], add( grid_smoothing_fx[i], -32768 /*-1.0f Q15*/ /*Q15*/ ) ) ); /*Q15*/
    3807      361240 :         move16();
    3808             :     }
    3809             : 
    3810      433488 :     FOR( i = 0; i < NUM_MAP_LSF; i++ )
    3811             :     {
    3812      361240 :         lsf_q[NUM_Q_LSF + i] = lsf_smooth[i]; /*Q15*/
    3813      361240 :         move16();
    3814             :     }
    3815             : 
    3816       72248 :     return;
    3817             : }
    3818             : 
    3819             : 
    3820       72248 : static void Dequant_mirror_point_fx(
    3821             :     const Word16 lsf_q[], /* i/o : Quantized lower LSFs Q15*/
    3822             :     const Word16 m_idx,   /* i   : Mirror point index   Q0 */
    3823             :     Word16 *m             /* i   : Mirroring point      Q15*/
    3824             : )
    3825             : {
    3826       72248 :     *m = add( mirror_point_q_cb_fx[m_idx], lsf_q[NUM_Q_LSF - 1] ); /*Q15*/
    3827       72248 :     move16();
    3828             : 
    3829       72248 :     return;
    3830             : }
    3831             : 
    3832             : 
    3833        6344 : static Word16 dotp_loc(
    3834             :     const Word16 x[], /* i  : vector x[]                   Qx */
    3835             :     const Word32 y[], /* i  : vector y[]                   Qy */
    3836             :     const Word16 n    /* i  : vector length                 */
    3837             : )
    3838             : {
    3839             :     Word16 i;
    3840             :     Word32 suma;
    3841        6344 :     Word16 guarded_bits = find_guarded_bits_fx( n );
    3842        6344 :     suma = L_shr( Mpy_32_16_1( y[0], x[0] ), guarded_bits ); /*Qx + Qy - guarded_bits*/
    3843             : 
    3844       50752 :     FOR( i = 1; i < n; i++ )
    3845             :     {
    3846       44408 :         suma = L_add( suma, L_shr( Mpy_32_16_1( y[i], x[i] ), guarded_bits ) ); /*Qx + Qy - guarded_bits*/
    3847             :     }
    3848        6344 :     suma = L_shl_sat( suma, guarded_bits ); /*Qx + Qy*/
    3849             : 
    3850        6344 :     return extract_h( suma ); /*Qx + Qy - 16*/
    3851             : }
    3852             : 
    3853      107174 : void ivas_dequantizeSHBparams_fx_9_1(
    3854             :     Decoder_State *st_fx,
    3855             :     const Word16 extl,        /* i  : extension layer                             */
    3856             :     Word32 extl_brate,        /* i  : extensiuon layer bitrate                    */
    3857             :     Word16 *Q_lsf,            /* o  : SHB LSF from de-quantization             Q15*/
    3858             :     Word16 *Q_subgain,        /* o  : SHB subframe gains from de-quantization  Q15*/
    3859             :     Word32 *Q_framegrain,     /* o  : SHB frame gain from de-quantization      Q18*/
    3860             :     Word16 *uv_flag,          /* o  : unvoiced flag*/
    3861             :     Word32 *Q_shb_ener_sf,    /* o  : Q15                                         */
    3862             :     Word16 *Q_shb_res_gshape, /* o  : Q14                                         */
    3863             :     Word16 *Q_mixFactors,     /* o  : Q15                                         */
    3864             :     Word16 *MSFlag )
    3865             : {
    3866             :     Word16 i, j, idxLSF, idxSubGain, idxFrameGain;
    3867             :     Word16 Q_combined_gains[NUM_SHB_SUBFR / 4];
    3868             :     Word16 lsf_q[LPC_SHB_ORDER];
    3869             :     Word16 lsf_idx[NUM_Q_LSF];
    3870             :     Word16 m_idx, grid_idx;
    3871             :     Word16 m;
    3872             :     Word32 L_tmp;
    3873             :     Word16 tmp, frac, exp;
    3874             :     Word16 idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
    3875             :     Word16 temp_shb_ener_sf_fx;
    3876             :     TD_BWE_DEC_HANDLE hBWE_TD;
    3877             : 
    3878             :     UWord32 Idx_lvq;
    3879             :     Word16 Idx, Idx_pred;
    3880             :     Word16 num_bits_lvq;
    3881             :     Word16 out[LATTICE_DIM];
    3882             :     const Word16 *cb_stage;
    3883             :     Word16 predictor_bits;
    3884      107174 :     Word16 nbits = NUM_BITS_SHB_MSLVQ;
    3885      107174 :     move16();
    3886      107174 :     hBWE_TD = st_fx->hBWE_TD;
    3887             : 
    3888             :     /* LSFs */
    3889             : 
    3890      107174 :     IF( EQ_16( extl, WB_TBE ) )
    3891             :     {
    3892        6839 :         IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
    3893             :         {
    3894        2427 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    3895             :             {
    3896           0 :                 idxFrameGain = hBWE_TD->gFrame_WB;
    3897           0 :                 move16();
    3898           0 :                 idxLSF = hBWE_TD->lsf_WB;
    3899           0 :                 move16();
    3900             :             }
    3901             :             ELSE
    3902             :             {
    3903        2427 :                 idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain_LBR_WB );
    3904        2427 :                 idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_LBR_WB_LSF );
    3905             :             }
    3906             : 
    3907        2427 :             Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_LBR_WB ), Q_lsf, LPC_SHB_ORDER_LBR_WB );
    3908        2427 :             set16_fx( Q_subgain, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    3909        2427 :             Copy32( SHBCB_FrameGain16_fx + idxFrameGain, Q_framegrain, 1 );
    3910             :         }
    3911             :         ELSE
    3912             :         {
    3913        4412 :             *uv_flag = (Word16) get_next_indice_fx( st_fx, 1 );
    3914        4412 :             move16();
    3915        4412 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    3916        4412 :             idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain );
    3917        4412 :             idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_WB_LSF );
    3918        4412 :             Copy( wb_bwe_lsfvq_cbook_8bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_WB ), Q_lsf, LPC_SHB_ORDER_WB );
    3919        4412 :             Copy( HBCB_SubGain5bit_fx + i_mult( idxSubGain, NUM_SHB_SUBFR / 4 ), Q_combined_gains, NUM_SHB_SUBFR / 4 );
    3920             : 
    3921       22060 :             FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    3922             :             {
    3923             : 
    3924       17648 :                 L_tmp = L_mult( Q_combined_gains[i], 21771 ); /*Q26  0.166096 in Q17 */
    3925       17648 :                 L_tmp = L_shr( L_tmp, 10 );
    3926       17648 :                 frac = L_Extract_lc( L_tmp, &exp );
    3927       17648 :                 tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    3928             :                 /* output of Pow2() will be: */
    3929             :                 /* 16384 < Pow2() <= 32767 */
    3930       17648 :                 Q_combined_gains[i] = shl( tmp, add( exp, 1 ) ); /* Q15 */
    3931       17648 :                 move16();
    3932             :             }
    3933             : 
    3934       22060 :             FOR( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
    3935             :             {
    3936       17648 :                 Q_subgain[i] = Q_combined_gains[i / 2]; // Q15
    3937       17648 :                 move16();
    3938       17648 :                 Q_subgain[i + 1] = Q_combined_gains[i / 2]; // Q15
    3939       17648 :                 move16();
    3940             :             }
    3941             : 
    3942             :             /* frame gain */
    3943        4412 :             Copy32( SHBCB_FrameGain64_fx + idxFrameGain, Q_framegrain, 1 );
    3944             :         }
    3945             :     }
    3946             :     ELSE /* SWB TBE DEC */
    3947             :     {
    3948      100335 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    3949             :         {
    3950           0 :             idxSubGain = hBWE_TD->idxSubGains;
    3951           0 :             move16();
    3952           0 :             idxFrameGain = hBWE_TD->idxFrameGain;
    3953           0 :             move16();
    3954             :         }
    3955             :         ELSE
    3956             :         {
    3957      100335 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    3958      100335 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    3959             :             {
    3960        3172 :                 idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN_1k75 );
    3961             :             }
    3962             :             ELSE
    3963             :             {
    3964       97163 :                 idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
    3965             :             }
    3966             :         }
    3967             : 
    3968             :         /* Multi Source Flag */
    3969      100335 :         test();
    3970      100335 :         test();
    3971      100335 :         IF( GE_16( st_fx->element_mode, IVAS_CPE_DFT ) && !( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) && st_fx->tdm_LRTD_flag ) )
    3972             :         {
    3973       22137 :             *MSFlag = (Word16) get_next_indice_fx( st_fx, STEREO_ICBWE_MSFLAG_BITS );
    3974       22137 :             move16();
    3975             :         }
    3976             :         ELSE
    3977             :         {
    3978       78198 :             *MSFlag = 0;
    3979       78198 :             move16();
    3980             :         }
    3981             : 
    3982      100335 :         IF( GE_32( st_fx->extl_brate, SWB_TBE_2k8 ) )
    3983             :         {
    3984       20738 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    3985             :             {
    3986           0 :                 idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
    3987           0 :                 move16();
    3988             :             }
    3989             :             ELSE
    3990             :             {
    3991       20738 :                 idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
    3992             :             }
    3993       20738 :             temp_shb_ener_sf_fx = usdequant_fx( idx_shb_fr_gain, 0, 86 ); /* 86 = 0.042f in Q11 = Qin-1 */
    3994             :             /* o: temp_shb_ener_sf_fx in Q12 */
    3995             : 
    3996             :             /* *Q_shb_ener_sf = Pow(10.0, temp_shb_ener_sf_fx );        */
    3997             :             /*                = pow(2, 3.321928*temp_shb_ener_sf_fx)    */
    3998       20738 :             L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
    3999       20738 :             L_tmp = L_shl( L_tmp, -10 );                  /* bring L_tmp from Q26 to Q16 */
    4000       20738 :             frac = L_Extract_lc( L_tmp, &exp );           /* Extract exponent */
    4001       20738 :             L_tmp = Pow2( 14, frac );
    4002       20738 :             *Q_shb_ener_sf = L_shl( L_tmp, sub( exp, 14 ) /*exp - 14 + 0 */ ); /* In Q_ener: 2*Q_shb+1, Q_shb = 0; */
    4003       20738 :             move32();
    4004             : 
    4005      124428 :             FOR( i = 0; i < 5; i++ )
    4006             :             {
    4007      103690 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4008             :                 {
    4009           0 :                     idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
    4010           0 :                     move16();
    4011             :                 }
    4012             :                 ELSE
    4013             :                 {
    4014      103690 :                     idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
    4015      103690 :                     move16();
    4016             :                 }
    4017      103690 :                 Q_shb_res_gshape[i] = usdequant_fx( idx_res_gs[i],
    4018             :                                                     2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
    4019             :                                                     1024 /*0.125f Q13*/  /*1024 = 0.125 in Q13 */
    4020             :                 );
    4021      103690 :                 move16();
    4022             :                 /* o: Q_shb_res_gshape in Q14 */
    4023             :             }
    4024             : 
    4025       20738 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4026             :             {
    4027           0 :                 idx_mixFac = hBWE_TD->idx_mixFac;
    4028           0 :                 move16();
    4029             :             }
    4030             :             ELSE
    4031             :             {
    4032       20738 :                 idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4033             :             }
    4034       20738 :             *Q_mixFactors = usdequant_fx( idx_mixFac, 4096 /* 0.125 in Q15 */, 2048 /* 0.125 in Q14 */ );
    4035       20738 :             move16();
    4036             :             /* o: Q_mixFactors in Q15 */
    4037             :         }
    4038             :         ELSE
    4039             :         {
    4040       79597 :             *Q_shb_ener_sf = L_deposit_l( 0 );
    4041       79597 :             move32();
    4042       79597 :             test();
    4043       79597 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4044             :             {
    4045        4038 :                 idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4046        4038 :                 *Q_mixFactors = usdequant_fx( idx_mixFac, 0 /* 0.0f in Q15*/, 2341 /*1.0f / ((1 << NUM_BITS_SHB_VF) - 1) in Q14*/ );
    4047        4038 :                 move16();
    4048             :             }
    4049             :             ELSE
    4050             :             {
    4051       75559 :                 *Q_mixFactors = 0;
    4052       75559 :                 move16();
    4053             :             }
    4054       79597 :             set16_fx( Q_shb_res_gshape, 0, 5 );
    4055             :         }
    4056             : 
    4057             :         /* LSFs */
    4058      100335 :         test();
    4059      100335 :         test();
    4060      100335 :         test();
    4061      100335 :         test();
    4062      100335 :         test();
    4063      100335 :         test();
    4064      100335 :         test();
    4065      100335 :         test();
    4066      100335 :         test();
    4067      100335 :         IF( ( EQ_32( st_fx->extl_brate, SWB_TBE_0k95 ) || EQ_32( st_fx->extl_brate, SWB_TBE_1k10 ) ) && EQ_16( st_fx->codec_mode, MODE1 ) )
    4068             :         {
    4069       26273 :             set16_fx( lsf_idx, 0, 5 );
    4070       26273 :             lsf_idx[0] = (Word16) get_next_indice_fx( st_fx, 8 );
    4071       26273 :             move16();
    4072       26273 :             grid_idx = 0;
    4073       26273 :             move16();
    4074       26273 :             m_idx = 0;
    4075       26273 :             move16();
    4076             : 
    4077       26273 :             Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
    4078             :         }
    4079       74062 :         ELSE IF( ( st_fx->rf_flag == 0 ) && !( ( ( st_fx->element_mode == EVS_MONO ) && EQ_32( st_fx->total_brate, ACELP_9k60 ) ) || ( ( st_fx->element_mode == EVS_MONO ) && ( ( st_fx->total_brate == 0 ) ) && ( ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) ) || ( EQ_32( st_fx->last_total_brate, ACELP_13k20 ) && st_fx->rf_flag_last ) ) ) ) )
    4080             :         {
    4081       74062 :             IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4082             :             {
    4083             :                 /* read multi-stage LVQ quantizer */
    4084        3172 :                 IF( GE_16( nbits, 19 ) )
    4085             :                 {
    4086        3172 :                     cb_stage = cb_LSF_BWE_fx[0];
    4087        3172 :                     move16();
    4088             :                 }
    4089             :                 ELSE
    4090             :                 {
    4091           0 :                     cb_stage = cb_LSF_BWE_fx[1];
    4092           0 :                     move16();
    4093             :                 }
    4094        3172 :                 set16_fx( lsf_q, 0, LPC_SHB_ORDER );
    4095             : 
    4096             :                 /* VQ part */
    4097        3172 :                 num_bits_lvq = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3];
    4098        3172 :                 Idx = get_next_indice_fx( st_fx, num_bits_lvq );
    4099        3172 :                 v_add_16( lsf_q, cb_stage + i_mult( Idx, 6 ), lsf_q, 6 );
    4100             : 
    4101             :                 /* MSLVQ part */
    4102        3172 :                 num_bits_lvq = sub( sub( nbits, num_bits_lvq ), config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2] );
    4103        3172 :                 predictor_bits = config_LSF_BWE[( NUM_BITS_SHB_MSLVQ - nbits ) * 3 + 2];
    4104        3172 :                 Idx_pred = 0;
    4105        3172 :                 move16();
    4106             : 
    4107        3172 :                 IF( EQ_16( num_bits_lvq, 16 ) )
    4108             :                 {
    4109             :                     /* MSLVQ part */
    4110           0 :                     Idx_lvq = L_add( get_next_indice_fx( st_fx, sub( num_bits_lvq, 1 ) ), L_shl( get_next_indice_fx( st_fx, 1 ), 15 ) );
    4111           0 :                     deindex_lvq_SHB_fx( Idx_lvq, out, num_bits_lvq, 0 );
    4112             :                 }
    4113             :                 ELSE
    4114             :                 {
    4115             :                     /* MSLVQ part */
    4116        3172 :                     Idx_lvq = get_next_indice_fx( st_fx, num_bits_lvq );
    4117        3172 :                     deindex_lvq_SHB_fx( Idx_lvq, out, num_bits_lvq, extract_l( LT_16( nbits, 19 ) ) );
    4118             :                 }
    4119             : 
    4120             :                 /* mvr2r( mean_lsf, Q_lsfs, LPC_SHB_ORDER ); */
    4121        3172 :                 v_add_16( lsf_q, out, lsf_q, LATTICE_DIM ); /* quantized mean removed data for first 8 dim*/
    4122             : 
    4123             :                 /* predict last 2 components */
    4124        3172 :                 IF( predictor_bits == 0 )
    4125             :                 {
    4126           0 :                     lsf_q[LATTICE_DIM] = dotp_loc( lsf_q, LastCoefPred_0bit_fx, LATTICE_DIM );
    4127           0 :                     move16();
    4128           0 :                     lsf_q[LATTICE_DIM + 1] = dotp_loc( lsf_q, &LastCoefPred_0bit_fx[LATTICE_DIM + 1], LATTICE_DIM );
    4129           0 :                     move16();
    4130             :                 }
    4131             :                 ELSE
    4132             :                 {
    4133        3172 :                     Idx_pred = (Word16) get_next_indice_fx( st_fx, 1 );
    4134        3172 :                     lsf_q[LATTICE_DIM] = dotp_loc( lsf_q, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * Idx_pred], LATTICE_DIM );
    4135        3172 :                     move16();
    4136        3172 :                     lsf_q[LATTICE_DIM + 1] = dotp_loc( lsf_q, &LastCoefPred_1bit_fx[2 * ( LATTICE_DIM + 1 ) * Idx_pred + LATTICE_DIM + 1], LATTICE_DIM );
    4137        3172 :                     move16();
    4138             :                 }
    4139             : 
    4140        3172 :                 IF( LT_16( nbits, NUM_BITS_SHB_MSLVQ ) )
    4141             :                 {
    4142           0 :                     Idx_pred = (Word16) get_next_indice_fx( st_fx, sub( NUM_BITS_SHB_MSLVQ, nbits ) );
    4143             :                 }
    4144             : 
    4145        3172 :                 v_add_16( SHB_LSF_mean_fx, lsf_q, lsf_q, LPC_SHB_ORDER );
    4146        3172 :                 v_sort( lsf_q, 0, LPC_SHB_ORDER - 1 );
    4147             :             }
    4148             :             ELSE
    4149             :             {
    4150             :                 /* LSFs */
    4151       70890 :                 test();
    4152       70890 :                 test();
    4153       70890 :                 test();
    4154       70890 :                 IF( EQ_32( extl_brate, SWB_TBE_1k6 ) || EQ_32( extl_brate, FB_TBE_1k8 ) || EQ_32( extl_brate, SWB_TBE_2k8 ) || EQ_32( extl_brate, FB_TBE_3k0 ) )
    4155             :                 {
    4156       70890 :                     IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4157             :                     {
    4158           0 :                         FOR( i = 0; i < NUM_Q_LSF; i++ )
    4159             :                         {
    4160           0 :                             lsf_idx[i] = hBWE_TD->lsf_idx[i];
    4161           0 :                             move16();
    4162             :                         }
    4163             :                     }
    4164             :                     ELSE
    4165             :                     {
    4166      425340 :                         FOR( i = 0; i < NUM_Q_LSF; i++ )
    4167             :                         {
    4168      354450 :                             lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
    4169      354450 :                             move16();
    4170             :                         }
    4171             :                     }
    4172             :                 }
    4173       70890 :                 Dequant_lower_LSF_fx( lsf_idx, lsf_q );
    4174             : 
    4175       70890 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4176             :                 {
    4177           0 :                     m_idx = hBWE_TD->m_idx;
    4178           0 :                     move16();
    4179             :                 }
    4180             :                 ELSE
    4181             :                 {
    4182       70890 :                     m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
    4183             :                 }
    4184             : 
    4185       70890 :                 Dequant_mirror_point_fx( lsf_q, m_idx, &m );
    4186             : 
    4187             :                 /* safety check in case of bit errors */
    4188       70890 :                 IF( GT_16( m, MAX_LSF_FX ) )
    4189             :                 {
    4190           0 :                     st_fx->BER_detect = 1;
    4191           0 :                     move16();
    4192           0 :                     m = MAX_LSF_FX - 1;
    4193           0 :                     move16();
    4194             :                 }
    4195             : 
    4196       70890 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4197             :                 {
    4198           0 :                     grid_idx = hBWE_TD->grid_idx;
    4199           0 :                     move16();
    4200             :                 }
    4201             :                 ELSE
    4202             :                 {
    4203       70890 :                     grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
    4204             :                 }
    4205             : 
    4206       70890 :                 Map_higher_LSF_fx( lsf_q, m, lsf_grid_fx[grid_idx] );
    4207             :             }
    4208      814682 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    4209             :             {
    4210             :                 /* safety check in case of bit errors */
    4211      740620 :                 IF( GT_16( lsf_q[LPC_SHB_ORDER - 1 - i], MAX_LSF_FX ) )
    4212             :                 {
    4213           0 :                     st_fx->BER_detect = 1;
    4214           0 :                     move16();
    4215           0 :                     lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF_FX - 1;
    4216           0 :                     move16();
    4217             :                 }
    4218      740620 :                 Q_lsf[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
    4219      740620 :                 move16();
    4220             :             }
    4221             :         }
    4222             :         ELSE
    4223             :         {
    4224           0 :             set16_fx( lsf_idx, 0, 5 );
    4225           0 :             Copy( hBWE_TD->lsf_idx, lsf_idx, 5 );
    4226           0 :             grid_idx = 0;
    4227           0 :             move16();
    4228           0 :             m_idx = 0;
    4229           0 :             move16();
    4230           0 :             Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
    4231             :         }
    4232             : 
    4233      100335 :         space_lsfs_fx( Q_lsf, LPC_SHB_ORDER );
    4234             : 
    4235             :         /* Dequantize subgain indices */
    4236      100335 :         j = i_mult( idxSubGain, NUM_SHB_SUBGAINS );
    4237      100335 :         move16();
    4238      501675 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4239             :         {
    4240             :             /*    Q_subgain[i] = (float) pow(10.0, SHBCB_SubGain5bit[j++]); */
    4241             : 
    4242      401340 :             L_tmp = L_mult( SHBCB_SubGain5bit_fx[j++], 27213 ); /*Q28  3.321928 in Q13 */
    4243      401340 :             L_tmp = L_shr( L_tmp, 12 );
    4244      401340 :             frac = L_Extract_lc( L_tmp, &exp );
    4245      401340 :             tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    4246             :             /* output of Pow2() will be: */
    4247             :             /* 16384 < Pow2() <= 32767 */
    4248      401340 :             Q_subgain[i] = shl( tmp, add( exp, 1 ) ); /*Q15*/
    4249      401340 :             move16();
    4250             :         }
    4251             : 
    4252     1705695 :         FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    4253             :         {
    4254     1605360 :             Word16 idx = 0;
    4255     1605360 :             move16();
    4256     1605360 :             IF( i != 0 )
    4257             :             {
    4258     1505025 :                 idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
    4259             :             }
    4260     1605360 :             Q_subgain[i] = Q_subgain[idx];
    4261     1605360 :             move16();
    4262             :         }
    4263             : 
    4264             :         /* Frame gain */
    4265      100335 :         IF( EQ_32( st_fx->extl_brate, SWB_TBE_1k75 ) )
    4266             :         {
    4267        3172 :             *Q_framegrain = L_mac( SHB_GAIN_QLOW_1k75_FX, idxFrameGain, SHB_GAIN_QDELTA_1k75_FX );
    4268        3172 :             move32();
    4269             :         }
    4270             :         ELSE
    4271             :         {
    4272       97163 :             *Q_framegrain = L_mac( SHB_GAIN_QLOW_FX, idxFrameGain, SHB_GAIN_QDELTA_FX );
    4273       97163 :             move32();
    4274             :         }                                                   /*Q18*/
    4275      100335 :         L_tmp = Mult_32_16( *Q_framegrain, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    4276      100335 :         frac = L_Extract_lc( L_tmp, &exp );
    4277      100335 :         L_tmp = Pow2( 30, frac );
    4278      100335 :         *Q_framegrain = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    4279      100335 :         move32();
    4280             :     }
    4281             : 
    4282      107174 :     return;
    4283             : }
    4284             : 
    4285             : 
    4286             : /*==========================================================================*/
    4287             : /* FUNCTION      : static void dequantizeSHBparams_fx_9_1 ()        */
    4288             : /*--------------------------------------------------------------------------*/
    4289             : /* PURPOSE       : Dequantize super highband spectral envolope        */
    4290             : /*          temporal gains and frame gain              */
    4291             : /*--------------------------------------------------------------------------*/
    4292             : /* INPUT ARGUMENTS  :                            */
    4293             : /* _Word16 extl               i  : extension layer                */
    4294             : /* _Word32 extl_brate         i  : extensiuon layer bitrate          */
    4295             : /*--------------------------------------------------------------------------*/
    4296             : /* OUTPUT ARGUMENTS :                            */
    4297             : /* _Word16 *Q_lsf,              o  : SHB LSF from de-quantization       Q15 */
    4298             : /* _Word16 *Q_subgain,      o  : SHB subframe gains from de-quantization Q15*/
    4299             : /* _Word32 *Q_framegrain      o  : SHB frame gain from de-quantization Q18  */
    4300             : /*--------------------------------------------------------------------------*/
    4301             : /* INPUT/OUTPUT ARGUMENTS :                          */
    4302             : /*--------------------------------------------------------------------------*/
    4303             : /* RETURN ARGUMENTS :                            */
    4304             : /*           _ None                          */
    4305             : /*--------------------------------------------------------------------------*/
    4306             : /* CALLED FROM :                               */
    4307             : /*==========================================================================*/
    4308             : 
    4309        1358 : static void dequantizeSHBparams_fx_9_1(
    4310             :     Decoder_State *st_fx,
    4311             :     const Word16 extl,        /* i  : extension layer                             */
    4312             :     Word32 extl_brate,        /* i  : extensiuon layer bitrate                    */
    4313             :     Word16 *Q_lsf,            /* o  : SHB LSF from de-quantization             Q15*/
    4314             :     Word16 *Q_subgain,        /* o  : SHB subframe gains from de-quantization  Q15*/
    4315             :     Word32 *Q_framegrain,     /* o  : SHB frame gain from de-quantization      Q18*/
    4316             :     Word16 *uv_flag,          /* o  : unvoiced flag*/
    4317             :     Word32 *Q_shb_ener_sf,    /* o  :                                          Q15*/
    4318             :     Word16 *Q_shb_res_gshape, /* o  :                                          Q14*/
    4319             :     Word16 *Q_mixFactors      /* o  :                                          Q15*/
    4320             : )
    4321             : {
    4322             :     Word16 i, j, idxLSF, idxSubGain, idxFrameGain;
    4323             :     Word16 Q_combined_gains[NUM_SHB_SUBFR / 4];
    4324             :     Word16 lsf_q[LPC_SHB_ORDER];
    4325             :     Word16 lsf_idx[NUM_Q_LSF];
    4326             :     Word16 m_idx, grid_idx;
    4327             :     Word16 m;
    4328             :     Word32 L_tmp;
    4329             :     Word16 tmp, frac, exp;
    4330             :     Word16 idx_shb_fr_gain, idx_res_gs[5], idx_mixFac;
    4331             :     Word16 temp_shb_ener_sf_fx;
    4332             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4333        1358 :     hBWE_TD = st_fx->hBWE_TD;
    4334             : 
    4335             :     /* LSFs */
    4336             : 
    4337        1358 :     IF( EQ_16( extl, WB_TBE ) )
    4338             :     {
    4339           0 :         IF( EQ_32( extl_brate, WB_TBE_0k35 ) )
    4340             :         {
    4341           0 :             idxFrameGain = hBWE_TD->gFrame_WB;
    4342           0 :             move16();
    4343           0 :             idxLSF = hBWE_TD->lsf_WB;
    4344           0 :             move16();
    4345             : 
    4346           0 :             Copy( lbr_wb_bwe_lsfvq_cbook_2bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_LBR_WB ), Q_lsf, LPC_SHB_ORDER_LBR_WB );
    4347           0 :             set16_fx( Q_subgain, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR / 2 );
    4348           0 :             Copy32( SHBCB_FrameGain16_fx + idxFrameGain, Q_framegrain, 1 );
    4349             :         }
    4350             :         ELSE
    4351             :         {
    4352           0 :             *uv_flag = (Word16) get_next_indice_fx( st_fx, 1 );
    4353           0 :             move16();
    4354           0 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    4355           0 :             idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain );
    4356           0 :             idxLSF = (Word16) get_next_indice_fx( st_fx, NUM_BITS_WB_LSF );
    4357           0 :             Copy( wb_bwe_lsfvq_cbook_8bit_fx + i_mult( idxLSF, LPC_SHB_ORDER_WB ), Q_lsf, LPC_SHB_ORDER_WB );
    4358           0 :             Copy( HBCB_SubGain5bit_fx + i_mult( idxSubGain, NUM_SHB_SUBFR / 4 ), Q_combined_gains, NUM_SHB_SUBFR / 4 );
    4359             : 
    4360           0 :             FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    4361             :             {
    4362             : 
    4363           0 :                 L_tmp = L_mult( Q_combined_gains[i], 21771 ); /*Q26  0.166096 in Q17 */
    4364           0 :                 L_tmp = L_shr( L_tmp, 10 );
    4365           0 :                 frac = L_Extract_lc( L_tmp, &exp );
    4366           0 :                 tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    4367             :                 /* output of Pow2() will be: */
    4368             :                 /* 16384 < Pow2() <= 32767 */
    4369           0 :                 Q_combined_gains[i] = shl( tmp, add( exp, 1 ) ); /* Q15 */
    4370           0 :                 move16();
    4371             :             }
    4372             : 
    4373           0 :             FOR( i = 0; i < NUM_SHB_SUBFR / 2; i += 2 )
    4374             :             {
    4375           0 :                 Q_subgain[i] = Q_combined_gains[i / 2];
    4376           0 :                 move16();
    4377           0 :                 Q_subgain[i + 1] = Q_combined_gains[i / 2];
    4378           0 :                 move16();
    4379             :             }
    4380             : 
    4381             :             /* frame gain */
    4382           0 :             Copy32( SHBCB_FrameGain64_fx + idxFrameGain, Q_framegrain, 1 );
    4383             :         }
    4384             :     }
    4385             :     ELSE /* SWB TBE DEC */
    4386             :     {
    4387        1358 :         IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4388             :         {
    4389         610 :             idxSubGain = hBWE_TD->idxSubGains;
    4390         610 :             move16();
    4391         610 :             idxFrameGain = hBWE_TD->idxFrameGain;
    4392         610 :             move16();
    4393             :         }
    4394             :         ELSE
    4395             :         {
    4396         748 :             idxSubGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    4397         748 :             idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
    4398             :         }
    4399             : 
    4400        1358 :         test();
    4401        1358 :         IF( EQ_32( st_fx->total_brate, ACELP_24k40 ) || EQ_32( st_fx->total_brate, ACELP_32k ) )
    4402             :         {
    4403         610 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4404             :             {
    4405         610 :                 idx_shb_fr_gain = hBWE_TD->idx_shb_fr_gain;
    4406         610 :                 move16();
    4407             :             }
    4408             :             ELSE
    4409             :             {
    4410           0 :                 idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
    4411             :             }
    4412         610 :             temp_shb_ener_sf_fx = usdequant_fx( idx_shb_fr_gain, 0, 86 ); /* 86 = 0.042f in Q11 = Qin-1 */
    4413             :             /* o: temp_shb_ener_sf_fx in Q12 */
    4414             : 
    4415             :             /* *Q_shb_ener_sf = Pow(10.0, temp_shb_ener_sf_fx );        */
    4416             :             /*                = pow(2, 3.321928*temp_shb_ener_sf_fx)    */
    4417         610 :             L_tmp = L_mult( temp_shb_ener_sf_fx, 27213 ); /* 3.321928 in Q13 -> L_tmp in Q12+Q13+1 = Q26 */
    4418         610 :             L_tmp = L_shl( L_tmp, -10 );                  /* bring L_tmp from Q26 to Q16 */
    4419         610 :             frac = L_Extract_lc( L_tmp, &exp );           /* Extract exponent */
    4420         610 :             L_tmp = Pow2( 14, frac );
    4421         610 :             *Q_shb_ener_sf = L_shl( L_tmp, sub( exp, 14 ) /* exp - 14 + 0 */ ); /* In Q_ener: 2*Q_shb+1, Q_shb = 0; */
    4422         610 :             move32();
    4423             : 
    4424        3660 :             FOR( i = 0; i < 5; i++ )
    4425             :             {
    4426        3050 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4427             :                 {
    4428        3050 :                     idx_res_gs[i] = hBWE_TD->idx_res_gs[i];
    4429        3050 :                     move16();
    4430             :                 }
    4431             :                 ELSE
    4432             :                 {
    4433           0 :                     idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
    4434           0 :                     move16();
    4435             :                 }
    4436        3050 :                 Q_shb_res_gshape[i] = usdequant_fx( idx_res_gs[i],
    4437             :                                                     2048 /*0.125f Q14*/, /*2048 = 0.125 in Q14 */
    4438             :                                                     1024 /*0.125f Q13*/  /*1024 = 0.125 in Q13 */
    4439             :                 );
    4440        3050 :                 move16();
    4441             :                 /* o: Q_shb_res_gshape in Q14 */
    4442             :             }
    4443             : 
    4444         610 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4445             :             {
    4446         610 :                 idx_mixFac = hBWE_TD->idx_mixFac;
    4447         610 :                 move16();
    4448             :             }
    4449             :             ELSE
    4450             :             {
    4451           0 :                 idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4452             :             }
    4453         610 :             *Q_mixFactors = usdequant_fx( idx_mixFac, 4096 /* 0.125 in Q15 */, 2048 /* 0.125 in Q14 */ );
    4454         610 :             move16();
    4455             :             /* o: Q_mixFactors in Q15 */
    4456             :         }
    4457             :         ELSE
    4458             :         {
    4459         748 :             *Q_shb_ener_sf = L_deposit_l( 0 );
    4460         748 :             move32();
    4461         748 :             *Q_mixFactors = 0;
    4462         748 :             move16();
    4463         748 :             set16_fx( Q_shb_res_gshape, 0, 5 );
    4464             :         }
    4465             : 
    4466             :         /* LSFs */
    4467             : 
    4468             : 
    4469        1358 :         test();
    4470        1358 :         test();
    4471        1358 :         test();
    4472        1358 :         test();
    4473        1358 :         test();
    4474        1358 :         IF( ( ( st_fx->rf_flag == 0 ) ) && !( ( EQ_32( st_fx->total_brate, ACELP_9k60 ) ) || ( ( ( st_fx->total_brate == 0 ) ) && ( ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) ) || ( EQ_32( st_fx->last_total_brate, ACELP_13k20 ) && EQ_16( st_fx->rf_flag_last, 1 ) ) ) ) ) )
    4475             : 
    4476             :         {
    4477             :             /* LSFs */
    4478        1358 :             test();
    4479        1358 :             test();
    4480        1358 :             test();
    4481        1358 :             IF( EQ_32( extl_brate, SWB_TBE_1k6 ) || EQ_32( extl_brate, FB_TBE_1k8 ) || EQ_32( extl_brate, SWB_TBE_2k8 ) || EQ_32( extl_brate, FB_TBE_3k0 ) )
    4482             :             {
    4483        1358 :                 IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4484             :                 {
    4485        3660 :                     FOR( i = 0; i < NUM_Q_LSF; i++ )
    4486             :                     {
    4487        3050 :                         lsf_idx[i] = hBWE_TD->lsf_idx[i];
    4488        3050 :                         move16();
    4489             :                     }
    4490             :                 }
    4491             :                 ELSE
    4492             :                 {
    4493        4488 :                     FOR( i = 0; i < NUM_Q_LSF; i++ )
    4494             :                     {
    4495        3740 :                         lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
    4496        3740 :                         move16();
    4497             :                     }
    4498             :                 }
    4499             :             }
    4500        1358 :             Dequant_lower_LSF_fx( lsf_idx, lsf_q );
    4501             : 
    4502        1358 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4503             :             {
    4504         610 :                 m_idx = hBWE_TD->m_idx;
    4505         610 :                 move16();
    4506             :             }
    4507             :             ELSE
    4508             :             {
    4509         748 :                 m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
    4510             :             }
    4511             : 
    4512        1358 :             Dequant_mirror_point_fx( lsf_q, m_idx, &m );
    4513             : 
    4514             :             /* safety check in case of bit errors */
    4515        1358 :             IF( GT_16( m, MAX_LSF_FX ) )
    4516             :             {
    4517           0 :                 st_fx->BER_detect = 1;
    4518           0 :                 move16();
    4519           0 :                 m = MAX_LSF_FX - 1;
    4520           0 :                 move16();
    4521             :             }
    4522             : 
    4523        1358 :             IF( EQ_16( st_fx->codec_mode, MODE2 ) )
    4524             :             {
    4525         610 :                 grid_idx = hBWE_TD->grid_idx;
    4526         610 :                 move16();
    4527             :             }
    4528             :             ELSE
    4529             :             {
    4530         748 :                 grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
    4531             :             }
    4532             : 
    4533        1358 :             Map_higher_LSF_fx( lsf_q, m, lsf_grid_fx[grid_idx] );
    4534             : 
    4535       14938 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    4536             :             {
    4537             :                 /* safety check in case of bit errors */
    4538       13580 :                 IF( GT_16( lsf_q[LPC_SHB_ORDER - 1 - i], MAX_LSF_FX ) )
    4539             :                 {
    4540           0 :                     st_fx->BER_detect = 1;
    4541           0 :                     move16();
    4542           0 :                     lsf_q[LPC_SHB_ORDER - 1 - i] = MAX_LSF_FX - 1;
    4543           0 :                     move16();
    4544             :                 }
    4545       13580 :                 Q_lsf[i] = sub( 16384, lsf_q[LPC_SHB_ORDER - 1 - i] );
    4546       13580 :                 move16();
    4547             :             }
    4548             :         }
    4549             :         ELSE
    4550             :         {
    4551           0 :             set16_fx( lsf_idx, 0, 5 );
    4552           0 :             Copy( hBWE_TD->lsf_idx, lsf_idx, 5 );
    4553           0 :             grid_idx = 0;
    4554           0 :             move16();
    4555           0 :             m_idx = 0;
    4556           0 :             move16();
    4557           0 :             Copy( swb_tbe_lsfvq_cbook_8b + i_mult( lsf_idx[0], LPC_SHB_ORDER ), Q_lsf, LPC_SHB_ORDER );
    4558             :         }
    4559             : 
    4560        1358 :         space_lsfs_fx( Q_lsf, LPC_SHB_ORDER );
    4561             : 
    4562             :         /* Dequantize subgain indices */
    4563        1358 :         j = i_mult( idxSubGain, NUM_SHB_SUBGAINS );
    4564        1358 :         move16();
    4565        6790 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    4566             :         {
    4567             :             /*    Q_subgain[i] = (float) pow(10.0, SHBCB_SubGain5bit[j++]); */
    4568             : 
    4569        5432 :             L_tmp = L_mult( SHBCB_SubGain5bit_fx[j], 27213 ); /*Q28  3.321928 in Q13 */
    4570        5432 :             j = add( j, 1 );
    4571        5432 :             L_tmp = L_shr( L_tmp, 12 );
    4572        5432 :             frac = L_Extract_lc( L_tmp, &exp );
    4573        5432 :             tmp = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
    4574             :             /* output of Pow2() will be: */
    4575             :             /* 16384 < Pow2() <= 32767 */
    4576        5432 :             Q_subgain[i] = shl( tmp, add( exp, 1 ) ); /*Q15*/
    4577        5432 :             move16();
    4578             :         }
    4579             : 
    4580       23086 :         FOR( i = NUM_SHB_SUBFR - 1; i >= 0; i-- )
    4581             :         {
    4582       21728 :             Word16 idx = 0;
    4583       21728 :             move16();
    4584       21728 :             IF( i != 0 )
    4585             :             {
    4586       20370 :                 idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
    4587             :             }
    4588       21728 :             Q_subgain[i] = Q_subgain[idx];
    4589       21728 :             move16();
    4590             :         }
    4591             : 
    4592             :         /* Frame gain */
    4593        1358 :         *Q_framegrain = L_mac( SHB_GAIN_QLOW_FX, idxFrameGain, SHB_GAIN_QDELTA_FX ); /*Q18*/
    4594        1358 :         move32();
    4595        1358 :         L_tmp = Mult_32_16( *Q_framegrain, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    4596        1358 :         frac = L_Extract_lc( L_tmp, &exp );
    4597        1358 :         L_tmp = Pow2( 30, frac );
    4598        1358 :         *Q_framegrain = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    4599        1358 :         move32();
    4600             :     }
    4601             : 
    4602        1358 :     return;
    4603             : }
    4604             : 
    4605             : /*-------------------------------------------------------------------*
    4606             :  * fb_tbe_dec()
    4607             :  *
    4608             :  * FB TBE decoder, 14(resp. 15.5) - 20 kHz band decoding module
    4609             :  *-------------------------------------------------------------------*/
    4610           0 : void fb_tbe_dec_fx(
    4611             :     Decoder_State *st,     /* i/o: encoder state structure                                */
    4612             :     const Word16 fb_exc[], /* i  : FB excitation from the SWB part      Q_fb_exc          */
    4613             :     Word16 Q_fb_exc,
    4614             :     Word16 *hb_synth, /* o  : high-band synthesis                    Q(15 - hb_synth_exp) */
    4615             :     Word16 hb_synth_exp )
    4616             : 
    4617             : {
    4618             :     Word16 i;
    4619           0 :     Word16 ratio = 0;
    4620           0 :     move16();
    4621           0 :     Word32 fb_exc_energy = 0;
    4622           0 :     move32();
    4623             :     Word16 fb_synth[L_FRAME48k];
    4624             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4625           0 :     hBWE_TD = st->hBWE_TD;
    4626             : 
    4627             :     /* decode FB slope information */
    4628           0 :     test();
    4629           0 :     test();
    4630           0 :     IF( EQ_16( st->extl, FB_TBE ) && !st->bfi )
    4631             :     {
    4632           0 :         IF( EQ_16( st->codec_mode, MODE2 ) )
    4633             :         {
    4634           0 :             i = hBWE_TD->idxGain;
    4635           0 :             move16();
    4636             :         }
    4637             :         ELSE
    4638             :         {
    4639           0 :             i = (Word16) get_next_indice_fx( st, 4 );
    4640             :         }
    4641           0 :         ratio = shl_sat( 1, i );
    4642             :     }
    4643           0 :     ELSE IF( EQ_16( st->extl, FB_TBE ) && st->bfi )
    4644             :     {
    4645           0 :         ratio = hBWE_TD->prev_fbbwe_ratio_fx;
    4646           0 :         move16();
    4647             :     }
    4648           0 :     fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k );
    4649             : 
    4650             :     /* FB TBE synthesis */
    4651           0 :     synthesise_fb_high_band_fx( fb_exc, Q_fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio_fx ), hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, hb_synth_exp
    4652             : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2
    4653             :                                 ,
    4654           0 :                                 st->element_mode
    4655             : #endif /*FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2*/
    4656             :     );
    4657             : 
    4658             :     /* add the fb_synth component to the hb_synth component */
    4659             :     /*  v_add_fx( hb_synth, fb_synth, hb_synth, L_FRAME48k );*/
    4660           0 :     FOR( i = 0; i < L_FRAME48k; i++ )
    4661             :     {
    4662           0 :         hb_synth[i] = add_sat( hb_synth[i], fb_synth[i] );
    4663           0 :         move16();
    4664             :     }
    4665           0 :     return;
    4666             : }
    4667             : 
    4668       39990 : void fb_tbe_dec_ivas_fx(
    4669             :     Decoder_State *st,     /* i/o: encoder state structure                 */
    4670             :     const Word16 fb_exc[], /* i  : FB excitation from the SWB part      Q_fb_exc   */
    4671             :     Word16 Q_fb_exc,
    4672             :     Word32 *hb_synth, /* o  : high-band synthesis                    Q(15 - hb_synth_exp) */
    4673             :     Word16 hb_synth_exp,
    4674             :     Word16 *fb_synth_ref, /*Q_fb_synth_ref*/
    4675             :     Word16 Q_fb_synth_ref,
    4676             :     Word16 output_frame )
    4677             : 
    4678             : {
    4679             :     Word16 i;
    4680       39990 :     Word16 ratio = 0;
    4681       39990 :     move16();
    4682       39990 :     Word32 fb_exc_energy = 0;
    4683       39990 :     move32();
    4684             :     Word16 fb_synth[L_FRAME48k];
    4685             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4686       39990 :     hBWE_TD = st->hBWE_TD;
    4687             : 
    4688             :     /* decode FB slope information */
    4689       39990 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    4690             :     {
    4691       34519 :         test();
    4692       34519 :         test();
    4693       34519 :         IF( EQ_16( st->extl, FB_TBE ) && !st->bfi )
    4694             :         {
    4695       33762 :             IF( EQ_16( st->codec_mode, MODE2 ) )
    4696             :             {
    4697           0 :                 i = hBWE_TD->idxGain;
    4698           0 :                 move16();
    4699             :             }
    4700             :             ELSE
    4701             :             {
    4702       33762 :                 i = (Word16) get_next_indice_fx( st, 4 );
    4703             :             }
    4704       33762 :             ratio = shl_sat( 1, i );
    4705             :         }
    4706         757 :         ELSE IF( EQ_16( st->extl, FB_TBE ) && st->bfi )
    4707             :         {
    4708         757 :             ratio = hBWE_TD->prev_fbbwe_ratio_fx;
    4709         757 :             move16();
    4710             :         }
    4711             :     }
    4712             :     ELSE
    4713             :     {
    4714        5471 :         IF( !st->bfi )
    4715             :         {
    4716        5371 :             st->next_bit_pos = add( st->next_bit_pos, NUM_BITS_FB_FRAMEGAIN );
    4717        5371 :             move16();
    4718             :         }
    4719             : 
    4720        5471 :         return;
    4721             :     }
    4722       34519 :     fb_exc_energy = sum2_fx_mod( fb_exc, L_FRAME16k );
    4723             : 
    4724             :     /* FB TBE synthesis */
    4725       34519 :     synthesise_fb_high_band_fx( fb_exc, Q_fb_exc, fb_synth, fb_exc_energy, ratio, st->L_frame, st->bfi, &( hBWE_TD->prev_fbbwe_ratio_fx ), hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, hb_synth_exp
    4726             : #ifdef FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2
    4727             :                                 ,
    4728       34519 :                                 st->element_mode
    4729             : #endif /*FIX_1439_SPEEDUP_SIMPLIFY_elliptic_bpf_48k_generic_STAGE2*/
    4730             :     );
    4731             : 
    4732       34519 :     test();
    4733       34519 :     IF( GE_16( st->element_mode, IVAS_CPE_DFT ) && ( st->idchan == 0 ) )
    4734             :     {
    4735        3935 :         Copy_Scale_sig( fb_synth, fb_synth_ref, L_FRAME48k, sub( Q_fb_synth_ref, add( hb_synth_exp, 16 ) ) ); // scaling is required
    4736             :     }
    4737             :     /* add the fb_synth component to the hb_synth component */
    4738             :     /*  v_add_fx( hb_synth, fb_synth, hb_synth, L_FRAME48k );*/
    4739    33172759 :     FOR( i = 0; i < L_FRAME48k; i++ )
    4740             :     {
    4741             :         // hb_synth[i] = L_add( hb_synth[i], L_deposit_l(fb_synth[i]));
    4742    33138240 :         hb_synth[i] = L_add( hb_synth[i], L_shl( fb_synth[i], Q11 ) ); // hb_synth_exp
    4743    33138240 :         move16();
    4744             :     }
    4745       34519 :     return;
    4746             : }
    4747             : 
    4748         610 : void tbe_read_bitstream_fx(
    4749             :     Decoder_State *st_fx )
    4750             : {
    4751             :     Word16 i;
    4752             :     TD_BWE_DEC_HANDLE hBWE_TD;
    4753         610 :     hBWE_TD = st_fx->hBWE_TD;
    4754             : 
    4755         610 :     test();
    4756         610 :     test();
    4757         610 :     test();
    4758         610 :     test();
    4759         610 :     test();
    4760         610 :     IF( ( EQ_16( st_fx->rf_flag, 1 ) || EQ_32( st_fx->total_brate, ACELP_9k60 ) ) && EQ_16( st_fx->bwidth, WB ) )
    4761             :     {
    4762             :         /* WB LSF */
    4763           0 :         hBWE_TD->lsf_WB = (Word16) get_next_indice_fx( st_fx, NUM_BITS_LBR_WB_LSF );
    4764           0 :         move16();
    4765             : 
    4766             :         /* WB frame gain */
    4767           0 :         hBWE_TD->gFrame_WB = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FrameGain_LBR_WB );
    4768           0 :         move16();
    4769             :     }
    4770         610 :     ELSE IF( ( GE_32( st_fx->total_brate, ACELP_9k60 ) ) && ( LE_32( st_fx->total_brate, ACELP_32k ) ) &&
    4771             :              ( ( EQ_16( st_fx->bwidth, SWB ) ) || ( EQ_16( st_fx->bwidth, FB ) ) ) )
    4772             :     {
    4773         610 :         test();
    4774         610 :         IF( ( ( st_fx->rf_flag == 0 ) ) && ( GT_32( st_fx->total_brate, ACELP_9k60 ) ) )
    4775             :         {
    4776        3660 :             FOR( i = 0; i < NUM_Q_LSF; i++ )
    4777             :             {
    4778        3050 :                 hBWE_TD->lsf_idx[i] = (Word16) get_next_indice_fx( st_fx, lsf_q_num_bits[i] );
    4779        3050 :                 move16();
    4780             :             }
    4781             : 
    4782         610 :             hBWE_TD->m_idx = (Word16) get_next_indice_fx( st_fx, MIRROR_POINT_BITS );
    4783         610 :             move16();
    4784         610 :             hBWE_TD->grid_idx = (Word16) get_next_indice_fx( st_fx, NUM_LSF_GRID_BITS );
    4785         610 :             move16();
    4786             :         }
    4787             :         ELSE
    4788             :         {
    4789           0 :             hBWE_TD->lsf_idx[0] = (Word16) get_next_indice_fx( st_fx, 8 );
    4790           0 :             move16();
    4791           0 :             hBWE_TD->m_idx = 0;
    4792           0 :             move16();
    4793           0 :             hBWE_TD->grid_idx = 0;
    4794           0 :             move16();
    4795             :         }
    4796             : 
    4797             :         /* shape gains */
    4798         610 :         hBWE_TD->idxSubGains = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_SUBGAINS );
    4799         610 :         move16();
    4800             : 
    4801             :         /* frame gain */
    4802         610 :         hBWE_TD->idxFrameGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_FRAMEGAIN );
    4803         610 :         move16();
    4804             : 
    4805         610 :         IF( GE_32( st_fx->total_brate, ACELP_24k40 ) )
    4806             :         {
    4807             :             /* sub frame energy*/
    4808         610 :             hBWE_TD->idx_shb_fr_gain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_ENER_SF );
    4809         610 :             move16();
    4810             : 
    4811             :             /* gain shapes residual */
    4812        3660 :             FOR( i = 0; i < NB_SUBFR16k; i++ )
    4813             :             {
    4814        3050 :                 hBWE_TD->idx_res_gs[i] = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_RES_GS );
    4815        3050 :                 move16();
    4816             :             }
    4817             : 
    4818             :             /* voicing factor */
    4819         610 :             hBWE_TD->idx_mixFac = (Word16) get_next_indice_fx( st_fx, NUM_BITS_SHB_VF );
    4820         610 :             move16();
    4821             :         }
    4822             : 
    4823         610 :         IF( EQ_16( st_fx->tec_tfa, 1 ) )
    4824             :         {
    4825         610 :             st_fx->tec_flag = (Word16) get_next_indice_fx( st_fx, BITS_TEC );
    4826         610 :             move16();
    4827         610 :             st_fx->tfa_flag = (Word16) get_next_indice_fx( st_fx, BITS_TFA );
    4828         610 :             move16();
    4829         610 :             test();
    4830         610 :             IF( st_fx->tfa_flag && st_fx->tec_flag )
    4831             :             {
    4832           5 :                 st_fx->tec_flag = 2;
    4833           5 :                 move16();
    4834           5 :                 st_fx->tfa_flag = 0;
    4835           5 :                 move16();
    4836             :             }
    4837             :         }
    4838             :         ELSE
    4839             :         {
    4840           0 :             st_fx->tec_flag = 0;
    4841           0 :             move16();
    4842           0 :             st_fx->tfa_flag = 0;
    4843           0 :             move16();
    4844             :         }
    4845             :     }
    4846             : 
    4847         610 :     IF( EQ_16( st_fx->bwidth, FB ) )
    4848             :     {
    4849           0 :         hBWE_TD->idxGain = (Word16) get_next_indice_fx( st_fx, 4 );
    4850           0 :         move16();
    4851             :     }
    4852         610 : }
    4853             : 
    4854             : 
    4855             : /*---------------------------------------------------------------------*
    4856             :  * GenTransition()
    4857             :  *
    4858             :  * Generate a highband transition signal from the gain shape overlap
    4859             :  * buffer to fill the gap caused by the delay alignment buffer when
    4860             :  * switching from TBE to IGF
    4861             :  *---------------------------------------------------------------------*/
    4862             : 
    4863          23 : void GenTransition_fx(
    4864             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                                        */
    4865             :     Word16 *output_HB,         /* o  : synthesized HB transitions signal  st_fx->prev_Q_bwe_syn2 */
    4866             :     const Word32 output_Fs,    /* i  : output sampling rate                                      */
    4867             :     Word16 rf_flag,            /* i  : RF flag                                                   */
    4868             :     Word32 total_bitrate       /* i  : total bitrate                                             */
    4869             : )
    4870             : {
    4871             :     Word16 i, length;
    4872             :     Word16 syn_overlap_32k[L_FRAME32k];
    4873             :     Word32 L_tmp;
    4874          23 :     Word16 ol_len = 2 * SHB_OVERLAP_LEN;
    4875             : 
    4876             :     /* set targeted length of transition signal */
    4877          23 :     length = shl( NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ), 1 );
    4878             : 
    4879             :     /* upsample overlap snippet */
    4880          23 :     Interpolate_allpass_steep_fx( hBWE_TD->syn_overlap_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, SHB_OVERLAP_LEN, syn_overlap_32k );
    4881             : 
    4882             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    4883          23 :     test();
    4884          23 :     IF( ( rf_flag != 0 ) || EQ_32( total_bitrate, ACELP_9k60 ) )
    4885             :     {
    4886           0 :         flip_and_downmix_generic_fx( syn_overlap_32k, syn_overlap_32k, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem_fx,
    4887           0 :                                      hBWE_TD->genSHBsynth_Hilbert_Mem_fx + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ),
    4888             :                                      &( hBWE_TD->syn_dm_phase ) );
    4889             :     }
    4890             :     ELSE
    4891             :     {
    4892         483 :         FOR( i = 0; i < ol_len; i = i + 2 )
    4893             :         {
    4894         460 :             syn_overlap_32k[i] = negate( syn_overlap_32k[i] );
    4895         460 :             move16();
    4896         460 :             syn_overlap_32k[i + 1] = syn_overlap_32k[i + 1];
    4897         460 :             move16();
    4898             :         }
    4899             :     }
    4900             : 
    4901             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    4902         943 :     FOR( i = 0; i < ol_len; i++ )
    4903             :     {
    4904         920 :         L_tmp = L_mult( window_shb_32k_fx[i], hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i] );
    4905         920 :         output_HB[i] = mac_r( L_tmp, window_shb_32k_fx[2 * L_SHB_LAHEAD - 1 - i], syn_overlap_32k[i] );
    4906         920 :         move16();
    4907             :     }
    4908             : 
    4909             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    4910        4209 :     FOR( ; i < length; i++ )
    4911             :     {
    4912        4186 :         output_HB[i] = hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i];
    4913        4186 :         move16();
    4914             :     }
    4915             : 
    4916          23 :     IF( EQ_32( output_Fs, 48000 ) )
    4917             :     {
    4918          23 :         interpolate_3_over_2_allpass_fx( output_HB, length, output_HB, hBWE_TD->int_3_over_2_tbemem_dec_fx, allpass_poles_3_ov_2 );
    4919             :     }
    4920           0 :     ELSE IF( EQ_32( output_Fs, 16000 ) )
    4921             :     {
    4922           0 :         Decimate_allpass_steep_fx( output_HB, hBWE_TD->mem_resamp_HB_32k_fx, L_FRAME32k, output_HB );
    4923             :     }
    4924             : 
    4925          23 :     return;
    4926             : }
    4927             : 
    4928             : /* IVAS 32-bit variant */
    4929        5722 : void GenTransition_fx32(
    4930             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                      */
    4931             :     Word32 *outputHB_fx,       /* o  : synthesized HB transitions signal : Q11 */
    4932             :     const Word32 output_Fs,    /* i  : output sampling rate              : Q0  */
    4933             :     const Word16 L_frame,      /* i  : ACELP frame length                : Q0  */
    4934             :     const Word16 prev_Qx )
    4935             : {
    4936             :     Word16 i, length;
    4937             : 
    4938             :     Word32 syn_overlap_32k_fx[2 * SHB_OVERLAP_LEN];
    4939             : 
    4940             :     /* set targeted length of transition signal */
    4941        5722 :     length = i_mult( 2, NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ) );
    4942             : 
    4943             :     /* upsample overlap snippet */
    4944        5722 :     Interpolate_allpass_steep_fx32( hBWE_TD->syn_overlap_fx_32, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, SHB_OVERLAP_LEN, syn_overlap_32k_fx );
    4945             : 
    4946             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    4947        5722 :     test();
    4948        5722 :     IF( EQ_16( L_frame, L_FRAME ) )
    4949             :     {
    4950        1953 :         flip_and_downmix_generic_fx32( syn_overlap_32k_fx, syn_overlap_32k_fx, 2 * SHB_OVERLAP_LEN, hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + HILBERT_ORDER1, hBWE_TD->genSHBsynth_Hilbert_Mem_fx + ( HILBERT_ORDER1 + 2 * HILBERT_ORDER2 ), &( hBWE_TD->syn_dm_phase ) );
    4951             :     }
    4952             :     ELSE
    4953             :     {
    4954      154529 :         FOR( i = 0; i < 2 * SHB_OVERLAP_LEN; i++ )
    4955             :         {
    4956      150760 :             IF( i % 2 == 0 )
    4957             :             {
    4958       75380 :                 syn_overlap_32k_fx[i] = L_negate( syn_overlap_32k_fx[i] );
    4959             :             }
    4960             :             ELSE
    4961             :             {
    4962       75380 :                 syn_overlap_32k_fx[i] = syn_overlap_32k_fx[i];
    4963             :             }
    4964      150760 :             move32();
    4965             :         }
    4966             :     }
    4967             : 
    4968             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    4969      234602 :     FOR( i = 0; i < 2 * L_SHB_LAHEAD; i++ )
    4970             :     {
    4971      228880 :         outputHB_fx[i] = L_add_sat( Mpy_32_16_1( hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i], window_shb_32k_fx[i] ), Mpy_32_16_1( syn_overlap_32k_fx[i], window_shb_32k_fx[2 * L_SHB_LAHEAD - 1 - i] ) );
    4972      228880 :         move32();
    4973             :     }
    4974             : 
    4975             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    4976      922806 :     FOR( ; i < length; i++ )
    4977             :     {
    4978      917084 :         outputHB_fx[i] = L_shl( hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i], sub( Q11, prev_Qx ) );
    4979      917084 :         move32();
    4980             :     }
    4981             : 
    4982        5722 :     IF( EQ_32( output_Fs, 48000 ) )
    4983             :     {
    4984        4170 :         interpolate_3_over_2_allpass_fx32( outputHB_fx, length, outputHB_fx, hBWE_TD->int_3_over_2_tbemem_dec_fx_32 );
    4985             :     }
    4986        1552 :     ELSE IF( EQ_32( output_Fs, 16000 ) )
    4987             :     {
    4988         128 :         Decimate_allpass_steep_fx32( outputHB_fx, hBWE_TD->mem_resamp_HB_32k_fx_32, L_FRAME32k, outputHB_fx );
    4989             :     }
    4990             : 
    4991        5722 :     return;
    4992             : }
    4993             : 
    4994             : 
    4995             : /*---------------------------------------------------------------------*
    4996             :  * GenTransition_WB()
    4997             :  *
    4998             :  *---------------------------------------------------------------------*/
    4999             : 
    5000           0 : void GenTransition_WB_fx(
    5001             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                   */
    5002             :     Word16 *output,            /* o  : synthesized transitions signal       */
    5003             :     const Word32 output_Fs     /* i  : output sampling rate                 */
    5004             : )
    5005             : {
    5006             :     Word16 i, length;
    5007             :     Word32 L_tmp;
    5008             :     Word16 speech_buf_16k1[L_FRAME16k], speech_buf_16k2[L_FRAME16k];
    5009             :     Word16 upsampled_synth[L_FRAME48k];
    5010             :     Word16 input_scaled[SHB_OVERLAP_LEN / 2];
    5011             : 
    5012             :     /* set targeted length of transition signal */
    5013           0 :     length = shl( NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ), 1 );
    5014             : 
    5015             :     /* upsample overlap snippet */
    5016           0 :     Copy_Scale_sig( hBWE_TD->syn_overlap_fx, input_scaled, SHB_OVERLAP_LEN / 2, hBWE_TD->prev_Qx );
    5017           0 :     Interpolate_allpass_steep_fx( input_scaled, hBWE_TD->state_lsyn_filt_shb_fx, SHB_OVERLAP_LEN / 2, speech_buf_16k1 );
    5018           0 :     Interpolate_allpass_steep_fx( speech_buf_16k1, hBWE_TD->state_lsyn_filt_dwn_shb_fx, SHB_OVERLAP_LEN, speech_buf_16k2 );
    5019             : 
    5020             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    5021           0 :     FOR( i = 0; i < SHB_OVERLAP_LEN; i += 2 )
    5022             :     {
    5023           0 :         speech_buf_16k2[i] = negate( speech_buf_16k2[i] );
    5024           0 :         speech_buf_16k2[i + 1] = speech_buf_16k2[i + 1];
    5025             :     }
    5026             : 
    5027             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    5028           0 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5029             :     {
    5030           0 :         L_tmp = L_mult( window_shb_fx[i], hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i] );
    5031           0 :         output[i] = mac_r( L_tmp, window_shb_fx[L_SHB_LAHEAD - 1 - i], speech_buf_16k2[i] );
    5032           0 :         move16();
    5033           0 :         output[i] = mult_r( output[i], 21299 );
    5034           0 :         move16();
    5035             :     }
    5036             : 
    5037             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    5038           0 :     FOR( ; i < length; i++ )
    5039             :     {
    5040           0 :         output[i] = mult_r( hBWE_TD->old_tbe_synth_fx[L_SHB_TRANSITION_LENGTH - 1 - i], 21299 );
    5041           0 :         move16();
    5042             :     }
    5043             : 
    5044             :     /* upsampling if necessary */
    5045           0 :     IF( EQ_32( output_Fs, 32000 ) )
    5046             :     {
    5047           0 :         Interpolate_allpass_steep_fx( output, hBWE_TD->mem_resamp_HB_fx, L_FRAME16k, upsampled_synth );
    5048           0 :         Copy( upsampled_synth, output, L_FRAME32k );
    5049             :     }
    5050           0 :     ELSE IF( EQ_32( output_Fs, 48000 ) )
    5051             :     {
    5052           0 :         interpolate_3_over_1_allpass_fx( output, L_FRAME16k, upsampled_synth, hBWE_TD->mem_resamp_HB_fx );
    5053           0 :         Copy( upsampled_synth, output, L_FRAME48k );
    5054             :     }
    5055             : 
    5056           0 :     return;
    5057             : }
    5058             : 
    5059             : 
    5060             : /* IVAS 32-bit variant */
    5061         116 : void GenTransition_WB_fx32(
    5062             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle                       */
    5063             :     Word32 *outputHB_fx,       /* o  : synthesized HB transitions signal  : Q11 */
    5064             :     const Word32 output_Fs     /* i  : output sampling rate                     */
    5065             : )
    5066             : {
    5067             :     Word16 i, length;
    5068             :     Word32 speech_buf_16k1_fx[SHB_OVERLAP_LEN], speech_buf_16k2_fx[2 * SHB_OVERLAP_LEN];
    5069             :     Word32 upsampled_synth_fx[L_FRAME48k];
    5070             : 
    5071             :     /* set targeted length of transition signal */
    5072         116 :     length = i_mult( 2, NS2SA_FX2( output_Fs, DELAY_BWE_TOTAL_NS ) );
    5073             : 
    5074             :     /* upsample overlap snippet */
    5075         116 :     Interpolate_allpass_steep_fx32( hBWE_TD->syn_overlap_fx_32, hBWE_TD->state_lsyn_filt_shb_fx_32, SHB_OVERLAP_LEN / 2, speech_buf_16k1_fx );
    5076         116 :     Interpolate_allpass_steep_fx32( speech_buf_16k1_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx_32, SHB_OVERLAP_LEN, speech_buf_16k2_fx );
    5077             : 
    5078             :     /* perform spectral flip and downmix with overlap snippet to match HB synth  */
    5079        2436 :     FOR( i = 0; i < SHB_OVERLAP_LEN; i++ )
    5080             :     {
    5081        2320 :         IF( i % 2 == 0 )
    5082             :         {
    5083        1160 :             speech_buf_16k2_fx[i] = L_negate( speech_buf_16k2_fx[i] );
    5084        1160 :             move32();
    5085             :         }
    5086             :         ELSE
    5087             :         {
    5088        1160 :             speech_buf_16k2_fx[i] = speech_buf_16k2_fx[i];
    5089        1160 :             move32();
    5090             :         }
    5091             :     }
    5092             : 
    5093             :     /* cross fade of overlap snippet and mirrored HB synth from previous frame */
    5094        2436 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5095             :     {
    5096        2320 :         outputHB_fx[i] = L_add( Mpy_32_16_1( hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i], window_shb_fx[i] ), Mpy_32_16_1( speech_buf_16k2_fx[i], window_shb_fx[L_SHB_LAHEAD - 1 - i] ) );
    5097        2320 :         move32();
    5098        2320 :         outputHB_fx[i] = Mpy_32_16_1( outputHB_fx[i], 21299 );
    5099        2320 :         move32();
    5100             :     }
    5101             : 
    5102             :     /* fill transition signal with mirrored HB synth from previous frame to fully fill delay alignment buffer gap */
    5103       13484 :     FOR( ; i < length; i++ )
    5104             :     {
    5105       13368 :         outputHB_fx[i] = hBWE_TD->old_tbe_synth_fx_32[L_SHB_TRANSITION_LENGTH - 1 - i];
    5106       13368 :         move32();
    5107       13368 :         outputHB_fx[i] = Mpy_32_16_1( outputHB_fx[i], 21299 );
    5108       13368 :         move32();
    5109             :     }
    5110             : 
    5111             :     /* upsampling if necessary */
    5112         116 :     IF( EQ_32( output_Fs, 32000 ) )
    5113             :     {
    5114          10 :         Interpolate_allpass_steep_fx32( outputHB_fx, hBWE_TD->mem_resamp_HB_fx_32, L_FRAME16k, upsampled_synth_fx );
    5115          10 :         Copy32( upsampled_synth_fx, outputHB_fx, L_FRAME32k );
    5116             :     }
    5117         106 :     ELSE IF( EQ_32( output_Fs, 48000 ) )
    5118             :     {
    5119          43 :         interpolate_3_over_1_allpass_fx32( outputHB_fx, L_FRAME16k, upsampled_synth_fx, hBWE_TD->mem_resamp_HB_fx_32 );
    5120          43 :         Copy32( upsampled_synth_fx, outputHB_fx, L_FRAME48k );
    5121             :     }
    5122             : 
    5123         116 :     return;
    5124             : }
    5125             : 
    5126             : 
    5127             : /*---------------------------------------------------------------------*
    5128             :  * TBEreset_dec()
    5129             :  *
    5130             :  *---------------------------------------------------------------------*/
    5131             : 
    5132      223956 : void TBEreset_dec_fx(
    5133             :     Decoder_State *st_fx /* i/o: decoder state structure                 */
    5134             : )
    5135             : {
    5136             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5137      223956 :     hBWE_TD = st_fx->hBWE_TD;
    5138             : 
    5139      223956 :     IF( NE_16( st_fx->last_core, ACELP_CORE ) )
    5140             :     {
    5141      222179 :         set16_fx( hBWE_TD->old_bwe_exc_fx, 0, PIT16k_MAX * 2 );
    5142      222179 :         hBWE_TD->bwe_non_lin_prev_scale_fx = L_deposit_l( 0 );
    5143      222179 :         move32();
    5144      222179 :         st_fx->prev_Q_bwe_exc = 31;
    5145      222179 :         move16();
    5146             :     }
    5147             : 
    5148      223956 :     test();
    5149      223956 :     IF( EQ_16( st_fx->bwidth, WB ) )
    5150             :     {
    5151        9645 :         wb_tbe_extras_reset_fx( hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx );
    5152        9645 :         wb_tbe_extras_reset_synth_fx( hBWE_TD->state_lsyn_filt_shb_fx, hBWE_TD->state_lsyn_filt_dwn_shb_fx, hBWE_TD->state_32and48k_WB_upsample_fx, hBWE_TD->mem_resamp_HB_fx );
    5153             : 
    5154        9645 :         set16_fx( hBWE_TD->mem_genSHBexc_filt_down_shb_fx, 0, 7 );
    5155        9645 :         set16_fx( hBWE_TD->state_lpc_syn_fx, 0, 10 );
    5156        9645 :         set16_fx( hBWE_TD->state_syn_shbexc_fx, 0, L_SHB_LAHEAD / 4 );
    5157        9645 :         set16_fx( hBWE_TD->syn_overlap_fx, 0, L_SHB_LAHEAD );
    5158        9645 :         set32_fx( hBWE_TD->syn_overlap_fx_32, 0, L_SHB_LAHEAD );
    5159        9645 :         set32_fx( hBWE_TD->mem_csfilt_fx, 0, 2 );
    5160             :     }
    5161      214311 :     ELSE IF( EQ_16( st_fx->bwidth, SWB ) || EQ_16( st_fx->bwidth, FB ) )
    5162             :     {
    5163      214311 :         swb_tbe_reset_fx( hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    5164      214311 :                           hBWE_TD->syn_overlap_fx, hBWE_TD->state_syn_shbexc_fx, &( hBWE_TD->tbe_demph_fx ),
    5165      214311 :                           &( hBWE_TD->tbe_premph_fx ), hBWE_TD->mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ) );
    5166             : 
    5167      214311 :         swb_tbe_reset_synth_fx( hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32 );
    5168             : 
    5169      214311 :         set16_fx( hBWE_TD->GainShape_Delay_fx, 0, NUM_SHB_SUBFR / 2 );
    5170      214311 :         set16_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx, 0, INTERP_3_2_MEM_LEN );
    5171      214311 :         set32_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx_32, 0, INTERP_3_2_MEM_LEN );
    5172      214311 :         set16_fx( hBWE_TD->mem_resamp_HB_32k_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5173      214311 :         set32_fx( hBWE_TD->mem_resamp_HB_32k_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5174      214311 :         hBWE_TD->prev_pow_exc16kWhtnd_fx32 = 1; /* Q0 1.f */
    5175      214311 :         move32();
    5176      214311 :         hBWE_TD->prev_mix_factor_fx = 32767; /* Q15 1.f */
    5177      214311 :         move16();
    5178             : 
    5179      214311 :         IF( EQ_16( st_fx->bwidth, FB ) )
    5180             :         {
    5181      153518 :             if ( st_fx->hBWE_FD != NULL )
    5182             :             {
    5183      153518 :                 st_fx->hBWE_FD->prev_fb_ener_adjust_fx = 0;
    5184      153518 :                 move16();
    5185             :             }
    5186      153518 :             set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
    5187      153518 :             hBWE_TD->fb_tbe_demph_fx = 0;
    5188      153518 :             move16();
    5189      153518 :             fb_tbe_reset_synth_fx( hBWE_TD->fbbwe_hpf_mem_fx, hBWE_TD->fbbwe_hpf_mem_fx_Q, &hBWE_TD->prev_fbbwe_ratio_fx );
    5190             :         }
    5191             :     }
    5192             : 
    5193      223956 :     return;
    5194             : }
    5195             : 
    5196             : 
    5197             : /*-------------------------------------------------------------------*
    5198             :  * td_bwe_dec_init()
    5199             :  *
    5200             :  * Initialize TD BWE state structure at the decoder
    5201             :  *-------------------------------------------------------------------*/
    5202             : 
    5203        3085 : void td_bwe_dec_init_fx(
    5204             :     TD_BWE_DEC_HANDLE hBWE_TD, /* i/o: TD BWE data handle      */
    5205             :     const Word16 extl,         /* i  :  BWE extension layer    */
    5206             :     const Word32 output_Fs     /* i  : output sampling rate    */
    5207             : )
    5208             : {
    5209             :     Word16 i;
    5210             : 
    5211             :     /* init. SHB buffers */;
    5212        3085 :     InitSWBdecBuffer_fx( hBWE_TD );
    5213             : 
    5214             :     /* reset SHB buffers */
    5215        3085 :     ResetSHBbuffer_Dec_fx( hBWE_TD, extl );
    5216             : 
    5217        3085 :     IF( EQ_32( output_Fs, 48000 ) )
    5218             :     {
    5219        2245 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[0], 0, 4 );
    5220        2245 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[1], 0, 4 );
    5221        2245 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[2], 0, 4 );
    5222        2245 :         set32_fx( hBWE_TD->fbbwe_hpf_mem_fx[3], 0, 4 );
    5223        2245 :         set16_fx( hBWE_TD->fbbwe_hpf_mem_fx_Q, 0, 4 );
    5224             :     }
    5225             : 
    5226        3085 :     set16_fx( hBWE_TD->mem_resamp_HB_fx, 0, INTERP_3_1_MEM_LEN );
    5227        3085 :     set32_fx( hBWE_TD->mem_resamp_HB_fx_32, 0, INTERP_3_1_MEM_LEN );
    5228        3085 :     set16_fx( hBWE_TD->mem_resamp_HB_32k_fx, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5229        3085 :     set32_fx( hBWE_TD->mem_resamp_HB_32k_fx_32, 0, 2 * ALLPASSSECTIONS_STEEP + 1 );
    5230             : 
    5231        3085 :     hBWE_TD->tilt_mem_fx = 0;
    5232        3085 :     move16();
    5233        3085 :     set16_fx( hBWE_TD->prev_lsf_diff_fx, 16384 /*0.5f in Q15*/, LPC_SHB_ORDER - 2 );
    5234        3085 :     hBWE_TD->prev_tilt_para_fx = 0;
    5235        3085 :     move16();
    5236        3085 :     set16_fx( hBWE_TD->cur_sub_Aq_fx, 0, M + 1 );
    5237        3085 :     set16_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx, 0, INTERP_3_2_MEM_LEN );
    5238        3085 :     set32_fx( hBWE_TD->int_3_over_2_tbemem_dec_fx_32, 0, INTERP_3_2_MEM_LEN );
    5239             : 
    5240             :     /* TD BWE post-processing */
    5241        3085 :     hBWE_TD->ptr_mem_stp_swb_fx = hBWE_TD->mem_stp_swb_fx + LPC_SHB_ORDER - 1;
    5242        3085 :     set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
    5243             : 
    5244       33935 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5245             :     {
    5246       30850 :         hBWE_TD->swb_lsp_prev_interp_fx[i] = swb_lsp_prev_interp_init[i];
    5247       30850 :         move16();
    5248             :     }
    5249             : 
    5250        3085 :     hBWE_TD->prev1_shb_ener_sf_fx = 32767; /* Q15*/
    5251        3085 :     move16();
    5252        3085 :     hBWE_TD->prev2_shb_ener_sf_fx = 32767; /* Q15*/
    5253        3085 :     move16();
    5254        3085 :     hBWE_TD->prev3_shb_ener_sf_fx = 32767; /* Q15*/
    5255        3085 :     move16();
    5256        3085 :     hBWE_TD->prev_res_shb_gshape_fx = 8192; /* 0.125 in Q14*/
    5257        3085 :     move16();
    5258        3085 :     hBWE_TD->prev_mixFactors_fx = 16384; /* 0.5 in Q15*/
    5259        3085 :     move16();
    5260        3085 :     hBWE_TD->prev_GainShape_fx = 0;
    5261        3085 :     move16();
    5262        3085 :     hBWE_TD->prev_Q_bwe_exc_fb = 51;
    5263        3085 :     move16();
    5264        3085 :     set16_fx( hBWE_TD->fb_state_lpc_syn_fx, 0, LPC_SHB_ORDER );
    5265        3085 :     hBWE_TD->fb_tbe_demph_fx = 0;
    5266        3085 :     move16();
    5267             : 
    5268        3085 :     set16_fx( hBWE_TD->old_hb_synth_fx, 0, L_FRAME48k );
    5269             : 
    5270        3085 :     hBWE_TD->prev_ener_fx = L_deposit_l( 0 );
    5271        3085 :     move32();
    5272             : 
    5273        3085 :     set16_fx( hBWE_TD->prev_lpc_wb_fx, 0, LPC_SHB_ORDER_WB );
    5274             : 
    5275        3085 :     return;
    5276             : }
    5277             : 
    5278             : /*-------------------------------------------------------------------*
    5279             :  * ResetSHBbuffer_Dec()
    5280             :  *
    5281             :  *
    5282             :  *-------------------------------------------------------------------*/
    5283             : 
    5284      102554 : static void calc_tilt_bwe_fx_loc(
    5285             :     const Word32 *sp_fx, /* i  : input signal   : Q11       */
    5286             :     Word32 *tilt_fx,     /* o  : signal tilt    : tilt_fx_q */
    5287             :     Word16 *tilt_fx_q,   /* o  : signal tilt                */
    5288             :     const Word16 N       /* i  : signal length  : Q0        */
    5289             : )
    5290             : {
    5291             :     Word16 i;
    5292             :     Word64 r0_fx, r1_fx;
    5293             : 
    5294      102554 :     r0_fx = EPSILON_FX_SMALL;
    5295      102554 :     move64();
    5296    26356378 :     FOR( i = 0; i < N; i++ )
    5297             :     {
    5298    26253824 :         r0_fx = W_add( r0_fx, W_shr( W_mult_32_32( sp_fx[i], sp_fx[i] ), 1 ) ); /*Q11*2 - 1*/
    5299             :     }
    5300      102554 :     r1_fx = W_deposit32_l( abs( L_sub( sp_fx[1], sp_fx[0] ) ) ); /*Q11*/
    5301    26151270 :     FOR( i = 2; i < N; i++ )
    5302             :     {
    5303    26048716 :         IF( W_mult_32_32( L_sub( sp_fx[i], sp_fx[i - 1] ) /*Q11*/, L_sub( sp_fx[i - 1], sp_fx[i - 2] ) /*Q11*/ ) /*Q11 * 2 - 1*/ < 0 )
    5304             :         {
    5305     6410255 :             r1_fx = W_add( r1_fx, W_deposit32_l( abs( L_sub( sp_fx[i], sp_fx[i - 1] ) ) ) ); /*Q11*/
    5306             :         }
    5307             :     }
    5308      102554 :     Word16 headroom_left_r0 = W_norm( r0_fx );
    5309      102554 :     Word16 headroom_left_r1 = W_norm( r1_fx );
    5310      102554 :     Word16 r0_q = 0, r1_q = 0;
    5311      102554 :     move16();
    5312      102554 :     move16();
    5313      102554 :     IF( LT_16( headroom_left_r0, 32 ) )
    5314             :     {
    5315      102371 :         r0_fx = W_shr( r0_fx, sub( 32, headroom_left_r0 ) );
    5316      102371 :         r0_q = sub( 31, sub( ( 2 * OUTPUT_Q ), sub( 32, headroom_left_r0 ) ) );
    5317             :     }
    5318             :     ELSE
    5319             :     {
    5320         183 :         r0_q = 31 - ( 2 * OUTPUT_Q );
    5321         183 :         move16();
    5322             :     }
    5323             : 
    5324      102554 :     IF( LT_16( headroom_left_r1, 32 ) )
    5325             :     {
    5326         785 :         r1_fx = W_shr( r1_fx, sub( 32, headroom_left_r1 ) );
    5327         785 :         r1_q = sub( OUTPUT_Q, sub( 32, headroom_left_r1 ) );
    5328             :     }
    5329             :     ELSE
    5330             :     {
    5331      101769 :         r1_q = OUTPUT_Q;
    5332      101769 :         move16();
    5333             :     }
    5334      102554 :     Word32 temp_r0_inv = ISqrt32( W_extract_l( r0_fx ), &r0_q );
    5335      102554 :     Word32 res = Mpy_32_32( W_extract_l( r1_fx ), temp_r0_inv );
    5336             :     // Word16 res_q = r1_q + ( r0_q < 0 ? ( 31 + ( -1 * r0_q ) ) : r0_q ) - 31;
    5337             :     Word16 res_q;
    5338      102554 :     IF( r0_q < 0 )
    5339             :     {
    5340      102484 :         res_q = add( r1_q, sub( add( 31, -r0_q ), 31 ) );
    5341             :     }
    5342             :     ELSE
    5343             :     {
    5344          70 :         res_q = add( r1_q, sub( r0_q, 31 ) );
    5345             :     }
    5346      102554 :     Word16 norm_res = norm_l( res );
    5347      102554 :     IF( norm_res > 0 )
    5348             :     {
    5349      101763 :         *tilt_fx = L_shl_sat( res, norm_res );
    5350      101763 :         move32();
    5351      101763 :         *tilt_fx_q = add( res_q, norm_res );
    5352      101763 :         move16();
    5353             :     }
    5354             :     ELSE
    5355             :     {
    5356         791 :         *tilt_fx = res;
    5357         791 :         move32();
    5358         791 :         *tilt_fx_q = res_q;
    5359         791 :         move16();
    5360             :     }
    5361      102554 :     return;
    5362             : }
    5363             : 
    5364             : /*-------------------------------------------------------------------*
    5365             :  * swb_tbe_dec()
    5366             :  *
    5367             :  * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
    5368             :  *-------------------------------------------------------------------*/
    5369       43172 : static void rescale_genSHB_mem_dec_ivas(
    5370             :     Decoder_State *st_fx,
    5371             :     Word16 sf /*Q0*/
    5372             : )
    5373             : {
    5374             :     Word16 i;
    5375             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5376       43172 :     hBWE_TD = st_fx->hBWE_TD;
    5377             : 
    5378      561236 :     FOR( i = 0; i < NL_BUFF_OFFSET; i++ )
    5379             :     {
    5380      518064 :         hBWE_TD->old_bwe_exc_extended_fx[i] = shl( hBWE_TD->old_bwe_exc_extended_fx[i], sf );
    5381      518064 :         move16();
    5382             :     }
    5383             : 
    5384      345376 :     FOR( i = 0; i < 7; i++ )
    5385             :     {
    5386      302204 :         hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i] = shl( hBWE_TD->mem_genSHBexc_filt_down_shb_fx[i], sf );
    5387      302204 :         move16();
    5388             :     }
    5389             : 
    5390             :     /*  -- Apply memory scaling for 13.2 and 16.4k bps using sf ----*/
    5391       43172 :     IF( LT_32( st_fx->total_brate, ACELP_24k40 ) )
    5392             :     {
    5393      386331 :         FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5394             :         {
    5395      351210 :             hBWE_TD->state_lpc_syn_fx[i] = shl_sat( hBWE_TD->state_lpc_syn_fx[i], sf );
    5396      351210 :             move16();
    5397             :         }
    5398             : 
    5399      737541 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5400             :         {
    5401      702420 :             hBWE_TD->state_syn_shbexc_fx[i] = shl_sat( hBWE_TD->state_syn_shbexc_fx[i], sf );
    5402      702420 :             move16();
    5403             :         }
    5404             :     }
    5405             : 
    5406       43172 :     hBWE_TD->mem_csfilt_fx[0] = L_shl( hBWE_TD->mem_csfilt_fx[0], sf );
    5407       43172 :     move32();
    5408             : 
    5409       43172 :     hBWE_TD->tbe_demph_fx = shl_r( hBWE_TD->tbe_demph_fx, sf );
    5410       43172 :     move16();
    5411       43172 :     hBWE_TD->tbe_premph_fx = shl_r( hBWE_TD->tbe_premph_fx, sf );
    5412       43172 :     move16();
    5413             : 
    5414       43172 :     return;
    5415             : }
    5416             : 
    5417      102554 : static void find_max_mem_dec_m3(
    5418             :     Decoder_State *st,
    5419             :     Word16 *n_mem3 )
    5420             : {
    5421             :     Word16 i;
    5422             :     // Word16 n_mem_32;
    5423             :     Word16 tempQ15;
    5424             :     Word16 max3;
    5425             :     // Word32 tempQ32, Lmax3;
    5426             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5427      102554 :     hBWE_TD = st->hBWE_TD;
    5428             : 
    5429             :     /* --------------------------------------------------------------*/
    5430             :     /* Find headroom for synthesis stage associated with these memories:
    5431             :           1. st->syn_overlap_fx*/
    5432      102554 :     max3 = 0;
    5433      102554 :     move16();
    5434             :     /* find max in prev overlapSyn */
    5435     2153634 :     FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    5436             :     {
    5437     2051080 :         tempQ15 = abs_s( hBWE_TD->syn_overlap_fx[i] );
    5438     2051080 :         max3 = s_max( max3, tempQ15 );
    5439             :     }
    5440             :     /* find max in prev genSHBsynth_state_lsyn_filt_shb_local_fx */
    5441             : 
    5442             :     /* estimate the norm for 16-bit memories  */
    5443      102554 :     *n_mem3 = norm_s( max3 );
    5444      102554 :     move16();
    5445      102554 :     if ( max3 == 0 )
    5446             :     {
    5447        8615 :         *n_mem3 = 15;
    5448        8615 :         move16();
    5449             :     }
    5450      102554 : }
    5451             : 
    5452             : /*-------------------------------------------------------------------*
    5453             :  * ivas_swb_tbe_dec_fx()
    5454             :  *
    5455             :  * SWB TBE decoder, 6 - 14 kHz (or 7.5 - 15.5 kHz) band decoding module
    5456             :  *-------------------------------------------------------------------*/
    5457      102554 : void ivas_swb_tbe_dec_fx(
    5458             :     Decoder_State *st,                    /* i/o: decoder state structure                           */
    5459             :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE, /* i/o: IC-BWE state structure                            */
    5460             :     const Word32 *bwe_exc_extended_fx,    /* i  : bandwidth extended excitation : Q_exc             */
    5461             :     Word16 Q_exc,
    5462             :     const Word16 voice_factors_fx[],    /* i  : voicing factors  : Q15                              */
    5463             :     const Word32 old_syn_12k8_16k_fx[], /* i  : low band synthesis : old_syn_fx                     */
    5464             :     Word16 *White_exc16k_fx,            /* o  : shaped white excitation for the FB TBE : Q_white_exc*/
    5465             :     Word32 *synth_fx,                   /* o  : SHB synthesis/final synthesis  : Qx                 */
    5466             :     Word16 *pitch_buf_fx,               /* i  : Q6                                                  */
    5467             :     Word16 *Q_white_exc )
    5468             : {
    5469             :     Word16 i, j, cnt, n;
    5470             :     Word16 stemp;
    5471             :     TD_BWE_DEC_HANDLE hBWE_TD;
    5472             :     Word32 shaped_shb_excitation_fx_32[L_FRAME16k + L_SHB_LAHEAD];
    5473             :     Word16 bwe_exc_extended_16[L_FRAME32k + NL_BUFF_OFFSET];
    5474             :     Word16 shaped_shb_excitation_fx[L_FRAME16k + L_SHB_LAHEAD];
    5475             :     Word16 lsf_shb_fx[LPC_SHB_ORDER], lpc_shb_fx[LPC_SHB_ORDER + 1], GainShape_fx[NUM_SHB_SUBFR]; // Q12,Q12,Q15
    5476             :     Word32 GainFrame_fx;                                                                          // Q18
    5477             :     Word32 error_fx[L_FRAME32k];
    5478             :     Word16 ener_fx;
    5479             :     Word32 L_ener;
    5480             :     Word16 is_fractive;
    5481             :     Word32 prev_pow_fx, curr_pow_fx, Lscale;
    5482             :     Word16 scale_fx;
    5483             :     Word16 max_val, temp_fx, shaped_shb_excitation_frac[L_FRAME16k + L_SHB_LAHEAD];
    5484             :     Word32 curr_frame_pow_fx;
    5485             :     Word16 curr_frame_pow_exp;
    5486             :     Word32 L_prev_ener_shb;
    5487             :     Word16 vf_modified_fx[NB_SUBFR16k];
    5488             :     Word16 f_fx, inc_fx;
    5489             :     Word32 GainFrame_prevfrm_fx;
    5490             :     Word32 tilt_swb_fec_32_fx;
    5491             :     Word16 tilt_swb_fec_fx_q;
    5492             :     Word16 tilt_swb_fec_fx;
    5493      102554 :     Word32 prev_ener_ratio_fx = 0; /* initialize just to avoid compiler warning */
    5494             :     Word16 lsp_shb_1_fx[LPC_SHB_ORDER], lsp_shb_2_fx[LPC_SHB_ORDER], lsp_temp_fx[LPC_SHB_ORDER];
    5495             :     Word16 lpc_shb_sf_fx[4 * ( LPC_SHB_ORDER + 1 )];
    5496             :     const Word16 *ptr_lsp_interp_coef_fx;
    5497             :     Word32 shb_ener_sf_32;
    5498             :     Word16 shb_res_gshape_fx[NB_SUBFR16k];
    5499             :     Word16 mixFactors_fx;
    5500             :     Word16 vind;
    5501             :     Word16 shb_res_dummy_fx[L_FRAME16k];
    5502             :     Word16 shaped_shb_excitationTemp_fx[L_FRAME16k];
    5503             :     Word32 ener_tmp_fx[NUM_SHB_SUBGAINS];
    5504             :     Word16 GainShape_tmp_fx[NUM_SHB_SUBGAINS];
    5505             :     Word16 pitch_fx;
    5506             :     Word16 l_subframe;
    5507             :     Word16 formant_fac_fx;
    5508             :     Word16 synth_scale_fx;
    5509             :     Word16 lsf_diff_fx[LPC_SHB_ORDER], w_fx[LPC_SHB_ORDER];
    5510             :     Word16 refl_fx[M];
    5511             :     Word16 tilt_para_fx;
    5512             :     Word16 *nlExc16k_fx, *mixExc16k_fx;
    5513             :     Word16 MSFlag;
    5514             :     Word16 feedback_fx;
    5515             :     Word16 GainShape_tilt_fx;
    5516             : 
    5517             :     // scaling
    5518             :     Word16 exp, tmp;
    5519             :     Word16 tmp1, tmp2;
    5520             :     Word16 mean_vf;
    5521             :     Word32 Lmax, L_tmp;
    5522             :     Word16 frac;
    5523             :     Word32 L_tmp1, L_tmp2;
    5524             :     Word16 Q_bwe_exc;
    5525             :     Word16 Q_bwe_exc_fb;
    5526             :     Word16 Q_shb;
    5527             :     Word16 n_mem, n_mem2, n_mem3, Qx, sc;
    5528             :     Word16 expa, expb;
    5529             :     Word16 fraca, fracb;
    5530             :     Word16 exp_ener, inv_ener;
    5531             : 
    5532      102554 :     hBWE_TD = st->hBWE_TD;
    5533             : 
    5534             :     /* initializations */
    5535      102554 :     GainFrame_fx = 0;
    5536      102554 :     move32();
    5537      102554 :     mixFactors_fx = 0;
    5538      102554 :     move16();
    5539      102554 :     shb_ener_sf_32 = 0;
    5540      102554 :     move32();
    5541      102554 :     set16_fx( shaped_shb_excitationTemp_fx, 0, L_FRAME16k );
    5542      102554 :     if ( st->hTdCngDec != NULL )
    5543             :     {
    5544       75201 :         st->hTdCngDec->shb_dtx_count_fx = 0;
    5545       75201 :         move16();
    5546             :     }
    5547      102554 :     is_fractive = 0;
    5548      102554 :     move16();
    5549             : 
    5550      102554 :     IF( hStereoICBWE != NULL )
    5551             :     {
    5552       16066 :         nlExc16k_fx = hStereoICBWE->nlExc16k_fx;
    5553       16066 :         mixExc16k_fx = hStereoICBWE->mixExc16k_fx;
    5554       16066 :         MSFlag = hStereoICBWE->MSFlag;
    5555       16066 :         move16();
    5556             :     }
    5557             :     ELSE
    5558             :     {
    5559       86488 :         nlExc16k_fx = NULL;
    5560       86488 :         mixExc16k_fx = NULL;
    5561       86488 :         MSFlag = 0;
    5562       86488 :         move16();
    5563             :     }
    5564             : 
    5565             :     /* find tilt */
    5566      102554 :     calc_tilt_bwe_fx_loc( old_syn_12k8_16k_fx, &tilt_swb_fec_32_fx, &tilt_swb_fec_fx_q, L_FRAME );
    5567      102554 :     tilt_swb_fec_fx = round_fx_sat( L_shl_sat( tilt_swb_fec_32_fx, sub( 11 + 16, tilt_swb_fec_fx_q ) ) );
    5568      102554 :     test();
    5569      102554 :     if ( st->bfi && st->clas_dec != UNVOICED_CLAS )
    5570             :     {
    5571        1694 :         tilt_swb_fec_fx = hBWE_TD->tilt_swb_fec_fx;
    5572        1694 :         move16();
    5573             :     }
    5574             : 
    5575             :     /* WB/SWB bandwidth switching */
    5576      102554 :     test();
    5577      102554 :     test();
    5578      102554 :     IF( ( GT_16( st->tilt_wb_fx, 10240 /*5 in Q11*/ ) && ( EQ_16( st->clas_dec, UNVOICED_CLAS ) ) ) || GT_16( st->tilt_wb_fx, 20480 /*10 in Q11*/ ) )
    5579             :     {
    5580        7104 :         test();
    5581        7104 :         test();
    5582        7104 :         test();
    5583        7104 :         test();
    5584        7104 :         test();
    5585        7104 :         test();
    5586        7104 :         test();
    5587        7104 :         test();
    5588        7104 :         IF( ( ( st->prev_fractive == 0 ) &&
    5589             :               ( LT_32( st->prev_enerLH_fx, L_shl( st->enerLH_fx, 1 ) ) && GT_32( st->prev_enerLH_fx, L_shr( st->enerLH_fx, 1 ) ) && LT_32( st->prev_enerLL_fx, L_shl( st->enerLL_fx, 1 ) ) && GT_32( st->prev_enerLL_fx, L_shr( st->enerLL_fx, 1 ) ) ) ) ||
    5590             :             ( EQ_16( st->prev_fractive, 1 ) &&
    5591             :               GT_32( L_shr( st->prev_enerLH_fx, 2 ), Mult_32_16( st->enerLH_fx, 24576 ) ) ) /* 24576 in Q13*/
    5592             :             || ( GT_32( L_shr( st->enerLL_fx, 1 ), Mult_32_16( st->enerLH_fx, 24576 ) ) &&  /*24576 = 1.5 in Q14*/
    5593             :                  LT_16( st->tilt_wb_fx, 20480 ) )                                           /* 20480 = 10 in Q11*/
    5594             :         )
    5595             :         {
    5596           0 :             is_fractive = 0;
    5597             :         }
    5598             :         ELSE
    5599             :         {
    5600        7104 :             is_fractive = 1;
    5601             :         }
    5602        7104 :         move16();
    5603             :     }
    5604             : 
    5605             :     /* WB/SWB bandwidth switching */
    5606      102554 :     IF( st->bws_cnt > 0 )
    5607             :     {
    5608          92 :         f_fx = 1489; /*1.0f / 22.0f in Q15*/
    5609          92 :         move16();
    5610          92 :         inc_fx = 1489; /*1.0f / 22.0f in Q15*/
    5611          92 :         move16();
    5612             : 
    5613          92 :         IF( EQ_16( is_fractive, 1 ) )
    5614             :         {
    5615           0 :             Copy( lsf_tab_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    5616             :         }
    5617             :         ELSE
    5618             :         {
    5619        1012 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5620             :             {
    5621         920 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
    5622         920 :                 move16();
    5623         920 :                 f_fx = add( f_fx, inc_fx ); /*Q15*/
    5624             :             }
    5625             :         }
    5626          92 :         test();
    5627          92 :         test();
    5628          92 :         test();
    5629          92 :         test();
    5630          92 :         test();
    5631          92 :         test();
    5632          92 :         test();
    5633          92 :         IF( ( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) && !( ( L_sub( L_shr( st->prev_enerLH_fx, 1 ), st->enerLH_fx ) < 0 ) && L_sub( st->prev_enerLH_fx, ( L_shr( st->enerLH_fx, 1 ) > 0 ) ) ) ) || st->last_core != ACELP_CORE || ( ( st->last_core == ACELP_CORE ) && GT_32( abs( L_sub( st->last_core_brate, st->core_brate ) ), 3600 ) ) || EQ_16( s_xor( is_fractive, st->prev_fractive ), 1 ) )
    5634             :         {
    5635          92 :             set16_fx( GainShape_fx, 11587, NUM_SHB_SUBFR ); /*0.3536f in Q15*/
    5636             :         }
    5637             :         ELSE
    5638             :         {
    5639           0 :             if ( GT_16( hBWE_TD->prev_GainShape_fx, 11587 ) ) /*0.3536f in Q15*/
    5640             :             {
    5641           0 :                 hBWE_TD->prev_GainShape_fx = 11587; /*0.3536f in Q15*/
    5642           0 :                 move16();
    5643             :             }
    5644           0 :             set16_fx( GainShape_fx, hBWE_TD->prev_GainShape_fx, NUM_SHB_SUBFR );
    5645             :         }
    5646             : 
    5647          92 :         Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
    5648          92 :         set16_fx( shb_res_gshape_fx, 3277 /*0.2f Q14*/, NB_SUBFR16k ); /* Q14 */
    5649             :     }
    5650             :     ELSE
    5651             :     {
    5652      102462 :         test();
    5653      102462 :         IF( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) )
    5654             :         {
    5655        6459 :             f_fx = 1489; /*Q15*/
    5656        6459 :             move16();
    5657        6459 :             inc_fx = 1489; /*Q15*/
    5658        6459 :             move16();
    5659       71049 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5660             :             {
    5661       64590 :                 hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
    5662       64590 :                 move16();
    5663       64590 :                 f_fx = add( f_fx, inc_fx );
    5664             :             }
    5665             :         }
    5666             : 
    5667      102462 :         IF( !st->bfi )
    5668             :         {
    5669      100335 :             IF( st->use_partial_copy )
    5670             :             {
    5671           0 :                 IF( NE_16( st->last_extl, SWB_TBE ) )
    5672             :                 {
    5673           0 :                     hBWE_TD->GainFrame_prevfrm_fx = 0;
    5674           0 :                     move32();
    5675           0 :                     f_fx = 1489 /*0.045454f Q15*/;
    5676           0 :                     move16();
    5677           0 :                     inc_fx = 1489 /*0.045454f Q15*/;
    5678           0 :                     move16();
    5679           0 :                     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    5680             :                     {
    5681           0 :                         hBWE_TD->lsp_prevfrm_fx[i] = f_fx; /*Q15*/
    5682           0 :                         move16();
    5683           0 :                         f_fx = add( f_fx, inc_fx ); /*Q15*/
    5684             :                     }
    5685             :                 }
    5686           0 :                 Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
    5687           0 :                 set16_fx( GainShape_fx, RECIP_ROOT_EIGHT_FX, NUM_SHB_SUBFR );
    5688             : 
    5689           0 :                 IF( EQ_16( st->rf_frame_type, RF_NELP ) )
    5690             :                 {
    5691             :                     /* Frame gain */
    5692             : 
    5693           0 :                     GainFrame_fx = L_mac( SHB_GAIN_QLOW_FX, st->rf_indx_tbeGainFr, SHB_GAIN_QDELTA_FX );
    5694           0 :                     L_tmp = Mult_32_16( GainFrame_fx, 27213 ); /*Q16*/ /* 3.321928 in Q13 */
    5695             : 
    5696           0 :                     frac = L_Extract_lc( L_tmp, &exp );
    5697           0 :                     L_tmp = Pow2( 30, frac );
    5698           0 :                     GainFrame_fx = L_shl( L_tmp, sub( exp, 12 ) ); /*Q18*/
    5699             : 
    5700           0 :                     test();
    5701           0 :                     test();
    5702           0 :                     test();
    5703           0 :                     test();
    5704           0 :                     test();
    5705           0 :                     test();
    5706           0 :                     IF( ( st->core == ACELP_CORE ) && ( st->last_core == ACELP_CORE ) && !st->prev_use_partial_copy && EQ_16( st->prev_coder_type, UNVOICED ) && NE_32( GainFrame_fx, hBWE_TD->GainFrame_prevfrm_fx ) && NE_16( st->next_coder_type, GENERIC ) && EQ_16( st->last_extl, SWB_TBE ) )
    5707             :                     {
    5708           0 :                         GainFrame_fx = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame_fx, 6553 /*0.2f in Q15*/ ) );
    5709             :                     }
    5710             :                 }
    5711             :                 ELSE
    5712             :                 {
    5713           0 :                     temp_fx = 0;
    5714           0 :                     move16();
    5715             :                     /* Frame gain */
    5716           0 :                     SWITCH( st->rf_indx_tbeGainFr )
    5717             :                     {
    5718           0 :                         case 0:
    5719           0 :                             GainFrame_fx = 131072; /* 0.5f in Q18 */
    5720           0 :                             move32();
    5721           0 :                             if ( LE_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) )
    5722             :                             {
    5723           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5724           0 :                                 move16();
    5725             :                             }
    5726           0 :                             BREAK;
    5727           0 :                         case 1:
    5728           0 :                             GainFrame_fx = 524288; /* 2.0f in Q18 */
    5729           0 :                             move32();
    5730           0 :                             test();
    5731           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 327680l /*1.25 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) )
    5732             :                             {
    5733           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5734           0 :                                 move16();
    5735             :                             }
    5736           0 :                             BREAK;
    5737           0 :                         case 2:
    5738           0 :                             GainFrame_fx = 1048576; /* 4.0f in Q18 */
    5739           0 :                             move32();
    5740           0 :                             test();
    5741           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 786432l /*3 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) )
    5742             :                             {
    5743           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5744           0 :                                 move16();
    5745             :                             }
    5746           0 :                             BREAK;
    5747           0 :                         case 3:
    5748           0 :                             GainFrame_fx = 2097152; /* 8.0f in Q18 */
    5749           0 :                             move32();
    5750           0 :                             test();
    5751           0 :                             if ( GT_32( hBWE_TD->GainFrame_prevfrm_fx, 1572864l /*6 Q18*/ ) && LE_32( hBWE_TD->GainFrame_prevfrm_fx, 4194304l /*16Q18*/ ) )
    5752             :                             {
    5753           0 :                                 temp_fx = 26214 /*0.8 Q15*/;
    5754           0 :                                 move16();
    5755             :                             }
    5756           0 :                             BREAK;
    5757           0 :                         default:
    5758           0 :                             fprintf( stderr, "RF SWB-TBE gain bits not supported." );
    5759             :                     }
    5760             : 
    5761           0 :                     IF( EQ_16( st->last_extl, SWB_TBE ) )
    5762             :                     {
    5763           0 :                         GainFrame_fx = L_add( Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, temp_fx ), Mult_32_16( GainFrame_fx, sub( 32767, temp_fx ) ) );
    5764             :                         /*Q18*/
    5765             :                     }
    5766             : 
    5767           0 :                     IF( ( st->core == ACELP_CORE ) && ( st->last_core == ACELP_CORE ) )
    5768             :                     {
    5769           0 :                         if ( !st->prev_use_partial_copy && EQ_16( st->last_coder_type, VOICED ) && EQ_16( st->rf_frame_type, RF_GENPRED ) && GT_32( GainFrame_fx, 2097152 /*8.0f in Q18*/ ) && LT_32( GainFrame_fx, 3059606 /*11.67f in Q18*/ ) )
    5770             :                         {
    5771           0 :                             GainFrame_fx = Mult_32_16( GainFrame_fx, 9830 /*0.3f in Q15*/ ); // Q18
    5772             :                         }
    5773             :                     }
    5774             :                 }
    5775             :             }
    5776             :             ELSE
    5777             :             {
    5778             :                 /* de-quantization */
    5779      100335 :                 ivas_dequantizeSHBparams_fx_9_1( st, st->extl, st->extl_brate, lsf_shb_fx, GainShape_fx, &GainFrame_fx, &stemp,
    5780             :                                                  &shb_ener_sf_32, shb_res_gshape_fx, &mixFactors_fx, &MSFlag );
    5781      100335 :                 if ( hStereoICBWE != NULL )
    5782             :                 {
    5783       15815 :                     hStereoICBWE->MSFlag = MSFlag;
    5784       15815 :                     move16();
    5785             :                 }
    5786             :             }
    5787             :         }
    5788             :         ELSE
    5789             :         {
    5790        2127 :             Copy( hBWE_TD->lsp_prevfrm_fx, lsf_shb_fx, LPC_SHB_ORDER );
    5791        2127 :             test();
    5792        2127 :             IF( EQ_16( st->codec_mode, MODE1 ) && ( st->element_mode == EVS_MONO ) )
    5793             :             {
    5794           0 :                 gradientGainShape( st, GainShape_fx, &GainFrame_fx );
    5795             :             }
    5796             :             ELSE
    5797             :             {
    5798       10635 :                 FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5799             :                 {
    5800       42540 :                     FOR( j = 0; j < 4; j++ )
    5801             :                     {
    5802       34032 :                         GainShape_fx[i * 4 + j] = mult_r( st->cummulative_damping, hBWE_TD->GainShape_Delay_fx[4 + i] );
    5803       34032 :                         move16();
    5804             :                     }
    5805             :                 }
    5806        2127 :                 IF( GT_16( tilt_swb_fec_fx, ( 8 << 11 ) ) ) /* tilt_swb_fec_fx in Q11 */
    5807             :                 {
    5808          46 :                     IF( EQ_16( st->nbLostCmpt, 1 ) )
    5809             :                     {
    5810          39 :                         GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 19661 /*0.6f Q15*/ );
    5811             :                     }
    5812           7 :                     ELSE IF( EQ_16( st->nbLostCmpt, 2 ) )
    5813             :                     {
    5814           7 :                         GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 11469 /*0.35f Q15*/ );
    5815             :                     }
    5816             :                     ELSE
    5817             :                     {
    5818           0 :                         GainFrame_fx = Mult_32_16( hBWE_TD->GainFrame_prevfrm_fx, 6554 /*0.2f Q15*/ );
    5819             :                     }
    5820          46 :                     GainFrame_fx = Mult_32_16( GainFrame_fx, st->cummulative_damping );
    5821             :                 }
    5822             :                 ELSE
    5823             :                 {
    5824        2081 :                     GainFrame_fx = hBWE_TD->GainFrame_prevfrm_fx;
    5825        2081 :                     move32(); /* gain locking */
    5826             :                 }
    5827             :             }
    5828             : 
    5829        2127 :             IF( GE_32( st->extl_brate, SWB_TBE_2k8 ) )
    5830             :             {
    5831         808 :                 test();
    5832         808 :                 IF( EQ_16( st->codec_mode, MODE1 ) && ( st->element_mode == EVS_MONO ) )
    5833             :                 {
    5834           0 :                     L_tmp = L_mult( extract_l( hBWE_TD->prev2_shb_ener_sf_fx ), extract_l( hBWE_TD->prev3_shb_ener_sf_fx ) ); /*Q1*/
    5835           0 :                     tmp = round_fx( root_a_fx( L_tmp, 1, &exp ) );                                                            /* Q = 15-exp */
    5836           0 :                     tmp1 = extract_l( hBWE_TD->prev1_shb_ener_sf_fx );                                                        /*Q0*/
    5837           0 :                     i = sub( norm_s( tmp1 ), 1 );
    5838           0 :                     tmp1 = shl( tmp1, i ); /* Qi */
    5839           0 :                     IF( tmp == 0 )
    5840             :                     {
    5841           0 :                         tmp = 32767 /*1.0f Q15*/;
    5842           0 :                         move16(); /*Q15*/
    5843             :                     }
    5844             :                     ELSE
    5845             :                     {
    5846           0 :                         scale_fx = div_s( tmp1, tmp ); /* Q15 - Q(15-exp) + Qi  = Qexp+i */
    5847           0 :                         scale_fx = s_max( scale_fx, 0 );
    5848           0 :                         tmp = shl_sat( scale_fx, sub( sub( 15, exp ), i ) ); /*Q15*/
    5849             :                     }
    5850           0 :                     scale_fx = mult_r( hBWE_TD->prev_res_shb_gshape_fx, tmp ); /* Q14 */
    5851           0 :                     test();
    5852           0 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    5853             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    5854             :                     {
    5855           0 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, scale_fx );
    5856             : 
    5857           0 :                         if ( GT_16( st->nbLostCmpt, 1 ) )
    5858             :                         {
    5859           0 :                             shb_ener_sf_32 = L_shr( shb_ener_sf_32, 1 );
    5860             :                         }
    5861             :                     }
    5862             :                     ELSE
    5863             :                     {
    5864           0 :                         L_tmp = L_mult( scale_fx, scale_fx ); /* Q29 */
    5865           0 :                         shb_ener_sf_32 = L_shl( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, round_fx( L_tmp ) ), 2 );
    5866             :                     }
    5867             :                 }
    5868             :                 ELSE
    5869             :                 {
    5870         808 :                     test();
    5871         808 :                     IF( GT_32( L_shr( hBWE_TD->prev2_shb_ener_sf_fx, 1 ), hBWE_TD->prev1_shb_ener_sf_fx ) ||
    5872             :                         GT_32( L_shr( hBWE_TD->prev3_shb_ener_sf_fx, 1 ), hBWE_TD->prev2_shb_ener_sf_fx ) )
    5873             :                     {
    5874          60 :                         shb_ener_sf_32 = L_shr( Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st->cummulative_damping ), 1 );
    5875             :                     }
    5876             :                     ELSE
    5877             :                     {
    5878         748 :                         shb_ener_sf_32 = Mult_32_16( hBWE_TD->prev1_shb_ener_sf_fx, st->cummulative_damping );
    5879             :                     }
    5880             :                 }
    5881             :             }
    5882             : 
    5883        2127 :             shb_ener_sf_32 = L_max( shb_ener_sf_32, 1l /*1.0f Q0*/ );
    5884        2127 :             mixFactors_fx = hBWE_TD->prev_mixFactors_fx;
    5885        2127 :             move16();
    5886             : 
    5887        2127 :             IF( EQ_16( st->codec_mode, MODE1 ) )
    5888             :             {
    5889        2127 :                 set16_fx( shb_res_gshape_fx, 3277 /*0.2f Q14*/, 5 ); /* Q14 */
    5890             :             }
    5891             :             ELSE
    5892             :             {
    5893           0 :                 set16_fx( shb_res_gshape_fx, 16384 /*1.0f Q14*/, 5 ); /* Q14 */
    5894             :             }
    5895             :         }
    5896             :     }
    5897             : 
    5898             :     /* get the gainshape delay */
    5899      102554 :     Copy( &hBWE_TD->GainShape_Delay_fx[4], &hBWE_TD->GainShape_Delay_fx[0], NUM_SHB_SUBFR / 4 );
    5900      512770 :     FOR( i = 0; i < NUM_SHB_SUBFR / 4; i++ )
    5901             :     {
    5902      410216 :         hBWE_TD->GainShape_Delay_fx[i + 4] = GainShape_fx[i * 4]; /*Q15*/
    5903      410216 :         move16();
    5904             :     }
    5905             : 
    5906      102554 :     L_tmp = L_mult( voice_factors_fx[0], 8192 );
    5907      102554 :     L_tmp = L_mac( L_tmp, voice_factors_fx[1], 8192 );
    5908      102554 :     L_tmp = L_mac( L_tmp, voice_factors_fx[2], 8192 );
    5909      102554 :     mean_vf = mac_r( L_tmp, voice_factors_fx[3], 8192 );
    5910             : 
    5911      102554 :     Copy( voice_factors_fx, vf_modified_fx, NB_SUBFR16k );
    5912             : 
    5913      102554 :     test();
    5914      102554 :     IF( EQ_16( st->coder_type, VOICED ) || GT_16( mean_vf, 13107 /*0.4f Q15*/ ) )
    5915             :     {
    5916       90228 :         FOR( i = 1; i < NB_SUBFR; i++ )
    5917             :         {
    5918       67671 :             L_tmp = L_mult( voice_factors_fx[i], 26214 /*0.8f Q15*/ );
    5919       67671 :             vf_modified_fx[i] = mac_r( L_tmp, voice_factors_fx[i - 1], 6554 /*0.2f Q15*/ );
    5920       67671 :             move16();
    5921             :         }
    5922             : 
    5923       22557 :         IF( st->L_frame != L_FRAME )
    5924             :         {
    5925       11477 :             L_tmp = L_mult( voice_factors_fx[4], 26214 /*0.8f Q15*/ );
    5926       11477 :             vf_modified_fx[4] = mac_r( L_tmp, voice_factors_fx[3], 6554 /*0.2f Q15*/ );
    5927       11477 :             move16();
    5928             :         }
    5929             :     }
    5930             : 
    5931      102554 :     test();
    5932      102554 :     IF( st->use_partial_copy && st->nelp_mode_dec )
    5933             :     {
    5934           0 :         set16_fx( vf_modified_fx, 0, NB_SUBFR16k );
    5935             :     }
    5936             : 
    5937             :     /* SHB LSF from current frame; and convert to LSP for interpolation */
    5938      102554 :     E_LPC_lsf_lsp_conversion( lsf_shb_fx, lsp_shb_2_fx, LPC_SHB_ORDER );
    5939             : 
    5940      102554 :     test();
    5941      102554 :     IF( EQ_16( st->last_extl, SWB_TBE ) || EQ_16( st->last_extl, FB_TBE ) )
    5942             :     {
    5943             :         /* SHB LSP values from prev. frame for interpolation */
    5944       96003 :         Copy( hBWE_TD->swb_lsp_prev_interp_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    5945             :     }
    5946             :     ELSE
    5947             :     {
    5948             :         /* Use current frame's LSPs; in effect no interpolation */
    5949        6551 :         Copy( lsp_shb_2_fx, lsp_shb_1_fx, LPC_SHB_ORDER );
    5950             :     }
    5951             : 
    5952      102554 :     test();
    5953      102554 :     test();
    5954      102554 :     test();
    5955      102554 :     IF( ( st->bws_cnt == 0 ) && ( st->bws_cnt1 == 0 ) && ( st->prev_use_partial_copy == 0 ) && ( st->use_partial_copy == 0 ) )
    5956             :     {
    5957      102200 :         lsf_diff_fx[0] = 16384;
    5958      102200 :         move16(); /*Q15*/
    5959      102200 :         lsf_diff_fx[LPC_SHB_ORDER - 1] = 16384;
    5960      102200 :         move16(); /*Q15*/
    5961      919800 :         FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    5962             :         {
    5963      817600 :             lsf_diff_fx[i] = sub( lsf_shb_fx[i], lsf_shb_fx[i - 1] );
    5964      817600 :             move16();
    5965             :         }
    5966             : 
    5967      102200 :         a2rc_fx( hBWE_TD->cur_sub_Aq_fx + 1, refl_fx, M );
    5968      102200 :         tmp = add( 16384, shr( refl_fx[0], 1 ) ); /*Q14*/
    5969      102200 :         tmp1 = mult( 27425, tmp );
    5970      102200 :         tmp1 = mult( tmp1, tmp );                      /*Q10*/
    5971      102200 :         tmp2 = shr( mult( 31715, tmp ), 2 );           /*Q10*/
    5972      102200 :         tilt_para_fx = add( sub( tmp1, tmp2 ), 1335 ); /*Q10*/
    5973             : 
    5974      102200 :         test();
    5975      102200 :         IF( NE_16( st->last_extl, SWB_TBE ) && NE_16( st->last_extl, FB_TBE ) )
    5976             :         {
    5977       57231 :             FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    5978             :             {
    5979       50872 :                 hBWE_TD->prev_lsf_diff_fx[i - 1] = shr( lsf_diff_fx[i], 1 );
    5980       50872 :                 move16();
    5981             :             }
    5982             :         }
    5983             : 
    5984      102200 :         IF( LE_32( st->extl_brate, FB_TBE_1k8 ) )
    5985             :         {
    5986       80703 :             test();
    5987       80703 :             test();
    5988       80703 :             test();
    5989       80703 :             test();
    5990       80703 :             test();
    5991       80703 :             IF( !( GT_16( hBWE_TD->prev_tilt_para_fx, 5120 ) && ( EQ_16( st->coder_type, TRANSITION ) || LT_16( tilt_para_fx, 1024 ) ) ) &&
    5992             :                 !( ( ( LT_16( hBWE_TD->prev_tilt_para_fx, 3072 ) && GE_16( st->prev_coder_type, VOICED ) ) ) && GT_16( tilt_para_fx, 5120 ) ) )
    5993             :             {
    5994      708795 :                 FOR( i = 1; i < LPC_SHB_ORDER - 1; i++ )
    5995             :                 {
    5996      630040 :                     IF( LT_16( lsf_diff_fx[i], hBWE_TD->prev_lsf_diff_fx[i - 1] ) )
    5997             :                     {
    5998      255598 :                         tmp = mult( 26214, lsf_diff_fx[i] );
    5999             : 
    6000      255598 :                         test();
    6001      255598 :                         IF( ( hBWE_TD->prev_lsf_diff_fx[i - 1] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    6002             :                         {
    6003           0 :                             st->BER_detect = 1;
    6004           0 :                             move16();
    6005           0 :                             tmp = 0;
    6006           0 :                             move16();
    6007             :                         }
    6008             :                         ELSE
    6009             :                         {
    6010      255598 :                             tmp = div_s( tmp, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    6011             :                         }
    6012             : 
    6013      255598 :                         tmp = s_max( tmp, 16384 );
    6014      255598 :                         w_fx[i] = s_min( tmp, 32767 );
    6015      255598 :                         move16();
    6016             :                     }
    6017             :                     ELSE
    6018             :                     {
    6019      374442 :                         tmp = mult( 26214, hBWE_TD->prev_lsf_diff_fx[i - 1] );
    6020             : 
    6021      374442 :                         test();
    6022      374442 :                         IF( ( lsf_diff_fx[i] <= 0 ) || ( tmp < 0 ) ) /* safety check in case of bit errors */
    6023             :                         {
    6024           0 :                             st->BER_detect = 1;
    6025           0 :                             move16();
    6026           0 :                             tmp = 0;
    6027           0 :                             move16();
    6028             :                         }
    6029             :                         ELSE
    6030             :                         {
    6031      374442 :                             tmp = div_s( tmp, lsf_diff_fx[i] );
    6032             :                         }
    6033             : 
    6034      374442 :                         tmp = s_max( tmp, 16384 );
    6035      374442 :                         w_fx[i] = s_min( tmp, 32767 );
    6036      374442 :                         move16();
    6037             :                     }
    6038             :                 }
    6039       78755 :                 w_fx[0] = w_fx[1];
    6040       78755 :                 move16();
    6041       78755 :                 w_fx[LPC_SHB_ORDER - 1] = w_fx[LPC_SHB_ORDER - 2];
    6042       78755 :                 move16();
    6043             : 
    6044      866305 :                 FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    6045             :                 {
    6046      787550 :                     tmp1 = mult( lsp_shb_1_fx[i], sub( 32767, w_fx[i] ) );
    6047      787550 :                     tmp2 = mult( lsp_shb_2_fx[i], w_fx[i] );
    6048      787550 :                     lsp_temp_fx[i] = add( tmp1, tmp2 );
    6049      787550 :                     move16();
    6050             :                 }
    6051             :             }
    6052             :             ELSE
    6053             :             {
    6054        1948 :                 Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
    6055             :             }
    6056             : 
    6057             :             /* convert from lsp to lsf */
    6058       80703 :             lsp2lsf_fx( lsp_temp_fx, lsf_shb_fx, LPC_SHB_ORDER, 1 );
    6059             :         }
    6060             : 
    6061      102200 :         Copy( lsf_diff_fx + 1, hBWE_TD->prev_lsf_diff_fx, LPC_SHB_ORDER - 2 );
    6062      102200 :         hBWE_TD->prev_tilt_para_fx = tilt_para_fx;
    6063      102200 :         move16();
    6064             :     }
    6065             :     ELSE
    6066             :     {
    6067         354 :         Copy( lsp_shb_2_fx, lsp_temp_fx, LPC_SHB_ORDER );
    6068             :     }
    6069             : 
    6070      102554 :     IF( GE_32( st->extl_brate, SWB_TBE_2k8 ) )
    6071             :     {
    6072             :         /* SHB LSP interpolation */
    6073       21546 :         ptr_lsp_interp_coef_fx = interpol_frac_shb; /*Q15*/
    6074      107730 :         FOR( j = 0; j < 4; j++ )
    6075             :         {
    6076      948024 :             FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    6077             :             {
    6078      861840 :                 L_tmp = L_mult( lsp_shb_1_fx[i], ( *ptr_lsp_interp_coef_fx ) );
    6079      861840 :                 lsp_temp_fx[i] = mac_r( L_tmp, lsp_shb_2_fx[i], ( *( ptr_lsp_interp_coef_fx + 1 ) ) );
    6080      861840 :                 move16();
    6081             :             }
    6082       86184 :             ptr_lsp_interp_coef_fx += 2;
    6083             : 
    6084       86184 :             tmp = i_mult( j, ( LPC_SHB_ORDER + 1 ) );
    6085             :             /* convert LSPs to LP coefficients */
    6086       86184 :             E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_sf_fx + tmp, LPC_SHB_ORDER );
    6087             :         }
    6088             :     }
    6089             : 
    6090             :     /* Save the SWB LSP values from current frame for interpolation */
    6091      102554 :     Copy( lsp_shb_2_fx, hBWE_TD->swb_lsp_prev_interp_fx, LPC_SHB_ORDER );
    6092             : 
    6093             :     /* save the shb_ener and mixFactor values */
    6094      102554 :     hBWE_TD->prev3_shb_ener_sf_fx = hBWE_TD->prev2_shb_ener_sf_fx;
    6095      102554 :     move32();
    6096      102554 :     hBWE_TD->prev2_shb_ener_sf_fx = hBWE_TD->prev1_shb_ener_sf_fx;
    6097      102554 :     move32();
    6098      102554 :     hBWE_TD->prev1_shb_ener_sf_fx = shb_ener_sf_32;
    6099      102554 :     move32();
    6100      102554 :     hBWE_TD->prev_res_shb_gshape_fx = shb_res_gshape_fx[4];
    6101      102554 :     move16();
    6102      102554 :     hBWE_TD->prev_mixFactors_fx = mixFactors_fx;
    6103      102554 :     move16();
    6104             : 
    6105             :     /* SWB CNG/DTX - update memories */
    6106      102554 :     IF( st->hTdCngDec != NULL )
    6107             :     {
    6108       75201 :         Copy( st->hTdCngDec->lsp_shb_prev_fx, st->hTdCngDec->lsp_shb_prev_prev_fx, LPC_SHB_ORDER );
    6109       75201 :         Copy( lsf_shb_fx, st->hTdCngDec->lsp_shb_prev_fx, LPC_SHB_ORDER );
    6110             :     }
    6111             : 
    6112             :     /* convert LSPs back into LP coeffs */
    6113      102554 :     E_LPC_f_lsp_a_conversion( lsp_temp_fx, lpc_shb_fx, LPC_SHB_ORDER );
    6114      102554 :     Copy_Scale_sig( lpc_shb_fx, lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Q12 */
    6115      102554 :     lpc_shb_fx[0] = ONE_IN_Q12;
    6116      102554 :     move16();
    6117             : 
    6118      102554 :     test();
    6119      102554 :     IF( EQ_32( st->extl_brate, SWB_TBE_1k10 ) || EQ_32( st->extl_brate, SWB_TBE_1k75 ) )
    6120        4106 :     {
    6121        4106 :         Word32 vind_temp = Mpy_32_32( L_shl( L_add( L_deposit_l( mixFactors_fx ), 1 ), 15 ), ( ( ( 1 << NUM_BITS_SHB_VF ) - 1 ) << 16 ) ); // check addition of 1
    6122        4106 :         vind = extract_l( L_shr( vind_temp, 15 ) ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */                                         /*mixFactors*7*/
    6123             :         /* i: mixFactors_fx in Q15 */
    6124             :         /* o: vind in Q0        */
    6125             :     }
    6126             :     ELSE
    6127             :     {
    6128       98448 :         vind = shl( mixFactors_fx, 3 - 15 ); /* 3 for mpy by 8.0f, -15 to bring it to Q0 */ /*mixFactors*8*/
    6129             :         /* i: mixFactors_fx in Q15 */
    6130             :         /* o: vind in Q0        */
    6131             :     }
    6132             : 
    6133             :     /* Determine formant PF strength */
    6134      102554 :     formant_fac_fx = swb_formant_fac_fx( lpc_shb_fx[1], &hBWE_TD->tilt_mem_fx );
    6135             :     /* i:lpc_shb_fx Q12, o:formant_fac_fx Q15 */
    6136      102554 :     IF( GT_32( st->total_brate, ACELP_32k ) )
    6137             :     {
    6138       43680 :         FOR( j = 0; j < 4; j++ )
    6139             :         {
    6140       34944 :             Copy( lpc_shb_fx, &lpc_shb_sf_fx[i_mult( j, ( LPC_SHB_ORDER + 1 ) )], LPC_SHB_ORDER + 1 );
    6141             :         }
    6142             :     }
    6143             : 
    6144             :     /* From low band excitation, generate highband excitation */
    6145             : 
    6146             :     /* -------- start of  memory rescaling  -------- */
    6147             :     /* ----- calculate optimum Q_bwe_exc and rescale memories accordingly ----- */
    6148      102554 :     Lmax = 0;
    6149      102554 :     move32();
    6150    65737114 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    6151             :     {
    6152    65634560 :         Lmax = L_max( Lmax, L_abs( bwe_exc_extended_fx[cnt] ) );
    6153             :     }
    6154      102554 :     Q_bwe_exc = norm_l( Lmax );
    6155      102554 :     if ( Lmax == 0 )
    6156             :     {
    6157         372 :         Q_bwe_exc = 31;
    6158         372 :         move16();
    6159             :     }
    6160      102554 :     Q_bwe_exc = add( Q_bwe_exc, add( Q_exc, Q_exc ) );
    6161      102554 :     find_max_mem_dec( st, &n_mem, &n_mem2, &n_mem3 ); /* for >=24.4, use n_mem2 lpc_syn, shb_20sample, and mem_stp_swb_fx memory */
    6162             : 
    6163      102554 :     tmp = add( st->prev_Q_bwe_exc, n_mem );
    6164      102554 :     if ( GT_16( Q_bwe_exc, tmp ) )
    6165             :     {
    6166       19485 :         Q_bwe_exc = tmp;
    6167       19485 :         move16();
    6168             :     }
    6169             : 
    6170             :     /* rescale the memories if Q_bwe_exc is different from previous frame */
    6171      102554 :     sc = sub( Q_bwe_exc, st->prev_Q_bwe_exc );
    6172      102554 :     IF( sc != 0 )
    6173             :     {
    6174       43172 :         rescale_genSHB_mem_dec_ivas( st, sc );
    6175             :     }
    6176             : 
    6177             :     /* rescale the bwe_exc_extended and bring it to 16-bit single precision with dynamic norm  */
    6178      102554 :     sc = sub( Q_bwe_exc, add( Q_exc, Q_exc ) );
    6179             : 
    6180    65737114 :     FOR( cnt = 0; cnt < L_FRAME32k; cnt++ )
    6181             :     {
    6182    65634560 :         bwe_exc_extended_16[cnt] = round_fx_sat( L_shl_sat( bwe_exc_extended_fx[cnt], sc ) );
    6183    65634560 :         move16();
    6184             :     }
    6185             : 
    6186             :     /* state_syn_shbexc_fx is kept at (st_fx->prev_Q_bwe_syn) for 24.4/32kbps or is kept at Q_bwe_exc for 13.2/16.4kbps */
    6187             : 
    6188             :     /* save the previous Q factor (32-bit) of the buffer */
    6189      102554 :     st->prev_Q_bwe_exc = Q_bwe_exc;
    6190      102554 :     move16();
    6191             : 
    6192      102554 :     Q_bwe_exc = sub( Q_bwe_exc, 16 ); /* Q_bwe_exc reflecting the single precision dynamic norm-ed buffers from here */
    6193             : 
    6194             :     /* -------- end of rescaling memories -------- */
    6195             : 
    6196      102554 :     Q_bwe_exc_fb = hBWE_TD->prev_Q_bwe_exc_fb;
    6197      102554 :     move16();
    6198             : 
    6199      102554 :     Q_shb = 0;
    6200      102554 :     move16();
    6201             : 
    6202      102554 :     Copy( hBWE_TD->state_syn_shbexc_fx, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    6203      102554 :     GenShapedSHBExcitation_ivas_dec_fx( shaped_shb_excitation_fx + L_SHB_LAHEAD, lpc_shb_fx, White_exc16k_fx,
    6204      102554 :                                         hBWE_TD->mem_csfilt_fx, hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->state_lpc_syn_fx,
    6205      102554 :                                         st->coder_type, bwe_exc_extended_16, hBWE_TD->bwe_seed, vf_modified_fx, st->extl,
    6206             :                                         &( hBWE_TD->tbe_demph_fx ), &( hBWE_TD->tbe_premph_fx ), lpc_shb_sf_fx, shb_ener_sf_32,
    6207      102554 :                                         shb_res_gshape_fx, shb_res_dummy_fx, &vind, formant_fac_fx, hBWE_TD->fb_state_lpc_syn_fx,
    6208      102554 :                                         &( hBWE_TD->fb_tbe_demph_fx ), &Q_bwe_exc, &Q_bwe_exc_fb, Q_shb, n_mem2, hBWE_TD->prev_Q_bwe_syn, st->total_brate, st->prev_bfi,
    6209      102554 :                                         st->element_mode, st->flag_ACELP16k, nlExc16k_fx, mixExc16k_fx, st->extl_brate, MSFlag,
    6210             :                                         NULL, &( hBWE_TD->prev_pow_exc16kWhtnd_fx32 ), &( hBWE_TD->prev_mix_factor_fx ), NULL, NULL );
    6211             : 
    6212      102554 :     *Q_white_exc = Q_bwe_exc_fb;
    6213      102554 :     move16();
    6214      102554 :     IF( EQ_16( st->extl, FB_TBE ) )
    6215             :     {
    6216       39990 :         hBWE_TD->prev_Q_bwe_exc_fb = Q_bwe_exc_fb;
    6217       39990 :         move16();
    6218             :     }
    6219             :     ELSE
    6220             :     {
    6221             :         /*Indirectly a memory reset of FB memories for next frame such that rescaling of memories would lead to 0 due to such high prev. Q value.
    6222             :         51 because of 31 + 20(shift of Q_bwe_exc_fb before de-emphasis)*/
    6223       62564 :         hBWE_TD->prev_Q_bwe_exc_fb = 51;
    6224       62564 :         move16();
    6225             :     }
    6226             : 
    6227             :     /* rescale the TBE post proc memory */
    6228     1128094 :     FOR( i = 0; i < LPC_SHB_ORDER; i++ )
    6229             :     {
    6230     1025540 :         hBWE_TD->mem_stp_swb_fx[i] = shl_sat( hBWE_TD->mem_stp_swb_fx[i], sub( Q_bwe_exc, hBWE_TD->prev_Q_bwe_syn ) );
    6231     1025540 :         move16();
    6232             :     }
    6233             :     /* fill-in missing SHB excitation */
    6234      102554 :     test();
    6235      102554 :     test();
    6236      102554 :     IF( ( EQ_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) && LE_32( st->last_core_brate, SID_2k40 ) )
    6237             :     {
    6238         160 :         Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, shaped_shb_excitation_fx, L_SHB_LAHEAD );
    6239             :     }
    6240             : 
    6241      102554 :     IF( hStereoICBWE != NULL )
    6242             :     {
    6243       16066 :         Copy( shaped_shb_excitation_fx + L_SHB_LAHEAD, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
    6244             :     }
    6245             : 
    6246      102554 :     test();
    6247      102554 :     IF( NE_32( st->extl_brate, SWB_TBE_1k10 ) && NE_32( st->extl_brate, SWB_TBE_1k75 ) )
    6248             :     {
    6249      492240 :         FOR( i = 0; i < L_FRAME16k; i += L_SUBFR16k )
    6250             :         {
    6251             :             /* TD BWE post-processing */
    6252      393792 :             PostShortTerm_ivas_dec_fx( &shaped_shb_excitation_fx[L_SHB_LAHEAD + i], lpc_shb_fx, &shaped_shb_excitationTemp_fx[i], hBWE_TD->mem_stp_swb_fx,
    6253      393792 :                                        hBWE_TD->ptr_mem_stp_swb_fx, &( hBWE_TD->gain_prec_swb_fx ), hBWE_TD->mem_zero_swb_fx, formant_fac_fx );
    6254             :         }
    6255             : 
    6256       98448 :         Copy( shaped_shb_excitationTemp_fx, &shaped_shb_excitation_fx[L_SHB_LAHEAD], L_FRAME16k ); /* Q_bwe_exc */
    6257             : 
    6258       98448 :         tmp = sub( shl( Q_bwe_exc, 1 ), 31 + 16 );
    6259       98448 :         prev_pow_fx = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    6260       98448 :         curr_pow_fx = L_shl_sat( 1407374848l /*0.00001f Q47*/, tmp ); /* 2*(Q_bwe_exc) */
    6261     3051888 :         FOR( i = 0; i < L_SHB_LAHEAD + 10; i++ )
    6262             :         {
    6263     2953440 :             prev_pow_fx = L_mac0_sat( prev_pow_fx, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] );                                         /*2*Q_bwe_exc*/
    6264     2953440 :             curr_pow_fx = L_mac0_sat( curr_pow_fx, shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10], shaped_shb_excitation_fx[i + L_SHB_LAHEAD + 10] ); /* 2*Q_bwe_exc */
    6265             :         }
    6266             : 
    6267       98448 :         if ( GT_16( voice_factors_fx[0], 24576 /*0.75f Q15*/ ) )
    6268             :         {
    6269        2962 :             curr_pow_fx = L_shr( curr_pow_fx, 2 ); /* Q(2*Q_bwe_exc) */
    6270             :         }
    6271             : 
    6272       98448 :         Lscale = root_a_over_b_fx( curr_pow_fx, shl( Q_bwe_exc, 1 ), prev_pow_fx, shl( Q_bwe_exc, 1 ), &exp );
    6273             : 
    6274     2067408 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    6275             :         {
    6276     1968960 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    6277     1968960 :             shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    6278     1968960 :             move16();
    6279             :         }
    6280       98448 :         IF( exp < 0 )
    6281             :         {
    6282        2619 :             Lscale = L_shl( Lscale, exp );
    6283        2619 :             exp = 0;
    6284        2619 :             move16();
    6285             :         }
    6286     1082928 :         FOR( ; i < L_SHB_LAHEAD + 10; i++ )
    6287             :         {
    6288      984480 :             temp_fx = i_mult_sat( sub( i, 19 ), 3277 /*0.1f Q15*/ );        /* Q15 */
    6289      984480 :             L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), temp_fx ); /* Q31-exp */
    6290      984480 :             temp_fx = sub( 32767 /*1.0f Q15*/, temp_fx );
    6291      984480 :             Lscale = L_add( Mult_32_16( Lscale, temp_fx ), L_tmp1 );
    6292      984480 :             L_tmp = Mult_32_16( Lscale, shaped_shb_excitation_fx[i] );             /* Q_bwe_exc + (31-exp) - 15 */
    6293      984480 :             shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */
    6294      984480 :             move16();
    6295             :         }
    6296             :     }
    6297             :     ELSE
    6298             :     {
    6299             :         /* reset the PF memories if the PF is not running */
    6300        4106 :         set16_fx( hBWE_TD->mem_stp_swb_fx, 0, LPC_SHB_ORDER );
    6301        4106 :         hBWE_TD->gain_prec_swb_fx = ONE_IN_Q14;
    6302        4106 :         move16();
    6303        4106 :         set16_fx( hBWE_TD->mem_zero_swb_fx, 0, LPC_SHB_ORDER );
    6304             :     }
    6305             : 
    6306             :     /* Update SHB excitation */
    6307      102554 :     Copy( shaped_shb_excitation_fx + L_FRAME16k, hBWE_TD->state_syn_shbexc_fx, L_SHB_LAHEAD ); /* Q_bwe_exc */
    6308      102554 :     l_subframe = L_FRAME16k / NUM_SHB_SUBGAINS;
    6309      102554 :     move16();
    6310      102554 :     L_ener = EPSILON_FX_SMALL;
    6311      102554 :     move32();
    6312             : 
    6313      512770 :     FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6314             :     {
    6315      410216 :         L_tmp = 0;
    6316      410216 :         move32();
    6317      410216 :         ener_tmp_fx[i] = EPSILON_FX_SMALL;
    6318      410216 :         move32();
    6319             : 
    6320      410216 :         Word64 tmp64 = 0;
    6321      410216 :         move64();
    6322    33227496 :         FOR( j = 0; j < l_subframe; j++ )
    6323             :         {
    6324    32817280 :             tmp64 = W_mac0_16_16( tmp64, shaped_shb_excitation_fx[add( i_mult( i, l_subframe ), j )], shaped_shb_excitation_fx[add( i_mult( i, l_subframe ), j )] ); /* 2*Q_bwe_exc */
    6325             :         }
    6326      410216 :         L_tmp = W_sat_l( tmp64 );
    6327             : 
    6328      410216 :         L_tmp = Mult_32_16( L_tmp, 410 /*0.0125 Q15*/ ); /* 2*Q_bwe_exc: ener_tmp_fx in (2*Q_bwe_exc) */
    6329      410216 :         IF( L_tmp != 0 )
    6330             :         {
    6331      409147 :             exp = norm_l( L_tmp );
    6332      409147 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    6333      409147 :             exp = sub( exp, sub( 30, i_mult( 2, Q_bwe_exc ) ) );
    6334             : 
    6335      409147 :             tmp = div_s( 16384, tmp );
    6336      409147 :             L_tmp = L_deposit_h( tmp );
    6337      409147 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    6338      409147 :             ener_tmp_fx[i] = L_shl_sat( L_tmp, sub( add( exp, shl( Q_bwe_exc, 1 ) ), 31 ) ); /*2 * Q_bwe_exc: Q31 -exp +exp +2 * Q_bwe_exc -31  */
    6339      409147 :             move32();
    6340      409147 :             L_ener = L_add_sat( L_ener, L_shr( ener_tmp_fx[i], 2 ) ); /* 2*Q_bwe_exc */
    6341             :         }
    6342             :     }
    6343      102554 :     ener_fx = s_max( 1, round_fx_sat( L_shl_sat( L_ener, sub( 18, shl( Q_bwe_exc, 1 ) ) ) ) ); /* Q2: 2*Q_bwe_exc+18-2*Q_bwe_exc-16 */
    6344             :     /* WB/SWB bandwidth switching */
    6345      102554 :     IF( st->bws_cnt > 0 )
    6346             :     {
    6347          92 :         IF( is_fractive == 0 )
    6348             :         {
    6349          92 :             IF( GT_16( st->tilt_wb_fx, 2048 ) ) /*assuming st->tilt_wb_fx in Q11*/
    6350             :             {
    6351           6 :                 st->tilt_wb_fx = 2048;
    6352           6 :                 move16();
    6353             :             }
    6354          86 :             ELSE IF( LT_16( st->tilt_wb_fx, 1024 ) )
    6355             :             {
    6356          82 :                 st->tilt_wb_fx = 1024;
    6357          82 :                 move16();
    6358             :             }
    6359          92 :             test();
    6360          92 :             if ( EQ_16( st->prev_fractive, 1 ) && GT_16( st->tilt_wb_fx, 1024 ) )
    6361             :             {
    6362           0 :                 st->tilt_wb_fx = 1024;
    6363           0 :                 move16();
    6364             :             }
    6365             :         }
    6366             :         ELSE
    6367             :         {
    6368           0 :             IF( GT_16( st->tilt_wb_fx, 8192 ) )
    6369             :             {
    6370           0 :                 IF( st->prev_fractive == 0 )
    6371             :                 {
    6372           0 :                     st->tilt_wb_fx = 8192;
    6373           0 :                     move16();
    6374             :                 }
    6375             :                 ELSE
    6376             :                 {
    6377           0 :                     st->tilt_wb_fx = 16384;
    6378           0 :                     move16();
    6379             :                 }
    6380             :             }
    6381             :             ELSE
    6382             :             {
    6383           0 :                 st->tilt_wb_fx = shl( st->tilt_wb_fx, 2 );
    6384           0 :                 move16();
    6385             :             }
    6386             :         }
    6387             : 
    6388          92 :         IF( ener_fx != 0 )
    6389             :         {
    6390          92 :             L_tmp = L_shl( L_mult0( ener_fx, st->tilt_wb_fx ), sub( st->Q_syn2, 13 ) ); /* 2+11 +st->Q_syn2 -13 = st->Q_syn2*/
    6391          92 :             exp_ener = norm_s( ener_fx );
    6392          92 :             tmp = shl( ener_fx, exp_ener );           /*Q(2+exp)*/
    6393          92 :             inv_ener = shr( div_s( 16384, tmp ), 1 ); /*Q(15+14-2-exp-1) = 26 - exp*/
    6394             : 
    6395          92 :             test();
    6396          92 :             IF( GT_32( L_tmp, st->enerLH_fx ) ) /*st->Q_syn2*/
    6397             :             {
    6398           0 :                 st->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st->enerLH_fx, inv_ener ), sub( sub( st->Q_syn2, exp_ener ), 16 ) ) ); /*Q11*/
    6399           0 :                 move16();
    6400             :                 /*st->Q_syn2 -1 + 26- exp_ener -15 -(st->Q_syn2 -exp_ener -16 ) -16    +1 -1 = (11) *0.5*/
    6401             :             }
    6402          92 :             ELSE IF( LT_32( L_tmp, Mult_32_16( st->enerLH_fx, 1638 ) ) && EQ_16( is_fractive, 1 ) )
    6403             :             {
    6404           0 :                 st->tilt_wb_fx = extract_h( L_shr_sat( Mult_32_16( st->enerLH_fx, inv_ener ), sub( sub( st->Q_syn2, exp_ener ), 15 ) ) ); /*Q11*/
    6405           0 :                 move16();
    6406             :                 /*st->Q_syn2 -1 + 26- exp_ener -15 -(st->Q_syn2 -exp_ener -15 ) -16 = (11) 0.25*/
    6407             :             }
    6408          92 :             L_tmp = L_mult0( st->prev_ener_shb_fx, inv_ener );         /*Q(1+15+14-3-exp_ener) = 27 -exp_ener*/
    6409          92 :             GainFrame_prevfrm_fx = L_shr( L_tmp, sub( 9, exp_ener ) ); /*27 -exp_ener -(9-exp_ener )= Q18*/
    6410             :         }
    6411             :         ELSE
    6412             :         {
    6413           0 :             GainFrame_prevfrm_fx = 0;
    6414           0 :             move32();
    6415             :         }
    6416             : 
    6417          92 :         IF( EQ_16( is_fractive, 1 ) )
    6418             :         {
    6419           0 :             GainFrame_fx = L_shl( L_deposit_l( st->tilt_wb_fx ), 10 );
    6420             :         }
    6421             :         ELSE
    6422             :         {
    6423          92 :             GainFrame_fx = L_shl( L_deposit_l( st->tilt_wb_fx ), 8 );
    6424             :         }
    6425             : 
    6426          92 :         test();
    6427          92 :         IF( EQ_16( ( is_fractive & st->prev_fractive ), 1 ) && GT_32( GainFrame_fx, GainFrame_prevfrm_fx ) )
    6428             :         {
    6429           0 :             GainFrame_fx = L_add( Mult_32_16( GainFrame_prevfrm_fx, 26214 ), Mult_32_16( GainFrame_fx, 6554 ) ); /* 18 +15 -15 = 18*/
    6430             :         }
    6431             :         ELSE
    6432             :         {
    6433          92 :             test();
    6434          92 :             test();
    6435          92 :             test();
    6436          92 :             test();
    6437          92 :             IF( ( LT_32( L_shr( st->prev_enerLH_fx, 1 ), st->enerLH_fx ) && GT_32( st->prev_enerLH_fx, L_shr( st->enerLH_fx, 1 ) ) ) && ( LT_32( L_shr( st->prev_enerLL_fx, 1 ), st->enerLL_fx ) && GT_32( st->prev_enerLL_fx, L_shr( st->enerLL_fx, 1 ) ) ) && ( s_xor( is_fractive, st->prev_fractive ) == 0 ) )
    6438             :             {
    6439           0 :                 GainFrame_fx = L_add( L_shr( GainFrame_fx, 1 ), L_shr( GainFrame_prevfrm_fx, 1 ) );
    6440             :             }
    6441             :             ELSE
    6442             :             {
    6443          92 :                 test();
    6444          92 :                 IF( ( is_fractive == 0 ) && EQ_16( st->prev_fractive, 1 ) )
    6445             :                 {
    6446           0 :                     L_tmp1 = L_shl( Mult_32_16( GainFrame_fx, 3277 ), 13 );                                                /* 31 */
    6447           0 :                     L_tmp = L_sub( 2147483647, L_tmp1 );                                                                   /* 31 */
    6448           0 :                     GainFrame_fx = L_add( Mult_32_32( GainFrame_fx, L_tmp ), Mult_32_32( GainFrame_prevfrm_fx, L_tmp1 ) ); /* 18 */
    6449             :                 }
    6450             :                 ELSE
    6451             :                 {
    6452          92 :                     GainFrame_fx = L_add( L_shr( GainFrame_fx, 1 ), L_shr( L_min( GainFrame_prevfrm_fx, GainFrame_fx ), 1 ) ); /* 18 */
    6453             :                 }
    6454             :             }
    6455             :         }
    6456             : 
    6457          92 :         GainFrame_fx = Mult_32_16( GainFrame_fx, i_mult( sub( N_WS2N_FRAMES, st->bws_cnt ), 819 ) ); /*Q18*/
    6458             :     }
    6459             :     ELSE
    6460             :     {
    6461      102462 :         IF( st->bws_cnt1 > 0 )
    6462             :         {
    6463         262 :             GainFrame_fx = Mult_32_16( GainFrame_fx, i_mult( st->bws_cnt1, 819 ) ); /*Q18*/
    6464             :         }
    6465      102462 :         IF( GE_16( st->nbLostCmpt, 1 ) )
    6466             :         {
    6467        2127 :             ener_fx = s_max( 1, ener_fx );
    6468        2127 :             exp_ener = norm_s( ener_fx );
    6469        2127 :             tmp = shl( ener_fx, exp_ener );                                                                            /*Q(2+exp)*/
    6470        2127 :             inv_ener = div_s( 16384, tmp );                                                                            /*Q(15+14-2-exp)*/
    6471        2127 :             prev_ener_ratio_fx = L_shr_sat( L_mult0( st->prev_ener_shb_fx, inv_ener ), add( sub( 9, exp_ener ), 1 ) ); /*Q: 1+27-exp-9+exp-1 = 18 */
    6472             :         }
    6473             : 
    6474      102462 :         IF( EQ_16( st->nbLostCmpt, 1 ) )
    6475             :         {
    6476        1189 :             test();
    6477        1189 :             test();
    6478        1189 :             test();
    6479        1189 :             test();
    6480        1189 :             test();
    6481        1189 :             test();
    6482        1189 :             test();
    6483        1189 :             test();
    6484        1189 :             test();
    6485        1189 :             test();
    6486        1189 :             IF( ( st->clas_dec != UNVOICED_CLAS ) && NE_16( st->clas_dec, UNVOICED_TRANSITION ) && LT_16( hBWE_TD->tilt_swb_fec_fx, 16384 ) &&
    6487             :                 ( ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) && LT_32( L_shr( st->enerLL_fx, 1 ), st->prev_enerLL_fx ) ) || ( GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) && LT_32( L_shr( st->enerLH_fx, 1 ), st->prev_enerLH_fx ) ) ) )
    6488             :             {
    6489           0 :                 IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) ) /*18*/
    6490             :                 {
    6491           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 13107 ), Mult_32_16( GainFrame_fx, 19661 ) ); /*18*/
    6492             :                 }
    6493           0 :                 ELSE IF( GT_32( L_shr( prev_ener_ratio_fx, 1 ), GainFrame_fx ) )
    6494             :                 {
    6495           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 26214 ), Mult_32_16( GainFrame_fx, 6554 ) );
    6496             :                 }
    6497             :                 ELSE
    6498             :                 {
    6499           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame_fx, 26214 ) );
    6500             :                 }
    6501             : 
    6502           0 :                 test();
    6503           0 :                 IF( GT_16( tilt_swb_fec_fx, hBWE_TD->tilt_swb_fec_fx ) && ( hBWE_TD->tilt_swb_fec_fx > 0 ) )
    6504             :                 {
    6505           0 :                     exp = norm_s( hBWE_TD->tilt_swb_fec_fx );
    6506           0 :                     tmp = shl( hBWE_TD->tilt_swb_fec_fx, exp );                                 /*Q(11+exp)*/
    6507           0 :                     tmp = div_s( 16384, tmp );                                                  /*Q(15+14-11-exp)*/
    6508           0 :                     tmp = extract_h( L_shl( L_mult0( tmp, st->tilt_wb_fx ), sub( exp, 1 ) ) );  /*18 -exp +11  + exp -1 -16  =12;          */
    6509           0 :                     GainFrame_fx = L_shl( Mult_32_16( GainFrame_fx, s_min( tmp, 20480 ) ), 3 ); /*Q18 = 18 +12 -15 +3 */
    6510             :                 }
    6511             :             }
    6512        1189 :             ELSE IF( ( ( st->clas_dec != UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 4096 ) ) && GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) &&
    6513             :                      ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) )
    6514             :             {
    6515           0 :                 GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 ), Mult_32_16( GainFrame_fx, 26214 ) );
    6516             :             }
    6517             :         }
    6518      101273 :         ELSE IF( GT_16( st->nbLostCmpt, 1 ) )
    6519             :         {
    6520         938 :             test();
    6521         938 :             test();
    6522         938 :             test();
    6523         938 :             test();
    6524         938 :             test();
    6525         938 :             test();
    6526         938 :             test();
    6527         938 :             test();
    6528         938 :             IF( GT_32( L_shr( prev_ener_ratio_fx, 2 ), GainFrame_fx ) && ( ( EQ_16( st->codec_mode, MODE1 ) && GT_32( st->enerLL_fx, st->prev_enerLL_fx ) && GT_32( st->enerLH_fx, st->prev_enerLH_fx ) ) || EQ_16( st->codec_mode, MODE2 ) ) )
    6529             :             {
    6530           0 :                 test();
    6531           0 :                 IF( GT_16( tilt_swb_fec_fx, 20480 /*10.0f in Q11*/ ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 /*10.0f in Q11*/ ) )
    6532             :                 {
    6533           0 :                     GainFrame_fx = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 26214 /*0.8f in Q15*/ ), Mult_32_16( GainFrame_fx, 6554 /*0.2f in Q15*/ ) ), L_shl( Mult_32_16( GainFrame_fx, 16384 /*4.0f in Q12*/ ), 3 ) ); /*Q18*/
    6534             :                 }
    6535             :                 ELSE
    6536             :                 {
    6537           0 :                     GainFrame_fx = L_min( L_add( Mult_32_16( prev_ener_ratio_fx, 16384 ), Mult_32_16( GainFrame_fx, 16384 ) ), L_shl( Mult_32_16( GainFrame_fx, 16384 ), 3 ) ); /*Q18*/
    6538             :                 }
    6539             :             }
    6540         938 :             ELSE IF( GT_32( prev_ener_ratio_fx, GainFrame_fx ) && ( ( EQ_16( st->codec_mode, MODE1 ) && GT_32( st->enerLL_fx, st->prev_enerLL_fx ) && GT_32( st->enerLH_fx, st->prev_enerLH_fx ) ) || EQ_16( st->codec_mode, MODE2 ) ) )
    6541             :             {
    6542           0 :                 test();
    6543           0 :                 IF( GT_16( tilt_swb_fec_fx, 20480 ) && GT_16( hBWE_TD->tilt_swb_fec_fx, 20480 ) )
    6544             :                 {
    6545           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 16384 /*0.5f in Q15*/ ), Mult_32_16( GainFrame_fx, 16384 /*0.5f in Q15*/ ) ); /* Q18  */
    6546             :                 }
    6547             :                 ELSE
    6548             :                 {
    6549           0 :                     GainFrame_fx = L_add( Mult_32_16( prev_ener_ratio_fx, 6554 /*0.2f in Q15*/ ), Mult_32_16( GainFrame_fx, 26214 /*0.8f in Q15*/ ) ); /* Q18  */
    6550             :                 }
    6551             :             }
    6552             :         }
    6553             :     }
    6554             : 
    6555      102554 :     st->prev_fractive = is_fractive;
    6556      102554 :     move16();
    6557             : 
    6558             :     /* Adjust the subframe and frame gain of the synthesized shb signal */
    6559             :     /* Scale the shaped excitation */
    6560      102554 :     IF( EQ_16( st->L_frame, L_FRAME ) )
    6561             :     {
    6562       48570 :         L_tmp = L_mult( pitch_buf_fx[0], 8192 );
    6563      194280 :         FOR( i = 1; i < NB_SUBFR; i++ )
    6564             :         {
    6565      145710 :             L_tmp = L_mac( L_tmp, pitch_buf_fx[i], 8192 ); /* pitch_buf in Q6 x 0.25 in Q15 */
    6566             :         }
    6567       48570 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    6568             :     }
    6569             :     ELSE
    6570             :     {
    6571       53984 :         L_tmp = L_mult( pitch_buf_fx[0], 6554 );
    6572      269920 :         FOR( i = 1; i < NB_SUBFR16k; i++ )
    6573             :         {
    6574      215936 :             L_tmp = L_mac( L_tmp, pitch_buf_fx[i], 6554 ); /* pitch_buf in Q6 x 0.2 in Q15 */
    6575             :         }
    6576       53984 :         pitch_fx = round_fx( L_tmp ); /* Q6 */
    6577             :     }
    6578             : 
    6579      102554 :     test();
    6580      102554 :     test();
    6581      102554 :     test();
    6582      102554 :     test();
    6583      102554 :     test();
    6584      102554 :     test();
    6585      102554 :     test();
    6586      102554 :     test();
    6587      102554 :     test();
    6588      102554 :     test();
    6589      102554 :     test();
    6590      102554 :     test();
    6591      102554 :     IF( ( ( GE_32( st->extl_brate, SWB_TBE_2k8 ) && EQ_16( st->prev_coder_type, st->coder_type ) && NE_16( st->coder_type, UNVOICED ) ) || ( LT_32( st->extl_brate, SWB_TBE_2k8 ) && ( EQ_16( st->prev_coder_type, st->coder_type ) || ( EQ_16( st->prev_coder_type, VOICED ) && EQ_16( st->coder_type, GENERIC ) ) || ( EQ_16( st->prev_coder_type, GENERIC ) && EQ_16( st->coder_type, VOICED ) ) ) ) ) && GT_16( pitch_fx, 4480 /*70 in Q6*/ ) && LT_16( st->extl, FB_TBE ) && NE_32( st->extl_brate, SWB_TBE_1k10 ) && NE_32( st->extl_brate, SWB_TBE_1k75 ) )
    6592             :     {
    6593      144110 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6594             :         {
    6595      115288 :             GainShape_tmp_fx[i] = GainShape_fx[i * 4]; /* Q15 */
    6596      115288 :             move16();
    6597             :         }
    6598             : 
    6599      144110 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6600             :         {
    6601      115288 :             L_tmp1 = Mult_32_16( ener_tmp_fx[i], GainShape_tmp_fx[i] );               /* (2*Q_bwe_exc) */
    6602      115288 :             L_tmp2 = Mult_32_16( hBWE_TD->prev_ener_fx, hBWE_TD->prev_GainShape_fx ); /* (2*st->prev_ener_fx_Q) */
    6603      115288 :             tmp = sub( shl( Q_bwe_exc, 1 ), shl( hBWE_TD->prev_ener_fx_Q, 1 ) );
    6604      115288 :             L_tmp2 = L_shl_sat( L_tmp2, tmp ); /* new Q = (2*Q_bwe_exc) */
    6605      115288 :             IF( GT_32( L_tmp1, L_tmp2 ) )
    6606             :             {
    6607       67696 :                 L_tmp = L_tmp2;
    6608       67696 :                 move32();
    6609       67696 :                 if ( L_tmp2 < 0 )
    6610             :                 {
    6611           0 :                     L_tmp = L_negate( L_tmp2 );
    6612             :                 }
    6613             : 
    6614       67696 :                 expb = norm_l( L_tmp );
    6615       67696 :                 fracb = round_fx_sat( L_shl_sat( L_tmp, expb ) );
    6616       67696 :                 expb = sub( 30, expb ); /* - (2*Q_bwe_exc_ext);                    */
    6617             : 
    6618       67696 :                 expa = norm_l( ener_tmp_fx[i] );
    6619       67696 :                 fraca = extract_h( L_shl( ener_tmp_fx[i], expa ) );
    6620       67696 :                 expa = sub( 30, expa );
    6621             : 
    6622       67696 :                 scale_fx = shr( sub( fraca, fracb ), 15 );
    6623       67696 :                 fracb = shl( fracb, scale_fx );
    6624       67696 :                 expb = sub( expb, scale_fx );
    6625             : 
    6626       67696 :                 tmp = div_s( fracb, fraca );
    6627       67696 :                 exp = sub( sub( expb, expa ), 1 );
    6628       67696 :                 tmp = shl( tmp, exp );
    6629       67696 :                 GainShape_tmp_fx[i] = add( tmp, shr( GainShape_tmp_fx[i], 1 ) ); /* Q15 */
    6630       67696 :                 move16();
    6631             :             }
    6632             : 
    6633      115288 :             hBWE_TD->prev_ener_fx = ener_tmp_fx[i];
    6634      115288 :             move32();
    6635      115288 :             hBWE_TD->prev_GainShape_fx = GainShape_tmp_fx[i];
    6636      115288 :             move16();
    6637      115288 :             hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    6638      115288 :             move16();
    6639             :         }
    6640             : 
    6641      489974 :         FOR( i = 0; i < NUM_SHB_SUBFR; i++ )
    6642             :         {
    6643      461152 :             Word16 idx = 0;
    6644      461152 :             move16();
    6645      461152 :             IF( i != 0 )
    6646             :             {
    6647      432330 :                 idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
    6648             :             }
    6649      461152 :             GainShape_fx[i] = GainShape_tmp_fx[idx];
    6650      461152 :             move16();
    6651             :         }
    6652             :     }
    6653             :     ELSE
    6654             :     {
    6655       73732 :         hBWE_TD->prev_ener_fx_Q = Q_bwe_exc;
    6656       73732 :         move16();
    6657             :     }
    6658      102554 :     hBWE_TD->prev_Q_bwe_syn = Q_bwe_exc;
    6659      102554 :     move16();
    6660             : 
    6661             : 
    6662             :     /* Gain shape smoothing after quantization */
    6663      102554 :     test();
    6664      102554 :     IF( EQ_32( st->extl_brate, SWB_TBE_1k10 ) || EQ_32( st->extl_brate, SWB_TBE_1k75 ) )
    6665             :     {
    6666       20530 :         FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6667             :         {
    6668       16424 :             GainShape_tmp_fx[i] = GainShape_fx[i * NUM_SHB_SUBGAINS];
    6669       16424 :             move16();
    6670             :         }
    6671             : 
    6672        4106 :         lls_interp_n_fx( GainShape_tmp_fx, NUM_SHB_SUBGAINS, &GainShape_tilt_fx, &temp_fx, 1 );
    6673             : 
    6674        4106 :         test();
    6675        4106 :         IF( GE_16( vind, 6 ) && LT_16( abs_s( GainShape_tilt_fx ), 3932 ) )
    6676             :         {
    6677        2465 :             feedback_fx = 9830;
    6678        2465 :             move16();
    6679       12325 :             FOR( i = 0; i < NUM_SHB_SUBGAINS; i++ )
    6680             :             {
    6681        9860 :                 GainShape_fx[i] = add_sat( mult( sub( 32767, feedback_fx ), GainShape_fx[i * NUM_SHB_SUBGAINS] ), mult( feedback_fx, GainShape_tmp_fx[i] ) );
    6682        9860 :                 move16();
    6683             :             }
    6684             : 
    6685       39440 :             FOR( i = NUM_SHB_SUBFR - 1; i > 0; i-- )
    6686             :             {
    6687       36975 :                 Word16 idx = 0;
    6688       36975 :                 move16();
    6689       36975 :                 IF( i != 0 )
    6690             :                 {
    6691       36975 :                     idx = idiv1616( i_mult( i, NUM_SHB_SUBGAINS ), NUM_SHB_SUBFR );
    6692             :                 }
    6693       36975 :                 GainShape_fx[i] = GainShape_fx[idx];
    6694       36975 :                 move16();
    6695             :             }
    6696             :         }
    6697             :     }
    6698             : 
    6699             :     /* fil-in missing memory */
    6700      102554 :     test();
    6701      102554 :     test();
    6702      102554 :     IF( ( EQ_16( st->element_mode, IVAS_CPE_TD ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) && LE_32( st->last_core_brate, SID_2k40 ) )
    6703             :     {
    6704        3360 :         FOR( i = 0; i < L_SHB_LAHEAD; i++ )
    6705             :         {
    6706        3200 :             Word16 intermediate = mult( shaped_shb_excitation_fx[i], subwin_shb_fx[L_SHB_LAHEAD - i] );
    6707        3200 :             Word32 intermediate_32 = Mpy_32_16_1( Mpy_32_16_1( GainFrame_fx, window_shb_fx[L_SHB_LAHEAD - 1 - i] ), intermediate );
    6708        3200 :             hBWE_TD->syn_overlap_fx[i] = round_fx( L_shl_sat( intermediate_32, sub( 16, ( add( Q_bwe_exc, 18 - 15 ) ) ) ) );
    6709        3200 :             move16();
    6710             :         }
    6711             :     }
    6712             : 
    6713      102554 :     Word16 n_mem3_new = 0;
    6714      102554 :     move16();
    6715      102554 :     find_max_mem_dec_m3( st, &n_mem3_new );
    6716             : 
    6717      102554 :     ScaleShapedSHB_fx( SHB_OVERLAP_LEN,
    6718             :                        shaped_shb_excitation_fx, /* i/o: Q_bwe_exc */
    6719      102554 :                        hBWE_TD->syn_overlap_fx,
    6720             :                        GainShape_fx, /* Q15  */
    6721             :                        GainFrame_fx, /* Q18  */
    6722             :                        window_shb_fx,
    6723             :                        subwin_shb_fx,
    6724      102554 :                        &Q_bwe_exc, &Qx, n_mem3_new, hBWE_TD->prev_Q_bwe_syn2 );
    6725             : 
    6726      102554 :     IF( hStereoICBWE != NULL )
    6727             :     {
    6728       16066 :         Copy_Scale_sig_16_32_DEPREC( lpc_shb_fx, hStereoICBWE->lpSHBRef_fx, LPC_SHB_ORDER + 1, 0 );
    6729       16066 :         Copy( GainShape_fx, hStereoICBWE->gshapeRef_fx, NUM_SHB_SUBFR );
    6730       16066 :         hStereoICBWE->gFrameRef_fx = GainFrame_fx;
    6731       16066 :         move32();
    6732             : 
    6733       16066 :         Copy( shaped_shb_excitation_fx, hStereoICBWE->shbSynthRef_fx, L_FRAME16k );
    6734             :     }
    6735             : 
    6736      102554 :     max_val = 0;
    6737      102554 :     move16();
    6738    32919834 :     FOR( i = 0; i < L_FRAME16k; i++ )
    6739             :     {
    6740    32817280 :         max_val = s_max( max_val, abs_s( shaped_shb_excitation_fx[i] ) ); /* Q0 */
    6741             :     }
    6742      102554 :     IF( max_val == 0 )
    6743             :     {
    6744         110 :         curr_frame_pow_fx = 0;
    6745         110 :         move32();
    6746         110 :         n = 0;
    6747         110 :         move16();
    6748             :     }
    6749             :     ELSE
    6750             :     {
    6751      102444 :         n = norm_s( max_val );
    6752      102444 :         max_val = 0;
    6753      102444 :         move16();
    6754    32884524 :         FOR( i = 0; i < L_FRAME16k; i++ )
    6755             :         {
    6756    32782080 :             shaped_shb_excitation_frac[i] = shl_sat( shaped_shb_excitation_fx[i], n ); /*Q_bwe_exc+n*/
    6757    32782080 :             move16();
    6758             :         }
    6759             : 
    6760      102444 :         curr_frame_pow_fx = 0;
    6761      102444 :         move32();
    6762    32884524 :         FOR( i = 0; i < L_FRAME16k; i++ )
    6763             :         {
    6764    32782080 :             L_tmp = L_mult0( shaped_shb_excitation_frac[i], shaped_shb_excitation_frac[i] ); /*2*(Q_bwe_exc+n)*/
    6765    32782080 :             curr_frame_pow_fx = L_add( curr_frame_pow_fx, L_shr( L_tmp, 9 ) );               /*2*(Q_bwe_exc+n)-9*/
    6766             :         }
    6767             :     }
    6768      102554 :     curr_frame_pow_exp = sub( shl( add( Q_bwe_exc, n ), 1 ), 9 );
    6769      102554 :     tmp = sub( hBWE_TD->prev_frame_pow_exp, curr_frame_pow_exp );
    6770      102554 :     IF( tmp > 0 ) /* shifting prev */
    6771             :     {
    6772         882 :         IF( GT_16( tmp, 32 ) )
    6773             :         {
    6774           0 :             hBWE_TD->prev_frame_pow_exp = add( curr_frame_pow_exp, 32 );
    6775           0 :             move16();
    6776           0 :             tmp = 32;
    6777           0 :             move16();
    6778             :         }
    6779         882 :         hBWE_TD->prev_swb_bwe_frame_pow_fx = L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, tmp );
    6780         882 :         move32();
    6781         882 :         hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    6782         882 :         move16();
    6783             :     }
    6784             :     ELSE /* shifting curr */
    6785             :     {
    6786      101672 :         IF( LT_16( tmp, -32 ) )
    6787             :         {
    6788         270 :             curr_frame_pow_exp = sub( hBWE_TD->prev_frame_pow_exp, 32 );
    6789         270 :             tmp = -32;
    6790         270 :             move16();
    6791             :         }
    6792      101672 :         curr_frame_pow_fx = L_shr( curr_frame_pow_fx, -tmp );
    6793      101672 :         curr_frame_pow_exp = hBWE_TD->prev_frame_pow_exp;
    6794      101672 :         move16();
    6795             :     }
    6796      102554 :     test();
    6797      102554 :     test();
    6798      102554 :     IF( !st->bfi && ( st->prev_bfi || st->prev_use_partial_copy ) )
    6799             :     {
    6800        1209 :         test();
    6801        1209 :         test();
    6802        1209 :         IF( ( GT_32( L_shr( curr_frame_pow_fx, 1 ), hBWE_TD->prev_swb_bwe_frame_pow_fx ) ) &&
    6803             :             ( GT_32( hBWE_TD->prev_swb_bwe_frame_pow_fx, L_tmp ) ) && EQ_16( st->prev_coder_type, UNVOICED ) )
    6804             :         {
    6805           0 :             L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6806           0 :             scale_fx = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    6807             : 
    6808           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6809           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6810           0 :             L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6811           0 :             temp_fx = round_fx( L_shl( L_tmp, exp ) ); /*Q15*/
    6812             :         }
    6813             :         ELSE
    6814             :         {
    6815        1209 :             scale_fx = temp_fx = 32767;
    6816        1209 :             move16(); /*Q15*/
    6817        1209 :             move16(); /*Q15*/
    6818             :         }
    6819             : 
    6820       10881 :         FOR( j = 0; j < 8; j++ )
    6821             :         {
    6822        9672 :             GainShape_fx[2 * j] = mult_r( GainShape_fx[2 * j], scale_fx );
    6823        9672 :             move16();
    6824        9672 :             GainShape_fx[2 * j + 1] = mult_r( GainShape_fx[2 * j + 1], scale_fx );
    6825        9672 :             move16();
    6826      396552 :             FOR( i = 0; i < L_FRAME16k / 8; i++ )
    6827             :             {
    6828      386880 :                 shaped_shb_excitation_fx[add( i, j * ( L_FRAME16k / 8 ) )] = mult_r( shaped_shb_excitation_fx[add( i, j * ( L_FRAME16k / 8 ) )], scale_fx );
    6829      386880 :                 move16();
    6830             :             }
    6831             : 
    6832        9672 :             IF( temp_fx > 0 )
    6833             :             {
    6834             :                 /* scale_fx <= temp_fx, due to scale_fx = sqrt( st->prev_swb_bwe_frame_pow_fx/curr_frame_pow_fx ), temp_fx = sqrt( scale_fx, 1.f/8.f )
    6835             :                    and curr_frame_pow_fx > st->prev_swb_bwe_frame_pow_fx -> scale_fx <= 1.0, sqrt(scale_fx, 1.f/8.f) >= scale_fx  */
    6836        9672 :                 IF( LT_16( scale_fx, temp_fx ) )
    6837             :                 {
    6838           0 :                     scale_fx = div_s( scale_fx, temp_fx );
    6839             :                 }
    6840             :                 ELSE
    6841             :                 {
    6842        9672 :                     scale_fx = 32767;
    6843        9672 :                     move16();
    6844             :                 }
    6845             :             }
    6846             :             ELSE
    6847             :             {
    6848           0 :                 scale_fx = 0;
    6849           0 :                 move16();
    6850             :             }
    6851             :         }
    6852             :     }
    6853             : 
    6854             :     /* adjust the FEC frame energy */
    6855      102554 :     IF( st->bfi )
    6856             :     {
    6857        2139 :         scale_fx = temp_fx = 4096;
    6858        2139 :         move16(); /*Q12*/
    6859        2139 :         move16();
    6860        2139 :         IF( EQ_16( st->nbLostCmpt, 1 ) )
    6861             :         {
    6862        1198 :             test();
    6863        1198 :             test();
    6864        1198 :             test();
    6865        1198 :             test();
    6866        1198 :             test();
    6867        1198 :             test();
    6868        1198 :             test();
    6869        1198 :             test();
    6870        1198 :             IF( GT_32( curr_frame_pow_fx, hBWE_TD->prev_swb_bwe_frame_pow_fx ) &&
    6871             :                 NE_16( st->prev_coder_type, UNVOICED ) &&
    6872             :                 ( st->last_good != UNVOICED_CLAS ) )
    6873             :             {
    6874          78 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp ); /*31 - exp*/
    6875          78 :                 scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );                                                                            /*Q12*/
    6876          78 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6877          78 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6878          78 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6879          78 :                 temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6880             :             }
    6881        1120 :             ELSE IF( LT_32( curr_frame_pow_fx, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) && EQ_16( st->nbLostCmpt, 1 ) &&
    6882             :                      ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) &&
    6883             :                      ( EQ_16( st->prev_coder_type, UNVOICED ) || ( st->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    6884             :             {
    6885           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6886           0 :                 scale_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6887           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6888           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6889           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6890           0 :                 temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6891             :             }
    6892             :         }
    6893         941 :         ELSE IF( GT_16( st->nbLostCmpt, 1 ) )
    6894             :         {
    6895         941 :             test();
    6896         941 :             test();
    6897         941 :             test();
    6898         941 :             test();
    6899         941 :             test();
    6900         941 :             IF( GT_32( curr_frame_pow_fx, hBWE_TD->prev_swb_bwe_frame_pow_fx ) )
    6901             :             {
    6902         224 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6903         224 :                 scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6904         224 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6905         224 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6906         224 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6907         224 :                 temp_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6908             :             }
    6909         717 :             ELSE IF( LT_32( curr_frame_pow_fx, L_shr( hBWE_TD->prev_swb_bwe_frame_pow_fx, 1 ) ) &&
    6910             :                      ( GT_32( st->enerLL_fx, L_shr( st->prev_enerLL_fx, 1 ) ) || GT_32( st->enerLH_fx, L_shr( st->prev_enerLH_fx, 1 ) ) ) &&
    6911             :                      ( EQ_16( st->prev_coder_type, UNVOICED ) || ( st->last_good == UNVOICED_CLAS ) || GT_16( hBWE_TD->tilt_swb_fec_fx, 10240 ) ) )
    6912             :             {
    6913           0 :                 L_tmp = root_a_over_b_fx( hBWE_TD->prev_swb_bwe_frame_pow_fx, curr_frame_pow_exp, curr_frame_pow_fx, curr_frame_pow_exp, &exp );
    6914           0 :                 L_tmp = L_min( L_tmp, L_shl_sat( 2, sub( 31, exp ) ) ); /*31 - exp*/
    6915           0 :                 scale_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) );   /*Q12*/
    6916           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6917           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6918           0 :                 L_tmp = root_a_fx( L_tmp, sub( 31, exp ), &exp );
    6919           0 :                 temp_fx = round_fx( L_shl( L_tmp, sub( exp, 3 ) ) ); /*Q12*/
    6920             :             }
    6921             :         }
    6922             : 
    6923       19251 :         FOR( j = 0; j < 8; j++ )
    6924             :         {
    6925       17112 :             GainShape_fx[2 * j] = shl_sat( mult_r( GainShape_fx[2 * j], scale_fx ), 3 );
    6926       17112 :             move16(); /* 15 +12 +3-15 =15*/
    6927       17112 :             GainShape_fx[add( 2 * j, 1 )] = shl_sat( mult_r( GainShape_fx[add( 2 * j, 1 )], scale_fx ), 3 );
    6928       17112 :             move16();
    6929      701592 :             FOR( i = 0; i < 40; i++ )
    6930             :             {
    6931      684480 :                 shaped_shb_excitation_fx[i + j * L_FRAME16k / 8] = shl( mult_r( shaped_shb_excitation_fx[i + j * L_FRAME16k / 8], scale_fx ), 3 );
    6932      684480 :                 move16(); /* Q_bwe_exc +12+3 -15  =Q_bwe_exc*/
    6933             :             }
    6934             : 
    6935       17112 :             IF( temp_fx > 0 )
    6936             :             {
    6937       17112 :                 IF( LT_16( scale_fx, temp_fx ) )
    6938             :                 {
    6939        2400 :                     scale_fx = shr( div_s( scale_fx, temp_fx ), 3 );
    6940             :                 }
    6941             :                 ELSE
    6942             :                 {
    6943       14712 :                     tmp1 = sub( norm_s( scale_fx ), 1 );
    6944       14712 :                     tmp2 = norm_s( temp_fx );
    6945       14712 :                     scale_fx = div_s( shl( scale_fx, tmp1 ), shl( temp_fx, tmp2 ) );
    6946       14712 :                     scale_fx = shr( scale_fx, add( sub( tmp1, tmp2 ), 3 ) );
    6947             :                 }
    6948             :             }
    6949             :             ELSE
    6950             :             {
    6951           0 :                 scale_fx = 0;
    6952           0 :                 move16();
    6953             :             }
    6954             :         }
    6955             :     }
    6956             : 
    6957      102554 :     hBWE_TD->prev_swb_bwe_frame_pow_fx = curr_frame_pow_fx;
    6958      102554 :     move32();
    6959      102554 :     hBWE_TD->prev_frame_pow_exp = curr_frame_pow_exp;
    6960      102554 :     move16();
    6961             : 
    6962      102554 :     Word64 prev_ener_shb64 = 0;
    6963      102554 :     move64();
    6964    32919834 :     FOR( i = 0; i < L_FRAME16k; i++ )
    6965             :     {
    6966    32817280 :         prev_ener_shb64 = W_mac0_16_16( prev_ener_shb64, shaped_shb_excitation_fx[i], shaped_shb_excitation_fx[i] ); /* Q0 */
    6967             :     }
    6968      102554 :     L_prev_ener_shb = W_sat_l( prev_ener_shb64 );
    6969             : 
    6970      102554 :     L_prev_ener_shb = Mult_32_16( L_prev_ener_shb, 26214 ); /* 2*Q_bwe_exc_mod+8; 26214=(1/L_FRAME16k) in Q23 */
    6971      102554 :     st->prev_ener_shb_fx = 0;
    6972      102554 :     move16();
    6973      102554 :     IF( L_prev_ener_shb != 0 )
    6974             :     {
    6975      102413 :         exp = norm_l( L_prev_ener_shb );
    6976      102413 :         tmp = extract_h( L_shl( L_prev_ener_shb, exp ) );
    6977      102413 :         exp = sub( exp, sub( 30, ( add( i_mult( 2, Q_bwe_exc ), 8 ) ) ) );
    6978             : 
    6979      102413 :         tmp = div_s( 16384, tmp );
    6980      102413 :         L_tmp = L_deposit_h( tmp );
    6981      102413 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    6982      102413 :         st->prev_ener_shb_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    6983      102413 :         move16();
    6984             :     }
    6985             : 
    6986      102554 :     IF( st->hBWE_FD != NULL )
    6987             :     {
    6988      102554 :         L_tmp = Mult_32_16( curr_frame_pow_fx, 26214 ); /* curr_frame_pow_exp+8; 26214=(1/L_FRAME16k) in Q23 */
    6989      102554 :         tmp = 0;
    6990      102554 :         move16();
    6991      102554 :         IF( L_tmp != 0 )
    6992             :         {
    6993      101687 :             exp = norm_l( L_tmp );
    6994      101687 :             tmp = extract_h( L_shl( L_tmp, exp ) );
    6995      101687 :             exp = sub( exp, sub( 30, add( curr_frame_pow_exp, 8 ) ) );
    6996             : 
    6997      101687 :             tmp = div_s( 16384, tmp );
    6998      101687 :             L_tmp = L_deposit_h( tmp );
    6999      101687 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    7000      101687 :             tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /* Q1 */
    7001             :         }
    7002      102554 :         set16_fx( st->hBWE_FD->prev_SWB_fenv_fx, tmp, SWB_FENV ); /* Q1 */
    7003             :     }
    7004             : 
    7005    32919834 :     FOR( i = 0; i < L_FRAME16k; i++ )
    7006             :     {
    7007    32817280 :         shaped_shb_excitation_fx_32[i] = L_shl( shaped_shb_excitation_fx[i], sub( Q11, Q_bwe_exc ) );
    7008    32817280 :         move32();
    7009             :     }
    7010             : 
    7011             :     /* generate 32kHz SHB synthesis from 12.8(16)kHz signal */
    7012      102554 :     GenSHBSynth_fx32( shaped_shb_excitation_fx_32, error_fx, hBWE_TD->genSHBsynth_Hilbert_Mem_fx, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, st->L_frame, &( hBWE_TD->syn_dm_phase ) );
    7013      102554 :     Copy_Scale_sig_32_16( st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, st->hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, 2 * ALLPASSSECTIONS_STEEP, -( Q11 - Q_bwe_exc ) );
    7014      102554 :     Copy32( error_fx + L_FRAME32k - L_SHB_TRANSITION_LENGTH, hBWE_TD->old_tbe_synth_fx_32, L_SHB_TRANSITION_LENGTH );
    7015             : 
    7016             :     /* resample SHB synthesis (if needed) and scale down */
    7017      102554 :     synth_scale_fx = 32767;
    7018      102554 :     move16(); /* 1.0 in Q15 */
    7019      102554 :     if ( EQ_16( st->codec_mode, MODE1 ) )
    7020             :     {
    7021      102554 :         synth_scale_fx = 29491;
    7022      102554 :         move16(); /* 0.9 in Q15 */
    7023             :     }
    7024             : 
    7025      102554 :     IF( EQ_32( st->output_Fs, 48000 ) )
    7026             :     {
    7027       70675 :         IF( EQ_16( st->extl, FB_TBE ) )
    7028             :         {
    7029       34519 :             tmp = norm_l( GainFrame_fx );
    7030       34519 :             if ( GainFrame_fx == 0 )
    7031             :             {
    7032           0 :                 tmp = 31;
    7033           0 :                 move16();
    7034             :             }
    7035       34519 :             L_tmp = L_shl( GainFrame_fx, tmp ); /* 18 + tmp */
    7036             : 
    7037       34519 :             tmp1 = 0;
    7038       34519 :             move16();
    7039             : 
    7040             : #ifdef FIX_1439_SPEEDUP_ivas_swb_tbe_dec_fx
    7041       34519 :             Word32 idx32 = L_shr_r( 0x00333333, 10 ); /*NUM_SHB_SUBFR/L_FRAME16k*/ // Q16
    7042             : #endif
    7043             : 
    7044    11080599 :             FOR( i = 0; i < L_FRAME16k; i++ )
    7045             :             {
    7046             : #ifndef FIX_1439_SPEEDUP_ivas_swb_tbe_dec_fx
    7047             :                 Word16 idx = 0;
    7048             :                 move16();
    7049             :                 IF( i != 0 )
    7050             :                 {
    7051             :                     idx = idiv1616( i_mult( NUM_SHB_SUBFR, i ), L_FRAME16k );
    7052             :                 }
    7053             : #else
    7054             :                 Word16 idx;
    7055    11046080 :                 idx = extract_h( imult3216( idx32, i ) ); /*Q0*/
    7056             : #endif
    7057    11046080 :                 L_tmp1 = Mult_32_16( L_tmp, GainShape_fx[idx] );                           /* Q : 18 + tmp +15 -15*/
    7058    11046080 :                 White_exc16k_fx[i] = round_fx( Mult_32_16( L_tmp1, White_exc16k_fx[i] ) ); /* 18 + tmp +*Q_white_exc -15 -16 */
    7059    11046080 :                 move16();
    7060    11046080 :                 tmp1 = s_max( tmp1, abs_s( White_exc16k_fx[i] ) );
    7061             :             }
    7062             : 
    7063       34519 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 13 ); /* *Q_white_exc + 18 + tmp -15 -16 */
    7064       34519 :             move16();
    7065       34519 :             tmp = norm_s( tmp1 );
    7066       34519 :             if ( tmp1 == 0 )
    7067             :             {
    7068          21 :                 tmp = 15;
    7069          21 :                 move16();
    7070             :             }
    7071             : 
    7072    11080599 :             FOR( i = 0; i < L_FRAME16k; i++ )
    7073             :             {
    7074    11046080 :                 White_exc16k_fx[i] = shl( White_exc16k_fx[i], sub( tmp, 1 ) );
    7075    11046080 :                 move16();
    7076             :             }
    7077       34519 :             *Q_white_exc = sub( add( *Q_white_exc, tmp ), 1 );
    7078       34519 :             move16();
    7079             :         }
    7080             : 
    7081       70675 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    7082             :         {
    7083    45302675 :             FOR( i = 0; i < L_FRAME32k; i++ )
    7084             :             {
    7085    45232000 :                 error_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
    7086    45232000 :                 move32();
    7087             :             }
    7088             :         }
    7089       70675 :         interpolate_3_over_2_allpass_fx32( error_fx, L_FRAME32k, synth_fx, hBWE_TD->int_3_over_2_tbemem_dec_fx_32 );
    7090             :     }
    7091       31879 :     ELSE IF( EQ_32( st->output_Fs, 32000 ) )
    7092             :     {
    7093       27603 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    7094             :         {
    7095    17693523 :             FOR( i = 0; i < L_FRAME32k; i++ )
    7096             :             {
    7097    17665920 :                 synth_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
    7098    17665920 :                 move32(); /*Qx*/
    7099             :             }
    7100             :         }
    7101             :         ELSE
    7102             :         {
    7103           0 :             Copy32( error_fx, synth_fx, L_FRAME32k );
    7104             :         }
    7105             :     }
    7106        4276 :     ELSE IF( EQ_32( st->output_Fs, 16000 ) )
    7107             :     {
    7108        4276 :         IF( NE_16( synth_scale_fx, 32767 ) ) /* 1.0 in Q15 */
    7109             :         {
    7110     2740916 :             FOR( i = 0; i < L_FRAME32k; i++ )
    7111             :             {
    7112     2736640 :                 error_fx[i] = Mpy_32_16_1( error_fx[i], synth_scale_fx );
    7113     2736640 :                 move32();
    7114             :             }
    7115             :         }
    7116             : 
    7117        4276 :         Decimate_allpass_steep_fx32( error_fx, hBWE_TD->mem_resamp_HB_32k_fx_32, L_FRAME32k, synth_fx );
    7118             :     }
    7119             : 
    7120             :     /* Update previous frame parameters for FEC */
    7121      102554 :     Copy( lsf_shb_fx, hBWE_TD->lsp_prevfrm_fx, LPC_SHB_ORDER );
    7122      102554 :     IF( EQ_16( st->codec_mode, MODE1 ) )
    7123             :     {
    7124      102554 :         hBWE_TD->GainFrame_prevfrm_fx = GainFrame_fx;
    7125      102554 :         move32(); /*Q18*/
    7126      102554 :         hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec_fx;
    7127      102554 :         move16();
    7128             : 
    7129      102554 :         if ( !st->bfi )
    7130             :         {
    7131      100415 :             hBWE_TD->GainAttn_fx = 32767; /*1.0f in Q15*/
    7132      100415 :             move16();
    7133             :         }
    7134             :     }
    7135             :     ELSE
    7136             :     {
    7137           0 :         IF( !st->bfi )
    7138             :         {
    7139           0 :             hBWE_TD->GainFrame_prevfrm_fx = GainFrame_fx;
    7140           0 :             move32(); /*Q18*/
    7141           0 :             hBWE_TD->tilt_swb_fec_fx = tilt_swb_fec_fx;
    7142           0 :             move16();
    7143           0 :             hBWE_TD->GainAttn_fx = 32767; /*1.0f in Q15*/
    7144           0 :             move16();
    7145             :         }
    7146             :     }
    7147             : 
    7148      102554 :     hBWE_TD->prev_ener_fx = ener_tmp_fx[NUM_SHB_SUBGAINS - 1];
    7149      102554 :     move32();
    7150      102554 :     hBWE_TD->prev_GainShape_fx = GainShape_fx[NUM_SHB_SUBFR - 1];
    7151      102554 :     move16();
    7152      102554 :     hBWE_TD->prev_Q_bwe_syn2 = Q_bwe_exc;
    7153      102554 :     move16();
    7154      102554 :     hBWE_TD->prev_Qx = Q_bwe_exc;
    7155      102554 :     move16();
    7156             : 
    7157      102554 :     return;
    7158             : }

Generated by: LCOV version 1.14