LCOV - code coverage report
Current view: top level - lib_enc - enc_pit_exc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 314 464 67.7 %
Date: 2025-09-14 03:13:15 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include "options.h"     /* Compilation switches                   */
       7             : #include "cnst.h"        /* Common constants                       */
       8             : #include "rom_com_fx.h"  /* Static table prototypes                */
       9             : #include "rom_com.h"     /* Static table prototypes                */
      10             : #include "prot_fx.h"     /* Function prototypes                    */
      11             : #include "prot_fx_enc.h" /* Function prototypes                    */
      12             : 
      13             : /*======================================================================*/
      14             : /* FUNCTION : enc_pit_exc_fx()                                                                              */
      15             : /*----------------------------------------------------------------------*/
      16             : /* PURPOSE : Encode pitch only contribution                                                             */
      17             : /*                                                                                                                                              */
      18             : /*----------------------------------------------------------------------*/
      19             : /*  INPUT ARGUMENTS :                                                                                               */
      20             : /* _ (Struct)   st_fx                   : encoder static memory                                 */
      21             : /* _ (Word16[]) speech_fx                       : input speech                  Qnew-1  */
      22             : /* _ (Word16[]) Aq_fx                   : LP filter coefficient             Q12         */
      23             : /* _ (Word16[]) A_fx                    : unquantized A(z) filter               */
      24             : /*                                with bandwidth expansion              Q12         */
      25             : /* _ (Word16) coder_type                : coding type                                                   */
      26             : /* _ (Word16) Es_pred_fx                : predicted scaled innov. energy Q8             */
      27             : /* _ (Word16[]) T_op_fx                 : open loop pitch Q0                                    */
      28             : /* _ (Word16[]) voicing_fx          : floating pitch values for each subframe Q15*/
      29             : /* _ (Word16[]) res_fx                  : residual signal                               Q_new    */
      30             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Qnew)                   */
      31             : /* _ (Word16[]) exc2_fx                 : adapt. excitation/total exc (Qnew)     */
      32             : /* _ (Word16[]) pitch_buf_fx    : floating pitch values for each subframe Q6*/
      33             : /* _ (Word16[]) *wH1,           : Weighted impulses response mask        */
      34             : /* _ (Word16) shift                     :       shift                                                            */
      35             : /* _ (Word16) Q_new                     :                                            */
      36             : /*-----------------------------------------------------------------------*/
      37             : /* OUTPUT ARGUMENTS :                                                                                                    */
      38             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Qnew)                   */
      39             : /* _ (Word16[]) exc2_fx                 : adapt. excitation/total exc (Qnew)     */
      40             : /* _ (Word16[]) syn_fx                  :core synthesis                                              */
      41             : /* _ (Word16[]) voice_factors_fx: voicing factors                               Q15              */
      42             : /* _ (Word16[]) bwe_exc_fx              : excitation for SWB TBE        Q0               */
      43             : /*-----------------------------------------------------------------------*/
      44             : 
      45             : /*-----------------------------------------------------------------------*/
      46             : /* RETURN ARGUMENTS :                                                                                                    */
      47             : /* _ None                                                                                                                                */
      48             : /*=======================================================================*/
      49             : 
      50           8 : void enc_pit_exc_fx(
      51             :     Encoder_State *st_fx,              /* i/o: State structure                                  */
      52             :     const Word16 *speech,              /* i  : Input speech                                     Q_new-1*/
      53             :     const Word16 Aw[],                 /* i  : weighted A(z) unquantized for subframes          Q12*/
      54             :     const Word16 Aq[],                 /* i  : 12k8 Lp coefficient                              Q12*/
      55             :     const Word16 Es_pred,              /* i  : predicted scaled innov. energy                   Q8*/
      56             :     const Word16 *res,                 /* i  : residual signal                                  Q_new*/
      57             :     Word16 *synth,                     /* i/o: core synthesis                                   Q_new*/
      58             :     Word16 *exc,                       /* i/o: current non-enhanced excitation                  Q_new*/
      59             :     Word16 *T0,                        /* i/o: close loop integer pitch                         Q0*/
      60             :     Word16 *T0_frac,                   /* i/o: close-loop pitch period - fractional part        Q0*/
      61             :     Word16 *pitch_buf,                 /* i/o: Fractionnal per subframe pitch                   Q6*/
      62             :     const Word16 nb_subfr,             /* i  : Number of subframe considered                    Q0*/
      63             :     Word16 *gpit,                      /* o  : pitch mean gpit                                  Q15*/
      64             :     Word16 *saved_bit_pos,             /* o  : saved position in the bitstream before pitch contribution        Q0*/
      65             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag                                 Q0*/
      66             :     const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                                             Q6*/
      67             :     Word16 Q_new,
      68             :     Word16 shift )
      69             : {
      70             :     Word16 xn[PIT_EXC_L_SUBFR];             /* Target vector for pitch search    */
      71             :     Word16 xn2[PIT_EXC_L_SUBFR];            /* Target vector for codebook search */
      72             :     Word16 h1[PIT_EXC_L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
      73             :     Word16 y1[PIT_EXC_L_SUBFR];             /* Filtered adaptive excitation      */
      74             :     Word16 code[2 * L_SUBFR];               /* Fixed codebook excitation         */
      75             :     Word16 y2[2 * L_SUBFR];                 /* Filtered algebraic excitation     */
      76             :     Word16 voice_fac;                       /* Voicing factor                    */
      77             :     Word32 gain_code;                       /* Gain of code                      */
      78             :     Word16 gain_inov;                       /* inovation gain                    */
      79             :     Word16 gain_pit;                        /* Pitch gain                        */
      80             :     Word16 pit_idx, i_subfr;                /* tmp variables                     */
      81             :     Word16 T0_min, T0_max;                  /* pitch variables                   */
      82             :     Word16 g_corr[10];                      /* ACELP correlation values + gain pitch */
      83             :     Word16 clip_gain, i;                    /* LSF clip gain and LP flag         */
      84             :     const Word16 *p_Aw, *p_Aq;              /* pointer to LP filter coefficient vector */
      85             :     Word16 cn1[L_SUBFR], *cn;               /* (Used only when L_subfr == L_SUBFR) Target vector in residual domain  */
      86             :     Word16 *pt_pitch;                       /* pointer to floating pitch         */
      87             :     Word16 L_subfr;
      88             :     Word16 cum_gpit, gpit_tmp;
      89             :     Word32 Local_BR, Pitch_BR;
      90           8 :     Word16 Pitch_CT, unbits_PI = 0; /* saved bits for PI                 */
      91           8 :     move16();
      92             :     Word32 norm_gain_code;
      93             :     Word16 pitch_limit_flag;
      94             :     Word16 h2[PIT_EXC_L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
      95             :     Word32 Ltmp;
      96             :     Word32 Lgcode;
      97             :     Word16 gcode16;
      98             :     Word16 shift_wsp;
      99             :     Word16 lp_select, lp_flag;
     100             :     Word16 use_fcb;
     101             :     Word32 gc_mem[NB_SUBFR - 1]; /* gain_code from previous subframes */
     102             :     Word16 gp_mem[NB_SUBFR - 1]; /* gain_pitch from previous subframes*/
     103           8 :     SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
     104           8 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     105           8 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
     106           8 :     LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
     107             : #ifndef ISSUE_1867_replace_overflow_libenc
     108             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     109             :     Flag Overflow = 0;
     110             :     move32();
     111             : #endif
     112             : #endif
     113             : 
     114             :     /*------------------------------------------------------------------*
     115             :      * Initialization
     116             :      *------------------------------------------------------------------*/
     117             : 
     118           8 :     pitch_limit_flag = 1;
     119           8 :     move16(); /* always extended pitch Q range */
     120           8 :     use_fcb = 0;
     121           8 :     unbits_PI = 0;
     122           8 :     move16();
     123           8 :     move16();
     124           8 :     Pitch_CT = GENERIC;
     125           8 :     move16();
     126             : 
     127           8 :     test();
     128           8 :     test();
     129           8 :     IF( st_fx->GSC_IVAS_mode > 0 && ( st_fx->GSC_noisy_speech || GT_32( st_fx->core_brate, GSC_H_RATE_STG ) ) )
     130             :     {
     131           0 :         Local_BR = ACELP_8k00;
     132           0 :         Pitch_BR = ACELP_8k00;
     133           0 :         move32();
     134           0 :         move32();
     135           0 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     136             :         {
     137           0 :             Local_BR = ACELP_14k80;
     138           0 :             move32();
     139           0 :             if ( st_fx->GSC_IVAS_mode > 0 )
     140             :             {
     141           0 :                 Local_BR = ACELP_9k60;
     142           0 :                 move32();
     143             :             }
     144           0 :             Pitch_BR = st_fx->core_brate; /* Q0 */
     145           0 :             move32();
     146             :         }
     147             :     }
     148           8 :     ELSE IF( st_fx->GSC_noisy_speech )
     149             :     {
     150           0 :         Local_BR = ACELP_7k20;
     151           0 :         move32();
     152           0 :         Pitch_BR = ACELP_7k20;
     153           0 :         move32();
     154           0 :         Pitch_CT = GENERIC;
     155           0 :         move16();
     156           0 :         if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     157             :         {
     158           0 :             Pitch_BR = st_fx->core_brate; /* Q0 */
     159           0 :             move32();
     160             :         }
     161             :     }
     162             :     ELSE
     163             :     {
     164           8 :         Local_BR = ACELP_7k20;
     165           8 :         move32();
     166           8 :         Pitch_BR = st_fx->core_brate; /* Q0 */
     167           8 :         move32();
     168           8 :         Pitch_CT = AUDIO;
     169           8 :         move16();
     170             : 
     171           8 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     172             :         {
     173           0 :             Local_BR = ACELP_13k20;
     174           0 :             move32();
     175           0 :             Pitch_CT = GENERIC;
     176           0 :             move16();
     177             :         }
     178             :     }
     179           8 :     gain_code = 0;
     180           8 :     move16();
     181             : 
     182           8 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     183             :     {
     184           0 :         T0_max = PIT16k_MAX;
     185           0 :         T0_min = PIT16k_MIN;
     186             :     }
     187             :     ELSE
     188             :     {
     189           8 :         T0_max = PIT_MAX;
     190           8 :         move16();
     191           8 :         T0_min = PIT_MIN;
     192           8 :         move16();
     193             :     }
     194           8 :     cum_gpit = 0;
     195           8 :     move16();
     196             : 
     197           8 :     L_subfr = mult_r( st_fx->L_frame, div_s( 1, nb_subfr ) ); /* Q0 */
     198             : 
     199           8 :     lp_flag = st_fx->acelp_cfg.ltf_mode; /* Q0 */
     200             : 
     201           8 :     test();
     202           8 :     test();
     203           8 :     test();
     204           8 :     test();
     205           8 :     test();
     206           8 :     test();
     207           8 :     test();
     208           8 :     test();
     209           8 :     IF( ( ( GE_32( st_fx->core_brate, MIN_RATE_FCB ) || ( EQ_16( st_fx->GSC_noisy_speech, 1 ) &&
     210             :                                                           ( ( EQ_16( st_fx->L_frame, L_FRAME ) && GE_32( st_fx->core_brate, ACELP_13k20 ) ) ||
     211             :                                                             ( EQ_16( st_fx->L_frame, L_FRAME16k ) && GE_32( st_fx->core_brate, GSC_H_RATE_STG ) ) || st_fx->GSC_IVAS_mode == 0 ) ) ) &&
     212             :           EQ_16( L_subfr, L_SUBFR ) ) )
     213             :     {
     214           0 :         use_fcb = 1;
     215           0 :         move16();
     216             :     }
     217           8 :     ELSE IF( st_fx->GSC_IVAS_mode > 0 && EQ_16( L_subfr, 2 * L_SUBFR ) && LT_16( st_fx->GSC_IVAS_mode, 3 ) )
     218             :     {
     219           0 :         use_fcb = 2;
     220           0 :         st_fx->acelp_cfg.fcb_mode = 1;
     221           0 :         move16();
     222           0 :         move16();
     223           0 :         set16_fx( st_fx->acelp_cfg.gains_mode, 6, NB_SUBFR );
     224           0 :         set16_fx( st_fx->acelp_cfg.pitch_bits, 9, NB_SUBFR );
     225           0 :         set16_fx( st_fx->acelp_cfg.fixed_cdk_index, 14, NB_SUBFR16k );
     226             :     }
     227             : 
     228           8 :     *saved_bit_pos = st_fx->next_bit_pos_fx;
     229           8 :     move16();
     230             : 
     231             :     /*------------------------------------------------------------------*
     232             :      * ACELP subframe loop
     233             :      *------------------------------------------------------------------*/
     234             : 
     235           8 :     cn = NULL;
     236           8 :     if ( EQ_16( L_subfr, L_SUBFR ) )
     237             :     {
     238           0 :         cn = cn1;
     239           0 :         move16();
     240             :     }
     241           8 :     p_Aw = Aw;
     242             : 
     243           8 :     p_Aq = Aq;
     244           8 :     pt_pitch = pitch_buf; /* pointer to the pitch buffer */
     245           8 :     shift_wsp = add( Q_new, shift );
     246             : 
     247          24 :     FOR( i_subfr = 0; i_subfr < st_fx->L_frame; i_subfr += L_subfr )
     248             :     {
     249             : 
     250             :         /*----------------------------------------------------------------*
     251             :          * Bandwidth expansion of A(z) filter coefficients
     252             :          * Find the the excitation search target "xn" and innovation
     253             :          *   target in residual domain "cn"
     254             :          * Compute impulse response, h1[], of weighted synthesis filter
     255             :          *----------------------------------------------------------------*/
     256             : 
     257          16 :         Copy( &res[i_subfr], &exc[i_subfr], L_subfr ); /* Q_new */
     258             :                                                        /* condition on target (compared to float) has been put outside the loop */
     259          16 :         find_targets_fx( speech, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq,
     260          16 :                          res, L_subfr, p_Aw, st_fx->preemph_fac, xn, cn, h1 );
     261          16 :         Copy_Scale_sig( h1, h2, L_subfr, -2 );     /* Q13 */
     262          16 :         Scale_sig( h1, L_subfr, add( 1, shift ) ); /* set h1[] in Q14 with scaling for convolution */
     263             : 
     264             :         /* scaling of xn[] to limit dynamic at 12 bits */
     265          16 :         Scale_sig( xn, L_subfr, shift ); /* Q_new - 1 + shift */
     266             : 
     267             :         /*----------------------------------------------------------------*
     268             :          * Close-loop pitch search and quantization
     269             :          * Adaptive exc. construction
     270             :          *----------------------------------------------------------------*/
     271             : 
     272          32 :         *pt_pitch = pit_encode_fx( hBstr, st_fx->acelp_cfg.pitch_bits, Pitch_BR, 0, st_fx->L_frame, Pitch_CT, &pitch_limit_flag, i_subfr, exc,
     273          16 :                                    L_subfr, st_fx->pitch, &T0_min, &T0_max, T0, T0_frac, h1, xn, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, st_fx->element_mode, Q_new ); /* Q6 */
     274             : 
     275             :         /*-----------------------------------------------------------------*
     276             :          * Find adaptive exitation
     277             :          *-----------------------------------------------------------------*/
     278             : 
     279          16 :         pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_subfr + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     280             :         /*-----------------------------------------------------------------*
     281             :          * Gain clipping test to avoid unstable synthesis on frame erasure
     282             :          * or in case of floating point encoder & fixed p. decoder
     283             :          *-----------------------------------------------------------------*/
     284             : 
     285          16 :         clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, AUDIO, xn, st_fx->clip_var_fx, sub( shift_wsp, 1 ) ); /* Q0 */
     286             : 
     287             :         /*-----------------------------------------------------------------*
     288             :          * Codebook target computation
     289             :          * (No LP filtering of the adaptive excitation)
     290             :          *-----------------------------------------------------------------*/
     291             : 
     292          16 :         lp_select = lp_filt_exc_enc_fx( MODE1, AUDIO, i_subfr, exc, h1, xn, y1, xn2, L_subfr, st_fx->L_frame, g_corr, clip_gain, &gain_pit, &lp_flag ); /* Q0 */
     293             : 
     294          16 :         IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
     295             :         {
     296           0 :             push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     297             :         }
     298             : 
     299             :         /*st_fx->lowrate_pitchGain = 0.9f * st_fx->lowrate_pitchGain + 0.1f * gain_pit;*/
     300             : #ifdef ISSUE_1867_replace_overflow_libenc
     301          16 :         hSpMusClas->lowrate_pitchGain = round_fx_sat( L_mac_sat( L_mult( 29491, hSpMusClas->lowrate_pitchGain ), 6554, gain_pit ) ); /*Q14*Q16(0.1) + Q15 -> Q15*/
     302             : #else
     303             :         hSpMusClas->lowrate_pitchGain = round_fx_o( L_mac_o( L_mult( 29491, hSpMusClas->lowrate_pitchGain ), 6554, gain_pit, &Overflow ), &Overflow ); /*Q14*Q16(0.1) + Q15 -> Q15*/
     304             : #endif
     305             : 
     306          16 :         gpit_tmp = gain_pit;
     307          16 :         move16(); /*Q14*/
     308          16 :         test();
     309          16 :         IF( use_fcb > 0 )
     310             :         {
     311             :             /* h2 in Q12 for codebook search */
     312             :             /* h1 has been scaled with 1 + shift so we need to remove 2 and (1+shift) = -3 - shift*/
     313           0 :             Copy_Scale_sig( h1, h2, L_subfr, sub( -2 - 1, shift ) );
     314             :         }
     315             : 
     316          16 :         IF( use_fcb == 0 )
     317             :         {
     318          16 :             IF( GE_32( st_fx->core_brate, MIN_RATE_FCB ) )
     319             :             {
     320           0 :                 pit_idx = vquant_fx( &gain_pit, mean_gp_fx, &gain_pit, dic_gp_fx, 1, 32 ); /* Q0 */
     321           0 :                 push_indice( hBstr, IND_PIT_IDX, pit_idx, 5 );
     322             :             }
     323             :             ELSE
     324             :             {
     325          16 :                 pit_idx = vquant_fx( &gain_pit, mean_gp_fx, &gain_pit, dic_gp_fx, 1, 16 ); /* Q0 */
     326          16 :                 push_indice( hBstr, IND_PIT_IDX, pit_idx, 4 );
     327             :             }
     328             :         }
     329           0 :         ELSE IF( use_fcb == 2 )
     330             :         {
     331             :             /*-----------------------------------------------------------------*
     332             :              * Innovation encoding
     333             :              *-----------------------------------------------------------------*/
     334             : 
     335           0 :             inov_encode_fx( st_fx, st_fx->core_brate, 0, st_fx->L_frame, st_fx->last_L_frame, GENERIC, st_fx->bwidth, 0, i_subfr, -1, p_Aq,
     336           0 :                             gain_pit, cn, exc, h2, st_fx->hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, 2 * L_SUBFR, shift );
     337             : 
     338             :             /*-----------------------------------------------------------------*
     339             :              * Gain encoding
     340             :              *-----------------------------------------------------------------*/
     341             : 
     342           0 :             gain_enc_lbr_fx( st_fx->hBstr, st_fx->acelp_cfg.gains_mode, GENERIC, i_subfr, xn, y1, shift_wsp, y2, code, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, gc_mem, gp_mem, clip_gain, 2 * L_SUBFR,
     343           0 :                              st_fx->element_mode );
     344             :         }
     345             :         ELSE
     346             :         {
     347             :             /*-----------------------------------------------------------------*
     348             :              * Innovation & gain encoding
     349             :              *-----------------------------------------------------------------*/
     350             : 
     351           0 :             inov_encode_fx( st_fx, Local_BR, 0, st_fx->L_frame, st_fx->last_L_frame, LOCAL_CT, WB, 1, i_subfr, -1, p_Aq,
     352           0 :                             gain_pit, cn, exc, h2, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, L_SUBFR, shift );
     353             : 
     354             :             /*-----------------------------------------------------------------*
     355             :              * Gain encoding
     356             :              *-----------------------------------------------------------------*/
     357             : 
     358           0 :             gain_enc_mless_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_FRAME, i_subfr, -1, xn, y1, shift_wsp, y2, code, Es_pred,
     359             :                                &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
     360             :         }
     361          16 :         gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit, st_fx->clip_var_fx );
     362             : 
     363          16 :         Lgcode = L_shl_sat( gain_code, Q_new ); /* scaled gain_code with Qnew -> Q16*/
     364          16 :         gcode16 = round_fx_sat( Lgcode );
     365          16 :         IF( use_fcb != 0 )
     366             :         {
     367           0 :             hLPDmem->tilt_code = Est_tilt2( &exc[i_subfr], gain_pit, code, Lgcode, &voice_fac, shift ); /* Q15 */
     368           0 :             move16();
     369             :         }
     370             :         ELSE
     371             :         {
     372          16 :             hLPDmem->tilt_code = 0;
     373          16 :             move16();
     374             :         }
     375             : 
     376             :         /*-----------------------------------------------------------------*
     377             :          * Update memory of the weighting filter
     378             :          *-----------------------------------------------------------------*/
     379             : 
     380          16 :         IF( use_fcb != 0 )
     381             :         {
     382           0 :             Ltmp = L_mult( gcode16, y2[L_subfr - 1] ); /* Q10 + Q_new */
     383             : #ifdef ISSUE_1867_replace_overflow_libenc
     384           0 :             Ltmp = L_shl_sat( Ltmp, add( 5, shift ) ); /* Q15 + Q_new + shift */
     385           0 :             Ltmp = L_negate( Ltmp );
     386           0 :             Ltmp = L_mac_sat( Ltmp, xn[L_subfr - 1], 16384 );    /* Q_new + Q15 + shift */
     387           0 :             Ltmp = L_msu_sat( Ltmp, y1[L_subfr - 1], gain_pit ); /* Q_new + Q15 + shift */
     388           0 :             Ltmp = L_shl_sat( Ltmp, sub( 1, shift ) );           /* Q_new + 15 */
     389           0 :             hLPDmem->mem_w0 = round_fx_sat( Ltmp );              /*Q_new-1         */
     390             : #else
     391             :             Ltmp = L_shl_o( Ltmp, add( 5, shift ), &Overflow );                                                                                        /* Q15 + Q_new + shift */
     392             :             Ltmp = L_negate( Ltmp );
     393             :             Ltmp = L_mac_o( Ltmp, xn[L_subfr - 1], 16384, &Overflow );         /* Q_new + Q15 + shift */
     394             :             Ltmp = L_msu_o( Ltmp, y1[L_subfr - 1], gain_pit, &Overflow );      /* Q_new + Q15 + shift */
     395             :             Ltmp = L_shl_o( Ltmp, sub( 1, shift ), &Overflow );                /* Q_new + 15 */
     396             :             hLPDmem->mem_w0 = round_fx_o( Ltmp, &Overflow );                   /*Q_new-1         */
     397             : #endif
     398           0 :             move16();
     399             :         }
     400             :         ELSE
     401             :         {
     402          16 :             Ltmp = L_mult( xn[L_subfr - 1], 16384 );             /* Q_new + 14 -shift */
     403          16 :             Ltmp = L_msu_sat( Ltmp, y1[L_subfr - 1], gain_pit ); /* Q_new + 14 -shift */
     404          16 :             Ltmp = L_shl_sat( Ltmp, sub( 1, shift ) );           /* Q_new + 15 */
     405          16 :             hLPDmem->mem_w0 = round_fx_sat( Ltmp );              /*Q_new-1         */
     406          16 :             move16();
     407             :         }
     408             : 
     409             :         /*-----------------------------------------------------------------*
     410             :          * Construct adaptive part of the excitation
     411             :          * Save the non-enhanced excitation for FEC_exc
     412             :          *-----------------------------------------------------------------*/
     413             : 
     414          16 :         IF( use_fcb != 0 )
     415             :         {
     416           0 :             FOR( i = 0; i < L_subfr; i++ )
     417             :             {
     418             :                 /* code in Q9, gain_pit in Q14 */
     419           0 :                 Ltmp = L_mult( gcode16, code[i] ); /* Q10 + Q_new */
     420             : #ifdef ISSUE_1867_replace_overflow_libenc
     421           0 :                 Ltmp = L_shl_sat( Ltmp, 5 );                          /* Q15 + Q_new */
     422           0 :                 Ltmp = L_mac_sat( Ltmp, exc[i + i_subfr], gain_pit ); /* Q15 + Q_new */
     423           0 :                 Ltmp = L_shl_sat( Ltmp, 1 );                          /* saturation can occur here              Q_new + Q16*/
     424           0 :                 exc[i + i_subfr] = round_fx_sat( Ltmp );              /* Q_new */
     425             : #else
     426             :                 Ltmp = L_shl_o( Ltmp, 5, &Overflow );                          /* Q15 + Q_new */
     427             :                 Ltmp = L_mac_o( Ltmp, exc[i + i_subfr], gain_pit, &Overflow ); /* Q15 + Q_new */
     428             :                 Ltmp = L_shl_o( Ltmp, 1, &Overflow );                          /* saturation can occur here         Q_new + Q16*/
     429             :                 exc[i + i_subfr] = round_fx_o( Ltmp, &Overflow );              /* Q_new */
     430             : #endif
     431           0 :                 move16();
     432             :             }
     433             :         }
     434             :         ELSE
     435             :         {
     436        2064 :             FOR( i = 0; i < L_subfr; i++ )
     437             :             {
     438             : 
     439        2048 :                 Ltmp = L_mult( exc[i + i_subfr], gain_pit ); /* Q15 + Q_new */
     440        2048 :                 Ltmp = L_shl_sat( Ltmp, 1 );                 /* saturation can occur here       Q16 + Q_new */
     441        2048 :                 exc[i + i_subfr] = round_fx_sat( Ltmp );     /* Q_new */
     442             :             }
     443             :         }
     444             : 
     445             :         /*-----------------------------------------------------------------*
     446             :          * Synthesize speech to update mem_syn[].
     447             :          * Update A(z) filters
     448             :          *-----------------------------------------------------------------*/
     449             : 
     450          16 :         Syn_filt_s( 1, p_Aq, M, &exc[i_subfr], &synth[i_subfr], L_subfr, hGSCEnc->mem_syn_tmp_fx, 1 );
     451             : 
     452          16 :         IF( EQ_16( L_subfr, 5 * L_SUBFR ) )
     453             :         {
     454           0 :             cum_gpit = gpit_tmp; /* Q14 */
     455           0 :             move16();
     456           0 :             pt_pitch++;
     457           0 :             *pt_pitch = *( pt_pitch - 1 );
     458           0 :             move16();
     459           0 :             pt_pitch++;
     460           0 :             *pt_pitch = *( pt_pitch - 1 );
     461           0 :             move16();
     462           0 :             pt_pitch++;
     463           0 :             *pt_pitch = *( pt_pitch - 1 );
     464           0 :             move16();
     465           0 :             pt_pitch++;
     466           0 :             *pt_pitch = *( pt_pitch - 1 );
     467           0 :             move16();
     468           0 :             pt_pitch++;
     469             : 
     470           0 :             p_Aw += 5 * ( M + 1 );
     471           0 :             p_Aq += 5 * ( M + 1 );
     472             :         }
     473          16 :         ELSE IF( EQ_16( L_subfr, 5 * L_SUBFR / 2 ) )
     474             :         {
     475           0 :             IF( i_subfr == 0 )
     476             :             {
     477           0 :                 cum_gpit = mult_r( gpit_tmp, 13107 /* .4f in Q15 */ ); /* Q14 */
     478           0 :                 pt_pitch++;
     479           0 :                 *pt_pitch = *( pt_pitch - 1 );
     480           0 :                 move16();
     481           0 :                 pt_pitch++;
     482           0 :                 p_Aw += 2 * ( M + 1 );
     483           0 :                 p_Aq += 2 * ( M + 1 );
     484             :             }
     485             :             ELSE
     486             :             {
     487           0 :                 cum_gpit = add( cum_gpit, mult_r( gpit_tmp, 19660 /* 0.6 in Q15 */ ) ); /* Q14 */
     488           0 :                 pt_pitch++;
     489           0 :                 *pt_pitch = *( pt_pitch - 1 );
     490           0 :                 move16();
     491           0 :                 pt_pitch++;
     492           0 :                 *pt_pitch = *( pt_pitch - 1 );
     493           0 :                 move16();
     494           0 :                 pt_pitch++;
     495           0 :                 p_Aw += 3 * ( M + 1 );
     496           0 :                 p_Aq += 3 * ( M + 1 );
     497             :             }
     498             :         }
     499          16 :         ELSE IF( EQ_16( L_subfr, 2 * L_SUBFR ) )
     500             :         {
     501          16 :             IF( i_subfr == 0 )
     502             :             {
     503           8 :                 cum_gpit = mult_r( gpit_tmp, 16384 /* 0.5 in Q15 */ ); /* Q14 */
     504             :             }
     505             :             ELSE
     506             :             {
     507           8 :                 cum_gpit = add( cum_gpit, mult_r( gpit_tmp, 16384 /* 0.5 in Q15 */ ) ); /* Q14 */
     508             :             }
     509          16 :             p_Aw += 2 * ( M + 1 );
     510          16 :             move16();
     511          16 :             p_Aq += 2 * ( M + 1 );
     512          16 :             move16();
     513          16 :             pt_pitch++;
     514          16 :             *pt_pitch = *( pt_pitch - 1 );
     515          16 :             move16();
     516          16 :             pt_pitch++;
     517             :         }
     518           0 :         ELSE IF( EQ_16( L_subfr, 4 * L_SUBFR ) )
     519             :         {
     520           0 :             cum_gpit = gpit_tmp; /* Q14 */
     521           0 :             move16();
     522             : 
     523           0 :             pt_pitch++;
     524           0 :             *pt_pitch = *( pt_pitch - 1 );
     525           0 :             move16();
     526           0 :             pt_pitch++;
     527           0 :             *pt_pitch = *( pt_pitch - 1 );
     528           0 :             move16();
     529           0 :             pt_pitch++;
     530           0 :             *pt_pitch = *( pt_pitch - 1 );
     531           0 :             pt_pitch++;
     532           0 :             p_Aw += 4 * ( M + 1 );
     533           0 :             p_Aq += 4 * ( M + 1 );
     534             :         }
     535             :         ELSE
     536             :         {
     537           0 :             IF( i_subfr == 0 )
     538             :             {
     539             : 
     540           0 :                 cum_gpit = mult_r( gpit_tmp, 8192 /* 0.25 in Q15 */ ); /* Q14 */
     541             :             }
     542             :             ELSE
     543             :             {
     544           0 :                 cum_gpit = add( cum_gpit, mult_r( gpit_tmp, 8192 /* 0.25 in Q15 */ ) ); /* Q14 */
     545             :             }
     546             : 
     547           0 :             pt_pitch++;
     548           0 :             p_Aw += ( M + 1 );
     549           0 :             p_Aq += ( M + 1 );
     550             :         }
     551             :     }
     552             : 
     553           8 :     cum_gpit = shl_sat( cum_gpit, 1 );                                   /*Q15*/
     554           8 :     *gpit = round_fx( L_mac( L_mult( 3277, *gpit ), 29491, cum_gpit ) ); /*Q15*/
     555             : 
     556           8 :     return;
     557             : }
     558             : 
     559             : 
     560       15091 : void enc_pit_exc_ivas_fx(
     561             :     Encoder_State *st_fx,              /* i/o: State structure                                  */
     562             :     const Word16 *speech,              /* i  : Input speech                                     Q_new-1*/
     563             :     const Word16 Aw[],                 /* i  : weighted A(z) unquantized for subframes          Q12*/
     564             :     const Word16 Aq[],                 /* i  : 12k8 Lp coefficient                              Q12*/
     565             :     const Word16 Es_pred,              /* i  : predicted scaled innov. energy                   Q8*/
     566             :     const Word16 *res,                 /* i  : residual signal                                  Q_new*/
     567             :     Word16 *synth,                     /* i/o: core synthesis                                   Q_new*/
     568             :     Word16 *exc,                       /* i/o: current non-enhanced excitation                  Q_new*/
     569             :     Word16 *T0,                        /* i/o: close loop integer pitch                         Q0*/
     570             :     Word16 *T0_frac,                   /* i/o: close-loop pitch period - fractional part        Q0*/
     571             :     Word16 *pitch_buf,                 /* i/o: Fractionnal per subframe pitch                   Q6*/
     572             :     const Word16 nb_subfr,             /* i  : Number of subframe considered                    Q0*/
     573             :     Word16 *gpit,                      /* o  : pitch mean gpit                                  Q15*/
     574             :     Word16 *saved_bit_pos,             /* o  : saved position in the bitstream before pitch contribution        Q0*/
     575             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag                                 Q0*/
     576             :     const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                                             Q6*/
     577             :     Word16 Q_new,
     578             :     Word16 shift )
     579             : {
     580             :     Word16 xn[PIT_EXC_L_SUBFR];             /* Target vector for pitch search    */
     581             :     Word16 xn2[PIT_EXC_L_SUBFR];            /* Target vector for codebook search */
     582             :     Word16 h1[PIT_EXC_L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
     583             :     Word16 cn[PIT_EXC_L_SUBFR];
     584             :     Word16 y1[PIT_EXC_L_SUBFR]; /* Filtered adaptive excitation      */
     585             :     Word16 code[2 * L_SUBFR];   /* Fixed codebook excitation         */
     586             :     Word16 y2[2 * L_SUBFR];     /* Filtered algebraic excitation     */
     587             :     Word16 voice_fac;           /* Voicing factor                    */
     588             :     Word32 gain_code;           /* Gain of code                      */
     589             :     Word16 gain_inov;           /* inovation gain                    */
     590             :     Word16 gain_pit;            /* Pitch gain                        */
     591             :     Word16 pit_idx, i_subfr;    /* tmp variables                     */
     592             :     Word16 T0_min, T0_max;      /* pitch variables                   */
     593             :     Word16 g_corr[10];          /* ACELP correlation values + gain pitch */
     594             :     Word16 clip_gain, i;        /* LSF clip gain and LP flag         */
     595             :     const Word16 *p_Aw, *p_Aq;  /* pointer to LP filter coefficient vector */
     596             :     Word16 *pt_pitch;           /* pointer to floating pitch         */
     597             :     Word16 L_subfr;
     598             :     Word16 cum_gpit, gpit_tmp;
     599             :     Word32 Local_BR, Pitch_BR;
     600       15091 :     Word16 Pitch_CT, unbits_PI = 0; /* saved bits for PI                 */
     601             :     Word32 norm_gain_code;
     602             :     Word16 pitch_limit_flag;
     603             :     Word16 h2[PIT_EXC_L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
     604             :     Word32 Ltmp;
     605             :     Word32 Lgcode;
     606             :     Word16 gcode16;
     607             :     Word16 shift_wsp;
     608             :     Word16 lp_select, lp_flag;
     609             :     Word16 use_fcb;
     610             :     Word32 gc_mem[NB_SUBFR - 1]; /* gain_code from previous subframes */
     611             :     Word16 gp_mem[NB_SUBFR - 1]; /* gain_pitch from previous subframes*/
     612             :     Word16 q_h1;
     613       15091 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     614       15091 :     GSC_ENC_HANDLE hGSCEnc = st_fx->hGSCEnc;
     615       15091 :     LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
     616             : #ifndef ISSUE_1867_replace_overflow_libenc
     617             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     618             :     Flag Overflow = 0;
     619             :     move32();
     620             : #endif
     621             : #endif
     622             : 
     623             :     /*------------------------------------------------------------------*
     624             :      * Initialization
     625             :      *------------------------------------------------------------------*/
     626             : 
     627       15091 :     pitch_limit_flag = 1;
     628       15091 :     move16(); /* always extended pitch Q range */
     629       15091 :     use_fcb = 0;
     630       15091 :     unbits_PI = 0;
     631       15091 :     move16();
     632       15091 :     move16();
     633       15091 :     Pitch_CT = GENERIC;
     634       15091 :     move16();
     635       15091 :     set16_fx( cn, 0, PIT_EXC_L_SUBFR );
     636       15091 :     test();
     637       15091 :     test();
     638       15091 :     IF( st_fx->GSC_IVAS_mode > 0 && ( st_fx->GSC_noisy_speech || GT_32( st_fx->core_brate, GSC_H_RATE_STG ) ) )
     639             :     {
     640         115 :         Local_BR = ACELP_8k00;
     641         115 :         Pitch_BR = ACELP_8k00;
     642         115 :         move32();
     643         115 :         move32();
     644         115 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     645             :         {
     646           0 :             Local_BR = ACELP_14k80;
     647           0 :             move32();
     648           0 :             if ( st_fx->GSC_IVAS_mode > 0 )
     649             :             {
     650           0 :                 Local_BR = ACELP_9k60;
     651           0 :                 move32();
     652             :             }
     653           0 :             Pitch_BR = st_fx->core_brate;
     654           0 :             move32();
     655             :         }
     656             :     }
     657       14976 :     ELSE IF( st_fx->GSC_noisy_speech )
     658             :     {
     659         696 :         Local_BR = ACELP_7k20;
     660         696 :         move32();
     661         696 :         Pitch_BR = ACELP_7k20;
     662         696 :         move32();
     663         696 :         Pitch_CT = GENERIC;
     664         696 :         move16();
     665         696 :         if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     666             :         {
     667           0 :             Pitch_BR = st_fx->core_brate;
     668           0 :             move32();
     669             :         }
     670             :     }
     671             :     ELSE
     672             :     {
     673             : 
     674       14280 :         Local_BR = ACELP_7k20;
     675       14280 :         move32();
     676       14280 :         Pitch_BR = st_fx->core_brate;
     677       14280 :         move32();
     678       14280 :         Pitch_CT = AUDIO;
     679       14280 :         move16();
     680             : 
     681       14280 :         if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     682             :         {
     683        2659 :             Local_BR = ACELP_13k20;
     684        2659 :             move32();
     685        2659 :             Pitch_CT = GENERIC;
     686        2659 :             move16();
     687             :         }
     688             :     }
     689       15091 :     gain_code = 0;
     690       15091 :     move16();
     691             : 
     692       15091 :     IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     693             :     {
     694        2659 :         T0_max = PIT16k_MAX;
     695        2659 :         T0_min = PIT16k_MIN;
     696             :     }
     697             :     ELSE
     698             :     {
     699       12432 :         T0_max = PIT_MAX;
     700       12432 :         move16();
     701       12432 :         T0_min = PIT_MIN;
     702       12432 :         move16();
     703             :     }
     704       15091 :     cum_gpit = 0;
     705       15091 :     move16();
     706             : 
     707       15091 :     L_subfr = mult_r( st_fx->L_frame, div_s( 1, nb_subfr ) ); /* Q0 */
     708             : 
     709       15091 :     lp_flag = st_fx->acelp_cfg.ltf_mode;
     710       15091 :     move16();
     711             : 
     712       15091 :     test();
     713       15091 :     test();
     714       15091 :     test();
     715       15091 :     test();
     716       15091 :     test();
     717       15091 :     test();
     718       15091 :     test();
     719       15091 :     test();
     720       15091 :     IF( ( ( GE_32( st_fx->core_brate, MIN_RATE_FCB ) || ( EQ_16( st_fx->GSC_noisy_speech, 1 ) &&
     721             :                                                           ( ( EQ_16( st_fx->L_frame, L_FRAME ) && GE_32( st_fx->core_brate, ACELP_13k20 ) ) ||
     722             :                                                             ( EQ_16( st_fx->L_frame, L_FRAME16k ) && GE_32( st_fx->core_brate, GSC_H_RATE_STG ) ) || st_fx->GSC_IVAS_mode == 0 ) ) ) &&
     723             :           EQ_16( L_subfr, L_SUBFR ) ) )
     724             :     {
     725         696 :         use_fcb = 1;
     726         696 :         move16();
     727             :     }
     728       14395 :     ELSE IF( st_fx->GSC_IVAS_mode > 0 && EQ_16( L_subfr, 2 * L_SUBFR ) && LT_16( st_fx->GSC_IVAS_mode, 3 ) )
     729             :     {
     730        2041 :         use_fcb = 2;
     731        2041 :         st_fx->acelp_cfg.fcb_mode = 1;
     732        2041 :         move16();
     733        2041 :         move16();
     734        2041 :         set16_fx( st_fx->acelp_cfg.gains_mode, 6, NB_SUBFR );
     735        2041 :         set16_fx( st_fx->acelp_cfg.pitch_bits, 9, NB_SUBFR );
     736        2041 :         set16_fx( st_fx->acelp_cfg.fixed_cdk_index, 14, NB_SUBFR16k );
     737             :     }
     738             : 
     739       15091 :     *saved_bit_pos = st_fx->next_bit_pos_fx; /* Q0 */
     740       15091 :     move16();
     741             : 
     742             :     /*------------------------------------------------------------------*
     743             :      * ACELP subframe loop
     744             :      *------------------------------------------------------------------*/
     745             : 
     746       15091 :     p_Aw = Aw;
     747       15091 :     p_Aq = Aq;
     748       15091 :     pt_pitch = pitch_buf; /* pointer to the pitch buffer */
     749       15091 :     shift_wsp = add( Q_new, shift );
     750             : 
     751       44099 :     FOR( i_subfr = 0; i_subfr < st_fx->L_frame; i_subfr += L_subfr )
     752             :     {
     753             : 
     754             :         /*----------------------------------------------------------------*
     755             :          * Bandwidth expansion of A(z) filter coefficients
     756             :          * Find the the excitation search target "xn" and innovation
     757             :          *   target in residual domain "cn"
     758             :          * Compute impulse response, h1[], of weighted synthesis filter
     759             :          *----------------------------------------------------------------*/
     760       29008 :         Copy( &res[i_subfr], &exc[i_subfr], L_subfr ); /* Q_new */
     761             :         /* condition on target (compared to float) has been put outside the loop */
     762             : 
     763       29008 :         find_targets_ivas_fx( speech, hGSCEnc->mem_syn_tmp_fx, i_subfr, &hGSCEnc->mem_w0_tmp_fx, p_Aq,
     764       29008 :                               res, L_subfr, p_Aw, st_fx->preemph_fac, xn, cn, h1 );
     765             : 
     766       29008 :         q_h1 = sub( 14, norm_s( h1[0] ) );
     767       29008 :         Copy_Scale_sig( h1, h2, L_subfr, sub( 11, q_h1 ) ); /*Q11*/
     768             : 
     769             :         /* scaling of xn[] to limit dynamic at 12 bits */
     770       29008 :         Scale_sig( xn, L_subfr, shift ); /* Q_new - 1 + shift */
     771             : 
     772             :         /*----------------------------------------------------------------*
     773             :          * Close-loop pitch search and quantization
     774             :          * Adaptive exc. construction
     775             :          *----------------------------------------------------------------*/
     776             : 
     777       58016 :         *pt_pitch = pit_encode_fx( hBstr, st_fx->acelp_cfg.pitch_bits, Pitch_BR, 0, st_fx->L_frame, Pitch_CT, &pitch_limit_flag, i_subfr, exc,
     778       29008 :                                    L_subfr, st_fx->pitch, &T0_min, &T0_max, T0, T0_frac, h1, xn, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, st_fx->element_mode, Q_new ); /* Q6 */
     779             : 
     780       29008 :         Scale_sig( h1, L_subfr, sub( 14, q_h1 ) ); /* set h1[] in Q14 with scaling for convolution Q14*/
     781             : 
     782             :         /*-----------------------------------------------------------------*
     783             :          * Find adaptive exitation
     784             :          *-----------------------------------------------------------------*/
     785             : 
     786       29008 :         pred_lt4_ivas_fx( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_subfr + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     787             : 
     788             :         /*-----------------------------------------------------------------*
     789             :          * Gain clipping test to avoid unstable synthesis on frame erasure
     790             :          * or in case of floating point encoder & fixed p. decoder
     791             :          *-----------------------------------------------------------------*/
     792             : 
     793       29008 :         clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, AUDIO, xn, st_fx->clip_var_fx, sub( shift_wsp, 1 ) ); /* Q0 */
     794             : 
     795             :         /*-----------------------------------------------------------------*
     796             :          * Codebook target computation
     797             :          * (No LP filtering of the adaptive excitation)
     798             :          *-----------------------------------------------------------------*/
     799             : 
     800       29008 :         lp_select = lp_filt_exc_enc_fx( MODE1, AUDIO, i_subfr, exc, h1, xn, y1, xn2, L_subfr, st_fx->L_frame, g_corr, clip_gain, &gain_pit, &lp_flag ); /* Q0 */
     801             : 
     802       29008 :         IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
     803             :         {
     804           0 :             push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     805             :         }
     806             : 
     807       29008 :         st_fx->hSpMusClas->lowrate_pitchGain = mac_r( L_mult( 29491 /* 0.9 in Q15 */, st_fx->hSpMusClas->lowrate_pitchGain ), 3277 /* 0.1 in Q15 */, gain_pit ); /* Q14 */
     808             : 
     809       29008 :         gpit_tmp = gain_pit;
     810       29008 :         move16(); /*Q14*/
     811       29008 :         test();
     812       29008 :         IF( use_fcb > 0 )
     813             :         {
     814             :             /* h2 in Q12 for codebook search */
     815             :             /* h1 has been scaled with 1 + shift so we need to remove 2 and (1+shift) = -3 - shift*/
     816        6866 :             Copy_Scale_sig( h1, h2, L_subfr, sub( -2 - 1, shift ) );
     817             :         }
     818             : 
     819       29008 :         IF( use_fcb == 0 )
     820             :         {
     821       22142 :             IF( GE_32( st_fx->core_brate, MIN_RATE_FCB ) )
     822             :             {
     823         869 :                 pit_idx = vquant_fx( &gain_pit, mean_gp_fx, &gain_pit, dic_gp_fx, 1, 32 );
     824         869 :                 push_indice( hBstr, IND_PIT_IDX, pit_idx, 5 );
     825             :             }
     826             :             ELSE
     827             :             {
     828       21273 :                 pit_idx = vquant_fx( &gain_pit, mean_gp_fx, &gain_pit, dic_gp_fx, 1, 16 );
     829       21273 :                 push_indice( hBstr, IND_PIT_IDX, pit_idx, 4 );
     830             :             }
     831             :         }
     832        6866 :         ELSE IF( use_fcb == 2 )
     833             :         {
     834             :             /*-----------------------------------------------------------------*
     835             :              * Innovation encoding
     836             :              *-----------------------------------------------------------------*/
     837             : 
     838        4082 :             inov_encode_ivas_fx( st_fx, st_fx->core_brate, 0, st_fx->L_frame, st_fx->last_L_frame, GENERIC, st_fx->bwidth, 0, i_subfr, -1, p_Aq,
     839        4082 :                                  gain_pit, cn, exc, h2, st_fx->hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, 2 * L_SUBFR, shift, Q_new );
     840             : 
     841             :             /*-----------------------------------------------------------------*
     842             :              * Gain encoding
     843             :              *-----------------------------------------------------------------*/
     844             : 
     845        4082 :             gain_enc_lbr_fx( st_fx->hBstr, st_fx->acelp_cfg.gains_mode, GENERIC, i_subfr, xn, y1, sub( shift_wsp, 1 ), y2, code, &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, gc_mem, gp_mem, clip_gain, 2 * L_SUBFR,
     846        4082 :                              st_fx->element_mode );
     847             :         }
     848             :         ELSE
     849             :         {
     850             :             /*-----------------------------------------------------------------*
     851             :              * Innovation & gain encoding
     852             :              *-----------------------------------------------------------------*/
     853             : 
     854        2784 :             inov_encode_ivas_fx( st_fx, Local_BR, 0, st_fx->L_frame, st_fx->last_L_frame, LOCAL_CT, WB, 1, i_subfr, -1, p_Aq,
     855        2784 :                                  gain_pit, cn, exc, h2, hLPDmem->tilt_code, *pt_pitch, xn2, code, y2, &unbits_PI, L_SUBFR, shift, Q_new );
     856             : 
     857             :             /*-----------------------------------------------------------------*
     858             :              * Gain encoding
     859             :              *-----------------------------------------------------------------*/
     860             : 
     861        2784 :             gain_enc_mless_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, st_fx->L_frame, i_subfr, -1, xn, y1, sub( shift_wsp, 1 ), y2, code, Es_pred,
     862             :                                &gain_pit, &gain_code, &gain_inov, &norm_gain_code, g_corr, clip_gain );
     863             :         }
     864             : 
     865       29008 :         gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit, st_fx->clip_var_fx );
     866             : 
     867       29008 :         Lgcode = L_shl_sat( gain_code, Q_new ); /* scaled gain_code with Qnew -> Q16*/
     868       29008 :         gcode16 = round_fx_sat( Lgcode );
     869       29008 :         IF( use_fcb != 0 )
     870             :         {
     871        6866 :             hLPDmem->tilt_code = est_tilt_ivas_fx( &exc[i_subfr], gain_pit, code, gain_code, &voice_fac, Q_new, L_subfr, 0 ); // Q15
     872        6866 :             move16();
     873             :         }
     874             :         ELSE
     875             :         {
     876       22142 :             hLPDmem->tilt_code = 0;
     877       22142 :             move16();
     878             :         }
     879             : 
     880             :         /*-----------------------------------------------------------------*
     881             :          * Update memory of the weighting filter
     882             :          *-----------------------------------------------------------------*/
     883             : 
     884       29008 :         IF( use_fcb != 0 )
     885             :         {
     886        6866 :             Ltmp = L_mult0( gcode16, y2[L_subfr - 1] );
     887             : #ifdef ISSUE_1867_replace_overflow_libenc
     888        6866 :             Ltmp = L_shl_sat( Ltmp, add( 5, shift ) ); // Q_new+14+shift
     889        6866 :             Ltmp = L_negate( Ltmp );
     890        6866 :             Ltmp = L_mac_sat( Ltmp, xn[L_subfr - 1], 16384 );    // Q_new-1+15+shift
     891        6866 :             Ltmp = L_msu_sat( Ltmp, y1[L_subfr - 1], gain_pit ); // Q_new-1+15+shift
     892        6866 :             Ltmp = L_shl_sat( Ltmp, sub( 1, shift ) );           // Q_new+15
     893        6866 :             hGSCEnc->mem_w0_tmp_fx = round_fx_sat( Ltmp );       /*Q_new-1         */
     894             : #else
     895             :             Ltmp = L_shl_o( Ltmp, add( 5, shift ), &Overflow );                // Q_new+14+shift
     896             :             Ltmp = L_negate( Ltmp );
     897             :             Ltmp = L_mac_o( Ltmp, xn[L_subfr - 1], 16384, &Overflow );         // Q_new-1+15+shift
     898             :             Ltmp = L_msu_o( Ltmp, y1[L_subfr - 1], gain_pit, &Overflow );      // Q_new-1+15+shift
     899             :             Ltmp = L_shl_o( Ltmp, sub( 1, shift ), &Overflow );                // Q_new+15
     900             :             hGSCEnc->mem_w0_tmp_fx = round_fx_o( Ltmp, &Overflow );            /*Q_new-1         */
     901             : #endif
     902             :         }
     903             :         ELSE
     904             :         {
     905       22142 :             Ltmp = L_mult( xn[L_subfr - 1], 16384 );             // Q_new-1+15+shift
     906       22142 :             Ltmp = L_msu_sat( Ltmp, y1[L_subfr - 1], gain_pit ); // Q_new-1+15+shift
     907       22142 :             Ltmp = L_shl_sat( Ltmp, sub( 1, shift ) );           // Q_new+15
     908       22142 :             hGSCEnc->mem_w0_tmp_fx = round_fx_sat( Ltmp );       /*Q_new-1         */
     909             :         }
     910             : 
     911             :         /*-----------------------------------------------------------------*
     912             :          * Construct adaptive part of the excitation
     913             :          * Save the non-enhanced excitation for FEC_exc
     914             :          *-----------------------------------------------------------------*/
     915             : 
     916       29008 :         IF( use_fcb != 0 )
     917             :         {
     918      707538 :             FOR( i = 0; i < L_subfr; i++ )
     919             :             {
     920             :                 /* code in Q9, gain_pit in Q14 */
     921      700672 :                 Ltmp = L_mult( gcode16, code[i] ); /* Q10 + Q_new */
     922             : #ifdef ISSUE_1867_replace_overflow_libenc
     923      700672 :                 Ltmp = L_shl_sat( Ltmp, 5 );                          /* Q15 + Q_new */
     924      700672 :                 Ltmp = L_mac_sat( Ltmp, exc[i + i_subfr], gain_pit ); /* Q15 + Q_new */
     925      700672 :                 Ltmp = L_shl_sat( Ltmp, 1 );                          /* saturation can occur here Q16 + Q_new */
     926      700672 :                 exc[i + i_subfr] = round_fx_sat( Ltmp );              /* Q_new */
     927             : #else
     928             :                 Ltmp = L_shl_o( Ltmp, 5, &Overflow );                          /* Q15 + Q_new */
     929             :                 Ltmp = L_mac_o( Ltmp, exc[i + i_subfr], gain_pit, &Overflow ); /* Q15 + Q_new */
     930             :                 Ltmp = L_shl_o( Ltmp, 1, &Overflow );                          /* saturation can occur here Q16 + Q_new */
     931             :                 exc[i + i_subfr] = round_fx_o( Ltmp, &Overflow );              /* Q_new */
     932             : #endif
     933      700672 :                 move16();
     934             :             }
     935             :         }
     936             :         ELSE
     937             :         {
     938     3354942 :             FOR( i = 0; i < L_subfr; i++ )
     939             :             {
     940             : 
     941     3332800 :                 Ltmp = L_mult( exc[i + i_subfr], gain_pit ); /* Q15 + Q_new */
     942     3332800 :                 Ltmp = L_shl_sat( Ltmp, 1 );                 /* saturation can occur here       Q16 + Q_new */
     943     3332800 :                 exc[i + i_subfr] = round_fx_sat( Ltmp );     /* Q_new */
     944     3332800 :                 move16();
     945             :             }
     946             :         }
     947             : 
     948             :         /*-----------------------------------------------------------------*
     949             :          * Synthesize speech to update mem_syn[].
     950             :          * Update A(z) filters
     951             :          *-----------------------------------------------------------------*/
     952             : 
     953       29008 :         Syn_filt_s( 1, p_Aq, M, &exc[i_subfr], &synth[i_subfr], L_subfr, hGSCEnc->mem_syn_tmp_fx, 1 );
     954             : 
     955       29008 :         IF( EQ_16( L_subfr, 5 * L_SUBFR ) )
     956             :         {
     957        2659 :             cum_gpit = gpit_tmp;
     958        2659 :             move16();
     959        2659 :             pt_pitch++;
     960        2659 :             *pt_pitch = *( pt_pitch - 1 );
     961        2659 :             move16();
     962        2659 :             pt_pitch++;
     963        2659 :             *pt_pitch = *( pt_pitch - 1 );
     964        2659 :             move16();
     965        2659 :             pt_pitch++;
     966        2659 :             *pt_pitch = *( pt_pitch - 1 );
     967        2659 :             move16();
     968        2659 :             pt_pitch++;
     969        2659 :             *pt_pitch = *( pt_pitch - 1 );
     970        2659 :             move16();
     971        2659 :             pt_pitch++;
     972             : 
     973        2659 :             p_Aw += 5 * ( M + 1 );
     974        2659 :             p_Aq += 5 * ( M + 1 );
     975             :         }
     976       26349 :         ELSE IF( EQ_16( L_subfr, 5 * L_SUBFR / 2 ) )
     977             :         {
     978           0 :             IF( i_subfr == 0 )
     979             :             {
     980           0 :                 cum_gpit = mult_r( gpit_tmp, 13107 /* .4f in Q15 */ ); /* Q14 */
     981           0 :                 pt_pitch++;
     982           0 :                 *pt_pitch = *( pt_pitch - 1 );
     983           0 :                 move16();
     984           0 :                 pt_pitch++;
     985           0 :                 p_Aw += 2 * ( M + 1 );
     986           0 :                 p_Aq += 2 * ( M + 1 );
     987             :             }
     988             :             ELSE
     989             :             {
     990           0 :                 cum_gpit = add( cum_gpit, mult_r( gpit_tmp, 19660 /* 0.6 in Q15 */ ) ); /* Q14 */
     991           0 :                 pt_pitch++;
     992           0 :                 *pt_pitch = *( pt_pitch - 1 );
     993           0 :                 move16();
     994           0 :                 pt_pitch++;
     995           0 :                 *pt_pitch = *( pt_pitch - 1 );
     996           0 :                 move16();
     997           0 :                 pt_pitch++;
     998           0 :                 p_Aw += 3 * ( M + 1 );
     999           0 :                 p_Aq += 3 * ( M + 1 );
    1000             :             }
    1001             :         }
    1002       26349 :         ELSE IF( EQ_16( L_subfr, 2 * L_SUBFR ) )
    1003             :         {
    1004        6654 :             IF( i_subfr == 0 )
    1005             :             {
    1006        3327 :                 cum_gpit = mult_r( gpit_tmp, 16384 /* 0.5 in Q15 */ ); /* Q14 */
    1007             :             }
    1008             :             ELSE
    1009             :             {
    1010        3327 :                 cum_gpit = add( cum_gpit, mult_r( gpit_tmp, 16384 /* 0.5 in Q15 */ ) ); /* Q14 */
    1011             :             }
    1012        6654 :             p_Aw += 2 * ( M + 1 );
    1013        6654 :             move16();
    1014        6654 :             p_Aq += 2 * ( M + 1 );
    1015        6654 :             move16();
    1016        6654 :             pt_pitch++;
    1017        6654 :             *pt_pitch = *( pt_pitch - 1 );
    1018        6654 :             move16();
    1019        6654 :             pt_pitch++;
    1020             :         }
    1021       19695 :         ELSE IF( EQ_16( L_subfr, 4 * L_SUBFR ) )
    1022             :         {
    1023        5575 :             cum_gpit = gpit_tmp;
    1024        5575 :             move16();
    1025             : 
    1026        5575 :             pt_pitch++;
    1027        5575 :             *pt_pitch = *( pt_pitch - 1 );
    1028        5575 :             move16();
    1029        5575 :             pt_pitch++;
    1030        5575 :             *pt_pitch = *( pt_pitch - 1 );
    1031        5575 :             move16();
    1032        5575 :             pt_pitch++;
    1033        5575 :             *pt_pitch = *( pt_pitch - 1 );
    1034        5575 :             pt_pitch++;
    1035        5575 :             p_Aw += 4 * ( M + 1 );
    1036        5575 :             p_Aq += 4 * ( M + 1 );
    1037             :         }
    1038             :         ELSE
    1039             :         {
    1040       14120 :             IF( i_subfr == 0 )
    1041             :             {
    1042             : 
    1043        3530 :                 cum_gpit = mult_r( gpit_tmp, 8192 /* 0.25 in Q15 */ ); /* Q14 */
    1044             :             }
    1045             :             ELSE
    1046             :             {
    1047       10590 :                 cum_gpit = add( cum_gpit, mult_r( gpit_tmp, 8192 /* 0.25 in Q15 */ ) ); /* Q14 */
    1048             :             }
    1049             : 
    1050       14120 :             pt_pitch++;
    1051       14120 :             p_Aw += ( M + 1 );
    1052       14120 :             p_Aq += ( M + 1 );
    1053             :         }
    1054             :     }
    1055             : 
    1056       15091 :     cum_gpit = shl_sat( cum_gpit, 1 );                                   /*Q15*/
    1057       15091 :     *gpit = round_fx( L_mac( L_mult( 3277, *gpit ), 29491, cum_gpit ) ); /*Q15*/
    1058             : 
    1059       15091 :     return;
    1060             : }

Generated by: LCOV version 1.14