Skip to content
FEC_HQ_phase_ecu.c 74.6 KiB
Newer Older
Marek Szczerba's avatar
Marek Szczerba committed
/******************************************************************************************************

   (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
Marek Szczerba's avatar
Marek Szczerba committed
   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.

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

/*====================================================================================
    EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
  ====================================================================================*/

#include <stdint.h>
#include "options.h"
#ifdef DEBUGGING
#include "debug.h"
#endif
#include <math.h>
#include "rom_dec.h"
#include "rom_com.h"
#include "cnst.h"
#include "prot.h"
#include "wmc_auto.h"
Marek Szczerba's avatar
Marek Szczerba committed

/*---------------------------------------------------------------------*
 * Local constants
 *---------------------------------------------------------------------*/

#define FEC_MAX                512
#define FEC_NB_PULSE_MAX       20
#define FEC_FFT_MAX_SIZE       512
#define FEC_DCIM_FILT_SIZE_MAX 60

#define PHASE_DITH ( PI2 )

#define DELTA_CORR             6 /* Range for phase correction around peak */
#define THRESH_TR_dB           10.0f
#define THRESH_TR_LIN          (float) pow( 10.0f, THRESH_TR_dB / 10.0f )
#define THRESH_TR_LIN_INV      (float) pow( 10.0f, -THRESH_TR_dB / 10.0f )
#define MAX_INCREASE_GRPOW     0.0f /* maximum amplification in case of transients */
#define MAX_INCREASE_GRPOW_LIN (float) pow( 10.0f, MAX_INCREASE_GRPOW / 10.0f )

#define PHASE_DITH_SCALE (float) pow( 2.0, -16.0 ) /* for scaling random short values  to  +/- pi */

#define BURST_PHDITH_THRESH     ( 4 - 1 ) /* speech start phase dither with <burst_phdith_thresh> losses in a row */
#define BURST_PHDITH_RAMPUP_LEN 2         /* speech ramp up degree of phase dither over a length of <burst_phdith_rampup_len> frames */
#define BURST_ATT_THRESH        ( 3 - 1 ) /* speech start attenuate with <burst_att_thresh> losses in a row */
#define ATT_PER_FRAME           4         /* speech attenuation in dB */
#define BETA_MUTE_THR           10        /* time threshold to start beta-noise attenuation */
#define BETA_MUTE_FAC           0.5f      /* attenuation factor per additional bad frame */

#define LGW32k 7
#define LGW16k 6
#define LGW48k LGW32k + 1 /* Use the same frequency groups as for SWB + 1 */

#define L_TRANA_LOG32k 8
#define L_TRANA_LOG16k 7

#define DELTA_CORR_F0_INT 2         /* Constant controls the bin range where Jacobsen is used */
#define ST_PFIND_SENS     0.93f     /* peakfinder sensitivity */
#define L_PROT_NS         32000000L /* Prototype frame length in nanoseconds (32 ms) */
#define PH_ECU_CORR_LIMIT 0.85f     /* Correlation limit for IVAS Phase ECU activation */
#define PH_ECU_N_LIMIT    56        /* fec_alg analysis frame limit for IVAS Phase ECU activation */
#define PFIND_SENS        0.97f     /* peakfinder sensitivity */

/*---------------------------------------------------------------------*
 * Local functions
 *---------------------------------------------------------------------*/

static int16_t rand_phase( const int16_t seed, float *sin_F, float *cos_F );
static float imax2_jacobsen_mag( const float *y_re, const float *y_im );

/*-------------------------------------------------------------------*
 * mult_rev2()
 *
 * Multiplication of two vectors second vector is multiplied in reverse order
 *-------------------------------------------------------------------*/

static void mult_rev2(
    const float x1[], /* i  : Input vector 1                                   */
    const float x2[], /* i  : Input vector 2                                   */
    float y[],        /* o  : Output vector that contains vector 1 .* vector 2 */
    const int16_t N   /* i  : Vector length                                    */
)
{
    int16_t i, j;

    for ( i = 0, j = N - 1; i < N; i++, j-- )
    {
        y[i] = x1[i] * x2[j];
    }

    return;
}


/*-------------------------------------------------------------------*
 * fft_spec2()
 *
 * Square magnitude of fft spectrum
 *-------------------------------------------------------------------*/

static void fft_spec2(
    float x[],      /* i/o: Input vector: complex spectrum -> square magnitude spectrum  */
    const int16_t N /* i  : Vector length                                                */
)
{
    int16_t i, j;

    for ( i = 1, j = N - 1; i < N / 2; i++, j-- )
    {
        x[i] = x[i] * x[i] + x[j] * x[j];
    }

    x[0] *= x[0];
    x[N / 2] *= x[N / 2];

    return;
}

/*------------------------------------------------------------------*
 * rand_phase()
 *
 * randomized phase in form of sin and cos components
 *------------------------------------------------------------------*/

/*! r: Updated seed from RNG */
static int16_t rand_phase(
    const int16_t seed, /* i  : RNG seed                          */
    float *sin_F,       /* o  : random phase sin value            */
    float *cos_F        /* o  : random phase cos value            */
)
{
    const float *sincos = sincos_t_ext + 128;
    int16_t seed2 = seed;
    own_random( &seed2 );

    if ( seed2 & 0x40 )
    {
        *sin_F = sincos[seed2 >> 8];
    }
    else
    {
        *sin_F = -sincos[seed2 >> 8];
    }

    if ( seed2 & 0x80 )
    {
        *cos_F = sincos[-( seed2 >> 8 )];
    }
    else
    {
        *cos_F = -sincos[-( seed2 >> 8 )];
    }

    return seed2;
}

/*-----------------------------------------------------------------------------
 * imax2_jacobsen_mag()
 *
 * refine peak interpolation using jacobsen and periodic speca ana windows
 *----------------------------------------------------------------------------*/

/*! r: The location, relative to the middle of the 3 given data point, of the maximum. (Q15)*/
float imax2_jacobsen_mag(
    const float *y_re, /* i  : The 3 given data points. real part order -1 0 1                       */
    const float *y_im  /* i  : The 3 given data points. imag part order  1 0 -1 (from FFT)           */
)
{
    float posi;
    const float *pY;
    float y_m1_re, y_0_re, y_p1_re;
    float y_m1_im, y_0_im, y_p1_im;
    float N_re, N_im;
    float D_re, D_im;
Loading
Loading full blame…