Commit 02644adc authored by Sumeyra Demir Kanik's avatar Sumeyra Demir Kanik
Browse files

Merge branch 'ericsson/hq-envelope-stability-fix' into 'main'

[NonBE] Merge of Contribution 23 HQ Envelope Stability Fix

See merge request !378
parents df4af9ec 7c2d1ead
Loading
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -1682,6 +1682,15 @@ enum
#define HALF_D_STAB_TBL_FX                  ( (Word16) 422 )        /* Q13 0.1013138/2.0 */
#define NUM_ENV_STAB_PLC_STATES             2                       /* Number of states of markov model */

#ifdef ENV_STAB_FIX
#define ENV_STAB_EST1                       2.93f                   /* env_stab estimation coefficient 1 */
#define ENV_STAB_EST2                       (-2.20f)                /* env_stab estimation coefficient 2 */
#define ENV_STAB_EST3                       0.741f                  /* env_stab estimation coefficient 3 */
#define STAB_FAC_EST1                       1.093f                  /* stab_fac HQ estimation coefficient 1 */
#define STAB_FAC_EST2                       (-5.84e-05f)            /* stab_fac HQ estimation coefficient 2, including Q12 scaling */
#define STAB_FAC_EST3                       0.125f                  /* stab_fac HQ estimation coefficient 3 */
#endif

