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