Commit 800f9ba9 authored by Jiaquan Huo's avatar Jiaquan Huo
Browse files

Add switches, prepare for public release

parent 41baa039
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -173,6 +173,8 @@

#define NON_BE_FIX_1137_GSC_IVAS_FXFLT_DECODING               /* VA: Add fix point bit allocation for special GSC mode such that float and fixed point have the same final bit allocation */

#define NONE_BE_FIX_816_LFE_PLC_FLOAT                   /* DLB: issue 816: reduce required precision to float for LFE-PLC*/
                         
/* ##################### End NON-BE switches ########################### */

/* ################## End DEVELOPMENT switches ######################### */
+436 −10
Original line number Diff line number Diff line
@@ -40,7 +40,9 @@
#include "debug.h"
#endif
#include "wmc_auto.h"
#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
#include "string.h"
#endif

/*------------------------------------------------------------------------------------------*
 * Local constants
@@ -53,20 +55,17 @@
#define LFE_PLC_RECLEN_48K ( ( IVAS_LFE_NUM_COEFFS_IN_SUBGRP + 1 ) * L_FRAME48k / IVAS_LFE_NUM_COEFFS_IN_SUBGRP + LFE_PLC_FDEL )
#define LFE_PLC_RECLEN     ( ( LFE_PLC_RECLEN_48K / LFE_PLC_DSF ) )
#define LFE_PLC_MUTE_THR   ( 10 )
#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
#define LFE_PLC_BURST_ATT  ( powf( powf( 10.0f, -3.0f / 20.0f ), 1.0f / ( LFE_PLC_FS * 20 / 1000 ) ) ) /* attenuate 3dB per frame starting with 10th consecutive loss */

#define MAX_LEN_LP 960

#define EPS_STOP 1e-5f
#else
#define LFE_PLC_BURST_ATT  ( pow( pow( 10.0, -3.0 / 20.0 ), 1.0 / ( LFE_PLC_FS * 0.02 ) ) ) /* attenuate 3dB per frame starting with 10th consecutive loss */
#define MAX_LEN_LP 960
#define EPS_STOP 1e-5
#endif

/*------------------------------------------------------------------------------------------*
 * Static function declarations
 *
 * Note (DLB): the local float precision functions defined below are replica of corresponding
 * float functions defined in tools.c, lpc_tools.c and syn_filt.c.
 * float precision arithmetic is required for proper functioning of the lfe_plc.
 *------------------------------------------------------------------------------------------*/

#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
/*---------------------------------------------------------------------*
 * lev_dur()
 *
@@ -353,8 +352,431 @@ static void recover_samples(

    return;
}
#else
/*------------------------------------------------------------------------------------------*
 * Static function declarations
 *
 * Note (DLB): the local double precision functions defined below are replica of corresponding
 * float functions defined in tools.c, lpc_tools.c and syn_filt.c.
 * Double precision arithmetic is required for proper functioning of the lfe_plc.
 *------------------------------------------------------------------------------------------*/

static void mvr2d(
    const float x[], /* i  : input vector  */
    double y[],      /* o  : output vector */
    const int16_t n  /* i  : vector size   */
)
{
    int16_t i;

    if ( n <= 0 )
    {
        /* cannot transfer vectors with size 0 */
        return;
    }

    for ( i = n - 1; i >= 0; i-- )
    {
        y[i] = x[i];
    }

    return;
}


/*---------------------------------------------------------------------*
 * autocorr()
 *
 * Compute autocorrelations of input signal
 *---------------------------------------------------------------------*/

static void d_autocorr(
    const double *x,        /* i  : input signal               */
    double *r,              /* o  : autocorrelations vector    */
    const int16_t m,        /* i  : order of LP filter         */
    const int16_t len,      /* i  : window size                */
    const double *wind,     /* i  : window                     */
    const int16_t rev_flag, /* i  : flag to reverse window     */
    const int16_t sym_flag, /* i  : symmetric window flag      */
    const int16_t no_thr    /* i  : flag to avoid thresholding */
)
{
    double t[MAX_LEN_LP];
    double s;
    int16_t i, j;

    /* Windowing of signal */
    if ( rev_flag == 1 )
    {
        /* time reversed window */
        for ( i = 0; i < len; i++ )
        {
            t[i] = x[i] * wind[len - i - 1];
        }
    }
    else if ( sym_flag == 1 )
    {
        /* symmetric window of even length */
        for ( i = 0; i < len / 2; i++ )
        {
            t[i] = x[i] * wind[i];
        }

        for ( ; i < len; i++ )
        {
            t[i] = x[i] * wind[len - 1 - i];
        }
    }
    else /* assymetric window */
    {
        for ( i = 0; i < len; i++ )
        {
            t[i] = x[i] * wind[i];
        }
    }

    /* Compute r[1] to r[m] */
    for ( i = 0; i <= m; i++ )
    {
        s = t[0] * t[i];
        for ( j = 1; j < len - i; j++ )
        {
            s += t[j] * t[i + j];
        }
        r[i] = s;
    }

    if ( r[0] < 100.0f && no_thr == 0 )
    {
        r[0] = 100.0f;
    }

    return;
}


