LCOV - code coverage report
Current view: top level - lib_dec - pit_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 231 267 86.5 %
Date: 2025-11-01 08:50:45 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #include "cnst.h"
      41             : #include "prot.h"
      42             : #include "rom_com.h"
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*----------------------------------------------------------*
      49             :  * pit_decode()
      50             :  *
      51             :  * Decode OL pitch lag
      52             :  *----------------------------------------------------------*/
      53             : 
      54             : /*! r: floating pitch value */
      55    18695394 : float pit_decode(
      56             :     Decoder_State *st,                  /* i/o: decoder state structure                 */
      57             :     const int32_t core_brate,           /* i  : core bitrate                            */
      58             :     const int16_t Opt_AMR_WB,           /* i  : flag indicating AMR-WB IO mode          */
      59             :     const int16_t L_frame,              /* i  : length of the frame                     */
      60             :     int16_t i_subfr,                    /* i  : subframe index                          */
      61             :     const int16_t coder_type,           /* i  : coding type                             */
      62             :     int16_t *limit_flag,                /* i/o: restrained(0) or extended(1) Q limits   */
      63             :     int16_t *T0,                        /* o  : close loop integer pitch                */
      64             :     int16_t *T0_frac,                   /* o  : close loop fractional part of the pitch */
      65             :     int16_t *T0_min,                    /* i/o: delta search min                        */
      66             :     int16_t *T0_max,                    /* i/o: delta search max                        */
      67             :     const int16_t L_subfr,              /* i  : subframe length                         */
      68             :     const int16_t tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag                */
      69             :     const float tdm_Pri_pitch_buf[]     /* i  : primary channel pitch buffer                    */
      70             : )
      71             : {
      72             :     float pitch;
      73             :     int16_t pitch_index, nBits, pit_flag;
      74             : 
      75    18695394 :     pitch_index = 0;
      76             : 
      77             :     /*----------------------------------------------------------------*
      78             :      * Set pit_flag = 0 for every subframe with absolute pitch search
      79             :      *----------------------------------------------------------------*/
      80             : 
      81    18695394 :     pit_flag = i_subfr;
      82    18695394 :     if ( i_subfr == 2 * L_SUBFR )
      83             :     {
      84     4175952 :         pit_flag = 0;
      85             :     }
      86             : 
      87             :     /*-------------------------------------------------------*
      88             :      * Retrieve the pitch index
      89             :      *-------------------------------------------------------*/
      90             : 
      91    18695394 :     if ( !Opt_AMR_WB )
      92             :     {
      93             :         /*----------------------------------------------------------------*
      94             :          * pitch Q: Set limit_flag to 0 for restrained limits, and 1 for extended limits
      95             :          *----------------------------------------------------------------*/
      96             : 
      97    18671654 :         if ( i_subfr == 0 )
      98             :         {
      99     4284131 :             *limit_flag = 1;
     100             : 
     101     4284131 :             if ( coder_type == VOICED )
     102             :             {
     103      256666 :                 *limit_flag = 2; /* double-extended limits */
     104             :             }
     105             : 
     106     4284131 :             if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
     107             :             {
     108       12765 :                 *limit_flag = 0;
     109             :             }
     110             :         }
     111    14387523 :         else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
     112             :         {
     113     1223875 :             if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
     114             :             {
     115      946830 :                 *limit_flag = 0;
     116             :             }
     117             :         }
     118             : 
     119             :         /*-------------------------------------------------------*
     120             :          *  Retrieve the number of Q bits
     121             :          *-------------------------------------------------------*/
     122             : 
     123    18671654 :         nBits = 0;
     124    18671654 :         if ( coder_type != AUDIO )
     125             :         {
     126    17840458 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_subfr];
     127             : 
     128    17840458 :             pitch_index = get_next_indice( st, nBits );
     129             :         }
     130             : 
     131             :         /*-------------------------------------------------------*
     132             :          * Pitch decoding in AUDIO mode
     133             :          * (both ACELP@12k8 and ACELP@16k cores)
     134             :          *-------------------------------------------------------*/
     135             : 
     136    18671654 :         if ( coder_type == AUDIO )
     137             :         {
     138      831196 :             if ( L_subfr == L_FRAME / 2 && i_subfr != 0 )
     139             :             {
     140       78191 :                 pit_flag = L_SUBFR;
     141             :             }
     142             : 
     143      831196 :             if ( pit_flag == 0 )
     144             :             {
     145      457201 :                 nBits = 10;
     146             :             }
     147             :             else
     148             :             {
     149      373995 :                 nBits = 6;
     150             :             }
     151             : 
     152      831196 :             pitch_index = get_next_indice( st, nBits );
     153             : 
     154      831196 :             if ( L_subfr == L_FRAME / 2 && i_subfr != 0 && pitch_index >= 32 ) /* safety check in case of bit errors */
     155             :             {
     156           0 :                 pitch_index = pitch_index >> 1;
     157           0 :                 st->BER_detect = 1;
     158             :             }
     159             : 
     160      831196 :             pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     161             :         }
     162    17840458 :         else if ( coder_type == VOICED )
     163             :         {
     164             :             /*-------------------------------------------------------*
     165             :              * Pitch decoding in VOICED mode
     166             :              * (ACELP@12k8 core only)
     167             :              *-------------------------------------------------------*/
     168             : 
     169     1026664 :             if ( i_subfr == 2 * L_SUBFR )
     170             :             {
     171      256666 :                 pit_flag = i_subfr;
     172             :             }
     173             : 
     174     1026664 :             pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     175             :         }
     176    16813794 :         else if ( st->idchan == 1 && ( tdm_Pitch_reuse_flag == 1 || nBits == 4 ) )
     177        1356 :         {
     178             :             /*-------------------------------------------------------*
     179             :              *  Pitch decoding with reusing of primary channel information
     180             :              *-------------------------------------------------------*/
     181             :             int16_t loc_T0, loc_frac, delta;
     182             : 
     183        1356 :             delta = 4;
     184        1356 :             pit_flag = L_SUBFR;
     185             : 
     186        1356 :             if ( L_subfr == 2 * L_SUBFR )
     187             :             {
     188           0 :                 loc_T0 = (int16_t) ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] );
     189           0 :                 loc_frac = (int16_t) ( ( ( 0.5f * tdm_Pri_pitch_buf[i_subfr / L_SUBFR] + 0.5f * tdm_Pri_pitch_buf[( i_subfr + L_SUBFR ) / L_SUBFR] ) - loc_T0 ) * 4.0f );
     190             :             }
     191             :             else
     192             :             {
     193        1356 :                 loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
     194        1356 :                 loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
     195             :             }
     196             : 
     197        1356 :             limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     198             : 
     199        1356 :             if ( nBits > 0 )
     200             :             {
     201           0 :                 pit_Q_dec( 0, pitch_index, nBits, delta, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     202             :             }
     203             :             else
     204             :             {
     205        1356 :                 *T0 = loc_T0;
     206        1356 :                 *T0_frac = loc_frac;
     207             :             }
     208             :         }
     209             :         else
     210             :         {
     211             :             /*-------------------------------------------------------*
     212             :              *  Pitch decoding in GENERIC mode
     213             :              * (both ACELP@12k8 and ACELP@16k cores)
     214             :              *-------------------------------------------------------*/
     215             : 
     216    16812438 :             if ( L_frame == L_FRAME )
     217             :             {
     218     6515465 :                 pit_Q_dec( 0, pitch_index, nBits, 8, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     219             :             }
     220             :             else
     221             :             {
     222    10296973 :                 pit16k_Q_dec( pitch_index, nBits, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     223             :             }
     224             :         }
     225             :     }
     226             : 
     227             :     /*-------------------------------------------------------*
     228             :      *  Pitch decoding in AMR-WB IO mode
     229             :      *-------------------------------------------------------*/
     230             : 
     231             :     else
     232             :     {
     233       23740 :         *limit_flag = 0;
     234             : 
     235       23740 :         if ( i_subfr == 0 || ( i_subfr == 2 * L_SUBFR && core_brate == ACELP_8k85 ) )
     236             :         {
     237        6389 :             nBits = 8;
     238             :         }
     239             :         else
     240             :         {
     241       17351 :             nBits = 5;
     242             :         }
     243             : 
     244       23740 :         if ( core_brate > ACELP_8k85 )
     245             :         {
     246       19676 :             nBits = 6;
     247             : 
     248       19676 :             if ( i_subfr == 0 || i_subfr == 2 * L_SUBFR )
     249             :             {
     250        9838 :                 nBits = 9;
     251             :             }
     252             :         }
     253             : 
     254       23740 :         pitch_index = get_next_indice( st, nBits );
     255             : 
     256       23740 :         pit_Q_dec( 1, pitch_index, nBits, 8, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     257             :     }
     258             : 
     259             :     /*-------------------------------------------------------*
     260             :      * Compute floating pitch output
     261             :      *-------------------------------------------------------*/
     262             : 
     263    18695394 :     pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     264             : 
     265    18695394 :     return pitch;
     266             : }
     267             : 
     268             : 
     269             : /*---------------------------------------------------------------------*
     270             :  * abs_pit_dec()
     271             :  *
     272             :  * Decode the absolute pitch
     273             :  *---------------------------------------------------------------------*/
     274             : 
     275     1442709 : void abs_pit_dec(
     276             :     const int16_t fr_steps,   /* i  : fractional resolution steps (0, 2, 4)    */
     277             :     int16_t pitch_index,      /* i  : pitch index                              */
     278             :     const int16_t limit_flag, /* i  : restrained(0) or extended(1) limits      */
     279             :     int16_t *T0,              /* o  : integer pitch lag                        */
     280             :     int16_t *T0_frac          /* o  : pitch fraction                           */
     281             : )
     282             : {
     283     1442709 :     if ( limit_flag == 0 )
     284             :     {
     285     1106978 :         if ( fr_steps == 2 )
     286             :         {
     287       55694 :             if ( pitch_index < ( PIT_FR1_8b - PIT_MIN ) * 2 )
     288             :             {
     289       28950 :                 *T0 = PIT_MIN + ( pitch_index / 2 );
     290       28950 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN ) * 2 );
     291       28950 :                 *T0_frac *= 2;
     292             :             }
     293             :             else
     294             :             {
     295       26744 :                 *T0 = pitch_index + PIT_FR1_8b - ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
     296       26744 :                 *T0_frac = 0;
     297             :             }
     298             :         }
     299     1051284 :         else if ( fr_steps == 4 )
     300             :         {
     301     1051284 :             if ( pitch_index < ( PIT_FR2_9b - PIT_MIN ) * 4 )
     302             :             {
     303      871969 :                 *T0 = PIT_MIN + ( pitch_index / 4 );
     304      871969 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN ) * 4;
     305             :             }
     306      179315 :             else if ( pitch_index < ( ( PIT_FR2_9b - PIT_MIN ) * 4 + ( PIT_FR1_9b - PIT_FR2_9b ) * 2 ) )
     307             :             {
     308       95989 :                 pitch_index -= ( PIT_FR2_9b - PIT_MIN ) * 4;
     309       95989 :                 *T0 = PIT_FR2_9b + ( pitch_index / 2 );
     310       95989 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_9b ) * 2;
     311       95989 :                 ( *T0_frac ) *= 2;
     312             :             }
     313             :             else
     314             :             {
     315       83326 :                 *T0 = pitch_index + PIT_FR1_9b - ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) - ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
     316       83326 :                 *T0_frac = 0;
     317             :             }
     318             :         }
     319             :         else /* fr_steps == 0 */
     320             :         {
     321             :             /* not used in the codec */
     322             :         }
     323             :     }
     324      335731 :     else if ( limit_flag == 1 ) /* extended Q range */
     325             :     {
     326      325961 :         if ( fr_steps == 2 )
     327             :         {
     328       20845 :             if ( pitch_index < ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 )
     329             :             {
     330       10896 :                 *T0 = PIT_MIN_EXTEND + ( pitch_index / 2 );
     331       10896 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN_EXTEND ) * 2 );
     332       10896 :                 *T0_frac *= 2;
     333             :             }
     334             :             else
     335             :             {
     336        9949 :                 *T0 = pitch_index + PIT_FR1_EXTEND_8b - ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
     337        9949 :                 *T0_frac = 0;
     338             :             }
     339             :         }
     340      305116 :         else if ( fr_steps == 4 )
     341             :         {
     342      305116 :             if ( pitch_index < ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 )
     343             :             {
     344      260379 :                 *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
     345      260379 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
     346             :             }
     347       44737 :             else if ( pitch_index < ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 + ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 ) )
     348             :             {
     349       10009 :                 pitch_index -= ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4;
     350       10009 :                 *T0 = PIT_FR2_EXTEND_9b + ( pitch_index / 2 );
     351       10009 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_EXTEND_9b ) * 2;
     352       10009 :                 ( *T0_frac ) *= 2;
     353             :             }
     354             :             else
     355             :             {
     356       34728 :                 *T0 = pitch_index + PIT_FR1_EXTEND_9b - ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 ) - ( ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 );
     357       34728 :                 *T0_frac = 0;
     358             :             }
     359             :         }
     360             :         else /* fr_steps == 0 */
     361             :         {
     362             :             /* not used in the codec */
     363             :         }
     364             :     }
     365             :     else /* limit_flag == 2 */
     366             :     {
     367        9770 :         if ( fr_steps == 2 )
     368             :         {
     369           0 :             if ( pitch_index < ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 )
     370             :             {
     371           0 :                 *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 2 );
     372           0 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 2 );
     373           0 :                 *T0_frac *= 2;
     374             :             }
     375             :             else
     376             :             {
     377           0 :                 *T0 = pitch_index + PIT_FR1_DOUBLEEXTEND_8b - ( ( PIT_FR1_DOUBLEEXTEND_8b - PIT_MIN_DOUBLEEXTEND ) * 2 );
     378           0 :                 *T0_frac = 0;
     379             :             }
     380             :         }
     381        9770 :         else if ( fr_steps == 4 )
     382             :         {
     383        9770 :             if ( pitch_index < ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 )
     384             :             {
     385        9272 :                 *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
     386        9272 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
     387             :             }
     388         498 :             else if ( pitch_index < ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 + ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 ) )
     389             :             {
     390         154 :                 pitch_index -= ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4;
     391         154 :                 *T0 = PIT_FR2_DOUBLEEXTEND_9b + ( pitch_index / 2 );
     392         154 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_DOUBLEEXTEND_9b ) * 2;
     393         154 :                 ( *T0_frac ) *= 2;
     394             :             }
     395             :             else
     396             :             {
     397         344 :                 *T0 = pitch_index + PIT_FR1_DOUBLEEXTEND_9b - ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 ) - ( ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 );
     398         344 :                 *T0_frac = 0;
     399             :             }
     400             :         }
     401             :         else /* fr_steps == 0 */
     402             :         {
     403             :             /* not used in the codec */
     404             :         }
     405             :     }
     406             : 
     407     1442709 :     return;
     408             : }
     409             : 
     410             : 
     411             : /*---------------------------------------------------------------------*
     412             :  * delta_pit_dec()
     413             :  *
     414             :  * Decode delta pitch
     415             :  *---------------------------------------------------------------------*/
     416             : 
     417    11380144 : void delta_pit_dec(
     418             :     const int16_t fr_steps,    /* i  : fractional resolution steps (0, 2, 4)    */
     419             :     const int16_t pitch_index, /* i  : pitch index                              */
     420             :     int16_t *T0,               /* o  : integer pitch lag                        */
     421             :     int16_t *T0_frac,          /* o  : pitch fraction                           */
     422             :     const int16_t T0_min       /* i  : delta search min                         */
     423             : )
     424             : {
     425    11380144 :     if ( fr_steps == 0 )
     426             :     {
     427       10586 :         *T0 = T0_min + pitch_index;
     428       10586 :         *T0_frac = 0;
     429             :     }
     430    11369558 :     else if ( fr_steps == 2 )
     431             :     {
     432      234883 :         *T0 = T0_min + pitch_index / 2;
     433      234883 :         *T0_frac = pitch_index - ( *T0 - T0_min ) * 2;
     434      234883 :         *T0_frac *= 2;
     435             :     }
     436    11134675 :     else if ( fr_steps == 4 )
     437             :     {
     438    11134675 :         *T0 = T0_min + pitch_index / 4;
     439    11134675 :         *T0_frac = pitch_index - ( *T0 - T0_min ) * 4;
     440             :     }
     441             : 
     442    11380144 :     return;
     443             : }
     444             : 
     445             : 
     446             : /*-------------------------------------------------*
     447             :  * pit_Q_dec()
     448             :  *
     449             :  * pitch decoding
     450             :  *-------------------------------------------------*/
     451             : 
     452     8790859 : void pit_Q_dec(
     453             :     const int16_t Opt_AMR_WB,  /* i  : flag indicating AMR-WB IO mode          */
     454             :     const int16_t pitch_index, /* i  : pitch index                             */
     455             :     const int16_t nBits,       /* i  : # of Q bits                             */
     456             :     const int16_t delta,       /* i  : Half the CL searched interval           */
     457             :     const int16_t pit_flag,    /* i  : absolute(0) or delta(1) pitch Q         */
     458             :     const int16_t limit_flag,  /* i  : restrained(0) or extended(1) Q limits   */
     459             :     int16_t *T0,               /* o  : integer pitch lag                       */
     460             :     int16_t *T0_frac,          /* o  : pitch fraction                          */
     461             :     int16_t *T0_min,           /* i/o: delta search min                        */
     462             :     int16_t *T0_max,           /* i/o: delta search max                        */
     463             :     int16_t *BER_detect        /* o  : BER detect flag                         */
     464             : )
     465             : {
     466     8790859 :     if ( nBits == 10 ) /* absolute decoding with 10 bits */
     467             :     {
     468     2683567 :         if ( limit_flag == 0 )
     469             :         {
     470           0 :             *T0 = PIT_MIN + ( pitch_index / 4 );
     471           0 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN ) * 4;
     472             :         }
     473     2683567 :         else if ( limit_flag == 1 )
     474             :         {
     475     2436671 :             *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
     476     2436671 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
     477             :         }
     478             :         else /* limit_flag == 2 */
     479             :         {
     480      246896 :             *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
     481      246896 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
     482             :         }
     483             :     }
     484     6107292 :     else if ( nBits == 9 ) /* absolute decoding with 9 bits */
     485             :     {
     486     1296097 :         abs_pit_dec( 4, pitch_index, limit_flag, T0, T0_frac );
     487             : 
     488             :         /* find T0_min and T0_max for delta search */
     489     1296097 :         if ( Opt_AMR_WB )
     490             :         {
     491        9838 :             limit_T0( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     492             :         }
     493             :     }
     494     4811195 :     else if ( nBits == 8 ) /* absolute decoding with 8 bits */
     495             :     {
     496       76232 :         abs_pit_dec( 2, pitch_index, limit_flag, T0, T0_frac );
     497             : 
     498             :         /* find T0_min and T0_max for delta search */
     499       76232 :         if ( Opt_AMR_WB )
     500             :         {
     501        1470 :             limit_T0( L_FRAME, delta, pit_flag, 0, *T0, 0, T0_min, T0_max ); /* T0_frac==0 to keep IO with AMR-WB */
     502             :         }
     503             :     }
     504     4734963 :     else if ( nBits == 6 ) /* relative decoding with 6 bits */
     505             :     {
     506     3814938 :         delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     507             :     }
     508      920025 :     else if ( nBits == 5 ) /* relative decoding with 5 bits */
     509             :     {
     510      920025 :         if ( delta == 8 )
     511             :         {
     512      150027 :             delta_pit_dec( 2, pitch_index, T0, T0_frac, *T0_min );
     513             :         }
     514             :         else /* delta == 4 */
     515             :         {
     516      769998 :             delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     517             :         }
     518             :     }
     519             :     else /* nBits == 4 */ /* relative decoding with 4 bits */
     520             :     {
     521           0 :         if ( delta == 8 )
     522             :         {
     523           0 :             delta_pit_dec( 0, pitch_index, T0, T0_frac, *T0_min );
     524             :         }
     525             :         else /* delta == 4 */
     526             :         {
     527           0 :             delta_pit_dec( 2, pitch_index, T0, T0_frac, *T0_min );
     528             :         }
     529             :     }
     530             : 
     531             :     /* biterror detection mechanism */
     532     8790859 :     if ( ( ( *T0 << 2 ) + *T0_frac ) > ( PIT_MAX << 2 ) + 2 && pit_flag == 0 && !Opt_AMR_WB )
     533             :     {
     534           0 :         *T0 = L_SUBFR;
     535           0 :         *T0_frac = 0;
     536           0 :         *BER_detect = 1;
     537             :     }
     538             : 
     539     8790859 :     if ( !Opt_AMR_WB )
     540             :     {
     541             :         /* find T0_min and T0_max for delta search */
     542     8767119 :         limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     543             :     }
     544             : 
     545     8790859 :     return;
     546             : }
     547             : 
     548             : /*-------------------------------------------------*
     549             :  * pit16k_Q_dec()
     550             :  *
     551             :  * pitch decoding @16kHz core
     552             :  *-------------------------------------------------*/
     553             : 
     554    10780283 : void pit16k_Q_dec(
     555             :     const int16_t pitch_index, /* i  : pitch index                             */
     556             :     const int16_t nBits,       /* i  : # of Q bits                             */
     557             :     const int16_t limit_flag,  /* i  : restrained(0) or extended(1) limits     */
     558             :     int16_t *T0,               /* o  : integer pitch lag                       */
     559             :     int16_t *T0_frac,          /* o  : pitch fraction                          */
     560             :     int16_t *T0_min,           /* i/o: delta search min                        */
     561             :     int16_t *T0_max,           /* i/o: delta search max                        */
     562             :     int16_t *BER_detect        /* o  : BER detect flag                         */
     563             : )
     564             : {
     565             :     int16_t index;
     566             : 
     567    10780283 :     if ( nBits == 10 ) /* absolute decoding with 10 bits */
     568             :     {
     569             :         {
     570     4620551 :             if ( pitch_index < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
     571             :             {
     572     4534986 :                 *T0 = PIT16k_MIN_EXTEND + ( pitch_index / 4 );
     573     4534986 :                 *T0_frac = pitch_index - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
     574             :             }
     575             :             else
     576             :             {
     577       85565 :                 index = pitch_index - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
     578       85565 :                 *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
     579       85565 :                 *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
     580       85565 :                 ( *T0_frac ) *= 2;
     581             :             }
     582             :         }
     583             :     }
     584     6159732 :     else if ( nBits == 9 ) /* absolute decoding with 9 bits */
     585             :     {
     586             :         {
     587           0 :             if ( pitch_index < ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 )
     588             :             {
     589           0 :                 *T0 = PIT16k_MIN_EXTEND + ( pitch_index / 4 );
     590           0 :                 *T0_frac = pitch_index - ( *T0 - PIT16k_MIN_EXTEND ) * 4;
     591             :             }
     592           0 :             else if ( pitch_index < ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 + ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 ) )
     593             :             {
     594           0 :                 index = pitch_index - ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4;
     595           0 :                 *T0 = PIT16k_FR2_EXTEND_9b + ( index / 2 );
     596           0 :                 *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_9b ) * 2;
     597           0 :                 ( *T0_frac ) *= 2;
     598             :             }
     599             :             else
     600             :             {
     601           0 :                 *T0 = pitch_index + PIT16k_FR1_EXTEND_9b - ( ( PIT16k_FR2_EXTEND_9b - PIT16k_MIN_EXTEND ) * 4 ) - ( ( PIT16k_FR1_EXTEND_9b - PIT16k_FR2_EXTEND_9b ) * 2 );
     602           0 :                 *T0_frac = 0;
     603             :             }
     604             :         }
     605             :     }
     606             :     else /* nBits == 6 */ /* relative decoding with 6 bits */
     607             :     {
     608     6159732 :         delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     609             :     }
     610             : 
     611             :     /* biterror detection mechanism */
     612    10780283 :     if ( ( ( *T0 << 2 ) + *T0_frac ) > ( PIT16k_MAX << 2 ) && nBits >= 9 )
     613             :     {
     614           0 :         *T0 = L_SUBFR;
     615           0 :         *T0_frac = 0;
     616           0 :         *BER_detect = 1;
     617             :     }
     618             : 
     619             :     /* find T0_min and T0_max for delta search */
     620    10780283 :     limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     621             : 
     622    10780283 :     return;
     623             : }
     624             : 
     625             : 
     626             : /*----------------------------------------------------------*
     627             :  * Mode2_pit_decode()
     628             :  *
     629             :  * Decode pitch lag
     630             :  *----------------------------------------------------------*/
     631             : 
     632             : /*! r: floating pitch value */
     633      147028 : float Mode2_pit_decode(
     634             :     const int16_t coder_type, /* i  :   coding model                            */
     635             :     const int16_t i_subfr,    /* i  :   subframe index                          */
     636             :     const int16_t L_subfr,    /* i  : sub-frame length                */
     637             :     int16_t **pt_indice,      /* i/o: quantization indices pointer              */
     638             :     int16_t *T0,              /* i/o: close loop integer pitch                  */
     639             :     int16_t *T0_frac,         /* o  : close loop fractional part of the pitch   */
     640             :     int16_t *T0_res,          /* i/o: pitch resolution                          */
     641             :     int16_t *T0_min,          /* i/o: lower limit for close-loop search         */
     642             :     int16_t *T0_min_frac,     /* i/o: lower limit for close-loop search         */
     643             :     int16_t *T0_max,          /* i/o: higher limit for close-loop search        */
     644             :     int16_t *T0_max_frac,     /* i/o: higher limit for close-loop search        */
     645             :     const int16_t pit_min,
     646             :     const int16_t pit_fr1,
     647             :     const int16_t pit_fr1b,
     648             :     const int16_t pit_fr2,
     649             :     const int16_t pit_max,
     650             :     const int16_t pit_res_max )
     651             : {
     652             :     float pitch;
     653             :     int16_t pit_res_max2;
     654             : 
     655      147028 :     if ( coder_type == 0 ) /*Unvoiced Coding do nothing*/
     656             :     {
     657           0 :         *T0 = L_subfr;
     658           0 :         *T0_frac = 0;
     659           0 :         *T0_res = 1;
     660             :     }
     661      147028 :     else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
     662             :     {
     663        3868 :         if ( i_subfr == 0 )
     664             :         {
     665         967 :             Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
     666             :         }
     667             :         else
     668             :         {
     669        2901 :             limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
     670        2901 :             *T0_res = ( pit_res_max >> 1 );
     671        2901 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     672             :         }
     673             :     }
     674      143160 :     else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
     675             :     {
     676             : 
     677        8336 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
     678             :         {
     679        4168 :             Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
     680             :         }
     681             :         else
     682             :         {
     683        4168 :             limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
     684        4168 :             *T0_res = ( pit_res_max >> 1 );
     685        4168 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     686             :         }
     687             :     }
     688      134824 :     else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
     689             :     {
     690       59955 :         pit_res_max2 = pit_res_max;
     691       59955 :         if ( pit_min == PIT_MIN_16k )
     692             :         {
     693       53075 :             pit_res_max2 = pit_res_max >> 1;
     694             :         }
     695             : 
     696       59955 :         if ( i_subfr == 0 )
     697             :         {
     698       12335 :             Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
     699             :         }
     700             :         else
     701             :         {
     702       47620 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
     703       47620 :             *T0_res = pit_res_max2;
     704       47620 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     705             :         }
     706             :     }
     707       74869 :     else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
     708             :     {
     709       74181 :         pit_res_max2 = pit_res_max;
     710       74181 :         if ( pit_min == PIT_MIN_16k )
     711             :         {
     712       69825 :             pit_res_max2 = pit_res_max >> 1;
     713             :         }
     714             : 
     715       74181 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
     716             :         {
     717       30108 :             Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1, pit_fr2, pit_res_max );
     718             :         }
     719             :         else
     720             :         {
     721       44073 :             limit_T0_voiced( 6, pit_res_max2, *T0, 0, 1, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
     722       44073 :             *T0_res = pit_res_max2;
     723       44073 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     724             :         }
     725             :     }
     726         688 :     else if ( coder_type == 8 ) /* 8/5/5/5 (RF all pred mode) */
     727             :     {
     728         496 :         if ( i_subfr == 0 )
     729             :         {
     730         124 :             Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
     731             :         }
     732             :         else
     733             :         {
     734         372 :             limit_T0_voiced( 5, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
     735         372 :             *T0_res = ( pit_res_max >> 1 );
     736         372 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     737             :         }
     738             :     }
     739         192 :     else if ( coder_type == 9 ) /* 8/0/8/0 (RF gen pred mode) */
     740             :     {
     741         192 :         if ( i_subfr == 0 )
     742             :         {
     743          48 :             Mode2_abs_pit_dec( T0, T0_frac, T0_res, pt_indice, pit_min, pit_fr1b, pit_min, pit_res_max );
     744             :         }
     745             :         else
     746             :         {
     747         144 :             limit_T0_voiced( 4, pit_res_max >> 1, *T0, *T0_frac, *T0_res, T0_min, T0_min_frac, T0_max, T0_max_frac, pit_min, pit_max );
     748         144 :             *T0_res = ( pit_res_max >> 1 );
     749         144 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     750             :         }
     751             :     }
     752             :     else /*RCELP 8 bits pitch delay*/
     753             :     {
     754           0 :         assert( 0 );
     755             :     }
     756             : 
     757             :     /*-------------------------------------------------------*
     758             :      * Compute floating pitch output
     759             :      *-------------------------------------------------------*/
     760             : 
     761      147028 :     pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / (float) ( *T0_res ); /* save subframe pitch values  */
     762             : 
     763      147028 :     return pitch;
     764             : }
     765             : 
     766             : 
     767             : /*---------------------------------------------------------------------*
     768             :  * Mode2_abs_pit_dec()
     769             :  *
     770             :  * Decode the absolute pitch
     771             :  *---------------------------------------------------------------------*/
     772             : 
     773       47750 : void Mode2_abs_pit_dec(
     774             :     int16_t *T0,         /* o  : integer pitch lag              */
     775             :     int16_t *T0_frac,    /* o  : pitch fraction                 */
     776             :     int16_t *T0_res,     /* o  : pitch resolution               */
     777             :     int16_t **pt_indice, /* i/o: pointer to Vector of Q indexes */
     778             :     const int16_t pit_min,
     779             :     const int16_t pit_fr1,
     780             :     const int16_t pit_fr2,
     781             :     const int16_t pit_res_max )
     782             : {
     783             :     int16_t index;
     784             :     int16_t pit_res_max_half;
     785             : 
     786       47750 :     index = **pt_indice;
     787       47750 :     ( *pt_indice )++;
     788       47750 :     pit_res_max_half = pit_res_max >> 1;
     789             : 
     790       47750 :     if ( index < ( pit_fr2 - pit_min ) * pit_res_max )
     791             :     {
     792        3559 :         *T0 = pit_min + ( index / pit_res_max );
     793             : 
     794        3559 :         *T0_frac = index - ( *T0 - pit_min ) * pit_res_max;
     795        3559 :         *T0_res = pit_res_max;
     796             :     }
     797       44191 :     else if ( index < ( ( pit_fr2 - pit_min ) * pit_res_max + ( pit_fr1 - pit_fr2 ) * pit_res_max_half ) )
     798             :     {
     799       35644 :         index -= ( pit_fr2 - pit_min ) * pit_res_max;
     800             : 
     801       35644 :         *T0 = pit_fr2 + ( index / pit_res_max_half );
     802             : 
     803       35644 :         *T0_frac = index - ( *T0 - pit_fr2 ) * pit_res_max_half;
     804       35644 :         *T0_res = pit_res_max_half;
     805             :     }
     806             :     else
     807             :     {
     808        8547 :         *T0 = index + pit_fr1 - ( ( pit_fr2 - pit_min ) * pit_res_max ) - ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
     809        8547 :         *T0_frac = 0;
     810        8547 :         *T0_res = 1;
     811             :     }
     812             : 
     813       47750 :     return;
     814             : }
     815             : 
     816             : 
     817             : /*---------------------------------------------------------------------*
     818             :  * Routine Mode2_delta_pit_dec()
     819             :  *
     820             :  * Decode delta pitch
     821             :  *---------------------------------------------------------------------*/
     822             : 
     823       99278 : void Mode2_delta_pit_dec(
     824             :     int16_t *T0,          /* o  : integer pitch lag              */
     825             :     int16_t *T0_frac,     /* o  : pitch fraction                 */
     826             :     int16_t T0_res,       /* i  : pitch resolution               */
     827             :     int16_t *T0_min,      /* i/o: delta search min               */
     828             :     int16_t *T0_min_frac, /* i  : delta search min               */
     829             :     int16_t **pt_indice   /* i/o: pointer to Vector of Q indexes */
     830             : )
     831             : {
     832             :     int16_t index;
     833             : 
     834       99278 :     index = **pt_indice;
     835       99278 :     ( *pt_indice )++;
     836             : 
     837       99278 :     *T0 = *T0_min + ( index + *T0_min_frac ) / T0_res;
     838             : 
     839       99278 :     *T0_frac = index + *T0_min_frac - ( *T0 - *T0_min ) * T0_res;
     840             : 
     841       99278 :     return;
     842             : }

Generated by: LCOV version 1.14