LCOV - code coverage report
Current view: top level - lib_enc - qlpc_stoch.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 94 98 95.9 %
Date: 2025-05-28 04:28:20 Functions: 2 2 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 <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include <math.h>
      44             : #include "cnst.h"
      45             : #include "prot.h"
      46             : #include "rom_com.h"
      47             : #include "rom_enc.h"
      48             : #include "basop_proto_func.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*-------------------------------------------------------------------*
      52             :  * lpc_quantization()
      53             :  *
      54             :  *
      55             :  *--------------------------------------------------------------------*/
      56             : 
      57     2496008 : void lpc_quantization(
      58             :     Encoder_State *st,
      59             :     const float lsp[],
      60             :     const float lspmid[],
      61             :     float lsp_q[],
      62             :     float lsf_q[],
      63             :     float lspmid_q[],
      64             :     const int16_t coder_type,
      65             :     const int16_t acelp_midLpc,
      66             :     int16_t param_lpc[],
      67             :     int16_t nbits_lpc[],
      68             :     int16_t *bits_param_lpc,
      69             :     int16_t *no_param_lpc )
      70             : {
      71             :     int16_t nb_indices;
      72             :     float lsfmid_q[M];
      73             :     int16_t lsfmid_idx;
      74             :     int16_t i;
      75             :     float lsf[M], lsfmid[M];
      76             :     int16_t force_sf;
      77             :     float fec_lsf[M], stab;
      78             : 
      79     2496008 :     nb_indices = 0;
      80             : 
      81             :     /****** High-rate LPC quantizer *******/
      82     2496008 :     if ( st->lpcQuantization == 0 )
      83             :     {
      84      886717 :         lsp2lsf( lsp, lsf, M, st->sr_core );
      85             : 
      86      886717 :         if ( st->core == TCX_10_CORE )
      87             :         {
      88       13291 :             lsp2lsf( lspmid, lsfmid, M, st->sr_core );
      89             :         }
      90             : 
      91      886717 :         qlpc_avq( lsf, lsfmid, lsf_q, lsfmid_q, param_lpc, &nb_indices, nbits_lpc, st->core, st->sr_core );
      92             : 
      93      886717 :         lsf2lsp( lsf_q, lsp_q, M, st->sr_core );
      94             : 
      95      886717 :         if ( st->core == TCX_10_CORE )
      96             :         {
      97       13291 :             lsf2lsp( lsfmid_q, lspmid_q, M, st->sr_core );
      98             :         }
      99             : 
     100      886717 :         assert( nb_indices <= NPRM_LPC_NEW );
     101             :     }
     102             : 
     103             :     /****** Low-rate LPC quantizer *******/
     104     1609291 :     else if ( st->lpcQuantization == 1 )
     105             :     {
     106     1609291 :         lsp2lsf( lsp, lsf, M, st->sr_core );
     107             : 
     108     1609291 :         force_sf = 0;
     109             :         /*Force safety net when possible in case of transitions*/
     110     1609291 :         if ( st->tc_cnt >= 1 || st->last_core_brate <= SID_2k40 || st->next_force_safety_net )
     111             :         {
     112      111052 :             force_sf = 1;
     113      111052 :             st->next_force_safety_net = 0;
     114             :         }
     115             : 
     116     1609291 :         if ( st->next_force_safety_net == 1 && st->Opt_RF_ON )
     117             :         {
     118           0 :             force_sf = 1;
     119           0 :             st->next_force_safety_net = 0;
     120             :         }
     121             : 
     122     1609291 :         if ( st->sr_core == INT_FS_16k && coder_type == UNVOICED )
     123             :         {
     124         656 :             lsf_end_enc( st, lsf, lsf_q, ENDLSF_NBITS, GENERIC, force_sf, param_lpc, no_param_lpc, bits_param_lpc, GENERIC, NULL );
     125             : 
     126         656 :             nb_indices = *no_param_lpc;
     127             :         }
     128             :         else
     129             :         {
     130     1608635 :             lsf_end_enc( st, lsf, lsf_q, ENDLSF_NBITS, coder_type, force_sf, param_lpc, no_param_lpc, bits_param_lpc, coder_type, NULL );
     131             : 
     132     1608635 :             nb_indices = *no_param_lpc;
     133             :         }
     134             : 
     135     1609291 :         FEC_lsf_estim_enc( st, fec_lsf );
     136             : 
     137             :         /* FEC - calculate LSF stability */
     138     1609291 :         stab = lsf_stab( lsf_q, fec_lsf, 0, st->L_frame );
     139             : 
     140     1609291 :         if ( stab < ( STAB_FAC_LIMIT + 0.2 ) && ( coder_type == VOICED || coder_type == GENERIC ) && st->Opt_RF_ON )
     141             :         {
     142         232 :             st->next_force_safety_net = 1;
     143             :         }
     144             : 
     145     1609291 :         lsf2lsp( lsf_q, lsp_q, M, st->sr_core );
     146             : 
     147     1609291 :         *nbits_lpc = ENDLSF_NBITS;
     148             :     }
     149             :     else
     150             :     {
     151           0 :         assert( 0 );
     152             :     }
     153             : 
     154     2496008 :     st->seed_acelp = 0;
     155    16826222 :     for ( i = nb_indices - 1; i >= 0; i-- )
     156             :     {
     157             :         /* rightshift before seed_acelp+param_lpc[i] to avoid overflows*/
     158    14330214 :         st->seed_acelp = (int16_t) ( ( ( ( st->seed_acelp ) >> 1 ) + param_lpc[i] ) * 31821L + 13849L );
     159             :     }
     160             : 
     161             :     /* Mid-frame LPC quantization */
     162     2496008 :     if ( st->lpcQuantization && acelp_midLpc )
     163             :     {
     164       19506 :         if ( st->rate_switching_reset == 0 )
     165             :         {
     166       19258 :             lsp2lsf( lspmid, lsfmid, M, st->sr_core );
     167       19258 :             midlsf_enc( st->lsf_old, lsf_q, lsfmid, &lsfmid_idx, M, st->Bin_E_old, st->narrowBand, st->sr_core, coder_type );
     168       19258 :             param_lpc[nb_indices++] = lsfmid_idx;
     169             : 
     170       19258 :             midlsf_dec( st->lsf_old, lsf_q, lsfmid_idx, lsfmid_q, M, coder_type, NULL, 0, 1 );
     171             : 
     172       19258 :             reorder_lsf( lsfmid_q, LSF_GAP_MID, M, st->sr_core );
     173       19258 :             lsf2lsp( lsfmid_q, lspmid_q, M, st->sr_core );
     174             :         }
     175             :         else
     176             :         {
     177         248 :             param_lpc[nb_indices++] = 0;
     178             :         }
     179             :     }
     180             : 
     181     2496008 :     return;
     182             : }
     183             : 
     184             : 
     185             : /*-------------------------------------------------------------------*
     186             :  * Unified_weighting()
     187             :  *
     188             :  * LSF weighting
     189             :  *-------------------------------------------------------------------*/
     190             : 
     191     5209470 : void Unified_weighting(
     192             :     const float Bin_Ener_128[], /* i  : FFT Bin energy 128 bins in two sets     */
     193             :     const float lsf[],          /* i  : LSF vector                              */
     194             :     float w[],                  /* o  : LP weighting filter (numerator)         */
     195             :     const int16_t narrowBand,   /* i  : flag for Narrowband                     */
     196             :     const int16_t unvoiced,     /* i  : flag for Unvoiced frame                 */
     197             :     const int32_t sr_core,      /* i  : sampling rate of core-coder             */
     198             :     const int16_t order         /* i  : LP order                                */
     199             : )
     200             : {
     201             :     int16_t i;
     202             :     float nf;
     203             :     const int16_t( *ptr_lsf_fit_model )[M];
     204             :     int16_t norm_lsf[M];
     205             :     float tmp, min_val;
     206             :     float w_fft[M];
     207             :     float Bin_Ener_160[160];
     208             :     const float *Bin_Ener;
     209             :     const float *Freq_w_Table;
     210             :     int16_t last_bin;
     211             : 
     212             :     /*Config. weighting*/
     213     5209470 :     if ( narrowBand )
     214             :     {
     215       12171 :         ptr_lsf_fit_model = lsf_unified_fit_model_nb;
     216       12171 :         nf = 6400.f;
     217             : 
     218       12171 :         last_bin = 127;
     219       12171 :         Bin_Ener = Bin_Ener_128;
     220             :     }
     221     5197299 :     else if ( sr_core == INT_FS_12k8 )
     222             :     {
     223     2556329 :         ptr_lsf_fit_model = lsf_unified_fit_model_wb;
     224     2556329 :         nf = 6400.f;
     225             : 
     226     2556329 :         last_bin = 127;
     227     2556329 :         Bin_Ener = Bin_Ener_128;
     228             :     }
     229             :     else
     230             :     {
     231     2640970 :         ptr_lsf_fit_model = lsf_unified_fit_model_wbhb;
     232     2640970 :         nf = 8000.f;
     233             : 
     234             :         /* Fill the missing part (128~159) of the bin energy */
     235     2640970 :         last_bin = 159;
     236             : 
     237     2640970 :         mvr2r( Bin_Ener_128, Bin_Ener_160, L_FFT / 2 );
     238             : 
     239             :         /* Find average bin energy (32 Energy) */
     240     2640970 :         tmp = 0.f;
     241    87152010 :         for ( i = 95; i < 127; i++ )
     242             :         {
     243    84511040 :             tmp += Bin_Ener_160[i];
     244             :         }
     245             : 
     246     2640970 :         tmp = tmp / 32.f;
     247    89792980 :         for ( i = 127; i < 160; i++ )
     248             :         {
     249    87152010 :             Bin_Ener_160[i] = tmp;
     250             :         }
     251             : 
     252     2640970 :         Bin_Ener = Bin_Ener_160;
     253             :     }
     254             : 
     255             :     /* 1) FFT weights*/
     256     5209470 :     if ( unvoiced )
     257             :     {
     258       64313 :         Freq_w_Table = Freq_Weight_UV;
     259             :     }
     260             :     else
     261             :     {
     262     5145157 :         Freq_w_Table = Freq_Weight_Com;
     263             :     }
     264             : 
     265             :     /* Use Envelope */
     266     5209470 :     min_val = 1000.0f;
     267    88560990 :     for ( i = 0; i < M; i++ )
     268             :     {
     269    83351520 :         norm_lsf[i] = (int16_t) ( ( lsf[i] / 50.f ) + 0.5f );
     270             : 
     271    83351520 :         if ( norm_lsf[i] == 0 )
     272             :         {
     273           0 :             w_fft[i] = Bin_Ener[1];
     274             :         }
     275    83351520 :         else if ( norm_lsf[i] == last_bin )
     276             :         {
     277         260 :             w_fft[i] = Bin_Ener[last_bin - 1];
     278             :         }
     279             :         else
     280             :         {
     281    83351260 :             tmp = max( Bin_Ener[norm_lsf[i]], Bin_Ener[norm_lsf[i] - 1] );
     282    83351260 :             w_fft[i] = max( Bin_Ener[norm_lsf[i] + 1], tmp );
     283             :         }
     284             : 
     285    83351520 :         if ( w_fft[i] < MIN_LOG_60dB )
     286             :         {
     287     2508873 :             w_fft[i] = MIN_LOG_VAL_60dB;
     288             :         }
     289             :         else
     290             :         {
     291    80842647 :             w_fft[i] = (float) ( 10.0 * log10( w_fft[i] ) );
     292             :         }
     293             : 
     294    83351520 :         if ( w_fft[i] < min_val )
     295             :         {
     296    25707064 :             min_val = w_fft[i];
     297             :         }
     298             :     }
     299             : 
     300    88560990 :     for ( i = 0; i < M; i++ )
     301             :     {
     302    83351520 :         w_fft[i] = (float) ( sqrt( w_fft[i] - min_val ) + 2.f );
     303    83351520 :         w_fft[i] *= Freq_w_Table[norm_lsf[i]];
     304             :     }
     305             : 
     306             :     /* 2) IHM weights*/
     307     5209470 :     w[0] = 1.f / ( lsf[0] - 0 ) + 1.f / ( lsf[1] - lsf[0] );
     308    78142050 :     for ( i = 1; i < order - 1; i++ )
     309             :     {
     310    72932580 :         w[i] = 1.f / ( lsf[i] - lsf[i - 1] ) + 1.f / ( lsf[i + 1] - lsf[i] );
     311             :     }
     312     5209470 :     w[order - 1] = 1.f / ( lsf[order - 1] - lsf[order - 2] ) +
     313     5209470 :                    1.f / ( nf - lsf[order - 1] );
     314             : 
     315             :     /* 3) Fitting model combining the two weights*/
     316    88560990 :     for ( i = 0; i < order; i++ )
     317             :     {
     318    83351520 :         w[i] *= ( nf / EVS_PI );
     319    83351520 :         w[i] = ( (float) ( ptr_lsf_fit_model[0][i] ) / ( 1 << 10 ) ) + w[i] * ( (float) ( ptr_lsf_fit_model[1][i] ) / ( 1 << 15 ) ) + w[i] * w[i] * ( (float) ( ptr_lsf_fit_model[2][i] ) / ( 1 << 18 ) ) + w_fft[i] * ( (float) ( ptr_lsf_fit_model[3][i] ) / ( 1 << 12 ) );
     320             : 
     321    83351520 :         if ( w[i] < 1.f / ( i + 1 ) )
     322             :         {
     323         281 :             w[i] = 1.f / ( i + 1 );
     324             :         }
     325             :     }
     326             : 
     327     5209470 :     return;
     328             : }

Generated by: LCOV version 1.14