Convert all CRLF in the SILK code, tabs to spaces, and trailing
[opus.git] / silk / fixed / silk_find_pitch_lags_FIX.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 #include "silk_main_FIX.h"
29 #include "silk_tuning_parameters.h"
30
31 /* Find pitch lags */
32 void silk_find_pitch_lags_FIX(
33     silk_encoder_state_FIX          *psEnc,         /* I/O  encoder state                               */
34     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  encoder control                             */
35     opus_int16                       res[],          /* O    residual                                    */
36     const opus_int16                 x[]             /* I    Speech signal                               */
37 )
38 {
39     opus_int   buf_len, i, scale;
40     opus_int32 thrhld_Q15, res_nrg;
41     const opus_int16 *x_buf, *x_buf_ptr;
42     opus_int16 Wsig[      FIND_PITCH_LPC_WIN_MAX ], *Wsig_ptr;
43     opus_int32 auto_corr[ MAX_FIND_PITCH_LPC_ORDER + 1 ];
44     opus_int16 rc_Q15[    MAX_FIND_PITCH_LPC_ORDER ];
45     opus_int32 A_Q24[     MAX_FIND_PITCH_LPC_ORDER ];
46     opus_int16 A_Q12[     MAX_FIND_PITCH_LPC_ORDER ];
47
48     /******************************************/
49     /* Setup buffer lengths etc based on Fs   */
50     /******************************************/
51     buf_len = psEnc->sCmn.la_pitch + psEnc->sCmn.frame_length + psEnc->sCmn.ltp_mem_length;
52
53     /* Safty check */
54     SKP_assert( buf_len >= psEnc->sCmn.pitch_LPC_win_length );
55
56     x_buf = x - psEnc->sCmn.ltp_mem_length;
57
58     /*************************************/
59     /* Estimate LPC AR coefficients      */
60     /*************************************/
61
62     /* Calculate windowed signal */
63
64     /* First LA_LTP samples */
65     x_buf_ptr = x_buf + buf_len - psEnc->sCmn.pitch_LPC_win_length;
66     Wsig_ptr  = Wsig;
67     silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch );
68
69     /* Middle un - windowed samples */
70     Wsig_ptr  += psEnc->sCmn.la_pitch;
71     x_buf_ptr += psEnc->sCmn.la_pitch;
72     SKP_memcpy( Wsig_ptr, x_buf_ptr, ( psEnc->sCmn.pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ) ) * sizeof( opus_int16 ) );
73
74     /* Last LA_LTP samples */
75     Wsig_ptr  += psEnc->sCmn.pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 );
76     x_buf_ptr += psEnc->sCmn.pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 );
77     silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch );
78
79     /* Calculate autocorrelation sequence */
80     silk_autocorr( auto_corr, &scale, Wsig, psEnc->sCmn.pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 );
81
82     /* Add white noise, as fraction of energy */
83     auto_corr[ 0 ] = SKP_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], SILK_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) ) + 1;
84
85     /* Calculate the reflection coefficients using schur */
86     res_nrg = silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder );
87
88     /* Prediction gain */
89     psEncCtrl->predGain_Q16 = silk_DIV32_varQ( auto_corr[ 0 ], SKP_max_int( res_nrg, 1 ), 16 );
90
91     /* Convert reflection coefficients to prediction coefficients */
92     silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder );
93
94     /* Convert From 32 bit Q24 to 16 bit Q12 coefs */
95     for( i = 0; i < psEnc->sCmn.pitchEstimationLPCOrder; i++ ) {
96         A_Q12[ i ] = ( opus_int16 )SKP_SAT16( SKP_RSHIFT( A_Q24[ i ], 12 ) );
97     }
98
99     /* Do BWE */
100     silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, SILK_FIX_CONST( FIND_PITCH_BANDWITH_EXPANSION, 16 ) );
101
102     /*****************************************/
103     /* LPC analysis filtering                */
104     /*****************************************/
105     silk_LPC_analysis_filter( res, x_buf, A_Q12, buf_len, psEnc->sCmn.pitchEstimationLPCOrder );
106
107     if( psEnc->sCmn.indices.signalType != TYPE_NO_VOICE_ACTIVITY && psEnc->sCmn.first_frame_after_reset == 0 ) {
108         /* Threshold for pitch estimator */
109         thrhld_Q15 = SILK_FIX_CONST( 0.6, 15 );
110         thrhld_Q15 = SKP_SMLABB( thrhld_Q15, SILK_FIX_CONST( -0.004, 15 ), psEnc->sCmn.pitchEstimationLPCOrder );
111         thrhld_Q15 = SKP_SMLABB( thrhld_Q15, SILK_FIX_CONST( -0.1,   7  ), psEnc->sCmn.speech_activity_Q8 );
112         thrhld_Q15 = SKP_SMLABB( thrhld_Q15, SILK_FIX_CONST( -0.15,  15 ), SKP_RSHIFT( psEnc->sCmn.prevSignalType, 1 ) );
113         thrhld_Q15 = SKP_SMLAWB( thrhld_Q15, SILK_FIX_CONST( -0.1,   16 ), psEnc->sCmn.input_tilt_Q15 );
114         thrhld_Q15 = SKP_SAT16(  thrhld_Q15 );
115
116         /*****************************************/
117         /* Call pitch estimator                  */
118         /*****************************************/
119         if( silk_pitch_analysis_core( res, psEncCtrl->pitchL, &psEnc->sCmn.indices.lagIndex, &psEnc->sCmn.indices.contourIndex,
120                 &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->sCmn.pitchEstimationThreshold_Q16,
121                 ( opus_int16 )thrhld_Q15, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity, psEnc->sCmn.nb_subfr ) == 0 )
122         {
123             psEnc->sCmn.indices.signalType = TYPE_VOICED;
124         } else {
125             psEnc->sCmn.indices.signalType = TYPE_UNVOICED;
126         }
127     } else {
128         SKP_memset( psEncCtrl->pitchL, 0, sizeof( psEncCtrl->pitchL ) );
129         psEnc->sCmn.indices.lagIndex = 0;
130         psEnc->sCmn.indices.contourIndex = 0;
131         psEnc->LTPCorr_Q15 = 0;
132     }
133 }