LCOV - code coverage report
Current view: top level - lib_enc - voiced_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 407 468 87.0 %
Date: 2025-05-28 04:28:20 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #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         117 : 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         117 :     float pwf = 0.7f, tmplpc[M + 1];
      69             :     DTFS_STRUCTURE X1_DTFS;
      70             : 
      71         117 :     X1_DTFS = refX1_DTFS; /* copy into local copy */
      72             : 
      73         117 :     DTFS_adjustLag( &X1_DTFS, X2_DTFS.lag );
      74             : 
      75         117 :     DTFS_poleFilter( &X1_DTFS, LPC2, M + 1 );
      76         117 :     DTFS_poleFilter( &X2_DTFS, LPC2, M + 1 );
      77             : 
      78        2106 :     for ( k = 0, tmp = 1.0; k < M + 1; k++ )
      79             :     {
      80        1989 :         tmplpc[k] = LPC2[k] * ( tmp *= pwf );
      81             :     }
      82         117 :     DTFS_zeroFilter( &X1_DTFS, tmplpc, M + 1 );
      83         117 :     DTFS_zeroFilter( &X2_DTFS, tmplpc, M + 1 );
      84             : 
      85         117 :     maxcorr = (float) -HUGE_VAL;
      86         117 :     fshift = Eshift;
      87         117 :     Adiff = max( 4.0f, refX1_DTFS.lag / 8 );
      88         117 :     diff = 1.0f; /* Non-Fractional alignment */
      89             : 
      90        1926 :     for ( n = Eshift - Adiff; n <= Eshift + Adiff; n += diff )
      91             :     {
      92        1809 :         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        1809 :         tmp1 = (float) ( PI2 * n / X2_DTFS.lag );
      95             : 
      96       43174 :         for ( k = 0; k <= min( X2_DTFS.lag >> 1, X2_DTFS.nH_4kHz ); k++, tmp += tmp1 )
      97             : 
      98             :         {
      99       41365 :             corr += (float) ( ( X1_DTFS.a[k] * X2_DTFS.a[k] + X1_DTFS.b[k] * X2_DTFS.b[k] ) * cos( tmp ) );
     100       41365 :             corr += (float) ( ( X1_DTFS.b[k] * X2_DTFS.a[k] - X1_DTFS.a[k] * X2_DTFS.b[k] ) * sin( tmp ) );
     101             :         }
     102             : 
     103        1809 :         if ( corr * ( 1.0f - 0.01f * fabs( n - Eshift ) ) > maxcorr )
     104             :         {
     105         836 :             fshift = n;
     106         836 :             maxcorr = corr;
     107             :         }
     108             :     }
     109             : 
     110         117 :     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        1638 : 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        1638 :     fdiff = (float) INT_FS_12k8 / X.lag;
     130             : 
     131        1638 :     en = 0.0f;
     132       31136 :     for ( freq = fdiff, k = 1; k <= min( ( X.lag - 1 ) >> 1, X.nH_4kHz ); k++, freq += fdiff )
     133             :     {
     134       29498 :         if ( freq < hband && freq >= lband )
     135             :         {
     136       25544 :             en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     137             :         }
     138             :     }
     139        1638 :     en /= 2.0;
     140        1638 :     if ( lband == 0.0 )
     141             :     {
     142           0 :         en += X.a[0] * X.a[0];
     143             :     }
     144        1638 :     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        1638 :     return en;
     149             : }
     150             : 
     151             : 
     152             : /*-------------------------------------------------------------------*
     153             :  * DTFS_freq_corr()
     154             :  *
     155             :  * Calculate correlation between two DTFS.
     156             :  *-------------------------------------------------------------------*/
     157             : 
     158             : /*! r: correlation */
     159         704 : 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         704 :     if ( X1_DTFS.lag < X2_DTFS.lag )
     171             :     {
     172          84 :         DTFS_zeroPadd( X2_DTFS.lag, &X1_DTFS );
     173             :     }
     174             : 
     175         704 :     corr = freq = 0.0;
     176         704 :     fdiff = (float) INT_FS_12k8 / X2_DTFS.lag;
     177       13980 :     for ( k = 0; k <= min( X2_DTFS.lag >> 1, X2_DTFS.nH_4kHz ); k++, freq += fdiff )
     178             :     {
     179       13276 :         if ( freq < hband && freq >= lband )
     180             :         {
     181       11572 :             corr += ( X1_DTFS.a[k] * X2_DTFS.a[k] + X1_DTFS.b[k] * X2_DTFS.b[k] );
     182             :         }
     183             :     }
     184             : 
     185         704 :     tmp = DTFS_getEngy_band( X1_DTFS, lband, hband ) * DTFS_getEngy_band( X2_DTFS, lband, hband );
     186             : 
     187         704 :     if ( tmp == 0.0 )
     188             :     {
     189           0 :         tmp = 0.001;
     190             :     }
     191             : 
     192         704 :     return corr / sqrt( tmp );
     193             : }
     194             : 
     195             : /*-------------------------------------------------------------------*
     196             :  * DTFS_peaktoaverage()
     197             :  *
     198             :  * Estimate peak to average ratio in the DTFS
     199             :  *-------------------------------------------------------------------*/
     200             : 
     201         364 : 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         364 :     float tmp, time[PIT_MAX], sum = 0.0, maxPosEn = 0.0, maxNegEn = 0.0;
     208             :     int16_t i;
     209             : 
     210         364 :     DTFS_fs_inv( &X, time, X.lag, 0.0 );
     211             : 
     212       23322 :     for ( i = 0; i < X.lag; i++ )
     213             :     {
     214       22958 :         tmp = SQR( time[i] );
     215       22958 :         if ( time[i] >= 0 )
     216             :         {
     217       11465 :             if ( tmp > maxPosEn )
     218             :             {
     219        1560 :                 maxPosEn = tmp;
     220             :             }
     221             :         }
     222             :         else
     223             :         {
     224       11493 :             if ( tmp > maxNegEn )
     225             :             {
     226        1699 :                 maxNegEn = tmp;
     227             :             }
     228             :         }
     229       22958 :         sum += tmp;
     230             :     }
     231             : 
     232         364 :     if ( sum == 0.0 )
     233             :     {
     234           0 :         *pos = 0.0f;
     235           0 :         *neg = 0.0f;
     236             :     }
     237             :     else
     238             :     {
     239         364 :         if ( maxPosEn == 0.0 )
     240             :         {
     241           0 :             *pos = 0.0;
     242             :         }
     243             :         else
     244             :         {
     245         364 :             *pos = (float) sqrt( maxPosEn * X.lag / sum );
     246             :         }
     247         364 :         if ( maxPosEn == 0.0 )
     248             :         {
     249           0 :             *neg = 0.0;
     250             :         }
     251             :         else
     252             :         {
     253         364 :             *neg = (float) sqrt( maxNegEn * X.lag / sum );
     254             :         }
     255             :     }
     256             : 
     257         364 :     return;
     258             : }
     259             : 
     260             : 
     261             : /*-------------------------------------------------------------------*
     262             :  * ppp_extract_pitch_period()
     263             :  *
     264             :  *  Pitch period extraction
     265             :  *-------------------------------------------------------------------*/
     266             : 
     267         452 : 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         452 :     int16_t spike = 0, range;
     276         452 :     float max1 = 0.0;
     277         452 :     const float *ptr = in + L_FRAME - l;
     278         452 :     float en1 = 0.0, en2 = 0.0, tmp;
     279         452 :     int16_t spike_near_edge = 0;
     280             :     float pos_max, neg_max;
     281         452 :     int16_t spike_pos = 0, spike_neg = 0;
     282             :     float x;
     283             : 
     284         452 :     pos_max = (float) -HUGE_VAL;
     285         452 :     neg_max = 0.0;
     286         452 :     *out_of_bound = 0;
     287             : 
     288       26728 :     for ( i = 0; i < l; i++ )
     289             :     {
     290       26276 :         if ( ( x = (float) fabs( ptr[i] ) ) > max1 )
     291             :         {
     292        2167 :             max1 = x;
     293        2167 :             spike = i;
     294             :         }
     295       26276 :         en1 += ptr[i] * ptr[i];
     296             :     }
     297             : 
     298         452 :     if ( ptr[spike] > 0 )
     299             :     {
     300         264 :         spike_pos = spike;
     301             : 
     302             :         /* search for neg spike around the pos spike */
     303        5544 :         for ( j = spike - 10; j < spike + 10; j++ )
     304             :         {
     305        5280 :             k = ( j + l ) % l;
     306        5280 :             if ( ptr[k] < neg_max )
     307             :             {
     308         845 :                 neg_max = ptr[k];
     309         845 :                 spike_neg = k;
     310             :             }
     311             :         }
     312             :     }
     313         188 :     else if ( ptr[spike] < 0 )
     314             :     {
     315         188 :         spike_neg = spike;
     316             : 
     317             :         /* search for pos spike around the neg spike */
     318        3948 :         for ( j = spike - 10; j < spike + 10; j++ )
     319             :         {
     320        3760 :             k = ( j + l ) % l;
     321        3760 :             if ( ptr[k] > pos_max )
     322             :             {
     323         667 :                 pos_max = ptr[k];
     324         667 :                 spike_pos = k;
     325             :             }
     326             :         }
     327             :     }
     328         452 :     if ( ( ( l - 1 - max( spike_pos, spike_neg ) ) <= 2 ) || ( min( spike_pos, spike_neg ) <= 2 ) )
     329             :     {
     330          96 :         *out_of_bound = 1;
     331          96 :         return spike_near_edge;
     332             :     }
     333         356 :     range = (int16_t) anint( max( CUTFREE_REL_RANGE * l, CUTFREE_ABS_RANGE ) );
     334         356 :     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         121 :         if ( L_FRAME - l - l < 0 )
     339             :         {
     340           4 :             *out_of_bound = 1;
     341           4 :             return spike_near_edge;
     342             :         }
     343         117 :         spike_near_edge = 1;
     344             :     }
     345         352 :     if ( spike - range < 0 )
     346             :     {
     347        3253 :         for ( i = 0; i < l + ( spike - range ); i++ )
     348             :         {
     349        3200 :             out[i] = ptr[i];
     350             :         }
     351             : 
     352             :         /* Grab Remaining From One Lag Before */
     353          53 :         ptr -= l;
     354         387 :         for ( ; i < l; i++ )
     355             :         {
     356         334 :             out[i] = ptr[i];
     357             :         }
     358             :     }
     359         299 :     else if ( spike + range >= l )
     360             :     {
     361        2182 :         for ( i = 0; i < spike - range; i++ )
     362             :         {
     363        2118 :             out[i] = ptr[i];
     364             :         }
     365             :         /* Grab Remaining From One Lag Before */
     366          64 :         if ( ptr - l + i >= in )
     367             :         {
     368        1582 :             for ( ptr -= l; i < l; i++ )
     369             :             {
     370        1518 :                 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       13809 :         for ( i = 0; i < l; i++ )
     384             :         {
     385       13574 :             out[i] = ptr[i];
     386             :         }
     387             :     }
     388             : 
     389             :     /* Energy adjustment added to eliminate artifacts at the end of voicing */
     390       21096 :     for ( i = 0; i < l; i++ )
     391             :     {
     392       20744 :         en2 += out[i] * out[i];
     393             :     }
     394             : 
     395         352 :     if ( en1 < en2 )
     396             :     {
     397          61 :         tmp = (float) sqrt( en1 / en2 );
     398        3789 :         for ( i = 0; i < l; i++ )
     399             :         {
     400        3728 :             out[i] *= tmp;
     401             :         }
     402             :     }
     403             : 
     404         352 :     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         380 : 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         380 :     fdiff = (float) INT_FS_12k8 / X.lag;
     428             : 
     429         380 :     en = 0.0f;
     430       11468 :     for ( freq = fdiff, k = 1; k <= ( ( X.lag - 1 ) >> 1 ); k++, freq += fdiff )
     431             :     {
     432       11088 :         if ( freq < hband && freq >= lband )
     433             :         {
     434        4462 :             en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     435             :         }
     436             :     }
     437         380 :     en /= 2.0;
     438         380 :     if ( lband == 0.0 )
     439             :     {
     440         285 :         en += X.a[0] * X.a[0];
     441             :     }
     442         380 :     if ( ( X.lag % 2 == 0 ) && ( hband == X.upper_cut_off_freq ) )
     443             :     {
     444          95 :         en += X.a[k] * X.a[k] + X.b[k] * X.b[k];
     445             :     }
     446         380 :     return en;
     447             : }
     448             : 
     449             : 
     450             : /*-------------------------------------------------------------------*
     451             :  * ppp_voiced_encoder()
     452             :  *
     453             :  *
     454             :  *--------------------------------------------------------------------*/
     455             : 
     456         467 : 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         467 :     int16_t spike_near_edge = 0;
     473             :     int16_t flag;
     474         467 :     int16_t delta_lag_E = 0, PPP_MODE_E, Q_delta_lag = 0;
     475         467 :     int16_t out_of_bound = 0;
     476         467 :     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         467 :     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         467 :     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         467 :     error = IVAS_ERR_OK;
     500             : 
     501         467 :     if ( ( error = DTFS_new( &CURRP_NQ ) ) != IVAS_ERR_OK )
     502             :     {
     503           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     504             :     }
     505         467 :     if ( ( error = DTFS_new( &TMPDTFS ) ) != IVAS_ERR_OK )
     506             :     {
     507           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     508             :     }
     509         467 :     if ( ( error = DTFS_new( &TMPDTFS2 ) ) != IVAS_ERR_OK )
     510             :     {
     511           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     512             :     }
     513         467 :     if ( ( error = DTFS_new( &TMPDTFS3 ) ) != IVAS_ERR_OK )
     514             :     {
     515           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     516             :     }
     517         467 :     if ( ( error = DTFS_new( &CURRP_Q_E ) ) != IVAS_ERR_OK )
     518             :     {
     519           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     520             :     }
     521         467 :     if ( ( error = DTFS_new( &dtfs_temp ) ) != IVAS_ERR_OK )
     522             :     {
     523           0 :         IVAS_ERROR( error, "Error creating DTFS structure" );
     524             :     }
     525             : 
     526         467 :     if ( bwidth == WB )
     527             :     {
     528         269 :         upper_cut_off_freq_of_interest = 4000;
     529         269 :         upper_cut_off_freq = 6400;
     530             :     }
     531         198 :     else if ( bwidth == NB )
     532             :     {
     533         198 :         upper_cut_off_freq_of_interest = 3300;
     534         198 :         upper_cut_off_freq = 4000;
     535             :     }
     536             : 
     537             :     /* Initialization */
     538         467 :     if ( hSC_VBR->firstTime_voicedenc )
     539             :     {
     540          12 :         hSC_VBR->firstTime_voicedenc = 0;
     541          12 :         hSC_VBR->dtfs_enc_lag = 0;
     542          12 :         hSC_VBR->dtfs_enc_nH = 0;
     543          12 :         hSC_VBR->dtfs_enc_nH_4kHz = 0;
     544          12 :         hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest = 3300.0;
     545          12 :         hSC_VBR->dtfs_enc_upper_cut_off_freq = 4000.0;
     546             : 
     547          12 :         set_f( hSC_VBR->dtfs_enc_a, 0, MAXLAG_WI );
     548          12 :         set_f( hSC_VBR->dtfs_enc_b, 0, MAXLAG_WI );
     549             :     }
     550             : 
     551             :     /* Figure out the PPP_MODE */
     552         467 :     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         467 :     if ( hSC_VBR->last_ppp_mode == 1 )
     569             :     {
     570          74 :         hSC_VBR->rate_control = 0;
     571             :     }
     572             : 
     573         467 :     PPP_MODE_E = 'Q';
     574         467 :     pl = min( MAX_LAG_PIT, (int16_t) rint_new( old_pitch_buf[( 2 * NB_SUBFR ) - 1] ) );
     575         467 :     l = min( MAX_LAG_PIT, (int16_t) rint_new( delay ) );
     576             : 
     577             :     /* Bump up if the lag is out_fx of range */
     578         467 :     if ( ( l - pl ) > 13 || ( l - pl ) < -11 || ( l < 19 ) || ( pl < 19 ) )
     579             :     {
     580          15 :         hSC_VBR->bump_up = 1;
     581             : 
     582          15 :         free( CURRP_NQ );
     583          15 :         free( TMPDTFS );
     584          15 :         free( TMPDTFS2 );
     585          15 :         free( TMPDTFS3 );
     586          15 :         free( CURRP_Q_E );
     587          15 :         free( dtfs_temp );
     588             : 
     589          15 :         return error;
     590             :     }
     591             : 
     592         452 :     if ( hSC_VBR->last_ppp_mode != 1 )
     593             :     {
     594             :         /* Obtain DTFS of last pl values of past excitation */
     595         378 :         DTFS_to_fs( exc - pl, pl, dtfs_temp, bwidth == WB ? (int16_t) 16000 : (int16_t) 8000, 0 );
     596             :     }
     597             : 
     598         452 :     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         452 :     spike_near_edge = ppp_extract_pitch_period( in, out, l, &out_of_bound );
     605             : 
     606         452 :     if ( out_of_bound == 1 )
     607             :     {
     608         100 :         hSC_VBR->bump_up = 1;
     609             : 
     610         100 :         free( CURRP_NQ );
     611         100 :         free( TMPDTFS );
     612         100 :         free( TMPDTFS2 );
     613         100 :         free( TMPDTFS3 );
     614         100 :         free( CURRP_Q_E );
     615         100 :         free( dtfs_temp );
     616             : 
     617         100 :         return error;
     618             :     }
     619             : 
     620             :     /* Get DTFS of current prototype */
     621         352 :     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         352 :     if ( spike_near_edge != 0 )
     628             :     {
     629         117 :         DTFS_to_fs( in + L_FRAME - l, l, TMPDTFS, bwidth == WB ? (int16_t) 16000 : (int16_t) 8000, 0 );
     630             : 
     631         117 :         tmp = DTFS_alignment_extract( *TMPDTFS, *CURRP_NQ, 0.0, lpc2 ); /* figure out how much to shift currp_nq to align with TMP */
     632             : 
     633         117 :         DTFS_phaseShift( CURRP_NQ, (float) ( PI2 * tmp / l ) );
     634             :     }
     635             : 
     636         352 :     temp_pl = (float) pl;
     637         352 :     temp_l = (float) l;
     638             : 
     639        1760 :     for ( i = 0; i < NB_SUBFR; i++ )
     640             :     {
     641             :         /* do the linear pitch interp to drive the nb_post_filt */
     642        1408 :         Interpol_delay( interp_delay, &( temp_pl ), &( temp_l ), i, frac_4sf );
     643        1408 :         pitch[i] = interp_delay[0];
     644             :     }
     645             : 
     646             : 
     647             :     /* Restoring PPP memories when the last frame is non-PPP */
     648         352 :     if ( hSC_VBR->last_ppp_mode != 1 )
     649             :     {
     650         297 :         hSC_VBR->ph_offset_E = 0.0;
     651         297 :         hSC_VBR->prev_cw_en = DTFS_getEngy( *dtfs_temp );
     652             : 
     653             :         /* Copy over dtfs_temp into TMPDTFS */
     654         297 :         DTFS_copy( TMPDTFS, *dtfs_temp );
     655             : 
     656         297 :         DTFS_car2pol( TMPDTFS );
     657             : 
     658         297 :         hSC_VBR->lastLgainE = (float) log10( TMPDTFS->lag * DTFS_setEngyHarm( 92.0, 1104.5, 0.0, 1104.5, 1.0, TMPDTFS ) );
     659         297 :         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         297 :         DTFS_to_erb( *TMPDTFS, hSC_VBR->lasterbE );
     662             :     }
     663             :     else
     664             :     {
     665             :         /* Copy DTFS related parameters from 'hSC_VBR' to 'dtfs_temp' structure */
     666          55 :         dtfs_temp->lag = hSC_VBR->dtfs_enc_lag;
     667          55 :         dtfs_temp->nH = hSC_VBR->dtfs_enc_nH;
     668          55 :         dtfs_temp->nH_4kHz = hSC_VBR->dtfs_enc_nH_4kHz;
     669          55 :         dtfs_temp->upper_cut_off_freq_of_interest = hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest;
     670          55 :         dtfs_temp->upper_cut_off_freq = hSC_VBR->dtfs_enc_upper_cut_off_freq;
     671             : 
     672          55 :         mvr2r( hSC_VBR->dtfs_enc_a, dtfs_temp->a, MAXLAG_WI );
     673          55 :         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         352 :     res_enratio = DTFS_getEngy( *CURRP_NQ ) / DTFS_getEngy( *dtfs_temp );
     682             : 
     683             :     /* Copy over CURRP_NQ into TMPDTFS */
     684         352 :     DTFS_copy( TMPDTFS, *CURRP_NQ );
     685             : 
     686             :     /* Copy over dtfs_temp into TMPDTFS2 */
     687         352 :     DTFS_copy( TMPDTFS2, *dtfs_temp );
     688             : 
     689         352 :     tmptmp = DTFS_alignment_full( *TMPDTFS2, *TMPDTFS, TMPDTFS->lag * 2 ); /* align of prev_cw wrt curr_cw, new method */
     690             : 
     691         352 :     tmptmp1 = TMPDTFS->lag - tmptmp;
     692         352 :     tmp = tmptmp1;
     693             : 
     694         352 :     DTFS_phaseShift( TMPDTFS, (float) ( -PI2 * tmp / TMPDTFS->lag ) ); /* fixed bug, phase shift by tmp computed in TMP.lag domain (above) */
     695         352 :     tmpres = (float) ( DTFS_freq_corr( *TMPDTFS, *TMPDTFS2, 100.0f, 3700.0f ) );
     696             : 
     697         352 :     DTFS_poleFilter( TMPDTFS, lpc2, M + 1 );
     698             : 
     699         352 :     DTFS_adjustLag( TMPDTFS2, TMPDTFS->lag ); /* operate in CL domain */
     700             : 
     701         352 :     DTFS_poleFilter( TMPDTFS2, lpc1, M + 1 );
     702             : 
     703         352 :     tmp = (float) ( DTFS_freq_corr( *TMPDTFS, *TMPDTFS2, 100.0f, 3700.0f ) );
     704             : 
     705         352 :     if ( DTFS_getEngy( *TMPDTFS2 ) > 0 )
     706             :     {
     707         352 :         sp_enratio = DTFS_getEngy( *TMPDTFS ) / DTFS_getEngy( *TMPDTFS2 );
     708             :     }
     709             :     else
     710             :     {
     711           0 :         sp_enratio = 0.0f;
     712             :     }
     713             : 
     714         352 :     if ( PPP_MODE_E == 'Q' )
     715             :     {
     716             :         /* Bump up if the lag is out of range */
     717         352 :         if ( ( ( l - pl ) > 13 ) || ( ( l - pl ) < -11 ) )
     718             :         {
     719           0 :             PPP_MODE_E = 'B';
     720             :         }
     721             :         else
     722             :         {
     723         352 :             delta_lag_E = (int16_t) ( l - pl );
     724             :         }
     725             : 
     726             :         /* Bump up if big change between the previous and the current CWs */
     727         352 :         if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     728             :         {
     729          27 :             if ( res_enratio > 5.0 && tmp < 0.65 )
     730             :             {
     731           0 :                 PPP_MODE_E = 'B';
     732             :             }
     733             :         }
     734             :         else
     735             :         {
     736         325 :             if ( res_enratio > 3.0 && tmp < 1.2 )
     737             :             {
     738          21 :                 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         352 :     if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     746             :     {
     747          27 :         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         325 :         if ( res_enratio < 0.092f )
     764             :         {
     765           1 :             hSC_VBR->bump_up = 1;
     766             :         }
     767             :     }
     768             : 
     769         352 :     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         352 :     if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     777             :     {
     778          27 :         if ( res_enratio > 14.5 )
     779             :         {
     780           1 :             hSC_VBR->bump_up = 1;
     781             : 
     782           1 :             free( CURRP_NQ );
     783           1 :             free( TMPDTFS );
     784           1 :             free( TMPDTFS2 );
     785           1 :             free( TMPDTFS3 );
     786           1 :             free( CURRP_Q_E );
     787           1 :             free( dtfs_temp );
     788             : 
     789           1 :             return error;
     790             :         }
     791             :     }
     792             :     else
     793             :     {
     794         325 :         if ( res_enratio > 7.0 )
     795             :         {
     796          14 :             hSC_VBR->bump_up = 1;
     797             :         }
     798             :     }
     799             : 
     800         351 :     if ( hSC_VBR->bump_up == 1 )
     801             :     {
     802          15 :         free( CURRP_NQ );
     803          15 :         free( TMPDTFS );
     804          15 :         free( TMPDTFS2 );
     805          15 :         free( TMPDTFS3 );
     806          15 :         free( CURRP_Q_E );
     807          15 :         free( dtfs_temp );
     808             : 
     809          15 :         return error;
     810             :     }
     811             : 
     812             :     /* Bump up when the previous frame is an unvoiced or a silent frame */
     813         336 :     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         336 :     if ( PPP_MODE_E == 'Q' )
     830             :     {
     831         317 :         flag = 1;
     832         317 :         if ( PPP_MODE_E == 'Q' )
     833             :         {
     834         317 :             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         317 :         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         178 :             DTFS_peaktoaverage( *TMPDTFS, &pos_nq, &neg_nq );
     855         178 :             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         178 :             if ( CURRP_Q_E->upper_cut_off_freq > 4000 )
     865             :             {
     866             :                 /* Use this bumup only for WB signals */
     867          95 :                 if ( DTFS_getEngy_band_wb( *CURRP_Q_E, 0.0, 2000.0 ) > 0 )
     868             :                 {
     869          95 :                     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          95 :                 low_band_en = (float) DTFS_getEngy_band_wb( *CURRP_Q_E, 0.0, 2000.0 );
     877             : 
     878          95 :                 if ( low_band_en < 25.0f && sp_hb_enratio < 1.6f )
     879             :                 {
     880           0 :                     PPP_MODE_E = 'B';
     881             :                 }
     882             :             }
     883             : 
     884         178 :             if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     885             :             {
     886          15 :                 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           1 :                     if ( pos_nq > neg_nq && pos_nq > 2.0f * pos_q )
     889             :                     {
     890           0 :                         PPP_MODE_E = 'B';
     891             :                     }
     892             : 
     893           1 :                     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         163 :                 if ( ( ( ( DTFS_getEngy( *CURRP_NQ ) > ( hSC_VBR->prev_cw_en ) ) && ( max( pos_nq, neg_nq ) > 3.5 ) ) && ( hSC_VBR->rate_control ) ) ||
     902         163 :                      ( ( ( DTFS_getEngy( *CURRP_NQ ) > 0.8 * ( hSC_VBR->prev_cw_en ) ) && ( max( pos_nq, neg_nq ) > 3.0 ) ) && ( !hSC_VBR->rate_control ) ) )
     903             :                 {
     904          33 :                     if ( ( ( pos_nq > neg_nq ) && ( pos_nq > 2.5 * pos_q ) && ( hSC_VBR->rate_control ) ) ||
     905          33 :                          ( ( pos_nq > neg_nq ) && ( pos_nq > 2.0 * pos_q ) && ( !hSC_VBR->rate_control ) ) )
     906             :                     {
     907           0 :                         PPP_MODE_E = 'B';
     908             :                     }
     909             : 
     910          33 :                     if ( ( ( ( pos_nq < neg_nq ) && ( neg_nq > 2.5 * neg_q ) ) && ( hSC_VBR->rate_control ) ) ||
     911          33 :                          ( ( 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         163 :                 if ( hSC_VBR->rate_control )
     918             :                 {
     919           8 :                     DTFS_peaktoaverage( *CURRP_NQ, &pos_nq0, &neg_nq0 );
     920             : 
     921        1280 :                     for ( impzi[0] = 1.0, i = 1; i < 160; i++ )
     922             :                     {
     923        1272 :                         impzi[i] = 0.0;
     924             :                     }
     925             : 
     926        1288 :                     for ( i = 0; i < 160; i++ )
     927             :                     {
     928        1280 :                         impzo[i] = 0.0;
     929             :                     }
     930             : 
     931          88 :                     for ( i = 0; i < 10; i++ )
     932             :                     {
     933          80 :                         mem[i] = 0.0;
     934             :                     }
     935             : 
     936           8 :                     SynthesisFilter( &impzo[0], &impzi[0], lpc2, &mem[0], 10, 160 );
     937             : 
     938        1288 :                     for ( i = 0; i < 160; i++ )
     939             :                     {
     940        1280 :                         energy_impz += ( impzo[i] * impzo[i] );
     941             :                     }
     942             : 
     943           8 :                     energy_impz = (float) ( 10 * log10( (float) energy_impz ) );
     944             : 
     945           8 :                     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           1 :                         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           1 :                             PPP_MODE_E = 'B';
     950             :                         }
     951             : 
     952           1 :                         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         178 :             DTFS_copy( TMPDTFS2, *CURRP_Q_E );
     961             : 
     962         178 :             DTFS_poleFilter( TMPDTFS, lpc2, M + 1 );
     963         178 :             DTFS_poleFilter( TMPDTFS2, lpc2, M + 1 );
     964             : 
     965         178 :             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         178 :             tmp2 = DTFS_getEngy_band( *TMPDTFS, 1500.0, upper_cut_off_freq_of_interest ) / DTFS_getEngy( *TMPDTFS );
     969         178 :             if ( tmp2 == 0.0 )
     970             :             {
     971           0 :                 tmp2 = 0.001;
     972             :             }
     973         178 :             if ( ( tmp2 > 0.05 ) && ( !hSC_VBR->rate_control ) )
     974             :             {
     975          26 :                 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          26 :                 if ( tmp2 == 0.0 )
     977             :                 {
     978           0 :                     tmp2 = 0.001;
     979             :                 }
     980          26 :                 if ( 10.0 * log10( tmp2 ) < 0.1 )
     981             :                 {
     982           9 :                     if ( res_enratio > 0.8 )
     983             :                     {
     984           7 :                         PPP_MODE_E = 'B';
     985             :                     }
     986             :                 }
     987             :             }
     988             : 
     989             :             /* To increase bump up, raise first threshold, lower second  */
     990         178 :             tmp = (float) ( 10.0 * log10( DTFS_getEngy( *TMPDTFS ) / DTFS_getEngy( *TMPDTFS3 ) ) );
     991             : 
     992         178 :             if ( ( tmp <= 0 ) && ( !hSC_VBR->rate_control ) )
     993             :             {
     994          17 :                 PPP_MODE_E = 'B';
     995             :             }
     996             : 
     997         178 :             if ( hSC_VBR->vadsnr < hSC_VBR->SNR_THLD )
     998             :             {
     999          15 :                 if ( ( ( tmp < 3.05 && max( res_enratio, sp_enratio ) > 0.8 ) && ( hSC_VBR->rate_control ) ) ||
    1000          13 :                      ( ( tmp < 2.8 && max( res_enratio, sp_enratio ) > 0.65 ) && ( !hSC_VBR->rate_control ) ) )
    1001             :                 {
    1002           6 :                     PPP_MODE_E = 'B';
    1003             :                 }
    1004             :             }
    1005             :             else
    1006             :             {
    1007         163 :                 if ( ( ( tmp < 2.4 && max( res_enratio, sp_enratio ) > 0.94 ) && ( hSC_VBR->rate_control ) ) ||
    1008          55 :                      ( ( tmp < 4.5 && max( res_enratio, sp_enratio ) > 0.5 ) && ( !hSC_VBR->rate_control ) ) )
    1009             :                 {
    1010          50 :                     PPP_MODE_E = 'B';
    1011             :                 }
    1012             :             }
    1013             :             /* -----End closed-loop Bump-Up */
    1014             :         }
    1015             :         else
    1016             :         {
    1017         139 :             PPP_MODE_E = 'B'; /* Amplitude quantization is failing */
    1018             :         }
    1019             :     }
    1020             :     else
    1021             :     {
    1022             :     }
    1023             : 
    1024         336 :     if ( PPP_MODE_E == 'B' )
    1025             :     {
    1026         218 :         hSC_VBR->bump_up = 1;
    1027             : 
    1028         218 :         free( CURRP_NQ );
    1029         218 :         free( TMPDTFS );
    1030         218 :         free( TMPDTFS2 );
    1031         218 :         free( TMPDTFS3 );
    1032         218 :         free( CURRP_Q_E );
    1033         218 :         free( dtfs_temp );
    1034             : 
    1035         218 :         return error;
    1036             :     }
    1037             : 
    1038         118 :     if ( hSC_VBR->Q_to_F )
    1039             :     {
    1040           8 :         hSC_VBR->patterncount += hSC_VBR->pattern_m;
    1041             : 
    1042           8 :         if ( hSC_VBR->patterncount >= 1000 )
    1043             :         {
    1044           8 :             hSC_VBR->patterncount -= 1000;
    1045           8 :             PPP_MODE_E = 'B';
    1046           8 :             hSC_VBR->bump_up = 1;
    1047             : 
    1048           8 :             free( CURRP_NQ );
    1049           8 :             free( TMPDTFS );
    1050           8 :             free( TMPDTFS2 );
    1051           8 :             free( TMPDTFS3 );
    1052           8 :             free( CURRP_Q_E );
    1053           8 :             free( dtfs_temp );
    1054             : 
    1055           8 :             return error;
    1056             :         }
    1057             :     }
    1058             : 
    1059             :     /* packetization of the delta lag in PPP */
    1060         110 :     if ( PPP_MODE_E == 'Q' )
    1061             :     {
    1062         110 :         Q_delta_lag = delta_lag_E + 11; /* to make it positive always */
    1063         110 :         push_indice( hBstr, IND_DELTALAG, Q_delta_lag, 5 );
    1064             :     }
    1065             : 
    1066         110 :     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         110 :     DTFS_copy( dtfs_temp, *CURRP_Q_E );
    1073         110 :     hSC_VBR->prev_cw_en = DTFS_getEngy( *CURRP_NQ );
    1074             : 
    1075             :     /* Copy DTFS related parameters from 'dtfs_temp' to 'hSC_VBR' structure */
    1076         110 :     hSC_VBR->dtfs_enc_lag = dtfs_temp->lag;
    1077         110 :     hSC_VBR->dtfs_enc_nH = dtfs_temp->nH;
    1078         110 :     hSC_VBR->dtfs_enc_nH_4kHz = dtfs_temp->nH_4kHz;
    1079         110 :     hSC_VBR->dtfs_enc_upper_cut_off_freq_of_interest = dtfs_temp->upper_cut_off_freq_of_interest;
    1080         110 :     hSC_VBR->dtfs_enc_upper_cut_off_freq = dtfs_temp->upper_cut_off_freq;
    1081             : 
    1082         110 :     mvr2r( dtfs_temp->a, hSC_VBR->dtfs_enc_a, MAXLAG_WI );
    1083         110 :     mvr2r( dtfs_temp->b, hSC_VBR->dtfs_enc_b, MAXLAG_WI );
    1084             : 
    1085         110 :     free( CURRP_NQ );
    1086         110 :     free( TMPDTFS );
    1087         110 :     free( TMPDTFS2 );
    1088         110 :     free( TMPDTFS3 );
    1089         110 :     free( CURRP_Q_E );
    1090         110 :     free( dtfs_temp );
    1091             : 
    1092         110 :     return error;
    1093             : }
    1094             : 
    1095             : 
    1096             : /*-------------------------------------------------------------------*
    1097             :  * SynthesisFilter()
    1098             :  *
    1099             :  *
    1100             :  *--------------------------------------------------------------------*/
    1101             : 
    1102           8 : 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        1288 :     for ( i = 0; i < length; i++ )
    1115             :     {
    1116       12800 :         for ( j = order - 1, acc = *input++; j > 0; j-- )
    1117             :         {
    1118       11520 :             acc -= coef[j] * memory[j];
    1119       11520 :             memory[j] = memory[j - 1];
    1120             :         }
    1121             : 
    1122        1280 :         acc -= coef[0] * memory[0];
    1123        1280 :         *output++ = acc;
    1124        1280 :         memory[0] = acc;
    1125             :     }
    1126             : 
    1127           8 :     return;
    1128             : }
    1129             : 
    1130             : 
    1131             : /*---------------------------------------------------------------------*
    1132             :  * sc_vbr_enc_init()
    1133             :  *
    1134             :  * Initialize SC-VBR encoder
    1135             :  *---------------------------------------------------------------------*/
    1136             : 
    1137         301 : void sc_vbr_enc_init(
    1138             :     SC_VBR_ENC_HANDLE hSC_VBR /* i/o: SC-VBR encoder handle   */
    1139             : )
    1140             : {
    1141         301 :     hSC_VBR->nelp_enc_seed = 0;
    1142         301 :     hSC_VBR->last_nelp_mode = 0;
    1143         301 :     hSC_VBR->pppcountE = 0;
    1144         301 :     hSC_VBR->last_ppp_mode = 0;
    1145         301 :     hSC_VBR->last_last_ppp_mode = 0;
    1146         301 :     hSC_VBR->firstTime_voicedenc = 1;
    1147         301 :     hSC_VBR->prev_ppp_gain_pit = 0.0;
    1148         301 :     hSC_VBR->prev_tilt_code = 0.0;
    1149             : 
    1150         301 :     hSC_VBR->ppp_mode = 0;
    1151         301 :     hSC_VBR->nelp_mode = 0;
    1152             : 
    1153         301 :     hSC_VBR->pattern_m = 0;
    1154         301 :     hSC_VBR->Last_Resort = 0;
    1155         301 :     hSC_VBR->set_ppp_generic = 0;
    1156         301 :     hSC_VBR->Q_to_F = 0;
    1157             : 
    1158         301 :     hSC_VBR->numactive = 0;          /* keep the count of the frames inside current 600 frame bloack.*/
    1159         301 :     hSC_VBR->sum_of_rates = 0.0f;    /* sum of the rates of past 600 active frames*/
    1160         301 :     hSC_VBR->global_avr_rate = 0.0f; /* global rate upto current time. recorded a (rate in kbps) *6000*/
    1161         301 :     hSC_VBR->global_frame_cnt = 0;   /* 600 active frame block count. Used to update the global rate*/
    1162             : 
    1163         301 :     hSC_VBR->rate_control = 0;
    1164         301 :     hSC_VBR->SNR_THLD = 67.0f;
    1165         301 :     hSC_VBR->mode_QQF = 1;
    1166             : 
    1167         301 :     set_f( hSC_VBR->shape1_filt_mem, 0, 20 );
    1168         301 :     set_f( hSC_VBR->shape2_filt_mem, 0, 20 );
    1169         301 :     set_f( hSC_VBR->shape3_filt_mem, 0, 20 );
    1170         301 :     set_f( hSC_VBR->txlpf1_filt1_mem, 0, 20 );
    1171         301 :     set_f( hSC_VBR->txlpf1_filt2_mem, 0, 20 );
    1172         301 :     set_f( hSC_VBR->txhpf1_filt1_mem, 0, 20 );
    1173         301 :     set_f( hSC_VBR->txhpf1_filt2_mem, 0, 20 );
    1174             : 
    1175         301 :     hSC_VBR->last_7k2_coder_type = GENERIC;
    1176         301 :     hSC_VBR->vbr_generic_ho = 0;
    1177         301 :     hSC_VBR->Local_VAD = 0;
    1178             : 
    1179         301 :     return;
    1180             : }

Generated by: LCOV version 1.14