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

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

Generated by: LCOV version 1.14