From 0868aa80a823a00f2ed2c565acc54c4b5e25ebe4 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 3 Dec 2024 14:27:31 +0100 Subject: [PATCH 1/2] Add NONBE_1233_HQ_CLASSIFIER_DIV_BY_ZERO to address potential division by zero in hq_classifier --- lib_com/options.h | 1 + lib_enc/hq_classifier_enc.c | 55 +++++++++++++++++++++++++++++++++++++ lib_enc/swb_tbe_enc.c | 2 ++ 3 files changed, 58 insertions(+) diff --git a/lib_com/options.h b/lib_com/options.h index 680c58215..3d991c6c4 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -170,6 +170,7 @@ #define NONBE_FIX_1205_TD_STEREO_MOD_CT /* VA: fix mismatch of coder_type (mod_ct) btw. TD stereo encoder and decoder */ #define NONBE_FIX_1213_SBA_DET_MAT_INV_3BY3 /*Dolby: issue 1213: fix for inverse of det < 0*/ #define NONBE_FIX_1204_MDCT_STEREO_NOISE_EST_SCALING /* FhG: fixes for decoder-side noise level estimation in MDCT-Stereo to prevent noise bursts in stereo switching */ +#define NONBE_1233_HQ_CLASSIFIER_DIV_BY_ZERO /* Eri: issue 1233: Address possible division by zero in hf_spectrum_sparseness() */ /* #################### End FIXES switches ############################ */ diff --git a/lib_enc/hq_classifier_enc.c b/lib_enc/hq_classifier_enc.c index a8d798e65..ae30c473e 100644 --- a/lib_enc/hq_classifier_enc.c +++ b/lib_enc/hq_classifier_enc.c @@ -573,6 +573,60 @@ static int16_t hf_spectrum_sparseness( inv_rms = 0.0f; crest_mod = 0.0f; maximum( A, L_SPEC_HB, &Amax ); +#ifdef NONBE_1233_HQ_CLASSIFIER_DIV_BY_ZERO + if ( Amax == 0 ) + { + /* For all-zero input the crest is 1.0 */ + crest = 1.0f; + crest_mod = 1.0f; + low_count = 0; + } + else + { + thr = Amax * PEAK_THRESHOLD; + movmean = 0.0f; /* avoid uninitialized warning */ + p_num = &inv_tbl[HALF_WIN_LENGTH + 1]; /* Table for division 1./(11:21) */ + for ( i = 0; i < L_SPEC_HB; i++ ) + { + inv_rms += A[i] * A[i]; + if ( A[i] < thr ) + { + low_count += 1; + } + if ( i <= HALF_WIN_LENGTH ) + { + if ( i == 0 ) + { + movmean = sum_f( &A[0], i + HALF_WIN_LENGTH + 1 ) * ( *p_num ); + } + else + { + p_num++; + movmean = movmean + ( A[i + HALF_WIN_LENGTH] - movmean ) * ( *p_num ); + } + } + else + { + if ( L_SPEC_HB <= i + HALF_WIN_LENGTH ) + { + p_num--; + movmean = movmean + ( movmean - A[i - HALF_WIN_LENGTH - 1] ) * ( *p_num ); + } + else + { + movmean = movmean + ( A[i + HALF_WIN_LENGTH] - A[i - HALF_WIN_LENGTH - 1] ) * ( *p_num ); + } + } + if ( crest_mod < movmean ) + { + crest_mod = movmean; + } + } + inv_rms = 1.0f / (float) sqrtf( inv_rms / L_SPEC_HB ); + crest = Amax * inv_rms; + crest_mod = crest_mod * inv_rms; + } +#else thr = Amax * PEAK_THRESHOLD; movmean = 0.0f; /* avoid uninitialized warning */ p_num = &inv_tbl[HALF_WIN_LENGTH + 1]; /* Table for division 1./(11:21) */ @@ -615,6 +669,7 @@ static int16_t hf_spectrum_sparseness( inv_rms = 1.0f / (float) sqrtf( inv_rms / L_SPEC_HB ); crest = Amax * inv_rms; crest_mod = crest_mod * inv_rms; +#endif *crest_lp = HQ_CREST_FAC_SM * ( *crest_lp ) + ( 1.0f - HQ_CREST_FAC_SM ) * crest; *crest_mod_lp = HQ_CREST_FAC_SM * ( *crest_mod_lp ) + ( 1.0f - HQ_CREST_FAC_SM ) * crest_mod; diff --git a/lib_enc/swb_tbe_enc.c b/lib_enc/swb_tbe_enc.c index 1a0dbbf7b..e9130d882 100644 --- a/lib_enc/swb_tbe_enc.c +++ b/lib_enc/swb_tbe_enc.c @@ -2220,6 +2220,8 @@ static void Quant_BWE_LSF( } } + + return; } -- GitLab From 1649b9fc28f2d5762f287a4da84adabb947010b9 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 3 Dec 2024 14:34:27 +0100 Subject: [PATCH 2/2] Remove whitespace added by mistake. --- lib_enc/swb_tbe_enc.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib_enc/swb_tbe_enc.c b/lib_enc/swb_tbe_enc.c index e9130d882..1a0dbbf7b 100644 --- a/lib_enc/swb_tbe_enc.c +++ b/lib_enc/swb_tbe_enc.c @@ -2220,8 +2220,6 @@ static void Quant_BWE_LSF( } } - - return; } -- GitLab