#define ATT_LIM_HANGOVER                    150                     /* Number of hangover frames for disabling stability dependent attenuation */
#define DELTA_TH                            5.0f                    /* Delta energy threshold for transient detection for envelope stability */
#define ENERGY_TH                           100.0f                  /* Energy threshold for transient detection */
+79 −8
Original line number Diff line number Diff line
@@ -66,7 +66,12 @@ float env_stability(
    const int16_t *ynrm,  /* i  : Norm vector for current frame          */
    const int16_t nb_sfm, /* i  : Number of sub-bands                    */
    int16_t *mem_norm,    /* i/o: Norm vector memory from past frame     */
#ifdef ENV_STAB_FIX
    int16_t *mem_env_delta,           /* i/o: Envelope stability memory for smoothing*/
    const int16_t core_switching_flag /* i  : Core switching flag        */
#else
    int16_t *mem_env_delta /* i/o: Envelope stability memory for smoothing*/
#endif
)
{
    Word16 env_delta;
@@ -82,6 +87,22 @@ float env_stability(
    Flag Overflow;
#endif /* BASOP_NOGLOB */

#ifdef ENV_STAB_FIX
    if ( core_switching_flag )
    {
        for ( i = 0; i < nb_sfm; i++ )
        {
            mem_norm[i] = ynrm[i];
        }
#ifdef BASOP_NOGLOB
        Overflow = 0;
        env_delta = shl_o( *mem_env_delta, 1, &Overflow );
#else
        env_delta = shl_o( *mem_env_delta, 1 );
#endif
    }
    else
    {
        /* Calculate envelope stability parameter */
        L_env_delta = L_deposit_l( 0 );
        for ( i = 0; i < nb_sfm; i++ )
@@ -128,7 +149,57 @@ float env_stability(
#else                                              /* BASOP_NOGLOB */
        env_delta = round_fx_o( L_shl_o( L_tmp, 1, &Overflow ), &Overflow );              /* Q13 */
#endif                                             /* BASOP_NOGLOB */
    }
#else

    /* Calculate envelope stability parameter */
    L_env_delta = L_deposit_l( 0 );
    for ( i = 0; i < nb_sfm; i++ )
    {
        tmp = sub( mem_norm[i], ynrm[i] );
        L_env_delta = L_mac0( L_env_delta, tmp, tmp );
        mem_norm[i] = ynrm[i];
    }

#ifdef DEBUGGING
    assert( nb_sfm == 27 || nb_sfm == 26 );
#endif
    inv_nb_sfm = 19418; /* Q19 */
    if ( nb_sfm == 26 )
    {
        inv_nb_sfm = 20165; /* Q19 */
    }
    exp = norm_l( L_env_delta );
    L_env_delta = Mult_32_16( L_shl( L_env_delta, exp ), inv_nb_sfm ); /* 0+exp+19-15 */

    L_tmp = Sqrt_l( L_env_delta, &exp2 ); /* exp+4+31+exp2 */

    exp = add( 35, add( exp, exp2 ) );
    if ( sub( s_and( exp, 1 ), 1 ) == 0 )
    {
        L_tmp = Mult_32_16( L_tmp, 23170 ); /* 1/sqrt(2) in Q15 */
    }
    exp = shr( exp, 1 );

#ifndef BASOP_NOGLOB
    env_delta = round_fx( L_shl( L_tmp, sub( 26, exp ) ) ); /* Q10 */
    L_tmp = L_mult0( 26214, env_delta );                    /* 26214 is 0.1 in Q18. Q28 */
    L_tmp = L_mac( L_tmp, 29491, *mem_env_delta );          /* 29491 is 0.9 in Q15. Q28 */
    *mem_env_delta = round_fx( L_tmp );                     /* Q12 */
#else  /* BASOP_NOGLOB */
    env_delta = round_fx_o( L_shl_o( L_tmp, sub( 26, exp ), &Overflow ), &Overflow ); /* Q10 */
    L_tmp = L_mult0( 26214, env_delta );                                              /* 26214 is 0.1 in Q18. Q28 */
    L_tmp = L_mac_o( L_tmp, 29491, *mem_env_delta, &Overflow );                       /* 29491 is 0.9 in Q15. Q28 */
    *mem_env_delta = round_fx_o( L_tmp, &Overflow );                                  /* Q12 */
#endif /* BASOP_NOGLOB */
    Overflow = 0;
#ifndef BASOP_NOGLOB
    env_delta = round_fx( L_shl( L_tmp, 1 ) ); /* Q13 */
#else /* BASOP_NOGLOB */
    env_delta = round_fx_o( L_shl_o( L_tmp, 1, &Overflow ), &Overflow );              /* Q13 */
#endif
                                               /* BASOP_NOGLOB */
#endif
    if ( Overflow != 0 ) /* Saturated due to the above up-shifting operation. */
    {
        env_stab = stab_trans_fx[L_STAB_TBL - 1];             /* The highest quantized index. */

lib_com/options.h

100755 → 100644
+1 −1
Original line number Diff line number Diff line
@@ -155,7 +155,7 @@
#define FIX_245_RANGE_CODER_VOIP_MSAN                   /* Issue 245: fix use-of-uninitialized-value in range coder in VoIP mode */
#define FIX_272_COV                                     /* Issue 272: Cleanup for code coverage related to calls to ivas_binaural_cldfb() */
#define FIX_235                                         /* Issue 235: Deallocation of HR filter memory separately for lib_rend (ROM) and lib_util (from file) */

#define ENV_STAB_FIX                                    /* Contribution 23: HQ envelope stability memory fix */

/* ################## End DEVELOPMENT switches ######################### */
/* clang-format on */
+15 −4
Original line number Diff line number Diff line
@@ -5414,7 +5414,13 @@ void hq_hr_dec(
    int16_t *ynrm,          /* o  : norm quantization index vector  */
    int16_t *is_transient,  /* o  : transient flag                  */
    int16_t *hqswb_clas,    /* o  : HQ SWB class                    */
#ifdef ENV_STAB_FIX
    float *SWB_fenv,                  /* o  : SWB frequency envelopes         */
    const int16_t core_switching_flag /* i  : Core switching flag             */
#else
    float *SWB_fenv        /* o  : SWB frequency envelopes              */
#endif

);

void hdecnrm_context(
@@ -5841,7 +5847,12 @@ float env_stability(
    const int16_t *ynrm,  /* i  : Norm vector for current frame          */
    const int16_t nb_sfm, /* i  : Number of sub-bands                    */
    int16_t *mem_norm,    /* i/o: Norm vector memory from past frame     */
#ifdef ENV_STAB_FIX
    int16_t *mem_env_delta,           /* i/o: Envelope stability memory for smoothing*/
    const int16_t core_switching_flag /* i  : Core switching flag             */
#else
    int16_t *mem_env_delta /* i/o: Envelope stability memory for smoothing*/
#endif
);

/*! r: New speech/music state */
+33 −0
Original line number Diff line number Diff line
@@ -70,6 +70,9 @@ ivas_error core_switching_pre_dec(
{
    int16_t i, oldLenClasBuff, newLenClasBuff;
    ivas_error error;
#ifdef ENV_STAB_FIX
    float tmp;
#endif

    error = IVAS_ERR_OK;

@@ -401,7 +404,11 @@ ivas_error core_switching_pre_dec(
        }
    }

#ifdef ENV_STAB_FIX
    if ( st->core == HQ_CORE && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE || ( ( st->element_mode != EVS_MONO ) && ( st->last_core != HQ_CORE ) ) ) )
#else
    if ( st->core == HQ_CORE && ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE ) )
#endif
    {
        set_f( st->hHQ_core->prev_env, 0, SFM_N_WB );
        set_f( st->hHQ_core->prev_normq, 0, SFM_N_WB );
@@ -418,8 +425,34 @@ ivas_error core_switching_pre_dec(
            set_f( st->hHQ_core->prev_coeff_out, 0, L_HQ_WB_BWE );
        }

#ifdef ENV_STAB_FIX
        if ( st->element_mode != EVS_MONO )
        {
            /* Estimate mem_env_delta to reinit env_stab */
            tmp = max( 0, ENV_STAB_EST1 + ( ENV_STAB_EST2 * st->stab_fac_smooth_lt ) + ( ENV_STAB_EST3 * st->log_energy_diff_lt ) );
            st->hHQ_core->mem_env_delta = (int16_t) min( MAX16B, (int32_t) ( tmp * ( 1 << 12 ) ) ); /* Convert to Q12 and handle saturation */

            if ( st->last_core != TCX_20_CORE && st->last_core != TCX_10_CORE )
            {
                set_f( st->hHQ_core->old_out, 0, output_frame );
                set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
            }

            st->hHQ_core->no_att_hangover = 0;
            st->hHQ_core->energy_lt = 300.0f;

            set_s( st->hHQ_core->old_is_transient, 0, 3 );
            set_f( st->hHQ_core->prev_noise_level, 0.0f, 2 );
            st->hHQ_core->prev_R = 0;
            set_s( st->hHQ_core->mem_norm + 1, 39, SFM_N_ENV_STAB - 1 );
            st->hHQ_core->prev_hqswb_clas = HQ_NORMAL;
            st->hHQ_core->prev_ni_ratio = 0.5f;
            set_f( st->hHQ_core->prev_En_sb, 0.0f, NB_SWB_SUBBANDS );
        }
#else
        set_f( st->hHQ_core->old_out, 0, output_frame );
        set_f( st->hHQ_core->old_outLB, 0, L_FRAME16k );
#endif
    }

    /* handle switching cases where preecho_sb was not called in the last frame (memory not up to date) */
Loading