LCOV - code coverage report
Current view: top level - lib_enc - qlpc_avq.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 129 133 97.0 %
Date: 2025-05-28 04:28:20 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      886717 : 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      886717 :     tmp_index = &index[0];
      68      886717 :     *nb_indices = 0;
      69             : 
      70             :     /* Quantize end_frame LPC */
      71      886717 :     set_f( lsf_q, 0, M );
      72             : 
      73      886717 :     tmp_index[0] = vlpc_1st_cod( lsf, lsf_q, sr_core, dummy );
      74             : 
      75      886717 :     nbt = vlpc_2st_cod( lsf, lsf_q, &tmp_index[1], 0, sr_core );
      76             : 
      77      886717 :     nit = 1 + 2 + index[1] + index[2];
      78      886717 :     tmp_index += nit;
      79      886717 :     *nb_indices += nit;
      80      886717 :     nbbits[0] = 8 + nbt;
      81             : 
      82      886717 :     *tmp_index = 0;
      83             : 
      84             :     /* Quantize mid-frame LPC */
      85      886717 :     if ( core == TCX_10_CORE )
      86             :     {
      87       13291 :         tmp_index++;
      88       13291 :         *nb_indices += 1;
      89             : 
      90             :         /* LPC2: Abs? */
      91       13291 :         set_f( lsfmid_q, 0, M );
      92             : 
      93       13291 :         tmp_index[0] = vlpc_1st_cod( lsfmid, lsfmid_q, sr_core, dummy );
      94             : 
      95       13291 :         nbits = vlpc_2st_cod( lsfmid, lsfmid_q, &tmp_index[1], 0, sr_core );
      96             : 
      97       13291 :         nbt = 8 + nbits;
      98       13291 :         nit = 1 + 2 + tmp_index[1] + tmp_index[2];
      99             : 
     100             :         /* LPC2: RelR? */
     101      225947 :         for ( i = 0; i < M; i++ )
     102             :         {
     103      212656 :             lsfmid_q0[i] = lsf_q[i];
     104             :         }
     105             : 
     106       13291 :         nbits = vlpc_2st_cod( lsfmid, lsfmid_q0, indxt, 3, sr_core );
     107             : 
     108       13291 :         if ( nbits < nbt )
     109             :         {
     110        6065 :             nbt = nbits;
     111        6065 :             nit = 2 + indxt[0] + indxt[1];
     112        6065 :             tmp_index[-1] = 1;
     113             : 
     114      103105 :             for ( i = 0; i < M; i++ )
     115             :             {
     116       97040 :                 lsfmid_q[i] = lsfmid_q0[i];
     117             :             }
     118             : 
     119       59951 :             for ( i = 0; i < nit; i++ )
     120             :             {
     121       53886 :                 tmp_index[i] = indxt[i];
     122             :             }
     123             :         }
     124             : 
     125       13291 :         tmp_index += nit;
     126       13291 :         *nb_indices += nit;
     127       13291 :         nbbits[1] = 1 + nbt;
     128             :     }
     129             : 
     130      886717 :     return;
     131             : }
     132             : 
     133             : 
     134             : /*-------------------------------------------------------------------*
     135             :  * unary_code()
     136             :  *
     137             :  *
     138             :  *--------------------------------------------------------------------*/
     139             : 
     140     2649981 : static int16_t unary_code( int16_t ind, BSTR_ENC_HANDLE hBstr )
     141             : {
     142             :     int16_t nb_bits;
     143             : 
     144     2649981 :     nb_bits = 1;
     145             : 
     146             :     /* Index bits */
     147     2649981 :     ind -= 1;
     148             : 
     149     2649981 :     while ( ind >= 16 )
     150             :     {
     151           0 :         push_next_indice( hBstr, 0xffffU, 16 );
     152           0 :         nb_bits += 16;
     153           0 :         ind -= 16;
     154             :     }
     155     2649981 :     if ( ind > 0 )
     156             :     {
     157     1882981 :         push_next_indice( hBstr, ( 1U << ind ) - 1, ind );
     158     1882981 :         nb_bits += ind;
     159             :     }
     160             : 
     161             :     /* Stop bit */
     162     2649981 :     push_next_indice( hBstr, 0, 1 );
     163             : 
     164     2649981 :     return ( nb_bits );
     165             : }
     166             : 
     167             : 
     168             : /*-------------------------------------------------------------------*
     169             :  * unpack4bits()
     170             :  *
     171             :  *
     172             :  *--------------------------------------------------------------------*/
     173             : 
     174    18039244 : static int16_t unpack4bits( int16_t nbits, const int16_t *prm, BSTR_ENC_HANDLE hBstr )
     175             : {
     176             :     int16_t i;
     177             : 
     178    18039244 :     if ( nbits == 0 )
     179             :     {
     180     1491004 :         push_next_indice( hBstr, 0, 0 );
     181     1491004 :         i = 1;
     182             :     }
     183             :     else
     184             :     {
     185    16548240 :         i = 0;
     186             : 
     187    49321523 :         while ( nbits > 4 )
     188             :         {
     189    32773283 :             push_next_indice( hBstr, prm[i], 4 );
     190    32773283 :             nbits -= 4;
     191    32773283 :             i++;
     192             :         }
     193    16548240 :         push_next_indice( hBstr, prm[i], nbits );
     194    16548240 :         i++;
     195             :     }
     196             : 
     197    18039244 :     return ( i );
     198             : }
     199             : 
     200             : 
     201             : /*-------------------------------------------------------------------*
     202             :  * encode_lpc_avq()
     203             :  *
     204             :  *
     205             :  *--------------------------------------------------------------------*/
     206             : 
     207     9374769 : 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     9374769 :     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     9374769 :     stereo_mode = 0;
     222             : 
     223     9374769 :     if ( element_mode == IVAS_CPE_MDCT )
     224             :     {
     225     8488052 :         bits_for_abs_quant = SNS_ABS_QUANT_BITS;
     226             :     }
     227             :     else
     228             :     {
     229      886717 :         bits_for_abs_quant = LPC_ABS_QUANT_BITS;
     230             :     }
     231             : 
     232     9374769 :     j = 0;
     233     9374769 :     nb_bits = 0;
     234             : 
     235    18954977 :     for ( k = 0; k < numlpc; k++ )
     236             :     {
     237             :         /* Retrieve quantizer type */
     238     9580208 :         if ( k == 0 )
     239             :         {
     240     9374769 :             q_type = 0;
     241             :         }
     242             :         else
     243             :         {
     244      205439 :             q_type = param_lpc[j++];
     245             :         }
     246             : 
     247     9580208 :         if ( element_mode == IVAS_CPE_MDCT && k == 0 )
     248             :         {
     249     8488052 :             stereo_mode = param_lpc[j++];
     250             :         }
     251             : 
     252             :         /* Determine number of AVQ indices */
     253     9580208 :         nb_ind = 0;
     254             : 
     255     9580208 :         if ( q_type == 0 )
     256             :         {
     257     9445033 :             st1 = param_lpc[j++];
     258             :         }
     259     9580208 :         qn1 = param_lpc[j++];
     260     9580208 :         qn2 = param_lpc[j++];
     261     9580208 :         if ( qn1 != SNS_LOW_BR_MODE )
     262             :         {
     263     9575545 :             nb_ind += qn1 + qn2;
     264             :         }
     265             : 
     266     9580208 :         if ( k == 0 || ( k == 1 && core != TCX_20_CORE ) )
     267             :         {
     268             :             /* Encode quantizer type */
     269     9580208 :             if ( k == 0 )
     270             :             {
     271     9374769 :                 nb = 0;
     272             :             }
     273             :             else
     274             :             {
     275      205439 :                 nb = 1;
     276      205439 :                 push_next_indice( hBstr, q_type, nb );
     277             :             }
     278             : 
     279     9580208 :             nb_bits += nb;
     280             : 
     281             :             /* Encode quantizer data */
     282     9580208 :             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     6348225 :                 push_next_indice( hBstr, st1, bits_for_abs_quant );
     286     6348225 :                 nb_bits += bits_for_abs_quant;
     287             :             }
     288             : 
     289     9580208 :             if ( element_mode == IVAS_CPE_MDCT && stereo_mode == 3 && st1 < 0 )
     290             :             {
     291     3096808 :                 push_next_indice( hBstr, st1 + 2, 1 );
     292     3096808 :                 nb_bits++;
     293             :             }
     294             : 
     295     9580208 :             if ( element_mode != IVAS_CPE_MDCT || ( st1 != -2 && qn1 != SNS_LOW_BR_MODE ) )
     296             :             {
     297             :                 /* 2 bits to specify Q2,Q3,Q4,ext */
     298     9019622 :                 nb_bits += 4;
     299     9019622 :                 i = qn1 - 2;
     300             : 
     301     9019622 :                 if ( ( i < 0 ) || ( i > 3 ) )
     302             :                 {
     303      533313 :                     i = 3;
     304             :                 }
     305     9019622 :                 push_next_indice( hBstr, i, 2 );
     306             : 
     307     9019622 :                 i = qn2 - 2;
     308             : 
     309     9019622 :                 if ( ( i < 0 ) || ( i > 3 ) )
     310             :                 {
     311     1349668 :                     i = 3;
     312             :                 }
     313     9019622 :                 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     9019622 :                 nb = qn1;
     318             : 
     319     9019622 :                 if ( nb > 6 )
     320             :                 {
     321       22624 :                     nb -= 3;
     322             :                 }
     323     8996998 :                 else if ( nb > 4 )
     324             :                 {
     325      812778 :                     nb -= 4;
     326             :                 }
     327     8184220 :                 else if ( nb == 0 )
     328             :                 {
     329      214829 :                     nb = 3;
     330             :                 }
     331             :                 else
     332             :                 {
     333     7969391 :                     nb = 0;
     334             :                 }
     335             : 
     336     9019622 :                 if ( nb > 0 )
     337             :                 {
     338     1050231 :                     unary_code( nb, hBstr );
     339             :                 }
     340     9019622 :                 nb_bits += nb;
     341             : 
     342     9019622 :                 nb = qn2;
     343             : 
     344     9019622 :                 if ( nb > 6 )
     345             :                 {
     346        2212 :                     nb -= 3;
     347             :                 }
     348     9017410 :                 else if ( nb > 4 )
     349             :                 {
     350      321363 :                     nb -= 4;
     351             :                 }
     352     8696047 :                 else if ( nb == 0 )
     353             :                 {
     354     1276175 :                     nb = 3;
     355             :                 }
     356             :                 else
     357             :                 {
     358     7419872 :                     nb = 0;
     359             :                 }
     360             : 
     361     9019622 :                 if ( nb > 0 )
     362             :                 {
     363     1599750 :                     unary_code( nb, hBstr );
     364             :                 }
     365     9019622 :                 nb_bits += nb;
     366             : 
     367     9019622 :                 avqBits = 4 * qn1;
     368     9019622 :                 unpack4bits( avqBits, &param_lpc[j], hBstr );
     369     9019622 :                 j += qn1;
     370     9019622 :                 nb_bits += avqBits;
     371             : 
     372     9019622 :                 avqBits = 4 * qn2;
     373     9019622 :                 unpack4bits( avqBits, &param_lpc[j], hBstr );
     374     9019622 :                 j += qn2;
     375     9019622 :                 nb_bits += avqBits;
     376             :             }
     377             :         }
     378             :         else
     379             :         {
     380           0 :             j += nb_ind;
     381             :         }
     382             :     }
     383             : 
     384     9374769 :     return ( nb_bits );
     385             : }

Generated by: LCOV version 1.14