LCOV - code coverage report
Current view: top level - lib_enc - gs_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 534 629 84.9 %
Date: 2025-05-03 01:55:50 Functions: 6 6 100.0 %

          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"
       6             : #include "cnst.h"
       7             : #include "rom_com_fx.h"
       8             : #include "rom_com.h"
       9             : #include "prot_fx.h"     /* Function prototypes                    */
      10             : #include "prot_fx_enc.h" /* Function prototypes                    */
      11             : 
      12             : /*-------------------------------------------------------------------*
      13             :  * Local function prototypes
      14             :  *-------------------------------------------------------------------*/
      15             : 
      16             : static Word16 edyn_fx( const Word16 *vec, const Word16 lvec, Word16 Qnew );
      17             : 
      18             : /*-------------------------------------------------------------------*
      19             :  * encod_audio()
      20             :  *
      21             :  * Encode audio (AC) frames
      22             :  *-------------------------------------------------------------------*/
      23           7 : void encod_audio_fx(
      24             :     Encoder_State *st_fx,              /* i/o: State structure                                          */
      25             :     const Word16 speech[],             /* i  : input speech                                Q_new   */
      26             :     const Word16 Aw[],                 /* i  : weighted A(z) unquantized for subframes       Q12      */
      27             :     const Word16 Aq[],                 /* i  : 12k8 Lp coefficient                           Q12      */
      28             :     const Word16 *res,                 /* i  : residual signal                             Q_new   */
      29             :     Word16 *synth,                     /* i/o: core synthesis                              Q-1     */
      30             :     Word16 *exc,                       /* i/o: current non-enhanced excitation             Q_new   */
      31             :     Word16 *pitch_buf,                 /* i/o: floating pitch values for each subframe     Q6           */
      32             :     Word16 *voice_factors,             /* o  : voicing factors                             Q15          */
      33             :     Word16 *bwe_exc,                   /* o  : excitation for SWB TBE                                           Q0              */
      34             :     const Word16 attack_flag,          /* i  : Flag that point to an attack coded with AC mode (GSC)    */
      35             :     Word16 *lsf_new,                   /* i  : current frame ISF vector                                         Qx2.56          */
      36             :     Word16 *tmp_noise,                 /* o  : noise energy   Q2*/
      37             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag             Q0*/
      38             :     const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                 Q6*/
      39             :     Word16 Q_new,
      40             :     Word16 shift )
      41             : {
      42             :     const Word16 *p_Aq;
      43             :     Word16 i, i_subfr, nb_subfr, last_pit_bin;
      44             :     Word16 T0_tmp, T0_frac_tmp, nb_subfr_flag;
      45           7 :     Word16 tmp_nb_bits_tot = 0;
      46           7 :     move16();
      47             :     Word16 Es_pred;
      48             :     Word16 dct_res[L_FRAME16k], dct_epit[L_FRAME16k];
      49           7 :     Word16 m_mean = 0;
      50           7 :     move16();
      51             :     Word16 saved_bit_pos;
      52             :     Word16 exc_wo_nf[L_FRAME16k];
      53             :     Word32 Lm_mean;
      54             :     Word16 nb_bits;
      55             :     Word16 indice;
      56           7 :     SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
      57           7 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
      58           7 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
      59           7 :     LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
      60             : 
      61           7 :     m_mean = 0;
      62           7 :     move16();
      63           7 :     tmp_nb_bits_tot = 0;
      64           7 :     move16();
      65             : 
      66           7 :     T0_tmp = 64;
      67           7 :     move16();
      68           7 :     T0_frac_tmp = 0;
      69           7 :     move16();
      70           7 :     Copy( hLPDmem->mem_syn, hGSCEnc->mem_syn_tmp_fx, M ); /* hLPDmem->q_mem_syn */
      71           7 :     hGSCEnc->mem_w0_tmp_fx = hLPDmem->mem_w0;
      72           7 :     move16();
      73           7 :     Es_pred = 0;
      74           7 :     move16();
      75             : 
      76             :     /*---------------------------------------------------------------*
      77             :      * Encode GSC IVAS mode
      78             :      * Encode GSC attack flag (used to reduce possible pre-echo)
      79             :      * Encode GSC SWB speech flag
      80             :      *---------------------------------------------------------------*/
      81             : #ifdef GSC_IVAS // TVB -->>>>>>
      82             :     test();
      83             :     IF( ( st_fx->element_mode > EVS_MONO ) && st_fx->idchan == 0 )
      84             :     {
      85             :         push_indice( hBstr, IND_GSC_IVAS_SP, st_fx->GSC_IVAS_mode, 2 );
      86             :     }
      87             : #endif
      88           7 :     IF( attack_flag > 0 )
      89             :     {
      90           0 :         push_indice( hBstr, IND_GSC_ATTACK, 1, 1 );
      91             :     }
      92             :     ELSE
      93             :     {
      94           7 :         push_indice( hBstr, IND_GSC_ATTACK, 0, 1 );
      95             :     }
      96             : 
      97             : 
      98           7 :     test();
      99           7 :     test();
     100           7 :     test();
     101           7 :     test();
     102           7 :     test();
     103           7 :     test();
     104           7 :     test();
     105           7 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) || ( NE_16( st_fx->coder_type, INACTIVE ) && ( ( EQ_16( st_fx->element_mode, EVS_MONO ) && GE_32( st_fx->total_brate, ACELP_13k20 ) ) ||
     106             :                                                                                         ( GT_16( st_fx->element_mode, EVS_MONO ) && GT_32( st_fx->total_brate, MIN_BRATE_GSC_NOISY_FLAG ) && GE_16( st_fx->bwidth, SWB ) && !st_fx->flag_ACELP16k ) ) ) )
     107             :     {
     108           0 :         push_indice( hBstr, IND_GSC_SWB_SPEECH, st_fx->GSC_noisy_speech, 1 );
     109             :     }
     110             :     /*---------------------------------------------------------------*
     111             :      * Find and encode the number of subframes
     112             :      *---------------------------------------------------------------*/
     113           7 :     test();
     114           7 :     IF( GE_32( st_fx->core_brate, ACELP_9k60 ) && LE_32( st_fx->core_brate, ACELP_13k20 ) )
     115             :     {
     116          42 :         FOR( i = 0; i < 5; i++ )
     117             :         {
     118          35 :             test();
     119          35 :             if ( GT_16( abs_s( hSpMusClas->gsc_lt_diff_etot_fx[MAX_LT - i - 1] ), 1536 /*6.0 in Q8*/ ) && EQ_16( hGSCEnc->cor_strong_limit, 1 ) )
     120             :             {
     121           0 :                 hGSCEnc->cor_strong_limit = 0;
     122           0 :                 move16();
     123             :             }
     124             :         }
     125             :     }
     126           7 :     test();
     127           7 :     test();
     128           7 :     test();
     129           7 :     test();
     130           7 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) || ( st_fx->GSC_noisy_speech && st_fx->GSC_IVAS_mode == 0 ) )
     131             :     {
     132           0 :         nb_subfr = NB_SUBFR;
     133           0 :         move16();
     134           0 :         test();
     135           0 :         test();
     136           0 :         if ( st_fx->GSC_IVAS_mode > 0 && LT_16( st_fx->GSC_IVAS_mode, 3 ) && LT_32( st_fx->core_brate, GSC_L_RATE_STG ) )
     137             :         {
     138           0 :             nb_subfr = 2;
     139           0 :             move16();
     140             :         }
     141           0 :         hGSCEnc->cor_strong_limit = 0;
     142           0 :         move16();
     143           0 :         nb_subfr_flag = 1;
     144           0 :         move16();
     145             :     }
     146           7 :     ELSE IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && ( LE_32( st_fx->core_brate, ACELP_13k20 ) || EQ_16( st_fx->coder_type, INACTIVE ) ) )
     147             :     {
     148           0 :         hGSCEnc->cor_strong_limit = 0;
     149           0 :         nb_subfr = SWNB_SUBFR;
     150           0 :         nb_subfr_flag = 1;
     151           0 :         move16();
     152           0 :         move16();
     153           0 :         move16();
     154             :     }
     155             :     ELSE
     156             :     {
     157           7 :         test();
     158           7 :         test();
     159           7 :         IF( ( hGSCEnc->cor_strong_limit == 0 || EQ_16( st_fx->coder_type, INACTIVE ) ) && GE_32( st_fx->core_brate, ACELP_9k60 ) )
     160             :         {
     161           7 :             nb_subfr = 2;
     162           7 :             move16();
     163           7 :             nb_subfr_flag = 0;
     164           7 :             move16();
     165           7 :             hGSCEnc->cor_strong_limit = 0;
     166           7 :             move16();
     167             :         }
     168             :         ELSE
     169             :         {
     170           0 :             nb_subfr = SWNB_SUBFR;
     171           0 :             move16();
     172           0 :             nb_subfr_flag = 1;
     173           0 :             move16();
     174             :         }
     175             : 
     176           7 :         test();
     177           7 :         test();
     178           7 :         test();
     179           7 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && ( GT_16( hSpMusClas->mold_corr_fx, 26214 ) && GE_32( st_fx->core_brate, MIN_RATE_4SBFR ) && NE_16( st_fx->coder_type, INACTIVE ) ) )
     180             :         {
     181           0 :             nb_subfr = shl( nb_subfr, 1 );
     182           0 :             nb_subfr_flag = s_or( nb_subfr_flag, 0x2 );
     183           0 :             logic16();
     184             :         }
     185             : 
     186           7 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && GE_32( st_fx->core_brate, MIN_RATE_4SBFR ) )
     187             :         {
     188           0 :             push_indice( hBstr, IND_HF_NOISE, nb_subfr_flag, 2 );
     189             :         }
     190           7 :         ELSE IF( GE_32( st_fx->core_brate, ACELP_9k60 ) )
     191             :         {
     192             :             /* nb_subfr_flag can only have the value 0 or 1 */
     193           7 :             push_indice( hBstr, IND_HF_NOISE, nb_subfr_flag, 1 );
     194             :         }
     195             :     }
     196           7 :     test();
     197           7 :     if ( EQ_16( st_fx->L_frame, L_FRAME16k ) && EQ_16( nb_subfr, NB_SUBFR ) )
     198             :     {
     199           0 :         nb_subfr = NB_SUBFR16k;
     200           0 :         move16();
     201             :     }
     202             : 
     203             :     /*---------------------------------------------------------------*
     204             :      * Compute adaptive (pitch) excitation contribution
     205             :      *---------------------------------------------------------------*/
     206             : 
     207           7 :     test();
     208           7 :     test();
     209           7 :     test();
     210           7 :     test();
     211           7 :     test();
     212           7 :     test();
     213           7 :     test();
     214           7 :     test();
     215           7 :     IF( !( st_fx->GSC_IVAS_mode > 0 && EQ_16( st_fx->L_frame, i_mult( nb_subfr, 2 * L_SUBFR ) ) && LT_16( st_fx->GSC_IVAS_mode, 3 ) ) &&
     216             :         ( ( GE_32( st_fx->core_brate, MIN_RATE_FCB ) || st_fx->GSC_noisy_speech ) &&
     217             :           ( ( EQ_16( nb_subfr, NB_SUBFR ) && EQ_16( st_fx->L_frame, L_FRAME ) ) || ( EQ_16( nb_subfr, NB_SUBFR16k ) && EQ_16( st_fx->L_frame, L_FRAME16k ) ) ) ) )
     218             :     {
     219           0 :         IF( ( st_fx->element_mode > EVS_MONO ) )
     220             :         {
     221           0 :             nb_bits = 5;
     222           0 :             move16();
     223             :         }
     224             :         ELSE
     225             :         {
     226           0 :             nb_bits = Es_pred_bits_tbl[BIT_ALLOC_IDX_fx( st_fx->core_brate, GENERIC, -1, -1 )];
     227           0 :             move16();
     228             :         }
     229           0 :         Es_pred_enc_fx( &Es_pred, &indice, st_fx->L_frame, res, st_fx->voicing_fx, nb_bits, 0, Q_new );
     230           0 :         push_indice( hBstr, IND_ES_PRED, indice, nb_bits );
     231             :     }
     232             : 
     233           7 :     enc_pit_exc_fx( st_fx, speech, Aw, Aq, Es_pred, res, synth, exc, &T0_tmp,
     234             :                     &T0_frac_tmp, pitch_buf, nb_subfr, &hGSCEnc->lt_gpitch_fx, &saved_bit_pos, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, Q_new, shift );
     235             : 
     236             :     /*---------------------------------------------------------------*
     237             :      * DCT transform
     238             :      *---------------------------------------------------------------*/
     239           7 :     edct_16fx( exc, dct_epit, st_fx->L_frame, 7, st_fx->element_mode );
     240           7 :     edct_16fx( res, dct_res, st_fx->L_frame, 7, st_fx->element_mode );
     241             :     /*---------------------------------------------------------------*
     242             :      * Calculate energy dynamics
     243             :      *---------------------------------------------------------------*/
     244           7 :     Lm_mean = L_deposit_l( 0 );
     245          63 :     FOR( i = 7; i < 15; i++ )
     246             :     {
     247             :         /*m_mean = add(m_mean,edyn_fx( dct_res+i*16, 16, Q_new )); */
     248          56 :         Lm_mean = L_mac( Lm_mean, edyn_fx( dct_res + i * 16, 16, Q_new ), 4096 ); /*Q7*/
     249             :     }
     250           7 :     m_mean = round_fx( Lm_mean ); /*Q7*/
     251             : 
     252           7 :     IF( GT_16( m_mean, hGSCEnc->mid_dyn_fx ) )
     253             :     {
     254             :         /*st_fx->mid_dyn_fx = 0.2f * st_fx->mid_dyn_fx + 0.8f * m_mean;*/
     255           5 :         hGSCEnc->mid_dyn_fx = round_fx( L_mac( L_mult( 26214, m_mean ), 6554, hGSCEnc->mid_dyn_fx ) ); /*Q7*/
     256           5 :         move16();
     257             :     }
     258             :     ELSE
     259             :     {
     260             :         /*st_fx->mid_dyn_fx = 0.6f * st_fx->mid_dyn_fx + 0.4f * m_mean;*/
     261           2 :         hGSCEnc->mid_dyn_fx = round_fx( L_mac( L_mult( 13107, m_mean ), 19661, hGSCEnc->mid_dyn_fx ) ); /*Q7*/
     262           2 :         move16();
     263             :     }
     264           7 :     IF( NE_16( st_fx->coder_type, INACTIVE ) )
     265             :     {
     266           0 :         hGSCEnc->noise_lev = sub( ( NOISE_LEVEL_SP3 + 1 ), usquant_fx( hGSCEnc->mid_dyn_fx, &m_mean, MIN_DYNAMIC_FX, shr( GSF_NF_DELTA_FX, 1 ), GSC_NF_STEPS ) );
     267           0 :         move16();
     268           0 :         hGSCEnc->noise_lev = s_min( hGSCEnc->noise_lev, NOISE_LEVEL_SP3 );
     269           0 :         move16();
     270             :     }
     271             : 
     272           7 :     hGSCEnc->past_dyn_dec = hGSCEnc->noise_lev;
     273           7 :     move16();
     274           7 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) )
     275             :     {
     276           0 :         hGSCEnc->noise_lev = NOISE_LEVEL_SP2;
     277           0 :         move16();
     278           0 :         IF( EQ_16( st_fx->GSC_IVAS_mode, 3 ) ) /* Music like */
     279             :         {
     280           0 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP0;
     281           0 :             move16();
     282             :         }
     283           0 :         ELSE IF( st_fx->GSC_noisy_speech == 0 )
     284             :         {
     285           0 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     286           0 :             move16();
     287             :         }
     288             :     }
     289           7 :     ELSE IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
     290             :     {
     291           0 :         hGSCEnc->noise_lev = s_max( hGSCEnc->noise_lev, NOISE_LEVEL_SP2 );
     292           0 :         move16();
     293           0 :         push_indice( hBstr, IND_NOISE_LEVEL, sub( hGSCEnc->noise_lev, NOISE_LEVEL_SP2 ), 2 );
     294             :     }
     295           7 :     ELSE IF( st_fx->GSC_noisy_speech )
     296             :     {
     297           0 :         hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     298           0 :         move16();
     299           0 :         move16();
     300             :     }
     301             :     ELSE
     302             :     {
     303           7 :         push_indice( hBstr, IND_NOISE_LEVEL, sub( hGSCEnc->noise_lev, NOISE_LEVEL_SP0 ), 3 );
     304             :     }
     305             : 
     306             :     /*---------------------------------------------------------------*
     307             :      * Find and encode the last band where the adaptive (pitch) contribution is significant
     308             :      *---------------------------------------------------------------*/
     309             : 
     310           7 :     last_pit_bin = Pit_exc_contribution_len_fx( st_fx, dct_res, dct_epit, pitch_buf, nb_subfr, &hGSCEnc->pit_exc_hangover, Q_new );
     311             : 
     312           7 :     IF( last_pit_bin == 0 )
     313             :     {
     314           0 :         hLPDmem->tilt_code = 0;
     315           0 :         move16();
     316             :     }
     317             :     ELSE
     318             :     {
     319             :         /*last_pit_bin++;*/
     320           7 :         last_pit_bin = add( last_pit_bin, 1 );
     321             :     }
     322             : 
     323             :     /*--------------------------------------------------------------------------------------*
     324             :      * GSC encoder
     325             :      *--------------------------------------------------------------------------------------*/
     326             : 
     327             :     /* Find the current total number of bits used */
     328           7 :     tmp_nb_bits_tot = hBstr->nb_bits_tot;
     329           7 :     move16();
     330             : 
     331             : 
     332           7 :     if ( st_fx->extl_brate > 0 )
     333             :     {
     334             :         /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
     335           7 :         tmp_nb_bits_tot = sub( tmp_nb_bits_tot, 1 );
     336             :     }
     337           7 :     test();
     338           7 :     test();
     339           7 :     if ( EQ_16( st_fx->coder_type, INACTIVE ) && LE_32( st_fx->core_brate, ACELP_9k60 ) && st_fx->idchan == 0 )
     340             :     {
     341             :         /* add 5 bits for noisiness */
     342           0 :         tmp_nb_bits_tot = add( tmp_nb_bits_tot, 5 );
     343             :     }
     344             : 
     345           7 :     gsc_enc_fx( st_fx, dct_res, dct_epit, last_pit_bin, tmp_nb_bits_tot, nb_subfr, lsf_new, exc_wo_nf, tmp_noise, Q_new );
     346             : 
     347             :     /*--------------------------------------------------------------------------------------*
     348             :      * iDCT transform
     349             :      *--------------------------------------------------------------------------------------*/
     350             : 
     351           7 :     edct_16fx( dct_epit, exc, st_fx->L_frame, 7, st_fx->element_mode );
     352           7 :     edct_16fx( exc_wo_nf, exc_wo_nf, st_fx->L_frame, 7, st_fx->element_mode );
     353             :     /*--------------------------------------------------------------------------------------*
     354             :      * Remove potential pre-echo in case an onset has been detected
     355             :      *--------------------------------------------------------------------------------------*/
     356             : 
     357           7 :     pre_echo_att_fx( &hGSCEnc->Last_frame_ener_fx, exc, attack_flag, Q_new, st_fx->last_coder_type, st_fx->L_frame );
     358             : 
     359             :     /*--------------------------------------------------------------------------------------*
     360             :      * Update BWE excitation
     361             :      *--------------------------------------------------------------------------------------*/
     362           7 :     IF( st_fx->hBWE_TD != NULL )
     363             :     {
     364           7 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     365             :         {
     366           0 :             set16_fx( voice_factors, 0, NB_SUBFR16k );
     367           0 :             interp_code_4over2_fx( exc, bwe_exc, L_FRAME16k );
     368             :         }
     369             :         ELSE
     370             :         {
     371           7 :             set16_fx( voice_factors, 0, NB_SUBFR );
     372           7 :             interp_code_5over2_fx( exc, bwe_exc, L_FRAME );
     373             :         }
     374             :     }
     375             :     /*--------------------------------------------------------------------------------------*
     376             :      * Synthesis
     377             :      *--------------------------------------------------------------------------------------*/
     378             : 
     379           7 :     p_Aq = Aq;
     380          35 :     FOR( i_subfr = 0; i_subfr < st_fx->L_frame; i_subfr += L_SUBFR )
     381             :     {
     382          28 :         Syn_filt_s( 1, p_Aq, M, &exc_wo_nf[i_subfr], &synth[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
     383          28 :         p_Aq += ( M + 1 );
     384             :     }
     385             : 
     386             :     /*--------------------------------------------------------------------------------------*
     387             :      * Updates
     388             :      *--------------------------------------------------------------------------------------*/
     389             : 
     390           7 :     hLPDmem->mem_w0 = hGSCEnc->mem_w0_tmp_fx; /*_DIFF_FLOAT_FIX_  The way it is written in the original fix point is that at this point mem_w0 falls back to its original value (before enc_pit_exc, seems not the case in float */
     391           7 :     move16();
     392           7 :     Copy( exc_wo_nf, exc, st_fx->L_frame );
     393             : 
     394           7 :     return;
     395             : }
     396             : 
     397       15159 : void encod_audio_ivas_fx(
     398             :     Encoder_State *st_fx,              /* i/o: State structure                                          */
     399             :     const Word16 speech[],             /* i  : input speech                                Q_new   */
     400             :     const Word16 Aw[],                 /* i  : weighted A(z) unquantized for subframes      Q12       */
     401             :     const Word16 Aq[],                 /* i  : 12k8 Lp coefficient                           Q12      */
     402             :     const Word16 *res,                 /* i  : residual signal                             Q_new   */
     403             :     Word16 *synth,                     /* i/o: core synthesis                              Q-1     */
     404             :     Word16 *exc,                       /* i/o: current non-enhanced excitation             Q_new   */
     405             :     Word16 *pitch_buf,                 /* i/o: floating pitch values for each subframe     Q6           */
     406             :     Word16 *voice_factors,             /* o  : voicing factors                             Q15          */
     407             :     Word16 *bwe_exc,                   /* o  : excitation for SWB TBE                                           Q0              */
     408             :     const Word16 attack_flag,          /* i  : Flag that point to an attack coded with AC mode (GSC)    */
     409             :     Word16 *lsf_new,                   /* i  : current frame ISF vector                                                 Qx2.56  */
     410             :     Word16 *tmp_noise,                 /* o  : noise energy   Q8*/
     411             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag             */
     412             :     const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                 */
     413             :     Word16 Q_new,
     414             :     Word16 shift )
     415             : {
     416             :     const Word16 *p_Aq;
     417             :     Word16 i, i_subfr, nb_subfr, last_pit_bin;
     418             :     Word16 T0_tmp, T0_frac_tmp, nb_subfr_flag;
     419       15159 :     Word16 tmp_nb_bits_tot = 0;
     420             :     Word16 Es_pred;
     421             :     Word16 dct_res[L_FRAME16k], dct_epit[L_FRAME16k];
     422       15159 :     Word16 m_mean = 0;
     423             :     Word16 saved_bit_pos;
     424             :     Word16 exc_wo_nf[L_FRAME16k];
     425             :     Word32 Lm_mean;
     426             :     Word16 nb_bits;
     427             :     Word16 indice;
     428       15159 :     SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
     429       15159 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
     430       15159 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     431       15159 :     LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
     432             : 
     433       15159 :     m_mean = 0;
     434       15159 :     move16();
     435       15159 :     tmp_nb_bits_tot = 0;
     436       15159 :     move16();
     437             : 
     438       15159 :     T0_tmp = 64;
     439       15159 :     move16();
     440       15159 :     T0_frac_tmp = 0;
     441       15159 :     move16();
     442       15159 :     Copy_Scale_sig( hLPDmem->mem_syn, hGSCEnc->mem_syn_tmp_fx, M, sub( sub( Q_new, 1 ), hLPDmem->q_mem_syn ) );
     443       15159 :     Copy_Scale_sig( &hLPDmem->mem_w0, &hGSCEnc->mem_w0_tmp_fx, 1, sub( sub( Q_new, 1 ), hLPDmem->q_mem_syn ) );
     444       15159 :     move16();
     445       15159 :     Es_pred = 0;
     446       15159 :     move16();
     447             : 
     448             :     /*---------------------------------------------------------------*
     449             :      * Encode GSC IVAS mode
     450             :      * Encode GSC attack flag (used to reduce possible pre-echo)
     451             :      * Encode GSC SWB speech flag
     452             :      *---------------------------------------------------------------*/
     453             : 
     454       15159 :     test();
     455       15159 :     if ( ( st_fx->element_mode > EVS_MONO ) && st_fx->idchan == 0 )
     456             :     {
     457       15159 :         push_indice( hBstr, IND_GSC_IVAS_SP, st_fx->GSC_IVAS_mode, 2 );
     458             :     }
     459             : 
     460       15159 :     IF( attack_flag > 0 )
     461             :     {
     462           0 :         push_indice( hBstr, IND_GSC_ATTACK, 1, 1 );
     463             :     }
     464             :     ELSE
     465             :     {
     466       15159 :         push_indice( hBstr, IND_GSC_ATTACK, 0, 1 );
     467             :     }
     468             : 
     469             : 
     470       15159 :     test();
     471       15159 :     test();
     472       15159 :     test();
     473       15159 :     test();
     474       15159 :     test();
     475       15159 :     test();
     476       15159 :     test();
     477       15159 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) || ( NE_16( st_fx->coder_type, INACTIVE ) && ( ( EQ_16( st_fx->element_mode, EVS_MONO ) && GE_32( st_fx->total_brate, ACELP_13k20 ) ) ||
     478             :                                                                                         ( GT_16( st_fx->element_mode, EVS_MONO ) && GT_32( st_fx->total_brate, MIN_BRATE_GSC_NOISY_FLAG ) && GE_16( st_fx->bwidth, SWB ) && !st_fx->flag_ACELP16k ) ) ) )
     479             :     {
     480        5630 :         push_indice( hBstr, IND_GSC_SWB_SPEECH, st_fx->GSC_noisy_speech, 1 );
     481             :     }
     482             :     /*---------------------------------------------------------------*
     483             :      * Find and encode the number of subframes
     484             :      *---------------------------------------------------------------*/
     485       15159 :     test();
     486       15159 :     IF( GE_32( st_fx->core_brate, ACELP_9k60 ) && LE_32( st_fx->core_brate, ACELP_13k20 ) )
     487             :     {
     488       21594 :         FOR( i = 0; i < 5; i++ )
     489             :         {
     490       17995 :             test();
     491       17995 :             if ( GT_16( abs_s( hSpMusClas->gsc_lt_diff_etot_fx[MAX_LT - i - 1] ), 1536 ) && EQ_16( hGSCEnc->cor_strong_limit, 1 ) )
     492             :             {
     493         101 :                 hGSCEnc->cor_strong_limit = 0;
     494         101 :                 move16();
     495             :             }
     496             :         }
     497             :     }
     498       15159 :     test();
     499       15159 :     test();
     500       15159 :     test();
     501       15159 :     test();
     502       15159 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) || ( st_fx->GSC_noisy_speech && st_fx->GSC_IVAS_mode == 0 ) )
     503             :     {
     504        5628 :         nb_subfr = NB_SUBFR;
     505        5628 :         move16();
     506        5628 :         test();
     507        5628 :         test();
     508        5628 :         if ( st_fx->GSC_IVAS_mode > 0 && LT_16( st_fx->GSC_IVAS_mode, 3 ) && LT_32( st_fx->core_brate, GSC_L_RATE_STG ) )
     509             :         {
     510        2104 :             nb_subfr = 2;
     511        2104 :             move16();
     512             :         }
     513        5628 :         hGSCEnc->cor_strong_limit = 0;
     514        5628 :         move16();
     515        5628 :         nb_subfr_flag = 1;
     516        5628 :         move16();
     517             :     }
     518        9531 :     ELSE IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && ( LE_32( st_fx->core_brate, ACELP_13k20 ) || EQ_16( st_fx->coder_type, INACTIVE ) ) )
     519             :     {
     520        2650 :         hGSCEnc->cor_strong_limit = 0;
     521        2650 :         nb_subfr = SWNB_SUBFR;
     522        2650 :         nb_subfr_flag = 1;
     523        2650 :         move16();
     524        2650 :         move16();
     525        2650 :         move16();
     526             :     }
     527             :     ELSE
     528             :     {
     529        6881 :         test();
     530        6881 :         test();
     531        6881 :         IF( ( hGSCEnc->cor_strong_limit == 0 || EQ_16( st_fx->coder_type, INACTIVE ) ) && GE_32( st_fx->core_brate, ACELP_9k60 ) )
     532             :         {
     533        1283 :             nb_subfr = 2;
     534        1283 :             move16();
     535        1283 :             nb_subfr_flag = 0;
     536        1283 :             move16();
     537        1283 :             hGSCEnc->cor_strong_limit = 0;
     538        1283 :             move16();
     539             :         }
     540             :         ELSE
     541             :         {
     542        5598 :             nb_subfr = SWNB_SUBFR;
     543        5598 :             move16();
     544        5598 :             nb_subfr_flag = 1;
     545        5598 :             move16();
     546             :         }
     547             : 
     548        6881 :         test();
     549        6881 :         test();
     550        6881 :         test();
     551        6881 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && ( GT_16( hSpMusClas->mold_corr_fx, 26214 ) && GE_32( st_fx->core_brate, MIN_RATE_4SBFR ) && NE_16( st_fx->coder_type, INACTIVE ) ) )
     552             :         {
     553           0 :             nb_subfr = shl( nb_subfr, 1 );
     554           0 :             nb_subfr_flag |= 0x2;
     555           0 :             logic16();
     556             :         }
     557             : 
     558        6881 :         test();
     559        6881 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && GE_32( st_fx->core_brate, MIN_RATE_4SBFR ) )
     560             :         {
     561           0 :             push_indice( hBstr, IND_HF_NOISE, nb_subfr_flag, 2 );
     562             :         }
     563        6881 :         ELSE IF( GE_32( st_fx->core_brate, ACELP_9k60 ) )
     564             :         {
     565             :             /* nb_subfr_flag can only have the value 0 or 1 */
     566        1292 :             push_indice( hBstr, IND_HF_NOISE, nb_subfr_flag, 1 );
     567             :         }
     568             :     }
     569       15159 :     test();
     570       15159 :     if ( EQ_16( st_fx->L_frame, L_FRAME16k ) && EQ_16( nb_subfr, NB_SUBFR ) )
     571             :     {
     572           0 :         nb_subfr = NB_SUBFR16k;
     573           0 :         move16();
     574             :     }
     575             : 
     576             :     /*---------------------------------------------------------------*
     577             :      * Compute adaptive (pitch) excitation contribution
     578             :      *---------------------------------------------------------------*/
     579             : 
     580       15159 :     test();
     581       15159 :     test();
     582       15159 :     test();
     583       15159 :     test();
     584       15159 :     test();
     585       15159 :     test();
     586       15159 :     test();
     587       15159 :     test();
     588       15159 :     IF( !( st_fx->GSC_IVAS_mode > 0 && EQ_16( st_fx->L_frame, i_mult( nb_subfr, 2 * L_SUBFR ) ) && LT_16( st_fx->GSC_IVAS_mode, 3 ) ) &&
     589             :         ( ( GE_32( st_fx->core_brate, MIN_RATE_FCB ) || st_fx->GSC_noisy_speech ) &&
     590             :           ( ( EQ_16( nb_subfr, NB_SUBFR ) && EQ_16( st_fx->L_frame, L_FRAME ) ) || ( EQ_16( nb_subfr, NB_SUBFR16k ) && EQ_16( st_fx->L_frame, L_FRAME16k ) ) ) ) )
     591             :     {
     592         696 :         IF( ( st_fx->element_mode > EVS_MONO ) )
     593             :         {
     594         696 :             nb_bits = 5;
     595         696 :             move16();
     596             :         }
     597             :         ELSE
     598             :         {
     599           0 :             nb_bits = Es_pred_bits_tbl[BIT_ALLOC_IDX_fx( st_fx->core_brate, GENERIC, -1, -1 )];
     600           0 :             move16();
     601             :         }
     602         696 :         Es_pred_enc_fx( &Es_pred, &indice, st_fx->L_frame, res, st_fx->voicing_fx, nb_bits, 0, Q_new );
     603         696 :         push_indice( hBstr, IND_ES_PRED, indice, nb_bits );
     604             :     }
     605             : 
     606       15159 :     enc_pit_exc_ivas_fx( st_fx, speech, Aw, Aq, Es_pred, res, synth, exc, &T0_tmp,
     607             :                          &T0_frac_tmp, pitch_buf, nb_subfr, &hGSCEnc->lt_gpitch_fx, &saved_bit_pos, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, Q_new, shift );
     608             : 
     609             :     /*---------------------------------------------------------------*
     610             :      * DCT transform
     611             :      *---------------------------------------------------------------*/
     612       15159 :     edct_16fx( exc, dct_epit, st_fx->L_frame, 7, st_fx->element_mode );
     613       15159 :     edct_16fx( res, dct_res, st_fx->L_frame, 7, st_fx->element_mode );
     614             :     /*---------------------------------------------------------------*
     615             :      * Calculate energy dynamics
     616             :      *---------------------------------------------------------------*/
     617       15159 :     Lm_mean = L_deposit_l( 0 );
     618      136431 :     FOR( i = 7; i < 15; i++ )
     619             :     {
     620             :         /*m_mean = add(m_mean,edyn_fx( dct_res+i*16, 16, Q_new )); */
     621      121272 :         Lm_mean = L_mac( Lm_mean, edyn_fx( dct_res + i * 16, 16, Q_new ), 4096 ); /*Q7*/
     622             :     }
     623       15159 :     m_mean = round_fx( Lm_mean ); /*Q7*/
     624             : 
     625       15159 :     IF( GT_16( m_mean, hGSCEnc->mid_dyn_fx ) )
     626             :     {
     627             :         /*st_fx->mid_dyn_fx = 0.2f * st_fx->mid_dyn_fx + 0.8f * m_mean;*/
     628        6884 :         hGSCEnc->mid_dyn_fx = round_fx( L_mac( L_mult( 26214, m_mean ), 6554, hGSCEnc->mid_dyn_fx ) ); /*Q7*/
     629        6884 :         move16();
     630             :     }
     631             :     ELSE
     632             :     {
     633             :         /*st_fx->mid_dyn_fx = 0.6f * st_fx->mid_dyn_fx + 0.4f * m_mean;*/
     634        8275 :         hGSCEnc->mid_dyn_fx = round_fx( L_mac( L_mult( 13107, m_mean ), 19661, hGSCEnc->mid_dyn_fx ) ); /*Q7*/
     635        8275 :         move16();
     636             :     }
     637       15159 :     IF( NE_16( st_fx->coder_type, INACTIVE ) )
     638             :     {
     639       10985 :         hGSCEnc->noise_lev = sub( ( NOISE_LEVEL_SP3 + 1 ), usquant_fx( hGSCEnc->mid_dyn_fx, &m_mean, MIN_DYNAMIC_FX, shr( GSF_NF_DELTA_FX, 1 ), GSC_NF_STEPS ) );
     640       10985 :         move16();
     641       10985 :         hGSCEnc->noise_lev = s_min( hGSCEnc->noise_lev, NOISE_LEVEL_SP3 );
     642       10985 :         move16();
     643             :     }
     644             : 
     645       15159 :     hGSCEnc->past_dyn_dec = hGSCEnc->noise_lev;
     646       15159 :     move16();
     647       15159 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) )
     648             :     {
     649        4932 :         hGSCEnc->noise_lev = NOISE_LEVEL_SP2;
     650        4932 :         move16();
     651        4932 :         IF( EQ_16( st_fx->GSC_IVAS_mode, 3 ) ) /* Music like */
     652             :         {
     653        2804 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP0;
     654        2804 :             move16();
     655             :         }
     656        2128 :         ELSE IF( st_fx->GSC_noisy_speech == 0 )
     657             :         {
     658        2016 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     659        2016 :             move16();
     660             :         }
     661             :     }
     662       10227 :     ELSE IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
     663             :     {
     664        5067 :         hGSCEnc->noise_lev = s_max( hGSCEnc->noise_lev, NOISE_LEVEL_SP2 );
     665        5067 :         push_indice( hBstr, IND_NOISE_LEVEL, sub( hGSCEnc->noise_lev, NOISE_LEVEL_SP2 ), 2 );
     666             :     }
     667        5160 :     ELSE IF( st_fx->GSC_noisy_speech )
     668             :     {
     669         696 :         hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     670         696 :         move16();
     671             :     }
     672             :     ELSE
     673             :     {
     674        4464 :         push_indice( hBstr, IND_NOISE_LEVEL, sub( hGSCEnc->noise_lev, NOISE_LEVEL_SP0 ), 3 );
     675             :     }
     676             : 
     677             :     /*---------------------------------------------------------------*
     678             :      * Find and encode the last band where the adaptive (pitch) contribution is significant
     679             :      *---------------------------------------------------------------*/
     680             : 
     681       15159 :     last_pit_bin = Pit_exc_contribution_len_ivas_fx( st_fx, dct_res, dct_epit, pitch_buf, nb_subfr, &hGSCEnc->pit_exc_hangover, Q_new );
     682             : 
     683       15159 :     IF( last_pit_bin == 0 )
     684             :     {
     685        2251 :         hLPDmem->tilt_code = 0;
     686        2251 :         move16();
     687             :     }
     688             :     ELSE
     689             :     {
     690             :         /*last_pit_bin++;*/
     691       12908 :         last_pit_bin = add( last_pit_bin, 1 );
     692             :     }
     693             : 
     694             :     /*--------------------------------------------------------------------------------------*
     695             :      * GSC encoder
     696             :      *--------------------------------------------------------------------------------------*/
     697             : 
     698             :     /* Find the current total number of bits used */
     699       15159 :     tmp_nb_bits_tot = hBstr->nb_bits_tot;
     700       15159 :     move16();
     701             : 
     702             : 
     703       15159 :     if ( st_fx->extl_brate > 0 )
     704             :     {
     705             :         /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
     706       15043 :         tmp_nb_bits_tot = sub( tmp_nb_bits_tot, 1 );
     707             :     }
     708       15159 :     test();
     709       15159 :     test();
     710       15159 :     if ( EQ_16( st_fx->coder_type, INACTIVE ) && LE_32( st_fx->core_brate, ACELP_9k60 ) && st_fx->idchan == 0 )
     711             :     {
     712             :         /* add 5 bits for noisiness */
     713         339 :         tmp_nb_bits_tot = add( tmp_nb_bits_tot, 5 );
     714             :     }
     715             : 
     716       15159 :     Word16 Q_exc = Q_new;
     717       15159 :     move16();
     718       15159 :     gsc_enc_ivas_fx( st_fx, dct_res, dct_epit, last_pit_bin, tmp_nb_bits_tot, nb_subfr, lsf_new, exc_wo_nf, tmp_noise, &Q_exc );
     719             : 
     720             :     /*--------------------------------------------------------------------------------------*
     721             :      * iDCT transform
     722             :      *--------------------------------------------------------------------------------------*/
     723             : 
     724       15159 :     edct_16fx( dct_epit, exc, st_fx->L_frame, 7, st_fx->element_mode );
     725       15159 :     Scale_sig( exc, st_fx->L_frame, sub( Q_new, Q_exc ) );
     726       15159 :     edct_16fx( exc_wo_nf, exc_wo_nf, st_fx->L_frame, 7, st_fx->element_mode );
     727       15159 :     Scale_sig( exc_wo_nf, st_fx->L_frame, sub( Q_new, Q_exc ) );
     728       15159 :     Q_exc = Q_new;
     729       15159 :     move16();
     730             :     /*--------------------------------------------------------------------------------------*
     731             :      * Remove potential pre-echo in case an onset has been detected
     732             :      *--------------------------------------------------------------------------------------*/
     733             : 
     734       15159 :     pre_echo_att_ivas_fx( &hGSCEnc->Last_frame_ener_fx, exc, attack_flag, Q_exc, st_fx->last_coder_type, st_fx->L_frame );
     735             : 
     736             :     /*--------------------------------------------------------------------------------------*
     737             :      * Update BWE excitation
     738             :      *--------------------------------------------------------------------------------------*/
     739       15159 :     IF( st_fx->hBWE_TD != NULL )
     740             :     {
     741       15159 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     742             :         {
     743        2650 :             set16_fx( voice_factors, 0, NB_SUBFR16k );
     744        2650 :             interp_code_4over2_fx( exc, bwe_exc, L_FRAME16k );
     745             :         }
     746             :         ELSE
     747             :         {
     748       12509 :             set16_fx( voice_factors, 0, NB_SUBFR );
     749       12509 :             interp_code_5over2_fx( exc, bwe_exc, L_FRAME );
     750             :         }
     751             :     }
     752             :     /*--------------------------------------------------------------------------------------*
     753             :      * Synthesis
     754             :      *--------------------------------------------------------------------------------------*/
     755             : 
     756       15159 :     p_Aq = Aq; /* Q12 */
     757       78445 :     FOR( i_subfr = 0; i_subfr < st_fx->L_frame; i_subfr += L_SUBFR )
     758             :     {
     759       63286 :         Syn_filt_s( 1, p_Aq, M, &exc_wo_nf[i_subfr], &synth[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
     760       63286 :         p_Aq += ( M + 1 );
     761             :     }
     762             : 
     763             :     /*--------------------------------------------------------------------------------------*
     764             :      * Updates
     765             :      *--------------------------------------------------------------------------------------*/
     766       15159 :     Copy_Scale_sig( &hGSCEnc->mem_w0_tmp_fx, &hLPDmem->mem_w0, 1, sub( hLPDmem->q_mem_syn, sub( Q_new, 1 ) ) );
     767             :     /*_DIFF_FLOAT_FIX_  The way it is written in the original fix point is that at this point mem_w0 falls back to its original value (before enc_pit_exc, seems not the case in float */
     768       15159 :     move16();
     769       15159 :     Copy( exc_wo_nf, exc, st_fx->L_frame ); /* Q_new */
     770             : 
     771       15159 :     return;
     772             : }
     773             : 
     774             : /*================================================================================*/
     775             : /* FUNCTION : void gsc_enc_fx   ()                                                                        */
     776             : /*--------------------------------------------------------------------------------*/
     777             : /* PURPOSE  :  Generic audio signal encoder                                       */
     778             : /*--------------------------------------------------------------------------------*/
     779             : /*    INPUT ARGUMENTS :                                                                                                                   */
     780             : /* _ (Word16) res_dct_in                 : dct of residual signal                                Q_exc    */
     781             : /* _ (Word16) Diff_len           : Lenght of the difference signal       Q0       */
     782             : /* _ (Word16) coder_type         : coding type                                       Q0       */
     783             : /* _ (Word16) bits_used          : Number of bit used before frequency Q Q0       */
     784             : /* _ (Word16) nb_subfr           : Number of subframe considered         Q0       */
     785             : /* _ (Word16)  Qexc              : Q format of exc_dct_in                                     */
     786             : /*--------------------------------------------------------------------------------*/
     787             : /* OUTPUT ARGUMENTS :                                                                                                             */
     788             : /* _ None                                                                         */
     789             : /*--------------------------------------------------------------------------------*/
     790             : /* INPUT/OUTPUT ARGUMENTS :                                                                                               */
     791             : /*   Encoder_State *st_fx:Encoder State Structure                              */
     792             : /* _ (Word16[]) exc_dct_in : dctof pitch-only excitation / total excitation Q_exc */
     793             : /*--------------------------------------------------------------------------------*/
     794             : /* RETURN ARGUMENTS :                                                                                                             */
     795             : /* _None                                                                                  */
     796             : /*================================================================================*/
     797             : 
     798             : 
     799           7 : void gsc_enc_fx(
     800             :     Encoder_State *st_fx, /* i/o: State structure                               */
     801             :     Word16 res_dct_in[],  /* i  : dct of residual signal                        Q_exc*/
     802             :     Word16 exc_dct_in[],  /* i/o: dct of pitch-only excitation / total excitation Q_exc*/
     803             :     const Word16 Diff_len,
     804             :     const Word16 bits_used,
     805             :     const Word16 nb_subfr,
     806             :     Word16 *lsf_new,   /* i  : ISFs at the end of the frame                  Qx2.56*/
     807             :     Word16 *exc_wo_nf, /* o  : excitation (in f domain) without noisefill    Q_exc*/
     808             :     Word16 *tmp_noise, /* o  : noise energy                                  Q2*/
     809             :     Word16 Q_exc )
     810             : {
     811             :     Word16 y2_filt[L_FRAME16k];
     812             :     Word16 exc_diffQ[L_FRAME16k];
     813             :     Word16 exc_diff[L_FRAME16k];
     814             :     Word16 bit, tmp;
     815             :     Word16 nb_subbands;
     816             :     Word16 pvq_len, i;
     817             :     Word16 bits_per_bands[MBANDS_GN_BITALLOC16k];
     818             :     Word16 tmp_band;
     819             :     Word16 concat_in[L_FRAME16k];
     820             :     Word16 concat_out[L_FRAME16k];
     821             :     Word16 max_ener_band[MBANDS_GN_BITALLOC16k], j;
     822             :     Word16 Ener_per_bd_iQ[MBANDS_GN_BITALLOC16k];
     823             :     Word16 last_bin, mean_gain;
     824             :     Word16 bitallocation_band[MBANDS_GN_BITALLOC16k];
     825             :     Word16 bitallocation_exc[2];
     826             : 
     827             :     Word16 inpulses_fx[NB_SFM];
     828             :     Word16 imaxpulse_fx[NB_SFM];
     829             :     Word16 Q_tmp;
     830             :     Word16 seed_init;
     831           7 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
     832           7 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     833             : 
     834           7 :     set16_fx( inpulses_fx, 0, NB_SFM );
     835           7 :     set16_fx( imaxpulse_fx, 0, NB_SFM );
     836             : 
     837             :     /*--------------------------------------------------------------------------------------*
     838             :      * Initialization
     839             :      *--------------------------------------------------------------------------------------*/
     840             : 
     841           7 :     bit = bits_used;
     842           7 :     move16();
     843           7 :     test();
     844           7 :     test();
     845           7 :     test();
     846           7 :     if ( EQ_16( st_fx->coder_type, INACTIVE ) && ( EQ_16( st_fx->tdm_LRTD_flag, 1 ) || EQ_16( st_fx->element_mode, IVAS_SCE ) ) && LE_32( st_fx->core_brate, GSC_LRES_GAINQ_LIMIT ) )
     847             :     {
     848           0 :         bit = add( bit, GSC_LRES_NB_NITS );
     849             :     }
     850           7 :     move16();
     851           7 :     set16_fx( exc_diffQ, 0, st_fx->L_frame );
     852           7 :     set16_fx( y2_filt, 0, st_fx->L_frame );
     853             : 
     854             :     /*--------------------------------------------------------------------------------------*
     855             :      * Calculate the difference between the residual spectrum and the spectrum of adaptive excitation
     856             :      * (non valuable temporal content present in exc_dct_in is already zeroed)
     857             :      *--------------------------------------------------------------------------------------*/
     858             : 
     859           7 :     Vr_subt( res_dct_in, exc_dct_in, exc_diff, st_fx->L_frame );
     860           7 :     exc_diff[0] = 0;
     861           7 :     move16();
     862             : 
     863             :     /*--------------------------------------------------------------------------------------*
     864             :      * Multiply the difference spectrum with the normalized spectral shape of the residual signal
     865             :      * This improves the stability of the differnece spectrum since the spectral shape of the
     866             :      * residual signal is less suseptible to rapid changes than the difference spectrum
     867             :      *--------------------------------------------------------------------------------------*/
     868           7 :     IF( Diff_len == 0 )
     869             :     {
     870           0 :         tmp_band = 0;
     871           0 :         move16();
     872             :     }
     873             :     ELSE
     874             :     {
     875           7 :         tmp_band = hGSCEnc->mem_last_pit_band;
     876           7 :         move16();
     877             :     }
     878             : 
     879           7 :     Ener_per_band_comp_fx( exc_diff, Ener_per_bd_iQ, Q_exc, MBANDS_GN, 1 /*, st_fx->L_frame IVAS_CODE*/ );
     880             : 
     881             :     /*--------------------------------------------------------------------------------------*
     882             :      * Gain quantizaion
     883             :      *--------------------------------------------------------------------------------------*/
     884           7 :     i = 0;
     885           7 :     move16();
     886          42 :     WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) )
     887             :     {
     888          42 :         IF( LE_32( st_fx->core_brate, brate_intermed_tbl[i] ) )
     889             :         {
     890           7 :             BREAK;
     891             :         }
     892          35 :         i++;
     893             :     }
     894             : 
     895           7 :     test();
     896           7 :     test();
     897           7 :     test();
     898           7 :     if ( GT_16( st_fx->element_mode, EVS_MONO ) && EQ_16( st_fx->coder_type, AUDIO ) &&
     899           0 :          LE_32( st_fx->core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
     900             :     {
     901           0 :         i--;
     902             :     }
     903             : 
     904           7 :     mean_gain = gsc_gainQ_fx( hBstr, /*st_fX->element_mode, st_fx->idchan,IVAS_CODE*/ Ener_per_bd_iQ, Ener_per_bd_iQ, brate_intermed_tbl[i], st_fx->coder_type, st_fx->bwidth /*, st_fx->L_frame, st_fx->tdm_LRTD_flag, st_fx->core_brate*/ );
     905             : 
     906           7 :     *tmp_noise = mult_r( 320, mean_gain ); /*10 in Q5  lp_gainc in Q3 */
     907           7 :     move16();
     908             : 
     909             :     /*--------------------------------------------------------------------------------------*
     910             :      * Frequency encoder
     911             :      *--------------------------------------------------------------------------------------*/
     912             : 
     913           7 :     bands_and_bit_alloc_fx( hGSCEnc->cor_strong_limit, hGSCEnc->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ,
     914           7 :                             max_ener_band, bits_per_bands, &nb_subbands, exc_diff, concat_in, &pvq_len, st_fx->coder_type, st_fx->bwidth,
     915           7 :                             st_fx->GSC_noisy_speech, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode );
     916             : 
     917           7 :     Q_tmp = Q_exc;
     918           7 :     move16();
     919           7 :     IF( bit == 0 )
     920             :     {
     921           0 :         set16_fx( concat_out, 0, L_FRAME16k );
     922             :     }
     923             :     ELSE
     924             :     {
     925           7 :         tmp = pvq_core_enc_fx( hBstr, concat_in, concat_out, &Q_tmp, bit, nb_subbands, gsc_sfm_start, gsc_sfm_end,
     926             :                                gsc_sfm_size, bits_per_bands, NULL, inpulses_fx, imaxpulse_fx, ACELP_CORE );
     927           7 :         Scale_sig( concat_out, gsc_sfm_end[nb_subbands - 1], sub( Q_PVQ_OUT, Q_tmp ) ); /* Q_PVQ_OUT */
     928           7 :         bit = sub( bit, tmp );
     929             :     }
     930             :     /* write unused bits */
     931           7 :     WHILE( bit > 0 )
     932             :     {
     933           0 :         i = s_min( bit, 16 );
     934           0 :         push_indice( hBstr, IND_UNUSED, 0, i );
     935           0 :         bit = sub( bit, i );
     936             :     }
     937             :     /* Reorder Q bands */
     938           7 :     seed_init = 0;
     939           7 :     move16();
     940           7 :     last_bin = 0;
     941           7 :     move16();
     942           7 :     set16_fx( bitallocation_band, 0, MBANDS_GN_BITALLOC16k );
     943             : 
     944          63 :     FOR( j = 0; j < nb_subbands; j++ )
     945             :     {
     946          56 :         Copy( concat_out + j * 16, exc_diffQ + max_ener_band[j] * 16, 16 ); /*Q12*/
     947             : 
     948          56 :         last_bin = s_max( last_bin, max_ener_band[j] );
     949             : 
     950          56 :         bitallocation_band[max_ener_band[j]] = 1;
     951          56 :         move16();
     952             : 
     953          56 :         seed_init = add( seed_init, inpulses_fx[j] );
     954          56 :         move16();
     955             :     }
     956           7 :     test();
     957           7 :     IF( NE_16( st_fx->last_coder_type, AUDIO )         /* First audio frame */
     958             :         && NE_16( st_fx->last_coder_type, UNVOICED ) ) /* last_coder_type == INACTIVE is overwritten in update_enc to UNVOICED */
     959             :     {
     960         387 :         FOR( j = 0; j < shl( nb_subbands, 4 ); j++ )
     961             :         {
     962         384 :             IF( concat_out[j] > 0 )
     963             :             {
     964          49 :                 seed_init = extract_l( L_shl( seed_init, 3 ) );
     965             :             }
     966         384 :             IF( concat_out[j] < 0 )
     967             :             {
     968          52 :                 seed_init = add( seed_init, 3 );
     969          52 :                 move16();
     970             :             }
     971             :         }
     972           3 :         hGSCEnc->seed_tcx = seed_init;
     973           3 :         move16();
     974             :     }
     975           7 :     test();
     976           7 :     IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && NE_16( st_fx->bwidth, NB ) )
     977             :     {
     978           0 :         bitallocation_exc[0] = 0;
     979           0 :         move16();
     980           0 :         bitallocation_exc[1] = 0;
     981           0 :         move16();
     982           0 :         if ( exc_diffQ[L_FRAME8k - 2] != 0 )
     983             :         {
     984           0 :             bitallocation_exc[0] = 1;
     985           0 :             move16();
     986             :         }
     987           0 :         if ( exc_diffQ[L_FRAME8k - 1] != 0 )
     988             :         {
     989           0 :             bitallocation_exc[1] = 1;
     990           0 :             move16();
     991             :         }
     992             :     }
     993             : 
     994             :     /*--------------------------------------------------------------------------------------*
     995             :      * Skip adaptive (pitch) contribution frequency band (no noise added over the adaptive (pitch) contribution)
     996             :      * Find x pulses between 1.6-3.2kHz to code in the spectrum of the residual signal
     997             :      * Gain is based on the inter-correlation gain between the pulses found and residual signal
     998             :      *--------------------------------------------------------------------------------------*/
     999             : 
    1000             :     {
    1001           7 :         freq_dnw_scaling_fx( hGSCEnc->cor_strong_limit, st_fx->coder_type, hGSCEnc->noise_lev, st_fx->core_brate, exc_diffQ, Q_PVQ_OUT, st_fx->L_frame );
    1002             :     }
    1003             :     /*--------------------------------------------------------------------------------------*
    1004             :      * Estimate noise level
    1005             :      *--------------------------------------------------------------------------------------*/
    1006             : 
    1007           7 :     highband_exc_dct_in_fx( st_fx->core_brate, mfreq_bindiv_loc, last_bin, Diff_len, hGSCEnc->noise_lev, tmp_band, exc_diffQ,
    1008           7 :                             &hGSCEnc->seed_tcx, Ener_per_bd_iQ, nb_subfr, exc_dct_in, st_fx->last_coder_type, bitallocation_band, lsf_new,
    1009           7 :                             hGSCEnc->last_exc_dct_in_fx, &st_fx->last_ener_fx, hGSCEnc->last_bitallocation_band, bitallocation_exc, 0, st_fx->coder_type,
    1010           7 :                             st_fx->bwidth, exc_wo_nf, Q_PVQ_OUT, Q_exc, st_fx->GSC_noisy_speech, NULL, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode );
    1011             : 
    1012           7 :     exc_dct_in[0] = 0;
    1013           7 :     move16();
    1014             : 
    1015           7 :     return;
    1016             : }
    1017             : 
    1018             : 
    1019       22696 : void gsc_enc_ivas_fx(
    1020             :     Encoder_State *st, /* i/o: State structure                                       */
    1021             :     Word16 res_dct_in_fx[],
    1022             :     /* i  : dct of residual signal                                */ // Q_exc
    1023             :     Word16 exc_dct_in_fx[],
    1024             :     /* i/o: dct of pitch-only excitation / total excitation       */ // Q_exc
    1025             :     const Word16 Diff_len,                                           /* i  : Lenght of the difference signal (before pure spectral)*/
    1026             :     const Word16 bits_used,                                          /* i  : Number of bit used before frequency Q                 */
    1027             :     const Word16 nb_subfr,                                           /* i  : Number of subframe considered                         */
    1028             :     const Word16 *lsf_new_fx,                                        /* Qx2.56 */
    1029             :     /* i  : ISFs at the end of the frame                          */ // Q15
    1030             :     Word16 *exc_wo_nf_fx,
    1031             :     /* o  : excitation (in f domain) without noisefill            */ // Q_exc
    1032             :     Word16 *tmp_noise_fx,                                            /* o  : long-term noise energy                                Q8*/
    1033             :     Word16 *Q_exc )
    1034             : {
    1035             :     Word16 i;
    1036             :     GSC_ENC_HANDLE hGSCEnc;
    1037             :     BSTR_ENC_HANDLE hBstr;
    1038             :     Word16 exc_diffQ_fx[L_FRAME16k];
    1039             :     Word16 exc_diff_fx[L_FRAME16k];
    1040             :     Word16 bit;
    1041             :     Word16 nb_subbands;
    1042             :     Word16 pvq_len;
    1043             :     Word16 bits_per_bands[MBANDS_GN_BITALLOC16k]; /*Q3*/
    1044             :     Word16 tmp_band;
    1045             :     Word16 concat_in_fx[L_FRAME16k];
    1046             :     Word16 concat_out_fx[L_FRAME16k];
    1047             :     Word16 max_ener_band[MBANDS_GN_BITALLOC16k], j;
    1048             :     Word16 Ener_per_bd_iQ_fx[MBANDS_GN16k];
    1049             :     Word16 last_bin;
    1050             :     Word16 bitallocation_band[MBANDS_GN_BITALLOC16k];
    1051             :     Word16 bitallocation_exc[2];
    1052             :     Word16 npulses[NB_SFM];
    1053             :     Word16 maxpulse[NB_SFM];
    1054             :     Word16 mean_gain_fx;
    1055             :     Word16 seed_init;
    1056             :     Word32 L_tmp;
    1057             :     Word16 max_eq;
    1058             : 
    1059             :     /*--------------------------------------------------------------------------------------*
    1060             :      * Initialization
    1061             :      *--------------------------------------------------------------------------------------*/
    1062             : 
    1063       22696 :     hGSCEnc = st->hGSCEnc;
    1064       22696 :     hBstr = st->hBstr;
    1065             : 
    1066       22696 :     bit = bits_used;
    1067       22696 :     move16();
    1068             : 
    1069       22696 :     test();
    1070       22696 :     test();
    1071       22696 :     test();
    1072       22696 :     if ( st->coder_type == INACTIVE && ( EQ_16( st->tdm_LRTD_flag, 1 ) || EQ_16( st->element_mode, IVAS_SCE ) ) && LE_32( st->core_brate, GSC_LRES_GAINQ_LIMIT ) )
    1073             :     {
    1074        7027 :         bit = add( bit, GSC_LRES_NB_NITS );
    1075             :     }
    1076             : 
    1077       22696 :     set16_fx( exc_diffQ_fx, 0, st->L_frame );
    1078       22696 :     set16_fx( Ener_per_bd_iQ_fx, 0, MBANDS_GN16k );
    1079             : 
    1080             :     /*--------------------------------------------------------------------------------------*
    1081             :      * Calculate the difference between the residual spectrum and the spectrum of adaptive excitation
    1082             :      * (non valuable temporal content present in exc_dct_in is already zeroed)
    1083             :      *--------------------------------------------------------------------------------------*/
    1084             : 
    1085       22696 :     v_sub_16( res_dct_in_fx, exc_dct_in_fx, exc_diff_fx, st->L_frame ); /* Q_exc */
    1086       22696 :     exc_diff_fx[0] = 0;
    1087       22696 :     move16();
    1088             : 
    1089             :     /*--------------------------------------------------------------------------------------*
    1090             :      * Multiply the difference spectrum with the normalized spectral shape of the residual signal
    1091             :      * This improves the stability of the differnece spectrum since the spectral shape of the
    1092             :      * residual signal is less suseptible to rapid changes than the difference spectrum
    1093             :      *--------------------------------------------------------------------------------------*/
    1094             : 
    1095       22696 :     IF( Diff_len == 0 )
    1096             :     {
    1097        2251 :         tmp_band = 0;
    1098             :     }
    1099             :     ELSE
    1100             :     {
    1101       20445 :         tmp_band = hGSCEnc->mem_last_pit_band;
    1102             :     }
    1103       22696 :     move16();
    1104             : 
    1105       22696 :     Ener_per_band_comp_ivas_fx( exc_diff_fx, Ener_per_bd_iQ_fx, *Q_exc, MBANDS_GN, 1, st->L_frame );
    1106             : 
    1107             :     /*--------------------------------------------------------------------------------------*
    1108             :      * Gain quantizaion
    1109             :      *--------------------------------------------------------------------------------------*/
    1110             : 
    1111       22696 :     i = 0;
    1112       22696 :     move16();
    1113             : 
    1114      105866 :     WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) )
    1115             :     {
    1116      105866 :         IF( LE_32( st->core_brate, brate_intermed_tbl[i] ) )
    1117             :         {
    1118       22696 :             BREAK;
    1119             :         }
    1120       83170 :         i++;
    1121             :     }
    1122             : 
    1123       22696 :     test();
    1124       22696 :     test();
    1125       22696 :     test();
    1126       33681 :     if ( st->element_mode > EVS_MONO && EQ_16( st->coder_type, AUDIO ) &&
    1127       18026 :          LE_32( st->core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
    1128             :     {
    1129        1292 :         i--;
    1130             :     }
    1131             : 
    1132       22696 :     mean_gain_fx = gsc_gainQ_ivas_fx( hBstr, st->element_mode, st->idchan, Ener_per_bd_iQ_fx, Ener_per_bd_iQ_fx, brate_intermed_tbl[i], st->coder_type, st->bwidth, st->L_frame, st->tdm_LRTD_flag, st->core_brate );
    1133             : 
    1134       22696 :     *tmp_noise_fx = mult( mean_gain_fx, 20480 ); // 20480 => 10 in Q11
    1135       22696 :     move16();                                    // Q8
    1136             : 
    1137             :     /*--------------------------------------------------------------------------------------*
    1138             :      * PVQ encoder
    1139             :      *--------------------------------------------------------------------------------------*/
    1140             : 
    1141       22696 :     bands_and_bit_alloc_ivas_fx( hGSCEnc->cor_strong_limit, hGSCEnc->noise_lev, st->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ_fx, max_ener_band, bits_per_bands, &nb_subbands, exc_diff_fx, concat_in_fx, &pvq_len, st->coder_type, st->bwidth, st->GSC_noisy_speech, st->L_frame, st->element_mode, st->GSC_IVAS_mode );
    1142             : 
    1143       22696 :     set16_fx( npulses, 0, NB_SFM );
    1144             : 
    1145       22696 :     IF( bit == 0 )
    1146             :     {
    1147           0 :         set16_fx( concat_out_fx, 0, L_FRAME16k );
    1148             :     }
    1149             :     ELSE
    1150             :     {
    1151       22696 :         Word16 Q_concat = Q12;
    1152       22696 :         move16();
    1153       22696 :         bit = sub( bit, pvq_core_enc_ivas_fx( hBstr, concat_in_fx, concat_out_fx, &Q_concat, bit, nb_subbands, gsc_sfm_start, gsc_sfm_end, gsc_sfm_size, bits_per_bands, NULL, npulses, maxpulse, ACELP_CORE ) );
    1154             :     }
    1155             : 
    1156             :     /* write unused bits */
    1157       22696 :     WHILE( bit > 0 )
    1158             :     {
    1159           0 :         i = s_min( bit, 16 );
    1160           0 :         push_indice( hBstr, IND_UNUSED, 0, i );
    1161           0 :         bit = sub( bit, i );
    1162             :     }
    1163             : 
    1164             :     /* Reorder Q bands */
    1165       22696 :     last_bin = 0;
    1166       22696 :     move16();
    1167       22696 :     set16_fx( bitallocation_band, 0, MBANDS_GN_BITALLOC16k );
    1168       22696 :     seed_init = 0;
    1169       22696 :     move16();
    1170             : 
    1171       22696 :     max_eq = 0;
    1172       22696 :     move16();
    1173             : 
    1174       22696 :     test();
    1175       22696 :     test();
    1176       22696 :     test();
    1177       22696 :     test();
    1178       22696 :     IF( ( ( ( LT_32( st->core_brate, ACELP_7k20 ) && EQ_16( st->GSC_noisy_speech, 1 ) ) || LT_32( st->core_brate, 6000 ) ) && LE_16( st->coder_type, UNVOICED ) ) || GE_16( st->GSC_IVAS_mode, 1 ) )
    1179             :     {
    1180       12409 :         j = emaximum_fx( Q12, concat_out_fx, shl( nb_subbands, 4 ), &L_tmp );
    1181       12409 :         IF( LE_16( add( abs_s( concat_out_fx[j] ), 41 /* 0.01f in Q12 */ ), ONE_IN_Q12 ) )
    1182             :         {
    1183        7011 :             max_eq = 32767; /* Q15 */
    1184        7011 :             move16();
    1185             :         }
    1186             :         ELSE
    1187             :         {
    1188        5398 :             max_eq = div_s( ONE_IN_Q12, add( abs_s( concat_out_fx[j] ), 41 /* 0.01f in Q12 */ ) ); /* Q15 */
    1189             :         }
    1190             :     }
    1191             : 
    1192      185569 :     FOR( j = 0; j < nb_subbands; j++ )
    1193             :     {
    1194      162873 :         Copy( concat_out_fx + i_mult( j, 16 ), exc_diffQ_fx + i_mult( max_ener_band[j], 16 ), 16 ); /* Q12 */
    1195             : 
    1196      162873 :         IF( GT_16( max_ener_band[j], last_bin ) )
    1197             :         {
    1198      118075 :             last_bin = max_ener_band[j];
    1199      118075 :             move16();
    1200             :         }
    1201             : 
    1202      162873 :         bitallocation_band[max_ener_band[j]] = 1;
    1203      162873 :         move16();
    1204             : 
    1205      162873 :         seed_init = add( seed_init, npulses[j] );
    1206             :     }
    1207       22696 :     test();
    1208       22696 :     IF( NE_16( st->last_coder_type, AUDIO )         /* First audio frame */
    1209             :         && NE_16( st->last_coder_type, UNVOICED ) ) /* last_coder_type == INACTIVE is overwritten in update_enc to UNVOICED */
    1210             :     {
    1211      455678 :         FOR( j = 0; j < nb_subbands * 16; j++ )
    1212             :         {
    1213      452256 :             IF( concat_out_fx[j] > 0 )
    1214             :             {
    1215       66065 :                 seed_init = (Word16) L_mult0( seed_init, 8 );
    1216       66065 :                 move16();
    1217             :             }
    1218      452256 :             IF( concat_out_fx[j] < 0 )
    1219             :             {
    1220       66175 :                 seed_init = (Word16) L_add( seed_init, 3 );
    1221       66175 :                 move16();
    1222             :             }
    1223             :         }
    1224             : 
    1225        3422 :         hGSCEnc->seed_tcx = seed_init;
    1226        3422 :         move16();
    1227             :     }
    1228             : 
    1229       22696 :     test();
    1230       22696 :     IF( EQ_32( st->core_brate, ACELP_8k00 ) && st->bwidth != NB )
    1231             :     {
    1232          41 :         bitallocation_exc[0] = 0;
    1233          41 :         move16();
    1234          41 :         bitallocation_exc[1] = 0;
    1235          41 :         move16();
    1236             : 
    1237          41 :         if ( exc_diffQ_fx[L_FRAME8k - 2] != 0 )
    1238             :         {
    1239           2 :             bitallocation_exc[0] = 1;
    1240           2 :             move16();
    1241             :         }
    1242             : 
    1243          41 :         if ( exc_diffQ_fx[L_FRAME8k - 1] != 0 )
    1244             :         {
    1245           4 :             bitallocation_exc[1] = 1;
    1246           4 :             move16();
    1247             :         }
    1248             :     }
    1249             : 
    1250             :     /*--------------------------------------------------------------------------------------*
    1251             :      * Skip adaptive (pitch) contribution frequency band (no noise added over the adaptive (pitch) contribution)
    1252             :      * Find x pulses between 1.6-3.2kHz to code in the spectrum of the residual signal
    1253             :      * Gain is based on the inter-correlation gain between the pulses found and residual signal
    1254             :      *--------------------------------------------------------------------------------------*/
    1255             : 
    1256       22696 :     test();
    1257       22696 :     test();
    1258       22696 :     test();
    1259       22696 :     IF( st->GSC_IVAS_mode >= 1 && st->GSC_noisy_speech == 1 )
    1260             :     {
    1261       21616 :         FOR( i = 64; i < st->L_frame; i++ )
    1262             :         {
    1263       21504 :             exc_diffQ_fx[i] = mult( exc_diffQ_fx[i], max_eq ); /* Q12 */
    1264       21504 :             move16();
    1265             :         }
    1266             :     }
    1267       22584 :     ELSE IF( st->core_brate < ACELP_7k20 && st->GSC_noisy_speech == 1 && st->coder_type <= UNVOICED )
    1268             :     {
    1269     1921589 :         FOR( i = 0; i < L_FRAME; i++ )
    1270             :         {
    1271     1914112 :             exc_diffQ_fx[i] = mult( exc_diffQ_fx[i], max_eq ); /* Q12 */
    1272     1914112 :             move16();
    1273             :         }
    1274             :     }
    1275             :     ELSE
    1276             :     {
    1277       15107 :         freq_dnw_scaling_fx( hGSCEnc->cor_strong_limit, st->coder_type, hGSCEnc->noise_lev, st->core_brate, exc_diffQ_fx, Q12, st->L_frame );
    1278             :     }
    1279             : 
    1280       22696 :     Word16 Q_exc_new = s_min( *Q_exc, hGSCEnc->Q_last_exc_dct_in );
    1281       22696 :     IF( NE_16( Q_exc_new, hGSCEnc->Q_last_exc_dct_in ) )
    1282             :     {
    1283       22645 :         Scale_sig( hGSCEnc->last_exc_dct_in_fx, st->L_frame, sub( Q_exc_new, hGSCEnc->Q_last_exc_dct_in ) ); /* Q_exc_new */
    1284       22645 :         hGSCEnc->Q_last_exc_dct_in = Q_exc_new;
    1285       22645 :         move16();
    1286             :     }
    1287             :     ELSE
    1288             :     {
    1289          51 :         Scale_sig( exc_dct_in_fx, st->L_frame, sub( Q_exc_new, *Q_exc ) ); /* Q_exc_new */
    1290          51 :         *Q_exc = Q_exc_new;
    1291          51 :         move16();
    1292             :     }
    1293             : 
    1294             :     /*--------------------------------------------------------------------------------------*
    1295             :      * Estimate noise level
    1296             :      *--------------------------------------------------------------------------------------*/
    1297       22696 :     highband_exc_dct_in_ivas_fx( st->core_brate, mfreq_bindiv_loc, last_bin, Diff_len, hGSCEnc->noise_lev, tmp_band, exc_diffQ_fx /* Q12 */, &hGSCEnc->seed_tcx, Ener_per_bd_iQ_fx, nb_subfr, exc_dct_in_fx /* Q_exc */,
    1298       22696 :                                  st->last_coder_type, bitallocation_band, lsf_new_fx /* x2.56 */, hGSCEnc->last_exc_dct_in_fx /* Q_exc */, &hGSCEnc->last_ener_fx /* Q0 */, hGSCEnc->last_bitallocation_band, bitallocation_exc, 0, st->coder_type,
    1299       22696 :                                  st->bwidth, exc_wo_nf_fx, Q12, Q_exc, st->GSC_noisy_speech, NULL, st->L_frame, st->element_mode, st->GSC_IVAS_mode );
    1300             : 
    1301       22696 :     exc_dct_in_fx[0] = 0;
    1302       22696 :     move16();
    1303             : 
    1304       22696 :     return;
    1305             : }
    1306             : 
    1307             : /*======================================================================*/
    1308             : /* FUNCTION : edyn_fx()                                                                         */
    1309             : /*----------------------------------------------------------------------*/
    1310             : /* PURPOSE :  Calculate energy dynamics in a vector                                             */
    1311             : /*            (ratio of energy maximum to energy mean)                                  */
    1312             : /*                                                                                                                                              */
    1313             : /*----------------------------------------------------------------------*/
    1314             : /*  INPUT ARGUMENTS :                                                                                               */
    1315             : /* _ (Word16 *) vec             : ratio of max to mean              Qnew                */
    1316             : /* _ (Word16)   lvec    : input vector                                          */
    1317             : /* _ (Word16) Q_new             :                                                                                               */
    1318             : /*-----------------------------------------------------------------------*/
    1319             : /* OUTPUT ARGUMENTS :                                                                                                    */
    1320             : /* _ (Word16) dyn           : ratio of energy maximum to energy mean (Q7)        */
    1321             : /*-----------------------------------------------------------------------*/
    1322             : 
    1323             : /*-----------------------------------------------------------------------*/
    1324             : /* RETURN ARGUMENTS :                                                                                                    */
    1325             : /* _ None                                                                                                                                */
    1326             : /*=======================================================================*/
    1327             : 
    1328      121328 : static Word16 edyn_fx(                    /* o  : ratio of max to mean    */
    1329             :                        const Word16 *vec, /* i  : input vector            Qnew*/
    1330             :                        const Word16 lvec, /* i  : length of input vector  */
    1331             :                        Word16 Qnew )
    1332             : {
    1333      121328 :     Word16 j = 0;
    1334             :     Word16 dyn;
    1335             :     Word32 L_tmp, ener_max, ener_mean;
    1336             :     Word16 tmp, exp2, tmp2, tmp1, exp1, exp3;
    1337             :     Word16 scale;
    1338             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1339      121328 :     Flag Overflow = 0;
    1340      121328 :     move32();
    1341             : #endif
    1342             : 
    1343      121328 :     ener_mean = L_shl( 1, shl( Qnew, 1 ) ); /*2*Qnew*/
    1344      121328 :     ener_max = L_shl( 1, shl( Qnew, 1 ) );
    1345             : 
    1346     2062576 :     FOR( j = 0; j < lvec; j++ )
    1347             :     {
    1348     1941248 :         L_tmp = L_mult0( vec[j], vec[j] ); /*2*Qnew*/
    1349     1941248 :         ener_max = L_max( ener_max, L_tmp );
    1350     1941248 :         ener_mean = L_add_o( ener_mean, L_tmp, &Overflow );
    1351             :     }
    1352             :     /*dyn = 10.0f * (ener_max / ener_mean);*/
    1353      121328 :     ener_mean = Mult_32_16( ener_mean, div_s( 1, lvec ) ); /*2*Qnew*/
    1354             : 
    1355      121328 :     IF( ener_mean > 0 )
    1356             :     {
    1357      121328 :         exp1 = norm_l( ener_mean );
    1358      121328 :         tmp1 = round_fx_o( L_shl( ener_mean, exp1 ), &Overflow );
    1359      121328 :         exp1 = sub( 30, exp1 );
    1360             : 
    1361      121328 :         exp2 = norm_l( ener_max );
    1362      121328 :         tmp2 = extract_h( L_shl( ener_max, exp2 ) );
    1363      121328 :         exp2 = sub( 30, exp2 );
    1364             : 
    1365      121328 :         scale = shr( sub( tmp1, tmp2 ), 15 );
    1366      121328 :         tmp2 = shl( tmp2, scale );
    1367      121328 :         exp2 = sub( exp2, scale );
    1368             : 
    1369      121328 :         exp3 = sub( exp1, exp2 );
    1370             : 
    1371      121328 :         tmp = div_s( tmp2, tmp1 ); /*Q(15+exp3)*/
    1372             : 
    1373      121328 :         L_tmp = L_shr_r( L_mult( tmp, 10 ), exp3 );
    1374      121328 :         dyn = round_fx( L_shl( L_tmp, 7 ) ); /*Q7*/
    1375             :     }
    1376             :     ELSE
    1377             :     {
    1378           0 :         dyn = 1280; /* 10.0f in Q7 */
    1379           0 :         move16();
    1380             :     }
    1381      121328 :     return dyn; /* Q7 */
    1382             : }
    1383             : 
    1384             : 
    1385             : /*-------------------------------------------------------------------*
    1386             :  * GSC_enc_init_fx()
    1387             :  *
    1388             :  * Initialize GSC encoder state structure
    1389             :  *-------------------------------------------------------------------*/
    1390             : 
    1391        3855 : void GSC_enc_init_fx(
    1392             :     GSC_ENC_HANDLE hGSCEnc /* i/o: GSC data handle  */
    1393             : )
    1394             : {
    1395             :     /* AC mode */
    1396        3855 :     hGSCEnc->seed_tcx = 15687; /* Q0 */
    1397        3855 :     move16();
    1398        3855 :     hGSCEnc->cor_strong_limit = 1;
    1399        3855 :     move16();
    1400        3855 :     set16_fx( hGSCEnc->last_exc_dct_in_fx, 0, L_FRAME16k );
    1401        3855 :     hGSCEnc->Q_last_exc_dct_in = Q15;
    1402        3855 :     move16();
    1403        3855 :     set16_fx( hGSCEnc->last_bitallocation_band, 0, 6 );
    1404             : 
    1405        3855 :     hGSCEnc->mem_last_pit_band = BAND1k2 + 1;
    1406        3855 :     move16();
    1407        3855 :     hGSCEnc->Last_frame_ener_fx = MAX_32;
    1408        3855 :     move32();
    1409        3855 :     hGSCEnc->lt_gpitch_fx = 0;
    1410        3855 :     move16();
    1411        3855 :     move16();
    1412        3855 :     hGSCEnc->pit_exc_hangover = 0;
    1413        3855 :     move16();
    1414             : 
    1415        3855 :     hGSCEnc->mem_w0_tmp_fx = 0;
    1416        3855 :     move16();
    1417             : 
    1418        3855 :     set16_fx( hGSCEnc->mem_syn_tmp_fx, 0, M );
    1419        3855 :     hGSCEnc->mid_dyn_fx = 5120; /*40 -> Q7 */
    1420        3855 :     move16();
    1421        3855 :     hGSCEnc->noise_lev = NOISE_LEVEL_SP0;
    1422        3855 :     move16();
    1423        3855 :     hGSCEnc->past_dyn_dec = 0;
    1424        3855 :     move16();
    1425             : 
    1426             : 
    1427        3855 :     return;
    1428             : }

Generated by: LCOV version 1.14