LCOV - code coverage report
Current view: top level - lib_enc - enc_gen_voic_rf.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 37 126 29.4 %
Date: 2025-05-23 08:37:30 Functions: 1 2 50.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 "cnst.h"
      43             : #include "prot.h"
      44             : #include "rom_com.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : /*-------------------------------------------------------------------*
      48             :  * reset_rf_indices()
      49             :  *
      50             :  * Initialization of oartial redundancy coding
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53      174278 : void reset_rf_indices(
      54             :     RF_ENC_HANDLE hRF,     /* i/o: RF state structure  */
      55             :     const int16_t L_frame, /* i  : frame length        */
      56             :     int16_t *rf_target_bits_write )
      57             : {
      58             :     int16_t i, j;
      59             : 
      60      174278 :     if ( hRF != NULL )
      61             :     {
      62        2463 :         hRF->rf_frame_type = 0; /* since this function is called every frame this will happen even for a SID frame, hence treating it as GSC frame, i.e no RF encoding */
      63             : 
      64        2463 :         hRF->rf_mem_w0 = 0;
      65        2463 :         set_f( hRF->rf_clip_var, 0, 6 );
      66        2463 :         hRF->rf_tilt_code = 0;
      67        2463 :         set_f( hRF->rf_mem_syn2, 0, M );
      68        2463 :         set_f( hRF->rf_dispMem, 0, 8 );
      69        2463 :         hRF->rf_gc_threshold = 0;
      70        2463 :         set_f( hRF->rf_tilt_buf, 0, NB_SUBFR16k );
      71             : 
      72        2463 :         hRF->rf_target_bits = 0;
      73        2463 :         *rf_target_bits_write = 0;
      74        2463 :         hRF->rf_tcxltp_pitch_int_past = L_frame;
      75        2463 :         hRF->rf_last_tns_active = 0;
      76        2463 :         hRF->rf_second_last_tns_active = 0;
      77        2463 :         hRF->rf_second_last_core = 0;
      78             : 
      79       24630 :         for ( i = 0; i < MAX_RF_FEC_OFFSET; i++ )
      80             :         {
      81       22167 :             hRF->rf_indx_frametype[i] = RF_NO_DATA;
      82       22167 :             hRF->rf_targetbits_buff[i] = 6; /* rf_mode: 1, rf_frame_type: 3, and fec_offset: 2 */
      83       22167 :             hRF->rf_indx_lsf[i][0] = 0;
      84       22167 :             hRF->rf_indx_lsf[i][1] = 0;
      85       22167 :             hRF->rf_indx_lsf[i][2] = 0;
      86       22167 :             hRF->rf_indx_EsPred[i] = 0;
      87       22167 :             hRF->rf_indx_nelp_fid[i] = 0;
      88       22167 :             hRF->rf_indx_nelp_iG1[i] = 0;
      89       22167 :             hRF->rf_indx_nelp_iG2[i][0] = 0;
      90       22167 :             hRF->rf_indx_nelp_iG2[i][1] = 0;
      91             : 
      92      133002 :             for ( j = 0; j < NB_SUBFR16k; j++ )
      93             :             {
      94      110835 :                 hRF->rf_indx_ltfMode[i][j] = 0;
      95      110835 :                 hRF->rf_indx_pitch[i][j] = 0;
      96      110835 :                 hRF->rf_indx_fcb[i][j] = 0;
      97      110835 :                 hRF->rf_indx_gain[i][j] = 0;
      98             :             }
      99             : 
     100       22167 :             hRF->rf_clas[i] = UNVOICED_CLAS;
     101       22167 :             hRF->rf_gain_tcx[i] = 0;
     102       22167 :             hRF->rf_tcxltp_param[i] = 0;
     103             : 
     104       22167 :             hRF->rf_indx_tbeGainFr[i] = 0;
     105             :         }
     106             :     }
     107             : 
     108      174278 :     return;
     109             : }
     110             : 
     111             : 
     112             : /*-------------------------------------------------------------------*
     113             :  * coder_acelp_rf()
     114             :  *
     115             :  * Encode excitation signal (partial redundancy)
     116             :  *-------------------------------------------------------------------*/
     117             : 
     118           0 : void coder_acelp_rf(
     119             :     const int16_t target_bits,   /* i  : target bits                 */
     120             :     const float speech[],        /* i  : speech[-M..lg]              */
     121             :     const int16_t coder_type,    /* i  : coding type                 */
     122             :     const int16_t rf_frame_type, /* i  : rf_frame_type               */
     123             :     const float A[],             /* i  : coefficients 4xAz[M+1]      */
     124             :     const float Aq[],            /* i  : coefficients 4xAz_q[M+1]    */
     125             :     const float voicing[],       /* i  : open-loop LTP gain          */
     126             :     const int16_t T_op[],        /* i  : open-loop LTP lag           */
     127             :     const float stab_fac,        /* i  : LP stability factor         */
     128             :     Encoder_State *st,           /* i/o: coder memory state          */
     129             :     ACELP_config *acelp_cfg,     /* i/o: configuration of the ACELP  */
     130             :     float *exc_rf,               /* i/o: pointer to RF excitation    */
     131             :     float *syn_rf                /* i/o: pointer to RF synthesis     */
     132             : )
     133             : {
     134             :     int16_t i, i_subfr, nSubfr;
     135             :     int16_t T0, T0_frac, T0_min, T0_min_frac, T0_max, T0_max_frac, T0_res;
     136             :     float Es_pred_rf;
     137             :     float gain_pit, gain_code, voice_fac;
     138             :     float prev_gain_pit;
     139             :     ACELP_CbkCorr g_corr;
     140             :     float g_corr2[6];
     141             :     const float *p_A, *p_Aq;
     142             :     float code[L_SUBFR];
     143             :     float xn[L_SUBFR], cn[L_SUBFR], h1[L_SUBFR];
     144             :     float xn2[L_SUBFR], y1[L_SUBFR], y2[L_SUBFR];
     145             :     float res_save;
     146             :     float exc2[L_SUBFR];
     147             :     float exc_nelp[L_FRAME];
     148             :     float syn2[L_FRAME16k];
     149             :     float past_gcode, gain_inov;
     150             :     int16_t clip_gain;
     151             :     float gain_code2;
     152             :     float code2[L_SUBFR];
     153             :     float y22[L_SUBFR];
     154             :     int16_t lp_select;
     155             :     int16_t *prm_rf;
     156             : 
     157           0 :     RF_ENC_HANDLE hRF = st->hRF;
     158             : 
     159             :     /*-----------------------------------------------------------------------*
     160             :      * Initialization
     161             :      *------------------------------------------------------------------------*/
     162           0 :     past_gcode = 0;
     163           0 :     gain_inov = 0;
     164           0 :     T0 = 0;
     165           0 :     T0_res = 0;
     166           0 :     T0_frac = 0;
     167           0 :     gain_pit = 0;
     168           0 :     gain_code = 0;
     169           0 :     voice_fac = 0;
     170           0 :     prev_gain_pit = 0;
     171           0 :     Es_pred_rf = 0;
     172           0 :     set_f( code, 0.0f, L_SUBFR );
     173             : 
     174             :     /*-----------------------------------------------------------------------*
     175             :      * Configure ACELP partial copy                                          *
     176             :      *-----------------------------------------------------------------------*/
     177             : 
     178           0 :     BITS_ALLOC_config_acelp( target_bits, rf_frame_type, &( hRF->acelp_cfg_rf ), 0, st->nb_subfr );
     179             : 
     180             :     /* Reset phase dispersion */
     181           0 :     if ( st->last_core > ACELP_CORE )
     182             :     {
     183           0 :         set_zero( hRF->rf_dispMem, 8 );
     184             :     }
     185             : 
     186             :     /*---------------------------------------------------------------*
     187             :      * Calculation of LP residual (filtering through A[z] filter)
     188             :      *---------------------------------------------------------------*/
     189             : 
     190           0 :     calc_residu( speech, exc_rf, Aq, st->L_frame );
     191             : 
     192             :     /*------------------------------------------------------------------------*
     193             :      * Find and quantize mean_ener_code for gain quantizer                    *
     194             :      *------------------------------------------------------------------------*/
     195             : 
     196           0 :     Es_pred_rf = 0;
     197           0 :     if ( acelp_cfg->nrg_mode > 0 && rf_frame_type != RF_NELP )
     198             :     {
     199           0 :         Es_pred_enc( &Es_pred_rf, &hRF->rf_indx_EsPred[0], st->L_frame, L_SUBFR, exc_rf, voicing, acelp_cfg->nrg_bits, acelp_cfg->nrg_mode > 1 );
     200             :     }
     201             : 
     202             :     /*------------------------------------------------------------------*
     203             :      * ACELP subframe loop
     204             :      *------------------------------------------------------------------*/
     205             : 
     206           0 :     p_A = A;
     207           0 :     p_Aq = Aq;
     208             : 
     209           0 :     res_save = exc_rf[0];
     210           0 :     nSubfr = 0;
     211             : 
     212           0 :     for ( i_subfr = 0; i_subfr < st->L_frame; i_subfr += L_SUBFR )
     213             :     {
     214           0 :         if ( rf_frame_type != RF_NELP )
     215             :         {
     216             :             /* Restore exc[i_subfr] and save next exc[L_SUBFR+i_subfr] */
     217           0 :             exc_rf[i_subfr] = res_save;
     218           0 :             res_save = exc_rf[L_SUBFR + i_subfr];
     219             : 
     220             :             /*--------------------------------------------------------------------------*
     221             :              * Find target for pitch search (xn[]), target for innovation search (cn[]) *
     222             :              * and impulse response of the weighted synthesis filter (h1[]).            *
     223             :              *--------------------------------------------------------------------------*/
     224             : 
     225           0 :             find_targets( speech, &syn_rf[i_subfr - M], i_subfr, &( hRF->rf_mem_w0 ), p_Aq, exc_rf, L_SUBFR, p_A, st->preemph_fac, xn, cn, h1 );
     226             :         }
     227             : 
     228             :         /* full frame nelp partial copy encoding */
     229           0 :         if ( rf_frame_type == RF_NELP )
     230             :         {
     231           0 :             if ( i_subfr == 0 )
     232             :             {
     233           0 :                 nelp_encoder( st, exc_rf, exc_nelp, 0 );
     234             :             }
     235           0 :             mvr2r( &exc_nelp[i_subfr], exc2, L_SUBFR );
     236           0 :             mvr2r( &exc_nelp[i_subfr], exc_rf, L_SUBFR );
     237             :         }
     238             :         else
     239             :         {
     240             :             /*-----------------------------------------------------------------*
     241             :              * Gain clipping test to avoid unstable synthesis on frame erasure
     242             :              * or in case of floating point encoder & fixed p. decoder
     243             :              *-----------------------------------------------------------------*/
     244             : 
     245           0 :             clip_gain = gp_clip( st->element_mode, st->core_brate, voicing, i_subfr, coder_type, xn, hRF->rf_clip_var );
     246             : 
     247             :             /*-----------------------------------------------------------------*
     248             :              * - find unity gain pitch excitation (adaptive codebook entry)    *
     249             :              *   with fractional interpolation.                                *
     250             :              * - find filtered pitch exc. y1[]=exc[] convolved with h1[])      *
     251             :              * - compute pitch gain1                                           *
     252             :              *-----------------------------------------------------------------*/
     253             : 
     254           0 :             if ( acelp_cfg->gains_mode[i_subfr / L_SUBFR] == 0 )
     255             :             {
     256           0 :                 gain_pit = prev_gain_pit;
     257             :             }
     258             : 
     259           0 :             if ( acelp_cfg->ltp_bits != 0 )
     260             :             {
     261           0 :                 prm_rf = &hRF->rf_indx_pitch[0][nSubfr];
     262             : 
     263             :                 /* Adaptive Codebook (GC and VC) */
     264           0 :                 Mode2_pit_encode( acelp_cfg->ltp_mode, i_subfr, &prm_rf, &exc_rf[i_subfr], T_op, &T0_min, &T0_min_frac, &T0_max, &T0_max_frac, &T0, &T0_frac, &T0_res, h1, xn, st->pit_min, st->pit_fr1, st->pit_fr1b, st->pit_fr2, st->pit_max, st->pit_res_max );
     265             : 
     266             :                 /* find ACB excitation */
     267           0 :                 if ( T0_res == ( st->pit_res_max >> 1 ) ) /* st->pit_res_max is 4 for 12.8kHz core */
     268             :                 {
     269           0 :                     pred_lt4( &exc_rf[i_subfr], &exc_rf[i_subfr], T0, T0_frac << 1, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     270             :                 }
     271             :                 else
     272             :                 {
     273           0 :                     pred_lt4( &exc_rf[i_subfr], &exc_rf[i_subfr], T0, T0_frac, L_SUBFR + 1, inter4_2, L_INTERPOL2, PIT_UP_SAMP );
     274             :                 }
     275             : 
     276             : 
     277             :                 /* filter adaptive codebook */
     278           0 :                 lp_select = lp_filt_exc_enc( MODE2, ( acelp_cfg->gains_mode[i_subfr / L_SUBFR] > 0 ) ? ( acelp_cfg->gains_mode[i_subfr / L_SUBFR] ) : ( 100 ), i_subfr, exc_rf, h1, xn, y1, xn2, L_SUBFR, st->L_frame, g_corr2, clip_gain, &( gain_pit ), &( acelp_cfg->ltf_mode ) );
     279             : 
     280           0 :                 if ( acelp_cfg->ltf_mode == NORMAL_OPERATION )
     281             :                 {
     282           0 :                     hRF->rf_indx_ltfMode[0][nSubfr] = lp_select;
     283             :                 }
     284             : 
     285           0 :                 g_corr.y1y1 = g_corr2[0];
     286           0 :                 g_corr.xy1 = -0.5f * ( g_corr2[1] - 0.01f ) + 0.01f;
     287             :             }
     288             :             else
     289             :             {
     290           0 :                 gain_pit = 0.f;
     291           0 :                 g_corr.xy1 = 0.f;
     292           0 :                 g_corr.y1y1 = 0.f;
     293           0 :                 set_zero( y1, L_SUBFR );
     294           0 :                 set_zero( exc_rf + i_subfr, L_SUBFR );
     295           0 :                 T0 = L_SUBFR;
     296           0 :                 T0_frac = 0;
     297           0 :                 T0_res = 1;
     298             :             }
     299             : 
     300             :             /*----------------------------------------------------------------------*
     301             :              *                 Encode the algebraic innovation                      *
     302             :              *----------------------------------------------------------------------*/
     303             : 
     304           0 :             if ( acelp_cfg->fixed_cdk_index[i_subfr / L_SUBFR] >= 0 )
     305             :             {
     306           0 :                 prm_rf = &hRF->rf_indx_fcb[0][nSubfr];
     307           0 :                 E_ACELP_innovative_codebook( exc_rf, T0, T0_frac, T0_res, gain_pit, hRF->rf_tilt_code, acelp_cfg, i_subfr, p_Aq, h1, xn, cn, y1, y2, st->acelp_autocorr, &prm_rf, code, st->L_frame, st->last_L_frame, st->total_brate );
     308             :             }
     309             :             else
     310             :             {
     311           0 :                 set_f( code, 0.0f, L_SUBFR );
     312           0 :                 set_f( y2, 0.0f, L_SUBFR );
     313             :             }
     314             : 
     315           0 :             if ( i_subfr < ( st->L_frame - L_SUBFR ) )
     316             :             {
     317           0 :                 E_corr_xy2( xn, y1, y2, g_corr2, L_SUBFR );
     318           0 :                 g_corr.y2y2 = 0.01F + g_corr2[2];
     319           0 :                 g_corr.xy2 = 0.01F + -0.5f * g_corr2[3];
     320           0 :                 g_corr.y1y2 = 0.01F + 0.5f * g_corr2[4];
     321             : 
     322           0 :                 g_corr.xx = 0.01F + dotp( xn, xn, L_SUBFR );
     323             : 
     324             :                 /*----------------------------------------------------------------------*
     325             :                  *                 Add Gaussian excitation                              *
     326             :                  *----------------------------------------------------------------------*/
     327             : 
     328           0 :                 gain_code2 = 0.f;
     329           0 :                 set_zero( code2, L_SUBFR );
     330           0 :                 set_zero( y22, L_SUBFR );
     331             : 
     332             :                 /*----------------------------------------------------------*
     333             :                  *  - Compute the fixed codebook gain                       *
     334             :                  *  - quantize fixed codebook gain                          *
     335             :                  *----------------------------------------------------------*/
     336             : 
     337           0 :                 if ( acelp_cfg->gains_mode[i_subfr / L_SUBFR] != 0 )
     338             :                 {
     339           0 :                     prm_rf = &hRF->rf_indx_gain[0][nSubfr];
     340           0 :                     encode_acelp_gains( code, acelp_cfg->gains_mode[i_subfr / L_SUBFR], Es_pred_rf, clip_gain, &g_corr, &gain_pit, &gain_code, &prm_rf, &past_gcode, &gain_inov, L_SUBFR, code2, &gain_code2, st->flag_noisy_speech_snr );
     341             :                 }
     342             : 
     343           0 :                 gp_clip_test_gain_pit( st->element_mode, st->core_brate, gain_pit, hRF->rf_clip_var );
     344             : 
     345             :                 /*----------------------------------------------------------*
     346             :                  * - voice factor (for codebook tilt sharpening)            *
     347             :                  *----------------------------------------------------------*/
     348             : 
     349           0 :                 hRF->rf_tilt_code = est_tilt( exc_rf + i_subfr, gain_pit, code, gain_code, &voice_fac, L_SUBFR, acelp_cfg->voice_tilt );
     350             : 
     351             :                 /*-----------------------------------------------------------------*
     352             :                  * Update memory of the weighting filter
     353             :                  *-----------------------------------------------------------------*/
     354             : 
     355           0 :                 hRF->rf_mem_w0 = xn[L_SUBFR - 1] - gain_pit * y1[L_SUBFR - 1] - gain_code * y2[L_SUBFR - 1] - gain_code2 * y22[L_SUBFR - 1];
     356             : 
     357             :                 /*-------------------------------------------------------*
     358             :                  * - Find the total excitation.                          *
     359             :                  *-------------------------------------------------------*/
     360             : 
     361           0 :                 for ( i = 0; i < L_SUBFR; i++ )
     362             :                 {
     363           0 :                     exc2[i] = gain_pit * exc_rf[i + i_subfr];
     364           0 :                     exc2[i] += gain_code2 * code2[i];
     365           0 :                     exc_rf[i + i_subfr] = exc2[i] + gain_code * code[i];
     366             :                 }
     367             : 
     368             :                 /*---------------------------------------------------------*
     369             :                  * Enhance the excitation                                  *
     370             :                  *---------------------------------------------------------*/
     371             : 
     372           0 :                 enhancer( MODE2, -1, acelp_cfg->fixed_cdk_index[i_subfr / L_SUBFR], 0, coder_type, st->L_frame, voice_fac, stab_fac, past_gcode, gain_inov, &hRF->rf_gc_threshold, code, exc2, gain_pit, hRF->rf_dispMem );
     373             :             }
     374             :         }
     375             : 
     376           0 :         if ( ( i_subfr < ( st->L_frame - L_SUBFR ) ) || ( rf_frame_type != RF_NELP ) )
     377             :         {
     378             :             /*----------------------------------------------------------*
     379             :              * - compute the synthesis speech                           *
     380             :              *----------------------------------------------------------*/
     381             : 
     382           0 :             syn_filt( p_Aq, M, exc2, &syn2[i_subfr], L_SUBFR, hRF->rf_mem_syn2, 1 );
     383             : 
     384           0 :             syn_filt( p_Aq, M, &exc_rf[i_subfr], &syn_rf[i_subfr], L_SUBFR, &syn_rf[i_subfr - M], 0 );
     385             : 
     386             :             /*----------------------------------------------------------*
     387             :              * Updates                                                  *
     388             :              *----------------------------------------------------------*/
     389             : 
     390           0 :             p_A += ( M + 1 );
     391           0 :             p_Aq += ( M + 1 );
     392           0 :             nSubfr++;
     393             : 
     394             :             /* copy current gain for next subframe use, in case there is no explicit encoding */
     395           0 :             prev_gain_pit = gain_pit;
     396             :         }
     397             : 
     398             :     } /* end of subframe loop */
     399             : 
     400           0 :     return;
     401             : }

Generated by: LCOV version 1.14