Commit eb7a1de6 authored by Jan Kiene's avatar Jan Kiene
Browse files

use version from main-pc for lib_rend/ivas_masa_merge_fx.c

parent 822202b5
Loading
Loading
Loading
Loading
Loading
+93 −86
Original line number Diff line number Diff line
@@ -39,7 +39,6 @@
#include "wmc_auto.h"
#include "ivas_prot_fx.h"

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

/*---------------------------------------------------------------------*
 * Local function prototypes
@@ -47,25 +46,9 @@

static void copy_masa_meta_tile_fx( MASA_DECODER_EXT_OUT_META_HANDLE outMeta, MASA_DECODER_EXT_OUT_META_HANDLE inMeta, const UWord8 sf, const UWord8 band );

static void full_stream_merge_fx(
    MASA_DECODER_EXT_OUT_META_HANDLE outMeta,                            /* o  : Merged metadata output                                                      */
    MASA_DECODER_EXT_OUT_META_HANDLE inMeta1,                            /* i  : Input metadata 1                                                            */
    Word32 inEne1_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i/o: TF-energy of input 1. after merge, contains the energy of the merged signal */
    Word16 inEne1_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],  /* i/o: TF-energy of input 1 Exponent                                               */
    MASA_DECODER_EXT_OUT_META_HANDLE inMeta2,                            /* i  : Input metadata 2                                                            */
    Word32 inEne2_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i  : TF-energy of input 2                                                        */
    Word16 inEne2_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]   /* i  : TF-energy of input 2 Exponent                                               */
);
static void full_stream_merge_fx( MASA_DECODER_EXT_OUT_META_HANDLE outMeta, MASA_DECODER_EXT_OUT_META_HANDLE inMeta1, Word32 inEne1[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], Word16 *inEne1_e, MASA_DECODER_EXT_OUT_META_HANDLE inMeta2, Word32 inEne2[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], Word16 *inEne2_e );

static void diffuse_meta_merge_1x1_fx(
    MASA_DECODER_EXT_OUT_META_HANDLE outMeta,                              /* o  : Merged metadata output        */
    MASA_DECODER_EXT_OUT_META_HANDLE inMeta,                               /* i  : Input metadata 1              */
    Word32 inEne_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],    /* i  : TF-energy of input 1          */
    Word16 inEne_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],     /* i  : TF-energy of input 1 Exponent */
    MASA_DECODER_EXT_OUT_META_HANDLE inMetaISM,                            /* i  : Input metadata 2              */
    Word32 inEneISM_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i  : TF-energy of input 2          */
    Word16 inEneISM_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]   /* i  : TF-energy of input 2 Exponent */
);
static void diffuse_meta_merge_1x1_fx( MASA_DECODER_EXT_OUT_META_HANDLE outMeta, MASA_DECODER_EXT_OUT_META_HANDLE inMeta, Word32 inEne_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], Word16 *inEne_e, MASA_DECODER_EXT_OUT_META_HANDLE inMetaISM, Word32 inEneISM_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], Word16 *inEneISM_e );


