LCOV - code coverage report
Current view: top level - lib_dec - range_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 53 55 96.4 %
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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include "cnst.h"
      43             : #include "rom_com.h"
      44             : #include "prot.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * Local prototypes
      49             :  *-------------------------------------------------------------------*/
      50             : 
      51             : static int16_t rc_dec_read( Decoder_State *st, PVQ_DEC_HANDLE hPVQ );
      52             : 
      53             : 
      54             : /*-------------------------------------------------------------------*
      55             :  * rc_dec_init()
      56             :  *
      57             :  * Initialize range coder
      58             :  *-------------------------------------------------------------------*/
      59             : 
      60       92424 : void rc_dec_init(
      61             :     Decoder_State *st,   /* i/o: Decoder State       */
      62             :     PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle  */
      63             :     int16_t tot_bits     /* i  : Total bit budget    */
      64             : )
      65             : {
      66             :     int16_t i;
      67             : 
      68       92424 :     hPVQ->rc_low = 0;
      69       92424 :     hPVQ->rc_range = 0xffffffff;
      70       92424 :     hPVQ->rc_num_bits = 0;
      71       92424 :     hPVQ->rc_offset = tot_bits + st->next_bit_pos;
      72       92424 :     hPVQ->rc_end = hPVQ->rc_offset;
      73             : 
      74      462120 :     for ( i = 0; i < 4; i++ )
      75             :     {
      76      369696 :         hPVQ->rc_low = ( hPVQ->rc_low << 8 ) + rc_dec_read( st, hPVQ );
      77             :     }
      78             : 
      79       92424 :     return;
      80             : }
      81             : 
      82             : /*-------------------------------------------------------------------*
      83             :  * rc_decode()
      84             :  *
      85             :  *  Decode symbol
      86             :  *-------------------------------------------------------------------*/
      87             : 
      88             : /*! r: Decoded value */
      89      979938 : uint32_t rc_decode(
      90             :     int16_t *BER_detect, /* o  : Bit error detection flag        */
      91             :     PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle              */
      92             :     uint32_t tot         /* i  : Total cumulative frequency      */
      93             : )
      94             : {
      95             :     uint32_t inv, val;
      96             :     int16_t exp;
      97             : 
      98      979938 :     inv = UL_inverse( tot, &exp );
      99      979938 :     hPVQ->rc_help = UMult_32_32( hPVQ->rc_range, inv );
     100      979938 :     hPVQ->rc_help = hPVQ->rc_help >> ( exp - 32 );
     101             : 
     102             :     /* safety check in case of bit errors */
     103      979938 :     val = hPVQ->rc_low / hPVQ->rc_help;
     104      979938 :     if ( val > tot )
     105             :     {
     106           0 :         *BER_detect = 1;
     107           0 :         return 0;
     108             :     }
     109             : 
     110      979938 :     return val;
     111             : }
     112             : 
     113             : /*-------------------------------------------------------------------*
     114             :  * rc_dec_update()
     115             :  *
     116             :  *  Update range coder
     117             :  *-------------------------------------------------------------------*/
     118             : 
     119      979938 : void rc_dec_update(
     120             :     Decoder_State *st,       /* i/o: Decoder State           */
     121             :     PVQ_DEC_HANDLE hPVQ,     /* i/o: PVQ decoder handle      */
     122             :     const uint32_t cum_freq, /* i  : Cumulative frequency    */
     123             :     const uint32_t sym_freq  /* i  : Symbol frequency        */
     124             : )
     125             : {
     126      979938 :     hPVQ->rc_low = hPVQ->rc_low - cum_freq * hPVQ->rc_help;
     127      979938 :     hPVQ->rc_range = hPVQ->rc_help * sym_freq;
     128             : 
     129     1771971 :     while ( hPVQ->rc_range < ( 1 << 24 ) )
     130             :     {
     131      792033 :         hPVQ->rc_num_bits += 8;
     132      792033 :         hPVQ->rc_low = ( hPVQ->rc_low << 8 ) + rc_dec_read( st, hPVQ );
     133      792033 :         hPVQ->rc_range <<= 8;
     134             :     }
     135             : 
     136      979938 :     return;
     137             : }
     138             : 
     139             : /*-------------------------------------------------------------------*
     140             :  * rc_dec_bits()
     141             :  *
     142             :  *  Decode bits
     143             :  *-------------------------------------------------------------------*/
     144             : 
     145             : /*! r: Decoded value */
     146     1552434 : uint32_t rc_dec_bits(
     147             :     Decoder_State *st,   /* i/o: Decoder State      */
     148             :     PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
     149             :     const int16_t bits   /* i  : Number of bits     */
     150             : )
     151             : {
     152             :     uint32_t value;
     153             : 
     154     1552434 :     hPVQ->rc_num_bits += bits;
     155             : 
     156     1552434 :     if ( bits > 16 )
     157             :     {
     158      139002 :         hPVQ->rc_offset -= bits - 16;
     159      139002 :         value = get_indice( st, hPVQ->rc_offset, bits - 16 ) << 16;
     160      139002 :         hPVQ->rc_offset -= 16;
     161      139002 :         value |= get_indice( st, hPVQ->rc_offset, 16 );
     162             :     }
     163             :     else
     164             :     {
     165     1413432 :         hPVQ->rc_offset -= bits;
     166     1413432 :         value = get_indice( st, hPVQ->rc_offset, bits );
     167             :     }
     168             : 
     169     1552434 :     return value;
     170             : }
     171             : 
     172             : /*-------------------------------------------------------------------*
     173             :  * rc_dec_uniform()
     174             :  *
     175             :  * Encode with uniform distribution
     176             :  *-------------------------------------------------------------------*/
     177             : 
     178             : /*! r: Decoded value   */
     179      885360 : uint32_t rc_dec_uniform(
     180             :     Decoder_State *st,   /* i/o: Decoder State   */
     181             :     PVQ_DEC_HANDLE hPVQ, /* i/o: PVQ decoder handle */
     182             :     const uint32_t tot   /* i  : Maximum value   */
     183             : )
     184             : {
     185             :     uint32_t value;
     186             :     int16_t n;
     187             : 
     188      885360 :     n = 32 - norm_ul( tot - 1 ); /* aligned to BASOP */
     189             : 
     190      885360 :     if ( n <= 8 )
     191             :     {
     192      218631 :         value = rc_decode( &st->BER_detect, hPVQ, tot );
     193      218631 :         rc_dec_update( st, hPVQ, value, 1 );
     194             :     }
     195             :     else
     196             :     {
     197      666729 :         n -= 8;
     198      666729 :         value = rc_decode( &st->BER_detect, hPVQ, ( tot >> n ) + 1 );
     199      666729 :         rc_dec_update( st, hPVQ, value, 1 );
     200      666729 :         value <<= n;
     201      666729 :         value |= rc_dec_bits( st, hPVQ, n );
     202             :     }
     203             : 
     204      885360 :     return value;
     205             : }
     206             : 
     207             : /*-------------------------------------------------------------------*
     208             :  * rc_dec_finish()
     209             :  *
     210             :  *  Finalize range decoder
     211             :  *-------------------------------------------------------------------*/
     212             : 
     213       92424 : void rc_dec_finish(
     214             :     Decoder_State *st,  /* i/o: Decoder State      */
     215             :     PVQ_DEC_HANDLE hPVQ /* i/o: PVQ decoder handle */
     216             : )
     217             : {
     218       92424 :     st->next_bit_pos = hPVQ->rc_end;
     219             : 
     220       92424 :     return;
     221             : }
     222             : 
     223             : 
     224             : /*-------------------------------------------------------------------*
     225             :  * rc_dec_read()
     226             :  *
     227             :  *  Read a byte from bitstream
     228             :  *-------------------------------------------------------------------*/
     229             : 
     230     1161729 : static int16_t rc_dec_read(
     231             :     Decoder_State *st,  /* i/o: Decoder state      */
     232             :     PVQ_DEC_HANDLE hPVQ /* i/o: PVQ decoder handle */
     233             : )
     234             : {
     235             :     int16_t bits;
     236             : 
     237     1161729 :     bits = hPVQ->rc_end - st->next_bit_pos;
     238             : 
     239             :     /* If the end of the buffer has been reached, pad the last byte with zeros */
     240     1161729 :     if ( bits < 8 )
     241             :     {
     242      115782 :         return ( get_next_indice( st, bits ) << ( 8 - bits ) );
     243             :     }
     244             :     else
     245             :     {
     246     1045947 :         return get_next_indice( st, 8 );
     247             :     }
     248             : }

Generated by: LCOV version 1.14