From b284947c986e0f1efe624f1537a7d874ec2b7670 Mon Sep 17 00:00:00 2001 From: naghibza Date: Mon, 10 Nov 2025 15:00:13 +0100 Subject: [PATCH 1/3] Resolve overflow by swapping the order of addition and multiplication --- lib_com/cnst.h | 13 +++++++----- lib_com/options.h | 2 +- lib_enc/bw_detect_fx.c | 48 ++++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/lib_com/cnst.h b/lib_com/cnst.h index cf1a7820f..97dd19f18 100644 --- a/lib_com/cnst.h +++ b/lib_com/cnst.h @@ -2787,12 +2787,15 @@ enum #define Fs_2_16k 20480 /* lsf max value (Use in reorder_fx.c) */ #define LG10 24660 /* 10*log10(2) in Q13 */ -#define TEN_MULT_LOG10_2_IN_Q29 1616142464 /* (10 * log10(2)) = 3.0103 (in Q29) */ +#define TEN_MULT_LOG10_2_IN_Q29 1616142464 /* (10 * log10(2)) = 3.0103 (in Q29) */ #define LG10_s3_0 16440 /* 10*log10(2)/1.55 = 1.00343331 in Q14 */ -#define LOG2_10 27213 /* log base 2 of 10 in Q12 */ -#define LOG2_10_Q29 1783446566 /* log base 2 of 10 in Q12 */ -#define LOG10_2_Q31 646456993 /* inverse log base 10 of 2 in Q31 */ -#define MU_MA_FX 10923 /* original prediction factor for the AMR WB tables (Q15) */ +#define LOG2_10 27213 /* log base 2 of 10 in Q12 */ +#define LOG2_10_Q29 1783446566 /* log base 2 of 10 in Q12 */ +#define LOG10_2_Q31 646456993 /* inverse log base 10 of 2 in Q31 */ +#ifdef FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA +#define INV_LOG2_10 9864 /* 1/log2(10), Q15*/ +#endif +#define MU_MA_FX 10923 /* original prediction factor for the AMR WB tables (Q15) */ #define E_MIN_FXQ15 115 /* Q15*/ #define E_MIN_FXQ31 7516193 /* 0.0035d in Q31*/ diff --git a/lib_com/options.h b/lib_com/options.h index 10ea208a2..6597217be 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -103,7 +103,7 @@ #define FIX_2184_EVS_STEREO_DMX_CHANNEL_DISAPPEARING /* Orange: Fix for issue 2184 - to prevent one channel from becoming inaudible in the mono downmix output */ #define FIX_2148_OBJ_EDIT_ISSUE_WITH_OSBA /* Nokia: Add missing code to solve issue */ #define FIX_2200_ISAR_PLC_CRASH /* Dolby: Fix for ISAR PLC crash observed with newly added BASOP tests */ - +#define FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA /* FhG: Resolve overflow by swapping the order of addition and multiplication */ /* ################### End FIXES switches ########################### */ /* #################### Start BASOP porting switches ############################ */ diff --git a/lib_enc/bw_detect_fx.c b/lib_enc/bw_detect_fx.c index 49391f60f..2cbfb0bd8 100644 --- a/lib_enc/bw_detect_fx.c +++ b/lib_enc/bw_detect_fx.c @@ -55,7 +55,7 @@ void bw_detect_fx( Word16 spect[L_FRAME48k], spect_bin[BWD_N_BINS_MAX]; Word32 spect32[L_FRAME48k], in_win32[BWD_TOTAL_WIDTH]; Word16 e_tmp, f_tmp; - Word32 L_tmp, sum32; + Word32 L_tmp, sum32, L_tmp_q_shift; Word32 L_tmp1, L_tmp2, L_tmp3; Word16 scale; @@ -131,12 +131,17 @@ void bw_detect_fx( } #endif L_tmp = BASOP_Util_Log2( cldfb_bin[0] ); /*(log2(660423549*2^(-31))/64)*2^31*/ -#ifdef FIX_2141_ASSERT_IN_OMASA_BITRATE_SWITSCHING - L_tmp = L_add_sat( L_tmp, L_shl_sat( L_deposit_l( cldfb_bin_Exp[0] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ +#ifdef FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA + L_tmp = Mpy_32_16_1( L_tmp, INV_LOG2_10 ); // instead of adding it to L_tmp_q_shift and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + L_tmp_q_shift = L_shl_sat( L_deposit_l( cldfb_bin_Exp[0] ), 31 - LD_DATA_SCALE ); // This term should be added to L_temp to align it to Q25 + L_tmp_q_shift = Mpy_32_16_1( L_tmp_q_shift, INV_LOG2_10 ); // instead of adding it to L_tmp and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + cldfb_bin[0] = L_add( L_tmp, L_tmp_q_shift ); /* Q25 */ #else - L_tmp = L_add_sat( L_tmp, L_shl( L_deposit_l( cldfb_bin_Exp[0] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ -#endif + L_tmp = L_add_sat( L_tmp, L_shl( L_deposit_l( cldfb_bin_Exp[0] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ cldfb_bin[0] = Mpy_32_16_1( L_tmp, 9864 /*1.0f/3.3219280948873623478703194294894f Q15*/ ); +#endif move32(); /* 1/log2(10) */ /* Q25 */ /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands, mid band(14) counted twice */ @@ -165,12 +170,17 @@ void bw_detect_fx( move32(); } L_tmp = BASOP_Util_Log2( cldfb_bin[i] ); /*(log2(660423549*2^(-31))/64)*2^31*/ -#ifdef FIX_2141_ASSERT_IN_OMASA_BITRATE_SWITSCHING - L_tmp = L_add( L_tmp, L_shl_sat( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ +#ifdef FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA + L_tmp = Mpy_32_16_1( L_tmp, INV_LOG2_10 ); // instead of adding it to L_tmp_q_shift and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + L_tmp_q_shift = L_shl_sat( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ); // This term should be added to L_temp to align it to Q25 + L_tmp_q_shift = Mpy_32_16_1( L_tmp_q_shift, INV_LOG2_10 ); // instead of adding it to L_tmp and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + cldfb_bin[i] = L_add( L_tmp, L_tmp_q_shift ); /* Q25 */ #else L_tmp = L_add( L_tmp, L_shl( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ -#endif cldfb_bin[i] = Mpy_32_16_1( L_tmp, 9864 /*1.0f/3.3219280948873623478703194294894f Q15*/ ); +#endif move32(); /* 1/log2(10) */ /* Q25 */ } } @@ -208,12 +218,17 @@ void bw_detect_fx( move32(); } L_tmp = BASOP_Util_Log2( cldfb_bin[i] ); /*(log2(660423549*2^(-31))/64)*2^31*/ -#ifdef FIX_2141_ASSERT_IN_OMASA_BITRATE_SWITSCHING - L_tmp = L_add( L_tmp, L_shl_sat( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ +#ifdef FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA + L_tmp = Mpy_32_16_1( L_tmp, INV_LOG2_10 ); // instead of adding it to L_tmp_q_shift and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + L_tmp_q_shift = L_shl_sat( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ); // This term should be added to L_temp to align it to Q25 + L_tmp_q_shift = Mpy_32_16_1( L_tmp_q_shift, INV_LOG2_10 ); // instead of adding it to L_tmp and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + cldfb_bin[i] = L_add( L_tmp, L_tmp_q_shift ); /* Q25 */ #else L_tmp = L_add( L_tmp, L_shl( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ -#endif cldfb_bin[i] = Mpy_32_16_1( L_tmp, 9864 /*1.0f/3.3219280948873623478703194294894f Q15*/ ); +#endif move32(); /* 1/log2(10) */ /* Q25 */ } } @@ -245,12 +260,17 @@ void bw_detect_fx( move32(); } L_tmp = BASOP_Util_Log2( cldfb_bin[i] ); /*(log2(660423549*2^(-31))/64)*2^31*/ -#ifdef FIX_2141_ASSERT_IN_OMASA_BITRATE_SWITSCHING - L_tmp = L_add( L_tmp, L_shl_sat( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ +#ifdef FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA + L_tmp = Mpy_32_16_1( L_tmp, INV_LOG2_10 ); // instead of adding it to L_tmp_q_shift and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + L_tmp_q_shift = L_shl_sat( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ); // This term should be added to L_temp to align it to Q25 + L_tmp_q_shift = Mpy_32_16_1( L_tmp_q_shift, INV_LOG2_10 ); // instead of adding it to L_tmp and then multiplying by INV_LOG2_10 ( to avoid saturation ). + + cldfb_bin[i] = L_add( L_tmp, L_tmp_q_shift ); /* Q25 */ #else L_tmp = L_add( L_tmp, L_shl( L_deposit_l( cldfb_bin_Exp[i] ), 31 - LD_DATA_SCALE ) ); /* Q25 */ -#endif cldfb_bin[i] = Mpy_32_16_1( L_tmp, 9864 /*1.0f/3.3219280948873623478703194294894f Q15*/ ); +#endif move32(); /* 1/log2(10) */ /* Q25 */ } } -- GitLab From 61fab6e9c7fda6a8b263def7fdc43856a791215e Mon Sep 17 00:00:00 2001 From: naghibza Date: Mon, 10 Nov 2025 15:17:57 +0100 Subject: [PATCH 2/3] Add MR switch. --- lib_enc/bw_detect_fx.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib_enc/bw_detect_fx.c b/lib_enc/bw_detect_fx.c index 2cbfb0bd8..08ad07149 100644 --- a/lib_enc/bw_detect_fx.c +++ b/lib_enc/bw_detect_fx.c @@ -55,7 +55,11 @@ void bw_detect_fx( Word16 spect[L_FRAME48k], spect_bin[BWD_N_BINS_MAX]; Word32 spect32[L_FRAME48k], in_win32[BWD_TOTAL_WIDTH]; Word16 e_tmp, f_tmp; +#ifdef FIX_2210_ASSERT_IN_BW_DETEC_FX_FOR_OMASA Word32 L_tmp, sum32, L_tmp_q_shift; +#else + Word32 L_tmp, sum32; +#endif Word32 L_tmp1, L_tmp2, L_tmp3; Word16 scale; -- GitLab From 33fcfb9d760c49c9f791df90676e10f4225b5f4c Mon Sep 17 00:00:00 2001 From: Markus Multrus Date: Mon, 10 Nov 2025 20:59:36 +0100 Subject: [PATCH 3/3] add disabled FIX_I4_OL_PITCH --- lib_com/options.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib_com/options.h b/lib_com/options.h index 9865b30c6..da5b38dfb 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -73,6 +73,7 @@ #endif /* ################### Start FIXES switches ########################### */ +/*#define FIX_I4_OL_PITCH*/ /* fix open-loop pitch used for EVS core switching */ #define REMOVE_BASOP_Util_Divide3232_Scale_cadence /* remove this division variant */ #define FIX_1990_SANITIZER_IN_REVERB_LOAD /* Nokia: Fix issue part of issue 1990 by introducing missing free of structure - keep until #2059 is addressed */ #define FIX_1999_TEMPORARY_DISABLE_DIST_ATT_CHECK /* Eri: Issue 1999: Range check on float values of distance attenuation, while the float values are not propagated to this function. The test is not correct, but configurable distance attenuation is not used in Characterization.*/ -- GitLab