Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /*********************************************************************** |
michael@0 | 2 | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
michael@0 | 3 | Redistribution and use in source and binary forms, with or without |
michael@0 | 4 | modification, are permitted provided that the following conditions |
michael@0 | 5 | are met: |
michael@0 | 6 | - Redistributions of source code must retain the above copyright notice, |
michael@0 | 7 | this list of conditions and the following disclaimer. |
michael@0 | 8 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 9 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 10 | documentation and/or other materials provided with the distribution. |
michael@0 | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
michael@0 | 12 | names of specific contributors, may be used to endorse or promote |
michael@0 | 13 | products derived from this software without specific prior written |
michael@0 | 14 | permission. |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
michael@0 | 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
michael@0 | 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
michael@0 | 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
michael@0 | 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
michael@0 | 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
michael@0 | 22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
michael@0 | 23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
michael@0 | 25 | POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | ***********************************************************************/ |
michael@0 | 27 | |
michael@0 | 28 | #ifdef HAVE_CONFIG_H |
michael@0 | 29 | #include "config.h" |
michael@0 | 30 | #endif |
michael@0 | 31 | |
michael@0 | 32 | #include "main_FIX.h" |
michael@0 | 33 | #include "stack_alloc.h" |
michael@0 | 34 | #include "tuning_parameters.h" |
michael@0 | 35 | |
michael@0 | 36 | /* Find pitch lags */ |
michael@0 | 37 | void silk_find_pitch_lags_FIX( |
michael@0 | 38 | silk_encoder_state_FIX *psEnc, /* I/O encoder state */ |
michael@0 | 39 | silk_encoder_control_FIX *psEncCtrl, /* I/O encoder control */ |
michael@0 | 40 | opus_int16 res[], /* O residual */ |
michael@0 | 41 | const opus_int16 x[], /* I Speech signal */ |
michael@0 | 42 | int arch /* I Run-time architecture */ |
michael@0 | 43 | ) |
michael@0 | 44 | { |
michael@0 | 45 | opus_int buf_len, i, scale; |
michael@0 | 46 | opus_int32 thrhld_Q13, res_nrg; |
michael@0 | 47 | const opus_int16 *x_buf, *x_buf_ptr; |
michael@0 | 48 | VARDECL( opus_int16, Wsig ); |
michael@0 | 49 | opus_int16 *Wsig_ptr; |
michael@0 | 50 | opus_int32 auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ]; |
michael@0 | 51 | opus_int16 rc_Q15[ MAX_FIND_PITCH_LPC_ORDER ]; |
michael@0 | 52 | opus_int32 A_Q24[ MAX_FIND_PITCH_LPC_ORDER ]; |
michael@0 | 53 | opus_int16 A_Q12[ MAX_FIND_PITCH_LPC_ORDER ]; |
michael@0 | 54 | SAVE_STACK; |
michael@0 | 55 | |
michael@0 | 56 | /******************************************/ |
michael@0 | 57 | /* Set up buffer lengths etc based on Fs */ |
michael@0 | 58 | /******************************************/ |
michael@0 | 59 | buf_len = psEnc->sCmn.la_pitch + psEnc->sCmn.frame_length + psEnc->sCmn.ltp_mem_length; |
michael@0 | 60 | |
michael@0 | 61 | /* Safety check */ |
michael@0 | 62 | silk_assert( buf_len >= psEnc->sCmn.pitch_LPC_win_length ); |
michael@0 | 63 | |
michael@0 | 64 | x_buf = x - psEnc->sCmn.ltp_mem_length; |
michael@0 | 65 | |
michael@0 | 66 | /*************************************/ |
michael@0 | 67 | /* Estimate LPC AR coefficients */ |
michael@0 | 68 | /*************************************/ |
michael@0 | 69 | |
michael@0 | 70 | /* Calculate windowed signal */ |
michael@0 | 71 | |
michael@0 | 72 | ALLOC( Wsig, psEnc->sCmn.pitch_LPC_win_length, opus_int16 ); |
michael@0 | 73 | |
michael@0 | 74 | /* First LA_LTP samples */ |
michael@0 | 75 | x_buf_ptr = x_buf + buf_len - psEnc->sCmn.pitch_LPC_win_length; |
michael@0 | 76 | Wsig_ptr = Wsig; |
michael@0 | 77 | silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch ); |
michael@0 | 78 | |
michael@0 | 79 | /* Middle un - windowed samples */ |
michael@0 | 80 | Wsig_ptr += psEnc->sCmn.la_pitch; |
michael@0 | 81 | x_buf_ptr += psEnc->sCmn.la_pitch; |
michael@0 | 82 | silk_memcpy( Wsig_ptr, x_buf_ptr, ( psEnc->sCmn.pitch_LPC_win_length - silk_LSHIFT( psEnc->sCmn.la_pitch, 1 ) ) * sizeof( opus_int16 ) ); |
michael@0 | 83 | |
michael@0 | 84 | /* Last LA_LTP samples */ |
michael@0 | 85 | Wsig_ptr += psEnc->sCmn.pitch_LPC_win_length - silk_LSHIFT( psEnc->sCmn.la_pitch, 1 ); |
michael@0 | 86 | x_buf_ptr += psEnc->sCmn.pitch_LPC_win_length - silk_LSHIFT( psEnc->sCmn.la_pitch, 1 ); |
michael@0 | 87 | silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch ); |
michael@0 | 88 | |
michael@0 | 89 | /* Calculate autocorrelation sequence */ |
michael@0 | 90 | silk_autocorr( auto_corr, &scale, Wsig, psEnc->sCmn.pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1, arch ); |
michael@0 | 91 | |
michael@0 | 92 | /* Add white noise, as fraction of energy */ |
michael@0 | 93 | auto_corr[ 0 ] = silk_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], SILK_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) ) + 1; |
michael@0 | 94 | |
michael@0 | 95 | /* Calculate the reflection coefficients using schur */ |
michael@0 | 96 | res_nrg = silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder ); |
michael@0 | 97 | |
michael@0 | 98 | /* Prediction gain */ |
michael@0 | 99 | psEncCtrl->predGain_Q16 = silk_DIV32_varQ( auto_corr[ 0 ], silk_max_int( res_nrg, 1 ), 16 ); |
michael@0 | 100 | |
michael@0 | 101 | /* Convert reflection coefficients to prediction coefficients */ |
michael@0 | 102 | silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder ); |
michael@0 | 103 | |
michael@0 | 104 | /* Convert From 32 bit Q24 to 16 bit Q12 coefs */ |
michael@0 | 105 | for( i = 0; i < psEnc->sCmn.pitchEstimationLPCOrder; i++ ) { |
michael@0 | 106 | A_Q12[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT( A_Q24[ i ], 12 ) ); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | /* Do BWE */ |
michael@0 | 110 | silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, SILK_FIX_CONST( FIND_PITCH_BANDWIDTH_EXPANSION, 16 ) ); |
michael@0 | 111 | |
michael@0 | 112 | /*****************************************/ |
michael@0 | 113 | /* LPC analysis filtering */ |
michael@0 | 114 | /*****************************************/ |
michael@0 | 115 | silk_LPC_analysis_filter( res, x_buf, A_Q12, buf_len, psEnc->sCmn.pitchEstimationLPCOrder ); |
michael@0 | 116 | |
michael@0 | 117 | if( psEnc->sCmn.indices.signalType != TYPE_NO_VOICE_ACTIVITY && psEnc->sCmn.first_frame_after_reset == 0 ) { |
michael@0 | 118 | /* Threshold for pitch estimator */ |
michael@0 | 119 | thrhld_Q13 = SILK_FIX_CONST( 0.6, 13 ); |
michael@0 | 120 | thrhld_Q13 = silk_SMLABB( thrhld_Q13, SILK_FIX_CONST( -0.004, 13 ), psEnc->sCmn.pitchEstimationLPCOrder ); |
michael@0 | 121 | thrhld_Q13 = silk_SMLAWB( thrhld_Q13, SILK_FIX_CONST( -0.1, 21 ), psEnc->sCmn.speech_activity_Q8 ); |
michael@0 | 122 | thrhld_Q13 = silk_SMLABB( thrhld_Q13, SILK_FIX_CONST( -0.15, 13 ), silk_RSHIFT( psEnc->sCmn.prevSignalType, 1 ) ); |
michael@0 | 123 | thrhld_Q13 = silk_SMLAWB( thrhld_Q13, SILK_FIX_CONST( -0.1, 14 ), psEnc->sCmn.input_tilt_Q15 ); |
michael@0 | 124 | thrhld_Q13 = silk_SAT16( thrhld_Q13 ); |
michael@0 | 125 | |
michael@0 | 126 | /*****************************************/ |
michael@0 | 127 | /* Call pitch estimator */ |
michael@0 | 128 | /*****************************************/ |
michael@0 | 129 | if( silk_pitch_analysis_core( res, psEncCtrl->pitchL, &psEnc->sCmn.indices.lagIndex, &psEnc->sCmn.indices.contourIndex, |
michael@0 | 130 | &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->sCmn.pitchEstimationThreshold_Q16, |
michael@0 | 131 | (opus_int)thrhld_Q13, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr, |
michael@0 | 132 | psEnc->sCmn.arch) == 0 ) |
michael@0 | 133 | { |
michael@0 | 134 | psEnc->sCmn.indices.signalType = TYPE_VOICED; |
michael@0 | 135 | } else { |
michael@0 | 136 | psEnc->sCmn.indices.signalType = TYPE_UNVOICED; |
michael@0 | 137 | } |
michael@0 | 138 | } else { |
michael@0 | 139 | silk_memset( psEncCtrl->pitchL, 0, sizeof( psEncCtrl->pitchL ) ); |
michael@0 | 140 | psEnc->sCmn.indices.lagIndex = 0; |
michael@0 | 141 | psEnc->sCmn.indices.contourIndex = 0; |
michael@0 | 142 | psEnc->LTPCorr_Q15 = 0; |
michael@0 | 143 | } |
michael@0 | 144 | RESTORE_STACK; |
michael@0 | 145 | } |