LCOV - code coverage report
Current view: top level - lib_dec - FEC_HQ_phase_ecu_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 1369 2574 53.2 %
Date: 2025-09-14 03:13:15 Functions: 22 36 61.1 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include "options.h" /* Compilation switches                   */
       7             : #include "rom_dec.h" /* Static table prototypes                */
       8             : #include "rom_com.h" /* Static table prototypes                */
       9             : #include "prot_fx.h"
      10             : #include "basop_util.h"
      11             : #include "ivas_prot_fx.h"
      12             : 
      13             : /*---------------------------------------------------------------------*
      14             :  * Local constants
      15             :  *---------------------------------------------------------------------*/
      16             : 
      17             : #define FEC_MAX                512
      18             : #define FEC_NB_PULSE_MAX       20
      19             : #define FEC_FFT_MAX_SIZE       512
      20             : #define FEC_DCIM_FILT_SIZE_MAX 60
      21             : 
      22             : #define ENV_STAB_DEC_THR 16384 /* Q15 st->env_stab based threshold for hq-ecu technology decision */
      23             : 
      24             : #define PHASE_DITH_fx              25736 /* 2*pi in Q12 */
      25             : #define DELTA_CORR                 6     /* Tuning parameter - defining range for phase correction around peak */
      26             : #define THRESH_TR_DB_FX            10
      27             : #define THRESH_TR_LIN_BY2_FX       ( 10 / 2 ) /* 10.0^(THRESH_TR_DB_FX/10.0)/2. Divided by 2 to facilitate fixed-point implementation. */
      28             : #define MAX_INCREASE_GRPPOW_FX     0          /* max. amplification in case of transients (in dB scale) */
      29             : #define MAX_INCREASE_GRPPOW_LIN_FX 32767      /* in Q15-- 10.0^(MAX_INCREASE_GRPPOW_FX/10.0)  (in linear scale) */
      30             : 
      31             : #define PHASE_DITH_SCALE_SHIFT 16 /* The number of bit shift equivalent to multiply by PHASE_DITH_SCALE */
      32             : 
      33             : #define BURST_PHDITH_THRESH     ( 4 - 1 ) /* speech start phase dither with <burst_phdith_thresh> losses in a row */
      34             : #define BURST_PHDITH_RAMPUP_LEN 2         /* speech ramp up degree of phase dither over a length of <burst_phdith_rampup_len> frames */
      35             : #define BURST_ATT_THRESH        ( 3 - 1 ) /* speech start attenuate with <burst_att_thresh> losses in a row */
      36             : #define ATT_PER_FRAME           4         /* speech attenuation in dB */
      37             : #define BETA_MUTE_THR           10        /* time threshold to start beta-noise attenuation */
      38             : 
      39             : #define LGW32K 7
      40             : #define LGW16K 6
      41             : #define LGW48K LGW32K + 1 /* Use the same frequency groups as for SWB + 1 */
      42             : 
      43             : #define LTRANALOG32K          8
      44             : #define LTRANALOG16K          7
      45             : #define PFIND_SENS_FX         31785 /* 0.97 in Q15 */
      46             : #define CMPLMNT_PFIND_SENS_FX 983   /* (1.0 - pfind_sen) in Q15 */
      47             : 
      48             : #define DELTA_CORR_F0_INT        2         /* Constant controls the bin range where Jacobsen is used */
      49             : #define L_PROT_NS                32000000L /* Prototype frame length in nanoseconds (32 ms) */
      50             : #define PH_ECU_CORR_LIMIT_Q15    27853     /* 0.85 in Q15 */
      51             : #define PH_ECU_N_LIMIT           56        /* fec_alg analysis frame limit for IVAS Phase ECU activation */
      52             : #define CMPLMNT_ST_PFIND_SENS_FX 2293      /* (1.0 - st_pfind_sen) in Q15 */
      53             : 
      54             : #define FEC_HQ_ECU_POINT5 ( 0x4000 ) /* 0.5 in Q15. Prefix with FEC_HQ namespace to avoid naming conflict. */
      55             : #define FEC_HQ_ECU_ROOT2  ( 0x5a83 ) /* sqrt(2) in Q14 */
      56             : #define FEC_HQ_HAMM_A0    17695      /* 0.54 in Q15 */
      57             : #define FEC_HQ_HAMM_A1    15073      /* 0.46 in Q15 */
      58             : #define FEC_HQ_WIN_A0     FEC_HQ_HAMM_A0
      59             : #define FEC_HQ_WIN_A1     FEC_HQ_HAMM_A1
      60             : 
      61             : static Word16 sqrt2ndOrder( const Word16 );
      62             : 
      63             : static void windowing( const Word16 *, Word16 *, const Word16 *, const Word16, const Word16 );
      64             : static void windowing_ROM_optimized( const Word16 *, Word16 *, const Word16, const Word16, const Word16 );
      65             : static void fft_spec2_fx( const Word16[], Word32[], const Word16 );
      66             : static void trans_ana_fx( const Word16 *, Word16 *, Word16 *, Word16 *, const Word16, const Word16, const Word16, const Word16, Word16 *, Word16 *, Word16 *, Word16 * );
      67             : static void peakfinder_fx( const Word16 *, const Word16, Word16 *, Word16 *, const Word16 );
      68             : static Word16 imax_fx( const Word16 *, const Word16 );
      69             : static void spec_ana_fx( const Word16 *prevsynth, Word16 *plocs, Word32 *plocsi, Word16 *num_plocs, Word16 *X_sav, const Word16 output_frame, const Word16 bwidth_fx, Word16 *Q );
      70             : static void subst_spec_fx( const Word16 *, const Word32 *, Word16 *, const Word16, Word16 *, const Word16 *, const Word16, const Word16 *, const Word16, Word16 *, const Word16 *, const Word16 *, Word16, const Word16 * );
      71             : static Word16 rand_phase_fx( const Word16 seed, Word16 *sin_F, Word16 *cos_F );
      72             : 
      73             : /*------------------------------------------------------------------*
      74             :  * rand_phase()
      75             :  *
      76             :  * randomized phase in form of sin and cos components
      77             :  *------------------------------------------------------------------*/
      78      115272 : static Word16 rand_phase_fx( const Word16 seed, Word16 *sin_F /*Q15*/, Word16 *cos_F /*Q15*/ )
      79             : {
      80      115272 :     const Word16 *sincos = sincos_t_ext_fx + 128; // Q15
      81      115272 :     Word16 seed2 = own_random2_fx( seed );
      82      115272 :     Word16 seed2_shift = shr( seed2, 8 );
      83             : 
      84      115272 :     *sin_F = negate( *( sincos + seed2_shift ) ); // Q15
      85      115272 :     move16();
      86      115272 :     if ( s_and( seed2, 0x40 ) != 0 )
      87             :     {
      88       57586 :         *sin_F = *( sincos + seed2_shift );
      89       57586 :         move16();
      90             :     }
      91             : 
      92      115272 :     *cos_F = negate( *( sincos - seed2_shift ) ); // Q15
      93      115272 :     move16();
      94      115272 :     if ( s_and( seed2, 0x80 ) != 0 )
      95             :     {
      96       57665 :         *cos_F = *( sincos - seed2_shift );
      97       57665 :         move16();
      98             :     }
      99             : 
     100      115272 :     return seed2;
     101             : }
     102             : 
     103             : /*! r: The location, relative to the middle of the 3 given data point, of the maximum. (Q15)*/
     104         886 : static Word16 ivas_imax2_jacobsen_mag_fx(
     105             :     const Word16 *y_re, /* i  : The 3 given data points. real part order -1 0 1 Qx                       */
     106             :     const Word16 *y_im  /* i  : The 3 given data points. imag part order  1 0 -1 (from FFT) Qx          */
     107             : )
     108             : {
     109             :     Word16 posi; // Q15
     110             :     const Word16 *pY;
     111             :     Word16 y_m1_re, y_0_re, y_p1_re;
     112             :     Word16 y_m1_im, y_0_im, y_p1_im;
     113             :     Word32 N_re, N_im;
     114             :     Word32 D_re, D_im;
     115             :     Word16 tmp, tmp_e;
     116             :     Word32 numer, denom;
     117             :     Word64 tmp_64_n, tmp_64_d;
     118             : 
     119             :     /* Jacobsen estimates peak offset relative y_0 using
     120             :      *                 X_m1 - X_p1
     121             :      *  d = REAL ( ------------------- ) * c_jacob
     122             :      *              2*X_0 - X_m1 -Xp1
     123             :      *
     124             :      *  Where c_jacob is a window  dependent constant
     125             :      */
     126             : #define C_JACOB_FX 18765 /*  % assume 0.1875 hammrect window 'symmetric' */ /*Q14*/
     127             : 
     128             :     /* Get the bin parameters into variables */
     129         886 :     pY = y_re;
     130         886 :     y_m1_re = *pY++;
     131         886 :     move16();
     132         886 :     y_0_re = *pY++;
     133         886 :     move16();
     134         886 :     y_p1_re = *pY++;
     135         886 :     move16();
     136             : 
     137             :     /* Same for imaginary parts - note reverse order from FFT */
     138         886 :     pY = y_im;
     139         886 :     y_p1_im = *pY++;
     140         886 :     move16();
     141         886 :     y_0_im = *pY++;
     142         886 :     move16();
     143         886 :     y_m1_im = *pY++;
     144         886 :     move16();
     145             : 
     146             :     /* prepare numerator real and imaginary parts*/
     147         886 :     N_re = L_sub( y_m1_re, y_p1_re );
     148         886 :     N_im = L_sub( y_m1_im, y_p1_im );
     149             : 
     150             :     /* prepare denominator real and imaginary parts */
     151             : 
     152         886 :     D_re = L_sub( L_sub( L_shl( y_0_re, 1 ), y_m1_re ), y_p1_re );
     153         886 :     D_im = L_sub( L_sub( L_shl( y_0_im, 1 ), y_m1_im ), y_p1_im );
     154             : 
     155             :     /* REAL part of complex division  */
     156         886 :     tmp_64_n = W_mac_32_32( W_mult_32_32( N_re, D_re ), N_im, D_im );
     157         886 :     tmp_64_d = W_mac_32_32( W_mult_32_32( D_re, D_re ), D_im, D_im );
     158         886 :     tmp_e = s_min( W_norm( tmp_64_n ), W_norm( tmp_64_d ) );
     159         886 :     numer = W_extract_h( W_shl( tmp_64_n, tmp_e ) );
     160         886 :     denom = W_extract_h( W_shl( tmp_64_d, tmp_e ) );
     161             : 
     162         886 :     test();
     163         886 :     IF( numer != 0 && denom != 0 )
     164             :     {
     165         886 :         tmp = BASOP_Util_Divide3232_Scale( numer, denom, &tmp_e );
     166         886 :         tmp = shl_sat( tmp, tmp_e );                     // Q15
     167         886 :         posi = shl_sat( mult_r( tmp, C_JACOB_FX ), Q1 ); // Q15
     168             :     }
     169             :     ELSE
     170             :     {
     171           0 :         posi = 0; /* flat top,  division is not possible choose center freq */
     172           0 :         move16();
     173             :     }
     174             : 
     175         886 :     return posi;
     176             : }
     177             : 
     178             : /*-----------------------------------------------------------------------------
     179             :  * fft_spec2_fx()
     180             :  *
     181             :  * Square magnitude of fft spectrum
     182             :  *----------------------------------------------------------------------------*/
     183         162 : static void fft_spec2_fx(
     184             :     const Word16 x[], /* i : Input vector: complex spectrum Q0*/
     185             :     Word32 xMagSq[],  /* o : Magnitude square spectrum Q0*/
     186             :     const Word16 N    /* i : Input vector length */
     187             : )
     188             : {
     189             :     Word16 i, l;
     190             :     const Word16 *pRe, *pIm;
     191             :     Word32 *pMagSq, acc;
     192             : 
     193             :     /* Magnitude at 0. */
     194         162 :     pMagSq = &xMagSq[0];
     195         162 :     pRe = &x[0];
     196         162 :     *pMagSq++ = L_mult0( *pRe, *pRe );
     197         162 :     move32();
     198         162 :     pRe++; /* Non-fractional multiply gives subsequent group power accumulation a bit headroom. */
     199             : 
     200             :     /* From 1 to (N/2 - 1). */
     201         162 :     l = sub( shr( N, 1 ), 1 ); /* N/2 - 1. */
     202         162 :     pIm = &x[N];
     203         162 :     pIm--;
     204       51072 :     FOR( i = 0; i < l; i++ )
     205             :     {
     206       50910 :         acc = L_mult0( *pRe, *pRe );
     207       50910 :         pRe++; /* Non-fractional mode multiply. */
     208       50910 :         *pMagSq++ = L_mac0( acc, *pIm, *pIm );
     209       50910 :         pIm--;
     210       50910 :         move32();
     211             :     }
     212             : 
     213             :     /* The magnitude square at N/2 */
     214         162 :     *pMagSq = L_mult0( *pRe, *pRe );
     215         162 :     move32();
     216         162 :     return;
     217             : }
     218             : 
     219             : /*-----------------------------------------------------------------------------
     220             :  * trans_ana_fx()
     221             :  *
     222             :  * Transient analysis
     223             :  *----------------------------------------------------------------------------*/
     224         132 : static void trans_ana_fx(
     225             :     const Word16 *xfp,            /* i  : Input signal                                       Q0  */
     226             :     Word16 *mag_chg,              /* o  : Magnitude modification                             Q15 */
     227             :     Word16 *ph_dith,              /* o  : Phase dither, 2*PI is not included (Q15, i.e., between 0.0 and 1.0) */
     228             :     Word16 *mag_chg_1st,          /* i/o: per band magnitude modifier for transients         Q15 */
     229             :     const Word16 output_frame,    /* i  : Frame length                                           */
     230             :     const Word16 time_offs,       /* i  : Time offset (integral multiple of output_frame)        */
     231             :     const Word16 est_mus_content, /* i  : 0.0=speech_like ... 1.0=Music    (==st->env_stab )     */
     232             :     const Word16 last_fec,        /* i  : signal that previous frame was concealed with fec_alg  */
     233             :     Word16 *alpha,                /* o  : Magnitude modification factors for fade to average     */
     234             :     Word16 *beta,                 /* o  : Magnitude modification factors for fade to average     */
     235             :     Word16 *beta_mute,            /* o  : Factor for long-term mute Q15                              */
     236             :     Word16 *Xavg                  /* o  : Frequency group average gain to fade to                */
     237             : )
     238             : {
     239             :     const Word16 *w_hamm, *pFftTbl;
     240             :     Word16 att_val, attDegreeFrames;
     241             :     Word16 xfp_left[L_TRANA48k], xfp_right[L_TRANA48k];
     242             :     Word32 magSqLeft[L_TRANA48k / 2 + 1], magSqRight[L_TRANA48k / 2 + 1];
     243             :     Word32 *pLeft, *pRight, *pGrPowLeft, *pGrPowRight;
     244             :     Word32 gr_pow_left[LGW_MAX], gr_pow_right[LGW_MAX];
     245             :     const Word16 *pXfp, *pGw;
     246             :     Word16 Ltrana, Ltrana_2, Lprot, three4thLength, LtranaLogMinus1, Lgw, i, k, l, burst_len;
     247             :     Word16 man, expo;
     248         132 :     Word16 att_always = 0; /* fixed attenuation per frequency group if set to  1 */
     249             :     Word16 oneOverFrame, roundEstMusContent, tmp16, headroom, lowerEdge;
     250         132 :     Word16 burst_phdith_thresh = BURST_PHDITH_THRESH; /*speech settings */
     251         132 :     Word16 burst_att_thresh = BURST_ATT_THRESH;
     252         132 :     Word16 att_per_frame = ATT_PER_FRAME;
     253         132 :     Word16 burst_phdith_rampup_len = BURST_PHDITH_RAMPUP_LEN;
     254             :     Word16 tr_dec[LGW_MAX];
     255             :     UWord16 lsb;
     256             :     Word32 acc;
     257             : 
     258         132 :     Lgw = 0;
     259         132 :     move16();
     260         132 :     LtranaLogMinus1 = 0;
     261         132 :     move16();
     262             : 
     263         132 :     move16(); // att_always
     264         132 :     move16(); // burst_phdith_thresh
     265         132 :     move16(); // burst_att_thresh
     266         132 :     move16(); // att_per_frame
     267         132 :     move16(); // burst_phdith_rampup_len
     268             : 
     269         132 :     pFftTbl = NULL;
     270             : 
     271             :     /* Initialisation to prevent warnings */
     272         132 :     oneOverFrame = 102; /* 1/320 in Q15 */
     273         132 :     move16();
     274         132 :     w_hamm = w_hamm16k_2_fx;
     275         132 :     move16();
     276         132 :     Lprot = 512;
     277         132 :     move16();
     278             : 
     279             :     /* check burst error */
     280         132 :     IF( EQ_16( output_frame, L_FRAME48k ) ) /* output_frame = (sampling frequency)/50 */
     281             :     {
     282         103 :         oneOverFrame = 34; /* 1/960 in Q15 */
     283         103 :         move16();
     284         103 :         w_hamm = w_hamm48k_2_fx;
     285         103 :         move16();
     286         103 :         Lgw = LGW48K;
     287         103 :         move16();
     288         103 :         Lprot = 1536; /* (2*output_frame)*1024/1280; */
     289         103 :         move16();
     290             :     }
     291          29 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
     292             :     {
     293          29 :         oneOverFrame = 51; /* 1/640 in Q15 */
     294          29 :         move16();
     295          29 :         w_hamm = w_hamm32k_2_fx;
     296          29 :         Lgw = LGW32K;
     297          29 :         move16();
     298          29 :         Lprot = 1024;
     299          29 :         move16();
     300          29 :         pFftTbl = FFT_W128; /* Table for 256 real input radix-2 FFT */
     301          29 :         LtranaLogMinus1 = LTRANALOG32K - 1;
     302          29 :         move16();
     303             :     }
     304             :     ELSE
     305             :     {
     306           0 :         oneOverFrame = 102; /* 1/320 in Q15 */
     307           0 :         move16();
     308           0 :         w_hamm = w_hamm16k_2_fx;
     309           0 :         Lgw = LGW16K;
     310           0 :         move16();
     311           0 :         Lprot = 512;
     312           0 :         move16();
     313           0 :         pFftTbl = FFT_W64; /* Table for 128 real input radix-2 FFT */
     314           0 :         LtranaLogMinus1 = LTRANALOG16K - 1;
     315           0 :         move16();
     316             :     }
     317         132 :     burst_len = add( mult_r( time_offs, oneOverFrame ), 1 );
     318             : 
     319         132 :     *ph_dith = 0;
     320         132 :     move16(); /*  typical pattern is 2*pi*[0  0 .25 .50 .75 1.0 1.0 1.0 ..] */
     321             : 
     322         132 :     IF( GE_16( output_frame, L_FRAME32k ) ) /*currently est_mus_content only calculated for SWB and FB */
     323             :     {
     324         132 :         roundEstMusContent = 0;
     325         132 :         move16();
     326         132 :         if ( GE_16( est_mus_content, FEC_HQ_ECU_POINT5 ) ) /* est_mus_content is in [0.0, 1.0]. */
     327             :         {
     328         131 :             roundEstMusContent = 1;
     329         131 :             move16();
     330             :         }
     331             : 
     332             :         /* softly shift attenuation just a bit later  for estimated "stable" music_content */
     333         132 :         burst_phdith_thresh = add( BURST_PHDITH_THRESH, roundEstMusContent );
     334         132 :         burst_att_thresh = add( BURST_ATT_THRESH, roundEstMusContent );
     335         132 :         att_per_frame = sub( ATT_PER_FRAME, roundEstMusContent ); /* only slighty less att for music */
     336             :     }
     337             :     ELSE
     338             :     {
     339             :         /* lock to music-like stable envelope  setting for now */
     340           0 :         burst_phdith_thresh = add( BURST_PHDITH_THRESH, 1 ); /* in Q0 */
     341           0 :         burst_att_thresh = add( BURST_ATT_THRESH, 1 );       /* in Q0 */
     342           0 :         att_per_frame = sub( ATT_PER_FRAME, 1 );             /* in Q0 */
     343             :     }
     344             : 
     345         132 :     IF( GT_16( burst_len, burst_phdith_thresh ) )
     346             :     {
     347             :         /* increase degree of dither */
     348             : #if BURST_PHDITH_RAMPUP_LEN != 2
     349             : #error The implementation of phase_dith=min(1.0, (burst_len - burst_phdith_thresh)/burst_phdith_rampup_len)) is incorrect
     350             : #endif
     351          72 :         *ph_dith = 32767; /* 1.0 in Q15. N.B. 2*PI is not included. */
     352          72 :         move16();
     353          72 :         tmp16 = sub( burst_len, burst_phdith_thresh );
     354          72 :         if ( LT_16( tmp16, burst_phdith_rampup_len ) )
     355             :         {
     356           2 :             *ph_dith = 16384; /* 0.5 in Q15. N.B. 2*PI is not included. */
     357           2 :             move16();
     358             :         }
     359             :     }
     360             : 
     361         132 :     attDegreeFrames = 0;
     362         132 :     move16();
     363         132 :     IF( GT_16( burst_len, burst_att_thresh ) )
     364             :     {
     365          74 :         att_always = 1;
     366          74 :         move16();
     367             :         /* increase degree of attenuation */
     368             : 
     369             :         /* N.B. To facilitate the subsequent 10^(-att_degree/20) implementation
     370             :          * so as to use direct table-lookup,
     371             :          * (burstLen - burst_att_thresh) is NOT multiplied by "att_per_frame". */
     372          74 :         attDegreeFrames = sub( burst_len, burst_att_thresh ); /* Not multiplied by att_per_frqme! */
     373             :         /* Furthermore, in order to minimize the size of the lookup-table required to
     374             :          * implement 10^(-att_degree/10), hard limit attDegreeFrames to (30% of 50)=15.
     375             :          * If attDegreeFrames is greater than 15, it means there are more than 15 successive
     376             :          * bad frames. In this case, no matter what to do, the sound quality will be bad.
     377             :          */
     378          74 :         if ( GT_16( attDegreeFrames, OFF_FRAMES_LIMIT ) )
     379             :         {
     380          14 :             attDegreeFrames = OFF_FRAMES_LIMIT; /* Hard limit the no. of frames */
     381          14 :             move16();
     382             :         }
     383             :     }
     384             : 
     385         132 :     Ltrana = shr( Lprot, 2 );
     386         132 :     Ltrana_2 = shr( Ltrana, 1 );
     387             : 
     388         132 :     test();
     389         132 :     test();
     390         132 :     IF( LE_16( burst_len, 1 ) || ( EQ_16( burst_len, 2 ) && last_fec != 0 ) )
     391             :     {
     392             : 
     393          54 :         set16_fx( alpha, 32767, LGW_MAX );
     394          54 :         set16_fx( beta, 0, LGW_MAX );
     395          54 :         *beta_mute = BETA_MUTE_FAC_INI;
     396          54 :         move16();
     397             : 
     398             :         /* Apply Hamming window */
     399          54 :         windowing( xfp, xfp_left, w_hamm, 0, Ltrana_2 ); /* 1st quarter */
     400          54 :         three4thLength = sub( Lprot, Ltrana );
     401          54 :         pXfp = xfp + three4thLength;
     402          54 :         windowing( pXfp, xfp_right, w_hamm, 0, Ltrana_2 ); /* 4th quarter */
     403             : 
     404             :         /* spectrum */
     405          54 :         IF( EQ_16( output_frame, L_FRAME48k ) )
     406             :         {
     407          25 :             fft3_fx( xfp_left, xfp_left, Ltrana );
     408          25 :             fft3_fx( xfp_right, xfp_right, Ltrana );
     409             :         }
     410             :         ELSE
     411             :         {
     412          29 :             r_fft_fx_lc( pFftTbl, Ltrana, Ltrana_2, LtranaLogMinus1, xfp_left, xfp_left, 1 );
     413          29 :             r_fft_fx_lc( pFftTbl, Ltrana, Ltrana_2, LtranaLogMinus1, xfp_right, xfp_right, 1 );
     414             :         }
     415             : 
     416             :         /* square representation */
     417          54 :         fft_spec2_fx( xfp_left, magSqLeft, Ltrana );
     418          54 :         fft_spec2_fx( xfp_right, magSqRight, Ltrana );
     419             : 
     420             :         /* band powers in frequency groups
     421             :          * exclude bin at PI from calculation */
     422          54 :         magSqLeft[Ltrana_2] = L_deposit_l( 0 );
     423          54 :         move32();
     424          54 :         magSqRight[Ltrana_2] = L_deposit_l( 0 );
     425          54 :         move32();
     426             :     }
     427             : 
     428         132 :     pGrPowLeft = &gr_pow_left[0];
     429         132 :     pGrPowRight = &gr_pow_right[0];
     430         132 :     pGw = gw;
     431        1159 :     FOR( k = 0; k < Lgw; k++ )
     432             :     {
     433        1027 :         test();
     434        1027 :         test();
     435        1027 :         IF( LE_16( burst_len, 1 ) || ( EQ_16( burst_len, 2 ) && last_fec != 0 ) )
     436             :         {
     437         403 :             lowerEdge = *pGw++;
     438         403 :             move16();
     439         403 :             l = sub( *pGw, lowerEdge );
     440         403 :             headroom = GR_POW_HEADROOM[k]; /* Number of bits to scale down preventing from saturation in accumulation.*/
     441         403 :             move16();
     442         403 :             pLeft = magSqLeft + lowerEdge;
     443         403 :             pRight = magSqRight + lowerEdge;
     444         403 :             *pGrPowLeft = L_deposit_l( 0 );
     445         403 :             move32();
     446         403 :             *pGrPowRight = L_deposit_l( 0 );
     447         403 :             move32();
     448        8861 :             FOR( i = 0; i < l; i++ )
     449             :             {
     450        8458 :                 acc = L_shr( *pLeft++, headroom ); /* Scale down to prevent from saturation. */
     451        8458 :                 *pGrPowLeft = L_add( *pGrPowLeft, acc );
     452        8458 :                 move32();
     453        8458 :                 acc = L_shr( *pRight++, headroom );
     454        8458 :                 *pGrPowRight = L_add( *pGrPowRight, acc );
     455        8458 :                 move32();
     456             :             }
     457             : 
     458             :             /*Xavg[k] = sqrt(0.5f*(gr_pow_left[k]+gr_pow_right[k])/(float)(gw[k+1]-gw[k]));*/
     459         403 :             acc = L_shr( L_add( *pGrPowLeft, *pGrPowRight ), 1 );
     460         403 :             acc = Mult_32_16( acc, gw_len_inv_fx[k] ); /* -headroom */
     461             : 
     462         403 :             acc = Sqrt_l( acc, &expo ); /* -headroom+31+expo */
     463             : 
     464         403 :             expo = sub( add( expo, 31 ), headroom );
     465         403 :             if ( EQ_16( s_and( expo, 1 ), 1 ) )
     466             :             {
     467         295 :                 acc = Mult_32_16( acc, 23170 ); /* 1/sqrt(2) in Q15 */
     468             :             }
     469         403 :             expo = shr( expo, 1 );
     470         403 :             Xavg[k] = round_fx( L_shl( acc, sub( sub( 16, expo ), 2 ) ) ); /* Q0, additional right shift by 2 to account for that Xavg is
     471             :                                                                               calculated using lenght N/4 fft but is applied on a fft of length N */
     472         403 :             move16();
     473             : 
     474             :             /*dither phase in case of transient */
     475             :             /* separate transition detection and application of forced burst dithering */
     476         403 :             tr_dec[k] = 0;
     477         403 :             move16();
     478         403 :             Mpy_32_16_ss( *pGrPowLeft, THRESH_TR_LIN_BY2_FX, &acc, &lsb ); /* To facilitate fixed-point implementation, divide threshold by 2. */
     479         403 :             acc = L_or( L_shl_sat( acc, 16 ), L_and( 0xffffL, lsb ) );     /* Equivalent to concatenate acc and lsb, and then down shift by 16 bits. */
     480         403 :             if ( GT_32( *pGrPowRight, acc ) )                              /* gr_pow_right > thres_tr_lin*gr_pow_left */
     481             :             {
     482          12 :                 tr_dec[k] = 1;
     483          12 :                 move16();
     484             :             }
     485         403 :             Mpy_32_16_ss( *pGrPowRight, THRESH_TR_LIN_BY2_FX, &acc, &lsb );
     486         403 :             acc = L_or( L_shl_sat( acc, 16 ), L_and( 0xffffL, lsb ) ); /* Equivalent to concatenate acc and lsb, and then down shift by 16 bits. */
     487         403 :             if ( GT_32( *pGrPowLeft, acc ) )                           /* gr_pow_left > thres_tr_lin*gr_pow_right */
     488             :             {
     489          10 :                 tr_dec[k] = 1;
     490          10 :                 move16();
     491             :             }
     492             : 
     493             :             /* magnitude modification */
     494         403 :             IF( add( tr_dec[k], att_always ) != 0 )
     495             :             {
     496             : 
     497             : #if MAX_INCREASE_GRPPOW_FX != 0
     498             : #error trans_ana_fx-- The following implementation is incorrect
     499             : #endif
     500          22 :                 att_val = 32767;
     501          22 :                 move16();
     502          22 :                 IF( *pGrPowRight > 0 )
     503             :                 {
     504          17 :                     IF( LT_32( *pGrPowRight, *pGrPowLeft ) ) /* i.e., (gr_pow_right/gr_pow_left) < 1.0 */
     505             :                     {
     506             :                         /* Compute sqrt(grp_pow_chg), where grp_pow_chg = gr_pow_right/gr_pow_left. */
     507           5 :                         tmp16 = ratio( *pGrPowRight, *pGrPowLeft, &expo ); /* tmp16 in Q14 */
     508           5 :                         expo = sub( expo, ( 15 - 14 ) );                   /* Now, tmp16 is considered in Q15 */
     509           5 :                         i = norm_s( tmp16 );
     510           5 :                         man = shl( tmp16, i ); /* Mandatory normalization before sqrtNthOrder(). */
     511           5 :                         expo = add( expo, i );
     512           5 :                         man = sqrt2ndOrder( man );
     513           5 :                         if ( s_and( expo, 1 ) != 0 ) /* Check even or odd. */
     514             :                         {
     515           4 :                             man = mult_r( man, FEC_HQ_ECU_ROOT2 );
     516             :                         }
     517           5 :                         expo = shr( expo, 1 );      /* Divided by 2-- square root operation. */
     518           5 :                         att_val = shr( man, expo ); /* Denormalize the mantissa back to Q15. */
     519             :                     }
     520             :                     /* ELSE do nothing because (gr_pow_right/gr_pow_left) >= 1.0 (i.e.,
     521             :                      * max_increase_grppow_lin) */
     522             :                 }
     523             : 
     524          22 :                 mag_chg_1st[k] = att_val;
     525          22 :                 move16();
     526          22 :                 mag_chg[k] = att_val;
     527          22 :                 move16();
     528             :             }
     529             :             ELSE
     530             :             {
     531         381 :                 mag_chg_1st[k] = 32767;
     532         381 :                 move16();
     533         381 :                 mag_chg[k] = 32767;
     534         381 :                 move16(); /* Set to 1.0 in Q15 */
     535             :             }
     536             :         }
     537             :         ELSE
     538             :         {
     539             :             /* Since attDegreeFrames is discrete (integer) and hard limited to OFF_FRAMES_LIMIT,
     540             :              * it is much easier to implement 10^(-att_degree/20.0) by a simply direct
     541             :              * table-lookup. Also, att_per_frame is discrete as well and can be
     542             :              * either ATT_PER_FRAME-1 or ATT_PER_FRAME and nothing else. This
     543             :              * means only 2 tables of size=(OFF_FRAMES_LIMIT+1) each are required.
     544             :              * To take square root into account, it is divided by 20 instead of 10. */
     545         624 :             IF( EQ_16( att_per_frame, ATT_PER_FRAME ) ) /* Select the corresponding lookup-table. */
     546             :             {
     547           0 :                 att_val = POW_ATT_TABLE0[attDegreeFrames]; /* 10^(-attDegreeFrames*(ATT_PER_FRAME)/20) */
     548           0 :                 move16();
     549             :             }
     550             :             ELSE
     551             :             {
     552         624 :                 att_val = POW_ATT_TABLE1[attDegreeFrames]; /* 10^(-attDegreeFrames*(ATT_PER_FRAME - 1)/20) */
     553         624 :                 move16();
     554             :             }
     555         624 :             mag_chg[k] = mult_r( mag_chg_1st[k], att_val ); /* Q15 */
     556         624 :             move16();
     557             : 
     558         624 :             IF( GT_16( burst_len, BETA_MUTE_THR ) )
     559             :             {
     560         480 :                 *beta_mute = shr( *beta_mute, 1 );
     561         480 :                 move16();
     562             :             }
     563         624 :             alpha[k] = mag_chg[k];
     564         624 :             move16();
     565             :             /*beta[k] = sqrt(1.0f - SQR(alpha[k])) * *beta_mute;*/
     566         624 :             acc = L_sub( 1073741824, L_mult0( alpha[k], alpha[k] ) );
     567         624 :             acc = Sqrt_l( acc, &expo );
     568         624 :             expo = add( 30, add( 31, expo ) );
     569         624 :             if ( EQ_16( s_and( expo, 1 ), 1 ) )
     570             :             {
     571         624 :                 acc = Mult_32_16( acc, 23170 ); /* 1/sqrt(2) in Q15 */
     572             :             }
     573         624 :             expo = shr( expo, 1 );
     574         624 :             beta[k] = mult_r( *beta_mute, round_fx( L_shl( acc, sub( 31, expo ) ) ) );
     575         624 :             move16();
     576             : 
     577         624 :             IF( GE_16( k, LGW32K - 1 ) )
     578             :             {
     579         156 :                 beta[k] = mult_r( beta[k], 3277 ); /* 0.1 in Q15 */
     580         156 :                 move16();
     581             :             }
     582         468 :             ELSE IF( GE_16( k, LGW16K - 1 ) )
     583             :             {
     584          78 :                 beta[k] = mult_r( beta[k], 16384 ); /* 0.5 in Q15 */
     585          78 :                 move16();
     586             :             }
     587             :         }
     588        1027 :         pGrPowLeft++;
     589        1027 :         pGrPowRight++;
     590             :     }
     591             : 
     592         132 :     return;
     593             : }
     594             : 
     595             : /*-----------------------------------------------------------------------------
     596             :  * peakfinder_fx()
     597             :  *
     598             :  * Peak-picking algorithm
     599             :  *----------------------------------------------------------------------------*/
     600          54 : static void ivas_peakfinder_fx(
     601             :     const Word16 *x0,      /* i : vector from which the maxima will be found                    */
     602             :     const Word16 len0,     /* i : length of input vector                                        */
     603             :     Word16 *plocs,         /* o : the indices of the identified peaks in x0                  Q0 */
     604             :     Word16 *cInd,          /* o : number of identified peaks                                 Q0 */
     605             :     const Word16 sel,      /* i : The amount above surrounding data for a peak to be identified */
     606             :     const Word16 endpoints /* i  : Flag to include endpoints in peak search                       */
     607             : )
     608             : {
     609             :     const Word16 *pX0;
     610             :     Word16 minMag, tempMag, leftMin;
     611             :     Word16 dx0[L_PROT48k_2], x[L_PROT48k_2 + 1], peakMag[MAX_PLOCS];
     612             :     Word16 *pDx0, *pDx01, *pX;
     613             :     Word16 i, len, tempLoc, foundPeak, ii, xInd, tmp16, threshold, xAt0, xAt1, xAt2;
     614             :     Word16 len0Minus1, len0Minus2, lenMinus1;
     615             :     Word16 indarr[L_PROT48k_2 + 1], peakLoc[MAX_PLOCS];
     616             :     Word16 *pInd;
     617             : 
     618          54 :     tempLoc = 0;
     619          54 :     move16();
     620             : 
     621             :     /* Find derivative */
     622          54 :     len0Minus1 = sub( len0, 1 );
     623          54 :     pX0 = x0 + 1;
     624          54 :     Vr_subt( pX0, x0, dx0, len0Minus1 );
     625             : 
     626       34102 :     FOR( i = 0; i < len0Minus1; i++ )
     627             :     {
     628       34048 :         if ( dx0[i] == 0 )
     629             :         {
     630        7663 :             dx0[i] = -1;
     631        7663 :             move16();
     632             :         }
     633             :     }
     634             : 
     635             :     /* Find where the derivative changes sign
     636             :        Include endpoints in potential peaks and valleys */
     637          54 :     pX = x;
     638          54 :     pX0 = x0;
     639          54 :     pInd = indarr;
     640          54 :     pDx01 = dx0;
     641          54 :     pDx0 = pDx01 + 1;
     642          54 :     len = 0;
     643          54 :     move16();
     644          54 :     IF( endpoints )
     645             :     {
     646          54 :         *pX++ = *pX0++;
     647          54 :         move16();
     648          54 :         *pInd++ = 0;
     649          54 :         move16();
     650          54 :         len = 2;
     651          54 :         move16();
     652             :     }
     653          54 :     len0Minus2 = sub( len0, 2 );
     654             : 
     655       34048 :     FOR( i = 0; i < len0Minus2; i++ )
     656             :     {
     657       33994 :         IF( s_xor( *pDx01++, *pDx0++ ) < 0 ) /* Detect sign change. */
     658             :         {
     659       15438 :             *pInd++ = add( i, 1 );
     660       15438 :             move16();
     661       15438 :             *pX++ = *pX0;
     662       15438 :             move16();
     663       15438 :             len = add( len, 1 );
     664             :         }
     665       33994 :         pX0++;
     666             :     }
     667          54 :     IF( endpoints )
     668             :     {
     669          54 :         *pInd = len0Minus1;
     670          54 :         move16();
     671          54 :         *pX = *pX0;
     672          54 :         move16();
     673             :     }
     674             :     /* x[] only has the peaks, valleys, and endpoints */
     675          54 :     minimum_fx( x, len, &minMag );
     676             : 
     677          54 :     pInd = indarr;
     678          54 :     test();
     679          54 :     test();
     680          54 :     IF( GT_16( len, 2 ) || ( !endpoints && ( len > 0 ) ) )
     681             :     {
     682             :         /* Set initial parameters for loop */
     683          54 :         tempMag = minMag;
     684          54 :         move16();
     685          54 :         foundPeak = 0;
     686          54 :         move16();
     687          54 :         leftMin = minMag;
     688          54 :         move16();
     689          54 :         threshold = add( leftMin, sel );
     690             : 
     691          54 :         IF( len > 0 )
     692             :         {
     693             :             /* Deal with first point a little differently since tacked it on
     694             :                Calculate the sign of the derivative since we took the first point
     695             :                on it does not necessarily alternate like the rest. */
     696             : 
     697             :             /* The first point is larger or equal to the second */
     698          54 :             pX = x;
     699          54 :             xAt0 = *pX++;
     700          54 :             move16();
     701          54 :             xAt1 = *pX++;
     702          54 :             move16();
     703          54 :             xAt2 = *pX--; /* After decrement, pX points to x[1]. */
     704          54 :             move16();
     705          54 :             IF( GE_16( xAt0, xAt1 ) )
     706             :             {
     707          10 :                 ii = -1;
     708          10 :                 move16();
     709          10 :                 IF( GE_16( xAt1, xAt2 ) ) /* x[1] is not extremum -> overwrite with x[0] */
     710             :                 {
     711           0 :                     *pX = xAt0; /* x[1] = x[0] */
     712           0 :                     move16();
     713           0 :                     tmp16 = *pInd++;
     714           0 :                     move16();
     715           0 :                     *pInd++ = tmp16; /* ind[1] = ind[0] */
     716           0 :                     move16();
     717           0 :                     len = sub( len, 1 );
     718             :                 }
     719          10 :                 pX--; /* After decrement, pX points to x[0]. */
     720             :             }
     721             :             ELSE /* First point is smaller than the second */
     722             :             {
     723          44 :                 ii = 0;
     724          44 :                 move16();
     725          44 :                 IF( LT_16( xAt1, xAt2 ) ) /* x[1] is not extremum -> overwrite with x[0] */
     726             :                 {
     727           0 :                     *pX = xAt0; /* x[1] = x[0] */
     728           0 :                     move16();
     729           0 :                     tmp16 = *pInd++;
     730           0 :                     move16();
     731           0 :                     *pInd++ = tmp16; /* ind[1] = ind[0] */
     732           0 :                     move16();
     733           0 :                     len = sub( len, 1 );
     734             :                 }
     735             :             }
     736          54 :             pX--; /* After decrement, pX points to either x[-1] or x[0]. */
     737             :         }
     738             :         ELSE
     739             :         {
     740             :             // PMTE ()
     741           0 :             ii = -1; /* First point is a peak */
     742           0 :             move16();
     743           0 :             IF( GE_16( len, 2 ) )
     744             :             {
     745           0 :                 if ( GE_16( x[1], x[0] ) )
     746             :                 {
     747           0 :                     ii = 0; /* First point is a valley, skip it */
     748           0 :                     move16();
     749             :                 }
     750             :             }
     751             :         }
     752          54 :         *cInd = 0;
     753          54 :         move16();
     754             :         /*Loop through extrema which should be peaks and then valleys*/
     755          54 :         lenMinus1 = sub( len, 1 );
     756             :         FOR( ;; )
     757             :         {
     758        7793 :             ii = add( ii, 1 ); /* This is a peak */
     759             : 
     760             :             /* Make sure we don't iterate past the length of our vector */
     761        7793 :             IF( GE_16( ii, lenMinus1 ) )
     762             :             {
     763          54 :                 BREAK;
     764             :             }
     765             : 
     766             :             /*Reset peak finding if we had a peak and the next peak is bigger
     767             :               than the last or the left min was small enough to reset.*/
     768        7739 :             IF( foundPeak > 0 )
     769             :             {
     770         890 :                 tempMag = minMag;
     771         890 :                 move16();
     772         890 :                 foundPeak = 0;
     773         890 :                 move16();
     774             :             }
     775             : 
     776             :             /* Found new peak that was larger than temp mag and selectivity larger
     777             :                than the minimum to its left. */
     778        7739 :             IF( GT_16( *( ++pX ), tempMag ) )
     779             :             {
     780        7660 :                 IF( GT_16( *pX, threshold ) ) /* threshold = leftMin + sel */
     781             :                 {
     782         987 :                     tempLoc = ii;
     783         987 :                     move16();
     784         987 :                     tempMag = *pX;
     785         987 :                     move16();
     786             :                 }
     787             :             }
     788             : 
     789        7739 :             ii = add( ii, 1 ); /* Move onto the valley */
     790        7739 :             pX++;
     791             : 
     792             :             /* Come down at least sel from peak */
     793        7739 :             IF( foundPeak == 0 )
     794             :             {
     795        7739 :                 IF( GT_16( tempMag, add( sel, *pX ) ) )
     796             :                 {
     797         890 :                     foundPeak = 1; /* We have found a peak */
     798         890 :                     move16();
     799         890 :                     leftMin = *pX;
     800         890 :                     move16();
     801         890 :                     threshold = add( leftMin, sel );
     802         890 :                     peakLoc[*cInd] = tempLoc; /* Add peak to index */
     803         890 :                     move16();
     804         890 :                     peakMag[*cInd] = tempMag;
     805         890 :                     move16();
     806         890 :                     *cInd = add( *cInd, 1 );
     807         890 :                     move16();
     808             :                 }
     809             :             }
     810        7739 :             IF( foundPeak == 0 ) /* The above IF-block has not found the peak yet. */
     811             :             {
     812        6849 :                 IF( LT_16( *pX, leftMin ) ) /* New left minimum */
     813             :                 {
     814         746 :                     leftMin = *pX;
     815         746 :                     move16();
     816         746 :                     threshold = add( leftMin, sel );
     817             :                 }
     818             :             }
     819             :         }
     820             : 
     821             :         /* Check end point */
     822          54 :         IF( GT_16( x[lenMinus1], tempMag ) )
     823             :         {
     824          38 :             IF( GT_16( x[lenMinus1], threshold ) ) /* threshold = leftMin + sel */
     825             :             {
     826           0 :                 peakLoc[*cInd] = lenMinus1;
     827           0 :                 move16();
     828           0 :                 peakMag[*cInd] = x[lenMinus1];
     829           0 :                 move16();
     830           0 :                 *cInd = add( *cInd, 1 );
     831           0 :                 move16();
     832           0 :                 foundPeak = 1;
     833           0 :                 move16();
     834             :             }
     835             :         }
     836          54 :         IF( foundPeak == 0 ) /* Check if we still need to add the last point */
     837             :         {
     838          54 :             IF( GT_16( tempMag, minMag ) )
     839             :             {
     840           0 :                 peakLoc[*cInd] = tempLoc;
     841           0 :                 move16();
     842           0 :                 peakMag[*cInd] = tempMag;
     843           0 :                 move16();
     844           0 :                 *cInd = add( *cInd, 1 );
     845           0 :                 move16();
     846             :             }
     847             :         }
     848             : 
     849             :         /* Create output */
     850         944 :         FOR( i = 0; i < *cInd; i++ )
     851             :         {
     852         890 :             plocs[i] = *( indarr + peakLoc[i] );
     853         890 :             move16();
     854             :         }
     855             :     }
     856             :     ELSE /* This is a monotone function where an endpoint is the only peak */
     857             :     {
     858           0 :         IF( endpoints )
     859             :         {
     860           0 :             xInd = 1;
     861           0 :             move16();
     862           0 :             if ( GT_16( x[0], x[1] ) )
     863             :             {
     864           0 :                 xInd = 0;
     865           0 :                 move16();
     866             :             }
     867             : 
     868           0 :             peakMag[0] = x[xInd];
     869           0 :             move16();
     870           0 :             IF( GT_16( peakMag[0], add( minMag, sel ) ) )
     871             :             {
     872           0 :                 plocs[0] = *( indarr + xInd );
     873           0 :                 move16();
     874           0 :                 *cInd = 1;
     875           0 :                 move16();
     876             :             }
     877             :             ELSE
     878             :             {
     879           0 :                 *cInd = 0;
     880           0 :                 move16();
     881             :             }
     882             :         }
     883             :         ELSE
     884             :         { /* Input constant or all zeros -- no peaks found */
     885           0 :             *cInd = 0;
     886           0 :             move16();
     887             :         }
     888             :     }
     889          54 : }
     890             : 
     891           0 : static void peakfinder_fx(
     892             :     const Word16 *x0,  /* i : vector from which the maxima will be found                    */
     893             :     const Word16 len0, /* i : length of input vector                                        */
     894             :     Word16 *plocs,     /* o : the indices of the identified peaks in x0                  Q0 */
     895             :     Word16 *cInd,      /* o : number of identified peaks                                 Q0 */
     896             :     const Word16 sel   /* i : The amount above surrounding data for a peak to be identified */
     897             : )
     898             : {
     899             :     const Word16 *pX0;
     900             :     Word16 minMag, tempMag, leftMin;
     901             :     Word16 dx0[L_PROT48k_2], x[L_PROT48k_2 + 1], peakMag[MAX_PLOCS];
     902             :     Word16 *pDx0, *pDx01, *pX;
     903             :     Word16 i, len, tempLoc, foundPeak, ii, xInd, tmp16, threshold, xAt0, xAt1, xAt2;
     904             :     Word16 len0Minus1, len0Minus2, lenMinus1;
     905             :     Word16 indarr[L_PROT48k_2 + 1], peakLoc[MAX_PLOCS];
     906             :     Word16 *pInd;
     907             : 
     908           0 :     tempLoc = 0;
     909           0 :     move16();
     910             : 
     911             :     /* Find derivative */
     912           0 :     len0Minus1 = sub( len0, 1 );
     913           0 :     pX0 = x0 + 1;
     914           0 :     Vr_subt( pX0, x0, dx0, len0Minus1 );
     915             : 
     916           0 :     FOR( i = 0; i < len0Minus1; i++ )
     917             :     {
     918           0 :         if ( dx0[i] == 0 )
     919             :         {
     920           0 :             dx0[i] = -1;
     921           0 :             move16();
     922             :         }
     923             :     }
     924             : 
     925             :     /* Find where the derivative changes sign
     926             :        Include endpoints in potential peaks and valleys */
     927           0 :     pX = x;
     928           0 :     pX0 = x0;
     929           0 :     pInd = indarr;
     930           0 :     pDx01 = dx0;
     931           0 :     pDx0 = pDx01 + 1;
     932             :     {
     933           0 :         *pX++ = *pX0++;
     934           0 :         move16();
     935           0 :         *pInd++ = 0;
     936           0 :         move16();
     937           0 :         len = 2;
     938           0 :         move16();
     939             :     }
     940           0 :     len0Minus2 = sub( len0, 2 );
     941             : 
     942           0 :     FOR( i = 0; i < len0Minus2; i++ )
     943             :     {
     944           0 :         IF( s_xor( *pDx01++, *pDx0++ ) < 0 ) /* Detect sign change. */
     945             :         {
     946           0 :             *pInd++ = add( i, 1 );
     947           0 :             move16();
     948           0 :             *pX++ = *pX0;
     949           0 :             move16();
     950           0 :             len = add( len, 1 );
     951             :         }
     952           0 :         pX0++;
     953             :     }
     954             :     {
     955           0 :         *pInd = len0Minus1;
     956           0 :         move16();
     957           0 :         *pX = *pX0;
     958           0 :         move16();
     959             :     }
     960             :     /* x[] only has the peaks, valleys, and endpoints */
     961           0 :     minimum_fx( x, len, &minMag );
     962             : 
     963           0 :     pInd = indarr;
     964           0 :     IF( GT_16( len, 2 ) )
     965             :     {
     966             :         /* Set initial parameters for loop */
     967           0 :         tempMag = minMag;
     968           0 :         move16();
     969           0 :         foundPeak = 0;
     970           0 :         move16();
     971           0 :         leftMin = minMag;
     972           0 :         move16();
     973           0 :         threshold = add( leftMin, sel );
     974             : 
     975             :         {
     976             :             /* Deal with first point a little differently since tacked it on
     977             :                Calculate the sign of the derivative since we took the first point
     978             :                on it does not necessarily alternate like the rest. */
     979             : 
     980             :             /* The first point is larger or equal to the second */
     981           0 :             pX = x;
     982           0 :             xAt0 = *pX++;
     983           0 :             move16();
     984           0 :             xAt1 = *pX++;
     985           0 :             move16();
     986           0 :             xAt2 = *pX--; /* After decrement, pX points to x[1]. */
     987           0 :             move16();
     988           0 :             IF( GE_16( xAt0, xAt1 ) )
     989             :             {
     990           0 :                 ii = -1;
     991           0 :                 move16();
     992           0 :                 IF( GE_16( xAt1, xAt2 ) ) /* x[1] is not extremum -> overwrite with x[0] */
     993             :                 {
     994           0 :                     *pX = xAt0; /* x[1] = x[0] */
     995           0 :                     move16();
     996           0 :                     tmp16 = *pInd++;
     997           0 :                     move16();
     998           0 :                     *pInd++ = tmp16; /* ind[1] = ind[0] */
     999           0 :                     move16();
    1000           0 :                     len = sub( len, 1 );
    1001             :                 }
    1002           0 :                 pX--; /* After decrement, pX points to x[0]. */
    1003             :             }
    1004             :             ELSE /* First point is smaller than the second */
    1005             :             {
    1006           0 :                 ii = 0;
    1007           0 :                 move16();
    1008           0 :                 IF( LT_16( xAt1, xAt2 ) ) /* x[1] is not extremum -> overwrite with x[0] */
    1009             :                 {
    1010           0 :                     *pX = xAt0; /* x[1] = x[0] */
    1011           0 :                     move16();
    1012           0 :                     tmp16 = *pInd++;
    1013           0 :                     move16();
    1014           0 :                     *pInd++ = tmp16; /* ind[1] = ind[0] */
    1015           0 :                     move16();
    1016           0 :                     len = sub( len, 1 );
    1017             :                 }
    1018             :             }
    1019           0 :             pX--; /* After decrement, pX points to either x[-1] or x[0]. */
    1020             :         }
    1021           0 :         *cInd = 0;
    1022           0 :         move16();
    1023             :         /*Loop through extrema which should be peaks and then valleys*/
    1024           0 :         lenMinus1 = sub( len, 1 );
    1025             :         FOR( ;; )
    1026             :         {
    1027           0 :             ii = add( ii, 1 ); /* This is a peak */
    1028             : 
    1029             :             /* Make sure we don't iterate past the length of our vector */
    1030           0 :             IF( GE_16( ii, lenMinus1 ) )
    1031             :             {
    1032           0 :                 BREAK;
    1033             :             }
    1034             : 
    1035             :             /*Reset peak finding if we had a peak and the next peak is bigger
    1036             :               than the last or the left min was small enough to reset.*/
    1037           0 :             IF( foundPeak > 0 )
    1038             :             {
    1039           0 :                 tempMag = minMag;
    1040           0 :                 move16();
    1041           0 :                 foundPeak = 0;
    1042           0 :                 move16();
    1043             :             }
    1044             : 
    1045             :             /* Found new peak that was larger than temp mag and selectivity larger
    1046             :                than the minimum to its left. */
    1047           0 :             IF( GT_16( *( ++pX ), tempMag ) )
    1048             :             {
    1049           0 :                 IF( GT_16( *pX, threshold ) ) /* threshold = leftMin + sel */
    1050             :                 {
    1051           0 :                     tempLoc = ii;
    1052           0 :                     move16();
    1053           0 :                     tempMag = *pX;
    1054           0 :                     move16();
    1055             :                 }
    1056             :             }
    1057             : 
    1058           0 :             ii = add( ii, 1 ); /* Move onto the valley */
    1059           0 :             pX++;
    1060             : 
    1061             :             /* Come down at least sel from peak */
    1062           0 :             IF( foundPeak == 0 )
    1063             :             {
    1064           0 :                 IF( GT_16( tempMag, add( sel, *pX ) ) )
    1065             :                 {
    1066           0 :                     foundPeak = 1; /* We have found a peak */
    1067           0 :                     move16();
    1068           0 :                     leftMin = *pX;
    1069           0 :                     move16();
    1070           0 :                     threshold = add( leftMin, sel );
    1071           0 :                     peakLoc[*cInd] = tempLoc; /* Add peak to index */
    1072           0 :                     move16();
    1073           0 :                     peakMag[*cInd] = tempMag;
    1074           0 :                     move16();
    1075           0 :                     *cInd = add( *cInd, 1 );
    1076           0 :                     move16();
    1077             :                 }
    1078             :             }
    1079           0 :             IF( foundPeak == 0 ) /* The above IF-block has not found the peak yet. */
    1080             :             {
    1081           0 :                 IF( LT_16( *pX, leftMin ) ) /* New left minimum */
    1082             :                 {
    1083           0 :                     leftMin = *pX;
    1084           0 :                     move16();
    1085           0 :                     threshold = add( leftMin, sel );
    1086             :                 }
    1087             :             }
    1088             :         }
    1089             : 
    1090             :         /* Check end point */
    1091           0 :         IF( GT_16( x[lenMinus1], tempMag ) )
    1092             :         {
    1093           0 :             IF( GT_16( x[lenMinus1], threshold ) ) /* threshold = leftMin + sel */
    1094             :             {
    1095           0 :                 peakLoc[*cInd] = lenMinus1;
    1096           0 :                 move16();
    1097           0 :                 peakMag[*cInd] = x[lenMinus1];
    1098           0 :                 move16();
    1099           0 :                 *cInd = add( *cInd, 1 );
    1100           0 :                 foundPeak = 1;
    1101           0 :                 move16();
    1102             :             }
    1103             :         }
    1104           0 :         IF( foundPeak == 0 ) /* Check if we still need to add the last point */
    1105             :         {
    1106           0 :             IF( GT_16( tempMag, minMag ) )
    1107             :             {
    1108           0 :                 peakLoc[*cInd] = tempLoc;
    1109           0 :                 move16();
    1110           0 :                 peakMag[*cInd] = tempMag;
    1111           0 :                 move16();
    1112           0 :                 *cInd = add( *cInd, 1 );
    1113           0 :                 move16();
    1114             :             }
    1115             :         }
    1116             : 
    1117             :         /* Create output */
    1118           0 :         FOR( i = 0; i < *cInd; i++ )
    1119             :         {
    1120           0 :             plocs[i] = *( indarr + peakLoc[i] );
    1121           0 :             move16();
    1122             :         }
    1123             :     }
    1124             :     ELSE /* This is a monotone function where an endpoint is the only peak */
    1125             :     {
    1126             :         {
    1127           0 :             xInd = 1;
    1128           0 :             move16();
    1129           0 :             if ( GT_16( x[0], x[1] ) )
    1130             :             {
    1131           0 :                 xInd = 0;
    1132           0 :                 move16();
    1133             :             }
    1134             : 
    1135           0 :             peakMag[0] = x[xInd];
    1136           0 :             move16();
    1137           0 :             IF( GT_16( peakMag[0], add( minMag, sel ) ) )
    1138             :             {
    1139           0 :                 plocs[0] = *( indarr + xInd );
    1140           0 :                 move16();
    1141           0 :                 *cInd = 1;
    1142           0 :                 move16();
    1143             :             }
    1144             :             ELSE
    1145             :             {
    1146           0 :                 *cInd = 0;
    1147           0 :                 move16();
    1148             :             }
    1149             :         }
    1150             :     }
    1151           0 : }
    1152             : 
    1153             : /*-----------------------------------------------------------------------------
    1154             :  * imax_fx()
    1155             :  *
    1156             :  * Get interpolated maximum position
    1157             :  *-----------------------------------------------------------------------------*/
    1158           0 : static Word16 imax_fx(                      /* o: The location, relative to the middle of the 3 given data point, of the maximum. (Q15) */
    1159             :                        const Word16 *y,     /* i: The 3 given data points. */
    1160             :                        const Word16 special /* i: -1 = left edge special case, 0 = normal, +1 = right edge special case */
    1161             : )
    1162             : {
    1163             :     Word16 posi;
    1164             :     Word16 y1, y2, y3, man, expo, edge;
    1165             :     const Word16 *pY;
    1166             :     Word32 numer, denom, sign, acc, y3_y1;
    1167             :     /* Seek the extremum of the parabola P(x) defined by 3 consecutive points
    1168             :        so that P([-1 0 1]) = [y1 y2 y3] */
    1169           0 :     pY = y;
    1170           0 :     y1 = *pY++, y2 = *pY++, y3 = *pY;
    1171           0 :     move16();
    1172           0 :     move16();
    1173           0 :     move16();
    1174             : 
    1175             :     /* The extremum value:
    1176             :      *   y2i = -0.125f * SQR(y3_y1) / (y1+y3-2*y2)+y2
    1177             :      * is not computed. Alternatively, the derivative of the parabola evaluated at y=0,
    1178             :      * dP/dy|y=0, is used to determine whether the extremum is maximum or not.
    1179             :      */
    1180             : 
    1181             :     /* Compute the extremum location: posi = (y3 - y1)/(4*y2 - 2*y1 - 2*y3). */
    1182           0 :     y3_y1 = L_sub( y3, y1 );
    1183           0 :     acc = L_shl( y2, 1 );         /* N.B. y2 is multiplied by 2 not 4. */
    1184           0 :     acc = L_sub( acc, y1 );       /* N.B. Y1 is not multiplied by 2. */
    1185           0 :     denom = L_sub( acc, y3 );     /* N.B. Y3 is not multiplied by 2. */
    1186           0 :     sign = L_xor( y3_y1, denom ); /* Preserve the sign since div_s() only takes positive arguments. */
    1187           0 :     numer = L_abs( y3_y1 );
    1188           0 :     denom = L_abs( denom );
    1189           0 :     IF( numer == 0 )
    1190             :     {
    1191           0 :         return 0;
    1192             :     }
    1193           0 :     IF( denom == 0 )
    1194             :     {
    1195           0 :         return 0;
    1196             :     }
    1197             :     /* Although the output of ratio() is in Q14, adding the missing factor of 2 (See above)
    1198             :      * in the denominator, the output is now considered to be in Q15. */
    1199           0 :     man = ratio( numer, denom, &expo ); /* The mantissa is considered in Q15 */
    1200           0 :     posi = shr_sat( man, expo );        /* in Q15 (Due to saturation, it is automatically bound inside [-1.0,1.0].) */
    1201           0 :     if ( sign < 0 )                     /* Restore the sign. */
    1202             :     {
    1203           0 :         posi = negate( posi );
    1204             :     }
    1205             : 
    1206             :     /* For both edges (left and right), the extremum found above may be minimum.
    1207             :      * It needs to reject the minimum. */
    1208           0 :     IF( special != 0 ) /* Either edge specical case. */
    1209             :     {
    1210           0 :         edge = 0x7fff; /* 1 in Q15 for the right edge special case */
    1211           0 :         move16();
    1212           0 :         if ( special < 0 )
    1213             :         {
    1214           0 :             edge = 0; /* Left edge special case */
    1215           0 :             move16();
    1216             :         }
    1217             : 
    1218             :         /* The derivative (slope) of the interpolating parabola = 2*A*y + B,
    1219             :          *   where A = (y3 + y1)/2 - y2
    1220             :          *     and B = (y3 - y1)/2.
    1221             :          * Therefore, the slope at y=0 is simply B. Use this slope to determine
    1222             :          * if the parabola is concave upward or downward.
    1223             :          */
    1224           0 :         IF( posi > 0 ) /* The extremum is in between the middle and the right given data points. */
    1225             :         {
    1226           0 :             IF( LE_16( y3, y1 ) ) /* Check the slope at y=0, i.e., at the middle given data point. */
    1227             :             {
    1228           0 :                 posi = edge; /* minimum case */
    1229           0 :                 move16();
    1230             :             }
    1231             :             ELSE
    1232             :             {
    1233           0 :                 posi = sub( 0x7fff, posi ); /* maximum case */
    1234             :             }
    1235             :         }
    1236             :         ELSE /* The extremum is in between the left and the middle given data points. */
    1237             :         {
    1238           0 :             IF( GE_16( y3, y1 ) )
    1239             :             {
    1240           0 :                 posi = edge; /* minimum case */
    1241           0 :                 move16();
    1242             :             }
    1243             :             ELSE
    1244             :             {
    1245           0 :                 posi = add( 0x7fff, posi ); /* maximum case */
    1246             :             }
    1247             :         }
    1248             :     }
    1249           0 :     return posi; /* Q15. The position either left or right relative to the index of the middle of the 3 given data points. */
    1250             : }
    1251             : 
    1252             : /*-----------------------------------------------------------------------------
    1253             :  * spec_ana_fx()
    1254             :  *
    1255             :  * Spectral analysis
    1256             :  *-----------------------------------------------------------------------------*/
    1257             : 
    1258          54 : static void ivas_spec_ana_fx(
    1259             :     const Word16 *prevsynth,   /* i : Input signal                                         */
    1260             :     Word16 *plocs,             /* o : The indicies of the identified peaks             Q0  */
    1261             :     Word32 *plocsi,            /* o : Interpolated positions of the identified peaks   Q16 */
    1262             :     Word16 *num_plocs,         /* o : Number of identified peaks                       Q0  */
    1263             :     Word16 *X_sav,             /* o : Stored fft spectrum                                  */
    1264             :     const Word16 output_frame, /* i : Frame length                                     Q0  */
    1265             :     const Word16 bwidth_fx,    /* i : Encoded bandwidth index                          Q0  */
    1266             :     Word16 *Q,                 /* o : Q value of the fft spectrum                          */
    1267             :     const Word16 element_mode, /* i  : IVAS element mode                               */
    1268             :     Word16 *noise_fac,         /* o  : for few peaks zeroing valleys decision making   Q15*/
    1269             :     const Word16 pcorr )
    1270             : {
    1271          54 :     Word16 Lprot, LprotLog2Minus1 = 0, hamm_len2 = 0, Lprot2, Lprot2_1, m, n;
    1272          54 :     const Word16 *pFftTbl = NULL;
    1273             :     Word16 xfp[L_PROT48k];
    1274             :     Word32 magSq[L_PROT48k / 2 + 1], *pMagSq;
    1275             :     Word16 *pXfp, *pXfp1, *pXsav, *pPlocs;
    1276             :     Word16 Xmax, Xmin, sel, man, expo, expoBy2;
    1277             :     Word16 sinTblOffset, rectLength, fraction, special;
    1278             :     Word32 *pPlocsi;
    1279             :     Word32 acc;
    1280             :     Word16 stop_band_start;
    1281             :     Word16 stop_band_length;
    1282          54 :     const Word16 *w_hamm = NULL;
    1283             :     Word16 window_corr, window_corr_step;
    1284             :     Word16 currPlocs, endPlocs, nJacob, k, i;
    1285             :     Word32 sig, noise, st_point, end_point;
    1286             :     Word32 xfp_32[3]; // Q + 16
    1287             : 
    1288          54 :     move16();    // LprotLog2Minus1
    1289          54 :     move16();    // hamm_len2
    1290          54 :     Lprot = 512; /* 1536=(2*output_frame)*1024/1280; */
    1291          54 :     move16();
    1292             : 
    1293          54 :     sinTblOffset = 0;
    1294          54 :     move16();
    1295             : 
    1296          54 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    1297             :     {
    1298          25 :         Lprot = L_PROT48k; /* 1536=(2*output_frame)*1024/1280; */
    1299          25 :         move16();
    1300          25 :         hamm_len2 = L_PROT_HAMM_LEN2_48k; /* half Hamming window = 288 */
    1301          25 :         move16();
    1302          25 :         w_hamm = w_hamm_sana48k_2_fx;
    1303             :     }
    1304          29 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    1305             :     {
    1306          29 :         Lprot = L_PROT32k; /* 1024 */
    1307          29 :         move16();
    1308          29 :         sinTblOffset = 4;
    1309          29 :         move16();
    1310          29 :         hamm_len2 = L_PROT_HAMM_LEN2_32k; /* half Hamming window = 192 */
    1311          29 :         move16();
    1312          29 :         pFftTbl = FFT_W512;  /* Table for 1024-point real input FFT */
    1313          29 :         LprotLog2Minus1 = 9; /* FFT stages for complex input FFT */
    1314          29 :         move16();
    1315          29 :         w_hamm = w_hamm_sana32k_2_fx;
    1316             :     }
    1317             :     ELSE
    1318             :     {
    1319           0 :         Lprot = 512;
    1320           0 :         move16();
    1321           0 :         sinTblOffset = 8;
    1322           0 :         move16();
    1323           0 :         hamm_len2 = L_PROT_HAMM_LEN2_16k; /* half Hamming window = 96 */
    1324           0 :         move16();
    1325           0 :         pFftTbl = FFT_W256;  /* Table for 512-point real input FFT */
    1326           0 :         LprotLog2Minus1 = 8; /* FFT stages for complex input FFT */
    1327           0 :         move16();
    1328           0 :         w_hamm = w_hamm_sana16k_2_fx;
    1329             :     }
    1330             : 
    1331          54 :     Lprot2 = shr( Lprot, 1 );
    1332          54 :     Lprot2_1 = add( Lprot2, 1 );
    1333          54 :     rectLength = sub( Lprot, shl( hamm_len2, 1 ) ); /* The length of the rectangular portion of the Hamming-Rectangular window. */
    1334             : 
    1335          54 :     *Q = s_max( 0, sub( Exp16Array( Lprot, prevsynth ), 1 ) );
    1336          54 :     move16();
    1337          54 :     Copy_Scale_sig( prevsynth, xfp, Lprot, *Q );
    1338             : 
    1339             : 
    1340          54 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    1341             :     {
    1342             :         /* Apply hamming-rect window */
    1343          25 :         IF( element_mode == EVS_MONO )
    1344             :         {
    1345           0 :             windowing( xfp, xfp, w_hamm_sana48k_2_fx, rectLength, hamm_len2 );
    1346             :         }
    1347             :         ELSE
    1348             :         {
    1349             :             // PMTE()
    1350          25 :             window_corr = w_hamm[0];
    1351          25 :             move16();
    1352          25 :             window_corr_step = idiv1616( w_hamm[0], hamm_len2 );
    1353        7225 :             FOR( i = 0; i < hamm_len2; i++ )
    1354             :             {
    1355        7200 :                 xfp[i] = mult_r( shl( prevsynth[i], *Q ), sub( w_hamm[i], window_corr ) );
    1356        7200 :                 move16();
    1357        7200 :                 xfp[Lprot - i - 1] = mult_r( shl( prevsynth[Lprot - i - 1], *Q ), sub( w_hamm[i], window_corr ) );
    1358        7200 :                 move16();
    1359        7200 :                 window_corr = sub( window_corr, window_corr_step );
    1360             :             }
    1361             :         }
    1362             :         /* Spectrum */
    1363          25 :         fft3_fx( xfp, xfp, Lprot );
    1364             :     }
    1365             :     ELSE
    1366             :     {
    1367          29 :         IF( element_mode == EVS_MONO )
    1368             :         {
    1369             :             /* Apply hamming-rect window */
    1370           0 :             windowing_ROM_optimized( xfp, xfp, sinTblOffset, rectLength, hamm_len2 );
    1371             :         }
    1372             :         ELSE
    1373             :         {
    1374             :             // PMTE()
    1375          29 :             window_corr = w_hamm[0];
    1376          29 :             move16();
    1377          29 :             window_corr_step = idiv1616( w_hamm[0], hamm_len2 );
    1378        5597 :             FOR( i = 0; i < hamm_len2; i++ )
    1379             :             {
    1380        5568 :                 xfp[i] = mult_r( shl( prevsynth[i], *Q ), sub( w_hamm[i], window_corr ) );
    1381        5568 :                 move16();
    1382        5568 :                 xfp[Lprot - i - 1] = mult_r( shl( prevsynth[Lprot - i - 1], *Q ), sub( w_hamm[i], window_corr ) );
    1383        5568 :                 move16();
    1384        5568 :                 window_corr = sub( window_corr, window_corr_step );
    1385             :             }
    1386             :         }
    1387             :         /* Spectrum */
    1388          29 :         r_fft_fx_lc( pFftTbl, Lprot, Lprot2, LprotLog2Minus1, xfp, xfp, 1 );
    1389             :     }
    1390             : 
    1391             :     /* Apply zeroing of non-coded FFT spectrum */
    1392          54 :     IF( GT_16( output_frame, inner_frame_tbl[bwidth_fx] ) )
    1393             :     {
    1394          11 :         stop_band_start = shl( 128, bwidth_fx );
    1395          11 :         stop_band_length = sub( Lprot, shl( stop_band_start, 1 ) );
    1396          11 :         stop_band_start = add( stop_band_start, 1 );
    1397          11 :         set16_fx( xfp + stop_band_start, 0, stop_band_length );
    1398             :     }
    1399             : 
    1400          54 :     pXfp = xfp;
    1401          54 :     pXsav = X_sav;
    1402             : 
    1403       68150 :     FOR( m = 0; m < Lprot; m++ )
    1404             :     {
    1405       68096 :         *pXsav++ = *pXfp++;
    1406       68096 :         move16();
    1407             :     }
    1408             : 
    1409             :     /* Magnitude representation */
    1410          54 :     fft_spec2_fx( xfp, magSq, Lprot );
    1411             : 
    1412             :     /* Compute xfp[m] = sqrt(magSq[m]) */
    1413          54 :     pXfp = xfp;
    1414          54 :     pMagSq = magSq;
    1415       34156 :     FOR( m = 0; m < Lprot2_1; m++ )
    1416             :     {
    1417       34102 :         IF( *pMagSq == 0 )
    1418             :         {
    1419        5526 :             *pXfp++ = extract_l( *pMagSq++ ); /* magSq[] is zero */
    1420        5526 :             move16();
    1421             :         }
    1422             :         ELSE
    1423             :         {
    1424       28576 :             expo = norm_l( *pMagSq );                    /* exponent */
    1425       28576 :             man = extract_h( L_shl( *pMagSq++, expo ) ); /* mantissa */
    1426       28576 :             man = sqrt2ndOrder( man );
    1427       28576 :             expoBy2 = shr( expo, 1 );   /* Divided by 2-- square root operation. */
    1428       28576 :             IF( s_and( expo, 1 ) == 0 ) /* Check even or odd. */
    1429             :             {
    1430       15599 :                 man = mult_r( man, FEC_HQ_ECU_ROOT2 ); /* FEC_HQ_ECU_ROOT2 is sqrt(2) in Q14 */
    1431       15599 :                 expoBy2 = sub( expoBy2, 1 );
    1432             :             }
    1433       28576 :             *pXfp++ = shr_sat( man, expoBy2 );
    1434       28576 :             move16(); /* Denormalize the mantissa back to Q0. */
    1435             :         }
    1436             :     }
    1437             : 
    1438             :     /* Find maximum and minimum. */
    1439          54 :     maximum_fx( xfp, Lprot2_1, &Xmax );
    1440          54 :     minimum_fx( xfp, Lprot2_1, &Xmin );
    1441          54 :     IF( element_mode == EVS_MONO )
    1442             :     {
    1443           0 :         sel = mult_r( sub( Xmax, Xmin ), CMPLMNT_PFIND_SENS_FX );
    1444             :     }
    1445             :     ELSE
    1446             :     {
    1447          54 :         sel = mult_r( sub( Xmax, Xmin ), CMPLMNT_ST_PFIND_SENS_FX );
    1448             :     }
    1449          54 :     ivas_peakfinder_fx( xfp, Lprot2_1, plocs, num_plocs, sel, TRUE );
    1450             : 
    1451             : 
    1452             :     /* Currently not the pitch correlation but some LF correlation */
    1453          54 :     if ( element_mode != EVS_MONO && *num_plocs > 50 && pcorr < 19661 /* 0.6f in Q15 */ )
    1454             :     {
    1455           0 :         *num_plocs = 0;
    1456           0 :         move16();
    1457             :     }
    1458             : 
    1459          54 :     IF( element_mode == EVS_MONO )
    1460             :     {
    1461             : 
    1462             :         /* Refine peaks */
    1463           0 :         pPlocsi = plocsi;
    1464           0 :         pPlocs = plocs;
    1465           0 :         n = sub( *num_plocs, 1 ); /* -1 so as to exclude the very last peak. */
    1466             :         /* Special case-- The very 1st peak if it is at 0 index position */
    1467           0 :         IF( *pPlocs == 0 ) /* Only the very 1st peak is possible the peak at 0 index position. */
    1468             :         {
    1469           0 :             fraction = imax_fx( xfp, -1 );  /* -1 signifies special left edge case. */
    1470           0 :             acc = L_deposit_h( *pPlocs++ ); /* N.B., (*pPlocs) must be zero here. */
    1471           0 :             *pPlocsi++ = L_mac( acc, fraction, 1 );
    1472           0 :             move32();        /* in Q16 */
    1473           0 :             n = sub( n, 1 ); /* This special case is taken care of-- one less to go */
    1474             :         }
    1475             :         /* All peaks except the very last peak but including the very 1st one if it has not been taken care of. */
    1476           0 :         pXfp1 = xfp - 1;
    1477           0 :         FOR( m = 0; m < n; m++ ) /* Loop through up to the last but one peak. (The last one is excluded.) */
    1478             :         {
    1479           0 :             pXfp = pXfp1 + *pPlocs;
    1480           0 :             fraction = imax_fx( pXfp, 0 ); /* in Q15 */
    1481           0 :             acc = L_deposit_h( *pPlocs++ );
    1482           0 :             *pPlocsi++ = L_mac( acc, fraction, 1 );
    1483           0 :             move32(); /* in Q16. Append the fractional part to the integral part. */
    1484             :         }
    1485           0 :         IF( n >= 0 )
    1486             :         {
    1487             :             /* Special case-- The very last peak */
    1488           0 :             pXfp = pXfp1 + *pPlocs;
    1489           0 :             IF( EQ_16( *pPlocs, Lprot2 ) ) /* Only the very last peak is possible the peak at Lprot2 index position. */
    1490             :             {
    1491           0 :                 pXfp--;      /* Special case needs extra decrement */
    1492           0 :                 special = 1; /* Signify special right edge case. */
    1493           0 :                 move16();
    1494             :             }
    1495             :             ELSE
    1496             :             {
    1497           0 :                 special = 0;
    1498           0 :                 move16();
    1499             :             }
    1500           0 :             fraction = imax_fx( pXfp, special ); /* in Q15 */
    1501           0 :             acc = L_deposit_h( *pPlocs );
    1502           0 :             *pPlocsi = L_mac( acc, fraction, 1 );
    1503           0 :             move32(); /* in Q16. Append the fractional part to the integral part. */
    1504             :         }
    1505             :     }
    1506             :     ELSE
    1507             :     {
    1508          54 :         Lprot2 = shr( Lprot, 1 );
    1509          54 :         Lprot2_1 = add( Lprot2, 1 );
    1510             : 
    1511             :         /* Refine peaks */
    1512          54 :         pPlocsi = plocsi;
    1513          54 :         pPlocs = plocs;
    1514          54 :         n = *num_plocs; /* number of peaks to process */
    1515          54 :         move16();
    1516             : 
    1517             :         /* Special case-- The very 1st peak if it is at 0 index position (DC) */
    1518             :         /* With DELTA_CORR_F0_INT == 2 one needs to handle both *pPlocs==0 and *pPlocs==1 */
    1519             :         // IF( n > 0 && *pPlocs == 0 ) /* Very 1st peak position possible to have a peak at 0/DC index position. */
    1520          54 :         test();
    1521          54 :         IF( n > 0 && *pPlocs == 0 ) /* Very 1st peak position possible to have a peak at 0/DC index position. */
    1522             :         {
    1523           0 :             Copy_Scale_sig_16_32_no_sat( &xfp[*pPlocs], xfp_32, 3, Q15 );           // Q + 15
    1524           0 :             acc = L_deposit_h( *pPlocs );                                           // Q16
    1525           0 :             *pPlocsi++ = L_add( acc, L_shl( imax_pos_fx( xfp_32 ) /* Q15 */, 1 ) ); // Q16
    1526           0 :             move32();
    1527           0 :             pPlocs++;
    1528           0 :             n = sub( n, 1 );
    1529             :         }
    1530             : 
    1531          54 :         test();
    1532          54 :         IF( n > 0 && EQ_16( *pPlocs, 1 ) ) /* Also 2nd peak position uses DC which makes jacobsen unsuitable. */
    1533             :         {
    1534           4 :             Copy_Scale_sig_16_32_no_sat( &xfp[*pPlocs - 1], xfp_32, 3, Q15 );       // Q + 15
    1535           4 :             acc = L_deposit_h( sub( *pPlocs, 1 ) );                                 // Q16
    1536           4 :             *pPlocsi++ = L_add( acc, L_shl( imax_pos_fx( xfp_32 ) /* Q15 */, 1 ) ); // Q16
    1537           4 :             move32();
    1538           4 :             currPlocs = *pPlocs++; // Q16
    1539           4 :             move16();
    1540           4 :             n = sub( n, 1 );
    1541             :         }
    1542             : 
    1543             :         /* All remaining peaks except the very last two possible integer positions */
    1544          54 :         currPlocs = *pPlocs++;
    1545          54 :         move16();
    1546          54 :         endPlocs = sub( Lprot2_1, DELTA_CORR_F0_INT ); /* last *pPlocs position for Jacobsen */
    1547             : 
    1548             :         /* precompute number of turns based on endpoint integer location  and make into  a proper for loop */
    1549          54 :         IF( n > 0 )
    1550             :         {
    1551          54 :             nJacob = n;
    1552          54 :             move16();
    1553          54 :             if ( sub( endPlocs, plocs[*num_plocs - 1] ) <= 0 )
    1554             :             {
    1555           0 :                 nJacob = sub( nJacob, 1 );
    1556             :             }
    1557             : 
    1558         940 :             FOR( k = 0; k < nJacob; k++ )
    1559             :             {
    1560         886 :                 fraction = ivas_imax2_jacobsen_mag_fx( &( X_sav[currPlocs - 1] ), &( X_sav[Lprot - 1 - currPlocs] ) ); /* in Q15 */
    1561         886 :                 acc = L_deposit_h( currPlocs );                                                                        // Q16
    1562         886 :                 *pPlocsi++ = L_mac( acc, fraction, 1 );                                                                // Q16
    1563         886 :                 move32();
    1564         886 :                 currPlocs = *pPlocs++; // Q16
    1565         886 :                 move16();
    1566             :             }
    1567          54 :             n = sub( n, nJacob );
    1568             :         }
    1569             : 
    1570             :         /* At this point there should at most two plocs left to process */
    1571             :         /* the position before fs/2 and fs/2 both use the same magnitude points */
    1572          54 :         IF( n > 0 )
    1573             :         {
    1574             :             /* [ . . .            .  .  .  . ]   Lprot/2+1 positions  */
    1575             :             /*   |                   |     |           */
    1576             :             /*   0         (Lprot/2-2)     (Lprot/2)   */
    1577             : 
    1578           0 :             IF( EQ_16( currPlocs, ( sub( Lprot2_1, DELTA_CORR_F0_INT ) ) ) ) /* Also 2nd last peak position uses fs/2  which makes jacobsen less suitable. */
    1579             :             {
    1580           0 :                 Copy_Scale_sig_16_32_no_sat( &xfp[currPlocs - 1], xfp_32, 3, Q15 );     // Q + 15
    1581           0 :                 acc = L_deposit_h( sub( currPlocs, 1 ) );                               // Q16
    1582           0 :                 *pPlocsi++ = L_add( acc, L_shl( imax_pos_fx( xfp_32 ) /* Q15 */, 1 ) ); // Q16
    1583           0 :                 move32();
    1584           0 :                 currPlocs = *pPlocs++; // Q16
    1585           0 :                 move16();
    1586           0 :                 n = sub( n, 1 );
    1587             :             }
    1588             : 
    1589             :             /* Here the only remaining point would be a  fs/2 plocs */
    1590             :             /*    pXfp = xfp + sub(Lprot2,1); already set just a reminder where it
    1591             :              * whould point */
    1592           0 :             IF( n > 0 ) /* fs/2 which makes special case . */
    1593             :             {
    1594           0 :                 Copy_Scale_sig_16_32_no_sat( &xfp[currPlocs - 2], xfp_32, 3, Q15 );     // Q + 15
    1595           0 :                 acc = L_deposit_h( sub( currPlocs, 2 ) );                               // Q16
    1596           0 :                 *pPlocsi++ = L_add( acc, L_shl( imax_pos_fx( xfp_32 ) /* Q15 */, 1 ) ); // Q16
    1597           0 :                 move32();
    1598           0 :                 currPlocs = *pPlocs++; // Q16
    1599           0 :                 move16();
    1600           0 :                 n = sub( n, 1 );
    1601             :             }
    1602             :         }
    1603             : 
    1604             :         /* For few peaks decide noise floor attenuation */
    1605          54 :         test();
    1606          54 :         IF( LT_16( *num_plocs, 3 ) && *num_plocs > 0 )
    1607             :         {
    1608           3 :             sig = L_add( sum16_32_fx( xfp, Lprot2_1 ), 1 );
    1609             : 
    1610             :             /*excluding peaks and neighboring bins*/
    1611           9 :             FOR( i = 0; i < *num_plocs; i++ )
    1612             :             {
    1613           6 :                 st_point = s_max( 0, sub( plocs[i], DELTA_CORR ) );
    1614           6 :                 end_point = s_min( sub( Lprot2_1, 1 ), add( plocs[i], DELTA_CORR ) );
    1615           6 :                 set16_fx( &xfp[st_point], 0, extract_l( L_add( L_sub( end_point, st_point ), 1 ) ) );
    1616             :             }
    1617           3 :             noise = L_add( sum16_32_fx( xfp, Lprot2_1 ), 1 );
    1618             : 
    1619           3 :             IF( LT_32( noise, Mpy_32_32( 64424509 /* 0.03 in Q31 */, sig ) ) )
    1620             :             {
    1621           0 :                 *noise_fac = 16384; /* 0.5f in Q15 */
    1622           0 :                 move16();
    1623             :             }
    1624             :             ELSE
    1625             :             {
    1626           3 :                 *noise_fac = 32767; /* 1.0f in Q15 */
    1627           3 :                 move16();
    1628             :             }
    1629             :         }
    1630             :     }
    1631          54 : }
    1632             : 
    1633           0 : static void spec_ana_fx(
    1634             :     const Word16 *prevsynth,   /* i : Input signal                                         */
    1635             :     Word16 *plocs,             /* o : The indicies of the identified peaks             Q0  */
    1636             :     Word32 *plocsi,            /* o : Interpolated positions of the identified peaks   Q16 */
    1637             :     Word16 *num_plocs,         /* o : Number of identified peaks                       Q0  */
    1638             :     Word16 *X_sav,             /* o : Stored fft spectrum                                  */
    1639             :     const Word16 output_frame, /* i : Frame length                                     Q0  */
    1640             :     const Word16 bwidth_fx,    /* i : Encoded bandwidth index                          Q0  */
    1641             :     Word16 *Q                  /* o : Q value of the fft spectrum                          */
    1642             : )
    1643             : {
    1644             :     Word16 Lprot, LprotLog2Minus1, hamm_len2, Lprot2, Lprot2_1, m, n;
    1645           0 :     const Word16 *pFftTbl = NULL;
    1646             :     Word16 xfp[L_PROT48k];
    1647             :     Word32 magSq[L_PROT48k / 2 + 1], *pMagSq;
    1648             :     Word16 *pXfp, *pXfp1, *pXsav, *pPlocs;
    1649             :     Word16 Xmax, Xmin, sel, man, expo, expoBy2;
    1650             :     Word16 sinTblOffset, rectLength, fraction, special;
    1651             :     Word32 *pPlocsi;
    1652             :     Word32 acc;
    1653             :     Word16 stop_band_start;
    1654             :     Word16 stop_band_length;
    1655             : 
    1656           0 :     LprotLog2Minus1 = 0;
    1657           0 :     hamm_len2 = 0;
    1658           0 :     move16();    // LprotLog2Minus1
    1659           0 :     move16();    // hamm_len2
    1660           0 :     Lprot = 512; /* 1536=(2*output_frame)*1024/1280; */
    1661           0 :     move16();
    1662             : 
    1663           0 :     sinTblOffset = 0;
    1664           0 :     move16();
    1665             : 
    1666           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    1667             :     {
    1668           0 :         Lprot = L_PROT48k; /* 1536=(2*output_frame)*1024/1280; */
    1669           0 :         move16();
    1670           0 :         hamm_len2 = L_PROT_HAMM_LEN2_48k; /* half Hamming window = 288 */
    1671           0 :         move16();
    1672             :     }
    1673           0 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    1674             :     {
    1675           0 :         Lprot = L_PROT32k; /* 1024 */
    1676           0 :         move16();
    1677           0 :         sinTblOffset = 4;
    1678           0 :         move16();
    1679           0 :         hamm_len2 = L_PROT_HAMM_LEN2_32k; /* half Hamming window = 192 */
    1680           0 :         move16();
    1681           0 :         pFftTbl = FFT_W512;  /* Table for 1024-point real input FFT */
    1682           0 :         LprotLog2Minus1 = 9; /* FFT stages for complex input FFT */
    1683           0 :         move16();
    1684             :     }
    1685             :     ELSE
    1686             :     {
    1687           0 :         Lprot = 512;
    1688           0 :         move16();
    1689           0 :         sinTblOffset = 8;
    1690           0 :         move16();
    1691           0 :         hamm_len2 = L_PROT_HAMM_LEN2_16k; /* half Hamming window = 96 */
    1692           0 :         move16();
    1693           0 :         pFftTbl = FFT_W256;  /* Table for 512-point real input FFT */
    1694           0 :         LprotLog2Minus1 = 8; /* FFT stages for complex input FFT */
    1695           0 :         move16();
    1696             :     }
    1697             : 
    1698           0 :     Lprot2 = shr( Lprot, 1 );
    1699           0 :     Lprot2_1 = add( Lprot2, 1 );
    1700           0 :     rectLength = sub( Lprot, shl( hamm_len2, 1 ) ); /* The length of the rectangular portion of the Hamming-Rectangular window. */
    1701             : 
    1702           0 :     *Q = s_max( 0, sub( Exp16Array( Lprot, prevsynth ), 1 ) );
    1703           0 :     move16();
    1704           0 :     Copy_Scale_sig( prevsynth, xfp, Lprot, *Q );
    1705             : 
    1706           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    1707             :     {
    1708             :         /* Apply hamming-rect window */
    1709             :         {
    1710           0 :             windowing( xfp, xfp, w_hamm_sana48k_2_fx, rectLength, hamm_len2 );
    1711             :         }
    1712             :         /* Spectrum */
    1713           0 :         fft3_fx( xfp, xfp, Lprot );
    1714             :     }
    1715             :     ELSE
    1716             :     {
    1717             :         {
    1718             :             /* Apply hamming-rect window */
    1719           0 :             windowing_ROM_optimized( xfp, xfp, sinTblOffset, rectLength, hamm_len2 );
    1720             :         }
    1721             :         /* Spectrum */
    1722           0 :         r_fft_fx_lc( pFftTbl, Lprot, Lprot2, LprotLog2Minus1, xfp, xfp, 1 );
    1723             :     }
    1724             : 
    1725             :     /* Apply zeroing of non-coded FFT spectrum */
    1726           0 :     IF( GT_16( output_frame, inner_frame_tbl[bwidth_fx] ) )
    1727             :     {
    1728           0 :         stop_band_start = shl( 128, bwidth_fx );
    1729           0 :         stop_band_length = sub( Lprot, shl( stop_band_start, 1 ) );
    1730           0 :         stop_band_start = add( stop_band_start, 1 );
    1731           0 :         set16_fx( xfp + stop_band_start, 0, stop_band_length );
    1732             :     }
    1733             : 
    1734           0 :     pXfp = xfp;
    1735           0 :     pXsav = X_sav;
    1736           0 :     FOR( m = 0; m < Lprot; m++ )
    1737             :     {
    1738           0 :         *pXsav++ = *pXfp++;
    1739           0 :         move16();
    1740             :     }
    1741             : 
    1742             :     /* Magnitude representation */
    1743           0 :     fft_spec2_fx( xfp, magSq, Lprot );
    1744             : 
    1745             :     /* Compute xfp[m] = sqrt(magSq[m]) */
    1746           0 :     pXfp = xfp;
    1747           0 :     pMagSq = magSq;
    1748           0 :     FOR( m = 0; m < Lprot2_1; m++ )
    1749             :     {
    1750           0 :         IF( *pMagSq == 0 )
    1751             :         {
    1752           0 :             *pXfp++ = extract_l( *pMagSq++ ); /* magSq[] is zero */
    1753             :         }
    1754             :         ELSE
    1755             :         {
    1756           0 :             expo = norm_l( *pMagSq );                    /* exponent */
    1757           0 :             man = extract_h( L_shl( *pMagSq++, expo ) ); /* mantissa */
    1758           0 :             man = sqrt2ndOrder( man );
    1759           0 :             expoBy2 = shr( expo, 1 );   /* Divided by 2-- square root operation. */
    1760           0 :             IF( s_and( expo, 1 ) == 0 ) /* Check even or odd. */
    1761             :             {
    1762           0 :                 man = mult_r( man, FEC_HQ_ECU_ROOT2 ); /* FEC_HQ_ECU_ROOT2 is sqrt(2) in Q14 */
    1763           0 :                 expoBy2 = sub( expoBy2, 1 );
    1764             :             }
    1765           0 :             *pXfp++ = shr_sat( man, expoBy2 );
    1766           0 :             move16(); /* Denormalize the mantissa back to Q0. */
    1767             :         }
    1768             :     }
    1769             : 
    1770             :     /* Find maximum and minimum. */
    1771           0 :     maximum_fx( xfp, Lprot2_1, &Xmax );
    1772           0 :     minimum_fx( xfp, Lprot2_1, &Xmin );
    1773             :     {
    1774           0 :         sel = mult_r( sub( Xmax, Xmin ), CMPLMNT_PFIND_SENS_FX );
    1775             :     }
    1776           0 :     peakfinder_fx( xfp, Lprot2_1, plocs, num_plocs, sel );
    1777             : 
    1778             : 
    1779             :     {
    1780             : 
    1781             :         /* Refine peaks */
    1782           0 :         pPlocsi = plocsi;
    1783           0 :         pPlocs = plocs;
    1784           0 :         n = sub( *num_plocs, 1 ); /* -1 so as to exclude the very last peak. */
    1785             :         /* Special case-- The very 1st peak if it is at 0 index position */
    1786           0 :         IF( *pPlocs == 0 ) /* Only the very 1st peak is possible the peak at 0 index position. */
    1787             :         {
    1788           0 :             fraction = imax_fx( xfp, -1 );  /* -1 signifies special left edge case. */
    1789           0 :             acc = L_deposit_h( *pPlocs++ ); /* N.B., (*pPlocs) must be zero here. */
    1790           0 :             *pPlocsi++ = L_mac( acc, fraction, 1 );
    1791           0 :             move32();        /* in Q16 */
    1792           0 :             n = sub( n, 1 ); /* This special case is taken care of-- one less to go */
    1793             :         }
    1794             :         /* All peaks except the very last peak but including the very 1st one if it has not been taken care of. */
    1795           0 :         pXfp1 = xfp - 1;
    1796           0 :         FOR( m = 0; m < n; m++ ) /* Loop through up to the last but one peak. (The last one is excluded.) */
    1797             :         {
    1798           0 :             pXfp = pXfp1 + *pPlocs;
    1799           0 :             fraction = imax_fx( pXfp, 0 ); /* in Q15 */
    1800           0 :             acc = L_deposit_h( *pPlocs++ );
    1801           0 :             *pPlocsi++ = L_mac( acc, fraction, 1 );
    1802           0 :             move32(); /* in Q16. Append the fractional part to the integral part. */
    1803             :         }
    1804           0 :         IF( n >= 0 )
    1805             :         {
    1806             :             /* Special case-- The very last peak */
    1807           0 :             pXfp = pXfp1 + *pPlocs;
    1808           0 :             IF( EQ_16( *pPlocs, Lprot2 ) ) /* Only the very last peak is possible the peak at Lprot2 index position. */
    1809             :             {
    1810           0 :                 pXfp--;      /* Special case needs extra decrement */
    1811           0 :                 special = 1; /* Signify special right edge case. */
    1812           0 :                 move16();
    1813             :             }
    1814             :             ELSE
    1815             :             {
    1816           0 :                 special = 0;
    1817           0 :                 move16();
    1818             :             }
    1819           0 :             fraction = imax_fx( pXfp, special ); /* in Q15 */
    1820           0 :             acc = L_deposit_h( *pPlocs );
    1821           0 :             *pPlocsi = L_mac( acc, fraction, 1 );
    1822           0 :             move32(); /* in Q16. Append the fractional part to the integral part. */
    1823             :         }
    1824             :     }
    1825           0 : }
    1826             : 
    1827             : /*-------------------------------------------------------------------*
    1828             :  * subst_spec_fx()
    1829             :  *
    1830             :  * Substitution spectrum calculation
    1831             :  *-------------------------------------------------------------------*/
    1832             : 
    1833         132 : static void ivas_subst_spec_fx(
    1834             :     const Word16 *plocs,           /* i   : The indices of the identified peaks                Q0  */
    1835             :     const Word32 *plocsi,          /* i   : Interpolated positions of the identified peaks     Q16 */
    1836             :     Word16 *num_plocs,             /* i/o : Number of identified peaks                         Q0  */
    1837             :     const Word16 time_offs,        /* i   : Time offset                                        Q0  */
    1838             :     Word16 *X,                     /* i/o : FFT spectrum                                           */
    1839             :     const Word16 *mag_chg,         /* i   : Magnitude modification                             Q15 */
    1840             :     const Word16 ph_dith,          /* i   : Phase dither, 2*PI is not included. (Q15, i.e., between 0.0 and 1.0) */
    1841             :     const Word16 *is_trans,        /* i   : Transient flags (either 0 or 1)                        */
    1842             :     const Word16 output_frame,     /* i   : Frame length                                       Q0  */
    1843             :     Word16 *seed,                  /* i/o : Random seed                                            */
    1844             :     const Word16 *alpha,           /* i   : Magnitude modification factors for fade to average Q15 */
    1845             :     const Word16 *beta,            /* i   : Magnitude modification factors for fade to average Q15 */
    1846             :     Word16 beta_mute,              /* i   : Factor for long-term mute                          Q15 */
    1847             :     const Word16 *Xavg,            /* i   : Frequency group averages to fade to                Q0  */
    1848             :     const Word16 element_mode,     /* i  : IVAS element mode                                  */
    1849             :     const Word16 ph_ecu_lookahead, /* i  : Phase ECU lookahead                                */
    1850             :     const Word16 noise_fac         /* i  : noise factor                                      Q15 */
    1851             : )
    1852             : {
    1853             :     Word16 Xph_short;
    1854             :     Word32 corr_phase[MAX_PLOCS], Xph;
    1855             :     Word32 *pCorrPhase;
    1856             :     Word16 cos_F, sin_F, tmp;
    1857             :     Word16 Lprot, m, i, e, im_ind, delta_corr_up, delta_corr_dn, delta_tmp;
    1858             :     Word32 tmp1;
    1859             :     Word16 j, re, im, *pReX, *pImX, lastPeak, lprotBy2Minus1, segmentLen;
    1860             :     Word16 pkLocation_1, pkLocation, pkLocation1;
    1861             :     const Word16 *pPlocs;
    1862             :     const Word32 *pPlocsi;
    1863             :     Word32 acc;
    1864             :     Word16 Lecu;
    1865             :     Word16 Lprot_inv;
    1866             :     Word16 k;
    1867             :     Word16 tmp2;
    1868             :     Word16 alpha_local;
    1869             :     Word16 beta_local;
    1870             :     Word16 expo;
    1871             :     Word16 one_peak_flag_mask;
    1872             :     Word16 mag_chg_local; /*for peak attenuation in burst */
    1873             : 
    1874         132 :     Lprot = 512;
    1875         132 :     move16();
    1876         132 :     Lprot_inv = 8192;
    1877         132 :     move16();
    1878         132 :     Lecu = shl( output_frame, 1 );
    1879             : 
    1880         132 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    1881             :     {
    1882         103 :         Lprot = L_PROT48k; /* 1536=(2*output_frame)*1024/1280; */
    1883         103 :         move16();
    1884         103 :         Lprot_inv = 2731; /* Q22 */
    1885         103 :         move16();
    1886             :     }
    1887          29 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    1888             :     {
    1889          29 :         Lprot = L_PROT32k; /* 1024 */
    1890          29 :         move16();
    1891          29 :         Lprot_inv = 4096; /* Q22 */
    1892          29 :         move16();
    1893             :     }
    1894             :     ELSE
    1895             :     {
    1896           0 :         Lprot = 512;
    1897           0 :         move16();
    1898           0 :         Lprot_inv = 8192; /* Q22 */
    1899           0 :         move16();
    1900             :     }
    1901             : 
    1902             :     /* Correction phase of the identified peaks */
    1903         132 :     IF( s_or( is_trans[0], is_trans[1] ) != 0 )
    1904             :     {
    1905           4 :         *num_plocs = 0;
    1906           4 :         move16();
    1907             :     }
    1908             :     ELSE
    1909             :     {
    1910             :         // tmp = NS2SA(output_frame*50,PH_ECU_ALDO_OLP2_NS-PH_ECU_LOOKAHEAD_NS);
    1911         128 :         tmp = NS2SA_FX2( L_mult0( output_frame, 50 ), PH_ECU_ALDO_OLP2_NS );
    1912         128 :         move16();
    1913         128 :         tmp = sub( tmp, ph_ecu_lookahead );
    1914         128 :         tmp = add( tmp, sub( Lecu, shr( sub( Lecu, Lprot ), 1 ) ) );
    1915         128 :         tmp = sub( tmp, shr( output_frame, 1 ) );
    1916         128 :         tmp1 = L_mac0( L_mult0( tmp, Lprot_inv ), time_offs, Lprot_inv ); /* Q22 */
    1917             : 
    1918         128 :         pPlocsi = plocsi;
    1919         128 :         pCorrPhase = corr_phase;
    1920        5637 :         FOR( m = 0; m < *num_plocs; m++ )
    1921             :         {
    1922        5509 :             acc = L_shl( Mpy_32_32( *pPlocsi++, tmp1 ), 9 ); /* 16+22+9-31 = 16*/
    1923        5509 :             *pCorrPhase++ = acc;                             /* in Q16. 2*PI is not included. */
    1924        5509 :             move32();
    1925             :         }
    1926             :     }
    1927         132 :     one_peak_flag_mask = 32767 /* 1.0f in Q15 */; /* all ones mask -> keep  */
    1928         132 :     move16();
    1929         132 :     IF( element_mode != EVS_MONO )
    1930             :     {
    1931         132 :         test();
    1932         132 :         if ( ( *num_plocs > 0 ) && ( sub( *num_plocs, 3 ) < 0 ) )
    1933             :         {
    1934           3 :             one_peak_flag_mask = noise_fac; /* all zeroes  mask -> zero  */
    1935           3 :             move16();
    1936             :         }
    1937         132 :         IF( *num_plocs == 0 )
    1938             :         {
    1939           4 :             X[0] = 0; /* reset DC if there are no  peaks */
    1940           4 :             move16();
    1941           4 :             X[Lprot / 2] = 0; /* also reset fs/2 if there are no peaks */
    1942           4 :             move16();
    1943             :         }
    1944             :     }
    1945         132 :     lprotBy2Minus1 = sub( shr( Lprot, 1 ), 1 );
    1946         132 :     i = 1;
    1947         132 :     move16();
    1948         132 :     k = 0;
    1949         132 :     move16();
    1950         132 :     im_ind = sub( Lprot, 1 );
    1951         132 :     move16();
    1952         132 :     pReX = X + i;
    1953         132 :     pImX = X + im_ind;
    1954         132 :     pPlocs = plocs;
    1955         132 :     pCorrPhase = corr_phase;
    1956         132 :     pkLocation = *pPlocs; /* N.B. No post-increment */
    1957         132 :     move16();
    1958         132 :     pkLocation1 = *pPlocs++;
    1959         132 :     move16();
    1960         132 :     lastPeak = sub( *num_plocs, 1 );
    1961        5641 :     FOR( m = 0; m < *num_plocs; m++ )
    1962             :     {
    1963        5509 :         delta_corr_dn = DELTA_CORR;
    1964        5509 :         move16();
    1965        5509 :         delta_corr_up = DELTA_CORR;
    1966        5509 :         move16();
    1967             : 
    1968        5509 :         pkLocation_1 = pkLocation; /* plocs[m - 1] */
    1969        5509 :         move16();
    1970        5509 :         pkLocation = pkLocation1; /* plocs[m] */
    1971        5509 :         move16();
    1972        5509 :         pkLocation1 = *pPlocs++; /* plocs[m + 1] */
    1973        5509 :         move16();
    1974        5509 :         IF( m > 0 )
    1975             :         {
    1976        5381 :             delta_tmp = shr( sub( sub( pkLocation, pkLocation_1 ), 1 ), 1 );
    1977        5381 :             if ( LT_16( delta_tmp, DELTA_CORR ) )
    1978             :             {
    1979        4626 :                 delta_corr_dn = delta_tmp;
    1980        4626 :                 move16();
    1981             :             }
    1982             :         }
    1983             : 
    1984        5509 :         IF( LT_16( m, lastPeak ) )
    1985             :         {
    1986        5381 :             delta_tmp = shr( sub( sub( pkLocation1, pkLocation ), 1 ), 1 );
    1987        5381 :             if ( LT_16( delta_tmp, DELTA_CORR ) )
    1988             :             {
    1989        4626 :                 delta_corr_up = delta_tmp;
    1990        4626 :                 move16();
    1991             :             }
    1992             :         }
    1993             : 
    1994             :         /* Input Xph */
    1995        5509 :         segmentLen = sub( sub( pkLocation, delta_corr_dn ), i );
    1996             :         /* i = add(i, segmentLen); */
    1997       14047 :         FOR( j = 0; j < segmentLen; j++ )
    1998             :         {
    1999        8538 :             *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2000             : 
    2001        8538 :             re = *pReX;
    2002        8538 :             move16();
    2003        8538 :             im = *pImX;
    2004        8538 :             move16();
    2005        8538 :             IF( element_mode == EVS_MONO )
    2006             :             {
    2007           0 :                 tmp = sub( mult_r( re, cos_F ), mult_r( im, sin_F ) );
    2008           0 :                 im = add( mult_r( re, sin_F ), mult_r( im, cos_F ) );
    2009             :             }
    2010             :             ELSE
    2011             :             {
    2012        8538 :                 tmp = mult_r( one_peak_flag_mask, sub( mult_r( re, cos_F ), mult_r( im, sin_F ) ) );
    2013        8538 :                 im = mult_r( one_peak_flag_mask, add( mult_r( re, sin_F ), mult_r( im, cos_F ) ) );
    2014             :             }
    2015        8538 :             IF( LT_16( alpha[k], 32766 ) )
    2016             :             {
    2017        5180 :                 *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2018        5180 :                 move16();
    2019        5180 :                 tmp2 = mult_r( beta[k], Xavg[k] );
    2020        5180 :                 *pReX++ = add( mult_r( alpha[k], tmp ), mult_r( tmp2, cos_F ) );
    2021        5180 :                 move16();
    2022        5180 :                 *pImX-- = add( mult_r( alpha[k], im ), mult_r( tmp2, sin_F ) );
    2023        5180 :                 move16();
    2024             :             }
    2025             :             ELSE
    2026             :             {
    2027        3358 :                 *pReX++ = mult_r( mag_chg[k], tmp );
    2028        3358 :                 move16();
    2029        3358 :                 *pImX-- = mult_r( mag_chg[k], im );
    2030        3358 :                 move16();
    2031             :             }
    2032        8538 :             i = add( i, 1 );
    2033        8538 :             if ( GE_16( i, ivas_gwlpr[k + 1] ) )
    2034             :             {
    2035          70 :                 k = add( k, 1 );
    2036             :             }
    2037             :         }
    2038             : 
    2039        5509 :         e = add( pkLocation, delta_corr_up );
    2040        5509 :         if ( GT_16( e, lprotBy2Minus1 ) )
    2041             :         {
    2042           0 :             e = lprotBy2Minus1;
    2043           0 :             move16();
    2044             :         }
    2045             : 
    2046        5509 :         Xph = *pCorrPhase;
    2047        5509 :         move32();
    2048        5509 :         Xph_short = s_and( extract_l( L_shr( Xph, 16 - 10 ) ), 0x3ff ); /* 10 bits precision after radix point */
    2049        5509 :         IF( GE_16( Xph_short, 512 ) )
    2050             :         {
    2051        2761 :             sin_F = negate( sincos_t_ext_fx[Xph_short - 512] );
    2052        2761 :             IF( LT_16( Xph_short, 768 ) )
    2053             :             {
    2054        1405 :                 cos_F = negate( sincos_t_ext_fx[Xph_short - ( 512 - 256 )] );
    2055             :             }
    2056             :             ELSE
    2057             :             {
    2058        1356 :                 cos_F = sincos_t_ext_fx[-Xph_short + ( 1024 + 256 )];
    2059        1356 :                 move16();
    2060             :             }
    2061             :         }
    2062             :         ELSE
    2063             :         {
    2064        2748 :             sin_F = sincos_t_ext_fx[Xph_short];
    2065        2748 :             move16();
    2066        2748 :             IF( LT_16( Xph_short, 256 ) )
    2067             :             {
    2068        1420 :                 cos_F = sincos_t_ext_fx[Xph_short + 256];
    2069        1420 :                 move16();
    2070             :             }
    2071             :             ELSE
    2072             :             {
    2073        1328 :                 cos_F = negate( sincos_t_ext_fx[-Xph_short + ( 256 + 512 )] );
    2074             :             }
    2075             :         }
    2076             : 
    2077        5509 :         segmentLen = add( sub( e, i ), 1 );
    2078             :         /* i = add(i, segmentLen); */
    2079       40815 :         FOR( j = 0; j < segmentLen; j++ )
    2080             :         {
    2081       35306 :             mag_chg_local = mag_chg[k];
    2082       35306 :             move16();
    2083       35306 :             IF( ph_dith != 0 )
    2084             :             {
    2085       25812 :                 Xph = *pCorrPhase; /* in Q16. 2*PI is not included. */
    2086       25812 :                 move32();
    2087       25812 :                 Random( seed );                 /* in Q0 */
    2088       25812 :                 acc = L_mult( *seed, ph_dith ); /* N.B. ph_dith[i] is in Q15, i.e., in between 0 and 1.0 (2*PI not included) */
    2089       25812 :                 acc = L_shr( acc, PHASE_DITH_SCALE_SHIFT );
    2090       25812 :                 Xph = L_add( Xph, acc ); /* in Q16. */
    2091             : 
    2092       25812 :                 IF( ph_dith > 0 ) /* up to 6 dB additional att of peaks in non_transient longer bursts, (when  peak phase is randomized ) */
    2093             :                 {
    2094             :                     /* mag_chg_local *= 0.5 + (1.0 - ph_dith[i])/2 where 0.5~= sqrt((float)pow(10.0,-6/10.0)) and ph_dith=0..1.0--> scale=1.0 ...5 */
    2095       25812 :                     mag_chg_local = mult_r( mag_chg_local, sub( 32767, shr( ph_dith, 1 ) ) );
    2096             :                 }
    2097       25812 :                 Xph_short = s_and( extract_l( L_shr( Xph, 16 - 10 ) ), 0x3ff );
    2098       25812 :                 IF( GE_16( Xph_short, 512 ) )
    2099             :                 {
    2100       12722 :                     sin_F = negate( sincos_t_ext_fx[Xph_short - 512] );
    2101       12722 :                     IF( LT_16( Xph_short, 768 ) )
    2102             :                     {
    2103        6328 :                         cos_F = negate( sincos_t_ext_fx[Xph_short - ( 512 - 256 )] );
    2104             :                     }
    2105             :                     ELSE
    2106             :                     {
    2107        6394 :                         cos_F = sincos_t_ext_fx[-Xph_short + ( 1024 + 256 )];
    2108        6394 :                         move16();
    2109             :                     }
    2110             :                 }
    2111             :                 ELSE
    2112             :                 {
    2113       13090 :                     sin_F = sincos_t_ext_fx[Xph_short];
    2114       13090 :                     move16();
    2115       13090 :                     IF( LT_16( Xph_short, 256 ) )
    2116             :                     {
    2117        6521 :                         cos_F = sincos_t_ext_fx[Xph_short + 256];
    2118        6521 :                         move16();
    2119             :                     }
    2120             :                     ELSE
    2121             :                     {
    2122        6569 :                         cos_F = negate( sincos_t_ext_fx[-Xph_short + ( 256 + 512 )] );
    2123             :                     }
    2124             :                 }
    2125             :             }
    2126             : 
    2127       35306 :             re = *pReX;
    2128       35306 :             move16();
    2129       35306 :             im = *pImX;
    2130       35306 :             move16();
    2131       35306 :             tmp = sub_sat( mult_r( re, cos_F ), mult_r( im, sin_F ) );
    2132       35306 :             im = add( mult_r( re, sin_F ), mult_r( im, cos_F ) );
    2133       35306 :             IF( LT_16( alpha[k], 32766 ) )
    2134             :             {
    2135       26529 :                 alpha_local = mag_chg_local;
    2136       26529 :                 move16();
    2137             : 
    2138       26529 :                 acc = L_sub( 1073741824L, L_mult0( alpha_local, alpha_local ) );
    2139       26529 :                 acc = Sqrt_l( acc, &expo );
    2140       26529 :                 expo = add( 30, add( 31, expo ) );
    2141       26529 :                 if ( EQ_16( s_and( expo, 1 ), 1 ) )
    2142             :                 {
    2143       26529 :                     acc = Mult_32_16( acc, 23170 ); /* 1/sqrt(2) in Q15 */
    2144             :                 }
    2145       26529 :                 expo = shr( expo, 1 );
    2146       26529 :                 beta_local = mult_r( beta_mute, round_fx( L_shl( acc, sub( 31, expo ) ) ) );
    2147             : 
    2148       26529 :                 IF( GE_16( k, LGW32K - 1 ) )
    2149             :                 {
    2150       10138 :                     beta_local = mult_r( beta_local, 3277 ); /* 0.1 in Q15 */
    2151             :                 }
    2152       16391 :                 ELSE IF( GE_16( k, LGW16K - 1 ) )
    2153             :                 {
    2154        8325 :                     beta_local = mult_r( beta_local, 16384 ); /* 0.5 in Q15 */
    2155             :                 }
    2156             : 
    2157       26529 :                 *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2158       26529 :                 move16();
    2159       26529 :                 tmp2 = mult_r( beta_local, Xavg[k] );
    2160       26529 :                 *pReX++ = add( mult_r( alpha_local, tmp ), mult_r( tmp2, cos_F ) );
    2161       26529 :                 move16();
    2162       26529 :                 *pImX-- = add( mult_r( alpha_local, im ), mult_r( tmp2, sin_F ) );
    2163       26529 :                 move16();
    2164             :             }
    2165             :             ELSE
    2166             :             {
    2167        8777 :                 *pReX++ = mult_r( mag_chg_local, tmp );
    2168        8777 :                 move16();
    2169        8777 :                 *pImX-- = mult_r( mag_chg_local, im );
    2170        8777 :                 move16();
    2171             :             }
    2172             : 
    2173       35306 :             i = add( i, 1 );
    2174       35306 :             if ( GE_16( i, ivas_gwlpr[k + 1] ) )
    2175             :             {
    2176         637 :                 k = add( k, 1 );
    2177             :             }
    2178             :         }
    2179        5509 :         pCorrPhase++;
    2180             :     }
    2181             : 
    2182         132 :     segmentLen = sub( shr( Lprot, 1 ), i );
    2183       50108 :     FOR( j = 0; j < segmentLen; j++ )
    2184             :     {
    2185       49976 :         *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2186       49976 :         move16();
    2187             : 
    2188       49976 :         re = *pReX;
    2189       49976 :         move16();
    2190       49976 :         im = *pImX;
    2191       49976 :         move16();
    2192             : 
    2193             : 
    2194       49976 :         IF( element_mode == EVS_MONO )
    2195             :         {
    2196           0 :             tmp = sub( mult_r( re, cos_F ), mult_r( im, sin_F ) );
    2197           0 :             im = add( mult_r( re, sin_F ), mult_r( im, cos_F ) );
    2198             :         }
    2199             :         ELSE
    2200             :         {
    2201       49976 :             tmp = mult_r( one_peak_flag_mask, sub( mult_r( re, cos_F ), mult_r( im, sin_F ) ) );
    2202       49976 :             im = mult_r( one_peak_flag_mask, add( mult_r( re, sin_F ), mult_r( im, cos_F ) ) );
    2203             :         }
    2204       49976 :         IF( LT_16( alpha[k], 32766 ) )
    2205             :         {
    2206       25049 :             *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2207       25049 :             move16();
    2208       25049 :             tmp2 = mult_r( beta[k], Xavg[k] );
    2209       25049 :             *pReX++ = add( mult_r( alpha[k], tmp ), mult_r( tmp2, cos_F ) );
    2210       25049 :             move16();
    2211       25049 :             *pImX-- = add( mult_r( alpha[k], im ), mult_r( tmp2, sin_F ) );
    2212       25049 :             move16();
    2213             :         }
    2214             :         ELSE
    2215             :         {
    2216       24927 :             *pReX++ = mult_r( mag_chg[k], tmp );
    2217       24927 :             move16();
    2218       24927 :             *pImX-- = mult_r( mag_chg[k], im );
    2219       24927 :             move16();
    2220             :         }
    2221             : 
    2222       49976 :         i = add( i, 1 );
    2223       49976 :         if ( GE_16( i, ivas_gwlpr[k + 1] ) )
    2224             :         {
    2225         320 :             k = add( k, 1 );
    2226             :         }
    2227             :     }
    2228         132 : }
    2229             : 
    2230           0 : static void subst_spec_fx(
    2231             :     const Word16 *plocs,       /* i   : The indices of the identified peaks                Q0  */
    2232             :     const Word32 *plocsi,      /* i   : Interpolated positions of the identified peaks     Q16 */
    2233             :     Word16 *num_plocs,         /* i/o : Number of identified peaks                         Q0  */
    2234             :     const Word16 time_offs,    /* i   : Time offset                                        Q0  */
    2235             :     Word16 *X,                 /* i/o : FFT spectrum                                           */
    2236             :     const Word16 *mag_chg,     /* i   : Magnitude modification                             Q15 */
    2237             :     const Word16 ph_dith,      /* i   : Phase dither, 2*PI is not included. (Q15, i.e., between 0.0 and 1.0) */
    2238             :     const Word16 *is_trans,    /* i   : Transient flags (either 0 or 1)                        */
    2239             :     const Word16 output_frame, /* i   : Frame length                                       Q0  */
    2240             :     Word16 *seed,              /* i/o : Random seed                                            */
    2241             :     const Word16 *alpha,       /* i   : Magnitude modification factors for fade to average Q15 */
    2242             :     const Word16 *beta,        /* i   : Magnitude modification factors for fade to average Q15 */
    2243             :     Word16 beta_mute,          /* i   : Factor for long-term mute                          Q15 */
    2244             :     const Word16 *Xavg         /* i   : Frequency group averages to fade to                Q0  */
    2245             : )
    2246             : {
    2247             :     Word16 Xph_short;
    2248             :     Word32 corr_phase[MAX_PLOCS], Xph;
    2249             :     Word32 *pCorrPhase;
    2250             :     Word16 cos_F, sin_F, tmp;
    2251             :     Word16 Lprot, m, i, e, im_ind, delta_corr_up, delta_corr_dn, delta_tmp;
    2252             :     UWord16 lsb;
    2253             :     Word16 j, re, im, *pReX, *pImX, lastPeak, lprotBy2Minus1, segmentLen;
    2254             :     Word16 pkLocation_1, pkLocation, pkLocation1;
    2255             :     const Word16 *pPlocs;
    2256             :     const Word32 *pPlocsi;
    2257             :     Word32 acc;
    2258             :     Word16 Lecu;
    2259             :     Word16 Lprot_inv;
    2260             :     Word16 k;
    2261             :     Word16 tmp2;
    2262             :     Word16 alpha_local;
    2263             :     Word16 beta_local;
    2264             :     Word16 expo;
    2265             :     Word16 mag_chg_local; /*for peak attenuation in burst */
    2266             : 
    2267           0 :     Lprot = 512;
    2268           0 :     move16();
    2269           0 :     Lprot_inv = 8192;
    2270           0 :     move16();
    2271           0 :     Lecu = shl( output_frame, 1 );
    2272             : 
    2273           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2274             :     {
    2275           0 :         Lprot = L_PROT48k; /* 1536=(2*output_frame)*1024/1280; */
    2276           0 :         move16();
    2277           0 :         Lprot_inv = 2731; /* Q22 */
    2278           0 :         move16();
    2279             :     }
    2280           0 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    2281             :     {
    2282           0 :         Lprot = L_PROT32k; /* 1024 */
    2283           0 :         move16();
    2284           0 :         Lprot_inv = 4096; /* Q22 */
    2285           0 :         move16();
    2286             :     }
    2287             :     ELSE
    2288             :     {
    2289           0 :         Lprot = 512;
    2290           0 :         move16();
    2291           0 :         Lprot_inv = 8192; /* Q22 */
    2292           0 :         move16();
    2293             :     }
    2294             : 
    2295             :     /* Correction phase of the identified peaks */
    2296           0 :     IF( s_or( is_trans[0], is_trans[1] ) != 0 )
    2297             :     {
    2298           0 :         *num_plocs = 0;
    2299           0 :         move16();
    2300             :     }
    2301             :     ELSE
    2302             :     {
    2303           0 :         tmp = NS2SA_FX2( L_mult0( output_frame, 50 ), PH_ECU_ALDO_OLP2_NS - PH_ECU_LOOKAHEAD_NS );
    2304           0 :         move16();
    2305           0 :         tmp = add( tmp, sub( Lecu, shr( sub( Lecu, Lprot ), 1 ) ) );
    2306           0 :         tmp = sub( tmp, shr( output_frame, 1 ) );
    2307           0 :         tmp = add( tmp, time_offs );
    2308           0 :         tmp = round_fx( L_shl( L_mult0( tmp, Lprot_inv ), 4 ) ); /* 0+22+4-16=10 */
    2309             : 
    2310           0 :         pPlocsi = plocsi;
    2311           0 :         pCorrPhase = corr_phase;
    2312           0 :         FOR( m = 0; m < *num_plocs; m++ )
    2313             :         {
    2314           0 :             Mpy_32_16_ss( *pPlocsi++, tmp, &acc, &lsb ); /* plocsi[] in Q16, tmp in Q10 and tmp does not include 2*PI. */
    2315           0 :             acc = L_add( L_shl( acc, 5 ), lshr( lsb, 11 ) );
    2316           0 :             *pCorrPhase++ = acc; /* in Q16. 2*PI is not included. */
    2317           0 :             move32();
    2318             :         }
    2319             :     }
    2320           0 :     lprotBy2Minus1 = sub( shr( Lprot, 1 ), 1 );
    2321           0 :     i = 1;
    2322           0 :     move16();
    2323           0 :     k = 0;
    2324           0 :     move16();
    2325           0 :     im_ind = sub( Lprot, 1 );
    2326           0 :     move16();
    2327           0 :     pReX = X + i;
    2328           0 :     pImX = X + im_ind;
    2329           0 :     pPlocs = plocs;
    2330           0 :     pCorrPhase = corr_phase;
    2331           0 :     pkLocation = *pPlocs; /* N.B. No post-increment */
    2332           0 :     move16();
    2333           0 :     pkLocation1 = *pPlocs++;
    2334           0 :     move16();
    2335           0 :     lastPeak = sub( *num_plocs, 1 );
    2336           0 :     FOR( m = 0; m < *num_plocs; m++ )
    2337             :     {
    2338           0 :         delta_corr_dn = DELTA_CORR;
    2339           0 :         move16();
    2340           0 :         delta_corr_up = DELTA_CORR;
    2341           0 :         move16();
    2342             : 
    2343           0 :         pkLocation_1 = pkLocation; /* plocs[m - 1] */
    2344           0 :         move16();
    2345           0 :         pkLocation = pkLocation1; /* plocs[m] */
    2346           0 :         move16();
    2347           0 :         pkLocation1 = *pPlocs++; /* plocs[m + 1] */
    2348           0 :         move16();
    2349           0 :         IF( m > 0 )
    2350             :         {
    2351           0 :             delta_tmp = shr( sub( sub( pkLocation, pkLocation_1 ), 1 ), 1 );
    2352           0 :             if ( LT_16( delta_tmp, DELTA_CORR ) )
    2353             :             {
    2354           0 :                 delta_corr_dn = delta_tmp;
    2355           0 :                 move16();
    2356             :             }
    2357             :         }
    2358             : 
    2359           0 :         IF( LT_16( m, lastPeak ) )
    2360             :         {
    2361           0 :             delta_tmp = shr( sub( sub( pkLocation1, pkLocation ), 1 ), 1 );
    2362           0 :             if ( LT_16( delta_tmp, DELTA_CORR ) )
    2363             :             {
    2364           0 :                 delta_corr_up = delta_tmp;
    2365           0 :                 move16();
    2366             :             }
    2367             :         }
    2368             : 
    2369             :         /* Input Xph */
    2370           0 :         segmentLen = sub( sub( pkLocation, delta_corr_dn ), i );
    2371             :         /* i = add(i, segmentLen); */
    2372           0 :         FOR( j = 0; j < segmentLen; j++ )
    2373             :         {
    2374           0 :             *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2375             : 
    2376           0 :             re = *pReX;
    2377           0 :             move16();
    2378           0 :             im = *pImX;
    2379           0 :             move16();
    2380             :             {
    2381           0 :                 tmp = sub( mult_r( re, cos_F ), mult_r( im, sin_F ) );
    2382           0 :                 im = add( mult_r( re, sin_F ), mult_r( im, cos_F ) );
    2383             :             }
    2384           0 :             IF( LT_16( alpha[k], 32766 ) )
    2385             :             {
    2386           0 :                 *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2387           0 :                 move16();
    2388           0 :                 tmp2 = mult_r( beta[k], Xavg[k] );
    2389           0 :                 *pReX++ = add( mult_r( alpha[k], tmp ), mult_r( tmp2, cos_F ) );
    2390           0 :                 move16();
    2391           0 :                 *pImX-- = add( mult_r( alpha[k], im ), mult_r( tmp2, sin_F ) );
    2392           0 :                 move16();
    2393             :             }
    2394             :             ELSE
    2395             :             {
    2396           0 :                 *pReX++ = mult_r( mag_chg[k], tmp );
    2397           0 :                 move16();
    2398           0 :                 *pImX-- = mult_r( mag_chg[k], im );
    2399           0 :                 move16();
    2400             :             }
    2401           0 :             i = add( i, 1 );
    2402           0 :             if ( GE_16( i, ivas_gwlpr[k + 1] ) )
    2403             :             {
    2404           0 :                 k = add( k, 1 );
    2405             :             }
    2406             :         }
    2407             : 
    2408           0 :         e = add( pkLocation, delta_corr_up );
    2409           0 :         if ( GT_16( e, lprotBy2Minus1 ) )
    2410             :         {
    2411           0 :             e = lprotBy2Minus1;
    2412           0 :             move16();
    2413             :         }
    2414             : 
    2415           0 :         Xph = *pCorrPhase;
    2416           0 :         move32();
    2417           0 :         Xph_short = s_and( extract_l( L_shr( Xph, 16 - 10 ) ), 0x3ff ); /* 10 bits precision after radix point */
    2418           0 :         IF( GE_16( Xph_short, 512 ) )
    2419             :         {
    2420           0 :             sin_F = negate( sincos_t_ext_fx[Xph_short - 512] );
    2421           0 :             IF( LT_16( Xph_short, 768 ) )
    2422             :             {
    2423           0 :                 cos_F = negate( sincos_t_ext_fx[Xph_short - ( 512 - 256 )] );
    2424             :             }
    2425             :             ELSE
    2426             :             {
    2427           0 :                 cos_F = sincos_t_ext_fx[-Xph_short + ( 1024 + 256 )];
    2428           0 :                 move16();
    2429             :             }
    2430             :         }
    2431             :         ELSE
    2432             :         {
    2433           0 :             sin_F = sincos_t_ext_fx[Xph_short];
    2434           0 :             move16();
    2435           0 :             IF( LT_16( Xph_short, 256 ) )
    2436             :             {
    2437           0 :                 cos_F = sincos_t_ext_fx[Xph_short + 256];
    2438           0 :                 move16();
    2439             :             }
    2440             :             ELSE
    2441             :             {
    2442           0 :                 cos_F = negate( sincos_t_ext_fx[-Xph_short + ( 256 + 512 )] );
    2443             :             }
    2444             :         }
    2445             : 
    2446           0 :         segmentLen = add( sub( e, i ), 1 );
    2447             :         /* i = add(i, segmentLen); */
    2448           0 :         FOR( j = 0; j < segmentLen; j++ )
    2449             :         {
    2450           0 :             mag_chg_local = mag_chg[k];
    2451           0 :             move16();
    2452           0 :             IF( ph_dith != 0 )
    2453             :             {
    2454           0 :                 Xph = *pCorrPhase; /* in Q16. 2*PI is not included. */
    2455           0 :                 move32();
    2456           0 :                 Random( seed );                 /* in Q0 */
    2457           0 :                 acc = L_mult( *seed, ph_dith ); /* N.B. ph_dith[i] is in Q15, i.e., in between 0 and 1.0 (2*PI not included) */
    2458           0 :                 acc = L_shr( acc, PHASE_DITH_SCALE_SHIFT );
    2459           0 :                 Xph = L_add( Xph, acc ); /* in Q16. */
    2460             : 
    2461           0 :                 IF( ph_dith > 0 ) /* up to 6 dB additional att of peaks in non_transient longer bursts, (when  peak phase is randomized ) */
    2462             :                 {
    2463             :                     /* mag_chg_local *= 0.5 + (1.0 - ph_dith[i])/2 where 0.5~= sqrt((float)pow(10.0,-6/10.0)) and ph_dith=0..1.0--> scale=1.0 ...5 */
    2464           0 :                     mag_chg_local = mult_r( mag_chg_local, sub( 32767, shr( ph_dith, 1 ) ) );
    2465             :                 }
    2466           0 :                 Xph_short = s_and( extract_l( L_shr( Xph, 16 - 10 ) ), 0x3ff );
    2467           0 :                 IF( GE_16( Xph_short, 512 ) )
    2468             :                 {
    2469           0 :                     sin_F = negate( sincos_t_ext_fx[Xph_short - 512] );
    2470           0 :                     IF( LT_16( Xph_short, 768 ) )
    2471             :                     {
    2472           0 :                         cos_F = negate( sincos_t_ext_fx[Xph_short - ( 512 - 256 )] );
    2473             :                     }
    2474             :                     ELSE
    2475             :                     {
    2476           0 :                         cos_F = sincos_t_ext_fx[-Xph_short + ( 1024 + 256 )];
    2477           0 :                         move16();
    2478             :                     }
    2479             :                 }
    2480             :                 ELSE
    2481             :                 {
    2482           0 :                     sin_F = sincos_t_ext_fx[Xph_short];
    2483           0 :                     move16();
    2484           0 :                     IF( LT_16( Xph_short, 256 ) )
    2485             :                     {
    2486           0 :                         cos_F = sincos_t_ext_fx[Xph_short + 256];
    2487           0 :                         move16();
    2488             :                     }
    2489             :                     ELSE
    2490             :                     {
    2491           0 :                         cos_F = negate( sincos_t_ext_fx[-Xph_short + ( 256 + 512 )] );
    2492             :                     }
    2493             :                 }
    2494             :             }
    2495             : 
    2496           0 :             re = *pReX;
    2497           0 :             move16();
    2498           0 :             im = *pImX;
    2499           0 :             move16();
    2500             :             {
    2501           0 :                 tmp = sub_sat( mult_r( re, cos_F ), mult_r( im, sin_F ) );
    2502           0 :                 im = add_sat( mult_r( re, sin_F ), mult_r( im, cos_F ) );
    2503             :             }
    2504           0 :             IF( LT_16( alpha[k], 32766 ) )
    2505             :             {
    2506           0 :                 alpha_local = mag_chg_local;
    2507           0 :                 move16();
    2508             : 
    2509           0 :                 acc = L_sub( 1073741824L, L_mult0( alpha_local, alpha_local ) );
    2510           0 :                 acc = Sqrt_l( acc, &expo );
    2511           0 :                 expo = add( 30, add( 31, expo ) );
    2512           0 :                 if ( EQ_16( s_and( expo, 1 ), 1 ) )
    2513             :                 {
    2514           0 :                     acc = Mult_32_16( acc, 23170 ); /* 1/sqrt(2) in Q15 */
    2515             :                 }
    2516           0 :                 expo = shr( expo, 1 );
    2517           0 :                 beta_local = mult_r( beta_mute, round_fx( L_shl( acc, sub( 31, expo ) ) ) );
    2518             : 
    2519           0 :                 IF( GE_16( k, LGW32K - 1 ) )
    2520             :                 {
    2521           0 :                     beta_local = mult_r( beta_local, 3277 ); /* 0.1 in Q15 */
    2522             :                 }
    2523           0 :                 ELSE IF( GE_16( k, LGW16K - 1 ) )
    2524             :                 {
    2525           0 :                     beta_local = mult_r( beta_local, 16384 ); /* 0.5 in Q15 */
    2526             :                 }
    2527             : 
    2528           0 :                 *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2529           0 :                 move16();
    2530           0 :                 tmp2 = mult_r( beta_local, Xavg[k] );
    2531           0 :                 *pReX++ = add( mult_r( alpha_local, tmp ), mult_r( tmp2, cos_F ) );
    2532           0 :                 move16();
    2533           0 :                 *pImX-- = add( mult_r( alpha_local, im ), mult_r( tmp2, sin_F ) );
    2534           0 :                 move16();
    2535             :             }
    2536             :             ELSE
    2537             :             {
    2538           0 :                 *pReX++ = mult_r( mag_chg_local, tmp );
    2539           0 :                 move16();
    2540           0 :                 *pImX-- = mult_r( mag_chg_local, im );
    2541           0 :                 move16();
    2542             :             }
    2543             : 
    2544           0 :             i = add( i, 1 );
    2545           0 :             if ( GE_16( i, ivas_gwlpr[k + 1] ) )
    2546             :             {
    2547           0 :                 k = add( k, 1 );
    2548             :             }
    2549             :         }
    2550           0 :         pCorrPhase++;
    2551             :     }
    2552             : 
    2553           0 :     segmentLen = sub( shr( Lprot, 1 ), i );
    2554           0 :     FOR( j = 0; j < segmentLen; j++ )
    2555             :     {
    2556           0 :         *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2557           0 :         move16();
    2558             : 
    2559           0 :         re = *pReX;
    2560           0 :         move16();
    2561           0 :         im = *pImX;
    2562           0 :         move16();
    2563           0 :         tmp = sub( mult_r( re, cos_F ), mult_r( im, sin_F ) );
    2564           0 :         im = add( mult_r( re, sin_F ), mult_r( im, cos_F ) );
    2565           0 :         IF( LT_16( alpha[k], 32766 ) )
    2566             :         {
    2567           0 :             *seed = rand_phase_fx( *seed, &sin_F, &cos_F );
    2568           0 :             move16();
    2569           0 :             tmp2 = mult_r( beta[k], Xavg[k] );
    2570           0 :             *pReX++ = add( mult_r( alpha[k], tmp ), mult_r( tmp2, cos_F ) );
    2571           0 :             move16();
    2572           0 :             *pImX-- = add( mult_r( alpha[k], im ), mult_r( tmp2, sin_F ) );
    2573           0 :             move16();
    2574             :         }
    2575             :         ELSE
    2576             :         {
    2577           0 :             *pReX++ = mult_r( mag_chg[k], tmp );
    2578           0 :             move16();
    2579           0 :             *pImX-- = mult_r( mag_chg[k], im );
    2580           0 :             move16();
    2581             :         }
    2582             : 
    2583           0 :         i = add( i, 1 );
    2584           0 :         if ( GE_16( i, ivas_gwlpr[k + 1] ) )
    2585             :         {
    2586           0 :             k = add( k, 1 );
    2587             :         }
    2588             :     }
    2589           0 : }
    2590             : 
    2591             : /*--------------------------------------------------------------------------
    2592             :  *  rec_wtda()
    2593             :  *
    2594             :  *  Windowing and TDA of reconstructed frame
    2595             :  *--------------------------------------------------------------------------*/
    2596             : 
    2597         132 : static void ivas_rec_wtda_fx(
    2598             :     Word16 *X,                 /* i  : FFT spectrum                          */
    2599             :     Word32 *ecu_rec,           /* o  : Reconstructed frame in tda domain     */
    2600             :     const Word16 output_frame, /* i  : Frame length                          */
    2601             :     const Word16 Lprot,        /* i  : Prototype frame length                */
    2602             :     const Word16 old_dec[270], /* i  : end of last decoded for OLA before tda and itda */
    2603             :     const Word16 element_mode, /* i  : IVAS element mode                   */
    2604             :     const Word16 *num_p,       /* i  : Number of peaks Q0                     */
    2605             :     const Word16 *plocs        /* i  : Peak locations Q0                      */
    2606             : )
    2607             : {
    2608             :     Word16 timesh;
    2609             :     Word16 Qin;
    2610             :     Word16 xf_len;
    2611             :     Word16 i, idx;
    2612             :     Word16 *p_ecu;
    2613             :     Word16 g;
    2614             :     Word16 tbl_delta;
    2615             :     Word16 xsubst_[2 * L_FRAME48k];
    2616             :     const Word16 *w_hamm;
    2617             :     Word16 *pX_start, *pX_end;
    2618             :     Word16 tmp, tmp_e;
    2619             :     Word16 hamm_len2;
    2620             :     Word16 *pNew;
    2621             :     const Word16 *pOldW, *pNewW;
    2622             :     Word16 xfwin[NS2SA( L_FRAME48k * FRAMES_PER_SEC, N_ZERO_MDCT_NS - ( 2 * FRAME_SIZE_NS - L_PROT_NS ) / 2 )];
    2623             :     const Word16 *pOld;
    2624             :     Word16 copy_len;
    2625             :     Word16 ola_len;
    2626             : 
    2627         132 :     copy_len = NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), ( 2 * FRAME_SIZE_NS - L_PROT_NS ) / 2 ); /* prototype fill on each side of xsubst to fill MDCT Frame */
    2628         132 :     move16();
    2629         132 :     ola_len = NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS - ( 2 * FRAME_SIZE_NS - L_PROT_NS ) / 2 ); /* remaining lengt of LA_ZEROS to overlap add decoded with xsubst */
    2630         132 :     move16();
    2631             : 
    2632         132 :     xf_len = 26;
    2633         132 :     move16();
    2634         132 :     tbl_delta = 10082; /* Q12 */
    2635         132 :     move16();
    2636         132 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2637             :     {
    2638         103 :         w_hamm = w_hamm_sana48k_2_fx;
    2639         103 :         hamm_len2 = L_PROT_HAMM_LEN2_48k;
    2640         103 :         move16();
    2641         103 :         xf_len = 78;
    2642         103 :         move16();
    2643         103 :         tbl_delta = 3361; /* Q12 */
    2644         103 :         move16();
    2645             :     }
    2646          29 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    2647             :     {
    2648          29 :         w_hamm = w_hamm_sana32k_2_fx;
    2649          29 :         hamm_len2 = L_PROT_HAMM_LEN2_32k;
    2650          29 :         move16();
    2651          29 :         xf_len = 52;
    2652          29 :         move16();
    2653          29 :         tbl_delta = 5041; /* Q12 */
    2654          29 :         move16();
    2655             :     }
    2656             :     ELSE
    2657             :     {
    2658           0 :         w_hamm = w_hamm_sana16k_2_fx;
    2659           0 :         hamm_len2 = L_PROT_HAMM_LEN2_16k;
    2660           0 :         move16();
    2661             :     }
    2662             : 
    2663         132 :     test();
    2664         132 :     test();
    2665         132 :     IF( element_mode != EVS_MONO && *num_p > 0 && GT_16( plocs[0], 3 ) )
    2666             :     {
    2667             :         /* Perform inverse windowing of hammrect */
    2668          26 :         pX_start = X;
    2669          26 :         pX_end = X + sub( Lprot, 1 );
    2670        6074 :         FOR( i = 0; i < hamm_len2; i++ )
    2671             :         {
    2672        6048 :             tmp_e = 0;
    2673        6048 :             tmp = BASOP_Util_Divide1616_Scale( ONE_IN_Q14, *w_hamm, &tmp_e );
    2674        6048 :             tmp = shl( tmp, 1 );
    2675        6048 :             tmp = shr( tmp, sub( 4, tmp_e ) );                  // Q11
    2676        6048 :             *pX_start = shl_sat( mult_r( *pX_start, tmp ), 4 ); // Qin
    2677        6048 :             move16();
    2678        6048 :             *pX_end = shl_sat( mult_r( *pX_end, tmp ), 4 ); // Qin
    2679        6048 :             move16();
    2680        6048 :             pX_start++;
    2681        6048 :             pX_end--;
    2682        6048 :             w_hamm++;
    2683             :         }
    2684             :     }
    2685             : 
    2686             :     /* extract reconstructed frame with aldo window */
    2687         132 :     timesh = sub( NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS ), shr( sub( shl( output_frame, 1 ), Lprot ), 1 ) );
    2688             : 
    2689         132 :     set16_fx( xsubst_, 0, add( sub( shl( output_frame, 1 ), Lprot ), timesh ) );
    2690         132 :     Copy( X, xsubst_ + add( sub( shl( output_frame, 1 ), Lprot ), timesh ), sub( Lprot, timesh ) );
    2691             : 
    2692             :     /* Copy and OLA look ahead zero part of MDCT window from decoded signal  */
    2693         132 :     IF( element_mode != EVS_MONO )
    2694             :     {
    2695         132 :         Copy( old_dec, xsubst_ + NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS ), copy_len ); /* also need to scale to Q0 ?? */
    2696         132 :         pOld = old_dec + copy_len;
    2697         132 :         pNew = xsubst_ + add( copy_len, NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS ) );
    2698         132 :         tmp = div_s( 1, shl( ola_len, 1 ) );                    // Q15
    2699         132 :         tmp = round_fx( L_shl( L_mult( tmp, EVS_PI_FX ), 2 ) ); // Q15
    2700         132 :         sinq_fx( tmp, 0, ola_len, xfwin );
    2701         132 :         v_mult16_fixed( xfwin, xfwin, xfwin, ola_len ); /* xfwin = sin^2 of 0..pi/4 */
    2702         132 :         pOldW = xfwin + sub( ola_len, 1 );
    2703         132 :         pNewW = xfwin;
    2704        9674 :         FOR( i = 0; i < ola_len; i++ )
    2705             :         {
    2706        9542 :             *pNew = add( mult_r( *pOld, *pOldW ), mult_r( *pNew, *pNewW ) );
    2707        9542 :             move16();
    2708        9542 :             pOld += 1;
    2709        9542 :             pNew += 1;
    2710        9542 :             pOldW -= 1;
    2711        9542 :             pNewW += 1;
    2712             :         }
    2713             :     }
    2714             :     ELSE
    2715             :     {
    2716             :         /* Smoothen onset of ECU frame */
    2717           0 :         p_ecu = xsubst_ + add( sub( shl( output_frame, 1 ), Lprot ), timesh );
    2718           0 :         FOR( i = 0; i < xf_len; ( i++, p_ecu++ ) )
    2719             :         {
    2720           0 :             idx = extract_l( L_shr( L_mult0( i, tbl_delta ), 12 ) ); // Q0
    2721           0 :             g = sincos_t_fx[idx];                                    // Q15
    2722           0 :             move16();
    2723           0 :             g = mult( g, g ); // Q15
    2724           0 :             *p_ecu = mult( g, ( *p_ecu ) );
    2725           0 :             move16();
    2726           0 :             p_ecu++;
    2727             :         }
    2728             :     }
    2729             : 
    2730             :     /* Apply TDA and windowing to ECU frame */
    2731         132 :     Qin = 0;
    2732         132 :     move16();
    2733         132 :     wtda_fx( xsubst_ + output_frame, &Qin, ecu_rec, NULL, 0, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */
    2734             :              output_frame );
    2735             : 
    2736         132 :     return;
    2737             : }
    2738             : 
    2739           0 : static void rec_wtda_fx(
    2740             :     Word16 *X,                 /* i  : FFT spectrum                          */
    2741             :     Word32 *ecu_rec,           /* o  : Reconstructed frame in tda domain     */
    2742             :     const Word16 output_frame, /* i  : Frame length                          */
    2743             :     const Word16 Lprot,        /* i  : Prototype frame length                */
    2744             :     const Word32 fs )
    2745             : {
    2746             :     Word16 l, Lprot2, timesh;
    2747             :     Word16 rec_buf[3 * L_FRAME48k];
    2748             :     Word16 *xsubst_, *out_ptr;
    2749             :     Word16 Qin;
    2750             :     Word16 xf_len;
    2751             :     Word16 i, idx;
    2752             :     Word16 *p_ecu;
    2753             :     Word16 g;
    2754             :     Word16 tbl_delta;
    2755             : 
    2756             :     // PMTE()
    2757           0 :     xsubst_ = rec_buf + output_frame;
    2758           0 :     Lprot2 = shr( Lprot, 1 );
    2759             : 
    2760             :     /* Initialize to WB constants */
    2761           0 :     xf_len = 26;
    2762           0 :     move16();
    2763           0 :     tbl_delta = 10082; /* Q12 */
    2764           0 :     move16();
    2765           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2766             :     {
    2767           0 :         xf_len = 78;
    2768           0 :         move16();
    2769           0 :         tbl_delta = 3361; /* Q12 */
    2770           0 :         move16();
    2771             :     }
    2772           0 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    2773             :     {
    2774           0 :         xf_len = 52;
    2775           0 :         move16();
    2776           0 :         tbl_delta = 5041; /* Q12 */
    2777           0 :         move16();
    2778             :     }
    2779             : 
    2780             :     /* extract reconstructed frame with aldo window */
    2781           0 :     l = sub( output_frame, Lprot2 );
    2782           0 :     set16_fx( xsubst_, 0, l );
    2783           0 :     Copy( X, xsubst_ + l, Lprot );
    2784           0 :     set16_fx( xsubst_ + add( output_frame, Lprot2 ), 0, l );
    2785             : 
    2786             :     /* Smoothen onset of ECU frame */
    2787           0 :     p_ecu = xsubst_ + sub( output_frame, Lprot2 );
    2788           0 :     FOR( i = 0; i < xf_len; i++ )
    2789             :     {
    2790           0 :         idx = extract_l( L_shr( L_mult0( i, tbl_delta ), 12 ) );
    2791           0 :         g = sincos_t_fx[idx];
    2792           0 :         move16();
    2793           0 :         g = mult( g, g );
    2794           0 :         *p_ecu = mult( g, ( *p_ecu ) );
    2795           0 :         move16();
    2796           0 :         p_ecu++;
    2797             :     }
    2798             : 
    2799           0 :     timesh = NS2SA_FX2( fs, 10000000L - PH_ECU_ALDO_OLP2_NS );
    2800           0 :     move16();
    2801             : 
    2802           0 :     set16_fx( rec_buf, 0, output_frame );
    2803           0 :     Qin = 0;
    2804           0 :     move16();
    2805           0 :     out_ptr = rec_buf + sub( shl( output_frame, 1 ), timesh );
    2806           0 :     wtda_fx( out_ptr, &Qin, ecu_rec, NULL, 0, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */
    2807             :              output_frame );
    2808           0 :     return;
    2809             : }
    2810             : 
    2811             : /*--------------------------------------------------------------------------
    2812             :  *  rec_frame_fx()
    2813             :  *
    2814             :  *  Frame reconstruction
    2815             :  *--------------------------------------------------------------------------*/
    2816             : 
    2817         132 : static void ivas_rec_frame_fx(
    2818             :     Word16 *X,                 /* i  : FFT spectrum */
    2819             :     Word32 *ecu_rec,           /* o  : Reconstructed frame in tda domain */
    2820             :     const Word16 output_frame, /* i  : Frame length */
    2821             :     const Word16 Q,
    2822             :     const Word16 *old_dec,     /* i  : end of last decoded for OLA before tda and itda */
    2823             :     const Word16 element_mode, /* i  : IVAS element mode                     */
    2824             :     const Word16 *num_p,       /* i  : Number of peaks                       */
    2825             :     const Word16 *plocs        /* i  : Peak locations                        */
    2826             : )
    2827             : {
    2828             :     const Word16 *pFftTbl;
    2829             :     Word16 Lprot, lprotLog2Minus1;
    2830             : 
    2831             :     /* Initialize to WB constants */
    2832         132 :     Lprot = 512;
    2833         132 :     move16();
    2834         132 :     lprotLog2Minus1 = 9 - 1;
    2835         132 :     move16();
    2836         132 :     pFftTbl = FFT_W256; /* Table for 512-point real input FFT */
    2837         132 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2838             :     {
    2839         103 :         Lprot = L_PROT48k; /* 1536 = (2*output_frame)*1024/1280 */
    2840         103 :         move16();
    2841             :     }
    2842          29 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    2843             :     {
    2844          29 :         Lprot = L_PROT32k; /* 1024 */
    2845          29 :         move16();
    2846          29 :         lprotLog2Minus1 = 10 - 1;
    2847          29 :         move16();
    2848          29 :         pFftTbl = FFT_W512; /* Table for 1024-point real input FFT */
    2849             :     }
    2850             : 
    2851             :     /* extend spectrum and IDFT */
    2852         132 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2853             :     {
    2854         103 :         ifft3_fx( X, X, Lprot );
    2855             :     }
    2856             :     ELSE
    2857             :     {
    2858          29 :         r_fft_fx_lc( pFftTbl, Lprot, shr( Lprot, 1 ), lprotLog2Minus1, X, X, 0 ); /* Inverse FFT */
    2859             :     }
    2860         132 :     Scale_sig( X, Lprot, -Q );
    2861             : 
    2862         132 :     ivas_rec_wtda_fx( X, ecu_rec, output_frame, Lprot, old_dec, element_mode, num_p, plocs );
    2863             : 
    2864         132 :     return;
    2865             : }
    2866             : 
    2867           0 : static void rec_frame_fx(
    2868             :     Word16 *X,                 /* i  : FFT spectrum */
    2869             :     Word32 *ecu_rec,           /* o  : Reconstructed frame in tda domain */
    2870             :     const Word16 output_frame, /* i  : Frame length */
    2871             :     const Word16 Q )
    2872             : {
    2873             :     const Word16 *pFftTbl;
    2874             :     Word16 Lprot, lprotLog2Minus1;
    2875             :     Word32 fs;
    2876             : 
    2877           0 :     fs = L_mult0( output_frame, 50 );
    2878             : 
    2879             :     /* Initialize to WB constants */
    2880           0 :     Lprot = 512;
    2881           0 :     move16();
    2882           0 :     lprotLog2Minus1 = 9 - 1;
    2883           0 :     move16();
    2884           0 :     pFftTbl = FFT_W256; /* Table for 512-point real input FFT */
    2885           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2886             :     {
    2887           0 :         Lprot = L_PROT48k; /* 1536 = (2*output_frame)*1024/1280 */
    2888           0 :         move16();
    2889             :     }
    2890           0 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    2891             :     {
    2892           0 :         Lprot = L_PROT32k; /* 1024 */
    2893           0 :         move16();
    2894           0 :         lprotLog2Minus1 = 10 - 1;
    2895           0 :         move16();
    2896           0 :         pFftTbl = FFT_W512; /* Table for 1024-point real input FFT */
    2897             :     }
    2898             : 
    2899             :     /* extend spectrum and IDFT */
    2900           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    2901             :     {
    2902           0 :         ifft3_fx( X, X, Lprot );
    2903             :     }
    2904             :     ELSE
    2905             :     {
    2906           0 :         r_fft_fx_lc( pFftTbl, Lprot, shr( Lprot, 1 ), lprotLog2Minus1, X, X, 0 ); /* Inverse FFT */
    2907             :     }
    2908           0 :     Scale_sig( X, Lprot, -Q );
    2909             : 
    2910           0 :     rec_wtda_fx( X, ecu_rec, output_frame, Lprot, fs );
    2911             : 
    2912           0 :     return;
    2913             : }
    2914             : 
    2915           0 : static Word32 mult_32_32_q( const Word32 a, const Word32 b, const Word16 q )
    2916             : {
    2917             :     Word32 hi;
    2918             :     UWord32 lo;
    2919           0 :     Mpy_32_32_ss( a, b, &hi, &lo );
    2920             : 
    2921           0 :     return L_or( L_shl_sat( hi, sub( 32 - 1, q ) ), L_lshr( (Word32) lo, add( q, 1 ) ) );
    2922             : }
    2923             : 
    2924         133 : static void fir_dwn_fx(
    2925             :     const Word16 x[],       /* i  : input vector Q(x_Q)                       */
    2926             :     const Word16 h[],       /* i  : impulse response of the FIR filter Q(h_Q)  */
    2927             :     const Word16 h_Q,       /* H's Q                                          */
    2928             :     Word16 y[],             /* o  : output vector (result of filtering) Q~    */
    2929             :     const Word16 L,         /* i  : input vector size                         */
    2930             :     const Word16 K,         /* i  : order of the FIR filter (K+1 coefs.)      */
    2931             :     const Word16 decimation /* i  : decimation                                */
    2932             : )
    2933             : {
    2934             :     Word32 s;
    2935             :     Word16 i, j;
    2936             :     const Word16 *ptr_h, *ptr_x;
    2937             :     Word16 *ptr_y;
    2938             :     Word16 Kdiv2;
    2939             :     Word16 centering;
    2940             :     Word16 tmp;
    2941             : 
    2942         133 :     centering = sub( 16, h_Q );
    2943         133 :     Kdiv2 = shr( K, 1 );
    2944             : 
    2945         133 :     ptr_y = y;
    2946             :     /* do the filtering */
    2947         798 :     FOR( i = Kdiv2; i < K; i += decimation )
    2948             :     {
    2949         665 :         s = L_deposit_l( 0 );
    2950         665 :         ptr_h = h + 1;
    2951         665 :         ptr_x = x + sub( i, 1 );
    2952             : 
    2953       26495 :         FOR( j = 1; j <= i; j++ )
    2954             :         {
    2955       25830 :             s = L_mac0_sat( s, *ptr_h++, *ptr_x-- ); // Q(h_Q) + Q(x_q)
    2956             :         }
    2957             : 
    2958         665 :         *ptr_y++ = extract_h( L_shl_sat( s, centering ) );
    2959         665 :         move16();
    2960             :     }
    2961       41363 :     FOR( i = K; i < L; i += decimation )
    2962             :     {
    2963       41230 :         s = L_deposit_l( 0 );
    2964       41230 :         ptr_h = h + 1;
    2965       41230 :         ptr_x = x + sub( i, 1 );
    2966       41230 :         move16();
    2967             : 
    2968     2329030 :         FOR( j = 1; j <= K; j++ )
    2969             :         {
    2970     2287800 :             s = L_mac0_sat( s, *ptr_h++, *ptr_x-- ); // Q(h_Q) + Q(x_q)
    2971             :         }
    2972             : 
    2973       41230 :         *ptr_y++ = extract_h( L_shl_sat( s, centering ) );
    2974       41230 :         move16();
    2975             :     }
    2976         133 :     tmp = add( L, Kdiv2 );
    2977         798 :     FOR( i = i; i < tmp; i += decimation )
    2978             :     {
    2979         665 :         s = L_deposit_l( 0 );
    2980         665 :         ptr_h = h + add( sub( i, L ), 1 );
    2981         665 :         move16();
    2982         665 :         ptr_x = x + sub( L, 1 );
    2983         665 :         move16();
    2984             : 
    2985       30185 :         FOR( j = i - L + 1; j <= K; j++ )
    2986             :         {
    2987       29520 :             s = L_mac0_sat( s, *ptr_h++, *ptr_x-- );
    2988             :         }
    2989             : 
    2990         665 :         *ptr_y++ = extract_h( L_shl_sat( s, centering ) );
    2991         665 :         move16();
    2992             :     }
    2993             : 
    2994         133 :     return;
    2995             : }
    2996             : 
    2997             : /*--------------------------------------------------------------------------
    2998             :  *  fec_ecu_pitch()
    2999             :  *
    3000             :  *  Pitch/correlation analysis and adaptive analysis frame length calculation
    3001             :  *--------------------------------------------------------------------------*/
    3002             : 
    3003         133 : static void fec_ecu_pitch_ivas_fx(
    3004             :     const Word16 *prevsynth_fx, /*Q15 16 */
    3005             :     Word16 *prevsynth_LP_fx,    /* Q15 16 */
    3006             :     const Word16 L,
    3007             :     Word16 *N,
    3008             :     Word16 *min_corr_fx, /* Q15 16 */
    3009             :     Word16 *decimatefator,
    3010             :     const Word16 HqVoicing )
    3011             : {
    3012             : 
    3013             :     Word16 i, filt_size;
    3014             :     Word16 QAsr, Ryy, cb_start, tmpQLP;
    3015             :     Word32 Ryytmp;
    3016             :     Word32 accA, accB, accBisqrt, accC, accCisqrt;
    3017             :     Word16 delay_ind, k;
    3018             :     const Word16 *Asr_LP_fx;
    3019             :     Word16 *ptr_LP, *ptr_LP2, *ptr_LP3, *ptr_LP4;
    3020             :     Word16 cb_end;
    3021             :     Word16 Lmul2, Lon20mul6, Lon20mul28, Lon20mul33, Lon20mul34;
    3022             : 
    3023             : 
    3024         133 :     SWITCH( L )
    3025             :     {
    3026         103 :         case L_FRAME48k:
    3027         103 :             *decimatefator = 6;
    3028         103 :             move16();
    3029         103 :             filt_size = 60;
    3030         103 :             move16();
    3031         103 :             Asr_LP_fx = Asr_LP48_fx;
    3032         103 :             QAsr = 17;
    3033         103 :             move16();
    3034         103 :             Lon20mul6 = 48;
    3035         103 :             move16();
    3036         103 :             Lon20mul28 = 224;
    3037         103 :             move16();
    3038         103 :             Lon20mul33 = 264;
    3039         103 :             move16();
    3040         103 :             Lon20mul34 = 272;
    3041         103 :             move16();
    3042         103 :             Lmul2 = 1920;
    3043         103 :             move16();
    3044         103 :             BREAK;
    3045             : 
    3046          30 :         case L_FRAME32k:
    3047          30 :             *decimatefator = 4;
    3048          30 :             move16();
    3049          30 :             filt_size = 40;
    3050          30 :             move16();
    3051          30 :             Asr_LP_fx = Asr_LP32_fx;
    3052          30 :             QAsr = 15;
    3053          30 :             move16();
    3054          30 :             Lon20mul6 = 48;
    3055          30 :             move16();
    3056          30 :             Lon20mul28 = 224;
    3057          30 :             move16();
    3058          30 :             Lon20mul33 = 264;
    3059          30 :             move16();
    3060          30 :             Lon20mul34 = 272;
    3061          30 :             move16();
    3062          30 :             Lmul2 = 1280;
    3063          30 :             move16();
    3064          30 :             BREAK;
    3065             : 
    3066           0 :         case L_FRAME16k:
    3067           0 :             *decimatefator = 2;
    3068           0 :             move16();
    3069           0 :             filt_size = 20;
    3070           0 :             move16();
    3071           0 :             Asr_LP_fx = Asr_LP16_fx;
    3072           0 :             QAsr = 15;
    3073           0 :             move16();
    3074           0 :             Lon20mul6 = 48;
    3075           0 :             move16();
    3076           0 :             Lon20mul28 = 224;
    3077           0 :             move16();
    3078           0 :             Lon20mul33 = 264;
    3079           0 :             move16();
    3080           0 :             Lon20mul34 = 272;
    3081           0 :             move16();
    3082           0 :             Lmul2 = 640;
    3083           0 :             move16();
    3084           0 :             BREAK;
    3085             : 
    3086           0 :         default:
    3087           0 :             *decimatefator = 2;
    3088           0 :             move16();
    3089           0 :             filt_size = 40;
    3090           0 :             move16();
    3091           0 :             Asr_LP_fx = Asr_LP16_fx;
    3092           0 :             QAsr = 15;
    3093           0 :             move16();
    3094           0 :             Lon20mul6 = 48;
    3095           0 :             move16();
    3096           0 :             Lon20mul28 = 224;
    3097           0 :             move16();
    3098           0 :             Lon20mul33 = 264;
    3099           0 :             move16();
    3100           0 :             Lon20mul34 = 272;
    3101           0 :             move16();
    3102           0 :             Lmul2 = 320;
    3103           0 :             move16();
    3104           0 :             BREAK;
    3105             :     }
    3106             : 
    3107             : 
    3108             :     /* Resampling to work at 8Khz */
    3109         133 :     fir_dwn_fx( prevsynth_fx, Asr_LP_fx, QAsr, prevsynth_LP_fx, Lmul2, filt_size, *decimatefator ); /* resampling without delay */
    3110             : 
    3111             : 
    3112         133 :     tmpQLP = Find_Max_Norm16( prevsynth_LP_fx, 320 );
    3113         133 :     Scale_sig( prevsynth_LP_fx, 320, sub( tmpQLP, 3 ) ); /* to avoid over scaling */
    3114             : 
    3115             : 
    3116             :     /* Correlation analysis */
    3117         133 :     *min_corr_fx = 0;
    3118         133 :     move16();
    3119         133 :     accC = L_deposit_l( 0 );
    3120             : 
    3121         133 :     ptr_LP = prevsynth_LP_fx + Lon20mul34;
    3122        6517 :     FOR( k = 0; k < Lon20mul6; k++ )
    3123             :     {
    3124        6384 :         accC = L_mac0( accC, *ptr_LP, *ptr_LP );
    3125        6384 :         ptr_LP++;
    3126             :     }
    3127             : 
    3128             : 
    3129         133 :     IF( EQ_16( HqVoicing, 1 ) )
    3130             :     {
    3131           0 :         cb_start = 0;
    3132           0 :         move16();
    3133           0 :         cb_end = Lon20mul33;
    3134           0 :         move16();
    3135             :     }
    3136             :     ELSE
    3137             :     {
    3138         133 :         cb_start = 0;
    3139         133 :         move16();
    3140         133 :         cb_end = Lon20mul28;
    3141         133 :         move16();
    3142             :     }
    3143             : 
    3144         133 :     accB = 0;
    3145         133 :     move16();
    3146         133 :     delay_ind = cb_start;
    3147         133 :     move16();
    3148             : 
    3149       22002 :     FOR( i = cb_start; i < cb_end; i++ ) /* cb_end = 35 let 6 ms min of loop size */
    3150             :     {
    3151       21911 :         accA = 0;
    3152       21911 :         move16();
    3153       21911 :         IF( EQ_16( i, cb_start ) )
    3154             :         {
    3155         133 :             accB = 0;
    3156         133 :             move16();
    3157         133 :             ptr_LP = prevsynth_LP_fx;
    3158         133 :             ptr_LP2 = prevsynth_LP_fx + Lon20mul34;
    3159        6517 :             FOR( k = 0; k < Lon20mul6; k++ )
    3160             :             {
    3161        6384 :                 accA = L_mac0( accA, *ptr_LP, *ptr_LP2 );
    3162        6384 :                 accB = L_mac0( accB, *ptr_LP, *ptr_LP );
    3163        6384 :                 ptr_LP++;
    3164        6384 :                 ptr_LP2++;
    3165             :             }
    3166             :         }
    3167             :         ELSE
    3168             :         {
    3169             : 
    3170             : 
    3171       21778 :             accB = L_mac0( L_msu0( accB, prevsynth_LP_fx[i - 1], prevsynth_LP_fx[i - 1] ), prevsynth_LP_fx[i + Lon20mul6 - 1], prevsynth_LP_fx[i + Lon20mul6 - 1] ); /* tmpQLP-5  */
    3172             : 
    3173             : 
    3174       21778 :             ptr_LP3 = prevsynth_LP_fx + i;
    3175       21778 :             ptr_LP4 = prevsynth_LP_fx + Lon20mul34;
    3176             : 
    3177     1067122 :             FOR( k = 0; k < Lon20mul6; k++ )
    3178             :             {
    3179     1045344 :                 accA = L_mac0( accA, *ptr_LP3, *ptr_LP4 ); /*2* tmpQLP-5  */
    3180     1045344 :                 ptr_LP3++;
    3181     1045344 :                 ptr_LP4++;
    3182             :             }
    3183             :         }
    3184             : 
    3185             :         /*accB*/
    3186             :         /*accC*/
    3187             :         /*accA Q 6-2*tmpQLP*/
    3188             : 
    3189       21911 :         accBisqrt = Isqrt( accB ); /* Q31 - 3 +  tmpQLP */
    3190       21911 :         accCisqrt = Isqrt( accC ); /* Q31 - 3 +  tmpQLP*/
    3191             : 
    3192             : 
    3193       21911 :         Ryytmp = L_shl_sat( Mult_32_32( accA, accCisqrt ), Q15 );            /*Q   6 -2*tmpQLP +  Q31 - 3 +  tmpQLP -16 -3 +tmpQLP      = Q15*/
    3194       21911 :         Ryy = extract_h( L_shl_sat( Mult_32_32( Ryytmp, accBisqrt ), 16 ) ); /*Q15 + Q31 - 3 +  tmpQLP -15 + 3- tmpQLP = Q31*/
    3195             : 
    3196       21911 :         IF( GT_16( Ryy, *min_corr_fx ) )
    3197             :         {
    3198         855 :             *min_corr_fx = Ryy;
    3199         855 :             move16();
    3200         855 :             delay_ind = i;
    3201         855 :             move16();
    3202             :         }
    3203             : 
    3204       21911 :         test();
    3205       21911 :         IF( HqVoicing == 0 && GT_16( *min_corr_fx, 31130 ) )
    3206             :         {
    3207          42 :             BREAK;
    3208             :         }
    3209             :     }
    3210             : 
    3211         133 :     *N = sub( Lon20mul34, delay_ind );
    3212         133 :     move16();
    3213             : 
    3214         133 :     Scale_sig( prevsynth_LP_fx, 320, negate( sub( tmpQLP, 3 ) ) );
    3215         133 :     return;
    3216             : }
    3217             : 
    3218           0 : static void fec_ecu_pitch_fx(
    3219             :     const Word16 *prevsynth_fx, /*Q15 16 */
    3220             :     Word16 *prevsynth_LP_fx,    /* Q15 16 */
    3221             :     const Word16 L,
    3222             :     Word16 *N,
    3223             :     Word16 *min_corr_fx, /* Q15 16 */
    3224             :     Word16 *decimatefator,
    3225             :     const Word16 HqVoicing )
    3226             : {
    3227             : 
    3228             :     Word16 i, filt_size;
    3229             :     Word16 QAsr, Ryy, cb_start, tmpQLP;
    3230             :     Word32 Ryytmp;
    3231             :     Word32 accA, accB, accBisqrt, accC, accCisqrt;
    3232             :     Word16 delay_ind, k;
    3233             :     const Word16 *Asr_LP_fx;
    3234             :     Word16 *ptr_LP, *ptr_LP2, *ptr_LP3, *ptr_LP4;
    3235             :     Word16 cb_end;
    3236             :     Word16 Lmul2, Lon20mul6, Lon20mul28, Lon20mul33, Lon20mul34;
    3237             : 
    3238             : 
    3239           0 :     SWITCH( L )
    3240             :     {
    3241           0 :         case L_FRAME48k:
    3242           0 :             *decimatefator = 6;
    3243           0 :             move16();
    3244           0 :             filt_size = 60;
    3245           0 :             move16();
    3246           0 :             Asr_LP_fx = Asr_LP48_fx;
    3247           0 :             QAsr = 17;
    3248           0 :             move16();
    3249           0 :             Lon20mul6 = 48;
    3250           0 :             move16();
    3251           0 :             Lon20mul28 = 224;
    3252           0 :             move16();
    3253           0 :             Lon20mul33 = 264;
    3254           0 :             move16();
    3255           0 :             Lon20mul34 = 272;
    3256           0 :             move16();
    3257           0 :             Lmul2 = 1920;
    3258           0 :             move16();
    3259           0 :             BREAK;
    3260             : 
    3261           0 :         case L_FRAME32k:
    3262           0 :             *decimatefator = 4;
    3263           0 :             move16();
    3264           0 :             filt_size = 40;
    3265           0 :             move16();
    3266           0 :             Asr_LP_fx = Asr_LP32_fx;
    3267           0 :             QAsr = 15;
    3268           0 :             move16();
    3269           0 :             Lon20mul6 = 48;
    3270           0 :             move16();
    3271           0 :             Lon20mul28 = 224;
    3272           0 :             move16();
    3273           0 :             Lon20mul33 = 264;
    3274           0 :             move16();
    3275           0 :             Lon20mul34 = 272;
    3276           0 :             move16();
    3277           0 :             Lmul2 = 1280;
    3278           0 :             move16();
    3279           0 :             BREAK;
    3280             : 
    3281           0 :         case L_FRAME16k:
    3282           0 :             *decimatefator = 2;
    3283           0 :             move16();
    3284           0 :             filt_size = 20;
    3285           0 :             move16();
    3286           0 :             Asr_LP_fx = Asr_LP16_fx;
    3287           0 :             QAsr = 15;
    3288           0 :             move16();
    3289           0 :             Lon20mul6 = 48;
    3290           0 :             move16();
    3291           0 :             Lon20mul28 = 224;
    3292           0 :             move16();
    3293           0 :             Lon20mul33 = 264;
    3294           0 :             move16();
    3295           0 :             Lon20mul34 = 272;
    3296           0 :             move16();
    3297           0 :             Lmul2 = 640;
    3298           0 :             move16();
    3299           0 :             BREAK;
    3300             : 
    3301           0 :         default:
    3302           0 :             *decimatefator = 2;
    3303           0 :             move16();
    3304           0 :             filt_size = 40;
    3305           0 :             move16();
    3306           0 :             Asr_LP_fx = Asr_LP16_fx;
    3307           0 :             QAsr = 15;
    3308           0 :             move16();
    3309           0 :             Lon20mul6 = 48;
    3310           0 :             move16();
    3311           0 :             Lon20mul28 = 224;
    3312           0 :             move16();
    3313           0 :             Lon20mul33 = 264;
    3314           0 :             move16();
    3315           0 :             Lon20mul34 = 272;
    3316           0 :             move16();
    3317           0 :             Lmul2 = 320;
    3318           0 :             move16();
    3319           0 :             BREAK;
    3320             :     }
    3321             : 
    3322             : 
    3323             :     /* Resampling to work at 8Khz */
    3324           0 :     fir_dwn_fx( prevsynth_fx, Asr_LP_fx, QAsr, prevsynth_LP_fx, Lmul2, filt_size, *decimatefator ); /* resampling without delay */
    3325             : 
    3326             : 
    3327           0 :     tmpQLP = Find_Max_Norm16( prevsynth_LP_fx, 320 );
    3328           0 :     Scale_sig( prevsynth_LP_fx, 320, sub( tmpQLP, 3 ) ); /* to avoid over scaling */
    3329             : 
    3330             : 
    3331             :     /* Correlation analysis */
    3332           0 :     *min_corr_fx = 0;
    3333           0 :     move16();
    3334           0 :     accC = L_deposit_l( 0 );
    3335             : 
    3336           0 :     ptr_LP = prevsynth_LP_fx + Lon20mul34;
    3337           0 :     FOR( k = 0; k < Lon20mul6; k++ )
    3338             :     {
    3339           0 :         accC = L_mac0( accC, *ptr_LP, *ptr_LP );
    3340           0 :         ptr_LP++;
    3341             :     }
    3342             : 
    3343             : 
    3344           0 :     IF( EQ_16( HqVoicing, 1 ) )
    3345             :     {
    3346           0 :         cb_start = 0;
    3347           0 :         move16();
    3348           0 :         cb_end = Lon20mul33;
    3349           0 :         move16();
    3350             :     }
    3351             :     ELSE
    3352             :     {
    3353           0 :         cb_start = 0;
    3354           0 :         move16();
    3355           0 :         cb_end = Lon20mul28;
    3356           0 :         move16();
    3357             :     }
    3358             : 
    3359           0 :     accB = 0;
    3360           0 :     move16();
    3361           0 :     delay_ind = cb_start;
    3362           0 :     move16();
    3363             : 
    3364           0 :     FOR( i = cb_start; i < cb_end; i++ ) /* cb_end = 35 let 6 ms min of loop size */
    3365             :     {
    3366           0 :         accA = 0;
    3367           0 :         move16();
    3368           0 :         IF( EQ_16( i, cb_start ) )
    3369             :         {
    3370           0 :             accB = 0;
    3371           0 :             move16();
    3372           0 :             ptr_LP = prevsynth_LP_fx;
    3373           0 :             ptr_LP2 = prevsynth_LP_fx + Lon20mul34;
    3374           0 :             FOR( k = 0; k < Lon20mul6; k++ )
    3375             :             {
    3376           0 :                 accA = L_mac0( accA, *ptr_LP, *ptr_LP2 );
    3377           0 :                 accB = L_mac0( accB, *ptr_LP, *ptr_LP );
    3378           0 :                 ptr_LP++;
    3379           0 :                 ptr_LP2++;
    3380             :             }
    3381             :         }
    3382             :         ELSE
    3383             :         {
    3384             : 
    3385             : 
    3386           0 :             accB = L_mac0( L_msu0( accB, prevsynth_LP_fx[i - 1], prevsynth_LP_fx[i - 1] ), prevsynth_LP_fx[i + Lon20mul6 - 1], prevsynth_LP_fx[i + Lon20mul6 - 1] ); /* tmpQLP-5  */
    3387             : 
    3388             : 
    3389           0 :             ptr_LP3 = prevsynth_LP_fx + i;
    3390           0 :             ptr_LP4 = prevsynth_LP_fx + Lon20mul34;
    3391             : 
    3392           0 :             FOR( k = 0; k < Lon20mul6; k++ )
    3393             :             {
    3394           0 :                 accA = L_mac0( accA, *ptr_LP3, *ptr_LP4 ); /*2* tmpQLP-5  */
    3395           0 :                 ptr_LP3++;
    3396           0 :                 ptr_LP4++;
    3397             :             }
    3398             :         }
    3399             : 
    3400             :         /*accB*/
    3401             :         /*accC*/
    3402             :         /*accA Q 6-2*tmpQLP*/
    3403             : 
    3404           0 :         accBisqrt = Isqrt( accB ); /* Q31 - 3 +  tmpQLP */
    3405           0 :         accCisqrt = Isqrt( accC ); /* Q31 - 3 +  tmpQLP*/
    3406             : 
    3407             : 
    3408           0 :         Ryytmp = mult_32_32_q( accA, accCisqrt, sub( 16 + 3, tmpQLP ) );   /*Q   6 -2*tmpQLP +  Q31 - 3 +  tmpQLP -16 -3 +tmpQLP      = Q15*/
    3409           0 :         Ryytmp = mult_32_32_q( Ryytmp, accBisqrt, add( 15 - 3, tmpQLP ) ); /*Q15 + Q31 - 3 +  tmpQLP -15 + 3- tmpQLP = Q31*/
    3410           0 :         Ryy = extract_h( Ryytmp );                                         /* Q15 */
    3411             : 
    3412             : 
    3413           0 :         IF( GT_16( Ryy, *min_corr_fx ) )
    3414             :         {
    3415           0 :             *min_corr_fx = Ryy;
    3416           0 :             move16();
    3417           0 :             delay_ind = i;
    3418           0 :             move16();
    3419             :         }
    3420             : 
    3421           0 :         test();
    3422           0 :         IF( HqVoicing == 0 && GT_16( *min_corr_fx, 31130 ) )
    3423             :         {
    3424           0 :             BREAK;
    3425             :         }
    3426             :     }
    3427             : 
    3428           0 :     *N = sub( Lon20mul34, delay_ind );
    3429           0 :     move16();
    3430             : 
    3431           0 :     Scale_sig( prevsynth_LP_fx, 320, negate( sub( tmpQLP, 3 ) ) );
    3432           0 :     return;
    3433             : }
    3434             : 
    3435           8 : static void sin_cos_est_fx( Word32 phi, Word16 *cosfreq, Word16 *sinfreq )
    3436             : {
    3437             : 
    3438             : 
    3439             :     /* i phi : normalized frequency beteween 0 and Pi (nyquist) in Q30 */
    3440             :     /* o  cosfreq & sinfreq : cos(phi) and sin (phi) in Q15 */
    3441             : 
    3442             :     Word16 i;
    3443             :     Word32 delta;
    3444             :     Word32 imin;
    3445             :     Word16 sinb, cosb;
    3446             :     Word32 sinv, cosv, tmp;
    3447             : 
    3448           8 :     i = 0;
    3449           8 :     move16();
    3450         540 :     FOR( imin = 0; imin < phi; imin += 4392264 )
    3451             :     {
    3452         532 :         i = add( i, 1 );
    3453             :     }
    3454             : 
    3455           8 :     delta = L_sub( phi, imin ); /*Q30 */
    3456           8 :     sinb = sincos_t_rad3_fx[i];
    3457           8 :     move16(); /*Q15 */
    3458           8 :     cosb = sincos_t_rad3_fx[384 - i];
    3459           8 :     move16(); /*Q15 */
    3460             : 
    3461             :     /*sinv = sin(phi) = sinb*cos(delta)+cosb*sin(delta) */
    3462             :     /*cosv = cos(phi) = cosb*cos(delta)-sinb*sin(delta) */
    3463             :     /*sin(delta) is approximated by delta for very small delta; cos(delta) is approximated by 1 */
    3464           8 :     tmp = L_shl( Mult_32_16( delta, cosb ), 1 ); /*Q31 */
    3465           8 :     sinv = L_add( L_deposit_h( sinb ), tmp );
    3466           8 :     tmp = L_shl( Mult_32_16( delta, sinb ), 1 ); /*Q31 */
    3467           8 :     cosv = L_sub_sat( L_deposit_h( cosb ), tmp );
    3468           8 :     *sinfreq = round_fx_sat( sinv );
    3469           8 :     move16();
    3470           8 :     *cosfreq = round_fx_sat( cosv );
    3471           8 :     move16();
    3472             : 
    3473           8 :     return;
    3474             : }
    3475             : 
    3476          32 : static Word16 abs_iter_fx( Word16 re /*Qx*/, Word16 im /*Qx*/, Word16 N )
    3477             : {
    3478             :     Word16 A, tmp, L_tmp1, L_tmp2;
    3479             :     Word16 i, exp;
    3480             : 
    3481             :     /*const Word16 cor[10] =  { 23170,      20724,      20106,      19950,      19911,      19902,      19899,      19899,    19899,      19898}; */
    3482             : 
    3483          32 :     exp = norm_s( re );
    3484          32 :     exp = s_min( exp, norm_s( im ) );
    3485          32 :     exp = sub( exp, 2 );
    3486             : 
    3487          32 :     re = shl( re, exp );
    3488          32 :     im = shl( im, exp );
    3489             : 
    3490             : 
    3491          32 :     IF( im < 0 )
    3492             :     {
    3493          13 :         im = negate( im );
    3494             :     }
    3495             :     ELSE
    3496             :     {
    3497          19 :         re = negate( re );
    3498             :     }
    3499          32 :     tmp = re;
    3500          32 :     move16();
    3501          32 :     re = im;
    3502          32 :     move16();
    3503          32 :     im = tmp;
    3504          32 :     move16();
    3505             : 
    3506             : 
    3507         192 :     FOR( i = 0; i < N; i++ )
    3508             :     {
    3509         160 :         L_tmp1 = shr( im, i );
    3510         160 :         L_tmp2 = shr( re, i );
    3511         160 :         IF( im < 0 )
    3512             :         {
    3513          83 :             L_tmp1 = negate( L_tmp1 );
    3514             :         }
    3515             :         ELSE
    3516             :         {
    3517          77 :             L_tmp2 = negate( L_tmp2 );
    3518             :         }
    3519         160 :         re = add( re, L_tmp1 );
    3520         160 :         im = add( im, L_tmp2 );
    3521             :     }
    3522          32 :     i = s_min( sub( i, 1 ), 9 );
    3523          32 :     tmp = abs_s( re );
    3524             :     /*A = round_fx(L_shr(L_mult(tmp, cor[i]), exp)); //this can be ommited, if we don't need the exact abs value */
    3525          32 :     A = shr_sat( tmp, exp );
    3526          32 :     return A;
    3527             : }
    3528             : /*--------------------------------------------------------------------------
    3529             :  *  fec_ecu_dft()
    3530             :  *
    3531             :  *  DFT analysis on adaptive frame length. Analysis frame stretched to
    3532             :  *  next power of 2 using linear interpolation.
    3533             :  *--------------------------------------------------------------------------*/
    3534             : 
    3535           1 : static void ivas_fec_ecu_dft_fx(
    3536             :     const Word16 *prevsynth_LP, /*Qin */
    3537             :     const Word16 N,
    3538             :     Word16 *Tfr,        /*Qout */
    3539             :     Word16 *Tfi,        /*Qout */
    3540             :     Word32 *sum_Tf_abs, /*Qout; */
    3541             :     Word16 *Tf_abs,     /*Qout */
    3542             :     Word16 *Nfft,
    3543             :     Word16 *exp,              /*Qout = Qin+exp */
    3544             :     const Word16 element_mode /* i  : IVAS element mode */
    3545             : )
    3546             : {
    3547             :     Word32 L_tmp, Tmp, Tfr32[512], Tfi32[512], fac, *Pt1, *Pt2;
    3548             :     Word16 i, tmp, tmp_short, N_LP, target[2 * L_FRAME48k], Tfr16[FEC_FFT_MAX_SIZE], *pt1, *pt2, *pt3, Lon20;
    3549             :     Word16 tmp_loop;
    3550             :     Word16 alignment_point;
    3551             : 
    3552           1 :     Lon20 = 8;
    3553           1 :     move16();
    3554           1 :     IF( element_mode == EVS_MONO )
    3555             :     {
    3556           0 :         alignment_point = sub( shl( 160, 1 ), i_mult( 3, Lon20 ) );
    3557             :     }
    3558             :     ELSE
    3559             :     {
    3560           1 :         alignment_point = shl( 160, 1 );
    3561             :     }
    3562           1 :     tmp = sub( alignment_point, N );
    3563           1 :     Copy( &prevsynth_LP[tmp], target, N );
    3564             : 
    3565             :     /* DFT  */
    3566             : 
    3567           1 :     L_tmp = L_deposit_l( N );
    3568          10 :     FOR( tmp = 0; L_tmp <= 16384; tmp++ )
    3569             :     {
    3570           9 :         L_tmp = L_shl( L_tmp, 1 );
    3571             :     }
    3572           1 :     *Nfft = shl( 1, sub( 15, tmp ) );
    3573           1 :     move16();
    3574             : 
    3575           1 :     set32_fx( Tfr32, 0, *Nfft );
    3576           1 :     set32_fx( Tfi32, 0, *Nfft );
    3577           1 :     Tfr16[0] = target[0];
    3578           1 :     move16();
    3579           1 :     Tfr16[*Nfft - 1] = target[N - 1];
    3580           1 :     move16();
    3581             : 
    3582           1 :     IF( EQ_16( *Nfft, N ) )
    3583             :     {
    3584           0 :         Copy( &target[1], &Tfr16[1], sub( *Nfft, 2 ) );
    3585             :     }
    3586             :     ELSE
    3587             :     {
    3588             : 
    3589           1 :         tmp = div_s( sub( N, 1 ), sub( *Nfft, 1 ) );
    3590           1 :         Tmp = L_deposit_l( tmp );
    3591           1 :         fac = L_add( Tmp, 0 );
    3592           1 :         tmp_loop = sub( *Nfft, 1 );
    3593          63 :         FOR( i = 1; i < tmp_loop; i++ ) /* interpolation for FFT */
    3594             :         {
    3595          62 :             tmp_short = extract_l( L_shr( Tmp, 15 ) );
    3596          62 :             tmp = extract_l( L_msu( Tmp, tmp_short, 16384 ) );
    3597          62 :             L_tmp = L_mult( sub_sat( target[tmp_short + 1], target[tmp_short] ), tmp ); /*Qin+16 */
    3598          62 :             Tfr16[i] = add_sat( target[tmp_short], round_fx_sat( L_tmp ) );
    3599          62 :             move16(); /*Qin */
    3600          62 :             Tmp = L_add( Tmp, fac );
    3601             :         }
    3602             :     }
    3603             : 
    3604             : 
    3605             :     /*to avoid overflow in DoRTFTn_fx() */
    3606           1 :     tmp = Exp16Array( *Nfft, Tfr16 );
    3607           1 :     *exp = add( tmp, add( 2, norm_s( *Nfft ) ) );
    3608           1 :     move16();
    3609           1 :     Copy_Scale_sig_16_32_no_sat( Tfr16, Tfr32, *Nfft, *exp ); /*Qin+exp; */
    3610           1 :     *exp = s_min( *exp, 15 );
    3611             : 
    3612           1 :     DoRTFTn_fx( Tfr32, Tfi32, *Nfft );
    3613           1 :     N_LP = shr( *Nfft, 1 );
    3614             : 
    3615             : 
    3616           1 :     L_tmp = L_deposit_l( 0 );
    3617             : 
    3618           1 :     pt1 = Tfr;
    3619           1 :     pt2 = Tfi;
    3620           1 :     pt3 = Tf_abs;
    3621           1 :     Pt1 = Tfr32;
    3622           1 :     Pt2 = Tfi32;
    3623          33 :     FOR( i = 0; i < N_LP; i++ )
    3624             :     {
    3625          32 :         *pt1 = extract_h( *Pt1 ); /*Qin+exp-16 */
    3626          32 :         move16();
    3627          32 :         *pt2 = extract_h( *Pt2 ); /*Qin+exp-16 */
    3628          32 :         move16();
    3629          32 :         *pt3 = abs_iter_fx( Tfr[i], Tfi[i], 5 );
    3630          32 :         move16();                         /*Qin+exp-16 */
    3631          32 :         L_tmp = L_mac0( L_tmp, *pt3, 1 ); /*Qin+exp-16 */
    3632          32 :         pt1++;
    3633          32 :         pt2++;
    3634          32 :         pt3++;
    3635          32 :         Pt1++;
    3636          32 :         Pt2++;
    3637             :     }
    3638           1 :     *sum_Tf_abs = L_tmp;
    3639           1 :     move32();
    3640           1 :     *exp = sub( *exp, 16 );
    3641           1 :     move16();
    3642           1 :     return;
    3643             : }
    3644             : 
    3645           0 : static void fec_ecu_dft_fx(
    3646             :     const Word16 *prevsynth_LP, /*Qin */
    3647             :     const Word16 N,
    3648             :     Word16 *Tfr,        /*Qout */
    3649             :     Word16 *Tfi,        /*Qout */
    3650             :     Word32 *sum_Tf_abs, /*Qout; */
    3651             :     Word16 *Tf_abs,     /*Qout */
    3652             :     Word16 *Nfft,
    3653             :     Word16 *exp /*Qout = Qin+exp */
    3654             : )
    3655             : {
    3656             :     Word32 L_tmp, Tmp, Tfr32[512], Tfi32[512], fac, *Pt1, *Pt2;
    3657             :     Word16 i, tmp, tmp_short, N_LP, target[2 * L_FRAME48k], Tfr16[FEC_FFT_MAX_SIZE], *pt1, *pt2, *pt3;
    3658             :     Word16 tmp_loop;
    3659           0 :     tmp = sub( 296, N );
    3660           0 :     Copy( &prevsynth_LP[tmp], target, N );
    3661             : 
    3662             : 
    3663             :     /* DFT  */
    3664             : 
    3665           0 :     L_tmp = L_deposit_l( N );
    3666           0 :     FOR( tmp = 0; L_tmp <= 16384; tmp++ )
    3667             :     {
    3668           0 :         L_tmp = L_shl( L_tmp, 1 );
    3669             :     }
    3670           0 :     *Nfft = shl( 1, sub( 15, tmp ) );
    3671           0 :     move16();
    3672             : 
    3673             : 
    3674           0 :     set32_fx( Tfr32, 0, *Nfft );
    3675           0 :     set32_fx( Tfi32, 0, *Nfft );
    3676           0 :     Tfr16[0] = target[0];
    3677           0 :     move16();
    3678           0 :     Tfr16[*Nfft - 1] = target[N - 1];
    3679           0 :     move16();
    3680             : 
    3681           0 :     IF( EQ_16( *Nfft, N ) )
    3682             :     {
    3683           0 :         Copy( &target[1], &Tfr16[1], sub( *Nfft, 2 ) );
    3684             :     }
    3685             :     ELSE
    3686             :     {
    3687             : 
    3688           0 :         tmp = div_s( sub( N, 1 ), sub( *Nfft, 1 ) );
    3689           0 :         Tmp = L_deposit_l( tmp );
    3690           0 :         fac = L_add( Tmp, 0 );
    3691           0 :         tmp_loop = sub( *Nfft, 1 );
    3692           0 :         FOR( i = 1; i < tmp_loop; i++ ) /* interpolation for FFT */
    3693             :         {
    3694           0 :             tmp_short = extract_l( L_shr( Tmp, 15 ) );
    3695           0 :             tmp = extract_l( L_msu( Tmp, tmp_short, 16384 ) );
    3696           0 :             L_tmp = L_mult( sub_sat( target[tmp_short + 1], target[tmp_short] ), tmp ); /*Qin+16 */
    3697           0 :             Tfr16[i] = add_sat( target[tmp_short], round_fx_sat( L_tmp ) );
    3698           0 :             move16(); /*Qin */
    3699           0 :             Tmp = L_add( Tmp, fac );
    3700             :         }
    3701             :     }
    3702             : 
    3703             : 
    3704             :     /*to avoid overflow in DoRTFTn_fx() */
    3705           0 :     tmp = Exp16Array( *Nfft, Tfr16 );
    3706           0 :     *exp = add( tmp, add( 2, norm_s( *Nfft ) ) );
    3707           0 :     move16();
    3708             :     {
    3709           0 :         Word16 loctmp = *exp;
    3710           0 :         move16();
    3711           0 :         loctmp = s_min( 15, loctmp );
    3712           0 :         Copy_Scale_sig_16_32_DEPREC( Tfr16, Tfr32, *Nfft, loctmp ); /*Qin+exp; */ /*Even with limiting loctmp, if Copy_Scale_sig_16_32_no_sat() is used, can lead to 1 difference */
    3713             :     }
    3714             : 
    3715           0 :     DoRTFTn_fx( Tfr32, Tfi32, *Nfft );
    3716           0 :     N_LP = shr( *Nfft, 1 );
    3717             : 
    3718             : 
    3719           0 :     L_tmp = L_deposit_l( 0 );
    3720             : 
    3721           0 :     pt1 = Tfr;
    3722           0 :     pt2 = Tfi;
    3723           0 :     pt3 = Tf_abs;
    3724           0 :     Pt1 = Tfr32;
    3725           0 :     Pt2 = Tfi32;
    3726           0 :     FOR( i = 0; i < N_LP; i++ )
    3727             :     {
    3728           0 :         *pt1 = extract_h( *Pt1 ); /*Qin+exp-16 */
    3729           0 :         move16();
    3730           0 :         *pt2 = extract_h( *Pt2 ); /*Qin+exp-16 */
    3731           0 :         move16();
    3732           0 :         *pt3 = abs_iter_fx( Tfr[i], Tfi[i], 5 );
    3733           0 :         move16();                         /*Qin+exp-16 */
    3734           0 :         L_tmp = L_mac0( L_tmp, *pt3, 1 ); /*Qin+exp-16 */
    3735           0 :         pt1++;
    3736           0 :         pt2++;
    3737           0 :         pt3++;
    3738           0 :         Pt1++;
    3739           0 :         Pt2++;
    3740             :     }
    3741           0 :     *sum_Tf_abs = L_tmp;
    3742           0 :     move32();
    3743           0 :     *exp = sub( *exp, 16 );
    3744           0 :     move16();
    3745           0 :     return;
    3746             : }
    3747             : 
    3748           8 : static void singenerator_fx(
    3749             :     const Word16 L,       /* i  : size of output */
    3750             :     const Word16 cosfreq, /* i  : cosine of 1-sample dephasing at the given frequency Q15*/
    3751             :     const Word16 sinfreq, /* i  : sine   of 1-sample dephasing at the given frequency Q15*/
    3752             :     const Word16 a_re,
    3753             :     /* i  : real part of complex spectral coefficient at the given frequency */ /*Qin */
    3754             :     const Word16 a_im,
    3755             :     /* i  : imag part of complex spectral coefficient at the given frequency */ /*Qin */
    3756             :     Word32 xx[] /* o  : output vector */                                        /*Qin+16 */
    3757             : )
    3758             : {
    3759             : 
    3760             :     Word32 *ptr, L_C0, L_S0, L_C1, L_S1;
    3761             :     Word16 C0, S0, C1, S1;
    3762             :     Word16 i;
    3763             : 
    3764           8 :     L_S0 = L_deposit_l( 0 );    /*prevent warning*/
    3765           8 :     L_C0 = L_deposit_h( a_re ); /*Qin+16 */
    3766           8 :     S0 = a_im;
    3767           8 :     move16();
    3768             : 
    3769           8 :     ptr = xx;
    3770             : 
    3771           8 :     *ptr = L_add_sat( *ptr, L_C0 );
    3772           8 :     move32();
    3773           8 :     ptr++;
    3774             : 
    3775        5120 :     FOR( i = 0; i < L / 2 - 1; i++ )
    3776             :     {
    3777        5112 :         C0 = extract_h( L_C0 );            /*Qin */
    3778        5112 :         L_C1 = L_mult( C0, cosfreq );      /*Qin+16 */
    3779        5112 :         L_C1 = L_msu( L_C1, S0, sinfreq ); /*Qin+16 */
    3780        5112 :         L_S1 = L_mult( C0, sinfreq );
    3781        5112 :         S1 = mac_r( L_S1, S0, cosfreq );
    3782        5112 :         *ptr = L_add_sat( *ptr, L_C1 );
    3783        5112 :         move32(); /*Qin+16 */
    3784        5112 :         ptr++;
    3785             : 
    3786        5112 :         C1 = extract_h( L_C1 );
    3787        5112 :         L_C0 = L_mult( C1, cosfreq );
    3788        5112 :         L_C0 = L_msu( L_C0, S1, sinfreq );
    3789        5112 :         L_S0 = L_mult( C1, sinfreq );
    3790        5112 :         S0 = mac_r( L_S0, S1, cosfreq );
    3791        5112 :         *ptr = L_add_sat( *ptr, L_C0 );
    3792        5112 :         move32();
    3793        5112 :         ptr++;
    3794             :     }
    3795             : 
    3796           8 :     C0 = extract_h( L_C0 );
    3797           8 :     S0 = extract_h( L_S0 );
    3798           8 :     L_C1 = L_mult( C0, cosfreq );
    3799           8 :     L_C1 = L_msu( L_C1, S0, sinfreq );
    3800           8 :     *ptr = L_add_sat( *ptr, L_C1 );
    3801           8 :     move32();
    3802           8 :     ptr++;
    3803             : 
    3804           8 :     return;
    3805             : }
    3806             : 
    3807           1 : static void sinusoidal_synthesis_fx(
    3808             :     const Word16 *Tfr, /*Qin */
    3809             :     const Word16 *Tfi, /*Qin */
    3810             :     Word16 *Tf_abs,    /*Qin */
    3811             :     const Word16 N,
    3812             :     const Word16 L,
    3813             :     const Word16 decimate_factor,
    3814             :     const Word16 Nfft,
    3815             :     const Word32 sum_Tf_abs, /*Qin */
    3816             :     Word16 *synthesis,       /*Qin */
    3817             :     const Word16 HqVoicing,
    3818             :     Word16 exp )
    3819             : {
    3820           1 :     Word16 i, k, nb_pulses, indmax = 0, nb_pulses_final;
    3821             :     Word16 pulses[FEC_MAX / 2];
    3822             :     Word16 mmax, maxi;
    3823             :     Word32 cumsum, freq, L_tmp;
    3824             :     Word16 freqi[FEC_NB_PULSE_MAX], tmp, q, inv_den, new_s, old, cpt;
    3825             :     Word16 a_re[FEC_NB_PULSE_MAX], a_im[FEC_NB_PULSE_MAX];
    3826           1 :     Word16 Lon20_10 = 80;
    3827             :     Word16 flag, Len;
    3828             :     Word16 cosfreq, sinfreq, sN, PL, glued;
    3829             :     Word32 synthesis_fx[2 * L_FRAME48k];
    3830             :     Word16 *pt1, *pt2, *pt3, *pt4;
    3831             : 
    3832           1 :     move16(); // indmax
    3833           1 :     move16(); // Lon20_10
    3834           1 :     flag = HqVoicing;
    3835           1 :     move16();
    3836           1 :     if ( GT_16( N, Lon20_10 ) )
    3837             :     {
    3838           0 :         flag = 1;
    3839           0 :         move16(); /*flag corresponds to condition sub(N, Lon20_10)>0 || HqVoicing */
    3840             :     }
    3841             : 
    3842             : 
    3843           1 :     pt4 = pulses;
    3844           1 :     nb_pulses = 0;
    3845           1 :     move16();
    3846           1 :     PL = 0;
    3847           1 :     move16();
    3848           1 :     cpt = 0;
    3849           1 :     move16();
    3850           1 :     old = 0;
    3851           1 :     move16();
    3852           1 :     glued = 1;
    3853           1 :     move16();
    3854           1 :     new_s = Tf_abs[1]; // Qin
    3855           1 :     move16();
    3856           1 :     if ( flag )
    3857             :     {
    3858           0 :         PL = 1;
    3859           0 :         move16();
    3860             :     }
    3861           1 :     tmp = sub( shr( N, 1 ), 3 );
    3862          26 :     WHILE( LE_16( cpt, tmp ) )
    3863             :     {
    3864          25 :         test();
    3865          25 :         IF( GT_16( Tf_abs[cpt], old ) && GT_16( Tf_abs[cpt], new_s ) )
    3866          11 :         {
    3867             :             Word16 tmp2;
    3868             : 
    3869          11 :             glued = cpt;
    3870          11 :             move16();
    3871             : 
    3872          11 :             tmp2 = add( add( cpt, PL ), 1 );
    3873          22 :             FOR( i = glued; i < tmp2; i++ )
    3874             :             {
    3875          11 :                 *pt4++ = i;
    3876          11 :                 move16();
    3877          11 :                 nb_pulses++;
    3878          11 :                 move16();
    3879             :             }
    3880          11 :             old = Tf_abs[cpt + PL];
    3881          11 :             move16();
    3882          11 :             new_s = Tf_abs[cpt + 2 + PL];
    3883          11 :             move16();
    3884          11 :             cpt = add( add( cpt, PL ), 1 );
    3885          11 :             move16();
    3886          11 :             glued = 1;
    3887          11 :             move16();
    3888             :         }
    3889             :         ELSE
    3890             :         {
    3891          14 :             old = Tf_abs[cpt];
    3892          14 :             move16();
    3893          14 :             new_s = Tf_abs[cpt + 2];
    3894          14 :             move16();
    3895          14 :             cpt++;
    3896          14 :             glued = 0;
    3897          14 :             move16();
    3898             :         }
    3899             :     }
    3900             : 
    3901             : 
    3902           1 :     nb_pulses_final = 0;
    3903           1 :     move16();
    3904             : 
    3905           1 :     sN = sub( 13, norm_s( Nfft ) ); /*for amplitude normalization by 2/nfft */
    3906             : 
    3907           1 :     cumsum = L_deposit_l( 0 );
    3908             : 
    3909             : 
    3910           1 :     L_tmp = Mult_32_16( sum_Tf_abs, 22938 ); // Qin
    3911             : 
    3912           1 :     pt1 = a_re;
    3913           1 :     pt2 = a_im;
    3914           1 :     pt3 = freqi;
    3915             : 
    3916           1 :     maxi = s_min( FEC_NB_PULSE_MAX, nb_pulses );
    3917           1 :     nb_pulses_final = maxi;
    3918           1 :     move16();
    3919             : 
    3920           1 :     Len = shl( L, 1 );
    3921           1 :     IF( HqVoicing )
    3922             :     {
    3923           0 :         FOR( i = 0; i < maxi; i++ )
    3924             :         {
    3925           0 :             mmax = 0;
    3926           0 :             move16();
    3927           0 :             pt4 = pulses;
    3928           0 :             FOR( k = 0; k < nb_pulses; k++ )
    3929             :             {
    3930           0 :                 tmp = *pt4++;
    3931           0 :                 move16();
    3932           0 :                 if ( GT_16( Tf_abs[tmp], mmax ) )
    3933             :                 {
    3934           0 :                     indmax = tmp;
    3935           0 :                     move16();
    3936             :                 }
    3937           0 :                 mmax = s_max( Tf_abs[tmp], mmax );
    3938             :             }
    3939             : 
    3940           0 :             *pt1++ = Tfr[indmax];
    3941           0 :             move16(); /*L_shr(Tfr[indmax], sN); //instead shr -> scaling of a_re is Qin+sN */
    3942           0 :             *pt2++ = Tfi[indmax];
    3943           0 :             move16(); /*L_shr(Tfi[indmax], sN); //instead shr -> scaling of a_im is Qin+sN */
    3944           0 :             *pt3++ = indmax;
    3945           0 :             move16();
    3946           0 :             Tf_abs[indmax] = -1;
    3947           0 :             move16();
    3948             :         }
    3949             :     }
    3950             :     ELSE
    3951             :     {
    3952             :         DO
    3953             :         {
    3954           8 :             mmax = 0;
    3955           8 :             move16();
    3956           8 :             pt4 = pulses;
    3957          96 :             FOR( k = 0; k < nb_pulses; k++ )
    3958             :             {
    3959          88 :                 tmp = *pt4++;
    3960          88 :                 move16();
    3961          88 :                 if ( GT_16( Tf_abs[tmp], mmax ) )
    3962             :                 {
    3963          19 :                     indmax = tmp;
    3964          19 :                     move16();
    3965             :                 }
    3966          88 :                 mmax = s_max( Tf_abs[tmp], mmax );
    3967             :             }
    3968             : 
    3969           8 :             cumsum = L_mac0( cumsum, mmax, 1 ); // Qin
    3970             : 
    3971           8 :             *pt1++ = Tfr[indmax];
    3972           8 :             move16(); /*L_shr(Tfr[indmax], sN); //instead shr -> scaling of a_re is Qin+sN */
    3973           8 :             *pt2++ = Tfi[indmax];
    3974           8 :             move16(); /*L_shr(Tfi[indmax], sN); //instead shr -> scaling of a_im is Qin+sN */
    3975           8 :             *pt3++ = indmax;
    3976           8 :             move16();
    3977           8 :             Tf_abs[indmax] = -1;
    3978           8 :             move16();
    3979             : 
    3980           8 :             maxi = sub( maxi, 1 );
    3981             :         }
    3982           8 :         WHILE( maxi > 0 && LT_32( cumsum, L_tmp ) );
    3983             : 
    3984           1 :         nb_pulses_final = sub( nb_pulses_final, maxi );
    3985           1 :         move16();
    3986             :     }
    3987             : 
    3988             : 
    3989             :     /* sinusoidal synthesis */
    3990             : 
    3991             : 
    3992           1 :     set32_fx( synthesis_fx, 0, Len );
    3993             : 
    3994           1 :     exp = add( exp, sN );
    3995             : 
    3996             : 
    3997           1 :     pt1 = a_re;
    3998           1 :     pt2 = a_im;
    3999           1 :     pt3 = freqi;
    4000           1 :     q = shr_r( N, 2 );
    4001           1 :     if ( GT_16( N, shl( q, 2 ) ) )
    4002             :     {
    4003           0 :         q = add( q, 1 );
    4004             :     }
    4005             : 
    4006           1 :     inv_den = i_mult2( N, decimate_factor ); /*Q0 */
    4007             : 
    4008             :     /*tmp = div_s(12868,inv_den);*/ /*Q15 */
    4009             : 
    4010             : 
    4011           9 :     FOR( i = 0; i < nb_pulses_final; i++ )
    4012             :     {
    4013             : 
    4014           8 :         tmp = div_s( shl( *pt3, 1 ), inv_den ); /*Q15 */                    /* ind*2/(N*decim) */
    4015           8 :         freq = L_shl( L_mult( tmp, 25736 ), 1 ); /*Q30 */                   /* ind*2/(N*decim)*pi/4*4    never greater than PI/2 */
    4016           8 :         sin_cos_est_fx( freq, &cosfreq, &sinfreq );                         /*cosfreq & sinfreq in Q15 */
    4017           8 :         singenerator_fx( Len, cosfreq, sinfreq, *pt1, *pt2, synthesis_fx ); /*Qin      */
    4018             : 
    4019           8 :         pt1++;
    4020           8 :         pt2++;
    4021           8 :         pt3++;
    4022             :     }
    4023           1 :     Copy_Scale_sig_32_16( synthesis_fx, synthesis, Len, negate( add( exp, 16 ) ) ); /*Qin */
    4024             : 
    4025             : 
    4026           1 :     return;
    4027             : }
    4028             : 
    4029           1 : static void ivas_fec_noise_filling_fx(
    4030             :     const Word16 *prevsynth_fx, /*Qsynth   */
    4031             :     Word16 *synthesis_fx,       /*Qsynth */
    4032             :     Word16 *ni_seed_forfec,
    4033             :     const Word16 L,
    4034             :     const Word16 N,
    4035             :     const Word16 HqVoicing,
    4036             :     Word16 *gapsynth_fx,       /*Qsynth */
    4037             :     const Word16 element_mode, /* i  : IVAS element mode                          */
    4038             :     const Word16 *old_out,
    4039             :     const Word16 Q_old_out )
    4040             : {
    4041             : 
    4042             :     Word16 Rnd_N_noise;
    4043             :     Word16 k, kk, i;
    4044             :     Word16 N_noise;
    4045             : 
    4046             :     Word16 tmp_fx, ind, q1, q2, L20, flag;
    4047             :     Word16 noisevect_fx[34 * L_FRAME48k / 20], SS_fx[L_FRAME48k / 2];
    4048             :     Word16 *pt1, *pt2, *pt3, *pt4, *pt5;
    4049             :     const Word16 *pt6;
    4050             :     Word32 L_tmp;
    4051             :     const Word16 *sinq_tab;
    4052             : 
    4053             :     const Word16 *p_mdct_ola;
    4054             :     Word16 alignment_point;
    4055             :     // PMTE()
    4056           1 :     L20 = extract_h( L_mult( 1639, L ) ); /*L/20 */
    4057           1 :     IF( element_mode == EVS_MONO )
    4058             :     {
    4059           0 :         alignment_point = sub( shl( L, 1 ), i_mult2( 3, L20 ) );
    4060             :     }
    4061             :     ELSE
    4062             :     {
    4063           1 :         alignment_point = shl( L, 1 );
    4064             :     }
    4065             : 
    4066           1 :     IF( EQ_16( L, L_FRAME32k ) )
    4067             :     {
    4068           1 :         sinq_tab = sinq_32k;
    4069             :     }
    4070           0 :     ELSE IF( EQ_16( L, L_FRAME48k ) )
    4071             :     {
    4072           0 :         sinq_tab = sinq_48k;
    4073             :     }
    4074             :     ELSE
    4075             :     {
    4076           0 :         sinq_tab = sinq_16k;
    4077             :     }
    4078             : 
    4079           1 :     Copy( prevsynth_fx + sub( alignment_point, N ), noisevect_fx, N );
    4080             : 
    4081             :     /* Noise addition on full band  */
    4082             :     /* residual  */
    4083             : 
    4084           1 :     tmp_fx = s_min( N, L );
    4085           1 :     N_noise = shr( tmp_fx, 1 );
    4086           1 :     ind = sub( N, tmp_fx );
    4087           1 :     pt1 = noisevect_fx;
    4088           1 :     pt2 = pt1 + ind;
    4089           1 :     move16();
    4090           1 :     pt3 = &synthesis_fx[ind];
    4091           1 :     move16();
    4092         221 :     FOR( k = 0; k < tmp_fx; k++ )
    4093             :     {
    4094         220 :         ( *pt1++ ) = sub_sat( ( *pt2++ ), ( *pt3++ ) );
    4095         220 :         move16();
    4096             :     }
    4097             : 
    4098           1 :     IF( HqVoicing )
    4099             :     {
    4100           0 :         Scale_sig( noisevect_fx, N, -2 );
    4101             :     }
    4102             : 
    4103           1 :     kk = 0;
    4104           1 :     move16();
    4105           1 :     k = 0;
    4106           1 :     move16();
    4107           1 :     Rnd_N_noise = N_noise;
    4108           1 :     move16();
    4109             : 
    4110           1 :     ind = shl( L, 1 );
    4111           1 :     flag = ind;
    4112           1 :     move16();
    4113           1 :     pt5 = synthesis_fx;
    4114          21 :     WHILE( flag > 0 )
    4115             :     {
    4116          20 :         tmp_fx = Random( ni_seed_forfec );
    4117             : 
    4118          20 :         L_tmp = L_mac( 1503264768, tmp_fx, 9830 );
    4119          20 :         IF( EQ_16( kk, 0 ) )
    4120             :         {
    4121          10 :             L_tmp = L_mac( 1073741824, tmp_fx, 6554 );
    4122             :         }
    4123             : 
    4124          20 :         kk = sub( 1, kk );
    4125          20 :         tmp_fx = round_fx( L_tmp );
    4126          20 :         Rnd_N_noise = extract_h( L_mult( N_noise, tmp_fx ) ); /*Q0 */
    4127             : 
    4128             : 
    4129          20 :         tmp_fx = div_s( 1, Rnd_N_noise );                         /*Q15 */
    4130          20 :         tmp_fx = round_fx( L_shl( L_mult( tmp_fx, 25736 ), 2 ) ); /*Q15 */
    4131             : 
    4132          20 :         sinq_fx( shr( tmp_fx, 1 ), shr( tmp_fx, 2 ), Rnd_N_noise, SS_fx );
    4133             : 
    4134          20 :         pt2 = &noisevect_fx[N_noise];
    4135          20 :         pt1 = pt2 - Rnd_N_noise;
    4136          20 :         pt3 = SS_fx;
    4137          20 :         pt4 = pt3 + sub( Rnd_N_noise, 1 );
    4138          20 :         tmp_fx = s_min( Rnd_N_noise, flag );
    4139        1300 :         FOR( i = 0; i < tmp_fx; i++ )
    4140             :         {
    4141        1280 :             L_tmp = L_mult( ( *pt1++ ), ( *pt3++ ) );           /*Qsynth+16 */
    4142        1280 :             L_tmp = L_mac_sat( L_tmp, ( *pt2++ ), ( *pt4-- ) ); /*Qsynth+16 */
    4143        1280 :             *pt5 = add_sat( *pt5, round_fx_sat( L_tmp ) );
    4144        1280 :             move16(); /*Qsynth */
    4145        1280 :             pt5++;
    4146             :         }
    4147          20 :         flag = sub( flag, tmp_fx );
    4148             :     }
    4149             : 
    4150           1 :     IF( element_mode == EVS_MONO )
    4151             :     {
    4152           0 :         kk = i_mult2( 7, L20 );
    4153           0 :         tmp_fx = i_mult2( 37, L20 );
    4154           0 :         p_mdct_ola = prevsynth_fx + tmp_fx;
    4155             :     }
    4156             :     ELSE
    4157             :     {
    4158           1 :         kk = NS2SA_FX2( L_mult0( L, FRAMES_PER_SEC ), N_ZERO_MDCT_NS );
    4159           1 :         p_mdct_ola = old_out + kk;
    4160             :     }
    4161             : 
    4162             :     /* overlappadd with the ms of valid mdct of the last frame */
    4163           1 :     tmp_fx = i_mult2( 3, L20 );
    4164           1 :     pt1 = &synthesis_fx[0];
    4165           1 :     pt6 = &p_mdct_ola[0];
    4166          97 :     FOR( k = 0; k < tmp_fx; k++ )
    4167             :     {
    4168          96 :         L_tmp = L_mult( *sinq_tab, *sinq_tab ); /*Q31 */
    4169          96 :         sinq_tab++;
    4170          96 :         q2 = round_fx( L_sub( 2147483647, L_tmp ) );                                                  /*Q15 */
    4171          96 :         q1 = round_fx( L_tmp );                                                                       /*Q15 */
    4172          96 :         L_tmp = L_mult( ( *pt1 ), q1 );                                                               /*Qsynth+16 */
    4173          96 :         L_tmp = L_add_sat( L_tmp, L_shr_sat( Mpy_32_16_1( L_deposit_h( *pt6++ ), q2 ), Q_old_out ) ); /*Qsynth+16 */
    4174          96 :         ( *pt1++ ) = round_fx_sat( L_tmp );                                                           /*Qsynth */
    4175          96 :         move16();
    4176             :     }
    4177             : 
    4178           1 :     Copy( synthesis_fx, synthesis_fx + kk, sub( 2 * L, kk ) );
    4179           1 :     Copy( synthesis_fx + L, gapsynth_fx, L );
    4180           1 :     Copy( prevsynth_fx + sub( alignment_point, kk ), synthesis_fx, kk );
    4181           1 : }
    4182             : 
    4183           0 : static void fec_noise_filling_fx(
    4184             :     const Word16 *prevsynth_fx, /*Qsynth   */
    4185             :     Word16 *synthesis_fx,       /*Qsynth */
    4186             :     Word16 *ni_seed_forfec,
    4187             :     const Word16 L,
    4188             :     const Word16 N,
    4189             :     const Word16 HqVoicing,
    4190             :     Word16 *gapsynth_fx /*Qsynth */
    4191             : )
    4192             : {
    4193             : 
    4194             :     Word16 Rnd_N_noise;
    4195             :     Word16 k, kk, i;
    4196             :     Word16 N_noise;
    4197             : 
    4198             :     Word16 tmp_fx, ind, q1, q2, L20, flag;
    4199             :     Word16 noisevect_fx[34 * L_FRAME48k / 20], SS_fx[L_FRAME48k / 2];
    4200             :     Word16 *pt1, *pt2, *pt3, *pt4, *pt5;
    4201             :     const Word16 *pt6;
    4202             :     Word32 L_tmp;
    4203             :     const Word16 *sinq_tab;
    4204             : 
    4205           0 :     IF( EQ_16( L, L_FRAME32k ) )
    4206             :     {
    4207           0 :         sinq_tab = sinq_32k;
    4208             :     }
    4209           0 :     ELSE IF( EQ_16( L, L_FRAME48k ) )
    4210             :     {
    4211           0 :         sinq_tab = sinq_48k;
    4212             :     }
    4213             :     ELSE
    4214             :     {
    4215           0 :         sinq_tab = sinq_16k;
    4216             :     }
    4217             : 
    4218           0 :     L20 = extract_h( L_mult( 1639, L ) ); /*L/20 */
    4219             : 
    4220             :     /*N=47*L/20-delay_ind*decimatefator-6*L/20; */
    4221             : 
    4222           0 :     tmp_fx = sub( sub( shl( L, 1 ), i_mult2( 3, L20 ) ), N );
    4223           0 :     Copy( prevsynth_fx + tmp_fx, noisevect_fx, N );
    4224             : 
    4225             :     /* Noise addition on full band  */
    4226             :     /* residual  */
    4227             : 
    4228           0 :     tmp_fx = s_min( N, L );
    4229           0 :     N_noise = shr( tmp_fx, 1 );
    4230           0 :     ind = sub( N, tmp_fx );
    4231           0 :     pt1 = noisevect_fx;
    4232           0 :     pt2 = pt1 + ind;
    4233           0 :     move16();
    4234           0 :     pt3 = &synthesis_fx[ind];
    4235           0 :     move16();
    4236           0 :     FOR( k = 0; k < tmp_fx; k++ )
    4237             :     {
    4238           0 :         ( *pt1++ ) = sub_sat( ( *pt2++ ), ( *pt3++ ) );
    4239           0 :         move16();
    4240             :     }
    4241             : 
    4242           0 :     IF( HqVoicing )
    4243             :     {
    4244           0 :         Scale_sig( noisevect_fx, N, -2 );
    4245             :     }
    4246             : 
    4247           0 :     kk = 0;
    4248           0 :     move16();
    4249           0 :     k = 0;
    4250           0 :     move16();
    4251           0 :     Rnd_N_noise = N_noise;
    4252           0 :     move16();
    4253             : 
    4254           0 :     ind = shl( L, 1 );
    4255           0 :     flag = ind;
    4256           0 :     move16();
    4257           0 :     pt5 = synthesis_fx;
    4258           0 :     WHILE( flag > 0 )
    4259             :     {
    4260           0 :         tmp_fx = Random( ni_seed_forfec );
    4261             : 
    4262           0 :         L_tmp = L_mac( 1503264768, tmp_fx, 9830 );
    4263           0 :         if ( kk == 0 )
    4264             :         {
    4265           0 :             L_tmp = L_mac( 1073741824, tmp_fx, 6554 );
    4266             :         }
    4267             : 
    4268           0 :         kk = sub( 1, kk );
    4269           0 :         tmp_fx = round_fx( L_tmp );
    4270           0 :         Rnd_N_noise = extract_h( L_mult( N_noise, tmp_fx ) ); /*Q0 */
    4271             : 
    4272             : 
    4273           0 :         tmp_fx = div_s( 1, Rnd_N_noise );                                 /*Q15 */
    4274           0 :         tmp_fx = round_fx_sat( L_shl_sat( L_mult( tmp_fx, 25736 ), 2 ) ); /*Q15 */
    4275             : 
    4276           0 :         sinq_fx( shr( tmp_fx, 1 ), shr( tmp_fx, 2 ), Rnd_N_noise, SS_fx );
    4277             : 
    4278           0 :         pt2 = &noisevect_fx[N_noise];
    4279           0 :         pt1 = pt2 - Rnd_N_noise;
    4280           0 :         pt3 = SS_fx;
    4281           0 :         pt4 = pt3 + sub( Rnd_N_noise, 1 );
    4282           0 :         tmp_fx = s_min( Rnd_N_noise, flag );
    4283           0 :         FOR( i = 0; i < tmp_fx; i++ )
    4284             :         {
    4285           0 :             L_tmp = L_mult( ( *pt1++ ), ( *pt3++ ) );           /*Qsynth+16 */
    4286           0 :             L_tmp = L_mac_sat( L_tmp, ( *pt2++ ), ( *pt4-- ) ); /*Qsynth+16 */
    4287           0 :             *pt5 = add_sat( *pt5, round_fx_sat( L_tmp ) );
    4288           0 :             move16(); /*Qsynth */
    4289           0 :             pt5++;
    4290             :         }
    4291           0 :         flag = sub( flag, tmp_fx );
    4292             :     }
    4293             : 
    4294           0 :     q1 = i_mult2( 7, L20 );
    4295           0 :     q2 = i_mult2( 33, L20 );
    4296             : 
    4297           0 :     Copy( synthesis_fx, synthesis_fx + q1, q2 );
    4298           0 :     Copy( synthesis_fx + L, gapsynth_fx, L );
    4299           0 :     Copy( prevsynth_fx + sub( i_mult2( 37, L20 ), q1 ), synthesis_fx, q1 );
    4300           0 :     pt1 = &synthesis_fx[q1];
    4301           0 :     q2 = i_mult2( 37, L20 );
    4302           0 :     pt6 = &prevsynth_fx[q2];
    4303           0 :     tmp_fx = i_mult2( 3, L20 );
    4304             : 
    4305             :     /* overlappadd with the ms of valid mdct of the last frame */
    4306           0 :     FOR( k = 0; k < tmp_fx; k++ )
    4307             :     {
    4308           0 :         L_tmp = L_mult( *sinq_tab, *sinq_tab ); /*Q30 */
    4309           0 :         sinq_tab++;
    4310           0 :         q2 = round_fx( L_sub( 2147483647, L_tmp ) ); /*Q15 */
    4311           0 :         q1 = round_fx( L_tmp );                      /*Q15 */
    4312           0 :         L_tmp = L_mult( ( *pt1 ), q1 );              /*Qsynth+16 */
    4313           0 :         L_tmp = L_mac( L_tmp, ( *pt6++ ), q2 );      /*Qsynth+16 */
    4314           0 :         ( *pt1++ ) = round_fx( L_tmp );              /*Qsynth */
    4315           0 :         move16();
    4316             :     }
    4317           0 : }
    4318             : 
    4319           1 : static void ivas_fec_alg_fx(
    4320             :     const Word16 *prevsynth,    /*Qin */
    4321             :     const Word16 *prevsynth_LP, /*Qin */
    4322             :     Word16 *ni_seed_forfec,
    4323             :     Word32 *ecu_rec, /*Qin+16 (Qin+15 to be coherent witch other scaling) */
    4324             :     const Word16 output_frame,
    4325             :     const Word16 N,
    4326             :     const Word16 decimatefactor,
    4327             :     const Word16 HqVoicing,
    4328             :     Word16 *gapsynth,          /*Qin */
    4329             :     const Word16 element_mode, /* i  : IVAS element mode                          */
    4330             :     const Word16 *old_out,
    4331             :     const Word16 Q_old_out )
    4332             : {
    4333             :     Word16 Nfft;
    4334             :     Word32 sum_Tf_abs;
    4335             :     Word16 Tfr[FEC_FFT_MAX_SIZE];
    4336             :     Word16 Tfi[FEC_FFT_MAX_SIZE];
    4337             :     Word16 Tf_abs[FEC_FFT_MAX_SIZE / 2];
    4338             :     Word16 synthesis[2 * L_FRAME48k];
    4339             :     Word16 exp;
    4340             :     Word16 n, Q;
    4341             : 
    4342           1 :     ivas_fec_ecu_dft_fx( prevsynth_LP, N, Tfr, Tfi, &sum_Tf_abs, Tf_abs, &Nfft, &exp, element_mode );
    4343             : 
    4344           1 :     sinusoidal_synthesis_fx( Tfr, Tfi, Tf_abs, N, output_frame, decimatefactor, Nfft, sum_Tf_abs, synthesis, HqVoicing, exp );
    4345             : 
    4346           1 :     ivas_fec_noise_filling_fx( prevsynth, synthesis, ni_seed_forfec, output_frame, i_mult2( N, decimatefactor ), HqVoicing, gapsynth, element_mode, old_out, Q_old_out );
    4347             : 
    4348           1 :     n = R1_48 - R2_48;
    4349           1 :     move16();
    4350             : 
    4351           1 :     test();
    4352           1 :     IF( EQ_16( output_frame, L_FRAME32k ) || EQ_16( output_frame, L_FRAME16k ) )
    4353             :     {
    4354           1 :         n = R1_16 - R2_16;
    4355           1 :         move16();
    4356             : 
    4357           1 :         if ( EQ_16( output_frame, L_FRAME32k ) )
    4358             :         {
    4359           1 :             n = 2 * N16_CORE_SW;
    4360           1 :             move16();
    4361             :         }
    4362             :     }
    4363           1 :     Q = 0;
    4364           1 :     move16();
    4365           1 :     wtda_fx( synthesis + sub( output_frame, n ), &Q, ecu_rec,
    4366             :              NULL,
    4367             :              NULL, ALDO_WINDOW, ALDO_WINDOW, output_frame ); /* return Q15 */
    4368             : 
    4369           1 :     return;
    4370             : }
    4371             : 
    4372           0 : static void fec_alg_fx(
    4373             :     const Word16 *prevsynth,    /*Qin */
    4374             :     const Word16 *prevsynth_LP, /*Qin */
    4375             :     Word16 *ni_seed_forfec,
    4376             :     Word32 *ecu_rec, /*Qin+16 (Qin+15 to be coherent witch other scaling) */
    4377             :     const Word16 output_frame,
    4378             :     const Word16 N,
    4379             :     const Word16 decimatefactor,
    4380             :     const Word16 HqVoicing,
    4381             :     Word16 *gapsynth /*Qin */
    4382             : )
    4383             : {
    4384             :     Word16 Nfft;
    4385             :     Word32 sum_Tf_abs;
    4386             :     Word16 Tfr[FEC_FFT_MAX_SIZE];
    4387             :     Word16 Tfi[FEC_FFT_MAX_SIZE];
    4388             :     Word16 Tf_abs[FEC_FFT_MAX_SIZE / 2];
    4389             :     Word16 synthesis[2 * L_FRAME48k];
    4390             :     Word16 exp;
    4391             :     Word16 n, Q;
    4392             : 
    4393           0 :     fec_ecu_dft_fx( prevsynth_LP, N, Tfr, Tfi, &sum_Tf_abs, Tf_abs, &Nfft, &exp );
    4394             : 
    4395           0 :     sinusoidal_synthesis_fx( Tfr, Tfi, Tf_abs, N, output_frame, decimatefactor, Nfft, sum_Tf_abs, synthesis, HqVoicing, exp );
    4396             : 
    4397           0 :     fec_noise_filling_fx( prevsynth, synthesis, ni_seed_forfec, output_frame, i_mult2( N, decimatefactor ), HqVoicing, gapsynth );
    4398             : 
    4399           0 :     n = R1_48 - R2_48;
    4400           0 :     move16();
    4401             : 
    4402           0 :     test();
    4403           0 :     IF( EQ_16( output_frame, L_FRAME32k ) || EQ_16( output_frame, L_FRAME16k ) )
    4404             :     {
    4405           0 :         n = R1_16 - R2_16;
    4406           0 :         move16();
    4407             : 
    4408           0 :         if ( EQ_16( output_frame, L_FRAME32k ) )
    4409             :         {
    4410           0 :             n = 2 * N16_CORE_SW;
    4411           0 :             move16();
    4412             :         }
    4413             :     }
    4414           0 :     Q = 0;
    4415           0 :     move16();
    4416           0 :     wtda_fx( synthesis + sub( output_frame, n ), &Q, ecu_rec,
    4417             :              NULL,
    4418             :              NULL, ALDO_WINDOW, ALDO_WINDOW, output_frame ); /* return Q15 */
    4419             : 
    4420           0 :     return;
    4421             : }
    4422             : 
    4423             : /*--------------------------------------------------------------------------
    4424             :  *  hq_phase_ecu_fx()
    4425             :  *
    4426             :  *  Main routine for HQ phase ECU
    4427             :  *--------------------------------------------------------------------------*/
    4428             : 
    4429         132 : static void ivas_hq_phase_ecu_fx(
    4430             :     const Word16 *prevsynth,          /* i  : buffer of previously synthesized signal Q0   */
    4431             :     Word32 *ecu_rec,                  /* o  : reconstructed frame in tda domain         */
    4432             :     Word16 *time_offs,                /* i/o: Sample offset for consecutive frame losses Q0*/
    4433             :     Word16 *X_sav,                    /* i/o: Stored spectrum of prototype frame        */
    4434             :     Word16 *Q_spec,                   /* i/o: Q value of stored spectrum                */
    4435             :     Word16 *num_p,                    /* i/o: Number of identified peaks Q0                */
    4436             :     Word16 *plocs,                    /* i/o: Peak locations Q0                            */
    4437             :     Word32 *plocsi,                   /* i/o: Interpolated peak locations           Q16 */
    4438             :     const Word16 env_stab,            /* i  : Envelope stability parameter              */
    4439             :     Word16 *last_fec,                 /* i/o: Flag for usage of pitch dependent ECU     */
    4440             :     Word16 *ph_ecu_active,            /* i  : Phase ECU active flag                     */
    4441             :     const Word16 prev_bfi,            /* i   : indicating burst frame error             */
    4442             :     const Word16 old_is_transient[2], /* i   : flags indicating previous transient frames */
    4443             :     Word16 *mag_chg_1st,              /* i/o: per band magnitude modifier for transients*/
    4444             :     Word16 *Xavg,                     /* i/o: Frequency group average gain to fade to Q0  */
    4445             :     Word16 *beta_mute,                /* o   : Factor for long-term mute Q15                */
    4446             :     const Word16 bwidth_fx,           /* i  : Encoded bandwidth                         */
    4447             :     const Word16 output_frame,        /* i   : frame length                             */
    4448             :     const Word16 pcorr,
    4449             :     const Word16 element_mode /* i  : IVAS element mode                          */
    4450             : )
    4451             : {
    4452             :     Word16 lprot, offset;
    4453             :     Word16 mag_chg[LGW_MAX], ph_dith, X[L_PROT48k];
    4454             :     Word16 seed;
    4455             :     Word16 alpha[LGW_MAX], beta[LGW_MAX];
    4456             : 
    4457             :     const Word16 *old_dec;
    4458             :     Word16 noise_fac;
    4459             :     Word16 ph_ecu_lookahead;
    4460             : 
    4461         132 :     noise_fac = 32767; /* 1.0f in Q15 */
    4462         132 :     move16();
    4463             : 
    4464         132 :     IF( element_mode == EVS_MONO )
    4465             :     {
    4466           0 :         ph_ecu_lookahead = NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), PH_ECU_LOOKAHEAD_NS );
    4467           0 :         move16();
    4468             :     }
    4469             :     ELSE
    4470             :     {
    4471         132 :         ph_ecu_lookahead = 0;
    4472         132 :         move16();
    4473             :     }
    4474         132 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    4475             :     {
    4476         103 :         lprot = L_PROT48k; /* 1536 = (2*output_frame)*1024/1280 */
    4477         103 :         move16();
    4478             :     }
    4479          29 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    4480             :     {
    4481          29 :         lprot = L_PROT32k; /* 1024 */
    4482          29 :         move16();
    4483             :     }
    4484           0 :     ELSE IF( EQ_16( output_frame, L_FRAME16k ) )
    4485             :     {
    4486           0 :         lprot = 512;
    4487           0 :         move16();
    4488             :     }
    4489             :     ELSE
    4490             :     {
    4491           0 :         lprot = 256;
    4492           0 :         move16();
    4493             :     }
    4494             : 
    4495         132 :     test();
    4496         132 :     test();
    4497         132 :     test();
    4498         132 :     IF( prev_bfi == 0 || ( prev_bfi != 0 && *last_fec != 0 && ( EQ_16( *time_offs, output_frame ) ) ) )
    4499             :     {
    4500          54 :         test();
    4501          54 :         test();
    4502             :         // PMT("verify condition compared to float")
    4503          54 :         if ( !( prev_bfi != 0 && *last_fec != 0 && element_mode == EVS_MONO ) )
    4504             :         {
    4505          54 :             *time_offs = 0;
    4506          54 :             move16();
    4507             :         }
    4508             : 
    4509          54 :         offset = add( sub( sub( shl( output_frame, 1 ), lprot ), *time_offs ), ph_ecu_lookahead );
    4510          54 :         trans_ana_fx( prevsynth + offset, mag_chg, &ph_dith, mag_chg_1st, output_frame, *time_offs, env_stab,
    4511          54 :                       *last_fec, alpha, beta, beta_mute, Xavg );
    4512             : 
    4513          54 :         ivas_spec_ana_fx( prevsynth + offset, plocs, plocsi, num_p, X_sav, output_frame, bwidth_fx, Q_spec, element_mode, &noise_fac, pcorr );
    4514             : 
    4515          54 :         test();
    4516          54 :         IF( prev_bfi != 0 && *last_fec != 0 )
    4517             :         {
    4518           0 :             *time_offs = add( *time_offs, output_frame );
    4519           0 :             move16();
    4520             :         }
    4521             :     }
    4522             :     ELSE
    4523             :     {
    4524          78 :         *time_offs = add_sat( *time_offs, output_frame );
    4525          78 :         move16();
    4526          78 :         offset = sub( shl( output_frame, 1 ), lprot );
    4527          78 :         trans_ana_fx( prevsynth + offset, mag_chg, &ph_dith, mag_chg_1st, output_frame, *time_offs, env_stab,
    4528             :                       0, alpha, beta, beta_mute, Xavg );
    4529             :     }
    4530             : 
    4531         132 :     Copy( X_sav, X, lprot );
    4532             : 
    4533             :     /* seed for own_rand2 */
    4534         132 :     seed = *time_offs;
    4535         132 :     move16();
    4536         132 :     IF( *num_p > 0 )
    4537             :     {
    4538         132 :         seed = extract_l( L_add( L_deposit_l( seed ), L_deposit_l( plocs[*num_p - 1] ) ) );
    4539             :     }
    4540             : 
    4541         132 :     ivas_subst_spec_fx( plocs, plocsi, num_p, *time_offs, X, mag_chg, ph_dith, old_is_transient, output_frame, &seed,
    4542         132 :                         alpha, beta, *beta_mute, Xavg, element_mode, ph_ecu_lookahead, noise_fac );
    4543             : 
    4544             :     /* reconstructed frame in tda domain */
    4545         132 :     old_dec = prevsynth + sub( shl( output_frame, 1 ), NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), N_ZERO_MDCT_NS ) );
    4546         132 :     ivas_rec_frame_fx( X, ecu_rec, output_frame, *Q_spec, old_dec, element_mode, num_p, plocs );
    4547             : 
    4548         132 :     *last_fec = 0;
    4549         132 :     move16();
    4550         132 :     *ph_ecu_active = 1;
    4551         132 :     move16();
    4552         132 : }
    4553             : 
    4554           0 : static void hq_phase_ecu_fx(
    4555             :     const Word16 *prevsynth,          /* i  : buffer of previously synthesized signal Q0   */
    4556             :     Word32 *ecu_rec,                  /* o  : reconstructed frame in tda domain         */
    4557             :     Word16 *time_offs,                /* i/o: Sample offset for consecutive frame losses Q0*/
    4558             :     Word16 *X_sav,                    /* i/o: Stored spectrum of prototype frame        */
    4559             :     Word16 *Q_spec,                   /* i/o: Q value of stored spectrum                */
    4560             :     Word16 *num_p,                    /* i/o: Number of identified peaks Q0                */
    4561             :     Word16 *plocs,                    /* i/o: Peak locations Q0                            */
    4562             :     Word32 *plocsi,                   /* i/o: Interpolated peak locations           Q16 */
    4563             :     const Word16 env_stab,            /* i  : Envelope stability parameter              */
    4564             :     Word16 *last_fec,                 /* i/o: Flag for usage of pitch dependent ECU     */
    4565             :     Word16 *ph_ecu_active,            /* i  : Phase ECU active flag                     */
    4566             :     const Word16 prev_bfi,            /* i   : indicating burst frame error             */
    4567             :     const Word16 old_is_transient[2], /* i   : flags indicating previous transient frames */
    4568             :     Word16 *mag_chg_1st,              /* i/o: per band magnitude modifier for transients*/
    4569             :     Word16 *Xavg,                     /* i/o: Frequency group average gain to fade to Q0   */
    4570             :     Word16 *beta_mute,                /* o   : Factor for long-term mute Q15                */
    4571             :     const Word16 bwidth_fx,           /* i  : Encoded bandwidth                         */
    4572             :     const Word16 output_frame         /* i   : frame length                             */
    4573             : )
    4574             : {
    4575             :     Word16 lprot, offset;
    4576             :     Word16 mag_chg[LGW_MAX], ph_dith, X[L_PROT48k];
    4577             :     Word16 seed;
    4578             :     Word16 alpha[LGW_MAX], beta[LGW_MAX];
    4579             : 
    4580           0 :     IF( EQ_16( output_frame, L_FRAME48k ) )
    4581             :     {
    4582           0 :         lprot = L_PROT48k; /* 1536 = (2*output_frame)*1024/1280 */
    4583           0 :         move16();
    4584             :     }
    4585           0 :     ELSE IF( EQ_16( output_frame, L_FRAME32k ) )
    4586             :     {
    4587           0 :         lprot = L_PROT32k; /* 1024 */
    4588           0 :         move16();
    4589             :     }
    4590           0 :     ELSE IF( output_frame == L_FRAME16k )
    4591             :     {
    4592           0 :         lprot = 512;
    4593           0 :         move16();
    4594             :     }
    4595             :     ELSE
    4596             :     {
    4597           0 :         lprot = 256;
    4598           0 :         move16();
    4599             :     }
    4600             : 
    4601           0 :     test();
    4602           0 :     test();
    4603           0 :     test();
    4604           0 :     IF( prev_bfi == 0 || ( prev_bfi != 0 && *last_fec != 0 && ( EQ_16( *time_offs, output_frame ) ) ) )
    4605             :     {
    4606           0 :         test();
    4607             :         // PMT("verify condition compared to float")
    4608           0 :         if ( !( prev_bfi != 0 && *last_fec != 0 ) )
    4609             :         {
    4610           0 :             *time_offs = 0;
    4611           0 :             move16();
    4612             :         }
    4613             : 
    4614           0 :         offset = sub( sub( shl( output_frame, 1 ), lprot ), *time_offs );
    4615           0 :         trans_ana_fx( prevsynth + offset, mag_chg, &ph_dith, mag_chg_1st, output_frame, *time_offs, env_stab,
    4616           0 :                       *last_fec, alpha, beta, beta_mute, Xavg );
    4617             : 
    4618           0 :         spec_ana_fx( prevsynth + offset, plocs, plocsi, num_p, X_sav, output_frame, bwidth_fx, Q_spec );
    4619             : 
    4620           0 :         test();
    4621           0 :         IF( prev_bfi != 0 && *last_fec != 0 )
    4622             :         {
    4623           0 :             *time_offs = add( *time_offs, output_frame );
    4624           0 :             move16();
    4625             :         }
    4626             :     }
    4627             :     ELSE
    4628             :     {
    4629           0 :         *time_offs = add( *time_offs, output_frame );
    4630           0 :         move16();
    4631             : 
    4632           0 :         offset = sub( shl( output_frame, 1 ), lprot );
    4633           0 :         trans_ana_fx( prevsynth + offset, mag_chg, &ph_dith, mag_chg_1st, output_frame, *time_offs, env_stab,
    4634             :                       0, alpha, beta, beta_mute, Xavg );
    4635             :     }
    4636             : 
    4637           0 :     Copy( X_sav, X, lprot );
    4638             : 
    4639             :     /* seed for own_rand2 */
    4640           0 :     seed = *time_offs;
    4641           0 :     move16();
    4642           0 :     IF( *num_p > 0 )
    4643             :     {
    4644           0 :         seed = add( seed, plocs[*num_p - 1] );
    4645             :     }
    4646             : 
    4647           0 :     subst_spec_fx( plocs, plocsi, num_p, *time_offs, X, mag_chg, ph_dith, old_is_transient, output_frame, &seed,
    4648           0 :                    alpha, beta, *beta_mute, Xavg );
    4649             : 
    4650             :     /* reconstructed frame in tda domain */
    4651           0 :     rec_frame_fx( X, ecu_rec, output_frame, *Q_spec );
    4652             : 
    4653           0 :     *last_fec = 0;
    4654           0 :     move16();
    4655           0 :     *ph_ecu_active = 1;
    4656           0 :     move16();
    4657           0 : }
    4658             : 
    4659             : 
    4660             : /*--------------------------------------------------------------------------
    4661             :  *  hq_ecu()
    4662             :  *
    4663             :  *  Main routine for HQ ECU
    4664             :  *--------------------------------------------------------------------------*/
    4665             : 
    4666         133 : void ivas_hq_ecu_fx(
    4667             :     const Word16 *prevsynth,          /* i  : buffer of previously synthesized signal Q0     */
    4668             :     Word32 *ecu_rec,                  /* o  : reconstructed frame in tda domain           */
    4669             :     Word16 *time_offs,                /* i/o: Sample offset for consecutive frame losses Q0  */
    4670             :     Word16 *X_sav,                    /* i/o: Stored spectrum of prototype frame          */
    4671             :     Word16 *Q_spec,                   /* i/o: Q value of stored spectrum                  */
    4672             :     Word16 *num_p,                    /* i/o: Number of identified peaks Q0                  */
    4673             :     Word16 *plocs,                    /* i/o: Peak locations Q0                             */
    4674             :     Word32 *plocsi,                   /* i/o: Interpolated peak locations             Q16 */
    4675             :     const Word16 env_stab,            /* i  : Envelope stability parameter                */
    4676             :     Word16 *last_fec,                 /* i/o: Flag for usage of pitch dependent ECU       */
    4677             :     const Word16 ph_ecu_HqVoicing,    /* i  : HQ Voicing flag                             */
    4678             :     Word16 *ph_ecu_active,            /* i  : Phase ECU active flag                       */
    4679             :     Word16 *gapsynth,                 /* o  : Gap synthesis                               */
    4680             :     const Word16 prev_bfi,            /* i  : indicating burst frame error                */
    4681             :     const Word16 old_is_transient[2], /* i  : flags indicating previous transient frames  */
    4682             :     Word16 *mag_chg_1st,              /* i/o: per band magnitude modifier for transients  */
    4683             :     Word16 *Xavg,                     /* i/o: Frequency group average gain to fade to Q0    */
    4684             :     Word16 *beta_mute,                /* o   : Factor for long-term mute Q15                  */
    4685             :     const Word16 output_frame,        /* i   : frame length                               */
    4686             :     Decoder_State *st_fx              /* i/o: decoder state structure                     */
    4687             : )
    4688             : {
    4689             :     Word16 N;
    4690             :     Word16 decimatefactor;
    4691             :     Word16 corr; /*Q15 */
    4692             :     Word16 prevsynth_LP[2 * L_FRAME8k];
    4693             :     HQ_DEC_HANDLE hHQ_core;
    4694             :     const Word16 *fec_alg_input;
    4695             :     Word16 evs_mode_selection;
    4696             :     Word16 ivas_mode_selection;
    4697             : 
    4698         133 :     hHQ_core = st_fx->hHQ_core;
    4699         133 :     corr = 0;
    4700         133 :     move16();
    4701         133 :     IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
    4702             :     {
    4703           0 :         fec_alg_input = prevsynth + NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), ACELP_LOOK_NS / 2 - PH_ECU_LOOKAHEAD_NS );
    4704             :     }
    4705             :     ELSE
    4706             :     {
    4707         133 :         fec_alg_input = prevsynth - NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), PH_ECU_LOOKAHEAD_NS );
    4708             :     }
    4709             :     /* init (values ar changed after) */
    4710             : 
    4711             : 
    4712             :     /* find pitch and R value */
    4713             : 
    4714         133 :     IF( !( LT_16( output_frame, L_FRAME16k ) ) )
    4715             :     {
    4716         133 :         fec_ecu_pitch_ivas_fx( fec_alg_input, prevsynth_LP, output_frame, &N, &corr, &decimatefactor, ph_ecu_HqVoicing );
    4717             :     }
    4718             :     ELSE
    4719             :     {
    4720           0 :         corr = 0;
    4721           0 :         decimatefactor = 4;
    4722           0 :         move16();
    4723           0 :         N = shr( output_frame, 2 );
    4724           0 :         move16(); /* just to avoid using uninitialized value in if statement below */
    4725             :     }
    4726             : 
    4727         133 :     test();
    4728         133 :     test();
    4729         133 :     test();
    4730         133 :     test();
    4731         133 :     test();
    4732         133 :     test();
    4733         133 :     test();
    4734         133 :     test();
    4735         133 :     test();
    4736         133 :     test();
    4737         133 :     test();
    4738         133 :     test();
    4739         133 :     test();
    4740         133 :     test();
    4741         133 :     test();
    4742             : 
    4743         133 :     evs_mode_selection = ( GE_32( st_fx->total_brate, 48000 ) && ( GE_16( output_frame, L_FRAME16k ) && !prev_bfi && ( !old_is_transient[0] || old_is_transient[1] ) &&
    4744         399 :                                                                    ( ph_ecu_HqVoicing || ( ( ( GT_16( hHQ_core->env_stab_plc_fx, 16384 ) /* 0.5 in Q15 */ ) && ( LT_16( corr, 19661 ) /* 0.6 in Q15 */ ) ) || ( LT_16( hHQ_core->env_stab_plc_fx, 16384 ) /* 0.5 in Q15 */ && ( GT_16( corr, 27853 ) /* 0.85 in Q15 */ ) ) ) ) ) ) ||
    4745         266 :                          ( LT_32( st_fx->total_brate, 48000 ) && ( ( ph_ecu_HqVoicing || GT_16( corr, 27853 ) /* 0.85 in Q15 */ ) && !prev_bfi && ( !old_is_transient[0] || old_is_transient[1] ) ) );
    4746             : 
    4747         133 :     test();
    4748         133 :     ivas_mode_selection = ( LT_16( N, PH_ECU_N_LIMIT ) ) || ( LT_16( corr, PH_ECU_CORR_LIMIT_Q15 ) );
    4749         133 :     test();
    4750         133 :     test();
    4751         133 :     test();
    4752         133 :     test();
    4753         133 :     IF( ( ( st_fx->element_mode == EVS_MONO ) && evs_mode_selection ) ||
    4754             :         ( ( st_fx->element_mode != EVS_MONO ) && evs_mode_selection && ivas_mode_selection ) )
    4755             :     {
    4756           1 :         ivas_fec_alg_fx( fec_alg_input, prevsynth_LP, &st_fx->hHQ_core->ni_seed_forfec, ecu_rec, output_frame, N, decimatefactor, ph_ecu_HqVoicing, gapsynth, st_fx->element_mode, st_fx->hHQ_core->old_out_fx, st_fx->hHQ_core->Q_old_wtda );
    4757           1 :         *last_fec = 1;
    4758           1 :         move16();
    4759           1 :         *ph_ecu_active = 0;
    4760           1 :         move16();
    4761           1 :         *time_offs = output_frame;
    4762           1 :         move16();
    4763             :     }
    4764             :     ELSE
    4765             :     {
    4766         132 :         ivas_hq_phase_ecu_fx( prevsynth - NS2SA_FX2( L_mult0( output_frame, FRAMES_PER_SEC ), PH_ECU_LOOKAHEAD_NS ), ecu_rec, time_offs, X_sav, Q_spec, num_p, plocs, plocsi,
    4767             :                               env_stab, last_fec, ph_ecu_active, prev_bfi, old_is_transient,
    4768         132 :                               mag_chg_1st, Xavg, beta_mute, st_fx->bwidth, output_frame, corr, st_fx->element_mode );
    4769             : 
    4770         132 :         *last_fec = 0;
    4771         132 :         move16();
    4772         132 :         *ph_ecu_active = 1;
    4773         132 :         move16();
    4774             :     }
    4775         133 :     return;
    4776             : }
    4777             : 
    4778           0 : void hq_ecu_fx(
    4779             :     const Word16 *prevsynth,          /* i  : buffer of previously synthesized signal Q0     */
    4780             :     Word32 *ecu_rec,                  /* o  : reconstructed frame in tda domain           */
    4781             :     Word16 *time_offs,                /* i/o: Sample offset for consecutive frame losses Q0  */
    4782             :     Word16 *X_sav,                    /* i/o: Stored spectrum of prototype frame          */
    4783             :     Word16 *Q_spec,                   /* i/o: Q value of stored spectrum                  */
    4784             :     Word16 *num_p,                    /* i/o: Number of identified peaks Q0                 */
    4785             :     Word16 *plocs,                    /* i/o: Peak locations Q0                             */
    4786             :     Word32 *plocsi,                   /* i/o: Interpolated peak locations             Q16 */
    4787             :     const Word16 env_stab,            /* i  : Envelope stability parameter                */
    4788             :     Word16 *last_fec,                 /* i/o: Flag for usage of pitch dependent ECU       */
    4789             :     const Word16 ph_ecu_HqVoicing,    /* i  : HQ Voicing flag                             */
    4790             :     Word16 *ph_ecu_active,            /* i  : Phase ECU active flag                       */
    4791             :     Word16 *gapsynth,                 /* o  : Gap synthesis                               */
    4792             :     const Word16 prev_bfi,            /* i  : indicating burst frame error                */
    4793             :     const Word16 old_is_transient[2], /* i  : flags indicating previous transient frames  */
    4794             :     Word16 *mag_chg_1st,              /* i/o: per band magnitude modifier for transients  */
    4795             :     Word16 *Xavg,                     /* i/o: Frequency group average gain to fade to Q0    */
    4796             :     Word16 *beta_mute,                /* o   : Factor for long-term mute Q15                 */
    4797             :     const Word16 output_frame,        /* i   : frame length                               */
    4798             :     Decoder_State *st_fx              /* i/o: decoder state structure                     */
    4799             : )
    4800             : {
    4801             :     Word16 N;
    4802             :     Word16 decimatefactor;
    4803             :     Word16 corr; /*Q15 */
    4804             :     Word16 prevsynth_LP[2 * L_FRAME8k];
    4805             :     /* init (values ar changed after) */
    4806           0 :     decimatefactor = 4;
    4807           0 :     move16();
    4808           0 :     N = shr( output_frame, 2 );
    4809             : 
    4810             : 
    4811             :     /* find pitch and R value */
    4812             : 
    4813           0 :     IF( !( LT_16( output_frame, L_FRAME16k ) ) )
    4814             :     {
    4815           0 :         fec_ecu_pitch_fx( prevsynth + NS2SA_FX2( L_mult0( output_frame, 50 ), ACELP_LOOK_NS / 2 - PH_ECU_LOOKAHEAD_NS ), prevsynth_LP, output_frame, &N, &corr, &decimatefactor, ph_ecu_HqVoicing );
    4816             :     }
    4817             :     ELSE
    4818             :     {
    4819           0 :         corr = 0;
    4820           0 :         move16(); /* just to avoid using uninitialized value in if statement below */
    4821             :     }
    4822             : 
    4823           0 :     test();
    4824           0 :     test();
    4825           0 :     test();
    4826           0 :     test();
    4827           0 :     test();
    4828           0 :     test();
    4829           0 :     test();
    4830           0 :     test();
    4831           0 :     test();
    4832           0 :     test();
    4833           0 :     test();
    4834           0 :     test();
    4835           0 :     test();
    4836           0 :     test();
    4837           0 :     test();
    4838           0 :     IF( ( GE_32( st_fx->total_brate, 48000 ) &&
    4839             :           ( GE_16( output_frame, L_FRAME16k ) && !prev_bfi && ( !old_is_transient[0] || old_is_transient[1] ) && ( NE_16( ph_ecu_HqVoicing, 0 ) || ( ( ( NE_16( st_fx->hHQ_core->env_stab_plc_fx, 0 ) ) && ( LT_16( corr, 19661 ) ) ) || ( !( NE_16( st_fx->hHQ_core->env_stab_plc_fx, 0 ) ) && ( GT_16( corr, 27853 ) ) ) ) ) ) ) ||
    4840             :         ( LT_32( st_fx->total_brate, 48000 ) && ( ( ph_ecu_HqVoicing || GT_16( corr, 27853 ) ) && !prev_bfi && ( !old_is_transient[0] || old_is_transient[1] ) ) ) )
    4841             :     {
    4842             : 
    4843           0 :         fec_alg_fx( prevsynth + NS2SA_FX2( L_mult0( output_frame, 50 ), ACELP_LOOK_NS / 2 - PH_ECU_LOOKAHEAD_NS ), prevsynth_LP, &st_fx->hHQ_core->ni_seed_forfec, ecu_rec, output_frame, N, decimatefactor, ph_ecu_HqVoicing, gapsynth );
    4844           0 :         *last_fec = 1;
    4845           0 :         move16();
    4846           0 :         *ph_ecu_active = 0;
    4847           0 :         move16();
    4848           0 :         *time_offs = output_frame;
    4849           0 :         move16();
    4850             :     }
    4851             :     ELSE
    4852             :     {
    4853           0 :         hq_phase_ecu_fx( prevsynth, ecu_rec, time_offs, X_sav, Q_spec, num_p, plocs, plocsi,
    4854             :                          env_stab, last_fec, ph_ecu_active, prev_bfi, old_is_transient,
    4855           0 :                          mag_chg_1st, Xavg, beta_mute, st_fx->bwidth, output_frame );
    4856             :     }
    4857           0 :     return;
    4858             : }
    4859             : 
    4860             : /*******************************************************************************
    4861             :  * The square root of x which MUST be 0.5 <= x < 1, i.e., x must be normalized.
    4862             :  * sqrt(x) is approximated by a polynomial of degree n.
    4863             :  *
    4864             :  * sqrt(x) = a0 x^n + a1 x^(n-1) + a2 x^(n-2) + ... + an
    4865             :  *         = (...((a0 x + a1) x + a2) x + ...) x + an
    4866             :  *
    4867             :  * The coefficients can be readily obtained by the following open source Octave
    4868             :  * (or commercial Matlab) script:
    4869             :  * order = 2;
    4870             :  * N = 400;
    4871             :  * x = linspace(0.5, 1.0, N);
    4872             :  * y = sqrt(x);
    4873             :  * p = polyfit(x, y, order)
    4874             :  * z = polyval(p, x);
    4875             :  * err = y - z;
    4876             :  * plot(err);
    4877             :  ******************************************************************************/
    4878             : 
    4879       28581 : static Word16 sqrt2ndOrder(                /* o: in Q15 (2nd order least square approx.) */
    4880             :                             const Word16 x /* i: x must be in between 0.5 and 1.0 (Q15). */
    4881             : )
    4882             : {
    4883             :     Word32 acc;
    4884             :     Word16 z;
    4885             : 
    4886       28581 :     acc = 1890205600L; /*  0.880195572812922 in Q31 */
    4887       28581 :     move32();
    4888       28581 :     z = mac_r( acc, x, -6506 ); /* -0.198537395405340 in Q15 */
    4889       28581 :     acc = 682030261L;           /*  0.317595089462249 in Q31 */
    4890       28581 :     move32();
    4891       28581 :     z = mac_r( acc, z, x ); /* in Q15 */
    4892       28581 :     return z;
    4893             : }
    4894             : 
    4895             : /*-----------------------------------------------------------------------------
    4896             :  * windowing()
    4897             :  *
    4898             :  * Apply a symmetric Hamming or Hamming-Rectangular window to the signal.
    4899             :  * If the "rectLength" parameter is zero, it is Hamming window; otherwise, the
    4900             :  * rectLength signifies the length of the rectangular part of the Hamming-Rectangular
    4901             :  * window.
    4902             :  *--------------------------------------------------------------------------- */
    4903         108 : static void windowing(
    4904             :     const Word16 *x,         /* i: Input signal Qx*/
    4905             :     Word16 *y,               /* o: Windowed output Qx*/
    4906             :     const Word16 *win,       /* i: Window coefficients Q15*/
    4907             :     const Word16 rectLength, /* i: Offset in between the 1st and 2nd symmetric halves of the Hamming window */
    4908             :     const Word16 halfLength  /* i: Half of the total length of a complete Hamming window. */
    4909             : )
    4910             : {
    4911             :     Word16 i;
    4912             :     Word16 *pY;
    4913             :     const Word16 *pX, *pW;
    4914         108 :     pX = x;
    4915         108 :     pW = win;
    4916         108 :     pY = y;
    4917       17132 :     FOR( i = 0; i < halfLength; i++ ) /* 1st symmetric half of the Hamming window */
    4918             :     {
    4919       17024 :         *pY++ = mult_r( *pX++, *pW++ );
    4920       17024 :         move16();
    4921             :     }
    4922         108 :     FOR( i = 0; i < rectLength; i++ ) /* If rectLength is zero, it's a pure Hamming window; otherwise Hamming-Rectangular. */
    4923             :     {
    4924           0 :         *pY++ = *pX++;
    4925           0 :         move16();
    4926             :     }
    4927       17132 :     FOR( i = 0; i < halfLength; i++ ) /* 2nd symmetric half of the Hamming window. */
    4928             :     {
    4929       17024 :         *pY++ = mult_r( *pX++, *( --pW ) );
    4930       17024 :         move16();
    4931             :     }
    4932         108 : }
    4933             : 
    4934             : /*-----------------------------------------------------------------------------
    4935             :  * windowing_ROM_optimized()
    4936             :  *
    4937             :  * The coefficients of the Hamming window are derived from the sine table
    4938             :  * shared with fft3_fx().
    4939             :  * The entire Hamming-Rectangular window is decomposed into 5 segments:
    4940             :  *   1. 1st half of the left half of the Hamming window
    4941             :  *   2. 2nd half of the left half of the Hamming window
    4942             :  *   3. The flat part of the rectangular region
    4943             :  *   4. 1st half of the right half of the Hamming window
    4944             :  *   5. 2nd half of the right half of the Hamming window
    4945             :  *----------------------------------------------------------------------------*/
    4946           0 : static void windowing_ROM_optimized(
    4947             :     const Word16 *x,          /* i: Input signal Qin*/
    4948             :     Word16 *y,                /* o: Windowed output Qin*/
    4949             :     const Word16 downSamples, /* i: Offset in accessing the sine table. */
    4950             :     const Word16 rectLength,  /* i: Length of the rectangular portion (excluding the Hamming window part) */
    4951             :     const Word16 halfLength   /* i: Half of the total length of the Hamming (excluding rectangular part) window */
    4952             : )
    4953             : {
    4954             :     Word16 i, hamm, quarterLen, initOffset;
    4955             :     Word16 *pY;
    4956             :     const Word16 *pX, *pSine;
    4957             :     Word32 acc;
    4958             : 
    4959           0 :     quarterLen = shr( halfLength, 1 ); /* 1/4 length of the entire Hamming (excluding the rectangular part) window. */
    4960           0 :     initOffset = add( T_SIN_PI_2, shr( downSamples, 1 ) );
    4961           0 :     pSine = sincos_t_rad3_fx + initOffset; // Q15
    4962           0 :     pX = x;
    4963           0 :     pY = y;
    4964             : 
    4965             :     /* 1st half of the left half of the Hamming window. */
    4966           0 :     FOR( i = 0; i < quarterLen; i++ )
    4967             :     {
    4968           0 :         pSine -= downSamples;                       /* Decrement address counter */
    4969           0 :         acc = L_deposit_h( FEC_HQ_WIN_A0 );         /* Derive the Hamming window coefficient from the sine table. Q31*/
    4970           0 :         hamm = msu_r( acc, *pSine, FEC_HQ_WIN_A1 ); // Q15
    4971           0 :         *pY++ = mult_r( hamm, *pX++ );              // Qin
    4972           0 :         move16();
    4973             :     }
    4974             : 
    4975             :     /* 2nd half of the left half of the Hamming window. */
    4976           0 :     FOR( i = 0; i < quarterLen; i++ )
    4977             :     {
    4978           0 :         acc = L_deposit_h( FEC_HQ_WIN_A0 );
    4979           0 :         hamm = mac_r_sat( acc, *pSine, FEC_HQ_WIN_A1 );
    4980           0 :         *pY++ = mult_r( hamm, *pX++ );
    4981           0 :         move16();
    4982           0 :         pSine += downSamples; /* Increment address counter */
    4983             :     }
    4984             : 
    4985             :     /* The rectangular flat region */
    4986           0 :     FOR( i = 0; i < rectLength; i++ )
    4987             :     {
    4988           0 :         *pY++ = *pX++;
    4989           0 :         move16();
    4990             :     }
    4991             : 
    4992             :     /* 1st half of the right half of the Hamming window. */
    4993           0 :     FOR( i = 0; i < quarterLen; i++ )
    4994             :     {
    4995           0 :         pSine -= downSamples;                           /* Decrement address counter */
    4996           0 :         acc = L_deposit_h( FEC_HQ_WIN_A0 );             // Q31
    4997           0 :         hamm = mac_r_sat( acc, *pSine, FEC_HQ_WIN_A1 ); // Q15
    4998           0 :         *pY++ = mult_r( hamm, *pX++ );
    4999           0 :         move16();
    5000             :     }
    5001             : 
    5002             :     /* 2nd half of the right half of the Hamming window. */
    5003           0 :     FOR( i = 0; i < quarterLen; i++ )
    5004             :     {
    5005           0 :         acc = L_deposit_h( FEC_HQ_WIN_A0 );
    5006           0 :         hamm = msu_r( acc, *pSine, FEC_HQ_WIN_A1 );
    5007           0 :         *pY++ = mult_r( hamm, *pX++ );
    5008           0 :         move16();
    5009           0 :         pSine += downSamples; /* Increment address counter */
    5010             :     }
    5011           0 : }

Generated by: LCOV version 1.14