Commit 9e4a32f5 authored by multrus's avatar multrus
Browse files

Merge branch...

Merge branch '617-ubsan-division-by-zero-in-stereo-cng-when-inut-is-16khz-and-output-is-32khz' into 'main'

Resolve #617 "UBSAN: division by zero in stereo CNG  when input is 16kHz and output is 32kHz"

See merge request !847
parents cd05f6d1 718ad002
Loading
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -200,8 +200,12 @@
#define FIX_629_UBSAN_MD_MAX_BITS                       /*Dlb : Fix for UBSAN issue 629 for MD MAX bits calculation*/

#define FIX_626_VARIABLE_TYPE_MDCT_CONC                 /* FhG: trivial fix to fix USAN error */

#define FIX_616_DIV_ZERO_MCT                            /*FhG : Fix UBSAN division by zero error of issue 616*/

#define FIX617_UBSAN_DIVBYZERO_STEREOCNG                /* Eri: Issue 617: Decoder UBSAN: division by zero in stereo cng when inut is 16kHz and output is 32kHz */
#define FIX_279_CODE_COVERAGE                           /* Dlb : issue 279 , clean up unused function */

/* ################## End BE DEVELOPMENT switches ######################### */


+17 −0
Original line number Diff line number Diff line
@@ -737,9 +737,15 @@ static void shb_CNG_decod(
        ener = hTdCngDec->shb_cng_ener;
    }


    gain = (float) sqrt( pow( 10, 0.1f * ener ) * L_FRAME16k / ener_excSHB );

    st->hTdCngDec->shb_cng_gain = ener;
#ifdef FIX617_UBSAN_DIVBYZERO_STEREOCNG
#ifdef DEBUGGING
    /* note: state shb_cng_gain is actually an energy value in dB */
#endif
#endif

    for ( i = 0; i < L_FRAME16k; i++ )
    {
@@ -783,6 +789,15 @@ void td_cng_dec_init(
    mvr2r( st->lsp_old, st->lspCNG, M );
    hTdCngDec->last_allow_cn_step = 0;
    hTdCngDec->shb_cng_ener = -6.02f;
#ifdef FIX617_UBSAN_DIVBYZERO_STEREOCNG
    if ( st->element_mode != EVS_MONO )
    {
        set_f( hTdCngDec->shb_lpcCNG, 0.0f, LPC_SHB_ORDER + 1 );
        hTdCngDec->shb_lpcCNG[0] = 1.0f;
        hTdCngDec->shb_cng_gain = -82.0; /* a  dB value approximately corresponding to  shb  index 0(used as index -15)   */
    }
#endif

    hTdCngDec->wb_cng_ener = -6.02f;
    hTdCngDec->last_wb_cng_ener = -6.02f;
    hTdCngDec->last_shb_cng_ener = -6.02f;
@@ -825,8 +840,10 @@ void td_cng_dec_init(
    hTdCngDec->shb_dtx_count = 0;
    hTdCngDec->trans_cnt = 0;
    hTdCngDec->burst_cnt = 0;

    hTdCngDec->last_shb_ener = 0.001f;


    set_f( hTdCngDec->interpol_3_2_cng_dec, 0.0f, INTERP_3_2_MEM_LEN );

    return;
+29 −0
Original line number Diff line number Diff line
@@ -342,12 +342,25 @@ static void stereo_dft_generate_comfort_noise(
        ptr0 = cngNoiseLevel_upd;
        ptr1 = ptr0 + 2;
        ptr2 = ptr1 + 1;
#ifdef FIX617_UBSAN_DIVBYZERO_STEREOCNG
        assert( st->lp_ener > 0.0f );
        factor = 2.0f * sqrtf( st->lp_ener / st->L_frame * 0.5f ); /* fixed factor  in the loop below */
        for ( i = 0; i < st->L_frame / 2 - 1; i++ )
        {
            ftmp = *ptr1 * *ptr1 + *ptr2 * *ptr2;
            assert( ftmp > 0.0f );
            *ptr0++ = factor * inv_sqrt( ftmp );
            ptr1 += 2;
            ptr2 += 2;
        }
#else
        for ( i = 0; i < st->L_frame / 2 - 1; i++ )
        {
            *ptr0++ = 2.0f * sqrtf( st->lp_ener / st->L_frame * 0.5f ) * inv_sqrt( *ptr1 * *ptr1 + *ptr2 * *ptr2 );
            ptr1 += 2;
            ptr2 += 2;
        }
#endif

        if ( min( output_frame, L_FRAME32k ) - hFdCngCom->stopFFTbin > 0 )
        {
@@ -375,9 +388,22 @@ static void stereo_dft_generate_comfort_noise(
            ptr0 = shb_shape;
            ptr1 = ptr0 + 2;
            ptr2 = ptr1 + 1;

            for ( i = 0; i < L_FRAME16k / 2 - 1; i++ )
            {
#ifdef FIX617_UBSAN_DIVBYZERO_STEREOCNG
                ftmp = ( *ptr1 * *ptr1 + *ptr2 * *ptr2 );
                assert( ftmp > 0.0f );
                ftmp = 1.0f / ftmp;
                /* in float:
                     both a = "div"=(1/(x^2+y^2) and sqrt(a)  is used and summed up in the same loop.

                  in BASOP:
                    sum up  using inv_sqrt( *ptr1 * *ptr1 + *ptr2 * *ptr2 ), in this loop
                    and then sum up  enr = sum( *ptr0 * *ptr0 ),  in a subsequent MAC loop  */
#else
                ftmp = 1.0f / ( *ptr1 * *ptr1 + *ptr2 * *ptr2 );
#endif
                enr += ftmp;
                *ptr0++ = sqrtf( ftmp );
                ptr1 += 2;
@@ -455,6 +481,9 @@ static void stereo_dft_generate_comfort_noise(
            {

                /* high band generation, flipped spectrum */
#ifdef FIX617_UBSAN_DIVBYZERO_STEREOCNG
                assert( enr != 0.0f );
#endif
                scale = sqrtf( powf( 10, 0.1f * st->hTdCngDec->shb_cng_gain ) / enr );
                ptr_shb = shb_shape + L_FRAME16k / 2 - 1;
                /* Averaging for Nyquist frequency */