LCOV - code coverage report
Current view: top level - lib_enc - enc_gen_voic_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 296 325 91.1 %
Date: 2025-09-14 03:13:15 Functions: 3 3 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"     /* Compilation switches                   */
       6             : #include "cnst.h"        /* Common constants                       */
       7             : #include "prot_fx.h"     /* Function prototypes                    */
       8             : #include "rom_com_fx.h"  /* Static table prototypes                */
       9             : #include "rom_com.h"     /* Static table prototypes                */
      10             : #include "prot_fx_enc.h" /* Function prototypes                    */
      11             : 
      12             : 
      13             : /*-------------------------------------------------------------------*
      14             :  * rem_offset()
      15             :  *
      16             :  *
      17             :  *-------------------------------------------------------------------*/
      18             : 
      19         468 : static void rem_offset(
      20             :     const Word16 *in,
      21             :     Word16 *out,
      22             :     const Word16 l_subfr )
      23             : {
      24             :     Word16 i;
      25             :     Word16 tmp;
      26         468 :     Word32 loffset = 0;
      27         468 :     move32();
      28             : 
      29         468 :     tmp = shl( 2, norm_s( l_subfr ) ); /* l_subfr is expected to be based 2 */
      30       30420 :     FOR( i = 0; i < l_subfr; i++ )
      31             :     {
      32       29952 :         loffset = L_mac( loffset, in[i], tmp );
      33             :     }
      34         468 :     tmp = round_fx_sat( loffset );
      35       30420 :     FOR( i = 0; i < l_subfr; i++ )
      36             :     {
      37       29952 :         out[i] = sub( in[i], tmp );
      38       29952 :         move16();
      39             :     }
      40             : 
      41         468 :     return;
      42             : }
      43             : 
      44             : 
      45             : /*======================================================================*/
      46             : /* FUNCTION : encod_gen_voic_fx()                                                                               */
      47             : /*----------------------------------------------------------------------*/
      48             : /* PURPOSE : encode generic (GC), voiced (VC) and AMR-WB IO frames              */
      49             : /*                                                                                                                                              */
      50             : /*----------------------------------------------------------------------*/
      51             : /*  INPUT ARGUMENTS :                                                                                               */
      52             : /* _ (Struct)   st_fx                   : encoder static memory                                 */
      53             : /* _ (Word16) L_frame_fx                : length of the frame               Q0          */
      54             : 
      55             : /* _ (Word16[]) speech_fx                       : input speech                  Q0              */
      56             : /* _ (Word16[]) Aq_fx                   : LP filter coefficient             Q12         */
      57             : /* _ (Word16[]) A_fx                    : unquantized A(z) filter               */
      58             : /*                                with bandwidth expansion              Q12         */
      59             : /* _ (Word16) coder_type                : coding type                                                   */
      60             : /* _ (Word16) Es_pred_fx                : predicted scaled innov. energy Q8             */
      61             : /* _ (Word16[]) T_op_fx                 : open loop pitch Q0                                    */
      62             : /* _ (Word16[]) voicing_fx          : floating pitch values for each subframe Q15*/
      63             : /* _ (Word16[]) res_fx                  : residual signal                               Q_new   */
      64             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Q0)                     */
      65             : /* _ (Word16[]) exc2_fx                 : adapt. excitation/total exc (Q0)               */
      66             : /* _ (Word16[]) pitch_buf_fx    : floating pitch values for each subframe Q6*/
      67             : /* _ (Word16) shift             :       shift                                                                            */
      68             : /* _ (Word16) Q_new             :                                                            */
      69             : /*-----------------------------------------------------------------------*/
      70             : /* OUTPUT ARGUMENTS :                                                                                                    */
      71             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Q0)                     */
      72             : /* _ (Word16[]) exc2_fx                 : adapt. excitation/total exc (Q0)               */
      73             : /* _ (Word16[]) syn_fx                  :core synthesis                                 Q_new   */
      74             : /* _ (Word16[]) voice_factors_fx: voicing factors                               Q15              */
      75             : /* _ (Word16[]) bwe_exc_fx              : excitation for SWB TBE        Q0               */
      76             : /*-----------------------------------------------------------------------*/
      77             : 
      78             : 
      79             : /*-----------------------------------------------------------------------*/
      80             : /* RETURN ARGUMENTS :                                                                                                    */
      81             : /* _ None                                                                                                                                */
      82             : /*=======================================================================*/
      83             : 
      84        1291 : void encod_gen_voic_fx(
      85             :     Encoder_State *st_fx,              /* i/o: state structure                                  */
      86             :     const Word16 speech_fx[],          /* i  : input speech                                     Q0*/
      87             :     const Word16 Aw_fx[],              /* i  : weighted A(z) unquantized for subframes          Q12*/
      88             :     const Word16 Aq_fx[],              /* i  : 12k8 Lp coefficient                              Q12*/
      89             :     const Word16 Es_pred_fx,           /* i  : predicted scaled innov. energy                   Q8*/
      90             :     const Word16 *res_fx,              /* i  : residual signal                                  Q_new*/
      91             :     Word16 *syn_fx,                    /* i/o: core synthesis                                   Q_new*/
      92             :     Word16 *exc_fx,                    /* i/o: current non-enhanced excitation                  Q0*/
      93             :     Word16 *exc2_fx,                   /* i/o: current enhanced excitation                      Q0*/
      94             :     Word16 *pitch_buf_fx,              /* i/o: floating pitch values for each subframe          Q6*/
      95             :     Word16 *voice_factors_fx,          /* o  : voicing factors                                  Q15*/
      96             :     Word16 *bwe_exc_fx,                /* o  : excitation for SWB TBE                           Q0*/
      97             :     Word16 *unbits_fx,                 /* i/o: number of unused bits                                                    Q0*/
      98             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag                 Q0*/
      99             :     const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                     Q6*/
     100             :     Word16 shift,
     101             :     Word16 Q_new )
     102             : {
     103             :     Word16 xn_fx[L_SUBFR];             /* Target vector for pitch search    */
     104             :     Word16 xn2_fx[L_SUBFR];            /* Target vector for codebook search */
     105             :     Word16 cn_fx[L_SUBFR];             /* Target vector in residual domain  */
     106             :     Word16 h1_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
     107             :     Word16 h2_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
     108             :     Word16 code_fx[L_SUBFR];           /* Fixed codebook excitation         */
     109        1291 :     Word16 y1_fx[L_SUBFR] = { 0 };     /* Filtered adaptive excitation      */
     110             :     Word16 y2_fx[L_SUBFR];             /* Filtered algebraic excitation     */
     111        1291 :     Word16 gain_pit_fx = 0;            /* Pitch gain                        */
     112        1291 :     move16();
     113             :     Word16 voice_fac_fx;     /* Voicing factor                    */
     114        1291 :     Word32 gain_code_fx = 0; /* Gain of code                      */
     115        1291 :     move32();
     116        1291 :     Word16 gain_inov_fx = 0; /* inovation gain                    */
     117        1291 :     move16();
     118             :     Word32 gc_mem[NB_SUBFR - 1];      /* gain_code from previous subframes */
     119             :     Word16 gp_mem[NB_SUBFR - 1];      /* gain_pitch from previous subframes*/
     120             :     Word16 i, i_subfr_fx;             /* tmp variables                     */
     121        1291 :     Word16 T0_fx = 0, T0_frac_fx = 0; /* close loop integer pitch and fractional part */
     122        1291 :     move16();
     123        1291 :     move16();
     124             :     Word16 T0_min_fx, T0_max_fx;     /* pitch variables                   */
     125             :     Word16 *pt_pitch_fx;             /* pointer to floating pitch buffer  */
     126             :     Word16 g_corr_fx[10];            /* ACELP correl, values + gain pitch */
     127             :     Word16 clip_gain_fx;             /* LSF clip gain                     */
     128             :     const Word16 *p_Aw_fx, *p_Aq_fx; /* pointer to LP filter coeff. vector*/
     129        1291 :     Word16 error_fx = 0;
     130        1291 :     Word16 gain_preQ_fx = 0; /* Gain of prequantizer excitation   */
     131        1291 :     move16();
     132        1291 :     move16();
     133             :     Word16 code_preQ_fx[L_SUBFR]; /* Prequantizer excitation           */
     134        1291 :     Word16 unbits_PI_fx = 0;      /* number of unused bits for  PI     */
     135        1291 :     Word32 norm_gain_code_fx = 0;
     136        1291 :     move16();
     137        1291 :     move32();
     138             :     Word16 pitch_limit_flag;
     139             :     Word16 gcode16;
     140             :     Word32 Ltmp;
     141             :     Word32 Ltmp1;
     142             :     Word32 Lgcode;
     143             :     Word16 tmp1_fx;
     144             :     Word16 shift_wsp;
     145             :     Word16 harm_flag_acelp;
     146             :     Word16 lp_select, lp_flag, L_frame;
     147             : #ifndef ISSUE_1867_replace_overflow_libenc
     148             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     149             :     Flag Overflow = 0;
     150             :     move32();
     151             : #endif
     152             : #endif
     153        1291 :     SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
     154        1291 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     155        1291 :     SP_MUS_CLAS_HANDLE hSpMusClas = st_fx->hSpMusClas;
     156        1291 :     LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
     157             : 
     158             :     /*------------------------------------------------------------------*
     159             :      * Initializations
     160             :      *------------------------------------------------------------------*/
     161             : 
     162        1291 :     gain_pit_fx = 0;
     163        1291 :     move16();
     164        1291 :     gain_code_fx = L_deposit_l( 0 );
     165        1291 :     gain_preQ_fx = 0;
     166        1291 :     move16();
     167        1291 :     unbits_PI_fx = 0;
     168        1291 :     move16();
     169        1291 :     error_fx = 0;
     170        1291 :     move16();
     171        1291 :     L_frame = st_fx->L_frame;
     172        1291 :     move16();
     173             : 
     174             : 
     175        1291 :     IF( EQ_16( L_frame, L_FRAME ) )
     176             :     {
     177         667 :         T0_max_fx = PIT_MAX;
     178         667 :         move16();
     179         667 :         T0_min_fx = PIT_MIN;
     180         667 :         move16();
     181             :     }
     182             :     ELSE /* L_frame == L_FRAME16k */
     183             :     {
     184         624 :         T0_max_fx = PIT16k_MAX;
     185         624 :         move16();
     186         624 :         T0_min_fx = PIT16k_MIN;
     187         624 :         move16();
     188             :     }
     189        1291 :     lp_flag = st_fx->acelp_cfg.ltf_mode;
     190        1291 :     move16();
     191             : 
     192        1291 :     *unbits_fx = 0;
     193        1291 :     move16();
     194             : 
     195        1291 :     p_Aw_fx = Aw_fx;            /* Q12 */
     196        1291 :     p_Aq_fx = Aq_fx;            /* Q12 */
     197        1291 :     pt_pitch_fx = pitch_buf_fx; /* Q6 */
     198        1291 :     gain_preQ_fx = 0;
     199        1291 :     move16();
     200        1291 :     set16_fx( code_preQ_fx, 0, L_SUBFR );
     201             : 
     202        1291 :     shift_wsp = add( Q_new, shift );
     203             : 
     204             :     /* set and write harmonicity flag */
     205        1291 :     harm_flag_acelp = 0;
     206        1291 :     move16();
     207             : 
     208        1291 :     test();
     209        1291 :     test();
     210        1291 :     IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && LE_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( st_fx->coder_type, GENERIC ) )
     211             :     {
     212           0 :         if ( GT_16( st_fx->last_harm_flag_acelp, 2 ) )
     213             :         {
     214           0 :             harm_flag_acelp = 1;
     215           0 :             move16();
     216             :         }
     217             : 
     218           0 :         push_indice( hBstr, IND_HARM_FLAG_ACELP, harm_flag_acelp, 1 );
     219             :     }
     220             : 
     221             :     /*------------------------------------------------------------------*
     222             :      * ACELP subframe loop
     223             :      *------------------------------------------------------------------*/
     224             : 
     225        7079 :     FOR( i_subfr_fx = 0; i_subfr_fx < L_frame; i_subfr_fx += L_SUBFR )
     226             :     {
     227             : 
     228             :         /*----------------------------------------------------------------*
     229             :          * Find the the excitation search target "xn" and innovation
     230             :          *   target in residual domain "cn"
     231             :          * Compute impulse response, h1[], of weighted synthesis filter
     232             :          *----------------------------------------------------------------*/
     233             : 
     234        5788 :         Copy( &res_fx[i_subfr_fx], &exc_fx[i_subfr_fx], L_SUBFR ); /*Q_new*/
     235             : 
     236        5788 :         find_targets_fx( speech_fx, hLPDmem->mem_syn, i_subfr_fx, &hLPDmem->mem_w0, p_Aq_fx,
     237        5788 :                          res_fx, L_SUBFR, p_Aw_fx, st_fx->preemph_fac, xn_fx, cn_fx, h1_fx );
     238             : 
     239        5788 :         Copy_Scale_sig( h1_fx, h2_fx, L_SUBFR, -2 );  /*Q11*/
     240        5788 :         Scale_sig( h1_fx, L_SUBFR, add( 1, shift ) ); /* set h1[] in Q14 with scaling for convolution Q(14+shift)*/
     241             : 
     242             :         /* scaling of xn[] to limit dynamic at 12 bits */
     243        5788 :         Scale_sig( xn_fx, L_SUBFR, shift );
     244             : 
     245             :         /*----------------------------------------------------------------*
     246             :          * Close-loop pitch search and quantization
     247             :          *----------------------------------------------------------------*/
     248             : 
     249       11576 :         *pt_pitch_fx = pit_encode_fx( hBstr, st_fx->acelp_cfg.pitch_bits, st_fx->core_brate, 0, L_frame, st_fx->coder_type, &pitch_limit_flag, i_subfr_fx, exc_fx,
     250        5788 :                                       L_SUBFR, st_fx->pitch, &T0_min_fx, &T0_max_fx, &T0_fx, &T0_frac_fx, h1_fx, xn_fx, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, st_fx->element_mode, Q_new ); /* Q6 */
     251             : 
     252        5788 :         tbe_celp_exc( L_frame, i_subfr_fx, T0_fx, T0_frac_fx, &error_fx, bwe_exc_fx );
     253             : 
     254             :         /*-----------------------------------------------------------------*
     255             :          * Find adaptive exitation
     256             :          *-----------------------------------------------------------------*/
     257             : 
     258        5788 :         pred_lt4( &exc_fx[i_subfr_fx], &exc_fx[i_subfr_fx], T0_fx, T0_frac_fx, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     259             : 
     260             :         /*-----------------------------------------------------------------*
     261             :          * Gain clipping test to avoid unstable synthesis on frame erasure
     262             :          *-----------------------------------------------------------------*/
     263             : 
     264        5788 :         clip_gain_fx = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr_fx, st_fx->coder_type, xn_fx, st_fx->clip_var_fx, sub( shift_wsp, 1 ) );
     265             : 
     266        5788 :         if ( EQ_16( st_fx->coder_type, INACTIVE ) )
     267             :         {
     268             :             /* in case of AVQ inactive, limit the gain to 0.65 */
     269           0 :             clip_gain_fx = 2;
     270           0 :             move16();
     271             :         }
     272             : 
     273             :         /*-----------------------------------------------------------------*
     274             :          * LP filtering of the adaptive excitation, codebook target computation
     275             :          *-----------------------------------------------------------------*/
     276             : 
     277        5788 :         lp_select = lp_filt_exc_enc_fx( MODE1, st_fx->coder_type, i_subfr_fx, exc_fx, h1_fx,
     278             :                                         xn_fx, y1_fx, xn2_fx, L_SUBFR, L_frame, g_corr_fx, clip_gain_fx, &gain_pit_fx, &lp_flag );
     279             : 
     280        5788 :         IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
     281             :         {
     282        5788 :             push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     283             :         }
     284             : 
     285             :         /*st_fx->lowrate_pitchGain = 0.9f * st_fx->lowrate_pitchGain + 0.1f * gain_pit_fx;*/
     286             : #ifdef ISSUE_1867_replace_overflow_libenc
     287        5788 :         hSpMusClas->lowrate_pitchGain = round_fx_sat( L_mac_sat( L_mult( 29491, hSpMusClas->lowrate_pitchGain ), 6554, gain_pit_fx ) ); /*Q14*Q16(0.1) + Q15 -> Q15*/
     288             : #else
     289             :         hSpMusClas->lowrate_pitchGain = round_fx_o( L_mac_o( L_mult( 29491, hSpMusClas->lowrate_pitchGain ), 6554, gain_pit_fx, &Overflow ), &Overflow ); /*Q14*Q16(0.1) + Q15 -> Q15*/
     290             : #endif
     291             : 
     292             :         /*-----------------------------------------------------------------*
     293             :          * Transform domain contribution encoding - active frames
     294             :          *-----------------------------------------------------------------*/
     295             : 
     296        5788 :         test();
     297        5788 :         IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && NE_16( st_fx->coder_type, INACTIVE ) )
     298             :         {
     299        3120 :             transf_cdbk_enc_fx( st_fx, harm_flag_acelp, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx,
     300             :                                 xn2_fx, y1_fx, y2_fx, Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
     301             :         }
     302             : 
     303             :         /*-----------------------------------------------------------------*
     304             :          * Innovation encoding
     305             :          *-----------------------------------------------------------------*/
     306             : 
     307        5788 :         inov_encode_fx( st_fx, st_fx->core_brate, 0, L_frame, st_fx->last_L_frame,
     308        5788 :                         st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag, i_subfr_fx, -1, p_Aq_fx,
     309        5788 :                         gain_pit_fx, cn_fx, exc_fx, h2_fx, hLPDmem->tilt_code, *pt_pitch_fx, xn2_fx, code_fx, y2_fx, &unbits_PI_fx, L_SUBFR, shift );
     310             : 
     311             :         /*-----------------------------------------------------------------*
     312             :          * Gain encoding
     313             :          *-----------------------------------------------------------------*/
     314             : 
     315        5788 :         IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
     316             :         {
     317           0 :             gain_enc_lbr_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->coder_type, i_subfr_fx, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx,
     318             :                              &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, gc_mem, gp_mem, clip_gain_fx, L_SUBFR,
     319           0 :                              st_fx->element_mode );
     320             :         }
     321        5788 :         ELSE IF( GT_32( st_fx->core_brate, ACELP_32k ) )
     322             :         {
     323        3120 :             gain_enc_SQ_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr_fx, xn_fx, y1_fx, y2_fx, code_fx, Es_pred_fx,
     324             :                             &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx, shift_wsp );
     325             :         }
     326             :         ELSE
     327             :         {
     328        2668 :             gain_enc_mless_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_frame, i_subfr_fx, -1, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx, Es_pred_fx,
     329             :                                &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx );
     330             :         }
     331        5788 :         IF( st_fx->Opt_SC_VBR )
     332             :         {
     333           0 :             if ( EQ_16( hSC_VBR->last_ppp_mode, 1 ) )
     334             :             {
     335             :                 /* SC-VBR - all other st->clip_var values will be updated even in a PPP frame */
     336           0 :                 st_fx->clip_var_fx[1] = gain_pit_fx; /*Q14*/
     337           0 :                 move16();
     338             :             }
     339             :         }
     340        5788 :         gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit_fx, st_fx->clip_var_fx );
     341             : 
     342             : #ifdef ISSUE_1867_replace_overflow_libenc
     343        5788 :         Lgcode = L_shl_sat( gain_code_fx, Q_new ); /* scaled gain_code with Qnew -> Q16*/
     344        5788 :         gcode16 = round_fx_sat( Lgcode );          /*Q0*/
     345             : #else
     346             :         Lgcode = L_shl_o( gain_code_fx, Q_new, &Overflow );                                                                                               /* scaled gain_code with Qnew -> Q16*/
     347             :         gcode16 = round_fx_o( Lgcode, &Overflow );                                                                                                        /*Q0*/
     348             : #endif
     349             : 
     350        5788 :         hLPDmem->tilt_code = Est_tilt2( &exc_fx[i_subfr_fx], gain_pit_fx, code_fx, Lgcode, &voice_fac_fx, shift ); /* Q15 */
     351        5788 :         move16();
     352             : 
     353             :         /*-----------------------------------------------------------------*
     354             :          * Transform domain contribution encoding - inactive frames
     355             :          *-----------------------------------------------------------------*/
     356             : 
     357        5788 :         test();
     358        5788 :         IF( GE_32( st_fx->core_brate, MAX_GSC_INACTIVE_BRATE ) && EQ_16( st_fx->coder_type, INACTIVE ) )
     359             :         {
     360           0 :             transf_cdbk_enc_fx( st_fx, 0, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx, xn2_fx, y1_fx, y2_fx,
     361             :                                 Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
     362             :         }
     363             : 
     364             :         /*-----------------------------------------------------------------*
     365             :          * Update memory of the weighting filter
     366             :          *-----------------------------------------------------------------*/
     367             : 
     368             :         /* st_fx->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]); */
     369        5788 :         Ltmp = L_mult( gcode16, y2_fx[L_SUBFR - 1] ); /*Q10*/
     370        5788 :         Ltmp = L_shl( Ltmp, add( 5, shift ) );        /*Q15+shift*/
     371        5788 :         Ltmp = L_negate( Ltmp );
     372        5788 :         Ltmp = L_mac( Ltmp, xn_fx[L_SUBFR - 1], 16384 /*Q14*/ );       /* Q_new-1+shift+14+1 */
     373        5788 :         Ltmp = L_msu( Ltmp, y1_fx[L_SUBFR - 1], gain_pit_fx /*Q14*/ ); /* Q_new-1+shift+14+1 */
     374        5788 :         Ltmp = L_shl_sat( Ltmp, sub( 1, shift ) );                     /* 15 + Q_new */
     375        5788 :         hLPDmem->mem_w0 = round_fx_sat( Ltmp );                        /*Q_new-1        */
     376        5788 :         move16();
     377        5788 :         IF( gain_preQ_fx != 0 )
     378             :         {
     379        3120 :             tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
     380             : 
     381      202800 :             FOR( i = 0; i < L_SUBFR; i++ )
     382             :             {
     383             :                 /* Contribution from AVQ layer */
     384             : #ifdef ISSUE_1867_replace_overflow_libenc
     385      199680 :                 Ltmp1 = L_mult_sat( gain_preQ_fx, code_preQ_fx[i] ); /* Q2 + Q6 -> Q9*/
     386      199680 :                 Ltmp1 = L_shl_sat( Ltmp1, tmp1_fx );                 /* Q16 + Q_exc */
     387             : #else
     388             :                 Ltmp1 = L_mult_o( gain_preQ_fx, code_preQ_fx[i], &Overflow );                                                                             /* Q2 + Q6 -> Q9*/
     389             :                 Ltmp1 = L_shl_o( Ltmp1, tmp1_fx, &Overflow );                                                                                             /* Q16 + Q_exc */
     390             : #endif
     391             : 
     392             :                 /* Compute exc2 */
     393             : #ifdef ISSUE_1867_replace_overflow_libenc
     394      199680 :                 Ltmp = L_shl_sat( L_mult( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 1 ); /* Q16 */
     395      199680 :                 exc2_fx[i + i_subfr_fx] = round_fx_sat( L_add_sat( Ltmp, Ltmp1 ) );   /* Q0 */
     396             : #else
     397             :                 Ltmp = L_shl_o( L_mult( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 1, &Overflow );                                                            /* Q16 */
     398             :                 exc2_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow );                                                     /* Q0 */
     399             : #endif
     400      199680 :                 move16();
     401             : 
     402             :                 /* code in Q9, gain_pit in Q14 */
     403      199680 :                 Ltmp = L_mult( gcode16, code_fx[i] );                      /* Q10 */
     404      199680 :                 Ltmp = L_shl( Ltmp, 5 );                                   /* Q15 */
     405      199680 :                 Ltmp = L_mac( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx ); /* Q15 */
     406             : #ifdef ISSUE_1867_replace_overflow_libenc
     407      199680 :                 Ltmp = L_shl_sat( Ltmp, 1 );                                       /* saturation can occur here Q16*/
     408      199680 :                 exc_fx[i + i_subfr_fx] = round_fx_sat( L_add_sat( Ltmp, Ltmp1 ) ); /* Q0 */
     409             : #else
     410             :                 Ltmp = L_shl_o( Ltmp, 1, &Overflow );                                                                                                     /* saturation can occur here Q16*/
     411             :                 exc_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow );                                                      /* Q0 */
     412             : #endif
     413             :             }
     414             :         }
     415             :         ELSE
     416             :         {
     417             :             /*-----------------------------------------------------------------*
     418             :              * Construct adaptive part of the excitation
     419             :              * Save the non-enhanced excitation for FEC_exc
     420             :              *-----------------------------------------------------------------*/
     421             : 
     422      173420 :             FOR( i = 0; i < L_SUBFR; i++ )
     423             :             {
     424             :                 /* code in Q9, gain_pit in Q14 */
     425      170752 :                 Ltmp = L_mult( gcode16, code_fx[i] ); /* Q10 */
     426             : #ifdef ISSUE_1867_replace_overflow_libenc
     427      170752 :                 Ltmp = L_shl_sat( Ltmp, 5 );                                   /* Q15 */
     428      170752 :                 Ltmp = L_mac_sat( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx ); /* Q15 */
     429      170752 :                 Ltmp = L_shl_sat( Ltmp, 1 );                                   /* saturation can occur here Q16*/
     430      170752 :                 exc_fx[i + i_subfr_fx] = round_fx_sat( Ltmp );                 /* Q0 */
     431             : #else
     432             :                 Ltmp = L_shl_o( Ltmp, 5, &Overflow );                                                                                                     /* Q15 */
     433             :                 Ltmp = L_mac_o( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx, &Overflow );                                                                   /* Q15 */
     434             :                 Ltmp = L_shl_o( Ltmp, 1, &Overflow );                                                                                                     /* saturation can occur here Q16*/
     435             :                 exc_fx[i + i_subfr_fx] = round_fx_o( Ltmp, &Overflow );                                                                                   /* Q0 */
     436             : #endif
     437             :             }
     438             :         }
     439             : 
     440             :         /*-----------------------------------------------------------------*
     441             :          * Prepare TBE excitation
     442             :          *-----------------------------------------------------------------*/
     443             : 
     444        5788 :         prep_tbe_exc_fx( L_frame, L_SUBFR, i_subfr_fx, gain_pit_fx, gain_code_fx, code_fx, voice_fac_fx,
     445        5788 :                          &voice_factors_fx[i_subfr_fx / L_SUBFR], bwe_exc_fx, gain_preQ_fx, code_preQ_fx, Q_new,
     446        5788 :                          T0_fx, T0_frac_fx, st_fx->coder_type, st_fx->core_brate,
     447        5788 :                          st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
     448             : 
     449             :         /*-----------------------------------------------------------------*
     450             :          * Synthesize speech to update mem_syn[].
     451             :          * Update A(z) filters
     452             :          *-----------------------------------------------------------------*/
     453             : 
     454        5788 :         Syn_filt_s( 1, p_Aq_fx, M, &exc_fx[i_subfr_fx], &syn_fx[i_subfr_fx], L_SUBFR, hLPDmem->mem_syn, 1 );
     455             : 
     456        5788 :         p_Aw_fx += ( M + 1 );
     457        5788 :         p_Aq_fx += ( M + 1 );
     458        5788 :         pt_pitch_fx++;
     459             :     }
     460             : 
     461             :     /* write reserved bits */
     462        1291 :     WHILE( unbits_PI_fx > 0 )
     463             :     {
     464           0 :         i = s_min( unbits_PI_fx, 16 );
     465           0 :         push_indice( hBstr, IND_UNUSED, 0, i );
     466           0 :         unbits_PI_fx -= i;
     467             :     }
     468        1291 :     IF( st_fx->Opt_SC_VBR )
     469             :     {
     470             :         /* SC-VBR */
     471           0 :         hSC_VBR->prev_ppp_gain_pit_fx = gain_pit_fx; /* Q14 */
     472           0 :         move16();
     473           0 :         hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code; /* Q15 */
     474           0 :         move16();
     475             :     }
     476             : 
     477        1291 :     return;
     478             : }
     479             : 
     480             : 
     481      117765 : void encod_gen_voic_ivas_fx(
     482             :     Encoder_State *st_fx,              /* i/o: state structure                                  */
     483             :     const Word16 speech_fx[],          /* i  : input speech                                     Qnew -1 */
     484             :     const Word16 Aw_fx[],              /* i  : weighted A(z) unquantized for subframes          Q12*/
     485             :     const Word16 Aq_fx[],              /* i  : 12k8 Lp coefficient                              Q12*/
     486             :     const Word16 Es_pred_fx,           /* i  : predicted scaled innov. energy                   Q8*/
     487             :     const Word16 *res_fx,              /* i  : residual signal                                  Q_new*/
     488             :     Word16 *syn_fx,                    /* i/o: core synthesis                                   Q_new - 1*/
     489             :     Word16 *exc_fx,                    /* i/o: current non-enhanced excitation                  Q_new*/
     490             :     Word16 *exc2_fx,                   /* i/o: current enhanced excitation                      Q_new*/
     491             :     Word16 *pitch_buf_fx,              /* i/o: floating pitch values for each subframe          Q6*/
     492             :     Word16 *voice_factors_fx,          /* o  : voicing factors                                  Q15*/
     493             :     Word16 *bwe_exc_fx,                /* o  : excitation for SWB TBE                           Q0*/
     494             :     Word16 *unbits_fx,                 /* i/o: number of unused bits                                                    Q0*/
     495             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag                 Q0*/
     496             :     const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                     Q6*/
     497             :     Word16 shift,
     498             :     Word16 Q_new )
     499             : {
     500             :     Word16 xn_fx[L_SUBFR];             /* Target vector for pitch search    */
     501             :     Word16 xn2_fx[L_SUBFR];            /* Target vector for codebook search */
     502             :     Word16 cn_fx[L_SUBFR];             /* Target vector in residual domain  */
     503             :     Word16 h1_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
     504             :     Word16 h2_fx[L_SUBFR + ( M + 1 )]; /* Impulse response vector           */
     505             :     Word16 code_fx[L_SUBFR];           /* Fixed codebook excitation         */
     506      117765 :     Word16 y1_fx[L_SUBFR] = { 0 };     /* Filtered adaptive excitation      */
     507             :     Word16 y2_fx[L_SUBFR];             /* Filtered algebraic excitation     */
     508      117765 :     Word16 gain_pit_fx = 0;            /* Pitch gain                        */
     509      117765 :     move16();
     510             :     Word16 voice_fac_fx;     /* Voicing factor                    */
     511      117765 :     Word32 gain_code_fx = 0; /* Gain of code                      */
     512      117765 :     Word16 gain_inov_fx = 0; /* inovation gain                    */
     513      117765 :     move32();
     514      117765 :     move16();
     515             :     Word32 gc_mem[NB_SUBFR - 1];      /* gain_code from previous subframes */
     516             :     Word16 gp_mem[NB_SUBFR - 1];      /* gain_pitch from previous subframes*/
     517             :     Word16 i, i_subfr_fx;             /* tmp variables                     */
     518      117765 :     Word16 T0_fx = 0, T0_frac_fx = 0; /* close loop integer pitch and fractional part */
     519      117765 :     move16();
     520      117765 :     move16();
     521             :     Word16 T0_min_fx, T0_max_fx;     /* pitch variables                   */
     522             :     Word16 *pt_pitch_fx;             /* pointer to floating pitch buffer  */
     523             :     Word16 g_corr_fx[10];            /* ACELP correl, values + gain pitch */
     524             :     Word16 clip_gain_fx;             /* LSF clip gain                     */
     525             :     const Word16 *p_Aw_fx, *p_Aq_fx; /* pointer to LP filter coeff. vector*/
     526      117765 :     Word16 error_fx = 0;
     527      117765 :     Word16 gain_preQ_fx = 0; /* Gain of prequantizer excitation   */
     528      117765 :     move16();
     529      117765 :     move16();
     530             :     Word16 code_preQ_fx[L_SUBFR]; /* Prequantizer excitation           */
     531      117765 :     Word16 unbits_PI_fx = 0;      /* number of unused bits for  PI     */
     532      117765 :     Word32 norm_gain_code_fx = 0;
     533      117765 :     move16();
     534      117765 :     move32();
     535             :     Word16 pitch_limit_flag;
     536             :     Word16 gcode16;
     537             :     Word32 Ltmp;
     538             :     Word32 Ltmp1;
     539             :     Word32 Lgcode;
     540             :     Word16 tmp1_fx;
     541             :     Word16 shift_wsp;
     542             :     Word16 harm_flag_acelp;
     543             :     Word16 lp_select, lp_flag, L_frame;
     544             :     Word16 q_h1;
     545             : #ifndef ISSUE_1867_replace_overflow_libenc
     546             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     547             :     Flag Overflow = 0;
     548             :     move32();
     549             : #endif
     550             : #endif
     551      117765 :     SC_VBR_ENC_HANDLE hSC_VBR = st_fx->hSC_VBR;
     552      117765 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     553      117765 :     LPD_state_HANDLE hLPDmem = st_fx->hLPDmem;
     554             : 
     555             :     /*------------------------------------------------------------------*
     556             :      * Initializations
     557             :      *------------------------------------------------------------------*/
     558             : 
     559      117765 :     gain_pit_fx = 0;
     560      117765 :     move16();
     561      117765 :     gain_code_fx = L_deposit_l( 0 );
     562      117765 :     gain_preQ_fx = 0;
     563      117765 :     move16();
     564      117765 :     unbits_PI_fx = 0;
     565      117765 :     move16();
     566      117765 :     error_fx = 0;
     567      117765 :     move16();
     568      117765 :     L_frame = st_fx->L_frame;
     569      117765 :     move16();
     570             : 
     571      117765 :     IF( EQ_16( L_frame, L_FRAME ) )
     572             :     {
     573       53879 :         T0_max_fx = PIT_MAX;
     574       53879 :         move16();
     575       53879 :         T0_min_fx = PIT_MIN;
     576       53879 :         move16();
     577             :     }
     578             :     ELSE /* L_frame == L_FRAME16k */
     579             :     {
     580       63886 :         T0_max_fx = PIT16k_MAX;
     581       63886 :         move16();
     582       63886 :         T0_min_fx = PIT16k_MIN;
     583       63886 :         move16();
     584             :     }
     585      117765 :     lp_flag = st_fx->acelp_cfg.ltf_mode;
     586      117765 :     move16();
     587             : 
     588      117765 :     *unbits_fx = 0;
     589      117765 :     move16();
     590             : 
     591      117765 :     p_Aw_fx = Aw_fx;            /*Q12*/
     592      117765 :     p_Aq_fx = Aq_fx;            /*Q12*/
     593      117765 :     pt_pitch_fx = pitch_buf_fx; /*Q6*/
     594      117765 :     gain_preQ_fx = 0;
     595      117765 :     move16();
     596      117765 :     set16_fx( code_preQ_fx, 0, L_SUBFR );
     597             : 
     598      117765 :     shift_wsp = add( Q_new, shift );
     599      117765 :     if ( LT_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) )
     600             :     {
     601      105265 :         shift_wsp = sub( shift_wsp, 1 );
     602             :     }
     603             : 
     604             :     /* set and write harmonicity flag */
     605      117765 :     harm_flag_acelp = 0;
     606      117765 :     move16();
     607             : 
     608      117765 :     test();
     609      117765 :     test();
     610      117765 :     IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && LE_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) && EQ_16( st_fx->coder_type, GENERIC ) )
     611             :     {
     612        8017 :         if ( GT_16( st_fx->last_harm_flag_acelp, 2 ) )
     613             :         {
     614         719 :             harm_flag_acelp = 1;
     615         719 :             move16();
     616             :         }
     617             : 
     618        8017 :         push_indice( hBstr, IND_HARM_FLAG_ACELP, harm_flag_acelp, 1 );
     619             :     }
     620             : 
     621             :     /*------------------------------------------------------------------*
     622             :      * ACELP subframe loop
     623             :      *------------------------------------------------------------------*/
     624             : 
     625      652711 :     FOR( i_subfr_fx = 0; i_subfr_fx < L_frame; i_subfr_fx += L_SUBFR )
     626             :     {
     627             : 
     628             :         /*----------------------------------------------------------------*
     629             :          * Find the the excitation search target "xn" and innovation
     630             :          *   target in residual domain "cn"
     631             :          * Compute impulse response, h1[], of weighted synthesis filter
     632             :          *----------------------------------------------------------------*/
     633             : 
     634      534946 :         Copy( &res_fx[i_subfr_fx], &exc_fx[i_subfr_fx], L_SUBFR ); /*Q_new*/
     635             : 
     636      534946 :         find_targets_ivas_fx( speech_fx, hLPDmem->mem_syn, i_subfr_fx, &hLPDmem->mem_w0, p_Aq_fx,
     637      534946 :                               res_fx, L_SUBFR, p_Aw_fx, st_fx->preemph_fac, xn_fx, cn_fx, h1_fx );
     638             : 
     639      534946 :         q_h1 = sub( 14, norm_s( h1_fx[0] ) );
     640      534946 :         Copy_Scale_sig( h1_fx, h2_fx, L_SUBFR, sub( 11, q_h1 ) ); /*Q11*/
     641      534946 :         Scale_sig( h1_fx, L_SUBFR, sub( 14, q_h1 ) );             /* set h1[] in Q14 with scaling for convolution Q14*/
     642             :         /* scaling of xn[] to limit dynamic at 12 bits */
     643      534946 :         Scale_sig( xn_fx, L_SUBFR, shift );
     644      534946 :         test();
     645      534946 :         test();
     646      534946 :         IF( Es_pred_fx < 0 && LE_16( i_subfr_fx, L_SUBFR ) && NE_16( st_fx->last_core, ACELP_CORE ) )
     647             :         {
     648             :             /* During core transition, when the temporal correlation is non existent  */
     649         234 :             rem_offset( xn_fx, xn_fx, L_SUBFR );
     650         234 :             rem_offset( cn_fx, cn_fx, L_SUBFR );
     651             :         }
     652             : 
     653             :         /*----------------------------------------------------------------*
     654             :          * Close-loop pitch search and quantization
     655             :          *----------------------------------------------------------------*/
     656             : 
     657     1069892 :         *pt_pitch_fx = pit_encode_fx( hBstr, st_fx->acelp_cfg.pitch_bits, st_fx->core_brate, 0, L_frame, st_fx->coder_type, &pitch_limit_flag, i_subfr_fx, exc_fx,
     658      534946 :                                       L_SUBFR, st_fx->pitch, &T0_min_fx, &T0_max_fx, &T0_fx, &T0_frac_fx, h1_fx, xn_fx, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf, st_fx->element_mode, Q_new ); /* Q6 */
     659      534946 :         move16();
     660             : 
     661      534946 :         tbe_celp_exc_ivas( st_fx->element_mode, st_fx->idchan, L_frame, L_SUBFR, i_subfr_fx, T0_fx, T0_frac_fx, &error_fx, bwe_exc_fx, st_fx->tdm_LRTD_flag );
     662             : 
     663             :         /*-----------------------------------------------------------------*
     664             :          * Find adaptive exitation
     665             :          *-----------------------------------------------------------------*/
     666             : 
     667      534946 :         pred_lt4_ivas_fx( &exc_fx[i_subfr_fx], &exc_fx[i_subfr_fx], T0_fx, T0_frac_fx, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     668             : 
     669             :         /*-----------------------------------------------------------------*
     670             :          * Gain clipping test to avoid unstable synthesis on frame erasure
     671             :          *-----------------------------------------------------------------*/
     672             : 
     673      534946 :         clip_gain_fx = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr_fx, st_fx->coder_type, xn_fx, st_fx->clip_var_fx, sub( shift_wsp, 1 ) );
     674             : 
     675      534946 :         if ( EQ_16( st_fx->coder_type, INACTIVE ) )
     676             :         {
     677             :             /* in case of AVQ inactive, limit the gain to 0.65 */
     678       13030 :             clip_gain_fx = 2;
     679       13030 :             move16();
     680             :         }
     681             : 
     682             :         /*-----------------------------------------------------------------*
     683             :          * LP filtering of the adaptive excitation, codebook target computation
     684             :          *-----------------------------------------------------------------*/
     685             : 
     686      534946 :         lp_select = lp_filt_exc_enc_fx( MODE1, st_fx->coder_type, i_subfr_fx, exc_fx, h1_fx,
     687             :                                         xn_fx, y1_fx, xn2_fx, L_SUBFR, L_frame, g_corr_fx, clip_gain_fx, &gain_pit_fx, &lp_flag );
     688             : 
     689      534946 :         IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
     690             :         {
     691      466030 :             push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     692             :         }
     693             : 
     694             :         /*-----------------------------------------------------------------*
     695             :          * Transform domain contribution encoding - active frames
     696             :          *-----------------------------------------------------------------*/
     697             : 
     698      534946 :         test();
     699      534946 :         IF( GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && NE_16( st_fx->coder_type, INACTIVE ) )
     700             :         {
     701       51245 :             transf_cdbk_enc_ivas_fx( st_fx, harm_flag_acelp, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx,
     702             :                                      xn2_fx, y1_fx, y2_fx, Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
     703             :         }
     704             : 
     705             :         /*-----------------------------------------------------------------*
     706             :          * Innovation encoding
     707             :          *-----------------------------------------------------------------*/
     708             : 
     709      534946 :         inov_encode_ivas_fx( st_fx, st_fx->core_brate, 0, L_frame, st_fx->last_L_frame,
     710      534946 :                              st_fx->coder_type, st_fx->bwidth, st_fx->sharpFlag, i_subfr_fx, -1, p_Aq_fx,
     711      534946 :                              gain_pit_fx, cn_fx, exc_fx, h2_fx, hLPDmem->tilt_code, *pt_pitch_fx, xn2_fx, code_fx, y2_fx, &unbits_PI_fx, L_SUBFR, shift, Q_new );
     712             : 
     713             :         /*-----------------------------------------------------------------*
     714             :          * Gain encoding
     715             :          *-----------------------------------------------------------------*/
     716             : 
     717      534946 :         IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
     718             :         {
     719       13560 :             gain_enc_lbr_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->coder_type, i_subfr_fx, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx,
     720             :                              &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, gc_mem, gp_mem, clip_gain_fx, L_SUBFR,
     721       13560 :                              st_fx->element_mode );
     722             :         }
     723      521386 :         ELSE IF( GT_32( st_fx->core_brate, ACELP_32k ) )
     724             :         {
     725       40380 :             gain_enc_SQ_fx( hBstr, st_fx->acelp_cfg.gains_mode, i_subfr_fx, xn_fx, y1_fx, y2_fx, code_fx, Es_pred_fx,
     726             :                             &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx, shift_wsp );
     727             :         }
     728             :         ELSE
     729             :         {
     730      481006 :             gain_enc_mless_fx( hBstr, st_fx->acelp_cfg.gains_mode, st_fx->element_mode, L_frame, i_subfr_fx, -1, xn_fx, y1_fx, shift_wsp, y2_fx, code_fx, Es_pred_fx,
     731             :                                &gain_pit_fx, &gain_code_fx, &gain_inov_fx, &norm_gain_code_fx, g_corr_fx, clip_gain_fx );
     732             :         }
     733      534946 :         IF( st_fx->Opt_SC_VBR )
     734             :         {
     735           0 :             if ( EQ_16( hSC_VBR->last_ppp_mode, 1 ) )
     736             :             {
     737             :                 /* SC-VBR - all other st->clip_var values will be updated even in a PPP frame */
     738           0 :                 st_fx->clip_var_fx[1] = gain_pit_fx; /* Q14 */
     739           0 :                 move16();
     740             :             }
     741             :         }
     742      534946 :         gp_clip_test_gain_pit_fx( st_fx->element_mode, st_fx->core_brate, gain_pit_fx, st_fx->clip_var_fx );
     743             : 
     744             : #ifdef ISSUE_1867_replace_overflow_libenc
     745      534946 :         Lgcode = L_shl_sat( gain_code_fx, Q_new ); /* scaled gain_code with Qnew -> Q16*/
     746      534946 :         gcode16 = round_fx_sat( Lgcode );
     747             : #else
     748             :         Lgcode = L_shl_o( gain_code_fx, Q_new, &Overflow );                                                                                               /* scaled gain_code with Qnew -> Q16*/
     749             :         gcode16 = round_fx_o( Lgcode, &Overflow );
     750             : #endif
     751             : 
     752      534946 :         hLPDmem->tilt_code = est_tilt_ivas_fx( &exc_fx[i_subfr_fx], gain_pit_fx, code_fx, gain_code_fx, &voice_fac_fx, Q_new, L_SUBFR, 0 ); /* Q15 */
     753      534946 :         move16();
     754             : 
     755             :         /*-----------------------------------------------------------------*
     756             :          * Transform domain contribution encoding - inactive frames
     757             :          *-----------------------------------------------------------------*/
     758             : 
     759      534946 :         test();
     760      534946 :         IF( !st_fx->inactive_coder_type_flag && EQ_16( st_fx->coder_type, INACTIVE ) )
     761             :         {
     762       13030 :             transf_cdbk_enc_ivas_fx( st_fx, 0, i_subfr_fx, cn_fx, exc_fx, p_Aq_fx, p_Aw_fx, h1_fx, xn_fx, xn2_fx, y1_fx, y2_fx,
     763             :                                      Es_pred_fx, &gain_pit_fx, gain_code_fx, g_corr_fx, clip_gain_fx, &gain_preQ_fx, code_preQ_fx, unbits_fx, Q_new, shift );
     764             :         }
     765             : 
     766             :         /*-----------------------------------------------------------------*
     767             :          * Update memory of the weighting filter
     768             :          *-----------------------------------------------------------------*/
     769             : 
     770             :         /*At this point
     771             :         xn has to be in Qnew
     772             :         yn has to be in Qnew
     773             :         y2_fx has to be in Q9
     774             :         gcode16 in Q_new
     775             :         gain_pit_fx in Q14
     776             :         */
     777      534946 :         Scale_sig( xn_fx, L_SUBFR, 1 );
     778      534946 :         Scale_sig( y1_fx, L_SUBFR, 1 );
     779             :         /* st_fx->mem_w0 = xn[L_SUBFR-1] - (gain_pit*y1[L_SUBFR-1]) - (gain_code*y2[L_SUBFR-1]); */
     780      534946 :         Ltmp = L_mult0( gcode16, y2_fx[L_SUBFR - 1] );
     781      534946 :         Ltmp = L_shl( Ltmp, add( 5, shift ) );
     782      534946 :         Ltmp = L_negate( Ltmp );
     783      534946 :         Word32 Ltmp2 = L_mult0( xn_fx[L_SUBFR - 1], 16384 );
     784      534946 :         Ltmp = L_add( Ltmp, Ltmp2 );
     785      534946 :         Word32 Ltmp3 = L_mult0( y1_fx[L_SUBFR - 1], gain_pit_fx );
     786      534946 :         Ltmp = L_sub( Ltmp, Ltmp3 );
     787             : 
     788             :         /*Ltmp is in Q14 + Qnew here
     789             :         We need memWo in Qnew -1 */
     790             : 
     791      534946 :         Ltmp = L_shl( Ltmp, sub( 1, shift ) );  // Q14 + Qnew + 1
     792      534946 :         hLPDmem->mem_w0 = round_fx_sat( Ltmp ); /*Q_new-1        */
     793      534946 :         move16();
     794      534946 :         IF( gain_preQ_fx != 0 )
     795             :         {
     796       64270 :             tmp1_fx = add( 16 - ( 2 + Q_AVQ_OUT_DEC + 1 ), Q_new );
     797             : 
     798     4177550 :             FOR( i = 0; i < L_SUBFR; i++ )
     799             :             {
     800             :                 /* Contribution from AVQ layer */
     801             : #ifdef ISSUE_1867_replace_overflow_libenc
     802     4113280 :                 Ltmp1 = L_mult_sat( gain_preQ_fx, code_preQ_fx[i] );
     803     4113280 :                 Ltmp1 = L_shl_sat( Ltmp1, tmp1_fx );
     804             : #else
     805             :                 Ltmp1 = L_mult_o( gain_preQ_fx, code_preQ_fx[i], &Overflow );
     806             :                 Ltmp1 = L_shl_o( Ltmp1, tmp1_fx, &Overflow );
     807             : #endif
     808             : 
     809             :                 /* Compute exc2 */
     810             : #ifdef ISSUE_1867_replace_overflow_libenc
     811     4113280 :                 Ltmp = L_shl_sat( L_mult( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 1 );
     812     4113280 :                 exc2_fx[i + i_subfr_fx] = round_fx_sat( L_add_sat( Ltmp, Ltmp1 ) );
     813             : #else
     814             :                 Ltmp = L_shl_o( L_mult( gain_pit_fx, exc_fx[i + i_subfr_fx] ), 1, &Overflow );
     815             :                 exc2_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow );
     816             : #endif
     817     4113280 :                 move16();
     818             : 
     819             :                 /* code in Q9, gain_pit in Q14 */
     820     4113280 :                 Ltmp = L_mult( gcode16, code_fx[i] );                      /*Qnew + 9 + 1 */
     821     4113280 :                 Ltmp = L_shl( Ltmp, 5 );                                   /*Qnew + 9+ 1+5 */
     822     4113280 :                 Ltmp = L_mac( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx ); /*Qnew + 14 + 1*/
     823             : #ifdef ISSUE_1867_replace_overflow_libenc
     824     4113280 :                 Ltmp = L_shl_sat( Ltmp, 1 ); /*Qnew + 14 + 1 +1  */ /* saturation can occur here Q16*/
     825     4113280 :                 exc_fx[i + i_subfr_fx] = round_fx_sat( L_add_sat( Ltmp, Ltmp1 ) );
     826             : #else
     827             :                 Ltmp = L_shl_o( Ltmp, 1, &Overflow ); /*Qnew + 14 + 1 +1  */ /* saturation can occur here Q16*/
     828             :                 exc_fx[i + i_subfr_fx] = round_fx_o( L_add_o( Ltmp, Ltmp1, &Overflow ), &Overflow );
     829             : #endif
     830     4113280 :                 move16();
     831             :             }
     832             :         }
     833             :         ELSE
     834             :         {
     835             :             /*-----------------------------------------------------------------*
     836             :              * Construct adaptive part of the excitation
     837             :              * Save the non-enhanced excitation for FEC_exc
     838             :              *-----------------------------------------------------------------*/
     839             : 
     840    30593940 :             FOR( i = 0; i < L_SUBFR; i++ )
     841             :             {
     842             :                 /* code in Q9, gain_pit in Q14 */
     843             :                 /*gcode16 in Qnew*/
     844    30123264 :                 Ltmp = L_mult( gcode16, code_fx[i] ); /*Qnew + 9 + 1 */
     845             : #ifdef ISSUE_1867_replace_overflow_libenc
     846    30123264 :                 Ltmp = L_shl_sat( Ltmp, 5 );                                   /*Qnew + 9+ 1+5 */
     847    30123264 :                 Ltmp = L_mac_sat( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx ); /*Qnew + 14 + 1*/
     848    30123264 :                 Ltmp = L_shl_sat( Ltmp, 1 );                                   /*Qnew + 14 + 1 +1  */
     849    30123264 :                 exc_fx[i + i_subfr_fx] = round_fx_sat( Ltmp );
     850             : #else
     851             :                 Ltmp = L_shl_o( Ltmp, 5, &Overflow );                                   /*Qnew + 9+ 1+5 */
     852             :                 Ltmp = L_mac_o( Ltmp, exc_fx[i + i_subfr_fx], gain_pit_fx, &Overflow ); /*Qnew + 14 + 1*/
     853             :                 Ltmp = L_shl_o( Ltmp, 1, &Overflow );                                   /*Qnew + 14 + 1 +1  */
     854             :                 exc_fx[i + i_subfr_fx] = round_fx_o( Ltmp, &Overflow );
     855             : #endif
     856             :             }
     857             :         }
     858             :         /*-----------------------------------------------------------------*
     859             :          * Prepare TBE excitation
     860             :          *-----------------------------------------------------------------*/
     861             : 
     862      534946 :         prep_tbe_exc_fx( L_frame, L_SUBFR, i_subfr_fx, gain_pit_fx, gain_code_fx, code_fx, voice_fac_fx,
     863      534946 :                          &voice_factors_fx[i_subfr_fx / L_SUBFR], bwe_exc_fx, gain_preQ_fx, code_preQ_fx, Q_new,
     864      534946 :                          T0_fx, T0_frac_fx, st_fx->coder_type, st_fx->core_brate,
     865      534946 :                          st_fx->element_mode, st_fx->idchan, st_fx->hBWE_TD != NULL, st_fx->tdm_LRTD_flag );
     866             : 
     867             :         /*-----------------------------------------------------------------*
     868             :          * Synthesize speech to update mem_syn[].
     869             :          * Update A(z) filters
     870             :          *-----------------------------------------------------------------*/
     871             : 
     872      534946 :         syn_filt_fx( 1, p_Aq_fx, M, &exc_fx[i_subfr_fx], &syn_fx[i_subfr_fx], L_SUBFR, hLPDmem->mem_syn, 1 );
     873             : 
     874      534946 :         p_Aw_fx += ( M + 1 );
     875      534946 :         p_Aq_fx += ( M + 1 );
     876      534946 :         pt_pitch_fx++;
     877             :     }
     878             : 
     879             :     /* write reserved bits */
     880      117765 :     WHILE( unbits_PI_fx > 0 )
     881             :     {
     882           0 :         i = s_min( unbits_PI_fx, 16 );
     883           0 :         push_indice( hBstr, IND_UNUSED, 0, i );
     884           0 :         unbits_PI_fx -= i;
     885             :     }
     886      117765 :     IF( st_fx->Opt_SC_VBR )
     887             :     {
     888             :         /* SC-VBR */
     889           0 :         hSC_VBR->prev_ppp_gain_pit_fx = gain_pit_fx;
     890           0 :         move16();
     891           0 :         hSC_VBR->prev_tilt_code_fx = hLPDmem->tilt_code;
     892           0 :         move16();
     893             :     }
     894      117765 :     return;
     895             : }

Generated by: LCOV version 1.14