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

Generated by: LCOV version 1.14