Commit 41eee916 authored by vaclav's avatar vaclav
Browse files

Merge branch 'basop-2345-remove-duplicated-code-in-core-encoder-non_linearity_fx' into 'main'

Remove duplicated code in core-coder: non-linearity and LP-CNG

Closes #2345

See merge request !2732
parents 98e6c7b1 ebf3b995
Loading
Loading
Loading
Loading
+24 −6
Original line number Diff line number Diff line
@@ -180,12 +180,14 @@ IF( GT_16( element_mode, IVAS_SCE ) )
{
    return;
}

/*---------------------------------------------------------------------*
 * Generate white noise vector
 *---------------------------------------------------------------------*/

/*for ( i=0; i<L_frame; i++ )exc2[i] = (float)own_random( seed );*/
Random_Fill( seed, L_frame, exc2, 4 );

/*------------------------------------------------------------*
 * Insert random variation for excitation energy
 *  (random variation is scaled according to *lp_ener value)
@@ -240,6 +242,7 @@ FOR( i_subfr = 0; i_subfr < L_frame; i_subfr += L_SUBFR )
        move16();
    }
}

IF( NE_16( Opt_AMR_WB, 1 ) )
{
    Copy( exc2, exc3, L_FRAME16k );
@@ -538,7 +541,6 @@ IF( NE_16( Opt_AMR_WB, 1 ) )
    }
    L_tmp2 = L_shr( L_tmp2, sub( add( Q_exc, Q_exc ), 5 ) ); /*Q6*/


    /*  enr = (*lp_ener)/enr; */
    /*  ftmp = sqrt(enr); */
    L_tmp = L_max( 1, *lp_ener ); /*Q6*/
@@ -592,12 +594,15 @@ ELSE
return;
}


/*-------------------------------------------------------*
 * cng_params_postupd_fx
 *
 * Post-update of CNG parameters
 *-------------------------------------------------------*/

