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

Generated by: LCOV version 1.14