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

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : #include <stdint.h>
       5             : #include <assert.h>
       6             : #include "options.h"    /* Compilation switches                   */
       7             : #include "cnst.h"       /* Common constants                       */
       8             : #include "rom_enc.h"    /* Encoder static table prototypes        */
       9             : #include "rom_com_fx.h" /* Static table prototypes                */
      10             : #include "rom_com.h"    /* Static table prototypes                */
      11             : //#include "prot_fx.h"       /* Function prototypes                    */
      12             : #include "rom_basop_util.h"
      13             : #include "prot_fx.h"     /* Function prototypes                    */
      14             : #include "prot_fx_enc.h" /* Function prototypes                    */
      15             : #include "prot_fx_enc.h" /* Function prototypes                    */
      16             : 
      17             : #define inv_T0_res InvIntTable
      18             : 
      19             : static void limit_T0_voiced2( Word16 res, const Word16 *T_op, Word16 *T0_min, Word16 *T0_min_frac, Word16 *T0_max, Word16 *T0_max_frac, Word16 pit_min, Word16 pit_max, Word16 i_subfr );
      20             : 
      21             : /*==============================================================================*/
      22             : /* FUNCTION : pit_encode_fx()                                                                                                   */
      23             : /*------------------------------------------------------------------------------*/
      24             : /* PURPOSE :   Close-loop pitch lag search and pitch lag quantization                   */
      25             : /*                         Adaptive excitation construction                                                                     */
      26             : /*------------------------------------------------------------------------------*/
      27             : /*  INPUT ARGUMENTS :                                                                                                           */
      28             : /* _ (Word16) core_brate: core bitrate                               Q0                 */
      29             : /* _ (Word16) Opt_AMR_WB: flag indicating AMR-WB IO mode                 Q0             */
      30             : /* _ (Word16) bwidth    : input signal bandwidth                         Q0             */
      31             : /* _ (Word16[]) T_op    : open loop pitch estimates in current frame Q0                 */
      32             : /* _ (Word16) T0_min    :  lower limit for close-loop search         Q0                 */
      33             : /* _ (Word16) T0_max    : higher limit for close-loop search         Q0                 */
      34             : /* _ (Word16) T0                : close loop integer pitch                       Q0             */
      35             : /* _ (Word16) T0_frac   : close loop fractional part of the pitch        Q0             */
      36             : /* _ (Word16) L_frame_fx        : length of the frame                        Q0                 */
      37             : /* _ (Word16[]) h1              : weighted filter input response                     Q15                */
      38             : /* _ (Word16[]) xn              : target vector                                      Q_new      */
      39             : /* _ (Word16) coder_type        : coding type                                                    Q0                     */
      40             : /* _ (Word16) i_subfr           : current sub frame indicator            Q0                     */
      41             : /* _ (Word16[]) exc_fx          : pointer to excitation signal frame     Q_new          */
      42             : /* _ (Word16[]) L_subfr         : subframe length                                    Q0                 */
      43             : /*------------------------------------------------------------------------------*/
      44             : /* OUTPUT ARGUMENTS :                                                                                                                   */
      45             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Q0)                                    */
      46             : /* _ (Word16) T0_min    :  lower limit for close-loop search         Q0                 */
      47             : /* _ (Word16) T0_max    : higher limit for close-loop search         Q0                 */
      48             : /* _ (Word16) T0                : close loop integer pitch                       Q0             */
      49             : /* _ (Word16) T0_frac   : close loop fractional part of the pitch        Q0             */
      50             : /*------------------------------------------------------------------------------*/
      51             : 
      52             : /*------------------------------------------------------------------------------*/
      53             : /* RETURN ARGUMENTS :                                                                                                                   */
      54             : /* _ None                                                                                                                                               */
      55             : /*==============================================================================*/
      56             : 
      57        5840 : Word16 pit_encode_fx(                                    /* o  : Fractional pitch for each subframe         */
      58             :                       BSTR_ENC_HANDLE hBstr,             /* i/o:  encoder bitstream handle */
      59             :                       const Word16 pitch_bits[],         /* i  : pitch bits                                  */
      60             :                       const Word32 core_brate,           /* i  : core bitrate                               */
      61             :                       const Word16 Opt_AMR_WB,           /* i  : flag indicating AMR-WB IO mode             */
      62             :                       const Word16 L_frame,              /* i  : length of the frame                        */
      63             :                       const Word16 coder_type,           /* i  : coding type                                */
      64             :                       Word16 *limit_flag,                /* i/o: restrained(0) or extended(1) Q limits      */
      65             :                       const Word16 i_subfr,              /* i  : subframe index                             */
      66             :                       Word16 *exc,                       /* i/o: pointer to excitation signal frame         */
      67             :                       const Word16 L_subfr,              /* i  : subframe length                            */
      68             :                       const Word16 *pitch,               /* i  : open loop pitch estimates in current frame */
      69             :                       Word16 *T0_min,                    /* i/o: lower limit for close-loop search          */
      70             :                       Word16 *T0_max,                    /* i/o: higher limit for close-loop search         */
      71             :                       Word16 *T0,                        /* i/o: close loop integer pitch                   */
      72             :                       Word16 *T0_frac,                   /* i/o: close loop fractional part of the pitch    */
      73             :                       const Word16 *h1,                  /* i  : weighted filter input response             */
      74             :                       const Word16 *xn,                  /* i  : target vector                              */
      75             :                       const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag            */
      76             :                       const Word16 tdm_Pri_pitch_buf[]   /* i  : primary channel pitch buffer                */
      77             : )
      78             : {
      79             :     Word16 pitch_cl;
      80             :     Word16 pit_flag, delta, mult_Top, nBits;
      81             :     Word16 L_sufr_sft;
      82             :     Word16 T_op[2]; /* values for two half-frames */
      83             :     (void) tdm_Pitch_reuse_flag;
      84             : 
      85        5840 :     L_sufr_sft = 6;
      86        5840 :     move16();
      87        5840 :     if ( EQ_16( L_subfr, 2 * L_SUBFR ) )
      88             :     {
      89          14 :         L_sufr_sft = 7;
      90          14 :         move16();
      91             :     }
      92             : 
      93             :     /*----------------------------------------------------------------*
      94             :      * convert pitch values to 16kHz domain
      95             :      *----------------------------------------------------------------*/
      96        5840 :     test();
      97        5840 :     test();
      98        5840 :     IF( EQ_16( L_frame, L_FRAME ) || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
      99             :     {
     100        2720 :         Copy( pitch, T_op, 2 );
     101             :     }
     102             :     ELSE /* L_frame == L_FRAME16k  */
     103             :     {
     104             :         /*T_op[0] = (int16_t)(pitch[0] * 1.25f + 0.5f);
     105             :         T_op[1] = (int16_t)(pitch[1] * 1.25f + 0.5f);*/
     106        3120 :         T_op[0] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[0] ), 2 ) ), 1 ), 1 );
     107        3120 :         move16();
     108        3120 :         T_op[1] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[1] ), 2 ) ), 1 ), 1 );
     109        3120 :         move16();
     110             :     }
     111             : 
     112             :     /*----------------------------------------------------------------*
     113             :      * Set pit_flag to 0 for every subframe with absolute pitch search
     114             :      *----------------------------------------------------------------*/
     115        5840 :     pit_flag = i_subfr;
     116        5840 :     move16();
     117        5840 :     if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
     118             :     {
     119        1302 :         pit_flag = 0;
     120        1302 :         move16();
     121             :     }
     122             : 
     123             :     /*-----------------------------------------------------------------*
     124             :      * Limit range of pitch search
     125             :      * Fractional pitch search
     126             :      * Pitch quantization
     127             :      *-----------------------------------------------------------------*/
     128        5840 :     mult_Top = 1;
     129        5840 :     move16();
     130             : 
     131        5840 :     IF( !Opt_AMR_WB )
     132             :     {
     133             :         /*----------------------------------------------------------------*
     134             :          * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
     135             :          *----------------------------------------------------------------*/
     136        5840 :         test();
     137        5840 :         test();
     138        5840 :         IF( i_subfr == 0 )
     139             :         {
     140        1324 :             *limit_flag = 1;
     141        1324 :             move16();
     142        1324 :             if ( EQ_16( coder_type, VOICED ) )
     143             :             {
     144         212 :                 *limit_flag = 2;
     145         212 :                 move16(); /* double-extended limits */
     146             :             }
     147        1324 :             test();
     148        1324 :             if ( EQ_16( coder_type, GENERIC ) && EQ_32( core_brate, ACELP_7k20 ) )
     149             :             {
     150           0 :                 *limit_flag = 0;
     151           0 :                 move16();
     152             :             }
     153             :         }
     154        4516 :         ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && EQ_16( coder_type, GENERIC ) && LE_32( core_brate, ACELP_13k20 ) )
     155             :         {
     156             :             /*if( *T0 > (PIT_FR1_EXTEND_8b + PIT_MIN)>>1 )*/
     157         459 :             if ( GT_16( *T0, shr( add( PIT_FR1_EXTEND_8b, PIT_MIN ), 1 ) ) )
     158             :             {
     159         316 :                 *limit_flag = 0;
     160         316 :                 move16();
     161             :             }
     162             :         }
     163             : 
     164        5840 :         IF( *limit_flag == 0 )
     165             :         {
     166         632 :             test();
     167         632 :             test();
     168         632 :             IF( i_subfr == 0 && LT_16( T_op[0], PIT_MIN ) )
     169             :             {
     170           0 :                 mult_Top = 2;
     171           0 :                 move16();
     172             :             }
     173         632 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && LT_16( T_op[1], PIT_MIN ) )
     174             :             {
     175           7 :                 mult_Top = 2;
     176           7 :                 move16();
     177             :             }
     178             :         }
     179             :         /*-------------------------------------------------------*
     180             :          *  Retrieve the number of Q bits
     181             :          *-------------------------------------------------------*/
     182        5840 :         nBits = 0;
     183        5840 :         move16();
     184        5840 :         IF( NE_16( coder_type, AUDIO ) )
     185             :         {
     186        5826 :             nBits = pitch_bits[i_subfr >> L_sufr_sft];
     187        5826 :             move16();
     188             :         }
     189        5840 :         IF( EQ_16( coder_type, AUDIO ) )
     190             :         {
     191             :             /*-------------------------------------------------------*
     192             :              *  Pitch encoding in AUDIO coder type
     193             :              *  (both ACELP@12k8 and ACELP@16k cores)
     194             :              *-------------------------------------------------------*/
     195             : 
     196          14 :             delta = 4;
     197          14 :             move16();
     198          14 :             test();
     199          14 :             test();
     200          14 :             if ( EQ_16( L_subfr, shr( L_frame, 1 ) ) && i_subfr != 0 && EQ_16( L_frame, L_FRAME ) )
     201             :             {
     202           7 :                 pit_flag = L_SUBFR;
     203           7 :                 move16();
     204             :             }
     205          14 :             IF( pit_flag == 0 )
     206             :             {
     207           7 :                 nBits = 10;
     208           7 :                 move16();
     209             :             }
     210             :             ELSE
     211             :             {
     212           7 :                 nBits = 6;
     213           7 :                 move16();
     214             :             }
     215             : 
     216             :             /* pitch lag search limitation */
     217          14 :             test();
     218          14 :             IF( i_subfr == 0 )
     219             :             {
     220           7 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     221             :             }
     222           7 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && pit_flag == 0 )
     223             :             {
     224           0 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     225             :             }
     226             : 
     227             :             /* search and encode the closed loop pitch period */
     228          14 :             *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_subfr );
     229          14 :             move16();
     230          14 :             pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     231             :         }
     232        5826 :         ELSE IF( EQ_16( coder_type, VOICED ) )
     233             :         {
     234             :             /*-------------------------------------------------------*
     235             :              *  Pitch encoding in VOICED code type (ACELP@12k8 core only)
     236             :              *-------------------------------------------------------*/
     237             : 
     238         848 :             delta = 4;
     239         848 :             move16();
     240         848 :             if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
     241             :             {
     242         212 :                 pit_flag = i_subfr;
     243         212 :                 move16();
     244             :             }
     245             : 
     246             :             /* pitch lag search limitation */
     247         848 :             IF( i_subfr == 0 )
     248             :             {
     249         212 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     250             :             }
     251             : 
     252             :             /* search and encode the closed loop pitch period */
     253             : 
     254         848 :             test();
     255         848 :             IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 5 ) )
     256             :             {
     257         636 :                 *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_DOUBLEEXTEND_9b, PIT_FR1_DOUBLEEXTEND_9b, L_FRAME, L_SUBFR );
     258         636 :                 move16();
     259             :             }
     260         212 :             ELSE IF( EQ_16( nBits, 10 ) )
     261             :             {
     262         212 :                 *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR );
     263         212 :                 move16();
     264             :             }
     265             : 
     266         848 :             pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     267             :         }
     268             :         ELSE
     269             :         {
     270             :             /*-------------------------------------------------------*
     271             :              *  Pitch encoding in GENERIC coder type
     272             :              *  (both ACELP@12k8 and ACELP@16k cores)
     273             :              *-------------------------------------------------------*/
     274             : 
     275        4978 :             delta = 8;
     276        4978 :             move16();
     277             : 
     278             :             /* pitch lag search limitation */
     279        4978 :             IF( i_subfr == 0 )
     280             :             {
     281        1105 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     282             :             }
     283        3873 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
     284             :             {
     285        1083 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     286             :             }
     287             : 
     288             :             /* search and encode the closed loop pitch period */
     289        4978 :             IF( EQ_16( L_frame, L_FRAME ) )
     290             :             {
     291        1839 :                 test();
     292        1839 :                 test();
     293        1839 :                 IF( EQ_16( nBits, 8 ) || EQ_16( nBits, 5 ) )
     294             :                 {
     295           0 :                     IF( *limit_flag == 0 )
     296             :                     {
     297           0 :                         *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     298           0 :                         move16();
     299             :                     }
     300             :                     ELSE
     301             :                     {
     302           0 :                         *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN_EXTEND, PIT_FR1_EXTEND_8b, L_FRAME, L_SUBFR );
     303           0 :                         move16();
     304             :                     }
     305             :                 }
     306        1839 :                 ELSE IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
     307             :                 {
     308        1377 :                     IF( *limit_flag == 0 )
     309             :                     {
     310         632 :                         *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR );
     311         632 :                         move16();
     312             :                     }
     313             :                     ELSE
     314             :                     {
     315         745 :                         *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_EXTEND_9b, PIT_FR1_EXTEND_9b, L_FRAME, L_SUBFR );
     316         745 :                         move16();
     317             :                     }
     318             :                 }
     319         462 :                 ELSE IF( EQ_16( nBits, 10 ) )
     320             :                 {
     321         462 :                     *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR );
     322         462 :                     move16();
     323             :                 }
     324             : 
     325        1839 :                 pit_Q_enc_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     326             :             }
     327             :             ELSE /* L_frame == L_FRAME16k */
     328             :             {
     329        3139 :                 test();
     330        3139 :                 IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
     331             :                 {
     332        1872 :                     *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, L_FRAME16k, L_SUBFR );
     333        1872 :                     move16();
     334             :                 }
     335        1267 :                 ELSE IF( EQ_16( nBits, 10 ) )
     336             :                 {
     337        1267 :                     *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR );
     338        1267 :                     move16();
     339             :                 }
     340             : 
     341        3139 :                 pit16k_Q_enc_fx( hBstr, nBits, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     342             :             }
     343             :         }
     344             :     }
     345             : 
     346             :     /*-------------------------------------------------------*
     347             :      *  Pitch encoding in AMR-WB IO mode
     348             :      *-------------------------------------------------------*/
     349             : 
     350             :     ELSE
     351             :     {
     352           0 :         delta = 8;
     353           0 :         move16();
     354           0 :         *limit_flag = 0;
     355           0 :         move16();
     356             : 
     357           0 :         IF( EQ_32( core_brate, ACELP_6k60 ) )
     358             :         {
     359           0 :             nBits = 5;
     360           0 :             move16();
     361             : 
     362             :             /* pitch lag search limitation */
     363           0 :             IF( i_subfr == 0 )
     364             :             {
     365           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
     366           0 :                 nBits = 8;
     367           0 :                 move16();
     368             :             }
     369             : 
     370           0 :             if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
     371             :             {
     372             :                 /* rewrite pit_flag - it must not be zero */
     373           0 :                 pit_flag = i_subfr;
     374           0 :                 move16();
     375             :             }
     376             : 
     377             :             /* search and encode the closed loop pitch period */
     378           0 :             *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     379           0 :             move16();
     380             :         }
     381           0 :         ELSE IF( EQ_32( core_brate, ACELP_8k85 ) )
     382             :         {
     383           0 :             nBits = 5;
     384           0 :             move16();
     385             : 
     386             :             /* pitch lag search limitation */
     387           0 :             IF( i_subfr == 0 )
     388             :             {
     389           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
     390           0 :                 nBits = 8;
     391           0 :                 move16();
     392             :             }
     393           0 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
     394             :             {
     395           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[1] ), 0, T0_min, T0_max );
     396           0 :                 nBits = 8;
     397           0 :                 move16();
     398             :             }
     399             : 
     400             :             /* search and encode the closed loop pitch period */
     401           0 :             *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     402           0 :             move16();
     403             :         }
     404             :         ELSE
     405             :         {
     406           0 :             nBits = 6;
     407           0 :             move16();
     408             : 
     409             :             /* pitch lag search limitation */
     410           0 :             IF( i_subfr == 0 )
     411             :             {
     412           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
     413           0 :                 nBits = 9;
     414           0 :                 move16();
     415             :             }
     416           0 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
     417             :             {
     418           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[1] ), 0, T0_min, T0_max );
     419           0 :                 nBits = 9;
     420           0 :                 move16();
     421             :             }
     422             :             ELSE
     423             :             {
     424           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     425             :             }
     426             : 
     427             :             /* search and encode the closed loop pitch period */
     428           0 :             *T0 = pitch_fr4_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR );
     429           0 :             move16();
     430             :         }
     431             : 
     432           0 :         pit_Q_enc_fx( hBstr, 1, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     433             :     }
     434             : 
     435             :     /*-------------------------------------------------------*
     436             :      * Compute floating pitch output
     437             :      *-------------------------------------------------------*/
     438             : 
     439             :     /*pitch = (float)(*T0) + (float)(*T0_frac)/4.0f;*/   /* save subframe pitch values  */
     440        5840 :     pitch_cl = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); /* save subframe pitch values Q6  */
     441             : 
     442        5840 :     return pitch_cl;
     443             : }
     444             : 
     445      561603 : Word16 pit_encode_ivas_fx(                                    /* o  : Fractional pitch for each subframe                  */
     446             :                            BSTR_ENC_HANDLE hBstr,             /* i/o:  encoder bitstream handle                           */
     447             :                            const Word16 pitch_bits[],         /* i  : pitch bits                                          */
     448             :                            const Word32 core_brate,           /* i  : core bitrate                                        */
     449             :                            const Word16 Opt_AMR_WB,           /* i  : flag indicating AMR-WB IO mode                      */
     450             :                            const Word16 L_frame,              /* i  : length of the frame                                 */
     451             :                            const Word16 coder_type,           /* i  : coding type                                         */
     452             :                            Word16 *limit_flag,                /* i/o: restrained(0) or extended(1) Q limits               */
     453             :                            const Word16 i_subfr,              /* i  : subframe index                                      */
     454             :                            Word16 *exc,                       /* i/o: pointer to excitation signal frame  Q_new           */
     455             :                            const Word16 L_subfr,              /* i  : subframe length                                     */
     456             :                            const Word16 *pitch,               /* i  : open loop pitch estimates in current frame          */
     457             :                            Word16 *T0_min,                    /* i/o: lower limit for close-loop search                   */
     458             :                            Word16 *T0_max,                    /* i/o: higher limit for close-loop search                  */
     459             :                            Word16 *T0,                        /* i/o: close loop integer pitch                            */
     460             :                            Word16 *T0_frac,                   /* i/o: close loop fractional part of the pitch             */
     461             :                            const Word16 *h1,                  /* i  : weighted filter input response Q(14 - norm_s(h1[0]) */
     462             :                            const Word16 *xn,                  /* i  : target vector                  Q_new                */
     463             :                            const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag                    */
     464             :                            const Word16 tdm_Pri_pitch_buf[],  /* i  : primary channel pitch buffer                        */
     465             :                            Word16 Q_new                       /* i                                                        */
     466             : )
     467             : {
     468             :     Word16 pitch_cl;
     469             :     Word16 pit_flag, delta, mult_Top, nBits;
     470             :     Word16 L_sufr_sft;
     471             :     Word16 T_op[2]; /* values for two half-frames */
     472             : 
     473      561603 :     L_sufr_sft = 6;
     474      561603 :     move16();
     475      561603 :     if ( EQ_16( L_subfr, 2 * L_SUBFR ) )
     476             :     {
     477        6784 :         L_sufr_sft = 7;
     478        6784 :         move16();
     479             :     }
     480             : 
     481             :     /*----------------------------------------------------------------*
     482             :      * convert pitch values to 16kHz domain
     483             :      *----------------------------------------------------------------*/
     484      561603 :     test();
     485      561603 :     test();
     486      561603 :     IF( EQ_16( L_frame, L_FRAME ) || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
     487             :     {
     488      239758 :         Copy( pitch, T_op, 2 );
     489             :     }
     490             :     ELSE /* L_frame == L_FRAME16k  */
     491             :     {
     492             :         /*T_op[0] = (int16_t)(pitch[0] * 1.25f + 0.5f);
     493             :         T_op[1] = (int16_t)(pitch[1] * 1.25f + 0.5f);*/
     494      321845 :         T_op[0] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[0] ), 2 ) ), 1 ), 1 );
     495      321845 :         move16();
     496      321845 :         T_op[1] = shr( add( round_fx( L_shl( L_mult( 20480, pitch[1] ), 2 ) ), 1 ), 1 );
     497      321845 :         move16();
     498             :     }
     499             : 
     500             :     /*----------------------------------------------------------------*
     501             :      * Set pit_flag to 0 for every subframe with absolute pitch search
     502             :      *----------------------------------------------------------------*/
     503      561603 :     pit_flag = i_subfr;
     504      561603 :     move16();
     505      561603 :     if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
     506             :     {
     507      124075 :         pit_flag = 0;
     508      124075 :         move16();
     509             :     }
     510             : 
     511             :     /*-----------------------------------------------------------------*
     512             :      * Limit range of pitch search
     513             :      * Fractional pitch search
     514             :      * Pitch quantization
     515             :      *-----------------------------------------------------------------*/
     516      561603 :     mult_Top = 1;
     517      561603 :     move16();
     518             : 
     519      561603 :     IF( !Opt_AMR_WB )
     520             :     {
     521             :         /*----------------------------------------------------------------*
     522             :          * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
     523             :          *----------------------------------------------------------------*/
     524      561603 :         test();
     525      561603 :         test();
     526      561603 :         IF( i_subfr == 0 )
     527             :         {
     528      132323 :             *limit_flag = 1;
     529      132323 :             move16();
     530      132323 :             if ( EQ_16( coder_type, VOICED ) )
     531             :             {
     532        8608 :                 *limit_flag = 2;
     533        8608 :                 move16(); /* double-extended limits */
     534             :             }
     535      132323 :             test();
     536      132323 :             if ( EQ_16( coder_type, GENERIC ) && EQ_32( core_brate, ACELP_7k20 ) )
     537             :             {
     538         927 :                 *limit_flag = 0;
     539         927 :                 move16();
     540             :             }
     541             :         }
     542      429280 :         ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && EQ_16( coder_type, GENERIC ) && LE_32( core_brate, ACELP_13k20 ) )
     543             :         {
     544             :             /*if( *T0 > (PIT_FR1_EXTEND_8b + PIT_MIN)>>1 )*/
     545       35332 :             if ( GT_16( *T0, shr( add( PIT_FR1_EXTEND_8b, PIT_MIN ), 1 ) ) )
     546             :             {
     547       26725 :                 *limit_flag = 0;
     548       26725 :                 move16();
     549             :             }
     550             :         }
     551             : 
     552      561603 :         IF( *limit_flag == 0 )
     553             :         {
     554       55591 :             test();
     555       55591 :             test();
     556       55591 :             IF( i_subfr == 0 && LT_16( T_op[0], PIT_MIN ) )
     557             :             {
     558          89 :                 mult_Top = 2;
     559          89 :                 move16();
     560             :             }
     561       55502 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && LT_16( T_op[1], PIT_MIN ) )
     562             :             {
     563         883 :                 mult_Top = 2;
     564         883 :                 move16();
     565             :             }
     566             :         }
     567             :         /*-------------------------------------------------------*
     568             :          *  Retrieve the number of Q bits
     569             :          *-------------------------------------------------------*/
     570      561603 :         nBits = 0;
     571      561603 :         move16();
     572      561603 :         IF( NE_16( coder_type, AUDIO ) )
     573             :         {
     574      538143 :             nBits = pitch_bits[i_subfr >> L_sufr_sft];
     575             :         }
     576      561603 :         test();
     577      561603 :         IF( EQ_16( coder_type, AUDIO ) )
     578             :         {
     579             :             /*-------------------------------------------------------*
     580             :              *  Pitch encoding in AUDIO coder type
     581             :              *  (both ACELP@12k8 and ACELP@16k cores)
     582             :              *-------------------------------------------------------*/
     583             : 
     584       23460 :             delta = 4;
     585       23460 :             move16();
     586       23460 :             test();
     587       23460 :             test();
     588       23460 :             if ( EQ_16( L_subfr, shr( L_frame, 1 ) ) && i_subfr != 0 && EQ_16( L_frame, L_FRAME ) )
     589             :             {
     590        3275 :                 pit_flag = L_SUBFR;
     591        3275 :                 move16();
     592             :             }
     593       23460 :             IF( pit_flag == 0 )
     594             :             {
     595       14529 :                 nBits = 10;
     596       14529 :                 move16();
     597             :             }
     598             :             ELSE
     599             :             {
     600        8931 :                 nBits = 6;
     601        8931 :                 move16();
     602             :             }
     603             : 
     604             :             /* pitch lag search limitation */
     605       23460 :             test();
     606       23460 :             IF( i_subfr == 0 )
     607             :             {
     608       11701 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     609             :             }
     610       11759 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) && pit_flag == 0 )
     611             :             {
     612        2828 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     613             :             }
     614             : 
     615             :             /* search and encode the closed loop pitch period */
     616       23460 :             *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_subfr, Q_new );
     617       23460 :             move16();
     618       23460 :             pit_Q_enc_ivas_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     619             :         }
     620      538143 :         ELSE IF( EQ_16( coder_type, VOICED ) )
     621             :         {
     622             :             /*-------------------------------------------------------*
     623             :              *  Pitch encoding in VOICED code type (ACELP@12k8 core only)
     624             :              *-------------------------------------------------------*/
     625             : 
     626       34432 :             delta = 4;
     627       34432 :             move16();
     628       34432 :             if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
     629             :             {
     630        8608 :                 pit_flag = i_subfr;
     631        8608 :                 move16();
     632             :             }
     633             : 
     634             :             /* pitch lag search limitation */
     635       34432 :             IF( i_subfr == 0 )
     636             :             {
     637        8608 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     638             :             }
     639             : 
     640             :             /* search and encode the closed loop pitch period */
     641       34432 :             test();
     642       34432 :             IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 5 ) )
     643             :             {
     644       26239 :                 *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_DOUBLEEXTEND_9b, PIT_FR1_DOUBLEEXTEND_9b, L_FRAME, L_SUBFR, Q_new );
     645       26239 :                 move16();
     646             :             }
     647        8193 :             ELSE IF( EQ_16( nBits, 10 ) )
     648             :             {
     649        8193 :                 *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR, Q_new );
     650        8193 :                 move16();
     651             :             }
     652             : 
     653       34432 :             pit_Q_enc_ivas_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     654             :         }
     655      503711 :         ELSE IF( EQ_16( tdm_Pitch_reuse_flag, 1 ) || EQ_16( nBits, 4 ) )
     656         140 :         {
     657             :             /*-------------------------------------------------------*
     658             :              *  Pitch encoding with reusing primary channel information
     659             :              *-------------------------------------------------------*/
     660             :             Word16 loc_T0, loc_frac;
     661             : 
     662         140 :             delta = 4;
     663         140 :             move16();
     664             : 
     665         140 :             pit_flag = L_SUBFR;
     666         140 :             move16();
     667             : 
     668         140 :             Word16 idx1 = shr( i_subfr, L_sufr_sft );
     669         140 :             Word16 idx2 = shr( add( i_subfr, 64 ), L_sufr_sft );
     670             : 
     671         140 :             IF( EQ_16( L_subfr, 2 * L_SUBFR ) )
     672             :             {
     673           0 :                 loc_T0 = add( shr( tdm_Pri_pitch_buf[idx1], 1 ), shr( tdm_Pri_pitch_buf[idx2], 1 ) ); // Q6
     674           0 :                 loc_frac = shr( sub( loc_T0, shl( shr( loc_T0, 6 ), 6 ) ), 4 );                       // Q2
     675           0 :                 loc_T0 = shr( loc_T0, 6 );                                                            // Q0
     676             :             }
     677             :             ELSE
     678             :             {
     679         140 :                 loc_T0 = tdm_Pri_pitch_buf[idx1];                               // Q6
     680         140 :                 loc_frac = shr( sub( loc_T0, shl( shr( loc_T0, 6 ), 6 ) ), 4 ); // Q2
     681         140 :                 loc_T0 = shr( loc_T0, 6 );                                      // Q0
     682             :             }
     683             : 
     684             :             /* pitch lag search limitation */
     685         140 :             limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     686         140 :             IF( nBits > 0 )
     687             :             {
     688             :                 /* search and encode the closed loop pitch period */
     689           0 :                 *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, Q_new );
     690           0 :                 move16();
     691           0 :                 IF( EQ_16( delta, 8 ) )
     692             :                 {
     693           0 :                     *T0_frac = 0;
     694           0 :                     move16();
     695             :                 }
     696           0 :                 pit_Q_enc_ivas_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     697             :             }
     698             :             ELSE
     699             :             {
     700         140 :                 *T0 = loc_T0;
     701         140 :                 move16();
     702         140 :                 *T0_frac = loc_frac;
     703         140 :                 move16();
     704             :             }
     705             :         }
     706             :         ELSE
     707             :         {
     708             :             /*-------------------------------------------------------*
     709             :              *  Pitch encoding in GENERIC coder type
     710             :              *  (both ACELP@12k8 and ACELP@16k cores)
     711             :              *-------------------------------------------------------*/
     712             : 
     713      503571 :             delta = 8;
     714      503571 :             move16();
     715             : 
     716             :             /* pitch lag search limitation */
     717      503571 :             IF( i_subfr == 0 )
     718             :             {
     719      111979 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     720             :             }
     721      391592 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
     722             :             {
     723      109329 :                 limit_T0_fx( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     724             :             }
     725             : 
     726             :             /* search and encode the closed loop pitch period */
     727      503571 :             IF( EQ_16( L_frame, L_FRAME ) )
     728             :             {
     729      181726 :                 test();
     730      181726 :                 test();
     731      181726 :                 IF( EQ_16( nBits, 8 ) || EQ_16( nBits, 5 ) )
     732             :                 {
     733       10394 :                     IF( *limit_flag == 0 )
     734             :                     {
     735        6931 :                         *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, Q_new );
     736        6931 :                         move16();
     737             :                     }
     738             :                     ELSE
     739             :                     {
     740        3463 :                         *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN_EXTEND, PIT_FR1_EXTEND_8b, L_FRAME, L_SUBFR, Q_new );
     741        3463 :                         move16();
     742             :                     }
     743             :                 }
     744      171332 :                 ELSE IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
     745             :                 {
     746      118733 :                     IF( *limit_flag == 0 )
     747             :                     {
     748       48590 :                         *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, Q_new );
     749       48590 :                         move16();
     750             :                     }
     751             :                     ELSE
     752             :                     {
     753       70143 :                         *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_EXTEND_9b, PIT_FR1_EXTEND_9b, L_FRAME, L_SUBFR, Q_new );
     754       70143 :                         move16();
     755             :                     }
     756             :                 }
     757       52599 :                 ELSE IF( EQ_16( nBits, 10 ) )
     758             :                 {
     759       52599 :                     *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR, Q_new );
     760             :                 }
     761             : 
     762      181726 :                 pit_Q_enc_ivas_fx( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     763             :             }
     764             :             ELSE /* L_frame == L_FRAME16k */
     765             :             {
     766      321845 :                 test();
     767      321845 :                 IF( EQ_16( nBits, 9 ) || EQ_16( nBits, 6 ) )
     768             :                 {
     769      191517 :                     *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, L_FRAME16k, L_SUBFR, Q_new );
     770      191517 :                     move16();
     771             :                 }
     772      130328 :                 ELSE IF( EQ_16( nBits, 10 ) )
     773             :                 {
     774      130328 :                     *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR, Q_new );
     775      130328 :                     move16();
     776             :                 }
     777             : 
     778      321845 :                 pit16k_Q_enc_ivas_fx( hBstr, nBits, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     779             :             }
     780             :         }
     781             :     }
     782             : 
     783             :     /*-------------------------------------------------------*
     784             :      *  Pitch encoding in AMR-WB IO mode
     785             :      *-------------------------------------------------------*/
     786             : 
     787             :     ELSE
     788             :     {
     789           0 :         delta = 8;
     790           0 :         move16();
     791           0 :         *limit_flag = 0;
     792           0 :         move16();
     793             : 
     794           0 :         IF( EQ_32( core_brate, ACELP_6k60 ) )
     795             :         {
     796           0 :             nBits = 5;
     797           0 :             move16();
     798             : 
     799             :             /* pitch lag search limitation */
     800           0 :             IF( i_subfr == 0 )
     801             :             {
     802           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
     803           0 :                 nBits = 8;
     804           0 :                 move16();
     805             :             }
     806             : 
     807           0 :             if ( EQ_16( i_subfr, 2 * L_SUBFR ) )
     808             :             {
     809             :                 /* rewrite pit_flag - it must not be zero */
     810           0 :                 pit_flag = i_subfr;
     811           0 :                 move16();
     812             :             }
     813             : 
     814             :             /* search and encode the closed loop pitch period */
     815           0 :             *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, Q_new );
     816           0 :             move16();
     817             :         }
     818           0 :         ELSE IF( EQ_32( core_brate, ACELP_8k85 ) )
     819             :         {
     820           0 :             nBits = 5;
     821           0 :             move16();
     822             : 
     823             :             /* pitch lag search limitation */
     824           0 :             IF( i_subfr == 0 )
     825             :             {
     826           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
     827           0 :                 nBits = 8;
     828           0 :                 move16();
     829             :             }
     830           0 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
     831             :             {
     832           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[1] ), 0, T0_min, T0_max );
     833           0 :                 nBits = 8;
     834           0 :                 move16();
     835             :             }
     836             : 
     837             :             /* search and encode the closed loop pitch period */
     838           0 :             *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR, Q_new );
     839           0 :             move16();
     840             :         }
     841             :         ELSE
     842             :         {
     843           0 :             nBits = 6;
     844           0 :             move16();
     845             : 
     846             :             /* pitch lag search limitation */
     847           0 :             IF( i_subfr == 0 )
     848             :             {
     849           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[0] ), 0, T0_min, T0_max );
     850           0 :                 nBits = 9;
     851           0 :                 move16();
     852             :             }
     853           0 :             ELSE IF( EQ_16( i_subfr, 2 * L_SUBFR ) )
     854             :             {
     855           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, *limit_flag, i_mult2( mult_Top, T_op[1] ), 0, T0_min, T0_max );
     856           0 :                 nBits = 9;
     857           0 :                 move16();
     858             :             }
     859             :             ELSE
     860             :             {
     861           0 :                 limit_T0_fx( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     862             :             }
     863             : 
     864             :             /* search and encode the closed loop pitch period */
     865           0 :             *T0 = pitch_fr4_ivas_fx( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR, Q_new );
     866           0 :             move16();
     867             :         }
     868             : 
     869           0 :         pit_Q_enc_ivas_fx( hBstr, 1, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     870             :     }
     871             : 
     872             :     /*-------------------------------------------------------*
     873             :      * Compute floating pitch output
     874             :      *-------------------------------------------------------*/
     875             : 
     876             :     /*pitch = (float)(*T0) + (float)(*T0_frac)/4.0f;*/   /* save subframe pitch values  */
     877      561603 :     pitch_cl = shl( add( shl( *T0, 2 ), *T0_frac ), 4 ); /* save subframe pitch values Q6  */
     878             : 
     879      561603 :     return pitch_cl;
     880             : }
     881             : 
     882             : /*-------------------------------------------------------------------*
     883             :  * abs_pit_enc()
     884             :  *
     885             :  * Encode pitch lag absolutely with resolution for shortest pitches
     886             :  * depending on parameter 'fr_step':
     887             :  * fr_step = 2: pitch range encoded with 8 bits
     888             :  * fr_step = 4: pitch range encoded with 8 bits
     889             :  *-------------------------------------------------------------------*/
     890             : 
     891       42804 : Word16 abs_pit_enc_fx(                          /* o  : pitch index                                              */
     892             :                        const Word16 fr_steps,   /* i  : fractional resolution steps (2 or 4) for shortest pitches*/
     893             :                        const Word16 limit_flag, /* i  : restrained(0) or extended(1) limits */
     894             :                        const Word16 T0,         /* i  : integer pitch lag                                        */
     895             :                        const Word16 T0_frac     /* i  : pitch fraction                                           */
     896             : )
     897             : {
     898       42804 :     Word16 pitch_index = 0;
     899             : 
     900       42804 :     IF( limit_flag == 0 )
     901             :     {
     902       31670 :         IF( EQ_16( fr_steps, 2 ) )
     903             :         {
     904             :             /*-----------------------------------------------------------------*
     905             :              * The pitch range is encoded absolutely with 8 bits
     906             :              * and is divided as follows:
     907             :              *   PIT_MIN to PIT_FR1_8b-1  resolution 1/2 (frac = 0 or 2)
     908             :              *   PIT_FR1_8b to PIT_MAX    resolution 1   (frac = 0)
     909             :              *-----------------------------------------------------------------*/
     910             : 
     911        3486 :             IF( LT_16( T0, PIT_FR1_8b ) )
     912             :             {
     913             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN*2);*/
     914        1853 :                 pitch_index = sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_MIN * 2 ) );
     915             :             }
     916             :             ELSE
     917             :             {
     918             :                 /*pitch_index = T0 - PIT_FR1_8b + ((PIT_FR1_8b-PIT_MIN)*2);*/
     919        1633 :                 pitch_index = add( sub( T0, PIT_FR1_8b ), ( ( PIT_FR1_8b - PIT_MIN ) * 2 ) );
     920             :             }
     921             :         }
     922       28184 :         ELSE IF( EQ_16( fr_steps, 4 ) )
     923             :         {
     924             :             /*-------------------------------------------------------------------*
     925             :              * The pitch range is encoded absolutely with 9 bits
     926             :              * and is divided as follows:
     927             :              *   PIT_MIN    to PIT_FR2_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     928             :              *   PIT_FR2_9b to PIT_FR1_9b-1  resolution 1/2 (frac = 0 or 2)
     929             :              *   PIT_FR1_9b to PIT_MAX       resolution 1   (frac = 0)
     930             :              *-------------------------------------------------------------------*/
     931             : 
     932       28184 :             IF( LT_16( T0, PIT_FR2_9b ) )
     933             :             {
     934             :                 /*pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
     935       22882 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN * 4 ) ) );
     936             :             }
     937        5302 :             ELSE IF( LT_16( T0, PIT_FR1_9b ) )
     938             :             {
     939             :                 /*  pitch_index = T0*2 + (T0_frac>>1) - (PIT_FR2_9b*2) + ((PIT_FR2_9b-PIT_MIN)*4);*/
     940        2561 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_FR2_9b * 2 ) ), ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) );
     941             :             }
     942             :             ELSE
     943             :             {
     944             :                 /* pitch_index = T0 - PIT_FR1_9b + ((PIT_FR2_9b-PIT_MIN)*4) + ((PIT_FR1_9b-PIT_FR2_9b)*2);*/
     945        2741 :                 pitch_index = add( add( sub( T0, PIT_FR1_9b ), ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) ), ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 ) );
     946             :             }
     947             :         }
     948             :         ELSE /* fr_step == 0 */
     949             :         {
     950             :             /* not used in the codec */
     951           0 :             pitch_index = 0;
     952           0 :             move16();
     953             :         }
     954             :     }
     955       11134 :     ELSE IF( EQ_16( limit_flag, 1 ) ) /* extended Q range */
     956             :     {
     957       10719 :         IF( EQ_16( fr_steps, 2 ) )
     958             :         {
     959             :             /*-----------------------------------------------------------------*
     960             :              * The pitch range is encoded absolutely with 8 bits
     961             :              * and is divided as follows:
     962             :              *   PIT_MIN_EXTEND to PIT_FR1_EXTEND_8b-1  resolution 1/2 (frac = 0 or 2)
     963             :              *   PIT_FR1_EXTEND_8b to PIT_MAX_EXTEND    resolution 1   (frac = 0)
     964             :              *-----------------------------------------------------------------*/
     965             : 
     966         992 :             IF( LT_16( T0, PIT_FR1_EXTEND_8b ) )
     967             :             {
     968             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN_EXTEND*2);*/
     969         488 :                 pitch_index = sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_MIN_EXTEND * 2 ) );
     970             :             }
     971             :             ELSE
     972             :             {
     973             :                 /*pitch_index = T0 - PIT_FR1_EXTEND_8b + ((PIT_FR1_EXTEND_8b-PIT_MIN_EXTEND)*2);*/
     974         504 :                 pitch_index = add( sub( T0, PIT_FR1_EXTEND_8b ), ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 ) );
     975             :             }
     976             :         }
     977        9727 :         ELSE IF( EQ_16( fr_steps, 4 ) )
     978             :         {
     979             :             /*-------------------------------------------------------------------*
     980             :              * The pitch range is encoded absolutely with 9 bits
     981             :              * and is divided as follows:
     982             :              *   PIT_MIN_EXTEND    to PIT_FR2__EXTEND9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     983             :              *   PIT_FR2_EXTEND_9b to PIT_FR1__EXTEND9b-1  resolution 1/2 (frac = 0 or 2)
     984             :              *   PIT_FR1_EXTEND_9b to PIT_MAX_EXTEND       resolution 1   (frac = 0)
     985             :              *-------------------------------------------------------------------*/
     986             : 
     987        9727 :             IF( LT_16( T0, PIT_FR2_EXTEND_9b ) )
     988             :             {
     989             :                 /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
     990        8547 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN_EXTEND * 4 ) ) );
     991             :             }
     992        1180 :             ELSE IF( T0 < PIT_FR1_EXTEND_9b )
     993             :             {
     994             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_FR2_EXTEND_9b*2) + ((PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4);*/
     995         235 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_FR2_EXTEND_9b * 2 ) ), ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) );
     996             :             }
     997             :             ELSE
     998             :             {
     999             :                 /*pitch_index = T0 - PIT_FR1_EXTEND_9b + ((PIT_FR2_EXTEND_9b-PIT_MIN_EXTEND)*4) + ((PIT_FR1_EXTEND_9b-PIT_FR2_EXTEND_9b)*2);*/
    1000         945 :                 pitch_index = add( add( sub( T0, PIT_FR1_EXTEND_9b ), ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) ), ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 ) );
    1001             :             }
    1002             :         }
    1003             :         ELSE /* fr_step == 0 */
    1004             :         {
    1005             :             /* not used in the codec */
    1006           0 :             pitch_index = 0;
    1007           0 :             move16();
    1008             :         }
    1009             :     }
    1010             :     ELSE /* double-extended Q range */
    1011             :     {
    1012         415 :         IF( EQ_16( fr_steps, 2 ) )
    1013             :         {
    1014             :             /*-----------------------------------------------------------------*
    1015             :              * The pitch range is encoded absolutely with 8 bits
    1016             :              * and is divided as follows:
    1017             :              *   PIT_MIN_DOUBLEEXTEND    to PIT_FR1_DOUBLEEXTEND_8b-1  resolution 1/2 (frac = 0 or 2)
    1018             :              *   PIT_FR1_DOUBLEEXTEND_8b to PIT_MAX_EXTEND             resolution 1   (frac = 0)
    1019             :              *-----------------------------------------------------------------*/
    1020             : 
    1021           0 :             IF( LT_16( T0, PIT_FR1_DOUBLEEXTEND_8b ) )
    1022             :             {
    1023             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_MIN_DOUBLEEXTEND*2);*/
    1024           0 :                 pitch_index = sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_MIN_DOUBLEEXTEND * 2 ) );
    1025             :             }
    1026             :             ELSE
    1027             :             {
    1028             :                 /*pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_8b + ((PIT_FR1_DOUBLEEXTEND_8b-PIT_MIN_DOUBLEEXTEND)*2);               */
    1029           0 :                 pitch_index = add( sub( T0, PIT_FR1_DOUBLEEXTEND_8b ), ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 ) );
    1030             :             }
    1031             :         }
    1032         415 :         ELSE IF( EQ_16( fr_steps, 4 ) )
    1033             :         {
    1034             :             /*-------------------------------------------------------------------*
    1035             :              * The pitch range is encoded absolutely with 9 bits
    1036             :              * and is divided as follows:
    1037             :              *   PIT_MIN_DOUBLEEXTEND    to PIT_FR2_DOUBLEEXTEND9b-1  resolution 1/4 (frac = 0,1,2 or 3)
    1038             :              *   PIT_FR2_DOUBLEEXTEND_9b to PIT_FR1_DOOBLEEXTEND9b-1  resolution 1/2 (frac = 0 or 2)
    1039             :              *   PIT_FR1_DOUBLEEXTEND_9b to PIT_MAX_EXTEND           resolution 1   (frac = 0)
    1040             :              *-------------------------------------------------------------------*/
    1041             : 
    1042         415 :             IF( LT_16( T0, PIT_FR2_DOUBLEEXTEND_9b ) )
    1043             :             {
    1044             :                 /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
    1045         389 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT_MIN_DOUBLEEXTEND * 4 ) ) );
    1046             :             }
    1047          26 :             ELSE IF( LT_16( T0, PIT_FR1_DOUBLEEXTEND_9b ) )
    1048             :             {
    1049             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT_FR2_DOUBLEEXTEND_9b*2) + ((PIT_FR2_DOUBLEEXTEND_9b-PIT_MIN_DOUBLEEXTEND)*4);*/
    1050           8 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT_FR2_DOUBLEEXTEND_9b * 2 ) ), ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) );
    1051             :             }
    1052             :             ELSE
    1053             :             {
    1054             :                 /*pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_9b + ((PIT_FR2_DOUBLEEXTEND_9b-PIT_MIN_DOUBLEEXTEND)*4) + ((PIT_FR1_DOUBLEEXTEND_9b-PIT_FR2_DOUBLEEXTEND_9b)*2);*/
    1055          18 :                 pitch_index = add( add( sub( T0, PIT_FR1_DOUBLEEXTEND_9b ), ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) ), ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 ) );
    1056             :             }
    1057             :         }
    1058             :         ELSE /* fr_step == 0 */
    1059             :         {
    1060             :             /* not used in the codec */
    1061           0 :             pitch_index = 0;
    1062           0 :             move16();
    1063             :         }
    1064             :     }
    1065             : 
    1066       42804 :     return pitch_index;
    1067             : }
    1068             : 
    1069             : 
    1070             : /*-------------------------------------------------------------------*
    1071             :  * delta_pit_enc:
    1072             :  *
    1073             :  * Encode pitch lag differentially from T0_min to T0_max
    1074             :  * with resolution depending on parameter 'fr_step':
    1075             :  * fr_step = 0: resolusion 1   (frac = 0), or
    1076             :  * fr_step = 2: resolusion 1/2 (frac = 0 or 2), or
    1077             :  * fr_step = 4: resolution 1/4 (frac = 0, 1, 2, or 3)
    1078             :  *-------------------------------------------------------------------*/
    1079             : 
    1080      147517 : Word16 delta_pit_enc_fx(                        /* o  : pitch index                         */
    1081             :                          const Word16 fr_steps, /* i  : fractional resolution steps (2 or 4)*/
    1082             :                          const Word16 T0,       /* i  : integer pitch lag                   */
    1083             :                          const Word16 T0_frac,  /* i  : pitch fraction                      */
    1084             :                          const Word16 T0_min    /* i  : delta search min                    */
    1085             : )
    1086             : {
    1087      147517 :     Word16 pitch_index = 0;
    1088      147517 :     move16();
    1089             : 
    1090      147517 :     IF( fr_steps == 0 )
    1091             :     {
    1092         196 :         pitch_index = sub( T0, T0_min );
    1093             :     }
    1094      147321 :     ELSE IF( EQ_16( fr_steps, 2 ) )
    1095             :     {
    1096             :         /* pitch_index = (T0 - T0_min) * 2 + (T0_frac>>1);*/
    1097        9267 :         pitch_index = add( shl( sub( T0, T0_min ), 1 ), shr( T0_frac, 1 ) );
    1098             :     }
    1099      138054 :     ELSE IF( EQ_16( fr_steps, 4 ) )
    1100             :     {
    1101             :         /*pitch_index = (T0 - T0_min) * 4 + T0_frac;*/
    1102      138054 :         pitch_index = add( shl( sub( T0, T0_min ), 2 ), T0_frac );
    1103             :     }
    1104             : 
    1105      147517 :     return pitch_index;
    1106             : }
    1107             : 
    1108             : /*-------------------------------------------------------------------*
    1109             :  * pitch_fr4()
    1110             :  *
    1111             :  * Find the closed loop pitch period with 1/4 subsample resolution.
    1112             :  *-------------------------------------------------------------------*/
    1113      561463 : Word16 pitch_fr4_ivas_fx(                          /* o  : chosen integer pitch lag                                       */
    1114             :                           const Word16 exc[],      /* i  : excitation buffer                          Q_new               */
    1115             :                           const Word16 xn[],       /* i  : target signal                              Q_new-1             */
    1116             :                           const Word16 h[],        /* i  : weighted synthesis filter impulse response Q(14 - norm_s[h[0]) */
    1117             :                           const Word16 t0_min,     /* i  : minimum value in the searched range.       Q0                  */
    1118             :                           const Word16 t0_max,     /* i  : maximum value in the searched range.       Q0                  */
    1119             :                           Word16 *pit_frac,        /* o  : chosen fraction (0, 1, 2 or 3)                                 */
    1120             :                           const Word16 i_subfr,    /* i  : flag to first subframe                                         */
    1121             :                           const Word16 limit_flag, /* i  : flag for limits (0=restrained, 1=extended)                     */
    1122             :                           const Word16 t0_fr2,     /* i  : minimum value for resolution 1/2                               */
    1123             :                           const Word16 t0_fr1,     /* i  : minimum value for resolution 1                                 */
    1124             :                           const Word16 L_frame,    /* i  : length of the frame                                            */
    1125             :                           const Word16 L_subfr,    /* i  : size of subframe                                               */
    1126             :                           Word16 Q_new             /* i                                                                   */
    1127             : )
    1128             : {
    1129             :     Word16 i;
    1130             :     Word16 t_min, t_max;
    1131             :     Word16 max_val, t0, t1, fraction, step, temp;
    1132             :     Word16 *corr;
    1133             :     Word16 corr_v[15 + 2 * L_INTERPOL1 + 1]; /* Total length = t0_max-t0_min+1+2*L_inter */
    1134             :     Word16 pit_min;
    1135             :     Word16 cor_max;
    1136             : 
    1137             :     /* initialization */
    1138      561463 :     IF( limit_flag == 0 )
    1139             :     {
    1140       55521 :         IF( EQ_16( L_frame, L_FRAME ) )
    1141             :         {
    1142       55521 :             pit_min = PIT_MIN;
    1143       55521 :             move16();
    1144             :         }
    1145             :         ELSE /* L_frame == L_FRAME16k */
    1146             :         {
    1147           0 :             pit_min = PIT16k_MIN;
    1148           0 :             move16();
    1149             :         }
    1150             :     }
    1151             :     ELSE
    1152             :     {
    1153      505942 :         IF( EQ_16( L_frame, L_FRAME ) )
    1154             :         {
    1155      184097 :             pit_min = PIT_MIN_EXTEND;
    1156      184097 :             move16();
    1157      184097 :             IF( EQ_16( limit_flag, 2 ) )
    1158             :             {
    1159       34432 :                 pit_min = PIT_MIN_DOUBLEEXTEND;
    1160       34432 :                 move16();
    1161             :             }
    1162             :         }
    1163             :         ELSE /* L_frame == L_FRAME16k */
    1164             :         {
    1165      321845 :             pit_min = PIT16k_MIN_EXTEND;
    1166      321845 :             move16();
    1167             :         }
    1168             :     }
    1169             : 
    1170             :     /*-----------------------------------------------------------------*
    1171             :      * - Find interval to compute normalized correlation
    1172             :      * - allocate memory to normalized correlation vector
    1173             :      * - Compute normalized correlation between target and filtered
    1174             :      *   excitation
    1175             :      *-----------------------------------------------------------------*/
    1176             : 
    1177      561463 :     t_min = sub( t0_min, L_INTERPOL1 );
    1178      561463 :     t_max = add( t0_max, L_INTERPOL1 );
    1179      561463 :     corr = &corr_v[-t_min];
    1180      561463 :     move16();
    1181      561463 :     move16(); /* corr[t_min..t_max] */
    1182             : 
    1183      561463 :     norm_corr_ivas_fx( exc, xn, h, t_min, t_max, corr, L_subfr, Q_new );
    1184             : 
    1185             :     /*-----------------------------------------------------------------*
    1186             :      * Find integer pitch
    1187             :      *-----------------------------------------------------------------*/
    1188             : 
    1189      561463 :     max_val = corr[t0_min];
    1190      561463 :     move16();
    1191      561463 :     t0 = t0_min;
    1192      561463 :     move16();
    1193             : 
    1194     8520272 :     FOR( i = add( t0_min, 1 ); i <= t0_max; i++ )
    1195             :     {
    1196     7958809 :         if ( GE_16( corr[i], max_val ) )
    1197             :         {
    1198     3123169 :             t0 = i;
    1199     3123169 :             move16();
    1200             :         }
    1201     7958809 :         max_val = s_max( corr[i], max_val );
    1202             :     }
    1203             : 
    1204      561463 :     IF( EQ_16( t0_fr1, pit_min ) )
    1205             :     {
    1206             :         /* don't search fraction (for 7b/4b quant) */
    1207           0 :         test();
    1208           0 :         IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) )
    1209             :         {
    1210           0 :             i = shl( shr( t0, 1 ), 1 ); /* 2 samples resolution */
    1211           0 :             if ( GT_16( add( i, 2 ), PIT_MAX ) )
    1212             :             {
    1213           0 :                 i = sub( i, 2 );
    1214             :             }
    1215           0 :             IF( GT_16( corr[i], corr[i + 2] ) )
    1216             :             {
    1217           0 :                 t0 = i;
    1218           0 :                 move16();
    1219             :             }
    1220             :             ELSE
    1221             :             {
    1222           0 :                 t0 = add( i, 2 );
    1223             :             }
    1224             :         }
    1225           0 :         *pit_frac = 0;
    1226           0 :         move16();
    1227             : 
    1228           0 :         return ( t0 );
    1229             :     }
    1230             : 
    1231      561463 :     test();
    1232      561463 :     IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr1 ) ) )
    1233             :     {
    1234        6205 :         *pit_frac = 0;
    1235        6205 :         move16();
    1236             : 
    1237        6205 :         return ( t0 );
    1238             :     }
    1239             : 
    1240             :     /*------------------------------------------------------------------*
    1241             :      * Search fractionnal pitch with 1/4 subsample resolution.
    1242             :      * search the fractions around t0 and choose the one which maximizes
    1243             :      * the interpolated normalized correlation.
    1244             :      *-----------------------------------------------------------------*/
    1245             : 
    1246      555258 :     t1 = t0;
    1247      555258 :     move16();
    1248      555258 :     step = 1;
    1249      555258 :     move16(); /* 1/4 subsample resolution */
    1250      555258 :     fraction = 1;
    1251      555258 :     move16();
    1252      555258 :     test();
    1253      555258 :     test();
    1254      555258 :     IF( ( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) ) || ( EQ_16( t0_fr2, pit_min ) ) )
    1255             :     {
    1256       13830 :         step = 2;
    1257       13830 :         move16(); /* 1/2 subsample resolution */
    1258       13830 :         fraction = 2;
    1259       13830 :         move16();
    1260             :     }
    1261             : 
    1262      555258 :     IF( EQ_16( t0, t0_min ) ) /* Limit case */
    1263             :     {
    1264       33434 :         fraction = 0;
    1265       33434 :         move16();
    1266       33434 :         cor_max = Interpol_4( &corr[t0], fraction );
    1267             :     }
    1268             :     ELSE
    1269             :     {
    1270      521824 :         t0 = sub( t0, 1 );
    1271      521824 :         cor_max = Interpol_4( &corr[t0], fraction );
    1272     1540052 :         FOR( i = add( fraction, step ); i <= 3; i = (Word16) ( i + step ) )
    1273             :         {
    1274     1018228 :             temp = Interpol_4( &corr[t0], i );
    1275     1018228 :             IF( GT_16( temp, cor_max ) )
    1276             :             {
    1277      949696 :                 cor_max = temp;
    1278      949696 :                 move16();
    1279      949696 :                 fraction = i;
    1280      949696 :                 move16();
    1281             :             }
    1282             :         }
    1283             :     }
    1284             : 
    1285     2748630 :     FOR( i = 0; i <= 3; i = (Word16) ( i + step ) )
    1286             :     {
    1287     2193372 :         temp = Interpol_4( &corr[t1], i );
    1288     2193372 :         IF( GT_16( temp, cor_max ) )
    1289             :         {
    1290      649136 :             cor_max = temp;
    1291      649136 :             move16();
    1292      649136 :             fraction = i;
    1293      649136 :             move16();
    1294      649136 :             t0 = t1;
    1295      649136 :             move16();
    1296             :         }
    1297             :     }
    1298             : 
    1299      555258 :     *pit_frac = fraction;
    1300      555258 :     move16();
    1301             : 
    1302      555258 :     return ( t0 );
    1303             : }
    1304             : 
    1305       47203 : Word16 pitch_fr4_fx(                          /* o  : chosen integer pitch lag                 */
    1306             :                      const Word16 exc[],      /* i  : excitation buffer                          Q_new*/
    1307             :                      const Word16 xn[],       /* i  : target signal                              Q_new-1+shift*/
    1308             :                      const Word16 h[],        /* i  : weighted synthesis filter impulse response Q(14+shift)*/
    1309             :                      const Word16 t0_min,     /* i  : minimum value in the searched range.       Q0*/
    1310             :                      const Word16 t0_max,     /* i  : maximum value in the searched range.       Q0*/
    1311             :                      Word16 *pit_frac,        /* o  : chosen fraction (0, 1, 2 or 3)             */
    1312             :                      const Word16 i_subfr,    /* i  : flag to first subframe                     */
    1313             :                      const Word16 limit_flag, /* i  : flag for limits (0=restrained, 1=extended) */
    1314             :                      const Word16 t0_fr2,     /* i  : minimum value for resolution 1/2           */
    1315             :                      const Word16 t0_fr1,     /* i  : minimum value for resolution 1             */
    1316             :                      const Word16 L_frame,    /* i  : length of the frame                        */
    1317             :                      const Word16 L_subfr     /* i  : size of subframe                           */
    1318             : )
    1319             : {
    1320             :     Word16 i;
    1321             :     Word16 t_min, t_max;
    1322             :     Word16 max_val, t0, t1, fraction, step, temp;
    1323             :     Word16 *corr;
    1324             :     Word16 corr_v[15 + 2 * L_INTERPOL1 + 1]; /* Total length = t0_max-t0_min+1+2*L_inter */
    1325             :     Word16 pit_min;
    1326             :     Word16 cor_max;
    1327             : 
    1328             :     /* initialization */
    1329       47203 :     IF( limit_flag == 0 )
    1330             :     {
    1331       26091 :         IF( EQ_16( L_frame, L_FRAME ) )
    1332             :         {
    1333       19512 :             pit_min = PIT_MIN;
    1334       19512 :             move16();
    1335             :         }
    1336             :         ELSE /* L_frame == L_FRAME16k */
    1337             :         {
    1338        6579 :             pit_min = PIT16k_MIN;
    1339        6579 :             move16();
    1340             :         }
    1341             :     }
    1342             :     ELSE
    1343             :     {
    1344       21112 :         IF( EQ_16( L_frame, L_FRAME ) )
    1345             :         {
    1346        2069 :             pit_min = PIT_MIN_EXTEND;
    1347        2069 :             move16();
    1348        2069 :             IF( EQ_16( limit_flag, 2 ) )
    1349             :             {
    1350         848 :                 pit_min = PIT_MIN_DOUBLEEXTEND;
    1351         848 :                 move16();
    1352             :             }
    1353             :         }
    1354             :         ELSE /* L_frame == L_FRAME16k */
    1355             :         {
    1356       19043 :             pit_min = PIT16k_MIN_EXTEND;
    1357       19043 :             move16();
    1358             :         }
    1359             :     }
    1360             : 
    1361             :     /*-----------------------------------------------------------------*
    1362             :      * - Find interval to compute normalized correlation
    1363             :      * - allocate memory to normalized correlation vector
    1364             :      * - Compute normalized correlation between target and filtered
    1365             :      *   excitation
    1366             :      *-----------------------------------------------------------------*/
    1367             : 
    1368       47203 :     t_min = sub( t0_min, L_INTERPOL1 );
    1369       47203 :     t_max = add( t0_max, L_INTERPOL1 );
    1370       47203 :     corr = &corr_v[-t_min];
    1371             :     /* corr[t_min..t_max] */
    1372             : 
    1373       47203 :     norm_corr_fx( exc, xn, h, t_min, t_max, corr, L_subfr );
    1374             : 
    1375             :     /*-----------------------------------------------------------------*
    1376             :      * Find integer pitch
    1377             :      *-----------------------------------------------------------------*/
    1378             : 
    1379       47203 :     max_val = corr[t0_min];
    1380       47203 :     move16();
    1381       47203 :     t0 = t0_min;
    1382       47203 :     move16();
    1383             : 
    1384      748352 :     FOR( i = t0_min + 1; i <= t0_max; i++ )
    1385             :     {
    1386      701149 :         if ( GE_16( corr[i], max_val ) )
    1387             :         {
    1388      279485 :             t0 = i;
    1389      279485 :             move16();
    1390             :         }
    1391      701149 :         max_val = s_max( corr[i], max_val );
    1392             :     }
    1393             : 
    1394       47203 :     IF( EQ_16( t0_fr1, pit_min ) )
    1395             :     {
    1396             :         /* don't search fraction (for 7b/4b quant) */
    1397         576 :         test();
    1398         576 :         IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) )
    1399             :         {
    1400           0 :             i = shl( shr( t0, 1 ), 1 ); /* 2 samples resolution */
    1401           0 :             IF( GT_16( add( i, 2 ), PIT_MAX ) )
    1402             :             {
    1403           0 :                 i = sub( i, 2 );
    1404             :             }
    1405           0 :             IF( GT_16( corr[i], corr[i + 2] ) )
    1406             :             {
    1407           0 :                 t0 = i;
    1408           0 :                 move16();
    1409             :             }
    1410             :             ELSE
    1411             :             {
    1412           0 :                 t0 = add( i, 2 );
    1413             :             }
    1414             :         }
    1415         576 :         *pit_frac = 0;
    1416         576 :         move16();
    1417             : 
    1418         576 :         return ( t0 );
    1419             :     }
    1420             : 
    1421       46627 :     test();
    1422       46627 :     IF( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr1 ) ) )
    1423             :     {
    1424        5266 :         *pit_frac = 0;
    1425        5266 :         move16();
    1426             : 
    1427        5266 :         return ( t0 );
    1428             :     }
    1429             : 
    1430             :     /*------------------------------------------------------------------*
    1431             :      * Search fractionnal pitch with 1/4 subsample resolution.
    1432             :      * search the fractions around t0 and choose the one which maximizes
    1433             :      * the interpolated normalized correlation.
    1434             :      *-----------------------------------------------------------------*/
    1435             : 
    1436       41361 :     t1 = t0;
    1437       41361 :     move16();
    1438       41361 :     step = 1;
    1439       41361 :     move16(); /* 1/4 subsample resolution */
    1440       41361 :     fraction = 1;
    1441       41361 :     move16();
    1442       41361 :     test();
    1443       41361 :     test();
    1444       41361 :     IF( ( ( i_subfr == 0 ) && ( GE_16( t0, t0_fr2 ) ) ) || ( EQ_16( t0_fr2, pit_min ) ) )
    1445             :     {
    1446        7662 :         step = 2;
    1447        7662 :         move16(); /* 1/2 subsample resolution */
    1448        7662 :         fraction = 2;
    1449        7662 :         move16();
    1450             :     }
    1451             : 
    1452       41361 :     IF( EQ_16( t0, t0_min ) ) /* Limit case */
    1453             :     {
    1454        3196 :         fraction = 0;
    1455        3196 :         move16();
    1456        3196 :         cor_max = Interpol_4( &corr[t0], fraction );
    1457             :     }
    1458             :     ELSE
    1459             :     {
    1460       38165 :         t0 = sub( t0, 1 );
    1461       38165 :         cor_max = Interpol_4( &corr[t0], fraction );
    1462      100661 :         FOR( i = fraction + step; i <= 3; i = ( i + step ) )
    1463             :         {
    1464       62496 :             temp = Interpol_4( &corr[t0], i );
    1465       62496 :             IF( GT_16( temp, cor_max ) )
    1466             :             {
    1467       57919 :                 cor_max = temp;
    1468       57919 :                 move16();
    1469       57919 :                 fraction = i;
    1470       57919 :                 move16();
    1471             :             }
    1472             :         }
    1473             :     }
    1474             : 
    1475      191481 :     FOR( i = 0; i <= 3; i = ( i + step ) )
    1476             :     {
    1477      150120 :         temp = Interpol_4( &corr[t1], i );
    1478      150120 :         IF( GT_16( temp, cor_max ) )
    1479             :         {
    1480       46246 :             cor_max = temp;
    1481       46246 :             move16();
    1482       46246 :             fraction = i;
    1483       46246 :             move16();
    1484       46246 :             t0 = t1;
    1485       46246 :             move16();
    1486             :         }
    1487             :     }
    1488             : 
    1489       41361 :     *pit_frac = fraction;
    1490       41361 :     move16();
    1491             : 
    1492       41361 :     return ( t0 );
    1493             : }
    1494             : 
    1495             : /*-------------------------------------------------------------------*
    1496             :  * norm_corr()
    1497             :  *
    1498             :  * Find the normalized correlation between the target vector and the
    1499             :  * filtered past excitation (correlation between target and filtered
    1500             :  * excitation divided by the square root of energy of filtered
    1501             :  * excitation)
    1502             :  *---------------------------------------------------------------------*/
    1503      561463 : void norm_corr_ivas_fx(
    1504             :     const Word16 exc[],   /* i  : excitation buffer                          Q_new                */
    1505             :     const Word16 xn[],    /* i  : target signal                              Q_new-1              */
    1506             :     const Word16 h[],     /* i  : weighted synthesis filter impulse response Q(14 - norm_s(h[0])) */
    1507             :     const Word16 t_min,   /* i  : minimum value of searched range                                 */
    1508             :     const Word16 t_max,   /* i  : maximum value of searched range                                 */
    1509             :     Word16 ncorr[],       /* o  : normalized correlation                                          */
    1510             :     const Word16 L_subfr, /* i  : subframe size                                                   */
    1511             :     Word16 Q_new          /* i                                                                    */
    1512             : )
    1513             : {
    1514             :     Word16 i, k, t;
    1515             :     Word16 corr, exp_corr, norm, exp_norm, exp;
    1516             :     Word16 excf[L_FRAME16k];
    1517             :     Word16 ncorr_e[15 + 2 * L_INTERPOL1 + 1];
    1518             :     Word16 h_e, e_max;
    1519             :     Word32 L_tmp;
    1520             :     Word64 W_tmp;
    1521             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1522      561463 :     Flag Overflow = 0;
    1523      561463 :     move16();
    1524             : #endif
    1525             : 
    1526      561463 :     k = negate( t_min );
    1527      561463 :     h_e = add( 1, norm_s( h[0] ) ); // exponent of h
    1528             :     /*-----------------------------------------------------------------*
    1529             :      * compute the filtered excitation for the first delay t_min
    1530             :      *-----------------------------------------------------------------*/
    1531             : 
    1532      561463 :     conv_fx( &exc[k], h, excf, L_subfr ); // Q_new - h_e
    1533             : 
    1534             :     /*----------------------------------------------------------------*
    1535             :      * loop for every possible period
    1536             :      *----------------------------------------------------------------*/
    1537             : 
    1538    13573439 :     FOR( t = t_min; t <= t_max; t++ )
    1539             :     {
    1540             :         /* Compute correlation between xn[] and excf[] */
    1541             : 
    1542    13011976 :         L_tmp = L_mac( 0, xn[0], excf[0] );
    1543   856670720 :         FOR( i = 1; i < L_subfr; i++ )
    1544             :         {
    1545   843658744 :             L_tmp = L_mac_o( L_tmp, xn[i], excf[i], &Overflow ); // (Q_new - 1) + (Q_new - h_e) + 1
    1546             :         }
    1547    13011976 :         exp = norm_l( L_tmp );
    1548    13011976 :         L_tmp = L_shl( L_tmp, exp );
    1549    13011976 :         exp_corr = sub( 31, add( exp, ( Q_new - 1 ) + ( Q_new - h_e ) + 1 ) );
    1550    13011976 :         corr = extract_h( L_tmp );
    1551             : 
    1552             :         /* Compute 1/sqrt(energy of excf[]) */
    1553    13011976 :         Word16 _0_01 = extract_l( L_shr( 21474836 /* 0.01 in Q31 */, sub( 31, add( shl( sub( Q_new, h_e ), 1 ), 1 ) ) ) );
    1554             : 
    1555    13011976 :         W_tmp = W_mac_16_16( _0_01, excf[0], excf[0] );
    1556   856670720 :         FOR( i = 1; i < L_subfr; i++ )
    1557             :         {
    1558   843658744 :             W_tmp = W_mac_16_16( W_tmp, excf[i], excf[i] ); // 2*(Q_new - h_e) + 1
    1559             :         }
    1560             : 
    1561    13011976 :         exp = W_norm( W_tmp );
    1562    13011976 :         L_tmp = W_extract_h( W_shl( W_tmp, exp ) ); // 2*(Q_new - h_e) + 1 + exp - 32
    1563    13011976 :         exp_norm = sub( 31 + 32, add( add( shl( sub( Q_new, h_e ), 1 ), 1 ), exp ) );
    1564             : 
    1565    13011976 :         L_tmp = Isqrt_lc( L_tmp, &exp_norm );
    1566    13011976 :         norm = extract_h( L_tmp );
    1567             : 
    1568             :         /* Normalize correlation = correlation * (1/sqrt(energy)) */
    1569    13011976 :         L_tmp = L_mult( corr, norm );
    1570             : 
    1571    13011976 :         ncorr[t] = round_fx_sat( L_tmp );
    1572    13011976 :         ncorr_e[t - t_min] = add( exp_corr, exp_norm );
    1573    13011976 :         move16();
    1574    13011976 :         move16();
    1575             : 
    1576             :         /* update the filtered excitation excf[] for the next iteration */
    1577    13011976 :         IF( NE_16( t, t_max ) )
    1578             :         {
    1579    12450513 :             k--;
    1580   819243072 :             FOR( i = (Word16) ( L_subfr - 1 ); i > 0; i-- )
    1581             :             {
    1582             :                 /* saturation can occur in add() */
    1583   806792559 :                 excf[i] = round_fx_sat( L_mac_sat( L_mult( excf[i - 1], 32767 ), exc[k], h[i] ) );
    1584   806792559 :                 move16();
    1585             :             }
    1586    12450513 :             excf[0] = mult_r( exc[k], h[0] );
    1587    12450513 :             move16();
    1588             :         }
    1589             :     }
    1590             :     // Aligning the values of ncorr to a common exponent
    1591      561463 :     maximum_fx( ncorr_e, t_max + 1 - t_min, &e_max );
    1592    13573439 :     FOR( t = t_min; t <= t_max; t++ )
    1593             :     {
    1594    13011976 :         ncorr[t] = shr( ncorr[t], sub( e_max, ncorr_e[t - t_min] ) );
    1595    13011976 :         move16();
    1596             :     }
    1597             : 
    1598      561463 :     return;
    1599             : }
    1600             : 
    1601             : 
    1602       47203 : void norm_corr_fx(
    1603             :     const Word16 exc[],  /* i  : excitation buffer                          Q_new*/
    1604             :     const Word16 xn[],   /* i  : target signal                              Q_new-1+shift*/
    1605             :     const Word16 h[],    /* i  : weighted synthesis filter impulse response Q(14+shift)*/
    1606             :     const Word16 t_min,  /* i  : minimum value of searched range            */
    1607             :     const Word16 t_max,  /* i  : maximum value of searched range            */
    1608             :     Word16 ncorr[],      /* o  : normalized correlation                     Q15 */
    1609             :     const Word16 L_subfr /* i  : subframe size                              */
    1610             : )
    1611             : {
    1612             :     Word16 i, k, t;
    1613             :     Word16 corr, exp_corr, norm, exp_norm, exp, scale;
    1614             :     Word16 excf[L_FRAME16k];
    1615             :     Word32 L_tmp;
    1616             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
    1617       47203 :     Flag Overflow = 0;
    1618       47203 :     move32();
    1619             : #endif
    1620             : 
    1621       47203 :     k = negate( t_min );
    1622             : 
    1623             :     /*-----------------------------------------------------------------*
    1624             :      * compute the filtered excitation for the first delay t_min
    1625             :      *-----------------------------------------------------------------*/
    1626             : 
    1627       47203 :     conv_fx( &exc[k], h, excf, L_subfr );
    1628             : 
    1629             :     /* Compute rounded down 1/sqrt(energy of xn[]) */
    1630       47203 :     L_tmp = L_mac_o( 1, xn[0], xn[0], &Overflow );
    1631     3021888 :     FOR( i = 1; i < L_subfr; i++ )
    1632             :     {
    1633     2974685 :         L_tmp = L_mac_o( L_tmp, xn[i], xn[i], &Overflow );
    1634             :     }
    1635       47203 :     exp = norm_l( L_tmp );
    1636       47203 :     exp = sub( 30, exp );
    1637             : 
    1638       47203 :     exp = add( exp, 2 );             /* energy of xn[] x 2 + rounded up     */
    1639       47203 :     scale = negate( shr( exp, 1 ) ); /* (1<<scale) < 1/sqrt(energy rounded) */
    1640             : 
    1641             :     /*----------------------------------------------------------------*
    1642             :      * loop for every possible period
    1643             :      *----------------------------------------------------------------*/
    1644             : 
    1645     1173179 :     FOR( t = t_min; t <= t_max; t++ )
    1646             :     {
    1647             :         /* Compute correlation between xn[] and excf[] */
    1648             : 
    1649     1125976 :         L_tmp = L_mac( 1, xn[0], excf[0] );
    1650    72076800 :         FOR( i = 1; i < L_subfr; i++ )
    1651             :         {
    1652    70950824 :             L_tmp = L_mac_o( L_tmp, xn[i], excf[i], &Overflow );
    1653             :         }
    1654     1125976 :         exp = norm_l( L_tmp );
    1655     1125976 :         L_tmp = L_shl( L_tmp, exp );
    1656     1125976 :         exp_corr = sub( 30, exp );
    1657     1125976 :         corr = extract_h( L_tmp );
    1658             : 
    1659             :         /* Compute 1/sqrt(energy of excf[]) */
    1660     1125976 :         L_tmp = L_mac( 1, excf[0], excf[0] );
    1661    72076800 :         FOR( i = 1; i < L_subfr; i++ )
    1662             :         {
    1663    70950824 :             L_tmp = L_mac_o( L_tmp, excf[i], excf[i], &Overflow );
    1664             :         }
    1665             : 
    1666     1125976 :         exp = norm_l( L_tmp );
    1667     1125976 :         L_tmp = L_shl( L_tmp, exp );
    1668     1125976 :         exp_norm = sub( 30, exp );
    1669             : 
    1670     1125976 :         L_tmp = Isqrt_lc( L_tmp, &exp_norm );
    1671     1125976 :         norm = extract_h( L_tmp );
    1672             : 
    1673             :         /* Normalize correlation = correlation * (1/sqrt(energy)) */
    1674     1125976 :         L_tmp = L_mult( corr, norm );
    1675     1125976 :         L_tmp = L_shl_sat( L_tmp, add( add( exp_corr, exp_norm ), scale ) );
    1676     1125976 :         ncorr[t] = round_fx_sat( L_tmp );
    1677             : 
    1678             :         /* update the filtered excitation excf[] for the next iteration */
    1679     1125976 :         IF( NE_16( t, t_max ) )
    1680             :         {
    1681     1078773 :             k--;
    1682    69054912 :             FOR( i = ( L_subfr - 1 ); i > 0; i-- )
    1683             :             {
    1684             :                 /* saturation can occur in add() */
    1685             :                 /*excf[i] = add(mult(exc[k], h[i]), excf[i - 1]);     move16();       */
    1686    67976139 :                 excf[i] = round_fx_sat( L_mac_sat( L_mult( excf[i - 1], 32767 ), exc[k], h[i] ) );
    1687    67976139 :                 move16();
    1688             :             }
    1689     1078773 :             excf[0] = mult_r( exc[k], h[0] );
    1690     1078773 :             move16();
    1691             :         }
    1692             :     }
    1693             : 
    1694       47203 :     return;
    1695             : }
    1696             : 
    1697             : /*-------------------------------------------------------------------*
    1698             :  * pit_Q_enc()
    1699             :  *
    1700             :  * Encode subframe pitch lag
    1701             :  *-------------------------------------------------------------------*/
    1702             : 
    1703        2814 : void pit_Q_enc_fx(
    1704             :     BSTR_ENC_HANDLE hBstr,   /* i/o:  encoder bitstream handle */
    1705             :     const Word16 Opt_AMR_WB, /* i  : flag indicating AMR-WB IO mode          */
    1706             :     const Word16 nBits,      /* i  : # of Q bits                             */
    1707             :     const Word16 delta,      /* i  : Half the CL searched interval           */
    1708             :     const Word16 pit_flag,   /* i  : absolute(0) or delta(1) pitch Q         */
    1709             :     const Word16 limit_flag, /* i  : restrained(0) or extended(1) Q limits   */
    1710             :     const Word16 T0,         /* i  : integer pitch lag                       */
    1711             :     const Word16 T0_frac,    /* i  : pitch fraction                          */
    1712             :     Word16 *T0_min,          /* i/o: delta search min                        */
    1713             :     Word16 *T0_max           /* o  : delta search max_val                        */
    1714             : )
    1715             : {
    1716             :     Word16 pitch_index;
    1717             : 
    1718        2814 :     IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
    1719             :     {
    1720         681 :         IF( limit_flag == 0 )
    1721             :         {
    1722             :             /* pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
    1723           0 :             pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN * 4 ) );
    1724             :         }
    1725         681 :         ELSE IF( EQ_16( limit_flag, 1 ) )
    1726             :         {
    1727             :             /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
    1728         469 :             pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_EXTEND * 4 ) );
    1729             :         }
    1730             :         ELSE /* limit_flag == 2 */
    1731             :         {
    1732             :             /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
    1733         212 :             pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_DOUBLEEXTEND * 4 ) );
    1734             :         }
    1735             :     }
    1736        2133 :     ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
    1737             :     {
    1738         465 :         pitch_index = abs_pit_enc_fx( 4, limit_flag, T0, T0_frac );
    1739             : 
    1740             :         /* find T0_min and T0_max for delta search */
    1741         465 :         IF( Opt_AMR_WB )
    1742             :         {
    1743           0 :             limit_T0_fx( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
    1744             :         }
    1745             :     }
    1746        1668 :     ELSE IF( EQ_16( nBits, 8 ) ) /* absolute encoding with 8 bits */
    1747             :     {
    1748           0 :         pitch_index = abs_pit_enc_fx( 2, limit_flag, T0, T0_frac );
    1749             : 
    1750             :         /* find T0_min and T0_max for delta search */
    1751           0 :         IF( Opt_AMR_WB )
    1752             :         {
    1753           0 :             limit_T0_fx( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
    1754             :         }
    1755             :     }
    1756        1668 :     ELSE IF( EQ_16( nBits, 6 ) ) /* relative encoding with 6 bits */
    1757             :     {
    1758        1002 :         pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
    1759             :     }
    1760         666 :     ELSE IF( EQ_16( nBits, 5 ) ) /* relative encoding with 5 bits */
    1761             :     {
    1762         666 :         IF( EQ_16( delta, 8 ) )
    1763             :         {
    1764          30 :             pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
    1765             :         }
    1766             :         ELSE /* delta == 4 */
    1767             :         {
    1768         636 :             pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
    1769             :         }
    1770             :     }
    1771             :     ELSE /* nBits == 4 ) */ /* relative encoding with 4 bits */
    1772             :     {
    1773           0 :         IF( EQ_16( delta, 8 ) )
    1774             :         {
    1775           0 :             pitch_index = delta_pit_enc_fx( 0, T0, T0_frac, *T0_min );
    1776             :         }
    1777             :         ELSE /* delta == 4 */
    1778             :         {
    1779           0 :             pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
    1780             :         }
    1781             :     }
    1782             : 
    1783        2814 :     IF( !Opt_AMR_WB )
    1784             :     {
    1785             :         /* find T0_min and T0_max for delta search */
    1786        2814 :         limit_T0_fx( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    1787             :     }
    1788             : 
    1789             :     {
    1790        2814 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1791             :     }
    1792             : 
    1793        2814 :     return;
    1794             : }
    1795             : 
    1796      250001 : void pit_Q_enc_ivas_fx(
    1797             :     BSTR_ENC_HANDLE hBstr,   /* i/o:  encoder bitstream handle */
    1798             :     const Word16 Opt_AMR_WB, /* i  : flag indicating AMR-WB IO mode          */
    1799             :     const Word16 nBits,      /* i  : # of Q bits                             */
    1800             :     const Word16 delta,      /* i  : Half the CL searched interval           */
    1801             :     const Word16 pit_flag,   /* i  : absolute(0) or delta(1) pitch Q         */
    1802             :     const Word16 limit_flag, /* i  : restrained(0) or extended(1) Q limits   */
    1803             :     const Word16 T0,         /* i  : integer pitch lag                       */
    1804             :     const Word16 T0_frac,    /* i  : pitch fraction                          */
    1805             :     Word16 *T0_min,          /* i/o: delta search min                        */
    1806             :     Word16 *T0_max           /* o  : delta search max_val                        */
    1807             : )
    1808             : {
    1809             :     Word16 pitch_index;
    1810             : 
    1811      250001 :     IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
    1812             :     {
    1813       75321 :         IF( limit_flag == 0 )
    1814             :         {
    1815             :             /* pitch_index = T0*4 + T0_frac - (PIT_MIN*4);*/
    1816           0 :             pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN * 4 ) );
    1817             :         }
    1818       75321 :         ELSE IF( EQ_16( limit_flag, 1 ) )
    1819             :         {
    1820             :             /*pitch_index = T0*4 + T0_frac - (PIT_MIN_EXTEND*4);*/
    1821       67128 :             pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_EXTEND * 4 ) );
    1822             :         }
    1823             :         ELSE /* limit_flag == 2 */
    1824             :         {
    1825             :             /*pitch_index = T0*4 + T0_frac - (PIT_MIN_DOUBLEEXTEND*4);*/
    1826        8193 :             pitch_index = sub( add( shl( T0, 2 ), T0_frac ), ( PIT_MIN_DOUBLEEXTEND * 4 ) );
    1827             :         }
    1828             :     }
    1829      174680 :     ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
    1830             :     {
    1831       36053 :         pitch_index = abs_pit_enc_fx( 4, limit_flag, T0, T0_frac );
    1832             : 
    1833             :         /* find T0_min and T0_max for delta search */
    1834       36053 :         IF( Opt_AMR_WB )
    1835             :         {
    1836           0 :             limit_T0_fx( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
    1837             :         }
    1838             :     }
    1839      138627 :     ELSE IF( EQ_16( nBits, 8 ) ) /* absolute encoding with 8 bits */
    1840             :     {
    1841        4460 :         pitch_index = abs_pit_enc_fx( 2, limit_flag, T0, T0_frac );
    1842             : 
    1843             :         /* find T0_min and T0_max for delta search */
    1844        4460 :         IF( Opt_AMR_WB )
    1845             :         {
    1846           0 :             limit_T0_fx( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
    1847             :         }
    1848             :     }
    1849      134167 :     ELSE IF( EQ_16( nBits, 6 ) ) /* relative encoding with 6 bits */
    1850             :     {
    1851      101443 :         pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
    1852             :     }
    1853       32724 :     ELSE IF( EQ_16( nBits, 5 ) ) /* relative encoding with 5 bits */
    1854             :     {
    1855       32724 :         IF( EQ_16( delta, 8 ) )
    1856             :         {
    1857        6900 :             pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
    1858             :         }
    1859             :         ELSE /* delta == 4 */
    1860             :         {
    1861       25824 :             pitch_index = delta_pit_enc_fx( 4, T0, T0_frac, *T0_min );
    1862             :         }
    1863             :     }
    1864             :     ELSE /* nBits == 4 ) */ /* relative encoding with 4 bits */
    1865             :     {
    1866           0 :         IF( EQ_16( delta, 8 ) )
    1867             :         {
    1868           0 :             pitch_index = delta_pit_enc_fx( 0, T0, T0_frac, *T0_min );
    1869             :         }
    1870             :         ELSE /* delta == 4 */
    1871             :         {
    1872           0 :             pitch_index = delta_pit_enc_fx( 2, T0, T0_frac, *T0_min );
    1873             :         }
    1874             :     }
    1875             : 
    1876      250001 :     IF( !Opt_AMR_WB )
    1877             :     {
    1878             :         /* find T0_min and T0_max for delta search */
    1879      250001 :         limit_T0_fx( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    1880             :     }
    1881             : 
    1882             :     {
    1883      250001 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1884             :     }
    1885             : 
    1886      250001 :     return;
    1887             : }
    1888             : 
    1889             : /*-------------------------------------------------------------------*
    1890             :  * pit16k_Q_enc()
    1891             :  *
    1892             :  * Encode subframe pitch lag @16kHz core
    1893             :  *-------------------------------------------------------------------*/
    1894             : 
    1895        3189 : void pit16k_Q_enc_fx(
    1896             :     BSTR_ENC_HANDLE hBstr,   /* i/o:  encoder bitstream handle */
    1897             :     const Word16 nBits,      /* i  : # of Q bits                             */
    1898             :     const Word16 limit_flag, /* i  : restrained(0) or extended(1) Q limits   */
    1899             :     const Word16 T0,         /* i  : integer pitch lag                       */
    1900             :     const Word16 T0_frac,    /* i  : pitch fraction                          */
    1901             :     Word16 *T0_min,          /* i/o: delta search min                        */
    1902             :     Word16 *T0_max           /* o  : delta search max_val                        */
    1903             : )
    1904             : {
    1905             :     Word16 pitch_index;
    1906             : 
    1907        3189 :     IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
    1908             :     {
    1909             :         {
    1910        1317 :             IF( LT_16( T0, PIT16k_FR2_EXTEND_10b ) )
    1911             :             {
    1912             :                 /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
    1913        1270 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
    1914             :             }
    1915             :             ELSE
    1916             :             {
    1917             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_10b*2) + ((PIT16k_FR2_EXTEND_10b-PIT16k_MIN_EXTEND)*4);*/
    1918          47 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT16k_FR2_EXTEND_10b * 2 ) ), ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 ) );
    1919             :             }
    1920             :         }
    1921             : 
    1922        1317 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1923             :     }
    1924        1872 :     ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
    1925             :     {
    1926             :         {
    1927             :             /*-------------------------------------------------------------------*
    1928             :              * The pitch range is encoded absolutely with 9 bits
    1929             :              * and is divided as follows:
    1930             :              *   PIT16k_EXTEND_MIN    to PIT16k_FR2_EXTEND_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
    1931             :              *   PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1  resolution 1/2 (frac = 0 or 2)
    1932             :              *   PIT16k_FR1_EXTEND_9b to PIT16k_MAX_EXTEND       resolution 1   (frac = 0)
    1933             :              *-------------------------------------------------------------------*/
    1934             : 
    1935           0 :             IF( LT_16( T0, PIT16k_FR2_EXTEND_9b ) )
    1936             :             {
    1937             :                 /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
    1938           0 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
    1939             :             }
    1940           0 :             ELSE IF( LT_16( T0, PIT16k_FR1_EXTEND_9b ) )
    1941             :             {
    1942             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_9b*2) + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4);*/
    1943           0 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT16k_FR2_EXTEND_9b * 2 ) ), ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) );
    1944             :             }
    1945             :             ELSE
    1946             :             {
    1947             :                 /*pitch_index = T0 - PIT16k_FR1_EXTEND_9b + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4) + ((PIT16k_FR1_EXTEND_9b-PIT16k_FR2_EXTEND_9b)*2);*/
    1948           0 :                 pitch_index = add( add( sub( T0, PIT16k_FR1_EXTEND_9b ), ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) ), ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) );
    1949             :             }
    1950             :         }
    1951             : 
    1952           0 :         push_indice( hBstr, IND_PITCH, pitch_index, 9 );
    1953             :     }
    1954             :     ELSE /* nBits == 6 */ /* relative encoding with 6 bits */
    1955             :     {
    1956             :         /*pitch_index = (T0 - *T0_min) * 4 + T0_frac;*/
    1957        1872 :         pitch_index = add( shl( sub( T0, *T0_min ), 2 ), T0_frac );
    1958             : 
    1959        1872 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1960             :     }
    1961             : 
    1962        3189 :     limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    1963             : 
    1964        3189 :     return;
    1965             : }
    1966             : 
    1967      328550 : void pit16k_Q_enc_ivas_fx(
    1968             :     BSTR_ENC_HANDLE hBstr,   /* i/o:  encoder bitstream handle */
    1969             :     const Word16 nBits,      /* i  : # of Q bits                             */
    1970             :     const Word16 limit_flag, /* i  : restrained(0) or extended(1) Q limits   */
    1971             :     const Word16 T0,         /* i  : integer pitch lag                       */
    1972             :     const Word16 T0_frac,    /* i  : pitch fraction                          */
    1973             :     Word16 *T0_min,          /* i/o: delta search min                        */
    1974             :     Word16 *T0_max           /* o  : delta search max_val                        */
    1975             : )
    1976             : {
    1977             :     Word16 pitch_index;
    1978             : 
    1979      328550 :     IF( EQ_16( nBits, 10 ) ) /* absolute encoding with 10 bits */
    1980             :     {
    1981             :         {
    1982      137033 :             IF( LT_16( T0, PIT16k_FR2_EXTEND_10b ) )
    1983             :             {
    1984             :                 /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
    1985      133532 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
    1986             :             }
    1987             :             ELSE
    1988             :             {
    1989             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_10b*2) + ((PIT16k_FR2_EXTEND_10b-PIT16k_MIN_EXTEND)*4);*/
    1990        3501 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT16k_FR2_EXTEND_10b * 2 ) ), ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 ) );
    1991             :             }
    1992             :         }
    1993             : 
    1994      137033 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1995             :     }
    1996      191517 :     ELSE IF( EQ_16( nBits, 9 ) ) /* absolute encoding with 9 bits */
    1997             :     {
    1998             :         {
    1999             :             /*-------------------------------------------------------------------*
    2000             :              * The pitch range is encoded absolutely with 9 bits
    2001             :              * and is divided as follows:
    2002             :              *   PIT16k_EXTEND_MIN    to PIT16k_FR2_EXTEND_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
    2003             :              *   PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1  resolution 1/2 (frac = 0 or 2)
    2004             :              *   PIT16k_FR1_EXTEND_9b to PIT16k_MAX_EXTEND       resolution 1   (frac = 0)
    2005             :              *-------------------------------------------------------------------*/
    2006             : 
    2007           0 :             IF( LT_16( T0, PIT16k_FR2_EXTEND_9b ) )
    2008             :             {
    2009             :                 /*pitch_index = T0*4 + T0_frac - (PIT16k_MIN_EXTEND*4);*/
    2010           0 :                 pitch_index = add( shl( T0, 2 ), sub( T0_frac, ( PIT16k_MIN_EXTEND * 4 ) ) );
    2011             :             }
    2012           0 :             ELSE IF( LT_16( T0, PIT16k_FR1_EXTEND_9b ) )
    2013             :             {
    2014             :                 /*pitch_index = T0*2 + (T0_frac>>1) - (PIT16k_FR2_EXTEND_9b*2) + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4);*/
    2015           0 :                 pitch_index = add( sub( add( shl( T0, 1 ), shr( T0_frac, 1 ) ), ( PIT16k_FR2_EXTEND_9b * 2 ) ), ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) );
    2016             :             }
    2017             :             ELSE
    2018             :             {
    2019             :                 /*pitch_index = T0 - PIT16k_FR1_EXTEND_9b + ((PIT16k_FR2_EXTEND_9b-PIT16k_MIN_EXTEND)*4) + ((PIT16k_FR1_EXTEND_9b-PIT16k_FR2_EXTEND_9b)*2);*/
    2020           0 :                 pitch_index = add( add( sub( T0, PIT16k_FR1_EXTEND_9b ), ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) ), ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) );
    2021             :             }
    2022             :         }
    2023             : 
    2024           0 :         push_indice( hBstr, IND_PITCH, pitch_index, 9 );
    2025             :     }
    2026             :     ELSE /* nBits == 6 */ /* relative encoding with 6 bits */
    2027             :     {
    2028             :         /*pitch_index = (T0 - *T0_min) * 4 + T0_frac;*/
    2029      191517 :         pitch_index = add( shl( sub( T0, *T0_min ), 2 ), T0_frac );
    2030             : 
    2031      191517 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    2032             :     }
    2033             : 
    2034      328550 :     limit_T0_fx( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    2035             : 
    2036      328550 :     return;
    2037             : }
    2038             : 
    2039             : /*------------------------------------------------------------------*
    2040             :  * pit_encode:
    2041             :  *
    2042             :  * Close-loop pitch lag search and pitch lag quantization
    2043             :  * Adaptive excitation construction
    2044             :  *------------------------------------------------------------------*/
    2045        3020 : void Mode2_pit_encode_fx(
    2046             :     const Word16 coder_type, /* i  : coding model                               */
    2047             :     const Word16 i_subfr,    /* i  : subframe index                             */
    2048             :     Word16 **pt_indice,      /* i/o: quantization indices pointer               */
    2049             :     Word16 *exc,             /* i/o: pointer to excitation signal frame         */
    2050             :     const Word16 *T_op,      /* i  : open loop pitch estimates in current frame */
    2051             :     Word16 *T0_min,          /* i/o: lower limit for close-loop search          */
    2052             :     Word16 *T0_min_frac,     /* i/o: lower limit for close-loop search          */
    2053             :     Word16 *T0_max,          /* i/o: higher limit for close-loop search         */
    2054             :     Word16 *T0_max_frac,     /* i/o: higher limit for close-loop search         */
    2055             :     Word16 *T0,              /* i/o: close loop integer pitch                   */
    2056             :     Word16 *T0_frac,         /* i/o: close loop fractional part of the pitch    */
    2057             :     Word16 *T0_res,          /* i/o: close loop pitch resolution                */
    2058             :     Word16 *h1,              /* i  : weighted filter impulse response 1Q14+shift*/
    2059             :     Word16 *xn,              /* i  : target vector                              */
    2060             :     Word16 pit_min,
    2061             :     Word16 pit_fr1,
    2062             :     Word16 pit_fr1b,
    2063             :     Word16 pit_fr2,
    2064             :     Word16 pit_max,
    2065             :     Word16 pit_res_max )
    2066             : {
    2067             :     Word16 pit_flag;
    2068             : 
    2069             :     BASOP_SATURATE_ERROR_ON_EVS;
    2070             : 
    2071             :     /* Pitch flag */
    2072        3020 :     pit_flag = i_subfr;
    2073        3020 :     move16();
    2074        3020 :     if ( EQ_16( i_subfr, ( 2 * L_SUBFR ) ) )
    2075             :     {
    2076         604 :         pit_flag = 0;
    2077         604 :         move16();
    2078             :     }
    2079             : 
    2080             :     /*-----------------------------------------------------------------*
    2081             :      *  - Limit range of pitch search
    2082             :      *  - Fractional pitch search
    2083             :      *  - Pitch quantization
    2084             :      *-----------------------------------------------------------------*/
    2085        3020 :     IF( coder_type == 0 ) /*Unvoiced Coding do nothing*/
    2086             :     {
    2087           0 :         *T0 = L_SUBFR;
    2088           0 :         move16();
    2089           0 :         *T0_frac = 0;
    2090           0 :         move16();
    2091           0 :         *T0_res = 1;
    2092           0 :         move16();
    2093             :     }
    2094        3020 :     ELSE IF( EQ_16( coder_type, 1 ) ) /* 8/4/4/4 (EVS) */
    2095             :     {
    2096           0 :         IF( i_subfr == 0 )
    2097             :         {
    2098           0 :             limit_T0_voiced( 4, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2099             :         }
    2100             :         ELSE
    2101             :         {
    2102           0 :             limit_T0_voiced( 4, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2103             :         }
    2104           0 :         *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
    2105             :                                             i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
    2106           0 :         move16();
    2107             : 
    2108           0 :         IF( i_subfr == 0 )
    2109             :         {
    2110           0 :             Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    2111             :         }
    2112             :         ELSE
    2113             :         {
    2114           0 :             Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
    2115             :         }
    2116             :     }
    2117        3020 :     ELSE IF( EQ_16( coder_type, 2 ) ) /* 8/5/8/5 (EVS) */
    2118             :     {
    2119           0 :         IF( i_subfr == 0 )
    2120             :         {
    2121           0 :             limit_T0_voiced( 5, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2122             :         }
    2123           0 :         ELSE IF( i_subfr == 2 * L_SUBFR )
    2124             :         {
    2125           0 :             limit_T0_voiced( 5, shr( pit_res_max, 1 ), T_op[1], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2126             :         }
    2127             :         ELSE
    2128             :         {
    2129           0 :             limit_T0_voiced( 5, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2130             :         }
    2131           0 :         *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
    2132             :                                             pit_flag, pit_min, pit_min, pit_fr1b, L_SUBFR );
    2133           0 :         move16();
    2134             : 
    2135           0 :         IF( pit_flag == 0 )
    2136             :         {
    2137           0 :             Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    2138             :         }
    2139             :         ELSE
    2140             :         {
    2141           0 :             Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
    2142             :         }
    2143             :     }
    2144        3020 :     ELSE IF( EQ_16( coder_type, 3 ) ) /* 9/6/6/6 (HRs- VC) */
    2145             :     {
    2146        1330 :         Word16 pit_res_max2 = pit_res_max;
    2147             : 
    2148        1330 :         IF( EQ_16( pit_min, PIT_MIN_16k ) )
    2149             :         {
    2150        1330 :             pit_res_max2 = shr( pit_res_max, 1 );
    2151             :         }
    2152             : 
    2153        1330 :         IF( ( i_subfr == 0 ) )
    2154             :         {
    2155         266 :             limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
    2156             :         }
    2157             :         ELSE
    2158             :         {
    2159        1064 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2160             :         }
    2161        1330 :         *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max,
    2162             :                                             i_subfr, pit_min, pit_fr2, pit_fr1, L_SUBFR );
    2163        1330 :         move16();
    2164             : 
    2165        1330 :         IF( i_subfr == 0 ) /* if 1st subframe */
    2166             :         {
    2167         266 :             Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    2168             :         }
    2169             :         ELSE
    2170             :         {
    2171        1064 :             Mode2_delta_pit_enc_fx( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    2172             :         }
    2173             :     }
    2174        1690 :     ELSE IF( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
    2175             :     {
    2176        1690 :         Word16 pit_res_max2 = pit_res_max;
    2177        1690 :         IF( EQ_16( pit_min, PIT_MIN_16k ) )
    2178             :         {
    2179        1690 :             pit_res_max2 = shr( pit_res_max, 1 );
    2180             :         }
    2181        1690 :         test();
    2182        1690 :         IF( ( i_subfr == 0 ) || EQ_16( i_subfr, shl( L_SUBFR, 1 ) ) )
    2183             :         {
    2184         676 :             limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
    2185             :         }
    2186             :         ELSE
    2187             :         {
    2188        1014 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2189             :         }
    2190        1690 :         *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max,
    2191             :                                             pit_flag, pit_min, pit_fr2, pit_fr1, L_SUBFR );
    2192        1690 :         move16();
    2193        1690 :         IF( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
    2194             :         {
    2195         676 :             Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    2196             :         }
    2197             :         ELSE /* if subframe 2 or 4 */
    2198             :         {
    2199        1014 :             Mode2_delta_pit_enc_fx( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    2200             :         }
    2201             :     }
    2202           0 :     ELSE IF( EQ_16( coder_type, 8 ) ) /* 8/5/5/5 (RF all pred mode) */
    2203             :     {
    2204           0 :         IF( i_subfr == 0 )
    2205             :         {
    2206           0 :             limit_T0_voiced( 5, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2207             :         }
    2208             :         ELSE
    2209             :         {
    2210           0 :             limit_T0_voiced( 5, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2211             :         }
    2212           0 :         *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
    2213             :                                             i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
    2214           0 :         move16();
    2215             : 
    2216           0 :         IF( i_subfr == 0 )
    2217             :         {
    2218           0 :             Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    2219             :         }
    2220             :         ELSE
    2221             :         {
    2222           0 :             Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
    2223             :         }
    2224             :     }
    2225           0 :     ELSE IF( EQ_16( coder_type, 9 ) ) /* 8/0/8/0 (RF mode Gen pred) */
    2226             :     {
    2227           0 :         IF( i_subfr == 0 )
    2228             :         {
    2229           0 :             limit_T0_voiced( 4, shr( pit_res_max, 1 ), T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2230             :         }
    2231             :         ELSE
    2232             :         {
    2233           0 :             limit_T0_voiced( 4, shr( pit_res_max, 1 ), *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    2234             :         }
    2235           0 :         *T0 = E_GAIN_closed_loop_search_fx( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, shr( pit_res_max, 1 ), T0_frac, T0_res, pit_res_max,
    2236             :                                             i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
    2237           0 :         move16();
    2238             : 
    2239           0 :         IF( i_subfr == 0 )
    2240             :         {
    2241           0 :             Mode2_abs_pit_enc_fx( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    2242             :         }
    2243             :         ELSE
    2244             :         {
    2245           0 :             Mode2_delta_pit_enc_fx( *T0, *T0_frac, shr( pit_res_max, 1 ), *T0_min, *T0_min_frac, pt_indice );
    2246             :         }
    2247             :     }
    2248             : 
    2249             :     BASOP_SATURATE_ERROR_OFF_EVS;
    2250             : 
    2251        3020 :     return;
    2252             : }
    2253             : 
    2254         942 : static void limit_T0_voiced2(
    2255             :     Word16 res,
    2256             :     const Word16 *T_op,
    2257             :     Word16 *T0_min,
    2258             :     Word16 *T0_min_frac,
    2259             :     Word16 *T0_max,
    2260             :     Word16 *T0_max_frac,
    2261             :     Word16 pit_min,
    2262             :     Word16 pit_max,
    2263             :     Word16 i_subfr )
    2264             : {
    2265             :     Word16 t, temp1, temp2, res2;
    2266             : 
    2267         942 :     assert( res > 1 && res <= 6 );
    2268             : 
    2269         942 :     res2 = res;
    2270         942 :     move16();
    2271         942 :     if ( EQ_16( res, 6 ) )
    2272             :     {
    2273           0 :         res2 = shr( res2, 1 );
    2274             :     }
    2275             : 
    2276             :     /* Lower-bound */
    2277         942 :     IF( i_subfr == 0 )
    2278             :     {
    2279         604 :         temp1 = sub( i_mult2( T_op[0], res ), 32 );
    2280             :     }
    2281             :     ELSE
    2282             :     {
    2283         338 :         temp1 = sub( i_mult2( T_op[1], res ), 32 );
    2284             :     }
    2285             : 
    2286         942 :     IF( LT_16( T_op[0], T_op[1] ) )
    2287             :     {
    2288         241 :         t = sub( i_mult2( T_op[0], res ), 16 );
    2289             :     }
    2290             :     ELSE
    2291             :     {
    2292         701 :         t = sub( i_mult2( T_op[1], res ), 16 );
    2293             :     }
    2294             : 
    2295         942 :     if ( LT_16( temp1, t ) )
    2296             :     {
    2297         831 :         temp1 = t;
    2298         831 :         move16();
    2299             :     }
    2300             : 
    2301         942 :     temp2 = mult( temp1, inv_T0_res[res2] );
    2302         942 :     if ( EQ_16( res, 6 ) )
    2303             :     {
    2304           0 :         temp2 = shr( temp2, 1 );
    2305             :     }
    2306             : 
    2307         942 :     *T0_min = temp2;
    2308         942 :     move16();
    2309             : 
    2310         942 :     *T0_min_frac = sub( temp1, i_mult2( temp2, res ) );
    2311         942 :     move16();
    2312             : 
    2313         942 :     IF( LT_16( *T0_min, pit_min ) )
    2314             :     {
    2315          13 :         *T0_min = pit_min;
    2316          13 :         move16();
    2317          13 :         *T0_min_frac = 0;
    2318          13 :         move16();
    2319             :     }
    2320             : 
    2321             :     /* Higher-bound */
    2322         942 :     temp1 = add( i_mult2( *T0_min, res ), add( *T0_min_frac, 63 ) );
    2323             : 
    2324         942 :     IF( T_op[0] < T_op[1] )
    2325             :     {
    2326         241 :         t = add( i_mult2( T_op[1], res ), add( 15, res ) );
    2327             :     }
    2328             :     ELSE
    2329             :     {
    2330         701 :         t = add( i_mult2( T_op[0], res ), add( 15, res ) );
    2331             :     }
    2332             : 
    2333         942 :     if ( GT_16( temp1, t ) )
    2334             :     {
    2335         846 :         temp1 = t;
    2336         846 :         move16();
    2337             :     }
    2338             : 
    2339         942 :     temp2 = mult( temp1, inv_T0_res[res] );
    2340             : 
    2341         942 :     *T0_max = temp2;
    2342         942 :     move16();
    2343             : 
    2344         942 :     *T0_max_frac = sub( temp1, i_mult2( temp2, res ) );
    2345         942 :     move16();
    2346             : 
    2347         942 :     IF( GT_16( *T0_max, pit_max ) )
    2348             :     {
    2349           6 :         *T0_max = pit_max;
    2350           6 :         *T0_max_frac = sub( res, 1 );
    2351           6 :         move16();
    2352           6 :         move16();
    2353             : 
    2354           6 :         temp1 = add( sub( i_mult2( *T0_max, res ), 64 ), res );
    2355             : 
    2356           6 :         temp2 = mult( temp1, inv_T0_res[res2] );
    2357           6 :         IF( EQ_16( res, 6 ) )
    2358             :         {
    2359           0 :             temp2 = shr( temp2, 1 );
    2360             :         }
    2361             : 
    2362           6 :         *T0_min = temp2;
    2363           6 :         move16();
    2364             : 
    2365           6 :         *T0_min_frac = sub( temp1, i_mult2( temp2, res ) );
    2366           6 :         move16();
    2367             :     }
    2368         942 : }
    2369             : 
    2370             : /*-------------------------------------------------------------------*
    2371             :  * abs_pit_enc:
    2372             :  *
    2373             :  * Encode pitch lag absolutely
    2374             :  *-------------------------------------------------------------------*/
    2375             : 
    2376         942 : void Mode2_abs_pit_enc_fx(
    2377             :     Word16 T0,          /* i  : integer pitch lag              */
    2378             :     Word16 T0_frac,     /* i  : pitch fraction                 */
    2379             :     Word16 **pt_indice, /* i/o: pointer to Vector of Q indexes */
    2380             :     Word16 pit_min,
    2381             :     Word16 pit_fr1,
    2382             :     Word16 pit_fr2,
    2383             :     Word16 pit_res_max )
    2384             : {
    2385             :     Word16 pit_res_max_half;
    2386             : 
    2387             : 
    2388         942 :     pit_res_max_half = shr( pit_res_max, 1 );
    2389             : 
    2390         942 :     IF( LT_16( T0, pit_fr2 ) )
    2391             :     {
    2392           0 :         **pt_indice = add( i_mult2( T0, pit_res_max ), sub( T0_frac, i_mult2( pit_min, pit_res_max ) ) );
    2393           0 :         move16();
    2394             :     }
    2395         942 :     ELSE IF( LT_16( T0, pit_fr1 ) )
    2396             :     {
    2397         825 :         **pt_indice = add( sub( add( i_mult2( T0, pit_res_max_half ), T0_frac ), i_mult2( pit_fr2, pit_res_max_half ) ), i_mult2( sub( pit_fr2, pit_min ), pit_res_max ) );
    2398         825 :         move16();
    2399             :     }
    2400             :     ELSE
    2401             :     {
    2402         117 :         **pt_indice = add( add( sub( T0, pit_fr1 ), i_mult2( sub( pit_fr2, pit_min ), pit_res_max ) ), i_mult2( sub( pit_fr1, pit_fr2 ), pit_res_max_half ) );
    2403         117 :         move16();
    2404             :     }
    2405             : 
    2406         942 :     ( *pt_indice )++;
    2407         942 : }
    2408             : 
    2409             : 
    2410             : /*-------------------------------------------------------------------*
    2411             :  * delta_pit_enc:
    2412             :  *
    2413             :  * Encode pitch lag differentially
    2414             :  *-------------------------------------------------------------------*/
    2415             : 
    2416        2078 : void Mode2_delta_pit_enc_fx(
    2417             :     Word16 T0,          /* i  : integer pitch lag              */
    2418             :     Word16 T0_frac,     /* i  : pitch fraction                 */
    2419             :     Word16 T0_res,      /* i  : pitch resolution               */
    2420             :     Word16 T0_min,      /* i/o: delta search min               */
    2421             :     Word16 T0_min_frac, /* i/o: delta search min               */
    2422             :     Word16 **pt_indice  /* i/o: pointer to Vector of Q indexes */
    2423             : )
    2424             : {
    2425             :     /***pt_indice = (T0 - T0_min) * T0_res + T0_frac - T0_min_frac;*/
    2426             : 
    2427        2078 :     **pt_indice = add( i_mult2( sub( T0, T0_min ), T0_res ), sub( T0_frac, T0_min_frac ) );
    2428        2078 :     move16();
    2429        2078 :     ( *pt_indice )++;
    2430        2078 : }

Generated by: LCOV version 1.14