Commit bbb30d91 authored by Devansh Kandpal's avatar Devansh Kandpal
Browse files

mid stage changes

parent 3f503ab0
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -1097,8 +1097,8 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    const IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pInput_params,
    const HRTFS_STATISTICS_HANDLE hHrtfStatistics,
    const int32_t output_Fs,
    float *pOutput_t60,
    float *pOutput_ene );
    Word32 *pOutput_t60,
    Word32 *pOutput_ene );
void ivas_reverb_interpolate_acoustic_data_fx(
  const Word16 input_table_size,
  const Word32 *pInput_fc, //input in Q16
@@ -1119,10 +1119,8 @@ void ivas_reverb_interpolate_energies_fx(
    const Word32 *pInput_ene_r, //input in Q28
    const Word16 output_table_size,
    const Word32 *pOutput_fc,
    Word32 *pOutput_ene_l_m, // output m
    Word32 *pOutput_ene_r_m, // output m
    Word16 *pOutput_ene_l_e, //output e
    Word16 *pOutput_ene_r_e  //output e
    Word32 *pOutput_ene_l,  // output in Q28
    Word32 *pOutput_ene_r   // output in Q28
);
#endif
/*---------------------------------------------------------------------------------*
+32 −24
Original line number Diff line number Diff line
@@ -936,15 +936,14 @@ void ivas_reverb_interpolate_energies_fx(
    const Word32 *pInput_ene_r, // input in Q28
    const Word16 output_table_size,
    const Word32 *pOutput_fc, // Q16
    Word32 *pOutput_ene_l_m,
    Word32 *pOutput_ene_r_m,
    Word16 *pOutput_ene_l_e,
    Word16 *pOutput_ene_r_e )
    Word32 *pOutput_ene_l,      // output in Q28
    Word32 *pOutput_ene_r )     // output in Q28
{
    Word16 input_idx, output_idx;
    Word16 input_idx, input_idx_next, output_idx;
    Word32 rel_offset;
    Word16 rel_offset_e;
    input_idx = 0;
    input_idx_next = 0;
    move16();

    FOR( output_idx = 0; output_idx < output_table_size; output_idx++ )
@@ -954,6 +953,8 @@ void ivas_reverb_interpolate_energies_fx(
        {
            input_idx = 0;
            move16();
            input_idx_next = 1;
            move16();
            rel_offset = 0;
            move32();
            rel_offset_e = 0;
@@ -965,7 +966,10 @@ void ivas_reverb_interpolate_energies_fx(
            IF( GT_32( pOutput_fc[output_idx], pInput_fc[input_table_size - 1] ) )
            {
                input_idx = sub( input_table_size, 2 );
                rel_offset = ONE_IN_Q30; // Q30;
                move16();
                input_idx_next = add( input_idx, 1 );
                move16();
                rel_offset = ONE_IN_Q15;
                move32();
                rel_offset_e = 1;
                move16();
@@ -973,30 +977,34 @@ void ivas_reverb_interpolate_energies_fx(
            /* otherwise use linear interpolation between 2 consecutive points in the input table */
            ELSE
            {
                WHILE( GT_32( pOutput_fc[output_idx], pInput_fc[input_idx + 1] ) )
                WHILE( GT_32( pOutput_fc[output_idx], pInput_fc[input_idx_next] ) )
                {
                    input_idx = add( input_idx, 1 );
                }
                rel_offset = BASOP_Util_Divide3232_Scale( L_sub( pOutput_fc[output_idx], pInput_fc[input_idx] ), L_sub( pInput_fc[input_idx + 1], pInput_fc[input_idx] ), &rel_offset_e ); // q15
                rel_offset = L_shl_sat( rel_offset, add( 16, rel_offset_e ) );
                rel_offset_e = 0;
                move16();
                // Rel_offset
                rel_offset = BASOP_Util_Divide3232_Scale( L_sub( pOutput_fc[output_idx], pInput_fc[input_idx] ), L_sub( pInput_fc[input_idx_next], pInput_fc[input_idx] ), &rel_offset_e ); // Q15
                rel_offset = L_shl_sat( rel_offset, rel_offset_e );
            }
        }
        Word32 mult1;
        Word16 mult_e = 0;
        move16();
        mult1 = Mpy_32_32( rel_offset, L_sub( pInput_ene_l[input_idx + 1], pInput_ene_l[input_idx] ) );
        pOutput_ene_l_m[output_idx] = BASOP_Util_Add_Mant32Exp( pInput_ene_l[input_idx], 3, mult1, add( 3, rel_offset_e ), &mult_e ); // 31 - (31 - rel_offset_e + 28 - 31)
        move32();
        pOutput_ene_l_e[output_idx] = mult_e;
        move16();

        mult1 = Mpy_32_32( rel_offset, L_sub( pInput_ene_r[input_idx + 1], pInput_ene_r[input_idx] ) );
        pOutput_ene_r_m[output_idx] = BASOP_Util_Add_Mant32Exp( pInput_ene_r[input_idx], 3, mult1, add( 3, rel_offset_e ), &mult_e ); // 31 - (31 - rel_offset_e + 28 - 31)
        move32();
        pOutput_ene_r_e[output_idx] = mult_e;
        move16();
        pOutput_ene_l[output_idx] = L_add( pInput_ene_l[input_idx], Mpy_32_32( rel_offset, L_sub( pInput_ene_l[input_idx_next], pInput_ene_l[input_idx] ) ) );
        pOutput_ene_r[output_idx] = L_add( pInput_ene_r[input_idx], Mpy_32_32( rel_offset, L_sub( pInput_ene_r[input_idx_next], pInput_ene_r[input_idx] ) ) );

        //Word32 mult1;
        //Word16 mult_e = 0;

        //move16();
        //mult1 = Mpy_32_32( rel_offset, L_sub( pInput_ene_l[input_idx + 1], pInput_ene_l[input_idx] ) );
        //pOutput_ene_l_m[output_idx] = BASOP_Util_Add_Mant32Exp( pInput_ene_l[input_idx], 3, mult1, add( 3, rel_offset_e ), &mult_e ); // 31 - (31 - rel_offset_e + 28 - 31)
        //move32();
        //pOutput_ene_l_e[output_idx] = mult_e;
        //move16();

        //mult1 = Mpy_32_32( rel_offset, L_sub( pInput_ene_r[input_idx + 1], pInput_ene_r[input_idx] ) );
        //pOutput_ene_r_m[output_idx] = BASOP_Util_Add_Mant32Exp( pInput_ene_r[input_idx], 3, mult1, add( 3, rel_offset_e ), &mult_e ); // 31 - (31 - rel_offset_e + 28 - 31)
        //move32();
        //pOutput_ene_r_e[output_idx] = mult_e;
        //move16();
    }

    return;
