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

Generated by: LCOV version 1.14