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, are permitted provided that the following conditions
5 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 Internet Society, IETF or IETF Trust, nor the
12 names of specific contributors, may be used to endorse or promote
13 products derived from this software without specific prior written
14 permission.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "main_FIX.h"
33 #include "stack_alloc.h"
34 #include "tuning_parameters.h"
35 
36 #if defined(MIPSr1_ASM)
37 #include "mips/prefilter_FIX_mipsr1.h"
38 #endif
39 
40 
41 #if !defined(OVERRIDE_silk_warped_LPC_analysis_filter_FIX)
42 #define silk_warped_LPC_analysis_filter_FIX(state, res_Q2, coef_Q13, input, lambda_Q16, length, order, arch) \
43     ((void)(arch),silk_warped_LPC_analysis_filter_FIX_c(state, res_Q2, coef_Q13, input, lambda_Q16, length, order))
44 #endif
45 
46 /* Prefilter for finding Quantizer input signal */
47 static OPUS_INLINE void silk_prefilt_FIX(
48     silk_prefilter_state_FIX    *P,                         /* I/O  state                               */
49     opus_int32                  st_res_Q12[],               /* I    short term residual signal          */
50     opus_int32                  xw_Q3[],                    /* O    prefiltered signal                  */
51     opus_int32                  HarmShapeFIRPacked_Q12,     /* I    Harmonic shaping coeficients        */
52     opus_int                    Tilt_Q14,                   /* I    Tilt shaping coeficient             */
53     opus_int32                  LF_shp_Q14,                 /* I    Low-frequancy shaping coeficients   */
54     opus_int                    lag,                        /* I    Lag for harmonic shaping            */
55     opus_int                    length                      /* I    Length of signals                   */
56 );
57 
silk_warped_LPC_analysis_filter_FIX_c(opus_int32 state[],opus_int32 res_Q2[],const opus_int16 coef_Q13[],const opus_int16 input[],const opus_int16 lambda_Q16,const opus_int length,const opus_int order)58 void silk_warped_LPC_analysis_filter_FIX_c(
59           opus_int32            state[],                    /* I/O  State [order + 1]                   */
60           opus_int32            res_Q2[],                   /* O    Residual signal [length]            */
61     const opus_int16            coef_Q13[],                 /* I    Coefficients [order]                */
62     const opus_int16            input[],                    /* I    Input signal [length]               */
63     const opus_int16            lambda_Q16,                 /* I    Warping factor                      */
64     const opus_int              length,                     /* I    Length of input signal              */
65     const opus_int              order                       /* I    Filter order (even)                 */
66 )
67 {
68     opus_int     n, i;
69     opus_int32   acc_Q11, tmp1, tmp2;
70 
71     /* Order must be even */
72     silk_assert( ( order & 1 ) == 0 );
73 
74     for( n = 0; n < length; n++ ) {
75         /* Output of lowpass section */
76         tmp2 = silk_SMLAWB( state[ 0 ], state[ 1 ], lambda_Q16 );
77         state[ 0 ] = silk_LSHIFT( input[ n ], 14 );
78         /* Output of allpass section */
79         tmp1 = silk_SMLAWB( state[ 1 ], state[ 2 ] - tmp2, lambda_Q16 );
80         state[ 1 ] = tmp2;
81         acc_Q11 = silk_RSHIFT( order, 1 );
82         acc_Q11 = silk_SMLAWB( acc_Q11, tmp2, coef_Q13[ 0 ] );
83         /* Loop over allpass sections */
84         for( i = 2; i < order; i += 2 ) {
85             /* Output of allpass section */
86             tmp2 = silk_SMLAWB( state[ i ], state[ i + 1 ] - tmp1, lambda_Q16 );
87             state[ i ] = tmp1;
88             acc_Q11 = silk_SMLAWB( acc_Q11, tmp1, coef_Q13[ i - 1 ] );
89             /* Output of allpass section */
90             tmp1 = silk_SMLAWB( state[ i + 1 ], state[ i + 2 ] - tmp2, lambda_Q16 );
91             state[ i + 1 ] = tmp2;
92             acc_Q11 = silk_SMLAWB( acc_Q11, tmp2, coef_Q13[ i ] );
93         }
94         state[ order ] = tmp1;
95         acc_Q11 = silk_SMLAWB( acc_Q11, tmp1, coef_Q13[ order - 1 ] );
96         res_Q2[ n ] = silk_LSHIFT( (opus_int32)input[ n ], 2 ) - silk_RSHIFT_ROUND( acc_Q11, 9 );
97     }
98 }
99 
silk_prefilter_FIX(silk_encoder_state_FIX * psEnc,const silk_encoder_control_FIX * psEncCtrl,opus_int32 xw_Q3[],const opus_int16 x[])100 void silk_prefilter_FIX(
101     silk_encoder_state_FIX          *psEnc,                                 /* I/O  Encoder state                                                               */
102     const silk_encoder_control_FIX  *psEncCtrl,                             /* I    Encoder control                                                             */
103     opus_int32                      xw_Q3[],                                /* O    Weighted signal                                                             */
104     const opus_int16                x[]                                     /* I    Speech signal                                                               */
105 )
106 {
107     silk_prefilter_state_FIX *P = &psEnc->sPrefilt;
108     opus_int   j, k, lag;
109     opus_int32 tmp_32;
110     const opus_int16 *AR1_shp_Q13;
111     const opus_int16 *px;
112     opus_int32 *pxw_Q3;
113     opus_int   HarmShapeGain_Q12, Tilt_Q14;
114     opus_int32 HarmShapeFIRPacked_Q12, LF_shp_Q14;
115     VARDECL( opus_int32, x_filt_Q12 );
116     VARDECL( opus_int32, st_res_Q2 );
117     opus_int16 B_Q10[ 2 ];
118     SAVE_STACK;
119 
120     /* Set up pointers */
121     px  = x;
122     pxw_Q3 = xw_Q3;
123     lag = P->lagPrev;
124     ALLOC( x_filt_Q12, psEnc->sCmn.subfr_length, opus_int32 );
125     ALLOC( st_res_Q2, psEnc->sCmn.subfr_length, opus_int32 );
126     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
127         /* Update Variables that change per sub frame */
128         if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
129             lag = psEncCtrl->pitchL[ k ];
130         }
131 
132         /* Noise shape parameters */
133         HarmShapeGain_Q12 = silk_SMULWB( (opus_int32)psEncCtrl->HarmShapeGain_Q14[ k ], 16384 - psEncCtrl->HarmBoost_Q14[ k ] );
134         silk_assert( HarmShapeGain_Q12 >= 0 );
135         HarmShapeFIRPacked_Q12  =                          silk_RSHIFT( HarmShapeGain_Q12, 2 );
136         HarmShapeFIRPacked_Q12 |= silk_LSHIFT( (opus_int32)silk_RSHIFT( HarmShapeGain_Q12, 1 ), 16 );
137         Tilt_Q14    = psEncCtrl->Tilt_Q14[   k ];
138         LF_shp_Q14  = psEncCtrl->LF_shp_Q14[ k ];
139         AR1_shp_Q13 = &psEncCtrl->AR1_Q13[   k * MAX_SHAPE_LPC_ORDER ];
140 
141         /* Short term FIR filtering*/
142         silk_warped_LPC_analysis_filter_FIX( P->sAR_shp, st_res_Q2, AR1_shp_Q13, px,
143             psEnc->sCmn.warping_Q16, psEnc->sCmn.subfr_length, psEnc->sCmn.shapingLPCOrder, psEnc->sCmn.arch );
144 
145         /* Reduce (mainly) low frequencies during harmonic emphasis */
146         B_Q10[ 0 ] = silk_RSHIFT_ROUND( psEncCtrl->GainsPre_Q14[ k ], 4 );
147         tmp_32 = silk_SMLABB( SILK_FIX_CONST( INPUT_TILT, 26 ), psEncCtrl->HarmBoost_Q14[ k ], HarmShapeGain_Q12 );   /* Q26 */
148         tmp_32 = silk_SMLABB( tmp_32, psEncCtrl->coding_quality_Q14, SILK_FIX_CONST( HIGH_RATE_INPUT_TILT, 12 ) );    /* Q26 */
149         tmp_32 = silk_SMULWB( tmp_32, -psEncCtrl->GainsPre_Q14[ k ] );                                                /* Q24 */
150         tmp_32 = silk_RSHIFT_ROUND( tmp_32, 14 );                                                                     /* Q10 */
151         B_Q10[ 1 ]= silk_SAT16( tmp_32 );
152         x_filt_Q12[ 0 ] = silk_MLA( silk_MUL( st_res_Q2[ 0 ], B_Q10[ 0 ] ), P->sHarmHP_Q2, B_Q10[ 1 ] );
153         for( j = 1; j < psEnc->sCmn.subfr_length; j++ ) {
154             x_filt_Q12[ j ] = silk_MLA( silk_MUL( st_res_Q2[ j ], B_Q10[ 0 ] ), st_res_Q2[ j - 1 ], B_Q10[ 1 ] );
155         }
156         P->sHarmHP_Q2 = st_res_Q2[ psEnc->sCmn.subfr_length - 1 ];
157 
158         silk_prefilt_FIX( P, x_filt_Q12, pxw_Q3, HarmShapeFIRPacked_Q12, Tilt_Q14, LF_shp_Q14, lag, psEnc->sCmn.subfr_length );
159 
160         px  += psEnc->sCmn.subfr_length;
161         pxw_Q3 += psEnc->sCmn.subfr_length;
162     }
163 
164     P->lagPrev = psEncCtrl->pitchL[ psEnc->sCmn.nb_subfr - 1 ];
165     RESTORE_STACK;
166 }
167 
168 #ifndef OVERRIDE_silk_prefilt_FIX
169 /* Prefilter for finding Quantizer input signal */
silk_prefilt_FIX(silk_prefilter_state_FIX * P,opus_int32 st_res_Q12[],opus_int32 xw_Q3[],opus_int32 HarmShapeFIRPacked_Q12,opus_int Tilt_Q14,opus_int32 LF_shp_Q14,opus_int lag,opus_int length)170 static OPUS_INLINE void silk_prefilt_FIX(
171     silk_prefilter_state_FIX    *P,                         /* I/O  state                               */
172     opus_int32                  st_res_Q12[],               /* I    short term residual signal          */
173     opus_int32                  xw_Q3[],                    /* O    prefiltered signal                  */
174     opus_int32                  HarmShapeFIRPacked_Q12,     /* I    Harmonic shaping coeficients        */
175     opus_int                    Tilt_Q14,                   /* I    Tilt shaping coeficient             */
176     opus_int32                  LF_shp_Q14,                 /* I    Low-frequancy shaping coeficients   */
177     opus_int                    lag,                        /* I    Lag for harmonic shaping            */
178     opus_int                    length                      /* I    Length of signals                   */
179 )
180 {
181     opus_int   i, idx, LTP_shp_buf_idx;
182     opus_int32 n_LTP_Q12, n_Tilt_Q10, n_LF_Q10;
183     opus_int32 sLF_MA_shp_Q12, sLF_AR_shp_Q12;
184     opus_int16 *LTP_shp_buf;
185 
186     /* To speed up use temp variables instead of using the struct */
187     LTP_shp_buf     = P->sLTP_shp;
188     LTP_shp_buf_idx = P->sLTP_shp_buf_idx;
189     sLF_AR_shp_Q12  = P->sLF_AR_shp_Q12;
190     sLF_MA_shp_Q12  = P->sLF_MA_shp_Q12;
191 
192     for( i = 0; i < length; i++ ) {
193         if( lag > 0 ) {
194             /* unrolled loop */
195             silk_assert( HARM_SHAPE_FIR_TAPS == 3 );
196             idx = lag + LTP_shp_buf_idx;
197             n_LTP_Q12 = silk_SMULBB(            LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 - 1) & LTP_MASK ], HarmShapeFIRPacked_Q12 );
198             n_LTP_Q12 = silk_SMLABT( n_LTP_Q12, LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2    ) & LTP_MASK ], HarmShapeFIRPacked_Q12 );
199             n_LTP_Q12 = silk_SMLABB( n_LTP_Q12, LTP_shp_buf[ ( idx - HARM_SHAPE_FIR_TAPS / 2 + 1) & LTP_MASK ], HarmShapeFIRPacked_Q12 );
200         } else {
201             n_LTP_Q12 = 0;
202         }
203 
204         n_Tilt_Q10 = silk_SMULWB( sLF_AR_shp_Q12, Tilt_Q14 );
205         n_LF_Q10   = silk_SMLAWB( silk_SMULWT( sLF_AR_shp_Q12, LF_shp_Q14 ), sLF_MA_shp_Q12, LF_shp_Q14 );
206 
207         sLF_AR_shp_Q12 = silk_SUB32( st_res_Q12[ i ], silk_LSHIFT( n_Tilt_Q10, 2 ) );
208         sLF_MA_shp_Q12 = silk_SUB32( sLF_AR_shp_Q12,  silk_LSHIFT( n_LF_Q10,   2 ) );
209 
210         LTP_shp_buf_idx = ( LTP_shp_buf_idx - 1 ) & LTP_MASK;
211         LTP_shp_buf[ LTP_shp_buf_idx ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( sLF_MA_shp_Q12, 12 ) );
212 
213         xw_Q3[i] = silk_RSHIFT_ROUND( silk_SUB32( sLF_MA_shp_Q12, n_LTP_Q12 ), 9 );
214     }
215 
216     /* Copy temp variable back to state */
217     P->sLF_AR_shp_Q12   = sLF_AR_shp_Q12;
218     P->sLF_MA_shp_Q12   = sLF_MA_shp_Q12;
219     P->sLTP_shp_buf_idx = LTP_shp_buf_idx;
220 }
221 #endif /* OVERRIDE_silk_prefilt_FIX */
222