LCOV - code coverage report
Current view: top level - lib_com - arith_coder_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 22b74eaeaa63d28e0fcc8ed21c8f7c451f461847 Lines: 239 253 94.5 %
Date: 2025-10-20 02:12:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include <assert.h>
       7             : #include "options.h"
       8             : #include "prot_fx.h"
       9             : #include "basop_util.h"
      10             : #include "basop_proto_func.h"
      11             : #include "cnst.h"
      12             : 
      13             : /* Fixed point implementation of exp(negate()) */
      14    11008960 : Word32 expfp(                    /* o: Q31 */
      15             :               const Word16 x,    /* i: mantissa  Q-e */
      16             :               const Word16 x_e ) /* i: exponent  Q0  */
      17             : {
      18             :     Word16 xi, xf, tmp;
      19             :     Word16 b0, b1, b2, b3;
      20             :     Word32 y, L_tmp;
      21             : 
      22             : 
      23    11008960 :     assert( x > 0 );
      24             : 
      25    11008960 :     L_tmp = L_shl( L_deposit_h( x ), x_e ); /* Q31 */
      26             : 
      27             :     /* split into integer and fractional parts */
      28    11008960 :     xi = round_fx( L_tmp );  /* Q15 */
      29    11008960 :     xf = extract_l( L_tmp ); /* Q31 */
      30             : 
      31             :     BASOP_SATURATE_WARNING_OFF_EVS;
      32    11008960 :     xf = negate( xf );
      33             :     BASOP_SATURATE_WARNING_ON_EVS;
      34             : 
      35             :     /* Fractional part */
      36             :     /* y = 65536
      37             :             +         xf
      38             :             +       ((xf*xf) / (2*65536))
      39             :             +   ((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)
      40             :             + ((((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)*xf) / (4*65536)); */
      41    11008960 :     y = L_mac0( 65536, xf, 1 ); /* Q16 */
      42    11008960 :     tmp = shr( mult( xf, xf ), 2 );
      43    11008960 :     y = L_mac0( y, tmp, 1 ); /* Q16 */
      44    11008960 :     tmp = shr( mult( shr( mult( tmp, xf ), 1 ), 65536 / 3 ), 1 );
      45    11008960 :     y = L_mac0( y, tmp, 1 ); /* Q16 */
      46    11008960 :     tmp = shr( mult( tmp, xf ), 3 );
      47    11008960 :     y = L_mac0( y, tmp, 1 ); /* Q16 */
      48             : 
      49             :     /* Integer part */
      50    11008960 :     b0 = s_and( xi, 1 );
      51    11008960 :     b1 = s_and( xi, 2 );
      52    11008960 :     b2 = s_and( xi, 4 );
      53    11008960 :     b3 = s_and( xi, 8 );
      54             : 
      55    11008960 :     IF( b0 != 0 )
      56             :     {
      57     5307175 :         y = Mpy_32_16_1( y, 24109 ); /* exp(-1) in -1Q16 */
      58             :     }
      59    11008960 :     IF( b1 != 0 )
      60             :     {
      61     5168275 :         y = Mpy_32_16_1( y, 17739 ); /* exp(-2) in -2Q17 */
      62             :     }
      63    11008960 :     IF( b2 != 0 )
      64             :     {
      65     6346530 :         y = Mpy_32_16_1( y, 19205 ); /* exp(-4) in -5Q20 */
      66             :     }
      67    11008960 :     IF( b3 != 0 )
      68             :     {
      69      878934 :         y = Mpy_32_16_1( y, 22513 ); /* exp(-8) in -11Q26 */
      70             :     }
      71             : 
      72             :     /* scaling: -1*b0 - 2*b1 -5*b2 -11*b3 */
      73    11008960 :     y = L_shr( y, add( add( xi, shr( xi, 2 ) ), shr( b3, 3 ) ) );
      74             : 
      75             :     /* zero for xi >= 16 */
      76    11008960 :     if ( shr( xi, 4 ) > 0 )
      77             :     {
      78           0 :         y = L_deposit_l( 0 );
      79             :     }
      80             : 
      81             : 
      82    11008960 :     return L_shl( y, 15 );
      83             : }
      84             : 
      85             : /* Fixed point implementation of pow(), where base is fixed point (16/16) and exponent a small *odd* integer
      86             :  *
      87             :  * Returns: *pout1 = ( (base/65536)^(2*exp - 1) ) * 65536
      88             :  *          *pout2 = ( (base/65536)^(2*exp + 1) ) * 65536
      89             :  *
      90             :  * NOTE: This function must be in sync with ari_decode_14bits_pow_fx() */
      91      877080 : void powfp_odd2(
      92             :     const Word16 base, /* Q15 */
      93             :     const Word16 exp,  /* Q0  */
      94             :     Word16 *pout1,     /* Q15 */
      95             :     Word16 *pout2      /* Q15 */
      96             : )
      97             : {
      98             :     /* this version is in sync with ari_enc_14bits_pow()
      99             :      * that is, we have to start multiplication from the largest power-of-two, in order to
     100             :      * get the rounding errors to appear at the same places */
     101             :     Word16 pows[12]; /* powers of two exponents*/
     102             :     Word16 exp2;
     103             :     Word16 out, out2;
     104             :     Word16 k, h, maxk;
     105             : 
     106      877080 :     assert( exp >= 0 );
     107             : 
     108      877080 :     out = base; /* Q15 */
     109      877080 :     move16();
     110      877080 :     out2 = 0x7FFF; /* 1 in Q15 */
     111      877080 :     move16();
     112      877080 :     IF( exp != 0 )
     113             :     {
     114      877080 :         exp2 = sub( exp, 1 );
     115      877080 :         maxk = sub( 15, norm_s( exp ) );
     116      877080 :         assert( maxk < 12 );
     117             : 
     118      877080 :         pows[0] = base; /* Q15 */
     119      877080 :         move16();
     120     2531533 :         FOR( k = 0; k < maxk; k++ )
     121             :         {
     122     1654453 :             pows[k + 1] = mult_r( pows[k], pows[k] ); /* Q15 */
     123     1654453 :             move16();
     124             :         }
     125      877080 :         k = sub( k, 1 );
     126      877080 :         h = shl( 1, k ); /* highest bit of exp2 */
     127      877080 :         out2 = base;
     128      877080 :         move16();
     129      877080 :         out = mult_r( out, pows[k + 1] ); /* we already know that "exp" has the highest bit set to one since we calculated .. */
     130             :         /* .. the effective length of "exp" earlier on, thus we omit the branch for out2 */
     131      877080 :         IF( s_and( exp2, h ) != 0 )
     132             :         {
     133      232233 :             out2 = mult_r( out2, pows[k + 1] ); /* Q15 */
     134             :         }
     135             : 
     136      877080 :         h = shr( h, 1 );
     137     1654453 :         FOR( k = sub( k, 1 ); k >= 0; k-- )
     138             :         {
     139      777373 :             IF( s_and( exp, h ) != 0 )
     140             :             {
     141      284086 :                 out = mult_r( out, pows[k + 1] ); /* Q15 */
     142             :             }
     143             : 
     144      777373 :             IF( s_and( exp2, h ) != 0 )
     145             :             {
     146      444634 :                 out2 = mult_r( out2, pows[k + 1] ); /* Q15 */
     147             :             }
     148             : 
     149      777373 :             h = shr( h, 1 );
     150             :         }
     151             :     }
     152             : 
     153      877080 :     *pout1 = out2; /* Q15 */
     154      877080 :     move16();
     155      877080 :     *pout2 = out; /* Q15 */
     156      877080 :     move16();
     157      877080 : }
     158             : 
     159             : /*------------------------------------------------------------------------
     160             :  * Function: tcx_arith_scale_envelope
     161             :  *
     162             :  * For optimal performance of the arithmetic coder, the envelope shape must
     163             :  * be scaled such that the expected bit-consumption of a signal that
     164             :  * follows the scaled shape coincides with the target bitrate.
     165             :  * This function calculates a first-guess scaling and then uses the bi-section
     166             :  * search to find the optimal scaling.
     167             :  *
     168             :  * We assume that lines follow the Laplacian distribution, whereby the expected
     169             :  * bit-consumption would be log2(2*e*s[k]), where s[k] is the envelope value
     170             :  * for the line in question. However, this theoretical formula assumes that
     171             :  * all lines are encoded with magnitude+sign. Since the sign is unnecessary
     172             :  * for 0-values, that estimate of bit-consumption is biased when s[k] is small.
     173             :  * Analytical solution of the expectation for small s[k] is difficult, whereby
     174             :  * we use the approximation log2(2*e*s[k] + 0.15 + 0.035 / s[k]) which is accurate
     175             :  * on the range 0.08 to 1.0.
     176             :  *
     177             :  * NOTE: This function must be bit-exact on all platforms such that encoder
     178             :  * and decoder remain synchronized.
     179             :  *-------------------------------------------------------------------------*/
     180       31326 : void tcx_arith_scale_envelope(
     181             :     const Word16 L_spec_core,    /* i: number of lines to scale    Q0 */
     182             :     Word16 L_frame,              /* i: number of lines             Q0 */
     183             :     const Word32 env[],          /* i: unscaled envelope           Q16 */
     184             :     Word16 target_bits,          /* i: number of available bits    Q0 */
     185             :     const Word16 low_complexity, /* i: low-complexity flag         Q0 */
     186             :     Word16 s_env[],              /* o: scaled envelope             Q15-e */
     187             :     Word16 *s_env_e              /* o: scaled envelope exponent    Q0 */
     188             : )
     189             : {
     190             :     Word32 ienv[N_MAX_ARI];
     191             :     Word16 scale, iscale, iscale_e, a_e, b, b_e;
     192             :     Word16 lob, hib, adjust;
     193             :     Word16 k, iter, max_iter, lob_bits, hib_bits;
     194             :     Word16 statesi, bits;
     195             :     Word32 mean, a, s, L_tmp;
     196             :     Word16 mean_e, tmp, tmp2;
     197             : 
     198       31326 :     lob_bits = 0;
     199       31326 :     move16();
     200       31326 :     hib_bits = 0;
     201       31326 :     move16();
     202             : 
     203             :     /* Boosting to account for expected spectrum truncation (kMax) */
     204             :     /* target_bits = (int)(target_bits * (1.2f - 0.00045f * target_bits + 0.00000025f * target_bits * target_bits)); */
     205       31326 :     L_tmp = L_shr( Mpy_32_16_1( L_mult0( target_bits, target_bits ), 17180 ), 6 ); /* Q15; 17180 -> 0.00000025f (Q36) */
     206       31326 :     L_tmp = L_sub( L_tmp, L_shr( L_mult0( target_bits, 30199 ), 11 ) );            /* Q15; 30199 -> 0.00045f (Q26) */
     207       31326 :     L_tmp = L_add( L_tmp, 39322 );                                                 /* Q15; 39322 -> 1.2f (Q15) */
     208       31326 :     L_tmp = Mpy_32_16_1( L_tmp, target_bits );                                     /* Q0 */
     209       31326 :     assert( L_tmp < 32768 );
     210       31326 :     target_bits = extract_l( L_tmp );
     211             : 
     212             :     /* Calculate inverse envelope and find initial scale guess based on mean */
     213       31326 :     mean = L_deposit_l( 0 );
     214     6561508 :     FOR( k = 0; k < L_frame; k++ )
     215             :     {
     216             :         /* ienv[k] = 1.0f / env[k];
     217             :         mean += ienv[k]; */
     218             : 
     219     6530182 :         tmp = norm_l( env[k] );
     220     6530182 :         tmp2 = sub( 15, tmp );
     221     6530182 :         tmp = Inv16( round_fx_sat( L_shl( env[k], tmp ) ), &tmp2 ); /* exp(tmp2) */
     222     6530182 :         ienv[k] = L_shl( L_deposit_h( tmp ), sub( tmp2, 15 ) );     /* Q16 */
     223     6530182 :         move32();
     224     6530182 :         mean = L_add( mean, ienv[k] ); /* Q16 */
     225             :     }
     226       31326 :     tmp = norm_s( L_frame );
     227       31326 :     tmp = shl( div_s( 8192, shl( L_frame, tmp ) ), sub( tmp, 7 ) );
     228       31326 :     mean = L_shr( Mpy_32_16_1( mean, tmp ), 6 ); /* Q16 */
     229             : 
     230             :     /* Rate dependent compensation to get closer to the target on average */
     231             :     /* mean = (float)pow(mean, (float)L_frame / (float)target_bits * 0.357f); */
     232       31326 :     tmp = BASOP_Util_Divide1616_Scale( L_frame, target_bits, &tmp2 ); /* exp(tmp2) */
     233       31326 :     tmp = mult_r( tmp, 11698 /*0.357f Q15*/ );
     234       31326 :     mean = BASOP_Util_fPow( mean, 15, L_deposit_h( tmp ), tmp2, &mean_e ); /* exp(mean_e) */
     235             : 
     236             :     /* Find first-guess scaling coefficient "scale" such that if "mean" is the
     237             :      * mean of the envelope, then the mean bit-consumption is approximately
     238             :      *
     239             :      * log2(2*e*mean*scale + 0.15 + 0.035/(mean*scale)) * L_frame = target_bits
     240             :      */
     241             :     /* a = 2*2.71828183f*mean*mean; */
     242       31326 :     tmp = round_fx( mean );                                      /* Q15 - mean_e */
     243       31326 :     a = L_mult( mult_r( tmp, 22268 /*2.71828183f Q13*/ ), tmp ); /* 2 * mean_e + 3 */
     244       31326 :     a_e = add( shl( mean_e, 1 ), 3 );
     245             : 
     246             :     /* b = (0.15f - (float)pow(2.0f, target_bits/(float)L_frame)) * mean; */
     247       31326 :     tmp = BASOP_Util_Divide1616_Scale( target_bits, L_frame, &tmp2 ); /* exp(tmp2) */
     248       31326 :     tmp = round_fx( BASOP_util_Pow2( L_deposit_h( tmp ), tmp2, &tmp2 ) );
     249       31326 :     b_e = BASOP_Util_Add_MantExp( 4915 /*0.15f Q15*/, 0, negate( tmp ), tmp2, &b );
     250       31326 :     b = mult_r( b, round_fx( mean ) ); /* exp(b_e + mean_e) */
     251       31326 :     b_e = add( b_e, mean_e );
     252             : 
     253             :     /* scale = (-b + (float)sqrt(b*b - 4.0f*a*0.035f)) / (2.0f * a); */
     254       31326 :     tmp = round_fx_sat( BASOP_Util_Add_Mant32Exp( L_mult( b, b ), shl( b_e, 1 ), Mpy_32_16_1( a, -4588 /*-4.0f*0.035f Q15*/ ), a_e, &tmp2 ) );
     255             : 
     256       31326 :     IF( tmp <= 0 )
     257             :     {
     258           0 :         tmp = 0;
     259           0 :         move16();
     260           0 :         set16_fx( s_env, 0, L_frame );
     261             :     }
     262             :     ELSE
     263             :     {
     264       31326 :         tmp = Sqrt16( tmp, &tmp2 );
     265             :     }
     266             : 
     267       31326 :     tmp2 = BASOP_Util_Add_MantExp( negate( b ), b_e, tmp, tmp2, &scale ); /* exp(scale) */
     268       31326 :     scale = BASOP_Util_Divide1616_Scale( scale, round_fx( a ), &tmp );
     269       31326 :     scale = shl_sat( scale, sub( sub( add( tmp, tmp2 ), a_e ), 1 ) ); /* Q15 */
     270             : 
     271             :     /* iscale = 1.0f / scale; */
     272       31326 :     iscale_e = 0;
     273       31326 :     move16();
     274       31326 :     iscale = Inv16( s_max( 1, scale ), &iscale_e ); /* exp(isacle_e) */
     275             : 
     276       31326 :     lob = 0;
     277       31326 :     move16();
     278       31326 :     hib = 0;
     279       31326 :     move16();
     280             : 
     281       31326 :     max_iter = 2;
     282       31326 :     move16();
     283       31326 :     if ( low_complexity )
     284             :     {
     285       28778 :         max_iter = 1;
     286       28778 :         move16();
     287             :     }
     288             : 
     289       65200 :     FOR( iter = 0; iter < max_iter; iter++ )
     290             :     {
     291       33874 :         statesi = 0x7FFF; /* 1 in Q15 */
     292       33874 :         move16();
     293       33874 :         bits = 0;
     294       33874 :         move16();
     295             : 
     296     7095416 :         FOR( k = 0; k < L_frame; k++ )
     297             :         {
     298     7061542 :             s = Mpy_32_16_1( ienv[k], scale ); /* Q16 */
     299             : 
     300     7061542 :             IF( LE_32( s, 5243l /*0.08f Q16*/ ) )
     301             :             {
     302             :                 /* If s = 0.08, the expected bit-consumption is log2(1.0224). Below 0.08, the bit-consumption
     303             :                    estimate function becomes inaccurate, so use log2(1.0224) for all values below 0.08. */
     304             :                 /* round(state * 1.0224 * 32768) */
     305       28523 :                 statesi = mult_r( statesi, 16751 /*1.0224 Q14*/ ); /* Q14 */
     306       28523 :                 tmp = norm_s( statesi );
     307       28523 :                 statesi = shl( statesi, tmp );     /* Q15 */
     308       28523 :                 bits = add( bits, sub( 1, tmp ) ); /* Q0 */
     309             :             }
     310     7033019 :             ELSE IF( LE_32( s, 16711680l /*255.0 Q16*/ ) )
     311             :             {
     312             :                 /* a = 5.436564f * s + 0.15f + 0.035f * env[k] * iscale; */
     313     7033019 :                 L_tmp = L_shl( Mpy_32_16_1( s, 22268 /*5.436564f Q12*/ ), 3 );                                           /* Q16 */
     314     7033019 :                 L_tmp = L_add( L_tmp, 9830l /*0.15f Q16*/ );                                                             /* Q16 */
     315     7033019 :                 L_tmp = L_add( L_tmp, L_shl( Mpy_32_16_1( env[k], mult_r( 1147 /*0.035f Q15*/, iscale ) ), iscale_e ) ); /* Q16 */
     316             : 
     317     7033019 :                 tmp = norm_l( L_tmp );
     318     7033019 :                 statesi = mult_r( statesi, round_fx_sat( L_shl( L_tmp, tmp ) ) );
     319     7033019 :                 bits = add( bits, sub( 15, tmp ) );
     320             : 
     321     7033019 :                 tmp = norm_s( statesi );
     322     7033019 :                 statesi = shl( statesi, tmp );
     323     7033019 :                 bits = sub( bits, tmp ); /* Q0 */
     324             :             }
     325             :             ELSE
     326             :             {
     327             :                 /* for large envelope values, s > 255, bit consumption is approx log2(2*e*s)
     328             :                  * further, we use round(log2(x)) = floor(log2(x)+0.5) = floor(log2(x*sqrt(2))) */
     329             :                 /* a = 5.436564f * s; */
     330           0 :                 L_tmp = Mpy_32_16_1( s, 31492 /*5.436564f * 1.4142f Q12*/ ); /* Q13 */
     331           0 :                 bits = add( bits, sub( 17, norm_l( L_tmp ) ) );              /* Q0 */
     332             :             }
     333             :         }
     334             : 
     335       33874 :         IF( LE_16( bits, target_bits ) ) /* Bits leftover => scale is too small */
     336             :         {
     337        3221 :             lob = scale; /* Q0 */
     338        3221 :             move16();
     339        3221 :             lob_bits = bits; /* Q0 */
     340        3221 :             move16();
     341             : 
     342        3221 :             IF( hib > 0 ) /* Bisection search */
     343             :             {
     344        2272 :                 adjust = div_s( sub( hib_bits, target_bits ), sub( hib_bits, lob_bits ) ); /* Q15 */
     345        2272 :                 scale = add( mult_r( sub( lob, hib ), adjust ), hib );
     346             :             }
     347             :             ELSE /* Initial scale adaptation */
     348             :             {
     349             :                 /* adjust = 1.05f * target_bits / (float)bits;
     350             :                    scale *= adjust; */
     351         949 :                 adjust = mult_r( 17203 /*1.05f Q14*/, target_bits );
     352         949 :                 adjust = BASOP_Util_Divide1616_Scale( adjust, bits, &tmp ); /* exp(tmp) */
     353         949 :                 scale = shl( mult_r( scale, adjust ), add( 1, tmp ) );
     354             :             }
     355             :         }
     356             :         ELSE /* Ran out of bits => scale is too large */
     357             :         {
     358       30653 :             hib = scale;
     359       30653 :             move16();
     360       30653 :             hib_bits = bits;
     361       30653 :             move16();
     362             : 
     363       30653 :             IF( lob > 0 ) /* Bisection search */
     364             :             {
     365         114 :                 adjust = div_s( sub( hib_bits, target_bits ), sub( hib_bits, lob_bits ) ); /* Q15 */
     366         114 :                 scale = add( mult_r( sub( lob, hib ), adjust ), hib );
     367             :             }
     368             :             ELSE
     369             :             { /* Initial scale adaptation */
     370       30539 :                 test();
     371       30539 :                 IF( target_bits <= 0 || bits <= 0 ) /* safety check in case of bit errors */
     372             :                 {
     373           0 :                     adjust = 0;
     374           0 :                     move16();
     375           0 :                     set16_fx( s_env, 0, L_frame );
     376             :                 }
     377             :                 ELSE
     378             :                 {
     379       30539 :                     adjust = div_s( mult_r( 31130 /*0.95f Q15*/, target_bits ), bits ); /* Q15 */
     380             :                 }
     381       30539 :                 scale = mult_r( scale, adjust );
     382             :             }
     383             :         }
     384       33874 :         iscale_e = 0;
     385       33874 :         move16();
     386             : 
     387       33874 :         IF( scale == 0 ) /* safety check in case of bit errors */
     388             :         {
     389           0 :             iscale = 0;
     390           0 :             move16();
     391           0 :             set16_fx( s_env, 0, L_frame );
     392             :         }
     393             :         ELSE
     394             :         {
     395       33874 :             iscale = Inv16( scale, &iscale_e );
     396             :         }
     397             :     }
     398       31326 :     L_frame = L_spec_core; /* Q0 */
     399       31326 :     move16();
     400             : 
     401       31326 :     tmp = getScaleFactor32( env, L_frame );
     402       31326 :     *s_env_e = sub( add( 15, iscale_e ), tmp );
     403       31326 :     move16();
     404             :     BASOP_SATURATE_WARNING_OFF_EVS;
     405       31326 :     a = L_shl_sat( 1265000, sub( 15, *s_env_e ) );
     406             :     BASOP_SATURATE_WARNING_ON_EVS;
     407             : 
     408    19719166 :     FOR( k = 0; k < L_frame; k++ )
     409             :     {
     410    19687840 :         L_tmp = Mpy_32_16_1( L_shl( env[k], tmp ), iscale ); /* Q31 - e */
     411    19687840 :         L_tmp = L_min( L_tmp, a );                           /* Q31 - e */
     412    19687840 :         s_env[k] = round_fx( L_tmp );                        /* Q15 - e */
     413    19687840 :         move16();
     414             :     }
     415       31326 : }
     416             : 
     417             : /*------------------------------------------------------------------------
     418             :  * Function: tcx_arith_render_envelope
     419             :  *
     420             :  * Calculate the envelope of the spectrum based on the LPC shape. The
     421             :  * envelope is used in a perceptual domain, whereby the LPC shape has to
     422             :  * be multiplied by the perceptual model.
     423             :  * Operations that are performed on the spectrum, which change the magnitude
     424             :  * expectation of lines, such as low-frequency emphasis, are included in the
     425             :  * envelope shape.
     426             :  * NOTE: This function must be bit-exact on all platforms such that encoder
     427             :  * and decoder remain synchronized.
     428             :  *-------------------------------------------------------------------------*/
     429       17444 : void tcx_arith_render_envelope(
     430             :     const Word16 A_ind[],     /* i: LPC coefficients of signal envelope                         Q12*/
     431             :     const Word16 L_frame,     /* i: number of spectral lines                                            Q0*/
     432             :     const Word16 L_spec,      /* Q0 */
     433             :     const Word16 preemph_fac, /* i: pre-emphasis factor                                                         Q15*/
     434             :     const Word16 gamma_w,     /* i: A_ind -> weighted envelope factor                                Q15*/
     435             :     const Word16 gamma_uw,    /* i: A_ind -> non-weighted envelope factor                    Q14*/
     436             :     Word32 env[]              /* o: shaped signal envelope                                                      Q16*/
     437             : )
     438             : {
     439             :     Word16 k;
     440             :     Word16 tmpA[M + 2];
     441             :     Word16 signal_env[FDNS_NPTS], signal_env_e[FDNS_NPTS];
     442             :     Word16 gainlpc[FDNS_NPTS], gainlpc_e[FDNS_NPTS];
     443             : 
     444             : 
     445             :     /* Compute perceptual LPC envelope, transform it into freq.-domain gains */
     446       17444 :     weight_a_fx( A_ind, tmpA, gamma_w, M );
     447       17444 :     lpc2mdct( tmpA, M, NULL, NULL, gainlpc, gainlpc_e, FDNS_NPTS, 0 );
     448             : 
     449             :     /* Add pre-emphasis tilt to LPC envelope, transform LPC into MDCT gains */
     450       17444 :     E_LPC_a_weight_inv( A_ind, signal_env, gamma_uw, M );
     451       17444 :     E_LPC_a_add_tilt( signal_env, tmpA, preemph_fac, M );
     452       17444 :     lpc2mdct( tmpA, M + 1, signal_env, signal_env_e, NULL, NULL, FDNS_NPTS, 0 );
     453             : 
     454             :     /* Compute weighted signal envelope in perceptual domain */
     455     1133860 :     FOR( k = 0; k < FDNS_NPTS; k++ )
     456             :     {
     457     1116416 :         signal_env[k] = mult_r( signal_env[k], gainlpc[k] ); /* exp(signal_env_e + gainlpc_e)  */
     458     1116416 :         move16();
     459     1116416 :         signal_env_e[k] = add( signal_env_e[k], gainlpc_e[k] );
     460     1116416 :         move16();
     461             :     }
     462             : 
     463             :     /* Adaptive low frequency emphasis */
     464       17444 :     set32_fx( env, 0x10000 /* 1 in Q16 */, L_frame );
     465             : 
     466       17444 :     AdaptLowFreqDeemph( env, 15,
     467             :                         1,
     468             :                         gainlpc, gainlpc_e,
     469             :                         L_frame, NULL );
     470             : 
     471             :     /* Scale from FDNS_NPTS to L_frame and multiply LFE gains */
     472       17444 :     mdct_noiseShaping_interp( env, L_frame, signal_env, signal_env_e );
     473             : 
     474     6455140 :     FOR( k = L_frame; k < L_spec; ++k )
     475             :     {
     476     6437696 :         env[k] = env[k - 1]; /* Q16 */
     477     6437696 :         move32();
     478             :     }
     479       17444 : }
     480             : 
     481             : #define WMC_TOOL_SKIP
     482             : 
     483             : /*-------------------------------------------------------*
     484             :  * expfp_evs()
     485             :  *
     486             :  * Fixed point implementation of exp()
     487             :  *-------------------------------------------------------*/
     488             : 
     489             : /*! r: Q15 */
     490     8549222 : Word16 expfp_evs_fx(
     491             :     const Word16 x,  /* i  : mantissa  Q15-e */
     492             :     const Word16 x_e /* i  : exponent  Q0 */
     493             : )
     494             : {
     495             :     Word16 xi, xf, tmp;
     496             :     Word16 b0, b1, b2, b3;
     497             :     Word32 y, L_tmp;
     498             : 
     499     8549222 :     assert( x <= 0 );
     500             : 
     501     8549222 :     L_tmp = L_negate( L_shl( L_deposit_h( x ), sub( x_e, 15 ) ) ); /* Q16 */
     502             : 
     503             :     /* split into integer and fractional parts */
     504     8549222 :     xi = round_fx( L_tmp );  /* Q0 */
     505     8549222 :     xf = extract_l( L_tmp ); /* Q16 */
     506             : 
     507             :     BASOP_SATURATE_WARNING_OFF;
     508     8549222 :     xf = negate( xf );
     509             :     BASOP_SATURATE_WARNING_ON;
     510             : 
     511             :     /* Fractional part */
     512             :     /* y = 65536
     513             :                     +         xf
     514             :                     +       ((xf*xf) / (2*65536))
     515             :                     +   ((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)
     516             :                     + ((((((((xf*xf) / (2*65536))*xf) / 65536)*65536/3) / 65536)*xf) / (4*65536)); */
     517     8549222 :     y = L_mac0( 65536, xf, 1 );     /* Q16 */
     518     8549222 :     tmp = shr( mult( xf, xf ), 2 ); /* Q15 */
     519     8549222 :     y = L_mac0( y, tmp, 1 );        /* Q16 */
     520     8549222 :     tmp = shr( mult( shr( mult( tmp, xf ), 1 ), 65536 / 3 ), 1 );
     521     8549222 :     y = L_mac0( y, tmp, 1 ); /* Q16 */
     522     8549222 :     tmp = shr( mult( tmp, xf ), 3 );
     523     8549222 :     y = L_mac0( y, tmp, 1 ); /* Q16 */
     524             : 
     525             :     /* Integer part */
     526     8549222 :     b0 = s_and( xi, 1 );
     527     8549222 :     b1 = s_and( xi, 2 );
     528     8549222 :     b2 = s_and( xi, 4 );
     529     8549222 :     b3 = s_and( xi, 8 );
     530             : 
     531     8549222 :     if ( b0 != 0 )
     532     4121490 :         y = Mpy_32_16_1( y, 24109 ); /* exp(-1) in -1Q16 */
     533     8549222 :     if ( b1 != 0 )
     534     3980361 :         y = Mpy_32_16_1( y, 17739 ); /* exp(-2) in -2Q17 */
     535     8549222 :     if ( b2 != 0 )
     536     4956772 :         y = Mpy_32_16_1( y, 19205 ); /* exp(-4) in -5Q20 */
     537     8549222 :     if ( b3 != 0 )
     538      589670 :         y = Mpy_32_16_1( y, 22513 ); /* exp(-8) in -11Q26 */
     539             : 
     540             :     /* scaling: -1*b0 - 2*b1 -5*b2 -11*b3 */
     541     8549222 :     y = L_shr( y, add( add( xi, shr( xi, 2 ) ), shr( b3, 3 ) ) ); /* Q16 */
     542             : 
     543             :     /* zero for xi >= 16 */
     544     8549222 :     if ( shr( xi, 4 ) > 0 )
     545             :     {
     546           0 :         y = L_deposit_l( 0 );
     547           0 :         move16();
     548             :     }
     549             : 
     550     8549222 :     return round_fx( L_shl( y, 15 ) );
     551             : }
     552             : 
     553             : /*-------------------------------------------------------*
     554             :  * powfp_odd2_evs()
     555             :  *
     556             :  * Fixed point implementation of pow(), where base is fixed point (16/16) and exponent a small *odd* integer
     557             :  *-------------------------------------------------------*/
     558             : /*
     559             :  *
     560             :  * Returns: *pout1 = ( (base/65536)^(2*exp - 1) ) * 65536
     561             :  *          *pout2 = ( (base/65536)^(2*exp + 1) ) * 65536
     562             :  *
     563             :  * NOTE: This function must be in sync with ari_decode_14bits_pow_ivas() */
     564             : 
     565             : 
     566             : /*------------------------------------------------------------------------
     567             :  * Function: tcx_arith_render_envelope_flt
     568             :  *
     569             :  * Calculate the envelope of the spectrum based on the LPC shape. The
     570             :  * envelope is used in a perceptual domain, whereby the LPC shape has to
     571             :  * be multiplied by the perceptual model.
     572             :  * Operations that are performed on the spectrum, which change the magnitude
     573             :  * expectation of lines, such as low-frequency emphasis, are included in the
     574             :  * envelope shape.
     575             :  * NOTE: This function must be bit-exact on all platforms such that encoder
     576             :  * and decoder remain synchronized.
     577             :  *-------------------------------------------------------------------------*/
     578             : 
     579       13882 : void tcx_arith_render_envelope_ivas_fx(
     580             :     const Word16 A_ind[],     /* i  : LPC coefficients of signal envelope        Q12*/
     581             :     const Word16 L_frame,     /* i  : number of spectral lines                   Q0*/
     582             :     const Word16 L_spec,      /* i  : length of the coded spectrum               Q0*/
     583             :     const Word16 preemph_fac, /* i  : pre-emphasis factor                        Q15*/
     584             :     const Word16 gamma_w,     /* i  : A_ind -> weighted envelope factor          Q15*/
     585             :     const Word16 gamma_uw,    /* i  : A_ind -> non-weighted envelope factor      Q14*/
     586             :     Word32 env[]              /* o  : shaped signal envelope                     Q16*/
     587             : )
     588             : {
     589             :     Word16 k;
     590             :     Word16 tmpA[M + 2];
     591             :     Word16 signal_env[FDNS_NPTS], signal_env_e[FDNS_NPTS];
     592             :     Word16 gainlpc[FDNS_NPTS], gainlpc_e[FDNS_NPTS];
     593             : 
     594             :     /* Compute perceptual LPC envelope, transform it into freq.-domain gains */
     595       13882 :     basop_weight_a( A_ind, tmpA, gamma_w );
     596       13882 :     basop_lpc2mdct_fx( tmpA, M, NULL, NULL, gainlpc, gainlpc_e );
     597             : 
     598             :     /* Add pre-emphasis tilt to LPC envelope, transform LPC into MDCT gains */
     599       13882 :     basop_weight_a_inv( A_ind, signal_env, gamma_uw );
     600       13882 :     basop_E_LPC_a_add_tilt( signal_env, tmpA, preemph_fac );
     601       13882 :     basop_lpc2mdct_fx( tmpA, M + 1, signal_env, signal_env_e, NULL, NULL );
     602             : 
     603             :     /* Compute weighted signal envelope in perceptual domain */
     604      902330 :     FOR( k = 0; k < FDNS_NPTS; k++ )
     605             :     {
     606      888448 :         signal_env[k] = mult_r( signal_env[k], gainlpc[k] ); /* exp(signal_env_e + gainlpc_e)  */
     607      888448 :         move16();
     608      888448 :         signal_env_e[k] = add( signal_env_e[k], gainlpc_e[k] );
     609      888448 :         move16();
     610             :     }
     611             : 
     612             :     /* Adaptive low frequency emphasis */
     613     3649274 :     FOR( k = 0; k < L_frame; k++ )
     614             :     {
     615     3635392 :         env[k] = 0x10000; /* 1 in Q16 */
     616     3635392 :         move32();
     617             :     }
     618             : 
     619       13882 :     basop_PsychAdaptLowFreqDeemph_fx( env, gainlpc, gainlpc_e, NULL );
     620             : 
     621             :     /* Scale from FDNS_NPTS to L_frame and multiply LFE gains */
     622       13882 :     basop_mdct_noiseShaping_interp_fx( env, L_frame, signal_env, signal_env_e );
     623             : 
     624     5057370 :     FOR( k = L_frame; k < L_spec; ++k )
     625             :     {
     626     5043488 :         env[k] = env[k - 1]; /* Q16 */
     627     5043488 :         move32();
     628             :     }
     629             : 
     630       13882 :     return;
     631             : }
     632             : 
     633             : #undef WMC_TOOL_SKIP

Generated by: LCOV version 1.14