LCOV - code coverage report
Current view: top level - lib_enc - ACcontextMapping_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 533 537 99.3 %
Date: 2025-05-28 04:28:20 Functions: 7 7 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             : #include <assert.h>
      40             : #include "cnst.h"
      41             : #include "rom_com.h"
      42             : #include "prot.h"
      43             : #include "ivas_prot.h" /* Range coder header file */
      44             : #include "ivas_rom_com.h"
      45             : #include "ivas_rom_enc.h"
      46             : #ifdef DEBUGGING
      47             : #include "debug.h"
      48             : #endif
      49             : #include "wmc_auto.h"
      50             : 
      51             : 
      52             : /*-------------------------------------------------------------------*
      53             :  * ACcontextMapping_encode2_no_mem_s17_LC()
      54             :  *
      55             :  * Arithmetic encoder
      56             :  *-------------------------------------------------------------------*/
      57             : 
      58       28064 : void ACcontextMapping_encode2_no_mem_s17_LC(
      59             :     BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle            */
      60             :     int16_t *x,
      61             :     int16_t nt,
      62             :     int16_t lastnz,
      63             :     int16_t nbbits,
      64             :     int16_t resQMaxBits,
      65             :     CONTEXT_HM_CONFIG *hm_cfg )
      66             : {
      67             :     int16_t ptr[BITBUFSIZE];
      68             :     Tastat as, as_overflow;
      69             :     int16_t bp, bp_overflow;
      70             :     int16_t a1, b1, a1_i, b1_i, k;
      71             :     uint16_t t;
      72             :     int16_t pki, lev1;
      73             :     int16_t rateFlag;
      74             :     int16_t value;
      75             :     int16_t nbbits_ntuples, nbbits_lsbs, nbbits_signs, nbbits_signs_overflow, nbbits_lsbs_overflow, flag_overflow;
      76             :     int16_t *lsbs_bits;
      77             :     int16_t nt_half;
      78             :     int32_t c[2], *ctx;
      79             :     int32_t p1, p2;
      80             :     int16_t ii[2];
      81             :     int16_t idx1, idx2, idx;
      82             :     int16_t numPeakIndicesOrig, numHoleIndices;
      83             :     uint16_t signs[N_MAX];
      84             :     int16_t nbbits_m2;
      85             : 
      86       28064 :     a1 = 0; /* to avoid compilation warnings */
      87       28064 :     b1 = 0; /* to avoid compilation warnings */
      88             : 
      89             :     /* Rate flag */
      90       28064 :     if ( nbbits > 400 )
      91             :     {
      92       14725 :         rateFlag = 2 << NBITS_CONTEXT;
      93             :     }
      94             :     else
      95             :     {
      96       13339 :         rateFlag = 0;
      97             :     }
      98             : 
      99             :     /* Init */
     100       28064 :     nt_half = nt >> 1;
     101       28064 :     c[0] = c[1] = 0;
     102             : 
     103             :     /* Bits for encoding the number of encoded tuples */
     104       28064 :     nbbits_ntuples = 0;
     105       28064 :     k = 1;
     106             : 
     107      272072 :     while ( k < nt / 2 )
     108             :     {
     109      244008 :         nbbits_ntuples++;
     110      244008 :         k = k << 1;
     111             :     }
     112             : 
     113       28064 :     t = 0;
     114       28064 :     bp = nbbits_ntuples;
     115       28064 :     nbbits_signs = 0;
     116       28064 :     nbbits_lsbs = 0;
     117       28064 :     nbbits_m2 = nbbits - 2;
     118       28064 :     flag_overflow = 0;
     119             : 
     120       28064 :     if ( hm_cfg )
     121             :     {
     122             :         /* mapped domain */
     123        3399 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     124        3399 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     125        3399 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     126             : 
     127             :         /* Mark hole indices beyond lastnz as pruned */
     128      679927 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     129             :         {
     130      676528 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     131             :         }
     132             : 
     133        3399 :         ii[0] = numPeakIndicesOrig;
     134        3399 :         ii[1] = 0;
     135             : 
     136        3399 :         p1 = p2 = 0; /* to avoid compilation warnings */
     137             :     }
     138             :     else
     139             :     {
     140             :         /* unmapped domain */
     141       24665 :         ii[0] = 0;
     142             : 
     143       24665 :         p1 = p2 = 0;
     144             : 
     145             :         /* Find last non-zero tuple */
     146             :         /* ensure termination of while loop by dummy value */
     147       24665 :         a1 = x[0];
     148       24665 :         x[0] = 1;
     149             : 
     150       78473 :         while ( x[lastnz - 1] == 0 && x[lastnz - 2] == 0 )
     151             :         {
     152       53808 :             lastnz -= 2;
     153             :         }
     154       24665 :         x[0] = a1;
     155             :     }
     156             : 
     157       28064 :     lsbs_bits = (int16_t *) ptr + nbbits - 1;
     158             : 
     159             :     /*Start Encoding*/
     160       28064 :     ari_start_encoding_14bits( &as );
     161             : 
     162             :     /*Main Loop through the 2-tuples*/
     163       28064 :     b1_i = -1;
     164             : 
     165     4404252 :     for ( k = 0; k < lastnz; k += 2 )
     166             :     {
     167             : 
     168     4376196 :         if ( hm_cfg )
     169             :         {
     170      479466 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     171      479466 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     172             :         }
     173             :         else
     174             :         {
     175     3896730 :             a1_i = get_next_coeff_unmapped( ii, &idx1 );
     176     3896730 :             b1_i = get_next_coeff_unmapped( ii, &idx2 );
     177             :         }
     178             : 
     179     4376196 :         idx = min( idx1, idx2 );
     180             : 
     181             :         /* Get context */
     182     4376196 :         ctx = &c[p1 | p2];
     183             : 
     184     4376196 :         t = (uint16_t) ( *ctx + rateFlag );
     185     4376196 :         t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     186             : 
     187             :         /* Init current 2-tuple encoding */
     188             : 
     189     4376196 :         if ( flag_overflow != 0 )
     190             :         {
     191           0 :             x[a1_i] = 0;
     192           0 :             x[b1_i] = 0;
     193             :         }
     194             : 
     195     4376196 :         a1 = (int16_t) abs( x[a1_i] );
     196     4376196 :         b1 = (int16_t) abs( x[b1_i] );
     197             : 
     198     4376196 :         lev1 = -1;
     199             : 
     200             :         /*Copy states*/
     201     4376196 :         ari_copy_states( &as, &as_overflow );
     202     4376196 :         bp_overflow = bp;
     203     4376196 :         nbbits_signs_overflow = nbbits_signs;
     204     4376196 :         nbbits_lsbs_overflow = nbbits_lsbs;
     205             : 
     206             :         /*Signs encoding*/
     207             : 
     208     4376196 :         if ( a1 > 0 )
     209             :         {
     210     2005513 :             signs[nbbits_signs++] = ( (uint16_t) x[a1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
     211             :         }
     212             : 
     213     4376196 :         if ( b1 > 0 )
     214             :         {
     215     2005876 :             signs[nbbits_signs++] = ( (uint16_t) x[b1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
     216             :         }
     217             : 
     218             :         /* MSBs coding */
     219     5719177 :         while ( max( a1, b1 ) >= A_THRES )
     220             :         {
     221     1342981 :             pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     222     1342981 :             bp = ari_encode_14bits_ext( ptr, bp, &as, VAL_ESC, ari_pk_s17_LC_ext[pki] );
     223             : 
     224     1342981 :             *lsbs_bits-- = a1 & 1;
     225     1342981 :             *lsbs_bits-- = b1 & 1;
     226             : 
     227             :             /* LSBs bit counting */
     228     1342981 :             nbbits_lsbs += 2;
     229             : 
     230     1342981 :             a1 >>= 1;
     231     1342981 :             b1 >>= 1;
     232             : 
     233     1342981 :             lev1 = min( lev1 + 1, 2 );
     234             :         }
     235             : 
     236     4376196 :         pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     237     4376196 :         bp = ari_encode_14bits_ext( ptr, bp, &as, a1 + A_THRES * b1, ari_pk_s17_LC_ext[pki] );
     238             : 
     239             : 
     240             :         /* Check bit budget */
     241     4376196 :         if ( bp + as.bits_to_follow + nbbits_signs + nbbits_lsbs > nbbits_m2 )
     242             :         {
     243           8 :             ari_copy_states( &as_overflow, &as );
     244           8 :             bp = bp_overflow;
     245             : 
     246           8 :             if ( !flag_overflow )
     247             :             {
     248           8 :                 nbbits_signs = nbbits_signs_overflow;
     249           8 :                 nbbits_lsbs = nbbits_lsbs_overflow;
     250             : 
     251           8 :                 if ( hm_cfg )
     252             :                 {
     253           4 :                     flag_overflow = 1;
     254             : 
     255             :                     /* Code from now only zeros */
     256           4 :                     x[a1_i] = 0;
     257           4 :                     x[b1_i] = 0;
     258           4 :                     lev1 = -1;
     259             : 
     260           4 :                     pki = ari_lookup_s17_LC[t];
     261           4 :                     bp = ari_encode_14bits_ext( ptr, bp, &as, 0, ari_pk_s17_LC_ext[pki] );
     262             : 
     263           4 :                     if ( bp + as.bits_to_follow + nbbits_signs + nbbits_lsbs > nbbits_m2 )
     264             :                     {
     265           4 :                         ari_copy_states( &as_overflow, &as );
     266           4 :                         bp = bp_overflow;
     267           4 :                         break;
     268             :                     }
     269             :                 }
     270             :                 else
     271             :                 {
     272           4 :                     break;
     273             :                 }
     274             :             }
     275             :             else
     276             :             {
     277           0 :                 break;
     278             :             }
     279             :         }
     280             : 
     281             :         /* Update context for next 2-tuple */
     282     4376188 :         if ( p1 == p2 )
     283             :         {
     284             :             /* peak-peak or hole-hole context */
     285             : 
     286     4316538 :             if ( lev1 <= 0 )
     287             :             {
     288     3955325 :                 t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
     289             :             }
     290             :             else
     291             :             {
     292      361213 :                 t = 13 + lev1;
     293             :             }
     294             : 
     295     4316538 :             *ctx = ( *ctx & 0xf ) * 16 + t;
     296             :         }
     297             :         else
     298             :         {
     299             :             /* mixed context */
     300             : 
     301       59650 :             if ( idx1 & 1 )
     302             :             {
     303             :                 /* update first context */
     304       29951 :                 c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
     305             :             }
     306             : 
     307       59650 :             if ( idx2 & 1 )
     308             :             {
     309             :                 /* update second context */
     310       29699 :                 c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
     311             :             }
     312             :         }
     313             :     } /*end of the 2-tuples loop*/
     314             : 
     315             : 
     316             :     /* End arithmetic coder, overflow management */
     317       28064 :     bp = ari_done_encoding_14bits( ptr, bp, &as );
     318             : 
     319             :     /*Overflow is detected*/
     320             : 
     321       28064 :     if ( k != lastnz )
     322             :     {
     323             : 
     324           8 :         if ( hm_cfg )
     325             :         {
     326             :             /*Fill with zero to be sure that decoder finish at the same position the MSB decoding*/
     327             : 
     328           7 :             for ( ; bp < nbbits - ( nbbits_signs + nbbits_lsbs ); )
     329             :             {
     330           3 :                 ptr[bp++] = 0;
     331             :             }
     332             :         }
     333             :         else
     334             :         {
     335           4 :             lastnz = k;
     336             :         }
     337             :     }
     338             : 
     339             :     /* Push number of encoded tuples */
     340       28064 :     value = ( lastnz >> 1 ) - 1;
     341       28064 :     push_next_indice( hBstr, value, nbbits_ntuples );
     342             : 
     343             :     /* Push arithmetic coded bits */
     344       28064 :     push_next_bits( hBstr, (uint16_t *) &ptr[nbbits_ntuples], bp - nbbits_ntuples );
     345             : 
     346             :     /* Push sign bits */
     347       28064 :     push_next_bits( hBstr, (uint16_t *) signs, nbbits_signs );
     348       28064 :     bp += nbbits_signs;
     349             : 
     350             :     /* write residual Quantization bits */
     351      178892 :     for ( k = 0; k < min( nbbits - bp - nbbits_lsbs, resQMaxBits ); k++ )
     352             :     {
     353      150828 :         ptr[nbbits - 1 - nbbits_lsbs - k] = x[nt + k];
     354             :     }
     355             : 
     356             :     /* Write filler bits */
     357      263314 :     for ( ; k < nbbits - bp - nbbits_lsbs; ++k )
     358             :     {
     359      235250 :         ptr[nbbits - 1 - nbbits_lsbs - k] = 0;
     360             :     }
     361             : 
     362             :     /* Check for debugging */
     363       28064 :     assert( bp + k <= nbbits );
     364             : 
     365             :     /* Push the rest of the buffer */
     366       28064 :     push_next_bits( hBstr, (uint16_t *) &ptr[bp], nbbits - bp );
     367             : 
     368             : #ifdef DEBUGGING
     369             :     /* return (bp+nbbits_lsbs);*/ /*return only for debug plot*/
     370             : #endif
     371       28064 :     return;
     372             : }
     373             : 
     374             : /*-------------------------------------------------------------------*
     375             :  * find_last_nz_pair()
     376             :  *
     377             :  *
     378             :  *-------------------------------------------------------------------*/
     379             : 
     380     4567341 : static int16_t find_last_nz_pair(
     381             :     const int16_t x[],
     382             :     const int16_t length,
     383             :     const CONTEXT_HM_CONFIG *hm_cfg )
     384             : {
     385             :     int16_t last_nz, i;
     386             :     const int16_t *tmp;
     387             : 
     388     4567341 :     last_nz = 2;
     389             : 
     390     4567341 :     if ( hm_cfg )
     391             :     {
     392             :         /* mapped kernel */
     393     4436105 :         tmp = hm_cfg->indexBuffer;
     394             : 
     395   843031075 :         for ( i = length; i >= 4; i -= 2 )
     396             :         {
     397             : 
     398   842519977 :             if ( x[tmp[i - 2]] != 0 || x[tmp[i - 1]] != 0 )
     399             :             {
     400     3925007 :                 last_nz = i;
     401     3925007 :                 break;
     402             :             }
     403             :         }
     404             :     }
     405             :     else
     406             :     {
     407             :         /* unmapped kernel */
     408             : 
     409    18468964 :         for ( i = length; i >= 4; i -= 2 )
     410             :         {
     411             : 
     412    18468044 :             if ( x[i - 2] != 0 || x[i - 1] != 0 )
     413             :             {
     414      130316 :                 last_nz = i;
     415      130316 :                 break;
     416             :             }
     417             :         }
     418             :     }
     419             : 
     420     4567341 :     return last_nz;
     421             : }
     422             : 
     423             : 
     424             : /*-------------------------------------------------------------------*
     425             :  * ACcontextMapping_encode2_estimate_no_mem_s17_LC()
     426             :  *
     427             :  *
     428             :  *-------------------------------------------------------------------*/
     429             : 
     430      182778 : int16_t ACcontextMapping_encode2_estimate_no_mem_s17_LC(
     431             :     const int16_t *x,
     432             :     const int16_t nt,
     433             :     int16_t *lastnz_out,
     434             :     int16_t *nEncoded,
     435             :     const int16_t target,
     436             :     int16_t *stop,
     437             :     CONTEXT_HM_CONFIG *hm_cfg )
     438             : {
     439             :     int16_t a1, b1, a1_i, b1_i;
     440             :     int16_t k, pki, lev1;
     441             :     uint16_t t;
     442             :     int16_t lastnz, lastnz2;
     443             :     int16_t rateFlag;
     444             :     int16_t nbits_old, nbits;
     445             :     int16_t stop2;
     446             :     int32_t proba;
     447             :     int16_t nlz;
     448             :     const uint16_t *cum_freq;
     449             :     int16_t symbol;
     450             :     const uint8_t *lookup;
     451             :     int16_t nt_half;
     452             :     int32_t c[2], *ctx;
     453             :     int32_t p1, p2;
     454             :     int16_t ii[2];
     455             :     int16_t idx1, idx2, idx;
     456      182778 :     int16_t numPeakIndicesOrig = 0, numHoleIndices = 0; /* only to avoid compiler warning */
     457             : 
     458             :     /* Rate flag */
     459      182778 :     if ( target > 400 )
     460             :     {
     461       86782 :         rateFlag = 2 << NBITS_CONTEXT;
     462             :     }
     463             :     else
     464             :     {
     465       95996 :         rateFlag = 0;
     466             :     }
     467             : 
     468             :     /* 2 bits = arithmetic coder initialization interval = 1 bits for rounding last proba + 1 bit?*/
     469      182778 :     nbits = 2;
     470             :     /*proba coded on 14bits -> proba=1*/
     471      182778 :     proba = 16384;
     472             : 
     473             :     /* Init */
     474      182778 :     nt_half = nt >> 1;
     475      182778 :     stop2 = 0;
     476      182778 :     c[0] = c[1] = 0;
     477             : 
     478             :     /* bits to encode lastnz */
     479      182778 :     k = 1;
     480             : 
     481     1773861 :     while ( k < nt / 2 )
     482             :     {
     483     1591083 :         nbits++;
     484     1591083 :         k = k << 1;
     485             :         /* check while condition */
     486             :     }
     487      182778 :     nbits_old = nbits;
     488             : 
     489      182778 :     nbits -= target;
     490             : 
     491             :     /* Find last non-zero tuple in the mapped domain signal */
     492      182778 :     lastnz = find_last_nz_pair( x, nt, hm_cfg );
     493             : 
     494             :     /* At least one tuple is coded */
     495      182778 :     lastnz2 = 2;
     496             : 
     497      182778 :     if ( hm_cfg )
     498             :     {
     499             :         /* mapped domain */
     500       51542 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     501       51542 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     502       51542 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     503             : 
     504             :         /* Mark hole indices beyond lastnz as pruned */
     505    11334232 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     506             :         {
     507    11282690 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     508             :         }
     509             : 
     510       51542 :         ii[0] = numPeakIndicesOrig;
     511       51542 :         ii[1] = 0;
     512             : 
     513       51542 :         p1 = p2 = 0; /* to avoid compilation warnings */
     514             :     }
     515             :     else
     516             :     {
     517             :         /* unmapped domain */
     518      131236 :         ii[0] = 0;
     519             : 
     520      131236 :         p1 = p2 = 0;
     521             :     }
     522             : 
     523             :     /* Main Loop through the 2-tuples */
     524    28620550 :     for ( k = 0; k < lastnz; k += 2 )
     525             :     {
     526    28457046 :         if ( hm_cfg )
     527             :         {
     528     7904400 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     529     7904400 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     530             :         }
     531             :         else
     532             :         {
     533    20552646 :             a1_i = get_next_coeff_unmapped( ii, &idx1 );
     534    20552646 :             b1_i = get_next_coeff_unmapped( ii, &idx2 );
     535             :         }
     536             : 
     537    28457046 :         idx = min( idx1, idx2 );
     538             : 
     539             :         /* Get context */
     540    28457046 :         ctx = &c[p1 | p2];
     541             : 
     542    28457046 :         t = (uint16_t) ( *ctx + rateFlag );
     543    28457046 :         t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     544             : 
     545             :         /* Init current 2-tuple encoding */
     546    28457046 :         a1 = (int16_t) abs( x[a1_i] );
     547    28457046 :         b1 = (int16_t) abs( x[b1_i] );
     548    28457046 :         lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
     549             : 
     550             :         /* Signs Bits */
     551    28457046 :         nbits += min( a1, 1 );
     552    28457046 :         nbits += min( b1, 1 );
     553             : 
     554             :         /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
     555    28457046 :         lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
     556             : 
     557             :         /* check while condition */
     558             :         /* MSBs coding */
     559    35566539 :         while ( max( a1, b1 ) >= A_THRES )
     560             :         {
     561     7109493 :             pki = lookup[lev1];
     562     7109493 :             cum_freq = ari_pk_s17_LC_ext[pki] + VAL_ESC;
     563             :             /*p1*p2=proba on 28 bits: p=0.5->power(2,27)*/
     564     7109493 :             proba *= *( cum_freq );
     565             :             /*Number of leading zero computed in one cycle=norm_l() in BASOP*/
     566     7109493 :             nlz = 2;
     567    16143283 :             while ( proba < 134217728 ) /*power(2,27)*/
     568             :             {
     569     9033790 :                 nlz++;
     570     9033790 :                 proba = proba << 1;
     571             :             }
     572     7109493 :             nbits += nlz;
     573             :             /*addition added as shift not done in norm_l(): real shift = 14-nlz*/
     574     7109493 :             proba >>= 14; /*proba is rounded down on 14 bits ->automatic over-estimation of bit consumption*/
     575             : 
     576     7109493 :             ( a1 ) >>= 1;
     577     7109493 :             ( b1 ) >>= 1;
     578             : 
     579     7109493 :             lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
     580             :             /* check while condition */
     581             :         }
     582    28457046 :         pki = lookup[lev1];
     583    28457046 :         symbol = a1 + A_THRES * b1;
     584    28457046 :         cum_freq = ari_pk_s17_LC_ext[pki] + symbol;
     585             :         /*p1*p2=proba on 28 bits: p=0.5->power(2,27)*/
     586    28457046 :         proba *= ( cum_freq[0] - cum_freq[1] );
     587             :         /*Number of leading zero computed in one cycle=norm_l() in BASOP*/
     588    28457046 :         nlz = 0;
     589   102406030 :         while ( proba < 134217728 ) /*power(2,27)*/
     590             :         {
     591    73948984 :             nlz++;
     592    73948984 :             proba = proba << 1;
     593             :         }
     594             : 
     595    28457046 :         nbits += nlz;
     596    28457046 :         proba >>= 14; /*proba is rounded down on 14 bits ->automatic over-estimation of bit consumption*/
     597             : 
     598             :         /* Should we truncate? */
     599    28457046 :         if ( nbits > 0 )
     600             :         {
     601     1529411 :             stop2 = 1;
     602             : 
     603     1529411 :             if ( *stop )
     604             :             {
     605       19274 :                 break;
     606             :             }
     607             :         }
     608             :         else
     609             :         {
     610    26927635 :             if ( hm_cfg || k == 0 || x[a1_i] || x[b1_i] )
     611             :             {
     612    19509106 :                 nbits_old = nbits + target;
     613    19509106 :                 lastnz2 = b1_i + 1;
     614             :             }
     615             :         }
     616             : 
     617             :         /* Update context for next 2-tuple */
     618    28437772 :         if ( p1 == p2 ) /* peak-peak or hole-hole context */
     619             :         {
     620    27107836 :             lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
     621             : 
     622    27107836 :             if ( lev1 <= 0 )
     623             :             {
     624    25246577 :                 t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
     625             :             }
     626             :             else
     627             :             {
     628     1861259 :                 t = 13 + lev1;
     629             :             }
     630             : 
     631    27107836 :             *ctx = ( *ctx & 0xf ) * 16 + t;
     632             :         }
     633             :         else
     634             :         {
     635             :             /* mixed context */
     636             : 
     637     1329936 :             if ( idx1 & 1 )
     638             :             {
     639             :                 /* update first context */
     640      651729 :                 c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
     641             :             }
     642             : 
     643     1329936 :             if ( idx2 & 1 )
     644             :             {
     645             :                 /* update second context */
     646      675295 :                 c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
     647             :             }
     648             :         }
     649             :     } /*end of the 2-tuples loop*/
     650             : 
     651      182778 :     nbits += target;
     652             : 
     653             :     /* Output */
     654             : 
     655      182778 :     if ( *stop )
     656             :     {
     657       21355 :         nbits = nbits_old;
     658             :     }
     659             : 
     660      182778 :     if ( stop2 )
     661             :     {
     662       99603 :         stop2 = nbits;
     663             :     }
     664      182778 :     *nEncoded = lastnz2;
     665      182778 :     *stop = stop2;
     666      182778 :     *lastnz_out = lastnz;
     667             : 
     668      182778 :     if ( hm_cfg )
     669             :     {
     670             :         /* Restore hole indices beyond lastnz */
     671    11334232 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     672             :         {
     673    11282690 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] - nt;
     674             :         }
     675       51542 :         hm_cfg->numPeakIndices = numPeakIndicesOrig;
     676             :     }
     677             : 
     678      182778 :     return nbits_old;
     679             : }
     680             : 
     681             : 
     682             : /* Range Coder Functions */
     683             : 
     684             : /*-------------------------------------------------------------------*
     685             :  * RCcontextMapping_encode2_no_mem_s17_LCS()
     686             :  *
     687             :  * Range encoder
     688             :  *-------------------------------------------------------------------*/
     689             : 
     690    12690604 : void RCcontextMapping_encode2_no_mem_s17_LCS(
     691             :     BSTR_ENC_HANDLE hBstr, /* i/o: bitstream handle            */
     692             :     int16_t *x,
     693             :     const int16_t nt,
     694             :     int16_t lastnz,
     695             :     const int16_t nbbits,
     696             :     const int16_t resQMaxBits,
     697             :     CONTEXT_HM_CONFIG *hm_cfg )
     698             : {
     699             :     int16_t ptr[BITBUFSIZE];
     700             :     RangeUniEncState rc_st_enc;
     701             :     int16_t bp;
     702             :     int16_t rc_tot_bits; /* No. of bits returned by range coder */
     703             :     int16_t pki, lev1;
     704             :     uint16_t t;
     705             :     int16_t rateFlag;
     706             :     int16_t value;
     707             :     int16_t nbbits_ntuples, nbbits_lsbs, nbbits_signs;
     708             :     int16_t *lsbs_bits;
     709             :     int16_t nt_half;
     710             :     int16_t signs[N_MAX];
     711             :     int16_t a1, b1, k;
     712             : 
     713    12690604 :     a1 = 0; /* to avoid compilation warnings */
     714    12690604 :     b1 = 0; /* to avoid compilation warnings */
     715             : 
     716             :     /* Init */
     717    12690604 :     nt_half = nt >> 1;
     718             : 
     719             :     /* Bits for encoding the number of encoded tuples */
     720    12690604 :     nbbits_ntuples = 0;
     721    12690604 :     k = 1;
     722   124803902 :     while ( k < nt / 2 )
     723             :     {
     724   112113298 :         nbbits_ntuples++;
     725   112113298 :         k = k << 1;
     726             :     }
     727             : 
     728    12690604 :     t = 0;
     729    12690604 :     nbbits_signs = 0;
     730    12690604 :     nbbits_lsbs = 0;
     731             : 
     732    12690604 :     if ( hm_cfg )
     733             :     {
     734             :         int16_t a1_i, b1_i;
     735             :         int32_t c[2], *ctx;
     736             :         int32_t p1, p2;
     737             :         int16_t ii[2];
     738             :         int16_t idx1, idx2, idx;
     739             :         int16_t numPeakIndicesOrig, numHoleIndices;
     740             : 
     741             :         /* Rate flag */
     742      144950 :         if ( nbbits > 400 )
     743             :         {
     744       57170 :             rateFlag = 2 << NBITS_CONTEXT;
     745             :         }
     746             :         else
     747             :         {
     748       87780 :             rateFlag = 0;
     749             :         }
     750             : 
     751      144950 :         c[0] = c[1] = 0;
     752             : 
     753             :         /* mapped domain */
     754      144950 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
     755      144950 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
     756      144950 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
     757             : 
     758             :         /* Mark hole indices beyond lastnz as pruned */
     759    50551892 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
     760             :         {
     761    50406942 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
     762             :         }
     763             : 
     764      144950 :         ii[0] = numPeakIndicesOrig;
     765      144950 :         ii[1] = 0;
     766             : 
     767      144950 :         p1 = p2 = 0; /* to avoid compilation warnings */
     768             : 
     769      144950 :         lsbs_bits = ptr + nbbits - 1;
     770             : 
     771             :         /*Start Encoding*/
     772             :         /* Initialize range encoder */
     773      144950 :         rc_uni_enc_init( &rc_st_enc );
     774             : 
     775             :         /*Main Loop through the 2-tuples*/
     776      144950 :         b1_i = -1;
     777             : 
     778    24297584 :         for ( k = 0; k < lastnz; k += 2 )
     779             :         {
     780    24152634 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
     781    24152634 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
     782             : 
     783    24152634 :             idx = min( idx1, idx2 );
     784             : 
     785             :             /* Get context */
     786    24152634 :             ctx = &c[p1 | p2];
     787             : 
     788    24152634 :             t = (uint16_t) ( *ctx + rateFlag );
     789    24152634 :             t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
     790             : 
     791             :             /* Init current 2-tuple encoding */
     792    24152634 :             a1 = (int16_t) abs( x[a1_i] );
     793    24152634 :             b1 = (int16_t) abs( x[b1_i] );
     794             : 
     795    24152634 :             lev1 = -1;
     796             : 
     797             :             /*Signs encoding*/
     798    24152634 :             if ( a1 > 0 )
     799             :             {
     800     5203251 :                 signs[nbbits_signs++] = ( (uint16_t) x[a1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
     801             :             }
     802             : 
     803    24152634 :             if ( b1 > 0 )
     804             :             {
     805     5178824 :                 signs[nbbits_signs++] = ( (uint16_t) x[b1_i] >> ( sizeof( uint16_t ) * 8 - 1 ) );
     806             :             }
     807             : 
     808             :             /* MSBs coding */
     809    26362525 :             while ( max( a1, b1 ) >= A_THRES )
     810             :             {
     811     2209891 :                 pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     812     2209891 :                 rc_uni_enc_encode_symbol_fastS( &rc_st_enc, VAL_ESC, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode ESC symbol */
     813             : 
     814     2209891 :                 *lsbs_bits-- = a1 & 1;
     815     2209891 :                 *lsbs_bits-- = b1 & 1;
     816             : 
     817             :                 /* LSBs bit counting */
     818     2209891 :                 nbbits_lsbs += 2;
     819             : 
     820     2209891 :                 a1 >>= 1;
     821     2209891 :                 b1 >>= 1;
     822             : 
     823     2209891 :                 lev1 = min( lev1 + 1, 2 );
     824             :             }
     825             : 
     826    24152634 :             pki = ari_lookup_s17_LC[t + ( ( lev1 + 1 ) << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     827    24152634 :             rc_uni_enc_encode_symbol_fastS( &rc_st_enc, a1 + A_THRES * b1, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode MSB symbol */
     828             : 
     829             :             /*Confirm that there is no overflow, i.e. bit-budget has not exceeded */
     830             : #ifdef DEBUGGING
     831             :             assert( rc_uni_enc_virtual_finish( &rc_st_enc ) <= nbbits - nbbits_signs - nbbits_lsbs - nbbits_ntuples );
     832             : #endif
     833             : 
     834             :             /* Update context for next 2-tuple */
     835    24152634 :             if ( p1 == p2 )
     836             :             {
     837             :                 /* peak-peak or hole-hole context */
     838             : 
     839    21943073 :                 if ( lev1 <= 0 )
     840             :                 {
     841    21540532 :                     t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
     842             :                 }
     843             :                 else
     844             :                 {
     845      402541 :                     t = 13 + lev1;
     846             :                 }
     847             : 
     848    21943073 :                 *ctx = ( *ctx & 0xf ) * 16 + t;
     849             :             }
     850             :             else
     851             :             {
     852             :                 /* mixed context */
     853             : 
     854     2209561 :                 if ( idx1 & 1 )
     855             :                 {
     856             :                     /* update first context */
     857     1093602 :                     c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
     858             :                 }
     859             : 
     860     2209561 :                 if ( idx2 & 1 )
     861             :                 {
     862             :                     /* update second context */
     863     1115959 :                     c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
     864             :                 }
     865             :             }
     866             : 
     867             :         } /*end of the 2-tuples loop*/
     868             :     }
     869             :     else /* if (!hm_cfg) */
     870             :     {
     871             :         int16_t cp;
     872             :         int16_t esc_nb, rateQ;
     873             :         uint16_t s;
     874             : 
     875             :         /* Rate flag */
     876    12545654 :         if ( nbbits > 400 )
     877             :         {
     878     8737909 :             rateFlag = 2;
     879             :         }
     880             :         else
     881             :         {
     882     3807745 :             rateFlag = 0;
     883             :         }
     884             : 
     885    12545654 :         s = 0;
     886             : 
     887             :         /* Find last non-zero tuple */
     888             :         /* ensure termination of while loop by dummy value */
     889    12545654 :         a1 = x[0];
     890    12545654 :         x[0] = 1; /* ensure first tuple is non-zero */
     891             : 
     892    97042346 :         while ( x[lastnz - 1] == 0 && x[lastnz - 2] == 0 )
     893             :         {
     894    84496692 :             lastnz -= 2;
     895             :         }
     896    12545654 :         x[0] = a1;
     897             : 
     898    12545654 :         lsbs_bits = ptr + nbbits - 1;
     899             : 
     900             :         /*Start Encoding*/
     901             :         /* Initialize range encoder */
     902    12545654 :         rc_uni_enc_init( &rc_st_enc );
     903             : 
     904             :         /*Main Loop through the 2-tuples*/
     905  2597526563 :         for ( k = 0; k < lastnz; k += 2 )
     906             :         {
     907             : 
     908             :             /* Init current 2-tuple encoding */
     909  2584980909 :             a1 = (int16_t) abs( x[k + 0] );
     910  2584980909 :             b1 = (int16_t) abs( x[k + 1] );
     911             : 
     912  2584980909 :             lev1 = 0;
     913  2584980909 :             esc_nb = 0;
     914             : 
     915             :             /*Signs encoding*/
     916  2584980909 :             if ( a1 > 0 )
     917             :             {
     918   997503252 :                 signs[nbbits_signs++] = ( (uint16_t) x[k + 0] >> ( sizeof( uint16_t ) * 8 - 1 ) );
     919             :             }
     920             : 
     921  2584980909 :             if ( b1 > 0 )
     922             :             {
     923   995953105 :                 signs[nbbits_signs++] = ( (uint16_t) x[k + 1] >> ( sizeof( uint16_t ) * 8 - 1 ) );
     924             :             }
     925             : 
     926  2584980909 :             rateQ = rateFlag + ( k > ( nt >> 1 ) );
     927             : 
     928             :             /* MSBs coding */
     929  3110544911 :             while ( max( a1, b1 ) >= A_THRES )
     930             :             {
     931   525564002 :                 pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     932   525564002 :                 rc_uni_enc_encode_symbol_fastS( &rc_st_enc, VAL_ESC, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode ESC symbol */
     933             : 
     934   525564002 :                 *lsbs_bits-- = a1 & 1;
     935   525564002 :                 *lsbs_bits-- = b1 & 1;
     936             : 
     937             :                 /* LSBs bit counting */
     938   525564002 :                 nbbits_lsbs += 2;
     939             : 
     940   525564002 :                 a1 >>= 1;
     941   525564002 :                 b1 >>= 1;
     942             : 
     943   525564002 :                 lev1++;
     944   525564002 :                 esc_nb = min( lev1, 3 );
     945             :             }
     946             : 
     947  2584980909 :             pki = ari_lookup_s17_LC[t + ( ( rateQ ) << NBITS_CONTEXT ) + ( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
     948  2584980909 :             rc_uni_enc_encode_symbol_fastS( &rc_st_enc, a1 + A_THRES * b1, cum_freq_ari_pk_s17_LC_ext[pki], sym_freq_ari_pk_s17_LC_ext[pki], 14 ); /* Encode MSB symbol */
     949             : 
     950             :             /*Confirm that there is no overflow, i.e. bit-budget has not exceeded */
     951             : #ifdef DEBUGGING
     952             :             assert( rc_uni_enc_virtual_finish( &rc_st_enc ) <= nbbits - nbbits_signs - nbbits_lsbs - nbbits_ntuples );
     953             : #endif
     954             : 
     955             :             /* Update context for next 2-tuple */
     956  2584980909 :             if ( esc_nb < 2 )
     957             :             {
     958  2453857115 :                 cp = 1 + ( ( a1 + b1 ) * ( esc_nb + 1 ) );
     959             :             }
     960             :             else
     961             :             {
     962   131123794 :                 cp = 12 + esc_nb;
     963             :             }
     964             : 
     965             :             /*Shift old 4 bits, replace last 4 bits*/
     966  2584980909 :             s = ( s << 4 ) + cp;
     967  2584980909 :             t = s & 0xFF;
     968             : 
     969             :         } /*end of the 2-tuples loop*/
     970             :     }
     971             : 
     972             :     /* Finish range encoder */
     973    12690604 :     rc_tot_bits = rc_uni_enc_finish( &rc_st_enc ); /* No. of bits consumed by range coder */
     974    12690604 :     bp = rc_tot_bits + nbbits_ntuples;             /* Update bitstream pointer */
     975             : 
     976             :     /* Cross-check that there is no overflow */
     977             : #ifdef DEBUGGING
     978             :     assert( k == lastnz );
     979             : #endif
     980             : 
     981             :     /* Push number of encoded tuples */
     982    12690604 :     value = ( lastnz >> 1 ) - 1;
     983    12690604 :     push_next_indice( hBstr, value, nbbits_ntuples );
     984             : 
     985             :     /* Push range coded bits from byte_buffer to bitstream */
     986             :     /* 1) Push all complete bytes, one byte at a time */
     987   901298624 :     for ( k = 0; k < ( rc_tot_bits >> 3 ); k++ )
     988             :     {
     989   888608020 :         push_next_indice( hBstr, rc_st_enc.byte_buffer[k], 8 );
     990             :     }
     991             :     /* 2) Push remaining bits */
     992    12690604 :     if ( ( rc_tot_bits & 7 ) != 0 )
     993             :     {
     994    11136057 :         push_next_indice( hBstr, rc_st_enc.byte_buffer[k] >> ( 8 - ( rc_tot_bits & 7 ) ), rc_tot_bits & 7 );
     995             :     }
     996             : 
     997             :     /* Push sign bits */
     998    12690604 :     push_next_bits( hBstr, (uint16_t *) signs, nbbits_signs );
     999    12690604 :     bp += nbbits_signs;
    1000             : 
    1001             :     /*write residual Quantization bits*/
    1002             : 
    1003   108598911 :     for ( k = 0; k < min( nbbits - bp - nbbits_lsbs, resQMaxBits ); k++ )
    1004             :     {
    1005    95908307 :         ptr[nbbits - 1 - nbbits_lsbs - k] = x[nt + k];
    1006             :     }
    1007             :     /* Write filler bits */
    1008   651251068 :     for ( ; k < nbbits - bp - nbbits_lsbs; ++k )
    1009             :     {
    1010   638560464 :         ptr[nbbits - 1 - nbbits_lsbs - k] = 0;
    1011             :     }
    1012             : 
    1013             :     /* Check for debugging */
    1014    12690604 :     assert( bp + k <= nbbits );
    1015             : 
    1016             :     /* Push the rest of the buffer */
    1017    12690604 :     push_next_bits( hBstr, (uint16_t *) &ptr[bp], nbbits - bp );
    1018             : 
    1019             :     /* return (bp+nbbits_lsbs);*/ /*return only for debug plot*/
    1020    12690604 :     return;
    1021             : }
    1022             : 
    1023             : /*-------------------------------------------------------------------*
    1024             :  * RCcontextMapping_encode2_estimate_no_mem_s17_LCS()
    1025             :  *
    1026             :  * Range coder bit-estimation
    1027             :  *-------------------------------------------------------------------*/
    1028             : 
    1029    56301477 : int16_t RCcontextMapping_encode2_estimate_no_mem_s17_LCS(
    1030             :     int16_t *x,       /* Spectral coefficients */
    1031             :     const int16_t nt, /* L - size of spectrum (no. of spectral coefficients) */
    1032             :     int16_t *lastnz_out,
    1033             :     int16_t *nEncoded,    /* No. of spectral coefficients that can be coded without an overflow occuring */
    1034             :     const int16_t target, /* Target bits */
    1035             :     int16_t *stop,
    1036             :     int16_t mode,
    1037             :     CONTEXT_HM_CONFIG *hm_cfg /* context-based harmonic model configuration */
    1038             : )
    1039             : {
    1040             :     /* Common variables */
    1041             :     int16_t a1, b1;
    1042             :     int16_t k, pki, lev1;
    1043             :     uint16_t t;
    1044             :     int16_t lastnz, lastnz2;
    1045             :     int16_t rateFlag;
    1046             :     float bit_estimate;
    1047             :     int16_t symbol;
    1048             :     const uint8_t *lookup;
    1049             :     float nbits2;
    1050             : 
    1051             :     /* Initialization */
    1052    56301477 :     bit_estimate = 2.0f;
    1053    56301477 :     nbits2 = 0.f;
    1054             : 
    1055             :     /* bits to encode lastnz */
    1056    56301477 :     k = 1;
    1057             : 
    1058   549256611 :     while ( k < nt / 2 )
    1059             :     {
    1060   492955134 :         bit_estimate++;
    1061   492955134 :         k = k << 1;
    1062             :         /* check while condition */
    1063             :     }
    1064             : 
    1065    56301477 :     nbits2 = bit_estimate;
    1066             : 
    1067    56301477 :     if ( hm_cfg )
    1068             :     {
    1069             :         int16_t a1_i, b1_i;
    1070             :         int16_t stop2;
    1071             :         int16_t total_output_bits;
    1072             :         int16_t nt_half;
    1073             :         int32_t c[2], *ctx;
    1074             :         int32_t p1, p2;
    1075             :         int16_t ii[2];
    1076             :         int16_t idx1, idx2, idx;
    1077     4384563 :         int16_t numPeakIndicesOrig = 0, numHoleIndices = 0; /* only to avoid compiler warning */
    1078             : 
    1079             :         /* Rate flag */
    1080     4384563 :         if ( target > 400 )
    1081             :         {
    1082     2305635 :             rateFlag = 2 << NBITS_CONTEXT; /* Select context-A for higher bitrates */
    1083             :         }
    1084             :         else
    1085             :         {
    1086     2078928 :             rateFlag = 0; /* Select context-B for lower bitrates */
    1087             :         }
    1088             : 
    1089     4384563 :         nt_half = nt >> 1;
    1090     4384563 :         stop2 = 0;
    1091     4384563 :         c[0] = c[1] = 0;
    1092             : 
    1093             :         /* Find last non-zero tuple in the mapped domain signal */
    1094     4384563 :         lastnz = find_last_nz_pair( x, nt, hm_cfg );
    1095             : 
    1096     4384563 :         lastnz2 = 2;
    1097             : 
    1098             :         /* mapped domain */
    1099     4384563 :         numPeakIndicesOrig = hm_cfg->numPeakIndices;
    1100     4384563 :         hm_cfg->numPeakIndices = min( hm_cfg->numPeakIndices, lastnz );
    1101     4384563 :         numHoleIndices = lastnz - hm_cfg->numPeakIndices;
    1102             : 
    1103             :         /* Mark hole indices beyond lastnz as pruned */
    1104  1486772524 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
    1105             :         {
    1106  1482387961 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] + nt;
    1107             :         }
    1108             : 
    1109     4384563 :         ii[0] = numPeakIndicesOrig;
    1110     4384563 :         ii[1] = 0;
    1111             : 
    1112     4384563 :         p1 = p2 = 0; /* to avoid compilation warnings */
    1113             : 
    1114             :         /* Main Loop through the 2-tuples */
    1115   765582585 :         for ( k = 0; k < lastnz; k += 2 )
    1116             :         {
    1117   762616978 :             a1_i = get_next_coeff_mapped( ii, &p1, &idx1, hm_cfg );
    1118   762616978 :             b1_i = get_next_coeff_mapped( ii, &p2, &idx2, hm_cfg );
    1119             : 
    1120   762616978 :             idx = min( idx1, idx2 );
    1121             : 
    1122             :             /* Get context */
    1123   762616978 :             ctx = &c[p1 | p2];
    1124             : 
    1125   762616978 :             t = (uint16_t) ( *ctx + rateFlag );
    1126   762616978 :             t += ( nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
    1127             : 
    1128             :             /* Init current 2-tuple encoding */
    1129   762616978 :             a1 = (int16_t) abs( x[a1_i] );
    1130   762616978 :             b1 = (int16_t) abs( x[b1_i] );
    1131   762616978 :             lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
    1132             : 
    1133             :             /* Signs Bits */
    1134   762616978 :             bit_estimate += min( a1, 1 );
    1135   762616978 :             bit_estimate += min( b1, 1 );
    1136             : 
    1137             :             /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
    1138   762616978 :             lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
    1139             : 
    1140             :             /* check while condition */
    1141             :             /* MSBs coding */
    1142   846835900 :             while ( max( a1, b1 ) >= A_THRES )
    1143             :             {
    1144    84218922 :                 pki = lookup[lev1]; /* ESC symbol */
    1145             : 
    1146    84218922 :                 bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
    1147    84218922 :                 bit_estimate += 2; /* Add 2 LSB bits corresponding to the bit-plane */
    1148             : 
    1149    84218922 :                 ( a1 ) >>= 1;
    1150    84218922 :                 ( b1 ) >>= 1;
    1151             : 
    1152    84218922 :                 lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
    1153             : 
    1154             :                 /* check while condition */
    1155             :             }
    1156             : 
    1157   762616978 :             pki = lookup[lev1];
    1158             : 
    1159   762616978 :             symbol = a1 + A_THRES * b1;
    1160   762616978 :             bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
    1161             : 
    1162             :             /* Should we truncate? */
    1163   762616978 :             if ( bit_estimate > target )
    1164             :             {
    1165   102064954 :                 stop2 = 1;
    1166             : 
    1167   102064954 :                 if ( *stop )
    1168             :                 {
    1169     1418956 :                     break;
    1170             :                 }
    1171             :             }
    1172             :             else
    1173             :             {
    1174   660552024 :                 lastnz2 = b1_i + 1;
    1175   660552024 :                 nbits2 = bit_estimate;
    1176             :             }
    1177             : 
    1178             :             /* Update context for next 2-tuple */
    1179   761198022 :             if ( p1 == p2 ) /* peak-peak or hole-hole context */
    1180             :             {
    1181   639573971 :                 lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
    1182             : 
    1183   639573971 :                 if ( lev1 <= 0 )
    1184             :                 {
    1185   624189958 :                     t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
    1186             :                 }
    1187             :                 else
    1188             :                 {
    1189    15384013 :                     t = 13 + lev1;
    1190             :                 }
    1191             : 
    1192   639573971 :                 *ctx = ( *ctx & 0xf ) * 16 + t;
    1193             :             }
    1194             :             else
    1195             :             {
    1196             :                 /* mixed context */
    1197             : 
    1198   121624051 :                 if ( idx1 & 1 )
    1199             :                 {
    1200             :                     /* update first context */
    1201    60521612 :                     c[p1] = update_mixed_context( c[p1], (int16_t) abs( x[a1_i] ) );
    1202             :                 }
    1203             : 
    1204   121624051 :                 if ( idx2 & 1 )
    1205             :                 {
    1206             :                     /* update second context */
    1207    60866826 :                     c[p2] = update_mixed_context( c[p2], (int16_t) abs( x[b1_i] ) );
    1208             :                 }
    1209             :             }
    1210             : 
    1211             :         } /*end of the 2-tuples loop*/
    1212             : 
    1213     4384563 :         total_output_bits = (int16_t) ( bit_estimate + 0.5f );
    1214     4384563 :         if ( *stop )
    1215             :         {
    1216     1881214 :             total_output_bits = (int16_t) ( nbits2 + 0.5f );
    1217             :         }
    1218             : 
    1219     4384563 :         if ( stop2 )
    1220             :         {
    1221     2839627 :             stop2 = total_output_bits;
    1222             :         }
    1223     4384563 :         *nEncoded = lastnz2;
    1224     4384563 :         *stop = stop2; /* If zero, it means no overflow occured during bit-estimation */
    1225     4384563 :         *lastnz_out = lastnz;
    1226             : 
    1227             :         /* Restore hole indices beyond lastnz */
    1228  1486772524 :         for ( k = numHoleIndices; k < hm_cfg->numHoleIndices; ++k )
    1229             :         {
    1230  1482387961 :             hm_cfg->holeIndices[k] = hm_cfg->holeIndices[k] - nt;
    1231             :         }
    1232     4384563 :         hm_cfg->numPeakIndices = numPeakIndicesOrig;
    1233             : 
    1234     4384563 :         return (int16_t) ( nbits2 + 0.5f );
    1235             :     }
    1236             :     else /* if (!hm_cfg) */
    1237             :     {
    1238             :         int16_t esc_nb, cp, rateQ;
    1239             :         uint16_t s;
    1240             :         int16_t tot_bits2;
    1241    51916914 :         int16_t overflow_flag = 0;
    1242             : 
    1243             :         /* Rate flag */
    1244    51916914 :         if ( target > 400 )
    1245             :         {
    1246    38395099 :             rateFlag = 2;
    1247             :         }
    1248             :         else
    1249             :         {
    1250    13521815 :             rateFlag = 0; /* Select context-B for lower bitrates */
    1251             :         }
    1252             : 
    1253    51916914 :         t = 0;
    1254    51916914 :         s = 0;
    1255    51916914 :         cp = 0;
    1256    51916914 :         lastnz = 1;
    1257    51916914 :         lastnz2 = 0;
    1258    51916914 :         tot_bits2 = 0;
    1259             : 
    1260             :         /* Find last non-zero tuple in the mapped domain signal */
    1261  8480275325 :         for ( lastnz = ( nt - 2 ); lastnz >= 0; lastnz -= 2 )
    1262             :         {
    1263  8478725251 :             if ( ( x[lastnz] != 0 ) || ( x[lastnz + 1] != 0 ) )
    1264             :             {
    1265             :                 break;
    1266             :             }
    1267             :         }
    1268    51916914 :         lastnz += 2;
    1269    51916914 :         if ( lastnz < 2 )
    1270             :         {
    1271     1550074 :             lastnz = 2; /* At least one tuple is coded */
    1272             :         }
    1273             : 
    1274    51916914 :         lastnz2 = 2;
    1275             : 
    1276             :         /* Main Loop through the 2-tuples */
    1277 10939424937 :         for ( k = 0; k < lastnz; k += 2 )
    1278             :         {
    1279             :             /* Init current 2-tuple encoding */
    1280 10887508023 :             a1 = (int16_t) abs( x[k] );
    1281 10887508023 :             b1 = (int16_t) abs( x[k + 1] );
    1282 10887508023 :             lev1 = 0;
    1283 10887508023 :             esc_nb = 0;
    1284 10887508023 :             rateQ = rateFlag + ( k > ( nt >> 1 ) );
    1285             : 
    1286             :             /* Signs Bits */
    1287 10887508023 :             bit_estimate += min( a1, 1 );
    1288 10887508023 :             bit_estimate += min( b1, 1 );
    1289             : 
    1290             :             /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
    1291 10887508023 :             lookup = &ari_lookup_s17_LC[t + ( rateQ << NBITS_CONTEXT )];
    1292             : 
    1293             :             /* check while condition */
    1294             :             /* MSBs coding */
    1295 13229435017 :             while ( max( a1, b1 ) >= A_THRES )
    1296             :             {
    1297  2341926994 :                 pki = lookup[( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
    1298             : 
    1299  2341926994 :                 bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
    1300  2341926994 :                 bit_estimate += 2; /* Add 2 LSB bits corresponding to the bit-plane */
    1301             : 
    1302  2341926994 :                 ( a1 ) >>= 1;
    1303  2341926994 :                 ( b1 ) >>= 1;
    1304             : 
    1305  2341926994 :                 lev1++;
    1306  2341926994 :                 esc_nb = min( lev1, 3 );
    1307             : 
    1308             :                 /* check while condition */
    1309             :             }
    1310             : 
    1311 10887508023 :             pki = lookup[( esc_nb << ( NBITS_CONTEXT + NBITS_RATEQ ) )];
    1312             : 
    1313 10887508023 :             symbol = a1 + A_THRES * b1;
    1314 10887508023 :             bit_estimate = bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
    1315             : 
    1316             :             /* Should we truncate? */
    1317 10887508023 :             if ( bit_estimate > target ) /* Overflow occured */
    1318             :             {
    1319   416022612 :                 overflow_flag = 1;
    1320             :             }
    1321             :             else
    1322             :             {
    1323 10471485411 :                 if ( abs( x[k] ) || abs( x[k + 1] ) ) /* No overflow & non-zero tuple */
    1324             :                 {
    1325  6064471610 :                     nbits2 = bit_estimate;
    1326  6064471610 :                     lastnz2 = k + 2;
    1327             :                 }
    1328             :             }
    1329             : 
    1330             :             /* Update context for next 2-tuple */
    1331 10887508023 :             if ( esc_nb < 2 )
    1332             :             {
    1333 10301908554 :                 cp = 1 + ( a1 + b1 ) * ( esc_nb + 1 );
    1334             :             }
    1335             :             else
    1336             :             {
    1337   585599469 :                 cp = 12 + esc_nb;
    1338             :             }
    1339             :             /*shift old bits and replace last 4 bits*/
    1340 10887508023 :             s = ( s << 4 ) + cp;
    1341 10887508023 :             t = s & 0xFF;
    1342             : 
    1343             :         } /*end of the 2-tuples loop*/
    1344             : 
    1345    51916914 :         tot_bits2 = (int16_t) ( nbits2 + 0.5f );
    1346    51916914 :         if ( lastnz2 < lastnz ) /* Overflow occured because unable to code all tuples */
    1347             :         {
    1348    19705960 :             overflow_flag = 1;
    1349             :         }
    1350    51916914 :         if ( mode == -1 )
    1351             :         {
    1352           0 :             tot_bits2 = (int16_t) ( bit_estimate + 0.5f );
    1353             :         }
    1354    51916914 :         if ( overflow_flag == 0 ) /* No overflow */
    1355             :         {
    1356    32210954 :             *stop = 0;
    1357             :         }
    1358             :         else /* Overflow */
    1359             :         {
    1360    19705960 :             if ( *stop )
    1361             :             {
    1362       93300 :                 *stop = tot_bits2;
    1363             :             }
    1364             :             else
    1365             :             {
    1366    19612660 :                 *stop = (int16_t) ( bit_estimate + 0.5f );
    1367             :             }
    1368             :         }
    1369             : 
    1370    51916914 :         *lastnz_out = lastnz;
    1371    51916914 :         *nEncoded = lastnz2;
    1372             :         /* Safety mechanism to avoid overflow */
    1373    51916914 :         if ( lastnz2 == 2 && overflow_flag == 1 )
    1374             :         {
    1375      122568 :             for ( k = 0; k < lastnz2; k++ )
    1376             :             {
    1377       81712 :                 x[k] = 0;
    1378             :             }
    1379             :         }
    1380             : 
    1381    51916914 :         return tot_bits2;
    1382             :     }
    1383             : }
    1384             : 
    1385             : /*-------------------------------------------------------------------*
    1386             :  * RCcontextMapping_encode2_estimate_bandWise_start()
    1387             :  *
    1388             :  * Range coder - start bandwise bit-estimation
    1389             :  *-------------------------------------------------------------------*/
    1390             : 
    1391     9529928 : int16_t RCcontextMapping_encode2_estimate_bandWise_start(
    1392             :     int16_t *x,
    1393             :     const int16_t nt,
    1394             :     const int16_t target,
    1395             :     HANDLE_RC_CONTEXT_MEM hContextMem )
    1396             : {
    1397             :     int16_t i, k;
    1398             : 
    1399             :     /* Rate flag */
    1400     9529928 :     if ( target > 400 )
    1401             :     {
    1402     9337368 :         hContextMem->rateFlag = 2 << NBITS_CONTEXT;
    1403             :     }
    1404             :     else
    1405             :     {
    1406      192560 :         hContextMem->rateFlag = 0;
    1407             :     }
    1408             : 
    1409     9529928 :     hContextMem->bit_estimate = 2.0f;
    1410             : 
    1411             :     /* Init */
    1412     9529928 :     hContextMem->nt_half = nt >> 1;
    1413             : 
    1414             :     /* bits to encode lastnz */
    1415     9529928 :     k = 1;
    1416             : 
    1417    88670468 :     while ( k < hContextMem->nt_half )
    1418             :     {
    1419    79140540 :         hContextMem->bit_estimate++;
    1420             : 
    1421    79140540 :         k = k << 1;
    1422             :         /* check while condition */
    1423             :     }
    1424             : 
    1425             :     /* bits to encode lastnz */
    1426     9529928 :     hContextMem->nbits_old = (int16_t) hContextMem->bit_estimate;
    1427             : 
    1428     9529928 :     hContextMem->ctx = 0;
    1429     9529928 :     hContextMem->lastnz = 2;
    1430             : 
    1431             :     /* Find last non-zero tuple  */
    1432             : 
    1433   178785866 :     for ( i = nt; i >= 4; i -= 2 )
    1434             :     {
    1435             : 
    1436   178624138 :         if ( x[i - 2] != 0 || x[i - 1] != 0 )
    1437             :         {
    1438     9368200 :             hContextMem->lastnz = i;
    1439     9368200 :             break;
    1440             :         }
    1441             :     }
    1442             : 
    1443     9529928 :     return (int16_t) hContextMem->bit_estimate;
    1444             : }
    1445             : 
    1446             : /*-------------------------------------------------------------------*
    1447             :  * RCcontextMapping_encode2_estimate_bandWise()
    1448             :  *
    1449             :  * Range coder - bandwise bit-estimation
    1450             :  *-------------------------------------------------------------------*/
    1451             : 
    1452   367559112 : int16_t RCcontextMapping_encode2_estimate_bandWise(
    1453             :     int16_t *x,
    1454             :     const int16_t start_line,
    1455             :     const int16_t end_line,
    1456             :     HANDLE_RC_CONTEXT_MEM hContextMem )
    1457             : {
    1458             :     int16_t a1, b1, a1_i, b1_i;
    1459             :     int16_t k, pki, lev1;
    1460             :     uint16_t t;
    1461   367559112 :     int16_t bandBits = 0;
    1462             :     int16_t total_output_bits; /* No. of bits after finalization */
    1463             :     int16_t symbol;
    1464             :     const uint8_t *lookup;
    1465             :     int16_t idx;
    1466             : 
    1467             :     /* Main Loop through the 2-tuples */
    1468             :     /*hContextMem->nt_half = end_line >> 1;*/
    1469  2735206840 :     for ( k = start_line; k < min( hContextMem->lastnz, end_line ); k += 2 )
    1470             :     {
    1471  2367647728 :         a1_i = k;
    1472  2367647728 :         b1_i = k + 1;
    1473             : 
    1474  2367647728 :         idx = k;
    1475             : 
    1476             :         /* Get context */
    1477  2367647728 :         t = hContextMem->ctx + hContextMem->rateFlag;
    1478  2367647728 :         t += ( hContextMem->nt_half >= idx ) ? 0 : ( 1 << NBITS_CONTEXT );
    1479             : 
    1480             :         /* Init current 2-tuple encoding */
    1481  2367647728 :         a1 = (int16_t) abs( x[a1_i] );
    1482  2367647728 :         b1 = (int16_t) abs( x[b1_i] );
    1483  2367647728 :         lev1 = -( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
    1484             : 
    1485             :         /* Signs Bits */
    1486  2367647728 :         hContextMem->bit_estimate += min( a1, 1 );
    1487  2367647728 :         hContextMem->bit_estimate += min( b1, 1 );
    1488             : 
    1489             :         /* pre-compute address of ari_pk_s17_LC_ext[0][Val_esc] to avoid doing it multiple times inside the loop */
    1490  2367647728 :         lookup = &ari_lookup_s17_LC[t] + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
    1491             : 
    1492             :         /* check while condition */
    1493             :         /* MSBs coding */
    1494  2910370383 :         while ( max( a1, b1 ) >= A_THRES )
    1495             :         {
    1496   542722655 :             pki = lookup[lev1];
    1497   542722655 :             hContextMem->bit_estimate = hContextMem->bit_estimate + ari_bit_estimate_s17_LC[pki][VAL_ESC];
    1498   542722655 :             hContextMem->bit_estimate += 2; /* Add the 2 LSB bits that were shifted out */
    1499             : 
    1500   542722655 :             ( a1 ) >>= 1;
    1501   542722655 :             ( b1 ) >>= 1;
    1502             : 
    1503   542722655 :             lev1 = min( lev1 + ( 1 << ( NBITS_CONTEXT + NBITS_RATEQ ) ), 2 << ( NBITS_CONTEXT + NBITS_RATEQ ) );
    1504             :             /* check while condition */
    1505             :         }
    1506             : 
    1507  2367647728 :         pki = lookup[lev1];
    1508  2367647728 :         symbol = a1 + A_THRES * b1; /* MSB symbol */
    1509  2367647728 :         hContextMem->bit_estimate = hContextMem->bit_estimate + ari_bit_estimate_s17_LC[pki][symbol];
    1510             : 
    1511             :         /* Update context */
    1512  2367647728 :         lev1 >>= NBITS_CONTEXT + NBITS_RATEQ;
    1513             : 
    1514  2367647728 :         if ( lev1 <= 0 )
    1515             :         {
    1516  2232698966 :             t = 1 + ( a1 + b1 ) * ( lev1 + 2 );
    1517             :         }
    1518             :         else
    1519             :         {
    1520   134948762 :             t = 13 + lev1;
    1521             :         }
    1522             : 
    1523  2367647728 :         hContextMem->ctx = ( hContextMem->ctx & 0xf ) * 16 + t;
    1524             : 
    1525             :     } /*end of the 2-tuples loop*/
    1526   367559112 :     total_output_bits = (int16_t) ( hContextMem->bit_estimate + 0.5f );
    1527             : 
    1528   367559112 :     bandBits = total_output_bits - hContextMem->nbits_old;
    1529   367559112 :     hContextMem->nbits_old = total_output_bits;
    1530             : 
    1531   367559112 :     return bandBits;
    1532             : }

Generated by: LCOV version 1.14