+11 −4
Original line number Diff line number Diff line
@@ -2439,24 +2439,31 @@ ivas_error ivas_binaural_reverb_init(

    if ( ( roomAcoustics != NULL ) && roomAcoustics->override )
    {
        float t60_temp[CLDFB_NO_CHANNELS_MAX];
        float ene_temp[CLDFB_NO_CHANNELS_MAX];
        /*float t60_temp[CLDFB_NO_CHANNELS_MAX];
        float ene_temp[CLDFB_NO_CHANNELS_MAX];*/

        revTimes = t60;
        revEne = ene;

        if ( ( error = ivas_reverb_prepare_cldfb_params( roomAcoustics, hHrtfStatistics, sampling_rate, t60_temp, ene_temp ) ) != IVAS_ERR_OK )
        /*if ( ( error = ivas_reverb_prepare_cldfb_params( roomAcoustics, hHrtfStatistics, sampling_rate, t60_temp, ene_temp ) ) != IVAS_ERR_OK )
        {
            return error;
        }*/

        if ( ( error = ivas_reverb_prepare_cldfb_params( roomAcoustics, hHrtfStatistics, sampling_rate, t60, ene ) ) != IVAS_ERR_OK )
        {
            return error;
        }

        //Port this??
        preDelay = (int16_t) roundf( 48000.0f * roomAcoustics->acousticPreDelay / CLDFB_NO_CHANNELS_MAX );
/*
#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
        floatToFixed_arrL( t60_temp, t60, Q26, CLDFB_NO_CHANNELS_MAX );
#else
        floatToFixed_arrL( t60_temp, t60, Q31, CLDFB_NO_CHANNELS_MAX );
#endif
        floatToFixed_arrL( ene_temp, ene, Q31, CLDFB_NO_CHANNELS_MAX );
        floatToFixed_arrL( ene_temp, ene, Q31, CLDFB_NO_CHANNELS_MAX );*/
    }
    else
    {
+103 −28
Original line number Diff line number Diff line
@@ -64,7 +64,7 @@ typedef struct cldfb_convolver_state
    float filter_states_im[BINAURAL_CONVBANDS][CLDFB_CONVOLVER_NTAPS_MAX];
} cldfb_convolver_state;

static void ivas_reverb_set_energies( const Word32 *avg_pwr_l, const Word32 *avg_pwr_r, const int32_t sampling_rate, float *avg_pwr_l_out, float *avg_pwr_r_out );
static void ivas_reverb_set_energies( const Word32 *avg_pwr_l, const Word32 *avg_pwr_r, const int32_t sampling_rate, Word32 *avg_pwr_l_out, Word32 *avg_pwr_r_out );

/*-----------------------------------------------------------------------------------------*
 * Function ivas_reverb_prepare_cldfb_params()
@@ -77,21 +77,29 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    const IVAS_ROOM_ACOUSTICS_CONFIG_DATA *pInput_params,
    const HRTFS_STATISTICS_HANDLE hHrtfStatistics,
    const int32_t output_Fs,
    float *pOutput_t60,
    float *pOutput_ene )
    Word32 *pOutput_t60,
    Word32 *pOutput_ene )
{
    int16_t idx;
    float avg_pwr_left[CLDFB_NO_CHANNELS_MAX];
    /*float avg_pwr_left[CLDFB_NO_CHANNELS_MAX];
    float avg_pwr_right[CLDFB_NO_CHANNELS_MAX];
    float delay_diff, ln_1e6_inverted, exp_argument;
    const float dist = DEFAULT_SRC_DIST;
    const float dmx_gain_2 = 4.0f * EVS_PI * dist * dist / 0.001f;
    const float dmx_gain_2 = 4.0f * EVS_PI * dist * dist / 0.001f;*/

    Word32 output_fc_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 output_t60_fx[CLDFB_NO_CHANNELS_MAX];
    Word16 output_t60_e[CLDFB_NO_CHANNELS_MAX];
    Word32 output_ene_fx[CLDFB_NO_CHANNELS_MAX];
    Word16 output_ene_e[CLDFB_NO_CHANNELS_MAX];
    Word32 delay_diff_fx;

    Word32 delay_diff_fx, ln_1e6_inverted_fx, L_tmp;
    const Word32 dmx_gain_2_fx = 1852986624; // Q16
    Word16 exp_argument_fx, exp_argument_e, tmp, tmp_flag;
    Word16 pow_exp;

    Word32 avg_pwr_left_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 avg_pwr_right_fx[CLDFB_NO_CHANNELS_MAX];

    for ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
@@ -101,30 +109,96 @@ ivas_error ivas_reverb_prepare_cldfb_params(
    ivas_reverb_interpolate_acoustic_data_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_rt60_fx, pInput_params->pAcoustic_dsr_fx,
                                              CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_t60_fx, output_ene_fx, output_t60_e, output_ene_e );

    /* adjust DSR for the delay difference */
    delay_diff_fx = L_sub( pInput_params->inputPreDelay_fx, pInput_params->acousticPreDelay_fx );
    /*ivas_reverb_interpolate_energies_fx( pInput_params->nBands, pInput_params->pFc_input_fx, pInput_params->pAcoustic_rt60_fx, pInput_params->pAcoustic_dsr_fx,
                                              CLDFB_NO_CHANNELS_MAX, output_fc_fx, output_t60_fx, output_ene_fx );*/

    delay_diff = (float) delay_diff_fx / ONE_IN_Q27;
    //Return  T60 in fixed-point
    for ( int i = 0; i < CLDFB_NO_CHANNELS_MAX; i++ )
    {
        pOutput_t60[i] = (float) fabs( me2f( output_t60_fx[i], output_t60_e[i] ) );
        pOutput_ene[i] = (float) fabs( me2f( output_ene_fx[i], output_ene_e[i] ) );
        pOutput_t60[i] = L_shl( output_t60_fx[i], sub( output_t60_e, 5 ) );
        //pOutput_t60[i] = output_t60_fx[i] * ( 1 << output_t60_e[i] ); // Will not work - mantissa is 16 bit
    }

    ln_1e6_inverted = 1.0f / logf( 1e06f );
    /*for ( int i = 0; i < CLDFB_NO_CHANNELS_MAX; i++ ) //Should not be required in fixed-point
    {
        pOutput_t60[i] = (float) fabs( me2f( output_t60_fx[i], output_t60_e[i] ) ); -->This operation must be recreated in fixed-point to return T60
        pOutput_ene[i] = (float) fabs( me2f( output_ene_fx[i], output_ene_e[i] ) );
    }*/

    /* adjust DSR for the delay difference */

    delay_diff_fx = L_sub( pInput_params->inputPreDelay_fx, pInput_params->acousticPreDelay_fx );
    ln_1e6_inverted_fx = 155440049; // Q31 /* 1.0f / logf( 1e06f ) */

    for ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
        exp_argument = delay_diff / ( pOutput_t60[idx] * ln_1e6_inverted );
        L_tmp = Mpy_32_32( output_t60_fx[idx], ln_1e6_inverted_fx );
        exp_argument_fx = BASOP_Util_Divide3232_Scale( delay_diff_fx, L_tmp, &exp_argument_e );
        exp_argument_e = add( exp_argument_e, sub( 4, output_t60_e[idx] ) ); // Q27 -> e4 (from other function). However, t60 is in Q26. This should be handled appropriately

        /* Limit exponent to approx +/-100 dB in case of incoherent value of delay_diff, to prevent overflow */
        exp_argument = min( exp_argument, 23.0f );
        exp_argument = max( exp_argument, -23.0f );
        pOutput_ene[idx] *= expf( exp_argument );
        // 23 in Q26
        tmp_flag = BASOP_Util_Cmp_Mant32Exp( L_deposit_h( exp_argument_fx ), exp_argument_e, 1543503872, 5 );
        IF( tmp_flag > 0 )
        {
            exp_argument_fx = 23552;
            move16();
            exp_argument_e = 5;
            move16();
        }

        tmp_flag = BASOP_Util_Cmp_Mant32Exp( L_deposit_h( exp_argument_fx ), exp_argument_e, 0, 31 );
        IF( tmp_flag < 0 )
        {
            tmp_flag = BASOP_Util_Cmp_Mant32Exp( L_deposit_h( negate( exp_argument_fx ) ), exp_argument_e, 1543503872, 5 );
            IF( tmp_flag < 0 )
            {
                exp_argument_fx = -23552;
                move16();
                exp_argument_e = 5;
                move16();
            }
        }

        Word16 tmp_exp;
        /* expf(exp_argument) -> pow(2, log2(e) * exp_argument) */
        tmp = mult( 23637, exp_argument_fx ); // exp_argument_e + 1
        tmp_exp = add( exp_argument_e, 1 );
        L_tmp = BASOP_util_Pow2( L_deposit_h( tmp ), tmp_exp, &pow_exp );
        L_tmp = Mpy_32_32( L_tmp, output_ene_fx[idx] );
        tmp_exp = add( pow_exp, output_ene_e[idx] );

        output_ene_fx[idx] = L_tmp;
        move32();
        output_ene_e[idx] = tmp_exp;
        move16();

    }
    ivas_reverb_set_energies( hHrtfStatistics->average_energy_l, hHrtfStatistics->average_energy_r, output_Fs, avg_pwr_left, avg_pwr_right );

    ivas_reverb_set_energies( hHrtfStatistics->average_energy_l, hHrtfStatistics->average_energy_r, output_Fs, avg_pwr_left_fx, avg_pwr_right_fx );

    for ( idx = 0; idx < CLDFB_NO_CHANNELS_MAX; idx++ )
    {
        pOutput_ene[idx] *= 0.5f * ( avg_pwr_left[idx] + avg_pwr_right[idx] ) * dmx_gain_2;
        Word32 tmp_ene;

        // For fixed-point porting -> energies are stored in Q31, but gain is calcualted in Q16. Final result must be in Q31.

        // Steps for this conversion:
        // Step 1 : add avg_pwr_left and avg_pwr_right (assuming same Q factor) and store in a temp variable -> use L_add : 32bit addition with saturation
        // Step 2 : scale the tmep variable by 0.5 -> can this be done by right shifting by 1?

        tmp_ene = ( L_add( avg_pwr_left_fx[idx], avg_pwr_right_fx[idx] ) ) >> 1; // Or Madd32_32()??

        // Step 3 : multiply temp variable by gain, with appropriate scaling for Q31 and Q16 multiplication
        tmp_ene = Mpy_32_32( tmp_ene, dmx_gain_2_fx ); // Mpy_32_32_ss multiplies two 32 bit numberrs in 64-bit with saturation or L_mult -> outputs word16
        // incomplete implentation

        // Step 4 : multiply step 3 result with output energy variable
        pOutput_ene[idx] = tmp_ene;


        /*floating-point
        pOutput_ene[idx] *= 0.5f * ( avg_pwr_left[idx] + avg_pwr_right[idx] ) * dmx_gain_2;*/
    }

    return IVAS_ERR_OK;
