LCOV - code coverage report
Current view: top level - lib_enc - transition_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 1044 1067 97.8 %
Date: 2025-09-14 03:13:15 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       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             :  * Local function prototypes
      15             :  *-----------------------------------------------------------------*/
      16             : 
      17             : static void gain_trans_enc_fx( Word32 gain_trans32, Word16 exc[], Word16 *quant_index, Word16 *quant_sign, Word16 Q_new );
      18             : 
      19             : static void tc_enc_fx( Encoder_State *st_fx, const Word16 i_subfr, Word16 *tc_subfr, Word16 *position, const Word16 *h1_fx, const Word16 *xn_fx, Word16 *exc_fx, Word16 *yy1_fx, Word16 *T0_min, Word16 *T0_max, Word16 *T0, Word16 *T0_frac, Word16 *gain_pit_fx, Word16 g_corr_fx[], Word16 *bwe_exc_fx, Word16 Q_new );
      20             : static void tc_enc_ivas_fx( Encoder_State *st_fx, const Word16 i_subfr, Word16 *tc_subfr, Word16 *position, const Word16 *h1_fx, const Word16 *xn_fx, Word16 *exc_fx, Word16 *yy1_fx, Word16 *T0_min, Word16 *T0_max, Word16 *T0, Word16 *T0_frac, Word16 *gain_pit_fx, Word16 g_corr_fx[], Word16 *bwe_exc_fx, Word16 Q_new );
      21             : 
      22             : 
      23             : /*==========================================================================*/
      24             : /* FUNCTION   : void transition_enc_fx ()                                                                   */
      25             : /*--------------------------------------------------------------------------*/
      26             : /* PURPOSE    :Principal function for adaptive excitation construction in TC*/
      27             : /*--------------------------------------------------------------------------*/
      28             : /* INPUT ARGUMENTS  :                                                                                                           */
      29             : /*   (Word32) core_brate                : core bitrate                                                  Q0      */
      30             : /*       (Word16)  bwidth               : input signal bandwidth                                Q0      */
      31             : /*       (Word16)  i_subfr                      : subrame index                                                 Q0      */
      32             : /*       (Word16*) Jopt_flag            : joint optimization flag                               Q0      */
      33             : /*       (Word16*) h1_fx                        : weighted filter input response                Q14     */
      34             : /*       (Word16*) xn_fx                        : target signal                                                 Q_new*/
      35             : /*       (Word16)  Q_new                        : input scaling                                     */
      36             : /*--------------------------------------------------------------------------*/
      37             : /* OUTPUT ARGUMENTS :                                                                                                           */
      38             : /*       (Word16*) y1_fx            : zero-memory filtered adaptive excitation Q12*/
      39             : /*       (Word16*) xn2_fx               : target vector for innovation search  Qnew */
      40             : /*       (Word16*) gain_pit_fx      : pitch gain  (0..GAIN_PIT_MAX)                      Q14*/
      41             : /*       (Word16[])g_corr_fx        : correlations <y1,y1>  and -2<xn,y1>       */
      42             : /*       (Word16**)pt_pitch_fx      : floating pitch values                  Q6 */
      43             : /*       (Word16*) bwe_exc_fx           : excitation for SWB TBE                             Q0 */
      44             : /*--------------------------------------------------------------------------*/
      45             : /* INPUT/OUTPUT ARGUMENTS :                                                                                                     */
      46             : /*       (Word16*) tc_subfr                 : TC subframe index                                     Q0  */
      47             : /*       (Word16*) position                 : index of the residual signal maximum      Q0  */
      48             : /*       (Word16*) T0_min               : lower pitch limit                                         Q0  */
      49             : /*       (Word16*) T0_max                   : higher pitch limit                                        Q0  */
      50             : /*       (Word16*) T0                           : close loop integer pitch                              Q0  */
      51             : /*       (Word16*) T0_frac                  : close loop fractional part of the pitch Q0*/
      52             : /*       (Word16*) exc_fx           : pointer to excitation signal frame                */
      53             : /*       (Word16*) gp_cl_fx         : memory of gain of pitch clipping algorithm*/
      54             : /*       (Word16*) clip_gain        : adaptive gain clipping flag                       Q0      */
      55             : /*--------------------------------------------------------------------------*/
      56             : /* RETURN ARGUMENTS :                                                                                                           */
      57             : /*                                       _ None                                                                                                 */
      58             : /*--------------------------------------------------------------------------*/
      59             : 
      60         546 : void transition_enc_fx(
      61             :     Encoder_State *st_fx, /* i/o: encoder state structure */
      62             :     const Word16 i_subfr, /* i  : subframe index                              */
      63             :     Word16 *tc_subfr,     /* i/o: TC subframe index                           */
      64             :     Word16 *Jopt_flag,    /* i  : joint optimization flag                     */
      65             :     Word16 *position,     /* i/o: maximum of residual signal index            */
      66             :     Word16 *T0,           /* i/o: close loop integer pitch                    Q0*/
      67             :     Word16 *T0_frac,      /* i/o: close loop fractional part of the pitch     Q0*/
      68             :     Word16 *T0_min,       /* i/o: lower limit for close-loop search           Q0*/
      69             :     Word16 *T0_max,       /* i/o: higher limit for close-loop search          Q0*/
      70             :     Word16 *exc_fx,       /* i/o: pointer to excitation signal frame          Q_new*/
      71             :     Word16 *y1_fx,        /* o  : zero-memory filtered adaptive excitation    Q_new-1+shift*/
      72             :     const Word16 *h1_fx,  /* i  : weighted filter input response              Q(14+shift)*/
      73             :     const Word16 *xn_fx,  /* i  : target vector                               Q_new-1+shift*/
      74             :     Word16 *xn2_fx,       /* o  : target vector for innovation search         Q_new-1+shift*/
      75             :     Word16 *gp_cl_fx,     /* i/o: memory of gain of pitch clipping algorithm  */
      76             :     Word16 *gain_pit_fx,  /* o  : adaptive excitation gain                    Q14*/
      77             :     Word16 *g_corr_fx,    /* o  : ACELP correlation values                    */
      78             :     Word16 *clip_gain,    /* i/o: adaptive gain clipping flag                 */
      79             :     Word16 **pt_pitch_fx, /* o  : floating pitch values                       */
      80             :     Word16 *bwe_exc_fx,   /* o  : excitation for SWB TBE                      Q_new*/
      81             :     Word16 *unbits_ACELP, /* i/o: unused bits                                 */
      82             :     Word16 Q_new,         /* i  : Current scaling */
      83             :     Word16 shift          /* i  : downscaling needs for 12 bits convolutions */
      84             : 
      85             : )
      86             : {
      87             :     Word16 pit_flag, pit_start, pit_limit, index, nBits;
      88             :     Word16 tmp, tmp1, i;
      89             :     Word32 offset;
      90             :     Word16 shift_wsp;
      91             :     Word16 limit_flag, mult_Top, lp_select, lp_flag;
      92             :     Word16 T_op[2]; /* values for two half-frames */
      93         546 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
      94             : 
      95             :     /* set limit_flag to 0 for restrained limits, and 1 for extended limits */
      96         546 :     limit_flag = 0;
      97         546 :     move16();
      98             : 
      99         546 :     pit_start = PIT_MIN;
     100         546 :     move16();
     101             : 
     102             :     /*----------------------------------------------------------------*
     103             :      * convert pitch values to 16kHz domain
     104             :      *----------------------------------------------------------------*/
     105             : 
     106         546 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) /*|| (tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0)*/ )
     107             :     {
     108         296 :         Copy( st_fx->pitch, T_op, 2 );
     109             :     }
     110             :     ELSE /* L_frame == L_FRAME16k  */
     111             :     {
     112             :         /*T_op[0] = (int16_t)(pitch[0] * 1.25f + 0.5f);
     113             :         T_op[1] = (int16_t)(pitch[1] * 1.25f + 0.5f);*/
     114         250 :         T_op[0] = add( st_fx->pitch[0], mult_r( st_fx->pitch[0], 8192 /*0.25f Q15*/ ) );
     115         250 :         T_op[1] = add( st_fx->pitch[1], mult_r( st_fx->pitch[1], 8192 /*0.25f Q15*/ ) );
     116         250 :         move16();
     117         250 :         move16();
     118             :     }
     119         546 :     shift_wsp = add( Q_new, shift );
     120             : 
     121         546 :     lp_flag = st_fx->acelp_cfg.ltf_mode;
     122         546 :     move16();
     123             : 
     124             :     /*-----------------------------------------------------------------*
     125             :      * TC: subrame determination for glottal shape search
     126             :      * -------------------------------------------------------
     127             :      * tc_subfr == 0         - TC in 1st subframe
     128             :      * tc_subfr == TC_0_0    - TC in 1st subframe + information about T0
     129             :      * tc_subfr == L_SUBFR   - TC in 2nd subframe
     130             :      * tc_subfr == 2*L_SUBFR - TC in 3rd subframe
     131             :      * tc_subfr == 3*L_SUBFR - TC in 4th subframe
     132             :      *-----------------------------------------------------------------*/
     133             : 
     134         546 :     IF( i_subfr == 0 )
     135             :     {
     136         124 :         mult_Top = 1;
     137         124 :         move16();
     138         124 :         IF( limit_flag == 0 )
     139             :         {
     140         124 :             test();
     141         124 :             IF( EQ_16( st_fx->L_frame, L_FRAME ) && LT_16( T_op[1], PIT_MIN ) )
     142             :             {
     143          14 :                 mult_Top = 2;
     144          14 :                 move16();
     145             :             }
     146         124 :             test();
     147         124 :             if ( EQ_16( st_fx->L_frame, L_FRAME16k ) && LT_16( T_op[1], PIT16k_MIN ) )
     148             :             {
     149           2 :                 mult_Top = 2;
     150           2 :                 move16();
     151             :             }
     152             :         }
     153             : 
     154         124 :         limit_T0_fx( st_fx->L_frame, 8, 0, limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     155             :     }
     156             : 
     157             :     /*-----------------------------------------------------------------*
     158             :      * zero adaptive excitation signal construction
     159             :      *-----------------------------------------------------------------*/
     160             : 
     161         546 :     IF( GT_16( *tc_subfr, i_subfr ) )
     162             :     {
     163          79 :         *gain_pit_fx = 0;
     164          79 :         move16();
     165          79 :         *clip_gain = 0;
     166          79 :         move16();
     167          79 :         g_corr_fx[0] = 16384;
     168          79 :         move16();
     169          79 :         g_corr_fx[1] = add( shl( sub( shift_wsp, 1 ), 1 ), 1 );
     170          79 :         move16();
     171          79 :         g_corr_fx[2] = -16384;
     172          79 :         move16();
     173          79 :         g_corr_fx[3] = shl( sub( shift_wsp, 1 ), 1 );
     174          79 :         move16();
     175             : 
     176          79 :         set16_fx( &exc_fx[i_subfr], 0, L_SUBFR ); /* set excitation for current subrame to 0 */
     177             : 
     178          79 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     179             :         {
     180          67 :             set16_fx( &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) ); /* set past excitation buffer to 0 */
     181             :         }
     182             :         ELSE
     183             :         {
     184          12 :             set16_fx( &bwe_exc_fx[i_subfr * 2], 0, L_SUBFR * 2 ); /* set past excitation buffer to 0 */
     185             :         }
     186             : 
     187          79 :         set16_fx( y1_fx, 0, L_SUBFR );  /* set filtered adaptive excitation to 0 */
     188          79 :         Copy( xn_fx, xn2_fx, L_SUBFR ); /* target vector for codebook search */
     189          79 :         *T0 = L_SUBFR;
     190          79 :         move16();
     191          79 :         *T0_frac = 0;
     192          79 :         move16();
     193             : 
     194          79 :         **pt_pitch_fx = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     195          79 :         move16(); /* save subframe pitch values Q6 */
     196             :     }
     197             : 
     198             :     /*-----------------------------------------------------------------*
     199             :      * glottal codebook contribution construction
     200             :      *-----------------------------------------------------------------*/
     201         467 :     ELSE IF( EQ_16( *tc_subfr, i_subfr ) )
     202             :     {
     203         124 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     204             :         {
     205          74 :             set16_fx( bwe_exc_fx - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
     206             :         }
     207             :         ELSE
     208             :         {
     209          50 :             set16_fx( bwe_exc_fx - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
     210             :         }
     211             : 
     212         124 :         tc_enc_fx( st_fx, i_subfr, tc_subfr, position, h1_fx, xn_fx, exc_fx,
     213             :                    y1_fx, T0_min, T0_max, T0, T0_frac, gain_pit_fx, g_corr_fx, bwe_exc_fx, Q_new );
     214             : 
     215         124 :         IF( EQ_16( *tc_subfr, TC_0_0 ) )
     216             :         {
     217             :             /* this is called only to compute unused bits */
     218          31 :             config_acelp1_fx( ENC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl, st_fx->extl_brate, L_FRAME, -1, &( st_fx->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, TC_0_0, 3, NULL, unbits_ACELP, st_fx->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st_fx->idchan, 0 /*tdm_Pitch_reuse_flag*/, st_fx->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
     219             :         }
     220             : 
     221         124 :         *clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, TRANSITION, xn_fx, gp_cl_fx, sub( shift_wsp, 1 ) );
     222         124 :         move16();
     223         124 :         updt_tar_fx( xn_fx, xn2_fx, y1_fx, *gain_pit_fx, L_SUBFR );
     224             : 
     225         124 :         **pt_pitch_fx = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     226         124 :         move16();
     227         124 :         *Jopt_flag = 1;
     228         124 :         move16();
     229             :     }
     230             : 
     231             :     /*--------------------------------------------------------------*
     232             :      * other subframes -> GENERIC encoding type,
     233             :      * standard adaptive excitation contribution
     234             :      * - exemption only in case when first glottal impulse is
     235             :      * in the 1st subframe and the second one in 2nd subframe
     236             :      * and later
     237             :      *--------------------------------------------------------------*/
     238             : 
     239         343 :     ELSE IF( LT_16( *tc_subfr, i_subfr ) )
     240             :     {
     241         343 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
     242             :         {
     243         155 :             *Jopt_flag = 1;
     244         155 :             move16();
     245             :             /* pit_flag for T0 bits number coding determination */
     246         155 :             test();
     247         155 :             IF( ( EQ_16( sub( i_subfr, *tc_subfr ), L_SUBFR ) ) || ( EQ_16( sub( i_subfr, *tc_subfr ), L_SUBFR - TC_0_0 ) ) )
     248             :             {
     249          54 :                 pit_flag = 0;
     250          54 :                 move16();
     251             :             }
     252             :             ELSE
     253             :             {
     254         101 :                 pit_flag = L_SUBFR;
     255         101 :                 move16();
     256             :             }
     257             : 
     258         155 :             IF( EQ_16( *tc_subfr, TC_0_0 ) )
     259             :             {
     260          93 :                 IF( EQ_16( i_subfr, L_SUBFR ) )
     261             :                 {
     262          31 :                     limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
     263             :                 }
     264          93 :                 pit_flag = 1;
     265          93 :                 move16();
     266             :             }
     267             : 
     268             :             /*----------------------------------------------------------*
     269             :              * if tc_subfr==0, change tc_subfr corresponding to the
     270             :              * second glot. impulse position
     271             :              *----------------------------------------------------------*/
     272             : 
     273         155 :             test();
     274         155 :             IF( ( *tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
     275             :             {
     276          17 :                 IF( GT_16( PIT_MIN, ( *position ) ) )
     277             :                 {
     278          12 :                     pit_start = sub( L_SUBFR, ( *position ) );
     279             :                 }
     280             :                 ELSE
     281             :                 {
     282           5 :                     pit_start = PIT_MIN;
     283           5 :                     move16();
     284             :                 }
     285          17 :                 pit_start = s_max( pit_start, PIT_MIN );
     286             : 
     287          17 :                 pit_limit = add( shl( pit_start, 1 ), *position );
     288             : 
     289             :                 /* Find the closed loop pitch period */
     290          17 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, pit_limit, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     291          17 :                 move16();
     292             : 
     293          17 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
     294             : 
     295             : 
     296        2737 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     297             :                 {
     298        2720 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
     299        2720 :                     move16();
     300             :                 }
     301             : 
     302             : 
     303          17 :                 test();
     304          17 :                 IF( GT_16( ( *T0 ), sub( 2 * L_SUBFR, ( *position ) ) ) )
     305             :                 {
     306           8 :                     IF( GE_16( add( ( *T0 ), ( *position ) ), 3 * L_SUBFR ) )
     307             :                     {
     308             :                         /* second glottal impulse is in the 4th subframe */
     309           3 :                         *tc_subfr = TC_0_192;
     310           3 :                         move16();
     311             :                     }
     312             :                     ELSE
     313             :                     {
     314             :                         /* second glottal impulse is in the 3rd subframe */
     315           5 :                         *tc_subfr = TC_0_128;
     316           5 :                         move16();
     317             :                     }
     318             :                 }
     319           9 :                 ELSE IF( ( *tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
     320             :                 {
     321             :                     /* second glottal impulse is in the 2nd subframe */
     322           9 :                     *tc_subfr = TC_0_64;
     323           9 :                     move16();
     324             :                 }
     325             :             }
     326             : 
     327         155 :             IF( LE_16( sub( i_subfr, *tc_subfr ), L_SUBFR ) )
     328             :             {
     329          54 :                 config_acelp1_fx( ENC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl, st_fx->extl_brate, st_fx->L_frame, -1, &( st_fx->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, *tc_subfr, 2, NULL, unbits_ACELP, st_fx->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st_fx->idchan, 0 /*tdm_Pitch_reuse_flag*/, st_fx->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
     330             :             }
     331             : 
     332             :             /*-----------------------------------------------------------------*
     333             :              * get number of bits for pitch encoding
     334             :              *-----------------------------------------------------------------*/
     335             : 
     336         155 :             nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
     337         155 :             move16();
     338             : 
     339             :             /*-----------------------------------------------------------------*
     340             :              * Find adaptive part of excitation, encode pitch period
     341             :              *-----------------------------------------------------------------*/
     342             : 
     343         155 :             test();
     344         155 :             test();
     345         155 :             test();
     346         155 :             test();
     347         155 :             test();
     348         155 :             test();
     349             :             /* first glottal impulse is in the 1st subrame */
     350         155 :             IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( GE_16( *tc_subfr, TC_0_128 ) ) )
     351             :             {
     352             :                 /*--------------------------------------------------------*
     353             :                  * second glottal impulse is in the 3rd or 4th subframe
     354             :                  * - build exc[] in 2nd subframe
     355             :                  *--------------------------------------------------------*/
     356             : 
     357           8 :                 *T0 = 2 * L_SUBFR;
     358           8 :                 move16();
     359           8 :                 *T0_frac = 0;
     360           8 :                 move16();
     361           8 :                 *Jopt_flag = 0;
     362           8 :                 move16();
     363           8 :                 set16_fx( &exc_fx[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
     364           8 :                 set16_fx( &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     365             :             }
     366         147 :             ELSE IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_64 ) ) )
     367             :             {
     368             :                 /*--------------------------------------------------------*
     369             :                  * second glottal impulse is in the 2nd subframe,
     370             :                  * - build exc[] in 2nd subframe
     371             : 
     372             :                 *--------------------------------------------------------*/
     373             : 
     374           9 :                 IF( LT_16( add( *T0, *position ), L_SUBFR ) )
     375             :                 {
     376             :                     /* impulse must be in the 2nd subframe (not in 1st) */
     377           0 :                     *T0 = sub( L_SUBFR, ( *position ) );
     378           0 :                     move16();
     379           0 :                     *T0_frac = 0;
     380           0 :                     move16();
     381             :                 }
     382           9 :                 IF( GE_16( add( *T0, *position ), 2 * L_SUBFR ) )
     383             :                 {
     384             :                     /* impulse must be in the 2nd subframe (not in 3rd) */
     385           1 :                     *T0 = sub( 2 * L_SUBFR - 1, ( *position ) );
     386           1 :                     move16();
     387           1 :                     *T0_frac = 2;
     388           1 :                     move16();
     389             :                 }
     390             : 
     391           9 :                 limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
     392             : 
     393             :                 /* 7bit ENCODER */
     394             :                 /*  index = (*T0-pit_start)*2 + *T0_frac/2;*/
     395           9 :                 index = add( shl( sub( *T0, pit_start ), 1 ), shr( *T0_frac, 1 ) );
     396           9 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     397             : 
     398             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     399           9 :                 pred_lt4( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     400             : 
     401           9 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
     402             : 
     403        1449 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     404             :                 {
     405        1440 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
     406        1440 :                     move16();
     407             :                 }
     408             :             }
     409         138 :             ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_128 ) ) )
     410             :             {
     411             :                 /*--------------------------------------------------------*
     412             :                  * second glottal impulse is in the 3rd subframe
     413             :                  * - build exc[] in 3rd subframe
     414             :                  *--------------------------------------------------------*/
     415             : 
     416           5 :                 pit_start = sub( 2 * L_SUBFR, ( *position ) );
     417           5 :                 pit_flag = 0;
     418           5 :                 move16();
     419             : 
     420           5 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, 3 * L_SUBFR, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     421           5 :                 move16();
     422             : 
     423           5 :                 IF( LT_16( add( ( *T0 ), ( *position ) ), 2 * L_SUBFR ) )
     424             :                 {
     425             :                     /* impulse must be in the 3rd subframe (not in 2nd) */
     426           0 :                     *T0 = sub( 2 * L_SUBFR, ( *position ) );
     427           0 :                     move16();
     428           0 :                     *T0_frac = 0;
     429           0 :                     move16();
     430             :                 }
     431             : 
     432           5 :                 IF( GE_16( add( ( *T0 ), ( *position ) ), 3 * L_SUBFR ) )
     433             :                 {
     434             :                     /* impulse must be in the 3rd subframe (not in 4th) */
     435           0 :                     *T0 = sub( 3 * L_SUBFR - 1, ( *position ) );
     436           0 :                     move16();
     437           0 :                     *T0_frac = 2;
     438           0 :                     move16();
     439             :                 }
     440             : 
     441           5 :                 limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
     442             : 
     443           5 :                 index = add( shl( sub( *T0, pit_start ), 1 ), shr( *T0_frac, 1 ) );
     444           5 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     445             : 
     446             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     447           5 :                 pred_lt4( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     448             : 
     449           5 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
     450             : 
     451             : 
     452         805 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     453             :                 {
     454         800 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
     455         800 :                     move16();
     456             :                 }
     457             :             }
     458         133 :             ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_192 ) ) )
     459             :             {
     460             :                 /*--------------------------------------------------------*
     461             :                  * second glottal impulse is in the 4th subframe
     462             :                  * - build exc[] in 3rd subframe
     463             :                  *--------------------------------------------------------*/
     464           3 :                 *T0 = 4 * L_SUBFR;
     465           3 :                 move16();
     466           3 :                 *T0_frac = 0;
     467           3 :                 move16();
     468           3 :                 *Jopt_flag = 0;
     469           3 :                 move16();
     470           3 :                 set16_fx( &exc_fx[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
     471           3 :                 set16_fx( &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     472             :             }
     473         130 :             ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_192 ) ) )
     474             :             {
     475             :                 /*--------------------------------------------------------*
     476             :                  * second glottal impulse is in the 4th subframe
     477             :                  * - build exc[] in 4th subframe
     478             :                  *--------------------------------------------------------*/
     479             : 
     480             :                 /* always T0_frac = 0 */
     481           3 :                 pit_flag = 0;
     482           3 :                 move16();
     483             : 
     484           3 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     485           3 :                 move16();
     486             : 
     487           3 :                 IF( LT_16( add( *T0, *position ), 3 * L_SUBFR ) )
     488             :                 {
     489             :                     /* impulse must be in the 4th subframe (not in 3rd) */
     490           1 :                     *T0 = sub( 3 * L_SUBFR, ( *position ) );
     491           1 :                     move16();
     492           1 :                     *T0_frac = 0;
     493           1 :                     move16();
     494             :                 }
     495             : 
     496           3 :                 pit_start = sub( 3 * L_SUBFR, ( *position ) );
     497           3 :                 pit_limit = sub( 2 * L_FRAME - PIT_MAX - 2, shl( *position, 1 ) );
     498             : 
     499           3 :                 IF( LT_16( ( *T0 ), pit_limit ) )
     500             :                 {
     501           2 :                     index = add( shl( sub( *T0, pit_start ), 1 ), shr( *T0_frac, 1 ) );
     502             :                 }
     503             :                 ELSE
     504             :                 {
     505           1 :                     index = add( sub( *T0, pit_limit ), shl( sub( pit_limit, pit_start ), 1 ) );
     506           1 :                     *T0_frac = 0;
     507           1 :                     move16();
     508             :                 }
     509           3 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     510             : 
     511             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     512           3 :                 pred_lt4( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     513             : 
     514           3 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
     515             : 
     516         483 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     517             :                 {
     518         480 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
     519         480 :                     move16();
     520             :                 }
     521             :             }
     522         127 :             ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_128 ) ) )
     523             :             {
     524             :                 /*--------------------------------------------------------*
     525             :                  * second glottal impulse in the 3rd subframe
     526             :                  * build exc[] in 4th subframe
     527             :                  *--------------------------------------------------------*/
     528             : 
     529           5 :                 pit_flag = L_SUBFR;
     530           5 :                 move16();
     531           5 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     532           5 :                 move16();
     533           5 :                 index = delta_pit_enc_fx( 2, *T0, *T0_frac, *T0_min );
     534           5 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     535             : 
     536             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     537           5 :                 pred_lt4( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     538             : 
     539           5 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
     540             : 
     541             : 
     542         805 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     543             :                 {
     544         800 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
     545         800 :                     move16();
     546             :                 }
     547             :             }
     548             : 
     549             :             /*------------------------------------------------------------*
     550             :              * first glottal impulse is NOT in the 1st subframe,
     551             :              * or two impulses are in the 1st subframe
     552             :              *------------------------------------------------------------*/
     553             : 
     554             :             ELSE
     555             :             {
     556         122 :                 test();
     557         122 :                 IF( EQ_16( nBits, 8 ) || EQ_16( nBits, 5 ) )
     558             :                 {
     559          31 :                     test();
     560          31 :                     test();
     561          31 :                     IF( !( ( *tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) ) )
     562             :                     {
     563          31 :                         *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     564          31 :                         move16();
     565             :                     }
     566             :                 }
     567             :                 ELSE
     568             :                 {
     569          91 :                     *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     570          91 :                     move16();
     571             :                 }
     572         122 :                 pit_Q_enc_fx( hBstr, 0, nBits, 8, pit_flag, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     573             : 
     574             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     575         122 :                 pred_lt4( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     576             : 
     577         122 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
     578             : 
     579       19642 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     580             :                 {
     581       19520 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
     582       19520 :                     move16();
     583             :                 }
     584             :             }
     585             : 
     586             :             /*-----------------------------------------------------------------*
     587             :              * - gain clipping test to avoid unstable synthesis
     588             :              * - LP filtering of the adaptive excitation (if non-zero)
     589             :              * - codebook target computation
     590             :              *-----------------------------------------------------------------*/
     591             : 
     592         155 :             IF( *Jopt_flag == 0 )
     593             :             {
     594             :                 /* adaptive/TC excitation is zero */
     595          11 :                 Copy( xn_fx, xn2_fx, L_SUBFR );
     596          11 :                 g_corr_fx[0] = 0;
     597          11 :                 move16();
     598          11 :                 g_corr_fx[1] = 0;
     599          11 :                 move16();
     600          11 :                 g_corr_fx[2] = 0;
     601          11 :                 move16();
     602          11 :                 g_corr_fx[3] = 0;
     603          11 :                 move16();
     604          11 :                 *clip_gain = 0;
     605          11 :                 move16();
     606             :             }
     607             :             ELSE
     608             :             {
     609         144 :                 *clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, TRANSITION, xn_fx, gp_cl_fx, ( Q_new + shift - 1 ) );
     610         144 :                 move16();
     611             : 
     612         144 :                 lp_select = lp_filt_exc_enc_fx( MODE1, TRANSITION, i_subfr, exc_fx, h1_fx,
     613         144 :                                                 xn_fx, y1_fx, xn2_fx, L_SUBFR, st_fx->L_frame, g_corr_fx, *clip_gain, gain_pit_fx, &lp_flag );
     614             : 
     615         144 :                 IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
     616             :                 {
     617         144 :                     push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     618             :                 }
     619             :             }
     620             : 
     621         155 :             **pt_pitch_fx = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     622         155 :             move16();
     623             : 
     624             :             /*---------------------------------------------------------------------*
     625             :              * fill the pitch buffer - needed for post-processing
     626             :              *---------------------------------------------------------------------*/
     627             : 
     628         155 :             test();
     629         155 :             test();
     630         155 :             test();
     631         155 :             test();
     632         155 :             test();
     633         155 :             IF( ( GE_16( *tc_subfr, 2 * L_SUBFR ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
     634             :             {
     635           1 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     636           1 :                 ( *pt_pitch_fx ) -= 3;
     637           1 :                 move16();
     638           1 :                 **pt_pitch_fx = tmp;
     639           1 :                 move16();
     640           1 :                 ( *pt_pitch_fx )++;
     641           1 :                 move16();
     642           1 :                 **pt_pitch_fx = tmp;
     643           1 :                 move16();
     644           1 :                 ( *pt_pitch_fx )++;
     645           1 :                 move16();
     646           1 :                 **pt_pitch_fx = tmp;
     647           1 :                 move16();
     648           1 :                 ( *pt_pitch_fx )++;
     649           1 :                 move16();
     650             :             }
     651         154 :             ELSE IF( ( EQ_16( *tc_subfr, L_SUBFR ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
     652             :             {
     653           5 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     654           5 :                 ( *pt_pitch_fx ) -= 2;
     655           5 :                 move16();
     656           5 :                 **pt_pitch_fx = tmp;
     657           5 :                 move16();
     658           5 :                 ( *pt_pitch_fx )++;
     659           5 :                 move16();
     660           5 :                 **pt_pitch_fx = tmp;
     661           5 :                 move16();
     662           5 :                 ( *pt_pitch_fx )++;
     663           5 :                 move16();
     664             :             }
     665         149 :             ELSE IF( ( EQ_16( *tc_subfr, TC_0_64 ) ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
     666             :             {
     667           9 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     668           9 :                 ( *pt_pitch_fx ) -= 1;
     669           9 :                 move16();
     670           9 :                 **pt_pitch_fx = tmp;
     671           9 :                 move16();
     672           9 :                 ( *pt_pitch_fx )++;
     673           9 :                 move16();
     674             :             }
     675         140 :             ELSE IF( ( EQ_16( *tc_subfr, TC_0_128 ) ) && ( EQ_16( i_subfr, 2 * L_SUBFR ) ) )
     676             :             {
     677           5 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     678           5 :                 ( *pt_pitch_fx ) -= 2;
     679           5 :                 move16();
     680           5 :                 **pt_pitch_fx = tmp;
     681           5 :                 move16();
     682           5 :                 ( *pt_pitch_fx )++;
     683           5 :                 move16();
     684           5 :                 **pt_pitch_fx = tmp;
     685           5 :                 move16();
     686           5 :                 ( *pt_pitch_fx )++;
     687           5 :                 move16();
     688             :             }
     689         135 :             ELSE IF( ( EQ_16( *tc_subfr, TC_0_192 ) ) && ( EQ_16( i_subfr, 3 * L_SUBFR ) ) )
     690             :             {
     691           3 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     692           3 :                 ( *pt_pitch_fx ) -= 3;
     693           3 :                 move16();
     694           3 :                 **pt_pitch_fx = tmp;
     695           3 :                 move16();
     696           3 :                 ( *pt_pitch_fx )++;
     697           3 :                 move16();
     698           3 :                 **pt_pitch_fx = tmp;
     699           3 :                 move16();
     700           3 :                 ( *pt_pitch_fx )++;
     701           3 :                 move16();
     702           3 :                 **pt_pitch_fx = tmp;
     703           3 :                 move16();
     704           3 :                 ( *pt_pitch_fx )++;
     705           3 :                 move16();
     706             :             }
     707             :         }
     708             :         ELSE /* L_frame == L_FRAME16k */
     709             :         {
     710         188 :             if ( GE_16( i_subfr, 2 * L_SUBFR ) )
     711             :             {
     712         149 :                 limit_flag = 1;
     713         149 :                 move16();
     714             :             }
     715             : 
     716         188 :             IF( LE_16( i_subfr, 2 * L_SUBFR ) )
     717             :             {
     718          88 :                 IF( LT_16( i_subfr, 2 * L_SUBFR ) )
     719             :                 {
     720          39 :                     mult_Top = 1;
     721          39 :                     move16();
     722          39 :                     if ( LT_16( T_op[0], PIT16k_MIN ) )
     723             :                     {
     724           4 :                         mult_Top = 2;
     725           4 :                         move16();
     726             :                     }
     727             : 
     728          39 :                     limit_T0_fx( L_FRAME16k, 8, 0, limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max ); /* TC0 second subfr. */
     729             :                 }
     730             :                 ELSE
     731             :                 {
     732          49 :                     limit_T0_fx( L_FRAME16k, 8, 0, limit_flag, T_op[1], 0, T0_min, T0_max ); /* TC0 third subfr., or TC64 third subfr. */
     733             :                 }
     734             :             }
     735             : 
     736             :             /*-----------------------------------------------------------------*
     737             :              * get number of bits for pitch encoding
     738             :              *-----------------------------------------------------------------*/
     739             : 
     740         188 :             nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
     741         188 :             move16();
     742             : 
     743             :             /*-----------------------------------------------------------------*
     744             :              * Find adaptive part of excitation, encode pitch period
     745             :              *-----------------------------------------------------------------*/
     746             : 
     747         188 :             IF( EQ_16( nBits, 10 ) )
     748             :             {
     749          49 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, st_fx->L_frame, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     750          49 :                 move16();
     751          49 :                 pit16k_Q_enc_fx( hBstr, nBits, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     752             :             }
     753         139 :             ELSE IF( EQ_16( nBits, 8 ) ) /* tc_subfr==0 && i_subfr==L_SUBFR */
     754             :             {
     755             :                 /*-----------------------------------------------------------------------------*
     756             :                  * The pitch range is encoded absolutely with 8 bits and is divided as follows:
     757             :                  *   PIT16k_MIN  to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
     758             :                  *   PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR     resolution 1/2 (frac = 0 or 2)
     759             :                  *-----------------------------------------------------------------------------*/
     760             : 
     761          39 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_TC0_2SUBFR, 2 * L_SUBFR, st_fx->L_frame, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     762          39 :                 move16();
     763             : 
     764          39 :                 IF( GT_16( *T0_max, 2 * L_SUBFR ) )
     765             :                 {
     766           7 :                     *T0 = 2 * L_SUBFR;
     767           7 :                     move16();
     768           7 :                     *T0_frac = 0;
     769           7 :                     move16();
     770             :                 }
     771             : 
     772          39 :                 IF( LT_16( *T0, PIT16k_FR2_TC0_2SUBFR ) )
     773             :                 {
     774             :                     /*index = (*T0)*4 + (*T0_frac) - (PIT16k_MIN*4);*/
     775          27 :                     index = add( shl( *T0, 2 ), sub( *T0_frac, PIT16k_MIN * 4 ) );
     776             :                 }
     777             :                 ELSE
     778             :                 {
     779             :                     /*index = (*T0)*2 + ((*T0_frac)>>1) - (PIT16k_FR2_TC0_2SUBFR*2) + ((PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4);*/
     780          12 :                     index = add( sub( add( shl( *T0, 1 ), shr( *T0_frac, 1 ) ), ( PIT16k_FR2_TC0_2SUBFR * 2 ) ), ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 );
     781             :                 }
     782          39 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     783             :             }
     784         100 :             ELSE IF( EQ_16( nBits, 6 ) )
     785             :             {
     786             :                 /* delta search */
     787         100 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, L_SUBFR, limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, st_fx->L_frame, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
     788         100 :                 move16();
     789             : 
     790         100 :                 index = delta_pit_enc_fx( 4, *T0, *T0_frac, *T0_min );
     791         100 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     792             :             }
     793         188 :             IF( EQ_16( nBits, 6 ) )
     794             :             {
     795         100 :                 limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     796             :             }
     797             : 
     798             :             /*-----------------------------------------------------------------*
     799             :              * - gain clipping test to avoid unstable synthesis
     800             :              * - LP filtering of the adaptive excitation
     801             :              * - codebook target computation
     802             :              *-----------------------------------------------------------------*/
     803             : 
     804         188 :             test();
     805         188 :             IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *T0, 2 * L_SUBFR ) ) )
     806             :             {
     807           7 :                 *gain_pit_fx = 0;
     808           7 :                 move16();
     809           7 :                 *clip_gain = 0;
     810           7 :                 move16();
     811           7 :                 g_corr_fx[0] = 0;
     812           7 :                 move16();
     813           7 :                 g_corr_fx[1] = 0;
     814           7 :                 move16();
     815           7 :                 *Jopt_flag = 0;
     816           7 :                 move16();
     817             : 
     818           7 :                 set16_fx( &exc_fx[i_subfr], 0, L_SUBFR + 1 );   /* set excitation for current subrame to 0 */
     819           7 :                 push_indice( hBstr, IND_LP_FILT_SELECT, 0, 1 ); /* this bit is actually not needed */
     820             : 
     821           7 :                 Copy( xn_fx, xn2_fx, L_SUBFR ); /* target vector for codebook search */
     822           7 :                 set16_fx( y1_fx, 0, L_SUBFR );  /* set filtered adaptive excitation to 0 */
     823           7 :                 set16_fx( &bwe_exc_fx[i_subfr * 2], 0, L_SUBFR * 2 );
     824             :             }
     825             :             ELSE
     826             :             {
     827             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     828         181 :                 pred_lt4( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     829         181 :                 offset = L_deposit_l( 0 );
     830             : 
     831         181 :                 tmp = extract_l( L_mult( *T0_frac, 32 ) ); /*Q8, 0.25 in Q7*/
     832         181 :                 tmp = add( 512, tmp );                     /*Q8; 2 in Q8*/
     833         181 :                 tmp = mult_r( tmp, 256 );                  /*Q16->Q0; 2 in Q7*/
     834             : 
     835         181 :                 tmp1 = sub( *T0, 2 );  /*Q0*/
     836         181 :                 tmp1 = shl( tmp1, 1 ); /*Q0 */
     837             : 
     838         181 :                 offset = add( tmp, tmp1 ); /*Q0*/
     839       23349 :                 FOR( i = 0; i < L_SUBFR * 2; i++ )
     840             :                 {
     841             :                     /* bwe_exc_fx[i + i_subfr * 2] = bwe_exc_fx[i + i_subfr * 2 - *T0 * 2  - (int) ((float) *T0_frac * 0.5f + 4 + 0.5f) + 4];move16();*/
     842       23168 :                     bwe_exc_fx[i + i_subfr * 2] = bwe_exc_fx[i + i_subfr * 2 - offset + 4];
     843       23168 :                     move16();
     844             :                 }
     845             : 
     846         181 :                 *clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, TRANSITION, xn_fx, gp_cl_fx, Q_new );
     847         181 :                 move16();
     848             : 
     849         181 :                 lp_select = lp_filt_exc_enc_fx( MODE1, TRANSITION, i_subfr, exc_fx, h1_fx,
     850         181 :                                                 xn_fx, y1_fx, xn2_fx, L_SUBFR, st_fx->L_frame, g_corr_fx, *clip_gain, gain_pit_fx, &lp_flag );
     851             : 
     852         181 :                 IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
     853             :                 {
     854         181 :                     push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     855             :                 }
     856             : 
     857         181 :                 *Jopt_flag = 1;
     858         181 :                 move16();
     859             :             }
     860             : 
     861             :             /***pt_pitch = (float)(*T0) + (float)(*T0_frac)/4.0f;*/ /* save subframe pitch value  */
     862             :             /***pt_pitch_fx = shl(add(*T0,shr(*T0_frac,2)),4); move16();*/
     863         188 :             tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
     864         188 :             **pt_pitch_fx = tmp;
     865         188 :             move16();
     866             : 
     867             :             /*---------------------------------------------------------------------*
     868             :              * fill the pitch buffer - needed for post-processing
     869             :              *---------------------------------------------------------------------*/
     870             : 
     871         188 :             test();
     872         188 :             test();
     873         188 :             IF( ( EQ_16( sub( i_subfr, *tc_subfr ), L_SUBFR ) ) || ( *tc_subfr == 0 && EQ_16( i_subfr, 2 * L_SUBFR ) ) )
     874             :             {
     875          89 :                 index = shr( i_subfr, 6 );
     876          89 :                 ( *pt_pitch_fx ) -= index;
     877          89 :                 move16();
     878             : 
     879         229 :                 FOR( i = 0; i < index; i++ )
     880             :                 {
     881         140 :                     **pt_pitch_fx = tmp;
     882         140 :                     move16();
     883         140 :                     ( *pt_pitch_fx )++;
     884         140 :                     move16();
     885             :                 }
     886             :             }
     887             :         }
     888             :     }
     889             : 
     890         546 :     return;
     891             : }
     892             : 
     893       63958 : void transition_enc_ivas_fx(
     894             :     Encoder_State *st_fx, /* i/o: encoder state structure */
     895             :     const Word16 i_subfr, /* i  : subframe index                              */
     896             :     Word16 *tc_subfr,     /* i/o: TC subframe index                           */
     897             :     Word16 *Jopt_flag,    /* i  : joint optimization flag                     */
     898             :     Word16 *position,     /* i/o: maximum of residual signal index            */
     899             :     Word16 *T0,           /* i/o: close loop integer pitch                    Q0*/
     900             :     Word16 *T0_frac,      /* i/o: close loop fractional part of the pitch     Q0*/
     901             :     Word16 *T0_min,       /* i/o: lower limit for close-loop search           Q0*/
     902             :     Word16 *T0_max,       /* i/o: higher limit for close-loop search          Q0*/
     903             :     Word16 *exc_fx,       /* i/o: pointer to excitation signal frame          Q_new*/
     904             :     Word16 *y1_fx,        /* o  : zero-memory filtered adaptive excitation    Q_new-1+shift*/
     905             :     const Word16 *h1_fx,  /* i  : weighted filter input response              Q(14+shift)*/
     906             :     const Word16 *xn_fx,  /* i  : target vector                               Q_new-1+shift*/
     907             :     Word16 *xn2_fx,       /* o  : target vector for innovation search         Q_new-1+shift*/
     908             :     Word16 *gp_cl_fx,     /* i/o: memory of gain of pitch clipping algorithm  */
     909             :     Word16 *gain_pit_fx,  /* o  : adaptive excitation gain                    Q14*/
     910             :     Word16 *g_corr_fx,    /* o  : ACELP correlation values                    */
     911             :     Word16 *clip_gain,    /* i/o: adaptive gain clipping flag                 */
     912             :     Word16 **pt_pitch_fx, /* o  : floating pitch values                       */
     913             :     Word16 *bwe_exc_fx,   /* o  : excitation for SWB TBE                      Q_new*/
     914             :     Word16 *unbits_ACELP, /* i/o: unused bits                                 */
     915             :     Word16 Q_new,         /* i  : Current scaling */
     916             :     Word16 shift          /* i  : downscaling needs for 12 bits convolutions */
     917             : )
     918             : {
     919             :     Word16 pit_flag, pit_start, pit_limit, index, nBits;
     920             :     Word16 tmp, tmp1, i;
     921             :     Word32 offset;
     922             :     Word16 shift_wsp;
     923             :     Word16 limit_flag, mult_Top, lp_select, lp_flag;
     924             :     Word16 T_op[2]; /* values for two half-frames */
     925       63958 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
     926             : 
     927             :     /* set limit_flag to 0 for restrained limits, and 1 for extended limits */
     928       63958 :     limit_flag = 0;
     929       63958 :     move16();
     930       63958 :     pit_start = PIT_MIN;
     931       63958 :     move16();
     932             : 
     933             :     /*----------------------------------------------------------------*
     934             :      * convert pitch values to 16kHz domain
     935             :      *----------------------------------------------------------------*/
     936             : 
     937       63958 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) /*|| (tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0)*/ )
     938             :     {
     939       29948 :         Copy( st_fx->pitch, T_op, 2 );
     940             :     }
     941             :     ELSE /* L_frame == L_FRAME16k  */
     942             :     {
     943             :         /*T_op[0] = (int16_t)(pitch[0] * 1.25f + 0.5f);
     944             :         T_op[1] = (int16_t)(pitch[1] * 1.25f + 0.5f);*/
     945       34010 :         T_op[0] = add( st_fx->pitch[0], mult_r( st_fx->pitch[0], 8192 /*0.25f Q15*/ ) );
     946       34010 :         move16();
     947       34010 :         T_op[1] = add( st_fx->pitch[1], mult_r( st_fx->pitch[1], 8192 /*0.25f Q15*/ ) );
     948       34010 :         move16();
     949             :     }
     950       63958 :     shift_wsp = add( Q_new, shift );
     951             : 
     952       63958 :     lp_flag = st_fx->acelp_cfg.ltf_mode;
     953       63958 :     move16();
     954             : 
     955             :     /*-----------------------------------------------------------------*
     956             :      * TC: subrame determination for glottal shape search
     957             :      * -------------------------------------------------------
     958             :      * tc_subfr == 0         - TC in 1st subframe
     959             :      * tc_subfr == TC_0_0    - TC in 1st subframe + information about T0
     960             :      * tc_subfr == L_SUBFR   - TC in 2nd subframe
     961             :      * tc_subfr == 2*L_SUBFR - TC in 3rd subframe
     962             :      * tc_subfr == 3*L_SUBFR - TC in 4th subframe
     963             :      *-----------------------------------------------------------------*/
     964             : 
     965       63958 :     IF( i_subfr == 0 )
     966             :     {
     967       14289 :         mult_Top = 1;
     968       14289 :         move16();
     969       14289 :         IF( limit_flag == 0 )
     970             :         {
     971       14289 :             test();
     972       14289 :             IF( EQ_16( st_fx->L_frame, L_FRAME ) && LT_16( T_op[1], PIT_MIN ) )
     973             :             {
     974         895 :                 mult_Top = 2;
     975         895 :                 move16();
     976             :             }
     977       14289 :             test();
     978       14289 :             if ( EQ_16( st_fx->L_frame, L_FRAME16k ) && LT_16( T_op[1], PIT16k_MIN ) )
     979             :             {
     980         732 :                 mult_Top = 2;
     981         732 :                 move16();
     982             :             }
     983             :         }
     984             : 
     985       14289 :         limit_T0_fx( st_fx->L_frame, 8, 0, limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     986             :     }
     987             : 
     988             :     /*-----------------------------------------------------------------*
     989             :      * zero adaptive excitation signal construction
     990             :      *-----------------------------------------------------------------*/
     991             : 
     992       63958 :     IF( GT_16( *tc_subfr, i_subfr ) )
     993             :     {
     994       19189 :         *gain_pit_fx = 0;
     995       19189 :         move16();
     996       19189 :         *clip_gain = 0;
     997       19189 :         move16();
     998       19189 :         g_corr_fx[0] = MAX16B /* 1.0f in Q15 */;
     999       19189 :         move16();
    1000       19189 :         g_corr_fx[1] = 0;
    1001       19189 :         move16();
    1002       19189 :         g_corr_fx[2] = MAX16B /* 1.0f in Q15 */;
    1003       19189 :         move16();
    1004       19189 :         g_corr_fx[3] = 0;
    1005       19189 :         move16();
    1006             : 
    1007       19189 :         set16_fx( &exc_fx[i_subfr], 0, L_SUBFR ); /* set excitation for current subrame to 0 */
    1008             : 
    1009       19189 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1010             :         {
    1011        7718 :             set16_fx( &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) ); /* set past excitation buffer to 0 */
    1012             :         }
    1013             :         ELSE
    1014             :         {
    1015       11471 :             set16_fx( &bwe_exc_fx[i_subfr * 2], 0, L_SUBFR * 2 ); /* set past excitation buffer to 0 */
    1016             :         }
    1017             : 
    1018       19189 :         set16_fx( y1_fx, 0, L_SUBFR );  /* set filtered adaptive excitation to 0 */
    1019       19189 :         Copy( xn_fx, xn2_fx, L_SUBFR ); /* target vector for codebook search */
    1020       19189 :         *T0 = L_SUBFR;
    1021       19189 :         move16();
    1022       19189 :         *T0_frac = 0;
    1023       19189 :         move16();
    1024             : 
    1025       19189 :         **pt_pitch_fx = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1026       19189 :         move16(); /* save subframe pitch values Q6 */
    1027             :     }
    1028             : 
    1029             :     /*-----------------------------------------------------------------*
    1030             :      * glottal codebook contribution construction
    1031             :      *-----------------------------------------------------------------*/
    1032             : 
    1033       44769 :     ELSE IF( EQ_16( *tc_subfr, i_subfr ) )
    1034             :     {
    1035       14289 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1036             :         {
    1037        7487 :             set16_fx( bwe_exc_fx - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
    1038             :         }
    1039             :         ELSE
    1040             :         {
    1041        6802 :             set16_fx( bwe_exc_fx - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
    1042             :         }
    1043             : 
    1044       14289 :         tc_enc_ivas_fx( st_fx, i_subfr, tc_subfr, position, h1_fx, xn_fx, exc_fx,
    1045             :                         y1_fx, T0_min, T0_max, T0, T0_frac, gain_pit_fx, g_corr_fx, bwe_exc_fx, Q_new );
    1046             : 
    1047       14289 :         IF( EQ_16( *tc_subfr, TC_0_0 ) )
    1048             :         {
    1049             :             /* this is called only to compute unused bits */
    1050        1676 :             config_acelp1_fx( ENC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl, st_fx->extl_brate, L_FRAME, -1, &( st_fx->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, TC_0_0, 3, NULL, unbits_ACELP, st_fx->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st_fx->idchan, 0 /*tdm_Pitch_reuse_flag*/, st_fx->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
    1051             :         }
    1052             : 
    1053       14289 :         *clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, TRANSITION, xn_fx, gp_cl_fx, sub( shift_wsp, 1 ) );
    1054       14289 :         move16();
    1055       14289 :         updt_tar_fx( xn_fx, xn2_fx, y1_fx, *gain_pit_fx, L_SUBFR );
    1056             : 
    1057       14289 :         **pt_pitch_fx = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1058       14289 :         move16();
    1059       14289 :         *Jopt_flag = 1;
    1060       14289 :         move16();
    1061             :     }
    1062             : 
    1063             :     /*--------------------------------------------------------------*
    1064             :      * other subframes -> GENERIC encoding type,
    1065             :      * standard adaptive excitation contribution
    1066             :      * - exemption only in case when first glottal impulse is
    1067             :      * in the 1st subframe and the second one in 2nd subframe
    1068             :      * and later
    1069             :      *--------------------------------------------------------------*/
    1070             : 
    1071       30480 :     ELSE IF( LT_16( *tc_subfr, i_subfr ) )
    1072             :     {
    1073       30480 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1074             :         {
    1075       14743 :             *Jopt_flag = 1;
    1076       14743 :             move16();
    1077             :             /* pit_flag for T0 bits number coding determination */
    1078       14743 :             test();
    1079       14743 :             IF( ( EQ_16( sub( i_subfr, *tc_subfr ), L_SUBFR ) ) || ( EQ_16( sub( i_subfr, *tc_subfr ), L_SUBFR - TC_0_0 ) ) )
    1080             :             {
    1081        5673 :                 pit_flag = 0;
    1082        5673 :                 move16();
    1083             :             }
    1084             :             ELSE
    1085             :             {
    1086        9070 :                 pit_flag = L_SUBFR;
    1087        9070 :                 move16();
    1088             :             }
    1089             : 
    1090       14743 :             IF( EQ_16( *tc_subfr, TC_0_0 ) )
    1091             :             {
    1092        5028 :                 IF( EQ_16( i_subfr, L_SUBFR ) )
    1093             :                 {
    1094        1676 :                     limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
    1095             :                 }
    1096        5028 :                 pit_flag = 1;
    1097        5028 :                 move16();
    1098             :             }
    1099             : 
    1100             :             /*----------------------------------------------------------*
    1101             :              * if tc_subfr==0, change tc_subfr corresponding to the
    1102             :              * second glot. impulse position
    1103             :              *----------------------------------------------------------*/
    1104             : 
    1105       14743 :             test();
    1106       14743 :             IF( ( *tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
    1107             :             {
    1108        2284 :                 IF( GT_16( PIT_MIN, ( *position ) ) )
    1109             :                 {
    1110        1467 :                     pit_start = sub( L_SUBFR, ( *position ) );
    1111             :                 }
    1112             :                 ELSE
    1113             :                 {
    1114         817 :                     pit_start = PIT_MIN;
    1115         817 :                     move16();
    1116             :                 }
    1117        2284 :                 pit_start = s_max( pit_start, PIT_MIN );
    1118             : 
    1119        2284 :                 pit_limit = add( shl( pit_start, 1 ), *position );
    1120             : 
    1121             :                 /* Find the closed loop pitch period */
    1122        2284 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, pit_limit, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1123        2284 :                 move16();
    1124             : 
    1125        2284 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
    1126             : 
    1127      367724 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
    1128             :                 {
    1129      365440 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
    1130      365440 :                     move16();
    1131             :                 }
    1132             : 
    1133        2284 :                 test();
    1134        2284 :                 IF( GT_16( ( *T0 ), sub( 2 * L_SUBFR, ( *position ) ) ) )
    1135             :                 {
    1136         901 :                     IF( GE_16( add( ( *T0 ), ( *position ) ), 3 * L_SUBFR ) )
    1137             :                     {
    1138             :                         /* second glottal impulse is in the 4th subframe */
    1139         213 :                         *tc_subfr = TC_0_192;
    1140         213 :                         move16();
    1141             :                     }
    1142             :                     ELSE
    1143             :                     {
    1144             :                         /* second glottal impulse is in the 3rd subframe */
    1145         688 :                         *tc_subfr = TC_0_128;
    1146         688 :                         move16();
    1147             :                     }
    1148             :                 }
    1149        1383 :                 ELSE IF( ( *tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) )
    1150             :                 {
    1151             :                     /* second glottal impulse is in the 2nd subframe */
    1152        1383 :                     *tc_subfr = TC_0_64;
    1153        1383 :                     move16();
    1154             :                 }
    1155             :             }
    1156             : 
    1157       14743 :             IF( LE_16( sub( i_subfr, *tc_subfr ), L_SUBFR ) )
    1158             :             {
    1159        5673 :                 config_acelp1_fx( ENC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl, st_fx->extl_brate, st_fx->L_frame, -1, &( st_fx->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, *tc_subfr, 2, NULL, unbits_ACELP, st_fx->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st_fx->idchan, 0 /*tdm_Pitch_reuse_flag*/, st_fx->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
    1160             :             }
    1161             : 
    1162             :             /*-----------------------------------------------------------------*
    1163             :              * get number of bits for pitch encoding
    1164             :              *-----------------------------------------------------------------*/
    1165             : 
    1166       14743 :             nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
    1167       14743 :             move16();
    1168             : 
    1169             :             /*-----------------------------------------------------------------*
    1170             :              * Find adaptive part of excitation, encode pitch period
    1171             :              *-----------------------------------------------------------------*/
    1172             : 
    1173       14743 :             test();
    1174       14743 :             test();
    1175       14743 :             test();
    1176       14743 :             test();
    1177       14743 :             test();
    1178       14743 :             test();
    1179             :             /* first glottal impulse is in the 1st subrame */
    1180       14743 :             IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( GE_16( *tc_subfr, TC_0_128 ) ) )
    1181             :             {
    1182             :                 /*--------------------------------------------------------*
    1183             :                  * second glottal impulse is in the 3rd or 4th subframe
    1184             :                  * - build exc[] in 2nd subframe
    1185             :                  *--------------------------------------------------------*/
    1186             : 
    1187         901 :                 *T0 = 2 * L_SUBFR;
    1188         901 :                 move16();
    1189         901 :                 *T0_frac = 0;
    1190         901 :                 move16();
    1191         901 :                 *Jopt_flag = 0;
    1192         901 :                 move16();
    1193         901 :                 set16_fx( &exc_fx[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
    1194         901 :                 set16_fx( &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
    1195             :             }
    1196       13842 :             ELSE IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_64 ) ) )
    1197             :             {
    1198             :                 /*--------------------------------------------------------*
    1199             :                  * second glottal impulse is in the 2nd subframe,
    1200             :                  * - build exc[] in 2nd subframe
    1201             :                  *--------------------------------------------------------*/
    1202             : 
    1203        1383 :                 IF( LT_16( add( *T0, *position ), L_SUBFR ) )
    1204             :                 {
    1205             :                     /* impulse must be in the 2nd subframe (not in 1st) */
    1206          12 :                     *T0 = sub( L_SUBFR, ( *position ) );
    1207          12 :                     move16();
    1208          12 :                     *T0_frac = 0;
    1209          12 :                     move16();
    1210             :                 }
    1211        1383 :                 IF( GE_16( add( *T0, *position ), 2 * L_SUBFR ) )
    1212             :                 {
    1213             :                     /* impulse must be in the 2nd subframe (not in 3rd) */
    1214          27 :                     *T0 = sub( 2 * L_SUBFR - 1, ( *position ) );
    1215          27 :                     move16();
    1216          27 :                     *T0_frac = 2;
    1217          27 :                     move16();
    1218             :                 }
    1219             : 
    1220        1383 :                 limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
    1221             : 
    1222             :                 /* 7bit ENCODER */
    1223             :                 /*  index = (*T0-pit_start)*2 + *T0_frac/2;*/
    1224        1383 :                 index = add( shl( sub( *T0, pit_start ), 1 ), shr( *T0_frac, 1 ) );
    1225        1383 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1226             : 
    1227             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
    1228        1383 :                 pred_lt4_ivas_fx( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
    1229             : 
    1230             : 
    1231        1383 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
    1232             : 
    1233      222663 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
    1234             :                 {
    1235      221280 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
    1236      221280 :                     move16();
    1237             :                 }
    1238             :             }
    1239       12459 :             ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_128 ) ) )
    1240             :             {
    1241             :                 /*--------------------------------------------------------*
    1242             :                  * second glottal impulse is in the 3rd subframe
    1243             :                  * - build exc[] in 3rd subframe
    1244             :                  *--------------------------------------------------------*/
    1245             : 
    1246         688 :                 pit_start = sub( 2 * L_SUBFR, ( *position ) );
    1247         688 :                 pit_flag = 0;
    1248         688 :                 move16();
    1249             : 
    1250         688 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, 3 * L_SUBFR, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1251         688 :                 move16();
    1252             : 
    1253         688 :                 IF( LT_16( add( ( *T0 ), ( *position ) ), 2 * L_SUBFR ) )
    1254             :                 {
    1255             :                     /* impulse must be in the 3rd subframe (not in 2nd) */
    1256          13 :                     *T0 = sub( 2 * L_SUBFR, ( *position ) );
    1257          13 :                     move16();
    1258          13 :                     *T0_frac = 0;
    1259          13 :                     move16();
    1260             :                 }
    1261             : 
    1262         688 :                 IF( GE_16( add( ( *T0 ), ( *position ) ), 3 * L_SUBFR ) )
    1263             :                 {
    1264             :                     /* impulse must be in the 3rd subframe (not in 4th) */
    1265           0 :                     *T0 = sub( 3 * L_SUBFR - 1, ( *position ) );
    1266           0 :                     move16();
    1267           0 :                     *T0_frac = 2;
    1268           0 :                     move16();
    1269             :                 }
    1270             : 
    1271         688 :                 limit_T0_fx( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
    1272             : 
    1273         688 :                 index = add( shl( sub( *T0, pit_start ), 1 ), shr( *T0_frac, 1 ) );
    1274         688 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1275             : 
    1276             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
    1277         688 :                 pred_lt4_ivas_fx( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
    1278             : 
    1279         688 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
    1280             : 
    1281             : 
    1282      110768 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
    1283             :                 {
    1284      110080 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
    1285      110080 :                     move16();
    1286             :                 }
    1287             :             }
    1288       11771 :             ELSE IF( ( EQ_16( i_subfr, 2 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_192 ) ) )
    1289             :             {
    1290             :                 /*--------------------------------------------------------*
    1291             :                  * second glottal impulse is in the 4th subframe
    1292             :                  * - build exc[] in 3rd subframe
    1293             :                  *--------------------------------------------------------*/
    1294             : 
    1295         213 :                 *T0 = 4 * L_SUBFR;
    1296         213 :                 move16();
    1297         213 :                 *T0_frac = 0;
    1298         213 :                 move16();
    1299         213 :                 *Jopt_flag = 0;
    1300         213 :                 move16();
    1301         213 :                 set16_fx( &exc_fx[i_subfr], 0, (Word16) ( L_SUBFR + 1 ) );
    1302         213 :                 set16_fx( &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], 0, (Word16) ( L_SUBFR * HIBND_ACB_L_FAC ) );
    1303             :             }
    1304       11558 :             ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_192 ) ) )
    1305             :             {
    1306             :                 /*--------------------------------------------------------*
    1307             :                  * second glottal impulse is in the 4th subframe
    1308             :                  * - build exc[] in 4th subframe
    1309             :                  *--------------------------------------------------------*/
    1310             : 
    1311             :                 /* always T0_frac = 0 */
    1312         213 :                 pit_flag = 0;
    1313         213 :                 move16();
    1314             : 
    1315         213 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1316         213 :                 move16();
    1317             : 
    1318         213 :                 IF( LT_16( add( *T0, *position ), 3 * L_SUBFR ) )
    1319             :                 {
    1320             :                     /* impulse must be in the 4th subframe (not in 3rd) */
    1321          14 :                     *T0 = sub( 3 * L_SUBFR, ( *position ) );
    1322          14 :                     move16();
    1323          14 :                     *T0_frac = 0;
    1324          14 :                     move16();
    1325             :                 }
    1326             : 
    1327         213 :                 pit_start = sub( 3 * L_SUBFR, ( *position ) );
    1328         213 :                 pit_limit = sub( 2 * L_FRAME - PIT_MAX - 2, shl( *position, 1 ) );
    1329             : 
    1330         213 :                 IF( LT_16( ( *T0 ), pit_limit ) )
    1331             :                 {
    1332         169 :                     index = add( shl( sub( *T0, pit_start ), 1 ), shr( *T0_frac, 1 ) );
    1333             :                 }
    1334             :                 ELSE
    1335             :                 {
    1336          44 :                     index = add( sub( *T0, pit_limit ), shl( sub( pit_limit, pit_start ), 1 ) );
    1337          44 :                     *T0_frac = 0;
    1338          44 :                     move16();
    1339             :                 }
    1340         213 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1341             : 
    1342             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
    1343         213 :                 pred_lt4_ivas_fx( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
    1344             : 
    1345         213 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
    1346             : 
    1347       34293 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
    1348             :                 {
    1349       34080 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
    1350       34080 :                     move16();
    1351             :                 }
    1352             :             }
    1353       11345 :             ELSE IF( ( EQ_16( i_subfr, 3 * L_SUBFR ) ) && ( EQ_16( *tc_subfr, TC_0_128 ) ) )
    1354             :             {
    1355             :                 /*--------------------------------------------------------*
    1356             :                  * second glottal impulse in the 3rd subframe
    1357             :                  * build exc[] in 4th subframe
    1358             :                  *--------------------------------------------------------*/
    1359             : 
    1360         688 :                 pit_flag = L_SUBFR;
    1361         688 :                 move16();
    1362             : 
    1363         688 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1364         688 :                 move16();
    1365         688 :                 index = delta_pit_enc_fx( 2, *T0, *T0_frac, *T0_min );
    1366         688 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1367             : 
    1368             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
    1369         688 :                 pred_lt4_ivas_fx( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
    1370             : 
    1371         688 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
    1372             : 
    1373      110768 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
    1374             :                 {
    1375      110080 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
    1376      110080 :                     move16();
    1377             :                 }
    1378             :             }
    1379             : 
    1380             :             /*------------------------------------------------------------*
    1381             :              * first glottal impulse is NOT in the 1st subframe,
    1382             :              * or two impulses are in the 1st subframe
    1383             :              *------------------------------------------------------------*/
    1384             : 
    1385             :             ELSE
    1386             :             {
    1387       10657 :                 test();
    1388       10657 :                 IF( EQ_16( nBits, 8 ) || EQ_16( nBits, 5 ) )
    1389             :                 {
    1390        1001 :                     test();
    1391        1001 :                     IF( !( ( *tc_subfr == 0 ) && ( EQ_16( i_subfr, L_SUBFR ) ) ) )
    1392             :                     {
    1393        1001 :                         *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1394        1001 :                         move16();
    1395             :                     }
    1396             :                 }
    1397             :                 ELSE
    1398             :                 {
    1399        9656 :                     *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1400        9656 :                     move16();
    1401             :                 }
    1402       10657 :                 pit_Q_enc_fx( hBstr, 0, nBits, 8, pit_flag, limit_flag, *T0, *T0_frac, T0_min, T0_max );
    1403             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
    1404       10657 :                 pred_lt4_ivas_fx( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
    1405             : 
    1406       10657 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac, st_fx->L_frame );
    1407             : 
    1408     1715777 :                 FOR( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
    1409             :                 {
    1410     1705120 :                     bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc_fx[i + i_subfr * HIBND_ACB_L_FAC - offset];
    1411     1705120 :                     move16();
    1412             :                 }
    1413             :             }
    1414             : 
    1415             :             /*-----------------------------------------------------------------*
    1416             :              * - gain clipping test to avoid unstable synthesis
    1417             :              * - LP filtering of the adaptive excitation (if non-zero)
    1418             :              * - codebook target computation
    1419             :              *-----------------------------------------------------------------*/
    1420             : 
    1421       14743 :             IF( *Jopt_flag == 0 )
    1422             :             {
    1423             :                 /* adaptive/TC excitation is zero */
    1424        1114 :                 Copy( xn_fx, xn2_fx, L_SUBFR );
    1425        1114 :                 g_corr_fx[0] = 0;
    1426        1114 :                 move16();
    1427        1114 :                 g_corr_fx[1] = 0;
    1428        1114 :                 move16();
    1429        1114 :                 g_corr_fx[2] = 0;
    1430        1114 :                 move16();
    1431        1114 :                 g_corr_fx[3] = 0;
    1432        1114 :                 move16();
    1433        1114 :                 *clip_gain = 0;
    1434        1114 :                 move16();
    1435             :             }
    1436             :             ELSE
    1437             :             {
    1438       13629 :                 *clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, TRANSITION, xn_fx, gp_cl_fx, ( Q_new + shift - 1 ) );
    1439       13629 :                 move16();
    1440             : 
    1441       13629 :                 lp_select = lp_filt_exc_enc_fx( MODE1, TRANSITION, i_subfr, exc_fx, h1_fx,
    1442       13629 :                                                 xn_fx, y1_fx, xn2_fx, L_SUBFR, st_fx->L_frame, g_corr_fx, *clip_gain, gain_pit_fx, &lp_flag );
    1443       13629 :                 IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
    1444             :                 {
    1445       10640 :                     push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
    1446             :                 }
    1447             :             }
    1448             : 
    1449       14743 :             **pt_pitch_fx = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1450       14743 :             move16();
    1451             : 
    1452             :             /*---------------------------------------------------------------------*
    1453             :              * fill the pitch buffer - needed for post-processing
    1454             :              *---------------------------------------------------------------------*/
    1455             : 
    1456       14743 :             test();
    1457       14743 :             test();
    1458       14743 :             test();
    1459       14743 :             test();
    1460       14743 :             test();
    1461       14743 :             IF( ( *tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
    1462             :             {
    1463         563 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1464         563 :                 ( *pt_pitch_fx ) -= 3;
    1465         563 :                 move16();
    1466         563 :                 **pt_pitch_fx = tmp;
    1467         563 :                 move16();
    1468         563 :                 ( *pt_pitch_fx )++;
    1469         563 :                 move16();
    1470         563 :                 **pt_pitch_fx = tmp;
    1471         563 :                 move16();
    1472         563 :                 ( *pt_pitch_fx )++;
    1473         563 :                 move16();
    1474         563 :                 **pt_pitch_fx = tmp;
    1475         563 :                 move16();
    1476         563 :                 ( *pt_pitch_fx )++;
    1477         563 :                 move16();
    1478             :             }
    1479       14180 :             ELSE IF( ( *tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
    1480             :             {
    1481        1150 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1482        1150 :                 ( *pt_pitch_fx ) -= 2;
    1483        1150 :                 move16();
    1484        1150 :                 **pt_pitch_fx = tmp;
    1485        1150 :                 move16();
    1486        1150 :                 ( *pt_pitch_fx )++;
    1487        1150 :                 move16();
    1488        1150 :                 **pt_pitch_fx = tmp;
    1489        1150 :                 move16();
    1490        1150 :                 ( *pt_pitch_fx )++;
    1491        1150 :                 move16();
    1492             :             }
    1493       13030 :             ELSE IF( ( *tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
    1494             :             {
    1495        1383 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1496        1383 :                 ( *pt_pitch_fx ) -= 1;
    1497        1383 :                 move16();
    1498        1383 :                 **pt_pitch_fx = tmp;
    1499        1383 :                 move16();
    1500        1383 :                 ( *pt_pitch_fx )++;
    1501        1383 :                 move16();
    1502             :             }
    1503       11647 :             ELSE IF( ( *tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
    1504             :             {
    1505         688 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1506         688 :                 ( *pt_pitch_fx ) -= 2;
    1507         688 :                 move16();
    1508         688 :                 **pt_pitch_fx = tmp;
    1509         688 :                 move16();
    1510         688 :                 ( *pt_pitch_fx )++;
    1511         688 :                 move16();
    1512         688 :                 **pt_pitch_fx = tmp;
    1513         688 :                 move16();
    1514         688 :                 ( *pt_pitch_fx )++;
    1515         688 :                 move16();
    1516             :             }
    1517       10959 :             ELSE IF( ( *tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
    1518             :             {
    1519         213 :                 tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1520         213 :                 ( *pt_pitch_fx ) -= 3;
    1521         213 :                 move16();
    1522         213 :                 **pt_pitch_fx = tmp;
    1523         213 :                 move16();
    1524         213 :                 ( *pt_pitch_fx )++;
    1525         213 :                 move16();
    1526         213 :                 **pt_pitch_fx = tmp;
    1527         213 :                 move16();
    1528         213 :                 ( *pt_pitch_fx )++;
    1529         213 :                 move16();
    1530         213 :                 **pt_pitch_fx = tmp;
    1531         213 :                 move16();
    1532         213 :                 ( *pt_pitch_fx )++;
    1533         213 :                 move16();
    1534             :             }
    1535             :         }
    1536             :         ELSE /* L_frame == L_FRAME16k */
    1537             :         {
    1538       15737 :             if ( GE_16( i_subfr, 2 * L_SUBFR ) )
    1539             :             {
    1540       12949 :                 limit_flag = 1;
    1541       12949 :                 move16();
    1542             :             }
    1543       15737 :             IF( LE_16( i_subfr, 2 * L_SUBFR ) )
    1544             :             {
    1545        6571 :                 IF( LT_16( i_subfr, 2 * L_SUBFR ) )
    1546             :                 {
    1547        2788 :                     mult_Top = 1;
    1548        2788 :                     move16();
    1549        2788 :                     if ( LT_16( T_op[0], PIT16k_MIN ) )
    1550             :                     {
    1551         165 :                         mult_Top = 2;
    1552         165 :                         move16();
    1553             :                     }
    1554             : 
    1555        2788 :                     limit_T0_fx( L_FRAME16k, 8, 0, limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max ); /* TC0 second subfr. */
    1556             :                 }
    1557             :                 ELSE
    1558             :                 {
    1559        3783 :                     limit_T0_fx( L_FRAME16k, 8, 0, limit_flag, T_op[1], 0, T0_min, T0_max ); /* TC0 third subfr., or TC64 third subfr. */
    1560             :                 }
    1561             :             }
    1562             : 
    1563             :             /*-----------------------------------------------------------------*
    1564             :              * get number of bits for pitch encoding
    1565             :              *-----------------------------------------------------------------*/
    1566             : 
    1567       15737 :             nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
    1568       15737 :             move16();
    1569             : 
    1570             :             /*-----------------------------------------------------------------*
    1571             :              * Find adaptive part of excitation, encode pitch period
    1572             :              *-----------------------------------------------------------------*/
    1573             : 
    1574       15737 :             IF( EQ_16( nBits, 10 ) )
    1575             :             {
    1576        3783 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, st_fx->L_frame, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1577        3783 :                 move16();
    1578        3783 :                 pit16k_Q_enc_fx( hBstr, nBits, limit_flag, *T0, *T0_frac, T0_min, T0_max );
    1579             :             }
    1580       11954 :             ELSE IF( EQ_16( nBits, 8 ) ) /* tc_subfr==0 && i_subfr==L_SUBFR */
    1581             :             {
    1582             :                 /*-----------------------------------------------------------------------------*
    1583             :                  * The pitch range is encoded absolutely with 8 bits and is divided as follows:
    1584             :                  *   PIT16k_MIN  to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
    1585             :                  *   PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR     resolution 1/2 (frac = 0 or 2)
    1586             :                  *-----------------------------------------------------------------------------*/
    1587             : 
    1588        2788 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_TC0_2SUBFR, 2 * L_SUBFR, st_fx->L_frame, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1589        2788 :                 move16();
    1590             : 
    1591        2788 :                 IF( GT_16( *T0_max, 2 * L_SUBFR ) )
    1592             :                 {
    1593         483 :                     *T0 = 2 * L_SUBFR;
    1594         483 :                     move16();
    1595         483 :                     *T0_frac = 0;
    1596         483 :                     move16();
    1597             :                 }
    1598             : 
    1599        2788 :                 IF( LT_16( *T0, PIT16k_FR2_TC0_2SUBFR ) )
    1600             :                 {
    1601             :                     /*index = (*T0)*4 + (*T0_frac) - (PIT16k_MIN*4);*/
    1602        1607 :                     index = add( shl( *T0, 2 ), sub( *T0_frac, PIT16k_MIN * 4 ) );
    1603             :                 }
    1604             :                 ELSE
    1605             :                 {
    1606             :                     /*index = (*T0)*2 + ((*T0_frac)>>1) - (PIT16k_FR2_TC0_2SUBFR*2) + ((PIT16k_FR2_TC0_2SUBFR-PIT16k_MIN)*4);*/
    1607        1181 :                     index = add( sub( add( shl( *T0, 1 ), shr( *T0_frac, 1 ) ), ( PIT16k_FR2_TC0_2SUBFR * 2 ) ), ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 );
    1608             :                 }
    1609        2788 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1610             :             }
    1611        9166 :             ELSE IF( EQ_16( nBits, 6 ) )
    1612             :             {
    1613             :                 /* delta search */
    1614        9166 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, L_SUBFR, limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, st_fx->L_frame, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1615        9166 :                 move16();
    1616             : 
    1617        9166 :                 index = delta_pit_enc_fx( 4, *T0, *T0_frac, *T0_min );
    1618        9166 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1619             :             }
    1620             : 
    1621       15737 :             IF( EQ_16( nBits, 6 ) )
    1622             :             {
    1623        9166 :                 limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
    1624             :             }
    1625             : 
    1626             :             /*-----------------------------------------------------------------*
    1627             :              * - gain clipping test to avoid unstable synthesis
    1628             :              * - LP filtering of the adaptive excitation
    1629             :              * - codebook target computation
    1630             :              *-----------------------------------------------------------------*/
    1631       15737 :             test();
    1632       15737 :             IF( ( EQ_16( i_subfr, L_SUBFR ) ) && ( EQ_16( *T0, 2 * L_SUBFR ) ) )
    1633             :             {
    1634         483 :                 *gain_pit_fx = 0;
    1635         483 :                 move16();
    1636         483 :                 *clip_gain = 0;
    1637         483 :                 move16();
    1638         483 :                 g_corr_fx[0] = 328 /* 0.01f in Q15 */;
    1639         483 :                 move16();
    1640         483 :                 g_corr_fx[1] = 0;
    1641         483 :                 move16();
    1642         483 :                 g_corr_fx[2] = 328 /* 0.01f in Q15 */;
    1643         483 :                 move16();
    1644         483 :                 g_corr_fx[3] = 0;
    1645         483 :                 move16();
    1646         483 :                 *Jopt_flag = 0;
    1647         483 :                 move16();
    1648             : 
    1649         483 :                 set16_fx( &exc_fx[i_subfr], 0, L_SUBFR + 1 );   /* set excitation for current subrame to 0 */
    1650         483 :                 push_indice( hBstr, IND_LP_FILT_SELECT, 0, 1 ); /* this bit is actually not needed */
    1651             : 
    1652         483 :                 Copy( xn_fx, xn2_fx, L_SUBFR ); /* target vector for codebook search */
    1653         483 :                 set16_fx( y1_fx, 0, L_SUBFR );  /* set filtered adaptive excitation to 0 */
    1654         483 :                 set16_fx( &bwe_exc_fx[i_subfr * 2], 0, L_SUBFR * 2 );
    1655             :             }
    1656             :             ELSE
    1657             :             {
    1658             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
    1659       15254 :                 pred_lt4_ivas_fx( &exc_fx[i_subfr], &exc_fx[i_subfr], *T0, *T0_frac, L_SUBFR + 1, L_pitch_inter4_2, L_INTERPOL2, PIT_UP_SAMP );
    1660       15254 :                 offset = L_deposit_l( 0 );
    1661             : 
    1662       15254 :                 tmp = extract_l( L_mult( *T0_frac, 32 ) ); /*Q8, 0.25 in Q7*/
    1663       15254 :                 tmp = add( 512, tmp );                     /*Q8; 2 in Q8*/
    1664       15254 :                 tmp = mult_r( tmp, 256 );                  /*Q16->Q0; 2 in Q7*/
    1665             : 
    1666       15254 :                 tmp1 = sub( *T0, 2 );  /*Q0*/
    1667       15254 :                 tmp1 = shl( tmp1, 1 ); /*Q0 */
    1668             : 
    1669       15254 :                 offset = add( tmp, tmp1 ); /*Q0*/
    1670     1967766 :                 FOR( i = 0; i < L_SUBFR * 2; i++ )
    1671             :                 {
    1672             :                     /* bwe_exc_fx[i + i_subfr * 2] = bwe_exc_fx[i + i_subfr * 2 - *T0 * 2  - (int) ((float) *T0_frac * 0.5f + 4 + 0.5f) + 4];move16();*/
    1673     1952512 :                     bwe_exc_fx[i + i_subfr * 2] = bwe_exc_fx[i + i_subfr * 2 - offset + 4];
    1674     1952512 :                     move16();
    1675             :                 }
    1676             : 
    1677       15254 :                 *clip_gain = gp_clip_fx( st_fx->element_mode, st_fx->core_brate, st_fx->voicing_fx, i_subfr, TRANSITION, xn_fx, gp_cl_fx, Q_new );
    1678       15254 :                 move16();
    1679             : 
    1680       15254 :                 lp_select = lp_filt_exc_enc_fx( MODE1, TRANSITION, i_subfr, exc_fx, h1_fx,
    1681       15254 :                                                 xn_fx, y1_fx, xn2_fx, L_SUBFR, st_fx->L_frame, g_corr_fx, *clip_gain, gain_pit_fx, &lp_flag );
    1682       15254 :                 IF( EQ_16( lp_flag, NORMAL_OPERATION ) )
    1683             :                 {
    1684       15254 :                     push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
    1685             :                 }
    1686             : 
    1687       15254 :                 *Jopt_flag = 1;
    1688       15254 :                 move16();
    1689             :             }
    1690             : 
    1691             :             /***pt_pitch = (float)(*T0) + (float)(*T0_frac)/4.0f;*/ /* save subframe pitch value  */
    1692             :             /***pt_pitch_fx = shl(add(*T0,shr(*T0_frac,2)),4); move16();*/
    1693       15737 :             tmp = shl( add( shl( *T0, 2 ), *T0_frac ), 4 );
    1694       15737 :             **pt_pitch_fx = tmp;
    1695       15737 :             move16();
    1696             : 
    1697             :             /*---------------------------------------------------------------------*
    1698             :              * fill the pitch buffer - needed for post-processing
    1699             :              *---------------------------------------------------------------------*/
    1700       15737 :             test();
    1701       15737 :             test();
    1702       15737 :             IF( ( EQ_16( sub( i_subfr, *tc_subfr ), L_SUBFR ) ) || ( *tc_subfr == 0 && EQ_16( i_subfr, 2 * L_SUBFR ) ) )
    1703             :             {
    1704        7494 :                 index = shr( i_subfr, 6 );
    1705        7494 :                 ( *pt_pitch_fx ) -= index;
    1706        7494 :                 move16();
    1707             : 
    1708       20863 :                 FOR( i = 0; i < index; i++ )
    1709             :                 {
    1710       13369 :                     **pt_pitch_fx = tmp;
    1711       13369 :                     move16();
    1712       13369 :                     ( *pt_pitch_fx )++;
    1713       13369 :                     move16();
    1714             :                 }
    1715             :             }
    1716             :         }
    1717             :     }
    1718             : 
    1719       63958 :     return;
    1720             : }
    1721             : 
    1722             : 
    1723             : /*-------------------------------------------------------------------------------------------*
    1724             :  * tc_enc()
    1725             :  *
    1726             :  * Principal function for transition coding (TC) in encoder.
    1727             :  * Glottal codebook contribution part:
    1728             :  *
    1729             :  *           |----|             |----|                                      xn
    1730             :  *  imp_pos->||   |  imp_shape->| g1 |                                       |
    1731             :  *           | |  |             | g2 |     ----   exc  |---|  y1   ----      |
    1732             :  *           |  | |-------------|    |----|gain|-------| h |------|gain|----(-)---> xn2
    1733             :  *           |   ||             | gn |     ----        |---|       ----
    1734             :  *           |----|             |----|
    1735             :  *          codebook          excitation gain_trans    h_orig     gain_pit
    1736             :  *
    1737             :  *-------------------------------------------------------------------------------------------*/
    1738             : 
    1739         124 : static void tc_enc_fx(
    1740             :     Encoder_State *st_fx, /* i/o: encoder state structure */
    1741             :     const Word16 i_subfr, /* i  : subrame index                           */
    1742             :     Word16 *tc_subfr,     /* i/o: TC subframe index                       */
    1743             :     Word16 *position,     /* i/o: index of the residual signal maximum    */
    1744             :     const Word16 *h1_fx,  /* i  : weighted filter input response          Q(14+shift)*/
    1745             :     const Word16 *xn_fx,  /* i  : target signal                           Q_new-1+shift*/
    1746             :     Word16 *exc_fx,       /* o  : glottal codebook contribution           Q_new*/
    1747             :     Word16 *yy1_fx,       /* o  : filtered glottal codebook contribution  */
    1748             :     Word16 *T0_min,       /* o  : lower pitch limit                       Q0*/
    1749             :     Word16 *T0_max,       /* o  : higher pitch limit                      Q0*/
    1750             :     Word16 *T0,           /* o  : close loop integer pitch                Q0*/
    1751             :     Word16 *T0_frac,      /* o  : close loop fractional part of the pitch Q0*/
    1752             :     Word16 *gain_pit_fx,  /* o  : pitch gain  (0..GAIN_PIT_MAX)           Q14*/
    1753             :     Word16 g_corr_fx[],   /* o  : correlations <y1,y1>  and -2<xn,y1>     */
    1754             :     Word16 *bwe_exc_fx,   /* i/o: excitation for SWB TBE               Q_new*/
    1755             :     Word16 Q_new          /* i  : input scaling  */
    1756             : )
    1757             : {
    1758             :     Word16 i, imp_shape, imp_pos, index, nBits, h1_tmp_fx[L_SUBFR];
    1759             :     Word16 pitch_index, pitch_sign_fx;
    1760             :     Word32 gain_trans32;
    1761         124 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    1762             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1763         124 :     Flag Overflow = 0;
    1764         124 :     move32();
    1765             : #endif
    1766         124 :     imp_pos = sub( *position, i_subfr );
    1767        8060 :     FOR( i = 0; i < L_SUBFR; i++ )
    1768             :     {
    1769        7936 :         h1_tmp_fx[i] = h1_fx[i];
    1770        7936 :         move16();
    1771             :     }
    1772             :     /*-----------------------------------------------------------------*
    1773             :      * get number of bits for pitch encoding
    1774             :      *-----------------------------------------------------------------*/
    1775             : 
    1776         124 :     nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
    1777         124 :     move16();
    1778             : 
    1779             :     /*--------------------------------------------------------------*
    1780             :      * Closed loop pitch search
    1781             :      *--------------------------------------------------------------*/
    1782             : 
    1783         124 :     *T0_frac = 0;
    1784         124 :     move16();
    1785             : 
    1786         124 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1787             :     {
    1788          74 :         test();
    1789          74 :         IF( ( LE_16( *T0_min, L_SUBFR ) ) || ( EQ_16( *tc_subfr, 3 * L_SUBFR ) ) )
    1790             :         {
    1791          56 :             IF( EQ_16( nBits, 9 ) )
    1792             :             {
    1793          20 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, 0, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1794          20 :                 move16();
    1795             :             }
    1796          36 :             ELSE IF( EQ_16( nBits, 6 ) )
    1797             :             {
    1798          36 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MIN, L_SUBFR, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1799          36 :                 move16();
    1800             :             }
    1801             :             ELSE
    1802             :             {
    1803           0 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MAX, PIT_MIN, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1804           0 :                 move16();
    1805             :             }
    1806             :         }
    1807             :         ELSE
    1808             :         {
    1809          18 :             *T0 = L_SUBFR;
    1810          18 :             move16();
    1811             :         }
    1812             : 
    1813          74 :         test();
    1814          74 :         if ( EQ_16( *tc_subfr, L_SUBFR ) && LT_16( *T0, L_SUBFR ) )
    1815             :         {
    1816           0 :             *T0 = L_SUBFR;
    1817           0 :             move16();
    1818             :         }
    1819             :     }
    1820             :     ELSE /* st_fx->L_frame == L_FRAME16k */
    1821             :     {
    1822          50 :         IF( EQ_16( nBits, 10 ) )
    1823             :         {
    1824           1 :             *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, 1, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1825           1 :             move16();
    1826             :         }
    1827          49 :         ELSE IF( EQ_16( nBits, 6 ) )
    1828             :         {
    1829             :             /* T0_frac with 1/2 sample resolution */
    1830          49 :             *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1_fx, *T0_min, *T0_max, T0_frac, 0, 0, PIT16k_MIN, L_SUBFR, L_FRAME16k, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    1831          49 :             move16();
    1832          49 :             IF( *T0 > L_SUBFR )
    1833             :             {
    1834          32 :                 *T0 = L_SUBFR;
    1835          32 :                 move16();
    1836          32 :                 *T0_frac = 0;
    1837          32 :                 move16();
    1838             :             }
    1839             :         }
    1840             :     }
    1841             : 
    1842             :     /* set tc_subfr to TC_0_0 */
    1843         124 :     test();
    1844         124 :     test();
    1845         124 :     test();
    1846         124 :     if ( i_subfr == 0 && EQ_16( st_fx->L_frame, L_FRAME ) && ( LT_16( *T0, L_SUBFR ) || EQ_16( *tc_subfr, 3 * L_SUBFR ) ) )
    1847             :     {
    1848          31 :         *tc_subfr = TC_0_0;
    1849          31 :         move16();
    1850             :     }
    1851             : 
    1852             :     /*--------------------------------------------------------------*
    1853             :      * Builds glottal codebook contribution
    1854             :      *--------------------------------------------------------------*/
    1855             : 
    1856         124 :     set_impulse_fx( xn_fx, h1_tmp_fx, &exc_fx[i_subfr], yy1_fx, &imp_shape, &imp_pos, &gain_trans32, Q_new ); /*chk h1_tmp_fx*/
    1857             : 
    1858             :     /*--------------------------------------------------------------*
    1859             :      * quantize gain_trans and scale glottal codebook contribution
    1860             :      *--------------------------------------------------------------*/
    1861             : 
    1862         124 :     gain_trans_enc_fx( gain_trans32, &exc_fx[i_subfr], &pitch_index, &pitch_sign_fx, Q_new );
    1863             : 
    1864             :     /* set past excitation buffer to zeros */
    1865         124 :     set16_fx( exc_fx - L_EXC_MEM, 0, L_EXC_MEM );
    1866             : 
    1867             :     /*--------------------------------------------------------------*
    1868             :      * adapt. search of the second impulse in the same subframe
    1869             :      * (when appears)
    1870             :      *--------------------------------------------------------------*/
    1871             : 
    1872         124 :     pred_lt4_tc_fx( exc_fx, *T0, *T0_frac, inter4_2_fx, imp_pos, i_subfr );
    1873         124 :     IF( st_fx->hBWE_TD != NULL )
    1874             :     {
    1875         124 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1876             :         {
    1877          74 :             interp_code_5over2_fx( &exc_fx[i_subfr], &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
    1878             :         }
    1879             :         ELSE
    1880             :         {
    1881          50 :             interp_code_4over2_fx( &exc_fx[i_subfr], &bwe_exc_fx[i_subfr * 2], L_SUBFR );
    1882             :         }
    1883             :     }
    1884             : 
    1885             :     /*--------------------------------------------------------------*
    1886             :      * compute glottal-shape codebook excitation
    1887             :      *--------------------------------------------------------------*/
    1888             : 
    1889             :     /* create filtered glottal codebook contribution */
    1890         124 :     conv_fx( &exc_fx[i_subfr], h1_fx, yy1_fx, L_SUBFR );
    1891             : 
    1892             :     /* gain_pit computation */
    1893         124 :     *gain_pit_fx = corr_xy1_fx( xn_fx, yy1_fx, g_corr_fx, L_SUBFR, 0, &Overflow );
    1894         124 :     move16();
    1895             : 
    1896             :     /*--------------------------------------------------------------*
    1897             :      * Encode parameters and write indices
    1898             :      *--------------------------------------------------------------*/
    1899             : 
    1900         124 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    1901             :     {
    1902          74 :         test();
    1903          74 :         test();
    1904          74 :         IF( ( ( i_subfr != 0 ) || ( EQ_16( *tc_subfr, TC_0_0 ) ) ) && ( NE_16( *tc_subfr, L_SUBFR ) ) )
    1905             :         {
    1906          52 :             test();
    1907             :             /* write pitch index */
    1908          52 :             IF( ( GE_16( *T0, L_SUBFR ) ) && ( NE_16( *tc_subfr, 3 * L_SUBFR ) ) )
    1909             :             {
    1910           1 :                 push_indice( hBstr, IND_PITCH, 0, nBits );
    1911             :             }
    1912          51 :             ELSE IF( EQ_16( *tc_subfr, 3 * L_SUBFR ) )
    1913             :             {
    1914          20 :                 IF( EQ_16( nBits, 9 ) )
    1915             :                 {
    1916          20 :                     index = abs_pit_enc_fx( 4, 0, *T0, *T0_frac );
    1917             :                 }
    1918             :                 ELSE
    1919             :                 {
    1920           0 :                     index = abs_pit_enc_fx( 2, 0, *T0, *T0_frac );
    1921             :                 }
    1922          20 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    1923             : 
    1924          20 :                 limit_T0_fx( L_FRAME, 8, 0, 0, *T0, 0, T0_min, T0_max );
    1925             :             }
    1926             :             ELSE
    1927             :             {
    1928          31 :                 IF( EQ_16( nBits, 6 ) )
    1929             :                 {
    1930          31 :                     index = delta_pit_enc_fx( 2, *T0, *T0_frac, PIT_MIN - 1 );
    1931          31 :                     push_indice( hBstr, IND_PITCH, index, nBits );
    1932             :                 }
    1933             :                 ELSE
    1934             :                 {
    1935           0 :                     index = delta_pit_enc_fx( 0, *T0, *T0_frac, PIT_MIN - 1 );
    1936           0 :                     push_indice( hBstr, IND_PITCH, index, nBits );
    1937             :                 }
    1938             :             }
    1939             :         }
    1940             :     }
    1941             :     ELSE /* st_fx->L_frame == L_FRAME16k */
    1942             :     {
    1943          50 :         IF( EQ_16( nBits, 10 ) )
    1944             :         {
    1945           1 :             pit16k_Q_enc_fx( hBstr, nBits, 1, *T0, *T0_frac, T0_min, T0_max );
    1946             :         }
    1947          49 :         ELSE IF( EQ_16( nBits, 6 ) )
    1948             :         {
    1949          49 :             index = add( shl( sub( *T0, PIT16k_MIN ), 1 ), shr( *T0_frac, 1 ) );
    1950          49 :             push_indice( hBstr, IND_PITCH, index, nBits );
    1951             :         }
    1952             :     }
    1953             : 
    1954         124 :     push_indice( hBstr, IND_TC_IMP_SHAPE, imp_shape, 3 );
    1955         124 :     push_indice( hBstr, IND_TC_IMP_POS, imp_pos, 6 );
    1956         124 :     push_indice( hBstr, IND_TC_IMP_SIGN, pitch_sign_fx, 1 );
    1957         124 :     push_indice( hBstr, IND_TC_IMP_GAIN, pitch_index, 3 );
    1958             : 
    1959         124 :     *position = add( imp_pos, i_subfr );
    1960         124 :     move16();
    1961             : 
    1962         124 :     return;
    1963             : }
    1964             : 
    1965             : 
    1966       14289 : static void tc_enc_ivas_fx(
    1967             :     Encoder_State *st_fx, /* i/o: encoder state structure */
    1968             :     const Word16 i_subfr, /* i  : subrame index                           */
    1969             :     Word16 *tc_subfr,     /* i/o: TC subframe index                       */
    1970             :     Word16 *position,     /* i/o: index of the residual signal maximum    */
    1971             :     const Word16 *h1,     /* i  : weighted filter input response          Q(14+shift)*/
    1972             :     const Word16 *xn_fx,  /* i  : target signal                           Q_new-1+shift*/
    1973             :     Word16 *exc_fx,       /* o  : glottal codebook contribution           Q_new*/
    1974             :     Word16 *yy1_fx,       /* o  : filtered glottal codebook contribution  */
    1975             :     Word16 *T0_min,       /* o  : lower pitch limit                       Q0*/
    1976             :     Word16 *T0_max,       /* o  : higher pitch limit                      Q0*/
    1977             :     Word16 *T0,           /* o  : close loop integer pitch                Q0*/
    1978             :     Word16 *T0_frac,      /* o  : close loop fractional part of the pitch Q0*/
    1979             :     Word16 *gain_pit_fx,  /* o  : pitch gain  (0..GAIN_PIT_MAX)           Q14*/
    1980             :     Word16 g_corr_fx[],   /* o  : correlations <y1,y1>  and -2<xn,y1>     */
    1981             :     Word16 *bwe_exc_fx,   /* i/o: excitation for SWB TBE               Q_new*/
    1982             :     Word16 Q_new          /* i  : input scaling  */
    1983             : )
    1984             : {
    1985             : 
    1986             :     Word16 h1_fx[L_SUBFR + ( M + 1 )];
    1987             :     Word16 imp_shape, imp_pos, index, nBits;
    1988             :     Word16 pitch_index, pitch_sign_fx;
    1989             :     Word32 gain_trans32;
    1990       14289 :     BSTR_ENC_HANDLE hBstr = st_fx->hBstr;
    1991             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1992       14289 :     Flag Overflow = 0;
    1993       14289 :     move32();
    1994             : #endif
    1995       14289 :     imp_pos = sub( *position, i_subfr );
    1996             :     /*-----------------------------------------------------------------*
    1997             :      * get number of bits for pitch encoding
    1998             :      *-----------------------------------------------------------------*/
    1999             : 
    2000       14289 :     nBits = st_fx->acelp_cfg.pitch_bits[shr( i_subfr, 6 )];
    2001       14289 :     move16();
    2002             : 
    2003             :     /*--------------------------------------------------------------*
    2004             :      * Closed loop pitch search
    2005             :      *--------------------------------------------------------------*/
    2006             : 
    2007       14289 :     *T0_frac = 0;
    2008       14289 :     move16();
    2009             : 
    2010       14289 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    2011             :     {
    2012        7487 :         test();
    2013        7487 :         IF( ( LE_16( *T0_min, L_SUBFR ) ) || ( EQ_16( *tc_subfr, 3 * L_SUBFR ) ) )
    2014             :         {
    2015        4598 :             IF( EQ_16( nBits, 9 ) )
    2016             :             {
    2017        1798 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    2018        1798 :                 move16();
    2019             :             }
    2020        2800 :             ELSE IF( EQ_16( nBits, 6 ) )
    2021             :             {
    2022        2223 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MIN, L_SUBFR, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    2023        2223 :                 move16();
    2024             :             }
    2025             :             ELSE
    2026             :             {
    2027         577 :                 *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MAX, PIT_MIN, L_FRAME, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    2028         577 :                 move16();
    2029             :             }
    2030             :         }
    2031             :         ELSE
    2032             :         {
    2033        2889 :             *T0 = L_SUBFR;
    2034        2889 :             move16();
    2035             :         }
    2036             : 
    2037        7487 :         test();
    2038        7487 :         if ( EQ_16( *tc_subfr, L_SUBFR ) && LT_16( *T0, L_SUBFR ) )
    2039             :         {
    2040         247 :             *T0 = L_SUBFR;
    2041         247 :             move16();
    2042             :         }
    2043             :     }
    2044             :     ELSE /* st_fx->L_frame == L_FRAME16k */
    2045             :     {
    2046        6802 :         IF( EQ_16( nBits, 10 ) )
    2047             :         {
    2048        3019 :             *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1, *T0_min, *T0_max, T0_frac, 0, 1, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    2049        3019 :             move16();
    2050             :         }
    2051        3783 :         ELSE IF( EQ_16( nBits, 6 ) )
    2052             :         {
    2053             :             /* T0_frac with 1/2 sample resolution */
    2054        3783 :             *T0 = pitch_fr4_fx( &exc_fx[i_subfr], xn_fx, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT16k_MIN, L_SUBFR, L_FRAME16k, L_SUBFR, 0 /*st_fx->element_mode*/, Q_new );
    2055        3783 :             move16();
    2056             : 
    2057        3783 :             IF( *T0 > L_SUBFR )
    2058             :             {
    2059        3044 :                 *T0 = L_SUBFR;
    2060        3044 :                 move16();
    2061        3044 :                 *T0_frac = 0;
    2062        3044 :                 move16();
    2063             :             }
    2064             :         }
    2065             :     }
    2066             : 
    2067             :     /* set tc_subfr to TC_0_0 */
    2068       14289 :     test();
    2069       14289 :     test();
    2070       14289 :     test();
    2071       14289 :     if ( i_subfr == 0 && EQ_16( st_fx->L_frame, L_FRAME ) && ( LT_16( *T0, L_SUBFR ) || EQ_16( *tc_subfr, 3 * L_SUBFR ) ) )
    2072             :     {
    2073        1676 :         *tc_subfr = TC_0_0;
    2074        1676 :         move16();
    2075             :     }
    2076             : 
    2077             :     /*--------------------------------------------------------------*
    2078             :      * Builds glottal codebook contribution
    2079             :      *--------------------------------------------------------------*/
    2080             : 
    2081       14289 :     set_impulse_fx( xn_fx, h1, &exc_fx[i_subfr], yy1_fx, &imp_shape, &imp_pos, &gain_trans32, Q_new );
    2082             : 
    2083             :     /*--------------------------------------------------------------*
    2084             :      * quantize gain_trans and scale glottal codebook contribution
    2085             :      *--------------------------------------------------------------*/
    2086             : 
    2087       14289 :     gain_trans_enc_fx( gain_trans32, &exc_fx[i_subfr], &pitch_index, &pitch_sign_fx, Q_new );
    2088             : 
    2089             :     /* set past excitation buffer to zeros */
    2090       14289 :     set16_fx( exc_fx - L_EXC_MEM, 0, L_EXC_MEM );
    2091             : 
    2092             :     /*--------------------------------------------------------------*
    2093             :      * adapt. search of the second impulse in the same subframe
    2094             :      * (when appears)
    2095             :      *--------------------------------------------------------------*/
    2096             : 
    2097       14289 :     pred_lt4_tc_fx( exc_fx, *T0, *T0_frac, inter4_2_fx, imp_pos, i_subfr );
    2098             : 
    2099       14289 :     IF( st_fx->hBWE_TD != NULL )
    2100             :     {
    2101       14289 :         IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    2102             :         {
    2103        7487 :             interp_code_5over2_fx( &exc_fx[i_subfr], &bwe_exc_fx[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
    2104             :         }
    2105             :         ELSE
    2106             :         {
    2107        6802 :             interp_code_4over2_fx( &exc_fx[i_subfr], &bwe_exc_fx[i_subfr * 2], L_SUBFR );
    2108             :         }
    2109             :     }
    2110             : 
    2111             :     /*--------------------------------------------------------------*
    2112             :      * compute glottal-shape codebook excitation
    2113             :      *--------------------------------------------------------------*/
    2114             : 
    2115       14289 :     Copy( h1, h1_fx, L_SUBFR + ( M + 1 ) );
    2116             :     /* create filtered glottal codebook contribution */
    2117       14289 :     conv_fx( &exc_fx[i_subfr], h1_fx, yy1_fx, L_SUBFR );
    2118             : 
    2119             :     /* gain_pit computation */
    2120       14289 :     *gain_pit_fx = corr_xy1_fx( xn_fx, yy1_fx, g_corr_fx, L_SUBFR, 0, &Overflow );
    2121       14289 :     move16();
    2122             : 
    2123             :     /*--------------------------------------------------------------*
    2124             :      * Encode parameters and write indices
    2125             :      *--------------------------------------------------------------*/
    2126       14289 :     IF( EQ_16( st_fx->L_frame, L_FRAME ) )
    2127             :     {
    2128        7487 :         test();
    2129        7487 :         test();
    2130        7487 :         IF( ( ( i_subfr != 0 ) || ( EQ_16( *tc_subfr, TC_0_0 ) ) ) && ( NE_16( *tc_subfr, L_SUBFR ) ) )
    2131             :         {
    2132        4053 :             test();
    2133             :             /* write pitch index */
    2134        4053 :             IF( ( GE_16( *T0, L_SUBFR ) ) && ( NE_16( *tc_subfr, 3 * L_SUBFR ) ) )
    2135             :             {
    2136         368 :                 push_indice( hBstr, IND_PITCH, 0, nBits );
    2137             :             }
    2138        3685 :             ELSE IF( EQ_16( *tc_subfr, 3 * L_SUBFR ) )
    2139             :             {
    2140        1814 :                 IF( EQ_16( nBits, 9 ) )
    2141             :                 {
    2142        1798 :                     index = abs_pit_enc_fx( 4, 0, *T0, *T0_frac );
    2143             :                 }
    2144             :                 ELSE
    2145             :                 {
    2146          16 :                     index = abs_pit_enc_fx( 2, 0, *T0, *T0_frac );
    2147             :                 }
    2148        1814 :                 push_indice( hBstr, IND_PITCH, index, nBits );
    2149             : 
    2150        1814 :                 limit_T0_fx( L_FRAME, 8, 0, 0, *T0, 0, T0_min, T0_max );
    2151             :             }
    2152             :             ELSE
    2153             :             {
    2154        1871 :                 IF( EQ_16( nBits, 6 ) )
    2155             :                 {
    2156        1676 :                     index = delta_pit_enc_fx( 2, *T0, *T0_frac, PIT_MIN - 1 );
    2157        1676 :                     push_indice( hBstr, IND_PITCH, index, nBits );
    2158             :                 }
    2159             :                 ELSE
    2160             :                 {
    2161         195 :                     index = delta_pit_enc_fx( 0, *T0, *T0_frac, PIT_MIN - 1 );
    2162         195 :                     push_indice( hBstr, IND_PITCH, index, nBits );
    2163             :                 }
    2164             :             }
    2165             :         }
    2166             :     }
    2167             :     ELSE /* st_fx->L_frame == L_FRAME16k */
    2168             :     {
    2169        6802 :         IF( EQ_16( nBits, 10 ) )
    2170             :         {
    2171        3019 :             pit16k_Q_enc_fx( hBstr, nBits, 1, *T0, *T0_frac, T0_min, T0_max );
    2172             :         }
    2173        3783 :         ELSE IF( EQ_16( nBits, 6 ) )
    2174             :         {
    2175        3783 :             index = add( shl( sub( *T0, PIT16k_MIN ), 1 ), shr( *T0_frac, 1 ) );
    2176        3783 :             push_indice( hBstr, IND_PITCH, index, nBits );
    2177             :         }
    2178             :     }
    2179       14289 :     push_indice( hBstr, IND_TC_IMP_SHAPE, imp_shape, 3 );
    2180       14289 :     push_indice( hBstr, IND_TC_IMP_POS, imp_pos, 6 );
    2181       14289 :     push_indice( hBstr, IND_TC_IMP_SIGN, pitch_sign_fx, 1 );
    2182       14289 :     push_indice( hBstr, IND_TC_IMP_GAIN, pitch_index, 3 );
    2183             : 
    2184       14289 :     *position = add( imp_pos, i_subfr );
    2185       14289 :     move16();
    2186       14289 :     return;
    2187             : }
    2188             : 
    2189             : 
    2190             : /*-----------------------------------------------------------------*
    2191             :  * gain_trans_enc()
    2192             :  *
    2193             :  * Quantize gain_trans of TC (gains of glottal impulses).
    2194             :  * - Uses scalar quantization prototypes tbl_gain_trans_tc[N_GAIN_TC].
    2195             :  * - Gains the glottal codebook contibution signal.
    2196             :  *-----------------------------------------------------------------*/
    2197             : 
    2198       14413 : static void gain_trans_enc_fx(
    2199             :     Word32 gain_trans32, /* i  : gain for mode Tc Q7             */
    2200             :     Word16 exc[],        /* i/o: glottal codebook contribution i:Q13 o:Q_new*gain_trans */
    2201             :     Word16 *quant_index, /* o  : index of quantized gain_trans   */
    2202             :     Word16 *quant_sign,  /* o  : sign of quantized gain_trans    */
    2203             :     Word16 Q_new         /* i  : curent scaling                  */
    2204             : )
    2205             : {
    2206             :     Word16 i, imax, istart, tmp16, gain_trans, gscale;
    2207             : #ifndef ISSUE_1867_replace_overflow_libenc
    2208             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    2209             :     Flag Overflow = 0;
    2210             :     move32();
    2211             : #endif
    2212             : #endif
    2213             : 
    2214       14413 :     istart = 0;
    2215       14413 :     move16();
    2216       14413 :     imax = 4;
    2217       14413 :     move16();
    2218       14413 :     gscale = 7;
    2219       14413 :     move16();
    2220             : #ifdef ISSUE_1867_replace_overflow_libenc
    2221       14413 :     gain_trans = extract_h( L_shl_sat( gain_trans32, 16 ) ); /* Q7 */
    2222             : #else
    2223             :     gain_trans = extract_h( L_shl_o( gain_trans32, 16, &Overflow ) );         /* Q7 */
    2224             : #endif
    2225             : 
    2226       14413 :     IF( GT_32( L_abs( gain_trans32 ), 29862L ) )
    2227             :     {
    2228             : #ifdef ISSUE_1867_replace_overflow_libenc
    2229        8043 :         gain_trans = extract_h( L_shl_sat( gain_trans32, 16 - 3 ) ); /* Q4 */
    2230             : #else
    2231             :         gain_trans = extract_h( L_shl_o( gain_trans32, 16 - 3, &Overflow ) ); /* Q4 */
    2232             : #endif
    2233        8043 :         istart = 4;
    2234        8043 :         move16();
    2235        8043 :         imax = N_GAIN_TC - 1;
    2236        8043 :         move16();
    2237        8043 :         gscale = 4;
    2238        8043 :         move16();
    2239             :     }
    2240             : 
    2241             :     /* qsign = 0 if *gain_trans < 0 else qsign = 1*/
    2242       14413 :     tmp16 = shr( gain_trans, 16 );
    2243       14413 :     *quant_sign = add( 1, tmp16 );
    2244       14413 :     move16();                 /* quantize sign */
    2245       14413 :     tmp16 = s_or( tmp16, 1 ); /* Keep sign */
    2246       14413 :     gain_trans = abs_s( gain_trans );
    2247             : 
    2248       14413 :     *quant_index = N_GAIN_TC - 1;
    2249       14413 :     move16();
    2250       35739 :     FOR( i = istart; i < imax; i++ )
    2251             :     {
    2252       33697 :         IF( LE_16( gain_trans, tbl_gain_trans_tc_fx[i] ) )
    2253             :         {
    2254       12371 :             *quant_index = i;
    2255       12371 :             move16();
    2256       12371 :             BREAK;
    2257             :         }
    2258             :     }
    2259             : 
    2260       14413 :     gain_trans = i_mult2( tbl_gain_trans_tc_fx[i], tmp16 ); /* Retreive quantized gain with sign */
    2261       14413 :     tmp16 = sub( Q_new, add( gscale, 13 - 16 + 1 ) );       /*remove 16 from rounding */
    2262      936845 :     FOR( i = 0; i < L_SUBFR; i++ )
    2263             :     {
    2264             :         /*exc[i] *= (*gain_trans);*/
    2265             : #ifdef ISSUE_1867_replace_overflow_libenc
    2266      922432 :         exc[i] = round_fx_sat( L_shl_sat( L_mult( exc[i], gain_trans ), tmp16 ) );
    2267             : #else
    2268             :         exc[i] = round_fx_o( L_shl_o( L_mult( exc[i], gain_trans ), tmp16, &Overflow ), &Overflow );
    2269             : #endif
    2270      922432 :         move16();
    2271             :     }
    2272             : 
    2273       14413 :     return;
    2274             : }
    2275             : 
    2276             : 
    2277             : /*-----------------------------------------------------------------*
    2278             :  * tc_classif_enc_fx()
    2279             :  *
    2280             :  * TC subframe determination
    2281             :  *-----------------------------------------------------------------*/
    2282             : 
    2283       14413 : void tc_classif_enc_fx(
    2284             :     const Word16 Q_new,       /* i  : scaling factor                                */
    2285             :     const Word16 L_frame,     /* i  : length of the frame                           */
    2286             :     Word16 *tc_subfr,         /* o  : TC subframe index                             */
    2287             :     Word16 *position,         /* o  :  maximum of residual signal index             */
    2288             :     const Word16 attack_flag, /* i  : attack flag                                   */
    2289             :     const Word16 pitch,       /* i  : open loop pitch estimates for first halfframe */
    2290             :     const Word16 *res         /* i  : pointer to the LP residual signal frame       */
    2291             : )
    2292             : {
    2293             :     Word32 temp;
    2294             :     Word16 T_op, i;
    2295             : #ifndef ISSUE_1867_replace_overflow_libenc
    2296             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    2297             :     Flag Overflow = 0;
    2298             :     move32();
    2299             : #endif
    2300             : #endif
    2301             : 
    2302       14413 :     T_op = pitch;
    2303       14413 :     move16();
    2304             : 
    2305       14413 :     IF( EQ_16( L_frame, L_FRAME16k ) )
    2306             :     {
    2307             :         /*T_op = (int16_t)(pitch * 1.25f + 0.5f);*/
    2308        6852 :         T_op = add( pitch, mult_r( pitch, 8192 ) );
    2309             :     }
    2310             : 
    2311       14413 :     *tc_subfr = -1;
    2312       14413 :     move16();
    2313       14413 :     IF( attack_flag )
    2314             :     {
    2315        5325 :         *tc_subfr = 3 * L_SUBFR;
    2316        5325 :         move16();
    2317        5325 :         IF( attack_flag > 0 )
    2318             :         {
    2319        5325 :             IF( EQ_16( L_frame, L_FRAME ) )
    2320             :             {
    2321             :                 /* *tc_subfr = NB_SUBFR * (attack_flag - 1) / 32 ; ATT_NSEG == 32*/
    2322        2527 :                 *tc_subfr = mult( i_mult( NB_SUBFR, sub( attack_flag, 1 ) ), 1024 ); /* divide by 32 Q15*/
    2323        2527 :                 move16();
    2324             :             }
    2325             :             ELSE
    2326             :             {
    2327             :                 /* *tc_subfr = NB_SUBFR16k * (attack_flag - 1) / 32 ; */
    2328        2798 :                 *tc_subfr = mult( i_mult( NB_SUBFR16k, sub( attack_flag, 1 ) ), 1024 ); /* divide by 32 Q15*/
    2329        2798 :                 move16();
    2330             :             }
    2331             :             /* *tc_subfr *= L_SUBFR; */
    2332        5325 :             *tc_subfr = i_mult( *tc_subfr, L_SUBFR );
    2333        5325 :             move16();
    2334             :         }
    2335             :     }
    2336             : 
    2337       14413 :     IF( attack_flag )
    2338             :     {
    2339             :         /*-----------------------------------------------------------------*
    2340             :          * TC: subrame determination for glottal shape search
    2341             :          * -------------------------------------------------------
    2342             :          * tc_subfr == 0         - TC in 1st subframe
    2343             :          * tc_subfr == TC_0_0    - TC in 1st subframe + information about T0
    2344             :          * tc_subfr == L_SUBFR   - TC in 2nd subframe
    2345             :          * tc_subfr == 2*L_SUBFR - TC in 3rd subframe
    2346             :          * tc_subfr == 3*L_SUBFR - TC in 4th subframe
    2347             :          *-----------------------------------------------------------------*/
    2348             : 
    2349             :         /** position = emaximum(res + *tc_subfr, min(T_op + 2, L_SUBFR), &temp) + *tc_subfr;*/
    2350        5325 :         *position = add( emaximum_fx( Q_new, res + *tc_subfr, s_min( add( T_op, 2 ), L_SUBFR ), &temp ), *tc_subfr );
    2351        5325 :         move16();
    2352             :     }
    2353             :     ELSE
    2354             :     {
    2355        9088 :         *position = emaximum_fx( Q_new, res, add( T_op, 2 ), &temp );
    2356        9088 :         move16();
    2357             : 
    2358             :         /* correction in case of possibly wrong T_op (double-pitch values) */
    2359        9088 :         test();
    2360        9088 :         test();
    2361        9088 :         test();
    2362        9088 :         IF( ( EQ_16( L_frame, L_FRAME ) && GT_16( T_op, 2 * PIT_MIN ) ) || ( EQ_16( L_frame, L_FRAME16k ) && GT_16( T_op, 2 * PIT16k_MIN ) ) )
    2363             :         {
    2364        4939 :             Word16 position_tmp, len, exp_aver = 0, exp = 0, exp2 = 0;
    2365        4939 :             move16();
    2366        4939 :             move16();
    2367        4939 :             move16();
    2368             :             Word32 aver, temp2, L_sum, L_temp1, L_temp2;
    2369             : 
    2370        4939 :             len = add( shr( T_op, 1 ), 2 );
    2371        4939 :             position_tmp = emaximum_fx( Q_new, res, len, &temp2 );
    2372        4939 :             L_sum = L_mac_sat( 1L, res[0], res[0] );
    2373      314745 :             FOR( i = 1; i < len; i++ )
    2374             :             {
    2375             : #ifdef ISSUE_1867_replace_overflow_libenc
    2376      309806 :                 L_sum = L_mac0_sat( L_sum, res[i], res[i] );
    2377             : #else
    2378             :                 L_sum = L_mac0_o( L_sum, res[i], res[i], &Overflow );
    2379             : #endif
    2380             :             }
    2381        4939 :             aver = L_sum; /*Q = 2*Q_new */
    2382        4939 :             move32();
    2383        4939 :             aver = root_a_over_b_fx( aver, 2 * Q_new, L_shl( len, 15 ), 15, &exp_aver ); /*Q = 31-exp_aver*/
    2384             : 
    2385        4939 :             temp = root_a_fx( temp, 0, &exp );    /* Q=31-exp */
    2386        4939 :             temp2 = root_a_fx( temp2, 0, &exp2 ); /* Q=31-exp2 */
    2387             : 
    2388        4939 :             L_temp2 = Mult_32_16( temp, 26214 ); /* Q=31-exp */
    2389        4939 :             L_temp1 = Mult_32_16( temp, 8192 );  /* Q=31-exp */
    2390             : 
    2391        4939 :             test();
    2392             : #ifdef ISSUE_1867_replace_overflow_libenc
    2393        4939 :             IF( GT_32( temp2, L_shl_sat( L_temp2, ( 31 - exp2 ) - ( 31 - exp ) ) ) &&
    2394             :                 LT_32( aver, L_shl_sat( L_temp1, ( 31 - exp_aver ) - ( 31 - exp ) ) ) )
    2395             : #else
    2396             :             IF( GT_32( temp2, L_shl_o( L_temp2, ( 31 - exp2 ) - ( 31 - exp ), &Overflow ) ) &&
    2397             :                 LT_32( aver, L_shl_o( L_temp1, ( 31 - exp_aver ) - ( 31 - exp ), &Overflow ) ) )
    2398             : #endif
    2399             :             {
    2400         337 :                 *position = position_tmp;
    2401         337 :                 move16();
    2402             :             }
    2403             :         }
    2404        9088 :         *tc_subfr = s_and( *position, 0x7FC0 );
    2405        9088 :         move16();
    2406             :     }
    2407             : 
    2408       14413 :     return;
    2409             : }

Generated by: LCOV version 1.14