LCOV - code coverage report
Current view: top level - lib_dec - dlpc_avq.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 102 105 97.1 %
Date: 2025-10-27 07:01:45 Functions: 4 4 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             : #include "prot.h"
      40             : #include "ivas_prot.h"
      41             : #include "wmc_auto.h"
      42             : 
      43             : /*------------------------------------------------------------------*
      44             :  * dlpc_avq()
      45             :  *
      46             :  * Variable bitrate multiple LPC un-quantizer
      47             :  *------------------------------------------------------------------*/
      48             : 
      49     1505607 : int16_t dlpc_avq(
      50             :     int16_t *index,       /* i  : Quantization indices        */
      51             :     float *LSF_Q,         /* o  : Quantized LSF vectors       */
      52             :     const int16_t numlpc, /* i  : Number of sets of lpc       */
      53             :     const int32_t sr_core /* i  : internal sampling rate      */
      54             : )
      55             : {
      56             :     int16_t i, nbi, last;
      57             :     int16_t *p_index, q_type;
      58             : 
      59             :     /* Last LPC index */
      60     1505607 :     if ( numlpc == 1 )
      61             :     {
      62     1468135 :         last = 0;
      63             :     }
      64             :     else
      65             :     {
      66       37472 :         last = M;
      67             :     }
      68             : 
      69     1505607 :     p_index = index;
      70             : 
      71             :     /* Decode last LPC */
      72    25595319 :     for ( i = 0; i < M; i++ )
      73             :     {
      74    24089712 :         LSF_Q[last + i] = 0.0f;
      75             :     }
      76             : 
      77     1505607 :     vlpc_1st_dec( p_index[0], &LSF_Q[last], sr_core );
      78     1505607 :     p_index++;
      79     1505607 :     vlpc_2st_dec( &LSF_Q[last], &p_index[0], 0, sr_core );
      80     1505607 :     nbi = 2 + p_index[0] + p_index[1];
      81     1505607 :     p_index += nbi;
      82             : 
      83             :     /* Decode intermediate LPC (512 framing) */
      84             : 
      85     1505607 :     if ( numlpc == 2 )
      86             :     {
      87       37472 :         q_type = p_index[0];
      88       37472 :         p_index++;
      89             : 
      90       37472 :         if ( q_type == 0 )
      91             :         {
      92             : 
      93      345899 :             for ( i = 0; i < M; i++ )
      94             :             {
      95      325552 :                 LSF_Q[i] = 0.0f;
      96             :             }
      97       20347 :             vlpc_1st_dec( p_index[0], &LSF_Q[0], sr_core );
      98       20347 :             p_index++;
      99       20347 :             vlpc_2st_dec( &LSF_Q[0], &p_index[0], 0, sr_core );
     100             :         }
     101       17125 :         else if ( q_type == 1 )
     102             :         {
     103      291125 :             for ( i = 0; i < M; i++ )
     104             :             {
     105      274000 :                 LSF_Q[i] = LSF_Q[M + i];
     106             :             }
     107             : 
     108       17125 :             vlpc_2st_dec( &LSF_Q[0], &p_index[0], 3, sr_core );
     109             :         }
     110             : 
     111       37472 :         nbi = 2 + p_index[0] + p_index[1];
     112       37472 :         p_index += nbi;
     113             :     }
     114             : 
     115     1505607 :     return (int16_t) ( p_index - index );
     116             : }
     117             : 
     118             : /*------------------------------------------------------------------*
     119             :  * unary_decode()
     120             :  *
     121             :  *
     122             :  *------------------------------------------------------------------*/
     123             : 
     124     2484243 : static int16_t unary_decode(
     125             :     Decoder_State *st,
     126             :     int16_t *ind )
     127             : {
     128             :     int16_t start_bit_pos;
     129             : 
     130     2484243 :     start_bit_pos = st->next_bit_pos;
     131             : 
     132             :     /* Index bits */
     133     2484243 :     *ind = 0;
     134             : 
     135     5762254 :     while ( get_next_indice_1( st ) && !st->BER_detect )
     136             :     {
     137     3278011 :         *ind += 1;
     138             :     }
     139             : 
     140     2484243 :     if ( *ind != 0 )
     141             :     {
     142     1836236 :         *ind += 1;
     143             :     }
     144             : 
     145     2484243 :     return st->next_bit_pos - start_bit_pos;
     146             : }
     147             : 
     148             : 
     149             : /*------------------------------------------------------------------*
     150             :  * pack4bits()
     151             :  *
     152             :  *
     153             :  *------------------------------------------------------------------*/
     154             : 
     155    20990908 : static int16_t pack4bits(
     156             :     int16_t nbits,
     157             :     Decoder_State *st,
     158             :     int16_t *prm )
     159             : {
     160             :     int16_t i;
     161             : 
     162    20990908 :     i = 0;
     163             : 
     164    60346288 :     while ( nbits > 4 )
     165             :     {
     166    39355380 :         prm[i] = get_next_indice( st, 4 );
     167    39355380 :         nbits -= 4;
     168    39355380 :         i++;
     169             :     }
     170    20990908 :     prm[i] = get_next_indice( st, nbits );
     171    20990908 :     i++;
     172             : 
     173    20990908 :     return ( i );
     174             : }
     175             : 
     176             : 
     177             : /*------------------------------------------------------------------*
     178             :  * decode_lpc_avq()
     179             :  *
     180             :  *
     181             :  *------------------------------------------------------------------*/
     182             : 
     183    10449048 : int16_t decode_lpc_avq(
     184             :     Decoder_State *st,            /* i/o: decoder state structure     */
     185             :     const int16_t numlpc,         /* i  : Number of sets of lpc       */
     186             :     int16_t *param_lpc,           /* o  : lpc parameters              */
     187             :     const int16_t ch,             /* i  : channel                     */
     188             :     const int16_t element_mode,   /* i  : element mode                */
     189             :     const int16_t sns_low_br_mode /* i  : SNS low-bitrate mode        */
     190             : )
     191             : {
     192             :     int16_t k, j;
     193             :     int16_t nb, qn1, qn2, avqBits, q_type;
     194             :     int16_t start_bit_pos;
     195    10449048 :     int16_t stereo_mode = 0;
     196             : 
     197    10449048 :     j = 0;
     198    10449048 :     start_bit_pos = st->next_bit_pos;
     199             : 
     200    21254856 :     for ( k = 0; k < numlpc; k++ )
     201             :     {
     202             :         /* Decode quantizer type */
     203             : 
     204    10805808 :         if ( k == 0 )
     205             :         {
     206    10449048 :             q_type = 0;
     207    10449048 :             nb = 0;
     208             :         }
     209             :         else
     210             :         {
     211      356760 :             nb = 1;
     212      356760 :             q_type = get_next_indice( st, nb );
     213      356760 :             param_lpc[j++] = q_type;
     214             :         }
     215             : 
     216    10805808 :         if ( element_mode == IVAS_CPE_MDCT && k == 0 )
     217             :         {
     218     8943441 :             if ( ch == 0 )
     219             :             {
     220     4897287 :                 stereo_mode = param_lpc[j];
     221             :             }
     222             :             else
     223             :             {
     224     4046154 :                 stereo_mode = ch;
     225             :             }
     226     8943441 :             param_lpc[j++] = stereo_mode;
     227             :         }
     228             : 
     229             :         /* Decode quantization indices */
     230             : 
     231    10805808 :         if ( q_type == 0 )
     232             :         {
     233             :             /* Absolute quantizer with 1st stage stochastic codebook */
     234    10598484 :             if ( element_mode == IVAS_CPE_MDCT )
     235             :             {
     236     9072530 :                 if ( stereo_mode != 3 )
     237             :                 {
     238     6358846 :                     param_lpc[j++] = get_next_indice( st, SNS_ABS_QUANT_BITS );
     239             :                 }
     240             :                 else
     241             :                 {
     242     2713684 :                     param_lpc[j++] = get_next_indice( st, 1 ) - 2;
     243             :                 }
     244             :             }
     245             :             else
     246             :             {
     247     1525954 :                 param_lpc[j++] = get_next_indice( st, LPC_ABS_QUANT_BITS );
     248             :             }
     249             :         }
     250             : 
     251             :         /*
     252             :          * 2nd stage decoding is skipped if:
     253             :          *   - we are in low bitrate mode and no joint SNS coding is used
     254             :          *   - OR the side-SNS-is-zero flag is set for joint SNS
     255             :          */
     256    10805808 :         if ( element_mode != IVAS_CPE_MDCT || ( !( sns_low_br_mode && ( stereo_mode == 0 || stereo_mode == 1 ) ) && !( q_type == 0 && param_lpc[j - 1] == -2 ) ) )
     257             :         {
     258             :             /* 2 bits to specify Q2,Q3,Q4,ext */
     259    10495454 :             qn1 = 2 + get_next_indice( st, 2 );
     260    10495454 :             qn2 = 2 + get_next_indice( st, 2 );
     261             : 
     262             :             /* Unary code */
     263             :             /* Q5 = 0, Q6=10, Q0=110, Q7=1110, ... */
     264             : 
     265    10495454 :             if ( qn1 > 4 )
     266             :             {
     267     1082539 :                 nb = unary_decode( st, &qn1 );
     268             : 
     269     1082539 :                 if ( nb == 1 )
     270      525714 :                     qn1 += 5;
     271      556825 :                 else if ( nb == 2 )
     272      391033 :                     qn1 += 4;
     273      165792 :                 else if ( nb == 3 )
     274      143814 :                     qn1 = 0;
     275             :                 else
     276       21978 :                     qn1 += 3;
     277             :             }
     278             : 
     279    10495454 :             if ( qn2 > 4 )
     280             :             {
     281     1401704 :                 nb = unary_decode( st, &qn2 );
     282             : 
     283     1401704 :                 if ( nb == 1 )
     284      122293 :                     qn2 += 5;
     285     1279411 :                 else if ( nb == 2 )
     286       84923 :                     qn2 += 4;
     287     1194488 :                 else if ( nb == 3 )
     288     1148714 :                     qn2 = 0;
     289             :                 else
     290       45774 :                     qn2 += 3;
     291             :             }
     292             : 
     293             :             /* check for potential bit errors */
     294    10495454 :             if ( qn1 > NB_SPHERE || qn2 > NB_SPHERE )
     295             :             {
     296           0 :                 qn1 = 0;
     297           0 :                 qn2 = 0;
     298           0 :                 st->BER_detect = 1;
     299             :             }
     300             : 
     301    10495454 :             param_lpc[j] = qn1;
     302    10495454 :             j++;
     303    10495454 :             param_lpc[j] = qn2;
     304    10495454 :             j++;
     305             : 
     306             :             /* Decode Split-by-2 algebraic VQ */
     307    10495454 :             avqBits = 4 * qn1;
     308    10495454 :             pack4bits( avqBits, st, &param_lpc[j] );
     309    10495454 :             j += qn1;
     310             : 
     311    10495454 :             avqBits = 4 * qn2;
     312    10495454 :             pack4bits( avqBits, st, &param_lpc[j] );
     313    10495454 :             j += qn2;
     314             :         }
     315             :         else
     316             :         {
     317      310354 :             param_lpc[j++] = 0;
     318      310354 :             param_lpc[j++] = 0;
     319             :         }
     320             :     }
     321             : 
     322    10449048 :     return st->next_bit_pos - start_bit_pos;
     323             : }

Generated by: LCOV version 1.14