Commit d0a75583 authored by Michael Sturm's avatar Michael Sturm
Browse files

Unifies IGF_replaceTCXNoise_2 function.

parent 594d23e7
Loading
Loading
Loading
Loading
+204 −1
Original line number Diff line number Diff line
@@ -250,6 +250,117 @@ static Word16 IGF_replaceTCXNoise_1( /**< out: Q0 | n
}
#endif

#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
/**********************************************************************/ /*
replaces TCX noise
**************************************************************************/
static void IGF_replaceTCXNoise_2( Word32* in,                           /**< in/out: Q31 | MDCT spectrum                */
                                   const Word16 in_exp,                  /**< in:         | MDCT spectrum exponent       */
                                   const Word16 s_l,                     /**< in: Q0      | noise headroom               */
                                   const Word16* TCXNoise,               /**< in: Q0      | tcx noise indicator vector   */
                                   const Word16 start,                   /**< in: Q0      | start MDCT subband index     */
                                   const Word16 stop,                    /**< in: Q0      | stop MDCT subband index      */
                                   Word32 totalNoiseNrg,                 /**< in:         | measured noise energy        */
                                   Word16 totalNoiseNrg_e,               /**< in:         | measured noise energy exp    */
                                   const Word16 n_noise_bands,           /**< in:         | number of noise bands in src */
                                   Word16* nfSeed,                       /**< in:         | random generator noise seed  */
                                   const Word16 element_mode             /**< in:         | IVAS element mode type       */
)
{
    Word16 g, n_noise_bands_tile, noise_band_ratio, sb, tmp, tmp_e, val;
    Word32 L_tmp, rE;

    n_noise_bands_tile = 0;
    move16();
    noise_band_ratio = 0;
    move16();
    val = 0;
    move16();
    tmp = 0;
    move16();
    tmp_e = 0;
    move16();
    rE = 0;
    move32();
    L_tmp = 0;
    move32();

    /* inject random noise and accumulate its energy */
    FOR( sb = start; sb < stop; sb++ )
    {
        IF( TCXNoise[sb] )
        {
            val = Random( nfSeed ); // Q0
            move16();
            in[sb] = L_deposit_l( val ); // Q0
            move32();
            val = shr( val, 5 );        // Q-5
            rE = L_mac( rE, val, val ); // Q-9
            n_noise_bands_tile = add( n_noise_bands_tile, 1 );
        }
    }

    /* compute noise gain factor */
    IF( NE_16( n_noise_bands_tile, 0 ) )
    {

        IF( EQ_16( element_mode, EVS_MONO ) )
        {
            totalNoiseNrg = L_shr( totalNoiseNrg, 1 ); // Q-9
        }

        /* make sure that rE is never 0 or smaller than totalNoiseNrg (for EVS) */
        IF( EQ_32( rE, 0 ) || ( LT_32( rE, totalNoiseNrg ) && EQ_16( element_mode, EVS_MONO ) ) )
        {
            rE = L_add( totalNoiseNrg, 0 ); /* save move32() -> use L_add(x, 0) = x; */ // Q31 - totalNoiseNrg_e
        }

        /* if totalNoiseNrg == 0, then rE must be at least 0x00010000, otherwise division by 0 will occur */
        IF ( EQ_32( totalNoiseNrg, 0 ) )
        {
            rE = L_max( rE, 0x00010000 ); // Q-9
        }

        IF( EQ_16( element_mode, EVS_MONO ) )
        {
            g = getSqrtWord32( L_mult( divide3232( totalNoiseNrg, rE ), 8192 /*1.0f / 4.0f Q15*/ ) ); // ((Q15 + Q15 + Q1) / 2) -> Q15
            g = shl_sat( g, 1 );
        }
        ELSE
        {
            noise_band_ratio = div_s( n_noise_bands_tile, n_noise_bands ); // Q15
            L_tmp = Mpy_32_16_1( totalNoiseNrg, noise_band_ratio ); // Q31 - totalNoiseNrg_e
            tmp = BASOP_Util_Divide3232_Scale( L_tmp, rE, &tmp_e );
            tmp_e = add( tmp_e, sub( totalNoiseNrg_e, 40 ) );
            g = Sqrt16( tmp, &tmp_e );
        }

        FOR( sb = start; sb < stop; sb++ )
        {
            IF( TCXNoise[sb] )
            {
                IF( EQ_16( element_mode, EVS_MONO ) )
                {
                    in[sb] = L_shr( L_mult( extract_l( in[sb] ), g ), s_l ); // Q15 + Q16 + Q1 - s_l
                    move32();
                }
                ELSE
                {
                    Word16 nrm = norm_l( in[sb] );
                    in[sb] = L_shl( in[sb], nrm ); // exp: 31 - tmp
                    move32();
                    in[sb] = Mpy_32_16_1( in[sb], g ); // exp: 31 - tmp + tmp_e
                    move32();
                    /* To handle corner cases */
                    in[sb] = L_shr_sat( in[sb], sub( in_exp, sub( add( 31, tmp_e ), nrm ) ) ); // Making the exponent same as original
                    move32();
                }
            }
        }
    }

}
#else
    /**********************************************************************/ /*
replaces TCX noise
**************************************************************************/
@@ -414,7 +525,7 @@ static void IGF_replaceTCXNoise_2_new_ivas( Word32 *in, /**< in
        }
    }
}

#endif

/**********************************************************************/               /*
              replaces TCX noise with noise band ratio (for IVAS)
@@ -888,6 +999,23 @@ static void IGF_prep_ivas( IGF_DEC_PRIVATE_DATA_HANDLE hPrivateData, /**< in
            /* medium whitening detected */
            IF( EQ_16( IGF_WHITENING_MID, hPrivateData->currWhiteningLevel[tile_idx] ) )
            {
#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
                IF( n_noise_bands != 0 )
                {
                    Word16 start = (EQ_16( element_mode, EVS_MONO ) ? minSrcSubband : strt_cpy);
                    IGF_replaceTCXNoise_2( igf_spec,
                                           specMed_e,
                                           hPrivateData->headroom_TCX_noise_white,
                                           TCXNoise,
                                           start,
                                           stop,
                                           totalNoiseNrg,
                                           totalNoiseNrg_exp,
                                           hPrivateData->n_noise_bands,
                                           hInfo->nfSeed,
                                           element_mode );
                }
#else
                IF( element_mode > EVS_MONO )
                {
                    IF( n_noise_bands != 0 )
@@ -916,6 +1044,7 @@ static void IGF_prep_ivas( IGF_DEC_PRIVATE_DATA_HANDLE hPrivateData, /**< in
                                               hInfo->nfSeed );
                    }
                }
#endif

                /* selected source spectrum is igf_spec, igf_spec contains the whitened signal in the core region */
                sel_spec = igf_spec;
@@ -929,6 +1058,23 @@ static void IGF_prep_ivas( IGF_DEC_PRIVATE_DATA_HANDLE hPrivateData, /**< in
            ELSE
            {

#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
                IF( n_noise_bands_off != 0 )
                {
                    Word16 start = ( EQ_16( element_mode, EVS_MONO ) ? minSrcSubband : strt_cpy );
                    IGF_replaceTCXNoise_2( src_spec,
                                           src_spec_e,
                                           hPrivateData->headroom_TCX_noise,
                                           TCXNoise,
                                           start,
                                           stop,
                                           totalNoiseNrg_off,
                                           totalNoiseNrg_off_exp,
                                           hPrivateData->n_noise_bands_off,
                                           hInfo->nfSeed,
                                           element_mode );
                }
#else
                IF( GT_16( element_mode, EVS_MONO ) )
                {
                    IF( n_noise_bands_off != 0 )
@@ -957,6 +1103,7 @@ static void IGF_prep_ivas( IGF_DEC_PRIVATE_DATA_HANDLE hPrivateData, /**< in
                                               hInfo->nfSeed );
                    }
                }
#endif
                /* selected source spectrum is pSpecFlat, pSpecFlat contains the signal before the LPC reshaping */
                sel_spec = src_spec;
                move16();
@@ -1062,6 +1209,19 @@ static void IGF_prepStereo(
            {
                IF( hPrivateDataL->n_noise_bands_off )
                {
#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
                    IGF_replaceTCXNoise_2( src_specL_fx,
                                           src_specL_e,
                                           0,
                                           TCXNoiseL,
                                           strt_cpy,
                                           stop,
                                           hPrivateDataL->totalNoiseNrg_off,
                                           hPrivateDataL->totalNoiseNrg_off_exp,
                                           hPrivateDataL->n_noise_bands_off,
                                           hInfoL->nfSeed,
                                           IVAS_CPE_MDCT );
#else
                    IGF_replaceTCXNoise_2_new_ivas( src_specL_fx,
                                                    src_specL_e,
                                                    TCXNoiseL,
@@ -1071,6 +1231,7 @@ static void IGF_prepStereo(
                                                    hPrivateDataL->totalNoiseNrg_off_exp,
                                                    hPrivateDataL->n_noise_bands_off,
                                                    hInfoL->nfSeed );
#endif
                }
                selectionL = 1;
                move16();
@@ -1097,6 +1258,19 @@ static void IGF_prepStereo(
            {
                IF( hPrivateDataR->n_noise_bands_off )
                {
#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
                    IGF_replaceTCXNoise_2( src_specR_fx,
                                           src_specR_e,
                                           0,
                                           TCXNoiseR,
                                           strt_cpy,
                                           stop,
                                           hPrivateDataR->totalNoiseNrg_off,
                                           hPrivateDataR->totalNoiseNrg_off_exp,
                                           hPrivateDataR->n_noise_bands_off,
                                           hInfoR->nfSeed,
                                           IVAS_CPE_MDCT );
#else
                    IGF_replaceTCXNoise_2_new_ivas( src_specR_fx,
                                                    src_specR_e,
                                                    TCXNoiseR,
@@ -1106,6 +1280,7 @@ static void IGF_prepStereo(
                                                    hPrivateDataR->totalNoiseNrg_off_exp,
                                                    hPrivateDataR->n_noise_bands_off,
                                                    hInfoR->nfSeed );
#endif
                }
                selectionR = 1;
                move16();
@@ -1227,6 +1402,19 @@ static void IGF_prepStereo(
                {
                    IF( hPrivateDataL->n_noise_bands_off )
                    {
#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
                        IGF_replaceTCXNoise_2( src_specL_fx,
                                               src_specL_e,
                                               0,
                                               TCXNoiseL,
                                               strt_cpy,
                                               stop,
                                               hPrivateDataL->totalNoiseNrg_off,
                                               hPrivateDataL->totalNoiseNrg_off_exp,
                                               hPrivateDataL->n_noise_bands_off,
                                               hInfoL->nfSeed,
                                               IVAS_CPE_MDCT );
#else
                        IGF_replaceTCXNoise_2_new_ivas( src_specL_fx,
                                                        src_specL_e,
                                                        TCXNoiseL,
@@ -1236,6 +1424,7 @@ static void IGF_prepStereo(
                                                        hPrivateDataL->totalNoiseNrg_off_exp,
                                                        hPrivateDataL->n_noise_bands_off,
                                                        hInfoL->nfSeed );
#endif
                    }
                    selectionL = 1;
                    move16();
@@ -1262,6 +1451,19 @@ static void IGF_prepStereo(
                {
                    IF( hPrivateDataR->n_noise_bands_off )
                    {
#ifdef FIX_2346_DUPLICATED_IGF_FUNCTIONS
                        IGF_replaceTCXNoise_2( src_specR_fx,
                                               src_specR_e,
                                               0,
                                               TCXNoiseR,
                                               strt_cpy,
                                               stop,
                                               hPrivateDataR->totalNoiseNrg_off,
                                               hPrivateDataR->totalNoiseNrg_off_exp,
                                               hPrivateDataR->n_noise_bands_off,
                                               hInfoR->nfSeed,
                                               IVAS_CPE_MDCT );
#else
                        IGF_replaceTCXNoise_2_new_ivas( src_specR_fx,
                                                        src_specR_e,
                                                        TCXNoiseR,
@@ -1271,6 +1473,7 @@ static void IGF_prepStereo(
                                                        hPrivateDataR->totalNoiseNrg_off_exp,
                                                        hPrivateDataR->n_noise_bands_off,
                                                        hInfoR->nfSeed );
#endif
                    }
                    selectionR = 1;
                    move16();