From 3b48390e65e7aac91a7a88ff4c9bd78f02461c76 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 13:27:53 +0100 Subject: [PATCH 01/12] harmonize wb_bwe_enc_fx() --- lib_com/options.h | 1 + lib_enc/evs_enc_fx.c | 4 + lib_enc/ivas_core_enc_fx.c | 4 + lib_enc/prot_fx_enc.h | 19 +--- lib_enc/swb_bwe_enc_fx.c | 187 ++++++++++++++++++++++--------------- 5 files changed, 125 insertions(+), 90 deletions(-) diff --git a/lib_com/options.h b/lib_com/options.h index 37ad8d099..8f55e8322 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -86,6 +86,7 @@ #define FIX_1904_HARM_GSC_ENC /* VA: #1904 Harmonization of EVS and IVAS GSC code */ #define FIX_2397_COPY_AQ_MDCT_CORE_BFI /* FhG: prevent copying of uninit memory in MDCT stereo core if bfi is set */ #define HARMONIZE_TBE /* VA: harmonize core-coder TBE function duplications */ +#define HARM_FD_BWE /* VA: harmonize core-coder FD BWE function duplications */ /* #################### End BE switches ################################## */ diff --git a/lib_enc/evs_enc_fx.c b/lib_enc/evs_enc_fx.c index 45e58c5e8..429961c7f 100644 --- a/lib_enc/evs_enc_fx.c +++ b/lib_enc/evs_enc_fx.c @@ -438,7 +438,11 @@ ivas_error evs_enc_fx( ELSE IF( EQ_16( st->extl, WB_BWE ) ) { /* WB BWE encoder */ +#ifdef HARM_FD_BWE + wb_bwe_enc_fx( st, new_inp_resamp16k ); +#else wb_bwe_enc_fx( st, new_inp_resamp16k, st->coder_type ); +#endif } /*---------------------------------------------------------------------* diff --git a/lib_enc/ivas_core_enc_fx.c b/lib_enc/ivas_core_enc_fx.c index 79a28b63a..a67005191 100644 --- a/lib_enc/ivas_core_enc_fx.c +++ b/lib_enc/ivas_core_enc_fx.c @@ -645,7 +645,11 @@ ivas_error ivas_core_enc_fx( ELSE IF( EQ_16( st->extl, WB_BWE ) && n == 0 && NE_16( st->element_mode, IVAS_CPE_MDCT ) ) { /* WB BWE encoder */ +#ifdef HARM_FD_BWE + wb_bwe_enc_fx( st, new_inp_resamp16k_fx[n] ); +#else wb_bwe_enc_ivas_fx( st, new_inp_resamp16k_fx[n] ); +#endif } /*---------------------------------------------------------------------* diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 030fee944..9f5bf5989 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -669,12 +669,14 @@ void vad_param_updt_fx( ); void wb_bwe_enc_fx( +#ifndef HARM_FD_BWE Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 *new_wb_speech_fx, /* i : original i signal at 16kHz */ Word16 coder_type /* i : coding type */ ); void wb_bwe_enc_ivas_fx( +#endif Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 *new_wb_speech_fx /* i : original i signal at 16kHz */ ); @@ -981,23 +983,6 @@ void set_bw_stereo_fx( CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structures */ ); -/* o : classification of wb signal */ -Word16 WB_BWE_encoding_fx( - const Word16 coder_type, /* i : coder type */ - const Word16 *yos_fx, /* i : MDCT coefficients of weighted original */ - Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ - Encoder_State *st_fx, /* i/o: Encoder structure */ - Word16 Q_synth, - Word16 Q_synth_lf ); - -/* o : classification of wb signal */ -Word16 WB_BWE_encoding_ivas_fx( - Encoder_State *st_fx, /* i/o: Encoder structure */ - const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ - Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ - Word16 Q_synth, - Word16 Q_synth_lf ); - void calculate_hangover_attenuation_gain_fx( Encoder_State *st, /* i : encoder state structure */ Word16 *att, /* o : attenuation factor */ diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index 8dfd67076..42652713d 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -11,39 +11,23 @@ #include "prot_fx_enc.h" /* Function prototypes */ /*---------------------------------------------------------------------* - * Local functions + * Local functions declarations *---------------------------------------------------------------------*/ -static Word16 SWB_BWE_encoding_fx( - Encoder_State *st_fx, /* i/o: encoder state structure */ - Word16 *insig_fx, /* i/o: delayed original input signal at 32kHz (might be rescaled)*/ - const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ - const Word16 *insig_hp_fx, /* i : delayed original highband input signal at 32kHz */ - const Word16 *synth_fx, /* i : delayed ACELP core synthesis at 12.8kHz */ - const Word16 *yos_fx, /* i : MDCT coefficients of the windowed original input signal at 32kHz */ - Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ - const Word16 tilt_nb_fx, /* i : SWB tilt */ - const Word16 st_offset, /* i : start frequency offset for BWE envelope */ - const Word16 coder_type, /* i : coding type */ - Word16 Q_insig_lp, - Word16 Q_shb, - Word16 Q_synth, - Word16 Q_synth_lf ); +static Word16 WB_BWE_encoding_fx( const Word16 coder_type, const Word16 *yos_fx, Word16 *WB_fenv_fx, Encoder_State *st_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); -static Word16 SWB_BWE_encoding_ivas_fx( - Encoder_State *st_fx, /* i/o: encoder state structure */ - Word16 *insig_fx, /* i/o: delayed original input signal at 32kHz (might be rescaled)*/ - const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ - const Word16 *insig_hp_fx, /* i : delayed original highband input signal at 32kHz */ - const Word16 *synth_fx, /* i : delayed ACELP core synthesis at 12.8kHz */ - const Word32 *yos_fx, /* i : MDCT coefficients of the windowed original input signal at 32kHz */ - Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ - const Word16 tilt_nb_fx, /* i : SWB tilt */ - const Word16 st_offset, /* i : start frequency offset for BWE envelope */ - Word16 Q_insig_lp, - Word16 Q_shb, - Word16 Q_synth, - Word16 Q_synth_lf ); +static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); + +static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 coder_type, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); + +static Word16 SWB_BWE_encoding_fx_32( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word32 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); + + +/*-------------------------------------------------------------------* + * delay_input_signal_fx() + * + * + *-------------------------------------------------------------------*/ static void delay_input_signal_fx( Word16 *old_sig, @@ -115,6 +99,7 @@ static void delay_input_signal_fx( return; } + /*-------------------------------------------------------------------* * wb_bwe_enc() * @@ -122,6 +107,7 @@ static void delay_input_signal_fx( *-------------------------------------------------------------------*/ void wb_bwe_enc_fx( +#ifndef HARM_FD_BWE Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 *new_wb_speech_fx, /* i : original input signal at 16kHz */ Word16 coder_type /* i : coding type */ @@ -201,6 +187,7 @@ void wb_bwe_enc_fx( *-------------------------------------------------------------------*/ void wb_bwe_enc_ivas_fx( +#endif Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 *new_wb_speech_fx /* i : original input signal at 16kHz */ ) @@ -232,12 +219,18 @@ void wb_bwe_enc_ivas_fx( /*---------------------------------------------------------------------*/ /* WB BWE encoding */ - - - /* MDCT of the core synthesis signal */ + /* MDCT of the core synthesis signal */ /*---------------------------------------------------------------------*/ + new_input_fx_exp = -1; move16(); +#ifdef HARM_FD_BWE + if ( st_fx->element_mode == EVS_MONO ) + { + new_input_fx_exp = 0; + move16(); + } +#endif wtda_fx( old_input_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ @@ -246,8 +239,37 @@ void wb_bwe_enc_ivas_fx( /* DCT of the ACELP core synthesis */ direct_transform_fx( L_wtda_synth_fx, yorig_32, 0, L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); - mode = WB_BWE_encoding_ivas_fx( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) + { + Word16 scl, Q_synth; + Word16 yorig_fx[L_FRAME16k]; + + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + 8 /*MAX_Q_NEW_INPUT*/, new_input_fx_exp ); + + /* Possible to Upscale? */ + IF( scl > 0 ) + { + /* Yes */ + /* Calc Room to Upscale */ + Q_synth = Find_Max_Norm32( yorig_32, L_FRAME16k ); + + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_synth, scl ); + } + Copy_Scale_sig32_16( yorig_32, yorig_fx, L_FRAME16k, scl ); + Q_synth = sub( add( sub( new_input_fx_exp, 16 ), scl ), 1 ); + + mode = WB_BWE_encoding_fx( st_fx->coder_type, yorig_fx, WB_fenv_fx, st_fx, Q_synth, Q_synth ); + } + ELSE +#endif + { + mode = WB_BWE_encoding_fx_32( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); + } move16(); + push_indice( st_fx->hBstr, IND_WB_CLASS, sub( mode, 2 ), 1 ); } @@ -559,13 +581,13 @@ void swb_bwe_enc_ivas_fx( /* SWB BWE encoding */ IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { - SWB_BWE_encoding_ivas_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); } ELSE { - SWB_BWE_encoding_ivas_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); } @@ -1901,11 +1923,11 @@ static void msvq_interpol_2_fx( *-------------------------------------------------------------------*/ static void calculate_Tonality_fx( - const Word16 *org, /* i : MDCT coefficients of original Q_new*/ - const Word16 *gen, /* i : MDCT coefficients of generated signal Q15*/ - Word16 *SFM_org, /* o : Spectral Flatness results Q12*/ - Word16 *SFM_gen, /* o : Spectral Flatness results Q12*/ - const Word16 length /* i : length for calculating tonality */ + const Word16 *org, /* i : MDCT coefficients of original Q_new*/ + const Word16 *gen, /* i : MDCT coefficients of generated signal Q15*/ + Word16 *SFM_org, /* o : Spectral Flatness results Q12*/ + Word16 *SFM_gen, /* o : Spectral Flatness results Q12*/ + const Word16 length /* i : length for calculating tonality */ ) { Word16 n_coeff; @@ -2048,6 +2070,7 @@ static void calculate_Tonality_fx( return; } +#ifndef HARM_FD_BWE /*-------------------------------------------------------------------* * calculate_Tonality_ivas_fx() * @@ -2201,6 +2224,7 @@ static void calculate_Tonality_ivas_fx( return; } +#endif /*-------------------------------------------------------------------* * energy_control_fx() @@ -2294,6 +2318,7 @@ static void energy_control_fx( return; } +#ifndef HARM_FD_BWE /*-------------------------------------------------------------------* * energy_control_ivas_fx() * @@ -2386,6 +2411,7 @@ static void energy_control_ivas_fx( } return; } +#endif /*-------------------------------------------------------------------* * WB_BWE_encoding() @@ -2393,13 +2419,14 @@ static void energy_control_ivas_fx( * WB BWE main encoder *-------------------------------------------------------------------*/ -Word16 WB_BWE_encoding_fx( /* o : classification of wb signal */ - const Word16 coder_type, /* i : coder type */ - const Word16 *yos_fx, /* i : MDCT coefficients of weighted original */ - Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ - Encoder_State *st_fx, /* i/o: Encoder structure */ - Word16 Q_synth, - Word16 Q_synth_lf ) +/* o : classification of wb signal */ +static Word16 WB_BWE_encoding_fx( + const Word16 coder_type, /* i : coder type */ + const Word16 *yos_fx, /* i : MDCT coefficients of weighted original */ + Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ + Encoder_State *st_fx, /* i/o: Encoder structure */ + const Word16 Q_synth, + const Word16 Q_synth_lf ) { Word16 mode; Word16 i, n_coeff, n_band; @@ -2459,17 +2486,18 @@ Word16 WB_BWE_encoding_fx( /* o : classification of wb } /*-------------------------------------------------------------------* - * WB_BWE_encoding_ivas() + * WB_BWE_encoding() * - * WB BWE main encoder + * WB BWE main encoder, 32-bit variant *-------------------------------------------------------------------*/ -Word16 WB_BWE_encoding_ivas_fx( /* o : classification of wb signal */ - Encoder_State *st_fx, /* i/o: Encoder structure */ - const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ - Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ - Word16 Q_synth, - Word16 Q_synth_lf ) +/* o : classification of wb signal */ +static Word16 WB_BWE_encoding_fx_32( + Encoder_State *st_fx, /* i/o: Encoder structure */ + const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ + Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ + const Word16 Q_synth, + const Word16 Q_synth_lf ) { Word16 mode; Word16 i, n_coeff, n_band; @@ -2484,6 +2512,7 @@ Word16 WB_BWE_encoding_ivas_fx( /* o : classification of Word16 q_shift, scale; Word16 q_WB_fenv[2]; Word16 yos_fx_16[L_FRAME16k]; + n_band = 0; move16(); FOR( i = 0; i < 2; i++ ) @@ -2510,7 +2539,11 @@ Word16 WB_BWE_encoding_ivas_fx( /* o : classification of mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); +#ifdef HARM_FD_BWE + energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); +#else energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); +#endif FOR( i = 0; i < 2; i++ ) { @@ -2545,6 +2578,7 @@ Word16 WB_BWE_encoding_ivas_fx( /* o : classification of * * SWB BWE encoder *-------------------------------------------------------------------*/ + static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, /* i/o: encoder state structure */ Word16 *insig_fx, /* i : delayed original input signal at 32kHz */ @@ -2556,10 +2590,10 @@ static Word16 SWB_BWE_encoding_fx( const Word16 tilt_nb_fx, /* i : SWB tilt */ const Word16 st_offset, /* i : start frequency offset for BWE envelope */ const Word16 coder_type, /* i : coding type */ - Word16 Q_insig_lp, - Word16 Q_shb, - Word16 Q_synth, - Word16 Q_synth_lf ) + const Word16 Q_insig_lp, + const Word16 Q_shb, + const Word16 Q_synth, + const Word16 Q_synth_lf ) { Word16 IsTransient, mode; Word16 index; @@ -2982,9 +3016,10 @@ static Word16 SWB_BWE_encoding_fx( /*-------------------------------------------------------------------* * SWB_BWE_encoding() * - * SWB BWE encoder + * SWB BWE encoder, 32-bit variant *-------------------------------------------------------------------*/ -static Word16 SWB_BWE_encoding_ivas_fx( + +static Word16 SWB_BWE_encoding_fx_32( Encoder_State *st_fx, /* i/o: encoder state structure */ Word16 *insig_fx, /* i : delayed original input signal at 32kHz */ const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ @@ -2994,10 +3029,10 @@ static Word16 SWB_BWE_encoding_ivas_fx( Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ const Word16 tilt_nb_fx, /* i : SWB tilt */ const Word16 st_offset, /* i : start frequency offset for BWE envelope */ - Word16 Q_insig_lp, - Word16 Q_shb, - Word16 Q_synth, - Word16 Q_synth_lf ) + const Word16 Q_insig_lp, + const Word16 Q_shb, + const Word16 Q_synth, + const Word16 Q_synth_lf ) { Word16 IsTransient, mode; Word16 index; @@ -3411,7 +3446,11 @@ static Word16 SWB_BWE_encoding_ivas_fx( mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); push_indice( hBstr, IND_SWB_CLASS, mode, 2 ); +#ifdef HARM_FD_BWE + energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); +#else energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); +#endif FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) { @@ -3555,6 +3594,7 @@ static void get_normalize_spec_fx_32( * calculate_tonality_fx_32() * *-------------------------------------------------------------------*/ + static void calculate_tonality_fx_32( const Word32 *org_fx, /* i : MDCT coefficients of original : Q12 */ const Word32 *gen_fx, /* i : MDCT coefficients of generated signal : Q12 */ @@ -3687,6 +3727,7 @@ static void calculate_tonality_fx_32( * energy_control_fx_32() * *-------------------------------------------------------------------*/ + static void energy_control_fx_32( Encoder_State *st_fx, /* i/o: encoder structure */ const Word16 core, /* i : core : Q0 */ @@ -3870,6 +3911,7 @@ static Word16 decision_hq_generic_class_fx_32( * hq_generic_encoding_fx() * *-------------------------------------------------------------------*/ + void hq_generic_encoding_fx( const Word32 *coefs_fx, /* i : MDCT coefficients of weighted original : Q12 */ Word16 *hq_generic_fenv_fx, /* i/o: energy of SWB envelope : Q3 */ @@ -3903,7 +3945,6 @@ void hq_generic_encoding_fx( move16(); } - energy_control_fx_32( st_fx, HQ_CORE, -1, -1, coefs_fx, hq_generic_offset, energy_factor_fx ); IF( EQ_16( hHQ_core->hq_generic_speech_class, 1 ) ) @@ -4021,7 +4062,6 @@ void hq_generic_encoding_fx( } } - /* Energy VQ */ IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) ) { @@ -4088,6 +4128,7 @@ void hq_generic_encoding_fx( return; } + /*-------------------------------------------------------------------* * fd_bwe_enc_init_fx() * @@ -4134,10 +4175,12 @@ void fd_bwe_enc_init_fx( return; } + /*-------------------------------------------------------------------* - * hq_generic_encoding_fx() + * hq_generic_hf_encoding_fx() * *-------------------------------------------------------------------*/ + void hq_generic_hf_encoding_fx( const Word32 *coefs_fx, /* i : MDCT coefficients of weighted original : Q12 */ Word16 *hq_generic_fenv_fx, /* i/o: energy of SWB envelope : Q1 */ @@ -4172,7 +4215,6 @@ void hq_generic_hf_encoding_fx( move16(); } - energy_control_fx_32( st_fx, HQ_CORE, -1, -1, coefs_fx, hq_generic_offset, energy_factor_fx ); IF( EQ_16( hHQ_core->hq_generic_speech_class, 1 ) ) @@ -4290,7 +4332,6 @@ void hq_generic_hf_encoding_fx( } } - /* Energy VQ */ IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) ) { -- GitLab From 7247cf483f4914ecec5733130d4cb3133cd7a6e8 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 14:31:31 +0100 Subject: [PATCH 02/12] harmonize swb_bwe_enc_fx() --- lib_enc/evs_enc_fx.c | 4 + lib_enc/ivas_core_enc_fx.c | 4 + lib_enc/prot_fx_enc.h | 5 + lib_enc/swb_bwe_enc_fx.c | 579 ++++++++++++++++++++++--------------- 4 files changed, 363 insertions(+), 229 deletions(-) diff --git a/lib_enc/evs_enc_fx.c b/lib_enc/evs_enc_fx.c index 429961c7f..da281e602 100644 --- a/lib_enc/evs_enc_fx.c +++ b/lib_enc/evs_enc_fx.c @@ -519,7 +519,11 @@ ivas_error evs_enc_fx( ELSE IF( EQ_16( st->extl, SWB_BWE ) || EQ_16( st->extl, FB_BWE ) ) { /* SWB BWE encoder */ +#ifdef HARM_FD_BWE + swb_bwe_enc_ivas_fx( st, EVS_MONO, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, 0, shb_speech, Q_shb_spch, sub( Q_new, 1 ) ); +#else swb_bwe_enc_fx( st, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, shb_speech, st->coder_type, Q_shb_spch, sub( Q_new, 1 ) ); +#endif } ELSE IF( EQ_16( st->extl, SWB_BWE_HIGHRATE ) || EQ_16( st->extl, FB_BWE_HIGHRATE ) ) { diff --git a/lib_enc/ivas_core_enc_fx.c b/lib_enc/ivas_core_enc_fx.c index a67005191..9fcf3ce11 100644 --- a/lib_enc/ivas_core_enc_fx.c +++ b/lib_enc/ivas_core_enc_fx.c @@ -749,7 +749,11 @@ ivas_error ivas_core_enc_fx( ELSE IF( EQ_16( st->extl, SWB_BWE ) || EQ_16( st->extl, FB_BWE ) ) { /* SWB(FB) BWE encoder */ +#ifdef HARM_FD_BWE + swb_bwe_enc_ivas_fx( st, last_element_mode, old_inp_12k8_fx[n], old_inp_16k_fx[n], old_syn_12k8_16k_fx[n], new_swb_speech_fx_16, st->q_inp, shb_speech_fx, 0, sub( Q_new[n], 1 ) ); +#else swb_bwe_enc_ivas_fx( st, last_element_mode, old_inp_12k8_fx[n], old_inp_16k_fx[n], old_syn_12k8_16k_fx[n], new_swb_speech_fx_16, st->q_inp, shb_speech_fx, sub( Q_new[n], 1 ) ); +#endif } Scale_sig( old_syn_12k8_16k_fx[n], L_FRAME16k, sub( Q1, Q_new[n] ) ); // Q0 diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index 9f5bf5989..d96056a5c 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -577,7 +577,12 @@ void swb_bwe_enc_ivas_fx( const Word16 *new_swb_speech_fx, /* i : original input signal at 32kHz */ const Word16 Q_new_swb_speech, /* i : Q for new_swb_speech_fx */ Word16 *shb_speech_fx, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ +#ifdef HARM_FD_BWE + Word16 Q_shb_speech, + Word16 Q_slb_speech ); +#else const Word16 Q_slb_speech ); +#endif void swb_CNG_enc_fx( Encoder_State *st_fx, /* i/o: State structure */ diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index 42652713d..7051ac495 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -16,7 +16,7 @@ static Word16 WB_BWE_encoding_fx( const Word16 coder_type, const Word16 *yos_fx, Word16 *WB_fenv_fx, Encoder_State *st_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); -static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); +static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, Word16 Q_synth_lf ); static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 coder_type, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); @@ -33,8 +33,8 @@ static void delay_input_signal_fx( Word16 *old_sig, Word16 *cur_sig, Word16 *new_sig, - Word16 m1, - Word16 m2, + const Word16 m1, + const Word16 m2, Word16 *Q_old, Word16 *Q_new ) { @@ -222,7 +222,7 @@ void wb_bwe_enc_ivas_fx( /* MDCT of the core synthesis signal */ /*---------------------------------------------------------------------*/ - new_input_fx_exp = -1; + new_input_fx_exp = -Q1; move16(); #ifdef HARM_FD_BWE if ( st_fx->element_mode == EVS_MONO ) @@ -287,14 +287,20 @@ void wb_bwe_enc_ivas_fx( void swb_bwe_enc_ivas_fx( Encoder_State *st_fx, /* i/o: encoder state structure */ - const Word16 last_element_mode, /* i : last element mode */ - Word16 *old_input_12k8_fx, /* i : input signal @12.8kHz for SWB BWE */ - Word16 *old_input_16k_fx, /* i : input signal @16kHz for SWB BWE */ + const Word16 last_element_mode, /* i : last element mode */ + Word16 *old_input_12k8_fx, /* i : input signal @12.8kHz for SWB BWE */ + Word16 *old_input_16k_fx, /* i : input signal @16kHz for SWB BWE */ const Word16 *old_syn_12k8_16k_fx, /* i : ACELP core synthesis at 12.8kHz or 16kHz */ const Word16 *new_swb_speech_fx, /* i : original input signal at 32kHz */ const Word16 Q_new_swb_speech, /* i : Q for new_swb_speech_fx */ Word16 *shb_speech_fx_Q0, /* i : SHB target signal (6-14kHz) at 16kHz Q0 */ - const Word16 Q_slb_speech ) +#ifdef HARM_FD_BWE + Word16 Q_shb_speech, + Word16 Q_slb_speech +#else + const Word16 Q_slb_speech +#endif +) { Word16 i; Word16 *new_input_fx; @@ -327,13 +333,20 @@ void swb_bwe_enc_ivas_fx( Word16 fb_band_begin; Word16 q_new_input_hp; Word16 shb_speech_fx[L_FRAME16k]; +#ifndef HARM_FD_BWE Word16 Q_shb_speech; +#endif FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD; TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD; - Q_shb_speech = getScaleFactor16( shb_speech_fx_Q0, L_FRAME16k ); - Copy_Scale_sig( shb_speech_fx_Q0, shb_speech_fx, L_FRAME16k, Q_shb_speech ); // Q0 -> Q_shb_spch +#ifdef HARM_FD_BWE + IF( st_fx->element_mode > EVS_MONO ) +#endif + { + Q_shb_speech = getScaleFactor16( shb_speech_fx_Q0, L_FRAME16k ); + Copy_Scale_sig( shb_speech_fx_Q0, shb_speech_fx, L_FRAME16k, Q_shb_speech ); // Q0 -> Q_shb_spch + } /*---------------------------------------------------------------------* * Delay the original input signal to be synchronized with ACELP core synthesis @@ -376,208 +389,217 @@ void swb_bwe_enc_ivas_fx( Copy( old_input_12k8_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP ); Copy( hBWE_TD->old_speech_shb_fx + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp_fx, Sample_Delay_HP ); } +#ifndef HARM_FD_BWE } +#endif Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP ); Copy( old_input_12k8_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME - Sample_Delay_LP ); Copy( old_input_12k8_fx + L_INP_MEM + L_FRAME - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP ); +#ifdef HARM_FD_BWE } ELSE { - Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS ); - Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS ); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) ) - { - Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS ); - } - Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ); - IF( st_fx->element_mode > EVS_MONO ) - { - Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) ); - Sample_Delay_HP = sub( Sample_Delay_HP, NS2SA( 16000, DELAY_FIR_RESAMPL_NS ) ); - - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) ) - { - Copy( old_input_16k_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP ); - Copy( hBWE_TD->old_speech_shb_fx + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp_fx, Sample_Delay_HP ); - } - } - Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP ); - Copy( old_input_16k_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP ); - Copy( old_input_16k_fx + L_INP_MEM + L_FRAME16k - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP ); + delay_input_signal_fx( hBWE_FD->old_input_lp_fx, old_input_lp_fx, &old_input_12k8_fx[L_INP_MEM], Sample_Delay_LP, L_FRAME, &hBWE_FD->prev_Q_input_lp, &Q_slb_speech ); } - - q_new_input_hp = s_min( Q_shb_speech, hBWE_FD->Q_new_input_hp ); - IF( LT_16( Q_shb_speech, hBWE_FD->Q_new_input_hp ) ) +#endif +} +ELSE +{ + Sample_Delay_SWB_BWE = NS2SA_FX2( inner_Fs, DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS ); + Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS ); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_TD ) ) { - Copy_Scale_sig( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP, sub( Q_shb_speech, hBWE_FD->Q_new_input_hp ) ); // Q_shb_speech - Copy( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP ); // Q_shb_speech + Sample_Delay_HP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS + DELAY_FIR_RESAMPL_NS - DELAY_CLDFB_NS - L_MEM_RECALC_TBE_NS ); } - ELSE + Sample_Delay_LP = NS2SA( 16000, ACELP_LOOK_NS + DELAY_FD_BWE_ENC_16k_NS ); + IF( st_fx->element_mode > EVS_MONO ) { - Copy( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP ); // hBWE_FD->Q_new_input_hp - Copy_Scale_sig( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP, sub( hBWE_FD->Q_new_input_hp, Q_shb_speech ) ); // hBWE_FD->Q_new_input_hp - } + Sample_Delay_SWB_BWE = sub( Sample_Delay_SWB_BWE, NS2SA_FX2( inner_Fs, DELAY_FIR_RESAMPL_NS ) ); + Sample_Delay_HP = sub( Sample_Delay_HP, NS2SA( 16000, DELAY_FIR_RESAMPL_NS ) ); - hBWE_FD->Q_new_input_hp = Q_shb_speech; - move16(); - Copy( shb_speech_fx + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp_fx, Sample_Delay_HP ); - new_input_fx = old_input_fx + Sample_Delay_SWB_BWE; - Copy( hBWE_FD->old_input_fx, old_input_fx, Sample_Delay_SWB_BWE ); - Copy( new_swb_speech_fx, new_input_fx, inner_frame ); - Copy( old_input_fx + inner_frame, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE ); - /*----------------------------------------------------------------------* - * Calculate tilt of the input signal and the ACELP core synthesis - *----------------------------------------------------------------------*/ - - /* tilt returned in Q24 goto to Q11 */ - tilt_nb_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( old_input_lp_fx, Q_slb_speech, st_fx->L_frame ), 3 ) ); - - /*---------------------------------------------------------------------* - * SWB BWE encoding - * FB BWE encoding - *---------------------------------------------------------------------*/ - new_input_fx_q = Q_new_swb_speech; - move16(); - test(); - IF( ( EQ_16( st_fx->idchan, 1 ) ) && ( EQ_16( last_element_mode, IVAS_CPE_DFT ) ) ) - { - FOR( i = 0; i < inner_frame; i++ ) + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) ) { - hBWE_FD->L_old_wtda_swb_fx[i] = mult_r( hBWE_FD->L_old_wtda_swb_fx[i], div_s( i, inner_frame ) ); - move16(); + Copy( old_input_16k_fx + L_INP_MEM - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP ); + Copy( hBWE_TD->old_speech_shb_fx + L_LOOK_16k + L_SUBFR16k - Sample_Delay_HP, new_input_hp_fx, Sample_Delay_HP ); } +#ifndef HARM_FD_BWE } - /* MDCT of the core synthesis signal */ - wtda_fx( old_input_fx, &new_input_fx_q, L_old_input_fx, hBWE_FD->L_old_wtda_swb_fx, - &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - inner_frame ); +#endif - /* DCT of the ACELP core synthesis */ - direct_transform_fx( L_old_input_fx, yorig_32, 0, inner_frame, &new_input_fx_q, st_fx->element_mode ); + Copy( hBWE_FD->old_input_lp_fx, old_input_lp_fx, Sample_Delay_LP ); + Copy( old_input_16k_fx + L_INP_MEM, &old_input_lp_fx[Sample_Delay_LP], L_FRAME16k - Sample_Delay_LP ); + Copy( old_input_16k_fx + L_INP_MEM + L_FRAME16k - Sample_Delay_LP, hBWE_FD->old_input_lp_fx, Sample_Delay_LP ); +#ifdef HARM_FD_BWE +} +ELSE +{ + delay_input_signal_fx( hBWE_FD->old_input_lp_fx, old_input_lp_fx, &old_input_16k_fx[L_INP_MEM], Sample_Delay_LP, L_FRAME16k, &hBWE_FD->prev_Q_input_lp, &Q_slb_speech ); +} +#endif +} - /* high-band gain control in case of BWS */ - IF( st_fx->bwidth_sw_cnt > 0 ) - { - v_multc_fx_16( &yorig_32[L_FRAME16k], div_s( st_fx->bwidth_sw_cnt, BWS_TRAN_PERIOD ), &yorig_32[L_FRAME16k], sub( inner_frame, L_FRAME16k ) ); - } +q_new_input_hp = s_min( Q_shb_speech, hBWE_FD->Q_new_input_hp ); +#ifdef HARM_FD_BWE +IF( st_fx->element_mode == EVS_MONO ) +{ + Copy( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP ); + Copy( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP ); +} +ELSE IF( LT_16( Q_shb_speech, hBWE_FD->Q_new_input_hp ) ) +#else + IF( LT_16( Q_shb_speech, hBWE_FD->Q_new_input_hp ) ) +#endif +{ + Copy_Scale_sig( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP, sub( Q_shb_speech, hBWE_FD->Q_new_input_hp ) ); // Q_shb_speech + Copy( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP ); // Q_shb_speech +} +ELSE +{ + Copy( hBWE_FD->new_input_hp_fx, new_input_hp_fx, Sample_Delay_HP ); // hBWE_FD->Q_new_input_hp + Copy_Scale_sig( shb_speech_fx, &new_input_hp_fx[Sample_Delay_HP], L_FRAME16k - Sample_Delay_HP, sub( hBWE_FD->Q_new_input_hp, Q_shb_speech ) ); // hBWE_FD->Q_new_input_hp +} - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + 8, new_input_fx_q ); - /* Possible to Upscale? */ - IF( scl > 0 ) +hBWE_FD->Q_new_input_hp = Q_shb_speech; +move16(); +Copy( shb_speech_fx + L_FRAME16k - Sample_Delay_HP, hBWE_FD->new_input_hp_fx, Sample_Delay_HP ); +new_input_fx = old_input_fx + Sample_Delay_SWB_BWE; +Copy( hBWE_FD->old_input_fx, old_input_fx, Sample_Delay_SWB_BWE ); +Copy( new_swb_speech_fx, new_input_fx, inner_frame ); +Copy( old_input_fx + inner_frame, hBWE_FD->old_input_fx, Sample_Delay_SWB_BWE ); + +/*----------------------------------------------------------------------* + * Calculate tilt of the input signal and the ACELP core synthesis + *----------------------------------------------------------------------*/ + +/* tilt returned in Q24 goto to Q11 */ +tilt_nb_fx = round_fx_sat( L_shl_sat( calc_tilt_bwe_fx( old_input_lp_fx, Q_slb_speech, st_fx->L_frame ), 3 ) ); + +/*---------------------------------------------------------------------* + * SWB BWE encoding + * FB BWE encoding + *---------------------------------------------------------------------*/ + +new_input_fx_q = Q_new_swb_speech; +move16(); + +test(); +IF( ( EQ_16( st_fx->idchan, 1 ) ) && ( EQ_16( last_element_mode, IVAS_CPE_DFT ) ) ) +{ + FOR( i = 0; i < inner_frame; i++ ) { - /* Yes */ - /* Calc Room to Upscale */ - Q_synth = Find_Max_Norm32( yorig_32, inner_frame ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_synth, scl ); + hBWE_FD->L_old_wtda_swb_fx[i] = mult_r( hBWE_FD->L_old_wtda_swb_fx[i], div_s( i, inner_frame ) ); + move16(); } - Copy_Scale_sig32_16( yorig_32, yorig_fx, inner_frame, scl ); - Q_synth = add( sub( new_input_fx_q, 16 ), scl ); - max = 0; +} + +/* MDCT of the core synthesis signal */ +wtda_fx( old_input_fx, &new_input_fx_q, L_old_input_fx, hBWE_FD->L_old_wtda_swb_fx, &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ inner_frame ); + +/* DCT of the ACELP core synthesis */ +direct_transform_fx( L_old_input_fx, yorig_32, 0, inner_frame, &new_input_fx_q, st_fx->element_mode ); + +/* high-band gain control in case of BWS */ +#ifdef HARM_FD_BWE +test(); +IF( st_fx->bwidth_sw_cnt > 0 && st_fx->element_mode > EVS_MONO ) +#else + IF( st_fx->bwidth_sw_cnt > 0 ) +#endif +{ + v_multc_fx_16( &yorig_32[L_FRAME16k], div_s( st_fx->bwidth_sw_cnt, BWS_TRAN_PERIOD ), &yorig_32[L_FRAME16k], sub( inner_frame, L_FRAME16k ) ); +} + +/* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ +scl = sub( 16 + 8, new_input_fx_q ); +/* Possible to Upscale? */ +IF( scl > 0 ) +{ + /* Yes */ + /* Calc Room to Upscale */ + Q_synth = Find_Max_Norm32( yorig_32, inner_frame ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_synth, scl ); +} +Copy_Scale_sig32_16( yorig_32, yorig_fx, inner_frame, scl ); +Q_synth = add( sub( new_input_fx_q, 16 ), scl ); +max = 0; +move16(); +Q_synth_hf = 0; +move16(); +IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) +{ + scl = 300; move16(); - Q_synth_hf = 0; +} +ELSE +{ + scl = 240; move16(); - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) - { - scl = 300; - move16(); - } - ELSE - { - scl = 240; - move16(); - } - FOR( i = scl; i < inner_frame; i++ ) - { - max = s_max( max, abs_s( yorig_fx[i] ) ); - } +} +FOR( i = scl; i < inner_frame; i++ ) +{ + max = s_max( max, abs_s( yorig_fx[i] ) ); +} - IF( max == 0 ) +IF( max == 0 ) +{ + exp = 15; + move16(); +} +ELSE +{ + exp = norm_s( max ); +} +Copy_Scale_sig( &yorig_fx[scl], &yorig_fx[scl], sub( inner_frame, scl ), exp ); +Q_synth_hf = add( exp, Q_synth ); + +test(); +IF( EQ_16( st_fx->last_extl, SWB_BWE ) || EQ_16( st_fx->last_extl, FB_BWE ) ) +{ + exp = norm_l( st_fx->EnergyLT_fx ); + IF( GT_16( add( st_fx->EnergyLT_fx_exp, exp ), shl( sub( Q_synth_hf, 4 ), 1 ) ) ) { - exp = 15; - move16(); + Q_shb = sub( Q_synth_hf, 4 ); + st_fx->EnergyLT_fx = L_shr( st_fx->EnergyLT_fx, sub( st_fx->EnergyLT_fx_exp, shl( Q_shb, 1 ) ) ); + move32(); } ELSE { - exp = norm_s( max ); + Q_shb = shr( add( st_fx->EnergyLT_fx_exp, exp ), 1 ); + if ( EQ_16( s_and( exp, 0x0001 ), 1 ) ) + { + exp = sub( exp, 1 ); + } + st_fx->EnergyLT_fx = L_shl( st_fx->EnergyLT_fx, exp ); + move32(); } - Copy_Scale_sig( &yorig_fx[scl], &yorig_fx[scl], sub( inner_frame, scl ), exp ); - Q_synth_hf = add( exp, Q_synth ); +} +ELSE +{ + Q_shb = sub( Q_synth_hf, 4 ); +} - test(); - IF( EQ_16( st_fx->last_extl, SWB_BWE ) || EQ_16( st_fx->last_extl, FB_BWE ) ) +#ifdef HARM_FD_BWE +IF( st_fx->element_mode == EVS_MONO ) +{ + Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, Q_shb_speech ) ); + + /* SWB BWE encoding */ + IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { - exp = norm_l( st_fx->EnergyLT_fx ); - IF( GT_16( add( st_fx->EnergyLT_fx_exp, exp ), shl( sub( Q_synth_hf, 4 ), 1 ) ) ) - { - Q_shb = sub( Q_synth_hf, 4 ); - st_fx->EnergyLT_fx = L_shr( st_fx->EnergyLT_fx, sub( st_fx->EnergyLT_fx_exp, shl( Q_shb, 1 ) ) ); - move32(); - } - ELSE - { - Q_shb = shr( add( st_fx->EnergyLT_fx_exp, exp ), 1 ); - if ( EQ_16( s_and( exp, 0x0001 ), 1 ) ) - { - exp = sub( exp, 1 ); - } - st_fx->EnergyLT_fx = L_shl( st_fx->EnergyLT_fx, exp ); - move32(); - } + SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, + SWB_fenv_fx, tilt_nb_fx, 80, st_fx->coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); } ELSE { - Q_shb = sub( Q_synth_hf, 4 ); + SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, + SWB_fenv_fx, tilt_nb_fx, 6, st_fx->coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); } +} +ELSE +{ Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, q_new_input_hp ) ); - /* FB BWE encoding */ - IF( EQ_16( st_fx->extl, FB_BWE ) ) - { - fb_band_begin = FB_BAND_BEGIN; - move16(); - if ( EQ_16( st_fx->L_frame, L_FRAME ) ) - { - fb_band_begin = FB_BAND_BEGIN_12k8; - move16(); - } - energy_fbe_fb_fx = L_deposit_l( 0 ); - FOR( i = fb_band_begin; i < FB_BAND_END; i++ ) - { - tmp = shr( yorig_fx[i], 4 ); - energy_fbe_fb_fx = L_mac0( energy_fbe_fb_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */ - } - ener_low_fx = 0; - move16(); - FOR( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ ) - { - tmp = shr( yorig_fx[i], 4 ); - ener_low_fx = L_mac0( ener_low_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */ - } - - IF( energy_fbe_fb_fx != 0 ) - { - exp = norm_l( energy_fbe_fb_fx ); - frac = extract_h( L_shl( energy_fbe_fb_fx, exp ) ); - tmp = div_s( 16384, frac ); /*15+14-(exp+2*(Q_synth_hf-4)-16) -->45-(exp+2*(Q_synth_hf-4)) */ - L_tmp = Mult_32_16( ener_low_fx, tmp ); /*45-(exp+2*(Q_synth_hf-4)) + 2*(Q_synth_hf-4) - 15 = 30-exp */ - exp1 = norm_l( L_tmp ); - L_tmp = L_shl( L_tmp, exp1 ); - exp = sub( sub( 31, exp1 ), sub( 30, exp ) ); - L_tmp = Isqrt_lc( L_tmp, &exp ); /*31-exp */ - fb_ener_adjust_fx = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /*Q15 */ - } - ELSE - { - fb_ener_adjust_fx = 0; - move16(); - } - - fb_ener_adjust_fx = s_min( fb_ener_adjust_fx, 16384 ); /*Q15 */ - idxGain = usquant_fx( fb_ener_adjust_fx, &ener_adjust_quan_fx, 0, 512, shl( 1, NUM_BITS_FB_FRAMEGAIN ) ); - } - /* SWB BWE encoding */ IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { @@ -589,15 +611,78 @@ void swb_bwe_enc_ivas_fx( SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); } +} +#else + Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, q_new_input_hp ) ); +#endif +/* FB BWE encoding */ +IF( EQ_16( st_fx->extl, FB_BWE ) ) +{ + fb_band_begin = FB_BAND_BEGIN; + move16(); + if ( EQ_16( st_fx->L_frame, L_FRAME ) ) + { + fb_band_begin = FB_BAND_BEGIN_12k8; + move16(); + } + energy_fbe_fb_fx = L_deposit_l( 0 ); + FOR( i = fb_band_begin; i < FB_BAND_END; i++ ) + { + tmp = shr( yorig_fx[i], 4 ); + energy_fbe_fb_fx = L_mac0( energy_fbe_fb_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */ + } + ener_low_fx = 0; + move16(); + FOR( i = fb_band_begin - FB_BAND_WIDTH; i < fb_band_begin; i++ ) + { + tmp = shr( yorig_fx[i], 4 ); + ener_low_fx = L_mac0( ener_low_fx, tmp, tmp ); /*2*(Q_synth_hf-4) */ + } - /* write FB BWE frame gain to the bitstream */ - IF( EQ_16( st_fx->extl, FB_BWE ) ) + IF( energy_fbe_fb_fx != 0 ) { - push_indice( st_fx->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN ); + exp = norm_l( energy_fbe_fb_fx ); + frac = extract_h( L_shl( energy_fbe_fb_fx, exp ) ); + tmp = div_s( 16384, frac ); /*15+14-(exp+2*(Q_synth_hf-4)-16) -->45-(exp+2*(Q_synth_hf-4)) */ + L_tmp = Mult_32_16( ener_low_fx, tmp ); /*45-(exp+2*(Q_synth_hf-4)) + 2*(Q_synth_hf-4) - 15 = 30-exp */ + exp1 = norm_l( L_tmp ); + L_tmp = L_shl( L_tmp, exp1 ); + exp = sub( sub( 31, exp1 ), sub( 30, exp ) ); + L_tmp = Isqrt_lc( L_tmp, &exp ); /*31-exp */ + fb_ener_adjust_fx = round_fx_sat( L_shl_sat( L_tmp, exp ) ); /*Q15 */ + } + ELSE + { + fb_ener_adjust_fx = 0; + move16(); } - return; + fb_ener_adjust_fx = s_min( fb_ener_adjust_fx, 16384 ); /*Q15 */ + idxGain = usquant_fx( fb_ener_adjust_fx, &ener_adjust_quan_fx, 0, 512, shl( 1, NUM_BITS_FB_FRAMEGAIN ) ); +} + +#ifdef HARM_FD_BWE +/* SWB BWE encoding */ +IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) +{ + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); +} +ELSE +{ + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); +} + +#endif +/* write FB BWE frame gain to the bitstream */ +IF( EQ_16( st_fx->extl, FB_BWE ) ) +{ + push_indice( st_fx->hBstr, IND_FB_SLOPE, idxGain, NUM_BITS_FB_FRAMEGAIN ); +} + +return; } /*-------------------------------------------------------------------* @@ -852,26 +937,28 @@ void swb_bwe_enc_fx( /*--------------------------------------------------------------------------*/ /* PURPOSE : Scalar quantizer routine */ /*--------------------------------------------------------------------------*/ -/* INPUT ARGUMENTS : */ -/* Word16 *cb i: quantizer codebook Q10 */ -/* Word16 cb_length i: length of codebook */ -/* Word16 cb_dim i: dimension of codebook */ +/* INPUT ARGUMENTS : */ +/* Word16 *cb i: quantizer codebook Q10 */ +/* Word16 cb_length i: length of codebook */ +/* Word16 cb_dim i: dimension of codebook */ /*--------------------------------------------------------------------------*/ -/* OUTPUT ARGUMENTS : */ +/* OUTPUT ARGUMENTS : */ /*--------------------------------------------------------------------------*/ -/* INPUT/OUTPUT ARGUMENTS : */ -/* Word16 *x i/o: energy of WB envelop Q10 */ +/* INPUT/OUTPUT ARGUMENTS : */ +/* Word16 *x i/o: energy of WB envelop Q10 */ /*--------------------------------------------------------------------------*/ -/* RETURN ARGUMENTS : */ -/* _ None */ +/* RETURN ARGUMENTS : */ +/* _ None */ /*--------------------------------------------------------------------------*/ -/* */ +/* */ /*==========================================================================*/ -static Word16 WB_BWE_fenv_q_fx( /* o: quantized gain index */ - Word16 *x, /* i/o: energy of WB envelop Q10*/ - const Word16 *cb, /* i: quantizer codebook Q10 */ - const Word16 cb_length, /* i: length of codebook */ - const Word16 cb_dim /* i: dimension of codebook */ + +/* o: quantized gain index */ +static Word16 WB_BWE_fenv_q_fx( + Word16 *x, /* i/o: energy of WB envelop Q10*/ + const Word16 *cb, /* i : quantizer codebook Q10*/ + const Word16 cb_length, /* i : length of codebook */ + const Word16 cb_dim /* i : dimension of codebook */ ) { Word16 i, j, indx = 0; @@ -1005,13 +1092,14 @@ static void get_normalize_spec_fx( * classify signal of above 6.4kHz, can be used for WB/SWB switch *-------------------------------------------------------------------*/ -static Word16 FD_BWE_class_fx( /* o : FD BWE class */ - const Word16 *fSpectrum, /* i : input spectrum */ - const Word32 fGain, /* i : global gain */ - const Word16 tilt_nb, /* i : BWE tilt */ - Word16 Q_syn, - Word16 Q_shb, - Encoder_State *st_fx /* i/o: Encoder structure */ +/* o : FD BWE class */ +static Word16 FD_BWE_class_fx( + const Word16 *fSpectrum, /* i : input spectrum */ + const Word32 fGain, /* i : global gain */ + const Word16 tilt_nb, /* i : BWE tilt */ + const Word16 Q_syn, + const Word16 Q_shb, + Encoder_State *st_fx /* i/o: Encoder structure */ ) { Word16 i, j, k, noise, sharpMod = 0; @@ -2488,7 +2576,7 @@ static Word16 WB_BWE_encoding_fx( /*-------------------------------------------------------------------* * WB_BWE_encoding() * - * WB BWE main encoder, 32-bit variant + * WB BWE main encoder *-------------------------------------------------------------------*/ /* o : classification of wb signal */ @@ -2496,8 +2584,8 @@ static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, /* i/o: Encoder structure */ const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ - const Word16 Q_synth, - const Word16 Q_synth_lf ) + Word16 Q_synth, + Word16 Q_synth_lf ) { Word16 mode; Word16 i, n_coeff, n_band; @@ -2513,47 +2601,78 @@ static Word16 WB_BWE_encoding_fx_32( Word16 q_WB_fenv[2]; Word16 yos_fx_16[L_FRAME16k]; - n_band = 0; - move16(); - FOR( i = 0; i < 2; i++ ) +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) { - energy_fx_64 = 0; - move64(); - FOR( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ ) + Word16 new_input_fx_exp = Q_synth; + + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scale = sub( 16 + 8 /*MAX_Q_NEW_INPUT*/, new_input_fx_exp ); + + /* Possible to Upscale? */ + IF( scale > 0 ) { - energy_fx_64 = W_add( energy_fx_64, W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ) ); /*2*Q_synth*/ - } - q_shift = W_norm( energy_fx_64 ); - energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/ - q_shift = sub( q_shift, 32 ); + /* Yes */ + /* Calc Room to Upscale */ + Q_synth = Find_Max_Norm32( yos_fx, L_FRAME16k ); - L_WB_fenv_fx[i] = energy_fx; - move32(); - q_WB_fenv[i] = add( shl( Q_synth, 1 ), q_shift ); + /* Stay within MAX_Q_NEW_INPUT */ + scale = s_min( Q_synth, scale ); + } + Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale ); + Q_synth = sub( add( sub( new_input_fx_exp, 16 ), scale ), 1 ); + Q_synth_lf = Q_synth; move16(); - n_band = add( n_band, 2 ); + + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, Q_synth, 0, st_fx ); + + energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, Q_synth_lf ); } + ELSE +#endif + { + n_band = 0; + move16(); + FOR( i = 0; i < 2; i++ ) + { + energy_fx_64 = 0; + move64(); + FOR( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ ) + { + energy_fx_64 = W_add( energy_fx_64, W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ) ); /*2*Q_synth*/ + } + q_shift = W_norm( energy_fx_64 ); + energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/ + q_shift = sub( q_shift, 32 ); + + L_WB_fenv_fx[i] = energy_fx; + move32(); + q_WB_fenv[i] = add( shl( Q_synth, 1 ), q_shift ); + move16(); + n_band = add( n_band, 2 ); + } - scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); - Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale ); + scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); + Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale ); - mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); #ifdef HARM_FD_BWE - energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif + } FOR( i = 0; i < 2; i++ ) { - ener_40 = mult_r( shr( energy_factor_fx[shl( i, 1 )], 1 ), 26214 ); /*Q19 */ - L_tmp = Mpy_32_16_1( L_WB_fenv_fx[i], ener_40 ); /*q_WB_fenv[i]+4 */ + ener_40 = mult_r( shr( energy_factor_fx[shl( i, 1 )], 1 ), 26214 ); /* Q19 */ + L_tmp = Mpy_32_16_1( L_WB_fenv_fx[i], ener_40 ); /* 2*Q_synth-2 / q_WB_fenv[i]+4 */ IF( L_tmp ) { exp = norm_l( L_tmp ); tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); - /*exp = 30-exp-(q_WB_fenv[i]+4); */ + /* exp = 30-exp-(2*Q_synth-2 / exp = 30-exp-(q_WB_fenv[i]+4) */ exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); L_tmp = Mpy_32_16( exp, tmp, 32767 ); /* Q16 */ WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /*Q10 */ @@ -3449,7 +3568,7 @@ static Word16 SWB_BWE_encoding_fx_32( #ifdef HARM_FD_BWE energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) @@ -3498,6 +3617,8 @@ static Word16 SWB_BWE_encoding_fx_32( return mode; } + + /*-------------------------------------------------------------------* * get_normalize_spec_fx_32() * -- GitLab From f519fd34b846aebea542a3067f69dbd7d862efa3 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 16:19:39 +0100 Subject: [PATCH 03/12] fixes --- lib_enc/swb_bwe_enc_fx.c | 62 ++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index 7051ac495..4fba849a0 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -16,7 +16,7 @@ static Word16 WB_BWE_encoding_fx( const Word16 coder_type, const Word16 *yos_fx, Word16 *WB_fenv_fx, Encoder_State *st_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); -static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, Word16 Q_synth_lf ); +static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, const Word16 Q_synth_lf ); static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 coder_type, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); @@ -233,13 +233,12 @@ void wb_bwe_enc_ivas_fx( #endif wtda_fx( old_input_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - L_FRAME16k ); + &st_fx->Q_old_wtda, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ L_FRAME16k ); /* DCT of the ACELP core synthesis */ direct_transform_fx( L_wtda_synth_fx, yorig_32, 0, L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); -#ifdef HARM_FD_BWE +#ifdef HARM_FD_BWEaa IF( st_fx->element_mode == EVS_MONO ) { Word16 scl, Q_synth; @@ -2585,7 +2584,7 @@ static Word16 WB_BWE_encoding_fx_32( const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ Word16 Q_synth, - Word16 Q_synth_lf ) + const Word16 Q_synth_lf ) { Word16 mode; Word16 i, n_coeff, n_band; @@ -2600,8 +2599,9 @@ static Word16 WB_BWE_encoding_fx_32( Word16 q_shift, scale; Word16 q_WB_fenv[2]; Word16 yos_fx_16[L_FRAME16k]; - #ifdef HARM_FD_BWE + Word16 Q_class, Q_energy, Q_fenv; + IF( st_fx->element_mode == EVS_MONO ) { Word16 new_input_fx_exp = Q_synth; @@ -2621,12 +2621,28 @@ static Word16 WB_BWE_encoding_fx_32( } Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale ); Q_synth = sub( add( sub( new_input_fx_exp, 16 ), scale ), 1 ); - Q_synth_lf = Q_synth; + + n_band = 0; move16(); + FOR( i = 0; i < 2; i++ ) + { + energy_fx = L_deposit_l( 0 ); + FOR( n_coeff = swb_bwe_subband[n_band]; n_coeff < swb_bwe_subband[n_band + 2]; n_coeff++ ) + { + energy_fx = L_add( energy_fx, L_shr( L_mult0( yos_fx_16[n_coeff], yos_fx_16[n_coeff] ), 6 ) ); /*2*Q_synth-6 */ + } - mode = FD_BWE_class_fx( yos_fx_16, 0, 0, Q_synth, 0, st_fx ); + L_WB_fenv_fx[i] = energy_fx; + move32(); /*2*Q_synth-6 */ + n_band = add( n_band, 2 ); + } - energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, Q_synth_lf ); + Q_class = Q_synth; + Q_energy = Q_synth; + Q_fenv = sub( shl( Q_synth, 1 ), 2 ); + move16(); + move16(); + move16(); } ELSE #endif @@ -2655,14 +2671,25 @@ static Word16 WB_BWE_encoding_fx_32( scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale ); - mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); + Q_class = sub( add( Q_synth, scale ), Q16 ); + Q_energy = sub( add( Q_synth_lf, scale ), Q16 ); + Q_fenv = add( q_WB_fenv[i], 4 ); + move16(); + move16(); + move16(); + } #ifdef HARM_FD_BWE - energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, Q_class, 0, st_fx ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); +#endif + +#ifdef HARM_FD_BWE + energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, Q_energy ); +#else + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif - } FOR( i = 0; i < 2; i++ ) { @@ -2673,9 +2700,14 @@ static Word16 WB_BWE_encoding_fx_32( exp = norm_l( L_tmp ); tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* exp = 30-exp-(2*Q_synth-2 / exp = 30-exp-(q_WB_fenv[i]+4) */ - exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); +#ifdef HARM_FD_BWE + exp = sub( sub( 30, exp ), Q_fenv ); +#else + exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); +#endif L_tmp = Mpy_32_16( exp, tmp, 32767 ); /* Q16 */ - WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /*Q10 */ + WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /* Q10 */ move16(); } ELSE -- GitLab From 2b1596d839cf7ffd234f001a8602e995f050a260 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 16:48:36 +0100 Subject: [PATCH 04/12] finish WB_BWE_encoding_fx() --- lib_enc/swb_bwe_enc_fx.c | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index 4fba849a0..d1158dc14 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -14,9 +14,13 @@ * Local functions declarations *---------------------------------------------------------------------*/ +#ifdef HARM_FD_BWE +static Word16 WB_BWE_encoding_fx( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, const Word16 Q_synth_lf ); +#else static Word16 WB_BWE_encoding_fx( const Word16 coder_type, const Word16 *yos_fx, Word16 *WB_fenv_fx, Encoder_State *st_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, const Word16 Q_synth_lf ); +#endif static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 coder_type, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); @@ -238,35 +242,11 @@ void wb_bwe_enc_ivas_fx( /* DCT of the ACELP core synthesis */ direct_transform_fx( L_wtda_synth_fx, yorig_32, 0, L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); -#ifdef HARM_FD_BWEaa - IF( st_fx->element_mode == EVS_MONO ) - { - Word16 scl, Q_synth; - Word16 yorig_fx[L_FRAME16k]; - - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + 8 /*MAX_Q_NEW_INPUT*/, new_input_fx_exp ); - - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_synth = Find_Max_Norm32( yorig_32, L_FRAME16k ); - - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_synth, scl ); - } - Copy_Scale_sig32_16( yorig_32, yorig_fx, L_FRAME16k, scl ); - Q_synth = sub( add( sub( new_input_fx_exp, 16 ), scl ), 1 ); - - mode = WB_BWE_encoding_fx( st_fx->coder_type, yorig_fx, WB_fenv_fx, st_fx, Q_synth, Q_synth ); - } - ELSE +#ifdef HARM_FD_BWE + mode = WB_BWE_encoding_fx( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); +#else + mode = WB_BWE_encoding_fx_32( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); #endif - { - mode = WB_BWE_encoding_fx_32( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); - } move16(); push_indice( st_fx->hBstr, IND_WB_CLASS, sub( mode, 2 ), 1 ); @@ -2508,6 +2488,7 @@ static void energy_control_ivas_fx( /* o : classification of wb signal */ static Word16 WB_BWE_encoding_fx( +#ifndef HARM_FD_BWE const Word16 coder_type, /* i : coder type */ const Word16 *yos_fx, /* i : MDCT coefficients of weighted original */ Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ @@ -2580,6 +2561,7 @@ static Word16 WB_BWE_encoding_fx( /* o : classification of wb signal */ static Word16 WB_BWE_encoding_fx_32( +#endif Encoder_State *st_fx, /* i/o: Encoder structure */ const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ @@ -2700,7 +2682,6 @@ static Word16 WB_BWE_encoding_fx_32( exp = norm_l( L_tmp ); tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* exp = 30-exp-(2*Q_synth-2 / exp = 30-exp-(q_WB_fenv[i]+4) */ - mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); #ifdef HARM_FD_BWE exp = sub( sub( 30, exp ), Q_fenv ); #else -- GitLab From 4fdc716131052889e5a8ae0c59cf9b38d93c78d8 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 17:57:45 +0100 Subject: [PATCH 05/12] harmonize SWB_BWE_encoding_fx_32() --- lib_enc/evs_enc_fx.c | 2 +- lib_enc/ivas_core_enc_fx.c | 2 +- lib_enc/prot_fx_enc.h | 2 + lib_enc/swb_bwe_enc_fx.c | 312 ++++++++++++++++++++++++++++--------- 4 files changed, 240 insertions(+), 78 deletions(-) diff --git a/lib_enc/evs_enc_fx.c b/lib_enc/evs_enc_fx.c index da281e602..bc557e315 100644 --- a/lib_enc/evs_enc_fx.c +++ b/lib_enc/evs_enc_fx.c @@ -520,7 +520,7 @@ ivas_error evs_enc_fx( { /* SWB BWE encoder */ #ifdef HARM_FD_BWE - swb_bwe_enc_ivas_fx( st, EVS_MONO, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, 0, shb_speech, Q_shb_spch, sub( Q_new, 1 ) ); + swb_bwe_enc_fx( st, EVS_MONO, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, 0, shb_speech, Q_shb_spch, sub( Q_new, 1 ) ); #else swb_bwe_enc_fx( st, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, shb_speech, st->coder_type, Q_shb_spch, sub( Q_new, 1 ) ); #endif diff --git a/lib_enc/ivas_core_enc_fx.c b/lib_enc/ivas_core_enc_fx.c index 9fcf3ce11..e4996e3ee 100644 --- a/lib_enc/ivas_core_enc_fx.c +++ b/lib_enc/ivas_core_enc_fx.c @@ -750,7 +750,7 @@ ivas_error ivas_core_enc_fx( { /* SWB(FB) BWE encoder */ #ifdef HARM_FD_BWE - swb_bwe_enc_ivas_fx( st, last_element_mode, old_inp_12k8_fx[n], old_inp_16k_fx[n], old_syn_12k8_16k_fx[n], new_swb_speech_fx_16, st->q_inp, shb_speech_fx, 0, sub( Q_new[n], 1 ) ); + swb_bwe_enc_fx( st, last_element_mode, old_inp_12k8_fx[n], old_inp_16k_fx[n], old_syn_12k8_16k_fx[n], new_swb_speech_fx_16, st->q_inp, shb_speech_fx, 0, sub( Q_new[n], 1 ) ); #else swb_bwe_enc_ivas_fx( st, last_element_mode, old_inp_12k8_fx[n], old_inp_16k_fx[n], old_syn_12k8_16k_fx[n], new_swb_speech_fx_16, st->q_inp, shb_speech_fx, sub( Q_new[n], 1 ) ); #endif diff --git a/lib_enc/prot_fx_enc.h b/lib_enc/prot_fx_enc.h index d96056a5c..6216ce816 100644 --- a/lib_enc/prot_fx_enc.h +++ b/lib_enc/prot_fx_enc.h @@ -558,6 +558,7 @@ void StableHighPitchDetect_fx( Word16 EspecdB[] ); void swb_bwe_enc_fx( +#ifndef HARM_FD_BWE Encoder_State *st_fx, /* i/o: encoder state structure */ Word16 *old_input_12k8_fx, /* i : i signal @12.8kHz for SWB BWE */ Word16 *old_input_16k_fx, /* i : i signal @16kHz for SWB BWE */ @@ -569,6 +570,7 @@ void swb_bwe_enc_fx( Word16 Q_slb_speech ); void swb_bwe_enc_ivas_fx( +#endif Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 last_element_mode, /* i : last element mode */ Word16 *old_input_12k8_fx, /* i : input signal @12.8kHz for SWB BWE */ diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index d1158dc14..d03b857c1 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -24,7 +24,11 @@ static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 coder_type, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); +#ifdef HARM_FD_BWE +static Word16 SWB_BWE_encoding_fx_32( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx_16, const Word32 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); +#else static Word16 SWB_BWE_encoding_fx_32( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word32 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); +#endif /*-------------------------------------------------------------------* @@ -212,6 +216,7 @@ void wb_bwe_enc_ivas_fx( /*---------------------------------------------------------------------* * Delay the original input signal to be synchronized with ACELP core synthesis *---------------------------------------------------------------------*/ + set16_fx( old_input_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS + DELAY_FIR_RESAMPL_NS ) + L_FRAME16k ); Sample_Delay_WB_BWE = NS2SA( 16000, DELAY_FD_BWE_ENC_12k8_NS ); move16(); @@ -258,13 +263,18 @@ void wb_bwe_enc_ivas_fx( return; } + /*-------------------------------------------------------------------* * swb_bwe_enc() * * SWB BWE encoder (only for 32kHz signals) *-------------------------------------------------------------------*/ +#ifdef HARM_FD_BWE +void swb_bwe_enc_fx( +#else void swb_bwe_enc_ivas_fx( +#endif Encoder_State *st_fx, /* i/o: encoder state structure */ const Word16 last_element_mode, /* i : last element mode */ Word16 *old_input_12k8_fx, /* i : input signal @12.8kHz for SWB BWE */ @@ -566,13 +576,23 @@ IF( st_fx->element_mode == EVS_MONO ) /* SWB BWE encoding */ IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { +#ifdef HARM_FD_BWE + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); +#else SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, SWB_fenv_fx, tilt_nb_fx, 80, st_fx->coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); +#endif } ELSE { +#ifdef HARM_FD_BWE + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); +#else SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, SWB_fenv_fx, tilt_nb_fx, 6, st_fx->coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); +#endif } } ELSE @@ -582,13 +602,23 @@ ELSE /* SWB BWE encoding */ IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { +#ifdef HARM_FD_BWE + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); +#else SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); +#endif } ELSE { +#ifdef HARM_FD_BWE + SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); +#else SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); +#endif } } #else @@ -641,7 +671,7 @@ IF( EQ_16( st_fx->extl, FB_BWE ) ) idxGain = usquant_fx( fb_ener_adjust_fx, &ener_adjust_quan_fx, 0, 512, shl( 1, NUM_BITS_FB_FRAMEGAIN ) ); } -#ifdef HARM_FD_BWE +#ifndef HARM_FD_BWE /* SWB BWE encoding */ IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { @@ -664,6 +694,7 @@ IF( EQ_16( st_fx->extl, FB_BWE ) ) return; } +#ifndef HARM_FD_BWE /*-------------------------------------------------------------------* * swb_bwe_enc() * @@ -909,12 +940,12 @@ void swb_bwe_enc_fx( return; } - +#endif /*==========================================================================*/ -/* FUNCTION : static Word16 WB_BWE_fenv_q_fx() */ +/* FUNCTION : static Word16 WB_BWE_fenv_q_fx() */ /*--------------------------------------------------------------------------*/ -/* PURPOSE : Scalar quantizer routine */ +/* PURPOSE : Scalar quantizer routine */ /*--------------------------------------------------------------------------*/ /* INPUT ARGUMENTS : */ /* Word16 *cb i: quantizer codebook Q10 */ @@ -932,7 +963,7 @@ void swb_bwe_enc_fx( /* */ /*==========================================================================*/ -/* o: quantized gain index */ +/* o: quantized gain index */ static Word16 WB_BWE_fenv_q_fx( Word16 *x, /* i/o: energy of WB envelop Q10*/ const Word16 *cb, /* i : quantizer codebook Q10*/ @@ -1350,6 +1381,7 @@ static Word16 FD_BWE_class_fx( * freq_weights_fx() * *-------------------------------------------------------------------*/ + static void freq_weights_fx( const Word16 Band_Ener[], /* i : Band energy Q8 */ const Word16 f_weighting[], /* i : weigting coefs. Q15 */ @@ -1418,6 +1450,7 @@ static void freq_weights_fx( * vqWithCand_w_fx() * *-------------------------------------------------------------------*/ + static void vqWithCand_w_fx( const Word16 *x, /* i : input vector Q8 */ const Word16 *E_ROM_dico, /* i : codebook Q8 */ @@ -2621,10 +2654,9 @@ static Word16 WB_BWE_encoding_fx_32( Q_class = Q_synth; Q_energy = Q_synth; - Q_fenv = sub( shl( Q_synth, 1 ), 2 ); - move16(); move16(); move16(); + Q_fenv = sub( shl( Q_synth, 1 ), 2 ); } ELSE #endif @@ -2656,9 +2688,6 @@ static Word16 WB_BWE_encoding_fx_32( Q_class = sub( add( Q_synth, scale ), Q16 ); Q_energy = sub( add( Q_synth_lf, scale ), Q16 ); Q_fenv = add( q_WB_fenv[i], 4 ); - move16(); - move16(); - move16(); } #ifdef HARM_FD_BWE @@ -3157,10 +3186,13 @@ static Word16 SWB_BWE_encoding_fx_32( const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ const Word16 *insig_hp_fx, /* i : delayed original highband input signal at 32kHz */ const Word16 *synth_fx, /* i : delayed ACELP core synthesis at 12.8kHz */ - const Word32 *yos_fx, /* i : MDCT coefficients of the windowed original input signal at 32kHz */ - Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ - const Word16 tilt_nb_fx, /* i : SWB tilt */ - const Word16 st_offset, /* i : start frequency offset for BWE envelope */ +#ifdef HARM_FD_BWE + const Word16 *yos_fx_16_orig, /* i : 16-bit MDCT coefficients of the windowed original input signal at 32kHz */ +#endif + const Word32 *yos_fx, /* i : 32-bit MDCT coefficients of the windowed original input signal at 32kHz */ + Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ + const Word16 tilt_nb_fx, /* i : SWB tilt */ + const Word16 st_offset, /* i : start frequency offset for BWE envelope */ const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, @@ -3193,6 +3225,9 @@ static Word16 SWB_BWE_encoding_fx_32( Word16 inner_frame; Word16 q_shift; Word16 yos_fx_16[L_FRAME_MAX]; +#ifdef HARM_FD_BWE + Word16 Q_class, Q_energy; +#endif FD_BWE_ENC_HANDLE hBWE_FD = st_fx->hBWE_FD; BSTR_ENC_HANDLE hBstr = st_fx->hBstr; @@ -3219,6 +3254,13 @@ static Word16 SWB_BWE_encoding_fx_32( move16(); } +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) + { + Copy( yos_fx_16_orig, yos_fx_16, inner_frame ); + } +#endif + /* HF transient detect */ IsTransient = detect_transient_fx( insig_hp_fx, L_FRAME16k, Q_shb, st_fx ); st_fx->EnergyLT_fx_exp = shl( Q_shb, 1 ); @@ -3233,19 +3275,37 @@ static Word16 SWB_BWE_encoding_fx_32( energy_fx = L_deposit_l( 0 ); FOR( i = 0; i < L; i++ ) { - energy_fx = L_add( energy_fx, L_shr( L_mult0( insig_lp_fx[i + tmp], insig_lp_fx[i + tmp] ), 7 ) ); /*Q = 2 * Q_insig_lp - 7 */ + energy_fx = L_add( energy_fx, L_shr( L_mult0( insig_lp_fx[i + tmp], insig_lp_fx[i + tmp] ), 7 ) ); /*Q = 2*Q_slb_speech - 7 / 2*Q_insig_lp - 7 */ } - if ( BASOP_Util_Cmp_Mant32Exp( Mpy_32_16_1( energy_fx, 5958 /* 1/5.5f in Q15 */ ), sub( 31 + 7, shl( Q_insig_lp, 1 ) ), hBWE_FD->EnergyLF_fx, hBWE_FD->EnergyLF_exp ) > 0 ) +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) { - IsTransient_LF = 1; + if ( GT_32( Mult_32_16( energy_fx, 5958 ), hBWE_FD->EnergyLF_fx ) ) + { + IsTransient_LF = 1; + move16(); + } + + hBWE_FD->EnergyLF_fx = energy_fx; + hBWE_FD->EnergyLF_exp = sub( 31 + 7, shl( Q_insig_lp, 1 ) ); + move32(); move16(); } + ELSE +#endif + { + if ( BASOP_Util_Cmp_Mant32Exp( Mpy_32_16_1( energy_fx, 5958 /* 1/5.5f in Q15 */ ), sub( 31 + 7, shl( Q_insig_lp, 1 ) ), hBWE_FD->EnergyLF_fx, hBWE_FD->EnergyLF_exp ) > 0 ) + { + IsTransient_LF = 1; + move16(); + } - hBWE_FD->EnergyLF_fx = energy_fx; - hBWE_FD->EnergyLF_exp = sub( 31 + 7, shl( Q_insig_lp, 1 ) ); - move32(); - move16(); + hBWE_FD->EnergyLF_fx = energy_fx; + hBWE_FD->EnergyLF_exp = sub( 31 + 7, shl( Q_insig_lp, 1 ) ); + move32(); + move16(); + } } /* tilt returned in Q24 go to Q11 */ @@ -3267,48 +3327,87 @@ static Word16 SWB_BWE_encoding_fx_32( push_indice( hBstr, IND_SWB_CLASS, mode, 2 ); /* Energy for the different bands and global energies */ - global_gain_fx = L_deposit_l( 0 ); - FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) { - energy_fx_64 = W_deposit32_l( 0 ); - FOR( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ ) + global_gain_fx = L_deposit_l( 0 ); + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) { - W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */ - energy_fx_64 = W_add( W_tmp, energy_fx_64 ); /*2*Q_synth */ + energy_fx = L_deposit_l( 0 ); + FOR( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ ) + { + L_tmp = L_shr( L_mult0( yos_fx_16[n_coeff], yos_fx_16[n_coeff] ), 7 ); /*2*Q_synth-7 */ + energy_fx = L_add( L_tmp, energy_fx ); /*2*Q_synth-7 */ + } + global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( sub( shl( Q_synth, 1 ), 7 ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */ + L_SWB_fenv_fx[n_band] = energy_fx; + move32(); } - q_shift = W_norm( energy_fx_64 ); - energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/ - q_shift = sub( q_shift, 32 ); - global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( add( shl( Q_synth, 1 ), q_shift ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */ - L_SWB_fenv_fx[n_band] = energy_fx; - move32(); - IF( L_SWB_fenv_fx[n_band] == 0 ) + global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */ + + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) { - q_SWB_fenv[n_band] = Q31; - move16(); + expd = norm_s( swb_bwe_trans_subband_width[n_band] ); + tmp = div_s( shl( 1, sub( 14, expd ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-expd) */ + L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], tmp ); /*2*Q_synth-7+29-expd - 15 */ + exp = norm_l( L_tmp ); + tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); + exp = sub( sub( 30, exp ), sub( add( shl( Q_synth, 1 ), 7 ), expd ) ); + L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */ + tmp = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */ + + SWB_fenv_fx[n_band] = sub( tmp, Mean_env_tr_fx[n_band] ); + move16(); /*Q8 */ } - ELSE + } + ELSE +#endif + { + global_gain_fx = L_deposit_l( 0 ); + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) { - q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift ); - move16(); + energy_fx_64 = W_deposit32_l( 0 ); + FOR( n_coeff = swb_bwe_trans_subband[n_band] + st_offset; n_coeff < swb_bwe_trans_subband[n_band + 1] + st_offset; n_coeff++ ) + { + W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */ + energy_fx_64 = W_add( W_tmp, energy_fx_64 ); /*2*Q_synth */ + } + q_shift = W_norm( energy_fx_64 ); + energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/ + q_shift = sub( q_shift, 32 ); + + global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( add( shl( Q_synth, 1 ), q_shift ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */ + L_SWB_fenv_fx[n_band] = energy_fx; + move32(); + IF( L_SWB_fenv_fx[n_band] == 0 ) + { + q_SWB_fenv[n_band] = Q31; + move16(); + } + ELSE + { + q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift ); + move16(); + } } - } - global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */ - FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) - { - expd = norm_s( swb_bwe_trans_subband_width[n_band] ); - tmp = div_s( shl( 1, sub( 14, expd ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-expd) */ - L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], tmp ); /*q_SWB_fenv[n_band]+29-expd - 15 */ - exp = norm_l( L_tmp ); - tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); - exp = sub( sub( 30, exp ), sub( add( q_SWB_fenv[n_band], Q14 ), expd ) ); - L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */ - tmp = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */ + global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */ - SWB_fenv_fx[n_band] = sub( tmp, Mean_env_tr_fx[n_band] ); - move16(); /*Q8 */ + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) + { + expd = norm_s( swb_bwe_trans_subband_width[n_band] ); + tmp = div_s( shl( 1, sub( 14, expd ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-expd) */ + L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], tmp ); /*q_SWB_fenv[n_band]+29-expd - 15 */ + exp = norm_l( L_tmp ); + tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); + exp = sub( sub( 30, exp ), sub( add( q_SWB_fenv[n_band], Q14 ), expd ) ); + L_tmp = Mpy_32_16( exp, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */ + tmp = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */ + + SWB_fenv_fx[n_band] = sub( tmp, Mean_env_tr_fx[n_band] ); + move16(); /*Q8 */ + } } WB_tenv_orig_fx = L_deposit_l( 0 ); @@ -3355,7 +3454,17 @@ static Word16 SWB_BWE_encoding_fx_32( expd = norm_l( WB_tenv_syn_fx ); den = round_fx_sat( L_shl( WB_tenv_syn_fx, expd ) ); - expd = sub( sub( 30, expd ), sub( shl( Q_insig_lp, 1 ), 7 ) ); + +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) + { + expd = sub( sub( 30, expd ), sub( shl( st_fx->Q_syn2, 1 ), 7 ) ); + } + ELSE +#endif + { + expd = sub( sub( 30, expd ), sub( shl( Q_insig_lp, 1 ), 7 ) ); + } scale = shr( sub( den, num ), 15 ); num = shl( num, scale ); @@ -3548,52 +3657,103 @@ static Word16 SWB_BWE_encoding_fx_32( ELSE { /* Energy for the different bands and global energies */ - global_gain_fx = L_deposit_l( 0 ); - FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) { - energy_fx_64 = W_deposit32_l( 0 ); - FOR( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ ) + global_gain_fx = L_deposit_l( 0 ); + FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) { - W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */ - energy_fx_64 = W_add( W_tmp, energy_fx_64 ); /*2*Q_synth */ + energy_fx = L_deposit_l( 0 ); + FOR( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ ) + { + L_tmp = L_shr( L_mult0( yos_fx_16[n_coeff], yos_fx_16[n_coeff] ), 5 ); /*2*Q_synth-5 */ + energy_fx = L_add( L_tmp, energy_fx ); /*2*Q_synth-5 */ + } + + IF( LT_16( n_band, sub( SWB_FENV, 2 ) ) ) + { + global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( 2 * Q_synth - 5, 2 * Q_shb ) ) ); /*2*Q_shb */ + } + L_SWB_fenv_fx[n_band] = energy_fx; + move32(); } - q_shift = W_norm( energy_fx_64 ); - energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/ - q_shift = sub( q_shift, 32 ); - IF( LT_16( n_band, SWB_FENV - 2 ) ) + Q_class = Q_synth; + Q_energy = Q_synth_lf; + move16(); + move16(); + } + ELSE +#endif + { + global_gain_fx = L_deposit_l( 0 ); + FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) { - global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( add( shl( Q_synth, 1 ), q_shift ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */ + energy_fx_64 = W_deposit32_l( 0 ); + FOR( n_coeff = swb_bwe_subband[n_band] + st_offset; n_coeff < swb_bwe_subband[n_band + 1] + st_offset; n_coeff++ ) + { + W_tmp = W_mult0_32_32( yos_fx[n_coeff], yos_fx[n_coeff] ); /*2*Q_synth */ + energy_fx_64 = W_add( W_tmp, energy_fx_64 ); /*2*Q_synth */ + } + q_shift = W_norm( energy_fx_64 ); + energy_fx = W_extract_h( W_shl( energy_fx_64, q_shift ) ); /*2*Q_synth + q_shift - 32*/ + q_shift = sub( q_shift, 32 ); + + IF( LT_16( n_band, SWB_FENV - 2 ) ) + { + global_gain_fx = L_add( global_gain_fx, L_shr( energy_fx, sub( add( shl( Q_synth, 1 ), q_shift ), shl( Q_shb, 1 ) ) ) ); /*2*Q_shb */ + } + L_SWB_fenv_fx[n_band] = energy_fx; + move32(); + q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift ); + move16(); } - L_SWB_fenv_fx[n_band] = energy_fx; - move32(); - q_SWB_fenv[n_band] = add( shl( Q_synth, 1 ), q_shift ); - move16(); + +#ifdef HARM_FD_BWE + scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); + Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); + + Q_class = sub( add( Q_synth, scale ), Q16 ); + Q_energy = sub( add( Q_synth_lf, scale ), Q16 ); +#endif } global_gain_fx = L_shr( global_gain_fx, 1 ); /*2*Q_shb */ - scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); - Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); - mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); +#ifdef HARM_FD_BWE + mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, Q_class, Q_shb, st_fx ); +#else + scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); + Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); + mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); +#endif push_indice( hBstr, IND_SWB_CLASS, mode, 2 ); #ifdef HARM_FD_BWE - energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, Q_energy ); #else energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) { - L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], energy_factor_fx[n_band] ); /*q_SWB_fenv[n_band] */ - L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] ); /*q_SWB_fenv[n_band] */ + L_tmp = Mult_32_16( L_SWB_fenv_fx[n_band], energy_factor_fx[n_band] ); /* 2*Q_synth-5 / q_SWB_fenv[n_band] */ + L_tmp = Mult_32_16( L_tmp, swb_inv_bwe_subband_width_fx[n_band] ); /* 2*Q_synth-5 / q_SWB_fenv[n_band] */ IF( L_tmp != 0 ) { expn = norm_l( L_tmp ); tmp = Log2_norm_lc( L_shl( L_tmp, expn ) ); - expn = sub( 30, add( expn, q_SWB_fenv[n_band] ) ); +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) + { + expn = sub( 30, add( expn, sub( shl( Q_synth, 1 ), 5 ) ) ); + } + ELSE +#endif + { + expn = sub( 30, add( expn, q_SWB_fenv[n_band] ) ); + } L_tmp = Mpy_32_16( expn, tmp, 24660 ); /* Q14 */ /*10log10(2) in Q13 */ SWB_fenv_fx[n_band] = round_fx( L_shl( L_tmp, 10 ) ); /* Q8 */ move16(); -- GitLab From 2e4d48e1aa0d85e7c2afb697e9a8118d35568907 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 18:23:44 +0100 Subject: [PATCH 06/12] editorial improvements --- lib_enc/swb_bwe_enc_fx.c | 160 +++++++++++++++++++++++---------------- 1 file changed, 93 insertions(+), 67 deletions(-) diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index d03b857c1..865409d38 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -16,18 +16,54 @@ #ifdef HARM_FD_BWE static Word16 WB_BWE_encoding_fx( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, const Word16 Q_synth_lf ); + +static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx_16, const Word32 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); #else -static Word16 WB_BWE_encoding_fx( const Word16 coder_type, const Word16 *yos_fx, Word16 *WB_fenv_fx, Encoder_State *st_fx, const Word16 Q_synth, const Word16 Q_synth_lf ); +static Word16 WB_BWE_encoding_fx( + const Word16 coder_type, /* i : coder type */ + const Word16 *yos_fx, /* i : MDCT coefficients of weighted original */ + Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ + Encoder_State *st_fx, /* i/o: Encoder structure */ + const Word16 Q_synth, + const Word16 Q_synth_lf ); -static Word16 WB_BWE_encoding_fx_32( Encoder_State *st_fx, const Word32 *yos_fx, Word16 *WB_fenv_fx, Word16 Q_synth, const Word16 Q_synth_lf ); -#endif +static Word16 WB_BWE_encoding_ivas_fx( + Encoder_State *st_fx, /* i/o: Encoder structure */ + const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ + Word16 *WB_fenv_fx, /* i/o: energy of WB envelope */ + Word16 Q_synth, + const Word16 Q_synth_lf ); -static Word16 SWB_BWE_encoding_fx( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 coder_type, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); +static Word16 SWB_BWE_encoding_fx( + Encoder_State *st_fx, /* i/o: encoder state structure */ + Word16 *insig_fx, /* i/o: delayed original input signal at 32kHz (might be rescaled)*/ + const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ + const Word16 *insig_hp_fx, /* i : delayed original highband input signal at 32kHz */ + const Word16 *synth_fx, /* i : delayed ACELP core synthesis at 12.8kHz */ + const Word16 *yos_fx, /* i : MDCT coefficients of the windowed original input signal at 32kHz */ + Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ + const Word16 tilt_nb_fx, /* i : SWB tilt */ + const Word16 st_offset, /* i : start frequency offset for BWE envelope */ + const Word16 coder_type, /* i : coding type */ + const Word16 Q_insig_lp, + const Word16 Q_shb, + const Word16 Q_synth, + const Word16 Q_synth_lf ); -#ifdef HARM_FD_BWE -static Word16 SWB_BWE_encoding_fx_32( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word16 *yos_fx_16, const Word32 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); -#else -static Word16 SWB_BWE_encoding_fx_32( Encoder_State *st_fx, Word16 *insig_fx, const Word16 *insig_lp_fx, const Word16 *insig_hp_fx, const Word16 *synth_fx, const Word32 *yos_fx, Word16 *SWB_fenv_fx, const Word16 tilt_nb_fx, const Word16 st_offset, const Word16 Q_insig_lp, const Word16 Q_shb, const Word16 Q_synth, const Word16 Q_synth_lf ); +static Word16 SWB_BWE_encoding_ivas_fx( + Encoder_State *st_fx, /* i/o: encoder state structure */ + Word16 *insig_fx, /* i/o: delayed original input signal at 32kHz (might be rescaled)*/ + const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ + const Word16 *insig_hp_fx, /* i : delayed original highband input signal at 32kHz */ + const Word16 *synth_fx, /* i : delayed ACELP core synthesis at 12.8kHz */ + const Word32 *yos_fx, /* i : MDCT coefficients of the windowed original input signal at 32kHz */ + Word16 *SWB_fenv_fx, /* o : frequency-domain quantized BWE envelope */ + const Word16 tilt_nb_fx, /* i : SWB tilt */ + const Word16 st_offset, /* i : start frequency offset for BWE envelope */ + const Word16 Q_insig_lp, + const Word16 Q_shb, + const Word16 Q_synth, + const Word16 Q_synth_lf ); #endif @@ -250,7 +286,7 @@ void wb_bwe_enc_ivas_fx( #ifdef HARM_FD_BWE mode = WB_BWE_encoding_fx( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); #else - mode = WB_BWE_encoding_fx_32( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); + mode = WB_BWE_encoding_ivas_fx( st_fx, yorig_32, WB_fenv_fx, new_input_fx_exp, new_input_fx_exp ); #endif move16(); @@ -322,7 +358,9 @@ void swb_bwe_enc_ivas_fx( Word16 fb_band_begin; Word16 q_new_input_hp; Word16 shb_speech_fx[L_FRAME16k]; -#ifndef HARM_FD_BWE +#ifdef HARM_FD_BWE + Word16 Qenc_synth_hf, Qenc_synth; +#else Word16 Q_shb_speech; #endif @@ -573,53 +611,31 @@ IF( st_fx->element_mode == EVS_MONO ) { Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, Q_shb_speech ) ); - /* SWB BWE encoding */ - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) - { -#ifdef HARM_FD_BWE - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); -#else - SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, - SWB_fenv_fx, tilt_nb_fx, 80, st_fx->coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); -#endif - } - ELSE - { -#ifdef HARM_FD_BWE - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); -#else - SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, - SWB_fenv_fx, tilt_nb_fx, 6, st_fx->coder_type, Q_slb_speech, Q_shb, Q_synth_hf, Q_synth ); -#endif - } + Qenc_synth_hf = Q_synth_hf; + Qenc_synth = Q_synth; + move16(); + move16(); } ELSE { Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, q_new_input_hp ) ); - /* SWB BWE encoding */ - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) - { -#ifdef HARM_FD_BWE - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); -#else - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); -#endif - } - ELSE - { -#ifdef HARM_FD_BWE - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); -#else - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); -#endif - } + Qenc_synth_hf = new_input_fx_q; + Qenc_synth = new_input_fx_q; + move16(); + move16(); +} + +/* SWB BWE encoding */ +IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) +{ + SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, Qenc_synth_hf, Qenc_synth ); +} +ELSE +{ + SWB_BWE_encoding_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, Qenc_synth_hf, Qenc_synth ); } #else Copy_Scale_sig( new_input_hp_fx, new_input_hp_fx, L_FRAME16k, sub( Q_shb, q_new_input_hp ) ); @@ -675,13 +691,13 @@ IF( EQ_16( st_fx->extl, FB_BWE ) ) /* SWB BWE encoding */ IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); + SWB_BWE_encoding_ivas_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 80, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); } ELSE { - SWB_BWE_encoding_fx_32( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, - SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); + SWB_BWE_encoding_ivas_fx( st_fx, old_input_fx, old_input_lp_fx, new_input_hp_fx, old_syn_12k8_16k_fx, yorig_32, + SWB_fenv_fx, tilt_nb_fx, 6, Q_slb_speech, Q_shb, new_input_fx_q, new_input_fx_q ); } #endif @@ -1009,6 +1025,13 @@ static Word16 WB_BWE_fenv_q_fx( return ( indx ); } + +/*-------------------------------------------------------------------* + * get_normalize_spec_fx() + * + * + *-------------------------------------------------------------------*/ + static void get_normalize_spec_fx( const Word16 core, /* i : core selected */ const Word16 extl, /* i : extension layer selected */ @@ -1102,7 +1125,7 @@ static void get_normalize_spec_fx( * classify signal of above 6.4kHz, can be used for WB/SWB switch *-------------------------------------------------------------------*/ -/* o : FD BWE class */ +/* o: FD BWE class */ static Word16 FD_BWE_class_fx( const Word16 *fSpectrum, /* i : input spectrum */ const Word32 fGain, /* i : global gain */ @@ -1646,7 +1669,6 @@ static Word16 vqSimple_w_fx( } } - /* Reading the selected vector */ Copy( &cb[index * dim], y, dim ); @@ -1658,11 +1680,11 @@ static Word16 vqSimple_w_fx( * MSVQ_Interpol_Tran_fx() * *-------------------------------------------------------------------*/ + static void MSVQ_Interpol_Tran_fx( - Word16 *SWB_env_energy, /* i/o : (original/quantized) energy Q8 */ - Word16 *indice /* o : quantized index */ + Word16 *SWB_env_energy, /* i/o: (original/quantized) energy Q8 */ + Word16 *indice /* o : quantized index */ ) - { Word16 k, n_band, candInd[N_CAND_TR], ind_tmp[2], tmp; Word16 env_temp11[SWB_FENV_TRANS / 2], env_temp12[SWB_FENV_TRANS / 2]; @@ -1744,6 +1766,7 @@ static void MSVQ_Interpol_Tran_fx( move16(); } } + return; } @@ -1879,6 +1902,7 @@ static void msvq_interpol_fx( * msvq_interpol_2_fx() * *-------------------------------------------------------------------*/ + static void msvq_interpol_2_fx( Word16 *hq_generic_fenv, /* i/o: (original/quantized) energy */ const Word16 *w_env, /* i : weighting coffecients */ @@ -2593,7 +2617,7 @@ static Word16 WB_BWE_encoding_fx( *-------------------------------------------------------------------*/ /* o : classification of wb signal */ -static Word16 WB_BWE_encoding_fx_32( +static Word16 WB_BWE_encoding_ivas_fx( #endif Encoder_State *st_fx, /* i/o: Encoder structure */ const Word32 *yos_fx, /* i : MDCT coefficients of weighted original */ @@ -2684,10 +2708,11 @@ static Word16 WB_BWE_encoding_fx_32( scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); Copy_Scale_sig32_16( yos_fx, yos_fx_16, L_FRAME16k, scale ); - +#ifdef HARM_FD_BWE Q_class = sub( add( Q_synth, scale ), Q16 ); Q_energy = sub( add( Q_synth_lf, scale ), Q16 ); Q_fenv = add( q_WB_fenv[i], 4 ); +#endif } #ifdef HARM_FD_BWE @@ -2741,6 +2766,7 @@ static Word16 WB_BWE_encoding_fx_32( *-------------------------------------------------------------------*/ static Word16 SWB_BWE_encoding_fx( +#ifndef HARM_FD_BWE Encoder_State *st_fx, /* i/o: encoder state structure */ Word16 *insig_fx, /* i : delayed original input signal at 32kHz */ const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ @@ -3180,7 +3206,8 @@ static Word16 SWB_BWE_encoding_fx( * SWB BWE encoder, 32-bit variant *-------------------------------------------------------------------*/ -static Word16 SWB_BWE_encoding_fx_32( +static Word16 SWB_BWE_encoding_ivas_fx( +#endif Encoder_State *st_fx, /* i/o: encoder state structure */ Word16 *insig_fx, /* i : delayed original input signal at 32kHz */ const Word16 *insig_lp_fx, /* i : delayed original lowband input signal at 32kHz */ @@ -4123,6 +4150,7 @@ static void energy_control_fx_32( * decision_hq_generic_class_fx_32() * *-------------------------------------------------------------------*/ + static Word16 decision_hq_generic_class_fx_32( const Word32 *coefs_fx, /* i: original MDCT spectrum : Q12 */ const Word16 hq_generic_offset /* i: frequency offset of high frequency spectrum : Q0 */ @@ -4130,7 +4158,6 @@ static Word16 decision_hq_generic_class_fx_32( { Word16 i, k; Word16 nband; - Word16 inv_band_fx; Word32 L_tmp, L_tmp1, L_tmp2; Word16 exp, tmp, tmp2; @@ -4191,6 +4218,7 @@ static Word16 decision_hq_generic_class_fx_32( } } avgp2a_fx = Mult_32_16( avgp2a_fx, inv_band_fx ); /*16 + 15 - 15 */ + IF( GT_32( avgp2a_fx, 187227 ) ) /*8.6 / 10log10(2), Q16 */ { return HQ_GENERIC_EXC1; @@ -4403,7 +4431,6 @@ void hq_generic_encoding_fx( move16(); } - IF( EQ_16( st_fx->bwidth, FB ) ) { FOR( n_band = 0; n_band < DIM_FB; n_band++ ) @@ -4680,7 +4707,6 @@ void hq_generic_hf_encoding_fx( move16(); } - IF( EQ_16( length, L_SPEC48k ) ) { FOR( n_band = 0; n_band < DIM_FB; n_band++ ) -- GitLab From b7ee1940e94d2bbcdd1bfe9bcca206cb400b6a24 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 18:24:07 +0100 Subject: [PATCH 07/12] clang-format --- lib_enc/swb_bwe_enc_fx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index 865409d38..e0f37fd23 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -4219,7 +4219,7 @@ static Word16 decision_hq_generic_class_fx_32( } avgp2a_fx = Mult_32_16( avgp2a_fx, inv_band_fx ); /*16 + 15 - 15 */ - IF( GT_32( avgp2a_fx, 187227 ) ) /*8.6 / 10log10(2), Q16 */ + IF( GT_32( avgp2a_fx, 187227 ) ) /*8.6 / 10log10(2), Q16 */ { return HQ_GENERIC_EXC1; } -- GitLab From a1deaf2881243f8b3187b161b29901be7d447f67 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 19:16:26 +0100 Subject: [PATCH 08/12] harmonize wb_bwe_dec_fx() --- lib_com/ivas_prot_fx.h | 3 +- lib_com/prot_fx.h | 157 +-- lib_com/swb_bwe_com_fx.c | 50 +- lib_dec/evs_dec_fx.c | 4 + lib_dec/ivas_core_dec_fx.c | 4 + lib_dec/swb_bwe_dec_fx.c | 1956 ++++++++++++++++++------------------ lib_dec/updt_dec_fx.c | 2 +- 7 files changed, 1052 insertions(+), 1124 deletions(-) diff --git a/lib_com/ivas_prot_fx.h b/lib_com/ivas_prot_fx.h index d87207a90..03e36f9fd 100644 --- a/lib_com/ivas_prot_fx.h +++ b/lib_com/ivas_prot_fx.h @@ -1891,6 +1891,7 @@ void GenShapedWBExcitation_ivas_fx( const Word16 igf_flag ); #endif +#ifndef HARM_FD_BWE /* o : Q_syn_hb*/ Word16 ivas_wb_bwe_dec_fx( Decoder_State *st_fx, /* i/o: decoder state structure */ @@ -1907,7 +1908,7 @@ Word16 ivas_wb_bwe_dec_fx( const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ Word16 *Qpost ); - +#endif void ivas_param_ism_config_fx( PARAM_ISM_CONFIG_HANDLE hParamIsm, /* i/o: IVAS Param ISM Config Structure */ const Word16 nchan_obj /* i : number of ISM channels */ diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index e7166845d..0f25bbd1f 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -317,6 +317,13 @@ Word32 calc_tilt_bwe_fx( const Word16 N /* i : signal length */ ); +/* o : Tilt in Q24 */ +Word16 calc_tilt_bwe_fx_32( + const Word32 *sp, /* i : i signal */ + const Word16 exp_sp, /* i : Exp of inp signal */ + const Word16 N /* i : signal length */ +); + void calc_norm_envelop_fx( const Word16 SWB_signal[], /* i : SWB spectrum Q_syn*/ Word32 *envelope, /* o : normalized envelope Q_syn*/ @@ -325,6 +332,14 @@ void calc_norm_envelop_fx( const Word16 st_offset /* i : offset */ ); +void calc_norm_envelop_fx_32( + const Word32 SWB_signal_fx[], /* i : SWB spectrum : Q12 */ + Word32 *envelope_fx, /* o : normalized envelope : Q16 */ + const Word16 L_swb_norm, /* i : length of envelope : Q0 */ + const Word16 SWB_flength, /* i : Length of i /output : Q0 */ + const Word16 st_offset /* i : offset : Q0 */ +); + void WB_BWE_decoding_fx( const Word16 *core_dec_freq, /* i : Frequency domain core decoded signal */ Word16 *WB_fenv, /* i : WB frequency envelopes */ @@ -378,14 +393,6 @@ void time_reduce_pre_echo_fx( Word16 Q_syn, Word16 Q_synth ); -void calc_norm_envelop_fx_32( - const Word32 SWB_signal_fx[], /* i : SWB spectrum : Q12 */ - Word32 *envelope_fx, /* o : normalized envelope : Q16 */ - const Word16 L_swb_norm, /* i : length of envelope : Q0 */ - const Word16 SWB_flength, /* i : Length of i /output : Q0 */ - const Word16 st_offset /* i : offset : Q0 */ -); - void hq_generic_decoding_fx( const Word16 HQ_mode, /* i : HQ mode */ Word32 *coeff_out1_fx, /* i/o: BWE i & temporary buffer */ @@ -2668,83 +2675,6 @@ Word16 E_UTIL_f_preemph3_ivas_fx( Word16 bits /* Q0 */ ); -Word16 WB_BWE_gain_pred_fx( - Word16 *WB_fenv, /* o : WB frequency envelopes */ - const Word16 *core_dec_freq, /* i : Frequency domain core decoded signal */ - const Word16 coder_type, /* i : coding type */ - Word16 prev_coder_type, /* i : coding type of last frame */ - Word16 prev_WB_fenv, /* i : envelope for last frame */ - Word16 *voice_factors, /* i : voicing factors Q15 */ - const Word16 pitch_buf[], /* i : pitch buffer Q6 */ - Word32 last_core_brate, /* i : previous frame core bitrate */ - Word16 last_wb_bwe_ener, /* i : previous frame wb bwe signal energy */ - Word16 Q_syn, - Word16 last_extl_fx, - Word16 tilt_wb_fx ); - -void calc_normal_length_fx( - const Word16 core, /* i : core */ - const Word16 *sp, /* i : i signal */ - const Word16 mode, /* i : i mode */ - const Word16 extl, /* i : extension layer */ - Word16 *L_swb_norm, /* o : normalize length */ - Word16 *prev_L_swb_norm, /*i/o : last normalize length */ - Word16 Q_syn ); - -/* o : Tilt in Q24 */ -Word16 ivas_calc_tilt_bwe_fx( - const Word32 *sp, /* i : i signal */ - const Word16 exp_sp, /* i : Exp of inp signal */ - const Word16 N /* i : signal length */ -); - -void calc_norm_envelop_fx( - const Word16 SWB_signal[], /* i : SWB spectrum Q_syn*/ - Word32 *envelope, /* o : normalized envelope Q_syn*/ - const Word16 L_swb_norm, /* i : length of envelope Q0 */ - const Word16 SWB_flength, /* i : Length of i /output */ - const Word16 st_offset /* i : offset */ -); - -void WB_BWE_decoding_fx( - const Word16 *core_dec_freq, /* i : Frequency domain core decoded signal */ - Word16 *WB_fenv, /* i : WB frequency envelopes */ - Word32 *WB_signal32, /* o : WB signal in MDCT domain */ - const Word16 WB_flength, /* i : Length of i /output */ - const Word16 mode, /* i : classification for WB signal */ - const Word16 last_extl, /* i : extl. layer for last frame */ - Word32 *prev_Energy, /* i/o: energy for last frame */ - Word16 *prev_WB_fenv, /* i/o: envelope for last frame */ - Word16 *prev_L_wb_norm, /* i/o: length for last frame wb norm */ - const Word16 extl, /* i : extension layer */ - const Word16 coder_type, /* i : coding type */ - const Word32 total_brate, /* i : core layer bitrate */ - Word16 *Seed, /* i/o: random generator seed */ - Word16 *prev_flag, /* i/o: attenu flag of last frame */ - Word16 prev_coder_type, /* i : coding type of last frame */ - Word16 Q_syn, - Word16 *Q_syn_hb /*o : Q value of WB_signal_32 */ -); - -void SWB_BWE_decoding_fx( - const Word16 *core_dec_freq, /* i : Frequency domain core decoded signal */ - Word16 *SWB_fenv, /* i/o: SWB frequency envelopes */ - Word32 *SWB_signal, /* o : SWB signal in MDCT domain */ - const Word16 SWB_flength, /* i : Length of i /output */ - const Word16 mode, /* i : classification for SWB signal */ - Word16 *frica_flag, /* o : fricative signal flag */ - Word16 *prev_Energy, /* i/o: energy for last frame */ - Word16 *prev_SWB_fenv, /* i/o: envelope for last frame */ - Word16 *prev_L_swb_norm, /* i/o: length for last frame wb norm */ - const Word16 tilt_nb, /* i : tilt of synthesis wb signal */ - Word16 *Seed, /* i/o: random generator seed */ - const Word16 st_offset, /* i : offset value due to different core */ - Word16 *prev_weight, /* i/o: excitation weight value of last frame */ - const Word16 extl, /* i : extension layer */ - Word16 Q_syn, - const Word16 last_extl /* i : extension layer of last frame */ -); - void time_envelop_shaping_fx( Word16 werr[], /* i/o: SHB synthesis Q_synth*/ Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ @@ -2757,50 +2687,6 @@ void time_envelop_shaping_ivas_fx( const Word16 L, /* i : frame length */ Word16 *Q_synth ); -void time_reduce_pre_echo_fx( - const Word16 *synth, /* i : ACELP core synthesis Q_syn*/ - Word16 *error, /* i/o: SHB BWE synthesis Q0*/ - Word16 prev_td_energy, /* o : last td energy Q0*/ - const Word16 L, /* i : subframe length */ - Word16 Q_syn, - Word16 Q_synth ); - -void calc_normal_length_fx_32( - const Word16 core, /* i : core : Q0 */ - const Word32 *sp, /* i : i signal : Q12 */ - const Word16 mode, /* i : i mode : Q0 */ - const Word16 extl, /* i : extension layer : Q0 */ - Word16 *L_swb_norm, /* o : normalize length : Q0 */ - Word16 *prev_L_swb_norm /*i/o : last normalize length : Q0 */ -); - -void calc_norm_envelop_fx_32( - const Word32 SWB_signal_fx[], /* i : SWB spectrum : Q12 */ - Word32 *envelope_fx, /* o : normalized envelope : Q16 */ - const Word16 L_swb_norm, /* i : length of envelope : Q0 */ - const Word16 SWB_flength, /* i : Length of i /output : Q0 */ - const Word16 st_offset /* i : offset : Q0 */ -); - -void hq_generic_decoding_fx( - const Word16 HQ_mode, /* i : HQ mode */ - Word32 *coeff_out1_fx, /* i/o: BWE i & temporary buffer */ - const Word16 *hq_generic_fenv_fx, /* i : SWB frequency envelopes */ - Word32 *coeff_out_fx, /* o : SWB signal in MDCT domain */ - const Word16 hq_generic_offset, /* i : frequency offset for representing hq generic*/ - Word16 *prev_L_swb_norm, /* i/o: last normalize length */ - const Word16 hq_generic_exc_clas, /* i : bwe excitation class */ - const Word16 *R ); - -void save_old_syn_fx( - const Word16 L_frame, /* i : frame length */ - const Word16 syn[], /* i : ACELP synthesis */ - Word16 old_syn[], /* o : old synthesis buffer */ - Word16 old_syn_mem[], /* i/o: old synthesis buffer memory */ - const Word16 preemph_fac, /* i : preemphasis factor */ - Word16 *mem_deemph /* i/o: deemphasis filter memory */ -); - void find_td_envelope_fx( const Word16 inp[], /* i : input signal Qx */ const Word16 len, /* i : length of the input signal */ @@ -7290,7 +7176,9 @@ Word16 WB_BWE_gain_deq_fx( Word16 *WB_fenv /*Q15*/ ); +/* o : Q_syn_hb*/ Word16 wb_bwe_dec_fx( +#ifndef HARM_FD_BWE Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis Q0/Qpost */ Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ const Word16 output_frame, /* i : frame length */ @@ -7298,6 +7186,17 @@ Word16 wb_bwe_dec_fx( const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ Decoder_State *st_fx, /* i/o: decoder state structure */ Word16 *Qpost ); +#else + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word32 output_fx32[], /* o : synthesis @internal Fs Q11*/ + Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis Q0/Qpost */ + Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ + const Word16 use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */ + const Word16 output_frame, /* i : frame length */ + Word16 *voice_factors_fx, /* i : voicing factors Q15 */ + const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ + Word16 *Qpost ); +#endif /* o : BWE class */ Word16 swb_bwe_gain_deq_fx( diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index a413064d2..50c3a11d3 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -338,18 +338,15 @@ Word16 WB_BWE_gain_pred_fx( * * calc_envelope of low frequency spectrum *-------------------------------------------------------------------*/ + static void calc_norm_envelop_lf_fx( - const Word32 SWB_signal[], - /* i : SWB spectrum */ /* Q12 */ - Word32 *envelope, - /* o : normalized envelope */ /* Q12 */ - Word16 *L_swb_norm, /* i/o : length of envelope */ - const Word16 HQ_mode, - /* i : HQ mode */ /* Q0 */ - const Word16 hq_generic_offset, - /* i : frequency offset for representing hq generic */ /* Q0 */ - Word16 *sfreq, /* i : starting frequency index */ - Word16 *efreq /* i : ending frequency index */ + const Word32 SWB_signal[], /* i : SWB spectrum Q12 */ + Word32 *envelope, /* o : normalized envelope Q12 */ + Word16 *L_swb_norm, /* i/o: length of envelope */ + const Word16 HQ_mode, /* i : HQ mode Q0 */ + const Word16 hq_generic_offset, /* i : frequency offset for representing hq generic Q0 */ + Word16 *sfreq, /* i : starting frequency index */ + Word16 *efreq /* i : ending frequency index */ ) { Word16 lookback, env_index, n_freq, n_lag_now, n_lag; @@ -423,6 +420,8 @@ static void calc_norm_envelop_lf_fx( return; } + + /*-------------------------------------------------------------------* * calc_normal_length() * @@ -550,16 +549,18 @@ void calc_normal_length_fx( return; } + /*-------------------------------------------------------------------* * calc_tilt_bwe() * * calculate tilt parameter *-------------------------------------------------------------------*/ -Word32 calc_tilt_bwe_fx( /* o : Tilt in Q24 */ - const Word16 *sp, /* i : input signal Q(15 - exp_sp) */ - const Word16 exp_sp, /* i : Exp of inp signal */ - const Word16 N /* i : signal length */ +/* o : Tilt in Q24 */ +Word32 calc_tilt_bwe_fx( + const Word16 *sp, /* i : input signal Q(15 - exp_sp) */ + const Word16 exp_sp, /* i : Exp of inp signal */ + const Word16 N /* i : signal length */ ) { Word16 i, j; @@ -645,10 +646,13 @@ Word32 calc_tilt_bwe_fx( /* o : Tilt in Q24 return L_temp; } -Word16 ivas_calc_tilt_bwe_fx( /* o : Tilt in Q24 */ - const Word32 *sp, /* i : input signal */ - const Word16 exp_sp, /* i : Exp of inp signal Q(15 - exp_sp) */ - const Word16 N /* i : signal length */ + + +/* o : Tilt in Q24 */ +Word16 calc_tilt_bwe_fx_32( + const Word32 *sp, /* i : input signal */ + const Word16 exp_sp, /* i : Exp of inp signal Q(15 - exp_sp) */ + const Word16 N /* i : signal length */ ) { Word16 i, j; @@ -809,11 +813,12 @@ void calc_norm_envelop_fx( /* _Word16 *Seed, i/o: random generator seed Q15 */ /* _Word16 *prev_flag, i/o: attenu flag of last frame Q0 */ /*--------------------------------------------------------------------------*/ -/* RETURN ARGUMENTS : */ -/* _ None */ +/* RETURN ARGUMENTS : */ +/* _ None */ /*--------------------------------------------------------------------------*/ -/* CALLED FROM : */ +/* CALLED FROM : */ /*==========================================================================*/ + void WB_BWE_decoding_fx( const Word16 *core_dec_freq, /* i : Frequency domain core decoded signal */ Word16 *WB_fenv, /* i : WB frequency envelopes */ @@ -1938,6 +1943,7 @@ return; /* RETURN ARGUMENTS : */ /* _ None */ /*--------------------------------------------------------------------------*/ + void time_envelop_shaping_fx( Word16 werr[], /* i/o: SHB synthesis Q_synth*/ Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ diff --git a/lib_dec/evs_dec_fx.c b/lib_dec/evs_dec_fx.c index 07b2deddc..9dd8f1873 100644 --- a/lib_dec/evs_dec_fx.c +++ b/lib_dec/evs_dec_fx.c @@ -370,7 +370,11 @@ ivas_error evs_dec_fx( ELSE IF( EQ_16( st_fx->extl, WB_BWE ) && st_fx->bws_cnt == 0 ) { /* WB BWE decoder */ +#ifdef HARM_FD_BWE + hb_synth_fx_exp = wb_bwe_dec_fx( st_fx, NULL, synth_fx, hb_synth_fx, 0, output_frame, voice_factors_fx, pitch_buf_fx, &Qpostd ); /*Q0*/ +#else hb_synth_fx_exp = wb_bwe_dec_fx( synth_fx, hb_synth_fx, output_frame, voice_factors_fx, pitch_buf_fx, st_fx, &Qpostd ); /*Q0*/ +#endif } /*---------------------------------------------------------------------* diff --git a/lib_dec/ivas_core_dec_fx.c b/lib_dec/ivas_core_dec_fx.c index 0b7e13a80..930ff5410 100644 --- a/lib_dec/ivas_core_dec_fx.c +++ b/lib_dec/ivas_core_dec_fx.c @@ -1073,7 +1073,11 @@ ivas_error ivas_core_dec_fx( { /* WB BWE decoder */ #ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING +#ifdef HARM_FD_BWE + Q_hb_synth_fx = wb_bwe_dec_fx( st, output_32_fx[n], synth_16_fx[n], hb_synth_16_fx[n], use_cldfb_for_dft, output_frame, voice_factors_fx[n], pitch_buf_fx[n], &Q_synth_fx ); +#else Q_hb_synth_fx = ivas_wb_bwe_dec_fx( st, output_32_fx[n], synth_16_fx[n], hb_synth_16_fx[n], use_cldfb_for_dft, output_frame, voice_factors_fx[n], pitch_buf_fx[n], &Q_synth_fx ); +#endif #else Q_hb_synth_fx = ivas_wb_bwe_dec_fx( st, output_16_fx[n], synth_16_fx[n], hb_synth_16_fx[n], use_cldfb_for_dft, output_frame, voice_factors_fx[n], pitch_buf_fx[n], &Q_synth_fx ); #endif diff --git a/lib_dec/swb_bwe_dec_fx.c b/lib_dec/swb_bwe_dec_fx.c index 61d5b3451..ef2a4fe25 100644 --- a/lib_dec/swb_bwe_dec_fx.c +++ b/lib_dec/swb_bwe_dec_fx.c @@ -354,7 +354,11 @@ Word16 WB_BWE_gain_deq_fx( *-------------------------------------------------------------------*/ /* o : Q_syn_hb */ +#ifdef HARM_FD_BWE +Word16 wb_bwe_dec_fx( +#else Word16 ivas_wb_bwe_dec_fx( +#endif Decoder_State *st_fx, /* i/o: decoder state structure */ #ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING Word32 output_fx32[], /* o : synthesis @internal Fs Q11*/ @@ -368,1216 +372,1226 @@ Word16 ivas_wb_bwe_dec_fx( Word16 *voice_factors_fx, /* i : voicing factors Q15 */ const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ Word16 *Qpost ) -{ - Word16 mode; - Word16 WB_fenv_fx[SWB_FENV]; - Word16 ysynth_fx[L_FRAME48k]; - Word16 Q_syn, exp, Q_syn_hb; - Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; + { + Word16 mode; + Word16 WB_fenv_fx[SWB_FENV]; + Word16 ysynth_fx[L_FRAME48k]; + Word16 Q_syn, exp, Q_syn_hb; + Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; #ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING - Word16 output[L_FRAME16k]; - Word16 Q_input, sf; + Word16 output[L_FRAME16k]; + Word16 Q_input, sf; #endif - Word16 scl, new_input_fx_exp; - Word16 i; - FD_BWE_DEC_HANDLE hBWE_FD; - Word16 ysynth_frame_size; - Word16 coder_type = st_fx->coder_type; - move16(); - - hBWE_FD = st_fx->hBWE_FD; + Word16 scl, new_input_fx_exp; + Word16 i; + FD_BWE_DEC_HANDLE hBWE_FD; + Word16 ysynth_frame_size; + Word16 coder_type = st_fx->coder_type; + move16(); - /* MDCT of the core synthesis signal */ - new_input_fx_exp = *Qpost; - move16(); - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) - { -#ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING - sf = getScaleFactor32( output_fx32, st_fx->L_frame ); + hBWE_FD = st_fx->hBWE_FD; - Q_input = 0; + /* MDCT of the core synthesis signal */ + new_input_fx_exp = *Qpost; move16(); - if ( LT_16( sf, 5 ) ) + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) { - Q_input = sub( sf, 5 ); - } +#ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING + sf = getScaleFactor32( output_fx32, st_fx->L_frame ); + + Q_input = 0; + move16(); + if ( LT_16( sf, 5 ) ) + { + Q_input = sub( sf, 5 ); + } - Copy_Scale_sig_32_16( output_fx32, output, st_fx->L_frame, sub( Q_input, Q11 ) ); // Q_input + Copy_Scale_sig_32_16( output_fx32, output, st_fx->L_frame, sub( Q_input, Q11 ) ); // Q_input #endif - /* IVAS_fmToDo: wtda() does not support L_FRAME length; thus temporarily resample the signal */ - /* IVAS_fmToDo: delay output[] by 1.25ms ? */ - lerp( output, ysynth_fx, L_FRAME16k, st_fx->L_frame ); + lerp( output, ysynth_fx, L_FRAME16k, st_fx->L_frame ); - wtda_fx( ysynth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k ); - *Qpost = sub( new_input_fx_exp, 15 ); - move16(); - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, /*st->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = L_FRAME16k; - } - ELSE - { - wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - output_frame ); - *Qpost = sub( new_input_fx_exp, 15 ); - move16(); - /* DCT of the ACELP core synthesis */ - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = output_frame; - } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = Find_Max_Norm32( ysynth_32, ysynth_frame_size ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); - } - Copy_Scale_sig32_16( ysynth_32, ysynth_fx, ysynth_frame_size, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - IF( !st_fx->bfi ) - { - IF( st_fx->extl_brate > 0 ) - { - /* de-quantization */ - mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); - st_fx->hBWE_FD->last_wb_bwe_ener_fx = extract_l( Mpy_32_16_r( L_add( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 /*0.5f in Q15*/ ) ); + wtda_fx( ysynth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k ); + + *Qpost = sub( new_input_fx_exp, 15 ); move16(); + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, /*st->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = L_FRAME16k; } ELSE { - Word32 tmp_brate; + wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, + &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ output_frame ); - tmp_brate = st_fx->last_core_brate; - move32(); - test(); - if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) - { - tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ - move32(); - } - if ( NE_16( st_fx->last_extl, WB_BWE ) ) + *Qpost = sub( new_input_fx_exp, 15 ); + move16(); + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = output_frame; + } + + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) + { + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = Find_Max_Norm32( ysynth_32, ysynth_frame_size ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); + } + Copy_Scale_sig32_16( ysynth_32, ysynth_fx, ysynth_frame_size, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); + + IF( !st_fx->bfi ) + { + IF( st_fx->extl_brate > 0 ) { - hBWE_FD->prev_SWB_fenv_fx[0] = 0; + /* de-quantization */ + mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); +#ifdef HARM_FD_BWE + IF( st_fx->element_mode == EVS_MONO ) + { + hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) + } + ELSE +#endif + { + hBWE_FD->last_wb_bwe_ener_fx = extract_l( Mpy_32_16_r( L_add( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 /*0.5f in Q15*/ ) ); + } move16(); } + ELSE + { + Word32 tmp_brate; + + tmp_brate = st_fx->last_core_brate; + move32(); + test(); + if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) + { + tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ + move32(); + } + if ( NE_16( st_fx->last_extl, WB_BWE ) ) + { + hBWE_FD->prev_SWB_fenv_fx[0] = 0; + move16(); + } - mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], - voice_factors_fx, pitch_buf_fx, tmp_brate, st_fx->hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); + mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], + voice_factors_fx, pitch_buf_fx, tmp_brate, st_fx->hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); + } } - } - ELSE - { - /* FEC */ - mode = NORMAL; - move16(); - FOR( i = 0; i < 2; i++ ) + ELSE { - WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); + /* FEC */ + mode = NORMAL; move16(); + FOR( i = 0; i < 2; i++ ) + { + WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); + move16(); + } } - } - test(); - IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) - { - Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); - } - - exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); - IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) - { - hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); - move32(); - } - ELSE - { - Q_syn = add( hBWE_FD->prev_Q_synth, exp ); - hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); - move32(); - } - - WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, - st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, - st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, - st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); - - IF( EQ_32( st_fx->output_Fs, 32000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); - } - ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); - } - Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + test(); + IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) + { + Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); + } - window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, - ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); + IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) + { + hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); + move32(); + } + ELSE + { + Q_syn = add( hBWE_FD->prev_Q_synth, exp ); + hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); + move32(); + } - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) - { - /* add HB synth from hf_synth() */ - v_add_16( hb_synth_fx, synth_fx, hb_synth_fx, output_frame ); - } + WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, + st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, + st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, + st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); - hBWE_FD->prev_mode = mode; - move16(); - hBWE_FD->prev_Q_synth = Q_syn; - move16(); + IF( EQ_32( st_fx->output_Fs, 32000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); + } + ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); + } - return Q_syn_hb; -} + Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, + ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); -Word16 wb_bwe_dec_fx( - Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis Q0/Qpost */ - Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ - const Word16 output_frame, /* i : frame length */ - Word16 *voice_factors_fx, /* i : voicing factors Q15 */ - const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ - Decoder_State *st_fx, /* i/o: decoder state structure */ - Word16 *Qpost ) -{ - Word16 mode; - Word16 WB_fenv_fx[SWB_FENV]; - Word16 ysynth_fx[L_FRAME48k]; - Word16 Q_syn, exp, Q_syn_hb; - Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; - Word16 scl, new_input_fx_exp; - Word16 i; - FD_BWE_DEC_HANDLE hBWE_FD; + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + { + /* add HB synth from hf_synth() */ + v_add_16( hb_synth_fx, synth_fx, hb_synth_fx, output_frame ); + } - Word16 coder_type = st_fx->coder_type; - move16(); + hBWE_FD->prev_mode = mode; + move16(); + hBWE_FD->prev_Q_synth = Q_syn; + move16(); - hBWE_FD = st_fx->hBWE_FD; + return Q_syn_hb; + } + +#ifndef HARM_FD_BWE + Word16 wb_bwe_dec_fx( + Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis Q0/Qpost */ + Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ + const Word16 output_frame, /* i : frame length */ + Word16 *voice_factors_fx, /* i : voicing factors Q15 */ + const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word16 *Qpost ) + { + Word16 mode; + Word16 WB_fenv_fx[SWB_FENV]; + Word16 ysynth_fx[L_FRAME48k]; + Word16 Q_syn, exp, Q_syn_hb; + Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; + Word16 scl, new_input_fx_exp; + Word16 i; + FD_BWE_DEC_HANDLE hBWE_FD; + + Word16 coder_type = st_fx->coder_type; + move16(); - /* MDCT of the core synthesis signal */ + hBWE_FD = st_fx->hBWE_FD; - new_input_fx_exp = *Qpost; - move16(); + /* MDCT of the core synthesis signal */ - { - wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - output_frame ); - *Qpost = sub( new_input_fx_exp, 15 ); + new_input_fx_exp = *Qpost; move16(); - /* DCT of the ACELP core synthesis */ - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); - } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); - } - Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - IF( !st_fx->bfi ) - { - IF( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) + { - /* de-quantization */ - mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); - hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) + wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, + &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ + output_frame ); + *Qpost = sub( new_input_fx_exp, 15 ); move16(); + /* DCT of the ACELP core synthesis */ + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); } - ELSE + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) { - Word32 tmp_brate; - - tmp_brate = st_fx->last_core_brate; - move32(); - - test(); - if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); + } + Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); + IF( !st_fx->bfi ) + { + IF( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) { - tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ - move32(); + /* de-quantization */ + mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); + hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) + move16(); } + ELSE + { + Word32 tmp_brate; - if ( NE_16( st_fx->last_extl, WB_BWE ) ) + tmp_brate = st_fx->last_core_brate; + move32(); + + test(); + if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) + { + tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ + move32(); + } + + if ( NE_16( st_fx->last_extl, WB_BWE ) ) + { + hBWE_FD->prev_SWB_fenv_fx[0] = 0; + move16(); + } + + mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], + voice_factors_fx, pitch_buf_fx, tmp_brate, hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); + } + } + ELSE + { + /* FEC */ + mode = NORMAL; + move16(); + FOR( i = 0; i < 2; i++ ) { - hBWE_FD->prev_SWB_fenv_fx[0] = 0; + WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); move16(); } - - mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], - voice_factors_fx, pitch_buf_fx, tmp_brate, hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); } - } - ELSE - { - /* FEC */ - mode = NORMAL; - move16(); - FOR( i = 0; i < 2; i++ ) + test(); + IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) { - WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); - move16(); + Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); } - } - test(); - IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) - { - Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); - } - - exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); - IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) - { - hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); - move32(); - } - ELSE - { - Q_syn = add( hBWE_FD->prev_Q_synth, exp ); - hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); - move32(); - } - - WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, - st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, - st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, - st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); - - IF( EQ_32( st_fx->output_Fs, 32000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); - } - ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); - } - - Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); - - window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, - ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - - hBWE_FD->prev_mode = mode; - move16(); - hBWE_FD->prev_Q_synth = Q_syn; - move16(); - - return Q_syn_hb; -} - -/*-------------------------------------------------------------------* - * swb_bwe_gain_deq() - * - * Decoding of SWB parameters - *-------------------------------------------------------------------*/ - -/* o : BWE class */ -Word16 swb_bwe_gain_deq_fx( - Decoder_State *st_fx, /* i/o: decoder state structure */ - const Word16 core, /* i : core : Q0 */ - Word16 *SWB_tenv, /* o : time-domain BWE envelope : Q0 */ - Word16 *SWB_fenv, /* o : frequency-domain BWE envelope : Q1 */ - const Word16 hr_flag, /* i : high rate flag : Q0 */ - const Word16 hqswb_clas /* i : HQ BWE class : Q0 */ -) -{ - Word16 index, mode, n_band; - Word16 indice[6]; - Word16 quant_tmp[SWB_FENV / 2], quant_tmp2[SWB_FENV / 2]; - Word16 nb_bits[6]; - Word16 nenv; - Word16 tmp, exp, frac; - Word32 L_tmp; - - IF( hqswb_clas > 0 ) - { - mode = (Word16) get_next_indice_fx( st_fx, 1 ); - IF( mode == 0 ) + exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); + IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) { - mode = (Word16) get_next_indice_fx( st_fx, 1 ); + hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); + move32(); } ELSE { - mode = HQ_GENERIC_SP_EXC; - move16(); + Q_syn = add( hBWE_FD->prev_Q_synth, exp ); + hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); + move32(); } - } - ELSE - { - mode = (Word16) get_next_indice_fx( st_fx, 2 ); - } - test(); - IF( EQ_16( mode, 1 ) && core == ACELP_CORE ) - { - FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) + WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, + st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, + st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, + st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); + + IF( EQ_32( st_fx->output_Fs, 32000 ) ) { - index = (Word16) get_next_indice_fx( st_fx, 4 ); - SWB_tenv[n_band] = shl_sat( 1, index ); - move16(); + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); + } + ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); } - indice[0] = (Word16) get_next_indice_fx( st_fx, 7 ); + Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + + window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, + ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + + hBWE_FD->prev_mode = mode; move16(); - indice[1] = (Word16) get_next_indice_fx( st_fx, 6 ); + hBWE_FD->prev_Q_synth = Q_syn; move16(); - tmp = shl( indice[0], 1 ); - FOR( n_band = 0; n_band < DIM_TR1; n_band++ ) - { - /*Env_TR_Cdbk1_fx[ indice[0]*DIM_TR1+n_band]*/ - quant_tmp[2 * n_band] = Env_TR_Cdbk1_fx[tmp + n_band]; /*Q8 */ - move16(); - } + return Q_syn_hb; + } +#endif - /*tmp = indice[1]*DIM_TR2*/ - tmp = shl( indice[1], 1 ); - quant_tmp[1] = add_sat( shr( add_sat( quant_tmp[0], quant_tmp[2] ), 1 ), Env_TR_Cdbk2_fx[tmp] ); - /* To prevent an unnecessary saturation, it would be better to write the code as below, but might have an impact on EVS bit exactness */ - /* quant_tmp[1] = add( extract_h( L_mac( L_mult( quant_tmp[0], 16384 ), quant_tmp[2], 16384 ) ), Env_TR_Cdbk2_fx[tmp] ); */ - move16(); /*Q8 */ - quant_tmp[3] = add( quant_tmp[2], Env_TR_Cdbk2_fx[tmp + 1] ); - move16(); /*Q8 */ - - FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) - { - tmp = add( quant_tmp[n_band], Mean_env_tr_fx[n_band] ); /*Q8 */ - - /*-----------------------------------------------------------------* - * SWB_fenv[n_band] = pow(10.0, tmp/40) - * = pow(2, 3.321928*tmp/40) - * = pow(2, 0.0830482*tmp) - *-----------------------------------------------------------------*/ - L_tmp = L_mult( tmp, 21771 ); /* 0.0830482 in Q18 -> Q27 */ - L_tmp = L_shr( L_tmp, 11 ); /* From Q27 to Q16 */ - frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ - tmp = extract_l( Pow2( 13, frac ) ); - exp = sub( exp, 13 ); - SWB_fenv[n_band] = shl( tmp, add( exp, 1 ) ); - move16(); /*Q1 */ - } - /* in case of band-width switching, attenuate frame gain */ - IF( st_fx->bws_cnt1 > 0 ) + /*-------------------------------------------------------------------* + * swb_bwe_gain_deq() + * + * Decoding of SWB parameters + *-------------------------------------------------------------------*/ + + /* o : BWE class */ + Word16 swb_bwe_gain_deq_fx( + Decoder_State *st_fx, /* i/o: decoder state structure */ + const Word16 core, /* i : core : Q0 */ + Word16 *SWB_tenv, /* o : time-domain BWE envelope : Q0 */ + Word16 *SWB_fenv, /* o : frequency-domain BWE envelope : Q1 */ + const Word16 hr_flag, /* i : high rate flag : Q0 */ + const Word16 hqswb_clas /* i : HQ BWE class : Q0 */ + ) + { + Word16 index, mode, n_band; + Word16 indice[6]; + Word16 quant_tmp[SWB_FENV / 2], quant_tmp2[SWB_FENV / 2]; + Word16 nb_bits[6]; + Word16 nenv; + Word16 tmp, exp, frac; + Word32 L_tmp; + + IF( hqswb_clas > 0 ) { - tmp = i_mult( st_fx->bws_cnt1, 1638 ); - FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) + mode = (Word16) get_next_indice_fx( st_fx, 1 ); + IF( mode == 0 ) { - SWB_tenv[n_band] = mult_r( SWB_tenv[n_band], tmp ); - move16(); + mode = (Word16) get_next_indice_fx( st_fx, 1 ); } - - FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) + ELSE { - SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], tmp ); + mode = HQ_GENERIC_SP_EXC; move16(); } } - } - ELSE - { - nb_bits[0] = 5; - move16(); - nb_bits[1] = 7; - move16(); - nb_bits[2] = 6; - move16(); - nb_bits[3] = 5; - move16(); - - IF( EQ_16( hr_flag, 1 ) ) - { - nb_bits[4] = 5; - move16(); - nenv = SWB_FENV - 2; - move16(); - } ELSE { - nb_bits[4] = 6; - move16(); - nenv = SWB_FENV; - move16(); + mode = (Word16) get_next_indice_fx( st_fx, 2 ); } - FOR( n_band = 0; n_band < 5; n_band++ ) + test(); + IF( EQ_16( mode, 1 ) && core == ACELP_CORE ) { - indice[n_band] = (Word16) get_next_indice_fx( st_fx, nb_bits[n_band] ); - move16(); - } + FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) + { + index = (Word16) get_next_indice_fx( st_fx, 4 ); + SWB_tenv[n_band] = shl_sat( 1, index ); + move16(); + } - IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) - { - indice[n_band] = (Word16) get_next_indice_fx( st_fx, 5 ); + indice[0] = (Word16) get_next_indice_fx( st_fx, 7 ); + move16(); + indice[1] = (Word16) get_next_indice_fx( st_fx, 6 ); move16(); - } - - Copy( &EnvCdbk11_fx[indice[0] * DIM11], quant_tmp, DIM11 ); - Copy( &EnvCdbk1st_fx[indice[1] * DIM1ST], quant_tmp2, DIM1ST ); - Copy( &EnvCdbk2nd_fx[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND ); - FOR( n_band = 0; n_band < ( DIM11 - 1 ); n_band++ ) - { - quant_tmp[n_band] = add( quant_tmp[n_band], quant_tmp2[n_band] ); + tmp = shl( indice[0], 1 ); + FOR( n_band = 0; n_band < DIM_TR1; n_band++ ) + { + /*Env_TR_Cdbk1_fx[ indice[0]*DIM_TR1+n_band]*/ + quant_tmp[2 * n_band] = Env_TR_Cdbk1_fx[tmp + n_band]; /*Q8 */ + move16(); + } + + /*tmp = indice[1]*DIM_TR2*/ + tmp = shl( indice[1], 1 ); + quant_tmp[1] = add_sat( shr( add_sat( quant_tmp[0], quant_tmp[2] ), 1 ), Env_TR_Cdbk2_fx[tmp] ); + /* To prevent an unnecessary saturation, it would be better to write the code as below, but might have an impact on EVS bit exactness */ + /* quant_tmp[1] = add( extract_h( L_mac( L_mult( quant_tmp[0], 16384 ), quant_tmp[2], 16384 ) ), Env_TR_Cdbk2_fx[tmp] ); */ move16(); /*Q8 */ - SWB_fenv[n_band * 2] = quant_tmp[n_band]; + quant_tmp[3] = add( quant_tmp[2], Env_TR_Cdbk2_fx[tmp + 1] ); move16(); /*Q8 */ - } - IF( EQ_16( hr_flag, 1 ) ) + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) + { + tmp = add( quant_tmp[n_band], Mean_env_tr_fx[n_band] ); /*Q8 */ + + /*-----------------------------------------------------------------* + * SWB_fenv[n_band] = pow(10.0, tmp/40) + * = pow(2, 3.321928*tmp/40) + * = pow(2, 0.0830482*tmp) + *-----------------------------------------------------------------*/ + L_tmp = L_mult( tmp, 21771 ); /* 0.0830482 in Q18 -> Q27 */ + L_tmp = L_shr( L_tmp, 11 ); /* From Q27 to Q16 */ + frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ + tmp = extract_l( Pow2( 13, frac ) ); + exp = sub( exp, 13 ); + SWB_fenv[n_band] = shl( tmp, add( exp, 1 ) ); + move16(); /*Q1 */ + } + /* in case of band-width switching, attenuate frame gain */ + IF( st_fx->bws_cnt1 > 0 ) + { + tmp = i_mult( st_fx->bws_cnt1, 1638 ); + FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) + { + SWB_tenv[n_band] = mult_r( SWB_tenv[n_band], tmp ); + move16(); + } + + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) + { + SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], tmp ); + move16(); + } + } + } + ELSE { - quant_tmp[6] = add( quant_tmp[6], quant_tmp2[6] ); - move16(); /*Q8 */ - SWB_fenv[11] = quant_tmp[6]; + nb_bits[0] = 5; + move16(); + nb_bits[1] = 7; + move16(); + nb_bits[2] = 6; + move16(); + nb_bits[3] = 5; move16(); - Copy( &EnvCdbk3rd_fx[indice[3] * DIM3RD], quant_tmp2, DIM3RD ); - Copy( &EnvCdbk3rd_fx[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD ); + IF( EQ_16( hr_flag, 1 ) ) + { + nb_bits[4] = 5; + move16(); + nenv = SWB_FENV - 2; + move16(); + } + ELSE + { + nb_bits[4] = 6; + move16(); + nenv = SWB_FENV; + move16(); + } FOR( n_band = 0; n_band < 5; n_band++ ) { - SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band + 1] ); - move16(); /*Q8 */ + indice[n_band] = (Word16) get_next_indice_fx( st_fx, nb_bits[n_band] ); + move16(); } - SWB_fenv[0] = add( SWB_fenv[0], quant_tmp2[0] ); - move16(); /*Q8 */ - } - ELSE - { - quant_tmp[DIM11 - 1] = add( quant_tmp[DIM11 - 1], quant_tmp2[DIM11 - 1] ); - move16(); /*Q8 */ - SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1]; - move16(); + IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) + { + indice[n_band] = (Word16) get_next_indice_fx( st_fx, 5 ); + move16(); + } - Copy( &EnvCdbk3rd_fx[i_mult2( indice[3], DIM3RD )], quant_tmp2, DIM3RD ); - Copy( &EnvCdbk4th_fx[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH ); + Copy( &EnvCdbk11_fx[indice[0] * DIM11], quant_tmp, DIM11 ); + Copy( &EnvCdbk1st_fx[indice[1] * DIM1ST], quant_tmp2, DIM1ST ); + Copy( &EnvCdbk2nd_fx[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND ); - FOR( n_band = 0; n_band < DIM12 - 1; n_band++ ) + FOR( n_band = 0; n_band < ( DIM11 - 1 ); n_band++ ) { - SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band] ); + quant_tmp[n_band] = add( quant_tmp[n_band], quant_tmp2[n_band] ); + move16(); /*Q8 */ + SWB_fenv[n_band * 2] = quant_tmp[n_band]; move16(); /*Q8 */ } - SWB_fenv[add( n_band * 2, 1 )] = add( quant_tmp[n_band], quant_tmp2[n_band] ); - move16(); /*Q8 */ - } + IF( EQ_16( hr_flag, 1 ) ) + { + quant_tmp[6] = add( quant_tmp[6], quant_tmp2[6] ); + move16(); /*Q8 */ + SWB_fenv[11] = quant_tmp[6]; + move16(); - FOR( n_band = 0; n_band < nenv; n_band++ ) - { - tmp = add( SWB_fenv[n_band], Mean_env_fx[n_band] ); /*Q8 */ + Copy( &EnvCdbk3rd_fx[indice[3] * DIM3RD], quant_tmp2, DIM3RD ); + Copy( &EnvCdbk3rd_fx[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD ); - L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */ - L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */ - frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ + FOR( n_band = 0; n_band < 5; n_band++ ) + { + SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band + 1] ); + move16(); /*Q8 */ + } - tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */ - /* output of Pow2() will be: */ - /* 16384 < Pow2() <= 32767 */ - exp = sub( exp, 13 ); - SWB_fenv[n_band] = shl_sat( tmp, add( exp, 1 ) ); - move16(); /*Q1 */ - } + SWB_fenv[0] = add( SWB_fenv[0], quant_tmp2[0] ); + move16(); /*Q8 */ + } + ELSE + { + quant_tmp[DIM11 - 1] = add( quant_tmp[DIM11 - 1], quant_tmp2[DIM11 - 1] ); + move16(); /*Q8 */ + SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1]; + move16(); - IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) - { - Copy( &EnvCdbkFB_fx[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB ); /*Q7 */ + Copy( &EnvCdbk3rd_fx[i_mult2( indice[3], DIM3RD )], quant_tmp2, DIM3RD ); + Copy( &EnvCdbk4th_fx[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH ); + + FOR( n_band = 0; n_band < DIM12 - 1; n_band++ ) + { + SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band] ); + move16(); /*Q8 */ + } - FOR( n_band = 0; n_band < DIM_FB; n_band++ ) + SWB_fenv[add( n_band * 2, 1 )] = add( quant_tmp[n_band], quant_tmp2[n_band] ); + move16(); /*Q8 */ + } + + FOR( n_band = 0; n_band < nenv; n_band++ ) { - tmp = add( SWB_fenv[n_band + nenv], Mean_env_fb_fx[n_band] ); - L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */ - L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */ + tmp = add( SWB_fenv[n_band], Mean_env_fx[n_band] ); /*Q8 */ + + L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */ + L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */ frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */ /* output of Pow2() will be: */ /* 16384 < Pow2() <= 32767 */ exp = sub( exp, 13 ); - SWB_fenv[n_band + nenv] = shl( tmp, add( exp, 1 ) ); - move16(); + SWB_fenv[n_band] = shl_sat( tmp, add( exp, 1 ) ); + move16(); /*Q1 */ } - } - /* in case of band-width switching, attenuate frame gain */ - } + IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) + { + Copy( &EnvCdbkFB_fx[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB ); /*Q7 */ - return mode; -} + FOR( n_band = 0; n_band < DIM_FB; n_band++ ) + { + tmp = add( SWB_fenv[n_band + nenv], Mean_env_fb_fx[n_band] ); + L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */ + L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */ + frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ + + tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */ + /* output of Pow2() will be: */ + /* 16384 < Pow2() <= 32767 */ + exp = sub( exp, 13 ); + SWB_fenv[n_band + nenv] = shl( tmp, add( exp, 1 ) ); + move16(); + } + } -/*-------------------------------------------------------------------* - * swb_bwe_dec() - * - * SWB BWE decoder - *-------------------------------------------------------------------*/ + /* in case of band-width switching, attenuate frame gain */ + } -/*o : Q_syn_hb */ -Word16 swb_bwe_dec_fx( - Decoder_State *st_fx, /* i/o: decoder state structure */ - Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis (might be rescaled inside wtda() ) Q0/Qpost */ - Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ - const Word16 output_frame, /* i : frame length */ - Word16 *Qpost ) -{ - Word16 i, l_subfr; - Word16 mode; - Word16 frica_flag = 0; - move16(); - Word16 idxGain; - Word16 Q_syn, Q_syn_hb; - Word16 ysynth_fx[L_FRAME48k]; - Word16 tmp; - Word16 SWB_tenv_fx[SWB_TENV]; - Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k]; - Word16 scl, new_input_fx_exp; - Word32 t_audio32_tmp[L_FRAME48k]; - Word32 SWB_tenv_tmp_fx[SWB_TENV]; - Word32 L_tmp; - Word16 exp, frac; - Word16 fb_ener_adjust_fx = 0; - move16(); - Word16 SWB_fenv_fx[SWB_FENV]; - Word16 L; - Word16 j = 0; - move16(); - Word16 ener_adjust_quan_fx; - Word16 tmp2; - Word16 fb_band_begin; - FD_BWE_DEC_HANDLE hBWE_FD; - hBWE_FD = st_fx->hBWE_FD; + return mode; + } - /*---------------------------------------------------------------------* - * SWB BWE decoding - *---------------------------------------------------------------------*/ - /* windowing of the ACELP core synthesis */ - new_input_fx_exp = *Qpost; - move16(); + /*-------------------------------------------------------------------* + * swb_bwe_dec() + * + * SWB BWE decoder + *-------------------------------------------------------------------*/ + /*o : Q_syn_hb */ + Word16 swb_bwe_dec_fx( + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis (might be rescaled inside wtda() ) Q0/Qpost */ + Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ + const Word16 output_frame, /* i : frame length */ + Word16 *Qpost ) { - wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &hBWE_FD->old_wtda_swb_fx_exp, - ALDO_WINDOW, - ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - output_frame ); - *Qpost = sub( new_input_fx_exp, 15 ); + Word16 i, l_subfr; + Word16 mode; + Word16 frica_flag = 0; + move16(); + Word16 idxGain; + Word16 Q_syn, Q_syn_hb; + Word16 ysynth_fx[L_FRAME48k]; + Word16 tmp; + Word16 SWB_tenv_fx[SWB_TENV]; + Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k]; + Word16 scl, new_input_fx_exp; + Word32 t_audio32_tmp[L_FRAME48k]; + Word32 SWB_tenv_tmp_fx[SWB_TENV]; + Word32 L_tmp; + Word16 exp, frac; + Word16 fb_ener_adjust_fx = 0; + move16(); + Word16 SWB_fenv_fx[SWB_FENV]; + Word16 L; + Word16 j = 0; + move16(); + Word16 ener_adjust_quan_fx; + Word16 tmp2; + Word16 fb_band_begin; + FD_BWE_DEC_HANDLE hBWE_FD; + hBWE_FD = st_fx->hBWE_FD; + + /*---------------------------------------------------------------------* + * SWB BWE decoding + *---------------------------------------------------------------------*/ + /* windowing of the ACELP core synthesis */ + new_input_fx_exp = *Qpost; move16(); - /* DCT of the ACELP core synthesis */ - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); - } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); - } - Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - IF( !st_fx->bfi ) - { - IF( st_fx->bws_cnt > 0 ) { - /* estimate parameters */ - mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); + wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, + &hBWE_FD->old_wtda_swb_fx_exp, + ALDO_WINDOW, + ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ + output_frame ); + *Qpost = sub( new_input_fx_exp, 15 ); + move16(); + /* DCT of the ACELP core synthesis */ + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); } - ELSE + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) { - /* de-quantization */ - mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); } + Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - L = SWB_FENV; - move16(); - if ( EQ_16( mode, TRANSIENT ) ) + IF( !st_fx->bfi ) { - L = SWB_FENV_TRANS; + IF( st_fx->bws_cnt > 0 ) + { + /* estimate parameters */ + mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); + } + ELSE + { + /* de-quantization */ + mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); + } + + L = SWB_FENV; move16(); - } - L_tmp = 0; - move16(); - FOR( i = 0; i < L; i++ ) - { - L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); - } + if ( EQ_16( mode, TRANSIENT ) ) + { + L = SWB_FENV_TRANS; + move16(); + } + L_tmp = 0; + move16(); + FOR( i = 0; i < L; i++ ) + { + L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); + } - exp = norm_s( L ); - tmp = div_s( shl( 1, sub( 14, exp ) ), L ); /*Q(29-exp) */ + exp = norm_s( L ); + tmp = div_s( shl( 1, sub( 14, exp ) ), L ); /*Q(29-exp) */ - L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(1+29-exp+1-16)->Q(15-exp) */ - st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( exp, 2 ) ) ); /*Q1 */ - move16(); - } - ELSE - { - /* SHB FEC */ - IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) - { - mode = hBWE_FD->prev_mode; + L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(1+29-exp+1-16)->Q(15-exp) */ + st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( exp, 2 ) ) ); /*Q1 */ move16(); } ELSE { - mode = NORMAL; - move16(); - } - - Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); - } - - /* reconstruction of MDCT spectrum of the error signal */ - set32_fx( ysynth_32, 0, output_frame ); - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) - { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, - &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); - } - ELSE - { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, - &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); - } - - test(); - IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) - { - FOR( i = 0; i < L_SUBFR; i++ ) - { - tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ - hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); - move16(); /*Q_synth */ - } + /* SHB FEC */ + IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) + { + mode = hBWE_FD->prev_mode; + move16(); + } + ELSE + { + mode = NORMAL; + move16(); + } - FOR( ; i < output_frame; i++ ) - { - hBWE_FD->mem_imdct_fx[i] = 0; - move16(); + Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); } - } - /* decode information */ - IF( EQ_16( st_fx->extl, FB_BWE ) ) - { - IF( !st_fx->bfi ) + /* reconstruction of MDCT spectrum of the error signal */ + set32_fx( ysynth_32, 0, output_frame ); + IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { - idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); - fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, + &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); } - ELSE IF( st_fx->bfi ) + ELSE { - fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; - move16(); + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, + &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); } - hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; - move16(); - IF( EQ_16( mode, TRANSIENT ) ) + test(); + IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) { - ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); /*Q13*/ + FOR( i = 0; i < L_SUBFR; i++ ) + { + tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ + hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); + move16(); /*Q_synth */ + } + + FOR( ; i < output_frame; i++ ) + { + hBWE_FD->mem_imdct_fx[i] = 0; + move16(); + } } - ELSE + + /* decode information */ + IF( EQ_16( st_fx->extl, FB_BWE ) ) { - IF( SWB_fenv_fx[7] != 0 ) + IF( !st_fx->bfi ) { - tmp = div_s( 1, SWB_fenv_fx[7] ); - move16(); /*Q14*/ - ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); /*Q13*/ + idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); + fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ + } + ELSE IF( st_fx->bfi ) + { + fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; + move16(); + } + + hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; + move16(); + IF( EQ_16( mode, TRANSIENT ) ) + { + ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); /*Q13*/ } ELSE { - ener_adjust_quan_fx = 0; - move16(); /*Q13*/ + IF( SWB_fenv_fx[7] != 0 ) + { + tmp = div_s( 1, SWB_fenv_fx[7] ); + move16(); /*Q14*/ + ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); /*Q13*/ + } + ELSE + { + ener_adjust_quan_fx = 0; + move16(); /*Q13*/ + } } - } - fb_band_begin = FB_BAND_BEGIN; - move16(); + fb_band_begin = FB_BAND_BEGIN; + move16(); - FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) - { - tmp = sub( 32767, i_mult( j, 1024 ) ); - tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ + FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) + { + tmp = sub( 32767, i_mult( j, 1024 ) ); + tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ - tmp2 = i_mult( j, 256 ); /*Q13*/ - tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ + tmp2 = i_mult( j, 256 ); /*Q13*/ + tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ - tmp = add( tmp, tmp2 ); /*Q13*/ - ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; - move16(); - ysynth_32[i] = L_shl( Mult_32_16( ysynth_32[i], tmp ), 2 ); - move32(); /*15+Q_syn */ - j = add( j, 1 ); - } + tmp = add( tmp, tmp2 ); /*Q13*/ + ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; + move16(); + ysynth_32[i] = L_shl( Mult_32_16( ysynth_32[i], tmp ), 2 ); + move32(); /*15+Q_syn */ + j = add( j, 1 ); + } - FOR( ; i < FB_BAND_END; i++ ) - { - ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; - move16(); - ysynth_32[i] = Mult_32_16( ysynth_32[i], fb_ener_adjust_fx ); - move32(); /*15+Q_syn */ + FOR( ; i < FB_BAND_END; i++ ) + { + ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; + move16(); + ysynth_32[i] = Mult_32_16( ysynth_32[i], fb_ener_adjust_fx ); + move32(); /*15+Q_syn */ + } } - } - Q_syn_hb = add( Q_syn, Q_32_BITS ); - Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); - window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - l_subfr = mult( output_frame, 8192 ); + Q_syn_hb = add( Q_syn, Q_32_BITS ); + Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + l_subfr = mult( output_frame, 8192 ); - test(); - IF( EQ_16( mode, TRANSIENT ) ) - { - FOR( i = 0; i < SWB_TENV; i++ ) + test(); + IF( EQ_16( mode, TRANSIENT ) ) { - SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i], 26214 ); - move32(); /*Q15 */ - } + FOR( i = 0; i < SWB_TENV; i++ ) + { + SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i], 26214 ); + move32(); /*Q15 */ + } - /* time envelope shaping when the current frame is TRANSIENT frame */ - time_envelop_shaping_fx( hb_synth_fx, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); - Q_syn_hb = sub( Q_syn_hb, 3 ); + /* time envelope shaping when the current frame is TRANSIENT frame */ + time_envelop_shaping_fx( hb_synth_fx, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); + Q_syn_hb = sub( Q_syn_hb, 3 ); - hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; - move16(); - } - ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) - { - time_reduce_pre_echo_fx( synth_fx, hb_synth_fx, hBWE_FD->prev_td_energy_fx, l_subfr, *Qpost, Q_syn_hb ); - } - ELSE - { - tmp = i_mult2( 3, l_subfr ); - L_tmp = L_deposit_l( 0 ); - FOR( i = 0; i < l_subfr; i++ ) + hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; + move16(); + } + ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) { - L_tmp = L_mac0_sat( L_tmp, hb_synth_fx[add( tmp, i )], hb_synth_fx[add( tmp, i )] ); /*(2*Q_syn_hb) */ + time_reduce_pre_echo_fx( synth_fx, hb_synth_fx, hBWE_FD->prev_td_energy_fx, l_subfr, *Qpost, Q_syn_hb ); } - L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ - - hBWE_FD->prev_td_energy_fx = 0; - move16(); - IF( L_tmp != 0 ) + ELSE { - exp = norm_l( L_tmp ); - frac = extract_h( L_shl( L_tmp, exp ) ); - exp = sub( exp, sub( 30, shl( Q_syn_hb, 1 ) ) ); + tmp = i_mult2( 3, l_subfr ); + L_tmp = L_deposit_l( 0 ); + FOR( i = 0; i < l_subfr; i++ ) + { + L_tmp = L_mac0_sat( L_tmp, hb_synth_fx[add( tmp, i )], hb_synth_fx[add( tmp, i )] ); /*(2*Q_syn_hb) */ + } + L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ - tmp = div_s( 16384, frac ); - L_tmp = L_deposit_h( tmp ); - L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */ - hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( exp, 15 ) ) ); /*Q0 */ + hBWE_FD->prev_td_energy_fx = 0; move16(); + IF( L_tmp != 0 ) + { + exp = norm_l( L_tmp ); + frac = extract_h( L_shl( L_tmp, exp ) ); + exp = sub( exp, sub( 30, shl( Q_syn_hb, 1 ) ) ); + + tmp = div_s( 16384, frac ); + L_tmp = L_deposit_h( tmp ); + L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */ + hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( exp, 15 ) ) ); /*Q0 */ + move16(); + } } - } - - hBWE_FD->prev_frica_flag = frica_flag; - move16(); - hBWE_FD->prev_mode = mode; - move16(); - - return Q_syn_hb; -} - - -/*-------------------------------------------------------------------* - * fd_bwe_dec_init() - * - * Initialize FD BWE state structure at the decoder - *-------------------------------------------------------------------*/ -void fd_bwe_dec_init_fx( - FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle */ -) -{ - set16_fx( hBWE_FD->old_syn_12k8_16k_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) ); - set16_fx( hBWE_FD->L_old_wtda_swb_fx, 0, L_FRAME48k ); - hBWE_FD->old_wtda_swb_fx_exp = 0; - move16(); + hBWE_FD->prev_frica_flag = frica_flag; + move16(); + hBWE_FD->prev_mode = mode; + move16(); - hBWE_FD->prev_mode = NORMAL; - move16(); - set16_fx( hBWE_FD->prev_SWB_fenv_fx, 0, SWB_FENV ); - hBWE_FD->prev_Energy_fx = 0; - move16(); - hBWE_FD->prev_L_swb_norm = 8; - move16(); - hBWE_FD->Seed = 21211; - move16(); - hBWE_FD->prev_frica_flag = 0; - move16(); - set16_fx( hBWE_FD->mem_imdct_fx, 0, L_FRAME48k ); - hBWE_FD->mem_imdct_exp_fx = 0; - move16(); - hBWE_FD->prev_td_energy_fx = 0; - move16(); - hBWE_FD->prev_weight_fx = 6554; /*0.2 in Q15*/ - move16(); - hBWE_FD->prev_flag = 0; - move16(); - hBWE_FD->last_wb_bwe_ener_fx = 0; - move16(); - hBWE_FD->prev_Energy_wb_fx = L_deposit_l( 0 ); - move32(); - hBWE_FD->memExp1 = 0; - move16(); + return Q_syn_hb; + } - /* Previous frame LPC initialization for PPP */ - hBWE_FD->prev_Q_synth = 0; - move16(); - hBWE_FD->mem_deemph_old_syn_fx = 0; - move16(); - hBWE_FD->prev_fb_ener_adjust_fx = 0; - move16(); + /*-------------------------------------------------------------------* + * fd_bwe_dec_init() + * + * Initialize FD BWE state structure at the decoder + *-------------------------------------------------------------------*/ - return; -} + void fd_bwe_dec_init_fx( + FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle */ + ) + { + set16_fx( hBWE_FD->old_syn_12k8_16k_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) ); + set16_fx( hBWE_FD->L_old_wtda_swb_fx, 0, L_FRAME48k ); + hBWE_FD->old_wtda_swb_fx_exp = 0; + move16(); -/*-------------------------------------------------------------------* - * WB_BWE_gain_deq() - * - * Decoding of WB parameters - *-------------------------------------------------------------------*/ + hBWE_FD->prev_mode = NORMAL; + move16(); + set16_fx( hBWE_FD->prev_SWB_fenv_fx, 0, SWB_FENV ); + hBWE_FD->prev_Energy_fx = 0; + move16(); + hBWE_FD->prev_L_swb_norm = 8; + move16(); + hBWE_FD->Seed = 21211; + move16(); + hBWE_FD->prev_frica_flag = 0; + move16(); + set16_fx( hBWE_FD->mem_imdct_fx, 0, L_FRAME48k ); + hBWE_FD->mem_imdct_exp_fx = 0; + move16(); + hBWE_FD->prev_td_energy_fx = 0; + move16(); + hBWE_FD->prev_weight_fx = 6554; /*0.2 in Q15*/ + move16(); + hBWE_FD->prev_flag = 0; + move16(); + hBWE_FD->last_wb_bwe_ener_fx = 0; + move16(); + hBWE_FD->prev_Energy_wb_fx = L_deposit_l( 0 ); + move32(); + hBWE_FD->memExp1 = 0; + move16(); + /* Previous frame LPC initialization for PPP */ + hBWE_FD->prev_Q_synth = 0; + move16(); -Word16 swb_bwe_dec_fx32( - Decoder_State *st_fx, /* i/o: decoder state structure */ - Word32 output_fx[], /* i : synthesis @internal Fs : Q11 */ - Word32 *synth_fx, /* i : ACELP core synthesis/final synthesis : Q11 */ - Word32 *hb_synth_fx, /* o : SHB synthesis/final synthesis : Q_syn_hb */ - Word16 use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */ - Word16 output_frame /* i : frame length */ -) -{ - Word16 L; - Word16 mode; - Word16 idxGain; - Word16 i, j, l_subfr; - Word16 fb_band_begin; - Word16 frica_flag = 0; - move16(); - Word16 ener_adjust_quan_fx; - Word16 fb_ener_adjust_fx = 0; - move16(); - Word16 scl, new_input_fx_exp, ysynth_frame_size; - Word16 Q_syn, Q_syn_hb, tmp, tmp2, q_tmp, Qsynth_fx16; + hBWE_FD->mem_deemph_old_syn_fx = 0; + move16(); + hBWE_FD->prev_fb_ener_adjust_fx = 0; + move16(); - Word16 ysynth_fx[L_FRAME48k]; - Word16 SWB_fenv_fx[SWB_FENV]; - Word16 SWB_tenv_fx[SWB_TENV]; - Word16 synth_fx16[L_FRAME48k]; - Word16 hb_synth_fx16[L_FRAME48k]; + return; + } - Word32 L_tmp; - Word32 yerror_fx[L_FRAME48k]; - Word32 ysynth_fx32[L_FRAME48k]; - Word32 SWB_tenv_tmp_fx[SWB_TENV]; - Word32 wtda_synth_fx[2 * L_FRAME48k]; + /*-------------------------------------------------------------------* + * swb_bwe_dec_fx32() + * + * SWB BWE decoder, 32-bit variant + *-------------------------------------------------------------------*/ - FD_BWE_DEC_HANDLE hBWE_FD; - hBWE_FD = st_fx->hBWE_FD; - /*---------------------------------------------------------------------* - * SWB BWE decoding - *---------------------------------------------------------------------*/ - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + Word16 swb_bwe_dec_fx32( + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word32 output_fx[], /* i : synthesis @internal Fs : Q11 */ + Word32 *synth_fx, /* i : ACELP core synthesis/final synthesis : Q11 */ + Word32 *hb_synth_fx, /* o : SHB synthesis/final synthesis : Q_syn_hb */ + Word16 use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */ + Word16 output_frame /* i : frame length */ + ) { - /* todo - wtda() does not support L_FRAME length; thus temporarily resample the signal */ - /* todo - delay output[] by 1.25ms ? */ - L_lerp_fx_q11( output_fx, ysynth_fx32, L_FRAME16k, st_fx->L_frame ); - - /* windowing of the ACELP core synthesis */ - wtda_fx32( ysynth_fx32, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, /*st_fx->L_frame*/ L_FRAME16k ); - - /* DCT of the ACELP core synthesis */ - new_input_fx_exp = 11; + Word16 L; + Word16 mode; + Word16 idxGain; + Word16 i, j, l_subfr; + Word16 fb_band_begin; + Word16 frica_flag = 0; move16(); - direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, /*st_fx->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = L_FRAME16k; + Word16 ener_adjust_quan_fx; + Word16 fb_ener_adjust_fx = 0; move16(); - } - ELSE - { - /* windowing of the ACELP core synthesis */ - wtda_fx32( synth_fx, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, output_frame ); + Word16 scl, new_input_fx_exp, ysynth_frame_size; + Word16 Q_syn, Q_syn_hb, tmp, tmp2, q_tmp, Qsynth_fx16; + + Word16 ysynth_fx[L_FRAME48k]; + Word16 SWB_fenv_fx[SWB_FENV]; + Word16 SWB_tenv_fx[SWB_TENV]; + Word16 synth_fx16[L_FRAME48k]; + Word16 hb_synth_fx16[L_FRAME48k]; + + Word32 L_tmp; + Word32 yerror_fx[L_FRAME48k]; + Word32 ysynth_fx32[L_FRAME48k]; + Word32 SWB_tenv_tmp_fx[SWB_TENV]; + Word32 wtda_synth_fx[2 * L_FRAME48k]; + + FD_BWE_DEC_HANDLE hBWE_FD; + hBWE_FD = st_fx->hBWE_FD; + + /*---------------------------------------------------------------------* + * SWB BWE decoding + *---------------------------------------------------------------------*/ + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + { + /* todo - wtda() does not support L_FRAME length; thus temporarily resample the signal */ + /* todo - delay output[] by 1.25ms ? */ + L_lerp_fx_q11( output_fx, ysynth_fx32, L_FRAME16k, st_fx->L_frame ); - /* DCT of the ACELP core synthesis */ - new_input_fx_exp = 11; - move16(); - direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = output_frame; - move16(); - } + /* windowing of the ACELP core synthesis */ + wtda_fx32( ysynth_fx32, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, /*st_fx->L_frame*/ L_FRAME16k ); - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + 11, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = sub( Find_Max_Norm32( ysynth_fx32, ysynth_frame_size ), 3 ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); - } - /*Don't upscale if already in 15 bits*/ - IF( GT_16( scl, 15 ) ) - { - FOR( i = 0; i < ysynth_frame_size; i++ ) - { - ysynth_fx[i] = extract_l( ysynth_fx32[i] ); + /* DCT of the ACELP core synthesis */ + new_input_fx_exp = 11; + move16(); + direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, /*st_fx->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = L_FRAME16k; move16(); - } - Q_syn = new_input_fx_exp; - move16(); - } - ELSE - { - Copy_Scale_sig32_16( ysynth_fx32, ysynth_fx, ysynth_frame_size, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - } - - IF( !st_fx->bfi ) - { - IF( st_fx->bws_cnt > 0 ) - { - /* estimate parameters */ - mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); } ELSE { - /* de-quantization */ - mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); - } + /* windowing of the ACELP core synthesis */ + wtda_fx32( synth_fx, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, output_frame ); - L = SWB_FENV; - move16(); - if ( EQ_16( mode, TRANSIENT ) ) - { - L = SWB_FENV_TRANS; + /* DCT of the ACELP core synthesis */ + new_input_fx_exp = 11; + move16(); + direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = output_frame; move16(); } - L_tmp = 0; - move32(); - FOR( i = 0; i < L; i++ ) + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + 11, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) { - L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = sub( Find_Max_Norm32( ysynth_fx32, ysynth_frame_size ), 3 ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); } - q_tmp = norm_s( L ); - tmp = div_s( shl( 1, sub( 14, q_tmp ) ), L ); /*Q(29-q_tmp) */ - L_tmp = Mpy_32_16_1( L_tmp, tmp ); /*Q(1+29-q_tmp+1-16)->Q(15-q_tmp) */ - st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( q_tmp, 2 ) ) ); /*Q1 */ - move16(); - } - ELSE - { - /* SHB FEC */ - - IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) + /*Don't upscale if already in 15 bits*/ + IF( GT_16( scl, 15 ) ) { - mode = hBWE_FD->prev_mode; + FOR( i = 0; i < ysynth_frame_size; i++ ) + { + ysynth_fx[i] = extract_l( ysynth_fx32[i] ); + move16(); + } + Q_syn = new_input_fx_exp; move16(); } ELSE { - mode = NORMAL; - move16(); + Copy_Scale_sig32_16( ysynth_fx32, ysynth_fx, ysynth_frame_size, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); } - Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); - } - - /* reconstruction of MDCT spectrum of the error signal */ - set32_fx( yerror_fx, 0, output_frame ); + IF( !st_fx->bfi ) + { + IF( st_fx->bws_cnt > 0 ) + { + /* estimate parameters */ + mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); + } + ELSE + { + /* de-quantization */ + mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); + } - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) - { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); - } - ELSE - { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); - } + L = SWB_FENV; + move16(); + if ( EQ_16( mode, TRANSIENT ) ) + { + L = SWB_FENV_TRANS; + move16(); + } - test(); - IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) - { - FOR( i = 0; i < L_SUBFR; i++ ) - { - tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ - hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); + L_tmp = 0; + move32(); + FOR( i = 0; i < L; i++ ) + { + L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); + } + q_tmp = norm_s( L ); + tmp = div_s( shl( 1, sub( 14, q_tmp ) ), L ); /*Q(29-q_tmp) */ + L_tmp = Mpy_32_16_1( L_tmp, tmp ); /*Q(1+29-q_tmp+1-16)->Q(15-q_tmp) */ + st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( q_tmp, 2 ) ) ); /*Q1 */ move16(); } - - FOR( ; i < output_frame; i++ ) + ELSE { - hBWE_FD->mem_imdct_fx[i] = 0; - move16(); + /* SHB FEC */ + + IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) + { + mode = hBWE_FD->prev_mode; + move16(); + } + ELSE + { + mode = NORMAL; + move16(); + } + + Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); } - } - /* decode information */ - IF( EQ_16( st_fx->extl, FB_BWE ) ) - { - IF( !st_fx->bfi ) + /* reconstruction of MDCT spectrum of the error signal */ + set32_fx( yerror_fx, 0, output_frame ); + + IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) { - idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); - fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); } - ELSE IF( st_fx->bfi ) + ELSE { - fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; - move16(); + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); } - hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; - move16(); - IF( EQ_16( mode, TRANSIENT ) ) + test(); + IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) { - ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); + FOR( i = 0; i < L_SUBFR; i++ ) + { + tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ + hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); + move16(); + } + + FOR( ; i < output_frame; i++ ) + { + hBWE_FD->mem_imdct_fx[i] = 0; + move16(); + } } - ELSE + + /* decode information */ + IF( EQ_16( st_fx->extl, FB_BWE ) ) { - IF( SWB_fenv_fx[7] != 0 ) + IF( !st_fx->bfi ) { - tmp = div_s( 1, SWB_fenv_fx[7] ); - ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); + idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); + fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ + } + ELSE IF( st_fx->bfi ) + { + fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; + move16(); + } + + hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; + move16(); + IF( EQ_16( mode, TRANSIENT ) ) + { + ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); } ELSE { - ener_adjust_quan_fx = 0; + IF( SWB_fenv_fx[7] != 0 ) + { + tmp = div_s( 1, SWB_fenv_fx[7] ); + ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); + } + ELSE + { + ener_adjust_quan_fx = 0; + move16(); + } + } + + fb_band_begin = FB_BAND_BEGIN; + move16(); + if ( EQ_16( st_fx->L_frame, L_FRAME ) ) + { + fb_band_begin = FB_BAND_BEGIN_12k8; move16(); } - } - fb_band_begin = FB_BAND_BEGIN; - move16(); - if ( EQ_16( st_fx->L_frame, L_FRAME ) ) - { - fb_band_begin = FB_BAND_BEGIN_12k8; + j = 0; move16(); + FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) + { + tmp = sub( 32767, i_mult( j, 1024 ) ); + tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ + + tmp2 = i_mult( j, 256 ); /*Q13*/ + tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ + + tmp = add( tmp, tmp2 ); /*Q13*/ + yerror_fx[i] = L_shl( Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], tmp ), 2 ); /*15+Q_syn */ + move32(); + j = add( j, 1 ); + } + FOR( ; i < FB_BAND_END; i++ ) + { + yerror_fx[i] = Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], fb_ener_adjust_fx ); /*15+Q_syn */ + move32(); + } } - j = 0; - move16(); - FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) - { - tmp = sub( 32767, i_mult( j, 1024 ) ); - tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ + /* iDCT of the error signal */ + Q_syn_hb = add( Q_syn, 15 ); + Inverse_Transform( yerror_fx, &Q_syn_hb, wtda_synth_fx, 0, output_frame, -1, st_fx->element_mode ); - tmp2 = i_mult( j, 256 ); /*Q13*/ - tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ + /* inverse windowing of the error signal */ + window_ola_fx( wtda_synth_fx, hb_synth_fx16, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + l_subfr = mult( output_frame, 8192 ); - tmp = add( tmp, tmp2 ); /*Q13*/ - yerror_fx[i] = L_shl( Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], tmp ), 2 ); /*15+Q_syn */ - move32(); - j = add( j, 1 ); - } - FOR( ; i < FB_BAND_END; i++ ) + test(); + IF( EQ_16( mode, TRANSIENT ) ) { - yerror_fx[i] = Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], fb_ener_adjust_fx ); /*15+Q_syn */ - move32(); - } - } + FOR( i = 0; i < SWB_TENV; i++ ) + { + SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i] /*Q0*/, 26214 /*0.8f Q15*/ ); // Q15 + move32(); + } - /* iDCT of the error signal */ - Q_syn_hb = add( Q_syn, 15 ); - Inverse_Transform( yerror_fx, &Q_syn_hb, wtda_synth_fx, 0, output_frame, -1, st_fx->element_mode ); + /* time envelope shaping when the current frame is TRANSIENT frame */ + time_envelop_shaping_ivas_fx( hb_synth_fx16, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); - /* inverse windowing of the error signal */ - window_ola_fx( wtda_synth_fx, hb_synth_fx16, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - l_subfr = mult( output_frame, 8192 ); + Q_syn_hb = sub( Q_syn_hb, 3 ); - test(); - IF( EQ_16( mode, TRANSIENT ) ) - { - FOR( i = 0; i < SWB_TENV; i++ ) - { - SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i] /*Q0*/, 26214 /*0.8f Q15*/ ); // Q15 - move32(); + hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; + move16(); } + ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) + { + Qsynth_fx16 = Find_Max_Norm32( synth_fx, output_frame ); + Qsynth_fx16 = sub( Qsynth_fx16, shr( add( sub( 15, norm_s( l_subfr ) ), 1 ), 1 ) ); + Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); + Qsynth_fx16 = add( 11 - 16, Qsynth_fx16 ); - /* time envelope shaping when the current frame is TRANSIENT frame */ - time_envelop_shaping_ivas_fx( hb_synth_fx16, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); + /* IVAS_fmToDo: synth[] is @internal_Fs!!! */ + time_reduce_pre_echo_fx( synth_fx16, hb_synth_fx16, hBWE_FD->prev_td_energy_fx, l_subfr, Qsynth_fx16, Q_syn_hb ); + } + ELSE + { + tmp = i_mult2( 3, l_subfr ); + L_tmp = L_deposit_l( 0 ); - Q_syn_hb = sub( Q_syn_hb, 3 ); + FOR( i = 0; i < l_subfr; i++ ) + { + L_tmp = L_mac0_sat( L_tmp, hb_synth_fx16[add( tmp, i )], hb_synth_fx16[add( tmp, i )] ); /*(2*Q_syn_hb) */ + } - hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; - move16(); - } - ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) - { - Qsynth_fx16 = Find_Max_Norm32( synth_fx, output_frame ); - Qsynth_fx16 = sub( Qsynth_fx16, shr( add( sub( 15, norm_s( l_subfr ) ), 1 ), 1 ) ); - Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); - Qsynth_fx16 = add( 11 - 16, Qsynth_fx16 ); + L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ + hBWE_FD->prev_td_energy_fx = 0; + move16(); - /* IVAS_fmToDo: synth[] is @internal_Fs!!! */ - time_reduce_pre_echo_fx( synth_fx16, hb_synth_fx16, hBWE_FD->prev_td_energy_fx, l_subfr, Qsynth_fx16, Q_syn_hb ); - } - ELSE - { - tmp = i_mult2( 3, l_subfr ); - L_tmp = L_deposit_l( 0 ); + IF( L_tmp != 0 ) + { + q_tmp = norm_l( L_tmp ); + tmp = extract_h( L_shl( L_tmp, q_tmp ) ); + q_tmp = sub( q_tmp, sub( 30, shl( Q_syn_hb, 1 ) ) ); + + tmp = div_s( 16384, tmp ); + L_tmp = L_deposit_h( tmp ); + L_tmp = Isqrt_lc( L_tmp, &q_tmp ); /*Q(31-exp) */ + hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( q_tmp, 15 ) ) ); /*Q0 */ + move16(); + } + } - FOR( i = 0; i < l_subfr; i++ ) + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) { - L_tmp = L_mac0_sat( L_tmp, hb_synth_fx16[add( tmp, i )], hb_synth_fx16[add( tmp, i )] ); /*(2*Q_syn_hb) */ + Qsynth_fx16 = add( 16 - 11, Q_syn_hb ); + Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); + + /* add HB synth from hf_synth() */ + v_add_16( hb_synth_fx16, synth_fx16, hb_synth_fx16, output_frame ); } - L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ - hBWE_FD->prev_td_energy_fx = 0; + hBWE_FD->prev_mode = mode; + move16(); + hBWE_FD->prev_frica_flag = frica_flag; move16(); - IF( L_tmp != 0 ) + FOR( i = 0; i < output_frame; i++ ) { - q_tmp = norm_l( L_tmp ); - tmp = extract_h( L_shl( L_tmp, q_tmp ) ); - q_tmp = sub( q_tmp, sub( 30, shl( Q_syn_hb, 1 ) ) ); - - tmp = div_s( 16384, tmp ); - L_tmp = L_deposit_h( tmp ); - L_tmp = Isqrt_lc( L_tmp, &q_tmp ); /*Q(31-exp) */ - hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( q_tmp, 15 ) ) ); /*Q0 */ - move16(); + hb_synth_fx[i] = L_deposit_l( hb_synth_fx16[i] ); // Q_syn_hb + move32(); } - } - - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) - { - Qsynth_fx16 = add( 16 - 11, Q_syn_hb ); - Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); - - /* add HB synth from hf_synth() */ - v_add_16( hb_synth_fx16, synth_fx16, hb_synth_fx16, output_frame ); - } - hBWE_FD->prev_mode = mode; - move16(); - hBWE_FD->prev_frica_flag = frica_flag; - move16(); - - FOR( i = 0; i < output_frame; i++ ) - { - hb_synth_fx[i] = L_deposit_l( hb_synth_fx16[i] ); // Q_syn_hb - move32(); + return Q_syn_hb; } - - return Q_syn_hb; -} diff --git a/lib_dec/updt_dec_fx.c b/lib_dec/updt_dec_fx.c index 868c62fe7..27983b42e 100644 --- a/lib_dec/updt_dec_fx.c +++ b/lib_dec/updt_dec_fx.c @@ -408,7 +408,7 @@ static void updt_bw_switching_fx( { IF( st_fx->element_mode != EVS_MONO ) { - st_fx->tilt_swb_fx = ivas_calc_tilt_bwe_fx( synth32, Qpost, L_FRAME32k ); + st_fx->tilt_swb_fx = calc_tilt_bwe_fx_32( synth32, Qpost, L_FRAME32k ); } ELSE { -- GitLab From 4e094ae84bb96fdd0cca0db5b4e7b35fed812ede Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 19:40:59 +0100 Subject: [PATCH 09/12] harmonize time_envelop_shaping_fx() --- lib_com/prot_fx.h | 13 +- lib_com/swb_bwe_com_fx.c | 42 +- lib_dec/swb_bwe_dec_fx.c | 1954 +++++++++++++++++++------------------- 3 files changed, 1020 insertions(+), 989 deletions(-) diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 0f25bbd1f..5e1a9028d 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -380,6 +380,9 @@ void SWB_BWE_decoding_fx( ); void time_envelop_shaping_fx( +#ifdef HARM_FD_BWE + const Word16 element_mode, /* i : element mode */ +#endif Word16 werr[], /* i/o: SHB synthesis Q_synth*/ Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ const Word16 L, /* i : frame length */ @@ -2675,6 +2678,7 @@ Word16 E_UTIL_f_preemph3_ivas_fx( Word16 bits /* Q0 */ ); +#ifndef HARM_FD_BWE void time_envelop_shaping_fx( Word16 werr[], /* i/o: SHB synthesis Q_synth*/ Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ @@ -2682,11 +2686,12 @@ void time_envelop_shaping_fx( Word16 *Q_synth ); void time_envelop_shaping_ivas_fx( - Word16 werr[], /* i/o: SHB synthesis Q_synth*/ - Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ - const Word16 L, /* i : frame length */ + const Word16 element_mode, /* i : element mode */ + Word16 werr[], /* i/o: SHB synthesis Q_synth*/ + Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ + const Word16 L, /* i : frame length */ Word16 *Q_synth ); - +#endif void find_td_envelope_fx( const Word16 inp[], /* i : input signal Qx */ const Word16 len, /* i : length of the input signal */ diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index 50c3a11d3..525a1db85 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -1945,6 +1945,7 @@ return; /*--------------------------------------------------------------------------*/ void time_envelop_shaping_fx( +#ifndef HARM_FD_BWE Word16 werr[], /* i/o: SHB synthesis Q_synth*/ Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ const Word16 L, /* i : frame length */ @@ -2024,6 +2025,9 @@ void time_envelop_shaping_fx( } void time_envelop_shaping_ivas_fx( +#else + const Word16 element_mode, /* i : element mode */ +#endif Word16 werr[], /* i/o: SHB synthesis Q_synth*/ Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ const Word16 L, /* i : frame length */ @@ -2040,31 +2044,47 @@ void time_envelop_shaping_ivas_fx( pit = werr; exp_L = norm_s( L ); inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */ + FOR( i = 0; i < SWB_TENV; i++ ) { - Energy_64 = L_deposit_l( 0 ); - FOR( j = 0; j < L / 4; j++ ) +#ifdef HARM_FD_BWE + IF( element_mode == EVS_MONO ) { - Energy_64 = W_mac0_16_16( Energy_64, *pit, *pit ); /*(2*Q_synth) */ - pit++; + Energy = L_deposit_l( 0 ); + FOR( j = 0; j < L / 4; j++ ) + { + Energy = L_mac0_sat( Energy, *pit, *pit ); /*(2*Q_synth) */ + pit++; + } + Energy_Q = shl( ( *Q_synth ), 1 ); + } + ELSE +#endif + { + Energy_64 = L_deposit_l( 0 ); + FOR( j = 0; j < L / 4; j++ ) + { + Energy_64 = W_mac0_16_16( Energy_64, *pit, *pit ); /*(2*Q_synth) */ + pit++; + } + w_tmp = W_norm( Energy_64 ); + Energy_64 = W_shl( Energy_64, w_tmp ); + Energy = W_extract_h( Energy_64 ); /*2*Q_synth + w_tmp -32*/ + Energy_Q = sub( add( shl( ( *Q_synth ), 1 ), w_tmp ), 32 ); } - w_tmp = W_norm( Energy_64 ); - Energy_64 = W_shl( Energy_64, w_tmp ); - Energy = W_extract_h( Energy_64 ); /*2*Q_synth + w_tmp -32*/ - Energy_Q = sub( add( shl( ( *Q_synth ), 1 ), w_tmp ), 32 ); + Energy = Mult_32_16( Energy, inv_L ); /*Q(29-exp_L-15) -> Q(-exp_L+14+2*Q_synth+w_tmp-32) */ Energy_16 = 0; move16(); - /*exp = 31-(-exp_L+14 +(2*(*Q_synth)+w_tmp-32)); */ + /*exp = 31-(-exp_L+14+Energy_Q); */ exp = sub( 17, sub( Energy_Q, exp_L ) ); IF( Energy != 0 ) { exp = norm_l( Energy ); frac = extract_h( L_shl( Energy, exp ) ); - /*exp = sub(exp, 30-(-exp_L+14-2+(2*(*Q_synth)+w_tmp-32))); */ + /*exp = sub(exp, 30-(-exp_L+14-2+Energy_Q)); */ exp = sub( exp, sub( 30, add( sub( Energy_Q, exp_L ), 14 - 2 ) ) ); - tmp = div_s( 16384, frac ); L_tmp = L_deposit_h( tmp ); Energy = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */ diff --git a/lib_dec/swb_bwe_dec_fx.c b/lib_dec/swb_bwe_dec_fx.c index ef2a4fe25..1d9ec7444 100644 --- a/lib_dec/swb_bwe_dec_fx.c +++ b/lib_dec/swb_bwe_dec_fx.c @@ -372,1226 +372,1232 @@ Word16 ivas_wb_bwe_dec_fx( Word16 *voice_factors_fx, /* i : voicing factors Q15 */ const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ Word16 *Qpost ) - { - Word16 mode; - Word16 WB_fenv_fx[SWB_FENV]; - Word16 ysynth_fx[L_FRAME48k]; - Word16 Q_syn, exp, Q_syn_hb; - Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; +{ + Word16 mode; + Word16 WB_fenv_fx[SWB_FENV]; + Word16 ysynth_fx[L_FRAME48k]; + Word16 Q_syn, exp, Q_syn_hb; + Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; #ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING - Word16 output[L_FRAME16k]; - Word16 Q_input, sf; + Word16 output[L_FRAME16k]; + Word16 Q_input, sf; #endif - Word16 scl, new_input_fx_exp; - Word16 i; - FD_BWE_DEC_HANDLE hBWE_FD; - Word16 ysynth_frame_size; - Word16 coder_type = st_fx->coder_type; - move16(); + Word16 scl, new_input_fx_exp; + Word16 i; + FD_BWE_DEC_HANDLE hBWE_FD; + Word16 ysynth_frame_size; + Word16 coder_type = st_fx->coder_type; + move16(); - hBWE_FD = st_fx->hBWE_FD; + hBWE_FD = st_fx->hBWE_FD; - /* MDCT of the core synthesis signal */ - new_input_fx_exp = *Qpost; - move16(); - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) - { + /* MDCT of the core synthesis signal */ + new_input_fx_exp = *Qpost; + move16(); + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + { #ifdef FIX_2280_REDUCTION_UNNECESSARY_SCALING - sf = getScaleFactor32( output_fx32, st_fx->L_frame ); + sf = getScaleFactor32( output_fx32, st_fx->L_frame ); - Q_input = 0; - move16(); - if ( LT_16( sf, 5 ) ) - { - Q_input = sub( sf, 5 ); - } + Q_input = 0; + move16(); + if ( LT_16( sf, 5 ) ) + { + Q_input = sub( sf, 5 ); + } - Copy_Scale_sig_32_16( output_fx32, output, st_fx->L_frame, sub( Q_input, Q11 ) ); // Q_input + Copy_Scale_sig_32_16( output_fx32, output, st_fx->L_frame, sub( Q_input, Q11 ) ); // Q_input #endif - lerp( output, ysynth_fx, L_FRAME16k, st_fx->L_frame ); + lerp( output, ysynth_fx, L_FRAME16k, st_fx->L_frame ); - wtda_fx( ysynth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k ); + wtda_fx( ysynth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /*st->L_frame*/ L_FRAME16k ); - *Qpost = sub( new_input_fx_exp, 15 ); - move16(); - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, /*st->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = L_FRAME16k; - } - ELSE - { - wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ output_frame ); + *Qpost = sub( new_input_fx_exp, 15 ); + move16(); + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, /*st->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = L_FRAME16k; + } + ELSE + { + wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, + &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ output_frame ); - *Qpost = sub( new_input_fx_exp, 15 ); - move16(); - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = output_frame; - } + *Qpost = sub( new_input_fx_exp, 15 ); + move16(); + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = output_frame; + } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = Find_Max_Norm32( ysynth_32, ysynth_frame_size ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); - } - Copy_Scale_sig32_16( ysynth_32, ysynth_fx, ysynth_frame_size, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) + { + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = Find_Max_Norm32( ysynth_32, ysynth_frame_size ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); + } + Copy_Scale_sig32_16( ysynth_32, ysynth_fx, ysynth_frame_size, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - IF( !st_fx->bfi ) + IF( !st_fx->bfi ) + { + IF( st_fx->extl_brate > 0 ) { - IF( st_fx->extl_brate > 0 ) - { - /* de-quantization */ - mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); + /* de-quantization */ + mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); #ifdef HARM_FD_BWE - IF( st_fx->element_mode == EVS_MONO ) - { - hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) - } - ELSE -#endif - { - hBWE_FD->last_wb_bwe_ener_fx = extract_l( Mpy_32_16_r( L_add( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 /*0.5f in Q15*/ ) ); - } - move16(); + IF( st_fx->element_mode == EVS_MONO ) + { + hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) } ELSE +#endif { - Word32 tmp_brate; - - tmp_brate = st_fx->last_core_brate; - move32(); - test(); - if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) - { - tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ - move32(); - } - if ( NE_16( st_fx->last_extl, WB_BWE ) ) - { - hBWE_FD->prev_SWB_fenv_fx[0] = 0; - move16(); - } - - mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], - voice_factors_fx, pitch_buf_fx, tmp_brate, st_fx->hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); + hBWE_FD->last_wb_bwe_ener_fx = extract_l( Mpy_32_16_r( L_add( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 /*0.5f in Q15*/ ) ); } + move16(); } ELSE { - /* FEC */ - mode = NORMAL; - move16(); - FOR( i = 0; i < 2; i++ ) + Word32 tmp_brate; + + tmp_brate = st_fx->last_core_brate; + move32(); + test(); + if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) + { + tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ + move32(); + } + if ( NE_16( st_fx->last_extl, WB_BWE ) ) { - WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); + hBWE_FD->prev_SWB_fenv_fx[0] = 0; move16(); } - } - test(); - IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) - { - Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); + mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], + voice_factors_fx, pitch_buf_fx, tmp_brate, st_fx->hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); } - - exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); - IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) - { - hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); - move32(); - } - ELSE + } + ELSE + { + /* FEC */ + mode = NORMAL; + move16(); + FOR( i = 0; i < 2; i++ ) { - Q_syn = add( hBWE_FD->prev_Q_synth, exp ); - hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); - move32(); + WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); + move16(); } + } - WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, - st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, - st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, - st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); + test(); + IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) + { + Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); + } - IF( EQ_32( st_fx->output_Fs, 32000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); - } - ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); - } + exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); + IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) + { + hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); + move32(); + } + ELSE + { + Q_syn = add( hBWE_FD->prev_Q_synth, exp ); + hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); + move32(); + } - Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, + st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, + st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, + st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); - window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, - ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + IF( EQ_32( st_fx->output_Fs, 32000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); + } + ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); + } - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) - { - /* add HB synth from hf_synth() */ - v_add_16( hb_synth_fx, synth_fx, hb_synth_fx, output_frame ); - } + Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); - hBWE_FD->prev_mode = mode; - move16(); - hBWE_FD->prev_Q_synth = Q_syn; - move16(); + window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, + ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - return Q_syn_hb; + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + { + /* add HB synth from hf_synth() */ + v_add_16( hb_synth_fx, synth_fx, hb_synth_fx, output_frame ); } + hBWE_FD->prev_mode = mode; + move16(); + hBWE_FD->prev_Q_synth = Q_syn; + move16(); + + return Q_syn_hb; +} + #ifndef HARM_FD_BWE - Word16 wb_bwe_dec_fx( - Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis Q0/Qpost */ - Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ - const Word16 output_frame, /* i : frame length */ - Word16 *voice_factors_fx, /* i : voicing factors Q15 */ - const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ - Decoder_State *st_fx, /* i/o: decoder state structure */ - Word16 *Qpost ) - { - Word16 mode; - Word16 WB_fenv_fx[SWB_FENV]; - Word16 ysynth_fx[L_FRAME48k]; - Word16 Q_syn, exp, Q_syn_hb; - Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; - Word16 scl, new_input_fx_exp; - Word16 i; - FD_BWE_DEC_HANDLE hBWE_FD; - - Word16 coder_type = st_fx->coder_type; - move16(); +Word16 wb_bwe_dec_fx( + Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis Q0/Qpost */ + Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ + const Word16 output_frame, /* i : frame length */ + Word16 *voice_factors_fx, /* i : voicing factors Q15 */ + const Word16 pitch_buf_fx[], /* i : pitch buffer Q6 */ + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word16 *Qpost ) +{ + Word16 mode; + Word16 WB_fenv_fx[SWB_FENV]; + Word16 ysynth_fx[L_FRAME48k]; + Word16 Q_syn, exp, Q_syn_hb; + Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k], t_audio32_tmp[L_FRAME48k]; + Word16 scl, new_input_fx_exp; + Word16 i; + FD_BWE_DEC_HANDLE hBWE_FD; + + Word16 coder_type = st_fx->coder_type; + move16(); - hBWE_FD = st_fx->hBWE_FD; + hBWE_FD = st_fx->hBWE_FD; - /* MDCT of the core synthesis signal */ + /* MDCT of the core synthesis signal */ - new_input_fx_exp = *Qpost; - move16(); + new_input_fx_exp = *Qpost; + move16(); + { + wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, + &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ + output_frame ); + *Qpost = sub( new_input_fx_exp, 15 ); + move16(); + /* DCT of the ACELP core synthesis */ + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); + } + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) + { + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); + } + Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); + IF( !st_fx->bfi ) + { + IF( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) { - wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - output_frame ); - *Qpost = sub( new_input_fx_exp, 15 ); + /* de-quantization */ + mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); + hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) move16(); - /* DCT of the ACELP core synthesis */ - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) - { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); - } - Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - IF( !st_fx->bfi ) + ELSE { - IF( EQ_32( st_fx->total_brate, ACELP_13k20 ) ) - { - /* de-quantization */ - mode = WB_BWE_gain_deq_fx( st_fx, WB_fenv_fx ); - hBWE_FD->last_wb_bwe_ener_fx = mult_r( add_sat( WB_fenv_fx[0], WB_fenv_fx[1] ), 16384 ); // this would not saturate if written like : rounf_fx(L_mac(L_mult(WB_fenv_fx[0], 16384), WB_fenv_fx[1], 16384)) - move16(); - } - ELSE - { - Word32 tmp_brate; + Word32 tmp_brate; - tmp_brate = st_fx->last_core_brate; - move32(); - - test(); - if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) - { - tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ - move32(); - } - - if ( NE_16( st_fx->last_extl, WB_BWE ) ) - { - hBWE_FD->prev_SWB_fenv_fx[0] = 0; - move16(); - } + tmp_brate = st_fx->last_core_brate; + move32(); - mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], - voice_factors_fx, pitch_buf_fx, tmp_brate, hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); + test(); + if ( EQ_32( st_fx->last_total_brate, ACELP_9k60 ) && EQ_16( st_fx->last_extl, SWB_TBE ) ) + { + tmp_brate = ACELP_8k00; /* this is needed in order to stay BE wrt. EVS mono */ + move32(); } - } - ELSE - { - /* FEC */ - mode = NORMAL; - move16(); - FOR( i = 0; i < 2; i++ ) + + if ( NE_16( st_fx->last_extl, WB_BWE ) ) { - WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); + hBWE_FD->prev_SWB_fenv_fx[0] = 0; move16(); } - } - test(); - IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) - { - Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); - } - exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); - IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) - { - hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); - move32(); + mode = WB_BWE_gain_pred_fx( WB_fenv_fx, ysynth_fx, coder_type, st_fx->prev_coder_type, hBWE_FD->prev_SWB_fenv_fx[0], + voice_factors_fx, pitch_buf_fx, tmp_brate, hBWE_FD->last_wb_bwe_ener_fx, Q_syn, st_fx->last_extl, st_fx->tilt_wb_fx ); } - ELSE + } + ELSE + { + /* FEC */ + mode = NORMAL; + move16(); + FOR( i = 0; i < 2; i++ ) { - Q_syn = add( hBWE_FD->prev_Q_synth, exp ); - hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); - move32(); + WB_fenv_fx[i] = mult_r( hBWE_FD->prev_SWB_fenv_fx[i], 24576 ); + move16(); } + } + test(); + IF( NE_16( st_fx->last_extl, WB_BWE ) || st_fx->bfi ) + { + Copy( WB_fenv_fx, hBWE_FD->prev_SWB_fenv_fx, 2 ); + } - WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, - st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, - st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, - st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); + exp = norm_l( hBWE_FD->prev_Energy_wb_fx ); + IF( GT_16( add( hBWE_FD->prev_Q_synth, exp ), Q_syn ) ) + { + hBWE_FD->prev_Energy_wb_fx = L_shr( hBWE_FD->prev_Energy_wb_fx, sub( hBWE_FD->prev_Q_synth, Q_syn ) ); + move32(); + } + ELSE + { + Q_syn = add( hBWE_FD->prev_Q_synth, exp ); + hBWE_FD->prev_Energy_wb_fx = L_shl( hBWE_FD->prev_Energy_wb_fx, exp ); + move32(); + } - IF( EQ_32( st_fx->output_Fs, 32000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); - } - ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) - { - set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); - } + WB_BWE_decoding_fx( ysynth_fx, WB_fenv_fx, ysynth_32, L_FRAME16k, mode, + st_fx->last_extl, &hBWE_FD->prev_Energy_wb_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, + st_fx->extl, coder_type, st_fx->total_brate, &hBWE_FD->Seed, &hBWE_FD->prev_flag, + st_fx->prev_coder_type, Q_syn, &Q_syn_hb ); - Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + IF( EQ_32( st_fx->output_Fs, 32000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME16k ); + } + ELSE IF( EQ_32( st_fx->output_Fs, 48000 ) ) + { + set32_fx( &ysynth_32[L_FRAME16k], 0, L_FRAME32k ); + } - window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, - ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); - hBWE_FD->prev_mode = mode; - move16(); - hBWE_FD->prev_Q_synth = Q_syn; - move16(); + window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, + ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - return Q_syn_hb; - } + hBWE_FD->prev_mode = mode; + move16(); + hBWE_FD->prev_Q_synth = Q_syn; + move16(); + + return Q_syn_hb; +} #endif - /*-------------------------------------------------------------------* - * swb_bwe_gain_deq() - * - * Decoding of SWB parameters - *-------------------------------------------------------------------*/ - - /* o : BWE class */ - Word16 swb_bwe_gain_deq_fx( - Decoder_State *st_fx, /* i/o: decoder state structure */ - const Word16 core, /* i : core : Q0 */ - Word16 *SWB_tenv, /* o : time-domain BWE envelope : Q0 */ - Word16 *SWB_fenv, /* o : frequency-domain BWE envelope : Q1 */ - const Word16 hr_flag, /* i : high rate flag : Q0 */ - const Word16 hqswb_clas /* i : HQ BWE class : Q0 */ - ) - { - Word16 index, mode, n_band; - Word16 indice[6]; - Word16 quant_tmp[SWB_FENV / 2], quant_tmp2[SWB_FENV / 2]; - Word16 nb_bits[6]; - Word16 nenv; - Word16 tmp, exp, frac; - Word32 L_tmp; +/*-------------------------------------------------------------------* + * swb_bwe_gain_deq() + * + * Decoding of SWB parameters + *-------------------------------------------------------------------*/ - IF( hqswb_clas > 0 ) +/* o : BWE class */ +Word16 swb_bwe_gain_deq_fx( + Decoder_State *st_fx, /* i/o: decoder state structure */ + const Word16 core, /* i : core : Q0 */ + Word16 *SWB_tenv, /* o : time-domain BWE envelope : Q0 */ + Word16 *SWB_fenv, /* o : frequency-domain BWE envelope : Q1 */ + const Word16 hr_flag, /* i : high rate flag : Q0 */ + const Word16 hqswb_clas /* i : HQ BWE class : Q0 */ +) +{ + Word16 index, mode, n_band; + Word16 indice[6]; + Word16 quant_tmp[SWB_FENV / 2], quant_tmp2[SWB_FENV / 2]; + Word16 nb_bits[6]; + Word16 nenv; + Word16 tmp, exp, frac; + Word32 L_tmp; + + IF( hqswb_clas > 0 ) + { + mode = (Word16) get_next_indice_fx( st_fx, 1 ); + IF( mode == 0 ) { mode = (Word16) get_next_indice_fx( st_fx, 1 ); - IF( mode == 0 ) - { - mode = (Word16) get_next_indice_fx( st_fx, 1 ); - } - ELSE - { - mode = HQ_GENERIC_SP_EXC; - move16(); - } } ELSE { - mode = (Word16) get_next_indice_fx( st_fx, 2 ); + mode = HQ_GENERIC_SP_EXC; + move16(); } + } + ELSE + { + mode = (Word16) get_next_indice_fx( st_fx, 2 ); + } - test(); - IF( EQ_16( mode, 1 ) && core == ACELP_CORE ) + test(); + IF( EQ_16( mode, 1 ) && core == ACELP_CORE ) + { + FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) { - FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) - { - index = (Word16) get_next_indice_fx( st_fx, 4 ); - SWB_tenv[n_band] = shl_sat( 1, index ); - move16(); - } - - indice[0] = (Word16) get_next_indice_fx( st_fx, 7 ); + index = (Word16) get_next_indice_fx( st_fx, 4 ); + SWB_tenv[n_band] = shl_sat( 1, index ); move16(); - indice[1] = (Word16) get_next_indice_fx( st_fx, 6 ); + } + + indice[0] = (Word16) get_next_indice_fx( st_fx, 7 ); + move16(); + indice[1] = (Word16) get_next_indice_fx( st_fx, 6 ); + move16(); + + tmp = shl( indice[0], 1 ); + FOR( n_band = 0; n_band < DIM_TR1; n_band++ ) + { + /*Env_TR_Cdbk1_fx[ indice[0]*DIM_TR1+n_band]*/ + quant_tmp[2 * n_band] = Env_TR_Cdbk1_fx[tmp + n_band]; /*Q8 */ move16(); + } - tmp = shl( indice[0], 1 ); - FOR( n_band = 0; n_band < DIM_TR1; n_band++ ) + /*tmp = indice[1]*DIM_TR2*/ + tmp = shl( indice[1], 1 ); + quant_tmp[1] = add_sat( shr( add_sat( quant_tmp[0], quant_tmp[2] ), 1 ), Env_TR_Cdbk2_fx[tmp] ); + /* To prevent an unnecessary saturation, it would be better to write the code as below, but might have an impact on EVS bit exactness */ + /* quant_tmp[1] = add( extract_h( L_mac( L_mult( quant_tmp[0], 16384 ), quant_tmp[2], 16384 ) ), Env_TR_Cdbk2_fx[tmp] ); */ + move16(); /*Q8 */ + quant_tmp[3] = add( quant_tmp[2], Env_TR_Cdbk2_fx[tmp + 1] ); + move16(); /*Q8 */ + + FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) + { + tmp = add( quant_tmp[n_band], Mean_env_tr_fx[n_band] ); /*Q8 */ + + /*-----------------------------------------------------------------* + * SWB_fenv[n_band] = pow(10.0, tmp/40) + * = pow(2, 3.321928*tmp/40) + * = pow(2, 0.0830482*tmp) + *-----------------------------------------------------------------*/ + L_tmp = L_mult( tmp, 21771 ); /* 0.0830482 in Q18 -> Q27 */ + L_tmp = L_shr( L_tmp, 11 ); /* From Q27 to Q16 */ + frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ + tmp = extract_l( Pow2( 13, frac ) ); + exp = sub( exp, 13 ); + SWB_fenv[n_band] = shl( tmp, add( exp, 1 ) ); + move16(); /*Q1 */ + } + /* in case of band-width switching, attenuate frame gain */ + IF( st_fx->bws_cnt1 > 0 ) + { + tmp = i_mult( st_fx->bws_cnt1, 1638 ); + FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) { - /*Env_TR_Cdbk1_fx[ indice[0]*DIM_TR1+n_band]*/ - quant_tmp[2 * n_band] = Env_TR_Cdbk1_fx[tmp + n_band]; /*Q8 */ + SWB_tenv[n_band] = mult_r( SWB_tenv[n_band], tmp ); move16(); } - /*tmp = indice[1]*DIM_TR2*/ - tmp = shl( indice[1], 1 ); - quant_tmp[1] = add_sat( shr( add_sat( quant_tmp[0], quant_tmp[2] ), 1 ), Env_TR_Cdbk2_fx[tmp] ); - /* To prevent an unnecessary saturation, it would be better to write the code as below, but might have an impact on EVS bit exactness */ - /* quant_tmp[1] = add( extract_h( L_mac( L_mult( quant_tmp[0], 16384 ), quant_tmp[2], 16384 ) ), Env_TR_Cdbk2_fx[tmp] ); */ - move16(); /*Q8 */ - quant_tmp[3] = add( quant_tmp[2], Env_TR_Cdbk2_fx[tmp + 1] ); - move16(); /*Q8 */ - FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) { - tmp = add( quant_tmp[n_band], Mean_env_tr_fx[n_band] ); /*Q8 */ - - /*-----------------------------------------------------------------* - * SWB_fenv[n_band] = pow(10.0, tmp/40) - * = pow(2, 3.321928*tmp/40) - * = pow(2, 0.0830482*tmp) - *-----------------------------------------------------------------*/ - L_tmp = L_mult( tmp, 21771 ); /* 0.0830482 in Q18 -> Q27 */ - L_tmp = L_shr( L_tmp, 11 ); /* From Q27 to Q16 */ - frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ - tmp = extract_l( Pow2( 13, frac ) ); - exp = sub( exp, 13 ); - SWB_fenv[n_band] = shl( tmp, add( exp, 1 ) ); - move16(); /*Q1 */ + SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], tmp ); + move16(); } - /* in case of band-width switching, attenuate frame gain */ - IF( st_fx->bws_cnt1 > 0 ) - { - tmp = i_mult( st_fx->bws_cnt1, 1638 ); - FOR( n_band = 0; n_band < SWB_TENV; n_band++ ) - { - SWB_tenv[n_band] = mult_r( SWB_tenv[n_band], tmp ); - move16(); - } + } + } + ELSE + { + nb_bits[0] = 5; + move16(); + nb_bits[1] = 7; + move16(); + nb_bits[2] = 6; + move16(); + nb_bits[3] = 5; + move16(); - FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ ) - { - SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], tmp ); - move16(); - } - } + IF( EQ_16( hr_flag, 1 ) ) + { + nb_bits[4] = 5; + move16(); + nenv = SWB_FENV - 2; + move16(); } ELSE { - nb_bits[0] = 5; + nb_bits[4] = 6; move16(); - nb_bits[1] = 7; + nenv = SWB_FENV; move16(); - nb_bits[2] = 6; + } + + FOR( n_band = 0; n_band < 5; n_band++ ) + { + indice[n_band] = (Word16) get_next_indice_fx( st_fx, nb_bits[n_band] ); move16(); - nb_bits[3] = 5; + } + + IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) + { + indice[n_band] = (Word16) get_next_indice_fx( st_fx, 5 ); move16(); + } - IF( EQ_16( hr_flag, 1 ) ) - { - nb_bits[4] = 5; - move16(); - nenv = SWB_FENV - 2; - move16(); - } - ELSE - { - nb_bits[4] = 6; - move16(); - nenv = SWB_FENV; - move16(); - } + Copy( &EnvCdbk11_fx[indice[0] * DIM11], quant_tmp, DIM11 ); + Copy( &EnvCdbk1st_fx[indice[1] * DIM1ST], quant_tmp2, DIM1ST ); + Copy( &EnvCdbk2nd_fx[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND ); - FOR( n_band = 0; n_band < 5; n_band++ ) - { - indice[n_band] = (Word16) get_next_indice_fx( st_fx, nb_bits[n_band] ); - move16(); - } + FOR( n_band = 0; n_band < ( DIM11 - 1 ); n_band++ ) + { + quant_tmp[n_band] = add( quant_tmp[n_band], quant_tmp2[n_band] ); + move16(); /*Q8 */ + SWB_fenv[n_band * 2] = quant_tmp[n_band]; + move16(); /*Q8 */ + } - IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) - { - indice[n_band] = (Word16) get_next_indice_fx( st_fx, 5 ); - move16(); - } + IF( EQ_16( hr_flag, 1 ) ) + { + quant_tmp[6] = add( quant_tmp[6], quant_tmp2[6] ); + move16(); /*Q8 */ + SWB_fenv[11] = quant_tmp[6]; + move16(); - Copy( &EnvCdbk11_fx[indice[0] * DIM11], quant_tmp, DIM11 ); - Copy( &EnvCdbk1st_fx[indice[1] * DIM1ST], quant_tmp2, DIM1ST ); - Copy( &EnvCdbk2nd_fx[indice[2] * DIM2ND], quant_tmp2 + DIM1ST, DIM2ND ); + Copy( &EnvCdbk3rd_fx[indice[3] * DIM3RD], quant_tmp2, DIM3RD ); + Copy( &EnvCdbk3rd_fx[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD ); - FOR( n_band = 0; n_band < ( DIM11 - 1 ); n_band++ ) + FOR( n_band = 0; n_band < 5; n_band++ ) { - quant_tmp[n_band] = add( quant_tmp[n_band], quant_tmp2[n_band] ); - move16(); /*Q8 */ - SWB_fenv[n_band * 2] = quant_tmp[n_band]; + SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band + 1] ); move16(); /*Q8 */ } - IF( EQ_16( hr_flag, 1 ) ) - { - quant_tmp[6] = add( quant_tmp[6], quant_tmp2[6] ); - move16(); /*Q8 */ - SWB_fenv[11] = quant_tmp[6]; - move16(); + SWB_fenv[0] = add( SWB_fenv[0], quant_tmp2[0] ); + move16(); /*Q8 */ + } + ELSE + { + quant_tmp[DIM11 - 1] = add( quant_tmp[DIM11 - 1], quant_tmp2[DIM11 - 1] ); + move16(); /*Q8 */ + SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1]; + move16(); - Copy( &EnvCdbk3rd_fx[indice[3] * DIM3RD], quant_tmp2, DIM3RD ); - Copy( &EnvCdbk3rd_fx[indice[4] * DIM3RD], quant_tmp2 + DIM3RD, DIM3RD ); + Copy( &EnvCdbk3rd_fx[i_mult2( indice[3], DIM3RD )], quant_tmp2, DIM3RD ); + Copy( &EnvCdbk4th_fx[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH ); - FOR( n_band = 0; n_band < 5; n_band++ ) - { - SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band + 1] ); - move16(); /*Q8 */ - } - - SWB_fenv[0] = add( SWB_fenv[0], quant_tmp2[0] ); - move16(); /*Q8 */ - } - ELSE + FOR( n_band = 0; n_band < DIM12 - 1; n_band++ ) { - quant_tmp[DIM11 - 1] = add( quant_tmp[DIM11 - 1], quant_tmp2[DIM11 - 1] ); + SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band] ); move16(); /*Q8 */ - SWB_fenv[( DIM11 - 1 ) * 2] = quant_tmp[DIM11 - 1]; - move16(); + } + + SWB_fenv[add( n_band * 2, 1 )] = add( quant_tmp[n_band], quant_tmp2[n_band] ); + move16(); /*Q8 */ + } - Copy( &EnvCdbk3rd_fx[i_mult2( indice[3], DIM3RD )], quant_tmp2, DIM3RD ); - Copy( &EnvCdbk4th_fx[indice[4] * DIM4TH], quant_tmp2 + DIM3RD, DIM4TH ); + FOR( n_band = 0; n_band < nenv; n_band++ ) + { + tmp = add( SWB_fenv[n_band], Mean_env_fx[n_band] ); /*Q8 */ - FOR( n_band = 0; n_band < DIM12 - 1; n_band++ ) - { - SWB_fenv[n_band * 2 + 1] = add_sat( shr( add_sat( quant_tmp[n_band], quant_tmp[n_band + 1] ), 1 ), quant_tmp2[n_band] ); - move16(); /*Q8 */ - } + L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */ + L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */ + frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ - SWB_fenv[add( n_band * 2, 1 )] = add( quant_tmp[n_band], quant_tmp2[n_band] ); - move16(); /*Q8 */ - } + tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */ + /* output of Pow2() will be: */ + /* 16384 < Pow2() <= 32767 */ + exp = sub( exp, 13 ); + SWB_fenv[n_band] = shl_sat( tmp, add( exp, 1 ) ); + move16(); /*Q1 */ + } - FOR( n_band = 0; n_band < nenv; n_band++ ) - { - tmp = add( SWB_fenv[n_band], Mean_env_fx[n_band] ); /*Q8 */ + IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) + { + Copy( &EnvCdbkFB_fx[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB ); /*Q7 */ - L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q26 */ - L_tmp = L_shr( L_tmp, 10 ); /* From Q26 to Q16 */ + FOR( n_band = 0; n_band < DIM_FB; n_band++ ) + { + tmp = add( SWB_fenv[n_band + nenv], Mean_env_fb_fx[n_band] ); + L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */ + L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */ frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */ /* output of Pow2() will be: */ /* 16384 < Pow2() <= 32767 */ exp = sub( exp, 13 ); - SWB_fenv[n_band] = shl_sat( tmp, add( exp, 1 ) ); - move16(); /*Q1 */ + SWB_fenv[n_band + nenv] = shl( tmp, add( exp, 1 ) ); + move16(); } + } - IF( EQ_16( hqswb_clas, HQ_GEN_FB ) ) - { - Copy( &EnvCdbkFB_fx[indice[5] * DIM_FB], &SWB_fenv[nenv], DIM_FB ); /*Q7 */ + /* in case of band-width switching, attenuate frame gain */ + } - FOR( n_band = 0; n_band < DIM_FB; n_band++ ) - { - tmp = add( SWB_fenv[n_band + nenv], Mean_env_fb_fx[n_band] ); - L_tmp = L_mult( tmp, 21771 ); /* 0.166096 in Q17 -> Q25 */ - L_tmp = L_shr( L_tmp, 9 ); /* From Q25 to Q16 */ - frac = L_Extract_lc( L_tmp, &exp ); /* Extract exponent of L_tmp */ - - tmp = extract_l( Pow2( 13, frac ) ); /* Put 13 as exponent so that */ - /* output of Pow2() will be: */ - /* 16384 < Pow2() <= 32767 */ - exp = sub( exp, 13 ); - SWB_fenv[n_band + nenv] = shl( tmp, add( exp, 1 ) ); - move16(); - } - } + return mode; +} - /* in case of band-width switching, attenuate frame gain */ - } +/*-------------------------------------------------------------------* + * swb_bwe_dec() + * + * SWB BWE decoder + *-------------------------------------------------------------------*/ - return mode; +/*o : Q_syn_hb */ +Word16 swb_bwe_dec_fx( + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis (might be rescaled inside wtda() ) Q0/Qpost */ + Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ + const Word16 output_frame, /* i : frame length */ + Word16 *Qpost ) +{ + Word16 i, l_subfr; + Word16 mode; + Word16 frica_flag = 0; + move16(); + Word16 idxGain; + Word16 Q_syn, Q_syn_hb; + Word16 ysynth_fx[L_FRAME48k]; + Word16 tmp; + Word16 SWB_tenv_fx[SWB_TENV]; + Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k]; + Word16 scl, new_input_fx_exp; + Word32 t_audio32_tmp[L_FRAME48k]; + Word32 SWB_tenv_tmp_fx[SWB_TENV]; + Word32 L_tmp; + Word16 exp, frac; + Word16 fb_ener_adjust_fx = 0; + move16(); + Word16 SWB_fenv_fx[SWB_FENV]; + Word16 L; + Word16 j = 0; + move16(); + Word16 ener_adjust_quan_fx; + Word16 tmp2; + Word16 fb_band_begin; + FD_BWE_DEC_HANDLE hBWE_FD; + hBWE_FD = st_fx->hBWE_FD; + + /*---------------------------------------------------------------------* + * SWB BWE decoding + *---------------------------------------------------------------------*/ + + /* windowing of the ACELP core synthesis */ + new_input_fx_exp = *Qpost; + move16(); + + wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, + &hBWE_FD->old_wtda_swb_fx_exp, ALDO_WINDOW, ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ output_frame ); + + *Qpost = sub( new_input_fx_exp, 15 ); + move16(); + /* DCT of the ACELP core synthesis */ + direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); + + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) + { + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); } + Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - /*-------------------------------------------------------------------* - * swb_bwe_dec() - * - * SWB BWE decoder - *-------------------------------------------------------------------*/ - - /*o : Q_syn_hb */ - Word16 swb_bwe_dec_fx( - Decoder_State *st_fx, /* i/o: decoder state structure */ - Word16 *synth_fx, /* i/o: ACELP core synthesis/final synthesis (might be rescaled inside wtda() ) Q0/Qpost */ - Word16 *hb_synth_fx, /* o : SHB synthesis/final synthesis Q_syn_hb */ - const Word16 output_frame, /* i : frame length */ - Word16 *Qpost ) + IF( !st_fx->bfi ) { - Word16 i, l_subfr; - Word16 mode; - Word16 frica_flag = 0; - move16(); - Word16 idxGain; - Word16 Q_syn, Q_syn_hb; - Word16 ysynth_fx[L_FRAME48k]; - Word16 tmp; - Word16 SWB_tenv_fx[SWB_TENV]; - Word32 L_wtda_synth_fx[2 * L_FRAME48k], ysynth_32[L_FRAME48k]; - Word16 scl, new_input_fx_exp; - Word32 t_audio32_tmp[L_FRAME48k]; - Word32 SWB_tenv_tmp_fx[SWB_TENV]; - Word32 L_tmp; - Word16 exp, frac; - Word16 fb_ener_adjust_fx = 0; - move16(); - Word16 SWB_fenv_fx[SWB_FENV]; - Word16 L; - Word16 j = 0; + IF( st_fx->bws_cnt > 0 ) + { + /* estimate parameters */ + mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); + } + ELSE + { + /* de-quantization */ + mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); + } + + L = SWB_FENV; move16(); - Word16 ener_adjust_quan_fx; - Word16 tmp2; - Word16 fb_band_begin; - FD_BWE_DEC_HANDLE hBWE_FD; - hBWE_FD = st_fx->hBWE_FD; - - /*---------------------------------------------------------------------* - * SWB BWE decoding - *---------------------------------------------------------------------*/ - /* windowing of the ACELP core synthesis */ - new_input_fx_exp = *Qpost; + if ( EQ_16( mode, TRANSIENT ) ) + { + L = SWB_FENV_TRANS; + move16(); + } + L_tmp = 0; move16(); + FOR( i = 0; i < L; i++ ) + { + L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); + } + + exp = norm_s( L ); + tmp = div_s( shl( 1, sub( 14, exp ) ), L ); /*Q(29-exp) */ + L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(1+29-exp+1-16)->Q(15-exp) */ + st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( exp, 2 ) ) ); /*Q1 */ + move16(); + } + ELSE + { + /* SHB FEC */ + IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) { - wtda_fx( synth_fx, &new_input_fx_exp, L_wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx, - &hBWE_FD->old_wtda_swb_fx_exp, - ALDO_WINDOW, - ALDO_WINDOW, /* window overlap of current frame (0: full, 2: none, or 3: half) */ - output_frame ); - *Qpost = sub( new_input_fx_exp, 15 ); + mode = hBWE_FD->prev_mode; move16(); - /* DCT of the ACELP core synthesis */ - direct_transform_fx( L_wtda_synth_fx, ysynth_32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + MAX_Q_NEW_INPUT, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) + ELSE { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = Find_Max_Norm32( ysynth_32, output_frame ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); + mode = NORMAL; + move16(); } - Copy_Scale_sig32_16( ysynth_32, ysynth_fx, output_frame, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); - IF( !st_fx->bfi ) + Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); + } + + /* reconstruction of MDCT spectrum of the error signal */ + set32_fx( ysynth_32, 0, output_frame ); + IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) + { + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, + &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + } + ELSE + { + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, + &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + } + + test(); + IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) + { + FOR( i = 0; i < L_SUBFR; i++ ) { - IF( st_fx->bws_cnt > 0 ) - { - /* estimate parameters */ - mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); - } - ELSE - { - /* de-quantization */ - mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); - } + tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ + hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); + move16(); /*Q_synth */ + } - L = SWB_FENV; - move16(); - if ( EQ_16( mode, TRANSIENT ) ) - { - L = SWB_FENV_TRANS; - move16(); - } - L_tmp = 0; + FOR( ; i < output_frame; i++ ) + { + hBWE_FD->mem_imdct_fx[i] = 0; move16(); - FOR( i = 0; i < L; i++ ) - { - L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); - } - - exp = norm_s( L ); - tmp = div_s( shl( 1, sub( 14, exp ) ), L ); /*Q(29-exp) */ + } + } - L_tmp = Mult_32_16( L_tmp, tmp ); /*Q(1+29-exp+1-16)->Q(15-exp) */ - st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( exp, 2 ) ) ); /*Q1 */ + /* decode information */ + IF( EQ_16( st_fx->extl, FB_BWE ) ) + { + IF( !st_fx->bfi ) + { + idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); + fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ + } + ELSE IF( st_fx->bfi ) + { + fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; move16(); } + + hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; + move16(); + IF( EQ_16( mode, TRANSIENT ) ) + { + ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); /*Q13*/ + } ELSE { - /* SHB FEC */ - IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) + IF( SWB_fenv_fx[7] != 0 ) { - mode = hBWE_FD->prev_mode; - move16(); + tmp = div_s( 1, SWB_fenv_fx[7] ); + move16(); /*Q14*/ + ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); /*Q13*/ } ELSE { - mode = NORMAL; - move16(); + ener_adjust_quan_fx = 0; + move16(); /*Q13*/ } - - Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); } + fb_band_begin = FB_BAND_BEGIN; + move16(); - /* reconstruction of MDCT spectrum of the error signal */ - set32_fx( ysynth_32, 0, output_frame ); - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) + FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, - &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + tmp = sub( 32767, i_mult( j, 1024 ) ); + tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ + + tmp2 = i_mult( j, 256 ); /*Q13*/ + tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ + + tmp = add( tmp, tmp2 ); /*Q13*/ + ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; + move16(); + ysynth_32[i] = L_shl( Mult_32_16( ysynth_32[i], tmp ), 2 ); + move32(); /*15+Q_syn */ + j = add( j, 1 ); } - ELSE + + FOR( ; i < FB_BAND_END; i++ ) { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, ysynth_32, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, - &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; + move16(); + ysynth_32[i] = Mult_32_16( ysynth_32[i], fb_ener_adjust_fx ); + move32(); /*15+Q_syn */ } + } - test(); - IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) + Q_syn_hb = add( Q_syn, Q_32_BITS ); + Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); + window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + l_subfr = mult( output_frame, 8192 ); + + test(); + IF( EQ_16( mode, TRANSIENT ) ) + { + FOR( i = 0; i < SWB_TENV; i++ ) { - FOR( i = 0; i < L_SUBFR; i++ ) - { - tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ - hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); - move16(); /*Q_synth */ - } + SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i], 26214 ); + move32(); /*Q15 */ + } - FOR( ; i < output_frame; i++ ) - { - hBWE_FD->mem_imdct_fx[i] = 0; - move16(); - } + /* time envelope shaping when the current frame is TRANSIENT frame */ +#ifdef HARM_FD_BWE + time_envelop_shaping_fx( st_fx->element_mode, hb_synth_fx, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); +#else + time_envelop_shaping_fx( hb_synth_fx, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); +#endif + Q_syn_hb = sub( Q_syn_hb, 3 ); + + hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; + move16(); + } + ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) + { + time_reduce_pre_echo_fx( synth_fx, hb_synth_fx, hBWE_FD->prev_td_energy_fx, l_subfr, *Qpost, Q_syn_hb ); + } + ELSE + { + tmp = i_mult2( 3, l_subfr ); + L_tmp = L_deposit_l( 0 ); + FOR( i = 0; i < l_subfr; i++ ) + { + L_tmp = L_mac0_sat( L_tmp, hb_synth_fx[add( tmp, i )], hb_synth_fx[add( tmp, i )] ); /*(2*Q_syn_hb) */ } + L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ - /* decode information */ - IF( EQ_16( st_fx->extl, FB_BWE ) ) + hBWE_FD->prev_td_energy_fx = 0; + move16(); + IF( L_tmp != 0 ) { - IF( !st_fx->bfi ) - { - idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); - fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ - } - ELSE IF( st_fx->bfi ) - { - fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; - move16(); - } + exp = norm_l( L_tmp ); + frac = extract_h( L_shl( L_tmp, exp ) ); + exp = sub( exp, sub( 30, shl( Q_syn_hb, 1 ) ) ); - hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; - move16(); - IF( EQ_16( mode, TRANSIENT ) ) - { - ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); /*Q13*/ - } - ELSE - { - IF( SWB_fenv_fx[7] != 0 ) - { - tmp = div_s( 1, SWB_fenv_fx[7] ); - move16(); /*Q14*/ - ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); /*Q13*/ - } - ELSE - { - ener_adjust_quan_fx = 0; - move16(); /*Q13*/ - } - } - fb_band_begin = FB_BAND_BEGIN; + tmp = div_s( 16384, frac ); + L_tmp = L_deposit_h( tmp ); + L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */ + hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( exp, 15 ) ) ); /*Q0 */ move16(); + } + } + + hBWE_FD->prev_frica_flag = frica_flag; + move16(); + hBWE_FD->prev_mode = mode; + move16(); + + return Q_syn_hb; +} + - FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) - { - tmp = sub( 32767, i_mult( j, 1024 ) ); - tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ +/*-------------------------------------------------------------------* + * fd_bwe_dec_init() + * + * Initialize FD BWE state structure at the decoder + *-------------------------------------------------------------------*/ - tmp2 = i_mult( j, 256 ); /*Q13*/ - tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ +void fd_bwe_dec_init_fx( + FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle */ +) +{ + set16_fx( hBWE_FD->old_syn_12k8_16k_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) ); + set16_fx( hBWE_FD->L_old_wtda_swb_fx, 0, L_FRAME48k ); + hBWE_FD->old_wtda_swb_fx_exp = 0; + move16(); - tmp = add( tmp, tmp2 ); /*Q13*/ - ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; - move16(); - ysynth_32[i] = L_shl( Mult_32_16( ysynth_32[i], tmp ), 2 ); - move32(); /*15+Q_syn */ - j = add( j, 1 ); - } + hBWE_FD->prev_mode = NORMAL; + move16(); + set16_fx( hBWE_FD->prev_SWB_fenv_fx, 0, SWB_FENV ); + hBWE_FD->prev_Energy_fx = 0; + move16(); + hBWE_FD->prev_L_swb_norm = 8; + move16(); + hBWE_FD->Seed = 21211; + move16(); + hBWE_FD->prev_frica_flag = 0; + move16(); + set16_fx( hBWE_FD->mem_imdct_fx, 0, L_FRAME48k ); + hBWE_FD->mem_imdct_exp_fx = 0; + move16(); + hBWE_FD->prev_td_energy_fx = 0; + move16(); + hBWE_FD->prev_weight_fx = 6554; /*0.2 in Q15*/ + move16(); + hBWE_FD->prev_flag = 0; + move16(); + hBWE_FD->last_wb_bwe_ener_fx = 0; + move16(); + hBWE_FD->prev_Energy_wb_fx = L_deposit_l( 0 ); + move32(); + hBWE_FD->memExp1 = 0; + move16(); - FOR( ; i < FB_BAND_END; i++ ) - { - ysynth_32[i] = ysynth_32[i - FB_BAND_WIDTH]; - move16(); - ysynth_32[i] = Mult_32_16( ysynth_32[i], fb_ener_adjust_fx ); - move32(); /*15+Q_syn */ - } - } + /* Previous frame LPC initialization for PPP */ + hBWE_FD->prev_Q_synth = 0; + move16(); - Q_syn_hb = add( Q_syn, Q_32_BITS ); - Inverse_Transform( ysynth_32, &Q_syn_hb, t_audio32_tmp, 0, output_frame, output_frame, st_fx->element_mode ); - window_ola_fx( t_audio32_tmp, hb_synth_fx, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - l_subfr = mult( output_frame, 8192 ); + hBWE_FD->mem_deemph_old_syn_fx = 0; + move16(); + hBWE_FD->prev_fb_ener_adjust_fx = 0; + move16(); - test(); - IF( EQ_16( mode, TRANSIENT ) ) - { - FOR( i = 0; i < SWB_TENV; i++ ) - { - SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i], 26214 ); - move32(); /*Q15 */ - } + return; +} - /* time envelope shaping when the current frame is TRANSIENT frame */ - time_envelop_shaping_fx( hb_synth_fx, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); - Q_syn_hb = sub( Q_syn_hb, 3 ); +/*-------------------------------------------------------------------* + * swb_bwe_dec_fx32() + * + * SWB BWE decoder, 32-bit variant + *-------------------------------------------------------------------*/ - hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; - move16(); - } - ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) - { - time_reduce_pre_echo_fx( synth_fx, hb_synth_fx, hBWE_FD->prev_td_energy_fx, l_subfr, *Qpost, Q_syn_hb ); - } - ELSE - { - tmp = i_mult2( 3, l_subfr ); - L_tmp = L_deposit_l( 0 ); - FOR( i = 0; i < l_subfr; i++ ) - { - L_tmp = L_mac0_sat( L_tmp, hb_synth_fx[add( tmp, i )], hb_synth_fx[add( tmp, i )] ); /*(2*Q_syn_hb) */ - } - L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ - hBWE_FD->prev_td_energy_fx = 0; - move16(); - IF( L_tmp != 0 ) - { - exp = norm_l( L_tmp ); - frac = extract_h( L_shl( L_tmp, exp ) ); - exp = sub( exp, sub( 30, shl( Q_syn_hb, 1 ) ) ); - - tmp = div_s( 16384, frac ); - L_tmp = L_deposit_h( tmp ); - L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */ - hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( exp, 15 ) ) ); /*Q0 */ - move16(); - } - } +Word16 swb_bwe_dec_fx32( + Decoder_State *st_fx, /* i/o: decoder state structure */ + Word32 output_fx[], /* i : synthesis @internal Fs : Q11 */ + Word32 *synth_fx, /* i : ACELP core synthesis/final synthesis : Q11 */ + Word32 *hb_synth_fx, /* o : SHB synthesis/final synthesis : Q_syn_hb */ + Word16 use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */ + Word16 output_frame /* i : frame length */ +) +{ + Word16 L; + Word16 mode; + Word16 idxGain; + Word16 i, j, l_subfr; + Word16 fb_band_begin; + Word16 frica_flag = 0; + move16(); + Word16 ener_adjust_quan_fx; + Word16 fb_ener_adjust_fx = 0; + move16(); + Word16 scl, new_input_fx_exp, ysynth_frame_size; + Word16 Q_syn, Q_syn_hb, tmp, tmp2, q_tmp, Qsynth_fx16; - hBWE_FD->prev_frica_flag = frica_flag; - move16(); - hBWE_FD->prev_mode = mode; - move16(); + Word16 ysynth_fx[L_FRAME48k]; + Word16 SWB_fenv_fx[SWB_FENV]; + Word16 SWB_tenv_fx[SWB_TENV]; + Word16 synth_fx16[L_FRAME48k]; + Word16 hb_synth_fx16[L_FRAME48k]; - return Q_syn_hb; - } + Word32 L_tmp; + Word32 yerror_fx[L_FRAME48k]; + Word32 ysynth_fx32[L_FRAME48k]; + Word32 SWB_tenv_tmp_fx[SWB_TENV]; + Word32 wtda_synth_fx[2 * L_FRAME48k]; + FD_BWE_DEC_HANDLE hBWE_FD; + hBWE_FD = st_fx->hBWE_FD; - /*-------------------------------------------------------------------* - * fd_bwe_dec_init() - * - * Initialize FD BWE state structure at the decoder - *-------------------------------------------------------------------*/ + /*---------------------------------------------------------------------* + * SWB BWE decoding + *---------------------------------------------------------------------*/ - void fd_bwe_dec_init_fx( - FD_BWE_DEC_HANDLE hBWE_FD /* i/o: FD BWE data handle */ - ) + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) { - set16_fx( hBWE_FD->old_syn_12k8_16k_fx, 0, NS2SA( 16000, DELAY_FD_BWE_ENC_NS ) ); - set16_fx( hBWE_FD->L_old_wtda_swb_fx, 0, L_FRAME48k ); - hBWE_FD->old_wtda_swb_fx_exp = 0; - move16(); + L_lerp_fx_q11( output_fx, ysynth_fx32, L_FRAME16k, st_fx->L_frame ); - hBWE_FD->prev_mode = NORMAL; - move16(); - set16_fx( hBWE_FD->prev_SWB_fenv_fx, 0, SWB_FENV ); - hBWE_FD->prev_Energy_fx = 0; - move16(); - hBWE_FD->prev_L_swb_norm = 8; - move16(); - hBWE_FD->Seed = 21211; - move16(); - hBWE_FD->prev_frica_flag = 0; - move16(); - set16_fx( hBWE_FD->mem_imdct_fx, 0, L_FRAME48k ); - hBWE_FD->mem_imdct_exp_fx = 0; - move16(); - hBWE_FD->prev_td_energy_fx = 0; - move16(); - hBWE_FD->prev_weight_fx = 6554; /*0.2 in Q15*/ - move16(); - hBWE_FD->prev_flag = 0; - move16(); - hBWE_FD->last_wb_bwe_ener_fx = 0; - move16(); - hBWE_FD->prev_Energy_wb_fx = L_deposit_l( 0 ); - move32(); - hBWE_FD->memExp1 = 0; - move16(); + /* windowing of the ACELP core synthesis */ + wtda_fx32( ysynth_fx32, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, /*st_fx->L_frame*/ L_FRAME16k ); - /* Previous frame LPC initialization for PPP */ - hBWE_FD->prev_Q_synth = 0; + /* DCT of the ACELP core synthesis */ + new_input_fx_exp = 11; move16(); + direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, /*st_fx->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = L_FRAME16k; + move16(); + } + ELSE + { + /* windowing of the ACELP core synthesis */ + wtda_fx32( synth_fx, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, output_frame ); - hBWE_FD->mem_deemph_old_syn_fx = 0; + /* DCT of the ACELP core synthesis */ + new_input_fx_exp = 11; move16(); - hBWE_FD->prev_fb_ener_adjust_fx = 0; + /* DCT of the ACELP core synthesis */ + direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); + ysynth_frame_size = output_frame; move16(); - - return; } - /*-------------------------------------------------------------------* - * swb_bwe_dec_fx32() - * - * SWB BWE decoder, 32-bit variant - *-------------------------------------------------------------------*/ - - - Word16 swb_bwe_dec_fx32( - Decoder_State *st_fx, /* i/o: decoder state structure */ - Word32 output_fx[], /* i : synthesis @internal Fs : Q11 */ - Word32 *synth_fx, /* i : ACELP core synthesis/final synthesis : Q11 */ - Word32 *hb_synth_fx, /* o : SHB synthesis/final synthesis : Q_syn_hb */ - Word16 use_cldfb_for_dft, /* i : flag to use of CLDFB for DFT Stereo */ - Word16 output_frame /* i : frame length */ - ) + /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ + scl = sub( 16 + 11, new_input_fx_exp ); + /* Possible to Upscale? */ + IF( scl > 0 ) { - Word16 L; - Word16 mode; - Word16 idxGain; - Word16 i, j, l_subfr; - Word16 fb_band_begin; - Word16 frica_flag = 0; - move16(); - Word16 ener_adjust_quan_fx; - Word16 fb_ener_adjust_fx = 0; - move16(); - Word16 scl, new_input_fx_exp, ysynth_frame_size; - Word16 Q_syn, Q_syn_hb, tmp, tmp2, q_tmp, Qsynth_fx16; - - Word16 ysynth_fx[L_FRAME48k]; - Word16 SWB_fenv_fx[SWB_FENV]; - Word16 SWB_tenv_fx[SWB_TENV]; - Word16 synth_fx16[L_FRAME48k]; - Word16 hb_synth_fx16[L_FRAME48k]; - - Word32 L_tmp; - Word32 yerror_fx[L_FRAME48k]; - Word32 ysynth_fx32[L_FRAME48k]; - Word32 SWB_tenv_tmp_fx[SWB_TENV]; - Word32 wtda_synth_fx[2 * L_FRAME48k]; - - FD_BWE_DEC_HANDLE hBWE_FD; - hBWE_FD = st_fx->hBWE_FD; - - /*---------------------------------------------------------------------* - * SWB BWE decoding - *---------------------------------------------------------------------*/ - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + /* Yes */ + /* Calc Room to Upscale */ + Q_syn = sub( Find_Max_Norm32( ysynth_fx32, ysynth_frame_size ), 3 ); + /* Stay within MAX_Q_NEW_INPUT */ + scl = s_min( Q_syn, scl ); + } + /*Don't upscale if already in 15 bits*/ + IF( GT_16( scl, 15 ) ) + { + FOR( i = 0; i < ysynth_frame_size; i++ ) { - /* todo - wtda() does not support L_FRAME length; thus temporarily resample the signal */ - /* todo - delay output[] by 1.25ms ? */ - L_lerp_fx_q11( output_fx, ysynth_fx32, L_FRAME16k, st_fx->L_frame ); - - /* windowing of the ACELP core synthesis */ - wtda_fx32( ysynth_fx32, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, /*st_fx->L_frame*/ L_FRAME16k ); - - /* DCT of the ACELP core synthesis */ - new_input_fx_exp = 11; - move16(); - direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, /*st_fx->L_frame*/ L_FRAME16k, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = L_FRAME16k; + ysynth_fx[i] = extract_l( ysynth_fx32[i] ); move16(); } + Q_syn = new_input_fx_exp; + move16(); + } + ELSE + { + Copy_Scale_sig32_16( ysynth_fx32, ysynth_fx, ysynth_frame_size, scl ); + Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); + } + + IF( !st_fx->bfi ) + { + IF( st_fx->bws_cnt > 0 ) + { + /* estimate parameters */ + mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); + } ELSE { - /* windowing of the ACELP core synthesis */ - wtda_fx32( synth_fx, wtda_synth_fx, hBWE_FD->L_old_wtda_swb_fx32, ALDO_WINDOW, ALDO_WINDOW, output_frame ); + /* de-quantization */ + mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); + } - /* DCT of the ACELP core synthesis */ - new_input_fx_exp = 11; - move16(); - direct_transform_fx( wtda_synth_fx, ysynth_fx32, 0, output_frame, &new_input_fx_exp, st_fx->element_mode ); - ysynth_frame_size = output_frame; + L = SWB_FENV; + move16(); + if ( EQ_16( mode, TRANSIENT ) ) + { + L = SWB_FENV_TRANS; move16(); } - /* Convert to 16 Bits (Calc Shift Required to Stay within MAX_Q_NEW_INPUT) */ - scl = sub( 16 + 11, new_input_fx_exp ); - /* Possible to Upscale? */ - IF( scl > 0 ) + L_tmp = 0; + move32(); + FOR( i = 0; i < L; i++ ) { - /* Yes */ - /* Calc Room to Upscale */ - Q_syn = sub( Find_Max_Norm32( ysynth_fx32, ysynth_frame_size ), 3 ); - /* Stay within MAX_Q_NEW_INPUT */ - scl = s_min( Q_syn, scl ); + L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); } - /*Don't upscale if already in 15 bits*/ - IF( GT_16( scl, 15 ) ) + q_tmp = norm_s( L ); + tmp = div_s( shl( 1, sub( 14, q_tmp ) ), L ); /*Q(29-q_tmp) */ + L_tmp = Mpy_32_16_1( L_tmp, tmp ); /*Q(1+29-q_tmp+1-16)->Q(15-q_tmp) */ + st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( q_tmp, 2 ) ) ); /*Q1 */ + move16(); + } + ELSE + { + /* SHB FEC */ + + IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) { - FOR( i = 0; i < ysynth_frame_size; i++ ) - { - ysynth_fx[i] = extract_l( ysynth_fx32[i] ); - move16(); - } - Q_syn = new_input_fx_exp; + mode = hBWE_FD->prev_mode; move16(); } ELSE { - Copy_Scale_sig32_16( ysynth_fx32, ysynth_fx, ysynth_frame_size, scl ); - Q_syn = add( sub( new_input_fx_exp, 16 ), scl ); + mode = NORMAL; + move16(); } - IF( !st_fx->bfi ) - { - IF( st_fx->bws_cnt > 0 ) - { - /* estimate parameters */ - mode = para_pred_bws_fx( st_fx, ysynth_fx, SWB_fenv_fx, Q_syn ); - } - ELSE - { - /* de-quantization */ - mode = swb_bwe_gain_deq_fx( st_fx, ACELP_CORE, SWB_tenv_fx, SWB_fenv_fx, 0, -1 ); - } + Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); + } - L = SWB_FENV; - move16(); - if ( EQ_16( mode, TRANSIENT ) ) - { - L = SWB_FENV_TRANS; - move16(); - } + /* reconstruction of MDCT spectrum of the error signal */ + set32_fx( yerror_fx, 0, output_frame ); - L_tmp = 0; - move32(); - FOR( i = 0; i < L; i++ ) - { - L_tmp = L_add( L_tmp, L_deposit_l( SWB_fenv_fx[i] ) ); - } - q_tmp = norm_s( L ); - tmp = div_s( shl( 1, sub( 14, q_tmp ) ), L ); /*Q(29-q_tmp) */ - L_tmp = Mpy_32_16_1( L_tmp, tmp ); /*Q(1+29-q_tmp+1-16)->Q(15-q_tmp) */ - st_fx->prev_ener_shb_fx = round_fx( L_shl( L_tmp, add( q_tmp, 2 ) ) ); /*Q1 */ + IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) + { + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + } + ELSE + { + SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + } + + test(); + IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) + { + FOR( i = 0; i < L_SUBFR; i++ ) + { + tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ + hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); move16(); } - ELSE - { - /* SHB FEC */ - - IF( NE_16( hBWE_FD->prev_mode, TRANSIENT ) ) - { - mode = hBWE_FD->prev_mode; - move16(); - } - ELSE - { - mode = NORMAL; - move16(); - } - Copy( hBWE_FD->prev_SWB_fenv_fx, SWB_fenv_fx, SWB_FENV ); + FOR( ; i < output_frame; i++ ) + { + hBWE_FD->mem_imdct_fx[i] = 0; + move16(); } + } - /* reconstruction of MDCT spectrum of the error signal */ - set32_fx( yerror_fx, 0, output_frame ); - - IF( EQ_16( st_fx->L_frame, L_FRAME16k ) ) + /* decode information */ + IF( EQ_16( st_fx->extl, FB_BWE ) ) + { + IF( !st_fx->bfi ) { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 80, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); + fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ } - ELSE + ELSE IF( st_fx->bfi ) { - SWB_BWE_decoding_fx( ysynth_fx, SWB_fenv_fx, yerror_fx, L_FRAME32k - 80, mode, &frica_flag, &hBWE_FD->prev_Energy_fx, hBWE_FD->prev_SWB_fenv_fx, &hBWE_FD->prev_L_swb_norm, st_fx->tilt_wb_fx, &hBWE_FD->Seed, 6, &hBWE_FD->prev_weight_fx, st_fx->extl, Q_syn, st_fx->last_extl ); + fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; + move16(); } - test(); - IF( EQ_16( hBWE_FD->prev_frica_flag, 1 ) && frica_flag == 0 ) + hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; + move16(); + IF( EQ_16( mode, TRANSIENT ) ) { - FOR( i = 0; i < L_SUBFR; i++ ) - { - tmp = sub( 32767, extract_l( L_mult0( i, 512 ) ) ); /*Q15 */ - hBWE_FD->mem_imdct_fx[i] = mult_r( hBWE_FD->mem_imdct_fx[i], tmp ); - move16(); - } - - FOR( ; i < output_frame; i++ ) - { - hBWE_FD->mem_imdct_fx[i] = 0; - move16(); - } + ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); } - - /* decode information */ - IF( EQ_16( st_fx->extl, FB_BWE ) ) + ELSE { - IF( !st_fx->bfi ) - { - idxGain = (Word16) get_next_indice_fx( st_fx, NUM_BITS_FB_FRAMEGAIN ); - fb_ener_adjust_fx = usdequant_fx( idxGain, FB_GAIN_QLOW_FX, FB_GAIN_QDELTA_FX ); /*Q15 */ - } - ELSE IF( st_fx->bfi ) - { - fb_ener_adjust_fx = hBWE_FD->prev_fb_ener_adjust_fx; - move16(); - } - - hBWE_FD->prev_fb_ener_adjust_fx = fb_ener_adjust_fx; - move16(); - IF( EQ_16( mode, TRANSIENT ) ) + IF( SWB_fenv_fx[7] != 0 ) { - ener_adjust_quan_fx = shr( fb_ener_adjust_fx, 2 ); + tmp = div_s( 1, SWB_fenv_fx[7] ); + ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); } ELSE { - IF( SWB_fenv_fx[7] != 0 ) - { - tmp = div_s( 1, SWB_fenv_fx[7] ); - ener_adjust_quan_fx = s_min( shr( i_mult_sat( SWB_fenv_fx[13], tmp ), 2 ), 32767 ); - } - ELSE - { - ener_adjust_quan_fx = 0; - move16(); - } - } - - fb_band_begin = FB_BAND_BEGIN; - move16(); - if ( EQ_16( st_fx->L_frame, L_FRAME ) ) - { - fb_band_begin = FB_BAND_BEGIN_12k8; + ener_adjust_quan_fx = 0; move16(); } + } - j = 0; + fb_band_begin = FB_BAND_BEGIN; + move16(); + if ( EQ_16( st_fx->L_frame, L_FRAME ) ) + { + fb_band_begin = FB_BAND_BEGIN_12k8; move16(); - FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) - { - tmp = sub( 32767, i_mult( j, 1024 ) ); - tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ - - tmp2 = i_mult( j, 256 ); /*Q13*/ - tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ - - tmp = add( tmp, tmp2 ); /*Q13*/ - yerror_fx[i] = L_shl( Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], tmp ), 2 ); /*15+Q_syn */ - move32(); - j = add( j, 1 ); - } - FOR( ; i < FB_BAND_END; i++ ) - { - yerror_fx[i] = Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], fb_ener_adjust_fx ); /*15+Q_syn */ - move32(); - } } - /* iDCT of the error signal */ - Q_syn_hb = add( Q_syn, 15 ); - Inverse_Transform( yerror_fx, &Q_syn_hb, wtda_synth_fx, 0, output_frame, -1, st_fx->element_mode ); + j = 0; + move16(); + FOR( i = fb_band_begin; i < add( fb_band_begin, DE_OFFSET1 ); i++ ) + { + tmp = sub( 32767, i_mult( j, 1024 ) ); + tmp = mult_r( tmp, ener_adjust_quan_fx ); /*Q13*/ - /* inverse windowing of the error signal */ - window_ola_fx( wtda_synth_fx, hb_synth_fx16, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); - l_subfr = mult( output_frame, 8192 ); + tmp2 = i_mult( j, 256 ); /*Q13*/ + tmp2 = mult_r( tmp2, fb_ener_adjust_fx ); /*Q13*/ - test(); - IF( EQ_16( mode, TRANSIENT ) ) + tmp = add( tmp, tmp2 ); /*Q13*/ + yerror_fx[i] = L_shl( Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], tmp ), 2 ); /*15+Q_syn */ + move32(); + j = add( j, 1 ); + } + FOR( ; i < FB_BAND_END; i++ ) { - FOR( i = 0; i < SWB_TENV; i++ ) - { - SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i] /*Q0*/, 26214 /*0.8f Q15*/ ); // Q15 - move32(); - } + yerror_fx[i] = Mpy_32_16_1( yerror_fx[i - FB_BAND_WIDTH], fb_ener_adjust_fx ); /*15+Q_syn */ + move32(); + } + } - /* time envelope shaping when the current frame is TRANSIENT frame */ - time_envelop_shaping_ivas_fx( hb_synth_fx16, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); + /* iDCT of the error signal */ + Q_syn_hb = add( Q_syn, 15 ); + Inverse_Transform( yerror_fx, &Q_syn_hb, wtda_synth_fx, 0, output_frame, -1, st_fx->element_mode ); - Q_syn_hb = sub( Q_syn_hb, 3 ); + /* inverse windowing of the error signal */ + window_ola_fx( wtda_synth_fx, hb_synth_fx16, &Q_syn_hb, hBWE_FD->mem_imdct_fx, &hBWE_FD->mem_imdct_exp_fx, output_frame, ALDO_WINDOW, ALDO_WINDOW, 0, 0, 0 ); + l_subfr = mult( output_frame, 8192 ); - hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; - move16(); - } - ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) + test(); + IF( EQ_16( mode, TRANSIENT ) ) + { + FOR( i = 0; i < SWB_TENV; i++ ) { - Qsynth_fx16 = Find_Max_Norm32( synth_fx, output_frame ); - Qsynth_fx16 = sub( Qsynth_fx16, shr( add( sub( 15, norm_s( l_subfr ) ), 1 ), 1 ) ); - Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); - Qsynth_fx16 = add( 11 - 16, Qsynth_fx16 ); - - /* IVAS_fmToDo: synth[] is @internal_Fs!!! */ - time_reduce_pre_echo_fx( synth_fx16, hb_synth_fx16, hBWE_FD->prev_td_energy_fx, l_subfr, Qsynth_fx16, Q_syn_hb ); + SWB_tenv_tmp_fx[i] = L_mult0( SWB_tenv_fx[i] /*Q0*/, 26214 /*0.8f Q15*/ ); // Q15 + move32(); } - ELSE - { - tmp = i_mult2( 3, l_subfr ); - L_tmp = L_deposit_l( 0 ); - FOR( i = 0; i < l_subfr; i++ ) - { - L_tmp = L_mac0_sat( L_tmp, hb_synth_fx16[add( tmp, i )], hb_synth_fx16[add( tmp, i )] ); /*(2*Q_syn_hb) */ - } + /* time envelope shaping when the current frame is TRANSIENT frame */ +#ifdef HARM_FD_BWE + time_envelop_shaping_fx( st_fx->element_mode, hb_synth_fx16, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); +#else + time_envelop_shaping_ivas_fx( hb_synth_fx16, SWB_tenv_tmp_fx, output_frame, &Q_syn_hb ); +#endif - L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ - hBWE_FD->prev_td_energy_fx = 0; - move16(); + Q_syn_hb = sub( Q_syn_hb, 3 ); - IF( L_tmp != 0 ) - { - q_tmp = norm_l( L_tmp ); - tmp = extract_h( L_shl( L_tmp, q_tmp ) ); - q_tmp = sub( q_tmp, sub( 30, shl( Q_syn_hb, 1 ) ) ); - - tmp = div_s( 16384, tmp ); - L_tmp = L_deposit_h( tmp ); - L_tmp = Isqrt_lc( L_tmp, &q_tmp ); /*Q(31-exp) */ - hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( q_tmp, 15 ) ) ); /*Q0 */ - move16(); - } - } + hBWE_FD->prev_td_energy_fx = SWB_tenv_fx[3]; + move16(); + } + ELSE IF( EQ_16( frica_flag, 1 ) && hBWE_FD->prev_frica_flag == 0 ) + { + Qsynth_fx16 = Find_Max_Norm32( synth_fx, output_frame ); + Qsynth_fx16 = sub( Qsynth_fx16, shr( add( sub( 15, norm_s( l_subfr ) ), 1 ), 1 ) ); + Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); + Qsynth_fx16 = add( 11 - 16, Qsynth_fx16 ); - test(); - IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) - { - Qsynth_fx16 = add( 16 - 11, Q_syn_hb ); - Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); + /* IVAS_fmToDo: synth[] is @internal_Fs!!! */ + time_reduce_pre_echo_fx( synth_fx16, hb_synth_fx16, hBWE_FD->prev_td_energy_fx, l_subfr, Qsynth_fx16, Q_syn_hb ); + } + ELSE + { + tmp = i_mult2( 3, l_subfr ); + L_tmp = L_deposit_l( 0 ); - /* add HB synth from hf_synth() */ - v_add_16( hb_synth_fx16, synth_fx16, hb_synth_fx16, output_frame ); + FOR( i = 0; i < l_subfr; i++ ) + { + L_tmp = L_mac0_sat( L_tmp, hb_synth_fx16[add( tmp, i )], hb_synth_fx16[add( tmp, i )] ); /*(2*Q_syn_hb) */ } - hBWE_FD->prev_mode = mode; - move16(); - hBWE_FD->prev_frica_flag = frica_flag; + L_tmp = Mult_32_16( L_tmp, div_s( 1, l_subfr ) ); /*(2*Q_syn_hb) */ + hBWE_FD->prev_td_energy_fx = 0; move16(); - FOR( i = 0; i < output_frame; i++ ) + IF( L_tmp != 0 ) { - hb_synth_fx[i] = L_deposit_l( hb_synth_fx16[i] ); // Q_syn_hb - move32(); + q_tmp = norm_l( L_tmp ); + tmp = extract_h( L_shl( L_tmp, q_tmp ) ); + q_tmp = sub( q_tmp, sub( 30, shl( Q_syn_hb, 1 ) ) ); + + tmp = div_s( 16384, tmp ); + L_tmp = L_deposit_h( tmp ); + L_tmp = Isqrt_lc( L_tmp, &q_tmp ); /*Q(31-exp) */ + hBWE_FD->prev_td_energy_fx = round_fx( L_shl( L_tmp, sub( q_tmp, 15 ) ) ); /*Q0 */ + move16(); } + } + + test(); + IF( EQ_16( st_fx->element_mode, IVAS_CPE_DFT ) && !use_cldfb_for_dft ) + { + Qsynth_fx16 = add( 16 - 11, Q_syn_hb ); + Copy_Scale_sig32_16( synth_fx, synth_fx16, output_frame, Qsynth_fx16 ); + + /* add HB synth from hf_synth() */ + v_add_16( hb_synth_fx16, synth_fx16, hb_synth_fx16, output_frame ); + } + + hBWE_FD->prev_mode = mode; + move16(); + hBWE_FD->prev_frica_flag = frica_flag; + move16(); - return Q_syn_hb; + FOR( i = 0; i < output_frame; i++ ) + { + hb_synth_fx[i] = L_deposit_l( hb_synth_fx16[i] ); // Q_syn_hb + move32(); } + + return Q_syn_hb; +} -- GitLab From 28007f8a99113c8a18bd90c05c6cd8c497634f00 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 20:04:53 +0100 Subject: [PATCH 10/12] fix --- lib_enc/swb_bwe_enc_fx.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index e0f37fd23..3315310f2 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -2639,7 +2639,7 @@ static Word16 WB_BWE_encoding_ivas_fx( Word16 q_WB_fenv[2]; Word16 yos_fx_16[L_FRAME16k]; #ifdef HARM_FD_BWE - Word16 Q_class, Q_energy, Q_fenv; + Word16 Q_class, Q_energy, Q_fenv[2]; IF( st_fx->element_mode == EVS_MONO ) { @@ -2680,7 +2680,8 @@ static Word16 WB_BWE_encoding_ivas_fx( Q_energy = Q_synth; move16(); move16(); - Q_fenv = sub( shl( Q_synth, 1 ), 2 ); + Q_fenv[0] = Q_fenv[1] = sub( shl( Q_synth, 1 ), 2 ); + move16(); } ELSE #endif @@ -2704,6 +2705,10 @@ static Word16 WB_BWE_encoding_ivas_fx( q_WB_fenv[i] = add( shl( Q_synth, 1 ), q_shift ); move16(); n_band = add( n_band, 2 ); + +#ifdef HARM_FD_BWE + Q_fenv[i] = add( q_WB_fenv[i], 4 ); +#endif } scale = s_min( L_norm_arr( yos_fx, L_FRAME16k ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); @@ -2711,20 +2716,19 @@ static Word16 WB_BWE_encoding_ivas_fx( #ifdef HARM_FD_BWE Q_class = sub( add( Q_synth, scale ), Q16 ); Q_energy = sub( add( Q_synth_lf, scale ), Q16 ); - Q_fenv = add( q_WB_fenv[i], 4 ); #endif } #ifdef HARM_FD_BWE mode = FD_BWE_class_fx( yos_fx_16, 0, 0, Q_class, 0, st_fx ); #else - mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); #endif #ifdef HARM_FD_BWE energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, Q_energy ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif FOR( i = 0; i < 2; i++ ) @@ -2737,9 +2741,9 @@ static Word16 WB_BWE_encoding_ivas_fx( tmp = Log2_norm_lc( L_shl( L_tmp, exp ) ); /* exp = 30-exp-(2*Q_synth-2 / exp = 30-exp-(q_WB_fenv[i]+4) */ #ifdef HARM_FD_BWE - exp = sub( sub( 30, exp ), Q_fenv ); + exp = sub( sub( 30, exp ), Q_fenv[i] ); #else - exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); + exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); #endif L_tmp = Mpy_32_16( exp, tmp, 32767 ); /* Q16 */ WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /* Q10 */ @@ -3750,16 +3754,16 @@ static Word16 SWB_BWE_encoding_ivas_fx( #ifdef HARM_FD_BWE mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, Q_class, Q_shb, st_fx ); #else - scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); - Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); - mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); + scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); + Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); + mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); #endif push_indice( hBstr, IND_SWB_CLASS, mode, 2 ); #ifdef HARM_FD_BWE energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, Q_energy ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) -- GitLab From d59b040ed1e4d9e8038737c93a11e8b37547f1c0 Mon Sep 17 00:00:00 2001 From: vaclav Date: Thu, 5 Feb 2026 20:05:54 +0100 Subject: [PATCH 11/12] clang-format --- lib_enc/swb_bwe_enc_fx.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index 3315310f2..ff139f385 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -2439,6 +2439,7 @@ static void energy_control_fx( } n_band = add( n_band, band_step ); } + return; } @@ -2722,13 +2723,13 @@ static Word16 WB_BWE_encoding_ivas_fx( #ifdef HARM_FD_BWE mode = FD_BWE_class_fx( yos_fx_16, 0, 0, Q_class, 0, st_fx ); #else - mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); + mode = FD_BWE_class_fx( yos_fx_16, 0, 0, sub( add( Q_synth, scale ), Q16 ), 0, st_fx ); #endif #ifdef HARM_FD_BWE energy_control_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, Q_energy ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, st_fx->coder_type, yos_fx_16, 0, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif FOR( i = 0; i < 2; i++ ) @@ -2743,7 +2744,7 @@ static Word16 WB_BWE_encoding_ivas_fx( #ifdef HARM_FD_BWE exp = sub( sub( 30, exp ), Q_fenv[i] ); #else - exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); + exp = sub( sub( 30, exp ), ( add( q_WB_fenv[i], 4 ) ) ); #endif L_tmp = Mpy_32_16( exp, tmp, 32767 ); /* Q16 */ WB_fenv_fx[i] = round_fx( L_shl( L_tmp, 10 ) ); /* Q10 */ @@ -3754,16 +3755,16 @@ static Word16 SWB_BWE_encoding_ivas_fx( #ifdef HARM_FD_BWE mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, Q_class, Q_shb, st_fx ); #else - scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); - Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); - mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); + scale = s_min( L_norm_arr( yos_fx, inner_frame ), sub( Q27, Q_synth ) /* To accomodate 10 in Q_synth*/ ); + Copy_Scale_sig32_16( yos_fx, yos_fx_16, inner_frame, scale ); + mode = FD_BWE_class_fx( yos_fx_16, global_gain_fx, tilt_nb_fx, sub( add( Q_synth, scale ), Q16 ), Q_shb, st_fx ); #endif push_indice( hBstr, IND_SWB_CLASS, mode, 2 ); #ifdef HARM_FD_BWE energy_control_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, Q_energy ); #else - energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); + energy_control_ivas_fx( st_fx, ACELP_CORE, mode, -1, yos_fx_16, st_offset, energy_factor_fx, sub( add( Q_synth_lf, scale ), Q16 ) ); #endif FOR( n_band = 0; n_band < SWB_FENV; n_band++ ) -- GitLab From 173a8ac50bf17f797dfb50595110de5623a1b33c Mon Sep 17 00:00:00 2001 From: vaclav Date: Fri, 6 Feb 2026 08:46:22 +0100 Subject: [PATCH 12/12] fix --- lib_com/prot_fx.h | 8 +++--- lib_enc/swb_bwe_enc_fx.c | 53 ++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index 6ba67449f..fc9821bf0 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -2686,10 +2686,12 @@ void time_envelop_shaping_fx( Word16 *Q_synth ); void time_envelop_shaping_ivas_fx( +#ifdef HARM_FD_BWE const Word16 element_mode, /* i : element mode */ - Word16 werr[], /* i/o: SHB synthesis Q_synth*/ - Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ - const Word16 L, /* i : frame length */ +#endif + Word16 werr[], /* i/o: SHB synthesis Q_synth*/ + Word32 SWB_tenv[], /* i/o: frequency envelope Q15*/ + const Word16 L, /* i : frame length */ Word16 *Q_synth ); #endif void find_td_envelope_fx( diff --git a/lib_enc/swb_bwe_enc_fx.c b/lib_enc/swb_bwe_enc_fx.c index ff139f385..05802bf45 100644 --- a/lib_enc/swb_bwe_enc_fx.c +++ b/lib_enc/swb_bwe_enc_fx.c @@ -368,7 +368,11 @@ void swb_bwe_enc_ivas_fx( TD_BWE_ENC_HANDLE hBWE_TD = st_fx->hBWE_TD; #ifdef HARM_FD_BWE - IF( st_fx->element_mode > EVS_MONO ) + IF( st_fx->element_mode == EVS_MONO ) + { + Copy( shb_speech_fx_Q0, shb_speech_fx, L_FRAME16k ); + } + ELSE #endif { Q_shb_speech = getScaleFactor16( shb_speech_fx_Q0, L_FRAME16k ); @@ -2047,6 +2051,9 @@ static void msvq_interpol_2_fx( *-------------------------------------------------------------------*/ static void calculate_Tonality_fx( +#ifdef HARM_FD_BWE + const Word16 element_mode, /* i : element mode */ +#endif const Word16 *org, /* i : MDCT coefficients of original Q_new*/ const Word16 *gen, /* i : MDCT coefficients of generated signal Q15*/ Word16 *SFM_org, /* o : Spectral Flatness results Q12*/ @@ -2088,19 +2095,40 @@ static void calculate_Tonality_fx( } } +#ifdef HARM_FD_BWE max = 0; move16(); - FOR( n_coeff = 0; n_coeff < length; n_coeff++ ) + IF( element_mode == EVS_MONO ) { - gen_spec[n_coeff] = abs_s( gen[n_coeff] ); - move16(); /*Q15 */ - /*test(); - if( sub(max,gen_spec[n_coeff]) < 0) +#endif + FOR( n_coeff = 0; n_coeff < length; n_coeff++ ) { - max = gen_spec[n_coeff];move16(); - }*/ - max = s_max( max, org_spec[n_coeff] ); + gen_spec[n_coeff] = abs_s( gen[n_coeff] ); + move16(); /*Q15 */ + /*test(); + if( sub(max,gen_spec[n_coeff]) < 0) + { + max = gen_spec[n_coeff];move16(); + }*/ + max = s_max( max, org_spec[n_coeff] ); + } +#ifdef HARM_FD_BWE } + ELSE + { + FOR( n_coeff = 0; n_coeff < length; n_coeff++ ) + { + gen_spec[n_coeff] = abs_s( gen[n_coeff] ); + move16(); /*Q15 */ + /*test(); + if( sub(max,gen_spec[n_coeff]) < 0) + { + max = gen_spec[n_coeff];move16(); + }*/ + max = s_max( max, gen_spec[n_coeff] ); + } + } +#endif l_shift = norm_s( max ); FOR( n_coeff = 0; n_coeff < length; n_coeff++ ) { @@ -2420,8 +2448,13 @@ static void energy_control_fx( FOR( n_band = 0; n_band < max_band; ) { - calculate_Tonality_fx( org_fx + swb_bwe_subband[n_band] + offset, SWB_signal_fx + swb_bwe_subband[n_band] + offset, +#ifdef HARM_FD_BWE + calculate_Tonality_fx( st_fx->element_mode, org_fx + swb_bwe_subband[n_band] + offset, SWB_signal_fx + swb_bwe_subband[n_band] + offset, &SFM_org_fx[n_band], &SFM_gen_fx[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] ); +#else + calculate_Tonality_fx( org_fx + swb_bwe_subband[n_band] + offset, SWB_signal_fx + swb_bwe_subband[n_band] + offset, + &SFM_org_fx[n_band], &SFM_gen_fx[n_band], swb_bwe_subband[n_band + band_step] - swb_bwe_subband[n_band] ); +#endif IF( LT_16( SFM_gen_fx[n_band], mult_r( 24576, SFM_org_fx[n_band] ) ) ) { -- GitLab