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

Generated by: LCOV version 1.14