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