Commit c70bf219 authored by TYAGIRIS's avatar TYAGIRIS
Browse files

remove code duplication

parent ce32390f
Loading
Loading
Loading
Loading
Loading
+87 −45
Original line number Diff line number Diff line
@@ -2131,6 +2131,76 @@ static void ivas_decode_huffman_bs(
    return;
}

#ifdef NONBE_FIX_862_UBSAN_SPAR_DEC_BR_SW_PLC
static void ivas_spar_plc_get_band_age(
    const int16_t *valid_bands,
    int16_t *base_band_age,
    const int16_t num_bands,
    int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS],
    int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
    int16_t *all_valid,
    int16_t *b_idx )
{
    int16_t b, idx;

    set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    idx = -1;
    *all_valid = 1;
    for ( b = 0; b < num_bands; b++ )
    {
        if ( valid_bands[b] != 0 )
        {
            base_band_age[b] = 0; /* reset band age */
            idx++;
            valid_band_idx[idx] = b;
        }
        else
        {
            base_band_age[b] += 1; /* increment the age of invalid bands */

            if ( base_band_age[b] > 3 )
            {
                last_valid_band_idx[b] = idx;
            }
            *all_valid = 0;
        }
    }
    *b_idx = idx;

    return;
}

static void ivas_spar_get_plc_interp_weights(
    int16_t valid_band_idx[IVAS_MAX_NUM_BANDS],
    int16_t last_valid_band_idx,
    int16_t idx,
    int16_t b,
    float *w,
    int16_t *id0,
    int16_t *id1 )
{
    if ( last_valid_band_idx < 0 ) /* Extrapolation */
    {
        *id1 = valid_band_idx[0];
        *id0 = 0;
        *w = 1;
    }
    else if ( last_valid_band_idx == idx ) /* Extrapolation */
    {
        *id1 = valid_band_idx[last_valid_band_idx];
        *id0 = valid_band_idx[last_valid_band_idx];
        *w = 0;
    }
    else /* Interpolation */
    {
        *id0 = valid_band_idx[last_valid_band_idx];
        *id1 = valid_band_idx[last_valid_band_idx + 1];
        *w = ( (float) ( b - *id0 ) ) / ( *id1 - *id0 );
    }
    return;
}
#endif

/*-----------------------------------------------------------------------------------------*
 * Function ivas_spar_md_fill_invalid_bands()
@@ -2151,6 +2221,10 @@ static void ivas_spar_md_fill_invalid_bands(
    int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    float w = 0;
#ifdef NONBE_FIX_862_UBSAN_SPAR_DEC_BR_SW_PLC
    ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
                                last_valid_band_idx, valid_band_idx, &all_valid, &idx );
#else
    set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );

@@ -2174,7 +2248,7 @@ static void ivas_spar_md_fill_invalid_bands(
            all_valid = 0;
        }
    }

#endif
    assert( idx > 0 ); /* some bands should be valid */

    if ( all_valid == 0 )
@@ -2184,6 +2258,11 @@ static void ivas_spar_md_fill_invalid_bands(
            /* check against non zero in if and else if */
            if ( base_band_age[b] > 3 ) /* old invalid bands */
            {
#ifdef NONBE_FIX_862_UBSAN_SPAR_DEC_BR_SW_PLC
                int16_t id0, id1;
                ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
                                                  idx, b, &w, &id0, &id1 );
#else
                int16_t tmp_id, id0, id1;

                tmp_id = last_valid_band_idx[b];
@@ -2205,7 +2284,7 @@ static void ivas_spar_md_fill_invalid_bands(
                    id1 = valid_band_idx[tmp_id + 1];
                    w = ( (float) ( b - id0 ) ) / ( id1 - id0 );
                }

#endif
                for ( i = 0; i < num_channels; i++ )
                {
                    for ( j = 0; j < num_channels; j++ )
@@ -2250,6 +2329,7 @@ static void ivas_spar_md_fill_invalid_bands(

    return;
}

#ifdef NONBE_FIX_862_UBSAN_SPAR_DEC_BR_SW_PLC
static void ivas_spar_md_fill_invalid_bandcoeffs(
    ivas_band_coeffs_t *pBand_coeffs,
@@ -2263,29 +2343,9 @@ static void ivas_spar_md_fill_invalid_bandcoeffs(
    int16_t valid_band_idx[IVAS_MAX_NUM_BANDS], idx = -1;
    int16_t last_valid_band_idx[IVAS_MAX_NUM_BANDS];
    float w = 0;
    set_s( valid_band_idx, 0, IVAS_MAX_NUM_BANDS );
    set_s( last_valid_band_idx, 0, IVAS_MAX_NUM_BANDS );

    all_valid = 1;
    for ( b = 0; b < num_bands; b++ )
    {
        if ( valid_bands[b] != 0 )
        {
            base_band_age[b] = 0; /* reset band age */
            idx++;
            valid_band_idx[idx] = b;
        }
        else
        {
            base_band_age[b] += 1; /* increment the age of invalid bands */

            if ( base_band_age[b] > 3 )
            {
                last_valid_band_idx[b] = idx;
            }
            all_valid = 0;
        }
    }
    ivas_spar_plc_get_band_age( valid_bands, base_band_age, num_bands,
                                last_valid_band_idx, valid_band_idx, &all_valid, &idx );

    assert( idx > 0 ); /* some bands should be valid */

@@ -2296,27 +2356,9 @@ static void ivas_spar_md_fill_invalid_bandcoeffs(
            /* check against non zero in if and else if */
            if ( ( base_band_age[b] > 3 ) || ( *first_valid_frame == 0 ) ) /* old invalid bands */
            {
                int16_t tmp_id, id0, id1;

                tmp_id = last_valid_band_idx[b];
                if ( tmp_id < 0 ) /* Extrapolation */
                {
                    id1 = valid_band_idx[0];
                    id0 = 0;
                    w = 1;
                }
                else if ( tmp_id == idx ) /* Extrapolation */
                {
                    id1 = valid_band_idx[tmp_id];
                    id0 = valid_band_idx[tmp_id];
                    w = 0;
                }
                else /* Interpolation */
                {
                    id0 = valid_band_idx[tmp_id];
                    id1 = valid_band_idx[tmp_id + 1];
                    w = ( (float) ( b - id0 ) ) / ( id1 - id0 );
                }
                int16_t id0, id1;
                ivas_spar_get_plc_interp_weights( valid_band_idx, last_valid_band_idx[b],
                                                  idx, b, &w, &id0, &id1 );

                for ( j = 0; j < IVAS_SPAR_MAX_CH - 1; j++ )
                {