From 1c7cce5e6489d12ca4708533de0ed5dbb6827b2d Mon Sep 17 00:00:00 2001 From: Sandesh Venkatesh Date: Wed, 15 Jan 2025 12:01:41 +0530 Subject: [PATCH] MSAN, ASAN fixes for encoder and decoder, LTV crash fix and high MLD fix [x] ltv_basop_encoder-OMASA 1TC 2ISM at 16.4 kbps, 16kHz in, 48kHz out, BINAURAL out crash fix [x] MSAN and ASAN fixes for ltv enc and dec [x] [stereo at 24.4 kbps, 32kHz in, 32kHz out] high MLD fix --- lib_com/prot_fx.h | 17 ++ lib_com/swb_tbe_com_fx.c | 301 +++++++++++++++++++++++++++ lib_dec/TonalComponentDetection_fx.c | 256 ++++++++++++++++++++++- lib_dec/gs_dec_fx.c | 4 + lib_dec/ivas_cpe_dec_fx.c | 4 + lib_dec/ivas_mdct_core_dec.c | 3 + lib_dec/ivas_post_proc.c | 4 + lib_enc/igf_enc.c | 5 + lib_enc/ivas_cpe_enc.c | 3 + lib_enc/ivas_stereo_dft_enc.c | 24 ++- lib_enc/swb_tbe_enc_fx.c | 10 +- 11 files changed, 623 insertions(+), 8 deletions(-) diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 0080f4125..5d62e81ed 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -2914,6 +2914,23 @@ void GenShapedWBExcitation_fx( const Word16 signal_type, const Word16 igf_flag ); +void GenShapedWBExcitation_ivas_enc_fx( + Word16 *excSHB, /* o : synthesized shaped shb exctiation */ + const Word16 *lpc_shb, /* i : lpc coefficients */ + Word16 *exc4kWhtnd, /* o : whitened synthesized shb excitation */ + Word32 *mem_csfilt, /* i/o : memory */ + Word16 *mem_genSHBexc_filt_down1, /* i/o : memory */ + Word16 *mem_genSHBexc_filt_down2, /* i/o : memory */ + Word16 *mem_genSHBexc_filt_down3, /* i/o : memory */ + Word16 *state_lpc_syn, /* i/o : memory */ + const Word16 coder_type, /* i : coding type */ + const Word16 *bwe_exc_extended, /* i : bandwidth extended exciatation */ + const Word16 Q_bwe_exc, + Word16 bwe_seed[], /* i/o : random number generator seed */ + const Word16 voice_factors[], /* i : voicing factor */ + const Word16 signal_type, + const Word16 igf_flag ); + void GenWBSynth_fx( const Word16 *input_synspeech, /* i : i synthesized speech */ Word16 *shb_syn_speech_16k, /* o : output highband compnent */ diff --git a/lib_com/swb_tbe_com_fx.c b/lib_com/swb_tbe_com_fx.c index 7b64a2923..884c11097 100644 --- a/lib_com/swb_tbe_com_fx.c +++ b/lib_com/swb_tbe_com_fx.c @@ -1951,6 +1951,307 @@ void GenShapedWBExcitation_fx( } +void GenShapedWBExcitation_ivas_enc_fx( + Word16 *excSHB, /* o : synthesized shaped shb exctiation Q(Q_bwe_exc) */ + const Word16 *lpc_shb, /* i : lpc coefficients Q12 */ + Word16 *exc4kWhtnd, /* o : whitened synthesized shb excitation Q(Q_bwe_exc) */ + Word32 *mem_csfilt, /* i/o : memory Q(Q_bwe_exc+16) */ + Word16 *mem_genSHBexc_filt_down1, /* i/o : memory Q(Q_bwe_exc) */ + Word16 *mem_genSHBexc_filt_down2, /* i/o : memory Q(Q_bwe_exc) */ + Word16 *mem_genSHBexc_filt_down3, /* i/o : memory Q(Q_bwe_exc) */ + Word16 *state_lpc_syn, /* i/o : memory Q(Q_bwe_exc) */ + const Word16 coder_type, /* i : coding type */ + const Word16 *bwe_exc_extended, /* i : bwidth extended exciatation Q(Q_bwe_exc) */ + const Word16 Q_bwe_exc, /* i : Q for memories */ + Word16 bwe_seed[], /* i/o : random number generator seed */ + const Word16 voice_factors[], /* i : voicing factor Q15 */ + const Word16 uv_flag, /* i : unvoiced flag */ + const Word16 igf_flag ) +{ + Word16 i, j, k; + Word16 wht_fil_mem[LPC_WHTN_ORDER_WB]; + Word16 lpc_whtn[LPC_WHTN_ORDER_WB + 1]; + Word16 R_h[LPC_WHTN_ORDER_WB + 2], R_l[LPC_WHTN_ORDER_WB + 2]; + Word16 Q_R; + Word16 excTmp[L_FRAME16k]; + Word16 excTmp2[L_FRAME16k / 4]; + Word16 excTmp2_frac[L_FRAME16k / 4]; + Word16 exc4k[L_FRAME16k / 4]; + Word16 exc4k_frac[L_FRAME16k / 4]; + Word32 exc4k_32[L_FRAME16k / 4]; + Word32 pow1, pow22; + Word16 scale; + Word32 excNoisyEnv[L_FRAME16k / 4]; + Word16 csfilt_num2[1] = { 1638 }; /* Q15*/ + Word16 neg_csfilt_den2[2] = { -32768, 31457 }; /* Q15 */ + Word32 L_tmp, Ltemp1, Ltemp2; + Word16 temp1, temp2, exp; + Word32 Lmax; + Word16 max_val, n1, n2, sc; + Word32 LepsP[LPC_WHTN_ORDER_WB + 1]; + Word16 tmp_vfac; + Word16 avg_voice_fac; +#ifdef BASOP_NOGLOB_DECLARE_LOCAL + Flag Overflow = 0; + move32(); +#endif + move16(); + move16(); + move16(); + + /*0.25f*sum_f(voice_factors, NB_SUBFR)*/ + L_tmp = L_mult( voice_factors[0], 8192 /*0.25 in Q15 */ ); + FOR( i = 1; i < NB_SUBFR; i++ ) + { + L_tmp = L_mac( L_tmp, voice_factors[i], 8192 /*0.25 in Q15 */ ); + } + avg_voice_fac = round_fx( L_tmp ); + + test(); + test(); + test(); + test(); + IF( igf_flag != 0 && ( EQ_16( coder_type, VOICED ) || GT_16( avg_voice_fac, 11469 /* 0.35 in Q15 */ ) ) ) /*Q15 -> 0.35f*/ + { + csfilt_num2[0] = 6554; + move16(); /*Q15 -> 0.2f*/ + neg_csfilt_den2[1] = 26214; + move16(); /*Q15 -> 0.8f*/ + } + ELSE IF( igf_flag != 0 && ( EQ_16( coder_type, UNVOICED ) || LT_16( avg_voice_fac, 6654 /* 0.2 in Q15 */ ) ) ) /*Q15 -> 0.2f*/ + { + csfilt_num2[0] = 328; + move16(); /*Q15 -> 0.01f*/ + neg_csfilt_den2[1] = 32440; + move16(); /*Q15 -> 0.99f*/ + } + set16_fx( wht_fil_mem, 0, LPC_WHTN_ORDER_WB ); + Decimate_allpass_steep_fx( bwe_exc_extended, mem_genSHBexc_filt_down1, L_FRAME32k, excTmp ); + flip_spectrum_and_decimby4_fx( excTmp, exc4k, L_FRAME16k, mem_genSHBexc_filt_down2, mem_genSHBexc_filt_down3, 0 ); + + IF( uv_flag ) + { + create_random_vector_fx( exc4kWhtnd, L_FRAME16k / 4, bwe_seed ); + IF( LT_16( Q_bwe_exc, 5 ) ) + { + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { + exc4kWhtnd[i] = shl_r( exc4kWhtnd[i], sub( Q_bwe_exc, 5 ) ); /*Q(Q_bwe_exc)/Q5(if Q_bwe_exc > 5) */ + move16(); + } + } + } + ELSE + { + autocorr_fx( exc4k, LPC_WHTN_ORDER_WB + 1, R_h, R_l, &Q_R, + L_FRAME16k / 4, win_flatten_4k_fx, 0, 1 ); + + /* Ensure R[0] isn't zero when entering Levinson Durbin */ + R_l[0] = s_max( R_l[0], 1 ); + move16(); + FOR( i = 1; i <= LPC_WHTN_ORDER_WB; i++ ) + { + L_tmp = Mpy_32( R_h[i], R_l[i], wac_h[i - 1], wac_l[i - 1] ); + L_Extract( L_tmp, &R_h[i], &R_l[i] ); + } + + E_LPC_lev_dur( R_h, R_l, lpc_whtn, LepsP, LPC_WHTN_ORDER_WB, NULL ); + + Copy_Scale_sig( lpc_whtn, lpc_whtn, LPC_WHTN_ORDER_WB + 1, sub( norm_s( lpc_whtn[0] ), 2 ) ); + + fir_fx( exc4k, lpc_whtn, exc4kWhtnd, wht_fil_mem, L_FRAME16k / 4, + LPC_WHTN_ORDER_WB, 0, 3 ); + + /* Ensure pow1 is greater than zero when computing normalization */ + max_val = 0; + move16(); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { + excTmp2[i] = abs_s( exc4kWhtnd[i] ); + move16(); /* Q_bwe_exc */ + max_val = s_max( max_val, excTmp2[i] ); + move16(); + } + + IF( max_val == 0 ) + { + pow1 = 1; + move16(); + n1 = 0; + move16(); + } + ELSE + { + n1 = norm_s( max_val ); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { +#ifdef BASOP_NOGLOB + excTmp2_frac[i] = shl_o( excTmp2[i], n1, &Overflow ); +#else + excTmp2_frac[i] = shl( excTmp2[i], n1 ); +#endif + move16(); /* Q14 */ + } + n1 = sub( sub( 14, n1 ), Q_bwe_exc ); + pow1 = 1; + move32(); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { +#ifdef BASOP_NOGLOB + L_tmp = L_mult_o( excTmp2_frac[i], excTmp2_frac[i], &Overflow ); /* Q29 */ + pow1 = L_add_o( pow1, L_shr( L_tmp, 7 ), &Overflow ); /* Q22 */ +#else + L_tmp = L_mult( excTmp2_frac[i], excTmp2_frac[i] ); /* Q29 */ + pow1 = L_add( pow1, L_shr( L_tmp, 7 ) ); /* Q22 */ +#endif + } + } + + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { +#ifdef BASOP_NOGLOB + excNoisyEnv[i] = L_add_o( *mem_csfilt, L_mult_o( csfilt_num2[0], excTmp2[i], &Overflow ), &Overflow ); +#else + excNoisyEnv[i] = L_add( *mem_csfilt, L_mult( csfilt_num2[0], excTmp2[i] ) ); +#endif + move32(); /* Q_bwe_exc+16 */ + *mem_csfilt = Mpy_32_16_1( excNoisyEnv[i], neg_csfilt_den2[1] ); + move32(); /* Q_bwe_exc+16 */ + } + + create_random_vector_fx( exc4k, L_FRAME16k / 4, bwe_seed ); + + /* Ensure pow22 is greater than zero when computing normalization */ + Lmax = 0; + move32(); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { + exc4k_32[i] = Mpy_32_16_1( excNoisyEnv[i], exc4k[i] ); + move32(); /* Q_bwe_exc+6 */ + Lmax = L_max( Lmax, L_abs( exc4k_32[i] ) ); + } + + IF( Lmax == 0 ) + { + pow22 = 1; + move16(); + n2 = 0; + move16(); + set16_fx( exc4k_frac, 0, L_FRAME16k / 4 ); + } + ELSE + { + n2 = norm_l( Lmax ); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { +#ifdef BASOP_NOGLOB + exc4k_frac[i] = extract_h( L_shl_o( exc4k_32[i], n2, &Overflow ) ); /* Q(14-n2) */ +#else + exc4k_frac[i] = extract_h( L_shl( exc4k_32[i], n2 ) ); /* Q(14-n2) */ +#endif + move16(); + } + n2 = sub( sub( 30, n2 ), add( Q_bwe_exc, 6 ) ); + pow22 = 1; + move32(); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { +#ifdef BASOP_NOGLOB + L_tmp = L_mult_o( exc4k_frac[i], exc4k_frac[i], &Overflow ); /* Q29 */ + pow22 = L_add_o( pow22, L_shr( L_tmp, 7 ), &Overflow ); /* Q22 */ +#else + L_tmp = L_mult( exc4k_frac[i], exc4k_frac[i] ); /* Q29 */ + pow22 = L_add( pow22, L_shr( L_tmp, 7 ) ); /* Q22 */ +#endif + } + } + + test(); + test(); + IF( EQ_16( coder_type, UNVOICED ) || ( igf_flag != 0 && LT_16( avg_voice_fac, 6654 /*0.2 in Q15 */ ) ) ) + { + L_tmp = root_a_over_b_fx( pow1, sub( 22, shl( n1, 1 ) ), pow22, sub( 22, shl( n2, 1 ) ), &exp ); +#ifdef BASOP_NOGLOB + scale = round_fx_o( L_shl_o( L_tmp, exp, &Overflow ), &Overflow ); /*Q15 */ +#else + scale = round_fx( L_shl( L_tmp, exp ) ); /*Q15 */ +#endif + sc = sub( add( n2, Q_bwe_exc ), 14 ); + FOR( i = 0; i < L_FRAME16k / 4; i++ ) + { +#ifdef BASOP_NOGLOB + exc4kWhtnd[i] = round_fx_o( L_shl_o( L_mult_o( exc4k_frac[i], scale, &Overflow ), sc, &Overflow ), &Overflow ); /* Q_bwe_exc+n2-10+16+ Q_bwe_exc + n2 -14 -16 = //Q_bwe_exc */ +#else + exc4kWhtnd[i] = round_fx( L_shl( L_mult( exc4k_frac[i], scale ), sc ) ); /* Q_bwe_exc+n2-10+16+ Q_bwe_exc + n2 -14 -16 = //Q_bwe_exc */ +#endif + move16(); + } + } + ELSE + { + sc = sub( add( n2, Q_bwe_exc ), 14 ); /* Q_bwe_exc+n2-14*/ + + k = 0; + move16(); + FOR( i = 0; i < 4; i++ ) + { + test(); + IF( igf_flag != 0 && EQ_16( coder_type, VOICED ) ) + { + /*tmp_vfac = 2*voice_factors[i]; + tmp_vfac = min(1, tmp_vfac);*/ +#ifdef BASOP_NOGLOB + tmp_vfac = shl_o( voice_factors[i], 1, &Overflow ); +#else + BASOP_SATURATE_WARNING_OFF_EVS + tmp_vfac = shl( voice_factors[i], 1 ); + BASOP_SATURATE_WARNING_ON_EVS +#endif + } + ELSE + { + tmp_vfac = voice_factors[i]; + move16(); + } + + Ltemp1 = root_a_fx( L_deposit_h( tmp_vfac ), 31, &exp ); +#ifdef BASOP_NOGLOB + temp1 = round_fx_o( L_shl_o( Ltemp1, exp, &Overflow ), &Overflow ); /* Q15 */ +#else + temp1 = round_fx( L_shl( Ltemp1, exp ) ); /* Q15 */ +#endif + L_tmp = Mpy_32_16_1( pow1, sub( 32767, tmp_vfac ) ); /* Q22*/ + Ltemp2 = root_a_over_b_fx( L_tmp, sub( 22, shl( n1, 1 ) ), pow22, sub( 22, shl( n2, 1 ) ), &exp ); +#ifdef BASOP_NOGLOB + temp2 = round_fx_o( L_shl_o( Ltemp2, exp, &Overflow ), &Overflow ); /* Q15 */ +#else + temp2 = round_fx( L_shl( Ltemp2, exp ) ); /* Q15 */ +#endif + FOR( j = 0; j < L_FRAME16k / 16; j++ ) + { +#ifdef BASOP_NOGLOB + L_tmp = L_mult_o( temp1, exc4kWhtnd[k], &Overflow ); /* Q(16+Q_bwe_exc) */ + L_tmp = L_add_o( L_tmp, L_shl_o( L_mult_o( temp2, exc4k_frac[k], &Overflow ), sc, &Overflow ), &Overflow ); /* Q(16+Q_bwe_exc) */ + exc4kWhtnd[k] = round_fx_o( L_tmp, &Overflow ); /* Q_bwe_exc */ +#else + L_tmp = L_mult( temp1, exc4kWhtnd[k] ); /* Q(16+Q_bwe_exc) */ + L_tmp = L_add( L_tmp, L_shl( L_mult( temp2, exc4k_frac[k] ), sc ) ); /* Q(16+Q_bwe_exc) */ + exc4kWhtnd[k] = round_fx( L_tmp ); /* Q_bwe_exc */ +#endif + move16(); + k = add( k, 1 ); + } + } + } + } + + syn_filt_fx( 0, lpc_shb, LPC_SHB_ORDER_WB, exc4kWhtnd, excSHB, L_FRAME16k / 4, state_lpc_syn, 1 ); + + + return; +} + + /*-------------------------------------------------------------------* * GenWBSynth() * diff --git a/lib_dec/TonalComponentDetection_fx.c b/lib_dec/TonalComponentDetection_fx.c index b555eff8e..569a2bffa 100644 --- a/lib_dec/TonalComponentDetection_fx.c +++ b/lib_dec/TonalComponentDetection_fx.c @@ -20,12 +20,15 @@ static void calcPseudoSpec( const Word32 *mdctSpec, const Word16 mdctSpec_exp, const Word16 nSamples, Word16 floorPowerSpectrum, Word32 *powerSpec, Word16 *powerSpec_exp ); static void getEnvelope( const Word16 nSamples, const Word32 *powerSpec, Word16 F0, Word32 *envelope, Word32 *smoothedSpectrum ); static void GetF0( Word16 const nSamples, Word16 const nSamplesCore, Word32 const *const powerSpectrum, Word32 const pitchLag, Word16 *const pOrigF0, Word16 *const pF0 ); +static void ivas_GetF0_fx( Word16 const nSamples, Word16 const nSamplesCore, Word32 const *const powerSpectrum, Word32 const pitchLag, Word16 *const pOrigF0, Word16 *const pF0 ); static void findStrongestHarmonics( const Word16 nSamples, const Word32 *powerSpectrum, const Word16 F0, const Word16 nTotalHarmonics, Word16 *pHarmonicIndexes, Word16 *pnHarmonics ); static void CorrectF0( const Word16 *pHarmonicIndexes, const Word16 nHarmonics, Word16 *pF0 ); +static void ivas_CorrectF0_fx( const Word16 *pHarmonicIndexes, const Word16 nHarmonics, Word16 *pF0 ); static void findCandidates( const Word16 nSamples, const Word32 *MDCTSpectrum, const Word16 MDCTSpectrum_exp, Word16 *thresholdModificationNew, Word16 floorPowerSpectrum ); static void modifyThreshold( Word16 i, Word16 F0, Word16 threshold, Word16 *thresholdModification ); static void modifyThresholds( Word16 F0, Word16 origF0, Word16 *thresholdModification ); static void RefineThresholdsUsingPitch( const Word16 nSamples, const Word16 nSamplesCore, const Word32 powerSpectrum[], const Word32 lastPitchLag, const Word32 currentPitchLag, Word16 *pF0, Word16 *thresholdModification ); +static void ivas_RefineThresholdsUsingPitch_fx( const Word16 nSamples, const Word16 nSamplesCore, const Word32 powerSpectrum[], const Word32 lastPitchLag, const Word32 currentPitchLag, Word16 *pF0, Word16 *thresholdModification ); static void findTonalComponents( Word16 *indexOfTonalPeak, Word16 *lowerIndex, Word16 *upperIndex, Word16 *numIndexes, Word16 nSamples, const Word32 *powerSpectrum, Word16 F0, Word16 *thresholdModification, Word16 element_mode ); /*-------------------------------------------------------------------* @@ -121,7 +124,7 @@ void ivas_DetectTonalComponents_fx( findCandidates( nSamples, pScaledMdctSpectrum, lastMDCTSpect_exp, thresholdModification, floorPowerSpectrum ); /* Refine peak candidates using the pitch information */ - RefineThresholdsUsingPitch( nSamples, nSamplesCore, secondLastPowerSpectrum, lastPitchLag, currentPitchLag, &F0, thresholdModification ); + ivas_RefineThresholdsUsingPitch_fx( nSamples, nSamplesCore, secondLastPowerSpectrum, lastPitchLag, currentPitchLag, &F0, thresholdModification ); /* Find peaks in the second last frame */ findTonalComponents( indexOfTonalPeak, lowerIndex, upperIndex, pNumIndexes, nSamples, secondLastPowerSpectrum, F0, thresholdModification, element_mode ); @@ -593,6 +596,65 @@ static void GetF0( return; } +static void ivas_GetF0_fx( + Word16 /*short*/ const nSamples, /*i - Q0 */ + Word16 /*short*/ const nSamplesCore, /*i - Q0 */ + Word32 /*int*/ const *const powerSpectrum, + /*i - Qx */ /*is justed handed over and given back*/ + Word32 /*int*/ const pitchLag, /*i - Q16*/ + Word16 /*short*/ *const pOrigF0, /*o - Q10*/ + Word16 /*short*/ *const pF0 ) /*o - Q10*/ +{ + Word16 /*short*/ tmpPitchLag; + Word16 /*short*/ rgiStrongHarmonics[MAX_PEAKS_FROM_PITCH]; /*Q0*/ + Word16 /*short*/ nTotalHarmonics, nStrongHarmonics; + Word16 tmp; + + + assert( LAST_HARMONIC_POS_TO_CHECK <= nSamplesCore ); + + /* Use only F0 >= 100 Hz */ + test(); + IF( ( pitchLag > 0 ) && ( LE_16( round_fx( pitchLag ), shr( nSamplesCore, 1 ) ) ) ) + { + + tmpPitchLag /*"halfPitchLag" in FLC - read as Q5 for comparison to halfpitchlag Q4*/ + = round_fx( L_shl( pitchLag, 4 ) ); /*no division by 2, will be done in following division - + furthermore, do a leftshift before rounding, to preserve more accuracy - + will be accommodated also in following division*/ + + /**pF0 = nSamplesCore/tmpPitchLag;*/ + BASOP_Util_Divide_MantExp( nSamplesCore, 0, tmpPitchLag, -( 1 /*division by 2*/ + 4 /*accommodate accuracy-prevention-leftshift*/ ), pF0, &tmp ); /*pF0 is Q15*/ + move16(); + *pF0 = shr_sat( *pF0, sub( 5, tmp ) ); /*Q10 without scalingfactor*/ + move16(); + *pOrigF0 = *pF0; /*Q10*/ + move16(); + tmp = 2 * LAST_HARMONIC_POS_TO_CHECK; + if ( LT_16( nSamples, 2 * LAST_HARMONIC_POS_TO_CHECK ) ) + { + move16(); + tmp = nSamples; + } + BASOP_Util_Divide_MantExp( tmp, 15, *pF0, 5, &nTotalHarmonics, &tmp ); + nTotalHarmonics = shl( nTotalHarmonics, sub( tmp, 15 ) ); + + + /* Get in rgiStrongHarmonics all i for which i*F0 are the strongest harmonics */ + findStrongestHarmonics( nSamples, powerSpectrum, *pF0, nTotalHarmonics, rgiStrongHarmonics, &nStrongHarmonics ); + + ivas_CorrectF0_fx( rgiStrongHarmonics, nStrongHarmonics, pF0 ); + } + ELSE + { + move16(); + move16(); + *pF0 = 0; + *pOrigF0 = 0; + } + return; +} + static void findStrongestHarmonics( const Word16 nSamples, @@ -833,6 +895,159 @@ static void CorrectF0( return; } +static void ivas_CorrectF0_fx( + const Word16 /*short*/ *pHarmonicIndexes, /*I - Q0 */ + const Word16 /*short*/ nHarmonics, /*I - Q0 */ + Word16 /*short*/ *pF0 ) /*I/O - Q10 range: {0}, [4..18) */ +{ + Word16 /*short*/ i; + Word16 /*short*/ F0; + Word16 /*short*/ diff[MAX_PEAKS_FROM_PITCH - 1], sortedDiff[MAX_PEAKS_FROM_PITCH - 1]; /*Q0*/ + Word16 /*short*/ iMostCommonDiff, nMostCommonDiff, nSameDiff, iMult; /*Q0*/ + + Word16 tmp; + + FOR( i = 0; i < MAX_PEAKS_FROM_PITCH - 1; i++ ) + { + diff[i] = 0; + sortedDiff[i] = 0; + move16(); + move16(); + } + + F0 = *pF0; /*Q10*/ + + test(); + IF( F0 > 0 && nHarmonics != 0 ) + { + tmp = sub( nHarmonics, 1 ); + FOR( i = 0; i < tmp; i++ ) + { + diff[i] = sub( pHarmonicIndexes[i + 1], pHarmonicIndexes[i] ); /*Q0*/ + move16(); + sortedDiff[i] = diff[i]; /*Q0*/ + move16(); + } + sort_fx( sortedDiff, 0, sub( nHarmonics, 1 + 1 ) ); + iMostCommonDiff = sortedDiff[0]; /*Q0*/ + move16(); + nSameDiff = 1; + move16(); + i = 1; + move16(); + IF( EQ_16( imult1616( sortedDiff[0], pHarmonicIndexes[0] ), 1 ) ) + { + /* Find how many distances between peaks have length 1 */ + FOR( ; i < tmp; i++ ) + { + if ( EQ_16( sortedDiff[i], 1 ) ) + { + nSameDiff = add( nSameDiff, 1 ); + } + } + } + nMostCommonDiff = nSameDiff; /*Q0*/ + move16(); + + /* If there are at least 3 distances between peaks with length 1 and if the 1st harmonic is in pHarmonicIndexes then keep the original F0 */ + /* Otherwise find the most common distance between peaks */ + IF( LT_16( nSameDiff, 3 ) ) + { + /* Find the most common difference */ + FOR( i = nSameDiff; i < tmp; i++ ) + { + IF( EQ_16( sortedDiff[i], sortedDiff[i - 1] ) ) + { + nSameDiff = add( nSameDiff, 1 ); + } + ELSE + { + IF( GT_16( nSameDiff, nMostCommonDiff ) ) + { + nMostCommonDiff = nSameDiff; /*Q0*/ + move16(); + iMostCommonDiff = sortedDiff[i - 1]; /*Q0*/ + move16(); + } + ELSE + { + test(); + IF( EQ_16( nSameDiff, nMostCommonDiff ) && GT_16( abs_s( sub( iMostCommonDiff, pHarmonicIndexes[0] ) ), abs_s( sub( sortedDiff[i - 1], pHarmonicIndexes[0] ) ) ) ) + { + nMostCommonDiff = nSameDiff; /*Q0*/ + move16(); + iMostCommonDiff = sortedDiff[i - 1]; /*Q0*/ + move16(); + } + } + nSameDiff = 1; + move16(); + } + } + IF( GT_16( nSameDiff, nMostCommonDiff ) ) + { + nMostCommonDiff = nSameDiff; /*Q0*/ + move16(); + iMostCommonDiff = sortedDiff[nHarmonics - 2]; /*Q0*/ + move16(); + } + } + + /* If there are enough peaks at the same distance */ + IF( GE_16( nMostCommonDiff, MAX_PEAKS_FROM_PITCH / 2 ) ) + { + iMult = 1; + move16(); + FOR( i = 0; i < tmp; i++ ) + { + IF( EQ_16( diff[i], iMostCommonDiff ) ) + { + iMult = pHarmonicIndexes[i]; /*Q0*/ + move16(); + BREAK; + } + /* for rare cases of octave mismatch or missing harmonics */ + test(); + test(); + IF( GT_16( sub( nHarmonics, 2 ), i ) && ( EQ_16( diff[i], diff[i + 1] ) ) && ( EQ_16( add( diff[i], diff[i + 1] ), iMostCommonDiff ) ) ) + { + iMult = pHarmonicIndexes[i]; /*Q0*/ + move16(); + BREAK; + } + } + + /* If the real F0 is much higher than the original F0 from the pitch */ + + IF( LE_16( iMult, 3 ) ) + { + /* Use iMostCommonDiff, because the lowest pHarmonicIndexes[i] (which is equal to iMult) may not correspond to the new F0, but to it's multiple */ + F0 = round_fx_sat( L_shl_sat( L_mult( iMostCommonDiff /*Q0*/, F0 /*Q10*/ ), 15 ) ); /*Q10*/ + } + ELSE + { + F0 = 0; + move16(); + } + } + /* Otherwise if there are at least 3 distances between peaks with length 1 and if the 1st harmonic is in pHarmonicIndexes then keep the original F0 */ + /* Otherwise don't use F0 */ + ELSE + { + test(); + if ( ( GT_16( iMostCommonDiff, 1 ) ) || ( LT_16( nMostCommonDiff, 3 ) ) ) + { + /* Not enough peaks at the same distance => don't use the pitch. */ + F0 = 0; + move16(); + } + } + *pF0 = F0; + move16(); + } + return; +} + static void modifyThreshold( Word16 /*short*/ i, /*I - Q0 */ Word16 /*short*/ F0, /*I - Q10*/ @@ -1073,6 +1288,45 @@ static void RefineThresholdsUsingPitch( return; } +static void ivas_RefineThresholdsUsingPitch_fx( + const Word16 nSamples, + const Word16 nSamplesCore, + const Word32 powerSpectrum[], /*Qx*/ + const Word32 lastPitchLag, /*Qx*/ + const Word32 currentPitchLag, /*Qx*/ + Word16 *pF0, /*Q10*/ + Word16 *thresholdModification /*Q10*/ +) +{ + Word16 pitchIsStable; + Word16 origF0; + Word32 L_tmp; + + /*pitchIsStable = (fabs(lastPitchLag-currentPitchLag) < 0.25f);*/ + pitchIsStable = 0; + move16(); + L_tmp = L_abs( L_sub( lastPitchLag, currentPitchLag ) ); + if ( LT_32( L_tmp, 16384l /*0.25f Q16*/ ) ) + { + pitchIsStable = 1; + move16(); + } + + IF( pitchIsStable ) + { + ivas_GetF0_fx( nSamples, + nSamplesCore, + powerSpectrum, lastPitchLag, &origF0, pF0 ); + + modifyThresholds( *pF0, origF0, thresholdModification ); + } + ELSE + { + *pF0 = 0; + move16(); + } + return; +} static void findTonalComponents( Word16 *indexOfTonalPeak, /* OUT Q0*/ Word16 *lowerIndex, /* OUT Q0*/ diff --git a/lib_dec/gs_dec_fx.c b/lib_dec/gs_dec_fx.c index bbf067419..670b19400 100644 --- a/lib_dec/gs_dec_fx.c +++ b/lib_dec/gs_dec_fx.c @@ -1586,7 +1586,11 @@ void gsc_dec_ivas_fx( #endif { pvq_core_dec_fx( st_fx, gsc_sfm_start, gsc_sfm_end, gsc_sfm_size, concat_out, &Q_tmp, bit, nb_subbands, bits_per_bands, NULL, inpulses_fx, imaxpulse_fx, ACELP_CORE ); +#ifdef MSAN_FIX + Scale_sig( concat_out, s_max( gsc_sfm_end[nb_subbands - 1], 0 ), sub( Q_PVQ_OUT, Q_tmp ) ); /* Q_PVQ_OUT */ +#else Scale_sig( concat_out, gsc_sfm_end[nb_subbands - 1], sub( Q_PVQ_OUT, Q_tmp ) ); /* Q_PVQ_OUT */ +#endif } seed_init = 0; move16(); diff --git a/lib_dec/ivas_cpe_dec_fx.c b/lib_dec/ivas_cpe_dec_fx.c index d8ebe8d4b..8eea5b209 100644 --- a/lib_dec/ivas_cpe_dec_fx.c +++ b/lib_dec/ivas_cpe_dec_fx.c @@ -346,6 +346,10 @@ ivas_error ivas_cpe_dec_fx( { sts[0]->total_brate = hCPE->element_brate; /* Only mono downmix was transmitted in this case */ move32(); +#ifdef MSAN_FIX + hCPE->hStereoDft->frame_sid_nodata = 0; + move16(); +#endif } ELSE { diff --git a/lib_dec/ivas_mdct_core_dec.c b/lib_dec/ivas_mdct_core_dec.c index b80bff932..fd71a6c30 100644 --- a/lib_dec/ivas_mdct_core_dec.c +++ b/lib_dec/ivas_mdct_core_dec.c @@ -1504,6 +1504,9 @@ void ivas_mdct_core_tns_ns_fx( move16(); set32_fx( xn_buf_fx, 0, L_MDCT_OVLP_MAX + L_FRAME_PLUS + L_MDCT_OVLP_MAX ); +#ifdef MSAN_FIX + set32_fx( sns_int_scf_fx, 0, FDNS_NPTS ); +#endif /* TNS, ITF, IMDCT and updates */ diff --git a/lib_dec/ivas_post_proc.c b/lib_dec/ivas_post_proc.c index ce361c894..b961b6dbb 100644 --- a/lib_dec/ivas_post_proc.c +++ b/lib_dec/ivas_post_proc.c @@ -296,7 +296,11 @@ void stereo_dft_dec_core_switching_fx( { Word16 mem_len = NS2SA_FX2( L_mult0( L_frameTCX, FRAMES_PER_SEC ), STEREO_DFT32MS_OVL_NS ); /*Q0*/ move16(); +#ifdef MSAN_FIX + Word32 mem_len_inv = mem_len_inv_tbl[( mem_len / 25 ) - 1]; +#else Word32 mem_len_inv = mem_len_inv_tbl[( mem_len / 25 )]; +#endif move32(); Word16 qmem_len = norm_l( mem_len ); Word32 mem_len_fx = L_shl( mem_len, qmem_len ); diff --git a/lib_enc/igf_enc.c b/lib_enc/igf_enc.c index 9ca1cd5fc..6b9077129 100644 --- a/lib_enc/igf_enc.c +++ b/lib_enc/igf_enc.c @@ -2590,6 +2590,11 @@ void IGFEncApplyMono_ivas_fx( move16(); Word32 common_pPowerSpectrum_fx[N_MAX + L_MDCT_OVLP_MAX]; + +#ifdef MSAN_FIX + set32_fx( common_pPowerSpectrum_fx, 0, N_MAX + L_MDCT_OVLP_MAX ); +#endif + Word16 common_pPowerSpectrum_exp = MIN16B; move16(); test(); diff --git a/lib_enc/ivas_cpe_enc.c b/lib_enc/ivas_cpe_enc.c index 4157ae142..33ffac914 100644 --- a/lib_enc/ivas_cpe_enc.c +++ b/lib_enc/ivas_cpe_enc.c @@ -80,6 +80,9 @@ ivas_error ivas_cpe_enc_fx( Word16 Q_new[CPE_CHANNELS] = { 0 }; Word16 fft_buff_fx[CPE_CHANNELS][2 * L_FFT]; /* FFT buffer */ Word16 fft_buff_fx_q[CPE_CHANNELS]; /* FFT buffer */ +#ifdef MSAN_FIX + set16_fx( fft_buff_fx_q, 0, CPE_CHANNELS ); +#endif Word16 fft_buff_fx_final_q = MAX_16; move16(); Word32 ener_fx[CPE_CHANNELS]; /* residual energy from Levinson-Durbin Q6 */ diff --git a/lib_enc/ivas_stereo_dft_enc.c b/lib_enc/ivas_stereo_dft_enc.c index 119dfc313..a7a3d1bbe 100644 --- a/lib_enc/ivas_stereo_dft_enc.c +++ b/lib_enc/ivas_stereo_dft_enc.c @@ -1862,8 +1862,28 @@ void stereo_dft_enc_process_fx( IF( pgIpd[0] != 0 ) { - c_fx = shl_sat( getCosWord16( extract_l( pgIpd[0] ) ), 1 ); // Q15 saturation expected - s_fx = getSinWord16( extract_l( pgIpd[0] ) ); // Q15 + Word32 theta = pgIpd[0]; + WHILE( GT_32( theta, EVS_2PI_FX ) ) + { + theta = L_sub( theta, EVS_2PI_FX ); /* Q13 */ + } + WHILE( LT_32( theta, -EVS_2PI_FX ) ) + { + theta = L_add( theta, EVS_2PI_FX ); /* Q13 */ + } + + IF( GT_32( theta, MAX_16 ) ) + { + theta = L_sub( theta, EVS_2PI_FX ); /* Q13 */ + } + ELSE IF( LT_32( theta, MIN_16 ) ) + { + theta = L_add( theta, EVS_2PI_FX ); /* Q13 */ + } + + c_fx = shl_sat( getCosWord16( extract_l( theta ) ), 1 ); + s_fx = getSinWord16( extract_l( theta ) ); + FOR( i = hStereoDft->band_limits_dmx[b]; i < hStereoDft->band_limits_dmx[b + 1]; i++ ) { /*rotate L*/ diff --git a/lib_enc/swb_tbe_enc_fx.c b/lib_enc/swb_tbe_enc_fx.c index 3dfb552e6..95974a08a 100644 --- a/lib_enc/swb_tbe_enc_fx.c +++ b/lib_enc/swb_tbe_enc_fx.c @@ -1614,10 +1614,10 @@ void wb_tbe_enc_ivas_fx( Copy( hBWE_TD->state_syn_shbexc_fx, shaped_wb_excitation, L_SHB_LAHEAD / 4 ); Q_bwe_exc_ext = sub( Q_bwe_exc, 16 ); - GenShapedWBExcitation_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx, - hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, - hBWE_TD->state_lpc_syn_fx, st_fx->coder_type, bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, - vf_modified_fx, uv_flag, st_fx->igf ); + GenShapedWBExcitation_ivas_enc_fx( shaped_wb_excitation + L_SHB_LAHEAD / 4, lpc_wb, exc4kWhtnd, hBWE_TD->mem_csfilt_fx, + hBWE_TD->mem_genSHBexc_filt_down_shb_fx, hBWE_TD->mem_genSHBexc_filt_down_wb2_fx, hBWE_TD->mem_genSHBexc_filt_down_wb3_fx, + hBWE_TD->state_lpc_syn_fx, st_fx->coder_type, bwe_exc_extended_16, Q_bwe_exc_ext, hBWE_TD->bwe_seed, + vf_modified_fx, uv_flag, st_fx->igf ); curr_pow = 0; move32(); @@ -5834,7 +5834,7 @@ static void QuantizeSHBframegain_ivas_fx( 1 << NUM_BITS_SHB_FrameGain_LBR_WB, &idxFrameGain, &Q_GainFrame, SHBCB_FrameGain16_fx ); test(); - IF( GT_32( Q_GainFrame, L_shl( Mult_32_16( *GainFrame, 17367 ), 1 ) ) && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */ + IF( GT_64( W_deposit32_l( Q_GainFrame ), W_shl( W_mult_32_16( *GainFrame, 17367 ), 15 ) ) && idxFrameGain > 0 ) /* 1.06 = +0.5 dB */ { idxFrameGain--; Q_GainFrame = L_add( SHBCB_FrameGain16_fx[idxFrameGain], 0 ); /* Q18 */ -- GitLab