1 /***********************************************************************
2 Copyright (c) 2006-2010, 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 "SKP_Silk_main_FIX.h"
29 
30 /* Find pitch lags */
SKP_Silk_find_pitch_lags_FIX(SKP_Silk_encoder_state_FIX * psEnc,SKP_Silk_encoder_control_FIX * psEncCtrl,SKP_int16 res[],const SKP_int16 x[])31 void SKP_Silk_find_pitch_lags_FIX(
32     SKP_Silk_encoder_state_FIX      *psEnc,         /* I/O  encoder state                               */
33     SKP_Silk_encoder_control_FIX    *psEncCtrl,     /* I/O  encoder control                             */
34     SKP_int16                       res[],          /* O    residual                                    */
35     const SKP_int16                 x[]             /* I    Speech signal                               */
36 )
37 {
38     SKP_Silk_predict_state_FIX *psPredSt = &psEnc->sPred;
39     SKP_int   buf_len, i, scale;
40     SKP_int32 thrhld_Q15;
41     const SKP_int16 *x_buf, *x_buf_ptr;
42     SKP_int16 Wsig[      FIND_PITCH_LPC_WIN_MAX ], *Wsig_ptr;
43     SKP_int32 auto_corr[ FIND_PITCH_LPC_ORDER_MAX + 1 ];
44     SKP_int16 rc_Q15[    FIND_PITCH_LPC_ORDER_MAX ];
45     SKP_int32 A_Q24[     FIND_PITCH_LPC_ORDER_MAX ];
46     SKP_int32 FiltState[ FIND_PITCH_LPC_ORDER_MAX ];
47     SKP_int16 A_Q12[     FIND_PITCH_LPC_ORDER_MAX ];
48 
49     /******************************************/
50     /* Setup buffer lengths etc based of Fs.  */
51     /******************************************/
52     buf_len = SKP_ADD_LSHIFT( psEnc->sCmn.la_pitch, psEnc->sCmn.frame_length, 1 );
53 
54     /* Safty check */
55     SKP_assert( buf_len >= psPredSt->pitch_LPC_win_length );
56 
57     x_buf = x - psEnc->sCmn.frame_length;
58 
59     /*************************************/
60     /* Estimate LPC AR coeficients */
61     /*************************************/
62 
63     /* Calculate windowed signal */
64 
65     /* First LA_LTP samples */
66     x_buf_ptr = x_buf + buf_len - psPredSt->pitch_LPC_win_length;
67     Wsig_ptr  = Wsig;
68     SKP_Silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 1, psEnc->sCmn.la_pitch );
69 
70     /* Middle un - windowed samples */
71     Wsig_ptr  += psEnc->sCmn.la_pitch;
72     x_buf_ptr += psEnc->sCmn.la_pitch;
73     SKP_memcpy( Wsig_ptr, x_buf_ptr, ( psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 ) ) * sizeof( SKP_int16 ) );
74 
75     /* Last LA_LTP samples */
76     Wsig_ptr  += psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 );
77     x_buf_ptr += psPredSt->pitch_LPC_win_length - SKP_LSHIFT( psEnc->sCmn.la_pitch, 1 );
78     SKP_Silk_apply_sine_window( Wsig_ptr, x_buf_ptr, 2, psEnc->sCmn.la_pitch );
79 
80     /* Calculate autocorrelation sequence */
81     SKP_Silk_autocorr( auto_corr, &scale, Wsig, psPredSt->pitch_LPC_win_length, psEnc->sCmn.pitchEstimationLPCOrder + 1 );
82 
83     /* add white noise, as fraction of energy */
84     auto_corr[ 0 ] = SKP_SMLAWB( auto_corr[ 0 ], auto_corr[ 0 ], FIND_PITCH_WHITE_NOISE_FRACTION_Q16 );
85 
86     /* calculate the reflection coefficients using schur */
87     SKP_Silk_schur( rc_Q15, auto_corr, psEnc->sCmn.pitchEstimationLPCOrder );
88 
89     /* convert reflection coefficients to prediction coefficients */
90     SKP_Silk_k2a( A_Q24, rc_Q15, psEnc->sCmn.pitchEstimationLPCOrder );
91 
92     /* Convert From 32 bit Q24 to 16 bit Q12 coefs */
93     for( i = 0; i < psEnc->sCmn.pitchEstimationLPCOrder; i++ ) {
94         A_Q12[ i ] = ( SKP_int16 )SKP_SAT16( SKP_RSHIFT( A_Q24[ i ], 12 ) );
95     }
96 
97     /* Do BWE */
98     SKP_Silk_bwexpander( A_Q12, psEnc->sCmn.pitchEstimationLPCOrder, FIND_PITCH_BANDWITH_EXPANSION_Q16 );
99 
100     /*****************************************/
101     /* LPC analysis filtering                */
102     /*****************************************/
103     SKP_memset( FiltState, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof(SKP_int32) ); /* Not really nessacary, but Valgrind will complain otherwise */
104     SKP_Silk_MA_Prediction( x_buf, A_Q12, FiltState, res, buf_len, psEnc->sCmn.pitchEstimationLPCOrder );
105     SKP_memset( res, 0, psEnc->sCmn.pitchEstimationLPCOrder * sizeof( SKP_int16 ) );
106 
107     /* Threshold for pitch estimator */
108     thrhld_Q15 = ( 1 << 14 ); // 0.5f in Q15
109     thrhld_Q15 = SKP_SMLABB( thrhld_Q15, -131, psEnc->sCmn.pitchEstimationLPCOrder );
110     thrhld_Q15 = SKP_SMLABB( thrhld_Q15,  -13, ( SKP_int16 )SKP_Silk_SQRT_APPROX( SKP_LSHIFT( ( SKP_int32 )psEnc->speech_activity_Q8, 8 ) ) );
111     thrhld_Q15 = SKP_SMLABB( thrhld_Q15, 4587, psEnc->sCmn.prev_sigtype );
112     thrhld_Q15 = SKP_MLA(    thrhld_Q15,  -31, SKP_RSHIFT( psEncCtrl->input_tilt_Q15, 8 ) );
113     thrhld_Q15 = SKP_SAT16(  thrhld_Q15 );
114 
115     /*****************************************/
116     /* Call Pitch estimator */
117     /*****************************************/
118     psEncCtrl->sCmn.sigtype = SKP_Silk_pitch_analysis_core( res, psEncCtrl->sCmn.pitchL, &psEncCtrl->sCmn.lagIndex,
119         &psEncCtrl->sCmn.contourIndex, &psEnc->LTPCorr_Q15, psEnc->sCmn.prevLag, psEnc->pitchEstimationThreshold_Q16,
120         ( SKP_int16 )thrhld_Q15, psEnc->sCmn.fs_kHz, psEnc->sCmn.pitchEstimationComplexity );
121 }
122