diff --git a/Workspace_msvc/lib_com.vcxproj b/Workspace_msvc/lib_com.vcxproj index 08d9d5a1ca167808cb11992fd0c33ea8368ec8e2..328ef7d1f51da5c2873dff695c94d2961a3229b8 100644 --- a/Workspace_msvc/lib_com.vcxproj +++ b/Workspace_msvc/lib_com.vcxproj @@ -165,7 +165,6 @@ - diff --git a/Workspace_msvc/lib_com.vcxproj.filters b/Workspace_msvc/lib_com.vcxproj.filters index e4f6aeacd61fb4a1fd15cf1f0254d08c95dbb06e..5dc30014cd98e10a55a21826da3373418eff8b4a 100644 --- a/Workspace_msvc/lib_com.vcxproj.filters +++ b/Workspace_msvc/lib_com.vcxproj.filters @@ -265,12 +265,6 @@ common_all_c - - common_all_c - - - common_all_c - common_all_c @@ -481,6 +475,9 @@ common_all_c + + common_all_c + diff --git a/lib_com/gs_bitallocation_fx.c b/lib_com/gs_bitallocation_fx.c index 77f974cac810e8b78fdc549a0b60092de77ec1a0..d9b9899e4c0add43009764e971bdbd5f5742aa33 100644 --- a/lib_com/gs_bitallocation_fx.c +++ b/lib_com/gs_bitallocation_fx.c @@ -1,6 +1,7 @@ /*==================================================================================== EVS Codec 3GPP TS26.452 Nov 04, 2021. Version 16.4.0 ====================================================================================*/ + #include #include "options.h" /* Compilation switches */ #include "cnst.h" /* Common constants */ @@ -8,6 +9,42 @@ #include "prot_fx.h" /* Function prototypes */ #include "assert.h" /* Debug prototypes */ #include "stl.h" +#include "ivas_prot_fx.h" + + +/*-------------------------------------------------------------------* + * 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 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 ); +static Word16 Find_norm_inv_fx( const Word32 ToDivide, Word16 *e_div ); +static Word16 Find_bit_alloc_IVAS_fx( 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 ); +static Word32 Find_bit_frac_fx( const Word16 nb_band, const Word16 remaining_bits ); + /*==================================================================================*/ /* FUNCTION : void bands_and_bit_alloc_fx(); */ @@ -33,20 +70,25 @@ /*----------------------------------------------------------------------------------*/ /* INPUT/OUTPUT ARGUMENTS : */ /* _ (Word16*) bit :Number of bit allowed for frequency quantization */ -/* _ (Word16*) Ener_per_bd_iQ : Quantized energy vector Q13 */ +/* _ (Word16*) Ener_per_bd_iQ : Quantized energy vector Q12 */ /* _ (Word32*) bits_per_bands : Number of bit allowed per allowed subband Q18 */ /*----------------------------------------------------------------------------------*/ /* RETURN ARGUMENTS : */ /* _ None */ /*==================================================================================*/ + +#ifdef FIX_2467_RENAME_GSC_FUNCTION void bands_and_bit_alloc_fx( +#else +void bands_and_bit_alloc_ivas_fx( +#endif const Word16 cor_strong_limit, /* i : HF correlation */ const Word16 noise_lev, /* i : dwn scaling factor */ const Word32 core_brate, /* i : core bit rate */ const Word16 Diff_len, /* i : Lenght of the difference signal (before pure spectral)*/ const Word16 bits_used, /* i : Number of bit used before frequency Q */ Word16 *bit, /* i/o: Number of bit allowed for frequency quantization */ - const Word16 *Ener_per_bd_iQ, /* i/o: Quantized energy vector */ + const Word16 *Ener_per_bd_iQ, /* i/o: Quantized energy vector Q12 */ Word16 *max_ener_band, /* o : Sorted order */ Word16 *out_bits_per_bands, /* i/o: Number of bit allowed per allowed subband Q3 */ Word16 *nb_subbands, /* o : Number of subband allowed */ @@ -61,22 +103,19 @@ void bands_and_bit_alloc_fx( const Word16 GSC_IVAS_mode /* i : GSC IVAS mode */ ) { - Word16 bandoffset, i, j, nb_bands_max, bit_new_bands, bit_tmp, st_band, nb_bands; Word16 ener_vec[MBANDS_GN_BITALLOC16k]; /*Q12 */ Word16 nb_tot_bands = 16; Word16 bit_index, bit_index_mem, imax; Word32 L_tmp; - Word32 sum_bit, bit_fracf; + Word32 sum_bit /*Q0*/, bit_fracf /*Q18*/; Word16 etmp; Word16 tmp; - Word16 Ener_per_bd_iQ_tmp[MBANDS_GN_BITALLOC16k]; Word16 pos, band; - Word16 SWB_bit_budget; - Word32 bits_per_bands[MBANDS_GN_BITALLOC16k]; + Word16 SWB_bit_budget; /* Q0 */ + Word32 bits_per_bands[MBANDS_GN_BITALLOC16k]; /* Q18 */ Word16 w_sum_bit; - (void) GSC_IVAS_mode; - (void) element_mode; + Word16 fzero_val; nb_tot_bands = MBANDS_GN; move16(); @@ -85,22 +124,22 @@ void bands_and_bit_alloc_fx( nb_tot_bands = MBANDS_GN_BITALLOC16k; move16(); } - Copy( Ener_per_bd_iQ, Ener_per_bd_iQ_tmp, nb_tot_bands ); set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k ); set16_fx( out_bits_per_bands, 0, MBANDS_GN_BITALLOC16k ); /* To adapt current energy band to PVQ freq band for sorting*/ ener_vec[0] = add_sat( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1] ); /*Q12 */ - Copy( Ener_per_bd_iQ_tmp + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */ + move16(); + Copy( Ener_per_bd_iQ + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */ ener_vec[MBANDS_GN - 1] = ener_vec[MBANDS_GN - 2]; move16(); IF( EQ_16( L_frame, L_FRAME16k ) ) { - ener_vec[16] = Ener_per_bd_iQ[16]; - ener_vec[17] = mac_r( L_mult( Ener_per_bd_iQ[16], 16384 ), Ener_per_bd_iQ[17], 16384 ); - ener_vec[18] = Ener_per_bd_iQ[17]; - ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 ); + ener_vec[16] = Ener_per_bd_iQ[16]; /*Q12*/ + ener_vec[17] = mac_r( L_mult( Ener_per_bd_iQ[16], 16384 /*0.5 in Q15*/ ), Ener_per_bd_iQ[17], 16384 /*0.5 in Q15*/ ); /*Q12*/ + ener_vec[18] = Ener_per_bd_iQ[17]; /*Q12*/ + ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 /*0.8 in Q15*/ ); /*Q12*/ move16(); move16(); move16(); @@ -116,27 +155,61 @@ void bands_and_bit_alloc_fx( move16(); bit_new_bands = 5; move16(); - bit_index = i_mult2( BRATE2IDX_fx( core_brate ), 17 ); + + test(); + if ( GT_32( core_brate, ACELP_16k40 ) && EQ_16( L_frame, L_FRAME16k ) ) + { + bit_new_bands = 7; + move16(); + } + + i = 0; + move16(); + WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) ) + { + IF( LE_32( core_brate, brate_intermed_tbl[i] ) ) + { + BREAK; + } + + IF( EQ_32( brate_intermed_tbl[i], ACELP_24k40 ) ) + { + BREAK; + } + + i = add( i, 1 ); + } + + test(); + test(); + test(); + if ( ( element_mode > EVS_MONO ) && EQ_16( coder_type, AUDIO ) && + LE_32( core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */ + { + i = sub( i, 1 ); + } + + bit_index = i_mult2( BRATE2IDX_fx( brate_intermed_tbl[i] ), 17 ); bit_index_mem = bit_index; move16(); test(); test(); - IF( ( EQ_16( coder_type, AUDIO ) || EQ_16( coder_type, INACTIVE ) ) && EQ_16( bwidth, NB ) ) + IF( ( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE ) ) && ( bwidth == NB ) ) { IF( GE_32( core_brate, ACELP_9k60 ) ) { /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - 25; */ - L_tmp = Mult_32_16( core_brate, 20971 ); + L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ ); tmp = extract_l( L_shr_r( L_tmp, 5 ) ); - *bit = sub( sub( tmp, bits_used ), 25 ); + *bit = sub( sub( tmp, bits_used ), 25 ); /*Q0*/ move16(); } ELSE { - L_tmp = Mult_32_16( core_brate, 20971 ); + L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ ); tmp = extract_l( L_shr_r( L_tmp, 5 ) ); - *bit = sub( sub( tmp, bits_used ), 21 ); + *bit = sub( sub( tmp, bits_used ), 21 ); /*Q0*/ move16(); } nb_tot_bands = 10; @@ -146,369 +219,965 @@ void bands_and_bit_alloc_fx( { /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - GSC_freq_bits[bit_index]; */ - L_tmp = Mult_32_16( core_brate, 20971 ); + L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ ); tmp = extract_l( L_shr_r( L_tmp, 5 ) ); *bit = sub( sub( tmp, bits_used ), GSC_freq_bits[bit_index] ); move16(); } - IF( EQ_16( GSC_noisy_speech, 1 ) ) + IF( EQ_16( L_frame, L_FRAME16k ) ) { - SWB_bit_budget = *bit; - move16(); - nb_bands = 5; + *bit = sub( *bit, 8 ); move16(); + } - st_band = nb_bands; + test(); + IF( coder_type == INACTIVE && LE_32( core_brate, GSC_LRES_GAINQ_LIMIT ) ) /* can happen only for 2nd channel inactive */ + { + *bit = add( *bit, GSC_LRES_NB_NITS ); move16(); + } - set32_fx( bits_per_bands, 0, MBANDS_GN ); - /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */ - bit_fracf = L_mult( div_s( 1, nb_bands ), shl( SWB_bit_budget, 2 ) ); /* Q18 */ - - nb_tot_bands = sub( nb_bands_max, 6 ); - nb_tot_bands = s_min( nb_tot_bands, 16 ); - - FOR( j = 0; j < 2; j++ ) + IF( *bit > 0 ) + { + IF( GSC_IVAS_mode > 0 ) { - i = j; + nb_tot_bands = Find_bit_alloc_IVAS_fx( core_brate, GSC_IVAS_mode, Diff_len, nb_tot_bands, bit, max_ener_band, ener_vec, bits_per_bands ); + nb_bands = nb_tot_bands; + } + ELSE IF( EQ_16( GSC_noisy_speech, 1 ) ) + { + SWB_bit_budget = *bit; /*Q0*/ move16(); - max_ener_band[j] = i; + nb_bands = 5; move16(); - ener_vec[i] = 0; + fzero_val = 0; move16(); - } - FOR( ; j < nb_bands; j++ ) - { - i = maximum_fx( ener_vec, nb_tot_bands, &etmp ); - max_ener_band[j] = i; - move16(); - ener_vec[i] = 0; + if ( element_mode > EVS_MONO ) + { + fzero_val = MIN16B; + move16(); + } + + test(); + IF( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO ) + { + nb_bands = 3; + move16(); + + if ( GT_16( SWB_bit_budget, 20 ) ) + { + nb_bands = 5; + move16(); + } + } + ELSE IF( LT_16( bwidth, SWB ) ) + { + nb_bands = 7; + move16(); + } + + st_band = nb_bands; move16(); - } - set32_fx( bits_per_bands, bit_fracf, nb_bands ); - } - ELSE - { - bit_index++; - bit_tmp = sub( *bit, GSC_freq_bits[bit_index] ); - bit_index++; - nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] ); - bit_index++; + set32_fx( bits_per_bands, 0, MBANDS_GN ); + /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */ + bit_fracf = Find_bit_frac_fx( nb_bands, SWB_bit_budget ); /*Q18*/ + nb_tot_bands = sub( nb_bands_max, 6 ); + nb_tot_bands = s_min( nb_tot_bands, 16 ); - *pvq_len = 112; - move16(); - st_band = 7; - move16(); + FOR( j = 0; j < 2; j++ ) + { + i = j; + move16(); + max_ener_band[j] = i; + move16(); + ener_vec[i] = fzero_val; + move16(); + } - IF( LE_32( core_brate, ACELP_9k60 ) ) + IF( LT_16( bwidth, SWB ) ) + { + test(); + if ( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO ) + { + nb_tot_bands = 5; + move16(); + } + + FOR( ; j < nb_bands; j++ ) + { + i = maximum_fx( ener_vec, nb_tot_bands, &etmp ); + max_ener_band[j] = i; + move16(); + ener_vec[i] = fzero_val; + move16(); + } + } + ELSE + { + FOR( ; j < nb_bands; j++ ) + { + i = maximum_fx( ener_vec, nb_tot_bands, &etmp ); + max_ener_band[j] = i; + move16(); + ener_vec[i] = fzero_val; + move16(); + } + } + + set32_fx( bits_per_bands, bit_fracf, nb_bands ); + } + ELSE { - *pvq_len = 80; + bit_index = add( bit_index, 1 ); + bit_tmp = sub( *bit, GSC_freq_bits[bit_index] ); + bit_index = add( bit_index, 1 ); + nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] ); + bit_index = add( bit_index, 1 ); + + *pvq_len = 112; move16(); - st_band = 5; + st_band = 7; move16(); - IF( Diff_len == 0 ) + test(); + IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) ) + { + *pvq_len = 160; + move16(); + st_band = 10; + move16(); + nb_bands = shr( *pvq_len, 4 ); + bit_tmp = sub( bit_tmp, 35 ); + bit_new_bands = 5; + move16(); + } + + IF( LE_32( core_brate, ACELP_9k60 ) ) + { + *pvq_len = 80; + move16(); + st_band = 5; + move16(); + + IF( Diff_len == 0 ) + { + nb_bands_max = add( nb_bands_max, 2 ); + bit_tmp = sub( bit_tmp, 13 ); + } + } + + ELSE IF( Diff_len == 0 ) { nb_bands_max = add( nb_bands_max, 2 ); - bit_tmp = sub( bit_tmp, 13 ); + bit_tmp = sub( bit_tmp, 17 ); } - } - ELSE IF( Diff_len == 0 ) - { - nb_bands_max = add( nb_bands_max, 2 ); - bit_tmp = sub( bit_tmp, 17 ); - } + nb_bands = shr( *pvq_len, 4 ); + nb_bands_max = s_min( nb_bands_max, MBANDS_GN_BITALLOC16k ); - nb_bands = shr( *pvq_len, 4 ); + /*------------------------------------------------------------------------ + * Ajustement of the maximum number of bands in function of the + * dynamics of the spectrum (more or less speech like) + *-----------------------------------------------------------------------*/ + test(); + test(); + test(); + test(); + IF( coder_type == INACTIVE || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) + { + /* Probably classification error -> concentrate bits on LF */ + IF( EQ_16( L_frame, L_FRAME16k ) && GE_32( core_brate, ACELP_24k40 ) ) + { + nb_bands_max = sub( nb_tot_bands, 2 ); + } + ELSE IF( GE_32( core_brate, ACELP_16k40 ) ) + { + nb_bands_max = add( nb_bands, 2 ); + } + ELSE IF( GE_32( core_brate, ACELP_8k00 ) ) + { + nb_bands_max = add( nb_bands, 1 ); + } + ELSE + { + nb_bands_max = nb_bands; + move16(); + } + } + ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP2 ) || + ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) /* Very low dynamic, tend to speech, do not try to code HF at all */ + { + nb_bands_max = sub( nb_bands_max, 2 ); + } + ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */ + { + nb_bands_max = sub( nb_bands_max, 1 ); + } - /*------------------------------------------------------------------------ - * Ajustement of the maximum number of bands in function of the - * dynamics of the spectrum (more or less speech like) - *-----------------------------------------------------------------------*/ - test(); - test(); - test(); - test(); - IF( EQ_16( coder_type, INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) - { - /* Probably classification error -> concentrate bits on LF */ - if ( GE_32( core_brate, ACELP_8k00 ) ) + IF( EQ_16( L_frame, L_FRAME16k ) ) { - nb_bands_max = add( nb_bands, 1 ); + IF( LT_32( core_brate, ACELP_24k40 ) ) + { + nb_bands_max = sub( nb_bands_max, 4 ); + } + ELSE IF( LT_32( core_brate, ACELP_32k ) ) + { + test(); + IF( GT_16( Diff_len, 0 ) || GE_16( noise_lev, NOISE_LEVEL_SP2 ) ) + { + nb_bands_max = sub( nb_bands_max, 2 ); + bit_new_bands = shl( bit_new_bands, 1 ); + } + } + ELSE IF( core_brate >= ACELP_32k ) + { + bit_new_bands = shl( bit_new_bands, 1 ); + } } - else + + test(); + if ( ( bwidth == NB ) && GT_16( nb_bands_max, 10 ) ) { - nb_bands_max = nb_bands; + nb_bands_max = 10; move16(); } - } - ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP2 ) || - ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) /* Very low dynamic, tend to speech, do not try to code HF at all */ - { - nb_bands_max = sub( nb_bands_max, 2 ); - } - ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */ - { - nb_bands_max = sub( nb_bands_max, 1 ); - } - test(); - if ( ( EQ_16( bwidth, NB ) ) && GT_16( nb_bands_max, 10 ) ) - { - nb_bands_max = 10; + /*------------------------------------------------------------------------ + * Find extra number of band to code according to bit rate availables + *-----------------------------------------------------------------------*/ + test(); + WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) ) + { + test(); + bit_tmp = sub( bit_tmp, bit_new_bands ); + nb_bands = add( nb_bands, 1 ); + } + + /*------------------------------------------------------------------------ + * Fractional bits to distribute on the first x bands + *-----------------------------------------------------------------------*/ + + test(); + IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) ) + { + bit_fracf = 0; + move32(); + } + ELSE + { + bit_fracf = Find_bit_frac_fx( st_band, bit_tmp ); /*Q18*/ + } + + /*------------------------------------------------------------------------ + * Complete the bit allocation per frequency band + *-----------------------------------------------------------------------*/ + imax = 5; move16(); - } - /*------------------------------------------------------------------------ - * Find extra number of band to code according to bit rate availables - *-----------------------------------------------------------------------*/ - test(); - WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) ) - { + IF( GT_32( core_brate, ACELP_9k60 ) ) + { + imax = 7; + move16(); + } + FOR( i = 0; i < imax; i++ ) + { + bits_per_bands[i] = L_add( GSC_freq_bits_fx_Q18[bit_index], bit_fracf ); /*Q18*/ + move32(); /* Q18 */ + bit_index = add( bit_index, 1 ); + } + + IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) ) + { + bit_index = 0; + move16(); + i = sub( imax, 1 ); + bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( Compl_GSC_freq_bits[bit_index], Q18 ) ); /*Q18*/ + move32(); + i = add( i, 1 ); + bit_index = add( bit_index, 1 ); + + FOR( ; i < 10; i++ ) + { + bits_per_bands[i] = L_add( bits_per_bands[i], L_add( L_shl( Compl_GSC_freq_bits[bit_index], Q18 ), bit_fracf ) ); /*Q18*/ + move32(); + bit_index = add( bit_index, 1 ); + } + } + + IF( Diff_len == 0 ) + { + bit_index = add( bit_index_mem, 10 ); + FOR( i = 0; i < 7; i++ ) + { + bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx_Q18[bit_index] ); /*Q18*/ + move32(); /*chk Q18 */ + bit_index = add( bit_index, 1 ); + } + } + + IF( bit_fracf < 0 ) + { + FOR( j = 0; j < nb_tot_bands; j++ ) + { + bits_per_bands[j] = L_max( bits_per_bands[j], 0 ); + move32(); + } + } + + /*-------------------------------------------------------------------------- + * Complete the bit allocation per frequency band for 16kHz high brate mode + *--------------------------------------------------------------------------*/ + test(); - bit_tmp = sub( bit_tmp, bit_new_bands ); - nb_bands = add( nb_bands, 1 ); - } + IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) ) + { + FOR( j = st_band; j < nb_bands; j++ ) + { + bits_per_bands[j] = L_shl( bit_new_bands, Q18 ); + move32(); + } - /*------------------------------------------------------------------------ - * Fractional bits to distribute on the first x bands - *-----------------------------------------------------------------------*/ + bit_fracf = L_shl( L_mult0( idiv1616( 16384, nb_bands ), bit_tmp ), 4 ); /*Q18*/ - { - bit_fracf = L_mult( div_s( 1, st_band ), shl( bit_tmp, 2 ) ); /* Q18 */ - } - /*------------------------------------------------------------------------ - * Complete the bit allocation per frequency band - *-----------------------------------------------------------------------*/ - imax = 5; - move16(); + etmp = divide3216( L_shr( bit_fracf, Q2 ), add( nb_bands, 1 ) ); /* Q15 */ - if ( GT_32( core_brate, ACELP_9k60 ) ) - { - imax = 7; + bit_fracf = L_shl( etmp, Q3 ); /* Q18 */ + + FOR( j = nb_bands - 1; j >= 0; j-- ) + { + bits_per_bands[j] = L_add( bits_per_bands[j], L_shl( etmp, Q3 ) ); /* Q18 */ + move32(); + etmp = extract_l( L_add( etmp, L_shr( bit_fracf, Q3 ) ) ); + } + } + ELSE + { + FOR( j = st_band; j < nb_bands; j++ ) + { + bits_per_bands[j] = L_shl( bit_new_bands, 18 ); + move32(); /*chk Q18 */ + } + } + + /*-------------------------------------------------------------------------- + * Compute a maximum band (band offset) for the search on maximal energy + * This is function of the spectral dynamic and the bitrate + *--------------------------------------------------------------------------*/ + + bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) ); + + test(); + test(); + test(); + test(); + test(); + IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) ) + { + bandoffset = sub( bandoffset, 1 ); + } + ELSE IF( ( LE_32( core_brate, ACELP_13k20 ) && ( ( coder_type == INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) ) || + ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) + { + bandoffset = add( bandoffset, 1 ); + } + + bandoffset = s_max( bandoffset, 0 ); + + /*-------------------------------------------------------------------------- + * Initiazed sorted vector + * For the first x bands to be included in th final sorted vector + * Sort the remaining bands in decrease energy order + *--------------------------------------------------------------------------*/ + FOR( j = 0; j < nb_tot_bands; j++ ) + { + max_ener_band[j] = -10; + move16(); + } + FOR( j = 0; j < st_band; j++ ) + { + max_ener_band[j] = j; + move16(); + ener_vec[j] = -10; + move16(); + } + pos = st_band; move16(); - } - FOR( i = 0; i < imax; i++ ) - { - bits_per_bands[i] = L_add( GSC_freq_bits_fx[bit_index], bit_fracf ); - move32(); /* Q18 */ - bit_index = add( bit_index, 1 ); - } + FOR( ; j < nb_bands; j++ ) + { + i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp ); + pos = s_max( pos, i ); + max_ener_band[j] = i; + move16(); + ener_vec[i] = -10; + move16(); + } - IF( Diff_len == 0 ) - { - bit_index = add( bit_index_mem, 10 ); - FOR( i = 0; i < 7; i++ ) + /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */ + test(); + test(); + test(); + IF( GT_16( sub( nb_tot_bands, bandoffset ), nb_bands ) && ( GT_16( pos, 7 ) && EQ_32( core_brate, ACELP_8k00 ) ) && EQ_16( bwidth, WB ) ) { - bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx[bit_index] ); - move32(); /*chk Q18 */ - bit_index = add( bit_index, 1 ); + band = sub( nb_tot_bands, add( bandoffset, nb_bands ) ); + FOR( j = 0; j < band; j++ ) + { + i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp ); + max_ener_band[nb_bands + j] = i; + move16(); + ener_vec[i] = -10; + move16(); + bits_per_bands[nb_bands + j] = 1310720; /*5 in Q18*/ + move32(); /*Q18 */ + } + nb_bands = add( nb_bands, band ); + + bit_tmp = i_mult2( band, 5 ); + + IF( LE_16( band, 2 ) ) + { + FOR( j = st_band - 1; j < nb_bands; j++ ) + { + bits_per_bands[j] = L_add( bits_per_bands[j], 262144 ); /*Q18 */ + move32(); + } + bit_tmp = add( bit_tmp, add( sub( nb_bands, st_band ), 1 ) ); + } + + i = 0; + move16(); + j = 0; + move16(); + FOR( ; bit_tmp > 0; bit_tmp-- ) + { + bits_per_bands[j] = L_sub( bits_per_bands[j], ONE_IN_Q18 ); /*Q18 */ + j = add( j, 1 ); + IF( EQ_16( j, sub( st_band, i ) ) ) + { + j = 0; + move16(); + } + test(); + if ( j == 0 && LT_16( i, sub( st_band, 1 ) ) ) + { + i = add( i, 1 ); + } + } } } /*-------------------------------------------------------------------------- - * Complete the bit allocation per frequency band for 16kHz high brate mode + * Bit sum verification for GSC inactive at very high rate + * The maximum number of bits per band of length 16 is 112 + * Redistribute the overage bits if needed *--------------------------------------------------------------------------*/ + sum_bit = 0; + move16(); + j = 0; + move16(); + FOR( i = 0; i < nb_bands; i++ ) + { + L_tmp = Mult_32_16( sum_bit, 10923 /*1/3 in Q15*/ ); + IF( GT_32( bits_per_bands[i], 29360128 ) ) /* 112 in Q18 */ + { + sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], 29360128 /* 112 in Q18 */ ) ); /* Q18 */ + bits_per_bands[i] = 29360128 /* 112 in Q18 */; + move32(); + j = add( i, 1 ); + } + ELSE IF( GT_32( L_add( bits_per_bands[i], L_tmp ), 29360128 ) ) /* Q18 */ + { + j = add( i, 1 ); + } + } + + IF( sum_bit != 0 ) { - FOR( j = st_band; j < nb_bands; j++ ) + tmp = sub( nb_bands, j ); + sum_bit = Mult_32_16( sum_bit, div_s( 1, tmp ) ); /* Q18 */ + FOR( i = j; i < nb_bands; i++ ) { - bits_per_bands[j] = L_shl( bit_new_bands, 18 ); - move32(); /*chk Q18 */ + bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit ); + move32(); /* Q18 */ } } /*-------------------------------------------------------------------------- - * Compute a maximum band (band offset) for the search on maximal energy - * This is function of the spectral dynamic and the bitrate + * second step of bit sum verification, normally sum_bit == *bit *--------------------------------------------------------------------------*/ - bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) ); + sum_bit = 0; + move32(); + FOR( i = 0; i < nb_bands; i++ ) + { + bits_per_bands[i] = L_shl( L_shr( bits_per_bands[i], Q18 ), Q18 ); + move32(); + sum_bit = L_add( sum_bit, L_shr( bits_per_bands[i], Q18 ) ); /*Q0*/ + } test(); - test(); - test(); - test(); - test(); - IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) ) + IF( GSC_IVAS_mode != 0 && LT_32( sum_bit, *bit ) ) /* If we need to add bits, we are doing it on the LF */ { - bandoffset = sub( bandoffset, 1 ); + reajust_bits_fx( bits_per_bands, 0, nb_bands, (Word16) sum_bit, *bit ); } - ELSE IF( ( LE_32( core_brate, ACELP_13k20 ) && ( EQ_16( coder_type, INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) ) || - ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) + ELSE { - bandoffset = add( bandoffset, 1 ); + reajust_bits_fx( bits_per_bands, sub( nb_bands, 1 ), 0, (Word16) sum_bit, *bit ); + } + w_sum_bit = 0; + move16(); + FOR( i = 0; i < nb_bands; i++ ) + { + out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 ); + move16(); + w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */ } + tmp = shl( *bit, 3 ); - bandoffset = s_max( bandoffset, 0 ); + /*-------------------------------------------------------------------------- + * Recompute the real number/length of frequency bands to encode + *--------------------------------------------------------------------------*/ + *nb_subbands = nb_bands; + move16(); + *pvq_len = shl( *nb_subbands, 4 ); + move16(); /*-------------------------------------------------------------------------- - * Initiazed sorted vector - * For the first x bands to be included in th final sorted vector - * Sort the remaining bands in decrease energy order + * Concatenate bands (encoder only) *--------------------------------------------------------------------------*/ - FOR( j = 0; j < nb_tot_bands; j++ ) + IF( exc_diff != NULL ) { - max_ener_band[j] = -10; - move16(); + FOR( j = 0; j < nb_bands; j++ ) + { + Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 ); + } } - FOR( j = 0; j < st_band; j++ ) + } + ELSE /* *bit == 0 */ + { + set16_fx( out_bits_per_bands, 0, nb_tot_bands ); + *nb_subbands = 0; + move16(); + *pvq_len = 0; + move16(); + } + + return; +} + + +/*-------------------------------------------------------------------* + * reajust_bits() + * + * + *-------------------------------------------------------------------*/ + +static void reajust_bits_fx( + Word32 *bits_per_bands, /*i : Q18*/ + const Word16 st_band, + const Word16 end_band, + const Word16 sum_bit_in, /*i : Q0*/ + const Word16 bit_bdgt_in /*i : Q0*/ ) +{ + Word16 i, amount_to_add, incr; + Word16 bit_bdgt, sum_bit; + + incr = 1; + move16(); + if ( LT_16( end_band, st_band ) ) + { + incr = -1; + move16(); + } + + IF( LT_16( bit_bdgt_in, sum_bit_in ) ) + { + amount_to_add = -1; + move16(); + bit_bdgt = sum_bit_in; + move16(); + sum_bit = bit_bdgt_in; + move16(); + } + ELSE + { + bit_bdgt = bit_bdgt_in; + move16(); + sum_bit = sum_bit_in; + move16(); + amount_to_add = 1; + move16(); + } + + i = st_band; + move16(); + WHILE( GT_16( bit_bdgt, sum_bit ) ) + { + /* 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], ONE_IN_Q18 ) ) ) { - max_ener_band[j] = j; - move16(); - ener_vec[j] = -10; - move16(); + /* bits_per_bands[i] += amount_to_add;*/ + bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( amount_to_add, Q18 ) ); /*Q18*/ + move32(); + sum_bit = add( sum_bit, abs_s( amount_to_add ) ); } - pos = st_band; - move16(); - FOR( ; j < nb_bands; j++ ) + + i = add( i, incr ); + if ( EQ_16( i, end_band ) ) { - i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp ); - pos = s_max( pos, i ); - max_ener_band[j] = i; - move16(); - ener_vec[i] = -10; + i = st_band; move16(); } + } - /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */ - test(); - test(); - test(); - IF( GT_16( sub( nb_tot_bands, bandoffset ), nb_bands ) && ( GT_16( pos, 7 ) && EQ_32( core_brate, ACELP_8k00 ) ) && EQ_16( bwidth, WB ) ) - { - band = sub( nb_tot_bands, add( bandoffset, nb_bands ) ); - FOR( j = 0; j < band; j++ ) - { - i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp ); - max_ener_band[add( nb_bands, j )] = i; - move16(); - ener_vec[i] = -10; - move16(); - bits_per_bands[add( nb_bands, j )] = 1310720; - move32(); /*Q18 */ - } - nb_bands = add( nb_bands, band ); + return; +} - bit_tmp = i_mult2( band, 5 ); +/*-------------------------------------------------------------------* + * Find_bit_frac() + * + * Computes the fraction of the remaining bit budget to allocate to the bands + *-------------------------------------------------------------------*/ - IF( LE_16( band, 2 ) ) - { - FOR( j = sub( st_band, 1 ); j < nb_bands; j++ ) - { - bits_per_bands[j] = L_add( bits_per_bands[j], 262144 ); /*Q18 */ - move32(); - } - bit_tmp = add( bit_tmp, add( sub( nb_bands, st_band ), 1 ) ); - } +static Word32 Find_bit_frac_fx( + const Word16 nb_band, + const Word16 remaining_bits ) +{ + Word16 inv_bandQ15; + Word32 L_out; - i = 0; - move16(); - j = 0; - move16(); - FOR( ; bit_tmp > 0; bit_tmp-- ) - { - bits_per_bands[j] = L_sub( bits_per_bands[j], 262144 ); /*Q18 */ - j = add( j, 1 ); - if ( EQ_16( j, sub( st_band, i ) ) ) - { - j = 0; - move16(); - } - test(); - if ( j == 0 && LT_16( i, sub( st_band, 1 ) ) ) - { - i = add( i, 1 ); - } - } - } + inv_bandQ15 = 6553; /*Q15*/ + move16(); + IF( EQ_16( nb_band, 7 ) ) + { + inv_bandQ15 = 4681; /*Q15*/ + move16(); } - /*-------------------------------------------------------------------------- - * Bit sum verification for GSC inactive at very high rate - * The maximum number of bits per band of length 16 is 112 - * Redistribute the overage bits if needed - *--------------------------------------------------------------------------*/ - sum_bit = 0; + ELSE IF( EQ_16( nb_band, 3 ) ) + { + inv_bandQ15 = 10922; /*Q15*/ + move16(); + } + ELSE IF( EQ_16( nb_band, 4 ) ) + { + inv_bandQ15 = 8192; /*Q15*/ + move16(); + } + ELSE IF( EQ_16( nb_band, 5 ) ) + { + inv_bandQ15 = 6536; /*Q15*/ + move16(); + } + ELSE IF( EQ_16( nb_band, 10 ) ) + { + inv_bandQ15 = 3277; /*Q15*/ + 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 ) ); /*Q18*/ + + return ( L_out ); +} + + +/* o : Number of band to encode */ +static Word16 Find_bit_alloc_IVAS_fx( + 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 Q18 */ +) +{ + Word32 mp, mb, nb_bands_adj, bit_adj; + Word16 nb_pulse_per_band[MBANDS_GN_BITALLOC16k]; + Word32 SWB_bit_budgetQ5; /* Q0 -> Q5 */ + Word32 SWB_bit_budgetQ18; /* 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_budgetQ18 = L_shl( *bit, 18 ); /* Q0 -> Q18 */ + SWB_bit_budgetQ5 = L_shl( *bit, 5 ); /* Q0 -> Q5 */ + st_band = 5; + nb_bands_max = nb_tot_bands_in; + + move16(); move16(); - j = 0; move16(); - FOR( i = 0; i < nb_bands; i++ ) + + 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(); + test(); + test(); + 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, SWB_bit_budgetQ18 ), 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, SWB_bit_budgetQ18 ), 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_fx( 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( SWB_bit_budgetQ5 ) ); /* (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_fx( L_add( mp, mb ), &e_div ); + Ltmp = Mpy_32_16_1( L_sub( SWB_bit_budgetQ18, 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++ ) { - L_tmp = Mult_32_16( sum_bit, 10923 ); + 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 */ + } - IF( GT_32( bits_per_bands[i], 29360128 ) ) /* 112 in Q18 */ + /* SWB_bit_budget -= bit_fracf; */ + SWB_bit_budgetQ18 = L_sub( SWB_bit_budgetQ18, 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 */ { - sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], 29360128 ) ); /* Q18 */ - bits_per_bands[i] = 29360128; - move32(); - j = add( i, 1 ); + nb_pulse_per_band[i] = 1; + move16(); } - ELSE IF( GT_32( L_add( bits_per_bands[i], L_tmp ), 29360128 ) ) /* Q18 */ + } + 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 { - j = add( i, 1 ); + i = j; + move16(); } + max_ener_band[j] = i; + move16(); + ener_vec[i] = MIN16B; + move16(); } - IF( sum_bit != 0 ) + /* Recompute the final bit distribution for HF */ + IF( GT_16( nb_tot_bands_loc, st_band ) ) { - tmp = sub( nb_bands, j ); - sum_bit = Mult_32_16( sum_bit, div_s( 1, tmp ) ); /* Q18 */ - FOR( i = j; i < nb_bands; i++ ) + /* 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_fx( 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_budgetQ18, 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[i] = L_add( bits_per_bands[i], sum_bit ); - move32(); /* Q18 */ + 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_budgetQ18 = L_sub( SWB_bit_budgetQ18, bits_per_bands[max_ener_band[j]] ); /* Q18 */ } } - /*-------------------------------------------------------------------------- - * second step of bit sum verification, normally sum_bit == *bit - *--------------------------------------------------------------------------*/ - w_sum_bit = 0; - move16(); - FOR( i = 0; i < nb_bands; i++ ) - { - out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 ); - move16(); - w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */ - } - tmp = shl( *bit, 3 ); - IF( GT_16( tmp, w_sum_bit ) ) + /* Series of verification in case bit allocated != the budget */ + IF( SWB_bit_budgetQ18 > 0 ) { - i = sub( nb_bands, 1 ); - move16(); - FOR( ; tmp > w_sum_bit; w_sum_bit += ( 1 << 3 ) ) + i = sub( st_band, 1 ); + WHILE( SWB_bit_budgetQ18 > 0 ) { - out_bits_per_bands[i] = add( out_bits_per_bands[i], 1 << 3 ); - move16(); + /* bits_per_bands[i]++; */ + bits_per_bands[i] = L_add( bits_per_bands[i], Q18_1_0 ); /*Q18*/ + move32(); + /* SWB_bit_budget--; */ + SWB_bit_budgetQ18 = L_sub( SWB_bit_budgetQ18, Q18_1_0 ); /*Q18*/ i = sub( i, 1 ); - if ( i == 0 ) + if ( EQ_16( i, -1 ) ) { - i = sub( nb_bands, 1 ); + i = sub( st_band, 1 ); } } } - /*-------------------------------------------------------------------------- - * Recompute the real number/length of frequency bands to encode - *--------------------------------------------------------------------------*/ - *nb_subbands = nb_bands; + /*nb_bands = nb_tot_bands_loc;*/ + + sum_bit = 0; + move32(); + j = 0; move16(); - *pvq_len = shl( *nb_subbands, 4 ); + 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 ) ); + } - /*-------------------------------------------------------------------------- - * Concatenate bands (encoder only) - *--------------------------------------------------------------------------*/ - IF( exc_diff != NULL ) + /* 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 ) { - FOR( j = 0; j < nb_bands; j++ ) + /* sum_bit /= (nb_bands - j); */ + d_tmp = Find_norm_inv_fx( 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++ ) { - Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 ); + bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit ); + move32(); } } - return; + return nb_tot_bands_loc; +} + + +/* Find normalized 1 / ToDivide */ +static Word16 Find_norm_inv_fx( + const Word32 ToDivide, + Word16 *e_div ) +{ + 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; } diff --git a/lib_com/gs_bitallocation_ivas_fx.c b/lib_com/gs_bitallocation_ivas_fx.c deleted file mode 100644 index 5d9516d2cf459f38294d726ea437ead5558cfe85..0000000000000000000000000000000000000000 --- a/lib_com/gs_bitallocation_ivas_fx.c +++ /dev/null @@ -1,1170 +0,0 @@ -/*==================================================================================== - EVS Codec 3GPP TS26.452 Nov 04, 2021. Version 16.4.0 - ====================================================================================*/ -#include -#include "options.h" /* Compilation switches */ -#include "cnst.h" /* Common constants */ -#include "rom_com.h" /* Static table prototypes */ -#include "prot_fx.h" /* Function prototypes */ -#include "assert.h" /* Debug prototypes */ -#include "stl.h" -#include "ivas_prot_fx.h" - -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 ); - -/*-------------------------------------------------------------------* - * 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_fx( const Word32 ToDivide, Word16 *e_div ); -static Word16 Find_bit_alloc_IVAS_fx( 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 ); -static Word32 Find_bit_frac_fx( const Word16 nb_band, const Word16 remaining_bits ); -/*==================================================================================*/ -/* FUNCTION : void bands_and_bit_alloc_ivas_fx(); */ -/*----------------------------------------------------------------------------------*/ -/* PURPOSE : AC mode (GSC) bands and bits allocation */ -/*----------------------------------------------------------------------------------*/ -/* INPUT ARGUMENTS : */ -/* _ (Word16) cor_strong_limit : HF correlation */ -/* _ (Word16) noise_lev : dwn scaling factor Q0 */ -/* _ (Word32) core_brate : core codec used Q0 */ -/* _ (Word16) Diff_len : Lenght of the difference signal Q0 */ -/* _ (Word16) bits_used : Number of bit used before frequency Q0 */ -/* _ (Word16) idx : Energy band 14 Q0 */ -/* _ (Word16*) exc_diff : Difference signal to quantize (Encoder only) */ -/* _ (Word16) coder_type : coding type Q0 */ -/* _ (Word16) bwidth : input signal bandwidth Q0 */ -/*----------------------------------------------------------------------------------*/ -/* OUTPUT ARGUMENTS : */ -/* _ (Word16*) max_ener_band : Sorted order */ -/* _ (Word16*) nb_subbands : Number of subband allowed Q0 */ -/* _ (Word16*) concat_in : Concatened PVQ's input vector (Encoder Only) */ -/* _ (Word16*) pvq_len : Number of bin covered with the PVQ Q0 */ -/*----------------------------------------------------------------------------------*/ -/* INPUT/OUTPUT ARGUMENTS : */ -/* _ (Word16*) bit :Number of bit allowed for frequency quantization */ -/* _ (Word16*) Ener_per_bd_iQ : Quantized energy vector Q12 */ -/* _ (Word32*) bits_per_bands : Number of bit allowed per allowed subband Q18 */ -/*----------------------------------------------------------------------------------*/ -/* RETURN ARGUMENTS : */ -/* _ None */ -/*==================================================================================*/ -void bands_and_bit_alloc_ivas_fx( - const Word16 cor_strong_limit, /* i : HF correlation */ - const Word16 noise_lev, /* i : dwn scaling factor */ - const Word32 core_brate, /* i : core bit rate */ - const Word16 Diff_len, /* i : Lenght of the difference signal (before pure spectral)*/ - const Word16 bits_used, /* i : Number of bit used before frequency Q */ - Word16 *bit, /* i/o: Number of bit allowed for frequency quantization */ - const Word16 *Ener_per_bd_iQ, /* i/o: Quantized energy vector Q12 */ - Word16 *max_ener_band, /* o : Sorted order */ - Word16 *out_bits_per_bands, /* i/o: Number of bit allowed per allowed subband Q3 */ - Word16 *nb_subbands, /* o : Number of subband allowed */ - const Word16 *exc_diff, /* i : Difference signal to quantize (encoder side only) */ - Word16 *concat_in, /* o : Concatened PVQ's input vector (encoder side only) */ - Word16 *pvq_len, /* o : Number of bin covered with the PVQ */ - const Word16 coder_type, /* i : coding type */ - const Word16 bwidth, /* i : input signal bandwidth */ - const Word16 GSC_noisy_speech, /* i : GSC noisy speech flag */ - const Word16 L_frame, /* i : frame length */ - const Word16 element_mode, /* i : element mode */ - const Word16 GSC_IVAS_mode /* i : GSC IVAS mode */ -) -{ - Word16 bandoffset, i, j, nb_bands_max, bit_new_bands, bit_tmp, st_band, nb_bands; - Word16 ener_vec[MBANDS_GN_BITALLOC16k]; /*Q12 */ - Word16 nb_tot_bands = 16; - Word16 bit_index, bit_index_mem, imax; - Word32 L_tmp; - Word32 sum_bit /*Q0*/, bit_fracf /*Q18*/; - Word16 etmp; - Word16 tmp; - Word16 pos, band; - Word32 SWB_bit_budget; /* Q0 -> Q18 */ - Word32 bits_per_bands[MBANDS_GN_BITALLOC16k]; /* Q18 */ - Word16 w_sum_bit; - Word16 fzero_val; - - nb_tot_bands = MBANDS_GN; - move16(); - if ( EQ_16( L_frame, L_FRAME16k ) ) - { - nb_tot_bands = MBANDS_GN_BITALLOC16k; - move16(); - } - - set32_fx( bits_per_bands, 0, MBANDS_GN_BITALLOC16k ); - set16_fx( out_bits_per_bands, 0, MBANDS_GN_BITALLOC16k ); - - /* To adapt current energy band to PVQ freq band for sorting*/ - ener_vec[0] = add_sat( Ener_per_bd_iQ[0], Ener_per_bd_iQ[1] ); /*Q12 */ - move16(); - Copy( Ener_per_bd_iQ + 1, ener_vec, MBANDS_GN - 1 ); /*Q12 */ - ener_vec[MBANDS_GN - 1] = ener_vec[MBANDS_GN - 2]; - move16(); - IF( EQ_16( L_frame, L_FRAME16k ) ) - { - ener_vec[16] = Ener_per_bd_iQ[16]; /*Q12*/ - ener_vec[17] = mac_r( L_mult( Ener_per_bd_iQ[16], 16384 /*0.5 in Q15*/ ), Ener_per_bd_iQ[17], 16384 /*0.5 in Q15*/ ); /*Q12*/ - ener_vec[18] = Ener_per_bd_iQ[17]; /*Q12*/ - ener_vec[19] = mult_r( Ener_per_bd_iQ[17], 26214 /*0.8 in Q15*/ ); /*Q12*/ - move16(); - move16(); - move16(); - move16(); - } - - /*------------------------------------------------------------------------ - * Determination of the number of bits available to the frequency domain - * Allocation of a maximum number of band to be encoded - *-----------------------------------------------------------------------*/ - - nb_bands_max = nb_tot_bands; - move16(); - bit_new_bands = 5; - move16(); - - test(); - if ( GT_32( core_brate, ACELP_16k40 ) && EQ_16( L_frame, L_FRAME16k ) ) - { - bit_new_bands = 7; - move16(); - } - - i = 0; - move16(); - WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) ) - { - IF( LE_32( core_brate, brate_intermed_tbl[i] ) ) - { - BREAK; - } - - IF( EQ_32( brate_intermed_tbl[i], ACELP_24k40 ) ) - { - BREAK; - } - - i = add( i, 1 ); - } - - test(); - test(); - test(); - if ( ( element_mode > EVS_MONO ) && EQ_16( coder_type, AUDIO ) && - LE_32( core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation should be mapped to 8 kb/s instead of 9.6 kb/s in this case */ - { - i = sub( i, 1 ); - } - - bit_index = i_mult2( BRATE2IDX_fx( brate_intermed_tbl[i] ), 17 ); - bit_index_mem = bit_index; - move16(); - - test(); - test(); - IF( ( EQ_16( coder_type, AUDIO ) || ( coder_type == INACTIVE ) ) && ( bwidth == NB ) ) - { - IF( GE_32( core_brate, ACELP_9k60 ) ) - { - /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - 25; */ - L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ ); - tmp = extract_l( L_shr_r( L_tmp, 5 ) ); - *bit = sub( sub( tmp, bits_used ), 25 ); /*Q0*/ - move16(); - } - ELSE - { - L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ ); - tmp = extract_l( L_shr_r( L_tmp, 5 ) ); - *bit = sub( sub( tmp, bits_used ), 21 ); /*Q0*/ - move16(); - } - nb_tot_bands = 10; - move16(); - } - ELSE - { - /* *bit = (short)(core_brate*(1.0f/50) + 0.5f) - bits_used - GSC_freq_bits[bit_index]; */ - - L_tmp = Mult_32_16( core_brate, 20971 /*1/50 in Q20*/ ); - tmp = extract_l( L_shr_r( L_tmp, 5 ) ); - *bit = sub( sub( tmp, bits_used ), GSC_freq_bits[bit_index] ); - move16(); - } - - IF( EQ_16( L_frame, L_FRAME16k ) ) - { - *bit = sub( *bit, 8 ); - move16(); - } - - test(); - IF( coder_type == INACTIVE && LE_32( core_brate, GSC_LRES_GAINQ_LIMIT ) ) /* can happen only for 2nd channel inactive */ - { - *bit = add( *bit, GSC_LRES_NB_NITS ); - move16(); - } - - IF( *bit > 0 ) - { - IF( GSC_IVAS_mode > 0 ) - { - nb_tot_bands = Find_bit_alloc_IVAS_fx( core_brate, GSC_IVAS_mode, Diff_len, nb_tot_bands, bit, max_ener_band, ener_vec, bits_per_bands ); - nb_bands = nb_tot_bands; - } - ELSE IF( EQ_16( GSC_noisy_speech, 1 ) ) - { - SWB_bit_budget = *bit; /*Q0*/ - move32(); - nb_bands = 5; - move16(); - fzero_val = 0; - move16(); - - if ( element_mode > EVS_MONO ) - { - fzero_val = MIN16B; - move16(); - } - - test(); - IF( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO ) - { - nb_bands = 3; - move16(); - - if ( GT_32( SWB_bit_budget, 20 ) ) - { - nb_bands = 5; - move16(); - } - } - ELSE IF( LT_16( bwidth, SWB ) ) - { - nb_bands = 7; - move16(); - } - - st_band = nb_bands; - move16(); - - set32_fx( bits_per_bands, 0, MBANDS_GN ); - /*bit_fracf = (1.0f/nb_bands)*(SWB_bit_budget); */ - bit_fracf = Find_bit_frac_fx( nb_bands, extract_l( SWB_bit_budget ) ); /*Q18*/ - nb_tot_bands = sub( nb_bands_max, 6 ); - nb_tot_bands = s_min( nb_tot_bands, 16 ); - - FOR( j = 0; j < 2; j++ ) - { - i = j; - move16(); - max_ener_band[j] = i; - move16(); - ener_vec[i] = fzero_val; - move16(); - } - - IF( LT_16( bwidth, SWB ) ) - { - test(); - if ( EQ_16( coder_type, UNVOICED ) && element_mode > EVS_MONO ) - { - nb_tot_bands = 5; - move16(); - } - - FOR( ; j < nb_bands; j++ ) - { - i = maximum_fx( ener_vec, nb_tot_bands, &etmp ); - max_ener_band[j] = i; - move16(); - ener_vec[i] = fzero_val; - move16(); - } - } - ELSE - { - FOR( ; j < nb_bands; j++ ) - { - i = maximum_fx( ener_vec, nb_tot_bands, &etmp ); - max_ener_band[j] = i; - move16(); - ener_vec[i] = fzero_val; - move16(); - } - } - - set32_fx( bits_per_bands, bit_fracf, nb_bands ); - } - ELSE - { - bit_index = add( bit_index, 1 ); - bit_tmp = sub( *bit, GSC_freq_bits[bit_index] ); - bit_index = add( bit_index, 1 ); - nb_bands_max = add( nb_bands_max, GSC_freq_bits[bit_index] ); - bit_index = add( bit_index, 1 ); - - *pvq_len = 112; - move16(); - st_band = 7; - move16(); - - test(); - IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) ) - { - *pvq_len = 160; - move16(); - st_band = 10; - move16(); - nb_bands = shr( *pvq_len, 4 ); - bit_tmp = sub( bit_tmp, 35 ); - bit_new_bands = 5; - move16(); - } - - IF( LE_32( core_brate, ACELP_9k60 ) ) - { - *pvq_len = 80; - move16(); - st_band = 5; - move16(); - - IF( Diff_len == 0 ) - { - nb_bands_max = add( nb_bands_max, 2 ); - bit_tmp = sub( bit_tmp, 13 ); - } - } - - ELSE IF( Diff_len == 0 ) - { - nb_bands_max = add( nb_bands_max, 2 ); - bit_tmp = sub( bit_tmp, 17 ); - } - - nb_bands = shr( *pvq_len, 4 ); - nb_bands_max = s_min( nb_bands_max, MBANDS_GN_BITALLOC16k ); - - /*------------------------------------------------------------------------ - * Ajustement of the maximum number of bands in function of the - * dynamics of the spectrum (more or less speech like) - *-----------------------------------------------------------------------*/ - test(); - test(); - test(); - test(); - IF( coder_type == INACTIVE || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) - { - /* Probably classification error -> concentrate bits on LF */ - IF( EQ_16( L_frame, L_FRAME16k ) && GE_32( core_brate, ACELP_24k40 ) ) - { - nb_bands_max = sub( nb_tot_bands, 2 ); - } - ELSE IF( GE_32( core_brate, ACELP_16k40 ) ) - { - nb_bands_max = add( nb_bands, 2 ); - } - ELSE IF( GE_32( core_brate, ACELP_8k00 ) ) - { - nb_bands_max = add( nb_bands, 1 ); - } - ELSE - { - nb_bands_max = nb_bands; - move16(); - } - } - ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP2 ) || - ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) /* Very low dynamic, tend to speech, do not try to code HF at all */ - { - nb_bands_max = sub( nb_bands_max, 2 ); - } - ELSE IF( GE_16( noise_lev, NOISE_LEVEL_SP1 ) ) /* Very low dynamic, tend to speech, code less HF */ - { - nb_bands_max = sub( nb_bands_max, 1 ); - } - - IF( EQ_16( L_frame, L_FRAME16k ) ) - { - IF( LT_32( core_brate, ACELP_24k40 ) ) - { - nb_bands_max = sub( nb_bands_max, 4 ); - } - ELSE IF( LT_32( core_brate, ACELP_32k ) ) - { - test(); - IF( GT_16( Diff_len, 0 ) || GE_16( noise_lev, NOISE_LEVEL_SP2 ) ) - { - nb_bands_max = sub( nb_bands_max, 2 ); - bit_new_bands = shl( bit_new_bands, 1 ); - } - } - ELSE IF( core_brate >= ACELP_32k ) - { - bit_new_bands = shl( bit_new_bands, 1 ); - } - } - - test(); - if ( ( bwidth == NB ) && GT_16( nb_bands_max, 10 ) ) - { - nb_bands_max = 10; - move16(); - } - - /*------------------------------------------------------------------------ - * Find extra number of band to code according to bit rate availables - *-----------------------------------------------------------------------*/ - test(); - WHILE( GE_16( bit_tmp, bit_new_bands ) && LE_16( nb_bands, sub( nb_bands_max, 1 ) ) ) - { - test(); - bit_tmp = sub( bit_tmp, bit_new_bands ); - nb_bands = add( nb_bands, 1 ); - } - - /*------------------------------------------------------------------------ - * Fractional bits to distribute on the first x bands - *-----------------------------------------------------------------------*/ - - test(); - IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) ) - { - bit_fracf = 0; - move32(); - } - ELSE - { - bit_fracf = Find_bit_frac_fx( st_band, bit_tmp ); /*Q18*/ - } - - /*------------------------------------------------------------------------ - * Complete the bit allocation per frequency band - *-----------------------------------------------------------------------*/ - imax = 5; - move16(); - - IF( GT_32( core_brate, ACELP_9k60 ) ) - { - imax = 7; - move16(); - } - FOR( i = 0; i < imax; i++ ) - { - bits_per_bands[i] = L_add( GSC_freq_bits_fx_Q18[bit_index], bit_fracf ); /*Q18*/ - move32(); /* Q18 */ - bit_index = add( bit_index, 1 ); - } - - IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_16k40 ) ) - { - bit_index = 0; - move16(); - i = sub( imax, 1 ); - bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( Compl_GSC_freq_bits[bit_index], Q18 ) ); /*Q18*/ - move32(); - i = add( i, 1 ); - bit_index = add( bit_index, 1 ); - - FOR( ; i < 10; i++ ) - { - bits_per_bands[i] = L_add( bits_per_bands[i], L_add( L_shl( Compl_GSC_freq_bits[bit_index], Q18 ), bit_fracf ) ); /*Q18*/ - move32(); - bit_index = add( bit_index, 1 ); - } - } - - IF( Diff_len == 0 ) - { - bit_index = add( bit_index_mem, 10 ); - FOR( i = 0; i < 7; i++ ) - { - bits_per_bands[i] = L_add( bits_per_bands[i], GSC_freq_bits_fx_Q18[bit_index] ); /*Q18*/ - move32(); /*chk Q18 */ - bit_index = add( bit_index, 1 ); - } - } - - IF( bit_fracf < 0 ) - { - FOR( j = 0; j < nb_tot_bands; j++ ) - { - bits_per_bands[j] = L_max( bits_per_bands[j], 0 ); - move32(); - } - } - - /*-------------------------------------------------------------------------- - * Complete the bit allocation per frequency band for 16kHz high brate mode - *--------------------------------------------------------------------------*/ - - test(); - IF( EQ_16( L_frame, L_FRAME16k ) && GT_32( core_brate, ACELP_32k ) ) - { - FOR( j = st_band; j < nb_bands; j++ ) - { - bits_per_bands[j] = L_shl( bit_new_bands, Q18 ); - move32(); - } - - bit_fracf = L_shl( L_mult0( idiv1616( 16384, nb_bands ), bit_tmp ), 4 ); /*Q18*/ - - etmp = divide3216( L_shr( bit_fracf, Q2 ), add( nb_bands, 1 ) ); /* Q15 */ - - bit_fracf = L_shl( etmp, Q3 ); /* Q18 */ - - FOR( j = nb_bands - 1; j >= 0; j-- ) - { - bits_per_bands[j] = L_add( bits_per_bands[j], L_shl( etmp, Q3 ) ); /* Q18 */ - move32(); - etmp = extract_l( L_add( etmp, L_shr( bit_fracf, Q3 ) ) ); - } - } - ELSE - { - FOR( j = st_band; j < nb_bands; j++ ) - { - bits_per_bands[j] = L_shl( bit_new_bands, 18 ); - move32(); /*chk Q18 */ - } - } - - /*-------------------------------------------------------------------------- - * Compute a maximum band (band offset) for the search on maximal energy - * This is function of the spectral dynamic and the bitrate - *--------------------------------------------------------------------------*/ - - bandoffset = sub( nb_tot_bands, add( nb_bands, 2 ) ); - - test(); - test(); - test(); - test(); - test(); - IF( LE_16( noise_lev, NOISE_LEVEL_SP1a ) ) - { - bandoffset = sub( bandoffset, 1 ); - } - ELSE IF( ( LE_32( core_brate, ACELP_13k20 ) && ( ( coder_type == INACTIVE ) || GE_16( noise_lev, NOISE_LEVEL_SP3 ) ) ) || - ( LE_32( core_brate, ACELP_13k20 ) && GE_32( core_brate, ACELP_9k60 ) && cor_strong_limit == 0 ) ) - { - bandoffset = add( bandoffset, 1 ); - } - - bandoffset = s_max( bandoffset, 0 ); - - /*-------------------------------------------------------------------------- - * Initiazed sorted vector - * For the first x bands to be included in th final sorted vector - * Sort the remaining bands in decrease energy order - *--------------------------------------------------------------------------*/ - FOR( j = 0; j < nb_tot_bands; j++ ) - { - max_ener_band[j] = -10; - move16(); - } - FOR( j = 0; j < st_band; j++ ) - { - max_ener_band[j] = j; - move16(); - ener_vec[j] = -10; - move16(); - } - pos = st_band; - move16(); - FOR( ; j < nb_bands; j++ ) - { - i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp ); - pos = s_max( pos, i ); - max_ener_band[j] = i; - move16(); - ener_vec[i] = -10; - move16(); - } - - /* re-allocate bits to the frames such that the highest band with allocated bits is higher than the threshold */ - test(); - test(); - test(); - IF( GT_16( sub( nb_tot_bands, bandoffset ), nb_bands ) && ( GT_16( pos, 7 ) && EQ_32( core_brate, ACELP_8k00 ) ) && EQ_16( bwidth, WB ) ) - { - band = sub( nb_tot_bands, add( bandoffset, nb_bands ) ); - FOR( j = 0; j < band; j++ ) - { - i = maximum_fx( ener_vec, sub( nb_tot_bands, bandoffset ), &etmp ); - max_ener_band[nb_bands + j] = i; - move16(); - ener_vec[i] = -10; - move16(); - bits_per_bands[nb_bands + j] = 1310720; /*5 in Q18*/ - move32(); /*Q18 */ - } - nb_bands = add( nb_bands, band ); - - bit_tmp = i_mult2( band, 5 ); - - IF( LE_16( band, 2 ) ) - { - FOR( j = st_band - 1; j < nb_bands; j++ ) - { - bits_per_bands[j] = L_add( bits_per_bands[j], 262144 ); /*Q18 */ - move32(); - } - bit_tmp = add( bit_tmp, add( sub( nb_bands, st_band ), 1 ) ); - } - - i = 0; - move16(); - j = 0; - move16(); - FOR( ; bit_tmp > 0; bit_tmp-- ) - { - bits_per_bands[j] = L_sub( bits_per_bands[j], ONE_IN_Q18 ); /*Q18 */ - j = add( j, 1 ); - IF( EQ_16( j, sub( st_band, i ) ) ) - { - j = 0; - move16(); - } - test(); - if ( j == 0 && LT_16( i, sub( st_band, 1 ) ) ) - { - i = add( i, 1 ); - } - } - } - } - - /*-------------------------------------------------------------------------- - * Bit sum verification for GSC inactive at very high rate - * The maximum number of bits per band of length 16 is 112 - * Redistribute the overage bits if needed - *--------------------------------------------------------------------------*/ - sum_bit = 0; - move16(); - j = 0; - move16(); - FOR( i = 0; i < nb_bands; i++ ) - { - L_tmp = Mult_32_16( sum_bit, 10923 /*1/3 in Q15*/ ); - - IF( GT_32( bits_per_bands[i], 29360128 ) ) /* 112 in Q18 */ - { - sum_bit = L_add( sum_bit, L_sub( bits_per_bands[i], 29360128 /* 112 in Q18 */ ) ); /* Q18 */ - bits_per_bands[i] = 29360128 /* 112 in Q18 */; - move32(); - j = add( i, 1 ); - } - ELSE IF( GT_32( L_add( bits_per_bands[i], L_tmp ), 29360128 ) ) /* Q18 */ - { - j = add( i, 1 ); - } - } - - IF( sum_bit != 0 ) - { - tmp = sub( nb_bands, j ); - sum_bit = Mult_32_16( sum_bit, div_s( 1, tmp ) ); /* Q18 */ - FOR( i = j; i < nb_bands; i++ ) - { - bits_per_bands[i] = L_add( bits_per_bands[i], sum_bit ); - move32(); /* Q18 */ - } - } - - /*-------------------------------------------------------------------------- - * second step of bit sum verification, normally sum_bit == *bit - *--------------------------------------------------------------------------*/ - - sum_bit = 0; - move32(); - FOR( i = 0; i < nb_bands; i++ ) - { - bits_per_bands[i] = L_shl( L_shr( bits_per_bands[i], Q18 ), Q18 ); - move32(); - sum_bit = L_add( sum_bit, L_shr( bits_per_bands[i], Q18 ) ); /*Q0*/ - } - - test(); - IF( GSC_IVAS_mode != 0 && LT_32( sum_bit, *bit ) ) /* If we need to add bits, we are doing it on the LF */ - { - reajust_bits_fx( bits_per_bands, 0, nb_bands, (Word16) sum_bit, *bit ); - } - ELSE - { - reajust_bits_fx( bits_per_bands, sub( nb_bands, 1 ), 0, (Word16) sum_bit, *bit ); - } - w_sum_bit = 0; - move16(); - FOR( i = 0; i < nb_bands; i++ ) - { - out_bits_per_bands[i] = shl( extract_l( L_shr( bits_per_bands[i], 18 ) ), 3 ); - move16(); - w_sum_bit = add( w_sum_bit, out_bits_per_bands[i] ); /* Q3 */ - } - tmp = shl( *bit, 3 ); - - /*-------------------------------------------------------------------------- - * Recompute the real number/length of frequency bands to encode - *--------------------------------------------------------------------------*/ - *nb_subbands = nb_bands; - move16(); - *pvq_len = shl( *nb_subbands, 4 ); - move16(); - - /*-------------------------------------------------------------------------- - * Concatenate bands (encoder only) - *--------------------------------------------------------------------------*/ - IF( exc_diff != NULL ) - { - FOR( j = 0; j < nb_bands; j++ ) - { - Copy( exc_diff + shl( max_ener_band[j], 4 ), concat_in + shl( j, 4 ), 16 ); - } - } - } - ELSE /* *bit == 0 */ - { - set16_fx( out_bits_per_bands, 0, nb_tot_bands ); - *nb_subbands = 0; - move16(); - *pvq_len = 0; - move16(); - } - - return; -} - - -/*-------------------------------------------------------------------* - * reajust_bits() - * - * - *-------------------------------------------------------------------*/ - -static void reajust_bits_fx( - Word32 *bits_per_bands, /*i : Q18*/ - const Word16 st_band, - const Word16 end_band, - const Word16 sum_bit_in, /*i : Q0*/ - const Word16 bit_bdgt_in /*i : Q0*/ ) -{ - Word16 i, amount_to_add, incr; - Word16 bit_bdgt, sum_bit; - - incr = 1; - move16(); - if ( LT_16( end_band, st_band ) ) - { - incr = -1; - move16(); - } - - IF( LT_16( bit_bdgt_in, sum_bit_in ) ) - { - amount_to_add = -1; - move16(); - bit_bdgt = sum_bit_in; - move16(); - sum_bit = bit_bdgt_in; - move16(); - } - ELSE - { - bit_bdgt = bit_bdgt_in; - move16(); - sum_bit = sum_bit_in; - move16(); - amount_to_add = 1; - move16(); - } - - i = st_band; - move16(); - WHILE( GT_16( bit_bdgt, sum_bit ) ) - { - /* 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], ONE_IN_Q18 ) ) ) - { - /* bits_per_bands[i] += amount_to_add;*/ - bits_per_bands[i] = L_add( bits_per_bands[i], L_shl( amount_to_add, Q18 ) ); /*Q18*/ - move32(); - sum_bit = add( sum_bit, abs_s( amount_to_add ) ); - } - - i = add( i, incr ); - if ( EQ_16( i, end_band ) ) - { - i = st_band; - move16(); - } - } - - return; -} - -/*-------------------------------------------------------------------* - * Find_bit_frac() - * - * Computes the fraction of the remaining bit budget to allocate to the bands - *-------------------------------------------------------------------*/ - -static Word32 Find_bit_frac_fx( - const Word16 nb_band, - const Word16 remaining_bits ) -{ - Word16 inv_bandQ15; - Word32 L_out; - - inv_bandQ15 = 6553; /*Q15*/ - move16(); - IF( EQ_16( nb_band, 7 ) ) - { - inv_bandQ15 = 4681; /*Q15*/ - move16(); - } - ELSE IF( EQ_16( nb_band, 3 ) ) - { - inv_bandQ15 = 10922; /*Q15*/ - move16(); - } - ELSE IF( EQ_16( nb_band, 4 ) ) - { - inv_bandQ15 = 8192; /*Q15*/ - move16(); - } - ELSE IF( EQ_16( nb_band, 5 ) ) - { - inv_bandQ15 = 6536; /*Q15*/ - move16(); - } - ELSE IF( EQ_16( nb_band, 10 ) ) - { - inv_bandQ15 = 3277; /*Q15*/ - 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 ) ); /*Q18*/ - - return ( L_out ); -} - - -/* o : Number of band to encode */ -static Word16 Find_bit_alloc_IVAS_fx( - 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 Q18 */ -) -{ - 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(); - test(); - test(); - 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_fx( 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_fx( 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_fx( 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 ); /*Q18*/ - move32(); - /* SWB_bit_budget--; */ - SWB_bit_budget = L_sub( SWB_bit_budget, Q18_1_0 ); /*Q18*/ - i = sub( i, 1 ); - 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_fx( 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; -} - - -/* Find normalized 1 / ToDivide */ -static Word16 Find_norm_inv_fx( - const Word32 ToDivide, - Word16 *e_div ) -{ - 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; -} diff --git a/lib_com/options.h b/lib_com/options.h index 23a5aa26c110b2fd3f840f5486a32a483164f218..f0d57f596a64e0df9dd3d4b83cc942b477cd5de9 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -91,6 +91,7 @@ #define HARM_2456_APPLY_SCALE /* FhG basop issue 2456: Harmonize apply_scale_ind(), apply_scale_ivas_fx() */ #define HARM_2454_TCX_RES_Q_SPEC /* FhG: harmonization of tcx_res_Q_spec_fx() and tcx_res_Q_spec_ivas_fx() */ #define FIX_FLOAT_1535_ARI_RES_Q_CLEANUP /* FhG: remove dead code from tcx_ari_res_Q_spec() */ +#define FIX_2467_RENAME_GSC_FUNCTION /* VA: basop issue 2467: Removal of unused function/table and renaming of _ivas_fx versions to default ones. */ /* #################### End BE switches ################################## */ diff --git a/lib_com/prot_fx.h b/lib_com/prot_fx.h index f32d05534d06517f2b9e81dca1a40013fb058929..76a9a3e472595e3b16a5b5b6d56ad76433bbf5a1 100644 --- a/lib_com/prot_fx.h +++ b/lib_com/prot_fx.h @@ -4542,6 +4542,7 @@ void longshr( Word16 bits, Word16 len ); +#ifndef FIX_2467_RENAME_GSC_FUNCTION void bands_and_bit_alloc_fx( const Word16 cor_strong_limit, /* i : HF correlation */ const Word16 noise_lev, /* i : dwn scaling factor */ @@ -4563,7 +4564,7 @@ void bands_and_bit_alloc_fx( const Word16 element_mode, /* i : element mode */ const Word16 GSC_IVAS_mode /* i : GSC IVAS mode */ ); - +#endif void fine_gain_pred_fx( const Word16 *sfm_start, /* i : Sub band start indices */ const Word16 *sfm_end, /* i : Sub band end indices */ @@ -10515,7 +10516,11 @@ Word16 quant_2p_2N1_fx( const Word16 N /* i: number of bits FOR position */ ); +#ifdef FIX_2467_RENAME_GSC_FUNCTION +void bands_and_bit_alloc_fx( +#else void bands_and_bit_alloc_ivas_fx( +#endif const Word16 cor_strong_limit, /* i : HF correlation */ const Word16 noise_lev, /* i : dwn scaling factor */ const Word32 core_brate, /* i : core bit rate */ diff --git a/lib_com/rom_com.h b/lib_com/rom_com.h index e247196a164906eb127a49f69b4c971d1bc936f1..69be48471100ef0060b00db904ed5bb7c75bd561 100644 --- a/lib_com/rom_com.h +++ b/lib_com/rom_com.h @@ -1045,7 +1045,9 @@ extern const Word16 YG_dicHR_4_16kHz_fx[]; /* Q12 */ extern const Word16 YG_meanL2G_16kHz_fx[]; /* Q12 */ extern const Word16 YG_dicL2G_16kHz_fx[]; /* Q12 */ extern const Word16 GSC_freq_bits[]; // Q0 -extern const Word32 GSC_freq_bits_fx[]; /*Q18*/ +#ifndef FIX_2467_RENAME_GSC_FUNCTION +extern const Word32 GSC_freq_bits_fx[]; /*Q18*/ +#endif extern const Word32 GSC_freq_bits_fx_Q18[]; /*Q18*/ extern const Word16 GSC_freq_DL0_bits[]; /* Q0 */ extern const Word16 Compl_GSC_freq_bits[]; /* Q0 */ diff --git a/lib_com/rom_com_fx.c b/lib_com/rom_com_fx.c index 3e86de9a6691d2880c8d942cf99e997f06422ad9..57297c3f5f376f548de2347ad38c9b2163ea7724 100644 --- a/lib_com/rom_com_fx.c +++ b/lib_com/rom_com_fx.c @@ -17263,6 +17263,8 @@ const Word16 GSC_freq_bits[] = 26, 96, 0, 28, 18, 13, 12,10,10, 5, 4, 4, 1, 0, 1, 3, 4, /* ACELP_22k60*/ 26, 96, 0, 28, 18, 13, 12,10,10, 5, 4, 4, 1, 0, 1, 3, 4 /* ACELP_24k40*/ }; // Q0 + +#ifndef FIX_2467_RENAME_GSC_FUNCTION const Word32 GSC_freq_bits_fx[] =/*Q18*/ { 5505024, 17563648, -1572864, 6553600, 3932160, 2883584, 2621440, 1310720, 0, 0, 1310720, 1048576, 0, 0, 1048576, 0, 0, /* ACELP_5k00*/ @@ -17278,7 +17280,7 @@ const Word32 GSC_freq_bits_fx[] =/*Q18*/ 8126464, 25165824, 0, 7340032, 4718592, 3407872, 3145728, 2621440, 2621440, 1310720, 1048576, 1048576, 262144, 0, 262144, 786432, 1048576, /* ACELP_22k60*/ 26, 96, -1, 28, 18, 13, 12,10,10, 5, 4, 4, 1, 0, 1, 3, 4, /* ACELP_16k40*/ 8126464, 25165824, 0, 7340032, 4718592, 3407872, 3145728, 2621440, 2621440, 1310720, 1048576, 1048576, 262144, 0, 262144, 786432, 1048576, /* ACELP_22k60*/ 26, 96, -1, 28, 18, 13, 12,10,10, 5, 4, 4, 1, 0, 1, 3, 4, /* ACELP_16k40*/ }; - +#endif const Word32 GSC_freq_bits_fx_Q18[] =/*Q18*/ { 5505024, 17301504, -1572864, 6553600, 3932160, 2883584, 2621440, 1310720, 0, 0, 1310720, 1048576, 0, 0, 1048576, 0, 0, diff --git a/lib_dec/gs_dec_fx.c b/lib_dec/gs_dec_fx.c index 0338c76b8d5c343893751b028a3096437e946699..d867298d1926d68040c4c6a1c2c7b45aaa09ddcf 100644 --- a/lib_dec/gs_dec_fx.c +++ b/lib_dec/gs_dec_fx.c @@ -8,6 +8,8 @@ #include "rom_com.h" #include "prot_fx.h" #include "ivas_cnst.h" + + /*=========================================================================*/ /* FUNCTION : void decod_audio_fx(); */ /*-------------------------------------------------------------------------*/ @@ -720,9 +722,15 @@ void gsc_dec_fx( * PVQ decoder *--------------------------------------------------------------------------------------*/ +#ifdef FIX_2467_RENAME_GSC_FUNCTION + bands_and_bit_alloc_fx( hGSCDec->cor_strong_limit, hGSCDec->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ, + max_ener_band, bits_per_bands, &nb_subbands, NULL, NULL, &pvq_len, coder_type, st_fx->bwidth, st_fx->GSC_noisy_speech, + st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode ); +#else bands_and_bit_alloc_ivas_fx( hGSCDec->cor_strong_limit, hGSCDec->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ, max_ener_band, bits_per_bands, &nb_subbands, NULL, NULL, &pvq_len, coder_type, st_fx->bwidth, st_fx->GSC_noisy_speech, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode ); +#endif IF( bit == 0 ) { diff --git a/lib_enc/gs_enc_fx.c b/lib_enc/gs_enc_fx.c index 7b3eefc951d3355c83bb43c07a41e1b22b300232..3c287885317d0700d2b94795f3c40a1aa588f29a 100644 --- a/lib_enc/gs_enc_fx.c +++ b/lib_enc/gs_enc_fx.c @@ -580,8 +580,13 @@ void gsc_enc_fx( * Frequency encoder *--------------------------------------------------------------------------------------*/ +#ifdef FIX_2467_RENAME_GSC_FUNCTION + bands_and_bit_alloc_fx( hGSCEnc->cor_strong_limit, hGSCEnc->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ, max_ener_band, bits_per_bands, &nb_subbands, exc_diff, concat_in, &pvq_len, + st_fx->coder_type, st_fx->bwidth, st_fx->GSC_noisy_speech, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode ); +#else bands_and_bit_alloc_ivas_fx( hGSCEnc->cor_strong_limit, hGSCEnc->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ, max_ener_band, bits_per_bands, &nb_subbands, exc_diff, concat_in, &pvq_len, st_fx->coder_type, st_fx->bwidth, st_fx->GSC_noisy_speech, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode ); +#endif Q_tmp = *Q_exc; move16();