Commit 0fe24b10 authored by Sandesh Venkatesh's avatar Sandesh Venkatesh
Browse files

Merge branch '802-different-bit-allocation-between-fix-and-floating-point' into 'main'

Resolve "Different bit allocation between fix and floating point"

See merge request !409
parents 86fd8580 bc7529bb
Loading
Loading
Loading
Loading
Loading
+401 −14
Original line number Diff line number Diff line
@@ -14,6 +14,37 @@

static void reajust_bits_fx( Word32 *bits_per_bands, const Word16 st_band, const Word16 end_band, const Word16 sum_bit_in, const Word16 bit_bdgt_in );

#ifdef FIX_802_1137_1137_GSC_IVAS_FXFLT_DECODING
/*-------------------------------------------------------------------*
 * Local constants
 *-------------------------------------------------------------------*/
#define Q15_0_33             10922                     /* 0.33 */
#define Q18_0_1              26214                     /* 0.1  */
#define Q18_0_50             131072                    /* 0.50 */
#define Q18_0_75             196608                    /* 0.75 */
#define Q18_0_76             199229                    /* 76/100 */
#define Q18_1_0              262144                    /* 1.0 */
#define Q18_1_2              314573                    /* 1.2 */
#define Q18_112              29360128                  /* 112 */
#define Q18_DSR_NB_PULSE     1179648                   /* 4.5  */
#define Q18_1_5xDSR_NB_PULSE 1769472                   /* 1.5x4.5  */
#define Q18_2_0xDSR_NB_PULSE ( Q18_DSR_NB_PULSE << 1 ) /* 2.0x4.5  */

#define Q31_0_00125 2684355   /* 0.125/100  */
#define Q31_0_0125  26843546  /* 0.0125  */
#define Q31_0_015   32212255  /* 0.0125  */
#define Q31_0_02    42949673  /* 0.02  */
#define Q31_0_17    365072220 /* 0.17  */
#define Q31_0_23    493921239 /* 0.23  */
/*-----------------------------------------------------------------*
 * Local functions
 *-----------------------------------------------------------------*/
