LCOV - code coverage report
Current view: top level - lib_enc - voiced_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 33 468 7.1 %
Date: 2025-05-23 08:37:30 Functions: 1 9 11.1 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #include <math.h>
      40             : #include "prot.h"
      41             : #include "cnst.h"
      42             : #include "rom_com.h"
      43             : #include "wmc_auto.h"
      44             : 
      45             : /*-------------------------------------------------------------------*
      46             :  * Local function prototypes
      47             :  *--------------------------------------------------------------------*/
      48             : 
      49             : static void SynthesisFilter( float *output, float *input, float *coef, float *memory, int16_t order, int16_t length );
      50             : 
      51             : 
      52             : /*-------------------------------------------------------------------*
      53             :  * DTFS_alignment_extract()
      54             :  *
      55             :  * Alignment for the best match between the reference DTFS and the test DTFS.
      56             :  *-------------------------------------------------------------------*/
      57             : 
      58           0 : static float DTFS_alignment_extract(
      59             :     DTFS_STRUCTURE refX1_DTFS, /* i  : X1 the reference DTFS to keep fixed          */
      60             :     DTFS_STRUCTURE X2_DTFS,    /* i  : X2 the test DTFS to shift to find best match */
      61             :     float Eshift,              /* i  : Expected shift - coarse value                */
      62             :     const float *LPC2          /* i  : LPC to filter to find correlation in spch    */
      63             : )
      64             : {
      65             :     /* Eshift is w.r.t  X2 */
      66             :     int16_t k;
      67             :     float maxcorr, corr, Adiff, diff, tmp, tmp1, fshift, n;
      68           0 :     float pwf = 0.7f, tmplpc[M + 1];
      69             :     DTFS_STRUCTURE X1_DTFS;
      70             : 
      71           0 :     X1_DTFS = refX1_DTFS; /* copy into local copy */
      72             : 
      73           0 :     DTFS_adjustLag( &X1_DTFS, X2_DTFS.lag );
      74             : 
      75           0 :     DTFS_poleFilter( &X1_DTFS, LPC2, M + 1 );
      76           0 :     DTFS_poleFilter( &X2_DTFS, LPC2, M + 1 );
      77             : 
      78           0 :     for ( k = 0, tmp = 1.0; k < M + 1; k++ )
      79             :     {
      80           0 :         tmplpc[k] = LPC2[k] * ( tmp *= pwf );
      81             :     }
      82           0 :     DTFS_zeroFilter( &X1_DTFS, tmplpc, M + 1 );
      83           0 :     DTFS_zeroFilter( &X2_DTFS, tmplpc, M + 1 );
      84             : 
      85           0 :     maxcorr = (float) -HUGE_VAL;
      86           0 :     fshift = Eshift;
      87           0 :     Adiff = max( 4.0f, refX1_DTFS.lag / 8 );
      88           0 :     diff = 1.0f; /* Non-Fractional alignment */
      89             : 
      90           0 :     for ( n = Eshift - Adiff; n <= Eshift + Adiff; n += diff )
      91             :     {
      92           0 :         corr = tmp = 0.0f;
      93             :         /* bit-exact optimization - PI2/X2_DTFS.lag should be counted as a single divide outside WI functions and passed in as input */
      94           0 :         tmp1 = (float) ( PI2 * n / X2_DTFS.lag );
      95             : 
      96           0 :         for ( k = 0; k <= min( X2_DTFS.lag >> 1, X2_DTFS.nH_4kHz ); k++, tmp += tmp1 )
      97             : 
      98             :         {
      99           0 :             corr += (float) ( ( X1_DTFS.a[k] * X2_DTFS.a[k] + X1_DTFS.b[k] * X2_DTFS.b[k] ) * cos( tmp ) );
     100           0 :             corr += (float) ( ( X1_DTFS.b[k] * X2_DTFS.a[k] - X1_DTFS.a[k] * X2_DTFS.b[k] ) * sin( tmp ) );
     101             :         }
     102             : 
     103           0 :         if ( corr * ( 1.0f - 0.01f * fabs( n - Eshift ) ) > maxcorr )
     104             :         {
     105           0 :             fshift = n;
     106           0 :             maxcorr = corr;
     107             :         }
     108             :     }
     109             : 
     110           0 :     return fshift; /* o  : shift value to shift X2 by   */
     111             : }
     112             : 
     113             : 
     114             : /*-------------------------------------------------------------------*
     115             :  * DTFS_getEngy_band()
     116             :  *
     117             :  *  Get DTFS energy in the specified range from lband to hband.
     118             :  *-------------------------------------------------------------------*/
     119             : 
     120           0 : static float DTFS_getEngy_band(
     121             :     DTFS_STRUCTURE X, /* i  : DTFS to compute energy of    */
     122             :     float lband,      /* i  : low end of band of interest  */
     123             :     float hband       /* i  : high end of band of interest */
     124             : )
     125             : {
     126             :     int16_t k;
     127             :     float en, freq, fdiff;
     128             : 
     129           0 :     fdiff = (float) INT_FS_12k8 / X.lag;
     130             : 
     131           0 :     en = 0.0f;
     132           0 :     for ( freq = fdiff, k = 1; k <= min( ( X.lag - 1 ) >> 1, X.nH_4kHz ); k++, freq += fdiff )
     133             :     {
     134           0 :         if ( freq < hband && freq >= lband )
     135             :         {
     136           0 :             en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     137             :         }
     138             :     }
     139           0 :     en /= 2.0;
     140           0 :     if ( lband == 0.0 )
     141             :     {
     142           0 :         en += X.a[0] * X.a[0];
     143             :     }
     144           0 :     if ( ( X.lag % 2 == 0 ) && ( hband == X.upper_cut_off_freq ) )
     145             :     {
     146           0 :         en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     147             :     }
     148           0 :     return en;
     149             : }
     150             : 
     151             : 
     152             : /*-------------------------------------------------------------------*
     153             :  * DTFS_freq_corr()
     154             :  *
     155             :  * Calculate correlation between two DTFS.
     156             :  *-------------------------------------------------------------------*/
     157             : 
     158             : /*! r: correlation */
     159           0 : static double DTFS_freq_corr(
     160             :     DTFS_STRUCTURE X1_DTFS, /* i  : X1 DTFS      */
     161             :     DTFS_STRUCTURE X2_DTFS, /* i  : X2 DTFS      */
     162             :     float lband,            /* i  : low cutoff   */
     163             :     float hband             /* i  : high cutoff  */
     164             : )
     165             : {
     166             :     int16_t k;
     167             :     double corr, fdiff, freq;
     168             :     double tmp;
     169             : 
     170           0 :     if ( X1_DTFS.lag < X2_DTFS.lag )
     171             :     {
     172           0 :         DTFS_zeroPadd( X2_DTFS.lag, &X1_DTFS );
     173             :     }
     174             : 
     175           0 :     corr = freq = 0.0;
     176           0 :     fdiff = (float) INT_FS_12k8 / X2_DTFS.lag;
     177           0 :     for ( k = 0; k <= min( X2_DTFS.lag >> 1, X2_DTFS.nH_4kHz ); k++, freq += fdiff )
     178             :     {
     179           0 :         if ( freq < hband && freq >= lband )
     180             :         {
     181           0 :             corr += ( X1_DTFS.a[k] * X2_DTFS.a[k] + X1_DTFS.b[k] * X2_DTFS.b[k] );
     182             :         }
     183             :     }
     184             : 
     185           0 :     tmp = DTFS_getEngy_band( X1_DTFS, lband, hband ) * DTFS_getEngy_band( X2_DTFS, lband, hband );
     186             : 
     187           0 :     if ( tmp == 0.0 )
     188             :     {
     189           0 :         tmp = 0.001;
     190             :     }
     191             : 
     192           0 :     return corr / sqrt( tmp );
     193             : }
     194             : 
     195             : /*-------------------------------------------------------------------*
     196             :  * DTFS_peaktoaverage()
     197             :  *
     198             :  * Estimate peak to average ratio in the DTFS
     199             :  *-------------------------------------------------------------------*/
     200             : 
     201           0 : static void DTFS_peaktoaverage(
     202             :     DTFS_STRUCTURE X, /* i  : DTFS                  */
     203             :     float *pos,       /* o  : positive peak to ave */
     204             :     float *neg        /* o  : negative peak to ave */
     205             : )
     206             : {
     207           0 :     float tmp, time[PIT_MAX], sum = 0.0, maxPosEn = 0.0, maxNegEn = 0.0;
     208             :     int16_t i;
     209             : 
     210           0 :     DTFS_fs_inv( &X, time, X.lag, 0.0 );
     211             : 
     212           0 :     for ( i = 0; i < X.lag; i++ )
     213             :     {
     214           0 :         tmp = SQR( time[i] );
     215           0 :         if ( time[i] >= 0 )
     216             :         {
     217           0 :             if ( tmp > maxPosEn )
     218             :             {
     219           0 :                 maxPosEn = tmp;
     220             :             }
     221             :         }
     222             :         else
     223             :         {
     224           0 :             if ( tmp > maxNegEn )
     225             :             {
     226           0 :                 maxNegEn = tmp;
     227             :             }
     228             :         }
     229           0 :         sum += tmp;
     230             :     }
     231             : 
     232           0 :     if ( sum == 0.0 )
     233             :     {
     234           0 :         *pos = 0.0f;
     235           0 :         *neg = 0.0f;
     236             :     }
     237             :     else
     238             :     {
     239           0 :         if ( maxPosEn == 0.0 )
     240             :         {
     241           0 :             *pos = 0.0;
     242             :         }
     243             :         else
     244             :         {
     245           0 :             *pos = (float) sqrt( maxPosEn * X.lag / sum );
     246             :         }
     247           0 :         if ( maxPosEn == 0.0 )
     248             :         {
     249           0 :             *neg = 0.0;
     250             :         }
     251             :         else
     252             :         {
     253           0 :             *neg = (float) sqrt( maxNegEn * X.lag / sum );
     254             :         }
     255             :     }
     256             : 
     257           0 :     return;
     258             : }
     259             : 
     260             : 
     261             : /*-------------------------------------------------------------------*
     262             :  * ppp_extract_pitch_period()
     263             :  *
     264             :  *  Pitch period extraction
     265             :  *-------------------------------------------------------------------*/
     266             : 
     267           0 : static int16_t ppp_extract_pitch_period(
     268             :     const float *in,      /* i  : input residual     */
     269             :     float *out,           /* o  : output residual    */
     270             :     const int16_t l,      /* i  : lag                */
     271             :     int16_t *out_of_bound /* o  : out of bound flag  */
     272             : )
     273             : {
     274             :     int16_t i, j, k;
     275           0 :     int16_t spike = 0, range;
     276           0 :     float max1 = 0.0;
     277           0 :     const float *ptr = in + L_FRAME - l;
     278           0 :     float en1 = 0.0, en2 = 0.0, tmp;
     279           0 :     int16_t spike_near_edge = 0;
     280             :     float pos_max, neg_max;
     281           0 :     int16_t spike_pos = 0, spike_neg = 0;
     282             :     float x;
     283             : 
     284           0 :     pos_max = (float) -HUGE_VAL;
     285           0 :     neg_max = 0.0;
     286           0 :     *out_of_bound = 0;
     287             : 
     288           0 :     for ( i = 0; i < l; i++ )
     289             :     {
     290           0 :         if ( ( x = (float) fabs( ptr[i] ) ) > max1 )
     291             :         {
     292           0 :             max1 = x;
     293           0 :             spike = i;
     294             :         }
     295           0 :         en1 += ptr[i] * ptr[i];
     296             :     }
     297             : 
     298           0 :     if ( ptr[spike] > 0 )
     299             :     {
     300           0 :         spike_pos = spike;
     301             : 
     302             :         /* search for neg spike around the pos spike */
     303           0 :         for ( j = spike - 10; j < spike + 10; j++ )
     304             :         {
     305           0 :             k = ( j + l ) % l;
     306           0 :             if ( ptr[k] < neg_max )
     307             :             {
     308           0 :                 neg_max = ptr[k];
     309           0 :                 spike_neg = k;
     310             :             }
     311             :         }
     312             :     }
     313           0 :     else if ( ptr[spike] < 0 )
     314             :     {
     315           0 :         spike_neg = spike;
     316             : 
     317             :         /* search for pos spike around the neg spike */
     318           0 :         for ( j = spike - 10; j < spike + 10; j++ )
     319             :         {
     320           0 :             k = ( j + l ) % l;
     321           0 :             if ( ptr[k] > pos_max )
     322             :             {
     323           0 :                 pos_max = ptr[k];
     324           0 :                 spike_pos = k;
     325             :             }
     326             :         }
     327             :     }
     328           0 :     if ( ( ( l - 1 - max( spike_pos, spike_neg ) ) <= 2 ) || ( min( spike_pos, spike_neg ) <= 2 ) )
     329             :     {
     330           0 :         *out_of_bound = 1;
     331           0 :         return spike_near_edge;
     332             :     }
     333           0 :     range = (int16_t) anint( max( CUTFREE_REL_RANGE * l, CUTFREE_ABS_RANGE ) );
     334           0 :     if ( ( spike - range < 0 ) || ( spike + range >= l ) )
     335             :     {
     336             :         /* need to grab from one lag before
     337             :         ensure that there is no array bound read */
     338           0 :         if ( L_FRAME - l - l < 0 )
     339             :         {
     340           0 :             *out_of_bound = 1;
     341           0 :             return spike_near_edge;
     342             :         }
     343           0 :         spike_near_edge = 1;
     344             :     }
     345           0 :     if ( spike - range < 0 )
     346             :     {
     347           0 :         for ( i = 0; i < l + ( spike - range ); i++ )
     348             :         {
     349           0 :             out[i] = ptr[i];
     350             :         }
     351             : 
     352             :         /* Grab Remaining From One Lag Before */
     353           0 :         ptr -= l;
     354           0 :         for ( ; i < l; i++ )
     355             :         {
     356           0 :             out[i] = ptr[i];
     357             :         }
     358             :     }
     359           0 :     else if ( spike + range >= l )
     360             :     {
     361           0 :         for ( i = 0; i < spike - range; i++ )
     362             :         {
     363           0 :             out[i] = ptr[i];
     364             :         }
     365             :         /* Grab Remaining From One Lag Before */
     366           0 :         if ( ptr - l + i >= in )
     367             :         {
     368           0 :             for ( ptr -= l; i < l; i++ )
     369             :             {
     370           0 :                 out[i] = ptr[i];
     371             :             }
     372             :         }
     373             :         else
     374             :         {
     375           0 :             for ( ; i < l; i++ )
     376             :             {
     377           0 :                 out[i] = ptr[i];
     378             :             }
     379             :         }
     380             :     }
     381             :     else
     382             :     {
     383           0 :         for ( i = 0; i < l; i++ )
     384             :         {
     385           0 :             out[i] = ptr[i];
     386             :         }
     387             :     }
     388             : 
     389             :     /* Energy adjustment added to eliminate artifacts at the end of voicing */
     390           0 :     for ( i = 0; i < l; i++ )
     391             :     {
     392           0 :         en2 += out[i] * out[i];
     393             :     }
     394             : 
     395           0 :     if ( en1 < en2 )
     396             :     {
     397           0 :         tmp = (float) sqrt( en1 / en2 );
     398           0 :         for ( i = 0; i < l; i++ )
     399             :         {
     400           0 :             out[i] *= tmp;
     401             :         }
     402             :     }
     403             : 
     404           0 :     return spike_near_edge;
     405             : }
     406             : 
     407             : 
     408             : /*-------------------------------------------------------------------*
     409             :  * DTFS_getEngy_band_wb()
     410             :  *
     411             :  *  Get DTFS energy in the specified range from lband to hband.
     412             :  *  This function is different to "DTFS_getEngy_band" as this can calculate
     413             :  *  lband, hband \in [1,6400] where "DTFS_getEngy_band" only upperlimited to
     414             :  *  4Khz. Possibility: modify ""DTFS_getEngy_band"" and get rid of this
     415             :  *  function.
     416             :  *-------------------------------------------------------------------*/
     417             : 
     418           0 : static float DTFS_getEngy_band_wb(
     419             :     DTFS_STRUCTURE X, /* i  : DTFS to compute energy of    */
     420             :     float lband,      /* i  : low end of band of interest  */
     421             :     float hband       /* i  : high end of band of interest */
     422             : )
     423             : {
     424             :     int16_t k;
     425             :     float en, freq, fdiff;
     426             : 
     427           0 :     fdiff = (float) INT_FS_12k8 / X.lag;
     428             : 
     429           0 :     en = 0.0f;
     430           0 :     for ( freq = fdiff, k = 1; k <= ( ( X.lag - 1 ) >> 1 ); k++, freq += fdiff )
     431             :     {
     432           0 :         if ( freq < hband && freq >= lband )
     433             :         {
     434           0 :             en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     435             :         }
     436             :     }
     437           0 :     en /= 2.0;
     438           0 :     if ( lband == 0.0 )
     439             :     {
     440           0 :         en += X.a[0] * X.a[0];
     441             :     }
     442           0 :     if ( ( X.lag % 2 == 0 ) && ( hband == X.upper_cut_off_freq ) )
     443             :     {
     444           0 :         en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     445             :     }
     446           0 :     return en;
     447             : }
     448             : 
     449             : 
     450             : /*-------------------------------------------------------------------*
     451             :  * ppp_voiced_encoder()
     452             :  *
     453             :  *
     454             :  *--------------------------------------------------------------------*/
     455             : 
     456           0 : ivas_error ppp_voiced_encoder(
     457             :     BSTR_ENC_HANDLE hBstr,             /* i/o: encoder bitstream handle                */
     458             :     SC_VBR_ENC_HANDLE hSC_VBR,         /* i/o: SC-VBR state structure                  */
     459             :     const int16_t bwidth,              /* i  : audio bandwidth                         */
     460             :     const int16_t last_coder_type_raw, /* i  : raw last_coder_type                     */
     461             :     const float old_pitch_buf[],       /* i  : buffer of old subframe pitch values     */
     462             :     float *in,                         /* i  : residual signal                         */
     463             :     float *out,                        /* o  : Quantized residual signal               */
     464             :     const int16_t delay,               /* i  : open loop pitch                         */
     465             :     float *lpc1,                       /* i  : prev frame de-emphasized LPC            */
     466             :     float *lpc2,                       /* i  : current frame de-emphasized LPC         */
     467             :     float *exc,                        /* i  : previous frame quantized excitation     */
     468             :     float *pitch                       /* o  : floating pitch values for each subframe */
     469             : )
     470             : {
     471             :     int16_t i;
     472           0 :     int16_t spike_near_edge = 0;
     473             :     int16_t flag;
     474           0 :     int16_t delta_lag_E = 0, PPP_MODE_E, Q_delta_lag = 0;
     475           0 :     int16_t out_of_bound = 0;
     476           0 :     float tmp, tmptmp, tmptmp1, res_enratio = 0, sp_enratio = 0;
     477             :     double tmp2;
     478             :     int16_t pl, l;
     479             :     float interp_delay[3], temp_pl, temp_l;
     480           0 :     int16_t upper_cut_off_freq_of_interest = 0, upper_cut_off_freq = 0;
     481             :     float pos_nq, neg_nq, pos_q, neg_q;
     482             :     float impzi[160];
     483             :     float impzo[160];
     484             :     float mem[10];
     485           0 :     float energy_impz = 0.0f;
     486             :     float pos_nq0, neg_nq0, tmpres;
     487             :     float sp_hb_enratio;
     488             :     float low_band_en;
     489             : 
     490             :     DTFS_STRUCTURE *CURRP_NQ;
     491             :     DTFS_STRUCTURE *TMPDTFS;
     492             :     DTFS_STRUCTURE *TMPDTFS2;
     493             :     DTFS_STRUCTURE *TMPDTFS3;
     494             :     DTFS_STRUCTURE *CURRP_Q_E;
     495             :     DTFS_STRUCTURE *dtfs_temp;
     496             : 
     497             :     ivas_error error;
     498             : 
     499           0 :     error = IVAS_ERR_OK;
     500             : 
     501           0 :     if ( ( error = DTFS_new( &CURRP_NQ ) ) != IVAS_ERR_OK )
     502             :     {
     503           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     504             :     }
     505           0 :     if ( ( error = DTFS_new( &TMPDTFS ) ) != IVAS_ERR_OK )
     506             :     {
     507           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     508             :     }
     509           0 :     if ( ( error = DTFS_new( &TMPDTFS2 ) ) != IVAS_ERR_OK )
     510             :     {
     511           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     512             :     }
     513           0 :     if ( ( error = DTFS_new( &TMPDTFS3 ) ) != IVAS_ERR_OK )
     514             :     {
     515           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     516             :     }
     517           0 :     if ( ( error = DTFS_new( &CURRP_Q_E ) ) != IVAS_ERR_OK )
     518             :     {
     519           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     520             :     }
     521           0 :     if ( ( error = DTFS_new( &dtfs_temp ) ) != IVAS_ERR_OK )
     522             :     {
     523           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     524             :     }
     525             : 
     526           0 :     if ( bwidth == WB )
     527             :     {
     528           0 :         upper_cut_off_freq_of_interest = 4000;
     529           0 :         upper_cut_off_freq = 6400;
     530             :     }
     531           0 :     else if ( bwidth == NB )
     532             :     {
     533           0 :         upper_cut_off_freq_of_interest = 3300;
     534           0 :         upper_cut_off_freq = 4000;
     535             :     }
     536             : 
     537             :     /* Initialization */
     538           0 :     if ( hSC_VBR->firstTime_voicedenc )
     539             :     {
     540           0 :         hSC_VBR->firstTime_voicedenc = 0;
     541           0 :         hSC_VBR->dtfs_enc_lag = 0;
     542           0 :         hSC_VBR->dtfs_enc_nH = 0;
     543           0 :         hSC_VBR->dtfs_enc_nH_4kHz = 0;
     544           0 :         hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest = 3300.0;
     545           0 :         hSC_VBR->dtfs_enc_upper_cut_off_freq = 4000.0;
     546             : 
     547           0 :         set_f( hSC_VBR->dtfs_enc_a, 0, MAXLAG_WI );
     548           0 :         set_f( hSC_VBR->dtfs_enc_b, 0, MAXLAG_WI );
     549             :     }
     550             : 
     551             :     /* Figure out the PPP_MODE */
     552           0 :     if ( hSC_VBR->last_ppp_mode == 1 && !hSC_VBR->mode_QQF )
     553             :     {
     554           0 :         hSC_VBR->bump_up = 1;
     555             : 
     556           0 :         free( CURRP_NQ );
     557           0 :         free( TMPDTFS );
     558           0 :         free( TMPDTFS2 );
     559           0 :         free( TMPDTFS3 );
     560           0 :         free( CURRP_Q_E );
     561           0 :         free( dtfs_temp );
     562             : 
     563           0 :         return error;
     564             :     }
     565             : 
     566             :     /* Use the aggresive bumpups if there are two consecutive Q frames */
     567             :     /* Aggresive bump upsare only used in the second Q frame */
     568           0 :     if ( hSC_VBR->last_ppp_mode == 1 )
     569             :     {
     570           0 :         hSC_VBR->rate_control = 0;
     571             :     }
     572             : 
     573           0 :     PPP_MODE_E = 'Q';
     574           0 :     pl = min( MAX_LAG_PIT, (int16_t) rint_new( old_pitch_buf[( 2 * NB_SUBFR ) - 1] ) );
     575           0 :     l = min( MAX_LAG_PIT, (int16_t) rint_new( delay ) );
     576             : 
     577             :     /* Bump up if the lag is out_fx of range */
     578           0 :     if ( ( l - pl ) > 13 || ( l - pl ) < -11 || ( l < 19 ) || ( pl < 19 ) )
     579             :     {
     580           0 :         hSC_VBR->bump_up = 1;
     581             : 
     582           0 :         free( CURRP_NQ );
     583           0 :         free( TMPDTFS );
     584           0 :         free( TMPDTFS2 );
     585           0 :         free( TMPDTFS3 );
     586           0 :         free( CURRP_Q_E );
     587           0 :         free( dtfs_temp );
     588             : 
     589           0 :         return error;
     590             :     }
     591             : 
     592           0 :     if ( hSC_VBR->last_ppp_mode != 1 )
     593             :     {
     594             :         /* Obtain DTFS of last pl values of past excitation */
     595           0 :         DTFS_to_fs( exc - pl, pl, dtfs_temp, bwidth == WB ? (int16_t) 16000 : (int16_t) 8000, 0 );
     596             :     }
     597             : 
     598           0 :     if ( last_coder_type_raw == UNVOICED )
     599             :     {
     600           0 :         pl = l; /* if prev frame was sil/uv */
     601             :     }
     602             : 
     603             :     /* Use the out array as a temp storage for currp */
     604           0 :     spike_near_edge = ppp_extract_pitch_period( in, out, l, &out_of_bound );
     605             : 
     606           0 :     if ( out_of_bound == 1 )
     607             :     {
     608           0 :         hSC_VBR->bump_up = 1;
     609             : 
     610           0 :         free( CURRP_NQ );
     611           0 :         free( TMPDTFS );
     612           0 :         free( TMPDTFS2 );
     613           0 :         free( TMPDTFS3 );
     614           0 :         free( CURRP_Q_E );
     615           0 :         free( dtfs_temp );
     616             : 
     617           0 :         return error;
     618             :     }
     619             : 
     620             :     /* Get DTFS of current prototype */
     621           0 :     DTFS_to_fs( out, l, CURRP_NQ, bwidth == WB ? (int16_t) 16000 : (int16_t) 8000, 0 );
     622             : 
     623             :     /* Ensure the extracted prototype is time-synchronous to the
     624             :      * last l samples of the frame. This proves to eliminate
     625             :      * some of the PPP-CELP transition problems.
     626             :      * Convert last samples into DTFS  */
     627           0 :     if ( spike_near_edge != 0 )
     628             :     {
     629           0 :         DTFS_to_fs( in + L_FRAME - l, l, TMPDTFS, bwidth == WB ? (int16_t) 16000 : (int16_t) 8000, 0 );
     630             : 
     631           0 :         tmp = DTFS_alignment_extract( *TMPDTFS, *CURRP_NQ, 0.0, lpc2 ); /* figure out how much to shift currp_nq to align with TMP */
     632             : 
     633           0 :         DTFS_phaseShift( CURRP_NQ, (float) ( PI2 * tmp / l ) );
     634             :     }
     635             : 
     636           0 :     temp_pl = (float) pl;
     637           0 :     temp_l = (float) l;
     638             : 
     639           0 :     for ( i = 0; i < NB_SUBFR; i++ )
     640             :     {
     641             :         /* do the linear pitch interp to drive the nb_post_filt */
     642           0 :         Interpol_delay( interp_delay, &( temp_pl ), &( temp_l ), i, frac_4sf );
     643           0 :         pitch[i] = interp_delay[0];
     644             :     }
     645             : 
     646             : 
     647             :     /* Restoring PPP memories when the last frame is non-PPP */
     648           0 :     if ( hSC_VBR->last_ppp_mode != 1 )
     649             :     {
     650           0 :         hSC_VBR->ph_offset_E = 0.0;
     651           0 :         hSC_VBR->prev_cw_en = DTFS_getEngy( *dtfs_temp );
     652             : 
     653             :         /* Copy over dtfs_temp into TMPDTFS */
     654           0 :         DTFS_copy( TMPDTFS, *dtfs_temp );
     655             : 
     656           0 :         DTFS_car2pol( TMPDTFS );
     657             : 
     658           0 :         hSC_VBR->lastLgainE = (float) log10( TMPDTFS->lag * DTFS_setEngyHarm( 92.0, 1104.5, 0.0, 1104.5, 1.0, TMPDTFS ) );
     659           0 :         hSC_VBR->lastHgainE = (float) log10( TMPDTFS->lag * DTFS_setEngyHarm( 1104.5, upper_cut_off_freq_of_interest, 1104.5, upper_cut_off_freq, 1.0, TMPDTFS ) );
     660             : 
     661           0 :         DTFS_to_erb( *TMPDTFS, hSC_VBR->lasterbE );
     662             :     }
     663             :     else
     664             :     {
     665             :         /* Copy DTFS related parameters from 'hSC_VBR' to 'dtfs_temp' structure */
     666           0 :         dtfs_temp->lag = hSC_VBR->dtfs_enc_lag;
     667           0 :         dtfs_temp->nH = hSC_VBR->dtfs_enc_nH;
     668           0 :         dtfs_temp->nH_4kHz = hSC_VBR->dtfs_enc_nH_4kHz;
     669           0 :         dtfs_temp->upper_cut_off_freq_of_interest = hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest;
     670           0 :         dtfs_temp->upper_cut_off_freq = hSC_VBR->dtfs_enc_upper_cut_off_freq;
     671             : 
     672           0 :         mvr2r( hSC_VBR->dtfs_enc_a, dtfs_temp->a, MAXLAG_WI );
     673           0 :         mvr2r( hSC_VBR->dtfs_enc_b, dtfs_temp->b, MAXLAG_WI );
     674             :     }
     675             : 
     676             :     /*-----Open-loop Bump-Up-------- */
     677             : 
     678             :     /* Energy ratio calculation in residual and speech domain */
     679             :     /* Also, compute correlation between the previous and the */
     680             :     /* current prototype */
     681           0 :     res_enratio = DTFS_getEngy( *CURRP_NQ ) / DTFS_getEngy( *dtfs_temp );
     682             : 
     683             :     /* Copy over CURRP_NQ into TMPDTFS */
     684           0 :     DTFS_copy( TMPDTFS, *CURRP_NQ );
     685             : 
     686             :     /* Copy over dtfs_temp into TMPDTFS2 */
     687           0 :     DTFS_copy( TMPDTFS2, *dtfs_temp );
     688             : 
     689           0 :     tmptmp = DTFS_alignment_full( *TMPDTFS2, *TMPDTFS, TMPDTFS->lag * 2 ); /* align of prev_cw wrt curr_cw, new method */
     690             : 
     691           0 :     tmptmp1 = TMPDTFS->lag - tmptmp;
     692           0 :     tmp = tmptmp1;
     693             : 
     694           0 :     DTFS_phaseShift( TMPDTFS, (float) ( -PI2 * tmp / TMPDTFS->lag ) ); /* fixed bug, phase shift by tmp computed in TMP.lag domain (above) */
     695           0 :     tmpres = (float) ( DTFS_freq_corr( *TMPDTFS, *TMPDTFS2, 100.0f, 3700.0f ) );
     696             : 
     697           0 :     DTFS_poleFilter( TMPDTFS, lpc2, M + 1 );
     698             : 
     699           0 :     DTFS_adjustLag( TMPDTFS2, TMPDTFS->lag ); /* operate in CL domain */
     700             : 
     701           0 :     DTFS_poleFilter( TMPDTFS2, lpc1, M + 1 );
     702             : 
     703           0 :     tmp = (float) ( DTFS_freq_corr( *TMPDTFS, *TMPDTFS2, 100.0f, 3700.0f ) );
     704             : 
     705           0 :     if ( DTFS_getEngy( *TMPDTFS2 ) > 0 )
     706             :     {
     707           0 :         sp_enratio = DTFS_getEngy( *TMPDTFS ) / DTFS_getEngy( *TMPDTFS2 );
     708             :     }
     709             :     else
     710             :     {
     711           0 :         sp_enratio = 0.0f;
     712             :     }
     713             : 
     714           0 :     if ( PPP_MODE_E == 'Q' )
     715             :     {
     716             :         /* Bump up if the lag is out of range */
     717           0 :         if ( ( ( l - pl ) > 13 ) || ( ( l - pl ) < -11 ) )
     718             :         {
     719           0 :             PPP_MODE_E = 'B';
     720             :         }
     721             :         else
     722             :         {
     723           0 :             delta_lag_E = (int16_t) ( l - pl );
     724             :         }
     725             : 
     726             :         /* Bump up if big change between the previous and the current CWs */
     727           0 :         if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     728             :         {
     729           0 :             if ( res_enratio > 5.0 && tmp < 0.65 )
     730             :             {
     731           0 :                 PPP_MODE_E = 'B';
     732             :             }
     733             :         }
     734             :         else
     735             :         {
     736           0 :             if ( res_enratio > 3.0 && tmp < 1.2 )
     737             :             {
     738           0 :                 PPP_MODE_E = 'B';
     739             :             }
     740             :         }
     741             :     }
     742             : 
     743             :     /* Rapid rampdown frame where time resolution is important */
     744             :     /* Not a suitable PPP frame -> Bump to CELP */
     745           0 :     if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     746             :     {
     747           0 :         if ( res_enratio < 0.025 )
     748             :         {
     749           0 :             hSC_VBR->bump_up = 1;
     750             : 
     751           0 :             free( CURRP_NQ );
     752           0 :             free( TMPDTFS );
     753           0 :             free( TMPDTFS2 );
     754           0 :             free( TMPDTFS3 );
     755           0 :             free( CURRP_Q_E );
     756           0 :             free( dtfs_temp );
     757             : 
     758           0 :             return error;
     759             :         }
     760             :     }
     761             :     else
     762             :     {
     763           0 :         if ( res_enratio < 0.092f )
     764             :         {
     765           0 :             hSC_VBR->bump_up = 1;
     766             :         }
     767             :     }
     768             : 
     769           0 :     if ( min( res_enratio, sp_enratio ) < 0.075f && tmp < -0.5f )
     770             :     {
     771           0 :         hSC_VBR->bump_up = 1;
     772             :     }
     773             : 
     774             :     /* Rapid rampup frame where time resolution is important */
     775             :     /* Not a suitable PPP frame -> Bump to CELP */
     776           0 :     if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     777             :     {
     778           0 :         if ( res_enratio > 14.5 )
     779             :         {
     780           0 :             hSC_VBR->bump_up = 1;
     781             : 
     782           0 :             free( CURRP_NQ );
     783           0 :             free( TMPDTFS );
     784           0 :             free( TMPDTFS2 );
     785           0 :             free( TMPDTFS3 );
     786           0 :             free( CURRP_Q_E );
     787           0 :             free( dtfs_temp );
     788             : 
     789           0 :             return error;
     790             :         }
     791             :     }
     792             :     else
     793             :     {
     794           0 :         if ( res_enratio > 7.0 )
     795             :         {
     796           0 :             hSC_VBR->bump_up = 1;
     797             :         }
     798             :     }
     799             : 
     800           0 :     if ( hSC_VBR->bump_up == 1 )
     801             :     {
     802           0 :         free( CURRP_NQ );
     803           0 :         free( TMPDTFS );
     804           0 :         free( TMPDTFS2 );
     805           0 :         free( TMPDTFS3 );
     806           0 :         free( CURRP_Q_E );
     807           0 :         free( dtfs_temp );
     808             : 
     809           0 :         return error;
     810             :     }
     811             : 
     812             :     /* Bump up when the previous frame is an unvoiced or a silent frame */
     813           0 :     if ( last_coder_type_raw == UNVOICED )
     814             :     {
     815           0 :         hSC_VBR->bump_up = 1;
     816             : 
     817           0 :         free( CURRP_NQ );
     818           0 :         free( TMPDTFS );
     819           0 :         free( TMPDTFS2 );
     820           0 :         free( TMPDTFS3 );
     821           0 :         free( CURRP_Q_E );
     822           0 :         free( dtfs_temp );
     823             : 
     824           0 :         return error;
     825             :     }
     826             :     /* -----End Open-loop Bump-Up */
     827             : 
     828             :     /* PPP-WI Quantization */
     829           0 :     if ( PPP_MODE_E == 'Q' )
     830             :     {
     831           0 :         flag = 1;
     832           0 :         if ( PPP_MODE_E == 'Q' )
     833             :         {
     834           0 :             if ( ( error = ppp_quarter_encoder( &flag, hBstr, CURRP_Q_E, TMPDTFS, dtfs_temp->lag, *CURRP_NQ, lpc2, &( hSC_VBR->lastLgainE ), &( hSC_VBR->lastHgainE ), &( hSC_VBR->lasterbE[0] ), *dtfs_temp ) ) != IVAS_ERR_OK )
     835             :             {
     836           0 :                 free( CURRP_NQ );
     837           0 :                 free( TMPDTFS );
     838           0 :                 free( TMPDTFS2 );
     839           0 :                 free( TMPDTFS3 );
     840           0 :                 free( CURRP_Q_E );
     841           0 :                 free( dtfs_temp );
     842             : 
     843           0 :                 return error;
     844             :             }
     845             :         }
     846             : 
     847           0 :         if ( flag )
     848             :         {
     849             :             /* TMPDTFS : Target prototype: Amp Quantized + Phase Unquantized         */
     850             :             /* TMPDTFS2: Quantized prototype: Amp Quantized + Phase Quantized        */
     851             :             /* TMPDTFS3: Delta prototype: Diff betw. target and quant. in speech dom */
     852             : 
     853             :             /* ----- Closed-loop Bump-Up ---------- */
     854           0 :             DTFS_peaktoaverage( *TMPDTFS, &pos_nq, &neg_nq );
     855           0 :             DTFS_peaktoaverage( *CURRP_Q_E, &pos_q, &neg_q );
     856             : 
     857             :             /* Before we perform the peak-to-average ratio comparison, we have to */
     858             :             /* ensure that the energy is not decaying and also the pitch pulse */
     859             :             /* is clearly defined */
     860             : 
     861             :             /*  Usually triggers in the slow ramp down frames. Does not fall under the test condition (res_enratio < 0.025) as
     862             :                 both frames have little energy and the ratio is not very small. Not suitable for PPP */
     863             : 
     864           0 :             if ( CURRP_Q_E->upper_cut_off_freq > 4000 )
     865             :             {
     866             :                 /* Use this bumup only for WB signals */
     867           0 :                 if ( DTFS_getEngy_band_wb( *CURRP_Q_E, 0.0, 2000.0 ) > 0 )
     868             :                 {
     869           0 :                     sp_hb_enratio = DTFS_getEngy_band_wb( *CURRP_Q_E, 2000.0, 6400.0 ) / DTFS_getEngy_band_wb( *CURRP_Q_E, 0.0, 2000.0 );
     870             :                 }
     871             :                 else
     872             :                 {
     873           0 :                     sp_hb_enratio = 0;
     874             :                 }
     875             : 
     876           0 :                 low_band_en = (float) DTFS_getEngy_band_wb( *CURRP_Q_E, 0.0, 2000.0 );
     877             : 
     878           0 :                 if ( low_band_en < 25.0f && sp_hb_enratio < 1.6f )
     879             :                 {
     880           0 :                     PPP_MODE_E = 'B';
     881             :                 }
     882             :             }
     883             : 
     884           0 :             if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     885             :             {
     886           0 :                 if ( DTFS_getEngy( *CURRP_NQ ) > 0.8f * hSC_VBR->prev_cw_en && max( pos_nq, neg_nq ) > 3.0f && hSC_VBR->rate_control )
     887             :                 {
     888           0 :                     if ( pos_nq > neg_nq && pos_nq > 2.0f * pos_q )
     889             :                     {
     890           0 :                         PPP_MODE_E = 'B';
     891             :                     }
     892             : 
     893           0 :                     if ( pos_nq < neg_nq && neg_nq > 2.0f * neg_q )
     894             :                     {
     895           0 :                         PPP_MODE_E = 'B';
     896             :                     }
     897             :                 }
     898             :             }
     899             :             else
     900             :             {
     901           0 :                 if ( ( ( ( DTFS_getEngy( *CURRP_NQ ) > ( hSC_VBR->prev_cw_en ) ) && ( max( pos_nq, neg_nq ) > 3.5 ) ) && ( hSC_VBR->rate_control ) ) ||
     902           0 :                      ( ( ( DTFS_getEngy( *CURRP_NQ ) > 0.8 * ( hSC_VBR->prev_cw_en ) ) && ( max( pos_nq, neg_nq ) > 3.0 ) ) && ( !hSC_VBR->rate_control ) ) )
     903             :                 {
     904           0 :                     if ( ( ( pos_nq > neg_nq ) && ( pos_nq > 2.5 * pos_q ) && ( hSC_VBR->rate_control ) ) ||
     905           0 :                          ( ( pos_nq > neg_nq ) && ( pos_nq > 2.0 * pos_q ) && ( !hSC_VBR->rate_control ) ) )
     906             :                     {
     907           0 :                         PPP_MODE_E = 'B';
     908             :                     }
     909             : 
     910           0 :                     if ( ( ( ( pos_nq < neg_nq ) && ( neg_nq > 2.5 * neg_q ) ) && ( hSC_VBR->rate_control ) ) ||
     911           0 :                          ( ( pos_nq < neg_nq ) && ( neg_nq > 2.0 * neg_q ) && ( !hSC_VBR->rate_control ) ) )
     912             :                     {
     913           0 :                         PPP_MODE_E = 'B';
     914             :                     }
     915             :                 }
     916             : 
     917           0 :                 if ( hSC_VBR->rate_control )
     918             :                 {
     919           0 :                     DTFS_peaktoaverage( *CURRP_NQ, &pos_nq0, &neg_nq0 );
     920             : 
     921           0 :                     for ( impzi[0] = 1.0, i = 1; i < 160; i++ )
     922             :                     {
     923           0 :                         impzi[i] = 0.0;
     924             :                     }
     925             : 
     926           0 :                     for ( i = 0; i < 160; i++ )
     927             :                     {
     928           0 :                         impzo[i] = 0.0;
     929             :                     }
     930             : 
     931           0 :                     for ( i = 0; i < 10; i++ )
     932             :                     {
     933           0 :                         mem[i] = 0.0;
     934             :                     }
     935             : 
     936           0 :                     SynthesisFilter( &impzo[0], &impzi[0], lpc2, &mem[0], 10, 160 );
     937             : 
     938           0 :                     for ( i = 0; i < 160; i++ )
     939             :                     {
     940           0 :                         energy_impz += ( impzo[i] * impzo[i] );
     941             :                     }
     942             : 
     943           0 :                     energy_impz = (float) ( 10 * log10( (float) energy_impz ) );
     944             : 
     945           0 :                     if ( ( DTFS_getEngy( *CURRP_Q_E ) > hSC_VBR->prev_cw_en ) && ( max( pos_q, neg_q ) > 3.5 ) && energy_impz > 15.0 && tmpres > 0.7 )
     946             :                     {
     947           0 :                         if ( ( pos_q > neg_q ) && ( ( pos_q > 3.0 * pos_nq0 ) || ( ( pos_q > 1.5 * pos_nq0 ) && ( neg_q < 1.5 * neg_q ) ) ) )
     948             :                         {
     949           0 :                             PPP_MODE_E = 'B';
     950             :                         }
     951             : 
     952           0 :                         if ( ( pos_q <= neg_q ) && ( ( neg_q > 3.0 * neg_nq0 ) || ( ( neg_q > 1.5 * neg_nq0 ) && ( pos_q < 1.5 * pos_q ) ) ) )
     953             :                         {
     954           0 :                             PPP_MODE_E = 'B';
     955             :                         }
     956             :                     }
     957             :                 }
     958             :             }
     959             : 
     960           0 :             DTFS_copy( TMPDTFS2, *CURRP_Q_E );
     961             : 
     962           0 :             DTFS_poleFilter( TMPDTFS, lpc2, M + 1 );
     963           0 :             DTFS_poleFilter( TMPDTFS2, lpc2, M + 1 );
     964             : 
     965           0 :             DTFS_sub( TMPDTFS3, *TMPDTFS, *TMPDTFS2 );
     966             : 
     967             :             /* operate in ADR mode only the rate control is active. This adds some bumpups to improve the speech quality */
     968           0 :             tmp2 = DTFS_getEngy_band( *TMPDTFS, 1500.0, upper_cut_off_freq_of_interest ) / DTFS_getEngy( *TMPDTFS );
     969           0 :             if ( tmp2 == 0.0 )
     970             :             {
     971           0 :                 tmp2 = 0.001;
     972             :             }
     973           0 :             if ( ( tmp2 > 0.05 ) && ( !hSC_VBR->rate_control ) )
     974             :             {
     975           0 :                 tmp2 = DTFS_getEngy_band( *TMPDTFS, 1500.0, upper_cut_off_freq_of_interest ) / DTFS_getEngy_band( *TMPDTFS3, 1500.0, upper_cut_off_freq_of_interest );
     976           0 :                 if ( tmp2 == 0.0 )
     977             :                 {
     978           0 :                     tmp2 = 0.001;
     979             :                 }
     980           0 :                 if ( 10.0 * log10( tmp2 ) < 0.1 )
     981             :                 {
     982           0 :                     if ( res_enratio > 0.8 )
     983             :                     {
     984           0 :                         PPP_MODE_E = 'B';
     985             :                     }
     986             :                 }
     987             :             }
     988             : 
     989             :             /* To increase bump up, raise first threshold, lower second  */
     990           0 :             tmp = (float) ( 10.0 * log10( DTFS_getEngy( *TMPDTFS ) / DTFS_getEngy( *TMPDTFS3 ) ) );
     991             : 
     992           0 :             if ( ( tmp <= 0 ) && ( !hSC_VBR->rate_control ) )
     993             :             {
     994           0 :                 PPP_MODE_E = 'B';
     995             :             }
     996             : 
     997           0 :             if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     998             :             {
     999           0 :                 if ( ( ( tmp < 3.05 && max( res_enratio, sp_enratio ) > 0.8 ) && ( hSC_VBR->rate_control ) ) ||
    1000           0 :                      ( ( tmp < 2.8 && max( res_enratio, sp_enratio ) > 0.65 ) && ( !hSC_VBR->rate_control ) ) )
    1001             :                 {
    1002           0 :                     PPP_MODE_E = 'B';
    1003             :                 }
    1004             :             }
    1005             :             else
    1006             :             {
    1007           0 :                 if ( ( ( tmp < 2.4 && max( res_enratio, sp_enratio ) > 0.94 ) && ( hSC_VBR->rate_control ) ) ||
    1008           0 :                      ( ( tmp < 4.5 && max( res_enratio, sp_enratio ) > 0.5 ) && ( !hSC_VBR->rate_control ) ) )
    1009             :                 {
    1010           0 :                     PPP_MODE_E = 'B';
    1011             :                 }
    1012             :             }
    1013             :             /* -----End closed-loop Bump-Up */
    1014             :         }
    1015             :         else
    1016             :         {
    1017           0 :             PPP_MODE_E = 'B'; /* Amplitude quantization is failing */
    1018             :         }
    1019             :     }
    1020             :     else
    1021             :     {
    1022             :     }
    1023             : 
    1024           0 :     if ( PPP_MODE_E == 'B' )
    1025             :     {
    1026           0 :         hSC_VBR->bump_up = 1;
    1027             : 
    1028           0 :         free( CURRP_NQ );
    1029           0 :         free( TMPDTFS );
    1030           0 :         free( TMPDTFS2 );
    1031           0 :         free( TMPDTFS3 );
    1032           0 :         free( CURRP_Q_E );
    1033           0 :         free( dtfs_temp );
    1034             : 
    1035           0 :         return error;
    1036             :     }
    1037             : 
    1038           0 :     if ( hSC_VBR->Q_to_F )
    1039             :     {
    1040           0 :         hSC_VBR->patterncount += hSC_VBR->pattern_m;
    1041             : 
    1042           0 :         if ( hSC_VBR->patterncount >= 1000 )
    1043             :         {
    1044           0 :             hSC_VBR->patterncount -= 1000;
    1045           0 :             PPP_MODE_E = 'B';
    1046           0 :             hSC_VBR->bump_up = 1;
    1047             : 
    1048           0 :             free( CURRP_NQ );
    1049           0 :             free( TMPDTFS );
    1050           0 :             free( TMPDTFS2 );
    1051           0 :             free( TMPDTFS3 );
    1052           0 :             free( CURRP_Q_E );
    1053           0 :             free( dtfs_temp );
    1054             : 
    1055           0 :             return error;
    1056             :         }
    1057             :     }
    1058             : 
    1059             :     /* packetization of the delta lag in PPP */
    1060           0 :     if ( PPP_MODE_E == 'Q' )
    1061             :     {
    1062           0 :         Q_delta_lag = delta_lag_E + 11; /* to make it positive always */
    1063           0 :         push_indice( hBstr, IND_DELTALAG, Q_delta_lag, 5 );
    1064             :     }
    1065             : 
    1066           0 :     if ( ( error = WIsyn( *dtfs_temp, CURRP_Q_E, lpc2, &( hSC_VBR->ph_offset_E ), out, L_FRAME, 0 ) ) != IVAS_ERR_OK )
    1067             :     {
    1068           0 :         return error;
    1069             :     }
    1070             : 
    1071             : 
    1072           0 :     DTFS_copy( dtfs_temp, *CURRP_Q_E );
    1073           0 :     hSC_VBR->prev_cw_en = DTFS_getEngy( *CURRP_NQ );
    1074             : 
    1075             :     /* Copy DTFS related parameters from 'dtfs_temp' to 'hSC_VBR' structure */
    1076           0 :     hSC_VBR->dtfs_enc_lag = dtfs_temp->lag;
    1077           0 :     hSC_VBR->dtfs_enc_nH = dtfs_temp->nH;
    1078           0 :     hSC_VBR->dtfs_enc_nH_4kHz = dtfs_temp->nH_4kHz;
    1079           0 :     hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest = dtfs_temp->upper_cut_off_freq_of_interest;
    1080           0 :     hSC_VBR->dtfs_enc_upper_cut_off_freq = dtfs_temp->upper_cut_off_freq;
    1081             : 
    1082           0 :     mvr2r( dtfs_temp->a, hSC_VBR->dtfs_enc_a, MAXLAG_WI );
    1083           0 :     mvr2r( dtfs_temp->b, hSC_VBR->dtfs_enc_b, MAXLAG_WI );
    1084             : 
    1085           0 :     free( CURRP_NQ );
    1086           0 :     free( TMPDTFS );
    1087           0 :     free( TMPDTFS2 );
    1088           0 :     free( TMPDTFS3 );
    1089           0 :     free( CURRP_Q_E );
    1090           0 :     free( dtfs_temp );
    1091             : 
    1092           0 :     return error;
    1093             : }
    1094             : 
    1095             : 
    1096             : /*-------------------------------------------------------------------*
    1097             :  * SynthesisFilter()
    1098             :  *
    1099             :  *
    1100             :  *--------------------------------------------------------------------*/
    1101             : 
    1102           0 : static void SynthesisFilter(
    1103             :     float *output,
    1104             :     float *input,
    1105             :     float *coef,
    1106             :     float *memory,
    1107             :     int16_t order,
    1108             :     int16_t length )
    1109             : {
    1110             :     int16_t i, j;
    1111             :     float acc;
    1112             : 
    1113             :     /* IIR filter for each subframe */
    1114           0 :     for ( i = 0; i < length; i++ )
    1115             :     {
    1116           0 :         for ( j = order - 1, acc = *input++; j > 0; j-- )
    1117             :         {
    1118           0 :             acc -= coef[j] * memory[j];
    1119           0 :             memory[j] = memory[j - 1];
    1120             :         }
    1121             : 
    1122           0 :         acc -= coef[0] * memory[0];
    1123           0 :         *output++ = acc;
    1124           0 :         memory[0] = acc;
    1125             :     }
    1126             : 
    1127           0 :     return;
    1128             : }
    1129             : 
    1130             : 
    1131             : /*---------------------------------------------------------------------*
    1132             :  * sc_vbr_enc_init()
    1133             :  *
    1134             :  * Initialize SC-VBR encoder
    1135             :  *---------------------------------------------------------------------*/
    1136             : 
    1137           3 : void sc_vbr_enc_init(
    1138             :     SC_VBR_ENC_HANDLE hSC_VBR /* i/o: SC-VBR encoder handle   */
    1139             : )
    1140             : {
    1141           3 :     hSC_VBR->nelp_enc_seed = 0;
    1142           3 :     hSC_VBR->last_nelp_mode = 0;
    1143           3 :     hSC_VBR->pppcountE = 0;
    1144           3 :     hSC_VBR->last_ppp_mode = 0;
    1145           3 :     hSC_VBR->last_last_ppp_mode = 0;
    1146           3 :     hSC_VBR->firstTime_voicedenc = 1;
    1147           3 :     hSC_VBR->prev_ppp_gain_pit = 0.0;
    1148           3 :     hSC_VBR->prev_tilt_code = 0.0;
    1149             : 
    1150           3 :     hSC_VBR->ppp_mode = 0;
    1151           3 :     hSC_VBR->nelp_mode = 0;
    1152             : 
    1153           3 :     hSC_VBR->pattern_m = 0;
    1154           3 :     hSC_VBR->Last_Resort = 0;
    1155           3 :     hSC_VBR->set_ppp_generic = 0;
    1156           3 :     hSC_VBR->Q_to_F = 0;
    1157             : 
    1158           3 :     hSC_VBR->numactive = 0;          /* keep the count of the frames inside current 600 frame bloack.*/
    1159           3 :     hSC_VBR->sum_of_rates = 0.0f;    /* sum of the rates of past 600 active frames*/
    1160           3 :     hSC_VBR->global_avr_rate = 0.0f; /* global rate upto current time. recorded a (rate in kbps) *6000*/
    1161           3 :     hSC_VBR->global_frame_cnt = 0;   /* 600 active frame block count. Used to update the global rate*/
    1162             : 
    1163           3 :     hSC_VBR->rate_control = 0;
    1164           3 :     hSC_VBR->SNR_THLD = 67.0f;
    1165           3 :     hSC_VBR->mode_QQF = 1;
    1166             : 
    1167           3 :     set_f( hSC_VBR->shape1_filt_mem, 0, 20 );
    1168           3 :     set_f( hSC_VBR->shape2_filt_mem, 0, 20 );
    1169           3 :     set_f( hSC_VBR->shape3_filt_mem, 0, 20 );
    1170           3 :     set_f( hSC_VBR->txlpf1_filt1_mem, 0, 20 );
    1171           3 :     set_f( hSC_VBR->txlpf1_filt2_mem, 0, 20 );
    1172           3 :     set_f( hSC_VBR->txhpf1_filt1_mem, 0, 20 );
    1173           3 :     set_f( hSC_VBR->txhpf1_filt2_mem, 0, 20 );
    1174             : 
    1175           3 :     hSC_VBR->last_7k2_coder_type = GENERIC;
    1176           3 :     hSC_VBR->vbr_generic_ho = 0;
    1177           3 :     hSC_VBR->Local_VAD = 0;
    1178             : 
    1179           3 :     return;
    1180             : }

Generated by: LCOV version 1.14