LCOV - code coverage report
Current view: top level - lib_enc - ari_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 134 135 99.3 %
Date: 2025-05-23 08:37:30 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 <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #include "cnst.h"
      41             : #include "prot.h"
      42             : #include "stat_com.h"
      43             : #include "basop_util.h"
      44             : #ifdef DEBUGGING
      45             : #include "debug.h"
      46             : #endif
      47             : #include "wmc_auto.h"
      48             : 
      49             : 
      50             : /*---------------------------------------------------------------
      51             :  * ari_copy_states()
      52             :  *
      53             :  * Copy state
      54             :  *-------------------------------------------------------------*/
      55             : 
      56    10910066 : void ari_copy_states(
      57             :     Tastat *source,
      58             :     Tastat *dest )
      59             : {
      60    10910066 :     dest->low = source->low;
      61    10910066 :     dest->high = source->high;
      62    10910066 :     dest->bits_to_follow = source->bits_to_follow;
      63             : 
      64    10910066 :     return;
      65             : }
      66             : 
      67             : /*---------------------------------------------------------------
      68             :   Ari encoder 14 bits routines
      69             :   -------------------------------------------------------------*/
      70             : 
      71             : /*---------------------------------------------------------------
      72             :  * ari_start_encoding_14bits()
      73             :  *
      74             :  * Start ArCo encoding
      75             :  *-------------------------------------------------------------*/
      76             : 
      77     2099349 : void ari_start_encoding_14bits(
      78             :     Tastat *s )
      79             : {
      80             :     /* : addressing is made with walking pointer s */
      81     2099349 :     s->low = 0;
      82     2099349 :     s->high = ari_q4new;
      83     2099349 :     s->bits_to_follow = 0;
      84             : 
      85     2099349 :     return;
      86             : }
      87             : 
      88             : 
      89             : /*---------------------------------------------------------------
      90             :  * ari_done_encoding_14bits()
      91             :  *
      92             :  * Finish ArCo encoding
      93             :  *-------------------------------------------------------------*/
      94             : 
      95     1199948 : int16_t ari_done_encoding_14bits(
      96             :     int16_t *ptr,
      97             :     int16_t bp,
      98             :     Tastat *s )
      99             : {
     100             :     int32_t low;
     101             :     int32_t bits_to_follow;
     102             : 
     103             :     /*  not needed, s points to s->low */
     104     1199948 :     low = s->low;
     105     1199948 :     bits_to_follow = s->bits_to_follow + 1;
     106             : 
     107     1199948 :     if ( low < ari_q1new )
     108             :     {
     109      763458 :         ptr[bp++] = 0; /*send a zero*/
     110     2280933 :         for ( ; bits_to_follow > 0; bits_to_follow-- )
     111             :         {
     112     1517475 :             ptr[bp++] = 1; /*send a one*/
     113             :         }
     114             :     }
     115             :     else
     116             :     {
     117      436490 :         ptr[bp++] = 1; /*send a one*/
     118     1327038 :         for ( ; bits_to_follow > 0; bits_to_follow-- )
     119             :         {
     120      890548 :             ptr[bp++] = 0; /*send a zero*/
     121             :         }
     122             :     }
     123             : 
     124             :     /*It is done so no need to save values-> no counting*/
     125             :     /*s->low = low;
     126             :     s->bits_to_follow = bits_to_follow;*/
     127             : 
     128     1199948 :     return bp;
     129             : }
     130             : 
     131             : 
     132             : /*---------------------------------------------------------------
     133             :  * ari_encode_14bits_ext()
     134             :  *
     135             :  * encode function for extended proba tables: less branches needed for coding
     136             :  *
     137             :  *-------------------------------------------------------------*/
     138             : 
     139     6317365 : int16_t ari_encode_14bits_ext(
     140             :     int16_t *ptr,
     141             :     int16_t bp,
     142             :     Tastat *s,
     143             :     int32_t symbol,
     144             :     const uint16_t *cum_freq )
     145             : {
     146             :     int32_t low, high, range;
     147             :     int32_t bits_to_follow;
     148             : 
     149             :     /*for all operation using bit_ptr=&ptr[bp]   */
     150             :     /* for reading s->high,low,bits_to_follow sequentially */
     151     6317365 :     high = s->high;
     152     6317365 :     low = s->low;
     153     6317365 :     range = high - low + 1;
     154             : 
     155     6317365 :     high = low + mul_sbc_14bits( range, cum_freq[symbol] ) - 1;
     156     6317365 :     low += mul_sbc_14bits( range, cum_freq[symbol + 1] );
     157             : 
     158     6317365 :     bits_to_follow = s->bits_to_follow;
     159             : 
     160             :     for ( ;; )
     161             :     {
     162    27179523 :         if ( high < ari_q2new )
     163             :         {
     164     7781660 :             ptr[bp++] = 0; /*send a zero*/
     165    10628488 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     166             :             {
     167     2846828 :                 ptr[bp++] = 1; /*send a one*/
     168             :             }
     169             :         }
     170             :         else
     171             :         {
     172    19397863 :             if ( low >= ari_q2new )
     173             :             {
     174     7927309 :                 ptr[bp++] = 1; /*send a one*/
     175     9384482 :                 for ( ; bits_to_follow > 0; bits_to_follow-- )
     176             :                 {
     177     1457173 :                     ptr[bp++] = 0; /*send a zero*/
     178             :                 }
     179     7927309 :                 low -= ari_q2new;
     180     7927309 :                 high -= ari_q2new; /* Subtract offset to top.  */
     181             :             }
     182             :             else
     183             :             {
     184             :                 /* Output an opposite bit   */
     185    11470554 :                 if ( low >= ari_q1new && high < ari_q3new ) /* Output an opposite bit   */
     186             :                 {
     187             :                     /* later if in middle half. */
     188     5153189 :                     bits_to_follow += 1;
     189     5153189 :                     low -= ari_q1new; /* Subtract offset to middle*/
     190     5153189 :                     high -= ari_q1new;
     191             :                 }
     192             :                 else
     193             :                 {
     194             :                     break; /* Otherwise exit loop. */
     195             :                 }
     196             :             }
     197             :         }
     198    20862158 :         low += low;
     199    20862158 :         high += high + 1; /* Scale up code range.     */
     200             :     }
     201             : 
     202     6317365 :     s->low = low;
     203     6317365 :     s->high = high;
     204     6317365 :     s->bits_to_follow = bits_to_follow;
     205             : 
     206     6317365 :     return bp;
     207             : }
     208             : 
     209             : 
     210             : /*------------------------------------------------------------------------
     211             :  * Function: ari_encode_14bits_range()
     212             :  *
     213             :  * Encode an cumulative frequency interval.
     214             :  *-------------------------------------------------------------------------*/
     215             : 
     216    10826387 : int16_t ari_encode_14bits_range(
     217             :     int16_t *ptr,
     218             :     int16_t bp,
     219             :     int32_t bits,
     220             :     Tastat *s,
     221             :     uint16_t cum_freq_low,
     222             :     uint16_t cum_freq_high )
     223             : {
     224             :     int32_t low, high, range;
     225             :     int32_t bits_to_follow;
     226             : 
     227             :     /*  not needed, s points to s->low */
     228    10826387 :     high = s->high;
     229    10826387 :     high++;
     230    10826387 :     low = s->low;
     231    10826387 :     range = high - low;
     232             : 
     233    10826387 :     high = low + mul_sbc_14bits( range, cum_freq_high );
     234    10826387 :     low += mul_sbc_14bits( range, cum_freq_low );
     235             : 
     236    10826387 :     bits_to_follow = s->bits_to_follow;
     237             : 
     238             :     /* while there are more than 16 bits left */
     239    12821896 :     for ( ; bp + 16 + bits_to_follow - bits < 0; )
     240             :     {
     241     5050769 :         if ( high <= ari_q2new )
     242             :         {
     243      654928 :             ptr[bp++] = 0; /*send a zero*/
     244      971541 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     245             :             {
     246      316613 :                 ptr[bp++] = 1; /*send a one*/
     247             :             }
     248             :         }
     249     4395841 :         else if ( low >= ari_q2new )
     250             :         {
     251             :             /* to reach this branch */
     252      721371 :             ptr[bp++] = 1; /*send a one*/
     253     1020831 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     254             :             {
     255      299460 :                 ptr[bp++] = 0; /*send a zero*/
     256             :             }
     257      721371 :             low -= ari_q2new;
     258      721371 :             high -= ari_q2new; /* Subtract offset to top.  */
     259             :         }
     260     3674470 :         else if ( low >= ari_q1new && high <= ari_q3new )
     261             :         {
     262             :             /* to reach this branch */
     263             :             /* Output an opposite bit   */
     264             :             /* later if in middle half. */
     265      619210 :             bits_to_follow += 1;
     266      619210 :             low -= ari_q1new; /* Subtract offset to middle*/
     267      619210 :             high -= ari_q1new;
     268             :         }
     269             :         else
     270             :         {
     271             :             /* to reach this branch */
     272             :             break; /* Otherwise exit loop.     */
     273             :         }
     274             : 
     275     1995509 :         low += low;
     276     1995509 :         high += high; /* Scale up code range.     */
     277             :     }
     278             :     /* if there are <= 16 bits left */
     279    10826387 :     if ( bp + 16 + bits_to_follow - bits >= 0 )
     280             :     {
     281             :         /* No need to do anyhing, but let's keep a place for a breakpoint */
     282     7771127 :         s->bits_to_follow = -1;
     283             :     }
     284             : 
     285    10826387 :     s->low = low;
     286    10826387 :     s->high = high - 1;
     287    10826387 :     s->bits_to_follow = bits_to_follow;
     288             : 
     289    10826387 :     return bp;
     290             : }
     291             : 
     292             : /*------------------------------------------------------------------------
     293             :  * Function: ari_encode_14bits_sign()
     294             :  *
     295             :  * Encode a sign with equal probabilities.
     296             :  *-------------------------------------------------------------------------*/
     297             : 
     298     2932831 : int16_t ari_encode_14bits_sign(
     299             :     int16_t *ptr,
     300             :     int16_t bp,
     301             :     int32_t bits,
     302             :     Tastat *s,
     303             :     int32_t sign )
     304             : {
     305             :     int32_t low, high, range;
     306             :     int32_t bits_to_follow;
     307             : 
     308             :     /*  not needed, s points to s->low */
     309     2932831 :     high = s->high;
     310     2932831 :     high++;
     311     2932831 :     low = s->low;
     312     2932831 :     range = high - low;
     313             : 
     314     2932831 :     if ( sign )
     315             :     {
     316     1471270 :         high = low + ( range >> 1 );
     317             :     }
     318             :     else
     319             :     {
     320     1461561 :         low += range >> 1;
     321             :     }
     322             : 
     323     2932831 :     bits_to_follow = s->bits_to_follow;
     324             : 
     325             :     /* while there are more than 16 bits left */
     326     5864884 :     for ( ; bp + 16 + bits_to_follow - bits < 0; )
     327             :     {
     328     5828036 :         if ( high <= ari_q2new )
     329             :         {
     330      566063 :             ptr[bp++] = 0; /*send a zero*/
     331     1053672 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     332             :             {
     333      487609 :                 ptr[bp++] = 1; /*send a one*/
     334             :             }
     335             :         }
     336     5261973 :         else if ( low >= ari_q2new )
     337             :         {
     338             :             /* to reach this branch */
     339      891132 :             ptr[bp++] = 1; /*send a one*/
     340     1511589 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     341             :             {
     342      620457 :                 ptr[bp++] = 0; /*send a zero*/
     343             :             }
     344      891132 :             low -= ari_q2new;
     345      891132 :             high -= ari_q2new; /* Subtract offset to top.  */
     346             :         }
     347     4370841 :         else if ( low >= ari_q1new && high <= ari_q3new )
     348             :         {
     349             :             /* to reach this branch */
     350             :             /* Output an opposite bit   */
     351             :             /* later if in middle half. */
     352     1474858 :             bits_to_follow += 1;
     353     1474858 :             low -= ari_q1new; /* Subtract offset to middle*/
     354     1474858 :             high -= ari_q1new;
     355             :         }
     356             :         else
     357             :         {
     358             :             /* to reach this branch */
     359             :             break; /* Otherwise exit loop.     */
     360             :         }
     361             : 
     362     2932053 :         low += low;
     363     2932053 :         high += high; /* Scale up code range.     */
     364             :     }
     365             : 
     366     2932831 :     s->low = low;
     367     2932831 :     s->high = high - 1;
     368     2932831 :     s->bits_to_follow = bits_to_follow;
     369             : 
     370     2932831 :     return bp;
     371             : }
     372             : 
     373             : /*------------------------------------------------------------------------
     374             :  * Function: ari_done_cbr_encoding_14bits()
     375             :  *
     376             :  * Finish up encoding in CBR mode.
     377             :  *-------------------------------------------------------------------------*/
     378             : 
     379       17457 : int16_t ari_done_cbr_encoding_14bits(
     380             :     int16_t *ptr,
     381             :     int16_t bp,
     382             :     int32_t bits,
     383             :     Tastat *s )
     384             : {
     385             :     int32_t high;
     386             :     int32_t bits_to_follow;
     387             :     uint16_t k;
     388             : 
     389       17457 :     while ( bits - bp - 16 - s->bits_to_follow > 0 )
     390             :     {
     391           0 :         bp = ari_encode_14bits_sign( ptr, bp, bits, s, 0 );
     392             :     }
     393             : 
     394             :     /*  not needed, s points to s->low */
     395       17457 :     high = s->high;
     396       17457 :     bits_to_follow = s->bits_to_follow;
     397             : 
     398       17457 :     if ( bits_to_follow )
     399             :     {
     400             :         /* If in upper half, then output a one, bits_to_follow zeros, and the remaining bits, except the first one */
     401        5516 :         if ( high < 0x8000 )
     402             :         {
     403        2775 :             ptr[bp++] = 0; /*send a zero*/
     404        8255 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     405             :             {
     406        5480 :                 ptr[bp++] = 1; /*send a one*/
     407             :             }
     408             :         }
     409             :         else
     410             :         {
     411        2741 :             ptr[bp++] = 1; /*send a one*/
     412        8303 :             for ( ; bits_to_follow > 0; bits_to_follow-- )
     413             :             {
     414        5562 :                 ptr[bp++] = 0; /*send a zero*/
     415             :             }
     416             :         }
     417             :         /* write remaining bits */
     418       82740 :         for ( k = 0x4000; k > 0; k >>= 1 )
     419             :         {
     420       82740 :             if ( k & high )
     421             :             {
     422       44848 :                 ptr[bp++] = 1; /*send a one*/
     423             :             }
     424             :             else
     425             :             {
     426       37892 :                 ptr[bp++] = 0; /*send a zero*/
     427             :             }
     428             : 
     429       82740 :             if ( bp >= bits )
     430             :             {
     431        5516 :                 break;
     432             :             }
     433             :         }
     434             :     }
     435             :     else
     436             :     {
     437             :         /* no carry-bits, just write all bits */
     438      191056 :         for ( k = 0x8000; k > 0; k >>= 1 )
     439             :         {
     440      191056 :             if ( k & high )
     441             :             {
     442      115039 :                 ptr[bp++] = 1; /*send a one*/
     443             :             }
     444             :             else
     445             :             {
     446       76017 :                 ptr[bp++] = 0; /*send a zero*/
     447             :             }
     448             : 
     449      191056 :             if ( bp >= bits )
     450             :             {
     451       11941 :                 break;
     452             :             }
     453             :         }
     454             :     }
     455             : 
     456       17457 :     return bp;
     457             : }

Generated by: LCOV version 1.14