LCOV - code coverage report
Current view: top level - lib_enc - gs_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 198 208 95.2 %
Date: 2025-05-23 08:37:30 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             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "cnst.h"
      44             : #include "rom_com.h"
      45             : #include "prot.h"
      46             : #include "ivas_prot.h"
      47             : #include "wmc_auto.h"
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * Local function prototypes
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53             : static float edyn( const float *vec, const int16_t lvec );
      54             : 
      55             : 
      56             : /*-------------------------------------------------------------------*
      57             :  * encod_audio()
      58             :  *
      59             :  * Encode audio (AC) frames
      60             :  *-------------------------------------------------------------------*/
      61             : 
      62       15245 : void encod_audio(
      63             :     Encoder_State *st,                  /* i/o: State structure                              */
      64             :     const float speech[],               /* i  : input speech                                 */
      65             :     const float Aw[],                   /* i  : weighted A(z) unquantized for subframes      */
      66             :     const float Aq[],                   /* i  : 12k8 Lp coefficient                          */
      67             :     const float *res,                   /* i  : residual signal                              */
      68             :     float *synth,                       /* i/o: core synthesis                               */
      69             :     float *exc,                         /* i/o: current non-enhanced excitation              */
      70             :     float *pitch_buf,                   /* i/o: floating pitch values for each subframe      */
      71             :     float *voice_factors,               /* o  : voicing factors                              */
      72             :     float *bwe_exc,                     /* o  : excitation for SWB TBE                       */
      73             :     const int16_t attack_flag,          /* i  : attack flag (GSC or TC)                      */
      74             :     float *lsf_new,                     /* i  : current frame ISF vector                     */
      75             :     float *tmp_noise,                   /* o  : long-term noise energy                       */
      76             :     const int16_t tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag             */
      77             :     const float tdm_Pri_pitch_buf[]     /* i  : primary channel pitch buffer                 */
      78             : )
      79             : {
      80             :     const float *p_Aq;
      81             :     int16_t i, i_subfr, nb_subfr, last_pit_bin;
      82             :     int16_t T0_tmp, T0_frac_tmp, nb_subfr_flag;
      83             :     int16_t tmp_nb_bits_tot;
      84             :     GSC_ENC_HANDLE hGSCEnc;
      85             :     float Es_pred;
      86             :     float dct_res[L_FRAME16k], dct_epit[L_FRAME16k];
      87             :     float m_mean;
      88             :     float exc_wo_nf[L_FRAME16k];
      89             :     int16_t nb_bits; /*number of bits*/
      90       15245 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
      91       15245 :     LPD_state_HANDLE hLPDmem = st->hLPDmem;
      92             : 
      93             :     /*---------------------------------------------------------------*
      94             :      * Initialization
      95             :      *---------------------------------------------------------------*/
      96             : 
      97       15245 :     hGSCEnc = st->hGSCEnc;
      98             : 
      99       15245 :     m_mean = 0.0f;
     100             : 
     101       15245 :     T0_tmp = 64;
     102       15245 :     T0_frac_tmp = 0;
     103       15245 :     mvr2r( hLPDmem->mem_syn, hGSCEnc->mem_syn_tmp, M );
     104       15245 :     hGSCEnc->mem_w0_tmp = hLPDmem->mem_w0;
     105       15245 :     Es_pred = 0;
     106             : 
     107             :     /*---------------------------------------------------------------*
     108             :      * Encode GSC IVAS mode
     109             :      * Encode GSC attack flag (used to reduce possible pre-echo)
     110             :      * Encode GSC SWB speech flag
     111             :      *---------------------------------------------------------------*/
     112             : 
     113       15245 :     if ( st->element_mode > EVS_MONO && st->idchan == 0 )
     114             :     {
     115       15239 :         push_indice( hBstr, IND_GSC_IVAS_SP, st->GSC_IVAS_mode, 2 );
     116             :     }
     117             : 
     118       15245 :     if ( attack_flag > 0 )
     119             :     {
     120           0 :         push_indice( hBstr, IND_GSC_ATTACK, 1, 1 );
     121             :     }
     122             :     else
     123             :     {
     124       15245 :         push_indice( hBstr, IND_GSC_ATTACK, 0, 1 );
     125             :     }
     126             : 
     127       15245 :     if ( st->GSC_IVAS_mode >= 1 || ( st->coder_type != INACTIVE && ( ( st->element_mode == EVS_MONO && st->total_brate >= ACELP_13k20 ) ||
     128        6245 :                                                                      ( st->element_mode > EVS_MONO && st->total_brate > MIN_BRATE_GSC_NOISY_FLAG && st->bwidth >= SWB && !st->flag_ACELP16k ) ) ) )
     129             :     {
     130        5675 :         push_indice( hBstr, IND_GSC_SWB_SPEECH, st->GSC_noisy_speech, 1 );
     131             :     }
     132             : 
     133             :     /*---------------------------------------------------------------*+
     134             :      * Find and encode the number of subframes
     135             :      *---------------------------------------------------------------*/
     136             : 
     137       15245 :     if ( st->core_brate >= ACELP_9k60 && st->core_brate <= ACELP_13k20 )
     138             :     {
     139       21876 :         for ( i = 0; i < 5; i++ )
     140             :         {
     141       18230 :             if ( fabs( st->hSpMusClas->gsc_lt_diff_etot[MAX_LT - i - 1] ) > 6.0f && hGSCEnc->cor_strong_limit == 1 )
     142             :             {
     143         103 :                 hGSCEnc->cor_strong_limit = 0;
     144             :             }
     145             :         }
     146             :     }
     147             : 
     148       15245 :     if ( st->GSC_IVAS_mode >= 1 || ( st->GSC_noisy_speech == 1 && st->GSC_IVAS_mode == 0 ) )
     149             :     {
     150        5673 :         nb_subfr = NB_SUBFR;
     151        5673 :         if ( st->GSC_IVAS_mode > 0 && st->GSC_IVAS_mode < 3 && st->core_brate < GSC_L_RATE_STG )
     152             :         {
     153        2043 :             nb_subfr = 2;
     154             :         }
     155             : 
     156        5673 :         hGSCEnc->cor_strong_limit = 0;
     157        5673 :         nb_subfr_flag = 1;
     158             :     }
     159        9572 :     else if ( st->L_frame == L_FRAME16k && ( st->core_brate <= ACELP_13k20 || st->coder_type == INACTIVE ) )
     160             :     {
     161        2637 :         hGSCEnc->cor_strong_limit = 0;
     162        2637 :         nb_subfr = SWNB_SUBFR;
     163        2637 :         nb_subfr_flag = 1;
     164             :     }
     165             :     else
     166             :     {
     167        6935 :         if ( ( hGSCEnc->cor_strong_limit == 0 || st->coder_type == INACTIVE ) && st->core_brate >= ACELP_9k60 )
     168             :         {
     169        1298 :             nb_subfr = 2;
     170        1298 :             nb_subfr_flag = 0;
     171        1298 :             hGSCEnc->cor_strong_limit = 0;
     172             :         }
     173             :         else
     174             :         {
     175        5637 :             nb_subfr = SWNB_SUBFR;
     176        5637 :             nb_subfr_flag = 1;
     177             :         }
     178             : 
     179        6935 :         if ( st->L_frame == L_FRAME16k && ( st->hSpMusClas->mold_corr > .8f && st->core_brate >= MIN_RATE_4SBFR && st->coder_type != INACTIVE ) )
     180             :         {
     181           0 :             nb_subfr *= 2;
     182           0 :             nb_subfr_flag |= 0x2;
     183             :         }
     184             : 
     185        6935 :         if ( st->L_frame == L_FRAME16k && st->core_brate >= MIN_RATE_4SBFR )
     186             :         {
     187           0 :             push_indice( hBstr, IND_HF_NOISE, nb_subfr_flag, 2 );
     188             :         }
     189        6935 :         else if ( st->core_brate >= ACELP_9k60 )
     190             :         {
     191             :             /* nb_subfr_flag can only have the value 0 or 1 */
     192        1305 :             push_indice( hBstr, IND_HF_NOISE, nb_subfr_flag, 1 );
     193             :         }
     194             :     }
     195             : 
     196       15245 :     if ( st->L_frame == L_FRAME16k && nb_subfr == NB_SUBFR )
     197             :     {
     198           0 :         nb_subfr = NB_SUBFR16k;
     199             :     }
     200             : 
     201             :     /*---------------------------------------------------------------*
     202             :      * Compute adaptive (pitch) excitation contribution
     203             :      *---------------------------------------------------------------*/
     204             : 
     205       15245 :     if ( !( st->GSC_IVAS_mode > 0 && st->L_frame / nb_subfr == 2 * L_SUBFR && st->GSC_IVAS_mode < 3 ) &&
     206       13202 :          ( ( st->core_brate >= MIN_RATE_FCB || st->GSC_noisy_speech ) &&
     207        1557 :            ( ( nb_subfr == NB_SUBFR && st->L_frame == L_FRAME ) || ( nb_subfr == NB_SUBFR16k && st->L_frame == L_FRAME16k ) ) ) )
     208             :     {
     209         689 :         if ( st->element_mode > EVS_MONO )
     210             :         {
     211         689 :             nb_bits = 5;
     212             :         }
     213             :         else
     214             :         {
     215           0 :             nb_bits = Es_pred_bits_tbl[BIT_ALLOC_IDX( st->core_brate, GENERIC, -1, -1 )];
     216             :         }
     217         689 :         Es_pred_enc( &Es_pred, &i, st->L_frame, L_SUBFR, res, st->voicing, nb_bits, 0 );
     218         689 :         push_indice( hBstr, IND_ES_PRED, i, nb_bits );
     219             :     }
     220             : 
     221       15245 :     enc_pit_exc( st, speech, Aw, Aq, Es_pred, res, synth, exc, &T0_tmp, &T0_frac_tmp, pitch_buf, nb_subfr, &hGSCEnc->lt_gpitch, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
     222             : 
     223             :     /*---------------------------------------------------------------*
     224             :      * DCT transform
     225             :      *---------------------------------------------------------------*/
     226             : 
     227       15245 :     edct( exc, dct_epit, st->L_frame, st->element_mode );
     228             : 
     229       15245 :     edct( res, dct_res, st->L_frame, st->element_mode );
     230             : 
     231             :     /*---------------------------------------------------------------*
     232             :      * Calculate energy dynamics
     233             :      *---------------------------------------------------------------*/
     234             : 
     235      137205 :     for ( i = 7; i < 15; i++ )
     236             :     {
     237      121960 :         m_mean += edyn( dct_res + i * 16, 16 );
     238             :     }
     239       15245 :     m_mean *= 0.125f;
     240             : 
     241       15245 :     if ( m_mean > hGSCEnc->mid_dyn )
     242             :     {
     243        6895 :         hGSCEnc->mid_dyn = 0.2f * hGSCEnc->mid_dyn + 0.8f * m_mean;
     244             :     }
     245             :     else
     246             :     {
     247        8350 :         hGSCEnc->mid_dyn = 0.6f * hGSCEnc->mid_dyn + 0.4f * m_mean;
     248             :     }
     249             : 
     250       15245 :     if ( st->coder_type != INACTIVE )
     251             :     {
     252       11096 :         hGSCEnc->noise_lev = ( NOISE_LEVEL_SP3 + 1 ) - usquant( hGSCEnc->mid_dyn, &m_mean, MIN_DYNAMIC, DYNAMIC_RANGE / GSC_NF_STEPS, GSC_NF_STEPS );
     253       11096 :         if ( hGSCEnc->noise_lev > NOISE_LEVEL_SP3 )
     254             :         {
     255        5424 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     256             :         }
     257             :     }
     258             : 
     259       15245 :     hGSCEnc->past_dyn_dec = hGSCEnc->noise_lev;
     260             : 
     261       15245 :     if ( st->GSC_IVAS_mode >= 1 )
     262             :     {
     263        4984 :         hGSCEnc->noise_lev = NOISE_LEVEL_SP2;
     264        4984 :         if ( st->GSC_IVAS_mode == 3 ) /* Music like */
     265             :         {
     266        2900 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP0;
     267             :         }
     268        2084 :         else if ( st->GSC_noisy_speech == 0 )
     269             :         {
     270        1972 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     271             :         }
     272             :     }
     273       10261 :     else if ( st->core_brate <= ACELP_8k00 )
     274             :     {
     275        5083 :         if ( hGSCEnc->noise_lev <= NOISE_LEVEL_SP2 )
     276             :         {
     277        2966 :             hGSCEnc->noise_lev = NOISE_LEVEL_SP2;
     278             :         }
     279             : 
     280        5083 :         push_indice( hBstr, IND_NOISE_LEVEL, hGSCEnc->noise_lev - NOISE_LEVEL_SP2, 2 );
     281             :     }
     282        5178 :     else if ( st->GSC_noisy_speech )
     283             :     {
     284         689 :         hGSCEnc->noise_lev = NOISE_LEVEL_SP3;
     285             :     }
     286             :     else
     287             :     {
     288        4489 :         push_indice( hBstr, IND_NOISE_LEVEL, hGSCEnc->noise_lev - NOISE_LEVEL_SP0, 3 );
     289             :     }
     290             : 
     291             :     /*---------------------------------------------------------------*
     292             :      * Find and encode the last band where the adaptive (pitch) contribution is significant
     293             :      *---------------------------------------------------------------*/
     294             : 
     295       15245 :     last_pit_bin = Pit_exc_contribution_len( st, dct_res, dct_epit, pitch_buf, &hGSCEnc->pit_exc_hangover );
     296             : 
     297       15245 :     if ( last_pit_bin == 0 )
     298             :     {
     299        2242 :         hLPDmem->tilt_code = 0.0f;
     300             :     }
     301             :     else
     302             :     {
     303       13003 :         last_pit_bin++;
     304             :     }
     305             : 
     306             :     /*--------------------------------------------------------------------------------------*
     307             :      * GSC encoder
     308             :      *--------------------------------------------------------------------------------------*/
     309             : 
     310             :     /* Find the current total number of bits used */
     311       15245 :     tmp_nb_bits_tot = hBstr->nb_bits_tot;
     312             : 
     313       15245 :     if ( st->extl_brate > 0 )
     314             :     {
     315             :         /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
     316       15129 :         tmp_nb_bits_tot--;
     317             :     }
     318             : 
     319       15245 :     if ( st->coder_type == INACTIVE && st->core_brate <= ACELP_9k60 && st->idchan == 0 )
     320             :     {
     321             :         /* add 5 bits for noisiness */
     322         350 :         tmp_nb_bits_tot += 5;
     323             :     }
     324             : 
     325       15245 :     gsc_enc( st, dct_res, dct_epit, last_pit_bin, tmp_nb_bits_tot, nb_subfr, lsf_new, exc_wo_nf, tmp_noise );
     326             : 
     327             :     /*--------------------------------------------------------------------------------------*
     328             :      * iDCT transform
     329             :      *--------------------------------------------------------------------------------------*/
     330             : 
     331       15245 :     edct( dct_epit, exc, st->L_frame, st->element_mode );
     332             : 
     333       15245 :     edct( exc_wo_nf, exc_wo_nf, st->L_frame, st->element_mode );
     334             : 
     335             :     /*--------------------------------------------------------------------------------------*
     336             :      * Remove potential pre-echo in case an onset has been detected
     337             :      *--------------------------------------------------------------------------------------*/
     338             : 
     339       15245 :     pre_echo_att( &hGSCEnc->Last_frame_ener, exc, attack_flag, st->last_coder_type, st->L_frame );
     340             : 
     341             :     /*--------------------------------------------------------------------------------------*
     342             :      * Update BWE excitation
     343             :      *--------------------------------------------------------------------------------------*/
     344             : 
     345       15245 :     if ( st->hBWE_TD != NULL )
     346             :     {
     347       15245 :         if ( st->L_frame == L_FRAME16k )
     348             :         {
     349        2637 :             set_f( voice_factors, 0.0f, NB_SUBFR16k );
     350        2637 :             interp_code_4over2( exc, bwe_exc, L_FRAME16k );
     351             :         }
     352             :         else
     353             :         {
     354       12608 :             set_f( voice_factors, 0.0f, NB_SUBFR );
     355       12608 :             interp_code_5over2( exc, bwe_exc, L_FRAME );
     356             :         }
     357             :     }
     358             : 
     359             :     /*--------------------------------------------------------------------------------------*
     360             :      * Synthesis
     361             :      *--------------------------------------------------------------------------------------*/
     362             : 
     363       15245 :     p_Aq = Aq;
     364       78862 :     for ( i_subfr = 0; i_subfr < st->L_frame; i_subfr += L_SUBFR )
     365             :     {
     366       63617 :         syn_filt( p_Aq, M, &exc_wo_nf[i_subfr], &synth[i_subfr], L_SUBFR, hLPDmem->mem_syn, 1 );
     367       63617 :         p_Aq += ( M + 1 );
     368             :     }
     369             : 
     370             :     /*--------------------------------------------------------------------------------------*
     371             :      * Updates
     372             :      *--------------------------------------------------------------------------------------*/
     373             : 
     374       15245 :     hLPDmem->mem_w0 = hGSCEnc->mem_w0_tmp;
     375       15245 :     mvr2r( exc_wo_nf, exc, st->L_frame );
     376             : 
     377       15245 :     return;
     378             : }
     379             : 
     380             : /*-------------------------------------------------------------------*
     381             :  * gsc_enc()
     382             :  *
     383             :  * Generic audio signal encoder
     384             :  *-------------------------------------------------------------------*/
     385             : 
     386       22806 : void gsc_enc(
     387             :     Encoder_State *st,       /* i/o: State structure                                       */
     388             :     float res_dct_in[],      /* i  : dct of residual signal                                */
     389             :     float exc_dct_in[],      /* i/o: dct of pitch-only excitation / total excitation       */
     390             :     const int16_t Diff_len,  /* i  : Lenght of the difference signal (before pure spectral)*/
     391             :     const int16_t bits_used, /* i  : Number of bit used before frequency Q                 */
     392             :     const int16_t nb_subfr,  /* i  : Number of subframe considered                         */
     393             :     const float *lsf_new,    /* i  : ISFs at the end of the frame                          */
     394             :     float *exc_wo_nf,        /* o  : excitation (in f domain) without noisefill            */
     395             :     float *tmp_noise         /* o  : long-term noise energy                                */
     396             : )
     397             : {
     398             :     int16_t i;
     399             :     GSC_ENC_HANDLE hGSCEnc;
     400             :     BSTR_ENC_HANDLE hBstr;
     401             :     float exc_diffQ[L_FRAME16k];
     402             :     float exc_diff[L_FRAME16k];
     403             :     int16_t bit;
     404             :     int16_t nb_subbands;
     405             :     int16_t pvq_len;
     406             :     int16_t bits_per_bands[MBANDS_GN_BITALLOC16k]; /*Q3*/
     407             :     int16_t tmp_band;
     408             :     float concat_in[L_FRAME16k];
     409             :     float concat_out[L_FRAME16k];
     410             :     int16_t max_ener_band[MBANDS_GN_BITALLOC16k], j;
     411             :     float Ener_per_bd_iQ[MBANDS_GN16k];
     412             :     int16_t last_bin;
     413             :     int16_t bitallocation_band[MBANDS_GN_BITALLOC16k];
     414             :     int16_t bitallocation_exc[2];
     415             :     int16_t npulses[NB_SFM];
     416             :     int16_t maxpulse[NB_SFM];
     417             :     float mean_gain;
     418             :     int16_t seed_init;
     419             :     float max_eq, max_eq_val;
     420             : 
     421             :     /*--------------------------------------------------------------------------------------*
     422             :      * Initialization
     423             :      *--------------------------------------------------------------------------------------*/
     424             : 
     425       22806 :     hGSCEnc = st->hGSCEnc;
     426       22806 :     hBstr = st->hBstr;
     427             : 
     428       22806 :     bit = bits_used;
     429       22806 :     if ( st->coder_type == INACTIVE && ( st->tdm_LRTD_flag == 1 || st->element_mode == IVAS_SCE ) && st->core_brate <= GSC_LRES_GAINQ_LIMIT )
     430             :     {
     431        7044 :         bit += GSC_LRES_NB_NITS;
     432             :     }
     433             : 
     434       22806 :     set_f( exc_diffQ, 0.0f, st->L_frame );
     435       22806 :     set_f( Ener_per_bd_iQ, 0.0f, MBANDS_GN16k );
     436             : 
     437             :     /*--------------------------------------------------------------------------------------*
     438             :      * Calculate the difference between the residual spectrum and the spectrum of adaptive excitation
     439             :      * (non valuable temporal content present in exc_dct_in is already zeroed)
     440             :      *--------------------------------------------------------------------------------------*/
     441             : 
     442       22806 :     v_sub( res_dct_in, exc_dct_in, exc_diff, st->L_frame );
     443       22806 :     exc_diff[0] = 0;
     444             : 
     445             :     /*--------------------------------------------------------------------------------------*
     446             :      * Multiply the difference spectrum with the normalized spectral shape of the residual signal
     447             :      * This improves the stability of the differnece spectrum since the spectral shape of the
     448             :      * residual signal is less suseptible to rapid changes than the difference spectrum
     449             :      *--------------------------------------------------------------------------------------*/
     450             : 
     451       22806 :     if ( Diff_len == 0 )
     452             :     {
     453        2242 :         tmp_band = 0;
     454             :     }
     455             :     else
     456             :     {
     457       20564 :         tmp_band = hGSCEnc->mem_last_pit_band;
     458             :     }
     459             : 
     460       22806 :     Ener_per_band_comp( exc_diff, Ener_per_bd_iQ, MBANDS_GN, 1, st->L_frame );
     461             : 
     462             :     /*--------------------------------------------------------------------------------------*
     463             :      * Gain quantizaion
     464             :      *--------------------------------------------------------------------------------------*/
     465             : 
     466       22806 :     i = 0;
     467      106092 :     while ( i < SIZE_BRATE_INTERMED_TBL )
     468             :     {
     469      106092 :         if ( st->core_brate <= brate_intermed_tbl[i] )
     470             :         {
     471       22806 :             break;
     472             :         }
     473       83286 :         i++;
     474             :     }
     475       22806 :     if ( st->element_mode > EVS_MONO && st->coder_type == AUDIO &&
     476       11096 :          st->core_brate <= STEREO_GSC_BIT_RATE_ALLOC && brate_intermed_tbl[i] == ACELP_9k60 ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */
     477             :     {
     478        1383 :         i--;
     479             :     }
     480             : 
     481       22806 :     mean_gain = gsc_gainQ( hBstr, st->element_mode, st->idchan, Ener_per_bd_iQ, Ener_per_bd_iQ, brate_intermed_tbl[i], st->coder_type, st->bwidth, st->L_frame, st->tdm_LRTD_flag, st->core_brate );
     482             : 
     483       22806 :     *tmp_noise = 10.0f * mean_gain;
     484             : 
     485             :     /*--------------------------------------------------------------------------------------*
     486             :      * PVQ encoder
     487             :      *--------------------------------------------------------------------------------------*/
     488             : 
     489       22806 :     bands_and_bit_alloc( hGSCEnc->cor_strong_limit, hGSCEnc->noise_lev, st->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ, max_ener_band, bits_per_bands, &nb_subbands, exc_diff, concat_in, &pvq_len, st->coder_type, st->bwidth, st->GSC_noisy_speech, st->L_frame, st->element_mode, st->GSC_IVAS_mode );
     490             : 
     491       22806 :     set_s( npulses, 0, NB_SFM );
     492             : 
     493       22806 :     if ( bit == 0 )
     494             :     {
     495           0 :         set_zero( concat_out, L_FRAME16k );
     496             :     }
     497             :     else
     498             :     {
     499       22806 :         bit -= pvq_core_enc( hBstr, concat_in, concat_out, bit, nb_subbands, gsc_sfm_start, gsc_sfm_end, gsc_sfm_size, bits_per_bands, NULL, npulses, maxpulse, ACELP_CORE );
     500             :     }
     501             : 
     502             :     /* write unused bits */
     503       22806 :     while ( bit > 0 )
     504             :     {
     505           0 :         i = min( bit, 16 );
     506           0 :         push_indice( hBstr, IND_UNUSED, 0, i );
     507           0 :         bit -= i;
     508             :     }
     509             : 
     510             :     /* Reorder Q bands */
     511       22806 :     last_bin = 0;
     512       22806 :     set_s( bitallocation_band, 0, MBANDS_GN_BITALLOC16k );
     513       22806 :     seed_init = 0;
     514       22806 :     max_eq = 0;
     515       22806 :     max_eq_val = 1.0f;
     516             : 
     517       22806 :     if ( ( ( ( st->core_brate < ACELP_7k20 && st->GSC_noisy_speech == 1 ) || st->core_brate < 6000 ) && st->coder_type <= UNVOICED ) || st->GSC_IVAS_mode >= 1 )
     518             :     {
     519       12483 :         j = emaximum( concat_out, nb_subbands * 16, &max_eq );
     520       12483 :         max_eq = max_eq_val / ( fabsf( concat_out[j] ) + 0.01f );
     521       12483 :         max_eq = min( max_eq_val, max_eq );
     522             :     }
     523             : 
     524      186633 :     for ( j = 0; j < nb_subbands; j++ )
     525             :     {
     526      163827 :         mvr2r( concat_out + j * 16, exc_diffQ + max_ener_band[j] * 16, 16 );
     527             : 
     528      163827 :         if ( max_ener_band[j] > last_bin )
     529             :         {
     530      118189 :             last_bin = max_ener_band[j];
     531             :         }
     532             : 
     533      163827 :         bitallocation_band[max_ener_band[j]] = 1;
     534             : 
     535      163827 :         seed_init += npulses[j];
     536             :     }
     537       22806 :     if ( st->last_coder_type != AUDIO         /* First audio frame */
     538       13544 :          && st->last_coder_type != UNVOICED ) /* last_coder_type == INACTIVE is overwritten in update_enc to UNVOICED */
     539             :     {
     540      461485 :         for ( j = 0; j < nb_subbands * 16; j++ )
     541             :         {
     542      458032 :             if ( concat_out[j] > 0 )
     543             :             {
     544       66132 :                 seed_init = (int16_t) ( (int32_t) seed_init * 8 );
     545             :             }
     546      458032 :             if ( concat_out[j] < 0 )
     547             :             {
     548       66269 :                 seed_init = (int16_t) ( (int32_t) seed_init + 3 );
     549             :             }
     550             :         }
     551             : 
     552        3453 :         hGSCEnc->seed_tcx = seed_init;
     553             :     }
     554             : 
     555       22806 :     if ( st->core_brate == ACELP_8k00 && st->bwidth != NB )
     556             :     {
     557          44 :         bitallocation_exc[0] = 0;
     558          44 :         bitallocation_exc[1] = 0;
     559             : 
     560          44 :         if ( exc_diffQ[L_FRAME8k - 2] != 0 )
     561             :         {
     562           1 :             bitallocation_exc[0] = 1;
     563             :         }
     564             : 
     565          44 :         if ( exc_diffQ[L_FRAME8k - 1] != 0 )
     566             :         {
     567           5 :             bitallocation_exc[1] = 1;
     568             :         }
     569             :     }
     570             : 
     571             :     /*--------------------------------------------------------------------------------------*
     572             :      * Skip adaptive (pitch) contribution frequency band (no noise added over the adaptive (pitch) contribution)
     573             :      * Find x pulses between 1.6-3.2kHz to code in the spectrum of the residual signal
     574             :      * Gain is based on the inter-correlation gain between the pulses found and residual signal
     575             :      *--------------------------------------------------------------------------------------*/
     576             : 
     577       22806 :     if ( st->GSC_IVAS_mode >= 1 && st->GSC_noisy_speech == 1 )
     578             :     {
     579       21616 :         for ( i = 64; i < st->L_frame; i++ )
     580             :         {
     581       21504 :             exc_diffQ[i] *= max_eq;
     582             :         }
     583             :     }
     584       22694 :     else if ( st->core_brate < ACELP_7k20 && st->GSC_noisy_speech == 1 && st->coder_type <= UNVOICED )
     585             :     {
     586     1927243 :         for ( i = 0; i < L_FRAME; i++ )
     587             :         {
     588     1919744 :             exc_diffQ[i] *= max_eq;
     589             :         }
     590             :     }
     591             :     else
     592             :     {
     593       15195 :         freq_dnw_scaling( hGSCEnc->cor_strong_limit, st->coder_type, hGSCEnc->noise_lev, st->core_brate, exc_diffQ, st->L_frame );
     594             :     }
     595             : 
     596             :     /*--------------------------------------------------------------------------------------*
     597             :      * Estimate noise level
     598             :      *--------------------------------------------------------------------------------------*/
     599             : 
     600       22806 :     highband_exc_dct_in( st->core_brate, mfreq_bindiv_loc, last_bin, Diff_len, hGSCEnc->noise_lev, tmp_band, exc_diffQ, &hGSCEnc->seed_tcx, Ener_per_bd_iQ, nb_subfr, exc_dct_in, st->last_coder_type, bitallocation_band, lsf_new, hGSCEnc->last_exc_dct_in, &hGSCEnc->last_ener, hGSCEnc->last_bitallocation_band, bitallocation_exc, 0, st->coder_type, st->bwidth, exc_wo_nf, st->GSC_noisy_speech, NULL, st->L_frame, st->element_mode, st->GSC_IVAS_mode );
     601             : 
     602       22806 :     exc_dct_in[0] = 0;
     603             : 
     604       22806 :     return;
     605             : }
     606             : 
     607             : 
     608             : /*---------------------------------------------------------------------*
     609             :  * edyn()
     610             :  *
     611             :  * Calculate energy dynamics in a vector (ratio of energy maximum to energy mean)
     612             :  *---------------------------------------------------------------------*/
     613             : 
     614             : /*! r: ratio of max to mean */
     615      121960 : static float edyn(
     616             :     const float *vec,  /* i  : input vector            */
     617             :     const int16_t lvec /* i  : length of input vector  */
     618             : )
     619             : {
     620             :     int16_t j;
     621             :     float temp, ener_max, ener_mean, dyn;
     622             : 
     623      121960 :     ener_mean = 1.0f;
     624      121960 :     ener_max = 1.0f;
     625             : 
     626     2073320 :     for ( j = 0; j < lvec; j++ )
     627             :     {
     628     1951360 :         temp = vec[j] * vec[j];
     629             : 
     630     1951360 :         if ( temp > ener_max )
     631             :         {
     632      421746 :             ener_max = temp;
     633             :         }
     634     1951360 :         ener_mean += temp;
     635             :     }
     636      121960 :     ener_mean /= lvec;
     637      121960 :     dyn = 10.0f * ( ener_max / ener_mean );
     638             : 
     639      121960 :     return dyn;
     640             : }
     641             : 
     642             : 
     643             : /*-------------------------------------------------------------------*
     644             :  * GSC_enc_init()
     645             :  *
     646             :  * Initialize GSC encoder state structure
     647             :  *-------------------------------------------------------------------*/
     648             : 
     649        3897 : void GSC_enc_init(
     650             :     GSC_ENC_HANDLE hGSCEnc /* i/o: GSC data handle  */
     651             : )
     652             : {
     653             :     /* AC mode */
     654        3897 :     hGSCEnc->seed_tcx = 15687;
     655        3897 :     hGSCEnc->cor_strong_limit = 1;
     656        3897 :     set_f( hGSCEnc->last_exc_dct_in, 0, L_FRAME16k );
     657        3897 :     hGSCEnc->last_ener = 0.0f;
     658        3897 :     set_s( hGSCEnc->last_bitallocation_band, 0, 6 );
     659        3897 :     hGSCEnc->mem_last_pit_band = BAND1k2 + 1;
     660             : 
     661        3897 :     hGSCEnc->lt_gpitch = 0.0f;
     662        3897 :     hGSCEnc->pit_exc_hangover = 0;
     663             : 
     664             :     /* GSC - pitch excitation parameters */
     665        3897 :     hGSCEnc->mem_w0_tmp = 0.0f;
     666        3897 :     set_f( hGSCEnc->mem_syn_tmp, 0.0f, M );
     667             : 
     668        3897 :     hGSCEnc->Last_frame_ener = (float) MAX_32;
     669        3897 :     hGSCEnc->mid_dyn = 40.0f;
     670        3897 :     hGSCEnc->noise_lev = NOISE_LEVEL_SP0;
     671        3897 :     hGSCEnc->past_dyn_dec = 0;
     672             : 
     673        3897 :     return;
     674             : }

Generated by: LCOV version 1.14