Commit 1f9fadfc authored by Dominik Weckbecker's avatar Dominik Weckbecker 💬
Browse files

do rescaling for each conversion individually

parent b958e6f0
Loading
Loading
Loading
Loading
Loading
+19 −28
Original line number Diff line number Diff line
@@ -55,18 +55,8 @@
#define INV_SQRT_3_OVER_5_   1.2909944f
#define INV_SQRT_45_OVER_32_ 0.8432740f

#define INV_SQRT_FOUR_PI_ 0.2820947f /* 1/sqrt(4/pi) */

#define AMBI_MAX_CHANNELS 16

/*
   For conversions to SN3D we want to avoid clipping the decoder input.
   The GLOBAL_SCALING_FACTOR ensures that the amplitude is always attenuated.
   The inverse INV_GLOBAL_SCALING_FACTOR restores the original amplitude when
   converting back to the input convention after the decoding.
*/
#define GLOBAL_SCALING_FACTOR     0.7071067f
#define INV_GLOBAL_SCALING_FACTOR 0.7071067f

/* --------------------------------
   normalization conversion tables
@@ -82,6 +72,8 @@ static const float N3D_SN3D[AMBI_MAX_CHANNELS] = { 1.0f,
                                                   INV_SQRT_5_, INV_SQRT_5_, INV_SQRT_5_, INV_SQRT_5_, INV_SQRT_5_,
                                                   INV_SQRT_7_, INV_SQRT_7_, INV_SQRT_7_, INV_SQRT_7_, INV_SQRT_7_, INV_SQRT_7_, INV_SQRT_7_ };

static const float N3D_SCALING_FACTOR = 1.0f;


static const float SN3D_MAXN[AMBI_MAX_CHANNELS] = { 1.0f,
                                                    1.0f, 1.0f, 1.0f,
@@ -93,6 +85,8 @@ static const float MAXN_SN3D[AMBI_MAX_CHANNELS] = { 1.0f,
                                                    INV_TWO_OVER_SQRT_3_, INV_TWO_OVER_SQRT_3_, 1.0f, INV_TWO_OVER_SQRT_3_, INV_TWO_OVER_SQRT_3_,
                                                    INV_SQRT_8_OVER_5_, INV_SQRT_3_OVER_5_, INV_SQRT_45_OVER_32_, 1.0f, INV_SQRT_45_OVER_32_, INV_SQRT_3_OVER_5_, INV_SQRT_8_OVER_5_ };

static const float MAXN_SCALING_FACTOR = SQRT_3_OVER_5_;


static const float SN3D_FM[AMBI_MAX_CHANNELS] = { INV_SQRT_2,
                                                  1.0f, 1.0f, 1.0f,
@@ -104,6 +98,9 @@ static const float FM_SN3D[AMBI_MAX_CHANNELS] = { SQRT_2_,
                                                  INV_TWO_OVER_SQRT_3_, INV_TWO_OVER_SQRT_3_, 1.0f, INV_TWO_OVER_SQRT_3_, INV_TWO_OVER_SQRT_3_,
                                                  INV_SQRT_8_OVER_5_, INV_SQRT_3_OVER_5_, INV_SQRT_45_OVER_32_, 1.0f, INV_SQRT_45_OVER_32_, INV_SQRT_3_OVER_5_, INV_SQRT_8_OVER_5_ };

static const float FM_SCALING_FACTOR = INV_SQRT_2;


/* ----------------------------
    channel re-ordering tables
   ---------------------------- */
@@ -199,6 +196,7 @@ int16_t renormalize_channels( float *in[], float *out[], int16_t order, AMBI_CHA
    int16_t n_chan = ( order + 1 ) * ( order + 1 );
    int16_t i_chan, i;
    const float *conversion_table = 0;
    float sf = 1.0f;

    /* conversion factors are aplpied on the channels assuming that they are still/already in ACN order */

@@ -207,14 +205,17 @@ int16_t renormalize_channels( float *in[], float *out[], int16_t order, AMBI_CHA
        if ( out_format == AMBI_NORM_N3D )
        {
            conversion_table = SN3D_N3D;
            sf = 1.f / N3D_SCALING_FACTOR;
        }
        else if ( out_format == AMBI_NORM_MAXN )
        {
            conversion_table = SN3D_MAXN;
            sf = 1.f / MAXN_SCALING_FACTOR;
        }
        else if ( out_format == AMBI_NORM_FM )
        {
            conversion_table = SN3D_FM;
            sf = 1.f / FM_SCALING_FACTOR;
        }
        else
        {
@@ -226,14 +227,17 @@ int16_t renormalize_channels( float *in[], float *out[], int16_t order, AMBI_CHA
        if ( in_format == AMBI_NORM_N3D )
        {
            conversion_table = N3D_SN3D;
            sf = N3D_SCALING_FACTOR;
        }
        else if ( in_format == AMBI_NORM_MAXN )
        {
            conversion_table = MAXN_SN3D;
            sf = MAXN_SCALING_FACTOR;
        }
        else if ( in_format == AMBI_NORM_FM )
        {
            conversion_table = FM_SN3D;
            sf = FM_SCALING_FACTOR;
        }
        else
        {
@@ -245,26 +249,13 @@ int16_t renormalize_channels( float *in[], float *out[], int16_t order, AMBI_CHA
        assert( 0 && "Conversion only supported to and from ACN-SN3D" );
    }

    if ( out_format == AMBI_NORM_SN3D )
    {
        for ( i_chan = 0; i_chan < n_chan; i_chan++ )
        {
            float conversion_factor = conversion_table[i_chan];
            for ( i = 0; i < L_FRAME48k; i++ )
            {
                out[i_chan][i] = GLOBAL_SCALING_FACTOR * in[i_chan][i] * conversion_factor;
            }
        }
    }
    else
    {

    for ( i_chan = 0; i_chan < n_chan; i_chan++ )
    {
        float conversion_factor = conversion_table[i_chan];
        for ( i = 0; i < L_FRAME48k; i++ )
        {
                out[i_chan][i] = INV_GLOBAL_SCALING_FACTOR * in[i_chan][i] * conversion_factor;
            }
            out[i_chan][i] = sf * in[i_chan][i] * conversion_factor;
        }
    }