From 4cfa5c7d239bebcb63096b7eea40ba63f7dcf6c1 Mon Sep 17 00:00:00 2001 From: Sandesh Venkatesh Date: Fri, 14 Mar 2025 14:18:39 +0530 Subject: [PATCH 1/3] Fix for 3GPP issue 1382: High MLD > 20, obvious audible differences between float and fixed, SBA 24.4 kbps, 32 khz - 1 Link #1382 --- lib_enc/ivas_tcx_core_enc_fx.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib_enc/ivas_tcx_core_enc_fx.c b/lib_enc/ivas_tcx_core_enc_fx.c index d5898cd4a..9066c1ca6 100644 --- a/lib_enc/ivas_tcx_core_enc_fx.c +++ b/lib_enc/ivas_tcx_core_enc_fx.c @@ -769,6 +769,8 @@ Word16 ivas_acelp_tcx20_switching_fx( Word32 tcx_snr; Flag Overflow; Word32 gain, noise; + Word16 noise_e = 0; + move16(); Word32 *pt_ener_sfr, ener_sfr[NB_SUBFR16k]; /* Initialization */ @@ -1214,12 +1216,15 @@ Word16 ivas_acelp_tcx20_switching_fx( gain = get_gain2( wsp + i, wsp + sub( i, T0 ), L_SUBFR ); noise = L_deposit_l( 1 ); - + noise_e = 0; + move16(); FOR( j = 0; j < L_SUBFR; j++ ) { - tmp16 = round_fx_o( L_shl_o( Mpy_32_16_r( gain, wsp[i + j - T0] ), 15, &Overflow ), &Overflow ); // q_in - tmp16 = sub_o( wsp[i + j], tmp16, &Overflow ); // q_in - noise = L_mac0_o( noise, tmp16, tmp16, &Overflow ); // 2*q_in// + tmp32 = Mpy_32_16_1( gain, wsp[i + j - T0] ); // Q16 + q_inp - 15 + tmp32 = L_sub( wsp[i + j], L_shr( tmp32, 1 ) ); // q_inp + tmp16 = norm_l( tmp32 ); + tmp32 = L_shl( tmp32, tmp16 ); // q_inp +tmp16 + noise = BASOP_Util_Add_Mant32Exp( noise, noise_e, Mpy_32_32( tmp32, tmp32 ), shl( sub( 31, add( q_inp, tmp16 ) ), 1 ), &noise_e ); // noise_e } test(); IF( noise == 0 || EQ_32( noise, 1 ) ) @@ -1238,7 +1243,7 @@ Word16 ivas_acelp_tcx20_switching_fx( ELSE { noise = Mpy_32_16_1( noise, scale ); - tmp32 = L_add( BASOP_Util_Log2( noise ), L_shl( sub( 31, add( q_inp, q_inp ) ), 25 ) ); // Q25 + tmp32 = L_add( BASOP_Util_Log2( noise ), L_shl( noise_e, 25 ) ); // Q25 } tmp32 = L_sub( *pt_ener_sfr, tmp32 ); // Q25 temp_energy = BASOP_Util_Add_Mant32Exp( tmp32, 6, temp_energy, ener_e, &ener_e ); -- GitLab From 4743eb2a3441f408b6a2b31b51de60755c516da6 Mon Sep 17 00:00:00 2001 From: Sandesh Venkatesh Date: Fri, 14 Mar 2025 17:27:26 +0530 Subject: [PATCH 2/3] Few more changes to resolve deviations in paths --- lib_com/ivas_prot_fx.h | 6 +-- lib_enc/core_enc_init_fx.c | 4 +- lib_enc/ivas_core_pre_proc_front_fx.c | 2 +- lib_enc/ivas_cpe_enc_fx.c | 8 +-- lib_enc/ivas_ism_enc_fx.c | 4 +- lib_enc/ivas_sce_enc_fx.c | 6 +-- lib_enc/ivas_stat_enc.h | 2 +- lib_enc/ivas_stereo_dft_enc_itd_fx.c | 2 +- lib_enc/ivas_tcx_core_enc_fx.c | 12 ++--- lib_enc/prot_fx_enc.h | 2 +- lib_enc/stat_enc.h | 5 +- lib_enc/tcx_ltp_enc_fx.c | 4 +- lib_enc/transient_detection_fx.c | 77 +++++++++++++++------------ 13 files changed, 74 insertions(+), 60 deletions(-) diff --git a/lib_com/ivas_prot_fx.h b/lib_com/ivas_prot_fx.h index 71de2db03..40eb28b70 100644 --- a/lib_com/ivas_prot_fx.h +++ b/lib_com/ivas_prot_fx.h @@ -2985,7 +2985,7 @@ Word16 ivas_acelp_tcx20_switching_fx( Word16 non_staX, /* i : unbound non-stationarity for sp/mu clas */ Word16 *pitch_fr, /* i : fraction pitch values */ Word16 *voicing_fr, /* i : fractional voicing values */ - Word16 currFlatness, /* i : flatness */ + Word32 currFlatness, /* i : flatness */ Word16 lsp_mid[M], /* i : LSPs at the middle of the frame */ Word16 stab_fac, /* i : LP filter stability */ Word32 *res_cod_SNR_M, @@ -3102,7 +3102,7 @@ Word16 transient_analysis_ivas_fx( void set_transient_stereo_fx( CPE_ENC_HANDLE hCPE, /* i : CPE structure */ - Word16 currFlatness[] /* i/o: current flatness Q7*/ + Word32 currFlatness[] /* i/o: current flatness Q21*/ ); void ivas_smc_mode_selection_fx( @@ -5955,7 +5955,7 @@ ivas_error pre_proc_front_ivas_fx( Word16 *fft_buff_fx_q, /* o : FFT buffer */ const Word16 tdm_A_PCh_fx[M + 1], /* i : unq. LP coeff. of primary channel Q12*/ const Word16 tdm_lsp_new_PCh_fx[M], /* i : unq. LSPs of primary channel Q15*/ - const Word16 currFlatness_fx, /* i : flatness parameter Q7*/ + const Word32 currFlatness_fx, /* i : flatness parameter Q21*/ const Word16 tdm_ratio_idx, /* i : Current Ratio_L index Q0*/ Word32 fr_bands_LR_fx[][2 * NB_BANDS], /* i : energy in frequency bands (fr_bands_LR_fx_q) fr_bands_LR_fx_q*/ Word16 fr_bands_LR_fx_q[CPE_CHANNELS], diff --git a/lib_enc/core_enc_init_fx.c b/lib_enc/core_enc_init_fx.c index 1c60f2ecd..bfe17fcd2 100644 --- a/lib_enc/core_enc_init_fx.c +++ b/lib_enc/core_enc_init_fx.c @@ -1028,8 +1028,8 @@ void init_coder_ace_plus_ivas_fx( { st->acelpFramesCount = 0; move16(); - st->prevTempFlatness_fx = 128 /*1.0f Q7*/; - move16(); + st->prevTempFlatness_32fx = ONE_IN_Q21 /*1.0f Q21*/; + move32(); } /* Initialize TBE */ diff --git a/lib_enc/ivas_core_pre_proc_front_fx.c b/lib_enc/ivas_core_pre_proc_front_fx.c index 42920a46a..45098926e 100644 --- a/lib_enc/ivas_core_pre_proc_front_fx.c +++ b/lib_enc/ivas_core_pre_proc_front_fx.c @@ -115,7 +115,7 @@ ivas_error pre_proc_front_ivas_fx( Word16 *fft_buff_fx_q, /* o : FFT buffer */ const Word16 tdm_A_PCh_fx[M + 1], /* i : unq. LP coeff. of primary channel Q12*/ const Word16 tdm_lsp_new_PCh_fx[M], /* i : unq. LSPs of primary channel Q15*/ - const Word16 currFlatness_fx, /* i : flatness parameter Q7*/ + const Word32 currFlatness_fx, /* i : flatness parameter Q21*/ const Word16 tdm_ratio_idx, /* i : Current Ratio_L index Q0*/ Word32 fr_bands_LR_fx[][2 * NB_BANDS], /* i : energy in frequency bands (fr_bands_LR_fx_q) fr_bands_LR_fx_q*/ Word16 fr_bands_LR_fx_q[CPE_CHANNELS], diff --git a/lib_enc/ivas_cpe_enc_fx.c b/lib_enc/ivas_cpe_enc_fx.c index 1152736f6..ea70a33d3 100644 --- a/lib_enc/ivas_cpe_enc_fx.c +++ b/lib_enc/ivas_cpe_enc_fx.c @@ -103,7 +103,7 @@ ivas_error ivas_cpe_enc_fx( Word16 vad_flag_dtx[CPE_CHANNELS]; /* HE-SAD flag with additional DTX HO */ Word32 enerBuffer_fx[CPE_CHANNELS][CLDFB_NO_CHANNELS_MAX]; /* energy buffer */ Word16 enerBuffer_fx_exp[CPE_CHANNELS]; /* energy buffer */ - Word16 currFlatness_fx[CPE_CHANNELS]; /* flatness parameter Q7 */ + Word32 currFlatness_fx[CPE_CHANNELS]; /* flatness parameter Q21 */ Word16 tdm_ratio_idx, tdm_ratio_idx_SM; /* temp. TD stereo parameters */ Word16 tdm_SM_or_LRTD_Pri; /* temp. TD stereo parameters */ @@ -517,7 +517,7 @@ ivas_error ivas_cpe_enc_fx( IF( sts[n]->hTranDet == NULL ) { currFlatness_fx[n] = 0; - move16(); + move32(); CONTINUE; } @@ -527,8 +527,8 @@ ivas_error ivas_cpe_enc_fx( RunTransientDetection_ivas_fx( sts[n]->input_fx, input_frame, sts[n]->hTranDet, sts[n]->q_inp ); // Note q of sts[n]->input_fx changes inside function } - currFlatness_fx[n] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) sts[n]->hTranDet, NSUBBLOCKS, 0 ); // Q7 - move16(); + currFlatness_fx[n] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) sts[n]->hTranDet, NSUBBLOCKS, 0 ); // Q21 + move32(); } /* Synchonize detection for downmix-based stereo */ diff --git a/lib_enc/ivas_ism_enc_fx.c b/lib_enc/ivas_ism_enc_fx.c index 1b648cc98..c2f77c159 100644 --- a/lib_enc/ivas_ism_enc_fx.c +++ b/lib_enc/ivas_ism_enc_fx.c @@ -88,7 +88,7 @@ ivas_error ivas_ism_enc_fx( Word16 vad_flag_dtx[MAX_NUM_OBJECTS][1]; /* HE-SAD flag with additional DTX HO */ Word32 enerBuffer_fx[MAX_NUM_OBJECTS][1][CLDFB_NO_CHANNELS_MAX]; /* energy buffer */ Word16 enerBuffer_fx_exp[MAX_NUM_OBJECTS][1]; /* energy buffer */ - Word16 currFlatness_fx[1]; /* flatness parameter */ + Word32 currFlatness_fx[1]; /* flatness parameter */ Word16 fft_buff_fx[MAX_NUM_OBJECTS][1][2 * L_FFT]; /* FFT buffer */ Word16 fft_buff_fx_q[MAX_NUM_OBJECTS][1]; /* FFT buffer */ Word32 fr_bands_fx[1][2 * NB_BANDS]; /* energy in frequency bands */ @@ -213,7 +213,7 @@ ivas_error ivas_ism_enc_fx( RunTransientDetection_ivas_fx( st->input_fx, input_frame, st->hTranDet, st->q_inp ); - currFlatness_fx[0] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, 0 ); // Q7 + currFlatness_fx[0] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, 0 ); // Q21 move16(); /*----------------------------------------------------------------* diff --git a/lib_enc/ivas_sce_enc_fx.c b/lib_enc/ivas_sce_enc_fx.c index 35b65e4c1..3a0f19c50 100644 --- a/lib_enc/ivas_sce_enc_fx.c +++ b/lib_enc/ivas_sce_enc_fx.c @@ -78,7 +78,7 @@ ivas_error ivas_sce_enc_fx( Word16 pitch_fr_fx[1][NB_SUBFR]; /* fractional pitch values */ Word16 voicing_fr_fx[1][NB_SUBFR]; /* fractional pitch gains */ Word16 relE_fx[1]; /* frame relative energy Q8 */ - Word16 currFlatness_fx[1]; /* flatness parameter Q7 */ + Word32 currFlatness_fx[1]; /* flatness parameter Q7 */ Word16 Etot_LR_fx[1]; /* total energy Q8 */ Word32 realBuffer_fx[1][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */ Word32 imagBuffer_fx[1][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */ @@ -180,8 +180,8 @@ ivas_error ivas_sce_enc_fx( RunTransientDetection_ivas_fx( st->input_fx, input_frame, st->hTranDet, q_input ); } - currFlatness_fx[0] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, 0 ); // Q7 - move16(); + currFlatness_fx[0] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, 0 ); // Q21 + move32(); /*----------------------------------------------------------------* * Configuration of core encoder diff --git a/lib_enc/ivas_stat_enc.h b/lib_enc/ivas_stat_enc.h index ac258cfc1..918acc417 100644 --- a/lib_enc/ivas_stat_enc.h +++ b/lib_enc/ivas_stat_enc.h @@ -66,7 +66,7 @@ typedef struct stereo_itd_data_struct Word16 prev_index; Word32 prev_avg_max_fx; Word16 prev_avg_max_fx_e; - Word16 currFlatness_fx; + Word32 currFlatness_fx; /* Xtalk classifier */ Word32 prev_m1_fx; // Q31 diff --git a/lib_enc/ivas_stereo_dft_enc_itd_fx.c b/lib_enc/ivas_stereo_dft_enc_itd_fx.c index 8a887de89..af10f7dea 100644 --- a/lib_enc/ivas_stereo_dft_enc_itd_fx.c +++ b/lib_enc/ivas_stereo_dft_enc_itd_fx.c @@ -2233,7 +2233,7 @@ void stereo_dft_enc_compute_itd_fx( test(); test(); test(); - IF( flag_noisy_speech_snr == 0 && EQ_16( hCPE->hCoreCoder[0]->vad_flag, 1 ) && hItd->detected_itd_flag == 0 && ( LT_16( hItd->currFlatness_fx, 192 ) /* 1.5 in Q7*/ || EQ_16( hCPE->hCoreCoder[0]->sp_aud_decision0, 1 ) ) ) + IF( flag_noisy_speech_snr == 0 && EQ_16( hCPE->hCoreCoder[0]->vad_flag, 1 ) && hItd->detected_itd_flag == 0 && ( LT_32( hItd->currFlatness_fx, 3145728 ) /* 1.5 in Q21*/ || EQ_16( hCPE->hCoreCoder[0]->sp_aud_decision0, 1 ) ) ) { // hItd->itd_thres *= 1.5f; hItd->itd_thres_fx = L_shl_sat( Mpy_32_32( hItd->itd_thres_fx, 1610612736 ), 1 ); /* Saturation added to avoid assertions (this needs to be investigated) */ diff --git a/lib_enc/ivas_tcx_core_enc_fx.c b/lib_enc/ivas_tcx_core_enc_fx.c index 9066c1ca6..67c41eed3 100644 --- a/lib_enc/ivas_tcx_core_enc_fx.c +++ b/lib_enc/ivas_tcx_core_enc_fx.c @@ -732,7 +732,7 @@ Word16 ivas_acelp_tcx20_switching_fx( Word16 non_staX, /*Q8 i : unbound non-stationarity for sp/mu clas*/ Word16 *pitch_fr, /*Q6 i : fraction pitch values */ Word16 *voicing_fr, /*Q15 i : fractional voicing values */ - Word16 currFlatness, /*Q7 i : flatness */ + Word32 currFlatness, /*Q21 i : flatness */ Word16 lsp_mid[M], /*Q15 i : LSPs at the middle of the frame */ Word16 stab_fac, /* i : LP filter stability */ Word32 *res_cod_SNR_M, @@ -1275,8 +1275,8 @@ Word16 ivas_acelp_tcx20_switching_fx( test(); if ( ( GT_32( snr_acelp, tcx_snr ) ) && ( LT_32( snr_acelp, L_add( tcx_snr, 131072 /*2.0f Q16*/ ) ) ) && - ( LT_16( add_o( st->prevTempFlatness_fx, currFlatness, &Overflow ), 416 /*3.25f Q7*/ ) || EQ_16( stab_fac, 0x7fff ) || - ( !flag_16k_smc && ( st->sp_aud_decision0 > 0 ) && LT_16( add_sat( st->prevTempFlatness_fx, currFlatness ), 2560 /*20.f Q7*/ ) ) ) && + ( LT_32( L_add_o( st->prevTempFlatness_32fx, currFlatness, &Overflow ), 6815744 /*3.25f Q21*/ ) || EQ_16( stab_fac, 0x7fff ) || + ( !flag_16k_smc && ( st->sp_aud_decision0 > 0 ) && LT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 41943040/*20.f Q21*/ ) ) ) && ( LE_16( st->Nb_ACELP_frames, 6 ) ) ) { dsnr = -131072 /*-2.0f Q16*/; @@ -1288,7 +1288,7 @@ Word16 ivas_acelp_tcx20_switching_fx( test(); if ( ( LT_32( snr_acelp, tcx_snr ) ) && ( GT_32( snr_acelp, L_sub( tcx_snr, 131072 /*2.0f Q16*/ ) ) ) && - ( GT_16( add_sat( st->prevTempFlatness_fx, currFlatness ), 416 /*3.25f Q7*/ ) ) && + ( GT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 6815744 /*3.25f Q21*/ ) ) && ( GE_16( st->Nb_ACELP_frames, 6 ) ) ) { dsnr = 131072 /*2.0f Q16*/; @@ -1334,7 +1334,7 @@ Word16 ivas_acelp_tcx20_switching_fx( /* Select ACELP or TCX */ test(); test(); - IF( GT_32( L_add( snr_acelp, dsnr ), tcx_snr ) && ( st->sp_aud_decision0 == 0 || GT_16( add_sat( st->prevTempFlatness_fx, currFlatness ), 416 /*3.25f Q7*/ ) ) ) + IF( GT_32( L_add( snr_acelp, dsnr ), tcx_snr ) && ( st->sp_aud_decision0 == 0 || GT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 6815744/*3.25f Q21*/ ) ) ) { smc_dec_ol = 0; move16(); @@ -1358,7 +1358,7 @@ Word16 ivas_acelp_tcx20_switching_fx( } #endif - st->prevTempFlatness_fx = currFlatness; + st->prevTempFlatness_32fx = currFlatness; move16(); return smc_dec_ol; } diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 3698eba02..151cd0f89 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -1131,7 +1131,7 @@ void RunTransientDetection_ivas_fx( * @return average temporal flatness measure with exponent AVG_FLAT_E */ Word16 GetTCXAvgTemporalFlatnessMeasure_fx( struct TransientDetection const *pTransientDetection, Word16 nCurrentSubblocks, Word16 nPrevSubblocks ); -Word16 GetTCXAvgTemporalFlatnessMeasure_ivas_fx( struct TransientDetection const *pTransientDetection, Word16 nCurrentSubblocks, Word16 nPrevSubblocks ); +Word32 GetTCXAvgTemporalFlatnessMeasure_ivas_fx( struct TransientDetection const *pTransientDetection, Word16 nCurrentSubblocks, Word16 nPrevSubblocks ); /** Get the maximum energy change using subblock energies aligned with the TCX. * @param pTransientDetection Structure that contains transient detectors to be run. diff --git a/lib_enc/stat_enc.h b/lib_enc/stat_enc.h index 41c6b0468..86af426bc 100644 --- a/lib_enc/stat_enc.h +++ b/lib_enc/stat_enc.h @@ -140,7 +140,9 @@ typedef struct DelayBuffer *pDelayBuffer; /* Delay buffer. */ Word32 subblockNrg[NSUBBLOCKS + MAX_TD_DELAY]; // IVAS Q(-1) Word32 accSubblockNrg[NSUBBLOCKS + MAX_TD_DELAY + 1]; // IVAS Q(-1) - Word16 subblockNrgChange[NSUBBLOCKS + MAX_TD_DELAY]; /* EVS: Q7(15 - SUBBLOCK_NRG_CHANGE_E) */ /* IVAS: Q3 */ + Word16 subblockNrgChange[NSUBBLOCKS + MAX_TD_DELAY]; /* EVS: Q7(15 - SUBBLOCK_NRG_CHANGE_E) */ + Word32 subblockNrgChange_32fx[NSUBBLOCKS + MAX_TD_DELAY]; /* IVAS: subblockNrgChange_exp */ + Word16 subblockNrgChange_exp[NSUBBLOCKS + MAX_TD_DELAY]; Word16 nDelay; /* Size of the delay buffer in use, as number of subblocks. Maximum delay from all users of this buffer. */ Word16 nPartialDelay; /* Delay of the input (modulo pDelayBuffer->nSubblockSize), nPartialDelay <= pDelayBuffer->nDelay. */ @@ -1669,6 +1671,7 @@ typedef struct enc_core_structure Word16 transient_info[3]; Word16 acelpFramesCount; Word16 prevTempFlatness_fx; /* exponent is AVG_FLAT_E Q7 in EVS */ /* Q4 in IVAS */ + Word32 prevTempFlatness_32fx; /* Q21 in IVAS */ // float currEnergyLookAhead; Word32 currEnergyLookAhead_fx; // Q31 diff --git a/lib_enc/tcx_ltp_enc_fx.c b/lib_enc/tcx_ltp_enc_fx.c index e1d478e95..94773b288 100644 --- a/lib_enc/tcx_ltp_enc_fx.c +++ b/lib_enc/tcx_ltp_enc_fx.c @@ -1035,13 +1035,13 @@ void tcx_ltp_encode_ivas_fx( IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) ) { - tempFlatness_fx = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS - NSUBBLOCKS_SHIFT, add( nPrevSubblocks, NSUBBLOCKS_SHIFT ) ); // Q7 + tempFlatness_fx = extract_l(L_shr(GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS - NSUBBLOCKS_SHIFT, add( nPrevSubblocks, NSUBBLOCKS_SHIFT ) ), 14)); // Q7 maxEnergyChange_fx = GetTCXMaxenergyChange_ivas_fx( st->hTranDet, (const Word8) isTCX10, NSUBBLOCKS - NSUBBLOCKS_SHIFT, add( nPrevSubblocks, NSUBBLOCKS_SHIFT ) ); // Q3 } ELSE { - tempFlatness_fx = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, nPrevSubblocks ); // Q7 + tempFlatness_fx = extract_l(L_shr((GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, nPrevSubblocks )), 14)); // Q7 maxEnergyChange_fx = GetTCXMaxenergyChange_ivas_fx( st->hTranDet, (const Word8) isTCX10, NSUBBLOCKS, nPrevSubblocks ); // Q3 } diff --git a/lib_enc/transient_detection_fx.c b/lib_enc/transient_detection_fx.c index 3c88ca224..44bd8cc3c 100644 --- a/lib_enc/transient_detection_fx.c +++ b/lib_enc/transient_detection_fx.c @@ -419,16 +419,17 @@ Word16 GetTCXAvgTemporalFlatnessMeasure_fx( struct TransientDetection const *pTr return i; } -Word16 GetTCXAvgTemporalFlatnessMeasure_ivas_fx( struct TransientDetection const *pTransientDetection, Word16 nCurrentSubblocks, Word16 nPrevSubblocks ) +Word32 GetTCXAvgTemporalFlatnessMeasure_ivas_fx( struct TransientDetection const *pTransientDetection, Word16 nCurrentSubblocks, Word16 nPrevSubblocks ) { - Word16 i; + Word32 i; TransientDetector const *pTransientDetector; SubblockEnergies const *pSubblockEnergies; Word16 nDelay; Word16 nRelativeDelay; - Word16 const *pSubblockNrgChange; + Word32 const *pSubblockNrgChange; + Word16 const *pSubblockNrgChange_exp; Word32 sumTempFlatness; - Word16 nTotBlocks; + Word16 nTotBlocks, sumTempFlatness_exp, exp; /* Initialization */ pTransientDetector = &pTransientDetection->transientDetector; @@ -445,15 +446,21 @@ Word16 GetTCXAvgTemporalFlatnessMeasure_ivas_fx( struct TransientDetection const assert( ( nPrevSubblocks <= nRelativeDelay ) && ( nCurrentSubblocks <= NSUBBLOCKS + nDelay ) ); - pSubblockNrgChange = &pSubblockEnergies->subblockNrgChange[sub( nRelativeDelay, nPrevSubblocks )]; // subblockNrgChange Q3 + pSubblockNrgChange = &pSubblockEnergies->subblockNrgChange_32fx[( nRelativeDelay - nPrevSubblocks )]; + pSubblockNrgChange_exp = &pSubblockEnergies->subblockNrgChange_exp[( nRelativeDelay - nPrevSubblocks )]; + sumTempFlatness = 0; + move32(); + sumTempFlatness_exp = 0; + move16(); FOR( i = 0; i < nTotBlocks; i++ ) { - sumTempFlatness = L_add( sumTempFlatness, L_deposit_l( pSubblockNrgChange[i] ) ); + sumTempFlatness = BASOP_Util_Add_Mant32Exp( sumTempFlatness, sumTempFlatness_exp, pSubblockNrgChange[i], pSubblockNrgChange_exp[i], &sumTempFlatness_exp ); } /* exponent = AVG_FLAT_E */ - i = div_l( L_shl( sumTempFlatness, 5 ), nTotBlocks ); // Q7 - + i = BASOP_Util_Divide3232_Scale_cadence( sumTempFlatness, nTotBlocks, &exp ); + exp = add( exp, sub( sumTempFlatness_exp, 31 ) ); + i = L_shl_sat( i, sub( exp, 10 ) ); // Can be saturated, since it is used for comparision againt 3.25/20.0f, Q21 return i; } @@ -546,7 +553,8 @@ Word16 GetTCXMaxenergyChange_ivas_fx( TRAN_DET_HANDLE hTranDet, SubblockEnergies const *pSubblockEnergies; Word16 nDelay; Word16 nRelativeDelay; - Word16 const *pSubblockNrgChange; + Word32 const *pSubblockNrgChange; + Word16 const *pSubblockNrgChange_exp; Word16 maxEnergyChange; Word16 nTotBlocks; @@ -563,7 +571,8 @@ Word16 GetTCXMaxenergyChange_ivas_fx( TRAN_DET_HANDLE hTranDet, maxEnergyChange = 0 /*0.0f Q3*/; move16(); assert( ( nPrevSubblocks <= nRelativeDelay ) && ( nCurrentSubblocks <= NSUBBLOCKS + nDelay ) ); - pSubblockNrgChange = &pSubblockEnergies->subblockNrgChange[nRelativeDelay - nPrevSubblocks]; + pSubblockNrgChange = &pSubblockEnergies->subblockNrgChange_32fx[nRelativeDelay - nPrevSubblocks]; + pSubblockNrgChange_exp = &pSubblockEnergies->subblockNrgChange_exp[nRelativeDelay - nPrevSubblocks]; IF( s_or( pTransientDetector->bIsAttackPresent, isTCX10 ) ) /* frame is TCX-10 */ { @@ -602,7 +611,7 @@ Word16 GetTCXMaxenergyChange_ivas_fx( TRAN_DET_HANDLE hTranDet, FOR( i = 0; i < nTotBlocks; i++ ) { - maxEnergyChange = s_max( maxEnergyChange, pSubblockNrgChange[i] ); + maxEnergyChange = s_max( maxEnergyChange, extract_l( L_shl_sat( pSubblockNrgChange[i], sub( pSubblockNrgChange_exp[i], 28 ) ) ) ); // Q3 } move16(); @@ -875,7 +884,7 @@ void SetTCXModeInfo_ivas_fx( move16(); } } - tmp = BASOP_Util_Divide3216_Scale( ONE_IN_Q23, GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) hTranDet, NSUBBLOCKS, 0 ), &exp_diff ); + tmp = BASOP_Util_Divide3232_Scale( ONE_IN_Q21, GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) hTranDet, NSUBBLOCKS, 0 ), &exp_diff ); tmp = shl_sat( tmp, exp_diff ); // Q15 test(); IF( isLongTermTransient_fx( L_deposit_h( tmp ), &hTcxEnc->tfm_mem_fx ) && EQ_16( st->element_mode, IVAS_CPE_MDCT ) ) @@ -905,7 +914,7 @@ void SetTCXModeInfo_ivas_fx( *tcxModeOverlap = ALDO_WINDOW; move16(); } - tmp = BASOP_Util_Divide3216_Scale( ONE_IN_Q23, GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) hTranDet, NSUBBLOCKS, 0 ), &exp_diff ); + tmp = BASOP_Util_Divide3232_Scale( ONE_IN_Q21, GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) hTranDet, NSUBBLOCKS, 0 ), &exp_diff ); tmp = shl_sat( tmp, exp_diff ); // Q15 test(); IF( isLongTermTransient_fx( L_deposit_h( tmp ), &hTcxEnc->tfm_mem_fx ) && EQ_16( st->element_mode, IVAS_CPE_MDCT ) ) @@ -1124,7 +1133,8 @@ static void InitSubblockEnergies_ivas_fx( Word16 nFrameLength, Word16 nDelay, De set32_fx( pSubblockEnergies->subblockNrg, 54 /* 107.37 in Q(-1) */, nMaxBuffSize ); set32_fx( pSubblockEnergies->accSubblockNrg, 54 /* 107.37 in Q(-1) */, nMaxBuffSize + 1 ); - set16_fx( pSubblockEnergies->subblockNrgChange, ONE_IN_Q3, nMaxBuffSize ); + set32_fx( pSubblockEnergies->subblockNrgChange_32fx, ONE_IN_Q15, nMaxBuffSize ); + set16_fx( pSubblockEnergies->subblockNrgChange_exp, 16, nMaxBuffSize ); IF( nDelay != 0 ) { pSubblockEnergies->nDelay = idiv1616( nDelay, pDelayBuffer->nSubblockSize ); @@ -1340,12 +1350,14 @@ static void UpdateSubblockEnergies_ivas_fx( Word16 const *input, Word16 nSamples /* Shift old subblock energies */ FOR( i = 0; i < pSubblockEnergies->nDelay; i++ ) { + move32(); move32(); move32(); move16(); pSubblockEnergies->subblockNrg[i] = pSubblockEnergies->subblockNrg[i + NSUBBLOCKS]; pSubblockEnergies->accSubblockNrg[i] = pSubblockEnergies->accSubblockNrg[i + NSUBBLOCKS]; - pSubblockEnergies->subblockNrgChange[i] = pSubblockEnergies->subblockNrgChange[i + NSUBBLOCKS]; + pSubblockEnergies->subblockNrgChange_32fx[i] = pSubblockEnergies->subblockNrgChange_32fx[i + NSUBBLOCKS]; + pSubblockEnergies->subblockNrgChange_exp[i] = pSubblockEnergies->subblockNrgChange_exp[i + NSUBBLOCKS]; } /* Compute filtered subblock energies for the new samples */ @@ -1492,7 +1504,8 @@ static void CalculateSubblockEnergies_ivas_fx( Word16 const *input, Word16 nSamp Word16 facAccSubblockNrg; Word32 *pSubblockNrg; Word32 *pAccSubblockNrg; - Word16 *pSubblockNrgChange; + Word32 *pSubblockNrgChange; + Word16 *pSubblockNrgChange_exp; Word32 *pAccSubblockTmp; Word16 nWindows; Word16 w, k, k2; @@ -1513,7 +1526,8 @@ static void CalculateSubblockEnergies_ivas_fx( Word16 const *input, Word16 nSamp delayBuffer = &pDelayBuffer->buffer[sub( pDelayBuffer->nDelay, nPartialDelay )]; pSubblockNrg = &pSubblockEnergies->subblockNrg[nDelay]; pAccSubblockNrg = &pSubblockEnergies->accSubblockNrg[nDelay]; - pSubblockNrgChange = &pSubblockEnergies->subblockNrgChange[nDelay]; + pSubblockNrgChange = &pSubblockEnergies->subblockNrgChange_32fx[nDelay]; + pSubblockNrgChange_exp = &pSubblockEnergies->subblockNrgChange_exp[nDelay]; /* nWindows = (nSamplesAvailable + nPartialDelay) / nSubblockSize */ nWindows = shr( div_s( add( nSamplesAvailable, nPartialDelay ), shl( nSubblockSize, 7 ) ), 8 ); @@ -1564,19 +1578,16 @@ static void CalculateSubblockEnergies_ivas_fx( Word16 const *input, Word16 nSamp IF( GT_32( w0, w1 ) ) { - k2 = BASOP_Util_Divide3232_uu_1616_Scale( w0, w1, &k ); + pSubblockNrgChange[w] = BASOP_Util_Divide3232_Scale_cadence( w0, w1, &k ); + pSubblockNrgChange_exp[w] = k; } ELSE { - k2 = BASOP_Util_Divide3232_uu_1616_Scale( w1, w0, &k ); + pSubblockNrgChange[w] = BASOP_Util_Divide3232_Scale_cadence( w1, w0, &k ); + pSubblockNrgChange_exp[w] = k; } + move32(); move16(); - pSubblockNrgChange[w] = MAX_16; - IF( LT_16( k, 12 ) ) - { - move16(); - pSubblockNrgChange[w] = shr_r( k2, sub( 12, k ) ); - } } } } @@ -1803,17 +1814,17 @@ Word16 transient_analysis_ivas_fx( *-------------------------------------------------------------------*/ void set_transient_stereo_fx( CPE_ENC_HANDLE hCPE, /* i : CPE structure */ - Word16 currFlatness[] /* i/o: current flatness */ + Word32 currFlatness[] /* i/o: current flatness */ ) { Word16 n, attackIsPresent; - Word16 currFlatnessMax; + Word32 currFlatnessMax; Encoder_State **sts; sts = hCPE->hCoreCoder; /* for DFT/TD based stereo ,map avg. flatness to individual stereo channels (M/S or X/Y) */ - maximum_fx( currFlatness, CPE_CHANNELS, &currFlatnessMax ); + maximum_32_fx( currFlatness, CPE_CHANNELS, &currFlatnessMax ); attackIsPresent = 0; move16(); @@ -1822,7 +1833,7 @@ void set_transient_stereo_fx( attackIsPresent = s_max( attackIsPresent, sts[n]->hTranDet->transientDetector.bIsAttackPresent ); } - set16_fx( currFlatness, currFlatnessMax, CPE_CHANNELS ); + set32_fx( currFlatness, currFlatnessMax, CPE_CHANNELS ); FOR( n = 0; n < CPE_CHANNELS; n++ ) { @@ -1850,8 +1861,8 @@ void set_transient_stereo_fx( move16(); FOR( n = 0; n < CPE_CHANNELS; n++ ) { - hCPE->hStereoDft->hItd->currFlatness_fx = s_max( hCPE->hStereoDft->hItd->currFlatness_fx, currFlatness[n] ); - move16(); + hCPE->hStereoDft->hItd->currFlatness_fx = L_max( hCPE->hStereoDft->hItd->currFlatness_fx, currFlatness[n] ); + move32(); } } @@ -1861,8 +1872,8 @@ void set_transient_stereo_fx( move16(); FOR( n = 0; n < CPE_CHANNELS; n++ ) { - hCPE->hStereoMdct->hItd->currFlatness_fx = s_max( hCPE->hStereoMdct->hItd->currFlatness_fx, currFlatness[n] ); - move16(); + hCPE->hStereoMdct->hItd->currFlatness_fx = L_max( hCPE->hStereoMdct->hItd->currFlatness_fx, currFlatness[n] ); + move32(); } } -- GitLab From ab4cf272f064a27adf3a7a8ed24a13d3279ee59f Mon Sep 17 00:00:00 2001 From: Sandesh Venkatesh Date: Fri, 14 Mar 2025 17:31:12 +0530 Subject: [PATCH 3/3] Clang formatting changes --- lib_enc/ivas_tcx_core_enc_fx.c | 4 ++-- lib_enc/stat_enc.h | 14 +++++++------- lib_enc/tcx_ltp_enc_fx.c | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib_enc/ivas_tcx_core_enc_fx.c b/lib_enc/ivas_tcx_core_enc_fx.c index 67c41eed3..3dc5f5b8a 100644 --- a/lib_enc/ivas_tcx_core_enc_fx.c +++ b/lib_enc/ivas_tcx_core_enc_fx.c @@ -1276,7 +1276,7 @@ Word16 ivas_acelp_tcx20_switching_fx( if ( ( GT_32( snr_acelp, tcx_snr ) ) && ( LT_32( snr_acelp, L_add( tcx_snr, 131072 /*2.0f Q16*/ ) ) ) && ( LT_32( L_add_o( st->prevTempFlatness_32fx, currFlatness, &Overflow ), 6815744 /*3.25f Q21*/ ) || EQ_16( stab_fac, 0x7fff ) || - ( !flag_16k_smc && ( st->sp_aud_decision0 > 0 ) && LT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 41943040/*20.f Q21*/ ) ) ) && + ( !flag_16k_smc && ( st->sp_aud_decision0 > 0 ) && LT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 41943040 /*20.f Q21*/ ) ) ) && ( LE_16( st->Nb_ACELP_frames, 6 ) ) ) { dsnr = -131072 /*-2.0f Q16*/; @@ -1334,7 +1334,7 @@ Word16 ivas_acelp_tcx20_switching_fx( /* Select ACELP or TCX */ test(); test(); - IF( GT_32( L_add( snr_acelp, dsnr ), tcx_snr ) && ( st->sp_aud_decision0 == 0 || GT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 6815744/*3.25f Q21*/ ) ) ) + IF( GT_32( L_add( snr_acelp, dsnr ), tcx_snr ) && ( st->sp_aud_decision0 == 0 || GT_32( L_add_sat( st->prevTempFlatness_32fx, currFlatness ), 6815744 /*3.25f Q21*/ ) ) ) { smc_dec_ol = 0; move16(); diff --git a/lib_enc/stat_enc.h b/lib_enc/stat_enc.h index 86af426bc..7ca996af5 100644 --- a/lib_enc/stat_enc.h +++ b/lib_enc/stat_enc.h @@ -137,14 +137,14 @@ typedef struct /* Subblock energies: Holds subblock energies and recursively accumulated energies. Also buffers the energies. */ typedef struct { - DelayBuffer *pDelayBuffer; /* Delay buffer. */ - Word32 subblockNrg[NSUBBLOCKS + MAX_TD_DELAY]; // IVAS Q(-1) - Word32 accSubblockNrg[NSUBBLOCKS + MAX_TD_DELAY + 1]; // IVAS Q(-1) - Word16 subblockNrgChange[NSUBBLOCKS + MAX_TD_DELAY]; /* EVS: Q7(15 - SUBBLOCK_NRG_CHANGE_E) */ + DelayBuffer *pDelayBuffer; /* Delay buffer. */ + Word32 subblockNrg[NSUBBLOCKS + MAX_TD_DELAY]; // IVAS Q(-1) + Word32 accSubblockNrg[NSUBBLOCKS + MAX_TD_DELAY + 1]; // IVAS Q(-1) + Word16 subblockNrgChange[NSUBBLOCKS + MAX_TD_DELAY]; /* EVS: Q7(15 - SUBBLOCK_NRG_CHANGE_E) */ Word32 subblockNrgChange_32fx[NSUBBLOCKS + MAX_TD_DELAY]; /* IVAS: subblockNrgChange_exp */ Word16 subblockNrgChange_exp[NSUBBLOCKS + MAX_TD_DELAY]; - Word16 nDelay; /* Size of the delay buffer in use, as number of subblocks. Maximum delay from all users of this buffer. */ - Word16 nPartialDelay; /* Delay of the input (modulo pDelayBuffer->nSubblockSize), nPartialDelay <= pDelayBuffer->nDelay. */ + Word16 nDelay; /* Size of the delay buffer in use, as number of subblocks. Maximum delay from all users of this buffer. */ + Word16 nPartialDelay; /* Delay of the input (modulo pDelayBuffer->nSubblockSize), nPartialDelay <= pDelayBuffer->nDelay. */ /* Decay factor for the recursive accumulation */ Word16 facAccSubblockNrg; @@ -1671,7 +1671,7 @@ typedef struct enc_core_structure Word16 transient_info[3]; Word16 acelpFramesCount; Word16 prevTempFlatness_fx; /* exponent is AVG_FLAT_E Q7 in EVS */ /* Q4 in IVAS */ - Word32 prevTempFlatness_32fx; /* Q21 in IVAS */ + Word32 prevTempFlatness_32fx; /* Q21 in IVAS */ // float currEnergyLookAhead; Word32 currEnergyLookAhead_fx; // Q31 diff --git a/lib_enc/tcx_ltp_enc_fx.c b/lib_enc/tcx_ltp_enc_fx.c index 94773b288..19afa8243 100644 --- a/lib_enc/tcx_ltp_enc_fx.c +++ b/lib_enc/tcx_ltp_enc_fx.c @@ -1035,13 +1035,13 @@ void tcx_ltp_encode_ivas_fx( IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) ) { - tempFlatness_fx = extract_l(L_shr(GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS - NSUBBLOCKS_SHIFT, add( nPrevSubblocks, NSUBBLOCKS_SHIFT ) ), 14)); // Q7 + tempFlatness_fx = extract_l( L_shr( GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS - NSUBBLOCKS_SHIFT, add( nPrevSubblocks, NSUBBLOCKS_SHIFT ) ), 14 ) ); // Q7 maxEnergyChange_fx = GetTCXMaxenergyChange_ivas_fx( st->hTranDet, (const Word8) isTCX10, NSUBBLOCKS - NSUBBLOCKS_SHIFT, add( nPrevSubblocks, NSUBBLOCKS_SHIFT ) ); // Q3 } ELSE { - tempFlatness_fx = extract_l(L_shr((GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, nPrevSubblocks )), 14)); // Q7 + tempFlatness_fx = extract_l( L_shr( ( GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) st->hTranDet, NSUBBLOCKS, nPrevSubblocks ) ), 14 ) ); // Q7 maxEnergyChange_fx = GetTCXMaxenergyChange_ivas_fx( st->hTranDet, (const Word8) isTCX10, NSUBBLOCKS, nPrevSubblocks ); // Q3 } -- GitLab