Commit ce41f41a authored by Jiaquan Huo's avatar Jiaquan Huo
Browse files

Further cleanup to highlight the changes

parent 80790586
Loading
Loading
Loading
Loading
Loading
+177 −304
Original line number Diff line number Diff line
@@ -40,9 +40,6 @@
#include "debug.h"
#endif
#include "wmc_auto.h"
#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
#include "string.h"
#endif

/*------------------------------------------------------------------------------------------*
 * Local constants
@@ -63,294 +60,7 @@
#define EPS_STOP 1e-5
#endif

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

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

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

    memcpy(a_out, a, sizeof(a));
    memset(epsP, 0, sizeof(*epsP) * LFE_PLC_LPCORD);

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

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

        rc[i - 1] = ( -s ) / err;
        if ( fabsf( rc[i - 1] ) > 0.99945f )
        {
            flag = 1; /* Test for unstable filter. If unstable keep old A(z) */
            return flag;
        }
        else
        {
            for (j = 0; j <= m; j++)
            {
                a_out[j] = a[j];
            }
        }
        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 lfeplc_a2rc(
    const float *a,       /* i  : LPC coefficients            */
    float *refl,          /* o  : Reflection co-efficients    */
    const int16_t lpcorder /* i  : LPC order                   */
)
{
    float ff[LFE_PLC_LPCORD];
    int16_t m, j, n;
    float 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.0f || km >= 1.0f )
        {
            for ( j = 0; j < lpcorder; j++ )
            {
                refl[j] = 0.0f;
            }

            return 0;
        }

        refl[m] = -km;
        denom = 1.0f / ( 1.0f - 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;
}

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

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

    fac = 1.0f + delta;
    fac1 = fac;

    for ( i = 0; i < LFE_PLC_LPCORD; i++ )
    {
        amod[i] = a[i] * fac;
        fac *= fac1;
    }
    stable = lfeplc_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 float find_max_delta(
    float *a )
{
    float delta;
    float eps;
    uint16_t stable;
    float fac;

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

    while ( check_stab( a, eps ) )
    {

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

    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 ( fabsf( eps ) > EPS_STOP )
            {
                eps = -fabsf( eps ) * fac;
            }
            else
            {
                eps = -fabsf( eps );
            }
        }
        else
        {
            if ( fabsf( eps ) < EPS_STOP )
            {
                break;
            }
            eps = fabsf( eps ) * fac;
        }
    }

    return delta;
}


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

static void recover_samples(
    const int16_t bfi_count,
    float *outbuf,
    float *rec_frame )
{
    int16_t i;
    float zeroes[LFE_PLC_RECLEN];
    float delta, fac, att;
    float r[LFE_PLC_LPCORD + 1], a[LFE_PLC_LPCORD + 1], pee[LFE_PLC_LPCORD + 1];
    autocorr(outbuf, r, LFE_PLC_LPCORD, LFE_PLC_BUFLEN, hamm_lfe_plc, 0, 1, 1 );

    if ( r[0] < POW_THR * LFE_PLC_BUFLEN )
    {
        set_zero( rec_frame, LFE_PLC_RECLEN );
        return;
    }
    lfeplc_lev_dur( a, r, LFE_PLC_LPCORD, pee );

    delta = find_max_delta( a + 1 );

    fac = 1.0f + delta;
    att = 1.0f;

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

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

    set_zero( zeroes, LFE_PLC_RECLEN );
    syn_filt( a, LFE_PLC_LPCORD, zeroes, rec_frame, LFE_PLC_RECLEN, outbuf + LFE_PLC_BUFLEN - LFE_PLC_LPCORD, 0 );

    return;
}
#else
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
/*------------------------------------------------------------------------------------------*
 * Static function declarations
 *
@@ -467,11 +177,34 @@ static int16_t d_lev_dur(
    const int16_t m, /* i  : order of LP filter           */
    double epsP[]    /* o  : prediction error energy      */
)
#else
/*---------------------------------------------------------------------*
 * lev_dur()
 *
 * Wiener-Levinson-Durbin algorithm to compute LP parameters from the autocorrelations
 * of input signal
 *---------------------------------------------------------------------*/

