LCOV - code coverage report
Current view: top level - lib_com - ivas_arith_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 98 106 92.5 %
Date: 2025-05-03 01:55:50 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             : #include "wmc_auto.h"
      36             : #include "prot_fx.h"
      37             : #include "ivas_prot_fx.h"
      38             : #include "stat_dec.h"
      39             : 
      40             : /*-----------------------------------------------------------------------------------------*
      41             :  * Function ivas_ari_start_decoding_14bits_ext_1_lfe()
      42             :  *
      43             :  * Start arithemetic coding
      44             :  *
      45             :  * Similar to ari_start_decoding_14bits_fx(), but do not read past frame boundary.
      46             :  *-----------------------------------------------------------------------------------------*/
      47             : 
      48      560908 : void ivas_ari_start_decoding_14bits_ext_1_lfe(
      49             :     Decoder_State *st,
      50             :     Tastat *s,
      51             :     Word16 *extra_bits_read )
      52             : {
      53             :     Word32 val;
      54             : 
      55      560908 :     IF( GE_16( st->bits_frame, add( st->next_bit_pos, cbitsnew ) ) )
      56             :     {
      57      560908 :         val = get_next_indice_fx( st, cbitsnew );
      58             :     }
      59             :     ELSE
      60             :     {
      61             :         Word16 rem_bits;
      62           0 :         rem_bits = sub( st->bits_frame, st->next_bit_pos );
      63           0 :         val = get_next_indice_fx( st, rem_bits );
      64           0 :         val = L_shl( val, sub( cbitsnew, rem_bits ) );
      65           0 :         *extra_bits_read = add( *extra_bits_read, sub( cbitsnew, rem_bits ) );
      66           0 :         move16();
      67             :     }
      68             : 
      69      560908 :     s->low = 0;
      70      560908 :     s->high = ari_q4new;
      71      560908 :     s->value = val;
      72             : 
      73      560908 :     move32();
      74      560908 :     move32();
      75      560908 :     move32();
      76             : 
      77      560908 :     return;
      78             : }
      79             : 
      80             : /* Helper function to determine the symbol. */
      81    11703352 : static UWord16 ivas_ari_get_symbol(
      82             :     const UWord16 *p,
      83             :     UWord32 range,
      84             :     UWord32 cum )
      85             : {
      86             :     UWord16 symbol;
      87             : 
      88    11703352 :     symbol = 0;
      89    11703352 :     move16();
      90             : 
      91    90655532 :     WHILE( GT_64( W_mult0_32_32( p[symbol + 1], range ), cum ) )
      92             :     {
      93    78952180 :         symbol = u_extract_l( UL_addNsD( symbol, 1 ) );
      94             :     }
      95             : 
      96    11703352 :     return symbol;
      97             : }
      98             : 
      99             : /*---------------------------------------------------------------
     100             :  * Function ivas_ari_decode_14bits_bit_ext_1_lfe()
     101             :  *
     102             :  *
     103             :  *-------------------------------------------------------------*/
     104             : 
     105    11703352 : UWord16 ivas_ari_decode_14bits_bit_ext_1_lfe(
     106             :     Decoder_State *st,
     107             :     Tastat *s,
     108             :     const UWord16 *cum_freq,
     109             :     Word16 *extra_bits_read )
     110             : {
     111             :     UWord16 symbol;
     112             :     UWord32 low, high, range, value;
     113             :     UWord32 cum;
     114             : 
     115             :     /* read s->low,high,value sequentially */
     116    11703352 :     low = s->low;
     117    11703352 :     high = s->high;
     118    11703352 :     value = s->value;
     119             : 
     120    11703352 :     move32();
     121    11703352 :     move32();
     122    11703352 :     move32();
     123             : 
     124    11703352 :     range = UL_addNsD( UL_subNsD( high, low ), 1 );
     125    11703352 :     cum = UL_subNsD( UL_lshl( ( UL_addNsD( UL_subNsD( value, low ), 1 ) ), stat_bitsnew ), 1 );
     126             : 
     127    11703352 :     symbol = ivas_ari_get_symbol( cum_freq, range, cum );
     128             : 
     129    11703352 :     high = UL_subNsD( UL_addNsD( low, mul_sbc_14bits( range, cum_freq[symbol] ) ), 1 );
     130    11703352 :     low = UL_addNsD( low, mul_sbc_14bits( range, cum_freq[symbol + 1] ) );
     131             : 
     132             :     FOR( ;; )
     133             :     {
     134    35137105 :         IF( GE_32( high, ari_q2new ) )
     135             :         {
     136    26825090 :             IF( GE_32( low, ari_q2new ) )
     137             :             {
     138     8362627 :                 value = UL_subNsD( value, ari_q2new );
     139     8362627 :                 low = UL_subNsD( low, ari_q2new );
     140     8362627 :                 high = UL_subNsD( high, ari_q2new );
     141             :             }
     142             :             ELSE
     143             :             {
     144    18462463 :                 test();
     145    18462463 :                 IF( GE_32( low, ari_q1new ) && LT_32( high, ari_q3new ) )
     146             :                 {
     147     6759111 :                     value = UL_subNsD( value, ari_q1new );
     148     6759111 :                     low = UL_subNsD( low, ari_q1new );
     149     6759111 :                     high = UL_subNsD( high, ari_q1new );
     150             :                 }
     151             :                 ELSE
     152             :                 {
     153             :                     BREAK;
     154             :                 }
     155             :             }
     156             :         }
     157             : 
     158    23433753 :         low = UL_addNsD( low, low );
     159    23433753 :         high = UL_addNsD( high, UL_addNsD( high, 1 ) );
     160             : 
     161    23433753 :         IF( GE_16( st->next_bit_pos, st->bits_frame ) )
     162             :         {
     163           0 :             value = UL_lshl( value, 1 );
     164           0 :             *extra_bits_read = add( *extra_bits_read, 1 );
     165           0 :             move16();
     166             :         }
     167             :         ELSE
     168             :         {
     169    23433753 :             value = UL_or( UL_lshl( value, 1 ), get_next_indice_1_fx( st ) );
     170             :         }
     171             :     }
     172             : 
     173    11703352 :     s->low = low;
     174    11703352 :     s->high = high;
     175    11703352 :     s->value = value;
     176             : 
     177    11703352 :     move32();
     178    11703352 :     move32();
     179    11703352 :     move32();
     180             : 
     181    11703352 :     return symbol;
     182             : }
     183             : 
     184             : /*---------------------------------------------------------------
     185             :  * Function ivas_ari_done_decoding_14bits_ext_1_lfe()
     186             :  *
     187             :  *
     188             :  *-------------------------------------------------------------*/
     189             : 
     190      560908 : void ivas_ari_done_decoding_14bits_ext_1_lfe(
     191             :     Decoder_State *st,
     192             :     const Word16 extra_bits_read )
     193             : {
     194      560908 :     get_next_indice_tmp( st, negate( sub( cbitsnew - 2, extra_bits_read ) ) );
     195             : 
     196      560908 :     return;
     197             : }
     198             : 
     199             : 
     200             : /*---------------------------------------------------------------
     201             :  * Function ivas_ari_done_encoding_14bits()
     202             :  *
     203             :  * Arith encoding of last table entry
     204             :  *-------------------------------------------------------------*/
     205      855804 : void ivas_ari_done_encoding_14bits_fx(
     206             :     BSTR_ENC_HANDLE hBstr,
     207             :     Tastat *s )
     208             : {
     209             :     Word32 low;
     210             :     Word32 bits_to_follow;
     211             : 
     212             :     /*  not needed, s points to s->low */
     213      855804 :     low = s->low;
     214      855804 :     move32();
     215      855804 :     bits_to_follow = L_add( s->bits_to_follow, 1 );
     216             : 
     217      855804 :     IF( LT_32( low, ari_q1new ) )
     218             :     {
     219      531853 :         push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     220             : 
     221     1671323 :         FOR( ; bits_to_follow > 0; bits_to_follow-- )
     222             :         {
     223     1139470 :             push_next_indice( hBstr, 1, 1 ); /*send a one*/
     224             :         }
     225             :     }
     226             :     ELSE
     227             :     {
     228      323951 :         push_next_indice( hBstr, 1, 1 ); /*send a one*/
     229      988021 :         FOR( ; bits_to_follow > 0; bits_to_follow-- )
     230             :         {
     231      664070 :             push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     232             :         }
     233             :     }
     234             : 
     235             :     /*It is done so no need to save values-> no counting*/
     236             :     /*s->low = low;
     237             :     s->bits_to_follow = bits_to_follow;*/
     238             : 
     239      855804 :     return;
     240             : }
     241             : 
     242             : /*---------------------------------------------------------------
     243             :  * Function ivas_ari_encode_14bits_ext()
     244             :  *
     245             :  * Arith encode function for extended proba tables
     246             :  *-------------------------------------------------------------*/
     247    17214038 : void ivas_ari_encode_14bits_ext_fx(
     248             :     BSTR_ENC_HANDLE hBstr,
     249             :     Tastat *s,
     250             :     Word32 symbol,
     251             :     const UWord16 *cum_freq )
     252             : {
     253             :     Word32 low, high, range;
     254             :     Word32 bits_to_follow;
     255             : 
     256             :     /*for all operation using bit_ptr=&ptr[bp]   */
     257             :     /* for reading s->high,low,bits_to_follow sequentially */
     258    17214038 :     high = s->high;
     259    17214038 :     low = s->low;
     260    17214038 :     range = L_add( L_sub( high, low ), 1 );
     261    17214038 :     move32();
     262    17214038 :     move32();
     263             : 
     264    17214038 :     high = L_sub( L_add( low, mul_sbc_14bits( range, cum_freq[symbol] ) ), 1 );
     265    17214038 :     low = L_add( low, mul_sbc_14bits( range, cum_freq[L_add( symbol, 1 )] ) );
     266             : 
     267    17214038 :     bits_to_follow = s->bits_to_follow;
     268    17214038 :     move32();
     269             : 
     270             :     FOR( ;; )
     271             :     {
     272    54788271 :         IF( LT_32( high, ari_q2new ) )
     273             :         {
     274    13300345 :             push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     275    18311784 :             FOR( ; bits_to_follow > 0; bits_to_follow-- )
     276             :             {
     277     5011439 :                 push_next_indice( hBstr, 1, 1 ); /*send a one*/
     278             :             }
     279             :         }
     280             :         ELSE
     281             :         {
     282    41487926 :             IF( GE_32( low, ari_q2new ) )
     283             :             {
     284    13550904 :                 push_next_indice( hBstr, 1, 1 ); /*send a one*/
     285    18308518 :                 FOR( ; bits_to_follow > 0; bits_to_follow-- )
     286             :                 {
     287     4757614 :                     push_next_indice( hBstr, 0, 1 ); /*send a zero*/
     288             :                 }
     289    13550904 :                 low = L_sub( low, ari_q2new );
     290    13550904 :                 high = L_sub( high, ari_q2new ); /* Subtract offset to top.  */
     291             :             }
     292             :             ELSE
     293             :             {
     294             :                 /* Output an opposite bit   */
     295    27937022 :                 test();
     296    27937022 :                 IF( GE_32( low, ari_q1new ) && LT_32( high, ari_q3new ) ) /* Output an opposite bit   */
     297             :                 {
     298             :                     /* later if in middle half. */
     299    10722984 :                     bits_to_follow = L_add( bits_to_follow, 1 );
     300    10722984 :                     low = L_sub( low, ari_q1new ); /* Subtract offset to middle*/
     301    10722984 :                     high = L_sub( high, ari_q1new );
     302             :                 }
     303             :                 ELSE
     304             :                 {
     305             :                     BREAK; /* Otherwise exit loop. */
     306             :                 }
     307             :             }
     308             :         }
     309    37574233 :         low = L_add( low, low );
     310    37574233 :         high = L_add( L_add( high, high ), 1 ); /* Scale up code range.     */
     311             :     }
     312             : 
     313    17214038 :     s->low = low;
     314    17214038 :     s->high = high;
     315    17214038 :     s->bits_to_follow = bits_to_follow;
     316    17214038 :     move32();
     317    17214038 :     move32();
     318    17214038 :     move32();
     319             : 
     320    17214038 :     return;
     321             : }

Generated by: LCOV version 1.14