LCOV - code coverage report
Current view: top level - lib_enc - transition_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 351 351 100.0 %
Date: 2025-05-28 04:28:20 Functions: 4 4 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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-----------------------------------------------------------------*
      49             :  * Local function prototypes
      50             :  *-----------------------------------------------------------------*/
      51             : 
      52             : static void gain_trans_enc( float *gain_trans, float exc[], int16_t *quant_index, int16_t *quant_sign );
      53             : 
      54             : static void tc_enc( Encoder_State *st, const int16_t i_subfr, int16_t *tc_subfr, int16_t *position, const float *h1, const float *xn, float *exc, float *y1, int16_t *T0_min, int16_t *T0_max, int16_t *T0, int16_t *T0_frac, float *gain_pit, float g_corr[], float *bwe_exc );
      55             : 
      56             : 
      57             : /*-----------------------------------------------------------------*
      58             :  * transition_enc()
      59             :  *
      60             :  * Principal function for adaptive excitation construction in TC
      61             :  *-----------------------------------------------------------------*/
      62             : 
      63      698183 : void transition_enc(
      64             :     Encoder_State *st,     /* i/o: encoder state structure                     */
      65             :     const int16_t i_subfr, /* i  : subframe index                              */
      66             :     int16_t *tc_subfr,     /* i/o: TC subframe index                           */
      67             :     int16_t *Jopt_flag,    /* i  : joint optimization flag                     */
      68             :     int16_t *position,     /* i/o: maximum of residual signal index            */
      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             :     int16_t *T0_min,       /* i/o: lower limit for close-loop search           */
      72             :     int16_t *T0_max,       /* i/o: higher limit for close-loop search          */
      73             :     float *exc,            /* i/o: pointer to excitation signal frame          */
      74             :     float *y1,             /* o  : zero-memory filtered adaptive excitation    */
      75             :     const float *h1,       /* i  : weighted filter input response              */
      76             :     const float *xn,       /* i  : target vector                               */
      77             :     float *xn2,            /* o  : target vector for innovation search         */
      78             :     float *gp_cl,          /* i/o: memory of gain of pitch clipping algorithm  */
      79             :     float *gain_pit,       /* o  : adaptive excitation gain                    */
      80             :     float *g_corr,         /* o  : ACELP correlation values                    */
      81             :     int16_t *clip_gain,    /* i/o: adaptive gain clipping flag                 */
      82             :     float **pt_pitch,      /* o  : floating pitch values                       */
      83             :     float *bwe_exc,        /* o  : excitation for SWB TBE                      */
      84             :     int16_t *unbits_ACELP  /* i/o: unused bits                                 */
      85             : )
      86             : {
      87             :     int16_t i, pit_flag, pit_start, pit_limit, index, nBits;
      88             :     int16_t limit_flag, mult_Top;
      89             :     int16_t lp_select, lp_flag;
      90      698183 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
      91             :     int16_t offset;
      92             :     int16_t T_op[2];
      93             : 
      94             :     /* set limit_flag to 0 for restrained limits, and 1 for extended limits */
      95      698183 :     limit_flag = 0;
      96             : 
      97      698183 :     pit_start = PIT_MIN;
      98             : 
      99      698183 :     if ( st->L_frame == L_FRAME16k )
     100             :     {
     101      397335 :         T_op[0] = (int16_t) ( st->pitch[0] * 1.25f + 0.5f );
     102      397335 :         T_op[1] = (int16_t) ( st->pitch[1] * 1.25f + 0.5f );
     103             :     }
     104             :     else
     105             :     {
     106      300848 :         T_op[0] = st->pitch[0];
     107      300848 :         T_op[1] = st->pitch[1];
     108             :     }
     109             : 
     110      698183 :     lp_flag = st->acelp_cfg.ltf_mode;
     111             : 
     112      698183 :     if ( i_subfr == 0 )
     113             :     {
     114      154679 :         mult_Top = 1;
     115      154679 :         if ( limit_flag == 0 )
     116             :         {
     117      154679 :             if ( st->L_frame == L_FRAME && T_op[1] < PIT_MIN )
     118             :             {
     119       12129 :                 mult_Top = 2;
     120             :             }
     121             : 
     122      154679 :             if ( st->L_frame == L_FRAME16k && T_op[1] < PIT16k_MIN )
     123             :             {
     124       11595 :                 mult_Top = 2;
     125             :             }
     126             :         }
     127             : 
     128      154679 :         limit_T0( st->L_frame, 8, 0, limit_flag, mult_Top * T_op[1], 0, T0_min, T0_max );
     129             :     }
     130             : 
     131             :     /*-----------------------------------------------------------------*
     132             :      * zero adaptive excitation signal construction
     133             :      *-----------------------------------------------------------------*/
     134             : 
     135      698183 :     if ( *tc_subfr > i_subfr )
     136             :     {
     137      217627 :         *gain_pit = 0.0f;
     138      217627 :         *clip_gain = 0;
     139      217627 :         g_corr[0] = 1.0f;
     140      217627 :         g_corr[1] = 1.0f;
     141             : 
     142      217627 :         set_f( &exc[i_subfr], 0, L_SUBFR ); /* set excitation for current subrame to 0 */
     143             : 
     144      217627 :         if ( st->L_frame == L_FRAME )
     145             :         {
     146       86913 :             set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) ); /* set past excitation buffer to 0 */
     147             :         }
     148             :         else
     149             :         {
     150      130714 :             set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
     151             :         }
     152             : 
     153      217627 :         set_f( y1, 0, L_SUBFR );   /* set filtered adaptive excitation to 0 */
     154      217627 :         mvr2r( xn, xn2, L_SUBFR ); /* target vector for codebook search */
     155      217627 :         *T0 = L_SUBFR;
     156      217627 :         *T0_frac = 0;
     157             : 
     158      217627 :         **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     159             :     }
     160             : 
     161             :     /*-----------------------------------------------------------------*
     162             :      * glottal codebook contribution construction
     163             :      *-----------------------------------------------------------------*/
     164             : 
     165      480556 :     else if ( *tc_subfr == i_subfr )
     166             :     {
     167      154679 :         if ( st->L_frame == L_FRAME )
     168             :         {
     169       75212 :             set_f( bwe_exc - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
     170             :         }
     171             :         else
     172             :         {
     173       79467 :             set_f( bwe_exc - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
     174             :         }
     175             : 
     176      154679 :         tc_enc( st, i_subfr, tc_subfr, position, h1, xn, exc, y1, T0_min, T0_max, T0, T0_frac, gain_pit, g_corr, bwe_exc );
     177             : 
     178      154679 :         if ( *tc_subfr == TC_0_0 )
     179             :         {
     180             :             /* this is called only to compute unused bits */
     181       19999 :             config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, L_FRAME, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, TC_0_0, 3, NULL, unbits_ACELP, st->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st->idchan, st->active_cnt, 0 /*tdm_Pitch_reuse_flag*/, st->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
     182             :         }
     183             : 
     184      154679 :         *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
     185             : 
     186      154679 :         updt_tar( xn, xn2, y1, *gain_pit, L_SUBFR );
     187             : 
     188      154679 :         **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     189      154679 :         *Jopt_flag = 1;
     190             :     }
     191             : 
     192             :     /*--------------------------------------------------------------*
     193             :      * other subframes -> GENERIC encoding type,
     194             :      * standard adaptive excitation contribution
     195             :      * - exemption only in case when first glottal impulse is
     196             :      * in the 1st subframe and the second one in 2nd subframe
     197             :      * and later
     198             :      *--------------------------------------------------------------*/
     199             : 
     200      325877 :     else if ( *tc_subfr < i_subfr )
     201             :     {
     202      325877 :         if ( st->L_frame == L_FRAME )
     203             :         {
     204      138723 :             *Jopt_flag = 1;
     205             : 
     206             :             /* pit_flag for T0 bits number coding determination */
     207      138723 :             if ( ( ( i_subfr - *tc_subfr ) == L_SUBFR ) || ( ( i_subfr - *tc_subfr ) == L_SUBFR - TC_0_0 ) )
     208             :             {
     209       53639 :                 pit_flag = 0;
     210             :             }
     211             :             else
     212             :             {
     213       85084 :                 pit_flag = L_SUBFR;
     214             :             }
     215      138723 :             if ( *tc_subfr == TC_0_0 )
     216             :             {
     217       59997 :                 if ( i_subfr == L_SUBFR )
     218             :                 {
     219       19999 :                     limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
     220             :                 }
     221       59997 :                 pit_flag = 1;
     222             :             }
     223             : 
     224             :             /*----------------------------------------------------------*
     225             :              * if tc_subfr==0, change tc_subfr corresponding to the
     226             :              * second glot. impulse position
     227             :              *----------------------------------------------------------*/
     228             : 
     229      138723 :             if ( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) )
     230             :             {
     231       17161 :                 if ( PIT_MIN > ( *position ) )
     232             :                 {
     233       10989 :                     pit_start = L_SUBFR - ( *position );
     234             :                 }
     235             :                 else
     236             :                 {
     237        6172 :                     pit_start = PIT_MIN;
     238             :                 }
     239       17161 :                 if ( pit_start < PIT_MIN )
     240             :                 {
     241         905 :                     pit_start = PIT_MIN;
     242             :                 }
     243             : 
     244       17161 :                 pit_limit = 2 * pit_start + ( *position );
     245             : 
     246             :                 /* Find the closed loop pitch period */
     247       17161 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, pit_limit, L_FRAME, L_SUBFR );
     248             : 
     249       17161 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     250     2762921 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     251             :                 {
     252     2745760 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     253             :                 }
     254             : 
     255       17161 :                 if ( ( *T0 ) > ( 2 * L_SUBFR - ( *position ) ) )
     256             :                 {
     257        7865 :                     if ( ( *T0 ) + ( *position ) >= 3 * L_SUBFR )
     258             :                     {
     259             :                         /* second glottal impulse is in the 4th subframe */
     260        2154 :                         *tc_subfr = TC_0_192;
     261             :                     }
     262             :                     else
     263             :                     {
     264             :                         /* second glottal impulse is in the 3rd subframe */
     265        5711 :                         *tc_subfr = TC_0_128;
     266             :                     }
     267             :                 }
     268        9296 :                 else if ( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) )
     269             :                 {
     270             :                     /* second glottal impulse is in the 2nd subframe */
     271        9296 :                     *tc_subfr = TC_0_64;
     272             :                 }
     273             :             }
     274             : 
     275      138723 :             if ( i_subfr - *tc_subfr <= L_SUBFR )
     276             :             {
     277       53639 :                 config_acelp1( ENC, st->total_brate, st->core_brate, st->core, st->extl, st->extl_brate, st->L_frame, -1, &( st->acelp_cfg ), hBstr->nb_bits_tot, TRANSITION, -1, *tc_subfr, 2, NULL, unbits_ACELP, st->element_mode, &i /*dummy*/, 0 /*tdm_lp_reuse_flag*/, 0 /*tdm_low_rate_mode*/, st->idchan, st->active_cnt, 0 /*tdm_Pitch_reuse_flag*/, st->tdm_LRTD_flag, 0 /*GSC_IVAS_mode*/ );
     278             :             }
     279             : 
     280             :             /*-----------------------------------------------------------------*
     281             :              * get number of bits for pitch encoding
     282             :              *-----------------------------------------------------------------*/
     283             : 
     284      138723 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     285             : 
     286             :             /*-----------------------------------------------------------------*
     287             :              * Find adaptive part of excitation, encode pitch period
     288             :              *-----------------------------------------------------------------*/
     289             : 
     290             :             /* first glottal impulse is in the 1st subrame */
     291      138723 :             if ( ( i_subfr == L_SUBFR ) && ( *tc_subfr >= TC_0_128 ) )
     292             :             {
     293             :                 /*--------------------------------------------------------*
     294             :                  * second glottal impulse is in the 3rd or 4th subframe
     295             :                  * - build exc[] in 2nd subframe
     296             :                  *--------------------------------------------------------*/
     297             : 
     298        7865 :                 *T0 = 2 * L_SUBFR;
     299        7865 :                 *T0_frac = 0;
     300        7865 :                 *Jopt_flag = 0;
     301             : 
     302        7865 :                 set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
     303        7865 :                 set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     304             :             }
     305      130858 :             else if ( ( i_subfr == L_SUBFR ) && ( *tc_subfr == TC_0_64 ) )
     306             :             {
     307             :                 /*--------------------------------------------------------*
     308             :                  * second glottal impulse is in the 2nd subframe,
     309             :                  * - build exc[] in 2nd subframe
     310             :                  *--------------------------------------------------------*/
     311             : 
     312        9296 :                 if ( ( *T0 ) + ( *position ) < L_SUBFR )
     313             :                 {
     314             :                     /* impulse must be in the 2nd subframe (not in 1st) */
     315          82 :                     *T0 = L_SUBFR - ( *position );
     316          82 :                     *T0_frac = 0;
     317             :                 }
     318             : 
     319        9296 :                 if ( ( *T0 ) + ( *position ) >= 2 * L_SUBFR )
     320             :                 {
     321             :                     /* impulse must be in the 2nd subframe (not in 3rd) */
     322         209 :                     *T0 = 2 * L_SUBFR - 1 - ( *position );
     323         209 :                     *T0_frac = 2;
     324             :                 }
     325             : 
     326        9296 :                 limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
     327             : 
     328             :                 /* 7bit ENCODER */
     329        9296 :                 index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
     330        9296 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     331             : 
     332             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     333        9296 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     334             : 
     335        9296 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     336     1496656 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     337             :                 {
     338     1487360 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     339             :                 }
     340             :             }
     341      121562 :             else if ( ( i_subfr == 2 * L_SUBFR ) && ( *tc_subfr == TC_0_128 ) )
     342             :             {
     343             :                 /*--------------------------------------------------------*
     344             :                  * second glottal impulse is in the 3rd subframe
     345             :                  * - build exc[] in 3rd subframe
     346             :                  *--------------------------------------------------------*/
     347             : 
     348        5711 :                 pit_start = 2 * L_SUBFR - ( *position );
     349             : 
     350        5711 :                 pit_flag = 0;
     351             : 
     352        5711 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, pit_flag, limit_flag, pit_start, 3 * L_SUBFR, L_FRAME, L_SUBFR );
     353             : 
     354        5711 :                 if ( ( *T0 ) + ( *position ) < 2 * L_SUBFR )
     355             :                 {
     356             :                     /* impulse must be in the 3rd subframe (not in 2nd) */
     357         146 :                     *T0 = 2 * L_SUBFR - ( *position );
     358         146 :                     *T0_frac = 0;
     359             :                 }
     360        5711 :                 if ( ( *T0 ) + ( *position ) >= 3 * L_SUBFR )
     361             :                 {
     362             :                     /* impulse must be in the 3rd subframe (not in 4th) */
     363           1 :                     *T0 = 3 * L_SUBFR - 1 - ( *position );
     364           1 :                     *T0_frac = 2;
     365             :                 }
     366             : 
     367        5711 :                 limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max for delta search */
     368             : 
     369        5711 :                 index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
     370        5711 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     371             : 
     372             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     373        5711 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     374             : 
     375        5711 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     376      919471 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     377             :                 {
     378      913760 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     379             :                 }
     380             :             }
     381      115851 :             else if ( ( i_subfr == 2 * L_SUBFR ) && ( *tc_subfr == TC_0_192 ) )
     382             :             {
     383             :                 /*--------------------------------------------------------*
     384             :                  * second glottal impulse is in the 4th subframe
     385             :                  * - build exc[] in 3rd subframe
     386             :                  *--------------------------------------------------------*/
     387             : 
     388        2154 :                 *T0 = 4 * L_SUBFR;
     389        2154 :                 *T0_frac = 0;
     390        2154 :                 *Jopt_flag = 0;
     391             : 
     392        2154 :                 set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
     393        2154 :                 set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     394             :             }
     395      113697 :             else if ( ( i_subfr == 3 * L_SUBFR ) && ( *tc_subfr == TC_0_192 ) )
     396             :             {
     397             :                 /*--------------------------------------------------------*
     398             :                  * second glottal impulse is in the 4th subframe
     399             :                  * - build exc[] in 4th subframe
     400             :                  *--------------------------------------------------------*/
     401             :                 /* always T0_frac = 0 */
     402        2154 :                 pit_flag = 0;
     403             : 
     404        2154 :                 *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 );
     405             : 
     406        2154 :                 if ( ( *T0 ) + ( *position ) < 3 * L_SUBFR )
     407             :                 {
     408             :                     /* impulse must be in the 4th subframe (not in 3rd) */
     409         284 :                     *T0 = 3 * L_SUBFR - ( *position );
     410         284 :                     *T0_frac = 0;
     411             :                 }
     412             : 
     413        2154 :                 pit_start = 3 * L_SUBFR - ( *position );
     414        2154 :                 pit_limit = 2 * L_FRAME - PIT_MAX - 2 - 2 * ( *position );
     415             : 
     416        2154 :                 if ( *T0 < pit_limit )
     417             :                 {
     418        1737 :                     index = ( *T0 - pit_start ) * 2 + *T0_frac / 2;
     419             :                 }
     420             :                 else
     421             :                 {
     422         417 :                     index = *T0 - pit_limit + ( pit_limit - pit_start ) * 2;
     423         417 :                     *T0_frac = 0;
     424             :                 }
     425             : 
     426        2154 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     427             : 
     428             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     429        2154 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     430             : 
     431        2154 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     432      346794 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     433             :                 {
     434      344640 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     435             :                 }
     436             :             }
     437      111543 :             else if ( ( i_subfr == 3 * L_SUBFR ) && ( *tc_subfr == TC_0_128 ) )
     438             :             {
     439             :                 /*--------------------------------------------------------*
     440             :                  * second glottal impulse in the 3rd subframe
     441             :                  * build exc[] in 4th subframe
     442             :                  *--------------------------------------------------------*/
     443             : 
     444        5711 :                 pit_flag = L_SUBFR;
     445        5711 :                 *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 );
     446        5711 :                 index = delta_pit_enc( 2, *T0, *T0_frac, *T0_min );
     447        5711 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     448             : 
     449             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     450        5711 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     451             : 
     452        5711 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     453      919471 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     454             :                 {
     455      913760 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     456             :                 }
     457             :             }
     458             : 
     459             :             /*------------------------------------------------------------*
     460             :              * first glottal impulse is NOT in the 1st subframe,
     461             :              * or two impulses are in the 1st subframe
     462             :              *------------------------------------------------------------*/
     463             :             else
     464             :             {
     465      105832 :                 if ( nBits == 8 || nBits == 5 )
     466             :                 {
     467       11554 :                     if ( !( ( *tc_subfr == 0 ) && ( i_subfr == L_SUBFR ) ) )
     468             :                     {
     469       11554 :                         *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 );
     470             :                     }
     471             :                 }
     472             :                 else
     473             :                 {
     474       94278 :                     *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 );
     475             :                 }
     476             : 
     477      105832 :                 pit_Q_enc( hBstr, 0, nBits, 8, pit_flag, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     478             : 
     479             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     480      105832 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     481             : 
     482      105832 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     483    17038952 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     484             :                 {
     485    16933120 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     486             :                 }
     487             :             }
     488             : 
     489      138723 :             if ( *Jopt_flag == 0 )
     490             :             {
     491             :                 /* adaptive/TC excitation is zero */
     492       10019 :                 mvr2r( xn, xn2, L_SUBFR );
     493       10019 :                 g_corr[0] = 0.0f;
     494       10019 :                 g_corr[1] = 0.0f;
     495       10019 :                 *clip_gain = 0;
     496             :             }
     497             :             else
     498             :             {
     499      128704 :                 *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
     500             : 
     501      128704 :                 lp_select = lp_filt_exc_enc( MODE1, TRANSITION, i_subfr, exc, h1, xn, y1, xn2, L_SUBFR, st->L_frame, g_corr, *clip_gain, gain_pit, &lp_flag );
     502             : 
     503      128704 :                 if ( lp_flag == NORMAL_OPERATION )
     504             :                 {
     505      101099 :                     push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     506             :                 }
     507             :             }
     508             : 
     509      138723 :             **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     510             : 
     511             :             /*---------------------------------------------------------------------*
     512             :              * fill the pitch buffer - needed for post-processing
     513             :              *---------------------------------------------------------------------*/
     514             : 
     515      138723 :             if ( ( *tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
     516             :             {
     517        5715 :                 ( *pt_pitch ) -= 3;
     518        5715 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     519        5715 :                 ( *pt_pitch )++;
     520        5715 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     521        5715 :                 ( *pt_pitch )++;
     522        5715 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     523        5715 :                 ( *pt_pitch )++;
     524             :             }
     525      133008 :             else if ( ( *tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
     526             :             {
     527       10764 :                 ( *pt_pitch ) -= 2;
     528       10764 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     529       10764 :                 ( *pt_pitch )++;
     530       10764 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     531       10764 :                 ( *pt_pitch )++;
     532             :             }
     533      122244 :             else if ( ( *tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
     534             :             {
     535        9296 :                 ( *pt_pitch ) -= 1;
     536        9296 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     537        9296 :                 ( *pt_pitch )++;
     538             :             }
     539      112948 :             else if ( ( *tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
     540             :             {
     541        5711 :                 ( *pt_pitch ) -= 2;
     542        5711 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     543        5711 :                 ( *pt_pitch )++;
     544        5711 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     545        5711 :                 ( *pt_pitch )++;
     546             :             }
     547      107237 :             else if ( ( *tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
     548             :             {
     549        2154 :                 ( *pt_pitch ) -= 3;
     550        2154 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     551        2154 :                 ( *pt_pitch )++;
     552        2154 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     553        2154 :                 ( *pt_pitch )++;
     554        2154 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     555        2154 :                 ( *pt_pitch )++;
     556             :             }
     557             :         }
     558             :         else /* L_frame == L_FRAME16k */
     559             :         {
     560      187154 :             if ( i_subfr >= 2 * L_SUBFR )
     561             :             {
     562      153991 :                 limit_flag = 1;
     563             :             }
     564             : 
     565      187154 :             if ( i_subfr <= 2 * L_SUBFR )
     566             :             {
     567       75711 :                 if ( i_subfr < 2 * L_SUBFR )
     568             :                 {
     569       33163 :                     mult_Top = 1;
     570       33163 :                     if ( T_op[0] < PIT16k_MIN )
     571             :                     {
     572        4238 :                         mult_Top = 2;
     573             :                     }
     574       33163 :                     limit_T0( L_FRAME16k, 8, 0, limit_flag, mult_Top * T_op[0], 0, T0_min, T0_max ); /* TC0 second subfr. */
     575             :                 }
     576             :                 else
     577             :                 {
     578       42548 :                     limit_T0( L_FRAME16k, 8, 0, limit_flag, T_op[1], 0, T0_min, T0_max ); /* TC0 third subfr., or TC64 third subfr. */
     579             :                 }
     580             :             }
     581             : 
     582             :             /*-----------------------------------------------------------------*
     583             :              * get number of bits for pitch encoding
     584             :              *-----------------------------------------------------------------*/
     585             : 
     586      187154 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     587             : 
     588             :             /*-----------------------------------------------------------------*
     589             :              * Find adaptive part of excitation, encode pitch period
     590             :              *-----------------------------------------------------------------*/
     591             : 
     592      187154 :             if ( nBits == 10 )
     593             :             {
     594       42548 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, st->L_frame, L_SUBFR );
     595       42548 :                 pit16k_Q_enc( hBstr, nBits, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     596             :             }
     597      144606 :             else if ( nBits == 8 ) /* tc_subfr==0 && i_subfr==L_SUBFR */
     598             :             {
     599             :                 /*-----------------------------------------------------------------------------*
     600             :                  * The pitch range is encoded absolutely with 8 bits and is divided as follows:
     601             :                  *   PIT16k_MIN  to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
     602             :                  *   PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR     resolution 1/2 (frac = 0 or 2)
     603             :                  *-----------------------------------------------------------------------------*/
     604             : 
     605       33163 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, limit_flag, PIT16k_FR2_TC0_2SUBFR, 2 * L_SUBFR, st->L_frame, L_SUBFR );
     606             : 
     607       33163 :                 if ( *T0_max > 2 * L_SUBFR )
     608             :                 {
     609        6193 :                     *T0 = 2 * L_SUBFR;
     610        6193 :                     *T0_frac = 0;
     611             :                 }
     612             : 
     613       33163 :                 if ( *T0 < PIT16k_FR2_TC0_2SUBFR )
     614             :                 {
     615       19832 :                     index = ( *T0 ) * 4 + ( *T0_frac ) - ( PIT16k_MIN * 4 );
     616             :                 }
     617             :                 else
     618             :                 {
     619       13331 :                     index = ( *T0 ) * 2 + ( ( *T0_frac ) >> 1 ) - ( PIT16k_FR2_TC0_2SUBFR * 2 ) + ( ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 );
     620             :                 }
     621             : 
     622       33163 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     623             :             }
     624      111443 :             else if ( nBits == 6 )
     625             :             {
     626             :                 /* delta search */
     627      111443 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, L_SUBFR, limit_flag, PIT16k_FR2_EXTEND_9b, PIT16k_FR1_EXTEND_9b, st->L_frame, L_SUBFR );
     628             : 
     629      111443 :                 index = delta_pit_enc( 4, *T0, *T0_frac, *T0_min );
     630             : 
     631      111443 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     632             :             }
     633      187154 :             if ( nBits == 6 )
     634             :             {
     635      111443 :                 limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     636             :             }
     637             : 
     638             :             /*-----------------------------------------------------------------*
     639             :              * - gain clipping test to avoid unstable synthesis
     640             :              * - LP filtering of the adaptive excitation
     641             :              * - codebook target computation
     642             :              *-----------------------------------------------------------------*/
     643             : 
     644      187154 :             if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
     645             :             {
     646        6193 :                 *gain_pit = 0.0f;
     647        6193 :                 *clip_gain = 0;
     648        6193 :                 g_corr[0] = 0.01f;
     649        6193 :                 g_corr[1] = 0.01f;
     650        6193 :                 *Jopt_flag = 0;
     651             : 
     652        6193 :                 set_f( &exc[i_subfr], 0, L_SUBFR + 1 ); /* set excitation for current subrame to 0 */
     653             : 
     654        6193 :                 push_indice( hBstr, IND_LP_FILT_SELECT, 0, 1 ); /* this bit is actually not needed */
     655             : 
     656        6193 :                 mvr2r( xn, xn2, L_SUBFR ); /* target vector for codebook search */
     657        6193 :                 set_f( y1, 0, L_SUBFR );   /* set filtered adaptive excitation to 0 */
     658        6193 :                 set_f( &bwe_exc[i_subfr * 2], 0, L_SUBFR * 2 );
     659             :             }
     660             :             else
     661             :             {
     662             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     663      180961 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     664             : 
     665    23343969 :                 for ( i = 0; i < L_SUBFR * 2; i++ )
     666             :                 {
     667    23163008 :                     bwe_exc[i + i_subfr * 2] = bwe_exc[i + i_subfr * 2 - *T0 * 2 - (int16_t) ( (float) *T0_frac * 0.5f + 4 + 0.5f ) + 4];
     668             :                 }
     669             : 
     670      180961 :                 *clip_gain = gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, TRANSITION, xn, gp_cl );
     671             : 
     672      180961 :                 lp_select = lp_filt_exc_enc( MODE1, TRANSITION, i_subfr, exc, h1, xn, y1, xn2, L_SUBFR, st->L_frame, g_corr, *clip_gain, gain_pit, &lp_flag );
     673             : 
     674      180961 :                 if ( lp_flag == NORMAL_OPERATION )
     675             :                 {
     676      180961 :                     push_indice( hBstr, IND_LP_FILT_SELECT, lp_select, 1 );
     677             :                 }
     678             : 
     679      180961 :                 *Jopt_flag = 1;
     680             :             }
     681             : 
     682      187154 :             **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch value  */
     683             : 
     684             :             /*---------------------------------------------------------------------*
     685             :              * fill the pitch buffer - needed for post-processing
     686             :              *---------------------------------------------------------------------*/
     687             : 
     688      187154 :             if ( ( i_subfr - *tc_subfr == L_SUBFR ) || ( *tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
     689             :             {
     690       90762 :                 index = i_subfr / L_SUBFR;
     691       90762 :                 ( *pt_pitch ) -= index;
     692             : 
     693      257929 :                 for ( i = 0; i < index; i++ )
     694             :                 {
     695      167167 :                     **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     696      167167 :                     ( *pt_pitch )++;
     697             :                 }
     698             :             }
     699             :         }
     700             :     }
     701             : 
     702      698183 :     return;
     703             : }
     704             : 
     705             : 
     706             : /*-------------------------------------------------------------------------------------------*
     707             :  * tc_enc()
     708             :  *
     709             :  * Principal function for transition coding (TC) in encoder.
     710             :  * Glottal codebook contribution part:
     711             :  *
     712             :  *           |----|             |----|                                      xn
     713             :  *  imp_pos->||   |  imp_shape->| g1 |                                       |
     714             :  *           | |  |             | g2 |     ----   exc  |---|  y1   ----      |
     715             :  *           |  | |-------------|    |----|gain|-------| h |------|gain|----(-)---> xn2
     716             :  *           |   ||             | gn |     ----        |---|       ----
     717             :  *           |----|             |----|
     718             :  *          codebook          excitation gain_trans    h_orig     gain_pit
     719             :  *
     720             :  *-------------------------------------------------------------------------------------------*/
     721             : 
     722      154679 : static void tc_enc(
     723             :     Encoder_State *st,     /* i/o: encoder state structure                 */
     724             :     const int16_t i_subfr, /* i  : subrame index                           */
     725             :     int16_t *tc_subfr,     /* i/o: TC subframe index                       */
     726             :     int16_t *position,     /* i/o: index of the residual signal maximum    */
     727             :     const float *h1,       /* i  : weighted filter input response          */
     728             :     const float *xn,       /* i  : target signal                           */
     729             :     float *exc,            /* o  : glottal codebook contribution           */
     730             :     float *y1,             /* o  : filtered glottal codebook contribution  */
     731             :     int16_t *T0_min,       /* o  : lower pitch limit                       */
     732             :     int16_t *T0_max,       /* o  : higher pitch limit                      */
     733             :     int16_t *T0,           /* o  : close loop integer pitch                */
     734             :     int16_t *T0_frac,      /* o  : close loop fractional part of the pitch */
     735             :     float *gain_pit,       /* o  : pitch gain  (0..GAIN_PIT_MAX)           */
     736             :     float g_corr[],        /* o  : correlations <y1,y1>  and -2<xn,y1>     */
     737             :     float *bwe_exc         /* i/o: excitation for SWB TBE                  */
     738             : )
     739             : {
     740             :     int16_t imp_shape, imp_pos, imp_sign, imp_gain, index, nBits;
     741             :     float gain_trans;
     742      154679 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     743             : 
     744      154679 :     imp_pos = *position - i_subfr;
     745             : 
     746             :     /*-----------------------------------------------------------------*
     747             :      * get number of bits for pitch encoding
     748             :      *-----------------------------------------------------------------*/
     749             : 
     750      154679 :     nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     751             : 
     752             :     /*--------------------------------------------------------------*
     753             :      * Closed loop pitch search
     754             :      *--------------------------------------------------------------*/
     755             : 
     756      154679 :     *T0_frac = 0;
     757      154679 :     if ( st->L_frame == L_FRAME )
     758             :     {
     759       75212 :         if ( ( *T0_min <= L_SUBFR ) || ( *tc_subfr == 3 * L_SUBFR ) )
     760             :         {
     761       52050 :             if ( nBits == 9 )
     762             :             {
     763       21438 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_FR2_9b, PIT_FR1_9b, L_FRAME, L_SUBFR );
     764             :             }
     765       30612 :             else if ( nBits == 6 )
     766             :             {
     767       23807 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MIN, L_SUBFR, L_FRAME, L_SUBFR );
     768             :             }
     769             :             else
     770             :             {
     771        6805 :                 *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT_MAX, PIT_MIN, L_FRAME, L_SUBFR );
     772             :             }
     773             :         }
     774             :         else
     775             :         {
     776       23162 :             *T0 = L_SUBFR;
     777             :         }
     778             : 
     779       75212 :         if ( ( *tc_subfr == L_SUBFR ) && ( *T0 < L_SUBFR ) )
     780             :         {
     781        3185 :             *T0 = L_SUBFR;
     782             :         }
     783             :     }
     784             :     else /* L_frame == L_FRAME16k */
     785             :     {
     786       79467 :         if ( nBits == 10 )
     787             :         {
     788       36919 :             *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 1, PIT16k_FR2_EXTEND_10b, PIT16k_MAX, L_FRAME16k, L_SUBFR );
     789             :         }
     790       42548 :         else if ( nBits == 6 )
     791             :         {
     792             :             /* T0_frac with 1/2 sample resolution */
     793       42548 :             *T0 = pitch_fr4( &exc[i_subfr], xn, h1, *T0_min, *T0_max, T0_frac, 0, 0, PIT16k_MIN, L_SUBFR, L_FRAME16k, L_SUBFR );
     794             : 
     795       42548 :             if ( *T0 > L_SUBFR )
     796             :             {
     797       32135 :                 *T0 = L_SUBFR;
     798       32135 :                 *T0_frac = 0;
     799             :             }
     800             :         }
     801             :     }
     802             : 
     803             :     /* set tc_subfr to TC_0_0 */
     804      154679 :     if ( i_subfr == 0 && st->L_frame == L_FRAME && ( *T0 < L_SUBFR || *tc_subfr == 3 * L_SUBFR ) )
     805             :     {
     806       19999 :         *tc_subfr = TC_0_0;
     807             :     }
     808             : 
     809             :     /*--------------------------------------------------------------*
     810             :      * Builds glottal codebook contribution
     811             :      *--------------------------------------------------------------*/
     812             : 
     813      154679 :     set_impulse( xn, h1, &exc[i_subfr], y1, &imp_shape, &imp_pos, &gain_trans );
     814             : 
     815             :     /*--------------------------------------------------------------*
     816             :      * quantize gain_trans and scale glottal codebook contribution
     817             :      *--------------------------------------------------------------*/
     818             : 
     819      154679 :     gain_trans_enc( &gain_trans, &exc[i_subfr], &imp_gain, &imp_sign );
     820             : 
     821             :     /* set past excitation buffer to zeros */
     822      154679 :     set_f( exc - L_EXC_MEM, 0, L_EXC_MEM );
     823             : 
     824             :     /*--------------------------------------------------------------*
     825             :      * adapt. search of the second impulse in the same subframe
     826             :      * (when appears)
     827             :      *--------------------------------------------------------------*/
     828             : 
     829      154679 :     pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
     830             : 
     831      154679 :     if ( st->hBWE_TD != NULL )
     832             :     {
     833      154679 :         if ( st->L_frame == L_FRAME )
     834             :         {
     835       75212 :             interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
     836             :         }
     837             :         else
     838             :         {
     839       79467 :             interp_code_4over2( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
     840             :         }
     841             :     }
     842             : 
     843             :     /*--------------------------------------------------------------*
     844             :      * compute glottal-shape codebook excitation
     845             :      *--------------------------------------------------------------*/
     846             : 
     847             :     /* create filtered glottal codebook contribution */
     848      154679 :     conv( &exc[i_subfr], h1, y1, L_SUBFR );
     849             : 
     850             :     /* gain_pit computation */
     851      154679 :     *gain_pit = corr_xy1( xn, y1, g_corr, L_SUBFR, 0 );
     852             : 
     853             :     /*--------------------------------------------------------------*
     854             :      * Encode parameters and write indices
     855             :      *--------------------------------------------------------------*/
     856             : 
     857      154679 :     if ( st->L_frame == L_FRAME )
     858             :     {
     859       75212 :         if ( ( ( i_subfr != 0 ) || ( *tc_subfr == TC_0_0 ) ) && ( *tc_subfr != L_SUBFR ) )
     860             :         {
     861             :             /* write pitch index */
     862       47287 :             if ( ( *T0 >= L_SUBFR ) && ( *tc_subfr != 3 * L_SUBFR ) )
     863             :             {
     864        3260 :                 push_indice( hBstr, IND_PITCH, 0, nBits );
     865             :             }
     866       44027 :             else if ( *tc_subfr == 3 * L_SUBFR )
     867             :             {
     868       21573 :                 if ( nBits == 9 )
     869             :                 {
     870       21438 :                     index = abs_pit_enc( 4, 0, *T0, *T0_frac );
     871             :                 }
     872             :                 else
     873             :                 {
     874         135 :                     index = abs_pit_enc( 2, 0, *T0, *T0_frac );
     875             :                 }
     876             : 
     877       21573 :                 push_indice( hBstr, IND_PITCH, index, nBits );
     878             : 
     879       21573 :                 limit_T0( L_FRAME, 8, 0, 0, *T0, 0, T0_min, T0_max );
     880             :             }
     881             :             else
     882             :             {
     883       22454 :                 if ( nBits == 6 )
     884             :                 {
     885       19999 :                     index = delta_pit_enc( 2, *T0, *T0_frac, PIT_MIN - 1 );
     886       19999 :                     push_indice( hBstr, IND_PITCH, index, nBits );
     887             :                 }
     888             :                 else
     889             :                 {
     890        2455 :                     index = delta_pit_enc( 0, *T0, *T0_frac, PIT_MIN - 1 );
     891        2455 :                     push_indice( hBstr, IND_PITCH, index, nBits );
     892             :                 }
     893             :             }
     894             :         }
     895             :     }
     896             :     else /* L_frame == L_FRAME16k */
     897             :     {
     898       79467 :         if ( nBits == 10 )
     899             :         {
     900       36919 :             pit16k_Q_enc( hBstr, nBits, 1, *T0, *T0_frac, T0_min, T0_max );
     901             :         }
     902       42548 :         else if ( nBits == 6 )
     903             :         {
     904       42548 :             index = 2 * ( *T0 - PIT16k_MIN ) + *T0_frac / 2;
     905       42548 :             push_indice( hBstr, IND_PITCH, index, nBits );
     906             :         }
     907             :     }
     908             : 
     909      154679 :     push_indice( hBstr, IND_TC_IMP_SHAPE, imp_shape, 3 );
     910      154679 :     push_indice( hBstr, IND_TC_IMP_POS, imp_pos, 6 );
     911      154679 :     push_indice( hBstr, IND_TC_IMP_SIGN, imp_sign, 1 );
     912      154679 :     push_indice( hBstr, IND_TC_IMP_GAIN, imp_gain, 3 );
     913             : 
     914      154679 :     *position = imp_pos + i_subfr;
     915             : 
     916      154679 :     return;
     917             : }
     918             : 
     919             : /*-----------------------------------------------------------------*
     920             :  * gain_trans_enc()
     921             :  *
     922             :  * Quantize gain_trans of TC (gains of glottal impulses).
     923             :  * - Uses scalar quantization prototypes tbl_gain_trans_tc[N_GAIN_TC].
     924             :  * - Gains the glottal codebook contibution signal.
     925             :  *-----------------------------------------------------------------*/
     926             : 
     927      154679 : static void gain_trans_enc(
     928             :     float *gain_trans,    /*  i/o: gain for TC                   */
     929             :     float exc[],          /*  i/o: glottal codebook contribution */
     930             :     int16_t *quant_index, /*  o  : index of quantized gain_trans */
     931             :     int16_t *quant_sign   /*  o  : sign of quantized gain_trans  */
     932             : )
     933             : {
     934             :     int16_t i;
     935             : 
     936             : 
     937      154679 :     if ( *gain_trans < 0 )
     938             :     {
     939       71731 :         *gain_trans *= -1;
     940       71731 :         *quant_sign = 0;
     941             :     }
     942             :     else
     943             :     {
     944       82948 :         *quant_sign = 1;
     945             :     }
     946             : 
     947      154679 :     *quant_index = N_GAIN_TC - 1;
     948             : 
     949      710964 :     for ( i = 0; i < N_GAIN_TC - 1; i++ )
     950             :     {
     951      685679 :         if ( *gain_trans < tbl_gain_trans_tc[i] )
     952             :         {
     953      129394 :             *quant_index = i;
     954      129394 :             break;
     955             :         }
     956             :     }
     957             : 
     958             :     /* restore gain_trans */
     959      154679 :     *gain_trans = tbl_gain_trans_tc[i];
     960             : 
     961      154679 :     if ( *quant_sign == 0 )
     962             :     {
     963       71731 :         *gain_trans *= -1;
     964             :     }
     965             : 
     966    10054135 :     for ( i = 0; i < L_SUBFR; i++ )
     967             :     {
     968     9899456 :         exc[i] *= ( *gain_trans );
     969             :     }
     970             : 
     971      154679 :     return;
     972             : }
     973             : 
     974             : 
     975             : /*-----------------------------------------------------------------*
     976             :  * tc_classif_enc()
     977             :  *
     978             :  * TC subframe determination
     979             :  *-----------------------------------------------------------------*/
     980             : 
     981      154679 : void tc_classif_enc(
     982             :     const int16_t L_frame,     /* i  : length of the frame                           */
     983             :     int16_t *tc_subfr,         /* o  : TC subframe index                             */
     984             :     int16_t *position,         /* o  :  maximum of residual signal index             */
     985             :     const int16_t attack_flag, /* i  : attack flag                                   */
     986             :     const int16_t pitch,       /* i  : open loop pitch estimates for first halfframe */
     987             :     const float *res           /* i  : pointer to the LP residual signal frame       */
     988             : )
     989             : {
     990             :     float temp;
     991             :     int16_t T_op;
     992             : 
     993      154679 :     T_op = pitch;
     994      154679 :     if ( L_frame == L_FRAME16k )
     995             :     {
     996       79467 :         T_op = (int16_t) ( pitch * 1.25f + 0.5f );
     997             :     }
     998             : 
     999      154679 :     *tc_subfr = -1;
    1000      154679 :     if ( attack_flag )
    1001             :     {
    1002       64333 :         *tc_subfr = 3 * L_SUBFR;
    1003             : 
    1004       64333 :         if ( attack_flag > 0 )
    1005             :         {
    1006       64333 :             if ( L_frame == L_FRAME )
    1007             :             {
    1008       29478 :                 *tc_subfr = NB_SUBFR * ( attack_flag - 1 ) / 32 /*ATT_NSEG*/;
    1009             :             }
    1010             :             else
    1011             :             {
    1012       34855 :                 *tc_subfr = NB_SUBFR16k * ( attack_flag - 1 ) / 32 /*ATT_NSEG*/;
    1013             :             }
    1014       64333 :             *tc_subfr *= L_SUBFR;
    1015             :         }
    1016             :     }
    1017             : 
    1018      154679 :     if ( attack_flag )
    1019             :     {
    1020             :         /*-----------------------------------------------------------------*
    1021             :          * TC: subrame determination for glottal shape search
    1022             :          * -------------------------------------------------------
    1023             :          * tc_subfr == 0         - TC in 1st subframe
    1024             :          * tc_subfr == TC_0_0    - TC in 1st subframe + information about T0
    1025             :          * tc_subfr == L_SUBFR   - TC in 2nd subframe
    1026             :          * tc_subfr == 2*L_SUBFR - TC in 3rd subframe
    1027             :          * tc_subfr == 3*L_SUBFR - TC in 4th subframe
    1028             :          *-----------------------------------------------------------------*/
    1029             : 
    1030       64333 :         *position = emaximum( res + *tc_subfr, min( T_op + 2, L_SUBFR ), &temp ) + *tc_subfr;
    1031             :     }
    1032             :     else
    1033             :     {
    1034       90346 :         *position = emaximum( res, (int16_t) ( T_op + 2 ), &temp );
    1035             : 
    1036             :         /* correction in case of possibly wrong T_op (double-pitch values) */
    1037       90346 :         if ( ( L_frame == L_FRAME && T_op > 2 * PIT_MIN ) ||
    1038       44612 :              ( L_frame == L_FRAME16k && T_op > 2 * PIT16k_MIN ) )
    1039             :         {
    1040             :             int16_t position_tmp, len;
    1041             :             float aver, temp2;
    1042             : 
    1043       44245 :             len = (int16_t) ( T_op / 2 + 2 );
    1044             : 
    1045       44245 :             position_tmp = emaximum( res, len, &temp2 );
    1046       44245 :             aver = dotp( res, res, len ) + 0.01f;
    1047       44245 :             aver = sqrtf( aver / len );
    1048             : 
    1049       44245 :             temp = sqrtf( temp );
    1050       44245 :             temp2 = sqrtf( temp2 );
    1051             : 
    1052       44245 :             if ( temp2 > 0.8f * temp && aver < 0.25f * temp )
    1053             :             {
    1054        3780 :                 *position = position_tmp;
    1055             :             }
    1056             :         }
    1057             : 
    1058       90346 :         *tc_subfr = (int16_t) floor( ( *position ) / L_SUBFR ) * L_SUBFR;
    1059             :     }
    1060             : 
    1061      154679 :     return;
    1062             : }

Generated by: LCOV version 1.14