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

Generated by: LCOV version 1.14