LCOV - code coverage report
Current view: top level - lib_dec - ACcontextMapping_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 202 231 87.4 %
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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include "cnst.h"
      43             : #include "rom_com.h"
      44             : #include "ivas_rom_com.h"
      45             : #include "prot.h"
      46             : #include "wmc_auto.h"
      47             : #include "ivas_prot.h" /* Range coder header file */
      48             : #include <assert.h>
      49             : 
      50             : /*-------------------------------------------------------------------*
      51             :  * ACcontextMapping_decode2_no_mem_s17_LC()
      52             :  *
      53             :  * Arithmetic decoder
      54             :  *-------------------------------------------------------------------*/
      55             : 
      56             : /*! r: resQBits */
      57        1938 : int16_t ACcontextMapping_decode2_no_mem_s17_LC(
      58             :     Decoder_State *st,        /* i/o: decoder state                             */
      59             :     int16_t *x,               /* o  : decoded spectrum                          */
      60             :     int16_t nt,               /* i  : size of spectrum                          */
      61             :     int16_t nbbits,           /* i  : bit budget                                */
      62             :     int16_t resQMaxBits,      /* i  : residual coding maximum bits              */
      63             :     CONTEXT_HM_CONFIG *hm_cfg /* i  : context-based harmonic model configuration*/
      64             : )
      65             : {
      66             :     Tastat as;
      67             :     int16_t start_bit_pos, lsbs_bit_pos, overflow_bit_pos;
      68             :     int16_t a, b, a1, b1, a1_i, b1_i, k;
      69             :     uint16_t t;
      70             :     int16_t lev, pki, esc_nb;
      71             :     int16_t rateFlag;
      72             :     int16_t lastnz, n;
      73             :     uint16_t r;
      74             :     int16_t resQBits;
      75             :     int16_t rest_bits, rest_bits_overflow;
      76             :     int16_t nt_half;
      77             :     int32_t c[2], *ctx;
      78             :     int32_t p1, p2;
      79             :     int16_t ii[2];
      80             :     int16_t idx1, idx2, idx;
      81             :     int16_t numPeakIndicesOrig, numHoleIndices;
      82             :     int16_t nbbits_m2;
      83             : 
      84        1938 :     set_s( x, 0, nt );
      85             : 
      86             :     /* Rate flag */
      87        1938 :     if ( nbbits > 400 )
      88             :     {
      89         114 :         rateFlag = 2 << NBITS_CONTEXT;
      90             :     }
      91             :     else
      92             :     {
      93        1824 :         rateFlag = 0;
      94             :     }
      95             : 
      96             :     /*Decode number of ntuples*/
      97        1938 :     start_bit_pos = st->next_bit_pos;
      98        1938 :     lsbs_bit_pos = start_bit_pos + nbbits - 1;
      99             : 
     100        1938 :     n = 0;
     101        1938 :     k = 1;
     102        1938 :     nt_half = nt >> 1;
     103             : 
     104       19380 :     while ( k < nt_half )
     105             :     {
     106       17442 :         ++n;
     107       17442 :         k = k << 1;
     108             :     }
     109        1938 :     n = get_next_indice( st, n ) + 1;
     110             : 
     111             :     /* Init */
     112        1938 :     c[0] = c[1] = 0;
     113             : 
     114        1938 :     t = 0;
     115             : 
     116        1938 :     lastnz = n << 1;
     117             : 
     118        1938 :     if ( lastnz > nt || st->BER_detect )
     119             :     {
     120           0 :         st->BER_detect = 1;
     121           0 :         return 0;
     122             :     }
     123             : 
     124        1938 :     if ( hm_cfg )
     125             :     {
     126             :         /* mapped domain */
     127         165 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     128         165 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     129         165 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     130             : 
     131             :         /* Mark hole indices beyond lastnz as pruned */
     132       72825 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     133             :         {
     134       72660 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     135             :         }
     136             : 
     137         165 :         ii[0] = numPeakIndicesOrig;
     138         165 :         ii[1] = 0;
     139             : 
     140         165 :         p1 = p2 = 0; /* to avoid compilation warnings */
     141             :     }
     142             :     else
     143             :     {
     144             :         /* unmapped domain */
     145        1773 :         ii[0] = 0;
     146        1773 :         p1 = p2 = 0;
     147             :     }
     148             : 
     149             :     /* Start Decoding */
     150             : 
     151        1938 :     ari_start_decoding_14bits( st, &as );
     152        1938 :     overflow_bit_pos = st->next_bit_pos;
     153             : 
     154        1938 :     nbbits_m2 = nbbits + cbitsnew - 2;
     155        1938 :     rest_bits_overflow = rest_bits = -nbbits_m2;
     156             : 
     157             :     /* Main Loop through the 2-tuples */
     158      200604 :     for ( k = 0; k < lastnz; k += 2 )
     159             :     {
     160      198666 :         if ( hm_cfg )
     161             :         {
     162       16470 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     163       16470 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     164             :         }
     165             :         else
     166             :         {
     167      182196 :             a1_i = get_next_coeff_unmapped( ii, &idx1 );
     168      182196 :             b1_i = get_next_coeff_unmapped( ii, &idx2 );
     169             :         }
     170             : 
     171      198666 :         idx = min( idx1, idx2 );
     172             : 
     173             :         /* Get context */
     174      198666 :         ctx = &c[p1 | p2];
     175             : 
     176      198666 :         t = (uint16_t) ( *ctx + rateFlag );
     177      198666 :         t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     178             : 
     179      198666 :         esc_nb = 0;
     180      198666 :         r = 0;
     181             :         /* BER detection: Check max value of context t leading to out-of-bound access to ari_lookup_s17_LC[]*/
     182      198666 :         if ( ( t >= ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ) ) || lsbs_bit_pos <= 0 )
     183             :         {
     184           0 :             st->BER_detect = 1;
     185           0 :             return 0;
     186             :         }
     187             : 
     188      198666 :         a = b = 0;
     189             :         /* MSBs decoding */
     190      210213 :         for ( lev = 0; lev < 15 && lsbs_bit_pos > 0; ++lev )
     191             :         {
     192      210213 :             esc_nb = min( lev, 3 );
     193      210213 :             pki = ari_lookup_s17_LC[t + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     194      210213 :             ari_decode_14bits_s17_ext( st, &r, &as, ari_pk_s17_LC_ext[pki] );
     195             : 
     196      210213 :             if ( r < VAL_ESC )
     197             :             {
     198      198666 :                 break;
     199             :             }
     200             : 
     201             :             /* LSBs decoding */
     202       11547 :             a += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     203       11547 :             b += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     204             :         }
     205             : 
     206      198666 :         if ( ( lsbs_bit_pos < -1 && r >= VAL_ESC ) || ( lev > 14 ) )
     207             :         {
     208           0 :             x[a1_i] = 0;
     209           0 :             x[b1_i] = 0;
     210           0 :             st->BER_detect = 1;
     211           0 :             return 0;
     212             :         }
     213             : 
     214             :         /* MSBs contributions */
     215      198666 :         b1 = r >> 2;
     216      198666 :         a1 = r & 0x3;
     217      198666 :         a += a1 << lev;
     218      198666 :         b += b1 << lev;
     219             : 
     220             :         /* lsbs bits sign bits */
     221      198666 :         rest_bits += 2 * lev;
     222             : 
     223      198666 :         rest_bits += min( a, 1 );
     224      198666 :         rest_bits += min( b, 1 );
     225             : 
     226             :         /* Dectect overflow */
     227             : 
     228      198666 :         if ( st->next_bit_pos - start_bit_pos + rest_bits > 0 )
     229             :         {
     230             :             /* Roll back bitstream position to overflow_bit_pos */
     231           0 :             get_next_indice_tmp( st, overflow_bit_pos - st->next_bit_pos );
     232           0 :             rest_bits = rest_bits_overflow;
     233           0 :             x[a1_i] = 0;
     234           0 :             x[b1_i] = 0;
     235           0 :             break;
     236             :         }
     237             : 
     238      198666 :         overflow_bit_pos = st->next_bit_pos;
     239      198666 :         rest_bits_overflow = rest_bits;
     240             : 
     241             :         /* Store decoded data */
     242      198666 :         x[a1_i] = a;
     243      198666 :         x[b1_i] = b;
     244             : 
     245             :         /* Update context for next 2-tuple */
     246      198666 :         if ( p1 == p2 )
     247             :         {
     248             :             /* peak-peak or hole-hole context */
     249      197898 :             lev = esc_nb - 1;
     250             : 
     251      197898 :             if ( lev <= 0 )
     252             :             {
     253      195225 :                 t = 1 + ( a1 + b1 ) * ( lev + 2 );
     254             :             }
     255             :             else
     256             :             {
     257        2673 :                 t = 13 + lev;
     258             :             }
     259             : 
     260      197898 :             *ctx = ( *ctx & 0xf ) * 16 + t;
     261             :         }
     262             :         else
     263             :         {
     264             :             /* mixed context */
     265             : 
     266         768 :             if ( idx1 & 1 )
     267             :             {
     268             :                 /* update first context */
     269         375 :                 c[p1] = update_mixed_context( c[p1], a );
     270             :             }
     271             : 
     272         768 :             if ( idx2 & 1 )
     273             :             {
     274             :                 /* update second context */
     275         393 :                 c[p2] = update_mixed_context( c[p2], b );
     276             :             }
     277             :         }
     278             :     }
     279             : 
     280             :     /* Total number of decoded AC bits */
     281        1938 :     get_next_indice_tmp( st, -( cbitsnew - 2 ) );
     282             : 
     283             :     /* detect overflow */
     284             : 
     285        1938 :     if ( k != lastnz )
     286             :     {
     287           0 :         rest_bits += nbbits_m2;
     288             :         /* Set bitstream position to (start_bit_pos+nbbits-rest_bits) */
     289           0 :         get_next_indice_tmp( st, ( start_bit_pos + nbbits - rest_bits ) - st->next_bit_pos );
     290             :     }
     291             : 
     292             :     /* Decode signs */
     293        1938 :     if ( hm_cfg )
     294             :     {
     295         165 :         n = nt;
     296             :     }
     297             :     else
     298             :     {
     299        1773 :         n = lastnz;
     300             :     }
     301             : 
     302      471930 :     for ( k = 0; k < n; k++ )
     303             :     {
     304      469992 :         if ( x[k] > 0 )
     305             :         {
     306      102489 :             x[k] *= 1 - 2 * get_next_indice_1( st );
     307             :         }
     308             :     }
     309             : 
     310             :     /* Decode Residual Q */
     311        1938 :     resQBits = min( resQMaxBits, lsbs_bit_pos + 1 - st->next_bit_pos );
     312             : 
     313       22488 :     for ( k = 0; k < resQBits; ++k )
     314             :     {
     315       20550 :         x[nt + k] = get_indice_1( st, lsbs_bit_pos - k );
     316             :     }
     317             : 
     318             :     /* Set bitstream pointer to end of buffer */
     319        1938 :     get_next_indice_tmp( st, ( start_bit_pos + nbbits ) - st->next_bit_pos );
     320             : 
     321             : 
     322        1938 :     return resQBits;
     323             : }
     324             : 
     325             : 
     326             : /*-------------------------------------------------------------------*
     327             :  * RCcontextMapping_decode2_no_mem_s17_LCS()
     328             :  *
     329             :  * Range decoder
     330             :  *-------------------------------------------------------------------*/
     331             : 
     332             : /*! r: resQBits */
     333     2566344 : int16_t RCcontextMapping_decode2_no_mem_s17_LCS(
     334             :     Decoder_State *st,         /* i/o: decoder state                             */
     335             :     int16_t *x,                /* o  : decoded spectrum                          */
     336             :     const int16_t nt,          /* i  : size of spectrum                          */
     337             :     const int16_t nbbits,      /* i  : bit budget                                */
     338             :     const int16_t resQMaxBits, /* i  : residual coding maximum bits              */
     339             :     CONTEXT_HM_CONFIG *hm_cfg  /* i  : context-based harmonic model configuration*/
     340             : )
     341             : {
     342             :     RangeUniDecState rc_st_dec; /* State of the range decoder */
     343             :     int16_t start_bit_pos, lsbs_bit_pos;
     344             :     int16_t a, b, a1, b1, k;
     345             :     uint16_t t;
     346             :     int16_t lev, pki, esc_nb;
     347             :     int16_t rateFlag;
     348             :     int16_t lastnz, n;
     349             :     uint16_t r;
     350             :     int16_t resQBits;
     351             : #ifdef DEBUGGING
     352             :     int16_t nbbits_m2;
     353             :     int16_t rest_bits;
     354             : #endif
     355             :     int16_t nt_half;
     356             :     int16_t bits_tups; /* No. of bits for coding the no. of tuples */
     357             : 
     358     2566344 :     set_s( x, 0, nt );
     359             : 
     360             :     /*Decode number of ntuples*/
     361     2566344 :     start_bit_pos = st->next_bit_pos;
     362     2566344 :     lsbs_bit_pos = start_bit_pos + nbbits - 1;
     363     2566344 :     n = 0;
     364     2566344 :     k = 1;
     365     2566344 :     nt_half = nt >> 1;
     366             : 
     367    25374897 :     while ( k < nt_half )
     368             :     {
     369    22808553 :         ++n;
     370    22808553 :         k = k << 1;
     371             :     }
     372             : 
     373     2566344 :     bits_tups = n;
     374             : 
     375     2566344 :     n = get_next_indice( st, n ) + 1;
     376             : 
     377     2566344 :     t = 0;
     378             : 
     379     2566344 :     lastnz = n << 1;
     380             : 
     381     2566344 :     if ( lastnz > nt )
     382             :     {
     383           0 :         st->BER_detect = 1;
     384           0 :         return 0;
     385             :     }
     386             : 
     387     2566344 :     if ( hm_cfg )
     388             :     {
     389             :         int16_t a1_i, b1_i;
     390             :         int32_t c[2], *ctx;
     391             :         int32_t p1, p2;
     392             :         int16_t ii[2];
     393             :         int16_t idx1, idx2, idx;
     394             :         int16_t numPeakIndicesOrig, numHoleIndices;
     395             : 
     396             :         /* Rate flag */
     397       37929 :         if ( nbbits > 400 )
     398             :         {
     399       17739 :             rateFlag = 2 << NBITS_CONTEXT;
     400             :         }
     401             :         else
     402             :         {
     403       20190 :             rateFlag = 0;
     404             :         }
     405             : 
     406             :         /* Init */
     407       37929 :         c[0] = c[1] = 0;
     408             : 
     409             :         /* mapped domain */
     410       37929 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     411       37929 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     412       37929 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     413             : 
     414             :         /* Mark hole indices beyond lastnz as pruned */
     415    16448229 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     416             :         {
     417    16410300 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     418             :         }
     419             : 
     420       37929 :         ii[0] = numPeakIndicesOrig;
     421       37929 :         ii[1] = 0;
     422             : 
     423       37929 :         p1 = p2 = 0; /* to avoid compilation warnings */
     424             : 
     425             :         /* Start Decoding */
     426             :         /* Initialize range decoder */
     427       37929 :         rc_uni_dec_init( &rc_st_dec, &st->bit_stream[st->next_bit_pos], nbbits ); /* (nbbits + 30) entries are read by the decoder */
     428             : 
     429             : #ifdef DEBUGGING
     430             :         nbbits_m2 = nbbits;
     431             :         rest_bits = -nbbits_m2;
     432             : #endif
     433             : 
     434             :         /* Main Loop through the 2-tuples */
     435     6943755 :         for ( k = 0; k < lastnz; k += 2 )
     436             :         {
     437     6905826 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     438     6905826 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     439     6905826 :             idx = min( idx1, idx2 );
     440             : 
     441             :             /* Get context */
     442     6905826 :             ctx = &c[p1 | p2];
     443             : 
     444     6905826 :             t = (uint16_t) ( *ctx + rateFlag );
     445     6905826 :             t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     446             : 
     447     6905826 :             esc_nb = 0;
     448     6905826 :             r = 0;
     449             : 
     450             :             /* BER detection: Check max value of context t leading to out-of-bound access to ari_lookup_s17_LC[]*/
     451     6905826 :             if ( ( t >= ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ) ) || lsbs_bit_pos <= 0 )
     452             :             {
     453           0 :                 st->BER_detect = 1;
     454           0 :                 return 0;
     455             :             }
     456             : 
     457     6905826 :             a = b = 0;
     458             : 
     459             :             /* MSBs decoding */
     460     7825143 :             for ( lev = 0; lev < 15 && lsbs_bit_pos > 0; ++lev )
     461             :             {
     462     7825143 :                 esc_nb = min( lev, 3 );
     463     7825143 :                 pki = ari_lookup_s17_LC[t + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     464     7825143 :                 r = rc_uni_dec_read_symbol_fastS( &rc_st_dec, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 17, 14 ); /*Alphabet size = 17 (2^4 = 16 MSB symbols + 1 ESC symbol) */
     465             :                                                                                                                                           /* r is the symbol read, the possible values are {0,1,....alphabet_size - 1}  */
     466             : 
     467     7825143 :                 if ( r < VAL_ESC ) /* If symbol read corresponds to MSB */
     468             :                 {
     469     6905826 :                     break;
     470             :                 }
     471             : 
     472             :                 /* LSBs decoding */
     473      919317 :                 a += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     474      919317 :                 b += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     475             :             }
     476             : 
     477     6905826 :             if ( ( lsbs_bit_pos < -1 && r >= VAL_ESC ) || ( lev > 14 ) )
     478             :             {
     479           0 :                 x[a1_i] = 0;
     480           0 :                 x[b1_i] = 0;
     481           0 :                 st->BER_detect = 1;
     482           0 :                 return 0;
     483             :             }
     484             : 
     485             :             /* MSBs contributions */
     486     6905826 :             b1 = r >> 2;
     487     6905826 :             a1 = r & 0x3;
     488     6905826 :             a += a1 << lev;
     489     6905826 :             b += b1 << lev;
     490             : 
     491             : #ifdef DEBUGGING
     492             :             /* Add 2 LSB bits per bit-plane */
     493             :             rest_bits += 2 * lev;
     494             :             /* Sign bits */
     495             :             rest_bits += min( a, 1 );
     496             :             rest_bits += min( b, 1 );
     497             : #endif
     498             : 
     499             :             /* Update bitstream pointer */
     500     6905826 :             st->next_bit_pos = start_bit_pos + bits_tups + rc_uni_dec_virtual_finish( &rc_st_dec );
     501             : 
     502             :             /* Confirm that there is no overflow */
     503             : #ifdef DEBUGGING
     504             :             assert( st->next_bit_pos - start_bit_pos + rest_bits <= 0 );
     505             : #endif
     506             : 
     507             :             /* Store decoded data */
     508     6905826 :             x[a1_i] = a;
     509     6905826 :             x[b1_i] = b;
     510             : 
     511             :             /* Update context for next 2-tuple */
     512     6905826 :             if ( p1 == p2 )
     513             :             {
     514             :                 /* peak-peak or hole-hole context */
     515     6277530 :                 lev = esc_nb - 1;
     516             : 
     517     6277530 :                 if ( lev <= 0 )
     518             :                 {
     519     6104001 :                     t = 1 + ( a1 + b1 ) * ( lev + 2 );
     520             :                 }
     521             :                 else
     522             :                 {
     523      173529 :                     t = 13 + lev;
     524             :                 }
     525             : 
     526     6277530 :                 *ctx = ( *ctx & 0xf ) * 16 + t;
     527             :             }
     528             :             else
     529             :             {
     530             :                 /* mixed context */
     531             : 
     532      628296 :                 if ( idx1 & 1 )
     533             :                 {
     534             :                     /* update first context */
     535      299247 :                     c[p1] = update_mixed_context( c[p1], a );
     536             :                 }
     537             : 
     538      628296 :                 if ( idx2 & 1 )
     539             :                 {
     540             :                     /* update second context */
     541      329049 :                     c[p2] = update_mixed_context( c[p2], b );
     542             :                 }
     543             :             }
     544             :         }
     545             : 
     546             :         /* We don't need to finish because virtual_finish() already does the same */
     547             :         /*st->next_bit_pos = rc_uni_dec_finish(&rc_st_dec);*/
     548             : 
     549             :         /* Check for bitstream errors */
     550       37929 :         assert( rc_st_dec.bit_error_detected == 0 );
     551             : 
     552             :         /* Cross-check: No overflow */
     553             : #ifdef DEBUGGING
     554             :         assert( k == lastnz );
     555             : #endif
     556             : 
     557             :         /* Decode signs */
     558       37929 :         n = nt;
     559             :     }
     560             :     else /* if(!hm_cfg) */
     561             :     {
     562             :         int16_t c, rateQ;
     563             :         uint16_t s;
     564             : 
     565             :         /* Rate flag */
     566     2528415 :         if ( nbbits > 400 )
     567             :         {
     568     1792305 :             rateFlag = 2;
     569             :         }
     570             :         else
     571             :         {
     572      736110 :             rateFlag = 0;
     573             :         }
     574             : 
     575             :         /* Start Decoding */
     576             :         /* Initialize range decoder */
     577     2528415 :         rc_uni_dec_init( &rc_st_dec, &st->bit_stream[st->next_bit_pos], nbbits ); /* (nbbits + 30) entries are read by the decoder */
     578             : 
     579             : #ifdef DEBUGGING
     580             :         nbbits_m2 = nbbits;
     581             :         rest_bits = -nbbits_m2;
     582             : #endif
     583             : 
     584     2528415 :         t = 0;
     585     2528415 :         s = 0;
     586             : 
     587             :         /* Main Loop through the 2-tuples */
     588   536795172 :         for ( k = 0; k < lastnz; k += 2 )
     589             :         {
     590   534266757 :             rateQ = rateFlag + ( k > ( nt_half ) );
     591             : 
     592             :             /* BER detection: Check max value of context t leading to out-of-bound access to ari_lookup_s17_LC[]*/
     593   534266757 :             if ( ( t >= ( 1 << ( NBITS_CONTEXT ) ) ) || lsbs_bit_pos <= 0 )
     594             :             {
     595           0 :                 st->BER_detect = 1;
     596           0 :                 return 0;
     597             :             }
     598             : 
     599   534266757 :             a = b = 0;
     600   534266757 :             esc_nb = 0;
     601   534266757 :             r = 0;
     602             : 
     603             :             /* MSBs decoding */
     604   632113653 :             for ( lev = 0; lev < 15 && lsbs_bit_pos > 0; ++lev )
     605             :             {
     606   632113653 :                 esc_nb = min( lev, 3 );
     607   632113653 :                 pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     608   632113653 :                 r = rc_uni_dec_read_symbol_fastS( &rc_st_dec, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 17, 14 ); /*Alphabet size = 17 (2^4 = 16 MSB symbols + 1 ESC symbol) */
     609             :                                                                                                                                           /* r is the symbol read, the possible values are {0,1,....alphabet_size - 1}  */
     610             : 
     611   632113653 :                 if ( r < VAL_ESC ) /* If symbol read corresponds to MSB */
     612             :                 {
     613   534266757 :                     break;
     614             :                 }
     615             : 
     616             :                 /* LSBs decoding */
     617    97846896 :                 a += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     618    97846896 :                 b += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     619             :             }
     620             : 
     621   534266757 :             if ( ( lsbs_bit_pos < -1 && r >= VAL_ESC ) || ( lev > 14 ) )
     622             :             {
     623           0 :                 x[k + 0] = 0;
     624           0 :                 x[k + 1] = 0;
     625           0 :                 st->BER_detect = 1;
     626           0 :                 return 0;
     627             :             }
     628             : 
     629             :             /* MSBs contributions */
     630   534266757 :             b1 = r >> 2;
     631   534266757 :             a1 = r & 0x3;
     632   534266757 :             a += a1 << lev;
     633   534266757 :             b += b1 << lev;
     634             : 
     635             : #ifdef DEBUGGING
     636             :             /* Add 2 LSB bits per bit-plane */
     637             :             rest_bits += 2 * lev;
     638             :             /* Sign bits */
     639             :             rest_bits += min( a, 1 );
     640             :             rest_bits += min( b, 1 );
     641             : #endif
     642             : 
     643             :             /* Update bitstream pointer */
     644   534266757 :             st->next_bit_pos = start_bit_pos + bits_tups + rc_uni_dec_virtual_finish( &rc_st_dec );
     645             : 
     646             :             /* Confirm that there is no overflow */
     647             : #ifdef DEBUGGING
     648             :             assert( st->next_bit_pos - start_bit_pos + rest_bits <= 0 );
     649             : #endif
     650             : 
     651             :             /* Store decoded data */
     652   534266757 :             x[k + 0] = a;
     653   534266757 :             x[k + 1] = b;
     654             : 
     655             :             /* Update context for next 2-tuple */
     656   534266757 :             if ( esc_nb < 2 )
     657             :             {
     658   510139638 :                 c = 1 + ( ( a1 + b1 ) * ( esc_nb + 1 ) );
     659             :             }
     660             :             else
     661             :             {
     662    24127119 :                 c = 12 + esc_nb;
     663             :             }
     664             : 
     665   534266757 :             s = s << 4; /*Shift old 4 bits*/
     666   534266757 :             s = s + c;  /*replace last 4 bits*/
     667   534266757 :             t = s & 0xFF;
     668             :         }
     669             : 
     670             :         /* Decode signs */
     671     2528415 :         n = lastnz;
     672             :     } /* end of if(hm_cfg) */
     673             : 
     674  1101380178 :     for ( k = 0; k < n; k++ )
     675             :     {
     676  1098813834 :         if ( x[k] > 0 )
     677             :         {
     678   437808933 :             x[k] *= 1 - 2 * get_next_indice_1( st );
     679             :         }
     680             :     }
     681             : 
     682             :     /* Decode Residual Q */
     683     2566344 :     resQBits = min( resQMaxBits, lsbs_bit_pos + 1 - st->next_bit_pos );
     684             : 
     685    13632351 :     for ( k = 0; k < resQBits; ++k )
     686             :     {
     687    11066007 :         x[nt + k] = get_indice_1( st, lsbs_bit_pos - k );
     688             :     }
     689             : 
     690             :     /* Set bitstream pointer to end of buffer */
     691     2566344 :     get_next_indice_tmp( st, ( start_bit_pos + nbbits ) - st->next_bit_pos );
     692             : 
     693     2566344 :     return resQBits;
     694             : }

Generated by: LCOV version 1.14