Commit 8e418e5c authored by Sandesh Venkatesh's avatar Sandesh Venkatesh
Browse files

Added ivas_sns_com_fx.c and ivas_stereo_eclvq_com_fx.c

parent 72f1f4ce
Loading
Loading
Loading
Loading
Loading
+314 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2023 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/

#include <stdint.h>
#include "options.h"
#include "cnst.h"
#include "prot.h"
#include "prot_fx1.h"
#include "prot_fx2.h"
#include "ivas_prot.h"
#include "rom_com.h"
#include "ivas_rom_com.h"
#include <math.h>
#include <assert.h>
#include "wmc_auto.h"


/*-------------------------------------------------------------------
 * sns_compute_scf_fx()
 *
 *
 *-------------------------------------------------------------------*/

void sns_compute_scf_fx(
    Word32 spectrum[],
    const PsychoacousticParameters *pPsychParams,
    const Word16 L_frame,
    Word32 *scf )
{
    Word16 i, n, k;
    Word32 x[FDNS_NPTS], xs[FDNS_NPTS], sum, mean, xl4[SNS_NPTS], nf, xl[FDNS_NPTS];
    Word32 L_tmp;
    const Word16 *pow_tilt;
    const UWord8 nBands = pPsychParams->nBands;
    const UWord8 *bandLengths = pPsychParams->bandLengths;
    Word8 bw = 0;

    const Word16 w_0 = 2730; // (1.0f / 12.0f) in Q15
    const Word16 w_1 = 5461; // (2.0f / 12.0f) in Q15
    const Word16 w_2 = 8192; // 0.25f ( 3.0f / 12.0f ) in Q15
    const Word16 w_3 = w_2;
    const Word16 w_4 = w_1;
    const Word16 w_5 = w_0;

    assert( nBands == FDNS_NPTS );

    set32_fx( x, 0, FDNS_NPTS );

    IF( bandLengths == NULL )
    {
        bw = (Word8) shr( L_frame, 6 );
        /* Energy per band */
        k = 0;
        FOR( i = 0; i < nBands; ++i )
        {
            x[i] = 0;
            FOR( n = 0; n < bw; ( ++n, ++k ) )
            {
                x[i] = L_add( x[i], spectrum[k] );
            }
            x[i] /= bw;
        }
    }
    ELSE
    {
        /* Energy per band */
        k = 0;
        FOR( i = 0; i < nBands; ++i )
        {
            x[i] = 0;
            FOR( n = 0; n < bandLengths[i]; ( ++n, ++k ) )
            {
                x[i] = L_add( x[i], spectrum[k] );
            }
            x[i] /= bandLengths[i];
        }
    }

    /* Smoothing */
    xs[0] = L_add( Mpy_32_16_1( x[0], 24576 /* 0.75 in Q15 */ ), Mpy_32_16_1( x[1], 8192 /* 0.25 in Q15 */ ) );

    FOR( i = 1; i < FDNS_NPTS - 1; i++ )
    {
        xs[i] = L_add( L_add( Mpy_32_16_1( x[i], 16384 /* 0.5 in Q15 */ ), Mpy_32_16_1( x[i - 1], 8192 /* 0.25 in Q15 */ ) ), Mpy_32_16_1( x[i + 1], 8192 /* 0.25 in Q15 */ ) );
    }

    xs[FDNS_NPTS - 1] = L_add( Mpy_32_16_1( x[FDNS_NPTS - 1], 24576 /* 0.75 in Q15 */ ), Mpy_32_16_1( x[FDNS_NPTS - 2], 8192 /* 0.25 in Q15 */ ) );

    /* Pre-emphasis */
    SWITCH( L_frame )
    {
        case L_FRAME16k:
            pow_tilt = pow_tilt_16k;
            BREAK;
        case L_FRAME25_6k:
            pow_tilt = pow_tilt_25_6k;
            BREAK;
        case L_FRAME32k:
            pow_tilt = pow_tilt_32k;
            BREAK;
        default:
            pow_tilt = NULL;
            assert( !"illegal frame length in sns_compute_scf_fx" );
    }

    FOR( i = 0; i < FDNS_NPTS; i++ )
    {
        xs[i] = Mpy_32_16_1( xs[i], pow_tilt[i] );
        xs[i] = L_shl( xs[i], Q8 ); // xs => Q12
    }

    /* Noise floor at -40dB */
    sum = sum32_fx( xs, FDNS_NPTS );
    mean = L_shr( sum, 6 );

    nf = Mpy_32_16_1( mean, 3 ); // 3 => powf( 10.0f, -4.0f ) in Q15
    nf = L_max( nf, 0 );         // 0 => powf( 2.0f, -32.0f ) in Q15


    FOR( i = 0; i < FDNS_NPTS; i++ )
    {
        if ( LT_32( xs[i], nf ) )
        {
            xs[i] = nf;
        }
    }

    /* Log-domain */
    FOR( i = 0; i < FDNS_NPTS; i++ )
    {
        Word16 e_tmp = norm_l( xs[i] );
        Word16 f_tmp = Log2_norm_lc( L_shl( xs[i], e_tmp ) );
        e_tmp = sub( sub( 30, e_tmp ), 12 );
        /* Note: Mpy_32_16 is used temporarily for this computation, It needs to be replaced with appropriate BASOP. */
        xl[i] = Mpy_32_16( e_tmp, f_tmp, 16384 ); /* Q16 */
    }

    /* Downsampling */
    L_tmp = L_deposit_l( 0 );
    L_tmp = Madd_32_16( L_tmp, xl[0], w_0 );
    L_tmp = Madd_32_16( L_tmp, xl[0], w_1 );
    L_tmp = Madd_32_16( L_tmp, xl[1], w_2 );
    L_tmp = Madd_32_16( L_tmp, xl[2], w_3 );
    L_tmp = Madd_32_16( L_tmp, xl[3], w_4 );
    L_tmp = Madd_32_16( L_tmp, xl[4], w_5 );
    xl4[0] = L_tmp;

    FOR( n = 1; n < SNS_NPTS - 1; n++ )
    {
        int16_t n4 = 4 * n;

        L_tmp = L_deposit_l( 0 );
        L_tmp = Madd_32_16( L_tmp, xl[n4 - 1], w_0 );
        L_tmp = Madd_32_16( L_tmp, xl[n4], w_1 );
        L_tmp = Madd_32_16( L_tmp, xl[n4 + 1], w_2 );
        L_tmp = Madd_32_16( L_tmp, xl[n4 + 2], w_3 );
        L_tmp = Madd_32_16( L_tmp, xl[n4 + 3], w_4 );
        L_tmp = Madd_32_16( L_tmp, xl[n4 + 4], w_5 );
        xl4[n] = L_tmp;
    }

    L_tmp = L_deposit_l( 0 );
    L_tmp = Madd_32_16( L_tmp, xl[FDNS_NPTS - 5], w_0 );
    L_tmp = Madd_32_16( L_tmp, xl[FDNS_NPTS - 4], w_1 );
    L_tmp = Madd_32_16( L_tmp, xl[FDNS_NPTS - 3], w_2 );
    L_tmp = Madd_32_16( L_tmp, xl[FDNS_NPTS - 2], w_3 );
    L_tmp = Madd_32_16( L_tmp, xl[FDNS_NPTS - 1], w_4 );
    L_tmp = Madd_32_16( L_tmp, xl[FDNS_NPTS - 1], w_5 );
    xl4[SNS_NPTS - 1] = L_tmp;

    /* Remove mean and scaling */
    sum = sum32_fx( xl4, SNS_NPTS );
    mean = L_shr( sum, 4 );

    FOR( i = 0; i < SNS_NPTS; i++ )
    {
        scf[i] = Mpy_32_16_1( L_sub( xl4[i], mean ), 27853 /* 0.85 in in Q15 */ );
    }

    return;
}

