LCOV - code coverage report
Current view: top level - lib_enc - nelp_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ b5bd5e0684ad2d9250297e1e7a0ddc986cb7b37e Lines: 162 175 92.6 %
Date: 2025-10-27 07:01:45 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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * quantize_uvg()
      50             :  *
      51             :  * Quantize unvoiced gains
      52             :  *--------------------------------------------------------------------*/
      53             : 
      54          60 : static void quantize_uvg(
      55             :     float *G,      /* i  : unvoiced gain     */
      56             :     int16_t *iG1,  /* i  : gain 1 index      */
      57             :     int16_t *iG2,  /* i  : gain 2 index      */
      58             :     float *quantG, /* o  : quantized gain    */
      59             :     const int16_t bwidth )
      60             : {
      61             :     float G1[2], G2[10];
      62             :     int16_t i, j, k;
      63             :     float mse, mmse;
      64          60 :     const float( *UVG1CB )[2] = NULL;
      65          60 :     const float( *UVG2CB1 )[5] = NULL;
      66          60 :     const float( *UVG2CB2 )[5] = NULL;
      67             : 
      68          60 :     if ( bwidth == NB )
      69             :     {
      70           8 :         UVG1CB = UVG1CB_NB;
      71           8 :         UVG2CB1 = UVG2CB1_NB;
      72           8 :         UVG2CB2 = UVG2CB2_NB;
      73             :     }
      74          52 :     else if ( bwidth == WB || bwidth == SWB )
      75             :     {
      76          52 :         UVG1CB = UVG1CB_WB;
      77          52 :         UVG2CB1 = UVG2CB1_WB;
      78          52 :         UVG2CB2 = UVG2CB2_WB;
      79             :     }
      80             : 
      81         180 :     for ( i = 0; i < 2; i++ )
      82             :     {
      83         120 :         G1[i] = 0;
      84         720 :         for ( j = 0; j < 5; j++ )
      85             :         {
      86         600 :             G1[i] += SQR( G[i * 5 + j] );
      87             :         }
      88         120 :         G1[i] = (float) log10( sqrt( G1[i] / 5 ) );
      89             :     }
      90             : 
      91          60 :     mmse = (float) 1e30;
      92          60 :     *iG1 = 0;
      93             : 
      94        1980 :     for ( i = 0; i < UVG1_CBSIZE; i++ )
      95             :     {
      96        1920 :         mse = SQR( G1[0] - ( UVG1CB[i][0] ) ) + SQR( G1[1] - ( UVG1CB[i][1] ) );
      97        1920 :         if ( mse < mmse )
      98             :         {
      99         212 :             *iG1 = i;
     100         212 :             mmse = mse;
     101             :         }
     102             :     }
     103             : 
     104          60 :     G1[0] = (float) pow( 10.0, ( UVG1CB[*iG1][0] ) );
     105          60 :     G1[1] = (float) pow( 10.0, ( UVG1CB[*iG1][1] ) );
     106             : 
     107         180 :     for ( i = 0; i < 2; i++ )
     108             :     {
     109         720 :         for ( j = 0; j < 5; j++ )
     110             :         {
     111         600 :             G2[i * 5 + j] = G[i * 5 + j] / G1[i];
     112             :         }
     113             :     }
     114             : 
     115         180 :     for ( i = 0; i < 2; i++ )
     116             :     {
     117         120 :         mmse = (float) 1e30;
     118         120 :         iG2[i] = 0;
     119        7800 :         for ( j = 0; j < UVG2_CBSIZE; j++ )
     120             :         {
     121        7680 :             mse = 0;
     122       46080 :             for ( k = 0; k < 5; k++ )
     123             :             {
     124       38400 :                 if ( i == 0 )
     125             :                 {
     126       19200 :                     mse += SQR( G2[i * 5 + k] - UVG2CB1[j][k] );
     127             :                 }
     128       19200 :                 else if ( i == 1 )
     129             :                 {
     130       19200 :                     mse += SQR( G2[i * 5 + k] - UVG2CB2[j][k] );
     131             :                 }
     132             :             }
     133             : 
     134        7680 :             if ( mse < mmse )
     135             :             {
     136         651 :                 mmse = mse;
     137         651 :                 iG2[i] = j;
     138             :             }
     139             :         }
     140             :     }
     141             : 
     142          60 :     mvr2r( G, G2, 10 );
     143             : 
     144          60 :     dequantize_uvg( *iG1, iG2, quantG, bwidth );
     145             : 
     146          60 :     return;
     147             : }
     148             : 
     149             : 
     150             : /*-------------------------------------------------------------------*
     151             :  * nelp_encoder()
     152             :  *
     153             :  * NELP encoder
     154             :  *--------------------------------------------------------------------*/
     155             : 
     156          60 : void nelp_encoder(
     157             :     Encoder_State *st, /* i/o: encoder state                      */
     158             :     float *in,         /* i  : residual signal                    */
     159             :     float *exc,        /* o  : NELP quantized excitation signal   */
     160             :     const int16_t reduce_gains )
     161             : {
     162             :     int16_t i, j;
     163          60 :     float *ptr = exc;
     164          60 :     int16_t lag = 25; /* to cover 25*9 + 31 */
     165             :     float Gains[10], gain_fac;
     166             :     int16_t iG1, iG2[2], fid;
     167             :     float fdbck, var_dB, tmp;
     168          60 :     float E1 = 0, E2, E3, R, EL1 = 0, EH1 = 0, EL2, EH2, RL, RH;
     169             :     float filtRes[L_FRAME];
     170             :     float ptr_tmp[L_FRAME];
     171             :     int16_t rf_flag;
     172             : 
     173          60 :     SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
     174          60 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     175             : 
     176          60 :     rf_flag = st->rf_mode;
     177             : 
     178          60 :     if ( ( hSC_VBR->last_nelp_mode == 1 ) && ( st->bwidth != st->last_bwidth ) )
     179             :     {
     180           0 :         hSC_VBR->last_nelp_mode = 0;
     181             :     }
     182             : 
     183          60 :     if ( st->bwidth == NB )
     184             :     {
     185           8 :         if ( hSC_VBR->last_nelp_mode != 1 )
     186             :         {
     187           2 :             set_f( hSC_VBR->bp1_filt_mem_nb, 0, 7 * 2 );
     188             :         }
     189             :     }
     190          52 :     else if ( st->bwidth == WB || st->bwidth == SWB )
     191             :     {
     192          52 :         if ( hSC_VBR->last_nelp_mode != 1 )
     193             :         {
     194          44 :             set_f( hSC_VBR->bp1_filt_mem_wb, 0, 4 * 2 );
     195             :         }
     196             :     }
     197             : 
     198          60 :     if ( hSC_VBR->last_nelp_mode != 1 )
     199             :     {
     200          46 :         if ( st->bwidth == WB || st->bwidth == SWB )
     201             :         {
     202          44 :             set_f( hSC_VBR->shape1_filt_mem, 0, 20 );
     203          44 :             set_f( hSC_VBR->shape2_filt_mem, 0, 20 );
     204          44 :             set_f( hSC_VBR->shape3_filt_mem, 0, 20 );
     205          44 :             set_f( hSC_VBR->txlpf1_filt1_mem, 0, 20 );
     206          44 :             set_f( hSC_VBR->txlpf1_filt2_mem, 0, 20 );
     207          44 :             set_f( hSC_VBR->txhpf1_filt1_mem, 0, 20 );
     208          44 :             set_f( hSC_VBR->txhpf1_filt2_mem, 0, 20 );
     209             :         }
     210             :     }
     211             : 
     212             :     /* Start Unvoiced/NELP Processing */
     213          60 :     if ( st->bwidth == WB || st->bwidth == SWB )
     214             :     {
     215       13364 :         for ( i = 0, E1 = 0.001f; i < L_FRAME; i++ )
     216             :         {
     217       13312 :             E1 += SQR( in[i] );
     218             :         }
     219             : 
     220          52 :         polezero_filter( in, filtRes, L_FRAME, txlpf1_num_coef, txlpf1_den_coef, 10, hSC_VBR->txlpf1_filt1_mem );
     221             : 
     222       13364 :         for ( i = 0, EL1 = 0.001f; i < L_FRAME; i++ )
     223             :         {
     224       13312 :             EL1 += SQR( filtRes[i] );
     225             :         }
     226             : 
     227          52 :         polezero_filter( in, filtRes, L_FRAME, txhpf1_num_coef, txhpf1_den_coef, 10, hSC_VBR->txhpf1_filt1_mem );
     228             : 
     229       13364 :         for ( i = 0, EH1 = 0.001f; i < L_FRAME; i++ )
     230             :         {
     231       13312 :             EH1 += SQR( filtRes[i] );
     232             :         }
     233             :     }
     234             : 
     235         600 :     for ( i = 0; i < 9; i++ )
     236             :     {
     237       14040 :         for ( j = i * lag, Gains[i] = 0.001f; j < ( i + 1 ) * lag; j++ )
     238             :         {
     239       13500 :             Gains[i] += SQR( in[j] );
     240             :         }
     241             : 
     242         540 :         Gains[i] = (float) sqrt( Gains[i] / lag );
     243             :     }
     244             : 
     245        1920 :     for ( j = i * lag, Gains[i] = 0.001f; j < L_FRAME; j++ )
     246             :     {
     247        1860 :         Gains[i] += SQR( in[j] );
     248             :     }
     249             : 
     250          60 :     Gains[i] = (float) sqrt( Gains[i] / ( L_FRAME - ( lag * i ) ) );
     251             : 
     252          60 :     if ( reduce_gains == 1 )
     253             :     {
     254          88 :         for ( i = 0; i < 10; i++ )
     255             :         {
     256          80 :             Gains[i] = ( Gains[i] * 0.6f );
     257             :         }
     258             :     }
     259             : 
     260          60 :     if ( hSC_VBR->last_nelp_mode != 1 ) /* if prev frame was not NELP then init mem*/
     261             :     {
     262          46 :         hSC_VBR->nelp_gain_mem = Gains[0];
     263             :     }
     264             : 
     265          60 :     tmp = (float) ( 20.0 * ( log10( Gains[0] ) - log10( hSC_VBR->nelp_gain_mem ) ) );
     266          60 :     var_dB = tmp * tmp;
     267         600 :     for ( i = 1; i < 10; i++ )
     268             :     {
     269         540 :         tmp = (float) ( 20.0 * ( log10( Gains[i] ) - log10( Gains[i - 1] ) ) );
     270         540 :         var_dB += tmp * tmp;
     271             :     }
     272             : 
     273          60 :     if ( hSC_VBR->last_nelp_mode != 1 )
     274             :     {
     275          46 :         var_dB *= 0.111f;
     276             :     }
     277             :     else
     278             :     {
     279          14 :         var_dB *= 0.1f;
     280             :     }
     281             : 
     282          60 :     fdbck = (float) ( 0.82f / ( 1.0f + exp( 0.25f * ( var_dB - 20.0f ) ) ) );
     283             : 
     284         660 :     for ( i = 0; i < 10; i++ )
     285             :     {
     286         600 :         Gains[i] = (float) ( ( 1.0f - fdbck ) * Gains[i] + fdbck * hSC_VBR->nelp_gain_mem );
     287         600 :         hSC_VBR->nelp_gain_mem = Gains[i];
     288             :     }
     289             : 
     290          60 :     quantize_uvg( Gains, &iG1, iG2, Gains, (int16_t) st->bwidth );
     291             : 
     292          60 :     if ( rf_flag )
     293             :     {
     294          40 :         st->hRF->rf_indx_nelp_iG1[0] = (int16_t) iG1;
     295          40 :         st->hRF->rf_indx_nelp_iG2[0][0] = (int16_t) iG2[0];
     296          40 :         st->hRF->rf_indx_nelp_iG2[0][1] = (int16_t) iG2[1];
     297             :     }
     298             :     else
     299             :     {
     300          20 :         push_indice( hBstr, IND_IG1, iG1, 5 );
     301          20 :         push_indice( hBstr, IND_IG2A, iG2[0], 6 );
     302          20 :         push_indice( hBstr, IND_IG2B, iG2[1], 6 );
     303             :     }
     304             : 
     305          60 :     if ( st->bwidth == WB || st->bwidth == SWB )
     306             :     {
     307          52 :         gain_fac = 1.16f;
     308             :     }
     309             :     else
     310             :     {
     311           8 :         gain_fac = 1.37f;
     312             :     }
     313             : 
     314          60 :     generate_nelp_excitation( &( hSC_VBR->nelp_enc_seed ), Gains, ptr, gain_fac );
     315             : 
     316          60 :     if ( st->bwidth == WB || st->bwidth == SWB )
     317             :     {
     318          52 :         polezero_filter( ptr, ptr_tmp, L_FRAME, bp1_num_coef_wb, bp1_den_coef_wb, 4, hSC_VBR->bp1_filt_mem_wb );
     319          52 :         mvr2r( ptr_tmp, ptr, L_FRAME );
     320             :     }
     321           8 :     else if ( st->bwidth == NB )
     322             :     {
     323           8 :         polezero_filter( ptr, ptr_tmp, L_FRAME, bp1_num_coef_nb_fx_order7, bp1_den_coef_nb_fx_order7, 7, hSC_VBR->bp1_filt_mem_nb );
     324           8 :         mvr2r( ptr_tmp, ptr, L_FRAME );
     325             :     }
     326             : 
     327       15420 :     for ( i = 0, E3 = 0.001f; i < L_FRAME; i++ )
     328             :     {
     329       15360 :         E3 += SQR( ptr[i] );
     330             :     }
     331             : 
     332          60 :     if ( st->bwidth == WB || st->bwidth == SWB )
     333             :     {
     334          52 :         polezero_filter( ptr, ptr_tmp, L_FRAME, shape1_num_coef, shape1_den_coef, 10, hSC_VBR->shape1_filt_mem );
     335          52 :         mvr2r( ptr_tmp, ptr, L_FRAME );
     336             : 
     337       13364 :         for ( i = 0, E2 = 0.001f; i < L_FRAME; i++ )
     338             :         {
     339       13312 :             E2 += SQR( ptr[i] );
     340             :         }
     341             : 
     342          52 :         R = (float) sqrt( E1 / E2 );
     343             : 
     344       13364 :         for ( i = 0; i < L_FRAME; i++ )
     345             :         {
     346       13312 :             filtRes[i] = R * ptr[i];
     347             :         }
     348             : 
     349          52 :         polezero_filter( filtRes, ptr_tmp, L_FRAME, txlpf1_num_coef, txlpf1_den_coef, 10, hSC_VBR->txlpf1_filt2_mem );
     350          52 :         mvr2r( ptr_tmp, filtRes, L_FRAME );
     351             : 
     352       13364 :         for ( i = 0, EL2 = 0.001f; i < L_FRAME; i++ )
     353             :         {
     354       13312 :             EL2 += SQR( filtRes[i] );
     355             :         }
     356             : 
     357       13364 :         for ( i = 0; i < L_FRAME; i++ )
     358             :         {
     359       13312 :             filtRes[i] = R * ptr[i];
     360             :         }
     361             : 
     362          52 :         polezero_filter( filtRes, ptr_tmp, L_FRAME, txhpf1_num_coef, txhpf1_den_coef, 10, hSC_VBR->txhpf1_filt2_mem );
     363          52 :         mvr2r( ptr_tmp, filtRes, L_FRAME );
     364             : 
     365       13364 :         for ( i = 0, EH2 = 0.001f; i < L_FRAME; i++ )
     366             :         {
     367       13312 :             EH2 += SQR( filtRes[i] );
     368             :         }
     369             : 
     370          52 :         RL = (float) 10.0f * (float) log10( EL1 / EL2 );
     371          52 :         RH = (float) 10.0f * (float) log10( EH1 / EH2 );
     372             : 
     373          52 :         fid = 0;
     374          52 :         if ( RL < -3 )
     375             :         {
     376           0 :             fid = 1;
     377             :         }
     378          52 :         else if ( RH < -3 )
     379             :         {
     380           0 :             fid = 2;
     381             :         }
     382             : 
     383          52 :         if ( rf_flag == 0 )
     384             :         {
     385             : 
     386          12 :             switch ( fid )
     387             :             {
     388           0 :                 case 1:
     389             :                     /* Update other filter memory */
     390           0 :                     polezero_filter( ptr, filtRes, L_FRAME, shape3_num_coef, shape3_den_coef, 10, hSC_VBR->shape3_filt_mem );
     391             : 
     392             :                     /* filter the residual to desired shape */
     393           0 :                     polezero_filter( ptr, ptr_tmp, L_FRAME, shape2_num_coef, shape2_den_coef, 10, hSC_VBR->shape2_filt_mem );
     394             : 
     395           0 :                     mvr2r( ptr_tmp, ptr, L_FRAME );
     396             : 
     397           0 :                     break;
     398           0 :                 case 2:
     399             :                     /* Update other filter memory */
     400           0 :                     polezero_filter( ptr, filtRes, L_FRAME, shape2_num_coef, shape2_den_coef, 10, hSC_VBR->shape2_filt_mem );
     401             : 
     402             :                     /* filter the residual to desired shape */
     403           0 :                     polezero_filter( ptr, ptr_tmp, L_FRAME, shape3_num_coef, shape3_den_coef, 10, hSC_VBR->shape3_filt_mem );
     404             : 
     405           0 :                     mvr2r( ptr_tmp, ptr, L_FRAME );
     406             : 
     407           0 :                     break;
     408          12 :                 default:
     409             :                     /* Update other filter memory */
     410          12 :                     polezero_filter( ptr, filtRes, L_FRAME, shape2_num_coef, shape2_den_coef, 10, hSC_VBR->shape2_filt_mem );
     411          12 :                     polezero_filter( ptr, filtRes, L_FRAME, shape3_num_coef, shape3_den_coef, 10, hSC_VBR->shape3_filt_mem );
     412             : 
     413          12 :                     break;
     414             :             }
     415             : 
     416        3084 :             for ( i = 0, E2 = 0.001f; i < L_FRAME; i++ )
     417             :             {
     418        3072 :                 E2 += SQR( ptr[i] );
     419             :             }
     420             : 
     421          12 :             R = (float) sqrt( E3 / E2 );
     422        3084 :             for ( i = 0; i < L_FRAME; i++ )
     423             :             {
     424        3072 :                 ptr[i] *= R;
     425             :             }
     426             :         }
     427             : 
     428          52 :         if ( rf_flag )
     429             :         {
     430          40 :             st->hRF->rf_indx_nelp_fid[0] = (int16_t) fid;
     431             :         }
     432             :         else
     433             :         {
     434          12 :             push_indice( hBstr, IND_NELP_FID, fid, 2 );
     435             :         }
     436             :     }
     437             : 
     438          60 :     if ( rf_flag == 0 )
     439             :     {
     440        5140 :         for ( i = 0; i < L_FRAME; i++ )
     441             :         {
     442        5120 :             exc[i] = ptr[i];
     443             :         }
     444             :     }
     445             : 
     446          60 :     return;
     447             : }

Generated by: LCOV version 1.14