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

Generated by: LCOV version 1.14