From e10e110e05dd3c9bde7e9f294f40a18d68c15090 Mon Sep 17 00:00:00 2001 From: malenov Date: Tue, 9 Sep 2025 17:56:56 +0200 Subject: [PATCH 1/4] prevent saturation when up-scaling lpc_shb_fx[] --- lib_com/options.h | 1 + lib_enc/swb_tbe_enc_fx.c | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib_com/options.h b/lib_com/options.h index b4d00a799..4f1da4861 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -107,6 +107,7 @@ #define NONBE_FIX_1143_MASA_BRSW /* Nok: Fix for issue 1143: MSAN use of uninitialized value in masa decoding to binaural with dtx bitrate switching and 5 % FER */ #define NONBE_FIX_1220_OMASA_JBM_EXT_USAN /* Nokia: fix issue 1220 OMASA EXT JBM USAN, also fix similar cases of free to avoid future problems */ #define NONBE_FIX_1376_MDCT_CONCEALMENT /* FhG: fix concealment artifact in MDCT Stereo with DTX, in case transition frame gets lost */ +#define FIX_ISSUE_2004_LPC_SHB_SAT /* Dolby: Issue 2004: prevent saturation of the LPC SHB filter */ /* #################### End BASOP porting switches ############################ */ diff --git a/lib_enc/swb_tbe_enc_fx.c b/lib_enc/swb_tbe_enc_fx.c index 490d80aae..e6fdfa0ba 100644 --- a/lib_enc/swb_tbe_enc_fx.c +++ b/lib_enc/swb_tbe_enc_fx.c @@ -3161,7 +3161,25 @@ void swb_tbe_enc_ivas_fx( enerG = Enr_1_Az_fx( lpc_shb1, 2 * L_SUBFR ); /* Q3 */ /* if the LP gain is greater than a threshold, avoid saturation */ +#ifdef FIX_ISSUE_2004_LPC_SHB_SAT + Word16 flag_sat = 0; + Word16 lpc_shb_fx0_req_shift = sub( norm_s( lpc_shb_fx[0] ), 2); + IF( GT_16( lpc_shb_fx0_req_shift, 0 ) ) + { + FOR( i = 1; i <= LPC_SHB_ORDER; i++ ) + { + IF( LT_16( norm_s( lpc_shb_fx[i] ), lpc_shb_fx0_req_shift ) ) + { + flag_sat = 1; + break; + } + } + } + + IF( GT_16( enerG, 256 /* 32.0 in Q3 */ ) || flag_sat ) +#else IF( GT_16( enerG, 256 /*32 Q3*/ ) ) +#endif { set16_fx( lpc_shb_fx, 0, LPC_SHB_ORDER + 1 ); E_LPC_lev_dur( R_h, R_l, lpc_shb_fx, LepsP, 2, NULL ); /* LPC in Q14 */ @@ -3169,7 +3187,8 @@ void swb_tbe_enc_ivas_fx( } /* this is needed as the E_LPC_lev_dur function outputs lpc in Q14 */ - scale_sig( lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); + Word16 tmp_shift = sub( norm_s( lpc_shb_fx[0] ), 2 ); + scale_sig( lpc_shb_fx, LPC_SHB_ORDER + 1, tmp_shift ); /* Expand bandwidth of the LP coeffs */ test(); -- GitLab From 9c58b7548246c7cf4627c754e8cc27a3bdf0bc75 Mon Sep 17 00:00:00 2001 From: Vladimir Malenovsky Date: Tue, 9 Sep 2025 18:04:15 +0200 Subject: [PATCH 2/4] clang format --- lib_enc/swb_tbe_enc_fx.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/lib_enc/swb_tbe_enc_fx.c b/lib_enc/swb_tbe_enc_fx.c index e6fdfa0ba..836befca0 100644 --- a/lib_enc/swb_tbe_enc_fx.c +++ b/lib_enc/swb_tbe_enc_fx.c @@ -3163,7 +3163,7 @@ void swb_tbe_enc_ivas_fx( /* if the LP gain is greater than a threshold, avoid saturation */ #ifdef FIX_ISSUE_2004_LPC_SHB_SAT Word16 flag_sat = 0; - Word16 lpc_shb_fx0_req_shift = sub( norm_s( lpc_shb_fx[0] ), 2); + Word16 lpc_shb_fx0_req_shift = sub( norm_s( lpc_shb_fx[0] ), 2 ); IF( GT_16( lpc_shb_fx0_req_shift, 0 ) ) { FOR( i = 1; i <= LPC_SHB_ORDER; i++ ) @@ -3922,7 +3922,7 @@ void swb_tbe_enc_ivas_fx( #ifdef ISSUE_1867_replace_overflow_libenc shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */ #else - shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */ + shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */ #endif move16(); } @@ -3944,7 +3944,7 @@ void swb_tbe_enc_ivas_fx( #ifdef ISSUE_1867_replace_overflow_libenc L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), tmp ); /* Q31-exp */ #else - L_tmp1 = Mult_32_16( L_shl_o( 1, sub( 31, exp ), &Overflow ), tmp ); /* Q31-exp */ + L_tmp1 = Mult_32_16( L_shl_o( 1, sub( 31, exp ), &Overflow ), tmp ); /* Q31-exp */ #endif tmp = sub( 32767 /*1.0f Q15*/, tmp ); L_tmp = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 ); @@ -3952,7 +3952,7 @@ void swb_tbe_enc_ivas_fx( #ifdef ISSUE_1867_replace_overflow_libenc shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */ #else - shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */ + shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */ #endif move16(); } @@ -3965,7 +3965,7 @@ void swb_tbe_enc_ivas_fx( #ifdef ISSUE_1867_replace_overflow_libenc L_tmp1 = Mult_32_16( L_shl_sat( 1, sub( 31, exp ) ), tmp ); /* Q31-exp */ #else - L_tmp1 = Mult_32_16( L_shl_o( 1, sub( 31, exp ), &Overflow ), tmp ); /* Q31-exp */ + L_tmp1 = Mult_32_16( L_shl_o( 1, sub( 31, exp ), &Overflow ), tmp ); /* Q31-exp */ #endif tmp = sub( 32767 /*1.0f Q15*/, tmp ); L_tmp = L_add( Mult_32_16( Lscale, tmp ), L_tmp1 ); @@ -3973,7 +3973,7 @@ void swb_tbe_enc_ivas_fx( #ifdef ISSUE_1867_replace_overflow_libenc shaped_shb_excitation_fx[i] = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /* Q_bwe_exc */ #else - shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */ + shaped_shb_excitation_fx[i] = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /* Q_bwe_exc */ #endif move16(); } @@ -4486,7 +4486,7 @@ void swb_tbe_enc_ivas_fx( #ifdef ISSUE_1867_replace_overflow_libenc tmp = round_fx_sat( L_shl_sat( L_tmp, 31 - ( 7 - exp ) ) ); /* Q15 */ #else - tmp = round_fx_o( L_shl_o( L_tmp, 31 - ( 7 - exp ), &Overflow ), &Overflow ); /* Q15 */ + tmp = round_fx_o( L_shl_o( L_tmp, 31 - ( 7 - exp ), &Overflow ), &Overflow ); /* Q15 */ #endif } tmp = s_min( tmp, 32767 /*1.0f Q15*/ ); @@ -4763,10 +4763,10 @@ static void EstimateSHBFrameGain_fx( sig = round_fx_sat( Mult_32_16( mod_syn[i], win_shb[i] ) ); /*Q_synSHB */ synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2*Q_synSHB */ #else - sig = mult_ro( oriSHB[i], win_shb[i], &Overflow ); /* Q_oriSHB */ - oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_orisHB*/ - sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[i] ), &Overflow ); /*Q_synSHB */ - synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_synSHB */ + sig = mult_ro( oriSHB[i], win_shb[i], &Overflow ); /* Q_oriSHB */ + oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_orisHB*/ + sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[i] ), &Overflow ); /*Q_synSHB */ + synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_synSHB */ #endif } @@ -4777,9 +4777,9 @@ static void EstimateSHBFrameGain_fx( sig = round_fx_sat( mod_syn[i] ); /* Q_oriSHB */ synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2*Q_oriSHB */ #else - oriNrg = L_mac0_o( oriNrg, oriSHB[i], oriSHB[i], &Overflow ); /* 2*Q_oriSHB */ - sig = round_fx_o( mod_syn[i], &Overflow ); /* Q_oriSHB */ - synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */ + oriNrg = L_mac0_o( oriNrg, oriSHB[i], oriSHB[i], &Overflow ); /* 2*Q_oriSHB */ + sig = round_fx_o( mod_syn[i], &Overflow ); /* Q_oriSHB */ + synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */ #endif } @@ -4792,8 +4792,8 @@ static void EstimateSHBFrameGain_fx( sig = round_fx_sat( Mult_32_16( mod_syn[i], win_shb[l_frame + l_shb_lahead - 1 - i] ) ); /* Q_oriSHB */ synNrg = L_mac0_sat( synNrg, sig, sig ); /* 2*Q_oriSHB */ #else - sig = mult_ro( oriSHB[i], win_shb[l_frame + l_shb_lahead - 1 - i], &Overflow ); /* Q_oriSHB */ - oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */ + sig = mult_ro( oriSHB[i], win_shb[l_frame + l_shb_lahead - 1 - i], &Overflow ); /* Q_oriSHB */ + oriNrg = L_mac0_o( oriNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */ sig = round_fx_o( Mult_32_16( mod_syn[i], win_shb[l_frame + l_shb_lahead - 1 - i] ), &Overflow ); /* Q_oriSHB */ synNrg = L_mac0_o( synNrg, sig, sig, &Overflow ); /* 2*Q_oriSHB */ -- GitLab From 235290bbf3cf4d34fb3c65d4b8a780f380445728 Mon Sep 17 00:00:00 2001 From: Markus Multrus Date: Tue, 9 Sep 2025 20:10:54 +0200 Subject: [PATCH 3/4] editorial - move switch to right section --- lib_com/options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_com/options.h b/lib_com/options.h index 4f1da4861..33158fcab 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -93,6 +93,7 @@ #define FIX_1996_MASKING_NOISE /* Dlb: Heavy precision loss in ola buffers causing discontinuity*/ #define FIX1998_APA_EXEC_SCALING /* FhG: fix scaling of apa_exec_ivas_fx(); avoid continuously worse scaling with previous data */ #define FIX2007_BASSPSFILTER_OVERFLOW /* FhG: use saturing operator, to avoid overflow in bass_psfilter_fx() */ +#define FIX_ISSUE_2004_LPC_SHB_SAT /* Dolby: Issue 2004: prevent saturation of the LPC SHB filter */ /* #################### Start BASOP porting switches ############################ */ @@ -107,7 +108,6 @@ #define NONBE_FIX_1143_MASA_BRSW /* Nok: Fix for issue 1143: MSAN use of uninitialized value in masa decoding to binaural with dtx bitrate switching and 5 % FER */ #define NONBE_FIX_1220_OMASA_JBM_EXT_USAN /* Nokia: fix issue 1220 OMASA EXT JBM USAN, also fix similar cases of free to avoid future problems */ #define NONBE_FIX_1376_MDCT_CONCEALMENT /* FhG: fix concealment artifact in MDCT Stereo with DTX, in case transition frame gets lost */ -#define FIX_ISSUE_2004_LPC_SHB_SAT /* Dolby: Issue 2004: prevent saturation of the LPC SHB filter */ /* #################### End BASOP porting switches ############################ */ -- GitLab From 6dfde5b6d987ccac3e7790db4421841bd42d350e Mon Sep 17 00:00:00 2001 From: "Malenovsky, Vladimir" Date: Wed, 10 Sep 2025 15:58:06 +0200 Subject: [PATCH 4/4] revert temp debugging modification --- lib_enc/swb_tbe_enc_fx.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib_enc/swb_tbe_enc_fx.c b/lib_enc/swb_tbe_enc_fx.c index 836befca0..e3f318127 100644 --- a/lib_enc/swb_tbe_enc_fx.c +++ b/lib_enc/swb_tbe_enc_fx.c @@ -3187,8 +3187,7 @@ void swb_tbe_enc_ivas_fx( } /* this is needed as the E_LPC_lev_dur function outputs lpc in Q14 */ - Word16 tmp_shift = sub( norm_s( lpc_shb_fx[0] ), 2 ); - scale_sig( lpc_shb_fx, LPC_SHB_ORDER + 1, tmp_shift ); + scale_sig( lpc_shb_fx, LPC_SHB_ORDER + 1, sub( norm_s( lpc_shb_fx[0] ), 2 ) ); /* Expand bandwidth of the LP coeffs */ test(); -- GitLab