/*---------------------------------------------------------------------*
@@ -160,61 +143,55 @@ void copy_masa_descriptive_meta_fx(
void diffuse_meta_merge_1x1_fx(
    MASA_DECODER_EXT_OUT_META_HANDLE outMeta,                              /* o  : Merged metadata output                   */
    MASA_DECODER_EXT_OUT_META_HANDLE inMeta,                               /* i  : Input metadata 1                         */
    Word32 inEne_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],    /* i  : TF-energy of input 1          */
    Word16 inEne_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],     /* i  : TF-energy of input 1 Exponent */
    Word32 inEne_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],    /* i/o: TF-energy of input 1. energy after merge */
    Word16 *inEne_e,                                                       /* i  : TF-energy of input 1 Exponent            */
    MASA_DECODER_EXT_OUT_META_HANDLE inMetaISM,                            /* i  : Input metadata 2                         */
    Word32 inEneISM_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i  : TF-energy of input 2                     */
    Word16 inEneISM_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]   /* i  : TF-energy of input 2 Exponent */
    Word16 *inEneISM_e                                                     /* i  : TF-energy of input 2 Exponent            */
)
{
    Word8 sf, band;
    Word16 max_e, in1_e[MASA_FREQUENCY_BANDS], i;
    FOR( sf = 0; sf < MAX_PARAM_SPATIAL_SUBFRAMES; sf++ )
    {
        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_tmp, L_tmp1, L_tmp2;
            Word32 dir_ratio_ism_fx, 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();

            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 );
            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 ) ) */
            energyTimesRatio_e = add( inEne_e[sf], scale );

            IF( GT_16( inEne_e[sf], inEneISM_e[sf] ) )
            {
                total_nrg_fx = L_add( L_shr( inEne_fx[sf][band], 1 ), L_shr( inEneISM_fx[sf][band], add( sub( inEne_e[sf], inEneISM_e[sf] ), 1 ) ) ); /* Q(30 - inEne_e[sf]) */
                total_nrg_e = add( inEne_e[sf], 1 );
            }
            ELSE
            {
                total_nrg_fx = L_add( L_shr( inEneISM_fx[sf][band], 1 ), L_shr( inEne_fx[sf][band], add( sub( inEneISM_e[sf], inEne_e[sf] ), 1 ) ) ); /* Q(30 - inEneISM_e[sf]) */
                total_nrg_e = add( inEneISM_e[sf], 1 );
            }
            /* target is original MASA diffuseness */
            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 );
            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 ) ) */
            total_diff_nrg_e = add( inEne_e[sf], scale );

            /* criterion is mean of ISM ratio and new ratio */
            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();
            dir_ratio_ism_fx = L_deposit_h( BASOP_Util_Divide1616_Scale( inMetaISM->directToTotalRatio[0][sf][band], UINT8_MAX, &dir_ratio_ism_e ) );
            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 ) );
            IF( L_tmp1 == 0 )
            {
                scale = 0;
                move16();
                L_tmp2 = MAX_32;
                move32();
            }
            ELSE
            {
                L_tmp2 = BASOP_Util_Add_Mant32Exp( ONE_IN_Q31, 0, L_negate( L_tmp1 ), scale, &scale );
            }
            L_tmp2 = L_sub( L_shl( 1, scale ), L_tmp1 );

            L_tmp1 = BASOP_Util_Add_Mant32Exp( dir_ratio_ism_fx, dir_ratio_ism_e, L_tmp2, scale, &tmp );
            L_tmp1 = L_shr( L_tmp1, 1 );
            energyTimesRatioISM_fx = Mpy_32_32( L_tmp1, inEneISM_fx[sf][band] ); /* Q( 31 - ( tmp + inEneISM_e[sf] ) ) */
            energyTimesRatioISM_e = add( tmp, inEneISM_e[sf][band] );
            energyTimesRatioISM_e = add( tmp, inEneISM_e[sf] );

            IF( ( BASOP_Util_Cmp_Mant32Exp( energyTimesRatioISM_fx, energyTimesRatioISM_e, energyTimesRatio_fx, energyTimesRatio_e ) > 0 ) )
            {
@@ -232,16 +209,13 @@ void diffuse_meta_merge_1x1_fx(

                tmp = BASOP_Util_Divide3232_Scale( total_diff_nrg_fx, L_add( EPSILON_FX, total_nrg_fx ), &scale );
                scale = add( scale, sub( total_diff_nrg_e, total_nrg_e ) );
                Word32 temp32 = L_shr( L_deposit_h( tmp ), sub( sub( 31, scale ), Q30 ) ); // Q30
                scale = 1;
                move16();
                dir_nrg_ratio_fx = L_sub( ONE_IN_Q30, temp32 );
                dir_nrg_ratio_fx = L_sub( L_shl( 1, scale ), L_deposit_h( tmp ) );
                dir_nrg_ratio_e = scale;
                move16();

                new_dir_ratio_fx = dir_ratio_ism_fx;
                new_dir_ratio_fx = dir_nrg_ratio_fx;
                move32();
                new_dir_ratio_e = dir_ratio_ism_e;
                new_dir_ratio_e = dir_nrg_ratio_e;
                move16();
                tmp = BASOP_Util_Cmp_Mant32Exp( dir_nrg_ratio_fx, dir_nrg_ratio_e, dir_ratio_ism_fx, dir_ratio_ism_e );
                IF( tmp <= 0 )
@@ -252,7 +226,7 @@ void diffuse_meta_merge_1x1_fx(
                    move16();
                }

                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 ) ) );
                outMeta->directToTotalRatio[0][sf][band] = (UWord8) imult1616( extract_l( L_shr( new_dir_ratio_fx, sub( 31, new_dir_ratio_e ) ) ), UINT8_MAX );
                move16();
                IF( GT_16( sub( 31, new_dir_ratio_e ), Q30 ) )
                {
@@ -265,7 +239,7 @@ void diffuse_meta_merge_1x1_fx(
                {
                    new_diff_ratio_fx = L_sub( L_shl( 1, sub( 31, new_dir_ratio_e ) ), new_dir_ratio_fx ); /* Q(31 - new_dir_ratiio_e) */
                }
                outMeta->diffuseToTotalRatio[sf][band] = (UWord8) extract_l( L_shr( new_diff_ratio_fx, sub( sub( 31, new_dir_ratio_e ), 8 ) ) );
                outMeta->diffuseToTotalRatio[sf][band] = (UWord8) imult1616( extract_l( L_shr( new_diff_ratio_fx, new_dir_ratio_e ) ), UINT8_MAX );
                move16();
            }
            ELSE
@@ -290,9 +264,29 @@ void diffuse_meta_merge_1x1_fx(
            outMeta->spreadCoherence[1][sf][band] = 0u;
            move16();

            inEne_fx[sf][band] = BASOP_Util_Add_Mant32Exp( inEne_fx[sf][band], inEne_e[sf][band], inEneISM_fx[sf][band], inEneISM_e[sf][band], &inEne_e[sf][band] ); /* Update energy for subsequent mergings */
            inEne_fx[sf][band] = BASOP_Util_Add_Mant32Exp( inEne_fx[sf][band], inEne_e[sf], inEneISM_fx[sf][band], inEneISM_e[sf], &in1_e[band] ); /* Update energy for subsequent mergings */
            move16();
        }

        max_e = in1_e[0];
        move16();
        FOR( i = 1; i < MASA_FREQUENCY_BANDS; i++ )
        {
            if ( LT_16( max_e, in1_e[i] ) )
            {
                max_e = in1_e[i];
                move16();
            }
        }

        FOR( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
        {
            inEne_fx[sf][i] = L_shr( inEne_fx[sf][i], sub( max_e, in1_e[i] ) ); /* Q(31 - max_e) */
            move32();
        }

        inEne_e[sf] = max_e;
        move16();
    }

    /* Set descriptive meta for mixed format */
@@ -324,15 +318,16 @@ void full_stream_merge_fx(
    MASA_DECODER_EXT_OUT_META_HANDLE outMeta,                            /* o  : Merged metadata output                                                      */
    MASA_DECODER_EXT_OUT_META_HANDLE inMeta1,                            /* i  : Input metadata 1                                                            */
    Word32 inEne1_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i/o: TF-energy of input 1. after merge, contains the energy of the merged signal */
    Word16 inEne1_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS],  /* i/o: TF-energy of input 1 Exponent                                               */
    Word16 *inEne1_e,                                                    /* i/o: TF-energy of input 1 Exponent                                               */
    MASA_DECODER_EXT_OUT_META_HANDLE inMeta2,                            /* i  : Input metadata 2                                                            */
    Word32 inEne2_fx[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS], /* i  : TF-energy of input 2                                                        */
    Word16 inEne2_e[MAX_PARAM_SPATIAL_SUBFRAMES][MASA_FREQUENCY_BANDS]   /* i  : TF-energy of input 2 Exponent                                               */
    Word16 *inEne2_e                                                     /* i  : TF-energy of input 2 Exponent                                               */
)
{
    UWord8 n_dirs_1, n_dirs_2;
    UWord8 sf, band;
    Word16 scale, dir_nrg_1_e, dir_nrg_2_e;
    Word16 scale, tmp, dir_nrg_1_e, dir_nrg_2_e, max_e, i;
    Word16 in1_e[MASA_FREQUENCY_BANDS];
    Word32 dir_nrg_1_fx, dir_nrg_2_fx, L_tmp;

    /* full stream select based on total direct energy */
@@ -343,35 +338,27 @@ void full_stream_merge_fx(
    {
        FOR( band = 0; band < MASA_FREQUENCY_BANDS; band++ )
        {
            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( 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] ) ) */
            dir_nrg_1_e = add( scale, inEne1_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] );
            tmp = BASOP_Util_Divide1616_Scale( inMeta1->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] ) ) */
            dir_nrg_2_e = add( scale, inEne2_e[sf] );

            IF( EQ_16( n_dirs_1, 2 ) )
            {
                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] );
                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] ) ) */
                scale = add( scale, inEne1_e[sf] );
                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 ) )
            {
                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] );
                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] ) ) */
                scale = add( scale, inEne2_e[sf] );
                dir_nrg_2_fx = BASOP_Util_Add_Mant32Exp( L_tmp, scale, dir_nrg_2_fx, dir_nrg_2_e, &dir_nrg_2_e );
            }

