LCOV - code coverage report
Current view: top level - lib_dec - pit_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 231 267 86.5 %
Date: 2025-09-14 08:49:17 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    10652154 : 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    10652154 :     pitch_index = 0;
      76             : 
      77             :     /*----------------------------------------------------------------*
      78             :      * Set pit_flag = 0 for every subframe with absolute pitch search
      79             :      *----------------------------------------------------------------*/
      80             : 
      81    10652154 :     pit_flag = i_subfr;
      82    10652154 :     if ( i_subfr == 2 * L_SUBFR )
      83             :     {
      84     2363110 :         pit_flag = 0;
      85             :     }
      86             : 
      87             :     /*-------------------------------------------------------*
      88             :      * Retrieve the pitch index
      89             :      *-------------------------------------------------------*/
      90             : 
      91    10652154 :     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    10628414 :         if ( i_subfr == 0 )
      98             :         {
      99     2421868 :             *limit_flag = 1;
     100             : 
     101     2421868 :             if ( coder_type == VOICED )
     102             :             {
     103      137875 :                 *limit_flag = 2; /* double-extended limits */
     104             :             }
     105             : 
     106     2421868 :             if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
     107             :             {
     108        8450 :                 *limit_flag = 0;
     109             :             }
     110             :         }
     111     8206546 :         else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
     112             :         {
     113      687286 :             if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
     114             :             {
     115      544660 :                 *limit_flag = 0;
     116             :             }
     117             :         }
     118             : 
     119             :         /*-------------------------------------------------------*
     120             :          *  Retrieve the number of Q bits
     121             :          *-------------------------------------------------------*/
     122             : 
     123    10628414 :         nBits = 0;
     124    10628414 :         if ( coder_type != AUDIO )
     125             :         {
     126    10183361 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_subfr];
     127             : 
     128    10183361 :             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    10628414 :         if ( coder_type == AUDIO )
     137             :         {
     138      445053 :             if ( L_subfr == L_FRAME / 2 && i_subfr != 0 )
     139             :             {
     140       33983 :                 pit_flag = L_SUBFR;
     141             :             }
     142             : 
     143      445053 :             if ( pit_flag == 0 )
     144             :             {
     145      248754 :                 nBits = 10;
     146             :             }
     147             :             else
     148             :             {
     149      196299 :                 nBits = 6;
     150             :             }
     151             : 
     152      445053 :             pitch_index = get_next_indice( st, nBits );
     153             : 
     154      445053 :             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      445053 :             pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     161             :         }
     162    10183361 :         else if ( coder_type == VOICED )
     163             :         {
     164             :             /*-------------------------------------------------------*
     165             :              * Pitch decoding in VOICED mode
     166             :              * (ACELP@12k8 core only)
     167             :              *-------------------------------------------------------*/
     168             : 
     169      551500 :             if ( i_subfr == 2 * L_SUBFR )
     170             :             {
     171      137875 :                 pit_flag = i_subfr;
     172             :             }
     173             : 
     174      551500 :             pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     175             :         }
     176     9631861 :         else if ( st->idchan == 1 && ( tdm_Pitch_reuse_flag == 1 || nBits == 4 ) )
     177         672 :         {
     178             :             /*-------------------------------------------------------*
     179             :              *  Pitch decoding with reusing of primary channel information
     180             :              *-------------------------------------------------------*/
     181             :             int16_t loc_T0, loc_frac, delta;
     182             : 
     183         672 :             delta = 4;
     184         672 :             pit_flag = L_SUBFR;
     185             : 
     186         672 :             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         672 :                 loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
     194         672 :                 loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
     195             :             }
     196             : 
     197         672 :             limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     198             : 
     199         672 :             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         672 :                 *T0 = loc_T0;
     206         672 :                 *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     9631189 :             if ( L_frame == L_FRAME )
     217             :             {
     218     3566844 :                 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     6064345 :                 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    10652154 :     pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     264             : 
     265    10652154 :     return pitch;
     266             : }
     267             : 
     268             : 
     269             : /*---------------------------------------------------------------------*
     270             :  * abs_pit_dec()
     271             :  *
     272             :  * Decode the absolute pitch
     273             :  *---------------------------------------------------------------------*/
     274             : 
     275      808712 : 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      808712 :     if ( limit_flag == 0 )
     284             :     {
     285      631041 :         if ( fr_steps == 2 )
     286             :         {
     287       35978 :             if ( pitch_index < ( PIT_FR1_8b - PIT_MIN ) * 2 )
     288             :             {
     289       18683 :                 *T0 = PIT_MIN + ( pitch_index / 2 );
     290       18683 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN ) * 2 );
     291       18683 :                 *T0_frac *= 2;
     292             :             }
     293             :             else
     294             :             {
     295       17295 :                 *T0 = pitch_index + PIT_FR1_8b - ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
     296       17295 :                 *T0_frac = 0;
     297             :             }
     298             :         }
     299      595063 :         else if ( fr_steps == 4 )
     300             :         {
     301      595063 :             if ( pitch_index < ( PIT_FR2_9b - PIT_MIN ) * 4 )
     302             :             {
     303      514408 :                 *T0 = PIT_MIN + ( pitch_index / 4 );
     304      514408 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN ) * 4;
     305             :             }
     306       80655 :             else if ( pitch_index < ( ( PIT_FR2_9b - PIT_MIN ) * 4 + ( PIT_FR1_9b - PIT_FR2_9b ) * 2 ) )
     307             :             {
     308       42143 :                 pitch_index -= ( PIT_FR2_9b - PIT_MIN ) * 4;
     309       42143 :                 *T0 = PIT_FR2_9b + ( pitch_index / 2 );
     310       42143 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_9b ) * 2;
     311       42143 :                 ( *T0_frac ) *= 2;
     312             :             }
     313             :             else
     314             :             {
     315       38512 :                 *T0 = pitch_index + PIT_FR1_9b - ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) - ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
     316       38512 :                 *T0_frac = 0;
     317             :             }
     318             :         }
     319             :         else /* fr_steps == 0 */
     320             :         {
     321             :             /* not used in the codec */
     322             :         }
     323             :     }
     324      177671 :     else if ( limit_flag == 1 ) /* extended Q range */
     325             :     {
     326      171401 :         if ( fr_steps == 2 )
     327             :         {
     328       13415 :             if ( pitch_index < ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 )
     329             :             {
     330        6525 :                 *T0 = PIT_MIN_EXTEND + ( pitch_index / 2 );
     331        6525 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN_EXTEND ) * 2 );
     332        6525 :                 *T0_frac *= 2;
     333             :             }
     334             :             else
     335             :             {
     336        6890 :                 *T0 = pitch_index + PIT_FR1_EXTEND_8b - ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
     337        6890 :                 *T0_frac = 0;
     338             :             }
     339             :         }
     340      157986 :         else if ( fr_steps == 4 )
     341             :         {
     342      157986 :             if ( pitch_index < ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 )
     343             :             {
     344      133261 :                 *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
     345      133261 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
     346             :             }
     347       24725 :             else if ( pitch_index < ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 + ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 ) )
     348             :             {
     349        6146 :                 pitch_index -= ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4;
     350        6146 :                 *T0 = PIT_FR2_EXTEND_9b + ( pitch_index / 2 );
     351        6146 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_EXTEND_9b ) * 2;
     352        6146 :                 ( *T0_frac ) *= 2;
     353             :             }
     354             :             else
     355             :             {
     356       18579 :                 *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       18579 :                 *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        6270 :         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        6270 :         else if ( fr_steps == 4 )
     382             :         {
     383        6270 :             if ( pitch_index < ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 )
     384             :             {
     385        6004 :                 *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
     386        6004 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
     387             :             }
     388         266 :             else if ( pitch_index < ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 + ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 ) )
     389             :             {
     390          90 :                 pitch_index -= ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4;
     391          90 :                 *T0 = PIT_FR2_DOUBLEEXTEND_9b + ( pitch_index / 2 );
     392          90 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_DOUBLEEXTEND_9b ) * 2;
     393          90 :                 ( *T0_frac ) *= 2;
     394             :             }
     395             :             else
     396             :             {
     397         176 :                 *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         176 :                 *T0_frac = 0;
     399             :             }
     400             :         }
     401             :         else /* fr_steps == 0 */
     402             :         {
     403             :             /* not used in the codec */
     404             :         }
     405             :     }
     406             : 
     407      808712 :     return;
     408             : }
     409             : 
     410             : 
     411             : /*---------------------------------------------------------------------*
     412             :  * delta_pit_dec()
     413             :  *
     414             :  * Decode delta pitch
     415             :  *---------------------------------------------------------------------*/
     416             : 
     417     6462671 : 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     6462671 :     if ( fr_steps == 0 )
     426             :     {
     427        6095 :         *T0 = T0_min + pitch_index;
     428        6095 :         *T0_frac = 0;
     429             :     }
     430     6456576 :     else if ( fr_steps == 2 )
     431             :     {
     432      130010 :         *T0 = T0_min + pitch_index / 2;
     433      130010 :         *T0_frac = pitch_index - ( *T0 - T0_min ) * 2;
     434      130010 :         *T0_frac *= 2;
     435             :     }
     436     6326566 :     else if ( fr_steps == 4 )
     437             :     {
     438     6326566 :         *T0 = T0_min + pitch_index / 4;
     439     6326566 :         *T0_frac = pitch_index - ( *T0 - T0_min ) * 4;
     440             :     }
     441             : 
     442     6462671 :     return;
     443             : }
     444             : 
     445             : 
     446             : /*-------------------------------------------------*
     447             :  * pit_Q_dec()
     448             :  *
     449             :  * pitch decoding
     450             :  *-------------------------------------------------*/
     451             : 
     452     4793466 : 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     4793466 :     if ( nBits == 10 ) /* absolute decoding with 10 bits */
     467             :     {
     468     1441388 :         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     1441388 :         else if ( limit_flag == 1 )
     474             :         {
     475     1309783 :             *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
     476     1309783 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
     477             :         }
     478             :         else /* limit_flag == 2 */
     479             :         {
     480      131605 :             *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
     481      131605 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
     482             :         }
     483             :     }
     484     3352078 :     else if ( nBits == 9 ) /* absolute decoding with 9 bits */
     485             :     {
     486      727901 :         abs_pit_dec( 4, pitch_index, limit_flag, T0, T0_frac );
     487             : 
     488             :         /* find T0_min and T0_max for delta search */
     489      727901 :         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     2624177 :     else if ( nBits == 8 ) /* absolute decoding with 8 bits */
     495             :     {
     496       49210 :         abs_pit_dec( 2, pitch_index, limit_flag, T0, T0_frac );
     497             : 
     498             :         /* find T0_min and T0_max for delta search */
     499       49210 :         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     2574967 :     else if ( nBits == 6 ) /* relative decoding with 6 bits */
     505             :     {
     506     2070420 :         delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     507             :     }
     508      504547 :     else if ( nBits == 5 ) /* relative decoding with 5 bits */
     509             :     {
     510      504547 :         if ( delta == 8 )
     511             :         {
     512       90922 :             delta_pit_dec( 2, pitch_index, T0, T0_frac, *T0_min );
     513             :         }
     514             :         else /* delta == 4 */
     515             :         {
     516      413625 :             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     4793466 :     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     4793466 :     if ( !Opt_AMR_WB )
     540             :     {
     541             :         /* find T0_min and T0_max for delta search */
     542     4769726 :         limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     543             :     }
     544             : 
     545     4793466 :     return;
     546             : }
     547             : 
     548             : /*-------------------------------------------------*
     549             :  * pit16k_Q_dec()
     550             :  *
     551             :  * pitch decoding @16kHz core
     552             :  *-------------------------------------------------*/
     553             : 
     554     6314889 : 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     6314889 :     if ( nBits == 10 ) /* absolute decoding with 10 bits */
     568             :     {
     569             :         {
     570     2683578 :             if ( pitch_index < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
     571             :             {
     572     2641669 :                 *T0 = PIT16k_MIN_EXTEND + ( pitch_index / 4 );
     573     2641669 :                 *T0_frac = pitch_index - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
     574             :             }
     575             :             else
     576             :             {
     577       41909 :                 index = pitch_index - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
     578       41909 :                 *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
     579       41909 :                 *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
     580       41909 :                 ( *T0_frac ) *= 2;
     581             :             }
     582             :         }
     583             :     }
     584     3631311 :     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     3631311 :         delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     609             :     }
     610             : 
     611             :     /* biterror detection mechanism */
     612     6314889 :     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     6314889 :     limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     621             : 
     622     6314889 :     return;
     623             : }
     624             : 
     625             : 
     626             : /*----------------------------------------------------------*
     627             :  * Mode2_pit_decode()
     628             :  *
     629             :  * Decode pitch lag
     630             :  *----------------------------------------------------------*/
     631             : 
     632             : /*! r: floating pitch value */
     633       86904 : 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       86904 :     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       86904 :     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       83036 :     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       74700 :     else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
     689             :     {
     690       36125 :         pit_res_max2 = pit_res_max;
     691       36125 :         if ( pit_min == PIT_MIN_16k )
     692             :         {
     693       29245 :             pit_res_max2 = pit_res_max >> 1;
     694             :         }
     695             : 
     696       36125 :         if ( i_subfr == 0 )
     697             :         {
     698        7569 :             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       28556 :             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       28556 :             *T0_res = pit_res_max2;
     704       28556 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     705             :         }
     706             :     }
     707       38575 :     else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
     708             :     {
     709       37887 :         pit_res_max2 = pit_res_max;
     710       37887 :         if ( pit_min == PIT_MIN_16k )
     711             :         {
     712       33555 :             pit_res_max2 = pit_res_max >> 1;
     713             :         }
     714             : 
     715       37887 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
     716             :         {
     717       15588 :             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       22299 :             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       22299 :             *T0_res = pit_res_max2;
     723       22299 :             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       86904 :     pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / (float) ( *T0_res ); /* save subframe pitch values  */
     762             : 
     763       86904 :     return pitch;
     764             : }
     765             : 
     766             : 
     767             : /*---------------------------------------------------------------------*
     768             :  * Mode2_abs_pit_dec()
     769             :  *
     770             :  * Decode the absolute pitch
     771             :  *---------------------------------------------------------------------*/
     772             : 
     773       28464 : 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       28464 :     index = **pt_indice;
     787       28464 :     ( *pt_indice )++;
     788       28464 :     pit_res_max_half = pit_res_max >> 1;
     789             : 
     790       28464 :     if ( index < ( pit_fr2 - pit_min ) * pit_res_max )
     791             :     {
     792        3549 :         *T0 = pit_min + ( index / pit_res_max );
     793             : 
     794        3549 :         *T0_frac = index - ( *T0 - pit_min ) * pit_res_max;
     795        3549 :         *T0_res = pit_res_max;
     796             :     }
     797       24915 :     else if ( index < ( ( pit_fr2 - pit_min ) * pit_res_max + ( pit_fr1 - pit_fr2 ) * pit_res_max_half ) )
     798             :     {
     799       19408 :         index -= ( pit_fr2 - pit_min ) * pit_res_max;
     800             : 
     801       19408 :         *T0 = pit_fr2 + ( index / pit_res_max_half );
     802             : 
     803       19408 :         *T0_frac = index - ( *T0 - pit_fr2 ) * pit_res_max_half;
     804       19408 :         *T0_res = pit_res_max_half;
     805             :     }
     806             :     else
     807             :     {
     808        5507 :         *T0 = index + pit_fr1 - ( ( pit_fr2 - pit_min ) * pit_res_max ) - ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
     809        5507 :         *T0_frac = 0;
     810        5507 :         *T0_res = 1;
     811             :     }
     812             : 
     813       28464 :     return;
     814             : }
     815             : 
     816             : 
     817             : /*---------------------------------------------------------------------*
     818             :  * Routine Mode2_delta_pit_dec()
     819             :  *
     820             :  * Decode delta pitch
     821             :  *---------------------------------------------------------------------*/
     822             : 
     823       58440 : 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       58440 :     index = **pt_indice;
     835       58440 :     ( *pt_indice )++;
     836             : 
     837       58440 :     *T0 = *T0_min + ( index + *T0_min_frac ) / T0_res;
     838             : 
     839       58440 :     *T0_frac = index + *T0_min_frac - ( *T0 - *T0_min ) * T0_res;
     840             : 
     841       58440 :     return;
     842             : }

Generated by: LCOV version 1.14