/*---------------------------------------------------------------------*
 * lev_dur()
 *
 * Wiener-Levinson-Durbin algorithm to compute LP parameters from the autocorrelations
 * of input signal
 *---------------------------------------------------------------------*/

/*! r: energy of prediction error   */
static int16_t d_lev_dur(
    double *a,       /* o  : LP coefficients (a[0] = 1.0) */
    const double *r, /* i  : vector of autocorrelations   */
    const int16_t m, /* i  : order of LP filter           */
    double epsP[]    /* o  : prediction error energy      */
)
{
    int16_t i, j, l;
    double buf[TCXLTP_LTP_ORDER];
    double *rc; /* reflection coefficients  0,...,m-1 */
    double s, at, err;
    int16_t flag = 0;

    rc = &buf[0];
    rc[0] = ( -r[1] ) / r[0];
    a[0] = 1.0;
    a[1] = rc[0];
    err = r[0] + r[1] * rc[0];

    if ( epsP != NULL )
    {
        epsP[0] = r[0];
        epsP[1] = err;
    }

    for ( i = 2; i <= m; i++ )
    {
        s = 0.0;
        for ( j = 0; j < i; j++ )
        {
            s += r[i - j] * a[j];
        }

        rc[i - 1] = ( -s ) / err;
        if ( fabs( rc[i - 1] ) > 0.99945f )
        {
            flag = 1; /* Test for unstable filter. If unstable keep old A(z) */
        }
        for ( j = 1; j <= i / 2; j++ )
        {
            l = i - j;
            at = a[j] + rc[i - 1] * a[l];
            a[l] += rc[i - 1] * a[j];
            a[j] = at;
        }

        a[i] = rc[i - 1];

        err += rc[i - 1] * s;

        if ( err <= 0.0f )
        {
            err = 0.01f;
        }

        if ( epsP != NULL )
        {
            epsP[i] = err;
        }
    }

    return ( flag );
}

/*-------------------------------------------------------------------*
 * a2rc()
 *
 * Convert from LPC to reflection coeff
 *-------------------------------------------------------------------*/

static uint16_t d_a2rc(
    const double *a,       /* i  : LPC coefficients            */
    double *refl,          /* o  : Reflection co-efficients    */
    const int16_t lpcorder /* i  : LPC order                   */
)
{
    double ff[LFE_PLC_LPCORD];
    int16_t m, j, n;
    double km, denom, x;

    for ( m = 0; m < lpcorder; m++ )
    {
        ff[m] = -a[m];
    }

    /* Initialization */
    for ( m = lpcorder - 1; m >= 0; m-- )
    {
        km = ff[m];
        if ( km <= -1.0 || km >= 1.0 )
        {
            for ( j = 0; j < lpcorder; j++ )
            {
                refl[j] = 0.0;
            }

            return 0;
        }

        refl[m] = -km;
        denom = 1.0 / ( 1.0 - km * km );
        for ( j = 0; j < m / 2; j++ )
        {
            n = m - 1 - j;
            x = denom * ff[j] + km * denom * ff[n];
            ff[n] = denom * ff[n] + km * denom * ff[j];
            ff[j] = x;
        }

        if ( m & 1 )
        {
            ff[j] = denom * ff[j] + km * denom * ff[j];
        }
    }

    return 1;
}


static void d_syn_filt(
    const double a[], /* i  : LP filter coefficients                     */
    const int16_t m,  /* i  : order of LP filter                         */
    const float x[],  /* i  : input signal                               */
    float y[],        /* o  : output signal                              */
    const int16_t l,  /* i  : size of filtering                          */
    const float mem[] /* i  : initial filter states                      */
)
{
    int16_t i, j;
    double buf[LFE_PLC_LPCORD + LFE_PLC_RECLEN]; /* temporary synthesis buffer */
    double s, *yy;

    yy = &buf[0];

    /*------------------------------------------------------------------*
     * copy initial filter states into synthesis buffer and do synthesis
     *------------------------------------------------------------------*/
    for ( i = 0; i < m; i++ )
    {
        *yy++ = mem[i];
    }

    /*-----------------------------------------------------------------------*
     * Do the filtering
     *-----------------------------------------------------------------------*/

    for ( i = 0; i < l; i++ )
    {
        s = x[i];
        for ( j = 1; j <= m; j++ )
        {
            s -= a[j] * yy[i - j];
        }

        yy[i] = s;
        y[i] = (float) s;
    }

    return;
}


