LCOV - code coverage report
Current view: top level - lib_dec - transition_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 238 244 97.5 %
Date: 2025-11-01 08:50:45 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     2470062 : 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     2470062 :     limit_flag = 0;
      83             : 
      84             :     /*---------------------------------------------------------------------*
      85             :      * zero adaptive contribution (glottal shape codebook search not
      86             :      *                             in first subframe(s) )
      87             :      *---------------------------------------------------------------------*/
      88             : 
      89     2470062 :     if ( tc_subfr > i_subfr + TC_0_192 )
      90             :     {
      91      753107 :         set_f( &exc[i_subfr], 0, L_SUBFR );
      92             : 
      93      753107 :         if ( L_frame == L_FRAME )
      94             :         {
      95      300419 :             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      452688 :             set_f( &bwe_exc[i_subfr * 2], 0, (int16_t) ( L_SUBFR * 2 ) ); /* set past excitation buffer to 0 */
     100             :         }
     101             : 
     102      753107 :         *T0 = L_SUBFR;
     103      753107 :         *T0_frac = 0;
     104      753107 :         **pt_pitch = (float) L_SUBFR;
     105             :     }
     106             : 
     107             :     /*---------------------------------------------------------------------*
     108             :      * glottal shape codebook search
     109             :      *---------------------------------------------------------------------*/
     110             : 
     111     1716955 :     else if ( ( tc_subfr - i_subfr >= 0 ) && ( tc_subfr - i_subfr <= TC_0_192 ) )
     112             :     {
     113      548402 :         set_f( exc - L_EXC_MEM, 0, L_EXC_MEM ); /* set past excitation buffer to 0 */
     114             : 
     115      548402 :         if ( L_frame == L_FRAME )
     116             :         {
     117      271948 :             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      276454 :             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      548402 :         tc_dec( st, L_frame, exc, T0, T0_frac, i_subfr, tc_subfr, position, bwe_exc );
     126             : 
     127      548402 :         **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     128      548402 :         *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     1168553 :     else if ( tc_subfr < i_subfr )
     140             :     {
     141     1168553 :         if ( L_frame == L_FRAME )
     142             :         {
     143      515425 :             *Jopt_flag = 1;
     144             : 
     145      515425 :             if ( ( i_subfr - tc_subfr >= L_SUBFR ) && ( i_subfr - tc_subfr <= L_SUBFR + TC_0_192 ) )
     146             :             {
     147       63216 :                 pit_flag = 0;
     148             :             }
     149             :             else
     150             :             {
     151      452209 :                 pit_flag = L_SUBFR;
     152             :             }
     153             : 
     154      515425 :             if ( tc_subfr == TC_0_0 )
     155             :             {
     156      198975 :                 if ( i_subfr == L_SUBFR )
     157             :                 {
     158       66325 :                     limit_T0( L_FRAME, 8, pit_flag, limit_flag, *T0, 0, T0_min, T0_max );
     159             :                 }
     160             : 
     161      198975 :                 pit_flag = 1;
     162             :             }
     163             : 
     164             :             /*-----------------------------------------------------------------*
     165             :              * get number of bits for pitch decoding
     166             :              *-----------------------------------------------------------------*/
     167             : 
     168      515425 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     169             : 
     170             :             /*------------------------------------------------------------*
     171             :              * first glottal impulse is in the 1st subframe
     172             :              *------------------------------------------------------------*/
     173             : 
     174      515425 :             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       24802 :                 *T0 = 2 * L_SUBFR;
     182       24802 :                 *T0_frac = 0;
     183       24802 :                 *Jopt_flag = 0;
     184             : 
     185             :                 /* set adaptive part of exciation for curent subframe to 0 */
     186       24802 :                 set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
     187             : 
     188       24802 :                 set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     189             :             }
     190      490623 :             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       47225 :                 if ( PIT_MIN > ( *position ) )
     198             :                 {
     199       39673 :                     pit_start = L_SUBFR - ( *position );
     200             :                 }
     201             :                 else
     202             :                 {
     203        7552 :                     pit_start = PIT_MIN;
     204             :                 }
     205             : 
     206       47225 :                 if ( pit_start < PIT_MIN )
     207             :                 {
     208        2609 :                     pit_start = PIT_MIN;
     209             :                 }
     210             : 
     211       47225 :                 pit_limit = 2 * pit_start + ( *position );
     212             : 
     213             :                 /* 7 bit pitch DECODER */
     214       47225 :                 index = get_next_indice( st, nBits );
     215             : 
     216       47225 :                 *T0 = (int16_t) ( floor( pit_start + index / 2 ) );
     217       47225 :                 *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
     218       47225 :                 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       47225 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     222             : 
     223       47225 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     224     7603225 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     225             :                 {
     226     7556000 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     227             :                 }
     228             :             }
     229      443398 :             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       18531 :                 pit_start = 2 * L_SUBFR - ( *position );
     238             : 
     239       18531 :                 index = get_next_indice( st, nBits );
     240             : 
     241       18531 :                 *T0 = (int16_t) ( floor( pit_start + (int16_t) ( index / 2 ) ) );
     242       18531 :                 *T0_frac = ( index - ( *T0 - pit_start ) * 2 ) * 2;
     243             : 
     244       18531 :                 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       18531 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     248             : 
     249       18531 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     250     2983491 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     251             :                 {
     252     2964960 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     253             :                 }
     254             :             }
     255      424867 :             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        6271 :                 *T0 = 4 * L_SUBFR;
     263        6271 :                 *T0_frac = 0;
     264        6271 :                 *Jopt_flag = 0;
     265             : 
     266             :                 /* set adaptive part of exciation for curent subframe to 0 */
     267        6271 :                 set_f( &exc[i_subfr], 0, (int16_t) ( L_SUBFR + 1 ) );
     268             : 
     269        6271 :                 set_f( &bwe_exc[i_subfr * HIBND_ACB_L_FAC], 0, (int16_t) ( L_SUBFR * HIBND_ACB_L_FAC ) );
     270             :             }
     271      418596 :             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        6271 :                 pit_start = 3 * L_SUBFR - ( *position );
     279        6271 :                 pit_limit = 2 * L_FRAME - PIT_MAX - 2 * ( *position ) - 2;
     280             : 
     281        6271 :                 index = get_next_indice( st, nBits );
     282             : 
     283        6271 :                 if ( index < ( pit_limit - pit_start ) * 2 )
     284             :                 {
     285        5035 :                     *T0 = (int16_t) ( floor( pit_start + ( index / 2 ) ) );
     286        5035 :                     *T0_frac = ( index - ( ( *T0 ) - pit_start ) * 2 ) * 2;
     287             :                 }
     288             :                 else
     289             :                 {
     290        1236 :                     *T0 = index + pit_limit - ( pit_limit - pit_start ) * 2;
     291        1236 :                     *T0_frac = 0;
     292             :                 }
     293             : 
     294             :                 /* biterror detection mechanism */
     295        6271 :                 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        6271 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     304             : 
     305        6271 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     306     1009631 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     307             :                 {
     308     1003360 :                     bwe_exc[i + i_subfr * HIBND_ACB_L_FAC] = bwe_exc[i + i_subfr * HIBND_ACB_L_FAC - offset];
     309             :                 }
     310             :             }
     311      412325 :             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       18531 :                 index = get_next_indice( st, nBits );
     319             : 
     320       18531 :                 delta_pit_dec( 2, index, T0, T0_frac, *T0_min );
     321             : 
     322             :                 /* Find the adaptive codebook vector. ACELP long-term prediction   */
     323       18531 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     324             : 
     325       18531 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     326     2983491 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     327             :                 {
     328     2964960 :                     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      393794 :                 index = get_next_indice( st, nBits );
     339             : 
     340      393794 :                 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      393794 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     344             : 
     345      393794 :                 offset = tbe_celp_exc_offset( *T0, *T0_frac );
     346    63400834 :                 for ( i = 0; i < L_SUBFR * HIBND_ACB_L_FAC; i++ )
     347             :                 {
     348    63007040 :                     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      515425 :             if ( *Jopt_flag )
     357             :             {
     358      484352 :                 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      515425 :             **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f; /* save subframe pitch values  */
     366      515425 :             if ( ( tc_subfr >= 2 * L_SUBFR ) && ( i_subfr == 3 * L_SUBFR ) )
     367             :             {
     368       26063 :                 ( *pt_pitch ) -= 3;
     369       26063 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     370       26063 :                 ( *pt_pitch )++;
     371       26063 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     372       26063 :                 ( *pt_pitch )++;
     373       26063 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     374       26063 :                 ( *pt_pitch )++;
     375             :             }
     376      489362 :             else if ( ( tc_subfr == L_SUBFR ) && ( i_subfr == 2 * L_SUBFR ) )
     377             :             {
     378       37153 :                 ( *pt_pitch ) -= 2;
     379       37153 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     380       37153 :                 ( *pt_pitch )++;
     381       37153 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     382       37153 :                 ( *pt_pitch )++;
     383             :             }
     384      452209 :             else if ( ( tc_subfr == TC_0_64 ) && ( i_subfr == L_SUBFR ) )
     385             :             {
     386       47225 :                 ( *pt_pitch ) -= 1;
     387       47225 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     388       47225 :                 ( *pt_pitch )++;
     389             :             }
     390      404984 :             else if ( ( tc_subfr == TC_0_128 ) && ( i_subfr == 2 * L_SUBFR ) )
     391             :             {
     392       18531 :                 ( *pt_pitch ) -= 2;
     393       18531 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     394       18531 :                 ( *pt_pitch )++;
     395       18531 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     396       18531 :                 ( *pt_pitch )++;
     397             :             }
     398      386453 :             else if ( ( tc_subfr == TC_0_192 ) && ( i_subfr == 3 * L_SUBFR ) )
     399             :             {
     400        6271 :                 ( *pt_pitch ) -= 3;
     401        6271 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     402        6271 :                 ( *pt_pitch )++;
     403        6271 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     404        6271 :                 ( *pt_pitch )++;
     405        6271 :                 **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     406        6271 :                 ( *pt_pitch )++;
     407             :             }
     408             :         }
     409             :         else /* L_frame == L_FRAME16k */
     410             :         {
     411      653128 :             if ( i_subfr >= 2 * L_SUBFR )
     412             :             {
     413      536513 :                 limit_flag = 1;
     414             :             }
     415             : 
     416      653128 :             if ( i_subfr - tc_subfr == L_SUBFR )
     417             :             {
     418      202877 :                 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      653128 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     426             : 
     427      653128 :             index = get_next_indice( st, nBits );
     428             : 
     429             :             /*-----------------------------------------------------------------*
     430             :              * Find adaptive part of excitation, encode pitch period
     431             :              *-----------------------------------------------------------------*/
     432             : 
     433      653128 :             if ( nBits == 10 )
     434             :             {
     435      146506 :                 pit16k_Q_dec( index, nBits, limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     436             :             }
     437      506622 :             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      116615 :                 if ( index < ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4 )
     446             :                 {
     447       65145 :                     *T0 = PIT16k_MIN + ( index / 4 );
     448       65145 :                     *T0_frac = index - ( *T0 - PIT16k_MIN ) * 4;
     449             :                 }
     450             :                 else
     451             :                 {
     452       51470 :                     index -= ( PIT16k_FR2_TC0_2SUBFR - PIT16k_MIN ) * 4;
     453       51470 :                     *T0 = PIT16k_FR2_TC0_2SUBFR + ( index / 2 );
     454       51470 :                     *T0_frac = index - ( *T0 - PIT16k_FR2_TC0_2SUBFR ) * 2;
     455       51470 :                     ( *T0_frac ) *= 2;
     456             :                 }
     457             : 
     458             :                 /* biterror detection mechanism */
     459      116615 :                 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      390007 :             else if ( nBits == 6 )
     467             :             {
     468      390007 :                 delta_pit_dec( 4, index, T0, T0_frac, *T0_min );
     469             :             }
     470      653128 :             if ( nBits == 6 )
     471             :             {
     472      390007 :                 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      653128 :             if ( ( i_subfr == L_SUBFR ) && ( *T0 == 2 * L_SUBFR ) )
     481             :             {
     482             :                 /* no adaptive excitation in the second subframe */
     483       22989 :                 set_f( &exc[i_subfr], 0, L_SUBFR + 1 );
     484             : 
     485       22989 :                 get_next_indice( st, 1 ); /* this bit is actually not needed */
     486             : 
     487       22989 :                 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      630139 :                 pred_lt4( &exc[i_subfr], &exc[i_subfr], *T0, *T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     493             : 
     494    81287931 :                 for ( i = 0; i < L_SUBFR * 2; i++ )
     495             :                 {
     496    80657792 :                     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      630139 :                 lp_filt_exc_dec( st, MODE1, i_subfr, L_SUBFR, L_frame, st->acelp_cfg.ltf_mode, exc );
     500             : 
     501      630139 :                 *Jopt_flag = 1;
     502             :             }
     503             : 
     504      653128 :             **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      653128 :             if ( ( i_subfr - tc_subfr == L_SUBFR ) || ( tc_subfr == 0 && i_subfr == 2 * L_SUBFR ) )
     511             :             {
     512      319492 :                 index = i_subfr / L_SUBFR;
     513      319492 :                 ( *pt_pitch ) -= index;
     514             : 
     515      913979 :                 for ( i = 0; i < index; i++ )
     516             :                 {
     517      594487 :                     **pt_pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     518      594487 :                     ( *pt_pitch )++;
     519             :                 }
     520             :             }
     521             :         }
     522             :     }
     523             : 
     524     2470062 :     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      548402 : 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      548402 :     nBits = st->acelp_cfg.pitch_bits[i_subfr / L_SUBFR];
     557             : 
     558             :     /*----------------------------------------------------------------*
     559             :      * decode parameter T0 (pitch period)
     560             :      *----------------------------------------------------------------*/
     561             : 
     562      548402 :     if ( L_frame == L_FRAME )
     563             :     {
     564      271948 :         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      109180 :             *T0 = L_SUBFR;
     567      109180 :             *T0_frac = 0;
     568             :         }
     569      162768 :         else if ( ( tc_subfr == 3 * L_SUBFR ) )
     570             :         {
     571       70380 :             i = get_next_indice( st, nBits );
     572             : 
     573       70380 :             if ( nBits == 9 )
     574             :             {
     575       70073 :                 abs_pit_dec( 4, i, 0, T0, T0_frac );
     576             :             }
     577             :             else
     578             :             {
     579         307 :                 abs_pit_dec( 2, i, 0, T0, T0_frac );
     580             :             }
     581             :         }
     582             :         else
     583             :         {
     584       92388 :             i = get_next_indice( st, nBits );
     585             : 
     586       92388 :             if ( i == 0 )
     587             :             {
     588       15477 :                 *T0 = L_SUBFR;
     589       15477 :                 *T0_frac = 0;
     590             :             }
     591             :             else
     592             :             {
     593       76911 :                 if ( tc_subfr == TC_0_0 )
     594             :                 {
     595       66325 :                     delta_pit_dec( 2, i, T0, T0_frac, PIT_MIN - 1 );
     596             :                 }
     597             :                 else
     598             :                 {
     599       10586 :                     delta_pit_dec( 0, i, T0, T0_frac, PIT_MIN - 1 );
     600             :                 }
     601             :             }
     602             :         }
     603             :     }
     604             :     else /* L_frame == L_FRAME16k */
     605             :     {
     606      276454 :         i = get_next_indice( st, nBits );
     607             : 
     608      276454 :         if ( nBits == 10 )
     609             :         {
     610      129948 :             if ( i < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
     611             :             {
     612      126491 :                 *T0 = PIT16k_MIN_EXTEND + ( i / 4 );
     613      126491 :                 *T0_frac = i - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
     614             :             }
     615             :             else
     616             :             {
     617        3457 :                 index = i - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
     618        3457 :                 *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
     619        3457 :                 *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
     620        3457 :                 ( *T0_frac ) *= 2;
     621             :             }
     622             :         }
     623      146506 :         else if ( nBits == 6 )
     624             :         {
     625      146506 :             *T0 = PIT16k_MIN + ( i / 2 );
     626      146506 :             *T0_frac = i - ( *T0 - PIT16k_MIN ) * 2;
     627      146506 :             *T0_frac *= 2;
     628             :         }
     629             :     }
     630             : 
     631             :     /*----------------------------------------------------------------*
     632             :      * decode other TC parameters
     633             :      *----------------------------------------------------------------*/
     634             : 
     635      548402 :     imp_shape = get_next_indice( st, 3 );
     636      548402 :     imp_pos = get_next_indice( st, 6 );
     637      548402 :     imp_sign = get_next_indice( st, 1 );
     638      548402 :     imp_gain = get_next_indice( st, 3 );
     639             : 
     640             :     /*----------------------------------------------------------------*
     641             :      * restore gain_trans
     642             :      * build glottal codebook contribution
     643             :      *----------------------------------------------------------------*/
     644             : 
     645      548402 :     gain_trans = tbl_gain_trans_tc[imp_gain];
     646             : 
     647      548402 :     if ( imp_sign == 0 )
     648             :     {
     649      260191 :         gain_trans *= -1;
     650             :     }
     651             : 
     652             :     /* build glottal codebook contribution */
     653      548402 :     set_f( &exc[i_subfr], 0, L_SUBFR );
     654             : 
     655     9871236 :     for ( i = ( imp_pos - L_IMPULSE2 ); i <= ( imp_pos + L_IMPULSE2 ); i++ )
     656             :     {
     657     9322834 :         if ( ( i >= 0 ) && ( i < L_SUBFR ) )
     658             :         {
     659     8758707 :             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      548402 :     pred_lt4_tc( exc, *T0, *T0_frac, inter4_2, imp_pos, i_subfr );
     669             : 
     670      548402 :     if ( st->hBWE_TD != NULL )
     671             :     {
     672      548402 :         if ( L_frame == L_FRAME )
     673             :         {
     674      271948 :             interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
     675             :         }
     676             :         else
     677             :         {
     678      276454 :             interp_code_4over2( &exc[i_subfr], &bwe_exc[i_subfr * 2], L_SUBFR );
     679             :         }
     680             :     }
     681             : 
     682      548402 :     *position = imp_pos + i_subfr;
     683             : 
     684      548402 :     return;
     685             : }
     686             : 
     687             : /*-------------------------------------------------------------------*
     688             :  * tc_classif()
     689             :  *
     690             :  * TC subframe classification decoding
     691             :  *-------------------------------------------------------------------*/
     692             : 
     693      548402 : int16_t tc_classif(
     694             :     Decoder_State *st /* i/o: decoder state structure           */
     695             : )
     696             : {
     697             :     int16_t tc_subfr, indice;
     698             : 
     699      548402 :     if ( st->L_frame == L_FRAME )
     700             :     {
     701      271948 :         if ( get_next_indice( st, 1 ) )
     702             :         {
     703       66325 :             tc_subfr = TC_0_0;
     704             :         }
     705             :         else
     706             :         {
     707      205623 :             if ( get_next_indice( st, 1 ) )
     708             :             {
     709       72027 :                 tc_subfr = 0;
     710             : 
     711       72027 :                 if ( get_next_indice( st, 1 ) )
     712             :                 {
     713        6271 :                     tc_subfr = TC_0_192;
     714             :                 }
     715             :                 else
     716             :                 {
     717       65756 :                     if ( get_next_indice( st, 1 ) )
     718             :                     {
     719       47225 :                         tc_subfr = TC_0_64;
     720             :                     }
     721             :                     else
     722             :                     {
     723       18531 :                         tc_subfr = TC_0_128;
     724             :                     }
     725             :                 }
     726             :             }
     727             :             else
     728             :             {
     729      133596 :                 if ( get_next_indice( st, 1 ) )
     730             :                 {
     731       37153 :                     tc_subfr = L_SUBFR;
     732             :                 }
     733             :                 else
     734             :                 {
     735       96443 :                     if ( get_next_indice( st, 1 ) )
     736             :                     {
     737       26063 :                         tc_subfr = 2 * L_SUBFR;
     738             :                     }
     739             :                     else
     740             :                     {
     741       70380 :                         tc_subfr = 3 * L_SUBFR;
     742             :                     }
     743             :                 }
     744             :             }
     745             :         }
     746             :     }
     747             :     else /* L_frame == L_FRAME16k */
     748             :     {
     749      276454 :         indice = get_next_indice( st, 2 );
     750             : 
     751      276454 :         if ( indice < 3 )
     752             :         {
     753      187130 :             tc_subfr = indice * L_SUBFR;
     754             :         }
     755             :         else
     756             :         {
     757       89324 :             if ( get_next_indice( st, 1 ) == 0 )
     758             :             {
     759       15747 :                 tc_subfr = 3 * L_SUBFR;
     760             :             }
     761             :             else
     762             :             {
     763       73577 :                 tc_subfr = 4 * L_SUBFR;
     764             :             }
     765             :         }
     766             :     }
     767             : 
     768      548402 :     return ( tc_subfr );
     769             : }

Generated by: LCOV version 1.14