LCOV - code coverage report
Current view: top level - lib_dec - FEC_HQ_phase_ecu_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 1369 2578 53.1 %
Date: 2025-05-03 01:55:50 Functions: 22 36 61.1 %

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

Generated by: LCOV version 1.14