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

Generated by: LCOV version 1.14