LCOV - code coverage report
Current view: top level - lib_com - ivas_arith.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 79 85 92.9 %
Date: 2025-05-29 08:28:55 Functions: 6 6 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             : #include <stdint.h>
      34             : #include "options.h"
      35             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "wmc_auto.h"
      39             : #include "prot.h"
      40             : #include "ivas_prot.h"
      41             : #include "stat_dec.h"
      42             : 
      43             : /*-----------------------------------------------------------------------------------------*
      44             :  * Function ivas_ari_start_decoding_14bits_ext_1_lfe()
      45             :  *
      46             :  * Start arithemetic coding
      47             :  *
      48             :  * Similar to ari_start_decoding_14bits(), but do not read past frame boundary.
      49             :  *-----------------------------------------------------------------------------------------*/
      50             : 
      51    25165904 : void ivas_ari_start_decoding_14bits_ext_1_lfe(
      52             :     Decoder_State *st,
      53             :     Tastat *s,
      54             :     int16_t *extra_bits_read )
      55             : {
      56             :     int32_t val;
      57             : 
      58    25165904 :     if ( st->bits_frame >= ( st->next_bit_pos + cbitsnew ) )
      59             :     {
      60    25165904 :         val = get_next_indice( st, cbitsnew );
      61             :     }
      62             :     else
      63             :     {
      64             :         int16_t rem_bits;
      65           0 :         rem_bits = st->bits_frame - st->next_bit_pos;
      66           0 :         val = get_next_indice( st, rem_bits );
      67           0 :         val = ( val << ( cbitsnew - rem_bits ) );
      68           0 :         *extra_bits_read = *extra_bits_read + ( cbitsnew - rem_bits );
      69             :     }
      70             : 
      71    25165904 :     s->low = 0;
      72    25165904 :     s->high = ari_q4new;
      73    25165904 :     s->value = val;
      74             : 
      75    25165904 :     return;
      76             : }
      77             : 
      78             : 
      79             : /* Helper function to determine the symbol. */
      80   422979040 : static uint16_t ivas_ari_get_symbol(
      81             :     const uint16_t *p,
      82             :     uint32_t range,
      83             :     uint32_t cum )
      84             : {
      85             :     uint16_t symbol;
      86             : 
      87   422979040 :     symbol = 0;
      88             : 
      89  3332465855 :     while ( ( p[symbol + 1] * range ) > cum )
      90             :     {
      91  2909486815 :         symbol = symbol + 1;
      92             :     }
      93             : 
      94   422979040 :     return symbol;
      95             : }
      96             : 
      97             : 
      98             : /*---------------------------------------------------------------
      99             :  * Function ivas_ari_decode_14bits_bit_ext_1_lfe()
     100             :  *
     101             :  *
     102             :  *-------------------------------------------------------------*/
     103             : 
     104   422979040 : uint16_t ivas_ari_decode_14bits_bit_ext_1_lfe(
     105             :     Decoder_State *st,
     106             :     Tastat *s,
     107             :     const uint16_t *cum_freq,
     108             :     int16_t *extra_bits_read )
     109             : {
     110             :     uint16_t symbol;
     111             :     uint32_t low, high, range, value;
     112             :     uint32_t cum;
     113             : 
     114             :     /* read s->low,high,value sequentially */
     115   422979040 :     low = s->low;
     116   422979040 :     high = s->high;
     117   422979040 :     value = s->value;
     118             : 
     119   422979040 :     range = high - low + 1;
     120   422979040 :     cum = ( ( ( (uint32_t) ( value - low + 1 ) ) << stat_bitsnew ) - ( (uint32_t) 1 ) );
     121             : 
     122   422979040 :     symbol = ivas_ari_get_symbol( cum_freq, range, cum );
     123             : 
     124   422979040 :     high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
     125   422979040 :     low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
     126             : 
     127             :     for ( ;; )
     128             :     {
     129  1281193555 :         if ( high >= ari_q2new )
     130             :         {
     131   971875143 :             if ( low >= ari_q2new )
     132             :             {
     133   303917413 :                 value -= ari_q2new;
     134   303917413 :                 low -= ari_q2new;
     135   303917413 :                 high -= ari_q2new;
     136             :             }
     137             :             else
     138             :             {
     139   667957730 :                 if ( low >= ari_q1new && high < ari_q3new )
     140             :                 {
     141   244978690 :                     value -= ari_q1new;
     142   244978690 :                     low -= ari_q1new;
     143   244978690 :                     high -= ari_q1new;
     144             :                 }
     145             :                 else
     146             :                 {
     147             :                     break;
     148             :                 }
     149             :             }
     150             :         }
     151   858214515 :         low += low;
     152   858214515 :         high += high + 1;
     153             : 
     154   858214515 :         if ( st->next_bit_pos >= st->bits_frame )
     155             :         {
     156           0 :             value = ( value << 1 );
     157           0 :             *extra_bits_read = *extra_bits_read + 1;
     158             :         }
     159             :         else
     160             :         {
     161   858214515 :             value = ( value << 1 ) | get_next_indice_1( st );
     162             :         }
     163             :     }
     164             : 
     165   422979040 :     s->low = low;
     166   422979040 :     s->high = high;
     167   422979040 :     s->value = value;
     168             : 
     169   422979040 :     return symbol;
     170             : }
     171             : 
     172             : 
     173             : /*---------------------------------------------------------------
     174             :  * Function ivas_ari_done_decoding_14bits_ext_1_lfe()
     175             :  *
     176             :  *
     177             :  *-------------------------------------------------------------*/
     178             : 
     179    25165904 : void ivas_ari_done_decoding_14bits_ext_1_lfe(
     180             :     Decoder_State *st,
     181             :     const int16_t extra_bits_read )
     182             : {
     183    25165904 :     get_next_indice_tmp( st, -( cbitsnew - 2 - extra_bits_read ) );
     184             : 
     185    25165904 :     return;
     186             : }
     187             : 
     188             : 
     189             : /*---------------------------------------------------------------
     190             :  * Function ivas_ari_done_encoding_14bits()
     191             :  *
     192             :  * Arith encoding of last table entry
     193             :  *-------------------------------------------------------------*/
     194             : 
     195    14957774 : void ivas_ari_done_encoding_14bits(
     196             :     BSTR_ENC_HANDLE hBstr,
     197             :     Tastat *s )
     198             : {
     199             :     int32_t low;
     200             :     int32_t bits_to_follow;
     201             : 
     202             :     /*  not needed, s points to s->low */
     203    14957774 :     low = s->low;
     204    14957774 :     bits_to_follow = s->bits_to_follow + 1;
     205             : 
     206    14957774 :     if ( low < ari_q1new )
     207             :     {
     208     9373228 :         push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     209             : 
     210    29806360 :         for ( ; bits_to_follow > 0; bits_to_follow-- )
     211             :         {
     212    20433132 :             push_next_indice( hBstr, 1, 1 ); /*send a one*/
     213             :         }
     214             :     }
     215             :     else
     216             :     {
     217     5584546 :         push_next_indice( hBstr, 1, 1 ); /*send a one*/
     218    16868832 :         for ( ; bits_to_follow > 0; bits_to_follow-- )
     219             :         {
     220    11284286 :             push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     221             :         }
     222             :     }
     223             : 
     224             :     /*It is done so no need to save values-> no counting*/
     225             :     /*s->low = low;
     226             :     s->bits_to_follow = bits_to_follow;*/
     227             : 
     228    14957774 :     return;
     229             : }
     230             : 
     231             : 
     232             : /*---------------------------------------------------------------
     233             :  * Function ivas_ari_encode_14bits_ext()
     234             :  *
     235             :  * Arith encode function for extended proba tables
     236             :  *-------------------------------------------------------------*/
     237             : 
     238   250377331 : void ivas_ari_encode_14bits_ext(
     239             :     BSTR_ENC_HANDLE hBstr,
     240             :     Tastat *s,
     241             :     int32_t symbol,
     242             :     const uint16_t *cum_freq )
     243             : {
     244             :     int32_t low, high, range;
     245             :     int32_t bits_to_follow;
     246             : 
     247             :     /*for all operation using bit_ptr=&ptr[bp]   */
     248             :     /* for reading s->high,low,bits_to_follow sequentially */
     249   250377331 :     high = s->high;
     250   250377331 :     low = s->low;
     251   250377331 :     range = high - low + 1;
     252             : 
     253   250377331 :     high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
     254   250377331 :     low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
     255             : 
     256   250377331 :     bits_to_follow = s->bits_to_follow;
     257             : 
     258             :     for ( ;; )
     259             :     {
     260   795310285 :         if ( high < ari_q2new )
     261             :         {
     262   196376270 :             push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     263   270022319 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     264             :             {
     265    73646049 :                 push_next_indice( hBstr, 1, 1 ); /*send a one*/
     266             :             }
     267             :         }
     268             :         else
     269             :         {
     270   598934015 :             if ( low >= ari_q2new )
     271             :             {
     272   192432671 :                 push_next_indice( hBstr, 1, 1 ); /*send a one*/
     273   258063311 :                 for ( ; bits_to_follow > 0; bits_to_follow-- )
     274             :                 {
     275    65630640 :                     push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     276             :                 }
     277   192432671 :                 low -= ari_q2new;
     278   192432671 :                 high -= ari_q2new; /* Subtract offset to top.  */
     279             :             }
     280             :             else
     281             :             {
     282             :                 /* Output an opposite bit   */
     283   406501344 :                 if ( low >= ari_q1new && high < ari_q3new ) /* Output an opposite bit   */
     284             :                 {
     285             :                     /* later if in middle half. */
     286   156124013 :                     bits_to_follow += 1;
     287   156124013 :                     low -= ari_q1new; /* Subtract offset to middle*/
     288   156124013 :                     high -= ari_q1new;
     289             :                 }
     290             :                 else
     291             :                 {
     292             :                     break; /* Otherwise exit loop. */
     293             :                 }
     294             :             }
     295             :         }
     296   544932954 :         low += low;
     297   544932954 :         high += high + 1; /* Scale up code range.     */
     298             :     }
     299             : 
     300   250377331 :     s->low = low;
     301   250377331 :     s->high = high;
     302   250377331 :     s->bits_to_follow = bits_to_follow;
     303             : 
     304   250377331 :     return;
     305             : }

Generated by: LCOV version 1.14