LCOV - code coverage report
Current view: top level - lib_dec - arith_coder_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 41 58 70.7 %
Date: 2025-05-23 08:37:30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "basop_util.h"
      47             : #include "basop_proto_func.h"
      48             : #include "wmc_auto.h"
      49             : 
      50             : /*-------------------------------------------------------*
      51             :  * tcx_arith_decode()
      52             :  *
      53             :  *
      54             :  *-------------------------------------------------------*/
      55             : 
      56             : /*! r: number of bits consumed */
      57       50958 : static int16_t tcx_arith_decode(
      58             :     const int16_t L_frame,     /* i  : number of spectral lines               */
      59             :     const Word16 envelope[],   /* i  : scaled envelope (Q15-e)                */
      60             :     Word16 envelope_e,         /* i  : scaled envelope exponent (Q0)          */
      61             :     const int16_t target_bits, /* i  : target bit budget                      */
      62             :     const int16_t prm[],       /* i  : bitstream parameters                   */
      63             :     float q_spectrum[]         /* o  : scalar quantized spectrum              */
      64             : )
      65             : {
      66             :     int16_t bp, k, q;
      67             :     uint16_t s;
      68             :     Tastat as;
      69             :     uint16_t exp_k;
      70             :     Word16 tmp;
      71             : 
      72       50958 :     bp = ari_start_decoding_14bits_prm( prm, 0, &as );
      73       50958 :     tmp = sub( envelope_e, 1 );
      74             : 
      75    31638663 :     for ( k = 0; k < L_frame; ++k )
      76             :     {
      77    31588947 :         if ( envelope[k] == 0 ) /* safety check in case of bit errors */
      78             :         {
      79           0 :             set_zero( q_spectrum, L_frame );
      80             : 
      81           0 :             return -1;
      82             :         }
      83             :         else
      84             :         {
      85    31588947 :             exp_k = expfp( negate( envelope[k] ), tmp );
      86             :         }
      87             : 
      88             :         /* decode line magnitude */
      89    31588947 :         bp = ari_decode_14bits_pow( prm, bp, target_bits, &q, &as, exp_k );
      90             : 
      91    31588947 :         if ( q )
      92             :         {
      93             :             /* line is non-zero, decode sign */
      94     1473564 :             bp = ari_decode_14bits_sign( prm, bp, target_bits, &s, &as );
      95     1473564 :             q_spectrum[k] = (float) q * ( 3 - 2 * s );
      96             :         }
      97             :         else
      98             :         {
      99             :             /* line is zero, no sign needed */
     100    30115383 :             q_spectrum[k] = 0.0f;
     101             :         }
     102             : 
     103    31588947 :         if ( as.high <= as.low )
     104             :         {
     105        1242 :             if ( bp < target_bits ) /* safety check in case of bit errors */
     106             :             {
     107           0 :                 bp = -1;
     108             :             }
     109        1242 :             break; /* no bits left, so exit loop */
     110             :         }
     111             :     }
     112      545493 :     for ( ; k < L_frame; k++ )
     113             :     {
     114      494535 :         q_spectrum[k] = 0.0f;
     115             :     }
     116             : 
     117       50958 :     return bp;
     118             : }
     119             : 
     120             : 
     121             : /*-------------------------------------------------------*
     122             :  * tcx_arith_decode_envelope()
     123             :  *
     124             :  *
     125             :  *-------------------------------------------------------*/
     126             : 
     127       50958 : void tcx_arith_decode_envelope(
     128             :     Decoder_State *st,           /* i/o: coder state                   */
     129             :     float q_spectrum[],          /* o  : quantised MDCT coefficients   */
     130             :     const int16_t L_frame,       /* i  : frame or MDCT length          */
     131             :     int16_t L_spec,              /* i  : length w/o BW limitation      */
     132             :     const Word16 A_ind[],        /* i  : quantised LPC coefficients    */
     133             :     const int16_t target_bits,   /* i  : number of available bits      */
     134             :     const int16_t prm[],         /* i  : bitstream parameters          */
     135             :     const int16_t use_hm,        /* i  : use HM in current frame?      */
     136             :     const int16_t prm_hm[],      /* i  : HM parameter area             */
     137             :     int16_t tcxltp_pitch,        /* i  : TCX LTP pitch in FD, -1 if n/a*/
     138             :     int16_t *arith_bits,         /* o  : bits used for ari. coding     */
     139             :     int16_t *signaling_bits,     /* o  : bits used for signaling       */
     140             :     const int16_t low_complexity /* i  : low-complexity flag           */
     141             : )
     142             : {
     143             :     Word32 env[N_MAX_ARI]; /* unscaled envelope (Q16) */
     144             :     Word16 *envelope;      /* scaled envelope (Q15-e) */
     145             :     Word16 envelope_e;
     146             :     int16_t L_spec_core;
     147             :     TCX_CONFIG_HANDLE hTcxCfg;
     148             :     TCX_DEC_HANDLE hTcxDec;
     149             :     int16_t k;
     150             :     float gamma_w, gamma_uw;
     151             :     int16_t hm_bits;
     152             : 
     153       50958 :     if ( L_spec > N_MAX_ARI || ( st->element_mode == EVS_MONO && ( target_bits > ( ACELP_13k20 / FRAMES_PER_SEC ) ) ) || ( st->element_mode == IVAS_SCE && ( st->bits_frame_nominal > ( LPC_SHAPED_ARI_MAX_RATE / FRAMES_PER_SEC ) ) ) || ( st->element_mode > IVAS_SCE && ( st->bits_frame_nominal > ( LPC_SHAPED_ARI_MAX_RATE_CPE / FRAMES_PER_SEC ) ) ) || ( target_bits <= 0 ) )
     154             :     {
     155             :         /* this could happen in case of bit errors */
     156           0 :         st->BER_detect = 1;
     157           0 :         L_spec = N_MAX_ARI;
     158           0 :         *signaling_bits = 0;
     159           0 :         *arith_bits = 0;
     160           0 :         set_zero( q_spectrum, L_frame );
     161             : 
     162           0 :         return;
     163             :     }
     164             : 
     165       50958 :     hTcxCfg = st->hTcxCfg;
     166       50958 :     hTcxDec = st->hTcxDec;
     167       50958 :     *signaling_bits = 0;
     168             : 
     169       50958 :     assert( hTcxDec->enableTcxLpc );
     170       50958 :     gamma_w = 1.0f;
     171       50958 :     gamma_uw = 1.0f / st->gamma;
     172             : 
     173             : #define WMC_TOOL_SKIP
     174       50958 :     tcx_arith_render_envelope( A_ind, L_frame, L_spec, FL2WORD16( hTcxCfg->preemph_fac ), FL2WORD16( gamma_w ), FL2WORD16( 0.5f * gamma_uw ), env );
     175             : #undef WMC_TOOL_SKIP
     176             : 
     177       50958 :     if ( use_hm )
     178             :     {
     179       46014 :         if ( prm_hm[0] )
     180             :         {
     181       20118 :             tcx_hm_decode( L_spec, env, target_bits, st->coder_type, prm_hm, tcxltp_pitch, &hm_bits );
     182             : 
     183       20118 :             if ( hm_bits < 0 )
     184             :             {
     185           0 :                 st->BER_detect = 1;
     186           0 :                 *signaling_bits = 0;
     187           0 :                 *arith_bits = 0;
     188           0 :                 set_zero( q_spectrum, L_frame );
     189             : 
     190           0 :                 return;
     191             :             }
     192             :         }
     193             :         else
     194             :         {
     195       25896 :             hm_bits = 1;
     196             :         }
     197       46014 :         *signaling_bits += hm_bits;
     198             :     }
     199             : 
     200       50958 :     L_spec_core = L_spec;
     201       50958 :     if ( st->igf )
     202             :     {
     203       50958 :         L_spec_core = min( L_spec_core, st->hIGFDec->infoIGFStartLine );
     204             :     }
     205             : 
     206       50958 :     envelope = (Word16 *) env;
     207       50958 :     tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
     208             : 
     209       50958 :     *arith_bits = tcx_arith_decode( L_spec, envelope, envelope_e, target_bits, prm, q_spectrum );
     210             : 
     211             :     /* safety check in case of bit errors */
     212       50958 :     if ( *arith_bits < 0 )
     213             :     {
     214           0 :         st->BER_detect = 1;
     215           0 :         set_zero( q_spectrum, L_frame );
     216             :     }
     217             : 
     218       50958 :     for ( k = L_spec; k < L_frame; ++k )
     219             :     {
     220           0 :         q_spectrum[k] = 0;
     221             :     }
     222             : 
     223       50958 :     return;
     224             : }

Generated by: LCOV version 1.14