LCOV - code coverage report
Current view: top level - lib_dec - arith_coder_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 60 180 33.3 %
Date: 2025-05-03 01:55:50 Functions: 2 4 50.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <assert.h>
       6             : #include <stdint.h>
       7             : #include "options.h"
       8             : #include "cnst.h"
       9             : #include "rom_com.h"
      10             : #include "prot_fx.h"
      11             : #include "basop_util.h"
      12             : #include "basop_proto_func.h"
      13             : 
      14             : /* Returns: number of bits consumed */
      15           0 : static Word16 tcx_arith_decode_fx(
      16             :     Word16 L_frame,          /* i: number of spectral lines      Q0 */
      17             :     const Word16 envelope[], /* i: scaled envelope               Q15-e */
      18             :     Word16 envelope_e,       /* i: scaled envelope exponent      Q0 */
      19             :     Word16 target_bits,      /* i: target bit budget             Q0 */
      20             :     Word16 prm[],            /* i: bit-stream                    Q0 */
      21             :     Word32 q_spectrum[],     /* o: scalar quantized spectrum     Q31-e */
      22             :     Word16 *q_spectrum_e,    /* o: spectrum exponent             Q0 */
      23             :     Word16 *nf_seed          /* o: noise filling seed            Q0 */
      24             : )
      25             : {
      26             :     Word16 bp, k, q, s;
      27             :     Tastat as;
      28             :     Word16 exp_k;
      29             :     Word16 tmp;
      30             :     Word32 L_tmp, Q;
      31             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
      32           0 :     Flag Overflow = 0;
      33           0 :     move32();
      34           0 :     Flag Carry = 0;
      35           0 :     move32();
      36             : #endif
      37             : 
      38             : 
      39           0 :     bp = ari_start_decoding_14bits_prm_fx( prm, 0, &as );
      40             : 
      41           0 :     tmp = sub( envelope_e, 1 + 15 );
      42           0 :     L_tmp = L_deposit_l( 0 );
      43           0 :     FOR( k = 0; k < L_frame; k++ )
      44             :     {
      45           0 :         IF( envelope[k] == 0 ) /* safety check in case of bit errors */
      46             :         {
      47           0 :             set32_fx( q_spectrum, 0, L_frame );
      48           0 :             return -1;
      49             :         }
      50             :         ELSE
      51             :         {
      52           0 :             exp_k = round_fx( expfp( envelope[k], tmp ) );
      53             :         }
      54             :         /* decode line magnitude */
      55           0 :         bp = ari_decode_14bits_pow_fx( prm, bp, target_bits, &q, &as, exp_k );
      56             : 
      57           0 :         IF( q == 0 )
      58             :         {
      59           0 :             q_spectrum[k] = L_deposit_l( 0 );
      60           0 :             move32();
      61             :         }
      62           0 :         IF( q != 0 ) /* line is non-zero, decode sign */
      63             :         {
      64           0 :             bp = ari_decode_14bits_sign_fx( prm, bp, target_bits, &s, &as );
      65             : 
      66           0 :             L_tmp = L_macNs_co( L_tmp, q, k, &Carry, &Overflow );
      67             : 
      68           0 :             Q = L_mult( q, negate( shl( 1, sub( 30, SPEC_EXP_DEC ) ) ) );
      69           0 :             IF( EQ_16( s, 0 ) )
      70           0 :             Q = L_mult( q, shl( 1, sub( 30, SPEC_EXP_DEC ) ) );
      71           0 :             q_spectrum[k] = Q;
      72           0 :             move32();
      73             :         }
      74             : 
      75           0 :         IF( ari_decode_overflow_fx( &as ) )
      76             :         {
      77           0 :             if ( LT_16( bp, target_bits ) ) /* safety check in case of bit errors */
      78             :             {
      79           0 :                 bp = -1;
      80           0 :                 move16();
      81             :             }
      82           0 :             BREAK; /* no bits left, so exit loop */
      83             :         }
      84             :     }
      85           0 :     *q_spectrum_e = SPEC_EXP_DEC;
      86           0 :     move16();
      87             : 
      88           0 :     set32_fx( q_spectrum + k, 0, sub( L_frame, k ) );
      89             : 
      90             :     /* noise filling seed */
      91           0 :     *nf_seed = extract_l( L_tmp );
      92           0 :     move16();
      93             : 
      94           0 :     return bp;
      95             : }
      96             : 
      97           0 : void tcx_arith_decode_envelope_fx(
      98             :     Word32 q_spectrum[],  /* o: quantised MDCT coefficients     Q31-e */
      99             :     Word16 *q_spectrum_e, /* o: MDCT exponent                   Q0 */
     100             :     Word16 L_frame,       /* i: frame or MDCT length            Q0 */
     101             :     Word16 L_spec,        /* i: length w/o BW limitation        Q0 */
     102             :     Decoder_State *st,
     103             :     const Word16 A_ind[],   /* i: quantised LPC coefficients      Q12 */
     104             :     Word16 target_bits,     /* i: number of available bits        Q0 */
     105             :     Word16 prm[],           /* i: bitstream parameters            Q0 */
     106             :     Word8 use_hm,           /* i: use HM in current frame?        */
     107             :     Word16 prm_hm[],        /* i: HM parameter area               Q0 */
     108             :     Word16 tcxltp_pitch,    /* i: TCX LTP pitch in FD, -1 if n/a  Q0*/
     109             :     Word16 *arith_bits,     /* o: bits used for ari. coding       Q0 */
     110             :     Word16 *signaling_bits, /* o: bits used for signaling         Q0 */
     111             :     Word16 *nf_seed         /* o: noise filling seed              Q0 */
     112             :     ,
     113             :     Word16 low_complexity /* i: low-complexity flag           Q0 */
     114             : )
     115             : {
     116             :     Word32 env[N_MAX_ARI]; /* unscaled envelope */
     117             :     Word16 *envelope;      /* scaled envelope */
     118             :     Word16 envelope_e;
     119             :     Word16 L_spec_core;
     120             :     TCX_CONFIG_HANDLE tcx_cfg;
     121             :     Word16 gamma_w, gamma_uw;
     122             :     Word16 hm_bits;
     123           0 :     tcx_cfg = st->hTcxCfg;
     124             : 
     125           0 :     test();
     126           0 :     test();
     127           0 :     test();
     128           0 :     test();
     129           0 :     test();
     130           0 :     test();
     131           0 :     test();
     132           0 :     IF( GT_16( L_spec, N_MAX_ARI ) || ( EQ_16( st->element_mode, EVS_MONO ) && GT_16( target_bits, ( ACELP_13k20 / FRAMES_PER_SEC ) ) ) ||
     133             :         ( EQ_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE / FRAMES_PER_SEC ) ) ) ) ||
     134             :         ( GT_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE_CPE / FRAMES_PER_SEC ) ) ) ) ||
     135             :         ( target_bits <= 0 ) )
     136             :     {
     137             :         /* this could happen in case of bit errors */
     138           0 :         st->BER_detect = 1;
     139           0 :         move16();
     140           0 :         L_spec = N_MAX_ARI;
     141           0 :         move16();
     142           0 :         *signaling_bits = 0;
     143           0 :         move16();
     144           0 :         *arith_bits = 0;
     145           0 :         move16();
     146           0 :         set32_fx( q_spectrum, 0, L_frame );
     147             : 
     148           0 :         return;
     149             :     }
     150             : 
     151             : 
     152           0 :     *signaling_bits = 0;
     153           0 :     move16();
     154           0 :     assert( st->hTcxDec->enableTcxLpc );
     155             : 
     156           0 :     gamma_w = 32767 /*1.0f Q15*/;
     157           0 :     move16();
     158           0 :     gamma_uw = st->inv_gamma;
     159           0 :     move16();
     160             : 
     161           0 :     tcx_arith_render_envelope( A_ind, L_frame, L_spec, tcx_cfg->preemph_fac, gamma_w, gamma_uw, env );
     162             : 
     163           0 :     IF( use_hm )
     164             :     {
     165           0 :         IF( prm_hm[0] )
     166             :         {
     167           0 :             tcx_hm_decode( L_spec, env, target_bits, tcx_cfg->coder_type, prm_hm, tcxltp_pitch, &hm_bits );
     168             : 
     169           0 :             IF( hm_bits < 0 )
     170             :             {
     171           0 :                 st->BER_detect = 1;
     172           0 :                 move16();
     173           0 :                 *signaling_bits = 0;
     174           0 :                 move16();
     175           0 :                 *arith_bits = 0;
     176           0 :                 move16();
     177           0 :                 set32_fx( q_spectrum, 0, L_frame );
     178           0 :                 return;
     179             :             }
     180             :         }
     181             :         ELSE
     182             :         {
     183           0 :             hm_bits = 1;
     184           0 :             move16();
     185             :         }
     186           0 :         *signaling_bits = add( *signaling_bits, hm_bits );
     187           0 :         move16();
     188             :     }
     189             :     ELSE
     190             :     {
     191           0 :         prm_hm[0] = 0; /* just to be sure */
     192           0 :         move16();
     193             :     }
     194             : 
     195           0 :     L_spec_core = L_spec;
     196           0 :     move16();
     197           0 :     IF( st->igf )
     198             :     {
     199           0 :         L_spec_core = s_min( L_spec_core, st->hIGFDec->infoIGFStartLine );
     200             :     }
     201           0 :     envelope = (Word16 *) env;
     202             : 
     203           0 :     tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
     204             : 
     205           0 :     *arith_bits = tcx_arith_decode_fx( L_spec, envelope, envelope_e, target_bits, prm, q_spectrum, q_spectrum_e, nf_seed );
     206           0 :     move16();
     207             : 
     208             :     /* safety check in case of bit errors */
     209           0 :     IF( *arith_bits < 0 )
     210             :     {
     211           0 :         st->BER_detect = 1;
     212           0 :         move16();
     213           0 :         set32_fx( q_spectrum, 0, L_frame );
     214             :     }
     215             : 
     216           0 :     set32_fx( q_spectrum + L_spec, 0, sub( L_frame, L_spec ) );
     217             : }
     218             : 
     219             : /*-------------------------------------------------------*
     220             :  * tcx_arith_decode()
     221             :  *
     222             :  *
     223             :  *-------------------------------------------------------*/
     224             : 
     225             : /*! r: number of bits consumed */
     226             : 
     227       13989 : static Word16 tcx_arith_decode_ivas_fx(
     228             :     const Word16 L_frame,     /* i  : number of spectral lines               */
     229             :     const Word16 envelope[],  /* i  : scaled envelope (Q15-envelope_e)                */
     230             :     Word16 envelope_e,        /* i  : scaled envelope exponent (Q0)          */
     231             :     const Word16 target_bits, /* i  : target bit budget                      */
     232             :     Word16 prm[],             /* i  : bitstream parameters                   */
     233             :     Word32 q_spectrum[],      /* o  : scalar quantized spectrum (Q31-q_spectrum_e) */
     234             :     Word16 *q_spectrum_e      /* o  : spectrum exponent                      */
     235             : )
     236             : {
     237             :     Word16 bp, k, q;
     238             :     Word16 s;
     239             :     Tastat as;
     240             :     UWord16 exp_k;
     241             :     Word16 tmp;
     242             : 
     243       13989 :     bp = ari_start_decoding_14bits_prm_ivas_fx( prm, 0, &as );
     244             : 
     245       13989 :     tmp = sub( envelope_e, 1 );
     246             : 
     247     8603693 :     FOR( k = 0; k < L_frame; k++ )
     248             :     {
     249     8590021 :         IF( EQ_16( envelope[k], 0 ) ) /* safety check in case of bit errors */
     250             :         {
     251           0 :             set32_fx( q_spectrum, 0, L_frame );
     252           0 :             return -1;
     253             :         }
     254             :         ELSE
     255             :         {
     256     8590021 :             exp_k = expfp_evs_fx( negate( envelope[k] ), tmp );
     257             :         }
     258             : 
     259             :         /* decode line magnitude */
     260     8590021 :         bp = ari_decode_14bits_pow_ivas( prm, bp, target_bits, &q, &as, exp_k );
     261             : 
     262     8590021 :         IF( q )
     263             :         {
     264             :             /* line is non-zero, decode sign */
     265      411553 :             bp = ari_decode_14bits_sign_ivas( prm, bp, target_bits, &s, &as );
     266      411553 :             q_spectrum[k] = L_mult( q, sub( 3, shl( s, 1 ) ) );
     267      411553 :             move32();
     268      411553 :             q_spectrum[k] = L_shl( q_spectrum[k], 30 - SPEC_EXP_DEC ); // Q(31-20)
     269      411553 :             move32();
     270             :         }
     271             :         ELSE
     272             :         {
     273             :             /* line is zero, no sign needed */
     274     8178468 :             q_spectrum[k] = 0;
     275     8178468 :             move32();
     276             :         }
     277             : 
     278     8590021 :         IF( LE_32( as.high, as.low ) )
     279             :         {
     280         317 :             if ( LT_16( bp, target_bits ) ) /* safety check in case of bit errors */
     281             :             {
     282           0 :                 bp = -1;
     283           0 :                 move16();
     284             :             }
     285         317 :             BREAK; /* no bits left, so exit loop */
     286             :         }
     287             :     }
     288       13989 :     *q_spectrum_e = SPEC_EXP_DEC;
     289       13989 :     move16();
     290             : 
     291       13989 :     set32_fx( q_spectrum + k, 0, sub( L_frame, k ) );
     292             : 
     293       13989 :     return bp;
     294             : }
     295             : 
     296             : /*-------------------------------------------------------*
     297             :  * tcx_arith_decode_envelope()
     298             :  *
     299             :  *
     300             :  *-------------------------------------------------------*/
     301             : 
     302       13989 : void tcx_arith_decode_envelope_ivas_fx(
     303             :     Decoder_State *st,          /* i/o: coder state                   */
     304             :     Word32 q_spectrum[],        /* o  : quantised MDCT coefficients Q(31-q_spectrum_e)  */
     305             :     Word16 *q_spectrum_e,       /* o  : MDCT exponent                 */
     306             :     const Word16 L_frame,       /* i  : frame or MDCT length          */
     307             :     Word16 L_spec,              /* i  : length w/o BW limitation      */
     308             :     const Word16 A_ind[],       /* i  : quantised LPC coefficients    */
     309             :     const Word16 target_bits,   /* i  : number of available bits      */
     310             :     Word16 prm[],               /* i  : bitstream parameters          */
     311             :     const Word16 use_hm,        /* i  : use HM in current frame?      */
     312             :     const Word16 prm_hm[],      /* i  : HM parameter area             */
     313             :     Word16 tcxltp_pitch,        /* i  : TCX LTP pitch in FD, -1 if n/a*/
     314             :     Word16 *arith_bits,         /* o  : bits used for ari. coding     */
     315             :     Word16 *signaling_bits,     /* o  : bits used for signaling       */
     316             :     const Word16 low_complexity /* i  : low-complexity flag           */
     317             : )
     318             : {
     319             :     Word32 env[N_MAX_ARI]; /* unscaled envelope (Q16) */
     320             :     Word16 *envelope;      /* scaled envelope (Q15-e) */
     321             :     Word16 envelope_e;
     322             :     Word16 L_spec_core;
     323             :     TCX_CONFIG_HANDLE hTcxCfg;
     324             :     TCX_DEC_HANDLE hTcxDec;
     325             :     Word16 gamma_w, gamma_uw;
     326             :     Word16 hm_bits;
     327             : 
     328       13989 :     test();
     329       13989 :     test();
     330       13989 :     test();
     331       13989 :     test();
     332       13989 :     test();
     333       13989 :     test();
     334       13989 :     test();
     335       13989 :     IF( GT_16( L_spec, N_MAX_ARI ) || ( ( st->element_mode == EVS_MONO ) && GT_16( target_bits, ( ACELP_13k20 / FRAMES_PER_SEC ) ) ) ||
     336             :         ( EQ_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE / FRAMES_PER_SEC ) ) ) ) ||
     337             :         ( GT_16( st->element_mode, IVAS_SCE ) && ( GT_16( st->bits_frame_nominal, ( LPC_SHAPED_ARI_MAX_RATE_CPE / FRAMES_PER_SEC ) ) ) ) ||
     338             :         ( target_bits <= 0 ) )
     339             :     {
     340             :         /* this could happen in case of bit errors */
     341           0 :         st->BER_detect = 1;
     342           0 :         move16();
     343           0 :         L_spec = N_MAX_ARI;
     344           0 :         move16();
     345           0 :         *signaling_bits = 0;
     346           0 :         move16();
     347           0 :         *arith_bits = 0;
     348           0 :         move16();
     349           0 :         set32_fx( q_spectrum, 0, L_frame );
     350             : 
     351           0 :         return;
     352             :     }
     353             : 
     354       13989 :     hTcxCfg = st->hTcxCfg;
     355       13989 :     hTcxDec = st->hTcxDec;
     356       13989 :     *signaling_bits = 0;
     357       13989 :     move16();
     358             : 
     359       13989 :     assert( hTcxDec->enableTcxLpc );
     360       13989 :     gamma_w = MAX16B;
     361       13989 :     move16();
     362       13989 :     gamma_uw = st->inv_gamma;
     363       13989 :     move16();
     364             : 
     365             : #define WMC_TOOL_SKIP
     366       13989 :     tcx_arith_render_envelope_ivas_fx( A_ind, L_frame, L_spec, hTcxCfg->preemph_fac, gamma_w, gamma_uw, env );
     367             : #undef WMC_TOOL_SKIP
     368             : 
     369       13989 :     IF( use_hm != 0 )
     370             :     {
     371       12695 :         IF( prm_hm[0] != 0 )
     372             :         {
     373        5210 :             tcx_hm_decode( L_spec, env, target_bits, st->coder_type, prm_hm, tcxltp_pitch, &hm_bits );
     374             : 
     375        5210 :             IF( hm_bits < 0 )
     376             :             {
     377           0 :                 st->BER_detect = 1;
     378           0 :                 move16();
     379           0 :                 *signaling_bits = 0;
     380           0 :                 move16();
     381           0 :                 *arith_bits = 0;
     382           0 :                 move16();
     383           0 :                 set32_fx( q_spectrum, 0, L_frame );
     384             : 
     385           0 :                 return;
     386             :             }
     387             :         }
     388             :         ELSE
     389             :         {
     390        7485 :             hm_bits = 1;
     391        7485 :             move16();
     392             :         }
     393       12695 :         *signaling_bits = add( *signaling_bits, hm_bits );
     394       12695 :         move16();
     395             :     }
     396             : 
     397       13989 :     L_spec_core = L_spec;
     398       13989 :     move16();
     399       13989 :     IF( st->igf )
     400             :     {
     401       13989 :         L_spec_core = s_min( L_spec_core, st->hIGFDec->infoIGFStartLine );
     402             :     }
     403             : 
     404       13989 :     envelope = (Word16 *) env;
     405       13989 :     tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
     406             : 
     407       13989 :     *arith_bits = tcx_arith_decode_ivas_fx( L_spec, envelope, envelope_e, target_bits, prm, q_spectrum, q_spectrum_e );
     408       13989 :     move16();
     409             : 
     410             :     /* safety check in case of bit errors */
     411       13989 :     IF( *arith_bits < 0 )
     412             :     {
     413           0 :         st->BER_detect = 1;
     414           0 :         move16();
     415           0 :         set32_fx( q_spectrum, 0, L_frame );
     416             :     }
     417             : 
     418       13989 :     set32_fx( q_spectrum + L_spec, 0, sub( L_frame, L_spec ) );
     419             : 
     420       13989 :     return;
     421             : }

Generated by: LCOV version 1.14