void cng_params_postupd_fx(
#ifndef HARM_NON_LINEARITY
    const Word16 ho_circ_ptr,          /* i  : pointer for CNG averaging buffers                  Q0    */
    Word16 *cng_buf_cnt,               /* i/o: counter for CNG store buffers                      Q0    */
    const Word16 *const cng_exc2_buf,  /* i  : Excitation buffer                                  Q_exc */
@@ -702,6 +707,7 @@ void cng_params_postupd_fx(
}

void cng_params_postupd_ivas_fx(
#endif
    const Word16 ho_circ_ptr,          /* i  : pointer for CNG averaging buffers                  Q0    */
    Word16 *cng_buf_cnt,               /* i/o: counter for CNG store buffers                      Q0    */
    const Word16 *const cng_exc2_buf,  /* i  : Excitation buffer                                  Q_exc */
@@ -781,7 +787,6 @@ void cng_params_postupd_ivas_fx(
        }
        ELSE
        {

            CNG_mode = get_cng_mode( last_active_brate );

            /* att = 1/pow(2,ENR_ATT_fx[CNG_mode]); */
@@ -828,6 +833,7 @@ void cng_params_postupd_ivas_fx(
 *
 * update CNG parameters
 *-------------------------------------------------------*/
#ifndef HARM_NON_LINEARITY
void cng_params_upd_fx(
    const Word16 lsp_new[],        /* i  : LSP aprameters                                     Q15   */
    const Word16 exc2[],           /* i  : current enhanced excitation                        Q_exc */
@@ -997,8 +1003,13 @@ void cng_params_upd_fx(

    return;
}
#endif

#ifdef HARM_NON_LINEARITY
void cng_params_upd_fx(
#else
void cng_params_upd_ivas_fx(
#endif
    const Word16 lsp_new[],         /* i  : LSP aprameters                                     Q15   */
    const Word16 exc2[],            /* i  : current enhanced excitation                        Q_exc */
    const Word16 L_frame,           /* i  : frame length                                       Q0    */
@@ -1008,7 +1019,7 @@ void cng_params_upd_ivas_fx(
    Word16 ho_lsp_circ[],           /* o  : old LSP buffer for CNG averaging                   Q15   */
    const Word16 Q_exc,             /* i  : Q value of excitation                                    */
    const Word16 enc_dec_flag,      /* i  : Flag indicating encoder or decoder (ENC,DEC)             */
    Word32 ho_env_circ[],           /* i/o: Envelope buffer                                    Q(6+shift)    */
    Word32 ho_env_circ[],           /* i/o: Envelope buffer                            Q6/Q(6+shift) */
    Word16 *cng_buf_cnt,            /* i/o: Counter of postponed FFT-processing instances            */
    Word16 cng_exc2_buf[],          /* i/o: Excitation buffer                                  Q_exc */
    Word16 cng_Qexc_buf[],          /* i/o: Q_exc buffer                                       Q0    */
@@ -1176,10 +1187,17 @@ void cng_params_upd_ivas_fx(
        /* update the circular buffer of old residual envelope */
        /* Copy32( env, &(ho_env_circ[add(shl(*ho_circ_ptr,4),shl(*ho_circ_ptr,2))]), NUM_ENV_CNG ); */
        Copy32( env, &( ho_env_circ[( *ho_circ_ptr ) * NUM_ENV_CNG] ), NUM_ENV_CNG );
#ifdef HARM_NON_LINEARITY
        IF( NE_16( element_mode, EVS_MONO ) )
#endif
        {
            scale_sig32( &( ho_env_circ[( *ho_circ_ptr ) * NUM_ENV_CNG] ), NUM_ENV_CNG, shift ); // Q(6+shift)
        }
    }

    *ho_circ_size = add( *ho_circ_size, 1 );
    move16();

    if ( GT_16( *ho_circ_size, HO_HIST_SIZE ) )
    {
        *ho_circ_size = HO_HIST_SIZE;
+1 −0
Original line number Diff line number Diff line
@@ -102,6 +102,7 @@
#define HARM_CORECODER_UPDT                             /* VA: basop issue 2342: Remove duplicated code in core-coder common update functions */
#define HARM_CORE_SW                                    /* VA: basop issue 2314: Remove duplicated code in the core switching */
#define HARM_PREPROC                                    /* VA: basop issue 2339: Remove duplicated code in the core-coder DTX  */
#define HARM_NON_LINEARITY                              /* VA: basop issue 2345: Remove duplicated code in core-coder: non_linearity_fx() and LP CNG */

/* #################### End BE switches ################################## */

+25 −15
Original line number Diff line number Diff line
@@ -3165,16 +3165,20 @@ void ScaleShapedWB_fx(
);
void non_linearity_fx(
#ifdef HARM_NON_LINEARITY
    const Word16 element_mode, /* i  : element mode               */
#endif
    const Word16 i[],        /* i  : i signal    Q_inp          */
    Word32 output[],         /* o  : output signal   2*Q_inp    */
    const Word16 length,     /* i  : i length                   */
    Word32 *prev_scale,      /* i/o: memory          Q30        */
    Word16 Q_inp,
    Word16 coder_type,     /* i  : Coder Type                     */
    const Word16 Q_inp,      /* i  : scaling of input           */
    const Word16 coder_type, /* i  : Coder Type                 */
    Word16 *voice_factors,   /* i  : Voice Factors              */
    const Word16 L_frame     /* i  : ACELP frame length         */
);
#ifndef HARM_NON_LINEARITY
void non_linearity_ivas_fx(
    const Word16 i[],    /* i  : i   signal    Q_inp          */
    Word32 output[],     /* o  : output signal   2*Q_inp      */
@@ -3185,7 +3189,7 @@ void non_linearity_ivas_fx(
    Word16 *voice_factors, /* i  : Voice Factors                  */
    const Word16 L_frame   /* i  : ACELP frame length             */
);
#endif
void interp_code_5over2_fx(
    const Word16 inp_code[], /* i  : i   vector                */
    Word16 interp_code[],    /* o  : output vector             */
@@ -4003,6 +4007,7 @@ void CNG_exc_fx(
);
void cng_params_postupd_fx(
#ifndef HARM_NON_LINEARITY
    const Word16 ho_circ_ptr,          /* i  : pointer for CNG averaging buffers                  Q0    */
    Word16 *cng_buf_cnt,               /* i/o: counter for CNG store buffers                      Q0    */
    const Word16 *const cng_exc2_buf,  /* i  : Excitation buffer                                  Q_exc */
@@ -4012,6 +4017,7 @@ void cng_params_postupd_fx(
);
void cng_params_postupd_ivas_fx(
#endif
    const Word16 ho_circ_ptr,          /* i  : pointer for CNG averaging buffers                  Q0    */
    Word16 *cng_buf_cnt,               /* i/o: counter for CNG store buffers                      Q0    */
    const Word16 *const cng_exc2_buf,  /* i  : Excitation buffer                                  Q_exc */
@@ -4023,6 +4029,7 @@ void cng_params_postupd_ivas_fx(
);
void cng_params_upd_fx(
#ifndef HARM_NON_LINEARITY
    const Word16 lsp_new[],        /* i  : LSP parameters                                     Q15   */
    const Word16 exc2[],           /* i  : current enhanced excitation                        Q_exc */
    const Word16 L_frame,          /* i  : frame length                                       Q0    */
@@ -4041,6 +4048,7 @@ void cng_params_upd_fx(
);
void cng_params_upd_ivas_fx(
#endif
    const Word16 lsp_new[],         /* i  : LSP aprameters                                     Q15   */
    const Word16 exc2[],            /* i  : current enhanced excitation                        Q_exc */
    const Word16 L_frame,           /* i  : frame length                                       Q0    */
@@ -6159,6 +6167,7 @@ void swb_CNG_dec_fx(
    const Word16 Qsyn       /* i  : Q value of ACELP core synthesis          */
);
#ifndef HARM_NON_LINEARITY
void swb_CNG_dec_ivas_fx(
    Decoder_State *st_fx,   /* i/o: State structure                          */
    const Word16 *synth_fx, /* i  : ACELP core synthesis at 32kHz        Qsyn*/
@@ -6166,7 +6175,7 @@ void swb_CNG_dec_ivas_fx(
    const Word16 sid_bw,    /* i  : 0-NB/WB, 1-SWB SID                     Q0*/
    const Word16 Qsyn       /* i  : Q value of ACELP core synthesis          */
);
#endif
void td_cng_dec_init_fx(
    DEC_CORE_HANDLE st /* i/o: decoder state structure     */
);
@@ -10713,12 +10722,13 @@ void writeTCXparam_fx(
    const Word16 target_bitsTCX10[2],
    const Word16 pre_past_flag );
#ifndef HARM_NON_LINEARITY
void calculate_hangover_attenuation_gain_ivas_fx(
    Encoder_State *st,          /* i  : encoder state structure         */
    Word16 *att,                /* o  : attenuation factor              */
    const Word16 vad_hover_flag /* i  : VAD hangover flag               */
);
#endif
void init_coder_ace_plus_ivas_fx(
    Encoder_State *st,             /* i  : Encoder state                          */
    const Word32 last_total_brate, /* i  : last total bitrate                     */
+83 −28
Original line number Diff line number Diff line
@@ -5327,6 +5327,7 @@ void ScaleShapedWB_fx(
    return;
}


/*-------------------------------------------------------------------*
 * non_linearity()
 *
@@ -5335,7 +5336,7 @@ void ScaleShapedWB_fx(

static Word32 non_linearity_scaled_copy(
    const Word16 input[],
    Word16 j,
    const Word16 j,
    const Word16 length,
    Word32 output[],
    Word32 prev_scale,
@@ -5345,7 +5346,6 @@ static Word32 non_linearity_scaled_copy(
    Word16 i;
    Word32 L_tmp;


    IF( en_abs )
    {
        FOR( i = 0; i < j; i++ )
@@ -5397,6 +5397,7 @@ static Word32 non_linearity_scaled_copy(
            move32();
        }
    }

    return prev_scale;
}

@@ -5404,12 +5405,12 @@ static Word32 non_linearity_scaled_copy(
/*-------------------------------------------------------------------*
 * non_linearity()
 *
 * Apply a non linearity to the SHB excitation
 * Apply a non linearity to the SHB excitation in IVAS formats
 * -------------------------------------------------------------------*/

static Word32 non_linearity_scaled_copy_ivas(
    const Word16 input[],
    Word16 j,
    const Word16 j,
    const Word16 length,
    Word32 output[],
    Word32 prev_scale,
@@ -5419,7 +5420,6 @@ static Word32 non_linearity_scaled_copy_ivas(
    Word16 i;
    Word32 L_tmp;


    IF( en_abs )
    {
        FOR( i = 0; i < j; i++ )
@@ -5507,6 +5507,7 @@ static Word32 non_linearity_scaled_copy_ivas(
            move32();
        }
    }

    return prev_scale;
}

@@ -5533,16 +5534,19 @@ static Word32 non_linearity_scaled_copy_ivas(
/*==========================================================================*/

void non_linearity_fx(
#ifdef HARM_NON_LINEARITY
    const Word16 element_mode, /* i  : element mode           */
#endif
    const Word16 input[],    /* i  : input signal     Q_inp */
    Word32 output[],         /* o  : output signal  2*Q_inp */
    const Word16 length,     /* i  : input length           */
    Word32 *pPrevScale,      /* i/o: memory Q30             */
    Word16 Q_inp,
    Word16 coder_type,     /* i  : Coder Type          */
    const Word16 Q_inp,      /* i  : scaling of input       */
    const Word16 coder_type, /* i  : Coder Type             */
    Word16 *voice_factors,   /* i  : Voice Factors          */
    const Word16 L_frame     /* i  : ACELP frame length     */

)
#ifndef HARM_NON_LINEARITY
{
    Word16 i, j;
    Word16 max_val = 0;
@@ -5783,6 +5787,7 @@ void non_linearity_ivas_fx(
    const Word16 L_frame   /* i  : ACELP frame length  */

)
#endif
{
    Word16 i, j;
    Word16 max_val = 0;
@@ -5804,23 +5809,25 @@ void non_linearity_ivas_fx(
    Word32 prev_scale;
    Word16 length_half;
    Word16 sc_factor;
#ifdef HARM_NON_LINEARITY
    Word32 prev_scale_factor;
#endif

    IF( EQ_16( L_frame, L_FRAME16k ) )
    {
        nframes = 5;
        nframes = NB_SUBFR16k;
        move16();
        ths = 17817;
        move16(); /* 0.87*5 in Q12 */
    }
    ELSE
    {
        nframes = 4;
        nframes = NB_SUBFR;
        move16();
        ths = 15400;
        move16(); /* 0.94*4 in Q12 */
    }


    FOR( i = 0; i < nframes; i++ )
    {
        v_fac = add( v_fac, shr( voice_factors[i], 3 ) ); /* Q12 */
@@ -5837,7 +5844,6 @@ void non_linearity_ivas_fx(
    prev_scale = *pPrevScale;
    move32();


    /* Delay Alignment in FX is done inside swb_tbe_enc_fx() */

    FOR( i = j = 0; i < length_half; i++ )
@@ -5851,7 +5857,6 @@ void non_linearity_ivas_fx(
        max_val = s_max( max_val, tmp );
    }


    IF( GT_16( max_val, shl_sat( 1, Q_inp ) ) )
    {
        exp = norm_s( max_val );
@@ -5864,10 +5869,27 @@ void non_linearity_ivas_fx(
        move32(); /* Q31; 0.67 in Q31 */
    }

#ifdef HARM_NON_LINEARITY
    IF( element_mode == EVS_MONO )
    {
        prev_scale_factor = Mult_32_16( prev_scale, 32 );
    }
    ELSE
    {
        sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 );                    /* Adapt the scaling factor allowed depending of max position  */
        sc_factor = s_max( sc_factor, 1 );                                           /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */
        prev_scale_factor = L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ); /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */
    }

    test();
    IF( prev_scale <= 0 || ( GT_32( prev_scale_factor, scale /*Q31*/ ) ) )
#else
    sc_factor = s_max( sub( 13, norm_s( add( j, 1 ) ) ), 0 ); /* Adapt the scaling factor allowed depending of max position  */
    sc_factor = s_max( sc_factor, 1 );                        /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */

    test();
    IF( prev_scale <= 0 || GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */
#endif
    {
        scale_step = 16384;
        move16();                       /* Q14 */
@@ -5875,7 +5897,6 @@ void non_linearity_ivas_fx(
    }
    ELSE
    {

        /* Computing log2(scale) */
        IF( j == 0 )
        {
@@ -5912,7 +5933,16 @@ void non_linearity_ivas_fx(
        }
    }

#ifdef HARM_NON_LINEARITY
    IF( element_mode == EVS_MONO )
    {
        prev_scale = non_linearity_scaled_copy( input, j, length_half, output, prev_scale, scale_step, en_abs );
    }
    ELSE
#endif
    {
        prev_scale = non_linearity_scaled_copy_ivas( input, j, length_half, output, prev_scale, scale_step, en_abs );
    }

    max_val = 0;
    move16();
@@ -5940,12 +5970,28 @@ void non_linearity_ivas_fx(
        move32(); /* Q31; 0.67 in Q31 */
    }

#ifdef HARM_NON_LINEARITY
    IF( element_mode == EVS_MONO )
    {
        prev_scale_factor = Mult_32_16( prev_scale, 32 );
    }
    ELSE
    {
        sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 ); /* allowed intra frame jump is smaller */
        sc_factor = s_max( sc_factor, 1 );                                            /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */
        prev_scale_factor = L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ );  /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */
    }

    test();
    IF( prev_scale <= 0 || ( GT_32( prev_scale_factor, scale /*Q31*/ ) ) )
#else
    /* if ( element_mode > EVS_MONO ) */                                                                     /* element mode is not needed because the function is duplicated for IVAS */
    sc_factor = s_max( sub( 12, norm_s( add( sub( j, length_half ), 1 ) ) ), 0 );                            /* allowed intra frame jump is smaller */
    sc_factor = s_max( sc_factor, 1 );                                                                       /* Note: The sc_factor is the log2 of the sc_factor in the float code to simplify condition below */

    test();
    IF( prev_scale <= 0 || GT_32( L_shr( prev_scale, sub( sc_factor, 1 ) /*Q30 -> Q31*/ ), scale /*Q31*/ ) ) /* Since the sc_factor is the log2 of sc_factor in float, we apply it using L_shr */
#endif
    {
        scale_step = 16384;
        move16();                       /*Q14 */
@@ -5989,7 +6035,16 @@ void non_linearity_ivas_fx(
        }
    }

#ifdef HARM_NON_LINEARITY
    IF( element_mode == EVS_MONO )
    {
        prev_scale = non_linearity_scaled_copy( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs );
    }
    ELSE
#endif
    {
        prev_scale = non_linearity_scaled_copy_ivas( input + length_half, sub( j, length_half ), sub( length, length_half ), output + length_half, prev_scale, scale_step, en_abs );
    }

    *pPrevScale = prev_scale;
    move32();
+13 −0
Original line number Diff line number Diff line
@@ -2281,12 +2281,20 @@ ivas_error acelp_core_dec_fx(
        {
            IF( EQ_16( st->element_mode, EVS_MONO ) )
            {
#ifdef HARM_NON_LINEARITY
                non_linearity_fx( st->element_mode, bwe_exc_fx, bwe_exc_extended_fx, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame );
#else
                non_linearity_fx( bwe_exc_fx, bwe_exc_extended_fx, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame );
#endif
            }
            ELSE
            {
                Copy_Scale_sig_16_32_no_sat( st->hBWE_TD->old_bwe_exc_extended_fx, bwe_exc_extended_fx, NL_BUFF_OFFSET, ( sub( shl( st->Q_exc, 1 ), st->hBWE_TD->q_old_bwe_exc_extended_fx ) ) ); /* Q(q_old_bwe_exc_extended_fx) -> Q(2 * Q_exc) */
#ifdef HARM_NON_LINEARITY
                non_linearity_fx( st->element_mode, bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame );
#else
                non_linearity_ivas_fx( bwe_exc_fx, bwe_exc_extended_fx + NL_BUFF_OFFSET, L_FRAME32k, &st->hBWE_TD->bwe_non_lin_prev_scale_fx, st->Q_exc, st->coder_type, voice_factors_fx, st->L_frame );
#endif
                exp = sub( L_norm_arr( bwe_exc_extended_fx + L_FRAME32k, NL_BUFF_OFFSET ), 16 );
                Copy_Scale_sig_32_16( bwe_exc_extended_fx + L_FRAME32k, st->hBWE_TD->old_bwe_exc_extended_fx, NL_BUFF_OFFSET, exp ); /* Q(2 * Q_exc) -> Q(q_old_bwe_exc_extended_fx) */
                st->hBWE_TD->q_old_bwe_exc_extended_fx = add( shl( st->Q_exc, 1 ), exp );
@@ -2313,6 +2321,10 @@ ivas_error acelp_core_dec_fx(
    IF( GT_32( st->core_brate, SID_2k40 ) && st->hTdCngDec != NULL && st->hFdCngDec != NULL )
    {
        /* update CNG parameters in active frames */
#ifdef HARM_NON_LINEARITY
        cng_params_upd_fx( lsp_new_fx, exc_fx, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx,
                           st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode, st->hFdCngDec->hFdCngCom->CngBandwidth );
#else
        IF( EQ_16( st->element_mode, EVS_MONO ) )
        {
            cng_params_upd_fx( lsp_new_fx, exc_fx, st->L_frame, &st->hTdCngDec->ho_circ_ptr, st->hTdCngDec->ho_ener_circ_fx, &st->hTdCngDec->ho_circ_size, st->hTdCngDec->ho_lsp_circ_fx,
@@ -2324,6 +2336,7 @@ ivas_error acelp_core_dec_fx(
                                    st->hTdCngDec->ho_lsp_circ_fx, st->Q_exc, DEC, st->hTdCngDec->ho_env_circ_fx, NULL, NULL, NULL, NULL, st->last_active_brate, st->element_mode,
                                    st->hFdCngDec->hFdCngCom->CngBandwidth );
        }
#endif

        /* Set 16k LSP flag for CNG buffer */
        st->hTdCngDec->ho_16k_lsp[st->hTdCngDec->ho_circ_ptr] = 0;
Loading