Commit a9d78e92 authored by Fabian Bauer's avatar Fabian Bauer
Browse files

Merge branch 'basop-2485-harmonize-perform_noise_estimation_enc_-ivas-_fx'...

Merge branch 'basop-2485-harmonize-perform_noise_estimation_enc_-ivas-_fx' into basop-2489-2485-harmonize-cng-enc
parents 7d94fdcd a983b3e9
Loading
Loading
Loading
Loading
+805 −0

File changed.

Preview size limit exceeded, changes collapsed.

+3 −0
Original line number Diff line number Diff line
@@ -88,6 +88,9 @@
#define FIX_2480_HARM_TONALMDCT                         /* FhG: basop issue 2480: Harmonize TonalMDCTConceal_Detect_fx() and TonalMDCTConceal_Detect_ivas_fx() */
#define FIX_2479_HARM_PITCH_GAIN                        /* FhG: basop issue 2479: Harmonize tcx_ltp_pitch_search_*(), tcx_ltp_find_gain_*fx() */
#define HARMONIZE_2481_EXTEND_SHRINK                    /* FhG: basop issue 2481: Harmonize extend_frm_*fx() and shrink_frm_*fx() */
#define FIX_2485_HARMONIZE_perform_noise_estimation_enc /* FhG: harmonize perform_noise_estimation_enc and perform_noise_estimation_enc_ivas */
#define FIX_2485_HARMONIZE_minimum_statistics           /* FhG: harmonize minimum_statistics and minimum_statistics_fx */

#define FIX_2489_HARMONIZE_FdCng_encodeSID              /* FhG: harmonize FdCng_encodeSID_fx() and FdCng_encodeSID_ivas_fx()  */

/* #################### End BE switches ################################## */
+28 −0
Original line number Diff line number Diff line
@@ -4587,6 +4587,33 @@ void expand_range_var_exp(
    Word16 *out_exp,
    const Word16 len );

#ifdef FIX_2485_HARMONIZE_minimum_statistics
void minimum_statistics_fx(
    Word16 len,                    /* i  : Total number of partitions (CLDFB or FFT)                   */
    Word16 lenFFT,                 /* i  : Number of FFT partitions                                  */
    Word16 *psize,                 /* i  : Partition sizes, fractional                             Q9*/
    Word16 *msPeriodog,            /* i  : Periodogram (energies)                                    */
    void *msNoiseFloor_buffer,     /* i/o: Noise floors (energies)                                Q25*/
    void *msNoiseEst_buffer,       /* i/o: Noise estimates (energies)                             Q25*/
    Word32 *msAlpha,               /* i/o: Forgetting factors                                        */
    Word16 *msPsd,                 /* i/o: Power Spectral Density (smoothed periodogram => energies) */
    Word16 *msPsdFirstMoment,      /* i/o: PSD statistics of 1st order (energy means)                */
    Word32 *msPsdSecondMoment,     /* i/o: PSD statistics of 2nd order (energy variances)            */
    Word32 *msMinBuf,              /* i/o: Buffer of minima (energies)                               */
    Word32 *msBminWin,             /* o  : Bias correction factors                                   */
    Word32 *msBminSubWin,          /* o  : Bias correction factors                                   */
    Word32 *msCurrentMin,          /* i/o: Local minima (energies)                                   */
    Word32 *msCurrentMinOut,       /* i/o: Local minima (energies)                                   */
    Word32 *msCurrentMinSubWindow, /* i/o: Local minima (energies)                                   */
    Word16 *msLocalMinFlag,        /* i  : Binary flag                                               */
    Word16 *msNewMinFlag,          /* i  : Binary flag                                               */
    Word16 *msPeriodogBuf,         /* i/o: Buffer of periodograms (energies)                         */
    Word16 *msPeriodogBufPtr,      /* i/o: Counter                                                   */
    HANDLE_FD_CNG_COM hFdCngCom,   /* i/o: FD_CNG structure containing all buffers and variables */
    const Word16 enc_dec,          /* i  : encoder/decoder indicator */
    const Word16 element_mode,     /* i  : IVAS element mode type    */
    const Word16 isIVAS );
