diff --git a/silk/CNG.c b/silk/CNG.c index c85353b051edf28aa5018438101e77f86e8d1b95..261cdb170b13967be36890861114c3b16e8fb967 100644 --- a/silk/CNG.c +++ b/silk/CNG.c @@ -34,7 +34,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /* Generates excitation for CNG LPC synthesis */ static inline void silk_CNG_exc( opus_int32 residual_Q10[], /* O CNG residual signal Q10 */ - opus_int32 exc_buf_Q10[], /* I Random samples buffer Q10 */ + opus_int32 exc_buf_Q14[], /* I Random samples buffer Q10 */ opus_int32 Gain_Q16, /* I Gain to apply */ opus_int length, /* I Length */ opus_int32 *rand_seed /* I/O Seed to random index generator */ @@ -54,7 +54,7 @@ static inline void silk_CNG_exc( idx = (opus_int)( silk_RSHIFT( seed, 24 ) & exc_mask ); silk_assert( idx >= 0 ); silk_assert( idx <= CNG_BUF_MASK_MAX ); - residual_Q10[ i ] = (opus_int16)silk_SAT16( silk_SMULWW( exc_buf_Q10[ idx ], Gain_Q16 ) ); + residual_Q10[ i ] = (opus_int16)silk_SAT16( silk_SMULWW( exc_buf_Q14[ idx ], Gain_Q16 >> 4 ) ); } *rand_seed = seed; } @@ -112,8 +112,8 @@ void silk_CNG( } } /* Update CNG excitation buffer with excitation from this subframe */ - silk_memmove( &psCNG->CNG_exc_buf_Q10[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q10, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) ); - silk_memcpy( psCNG->CNG_exc_buf_Q10, &psDec->exc_Q10[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) ); + silk_memmove( &psCNG->CNG_exc_buf_Q14[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q14, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) ); + silk_memcpy( psCNG->CNG_exc_buf_Q14, &psDec->exc_Q14[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) ); /* Smooth gains */ for( i = 0; i < psDec->nb_subfr; i++ ) { @@ -125,7 +125,7 @@ void silk_CNG( if( psDec->lossCnt ) { /* Generate CNG excitation */ - silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q10, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); + silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q14, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed ); /* Convert CNG NLSF to filter representation */ silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order ); diff --git a/silk/NSQ.c b/silk/NSQ.c index 96fec5401a5c007b627b8e7023a086b04f86751b..c4941ada5b217fb02f7e4826e89a196bb7b6b2c3 100644 --- a/silk/NSQ.c +++ b/silk/NSQ.c @@ -99,7 +99,7 @@ void silk_NSQ( /* Set unvoiced lag to the previous one, overwrite later for voiced */ lag = NSQ->lagPrev; - silk_assert( NSQ->prev_inv_gain_Q31 != 0 ); + silk_assert( NSQ->prev_gain_Q16 != 0 ); offset_Q10 = silk_Quantization_Offsets_Q10[ psIndices->signalType >> 1 ][ psIndices->quantOffsetType ]; @@ -159,7 +159,7 @@ void silk_NSQ( /* Save quantized speech and noise shaping signals */ /* DEBUG_STORE_DATA( enc.pcm, &NSQ->xq[ psEncC->ltp_mem_length ], psEncC->frame_length * sizeof( opus_int16 ) ) */ silk_memmove( NSQ->xq, &NSQ->xq[ psEncC->frame_length ], psEncC->ltp_mem_length * sizeof( opus_int16 ) ); - silk_memmove( NSQ->sLTP_shp_Q10, &NSQ->sLTP_shp_Q10[ psEncC->frame_length ], psEncC->ltp_mem_length * sizeof( opus_int32 ) ); + silk_memmove( NSQ->sLTP_shp_Q14, &NSQ->sLTP_shp_Q14[ psEncC->frame_length ], psEncC->ltp_mem_length * sizeof( opus_int32 ) ); } /***********************************/ @@ -188,14 +188,15 @@ static inline void silk_noise_shape_quantizer( ) { opus_int i, j; - opus_int32 LTP_pred_Q13, LPC_pred_Q10, n_AR_Q10, n_LTP_Q14; - opus_int32 n_LF_Q10, r_Q10, rr_Q10, q1_Q10, q2_Q10, rd1_Q10, rd2_Q10; - opus_int32 dither, exc_Q10, LPC_exc_Q10, xq_Q10; - opus_int32 tmp1, tmp2, sLF_AR_shp_Q10; + opus_int32 LTP_pred_Q13, LPC_pred_Q10, n_AR_Q12, n_LTP_Q13; + opus_int32 n_LF_Q12, r_Q10, rr_Q10, q1_Q0, q1_Q10, q2_Q10, rd1_Q20, rd2_Q20; + opus_int32 dither, exc_Q14, LPC_exc_Q14, xq_Q14, Gain_Q10; + opus_int32 tmp1, tmp2, sLF_AR_shp_Q14; opus_int32 *psLPC_Q14, *shp_lag_ptr, *pred_lag_ptr; - shp_lag_ptr = &NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; + shp_lag_ptr = &NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; pred_lag_ptr = &sLTP_Q15[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; + Gain_Q10 = silk_RSHIFT( Gain_Q16, 6 ); /* Set up short term AR state */ psLPC_Q14 = &NSQ->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - 1 ]; @@ -250,46 +251,45 @@ static inline void silk_noise_shape_quantizer( tmp2 = psLPC_Q14[ 0 ]; tmp1 = NSQ->sAR2_Q14[ 0 ]; NSQ->sAR2_Q14[ 0 ] = tmp2; - n_AR_Q10 = silk_SMULWB( tmp2, AR_shp_Q13[ 0 ] ); + n_AR_Q12 = silk_RSHIFT( shapingLPCOrder, 1 ); + n_AR_Q12 = silk_SMLAWB( n_AR_Q12, tmp2, AR_shp_Q13[ 0 ] ); for( j = 2; j < shapingLPCOrder; j += 2 ) { tmp2 = NSQ->sAR2_Q14[ j - 1 ]; NSQ->sAR2_Q14[ j - 1 ] = tmp1; - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, tmp1, AR_shp_Q13[ j - 1 ] ); + n_AR_Q12 = silk_SMLAWB( n_AR_Q12, tmp1, AR_shp_Q13[ j - 1 ] ); tmp1 = NSQ->sAR2_Q14[ j + 0 ]; NSQ->sAR2_Q14[ j + 0 ] = tmp2; - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, tmp2, AR_shp_Q13[ j ] ); + n_AR_Q12 = silk_SMLAWB( n_AR_Q12, tmp2, AR_shp_Q13[ j ] ); } NSQ->sAR2_Q14[ shapingLPCOrder - 1 ] = tmp1; - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, tmp1, AR_shp_Q13[ shapingLPCOrder - 1 ] ); + n_AR_Q12 = silk_SMLAWB( n_AR_Q12, tmp1, AR_shp_Q13[ shapingLPCOrder - 1 ] ); - n_AR_Q10 = silk_RSHIFT( n_AR_Q10, 1 ); /* Q11 -> Q10 */ - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, NSQ->sLF_AR_shp_Q12, Tilt_Q14 ); + n_AR_Q12 = silk_LSHIFT32( n_AR_Q12, 1 ); /* Q11 -> Q12 */ + n_AR_Q12 = silk_SMLAWB( n_AR_Q12, NSQ->sLF_AR_shp_Q14, Tilt_Q14 ); - n_LF_Q10 = silk_LSHIFT( silk_SMULWB( NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - 1 ], LF_shp_Q14 ), 2 ); - n_LF_Q10 = silk_SMLAWT( n_LF_Q10, NSQ->sLF_AR_shp_Q12, LF_shp_Q14 ); + n_LF_Q12 = silk_SMULWB( NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - 1 ], LF_shp_Q14 ); + n_LF_Q12 = silk_SMLAWT( n_LF_Q12, NSQ->sLF_AR_shp_Q14, LF_shp_Q14 ); silk_assert( lag > 0 || signalType != TYPE_VOICED ); - /* Long-term shaping */ + /* Combine prediction and noise shaping signals */ + tmp1 = silk_SUB32( silk_LSHIFT32( LPC_pred_Q10, 2 ), n_AR_Q12 ); /* Q12 */ + tmp1 = silk_SUB32( tmp1, n_LF_Q12 ); /* Q12 */ if( lag > 0 ) { /* Symmetric, packed FIR coefficients */ - n_LTP_Q14 = silk_SMULWB( silk_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); - n_LTP_Q14 = silk_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); - n_LTP_Q14 = silk_LSHIFT( n_LTP_Q14, 6 ); + n_LTP_Q13 = silk_SMULWB( silk_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); + n_LTP_Q13 = silk_SMLAWT( n_LTP_Q13, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); + n_LTP_Q13 = silk_LSHIFT( n_LTP_Q13, 1 ); shp_lag_ptr++; - tmp1 = silk_SUB32( silk_LSHIFT32( LTP_pred_Q13, 1 ), n_LTP_Q14 ); /* Add Q14 stuff */ - tmp1 = silk_RSHIFT( tmp1, 4 ); /* convert to Q10 */ - tmp1 = silk_ADD32( tmp1, LPC_pred_Q10 ); /* add Q10 stuff */ - tmp1 = silk_SUB32( tmp1, n_AR_Q10 ); /* subtract Q10 stuff */ + tmp2 = silk_SUB32( LTP_pred_Q13, n_LTP_Q13 ); /* Q13 */ + tmp1 = silk_ADD_LSHIFT32( tmp2, tmp1, 1 ); /* Q13 */ + tmp1 = silk_RSHIFT_ROUND( tmp1, 3 ); /* Q10 */ } else { - tmp1 = silk_SUB32( LPC_pred_Q10, n_AR_Q10 ); /* subtract Q10 stuff */ + tmp1 = silk_RSHIFT_ROUND( tmp1, 2 ); /* Q10 */ } - /* Input minus prediction plus noise feedback */ - /*r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP;*/ - tmp1 = silk_SUB32( tmp1, n_LF_Q10 ); /* subtract Q10 stuff */ - r_Q10 = silk_SUB32( x_sc_Q10[ i ], tmp1 ); + r_Q10 = silk_SUB32( x_sc_Q10[ i ], tmp1 ); /* residual error Q10 */ /* Flip sign depending on dither */ r_Q10 = r_Q10 ^ dither; @@ -297,64 +297,64 @@ static inline void silk_noise_shape_quantizer( /* Find two quantization level candidates and measure their rate-distortion */ q1_Q10 = silk_SUB32( r_Q10, offset_Q10 ); - q1_Q10 = silk_RSHIFT( q1_Q10, 10 ); - if( q1_Q10 > 0 ) { - q1_Q10 = silk_SUB32( silk_LSHIFT( q1_Q10, 10 ), QUANT_LEVEL_ADJUST_Q10 ); + q1_Q0 = silk_RSHIFT( q1_Q10, 10 ); + if( q1_Q0 > 0 ) { + q1_Q10 = silk_SUB32( silk_LSHIFT( q1_Q0, 10 ), QUANT_LEVEL_ADJUST_Q10 ); q1_Q10 = silk_ADD32( q1_Q10, offset_Q10 ); q2_Q10 = silk_ADD32( q1_Q10, 1024 ); - rd1_Q10 = silk_SMULBB( q1_Q10, Lambda_Q10 ); - rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); - } else if( q1_Q10 == 0 ) { + rd1_Q20 = silk_SMULBB( q1_Q10, Lambda_Q10 ); + rd2_Q20 = silk_SMULBB( q2_Q10, Lambda_Q10 ); + } else if( q1_Q0 == 0 ) { q1_Q10 = offset_Q10; q2_Q10 = silk_ADD32( q1_Q10, 1024 - QUANT_LEVEL_ADJUST_Q10 ); - rd1_Q10 = silk_SMULBB( q1_Q10, Lambda_Q10 ); - rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); - } else if( q1_Q10 == -1 ) { + rd1_Q20 = silk_SMULBB( q1_Q10, Lambda_Q10 ); + rd2_Q20 = silk_SMULBB( q2_Q10, Lambda_Q10 ); + } else if( q1_Q0 == -1 ) { q2_Q10 = offset_Q10; q1_Q10 = silk_SUB32( q2_Q10, 1024 - QUANT_LEVEL_ADJUST_Q10 ); - rd1_Q10 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); - rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); - } else { /* Q1_Q10 < -1 */ - q1_Q10 = silk_ADD32( silk_LSHIFT( q1_Q10, 10 ), QUANT_LEVEL_ADJUST_Q10 ); + rd1_Q20 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); + rd2_Q20 = silk_SMULBB( q2_Q10, Lambda_Q10 ); + } else { /* Q1_Q0 < -1 */ + q1_Q10 = silk_ADD32( silk_LSHIFT( q1_Q0, 10 ), QUANT_LEVEL_ADJUST_Q10 ); q1_Q10 = silk_ADD32( q1_Q10, offset_Q10 ); q2_Q10 = silk_ADD32( q1_Q10, 1024 ); - rd1_Q10 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); - rd2_Q10 = silk_SMULBB( -q2_Q10, Lambda_Q10 ); + rd1_Q20 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); + rd2_Q20 = silk_SMULBB( -q2_Q10, Lambda_Q10 ); } rr_Q10 = silk_SUB32( r_Q10, q1_Q10 ); - rd1_Q10 = silk_RSHIFT( silk_SMLABB( rd1_Q10, rr_Q10, rr_Q10 ), 10 ); + rd1_Q20 = silk_SMLABB( rd1_Q20, rr_Q10, rr_Q10 ); rr_Q10 = silk_SUB32( r_Q10, q2_Q10 ); - rd2_Q10 = silk_RSHIFT( silk_SMLABB( rd2_Q10, rr_Q10, rr_Q10 ), 10 ); + rd2_Q20 = silk_SMLABB( rd2_Q20, rr_Q10, rr_Q10 ); - if( rd2_Q10 < rd1_Q10 ) { + if( rd2_Q20 < rd1_Q20 ) { q1_Q10 = q2_Q10; } pulses[ i ] = (opus_int8)silk_RSHIFT_ROUND( q1_Q10, 10 ); /* Excitation */ - exc_Q10 = q1_Q10 ^ dither; + exc_Q14 = silk_LSHIFT( q1_Q10, 4 ) ^ dither; /* Add predictions */ - LPC_exc_Q10 = silk_ADD32( exc_Q10, silk_RSHIFT_ROUND( LTP_pred_Q13, 3 ) ); - xq_Q10 = silk_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); + LPC_exc_Q14 = silk_ADD_LSHIFT32( exc_Q14, LTP_pred_Q13, 1 ); + xq_Q14 = silk_ADD_LSHIFT32( LPC_exc_Q14, LPC_pred_Q10, 4 ); /* Scale XQ back to normal level before saving */ - xq[ i ] = ( opus_int16 )silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( xq_Q10, Gain_Q16 ), 10 ) ); + xq[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( xq_Q14, Gain_Q10 ), 8 ) ); /* Update states */ psLPC_Q14++; - *psLPC_Q14 = silk_LSHIFT( xq_Q10, 4 ); - sLF_AR_shp_Q10 = silk_SUB32( xq_Q10, n_AR_Q10 ); - NSQ->sLF_AR_shp_Q12 = silk_LSHIFT( sLF_AR_shp_Q10, 2 ); + *psLPC_Q14 = xq_Q14; + sLF_AR_shp_Q14 = silk_SUB_LSHIFT32( xq_Q14, n_AR_Q12, 2 ); + NSQ->sLF_AR_shp_Q14 = sLF_AR_shp_Q14; - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx ] = silk_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); - sLTP_Q15[ NSQ->sLTP_buf_idx ] = silk_LSHIFT( LPC_exc_Q10, 5 ); + NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx ] = silk_SUB_LSHIFT32( sLF_AR_shp_Q14, n_LF_Q12, 2 ); + sLTP_Q15[ NSQ->sLTP_buf_idx ] = silk_LSHIFT( LPC_exc_Q14, 1 ); NSQ->sLTP_shp_buf_idx++; NSQ->sLTP_buf_idx++; /* Make dither dependent on quantized signal */ - NSQ->rand_seed = silk_ADD32_ovflw(NSQ->rand_seed, pulses[ i ]); + NSQ->rand_seed = silk_ADD32_ovflw( NSQ->rand_seed, pulses[ i ] ); } /* Update LPC synth buffer */ @@ -378,12 +378,13 @@ static inline void silk_nsq_scale_states( opus_int i, lag; opus_int32 gain_adj_Q16, inv_gain_Q31, inv_gain_Q23; - inv_gain_Q31 = silk_INVERSE32_varQ( silk_max( Gains_Q16[ subfr ], 1 ), 47 ); lag = pitchL[ subfr ]; + inv_gain_Q31 = silk_INVERSE32_varQ( silk_max( Gains_Q16[ subfr ], 1 ), 47 ); + silk_assert( inv_gain_Q31 != 0 ); /* Calculate gain adjustment factor */ - if( inv_gain_Q31 != NSQ->prev_inv_gain_Q31 ) { - gain_adj_Q16 = silk_DIV32_varQ( inv_gain_Q31, NSQ->prev_inv_gain_Q31, 16 ); + if( Gains_Q16[ subfr ] != NSQ->prev_gain_Q16 ) { + gain_adj_Q16 = silk_DIV32_varQ( NSQ->prev_gain_Q16, Gains_Q16[ subfr ], 16 ); } else { gain_adj_Q16 = 1 << 16; } @@ -395,8 +396,7 @@ static inline void silk_nsq_scale_states( } /* Save inverse gain */ - silk_assert( inv_gain_Q31 != 0 ); - NSQ->prev_inv_gain_Q31 = inv_gain_Q31; + NSQ->prev_gain_Q16 = Gains_Q16[ subfr ]; /* After rewhitening the LTP state is un-scaled, so scale with inv_gain_Q16 */ if( NSQ->rewhite_flag ) { @@ -414,7 +414,7 @@ static inline void silk_nsq_scale_states( if( gain_adj_Q16 != 1 << 16 ) { /* Scale long-term shaping state */ for( i = NSQ->sLTP_shp_buf_idx - psEncC->ltp_mem_length; i < NSQ->sLTP_shp_buf_idx; i++ ) { - NSQ->sLTP_shp_Q10[ i ] = silk_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q10[ i ] ); + NSQ->sLTP_shp_Q14[ i ] = silk_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q14[ i ] ); } /* Scale long-term prediction state */ @@ -424,7 +424,7 @@ static inline void silk_nsq_scale_states( } } - NSQ->sLF_AR_shp_Q12 = silk_SMULWW( gain_adj_Q16, NSQ->sLF_AR_shp_Q12 ); + NSQ->sLF_AR_shp_Q14 = silk_SMULWW( gain_adj_Q16, NSQ->sLF_AR_shp_Q14 ); /* Scale short-term prediction and shaping states */ for( i = 0; i < NSQ_LPC_BUF_LENGTH; i++ ) { diff --git a/silk/NSQ_del_dec.c b/silk/NSQ_del_dec.c index b13ecca862f29a74270c00f35c12adfd8f49f15b..2fd2a9d1a0cea0ea7bd5eb2233609a9abae0ba83 100644 --- a/silk/NSQ_del_dec.c +++ b/silk/NSQ_del_dec.c @@ -35,11 +35,11 @@ typedef struct { opus_int32 sLPC_Q14[ MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH ]; opus_int32 RandState[ DECISION_DELAY ]; opus_int32 Q_Q10[ DECISION_DELAY ]; - opus_int32 Xq_Q10[ DECISION_DELAY ]; - opus_int32 Pred_Q16[ DECISION_DELAY ]; - opus_int32 Shape_Q10[ DECISION_DELAY ]; + opus_int32 Xq_Q14[ DECISION_DELAY ]; + opus_int32 Pred_Q15[ DECISION_DELAY ]; + opus_int32 Shape_Q14[ DECISION_DELAY ]; opus_int32 sAR2_Q14[ MAX_SHAPE_LPC_ORDER ]; - opus_int32 LF_AR_Q12; + opus_int32 LF_AR_Q14; opus_int32 Seed; opus_int32 SeedInit; opus_int32 RD_Q10; @@ -49,9 +49,9 @@ typedef struct { opus_int32 Q_Q10; opus_int32 RD_Q10; opus_int32 xq_Q14; - opus_int32 LF_AR_Q12; - opus_int32 sLTP_shp_Q10; - opus_int32 LPC_exc_Q16; + opus_int32 LF_AR_Q14; + opus_int32 sLTP_shp_Q14; + opus_int32 LPC_exc_Q14; } NSQ_sample_struct; static inline void silk_nsq_del_dec_scale_states( @@ -82,7 +82,7 @@ static inline void silk_noise_shape_quantizer_del_dec( opus_int8 pulses[], /* O */ opus_int16 xq[], /* O */ opus_int32 sLTP_Q15[], /* I/O LTP filter state */ - opus_int32 delayedGain_Q16[], /* I/O Gain delay buffer */ + opus_int32 delayedGain_Q10[], /* I/O Gain delay buffer */ const opus_int16 a_Q12[], /* I Short term prediction coefs */ const opus_int16 b_Q14[], /* I Long term prediction coefs */ const opus_int16 AR_shp_Q13[], /* I Noise shaping coefs */ @@ -129,16 +129,16 @@ void silk_NSQ_del_dec( opus_int16 sLTP[ 2 * MAX_FRAME_LENGTH ]; opus_int32 HarmShapeFIRPacked_Q14; opus_int offset_Q10; - opus_int32 RDmin_Q10; + opus_int32 RDmin_Q10, Gain_Q10; opus_int32 x_sc_Q10[ MAX_SUB_FRAME_LENGTH ]; - opus_int32 delayedGain_Q16[ DECISION_DELAY ]; + opus_int32 delayedGain_Q10[ DECISION_DELAY ]; NSQ_del_dec_struct psDelDec[ MAX_DEL_DEC_STATES ]; NSQ_del_dec_struct *psDD; /* Set unvoiced lag to the previous one, overwrite later for voiced */ lag = NSQ->lagPrev; - silk_assert( NSQ->prev_inv_gain_Q31 != 0 ); + silk_assert( NSQ->prev_gain_Q16 != 0 ); /* Initialize delayed decision states */ silk_memset( psDelDec, 0, psEncC->nStatesDelayedDecision * sizeof( NSQ_del_dec_struct ) ); @@ -147,8 +147,8 @@ void silk_NSQ_del_dec( psDD->Seed = ( k + psIndices->Seed ) & 3; psDD->SeedInit = psDD->Seed; psDD->RD_Q10 = 0; - psDD->LF_AR_Q12 = NSQ->sLF_AR_shp_Q12; - psDD->Shape_Q10[ 0 ] = NSQ->sLTP_shp_Q10[ psEncC->ltp_mem_length - 1 ]; + psDD->LF_AR_Q14 = NSQ->sLF_AR_shp_Q14; + psDD->Shape_Q14[ 0 ] = NSQ->sLTP_shp_Q14[ psEncC->ltp_mem_length - 1 ]; silk_memcpy( psDD->sLPC_Q14, NSQ->sLPC_Q14, NSQ_LPC_BUF_LENGTH * sizeof( opus_int32 ) ); silk_memcpy( psDD->sAR2_Q14, NSQ->sAR2_Q14, sizeof( NSQ->sAR2_Q14 ) ); } @@ -222,8 +222,8 @@ void silk_NSQ_del_dec( last_smple_idx = ( last_smple_idx - 1 ) & DECISION_DELAY_MASK; pulses[ i - decisionDelay ] = (opus_int8)silk_RSHIFT_ROUND( psDD->Q_Q10[ last_smple_idx ], 10 ); pxq[ i - decisionDelay ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( - silk_SMULWW( psDD->Xq_Q10[ last_smple_idx ], Gains_Q16[ 1 ] ), 10 ) ); - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q10[ last_smple_idx ]; + silk_SMULWW( psDD->Xq_Q14[ last_smple_idx ], Gains_Q16[ 1 ] ), 14 ) ); + NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q14[ last_smple_idx ]; } subfr = 0; @@ -245,7 +245,7 @@ void silk_NSQ_del_dec( psEncC->nStatesDelayedDecision, LTP_scale_Q14, Gains_Q16, pitchL, psIndices->signalType, decisionDelay ); silk_noise_shape_quantizer_del_dec( NSQ, psDelDec, psIndices->signalType, x_sc_Q10, pulses, pxq, sLTP_Q15, - delayedGain_Q16, A_Q12, B_Q14, AR_shp_Q13, lag, HarmShapeFIRPacked_Q14, Tilt_Q14[ k ], LF_shp_Q14[ k ], + delayedGain_Q10, A_Q12, B_Q14, AR_shp_Q13, lag, HarmShapeFIRPacked_Q14, Tilt_Q14[ k ], LF_shp_Q14[ k ], Gains_Q16[ k ], Lambda_Q10, offset_Q10, psEncC->subfr_length, subfr++, psEncC->shapingLPCOrder, psEncC->predictLPCOrder, psEncC->warping_Q16, psEncC->nStatesDelayedDecision, &smpl_buf_idx, decisionDelay ); @@ -268,24 +268,25 @@ void silk_NSQ_del_dec( psDD = &psDelDec[ Winner_ind ]; psIndices->Seed = psDD->SeedInit; last_smple_idx = smpl_buf_idx + decisionDelay; + Gain_Q10 = silk_RSHIFT32( Gains_Q16[ psEncC->nb_subfr - 1 ], 6 ); for( i = 0; i < decisionDelay; i++ ) { last_smple_idx = ( last_smple_idx - 1 ) & DECISION_DELAY_MASK; pulses[ i - decisionDelay ] = (opus_int8)silk_RSHIFT_ROUND( psDD->Q_Q10[ last_smple_idx ], 10 ); pxq[ i - decisionDelay ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( - silk_SMULWW( psDD->Xq_Q10[ last_smple_idx ], Gains_Q16[ psEncC->nb_subfr - 1 ] ), 10 ) ); - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q10[ last_smple_idx ]; + silk_SMULWW( psDD->Xq_Q14[ last_smple_idx ], Gain_Q10 ), 8 ) ); + NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - decisionDelay + i ] = psDD->Shape_Q14[ last_smple_idx ]; } silk_memcpy( NSQ->sLPC_Q14, &psDD->sLPC_Q14[ psEncC->subfr_length ], NSQ_LPC_BUF_LENGTH * sizeof( opus_int32 ) ); silk_memcpy( NSQ->sAR2_Q14, psDD->sAR2_Q14, sizeof( psDD->sAR2_Q14 ) ); /* Update states */ - NSQ->sLF_AR_shp_Q12 = psDD->LF_AR_Q12; + NSQ->sLF_AR_shp_Q14 = psDD->LF_AR_Q14; NSQ->lagPrev = pitchL[ psEncC->nb_subfr - 1 ]; /* Save quantized speech signal */ /* DEBUG_STORE_DATA( enc.pcm, &NSQ->xq[psEncC->ltp_mem_length], psEncC->frame_length * sizeof( opus_int16 ) ) */ silk_memmove( NSQ->xq, &NSQ->xq[ psEncC->frame_length ], psEncC->ltp_mem_length * sizeof( opus_int16 ) ); - silk_memmove( NSQ->sLTP_shp_Q10, &NSQ->sLTP_shp_Q10[ psEncC->frame_length ], psEncC->ltp_mem_length * sizeof( opus_int32 ) ); + silk_memmove( NSQ->sLTP_shp_Q14, &NSQ->sLTP_shp_Q14[ psEncC->frame_length ], psEncC->ltp_mem_length * sizeof( opus_int32 ) ); } /******************************************/ @@ -299,7 +300,7 @@ static inline void silk_noise_shape_quantizer_del_dec( opus_int8 pulses[], /* O */ opus_int16 xq[], /* O */ opus_int32 sLTP_Q15[], /* I/O LTP filter state */ - opus_int32 delayedGain_Q16[], /* I/O Gain delay buffer */ + opus_int32 delayedGain_Q10[], /* I/O Gain delay buffer */ const opus_int16 a_Q12[], /* I Short term prediction coefs */ const opus_int16 b_Q14[], /* I Long term prediction coefs */ const opus_int16 AR_shp_Q13[], /* I Noise shaping coefs */ @@ -322,10 +323,10 @@ static inline void silk_noise_shape_quantizer_del_dec( { opus_int i, j, k, Winner_ind, RDmin_ind, RDmax_ind, last_smple_idx; opus_int32 Winner_rand_state; - opus_int32 LTP_pred_Q13, LPC_pred_Q10, n_AR_Q10, n_LTP_Q14, LTP_Q10; - opus_int32 n_LF_Q10, r_Q10, rr_Q10, rd1_Q10, rd2_Q10, RDmin_Q10, RDmax_Q10; - opus_int32 q1_Q10, q2_Q10, dither, exc_Q10, LPC_exc_Q10, xq_Q10; - opus_int32 tmp1, tmp2, sLF_AR_shp_Q10; + opus_int32 LTP_pred_Q14, LPC_pred_Q14, n_AR_Q14, n_LTP_Q14; + opus_int32 n_LF_Q14, r_Q10, rr_Q10, rd1_Q10, rd2_Q10, RDmin_Q10, RDmax_Q10; + opus_int32 q1_Q0, q1_Q10, q2_Q10, dither, exc_Q14, LPC_exc_Q14, xq_Q14, Gain_Q10; + opus_int32 tmp1, tmp2, sLF_AR_shp_Q14; opus_int32 *pred_lag_ptr, *shp_lag_ptr, *psLPC_Q14; NSQ_sample_struct psSampleState[ MAX_DEL_DEC_STATES ][ 2 ]; NSQ_del_dec_struct *psDD; @@ -333,8 +334,9 @@ static inline void silk_noise_shape_quantizer_del_dec( silk_assert( nStatesDelayedDecision > 0 ); - shp_lag_ptr = &NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; + shp_lag_ptr = &NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; pred_lag_ptr = &sLTP_Q15[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; + Gain_Q10 = silk_RSHIFT( Gain_Q16, 6 ); for( i = 0; i < length; i++ ) { /* Perform common calculations used in all states */ @@ -343,15 +345,16 @@ static inline void silk_noise_shape_quantizer_del_dec( if( signalType == TYPE_VOICED ) { /* Unrolled loop */ /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ - LTP_pred_Q13 = 2; - LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ 0 ], b_Q14[ 0 ] ); - LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -1 ], b_Q14[ 1 ] ); - LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -2 ], b_Q14[ 2 ] ); - LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -3 ], b_Q14[ 3 ] ); - LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -4 ], b_Q14[ 4 ] ); + LTP_pred_Q14 = 2; + LTP_pred_Q14 = silk_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ 0 ], b_Q14[ 0 ] ); + LTP_pred_Q14 = silk_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -1 ], b_Q14[ 1 ] ); + LTP_pred_Q14 = silk_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -2 ], b_Q14[ 2 ] ); + LTP_pred_Q14 = silk_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -3 ], b_Q14[ 3 ] ); + LTP_pred_Q14 = silk_SMLAWB( LTP_pred_Q14, pred_lag_ptr[ -4 ], b_Q14[ 4 ] ); + LTP_pred_Q14 = silk_LSHIFT( LTP_pred_Q14, 1 ); /* Q13 -> Q14 */ pred_lag_ptr++; } else { - LTP_pred_Q13 = 0; + LTP_pred_Q14 = 0; } /* Long-term shaping */ @@ -359,12 +362,10 @@ static inline void silk_noise_shape_quantizer_del_dec( /* Symmetric, packed FIR coefficients */ n_LTP_Q14 = silk_SMULWB( silk_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); n_LTP_Q14 = silk_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); - n_LTP_Q14 = silk_LSHIFT( n_LTP_Q14, 6 ); + n_LTP_Q14 = silk_SUB_LSHIFT32( LTP_pred_Q14, n_LTP_Q14, 2 ); /* Q12 -> Q14 */ shp_lag_ptr++; - - LTP_Q10 = silk_RSHIFT( silk_SUB32( silk_LSHIFT( LTP_pred_Q13, 1 ), n_LTP_Q14 ), 4 ); } else { - LTP_Q10 = 0; + n_LTP_Q14 = 0; } for( k = 0; k < nStatesDelayedDecision; k++ ) { @@ -385,25 +386,26 @@ static inline void silk_noise_shape_quantizer_del_dec( /* Short-term prediction */ silk_assert( predictLPCOrder == 10 || predictLPCOrder == 16 ); /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ - LPC_pred_Q10 = silk_RSHIFT( predictLPCOrder, 1 ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ 0 ], a_Q12[ 0 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -1 ], a_Q12[ 1 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -2 ], a_Q12[ 2 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -3 ], a_Q12[ 3 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -4 ], a_Q12[ 4 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -5 ], a_Q12[ 5 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -6 ], a_Q12[ 6 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -7 ], a_Q12[ 7 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -8 ], a_Q12[ 8 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -9 ], a_Q12[ 9 ] ); + LPC_pred_Q14 = silk_RSHIFT( predictLPCOrder, 1 ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ 0 ], a_Q12[ 0 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -1 ], a_Q12[ 1 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -2 ], a_Q12[ 2 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -3 ], a_Q12[ 3 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -4 ], a_Q12[ 4 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -5 ], a_Q12[ 5 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -6 ], a_Q12[ 6 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -7 ], a_Q12[ 7 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -8 ], a_Q12[ 8 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -9 ], a_Q12[ 9 ] ); if( predictLPCOrder == 16 ) { - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -10 ], a_Q12[ 10 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -11 ], a_Q12[ 11 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -12 ], a_Q12[ 12 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -13 ], a_Q12[ 13 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -14 ], a_Q12[ 14 ] ); - LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, psLPC_Q14[ -15 ], a_Q12[ 15 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -10 ], a_Q12[ 10 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -11 ], a_Q12[ 11 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -12 ], a_Q12[ 12 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -13 ], a_Q12[ 13 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -14 ], a_Q12[ 14 ] ); + LPC_pred_Q14 = silk_SMLAWB( LPC_pred_Q14, psLPC_Q14[ -15 ], a_Q12[ 15 ] ); } + LPC_pred_Q14 = silk_LSHIFT( LPC_pred_Q14, 4 ); /* Q10 -> Q14 */ /* Noise shape feedback */ silk_assert( ( shapingLPCOrder & 1 ) == 0 ); /* check that order is even */ @@ -412,33 +414,38 @@ static inline void silk_noise_shape_quantizer_del_dec( /* Output of allpass section */ tmp1 = silk_SMLAWB( psDD->sAR2_Q14[ 0 ], psDD->sAR2_Q14[ 1 ] - tmp2, warping_Q16 ); psDD->sAR2_Q14[ 0 ] = tmp2; - n_AR_Q10 = silk_SMULWB( tmp2, AR_shp_Q13[ 0 ] ); + n_AR_Q14 = silk_RSHIFT( shapingLPCOrder, 1 ); + n_AR_Q14 = silk_SMLAWB( n_AR_Q14, tmp2, AR_shp_Q13[ 0 ] ); /* Loop over allpass sections */ for( j = 2; j < shapingLPCOrder; j += 2 ) { /* Output of allpass section */ tmp2 = silk_SMLAWB( psDD->sAR2_Q14[ j - 1 ], psDD->sAR2_Q14[ j + 0 ] - tmp1, warping_Q16 ); psDD->sAR2_Q14[ j - 1 ] = tmp1; - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, tmp1, AR_shp_Q13[ j - 1 ] ); + n_AR_Q14 = silk_SMLAWB( n_AR_Q14, tmp1, AR_shp_Q13[ j - 1 ] ); /* Output of allpass section */ tmp1 = silk_SMLAWB( psDD->sAR2_Q14[ j + 0 ], psDD->sAR2_Q14[ j + 1 ] - tmp2, warping_Q16 ); psDD->sAR2_Q14[ j + 0 ] = tmp2; - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, tmp2, AR_shp_Q13[ j ] ); + n_AR_Q14 = silk_SMLAWB( n_AR_Q14, tmp2, AR_shp_Q13[ j ] ); } psDD->sAR2_Q14[ shapingLPCOrder - 1 ] = tmp1; - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, tmp1, AR_shp_Q13[ shapingLPCOrder - 1 ] ); + n_AR_Q14 = silk_SMLAWB( n_AR_Q14, tmp1, AR_shp_Q13[ shapingLPCOrder - 1 ] ); - n_AR_Q10 = silk_RSHIFT( n_AR_Q10, 1 ); /* Q11 -> Q10 */ - n_AR_Q10 = silk_SMLAWB( n_AR_Q10, psDD->LF_AR_Q12, Tilt_Q14 ); + n_AR_Q14 = silk_LSHIFT( n_AR_Q14, 1 ); /* Q11 -> Q12 */ + n_AR_Q14 = silk_SMLAWB( n_AR_Q14, psDD->LF_AR_Q14, Tilt_Q14 ); /* Q12 */ + n_AR_Q14 = silk_LSHIFT( n_AR_Q14, 2 ); /* Q12 -> Q14 */ - n_LF_Q10 = silk_LSHIFT( silk_SMULWB( psDD->Shape_Q10[ *smpl_buf_idx ], LF_shp_Q14 ), 2 ); - n_LF_Q10 = silk_SMLAWT( n_LF_Q10, psDD->LF_AR_Q12, LF_shp_Q14 ); + n_LF_Q14 = silk_SMULWB( psDD->Shape_Q14[ *smpl_buf_idx ], LF_shp_Q14 ); /* Q12 */ + n_LF_Q14 = silk_SMLAWT( n_LF_Q14, psDD->LF_AR_Q14, LF_shp_Q14 ); /* Q12 */ + n_LF_Q14 = silk_LSHIFT( n_LF_Q14, 2 ); /* Q12 -> Q14 */ /* Input minus prediction plus noise feedback */ /* r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP */ - tmp1 = silk_ADD32( LTP_Q10, LPC_pred_Q10 ); /* add Q10 stuff */ - tmp1 = silk_SUB32( tmp1, n_AR_Q10 ); /* subtract Q10 stuff */ - tmp1 = silk_SUB32( tmp1, n_LF_Q10 ); /* subtract Q10 stuff */ - r_Q10 = silk_SUB32( x_Q10[ i ], tmp1 ); /* residual error Q10 */ + tmp1 = silk_ADD32( n_AR_Q14, n_LF_Q14 ); /* Q14 */ + tmp2 = silk_ADD32( n_LTP_Q14, LPC_pred_Q14 ); /* Q13 */ + tmp1 = silk_SUB32( tmp2, tmp1 ); /* Q13 */ + tmp1 = silk_RSHIFT_ROUND( tmp1, 4 ); /* Q10 */ + + r_Q10 = silk_SUB32( x_Q10[ i ], tmp1 ); /* residual error Q10 */ /* Flip sign depending on dither */ r_Q10 = r_Q10 ^ dither; @@ -446,25 +453,25 @@ static inline void silk_noise_shape_quantizer_del_dec( /* Find two quantization level candidates and measure their rate-distortion */ q1_Q10 = silk_SUB32( r_Q10, offset_Q10 ); - q1_Q10 = silk_RSHIFT( q1_Q10, 10 ); - if( q1_Q10 > 0 ) { - q1_Q10 = silk_SUB32( silk_LSHIFT( q1_Q10, 10 ), QUANT_LEVEL_ADJUST_Q10 ); + q1_Q0 = silk_RSHIFT( q1_Q10, 10 ); + if( q1_Q0 > 0 ) { + q1_Q10 = silk_SUB32( silk_LSHIFT( q1_Q0, 10 ), QUANT_LEVEL_ADJUST_Q10 ); q1_Q10 = silk_ADD32( q1_Q10, offset_Q10 ); q2_Q10 = silk_ADD32( q1_Q10, 1024 ); rd1_Q10 = silk_SMULBB( q1_Q10, Lambda_Q10 ); rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); - } else if( q1_Q10 == 0 ) { + } else if( q1_Q0 == 0 ) { q1_Q10 = offset_Q10; q2_Q10 = silk_ADD32( q1_Q10, 1024 - QUANT_LEVEL_ADJUST_Q10 ); rd1_Q10 = silk_SMULBB( q1_Q10, Lambda_Q10 ); rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); - } else if( q1_Q10 == -1 ) { + } else if( q1_Q0 == -1 ) { q2_Q10 = offset_Q10; q1_Q10 = silk_SUB32( q2_Q10, 1024 - QUANT_LEVEL_ADJUST_Q10 ); rd1_Q10 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); - } else { /* Q1_Q10 < -1 */ - q1_Q10 = silk_ADD32( silk_LSHIFT( q1_Q10, 10 ), QUANT_LEVEL_ADJUST_Q10 ); + } else { /* q1_Q0 < -1 */ + q1_Q10 = silk_ADD32( silk_LSHIFT( q1_Q0, 10 ), QUANT_LEVEL_ADJUST_Q10 ); q1_Q10 = silk_ADD32( q1_Q10, offset_Q10 ); q2_Q10 = silk_ADD32( q1_Q10, 1024 ); rd1_Q10 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); @@ -490,34 +497,34 @@ static inline void silk_noise_shape_quantizer_del_dec( /* Update states for best quantization */ /* Quantized excitation */ - exc_Q10 = psSS[ 0 ].Q_Q10 ^ dither; + exc_Q14 = silk_LSHIFT32( psSS[ 0 ].Q_Q10, 4 ) ^ dither; /* Add predictions */ - LPC_exc_Q10 = exc_Q10 + silk_RSHIFT_ROUND( LTP_pred_Q13, 3 ); - xq_Q10 = silk_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); + LPC_exc_Q14 = silk_ADD32( exc_Q14, LTP_pred_Q14 ); + xq_Q14 = silk_ADD32( LPC_exc_Q14, LPC_pred_Q14 ); /* Update states */ - sLF_AR_shp_Q10 = silk_SUB32( xq_Q10, n_AR_Q10 ); - psSS[ 0 ].sLTP_shp_Q10 = silk_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); - psSS[ 0 ].LF_AR_Q12 = silk_LSHIFT( sLF_AR_shp_Q10, 2 ); - psSS[ 0 ].xq_Q14 = silk_LSHIFT( xq_Q10, 4 ); - psSS[ 0 ].LPC_exc_Q16 = silk_LSHIFT( LPC_exc_Q10, 6 ); + sLF_AR_shp_Q14 = silk_SUB32( xq_Q14, n_AR_Q14 ); + psSS[ 0 ].sLTP_shp_Q14 = silk_SUB32( sLF_AR_shp_Q14, n_LF_Q14 ); + psSS[ 0 ].LF_AR_Q14 = sLF_AR_shp_Q14; + psSS[ 0 ].LPC_exc_Q14 = LPC_exc_Q14; + psSS[ 0 ].xq_Q14 = xq_Q14; /* Update states for second best quantization */ /* Quantized excitation */ - exc_Q10 = psSS[ 1 ].Q_Q10 ^ dither; + exc_Q14 = silk_LSHIFT32( psSS[ 1 ].Q_Q10, 4 ) ^ dither; /* Add predictions */ - LPC_exc_Q10 = exc_Q10 + silk_RSHIFT_ROUND( LTP_pred_Q13, 3 ); - xq_Q10 = silk_ADD32( LPC_exc_Q10, LPC_pred_Q10 ); + LPC_exc_Q14 = silk_ADD32( exc_Q14, LTP_pred_Q14 ); + xq_Q14 = silk_ADD32( LPC_exc_Q14, LPC_pred_Q14 ); /* Update states */ - sLF_AR_shp_Q10 = silk_SUB32( xq_Q10, n_AR_Q10 ); - psSS[ 1 ].sLTP_shp_Q10 = silk_SUB32( sLF_AR_shp_Q10, n_LF_Q10 ); - psSS[ 1 ].LF_AR_Q12 = silk_LSHIFT( sLF_AR_shp_Q10, 2 ); - psSS[ 1 ].xq_Q14 = silk_LSHIFT( xq_Q10, 4 ); - psSS[ 1 ].LPC_exc_Q16 = silk_LSHIFT( LPC_exc_Q10, 6 ); + sLF_AR_shp_Q14 = silk_SUB32( xq_Q14, n_AR_Q14 ); + psSS[ 1 ].sLTP_shp_Q14 = silk_SUB32( sLF_AR_shp_Q14, n_LF_Q14 ); + psSS[ 1 ].LF_AR_Q14 = sLF_AR_shp_Q14; + psSS[ 1 ].LPC_exc_Q14 = LPC_exc_Q14; + psSS[ 1 ].xq_Q14 = xq_Q14; } *smpl_buf_idx = ( *smpl_buf_idx - 1 ) & DECISION_DELAY_MASK; /* Index to newest samples */ @@ -537,8 +544,8 @@ static inline void silk_noise_shape_quantizer_del_dec( Winner_rand_state = psDelDec[ Winner_ind ].RandState[ last_smple_idx ]; for( k = 0; k < nStatesDelayedDecision; k++ ) { if( psDelDec[ k ].RandState[ last_smple_idx ] != Winner_rand_state ) { - psSampleState[ k ][ 0 ].RD_Q10 = silk_ADD32( psSampleState[ k ][ 0 ].RD_Q10, ( silk_int32_MAX >> 4 ) ); - psSampleState[ k ][ 1 ].RD_Q10 = silk_ADD32( psSampleState[ k ][ 1 ].RD_Q10, ( silk_int32_MAX >> 4 ) ); + psSampleState[ k ][ 0 ].RD_Q10 = silk_ADD32( psSampleState[ k ][ 0 ].RD_Q10, silk_int32_MAX >> 4 ); + psSampleState[ k ][ 1 ].RD_Q10 = silk_ADD32( psSampleState[ k ][ 1 ].RD_Q10, silk_int32_MAX >> 4 ); silk_assert( psSampleState[ k ][ 0 ].RD_Q10 >= 0 ); } } @@ -563,8 +570,8 @@ static inline void silk_noise_shape_quantizer_del_dec( /* Replace a state if best from second set outperforms worst in first set */ if( RDmin_Q10 < RDmax_Q10 ) { - silk_memcpy( ((opus_int32 *)&psDelDec[ RDmax_ind ]) + i, - ((opus_int32 *)&psDelDec[ RDmin_ind ]) + i, sizeof( NSQ_del_dec_struct ) - i * sizeof( opus_int32) ); + silk_memcpy( ( (opus_int32 *)&psDelDec[ RDmax_ind ] ) + i, + ( (opus_int32 *)&psDelDec[ RDmin_ind ] ) + i, sizeof( NSQ_del_dec_struct ) - i * sizeof( opus_int32) ); silk_memcpy( &psSampleState[ RDmax_ind ][ 0 ], &psSampleState[ RDmin_ind ][ 1 ], sizeof( NSQ_sample_struct ) ); } @@ -573,9 +580,9 @@ static inline void silk_noise_shape_quantizer_del_dec( if( subfr > 0 || i >= decisionDelay ) { pulses[ i - decisionDelay ] = (opus_int8)silk_RSHIFT_ROUND( psDD->Q_Q10[ last_smple_idx ], 10 ); xq[ i - decisionDelay ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( - silk_SMULWW( psDD->Xq_Q10[ last_smple_idx ], delayedGain_Q16[ last_smple_idx ] ), 10 ) ); - NSQ->sLTP_shp_Q10[ NSQ->sLTP_shp_buf_idx - decisionDelay ] = psDD->Shape_Q10[ last_smple_idx ]; - sLTP_Q15[ NSQ->sLTP_buf_idx - decisionDelay ] = psDD->Pred_Q16[ last_smple_idx ] >> 1; + silk_SMULWW( psDD->Xq_Q14[ last_smple_idx ], delayedGain_Q10[ last_smple_idx ] ), 8 ) ); + NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - decisionDelay ] = psDD->Shape_Q14[ last_smple_idx ]; + sLTP_Q15[ NSQ->sLTP_buf_idx - decisionDelay ] = psDD->Pred_Q15[ last_smple_idx ]; } NSQ->sLTP_shp_buf_idx++; NSQ->sLTP_buf_idx++; @@ -584,17 +591,17 @@ static inline void silk_noise_shape_quantizer_del_dec( for( k = 0; k < nStatesDelayedDecision; k++ ) { psDD = &psDelDec[ k ]; psSS = &psSampleState[ k ][ 0 ]; - psDD->LF_AR_Q12 = psSS->LF_AR_Q12; + psDD->LF_AR_Q14 = psSS->LF_AR_Q14; psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH + i ] = psSS->xq_Q14; - psDD->Xq_Q10[ *smpl_buf_idx ] = silk_RSHIFT( psSS->xq_Q14, 4 ); + psDD->Xq_Q14[ *smpl_buf_idx ] = psSS->xq_Q14; psDD->Q_Q10[ *smpl_buf_idx ] = psSS->Q_Q10; - psDD->Pred_Q16[ *smpl_buf_idx ] = psSS->LPC_exc_Q16; - psDD->Shape_Q10[ *smpl_buf_idx ] = psSS->sLTP_shp_Q10; + psDD->Pred_Q15[ *smpl_buf_idx ] = silk_LSHIFT32( psSS->LPC_exc_Q14, 1 ); + psDD->Shape_Q14[ *smpl_buf_idx ] = psSS->sLTP_shp_Q14; psDD->Seed = silk_ADD32_ovflw( psDD->Seed, silk_RSHIFT_ROUND( psSS->Q_Q10, 10 ) ); psDD->RandState[ *smpl_buf_idx ] = psDD->Seed; psDD->RD_Q10 = psSS->RD_Q10; } - delayedGain_Q16[ *smpl_buf_idx ] = Gain_Q16; + delayedGain_Q10[ *smpl_buf_idx ] = Gain_Q10; } /* Update LPC states */ for( k = 0; k < nStatesDelayedDecision; k++ ) { @@ -624,12 +631,13 @@ static inline void silk_nsq_del_dec_scale_states( opus_int32 gain_adj_Q16, inv_gain_Q31, inv_gain_Q23; NSQ_del_dec_struct *psDD; - inv_gain_Q31 = silk_INVERSE32_varQ( silk_max( Gains_Q16[ subfr ], 1 ), 47 ); lag = pitchL[ subfr ]; + inv_gain_Q31 = silk_INVERSE32_varQ( silk_max( Gains_Q16[ subfr ], 1 ), 47 ); + silk_assert( inv_gain_Q31 != 0 ); /* Calculate gain adjustment factor */ - if( inv_gain_Q31 != NSQ->prev_inv_gain_Q31 ) { - gain_adj_Q16 = silk_DIV32_varQ( inv_gain_Q31, NSQ->prev_inv_gain_Q31, 16 ); + if( Gains_Q16[ subfr ] != NSQ->prev_gain_Q16 ) { + gain_adj_Q16 = silk_DIV32_varQ( NSQ->prev_gain_Q16, Gains_Q16[ subfr ], 16 ); } else { gain_adj_Q16 = 1 << 16; } @@ -641,8 +649,7 @@ static inline void silk_nsq_del_dec_scale_states( } /* Save inverse gain */ - silk_assert( inv_gain_Q31 != 0 ); - NSQ->prev_inv_gain_Q31 = inv_gain_Q31; + NSQ->prev_gain_Q16 = Gains_Q16[ subfr ]; /* After rewhitening the LTP state is un-scaled, so scale with inv_gain_Q16 */ if( NSQ->rewhite_flag ) { @@ -660,7 +667,7 @@ static inline void silk_nsq_del_dec_scale_states( if( gain_adj_Q16 != 1 << 16 ) { /* Scale long-term shaping state */ for( i = NSQ->sLTP_shp_buf_idx - psEncC->ltp_mem_length; i < NSQ->sLTP_shp_buf_idx; i++ ) { - NSQ->sLTP_shp_Q10[ i ] = silk_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q10[ i ] ); + NSQ->sLTP_shp_Q14[ i ] = silk_SMULWW( gain_adj_Q16, NSQ->sLTP_shp_Q14[ i ] ); } /* Scale long-term prediction state */ @@ -674,7 +681,7 @@ static inline void silk_nsq_del_dec_scale_states( psDD = &psDelDec[ k ]; /* Scale scalar states */ - psDD->LF_AR_Q12 = silk_SMULWW( gain_adj_Q16, psDD->LF_AR_Q12 ); + psDD->LF_AR_Q14 = silk_SMULWW( gain_adj_Q16, psDD->LF_AR_Q14 ); /* Scale short-term prediction and shaping states */ for( i = 0; i < NSQ_LPC_BUF_LENGTH; i++ ) { @@ -684,8 +691,8 @@ static inline void silk_nsq_del_dec_scale_states( psDD->sAR2_Q14[ i ] = silk_SMULWW( gain_adj_Q16, psDD->sAR2_Q14[ i ] ); } for( i = 0; i < DECISION_DELAY; i++ ) { - psDD->Pred_Q16[ i ] = silk_SMULWW( gain_adj_Q16, psDD->Pred_Q16[ i ] ); - psDD->Shape_Q10[ i ] = silk_SMULWW( gain_adj_Q16, psDD->Shape_Q10[ i ] ); + psDD->Pred_Q15[ i ] = silk_SMULWW( gain_adj_Q16, psDD->Pred_Q15[ i ] ); + psDD->Shape_Q14[ i ] = silk_SMULWW( gain_adj_Q16, psDD->Shape_Q14[ i ] ); } } } diff --git a/silk/PLC.c b/silk/PLC.c index fdf46bb7822d88e722e01e2d41bd63f55be740b6..2d140b24da55ebd8fc4ed84be0b717e6c309d5ca 100644 --- a/silk/PLC.c +++ b/silk/PLC.c @@ -174,7 +174,7 @@ static inline void silk_PLC_conceal( opus_int lag, idx, sLTP_buf_idx, shift1, shift2; opus_int32 rand_seed, harm_Gain_Q15, rand_Gain_Q15, inv_gain_Q30; opus_int32 energy1, energy2, *rand_ptr, *pred_lag_ptr; - opus_int32 LPC_exc_Q14, LPC_pred_Q10, LTP_pred_Q12; + opus_int32 LPC_pred_Q10, LTP_pred_Q12; opus_int16 rand_scale_Q14; opus_int16 *B_Q14, *exc_buf_ptr; opus_int32 *sLPC_Q14_ptr; @@ -185,7 +185,7 @@ static inline void silk_PLC_conceal( silk_PLC_struct *psPLC = &psDec->sPLC; if( psDec->first_frame_after_reset ) { - silk_memset(psPLC->prevLPC_Q12, 0, MAX_LPC_ORDER*sizeof(psPLC->prevLPC_Q12[ 0 ])); + silk_memset( psPLC->prevLPC_Q12, 0, sizeof( psPLC->prevLPC_Q12 ) ); } /* Find random noise component */ @@ -194,7 +194,7 @@ static inline void silk_PLC_conceal( for( k = 0; k < 2; k++ ) { for( i = 0; i < psPLC->subfr_length; i++ ) { exc_buf_ptr[ i ] = (opus_int16)silk_RSHIFT( - silk_SMULWW( psDec->exc_Q10[ i + ( k + psPLC->nb_subfr - 2 ) * psPLC->subfr_length ], psPLC->prevGain_Q16[ k ] ), 10 ); + silk_SMULWW( psDec->exc_Q14[ i + ( k + psPLC->nb_subfr - 2 ) * psPLC->subfr_length ], psPLC->prevGain_Q16[ k ] ), 14 ); } exc_buf_ptr += psPLC->subfr_length; } @@ -204,10 +204,10 @@ static inline void silk_PLC_conceal( if( silk_RSHIFT( energy1, shift2 ) < silk_RSHIFT( energy2, shift1 ) ) { /* First sub-frame has lowest energy */ - rand_ptr = &psDec->exc_Q10[ silk_max_int( 0, ( psPLC->nb_subfr - 1 ) * psPLC->subfr_length - RAND_BUF_SIZE ) ]; + rand_ptr = &psDec->exc_Q14[ silk_max_int( 0, ( psPLC->nb_subfr - 1 ) * psPLC->subfr_length - RAND_BUF_SIZE ) ]; } else { /* Second sub-frame has lowest energy */ - rand_ptr = &psDec->exc_Q10[ silk_max_int( 0, psPLC->nb_subfr * psPLC->subfr_length - RAND_BUF_SIZE ) ]; + rand_ptr = &psDec->exc_Q14[ silk_max_int( 0, psPLC->nb_subfr * psPLC->subfr_length - RAND_BUF_SIZE ) ]; } /* Set up Gain to random noise component */ @@ -288,9 +288,7 @@ static inline void silk_PLC_conceal( /* Generate LPC excitation */ rand_seed = silk_RAND( rand_seed ); idx = silk_RSHIFT( rand_seed, 25 ) & RAND_BUF_MASK; - LPC_exc_Q14 = silk_LSHIFT32( silk_SMULWB( rand_ptr[ idx ], rand_scale_Q14 ), 6 ); /* Random noise part */ - LPC_exc_Q14 = silk_ADD32( LPC_exc_Q14, silk_LSHIFT32( LTP_pred_Q12, 2 ) ); /* Harmonic part */ - sLTP_Q14[ sLTP_buf_idx ] = LPC_exc_Q14; + sLTP_Q14[ sLTP_buf_idx ] = silk_LSHIFT32( silk_SMLAWB( LTP_pred_Q12, rand_ptr[ idx ], rand_scale_Q14 ), 2 ); sLTP_buf_idx++; } diff --git a/silk/control_codec.c b/silk/control_codec.c index 1615a16c9d34639f4836b3a74c3876a98ab80641..5a997018b5aa5bb66169a65a63727e2c9ea07b61 100644 --- a/silk/control_codec.c +++ b/silk/control_codec.c @@ -247,7 +247,7 @@ opus_int silk_setup_fs( psEnc->sPrefilt.lagPrev = 100; psEnc->sShape.LastGainIndex = 10; psEnc->sCmn.sNSQ.lagPrev = 100; - psEnc->sCmn.sNSQ.prev_inv_gain_Q31 = silk_int32_MAX; + psEnc->sCmn.sNSQ.prev_gain_Q16 = 65536; psEnc->sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; psEnc->sCmn.fs_kHz = fs_kHz; diff --git a/silk/decode_core.c b/silk/decode_core.c index 4b36aa07dcab052bdba6feeb5ec1591fa6754682..7f90e03be6b63ae1137ecd71cedabe5321109575 100644 --- a/silk/decode_core.c +++ b/silk/decode_core.c @@ -46,11 +46,11 @@ void silk_decode_core( opus_int16 sLTP[ MAX_FRAME_LENGTH ]; opus_int32 sLTP_Q15[ 2 * MAX_FRAME_LENGTH ]; opus_int32 LTP_pred_Q13, LPC_pred_Q10, Gain_Q10, inv_gain_Q31, gain_adj_Q16, rand_seed, offset_Q10; - opus_int32 *pred_lag_ptr, *pexc_Q10, *pres_Q10; - opus_int32 res_Q10[ MAX_SUB_FRAME_LENGTH ]; + opus_int32 *pred_lag_ptr, *pexc_Q14, *pres_Q14; + opus_int32 res_Q14[ MAX_SUB_FRAME_LENGTH ]; opus_int32 sLPC_Q14[ MAX_SUB_FRAME_LENGTH + MAX_LPC_ORDER ]; - silk_assert( psDec->prev_inv_gain_Q31 != 0 ); + silk_assert( psDec->prev_gain_Q16 != 0 ); offset_Q10 = silk_Quantization_Offsets_Q10[ psDec->indices.signalType >> 1 ][ psDec->indices.quantOffsetType ]; @@ -64,28 +64,28 @@ void silk_decode_core( rand_seed = psDec->indices.Seed; for( i = 0; i < psDec->frame_length; i++ ) { rand_seed = silk_RAND( rand_seed ); - psDec->exc_Q10[ i ] = silk_LSHIFT( (opus_int32)pulses[ i ], 10 ); - if( psDec->exc_Q10[ i ] > 0 ) { - psDec->exc_Q10[ i ] -= QUANT_LEVEL_ADJUST_Q10; + psDec->exc_Q14[ i ] = silk_LSHIFT( (opus_int32)pulses[ i ], 14 ); + if( psDec->exc_Q14[ i ] > 0 ) { + psDec->exc_Q14[ i ] -= QUANT_LEVEL_ADJUST_Q10 << 4; } else - if( psDec->exc_Q10[ i ] < 0 ) { - psDec->exc_Q10[ i ] += QUANT_LEVEL_ADJUST_Q10; + if( psDec->exc_Q14[ i ] < 0 ) { + psDec->exc_Q14[ i ] += QUANT_LEVEL_ADJUST_Q10 << 4; } - psDec->exc_Q10[ i ] += offset_Q10; - psDec->exc_Q10[ i ] ^= silk_RSHIFT( rand_seed, 31 ); + psDec->exc_Q14[ i ] += offset_Q10 << 4; + psDec->exc_Q14[ i ] ^= silk_RSHIFT( rand_seed, 31 ); - rand_seed = silk_ADD32_ovflw(rand_seed, pulses[ i ]); + rand_seed = silk_ADD32_ovflw( rand_seed, pulses[ i ] ); } /* Copy LPC state */ silk_memcpy( sLPC_Q14, psDec->sLPC_Q14_buf, MAX_LPC_ORDER * sizeof( opus_int32 ) ); - pexc_Q10 = psDec->exc_Q10; + pexc_Q14 = psDec->exc_Q14; pxq = xq; sLTP_buf_idx = psDec->ltp_mem_length; /* Loop over subframes */ for( k = 0; k < psDec->nb_subfr; k++ ) { - pres_Q10 = res_Q10; + pres_Q14 = res_Q14; A_Q12 = psDecCtrl->PredCoef_Q12[ k >> 1 ]; /* Preload LPC coeficients to array on stack. Gives small performance gain */ @@ -97,8 +97,8 @@ void silk_decode_core( inv_gain_Q31 = silk_INVERSE32_varQ( psDecCtrl->Gains_Q16[ k ], 47 ); /* Calculate gain adjustment factor */ - if( inv_gain_Q31 != psDec->prev_inv_gain_Q31 ) { - gain_adj_Q16 = silk_DIV32_varQ( inv_gain_Q31, psDec->prev_inv_gain_Q31, 16 ); + if( psDecCtrl->Gains_Q16[ k ] != psDec->prev_gain_Q16 ) { + gain_adj_Q16 = silk_DIV32_varQ( psDec->prev_gain_Q16, psDecCtrl->Gains_Q16[ k ], 16 ); /* Scale short term state */ for( i = 0; i < MAX_LPC_ORDER; i++ ) { @@ -110,7 +110,7 @@ void silk_decode_core( /* Save inv_gain */ silk_assert( inv_gain_Q31 != 0 ); - psDec->prev_inv_gain_Q31 = inv_gain_Q31; + psDec->prev_gain_Q16 = psDecCtrl->Gains_Q16[ k ]; /* Avoid abrupt transition from voiced PLC to unvoiced normal decoding */ if( psDec->lossCnt && psDec->prevSignalType == TYPE_VOICED && @@ -174,14 +174,14 @@ void silk_decode_core( pred_lag_ptr++; /* Generate LPC excitation */ - pres_Q10[ i ] = silk_ADD32( pexc_Q10[ i ], silk_RSHIFT_ROUND( LTP_pred_Q13, 3 ) ); + pres_Q14[ i ] = silk_ADD_LSHIFT32( pexc_Q14[ i ], LTP_pred_Q13, 1 ); /* Update states */ - sLTP_Q15[ sLTP_buf_idx ] = silk_LSHIFT( pres_Q10[ i ], 5 ); + sLTP_Q15[ sLTP_buf_idx ] = silk_LSHIFT( pres_Q14[ i ], 1 ); sLTP_buf_idx++; } } else { - pres_Q10 = pexc_Q10; + pres_Q14 = pexc_Q14; } for( i = 0; i < psDec->subfr_length; i++ ) { @@ -209,7 +209,7 @@ void silk_decode_core( } /* Add prediction to LPC excitation */ - sLPC_Q14[ MAX_LPC_ORDER + i ] = silk_LSHIFT( silk_ADD32( pres_Q10[ i ], LPC_pred_Q10 ), 4 ); + sLPC_Q14[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT32( pres_Q14[ i ], LPC_pred_Q10, 4 ); /* Scale with gain */ pxq[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( sLPC_Q14[ MAX_LPC_ORDER + i ], Gain_Q10 ), 8 ) ); @@ -219,7 +219,7 @@ void silk_decode_core( /* Update LPC filter state */ silk_memcpy( sLPC_Q14, &sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( opus_int32 ) ); - pexc_Q10 += psDec->subfr_length; + pexc_Q14 += psDec->subfr_length; pxq += psDec->subfr_length; } diff --git a/silk/define.h b/silk/define.h index 6730f1427c47231090862ff19478451131c9ca08..f398bf8b7908a2d8ba59d7bc52b26f084169e362 100644 --- a/silk/define.h +++ b/silk/define.h @@ -130,7 +130,7 @@ extern "C" #define QUANT_LEVEL_ADJUST_Q10 80 /* Maximum numbers of iterations used to stabilize an LPC vector */ -#define MAX_LPC_STABILIZE_ITERATIONS 30 +#define MAX_LPC_STABILIZE_ITERATIONS 15 #define MAX_PREDICTION_POWER_GAIN 1e4f #define MAX_PREDICTION_POWER_GAIN_AFTER_RESET 1e2f @@ -190,7 +190,7 @@ extern "C" #define VAD_N_BANDS 4 #define VAD_INTERNAL_SUBFRAMES_LOG2 2 -#define VAD_INTERNAL_SUBFRAMES (1 << VAD_INTERNAL_SUBFRAMES_LOG2) +#define VAD_INTERNAL_SUBFRAMES ( 1 << VAD_INTERNAL_SUBFRAMES_LOG2 ) #define VAD_NOISE_LEVEL_SMOOTH_COEF_Q16 1024 /* Must be < 4096 */ #define VAD_NOISE_LEVELS_BIAS 50 diff --git a/silk/enc_API.c b/silk/enc_API.c index d662bf25227d20953622fa5af8666c98a8a61128..4eb80f9e3a913458f1a85f7af3c38f9a555bb14d 100644 --- a/silk/enc_API.c +++ b/silk/enc_API.c @@ -388,11 +388,11 @@ opus_int silk_Encode( /* O Returns error co silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); - psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; - psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; - psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; - psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; - psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_inv_gain_Q31 = silk_int32_MAX; + psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; + psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; + psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; + psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; + psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; } silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ] ); diff --git a/silk/fixed/prefilter_FIX.c b/silk/fixed/prefilter_FIX.c index 89727ed44493ea757786ef25a5a79b70d8771c83..31b0cb57171434d4e92725cdef16324f6757055a 100644 --- a/silk/fixed/prefilter_FIX.c +++ b/silk/fixed/prefilter_FIX.c @@ -67,7 +67,8 @@ void silk_warped_LPC_analysis_filter_FIX( /* Output of allpass section */ tmp1 = silk_SMLAWB( state[ 1 ], state[ 2 ] - tmp2, lambda_Q16 ); state[ 1 ] = tmp2; - acc_Q11 = silk_SMULWB( tmp2, coef_Q13[ 0 ] ); + acc_Q11 = silk_RSHIFT( order, 1 ); + acc_Q11 = silk_SMLAWB( acc_Q11, tmp2, coef_Q13[ 0 ] ); /* Loop over allpass sections */ for( i = 2; i < order; i += 2 ) { /* Output of allpass section */ diff --git a/silk/float/find_pred_coefs_FLP.c b/silk/float/find_pred_coefs_FLP.c index 9c0726a24da213954853e66739ea3262461ed201..d30eeef818623fb0df684582d7b3168eb68f4fb4 100644 --- a/silk/float/find_pred_coefs_FLP.c +++ b/silk/float/find_pred_coefs_FLP.c @@ -96,7 +96,7 @@ void silk_find_pred_coefs_FLP( if( psEnc->sCmn.first_frame_after_reset ) { minInvGain = 1.0f / MAX_PREDICTION_POWER_GAIN_AFTER_RESET; } else { - minInvGain = (silk_float)powf( 2, psEncCtrl->LTPredCodGain / 3 ) / MAX_PREDICTION_POWER_GAIN; + minInvGain = (silk_float)pow( 2, psEncCtrl->LTPredCodGain / 3 ) / MAX_PREDICTION_POWER_GAIN; minInvGain /= 0.25f + 0.75f * psEncCtrl->coding_quality; } diff --git a/silk/gain_quant.c b/silk/gain_quant.c index 287e936f79b39ae609587f1fe2dc08b516f72097..a8fc7d22d9598719a7cfa6398098ca2d760719b3 100644 --- a/silk/gain_quant.c +++ b/silk/gain_quant.c @@ -47,14 +47,14 @@ void silk_gains_quant( opus_int k, double_step_size_threshold; for( k = 0; k < nb_subfr; k++ ) { - /* Add half of previous quantization error, convert to log scale, scale, floor() */ + /* Convert to log scale, scale, floor() */ ind[ k ] = silk_SMULWB( SCALE_Q16, silk_lin2log( gain_Q16[ k ] ) - OFFSET ); /* Round towards previous quantized gain (hysteresis) */ if( ind[ k ] < *prev_ind ) { ind[ k ]++; } - ind[ k ] = silk_max_int( ind[ k ], 0 ); + ind[ k ] = silk_LIMIT_int( ind[ k ], 0, N_LEVELS_QGAIN - 1 ); /* Compute delta indices and limit */ if( k == 0 && conditional == 0 ) { @@ -84,7 +84,7 @@ void silk_gains_quant( ind[ k ] -= MIN_DELTA_GAIN_QUANT; } - /* Convert to linear scale and scale */ + /* Scale and convert to linear scale */ gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ } } @@ -102,7 +102,8 @@ void silk_gains_dequant( for( k = 0; k < nb_subfr; k++ ) { if( k == 0 && conditional == 0 ) { - *prev_ind = ind[ k ]; + /* Gain index is not allowed to go down more than 16 steps (~21.8 dB) */ + *prev_ind = silk_max_int( ind[ k ], *prev_ind - 16 ); } else { /* Delta index */ ind_tmp = ind[ k ] + MIN_DELTA_GAIN_QUANT; @@ -115,9 +116,9 @@ void silk_gains_dequant( *prev_ind += ind_tmp; } } - *prev_ind = silk_min( *prev_ind, N_LEVELS_QGAIN - 1 ); + *prev_ind = silk_LIMIT_int( *prev_ind, 0, N_LEVELS_QGAIN - 1 ); - /* Convert to linear scale and scale */ + /* Scale and convert to linear scale */ gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ } } diff --git a/silk/init_decoder.c b/silk/init_decoder.c index c1c21fdb8eddc65a9265ce6f7133bc216e65e72d..fe50a81745b15c1d70944a3dc5d0c4257ff1ad8d 100644 --- a/silk/init_decoder.c +++ b/silk/init_decoder.c @@ -43,7 +43,7 @@ opus_int silk_init_decoder( /* Used to deactivate LSF interpolation */ psDec->first_frame_after_reset = 1; - psDec->prev_inv_gain_Q31 = silk_int32_MAX; + psDec->prev_gain_Q16 = 65536; /* Reset CNG state */ silk_CNG_Reset( psDec ); diff --git a/silk/log2lin.c b/silk/log2lin.c index 1a78de593c038cf25681c94b7f5e1adcba4220c2..0a3d90be8f9978f3c285281d69e9a869dec20bb2 100644 --- a/silk/log2lin.c +++ b/silk/log2lin.c @@ -47,10 +47,10 @@ opus_int32 silk_log2lin( frac_Q7 = inLog_Q7 & 0x7F; if( inLog_Q7 < 2048 ) { /* Piece-wise parabolic approximation */ - out = silk_ADD_RSHIFT( out, silk_MUL( out, silk_SMLAWB( frac_Q7, silk_MUL( frac_Q7, 128 - frac_Q7 ), -174 ) ), 7 ); + out = silk_ADD_RSHIFT( out, silk_MUL( out, silk_SMLAWB( frac_Q7, silk_SMULBB( frac_Q7, 128 - frac_Q7 ), -174 ) ), 7 ); } else { /* Piece-wise parabolic approximation */ - out = silk_MLA( out, silk_RSHIFT( out, 7 ), silk_SMLAWB( frac_Q7, silk_MUL( frac_Q7, 128 - frac_Q7 ), -174 ) ); + out = silk_MLA( out, silk_RSHIFT( out, 7 ), silk_SMLAWB( frac_Q7, silk_SMULBB( frac_Q7, 128 - frac_Q7 ), -174 ) ); } return out; } diff --git a/silk/structs.h b/silk/structs.h index aee53ce8a2c1dd4c82ee01abec6953dcff76eba6..6bada59b91a942e4c0a0b5a04dbaa9ae8d447a4e 100644 --- a/silk/structs.h +++ b/silk/structs.h @@ -44,15 +44,15 @@ extern "C" /************************************/ typedef struct { opus_int16 xq[ 2 * MAX_FRAME_LENGTH ]; /* Buffer for quantized output signal */ - opus_int32 sLTP_shp_Q10[ 2 * MAX_FRAME_LENGTH ]; + opus_int32 sLTP_shp_Q14[ 2 * MAX_FRAME_LENGTH ]; opus_int32 sLPC_Q14[ MAX_SUB_FRAME_LENGTH + NSQ_LPC_BUF_LENGTH ]; opus_int32 sAR2_Q14[ MAX_SHAPE_LPC_ORDER ]; - opus_int32 sLF_AR_shp_Q12; + opus_int32 sLF_AR_shp_Q14; opus_int lagPrev; opus_int sLTP_buf_idx; opus_int sLTP_shp_buf_idx; opus_int32 rand_seed; - opus_int32 prev_inv_gain_Q31; + opus_int32 prev_gain_Q16; opus_int rewhite_flag; } silk_nsq_state; @@ -243,7 +243,7 @@ typedef struct { /* Struct for CNG */ typedef struct { - opus_int32 CNG_exc_buf_Q10[ MAX_FRAME_LENGTH ]; + opus_int32 CNG_exc_buf_Q14[ MAX_FRAME_LENGTH ]; opus_int16 CNG_smth_NLSF_Q15[ MAX_LPC_ORDER ]; opus_int32 CNG_synth_state[ MAX_LPC_ORDER ]; opus_int32 CNG_smth_Gain_Q16; @@ -255,8 +255,8 @@ typedef struct { /* Decoder state */ /********************************/ typedef struct { - opus_int32 prev_inv_gain_Q31; - opus_int32 exc_Q10[ MAX_FRAME_LENGTH ]; + opus_int32 prev_gain_Q16; + opus_int32 exc_Q14[ MAX_FRAME_LENGTH ]; opus_int32 sLPC_Q14_buf[ MAX_LPC_ORDER ]; opus_int16 outBuf[ MAX_FRAME_LENGTH + 2 * MAX_SUB_FRAME_LENGTH ]; /* Buffer for output signal */ opus_int lagPrev; /* Previous Lag */