Commit 25242c01 authored by Sandesh Venkatesh's avatar Sandesh Venkatesh
Browse files

Fix to correct the rounding of directToTotalRatio

This change improves the precision as well as corrects the rounding of directToTotalRatio.
Optimization: This MR removes 7 instances of BASOP_Util_Divide1616_Scale used in 2 functions: diffuse_meta_merge_1x1_fx and full_stream_merge_fx
parent 94b0164c
Loading
Loading
Loading
Loading
Loading
+31 −16
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@
#include "wmc_auto.h"
#include "ivas_prot_fx.h"

#define INV_UINT8_MAX 8421505 /* 1/UINT8_MAX in Q31 */

/*---------------------------------------------------------------------*
 * Local function prototypes
@@ -172,23 +173,29 @@ void diffuse_meta_merge_1x1_fx(
        FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
        {
            Word32 energyTimesRatio_fx, energyTimesRatioISM_fx, total_diff_nrg_fx, dir_nrg_ratio_fx, total_nrg_fx = 0;
            Word32 dir_ratio_ism_fx, L_tmp1, L_tmp2;
            Word32 dir_ratio_ism_fx, L_tmp, L_tmp1, L_tmp2;
            Word16 scale, energyTimesRatio_e, tmp, total_nrg_e = 0, total_diff_nrg_e, dir_ratio_ism_e, energyTimesRatioISM_e, dir_nrg_ratio_e;
            move32();
            move16();

            tmp = BASOP_Util_Divide1616_Scale( inMeta->directToTotalRatio[0][sf][band], UINT8_MAX, &scale );
            energyTimesRatio_fx = Mpy_32_16_r( inEne_fx[sf][band], tmp ); /* Q( 31 - ( nEne_e[sf] + scale ) ) */
            L_tmp = Mpy_32_16_1( INV_UINT8_MAX, inMeta->directToTotalRatio[0][sf][band] ); // Q31 - 15 => Q16
            scale = 15;
            move16();
            energyTimesRatio_fx = Mpy_32_32_r( inEne_fx[sf][band], L_tmp ); /* Q( 31 - ( nEne_e[sf] + scale ) ) */
            energyTimesRatio_e = add( inEne_e[sf][band], scale );
            total_nrg_fx = BASOP_Util_Add_Mant32Exp( inEne_fx[sf][band], inEne_e[sf][band], inEneISM_fx[sf][band], inEneISM_e[sf][band], &total_nrg_e );

            /* target is original MASA diffuseness */
            tmp = BASOP_Util_Divide1616_Scale( inMeta->diffuseToTotalRatio[sf][band], UINT8_MAX, &scale );
            total_diff_nrg_fx = Mpy_32_16_r( inEne_fx[sf][band], tmp ); /* Q( 31 - ( nEne_e[sf] + scale ) ) */
            L_tmp = Mpy_32_16_1( INV_UINT8_MAX, inMeta->diffuseToTotalRatio[sf][band] ); // Q31 - 15 => Q16
            scale = 15;
            move16();
            total_diff_nrg_fx = Mpy_32_32_r( inEne_fx[sf][band], L_tmp ); /* Q( 31 - ( nEne_e[sf] + scale ) ) */
            total_diff_nrg_e = add( inEne_e[sf][band], scale );

            /* criterion is mean of ISM ratio and new ratio */
            dir_ratio_ism_fx = L_deposit_h( BASOP_Util_Divide1616_Scale( inMetaISM->directToTotalRatio[0][sf][band], UINT8_MAX, &dir_ratio_ism_e ) );
            dir_ratio_ism_fx = Mpy_32_16_1( INV_UINT8_MAX, inMetaISM->directToTotalRatio[0][sf][band] ); // Q31 - 15 => Q16
            dir_ratio_ism_e = 15;
            move16();
            tmp = BASOP_Util_Divide3232_Scale( total_diff_nrg_fx, L_add( total_nrg_fx, EPSILON_FX ), &scale );
            L_tmp1 = L_deposit_h( tmp ); /* Q( 31 - ( scale + total_nrg_e - total_diff_nrg_e ) ) */
            scale = add( scale, sub( total_diff_nrg_e, total_nrg_e ) );
