Commit a51ebd68 authored by Koen Vos's avatar Koen Vos Committed by Jean-Marc Valin
Browse files

Accuracy improvements to help float implementations

Also clamps the gain to avoid forcing a float decoder to emulate the
state rescaling.
parent 5609cec9
......@@ -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 );
......
......@@ -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++ ) {
......
This diff is collapsed.
......@@ -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++;
}
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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
......
......@@ -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 ] );
......
......@@ -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 */
......
......@@ -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;
}
......
......@@ -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 */
}
}
......
......@@ -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 );
......
......@@ -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;
}
......@@ -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;