@@ -384,9 +371,29 @@ void full_stream_merge_fx(
                copy_masa_meta_tile_fx( outMeta, inMeta2, sf, band );
            }

            inEne1_fx[sf][band] = BASOP_Util_Add_Mant32Exp( inEne1_fx[sf][band], inEne1_e[sf][band], inEne2_fx[sf][band], inEne2_e[sf][band], &inEne1_e[sf][band] );
            inEne1_fx[sf][band] = BASOP_Util_Add_Mant32Exp( inEne1_fx[sf][band], inEne1_e[sf], inEne2_fx[sf][band], inEne2_e[sf], &in1_e[band] );
            move32();
        }

        max_e = in1_e[0];
        move16();
        FOR( i = 1; i < MASA_FREQUENCY_BANDS; i++ )
        {
            if ( LT_16( max_e, in1_e[i] ) )
            {
                max_e = in1_e[i];
                move16();
            }
        }

        FOR( i = 0; i < MASA_FREQUENCY_BANDS; i++ )
        {
            inEne1_fx[sf][i] = L_shr( inEne1_fx[sf][i], sub( max_e, in1_e[i] ) ); /* Q( 31 - max_e ) */
            move32();
        }

        inEne1_e[sf] = max_e;
        move16();
    }

    /* Set descriptive meta for mixed format */