LCOV - code coverage report
Current view: top level - lib_enc - pit_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 8834b716eb6d7dfb881d5c69dd21cb18e1692722 Lines: 298 394 75.6 %
Date: 2025-07-09 08:36:12 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #include <math.h>
      41             : #include "cnst.h"
      42             : #include "prot.h"
      43             : #include "rom_enc.h"
      44             : #include "rom_com.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : /*------------------------------------------------------------------*
      48             :  * pit_encode()
      49             :  *
      50             :  * Close-loop pitch lag search and pitch lag quantization
      51             :  * Adaptive excitation construction
      52             :  *------------------------------------------------------------------*/
      53             : 
      54             : /*! r: Fractional pitch for each subframe */
      55      568595 : float pit_encode(
      56             :     BSTR_ENC_HANDLE hBstr,              /* i/o: encoder bitstream handle                    */
      57             :     const int16_t pitch_bits[],         /* i  : pitch bits                                  */
      58             :     const int32_t core_brate,           /* i  : core bitrate                                */
      59             :     const int16_t Opt_AMR_WB,           /* i  : flag indicating AMR-WB IO mode              */
      60             :     const int16_t L_frame,              /* i  : length of the frame                         */
      61             :     const int16_t coder_type,           /* i  : coding type                                 */
      62             :     int16_t *limit_flag,                /* i/o: restrained(0) or extended(1) Q limits       */
      63             :     const int16_t i_subfr,              /* i  : subframe index                              */
      64             :     float *exc,                         /* i/o: pointer to excitation signal frame          */
      65             :     const int16_t L_subfr,              /* i  : subframe length                             */
      66             :     const int16_t *pitch,               /* i  : open loop pitch estimates in current frame  */
      67             :     int16_t *T0_min,                    /* i/o: lower limit for close-loop search           */
      68             :     int16_t *T0_max,                    /* i/o: higher limit for close-loop search          */
      69             :     int16_t *T0,                        /* i/o: close loop integer pitch                    */
      70             :     int16_t *T0_frac,                   /* i/o: close loop fractional part of the pitch     */
      71             :     const float *h1,                    /* i  : weighted filter input response              */
      72             :     const float *xn,                    /* i  : target vector                               */
      73             :     const int16_t tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag            */
      74             :     const float tdm_Pri_pitch_buf[]     /* i  : primary channel pitch buffer                */
      75             : )
      76             : {
      77             :     float pitch_cl;
      78             :     int16_t pit_flag, delta, mult_Top, nBits;
      79             :     int16_t T_op[2]; /* values for two half-frames */
      80             : 
      81             :     /*----------------------------------------------------------------*
      82             :      * convert pitch values to 16kHz domain
      83             :      *----------------------------------------------------------------*/
      84             : 
      85      568595 :     if ( L_frame == L_FRAME || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
      86             :     {
      87      242268 :         mvs2s( pitch, T_op, 2 );
      88             :     }
      89             :     else /* L_frame == L_FRAME16k  */
      90             :     {
      91      326327 :         T_op[0] = (int16_t) ( pitch[0] * 1.25f + 0.5f );
      92      326327 :         T_op[1] = (int16_t) ( pitch[1] * 1.25f + 0.5f );
      93             :     }
      94             : 
      95             :     /*----------------------------------------------------------------*
      96             :      * Initializations
      97             :      *----------------------------------------------------------------*/
      98             : 
      99             :     /* Set pit_flag to 0 for every subframe with absolute pitch search */
     100      568595 :     pit_flag = i_subfr;
     101      568595 :     if ( i_subfr == 2 * L_SUBFR )
     102             :     {
     103      125560 :         pit_flag = 0;
     104             :     }
     105             : 
     106             :     /*-----------------------------------------------------------------*
     107             :      * Limit range of pitch search
     108             :      * Fractional pitch search
     109             :      * Pitch quantization
     110             :      *-----------------------------------------------------------------*/
     111             : 
     112      568595 :     mult_Top = 1;
     113             : 
     114      568595 :     if ( !Opt_AMR_WB )
     115             :     {
     116             :         /*----------------------------------------------------------------*
     117             :          * Set limit_flag to 0 for restrained limits, and 1 for extended limits
     118             :          *----------------------------------------------------------------*/
     119             : 
     120      568595 :         if ( i_subfr == 0 )
     121             :         {
     122      133859 :             *limit_flag = 1;
     123      133859 :             if ( coder_type == VOICED )
     124             :             {
     125        8880 :                 *limit_flag = 2; /* double-extended limits */
     126             :             }
     127             : 
     128      133859 :             if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
     129             :             {
     130         915 :                 *limit_flag = 0;
     131             :             }
     132             :         }
     133      434736 :         else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
     134             :         {
     135       35607 :             if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
     136             :             {
     137       26965 :                 *limit_flag = 0;
     138             :             }
     139             :         }
     140             : 
     141             :         /* check the minimum pitch value */
     142      568595 :         if ( *limit_flag == 0 )
     143             :         {
     144       56048 :             if ( ( i_subfr == 0 && T_op[0] < PIT_MIN ) || ( i_subfr == 2 * L_SUBFR && T_op[1] < PIT_MIN ) )
     145             :             {
     146         930 :                 mult_Top = 2;
     147             :             }
     148             :         }
     149             : 
     150             :         /*-------------------------------------------------------*
     151             :          *  Retrieve the number of Q bits
     152             :          *-------------------------------------------------------*/
     153             : 
     154      568595 :         nBits = 0;
     155      568595 :         if ( coder_type != AUDIO )
     156             :         {
     157      544760 :             nBits = pitch_bits[i_subfr / L_subfr];
     158             :         }
     159             : 
     160      568595 :         if ( coder_type == AUDIO )
     161             :         {
     162             :             /*-------------------------------------------------------*
     163             :              *  Pitch encoding in AUDIO coder type
     164             :              *  (both ACELP@12k8 and ACELP@16k cores)
     165             :              *-------------------------------------------------------*/
     166             : 
     167       23835 :             delta = 4;
     168             : 
     169       23835 :             if ( L_subfr == L_frame / 2 && i_subfr != 0 )
     170             :             {
     171        3229 :                 pit_flag = L_SUBFR;
     172             :             }
     173             : 
     174       23835 :             if ( pit_flag == 0 )
     175             :             {
     176       14736 :                 nBits = 10;
     177             :             }
     178             :             else
     179             :             {
     180        9099 :                 nBits = 6;
     181             :             }
     182             : 
     183             :             /* pitch lag search limitation */
     184       23835 :             if ( i_subfr == 0 )
     185             :             {
     186       11801 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     187             :             }
     188       12034 :             else if ( i_subfr == 2 * L_SUBFR && pit_flag == 0 )
     189             :             {
     190        2935 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     191             :             }
     192             : 
     193             :             /* search and encode the closed loop pitch period */
     194       23835 :             *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_subfr );
     195             : 
     196       23835 :             pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     197             :         }
     198      544760 :         else if ( coder_type == VOICED )
     199             :         {
     200             :             /*-------------------------------------------------------*
     201             :              *  Pitch encoding in VOICED coder type (ACELP@12k8 core only)
     202             :              *-------------------------------------------------------*/
     203             : 
     204       35520 :             delta = 4;
     205             : 
     206       35520 :             if ( i_subfr == 2 * L_SUBFR )
     207             :             {
     208        8880 :                 pit_flag = i_subfr;
     209             :             }
     210             : 
     211             :             /* pitch lag search limitation */
     212       35520 :             if ( i_subfr == 0 )
     213             :             {
     214        8880 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     215             :             }
     216             : 
     217             :             /* search and encode the closed loop pitch period */
     218       35520 :             if ( nBits == 9 || nBits == 5 )
     219             :             {
     220       27049 :                 *T0 = pitch_fr4( &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 );
     221             :             }
     222        8471 :             else if ( nBits == 10 )
     223             :             {
     224        8471 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR );
     225             :             }
     226             : 
     227       35520 :             pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     228             :         }
     229      509240 :         else if ( tdm_Pitch_reuse_flag == 1 || nBits == 4 )
     230         132 :         {
     231             :             /*-------------------------------------------------------*
     232             :              *  Pitch encoding with reusing primary channel information
     233             :              *-------------------------------------------------------*/
     234             :             int16_t loc_T0, loc_frac;
     235             : 
     236         132 :             delta = 4;
     237             : 
     238         132 :             pit_flag = L_SUBFR;
     239             : 
     240         132 :             if ( L_subfr == 2 * L_SUBFR )
     241             :             {
     242           0 :                 loc_T0 = (int16_t) ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] );
     243           0 :                 loc_frac = (int16_t) ( ( ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] ) - loc_T0 ) * 4.0f );
     244             :             }
     245             :             else
     246             :             {
     247         132 :                 loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
     248         132 :                 loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
     249             :             }
     250             : 
     251             :             /* pitch lag search limitation */
     252         132 :             limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     253         132 :             if ( nBits > 0 )
     254             :             {
     255             :                 /* search and encode the closed loop pitch period */
     256           0 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     257           0 :                 if ( delta == 8 )
     258             :                 {
     259           0 :                     *T0_frac = 0;
     260             :                 }
     261           0 :                 pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     262             :             }
     263             :             else
     264             :             {
     265         132 :                 *T0 = loc_T0;
     266         132 :                 *T0_frac = loc_frac;
     267             :             }
     268             :         }
     269             :         else
     270             :         {
     271             :             /*-------------------------------------------------------*
     272             :              *  Pitch encoding in GENERIC coder type
     273             :              *  (both ACELP@12k8 and ACELP@16k cores)
     274             :              *-------------------------------------------------------*/
     275             : 
     276      509108 :             delta = 8;
     277             : 
     278             :             /* pitch lag search limitation */
     279      509108 :             if ( i_subfr == 0 )
     280             :             {
     281      113145 :                 limit_T0( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     282             :             }
     283      395963 :             else if ( i_subfr == 2 * L_SUBFR )
     284             :             {
     285      110483 :                 limit_T0( 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      509108 :             if ( L_frame == L_FRAME )
     290             :             {
     291      182761 :                 if ( nBits == 8 || nBits == 5 )
     292             :                 {
     293       10362 :                     if ( *limit_flag == 0 )
     294             :                     {
     295        6894 :                         *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     296             :                     }
     297             :                     else
     298             :                     {
     299        3468 :                         *T0 = pitch_fr4( &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 );
     300             :                     }
     301             :                 }
     302      172399 :                 else if ( nBits == 9 || nBits == 6 )
     303             :                 {
     304      119552 :                     if ( *limit_flag == 0 )
     305             :                     {
     306       49088 :                         *T0 = pitch_fr4( &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 );
     307             :                     }
     308             :                     else
     309             :                     {
     310       70464 :                         *T0 = pitch_fr4( &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 );
     311             :                     }
     312             :                 }
     313       52847 :                 else if ( nBits == 10 )
     314             :                 {
     315       52847 :                     *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MAX, PIT_MAX, L_FRAME, L_SUBFR );
     316             :                 }
     317             : 
     318      182761 :                 pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     319             :             }
     320             :             else /* L_frame == L_FRAME16k */
     321             :             {
     322      326347 :                 if ( nBits == 9 || nBits == 6 )
     323             :                 {
     324      194214 :                     *T0 = pitch_fr4( &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 );
     325             :                 }
     326      132133 :                 else if ( nBits == 10 )
     327             :                 {
     328      132133 :                     *T0 = pitch_fr4( &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 );
     329             :                 }
     330             : 
     331      326347 :                 pit16k_Q_enc( hBstr, nBits, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     332             :             }
     333             :         }
     334             :     }
     335             : 
     336             :     /*-------------------------------------------------------*
     337             :      *  Pitch encoding in AMR-WB IO mode
     338             :      *-------------------------------------------------------*/
     339             : 
     340             :     else
     341             :     {
     342           0 :         delta = 8;
     343           0 :         *limit_flag = 0;
     344             : 
     345           0 :         if ( core_brate == ACELP_6k60 )
     346             :         {
     347           0 :             nBits = 5;
     348             : 
     349             :             /* pitch lag search limitation */
     350           0 :             if ( i_subfr == 0 )
     351             :             {
     352           0 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     353           0 :                 nBits = 8;
     354             :             }
     355             : 
     356           0 :             if ( i_subfr == 2 * L_SUBFR )
     357             :             {
     358             :                 /* rewrite pit_flag - it must not be zero */
     359           0 :                 pit_flag = i_subfr;
     360             :             }
     361             : 
     362             :             /* search and encode the closed loop pitch period */
     363           0 :             *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     364             :         }
     365           0 :         else if ( core_brate == ACELP_8k85 )
     366             :         {
     367           0 :             nBits = 5;
     368             : 
     369             :             /* pitch lag search limitation */
     370           0 :             if ( i_subfr == 0 )
     371             :             {
     372           0 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     373           0 :                 nBits = 8;
     374             :             }
     375           0 :             else if ( i_subfr == 2 * L_SUBFR )
     376             :             {
     377           0 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     378           0 :                 nBits = 8;
     379             :             }
     380             : 
     381             :             /* search and encode the closed loop pitch period */
     382           0 :             *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, *limit_flag, PIT_MIN, PIT_FR1_8b, L_FRAME, L_SUBFR );
     383             :         }
     384             :         else
     385             :         {
     386           0 :             nBits = 6;
     387             : 
     388             :             /* pitch lag search limitation */
     389           0 :             if ( i_subfr == 0 )
     390             :             {
     391           0 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     392           0 :                 nBits = 9;
     393             :             }
     394           0 :             else if ( i_subfr == 2 * L_SUBFR )
     395             :             {
     396           0 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     397           0 :                 nBits = 9;
     398             :             }
     399             :             else
     400             :             {
     401           0 :                 limit_T0( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     402             :             }
     403             : 
     404             :             /* search and encode the closed loop pitch period */
     405           0 :             *T0 = pitch_fr4( &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 );
     406             :         }
     407             : 
     408           0 :         pit_Q_enc( hBstr, 1, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     409             :     }
     410             : 
     411             :     /*-------------------------------------------------------*
     412             :      * Compute floating pitch output
     413             :      *-------------------------------------------------------*/
     414             : 
     415      568595 :     pitch_cl = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     416             : 
     417      568595 :     return pitch_cl;
     418             : }
     419             : 
     420             : /*-------------------------------------------------------------------*
     421             :  * pitch_fr4()
     422             :  *
     423             :  * Find the closed loop pitch period with 1/4 subsample resolution.
     424             :  *-------------------------------------------------------------------*/
     425             : 
     426             : /*! r: chosen integer pitch lag */
     427      610442 : int16_t pitch_fr4(
     428             :     const float exc[],        /* i  : excitation buffer                          */
     429             :     const float xn[],         /* i  : target signal                              */
     430             :     const float h[],          /* i  : weighted synthesis filter impulse response */
     431             :     const int16_t t0_min,     /* i  : minimum value in the searched range.       */
     432             :     const int16_t t0_max,     /* i  : maximum value in the searched range.       */
     433             :     int16_t *pit_frac,        /* o  : chosen fraction (0, 1, 2 or 3)             */
     434             :     const int16_t i_subfr,    /* i  : flag to first subframe                     */
     435             :     const int16_t limit_flag, /* i  : flag for limits (0=restrained, 1=extended) */
     436             :     const int16_t t0_fr2,     /* i  : minimum value for resolution 1/2           */
     437             :     const int16_t t0_fr1,     /* i  : minimum value for resolution 1             */
     438             :     const int16_t L_frame,    /* i  : length of the frame                        */
     439             :     const int16_t L_subfr     /* i  : size of subframe                           */
     440             : )
     441             : {
     442             :     int16_t i, fraction, step;
     443             :     int16_t t0, t1, t_min, t_max, pit_min;
     444             :     float cor_max, max_val, temp;
     445             :     float *corr, corr_v[15 + 2 * L_INTERPOL1 + 1];
     446             : 
     447             :     /* initialization */
     448      610442 :     if ( limit_flag == 0 )
     449             :     {
     450       81905 :         if ( L_frame == L_FRAME )
     451             :         {
     452       75278 :             pit_min = PIT_MIN;
     453             :         }
     454             :         else /* L_frame == L_FRAME16k */
     455             :         {
     456        6627 :             pit_min = PIT16k_MIN;
     457             :         }
     458             :     }
     459             :     else
     460             :     {
     461      528537 :         if ( L_frame == L_FRAME )
     462             :         {
     463      186134 :             pit_min = PIT_MIN_EXTEND;
     464      186134 :             if ( limit_flag == 2 )
     465             :             {
     466       35520 :                 pit_min = PIT_MIN_DOUBLEEXTEND;
     467             :             }
     468             :         }
     469             :         else /* L_frame == L_FRAME16k */
     470             :         {
     471      342403 :             pit_min = PIT16k_MIN_EXTEND;
     472             :         }
     473             :     }
     474             : 
     475             : 
     476             :     /*-----------------------------------------------------------------*
     477             :      * - Find interval to compute normalized correlation
     478             :      * - allocate memory to normalized correlation vector
     479             :      * - Compute normalized correlation between target and filtered
     480             :      *   excitation
     481             :      *-----------------------------------------------------------------*/
     482             : 
     483      610442 :     t_min = t0_min - L_INTERPOL1;
     484      610442 :     t_max = t0_max + L_INTERPOL1;
     485      610442 :     corr = &corr_v[0] - t_min; /* corr[t_min..t_max] */
     486             : 
     487      610442 :     norm_corr( exc, xn, h, t_min, t_max, corr, L_subfr );
     488             : 
     489             :     /*-----------------------------------------------------------------*
     490             :      * Find integer pitch
     491             :      *-----------------------------------------------------------------*/
     492             : 
     493      610442 :     max_val = corr[t0_min];
     494      610442 :     t0 = t0_min;
     495             : 
     496     9292232 :     for ( i = t0_min + 1; i <= t0_max; i++ )
     497             :     {
     498     8681790 :         if ( corr[i] >= max_val )
     499             :         {
     500     3403459 :             max_val = corr[i];
     501     3403459 :             t0 = i;
     502             :         }
     503             :     }
     504             : 
     505      610442 :     if ( t0_fr1 == pit_min )
     506             :     {
     507             :         /* don't search fraction (for 7b/4b quant) */
     508         565 :         if ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) )
     509             :         {
     510           0 :             i = ( t0 >> 1 ) * 2; /* 2 samples resolution */
     511           0 :             if ( ( i + 2 ) > PIT_MAX )
     512             :             {
     513           0 :                 i -= 2;
     514             :             }
     515           0 :             if ( corr[i] > corr[i + 2] )
     516             :             {
     517           0 :                 t0 = i;
     518             :             }
     519             :             else
     520             :             {
     521           0 :                 t0 = i + 2;
     522             :             }
     523             :         }
     524             : 
     525         565 :         *pit_frac = 0;
     526             : 
     527         565 :         return ( t0 );
     528             :     }
     529      609877 :     if ( ( i_subfr == 0 ) && ( t0 >= t0_fr1 ) )
     530             :     {
     531       11371 :         *pit_frac = 0;
     532             : 
     533       11371 :         return ( t0 );
     534             :     }
     535             : 
     536             :     /*------------------------------------------------------------------*
     537             :      * Search fractionnal pitch with 1/4 subsample resolution.
     538             :      * search the fractions around t0 and choose the one which maximizes
     539             :      * the interpolated normalized correlation.
     540             :      *-----------------------------------------------------------------*/
     541             : 
     542      598506 :     t1 = t0;
     543      598506 :     step = 1; /* 1/4 subsample resolution */
     544      598506 :     fraction = 1;
     545      598506 :     if ( ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) ) || ( t0_fr2 == pit_min ) )
     546             :     {
     547       21645 :         step = 2; /* 1/2 subsample resolution */
     548       21645 :         fraction = 2;
     549             :     }
     550             : 
     551      598506 :     if ( t0 == t0_min ) /* Limit case */
     552             :     {
     553       37183 :         fraction = 0;
     554       37183 :         cor_max = interpolation( &corr[t0], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
     555             :     }
     556             :     else /* Process negative fractions */
     557             :     {
     558      561323 :         t0--;
     559      561323 :         cor_max = interpolation( &corr[t0], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
     560     1644525 :         for ( i = ( fraction + step ); i <= 3; i = i + step )
     561             :         {
     562     1083202 :             temp = interpolation( &corr[t0], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
     563     1083202 :             if ( temp > cor_max )
     564             :             {
     565     1011243 :                 cor_max = temp;
     566     1011243 :                 fraction = i;
     567             :             }
     568             :         }
     569             :     }
     570             : 
     571     2949240 :     for ( i = 0; i <= 3; i = i + step ) /* Process positive fractions */
     572             :     {
     573     2350734 :         temp = interpolation( &corr[t1], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
     574     2350734 :         if ( temp > cor_max )
     575             :         {
     576      701570 :             cor_max = temp;
     577      701570 :             fraction = i;
     578      701570 :             t0 = t1;
     579             :         }
     580             :     }
     581             : 
     582      598506 :     *pit_frac = fraction;
     583             : 
     584      598506 :     return ( t0 );
     585             : }
     586             : 
     587             : /*-------------------------------------------------------------------*
     588             :  * norm_corr()
     589             :  *
     590             :  * Find the normalized correlation between the target vector and the
     591             :  * filtered past excitation (correlation between target and filtered
     592             :  * excitation divided by the square root of energy of filtered
     593             :  * excitation)
     594             :  *---------------------------------------------------------------------*/
     595             : 
     596      613462 : void norm_corr(
     597             :     const float exc[],    /* i  : excitation buffer                          */
     598             :     const float xn[],     /* i  : target signal                              */
     599             :     const float h[],      /* i  : weighted synthesis filter impulse response */
     600             :     const int16_t t_min,  /* i  : minimum value of searched range            */
     601             :     const int16_t t_max,  /* i  : maximum value of searched range            */
     602             :     float corr_norm[],    /* o  : normalized correlation                     */
     603             :     const int16_t L_subfr /* i  : subframe size                              */
     604             : )
     605             : {
     606             :     int16_t t, j, k;
     607             :     float excf[L_FRAME16k]; /* filtered past excitation */ /* length up to L_FRAME in GSC */
     608             :     float alp, ps, norm;
     609             : 
     610      613462 :     k = -t_min;
     611             : 
     612             :     /*-----------------------------------------------------------------*
     613             :      * compute the filtered excitation for the first delay t_min
     614             :      *-----------------------------------------------------------------*/
     615             : 
     616      613462 :     conv( &exc[k], h, excf, L_subfr );
     617             : 
     618             :     /*-----------------------------------------------------------------*
     619             :      * loop for every possible period
     620             :      *-----------------------------------------------------------------*/
     621             : 
     622    14871147 :     for ( t = t_min; t <= t_max; t++ )
     623             :     {
     624             :         /* Compute correlation between xn[] and excf[] */
     625    14257685 :         ps = 0.0f;
     626   950679381 :         for ( j = 0; j < L_subfr; ++j )
     627             :         {
     628   936421696 :             ps += xn[j] * excf[j];
     629             :         }
     630             : 
     631             :         /* Compute 1/sqrt(energie of excf[]) */
     632    14257685 :         alp = 0.01f;
     633   950679381 :         for ( j = 0; j < L_subfr; ++j )
     634             :         {
     635   936421696 :             alp += excf[j] * excf[j];
     636             :         }
     637    14257685 :         norm = inv_sqrt( alp );
     638             : 
     639             :         /* Normalize correlation = correlation * (1/sqrt(energie)) */
     640    14257685 :         corr_norm[t] = ps * norm;
     641             : 
     642             :         /* update the filtered excitation excf[] for the next iteration */
     643    14257685 :         if ( t != t_max )
     644             :         {
     645    13644223 :             k--;
     646   895664512 :             for ( j = L_subfr - 1; j > 0; j-- )
     647             :             {
     648   882020289 :                 excf[j] = excf[j - 1] + exc[k] * h[j];
     649             :             }
     650    13644223 :             excf[0] = exc[k];
     651             :         }
     652             :     }
     653      613462 :     return;
     654             : }
     655             : 
     656             : /*-------------------------------------------------------------------*
     657             :  * abs_pit_enc()
     658             :  *
     659             :  * Encode pitch lag absolutely with resolution for shortest pitches
     660             :  * depending on parameter 'fr_step':
     661             :  * fr_step = 2: pitch range encoded with 8 bits
     662             :  * fr_step = 4: pitch range encoded with 9 bits
     663             :  *-------------------------------------------------------------------*/
     664             : 
     665             : /*! r: pitch index */
     666       42599 : int16_t abs_pit_enc(
     667             :     const int16_t fr_steps,   /* i  : fractional resolution step          */
     668             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) limits */
     669             :     const int16_t T0,         /* i  : integer pitch lag                   */
     670             :     const int16_t T0_frac     /* i  : pitch fraction                      */
     671             : )
     672             : {
     673             :     int16_t pitch_index;
     674             : 
     675       42599 :     if ( limit_flag == 0 )
     676             :     {
     677       31581 :         if ( fr_steps == 2 )
     678             :         {
     679             :             /*-----------------------------------------------------------------*
     680             :              * The pitch range is encoded absolutely with 8 bits
     681             :              * and is divided as follows:
     682             :              *   PIT_MIN to PIT_FR1_8b-1  resolution 1/2 (frac = 0 or 2)
     683             :              *   PIT_FR1_8b to PIT_MAX    resolution 1   (frac = 0)
     684             :              *-----------------------------------------------------------------*/
     685        3463 :             if ( T0 < PIT_FR1_8b )
     686             :             {
     687        1844 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN * 2 );
     688             :             }
     689             :             else
     690             :             {
     691        1619 :                 pitch_index = T0 - PIT_FR1_8b + ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
     692             :             }
     693             :         }
     694       28118 :         else if ( fr_steps == 4 )
     695             :         {
     696             :             /*-------------------------------------------------------------------*
     697             :              * The pitch range is encoded absolutely with 9 bits
     698             :              * and is divided as follows:
     699             :              *   PIT_MIN    to PIT_FR2_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     700             :              *   PIT_FR2_9b to PIT_FR1_9b-1  resolution 1/2 (frac = 0 or 2)
     701             :              *   PIT_FR1_9b to PIT_MAX       resolution 1   (frac = 0)
     702             :              *-------------------------------------------------------------------*/
     703       28118 :             if ( T0 < PIT_FR2_9b )
     704             :             {
     705       22876 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
     706             :             }
     707        5242 :             else if ( T0 < PIT_FR1_9b )
     708             :             {
     709        2511 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_9b * 2 ) + ( ( PIT_FR2_9b - PIT_MIN ) * 4 );
     710             :             }
     711             :             else
     712             :             {
     713        2731 :                 pitch_index = T0 - PIT_FR1_9b + ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) + ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
     714             :             }
     715             :         }
     716             :         else /* fr_step == 0 */
     717             :         {
     718             :             /* not used in the codec */
     719           0 :             pitch_index = 0;
     720             :         }
     721             :     }
     722       11018 :     else if ( limit_flag == 1 ) /* extended Q range */
     723             :     {
     724       10609 :         if ( fr_steps == 2 )
     725             :         {
     726             :             /*-----------------------------------------------------------------*
     727             :              * The pitch range is encoded absolutely with 8 bits
     728             :              * and is divided as follows:
     729             :              *   PIT_MIN_EXTEND to PIT_FR1_EXTEND_8b-1  resolution 1/2 (frac = 0 or 2)
     730             :              *   PIT_FR1_EXTEND_8b to PIT_MAX           resolution 1   (frac = 0)
     731             :              *-----------------------------------------------------------------*/
     732             : 
     733         991 :             if ( T0 < PIT_FR1_EXTEND_8b )
     734             :             {
     735         496 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_EXTEND * 2 );
     736             :             }
     737             :             else
     738             :             {
     739         495 :                 pitch_index = T0 - PIT_FR1_EXTEND_8b + ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
     740             :             }
     741             :         }
     742        9618 :         else if ( fr_steps == 4 )
     743             :         {
     744             :             /*-------------------------------------------------------------------*
     745             :              * The pitch range is encoded absolutely with 9 bits
     746             :              * and is divided as follows:
     747             :              *   PIT_MIN_EXTEND    to PIT_FR2__EXTEND9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     748             :              *   PIT_FR2_EXTEND_9b to PIT_FR1__EXTEND9b-1  resolution 1/2 (frac = 0 or 2)
     749             :              *   PIT_FR1_EXTEND_9b to PIT_MAX              resolution 1   (frac = 0)
     750             :              *-------------------------------------------------------------------*/
     751             : 
     752        9618 :             if ( T0 < PIT_FR2_EXTEND_9b )
     753             :             {
     754        8429 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
     755             :             }
     756        1189 :             else if ( T0 < PIT_FR1_EXTEND_9b )
     757             :             {
     758         232 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_EXTEND_9b * 2 ) + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 );
     759             :             }
     760             :             else
     761             :             {
     762         957 :                 pitch_index = T0 - PIT_FR1_EXTEND_9b + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) + ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 );
     763             :             }
     764             :         }
     765             :         else /* fr_step == 0 */
     766             :         {
     767             :             /* not used in the codec */
     768           0 :             pitch_index = 0;
     769             :         }
     770             :     }
     771             :     else /* double-extended Q range */
     772             :     {
     773         409 :         if ( fr_steps == 2 )
     774             :         {
     775             :             /*-----------------------------------------------------------------*
     776             :              * The pitch range is encoded absolutely with 8 bits
     777             :              * and is divided as follows:
     778             :              *   PIT_MIN_DOUBLEEXTEND    to PIT_FR1_DOUBLEEXTEND_8b-1  resolution 1/2 (frac = 0 or 2)
     779             :              *   PIT_FR1_DOUBLEEXTEND_8b to PIT_MAX                    resolution 1   (frac = 0)
     780             :              *-----------------------------------------------------------------*/
     781             : 
     782           0 :             if ( T0 < PIT_FR1_DOUBLEEXTEND_8b )
     783             :             {
     784           0 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_DOUBLEEXTEND * 2 );
     785             :             }
     786             :             else
     787             :             {
     788           0 :                 pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_8b + ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 );
     789             :             }
     790             :         }
     791         409 :         else if ( fr_steps == 4 )
     792             :         {
     793             :             /*-------------------------------------------------------------------*
     794             :              * The pitch range is encoded absolutely with 9 bits
     795             :              * and is divided as follows:
     796             :              *   PIT_MIN_DOUBLEEXTEND    to PIT_FR2_DOUBLEEXTEND9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     797             :              *   PIT_FR2_DOUBLEEXTEND_9b to PIT_FR1_DOOBLEEXTEND9b-1  resolution 1/2 (frac = 0 or 2)
     798             :              *   PIT_FR1_DOUBLEEXTEND_9b to PIT_MAX                   resolution 1   (frac = 0)
     799             :              *-------------------------------------------------------------------*/
     800             : 
     801         409 :             if ( T0 < PIT_FR2_DOUBLEEXTEND_9b )
     802             :             {
     803         381 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
     804             :             }
     805          28 :             else if ( T0 < PIT_FR1_DOUBLEEXTEND_9b )
     806             :             {
     807          10 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_DOUBLEEXTEND_9b * 2 ) + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 );
     808             :             }
     809             :             else
     810             :             {
     811          18 :                 pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_9b + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) + ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 );
     812             :             }
     813             :         }
     814             :         else /* fr_step == 0 */
     815             :         {
     816             :             /* not used in the codec */
     817           0 :             pitch_index = 0;
     818             :         }
     819             :     }
     820             : 
     821       42599 :     return pitch_index;
     822             : }
     823             : 
     824             : /*-------------------------------------------------------------------*
     825             :  * delta_pit_enc()
     826             :  *
     827             :  * Encode pitch lag differentially from T0_min to T0_max
     828             :  * with resolution depending on parameter 'fr_step':
     829             :  * fr_step = 0: resolution 1   (frac = 0), or
     830             :  * fr_step = 2: resolution 1/2 (frac = 0 or 2), or
     831             :  * fr_step = 4: resolution 1/4 (frac = 0, 1, 2, or 3)
     832             :  *-------------------------------------------------------------------*/
     833             : 
     834             : /*! r: pitch index */
     835      147973 : int16_t delta_pit_enc(
     836             :     const int16_t fr_steps, /* i  : fractional resolution step              */
     837             :     const int16_t T0,       /* i  : integer pitch lag                       */
     838             :     const int16_t T0_frac,  /* i  : pitch fraction                          */
     839             :     const int16_t T0_min    /* i  : delta search min                        */
     840             : )
     841             : {
     842             :     int16_t pitch_index;
     843             : 
     844      147973 :     if ( fr_steps == 0 )
     845             :     {
     846         195 :         pitch_index = T0 - T0_min;
     847             :     }
     848      147778 :     else if ( fr_steps == 2 )
     849             :     {
     850        9416 :         pitch_index = ( T0 - T0_min ) * 2 + ( T0_frac >> 1 );
     851             :     }
     852             :     else /* fr_steps == 4 */
     853             :     {
     854      138362 :         pitch_index = ( T0 - T0_min ) * 4 + T0_frac;
     855             :     }
     856             : 
     857      147973 :     return pitch_index;
     858             : }
     859             : 
     860             : /*-------------------------------------------------------------------*
     861             :  * pit_Q_enc()
     862             :  *
     863             :  * Encode subframe pitch lag
     864             :  *-------------------------------------------------------------------*/
     865             : 
     866      252932 : void pit_Q_enc(
     867             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle              */
     868             :     const int16_t Opt_AMR_WB, /* i  : flag indicating AMR-WB IO mode        */
     869             :     const int16_t nBits,      /* i  : # of Q bits                           */
     870             :     const int16_t delta,      /* i  : Half the CL searched interval         */
     871             :     const int16_t pit_flag,   /* i  : absolute(0) or delta(1) pitch Q       */
     872             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) Q limits */
     873             :     const int16_t T0,         /* i  : integer pitch lag                     */
     874             :     const int16_t T0_frac,    /* i  : pitch fraction                        */
     875             :     int16_t *T0_min,          /* i/o: delta search min                      */
     876             :     int16_t *T0_max           /* o  : delta search max                      */
     877             : )
     878             : {
     879             :     int16_t pitch_index;
     880             : 
     881      252932 :     if ( nBits == 10 ) /* absolute encoding with 10 bits */
     882             :     {
     883       76054 :         if ( limit_flag == 0 )
     884             :         {
     885           0 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
     886             :         }
     887       76054 :         else if ( limit_flag == 1 )
     888             :         {
     889       67583 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
     890             :         }
     891             :         else /* limit_flag == 2 */
     892             :         {
     893        8471 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
     894             :         }
     895             :     }
     896      176878 :     else if ( nBits == 9 ) /* absolute encoding with 9 bits */
     897             :     {
     898       36340 :         pitch_index = abs_pit_enc( 4, limit_flag, T0, T0_frac );
     899             : 
     900             :         /* find T0_min and T0_max for delta search */
     901       36340 :         if ( Opt_AMR_WB )
     902             :         {
     903           0 :             limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     904             :         }
     905             :     }
     906      140538 :     else if ( nBits == 8 ) /* absolute encoding with 8 bits */
     907             :     {
     908        4438 :         pitch_index = abs_pit_enc( 2, limit_flag, T0, T0_frac );
     909             : 
     910             :         /* find T0_min and T0_max for delta search */
     911        4438 :         if ( Opt_AMR_WB )
     912             :         {
     913           0 :             limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     914             :         }
     915             :     }
     916      136100 :     else if ( nBits == 6 ) /* relative encoding with 6 bits */
     917             :     {
     918      102485 :         pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
     919             :     }
     920       33615 :     else if ( nBits == 5 ) /* relative encoding with 5 bits */
     921             :     {
     922       33615 :         if ( delta == 8 )
     923             :         {
     924        6975 :             pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
     925             :         }
     926             :         else /* delta == 4 */
     927             :         {
     928       26640 :             pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
     929             :         }
     930             :     }
     931             :     else /* nBits == 4 ) */ /* relative encoding with 4 bits */
     932             :     {
     933           0 :         if ( delta == 8 )
     934             :         {
     935           0 :             pitch_index = delta_pit_enc( 0, T0, T0_frac, *T0_min );
     936             :         }
     937             :         else /* delta == 4 */
     938             :         {
     939           0 :             pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
     940             :         }
     941             :     }
     942             : 
     943      252932 :     if ( !Opt_AMR_WB )
     944             :     {
     945             :         /* find T0_min and T0_max for delta search */
     946      252932 :         limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
     947             :     }
     948             : 
     949      252932 :     push_indice( hBstr, IND_PITCH, pitch_index, nBits );
     950             : 
     951      252932 :     return;
     952             : }
     953             : 
     954             : /*-------------------------------------------------------------------*
     955             :  * pit16k_Q_enc()
     956             :  *
     957             :  * Encode subframe pitch lag @16kHz core
     958             :  *-------------------------------------------------------------------*/
     959             : 
     960      333166 : void pit16k_Q_enc(
     961             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle                */
     962             :     const int16_t nBits,      /* i  : # of Q bits                             */
     963             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) Q limits   */
     964             :     const int16_t T0,         /* i  : integer pitch lag                       */
     965             :     const int16_t T0_frac,    /* i  : pitch fraction                          */
     966             :     int16_t *T0_min,          /* i/o: delta search min                        */
     967             :     int16_t *T0_max           /* o  : delta search max                        */
     968             : )
     969             : {
     970             :     int16_t pitch_index;
     971             : 
     972      333166 :     if ( nBits == 10 ) /* absolute encoding with 10 bits */
     973             :     {
     974      138952 :         if ( T0 < PIT16k_FR2_EXTEND_10b )
     975             :         {
     976      135429 :             pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
     977             :         }
     978             :         else
     979             :         {
     980        3523 :             pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_10b * 2 ) + ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 );
     981             :         }
     982             : 
     983      138952 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
     984             :     }
     985      194214 :     else if ( nBits == 9 ) /* absolute encoding with 9 bits */
     986             :     {
     987             :         {
     988             :             /*-------------------------------------------------------------------*
     989             :              * The pitch range is encoded absolutely with 9 bits
     990             :              * and is divided as follows:
     991             :              *   PIT16k_EXTEND_MIN    to PIT16k_FR2_EXTEND_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     992             :              *   PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1  resolution 1/2 (frac = 0 or 2)
     993             :              *   PIT16k_FR1_EXTEND_9b to PIT16k_MAX              resolution 1   (frac = 0)
     994             :              *-------------------------------------------------------------------*/
     995             : 
     996           0 :             if ( T0 < PIT16k_FR2_EXTEND_9b )
     997             :             {
     998           0 :                 pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
     999             :             }
    1000           0 :             else if ( T0 < PIT16k_FR1_EXTEND_9b )
    1001             :             {
    1002           0 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_9b * 2 ) + ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 );
    1003             :             }
    1004             :             else
    1005             :             {
    1006           0 :                 pitch_index = T0 - PIT16k_FR1_EXTEND_9b + ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) + ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 );
    1007             :             }
    1008             :         }
    1009             : 
    1010           0 :         push_indice( hBstr, IND_PITCH, pitch_index, 9 );
    1011             :     }
    1012             :     else /* nBits == 6 */ /* relative encoding with 6 bits */
    1013             :     {
    1014      194214 :         pitch_index = ( T0 - *T0_min ) * 4 + T0_frac;
    1015             : 
    1016      194214 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1017             :     }
    1018             : 
    1019      333166 :     limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    1020             : 
    1021      333166 :     return;
    1022             : }
    1023             : 
    1024             : 
    1025             : /*------------------------------------------------------------------*
    1026             :  * limit_T0_voiced2:
    1027             :  *
    1028             :  *
    1029             :  *------------------------------------------------------------------*/
    1030             : 
    1031         940 : static void limit_T0_voiced2(
    1032             :     const int16_t res,
    1033             :     const int16_t *T_op,
    1034             :     int16_t *T0_min,
    1035             :     int16_t *T0_min_frac,
    1036             :     int16_t *T0_max,
    1037             :     int16_t *T0_max_frac,
    1038             :     const int16_t pit_min,
    1039             :     const int16_t pit_max,
    1040             :     const int16_t i_subfr )
    1041             : {
    1042             :     int16_t t, temp1, temp2;
    1043             : 
    1044             :     /* Lower-bound */
    1045         940 :     if ( i_subfr == 0 )
    1046             :     {
    1047         604 :         temp1 = ( T_op[0] * res ) - 32;
    1048             :     }
    1049             :     else
    1050             :     {
    1051         336 :         temp1 = ( T_op[1] * res ) - 32;
    1052             :     }
    1053             : 
    1054         940 :     if ( T_op[0] < T_op[1] )
    1055             :     {
    1056         268 :         t = ( T_op[0] * res ) - 16;
    1057             :     }
    1058             :     else
    1059             :     {
    1060         672 :         t = ( T_op[1] * res ) - 16;
    1061             :     }
    1062             : 
    1063         940 :     if ( temp1 < t )
    1064             :     {
    1065         823 :         temp1 = t;
    1066             :     }
    1067             : 
    1068         940 :     temp2 = temp1 / res;
    1069         940 :     *T0_min = temp2;
    1070         940 :     *T0_min_frac = temp1 - temp2 * res;
    1071             : 
    1072         940 :     if ( *T0_min < pit_min )
    1073             :     {
    1074          15 :         *T0_min = pit_min;
    1075          15 :         *T0_min_frac = 0;
    1076             :     }
    1077             : 
    1078             :     /* Higher-bound */
    1079         940 :     temp1 = ( *T0_min * res ) + *T0_min_frac + 64 - 1;
    1080             : 
    1081         940 :     if ( T_op[0] < T_op[1] )
    1082             :     {
    1083         268 :         t = ( T_op[1] * res ) + 16 + res - 1;
    1084             :     }
    1085             :     else
    1086             :     {
    1087         672 :         t = ( T_op[0] * res ) + 16 + res - 1;
    1088             :     }
    1089             : 
    1090         940 :     if ( temp1 > t )
    1091             :     {
    1092         845 :         temp1 = t;
    1093             :     }
    1094             : 
    1095         940 :     temp2 = temp1 / res;
    1096         940 :     *T0_max = temp2;
    1097         940 :     *T0_max_frac = temp1 - temp2 * res;
    1098             : 
    1099         940 :     if ( *T0_max > pit_max )
    1100             :     {
    1101           7 :         *T0_max = pit_max;
    1102           7 :         *T0_max_frac = res - 1;
    1103           7 :         temp1 = ( *T0_max * res ) - 64 + res;
    1104           7 :         temp2 = temp1 / res;
    1105           7 :         *T0_min = temp2;
    1106           7 :         *T0_min_frac = temp1 - temp2 * res;
    1107             :     }
    1108             : 
    1109         940 :     return;
    1110             : }
    1111             : 
    1112             : 
    1113             : /*------------------------------------------------------------------*
    1114             :  * Mode2_pit_encode:
    1115             :  *
    1116             :  * Close-loop pitch lag search and pitch lag quantization
    1117             :  * Adaptive excitation construction
    1118             :  *------------------------------------------------------------------*/
    1119             : 
    1120        3020 : void Mode2_pit_encode(
    1121             :     const int16_t coder_type, /* i  : coding model                               */
    1122             :     const int16_t i_subfr,    /* i  : subframe index                             */
    1123             :     int16_t **pt_indice,      /* i/o: quantization indices pointer               */
    1124             :     float *exc,               /* i/o: pointer to excitation signal frame         */
    1125             :     const int16_t *T_op,      /* i  : open loop pitch estimates in current frame */
    1126             :     int16_t *T0_min,          /* i/o: lower limit for close-loop search          */
    1127             :     int16_t *T0_min_frac,     /* i/o: lower limit for close-loop search          */
    1128             :     int16_t *T0_max,          /* i/o: higher limit for close-loop search         */
    1129             :     int16_t *T0_max_frac,     /* i/o: higher limit for close-loop search         */
    1130             :     int16_t *T0,              /* i/o: close loop integer pitch                   */
    1131             :     int16_t *T0_frac,         /* i/o: close loop fractional part of the pitch    */
    1132             :     int16_t *T0_res,          /* i/o: close loop pitch resolution                */
    1133             :     float *h1,                /* i  : weighted filter impulse response           */
    1134             :     float *xn,                /* i  : target vector                              */
    1135             :     const int16_t pit_min,
    1136             :     const int16_t pit_fr1,
    1137             :     const int16_t pit_fr1b,
    1138             :     const int16_t pit_fr2,
    1139             :     const int16_t pit_max,
    1140             :     const int16_t pit_res_max )
    1141             : {
    1142             :     int16_t pit_flag;
    1143             : 
    1144             :     /* Pitch flag */
    1145        3020 :     pit_flag = i_subfr;
    1146             : 
    1147        3020 :     if ( i_subfr == ( 2 * L_SUBFR ) )
    1148             :     {
    1149         604 :         pit_flag = 0;
    1150             :     }
    1151             : 
    1152             :     /*-----------------------------------------------------------------*
    1153             :      *  - Limit range of pitch search
    1154             :      *  - Fractional pitch search
    1155             :      *  - Pitch quantization
    1156             :      *-----------------------------------------------------------------*/
    1157             : 
    1158        3020 :     if ( coder_type == 0 ) /*Unvoiced Coding do nothing*/
    1159             :     {
    1160           0 :         *T0 = L_SUBFR;
    1161           0 :         *T0_frac = 0;
    1162           0 :         *T0_res = 1;
    1163             :     }
    1164        3020 :     else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
    1165             :     {
    1166           0 :         if ( i_subfr == 0 )
    1167             :         {
    1168           0 :             limit_T0_voiced( 4, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1169             :         }
    1170             :         else
    1171             :         {
    1172           0 :             limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1173             :         }
    1174             : 
    1175           0 :         *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
    1176             : 
    1177           0 :         if ( i_subfr == 0 )
    1178             :         {
    1179           0 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1180             :         }
    1181             :         else
    1182             :         {
    1183           0 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1184             :         }
    1185             :     }
    1186        3020 :     else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
    1187             :     {
    1188             : 
    1189           0 :         if ( i_subfr == 0 )
    1190             :         {
    1191           0 :             limit_T0_voiced( 5, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1192             :         }
    1193           0 :         else if ( i_subfr == 2 * L_SUBFR )
    1194             :         {
    1195           0 :             limit_T0_voiced( 5, pit_res_max >> 1, T_op[1], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1196             :         }
    1197             :         else
    1198             :         {
    1199           0 :             limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1200             :         }
    1201             : 
    1202           0 :         *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, pit_flag, pit_min, pit_min, pit_fr1b, L_SUBFR );
    1203             : 
    1204           0 :         if ( pit_flag == 0 )
    1205             :         {
    1206           0 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1207             :         }
    1208             :         else
    1209             :         {
    1210           0 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1211             :         }
    1212             :     }
    1213        3020 :     else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
    1214             :     {
    1215        1340 :         int16_t pit_res_max2 = pit_res_max;
    1216             : 
    1217        1340 :         if ( pit_min == PIT_MIN_16k )
    1218             :         {
    1219             : 
    1220        1340 :             pit_res_max2 = pit_res_max >> 1;
    1221             :         }
    1222             : 
    1223        1340 :         if ( i_subfr == 0 )
    1224             :         {
    1225             : 
    1226         268 :             limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
    1227             :         }
    1228             :         else
    1229             :         {
    1230        1072 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1231             :         }
    1232             : 
    1233        1340 :         *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_fr2, pit_fr1, L_SUBFR );
    1234             : 
    1235        1340 :         if ( i_subfr == 0 ) /* if 1st subframe */
    1236             :         {
    1237         268 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    1238             :         }
    1239             :         else
    1240             :         {
    1241        1072 :             Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    1242             :         }
    1243             :     }
    1244        1680 :     else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
    1245             :     {
    1246        1680 :         int16_t pit_res_max2 = pit_res_max;
    1247             : 
    1248        1680 :         if ( pit_min == PIT_MIN_16k )
    1249             :         {
    1250             : 
    1251        1680 :             pit_res_max2 = pit_res_max >> 1;
    1252             :         }
    1253             : 
    1254             : 
    1255        1680 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_SUBFR ) )
    1256             :         {
    1257             : 
    1258         672 :             limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
    1259             :         }
    1260             :         else
    1261             :         {
    1262        1008 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1263             :         }
    1264             : 
    1265        1680 :         *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max2, T0_frac, T0_res, pit_res_max, pit_flag, pit_min, pit_fr2, pit_fr1, L_SUBFR );
    1266             : 
    1267        1680 :         if ( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
    1268             :         {
    1269         672 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    1270             :         }
    1271             :         else /* if subframe 2 or 4 */
    1272             :         {
    1273        1008 :             Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    1274             :         }
    1275             :     }
    1276           0 :     else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
    1277             :     {
    1278           0 :         if ( i_subfr == 0 )
    1279             :         {
    1280           0 :             limit_T0_voiced( 5, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1281             :         }
    1282             :         else
    1283             :         {
    1284           0 :             limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1285             :         }
    1286           0 :         *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
    1287             : 
    1288           0 :         if ( i_subfr == 0 )
    1289             :         {
    1290           0 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1291             :         }
    1292             :         else
    1293             :         {
    1294           0 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1295             :         }
    1296             :     }
    1297           0 :     else if ( coder_type == 9 ) /* 8/0/8/0 (RF mode Gen pred) */
    1298             :     {
    1299           0 :         if ( i_subfr == 0 )
    1300             :         {
    1301           0 :             limit_T0_voiced( 4, pit_res_max >> 1, T_op[0], 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1302             :         }
    1303             :         else
    1304             :         {
    1305           0 :             limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1306             :         }
    1307           0 :         *T0 = E_GAIN_closed_loop_search( exc, xn, h1, *T0_min, *T0_min_frac, *T0_max, *T0_max_frac, pit_res_max >> 1, T0_frac, T0_res, pit_res_max, i_subfr, pit_min, pit_min, pit_fr1b, L_SUBFR );
    1308             : 
    1309           0 :         if ( i_subfr == 0 )
    1310             :         {
    1311           0 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1312             :         }
    1313             :         else
    1314             :         {
    1315           0 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1316             :         }
    1317             :     }
    1318             :     else
    1319             :     {
    1320           0 :         assert( 0 );
    1321             :     }
    1322             : 
    1323        3020 :     return;
    1324             : }
    1325             : 
    1326             : 
    1327             : /*-------------------------------------------------------------------*
    1328             :  * Mode2_abs_pit_enc:
    1329             :  *
    1330             :  * Encode pitch lag absolutely
    1331             :  *-------------------------------------------------------------------*/
    1332             : 
    1333         940 : void Mode2_abs_pit_enc(
    1334             :     const int16_t T0,      /* i  : integer pitch lag              */
    1335             :     const int16_t T0_frac, /* i  : pitch fraction                 */
    1336             :     int16_t **pt_indice,   /* i/o: pointer to Vector of Q indexes */
    1337             :     const int16_t pit_min,
    1338             :     const int16_t pit_fr1,
    1339             :     const int16_t pit_fr2,
    1340             :     const int16_t pit_res_max )
    1341             : {
    1342             :     int16_t pit_res_max_half;
    1343             : 
    1344         940 :     pit_res_max_half = pit_res_max >> 1;
    1345             : 
    1346         940 :     if ( T0 < pit_fr2 )
    1347             :     {
    1348           0 :         **pt_indice = T0 * pit_res_max + T0_frac - ( pit_min * pit_res_max );
    1349             :     }
    1350         940 :     else if ( T0 < pit_fr1 )
    1351             :     {
    1352         832 :         **pt_indice = T0 * pit_res_max_half + T0_frac - ( pit_fr2 * pit_res_max_half ) + ( ( pit_fr2 - pit_min ) * pit_res_max );
    1353             :     }
    1354             :     else
    1355             :     {
    1356         108 :         **pt_indice = T0 - pit_fr1 + ( ( pit_fr2 - pit_min ) * pit_res_max ) + ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
    1357             :     }
    1358             : 
    1359         940 :     ( *pt_indice )++;
    1360             : 
    1361         940 :     return;
    1362             : }
    1363             : 
    1364             : 
    1365             : /*-------------------------------------------------------------------*
    1366             :  * Mode2_delta_pit_enc:
    1367             :  *
    1368             :  * Encode pitch lag differentially
    1369             :  *-------------------------------------------------------------------*/
    1370             : 
    1371        2080 : void Mode2_delta_pit_enc(
    1372             :     const int16_t T0,          /* i  : integer pitch lag              */
    1373             :     const int16_t T0_frac,     /* i  : pitch fraction                 */
    1374             :     const int16_t T0_res,      /* i  : pitch resolution               */
    1375             :     const int16_t T0_min,      /* i  : delta search min               */
    1376             :     const int16_t T0_min_frac, /* i  : delta search min               */
    1377             :     int16_t **pt_indice        /* o  : pointer to Vector of Q indexes */
    1378             : )
    1379             : {
    1380             : 
    1381        2080 :     **pt_indice = ( T0 - T0_min ) * T0_res + T0_frac - T0_min_frac;
    1382             : 
    1383        2080 :     ( *pt_indice )++;
    1384             : 
    1385        2080 :     return;
    1386             : }

Generated by: LCOV version 1.14