/*-----------------------------------------------------------------------------------------*
 * Function check_stab()
 *
 * LPC filter stability check applying given sharpening value delta
 *-----------------------------------------------------------------------------------------*/

static uint16_t check_stab(
    const double *a,
    double delta )
{
    double amod[LFE_PLC_LPCORD], refl[LFE_PLC_LPCORD];
    int16_t i;
    double fac;
    double fac1;
    uint16_t stable;

    fac = 1.0 + delta;
    fac1 = fac;

    for ( i = 0; i < LFE_PLC_LPCORD; i++ )
    {
        amod[i] = a[i] * fac;
        fac *= fac1;
    }
    stable = d_a2rc( amod, refl, LFE_PLC_LPCORD );

    return stable;
}


/*-----------------------------------------------------------------------------------------*
 * Function find_max_delta()
 *
 * Find maximum LPC filter sharpening by iteration to get a filter that is almost instable
 *-----------------------------------------------------------------------------------------*/

static double find_max_delta(
    double *a )
{
    double delta;
    double eps;
    uint16_t stable;
    double fac;

    delta = 0.0;
    eps = 0.01;
    fac = 2;
    stable = FALSE;

    while ( check_stab( a, eps ) )
    {

        eps *= fac;
        stable = TRUE;
    }
    fac = 0.5;

    if ( stable )
    {
        eps *= fac;
    }

    while ( !stable )
    {
        eps *= fac;
        stable = check_stab( a, eps );
    }

    /* must be stable with current eps */
    delta = eps;
    eps *= fac;

    while ( 1 )
    {
        delta += eps;
        stable = check_stab( a, delta );

        if ( !stable )
        {
            if ( fabs( eps ) > EPS_STOP )
            {
                eps = -fabs( eps ) * fac;
            }
            else
            {
                eps = -fabs( eps );
            }
        }
        else
        {
            if ( fabs( eps ) < EPS_STOP )
            {
                break;
            }
            eps = fabs( eps ) * fac;
        }
    }

    return delta;
}


/*-----------------------------------------------------------------------------------------*
 * Function recover_samples()
 *
 * recover lost samples by extrapolation of signal buffer
 *-----------------------------------------------------------------------------------------*/

static void recover_samples(
    const int16_t bfi_count,
    const float *outbuf,
    float *rec_frame )
{
    int16_t i;
    float zeroes[LFE_PLC_RECLEN];
    double delta, fac, att;
    double d_outbuf[LFE_PLC_BUFLEN], d_r[LFE_PLC_LPCORD + 1], d_a[LFE_PLC_LPCORD + 1], d_pee[LFE_PLC_LPCORD + 1];

    mvr2d( outbuf, d_outbuf, LFE_PLC_BUFLEN );
    d_autocorr( d_outbuf, d_r, LFE_PLC_LPCORD, LFE_PLC_BUFLEN, d_hamm_lfe_plc, 0, 1, 1 );

    if ( d_r[0] < POW_THR * LFE_PLC_BUFLEN )
    {
        set_zero( rec_frame, LFE_PLC_RECLEN );
        return;
    }
    d_lev_dur( d_a, d_r, LFE_PLC_LPCORD, d_pee );

    delta = find_max_delta( d_a + 1 );

    fac = 1.0 + delta;
    att = 1.0;

    if ( bfi_count >= LFE_PLC_MUTE_THR )
    {
        att = LFE_PLC_BURST_ATT;
        fac *= att;
    }

    for ( i = 1; i <= LFE_PLC_LPCORD; i++ )
    {
        d_a[i] = d_a[i] * fac;
        fac *= att * ( 1.0 + delta );
    }

    set_zero( zeroes, LFE_PLC_RECLEN );
    d_syn_filt( d_a, LFE_PLC_LPCORD, zeroes, rec_frame, LFE_PLC_RECLEN, outbuf + LFE_PLC_BUFLEN - LFE_PLC_LPCORD );

    return;
}
#endif

/*-----------------------------------------------------------------------------------------*
 * Function ivas_lfe_tdplc()
 *
@@ -363,7 +785,11 @@ static void recover_samples(

void ivas_lfe_tdplc(
    LFE_DEC_HANDLE hLFE,       /* i/o: LFE decoder handle           */
#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
    float *prevsynth,    /* i  : previous frame synthesis     */
#else
    const float *prevsynth,    /* i  : previous frame synthesis     */
#endif
    float *ytda,               /* o  : output time-domain buffer    */
    const int16_t output_frame /* i  : output frame length          */
)