LCOV - code coverage report
Current view: top level - lib_dec - hdecnrm.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 102 118 86.4 %
Date: 2025-05-23 08:37:30 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             : /*====================================================================================
      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 "prot.h"
      44             : #include "rom_com.h"
      45             : #include "rom_dec.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : 
      49             : /*--------------------------------------------------------------------------*/
      50             : /*  Function  decode_huff_context()                                         */
      51             : /*                                                                          */
      52             : /*  Context based Huffman decoding for indices of quantized norms           */
      53             : /*--------------------------------------------------------------------------*/
      54             : 
      55             : /*! r: decoded index */
      56      415497 : static int16_t decode_huff_context(
      57             :     Decoder_State *st,      /* i/o: decoder state structure     */
      58             :     const int16_t *hufftab, /* i  : Huffman table               */
      59             :     int16_t *rbits          /* i/o: the number of read bits     */
      60             : )
      61             : {
      62     1276092 :     while ( *hufftab > 0 )
      63             :     {
      64      860595 :         *rbits += ( *hufftab & 0xf );
      65      860595 :         hufftab += ( *hufftab >> 4 ) + get_next_indice( st, *hufftab & 0xf );
      66             :     }
      67             : 
      68      415497 :     return ( -*hufftab );
      69             : }
      70             : 
      71             : 
      72             : /*--------------------------------------------------------------------------*/
      73             : /*  Function  hdecnrm()                                                     */
      74             : /*                                                                          */
      75             : /*  Huffman decoding for indices of quantized norms                         */
      76             : /*--------------------------------------------------------------------------*/
      77             : 
      78        2961 : void hdecnrm(
      79             :     Decoder_State *st, /* i/o: decoder state structure     */
      80             :     const int16_t N,   /* i  : number of norms             */
      81             :     int16_t *index     /* o  : indices of quantized norms  */
      82             : )
      83             : {
      84             :     int16_t i, j, k, n, m;
      85             :     int16_t temp;
      86             :     int16_t *pidx;
      87             : 
      88        2961 :     pidx = index;
      89             : 
      90        2961 :     m = N - 1;
      91       83166 :     for ( i = 0; i < m; i++ )
      92             :     {
      93       80205 :         j = 0;
      94       80205 :         k = 0;
      95       80205 :         if ( get_next_indice_1( st ) )
      96             :         {
      97       39105 :             j = 1;
      98             :         }
      99             : 
     100       80205 :         if ( get_next_indice_1( st ) )
     101             :         {
     102       40602 :             k = 1;
     103             :         }
     104       80205 :         n = j * 2 + k;
     105       80205 :         j = j * 4;
     106       80205 :         temp = 16 + n - j;
     107             : 
     108       80205 :         if ( get_next_indice_1( st ) )
     109             :         {
     110       44904 :             temp = 12 + n + j;
     111             : 
     112       44904 :             if ( get_next_indice_1( st ) )
     113             :             {
     114       20199 :                 j = 0;
     115       20199 :                 if ( get_next_indice_1( st ) )
     116             :                 {
     117        8877 :                     j = 1;
     118             :                 }
     119             : 
     120       20199 :                 temp = 8 + n;
     121             : 
     122       20199 :                 if ( j != 0 )
     123             :                 {
     124        8877 :                     temp += 12;
     125             :                 }
     126             : 
     127       20199 :                 if ( get_next_indice_1( st ) )
     128             :                 {
     129        3465 :                     temp = n;
     130             : 
     131        3465 :                     if ( get_next_indice_1( st ) )
     132             :                     {
     133        1518 :                         temp = n + 4;
     134             :                     }
     135             : 
     136        3465 :                     if ( j != 0 )
     137             :                     {
     138        1863 :                         temp += 24;
     139             :                     }
     140             :                 }
     141             :             }
     142             :         }
     143             : 
     144       80205 :         *pidx++ = temp;
     145             :     }
     146             : 
     147        2961 :     return;
     148             : }
     149             : 
     150             : /*--------------------------------------------------------------------------
     151             :  *  huff_dec()
     152             :  *
     153             :  *  Huffman decoding
     154             :  *--------------------------------------------------------------------------*/
     155             : 
     156        7173 : void huff_dec(
     157             :     Decoder_State *st,         /* i/o: decoder state structure                         */
     158             :     const int16_t N,           /* i  : Number of codewords to decode                   */
     159             :     const int16_t buffer_len,  /* i  : Number of bits to read                          */
     160             :     const int16_t num_lengths, /* i  : Number of different huffman codeword lengths    */
     161             :     const int16_t *thres,      /* i  : Threshold of first codeword of each length      */
     162             :     const int16_t *offset,     /* i  : Offset for first codeword                       */
     163             :     const int16_t *huff_tab,   /* i  : Huffman table order by codeword lengths         */
     164             :     int16_t *index             /* o  : Decoded index                                   */
     165             : )
     166             : {
     167             :     int16_t i, j, k;
     168             :     uint16_t val;
     169        7173 :     int16_t last_bits = buffer_len;
     170             : 
     171        7173 :     val = 0;
     172        7173 :     j = 0;
     173      127404 :     for ( i = 0; i < N; i++ )
     174             :     {
     175      120231 :         last_bits = buffer_len - j;
     176      120231 :         val <<= last_bits;
     177      120231 :         val &= ( 1 << buffer_len ) - 1; /* 0xFFF; */
     178      120231 :         val |= get_next_indice( st, last_bits );
     179             : 
     180             :         /* Find codeword length */
     181      120231 :         j = num_lengths - 1;
     182      265509 :         while ( val < thres[j] )
     183             :         {
     184      145278 :             j--;
     185             :         }
     186      120231 :         k = ( val - thres[j] ) >> j;
     187      120231 :         *index++ = huff_tab[offset[j] + k];
     188             :     }
     189             : 
     190             :     /* Put back unused bits */
     191        7173 :     st->next_bit_pos -= j;
     192             : 
     193        7173 :     return;
     194             : }
     195             : 
     196             : 
     197             : /*--------------------------------------------------------------------------
     198             :  *  hdecnrm_context()
     199             :  *
     200             :  *  Huffman decoding for indices of quantized norms
     201             :  *--------------------------------------------------------------------------*/
     202             : 
     203       14409 : void hdecnrm_context(
     204             :     Decoder_State *st, /* i/o: decoder state structure     */
     205             :     const int16_t N,   /* i  : number of norms             */
     206             :     int16_t *index,    /* o  : indices of quantized norms  */
     207             :     int16_t *n_length  /* o  : decoded stream length       */
     208             : )
     209             : {
     210             :     int16_t i, prevj;
     211             : 
     212       14409 :     prevj = index[0] + OFFSET_NORM;
     213      429906 :     for ( i = 1; i < N; i++ )
     214             :     {
     215      415497 :         if ( prevj > HTH_NORM )
     216             :         {
     217             :             /* above */
     218       87663 :             index[i] = decode_huff_context( st, hntable, n_length );
     219       87663 :             index[i] = 31 - index[i];
     220             :         }
     221             :         else
     222             :         {
     223      327834 :             if ( prevj < LTH_NORM )
     224             :             {
     225             :                 /* less */
     226       53517 :                 index[i] = decode_huff_context( st, hntable, n_length );
     227             :             }
     228             :             else
     229             :             {
     230             :                 /* equal */
     231      274317 :                 index[i] = decode_huff_context( st, hetable, n_length );
     232             :             }
     233             :         }
     234      415497 :         prevj = index[i];
     235             :     }
     236             : 
     237       14409 :     return;
     238             : }
     239             : 
     240             : 
     241             : /*--------------------------------------------------------------------------
     242             :  *  hdecnrm_resize()
     243             :  *
     244             :  *
     245             :  *--------------------------------------------------------------------------*/
     246             : 
     247        7053 : void hdecnrm_resize(
     248             :     Decoder_State *st, /* i/o: decoder state structure                 */
     249             :     const int16_t N,   /* i  : number of SFMs                          */
     250             :     int16_t *index     /* o  : norm quantization index vector          */
     251             : )
     252             : {
     253             :     int16_t i, j, k, m;
     254             :     int16_t temp;
     255             :     int16_t *pidx;
     256             : 
     257        7053 :     pidx = index;
     258             : 
     259        7053 :     m = N - 1;
     260      212559 :     for ( i = 0; i < m; i++ )
     261             :     {
     262      205506 :         j = 0;
     263      205506 :         k = 0;
     264             : 
     265      455427 :         for ( j = 0; j < 11; j++ )
     266             :         {
     267      455415 :             if ( get_next_indice_1( st ) )
     268             :             {
     269      249921 :                 k++;
     270             :             }
     271             :             else
     272             :             {
     273      205494 :                 break;
     274             :             }
     275             :         }
     276             : 
     277      205506 :         if ( k == 11 )
     278             :         {
     279          12 :             temp = 25;
     280             :         }
     281      205494 :         else if ( k == 10 )
     282             :         {
     283          36 :             temp = 5;
     284             :         }
     285      205458 :         else if ( k == 9 )
     286             :         {
     287          57 :             temp = 6;
     288             :         }
     289             :         else
     290             :         {
     291      205401 :             if ( get_next_indice_1( st ) )
     292             :             {
     293      100206 :                 temp = 16 + k;
     294             :             }
     295             :             else
     296             :             {
     297      105195 :                 temp = 15 - k;
     298             :             }
     299             :         }
     300             : 
     301      205506 :         *pidx++ = temp;
     302             :     }
     303             : 
     304        7053 :     return;
     305             : }
     306             : 
     307             : 
     308             : /*--------------------------------------------------------------------------
     309             :  * hdecnrm_trans()
     310             :  *
     311             :  * Huffman decoding for indices of quantized norms
     312             :  *--------------------------------------------------------------------------*/
     313             : 
     314           6 : void hdecnrm_tran(
     315             :     Decoder_State *st, /* i/o: decoder state structure     */
     316             :     const int16_t N,   /* i  : number of norms             */
     317             :     int16_t *index     /* o  : indices of quantized norms  */
     318             : )
     319             : {
     320             :     int16_t i, j, k, n, m;
     321             :     int16_t temp;
     322             :     int16_t *pidx;
     323             :     int16_t l;
     324             : 
     325           6 :     pidx = index;
     326             : 
     327           6 :     m = N - 1;
     328         192 :     for ( i = 0; i < m; i++ )
     329             :     {
     330         186 :         j = 0;
     331         186 :         k = 0;
     332         186 :         if ( get_next_indice_1( st ) )
     333             :         {
     334          90 :             j = 1;
     335             :         }
     336             : 
     337         186 :         if ( get_next_indice_1( st ) )
     338             :         {
     339          48 :             k = 1;
     340             :         }
     341             : 
     342         186 :         n = k * 2 + j;
     343         186 :         l = k * 4;
     344         186 :         if ( ( j == 0 && k == 0 ) || ( j == 1 && k == 0 ) || ( j == 1 && k == 1 ) )
     345             :         {
     346         180 :             temp = 15 + l - n;
     347             :         }
     348             :         else
     349             :         {
     350           6 :             if ( get_next_indice_1( st ) )
     351             :             {
     352           0 :                 temp = 15 + n - l;
     353             :             }
     354             :             else
     355             :             {
     356           6 :                 temp = 15 + l - n;
     357           6 :                 if ( get_next_indice_1( st ) )
     358             :                 {
     359           0 :                     for ( k = 0; k < 3; )
     360             :                     {
     361           0 :                         if ( get_next_indice_1( st ) )
     362             :                         {
     363           0 :                             k++;
     364             :                         }
     365             :                         else
     366             :                         {
     367           0 :                             break;
     368             :                         }
     369             :                     }
     370             : 
     371           0 :                     if ( k == 0 || k == 3 )
     372             :                     {
     373           0 :                         temp -= 5;
     374           0 :                         if ( k == 3 )
     375             :                         {
     376           0 :                             temp--;
     377             :                         }
     378             :                     }
     379           0 :                     else if ( k == 1 )
     380             :                     {
     381           0 :                         temp++;
     382             :                     }
     383             :                     else
     384             :                     {
     385           0 :                         temp += 2;
     386           0 :                         if ( get_next_indice_1( st ) )
     387             :                         {
     388           0 :                             temp++;
     389           0 :                             if ( get_next_indice_1( st ) )
     390             :                             {
     391           0 :                                 temp++;
     392             :                             }
     393             :                         }
     394             :                     }
     395             :                 }
     396             :             }
     397             :         }
     398             : 
     399         186 :         *pidx++ = temp;
     400             :     }
     401             : 
     402           6 :     return;
     403             : }

Generated by: LCOV version 1.14