static Word16 Find_norm_inv( const Word32 ToDivide, Word16 *e_div );
static Word16 Find_bit_alloc_IVAS( const Word32 core_brate, const Word16 GSC_IVAS_mode, const Word16 Diff_len, const Word16 nb_tot_bands, Word16 *bit, Word16 *max_ener_band, Word16 *ener_vec, Word32 *bits_per_bands );
#endif
#ifdef FIX_802__NON_BE_DECODING
static Word32 Find_bit_frac( const Word16 nb_band, const Word16 remaining_bits );
#endif
/*==================================================================================*/
/* FUNCTION : void bands_and_bit_alloc_ivas_fx();							            */
/*----------------------------------------------------------------------------------*/
@@ -81,11 +112,9 @@ void bands_and_bit_alloc_ivas_fx(
    Word32 bits_per_bands[MBANDS_GN_BITALLOC16k]; // Q18
    Word16 w_sum_bit;
    Word16 fzero_val;
#ifndef FIX_802_1137_1137_GSC_IVAS_FXFLT_DECODING
    Word32 mp, mb, nb_bands_adj, bit_adj;
    Word16 nb_pulse_per_band[MBANDS_GN_BITALLOC16k];
#ifdef REMOVE_IVAS_UNUSED_PARAMETERS_WARNING
    (void) GSC_IVAS_mode;
    (void) element_mode;
#endif
#ifdef BASOP_NOGLOB_DECLARE_LOCAL
    Flag Overflow = 0;
@@ -216,6 +245,7 @@ void bands_and_bit_alloc_ivas_fx(
    {
        IF( GSC_IVAS_mode > 0 )
        {
#ifndef FIX_802_1137_1137_GSC_IVAS_FXFLT_DECODING
            SWB_bit_budget = *bit; // Q0
            st_band = 5;

@@ -421,6 +451,10 @@ void bands_and_bit_alloc_ivas_fx(
                    bits_per_bands[i] += sum_bit;
                }
            }
#else
            nb_tot_bands = Find_bit_alloc_IVAS( core_brate, GSC_IVAS_mode, Diff_len, nb_tot_bands, bit, max_ener_band, ener_vec, bits_per_bands );
            nb_bands = nb_tot_bands;
#endif
        }
        ELSE
#endif
@@ -460,8 +494,11 @@ void bands_and_bit_alloc_ivas_fx(

            set32_fx( bits_per_bands, 0, MBANDS_GN );
            /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */
#ifdef FIX_802__NON_BE_DECODING
            bit_fracf = Find_bit_frac( nb_bands, extract_l( SWB_bit_budget ) );
#else
        bit_fracf = L_mult( div_s( 1, nb_bands ), shl( (Word16) SWB_bit_budget, 2 ) ); /* Q18  */

#endif
            nb_tot_bands = sub( nb_bands_max, 6 );
            nb_tot_bands = s_min( nb_tot_bands, 16 );

@@ -642,7 +679,11 @@ void bands_and_bit_alloc_ivas_fx(
            ELSE
#endif
            {
#ifdef FIX_802__NON_BE_DECODING
                bit_fracf = Find_bit_frac( st_band, bit_tmp );
#else
            bit_fracf = L_mult( div_s( 1, st_band ), shl( bit_tmp, 2 ) );              /* Q18  */
#endif
            }
            /*------------------------------------------------------------------------
             * Complete the bit allocation per frequency band
@@ -968,43 +1009,389 @@ static void reajust_bits_fx(
    Word16 bit_bdgt, sum_bit;

    incr = 1;
    IF( end_band < st_band )
    move16();
    if ( LT_16( end_band, st_band ) )
    {
        incr = -1;
        move16();
    }

    IF( bit_bdgt_in < sum_bit_in )
    IF( LT_16( bit_bdgt_in, sum_bit_in ) )
    {
        amount_to_add = -1;
        bit_bdgt = sum_bit_in;
        sum_bit = bit_bdgt_in;
        move16();
        move16();
        move16();
    }
    ELSE
    {
        bit_bdgt = bit_bdgt_in;
        sum_bit = sum_bit_in;
        amount_to_add = 1;
        move16();
        move16();
        move16();
    }

    i = st_band;
    WHILE( bit_bdgt > sum_bit )
    move16();
    WHILE( GT_16( bit_bdgt, sum_bit ) )
    {
        // if (amount_to_add > 0 || (amount_to_add < 0 && bits_per_bands[i] > 1))
        IF( amount_to_add > 0 || ( amount_to_add < 0 && bits_per_bands[i] > 262144 ) )
        /* if (amount_to_add > 0 || (amount_to_add < 0 && bits_per_bands[i] > 1))*/
        test();
        test();
        IF( amount_to_add > 0 || ( amount_to_add < 0 && GT_32( bits_per_bands[i], 262144 ) ) )
        {
            // bits_per_bands[i] += amount_to_add;
            /* bits_per_bands[i] += amount_to_add;*/
            bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( amount_to_add, Q18 ) );
            move32();
            sum_bit = add( sum_bit, abs_s( amount_to_add ) );
        }

        i += incr;
        IF( i == end_band )
        i = add( i, incr );
        IF( EQ_16( i, end_band ) )
        {
            i = st_band;
            move16();
        }
    }

    return;
}
#ifdef FIX_802__NON_BE_DECODING

/*-------------------------------------------------------------------*
 * Find_bit_frac()
 *
 * Computes the fraction of the remaining bit budget to allocate to the bands
 *-------------------------------------------------------------------*/

static Word32 Find_bit_frac(
    const Word16 nb_band,
    const Word16 remaining_bits )
{
    int16_t inv_bandQ15;
    int32_t L_out;

    inv_bandQ15 = 6553;
    move16();
    IF( nb_band == 7 )
    {
        inv_bandQ15 = 4681;
        move16();
    }
    ELSE IF( nb_band == 3 )
    {
        inv_bandQ15 = 10922;
        move16();
    }
    ELSE IF( nb_band == 4 )
    {
        inv_bandQ15 = 8192;
        move16();
    }
    ELSE IF( nb_band == 5 )
    {
        inv_bandQ15 = 6536;
        move16();
    }
    ELSE IF( nb_band == 10 )
    {
        inv_bandQ15 = 3277;
        move16();
    }
    ELSE
    {
#ifdef DEBUGGING
        printf( "1/%d NOT DEFINED in Find_bit_frac\n", nb_band );
#endif
    }
    L_out = L_mult( inv_bandQ15, shl( remaining_bits, 2 ) );

    return ( L_out );
}
#endif
#ifdef FIX_802_1137_1137_GSC_IVAS_FXFLT_DECODING
static Word16 Find_bit_alloc_IVAS(                               /*o: Number of band to encode */
                                   const Word32 core_brate,      /* i  : core bit rate                                         */
                                   const Word16 GSC_IVAS_mode,   /* i  : GSC IVAS mode                                         */
                                   const Word16 Diff_len,        /* i  : Length of the difference signal (before pure spectral)*/
                                   const Word16 nb_tot_bands_in, /* i  : total number of band */
                                   Word16 *bit,                  /* i/o: Number of bit allowed for frequency quantization      */
                                   Word16 *max_ener_band,        /* i/o: Energy based sorted order                             */
                                   Word16 *ener_vec,             /* i/o: Energy per band order                                 */
                                   Word32 *bits_per_bands        /* o  : Number of bit allowed per allowed subband        Q3   */
)
{
    Word32 mp, mb, nb_bands_adj, bit_adj;
    Word16 nb_pulse_per_band[MBANDS_GN_BITALLOC16k];
    Word32 SWB_bit_budget; // Q0 -> Q18
    Word16 i, j, nb_bands_max, st_band, nb_tot_bands_loc, etmp;
    Word32 sum_bit /*Q18*/, bit_fracf /*Q18*/;
    Word16 d_tmp, e_div, tmp16;
    Word32 Ltmp, etmp_32fx;

    SWB_bit_budget = *bit; // Q0
    st_band = 5;
    nb_bands_max = nb_tot_bands_in;

    move16();
    move16();
    move16();

    set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k );

    /* Decide the percentage of bits allocated to LF (between 50-75%) depending of the temporal contribution in GSC */
    /* bit_fracf = ( -0.125f * Diff_len + 76.0f ) / 100; */
    bit_fracf = L_add( Mpy_32_32( -Q31_0_00125, L_shl( Diff_len, Q18 ) ), Q18_0_76 ); /* Q18 */

    /* bit_fracf = check_bounds(bit_fracf, 0.50f, 0.75f); */
    bit_fracf = check_bounds_l( bit_fracf, Q18_0_50, Q18_0_75 ); /* Q18 */

    /* Adjustment of the bitrate between LF and HF base on the content type */
    /* 1 = new GSC bit alloc
               2 = GSC bit alloc for tc frame
               3 = more music like (should not happen often given music is coded with dft) */

    if ( LE_16( GSC_IVAS_mode, 3 ) )
    {
        nb_bands_max = sub( nb_bands_max, 6 );
    }
    IF( EQ_16( GSC_IVAS_mode, 2 ) )
    {
        /* bit_fracf += 0.1f; */
        bit_fracf = L_add( bit_fracf, Q18_0_1 ); /* Q18*/
        nb_bands_max = sub( nb_bands_max, 1 );
    }
    IF( EQ_16( GSC_IVAS_mode, 3 ) )
    {
        /* bit_fracf -= 0.1f; */
        bit_fracf = L_sub( bit_fracf, Q18_0_1 ); /* Q18*/
        nb_bands_max = add( nb_bands_max, 3 );
    }

    /* First find how much we want to share between LF and HF, at low bitrate, a miminum of bits is needed in LF by limitating the number of bands*/
    /* Adjust the number of band based on the content type and bitrate */

    /* nb_bands_adj = 1.0f; */
    nb_bands_adj = Q18_1_0;
    move32();
    IF( EQ_16( GSC_IVAS_mode, 1 ) && LT_32( core_brate, GSC_L_RATE_STG ) )
    {
        /* nb_bands_adj = 0.0125f * SWB_bit_budget - 0.75f;*/
        nb_bands_adj = L_sub( Mpy_32_32( Q31_0_0125, L_shl( SWB_bit_budget, Q18 ) ), Q18_0_75 ); // Q18
    }
    ELSE IF( NE_16( GSC_IVAS_mode, 2 ) && GT_32( core_brate, GSC_H_RATE_STG ) )
    {
        /*nb_bands_adj = 0.02f * SWB_bit_budget - 1.2f;*/
        nb_bands_adj = L_sub( Mpy_32_32( Q31_0_02, L_shl( SWB_bit_budget, Q18 ) ), Q18_1_2 ); // Q18
    }
    /*nb_bands_max = (int16_t)(nb_bands_max * nb_bands_adj + 0.5f);*/

    nb_bands_max = round_fx( Mpy_32_16_1( L_shl( nb_bands_adj, 5 ), shl( nb_bands_max, 10 - 2 ) ) ); /* Q0 */
    nb_bands_max = check_bounds_s( nb_bands_max, 5, nb_tot_bands_in );

    /* bit_fracf *= SWB_bit_budget;*/

    /* At this point bit_fracf has a value below 1.0 */
    bit_fracf = Mpy_32_16_1( L_shl( bit_fracf, 10 ), extract_l( L_shl( SWB_bit_budget, 5 ) ) ); /* (Q(18+10)*Q(0+5) + 1 - 16 =  Q18 */

    /* Estimation of the number of bit used in HF */
    /* with only the first weighting The number of bits in max_ener_band[st_band-1] = 17% of bit_fracf */
    /* mb = .17f * bit_fracf;*/
    mb = Mpy_32_32( Q31_0_17, bit_fracf ); /* Q18 */

    /* mp = 2 * DSR_NB_PULSE;*/
    mp = Q18_2_0xDSR_NB_PULSE;
    move32();

    test();
    IF( LT_32( core_brate, GSC_L_RATE_STG ) && EQ_16( GSC_IVAS_mode, 3 ) )
    {
        /* mp = 1.5f * DSR_NB_PULSE;*/
        mp = Q18_1_5xDSR_NB_PULSE;
        move32();
    }
    ELSE IF( LT_32( core_brate, GSC_L_RATE_STG ) )
    {
        /* mp = DSR_NB_PULSE;*/
        mp = Q18_DSR_NB_PULSE;
        move32();
    }

    /* We want  max_ener_band[st_band] <=  max_ener_band[st_band-1] and  max_ener_band[nb_bands_max-1] <=  max_ener_band[st_band]*/
    /* We will estimate the number of bits to allocate of HF and put the remaining bits, if any, back on LF */
    /* compute the total possible number of band to be coded */

    /* nb_tot_bands = (int16_t)((SWB_bit_budget - bit_fracf) / (mp + (mb - mp) / 2.0f)); */
    d_tmp = Find_norm_inv( L_add( mp, mb ), &e_div );
    Ltmp = Mpy_32_16_1( L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ), d_tmp ); /* Perform mult by 1/den */
    nb_tot_bands_loc = extract_h( L_shl( Ltmp, sub( 1, e_div ) ) );                /* adjust exponent:   1 is to take into account the  / 2.0f, and e_div for the num and den of the division*/
    mp = L_min( mp, mb );

    tmp16 = sub( add( nb_tot_bands_loc, st_band ), nb_bands_max );
    IF( tmp16 > 0 )
    {
        /* bit_adj = ( ( mb + mp ) / 2 ) * ( nb_tot_bands_loc + st_band - nb_bands_max ); */
        bit_adj = Mpy_32_16_1( L_shl( L_add( mb, mp ), 5 ), shl( tmp16, 10 - 1 ) ); /* Q18+5 * Q0+10 + 1 -1 - 16 = Q18 (-1 is to cover for the /2 in the equation) */
        bit_adj = L_max( 0, bit_adj );
        nb_tot_bands_loc = sub( nb_bands_max, st_band );
        /* bit_fracf += bit_adj; */
        bit_fracf = L_add( bit_fracf, bit_adj ); /* Q18 */
    }
    nb_tot_bands_loc = add( nb_tot_bands_loc, st_band );

    /* Allocate bits to LF */
    /* etmp = 0.23f; */
    etmp_32fx = Q31_0_23;
    move32();
    FOR( j = 0; j < st_band; j++ )
    {
        i = j;
        move16();
        max_ener_band[j] = i;
        move16();
        ener_vec[i] = MIN16B;
        move16();
        /* bits_per_bands[j] = etmp * bit_fracf; */
        bits_per_bands[j] = Mpy_32_32( bit_fracf, etmp_32fx ); /* 18 + 31 + 1 - 32 = Q18 */
        move32();
        /* etmp -= 0.015f; */
        etmp_32fx = L_sub( etmp_32fx, Q31_0_015 ); /* Q18 */
    }

    /* SWB_bit_budget -= bit_fracf; */
    SWB_bit_budget = L_sub( L_shl( SWB_bit_budget, Q18 ), bit_fracf ); /* Q0->Q18 */

    /* Find low energy band in HF */
    set16_fx( nb_pulse_per_band, 2, MBANDS_GN_BITALLOC16k );
    FOR( i = st_band + 2; i < nb_tot_bands_loc - 1; i++ )
    {
        test();
        if ( LT_16( ener_vec[i], ener_vec[i - 1] ) && LT_16( ener_vec[i], ener_vec[i + 1] ) ) /* i +1 and i -1 can be considered as 2 ptrs */
        {
            nb_pulse_per_band[i] = 1;
            move16();
        }
    }
    FOR( j = st_band; j < nb_tot_bands_loc; j++ )
    {
        IF( GT_16( j, 6 ) )
        {
            i = maximum_fx( ener_vec, nb_tot_bands_loc, &etmp );
        }
        ELSE
        {
            i = j;
            move16();
        }
        max_ener_band[j] = i;
        move16();
        ener_vec[i] = MIN16B;
        move16();
    }

    /* Recompute the final bit distribution for HF */
    IF( GT_16( nb_tot_bands_loc, st_band ) )
    {
        /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp;  */
        /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
        /* This is not bit exact because of the precision lost */

        /* mb = ( SWB_bit_budget * 2 / ( nb_tot_bands_loc - st_band ) ) - mp; */
        d_tmp = Find_norm_inv( L_deposit_h( sub( nb_tot_bands_loc, st_band ) ), &e_div );
        mb = L_sub( L_shr( Mpy_32_16_1( L_shl( SWB_bit_budget, 1 ), d_tmp ), e_div ), mp ); /* Q18 */

        /* bit_fracf = ( mb - mp ) / ( nb_tot_bands_loc - st_band ); */
        bit_fracf = L_shr( Mpy_32_16_1( L_sub( mb, mp ), d_tmp ), e_div ); /* Q18 */

        mb = L_sub( mb, bit_fracf );
        /* Do the distribution */
        FOR( j = st_band; j < nb_tot_bands_loc; j++ )
        {
            bits_per_bands[max_ener_band[j]] = Q18_DSR_NB_PULSE;
            move32();
            if ( GT_16( nb_pulse_per_band[max_ener_band[j]], 1 ) )
            {
                bits_per_bands[max_ener_band[j]] = mb;
                move32();
            }
            mb = L_sub( mb, bit_fracf );
            SWB_bit_budget = L_sub( SWB_bit_budget, bits_per_bands[max_ener_band[j]] ); // Q18
        }
    }

    /* Series of verification in case bit allocated != the budget */
    IF( SWB_bit_budget > 0 )
    {
        i = sub( st_band, 1 );
        WHILE( SWB_bit_budget > 0 )
        {
            /* bits_per_bands[i]++; */
            bits_per_bands[i] = L_add( bits_per_bands[i], Q18_1_0 );
            move32();
            /* SWB_bit_budget--; */
            SWB_bit_budget = L_sub( SWB_bit_budget, Q18_1_0 );
            i--;
            if ( EQ_16( i, -1 ) )
            {
                i = sub( st_band, 1 );
            }
        }
    }

    /*nb_bands = nb_tot_bands_loc;*/

    sum_bit = 0;
    move32();
    j = 0;
    move16();
    FOR( i = 0; i < nb_tot_bands_loc; i++ )
    {
        /* if (bits_per_bands[i] > 112) */
        IF( GT_32( bits_per_bands[i], Q18_112 ) )
        {
            /* sum_bit += bits_per_bands[i] - 112; */
            sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], Q18_112 ) );
            /* bits_per_bands[i] = 112; */
            bits_per_bands[i] = Q18_112;
            move32();
            j = add( j, add( i, 1 ) );
        }

        /* safety check for overage bit reallocation */
        /* else if (bits_per_bands[i] + sum_bit / 3 > 112) */
        ELSE IF( GT_32( L_add( bits_per_bands[i], Mpy_32_16_1( sum_bit, Q15_0_33 ) ), Q18_112 ) )
        {
            j = add( j, add( i, 1 ) );
        }
    }

    IF( sum_bit != 0 )
    {
        /* sum_bit /= (nb_bands - j); */
        d_tmp = Find_norm_inv( L_deposit_h( sub( nb_tot_bands_loc, j ) ), &e_div );
        sum_bit = L_shr( Mpy_32_16_1( sum_bit, d_tmp ), e_div ); /* Q18 */
        FOR( i = j; i < nb_tot_bands_loc; i++ )
        {
            bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit );
            move32();
        }
    }
    return nb_tot_bands_loc;
}

