From dec31eaf27e965a0dae0085ed9523a84c3c19e54 Mon Sep 17 00:00:00 2001 From: Anjaneyulu Sana Date: Mon, 17 Feb 2025 22:25:03 +0530 Subject: [PATCH] Partial fix for 3GPP issue 1129, ivas function for tcx_arith_encode_envelope_fx and revamped the function IGF_ErodeSpectrum_ivas_fx --- lib_enc/arith_coder_enc_fx.c | 131 ++++++++++++++++++++++++++++++++ lib_enc/cod_tcx_fx.c | 11 ++- lib_enc/igf_enc.c | 6 +- lib_enc/igf_enc_fx.c | 142 +++++++++++++++-------------------- lib_enc/prot_fx_enc.h | 21 +++++- 5 files changed, 223 insertions(+), 88 deletions(-) diff --git a/lib_enc/arith_coder_enc_fx.c b/lib_enc/arith_coder_enc_fx.c index b97fd7931..98a5173f5 100644 --- a/lib_enc/arith_coder_enc_fx.c +++ b/lib_enc/arith_coder_enc_fx.c @@ -792,3 +792,134 @@ void tcx_arith_encode_envelope_fx( *nf_seed = extract_l( L_tmp2 ); move16(); } + +void tcx_arith_encode_envelope_ivas_fx( + Word32 spectrum[], /* i/o: MDCT coefficients Q31-e */ + Word16 *spectrum_e, /* i/o: MDCT exponent Q0 */ + Word16 signs[], /* o: signs (spectrum[.]<0) Q0 */ + const Word16 L_frame, /* i: frame or MDCT length Q0 */ + const Word16 L_spec, /* i: frame or MDCT length Q0 */ + Encoder_State *st, /* i/o: coder state */ + const Word16 A_ind[], /* i: quantised LPC coefficients Q12 */ + Word16 target_bits, /* i: number of available bits Q0 */ + Word16 prm[], /* o: bitstream parameters Q0 */ + const Word8 use_hm, /* i: use HM in current frame? */ + Word16 prm_hm[], /* o: HM parameter area Q0 */ + const Word16 tcxltp_pitch, /* i: TCX LTP pitch in FD, -1 if n/a Q0*/ + Word16 *arith_bits, /* o: bits used for ari. coding Q0 */ + Word16 *signaling_bits, /* o: bits used for signaling Q0 */ + const Word16 low_complexity /* i: low-complexity flag Q0 */ +) +{ + Word32 env[N_MAX_ARI]; /* unscaled envelope (Q16) */ + Word16 *envelope; /* scaled envelope (Q15-e) */ + Word16 envelope_e; + Word16 exponents[N_MAX_ARI]; /* Q15 */ + Word16 L_spec_core; + Word16 *q_spectrum; + TCX_CONFIG_HANDLE hTcxCfg; + Word16 scale, scale_e; + Word16 k, kMax; + Word16 deadzone; + const Word8 *deadzone_flags; + Word16 gamma_w, gamma_uw; + Word16 hm_bits; + Word32 L_tmp; + Word16 tmp; + TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc; + + assert( L_spec <= N_MAX_ARI ); + + hTcxCfg = st->hTcxCfg; + deadzone = hTcxCfg->sq_rounding; + move16(); + deadzone_flags = hTcxEnc->memQuantZeros; + *signaling_bits = 0; + move16(); + + assert( st->enableTcxLpc ); + gamma_w = 32767 /*1.0f Q15*/; + move16(); + gamma_uw = st->inv_gamma; + move16(); + + tcx_arith_render_envelope( A_ind, L_frame, L_spec, hTcxCfg->preemph_fac, gamma_w, gamma_uw, env ); + + FOR( k = 0; k < L_spec; k++ ) + { + signs[k] = extract_l( L_lshr( spectrum[k], 31 ) ); + move16(); + if ( spectrum[k] < 0 ) + { + spectrum[k] = L_abs( spectrum[k] ); + move32(); + } + } + + IF( use_hm != 0 ) + { + tcx_hm_analyse_fx( spectrum, spectrum_e, L_spec, env, target_bits, hTcxCfg->coder_type, prm_hm, tcxltp_pitch, hTcxEnc->tcxltp_gain, &hm_bits ); + + target_bits = sub( target_bits, hm_bits ); + *signaling_bits = add( *signaling_bits, hm_bits ); + move16(); + } + ELSE + { + prm_hm[0] = 0; /* just to be sure */ + move16(); + hm_bits = 0; + move16(); + } + + L_spec_core = L_spec; + move16(); + if ( st->igf ) + { + L_spec_core = s_min( L_spec_core, st->hIGFEnc->infoStartLine ); + } + envelope = (Word16 *) env; + + tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e ); + + tmp = sub( envelope_e, 1 + 15 ); + FOR( k = 0; k < L_spec; k++ ) + { + exponents[k] = round_fx( expfp( envelope[k], tmp ) ); + move16(); + } + + scale = tcx_arith_rateloop( spectrum, *spectrum_e, L_spec, envelope, envelope_e, exponents, target_bits, deadzone, deadzone_flags, &( hTcxEnc->tcx_target_bits_fac ), &scale_e ); + + /* Final quantization */ + kMax = tcx_arith_find_kMax( spectrum, *spectrum_e, L_spec, scale, scale_e, deadzone, deadzone_flags ); + + q_spectrum = (Word16 *) env; /* Reuse buffer */ + + L_tmp = L_mult( deadzone, 1 ); /* Q16 */ + tmp = add( sub( *spectrum_e, 15 ), scale_e ); + FOR( k = 0; k <= kMax; k++ ) + { + /* quantise using dead-zone */ + q_spectrum[k] = extract_h( L_add( L_shl( Mpy_32_16_1( spectrum[k], scale ), tmp ), L_tmp ) ); + move16(); + } + + /* Final encoding */ + *arith_bits = tcx_arith_encode( q_spectrum, signs, kMax, L_spec, exponents, target_bits, prm ); + move16(); + + /* Multiply back the signs */ + FOR( k = 0; k <= kMax; k++ ) + { + if ( signs[k] != 0 ) + L_tmp = L_mult( q_spectrum[k], -( 1 << ( 30 - SPEC_EXP_DEC ) ) ); + if ( signs[k] == 0 ) + L_tmp = L_mult( q_spectrum[k], 1 << ( 30 - SPEC_EXP_DEC ) ); + spectrum[k] = L_tmp; + move32(); + } + *spectrum_e = SPEC_EXP_DEC; + move16(); + set32_fx( spectrum + k, 0, sub( s_max( L_frame, L_spec ), k ) ); +} diff --git a/lib_enc/cod_tcx_fx.c b/lib_enc/cod_tcx_fx.c index ebec48fa7..94b030f5a 100644 --- a/lib_enc/cod_tcx_fx.c +++ b/lib_enc/cod_tcx_fx.c @@ -4192,13 +4192,22 @@ void QuantizeTCXSpectrum_fx( low_complexiety = 1; move16(); } - tcx_arith_encode_envelope_fx( spectrum_fx, spectrum_e, hm_cfg->indexBuffer, L_frame, L_spec, st, Aqind, sqTargetBits, sqQ, tmp8, prm_hm, /* HM parameter area */ LtpPitchLag, &sqBits, &signaling_bits, nf_seed, low_complexiety ); + tcx_arith_encode_envelope_ivas_fx( spectrum_fx, spectrum_e, hm_cfg->indexBuffer, L_frame, L_spec, st, Aqind, sqTargetBits, sqQ, tmp8, prm_hm, /* HM parameter area */ LtpPitchLag, &sqBits, &signaling_bits, low_complexiety ); sqTargetBits = sub( sqTargetBits, signaling_bits ); *prm_target = sqTargetBits; move16(); /* Noise filling seed */ + Word64 seed = 0; + move64(); + FOR( i = 0; i < noiseFillingBorder; ++i ) + { + /* *nf_seed += (int16_t) ( abs( (int16_t) spectrum[i] ) * i * 2 ); */ + seed = W_mac_32_32( seed, L_abs( spectrum_fx[i] ), i ); // exp: spectrum_e + } + *nf_seed = extract_l( W_extract_l( W_shr( seed, sub( 31, *spectrum_e ) ) ) ); // Q0 + move16(); } *hm_active = prm_hm[0]; diff --git a/lib_enc/igf_enc.c b/lib_enc/igf_enc.c index a85843be2..d20169e73 100644 --- a/lib_enc/igf_enc.c +++ b/lib_enc/igf_enc.c @@ -2593,7 +2593,6 @@ void IGFEncApplyMono_ivas_fx( Word16 *pPowerSpectrumParameter_exp; Word16 att_fx = MAX16B; Word16 last_core_acelp; - Word16 highPassEner_exp; move16(); Word32 common_pPowerSpectrum_fx[N_MAX + L_MDCT_OVLP_MAX]; @@ -2670,7 +2669,7 @@ void IGFEncApplyMono_ivas_fx( move16(); } } - IGF_ErodeSpectrum_ivas_fx( &highPassEner_exp, st->hIGFEnc, pMDCTSpectrum_fx, pPowerSpectrumParameter_fx, common_pPowerSpectrum_exp, igfGridIdx, 0 ); + IGF_ErodeSpectrum_ivas_fx( st->hIGFEnc, pMDCTSpectrum_fx, pPowerSpectrumParameter_fx, common_pPowerSpectrum_exp, igfGridIdx, 0 ); } @@ -2694,7 +2693,6 @@ void IGFEncApplyStereo_fx( const Word32 element_brate, /* i : element bitrate */ const Word16 mct_on ) { - Word16 highPassEner_exp; Word32 *pPowerSpectrumParameter_fx[NB_DIV]; /* If it is NULL it informs a function that specific handling is needed */ Word32 *pPowerSpectrumParameterMsInv_fx[NB_DIV]; Word16 coreMsMask[N_MAX]; @@ -2768,7 +2766,7 @@ void IGFEncApplyStereo_fx( IGF_Whitening_ivas_fx( hIGFEnc[ch], pPowerSpectrumParameter_fx[ch], &exp_pPowerSpectrum[0], igfGridIdx, sts[ch]->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, ( sts[0]->hTcxEnc->fUseTns[frameno] || sts[1]->hTcxEnc->fUseTns[frameno] ), sp_aud_decision0, element_brate, sts[ch]->element_mode ); - IGF_ErodeSpectrum_ivas_fx( &highPassEner_exp, hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], pPowerSpectrumParameter_fx[ch], sts[ch]->hTcxEnc->spectrum_e[frameno], igfGridIdx, mct_on ); + IGF_ErodeSpectrum_ivas_fx( hIGFEnc[ch], sts[ch]->hTcxEnc->spectrum_fx[frameno], pPowerSpectrumParameter_fx[ch], sts[ch]->hTcxEnc->spectrum_e[frameno], igfGridIdx, mct_on ); } return; } diff --git a/lib_enc/igf_enc_fx.c b/lib_enc/igf_enc_fx.c index 268166ba9..ca1759c89 100644 --- a/lib_enc/igf_enc_fx.c +++ b/lib_enc/igf_enc_fx.c @@ -525,8 +525,7 @@ void IGF_ErodeSpectrum( Word16 *highPassEner_exp, /**< ou } } -void IGF_ErodeSpectrum_ivas_fx( Word16 *highPassEner_exp, /**< out: | exponent of highPassEner */ - const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in: | instance handle of IGF Encoder */ +void IGF_ErodeSpectrum_ivas_fx( const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in: | instance handle of IGF Encoder */ Word32 *pSpectrum, /**< in/out: | MDCT spectrum Qx*/ Word32 *pPowerSpectrum, /**< in/out: | power spectrum */ Word16 pPowerSpectrum_exp, /**< in: | exponent of power spectrum */ @@ -541,26 +540,17 @@ void IGF_ErodeSpectrum_ivas_fx( Word16 *highPassEner_exp, /**< ou Word32 highPassEner; /* Q31 */ Word32 lastLine; Word32 nextLine; - Word32 L_c; - Word32 highPassEner_Ovfl; - Word16 s; - Word16 tmploop; Word16 *swb_offset; Word16 sfb; Word16 startSfb; Word16 stopSfb; Word16 line; - Word16 flag; Word16 *igfScaleF; Word16 tmp; - Word32 L_tmp; - -#ifdef BASOP_NOGLOB_DECLARE_LOCAL - Flag Overflow = 0; - Flag Carry = 0; - move32(); - move32(); -#endif + Word16 factor; + Word16 exp1, exp2; + Word16 num, den; + Word32 temp; hPrivateData = &hInstance->igfData; hGrid = &hPrivateData->igfInfo.grid[igfGridIdx]; @@ -576,10 +566,6 @@ void IGF_ErodeSpectrum_ivas_fx( Word16 *highPassEner_exp, /**< ou move16(); igfScaleF = hPrivateData->igfScfQuantized; move16(); - *highPassEner_exp = 0; - move16(); - highPassEner = 0; - move32(); IF( NULL == pPowerSpectrum ) { @@ -593,90 +579,82 @@ void IGF_ErodeSpectrum_ivas_fx( Word16 *highPassEner_exp, /**< ou IF( igfBgn > 0 ) { - L_c = 0; - move32(); + Word64 sum = 0; + move64(); FOR( i = 0; i < igfBgn; i++ ) { - Carry = 0; - highPassEner = L_add_co( highPassEner, Mpy_32_16_1( pPowerSpectrum[i], shl( i, 4 ) /*Q4*/ ) /*Q20, pPowerSpectrum_exp*/, &Carry, &Overflow ); - Overflow = 0; - L_c = L_macNs_co( L_c, 0, 0, &Carry, &Overflow ); + sum = W_mac_32_16( sum, pPowerSpectrum[i], i ); // Q: 31-pPowerSpectrum_exp+1 } + exp1 = W_norm( sum ); + sum = W_shl( sum, sub( exp1, 1 ) ); // Q: 31-pPowerSpectrum_exp+1+exp1-1 + num = extract_h( W_extract_h( sum ) ); // Q: 31-pPowerSpectrum_exp+exp1-48 = -pPowerSpectrum_exp+exp1-17 + exp1 = add( 32, sub( pPowerSpectrum_exp, exp1 ) ); // exp: 32+pPowerSpectrum_exp-exp1 - highPassEner = norm_llQ31( L_c, highPassEner, highPassEner_exp ); /*Q20, highPassEner_exp*/ - *highPassEner_exp = add( *highPassEner_exp, pPowerSpectrum_exp ); - test(); - test(); - test(); - test(); - test(); IF( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_9600 ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_RF_SWB_13200 ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_13200 ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_16400_CPE ) ) { - igfBgn = shl( igfBgn, 0 ); + factor = ONE_IN_Q14; // Q14 + move16(); } ELSE IF( mct_on && ( EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_48000_CPE ) || EQ_16( hPrivateData->igfInfo.bitRateIndex, IGF_BITRATE_SWB_64000_CPE ) ) ) { - igfBgn = imult1616( igfBgn, 45 /*0.7.Q6*/ ); - igfBgn = shr( igfBgn, 7 ); + factor = 11469; // 0.7f in Q14 + move16(); } ELSE { - igfBgn = shl( igfBgn, 1 ); + factor = 32767; // 2.f in Q14 + move16(); } - highPassEner = L_deposit_l( BASOP_Util_Divide3216_Scale( highPassEner /*Q20, highPassEner_exp*/, igfBgn /*Q0*/, &s ) ); /*Q15, highPassEner_exp+11-16+s*/ - *highPassEner_exp = add( add( *highPassEner_exp, s ), 12 - 16 + ( 31 - 15 ) ); /*Q15->Q31,highPassEner_exp*/ - lastLine = pSpectrum[i - 1]; + + temp = L_mult( igfBgn, factor ); // exp: 16 + exp2 = norm_l( temp ); + den = extract_h( L_shl( temp, exp2 ) ); // exp: 16-exp2 + exp2 = sub( 16, exp2 ); + + highPassEner = L_deposit_h( div_s( num, den ) ); // exp: exp1-exp2 + + /* highPassEner is used only for comparison, saturation doesn't effect the outcome */ + highPassEner = L_shl_sat( highPassEner, sub( sub( exp1, exp2 ), pPowerSpectrum_exp ) ); // exp: pPowerSpectrum_exp + + lastLine = pSpectrum[i - 1]; // Qx move32(); - nextLine = 0; + nextLine = pSpectrum[i]; // Qx move32(); - /* May overflow - just for threshold comparison */ - /* negate because the negated may be 1 larger in abs, */ - /* so whenever compared to the negation of a maximum possible pPowerspectrum, it is still larger */ - highPassEner_Ovfl = L_shl_o( L_negate( highPassEner ), sub( *highPassEner_exp, pPowerSpectrum_exp ), &Overflow ); - L_tmp = L_add_o( pPowerSpectrum[i - 1], highPassEner_Ovfl, &Overflow ); - - if ( L_tmp >= 0 ) + if ( LT_32( pPowerSpectrum[i - 1], highPassEner ) ) { - nextLine = pSpectrum[i]; + nextLine = 0; move32(); } - tmploop = sub( igfEnd, 1 ); - FOR( /*i*/; i < tmploop; i++ ) + + FOR( /*i*/; i < igfEnd - 1; i++ ) { /* May overflow - just for threshold comparison */ - BASOP_SATURATE_WARNING_OFF_EVS - L_tmp = L_add_sat( pPowerSpectrum[i], highPassEner_Ovfl ); - BASOP_SATURATE_WARNING_ON_EVS; - - IF( L_tmp < 0 ) + IF( LT_32( pPowerSpectrum[i], highPassEner ) ) { - lastLine = pSpectrum[i]; + lastLine = pSpectrum[i]; // Qx move32(); - pSpectrum[i] = nextLine; + pSpectrum[i] = nextLine; // Qx move32(); nextLine = 0; move32(); } ELSE { - pSpectrum[i - 1] = lastLine; + pSpectrum[i - 1] = lastLine; // Qx move32(); - lastLine = pSpectrum[i]; + lastLine = pSpectrum[i]; // Qx move32(); - nextLine = pSpectrum[i + 1]; + nextLine = pSpectrum[i + 1]; // Qx move32(); } } /* May overflow - just for threshold comparison */ - BASOP_SATURATE_WARNING_OFF_EVS - L_tmp = L_add_sat( pPowerSpectrum[i], highPassEner_Ovfl ); - BASOP_SATURATE_WARNING_ON_EVS - if ( L_tmp < 0 ) + if ( LT_32( pPowerSpectrum[i], highPassEner ) ) { pSpectrum[i] = 0; move32(); @@ -692,27 +670,29 @@ void IGF_ErodeSpectrum_ivas_fx( Word16 *highPassEner_exp, /**< ou move32(); } - FOR( sfb = startSfb; sfb < stopSfb; sfb++ ) + // Below check is present at the beginning of the function and is not required here + /* IF( NULL != pPowerSpectrum ) */ { - flag = 0; - move16(); - FOR( line = swb_offset[sfb]; line < swb_offset[sfb + 1]; line++ ) + FOR( sfb = startSfb; sfb < stopSfb; sfb++ ) { - if ( pSpectrum[line] != 0 ) + tmp = 0; + move16(); + FOR( line = swb_offset[sfb]; line < swb_offset[sfb + 1]; line++ ) { - flag = 1; - move16(); + if ( pSpectrum[line] != 0 ) + { + tmp = add( tmp, 1 ); + } } - } - tmp = igfScaleF[sfb]; - move16(); - IF( flag ) - { - tmp = sub( igfScaleF[sfb], 1 ); - } - if ( igfScaleF[sfb] ) - { - igfScaleF[sfb] = tmp; + + Word16 igfScaleF_cnt = igfScaleF[sfb]; + move16(); + test(); + if ( tmp && igfScaleF[sfb] ) + { + igfScaleF_cnt = sub( igfScaleF[sfb], 1 ); + } + igfScaleF[sfb] = igfScaleF_cnt; move16(); } } diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 280a76c94..711d7069a 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -2432,6 +2432,24 @@ void tcx_arith_encode_envelope_fx( const Word16 low_complexity /* i: low-complexity flag Q0 */ ); +void tcx_arith_encode_envelope_ivas_fx( + Word32 spectrum[], /* i/o: MDCT coefficients Q31-e */ + Word16 *spectrum_e, /* i/o: MDCT exponent Q0 */ + Word16 signs[], /* o: signs (spectrum[.]<0) Q0 */ + const Word16 L_frame, /* i: frame or MDCT length Q0 */ + const Word16 L_spec, /* i: frame or MDCT length Q0 */ + Encoder_State *st, /* i/o: coder state */ + const Word16 A_ind[], /* i: quantised LPC coefficients Q12 */ + Word16 target_bits, /* i: number of available bits Q0 */ + Word16 prm[], /* o: bitstream parameters Q0 */ + const Word8 use_hm, /* i: use HM in current frame? */ + Word16 prm_hm[], /* o: HM parameter area Q0 */ + const Word16 tcxltp_pitch, /* i: TCX LTP pitch in FD, -1 if n/a Q0*/ + Word16 *arith_bits, /* o: bits used for ari. coding Q0 */ + Word16 *signaling_bits, /* o: bits used for signaling Q0 */ + const Word16 low_complexity /* i: low-complexity flag Q0 */ +); + /** Quantize gain. * Quantize gain in range [0..127], * @param n Length of the spectrum. @@ -3050,8 +3068,7 @@ void IGF_ErodeSpectrum( Word16 *highPassEner_exp, /**< out: | const Word16 igfGridIdx /**< in: Q0 | IGF grid index */ ); -void IGF_ErodeSpectrum_ivas_fx( Word16 *highPassEner_exp, /**< out: | exponent of highPassEner */ - const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in: | instance handle of IGF Encoder */ +void IGF_ErodeSpectrum_ivas_fx( const IGF_ENC_INSTANCE_HANDLE hInstance, /**< in: | instance handle of IGF Encoder */ Word32 *pSpectrum, /**< in/out: | MDCT spectrum */ Word32 *pPowerSpectrum, /**< in/out: | power spectrum */ Word16 pPowerSpectrum_exp, /**< in: | exponent of power spectrum */ -- GitLab