LCOV - code coverage report
Current view: top level - lib_dec - transition_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 238 244 97.5 %
Date: 2025-05-23 08:37:30 Functions: 3 3 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 tc_dec( Decoder_State *st, const int16_t L_frame, float exc[], int16_t *T0, int16_t *T0_frac, const int16_t i_subfr, const int16_t tc_subfr, int16_t *position, float bwe_exc[] );
      53             : 
      54             : 
      55             : /*-------------------------------------------------------------------*
      56             :  * transition_dec()
      57             :  *
      58             :  * Principal function for TC decoding
      59             :  *-------------------------------------------------------------------*/
      60             : 
      61      187083 : void transition_dec(
      62             :     Decoder_State *st,      /* i/o: decoder state structure                 */
      63             :     const int16_t L_frame,  /* i  : length of the frame                     */
      64             :     const int16_t i_subfr,  /* i  : subframe index                          */
      65             :     const int16_t tc_subfr, /* i  : TC subframe index                       */
      66             :     int16_t *Jopt_flag,     /* i  : joint optimization flag                 */
      67             :     float *exc,             /* o  : excitation signal                       */
      68             :     int16_t *T0,            /* o  : close loop integer pitch                */
      69             :     int16_t *T0_frac,       /* o  : close loop fractional part of the pitch */
      70             :     int16_t *T0_min,        /* i/o: delta search min for sf 2 & 4           */
      71             :     int16_t *T0_max,        /* i/o: delta search max for sf 2 & 4           */
      72             :     float **pt_pitch,       /* o  : floating pitch values                   */
      73             :     int16_t *position,      /* i/o: first glottal impulse position in frame */
      74             :     float *bwe_exc          /* o  : excitation for SWB TBE                  */
      75             : )
      76             : {
      77             :     int16_t i, pit_flag, pit_start, pit_limit, index, nBits;
      78             :     int16_t limit_flag;
      79             :     int16_t offset;
      80             : 
      81             :     /* Set limit_flag to 0 for restrained limits, and 1 for extended limits */
      82      187083 :     limit_flag = 0;
      83             : 
      84             :     /*---------------------------------------------------------------------*
      85             :      * zero adaptive contribution (glottal shape codebook search not
      86             :      *                             in first subframe(s) )
      87             :      *---------------------------------------------------------------------*/
      88             : 
      89      187083 :     if ( tc_subfr > i_subfr + TC_0_192 )
      90             :     {
      91       55389 :         set_f( &exc[i_subfr], 0, L_SUBFR );
      92             : 
      93       55389 :         if ( L_frame == L_FRAME )
      94             :         {
      95       22584 :             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 */
      96             :         }
      97             :         else
      98             :         {
      99       32805 :             set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
     100             :         }
     101             : 
     102       55389 :         *T0 = L_SUBFR;
     103       55389 :         *T0_frac = 0;
     104       55389 :         **pt_pitch = (float) L_SUBFR;
     105             :     }
     106             : 
     107             :     /*---------------------------------------------------------------------*
     108             :      * glottal shape codebook search
     109             :      *---------------------------------------------------------------------*/
     110             : 
     111      131694 :     else if ( ( tc_subfr - i_subfr >= 0 ) && ( tc_subfr - i_subfr <= TC_0_192 ) )
     112             :     {
     113       41832 :         set_f( exc - L_EXC_MEM, 0, L_EXC_MEM ); /* set past excitation buffer to 0 */
     114             : 
     115       41832 :         if ( L_frame == L_FRAME )
     116             :         {
     117       22077 :             set_f( bwe_exc - PIT_MAX * HIBND_ACB_L_FAC, 0, PIT_MAX * HIBND_ACB_L_FAC ); /* set past excitation buffer to 0 */
     118             :         }
     119             :         else
     120             :         {
     121       19755 :             set_f( bwe_exc - PIT16k_MAX * 2, 0, PIT16k_MAX * 2 ); /* set past excitation buffer to 0 */
     122             :         }
     123             : 
     124             :         /* glottal shape codebook contribution construction */
     125       41832 :         tc_dec( st, L_frame, exc, T0, T0_frac, i_subfr, tc_subfr, position, bwe_exc );
     126             : 
     127       41832 :         **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     128       41832 :         *Jopt_flag = 1;
     129             :     }
     130             : 
     131             :     /*---------------------------------------------------------------------*
     132             :      * Regular ACELP Decoding using GENERIC type decoder
     133             :      * (all subframes following subframe with glottal shape codebook seach)
     134             :      * - search the position of the 2nd glottal impulse in case that the first
     135             :      *   one is in the 1st subframe (different adaptive contribution
     136             :      *   construction and the pitch period coding is used)
     137             :      *---------------------------------------------------------------------*/
     138             : 
     139       89862 :     else if ( tc_subfr < i_subfr )
     140             :     {
     141       89862 :         if ( L_frame == L_FRAME )
     142             :         {
     143       43647 :             *Jopt_flag = 1;
     144             : 
     145       43647 :             if ( ( i_subfr - tc_subfr >= L_SUBFR ) && ( i_subfr - tc_subfr <= L_SUBFR + TC_0_192 ) )
     146             :             {
     147        5052 :                 pit_flag = 0;
     148             :             }
     149             :             else
     150             :             {
     151       38595 :                 pit_flag = L_SUBFR;
     152             :             }
     153             : 
     154       43647 :             if ( tc_subfr == TC_0_0 )
     155             :             {
     156       15426 :                 if ( i_subfr == L_SUBFR )
     157             :                 {
     158        5142 :                     limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
     159             :                 }
     160             : 
     161       15426 :                 pit_flag = 1;
     162             :             }
     163             : 
     164             :             /*-----------------------------------------------------------------*
     165             :              * get number of bits for pitch decoding
     166             :              *-----------------------------------------------------------------*/
     167             : 
     168       43647 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     169             : 
     170             :             /*------------------------------------------------------------*
     171             :              * first glottal impulse is in the 1st subframe
     172             :              *------------------------------------------------------------*/
     173             : 
     174       43647 :             if ( ( i_subfr == L_SUBFR ) && ( tc_subfr >= TC_0_128 ) )
     175             :             {
     176             :                 /*--------------------------------------------------------*
     177             :                  * second glottal impulse is in the 3rd or 4th subframe
     178             :                  * - build exc[] in 2nd subframe
     179             :                  *--------------------------------------------------------*/
     180             : 
     181        2640 :                 *T0 = 2 * L_SUBFR;
     182        2640 :                 *T0_frac = 0;
     183        2640 :                 *Jopt_flag = 0;
     184             : 
     185             :                 /* set adaptive part of exciation for curent subframe to 0 */
     186        2640 :                 set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
     187             : 
     188        2640 :                 set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     189             :             }
     190       41007 :             else if ( ( i_subfr == L_SUBFR ) && ( tc_subfr == TC_0_64 ) )
     191             :             {
     192             :                 /*--------------------------------------------------------*
     193             :                  * second glottal impulse is in the 2nd subframe,
     194             :                  * - build exc[] in 2nd subframe
     195             :                  *--------------------------------------------------------*/
     196             : 
     197        3951 :                 if ( PIT_MIN > ( *position ) )
     198             :                 {
     199        2949 :                     pit_start = L_SUBFR - ( *position );
     200             :                 }
     201             :                 else
     202             :                 {
     203        1002 :                     pit_start = PIT_MIN;
     204             :                 }
     205             : 
     206        3951 :                 if ( pit_start < PIT_MIN )
     207             :                 {
     208         216 :                     pit_start = PIT_MIN;
     209             :                 }
     210             : 
     211        3951 :                 pit_limit = 2 * pit_start + ( *position );
     212             : 
     213             :                 /* 7 bit pitch DECODER */
     214        3951 :                 index = get_next_indice( st, nBits );
     215             : 
     216        3951 :                 *T0 = (int16_t) ( floor( pit_start + index / 2 ) );
     217        3951 :                 *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
     218        3951 :                 limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
     219             : 
     220             :                 /* Find the adaptive codebook vector - ACELP long-term prediction   */
     221        3951 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     222             : 
     223        3951 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     224      636111 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     225             :                 {
     226      632160 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     227             :                 }
     228             :             }
     229       37056 :             else if ( ( i_subfr == 2 * L_SUBFR ) && ( tc_subfr == TC_0_128 ) )
     230             :             {
     231             :                 /*--------------------------------------------------------*
     232             :                  * second glottal impulse is in the 3rd subframe
     233             :                  * - build exc[] in 3rd subframe
     234             :                  *--------------------------------------------------------*/
     235             : 
     236             :                 /* 7bit pitch DECODER */
     237        1995 :                 pit_start = 2 * L_SUBFR - ( *position );
     238             : 
     239        1995 :                 index = get_next_indice( st, nBits );
     240             : 
     241        1995 :                 *T0 = (int16_t) ( floor( pit_start + (int16_t) ( index / 2 ) ) );
     242        1995 :                 *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
     243             : 
     244        1995 :                 limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max ); /* find T0_min and T0_max */
     245             : 
     246             :                 /* Find the adaptive codebook vector. ACELP long-term prediction */
     247        1995 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     248             : 
     249        1995 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     250      321195 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     251             :                 {
     252      319200 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     253             :                 }
     254             :             }
     255       35061 :             else if ( ( i_subfr == 2 * L_SUBFR ) && ( tc_subfr == TC_0_192 ) )
     256             :             {
     257             :                 /*--------------------------------------------------------*
     258             :                  * second glottal impulse is in the 4th subframe
     259             :                  * - build exc[] in 3rd subframe
     260             :                  *--------------------------------------------------------*/
     261             : 
     262         645 :                 *T0 = 4 * L_SUBFR;
     263         645 :                 *T0_frac = 0;
     264         645 :                 *Jopt_flag = 0;
     265             : 
     266             :                 /* set adaptive part of exciation for curent subframe to 0 */
     267         645 :                 set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
     268             : 
     269         645 :                 set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     270             :             }
     271       34416 :             else if ( ( i_subfr == 3 * L_SUBFR ) && ( tc_subfr == TC_0_192 ) )
     272             :             {
     273             :                 /*--------------------------------------------------------*
     274             :                  * second glottal impulse is in the 4th subframe
     275             :                  * - build exc[] in 4th subframe
     276             :                  *--------------------------------------------------------*/
     277             : 
     278         645 :                 pit_start = 3 * L_SUBFR - ( *position );
     279         645 :                 pit_limit = 2 * L_FRAME - PIT_MAX - 2 * ( *position ) - 2;
     280             : 
     281         645 :                 index = get_next_indice( st, nBits );
     282             : 
     283         645 :                 if ( index < ( pit_limit - pit_start ) * 2 )
     284             :                 {
     285         486 :                     *T0 = (int16_t) ( floor( pit_start + ( index / 2 ) ) );
     286         486 :                     *T0_frac = ( index - ( ( *T0 ) - pit_start ) * 2 ) * 2;
     287             :                 }
     288             :                 else
     289             :                 {
     290         159 :                     *T0 = index + pit_limit - ( pit_limit - pit_start ) * 2;
     291         159 :                     *T0_frac = 0;
     292             :                 }
     293             : 
     294             :                 /* biterror detection mechanism */
     295         645 :                 if ( ( ( *T0 << 2 ) + *T0_frac ) > ( PIT_MAX << 2 ) + 2 )
     296             :                 {
     297           0 :                     *T0 = L_SUBFR;
     298           0 :                     *T0_frac = 0;
     299           0 :                     st->BER_detect = 1;
     300             :                 }
     301             : 
     302             :                 /* Find the adaptive codebook vector. ACELP long-term prediction   */
     303         645 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     304             : 
     305         645 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     306      103845 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     307             :                 {
     308      103200 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     309             :                 }
     310             :             }
     311       33771 :             else if ( ( i_subfr == 3 * L_SUBFR ) && ( tc_subfr == TC_0_128 ) )
     312             :             {
     313             :                 /*--------------------------------------------------------*
     314             :                  * second glottal impulse in the 3rd subframe
     315             :                  * build exc[] in 4th subframe
     316             :                  *--------------------------------------------------------*/
     317             : 
     318        1995 :                 index = get_next_indice( st, nBits );
     319             : 
     320        1995 :                 delta_pit_dec( 2, index, T0, T0_frac, *T0_min );
     321             : 
     322             :                 /* Find the adaptive codebook vector. ACELP long-term prediction   */
     323        1995 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     324             : 
     325        1995 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     326      321195 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     327             :                 {
     328      319200 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     329             :                 }
     330             :             }
     331             : 
     332             :             /*------------------------------------------------------------*
     333             :              * first glottal impulse is NOT in the 1st subframe,
     334             :              * or two impulses are in the 1st subframe
     335             :              *------------------------------------------------------------*/
     336             :             else
     337             :             {
     338       31776 :                 index = get_next_indice( st, nBits );
     339             : 
     340       31776 :                 pit_Q_dec( 0, index, nBits, 8, pit_flag, limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     341             : 
     342             :                 /* Find the adaptive codebook vector */
     343       31776 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     344             : 
     345       31776 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     346     5115936 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     347             :                 {
     348     5084160 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     349             :                 }
     350             :             }
     351             : 
     352             :             /*-----------------------------------------------------------------*
     353             :              * LP filtering of the adaptive excitation (if non-zero)
     354             :              *-----------------------------------------------------------------*/
     355             : 
     356       43647 :             if ( *Jopt_flag )
     357             :             {
     358       40362 :                 lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
     359             :             }
     360             : 
     361             :             /*---------------------------------------------------------------------*
     362             :              * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
     363             :              *---------------------------------------------------------------------*/
     364             : 
     365       43647 :             **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     366       43647 :             if ( ( tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
     367             :             {
     368        1656 :                 ( *pt_pitch ) -= 3;
     369        1656 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     370        1656 :                 ( *pt_pitch )++;
     371        1656 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     372        1656 :                 ( *pt_pitch )++;
     373        1656 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     374        1656 :                 ( *pt_pitch )++;
     375             :             }
     376       41991 :             else if ( ( tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
     377             :             {
     378        3396 :                 ( *pt_pitch ) -= 2;
     379        3396 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     380        3396 :                 ( *pt_pitch )++;
     381        3396 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     382        3396 :                 ( *pt_pitch )++;
     383             :             }
     384       38595 :             else if ( ( tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
     385             :             {
     386        3951 :                 ( *pt_pitch ) -= 1;
     387        3951 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     388        3951 :                 ( *pt_pitch )++;
     389             :             }
     390       34644 :             else if ( ( tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
     391             :             {
     392        1995 :                 ( *pt_pitch ) -= 2;
     393        1995 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     394        1995 :                 ( *pt_pitch )++;
     395        1995 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     396        1995 :                 ( *pt_pitch )++;
     397             :             }
     398       32649 :             else if ( ( tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
     399             :             {
     400         645 :                 ( *pt_pitch ) -= 3;
     401         645 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     402         645 :                 ( *pt_pitch )++;
     403         645 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     404         645 :                 ( *pt_pitch )++;
     405         645 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     406         645 :                 ( *pt_pitch )++;
     407             :             }
     408             :         }
     409             :         else /* L_frame == L_FRAME16k */
     410             :         {
     411       46215 :             if ( i_subfr >= 2 * L_SUBFR )
     412             :             {
     413       38037 :                 limit_flag = 1;
     414             :             }
     415             : 
     416       46215 :             if ( i_subfr - tc_subfr == L_SUBFR )
     417             :             {
     418       13797 :                 limit_T0( L_FRAME16k, 8, 0, limit_flag, *T0, *T0_frac, T0_min, T0_max ); /* find T0_min and T0_max */
     419             :             }
     420             : 
     421             :             /*-----------------------------------------------------------------*
     422             :              * get number of bits and index for pitch decoding
     423             :              *-----------------------------------------------------------------*/
     424             : 
     425       46215 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     426             : 
     427       46215 :             index = get_next_indice( st, nBits );
     428             : 
     429             :             /*-----------------------------------------------------------------*
     430             :              * Find adaptive part of excitation, encode pitch period
     431             :              *-----------------------------------------------------------------*/
     432             : 
     433       46215 :             if ( nBits == 10 )
     434             :             {
     435       11136 :                 pit16k_Q_dec( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     436             :             }
     437       35079 :             else if ( nBits == 8 ) /* tc_subfr==0 && i_subfr==L_SUBFR */
     438             :             {
     439             :                 /*-----------------------------------------------------------------------------*
     440             :                  * The pitch range is encoded absolutely with 8 bits and is divided as follows:
     441             :                  *   PIT16k_MIN  to PIT16k_FR2_TC0_2SUBFR-1 resolution 1/4 (frac = 0,1,2 or 3)
     442             :                  *   PIT16k_FR2_TC0_2SUBFR to 2*L_SUBFR     resolution 1/2 (frac = 0 or 2)
     443             :                  *-----------------------------------------------------------------------------*/
     444             : 
     445        8178 :                 if ( index < ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 )
     446             :                 {
     447        4764 :                     *T0 = PIT16k_MIN + ( index / 4 );
     448        4764 :                     *T0_frac = index - ( *T0 - PIT16k_MIN ) * 4;
     449             :                 }
     450             :                 else
     451             :                 {
     452        3414 :                     index -= ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4;
     453        3414 :                     *T0 = PIT16k_FR2_TC0_2SUBFR + ( index / 2 );
     454        3414 :                     *T0_frac = index - ( *T0 - PIT16k_FR2_TC0_2SUBFR ) * 2;
     455        3414 :                     ( *T0_frac ) *= 2;
     456             :                 }
     457             : 
     458             :                 /* biterror detection mechanism */
     459        8178 :                 if ( ( ( *T0 << 2 ) + *T0_frac ) > ( ( 2 * L_SUBFR ) << 2 ) )
     460             :                 {
     461           0 :                     *T0 = L_SUBFR;
     462           0 :                     *T0_frac = 0;
     463           0 :                     st->BER_detect = 1;
     464             :                 }
     465             :             }
     466       26901 :             else if ( nBits == 6 )
     467             :             {
     468       26901 :                 delta_pit_dec( 4, index, T0, T0_frac, *T0_min );
     469             :             }
     470       46215 :             if ( nBits == 6 )
     471             :             {
     472       26901 :                 limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max ); /* find T0_min and T0_max */
     473             :             }
     474             : 
     475             :             /*-----------------------------------------------------------------*
     476             :              * - find the adaptive codebook vector
     477             :              * - LP filtering of the adaptive excitation (if non-zero)
     478             :              *-----------------------------------------------------------------*/
     479             : 
     480       46215 :             if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
     481             :             {
     482             :                 /* no adaptive excitation in the second subframe */
     483        1323 :                 set_f( &exc[i_subfr], 0, L_SUBFR + 1 );
     484             : 
     485        1323 :                 get_next_indice( st, 1 ); /* this bit is actually not needed */
     486             : 
     487        1323 :                 set_f( &bwe_exc[i_subfr * 2], 0, L_SUBFR * 2 );
     488             :             }
     489             :             else
     490             :             {
     491             :                 /* Find the adaptive codebook vector - ACELP long-term prediction */
     492       44892 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     493             : 
     494     5791068 :                 for ( i = 0; i < L_SUBFR * 2; i++ )
     495             :                 {
     496     5746176 :                     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];
     497             :                 }
     498             : 
     499       44892 :                 lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
     500             : 
     501       44892 :                 *Jopt_flag = 1;
     502             :             }
     503             : 
     504       46215 :             **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     505             : 
     506             :             /*---------------------------------------------------------------------*
     507             :              * fill the pitch buffer - needed for post-processing and FEC_clas_estim()
     508             :              *---------------------------------------------------------------------*/
     509             : 
     510       46215 :             if ( ( i_subfr - tc_subfr == L_SUBFR ) || ( tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
     511             :             {
     512       21975 :                 index = i_subfr / L_SUBFR;
     513       21975 :                 ( *pt_pitch ) -= index;
     514             : 
     515       61101 :                 for ( i = 0; i < index; i++ )
     516             :                 {
     517       39126 :                     **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     518       39126 :                     ( *pt_pitch )++;
     519             :                 }
     520             :             }
     521             :         }
     522             :     }
     523             : 
     524      187083 :     return;
     525             : }
     526             : 
     527             : /*---------------------------------------------------------------------*
     528             :  *  tc_dec()
     529             :  *
     530             :  *  Principal function for TC decoding.
     531             :  *  - constructs glottal codebook contribution
     532             :  *  - uses pitch sharpening
     533             :  *  - uses gain_trans
     534             :  *---------------------------------------------------------------------*/
     535             : 
     536       41832 : static void tc_dec(
     537             :     Decoder_State *st,      /* i/o: decoder state structure                     */
     538             :     const int16_t L_frame,  /* i  : length of the frame                         */
     539             :     float exc[],            /* o  : glottal codebook contribution               */
     540             :     int16_t *T0,            /* o  : close-loop pitch period                     */
     541             :     int16_t *T0_frac,       /* o  : close-loop pitch period - fractional part   */
     542             :     const int16_t i_subfr,  /* i  : subframe index                              */
     543             :     const int16_t tc_subfr, /* i  : TC subframe index                           */
     544             :     int16_t *position,      /* o  : first glottal impulse position in frame     */
     545             :     float bwe_exc[]         /* o  : excitation for SWB TBE                      */
     546             : )
     547             : {
     548             :     int16_t i, imp_shape, imp_pos, imp_sign, imp_gain, nBits;
     549             :     float gain_trans;
     550             :     int16_t index;
     551             : 
     552             :     /*----------------------------------------------------------------*
     553             :      * find the number of bits
     554             :      *----------------------------------------------------------------*/
     555             : 
     556       41832 :     nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     557             : 
     558             :     /*----------------------------------------------------------------*
     559             :      * decode parameter T0 (pitch period)
     560             :      *----------------------------------------------------------------*/
     561             : 
     562       41832 :     if ( L_frame == L_FRAME )
     563             :     {
     564       22077 :         if ( ( ( i_subfr == 0 ) && ( ( tc_subfr == 0 ) || ( tc_subfr == TC_0_64 ) || ( tc_subfr == TC_0_128 ) || ( tc_subfr == TC_0_192 ) ) ) || ( tc_subfr == L_SUBFR ) )
     565             :         {
     566        9987 :             *T0 = L_SUBFR;
     567        9987 :             *T0_frac = 0;
     568             :         }
     569       12090 :         else if ( ( tc_subfr == 3 * L_SUBFR ) )
     570             :         {
     571        5292 :             i = get_next_indice( st, nBits );
     572             : 
     573        5292 :             if ( nBits == 9 )
     574             :             {
     575        5244 :                 abs_pit_dec( 4, i, 0, T0, T0_frac );
     576             :             }
     577             :             else
     578             :             {
     579          48 :                 abs_pit_dec( 2, i, 0, T0, T0_frac );
     580             :             }
     581             :         }
     582             :         else
     583             :         {
     584        6798 :             i = get_next_indice( st, nBits );
     585             : 
     586        6798 :             if ( i == 0 )
     587             :             {
     588        1083 :                 *T0 = L_SUBFR;
     589        1083 :                 *T0_frac = 0;
     590             :             }
     591             :             else
     592             :             {
     593        5715 :                 if ( tc_subfr == TC_0_0 )
     594             :                 {
     595        5142 :                     delta_pit_dec( 2, i, T0, T0_frac, PIT_MIN - 1 );
     596             :                 }
     597             :                 else
     598             :                 {
     599         573 :                     delta_pit_dec( 0, i, T0, T0_frac, PIT_MIN - 1 );
     600             :                 }
     601             :             }
     602             :         }
     603             :     }
     604             :     else /* L_frame == L_FRAME16k */
     605             :     {
     606       19755 :         i = get_next_indice( st, nBits );
     607             : 
     608       19755 :         if ( nBits == 10 )
     609             :         {
     610        8619 :             if ( i < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
     611             :             {
     612        8418 :                 *T0 = PIT16k_MIN_EXTEND + ( i / 4 );
     613        8418 :                 *T0_frac = i - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
     614             :             }
     615             :             else
     616             :             {
     617         201 :                 index = i - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
     618         201 :                 *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
     619         201 :                 *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
     620         201 :                 ( *T0_frac ) *= 2;
     621             :             }
     622             :         }
     623       11136 :         else if ( nBits == 6 )
     624             :         {
     625       11136 :             *T0 = PIT16k_MIN + ( i / 2 );
     626       11136 :             *T0_frac = i - ( *T0 - PIT16k_MIN ) * 2;
     627       11136 :             *T0_frac *= 2;
     628             :         }
     629             :     }
     630             : 
     631             :     /*----------------------------------------------------------------*
     632             :      * decode other TC parameters
     633             :      *----------------------------------------------------------------*/
     634             : 
     635       41832 :     imp_shape = get_next_indice( st, 3 );
     636       41832 :     imp_pos = get_next_indice( st, 6 );
     637       41832 :     imp_sign = get_next_indice( st, 1 );
     638       41832 :     imp_gain = get_next_indice( st, 3 );
     639             : 
     640             :     /*----------------------------------------------------------------*
     641             :      * restore gain_trans
     642             :      * build glottal codebook contribution
     643             :      *----------------------------------------------------------------*/
     644             : 
     645       41832 :     gain_trans = tbl_gain_trans_tc[imp_gain];
     646             : 
     647       41832 :     if ( imp_sign == 0 )
     648             :     {
     649       20976 :         gain_trans *= -1;
     650             :     }
     651             : 
     652             :     /* build glottal codebook contribution */
     653       41832 :     set_f( &exc[i_subfr], 0, L_SUBFR );
     654             : 
     655      752976 :     for ( i = ( imp_pos - L_IMPULSE2 ); i <= ( imp_pos + L_IMPULSE2 ); i++ )
     656             :     {
     657      711144 :         if ( ( i >= 0 ) && ( i < L_SUBFR ) )
     658             :         {
     659      668442 :             exc[i + i_subfr] = glottal_cdbk[(imp_shape) *L_IMPULSE + i - imp_pos + L_IMPULSE2] * gain_trans;
     660             :         }
     661             :     }
     662             : 
     663             :     /*--------------------------------------------------------------*
     664             :      * adapt. search of the second impulse in the same subframe
     665             :      * (when appears)
     666             :      *--------------------------------------------------------------*/
     667             : 
     668       41832 :     pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
     669             : 
     670       41832 :     if ( st->hBWE_TD != NULL )
     671             :     {
     672       41832 :         if ( L_frame == L_FRAME )
     673             :         {
     674       22077 :             interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
     675             :         }
     676             :         else
     677             :         {
     678       19755 :             interp_code_4over2( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
     679             :         }
     680             :     }
     681             : 
     682       41832 :     *position = imp_pos + i_subfr;
     683             : 
     684       41832 :     return;
     685             : }
     686             : 
     687             : /*-------------------------------------------------------------------*
     688             :  * tc_classif()
     689             :  *
     690             :  * TC subframe classification decoding
     691             :  *-------------------------------------------------------------------*/
     692             : 
     693       41832 : int16_t tc_classif(
     694             :     Decoder_State *st /* i/o: decoder state structure           */
     695             : )
     696             : {
     697             :     int16_t tc_subfr, indice;
     698             : 
     699       41832 :     if ( st->L_frame == L_FRAME )
     700             :     {
     701       22077 :         if ( get_next_indice( st, 1 ) )
     702             :         {
     703        5142 :             tc_subfr = TC_0_0;
     704             :         }
     705             :         else
     706             :         {
     707       16935 :             if ( get_next_indice( st, 1 ) )
     708             :             {
     709        6591 :                 tc_subfr = 0;
     710             : 
     711        6591 :                 if ( get_next_indice( st, 1 ) )
     712             :                 {
     713         645 :                     tc_subfr = TC_0_192;
     714             :                 }
     715             :                 else
     716             :                 {
     717        5946 :                     if ( get_next_indice( st, 1 ) )
     718             :                     {
     719        3951 :                         tc_subfr = TC_0_64;
     720             :                     }
     721             :                     else
     722             :                     {
     723        1995 :                         tc_subfr = TC_0_128;
     724             :                     }
     725             :                 }
     726             :             }
     727             :             else
     728             :             {
     729       10344 :                 if ( get_next_indice( st, 1 ) )
     730             :                 {
     731        3396 :                     tc_subfr = L_SUBFR;
     732             :                 }
     733             :                 else
     734             :                 {
     735        6948 :                     if ( get_next_indice( st, 1 ) )
     736             :                     {
     737        1656 :                         tc_subfr = 2 * L_SUBFR;
     738             :                     }
     739             :                     else
     740             :                     {
     741        5292 :                         tc_subfr = 3 * L_SUBFR;
     742             :                     }
     743             :                 }
     744             :             }
     745             :         }
     746             :     }
     747             :     else /* L_frame == L_FRAME16k */
     748             :     {
     749       19755 :         indice = get_next_indice( st, 2 );
     750             : 
     751       19755 :         if ( indice < 3 )
     752             :         {
     753       13104 :             tc_subfr = indice * L_SUBFR;
     754             :         }
     755             :         else
     756             :         {
     757        6651 :             if ( get_next_indice( st, 1 ) == 0 )
     758             :             {
     759         693 :                 tc_subfr = 3 * L_SUBFR;
     760             :             }
     761             :             else
     762             :             {
     763        5958 :                 tc_subfr = 4 * L_SUBFR;
     764             :             }
     765             :         }
     766             :     }
     767             : 
     768       41832 :     return ( tc_subfr );
     769             : }

Generated by: LCOV version 1.14