From b87e5972bcb40aa5bb52e9bea3c9c408a94391dd Mon Sep 17 00:00:00 2001 From: vaclav Date: Fri, 16 Jan 2026 15:23:20 +0100 Subject: [PATCH 01/11] fix HARM_NON_LINEARITY --- lib_com/options.h | 1 + lib_com/prot_fx.h | 19 ++++++++-------- lib_com/swb_tbe_com_fx.c | 43 +++++++++++++++++++++++-------------- lib_dec/acelp_core_dec_fx.c | 4 ++++ lib_enc/acelp_core_enc_fx.c | 4 ++++ 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/lib_com/options.h b/lib_com/options.h index ffac0ff4e..49b6d9802 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -101,6 +101,7 @@ #define HARM_CORECODER_UPDT /* VA: basop issue 2342: Remove duplicated code in core-coder common update functions */ #define HARM_CORE_SW /* VA: basop issue 2314: Remove duplicated code in the core switching */ #define HARM_PREPROC /* VA: basop issue 2339: Remove duplicated code in the core-coder DTX */ +#define HARM_NON_LINEARITY /* VA: basop issue 2345: Remove duplicated code in core-coder: non_linearity_fx() */ /* #################### End BE switches ################################## */ diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 9c207b71e..8076cd1ea 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -3165,16 +3165,17 @@ void ScaleShapedWB_fx( ); void non_linearity_fx( - const Word16 i[], /* i : i signal Q_inp */ - Word32 output[], /* o : output signal 2*Q_inp */ - const Word16 length, /* i : i length */ - Word32 *prev_scale, /* i/o: memory Q30 */ - Word16 Q_inp, - Word16 coder_type, /* i : Coder Type */ - Word16 *voice_factors, /* i : Voice Factors */ - const Word16 L_frame /* i : ACELP frame length */ + const Word16 i[], /* i : i signal Q_inp */ + Word32 output[], /* o : output signal 2*Q_inp */ + const Word16 length, /* i : i length */ + Word32 *prev_scale, /* i/o: memory Q30 */ + const Word16 Q_inp, /* i : scaling of input */ + const Word16 coder_type, /* i : Coder Type */ + Word16 *voice_factors, /* i : Voice Factors */ + const Word16 L_frame /* i : ACELP frame length */ ); +#ifndef HARM_NON_LINEARITY void non_linearity_ivas_fx( const Word16 i[], /* i : i signal Q_inp */ Word32 output[], /* o : output signal 2*Q_inp */ @@ -3185,7 +3186,7 @@ void non_linearity_ivas_fx( Word16 *voice_factors, /* i : Voice Factors */ const Word16 L_frame /* i : ACELP frame length */ ); - +#endif void interp_code_5over2_fx( const Word16 inp_code[], /* i : i vector */ Word16 interp_code[], /* o : output vector */ diff --git a/lib_com/swb_tbe_com_fx.c b/lib_com/swb_tbe_com_fx.c index d65434759..0ad5eff5d 100644 --- a/lib_com/swb_tbe_com_fx.c +++ b/lib_com/swb_tbe_com_fx.c @@ -5327,6 +5327,7 @@ void ScaleShapedWB_fx( return; } +#ifndef HARM_NON_LINEARITY /*-------------------------------------------------------------------* * non_linearity() * @@ -5399,7 +5400,7 @@ static Word32 non_linearity_scaled_copy( } return prev_scale; } - +#endif /*-------------------------------------------------------------------* * non_linearity() @@ -5407,9 +5408,13 @@ static Word32 non_linearity_scaled_copy( * Apply a non linearity to the SHB excitation * -------------------------------------------------------------------*/ +#ifdef HARM_NON_LINEARITY +static Word32 non_linearity_scaled_copy( +#else static Word32 non_linearity_scaled_copy_ivas( +#endif const Word16 input[], - Word16 j, + const Word16 j, const Word16 length, Word32 output[], Word32 prev_scale, @@ -5419,7 +5424,6 @@ static Word32 non_linearity_scaled_copy_ivas( Word16 i; Word32 L_tmp; - IF( en_abs ) { FOR( i = 0; i < j; i++ ) @@ -5507,6 +5511,7 @@ static Word32 non_linearity_scaled_copy_ivas( move32(); } } + return prev_scale; } @@ -5533,16 +5538,16 @@ static Word32 non_linearity_scaled_copy_ivas( /*==========================================================================*/ void non_linearity_fx( - const Word16 input[], /* i : input signal Q_inp */ - Word32 output[], /* o : output signal 2*Q_inp */ - const Word16 length, /* i : input length */ - Word32 *pPrevScale, /* i/o: memory Q30 */ - Word16 Q_inp, - Word16 coder_type, /* i : Coder Type */ - Word16 *voice_factors, /* i : Voice Factors */ - const Word16 L_frame /* i : ACELP frame length */ - + const Word16 input[], /* i : input signal Q_inp */ + Word32 output[], /* o : output signal 2*Q_inp */ + const Word16 length, /* i : input length */ + Word32 *pPrevScale, /* i/o: memory Q30 */ + const Word16 Q_inp, /* i : scaling of input */ + const Word16 coder_type, /* i : Coder Type */ + Word16 *voice_factors, /* i : Voice Factors */ + const Word16 L_frame /* i : ACELP frame length */ ) +#ifndef HARM_NON_LINEARITY { Word16 i, j; Word16 max_val = 0; @@ -5783,6 +5788,7 @@ void non_linearity_ivas_fx( const Word16 L_frame /* i : ACELP frame length */ ) +#endif { Word16 i, j; Word16 max_val = 0; @@ -5820,7 +5826,6 @@ void non_linearity_ivas_fx( move16(); /* 0.94*4 in Q12 */ } - FOR( i = 0; i < nframes; i++ ) { v_fac = add( v_fac, shr( voice_factors[i], 3 ) ); /* Q12 */ @@ -5837,7 +5842,6 @@ void non_linearity_ivas_fx( prev_scale = *pPrevScale; move32(); - /* Delay Alignment in FX is done inside swb_tbe_enc_fx() */ FOR( i = j = 0; i < length_half; i++ ) @@ -5851,7 +5855,6 @@ void non_linearity_ivas_fx( max_val = s_max( max_val, tmp ); } - IF( GT_16( max_val, shl_sat( 1, Q_inp ) ) ) { exp = norm_s( max_val ); @@ -5866,6 +5869,7 @@ void non_linearity_ivas_fx( sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position */ sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ + test(); IF( prev_scale <= 0 || GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ { @@ -5875,7 +5879,6 @@ void non_linearity_ivas_fx( } ELSE { - /* Computing log2(scale) */ IF( j == 0 ) { @@ -5912,7 +5915,11 @@ void non_linearity_ivas_fx( } } +#ifdef HARM_NON_LINEARITY + prev_scale = non_linearity_scaled_copy( input, j, length_half, output, prev_scale, scale_step, en_abs ); +#else prev_scale = non_linearity_scaled_copy_ivas( input, j, length_half, output, prev_scale, scale_step, en_abs ); +#endif max_val = 0; move16(); @@ -5989,7 +5996,11 @@ void non_linearity_ivas_fx( } } +#ifdef HARM_NON_LINEARITY + prev_scale = non_linearity_scaled_copy( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs ); +#else prev_scale = non_linearity_scaled_copy_ivas( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs ); +#endif *pPrevScale = prev_scale; move32(); diff --git a/lib_dec/acelp_core_dec_fx.c b/lib_dec/acelp_core_dec_fx.c index c5d367699..035800779 100644 --- a/lib_dec/acelp_core_dec_fx.c +++ b/lib_dec/acelp_core_dec_fx.c @@ -2286,7 +2286,11 @@ ivas_error acelp_core_dec_fx( ELSE { Copy_Scale_sig_16_32_no_sat( st->hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_fx, NL_BUFF_OFFSET, ( sub( shl( st->Q_exc, 1 ), st->hBWE_TD->q_old_bwe_exc_extended_fx ) ) ); /* Q(q_old_bwe_exc_extended_fx) -> Q(2 * Q_exc) */ +#ifdef HARM_NON_LINEARITY + non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); +#else non_linearity_ivas_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); +#endif exp = sub( L_norm_arr( bwe_exc_extended_fx + L_FRAME32k, NL_BUFF_OFFSET ), 16 ); Copy_Scale_sig_32_16( bwe_exc_extended_fx + L_FRAME32k, st->hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET, exp ); /* Q(2 * Q_exc) -> Q(q_old_bwe_exc_extended_fx) */ st->hBWE_TD->q_old_bwe_exc_extended_fx = add( shl( st->Q_exc, 1 ), exp ); diff --git a/lib_enc/acelp_core_enc_fx.c b/lib_enc/acelp_core_enc_fx.c index 93b4b46e3..28e8eeb08 100644 --- a/lib_enc/acelp_core_enc_fx.c +++ b/lib_enc/acelp_core_enc_fx.c @@ -1434,7 +1434,11 @@ ivas_error acelp_core_enc_ivas_fx( { /* Apply a non linearity to the SHB excitation */ Copy_Scale_sig_16_32_no_sat( st->hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_fx, NL_BUFF_OFFSET, ( sub( shl( Q_new, 1 ), sub( st->hBWE_TD->prev_Q_bwe_exc, 16 ) ) ) ); // prev_Q_bwe_exc +#ifdef HARM_NON_LINEARITY + non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, st->coder_type, voice_factors_fx, st->L_frame ); +#else non_linearity_ivas_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, st->coder_type, voice_factors_fx, st->L_frame ); +#endif Copy_Scale_sig_32_16( bwe_exc_extended_fx + L_FRAME32k, st->hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET, negate( sub( shl( Q_new, 1 ), sub( st->hBWE_TD->prev_Q_bwe_exc, 16 ) ) ) ); // prev_Q_bwe_exc } -- GitLab From 7738e1ea85313cfe14744174b66248d57931992b Mon Sep 17 00:00:00 2001 From: vaclav Date: Fri, 16 Jan 2026 21:15:42 +0100 Subject: [PATCH 02/11] fix EVS BE --- lib_com/prot_fx.h | 3 + lib_com/swb_tbe_com_fx.c | 107 +++++++++++++++++++++++++------- lib_dec/acelp_core_dec_fx.c | 6 +- lib_dec/dec_acelp_tcx_main_fx.c | 4 ++ lib_enc/acelp_core_enc_fx.c | 6 +- lib_enc/enc_acelp_tcx_main_fx.c | 6 +- 6 files changed, 108 insertions(+), 24 deletions(-) diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 8076cd1ea..2599d8b12 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -3165,6 +3165,9 @@ void ScaleShapedWB_fx( ); void non_linearity_fx( +#ifdef HARM_NON_LINEARITY + const Word16 element_mode, /* i : element mode */ +#endif const Word16 i[], /* i : i signal Q_inp */ Word32 output[], /* o : output signal 2*Q_inp */ const Word16 length, /* i : i length */ diff --git a/lib_com/swb_tbe_com_fx.c b/lib_com/swb_tbe_com_fx.c index 0ad5eff5d..5323e59da 100644 --- a/lib_com/swb_tbe_com_fx.c +++ b/lib_com/swb_tbe_com_fx.c @@ -5327,7 +5327,7 @@ void ScaleShapedWB_fx( return; } -#ifndef HARM_NON_LINEARITY + /*-------------------------------------------------------------------* * non_linearity() * @@ -5336,7 +5336,7 @@ void ScaleShapedWB_fx( static Word32 non_linearity_scaled_copy( const Word16 input[], - Word16 j, + const Word16 j, const Word16 length, Word32 output[], Word32 prev_scale, @@ -5346,7 +5346,6 @@ static Word32 non_linearity_scaled_copy( Word16 i; Word32 L_tmp; - IF( en_abs ) { FOR( i = 0; i < j; i++ ) @@ -5398,21 +5397,18 @@ static Word32 non_linearity_scaled_copy( move32(); } } + return prev_scale; } -#endif + /*-------------------------------------------------------------------* * non_linearity() * - * Apply a non linearity to the SHB excitation + * Apply a non linearity to the SHB excitation in IVAS formats * -------------------------------------------------------------------*/ -#ifdef HARM_NON_LINEARITY -static Word32 non_linearity_scaled_copy( -#else static Word32 non_linearity_scaled_copy_ivas( -#endif const Word16 input[], const Word16 j, const Word16 length, @@ -5538,6 +5534,9 @@ static Word32 non_linearity_scaled_copy_ivas( /*==========================================================================*/ void non_linearity_fx( +#ifdef HARM_NON_LINEARITY + const Word16 element_mode, /* i : element mode */ +#endif const Word16 input[], /* i : input signal Q_inp */ Word32 output[], /* o : output signal 2*Q_inp */ const Word16 length, /* i : input length */ @@ -5810,6 +5809,9 @@ void non_linearity_ivas_fx( Word32 prev_scale; Word16 length_half; Word16 sc_factor; +#ifdef HARM_NON_LINEARITY + Word16 cond; +#endif IF( EQ_16( L_frame, L_FRAME16k ) ) { @@ -5867,11 +5869,37 @@ void non_linearity_ivas_fx( move32(); /* Q31; 0.67 in Q31 */ } +#ifdef HARM_NON_LINEARITY + cond = 0; + move16(); + IF( element_mode == EVS_MONO ) + { + if ( GT_32( Mult_32_16( prev_scale, 32 ), scale ) ) + { + cond = 1; + move16(); + } + } + ELSE + { + sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position */ + sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ + if ( GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) + { + cond = 1; + move16(); + } + } + + test(); + IF( prev_scale <= 0 || cond ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ +#else sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position */ sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ test(); IF( prev_scale <= 0 || GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ +#endif { scale_step = 16384; move16(); /* Q14 */ @@ -5916,10 +5944,15 @@ void non_linearity_ivas_fx( } #ifdef HARM_NON_LINEARITY - prev_scale = non_linearity_scaled_copy( input, j, length_half, output, prev_scale, scale_step, en_abs ); -#else - prev_scale = non_linearity_scaled_copy_ivas( input, j, length_half, output, prev_scale, scale_step, en_abs ); + IF( element_mode == EVS_MONO ) + { + prev_scale = non_linearity_scaled_copy( input, j, length_half, output, prev_scale, scale_step, en_abs ); + } + ELSE #endif + { + prev_scale = non_linearity_scaled_copy_ivas( input, j, length_half, output, prev_scale, scale_step, en_abs ); + } max_val = 0; move16(); @@ -5947,12 +5980,39 @@ void non_linearity_ivas_fx( move32(); /* Q31; 0.67 in Q31 */ } - /* if ( element_mode > EVS_MONO ) */ /* element mode is not needed because the function is duplicated for IVAS */ - sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 ); /* allowed intra frame jump is smaller */ - sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ +#ifdef HARM_NON_LINEARITY + cond = 0; + move16(); + IF( element_mode == EVS_MONO ) + { + if ( GT_32( Mult_32_16( prev_scale, 32 ), scale ) ) + { + cond = 1; + move16(); + } + } + ELSE + { + sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 ); /* allowed intra frame jump is smaller */ + sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ + + if ( GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ + { + cond = 1; + move16(); + } + } + + test(); + IF( prev_scale <= 0 || cond ) +#else + /* if ( element_mode > EVS_MONO ) */ /* element mode is not needed because the function is duplicated for IVAS */ + sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 ); /* allowed intra frame jump is smaller */ + sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ test(); IF( prev_scale <= 0 || GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ +#endif { scale_step = 16384; move16(); /*Q14 */ @@ -5997,10 +6057,15 @@ void non_linearity_ivas_fx( } #ifdef HARM_NON_LINEARITY - prev_scale = non_linearity_scaled_copy( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs ); -#else - prev_scale = non_linearity_scaled_copy_ivas( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs ); + IF( element_mode == EVS_MONO ) + { + prev_scale = non_linearity_scaled_copy( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs ); + } + ELSE #endif + { + prev_scale = non_linearity_scaled_copy_ivas( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs ); + } *pPrevScale = prev_scale; move32(); @@ -6046,7 +6111,7 @@ void create_random_vector_fx( L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[0] ) ) ); /*Q23 */ } #else - L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[0] ) ) ); /*Q23 */ + L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[0] ) ) ); /*Q23 */ #endif j = extract_l( L_shr( L_tmp, 23 ) ); @@ -6063,7 +6128,7 @@ void create_random_vector_fx( } #else - L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[1] ) ) ); /*Q23 */ + L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[1] ) ) ); /*Q23 */ #endif k = extract_l( L_shr( L_tmp, 23 ) ); @@ -6081,7 +6146,7 @@ void create_random_vector_fx( L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[1] ) ) ); /*Q23 */ } #else - L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[1] ) ) ); /*Q23 */ + L_tmp = L_abs( Mult_32_16( 2144047674, Random( &seed[1] ) ) ); /*Q23 */ #endif k = extract_l( L_shr( L_tmp, 23 ) ); diff --git a/lib_dec/acelp_core_dec_fx.c b/lib_dec/acelp_core_dec_fx.c index 035800779..0646713cc 100644 --- a/lib_dec/acelp_core_dec_fx.c +++ b/lib_dec/acelp_core_dec_fx.c @@ -2281,13 +2281,17 @@ ivas_error acelp_core_dec_fx( { IF( EQ_16( st->element_mode, EVS_MONO ) ) { +#ifdef HARM_NON_LINEARITY + non_linearity_fx( st->element_mode, bwe_exc_fx, bwe_exc_extended_fx, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); +#else non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); +#endif } ELSE { Copy_Scale_sig_16_32_no_sat( st->hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_fx, NL_BUFF_OFFSET, ( sub( shl( st->Q_exc, 1 ), st->hBWE_TD->q_old_bwe_exc_extended_fx ) ) ); /* Q(q_old_bwe_exc_extended_fx) -> Q(2 * Q_exc) */ #ifdef HARM_NON_LINEARITY - non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); + non_linearity_fx( st->element_mode, bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); #else non_linearity_ivas_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame ); #endif diff --git a/lib_dec/dec_acelp_tcx_main_fx.c b/lib_dec/dec_acelp_tcx_main_fx.c index 94d4486b1..b475a31d7 100644 --- a/lib_dec/dec_acelp_tcx_main_fx.c +++ b/lib_dec/dec_acelp_tcx_main_fx.c @@ -479,7 +479,11 @@ Word16 dec_acelp_tcx_frame_fx( test(); IF( st->core == ACELP_CORE && st->igf != 0 && st->con_tcx == 0 ) { +#ifdef HARM_NON_LINEARITY + non_linearity_fx( EVS_MONO, ptr_bwe_exc, bwe_exc_extended, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors, st->L_frame ); +#else non_linearity_fx( ptr_bwe_exc, bwe_exc_extended, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors, st->L_frame ); +#endif /* update the old BWE exe memory */ Copy( &old_bwe_exc[L_FRAME32k], hBWE_TD->old_bwe_exc_fx, PIT16k_MAX * 2 ); } diff --git a/lib_enc/acelp_core_enc_fx.c b/lib_enc/acelp_core_enc_fx.c index 28e8eeb08..6a96a81d0 100644 --- a/lib_enc/acelp_core_enc_fx.c +++ b/lib_enc/acelp_core_enc_fx.c @@ -620,7 +620,11 @@ ivas_error acelp_core_enc_fx( IF( !st_fx->Opt_SC_VBR ) { /* Apply a non linearity to the SHB excitation */ +#ifdef HARM_NON_LINEARITY + non_linearity_fx( st_fx->element_mode, bwe_exc_fx, bwe_exc_extended_fx, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, coder_type, voice_factors_fx, st_fx->L_frame ); +#else non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, coder_type, voice_factors_fx, st_fx->L_frame ); +#endif } test(); if ( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, FRAME_NO_DATA ) ) @@ -1435,7 +1439,7 @@ ivas_error acelp_core_enc_ivas_fx( /* Apply a non linearity to the SHB excitation */ Copy_Scale_sig_16_32_no_sat( st->hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_fx, NL_BUFF_OFFSET, ( sub( shl( Q_new, 1 ), sub( st->hBWE_TD->prev_Q_bwe_exc, 16 ) ) ) ); // prev_Q_bwe_exc #ifdef HARM_NON_LINEARITY - non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, st->coder_type, voice_factors_fx, st->L_frame ); + non_linearity_fx( st->element_mode, bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, st->coder_type, voice_factors_fx, st->L_frame ); #else non_linearity_ivas_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, Q_new, st->coder_type, voice_factors_fx, st->L_frame ); #endif diff --git a/lib_enc/enc_acelp_tcx_main_fx.c b/lib_enc/enc_acelp_tcx_main_fx.c index da8443f06..5e3c9241b 100644 --- a/lib_enc/enc_acelp_tcx_main_fx.c +++ b/lib_enc/enc_acelp_tcx_main_fx.c @@ -63,15 +63,19 @@ void enc_acelp_tcx_main_fx( { core_encode_twodiv_fx( new_samples, st, st->coder_type, Aw, vad_hover_flag, Q_new, shift ); } + /*-----------------------------------------------------------------* * Apply non linearity to the SHB excitation *-----------------------------------------------------------------*/ - test(); IF( EQ_16( st->core, ACELP_CORE ) && st->igf != 0 ) { +#ifdef HARM_NON_LINEARITY + non_linearity_fx( EVS_MONO, ptr_bwe_exc, bwe_exc_extended, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, *Q_new, st->coder_type, voice_factors, st->L_frame ); +#else non_linearity_fx( ptr_bwe_exc, bwe_exc_extended, L_FRAME32k, &hBWE_TD->bwe_non_lin_prev_scale_fx, *Q_new, st->coder_type, voice_factors, st->L_frame ); +#endif /* update the old BWE exe memory */ Copy( &old_bwe_exc[L_FRAME32k], hBWE_TD->old_bwe_exc_fx, PIT16k_MAX * 2 ); /*Q_exc*/ -- GitLab From b6dcb71409d4f252ae60b2ef3560fd1152f9f1ee Mon Sep 17 00:00:00 2001 From: vaclav Date: Sat, 17 Jan 2026 14:04:13 +0100 Subject: [PATCH 03/11] simplification --- lib_com/swb_tbe_com_fx.c | 42 +++++++++++----------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/lib_com/swb_tbe_com_fx.c b/lib_com/swb_tbe_com_fx.c index 5323e59da..85aa858f0 100644 --- a/lib_com/swb_tbe_com_fx.c +++ b/lib_com/swb_tbe_com_fx.c @@ -5811,18 +5811,19 @@ void non_linearity_ivas_fx( Word16 sc_factor; #ifdef HARM_NON_LINEARITY Word16 cond; + Word32 prev_scale_factor; #endif IF( EQ_16( L_frame, L_FRAME16k ) ) { - nframes = 5; + nframes = NB_SUBFR16k; move16(); ths = 17817; move16(); /* 0.87*5 in Q12 */ } ELSE { - nframes = 4; + nframes = NB_SUBFR; move16(); ths = 15400; move16(); /* 0.94*4 in Q12 */ @@ -5870,29 +5871,19 @@ void non_linearity_ivas_fx( } #ifdef HARM_NON_LINEARITY - cond = 0; - move16(); IF( element_mode == EVS_MONO ) { - if ( GT_32( Mult_32_16( prev_scale, 32 ), scale ) ) - { - cond = 1; - move16(); - } + prev_scale_factor = Mult_32_16( prev_scale, 32 ); } ELSE { - sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position */ - sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ - if ( GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) - { - cond = 1; - move16(); - } + sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position */ + sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ + prev_scale_factor = L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ); /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ } test(); - IF( prev_scale <= 0 || cond ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ + IF( prev_scale <= 0 || ( GT_32( prev_scale_factor, scale /*Q31*/ ) ) ) #else sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position */ sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ @@ -5981,30 +5972,19 @@ void non_linearity_ivas_fx( } #ifdef HARM_NON_LINEARITY - cond = 0; - move16(); IF( element_mode == EVS_MONO ) { - if ( GT_32( Mult_32_16( prev_scale, 32 ), scale ) ) - { - cond = 1; - move16(); - } + prev_scale_factor = Mult_32_16( prev_scale, 32 ); } ELSE { sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 ); /* allowed intra frame jump is smaller */ sc_factor = s_max( sc_factor, 1 ); /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */ - - if ( GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ - { - cond = 1; - move16(); - } + prev_scale_factor = L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ); /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */ } test(); - IF( prev_scale <= 0 || cond ) + IF( prev_scale <= 0 || ( GT_32( prev_scale_factor, scale /*Q31*/ ) ) ) #else /* if ( element_mode > EVS_MONO ) */ /* element mode is not needed because the function is duplicated for IVAS */ sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 ); /* allowed intra frame jump is smaller */ -- GitLab From a9803e23495cdce15fb91bfc74c4f7c6dec8f813 Mon Sep 17 00:00:00 2001 From: vaclav Date: Sat, 17 Jan 2026 15:58:05 +0100 Subject: [PATCH 04/11] harmonize cng_params_upd_fx() and cng_params_postupd_fx() --- lib_com/cng_exc_fx.c | 30 ++++++++++++++++++++++++------ lib_com/prot_fx.h | 12 ++++++++---- lib_dec/acelp_core_dec_fx.c | 5 +++++ lib_dec/dec_LPD_fx.c | 11 +++++++++++ lib_dec/ivas_tcx_core_dec_fx.c | 11 +++++++++++ lib_enc/acelp_core_enc_fx.c | 21 +++++++++++++++++++++ lib_enc/core_enc_ol_fx.c | 11 +++++++++++ lib_enc/ivas_tcx_core_enc_fx.c | 10 ++++++++++ 8 files changed, 101 insertions(+), 10 deletions(-) diff --git a/lib_com/cng_exc_fx.c b/lib_com/cng_exc_fx.c index f96fba710..4d2d76baa 100644 --- a/lib_com/cng_exc_fx.c +++ b/lib_com/cng_exc_fx.c @@ -180,12 +180,14 @@ IF( GT_16( element_mode, IVAS_SCE ) ) { return; } + /*---------------------------------------------------------------------* * Generate white noise vector *---------------------------------------------------------------------*/ /*for ( i=0; icore_brate, SID_2k40 ) && st->hTdCngDec != NULL && st->hFdCngDec != NULL ) { /* update CNG parameters in active frames */ +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsp_new_fx, exc_fx, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, + st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth ); +#else IF( EQ_16( st->element_mode, EVS_MONO ) ) { cng_params_upd_fx( lsp_new_fx, exc_fx, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, @@ -2332,6 +2336,7 @@ ivas_error acelp_core_dec_fx( st->hTdCngDec->ho_lsp_circ_fx, st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth ); } +#endif /* Set 16k LSP flag for CNG buffer */ st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = 0; diff --git a/lib_dec/dec_LPD_fx.c b/lib_dec/dec_LPD_fx.c index 6a610063c..07372eae0 100644 --- a/lib_dec/dec_LPD_fx.c +++ b/lib_dec/dec_LPD_fx.c @@ -934,16 +934,27 @@ void decoder_LPD_fx( E_LPC_lev_dur( r_h, r_l, A, NULL, M, NULL ); E_LPC_a_lsp_conversion( A, lsptmp, &xspnew_uw[0], M ); Residu3_fx( A, buf + L_LP - L_FRAME, res, L_FRAME, 1 ); + +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsptmp, res, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, + Qexc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, EVS_MONO, -1 ); +#else cng_params_upd_fx( lsptmp, res, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, Qexc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate ); +#endif } ELSE { +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( &lsp[M], st->old_exc_fx + L_EXC_MEM_DEC - st->L_frame, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, + st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, EVS_MONO, -1 ); +#else cng_params_upd_fx( &lsp[M], st->old_exc_fx + L_EXC_MEM_DEC - st->L_frame, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate ); +#endif } /* Set 16k LSP flag for CNG buffer */ diff --git a/lib_dec/ivas_tcx_core_dec_fx.c b/lib_dec/ivas_tcx_core_dec_fx.c index 3b4cc87f3..f1c760993 100644 --- a/lib_dec/ivas_tcx_core_dec_fx.c +++ b/lib_dec/ivas_tcx_core_dec_fx.c @@ -753,13 +753,24 @@ void stereo_tcx_core_dec_fx( IF( st->hTdCngDec != NULL ) { +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsptmp, res, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, + st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth ); +#else cng_params_upd_ivas_fx( lsptmp, res, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth ); +#endif } } ELSE IF( st->hTdCngDec != NULL ) { +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( &lsp_fx[M], st->old_exc_fx + sub( L_EXC_MEM_DEC, st->L_frame ), st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, + st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth ); +#else cng_params_upd_ivas_fx( &lsp_fx[M], st->old_exc_fx + sub( L_EXC_MEM_DEC, st->L_frame ), st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx, st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth ); +#endif } + /* Set 16k LSP flag for CNG buffer */ // st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = ( st->L_frame == L_FRAME ? 0 : 1 ); IF( EQ_16( st->L_frame, L_FRAME ) ) diff --git a/lib_enc/acelp_core_enc_fx.c b/lib_enc/acelp_core_enc_fx.c index 6a96a81d0..6e1709ce1 100644 --- a/lib_enc/acelp_core_enc_fx.c +++ b/lib_enc/acelp_core_enc_fx.c @@ -274,8 +274,14 @@ ivas_error acelp_core_enc_fx( IF( EQ_16( st_fx->cng_type, LP_CNG ) ) { /* Run CNG post parameter update */ +#ifdef HARM_NON_LINEARITY + cng_params_postupd_fx( hTdCngEnc->ho_circ_ptr, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, + hTdCngEnc->cng_brate_buf, hTdCngEnc->ho_env_circ_fx, EVS_MONO, -1 ); +#else cng_params_postupd_fx( hTdCngEnc->ho_circ_ptr, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hTdCngEnc->ho_env_circ_fx ); +#endif + /* encode CNG parameters */ CNG_enc_fx( st_fx, Aq_fx, inp_fx, ener_fx, lsp_mid_fx, lsp_new_fx, lsf_new_fx, &allow_cn_step_fx, sub( Q_new, 1 ), q_env, &sid_bw ); @@ -642,9 +648,14 @@ ivas_error acelp_core_enc_fx( IF( hTdCngEnc != NULL && ( st_fx->Opt_DTX_ON != 0 ) && ( GT_32( st_fx->core_brate, SID_2k40 ) ) ) { /* update CNG parameters in active frames */ +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsp_new_fx, exc_fx, st_fx->L_frame, &hTdCngEnc->ho_circ_ptr, hTdCngEnc->ho_ener_circ_fx, &hTdCngEnc->ho_circ_size, hTdCngEnc->ho_lsp_circ_fx, + Q_new, ENC, NULL, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hDtxEnc->last_active_brate, EVS_MONO, -1 ); +#else cng_params_upd_fx( lsp_new_fx, exc_fx, st_fx->L_frame, &hTdCngEnc->ho_circ_ptr, hTdCngEnc->ho_ener_circ_fx, &hTdCngEnc->ho_circ_size, hTdCngEnc->ho_lsp_circ_fx, Q_new, ENC, NULL, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hDtxEnc->last_active_brate ); +#endif IF( EQ_16( st_fx->L_frame, L_FRAME ) ) { @@ -972,7 +983,11 @@ ivas_error acelp_core_enc_ivas_fx( IF( st->cng_type == LP_CNG ) { /* Run CNG post parameter update */ +#ifdef HARM_NON_LINEARITY + cng_params_postupd_fx( st->hTdCngEnc->ho_circ_ptr, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hTdCngEnc->ho_env_circ_fx, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#else cng_params_postupd_ivas_fx( st->hTdCngEnc->ho_circ_ptr, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hTdCngEnc->ho_env_circ_fx, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#endif /* encode CNG parameters */ CNG_enc_ivas_fx( st, Aq, inp, /*ener_fx,*/ lsp_mid, lsp_new, lsf_new_fx, &allow_cn_step, sub( Q_new, 1 ), q_env_fx, &sid_bw ); @@ -1488,10 +1503,16 @@ ivas_error acelp_core_enc_ivas_fx( st->hTdCngEnc->cng_Qexc_buf[ii] = q_exc; move16(); } + +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsp_new, exc_fx, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ_fx, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ_fx, + q_exc, ENC, st->hTdCngEnc->ho_env_circ_fx, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#else cng_params_upd_ivas_fx( lsp_new, exc_fx, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ_fx, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ_fx, q_exc, ENC, st->hTdCngEnc->ho_env_circ_fx, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#endif IF( EQ_16( st->L_frame, L_FRAME ) ) { diff --git a/lib_enc/core_enc_ol_fx.c b/lib_enc/core_enc_ol_fx.c index cdecaf3d6..ce0a8f23e 100644 --- a/lib_enc/core_enc_ol_fx.c +++ b/lib_enc/core_enc_ol_fx.c @@ -460,18 +460,29 @@ void core_encode_openloop_fx( E_LPC_lev_dur( r_h, r_l, A, NULL, M, NULL ); E_LPC_a_lsp_conversion( A, lsptmp, lsp_new, M ); Residu3_fx( A, buf + L_LP - L_FRAME, res, L_FRAME, 1 ); + +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsptmp, res, st->L_frame, &hTdCngEnc->ho_circ_ptr, hTdCngEnc->ho_ener_circ_fx, &hTdCngEnc->ho_circ_size, hTdCngEnc->ho_lsp_circ_fx, + Q_new, ENC, NULL, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hDtxEnc->last_active_brate, EVS_MONO, -1 ); +#else cng_params_upd_fx( lsptmp, res, st->L_frame, &hTdCngEnc->ho_circ_ptr, hTdCngEnc->ho_ener_circ_fx, &hTdCngEnc->ho_circ_size, hTdCngEnc->ho_lsp_circ_fx, Q_new, ENC, NULL, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hDtxEnc->last_active_brate ); +#endif } ELSE { +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsp_new, hLPDmem->old_exc + L_EXC_MEM - st->L_frame, st->L_frame, &hTdCngEnc->ho_circ_ptr, hTdCngEnc->ho_ener_circ_fx, &hTdCngEnc->ho_circ_size, hTdCngEnc->ho_lsp_circ_fx, + Q_new, ENC, NULL, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hDtxEnc->last_active_brate, EVS_MONO, -1 ); +#else cng_params_upd_fx( lsp_new, hLPDmem->old_exc + L_EXC_MEM - st->L_frame, st->L_frame, &hTdCngEnc->ho_circ_ptr, hTdCngEnc->ho_ener_circ_fx, &hTdCngEnc->ho_circ_size, hTdCngEnc->ho_lsp_circ_fx, Q_new, ENC, NULL, &hTdCngEnc->cng_buf_cnt, hTdCngEnc->cng_exc2_buf, hTdCngEnc->cng_Qexc_buf, hTdCngEnc->cng_brate_buf, hDtxEnc->last_active_brate ); +#endif } IF( EQ_16( st->L_frame, L_FRAME ) ) diff --git a/lib_enc/ivas_tcx_core_enc_fx.c b/lib_enc/ivas_tcx_core_enc_fx.c index 18d79f26d..733a64376 100644 --- a/lib_enc/ivas_tcx_core_enc_fx.c +++ b/lib_enc/ivas_tcx_core_enc_fx.c @@ -634,17 +634,27 @@ void stereo_tcx_core_enc( E_LPC_a_lsp_conversion( A_fx, lsptmp_fx, lsp_new_fx, M ); Residu3_fx( A_fx, buf_fx + L_LP - L_FRAME, res_fx, L_FRAME, 0 ); +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsptmp_fx, res_fx, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ_fx, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ_fx, + Q_r, ENC, st->hTdCngEnc->ho_env_circ_fx, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#else cng_params_upd_ivas_fx( lsptmp_fx, res_fx, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ_fx, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ_fx, Q_r, ENC, st->hTdCngEnc->ho_env_circ_fx, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#endif Q_exc = Q_r; move16(); } ELSE { +#ifdef HARM_NON_LINEARITY + cng_params_upd_fx( lsp_new_fx, st->hLPDmem->old_exc + L_EXC_MEM - st->L_frame, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ_fx, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ_fx, + st->hLPDmem->q_lpd_old_exc, ENC, st->hTdCngEnc->ho_env_circ_fx, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#else cng_params_upd_ivas_fx( lsp_new_fx, st->hLPDmem->old_exc + L_EXC_MEM - st->L_frame, st->L_frame, &st->hTdCngEnc->ho_circ_ptr, st->hTdCngEnc->ho_ener_circ_fx, &st->hTdCngEnc->ho_circ_size, st->hTdCngEnc->ho_lsp_circ_fx, st->hLPDmem->q_lpd_old_exc, ENC, st->hTdCngEnc->ho_env_circ_fx, &st->hTdCngEnc->cng_buf_cnt, st->hTdCngEnc->cng_exc2_buf, st->hTdCngEnc->cng_Qexc_buf, st->hTdCngEnc->cng_brate_buf, st->hDtxEnc->last_active_brate, st->element_mode, st->hFdCngEnc->hFdCngCom->CngBandwidth ); +#endif } IF( EQ_16( st->L_frame, L_FRAME ) ) -- GitLab From 82b98c43b5e2e9b1f84af74719b616ddb69d4488 Mon Sep 17 00:00:00 2001 From: vaclav Date: Sun, 18 Jan 2026 17:34:51 +0100 Subject: [PATCH 05/11] simplification --- lib_com/swb_tbe_com_fx.c | 1 - lib_enc/acelp_core_enc_fx.c | 11 + lib_enc/amr_wb_enc_fx.c | 4 + lib_enc/cng_enc_fx.c | 501 ++++++++++++++++++++++++------------ lib_enc/prot_fx_enc.h | 9 +- 5 files changed, 354 insertions(+), 172 deletions(-) diff --git a/lib_com/swb_tbe_com_fx.c b/lib_com/swb_tbe_com_fx.c index 85aa858f0..60ac7bd81 100644 --- a/lib_com/swb_tbe_com_fx.c +++ b/lib_com/swb_tbe_com_fx.c @@ -5810,7 +5810,6 @@ void non_linearity_ivas_fx( Word16 length_half; Word16 sc_factor; #ifdef HARM_NON_LINEARITY - Word16 cond; Word32 prev_scale_factor; #endif diff --git a/lib_enc/acelp_core_enc_fx.c b/lib_enc/acelp_core_enc_fx.c index 6e1709ce1..67def7a08 100644 --- a/lib_enc/acelp_core_enc_fx.c +++ b/lib_enc/acelp_core_enc_fx.c @@ -97,6 +97,9 @@ ivas_error acelp_core_enc_fx( error = IVAS_ERR_OK; (void) hStereoTD; +#ifdef HARM_NON_LINEARITY + (void) ener_fx; +#endif /*------------------------------------------------------------------* * Initialization *------------------------------------------------------------------*/ @@ -283,7 +286,11 @@ ivas_error acelp_core_enc_fx( #endif /* encode CNG parameters */ +#ifdef HARM_NON_LINEARITY + CNG_enc_fx( st_fx, Aq_fx, inp_fx, lsp_mid_fx, lsp_new_fx, lsf_new_fx, &allow_cn_step_fx, sub( Q_new, 1 ), q_env, &sid_bw ); +#else CNG_enc_fx( st_fx, Aq_fx, inp_fx, ener_fx, lsp_mid_fx, lsp_new_fx, lsf_new_fx, &allow_cn_step_fx, sub( Q_new, 1 ), q_env, &sid_bw ); +#endif /* comfort noise generation */ CNG_exc_fx( st_fx->core_brate, st_fx->L_frame, &hTdCngEnc->Enew_fx, &hTdCngEnc->cng_seed, exc_fx, exc2_fx, &hTdCngEnc->lp_ener_fx, st_fx->last_core_brate, @@ -990,7 +997,11 @@ ivas_error acelp_core_enc_ivas_fx( #endif /* encode CNG parameters */ +#ifdef HARM_NON_LINEARITY + CNG_enc_fx( st, Aq, inp, lsp_mid, lsp_new, lsf_new_fx, &allow_cn_step, sub( Q_new, 1 ), q_env_fx, &sid_bw ); +#else CNG_enc_ivas_fx( st, Aq, inp, /*ener_fx,*/ lsp_mid, lsp_new, lsf_new_fx, &allow_cn_step, sub( Q_new, 1 ), q_env_fx, &sid_bw ); +#endif /* comfort noise generation */ CNG_exc_fx( st->core_brate, st->L_frame, &st->hTdCngEnc->Enew_fx, &st->hTdCngEnc->cng_seed, exc_fx, exc2_fx, &st->hTdCngEnc->lp_ener_fx, st->last_core_brate, diff --git a/lib_enc/amr_wb_enc_fx.c b/lib_enc/amr_wb_enc_fx.c index 72f981259..c1865862f 100644 --- a/lib_enc/amr_wb_enc_fx.c +++ b/lib_enc/amr_wb_enc_fx.c @@ -471,7 +471,11 @@ void amr_wb_enc_fx( IF( EQ_32( st->core_brate, SID_1k75 ) || EQ_32( st->core_brate, FRAME_NO_DATA ) ) { /* encode CNG parameters */ +#ifdef HARM_NON_LINEARITY + CNG_enc_fx( st, Aq, inp, isp_new, isp_new, isf_new, &allow_cn_step, sub( Q_new, 1 ), q_env, &sid_bw ); +#else CNG_enc_fx( st, Aq, inp, ener, isp_new, isp_new, isf_new, &allow_cn_step, sub( Q_new, 1 ), q_env, &sid_bw ); +#endif /* comfort noise generation */ CNG_exc_fx( st->core_brate, L_FRAME, &hTdCngEnc->Enew_fx, &hTdCngEnc->cng_seed, exc, exc2, &hTdCngEnc->lp_ener_fx, diff --git a/lib_enc/cng_enc_fx.c b/lib_enc/cng_enc_fx.c index de211162b..10275fb85 100644 --- a/lib_enc/cng_enc_fx.c +++ b/lib_enc/cng_enc_fx.c @@ -1,6 +1,7 @@ /*==================================================================================== EVS Codec 3GPP TS26.452 Nov 04, 2021. Version 16.4.0 ====================================================================================*/ + #include #include "options.h" /* Compilation switches */ #include "cnst.h" /* Common constants */ @@ -8,9 +9,9 @@ #include "rom_com.h" /* Static table prototypes */ #include "prot_fx.h" /* Function prototypes */ #include "prot_fx_enc.h" /* Function prototypes */ -//#include "basop_mpy.h" - #include + + /*---------------------------------------------------------------------* * Local constants *---------------------------------------------------------------------*/ @@ -20,18 +21,24 @@ #define INT_H 50 #define INT_L 8 + /*---------------------------------------------------------------------* * Local function prototypes *---------------------------------------------------------------------*/ + static void shb_CNG_encod_fx( Encoder_State *st_fx, const Word16 update_fx ); static Word16 shb_DTX_fx( Encoder_State *st_fx, const Word16 *shb_speech_fx, const Word16 *syn_12k8_16k ); static Word16 shb_DTX_ivas_fx( Encoder_State *st, const Word16 *shb_speech_fx, const Word16 *syn_12k8_16k_fx ); static void shb_CNG_encod_ivas_fx( Encoder_State *st, const Word16 update ); + + /*---------------------------------------------------------------------* * CNG_enc() * * Confort noise generation for the coder *---------------------------------------------------------------------*/ + +#ifndef HARM_NON_LINEARITY void CNG_enc_fx( Encoder_State *st_fx, /* i/o: State structure */ Word16 Aq[], /* o : LP coefficients Q12 */ @@ -1169,11 +1176,13 @@ void CNG_enc_fx( } void CNG_enc_ivas_fx( - Encoder_State *st_fx, /* i/o: State structure */ - Word16 Aq[], /* o : LP coefficients Q12 */ - const Word16 *speech, /* i : pointer to current frame input speech buffer Q_new */ - // Word32 L_ener, /* i : residual energy from Levinson-Durbin Q6 */ - const Word16 *lsp_mid, /* i : mid frame LSPs Q15 */ +#else +void CNG_enc_fx( +#endif + Encoder_State *st_fx, /* i/o: State structure */ + Word16 Aq[], /* o : LP coefficients Q12 */ + const Word16 *speech, /* i : pointer to current frame input speech buffer Q_new */ + const Word16 *lsp_mid, /* i : mid frame LSPs Q15 */ Word16 *lsp_new, /* i/o: current frame ISPs Q15 */ Word16 *lsf_new, /* i/o: current frame ISFs Qlog2(2.56) */ Word16 *allow_cn_step, /* o : allow CN step Q0 */ @@ -1207,9 +1216,14 @@ void CNG_enc_ivas_fx( Word16 max_idx[2]; Word16 ftmp_fx; Word16 lsp_tmp[M]; +#ifdef HARM_NON_LINEARITY + Word32 dev, max_dev, dist; + Word16 dev16, max_dev16, dist16; +#else Word32 dev; Word32 max_dev; Word32 dist; +#endif Word16 max_idx1[2]; Word16 fft_io[L_FRAME16k]; Word16 *ptR, *ptI; @@ -1233,6 +1247,10 @@ void CNG_enc_ivas_fx( Word64 w_temp; Word32 inv_frame_len; Word32 L_ener; +#ifdef HARM_NON_LINEARITY + Word16 *pt_res; + const Word16 *pt_sp; +#endif step_inv = 0; move16(); @@ -1256,7 +1274,6 @@ void CNG_enc_ivas_fx( move16(); st_lp_sp_enr = hTdCngEnc->lp_sp_enr_fx; move16(); - /* Temp variables for floating point functions */ lp_ener_thr_scale = 8; /* 4.0f*/ /* Q1 */ move16(); @@ -1266,58 +1283,91 @@ void CNG_enc_ivas_fx( move16(); } - w_temp = 1; - move64(); - - FOR( j = 0; j < st_fx->L_frame; j++ ) +#ifdef HARM_NON_LINEARITY + IF( st_fx->element_mode == EVS_MONO ) { - w_temp = W_mac0_16_16( w_temp, speech[j], speech[j] ); + pt_sp = speech; + L_ener = L_deposit_l( 1 ); + /* L_ener = L_add(L_shr(sum2_f_fx( speech, L_frame ), 8) , L_ener);*/ + IF( EQ_16( st_fx->L_frame, L_FRAME ) ) + { + FOR( j = 0; j < 128; j++ ) + { + L_tmp = L_mult0( *pt_sp, *pt_sp ); + pt_sp++; + L_tmp = L_mac0( L_tmp, *pt_sp, *pt_sp ); + pt_sp++; + L_ener = L_add( L_ener, L_shr( L_tmp, 7 ) ); /* 2*Q_new + 1, divide by L_frame done here */ + } + } + ELSE /* L_FRAME16k */ + { + FOR( j = 0; j < 80; j++ ) + { + L_tmp = L_mult0( *pt_sp, *pt_sp ); + pt_sp++; + L_tmp = L_mac0( L_tmp, *pt_sp, *pt_sp ); + pt_sp++; + L_ener = L_add( L_ener, L_shr( Mult_32_16( L_tmp, 26214 /* 256/320, Q15 */ ), 7 ) ); /* 2*Q_new + 1, divide by L_frame done here */ + } + } } - exp = W_norm( w_temp ); + ELSE +#endif + { + w_temp = 1; + move64(); - inv_frame_len = 0; - move32(); + FOR( j = 0; j < st_fx->L_frame; j++ ) + { + w_temp = W_mac0_16_16( w_temp, speech[j], speech[j] ); + } + exp = W_norm( w_temp ); - SWITCH( st_fx->L_frame ) - { - case L_FRAME25_6k: - inv_frame_len = ONE_BY_L_FRAME25_6k_Q31; - move16(); - BREAK; - case L_FRAME: - inv_frame_len = ONE_BY_L_FRAME_Q31; - move16(); - BREAK; - case L_FRAME48k: - inv_frame_len = ONE_BY_L_FRAME48k_Q31; - move16(); - BREAK; - case 240: - inv_frame_len = ONE_BY_240_Q31; - move16(); - BREAK; - case L_FRAME32k: - inv_frame_len = ONE_BY_L_FRAME32k_Q31; - move16(); - BREAK; - case L_FRAME16k: - inv_frame_len = ONE_BY_L_FRAME16k_Q31; - move16(); - BREAK; - case L_FRAME8k: - inv_frame_len = ONE_BY_L_FRAME8k_Q31; - move16(); - BREAK; - case L_FRAME4k: - inv_frame_len = ONE_BY_L_FRAME4k_Q31; - move16(); - BREAK; - default: - inv_frame_len = divide3216( 1, st_fx->L_frame ); + inv_frame_len = 0; + move32(); + + SWITCH( st_fx->L_frame ) + { + case L_FRAME25_6k: + inv_frame_len = ONE_BY_L_FRAME25_6k_Q31; + move16(); + BREAK; + case L_FRAME: + inv_frame_len = ONE_BY_L_FRAME_Q31; + move16(); + BREAK; + case L_FRAME48k: + inv_frame_len = ONE_BY_L_FRAME48k_Q31; + move16(); + BREAK; + case 240: + inv_frame_len = ONE_BY_240_Q31; + move16(); + BREAK; + case L_FRAME32k: + inv_frame_len = ONE_BY_L_FRAME32k_Q31; + move16(); + BREAK; + case L_FRAME16k: + inv_frame_len = ONE_BY_L_FRAME16k_Q31; + move16(); + BREAK; + case L_FRAME8k: + inv_frame_len = ONE_BY_L_FRAME8k_Q31; + move16(); + BREAK; + case L_FRAME4k: + inv_frame_len = ONE_BY_L_FRAME4k_Q31; + move16(); + BREAK; + default: + inv_frame_len = divide3216( 1, st_fx->L_frame ); + } + L_ener = W_extract_h( W_shl( w_temp, exp ) ); /* Q = 2*Q_new+exp-32 */ + L_ener = Mpy_32_32( L_ener, inv_frame_len ); /* Q = 2*Q_new+exp-32 */ + L_ener = L_shl( L_ener, sub( 33, exp ) ); /* Q = 2*Q_new+1 */ } - L_ener = W_extract_h( W_shl( w_temp, exp ) ); /* Q = 2*Q_new+exp-32 */ - L_ener = Mpy_32_32( L_ener, inv_frame_len ); /* Q = 2*Q_new+exp-32 */ - L_ener = L_shl( L_ener, sub( 33, exp ) ); /* Q = 2*Q_new+1 */ hi = norm_l( L_ener ); lo = Log2_norm_lc( L_shl( L_ener, hi ) ); @@ -1374,6 +1424,7 @@ void CNG_enc_ivas_fx( * Find CNG spectral envelope * Find LSP median *-----------------------------------------------------------------*/ + test(); test(); IF( ( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, SID_1k75 ) ) && GE_16( hDtxEnc->cng_cnt, sub( hDtxEnc->cng_hist_size, 1 ) ) ) @@ -1456,6 +1507,7 @@ void CNG_enc_ivas_fx( * Quantize CNG spectral envelope (only in SID frame) * Quantize the LSF vector *-----------------------------------------------------------------*/ + *allow_cn_step = 0; move16(); test(); @@ -1493,7 +1545,6 @@ void CNG_enc_ivas_fx( } } - test(); IF( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, SID_1k75 ) ) { @@ -1531,6 +1582,7 @@ void CNG_enc_ivas_fx( * CNG spectral envelope update * Find A(z) coefficients *---------------------------------------------------------------------*/ + test(); test(); IF( ( st_fx->last_core_brate == FRAME_NO_DATA ) || EQ_32( st_fx->last_core_brate, SID_1k75 ) || EQ_32( st_fx->last_core_brate, SID_2k40 ) ) @@ -1635,17 +1687,42 @@ void CNG_enc_ivas_fx( IF( hTdCngEnc->burst_ho_cnt > 0 ) { +#ifdef HARM_NON_LINEARITY + IF( st_fx->element_mode == EVS_MONO ) + { + /**allow_cn_step |= ( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] > 4 * hTdCngEnc->lp_ener_fx ); */ + /*allow_cn_step |= (hDtxEnc->first_CNG || st->element_mode == EVS_MONO) && (hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener);*/ + /* (hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener); */ + L_tmp1 = L_shr( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr], 2 ); + IF( NE_16( lp_ener_thr_scale, 8 ) ) + { + L_tmp1 = L_add( L_tmp1, L_shr( hTdCngEnc->lp_ener_fx, 8 ) ); + } + L_tmp1 = L_sub( L_tmp1, hTdCngEnc->lp_ener_fx ); + } + ELSE + { + L_tmp1 = 0; + move16(); + } +#else /**allow_cn_step |= ( hDtxEnc->first_CNG || st->element_mode == EVS_MONO ) && ( hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener );*/ w_temp = W_msu_32_16( W_shl( W_deposit32_l( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] ), 2 ), hTdCngEnc->lp_ener_fx, lp_ener_thr_scale ); /*Q8*/ +#endif test(); test(); +#ifdef HARM_NON_LINEARITY + IF( ( hDtxEnc->first_CNG > 0 || ( st_fx->element_mode == EVS_MONO ) ) && L_tmp1 > 0 ) +#else IF( ( hDtxEnc->first_CNG > 0 || ( st_fx->element_mode == EVS_MONO ) ) && w_temp > 0 ) +#endif { *allow_cn_step = s_or( *allow_cn_step, 1 ); move16(); } } + test(); IF( *allow_cn_step == 0 && hTdCngEnc->ho_hist_size > 0 ) { @@ -1798,18 +1875,43 @@ void CNG_enc_ivas_fx( } } - dist = 0; /*Q15 */ - move32(); - max_dev = 0; /*Q15 */ - move32(); - FOR( i = 0; i < M; i++ ) +#ifdef HARM_NON_LINEARITY + IF( st_fx->element_mode == EVS_MONO ) { - dev = L_abs( L_sub( L_deposit_l( lsp_tmp[i] ), L_deposit_l( lsp_new[i] ) ) ); /*Q15 */ - dist = L_add( dist, dev ); /*Q15 */ - if ( GT_32( dev, max_dev ) ) + dist16 = 0; /*Q15 */ + move16(); + max_dev16 = 0; /*Q15 */ + move16(); + move16(); + FOR( i = 0; i < M; i++ ) { - max_dev = dev; - move32(); + dev16 = abs_s( sub( lsp_tmp[i], lsp_new[i] ) ); /*Q15 */ + dist16 = add_sat( dist16, dev16 ); /*Q15 */ + if ( GT_16( dev16, max_dev16 ) ) + { + max_dev16 = dev16; + move16(); + } + } + dist = L_deposit_l( dist16 ); + max_dev = L_deposit_l( max_dev16 ); + } + ELSE +#endif + { + dist = 0; /*Q15 */ + move32(); + max_dev = 0; /*Q15 */ + move32(); + FOR( i = 0; i < M; i++ ) + { + dev = L_abs( L_sub( L_deposit_l( lsp_tmp[i] ), L_deposit_l( lsp_new[i] ) ) ); /*Q15 */ + dist = L_add( dist, dev ); /*Q15 */ + if ( GT_32( dev, max_dev ) ) + { + max_dev = dev; + move32(); + } } } @@ -1865,6 +1967,7 @@ void CNG_enc_ivas_fx( Copy( lsp_new, hDtxEnc->lspCNG_fx, M ); /* use newly analyzed ISFs */ /* Q15 */ } } + IF( st_fx->Opt_AMR_WB != 0 ) { E_LPC_f_isp_a_conversion( hDtxEnc->lspCNG_fx, Aq, M ); @@ -1872,8 +1975,13 @@ void CNG_enc_ivas_fx( ELSE { E_LPC_f_lsp_a_conversion( hDtxEnc->lspCNG_fx, Aq, M ); - exp = sub( Q14, norm_s( Aq[0] ) ); - Scale_sig( Aq, M + 1, sub( Q12, exp ) ); // Q12 +#ifdef HARM_NON_LINEARITY + IF( st_fx->element_mode != EVS_MONO ) +#endif + { + exp = sub( Q14, norm_s( Aq[0] ) ); + Scale_sig( Aq, M + 1, sub( Q12, exp ) ); // Q12 + } } tmp_loop = shr( st_fx->L_frame, 6 ); @@ -1881,6 +1989,7 @@ void CNG_enc_ivas_fx( { Copy( Aq, &Aq[i * ( M + 1 )], M + 1 ); } + /*-----------------------------------------------------------------* * Find residual signal * Calculate residual signal energy per sample @@ -1956,72 +2065,127 @@ void CNG_enc_ivas_fx( /* calculate the residual signal energy */ /*enr = dotp( res, res, L_frame ) / L_frame; */ - w_temp = 1; - move64(); - FOR( j = 0; j < st_fx->L_frame; j++ ) +#ifdef HARM_NON_LINEARITY + IF( st_fx->element_mode == EVS_MONO ) { - w_temp = W_mac0_16_16( w_temp, res[j], res[j] ); - } - exp = W_norm( w_temp ); - - SWITCH( st_fx->L_frame ) - { - case L_FRAME25_6k: - inv_frame_len = ONE_BY_L_FRAME25_6k_Q31; - move16(); - BREAK; - case L_FRAME: - inv_frame_len = ONE_BY_L_FRAME_Q31; - move16(); - BREAK; - case L_FRAME48k: - inv_frame_len = ONE_BY_L_FRAME48k_Q31; - move16(); - BREAK; - case 240: - inv_frame_len = ONE_BY_240_Q31; - move16(); - BREAK; - case L_FRAME32k: - inv_frame_len = ONE_BY_L_FRAME32k_Q31; - move16(); - BREAK; - case L_FRAME16k: - inv_frame_len = ONE_BY_L_FRAME16k_Q31; - move16(); - BREAK; - case L_FRAME8k: - inv_frame_len = ONE_BY_L_FRAME8k_Q31; - move16(); - BREAK; - case L_FRAME4k: - inv_frame_len = ONE_BY_L_FRAME4k_Q31; - move16(); - BREAK; - default: - inv_frame_len = divide3216( 1, st_fx->L_frame ); - } - L_ener = W_extract_h( W_shl( w_temp, exp ) ); /* Q = 2*Q_new+exp-32 */ - L_ener = Mpy_32_32( L_ener, inv_frame_len ); /* Q = 2*Q_new+exp-32 */ - L_ener = L_shl( L_ener, sub( 33, exp ) ); /* Q = 2*Q_new+1 */ + Word16 maxv, scale; - /* convert log2 of residual signal energy */ - /*enr = (float)log10( enr + 0.1f ) / (float)log10( 2.0f ); */ - - IF( L_ener == 0 ) - { - enr = -850; /*log(0.1) base 2 in Q8*/ + maxv = 0; move16(); + FOR( i = 0; i < st_fx->L_frame; i++ ) + { + maxv = s_max( maxv, abs_s( res[i] ) ); + } + scale = norm_s( maxv ); + pt_res = res; + L_ener = L_deposit_l( 1 ); + IF( EQ_16( st_fx->L_frame, L_FRAME ) ) + { + FOR( j = 0; j < 128; j++ ) + { + tmpv = shl( *pt_res, scale ); + L_tmp = L_mult0( tmpv, tmpv ); + pt_res++; + tmpv = shl( *pt_res, scale ); + L_tmp = L_mac0_sat( L_tmp, tmpv, tmpv ); /* 2*(Q_new+scale) */ + pt_res++; + L_ener = L_add_sat( L_ener, L_shr( L_tmp, 7 ) ); /* 2*(Q_new+scale)+1, divide by L_frame done here */ + } + } + ELSE /* L_FRAME16k */ + { + FOR( j = 0; j < 160; j++ ) + { + tmpv = shl( *pt_res, scale ); + L_tmp = L_mult0( tmpv, tmpv ); + pt_res++; + tmpv = shl( *pt_res, scale ); + L_tmp = L_mac0_sat( L_tmp, tmpv, tmpv ); /* 2*(Q_new+scale) */ + pt_res++; + L_ener = L_add_sat( L_ener, L_shr( Mult_32_16( L_tmp, 26214 /* 256/320, Q15 */ ), 7 ) ); /* 2*(Q_new+scale)+15+1-16+1, divide by L_frame done here */ + } + } + + /* convert log2 of residual signal energy */ + /*enr = (float)log10( enr + 0.1f ) / (float)log10( 2.0f ); */ + hi = norm_l( L_ener ); + lo = Log2_norm_lc( L_shl( L_ener, hi ) ); + hi = sub( 29, hi ); /* log2 exp in Q2*(Q_new+scale) */ + hi = sub( hi, shl( add( Q_new, scale ), 1 ) ); /* Q0 */ + L_tmp = L_Comp( hi, lo ); /* Q16 */ + enr = round_fx( L_shl( L_tmp, 8 ) ); /* Q8 (16+8-16) */ } ELSE +#endif { - hi = norm_l( L_ener ); - lo = Log2_norm_lc( L_shl( L_ener, hi ) ); - hi = sub( 29, hi ); /* log2 exp in Q2*Q_new */ - hi = sub( hi, shl( Q_new, 1 ) ); /* Q0 */ - L_tmp = L_Comp( hi, lo ); /* Q16 */ - enr = round_fx( L_shl( L_tmp, 8 ) ); /* Q8 (16+8-16) */ + w_temp = 1; + move64(); + FOR( j = 0; j < st_fx->L_frame; j++ ) + { + w_temp = W_mac0_16_16( w_temp, res[j], res[j] ); + } + exp = W_norm( w_temp ); + + SWITCH( st_fx->L_frame ) + { + case L_FRAME25_6k: + inv_frame_len = ONE_BY_L_FRAME25_6k_Q31; + move16(); + BREAK; + case L_FRAME: + inv_frame_len = ONE_BY_L_FRAME_Q31; + move16(); + BREAK; + case L_FRAME48k: + inv_frame_len = ONE_BY_L_FRAME48k_Q31; + move16(); + BREAK; + case 240: + inv_frame_len = ONE_BY_240_Q31; + move16(); + BREAK; + case L_FRAME32k: + inv_frame_len = ONE_BY_L_FRAME32k_Q31; + move16(); + BREAK; + case L_FRAME16k: + inv_frame_len = ONE_BY_L_FRAME16k_Q31; + move16(); + BREAK; + case L_FRAME8k: + inv_frame_len = ONE_BY_L_FRAME8k_Q31; + move16(); + BREAK; + case L_FRAME4k: + inv_frame_len = ONE_BY_L_FRAME4k_Q31; + move16(); + BREAK; + default: + inv_frame_len = divide3216( 1, st_fx->L_frame ); + } + L_ener = W_extract_h( W_shl( w_temp, exp ) ); /* Q = 2*Q_new+exp-32 */ + L_ener = Mpy_32_32( L_ener, inv_frame_len ); /* Q = 2*Q_new+exp-32 */ + L_ener = L_shl( L_ener, sub( 33, exp ) ); /* Q = 2*Q_new+1 */ + + /* convert log2 of residual signal energy */ + /*enr = (float)log10( enr + 0.1f ) / (float)log10( 2.0f ); */ + + IF( L_ener == 0 ) + { + enr = -850; /*log(0.1) base 2 in Q8*/ + move16(); + } + ELSE + { + hi = norm_l( L_ener ); + lo = Log2_norm_lc( L_shl( L_ener, hi ) ); + hi = sub( 29, hi ); /* log2 exp in Q2*Q_new */ + hi = sub( hi, shl( Q_new, 1 ) ); /* Q0 */ + L_tmp = L_Comp( hi, lo ); /* Q16 */ + enr = round_fx( L_shl( L_tmp, 8 ) ); /* Q8 (16+8-16) */ + } } + /* update the circular buffer of old energies */ hTdCngEnc->cng_ener_hist_fx[hTdCngEnc->cng_hist_ptr] = enr; move16(); /* Q8 */ @@ -2029,6 +2193,7 @@ void CNG_enc_ivas_fx( /*-----------------------------------------------------------------* * Quantize residual signal energy (only in SID frame) *-----------------------------------------------------------------*/ + test(); IF( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, SID_1k75 ) ) { @@ -2368,6 +2533,8 @@ void CNG_enc_ivas_fx( return; } + + /*---------------------------------------------------------------------* * swb_CNG_enc() * @@ -2948,7 +3115,6 @@ static Word16 shb_DTX_ivas_fx( { Word16 i; Word16 update; - Word16 allow_cn_step = 0; move16(); Word16 shb_old_speech_fx[( ACELP_LOOK_12k8 + L_SUBFR + L_FRAME ) * 5 / 4]; @@ -3011,7 +3177,11 @@ static Word16 shb_DTX_ivas_fx( } ELSE { +#ifdef HARM_NON_LINEARITY + att_fx = 1664; /*6.5 in Q8*/ +#else att_fx = -1664; // Q8 +#endif move16(); } @@ -3019,8 +3189,6 @@ static Word16 shb_DTX_ivas_fx( IF( st->hDtxEnc->first_CNG == 0 ) { - - hTdCngEnc->mov_wb_cng_ener_fx = log_wb_ener_fx; // Q8 hTdCngEnc->mov_shb_cng_ener_fx = log_shb_ener_fx; // Q8 hTdCngEnc->last_wb_cng_ener_fx = log_wb_ener_fx; // Q8 @@ -3030,6 +3198,7 @@ static Word16 shb_DTX_ivas_fx( move16(); move16(); } + IF( GT_16( abs_s( sub( log_wb_ener_fx, hTdCngEnc->mov_wb_cng_ener_fx ) ), 3072 /*12 in Q8*/ ) ) { allow_cn_step = 1; @@ -3067,47 +3236,45 @@ static Word16 shb_DTX_ivas_fx( hTdCngEnc->shb_NO_DATA_cnt = add( hTdCngEnc->shb_NO_DATA_cnt, 1 ); update = 0; - move16(); move16(); IF( EQ_32( st->core_brate, SID_2k40 ) ) - { - test(); - test(); - test(); - IF( st->hDtxEnc->first_CNG == 0 ) - { - update = 1; - move16(); - } - ELSE IF( hTdCngEnc->shb_cng_ini_cnt > 0 ) - { - hTdCngEnc->shb_cng_ini_cnt = sub( hTdCngEnc->shb_cng_ini_cnt, 1 ); - update = 1; - move16(); - move16(); - } - ELSE IF( EQ_16( hTdCngEnc->last_vad, 1 ) ) - { - update = 1; - move16(); - } - ELSE IF( GE_16( hTdCngEnc->shb_NO_DATA_cnt, 100 ) ) - { - update = 1; - move16(); - } - ELSE IF( GT_16( abs_s( sub( sub( hTdCngEnc->mov_wb_cng_ener_fx, hTdCngEnc->mov_shb_cng_ener_fx ), sub( hTdCngEnc->last_wb_cng_ener_fx, hTdCngEnc->last_shb_cng_ener_fx ) ) ), 768 ) ) - { - update = 1; - move16(); - } - ELSE IF( ( GE_16( st->bwidth, SWB ) && LT_16( hTdCngEnc->last_SID_bwidth, SWB ) ) || ( LT_16( st->bwidth, SWB ) && GE_16( hTdCngEnc->last_SID_bwidth, SWB ) ) ) - { - update = 1; - move16(); - } + { test(); + test(); + test(); + IF( st->hDtxEnc->first_CNG == 0 ) + { + update = 1; + move16(); + } + ELSE IF( hTdCngEnc->shb_cng_ini_cnt > 0 ) + { + hTdCngEnc->shb_cng_ini_cnt = sub( hTdCngEnc->shb_cng_ini_cnt, 1 ); + update = 1; + move16(); + move16(); + } + ELSE IF( EQ_16( hTdCngEnc->last_vad, 1 ) ) + { + update = 1; + move16(); + } + ELSE IF( GE_16( hTdCngEnc->shb_NO_DATA_cnt, 100 ) ) + { + update = 1; + move16(); + } + ELSE IF( GT_16( abs_s( sub( sub( hTdCngEnc->mov_wb_cng_ener_fx, hTdCngEnc->mov_shb_cng_ener_fx ), sub( hTdCngEnc->last_wb_cng_ener_fx, hTdCngEnc->last_shb_cng_ener_fx ) ) ), 768 ) ) + { + update = 1; + move16(); + } + ELSE IF( ( GE_16( st->bwidth, SWB ) && LT_16( hTdCngEnc->last_SID_bwidth, SWB ) ) || ( LT_16( st->bwidth, SWB ) && GE_16( hTdCngEnc->last_SID_bwidth, SWB ) ) ) + { + update = 1; + move16(); + } hTdCngEnc->last_SID_bwidth = st->bwidth; move16(); diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 82443f044..d60fdc565 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -1781,6 +1781,7 @@ void reset_rf_indices_fx( ); void CNG_enc_fx( +#ifndef HARM_NON_LINEARITY Encoder_State *st_fx, /* i/o: State structure */ Word16 Aq[], /* o : LP coefficients Q12 */ const Word16 *speech, /* i : pointer to current frame i speech buffer Q_new */ @@ -1794,10 +1795,10 @@ void CNG_enc_fx( Word16 *sid_bw ); void CNG_enc_ivas_fx( - Encoder_State *st_fx, /* i/o: State structure */ - Word16 Aq[], /* o : LP coefficients Q12 */ - const Word16 *speech, /* i : pointer to current frame i speech buffer Q_new */ - // Word32 L_enr, /* i : residual energy from Levinson-Durbin Q6 */ +#endif + Encoder_State *st_fx, /* i/o: State structure */ + Word16 Aq[], /* o : LP coefficients Q12 */ + const Word16 *speech, /* i : pointer to current frame i speech buffer Q_new */ const Word16 *lsp_mid, /* i : mid frame LSPs Q15 */ Word16 *lsp_new, /* i/o: current frame ISPs Q15 */ Word16 *lsf_new, /* i/o: current frame ISFs Qlog2(2.56) */ -- GitLab From ffe011f5103370e7356278d4033507445fd7cd26 Mon Sep 17 00:00:00 2001 From: vaclav Date: Sun, 18 Jan 2026 18:02:47 +0100 Subject: [PATCH 06/11] harmonize swb_CNG_enc_fx() --- lib_com/prot_fx.h | 3 +- lib_enc/acelp_core_enc_fx.c | 5 ++ lib_enc/cng_enc_fx.c | 152 ++++++++++++++++++++++++------------ lib_enc/cod_tcx_fx.c | 4 + lib_enc/hq_hr_enc_fx.c | 4 + lib_enc/igf_enc_fx.c | 4 + lib_enc/ivas_core_enc_fx.c | 4 + lib_enc/prot_fx_enc.h | 3 +- 8 files changed, 128 insertions(+), 51 deletions(-) diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 65a9079ac..6f48321b6 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -10721,12 +10721,13 @@ void writeTCXparam_fx( const Word16 target_bitsTCX10[2], const Word16 pre_past_flag ); +#ifndef HARM_NON_LINEARITY void calculate_hangover_attenuation_gain_ivas_fx( Encoder_State *st, /* i : encoder state structure */ Word16 *att, /* o : attenuation factor */ const Word16 vad_hover_flag /* i : VAD hangover flag */ ); - +#endif void init_coder_ace_plus_ivas_fx( Encoder_State *st, /* i : Encoder state */ const Word32 last_total_brate, /* i : last total bitrate */ diff --git a/lib_enc/acelp_core_enc_fx.c b/lib_enc/acelp_core_enc_fx.c index 67def7a08..104e9bc96 100644 --- a/lib_enc/acelp_core_enc_fx.c +++ b/lib_enc/acelp_core_enc_fx.c @@ -1266,7 +1266,12 @@ ivas_error acelp_core_enc_ivas_fx( *---------------------------------------------------------------*/ calc_residu_fx( st, inp, res_fx, Aq ); + +#ifdef HARM_NON_LINEARITY + calculate_hangover_attenuation_gain_fx( st, &att_fx, vad_hover_flag ); +#else calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag ); +#endif IF( NE_16( att_fx, 32767 /* ONE_IN_Q15 */ ) ) { diff --git a/lib_enc/cng_enc_fx.c b/lib_enc/cng_enc_fx.c index 10275fb85..730ad75c2 100644 --- a/lib_enc/cng_enc_fx.c +++ b/lib_enc/cng_enc_fx.c @@ -28,8 +28,10 @@ static void shb_CNG_encod_fx( Encoder_State *st_fx, const Word16 update_fx ); static Word16 shb_DTX_fx( Encoder_State *st_fx, const Word16 *shb_speech_fx, const Word16 *syn_12k8_16k ); +#ifndef HARM_NON_LINEARITY static Word16 shb_DTX_ivas_fx( Encoder_State *st, const Word16 *shb_speech_fx, const Word16 *syn_12k8_16k_fx ); static void shb_CNG_encod_ivas_fx( Encoder_State *st, const Word16 update ); +#endif /*---------------------------------------------------------------------* @@ -1408,6 +1410,7 @@ void CNG_enc_fx( move16(); } } + /* update the pointer to circular buffer of old LSP vectors */ hTdCngEnc->cng_hist_ptr = add( hTdCngEnc->cng_hist_ptr, 1 ); move16(); @@ -2513,6 +2516,7 @@ void CNG_enc_fx( /*-----------------------------------------------------------------* * Updates *-----------------------------------------------------------------*/ + /* update the SID frames counter */ test(); IF( EQ_32( st_fx->core_brate, SID_2k40 ) || EQ_32( st_fx->core_brate, SID_1k75 ) ) @@ -2534,7 +2538,7 @@ void CNG_enc_fx( return; } - +#ifndef HARM_NON_LINEARITY /*---------------------------------------------------------------------* * swb_CNG_enc() * @@ -2657,6 +2661,7 @@ static void shb_CNG_encod_fx( * * Decide if encoding SHB SID or not *---------------------------------------------------------------------*/ + static Word16 shb_DTX_fx( Encoder_State *st_fx, /* i/o: State structure */ const Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz (Q0) */ @@ -2836,13 +2841,16 @@ static Word16 shb_DTX_fx( return ( update_fx ); } +#endif /*---------------------------------------------------------------------* * calculate_hangover_attenuation_gain_fx() * * *---------------------------------------------------------------------*/ + void calculate_hangover_attenuation_gain_fx( +#ifndef HARM_NON_LINEARITY Encoder_State *st, /* i : encoder state structure */ Word16 *att, /* o : attenuation factor Q15 */ const Word16 vad_hover_flag /* i : VAD hangover flag */ @@ -2885,9 +2893,10 @@ void calculate_hangover_attenuation_gain_fx( } void calculate_hangover_attenuation_gain_ivas_fx( - Encoder_State *st, /* i : encoder state structure */ - Word16 *att, /* o : attenuation factor Q15 */ - const Word16 vad_hover_flag /* i : VAD hangover flag */ +#endif + Encoder_State *st, /* i : encoder state structure */ + Word16 *att, /* o : attenuation factor Q15 */ + const Word16 vad_hover_flag /* i : VAD hangover flag */ ) { Word16 lim, result_e; @@ -2909,6 +2918,22 @@ void calculate_hangover_attenuation_gain_ivas_fx( *att = extract_h( L_shl_sat( L_tmp, result_e ) ); move16(); } +#ifdef HARM_NON_LINEARITY + ELSE IF( st->element_mode == EVS_MONO ) + { + Word16 offset = 5; + move16(); + test(); + if ( EQ_16( st->bwidth, WB ) && st->hDtxEnc->CNG_mode >= 0 ) + { + offset = st->hDtxEnc->CNG_mode; + move16(); + } + assert( st->hTdCngEnc->burst_ho_cnt > 0 ); + *att = CNG_burst_att_fx[offset][st->hTdCngEnc->burst_ho_cnt - 1]; /*Q15*/ + move16(); + } +#endif ELSE { test(); @@ -2938,10 +2963,21 @@ void calculate_hangover_attenuation_gain_ivas_fx( return; } + +#ifdef HARM_NON_LINEARITY +/*---------------------------------------------------------------------* + * swb_CNG_enc() + * + * SWB DTX/CNG encoding + *---------------------------------------------------------------------*/ + +void swb_CNG_enc_fx( +#else void swb_CNG_enc_ivas_fx( +#endif Encoder_State *st, /* i/o: State structure */ - const Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ - const Word16 *syn_12k8_16k_fx /* i : ACELP core synthesis at 12.8kHz or 16kHz Q0 */ + const Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ + const Word16 *syn_12k8_16k_fx /* i : ACELP core synthesis at 12.8kHz or 16kHz Q0 */ ) { Word16 shb_SID_updt; @@ -2955,10 +2991,18 @@ void swb_CNG_enc_ivas_fx( IF( GE_32( st->input_Fs, L_FRAME32k * FRAMES_PER_SEC ) ) { /* decide if SHB SID encoding or not */ +#ifdef HARM_NON_LINEARITY + shb_SID_updt = shb_DTX_fx( st, shb_speech_fx, syn_12k8_16k_fx ); +#else shb_SID_updt = shb_DTX_ivas_fx( st, shb_speech_fx, syn_12k8_16k_fx ); +#endif /* SHB CNG encoding */ +#ifdef HARM_NON_LINEARITY + shb_CNG_encod_fx( st, shb_SID_updt ); +#else shb_CNG_encod_ivas_fx( st, shb_SID_updt ); +#endif } ELSE IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) && EQ_32( st->core_brate, SID_2k40 ) ) { @@ -2988,7 +3032,11 @@ void swb_CNG_enc_ivas_fx( * SID parameters encoding for SHB signal *---------------------------------------------------------------------*/ +#ifdef HARM_NON_LINEARITY +static void shb_CNG_encod_fx( +#else static void shb_CNG_encod_ivas_fx( +#endif Encoder_State *st, /* i/o: State structure */ const Word16 update /* i : SID update flag */ ) @@ -2996,17 +3044,17 @@ static void shb_CNG_encod_ivas_fx( Word16 idx_ener = 0; move16(); BSTR_ENC_HANDLE hBstr = st->hBstr; - Word16 ener_mid_dec_thr_fx; IF( EQ_16( update, 1 ) ) { IF( st->element_mode == EVS_MONO ) { - /* 6.0 in Q8 -> 1510 */ - /* 0.9 in Q15 29491 */ - /* ( 1 / log10(2.0) ) * 0.1 in Q15 ->10886 */ +#ifdef HARM_NON_LINEARITY + idx_ener = shr( add( mult( st->hTdCngEnc->mov_shb_cng_ener_fx, 9797 ), 1510 ), 8 ); /* Q0 */ +#else idx_ener = shr( mult( add( mult( st->hTdCngEnc->mov_shb_cng_ener_fx, 10886 ), 1510 ), 29491 ), 8 ); /* Q0 */ +#endif } ELSE { @@ -3017,7 +3065,6 @@ static void shb_CNG_encod_ivas_fx( idx_ener = shr( mult( add( mult( st->hTdCngEnc->mov_shb_cng_ener_fx, 10886 ), 1510 ), 22938 ), 8 ); /* Q0 */ } - if ( LT_16( st->bwidth, SWB ) ) { idx_ener = 0; @@ -3078,6 +3125,7 @@ static void shb_CNG_encod_ivas_fx( push_indice( hBstr, IND_SHB_CNG_GAIN, idx_ener, 4 ); push_indice( hBstr, IND_SID_BW, 1, 1 ); delete_indice( hBstr, IND_CNG_ENV1 ); + IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) ) { push_indice( st->hBstr, IND_BWIDTH, st->bwidth, 2 ); @@ -3086,6 +3134,7 @@ static void shb_CNG_encod_ivas_fx( { push_indice( hBstr, IND_UNUSED, 0, 2 ); } + st->hTdCngEnc->ho_sid_bw = L_shl( L_and( st->hTdCngEnc->ho_sid_bw, (Word32) 0x3fffffffL ), 1 ); st->hTdCngEnc->ho_sid_bw = L_or( st->hTdCngEnc->ho_sid_bw, 0x1L ); move32(); @@ -3101,16 +3150,21 @@ static void shb_CNG_encod_ivas_fx( return; } + /*---------------------------------------------------------------------* * shb_DTX() * * Decide if encoding SHB SID or not *---------------------------------------------------------------------*/ +#ifdef HARM_NON_LINEARITY +static Word16 shb_DTX_fx( +#else static Word16 shb_DTX_ivas_fx( +#endif Encoder_State *st, /* i/o: State structure */ - const Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ - const Word16 *syn_12k8_16k_fx /* i : ACELP core synthesis at 12.8kHz or 16kHz Q0 */ + const Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ + const Word16 *syn_12k8_16k_fx /* i : ACELP core synthesis at 12.8kHz or 16kHz Q0 */ ) { Word16 i; @@ -3163,7 +3217,6 @@ static Word16 shb_DTX_ivas_fx( exp = sub( 30 - 1, exp ); shb_ener_fx = Mpy_32_16( exp, fra, LG10 ); - test(); IF( EQ_16( st->element_mode, IVAS_SCE ) || EQ_16( st->element_mode, IVAS_CPE_DFT ) ) { @@ -3240,41 +3293,42 @@ static Word16 shb_DTX_ivas_fx( move16(); IF( EQ_32( st->core_brate, SID_2k40 ) ) - { test(); - test(); - test(); - IF( st->hDtxEnc->first_CNG == 0 ) - { - update = 1; - move16(); - } - ELSE IF( hTdCngEnc->shb_cng_ini_cnt > 0 ) - { - hTdCngEnc->shb_cng_ini_cnt = sub( hTdCngEnc->shb_cng_ini_cnt, 1 ); - update = 1; - move16(); - move16(); - } - ELSE IF( EQ_16( hTdCngEnc->last_vad, 1 ) ) - { - update = 1; - move16(); - } - ELSE IF( GE_16( hTdCngEnc->shb_NO_DATA_cnt, 100 ) ) - { - update = 1; - move16(); - } - ELSE IF( GT_16( abs_s( sub( sub( hTdCngEnc->mov_wb_cng_ener_fx, hTdCngEnc->mov_shb_cng_ener_fx ), sub( hTdCngEnc->last_wb_cng_ener_fx, hTdCngEnc->last_shb_cng_ener_fx ) ) ), 768 ) ) - { - update = 1; - move16(); - } - ELSE IF( ( GE_16( st->bwidth, SWB ) && LT_16( hTdCngEnc->last_SID_bwidth, SWB ) ) || ( LT_16( st->bwidth, SWB ) && GE_16( hTdCngEnc->last_SID_bwidth, SWB ) ) ) - { - update = 1; - move16(); - } + { + test(); + test(); + test(); + IF( st->hDtxEnc->first_CNG == 0 ) + { + update = 1; + move16(); + } + ELSE IF( hTdCngEnc->shb_cng_ini_cnt > 0 ) + { + hTdCngEnc->shb_cng_ini_cnt = sub( hTdCngEnc->shb_cng_ini_cnt, 1 ); + update = 1; + move16(); + move16(); + } + ELSE IF( EQ_16( hTdCngEnc->last_vad, 1 ) ) + { + update = 1; + move16(); + } + ELSE IF( GE_16( hTdCngEnc->shb_NO_DATA_cnt, 100 ) ) + { + update = 1; + move16(); + } + ELSE IF( GT_16( abs_s( sub( sub( hTdCngEnc->mov_wb_cng_ener_fx, hTdCngEnc->mov_shb_cng_ener_fx ), sub( hTdCngEnc->last_wb_cng_ener_fx, hTdCngEnc->last_shb_cng_ener_fx ) ) ), 768 ) ) + { + update = 1; + move16(); + } + ELSE IF( ( GE_16( st->bwidth, SWB ) && LT_16( hTdCngEnc->last_SID_bwidth, SWB ) ) || ( LT_16( st->bwidth, SWB ) && GE_16( hTdCngEnc->last_SID_bwidth, SWB ) ) ) + { + update = 1; + move16(); + } hTdCngEnc->last_SID_bwidth = st->bwidth; move16(); diff --git a/lib_enc/cod_tcx_fx.c b/lib_enc/cod_tcx_fx.c index e1a246817..8e40d9530 100644 --- a/lib_enc/cod_tcx_fx.c +++ b/lib_enc/cod_tcx_fx.c @@ -3617,7 +3617,11 @@ void QuantizeTCXSpectrum_fx( test(); IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_CPE_TD ) ) { +#ifdef HARM_NON_LINEARITY + calculate_hangover_attenuation_gain_fx( st, &att_fx, vad_hover_flag ); +#else calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag ); +#endif *gain_tcx_fx = mult( *gain_tcx_fx, att_fx ); move16(); } diff --git a/lib_enc/hq_hr_enc_fx.c b/lib_enc/hq_hr_enc_fx.c index d9a1dd5b7..8f70a5b36 100644 --- a/lib_enc/hq_hr_enc_fx.c +++ b/lib_enc/hq_hr_enc_fx.c @@ -337,7 +337,11 @@ void hq_hr_enc_ivas_fx( test(); IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_CPE_TD ) ) { +#ifdef HARM_NON_LINEARITY + calculate_hangover_attenuation_gain_fx( st, &att_fx, vad_hover_flag ); +#else calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag ); +#endif v_multc_att32( t_audio_fx, att_fx, t_audio_fx, sfm_end[( num_sfm - 1 )] ); /* Q12 */ } diff --git a/lib_enc/igf_enc_fx.c b/lib_enc/igf_enc_fx.c index 6c55e8b31..0adf23deb 100755 --- a/lib_enc/igf_enc_fx.c +++ b/lib_enc/igf_enc_fx.c @@ -4137,7 +4137,11 @@ void IGFEncApplyMono_ivas_fx( test(); IF( EQ_16( st->element_mode, IVAS_CPE_DFT ) || EQ_16( st->element_mode, IVAS_CPE_TD ) ) { +#ifdef HARM_NON_LINEARITY + calculate_hangover_attenuation_gain_fx( st, &att_fx, vad_hover_flag ); +#else calculate_hangover_attenuation_gain_ivas_fx( st, &att_fx, vad_hover_flag ); +#endif } IGF_CalculateEnvelope_ivas_fx( st->hIGFEnc, pMDCTSpectrum_fx, e_mdct, pPowerSpectrumParameter_fx, pPowerSpectrumParameter_exp, igfGridIdx, st->hTranDet->transientDetector.bIsAttackPresent, last_core_acelp, st->element_mode, att_fx ); diff --git a/lib_enc/ivas_core_enc_fx.c b/lib_enc/ivas_core_enc_fx.c index 7dd2bcb04..8d4d4fbd7 100644 --- a/lib_enc/ivas_core_enc_fx.c +++ b/lib_enc/ivas_core_enc_fx.c @@ -796,7 +796,11 @@ ivas_error ivas_core_enc_fx( { /* SHB DTX/CNG encoder */ Copy_Scale_sig_32_16( shb_speech_fx32, shb_speech_fx, L_FRAME16k, negate( Q_shb_spch ) ); // Q0 +#ifdef HARM_NON_LINEARITY + swb_CNG_enc_fx( st, shb_speech_fx /* Unmodified */, old_syn_12k8_16k_fx[n] ); +#else swb_CNG_enc_ivas_fx( st, shb_speech_fx /* Unmodified */, old_syn_12k8_16k_fx[n] ); +#endif } /*-------------------------------------------------------------------* diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index d60fdc565..1738de1dd 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -645,12 +645,13 @@ void swb_CNG_enc_fx( const Word16 *syn_12k8_16k_fx /* i : ACELP core synthesis at 12.8kHz or 16kHz */ ); +#ifndef HARM_NON_LINEARITY void swb_CNG_enc_ivas_fx( Encoder_State *st, /* i/o: State structure */ const Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ const Word16 *syn_12k8_16k_fx /* i : ACELP core synthesis at 12.8kHz or 16kHz Q0 */ ); - +#endif void swb_pre_proc_fx( Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 *input_fx, /* i : original i signal */ -- GitLab From ab189c20b0f2f52c6ac55087af6c38ae2ca9cbdf Mon Sep 17 00:00:00 2001 From: vaclav Date: Sun, 18 Jan 2026 18:06:57 +0100 Subject: [PATCH 07/11] comment --- lib_com/options.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_com/options.h b/lib_com/options.h index eb3a6128c..8d7e4d3d6 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -102,7 +102,7 @@ #define HARM_CORECODER_UPDT /* VA: basop issue 2342: Remove duplicated code in core-coder common update functions */ #define HARM_CORE_SW /* VA: basop issue 2314: Remove duplicated code in the core switching */ #define HARM_PREPROC /* VA: basop issue 2339: Remove duplicated code in the core-coder DTX */ -#define HARM_NON_LINEARITY /* VA: basop issue 2345: Remove duplicated code in core-coder: non_linearity_fx() */ +#define HARM_NON_LINEARITY /* VA: basop issue 2345: Remove duplicated code in core-coder: non_linearity_fx() and LP CNG */ /* #################### End BE switches ################################## */ -- GitLab From 83a26e8a49144361cc9a086ad745e3ec6f1a16b3 Mon Sep 17 00:00:00 2001 From: vaclav Date: Sun, 18 Jan 2026 18:41:28 +0100 Subject: [PATCH 08/11] harmonize swb_CNG_dec_fx() --- lib_com/prot_fx.h | 3 +- lib_dec/cng_dec_fx.c | 390 +++++++++++++++++++++++++++---------- lib_dec/ivas_core_dec_fx.c | 4 + 3 files changed, 292 insertions(+), 105 deletions(-) diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 6f48321b6..604b7c97b 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -6167,6 +6167,7 @@ void swb_CNG_dec_fx( const Word16 Qsyn /* i : Q value of ACELP core synthesis */ ); +#ifndef HARM_NON_LINEARITY void swb_CNG_dec_ivas_fx( Decoder_State *st_fx, /* i/o: State structure */ const Word16 *synth_fx, /* i : ACELP core synthesis at 32kHz Qsyn*/ @@ -6174,7 +6175,7 @@ void swb_CNG_dec_ivas_fx( const Word16 sid_bw, /* i : 0-NB/WB, 1-SWB SID Q0*/ const Word16 Qsyn /* i : Q value of ACELP core synthesis */ ); - +#endif void td_cng_dec_init_fx( DEC_CORE_HANDLE st /* i/o: decoder state structure */ ); diff --git a/lib_dec/cng_dec_fx.c b/lib_dec/cng_dec_fx.c index 0006bacbb..a88605896 100644 --- a/lib_dec/cng_dec_fx.c +++ b/lib_dec/cng_dec_fx.c @@ -9,9 +9,11 @@ #include "prot_fx.h" #include "ivas_cnst.h" +#ifndef HARM_NON_LINEARITY /*Temporary location to be move in prot* when merge is done*/ void E_LPC_f_isp_a_conversion( const Word16 *isp, Word16 *a, const Word16 m ); void E_LPC_f_lsp_a_conversion( const Word16 *isp, Word16 *a, const Word16 m ); +#endif /*-----------------------------------------------------------------* * Local function prototypes @@ -19,7 +21,10 @@ void E_LPC_f_lsp_a_conversion( const Word16 *isp, Word16 *a, const Word16 m ); static void shb_CNG_decod_fx( Decoder_State *st_fx, const Word16 *synth_fx, Word16 *shb_synth_fx, const Word16 sid_bw, const Word16 Qsyn ); +#ifndef HARM_NON_LINEARITY static void shb_CNG_decod_ivas_fx( Decoder_State *st_fx, const Word16 *synth_fx, Word16 *shb_synth_fx, const Word16 sid_bw, const Word16 Qsyn ); +#endif + /*-----------------------------------------------------------------* * CNG_dec_fx() @@ -723,7 +728,11 @@ void swb_CNG_dec_fx( { st_fx->last_vad_fx = 1; move16(); +#ifdef HARM_NON_LINEARITY + st_fx->hTdCngDec->burst_cnt_fx = add_sat( st_fx->hTdCngDec->burst_cnt_fx, 1 ); +#else st_fx->hTdCngDec->burst_cnt_fx = add( st_fx->hTdCngDec->burst_cnt_fx, 1 ); +#endif move16(); if ( GT_16( st_fx->hTdCngDec->burst_cnt_fx, 10 ) ) { @@ -735,6 +744,7 @@ void swb_CNG_dec_fx( return; } +#ifndef HARM_NON_LINEARITY void swb_CNG_dec_ivas_fx( Decoder_State *st_fx, /* i/o: State structure */ const Word16 *synth_fx, /* i : ACELP core synthesis at 32kHz Qsyn*/ @@ -772,6 +782,7 @@ void swb_CNG_dec_ivas_fx( return; } +#endif /*---------------------------------------------------------------------* * shb_CNG_decod() @@ -780,12 +791,17 @@ void swb_CNG_dec_ivas_fx( *---------------------------------------------------------------------*/ static void shb_CNG_decod_fx( - Decoder_State *st_fx, /* i/o: State structure */ +#ifdef HARM_NON_LINEARITY + Decoder_State *st, /* i/o: State structure */ +#else + Decoder_State *st_fx, /* i/o: State structure */ +#endif const Word16 *synth_fx, /* i : ACELP core synthesis at 32kHz Qsyn*/ Word16 *shb_synth_fx, /* o : high-band CNG synthesis Qx*/ const Word16 sid_bw, /* i : 0-NB/WB, 1-SWB SID Q0*/ const Word16 Qsyn /* i : Q value of ACELP core synthesis */ ) +#ifndef HARM_NON_LINEARITY { Word16 i; Word16 idx_ener_fx; @@ -1052,6 +1068,7 @@ static void shb_CNG_decod_ivas_fx( Word16 *shb_synth_fx, /* o : high-band CNG synthesis Qx*/ const Word16 sid_bw, /* i : 0-NB/WB, 1-SWB SID Q0*/ const Word16 Qsyn ) +#endif { Word16 i; Word16 idx_ener; @@ -1074,6 +1091,9 @@ static void shb_CNG_decod_ivas_fx( Word16 exp, exp1; Word16 fra; Word32 L_tmp; +#ifdef HARM_NON_LINEARITY + Word16 tmp_16, tmp_16_2, step_fx16, gain_fx16, ener_fx16; +#endif Word16 allow_cn_step_fx; Word16 q; TD_BWE_DEC_HANDLE hBWE_TD; @@ -1099,8 +1119,14 @@ static void shb_CNG_decod_ivas_fx( /* de-quantization of SHB CNG parameters */ IF( st->element_mode == EVS_MONO ) { +#ifdef HARM_NON_LINEARITY + L_tmp = L_mult( idx_ener, 27400 ); /*Q14 */ + hTdCngDec->last_shb_cng_ener_fx = extract_l( L_shr( L_sub( L_tmp, 295924 ), 6 ) ); /*Q8 */ + move16(); +#else hTdCngDec->last_shb_cng_ener_fx_32 = L_sub( L_mult0( idx_ener, 6850 ), 36991 ); // Q11 move32(); +#endif } ELSE { @@ -1114,10 +1140,25 @@ static void shb_CNG_decod_ivas_fx( interp_fx = s_min( hTdCngDec->shb_dtx_count_fx, 32 ); interp_fx = shl_sat( interp_fx, 10 ); /*Q15*/ - FOR( i = 0; i < LPC_SHB_ORDER; i++ ) +#ifdef HARM_NON_LINEARITY + IF( st->element_mode == EVS_MONO ) { - shb_lspCNG_fx[i] = add( mult_r( interp_fx, hTdCngDec->lsp_shb_prev_fx[i] ), mult_r( sub( 32767, interp_fx ), hTdCngDec->lsp_shb_prev_prev_fx[i] ) ); // Q14 - move16(); + FOR( i = 0; i < LPC_SHB_ORDER; i++ ) + { + tmp_16_2 = mult( interp_fx, hTdCngDec->lsp_shb_prev_fx[i] ); /*Q14*/ + tmp_16 = mult( sub( 32767, interp_fx ), hTdCngDec->lsp_shb_prev_prev_fx[i] ); /*Q14*/ + shb_lspCNG_fx[i] = add( tmp_16_2, tmp_16 ); + move16(); /*Q14*/ + } + } + ELSE +#endif + { + FOR( i = 0; i < LPC_SHB_ORDER; i++ ) + { + shb_lspCNG_fx[i] = add( mult_r( interp_fx, hTdCngDec->lsp_shb_prev_fx[i] ), mult_r( sub( 32767, interp_fx ), hTdCngDec->lsp_shb_prev_prev_fx[i] ) ); // Q14 + move16(); + } } IF( LE_16( hTdCngDec->shb_dtx_count_fx, 1000 ) ) @@ -1129,80 +1170,162 @@ static void shb_CNG_decod_ivas_fx( E_LPC_lsf_lsp_conversion( shb_lspCNG_fx, tmp_lsp, LPC_SHB_ORDER ); /*Q14*/ E_LPC_f_lsp_a_conversion( tmp_lsp, shb_lpcCNG_fx, LPC_SHB_ORDER ); - Copy_Scale_sig( shb_lpcCNG_fx, hTdCngDec->shb_lpcCNG_fx, LPC_SHB_ORDER + 1, sub( norm_s( shb_lpcCNG_fx[0] ), -1 ) ); /* Q15 */ - +#ifdef HARM_NON_LINEARITY + IF( st->element_mode != EVS_MONO ) +#endif + { + Copy_Scale_sig( shb_lpcCNG_fx, hTdCngDec->shb_lpcCNG_fx, LPC_SHB_ORDER + 1, sub( norm_s( shb_lpcCNG_fx[0] ), -1 ) ); /* Q15 */ + } Copy_Scale_sig( shb_lpcCNG_fx, shb_lpcCNG_fx, LPC_SHB_ORDER + 1, sub( norm_s( shb_lpcCNG_fx[0] ), 2 ) ); /* Q12 */ /* SHB energy estimation */ - wb_ener_fx = L_deposit_l( 1 ); /*Q1 */ - IF( NE_16( st->element_mode, IVAS_CPE_DFT ) ) +#ifdef HARM_NON_LINEARITY + IF( st->element_mode == EVS_MONO ) { + wb_ener_fx = L_deposit_l( 1 ); /*Q1 */ FOR( i = 0; i < L_FRAME32k; i++ ) { wb_ener_fx = L_add( wb_ener_fx, Mpy_32_16_1( L_mult0( synth_fx[i], synth_fx[i] ), 51 ) ); /* 2*Qsyn */ } - } - exp = norm_l( wb_ener_fx ); - fra = Log2_norm_lc( L_shl( wb_ener_fx, exp ) ); - exp = sub( 30, add( exp, shl( Qsyn, 1 ) ) ); - wb_ener_fx = Mpy_32_16( exp, fra, LG10 ); - wb_ener16_fx = round_fx( L_shl( wb_ener_fx, 10 ) ); /*wb_ener_fx in Q8 */ - Word32 wb_ener32_fx = L_shl( wb_ener16_fx, 3 ); /*wb_ener_fx in Q11 */ - if ( EQ_16( st->first_CNG, 0 ) ) - { - hTdCngDec->wb_cng_ener_fx_32 = wb_ener32_fx; - move32(); /*Q11 */ - } - if ( GT_32( L_abs( L_sub( wb_ener32_fx, hTdCngDec->wb_cng_ener_fx_32 ) ), 24576 ) ) /* 12.0f in Q11 */ - { - allow_cn_step_fx = 1; - move16(); - } + exp = norm_l( wb_ener_fx ); + fra = Log2_norm_lc( L_shl( wb_ener_fx, exp ) ); + exp = sub( 30, add( exp, shl( Qsyn, 1 ) ) ); + wb_ener_fx = Mpy_32_16( exp, fra, LG10 ); + wb_ener16_fx = round_fx( L_shl( wb_ener_fx, 10 ) ); /*wb_ener_fx in Q8 */ + if ( !st->first_CNG ) + { + hTdCngDec->wb_cng_ener_fx = wb_ener16_fx; + move16(); /*Q8 */ + } + if ( GT_16( abs_s( sub( wb_ener16_fx, hTdCngDec->wb_cng_ener_fx ) ), 3072 ) ) + { + allow_cn_step_fx = 1; + move16(); + } - IF( EQ_16( allow_cn_step_fx, 1 ) ) - { - hTdCngDec->wb_cng_ener_fx_32 = wb_ener32_fx; - move32(); /*Q11 */ + IF( EQ_16( allow_cn_step_fx, 1 ) ) + { + hTdCngDec->wb_cng_ener_fx = wb_ener16_fx; + move16(); /*Q8 */ + } + ELSE + { + tmp_16 = sub( wb_ener16_fx, hTdCngDec->wb_cng_ener_fx ); /*Q8 */ + tmp_16 = mult_r( tmp_16, 29491 ); /*Q8 */ + hTdCngDec->wb_cng_ener_fx = add( hTdCngDec->wb_cng_ener_fx, tmp_16 ); /*Q8 */ + move16(); + } + + test(); + test(); + IF( EQ_32( st->core_brate, SID_2k40 ) && EQ_16( sid_bw, 1 ) && ( st->bfi == 0 ) ) + { + hTdCngDec->last_wb_cng_ener_fx = hTdCngDec->wb_cng_ener_fx; + move16(); + + if ( !st->first_CNG ) + { + hTdCngDec->shb_cng_ener_fx = hTdCngDec->last_shb_cng_ener_fx; + move16(); + } + } + + gain_fx16 = sub( hTdCngDec->wb_cng_ener_fx, hTdCngDec->last_wb_cng_ener_fx ); /* Q8 */ + if ( GT_16( gain_fx16, 15 ) ) + { + gain_fx = 15; + move16(); + } + step_fx16 = sub( add( gain_fx16, hTdCngDec->last_shb_cng_ener_fx ), hTdCngDec->shb_cng_ener_fx ); /*Q8 */ + + test(); + IF( EQ_16( allow_cn_step_fx, 1 ) || GT_32( st->last_core_brate, SID_2k40 ) ) + { + hTdCngDec->shb_cng_ener_fx = add( hTdCngDec->shb_cng_ener_fx, step_fx16 ); /* Q8 */ + move16(); + } + ELSE + { + hTdCngDec->shb_cng_ener_fx = add( hTdCngDec->shb_cng_ener_fx, mult( 8192, step_fx16 ) ); /*Q8 */ + move16(); + } } ELSE +#endif { - tmp = L_sub( wb_ener32_fx, hTdCngDec->wb_cng_ener_fx_32 ); /*Q11 */ - tmp = Mpy_32_16_1( tmp, 29491 ); /*Q11 */ - hTdCngDec->wb_cng_ener_fx_32 = L_add( hTdCngDec->wb_cng_ener_fx_32, tmp ); /*Q11 */ - move32(); - } - test(); - test(); - IF( EQ_32( st->core_brate, SID_2k40 ) && EQ_16( sid_bw, 1 ) && EQ_16( st->bfi, 0 ) ) - { - hTdCngDec->last_wb_cng_ener_fx_32 = hTdCngDec->wb_cng_ener_fx_32; /* Q11 */ - move32(); + wb_ener_fx = L_deposit_l( 1 ); /*Q1 */ + IF( NE_16( st->element_mode, IVAS_CPE_DFT ) ) + { + FOR( i = 0; i < L_FRAME32k; i++ ) + { + wb_ener_fx = L_add( wb_ener_fx, Mpy_32_16_1( L_mult0( synth_fx[i], synth_fx[i] ), 51 ) ); /* 2*Qsyn */ + } + } + exp = norm_l( wb_ener_fx ); + fra = Log2_norm_lc( L_shl( wb_ener_fx, exp ) ); + exp = sub( 30, add( exp, shl( Qsyn, 1 ) ) ); + wb_ener_fx = Mpy_32_16( exp, fra, LG10 ); + wb_ener16_fx = round_fx( L_shl( wb_ener_fx, 10 ) ); /*wb_ener_fx in Q8 */ + Word32 wb_ener32_fx = L_shl( wb_ener16_fx, 3 ); /*wb_ener_fx in Q11 */ + if ( EQ_16( st->first_CNG, 0 ) ) + { + hTdCngDec->wb_cng_ener_fx_32 = wb_ener32_fx; + move32(); /*Q11 */ + } + if ( GT_32( L_abs( L_sub( wb_ener32_fx, hTdCngDec->wb_cng_ener_fx_32 ) ), 24576 ) ) /* 12.0f in Q11 */ + { + allow_cn_step_fx = 1; + move16(); + } - if ( !st->first_CNG ) + IF( EQ_16( allow_cn_step_fx, 1 ) ) { - hTdCngDec->shb_cng_ener_fx_32 = hTdCngDec->last_shb_cng_ener_fx_32; /* Q11 */ + hTdCngDec->wb_cng_ener_fx_32 = wb_ener32_fx; + move32(); /*Q11 */ + } + ELSE + { + tmp = L_sub( wb_ener32_fx, hTdCngDec->wb_cng_ener_fx_32 ); /*Q11 */ + tmp = Mpy_32_16_1( tmp, 29491 ); /*Q11 */ + hTdCngDec->wb_cng_ener_fx_32 = L_add( hTdCngDec->wb_cng_ener_fx_32, tmp ); /*Q11 */ move32(); } - } - gain_fx = L_sub( hTdCngDec->wb_cng_ener_fx_32, hTdCngDec->last_wb_cng_ener_fx_32 ); /*Q11 */ - if ( GT_32( gain_fx, 30720 ) ) - { - gain_fx = 30720; - move32(); - } - step_fx = L_sub( L_add( gain_fx, hTdCngDec->last_shb_cng_ener_fx_32 ), hTdCngDec->shb_cng_ener_fx_32 ); /*Q11 */ - test(); - IF( EQ_16( allow_cn_step_fx, 1 ) || GT_32( st->last_core_brate, SID_2k40 ) ) - { - hTdCngDec->shb_cng_ener_fx_32 = L_add( hTdCngDec->shb_cng_ener_fx_32, step_fx ); /* Q11 */ - move32(); - } - ELSE - { - hTdCngDec->shb_cng_ener_fx_32 = L_add( hTdCngDec->shb_cng_ener_fx_32, L_shr( step_fx, 2 ) ); /*Q11 */ - move32(); + test(); + test(); + IF( EQ_32( st->core_brate, SID_2k40 ) && EQ_16( sid_bw, 1 ) && EQ_16( st->bfi, 0 ) ) + { + hTdCngDec->last_wb_cng_ener_fx_32 = hTdCngDec->wb_cng_ener_fx_32; /* Q11 */ + move32(); + + if ( !st->first_CNG ) + { + hTdCngDec->shb_cng_ener_fx_32 = hTdCngDec->last_shb_cng_ener_fx_32; /* Q11 */ + move32(); + } + } + + gain_fx = L_sub( hTdCngDec->wb_cng_ener_fx_32, hTdCngDec->last_wb_cng_ener_fx_32 ); /*Q11 */ + if ( GT_32( gain_fx, 30720 ) ) + { + gain_fx = 30720; + move32(); + } + step_fx = L_sub( L_add( gain_fx, hTdCngDec->last_shb_cng_ener_fx_32 ), hTdCngDec->shb_cng_ener_fx_32 ); /*Q11 */ + + test(); + IF( EQ_16( allow_cn_step_fx, 1 ) || GT_32( st->last_core_brate, SID_2k40 ) ) + { + hTdCngDec->shb_cng_ener_fx_32 = L_add( hTdCngDec->shb_cng_ener_fx_32, step_fx ); /* Q11 */ + move32(); + } + ELSE + { + hTdCngDec->shb_cng_ener_fx_32 = L_add( hTdCngDec->shb_cng_ener_fx_32, L_shr( step_fx, 2 ) ); /*Q11 */ + move32(); + } } + /* generate white noise excitation */ FOR( i = 0; i < L_FRAME16k; i++ ) { @@ -1238,56 +1361,110 @@ static void shb_CNG_decod_ivas_fx( } } - ener_fx = hTdCngDec->shb_cng_ener_fx_32; - move32(); /*Q11 */ - IF( GT_16( st->hTdCngDec->trans_cnt_fx, 0 ) ) +#ifdef HARM_NON_LINEARITY + IF( st->element_mode == EVS_MONO ) { - i = extract_l( L_mult0( hTdCngDec->trans_cnt_fx, 17 ) ); /*Q0 */ - ener_fx = L_add( hTdCngDec->shb_cng_ener_fx_32, Mpy_32_16_1( L_sub( hTdCngDec->last_shb_ener_fx_32, hTdCngDec->shb_cng_ener_fx_32 ), sin_table256_fx[i] ) ); /*Q11 */ - hTdCngDec->trans_cnt_fx = sub( hTdCngDec->trans_cnt_fx, 1 ); - move16(); - } + ener_fx16 = hTdCngDec->shb_cng_ener_fx; + move16(); /*Q8 */ + IF( hTdCngDec->trans_cnt_fx > 0 ) + { + i = extract_l( L_mult0( hTdCngDec->trans_cnt_fx, 17 ) ); /*Q0 */ + ener_fx16 = add_sat( hTdCngDec->shb_cng_ener_fx, mult( sin_table256_fx[i], sub_sat( hTdCngDec->last_shb_ener_fx, hTdCngDec->shb_cng_ener_fx ) ) ); /*Q8 */ + hTdCngDec->trans_cnt_fx = sub( hTdCngDec->trans_cnt_fx, 1 ); + move16(); + } - tmp = L_shr( Mpy_32_16_1( ener_fx, 3277 ), 3 ); /*Q8 */ - IF( GT_32( tmp, 32767 ) ) - abort(); - Word16 tmp_16; - tmp_16 = (Word16) tmp; - move16(); - L_tmp = L_mult( 27213, tmp_16 ); /*Q22, 27213=3.321928 in Q13 */ - L_tmp = L_shr( L_tmp, 6 ); /*Q16 */ - L_tmp = L_add( L_tmp, L_shl( 10, 16 ) ); - if ( ( L_tmp < 0 ) ) - { - L_tmp = 0; - move32(); + tmp_16 = mult( 3277, ener_fx16 ); /*Q8 */ + L_tmp = L_mult( 27213, tmp_16 ); /*Q22, 27213=3.321928 in Q13 */ + L_tmp = L_shr( L_tmp, 6 ); /*Q16 */ + L_tmp = L_add( L_tmp, 10 << 16 ); + if ( L_tmp < 0 ) + { + L_tmp = 0; + move32(); + } + fra = L_Extract_lc( L_tmp, &exp ); + L_tmp = L_shl_sat( Pow2( exp, fra ), 5 ); /*Q5 */ + L_tmp = L_shr( L_tmp, 10 ); + if ( !L_tmp ) + { + L_tmp = 1; /*Q5 */ + } + exp = norm_l( L_tmp ); + L_tmp = L_shl( L_tmp, exp ); /*Q31*/ + tmp_16 = extract_h( L_tmp ); /*Q15*/ + exp = sub( exp, 16 ); + exp1 = norm_s( ener_excSHB_fx ); + fra = shl( ener_excSHB_fx, exp1 ); /*Q15*/ + + IF( GT_16( fra, tmp_16 ) ) + { + fra = shr( fra, 1 ); /*Q15*/ + exp1 = sub( exp1, 1 ); + } + tmp_16 = div_s( fra, tmp_16 ); /*Q15*/ + + L_tmp = L_deposit_h( tmp_16 ); /*Q31 */ + tmp = sub( add( 5, exp ), add( q, exp1 ) ); + L_gain_fx = Isqrt_lc( L_tmp, &tmp_16 ); /*Q31-Qtmp */ } - fra = L_Extract_lc( L_tmp, &exp ); - L_tmp = L_shr( Pow2( exp, fra ), 5 ); /*Q5 */ - if ( !L_tmp ) + ELSE +#endif { - L_tmp = 1; - move32(); /*Q5 */ - } - exp = norm_l( L_tmp ); - L_tmp = L_shl( L_tmp, exp ); /*Q31*/ - tmp_16 = extract_h( L_tmp ); /*Q15*/ - exp = sub( exp, 16 ); - exp1 = norm_s( ener_excSHB_fx ); - fra = shl( ener_excSHB_fx, exp1 ); /*Q15*/ + ener_fx = hTdCngDec->shb_cng_ener_fx_32; + move32(); /*Q11 */ + IF( GT_16( st->hTdCngDec->trans_cnt_fx, 0 ) ) + { + i = extract_l( L_mult0( hTdCngDec->trans_cnt_fx, 17 ) ); /*Q0 */ + ener_fx = L_add( hTdCngDec->shb_cng_ener_fx_32, Mpy_32_16_1( L_sub( hTdCngDec->last_shb_ener_fx_32, hTdCngDec->shb_cng_ener_fx_32 ), sin_table256_fx[i] ) ); /*Q11 */ + hTdCngDec->trans_cnt_fx = sub( hTdCngDec->trans_cnt_fx, 1 ); + move16(); + } - IF( GT_16( fra, tmp_16 ) ) - { - fra = shr( fra, 1 ); /*Q15*/ - exp1 = sub( exp1, 1 ); + tmp = L_shr( Mpy_32_16_1( ener_fx, 3277 ), 3 ); /*Q8 */ + IF( GT_32( tmp, 32767 ) ) + abort(); +#ifndef HARM_NON_LINEARITY + Word16 tmp_16; +#endif + tmp_16 = (Word16) tmp; + move16(); + L_tmp = L_mult( 27213, tmp_16 ); /*Q22, 27213=3.321928 in Q13 */ + L_tmp = L_shr( L_tmp, 6 ); /*Q16 */ + L_tmp = L_add( L_tmp, L_shl( 10, 16 ) ); + if ( L_tmp < 0 ) + { + L_tmp = 0; + move32(); + } + fra = L_Extract_lc( L_tmp, &exp ); + L_tmp = L_shr( Pow2( exp, fra ), 5 ); /*Q5 */ + if ( !L_tmp ) + { + L_tmp = 1; + move32(); /*Q5 */ + } + exp = norm_l( L_tmp ); + L_tmp = L_shl( L_tmp, exp ); /*Q31*/ + tmp_16 = extract_h( L_tmp ); /*Q15*/ + exp = sub( exp, 16 ); + exp1 = norm_s( ener_excSHB_fx ); + fra = shl( ener_excSHB_fx, exp1 ); /*Q15*/ + + IF( GT_16( fra, tmp_16 ) ) + { + fra = shr( fra, 1 ); /*Q15*/ + exp1 = sub( exp1, 1 ); + } + tmp_16 = div_s( fra, tmp_16 ); /*Q15*/ + + L_tmp = L_deposit_h( tmp_16 ); /*Q31 */ + tmp_16 = sub( add( 5, exp ), add( q, exp1 ) ); + L_gain_fx = Isqrt_lc( L_tmp, &tmp_16 ); /*Q31-Qtmp */ + hTdCngDec->shb_cng_gain_fx_32 = ener_fx; + move32(); } - tmp_16 = div_s( fra, tmp_16 ); /*Q15*/ - L_tmp = L_deposit_h( tmp_16 ); /*Q31 */ - tmp_16 = sub( add( 5, exp ), add( q, exp1 ) ); - L_gain_fx = Isqrt_lc( L_tmp, &tmp_16 ); /*Q31-Qtmp */ - hTdCngDec->shb_cng_gain_fx_32 = ener_fx; - move32(); FOR( i = 0; i < L_FRAME16k; i++ ) { shb_syn16k_fx[i] = extract_l( L_shr( Mpy_32_16_1( L_gain_fx, excSHB_fx[i] ), sub( 5, tmp_16 ) ) ); /*Q3 = 31-Qtmp-8-15-5+Qtmp */ @@ -1317,7 +1494,12 @@ static void shb_CNG_decod_ivas_fx( interpolate_3_over_2_allpass_fx( shb_synth_fx, L_FRAME32k, shb_synth_fx, hTdCngDec->interpol_3_2_cng_dec_fx, allpass_poles_3_ov_2 ); } - Scale_sig( shb_synth_fx, L_FRAME48k, -3 ); /* Qx - 3 */ +#ifdef HARM_NON_LINEARITY + IF( st->element_mode != EVS_MONO ) +#endif + { + Scale_sig( shb_synth_fx, L_FRAME48k, -3 ); /* Qx - 3 */ + } ResetSHBbuffer_Dec_fx( st->hBWE_TD, st->extl ); diff --git a/lib_dec/ivas_core_dec_fx.c b/lib_dec/ivas_core_dec_fx.c index 2fda2e5f3..7921a9044 100644 --- a/lib_dec/ivas_core_dec_fx.c +++ b/lib_dec/ivas_core_dec_fx.c @@ -1186,7 +1186,11 @@ ivas_error ivas_core_dec_fx( Scale_sig( hBWE_TD->state_lpc_syn_fx, LPC_SHB_ORDER, sub( Q8, hBWE_TD->prev_Q_bwe_syn ) ); // Q8 Copy_Scale_sig_32_16( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, ( 2 * ALLPASSSECTIONS_STEEP ), sub( hBWE_TD->prev_Q_bwe_syn2, Q11 ) ); // prev_Q_bew_syn2 +#ifdef HARM_NON_LINEARITY + swb_CNG_dec_fx( st, synth_fxl, hb_synth_16_fx[n], sid_bw[n], negate( q ) ); +#else swb_CNG_dec_ivas_fx( st, synth_fxl, hb_synth_16_fx[n], sid_bw[n], negate( q ) ); +#endif test(); IF( st->core_brate == FRAME_NO_DATA || EQ_32( st->core_brate, SID_2k40 ) ) -- GitLab From c074f054e05125e4900fea9079a73e074cd12812 Mon Sep 17 00:00:00 2001 From: vaclav Date: Sun, 18 Jan 2026 21:01:09 +0100 Subject: [PATCH 09/11] fixes --- lib_dec/cng_dec_fx.c | 22 ++++++++++++++++------ lib_dec/ivas_core_dec_fx.c | 10 +++++++++- lib_enc/cng_enc_fx.c | 17 +++++++++++++++-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib_dec/cng_dec_fx.c b/lib_dec/cng_dec_fx.c index a88605896..27c528b24 100644 --- a/lib_dec/cng_dec_fx.c +++ b/lib_dec/cng_dec_fx.c @@ -1233,7 +1233,7 @@ static void shb_CNG_decod_ivas_fx( gain_fx16 = sub( hTdCngDec->wb_cng_ener_fx, hTdCngDec->last_wb_cng_ener_fx ); /* Q8 */ if ( GT_16( gain_fx16, 15 ) ) { - gain_fx = 15; + gain_fx16 = 15; move16(); } step_fx16 = sub( add( gain_fx16, hTdCngDec->last_shb_cng_ener_fx ), hTdCngDec->shb_cng_ener_fx ); /*Q8 */ @@ -1336,7 +1336,6 @@ static void shb_CNG_decod_ivas_fx( /* synthesis filtering */ Syn_filt_s( 0, shb_lpcCNG_fx, LPC_SHB_ORDER, excTmp_fx, excSHB_fx, L_FRAME16k, hBWE_TD->state_lpc_syn_fx, 1 ); - /* synthesis signal gain shaping */ L_tmp = 0; move32(); @@ -1405,12 +1404,12 @@ static void shb_CNG_decod_ivas_fx( tmp_16 = div_s( fra, tmp_16 ); /*Q15*/ L_tmp = L_deposit_h( tmp_16 ); /*Q31 */ - tmp = sub( add( 5, exp ), add( q, exp1 ) ); + tmp_16 = sub( add( 5, exp ), add( q, exp1 ) ); L_gain_fx = Isqrt_lc( L_tmp, &tmp_16 ); /*Q31-Qtmp */ } ELSE -#endif { +#endif ener_fx = hTdCngDec->shb_cng_ener_fx_32; move32(); /*Q11 */ IF( GT_16( st->hTdCngDec->trans_cnt_fx, 0 ) ) @@ -1463,7 +1462,9 @@ static void shb_CNG_decod_ivas_fx( L_gain_fx = Isqrt_lc( L_tmp, &tmp_16 ); /*Q31-Qtmp */ hTdCngDec->shb_cng_gain_fx_32 = ener_fx; move32(); +#ifdef HARM_NON_LINEARITY } +#endif FOR( i = 0; i < L_FRAME16k; i++ ) { @@ -1496,10 +1497,19 @@ static void shb_CNG_decod_ivas_fx( #ifdef HARM_NON_LINEARITY IF( st->element_mode != EVS_MONO ) -#endif { - Scale_sig( shb_synth_fx, L_FRAME48k, -3 ); /* Qx - 3 */ + IF( EQ_32( st->output_Fs, 32000 ) ) + { + Scale_sig( shb_synth_fx, L_FRAME32k, -3 ); /* Qx - 3 */ + } + ELSE + { + Scale_sig( shb_synth_fx, L_FRAME48k, -3 ); /* Qx - 3 */ + } } +#else + Scale_sig( shb_synth_fx, L_FRAME48k, -3 ); /* Qx - 3 */ +#endif ResetSHBbuffer_Dec_fx( st->hBWE_TD, st->extl ); diff --git a/lib_dec/ivas_core_dec_fx.c b/lib_dec/ivas_core_dec_fx.c index 7921a9044..ad82719e2 100644 --- a/lib_dec/ivas_core_dec_fx.c +++ b/lib_dec/ivas_core_dec_fx.c @@ -1181,7 +1181,11 @@ ivas_error ivas_core_dec_fx( q = 2; move16(); - Copy_Scale_sig_32_16( hb_synth_32_fx[n], hb_synth_16_fx[n], L_FRAME48k, -( Q11 ) ); // Q0 +#ifdef HARM_NON_LINEARITY + Copy_Scale_sig_32_16( hb_synth_32_fx[n], hb_synth_16_fx[n], output_frame, -( Q11 ) ); // Q0 +#else + Copy_Scale_sig_32_16( hb_synth_32_fx[n], hb_synth_16_fx[n], L_FRAME48k, -( Q11 ) ); // Q0 +#endif Copy_Scale_sig_32_16( synth_32_fx[n], synth_fxl, output_frame, negate( add( Q11, q ) ) ); // Q0 Scale_sig( hBWE_TD->state_lpc_syn_fx, LPC_SHB_ORDER, sub( Q8, hBWE_TD->prev_Q_bwe_syn ) ); // Q8 Copy_Scale_sig_32_16( hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx_32, hBWE_TD->genSHBsynth_state_lsyn_filt_shb_local_fx, ( 2 * ALLPASSSECTIONS_STEEP ), sub( hBWE_TD->prev_Q_bwe_syn2, Q11 ) ); // prev_Q_bew_syn2 @@ -1195,7 +1199,11 @@ ivas_error ivas_core_dec_fx( test(); IF( st->core_brate == FRAME_NO_DATA || EQ_32( st->core_brate, SID_2k40 ) ) { +#ifdef HARM_NON_LINEARITY + Copy_Scale_sig_16_32_DEPREC( hb_synth_16_fx[n], hb_synth_32_fx[n], output_frame, ( Q11 ) ); // Q11 +#else Copy_Scale_sig_16_32_DEPREC( hb_synth_16_fx[n], hb_synth_32_fx[n], L_FRAME48k, ( Q11 ) ); // Q11 +#endif } Scale_sig( hBWE_TD->state_lpc_syn_fx, LPC_SHB_ORDER, negate( sub( Q8, hBWE_TD->prev_Q_bwe_syn ) ) ); // Q0 diff --git a/lib_enc/cng_enc_fx.c b/lib_enc/cng_enc_fx.c index 730ad75c2..13c961a0b 100644 --- a/lib_enc/cng_enc_fx.c +++ b/lib_enc/cng_enc_fx.c @@ -2852,8 +2852,8 @@ static Word16 shb_DTX_fx( void calculate_hangover_attenuation_gain_fx( #ifndef HARM_NON_LINEARITY Encoder_State *st, /* i : encoder state structure */ - Word16 *att, /* o : attenuation factor Q15 */ - const Word16 vad_hover_flag /* i : VAD hangover flag */ + Word16 *att, /* o : attenuation factor Q15 */ + const Word16 vad_hover_flag /* i : VAD hangover flag */ ) { Word16 offset; @@ -3302,7 +3302,11 @@ static Word16 shb_DTX_ivas_fx( update = 1; move16(); } +#ifdef HARM_NON_LINEARITY + ELSE IF( st->element_mode != EVS_MONO && hTdCngEnc->shb_cng_ini_cnt > 0 ) +#else ELSE IF( hTdCngEnc->shb_cng_ini_cnt > 0 ) +#endif { hTdCngEnc->shb_cng_ini_cnt = sub( hTdCngEnc->shb_cng_ini_cnt, 1 ); update = 1; @@ -3319,6 +3323,15 @@ static Word16 shb_DTX_ivas_fx( update = 1; move16(); } +#ifdef HARM_NON_LINEARITY + ELSE IF( st->element_mode == EVS_MONO && hTdCngEnc->shb_cng_ini_cnt > 0 ) + { + hTdCngEnc->shb_cng_ini_cnt = sub( hTdCngEnc->shb_cng_ini_cnt, 1 ); + update = 1; + move16(); + move16(); + } +#endif ELSE IF( GT_16( abs_s( sub( sub( hTdCngEnc->mov_wb_cng_ener_fx, hTdCngEnc->mov_shb_cng_ener_fx ), sub( hTdCngEnc->last_wb_cng_ener_fx, hTdCngEnc->last_shb_cng_ener_fx ) ) ), 768 ) ) { update = 1; -- GitLab From 54b8e2604e166a7e26271c65ad8fac7b5a3cce8e Mon Sep 17 00:00:00 2001 From: vaclav Date: Mon, 19 Jan 2026 12:14:40 +0100 Subject: [PATCH 10/11] two fixes to restore BE --- lib_enc/cng_enc_fx.c | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/lib_enc/cng_enc_fx.c b/lib_enc/cng_enc_fx.c index 13c961a0b..bd8467eff 100644 --- a/lib_enc/cng_enc_fx.c +++ b/lib_enc/cng_enc_fx.c @@ -1304,13 +1304,16 @@ void CNG_enc_fx( } ELSE /* L_FRAME16k */ { - FOR( j = 0; j < 80; j++ ) + FOR( i = 0; i < 2; i++ ) { - L_tmp = L_mult0( *pt_sp, *pt_sp ); - pt_sp++; - L_tmp = L_mac0( L_tmp, *pt_sp, *pt_sp ); - pt_sp++; - L_ener = L_add( L_ener, L_shr( Mult_32_16( L_tmp, 26214 /* 256/320, Q15 */ ), 7 ) ); /* 2*Q_new + 1, divide by L_frame done here */ + FOR( j = 0; j < 80; j++ ) + { + L_tmp = L_mult0( *pt_sp, *pt_sp ); + pt_sp++; + L_tmp = L_mac0( L_tmp, *pt_sp, *pt_sp ); + pt_sp++; + L_ener = L_add( L_ener, L_shr( Mult_32_16( L_tmp, 26214 /* 256/320, Q15 */ ), 7 ) ); /* 2*Q_new + 1, divide by L_frame done here */ + } } } } @@ -1690,36 +1693,12 @@ void CNG_enc_fx( IF( hTdCngEnc->burst_ho_cnt > 0 ) { -#ifdef HARM_NON_LINEARITY - IF( st_fx->element_mode == EVS_MONO ) - { - /**allow_cn_step |= ( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] > 4 * hTdCngEnc->lp_ener_fx ); */ - /*allow_cn_step |= (hDtxEnc->first_CNG || st->element_mode == EVS_MONO) && (hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener);*/ - /* (hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener); */ - L_tmp1 = L_shr( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr], 2 ); - IF( NE_16( lp_ener_thr_scale, 8 ) ) - { - L_tmp1 = L_add( L_tmp1, L_shr( hTdCngEnc->lp_ener_fx, 8 ) ); - } - L_tmp1 = L_sub( L_tmp1, hTdCngEnc->lp_ener_fx ); - } - ELSE - { - L_tmp1 = 0; - move16(); - } -#else /**allow_cn_step |= ( hDtxEnc->first_CNG || st->element_mode == EVS_MONO ) && ( hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener );*/ w_temp = W_msu_32_16( W_shl( W_deposit32_l( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] ), 2 ), hTdCngEnc->lp_ener_fx, lp_ener_thr_scale ); /*Q8*/ -#endif test(); test(); -#ifdef HARM_NON_LINEARITY - IF( ( hDtxEnc->first_CNG > 0 || ( st_fx->element_mode == EVS_MONO ) ) && L_tmp1 > 0 ) -#else IF( ( hDtxEnc->first_CNG > 0 || ( st_fx->element_mode == EVS_MONO ) ) && w_temp > 0 ) -#endif { *allow_cn_step = s_or( *allow_cn_step, 1 ); move16(); -- GitLab From ebf3b995fbe6dba60f6e9e4c7c92aeed0efe82d4 Mon Sep 17 00:00:00 2001 From: vaclav Date: Mon, 19 Jan 2026 12:34:26 +0100 Subject: [PATCH 11/11] revert a change in CNG_enc_fx() --- lib_enc/cng_enc_fx.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib_enc/cng_enc_fx.c b/lib_enc/cng_enc_fx.c index bd8467eff..1bc7cab41 100644 --- a/lib_enc/cng_enc_fx.c +++ b/lib_enc/cng_enc_fx.c @@ -1693,8 +1693,26 @@ void CNG_enc_fx( IF( hTdCngEnc->burst_ho_cnt > 0 ) { - /**allow_cn_step |= ( hDtxEnc->first_CNG || st->element_mode == EVS_MONO ) && ( hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener );*/ - w_temp = W_msu_32_16( W_shl( W_deposit32_l( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] ), 2 ), hTdCngEnc->lp_ener_fx, lp_ener_thr_scale ); /*Q8*/ +#ifdef HARM_NON_LINEARITY + IF( st_fx->element_mode == EVS_MONO ) + { + /**allow_cn_step |= ( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] > 4 * hTdCngEnc->lp_ener_fx ); */ + /*allow_cn_step |= (hDtxEnc->first_CNG || st->element_mode == EVS_MONO) && (hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener);*/ + /* (hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener); */ + L_tmp1 = L_shr( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr], 2 ); + IF( NE_16( lp_ener_thr_scale, 8 ) ) + { + L_tmp1 = L_add( L_tmp1, L_shr( hTdCngEnc->lp_ener_fx, 8 ) ); + } + L_tmp1 = L_sub( L_tmp1, hTdCngEnc->lp_ener_fx ); + w_temp = W_deposit32_l( L_tmp1 ); + } + ELSE +#endif + { + /**allow_cn_step |= ( hDtxEnc->first_CNG || st->element_mode == EVS_MONO ) && ( hTdCngEnc->ho_ener_hist[hTdCngEnc->ho_hist_ptr] > lp_ener_thr_scale * hTdCngEnc->lp_ener );*/ + w_temp = W_msu_32_16( W_shl( W_deposit32_l( hTdCngEnc->ho_ener_hist_fx[hTdCngEnc->ho_hist_ptr] ), 2 ), hTdCngEnc->lp_ener_fx, lp_ener_thr_scale ); /*Q8*/ + } test(); test(); -- GitLab