1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/fixed/find_pitch_lags_FIX.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,145 @@ 1.4 +/*********************************************************************** 1.5 +Copyright (c) 2006-2011, Skype Limited. All rights reserved. 1.6 +Redistribution and use in source and binary forms, with or without 1.7 +modification, are permitted provided that the following conditions 1.8 +are met: 1.9 +- Redistributions of source code must retain the above copyright notice, 1.10 +this list of conditions and the following disclaimer. 1.11 +- Redistributions in binary form must reproduce the above copyright 1.12 +notice, this list of conditions and the following disclaimer in the 1.13 +documentation and/or other materials provided with the distribution. 1.14 +- Neither the name of Internet Society, IETF or IETF Trust, nor the 1.15 +names of specific contributors, may be used to endorse or promote 1.16 +products derived from this software without specific prior written 1.17 +permission. 1.18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.19 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.20 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.21 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 1.22 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.23 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.24 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.25 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.26 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.27 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.28 +POSSIBILITY OF SUCH DAMAGE. 1.29 +***********************************************************************/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +#include "config.h" 1.33 +#endif 1.34 + 1.35 +#include "main_FIX.h" 1.36 +#include "stack_alloc.h" 1.37 +#include "tuning_parameters.h" 1.38 + 1.39 +/* Find pitch lags */ 1.40 +void silk_find_pitch_lags_FIX( 1.41 + silk_encoder_state_FIX *psEnc, /* I/O encoder state */ 1.42 + silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ 1.43 + opus_int16 res[], /* O residual */ 1.44 + const opus_int16 x[], /* I Speech signal */ 1.45 + int arch /* I Run-time architecture */ 1.46 +) 1.47 +{ 1.48 + opus_int buf_len, i, scale; 1.49 + opus_int32 thrhld_Q13, res_nrg; 1.50 + const opus_int16 *x_buf, *x_buf_ptr; 1.51 + VARDECL( opus_int16, Wsig ); 1.52 + opus_int16 *Wsig_ptr; 1.53 + opus_int32 auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ]; 1.54 + opus_int16 rc_Q15[ MAX_FIND_PITCH_LPC_ORDER ]; 1.55 + opus_int32 A_Q24[ MAX_FIND_PITCH_LPC_ORDER ]; 1.56 + opus_int16 A_Q12[ MAX_FIND_PITCH_LPC_ORDER ]; 1.57 + SAVE_STACK; 1.58 + 1.59 + /******************************************/ 1.60 + /* Set up buffer lengths etc based on Fs */ 1.61 + /******************************************/ 1.62 + buf_len = psEnc->sCmn.la_pitch + psEnc->sCmn.frame_length + psEnc->sCmn.ltp_mem_length; 1.63 + 1.64 + /* Safety check */ 1.65 + silk_assert( buf_len >= psEnc->sCmn.pitch_LPC_win_length ); 1.66 + 1.67 + x_buf = x - psEnc->sCmn.ltp_mem_length; 1.68 + 1.69 + /*************************************/ 1.70 + /* Estimate LPC AR coefficients */ 1.71 + /*************************************/ 1.72 + 1.73 + /* Calculate windowed signal */ 1.74 + 1.75 + ALLOC( Wsig, psEnc->sCmn.pitch_LPC_win_length, opus_int16 ); 1.76 + 1.77 + /* First LA_LTP samples */ 1.78 + x_buf_ptr = x_buf + buf_len - psEnc->sCmn.pitch_LPC_win_length; 1.79 + Wsig_ptr = Wsig; 1.80 + silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch ); 1.81 + 1.82 + /* Middle un - windowed samples */ 1.83 + Wsig_ptr += psEnc->sCmn.la_pitch; 1.84 + x_buf_ptr += psEnc->sCmn.la_pitch; 1.85 + silk_memcpy( Wsig_ptr, x_buf_ptr, ( psEnc->sCmn.pitch_LPC_win_length - silk_LSHIFT( psEnc->sCmn.la_pitch, 1 ) ) * sizeof( opus_int16 ) ); 1.86 + 1.87 + /* Last LA_LTP samples */ 1.88 + Wsig_ptr += psEnc->sCmn.pitch_LPC_win_length - silk_LSHIFT( psEnc->sCmn.la_pitch, 1 ); 1.89 + x_buf_ptr += psEnc->sCmn.pitch_LPC_win_length - silk_LSHIFT( psEnc->sCmn.la_pitch, 1 ); 1.90 + silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch ); 1.91 + 1.92 + /* Calculate autocorrelation sequence */ 1.93 + silk_autocorr( auto_corr, &scale, Wsig, psEnc->sCmn.pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1, arch ); 1.94 + 1.95 + /* Add white noise, as fraction of energy */ 1.96 + auto_corr[ 0 ] = silk_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], SILK_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) ) + 1; 1.97 + 1.98 + /* Calculate the reflection coefficients using schur */ 1.99 + res_nrg = silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder ); 1.100 + 1.101 + /* Prediction gain */ 1.102 + psEncCtrl->predGain_Q16 = silk_DIV32_varQ( auto_corr[ 0 ], silk_max_int( res_nrg, 1 ), 16 ); 1.103 + 1.104 + /* Convert reflection coefficients to prediction coefficients */ 1.105 + silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder ); 1.106 + 1.107 + /* Convert From 32 bit Q24 to 16 bit Q12 coefs */ 1.108 + for( i = 0; i < psEnc->sCmn.pitchEstimationLPCOrder; i++ ) { 1.109 + A_Q12[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT( A_Q24[ i ], 12 ) ); 1.110 + } 1.111 + 1.112 + /* Do BWE */ 1.113 + silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, SILK_FIX_CONST( FIND_PITCH_BANDWIDTH_EXPANSION, 16 ) ); 1.114 + 1.115 + /*****************************************/ 1.116 + /* LPC analysis filtering */ 1.117 + /*****************************************/ 1.118 + silk_LPC_analysis_filter( res, x_buf, A_Q12, buf_len, psEnc->sCmn.pitchEstimationLPCOrder ); 1.119 + 1.120 + if( psEnc->sCmn.indices.signalType != TYPE_NO_VOICE_ACTIVITY && psEnc->sCmn.first_frame_after_reset == 0 ) { 1.121 + /* Threshold for pitch estimator */ 1.122 + thrhld_Q13 = SILK_FIX_CONST( 0.6, 13 ); 1.123 + thrhld_Q13 = silk_SMLABB( thrhld_Q13, SILK_FIX_CONST( -0.004, 13 ), psEnc->sCmn.pitchEstimationLPCOrder ); 1.124 + thrhld_Q13 = silk_SMLAWB( thrhld_Q13, SILK_FIX_CONST( -0.1, 21 ), psEnc->sCmn.speech_activity_Q8 ); 1.125 + thrhld_Q13 = silk_SMLABB( thrhld_Q13, SILK_FIX_CONST( -0.15, 13 ), silk_RSHIFT( psEnc->sCmn.prevSignalType, 1 ) ); 1.126 + thrhld_Q13 = silk_SMLAWB( thrhld_Q13, SILK_FIX_CONST( -0.1, 14 ), psEnc->sCmn.input_tilt_Q15 ); 1.127 + thrhld_Q13 = silk_SAT16( thrhld_Q13 ); 1.128 + 1.129 + /*****************************************/ 1.130 + /* Call pitch estimator */ 1.131 + /*****************************************/ 1.132 + if( silk_pitch_analysis_core( res, psEncCtrl->pitchL, &psEnc->sCmn.indices.lagIndex, &psEnc->sCmn.indices.contourIndex, 1.133 + &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->sCmn.pitchEstimationThreshold_Q16, 1.134 + (opus_int)thrhld_Q13, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr, 1.135 + psEnc->sCmn.arch) == 0 ) 1.136 + { 1.137 + psEnc->sCmn.indices.signalType = TYPE_VOICED; 1.138 + } else { 1.139 + psEnc->sCmn.indices.signalType = TYPE_UNVOICED; 1.140 + } 1.141 + } else { 1.142 + silk_memset( psEncCtrl->pitchL, 0, sizeof( psEncCtrl->pitchL ) ); 1.143 + psEnc->sCmn.indices.lagIndex = 0; 1.144 + psEnc->sCmn.indices.contourIndex = 0; 1.145 + psEnc->LTPCorr_Q15 = 0; 1.146 + } 1.147 + RESTORE_STACK; 1.148 +}