From c498803c9d1469a7b798d069bf86306baa6f0767 Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 3 Dec 2024 14:09:53 +0100 Subject: [PATCH 1/2] Added 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 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/lib_com/options.h b/lib_com/options.h index ca759dc86c..2d349eb5d4 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -174,6 +174,7 @@ #define NONBE_FIX_1196_TD_HEADTRACKING_INTERPOLATION /* Ericsson: Issue 1196, Always apply filter interpolation for each subframe */ #define NONBE_1220_OMASA_JBM_BRATE_SW_FLUSH /* VA: issue 1220: fix bug in renderer flush in OMASA 1ISM JBM bitrate switching */ #define NONBE_1229_FIX_ISM1_DPID /* Eri: issue 1229: fix bug causing ISM 1 to use default -dpid instead of the specified one */ +#define NONBE_1233_HQ_CLASSIFIER_DIV_BY_ZERO /* Eri: issue 1233: Address possible division by zero in hf_spectrum_sparseness() */ /* ##################### End NON-BE switches ########################### */ diff --git a/lib_enc/hq_classifier_enc.c b/lib_enc/hq_classifier_enc.c index a41942d658..33fbedea9b 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 ( A == 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; -- GitLab From 52c98dbdd719b827f3fcbc1424c3c9e1fc87c52b Mon Sep 17 00:00:00 2001 From: Erik Norvell Date: Tue, 3 Dec 2024 14:20:21 +0100 Subject: [PATCH 2/2] Fix A->Amax in if-statement in HQ classifier --- lib_enc/hq_classifier_enc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_enc/hq_classifier_enc.c b/lib_enc/hq_classifier_enc.c index 33fbedea9b..b0ef15969b 100644 --- a/lib_enc/hq_classifier_enc.c +++ b/lib_enc/hq_classifier_enc.c @@ -574,7 +574,7 @@ static int16_t hf_spectrum_sparseness( crest_mod = 0.0f; maximum( A, L_SPEC_HB, &Amax ); #ifdef NONBE_1233_HQ_CLASSIFIER_DIV_BY_ZERO - if ( A == 0 ) + if ( Amax == 0 ) { /* For all-zero input the crest is 1.0 */ crest = 1.0f; -- GitLab