LCOV - code coverage report
Current view: top level - lib_dec - ACcontextMapping_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 209 231 90.5 %
Date: 2025-11-01 09:50:07 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       39838 : 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       39838 :     set_s( x, 0, nt );
      85             : 
      86             :     /* Rate flag */
      87       39838 :     if ( nbbits > 400 )
      88             :     {
      89       16375 :         rateFlag = 2 << NBITS_CONTEXT;
      90             :     }
      91             :     else
      92             :     {
      93       23463 :         rateFlag = 0;
      94             :     }
      95             : 
      96             :     /*Decode number of ntuples*/
      97       39838 :     start_bit_pos = st->next_bit_pos;
      98       39838 :     lsbs_bit_pos = start_bit_pos + nbbits - 1;
      99             : 
     100       39838 :     n = 0;
     101       39838 :     k = 1;
     102       39838 :     nt_half = nt >> 1;
     103             : 
     104      388525 :     while ( k < nt_half )
     105             :     {
     106      348687 :         ++n;
     107      348687 :         k = k << 1;
     108             :     }
     109       39838 :     n = get_next_indice( st, n ) + 1;
     110             : 
     111             :     /* Init */
     112       39838 :     c[0] = c[1] = 0;
     113             : 
     114       39838 :     t = 0;
     115             : 
     116       39838 :     lastnz = n << 1;
     117             : 
     118       39838 :     if ( lastnz > nt || st->BER_detect )
     119             :     {
     120           0 :         st->BER_detect = 1;
     121           0 :         return 0;
     122             :     }
     123             : 
     124       39838 :     if ( hm_cfg )
     125             :     {
     126             :         /* mapped domain */
     127        5282 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     128        5282 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     129        5282 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     130             : 
     131             :         /* Mark hole indices beyond lastnz as pruned */
     132     1207210 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     133             :         {
     134     1201928 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     135             :         }
     136             : 
     137        5282 :         ii[0] = numPeakIndicesOrig;
     138        5282 :         ii[1] = 0;
     139             : 
     140        5282 :         p1 = p2 = 0; /* to avoid compilation warnings */
     141             :     }
     142             :     else
     143             :     {
     144             :         /* unmapped domain */
     145       34556 :         ii[0] = 0;
     146       34556 :         p1 = p2 = 0;
     147             :     }
     148             : 
     149             :     /* Start Decoding */
     150             : 
     151       39838 :     ari_start_decoding_14bits( st, &as );
     152       39838 :     overflow_bit_pos = st->next_bit_pos;
     153             : 
     154       39838 :     nbbits_m2 = nbbits + cbitsnew - 2;
     155       39838 :     rest_bits_overflow = rest_bits = -nbbits_m2;
     156             : 
     157             :     /* Main Loop through the 2-tuples */
     158     5274540 :     for ( k = 0; k < lastnz; k += 2 )
     159             :     {
     160     5234710 :         if ( hm_cfg )
     161             :         {
     162      638916 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     163      638916 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     164             :         }
     165             :         else
     166             :         {
     167     4595794 :             a1_i = get_next_coeff_unmapped( ii, &idx1 );
     168     4595794 :             b1_i = get_next_coeff_unmapped( ii, &idx2 );
     169             :         }
     170             : 
     171     5234710 :         idx = min( idx1, idx2 );
     172             : 
     173             :         /* Get context */
     174     5234710 :         ctx = &c[p1 | p2];
     175             : 
     176     5234710 :         t = (uint16_t) ( *ctx + rateFlag );
     177     5234710 :         t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     178             : 
     179     5234710 :         esc_nb = 0;
     180     5234710 :         r = 0;
     181             :         /* BER detection: Check max value of context t leading to out-of-bound access to ari_lookup_s17_LC[]*/
     182     5234710 :         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     5234710 :         a = b = 0;
     189             :         /* MSBs decoding */
     190     6324943 :         for ( lev = 0; lev < 15 && lsbs_bit_pos > 0; ++lev )
     191             :         {
     192     6324943 :             esc_nb = min( lev, 3 );
     193     6324943 :             pki = ari_lookup_s17_LC[t + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     194     6324943 :             ari_decode_14bits_s17_ext( st, &r, &as, ari_pk_s17_LC_ext[pki] );
     195             : 
     196     6324943 :             if ( r < VAL_ESC )
     197             :             {
     198     5234710 :                 break;
     199             :             }
     200             : 
     201             :             /* LSBs decoding */
     202     1090233 :             a += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     203     1090233 :             b += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     204             :         }
     205             : 
     206     5234710 :         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     5234710 :         b1 = r >> 2;
     216     5234710 :         a1 = r & 0x3;
     217     5234710 :         a += a1 << lev;
     218     5234710 :         b += b1 << lev;
     219             : 
     220             :         /* lsbs bits sign bits */
     221     5234710 :         rest_bits += 2 * lev;
     222             : 
     223     5234710 :         rest_bits += min( a, 1 );
     224     5234710 :         rest_bits += min( b, 1 );
     225             : 
     226             :         /* Dectect overflow */
     227             : 
     228     5234710 :         if ( st->next_bit_pos - start_bit_pos + rest_bits > 0 )
     229             :         {
     230             :             /* Roll back bitstream position to overflow_bit_pos */
     231           8 :             get_next_indice_tmp( st, overflow_bit_pos - st->next_bit_pos );
     232           8 :             rest_bits = rest_bits_overflow;
     233           8 :             x[a1_i] = 0;
     234           8 :             x[b1_i] = 0;
     235           8 :             break;
     236             :         }
     237             : 
     238     5234702 :         overflow_bit_pos = st->next_bit_pos;
     239     5234702 :         rest_bits_overflow = rest_bits;
     240             : 
     241             :         /* Store decoded data */
     242     5234702 :         x[a1_i] = a;
     243     5234702 :         x[b1_i] = b;
     244             : 
     245             :         /* Update context for next 2-tuple */
     246     5234702 :         if ( p1 == p2 )
     247             :         {
     248             :             /* peak-peak or hole-hole context */
     249     5167027 :             lev = esc_nb - 1;
     250             : 
     251     5167027 :             if ( lev <= 0 )
     252             :             {
     253     4877503 :                 t = 1 + ( a1 + b1 ) * ( lev + 2 );
     254             :             }
     255             :             else
     256             :             {
     257      289524 :                 t = 13 + lev;
     258             :             }
     259             : 
     260     5167027 :             *ctx = ( *ctx & 0xf ) * 16 + t;
     261             :         }
     262             :         else
     263             :         {
     264             :             /* mixed context */
     265             : 
     266       67675 :             if ( idx1 & 1 )
     267             :             {
     268             :                 /* update first context */
     269       34305 :                 c[p1] = update_mixed_context( c[p1], a );
     270             :             }
     271             : 
     272       67675 :             if ( idx2 & 1 )
     273             :             {
     274             :                 /* update second context */
     275       33370 :                 c[p2] = update_mixed_context( c[p2], b );
     276             :             }
     277             :         }
     278             :     }
     279             : 
     280             :     /* Total number of decoded AC bits */
     281       39838 :     get_next_indice_tmp( st, -( cbitsnew - 2 ) );
     282             : 
     283             :     /* detect overflow */
     284             : 
     285       39838 :     if ( k != lastnz )
     286             :     {
     287           8 :         rest_bits += nbbits_m2;
     288             :         /* Set bitstream position to (start_bit_pos+nbbits-rest_bits) */
     289           8 :         get_next_indice_tmp( st, ( start_bit_pos + nbbits - rest_bits ) - st->next_bit_pos );
     290             :     }
     291             : 
     292             :     /* Decode signs */
     293       39838 :     if ( hm_cfg )
     294             :     {
     295        5282 :         n = nt;
     296             :     }
     297             :     else
     298             :     {
     299       34556 :         n = lastnz;
     300             :     }
     301             : 
     302    11711586 :     for ( k = 0; k < n; k++ )
     303             :     {
     304    11671748 :         if ( x[k] > 0 )
     305             :         {
     306     4071859 :             x[k] *= 1 - 2 * get_next_indice_1( st );
     307             :         }
     308             :     }
     309             : 
     310             :     /* Decode Residual Q */
     311       39838 :     resQBits = min( resQMaxBits, lsbs_bit_pos + 1 - st->next_bit_pos );
     312             : 
     313      314197 :     for ( k = 0; k < resQBits; ++k )
     314             :     {
     315      274359 :         x[nt + k] = get_indice_1( st, lsbs_bit_pos - k );
     316             :     }
     317             : 
     318             :     /* Set bitstream pointer to end of buffer */
     319       39838 :     get_next_indice_tmp( st, ( start_bit_pos + nbbits ) - st->next_bit_pos );
     320             : 
     321             : 
     322       39838 :     return resQBits;
     323             : }
     324             : 
     325             : 
     326             : /*-------------------------------------------------------------------*
     327             :  * RCcontextMapping_decode2_no_mem_s17_LCS()
     328             :  *
     329             :  * Range decoder
     330             :  *-------------------------------------------------------------------*/
     331             : 
     332             : /*! r: resQBits */
     333    32791791 : 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    32791791 :     set_s( x, 0, nt );
     359             : 
     360             :     /*Decode number of ntuples*/
     361    32791791 :     start_bit_pos = st->next_bit_pos;
     362    32791791 :     lsbs_bit_pos = start_bit_pos + nbbits - 1;
     363    32791791 :     n = 0;
     364    32791791 :     k = 1;
     365    32791791 :     nt_half = nt >> 1;
     366             : 
     367   322722369 :     while ( k < nt_half )
     368             :     {
     369   289930578 :         ++n;
     370   289930578 :         k = k << 1;
     371             :     }
     372             : 
     373    32791791 :     bits_tups = n;
     374             : 
     375    32791791 :     n = get_next_indice( st, n ) + 1;
     376             : 
     377    32791791 :     t = 0;
     378             : 
     379    32791791 :     lastnz = n << 1;
     380             : 
     381    32791791 :     if ( lastnz > nt )
     382             :     {
     383           0 :         st->BER_detect = 1;
     384           0 :         return 0;
     385             :     }
     386             : 
     387    32791791 :     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      347124 :         if ( nbbits > 400 )
     398             :         {
     399      139971 :             rateFlag = 2 << NBITS_CONTEXT;
     400             :         }
     401             :         else
     402             :         {
     403      207153 :             rateFlag = 0;
     404             :         }
     405             : 
     406             :         /* Init */
     407      347124 :         c[0] = c[1] = 0;
     408             : 
     409             :         /* mapped domain */
     410      347124 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     411      347124 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     412      347124 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     413             : 
     414             :         /* Mark hole indices beyond lastnz as pruned */
     415   120568299 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     416             :         {
     417   120221175 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     418             :         }
     419             : 
     420      347124 :         ii[0] = numPeakIndicesOrig;
     421      347124 :         ii[1] = 0;
     422             : 
     423      347124 :         p1 = p2 = 0; /* to avoid compilation warnings */
     424             : 
     425             :         /* Start Decoding */
     426             :         /* Initialize range decoder */
     427      347124 :         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    58807035 :         for ( k = 0; k < lastnz; k += 2 )
     436             :         {
     437    58459911 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     438    58459911 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     439    58459911 :             idx = min( idx1, idx2 );
     440             : 
     441             :             /* Get context */
     442    58459911 :             ctx = &c[p1 | p2];
     443             : 
     444    58459911 :             t = (uint16_t) ( *ctx + rateFlag );
     445    58459911 :             t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     446             : 
     447    58459911 :             esc_nb = 0;
     448    58459911 :             r = 0;
     449             : 
     450             :             /* BER detection: Check max value of context t leading to out-of-bound access to ari_lookup_s17_LC[]*/
     451    58459911 :             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    58459911 :             a = b = 0;
     458             : 
     459             :             /* MSBs decoding */
     460    63512694 :             for ( lev = 0; lev < 15 && lsbs_bit_pos > 0; ++lev )
     461             :             {
     462    63512694 :                 esc_nb = min( lev, 3 );
     463    63512694 :                 pki = ari_lookup_s17_LC[t + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     464    63512694 :                 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    63512694 :                 if ( r < VAL_ESC ) /* If symbol read corresponds to MSB */
     468             :                 {
     469    58459911 :                     break;
     470             :                 }
     471             : 
     472             :                 /* LSBs decoding */
     473     5052783 :                 a += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     474     5052783 :                 b += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     475             :             }
     476             : 
     477    58459911 :             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    58459911 :             b1 = r >> 2;
     487    58459911 :             a1 = r & 0x3;
     488    58459911 :             a += a1 << lev;
     489    58459911 :             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    58459911 :             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    58459911 :             x[a1_i] = a;
     509    58459911 :             x[b1_i] = b;
     510             : 
     511             :             /* Update context for next 2-tuple */
     512    58459911 :             if ( p1 == p2 )
     513             :             {
     514             :                 /* peak-peak or hole-hole context */
     515    53163471 :                 lev = esc_nb - 1;
     516             : 
     517    53163471 :                 if ( lev <= 0 )
     518             :                 {
     519    52250658 :                     t = 1 + ( a1 + b1 ) * ( lev + 2 );
     520             :                 }
     521             :                 else
     522             :                 {
     523      912813 :                     t = 13 + lev;
     524             :                 }
     525             : 
     526    53163471 :                 *ctx = ( *ctx & 0xf ) * 16 + t;
     527             :             }
     528             :             else
     529             :             {
     530             :                 /* mixed context */
     531             : 
     532     5296440 :                 if ( idx1 & 1 )
     533             :                 {
     534             :                     /* update first context */
     535     2635764 :                     c[p1] = update_mixed_context( c[p1], a );
     536             :                 }
     537             : 
     538     5296440 :                 if ( idx2 & 1 )
     539             :                 {
     540             :                     /* update second context */
     541     2660676 :                     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      347124 :         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      347124 :         n = nt;
     559             :     }
     560             :     else /* if(!hm_cfg) */
     561             :     {
     562             :         int16_t c, rateQ;
     563             :         uint16_t s;
     564             : 
     565             :         /* Rate flag */
     566    32444667 :         if ( nbbits > 400 )
     567             :         {
     568    22781604 :             rateFlag = 2;
     569             :         }
     570             :         else
     571             :         {
     572     9663063 :             rateFlag = 0;
     573             :         }
     574             : 
     575             :         /* Start Decoding */
     576             :         /* Initialize range decoder */
     577    32444667 :         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    32444667 :         t = 0;
     585    32444667 :         s = 0;
     586             : 
     587             :         /* Main Loop through the 2-tuples */
     588  6794040747 :         for ( k = 0; k < lastnz; k += 2 )
     589             :         {
     590  6761596080 :             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  6761596080 :             if ( ( t >= ( 1 << ( NBITS_CONTEXT ) ) ) || lsbs_bit_pos <= 0 )
     594             :             {
     595           0 :                 st->BER_detect = 1;
     596           0 :                 return 0;
     597             :             }
     598             : 
     599  6761596080 :             a = b = 0;
     600  6761596080 :             esc_nb = 0;
     601  6761596080 :             r = 0;
     602             : 
     603             :             /* MSBs decoding */
     604  8139977106 :             for ( lev = 0; lev < 15 && lsbs_bit_pos > 0; ++lev )
     605             :             {
     606  8139977106 :                 esc_nb = min( lev, 3 );
     607  8139977106 :                 pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     608  8139977106 :                 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  8139977106 :                 if ( r < VAL_ESC ) /* If symbol read corresponds to MSB */
     612             :                 {
     613  6761596080 :                     break;
     614             :                 }
     615             : 
     616             :                 /* LSBs decoding */
     617  1378381026 :                 a += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     618  1378381026 :                 b += get_indice_1( st, lsbs_bit_pos-- ) << lev;
     619             :             }
     620             : 
     621  6761596080 :             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  6761596080 :             b1 = r >> 2;
     631  6761596080 :             a1 = r & 0x3;
     632  6761596080 :             a += a1 << lev;
     633  6761596080 :             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  6761596080 :             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  6761596080 :             x[k + 0] = a;
     653  6761596080 :             x[k + 1] = b;
     654             : 
     655             :             /* Update context for next 2-tuple */
     656  6761596080 :             if ( esc_nb < 2 )
     657             :             {
     658  6417303090 :                 c = 1 + ( ( a1 + b1 ) * ( esc_nb + 1 ) );
     659             :             }
     660             :             else
     661             :             {
     662   344292990 :                 c = 12 + esc_nb;
     663             :             }
     664             : 
     665  6761596080 :             s = s << 4; /*Shift old 4 bits*/
     666  6761596080 :             s = s + c;  /*replace last 4 bits*/
     667  6761596080 :             t = s & 0xFF;
     668             :         }
     669             : 
     670             :         /* Decode signs */
     671    32444667 :         n = lastnz;
     672             :     } /* end of if(hm_cfg) */
     673             : 
     674 13793786991 :     for ( k = 0; k < n; k++ )
     675             :     {
     676 13760995200 :         if ( x[k] > 0 )
     677             :         {
     678  5204801748 :             x[k] *= 1 - 2 * get_next_indice_1( st );
     679             :         }
     680             :     }
     681             : 
     682             :     /* Decode Residual Q */
     683    32791791 :     resQBits = min( resQMaxBits, lsbs_bit_pos + 1 - st->next_bit_pos );
     684             : 
     685   293652891 :     for ( k = 0; k < resQBits; ++k )
     686             :     {
     687   260861100 :         x[nt + k] = get_indice_1( st, lsbs_bit_pos - k );
     688             :     }
     689             : 
     690             :     /* Set bitstream pointer to end of buffer */
     691    32791791 :     get_next_indice_tmp( st, ( start_bit_pos + nbbits ) - st->next_bit_pos );
     692             : 
     693    32791791 :     return resQBits;
     694             : }

Generated by: LCOV version 1.14