LCOV - code coverage report
Current view: top level - lib_enc - qlpc_avq.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 129 133 97.0 %
Date: 2025-05-23 08:37:30 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             :  * qlpc_avq()
      45             :  *
      46             :  *
      47             :  *--------------------------------------------------------------------*/
      48             : 
      49      106898 : void qlpc_avq(
      50             :     const float *lsf,     /* i  : Input LSF vectors               */
      51             :     const float *lsfmid,  /* i  : Input mid-LSF vectors           */
      52             :     float *lsf_q,         /* o  : Quantized LFS vectors           */
      53             :     float *lsfmid_q,      /* o  : Quantized mid-LFS vectors       */
      54             :     int16_t *index,       /* o  : Quantization indices            */
      55             :     int16_t *nb_indices,  /* o  : Number of quantization indices  */
      56             :     int16_t *nbbits,      /* o  : Number of quantization bits     */
      57             :     const int16_t core,   /* i  : core                            */
      58             :     const int32_t sr_core /* i  : internal sampling rate          */
      59             : )
      60             : {
      61             :     int16_t i;
      62             :     float lsfmid_q0[M];
      63             :     int16_t *tmp_index, indxt[256], nbits, nbt, nit;
      64             :     float dummy[M];
      65             : 
      66             :     /* Init */
      67      106898 :     tmp_index = &index[0];
      68      106898 :     *nb_indices = 0;
      69             : 
      70             :     /* Quantize end_frame LPC */
      71      106898 :     set_f( lsf_q, 0, M );
      72             : 
      73      106898 :     tmp_index[0] = vlpc_1st_cod( lsf, lsf_q, sr_core, dummy );
      74             : 
      75      106898 :     nbt = vlpc_2st_cod( lsf, lsf_q, &tmp_index[1], 0, sr_core );
      76             : 
      77      106898 :     nit = 1 + 2 + index[1] + index[2];
      78      106898 :     tmp_index += nit;
      79      106898 :     *nb_indices += nit;
      80      106898 :     nbbits[0] = 8 + nbt;
      81             : 
      82      106898 :     *tmp_index = 0;
      83             : 
      84             :     /* Quantize mid-frame LPC */
      85      106898 :     if ( core == TCX_10_CORE )
      86             :     {
      87        2077 :         tmp_index++;
      88        2077 :         *nb_indices += 1;
      89             : 
      90             :         /* LPC2: Abs? */
      91        2077 :         set_f( lsfmid_q, 0, M );
      92             : 
      93        2077 :         tmp_index[0] = vlpc_1st_cod( lsfmid, lsfmid_q, sr_core, dummy );
      94             : 
      95        2077 :         nbits = vlpc_2st_cod( lsfmid, lsfmid_q, &tmp_index[1], 0, sr_core );
      96             : 
      97        2077 :         nbt = 8 + nbits;
      98        2077 :         nit = 1 + 2 + tmp_index[1] + tmp_index[2];
      99             : 
     100             :         /* LPC2: RelR? */
     101       35309 :         for ( i = 0; i < M; i++ )
     102             :         {
     103       33232 :             lsfmid_q0[i] = lsf_q[i];
     104             :         }
     105             : 
     106        2077 :         nbits = vlpc_2st_cod( lsfmid, lsfmid_q0, indxt, 3, sr_core );
     107             : 
     108        2077 :         if ( nbits < nbt )
     109             :         {
     110         921 :             nbt = nbits;
     111         921 :             nit = 2 + indxt[0] + indxt[1];
     112         921 :             tmp_index[-1] = 1;
     113             : 
     114       15657 :             for ( i = 0; i < M; i++ )
     115             :             {
     116       14736 :                 lsfmid_q[i] = lsfmid_q0[i];
     117             :             }
     118             : 
     119       10003 :             for ( i = 0; i < nit; i++ )
     120             :             {
     121        9082 :                 tmp_index[i] = indxt[i];
     122             :             }
     123             :         }
     124             : 
     125        2077 :         tmp_index += nit;
     126        2077 :         *nb_indices += nit;
     127        2077 :         nbbits[1] = 1 + nbt;
     128             :     }
     129             : 
     130      106898 :     return;
     131             : }
     132             : 
     133             : 
     134             : /*-------------------------------------------------------------------*
     135             :  * unary_code()
     136             :  *
     137             :  *
     138             :  *--------------------------------------------------------------------*/
     139             : 
     140      176164 : static int16_t unary_code( int16_t ind, BSTR_ENC_HANDLE hBstr )
     141             : {
     142             :     int16_t nb_bits;
     143             : 
     144      176164 :     nb_bits = 1;
     145             : 
     146             :     /* Index bits */
     147      176164 :     ind -= 1;
     148             : 
     149      176164 :     while ( ind >= 16 )
     150             :     {
     151           0 :         push_next_indice( hBstr, 0xffffU, 16 );
     152           0 :         nb_bits += 16;
     153           0 :         ind -= 16;
     154             :     }
     155      176164 :     if ( ind > 0 )
     156             :     {
     157      125779 :         push_next_indice( hBstr, ( 1U << ind ) - 1, ind );
     158      125779 :         nb_bits += ind;
     159             :     }
     160             : 
     161             :     /* Stop bit */
     162      176164 :     push_next_indice( hBstr, 0, 1 );
     163             : 
     164      176164 :     return ( nb_bits );
     165             : }
     166             : 
     167             : 
     168             : /*-------------------------------------------------------------------*
     169             :  * unpack4bits()
     170             :  *
     171             :  *
     172             :  *--------------------------------------------------------------------*/
     173             : 
     174     1286260 : static int16_t unpack4bits( int16_t nbits, const int16_t *prm, BSTR_ENC_HANDLE hBstr )
     175             : {
     176             :     int16_t i;
     177             : 
     178     1286260 :     if ( nbits == 0 )
     179             :     {
     180       92373 :         push_next_indice( hBstr, 0, 0 );
     181       92373 :         i = 1;
     182             :     }
     183             :     else
     184             :     {
     185     1193887 :         i = 0;
     186             : 
     187     3593361 :         while ( nbits > 4 )
     188             :         {
     189     2399474 :             push_next_indice( hBstr, prm[i], 4 );
     190     2399474 :             nbits -= 4;
     191     2399474 :             i++;
     192             :         }
     193     1193887 :         push_next_indice( hBstr, prm[i], nbits );
     194     1193887 :         i++;
     195             :     }
     196             : 
     197     1286260 :     return ( i );
     198             : }
     199             : 
     200             : 
     201             : /*-------------------------------------------------------------------*
     202             :  * encode_lpc_avq()
     203             :  *
     204             :  *
     205             :  *--------------------------------------------------------------------*/
     206             : 
     207      649272 : int16_t encode_lpc_avq(
     208             :     BSTR_ENC_HANDLE hBstr,     /* i/o: bitstream handle                                  */
     209             :     const int16_t numlpc,      /* i  : Number of sets of lpc                             */
     210             :     const int16_t *param_lpc,  /* i  : lpc parameters                                    */
     211             :     const int16_t core,        /* i  : core                                              */
     212             :     const int16_t element_mode /* i  : element mode - decides between SNS and LPC coding */
     213             : )
     214             : {
     215             :     int16_t k, j;
     216             :     int16_t q_type, nb_ind;
     217      649272 :     int16_t i, qn1, qn2, nb, avqBits, st1 = 0;
     218             :     int16_t nb_bits;
     219             :     int16_t stereo_mode, bits_for_abs_quant;
     220             : 
     221      649272 :     stereo_mode = 0;
     222             : 
     223      649272 :     if ( element_mode == IVAS_CPE_MDCT )
     224             :     {
     225      542374 :         bits_for_abs_quant = SNS_ABS_QUANT_BITS;
     226             :     }
     227             :     else
     228             :     {
     229      106898 :         bits_for_abs_quant = LPC_ABS_QUANT_BITS;
     230             :     }
     231             : 
     232      649272 :     j = 0;
     233      649272 :     nb_bits = 0;
     234             : 
     235     1313044 :     for ( k = 0; k < numlpc; k++ )
     236             :     {
     237             :         /* Retrieve quantizer type */
     238      663772 :         if ( k == 0 )
     239             :         {
     240      649272 :             q_type = 0;
     241             :         }
     242             :         else
     243             :         {
     244       14500 :             q_type = param_lpc[j++];
     245             :         }
     246             : 
     247      663772 :         if ( element_mode == IVAS_CPE_MDCT && k == 0 )
     248             :         {
     249      542374 :             stereo_mode = param_lpc[j++];
     250             :         }
     251             : 
     252             :         /* Determine number of AVQ indices */
     253      663772 :         nb_ind = 0;
     254             : 
     255      663772 :         if ( q_type == 0 )
     256             :         {
     257      655016 :             st1 = param_lpc[j++];
     258             :         }
     259      663772 :         qn1 = param_lpc[j++];
     260      663772 :         qn2 = param_lpc[j++];
     261      663772 :         if ( qn1 != SNS_LOW_BR_MODE )
     262             :         {
     263      663469 :             nb_ind += qn1 + qn2;
     264             :         }
     265             : 
     266      663772 :         if ( k == 0 || ( k == 1 && core != TCX_20_CORE ) )
     267             :         {
     268             :             /* Encode quantizer type */
     269      663772 :             if ( k == 0 )
     270             :             {
     271      649272 :                 nb = 0;
     272             :             }
     273             :             else
     274             :             {
     275       14500 :                 nb = 1;
     276       14500 :                 push_next_indice( hBstr, q_type, nb );
     277             :             }
     278             : 
     279      663772 :             nb_bits += nb;
     280             : 
     281             :             /* Encode quantizer data */
     282      663772 :             if ( ( ( q_type == 0 ) && element_mode != IVAS_CPE_MDCT ) || ( ( q_type == 0 ) && ( st1 >= 0 ) && element_mode == IVAS_CPE_MDCT ) )
     283             :             {
     284             :                 /* Absolute quantizer with 1st stage stochastic codebook */
     285      467405 :                 push_next_indice( hBstr, st1, bits_for_abs_quant );
     286      467405 :                 nb_bits += bits_for_abs_quant;
     287             :             }
     288             : 
     289      663772 :             if ( element_mode == IVAS_CPE_MDCT && stereo_mode == 3 && st1 < 0 )
     290             :             {
     291      187611 :                 push_next_indice( hBstr, st1 + 2, 1 );
     292      187611 :                 nb_bits++;
     293             :             }
     294             : 
     295      663772 :             if ( element_mode != IVAS_CPE_MDCT || ( st1 != -2 && qn1 != SNS_LOW_BR_MODE ) )
     296             :             {
     297             :                 /* 2 bits to specify Q2,Q3,Q4,ext */
     298      643130 :                 nb_bits += 4;
     299      643130 :                 i = qn1 - 2;
     300             : 
     301      643130 :                 if ( ( i < 0 ) || ( i > 3 ) )
     302             :                 {
     303       39837 :                     i = 3;
     304             :                 }
     305      643130 :                 push_next_indice( hBstr, i, 2 );
     306             : 
     307      643130 :                 i = qn2 - 2;
     308             : 
     309      643130 :                 if ( ( i < 0 ) || ( i > 3 ) )
     310             :                 {
     311       85942 :                     i = 3;
     312             :                 }
     313      643130 :                 push_next_indice( hBstr, i, 2 );
     314             : 
     315             :                 /* Unary code for abs and rel LPC0/LPC2 */
     316             :                 /* Q5 = 0, Q6=10, Q0=110, Q7=1110, ... */
     317      643130 :                 nb = qn1;
     318             : 
     319      643130 :                 if ( nb > 6 )
     320             :                 {
     321         993 :                     nb -= 3;
     322             :                 }
     323      642137 :                 else if ( nb > 4 )
     324             :                 {
     325       67703 :                     nb -= 4;
     326             :                 }
     327      574434 :                 else if ( nb == 0 )
     328             :                 {
     329       11598 :                     nb = 3;
     330             :                 }
     331             :                 else
     332             :                 {
     333      562836 :                     nb = 0;
     334             :                 }
     335             : 
     336      643130 :                 if ( nb > 0 )
     337             :                 {
     338       80294 :                     unary_code( nb, hBstr );
     339             :                 }
     340      643130 :                 nb_bits += nb;
     341             : 
     342      643130 :                 nb = qn2;
     343             : 
     344      643130 :                 if ( nb > 6 )
     345             :                 {
     346         149 :                     nb -= 3;
     347             :                 }
     348      642981 :                 else if ( nb > 4 )
     349             :                 {
     350       14946 :                     nb -= 4;
     351             :                 }
     352      628035 :                 else if ( nb == 0 )
     353             :                 {
     354       80775 :                     nb = 3;
     355             :                 }
     356             :                 else
     357             :                 {
     358      547260 :                     nb = 0;
     359             :                 }
     360             : 
     361      643130 :                 if ( nb > 0 )
     362             :                 {
     363       95870 :                     unary_code( nb, hBstr );
     364             :                 }
     365      643130 :                 nb_bits += nb;
     366             : 
     367      643130 :                 avqBits = 4 * qn1;
     368      643130 :                 unpack4bits( avqBits, &param_lpc[j], hBstr );
     369      643130 :                 j += qn1;
     370      643130 :                 nb_bits += avqBits;
     371             : 
     372      643130 :                 avqBits = 4 * qn2;
     373      643130 :                 unpack4bits( avqBits, &param_lpc[j], hBstr );
     374      643130 :                 j += qn2;
     375      643130 :                 nb_bits += avqBits;
     376             :             }
     377             :         }
     378             :         else
     379             :         {
     380           0 :             j += nb_ind;
     381             :         }
     382             :     }
     383             : 
     384      649272 :     return ( nb_bits );
     385             : }

Generated by: LCOV version 1.14