Fix stack corruption with high rate SILK encoding added by the recent CBR changes...
[opus.git] / silk / float / find_pitch_lags_FLP.c
1 /***********************************************************************
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include "main_FLP.h"
34 #include "tuning_parameters.h"
35
36 void silk_find_pitch_lags_FLP(
37     silk_encoder_state_FLP          *psEnc,             /* I/O  Encoder state FLP                       */
38     silk_encoder_control_FLP        *psEncCtrl,         /* I/O  Encoder control FLP                     */
39           silk_float                 res[],              /* O    Residual                                */
40     const silk_float                 x[]                 /* I    Speech signal                           */
41 )
42 {
43     opus_int   buf_len;
44     silk_float thrhld, res_nrg;
45     const silk_float *x_buf_ptr, *x_buf;
46     silk_float auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ];
47     silk_float A[         MAX_FIND_PITCH_LPC_ORDER ];
48     silk_float refl_coef[ MAX_FIND_PITCH_LPC_ORDER ];
49     silk_float Wsig[      FIND_PITCH_LPC_WIN_MAX ];
50     silk_float *Wsig_ptr;
51
52     /******************************************/
53     /* Setup 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     /* Safty 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 coeficients            */
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_FLP( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch );
72
73     /* Middle non-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 - ( psEnc->sCmn.la_pitch << 1 ) ) * sizeof( silk_float ) );
77
78     /* Last LA_LTP samples */
79     Wsig_ptr  += psEnc->sCmn.pitch_LPC_win_length - ( psEnc->sCmn.la_pitch << 1 );
80     x_buf_ptr += psEnc->sCmn.pitch_LPC_win_length - ( psEnc->sCmn.la_pitch << 1 );
81     silk_apply_sine_window_FLP( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch );
82
83     /* Calculate autocorrelation sequence */
84     silk_autocorrelation_FLP( auto_corr, Wsig, psEnc->sCmn.pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 );
85
86     /* Add white noise, as a fraction of the energy */
87     auto_corr[ 0 ] += auto_corr[ 0 ] * FIND_PITCH_WHITE_NOISE_FRACTION + 1;
88
89     /* Calculate the reflection coefficients using Schur */
90     res_nrg = silk_schur_FLP( refl_coef, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder );
91
92     /* Prediction gain */
93     psEncCtrl->predGain = auto_corr[ 0 ] / silk_max_float( res_nrg, 1.0f );
94
95     /* Convert reflection coefficients to prediction coefficients */
96     silk_k2a_FLP( A, refl_coef, psEnc->sCmn.pitchEstimationLPCOrder );
97
98     /* Bandwidth expansion */
99     silk_bwexpander_FLP( A, psEnc->sCmn.pitchEstimationLPCOrder, FIND_PITCH_BANDWITH_EXPANSION );
100
101     /*****************************************/
102     /* LPC analysis filtering                */
103     /*****************************************/
104     silk_LPC_analysis_filter_FLP( res, A, x_buf, buf_len, psEnc->sCmn.pitchEstimationLPCOrder );
105
106     if( psEnc->sCmn.indices.signalType != TYPE_NO_VOICE_ACTIVITY && psEnc->sCmn.first_frame_after_reset == 0 ) {
107         /* Threshold for pitch estimator */
108         thrhld  = 0.6f;
109         thrhld -= 0.004f * psEnc->sCmn.pitchEstimationLPCOrder;
110         thrhld -= 0.1f   * psEnc->sCmn.speech_activity_Q8 * ( 1.0f /  256.0f );
111         thrhld -= 0.15f  * (psEnc->sCmn.prevSignalType >> 1);
112         thrhld -= 0.1f   * psEnc->sCmn.input_tilt_Q15 * ( 1.0f / 32768.0f );
113
114         /*****************************************/
115         /* Call Pitch estimator                  */
116         /*****************************************/
117         if( silk_pitch_analysis_core_FLP( res, psEncCtrl->pitchL, &psEnc->sCmn.indices.lagIndex,
118             &psEnc->sCmn.indices.contourIndex, &psEnc->LTPCorr, psEnc->sCmn.prevLag, psEnc->sCmn.pitchEstimationThreshold_Q16 / 65536.0f,
119             thrhld, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr ) == 0 )
120         {
121             psEnc->sCmn.indices.signalType = TYPE_VOICED;
122         } else {
123             psEnc->sCmn.indices.signalType = TYPE_UNVOICED;
124         }
125     } else {
126         silk_memset( psEncCtrl->pitchL, 0, sizeof( psEncCtrl->pitchL ) );
127         psEnc->sCmn.indices.lagIndex = 0;
128         psEnc->sCmn.indices.contourIndex = 0;
129         psEnc->LTPCorr = 0;
130     }
131 }