/*! r: energy of prediction error   */
static int16_t lfeplc_lev_dur(
    float *a_out,       /* o  : LP coefficients (a[0] = 1.0) */
    const float *r, /* i  : vector of autocorrelations   */
    const int16_t m, /* i  : order of LP filter           */
    float epsP[]    /* o  : prediction error energy      */
)
#endif
{
    int16_t i, j, l;
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    double buf[TCXLTP_LTP_ORDER];
    double *rc; /* reflection coefficients  0,...,m-1 */
    double s, at, err;
#else
    float buf[TCXLTP_LTP_ORDER];
    float *rc; /* reflection coefficients  0,...,m-1 */
    float s, at, err;
    float a[LFE_PLC_LPCORD + 1] = {0.f};
#endif
    int16_t flag = 0;

    rc = &buf[0];
@@ -480,6 +213,14 @@ static int16_t d_lev_dur(
    a[1] = rc[0];
    err = r[0] + r[1] * rc[0];

#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
    for (i = 0; i <= LFE_PLC_LPCORD; i++)
    {
        a_out[i] = 0.f;
        epsP[i] = 0.f;
    }
#endif

    if ( epsP != NULL )
    {
        epsP[0] = r[0];
@@ -488,17 +229,37 @@ static int16_t d_lev_dur(

    for ( i = 2; i <= m; i++ )
    {
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        s = 0.0;
#else
        s = 0.0f;
#endif
        for ( j = 0; j < i; j++ )
        {
            s += r[i - j] * a[j];
        }

        rc[i - 1] = ( -s ) / err;
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        if ( fabs( rc[i - 1] ) > 0.99945f )
#else
        if ( fabsf( rc[i - 1] ) > 0.99945f )
#endif
        {
            flag = 1; /* Test for unstable filter. If unstable keep old A(z) */
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        }
#else
            return flag;
        }
        else
        {
            for (j = 0; j <=m; j++)
            {
                a_out[j] = a[j];
            }
        }
#endif
        for ( j = 1; j <= i / 2; j++ )
        {
            l = i - j;
@@ -530,16 +291,28 @@ static int16_t d_lev_dur(
 *
 * Convert from LPC to reflection coeff
 *-------------------------------------------------------------------*/

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

    for ( m = 0; m < lpcorder; m++ )
    {
@@ -550,18 +323,30 @@ static uint16_t d_a2rc(
    for ( m = lpcorder - 1; m >= 0; m-- )
    {
        km = ff[m];
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        if ( km <= -1.0 || km >= 1.0 )
#else
        if ( km <= -1.0f || km >= 1.0f )
#endif
        {
            for ( j = 0; j < lpcorder; j++ )
            {
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
                refl[j] = 0.0;
#else
                refl[j] = 0.0f;
#endif
            }

            return 0;
        }

        refl[m] = -km;
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        denom = 1.0 / ( 1.0 - km * km );
#else
        denom = 1.0f / ( 1.0f - km * km );
#endif
        for ( j = 0; j < m / 2; j++ )
        {
            n = m - 1 - j;
@@ -579,7 +364,7 @@ static uint16_t d_a2rc(
    return 1;
}


#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
static void d_syn_filt(
    const double a[], /* i  : LP filter coefficients                     */
    const int16_t m,  /* i  : order of LP filter                         */
@@ -621,25 +406,38 @@ static void d_syn_filt(

    return;
}

#endif

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

#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
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;
#else
static uint16_t check_stab(
    const float *a,
    float delta )
{
    float amod[LFE_PLC_LPCORD], refl[LFE_PLC_LPCORD];
    float fac;
    float fac1;
#endif
    int16_t i;
    uint16_t stable;

#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    fac = 1.0 + delta;
#else
    fac = 1.0f + delta;
#endif
    fac1 = fac;

    for ( i = 0; i < LFE_PLC_LPCORD; i++ )
@@ -647,7 +445,11 @@ static uint16_t check_stab(
        amod[i] = a[i] * fac;
        fac *= fac1;
    }
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    stable = d_a2rc( amod, refl, LFE_PLC_LPCORD );
#else
    stable = lfeplc_a2rc( amod, refl, LFE_PLC_LPCORD );
#endif

    return stable;
}
@@ -658,17 +460,30 @@ static uint16_t check_stab(
 *
 * Find maximum LPC filter sharpening by iteration to get a filter that is almost instable
 *-----------------------------------------------------------------------------------------*/

#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
static double find_max_delta(
    double *a )
{
    double delta;
    double eps;
    uint16_t stable;
    double fac;
#else
static float find_max_delta(
    float *a )
{
    float delta;
    float eps;
    float fac;
#endif
    uint16_t stable;

#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    delta = 0.0;
    eps = 0.01;
#else
    delta = 0.0f;
    eps = 0.01f;
#endif
    fac = 2;
    stable = FALSE;

@@ -678,7 +493,11 @@ static double find_max_delta(
        eps *= fac;
        stable = TRUE;
    }
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    fac = 0.5;
#else
    fac = 0.5f;
#endif

    if ( stable )
    {
@@ -700,6 +519,7 @@ static double find_max_delta(
        delta += eps;
        stable = check_stab( a, delta );

#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        if ( !stable )
        {
            if ( fabs( eps ) > EPS_STOP )
@@ -719,6 +539,27 @@ static double find_max_delta(
            }
            eps = fabs( eps ) * fac;
        }
#else
        if ( !stable )
        {
            if ( fabsf( eps ) > EPS_STOP )
            {
                eps = -fabsf( eps ) * fac;
            }
            else
            {
                eps = -fabsf( eps );
            }
        }
        else
        {
            if ( fabsf( eps ) < EPS_STOP )
            {
                break;
            }
            eps = fabsf( eps ) * fac;
        }
#endif
    }

    return delta;
@@ -730,31 +571,55 @@ static double find_max_delta(
 *
 * recover lost samples by extrapolation of signal buffer
 *-----------------------------------------------------------------------------------------*/

static void recover_samples(
    const int16_t bfi_count,
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    const float *outbuf,
#else
    float *outbuf,
#endif
    float *rec_frame )
{
    int16_t i;
    float zeroes[LFE_PLC_RECLEN];
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    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 );
#else
    float delta, fac, att;
    float r[LFE_PLC_LPCORD + 1], a[LFE_PLC_LPCORD + 1], pee[LFE_PLC_LPCORD + 1];

    autocorr(outbuf, r, LFE_PLC_LPCORD, LFE_PLC_BUFLEN, hamm_lfe_plc, 0, 1, 1 );
#endif

#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    if ( d_r[0] < POW_THR * LFE_PLC_BUFLEN )
#else
    if ( r[0] < POW_THR * LFE_PLC_BUFLEN )
#endif
    {
        set_zero( rec_frame, LFE_PLC_RECLEN );
        return;
    }
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
    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;
#else
    lfeplc_lev_dur( a, r, LFE_PLC_LPCORD, pee );

    delta = find_max_delta( a + 1 );

    fac = 1.0f + delta;
    att = 1.0f;
#endif

    if ( bfi_count >= LFE_PLC_MUTE_THR )
    {
@@ -764,16 +629,24 @@ static void recover_samples(

    for ( i = 1; i <= LFE_PLC_LPCORD; i++ )
    {
#ifndef NONE_BE_FIX_816_LFE_PLC_FLOAT
        d_a[i] = d_a[i] * fac;
        fac *= att * ( 1.0 + delta );
#else
        a[i] = a[i] * fac;
        fac *= att * ( 1.0f + delta );
#endif
    }

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

#else
    syn_filt( a, LFE_PLC_LPCORD, zeroes, rec_frame, LFE_PLC_RECLEN, outbuf + LFE_PLC_BUFLEN - LFE_PLC_LPCORD, 0);
#endif
    return;
}
#endif


/*-----------------------------------------------------------------------------------------*
 * Function ivas_lfe_tdplc()
@@ -786,7 +659,7 @@ void ivas_lfe_tdplc(
#ifdef NONE_BE_FIX_816_LFE_PLC_FLOAT
    float *prevsynth,                                     /* i  : previous frame synthesis                */
#else
    const float *prevsynth,    /* i  : previous frame synthesis     */
    double *prevsynth,                                     /* i  : previous frame synthesis                */
#endif
    float *ytda,               /* o  : output time-domain buffer    */
    const int16_t output_frame /* i  : output frame length          */