From 33b7e7d3819fb21a35867e70df194e6f85761c3f Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Tue, 24 Mar 2026 10:38:01 +0100 Subject: [PATCH 01/10] added macro FIX_2489_HARMONIZE_FdCng_encodeSID, added harmonized FdCng_encodeSID_fx() --- lib_com/options.h | 1 + lib_enc/acelp_core_enc_fx.c | 8 + lib_enc/enc_acelp_tcx_main_fx.c | 4 + lib_enc/fd_cng_enc_fx.c | 370 +++++++++++++++++++++++++++++++- lib_enc/prot_fx_enc.h | 8 + 5 files changed, 390 insertions(+), 1 deletion(-) diff --git a/lib_com/options.h b/lib_com/options.h index eb5d8a66e..0ff4f7bcf 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -89,6 +89,7 @@ #define FIX_2455_HARMONIZE_generate_comfort_noise_enc /* FhG: harmonize generate_comfort_noise_enc and generate_comfort_noise_enc_ivas */ #define FIX_2455_HARMONIZE_configureFdCngEnc /* FhG: harmonize generate_comfort_noise_enc and generate_comfort_noise_enc_ivas */ #define FIX_2463_EVS_BWE_LSF /* VA: basop issue 2463: harmonize calling of Quant_BWE_LSF_fx() */ +#define FIX_2489_HARMONIZE_FdCng_encodeSID /* FhG: harmonize FdCng_encodeSID_fx() and FdCng_encodeSID_ivas_fx() */ /* #################### End BE switches ################################## */ diff --git a/lib_enc/acelp_core_enc_fx.c b/lib_enc/acelp_core_enc_fx.c index 904577880..154ef9ab6 100644 --- a/lib_enc/acelp_core_enc_fx.c +++ b/lib_enc/acelp_core_enc_fx.c @@ -358,7 +358,11 @@ ivas_error acelp_core_enc_fx( test(); IF( EQ_32( st->core_brate, SID_2k40 ) && NE_16( st->element_mode, IVAS_CPE_MDCT ) ) { +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID + FdCng_encodeSID_fx( st ); +#else FdCng_encodeSID_fx( st->hFdCngEnc, st, st->preemph_fac ); +#endif st->hDtxEnc->last_CNG_L_frame = st->L_frame; move16(); } @@ -397,7 +401,11 @@ ivas_error acelp_core_enc_fx( test(); IF( EQ_32( st->core_brate, SID_2k40 ) && NE_16( st->element_mode, IVAS_CPE_MDCT ) ) { +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID + FdCng_encodeSID_fx( st ); +#else FdCng_encodeSID_ivas_fx( st ); +#endif st->hDtxEnc->last_CNG_L_frame = st->L_frame; move16(); } diff --git a/lib_enc/enc_acelp_tcx_main_fx.c b/lib_enc/enc_acelp_tcx_main_fx.c index 7814aadba..00d389b47 100644 --- a/lib_enc/enc_acelp_tcx_main_fx.c +++ b/lib_enc/enc_acelp_tcx_main_fx.c @@ -88,7 +88,11 @@ void enc_acelp_tcx_main_fx( /* Run SID Coder */ IF( st->core_brate == SID_2k40 ) { +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID + FdCng_encodeSID_fx( st ); +#else FdCng_encodeSID_fx( st->hFdCngEnc, st, st->preemph_fac ); +#endif } /* Generate Comfort Noise */ diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index 2dbb0ac90..e311a585d 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -1071,7 +1071,7 @@ static void msvq_encoder( return; } - +#ifndef FIX_2489_HARMONIZE_FdCng_encodeSID /* FdCng_encodeSID_fx @@ -1091,6 +1091,7 @@ static void msvq_encoder( Returns: void */ + void FdCng_encodeSID_fx( HANDLE_FD_CNG_ENC stenc, /* i/o: pointer to FD_CNG structure containing all buffers and variables */ Encoder_State *corest, @@ -1293,6 +1294,7 @@ void FdCng_encodeSID_fx( return; } +#endif /*FIX_2489_HARMONIZE_FdCng_encodeSID*/ #ifndef FIX_2455_HARMONIZE_generate_comfort_noise_enc void generate_comfort_noise_enc_fx( @@ -2483,6 +2485,371 @@ void perform_noise_estimation_enc_ivas_fx( * * Generate a bitstream out of the partition levels *-------------------------------------------------------------------*/ +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID +void FdCng_encodeSID_fx( + Encoder_State *corest /* i/o: encoder state structure */ +) +{ + Word16 i, index, N; + Word32 temp, gain_fx, e_fx; + HANDLE_FD_CNG_ENC hFdCngEnc = corest->hFdCngEnc; + HANDLE_FD_CNG_COM hFdCngCom = hFdCngEnc->hFdCngCom; + Word16 preemph_fac = corest->preemph_fac; // Q15 + move16(); + BSTR_ENC_HANDLE hBstr = corest->hBstr; + Word16 indices[32]; + Word32 v_fx[32]; + Word32 *E_fx; + Word16 v_e, E_Exp; + Word16 normFacN, normShiftN; + Word16 normFacGain, normShiftGain; + Word32 E_ExpLd64; + + IF( corest->element_mode > 0 ) + { + (void) normFacN; + (void) normShiftN; + (void) normFacGain; + (void) normShiftGain; + (void) E_ExpLd64; + } + ELSE + { + (void) v_e; + } + + /* Init */ + + E_fx = hFdCngEnc->msNoiseEst_fx; + N = hFdCngEnc->npartDec; + move16(); + E_Exp = hFdCngEnc->msNoiseEst_fx_exp; + move16(); + IF( corest->element_mode == 0 ) + { + E_ExpLd64 = L_shl( E_Exp, WORD32_BITS - 1 - LD_DATA_SCALE ); + + normFacN = getNormReciprocalWord16( N ); + normShiftN = BASOP_util_norm_s_bands2shift( N ); + + normFacGain = getNormReciprocalWord16( N_GAIN_MAX - N_GAIN_MIN ); + normShiftGain = BASOP_util_norm_s_bands2shift( N_GAIN_MAX - N_GAIN_MIN ); + } + ELSE + { + set_zero_fx( v_fx, FDCNG_VQ_MAX_LEN ); + } + + + /* Convert to LOG */ + e_fx = L_deposit_l( 0 ); + IF( corest->element_mode == 0 ) + { + /* e: Q14.23 format, v_fx: Q9.23 format */ + temp = Mpy_32_32_r( L_shl( 1, sub( 31, E_Exp ) ), 214748 ); /* 1e-4f, Q31-E_Exp */ + FOR( i = 0; i < N; i++ ) + { + /* assert( E_fx[i] != 0 ); */ + /* constant: 0.75257498916 = 10.0 * log10(2.0)/log10(10.0) * 0.25 */ + v_fx[i] = Mpy_32_16_1( L_add( BASOP_Util_Log2( L_add( E_fx[i], L_max( 1, temp ) ) ), E_ExpLd64 ), 24660 /*0.75257498916 Q15*/ ); + move32(); + e_fx = L_add( e_fx, L_shr( v_fx[i], normShiftN ) ); + } + e_fx = L_shl( Mpy_32_16_1( e_fx, shl( normFacN, sub( normShiftN, 1 ) ) ), 1 ); + } + ELSE + { + FOR( i = 0; i < N; i++ ) + { + IF( E_fx[i] == 0 ) + { + /* 10 * log(1e-4) = 10 * (-4) = -40 */ + v_fx[i] = -41943040; // -40.0 in Q20 + move32(); + } + ELSE + { + v_fx[i] = Mpy_32_32( 671088640 /*10 in Q26*/, BASOP_Util_Log10( E_fx[i], E_Exp ) ); // Q20 = 26+25-31 + move32(); + } + e_fx = L_add( e_fx, L_shr( v_fx[i], 1 ) ); // Q19, add one bit headroom + } + } + + /* Normalize MSVQ input */ + + gain_fx = 0; + move32(); + IF( corest->element_mode == 0 ) + { + /* gain: Q9.23 format */ + FOR( i = N_GAIN_MIN; i < N_GAIN_MAX; i++ ) + { + gain_fx = L_add( gain_fx, L_shr( v_fx[i], normShiftGain ) ); + } + gain_fx = L_shl( Mpy_32_16_1( gain_fx, shl( normFacGain, sub( normShiftGain, 1 ) ) ), 1 ); + } + ELSE + { + FOR( i = N_GAIN_MIN; i < N_GAIN_MAX; i++ ) + { + gain_fx = L_add( gain_fx, v_fx[i] ); // Q20 + } + + /*gain /= (float) ( N_GAIN_MAX - N_GAIN_MIN );*/ + gain_fx = Mpy_32_32( gain_fx, 165191050 /* 1/13 in Q31*/ ); // Q20 + + FOR( i = 0; i < N; i++ ) + { + v_fx[i] = L_sub( v_fx[i], gain_fx ); // Q20 + move32(); + } + + v_e = 11; // Q20 + move16(); + } + + /*MSVQ*/ + IF( corest->element_mode == 0 ) + { + Word16 v16[32]; + FOR( i = 0; i < N; i++ ) + { + v16[i] = extract_h( L_sub( v_fx[i], gain_fx ) ); + } + + /* MSVQ encoder */ + msvq_encoder( cdk_37bits, v16, levels_37bits, FD_CNG_maxC_37bits, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices ); + + /* MSVQ decoder */ + msvq_decoder( cdk_37bits, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, v16 ); + + FOR( i = 0; i < N; i++ ) + { + v_fx[i] = L_deposit_h( v16[i] ); + } + } + ELSE + { + /* MSVQ encoder */ + Word16 w_fx[32]; + set_val_Word16( w_fx, ONE_IN_Q8, N ); + Word32 dct_target_fx[FDCNG_VQ_DCT_MAXTRUNC]; + Word32 tot_sig_ext_fx[FDCNG_VQ_MAX_LEN]; + + /* DCT domain compressed/truncated indices used for first stage */ + /* quantization with stage1 stored in DCT24 domain, stages 2 through 6 directly dearched + in FDCNG band domain + */ + Word32 tmpRAM_fx[FDCNG_VQ_MAX_LEN][FDCNG_VQ_DCT_MAXTRUNC]; + Word32 *invTrfMatrix_fx = (Word32 *) tmpRAM_fx; /* dynamically filled */ + + IF( EQ_16( N, FDCNG_VQ_MAX_LEN_WB ) ) + { + /* truncated DCT21 analysis */ + create_IDCT_N_Matrix_fx( invTrfMatrix_fx, N, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM_fx ) / ( sizeof( Word32 ) ) ); // Q31 + + dctT2_N_apply_matrix_fx( v_fx /*Q20*/, dct_target_fx, FDCNG_VQ_DCT_MAXTRUNC, N, invTrfMatrix_fx, FDCNG_VQ_DCT_MAXTRUNC, DCT_T2_21_XX ); // Q20 + + /* truncated IDCT21 extension to 24 bands */ + extend_dctN_input_fx( v_fx, dct_target_fx, N, tot_sig_ext_fx, FDCNG_VQ_MAX_LEN, invTrfMatrix_fx, FDCNG_VQ_DCT_MAXTRUNC, IDCT_T2_XX_21 ); // Q20 + + Copy32( tot_sig_ext_fx, v_fx, FDCNG_VQ_MAX_LEN ); /* write extended result as input to VQ stage #1 */ // Q20 + } + + create_IDCT_N_Matrix_fx( invTrfMatrix_fx, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM_fx ) / ( sizeof( Word32 ) ) ); // Q31 + + msvq_enc_ivas_fx( ivas_cdk_37bits_fx, Q7, NULL, NULL, v_fx, v_e, levels_37bits, FD_CNG_maxC_37bits, FD_CNG_stages_37bits, w_fx, N, FD_CNG_maxN_37bits, 1, invTrfMatrix_fx, indices ); + + msvq_dec_fx( ivas_cdk_37bits_fx, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix_fx, v_fx, NULL, 7 ); + + v_e = sub( 31, sub( 20, find_guarded_bits_fx( N ) ) ); + } + + /* Compute gain */ + IF( corest->element_mode == 0 ) + { + /*Q9.23 format */ + gain_fx = 0; + FOR( i = 0; i < N; i++ ) + { + gain_fx = L_add( gain_fx, L_shr( v_fx[i], normShiftN ) ); + } + gain_fx = L_sub( e_fx, L_shl( Mpy_32_16_1( gain_fx, shl( normFacN, sub( normShiftN, 1 ) ) ), 1 ) ); + + /* Apply bitrate-dependant scale */ + { + apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, scaleTableMono, SIZE_SCALE_TABLE_MONO ); + } + } + ELSE + { + gain_fx = 0; + move32(); + FOR( i = 0; i < N; i++ ) + { + gain_fx = L_add( gain_fx, v_fx[i] ); // Q = 31 - v_e + } + e_fx = L_shl( e_fx, sub( 12, v_e ) ); // Q = 31 - v_e + gain_fx = Mpy_32_16_1( L_sub( e_fx, gain_fx ), div_s( 1, N ) ); // Q = 31 - v_e + gain_fx = L_shl( gain_fx, sub( v_e, 8 ) ); // Q23 + + /* Apply bitrate-dependant scale */ + apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, scaleTableStereo, SIZE_SCALE_TABLE_STEREO ); + } + + + /* Quantize gain */ + IF( corest->element_mode == 0 ) + { + /*Q14.23 format */ + gain_fx = L_add( gain_fx, L_shr( gain_fx, 1 ) ); + gain_fx = L_add( gain_fx, 507510784l /*60.5 Q23*/ ); + index = extract_l( L_shr( gain_fx, WORD32_BITS - 1 - 8 ) ); + } + ELSE + { + temp = Madd_32_32( L_shl( GAIN_Q_OFFSET_IVAS_FX_Q0, 22 ), gain_fx, 1610612736 /*1.5 in Q30*/ ); // Q22 + index = extract_l( L_shr( L_add( temp, ONE_IN_Q21 ), 22 ) ); // Q0 + } + + if ( index < 0 ) + { + index = 0; + move16(); + } + + if ( GT_16( index, 127 ) ) + { + index = 127; + move16(); + } + + /* Apply gain and undo log */ + IF( corest->element_mode == 0 ) + { + /* gain Q14.23 format */ + gain_fx = L_shl( L_deposit_l( index ), WORD32_BITS - 1 - 8 ); + gain_fx = L_sub( gain_fx, 503316480l /*60.0 Q23*/ ); + gain_fx = Mpy_32_16_1( gain_fx, 21845 /*2.0f/3.0f Q15*/ ); + Word16 sidNoiseEst_Exp = 0; + + /* sidNoiseEst: format Q6.26, 0.66438561897 = log10(10)/log10(2.0) / 10.0 * 2.0 */ + + /* calculate worst case for scaling */ + { + Word32 maxVal = 0x80000000 /*-1.0 Q31*/; + move32(); + FOR( i = 0; i < N; i++ ) + { + maxVal = L_max( maxVal, v_fx[i] ); + } + + maxVal = L_add( maxVal, gain_fx ); + maxVal = L_shl( Mpy_32_16_1( maxVal, 21771 /*0.66438561897 Q15*/ ), 1 ); + move16(); + WHILE( maxVal >= 0 ) + { + maxVal = L_sub( maxVal, 33554432l /*0.015625 Q31*/ ); + sidNoiseEst_Exp = add( sidNoiseEst_Exp, 1 ); + } + hFdCngCom->sidNoiseEstExp = sidNoiseEst_Exp; + move16(); + E_ExpLd64 = L_shl( sidNoiseEst_Exp, WORD32_BITS - 1 - LD_DATA_SCALE ); + } + + FOR( i = 0; i < N; i++ ) + { + temp = L_add( v_fx[i], gain_fx ); + temp = L_shl( Mpy_32_16_1( temp, 21771 /*0.66438561897 Q15*/ ), 1 ); + temp = L_sub( temp, E_ExpLd64 ); + assert( temp < 0 ); + hFdCngCom->sidNoiseEst[i] = BASOP_Util_InvLog2( temp ); + move32(); + } + } + ELSE + { + gain_fx = L_shl( L_mult0( sub( index, GAIN_Q_OFFSET_IVAS_FX_Q0 ), 21845 /*1.5 in Q15*/ ), sub( 16, v_e ) ); // Q = 31-v_e + + Word16 exp[32]; + FOR( i = 0; i < N; i++ ) + { + temp = Mpy_32_32( L_add( v_fx[i], gain_fx ), 214748365 /* 0.1 in Q31*/ ); // Q = 31-v_e + hFdCngCom->sidNoiseEst[i] = BASOP_Util_fPow( 10, 31, temp, v_e, &exp[i] ); + move32(); + } + + maximum_s( exp, N, &hFdCngCom->sidNoiseEstExp ); + + FOR( i = 0; i < N; i++ ) + { + hFdCngCom->sidNoiseEst[i] = L_shr( hFdCngCom->sidNoiseEst[i], sub( hFdCngCom->sidNoiseEstExp, exp[i] ) ); // exp = hFdCngCom->sidNoiseEstExp + move32(); + } + } + + /* NB last band energy compensation */ + IF( EQ_16( hFdCngCom->CngBandwidth, NB ) ) + { + hFdCngCom->sidNoiseEst[N - 1] = Mpy_32_16_1( hFdCngCom->sidNoiseEst[N - 1], NB_LAST_BAND_SCALE ); // exp(hFdCngCom->sidNoiseEstExp) + move32(); + } + + test(); + IF( EQ_16( hFdCngCom->CngBandwidth, SWB ) && LE_32( hFdCngCom->CngBitrate, ACELP_13k20 ) ) + { + hFdCngCom->sidNoiseEst[N - 1] = Mpy_32_16_1( hFdCngCom->sidNoiseEst[N - 1], SWB_13k2_LAST_BAND_SCALE ); // exp(hFdCngCom->sidNoiseEstExp) + move32(); + } + + + /* Write bitstream */ + IF( EQ_16( corest->codec_mode, MODE2 ) ) + { + FOR( i = 0; i < FD_CNG_stages_37bits; i++ ) + { + push_next_indice( hBstr, indices[i], bits_37bits[i] ); + } + push_next_indice( hBstr, index, 7 ); + } + ELSE + { + Word16 is_frame_len_16k = 0; + move16(); + Word16 IDX = IND_ACELP_16KHZ; + move16(); + push_indice( hBstr, IND_SID_TYPE, 1, 1 ); + if ( corest->element_mode > 0 ) + { + IDX = IND_BWIDTH; + move16(); + } + push_indice( hBstr, IDX, corest->bwidth, 2 ); + if ( EQ_16( corest->L_frame, L_FRAME16k ) ) + { + is_frame_len_16k = 1; + move16(); + } + push_indice( hBstr, IND_ACELP_16KHZ, is_frame_len_16k, 1 ); + FOR( i = 0; i < FD_CNG_stages_37bits; i++ ) + { + push_indice( hBstr, IND_LSF, indices[i], bits_37bits[i] ); + } + + push_indice( hBstr, IND_ENERGY, index, 7 ); + } + + /* Interpolate the bin/band-wise levels from the partition levels */ + scalebands( hFdCngCom->sidNoiseEst, hFdCngEnc->partDec, hFdCngEnc->npartDec, hFdCngEnc->midbandDec, hFdCngEnc->nFFTpartDec, sub( hFdCngEnc->stopBandDec, hFdCngEnc->startBandDec ), hFdCngCom->cngNoiseLevel, 1 ); + hFdCngCom->cngNoiseLevelExp = hFdCngCom->sidNoiseEstExp; + move16(); + + lpc_from_spectrum( hFdCngCom, hFdCngEnc->startBandDec, hFdCngEnc->stopFFTbinDec, preemph_fac ); + + return; +} +#else /*FIX_2489_HARMONIZE_FdCng_encodeSID*/ void FdCng_encodeSID_ivas_fx( Encoder_State *st /* i/o: encoder state structure */ ) @@ -2710,6 +3077,7 @@ void FdCng_encodeSID_ivas_fx( return; } +#endif /*FIX_2489_HARMONIZE_FdCng_encodeSID*/ /*-------------------------------------------------------------------* diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 0281d8f6d..424424a67 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -1473,11 +1473,17 @@ void CNG_enc_fx( Word16 *sid_bw ); /* Generate a bitstream out of the partition levels */ +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID +void FdCng_encodeSID_fx( + HANDLE_FD_CNG_ENC st /* i/o: FD_CNG structure containing all buffers and variables */ +); +#else void FdCng_encodeSID_fx( HANDLE_FD_CNG_ENC st, /* i/o: FD_CNG structure containing all buffers and variables */ Encoder_State *corest, Word16 preemph_fac /* i : preemphase factor */ ); +#endif #ifdef FIX_2455_HARMONIZE_generate_comfort_noise_enc /* Generate the comfort noise based on the target noise level */ @@ -1647,9 +1653,11 @@ Word16 AdjustFirstSID_fx( Encoder_State *stcod /* i : pointer to Coder_State_Plus structure */ ); +#ifndef FIX_2489_HARMONIZE_FdCng_encodeSID void FdCng_encodeSID_ivas_fx( Encoder_State *st /* i/o: encoder state structure */ ); +#endif void resetFdCngEnc_fx( Encoder_State *st ); -- GitLab From e533c1958ca7a82c016fb6378064400eac260ce9 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Tue, 24 Mar 2026 10:49:23 +0100 Subject: [PATCH 02/10] clang patch --- lib_enc/fd_cng_enc_fx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index e311a585d..fbb9fed88 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -2849,7 +2849,7 @@ void FdCng_encodeSID_fx( return; } -#else /*FIX_2489_HARMONIZE_FdCng_encodeSID*/ +#else /*FIX_2489_HARMONIZE_FdCng_encodeSID*/ void FdCng_encodeSID_ivas_fx( Encoder_State *st /* i/o: encoder state structure */ ) -- GitLab From 24fecbe5ffe7010303110351ac87be4246acf253 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Tue, 24 Mar 2026 11:18:03 +0100 Subject: [PATCH 03/10] fix argument --- lib_enc/prot_fx_enc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 424424a67..39ab040f9 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -1475,7 +1475,7 @@ void CNG_enc_fx( /* Generate a bitstream out of the partition levels */ #ifdef FIX_2489_HARMONIZE_FdCng_encodeSID void FdCng_encodeSID_fx( - HANDLE_FD_CNG_ENC st /* i/o: FD_CNG structure containing all buffers and variables */ + Encoder_State *corest ); #else void FdCng_encodeSID_fx( -- GitLab From fcfcf1deded7f9e207677077978aeeb1f93a352a Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Tue, 24 Mar 2026 11:34:10 +0100 Subject: [PATCH 04/10] clang patch --- lib_enc/prot_fx_enc.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 39ab040f9..dddbea356 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -1475,8 +1475,7 @@ void CNG_enc_fx( /* Generate a bitstream out of the partition levels */ #ifdef FIX_2489_HARMONIZE_FdCng_encodeSID void FdCng_encodeSID_fx( - Encoder_State *corest -); + Encoder_State *corest ); #else void FdCng_encodeSID_fx( HANDLE_FD_CNG_ENC st, /* i/o: FD_CNG structure containing all buffers and variables */ -- GitLab From 0477b37e350cfec1cee47eaee95e6962a38939c4 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Tue, 24 Mar 2026 14:18:42 +0100 Subject: [PATCH 05/10] create variable to be able to have simple modification by setting element_mode=0 to have EVS-calculation even in IVAS-mode --- lib_enc/fd_cng_enc_fx.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index fbb9fed88..5ff391f59 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -2504,8 +2504,9 @@ void FdCng_encodeSID_fx( Word16 normFacN, normShiftN; Word16 normFacGain, normShiftGain; Word32 E_ExpLd64; + Word16 element_mode = element_mode; - IF( corest->element_mode > 0 ) + IF( element_mode > 0 ) { (void) normFacN; (void) normShiftN; @@ -2525,7 +2526,7 @@ void FdCng_encodeSID_fx( move16(); E_Exp = hFdCngEnc->msNoiseEst_fx_exp; move16(); - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { E_ExpLd64 = L_shl( E_Exp, WORD32_BITS - 1 - LD_DATA_SCALE ); @@ -2543,7 +2544,7 @@ void FdCng_encodeSID_fx( /* Convert to LOG */ e_fx = L_deposit_l( 0 ); - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { /* e: Q14.23 format, v_fx: Q9.23 format */ temp = Mpy_32_32_r( L_shl( 1, sub( 31, E_Exp ) ), 214748 ); /* 1e-4f, Q31-E_Exp */ @@ -2580,7 +2581,7 @@ void FdCng_encodeSID_fx( gain_fx = 0; move32(); - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { /* gain: Q9.23 format */ FOR( i = N_GAIN_MIN; i < N_GAIN_MAX; i++ ) @@ -2610,7 +2611,7 @@ void FdCng_encodeSID_fx( } /*MSVQ*/ - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { Word16 v16[32]; FOR( i = 0; i < N; i++ ) @@ -2667,7 +2668,7 @@ void FdCng_encodeSID_fx( } /* Compute gain */ - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { /*Q9.23 format */ gain_fx = 0; @@ -2700,7 +2701,7 @@ void FdCng_encodeSID_fx( /* Quantize gain */ - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { /*Q14.23 format */ gain_fx = L_add( gain_fx, L_shr( gain_fx, 1 ) ); @@ -2726,7 +2727,7 @@ void FdCng_encodeSID_fx( } /* Apply gain and undo log */ - IF( corest->element_mode == 0 ) + IF( element_mode == 0 ) { /* gain Q14.23 format */ gain_fx = L_shl( L_deposit_l( index ), WORD32_BITS - 1 - 8 ); @@ -2820,7 +2821,7 @@ void FdCng_encodeSID_fx( Word16 IDX = IND_ACELP_16KHZ; move16(); push_indice( hBstr, IND_SID_TYPE, 1, 1 ); - if ( corest->element_mode > 0 ) + if ( element_mode > 0 ) { IDX = IND_BWIDTH; move16(); -- GitLab From fd1404b7410b718599e42413f7f753de8709c4e4 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Wed, 25 Mar 2026 08:38:35 +0100 Subject: [PATCH 06/10] fix bug --- lib_enc/fd_cng_enc_fx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index 5ff391f59..14d4d8051 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -2504,7 +2504,7 @@ void FdCng_encodeSID_fx( Word16 normFacN, normShiftN; Word16 normFacGain, normShiftGain; Word32 E_ExpLd64; - Word16 element_mode = element_mode; + Word16 element_mode = corest->element_mode; IF( element_mode > 0 ) { -- GitLab From 362851f9bfe6a5fb6ae8f194623c357227aa8ff6 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Wed, 25 Mar 2026 09:57:18 +0100 Subject: [PATCH 07/10] cleanup a bit --- lib_enc/fd_cng_enc_fx.c | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index 40dddc932..20d3f87ec 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -1946,22 +1946,21 @@ void FdCng_encodeSID_fx( ) { Word16 i, index, N; - Word32 temp, gain_fx, e_fx; - HANDLE_FD_CNG_ENC hFdCngEnc = corest->hFdCngEnc; - HANDLE_FD_CNG_COM hFdCngCom = hFdCngEnc->hFdCngCom; - Word16 preemph_fac = corest->preemph_fac; // Q15 - move16(); - BSTR_ENC_HANDLE hBstr = corest->hBstr; + Word16 normFacGain, normShiftGain, normFacN, normShiftN, E_Exp; Word16 indices[32]; + Word32 temp, gain_fx, e_fx, E_ExpLd64; Word32 v_fx[32]; Word32 *E_fx; - Word16 v_e, E_Exp; - Word16 normFacN, normShiftN; - Word16 normFacGain, normShiftGain; - Word32 E_ExpLd64; Word16 element_mode = corest->element_mode; + move16(); + Word16 preemph_fac = corest->preemph_fac; // Q15 + move16(); - IF( element_mode > 0 ) + BSTR_ENC_HANDLE hBstr = corest->hBstr; + HANDLE_FD_CNG_ENC hFdCngEnc = corest->hFdCngEnc; + HANDLE_FD_CNG_COM hFdCngCom = hFdCngEnc->hFdCngCom; + + if( element_mode > 0 ) { (void) normFacN; (void) normShiftN; @@ -1969,10 +1968,6 @@ void FdCng_encodeSID_fx( (void) normShiftGain; (void) E_ExpLd64; } - ELSE - { - (void) v_e; - } /* Init */ @@ -2060,9 +2055,6 @@ void FdCng_encodeSID_fx( v_fx[i] = L_sub( v_fx[i], gain_fx ); // Q20 move32(); } - - v_e = 11; // Q20 - move16(); } /*MSVQ*/ @@ -2093,6 +2085,7 @@ void FdCng_encodeSID_fx( Word32 dct_target_fx[FDCNG_VQ_DCT_MAXTRUNC]; Word32 tot_sig_ext_fx[FDCNG_VQ_MAX_LEN]; + /* DCT domain compressed/truncated indices used for first stage */ /* quantization with stage1 stored in DCT24 domain, stages 2 through 6 directly dearched in FDCNG band domain @@ -2115,11 +2108,9 @@ void FdCng_encodeSID_fx( create_IDCT_N_Matrix_fx( invTrfMatrix_fx, FDCNG_VQ_MAX_LEN, FDCNG_VQ_DCT_MAXTRUNC, sizeof( tmpRAM_fx ) / ( sizeof( Word32 ) ) ); // Q31 - msvq_enc_ivas_fx( ivas_cdk_37bits_fx, Q7, NULL, NULL, v_fx, v_e, levels_37bits, FD_CNG_maxC_37bits, FD_CNG_stages_37bits, w_fx, N, FD_CNG_maxN_37bits, 1, invTrfMatrix_fx, indices ); + msvq_enc_ivas_fx( ivas_cdk_37bits_fx, Q7, NULL, NULL, v_fx, 11 /*Q20*/, levels_37bits, FD_CNG_maxC_37bits, FD_CNG_stages_37bits, w_fx, N, FD_CNG_maxN_37bits, 1, invTrfMatrix_fx, indices ); msvq_dec_fx( ivas_cdk_37bits_fx, NULL, NULL, FD_CNG_stages_37bits, N, FD_CNG_maxN_37bits, indices, 1, invTrfMatrix_fx, v_fx, NULL, 7 ); - - v_e = sub( 31, sub( 20, find_guarded_bits_fx( N ) ) ); } /* Compute gain */ @@ -2140,6 +2131,7 @@ void FdCng_encodeSID_fx( } ELSE { + Word16 v_e = sub( 31, sub( 20, find_guarded_bits_fx( N ) ) ); gain_fx = 0; move32(); FOR( i = 0; i < N; i++ ) @@ -2226,6 +2218,7 @@ void FdCng_encodeSID_fx( } ELSE { + Word16 v_e = sub( 31, sub( 20, find_guarded_bits_fx( N ) ) ); gain_fx = L_shl( L_mult0( sub( index, GAIN_Q_OFFSET_IVAS_FX_Q0 ), 21845 /*1.5 in Q15*/ ), sub( 16, v_e ) ); // Q = 31-v_e Word16 exp[32]; -- GitLab From bc3120e8412ade3a3d1ef96c9ea1d376f22c86be Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Wed, 25 Mar 2026 10:49:16 +0100 Subject: [PATCH 08/10] more cleanup --- lib_enc/fd_cng_enc_fx.c | 37 ++++++++++++------------------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index 20d3f87ec..98d3cec89 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -1941,14 +1941,15 @@ void perform_noise_estimation_enc_ivas_fx( * Generate a bitstream out of the partition levels *-------------------------------------------------------------------*/ #ifdef FIX_2489_HARMONIZE_FdCng_encodeSID +#define FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP /* FhG: check FdCng_encodeSID higher precision in IVAS case - development */ void FdCng_encodeSID_fx( Encoder_State *corest /* i/o: encoder state structure */ ) { Word16 i, index, N; - Word16 normFacGain, normShiftGain, normFacN, normShiftN, E_Exp; + Word16 E_Exp; Word16 indices[32]; - Word32 temp, gain_fx, e_fx, E_ExpLd64; + Word32 temp, gain_fx, e_fx; Word32 v_fx[32]; Word32 *E_fx; Word16 element_mode = corest->element_mode; @@ -1960,14 +1961,6 @@ void FdCng_encodeSID_fx( HANDLE_FD_CNG_ENC hFdCngEnc = corest->hFdCngEnc; HANDLE_FD_CNG_COM hFdCngCom = hFdCngEnc->hFdCngCom; - if( element_mode > 0 ) - { - (void) normFacN; - (void) normShiftN; - (void) normFacGain; - (void) normShiftGain; - (void) E_ExpLd64; - } /* Init */ @@ -1976,26 +1969,14 @@ void FdCng_encodeSID_fx( move16(); E_Exp = hFdCngEnc->msNoiseEst_fx_exp; move16(); - IF( element_mode == 0 ) - { - E_ExpLd64 = L_shl( E_Exp, WORD32_BITS - 1 - LD_DATA_SCALE ); - - normFacN = getNormReciprocalWord16( N ); - normShiftN = BASOP_util_norm_s_bands2shift( N ); - - normFacGain = getNormReciprocalWord16( N_GAIN_MAX - N_GAIN_MIN ); - normShiftGain = BASOP_util_norm_s_bands2shift( N_GAIN_MAX - N_GAIN_MIN ); - } - ELSE - { - set_zero_fx( v_fx, FDCNG_VQ_MAX_LEN ); - } - /* Convert to LOG */ e_fx = L_deposit_l( 0 ); IF( element_mode == 0 ) { + Word16 normShiftN = BASOP_util_norm_s_bands2shift( N ); + Word16 normFacN = getNormReciprocalWord16( N ); + Word32 E_ExpLd64 = L_shl( E_Exp, WORD32_BITS - 1 - LD_DATA_SCALE ); /* e: Q14.23 format, v_fx: Q9.23 format */ temp = Mpy_32_32_r( L_shl( 1, sub( 31, E_Exp ) ), 214748 ); /* 1e-4f, Q31-E_Exp */ FOR( i = 0; i < N; i++ ) @@ -2010,6 +1991,7 @@ void FdCng_encodeSID_fx( } ELSE { + set_zero_fx( v_fx, FDCNG_VQ_MAX_LEN ); FOR( i = 0; i < N; i++ ) { IF( E_fx[i] == 0 ) @@ -2033,6 +2015,8 @@ void FdCng_encodeSID_fx( move32(); IF( element_mode == 0 ) { + Word16 normFacGain = getNormReciprocalWord16( N_GAIN_MAX - N_GAIN_MIN ); + Word16 normShiftGain = BASOP_util_norm_s_bands2shift( N_GAIN_MAX - N_GAIN_MIN ); /* gain: Q9.23 format */ FOR( i = N_GAIN_MIN; i < N_GAIN_MAX; i++ ) { @@ -2116,6 +2100,8 @@ void FdCng_encodeSID_fx( /* Compute gain */ IF( element_mode == 0 ) { + Word16 normShiftN = BASOP_util_norm_s_bands2shift( N ); + Word16 normFacN = getNormReciprocalWord16( N ); /*Q9.23 format */ gain_fx = 0; FOR( i = 0; i < N; i++ ) @@ -2176,6 +2162,7 @@ void FdCng_encodeSID_fx( /* Apply gain and undo log */ IF( element_mode == 0 ) { + Word32 E_ExpLd64; /* gain Q14.23 format */ gain_fx = L_shl( L_deposit_l( index ), WORD32_BITS - 1 - 8 ); gain_fx = L_sub( gain_fx, 503316480l /*60.0 Q23*/ ); -- GitLab From 62ce374386e611086b71b00173337dbc2f86e344 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Wed, 25 Mar 2026 11:00:42 +0100 Subject: [PATCH 09/10] create macro to test evs-precision in ivas-case --- lib_enc/fd_cng_enc_fx.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index 98d3cec89..82547aba0 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -1941,7 +1941,7 @@ void perform_noise_estimation_enc_ivas_fx( * Generate a bitstream out of the partition levels *-------------------------------------------------------------------*/ #ifdef FIX_2489_HARMONIZE_FdCng_encodeSID -#define FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP /* FhG: check FdCng_encodeSID higher precision in IVAS case - development */ +#define FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP /* FhG: activate FdCng_encodeSID higher precision in IVAS case - development */ void FdCng_encodeSID_fx( Encoder_State *corest /* i/o: encoder state structure */ ) @@ -1972,7 +1972,10 @@ void FdCng_encodeSID_fx( /* Convert to LOG */ e_fx = L_deposit_l( 0 ); + +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP IF( element_mode == 0 ) +#endif { Word16 normShiftN = BASOP_util_norm_s_bands2shift( N ); Word16 normFacN = getNormReciprocalWord16( N ); @@ -1989,6 +1992,7 @@ void FdCng_encodeSID_fx( } e_fx = L_shl( Mpy_32_16_1( e_fx, shl( normFacN, sub( normShiftN, 1 ) ) ), 1 ); } +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP ELSE { set_zero_fx( v_fx, FDCNG_VQ_MAX_LEN ); @@ -2008,12 +2012,15 @@ void FdCng_encodeSID_fx( e_fx = L_add( e_fx, L_shr( v_fx[i], 1 ) ); // Q19, add one bit headroom } } +#endif /* Normalize MSVQ input */ gain_fx = 0; move32(); +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP IF( element_mode == 0 ) +#endif { Word16 normFacGain = getNormReciprocalWord16( N_GAIN_MAX - N_GAIN_MIN ); Word16 normShiftGain = BASOP_util_norm_s_bands2shift( N_GAIN_MAX - N_GAIN_MIN ); @@ -2024,6 +2031,7 @@ void FdCng_encodeSID_fx( } gain_fx = L_shl( Mpy_32_16_1( gain_fx, shl( normFacGain, sub( normShiftGain, 1 ) ) ), 1 ); } +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP ELSE { FOR( i = N_GAIN_MIN; i < N_GAIN_MAX; i++ ) @@ -2040,6 +2048,7 @@ void FdCng_encodeSID_fx( move32(); } } +#endif /*MSVQ*/ IF( element_mode == 0 ) @@ -2098,7 +2107,9 @@ void FdCng_encodeSID_fx( } /* Compute gain */ +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP IF( element_mode == 0 ) +#endif { Word16 normShiftN = BASOP_util_norm_s_bands2shift( N ); Word16 normFacN = getNormReciprocalWord16( N ); @@ -2109,12 +2120,8 @@ void FdCng_encodeSID_fx( gain_fx = L_add( gain_fx, L_shr( v_fx[i], normShiftN ) ); } gain_fx = L_sub( e_fx, L_shl( Mpy_32_16_1( gain_fx, shl( normFacN, sub( normShiftN, 1 ) ) ), 1 ) ); - - /* Apply bitrate-dependant scale */ - { - apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, scaleTableMono, SIZE_SCALE_TABLE_MONO ); - } } +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP ELSE { Word16 v_e = sub( 31, sub( 20, find_guarded_bits_fx( N ) ) ); @@ -2127,25 +2134,32 @@ void FdCng_encodeSID_fx( e_fx = L_shl( e_fx, sub( 12, v_e ) ); // Q = 31 - v_e gain_fx = Mpy_32_16_1( L_sub( e_fx, gain_fx ), div_s( 1, N ) ); // Q = 31 - v_e gain_fx = L_shl( gain_fx, sub( v_e, 8 ) ); // Q23 - - /* Apply bitrate-dependant scale */ - apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, scaleTableStereo, SIZE_SCALE_TABLE_STEREO ); } +#endif + + /* Apply bitrate-dependant scale */ + apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, + ( element_mode == 0 ) ? ( scaleTableMono ) : ( scaleTableStereo ), + ( element_mode == 0 ) ? ( SIZE_SCALE_TABLE_MONO ) : ( SIZE_SCALE_TABLE_STEREO ) ); /* Quantize gain */ +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP IF( element_mode == 0 ) +#endif { /*Q14.23 format */ gain_fx = L_add( gain_fx, L_shr( gain_fx, 1 ) ); gain_fx = L_add( gain_fx, 507510784l /*60.5 Q23*/ ); index = extract_l( L_shr( gain_fx, WORD32_BITS - 1 - 8 ) ); } +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP ELSE { temp = Madd_32_32( L_shl( GAIN_Q_OFFSET_IVAS_FX_Q0, 22 ), gain_fx, 1610612736 /*1.5 in Q30*/ ); // Q22 index = extract_l( L_shr( L_add( temp, ONE_IN_Q21 ), 22 ) ); // Q0 } +#endif if ( index < 0 ) { @@ -2160,7 +2174,9 @@ void FdCng_encodeSID_fx( } /* Apply gain and undo log */ +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP IF( element_mode == 0 ) +#endif { Word32 E_ExpLd64; /* gain Q14.23 format */ @@ -2203,6 +2219,7 @@ void FdCng_encodeSID_fx( move32(); } } +#ifdef FIX_2489_HARMONIZE_FdCng_encodeSID_IVASHP ELSE { Word16 v_e = sub( 31, sub( 20, find_guarded_bits_fx( N ) ) ); @@ -2224,6 +2241,7 @@ void FdCng_encodeSID_fx( move32(); } } +#endif /* NB last band energy compensation */ IF( EQ_16( hFdCngCom->CngBandwidth, NB ) ) -- GitLab From 0c3550490443415438501d163a54a6c4d0ece0a9 Mon Sep 17 00:00:00 2001 From: Fabian Bauer Date: Wed, 25 Mar 2026 11:02:54 +0100 Subject: [PATCH 10/10] clang patch --- lib_enc/fd_cng_enc_fx.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib_enc/fd_cng_enc_fx.c b/lib_enc/fd_cng_enc_fx.c index 82547aba0..a5f180232 100644 --- a/lib_enc/fd_cng_enc_fx.c +++ b/lib_enc/fd_cng_enc_fx.c @@ -2138,9 +2138,9 @@ void FdCng_encodeSID_fx( #endif /* Apply bitrate-dependant scale */ - apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, - ( element_mode == 0 ) ? ( scaleTableMono ) : ( scaleTableStereo ), - ( element_mode == 0 ) ? ( SIZE_SCALE_TABLE_MONO ) : ( SIZE_SCALE_TABLE_STEREO ) ); + apply_scale( &gain_fx, hFdCngCom->CngBandwidth, hFdCngCom->CngBitrate, + ( element_mode == 0 ) ? ( scaleTableMono ) : ( scaleTableStereo ), + ( element_mode == 0 ) ? ( SIZE_SCALE_TABLE_MONO ) : ( SIZE_SCALE_TABLE_STEREO ) ); /* Quantize gain */ -- GitLab