cannam@154: /*********************************************************************** cannam@154: Copyright (c) 2006-2011, Skype Limited. All rights reserved. cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: - Redistributions of source code must retain the above copyright notice, cannam@154: this list of conditions and the following disclaimer. cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: - Neither the name of Internet Society, IETF or IETF Trust, nor the cannam@154: names of specific contributors, may be used to endorse or promote cannam@154: products derived from this software without specific prior written cannam@154: permission. cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" cannam@154: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE cannam@154: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE cannam@154: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE cannam@154: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR cannam@154: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF cannam@154: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS cannam@154: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN cannam@154: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) cannam@154: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE cannam@154: POSSIBILITY OF SUCH DAMAGE. cannam@154: ***********************************************************************/ cannam@154: cannam@154: #ifndef __NSQ_DEL_DEC_MIPSR1_H__ cannam@154: #define __NSQ_DEL_DEC_MIPSR1_H__ cannam@154: cannam@154: #ifdef HAVE_CONFIG_H cannam@154: #include "config.h" cannam@154: #endif cannam@154: cannam@154: #include "main.h" cannam@154: #include "stack_alloc.h" cannam@154: cannam@154: #define OVERRIDE_silk_noise_shape_quantizer_del_dec cannam@154: static inline void silk_noise_shape_quantizer_del_dec( cannam@154: silk_nsq_state *NSQ, /* I/O NSQ state */ cannam@154: NSQ_del_dec_struct psDelDec[], /* I/O Delayed decision states */ cannam@154: opus_int signalType, /* I Signal type */ cannam@154: const opus_int32 x_Q10[], /* I */ cannam@154: opus_int8 pulses[], /* O */ cannam@154: opus_int16 xq[], /* O */ cannam@154: opus_int32 sLTP_Q15[], /* I/O LTP filter state */ cannam@154: opus_int32 delayedGain_Q10[], /* I/O Gain delay buffer */ cannam@154: const opus_int16 a_Q12[], /* I Short term prediction coefs */ cannam@154: const opus_int16 b_Q14[], /* I Long term prediction coefs */ cannam@154: const opus_int16 AR_shp_Q13[], /* I Noise shaping coefs */ cannam@154: opus_int lag, /* I Pitch lag */ cannam@154: opus_int32 HarmShapeFIRPacked_Q14, /* I */ cannam@154: opus_int Tilt_Q14, /* I Spectral tilt */ cannam@154: opus_int32 LF_shp_Q14, /* I */ cannam@154: opus_int32 Gain_Q16, /* I */ cannam@154: opus_int Lambda_Q10, /* I */ cannam@154: opus_int offset_Q10, /* I */ cannam@154: opus_int length, /* I Input length */ cannam@154: opus_int subfr, /* I Subframe number */ cannam@154: opus_int shapingLPCOrder, /* I Shaping LPC filter order */ cannam@154: opus_int predictLPCOrder, /* I Prediction filter order */ cannam@154: opus_int warping_Q16, /* I */ cannam@154: opus_int nStatesDelayedDecision, /* I Number of states in decision tree */ cannam@154: opus_int *smpl_buf_idx, /* I/O Index to newest samples in buffers */ cannam@154: opus_int decisionDelay, /* I */ cannam@154: int arch /* I */ cannam@154: ) cannam@154: { cannam@154: opus_int i, j, k, Winner_ind, RDmin_ind, RDmax_ind, last_smple_idx; cannam@154: opus_int32 Winner_rand_state; cannam@154: opus_int32 LTP_pred_Q14, LPC_pred_Q14, n_AR_Q14, n_LTP_Q14; cannam@154: opus_int32 n_LF_Q14, r_Q10, rr_Q10, rd1_Q10, rd2_Q10, RDmin_Q10, RDmax_Q10; cannam@154: opus_int32 q1_Q0, q1_Q10, q2_Q10, exc_Q14, LPC_exc_Q14, xq_Q14, Gain_Q10; cannam@154: opus_int32 tmp1, tmp2, sLF_AR_shp_Q14; cannam@154: opus_int32 *pred_lag_ptr, *shp_lag_ptr, *psLPC_Q14; cannam@154: NSQ_sample_struct psSampleState[ MAX_DEL_DEC_STATES ][ 2 ]; cannam@154: NSQ_del_dec_struct *psDD; cannam@154: NSQ_sample_struct *psSS; cannam@154: opus_int16 b_Q14_0, b_Q14_1, b_Q14_2, b_Q14_3, b_Q14_4; cannam@154: opus_int16 a_Q12_0, a_Q12_1, a_Q12_2, a_Q12_3, a_Q12_4, a_Q12_5, a_Q12_6; cannam@154: opus_int16 a_Q12_7, a_Q12_8, a_Q12_9, a_Q12_10, a_Q12_11, a_Q12_12, a_Q12_13; cannam@154: opus_int16 a_Q12_14, a_Q12_15; cannam@154: cannam@154: opus_int32 cur, prev, next; cannam@154: cannam@154: /*Unused.*/ cannam@154: (void)arch; cannam@154: cannam@154: //Intialize b_Q14 variables cannam@154: b_Q14_0 = b_Q14[ 0 ]; cannam@154: b_Q14_1 = b_Q14[ 1 ]; cannam@154: b_Q14_2 = b_Q14[ 2 ]; cannam@154: b_Q14_3 = b_Q14[ 3 ]; cannam@154: b_Q14_4 = b_Q14[ 4 ]; cannam@154: cannam@154: //Intialize a_Q12 variables cannam@154: a_Q12_0 = a_Q12[0]; cannam@154: a_Q12_1 = a_Q12[1]; cannam@154: a_Q12_2 = a_Q12[2]; cannam@154: a_Q12_3 = a_Q12[3]; cannam@154: a_Q12_4 = a_Q12[4]; cannam@154: a_Q12_5 = a_Q12[5]; cannam@154: a_Q12_6 = a_Q12[6]; cannam@154: a_Q12_7 = a_Q12[7]; cannam@154: a_Q12_8 = a_Q12[8]; cannam@154: a_Q12_9 = a_Q12[9]; cannam@154: a_Q12_10 = a_Q12[10]; cannam@154: a_Q12_11 = a_Q12[11]; cannam@154: a_Q12_12 = a_Q12[12]; cannam@154: a_Q12_13 = a_Q12[13]; cannam@154: a_Q12_14 = a_Q12[14]; cannam@154: a_Q12_15 = a_Q12[15]; cannam@154: cannam@154: long long temp64; cannam@154: cannam@154: silk_assert( nStatesDelayedDecision > 0 ); cannam@154: cannam@154: shp_lag_ptr = &NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - lag + HARM_SHAPE_FIR_TAPS / 2 ]; cannam@154: pred_lag_ptr = &sLTP_Q15[ NSQ->sLTP_buf_idx - lag + LTP_ORDER / 2 ]; cannam@154: Gain_Q10 = silk_RSHIFT( Gain_Q16, 6 ); cannam@154: cannam@154: for( i = 0; i < length; i++ ) { cannam@154: /* Perform common calculations used in all states */ cannam@154: cannam@154: /* Long-term prediction */ cannam@154: if( signalType == TYPE_VOICED ) { cannam@154: /* Unrolled loop */ cannam@154: /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */ cannam@154: temp64 = __builtin_mips_mult(pred_lag_ptr[ 0 ], b_Q14_0 ); cannam@154: temp64 = __builtin_mips_madd( temp64, pred_lag_ptr[ -1 ], b_Q14_1 ); cannam@154: temp64 = __builtin_mips_madd( temp64, pred_lag_ptr[ -2 ], b_Q14_2 ); cannam@154: temp64 = __builtin_mips_madd( temp64, pred_lag_ptr[ -3 ], b_Q14_3 ); cannam@154: temp64 = __builtin_mips_madd( temp64, pred_lag_ptr[ -4 ], b_Q14_4 ); cannam@154: temp64 += 32768; cannam@154: LTP_pred_Q14 = __builtin_mips_extr_w(temp64, 16); cannam@154: LTP_pred_Q14 = silk_LSHIFT( LTP_pred_Q14, 1 ); /* Q13 -> Q14 */ cannam@154: pred_lag_ptr++; cannam@154: } else { cannam@154: LTP_pred_Q14 = 0; cannam@154: } cannam@154: cannam@154: /* Long-term shaping */ cannam@154: if( lag > 0 ) { cannam@154: /* Symmetric, packed FIR coefficients */ cannam@154: n_LTP_Q14 = silk_SMULWB( silk_ADD32( shp_lag_ptr[ 0 ], shp_lag_ptr[ -2 ] ), HarmShapeFIRPacked_Q14 ); cannam@154: n_LTP_Q14 = silk_SMLAWT( n_LTP_Q14, shp_lag_ptr[ -1 ], HarmShapeFIRPacked_Q14 ); cannam@154: n_LTP_Q14 = silk_SUB_LSHIFT32( LTP_pred_Q14, n_LTP_Q14, 2 ); /* Q12 -> Q14 */ cannam@154: shp_lag_ptr++; cannam@154: } else { cannam@154: n_LTP_Q14 = 0; cannam@154: } cannam@154: cannam@154: for( k = 0; k < nStatesDelayedDecision; k++ ) { cannam@154: /* Delayed decision state */ cannam@154: psDD = &psDelDec[ k ]; cannam@154: cannam@154: /* Sample state */ cannam@154: psSS = psSampleState[ k ]; cannam@154: cannam@154: /* Generate dither */ cannam@154: psDD->Seed = silk_RAND( psDD->Seed ); cannam@154: cannam@154: /* Pointer used in short term prediction and shaping */ cannam@154: psLPC_Q14 = &psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH - 1 + i ]; cannam@154: /* Short-term prediction */ cannam@154: silk_assert( predictLPCOrder == 10 || predictLPCOrder == 16 ); cannam@154: temp64 = __builtin_mips_mult(psLPC_Q14[ 0 ], a_Q12_0 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -1 ], a_Q12_1 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -2 ], a_Q12_2 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -3 ], a_Q12_3 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -4 ], a_Q12_4 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -5 ], a_Q12_5 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -6 ], a_Q12_6 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -7 ], a_Q12_7 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -8 ], a_Q12_8 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -9 ], a_Q12_9 ); cannam@154: if( predictLPCOrder == 16 ) { cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -10 ], a_Q12_10 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -11 ], a_Q12_11 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -12 ], a_Q12_12 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -13 ], a_Q12_13 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -14 ], a_Q12_14 ); cannam@154: temp64 = __builtin_mips_madd( temp64, psLPC_Q14[ -15 ], a_Q12_15 ); cannam@154: } cannam@154: temp64 += 32768; cannam@154: LPC_pred_Q14 = __builtin_mips_extr_w(temp64, 16); cannam@154: cannam@154: LPC_pred_Q14 = silk_LSHIFT( LPC_pred_Q14, 4 ); /* Q10 -> Q14 */ cannam@154: cannam@154: /* Noise shape feedback */ cannam@154: silk_assert( ( shapingLPCOrder & 1 ) == 0 ); /* check that order is even */ cannam@154: /* Output of lowpass section */ cannam@154: tmp2 = silk_SMLAWB( psLPC_Q14[ 0 ], psDD->sAR2_Q14[ 0 ], warping_Q16 ); cannam@154: /* Output of allpass section */ cannam@154: tmp1 = silk_SMLAWB( psDD->sAR2_Q14[ 0 ], psDD->sAR2_Q14[ 1 ] - tmp2, warping_Q16 ); cannam@154: psDD->sAR2_Q14[ 0 ] = tmp2; cannam@154: cannam@154: temp64 = __builtin_mips_mult(tmp2, AR_shp_Q13[ 0 ] ); cannam@154: cannam@154: prev = psDD->sAR2_Q14[ 1 ]; cannam@154: cannam@154: /* Loop over allpass sections */ cannam@154: for( j = 2; j < shapingLPCOrder; j += 2 ) { cannam@154: cur = psDD->sAR2_Q14[ j ]; cannam@154: next = psDD->sAR2_Q14[ j+1 ]; cannam@154: /* Output of allpass section */ cannam@154: tmp2 = silk_SMLAWB( prev, cur - tmp1, warping_Q16 ); cannam@154: psDD->sAR2_Q14[ j - 1 ] = tmp1; cannam@154: temp64 = __builtin_mips_madd( temp64, tmp1, AR_shp_Q13[ j - 1 ] ); cannam@154: temp64 = __builtin_mips_madd( temp64, tmp2, AR_shp_Q13[ j ] ); cannam@154: /* Output of allpass section */ cannam@154: tmp1 = silk_SMLAWB( cur, next - tmp2, warping_Q16 ); cannam@154: psDD->sAR2_Q14[ j + 0 ] = tmp2; cannam@154: prev = next; cannam@154: } cannam@154: psDD->sAR2_Q14[ shapingLPCOrder - 1 ] = tmp1; cannam@154: temp64 = __builtin_mips_madd( temp64, tmp1, AR_shp_Q13[ shapingLPCOrder - 1 ] ); cannam@154: temp64 += 32768; cannam@154: n_AR_Q14 = __builtin_mips_extr_w(temp64, 16); cannam@154: n_AR_Q14 = silk_LSHIFT( n_AR_Q14, 1 ); /* Q11 -> Q12 */ cannam@154: n_AR_Q14 = silk_SMLAWB( n_AR_Q14, psDD->LF_AR_Q14, Tilt_Q14 ); /* Q12 */ cannam@154: n_AR_Q14 = silk_LSHIFT( n_AR_Q14, 2 ); /* Q12 -> Q14 */ cannam@154: cannam@154: n_LF_Q14 = silk_SMULWB( psDD->Shape_Q14[ *smpl_buf_idx ], LF_shp_Q14 ); /* Q12 */ cannam@154: n_LF_Q14 = silk_SMLAWT( n_LF_Q14, psDD->LF_AR_Q14, LF_shp_Q14 ); /* Q12 */ cannam@154: n_LF_Q14 = silk_LSHIFT( n_LF_Q14, 2 ); /* Q12 -> Q14 */ cannam@154: cannam@154: /* Input minus prediction plus noise feedback */ cannam@154: /* r = x[ i ] - LTP_pred - LPC_pred + n_AR + n_Tilt + n_LF + n_LTP */ cannam@154: tmp1 = silk_ADD32( n_AR_Q14, n_LF_Q14 ); /* Q14 */ cannam@154: tmp2 = silk_ADD32( n_LTP_Q14, LPC_pred_Q14 ); /* Q13 */ cannam@154: tmp1 = silk_SUB32( tmp2, tmp1 ); /* Q13 */ cannam@154: tmp1 = silk_RSHIFT_ROUND( tmp1, 4 ); /* Q10 */ cannam@154: cannam@154: r_Q10 = silk_SUB32( x_Q10[ i ], tmp1 ); /* residual error Q10 */ cannam@154: cannam@154: /* Flip sign depending on dither */ cannam@154: if ( psDD->Seed < 0 ) { cannam@154: r_Q10 = -r_Q10; cannam@154: } cannam@154: r_Q10 = silk_LIMIT_32( r_Q10, -(31 << 10), 30 << 10 ); cannam@154: cannam@154: /* Find two quantization level candidates and measure their rate-distortion */ cannam@154: q1_Q10 = silk_SUB32( r_Q10, offset_Q10 ); cannam@154: q1_Q0 = silk_RSHIFT( q1_Q10, 10 ); cannam@154: if( q1_Q0 > 0 ) { cannam@154: q1_Q10 = silk_SUB32( silk_LSHIFT( q1_Q0, 10 ), QUANT_LEVEL_ADJUST_Q10 ); cannam@154: q1_Q10 = silk_ADD32( q1_Q10, offset_Q10 ); cannam@154: q2_Q10 = silk_ADD32( q1_Q10, 1024 ); cannam@154: rd1_Q10 = silk_SMULBB( q1_Q10, Lambda_Q10 ); cannam@154: rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); cannam@154: } else if( q1_Q0 == 0 ) { cannam@154: q1_Q10 = offset_Q10; cannam@154: q2_Q10 = silk_ADD32( q1_Q10, 1024 - QUANT_LEVEL_ADJUST_Q10 ); cannam@154: rd1_Q10 = silk_SMULBB( q1_Q10, Lambda_Q10 ); cannam@154: rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); cannam@154: } else if( q1_Q0 == -1 ) { cannam@154: q2_Q10 = offset_Q10; cannam@154: q1_Q10 = silk_SUB32( q2_Q10, 1024 - QUANT_LEVEL_ADJUST_Q10 ); cannam@154: rd1_Q10 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); cannam@154: rd2_Q10 = silk_SMULBB( q2_Q10, Lambda_Q10 ); cannam@154: } else { /* q1_Q0 < -1 */ cannam@154: q1_Q10 = silk_ADD32( silk_LSHIFT( q1_Q0, 10 ), QUANT_LEVEL_ADJUST_Q10 ); cannam@154: q1_Q10 = silk_ADD32( q1_Q10, offset_Q10 ); cannam@154: q2_Q10 = silk_ADD32( q1_Q10, 1024 ); cannam@154: rd1_Q10 = silk_SMULBB( -q1_Q10, Lambda_Q10 ); cannam@154: rd2_Q10 = silk_SMULBB( -q2_Q10, Lambda_Q10 ); cannam@154: } cannam@154: rr_Q10 = silk_SUB32( r_Q10, q1_Q10 ); cannam@154: rd1_Q10 = silk_RSHIFT( silk_SMLABB( rd1_Q10, rr_Q10, rr_Q10 ), 10 ); cannam@154: rr_Q10 = silk_SUB32( r_Q10, q2_Q10 ); cannam@154: rd2_Q10 = silk_RSHIFT( silk_SMLABB( rd2_Q10, rr_Q10, rr_Q10 ), 10 ); cannam@154: cannam@154: if( rd1_Q10 < rd2_Q10 ) { cannam@154: psSS[ 0 ].RD_Q10 = silk_ADD32( psDD->RD_Q10, rd1_Q10 ); cannam@154: psSS[ 1 ].RD_Q10 = silk_ADD32( psDD->RD_Q10, rd2_Q10 ); cannam@154: psSS[ 0 ].Q_Q10 = q1_Q10; cannam@154: psSS[ 1 ].Q_Q10 = q2_Q10; cannam@154: } else { cannam@154: psSS[ 0 ].RD_Q10 = silk_ADD32( psDD->RD_Q10, rd2_Q10 ); cannam@154: psSS[ 1 ].RD_Q10 = silk_ADD32( psDD->RD_Q10, rd1_Q10 ); cannam@154: psSS[ 0 ].Q_Q10 = q2_Q10; cannam@154: psSS[ 1 ].Q_Q10 = q1_Q10; cannam@154: } cannam@154: cannam@154: /* Update states for best quantization */ cannam@154: cannam@154: /* Quantized excitation */ cannam@154: exc_Q14 = silk_LSHIFT32( psSS[ 0 ].Q_Q10, 4 ); cannam@154: if ( psDD->Seed < 0 ) { cannam@154: exc_Q14 = -exc_Q14; cannam@154: } cannam@154: cannam@154: /* Add predictions */ cannam@154: LPC_exc_Q14 = silk_ADD32( exc_Q14, LTP_pred_Q14 ); cannam@154: xq_Q14 = silk_ADD32( LPC_exc_Q14, LPC_pred_Q14 ); cannam@154: cannam@154: /* Update states */ cannam@154: sLF_AR_shp_Q14 = silk_SUB32( xq_Q14, n_AR_Q14 ); cannam@154: psSS[ 0 ].sLTP_shp_Q14 = silk_SUB32( sLF_AR_shp_Q14, n_LF_Q14 ); cannam@154: psSS[ 0 ].LF_AR_Q14 = sLF_AR_shp_Q14; cannam@154: psSS[ 0 ].LPC_exc_Q14 = LPC_exc_Q14; cannam@154: psSS[ 0 ].xq_Q14 = xq_Q14; cannam@154: cannam@154: /* Update states for second best quantization */ cannam@154: cannam@154: /* Quantized excitation */ cannam@154: exc_Q14 = silk_LSHIFT32( psSS[ 1 ].Q_Q10, 4 ); cannam@154: if ( psDD->Seed < 0 ) { cannam@154: exc_Q14 = -exc_Q14; cannam@154: } cannam@154: cannam@154: cannam@154: /* Add predictions */ cannam@154: LPC_exc_Q14 = silk_ADD32( exc_Q14, LTP_pred_Q14 ); cannam@154: xq_Q14 = silk_ADD32( LPC_exc_Q14, LPC_pred_Q14 ); cannam@154: cannam@154: /* Update states */ cannam@154: sLF_AR_shp_Q14 = silk_SUB32( xq_Q14, n_AR_Q14 ); cannam@154: psSS[ 1 ].sLTP_shp_Q14 = silk_SUB32( sLF_AR_shp_Q14, n_LF_Q14 ); cannam@154: psSS[ 1 ].LF_AR_Q14 = sLF_AR_shp_Q14; cannam@154: psSS[ 1 ].LPC_exc_Q14 = LPC_exc_Q14; cannam@154: psSS[ 1 ].xq_Q14 = xq_Q14; cannam@154: } cannam@154: cannam@154: *smpl_buf_idx = ( *smpl_buf_idx - 1 ) % DECISION_DELAY; cannam@154: if( *smpl_buf_idx < 0 ) *smpl_buf_idx += DECISION_DELAY; cannam@154: last_smple_idx = ( *smpl_buf_idx + decisionDelay ) % DECISION_DELAY; cannam@154: cannam@154: /* Find winner */ cannam@154: RDmin_Q10 = psSampleState[ 0 ][ 0 ].RD_Q10; cannam@154: Winner_ind = 0; cannam@154: for( k = 1; k < nStatesDelayedDecision; k++ ) { cannam@154: if( psSampleState[ k ][ 0 ].RD_Q10 < RDmin_Q10 ) { cannam@154: RDmin_Q10 = psSampleState[ k ][ 0 ].RD_Q10; cannam@154: Winner_ind = k; cannam@154: } cannam@154: } cannam@154: cannam@154: /* Increase RD values of expired states */ cannam@154: Winner_rand_state = psDelDec[ Winner_ind ].RandState[ last_smple_idx ]; cannam@154: for( k = 0; k < nStatesDelayedDecision; k++ ) { cannam@154: if( psDelDec[ k ].RandState[ last_smple_idx ] != Winner_rand_state ) { cannam@154: psSampleState[ k ][ 0 ].RD_Q10 = silk_ADD32( psSampleState[ k ][ 0 ].RD_Q10, silk_int32_MAX >> 4 ); cannam@154: psSampleState[ k ][ 1 ].RD_Q10 = silk_ADD32( psSampleState[ k ][ 1 ].RD_Q10, silk_int32_MAX >> 4 ); cannam@154: silk_assert( psSampleState[ k ][ 0 ].RD_Q10 >= 0 ); cannam@154: } cannam@154: } cannam@154: cannam@154: /* Find worst in first set and best in second set */ cannam@154: RDmax_Q10 = psSampleState[ 0 ][ 0 ].RD_Q10; cannam@154: RDmin_Q10 = psSampleState[ 0 ][ 1 ].RD_Q10; cannam@154: RDmax_ind = 0; cannam@154: RDmin_ind = 0; cannam@154: for( k = 1; k < nStatesDelayedDecision; k++ ) { cannam@154: /* find worst in first set */ cannam@154: if( psSampleState[ k ][ 0 ].RD_Q10 > RDmax_Q10 ) { cannam@154: RDmax_Q10 = psSampleState[ k ][ 0 ].RD_Q10; cannam@154: RDmax_ind = k; cannam@154: } cannam@154: /* find best in second set */ cannam@154: if( psSampleState[ k ][ 1 ].RD_Q10 < RDmin_Q10 ) { cannam@154: RDmin_Q10 = psSampleState[ k ][ 1 ].RD_Q10; cannam@154: RDmin_ind = k; cannam@154: } cannam@154: } cannam@154: cannam@154: /* Replace a state if best from second set outperforms worst in first set */ cannam@154: if( RDmin_Q10 < RDmax_Q10 ) { cannam@154: silk_memcpy( ( (opus_int32 *)&psDelDec[ RDmax_ind ] ) + i, cannam@154: ( (opus_int32 *)&psDelDec[ RDmin_ind ] ) + i, sizeof( NSQ_del_dec_struct ) - i * sizeof( opus_int32) ); cannam@154: silk_memcpy( &psSampleState[ RDmax_ind ][ 0 ], &psSampleState[ RDmin_ind ][ 1 ], sizeof( NSQ_sample_struct ) ); cannam@154: } cannam@154: cannam@154: /* Write samples from winner to output and long-term filter states */ cannam@154: psDD = &psDelDec[ Winner_ind ]; cannam@154: if( subfr > 0 || i >= decisionDelay ) { cannam@154: pulses[ i - decisionDelay ] = (opus_int8)silk_RSHIFT_ROUND( psDD->Q_Q10[ last_smple_idx ], 10 ); cannam@154: xq[ i - decisionDelay ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( cannam@154: silk_SMULWW( psDD->Xq_Q14[ last_smple_idx ], delayedGain_Q10[ last_smple_idx ] ), 8 ) ); cannam@154: NSQ->sLTP_shp_Q14[ NSQ->sLTP_shp_buf_idx - decisionDelay ] = psDD->Shape_Q14[ last_smple_idx ]; cannam@154: sLTP_Q15[ NSQ->sLTP_buf_idx - decisionDelay ] = psDD->Pred_Q15[ last_smple_idx ]; cannam@154: } cannam@154: NSQ->sLTP_shp_buf_idx++; cannam@154: NSQ->sLTP_buf_idx++; cannam@154: cannam@154: /* Update states */ cannam@154: for( k = 0; k < nStatesDelayedDecision; k++ ) { cannam@154: psDD = &psDelDec[ k ]; cannam@154: psSS = &psSampleState[ k ][ 0 ]; cannam@154: psDD->LF_AR_Q14 = psSS->LF_AR_Q14; cannam@154: psDD->sLPC_Q14[ NSQ_LPC_BUF_LENGTH + i ] = psSS->xq_Q14; cannam@154: psDD->Xq_Q14[ *smpl_buf_idx ] = psSS->xq_Q14; cannam@154: psDD->Q_Q10[ *smpl_buf_idx ] = psSS->Q_Q10; cannam@154: psDD->Pred_Q15[ *smpl_buf_idx ] = silk_LSHIFT32( psSS->LPC_exc_Q14, 1 ); cannam@154: psDD->Shape_Q14[ *smpl_buf_idx ] = psSS->sLTP_shp_Q14; cannam@154: psDD->Seed = silk_ADD32_ovflw( psDD->Seed, silk_RSHIFT_ROUND( psSS->Q_Q10, 10 ) ); cannam@154: psDD->RandState[ *smpl_buf_idx ] = psDD->Seed; cannam@154: psDD->RD_Q10 = psSS->RD_Q10; cannam@154: } cannam@154: delayedGain_Q10[ *smpl_buf_idx ] = Gain_Q10; cannam@154: } cannam@154: /* Update LPC states */ cannam@154: for( k = 0; k < nStatesDelayedDecision; k++ ) { cannam@154: psDD = &psDelDec[ k ]; cannam@154: silk_memcpy( psDD->sLPC_Q14, &psDD->sLPC_Q14[ length ], NSQ_LPC_BUF_LENGTH * sizeof( opus_int32 ) ); cannam@154: } cannam@154: } cannam@154: cannam@154: #endif /* __NSQ_DEL_DEC_MIPSR1_H__ */