#else
void minimum_statistics(
    Word16 len,                    /* i  : Total number of partitions (CLDFB or FFT)                   */
    Word16 lenFFT,                 /* i  : Number of FFT partitions                                  */
@@ -4636,6 +4663,7 @@ void minimum_statistics_fx(
    const Word16 enc_dec,          /* i  : encoder/decoder indicator */
    const Word16 element_mode      /* i  : IVAS element mode type    */
);
#endif

/* Apply bitrate-dependant scale */
void apply_scale(
+34 −1
Original line number Diff line number Diff line
@@ -1280,6 +1280,33 @@ void perform_noise_estimation_dec_fx(
        compress_range( hFdCngDec->msPeriodog, hFdCngDec->msPeriodog_exp, hFdCngDec->msLogPeriodog, npart );

        /* Call the minimum statistics routine for noise estimation */
#ifdef FIX_2485_HARMONIZE_minimum_statistics
        minimum_statistics_fx(
            npart,
            nFFTpart,
            psize_norm,
            hFdCngDec->msLogPeriodog,
            hFdCngDec->msNoiseFloor,
            hFdCngDec->msLogNoiseEst,
            hFdCngDec->msAlpha,
            hFdCngDec->msPsd,
            hFdCngDec->msPsdFirstMoment,
            hFdCngDec->msPsdSecondMoment,
            hFdCngDec->msMinBuf,
            hFdCngDec->msBminWin,
            hFdCngDec->msBminSubWin,
            hFdCngDec->msCurrentMin,
            hFdCngDec->msCurrentMinOut,
            hFdCngDec->msCurrentMinSubWindow,
            hFdCngDec->msLocalMinFlag,
            hFdCngDec->msNewMinFlag,
            hFdCngDec->msPeriodogBuf,
            &( hFdCngDec->msPeriodogBufPtr ),
            hFdCngDec->hFdCngCom,
            0,
            0,
            0 );
#else
        minimum_statistics(
            npart,
            nFFTpart,
@@ -1302,6 +1329,7 @@ void perform_noise_estimation_dec_fx(
            hFdCngDec->msPeriodogBuf,
            &( hFdCngDec->msPeriodogBufPtr ),
            hFdCngDec->hFdCngCom );
#endif

        /* Expand MS outputs */
        expand_range( hFdCngDec->msLogNoiseEst, hFdCngDec->msNoiseEst, &hFdCngDec->msNoiseEst_exp, npart );
@@ -2099,7 +2127,12 @@ void perform_noise_estimation_dec_ivas_fx(
            hFdCngDec->msPeriodogBuf,
            &( hFdCngDec->msPeriodogBufPtr ),
            hFdCngDec->hFdCngCom,
            DEC, element_mode );
            DEC, element_mode
#ifdef FIX_2485_HARMONIZE_minimum_statistics
            ,
            1
#endif
        );

        FOR( i = 0; i < hFdCngDec->npart_shaping; i++ )
        {
+290 −1
Original line number Diff line number Diff line
@@ -405,6 +405,7 @@ void resetFdCngEnc_fx(
    return;
}

#ifndef FIX_2485_HARMONIZE_perform_noise_estimation_enc
/*
   perform_noise_estimation_enc_fx

@@ -565,6 +566,7 @@ void perform_noise_estimation_enc_fx(

    return;
}
#endif /*FIX_2485_HARMONIZE_perform_noise_estimation_enc*/

/*
   AdjustFirstSID_fx
@@ -1743,7 +1745,7 @@ Word16 cng_energy_ivas_fx(
    return enr;
}


#ifndef FIX_2485_HARMONIZE_perform_noise_estimation_enc
void perform_noise_estimation_enc_ivas_fx(
    Word32 *band_energies, /* i  : energy in critical bands without minimum noise floor MODE2_E_MIN    band_energies_exp*/
    Word16 band_energies_exp,
@@ -1933,6 +1935,293 @@ void perform_noise_estimation_enc_ivas_fx(

    return;
}
#endif /*FIX_2485_HARMONIZE_perform_noise_estimation_enc*/

#ifdef FIX_2485_HARMONIZE_perform_noise_estimation_enc
void perform_noise_estimation_enc_fx(
    Word32 *band_energies, /* i  : energy in critical bands without minimum noise floor MODE2_E_MIN    band_energies_exp*/
    Word16 band_energies_exp,
    Word32 *enerBuffer, /* enerBuffer_exp */
    Word16 enerBuffer_exp,
    HANDLE_FD_CNG_ENC hFdCngEnc, /* i/o: CNG structure containing all buffers and variables        */
    const Word32 input_Fs,       /* i  : input sampling rate                                     Q0*/
    CPE_ENC_HANDLE hCPE,         /* i  : CPE encoder structure                                     */
    int element_mode             /* i  : is element EVS or IVAS                                    */
)
{
    if ( element_mode == 0 )
    {
        (void) input_Fs;
        (void) hCPE;
    }

    Word16 i, j, s, s1, s2;
    Word16 numBands;
    Word16 nFFTpart = hFdCngEnc->hFdCngCom->nFFTpart; /* Q0 */
    move16();
    Word16 numCoreBands = hFdCngEnc->hFdCngCom->numCoreBands; /* Q0 */
    move16();
    Word16 regularStopBand = hFdCngEnc->hFdCngCom->regularStopBand;
    move16();
    Word16 numSlots = hFdCngEnc->hFdCngCom->numSlots; /* Q0 */
    move16();
    Word16 nCLDFBpart = hFdCngEnc->hFdCngCom->nCLDFBpart; /* Q0 */
    move16();
    assert( numSlots == 16 );
    Word32 *periodog = hFdCngEnc->hFdCngCom->periodog; /* exp(peridog_exp) */
    Word32 *ptr_per_fx = periodog;
    Word32 *msPeriodog_fx = hFdCngEnc->msPeriodog_fx;
    Word32 tmp;

    /* preemphasis compensation and grouping of per bin energies into msPeriodog */
    FOR( i = 0; i < nFFTpart; i++ )
    {
        tmp = L_add( L_shr( band_energies[i], 1 ), L_shr( band_energies[i + NB_BANDS], 1 ) ); /* exp(band_energies_exp) */
        msPeriodog_fx[i] = Mpy_32_16_1( tmp, preemphCompensation_fx[i] );                     /* exp(band_energies_exp + 4) */
        move32();
    }

    /* exponent for fft part of msPeriodog */
    hFdCngEnc->msPeriodog_fx_exp_fft = add( band_energies_exp, PREEMPH_COMPENSATION_EXP );
    move16();

    numBands = sub( regularStopBand, numCoreBands ); /* Q0 */

    /* Adjust to the desired time resolution by averaging the periodograms over the CLDFB time slots */
    IF( element_mode > 0 ) /*IVAS: more precission*/
    {
        Word16 max_exp = -31;
        move16();
        Word16 periodog_exp[PERIODOGLEN];
        Word64 periodog_64;
        Word32 scaleEB_fx = 0;
        move32();
        i = 0;
        move16();

        test();
        IF( hCPE != NULL && hCPE->hStereoDft != NULL )
        {
            SWITCH( input_Fs )
            {
                case 8000:
                    scaleEB_fx = 251648; // Q35
                    move32();
                    BREAK;
                case 16000:
                    scaleEB_fx = 62912; // Q35
                    move32();
                    BREAK;
                case 32000:
                    scaleEB_fx = 15728; // Q35
                    move32();
                    BREAK;
                case 48000:
                    scaleEB_fx = 6991; // Q35
                    move32();
                    BREAK;
                default:
                    assert( 0 && "invalid sample rate" );
            }
        }
        ELSE
        {
            Word32 numSlots_inv_fx = 1073741824; // Q34 of .0625
            move32();
            scaleEB_fx = Mpy_32_32( numSlots_inv_fx, L_deposit_l( hFdCngEnc->hFdCngCom->scalingFactor ) ); // Q34 + Q30 - Q31 = Q33
            scaleEB_fx = L_shl( scaleEB_fx, 2 );                                                           // Q35
        }

        FOR( j = numCoreBands; j < regularStopBand; j++ )
        {
            periodog_64 = W_mult_32_32( enerBuffer[j], scaleEB_fx );
            Word16 scale = W_norm( periodog_64 );
            *ptr_per_fx = W_extract_h( W_shl( periodog_64, scale ) );
            move32();
            periodog_exp[i] = sub( Q31, add( add( sub( Q31, enerBuffer_exp ), 35 - 31 ), scale ) );
            move16();
            if ( *ptr_per_fx )
            {
                max_exp = s_max( max_exp, periodog_exp[i] );
            }
            ptr_per_fx++;
            i++;
        }

        /* exponent for cldfb part of msPeriodog */
        FOR( i = 0; i < numBands; i++ )
        {
            periodog[i] = L_shr( periodog[i], sub( max_exp, periodog_exp[i] ) );
            move16();
        }
        hFdCngEnc->hFdCngCom->exp_cldfb_periodog = max_exp;
        move16();
    }
    ELSE /*EVS_MONO*/
    {
        IF( numBands > 0 )
        {
            /* Adjust to the desired time resolution by averaging the periodograms over the CLDFB time slots */
            FOR( j = numCoreBands; j < regularStopBand; j++ )
            {
                *ptr_per_fx = Mpy_32_16_1( enerBuffer[j], hFdCngEnc->hFdCngCom->scalingFactor ); /* exp(enerBuffer_exp) */
                move32();

                ptr_per_fx++;
            }

            /* exponent for cldfb part of msPeriodog */
            hFdCngEnc->hFdCngCom->exp_cldfb_periodog = add( sub( enerBuffer_exp, 4 ), CLDFBscalingFactor_EXP );
        }
    }

    IF( numBands > 0 )
    {
        ///* Adjust CLDFB filterbank to the desired frequency resolution by averaging over spectral partitions for SID transmission */
        bandcombinepow(
            periodog,
            hFdCngEnc->hFdCngCom->exp_cldfb_periodog,
            numBands,
            hFdCngEnc->hFdCngCom->CLDFBpart,
            nCLDFBpart,
            hFdCngEnc->hFdCngCom->CLDFBpsize_inv,
            &msPeriodog_fx[nFFTpart],
            &hFdCngEnc->msPeriodog_fx_exp_cldfb );

        ///* find common exponent for fft part and cldfb part of msperiodog */
        s1 = L_norm_arr( msPeriodog_fx, nFFTpart );
        s2 = L_norm_arr( &msPeriodog_fx[nFFTpart], nCLDFBpart );

        s = s_max( sub( hFdCngEnc->msPeriodog_fx_exp_fft, s1 ), sub( hFdCngEnc->msPeriodog_fx_exp_cldfb, s2 ) );
        s1 = sub( s, hFdCngEnc->msPeriodog_fx_exp_fft );
        s2 = sub( s, hFdCngEnc->msPeriodog_fx_exp_cldfb );

        hFdCngEnc->msPeriodog_fx_exp_fft = s;
        move16();
        hFdCngEnc->msPeriodog_fx_exp_cldfb = s;
        move16();

        FOR( i = 0; i < nFFTpart; i++ )
        {
            msPeriodog_fx[i] = L_shr( msPeriodog_fx[i], s1 ); // hFdCngEnc->msPeriodog_fx_exp_fft
            move32();
        }

        FOR( i = 0; i < nCLDFBpart; i++ )
        {
            msPeriodog_fx[nFFTpart + i] = L_shr( msPeriodog_fx[nFFTpart + i], s2 ); /*  hFdCngEnc->msPeriodog_fx_exp_fft */
            move32();
        }
    }
    /* exponent for entire msPeriodog vector */
    hFdCngEnc->msPeriodog_fx_exp = hFdCngEnc->msPeriodog_fx_exp_fft;
    move16();

    /* Compress MS inputs */
    compress_range( msPeriodog_fx, hFdCngEnc->msPeriodog_fx_exp, hFdCngEnc->msLogPeriodog_fx, hFdCngEnc->hFdCngCom->npart );

    /* Call the minimum statistics routine for noise estimation */
#ifdef FIX_2485_HARMONIZE_minimum_statistics
    minimum_statistics_fx(
        hFdCngEnc->hFdCngCom->npart,
        nFFTpart,
        hFdCngEnc->hFdCngCom->psize_norm,
        hFdCngEnc->msLogPeriodog_fx,
        ( element_mode == 0 ) ? ( (void *) hFdCngEnc->msNoiseFloor_fx ) : ( (void *) hFdCngEnc->msNoiseFloor_32fx ),
        ( element_mode == 0 ) ? ( (void *) hFdCngEnc->msLogNoiseEst_fx ) : ( (void *) hFdCngEnc->msLogNoiseEst_32fx ),
        hFdCngEnc->msAlpha_fx,
        hFdCngEnc->msPsd_fx,
        hFdCngEnc->msPsdFirstMoment_fx,
        hFdCngEnc->msPsdSecondMoment_fx,
        hFdCngEnc->msMinBuf_fx,
        hFdCngEnc->msBminWin_fx,
        hFdCngEnc->msBminSubWin_fx,
        hFdCngEnc->msCurrentMin_fx,
        hFdCngEnc->msCurrentMinOut_fx,
        hFdCngEnc->msCurrentMinSubWindow_fx,
        hFdCngEnc->msLocalMinFlag,
        hFdCngEnc->msNewMinFlag,
        hFdCngEnc->msPeriodogBuf_fx,
        &( hFdCngEnc->msPeriodogBufPtr ),
        hFdCngEnc->hFdCngCom,
        ENC,
        ( hCPE == NULL ) ? 0 : hCPE->element_mode,
        element_mode );


    /* Expand MS outputs */
    IF( element_mode > 0 )
    {
        expand_range_fx( hFdCngEnc->msLogNoiseEst_32fx, hFdCngEnc->msNoiseEst_fx, &hFdCngEnc->msNoiseEst_fx_exp, hFdCngEnc->hFdCngCom->npart );
    }
    ELSE
    {
        expand_range( hFdCngEnc->msLogNoiseEst_fx, hFdCngEnc->msNoiseEst_fx, &hFdCngEnc->msNoiseEst_fx_exp, hFdCngEnc->hFdCngCom->npart );
    }
#else
    IF( element_mode > 0 )
    {
        minimum_statistics_fx(
            hFdCngEnc->hFdCngCom->npart,
            nFFTpart,
            hFdCngEnc->hFdCngCom->psize_norm,
            hFdCngEnc->msLogPeriodog_fx,
            hFdCngEnc->msNoiseFloor_32fx,  /*32bit*/
            hFdCngEnc->msLogNoiseEst_32fx, /*32bit*/
            hFdCngEnc->msAlpha_fx,
            hFdCngEnc->msPsd_fx,
            hFdCngEnc->msPsdFirstMoment_fx,
            hFdCngEnc->msPsdSecondMoment_fx,
            hFdCngEnc->msMinBuf_fx,
            hFdCngEnc->msBminWin_fx,
            hFdCngEnc->msBminSubWin_fx,
            hFdCngEnc->msCurrentMin_fx,
            hFdCngEnc->msCurrentMinOut_fx,
            hFdCngEnc->msCurrentMinSubWindow_fx,
            hFdCngEnc->msLocalMinFlag,
            hFdCngEnc->msNewMinFlag,
            hFdCngEnc->msPeriodogBuf_fx,
            &( hFdCngEnc->msPeriodogBufPtr ),
            hFdCngEnc->hFdCngCom,
            ENC,
            ( hCPE == NULL ) ? 0 : hCPE->element_mode );

        /* Expand MS outputs */
        expand_range_fx( hFdCngEnc->msLogNoiseEst_32fx, hFdCngEnc->msNoiseEst_fx, &hFdCngEnc->msNoiseEst_fx_exp, hFdCngEnc->hFdCngCom->npart );
    }
    ELSE
    {
        minimum_statistics(
            hFdCngEnc->hFdCngCom->npart,
            nFFTpart,
            hFdCngEnc->hFdCngCom->psize_norm,
            hFdCngEnc->msLogPeriodog_fx,
            hFdCngEnc->msNoiseFloor_fx,  /*16bit*/
            hFdCngEnc->msLogNoiseEst_fx, /*16bit*/
            hFdCngEnc->msAlpha_fx,
            hFdCngEnc->msPsd_fx,
            hFdCngEnc->msPsdFirstMoment_fx,
            hFdCngEnc->msPsdSecondMoment_fx,
            hFdCngEnc->msMinBuf_fx,
            hFdCngEnc->msBminWin_fx,
            hFdCngEnc->msBminSubWin_fx,
            hFdCngEnc->msCurrentMin_fx,
            hFdCngEnc->msCurrentMinOut_fx,
            hFdCngEnc->msCurrentMinSubWindow_fx,
            hFdCngEnc->msLocalMinFlag,
            hFdCngEnc->msNewMinFlag,
            hFdCngEnc->msPeriodogBuf_fx,
            &( hFdCngEnc->msPeriodogBufPtr ),
            hFdCngEnc->hFdCngCom );

        /* Expand MS outputs */
        expand_range( hFdCngEnc->msLogNoiseEst_fx, hFdCngEnc->msNoiseEst_fx, &hFdCngEnc->msNoiseEst_fx_exp, hFdCngEnc->hFdCngCom->npart );
    }
#endif /*FIX_2485_HARMONIZE_minimum_statistics*/

    return;
}
#endif /*FIX_2485_HARMONIZE_perform_noise_estimation_enc*/


/*-------------------------------------------------------------------*
Loading