/*-------------------------------------------------------------------
 * sns_interpolate_scalefactors_fx()
 *
 *
 *-------------------------------------------------------------------*/

void sns_interpolate_scalefactors_fx(
    Word32 *scf_int,    /* o  : interpolated scalefactors for spectrum shaping*/
    const Word32 *scf,  /* i  : sns scalefactors as derived from the signal or read from the bitstream */
    Word16 encoder_side /* i  : flag, if scalefactors have to be inverted */
)
{
    Word16 n;
    Word32 L_tmp;
    Word16 exp;

    /* Interpolation */
    scf_int[0] = scf[0];
    scf_int[1] = scf[0];

    FOR( n = 0; n <= M - 2; n++ )
    {
        scf_int[n * 4 + 2] = L_add( scf[n], Mpy_32_16_1( L_sub( scf[n + 1], scf[n] ), 4096 ) );  /* 4096  -> 1/8 in Q15 */
        scf_int[n * 4 + 3] = L_add( scf[n], Mpy_32_16_1( L_sub( scf[n + 1], scf[n] ), 12288 ) ); /* 12288 -> 3/8 in Q15 */
        scf_int[n * 4 + 4] = L_add( scf[n], Mpy_32_16_1( L_sub( scf[n + 1], scf[n] ), 20480 ) ); /* 20480 -> 5/8 in Q15 */
        scf_int[n * 4 + 5] = L_add( scf[n], Mpy_32_16_1( L_sub( scf[n + 1], scf[n] ), 28672 ) ); /* 28672 -> 7/8 in Q15 */
    }

    scf_int[FDNS_NPTS - 2] = L_add( scf[M - 1], Mpy_32_16_1( L_sub( scf[M - 1], scf[M - 2] ), 4096 ) );
    scf_int[FDNS_NPTS - 1] = L_add( scf[M - 1], Mpy_32_16_1( L_sub( scf[M - 1], scf[M - 2] ), 12288 ) );

    /* Inversion at encoder-side */
    IF( encoder_side == ENC )
    {
        FOR( n = 0; n < FDNS_NPTS; n++ )
        {
            scf_int[n] = L_negate( scf_int[n] );
        }
    }

    /* Linear domain */
    FOR( n = 0; n < FDNS_NPTS; n++ )
    {
        L_tmp = BASOP_util_Pow2( scf_int[n], Q15, &exp );
        exp = sub( 15, exp );
        scf_int[n] = L_shr( L_tmp, exp );
    }

    return;
}


