LCOV - code coverage report
Current view: top level - lib_enc - pit_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 362 394 91.9 %
Date: 2025-05-28 04:28:20 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     5275827 : 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     5275827 :     if ( L_frame == L_FRAME || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
      86             :     {
      87     2239400 :         mvs2s( pitch, T_op, 2 );
      88             :     }
      89             :     else /* L_frame == L_FRAME16k  */
      90             :     {
      91     3036427 :         T_op[0] = (int16_t) ( pitch[0] * 1.25f + 0.5f );
      92     3036427 :         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     5275827 :     pit_flag = i_subfr;
     101     5275827 :     if ( i_subfr == 2 * L_SUBFR )
     102             :     {
     103     1173378 :         pit_flag = 0;
     104             :     }
     105             : 
     106             :     /*-----------------------------------------------------------------*
     107             :      * Limit range of pitch search
     108             :      * Fractional pitch search
     109             :      * Pitch quantization
     110             :      *-----------------------------------------------------------------*/
     111             : 
     112     5275827 :     mult_Top = 1;
     113             : 
     114     5275827 :     if ( !Opt_AMR_WB )
     115             :     {
     116             :         /*----------------------------------------------------------------*
     117             :          * Set limit_flag to 0 for restrained limits, and 1 for extended limits
     118             :          *----------------------------------------------------------------*/
     119             : 
     120     5258219 :         if ( i_subfr == 0 )
     121             :         {
     122     1212526 :             *limit_flag = 1;
     123     1212526 :             if ( coder_type == VOICED )
     124             :             {
     125       76466 :                 *limit_flag = 2; /* double-extended limits */
     126             :             }
     127             : 
     128     1212526 :             if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
     129             :             {
     130        5179 :                 *limit_flag = 0;
     131             :             }
     132             :         }
     133     4045693 :         else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
     134             :         {
     135      321200 :             if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
     136             :             {
     137      240544 :                 *limit_flag = 0;
     138             :             }
     139             :         }
     140             : 
     141             :         /* check the minimum pitch value */
     142     5258219 :         if ( *limit_flag == 0 )
     143             :         {
     144      491271 :             if ( ( i_subfr == 0 && T_op[0] < PIT_MIN ) || ( i_subfr == 2 * L_SUBFR && T_op[1] < PIT_MIN ) )
     145             :             {
     146        8917 :                 mult_Top = 2;
     147             :             }
     148             :         }
     149             : 
     150             :         /*-------------------------------------------------------*
     151             :          *  Retrieve the number of Q bits
     152             :          *-------------------------------------------------------*/
     153             : 
     154     5258219 :         nBits = 0;
     155     5258219 :         if ( coder_type != AUDIO )
     156             :         {
     157     5032008 :             nBits = pitch_bits[i_subfr / L_subfr];
     158             :         }
     159             : 
     160     5258219 :         if ( coder_type == AUDIO )
     161             :         {
     162             :             /*-------------------------------------------------------*
     163             :              *  Pitch encoding in AUDIO coder type
     164             :              *  (both ACELP@12k8 and ACELP@16k cores)
     165             :              *-------------------------------------------------------*/
     166             : 
     167      226211 :             delta = 4;
     168             : 
     169      226211 :             if ( L_subfr == L_frame / 2 && i_subfr != 0 )
     170             :             {
     171       28699 :                 pit_flag = L_SUBFR;
     172             :             }
     173             : 
     174      226211 :             if ( pit_flag == 0 )
     175             :             {
     176      124820 :                 nBits = 10;
     177             :             }
     178             :             else
     179             :             {
     180      101391 :                 nBits = 6;
     181             :             }
     182             : 
     183             :             /* pitch lag search limitation */
     184      226211 :             if ( i_subfr == 0 )
     185             :             {
     186       88474 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     187             :             }
     188      137737 :             else if ( i_subfr == 2 * L_SUBFR && pit_flag == 0 )
     189             :             {
     190       36346 :                 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      226211 :             *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      226211 :             pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     197             :         }
     198     5032008 :         else if ( coder_type == VOICED )
     199             :         {
     200             :             /*-------------------------------------------------------*
     201             :              *  Pitch encoding in VOICED coder type (ACELP@12k8 core only)
     202             :              *-------------------------------------------------------*/
     203             : 
     204      305864 :             delta = 4;
     205             : 
     206      305864 :             if ( i_subfr == 2 * L_SUBFR )
     207             :             {
     208       76466 :                 pit_flag = i_subfr;
     209             :             }
     210             : 
     211             :             /* pitch lag search limitation */
     212      305864 :             if ( i_subfr == 0 )
     213             :             {
     214       76466 :                 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      305864 :             if ( nBits == 9 || nBits == 5 )
     219             :             {
     220      232757 :                 *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       73107 :             else if ( nBits == 10 )
     223             :             {
     224       73107 :                 *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      305864 :             pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     228             :         }
     229     4726144 :         else if ( tdm_Pitch_reuse_flag == 1 || nBits == 4 )
     230         408 :         {
     231             :             /*-------------------------------------------------------*
     232             :              *  Pitch encoding with reusing primary channel information
     233             :              *-------------------------------------------------------*/
     234             :             int16_t loc_T0, loc_frac;
     235             : 
     236         408 :             delta = 4;
     237             : 
     238         408 :             pit_flag = L_SUBFR;
     239             : 
     240         408 :             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         408 :                 loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
     248         408 :                 loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
     249             :             }
     250             : 
     251             :             /* pitch lag search limitation */
     252         408 :             limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     253         408 :             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         408 :                 *T0 = loc_T0;
     266         408 :                 *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     4725736 :             delta = 8;
     277             : 
     278             :             /* pitch lag search limitation */
     279     4725736 :             if ( i_subfr == 0 )
     280             :             {
     281     1047484 :                 limit_T0( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     282             :             }
     283     3678252 :             else if ( i_subfr == 2 * L_SUBFR )
     284             :             {
     285     1027363 :                 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     4725736 :             if ( L_frame == L_FRAME )
     290             :             {
     291     1689049 :                 if ( nBits == 8 || nBits == 5 )
     292             :                 {
     293       55159 :                     if ( *limit_flag == 0 )
     294             :                     {
     295       36539 :                         *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       18620 :                         *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     1633890 :                 else if ( nBits == 9 || nBits == 6 )
     303             :                 {
     304     1126445 :                     if ( *limit_flag == 0 )
     305             :                     {
     306      454528 :                         *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      671917 :                         *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      507445 :                 else if ( nBits == 10 )
     314             :                 {
     315      507445 :                     *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     1689049 :                 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     3036687 :                 if ( nBits == 9 || nBits == 6 )
     323             :                 {
     324     1809981 :                     *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     1226706 :                 else if ( nBits == 10 )
     327             :                 {
     328     1226706 :                     *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     3036687 :                 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       17608 :         delta = 8;
     343       17608 :         *limit_flag = 0;
     344             : 
     345       17608 :         if ( core_brate == ACELP_6k60 )
     346             :         {
     347        1480 :             nBits = 5;
     348             : 
     349             :             /* pitch lag search limitation */
     350        1480 :             if ( i_subfr == 0 )
     351             :             {
     352         370 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     353         370 :                 nBits = 8;
     354             :             }
     355             : 
     356        1480 :             if ( i_subfr == 2 * L_SUBFR )
     357             :             {
     358             :                 /* rewrite pit_flag - it must not be zero */
     359         370 :                 pit_flag = i_subfr;
     360             :             }
     361             : 
     362             :             /* search and encode the closed loop pitch period */
     363        1480 :             *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       16128 :         else if ( core_brate == ACELP_8k85 )
     366             :         {
     367        1080 :             nBits = 5;
     368             : 
     369             :             /* pitch lag search limitation */
     370        1080 :             if ( i_subfr == 0 )
     371             :             {
     372         270 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     373         270 :                 nBits = 8;
     374             :             }
     375         810 :             else if ( i_subfr == 2 * L_SUBFR )
     376             :             {
     377         270 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     378         270 :                 nBits = 8;
     379             :             }
     380             : 
     381             :             /* search and encode the closed loop pitch period */
     382        1080 :             *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       15048 :             nBits = 6;
     387             : 
     388             :             /* pitch lag search limitation */
     389       15048 :             if ( i_subfr == 0 )
     390             :             {
     391        3762 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     392        3762 :                 nBits = 9;
     393             :             }
     394       11286 :             else if ( i_subfr == 2 * L_SUBFR )
     395             :             {
     396        3762 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     397        3762 :                 nBits = 9;
     398             :             }
     399             :             else
     400             :             {
     401        7524 :                 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       15048 :             *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       17608 :         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     5275827 :     pitch_cl = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     416             : 
     417     5275827 :     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     5730659 : 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     5730659 :     if ( limit_flag == 0 )
     449             :     {
     450      773005 :         if ( L_frame == L_FRAME )
     451             :         {
     452      697294 :             pit_min = PIT_MIN;
     453             :         }
     454             :         else /* L_frame == L_FRAME16k */
     455             :         {
     456       75711 :             pit_min = PIT16k_MIN;
     457             :         }
     458             :     }
     459             :     else
     460             :     {
     461     4957654 :         if ( L_frame == L_FRAME )
     462             :         {
     463     1730057 :             pit_min = PIT_MIN_EXTEND;
     464     1730057 :             if ( limit_flag == 2 )
     465             :             {
     466      305864 :                 pit_min = PIT_MIN_DOUBLEEXTEND;
     467             :             }
     468             :         }
     469             :         else /* L_frame == L_FRAME16k */
     470             :         {
     471     3227597 :             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     5730659 :     t_min = t0_min - L_INTERPOL1;
     484     5730659 :     t_max = t0_max + L_INTERPOL1;
     485     5730659 :     corr = &corr_v[0] - t_min; /* corr[t_min..t_max] */
     486             : 
     487     5730659 :     norm_corr( exc, xn, h, t_min, t_max, corr, L_subfr );
     488             : 
     489             :     /*-----------------------------------------------------------------*
     490             :      * Find integer pitch
     491             :      *-----------------------------------------------------------------*/
     492             : 
     493     5730659 :     max_val = corr[t0_min];
     494     5730659 :     t0 = t0_min;
     495             : 
     496    87433944 :     for ( i = t0_min + 1; i <= t0_max; i++ )
     497             :     {
     498    81703285 :         if ( corr[i] >= max_val )
     499             :         {
     500    31686211 :             max_val = corr[i];
     501    31686211 :             t0 = i;
     502             :         }
     503             :     }
     504             : 
     505     5730659 :     if ( t0_fr1 == pit_min )
     506             :     {
     507             :         /* don't search fraction (for 7b/4b quant) */
     508        6805 :         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        6805 :         *pit_frac = 0;
     526             : 
     527        6805 :         return ( t0 );
     528             :     }
     529     5723854 :     if ( ( i_subfr == 0 ) && ( t0 >= t0_fr1 ) )
     530             :     {
     531      104514 :         *pit_frac = 0;
     532             : 
     533      104514 :         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     5619340 :     t1 = t0;
     543     5619340 :     step = 1; /* 1/4 subsample resolution */
     544     5619340 :     fraction = 1;
     545     5619340 :     if ( ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) ) || ( t0_fr2 == pit_min ) )
     546             :     {
     547      175324 :         step = 2; /* 1/2 subsample resolution */
     548      175324 :         fraction = 2;
     549             :     }
     550             : 
     551     5619340 :     if ( t0 == t0_min ) /* Limit case */
     552             :     {
     553      352967 :         fraction = 0;
     554      352967 :         cor_max = interpolation( &corr[t0], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
     555             :     }
     556             :     else /* Process negative fractions */
     557             :     {
     558     5266373 :         t0--;
     559     5266373 :         cor_max = interpolation( &corr[t0], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
     560    15482335 :         for ( i = ( fraction + step ); i <= 3; i = i + step )
     561             :         {
     562    10215962 :             temp = interpolation( &corr[t0], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
     563    10215962 :             if ( temp > cor_max )
     564             :             {
     565     9517095 :                 cor_max = temp;
     566     9517095 :                 fraction = i;
     567             :             }
     568             :         }
     569             :     }
     570             : 
     571    27746052 :     for ( i = 0; i <= 3; i = i + step ) /* Process positive fractions */
     572             :     {
     573    22126712 :         temp = interpolation( &corr[t1], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
     574    22126712 :         if ( temp > cor_max )
     575             :         {
     576     6526932 :             cor_max = temp;
     577     6526932 :             fraction = i;
     578     6526932 :             t0 = t1;
     579             :         }
     580             :     }
     581             : 
     582     5619340 :     *pit_frac = fraction;
     583             : 
     584     5619340 :     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     5806992 : 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     5806992 :     k = -t_min;
     611             : 
     612             :     /*-----------------------------------------------------------------*
     613             :      * compute the filtered excitation for the first delay t_min
     614             :      *-----------------------------------------------------------------*/
     615             : 
     616     5806992 :     conv( &exc[k], h, excf, L_subfr );
     617             : 
     618             :     /*-----------------------------------------------------------------*
     619             :      * loop for every possible period
     620             :      *-----------------------------------------------------------------*/
     621             : 
     622   141042833 :     for ( t = t_min; t <= t_max; t++ )
     623             :     {
     624             :         /* Compute correlation between xn[] and excf[] */
     625   135235841 :         ps = 0.0f;
     626  8921079105 :         for ( j = 0; j < L_subfr; ++j )
     627             :         {
     628  8785843264 :             ps += xn[j] * excf[j];
     629             :         }
     630             : 
     631             :         /* Compute 1/sqrt(energie of excf[]) */
     632   135235841 :         alp = 0.01f;
     633  8921079105 :         for ( j = 0; j < L_subfr; ++j )
     634             :         {
     635  8785843264 :             alp += excf[j] * excf[j];
     636             :         }
     637   135235841 :         norm = inv_sqrt( alp );
     638             : 
     639             :         /* Normalize correlation = correlation * (1/sqrt(energie)) */
     640   135235841 :         corr_norm[t] = ps * norm;
     641             : 
     642             :         /* update the filtered excitation excf[] for the next iteration */
     643   135235841 :         if ( t != t_max )
     644             :         {
     645   129428849 :             k--;
     646  8406023936 :             for ( j = L_subfr - 1; j > 0; j-- )
     647             :             {
     648  8276595087 :                 excf[j] = excf[j - 1] + exc[k] * h[j];
     649             :             }
     650   129428849 :             excf[0] = exc[k];
     651             :         }
     652             :     }
     653     5806992 :     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      390541 : 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      390541 :     if ( limit_flag == 0 )
     676             :     {
     677      293377 :         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       19327 :             if ( T0 < PIT_FR1_8b )
     686             :             {
     687       10541 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN * 2 );
     688             :             }
     689             :             else
     690             :             {
     691        8786 :                 pitch_index = T0 - PIT_FR1_8b + ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
     692             :             }
     693             :         }
     694      274050 :         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      274050 :             if ( T0 < PIT_FR2_9b )
     704             :             {
     705      215790 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
     706             :             }
     707       58260 :             else if ( T0 < PIT_FR1_9b )
     708             :             {
     709       31195 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_9b * 2 ) + ( ( PIT_FR2_9b - PIT_MIN ) * 4 );
     710             :             }
     711             :             else
     712             :             {
     713       27065 :                 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       97164 :     else if ( limit_flag == 1 ) /* extended Q range */
     723             :     {
     724       93805 :         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        4857 :             if ( T0 < PIT_FR1_EXTEND_8b )
     734             :             {
     735        2837 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_EXTEND * 2 );
     736             :             }
     737             :             else
     738             :             {
     739        2020 :                 pitch_index = T0 - PIT_FR1_EXTEND_8b + ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
     740             :             }
     741             :         }
     742       88948 :         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       88948 :             if ( T0 < PIT_FR2_EXTEND_9b )
     753             :             {
     754       76355 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
     755             :             }
     756       12593 :             else if ( T0 < PIT_FR1_EXTEND_9b )
     757             :             {
     758        2444 :                 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       10149 :                 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        3359 :         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        3359 :         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        3359 :             if ( T0 < PIT_FR2_DOUBLEEXTEND_9b )
     802             :             {
     803        3152 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
     804             :             }
     805         207 :             else if ( T0 < PIT_FR1_DOUBLEEXTEND_9b )
     806             :             {
     807          70 :                 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         137 :                 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      390541 :     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     1409832 : 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     1409832 :     if ( fr_steps == 0 )
     845             :     {
     846        2455 :         pitch_index = T0 - T0_min;
     847             :     }
     848     1407377 :     else if ( fr_steps == 2 )
     849             :     {
     850       70934 :         pitch_index = ( T0 - T0_min ) * 2 + ( T0_frac >> 1 );
     851             :     }
     852             :     else /* fr_steps == 4 */
     853             :     {
     854     1336443 :         pitch_index = ( T0 - T0_min ) * 4 + T0_frac;
     855             :     }
     856             : 
     857     1409832 :     return pitch_index;
     858             : }
     859             : 
     860             : /*-------------------------------------------------------------------*
     861             :  * pit_Q_enc()
     862             :  *
     863             :  * Encode subframe pitch lag
     864             :  *-------------------------------------------------------------------*/
     865             : 
     866     2344564 : 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     2344564 :     if ( nBits == 10 ) /* absolute encoding with 10 bits */
     882             :     {
     883      705372 :         if ( limit_flag == 0 )
     884             :         {
     885           0 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
     886             :         }
     887      705372 :         else if ( limit_flag == 1 )
     888             :         {
     889      632265 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
     890             :         }
     891             :         else /* limit_flag == 2 */
     892             :         {
     893       73107 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
     894             :         }
     895             :     }
     896     1639192 :     else if ( nBits == 9 ) /* absolute encoding with 9 bits */
     897             :     {
     898      344919 :         pitch_index = abs_pit_enc( 4, limit_flag, T0, T0_frac );
     899             : 
     900             :         /* find T0_min and T0_max for delta search */
     901      344919 :         if ( Opt_AMR_WB )
     902             :         {
     903        7524 :             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     1294273 :     else if ( nBits == 8 ) /* absolute encoding with 8 bits */
     907             :     {
     908       24049 :         pitch_index = abs_pit_enc( 2, limit_flag, T0, T0_frac );
     909             : 
     910             :         /* find T0_min and T0_max for delta search */
     911       24049 :         if ( Opt_AMR_WB )
     912             :         {
     913         910 :             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     1270224 :     else if ( nBits == 6 ) /* relative encoding with 6 bits */
     917             :     {
     918      995602 :         pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
     919             :     }
     920      274622 :     else if ( nBits == 5 ) /* relative encoding with 5 bits */
     921             :     {
     922      274622 :         if ( delta == 8 )
     923             :         {
     924       45224 :             pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
     925             :         }
     926             :         else /* delta == 4 */
     927             :         {
     928      229398 :             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     2344564 :     if ( !Opt_AMR_WB )
     944             :     {
     945             :         /* find T0_min and T0_max for delta search */
     946     2326956 :         limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
     947             :     }
     948             : 
     949     2344564 :     push_indice( hBstr, IND_PITCH, pitch_index, nBits );
     950             : 
     951     2344564 :     return;
     952             : }
     953             : 
     954             : /*-------------------------------------------------------------------*
     955             :  * pit16k_Q_enc()
     956             :  *
     957             :  * Encode subframe pitch lag @16kHz core
     958             :  *-------------------------------------------------------------------*/
     959             : 
     960     3116154 : 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     3116154 :     if ( nBits == 10 ) /* absolute encoding with 10 bits */
     973             :     {
     974     1306173 :         if ( T0 < PIT16k_FR2_EXTEND_10b )
     975             :         {
     976     1280095 :             pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
     977             :         }
     978             :         else
     979             :         {
     980       26078 :             pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_10b * 2 ) + ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 );
     981             :         }
     982             : 
     983     1306173 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
     984             :     }
     985     1809981 :     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     1809981 :         pitch_index = ( T0 - *T0_min ) * 4 + T0_frac;
    1015             : 
    1016     1809981 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1017             :     }
    1018             : 
    1019     3116154 :     limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    1020             : 
    1021     3116154 :     return;
    1022             : }
    1023             : 
    1024             : 
    1025             : /*------------------------------------------------------------------*
    1026             :  * limit_T0_voiced2:
    1027             :  *
    1028             :  *
    1029             :  *------------------------------------------------------------------*/
    1030             : 
    1031       18819 : 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       18819 :     if ( i_subfr == 0 )
    1046             :     {
    1047       12344 :         temp1 = ( T_op[0] * res ) - 32;
    1048             :     }
    1049             :     else
    1050             :     {
    1051        6475 :         temp1 = ( T_op[1] * res ) - 32;
    1052             :     }
    1053             : 
    1054       18819 :     if ( T_op[0] < T_op[1] )
    1055             :     {
    1056        5955 :         t = ( T_op[0] * res ) - 16;
    1057             :     }
    1058             :     else
    1059             :     {
    1060       12864 :         t = ( T_op[1] * res ) - 16;
    1061             :     }
    1062             : 
    1063       18819 :     if ( temp1 < t )
    1064             :     {
    1065       16038 :         temp1 = t;
    1066             :     }
    1067             : 
    1068       18819 :     temp2 = temp1 / res;
    1069       18819 :     *T0_min = temp2;
    1070       18819 :     *T0_min_frac = temp1 - temp2 * res;
    1071             : 
    1072       18819 :     if ( *T0_min < pit_min )
    1073             :     {
    1074         465 :         *T0_min = pit_min;
    1075         465 :         *T0_min_frac = 0;
    1076             :     }
    1077             : 
    1078             :     /* Higher-bound */
    1079       18819 :     temp1 = ( *T0_min * res ) + *T0_min_frac + 64 - 1;
    1080             : 
    1081       18819 :     if ( T_op[0] < T_op[1] )
    1082             :     {
    1083        5955 :         t = ( T_op[1] * res ) + 16 + res - 1;
    1084             :     }
    1085             :     else
    1086             :     {
    1087       12864 :         t = ( T_op[0] * res ) + 16 + res - 1;
    1088             :     }
    1089             : 
    1090       18819 :     if ( temp1 > t )
    1091             :     {
    1092       16500 :         temp1 = t;
    1093             :     }
    1094             : 
    1095       18819 :     temp2 = temp1 / res;
    1096       18819 :     *T0_max = temp2;
    1097       18819 :     *T0_max_frac = temp1 - temp2 * res;
    1098             : 
    1099       18819 :     if ( *T0_max > pit_max )
    1100             :     {
    1101         201 :         *T0_max = pit_max;
    1102         201 :         *T0_max_frac = res - 1;
    1103         201 :         temp1 = ( *T0_max * res ) - 64 + res;
    1104         201 :         temp2 = temp1 / res;
    1105         201 :         *T0_min = temp2;
    1106         201 :         *T0_min_frac = temp1 - temp2 * res;
    1107             :     }
    1108             : 
    1109       18819 :     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       76333 : 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       76333 :     pit_flag = i_subfr;
    1146             : 
    1147       76333 :     if ( i_subfr == ( 2 * L_SUBFR ) )
    1148             :     {
    1149       16400 :         pit_flag = 0;
    1150             :     }
    1151             : 
    1152             :     /*-----------------------------------------------------------------*
    1153             :      *  - Limit range of pitch search
    1154             :      *  - Fractional pitch search
    1155             :      *  - Pitch quantization
    1156             :      *-----------------------------------------------------------------*/
    1157             : 
    1158       76333 :     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       76333 :     else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
    1165             :     {
    1166        2952 :         if ( i_subfr == 0 )
    1167             :         {
    1168         738 :             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        2214 :             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        2952 :         *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        2952 :         if ( i_subfr == 0 )
    1178             :         {
    1179         738 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1180             :         }
    1181             :         else
    1182             :         {
    1183        2214 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1184             :         }
    1185             :     }
    1186       73381 :     else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
    1187             :     {
    1188             : 
    1189        7852 :         if ( i_subfr == 0 )
    1190             :         {
    1191        1963 :             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        5889 :         else if ( i_subfr == 2 * L_SUBFR )
    1194             :         {
    1195        1963 :             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        3926 :             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        7852 :         *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        7852 :         if ( pit_flag == 0 )
    1205             :         {
    1206        3926 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1207             :         }
    1208             :         else
    1209             :         {
    1210        3926 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1211             :         }
    1212             :     }
    1213       65529 :     else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
    1214             :     {
    1215       28341 :         int16_t pit_res_max2 = pit_res_max;
    1216             : 
    1217       28341 :         if ( pit_min == PIT_MIN_16k )
    1218             :         {
    1219             : 
    1220       24325 :             pit_res_max2 = pit_res_max >> 1;
    1221             :         }
    1222             : 
    1223       28341 :         if ( i_subfr == 0 )
    1224             :         {
    1225             : 
    1226        5869 :             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       22472 :             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       28341 :         *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       28341 :         if ( i_subfr == 0 ) /* if 1st subframe */
    1236             :         {
    1237        5869 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    1238             :         }
    1239             :         else
    1240             :         {
    1241       22472 :             Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    1242             :         }
    1243             :     }
    1244       37188 :     else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
    1245             :     {
    1246       31768 :         int16_t pit_res_max2 = pit_res_max;
    1247             : 
    1248       31768 :         if ( pit_min == PIT_MIN_16k )
    1249             :         {
    1250             : 
    1251       29340 :             pit_res_max2 = pit_res_max >> 1;
    1252             :         }
    1253             : 
    1254             : 
    1255       31768 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_SUBFR ) )
    1256             :         {
    1257             : 
    1258       12950 :             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       18818 :             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       31768 :         *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       31768 :         if ( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
    1268             :         {
    1269       12950 :             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       18818 :             Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    1274             :         }
    1275             :     }
    1276        5420 :     else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
    1277             :     {
    1278        3524 :         if ( i_subfr == 0 )
    1279             :         {
    1280         881 :             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        2643 :             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        3524 :         *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        3524 :         if ( i_subfr == 0 )
    1289             :         {
    1290         881 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1291             :         }
    1292             :         else
    1293             :         {
    1294        2643 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1295             :         }
    1296             :     }
    1297        1896 :     else if ( coder_type == 9 ) /* 8/0/8/0 (RF mode Gen pred) */
    1298             :     {
    1299        1896 :         if ( i_subfr == 0 )
    1300             :         {
    1301         474 :             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        1422 :             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        1896 :         *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        1896 :         if ( i_subfr == 0 )
    1310             :         {
    1311         474 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1312             :         }
    1313             :         else
    1314             :         {
    1315        1422 :             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       76333 :     return;
    1324             : }
    1325             : 
    1326             : 
    1327             : /*-------------------------------------------------------------------*
    1328             :  * Mode2_abs_pit_enc:
    1329             :  *
    1330             :  * Encode pitch lag absolutely
    1331             :  *-------------------------------------------------------------------*/
    1332             : 
    1333       24838 : 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       24838 :     pit_res_max_half = pit_res_max >> 1;
    1345             : 
    1346       24838 :     if ( T0 < pit_fr2 )
    1347             :     {
    1348        2049 :         **pt_indice = T0 * pit_res_max + T0_frac - ( pit_min * pit_res_max );
    1349             :     }
    1350       22789 :     else if ( T0 < pit_fr1 )
    1351             :     {
    1352       17624 :         **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        5165 :         **pt_indice = T0 - pit_fr1 + ( ( pit_fr2 - pit_min ) * pit_res_max ) + ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
    1357             :     }
    1358             : 
    1359       24838 :     ( *pt_indice )++;
    1360             : 
    1361       24838 :     return;
    1362             : }
    1363             : 
    1364             : 
    1365             : /*-------------------------------------------------------------------*
    1366             :  * Mode2_delta_pit_enc:
    1367             :  *
    1368             :  * Encode pitch lag differentially
    1369             :  *-------------------------------------------------------------------*/
    1370             : 
    1371       51495 : 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       51495 :     **pt_indice = ( T0 - T0_min ) * T0_res + T0_frac - T0_min_frac;
    1382             : 
    1383       51495 :     ( *pt_indice )++;
    1384             : 
    1385       51495 :     return;
    1386             : }

Generated by: LCOV version 1.14