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