LCOV - code coverage report
Current view: top level - lib_enc - enc_uv.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 45 50 90.0 %
Date: 2025-05-23 08:37:30 Functions: 1 1 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 "wmc_auto.h"
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * encod_unvoiced()
      50             :  *
      51             :  * Encode unvoiced (UC) frames
      52             :  *-------------------------------------------------------------------*/
      53             : 
      54        2037 : void encod_unvoiced(
      55             :     Encoder_State *st,               /* i/o: state structure                         */
      56             :     const float *speech,             /* i  : input speech                            */
      57             :     const float Aw[],                /* i  : weighted A(z) unquantized for subframes */
      58             :     const float *Aq,                 /* i  : LP coefficients                         */
      59             :     const float Es_pred,             /* i  : predicted scaled innov. energy          */
      60             :     const int16_t uc_two_stage_flag, /* i  : flag indicating two-stage UC            */
      61             :     const float *res,                /* i  : residual signal                         */
      62             :     float *syn,                      /* o  : core synthesis                          */
      63             :     float *tmp_noise,                /* o  : long-term noise energy                  */
      64             :     float *exc,                      /* i/o: current non-enhanced excitation         */
      65             :     float *pitch_buf,                /* o  : floating pitch values for each subframe */
      66             :     float *voice_factors,            /* o  : voicing factors                         */
      67             :     float *bwe_exc                   /* i/o: excitation for SWB TBE                  */
      68             : )
      69             : {
      70             :     float xn[L_SUBFR];        /* Target vector for pitch search               */
      71             :     float h1[L_SUBFR];        /* Impulse response vector                      */
      72             :     float code[L_SUBFR];      /* Fixed codebook excitation                    */
      73             :     float y2[L_SUBFR];        /* Filtered algebraic excitation                */
      74             :     float *pt_pitch;          /* pointer to floating pitch buffer             */
      75             :     float gain_pit;           /* Pitch gain                                   */
      76             :     float voice_fac;          /* Voicing factor                               */
      77             :     float gain_code;          /* gain of code                                 */
      78             :     float gain_inov;          /* inovative gain                               */
      79             :     const float *p_Aw, *p_Aq; /* pointer to LP filter coeff. vector           */
      80             :     int16_t i_subfr;
      81             :     float norm_gain_code;
      82             :     float cn[L_SUBFR];    /* Target vector in residual domain               */
      83             :     float y1[L_SUBFR];    /* Filtered adaptive excitation                   */
      84             :     float code2[L_SUBFR]; /* Gaussian excitation                            */
      85             :     float y22[L_SUBFR];   /* Filtered Gaussian excitation                   */
      86             :     int16_t i, unbits_PI;
      87             :     ACELP_CbkCorr g_corr;
      88             :     float gain_code2, g_corr2[6], exc2[L_SUBFR];
      89             :     int16_t index;
      90             : 
      91        2037 :     LPD_state_HANDLE hLPDmem = st->hLPDmem;
      92             : 
      93             :     /*------------------------------------------------------------------*
      94             :      * Initializations
      95             :      *------------------------------------------------------------------*/
      96             : 
      97        2037 :     gain_pit = 0;
      98             : 
      99        2037 :     if ( st->Opt_SC_VBR && st->vad_flag == 0 )
     100             :     {
     101           0 :         if ( st->hSC_VBR->last_ppp_mode == 1 || st->hSC_VBR->last_nelp_mode == 1 )
     102             :         {
     103             :             /* SC_VBR - reset the encoder, to avoid memory not updated issue for the
     104             :                case when UNVOICED mode is used to code inactive speech */
     105           0 :             CNG_reset_enc( st, pitch_buf, voice_factors, 1 );
     106             :         }
     107             :     }
     108             : 
     109        2037 :     p_Aw = Aw;
     110        2037 :     p_Aq = Aq;
     111        2037 :     pt_pitch = pitch_buf;
     112             : 
     113             :     /*----------------------------------------------------------------*
     114             :      * subframe loop
     115             :      *----------------------------------------------------------------*/
     116             : 
     117       10185 :     for ( i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR )
     118             :     {
     119             :         /*----------------------------------------------------------------*
     120             :          * Bandwidth expansion of A(z) filter coefficients
     121             :          * Find the excitation search target "xn" and innovation target in residual domain "cn"
     122             :          * Compute impulse response, h1[], of weighted synthesis filter
     123             :          *----------------------------------------------------------------*/
     124             : 
     125        8148 :         mvr2r( &res[i_subfr], &exc[i_subfr], L_SUBFR );
     126             : 
     127        8148 :         find_targets( speech, hLPDmem->mem_syn, i_subfr, &hLPDmem->mem_w0, p_Aq, res, L_SUBFR, p_Aw, st->preemph_fac, xn, cn, h1 );
     128             : 
     129        8148 :         if ( !uc_two_stage_flag )
     130             :         {
     131             :             /*----------------------------------------------------------------*
     132             :              * Unvoiced subframe processing
     133             :              *----------------------------------------------------------------*/
     134             : 
     135           0 :             *pt_pitch = gaus_encode( st, i_subfr, h1, xn, exc, &hLPDmem->mem_w0, st->clip_var, &hLPDmem->tilt_code, code, &gain_code, y2, &gain_inov, &voice_fac, &gain_pit, &norm_gain_code );
     136             :         }
     137             :         else
     138             :         {
     139             :             /*----------------------------------------------------------------*
     140             :              * Unvoiced subframe processing in two stages
     141             :              *----------------------------------------------------------------*/
     142             : 
     143             :             /* No adaptive codebook (UC) */
     144        8148 :             set_zero( y1, L_SUBFR );
     145        8148 :             set_zero( exc + i_subfr, L_SUBFR );
     146             : 
     147             :             /*-----------------------------------------------------------------*
     148             :              * Gain clipping test to avoid unstable synthesis on frame erasure
     149             :              *-----------------------------------------------------------------*/
     150             : 
     151        8148 :             gp_clip( st->element_mode, st->core_brate, st->voicing, i_subfr, UNVOICED, xn, st->clip_var );
     152             : 
     153        8148 :             *pt_pitch = (float) L_SUBFR;
     154             : 
     155             :             /*-----------------------------------------------------------------*
     156             :              * Innovation encoding
     157             :              *-----------------------------------------------------------------*/
     158             : 
     159        8148 :             inov_encode( st, st->core_brate, 0, L_FRAME, st->last_L_frame, UNVOICED, st->bwidth, 1, i_subfr, -1, p_Aq, gain_pit, cn, exc, h1, hLPDmem->tilt_code, *pt_pitch, xn, code, y2, &unbits_PI, L_SUBFR );
     160             : 
     161        8148 :             E_corr_xy2( xn, y1, y2, g_corr2, L_SUBFR );
     162        8148 :             g_corr.y2y2 = 0.01F + g_corr2[2];
     163        8148 :             g_corr.xy2 = 0.01F + -0.5f * g_corr2[3];
     164        8148 :             g_corr.y1y2 = 0.01F + 0.5f * g_corr2[4];
     165             : 
     166        8148 :             g_corr.xx = 0.01F + dotp( xn, xn, L_SUBFR );
     167             : 
     168             :             /*----------------------------------------------------------------------*
     169             :              * Add Gaussian excitation
     170             :              *----------------------------------------------------------------------*/
     171             : 
     172        8148 :             assert( gain_pit == 0.f );
     173             : 
     174        8148 :             gauss_L2( h1, code2, y2, y22, &gain_code2, g_corr2, hLPDmem->tilt_code, p_Aq, FORMANT_SHARPENING_G1, &( st->seed_acelp ) );
     175             : 
     176        8148 :             g_corr.xy1 = 0.f;
     177        8148 :             g_corr.y1y1 = g_corr2[0];
     178        8148 :             g_corr.y1y2 = g_corr2[4];
     179             : 
     180             :             /*-----------------------------------------------------------------*
     181             :              * Gain encoding
     182             :              *-----------------------------------------------------------------*/
     183             : 
     184        8148 :             index = gain_enc_gacelp_uv( code, code2, L_SUBFR, Es_pred, &gain_pit, &gain_code, &gain_code2, &g_corr, &norm_gain_code, &gain_inov, st->flag_noisy_speech_snr );
     185             : 
     186             : #ifdef DEBUGGING
     187             :             assert( st->acelp_cfg.gains_mode[i_subfr / L_SUBFR] == 7 && "Error: UC two-stage, only 5+2 gain Q is supported" );
     188             : #endif
     189        8148 :             push_indice( st->hBstr, IND_GAIN, index, st->acelp_cfg.gains_mode[i_subfr / L_SUBFR] );
     190             : 
     191        8148 :             gp_clip_test_gain_pit( st->element_mode, st->core_brate, gain_pit, st->clip_var );
     192             : 
     193             :             /*----------------------------------------------------------*
     194             :              * - voice factor (for codebook tilt sharpening)            *
     195             :              *----------------------------------------------------------*/
     196             : 
     197        8148 :             hLPDmem->tilt_code = est_tilt( exc + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, 0 );
     198             : 
     199             :             /*-----------------------------------------------------------------*
     200             :              * Update memory of the weighting filter
     201             :              *-----------------------------------------------------------------*/
     202             : 
     203        8148 :             hLPDmem->mem_w0 = xn[L_SUBFR - 1] - gain_code * y2[L_SUBFR - 1] - gain_code2 * y22[L_SUBFR - 1];
     204             : 
     205             :             /*-------------------------------------------------------*
     206             :              * - Find the total excitation.                          *
     207             :              *-------------------------------------------------------*/
     208             : 
     209      529620 :             for ( i = 0; i < L_SUBFR; i++ )
     210             :             {
     211      521472 :                 exc2[i] = gain_pit * exc[i + i_subfr];
     212      521472 :                 exc2[i] += gain_code2 * code2[i];
     213      521472 :                 exc[i + i_subfr] = exc2[i] + gain_code * code[i];
     214             :             }
     215             :         }
     216             : 
     217        8148 :         *tmp_noise = norm_gain_code;
     218             : 
     219        8148 :         voice_factors[i_subfr / L_SUBFR] = 0.0f;
     220             : 
     221        8148 :         if ( st->hBWE_TD != NULL )
     222             :         {
     223        8148 :             interp_code_5over2( &exc[i_subfr], &bwe_exc[i_subfr * HIBND_ACB_L_FAC], L_SUBFR );
     224             :         }
     225             : 
     226             :         /*-----------------------------------------------------------------*
     227             :          * Synthesize speech to update mem_syn[].
     228             :          * Update A(z) filters
     229             :          *-----------------------------------------------------------------*/
     230             : 
     231        8148 :         syn_filt( p_Aq, M, &exc[i_subfr], &syn[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
     232             : 
     233        8148 :         p_Aw += ( M + 1 );
     234        8148 :         p_Aq += ( M + 1 );
     235        8148 :         pt_pitch++;
     236             :     }
     237             : 
     238             :     /* SC-VBR */
     239        2037 :     if ( st->Opt_SC_VBR )
     240             :     {
     241           0 :         st->hSC_VBR->prev_ppp_gain_pit = gain_pit;
     242           0 :         st->hSC_VBR->prev_tilt_code = hLPDmem->tilt_code;
     243             :     }
     244             : 
     245        2037 :     return;
     246             : }

Generated by: LCOV version 1.14