@@ -245,7 +252,7 @@ void diffuse_meta_merge_1x1_fx(
                    move16();
                }

                outMeta->directToTotalRatio[0][sf][band] = (UWord8) extract_l( L_shr( new_dir_ratio_fx, sub( sub( 31, new_dir_ratio_e ), 8 ) ) );
                outMeta->directToTotalRatio[0][sf][band] = (UWord8) extract_l( L_shr( Mpy_32_16_1( new_dir_ratio_fx, UINT8_MAX ) /* (31 - new_dir_ratio_e) - 15*/, sub( 31 - 15, new_dir_ratio_e ) ) );
                move16();
                IF( GT_16( sub( 31, new_dir_ratio_e ), Q30 ) )
                {
@@ -325,7 +332,7 @@ void full_stream_merge_fx(
{
    UWord8 n_dirs_1, n_dirs_2;
    UWord8 sf, band;
    Word16 scale, tmp, dir_nrg_1_e, dir_nrg_2_e;
    Word16 scale, dir_nrg_1_e, dir_nrg_2_e;
    Word32 dir_nrg_1_fx, dir_nrg_2_fx, L_tmp;

    /* full stream select based on total direct energy */
@@ -336,26 +343,34 @@ void full_stream_merge_fx(
    {
        FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
        {
            tmp = BASOP_Util_Divide1616_Scale( inMeta1->directToTotalRatio[0][sf][band], UINT8_MAX, &scale );
            dir_nrg_1_fx = Mpy_32_32( L_deposit_h( tmp ), inEne1_fx[sf][band] ); /* Q( 31 - ( scale + inEne1_e[sf] ) ) */
            L_tmp = Mpy_32_16_1( INV_UINT8_MAX, inMeta1->directToTotalRatio[0][sf][band] ); // Q31 - 15 => Q16
            scale = 15;
            move16();
            dir_nrg_1_fx = Mpy_32_32( L_tmp, inEne1_fx[sf][band] ); /* Q( 31 - ( scale + inEne1_e[sf] ) ) */
            dir_nrg_1_e = add( scale, inEne1_e[sf][band] );

            tmp = BASOP_Util_Divide1616_Scale( inMeta2->directToTotalRatio[0][sf][band], UINT8_MAX, &scale );
            dir_nrg_2_fx = Mpy_32_32( L_deposit_h( tmp ), inEne2_fx[sf][band] ); /* Q( 31 - ( scale + inEne2_e[sf] ) ) */
            L_tmp = Mpy_32_16_1( INV_UINT8_MAX, inMeta2->directToTotalRatio[0][sf][band] ); // Q31 - 15 => Q16
            scale = 15;
            move16();
            dir_nrg_2_fx = Mpy_32_32( L_tmp, inEne2_fx[sf][band] ); /* Q( 31 - ( scale + inEne2_e[sf] ) ) */
            dir_nrg_2_e = add( scale, inEne2_e[sf][band] );

            IF( EQ_16( n_dirs_1, 2 ) )
            {
                tmp = BASOP_Util_Divide1616_Scale( inMeta1->directToTotalRatio[1][sf][band], UINT8_MAX, &scale );
                L_tmp = Mpy_32_32( L_deposit_h( tmp ), inEne1_fx[sf][band] ); /* Q( 31 - ( scale + inEne1_e[sf] ) ) */
                L_tmp = Mpy_32_16_1( INV_UINT8_MAX, inMeta1->directToTotalRatio[1][sf][band] ); // Q31 - 15 => Q16
                scale = 15;
                move16();
                L_tmp = Mpy_32_32( L_tmp, inEne1_fx[sf][band] ); /* Q( 31 - ( scale + inEne1_e[sf] ) ) */
                scale = add( scale, inEne1_e[sf][band] );
                dir_nrg_1_fx = BASOP_Util_Add_Mant32Exp( L_tmp, scale, dir_nrg_1_fx, dir_nrg_1_e, &dir_nrg_1_e );
            }

            IF( EQ_16( n_dirs_2, 2 ) )
            {
                tmp = BASOP_Util_Divide1616_Scale( inMeta2->directToTotalRatio[1][sf][band], UINT8_MAX, &scale );
                L_tmp = Mpy_32_32( L_deposit_h( tmp ), inEne2_fx[sf][band] ); /* Q( 31 - ( scale + inEne2_e[sf] ) ) */
                L_tmp = Mpy_32_16_1( INV_UINT8_MAX, inMeta2->directToTotalRatio[1][sf][band] ); // Q31 - 15 => Q16
                scale = 15;
                move16();
                L_tmp = Mpy_32_32( L_tmp, inEne2_fx[sf][band] ); /* Q( 31 - ( scale + inEne2_e[sf] ) ) */
                scale = add( scale, inEne2_e[sf][band] );
                dir_nrg_2_fx = BASOP_Util_Add_Mant32Exp( L_tmp, scale, dir_nrg_2_fx, dir_nrg_2_e, &dir_nrg_2_e );
            }