LCOV - code coverage report
Current view: top level - lib_enc - pit_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 363 395 91.9 %
Date: 2026-02-12 08:06:51 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 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     6124141 : 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     6124141 :     if ( L_frame == L_FRAME || ( tdm_Pri_pitch_buf != NULL && tdm_Pri_pitch_buf[0] < 0 ) )
      86             :     {
      87     2602758 :         mvs2s( pitch, T_op, 2 );
      88             :     }
      89             :     else /* L_frame == L_FRAME16k  */
      90             :     {
      91     3521383 :         T_op[0] = (int16_t) ( pitch[0] * 1.25f + 0.5f );
      92     3521383 :         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     6124141 :     pit_flag = i_subfr;
     101     6124141 :     if ( i_subfr == 2 * L_SUBFR )
     102             :     {
     103     1363722 :         pit_flag = 0;
     104             :     }
     105             : 
     106             :     /*-----------------------------------------------------------------*
     107             :      * Limit range of pitch search
     108             :      * Fractional pitch search
     109             :      * Pitch quantization
     110             :      *-----------------------------------------------------------------*/
     111             : 
     112     6124141 :     mult_Top = 1;
     113             : 
     114     6124141 :     if ( !Opt_AMR_WB )
     115             :     {
     116             :         /*----------------------------------------------------------------*
     117             :          * Set limit_flag to 0 for restrained limits, and 1 for extended limits
     118             :          *----------------------------------------------------------------*/
     119             : 
     120     6106533 :         if ( i_subfr == 0 )
     121             :         {
     122     1408579 :             *limit_flag = 1;
     123     1408579 :             if ( coder_type == VOICED )
     124             :             {
     125       85084 :                 *limit_flag = 2; /* double-extended limits */
     126             :             }
     127             : 
     128     1408579 :             if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
     129             :             {
     130        5646 :                 *limit_flag = 0;
     131             :             }
     132             :         }
     133     4697954 :         else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
     134             :         {
     135      364587 :             if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
     136             :             {
     137      272850 :                 *limit_flag = 0;
     138             :             }
     139             :         }
     140             : 
     141             :         /* check the minimum pitch value */
     142     6106533 :         if ( *limit_flag == 0 )
     143             :         {
     144      556855 :             if ( ( i_subfr == 0 && T_op[0] < PIT_MIN ) || ( i_subfr == 2 * L_SUBFR && T_op[1] < PIT_MIN ) )
     145             :             {
     146       10389 :                 mult_Top = 2;
     147             :             }
     148             :         }
     149             : 
     150             :         /*-------------------------------------------------------*
     151             :          *  Retrieve the number of Q bits
     152             :          *-------------------------------------------------------*/
     153             : 
     154     6106533 :         nBits = 0;
     155     6106533 :         if ( coder_type != AUDIO )
     156             :         {
     157     5832554 :             nBits = pitch_bits[i_subfr / L_subfr];
     158             :         }
     159             : 
     160     6106533 :         if ( coder_type == AUDIO )
     161             :         {
     162             :             /*-------------------------------------------------------*
     163             :              *  Pitch encoding in AUDIO coder type
     164             :              *  (both ACELP@12k8 and ACELP@16k cores)
     165             :              *-------------------------------------------------------*/
     166             : 
     167      273979 :             delta = 4;
     168             : 
     169      273979 :             if ( L_subfr == L_frame / 2 && i_subfr != 0 )
     170             :             {
     171       35992 :                 pit_flag = L_SUBFR;
     172             :             }
     173             : 
     174      273979 :             if ( pit_flag == 0 )
     175             :             {
     176      150149 :                 nBits = 10;
     177             :             }
     178             :             else
     179             :             {
     180      123830 :                 nBits = 6;
     181             :             }
     182             : 
     183             :             /* pitch lag search limitation */
     184      273979 :             if ( i_subfr == 0 )
     185             :             {
     186      106230 :                 limit_T0( L_FRAME, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     187             :             }
     188      167749 :             else if ( i_subfr == 2 * L_SUBFR && pit_flag == 0 )
     189             :             {
     190       43919 :                 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      273979 :             *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      273979 :             pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     197             :         }
     198     5832554 :         else if ( coder_type == VOICED )
     199             :         {
     200             :             /*-------------------------------------------------------*
     201             :              *  Pitch encoding in VOICED coder type (ACELP@12k8 core only)
     202             :              *-------------------------------------------------------*/
     203             : 
     204      340336 :             delta = 4;
     205             : 
     206      340336 :             if ( i_subfr == 2 * L_SUBFR )
     207             :             {
     208       85084 :                 pit_flag = i_subfr;
     209             :             }
     210             : 
     211             :             /* pitch lag search limitation */
     212      340336 :             if ( i_subfr == 0 )
     213             :             {
     214       85084 :                 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      340336 :             if ( nBits == 9 || nBits == 5 )
     219             :             {
     220      258847 :                 *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       81489 :             else if ( nBits == 10 )
     223             :             {
     224       81489 :                 *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      340336 :             pit_Q_enc( hBstr, 0, nBits, delta, pit_flag, *limit_flag, *T0, *T0_frac, T0_min, T0_max );
     228             :         }
     229     5492218 :         else if ( tdm_Pitch_reuse_flag == 1 || nBits == 4 )
     230         460 :         {
     231             :             /*-------------------------------------------------------*
     232             :              *  Pitch encoding with reusing primary channel information
     233             :              *-------------------------------------------------------*/
     234             :             int16_t loc_T0, loc_frac;
     235             : 
     236         460 :             delta = 4;
     237             : 
     238         460 :             pit_flag = L_SUBFR;
     239             : 
     240         460 :             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         460 :                 loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
     248         460 :                 loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
     249             :             }
     250             : 
     251             :             /* pitch lag search limitation */
     252         460 :             limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     253         460 :             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         460 :                 *T0 = loc_T0;
     266         460 :                 *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     5491758 :             delta = 8;
     277             : 
     278             :             /* pitch lag search limitation */
     279     5491758 :             if ( i_subfr == 0 )
     280             :             {
     281     1217150 :                 limit_T0( L_frame, delta, pit_flag, *limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max );
     282             :             }
     283     4274608 :             else if ( i_subfr == 2 * L_SUBFR )
     284             :             {
     285     1194210 :                 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     5491758 :             if ( L_frame == L_FRAME )
     290             :             {
     291     1970076 :                 if ( nBits == 8 || nBits == 5 )
     292             :                 {
     293       61171 :                     if ( *limit_flag == 0 )
     294             :                     {
     295       40311 :                         *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       20860 :                         *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     1908905 :                 else if ( nBits == 9 || nBits == 6 )
     303             :                 {
     304     1306035 :                     if ( *limit_flag == 0 )
     305             :                     {
     306      516314 :                         *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      789721 :                         *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      602870 :                 else if ( nBits == 10 )
     314             :                 {
     315      602870 :                     *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     1970076 :                 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     3521682 :                 if ( nBits == 9 || nBits == 6 )
     323             :                 {
     324     2099292 :                     *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     1422390 :                 else if ( nBits == 10 )
     327             :                 {
     328     1422390 :                     *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     3521682 :                 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     6124141 :     pitch_cl = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     416             : 
     417     6124141 :     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     6668192 : 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             :     int16_t corr_off;
     447             : 
     448             :     /* initialization */
     449     6668192 :     if ( limit_flag == 0 )
     450             :     {
     451      891446 :         if ( L_frame == L_FRAME )
     452             :         {
     453      802208 :             pit_min = PIT_MIN;
     454             :         }
     455             :         else /* L_frame == L_FRAME16k */
     456             :         {
     457       89238 :             pit_min = PIT16k_MIN;
     458             :         }
     459             :     }
     460             :     else
     461             :     {
     462     5776746 :         if ( L_frame == L_FRAME )
     463             :         {
     464     2027766 :             pit_min = PIT_MIN_EXTEND;
     465     2027766 :             if ( limit_flag == 2 )
     466             :             {
     467      340336 :                 pit_min = PIT_MIN_DOUBLEEXTEND;
     468             :             }
     469             :         }
     470             :         else /* L_frame == L_FRAME16k */
     471             :         {
     472     3748980 :             pit_min = PIT16k_MIN_EXTEND;
     473             :         }
     474             :     }
     475             : 
     476             : 
     477             :     /*-----------------------------------------------------------------*
     478             :      * - Find interval to compute normalized correlation
     479             :      * - allocate memory to normalized correlation vector
     480             :      * - Compute normalized correlation between target and filtered
     481             :      *   excitation
     482             :      *-----------------------------------------------------------------*/
     483             : 
     484     6668192 :     t_min = t0_min - L_INTERPOL1;
     485     6668192 :     t_max = t0_max + L_INTERPOL1;
     486             : 
     487     6668192 :     corr = corr_v;
     488     6668192 :     corr_off = -t_min;
     489             : 
     490     6668192 :     norm_corr( exc, xn, h, t_min, t_max, corr + corr_off, L_subfr );
     491             : 
     492             :     /*-----------------------------------------------------------------*
     493             :      * Find integer pitch
     494             :      *-----------------------------------------------------------------*/
     495             : 
     496     6668192 :     max_val = corr[t0_min + corr_off];
     497     6668192 :     t0 = t0_min;
     498             : 
     499   101776552 :     for ( i = t0_min + 1; i <= t0_max; i++ )
     500             :     {
     501    95108360 :         if ( corr[i + corr_off] >= max_val )
     502             :         {
     503    36742634 :             max_val = corr[i + corr_off];
     504    36742634 :             t0 = i;
     505             :         }
     506             :     }
     507             : 
     508     6668192 :     if ( t0_fr1 == pit_min )
     509             :     {
     510             :         /* don't search fraction (for 7b/4b quant) */
     511        8242 :         if ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) )
     512             :         {
     513           0 :             i = ( t0 >> 1 ) * 2; /* 2 samples resolution */
     514           0 :             if ( ( i + 2 ) > PIT_MAX )
     515             :             {
     516           0 :                 i -= 2;
     517             :             }
     518           0 :             if ( corr[i + corr_off] > corr[i + 2 + corr_off] )
     519             :             {
     520           0 :                 t0 = i;
     521             :             }
     522             :             else
     523             :             {
     524           0 :                 t0 = i + 2;
     525             :             }
     526             :         }
     527             : 
     528        8242 :         *pit_frac = 0;
     529             : 
     530        8242 :         return ( t0 );
     531             :     }
     532     6659950 :     if ( ( i_subfr == 0 ) && ( t0 >= t0_fr1 ) )
     533             :     {
     534      120265 :         *pit_frac = 0;
     535             : 
     536      120265 :         return ( t0 );
     537             :     }
     538             : 
     539             :     /*------------------------------------------------------------------*
     540             :      * Search fractionnal pitch with 1/4 subsample resolution.
     541             :      * search the fractions around t0 and choose the one which maximizes
     542             :      * the interpolated normalized correlation.
     543             :      *-----------------------------------------------------------------*/
     544             : 
     545     6539685 :     t1 = t0;
     546     6539685 :     step = 1; /* 1/4 subsample resolution */
     547     6539685 :     fraction = 1;
     548     6539685 :     if ( ( ( i_subfr == 0 ) && ( t0 >= t0_fr2 ) ) || ( t0_fr2 == pit_min ) )
     549             :     {
     550      203260 :         step = 2; /* 1/2 subsample resolution */
     551      203260 :         fraction = 2;
     552             :     }
     553             : 
     554     6539685 :     if ( t0 == t0_min ) /* Limit case */
     555             :     {
     556      415363 :         fraction = 0;
     557      415363 :         cor_max = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
     558             :     }
     559             :     else /* Process negative fractions */
     560             :     {
     561     6124322 :         t0--;
     562     6124322 :         cor_max = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, fraction, PIT_UP_SAMP, 4 );
     563    18006240 :         for ( i = ( fraction + step ); i <= 3; i = i + step )
     564             :         {
     565    11881918 :             temp = interpolation( &corr[t0 + corr_off], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
     566    11881918 :             if ( temp > cor_max )
     567             :             {
     568    11068836 :                 cor_max = temp;
     569    11068836 :                 fraction = i;
     570             :             }
     571             :         }
     572             :     }
     573             : 
     574    32291905 :     for ( i = 0; i <= 3; i = i + step ) /* Process positive fractions */
     575             :     {
     576    25752220 :         temp = interpolation( &corr[t1 + corr_off], E_ROM_inter4_1, i, PIT_UP_SAMP, 4 );
     577    25752220 :         if ( temp > cor_max )
     578             :         {
     579     7595717 :             cor_max = temp;
     580     7595717 :             fraction = i;
     581     7595717 :             t0 = t1;
     582             :         }
     583             :     }
     584             : 
     585     6539685 :     *pit_frac = fraction;
     586             : 
     587     6539685 :     return ( t0 );
     588             : }
     589             : 
     590             : /*-------------------------------------------------------------------*
     591             :  * norm_corr()
     592             :  *
     593             :  * Find the normalized correlation between the target vector and the
     594             :  * filtered past excitation (correlation between target and filtered
     595             :  * excitation divided by the square root of energy of filtered
     596             :  * excitation)
     597             :  *---------------------------------------------------------------------*/
     598             : 
     599     6754915 : void norm_corr(
     600             :     const float exc[],    /* i  : excitation buffer                          */
     601             :     const float xn[],     /* i  : target signal                              */
     602             :     const float h[],      /* i  : weighted synthesis filter impulse response */
     603             :     const int16_t t_min,  /* i  : minimum value of searched range            */
     604             :     const int16_t t_max,  /* i  : maximum value of searched range            */
     605             :     float corr_norm[],    /* o  : normalized correlation                     */
     606             :     const int16_t L_subfr /* i  : subframe size                              */
     607             : )
     608             : {
     609             :     int16_t t, j, k;
     610             :     float excf[L_FRAME16k]; /* filtered past excitation */ /* length up to L_FRAME in GSC */
     611             :     float alp, ps, norm;
     612             : 
     613     6754915 :     k = -t_min;
     614             : 
     615             :     /*-----------------------------------------------------------------*
     616             :      * compute the filtered excitation for the first delay t_min
     617             :      *-----------------------------------------------------------------*/
     618             : 
     619     6754915 :     conv( &exc[k], h, excf, L_subfr );
     620             : 
     621             :     /*-----------------------------------------------------------------*
     622             :      * loop for every possible period
     623             :      *-----------------------------------------------------------------*/
     624             : 
     625   164113273 :     for ( t = t_min; t <= t_max; t++ )
     626             :     {
     627             :         /* Compute correlation between xn[] and excf[] */
     628   157358358 :         ps = 0.0f;
     629 10382856854 :         for ( j = 0; j < L_subfr; ++j )
     630             :         {
     631 10225498496 :             ps += xn[j] * excf[j];
     632             :         }
     633             : 
     634             :         /* Compute 1/sqrt(energie of excf[]) */
     635   157358358 :         alp = 0.01f;
     636 10382856854 :         for ( j = 0; j < L_subfr; ++j )
     637             :         {
     638 10225498496 :             alp += excf[j] * excf[j];
     639             :         }
     640   157358358 :         norm = inv_sqrt( alp );
     641             : 
     642             :         /* Normalize correlation = correlation * (1/sqrt(energie)) */
     643   157358358 :         corr_norm[t] = ps * norm;
     644             : 
     645             :         /* update the filtered excitation excf[] for the next iteration */
     646   157358358 :         if ( t != t_max )
     647             :         {
     648   150603443 :             k--;
     649  9783523712 :             for ( j = L_subfr - 1; j > 0; j-- )
     650             :             {
     651  9632920269 :                 excf[j] = excf[j - 1] + exc[k] * h[j];
     652             :             }
     653   150603443 :             excf[0] = exc[k];
     654             :         }
     655             :     }
     656     6754915 :     return;
     657             : }
     658             : 
     659             : /*-------------------------------------------------------------------*
     660             :  * abs_pit_enc()
     661             :  *
     662             :  * Encode pitch lag absolutely with resolution for shortest pitches
     663             :  * depending on parameter 'fr_step':
     664             :  * fr_step = 2: pitch range encoded with 8 bits
     665             :  * fr_step = 4: pitch range encoded with 9 bits
     666             :  *-------------------------------------------------------------------*/
     667             : 
     668             : /*! r: pitch index */
     669      443127 : int16_t abs_pit_enc(
     670             :     const int16_t fr_steps,   /* i  : fractional resolution step          */
     671             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) limits */
     672             :     const int16_t T0,         /* i  : integer pitch lag                   */
     673             :     const int16_t T0_frac     /* i  : pitch fraction                      */
     674             : )
     675             : {
     676             :     int16_t pitch_index;
     677             : 
     678      443127 :     if ( limit_flag == 0 )
     679             :     {
     680      333211 :         if ( fr_steps == 2 )
     681             :         {
     682             :             /*-----------------------------------------------------------------*
     683             :              * The pitch range is encoded absolutely with 8 bits
     684             :              * and is divided as follows:
     685             :              *   PIT_MIN to PIT_FR1_8b-1  resolution 1/2 (frac = 0 or 2)
     686             :              *   PIT_FR1_8b to PIT_MAX    resolution 1   (frac = 0)
     687             :              *-----------------------------------------------------------------*/
     688       21224 :             if ( T0 < PIT_FR1_8b )
     689             :             {
     690       11618 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN * 2 );
     691             :             }
     692             :             else
     693             :             {
     694        9606 :                 pitch_index = T0 - PIT_FR1_8b + ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
     695             :             }
     696             :         }
     697      311987 :         else if ( fr_steps == 4 )
     698             :         {
     699             :             /*-------------------------------------------------------------------*
     700             :              * The pitch range is encoded absolutely with 9 bits
     701             :              * and is divided as follows:
     702             :              *   PIT_MIN    to PIT_FR2_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     703             :              *   PIT_FR2_9b to PIT_FR1_9b-1  resolution 1/2 (frac = 0 or 2)
     704             :              *   PIT_FR1_9b to PIT_MAX       resolution 1   (frac = 0)
     705             :              *-------------------------------------------------------------------*/
     706      311987 :             if ( T0 < PIT_FR2_9b )
     707             :             {
     708      246959 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
     709             :             }
     710       65028 :             else if ( T0 < PIT_FR1_9b )
     711             :             {
     712       34403 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_9b * 2 ) + ( ( PIT_FR2_9b - PIT_MIN ) * 4 );
     713             :             }
     714             :             else
     715             :             {
     716       30625 :                 pitch_index = T0 - PIT_FR1_9b + ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) + ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
     717             :             }
     718             :         }
     719             :         else /* fr_step == 0 */
     720             :         {
     721             :             /* not used in the codec */
     722           0 :             pitch_index = 0;
     723             :         }
     724             :     }
     725      109916 :     else if ( limit_flag == 1 ) /* extended Q range */
     726             :     {
     727      106321 :         if ( fr_steps == 2 )
     728             :         {
     729             :             /*-----------------------------------------------------------------*
     730             :              * The pitch range is encoded absolutely with 8 bits
     731             :              * and is divided as follows:
     732             :              *   PIT_MIN_EXTEND to PIT_FR1_EXTEND_8b-1  resolution 1/2 (frac = 0 or 2)
     733             :              *   PIT_FR1_EXTEND_8b to PIT_MAX           resolution 1   (frac = 0)
     734             :              *-----------------------------------------------------------------*/
     735             : 
     736        5545 :             if ( T0 < PIT_FR1_EXTEND_8b )
     737             :             {
     738        3173 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_EXTEND * 2 );
     739             :             }
     740             :             else
     741             :             {
     742        2372 :                 pitch_index = T0 - PIT_FR1_EXTEND_8b + ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
     743             :             }
     744             :         }
     745      100776 :         else if ( fr_steps == 4 )
     746             :         {
     747             :             /*-------------------------------------------------------------------*
     748             :              * The pitch range is encoded absolutely with 9 bits
     749             :              * and is divided as follows:
     750             :              *   PIT_MIN_EXTEND    to PIT_FR2__EXTEND9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     751             :              *   PIT_FR2_EXTEND_9b to PIT_FR1__EXTEND9b-1  resolution 1/2 (frac = 0 or 2)
     752             :              *   PIT_FR1_EXTEND_9b to PIT_MAX              resolution 1   (frac = 0)
     753             :              *-------------------------------------------------------------------*/
     754             : 
     755      100776 :             if ( T0 < PIT_FR2_EXTEND_9b )
     756             :             {
     757       86850 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
     758             :             }
     759       13926 :             else if ( T0 < PIT_FR1_EXTEND_9b )
     760             :             {
     761        2692 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_EXTEND_9b * 2 ) + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 );
     762             :             }
     763             :             else
     764             :             {
     765       11234 :                 pitch_index = T0 - PIT_FR1_EXTEND_9b + ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) + ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 );
     766             :             }
     767             :         }
     768             :         else /* fr_step == 0 */
     769             :         {
     770             :             /* not used in the codec */
     771           0 :             pitch_index = 0;
     772             :         }
     773             :     }
     774             :     else /* double-extended Q range */
     775             :     {
     776        3595 :         if ( fr_steps == 2 )
     777             :         {
     778             :             /*-----------------------------------------------------------------*
     779             :              * The pitch range is encoded absolutely with 8 bits
     780             :              * and is divided as follows:
     781             :              *   PIT_MIN_DOUBLEEXTEND    to PIT_FR1_DOUBLEEXTEND_8b-1  resolution 1/2 (frac = 0 or 2)
     782             :              *   PIT_FR1_DOUBLEEXTEND_8b to PIT_MAX                    resolution 1   (frac = 0)
     783             :              *-----------------------------------------------------------------*/
     784             : 
     785           0 :             if ( T0 < PIT_FR1_DOUBLEEXTEND_8b )
     786             :             {
     787           0 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_MIN_DOUBLEEXTEND * 2 );
     788             :             }
     789             :             else
     790             :             {
     791           0 :                 pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_8b + ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 );
     792             :             }
     793             :         }
     794        3595 :         else if ( fr_steps == 4 )
     795             :         {
     796             :             /*-------------------------------------------------------------------*
     797             :              * The pitch range is encoded absolutely with 9 bits
     798             :              * and is divided as follows:
     799             :              *   PIT_MIN_DOUBLEEXTEND    to PIT_FR2_DOUBLEEXTEND9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     800             :              *   PIT_FR2_DOUBLEEXTEND_9b to PIT_FR1_DOOBLEEXTEND9b-1  resolution 1/2 (frac = 0 or 2)
     801             :              *   PIT_FR1_DOUBLEEXTEND_9b to PIT_MAX                   resolution 1   (frac = 0)
     802             :              *-------------------------------------------------------------------*/
     803             : 
     804        3595 :             if ( T0 < PIT_FR2_DOUBLEEXTEND_9b )
     805             :             {
     806        3370 :                 pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
     807             :             }
     808         225 :             else if ( T0 < PIT_FR1_DOUBLEEXTEND_9b )
     809             :             {
     810          75 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT_FR2_DOUBLEEXTEND_9b * 2 ) + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 );
     811             :             }
     812             :             else
     813             :             {
     814         150 :                 pitch_index = T0 - PIT_FR1_DOUBLEEXTEND_9b + ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) + ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 );
     815             :             }
     816             :         }
     817             :         else /* fr_step == 0 */
     818             :         {
     819             :             /* not used in the codec */
     820           0 :             pitch_index = 0;
     821             :         }
     822             :     }
     823             : 
     824      443127 :     return pitch_index;
     825             : }
     826             : 
     827             : /*-------------------------------------------------------------------*
     828             :  * delta_pit_enc()
     829             :  *
     830             :  * Encode pitch lag differentially from T0_min to T0_max
     831             :  * with resolution depending on parameter 'fr_step':
     832             :  * fr_step = 0: resolution 1   (frac = 0), or
     833             :  * fr_step = 2: resolution 1/2 (frac = 0 or 2), or
     834             :  * fr_step = 4: resolution 1/4 (frac = 0, 1, 2, or 3)
     835             :  *-------------------------------------------------------------------*/
     836             : 
     837             : /*! r: pitch index */
     838     1644349 : int16_t delta_pit_enc(
     839             :     const int16_t fr_steps, /* i  : fractional resolution step              */
     840             :     const int16_t T0,       /* i  : integer pitch lag                       */
     841             :     const int16_t T0_frac,  /* i  : pitch fraction                          */
     842             :     const int16_t T0_min    /* i  : delta search min                        */
     843             : )
     844             : {
     845             :     int16_t pitch_index;
     846             : 
     847     1644349 :     if ( fr_steps == 0 )
     848             :     {
     849        2942 :         pitch_index = T0 - T0_min;
     850             :     }
     851     1641407 :     else if ( fr_steps == 2 )
     852             :     {
     853       82483 :         pitch_index = ( T0 - T0_min ) * 2 + ( T0_frac >> 1 );
     854             :     }
     855             :     else /* fr_steps == 4 */
     856             :     {
     857     1558924 :         pitch_index = ( T0 - T0_min ) * 4 + T0_frac;
     858             :     }
     859             : 
     860     1644349 :     return pitch_index;
     861             : }
     862             : 
     863             : /*-------------------------------------------------------------------*
     864             :  * pit_Q_enc()
     865             :  *
     866             :  * Encode subframe pitch lag
     867             :  *-------------------------------------------------------------------*/
     868             : 
     869     2730480 : void pit_Q_enc(
     870             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle              */
     871             :     const int16_t Opt_AMR_WB, /* i  : flag indicating AMR-WB IO mode        */
     872             :     const int16_t nBits,      /* i  : # of Q bits                           */
     873             :     const int16_t delta,      /* i  : Half the CL searched interval         */
     874             :     const int16_t pit_flag,   /* i  : absolute(0) or delta(1) pitch Q       */
     875             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) Q limits */
     876             :     const int16_t T0,         /* i  : integer pitch lag                     */
     877             :     const int16_t T0_frac,    /* i  : pitch fraction                        */
     878             :     int16_t *T0_min,          /* i/o: delta search min                      */
     879             :     int16_t *T0_max           /* o  : delta search max                      */
     880             : )
     881             : {
     882             :     int16_t pitch_index;
     883             : 
     884     2730480 :     if ( nBits == 10 ) /* absolute encoding with 10 bits */
     885             :     {
     886      834508 :         if ( limit_flag == 0 )
     887             :         {
     888           0 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN * 4 );
     889             :         }
     890      834508 :         else if ( limit_flag == 1 )
     891             :         {
     892      753019 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_EXTEND * 4 );
     893             :         }
     894             :         else /* limit_flag == 2 */
     895             :         {
     896       81489 :             pitch_index = T0 * 4 + T0_frac - ( PIT_MIN_DOUBLEEXTEND * 4 );
     897             :         }
     898             :     }
     899     1895972 :     else if ( nBits == 9 ) /* absolute encoding with 9 bits */
     900             :     {
     901      391135 :         pitch_index = abs_pit_enc( 4, limit_flag, T0, T0_frac );
     902             : 
     903             :         /* find T0_min and T0_max for delta search */
     904      391135 :         if ( Opt_AMR_WB )
     905             :         {
     906        7524 :             limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     907             :         }
     908             :     }
     909     1504837 :     else if ( nBits == 8 ) /* absolute encoding with 8 bits */
     910             :     {
     911       26623 :         pitch_index = abs_pit_enc( 2, limit_flag, T0, T0_frac );
     912             : 
     913             :         /* find T0_min and T0_max for delta search */
     914       26623 :         if ( Opt_AMR_WB )
     915             :         {
     916         910 :             limit_T0( L_FRAME, delta, pit_flag, 0, T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     917             :         }
     918             :     }
     919     1478214 :     else if ( nBits == 6 ) /* relative encoding with 6 bits */
     920             :     {
     921     1171792 :         pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
     922             :     }
     923      306422 :     else if ( nBits == 5 ) /* relative encoding with 5 bits */
     924             :     {
     925      306422 :         if ( delta == 8 )
     926             :         {
     927       51170 :             pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
     928             :         }
     929             :         else /* delta == 4 */
     930             :         {
     931      255252 :             pitch_index = delta_pit_enc( 4, T0, T0_frac, *T0_min );
     932             :         }
     933             :     }
     934             :     else /* nBits == 4 ) */ /* relative encoding with 4 bits */
     935             :     {
     936           0 :         if ( delta == 8 )
     937             :         {
     938           0 :             pitch_index = delta_pit_enc( 0, T0, T0_frac, *T0_min );
     939             :         }
     940             :         else /* delta == 4 */
     941             :         {
     942           0 :             pitch_index = delta_pit_enc( 2, T0, T0_frac, *T0_min );
     943             :         }
     944             :     }
     945             : 
     946     2730480 :     if ( !Opt_AMR_WB )
     947             :     {
     948             :         /* find T0_min and T0_max for delta search */
     949     2712872 :         limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
     950             :     }
     951             : 
     952     2730480 :     push_indice( hBstr, IND_PITCH, pitch_index, nBits );
     953             : 
     954     2730480 :     return;
     955             : }
     956             : 
     957             : /*-------------------------------------------------------------------*
     958             :  * pit16k_Q_enc()
     959             :  *
     960             :  * Encode subframe pitch lag @16kHz core
     961             :  *-------------------------------------------------------------------*/
     962             : 
     963     3617100 : void pit16k_Q_enc(
     964             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle                */
     965             :     const int16_t nBits,      /* i  : # of Q bits                             */
     966             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) Q limits   */
     967             :     const int16_t T0,         /* i  : integer pitch lag                       */
     968             :     const int16_t T0_frac,    /* i  : pitch fraction                          */
     969             :     int16_t *T0_min,          /* i/o: delta search min                        */
     970             :     int16_t *T0_max           /* o  : delta search max                        */
     971             : )
     972             : {
     973             :     int16_t pitch_index;
     974             : 
     975     3617100 :     if ( nBits == 10 ) /* absolute encoding with 10 bits */
     976             :     {
     977     1517808 :         if ( T0 < PIT16k_FR2_EXTEND_10b )
     978             :         {
     979     1486333 :             pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
     980             :         }
     981             :         else
     982             :         {
     983       31475 :             pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_10b * 2 ) + ( ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 );
     984             :         }
     985             : 
     986     1517808 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
     987             :     }
     988     2099292 :     else if ( nBits == 9 ) /* absolute encoding with 9 bits */
     989             :     {
     990             :         {
     991             :             /*-------------------------------------------------------------------*
     992             :              * The pitch range is encoded absolutely with 9 bits
     993             :              * and is divided as follows:
     994             :              *   PIT16k_EXTEND_MIN    to PIT16k_FR2_EXTEND_9b-1  resolution 1/4 (frac = 0,1,2 or 3)
     995             :              *   PIT16k_FR2_EXTEND_9b to PIT16k_FR1_EXTEND_9b-1  resolution 1/2 (frac = 0 or 2)
     996             :              *   PIT16k_FR1_EXTEND_9b to PIT16k_MAX              resolution 1   (frac = 0)
     997             :              *-------------------------------------------------------------------*/
     998             : 
     999           0 :             if ( T0 < PIT16k_FR2_EXTEND_9b )
    1000             :             {
    1001           0 :                 pitch_index = T0 * 4 + T0_frac - ( PIT16k_MIN_EXTEND * 4 );
    1002             :             }
    1003           0 :             else if ( T0 < PIT16k_FR1_EXTEND_9b )
    1004             :             {
    1005           0 :                 pitch_index = T0 * 2 + ( T0_frac >> 1 ) - ( PIT16k_FR2_EXTEND_9b * 2 ) + ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 );
    1006             :             }
    1007             :             else
    1008             :             {
    1009           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 );
    1010             :             }
    1011             :         }
    1012             : 
    1013           0 :         push_indice( hBstr, IND_PITCH, pitch_index, 9 );
    1014             :     }
    1015             :     else /* nBits == 6 */ /* relative encoding with 6 bits */
    1016             :     {
    1017     2099292 :         pitch_index = ( T0 - *T0_min ) * 4 + T0_frac;
    1018             : 
    1019     2099292 :         push_indice( hBstr, IND_PITCH, pitch_index, nBits );
    1020             :     }
    1021             : 
    1022     3617100 :     limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, T0, T0_frac, T0_min, T0_max );
    1023             : 
    1024     3617100 :     return;
    1025             : }
    1026             : 
    1027             : 
    1028             : /*------------------------------------------------------------------*
    1029             :  * limit_T0_voiced2:
    1030             :  *
    1031             :  *
    1032             :  *------------------------------------------------------------------*/
    1033             : 
    1034       21913 : static void limit_T0_voiced2(
    1035             :     const int16_t res,
    1036             :     const int16_t *T_op,
    1037             :     int16_t *T0_min,
    1038             :     int16_t *T0_min_frac,
    1039             :     int16_t *T0_max,
    1040             :     int16_t *T0_max_frac,
    1041             :     const int16_t pit_min,
    1042             :     const int16_t pit_max,
    1043             :     const int16_t i_subfr )
    1044             : {
    1045             :     int16_t t, temp1, temp2;
    1046             : 
    1047             :     /* Lower-bound */
    1048       21913 :     if ( i_subfr == 0 )
    1049             :     {
    1050       14362 :         temp1 = ( T_op[0] * res ) - 32;
    1051             :     }
    1052             :     else
    1053             :     {
    1054        7551 :         temp1 = ( T_op[1] * res ) - 32;
    1055             :     }
    1056             : 
    1057       21913 :     if ( T_op[0] < T_op[1] )
    1058             :     {
    1059        6837 :         t = ( T_op[0] * res ) - 16;
    1060             :     }
    1061             :     else
    1062             :     {
    1063       15076 :         t = ( T_op[1] * res ) - 16;
    1064             :     }
    1065             : 
    1066       21913 :     if ( temp1 < t )
    1067             :     {
    1068       18763 :         temp1 = t;
    1069             :     }
    1070             : 
    1071       21913 :     temp2 = temp1 / res;
    1072       21913 :     *T0_min = temp2;
    1073       21913 :     *T0_min_frac = temp1 - temp2 * res;
    1074             : 
    1075       21913 :     if ( *T0_min < pit_min )
    1076             :     {
    1077         510 :         *T0_min = pit_min;
    1078         510 :         *T0_min_frac = 0;
    1079             :     }
    1080             : 
    1081             :     /* Higher-bound */
    1082       21913 :     temp1 = ( *T0_min * res ) + *T0_min_frac + 64 - 1;
    1083             : 
    1084       21913 :     if ( T_op[0] < T_op[1] )
    1085             :     {
    1086        6837 :         t = ( T_op[1] * res ) + 16 + res - 1;
    1087             :     }
    1088             :     else
    1089             :     {
    1090       15076 :         t = ( T_op[0] * res ) + 16 + res - 1;
    1091             :     }
    1092             : 
    1093       21913 :     if ( temp1 > t )
    1094             :     {
    1095       19301 :         temp1 = t;
    1096             :     }
    1097             : 
    1098       21913 :     temp2 = temp1 / res;
    1099       21913 :     *T0_max = temp2;
    1100       21913 :     *T0_max_frac = temp1 - temp2 * res;
    1101             : 
    1102       21913 :     if ( *T0_max > pit_max )
    1103             :     {
    1104         222 :         *T0_max = pit_max;
    1105         222 :         *T0_max_frac = res - 1;
    1106         222 :         temp1 = ( *T0_max * res ) - 64 + res;
    1107         222 :         temp2 = temp1 / res;
    1108         222 :         *T0_min = temp2;
    1109         222 :         *T0_min_frac = temp1 - temp2 * res;
    1110             :     }
    1111             : 
    1112       21913 :     return;
    1113             : }
    1114             : 
    1115             : 
    1116             : /*------------------------------------------------------------------*
    1117             :  * Mode2_pit_encode:
    1118             :  *
    1119             :  * Close-loop pitch lag search and pitch lag quantization
    1120             :  * Adaptive excitation construction
    1121             :  *------------------------------------------------------------------*/
    1122             : 
    1123       86723 : void Mode2_pit_encode(
    1124             :     const int16_t coder_type, /* i  : coding model                               */
    1125             :     const int16_t i_subfr,    /* i  : subframe index                             */
    1126             :     int16_t **pt_indice,      /* i/o: quantization indices pointer               */
    1127             :     float *exc,               /* i/o: pointer to excitation signal frame         */
    1128             :     const int16_t *T_op,      /* i  : open loop pitch estimates in current frame */
    1129             :     int16_t *T0_min,          /* i/o: lower limit for close-loop search          */
    1130             :     int16_t *T0_min_frac,     /* i/o: lower limit for close-loop search          */
    1131             :     int16_t *T0_max,          /* i/o: higher limit for close-loop search         */
    1132             :     int16_t *T0_max_frac,     /* i/o: higher limit for close-loop search         */
    1133             :     int16_t *T0,              /* i/o: close loop integer pitch                   */
    1134             :     int16_t *T0_frac,         /* i/o: close loop fractional part of the pitch    */
    1135             :     int16_t *T0_res,          /* i/o: close loop pitch resolution                */
    1136             :     float *h1,                /* i  : weighted filter impulse response           */
    1137             :     float *xn,                /* i  : target vector                              */
    1138             :     const int16_t pit_min,
    1139             :     const int16_t pit_fr1,
    1140             :     const int16_t pit_fr1b,
    1141             :     const int16_t pit_fr2,
    1142             :     const int16_t pit_max,
    1143             :     const int16_t pit_res_max )
    1144             : {
    1145             :     int16_t pit_flag;
    1146             : 
    1147             :     /* Pitch flag */
    1148       86723 :     pit_flag = i_subfr;
    1149             : 
    1150       86723 :     if ( i_subfr == ( 2 * L_SUBFR ) )
    1151             :     {
    1152       18493 :         pit_flag = 0;
    1153             :     }
    1154             : 
    1155             :     /*-----------------------------------------------------------------*
    1156             :      *  - Limit range of pitch search
    1157             :      *  - Fractional pitch search
    1158             :      *  - Pitch quantization
    1159             :      *-----------------------------------------------------------------*/
    1160             : 
    1161       86723 :     if ( coder_type == 0 ) /*Unvoiced Coding do nothing*/
    1162             :     {
    1163           0 :         *T0 = L_SUBFR;
    1164           0 :         *T0_frac = 0;
    1165           0 :         *T0_res = 1;
    1166             :     }
    1167       86723 :     else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
    1168             :     {
    1169        3156 :         if ( i_subfr == 0 )
    1170             :         {
    1171         789 :             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 );
    1172             :         }
    1173             :         else
    1174             :         {
    1175        2367 :             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 );
    1176             :         }
    1177             : 
    1178        3156 :         *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 );
    1179             : 
    1180        3156 :         if ( i_subfr == 0 )
    1181             :         {
    1182         789 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1183             :         }
    1184             :         else
    1185             :         {
    1186        2367 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1187             :         }
    1188             :     }
    1189       83567 :     else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
    1190             :     {
    1191             : 
    1192        7948 :         if ( i_subfr == 0 )
    1193             :         {
    1194        1987 :             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 );
    1195             :         }
    1196        5961 :         else if ( i_subfr == 2 * L_SUBFR )
    1197             :         {
    1198        1987 :             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 );
    1199             :         }
    1200             :         else
    1201             :         {
    1202        3974 :             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 );
    1203             :         }
    1204             : 
    1205        7948 :         *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 );
    1206             : 
    1207        7948 :         if ( pit_flag == 0 )
    1208             :         {
    1209        3974 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1210             :         }
    1211             :         else
    1212             :         {
    1213        3974 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1214             :         }
    1215             :     }
    1216       75619 :     else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
    1217             :     {
    1218       33051 :         int16_t pit_res_max2 = pit_res_max;
    1219             : 
    1220       33051 :         if ( pit_min == PIT_MIN_16k )
    1221             :         {
    1222             : 
    1223       29035 :             pit_res_max2 = pit_res_max >> 1;
    1224             :         }
    1225             : 
    1226       33051 :         if ( i_subfr == 0 )
    1227             :         {
    1228             : 
    1229        6811 :             limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
    1230             :         }
    1231             :         else
    1232             :         {
    1233       26240 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1234             :         }
    1235             : 
    1236       33051 :         *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 );
    1237             : 
    1238       33051 :         if ( i_subfr == 0 ) /* if 1st subframe */
    1239             :         {
    1240        6811 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    1241             :         }
    1242             :         else
    1243             :         {
    1244       26240 :             Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    1245             :         }
    1246             :     }
    1247       42568 :     else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
    1248             :     {
    1249       37148 :         int16_t pit_res_max2 = pit_res_max;
    1250             : 
    1251       37148 :         if ( pit_min == PIT_MIN_16k )
    1252             :         {
    1253             : 
    1254       34720 :             pit_res_max2 = pit_res_max >> 1;
    1255             :         }
    1256             : 
    1257             : 
    1258       37148 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_SUBFR ) )
    1259             :         {
    1260             : 
    1261       15102 :             limit_T0_voiced2( pit_res_max2, T_op, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max, i_subfr );
    1262             :         }
    1263             :         else
    1264             :         {
    1265       22046 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
    1266             :         }
    1267             : 
    1268       37148 :         *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 );
    1269             : 
    1270       37148 :         if ( pit_flag == 0 ) /* if 1st/3rd/5th subframe */
    1271             :         {
    1272       15102 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
    1273             :         }
    1274             :         else /* if subframe 2 or 4 */
    1275             :         {
    1276       22046 :             Mode2_delta_pit_enc( *T0, *T0_frac, pit_res_max2, *T0_min, *T0_min_frac, pt_indice );
    1277             :         }
    1278             :     }
    1279        5420 :     else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
    1280             :     {
    1281        3524 :         if ( i_subfr == 0 )
    1282             :         {
    1283         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 );
    1284             :         }
    1285             :         else
    1286             :         {
    1287        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 );
    1288             :         }
    1289        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 );
    1290             : 
    1291        3524 :         if ( i_subfr == 0 )
    1292             :         {
    1293         881 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1294             :         }
    1295             :         else
    1296             :         {
    1297        2643 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1298             :         }
    1299             :     }
    1300        1896 :     else if ( coder_type == 9 ) /* 8/0/8/0 (RF mode Gen pred) */
    1301             :     {
    1302        1896 :         if ( i_subfr == 0 )
    1303             :         {
    1304         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 );
    1305             :         }
    1306             :         else
    1307             :         {
    1308        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 );
    1309             :         }
    1310        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 );
    1311             : 
    1312        1896 :         if ( i_subfr == 0 )
    1313             :         {
    1314         474 :             Mode2_abs_pit_enc( *T0, *T0_frac, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
    1315             :         }
    1316             :         else
    1317             :         {
    1318        1422 :             Mode2_delta_pit_enc( *T0, *T0_frac, ( pit_res_max >> 1 ), *T0_min, *T0_min_frac, pt_indice );
    1319             :         }
    1320             :     }
    1321             :     else
    1322             :     {
    1323           0 :         assert( 0 );
    1324             :     }
    1325             : 
    1326       86723 :     return;
    1327             : }
    1328             : 
    1329             : 
    1330             : /*-------------------------------------------------------------------*
    1331             :  * Mode2_abs_pit_enc:
    1332             :  *
    1333             :  * Encode pitch lag absolutely
    1334             :  *-------------------------------------------------------------------*/
    1335             : 
    1336       28031 : void Mode2_abs_pit_enc(
    1337             :     const int16_t T0,      /* i  : integer pitch lag              */
    1338             :     const int16_t T0_frac, /* i  : pitch fraction                 */
    1339             :     int16_t **pt_indice,   /* i/o: pointer to Vector of Q indexes */
    1340             :     const int16_t pit_min,
    1341             :     const int16_t pit_fr1,
    1342             :     const int16_t pit_fr2,
    1343             :     const int16_t pit_res_max )
    1344             : {
    1345             :     int16_t pit_res_max_half;
    1346             : 
    1347       28031 :     pit_res_max_half = pit_res_max >> 1;
    1348             : 
    1349       28031 :     if ( T0 < pit_fr2 )
    1350             :     {
    1351        2049 :         **pt_indice = T0 * pit_res_max + T0_frac - ( pit_min * pit_res_max );
    1352             :     }
    1353       25982 :     else if ( T0 < pit_fr1 )
    1354             :     {
    1355       20475 :         **pt_indice = T0 * pit_res_max_half + T0_frac - ( pit_fr2 * pit_res_max_half ) + ( ( pit_fr2 - pit_min ) * pit_res_max );
    1356             :     }
    1357             :     else
    1358             :     {
    1359        5507 :         **pt_indice = T0 - pit_fr1 + ( ( pit_fr2 - pit_min ) * pit_res_max ) + ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
    1360             :     }
    1361             : 
    1362       28031 :     ( *pt_indice )++;
    1363             : 
    1364       28031 :     return;
    1365             : }
    1366             : 
    1367             : 
    1368             : /*-------------------------------------------------------------------*
    1369             :  * Mode2_delta_pit_enc:
    1370             :  *
    1371             :  * Encode pitch lag differentially
    1372             :  *-------------------------------------------------------------------*/
    1373             : 
    1374       58692 : void Mode2_delta_pit_enc(
    1375             :     const int16_t T0,          /* i  : integer pitch lag              */
    1376             :     const int16_t T0_frac,     /* i  : pitch fraction                 */
    1377             :     const int16_t T0_res,      /* i  : pitch resolution               */
    1378             :     const int16_t T0_min,      /* i  : delta search min               */
    1379             :     const int16_t T0_min_frac, /* i  : delta search min               */
    1380             :     int16_t **pt_indice        /* o  : pointer to Vector of Q indexes */
    1381             : )
    1382             : {
    1383             : 
    1384       58692 :     **pt_indice = ( T0 - T0_min ) * T0_res + T0_frac - T0_min_frac;
    1385             : 
    1386       58692 :     ( *pt_indice )++;
    1387             : 
    1388       58692 :     return;
    1389             : }

Generated by: LCOV version 1.14