LCOV - code coverage report
Current view: top level - lib_dec - pit_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 231 267 86.5 %
Date: 2025-10-27 07:01: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     5700215 : 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     5700215 :     pitch_index = 0;
      76             : 
      77             :     /*----------------------------------------------------------------*
      78             :      * Set pit_flag = 0 for every subframe with absolute pitch search
      79             :      *----------------------------------------------------------------*/
      80             : 
      81     5700215 :     pit_flag = i_subfr;
      82     5700215 :     if ( i_subfr == 2 * L_SUBFR )
      83             :     {
      84     1290827 :         pit_flag = 0;
      85             :     }
      86             : 
      87             :     /*-------------------------------------------------------*
      88             :      * Retrieve the pitch index
      89             :      *-------------------------------------------------------*/
      90             : 
      91     5700215 :     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     5685815 :         if ( i_subfr == 0 )
      98             :         {
      99     1330560 :             *limit_flag = 1;
     100             : 
     101     1330560 :             if ( coder_type == VOICED )
     102             :             {
     103       83886 :                 *limit_flag = 2; /* double-extended limits */
     104             :             }
     105             : 
     106     1330560 :             if ( coder_type == GENERIC && core_brate == ACELP_7k20 )
     107             :             {
     108        4705 :                 *limit_flag = 0;
     109             :             }
     110             :         }
     111     4355255 :         else if ( i_subfr == 2 * L_SUBFR && coder_type == GENERIC && core_brate <= ACELP_13k20 )
     112             :         {
     113      461428 :             if ( *T0 > ( PIT_FR1_EXTEND_8b + PIT_MIN ) >> 1 )
     114             :             {
     115      376507 :                 *limit_flag = 0;
     116             :             }
     117             :         }
     118             : 
     119             :         /*-------------------------------------------------------*
     120             :          *  Retrieve the number of Q bits
     121             :          *-------------------------------------------------------*/
     122             : 
     123     5685815 :         nBits = 0;
     124     5685815 :         if ( coder_type != AUDIO )
     125             :         {
     126     5387608 :             nBits = st->acelp_cfg.pitch_bits[i_subfr / L_subfr];
     127             : 
     128     5387608 :             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     5685815 :         if ( coder_type == AUDIO )
     137             :         {
     138      298207 :             if ( L_subfr == L_FRAME / 2 && i_subfr != 0 )
     139             :             {
     140       20967 :                 pit_flag = L_SUBFR;
     141             :             }
     142             : 
     143      298207 :             if ( pit_flag == 0 )
     144             :             {
     145      168380 :                 nBits = 10;
     146             :             }
     147             :             else
     148             :             {
     149      129827 :                 nBits = 6;
     150             :             }
     151             : 
     152      298207 :             pitch_index = get_next_indice( st, nBits );
     153             : 
     154      298207 :             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      298207 :             pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     161             :         }
     162     5387608 :         else if ( coder_type == VOICED )
     163             :         {
     164             :             /*-------------------------------------------------------*
     165             :              * Pitch decoding in VOICED mode
     166             :              * (ACELP@12k8 core only)
     167             :              *-------------------------------------------------------*/
     168             : 
     169      335544 :             if ( i_subfr == 2 * L_SUBFR )
     170             :             {
     171       83886 :                 pit_flag = i_subfr;
     172             :             }
     173             : 
     174      335544 :             pit_Q_dec( 0, pitch_index, nBits, 4, pit_flag, *limit_flag, T0, T0_frac, T0_min, T0_max, &st->BER_detect );
     175             :         }
     176     5052064 :         else if ( st->idchan == 1 && ( tdm_Pitch_reuse_flag == 1 || nBits == 4 ) )
     177         396 :         {
     178             :             /*-------------------------------------------------------*
     179             :              *  Pitch decoding with reusing of primary channel information
     180             :              *-------------------------------------------------------*/
     181             :             int16_t loc_T0, loc_frac, delta;
     182             : 
     183         396 :             delta = 4;
     184         396 :             pit_flag = L_SUBFR;
     185             : 
     186         396 :             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         396 :                 loc_T0 = (int16_t) tdm_Pri_pitch_buf[i_subfr / L_SUBFR];
     194         396 :                 loc_frac = (int16_t) ( ( tdm_Pri_pitch_buf[i_subfr / L_SUBFR] - loc_T0 ) * 4.0f );
     195             :             }
     196             : 
     197         396 :             limit_T0( L_FRAME, delta, pit_flag, *limit_flag, loc_T0, loc_frac, T0_min, T0_max );
     198             : 
     199         396 :             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         396 :                 *T0 = loc_T0;
     206         396 :                 *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     5051668 :             if ( L_frame == L_FRAME )
     217             :             {
     218     2360079 :                 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     2691589 :                 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       14400 :         *limit_flag = 0;
     234             : 
     235       14400 :         if ( i_subfr == 0 || ( i_subfr == 2 * L_SUBFR && core_brate == ACELP_8k85 ) )
     236             :         {
     237        4024 :             nBits = 8;
     238             :         }
     239             :         else
     240             :         {
     241       10376 :             nBits = 5;
     242             :         }
     243             : 
     244       14400 :         if ( core_brate > ACELP_8k85 )
     245             :         {
     246       10976 :             nBits = 6;
     247             : 
     248       10976 :             if ( i_subfr == 0 || i_subfr == 2 * L_SUBFR )
     249             :             {
     250        5488 :                 nBits = 9;
     251             :             }
     252             :         }
     253             : 
     254       14400 :         pitch_index = get_next_indice( st, nBits );
     255             : 
     256       14400 :         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     5700215 :     pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / 4.0f;
     264             : 
     265     5700215 :     return pitch;
     266             : }
     267             : 
     268             : 
     269             : /*---------------------------------------------------------------------*
     270             :  * abs_pit_dec()
     271             :  *
     272             :  * Decode the absolute pitch
     273             :  *---------------------------------------------------------------------*/
     274             : 
     275      531926 : 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      531926 :     if ( limit_flag == 0 )
     284             :     {
     285      424094 :         if ( fr_steps == 2 )
     286             :         {
     287       22551 :             if ( pitch_index < ( PIT_FR1_8b - PIT_MIN ) * 2 )
     288             :             {
     289       11742 :                 *T0 = PIT_MIN + ( pitch_index / 2 );
     290       11742 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN ) * 2 );
     291       11742 :                 *T0_frac *= 2;
     292             :             }
     293             :             else
     294             :             {
     295       10809 :                 *T0 = pitch_index + PIT_FR1_8b - ( ( PIT_FR1_8b - PIT_MIN ) * 2 );
     296       10809 :                 *T0_frac = 0;
     297             :             }
     298             :         }
     299      401543 :         else if ( fr_steps == 4 )
     300             :         {
     301      401543 :             if ( pitch_index < ( PIT_FR2_9b - PIT_MIN ) * 4 )
     302             :             {
     303      367263 :                 *T0 = PIT_MIN + ( pitch_index / 4 );
     304      367263 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN ) * 4;
     305             :             }
     306       34280 :             else if ( pitch_index < ( ( PIT_FR2_9b - PIT_MIN ) * 4 + ( PIT_FR1_9b - PIT_FR2_9b ) * 2 ) )
     307             :             {
     308       16949 :                 pitch_index -= ( PIT_FR2_9b - PIT_MIN ) * 4;
     309       16949 :                 *T0 = PIT_FR2_9b + ( pitch_index / 2 );
     310       16949 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_9b ) * 2;
     311       16949 :                 ( *T0_frac ) *= 2;
     312             :             }
     313             :             else
     314             :             {
     315       17331 :                 *T0 = pitch_index + PIT_FR1_9b - ( ( PIT_FR2_9b - PIT_MIN ) * 4 ) - ( ( PIT_FR1_9b - PIT_FR2_9b ) * 2 );
     316       17331 :                 *T0_frac = 0;
     317             :             }
     318             :         }
     319             :         else /* fr_steps == 0 */
     320             :         {
     321             :             /* not used in the codec */
     322             :         }
     323             :     }
     324      107832 :     else if ( limit_flag == 1 ) /* extended Q range */
     325             :     {
     326      104061 :         if ( fr_steps == 2 )
     327             :         {
     328       10052 :             if ( pitch_index < ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 )
     329             :             {
     330        4464 :                 *T0 = PIT_MIN_EXTEND + ( pitch_index / 2 );
     331        4464 :                 *T0_frac = pitch_index - ( ( *T0 - PIT_MIN_EXTEND ) * 2 );
     332        4464 :                 *T0_frac *= 2;
     333             :             }
     334             :             else
     335             :             {
     336        5588 :                 *T0 = pitch_index + PIT_FR1_EXTEND_8b - ( ( PIT_FR1_EXTEND_8b - PIT_MIN_EXTEND ) * 2 );
     337        5588 :                 *T0_frac = 0;
     338             :             }
     339             :         }
     340       94009 :         else if ( fr_steps == 4 )
     341             :         {
     342       94009 :             if ( pitch_index < ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 )
     343             :             {
     344       78910 :                 *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
     345       78910 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
     346             :             }
     347       15099 :             else if ( pitch_index < ( ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4 + ( PIT_FR1_EXTEND_9b - PIT_FR2_EXTEND_9b ) * 2 ) )
     348             :             {
     349        4355 :                 pitch_index -= ( PIT_FR2_EXTEND_9b - PIT_MIN_EXTEND ) * 4;
     350        4355 :                 *T0 = PIT_FR2_EXTEND_9b + ( pitch_index / 2 );
     351        4355 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_EXTEND_9b ) * 2;
     352        4355 :                 ( *T0_frac ) *= 2;
     353             :             }
     354             :             else
     355             :             {
     356       10744 :                 *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       10744 :                 *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        3771 :         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        3771 :         else if ( fr_steps == 4 )
     382             :         {
     383        3771 :             if ( pitch_index < ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 )
     384             :             {
     385        3673 :                 *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
     386        3673 :                 *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
     387             :             }
     388          98 :             else if ( pitch_index < ( ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4 + ( PIT_FR1_DOUBLEEXTEND_9b - PIT_FR2_DOUBLEEXTEND_9b ) * 2 ) )
     389             :             {
     390          30 :                 pitch_index -= ( PIT_FR2_DOUBLEEXTEND_9b - PIT_MIN_DOUBLEEXTEND ) * 4;
     391          30 :                 *T0 = PIT_FR2_DOUBLEEXTEND_9b + ( pitch_index / 2 );
     392          30 :                 *T0_frac = pitch_index - ( *T0 - PIT_FR2_DOUBLEEXTEND_9b ) * 2;
     393          30 :                 ( *T0_frac ) *= 2;
     394             :             }
     395             :             else
     396             :             {
     397          68 :                 *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          68 :                 *T0_frac = 0;
     399             :             }
     400             :         }
     401             :         else /* fr_steps == 0 */
     402             :         {
     403             :             /* not used in the codec */
     404             :         }
     405             :     }
     406             : 
     407      531926 :     return;
     408             : }
     409             : 
     410             : 
     411             : /*---------------------------------------------------------------------*
     412             :  * delta_pit_dec()
     413             :  *
     414             :  * Decode delta pitch
     415             :  *---------------------------------------------------------------------*/
     416             : 
     417     3406745 : 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     3406745 :     if ( fr_steps == 0 )
     426             :     {
     427        4094 :         *T0 = T0_min + pitch_index;
     428        4094 :         *T0_frac = 0;
     429             :     }
     430     3402651 :     else if ( fr_steps == 2 )
     431             :     {
     432       77210 :         *T0 = T0_min + pitch_index / 2;
     433       77210 :         *T0_frac = pitch_index - ( *T0 - T0_min ) * 2;
     434       77210 :         *T0_frac *= 2;
     435             :     }
     436     3325441 :     else if ( fr_steps == 4 )
     437             :     {
     438     3325441 :         *T0 = T0_min + pitch_index / 4;
     439     3325441 :         *T0_frac = pitch_index - ( *T0 - T0_min ) * 4;
     440             :     }
     441             : 
     442     3406745 :     return;
     443             : }
     444             : 
     445             : 
     446             : /*-------------------------------------------------*
     447             :  * pit_Q_dec()
     448             :  *
     449             :  * pitch decoding
     450             :  *-------------------------------------------------*/
     451             : 
     452     3133503 : 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     3133503 :     if ( nBits == 10 ) /* absolute decoding with 10 bits */
     467             :     {
     468      943378 :         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      943378 :         else if ( limit_flag == 1 )
     474             :         {
     475      863263 :             *T0 = PIT_MIN_EXTEND + ( pitch_index / 4 );
     476      863263 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN_EXTEND ) * 4;
     477             :         }
     478             :         else /* limit_flag == 2 */
     479             :         {
     480       80115 :             *T0 = PIT_MIN_DOUBLEEXTEND + ( pitch_index / 4 );
     481       80115 :             *T0_frac = pitch_index - ( *T0 - PIT_MIN_DOUBLEEXTEND ) * 4;
     482             :         }
     483             :     }
     484     2190125 :     else if ( nBits == 9 ) /* absolute decoding with 9 bits */
     485             :     {
     486      484628 :         abs_pit_dec( 4, pitch_index, limit_flag, T0, T0_frac );
     487             : 
     488             :         /* find T0_min and T0_max for delta search */
     489      484628 :         if ( Opt_AMR_WB )
     490             :         {
     491        5488 :             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     1705497 :     else if ( nBits == 8 ) /* absolute decoding with 8 bits */
     495             :     {
     496       32521 :         abs_pit_dec( 2, pitch_index, limit_flag, T0, T0_frac );
     497             : 
     498             :         /* find T0_min and T0_max for delta search */
     499       32521 :         if ( Opt_AMR_WB )
     500             :         {
     501        1280 :             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     1672976 :     else if ( nBits == 6 ) /* relative decoding with 6 bits */
     505             :     {
     506     1362847 :         delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     507             :     }
     508      310129 :     else if ( nBits == 5 ) /* relative decoding with 5 bits */
     509             :     {
     510      310129 :         if ( delta == 8 )
     511             :         {
     512       58471 :             delta_pit_dec( 2, pitch_index, T0, T0_frac, *T0_min );
     513             :         }
     514             :         else /* delta == 4 */
     515             :         {
     516      251658 :             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     3133503 :     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     3133503 :     if ( !Opt_AMR_WB )
     540             :     {
     541             :         /* find T0_min and T0_max for delta search */
     542     3119103 :         limit_T0( L_FRAME, delta, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     543             :     }
     544             : 
     545     3133503 :     return;
     546             : }
     547             : 
     548             : /*-------------------------------------------------*
     549             :  * pit16k_Q_dec()
     550             :  *
     551             :  * pitch decoding @16kHz core
     552             :  *-------------------------------------------------*/
     553             : 
     554     2839581 : 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     2839581 :     if ( nBits == 10 ) /* absolute decoding with 10 bits */
     568             :     {
     569             :         {
     570     1227483 :             if ( pitch_index < ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4 )
     571             :             {
     572     1209445 :                 *T0 = PIT16k_MIN_EXTEND + ( pitch_index / 4 );
     573     1209445 :                 *T0_frac = pitch_index - ( ( *T0 - PIT16k_MIN_EXTEND ) * 4 );
     574             :             }
     575             :             else
     576             :             {
     577       18038 :                 index = pitch_index - ( PIT16k_FR2_EXTEND_10b - PIT16k_MIN_EXTEND ) * 4;
     578       18038 :                 *T0 = PIT16k_FR2_EXTEND_10b + ( index / 2 );
     579       18038 :                 *T0_frac = index - ( *T0 - PIT16k_FR2_EXTEND_10b ) * 2;
     580       18038 :                 ( *T0_frac ) *= 2;
     581             :             }
     582             :         }
     583             :     }
     584     1612098 :     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     1612098 :         delta_pit_dec( 4, pitch_index, T0, T0_frac, *T0_min );
     609             :     }
     610             : 
     611             :     /* biterror detection mechanism */
     612     2839581 :     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     2839581 :     limit_T0( L_FRAME16k, 8, L_SUBFR, limit_flag, *T0, *T0_frac, T0_min, T0_max );
     621             : 
     622     2839581 :     return;
     623             : }
     624             : 
     625             : 
     626             : /*----------------------------------------------------------*
     627             :  * Mode2_pit_decode()
     628             :  *
     629             :  * Decode pitch lag
     630             :  *----------------------------------------------------------*/
     631             : 
     632             : /*! r: floating pitch value */
     633       42272 : 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       42272 :     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       42272 :     else if ( coder_type == 1 ) /* 8/4/4/4 (EVS) */
     662             :     {
     663        2172 :         if ( i_subfr == 0 )
     664             :         {
     665         543 :             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        1629 :             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        1629 :             *T0_res = ( pit_res_max >> 1 );
     671        1629 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     672             :         }
     673             :     }
     674       40100 :     else if ( coder_type == 2 ) /* 8/5/8/5 (EVS) */
     675             :     {
     676             : 
     677        1472 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
     678             :         {
     679         736 :             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         736 :             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         736 :             *T0_res = ( pit_res_max >> 1 );
     685         736 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     686             :         }
     687             :     }
     688       38628 :     else if ( coder_type == 3 ) /* 9/6/6/6 (HRs- VC) */
     689             :     {
     690       20950 :         pit_res_max2 = pit_res_max;
     691       20950 :         if ( pit_min == PIT_MIN_16k )
     692             :         {
     693       14070 :             pit_res_max2 = pit_res_max >> 1;
     694             :         }
     695             : 
     696       20950 :         if ( i_subfr == 0 )
     697             :         {
     698        4534 :             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       16416 :             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       16416 :             *T0_res = pit_res_max2;
     704       16416 :             Mode2_delta_pit_dec( T0, T0_frac, *T0_res, T0_min, T0_min_frac, pt_indice );
     705             :         }
     706             :     }
     707       17678 :     else if ( coder_type == 4 ) /* 9/6/9/6 (AMRWB) */
     708             :     {
     709       16990 :         pit_res_max2 = pit_res_max;
     710       16990 :         if ( pit_min == PIT_MIN_16k )
     711             :         {
     712       12670 :             pit_res_max2 = pit_res_max >> 1;
     713             :         }
     714             : 
     715       16990 :         if ( ( i_subfr == 0 ) || ( i_subfr == 2 * L_subfr ) )
     716             :         {
     717        7228 :             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        9762 :             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        9762 :             *T0_res = pit_res_max2;
     723        9762 :             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       42272 :     pitch = (float) ( *T0 ) + (float) ( *T0_frac ) / (float) ( *T0_res ); /* save subframe pitch values  */
     762             : 
     763       42272 :     return pitch;
     764             : }
     765             : 
     766             : 
     767             : /*---------------------------------------------------------------------*
     768             :  * Mode2_abs_pit_dec()
     769             :  *
     770             :  * Decode the absolute pitch
     771             :  *---------------------------------------------------------------------*/
     772             : 
     773       13213 : 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       13213 :     index = **pt_indice;
     787       13213 :     ( *pt_indice )++;
     788       13213 :     pit_res_max_half = pit_res_max >> 1;
     789             : 
     790       13213 :     if ( index < ( pit_fr2 - pit_min ) * pit_res_max )
     791             :     {
     792        3544 :         *T0 = pit_min + ( index / pit_res_max );
     793             : 
     794        3544 :         *T0_frac = index - ( *T0 - pit_min ) * pit_res_max;
     795        3544 :         *T0_res = pit_res_max;
     796             :     }
     797        9669 :     else if ( index < ( ( pit_fr2 - pit_min ) * pit_res_max + ( pit_fr1 - pit_fr2 ) * pit_res_max_half ) )
     798             :     {
     799        8537 :         index -= ( pit_fr2 - pit_min ) * pit_res_max;
     800             : 
     801        8537 :         *T0 = pit_fr2 + ( index / pit_res_max_half );
     802             : 
     803        8537 :         *T0_frac = index - ( *T0 - pit_fr2 ) * pit_res_max_half;
     804        8537 :         *T0_res = pit_res_max_half;
     805             :     }
     806             :     else
     807             :     {
     808        1132 :         *T0 = index + pit_fr1 - ( ( pit_fr2 - pit_min ) * pit_res_max ) - ( ( pit_fr1 - pit_fr2 ) * pit_res_max_half );
     809        1132 :         *T0_frac = 0;
     810        1132 :         *T0_res = 1;
     811             :     }
     812             : 
     813       13213 :     return;
     814             : }
     815             : 
     816             : 
     817             : /*---------------------------------------------------------------------*
     818             :  * Routine Mode2_delta_pit_dec()
     819             :  *
     820             :  * Decode delta pitch
     821             :  *---------------------------------------------------------------------*/
     822             : 
     823       29059 : 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       29059 :     index = **pt_indice;
     835       29059 :     ( *pt_indice )++;
     836             : 
     837       29059 :     *T0 = *T0_min + ( index + *T0_min_frac ) / T0_res;
     838             : 
     839       29059 :     *T0_frac = index + *T0_min_frac - ( *T0 - *T0_min ) * T0_res;
     840             : 
     841       29059 :     return;
     842             : }

Generated by: LCOV version 1.14