Commit 7247cf48 authored by vaclav's avatar vaclav
Browse files

harmonize swb_bwe_enc_fx()

parent 3b48390e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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 ) )
    {
+4 −0
Original line number Diff line number Diff line
@@ -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
+5 −0
Original line number Diff line number Diff line
@@ -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                           */
+350 −229
Original line number Diff line number Diff line
@@ -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 )
@@ -294,7 +294,13 @@ 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_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;

#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,10 +389,19 @@ 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
    {
        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 );
    }
#endif
}
ELSE
{
@@ -400,14 +422,33 @@ void swb_bwe_enc_ivas_fx(
            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
    }
#endif

    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
}

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
@@ -425,6 +466,7 @@ void swb_bwe_enc_ivas_fx(
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
 *----------------------------------------------------------------------*/
@@ -436,8 +478,10 @@ void swb_bwe_enc_ivas_fx(
 * 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 ) ) )
{
@@ -447,16 +491,20 @@ void swb_bwe_enc_ivas_fx(
        move16();
    }
}

/* 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 );
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 ) );
}
@@ -530,8 +578,44 @@ void swb_bwe_enc_ivas_fx(
{
    Q_shb = sub( Q_synth_hf, 4 );
}

#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 ) )
    {
        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
    {
        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 ) );

    /* 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 );
    }
}
#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 ) )
{
@@ -578,6 +662,7 @@ void swb_bwe_enc_ivas_fx(
    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 ) )
{
@@ -590,7 +675,7 @@ void swb_bwe_enc_ivas_fx(
                            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 ) )
{
@@ -867,7 +952,9 @@ void swb_bwe_enc_fx(
/*--------------------------------------------------------------------------*/
/*                                                                          */
/*==========================================================================*/
static Word16 WB_BWE_fenv_q_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*/
    const Word16 cb_length, /* i  : length of codebook    */
@@ -1005,12 +1092,13 @@ 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        */
/* 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            */
                               Word16 Q_syn,
                               Word16 Q_shb,
    const Word16 Q_syn,
    const Word16 Q_shb,
    Encoder_State *st_fx /* i/o: Encoder structure   */
)
{
@@ -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,6 +2601,36 @@ static Word16 WB_BWE_encoding_fx_32(
    Word16 q_WB_fenv[2];
    Word16 yos_fx_16[L_FRAME16k];

#ifdef HARM_FD_BWE
    IF( st_fx->element_mode == EVS_MONO )
    {
        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 )
        {
            /* Yes */
            /* Calc Room to Upscale */
            Q_synth = Find_Max_Norm32( yos_fx, L_FRAME16k );

            /* 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();

        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++ )
@@ -2544,16 +2662,17 @@ static Word16 WB_BWE_encoding_fx_32(
#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++ )
    {
        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 */
        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 */
@@ -3498,6 +3617,8 @@ static Word16 SWB_BWE_encoding_fx_32(

    return mode;
}


/*-------------------------------------------------------------------*
 * get_normalize_spec_fx_32()
 *