@@ -142,18 +216,16 @@ static void ivas_reverb_set_energies(
    const Word32 *avg_pwr_l,
    const Word32 *avg_pwr_r,
    const int32_t sampling_rate,
    float *avg_pwr_left,
    float *avg_pwr_right )
    Word32 *avg_pwr_left,
    Word32 *avg_pwr_right )
{
    int16_t freq_idx;
    float input_fc[FFT_SPECTRUM_SIZE];
#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
    Word32 input_fc_fx[FFT_SPECTRUM_SIZE];
    Word32 output_fc_fx[CLDFB_NO_CHANNELS_MAX];
    Word32 avg_pwr_left_fx[CLDFB_NO_CHANNELS_MAX];
    Word16 avg_pwr_left_e[CLDFB_NO_CHANNELS_MAX];
    Word32 avg_pwr_right_fx[CLDFB_NO_CHANNELS_MAX];
    Word16 avg_pwr_right_e[CLDFB_NO_CHANNELS_MAX];
    Word32 avg_pwr_left_fx[CLDFB_NO_CHANNELS_MAX];  // Q28
    Word32 avg_pwr_right_fx[CLDFB_NO_CHANNELS_MAX]; // Q28
    const Word16 cldfb_freq_halfstep = MAX_SAMPLING_RATE / ( 4 * CLDFB_NO_CHANNELS_MAX );
#endif

@@ -164,7 +236,7 @@ static void ivas_reverb_set_energies(
    {
        input_fc[freq_idx] = freq_idx * ( 0.5f * sampling_rate / (float) ( avg_pwr_len - 1 ) );
#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
        input_fc_fx[freq_idx] = input_fc[freq_idx] * ONE_IN_Q16;
        input_fc_fx[freq_idx] = (Word32) input_fc[freq_idx] * ONE_IN_Q16;
#endif
    }

@@ -200,7 +272,7 @@ static void ivas_reverb_set_energies(

#ifdef FIX_1741_REVERB_TIMES_Q_FORMAT
    ivas_reverb_interpolate_energies_fx( avg_pwr_len, input_fc_fx, avg_pwr_l, avg_pwr_r,
                                         CLDFB_NO_CHANNELS_MAX, output_fc_fx, avg_pwr_left_fx, avg_pwr_right_fx, avg_pwr_left_e, avg_pwr_right_e );
                                         CLDFB_NO_CHANNELS_MAX, output_fc_fx, avg_pwr_left_fx, avg_pwr_right_fx );
#else
    ivas_reverb_interpolate_acoustic_data_fx( FFT_SPECTRUM_SIZE, input_fc_fx, avg_pwr_l, avg_pwr_r,
                                              CLDFB_NO_CHANNELS_MAX, output_fc_fx, avg_pwr_left_fx, avg_pwr_right_fx, avg_pwr_left_e, avg_pwr_right_e );
@@ -208,8 +280,11 @@ static void ivas_reverb_set_energies(

    for ( int i = 0; i < 60; i++ )
    {
        avg_pwr_left[i] = (float) fabs( me2f( avg_pwr_left_fx[i], avg_pwr_left_e[i] ) );
        avg_pwr_right[i] = (float) fabs( me2f( avg_pwr_right_fx[i], avg_pwr_right_e[i] ) );
        // avg_pwr_left[i] = (float)avg_pwr_left_fx[i] / ONE_IN_Q28; //(float) fabs( me2f( avg_pwr_left_fx[i], avg_pwr_left_e[i] ) );
        // avg_pwr_right[i] = (float) avg_pwr_right_fx[i] / ONE_IN_Q28; // (float) fabs( me2f( avg_pwr_right_fx[i], avg_pwr_right_e[i] ) );

        avg_pwr_left[i] = avg_pwr_left_fx[i];
        avg_pwr_right[i] = avg_pwr_right_fx[i];
    }

#ifndef FIX_1741_REVERB_TIMES_Q_FORMAT