static Word16 Find_norm_inv( const Word32 ToDivide, Word16 *e_div ) /* Find normalized 1 / ToDivide */
{
    Word16 d_tmp, e_tmp;
    e_tmp = norm_l( ToDivide );
    d_tmp = round_fx( L_shl( ToDivide, e_tmp ) );
    d_tmp = div_s( 16384, d_tmp ); /* 1.0 in Q14, dividend is normalize so >= 16384 as required for the division */
    *e_div = sub( 14, e_tmp );
    move16();
    return d_tmp;
}
#endif
#endif
+4 −0
Original line number Diff line number Diff line
@@ -149,6 +149,10 @@
#define FIX_798_LSF_SECONDARY_CH_MISSING_CODE  /* Adding the missing code to properly render the secondary channel of TD stereo*/
#define FIX_798_WRONG_CPY_OF_PITCH             /* The copy of the pitch from primary to secondary channel was wrong AND safety check was really wrong */
#define FIX_800_PROPOSAL_HB_LPC_COEFFICIENT     /* Proposal to fix wrong shape of TDBWE, #800 */ 

#define FIX_802__NON_BE_DECODING                    /* Fix possible difference float and fixed point when computing the GSC bit allocation */
#define FIX_802_1137_1137_GSC_IVAS_FXFLT_DECODING   /* VA: ISSUES 802 and 1137 Made sure that float and fixed point GCS bit allocation is the same during IVAS modes  */

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

/* clang-format on */