Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include Chris@69: #include "main_FLP.h" Chris@69: #include "tuning_parameters.h" Chris@69: Chris@69: void silk_find_pitch_lags_FLP( Chris@69: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ Chris@69: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ Chris@69: silk_float res[], /* O Residual */ Chris@69: const silk_float x[], /* I Speech signal */ Chris@69: int arch /* I Run-time architecture */ Chris@69: ) Chris@69: { Chris@69: opus_int buf_len; Chris@69: silk_float thrhld, res_nrg; Chris@69: const silk_float *x_buf_ptr, *x_buf; Chris@69: silk_float auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ]; Chris@69: silk_float A[ MAX_FIND_PITCH_LPC_ORDER ]; Chris@69: silk_float refl_coef[ MAX_FIND_PITCH_LPC_ORDER ]; Chris@69: silk_float Wsig[ FIND_PITCH_LPC_WIN_MAX ]; Chris@69: silk_float *Wsig_ptr; Chris@69: Chris@69: /******************************************/ Chris@69: /* Set up buffer lengths etc based on Fs */ Chris@69: /******************************************/ Chris@69: buf_len = psEnc->sCmn.la_pitch + psEnc->sCmn.frame_length + psEnc->sCmn.ltp_mem_length; Chris@69: Chris@69: /* Safety check */ Chris@69: celt_assert( buf_len >= psEnc->sCmn.pitch_LPC_win_length ); Chris@69: Chris@69: x_buf = x - psEnc->sCmn.ltp_mem_length; Chris@69: Chris@69: /******************************************/ Chris@69: /* Estimate LPC AR coeficients */ Chris@69: /******************************************/ Chris@69: Chris@69: /* Calculate windowed signal */ Chris@69: Chris@69: /* First LA_LTP samples */ Chris@69: x_buf_ptr = x_buf + buf_len - psEnc->sCmn.pitch_LPC_win_length; Chris@69: Wsig_ptr = Wsig; Chris@69: silk_apply_sine_window_FLP( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch ); Chris@69: Chris@69: /* Middle non-windowed samples */ Chris@69: Wsig_ptr += psEnc->sCmn.la_pitch; Chris@69: x_buf_ptr += psEnc->sCmn.la_pitch; Chris@69: silk_memcpy( Wsig_ptr, x_buf_ptr, ( psEnc->sCmn.pitch_LPC_win_length - ( psEnc->sCmn.la_pitch << 1 ) ) * sizeof( silk_float ) ); Chris@69: Chris@69: /* Last LA_LTP samples */ Chris@69: Wsig_ptr += psEnc->sCmn.pitch_LPC_win_length - ( psEnc->sCmn.la_pitch << 1 ); Chris@69: x_buf_ptr += psEnc->sCmn.pitch_LPC_win_length - ( psEnc->sCmn.la_pitch << 1 ); Chris@69: silk_apply_sine_window_FLP( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch ); Chris@69: Chris@69: /* Calculate autocorrelation sequence */ Chris@69: silk_autocorrelation_FLP( auto_corr, Wsig, psEnc->sCmn.pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 ); Chris@69: Chris@69: /* Add white noise, as a fraction of the energy */ Chris@69: auto_corr[ 0 ] += auto_corr[ 0 ] * FIND_PITCH_WHITE_NOISE_FRACTION + 1; Chris@69: Chris@69: /* Calculate the reflection coefficients using Schur */ Chris@69: res_nrg = silk_schur_FLP( refl_coef, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder ); Chris@69: Chris@69: /* Prediction gain */ Chris@69: psEncCtrl->predGain = auto_corr[ 0 ] / silk_max_float( res_nrg, 1.0f ); Chris@69: Chris@69: /* Convert reflection coefficients to prediction coefficients */ Chris@69: silk_k2a_FLP( A, refl_coef, psEnc->sCmn.pitchEstimationLPCOrder ); Chris@69: Chris@69: /* Bandwidth expansion */ Chris@69: silk_bwexpander_FLP( A, psEnc->sCmn.pitchEstimationLPCOrder, FIND_PITCH_BANDWIDTH_EXPANSION ); Chris@69: Chris@69: /*****************************************/ Chris@69: /* LPC analysis filtering */ Chris@69: /*****************************************/ Chris@69: silk_LPC_analysis_filter_FLP( res, A, x_buf, buf_len, psEnc->sCmn.pitchEstimationLPCOrder ); Chris@69: Chris@69: if( psEnc->sCmn.indices.signalType != TYPE_NO_VOICE_ACTIVITY && psEnc->sCmn.first_frame_after_reset == 0 ) { Chris@69: /* Threshold for pitch estimator */ Chris@69: thrhld = 0.6f; Chris@69: thrhld -= 0.004f * psEnc->sCmn.pitchEstimationLPCOrder; Chris@69: thrhld -= 0.1f * psEnc->sCmn.speech_activity_Q8 * ( 1.0f / 256.0f ); Chris@69: thrhld -= 0.15f * (psEnc->sCmn.prevSignalType >> 1); Chris@69: thrhld -= 0.1f * psEnc->sCmn.input_tilt_Q15 * ( 1.0f / 32768.0f ); Chris@69: Chris@69: /*****************************************/ Chris@69: /* Call Pitch estimator */ Chris@69: /*****************************************/ Chris@69: if( silk_pitch_analysis_core_FLP( res, psEncCtrl->pitchL, &psEnc->sCmn.indices.lagIndex, Chris@69: &psEnc->sCmn.indices.contourIndex, &psEnc->LTPCorr, psEnc->sCmn.prevLag, psEnc->sCmn.pitchEstimationThreshold_Q16 / 65536.0f, Chris@69: thrhld, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr, arch ) == 0 ) Chris@69: { Chris@69: psEnc->sCmn.indices.signalType = TYPE_VOICED; Chris@69: } else { Chris@69: psEnc->sCmn.indices.signalType = TYPE_UNVOICED; Chris@69: } Chris@69: } else { Chris@69: silk_memset( psEncCtrl->pitchL, 0, sizeof( psEncCtrl->pitchL ) ); Chris@69: psEnc->sCmn.indices.lagIndex = 0; Chris@69: psEnc->sCmn.indices.contourIndex = 0; Chris@69: psEnc->LTPCorr = 0; Chris@69: } Chris@69: }