/*-------------------------------------------------------------------
 * sns_shape_spectrum_fx()
 *
 *
 *-------------------------------------------------------------------*/

void sns_shape_spectrum_fx(
    Word32 spectrum[],                            /* i/o: spectrum to be shaped                                     */
    const PsychoacousticParameters *pPsychParams, /* i  : psychoacoustic parameters used to get the frequency bands */
    const Word32 *scf_int,                        /* i  : already interpolated SNS scalefactors                     */
    const Word16 L_frame                          /* i  : frame length                                              */
)
{
    Word16 i, n, k, bw;
    Word64 L64_tmp;
    const UWord8 nBands = pPsychParams->nBands;
    const UWord8 *bandLengths = pPsychParams->bandLengths;

    IF( bandLengths == NULL )
    {
        bw = L_frame / nBands;

        /* Shape spectrum */
        k = 0;
        FOR( i = 0; i < nBands; ++i )
        {
            FOR( n = 0; n < bw; ( ++n, ++k ) )
            {
                L64_tmp = W_mult_32_32( spectrum[k], scf_int[i] ); // Q32
                L64_tmp = W_shr( L64_tmp, 26 );                    // Q6
                spectrum[k] = W_sat_l( L64_tmp );                  // Q6
            }
        }
    }
    ELSE
    {
        /* Shape spectrum */
        k = 0;
        FOR( i = 0; i < nBands; ++i )
        {
            FOR( n = 0; n < bandLengths[i]; ( ++n, ++k ) )
            {
                L64_tmp = W_mult_32_32( spectrum[k], scf_int[i] ); // Q32
                L64_tmp = W_shr( L64_tmp, 26 );                    // Q6
                spectrum[k] = W_sat_l( L64_tmp );                  // Q6
            }
        }
    }

    return;
}
+109 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2023 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/

#include <stdint.h>
#include "options.h"
#include <math.h>
#include "ivas_prot.h"
#include "ivas_prot_fx.h"
#include "ivas_cnst.h"
#include <assert.h>
#include "prot.h"
#include "wmc_auto.h"

/*---------------------------------------------------------------
 * ECSQ_init_instance_fx()
 *
 * initialize the encoder or decoder instance ecsq_inst, using the configuration index config_index;
 * ac_handle is a pointer to a structure containing the bitstream writer and the arithmetic coding state;
 * the return value is the approximate number of bits written, expressed in 22Q10 fixed-point representation
 * ---------------------------------------------------------------*/

void ECSQ_init_instance_fx(
    ECSQ_instance *ecsq_inst,
    const Word16 config_index,
    void *ac_handle )
{
    ecsq_inst->config_index = config_index;
    ecsq_inst->encoding_active = 1;
    ecsq_inst->bit_count_estimate = 0;
    ecsq_inst->ac_handle = ac_handle;

    return;
}

/*---------------------------------------------------------------
 * ECSQ_dequantize_gain_fx()
 *
 * dequantize global gain index
 * ---------------------------------------------------------------*/

Word32 ECSQ_dequantize_gain_fx(
    const Word16 index )
{
    Word32 global_gain;
    Word32 L_tmp, L_prod;
    Word16 gg_e, tmp_e = 16;

    /* pow(10.0, index * ECLVQ_INV_GLOBAL_GAIN_FACTOR) = pow(2.0,(index * ECLVQ_INV_GLOBAL_GAIN_FACTOR)*3.321928 */
    L_prod = Mpy_32_16_1( ECLVQ_INV_GLOBAL_GAIN_FACTOR_Q24, shl( index, 8 ) ); /* Q17 */
    L_tmp = Mpy_32_16_1( L_prod, 27213 );                                      /* Q17 + Q13 >> 15 => Q15 */

    global_gain = BASOP_util_Pow2( L_tmp, tmp_e, &gg_e );
    gg_e = sub( 16, gg_e );

    global_gain = L_shr( global_gain, gg_e );

    return global_gain;
}

/*---------------------------------------------------------------
 * ECSQ_dequantize_vector_fx()
 *
 * dequantize an integer-valued vector using optimal reconstruction points, which depend on the value of config_index
 * ---------------------------------------------------------------*/

void ECSQ_dequantize_vector_fx(
    const Word16 *input,
    const Word32 global_gain,
    const Word16 N,
    Word32 *output )
{
    Word16 i;

    FOR( i = 0; i < N; ++i )
    {
        output[i] = Mpy_32_16_1( global_gain, input[i] );
    }

    return;
}