LCOV - code coverage report
Current view: top level - lib_dec - hf_synth_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 574a190e3c6896c6c4ed10d7f23649709a0c4347 Lines: 143 986 14.5 %
Date: 2025-06-27 02:59:36 Functions: 6 14 42.9 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : #include <stdint.h>
       5             : #include "options.h" /* Compilation switches                   */
       6             : #include "cnst.h"    /* Common constants                       */
       7             : #include "prot_fx.h" /* Function prototypes                    */
       8             : #include "rom_com.h" /* Static table prototypes                */
       9             : #include "basop32.h"
      10             : 
      11             : /*---------------------------------------------------------------------*
      12             :  * Local constants
      13             :  *---------------------------------------------------------------------*/
      14             : 
      15             : #define L_SUBFR48k 240
      16             : #define L_SUBFR32k 160
      17             : 
      18             : /*---------------------------------------------------------------------*
      19             :  * Local function prototypes
      20             :  *---------------------------------------------------------------------*/
      21             : 
      22             : static void filt_6k_7k_scale_fx( Word16 signal[], Word16 lg, Word16 mem[], Word16 fact, Word16 exp );
      23             : static void hf_synthesis_fx( ZERO_BWE_DEC_HANDLE hBWE_zero, const Word32 core_brate, const Word16 output_subfr, const Word16 Aq[], const Word16 exc[], const Word16 Q_exc, Word16 synth[], Word16 synth16k[], const Word16 Q_syn );
      24             : static void hf_synthesis_ivas_fx( ZERO_BWE_DEC_HANDLE hBWE_zero, const Word32 core_brate, const Word16 output_subfr, const Word16 Aq[], const Word16 exc[], const Word16 Q_exc, Word16 synth[], Word16 synth16k[], const Word16 Q_syn );
      25             : static void hf_synthesis_amr_wb_fx( const Word32 core_brate, const Word16 output_subfr, const Word16 Ap[], Word16 exc16k[], Word16 synth_out[], Word16 *mem_syn_hf, Word16 *delay_syn_hf, Word16 *mem_hp_interp, Word16 p_r, Word16 HF_corr_gain, Word16 til, Word16 voice_factors, const Word16 exc[], const Word16 Q_exc, const Word16 Q_out, Word16 qhf );
      26             : static void envelope_fx( AMRWB_IO_DEC_HANDLE hAmrwb_IO, const Word32 core_brate, const Word16 Aq[], Word16 Ap[], Word16 *r, Word16 tilt0, Word16 tilt, Word16 voice_factor );
      27             : static void AdaptiveStartBand_fx( Word16 *start_band, const Word32 rate, const Word16 *lsf, const Word16 voicing_fac, const Word16 clas, Word16 *voicing_flag, Word16 *start_band_old, Word32 *OptCrit_old );
      28             : 
      29             : 
      30             : /*-------------------------------------------------------------------*
      31             :  * hf_synth_init()
      32             :  *
      33             :  * hf synthesis filters initialization
      34             :  * - initialization of 400 Hz high pass filter
      35             :  * - initialization of band pass 6kHz to 7kHz FIR filter
      36             :  *-------------------------------------------------------------------*/
      37             : 
      38        3256 : void hf_synth_init_fx(
      39             :     ZERO_BWE_DEC_HANDLE hBWE_zero /* o  : zero BWE decoder handle */
      40             : )
      41             : {
      42        3256 :     hBWE_zero->seed2 = RANDOM_INITSEED;
      43        3256 :     move16();
      44        3256 :     set16_fx( hBWE_zero->mem_hf_fx, 0, ( L_FIR - 1 ) );
      45        3256 :     set16_fx( hBWE_zero->mem_syn_hf_fx, 0, M );
      46        3256 :     set16_fx( hBWE_zero->mem_hp400_fx, 0, 4 );
      47        3256 :     set16_fx( hBWE_zero->delay_syn_hf_fx, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
      48        3256 :     set16_fx( hBWE_zero->mem_hp_interp_fx, 0, INTERP_3_1_MEM_LEN );
      49        3256 :     hBWE_zero->memExp1 = 0;
      50        3256 :     move16();
      51        3256 :     return;
      52             : }
      53      350067 : void hf_synth_reset_fx(
      54             :     ZERO_BWE_DEC_HANDLE hBWE_zero /* o  : zero BWE decoder handle */
      55             : )
      56             : {
      57             :     Word16 i;
      58             : 
      59   112371507 :     FOR( i = 0; i < L_FRAME16k; i++ )
      60             :     {
      61   112021440 :         Random( &hBWE_zero->seed2 );
      62             :     }
      63             : 
      64      350067 :     set16_fx( hBWE_zero->mem_hf_fx, 0, ( L_FIR - 1 ) );
      65      350067 :     set16_fx( hBWE_zero->mem_syn_hf_fx, 0, M );
      66      350067 :     set16_fx( hBWE_zero->mem_hp400_fx, 0, 4 );
      67             : 
      68      350067 :     set16_fx( hBWE_zero->delay_syn_hf_fx, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
      69      350067 :     set16_fx( hBWE_zero->mem_hp_interp_fx, 0, INTERP_3_1_MEM_LEN );
      70      350067 :     hBWE_zero->memExp1 = 0;
      71      350067 :     move16();
      72      350067 :     return;
      73             : }
      74             : 
      75             : /*---------------------------------------------------------------------*
      76             :  * hf_synth()
      77             :  *
      78             :  * High frequency regeneration
      79             :  *---------------------------------------------------------------------*/
      80             : 
      81           0 : void hf_synth_fx(
      82             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* i/o: handle to 0 bit BWE parameters          */
      83             :     const Word32 core_brate,       /* i  : core bitrate                                          Q0*/
      84             :     const Word16 output_frame,     /* i  : output frame length               Q0*/
      85             :     const Word16 *Aq,              /* i  : quantized Az                                         Q12*/
      86             :     const Word16 *exc,             /* i  : excitation at 12.8 kHz                 Q_exc*/
      87             :     Word16 *synth,                 /* i  : 12.8kHz synthesis signal              Q_syn2*/
      88             :     Word16 *synth16k,              /* o  : 16kHz synthesis signal        Q_syn2*/
      89             :     const Word16 Q_exc,            /* i  : excitation scaling             */
      90             :     const Word16 Q_syn2            /* i  : synthesis scaling              */
      91             : )
      92             : {
      93             :     const Word16 *p_Aq;
      94             :     Word16 i_subfr, output_subfr;
      95             : 
      96           0 :     output_subfr = output_frame / NB_SUBFR;
      97           0 :     move16();
      98             : 
      99           0 :     p_Aq = Aq; /* Q12 */
     100           0 :     FOR( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
     101             :     {
     102           0 :         hf_synthesis_fx( hBWE_zero, core_brate, output_subfr, p_Aq, &exc[i_subfr], Q_exc, &synth[i_subfr], &synth16k[imult3216( (Word32) i_subfr, output_subfr ) / L_SUBFR], Q_syn2 );
     103             : 
     104           0 :         p_Aq += ( M + 1 ); /* Q12 */
     105             :     }
     106             : 
     107           0 :     return;
     108             : }
     109             : 
     110             : /*-----------------------------------------------------------------------------------*
     111             :  * hf_synthesis()
     112             :  *
     113             :  * HF noise synthesis
     114             :  * - Generate HF noise between 6 and 7 kHz.
     115             :  * - Set energy of noise according to synthesis tilt.
     116             :  *     tilt > 0.8 ==> - 14 dB (voiced)
     117             :  *     tilt   0.5 ==> - 6 dB  (voiced or noise)
     118             :  *     tilt < 0.0 ==>   0 dB  (noise)
     119             :  *-----------------------------------------------------------------------------------*/
     120             : 
     121           0 : static void hf_synthesis_fx(
     122             :     ZERO_BWE_DEC_HANDLE hBWE_zero,
     123             :     const Word32 core_brate,   /* i  : core bitrate                                Q0*/
     124             :     const Word16 output_subfr, /* i  : output sub-frame length                     Q0*/
     125             :     const Word16 Aq[],         /* i  : quantized Az                                                               Q12*/
     126             :     const Word16 exc[],        /* i  : excitation at 12.8 kHz                                   Q_exc*/
     127             :     const Word16 Q_exc,        /* i  : excitation scaling                                    */
     128             :     Word16 synth[],            /* i  : 12.8kHz synthesis signal                                 Q_syn*/
     129             :     Word16 synth16k[],         /* i/o: 16kHz synthesis signal                                   Q_syn*/
     130             :     const Word16 Q_syn         /* i  : synthesis scaling                                     */
     131             : )
     132             : {
     133             :     Word16 i, s;
     134             :     Word16 HF_syn[L_SUBFR16k], upsampled_HF_syn[L_FRAME48k / NB_SUBFR];
     135             :     Word16 HF_exc[L_SUBFR16k];
     136             :     Word16 tmp, ener, exp1, exp2, scale;
     137             :     Word32 L_tmp;
     138             :     Word16 Ap[M16k + 1];
     139             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     140           0 :     Flag Overflow = 0;
     141           0 :     move32();
     142             : #endif
     143             : 
     144             :     /*-----------------------------------------------------------------*
     145             :      * generate white noise vector
     146             :      *-----------------------------------------------------------------*/
     147             : 
     148           0 :     Random_Fill( &hBWE_zero->seed2, L_SUBFR16k, HF_exc, 3 ); /* 3 = Shift Right by 3 */
     149             : 
     150             :     /* o: HF_exc in Q-3 */
     151             : 
     152             :     /*-----------------------------------------------------------------*
     153             :      * calculate energy scaling factor so that white noise would have the
     154             :      * same energy as exc12k8
     155             :      *-----------------------------------------------------------------*/
     156             : 
     157             :     /*ener = sum2_f( exc, L_SUBFR ) + 0.01f*/
     158           0 :     ener = extract_h( Dot_product12( exc, exc, L_SUBFR, &exp2 ) ); /* Q15 */
     159           0 :     exp2 = sub( exp2, add( Q_exc, Q_exc ) );
     160             : 
     161             :     /*tmp = round_fx(Dot_product12(HF_exc, HF_exc, output_subfr, &exp1)); */
     162           0 :     L_tmp = Dot_product12( HF_exc, HF_exc, L_SUBFR16k, &exp1 );
     163           0 :     tmp = round_fx( L_tmp ); /* Q15 */
     164             :     /* tmp = (float)(sqrt(ener/tmp)) */
     165             :     /* scale is -1 if tmp > ener */
     166           0 :     scale = shr( sub( ener, tmp ), 15 ); /* Q0 */
     167           0 :     tmp = shl( tmp, scale );             /* Q15 + scale*/
     168           0 :     exp1 = sub( exp1, scale );
     169             : 
     170           0 :     tmp = div_s( tmp, ener ); /* Q15 */
     171           0 :     exp1 = sub( exp1, exp2 );
     172             : 
     173           0 :     L_tmp = L_deposit_h( tmp ); /* Q31 */
     174             : 
     175           0 :     L_tmp = Isqrt_lc( L_tmp, &exp1 );
     176           0 :     scale = round_fx( L_tmp ); /* Q18 when Q_exc=-1, HF_exc in Q-3 */
     177             : 
     178           0 :     exp2 = sub( hBWE_zero->memExp1, exp1 );
     179           0 :     hBWE_zero->memExp1 = exp1;
     180           0 :     move16();
     181             : 
     182             :     /*-----------------------------------------------------------------*
     183             :      * calculate energy scaling factor to respect tilt of synth12k8
     184             :      * (tilt: 1=voiced, -1=unvoiced)
     185             :      *-----------------------------------------------------------------*/
     186           0 :     hp400_12k8_fx( synth, L_SUBFR, hBWE_zero->mem_hp400_fx );
     187             :     /* i: mem_hp400 in Q_syn */
     188             :     /* i: synth in Q_syn */
     189             :     /* o: synth in Q_syn-3 */
     190             : 
     191           0 :     L_tmp = L_mac( 1L, synth[0], synth[0] ); /* 2*(Q_syn-3)+1 */
     192           0 :     FOR( i = 1; i < L_SUBFR; i++ )
     193             :     {
     194           0 :         L_tmp = L_mac_sat( L_tmp, synth[i], synth[i] ); /* 2*(Q_syn-3)+1 */
     195             :     }
     196           0 :     tmp = norm_l( L_tmp );
     197           0 :     ener = extract_h( L_shl( L_tmp, tmp ) ); /* ener = r[0] */
     198             :     /*ener in Q = 2*(Q_syn-3)+1 = Q-5 when Q_syn=0*/
     199             : 
     200           0 :     L_tmp = L_mac( 1L, synth[1], synth[0] ); /* 2*(Q_syn-3)+1 */
     201           0 :     FOR( i = 2; i < L_SUBFR; i++ )
     202             :     {
     203           0 :         L_tmp = L_mac_sat( L_tmp, synth[i], synth[i - 1] ); /* 2*(Q_syn-3)+1 */
     204             :     }
     205           0 :     tmp = extract_h( L_shl( L_tmp, tmp ) ); /* tmp = r[1] */
     206             :     /*tmp in Q = 2*(Q_syn-3)+1 = Q-5 when Q_syn=0 */
     207             :     /*we use the same normalization factor for both ener and tmp, */
     208             :     /*if the headroom in "tmp" is less than "ener", tmp can saturate */
     209             :     /*but this is ok since below we apply some thresholds to tmp */
     210             : 
     211           0 :     tmp = s_max( 0, tmp );
     212           0 :     if ( tmp > 0 )
     213             :     {
     214           0 :         tmp = div_s( tmp, ener ); /* Q15 */
     215             :     }
     216             : 
     217             :     /*-----------------------------------------------------------------*
     218             :      * modify energy of white noise according to synthesis tilt
     219             :      *-----------------------------------------------------------------*/
     220             : 
     221             :     /* tmp = 1.0 - fac */
     222           0 :     tmp = add_o( 1, sub( 32767 /* 1 in Q15 */, tmp ), &Overflow );
     223           0 :     test();
     224           0 :     if ( core_brate == FRAME_NO_DATA || EQ_32( core_brate, SID_2k40 ) )
     225             :     {
     226             :         /* emphasize HF noise in CNG */
     227             :         /*fac *= 2.0f;*/
     228           0 :         tmp = add_o( tmp, tmp, &Overflow ); /* Q15 */
     229             :     }
     230           0 :     tmp = s_max( tmp, 3277 ); /* 0.1 in Q15 */
     231             : 
     232             :     /*scale *= fac;*/
     233           0 :     tmp = mult_r( scale, tmp );
     234             :     /*-----------------------------------------------------------------*
     235             :      * modify HF excitation according to both calculated scaling factors
     236             :      * high pass filtering (0.94ms of delay)
     237             :      *-----------------------------------------------------------------*/
     238             : 
     239           0 :     filt_6k_7k_scale_fx( HF_exc, L_SUBFR16k, hBWE_zero->mem_hf_fx, tmp, exp2 );
     240             :     /* i: input HF_exc is scaled in float, here scaling is done inside this filter */
     241             :     /* i: mem_hf in Q-2 */
     242             :     /* o: HF_exc in Q0 */
     243             : 
     244             :     /*-----------------------------------------------------------------*
     245             :      * synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz
     246             :      *-----------------------------------------------------------------*/
     247             : 
     248             :     /*weight_a( Aq, Ap, 0.6f, M );*/
     249           0 :     weight_a_lc_fx( Aq, Ap, Gamma_19661_Tbl_fx, M );
     250             :     /* o: Ap in Q14 */
     251             : 
     252           0 :     Syn_filt_s( 0, Ap, M, HF_exc, HF_syn, L_SUBFR16k, hBWE_zero->mem_syn_hf_fx, 1 );
     253             :     /* i: Ap in Q14 */
     254             :     /* i: HF_exc in Q0 */
     255             :     /* o: HF_syn in Q0 */
     256             :     /* i/o: mem_syn_hf in Q0 */
     257             : 
     258           0 :     Scale_sig( HF_syn, L_SUBFR16k, ( add( Q_syn, exp1 ) ) ); /* bring HF_syn to (Q_syn+exp1) */
     259             : 
     260             :     /*-----------------------------------------------------------------*
     261             :      * add filtered HF noise to speech synthesis
     262             :      *-----------------------------------------------------------------*/
     263             : 
     264             :     /* delay by 5 samples @16kHz to compensate CLDFB resampling delay (20samples) and HP filtering delay (roughly 15 samples) */
     265           0 :     delay_signal_fx( HF_syn, L_SUBFR16k, hBWE_zero->delay_syn_hf_fx, NS2SA_FX2( 16000, DELAY_CLDFB_NS ) - 15 );
     266             : 
     267             :     /* interpolate the HF synthesis */
     268           0 :     IF( EQ_16( output_subfr, L_SUBFR48k ) ) /* 48kHz sampled output */
     269             :     {
     270           0 :         s = s_max( s_min( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN - 3 ) ), 3 ),
     271           0 :                           sub( Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx + INTERP_3_1_MEM_LEN - 3, 3 ), 1 ) ),
     272             :                    0 );
     273           0 :         Scale_sig( HF_syn, L_SUBFR16k, s );                              /* Q_syn+exp1+s */
     274           0 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN, s ); /* Qx + s */
     275           0 :         interpolate_3_over_1_allpass_fx( HF_syn, L_SUBFR16k, upsampled_HF_syn, hBWE_zero->mem_hp_interp_fx );
     276           0 :         Scale_sig( upsampled_HF_syn, 3 * L_SUBFR16k, -s );                /* Q_syn + exp1 + s */
     277           0 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN, -s ); /* Qx */
     278           0 :         Scale_sig( HF_syn, L_SUBFR16k, -s );                              /* Q_syn+exp1 */
     279           0 :         Scale_sig( upsampled_HF_syn, L_SUBFR48k, -1 );
     280             :     }
     281           0 :     ELSE IF( EQ_16( output_subfr, L_SUBFR32k ) ) /* 32kHz sampled output */
     282             :     {
     283           0 :         s = s_max( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP ) ), 2 ), 0 );
     284           0 :         Scale_sig( HF_syn, L_SUBFR16k, s );                                     /* Q_syn+exp1+s */
     285           0 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP, s ); /* Qx + s */
     286           0 :         Interpolate_allpass_steep_fx( HF_syn, hBWE_zero->mem_hp_interp_fx, L_SUBFR16k, upsampled_HF_syn );
     287           0 :         Scale_sig( upsampled_HF_syn, 2 * L_SUBFR16k, -s );                       /* Q_syn + exp1 */
     288           0 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP, -s ); /* Qx */
     289           0 :         Scale_sig( HF_syn, L_SUBFR16k, -s );                                     /* Q_syn+exp1 */
     290             :     }
     291             :     ELSE /* 16kHz sampled output */
     292             :     {
     293           0 :         Copy( HF_syn, upsampled_HF_syn, L_SUBFR16k ); /* Q_syn */
     294             :     }
     295           0 :     Vr_add( synth16k, upsampled_HF_syn, synth16k, output_subfr );
     296             : 
     297           0 :     return;
     298             : }
     299             : 
     300       14951 : void hf_synth_ivas_fx(
     301             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* i/o: handle to 0 bit BWE parameters          */
     302             :     const Word32 core_brate,       /* i  : core bitrate                                          Q0*/
     303             :     const Word16 output_frame,     /* i  : output frame length               Q0*/
     304             :     const Word16 *Aq,              /* i  : quantized Az                                         Q12*/
     305             :     const Word16 *exc,             /* i  : excitation at 12.8 kHz                 Q_exc*/
     306             :     Word16 *synth,                 /* i  : 12.8kHz synthesis signal              Q_syn2*/
     307             :     Word16 *synth16k,              /* o  : 16kHz synthesis signal        Q_syn2*/
     308             :     const Word16 Q_exc,            /* i  : excitation scaling             */
     309             :     const Word16 Q_syn2            /* i  : synthesis scaling              */
     310             : )
     311             : {
     312             :     const Word16 *p_Aq;
     313             :     Word16 i_subfr, output_subfr;
     314             : 
     315       14951 :     output_subfr = shr( output_frame, 2 );
     316             : 
     317       14951 :     p_Aq = Aq; /* Q12 */
     318       74755 :     FOR( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
     319             :     {
     320       59804 :         hf_synthesis_ivas_fx( hBWE_zero, core_brate, output_subfr, p_Aq, &exc[i_subfr], Q_exc, &synth[i_subfr], &synth16k[i_subfr * output_subfr / L_SUBFR], Q_syn2 );
     321             : 
     322       59804 :         p_Aq += ( M + 1 ); /* Q12 */
     323             :     }
     324             : 
     325       14951 :     return;
     326             : }
     327             : 
     328       59804 : static void hf_synthesis_ivas_fx(
     329             :     ZERO_BWE_DEC_HANDLE hBWE_zero,
     330             :     const Word32 core_brate,   /* i  : core bitrate                                Q0*/
     331             :     const Word16 output_subfr, /* i  : output sub-frame length                     Q0*/
     332             :     const Word16 Aq[],         /* i  : quantized Az                               Q12*/
     333             :     const Word16 exc[],        /* i  : excitation at 12.8 kHz                   Q_exc*/
     334             :     const Word16 Q_exc,        /* i  : excitation scaling                            */
     335             :     Word16 synth[],            /* i  : 12.8kHz synthesis signal                 Q_syn*/
     336             :     Word16 synth16k[],         /* i/o: 16kHz synthesis signal                   Q_syn*/
     337             :     const Word16 Q_syn         /* i  : synthesis scaling                             */
     338             : )
     339             : {
     340             :     Word16 i, s;
     341             :     Word16 HF_syn[L_SUBFR16k], upsampled_HF_syn[L_FRAME48k / NB_SUBFR];
     342             :     Word16 HF_exc[L_SUBFR16k];
     343             :     Word16 exp1, exp2, scale, tmp, ener, Q_tmp, Q_ener, sft;
     344             :     Word32 L_tmp, ONE, P_ONE;
     345             :     Word16 Ap[M16k + 1];
     346             :     Word64 prod;
     347             : 
     348             :     /*-----------------------------------------------------------------*
     349             :      * generate white noise vector
     350             :      *-----------------------------------------------------------------*/
     351             : 
     352       59804 :     Random_Fill( &hBWE_zero->seed2, L_SUBFR16k, HF_exc, 3 ); /* 3 = Shift Right by 3 */
     353             : 
     354             :     /* o: HF_exc in Q(-3) */
     355             : 
     356             :     /*-----------------------------------------------------------------*
     357             :      * calculate energy scaling factor so that white noise would have the
     358             :      * same energy as exc12k8
     359             :      *-----------------------------------------------------------------*/
     360             : 
     361             :     /*ener = sum2_f( exc, L_SUBFR ) + 0.01f*/
     362       59804 :     ener = extract_h( Dot_product12( exc, exc, L_SUBFR, &exp2 ) );
     363       59804 :     exp2 = sub( exp2, add( Q_exc, Q_exc ) ); // ener exponent
     364             : 
     365             :     /*tmp = round_fx(Dot_product12(HF_exc, HF_exc, output_subfr, &exp1)); */
     366       59804 :     L_tmp = Dot_product12( HF_exc, HF_exc, L_SUBFR16k, &exp1 );
     367       59804 :     tmp = round_fx( L_tmp );
     368       59804 :     exp1 = add( exp1, 6 ); /* tmp exponent */
     369             : 
     370       59804 :     ener = shr( ener, 1 ); /* to avoid the assertion in div_s() further*/
     371       59804 :     exp2 = add( exp2, 1 );
     372             : 
     373       59804 :     tmp = div_s( ener, tmp );
     374       59804 :     exp1 = sub( exp2, exp1 );
     375             : 
     376       59804 :     scale = Sqrt16( tmp, &exp1 ); /* scale exponent = exp1 */
     377             : 
     378             :     /*-----------------------------------------------------------------*
     379             :      * calculate energy scaling factor to respect tilt of synth12k8
     380             :      * (tilt: 1=voiced, -1=unvoiced)
     381             :      *-----------------------------------------------------------------*/
     382             : 
     383       59804 :     hp400_12k8_ivas_fx( synth, L_SUBFR, hBWE_zero->mem_hp400_fx );
     384             : 
     385             :     /* i: mem_hp400 in Q_syn */
     386             :     /* i: synth in Q_syn */
     387             :     /* o: synth in Q_syn-3 */
     388       59804 :     prod = W_mac0_16_16( 1L, synth[0], synth[0] ); /* 2*(Q_syn-3) */
     389     3827456 :     FOR( i = 1; i < L_SUBFR; i++ )
     390             :     {
     391     3767652 :         prod = W_mac0_16_16( prod, synth[i], synth[i] ); /* 2*(Q_syn-3) */
     392             :     }
     393       59804 :     sft = W_norm( prod );
     394       59804 :     ener = extract_h( W_extract_h( W_shl( prod, sft ) ) );
     395       59804 :     Q_ener = sub( add( shl( sub( Q_syn, 3 ), 1 ), sft ), 48 );
     396             : 
     397       59804 :     prod = W_mac0_16_16( 1L, synth[1], synth[0] ); /* 2*(Q_syn-3) */
     398     3767652 :     FOR( i = 2; i < L_SUBFR; i++ )
     399             :     {
     400     3707848 :         prod = W_mac0_16_16( prod, synth[i], synth[i - 1] ); /* 2*(Q_syn-3) */
     401             :     }
     402       59804 :     sft = sub( W_norm( prod ), 1 );
     403       59804 :     tmp = extract_h( W_extract_h( W_shl( prod, sft ) ) );
     404       59804 :     Q_tmp = sub( add( shl( sub( Q_syn, 3 ), 1 ), sft ), 48 );
     405             : 
     406       59804 :     tmp = s_max( 0, tmp );
     407       59804 :     IF( tmp > 0 )
     408             :     {
     409       59758 :         tmp = div_s( tmp, ener );
     410       59758 :         Q_tmp = add( 15, sub( Q_tmp, Q_ener ) );
     411             :     }
     412             : 
     413             :     /*-----------------------------------------------------------------*
     414             :      * modify energy of white noise according to synthesis tilt
     415             :      *-----------------------------------------------------------------*/
     416             :     /* tmp = 1.0 - fac */
     417       59804 :     ONE = L_shl( 1, Q_tmp );
     418       59804 :     P_ONE = L_shl( 3277 /* 0.1 in Q15 */, sub( Q_tmp, 15 ) );
     419       59804 :     L_tmp = L_msu0( ONE, tmp, 1 );
     420       59804 :     test();
     421       59804 :     if ( core_brate == FRAME_NO_DATA || EQ_32( core_brate, SID_2k40 ) )
     422             :     {
     423             :         /* emphasize HF noise in CNG */
     424             :         /*fac *= 2.0f;*/
     425       29384 :         L_tmp = L_add( L_tmp, L_tmp );
     426             :     }
     427       59804 :     L_tmp = L_max( L_tmp, P_ONE );
     428       59804 :     L_tmp = L_min( L_tmp, ONE );
     429             : 
     430       59804 :     sft = norm_l( L_tmp );
     431       59804 :     L_tmp = L_shl( L_tmp, sft );
     432             : 
     433       59804 :     tmp = round_fx( L_tmp );
     434       59804 :     Q_tmp = sub( add( Q_tmp, sft ), 16 );
     435             : 
     436             :     /*scale *= fac;*/
     437       59804 :     tmp = mult_r( scale, tmp ); /* Q = (15 - exp1) + Q_tmp - 15 */
     438       59804 :     Q_tmp = sub( Q_tmp, exp1 );
     439             :     /*-----------------------------------------------------------------*
     440             :      * modify HF excitation according to both calculated scaling factors
     441             :      * high pass filtering (0.94ms of delay)
     442             :      *-----------------------------------------------------------------*/
     443             : 
     444       59804 :     exp2 = sub( hBWE_zero->memExp1, exp1 );
     445       59804 :     hBWE_zero->memExp1 = exp1;
     446       59804 :     move16();
     447             : 
     448       59804 :     filt_6k_7k_scale_fx( HF_exc, L_SUBFR16k, hBWE_zero->mem_hf_fx, tmp, exp2 );
     449             :     /* i: HF_exc in Q(-3) */
     450             :     /* o: HF_exc in ((-3) + Q_tmp - 17) */
     451             :     /* o: hBWE_zero->mem_hf_fx in Q(HF_exc)-2 */
     452             : 
     453             :     /*-----------------------------------------------------------------*
     454             :      * synthesis of noise: 4.8kHz..5.6kHz --> 6kHz..7kHz
     455             :      *-----------------------------------------------------------------*/
     456             : 
     457             :     /*weight_a( Aq, Ap, 0.6f, M );*/
     458       59804 :     weight_a_lc_fx( Aq, Ap, Gamma_19661_Tbl_fx, M );
     459             :     /* o: Ap in Q14 */
     460             : 
     461       59804 :     Syn_filt_s( 0, Ap, M, HF_exc, HF_syn, L_SUBFR16k, hBWE_zero->mem_syn_hf_fx, 1 );
     462             :     /* o: HF_syn in same Q as HF_exc */
     463             :     /* o: mem_syn_hf_fx same Q as HF_syn */
     464             : 
     465       59804 :     Scale_sig( HF_syn, L_SUBFR16k, ( add( Q_syn, exp1 ) ) ); /* bring HF_syn to (Q_syn+exp1) */
     466             : 
     467             :     /*-----------------------------------------------------------------*
     468             :      * add filtered HF noise to speech synthesis
     469             :      *-----------------------------------------------------------------*/
     470             : 
     471             :     /* delay by 5 samples @16kHz to compensate CLDFB resampling delay (20samples) and HP filtering delay (roughly 15 samples) */
     472       59804 :     delay_signal_fx( HF_syn, L_SUBFR16k, hBWE_zero->delay_syn_hf_fx, NS2SA_FX2( 16000, DELAY_CLDFB_NS ) - 15 );
     473             : 
     474             :     /* interpolate the HF synthesis */
     475       59804 :     IF( EQ_16( output_subfr, L_SUBFR48k ) ) /* 48kHz sampled output */
     476             :     {
     477       50072 :         s = s_max( s_min( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN - 3 ) ), 3 ),
     478       50072 :                           sub( Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx + INTERP_3_1_MEM_LEN - 3, 3 ), 1 ) ),
     479             :                    0 );
     480       50072 :         Scale_sig( HF_syn, L_SUBFR16k, s );                              /* Q_syn+exp1+s */
     481       50072 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN, s ); /* Qx + s */
     482       50072 :         interpolate_3_over_1_allpass_fx( HF_syn, L_SUBFR16k, upsampled_HF_syn, hBWE_zero->mem_hp_interp_fx );
     483       50072 :         Scale_sig( upsampled_HF_syn, 3 * L_SUBFR16k, -s );                /* Q_syn + exp1 + s */
     484       50072 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, INTERP_3_1_MEM_LEN, -s ); /* Qx */
     485       50072 :         Scale_sig( HF_syn, L_SUBFR16k, -s );                              /* Q_syn+exp1 */
     486       50072 :         Scale_sig( upsampled_HF_syn, L_SUBFR48k, -1 );
     487             :     }
     488        9732 :     ELSE IF( EQ_16( output_subfr, L_SUBFR32k ) ) /* 32kHz sampled output */
     489             :     {
     490        6368 :         s = s_max( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP ) ), 2 ), 0 );
     491        6368 :         Scale_sig( HF_syn, L_SUBFR16k, s );                                     /* Q_syn+exp1+s */
     492        6368 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP, s ); /* Qx + s */
     493        6368 :         Interpolate_allpass_steep_fx( HF_syn, hBWE_zero->mem_hp_interp_fx, L_SUBFR16k, upsampled_HF_syn );
     494        6368 :         Scale_sig( upsampled_HF_syn, 2 * L_SUBFR16k, -s );                       /* Q_syn + exp1 */
     495        6368 :         Scale_sig( hBWE_zero->mem_hp_interp_fx, 2 * ALLPASSSECTIONS_STEEP, -s ); /* Qx */
     496        6368 :         Scale_sig( HF_syn, L_SUBFR16k, -s );                                     /* Q_syn+exp1 */
     497             :     }
     498             :     ELSE /* 16kHz sampled output */
     499             :     {
     500        3364 :         Copy( HF_syn, upsampled_HF_syn, L_SUBFR16k ); /* Q_syn */
     501             :     }
     502       59804 :     Vr_add( synth16k, upsampled_HF_syn, synth16k, output_subfr );
     503             : 
     504       59804 :     return;
     505             : }
     506             : 
     507             : /*-------------------------------------------------------------------*
     508             :  * filt_6k_7k:
     509             :  *
     510             :  * 15th order band pass 6kHz to 7kHz FIR filter.
     511             :  *
     512             :  * frequency:  4kHz   5kHz  5.5kHz  6kHz  6.5kHz 7kHz  7.5kHz  8kHz
     513             :  * dB loss:   -60dB  -45dB  -13dB   -3dB   0dB   -3dB  -13dB  -45dB
     514             :  * (gain=4.0)
     515             :  *-------------------------------------------------------------------*/
     516       59804 : static void filt_6k_7k_scale_fx(
     517             :     Word16 signal[], /* i/o: signal                                     Qx*/
     518             :     Word16 lg,       /* i  : length of input            Q0*/
     519             :     Word16 mem[],    /* i/o: memory (size=30)      exp*/
     520             :     Word16 fact,     /* i  : multiply factor        Q0*/
     521             :     Word16 exp       /* i  : Mem Exponent                         */
     522             : )
     523             : {
     524             :     Word16 i, x[L_FRAME48k / NB_SUBFR + ( L_FIR - 1 )];
     525             :     Word32 L_tmp;
     526             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     527       59804 :     Flag Overflow = 0;
     528       59804 :     move32();
     529             : #endif
     530       59804 :     Copy_Scale_sig( mem, x, L_FIR - 1, exp );
     531             : 
     532     4844124 :     FOR( i = 0; i < lg; i++ )
     533             :     {
     534     4784320 :         x[i + L_FIR - 1] = shr( mult( signal[i], fact ), 2 ); /* Qx - 2 */
     535     4784320 :         move16();                                             /* gain of filter = 4 */
     536             :     }
     537     4844124 :     FOR( i = 0; i < lg; i++ )
     538             :     {
     539             : 
     540             :         Word16 j;
     541     4784320 :         L_tmp = 0;
     542     4784320 :         move32();
     543   153098240 :         FOR( j = 0; j < 31; j++ )
     544             :         {
     545   148313920 :             L_tmp = L_mac_o( L_tmp, x[i + j], fir_6k_7k_fx[j], &Overflow ); /* Q16 */
     546             :         }
     547             : 
     548     4784320 :         signal[i] = round_fx_o( L_tmp, &Overflow ); /* Q0 */
     549     4784320 :         move16();
     550             :     }
     551       59804 :     Copy( x + lg, mem, L_FIR - 1 ); /* Qx - 2 */
     552       59804 : }
     553             : 
     554             : /*-------------------------------------------------------------------*
     555             :  * hf_synth_amr_wb_init()
     556             :  *
     557             :  * hf synthesis filters initialization
     558             :  * - initialization of 1600 Hz low pass filter
     559             :  * - initialization of band pass 6kHz to 8kHz FIR filter for noise and line resampled signals
     560             :  *-------------------------------------------------------------------*/
     561             : 
     562           6 : void hf_synth_amr_wb_init_fx(
     563             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO /* i/o: AMR-WB IO data handle   */
     564             : )
     565             : {
     566           6 :     hAmrwb_IO->prev_r_fx = 0;
     567           6 :     move16();
     568           6 :     hAmrwb_IO->fmerit_w_sm_fx = 0;
     569           6 :     move16();
     570           6 :     hAmrwb_IO->frame_count_fx = 0;
     571           6 :     move16();
     572           6 :     hAmrwb_IO->ne_min_fx = -7680;
     573           6 :     move16(); /*-30.0f in Q8*/
     574           6 :     hAmrwb_IO->fmerit_m_sm_fx = 0;
     575           6 :     move16();
     576           6 :     hAmrwb_IO->voice_fac_amr_wb_hf = 0;
     577           6 :     move16();
     578           6 :     hAmrwb_IO->unvoicing_fx = 0;
     579           6 :     move16();
     580           6 :     hAmrwb_IO->unvoicing_sm_fx = 32767;
     581           6 :     move16(); /*1.0f in Q15*/
     582           6 :     hAmrwb_IO->unvoicing_flag_fx = 0;
     583           6 :     move16();
     584           6 :     hAmrwb_IO->voicing_flag_fx = 0;
     585           6 :     move16();
     586           6 :     hAmrwb_IO->start_band_old_fx = 160;
     587           6 :     move16();
     588           6 :     hAmrwb_IO->OptCrit_old_fx = 32768;
     589           6 :     move32(); /*1.0f in Q15*/
     590             :     ;
     591           6 :     return;
     592             : }
     593             : 
     594             : 
     595             : /*-------------------------------------------------------------------*
     596             :  * hf_synth_amr_wb_reset()
     597             :  *
     598             :  * reset of HF synthesis filters
     599             :  * - needed in switching scenarios
     600             :  *-------------------------------------------------------------------*/
     601           0 : void hf_synth_amr_wb_reset_fx(
     602             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* o  : zero BWE decoder handle */
     603             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO  /* i/o: AMR-WB IO data handle   */
     604             : )
     605             : {
     606             :     Word16 i;
     607             : 
     608             : 
     609           0 :     FOR( i = 0; i < L_FRAME16k; i++ )
     610             :     {
     611           0 :         Random( &hBWE_zero->seed2 );
     612             :     }
     613             : 
     614           0 :     set16_fx( hBWE_zero->mem_syn_hf_fx, 0, M );
     615           0 :     set16_fx( hBWE_zero->delay_syn_hf_fx, 0, NS2SA( 16000, DELAY_CLDFB_NS ) );
     616           0 :     set16_fx( hBWE_zero->mem_hp_interp_fx, 0, INTERP_3_1_MEM_LEN );
     617             : 
     618           0 :     hAmrwb_IO->prev_r_fx = 0;
     619           0 :     move16();
     620           0 :     hAmrwb_IO->fmerit_w_sm_fx = 0;
     621           0 :     move16();
     622           0 :     hAmrwb_IO->frame_count_fx = 0;
     623           0 :     move16();
     624           0 :     hAmrwb_IO->ne_min_fx = -7680;
     625           0 :     move16(); /*-30.0f in Q8*/
     626           0 :     hAmrwb_IO->fmerit_m_sm_fx = 0;
     627           0 :     move16();
     628           0 :     hAmrwb_IO->voice_fac_amr_wb_hf = 0;
     629           0 :     move16();
     630           0 :     hAmrwb_IO->unvoicing_fx = 0;
     631           0 :     move16();
     632           0 :     hAmrwb_IO->unvoicing_sm_fx = 32767;
     633           0 :     move16(); /*1.0f in Q15*/
     634           0 :     hAmrwb_IO->unvoicing_flag_fx = 0;
     635           0 :     move16();
     636           0 :     hAmrwb_IO->voicing_flag_fx = 0;
     637           0 :     move16();
     638           0 :     hAmrwb_IO->start_band_old_fx = 160;
     639           0 :     move16();
     640           0 :     hAmrwb_IO->OptCrit_old_fx = 32768;
     641           0 :     move32(); /*1.0f in Q15*/
     642             : 
     643           0 :     return;
     644             : }
     645             : 
     646             : /*-------------------------------------------------------------------*
     647             :  * hf_synth_amr_wb()
     648             :  *
     649             :  * HF synthesis in AMR-WB IO
     650             :  *-------------------------------------------------------------------*/
     651             : 
     652           0 : void hf_synth_amr_wb_fx(
     653             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO, /* i/o: AMR-WB IO data handle   */
     654             :     ZERO_BWE_DEC_HANDLE hBWE_zero, /* o  : zero BWE decoder handle */
     655             :     const Word32 core_brate,       /* i  : core bitrate                                      Q0*/
     656             :     const Word16 output_frame,     /* i  : output frame length                               Q0*/
     657             :     const Word16 *Aq,              /* i  : quantized Az :                                                                       Q12*/
     658             :     const Word16 *exc,             /* i  : excitation at 12.8 kHz  :                                      Q_exc*/
     659             :     Word16 *synth,                 /* i/o: synthesis signal at 12.8k :                                    Q_syn*/
     660             :     Word16 *amr_io_class,          /* i  : signal class (determined by FEC algorithm)        Q0*/
     661             :     Word16 *synth_out,             /* i/o: output signal at output Fs  :                                  Q_out*/
     662             :     Word16 fmerit,                 /* i  : classify parameter from FEC :                                        Q14*/
     663             :     const Word16 *hf_gain,         /* i  : decoded HF gain                                   Q0*/
     664             :     const Word16 *voice_factors,   /* i  : voicing factors  :                                                           Q15*/
     665             :     const Word16 pitch_buf[],      /* i  : pitch buffer  :                                                                       Q5*/
     666             :     const Word16 ng_ener_ST,       /* i  : Noise gate - short-term energy :                                      Q8*/
     667             :     const Word16 *lsf_new,         /* i  : ISF vector  :                                                                         Q2*/
     668             :     const Word16 Q_exc,            /* i  : exc scaling                                         */
     669             :     const Word16 Q_out             /* i  : Q_syn2-1                                            */
     670             : )
     671             : {
     672           0 :     Word16 core_type = 1;
     673             :     Word16 start_band;
     674             :     Word16 HF_corr_gain[NB_SUBFR];
     675             :     Word16 filt_weight_coeff;
     676             :     Word16 i, j, output_subfr;
     677             :     Word16 *pt3, *pt4, *pt5;
     678             :     const Word16 *pt6, *pt7;
     679             :     Word16 *pt1, *pt2;
     680             :     Word32 enr1, enr2;
     681             :     Word16 til[NB_SUBFR], til0[NB_SUBFR];
     682             :     Word16 tmp1, tmp2;
     683             :     Word32 L_tmp;
     684             :     Word16 exp;
     685             :     Word16 pitch_var_cur;
     686             :     Word16 voice_fac;
     687             :     Word16 fmerit_w;
     688             :     Word16 fmerit_m;
     689             :     Word16 *p_Ap;
     690             :     const Word16 *p_Aq;
     691             :     Word16 Ap[( M16k + 1 ) * NB_SUBFR];
     692             :     Word16 sub_gain[NB_SUBFR];
     693             :     Word16 exc16k[L_FRAME16k];
     694             :     Word16 qhf;
     695             :     Word16 dct_exc[L_FRAME], dct_hb[L_FRAME16k], filt_weight[L_SUBFR16k];
     696             :     Word16 hb_ener, g, hb_tonal[L_SUBFR16k], tonal_ener, hb_amb[L_SUBFR16k], inv_g;
     697             :     Word16 fb, fn, signum[L_SUBFR16k];
     698             :     Word16 alpha, beta, ener, tmp, scale, rev_filt_weight_coeff, qdct, q1, q2, q3, q4, shift;
     699             :     Word16 e_subfr1, e_subfr2;
     700             :     Word32 exc32[L_FRAME], dct_exc32[L_FRAME], dct_hb32[L_FRAME16k], exc16k32[L_FRAME16k];
     701             :     Word16 q_tmp;
     702             :     Word16 gamma;
     703           0 :     move16();
     704             : 
     705           0 :     Scale_sig( synth, L_FRAME, -3 ); /* Q_syn - 3 */
     706             : 
     707           0 :     pt1 = synth + 1; /* Q_syn - 3 */
     708           0 :     pt2 = synth;     /* Q_syn - 3 */
     709           0 :     pt3 = til;
     710           0 :     pt4 = til0;
     711           0 :     FOR( i = 0; i < NB_SUBFR; i++ )
     712             :     {
     713           0 :         enr1 = Dot_product( pt2, pt2, L_SUBFR );     /*2*(Q_syn-3)+1 */
     714           0 :         enr2 = Dot_product( pt1, pt2, L_SUBFR - 1 ); /*2*(Q_syn-3)+1 */
     715             : 
     716           0 :         tmp1 = extract_h( enr1 ); /*2*(Q_syn-3) - 15 */
     717           0 :         tmp1 = s_max( 1, tmp1 );
     718           0 :         tmp2 = extract_h( enr2 ); /*2*(Q_syn-3) - 15 */
     719             : 
     720           0 :         exp = norm_s( tmp1 );
     721           0 :         tmp1 = div_s( shl( 1, sub( 14, exp ) ), tmp1 );   /*Q(29-exp-2*(Q_syn-3)-1) */
     722           0 :         L_tmp = L_mult( tmp2, tmp1 );                     /*30-exp-2*(Q_syn-3)-1+2*(Q_syn-3)+1 - >30-exp */
     723           0 :         *pt3 = round_fx( L_shl( L_tmp, sub( exp, 1 ) ) ); /*13 */
     724           0 :         move16();
     725           0 :         *pt4++ = *pt3++;
     726           0 :         move16();
     727             : 
     728           0 :         pt1 += L_SUBFR;
     729           0 :         pt2 += L_SUBFR;
     730             :     }
     731             : 
     732           0 :     output_subfr = shr( output_frame, 2 ); /* Q0 */
     733             : 
     734           0 :     if ( NE_16( *amr_io_class, 7 ) )
     735             :     {
     736           0 :         core_type = 0;
     737           0 :         move16();
     738             :     }
     739             : 
     740             :     /* modify LF parameters for excitation weighting or sub-frame gains calculating */
     741           0 :     pitch_var_cur = 0;
     742           0 :     move16();
     743           0 :     pt6 = pitch_buf;     /* Q5 */
     744           0 :     pt7 = pitch_buf + 1; /* Q5 */
     745           0 :     FOR( i = 0; i < 3; i++ )
     746             :     {
     747           0 :         tmp1 = abs_s( sub( *pt6++, *pt7++ ) );                /*Q5 */
     748           0 :         pitch_var_cur = add( pitch_var_cur, shr( tmp1, 1 ) ); /*Q6 -> Q5 */
     749             :     }
     750           0 :     test();
     751           0 :     IF( GT_16( hAmrwb_IO->frame_count_fx, FRAME_COUNT_HF_SYNTH ) && *amr_io_class == UNVOICED_CLAS )
     752             :     {
     753           0 :         hAmrwb_IO->frame_count_fx = 0;
     754           0 :         hAmrwb_IO->ne_min_fx = -7680; /* -30.0f in Q8 */
     755           0 :         move16();
     756           0 :         move16(); /*Q8;  */
     757             :     }
     758             :     ELSE
     759             :     {
     760           0 :         hAmrwb_IO->frame_count_fx = s_min( hAmrwb_IO->frame_count_fx, 2 * FRAME_COUNT_HF_SYNTH - 1 ); /* Q0 */
     761           0 :         move16();
     762           0 :         hAmrwb_IO->frame_count_fx = add( hAmrwb_IO->frame_count_fx, 1 ); /* Q0 */
     763           0 :         move16();
     764           0 :         hAmrwb_IO->ne_min_fx = s_min( hAmrwb_IO->ne_min_fx, ng_ener_ST ); /* Q8 */
     765           0 :         move16();
     766             :     }
     767             : 
     768           0 :     pt6 = voice_factors;
     769           0 :     L_tmp = L_mult( *pt6++, 4096 /* 0.25 IN Q14 */ ); /* Q30 */
     770           0 :     FOR( i = 1; i < 4; i++ )
     771             :     {
     772           0 :         L_tmp = L_mac( L_tmp, *pt6++, 4096 /* 0.25 IN Q14 */ ); /* Q30 */
     773             :     }
     774             : 
     775           0 :     voice_fac = round_fx( L_tmp ); /* Q14 */
     776             : 
     777             :     /*fmerit_w = fmerit > 5734 ? 5734 : (fmerit < 2458 ? 2458 : fmerit); //Q14 */
     778             : 
     779           0 :     fmerit_w = s_min( fmerit, 5734 /* 0.35 IN Q14 */ );   /* Q14 */
     780           0 :     fmerit_w = s_max( fmerit_w, 2458 /* 0.15 IN Q14 */ ); /* Q14 */
     781             : 
     782           0 :     if ( EQ_16( core_type, 1 ) )
     783             :     {
     784           0 :         fmerit_w = shr( fmerit_w, 1 ); /* Q14 */
     785             :     }
     786             : 
     787           0 :     L_tmp = L_mult( fmerit_w, add_sat( 16384, voice_fac ) ); /* Q14 */
     788           0 :     fmerit_w = extract_l( L_shr( L_tmp, 15 ) );              /*Q14 */
     789             :     /**fmerit_w_sm = add(mult_r(*fmerit_w_sm, 29491), mult_r(fmerit_w, 3277));  //Q14 */
     790           0 :     hAmrwb_IO->fmerit_w_sm_fx = round_fx( L_mac( L_mult( hAmrwb_IO->fmerit_w_sm_fx, 29491 ), fmerit_w, 3277 ) ); /*Q14 */
     791           0 :     move16();
     792           0 :     fmerit_w = hAmrwb_IO->fmerit_w_sm_fx;
     793             : 
     794           0 :     move16();
     795             : 
     796           0 :     tmp1 = fmerit; /* Q14 */
     797           0 :     move16();
     798           0 :     if ( LT_16( fmerit, 8192 /* 0.5 in Q14 */ ) )
     799             :     {
     800           0 :         tmp1 = 16384; /* 1.0f in Q14 */
     801           0 :         move16();
     802             :     }
     803           0 :     fmerit_m = negate( add( -32768, tmp1 ) );
     804           0 :     hAmrwb_IO->fmerit_m_sm_fx = add( shr( hAmrwb_IO->fmerit_m_sm_fx, 1 ), shr( fmerit_m, 1 ) ); /*Q14 */
     805           0 :     move16();
     806           0 :     fmerit_m = hAmrwb_IO->fmerit_m_sm_fx; /* Q14 */
     807           0 :     move16();
     808             : 
     809           0 :     pt1 = til;
     810           0 :     FOR( i = 0; i < NB_SUBFR; i++ )
     811             :     {
     812           0 :         tmp = sub( pitch_var_cur, 320 );
     813           0 :         tmp = s_and( tmp, *pt1 );
     814           0 :         if ( tmp < 0 )
     815             :         {
     816           0 :             *pt1 = 1638; /* 0.2 in Q13 */
     817           0 :             move16();
     818             :         }
     819             : 
     820           0 :         tmp1 = sub( 8192 /* 1.0 in Q13 */, *pt1 );   /* Q13 */
     821           0 :         *pt1 = s_max( 6554 /* 0.8 in Q13 */, tmp1 ); /* Q13 */
     822           0 :         move16();
     823           0 :         tmp1 = add( hAmrwb_IO->ne_min_fx, 7680 /* 30.0 in Q30 */ ); /*Q8  */
     824           0 :         tmp1 = mult_r( tmp1, 7340 );                                /*Q20 - > 0.007  //Q13 */
     825           0 :         *pt1 = add( *pt1, tmp1 );
     826           0 :         move16(); /*Q13 */
     827             : 
     828           0 :         L_tmp = L_mult0( *pt1, fmerit_m );        /*Q13+14 */
     829           0 :         *pt1++ = extract_l( L_shr( L_tmp, 14 ) ); /*Q13 */
     830           0 :         move16();
     831             :     }
     832             :     /* predict LPC coefficents and calculate sub-frame gains */
     833           0 :     p_Aq = Aq;
     834           0 :     p_Ap = Ap;
     835           0 :     pt1 = sub_gain;
     836           0 :     pt2 = til0;
     837           0 :     pt3 = til;
     838           0 :     pt6 = voice_factors;
     839           0 :     FOR( i = 0; i < NB_SUBFR; i++ )
     840             :     {
     841           0 :         envelope_fx( hAmrwb_IO, core_brate, p_Aq, p_Ap, pt1, *pt2, *pt3, *pt6 );
     842           0 :         pt1++;
     843           0 :         pt2++;
     844           0 :         pt3++;
     845           0 :         pt6++;
     846             : 
     847           0 :         p_Aq += ( M + 1 );
     848           0 :         p_Ap += ( M + 1 );
     849             :     }
     850             : 
     851           0 :     AdaptiveStartBand_fx( &start_band, core_brate, lsf_new, voice_fac, *amr_io_class, &hAmrwb_IO->voicing_flag_fx, &hAmrwb_IO->start_band_old_fx, &hAmrwb_IO->OptCrit_old_fx );
     852             : 
     853           0 :     q_tmp = Exp16Array( L_FRAME, exc );
     854           0 :     qdct = sub( q_tmp, 1 );
     855           0 :     Copy_Scale_sig_16_32_DEPREC( exc, exc32, L_FRAME, qdct ); /* Qexc + qdct */
     856             : 
     857           0 :     qdct = add( qdct, Q_exc );
     858           0 :     edct_fx( exc32, dct_exc32, L_FRAME, &qdct );
     859           0 :     q_tmp = Exp32Array( L_FRAME, dct_exc32 );
     860             : 
     861           0 :     q_tmp = sub( q_tmp, 16 );
     862           0 :     Copy_Scale_sig_32_16( dct_exc32, dct_exc, L_FRAME, q_tmp ); /* qdct + qtmp */
     863           0 :     qdct = add( qdct, q_tmp );
     864             : 
     865           0 :     set16_fx( dct_hb, 0, L_FRAME16k );
     866           0 :     pt1 = &dct_hb[200];  /* qdct */
     867           0 :     pt2 = &dct_exc[200]; /* qdct */
     868           0 :     FOR( i = 200; i < 240; i++ )
     869             :     {
     870           0 :         *pt1++ = *pt2++;
     871           0 :         move16(); /*qdct */
     872             :     }
     873           0 :     set16_fx( signum, 1, L_SUBFR16k );
     874           0 :     pt1 = dct_hb + 240;
     875           0 :     pt2 = dct_exc + start_band;
     876           0 :     pt3 = signum;
     877           0 :     FOR( i = 240; i < L_FRAME16k; i++ )
     878             :     {
     879           0 :         if ( *pt2 < 0 )
     880             :         {
     881           0 :             *pt3 = -1;
     882           0 :             move16();
     883             :         }
     884           0 :         *pt1++ = abs_s( *pt2++ );
     885           0 :         move16(); /*qdct */
     886           0 :         pt3++;
     887             :     }
     888           0 :     hb_ener = dot_prod_satcontr( &dct_hb[240], &dct_hb[240], qdct, qdct, &q1, L_SUBFR16k );
     889             : 
     890           0 :     L_tmp = L_shl( L_mult( start_band, 205 /* 0.00625f in Q15 */ ), 14 ); /*Q30 */
     891           0 :     tmp = round_fx( L_tmp );                                              /*Q14 */
     892           0 :     tmp = sub( 18022 /* 1.1f in Q14 */, tmp );                            /*Q14 */
     893           0 :     fmerit_w = round_fx( L_shl( L_mult( fmerit_w, tmp ), 1 ) );           /*Q: 14+14+1+1-16 = 14 */
     894             : 
     895             : 
     896           0 :     L_tmp = L_deposit_l( fmerit_w ); /*Q14 */
     897           0 :     L_tmp = Isqrt( L_tmp );          /*Q(31-7) */
     898           0 :     tmp = round_fx( L_tmp );         /*Q8 */
     899           0 :     q2 = norm_s( tmp );
     900           0 :     alpha = div_s( shl( 1, sub( 14, q2 ) ), tmp ); /*Q(29-q2-8); */
     901           0 :     alpha = shl( alpha, sub( q2, 7 ) );            /*Q14 */
     902             : 
     903           0 :     beta = sub( 16384 /* 1.0f in Q14 */, fmerit_w ); /*Q14 */
     904             : 
     905           0 :     L_tmp = L_mult( alpha, 31130 /* 0.95f in Q15 */ ); /*Q30 */
     906           0 :     gamma = round_fx( L_tmp );                         /*Q14 */
     907           0 :     gamma = sub( 17203 /* 1.05f in Q14 */, gamma );    /*Q14 */
     908           0 :     gamma = s_min( 16384 /* 1.0f in Q14 */, gamma );   /* Q14 */
     909           0 :     gamma = s_max( 4915 /* 0.3f in Q14 */, gamma );    /* Q14 */
     910             : 
     911           0 :     IF( LT_16( beta, 16384 /* 1 in Q14 */ ) )
     912             :     {
     913           0 :         L_tmp = 1; /*variable for tonal energy*/
     914           0 :         move32();
     915             : 
     916           0 :         pt1 = hb_amb;
     917           0 :         pt2 = hb_tonal;
     918           0 :         pt3 = &dct_hb[240];
     919           0 :         FOR( i = 0; i < 8; i++ )
     920             :         {
     921           0 :             fn = add( i, 8 );      /* Q0 */
     922           0 :             tmp1 = div_s( 1, fn ); /*Q15  */
     923           0 :             tmp = 0;
     924           0 :             move16();
     925           0 :             pt4 = &dct_hb[240];
     926           0 :             FOR( j = 0; j < fn; j++ )
     927             :             {
     928           0 :                 tmp = add_sat( tmp, shr( *pt4++, 2 ) ); /*qdct-2 */
     929             :             }
     930           0 :             *pt1 = round_fx( L_shl( L_mult( tmp, tmp1 ), 2 ) ); /*qdct */
     931           0 :             move16();
     932           0 :             *pt2 = sub( *pt3, *pt1 ); /* qdct */
     933           0 :             move16();
     934           0 :             IF( *pt2 > 0 )
     935             :             {
     936           0 :                 L_tmp = L_mac0( L_tmp, shr( *pt2, 1 ), shr( *pt2, 1 ) ); /*2*qdct-2 */
     937             :             }
     938           0 :             pt1++;
     939           0 :             pt2++;
     940           0 :             pt3++;
     941             :         }
     942           0 :         FOR( ; i < L_SUBFR16k - 8; i++ )
     943             :         {
     944           0 :             fb = sub( i, 7 );   /* Q0 */
     945           0 :             fn = add( fb, 15 ); /* Q0 */
     946           0 :             tmp = 0;
     947           0 :             move16();
     948           0 :             pt4 = &dct_hb[fb + 240];
     949           0 :             FOR( j = fb; j < fn; j++ )
     950             :             {
     951           0 :                 tmp = add_sat( tmp, shr( *pt4++, 2 ) ); /*qdct-2 */
     952             :             }
     953           0 :             *pt1 = mult_r( tmp, 8738 );
     954           0 :             move16(); /* 8738 = 1/15 in Q17 qdct */
     955           0 :             *pt2 = sub( *pt3, *pt1 );
     956           0 :             move16();
     957           0 :             IF( *pt2 > 0 )
     958             :             {
     959           0 :                 L_tmp = L_mac0_sat( L_tmp, shr( *pt2, 1 ), shr( *pt2, 1 ) ); /*2*qdct-2 */
     960             :             }
     961           0 :             pt1++;
     962           0 :             pt2++;
     963           0 :             pt3++;
     964             :         }
     965           0 :         FOR( ; i < L_SUBFR16k; i++ )
     966             :         {
     967           0 :             fb = sub( i, 7 ); /* Q0 */
     968           0 :             tmp = 0;
     969           0 :             move16();
     970           0 :             fn = sub( L_SUBFR16k, fb ); /* Q0 */
     971           0 :             tmp1 = div_s( 1, fn );      /*Q15 */
     972           0 :             pt4 = &dct_hb[fb + 240];
     973           0 :             FOR( j = fb; j < L_SUBFR16k; j++ )
     974             :             {
     975           0 :                 tmp = add_sat( tmp, shr( *pt4++, 2 ) ); /*qdct-2 */
     976             :             }
     977           0 :             *pt1 = round_fx( L_shl( L_mult( tmp, tmp1 ), 2 ) ); /*qdct */
     978           0 :             move16();
     979           0 :             *pt2 = sub( *pt3, *pt1 );
     980           0 :             move16();
     981           0 :             IF( *pt2 > 0 )
     982             :             {
     983           0 :                 L_tmp = L_mac0_sat( L_tmp, shr( *pt2, 1 ), shr( *pt2, 1 ) ); /*2*qdct-2 */
     984             :             }
     985           0 :             pt1++;
     986           0 :             pt2++;
     987           0 :             pt3++;
     988             :         }
     989             : 
     990             :         /*scaling of hb_ener is q1; tonal_ener<hb_ener, so scaling of tonal_ener can be q1 too*/
     991           0 :         tonal_ener = round_fx( L_shr( L_tmp, sub( shl( qdct, 1 ), add( q1, 18 ) ) ) );
     992           0 :         tmp = sub( hb_ener, tonal_ener );                          /*q1 */
     993           0 :         tmp1 = round_fx( L_shl( L_mult( beta, tonal_ener ), 1 ) ); /*q1 */
     994           0 :         tmp1 = sub( hb_ener, tmp1 );                               /*q1 */
     995           0 :         tmp = div_s( tmp, s_max( tmp1, 1 ) );                      /*Q15 */
     996           0 :         g = shl( mult( beta, tmp ), 1 );                           /*Q15 */
     997           0 :         q2 = norm_s( g );
     998           0 :         inv_g = div_s( shl( 1, sub( 14, q2 ) ), g ); /*Q(29-q2-15); */
     999           0 :         inv_g = shl( inv_g, sub( q2, 2 ) );          /*Q12 */
    1000           0 :         pt1 = hb_tonal;
    1001           0 :         pt2 = hb_amb;
    1002           0 :         pt3 = &dct_hb[240];
    1003           0 :         pt4 = signum;
    1004           0 :         FOR( i = 0; i < L_SUBFR16k; i++ )
    1005             :         {
    1006           0 :             IF( *pt1 > 0 )
    1007             :             {
    1008           0 :                 *pt1 = mult_r( *pt1, g ); /*qdct  */
    1009           0 :                 move16();
    1010             :             }
    1011           0 :             *pt2 = round_fx_sat( L_shl_sat( L_mult( *pt2, inv_g ), 3 ) ); /*qdct */
    1012           0 :             move16();
    1013           0 :             *pt3 = add_sat( *pt1, *pt2 );
    1014           0 :             move16();
    1015           0 :             *pt3 = extract_l( L_mult0( *pt3, *pt4 ) ); /*qdct   */
    1016           0 :             move16();
    1017           0 :             pt1++;
    1018           0 :             pt2++;
    1019           0 :             pt3++;
    1020           0 :             pt4++;
    1021             :         }
    1022             : 
    1023           0 :         ener = dot_prod_satcontr( &dct_hb[240], &dct_hb[240], qdct, qdct, &q2, L_SUBFR16k ); /* q2 */
    1024           0 :         scale = div_s( shl( 1, 14 ), hb_ener );                                              /*Q(29-q1) */
    1025           0 :         L_tmp = L_mult( ener, scale );                                                       /*30-q1+q2 */
    1026           0 :         q2 = sub( q1, q2 );                                                                  /*30-q2 */
    1027           0 :         scale = round_fx( Isqrt( L_shl( L_tmp, sub( q2, 24 ) ) ) );                          /*Q12  */
    1028           0 :         scale = round_fx_sat( L_shl_sat( L_mult( scale, gamma ), 4 ) );                      /*Q15 */
    1029             :     }
    1030             :     ELSE
    1031             :     {
    1032           0 :         scale = 32767; /*~1 in Q15 */
    1033           0 :         move16();
    1034             :     }
    1035             : 
    1036           0 :     IF( EQ_32( core_brate, ACELP_6k60 ) )
    1037             :     {
    1038           0 :         filt_weight_coeff = 60; /* Q0 */
    1039           0 :         move16();
    1040           0 :         rev_filt_weight_coeff = 555;
    1041           0 :         move16(); /* 1/(filt_weight_coeff-1) Q15 */
    1042             :     }
    1043           0 :     ELSE IF( EQ_32( core_brate, ACELP_8k85 ) )
    1044             :     {
    1045           0 :         filt_weight_coeff = 40; /* Q0 */
    1046           0 :         move16();
    1047           0 :         rev_filt_weight_coeff = 840; /* 1/(filt_weight_coeff-1) Q15 */
    1048           0 :         move16();
    1049             :     }
    1050             :     ELSE
    1051             :     {
    1052           0 :         filt_weight_coeff = 20; /* Q0 */
    1053           0 :         move16();
    1054           0 :         rev_filt_weight_coeff = 1725; /* 1/(filt_weight_coeff-1) Q15 */
    1055           0 :         move16();
    1056             :     }
    1057             : 
    1058           0 :     pt1 = filt_weight;
    1059           0 :     FOR( i = 0; i < filt_weight_coeff; i++ )
    1060             :     {
    1061           0 :         L_tmp = L_mult( -32735, rev_filt_weight_coeff ); /*Q31 */
    1062           0 :         L_tmp = L_shl( Mult_32_16( L_tmp, i ), 14 );     /*Q16+14 */
    1063           0 :         *pt1++ = add( round_fx( L_tmp ), 16384 );
    1064           0 :         move16(); /*Q14 */
    1065             :     }
    1066             : 
    1067           0 :     IF( EQ_32( core_brate, ACELP_23k85 ) )
    1068             :     {
    1069           0 :         pt1 = dct_hb + 240;
    1070           0 :         tmp = sub( filt_weight_coeff, 80 ); /* Q0 */
    1071           0 :         pt3 = filt_weight + tmp;
    1072           0 :         FOR( i = 240; i < L_FRAME16k; i++ )
    1073             :         {
    1074           0 :             *pt1 = mult_r( *pt1, scale ); /*qdct */
    1075           0 :             move16();
    1076             : 
    1077           0 :             IF( GE_16( i, sub( L_FRAME16k, filt_weight_coeff ) ) )
    1078             :             {
    1079           0 :                 *pt1 = round_fx( L_shl( L_mult( *pt3, *pt1 ), 1 ) ); /*qdct */
    1080           0 :                 move16();
    1081             :             }
    1082           0 :             pt1++;
    1083           0 :             pt3++;
    1084             :         }
    1085           0 :         pt1 = dct_hb + 200;
    1086           0 :         pt6 = filt_hp_fx;
    1087           0 :         FOR( i = 200; i < 256; i++ )
    1088             :         {
    1089           0 :             *pt1 = mult_r( *pt6++, *pt1 );
    1090           0 :             move16(); /*qdct */
    1091           0 :             pt1++;
    1092             :         }
    1093           0 :         pt1 = HF_corr_gain;
    1094           0 :         pt6 = hf_gain;
    1095           0 :         FOR( i = 0; i < NB_SUBFR; i++ )
    1096             :         {
    1097           0 :             tmp = *pt6++;
    1098           0 :             move16();
    1099           0 :             *pt1++ = HP_gain_fx[tmp]; /* Q15 */
    1100           0 :             move16();
    1101             :         }
    1102             :     }
    1103             :     ELSE
    1104             :     {
    1105           0 :         pt1 = dct_hb + 240;
    1106           0 :         tmp = sub( filt_weight_coeff, 80 ); /* Q0 */
    1107           0 :         pt3 = filt_weight + tmp;
    1108           0 :         FOR( i = 240; i < L_FRAME16k; i++ )
    1109             :         {
    1110           0 :             *pt1 = mult_r( *pt1, scale ); /*qdct */
    1111           0 :             IF( GT_16( i, 255 ) )
    1112             :             {
    1113           0 :                 *pt1 = mult_r( 19505 /* 0.6 in Q15 */, *pt1 );
    1114           0 :                 move16();
    1115             :             }
    1116             : 
    1117           0 :             IF( GE_16( i, sub( L_FRAME16k, filt_weight_coeff ) ) )
    1118             :             {
    1119           0 :                 *pt1 = round_fx( L_shl( L_mult( *pt3, *pt1 ), 1 ) ); /*qdct */
    1120           0 :                 move16();
    1121             :             }
    1122           0 :             pt1++;
    1123           0 :             pt3++;
    1124             :         }
    1125             : 
    1126           0 :         pt1 = dct_hb + 200;
    1127           0 :         pt6 = filt_hp_fx;
    1128           0 :         pt7 = deem_tab_fx; /* Q15 */
    1129           0 :         FOR( i = 200; i < 256; i++ )
    1130             :         {
    1131           0 :             *pt1 = mult_r( *pt6++, *pt1 );
    1132           0 :             move16(); /*qdct */
    1133           0 :             *pt1 = mult_r( *pt7++, *pt1 );
    1134           0 :             move16(); /* qdct */
    1135           0 :             pt1++;
    1136             :         }
    1137             :     }
    1138             : 
    1139           0 :     q_tmp = Exp16Array( L_FRAME16k, dct_hb );
    1140           0 :     qhf = sub( q_tmp, 1 );
    1141           0 :     Copy_Scale_sig_16_32_DEPREC( dct_hb, dct_hb32, L_FRAME16k, qhf ); /* qhf + qdct */
    1142           0 :     qhf = add( qhf, qdct );
    1143           0 :     edct_fx( dct_hb32, exc16k32, L_FRAME16k, &qhf );
    1144           0 :     q_tmp = Exp32Array( L_FRAME16k, exc16k32 );
    1145           0 :     q_tmp = sub( q_tmp, 16 );
    1146           0 :     Copy_Scale_sig_32_16( exc16k32, exc16k, L_FRAME16k, q_tmp ); /* qhf + qtmp */
    1147           0 :     qhf = add( qhf, q_tmp );
    1148             : 
    1149           0 :     ener = dot_prod_satcontr( exc, exc, Q_exc, Q_exc, &q1, L_FRAME );
    1150           0 :     tmp = dot_prod_satcontr( exc16k, exc16k, qhf, qhf, &q2, L_FRAME16k );
    1151             : 
    1152           0 :     pt6 = exc;
    1153           0 :     pt2 = exc16k;
    1154             : 
    1155           0 :     FOR( i = 0; i < NB_SUBFR; i++ )
    1156             :     {
    1157           0 :         e_subfr1 = dot_prod_satcontr( pt6, pt6, Q_exc, Q_exc, &q3, L_SUBFR );
    1158           0 :         e_subfr2 = dot_prod_satcontr( pt2, pt2, qhf, qhf, &q4, L_SUBFR16k );
    1159             : 
    1160           0 :         L_tmp = L_mult( e_subfr1, tmp ); /*Q(q2+q3+1) */
    1161           0 :         q3 = add( add( q2, q3 ), 1 );
    1162           0 :         shift = norm_l( L_tmp );
    1163           0 :         L_tmp = L_shl( L_tmp, shift ); /*Q(q3+shift); */
    1164           0 :         q3 = add( q3, shift );
    1165           0 :         scale = round_fx_sat( L_tmp ); /*Q(q3-16); */
    1166           0 :         q3 = sub( q3, 16 );
    1167           0 :         scale = div_s( shl( 1, 14 ), scale ); /*Q(29-q3)  */
    1168           0 :         L_tmp = L_mult( scale, ener );        /*Q(29-q3+q1+1) */
    1169           0 :         shift = norm_l( L_tmp );
    1170           0 :         L_tmp = L_shl( L_tmp, shift );     /*Q(29-q3+q1+1+shift) */
    1171           0 :         scale = round_fx_sat( L_tmp );     /*Q(29-q3+q1+1+shift-16) */
    1172           0 :         L_tmp = L_mult( scale, e_subfr2 ); /*Q(29-q3+q1+1+shift-16+q4+1)=Q(15+q1-q3+q4+shift) */
    1173           0 :         q3 = sub( 15, q3 );
    1174           0 :         q3 = add( q3, q1 );
    1175           0 :         q3 = add( q3, shift );
    1176           0 :         q3 = add( q3, q4 );
    1177           0 :         scale = round_fx_sat( Isqrt( L_shl_sat( L_tmp, sub( 6, q3 ) ) ) ); /*Q12 */
    1178           0 :         pt6 += L_SUBFR;
    1179           0 :         FOR( j = 0; j < L_SUBFR16k; j++ )
    1180             :         {
    1181           0 :             *pt2 = round_fx_sat( L_shl_sat( L_mult( *pt2, scale ), 2 ) ); /*qhf-1 */
    1182           0 :             move16();
    1183           0 :             pt2++;
    1184             :         }
    1185             :     }
    1186           0 :     qhf = sub( qhf, 1 );
    1187             : 
    1188           0 :     p_Ap = Ap;
    1189           0 :     pt1 = exc16k;
    1190           0 :     pt2 = synth_out;
    1191           0 :     pt3 = sub_gain;
    1192           0 :     pt4 = HF_corr_gain;
    1193           0 :     pt5 = til0;
    1194           0 :     pt6 = voice_factors;
    1195           0 :     pt7 = exc;
    1196             : 
    1197           0 :     FOR( i = 0; i < NB_SUBFR; i++ )
    1198             :     {
    1199           0 :         hf_synthesis_amr_wb_fx( core_brate, output_subfr, p_Ap, pt1, pt2, hBWE_zero->mem_syn_hf_fx, hBWE_zero->delay_syn_hf_fx,
    1200           0 :                                 hBWE_zero->mem_hp_interp_fx, *pt3, *pt4, *pt5, *pt6, pt7, Q_exc, Q_out, qhf );
    1201           0 :         p_Ap += ( M + 1 );
    1202           0 :         pt1 += L_SUBFR16k;
    1203           0 :         pt2 += output_subfr;
    1204           0 :         pt3++;
    1205           0 :         pt4++;
    1206           0 :         pt5++;
    1207           0 :         pt6++;
    1208           0 :         pt7 += L_SUBFR;
    1209             :     }
    1210             : 
    1211           0 :     return;
    1212             : }
    1213           0 : static void hf_synthesis_amr_wb_fx(
    1214             :     const Word32 core_brate,   /* i  : core bitrate :                                           Q0*/
    1215             :     const Word16 output_subfr, /* i  : output sub-frame length :                        Q0*/
    1216             :     const Word16 Ap[],         /* i  : quantized Aq :                                      Q12*/
    1217             :     Word16 exc16k[],           /* i  : excitation at 16 kHz :                      qhf*/
    1218             :     Word16 synth_out[],        /* i/o: synthesis signal at output Fs :   Q_out*/
    1219             :     Word16 *mem_syn_hf,        /* i/o: HF synthesis memory :                     Q_out*/
    1220             :     Word16 *delay_syn_hf,      /* i/o: HF synthesis memory :                     Q_out*/
    1221             :     Word16 *mem_hp_interp,     /* i/o: interpol. memory  :                               Q_out*/
    1222             :     Word16 p_r,                /* i  : sub-frame gain :                                    Q12*/
    1223             :     Word16 HF_corr_gain,       /* i  : HF gain index :                                     Q14*/
    1224             :     Word16 til,                /* Q14 */
    1225             :     Word16 voice_factors,      /* Q14 */
    1226             :     const Word16 exc[],        /* i  : excitation at 12.8 kHz :                   Qexc*/
    1227             :     const Word16 Q_exc,        /*exc scaling*/
    1228             :     const Word16 Q_out,        /*synth_out scaling*/
    1229             :     Word16 qhf                 /*exc16k scaling*/
    1230             : )
    1231             : {
    1232             :     Word16 i;
    1233             :     Word16 HF_syn[L_SUBFR16k], upsampled_HF_syn[L_SUBFR48k];
    1234             :     Word16 ener, tmp, scale, exc2385[L_SUBFR16k];
    1235             :     Word32 L_tmp;
    1236             :     Word16 q1, q2, q3, shift;
    1237             :     Word16 *pt1, *pt2, flag;
    1238           0 :     IF( EQ_32( core_brate, ACELP_23k85 ) )
    1239             :     {
    1240           0 :         ener = dot_prod_satcontr( exc, exc, Q_exc, Q_exc, &q1, L_SUBFR );
    1241           0 :         tmp = dot_prod_satcontr( exc16k, exc16k, qhf, qhf, &q2, L_SUBFR16k );
    1242             : 
    1243           0 :         L_tmp = L_mult( ener, 6554 /* 0.2 in Q15 */ ); /*Q(q1+16) */
    1244           0 :         q3 = norm_l( L_tmp );
    1245           0 :         L_tmp = L_shl( L_tmp, q3 ); /*Q(q1+q3+16) */
    1246           0 :         ener = extract_h( L_tmp );  /*Q(q1+q3); */
    1247           0 :         q1 = add( q1, q3 );
    1248             : 
    1249           0 :         scale = div_s( shl( 1, 14 ), ener );                                /*Q(29-q1) */
    1250           0 :         L_tmp = L_mult( tmp, scale );                                       /*30-q1+q2 */
    1251           0 :         q2 = sub( q1, q2 );                                                 /*30-q2 */
    1252           0 :         scale = round_fx_sat( Isqrt( L_shl_sat( L_tmp, sub( q2, 24 ) ) ) ); /*Q12  */
    1253           0 :         pt1 = exc16k;
    1254           0 :         pt2 = exc2385;
    1255           0 :         FOR( i = 0; i < L_SUBFR16k; i++ )
    1256             :         {
    1257           0 :             L_tmp = L_mult( *pt1++, HF_corr_gain ); /*qhf+15*/
    1258           0 :             L_tmp = Mult_32_16( L_tmp, scale );     /*qhf-1+12+1*/
    1259           0 :             *pt2++ = round_fx( L_shl( L_tmp, 1 ) ); /*qhf-3*/
    1260           0 :             move16();
    1261             :         }
    1262             : 
    1263           0 :         pt1 = exc16k;
    1264           0 :         FOR( i = 0; i < L_SUBFR16k; i++ )
    1265             :         {
    1266           0 :             *pt1 = mult_r( *pt1, p_r ); /*qhf-3*/
    1267           0 :             move16();
    1268           0 :             pt1++;
    1269             :         }
    1270             : 
    1271           0 :         qhf = sub( qhf, 3 );
    1272             : 
    1273           0 :         ener = dot_prod_satcontr( exc16k, exc16k, qhf, qhf, &q1, L_SUBFR16k );
    1274           0 :         tmp = dot_prod_satcontr( exc2385, exc2385, qhf, qhf, &q2, L_SUBFR16k );
    1275           0 :         L_tmp = L_mult( ener, 9830 ); /*Q(q1+16) */
    1276           0 :         q3 = norm_l( L_tmp );
    1277           0 :         L_tmp = L_shl( L_tmp, q3 ); /*Q(q1+q3+16) */
    1278           0 :         ener = extract_h( L_tmp );  /*Q(q1+q3); */
    1279           0 :         q1 = add( q1, q3 );
    1280             : 
    1281           0 :         scale = div_s( shl( 1, 14 ), ener );                                /*Q(29-q1) */
    1282           0 :         L_tmp = L_mult( tmp, scale );                                       /*30-q1+q2 */
    1283           0 :         q2 = sub( q1, q2 );                                                 /*30-q2 */
    1284           0 :         scale = round_fx_sat( Isqrt( L_shl_sat( L_tmp, sub( q2, 24 ) ) ) ); /*Q12  */
    1285           0 :         flag = negate( s_and( til, -0x8000 ) );
    1286           0 :         if ( GT_16( scale, 4096 /* 1.0f in Q12 */ ) )
    1287             :         {
    1288           0 :             flag = 1;
    1289           0 :             move16();
    1290             :         }
    1291           0 :         IF( flag )
    1292             :         {
    1293           0 :             Copy( exc2385, exc16k, L_SUBFR16k ); /* qhf */
    1294             :         }
    1295             :         ELSE
    1296             :         {
    1297           0 :             pt1 = exc16k;
    1298           0 :             pt2 = exc2385;
    1299           0 :             FOR( i = 0; i < L_SUBFR16k; i++ )
    1300             :             {
    1301           0 :                 tmp = sub( 16348 /* 1 in Q14 */, shl( til, 1 ) );                               /*Q14 */
    1302           0 :                 L_tmp = L_mult( tmp, sub( 26214 /* 1.6f in Q14 */, shr( voice_factors, 1 ) ) ); /*Q29*/
    1303           0 :                 tmp = round_fx( L_shr( L_tmp, 1 ) );                                            /*Q12*/
    1304           0 :                 tmp = s_min( tmp, 4096 /* 1 in Q12 */ );
    1305           0 :                 tmp = s_max( tmp, scale );
    1306           0 :                 *pt1++ = round_fx( L_shl( L_mult( *pt2++, tmp ), 3 ) ) /*qhf*/;
    1307           0 :                 move16();
    1308             :             }
    1309             :         }
    1310             :     }
    1311             :     ELSE
    1312             :     {
    1313           0 :         pt1 = exc16k;
    1314           0 :         FOR( i = 0; i < L_SUBFR16k; i++ )
    1315             :         {
    1316           0 :             *pt1 = mult_r( *pt1, p_r ); /*qhf-3*/
    1317           0 :             move16();
    1318           0 :             pt1++;
    1319             :         }
    1320             : 
    1321           0 :         qhf = sub( qhf, 3 );
    1322             :     }
    1323             : 
    1324           0 :     shift = sub( qhf, Q_out );
    1325           0 :     Syn_filt_s( shift, Ap, M, exc16k, HF_syn, L_SUBFR16k, mem_syn_hf, 1 ); /*Q_out=qhf-shift */
    1326             : 
    1327             :     /*-----------------------------------------------------------------*
    1328             :      * Resample to output sampling rate
    1329             :      * Synchronize LB and HB components (delay componsation)
    1330             :      * Add synthesised high band to speech synthesis
    1331             :      *-----------------------------------------------------------------*/
    1332             : 
    1333           0 :     delay_signal_fx( HF_syn, L_SUBFR16k, delay_syn_hf, NS2SA( 16000, DELAY_CLDFB_NS ) );
    1334             : 
    1335           0 :     IF( EQ_16( output_subfr, L_SUBFR48k ) ) /* 48kHz sampled output */
    1336             :     {
    1337             :         Word16 s;
    1338           0 :         s = s_max( s_min( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( mem_hp_interp, INTERP_3_1_MEM_LEN - 3 ) ), 3 ),
    1339           0 :                           sub( Find_Max_Norm16( mem_hp_interp + INTERP_3_1_MEM_LEN - 3, 3 ), 1 ) ),
    1340             :                    0 );
    1341           0 :         Scale_sig( HF_syn, L_SUBFR16k, s );                /* Qout + s */
    1342           0 :         Scale_sig( mem_hp_interp, INTERP_3_1_MEM_LEN, s ); /* Qout + s */
    1343             : 
    1344           0 :         interpolate_3_over_1_allpass_fx( HF_syn, L_SUBFR16k, upsampled_HF_syn, mem_hp_interp );
    1345             : 
    1346           0 :         Scale_sig( upsampled_HF_syn, L_SUBFR48k, add( -s, -1 ) ); /* Qout - 1 */
    1347           0 :         Scale_sig( mem_hp_interp, INTERP_3_1_MEM_LEN, -s );       /* Qout */
    1348           0 :         Scale_sig( HF_syn, L_SUBFR16k, -s );                      /* Qout */
    1349             :     }
    1350           0 :     ELSE IF( EQ_16( output_subfr, L_SUBFR32k ) ) /* 32kHz sampled output */
    1351             :     {
    1352             :         Word16 s;
    1353           0 :         s = s_max( sub( s_min( Find_Max_Norm16( HF_syn, L_SUBFR16k ), Find_Max_Norm16( mem_hp_interp, 2 * ALLPASSSECTIONS_STEEP ) ), 2 ), 0 );
    1354           0 :         Scale_sig( HF_syn, L_SUBFR16k, s );                       /* Qout + s */
    1355           0 :         Scale_sig( mem_hp_interp, 2 * ALLPASSSECTIONS_STEEP, s ); /* Qout + s */
    1356             : 
    1357           0 :         Interpolate_allpass_steep_fx( HF_syn, mem_hp_interp, L_SUBFR16k, upsampled_HF_syn );
    1358             : 
    1359           0 :         Scale_sig( upsampled_HF_syn, 2 * L_SUBFR16k, -s );         /* Qout */
    1360           0 :         Scale_sig( mem_hp_interp, 2 * ALLPASSSECTIONS_STEEP, -s ); /* Qout */
    1361           0 :         Scale_sig( HF_syn, L_SUBFR16k, -s );                       /* Qout */
    1362             :     }
    1363             :     ELSE /* 16kHz sampled output */
    1364             :     {
    1365           0 :         Copy( HF_syn, upsampled_HF_syn, L_SUBFR16k ); /* Qout */
    1366             :     }
    1367             : 
    1368           0 :     Vr_add( synth_out, upsampled_HF_syn, synth_out, output_subfr );
    1369             : 
    1370           0 :     return;
    1371             : }
    1372             : 
    1373           0 : static Word16 EnhanceClass_fx(
    1374             :     const Word16 qq_fx,           /* Qx */
    1375             :     const Word16 pp_fx,           /* Qx */
    1376             :     const Word16 tilt0_fx,        /* i  : spectrum tilt                Q13*/
    1377             :     const Word16 tilt_fx,         /* i  : spectrum tilt                Q13*/
    1378             :     const Word16 voice_factor_fx, /* i  : voice factor                 Q15*/
    1379             :     Word16 *voice_fac_fx,         /* i/o: smoothed voiced parameter    Q15*/
    1380             :     Word16 *unvoicing_fx,         /* i/o: unvoiced parameter           Q15*/
    1381             :     Word16 *unvoicing_sm_fx,      /* i/o: smoothed unvoiced parameter  Q15*/
    1382             :     Word16 *unvoicing_flag        /* i/o: unvoiced flag                Q0*/
    1383             : )
    1384             : {
    1385             :     Word16 unvoicing_tmp_fx;
    1386             :     Word16 tmp, tmp1;
    1387             :     Word32 L_tmp;
    1388             :     Flag Overflow;
    1389             : 
    1390             : 
    1391             :     /* Decide (*unvoicing_flag) to allow BWE enhancement when qq>pp */
    1392             :     /**voice_fac_fx = add(mult_r(*voice_fac_fx, 24576), mult_r(voice_factor_fx, 8192));  //Q15 */
    1393           0 :     *voice_fac_fx = round_fx( L_mac( L_mult( *voice_fac_fx, 24576 /* 0.75 in Q15 */ ), voice_factor_fx, 8192 /* 0.25 in Q15 */ ) ); /*Q15 */
    1394           0 :     move16();
    1395             : 
    1396           0 :     tmp = mult_r( sub( 8192 /* 1.0f in Q13 */, tilt0_fx ), 16384 /* 1.0f in Q15 */ ); /*Q13 */
    1397             : 
    1398           0 :     L_tmp = L_sub( 32768 /* 1.0 in Q15 */, *voice_fac_fx ); /*Q15 */
    1399             : 
    1400           0 :     L_tmp = Mult_32_16( L_tmp, tmp ); /*Q13 */
    1401           0 :     tmp = extract_l( L_tmp );         /*Q13 */
    1402             : 
    1403           0 :     tmp1 = mult_r( tilt_fx, 21845 ); /*Q15->1/1.5 ->Q13+15-15->Q13 */
    1404           0 :     tmp1 = s_min( tmp1, 8192 /* 1.0f in Q13 */ );
    1405             : 
    1406           0 :     L_tmp = L_mult( tmp, tmp1 );                        /*Q13+Q13+1 */
    1407           0 :     unvoicing_tmp_fx = extract_l( L_shr( L_tmp, 12 ) ); /*Q15 */
    1408             : 
    1409             :     /**unvoicing_fx = add(mult_r(16384, *unvoicing_fx), mult_r(16384, unvoicing_tmp_fx));  //Q15 */
    1410           0 :     *unvoicing_fx = round_fx( L_mac( L_mult( 16384 /* 0.5 in Q15 */, *unvoicing_fx ), 16384 /* 0.5 in Q15 */, unvoicing_tmp_fx ) ); /*Q15 */
    1411           0 :     move16();
    1412             : 
    1413           0 :     IF( GT_16( *unvoicing_sm_fx, *unvoicing_fx ) )
    1414             :     {
    1415             :         /**unvoicing_sm_fx = add(mult_r(29491, *unvoicing_sm_fx), mult_r(3277, *unvoicing_fx));  //Q15 */
    1416           0 :         *unvoicing_sm_fx = round_fx( L_mac( L_mult( 29491 /* 0.9 in Q15 */, *unvoicing_sm_fx ), 3277 /* 0.1 in Q15 */, *unvoicing_fx ) ); /*Q15 */
    1417           0 :         move16();
    1418             :     }
    1419             :     ELSE
    1420             :     {
    1421             :         /**unvoicing_sm_fx = add(mult_r(32440, *unvoicing_sm_fx), mult_r(328, *unvoicing_fx));  //Q15 */
    1422           0 :         *unvoicing_sm_fx = round_fx( L_mac( L_mult( 32440 /* 0.99 in Q15 */, *unvoicing_sm_fx ), 328 /* 0.01 in Q15 */, *unvoicing_fx ) ); /*Q15 */
    1423           0 :         move16();
    1424             :     }
    1425             : 
    1426           0 :     if ( GT_16( sub_o( *unvoicing_fx, *unvoicing_sm_fx, &Overflow ), 3277 /* 0.1 in Q15 */ ) )
    1427             :     {
    1428           0 :         *unvoicing_flag = 1;
    1429           0 :         move16();
    1430             :     }
    1431             : 
    1432           0 :     if ( LT_16( sub_o( *unvoicing_fx, *unvoicing_sm_fx, &Overflow ), 1638 /* 0.05 in Q15 */ ) )
    1433             :     {
    1434           0 :         *unvoicing_flag = 0;
    1435           0 :         move16();
    1436             :     }
    1437           0 :     test();
    1438           0 :     return ( *unvoicing_flag && GT_16( qq_fx, pp_fx ) );
    1439             : }
    1440             : 
    1441           0 : static void envelope_fx(
    1442             :     AMRWB_IO_DEC_HANDLE hAmrwb_IO,
    1443             :     const Word32 core_brate,    /* i  : core bitrate                                                              Q0*/
    1444             :     const Word16 Aq_dyn_scal[], /* i  : de-quant. LPC coefficents, dynamic scaling       Q12*/
    1445             :     Word16 Ap[],                /* o  : extended LPC coefficents,                                        Q12*/
    1446             :     Word16 *sub_gain,           /* o  : sub-frame gain,                                                          Q12*/
    1447             :     Word16 tilt0,               /* i  : spectrum tilt,                                                           Q14*/
    1448             :     Word16 tilt,                /* i  : spectrum tilt,                                                           Q13*/
    1449             :     Word16 voice_factor         /* i  : voice factor,                                                            Q15*/
    1450             : )
    1451             : {
    1452             : 
    1453             :     Word16 px, py, rx, ry, pp, rr, qx, qy, qq; /*Q10*/
    1454             :     Word16 i, Unvoicing_flag;
    1455             :     Word16 alpha;                  /*Q14*/
    1456             :     Word16 est_level1, est_level2; /*Q10*/
    1457             :     Word32 L_tmp;
    1458             :     Word16 tmp, q1, q2, q3, shift;
    1459             :     Word16 As[3], k1, k2;
    1460             :     Word16 *pt1;
    1461             :     const Word16 *pt2, *pt3;
    1462             :     Word16 Aq[M + 1];
    1463             : 
    1464             : 
    1465             :     /* Aq has dynamic scaling
    1466             :       go back to Q12 to make sure there's no overflow while calculating qx,qy*/
    1467           0 :     shift = sub( norm_s( Aq_dyn_scal[0] ), 2 );
    1468           0 :     Copy_Scale_sig( Aq_dyn_scal, Aq, M + 1, shift ); /* Q12 + shift */
    1469             : 
    1470             :     /* LPC envelope weighting */
    1471           0 :     IF( EQ_32( core_brate, ACELP_6k60 ) )
    1472             :     {
    1473           0 :         weight_a_lc_fx( Aq, Ap, Gamma_29491_Tbl, M );
    1474             :     }
    1475             :     ELSE
    1476             :     {
    1477           0 :         weight_a_lc_fx( Aq, Ap, Gamma_19661_Tbl_fx, M );
    1478             :     }
    1479             :     /* Ap has dynamic scaling
    1480             :       go back to Q12 to make sure there's no overflow while calculating px,py*/
    1481           0 :     shift = sub( norm_s( Ap[0] ), 2 );
    1482           0 :     IF( shift != 0 )
    1483             :     {
    1484           0 :         Scale_sig( Ap, M + 1, shift ); /* Q12 + shift */
    1485             :     }
    1486             : 
    1487             :     /* LPC envelope level estimate */
    1488           0 :     L_tmp = 0;
    1489           0 :     move32();
    1490           0 :     pt1 = Ap;           /* Q12 */
    1491           0 :     pt2 = exp_tab_p_fx; /* Q14 */
    1492           0 :     FOR( i = 0; i <= M; i++ )
    1493             :     {
    1494           0 :         L_tmp = L_mac( L_tmp, *pt1++, *pt2++ ); /* Q27 */
    1495             :     }
    1496           0 :     q1 = norm_l( L_tmp );
    1497           0 :     L_tmp = L_shl( L_tmp, q1 );         /*Q(27+q1)*/
    1498           0 :     px = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
    1499             : 
    1500           0 :     L_tmp = L_deposit_l( 0 );
    1501             : 
    1502           0 :     pt1 = Ap;                /* Q12 */
    1503           0 :     pt2 = exp_tab_p_fx + 33; /* Q14 */
    1504           0 :     FOR( i = 0; i <= M; i++ )
    1505             :     {
    1506           0 :         L_tmp = L_mac( L_tmp, *pt1++, *pt2-- ); /* Q27 */
    1507             :     }
    1508           0 :     q2 = norm_l( L_tmp );
    1509           0 :     shift = sub( q1, q2 );
    1510           0 :     IF( shift >= 0 )
    1511             :     {
    1512           0 :         px = shr( px, shift );
    1513           0 :         L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
    1514           0 :         q1 = q2;
    1515           0 :         move16();
    1516             :     }
    1517             :     ELSE
    1518             :     {
    1519           0 :         L_tmp = L_shl( L_tmp, q1 ); /*Q(27+q1)*/
    1520             :     }
    1521           0 :     py = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
    1522             : 
    1523           0 :     L_tmp = L_deposit_l( 0 );
    1524           0 :     pt2 = Aq;           /* Q12 */
    1525           0 :     pt3 = exp_tab_q_fx; /* Q14 */
    1526           0 :     FOR( i = 0; i <= M; i++ )
    1527             :     {
    1528           0 :         L_tmp = L_mac_sat( L_tmp, *pt2++, *pt3++ ); /* Q27 */
    1529             :     }
    1530           0 :     q2 = norm_l( L_tmp );
    1531           0 :     L_tmp = L_shl( L_tmp, q2 );         /*Q(27+q2)*/
    1532           0 :     rx = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q2)*/
    1533             : 
    1534           0 :     L_tmp = L_deposit_l( 0 );
    1535           0 :     pt2 = Aq;                /* Q12 */
    1536           0 :     pt3 = exp_tab_q_fx + 33; /* Q14 */
    1537           0 :     FOR( i = 0; i <= M; i++ )
    1538             :     {
    1539           0 :         L_tmp = L_mac_sat( L_tmp, *pt2++, *pt3-- ); /* Q14 */
    1540             :     }
    1541           0 :     q3 = norm_l( L_tmp );
    1542           0 :     shift = sub( q2, q3 );
    1543           0 :     IF( shift >= 0 )
    1544             :     {
    1545           0 :         rx = shr( rx, shift );
    1546           0 :         L_tmp = L_shl( L_tmp, q3 ); /*Q(27+q3)*/
    1547           0 :         q2 = q3;
    1548           0 :         move16();
    1549             :     }
    1550             :     ELSE
    1551             :     {
    1552           0 :         L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
    1553             :     }
    1554           0 :     ry = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q2)*/
    1555             : 
    1556           0 :     L_tmp = L_mult( px, px );
    1557           0 :     L_tmp = L_mac( L_tmp, py, py );                                        /*Q(21+2*q1)*/
    1558           0 :     pp = round_fx_sat( Isqrt( L_shr( L_tmp, add( 11, shl( q1, 1 ) ) ) ) ); /*Q10*/
    1559           0 :     L_tmp = L_mult( rx, rx );
    1560           0 :     L_tmp = L_mac( L_tmp, ry, ry );                                        /*Q(21+2*q1)*/
    1561           0 :     rr = round_fx_sat( Isqrt( L_shr( L_tmp, add( 11, shl( q2, 1 ) ) ) ) ); /*Q10*/
    1562           0 :     Copy( Aq, As, 3 );
    1563           0 :     IF( EQ_16( shr( As[2], 1 ), -2048 /* -1.0f in Q11 */ ) )
    1564             :     {
    1565           0 :         k2 = -2458; /* -0.6f in Q12 */
    1566           0 :         move16();
    1567           0 :         k1 = 4055; /* 0.99f in Q12 */
    1568           0 :         move16();
    1569           0 :         if ( As[1] < 0 )
    1570             :         {
    1571           0 :             k1 = -k1; /* Q12 */
    1572           0 :             move16();
    1573             :         }
    1574             :     }
    1575             :     ELSE
    1576             :     {
    1577           0 :         k1 = add( 2048 /* 1 in Q11 */, shr( As[2], 1 ) ); /*Q11 */
    1578           0 :         q1 = 11;
    1579           0 :         move16();
    1580           0 :         q2 = norm_s( k1 );
    1581           0 :         k1 = ( shl( k1, q2 ) ); /*q1+q2 */
    1582           0 :         tmp = abs_s( k1 );
    1583           0 :         q1 = add( q1, q2 );
    1584           0 :         tmp = div_s( shl( 1, 14 ), tmp ); /*Q(29-q1) */
    1585           0 :         if ( k1 < 0 )
    1586             :         {
    1587           0 :             tmp = negate( tmp );
    1588             :         }
    1589             : 
    1590           0 :         L_tmp = L_mult( As[1], tmp ); /*Q(42-q1) */
    1591           0 :         q1 = sub( q1, 14 );
    1592           0 :         k1 = round_fx_sat( L_shl_sat( L_tmp, q1 ) ); /*Q12 */
    1593           0 :         k2 = As[2];
    1594           0 :         move16(); /*Q12 */
    1595           0 :         if ( GT_16( k2, 2458 /* 0.6f in Q12 */ ) )
    1596             :         {
    1597           0 :             k2 = 2458; /* 0.6f in Q12 */
    1598           0 :             move16();
    1599             :         }
    1600           0 :         if ( LT_16( k2, -2458 /* -0.6f in Q12 */ ) )
    1601             :         {
    1602           0 :             k2 = -2458; /* -0.6f in Q12 */
    1603           0 :             move16();
    1604             :         }
    1605           0 :         if ( GT_16( k1, 4055 /* 0.99f in Q12 */ ) )
    1606             :         {
    1607           0 :             k1 = 4055; /* 0.99f in Q12 */
    1608           0 :             move16();
    1609             :         }
    1610           0 :         if ( LT_16( k1, -4055 /* -0.99f in Q12 */ ) )
    1611             :         {
    1612           0 :             k1 = -4055; /* -0.99f in Q12 */
    1613           0 :             move16();
    1614             :         }
    1615             :     }
    1616           0 :     As[1] = add( 4096 /* 1 in Q12 */, k2 );
    1617           0 :     move16();
    1618           0 :     L_tmp = L_mult( As[1], k1 );           /*Q25 */
    1619           0 :     As[1] = round_fx( L_shl( L_tmp, 3 ) ); /*Q12 */
    1620           0 :     move16();
    1621           0 :     As[2] = k2;
    1622           0 :     move16(); /*Q12 */
    1623             : 
    1624           0 :     L_tmp = L_deposit_l( 0 );
    1625           0 :     pt1 = As;           /* Q12 */
    1626           0 :     pt2 = exp_tab_q_fx; /* Q14 */
    1627           0 :     FOR( i = 0; i < 3; i++ )
    1628             :     {
    1629           0 :         L_tmp = L_mac( L_tmp, *pt1++, *pt2++ ); /* Q27 */
    1630             :     }
    1631           0 :     q1 = norm_l( L_tmp );
    1632           0 :     L_tmp = L_shl( L_tmp, q1 );         /*Q(27+q1)*/
    1633           0 :     qx = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
    1634             : 
    1635             : 
    1636           0 :     L_tmp = L_deposit_l( 0 );
    1637           0 :     pt1 = As;                /* Q12 */
    1638           0 :     pt2 = exp_tab_q_fx + 33; /* Q14 */
    1639           0 :     FOR( i = 0; i < 3; i++ )
    1640             :     {
    1641           0 :         L_tmp = L_mac( L_tmp, *pt1++, *pt2-- ); /* Q27 */
    1642             :     }
    1643           0 :     q2 = norm_l( L_tmp );
    1644           0 :     shift = sub( q1, q2 );
    1645           0 :     IF( shift >= 0 )
    1646             :     {
    1647           0 :         qx = shr( qx, shift );
    1648           0 :         L_tmp = L_shl( L_tmp, q2 ); /*Q(27+q2)*/
    1649           0 :         q1 = q2;
    1650           0 :         move16();
    1651             :     }
    1652             :     ELSE
    1653             :     {
    1654           0 :         L_tmp = L_shl( L_tmp, q1 ); /*Q(27+q1)*/
    1655             :     }
    1656           0 :     qy = round_fx( L_shr( L_tmp, 1 ) ); /*Q(10+q1)*/
    1657             : 
    1658             : 
    1659           0 :     L_tmp = L_mult( qx, qx );                                          /* Q21 + 2q1 */
    1660           0 :     L_tmp = L_mac( L_tmp, qy, qy );                                    /* Q21 + 2q1 */
    1661           0 :     qq = round_fx( Isqrt( L_shr( L_tmp, add( 11, shl( q1, 1 ) ) ) ) ); /*Q10*/
    1662             : 
    1663           0 :     Unvoicing_flag = EnhanceClass_fx( rr, pp, tilt0, tilt, voice_factor, &hAmrwb_IO->voice_fac_amr_wb_hf, &hAmrwb_IO->unvoicing_fx, &hAmrwb_IO->unvoicing_sm_fx, &hAmrwb_IO->unvoicing_flag_fx ); /* Q0 */
    1664           0 :     alpha = 0;
    1665           0 :     move16();
    1666           0 :     IF( Unvoicing_flag )
    1667             :     {
    1668           0 :         IF( GT_16( rr, ( hAmrwb_IO->prev_r_fx ) ) )
    1669             :         {
    1670           0 :             rr = shr( add_sat( rr, ( hAmrwb_IO->prev_r_fx ) ), 1 );
    1671             :         }
    1672             : 
    1673           0 :         hAmrwb_IO->prev_r_fx = rr;
    1674           0 :         move16();
    1675             : 
    1676           0 :         L_tmp = L_mult( tilt, sub( 26214 /* 1.6f in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
    1677             : 
    1678           0 :         L_tmp = L_min( L_tmp, 268435456 /* 1 in Q28 */ );
    1679             : 
    1680           0 :         L_tmp = Mult_32_16( L_tmp, rr );                                                /*Q23*/
    1681           0 :         rr = round_fx( L_shl( L_tmp, 3 ) );                                             /*Q10*/
    1682           0 :         L_tmp = L_mult( tilt, sub( 26214 /* 1.6f in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
    1683           0 :         L_tmp = L_max( L_tmp, 268435456 /* 1 in Q28 */ );
    1684           0 :         L_tmp = Mult_32_16( L_tmp, qq );            /*Q23*/
    1685           0 :         qq = round_fx_sat( L_shl_sat( L_tmp, 3 ) ); /*Q10*/
    1686           0 :         rr = s_min( rr, qq );                       /* Q10 */
    1687           0 :         rr = s_max( rr, pp );                       /* Q10 */
    1688             :     }
    1689             :     ELSE
    1690             :     {
    1691           0 :         test();
    1692           0 :         IF( LT_16( rr, 1024 /* 1 in Q10 */ ) && LT_16( ( hAmrwb_IO->prev_r_fx ), 1024 /* 1 in Q10 */ ) )
    1693             :         {
    1694           0 :             L_tmp = L_mult( rr, rr );                          /*Q21*/
    1695           0 :             tmp = round_fx( L_shl( L_tmp, 9 ) );               /*Q14*/
    1696           0 :             L_tmp = L_sub( 2097152 /* 1 in Q21 */, L_tmp );    /*Q21*/
    1697           0 :             alpha = round_fx( L_shl( L_tmp, 9 ) );             /*Q14*/
    1698           0 :             L_tmp = L_mult( alpha, ( hAmrwb_IO->prev_r_fx ) ); /*Q25*/
    1699           0 :             L_tmp = L_mac( L_tmp, tmp, rr );                   /*Q25*/
    1700           0 :             rr = round_fx_sat( L_shl_sat( L_tmp, 1 ) );        /*Q10*/
    1701             :         }
    1702             : 
    1703           0 :         hAmrwb_IO->prev_r_fx = rr;
    1704           0 :         move16();
    1705             : 
    1706           0 :         L_tmp = L_mult( tilt, sub( 26214 /* 1.6f in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
    1707             : 
    1708           0 :         L_tmp = L_min( L_tmp, 268435456 /* 1 in Q28 */ );
    1709           0 :         L_tmp = Mult_32_16( L_tmp, qq );            /*Q23*/
    1710           0 :         est_level1 = round_fx( L_shl( L_tmp, 3 ) ); /*Q10*/
    1711             : 
    1712           0 :         tmp = pp;
    1713           0 :         move16();
    1714           0 :         tmp = s_min( tmp, qq );
    1715           0 :         rr = s_min( tmp, rr );
    1716             : 
    1717           0 :         L_tmp = L_mult( abs_s( sub( tilt, 8192 /* 1 in Q13 */ ) ), sub( 26214 /* 1.6 in Q14 */, shr( voice_factor, 1 ) ) ); /*Q28*/
    1718           0 :         L_tmp = L_add( L_tmp, 268435456 /* 1 in Q28 */ );
    1719           0 :         L_tmp = Mult_32_16( L_tmp, rr );            /*Q23*/
    1720           0 :         est_level2 = round_fx( L_shl( L_tmp, 3 ) ); /*Q10*/
    1721             : 
    1722           0 :         rr = s_min( est_level1, est_level2 );
    1723             :     }
    1724             : 
    1725             : 
    1726           0 :     q1 = norm_s( pp );
    1727           0 :     tmp = div_s( shl( 1, sub( 14, q1 ) ), pp ); /*Q(29-q1-10) */
    1728           0 :     L_tmp = L_mult( rr, tmp );                  /*Q(30-q1-10+10) */
    1729             : 
    1730           0 :     *sub_gain = s_min( 20480 /* 5.0f in Q12 */, round_fx_sat( L_shl_sat( L_tmp, sub( q1, 2 ) ) ) ); /*Q12 */
    1731           0 :     move16();
    1732           0 :     return;
    1733             : }
    1734             : 
    1735             : /*---------------------------------------------------------------------*
    1736             :  * AdaptiveStartBand_fx()
    1737             :  *
    1738             :  * adaptively select the start band of bandwidth extension
    1739             :  *---------------------------------------------------------------------*/
    1740             : 
    1741           0 : static void AdaptiveStartBand_fx(
    1742             :     Word16 *start_band,          /* o  : start point of copied band                Q0*/
    1743             :     const Word32 core_rate,      /* i  : core bitrate                              Q0*/
    1744             :     const Word16 *lsf_fx,        /* i  : lsf frequency                             Q2*/
    1745             :     const Word16 voicing_fac_fx, /* i  : voicing factors                          Q14*/
    1746             :     const Word16 clas,           /* i  : signal class (determined by FEC algorithm) Q0*/
    1747             :     Word16 *voicing_flag,        /* Q0 */
    1748             :     Word16 *start_band_old,      /* Q0 */
    1749             :     Word32 *OptCrit_old_fx       /*i/o : Q15 */
    1750             : )
    1751             : {
    1752             :     Word16 i, pos, M2, voicing_flag_old;
    1753             :     Word16 lsf_diff_fx[M];
    1754             :     Word16 W_fx;
    1755             :     Word16 tmp1, tmp2;
    1756             :     Word32 L_tmp;
    1757           0 :     Word32 OptCrit_fx = 32768, Crit_fx;
    1758             :     Word16 *pt1, flag;
    1759             :     const Word16 *pt2, *pt3;
    1760             : 
    1761           0 :     move32(); // corresponding to initialization of OptCrit_fx
    1762             :     /*voicing switching flag : to avoid switching start band frequently in VOICED or AUDIO area*/
    1763           0 :     voicing_flag_old = *voicing_flag; /* Q0 */
    1764           0 :     move16();
    1765           0 :     test();
    1766           0 :     test();
    1767           0 :     test();
    1768           0 :     if ( GT_16( voicing_fac_fx, 6554 /* 0.4 in Q14 */ ) || ( GT_16( voicing_fac_fx, 4915 /* 0.3 in Q14 */ ) && GE_16( clas, VOICED_CLAS ) ) || EQ_16( clas, AUDIO_CLAS ) )
    1769             :     {
    1770           0 :         *voicing_flag = 1; /* Q0 */
    1771           0 :         move16();
    1772             :     }
    1773             : 
    1774           0 :     test();
    1775           0 :     if ( LT_16( voicing_fac_fx, 3277 /* 0.2 in Q14 */ ) && LT_16( clas, VOICED_CLAS ) )
    1776             :     {
    1777           0 :         *voicing_flag = 0;
    1778           0 :         move16();
    1779             :     }
    1780             : 
    1781             :     /* core_rate adaptive start band */
    1782           0 :     *start_band = 160;
    1783           0 :     move16();
    1784           0 :     IF( LT_32( core_rate, ACELP_23k05 ) )
    1785             :     {
    1786           0 :         pt1 = lsf_diff_fx + 1;
    1787           0 :         pt2 = lsf_fx + 1;
    1788           0 :         pt3 = lsf_fx;
    1789           0 :         FOR( i = 1; i < ( M - 1 ); i++ )
    1790             :         {
    1791           0 :             *pt1++ = sub( *pt2++, *pt3++ );
    1792           0 :             move16(); /*Q2 */
    1793             :         }
    1794           0 :         tmp1 = extract_l( Mult_32_16( core_rate, 27046 /* 0.00005 in Q29 */ ) ); /*Q14 */
    1795           0 :         L_tmp = L_shr( L_mult0( tmp1, 22370 /* 1/6000 in Q27 */ ), 15 );         /*Q27->1/6000 ->Q26 */
    1796           0 :         tmp2 = extract_l( L_tmp );                                               /*Q26 */
    1797           0 :         W_fx = mult_r( tmp1, tmp2 );                                             /*Q25 */
    1798             : 
    1799           0 :         IF( EQ_16( clas, AUDIO_CLAS ) )
    1800             :         {
    1801           0 :             W_fx = mult_r( W_fx, 24576 /* 0.75 in Q15 */ ); /*Q25 */
    1802             :         }
    1803             : 
    1804           0 :         pos = 2;
    1805           0 :         move16();
    1806           0 :         M2 = M - 2;
    1807           0 :         move16();
    1808           0 :         IF( EQ_16( *voicing_flag, 1 ) )
    1809             :         {
    1810           0 :             IF( LE_32( core_rate, ACELP_8k85 ) )
    1811             :             {
    1812           0 :                 M2 = M - 8;
    1813           0 :                 move16();
    1814             :             }
    1815           0 :             ELSE IF( LE_32( core_rate, ACELP_12k65 ) )
    1816             :             {
    1817           0 :                 M2 = M - 6;
    1818           0 :                 move16();
    1819             :             }
    1820           0 :             ELSE IF( LE_32( core_rate, ACELP_15k85 ) )
    1821             :             {
    1822           0 :                 M2 = M - 4;
    1823           0 :                 move16();
    1824             :             }
    1825             :         }
    1826             : 
    1827             :         /*do the procedure for i==2*/
    1828           0 :         L_tmp = L_max( L_msu( 171798692 /* 1 in Q2.56+25+1 */, lsf_fx[2], W_fx ), 171799 /* 0.001 in Q2.56+25+1 */ ); /* Q2.56+25+1 */
    1829           0 :         Crit_fx = Mult_32_16( L_tmp, lsf_diff_fx[2] );                                                                /* Q2.56+25+1+2.56-15 = Q11+2.56+2.56 */
    1830             : 
    1831           0 :         OptCrit_fx = L_add( Crit_fx, 0 );
    1832           0 :         pos = 2;
    1833           0 :         move16();
    1834             :         /*----------------------------------------------------------------*/
    1835             : 
    1836           0 :         pt2 = &lsf_fx[3];
    1837           0 :         pt1 = &lsf_diff_fx[3];
    1838           0 :         FOR( i = 3; i < M2; i++ )
    1839             :         {
    1840           0 :             L_tmp = L_max( L_msu( 171798692 /* 1 in Q2.56+25+1 */, *pt2++, W_fx ), 171799 /* 0.001 in Q2.56+25+1 */ ); /* Q2.56+25+1 */
    1841             : 
    1842           0 :             Crit_fx = Mult_32_16( L_tmp, *pt1++ ); /* Q2.56+25+1+2.56-15 = Q11+2.56+2.56 */
    1843             : 
    1844           0 :             IF( LE_32( Crit_fx, OptCrit_fx ) )
    1845             :             {
    1846           0 :                 OptCrit_fx = L_add( Crit_fx, 0 ); /* Q11+2.56+2.56 */
    1847           0 :                 pos = i;                          /* Q0 */
    1848           0 :                 move16();
    1849             :             }
    1850             :         }
    1851             : 
    1852           0 :         tmp1 = add( mult_r( lsf_fx[pos - 1], 256 ), mult_r( lsf_fx[pos], 256 ) ); /* Q0 */
    1853           0 :         *start_band = s_min( s_max( sub( tmp1, 40 ), 40 ), 160 );                 /* Q0 */
    1854           0 :         move16();
    1855             : 
    1856           0 :         L_tmp = Mult_32_16( *OptCrit_old_fx, 22938 /* 0.7 in Q15 */ ); /* Q11+2.56+2.56 */
    1857           0 :         test();
    1858           0 :         test();
    1859           0 :         test();
    1860           0 :         test();
    1861           0 :         IF( NE_16( voicing_flag_old, *voicing_flag ) || ( *voicing_flag == 0 && LT_32( OptCrit_fx, *OptCrit_old_fx ) ) ||
    1862             :             ( LT_32( OptCrit_fx, L_tmp ) && GT_32( *OptCrit_old_fx, 858993 /* 64 in Q11+2.56+2.56 */ ) ) )
    1863             :         {
    1864           0 :             *OptCrit_old_fx = OptCrit_fx; /* Q15 */
    1865           0 :             move16();
    1866           0 :             test();
    1867           0 :             test();
    1868           0 :             if ( LT_16( abs_s( sub( ( *start_band ), ( *start_band_old ) ) ), 20 ) && EQ_16( *voicing_flag, 1 ) && EQ_16( voicing_flag_old, 1 ) )
    1869             :             {
    1870           0 :                 *start_band = *start_band_old; /* Q0 */
    1871           0 :                 move16();
    1872             :             }
    1873             :         }
    1874             :         ELSE
    1875             :         {
    1876           0 :             test();
    1877           0 :             if ( LT_32( OptCrit_fx, ( *OptCrit_old_fx ) ) && EQ_16( ( *voicing_flag ), 1 ) )
    1878             :             {
    1879           0 :                 *OptCrit_old_fx = OptCrit_fx; /* Q15 */
    1880           0 :                 move16();
    1881             :             }
    1882             : 
    1883           0 :             *start_band = *start_band_old; /* Q0 */
    1884           0 :             move16();
    1885             :         }
    1886             : 
    1887           0 :         IF( EQ_16( clas, AUDIO_CLAS ) )
    1888             :         {
    1889           0 :             *start_band = s_min( *start_band, 120 ); /* Q0 */
    1890           0 :             move16();
    1891             :         }
    1892             : 
    1893           0 :         flag = sub( s_and( *start_band, 0x0001 ), 1 );
    1894           0 :         IF( flag == 0 )
    1895             :         {
    1896           0 :             *start_band = sub( *start_band, 1 ); /* Q0 */
    1897           0 :             move16();
    1898             :         }
    1899             :     }
    1900             : 
    1901           0 :     *start_band_old = *start_band; /* Q0 */
    1902           0 :     move16();
    1903             : 
    1904           0 :     return;
    1905             : }

Generated by: LCOV version 1.14