LCOV - code coverage report
Current view: top level - lib_dec - dec_higher_acelp_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e95243e9e67ddeb69dddf129509de1b3d95b402e Lines: 122 123 99.2 %
Date: 2025-09-15 02:22:33 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : #include <stdint.h>
       5             : #include "options.h" /* Compilation switches                   */
       6             : #include <math.h>
       7             : #include "cnst.h"    /* Common constants                       */
       8             : #include "prot_fx.h" /* Function prototypes                    */
       9             : #include "rom_com.h" /* Static table prototypes                */
      10             : 
      11             : /*-----------------------------------------------------------------*
      12             :  * transf_cdbk_dec()
      13             :  * Transform domain contribution decoding
      14             :  *-----------------------------------------------------------------*/
      15       62685 : void transf_cdbk_dec_fx(
      16             :     Decoder_State *st_fx,         /* i/o: decoder state structure */
      17             :     const Word16 harm_flag_acelp, /* i  : harmonic flag for higher rates ACELP            */
      18             :     const Word16 i_subfr,         /* i  : subframe index                                  */
      19             :     const Word16 Es_pred,         /* i  : predicited scaled innovation energy (Q8)        */
      20             :     const Word32 gain_code,       /* i  : innovative excitation gain (Q16)                */
      21             :     Word16 *gain_preQ,            /* o  : prequantizer excitation gain (Q2)               */
      22             :     Word32 *norm_gain_preQ,       /* o  : normalized prequantizer excitation gain (Q16)   */
      23             :     Word16 code_preQ[],           /* o  : prequantizer excitation (Q8)                    */
      24             :     Word16 *unbits                /* o  : number of AVQ unused bits                       */
      25             : )
      26             : {
      27             :     Word16 i, index, nBits;
      28             :     Word16 nq[L_SUBFR / WIDTH_BAND];
      29             :     Word16 gain16, exp16, tmp16;
      30             :     Word32 L_tmp;
      31             :     Word32 dct_code32[L_SUBFR];
      32             :     Word16 qdct;
      33             :     Word16 avq_bit_sFlag;
      34             :     Word16 trgtSvPos;
      35             :     Word16 Nsv;
      36       62685 :     Nsv = 8;
      37       62685 :     move16();
      38             : 
      39       62685 :     avq_bit_sFlag = 0;
      40       62685 :     move16();
      41       62685 :     if ( ( st_fx->element_mode > EVS_MONO ) )
      42             :     {
      43       59315 :         move16();
      44       59315 :         avq_bit_sFlag = 1;
      45             :     }
      46             : 
      47             :     /*--------------------------------------------------------------*
      48             :      * Set bit-allocation
      49             :      *--------------------------------------------------------------*/
      50             : 
      51       62685 :     nBits = st_fx->acelp_cfg.AVQ_cdk_bits[( i_subfr >> 6 )];
      52       62685 :     move16();
      53             : 
      54             :     /* increase # of AVQ allocated bits by unused bits from the previous subframe */
      55       62685 :     nBits = add( nBits, *unbits );
      56             : 
      57             :     /*--------------------------------------------------------------*
      58             :      * Dequantize prequantizer excitation gain
      59             :      *--------------------------------------------------------------*/
      60             : 
      61       62685 :     index = (Word16) get_next_indice_fx( st_fx, G_AVQ_BITS );
      62             : 
      63       62685 :     IF( ( st_fx->coder_type == INACTIVE ) )
      64             :     {
      65        9700 :         IF( GT_32( st_fx->core_brate, 56000 ) )
      66             :         {
      67           0 :             gain16 = usdequant_fx( index, G_AVQ_MIN_INACT_64k_Q12, G_AVQ_DELTA_INACT_64k_Q12 >> 1 ); // Q12
      68             :         }
      69        9700 :         ELSE IF( GT_32( st_fx->core_brate, 42000 ) )
      70             :         {
      71        1140 :             gain16 = usdequant_fx( index, G_AVQ_MIN_INACT_48k_Q12, G_AVQ_DELTA_INACT_48k_Q12 >> 1 ); // Q12
      72             :         }
      73             :         ELSE
      74             :         {
      75        8560 :             gain16 = usdequant_fx( index, G_AVQ_MIN_INACT_Q12, G_AVQ_DELTA_INACT_Q12 >> 1 ); // Q12
      76             :         }
      77             : 
      78        9700 :         L_tmp = Mult_32_16( gain_code, gain16 ); /* Q16 * Q12 - 15 -> Q13*/
      79        9700 :         L_tmp = L_shl_sat( L_tmp, 5 );           /* Q13 -> Q18*/
      80        9700 :         *gain_preQ = round_fx_sat( L_tmp );      /* Q2*/
      81        9700 :         move16();
      82             :     }
      83             :     ELSE
      84             :     {
      85       52985 :         test();
      86       52985 :         IF( GT_32( st_fx->core_brate, ACELP_24k40 ) && LE_32( st_fx->core_brate, 42000 ) )
      87             :         {
      88       44580 :             gain16 = gain_dequant_fx( index, G_AVQ_MIN_32kbps_Q15, G_AVQ_MAX_Q0, G_AVQ_BITS, &exp16 ); // Q0
      89             :         }
      90             :         ELSE
      91             :         {
      92        8405 :             gain16 = gain_dequant_fx( index, G_AVQ_MIN_Q15, G_AVQ_MAX_Q0, G_AVQ_BITS, &exp16 ); // Q0
      93             :         }
      94             : 
      95       52985 :         IF( Es_pred < 0 )
      96             :         {
      97         175 :             tmp16 = shr( negate( Es_pred ), 2 ); // Q8
      98         175 :             L_tmp = L_mult( gain16, tmp16 );     /* Q0*Q8 -> Q9*/
      99             :         }
     100             :         ELSE
     101             :         {
     102       52810 :             L_tmp = L_mult( gain16, Es_pred ); /* Q0*Q8 -> Q9*/
     103             :         }
     104       52985 :         L_tmp = L_shl( L_tmp, add( exp16, 9 ) ); /* Q18*/
     105       52985 :         *gain_preQ = round_fx( L_tmp );          /* Q2*/
     106       52985 :         move16();
     107             :     }
     108             : 
     109       62685 :     trgtSvPos = sub( Nsv, 1 );
     110       62685 :     test();
     111       62685 :     test();
     112       62685 :     test();
     113       62685 :     test();
     114       62685 :     test();
     115       62685 :     IF( avq_bit_sFlag && GT_16( nBits, 85 ) && !harm_flag_acelp && ( EQ_16( st_fx->coder_type, GENERIC ) || EQ_16( st_fx->coder_type, TRANSITION ) || ( st_fx->coder_type == INACTIVE ) ) )
     116             :     {
     117       16552 :         trgtSvPos = 2;
     118       16552 :         avq_bit_sFlag = 2;
     119       16552 :         move16();
     120       16552 :         move16();
     121             :     }
     122             : 
     123             :     /*--------------------------------------------------------------*
     124             :      * Demultiplex and decode subvectors from bit-stream
     125             :      *--------------------------------------------------------------*/
     126             : 
     127       62685 :     AVQ_demuxdec_fx( st_fx, code_preQ, &nBits, 8, nq, avq_bit_sFlag, trgtSvPos );
     128             :     Word16 q_Code_preQ;
     129       62685 :     IF( ( st_fx->element_mode == EVS_MONO ) )
     130             :     {
     131        3370 :         q_Code_preQ = Q_AVQ_OUT_DEC;
     132        3370 :         move16();
     133             :     }
     134             :     ELSE
     135             :     {
     136       59315 :         q_Code_preQ = Q_AVQ_OUT;
     137       59315 :         move16();
     138             :     }
     139     4074525 :     FOR( i = 0; i < L_SUBFR; i++ )
     140             :     {
     141     4011840 :         code_preQ[i] = shl_sat( code_preQ[i], q_Code_preQ );
     142     4011840 :         move16(); /* code_preQ in Q6*/
     143             :     }
     144             : 
     145             :     /* save # of AVQ unused bits for next subframe */
     146       62685 :     *unbits = nBits;
     147       62685 :     move16();
     148             : 
     149             :     /*--------------------------------------------------------------*
     150             :      * iDCT transform
     151             :      *--------------------------------------------------------------*/
     152             : 
     153       62685 :     test();
     154       62685 :     test();
     155       62685 :     IF( ( st_fx->coder_type == INACTIVE ) || GT_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) || harm_flag_acelp )
     156             :     {
     157       27060 :         qdct = 0;
     158       27060 :         move16();
     159       27060 :         edct2_fx( L_SUBFR, 1, code_preQ, dct_code32, &qdct, ip_edct2_64, w_edct2_64_fx );
     160             :         /*qdct = sub(q_Code_preQ,qdct+q_Code_preQ);*/
     161       27060 :         qdct = negate( qdct );
     162       27060 :         Copy_Scale_sig_32_16( dct_code32, code_preQ, L_SUBFR, qdct ); /* Output in q_Code_preQ */
     163             :         /*qdct = q_Code_preQ;*/
     164             :     }
     165             :     /*--------------------------------------------------------------*
     166             :      * Preemphasise
     167             :      *--------------------------------------------------------------*/
     168             :     /* in extreme cases at subframe boundaries, lower the preemphasis memory to avoid a saturation */
     169       62685 :     test();
     170       62685 :     IF( ( nq[7] != 0 ) && ( GT_16( sub( st_fx->last_nq_preQ, nq[0] ), 7 ) ) )
     171             :     {
     172             :         /* *mem_preemp /= 16; */
     173           1 :         st_fx->mem_preemp_preQ_fx = shr( st_fx->mem_preemp_preQ_fx, 4 );
     174           1 :         move16();
     175             :     }
     176       62685 :     st_fx->last_nq_preQ = nq[7];
     177       62685 :     move16();
     178             : 
     179             :     /* TD pre-quantizer: in extreme cases at subframe boundaries, lower the preemphasis memory to avoid a saturation */
     180       62685 :     test();
     181       62685 :     test();
     182       62685 :     test();
     183       62685 :     test();
     184       62685 :     test();
     185       62685 :     IF( st_fx->element_mode > EVS_MONO && ( st_fx->coder_type != INACTIVE ) && GE_32( st_fx->core_brate, MIN_BRATE_AVQ_EXC ) && LE_32( st_fx->core_brate, MAX_BRATE_AVQ_EXC_TD ) && !harm_flag_acelp && code_preQ[0] != 0 )
     186             :     {
     187             :         // PMT("Fixed point taking accound of the scaling needs to be done here ")
     188       16156 :         IF( GT_16( abs_s( st_fx->last_code_preq ), shl_sat( abs_s( code_preQ[0] ), 4 ) ) )
     189             :         {
     190           1 :             st_fx->mem_preemp_preQ_fx = shr( st_fx->mem_preemp_preQ_fx, 4 );
     191           1 :             move16();
     192             :         }
     193       16155 :         ELSE IF( GT_16( abs_s( ( st_fx->last_code_preq ) ), shl_sat( abs_s( code_preQ[0] ), 3 ) ) )
     194             :         {
     195           2 :             st_fx->mem_preemp_preQ_fx = shr( st_fx->mem_preemp_preQ_fx, 3 );
     196           2 :             move16();
     197             :         }
     198             :     }
     199             : 
     200       62685 :     st_fx->last_code_preq = code_preQ[L_SUBFR - 1]; // q_Code_preQ
     201       62685 :     move16();
     202       62685 :     PREEMPH_FX( code_preQ, FAC_PRE_AVQ_FX, L_SUBFR, &st_fx->mem_preemp_preQ_fx );
     203             :     /*--------------------------------------------------------------*
     204             :      * Compute normalized prequantizer excitation gain for FEC
     205             :      *
     206             :      * somewhat attenuate pre-quantizer normalized gain for FEC
     207             :      *--------------------------------------------------------------*/
     208             : 
     209             :     /*Ecode = (sum2_f( code_preQ, L_SUBFR ) + 0.01f) / L_SUBFR;*/
     210             :     /*norm_gain_preQ = 0.8f * (*gain_preQ) * (float)sqrt( Ecode );*/
     211             : 
     212       62685 :     L_tmp = Dot_product12( code_preQ, code_preQ, L_SUBFR, &exp16 );
     213             : 
     214       62685 :     IF( EQ_32( L_tmp, L_shl( 1, sub( 30, exp16 ) ) ) )
     215             :     {
     216             :         /* pre-quantizer contribution is zero */
     217         737 :         *norm_gain_preQ = 1;
     218         737 :         move16();
     219             :     }
     220             :     ELSE
     221             :     {
     222       61948 :         exp16 = sub( exp16, add( imult1616( q_Code_preQ, 2 ), 6 ) ); /* exp: (code_preQ in q_Code_preQ), -6 (/L_SUBFR) */
     223       61948 :         L_tmp = Isqrt_lc( L_tmp, &exp16 );
     224       61948 :         tmp16 = extract_h( L_tmp );
     225       61948 :         exp16 = sub( sub( 15, 10 ), exp16 ); /* tmp16 in Q10+exp16*/
     226       61948 :         tmp16 = div_s( 16384, tmp16 );       /* Q15+Q14-(Q10+Qexp16) = Q19-exp16*/
     227             : 
     228       61948 :         L_tmp = L_mult( *gain_preQ, tmp16 ); /* Q2+Q19-exp16+1 -> Q22-exp16    */
     229       61948 :         L_tmp = Mult_32_16( L_tmp, 26214 );  /* Q22-Qexp16+Q15+1-16 -> Q22-exp16*/
     230       61948 :         *norm_gain_preQ = L_shr( L_tmp, sub( 6, exp16 ) );
     231       61948 :         move32(); /* Q22-exp16 -> Q16*/
     232             :     }
     233             : 
     234             : 
     235       62685 :     st_fx->use_acelp_preq = 1;
     236       62685 :     move16();
     237             : 
     238       62685 :     return;
     239             : }
     240             : 
     241             : /*==========================================================================*/
     242             : /* FUNCTION      : Word16 gain_dequant_fx ()                                                            */
     243             : /*--------------------------------------------------------------------------*/
     244             : /* PURPOSE       :                                                                                                                      */
     245             : /*      * Returns decoded gain quantized between the specified                                  */
     246             : /*  * range using the specified number of levels.                                                       */
     247             : /*--------------------------------------------------------------------------*/
     248             : /* INPUT ARGUMENTS  :                                                                                                           */
     249             : /*      Word16 index    i: quantization index                                                           */
     250             : /*      Word16 min_val  i : value of lower limit                                                                */
     251             : /*      Word16 max_val  i : value of upper limit                                                                */
     252             : /*      Word16 bits             i : number of bits to dequantize                                                */
     253             : /*--------------------------------------------------------------------------*/
     254             : /* OUTPUT ARGUMENTS :                                                                                                           */
     255             : /* _Word16 *expg        o :                                                                                                             */
     256             : /*--------------------------------------------------------------------------*/
     257             : /* INPUT/OUTPUT ARGUMENTS :                                                                                                     */
     258             : /*--------------------------------------------------------------------------*/
     259             : /* RETURN ARGUMENTS :                                                                                                           */
     260             : /*                                       Word16 gain                                                                    Q0              */
     261             : /*--------------------------------------------------------------------------*/
     262             : /* CALLED FROM :                                                                                                                    */
     263             : /*==========================================================================*/
     264       98101 : Word16 gain_dequant_fx(                       /* o: decoded gain */
     265             :                         Word16 index,         /* i: quantization index */
     266             :                         const Word16 min_val, /* i: value of lower limit */
     267             :                         const Word16 max_val, /* i: value of upper limit */
     268             :                         const Word16 bits,    /* i: number of bits to dequantize */
     269             :                         Word16 *expg )
     270             : {
     271             :     Word16 gain, c_min, c_max;
     272             :     Word16 levels;
     273             :     Word16 e_tmp, f_tmp;
     274             :     Word16 tmp, frac;
     275             :     Word32 L_tmp;
     276       98101 :     levels = shl( 1, bits );
     277             : 
     278             :     /*c_min = (float)log10(min);
     279             :     c_mult = (float) ((levels-1)/(log10(max)-c_min));
     280             :     gain = (float)pow( 10.0, (((float)index)/c_mult) + c_min );*/
     281             : 
     282             :     /*
     283             :     y =(index/cmult) + c_min
     284             :       = index/((levels-1)/(log10(max_val)-log10(min))) + log10(min)
     285             :       = (index*(log10(max_val)-log10(min)))/(levels-1) + log10(min)
     286             :       = x*log10(max_val) + (1-x)*log10(min)
     287             :       where x = index/(levels-1)
     288             : 
     289             :     gain = pow(10.0,y)
     290             :          = pow(2,3.321928*y)
     291             :     */
     292             : 
     293       98101 :     tmp = div_s( 1, sub( levels, 1 ) );                  /*Q15*/
     294       98101 :     tmp = extract_l( L_shr( L_mult( index, tmp ), 1 ) ); /*Q15*/
     295             : 
     296       98101 :     e_tmp = norm_l( max_val );
     297       98101 :     f_tmp = Log2_norm_lc( L_shl( max_val, e_tmp ) );
     298       98101 :     e_tmp = sub( 30, e_tmp );                          /*Q(max_val)=0*/
     299       98101 :     L_tmp = Mpy_32_16( e_tmp, f_tmp, 9864 ); /* Q16 */ /*log10(2) in Q15*/
     300       98101 :     c_max = round_fx( L_shl( L_tmp, 14 ) );            /* Q14 */
     301             : 
     302       98101 :     e_tmp = norm_l( min_val );
     303       98101 :     f_tmp = Log2_norm_lc( L_shl( min_val, e_tmp ) );
     304       98101 :     e_tmp = sub( sub( 30, e_tmp ), 15 );               /*Q(min_val)=15*/
     305       98101 :     L_tmp = Mpy_32_16( e_tmp, f_tmp, 9864 ); /* Q16 */ /*log10(2) in Q15*/
     306       98101 :     c_min = round_fx( L_shl( L_tmp, 14 ) );            /* Q14 */
     307             : 
     308       98101 :     L_tmp = L_mult( tmp, c_max );                     /*Q30*/
     309       98101 :     L_tmp = L_mac( L_tmp, sub( 32767, tmp ), c_min ); /*Q30*/
     310             : 
     311       98101 :     L_tmp = Mult_32_16( L_tmp, 27213 ); /*Q28, 3.321928 in Q13*/
     312       98101 :     L_tmp = L_shr( L_tmp, 12 );         /*Q28->Q16*/
     313             : 
     314       98101 :     frac = L_Extract_lc( L_tmp, expg ); /* Extract exponent of gcode0 */
     315             : 
     316       98101 :     gain = extract_l( Pow2( 14, frac ) ); /* Put 14 as exponent so that */
     317             :     /* output of Pow2() will be: */
     318             :     /* 16384 < Pow2() <= 32767 */
     319       98101 :     *expg = sub( *expg, 14 );
     320       98101 :     move16();
     321             : 
     322       98101 :     return ( gain );
     323             : }

Generated by: LCOV version 1.14