c4789e66c5f2586ab0724b57e5637ac26106a0a8
[opus.git] / silk / fixed / silk_noise_shape_analysis_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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "silk_main_FIX.h"
33 #include "silk_tuning_parameters.h"
34
35 /* Compute gain to make warped filter coefficients have a zero mean log frequency response on a     */
36 /* non-warped frequency scale. (So that it can be implemented with a minimum-phase monic filter.)   */
37 static inline opus_int32 warped_gain( /* gain in Q16*/
38     const opus_int32     *coefs_Q24,
39     opus_int             lambda_Q16,
40     opus_int             order
41 ) {
42     opus_int   i;
43     opus_int32 gain_Q24;
44
45     lambda_Q16 = -lambda_Q16;
46     gain_Q24 = coefs_Q24[ order - 1 ];
47     for( i = order - 2; i >= 0; i-- ) {
48         gain_Q24 = silk_SMLAWB( coefs_Q24[ i ], gain_Q24, lambda_Q16 );
49     }
50     gain_Q24  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), gain_Q24, -lambda_Q16 );
51     return silk_INVERSE32_varQ( gain_Q24, 40 );
52 }
53
54 /* Convert warped filter coefficients to monic pseudo-warped coefficients and limit maximum     */
55 /* amplitude of monic warped coefficients by using bandwidth expansion on the true coefficients */
56 static inline void limit_warped_coefs(
57     opus_int32           *coefs_syn_Q24,
58     opus_int32           *coefs_ana_Q24,
59     opus_int             lambda_Q16,
60     opus_int32           limit_Q24,
61     opus_int             order
62 ) {
63     opus_int   i, iter, ind = 0;
64     opus_int32 tmp, maxabs_Q24, chirp_Q16, gain_syn_Q16, gain_ana_Q16;
65     opus_int32 nom_Q16, den_Q24;
66
67     /* Convert to monic coefficients */
68     lambda_Q16 = -lambda_Q16;
69     for( i = order - 1; i > 0; i-- ) {
70         coefs_syn_Q24[ i - 1 ] = silk_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );
71         coefs_ana_Q24[ i - 1 ] = silk_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );
72     }
73     lambda_Q16 = -lambda_Q16;
74     nom_Q16  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 16 ), -lambda_Q16,        lambda_Q16 );
75     den_Q24  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), coefs_syn_Q24[ 0 ], lambda_Q16 );
76     gain_syn_Q16 = silk_DIV32_varQ( nom_Q16, den_Q24, 24 );
77     den_Q24  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), coefs_ana_Q24[ 0 ], lambda_Q16 );
78     gain_ana_Q16 = silk_DIV32_varQ( nom_Q16, den_Q24, 24 );
79     for( i = 0; i < order; i++ ) {
80         coefs_syn_Q24[ i ] = silk_SMULWW( gain_syn_Q16, coefs_syn_Q24[ i ] );
81         coefs_ana_Q24[ i ] = silk_SMULWW( gain_ana_Q16, coefs_ana_Q24[ i ] );
82     }
83
84     for( iter = 0; iter < 10; iter++ ) {
85         /* Find maximum absolute value */
86         maxabs_Q24 = -1;
87         for( i = 0; i < order; i++ ) {
88             tmp = silk_max( silk_abs_int32( coefs_syn_Q24[ i ] ), silk_abs_int32( coefs_ana_Q24[ i ] ) );
89             if( tmp > maxabs_Q24 ) {
90                 maxabs_Q24 = tmp;
91                 ind = i;
92             }
93         }
94         if( maxabs_Q24 <= limit_Q24 ) {
95             /* Coefficients are within range - done */
96             return;
97         }
98
99         /* Convert back to true warped coefficients */
100         for( i = 1; i < order; i++ ) {
101             coefs_syn_Q24[ i - 1 ] = silk_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );
102             coefs_ana_Q24[ i - 1 ] = silk_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );
103         }
104         gain_syn_Q16 = silk_INVERSE32_varQ( gain_syn_Q16, 32 );
105         gain_ana_Q16 = silk_INVERSE32_varQ( gain_ana_Q16, 32 );
106         for( i = 0; i < order; i++ ) {
107             coefs_syn_Q24[ i ] = silk_SMULWW( gain_syn_Q16, coefs_syn_Q24[ i ] );
108             coefs_ana_Q24[ i ] = silk_SMULWW( gain_ana_Q16, coefs_ana_Q24[ i ] );
109         }
110
111         /* Apply bandwidth expansion */
112         chirp_Q16 = SILK_FIX_CONST( 0.99, 16 ) - silk_DIV32_varQ(
113             silk_SMULWB( maxabs_Q24 - limit_Q24, silk_SMLABB( SILK_FIX_CONST( 0.8, 10 ), SILK_FIX_CONST( 0.1, 10 ), iter ) ),
114             silk_MUL( maxabs_Q24, ind + 1 ), 22 );
115         silk_bwexpander_32( coefs_syn_Q24, order, chirp_Q16 );
116         silk_bwexpander_32( coefs_ana_Q24, order, chirp_Q16 );
117
118         /* Convert to monic warped coefficients */
119         lambda_Q16 = -lambda_Q16;
120         for( i = order - 1; i > 0; i-- ) {
121             coefs_syn_Q24[ i - 1 ] = silk_SMLAWB( coefs_syn_Q24[ i - 1 ], coefs_syn_Q24[ i ], lambda_Q16 );
122             coefs_ana_Q24[ i - 1 ] = silk_SMLAWB( coefs_ana_Q24[ i - 1 ], coefs_ana_Q24[ i ], lambda_Q16 );
123         }
124         lambda_Q16 = -lambda_Q16;
125         nom_Q16  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 16 ), -lambda_Q16,        lambda_Q16 );
126         den_Q24  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), coefs_syn_Q24[ 0 ], lambda_Q16 );
127         gain_syn_Q16 = silk_DIV32_varQ( nom_Q16, den_Q24, 24 );
128         den_Q24  = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), coefs_ana_Q24[ 0 ], lambda_Q16 );
129         gain_ana_Q16 = silk_DIV32_varQ( nom_Q16, den_Q24, 24 );
130         for( i = 0; i < order; i++ ) {
131             coefs_syn_Q24[ i ] = silk_SMULWW( gain_syn_Q16, coefs_syn_Q24[ i ] );
132             coefs_ana_Q24[ i ] = silk_SMULWW( gain_ana_Q16, coefs_ana_Q24[ i ] );
133         }
134     }
135     silk_assert( 0 );
136 }
137
138 /**************************************************************/
139 /* Compute noise shaping coefficients and initial gain values */
140 /**************************************************************/
141 void silk_noise_shape_analysis_FIX(
142     silk_encoder_state_FIX          *psEnc,         /* I/O  Encoder state FIX                           */
143     silk_encoder_control_FIX        *psEncCtrl,     /* I/O  Encoder control FIX                         */
144     const opus_int16                 *pitch_res,     /* I    LPC residual from pitch analysis            */
145     const opus_int16                 *x              /* I    Input signal [ frame_length + la_shape ]    */
146 )
147 {
148     silk_shape_state_FIX *psShapeSt = &psEnc->sShape;
149     opus_int     k, i, nSamples, Qnrg, b_Q14, warping_Q16, scale = 0;
150     opus_int32   SNR_adj_dB_Q7, HarmBoost_Q16, HarmShapeGain_Q16, Tilt_Q16, tmp32;
151     opus_int32   nrg, pre_nrg_Q30, log_energy_Q7, log_energy_prev_Q7, energy_variation_Q7;
152     opus_int32   delta_Q16, BWExp1_Q16, BWExp2_Q16, gain_mult_Q16, gain_add_Q16, strength_Q16, b_Q8;
153     opus_int32   auto_corr[     MAX_SHAPE_LPC_ORDER + 1 ];
154     opus_int32   refl_coef_Q16[ MAX_SHAPE_LPC_ORDER ];
155     opus_int32   AR1_Q24[       MAX_SHAPE_LPC_ORDER ];
156     opus_int32   AR2_Q24[       MAX_SHAPE_LPC_ORDER ];
157     opus_int16   x_windowed[    SHAPE_LPC_WIN_MAX ];
158     opus_int32   sqrt_nrg[ MAX_NB_SUBFR ], Qnrg_vec[ MAX_NB_SUBFR ];
159     const opus_int16 *x_ptr, *pitch_res_ptr;
160
161     /* Point to start of first LPC analysis block */
162     x_ptr = x - psEnc->sCmn.la_shape;
163
164     /****************/
165     /* GAIN CONTROL */
166     /****************/
167     SNR_adj_dB_Q7 = psEnc->sCmn.SNR_dB_Q7;
168
169     /* Input quality is the average of the quality in the lowest two VAD bands */
170     psEncCtrl->input_quality_Q14 = ( opus_int )silk_RSHIFT( ( opus_int32 )psEnc->sCmn.input_quality_bands_Q15[ 0 ]
171         + psEnc->sCmn.input_quality_bands_Q15[ 1 ], 2 );
172
173     /* Coding quality level, between 0.0_Q0 and 1.0_Q0, but in Q14 */
174     psEncCtrl->coding_quality_Q14 = silk_RSHIFT( silk_sigm_Q15( silk_RSHIFT_ROUND( SNR_adj_dB_Q7 -
175         SILK_FIX_CONST( 18.0, 7 ), 4 ) ), 1 );
176
177     /* Reduce coding SNR during low speech activity */
178     if( psEnc->sCmn.useCBR == 0 ) {
179         b_Q8 = SILK_FIX_CONST( 1.0, 8 ) - psEnc->sCmn.speech_activity_Q8;
180         b_Q8 = silk_SMULWB( silk_LSHIFT( b_Q8, 8 ), b_Q8 );
181         SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7,
182             silk_SMULBB( SILK_FIX_CONST( -BG_SNR_DECR_dB, 7 ) >> ( 4 + 1 ), b_Q8 ),                                       /* Q11*/
183             silk_SMULWB( SILK_FIX_CONST( 1.0, 14 ) + psEncCtrl->input_quality_Q14, psEncCtrl->coding_quality_Q14 ) );     /* Q12*/
184     }
185
186     if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
187         /* Reduce gains for periodic signals */
188         SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7, SILK_FIX_CONST( HARM_SNR_INCR_dB, 8 ), psEnc->LTPCorr_Q15 );
189     } else {
190         /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */
191         SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7,
192             silk_SMLAWB( SILK_FIX_CONST( 6.0, 9 ), -SILK_FIX_CONST( 0.4, 18 ), psEnc->sCmn.SNR_dB_Q7 ),
193             SILK_FIX_CONST( 1.0, 14 ) - psEncCtrl->input_quality_Q14 );
194     }
195
196     /*************************/
197     /* SPARSENESS PROCESSING */
198     /*************************/
199     /* Set quantizer offset */
200     if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
201         /* Initally set to 0; may be overruled in process_gains(..) */
202         psEnc->sCmn.indices.quantOffsetType = 0;
203         psEncCtrl->sparseness_Q8 = 0;
204     } else {
205         /* Sparseness measure, based on relative fluctuations of energy per 2 milliseconds */
206         nSamples = silk_LSHIFT( psEnc->sCmn.fs_kHz, 1 );
207         energy_variation_Q7 = 0;
208         log_energy_prev_Q7  = 0;
209         pitch_res_ptr = pitch_res;
210         for( k = 0; k < silk_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) / 2; k++ ) {
211             silk_sum_sqr_shift( &nrg, &scale, pitch_res_ptr, nSamples );
212             nrg += silk_RSHIFT( nSamples, scale );           /* Q(-scale)*/
213
214             log_energy_Q7 = silk_lin2log( nrg );
215             if( k > 0 ) {
216                 energy_variation_Q7 += silk_abs( log_energy_Q7 - log_energy_prev_Q7 );
217             }
218             log_energy_prev_Q7 = log_energy_Q7;
219             pitch_res_ptr += nSamples;
220         }
221
222         psEncCtrl->sparseness_Q8 = silk_RSHIFT( silk_sigm_Q15( silk_SMULWB( energy_variation_Q7 -
223             SILK_FIX_CONST( 5.0, 7 ), SILK_FIX_CONST( 0.1, 16 ) ) ), 7 );
224
225         /* Set quantization offset depending on sparseness measure */
226         if( psEncCtrl->sparseness_Q8 > SILK_FIX_CONST( SPARSENESS_THRESHOLD_QNT_OFFSET, 8 ) ) {
227             psEnc->sCmn.indices.quantOffsetType = 0;
228         } else {
229             psEnc->sCmn.indices.quantOffsetType = 1;
230         }
231
232         /* Increase coding SNR for sparse signals */
233         SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7, SILK_FIX_CONST( SPARSE_SNR_INCR_dB, 15 ), psEncCtrl->sparseness_Q8 - SILK_FIX_CONST( 0.5, 8 ) );
234     }
235
236     /*******************************/
237     /* Control bandwidth expansion */
238     /*******************************/
239     /* More BWE for signals with high prediction gain */
240     strength_Q16 = silk_SMULWB( psEncCtrl->predGain_Q16, SILK_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) );
241     BWExp1_Q16 = BWExp2_Q16 = silk_DIV32_varQ( SILK_FIX_CONST( BANDWIDTH_EXPANSION, 16 ),
242         silk_SMLAWW( SILK_FIX_CONST( 1.0, 16 ), strength_Q16, strength_Q16 ), 16 );
243     delta_Q16  = silk_SMULWB( SILK_FIX_CONST( 1.0, 16 ) - silk_SMULBB( 3, psEncCtrl->coding_quality_Q14 ),
244         SILK_FIX_CONST( LOW_RATE_BANDWIDTH_EXPANSION_DELTA, 16 ) );
245     BWExp1_Q16 = silk_SUB32( BWExp1_Q16, delta_Q16 );
246     BWExp2_Q16 = silk_ADD32( BWExp2_Q16, delta_Q16 );
247     /* BWExp1 will be applied after BWExp2, so make it relative */
248     BWExp1_Q16 = silk_DIV32_16( silk_LSHIFT( BWExp1_Q16, 14 ), silk_RSHIFT( BWExp2_Q16, 2 ) );
249
250     if( psEnc->sCmn.warping_Q16 > 0 ) {
251         /* Slightly more warping in analysis will move quantization noise up in frequency, where it's better masked */
252         warping_Q16 = silk_SMLAWB( psEnc->sCmn.warping_Q16, psEncCtrl->coding_quality_Q14, SILK_FIX_CONST( 0.01, 18 ) );
253     } else {
254         warping_Q16 = 0;
255     }
256
257     /********************************************/
258     /* Compute noise shaping AR coefs and gains */
259     /********************************************/
260     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
261         /* Apply window: sine slope followed by flat part followed by cosine slope */
262         opus_int shift, slope_part, flat_part;
263         flat_part = psEnc->sCmn.fs_kHz * 3;
264         slope_part = silk_RSHIFT( psEnc->sCmn.shapeWinLength - flat_part, 1 );
265
266         silk_apply_sine_window( x_windowed, x_ptr, 1, slope_part );
267         shift = slope_part;
268         silk_memcpy( x_windowed + shift, x_ptr + shift, flat_part * sizeof(opus_int16) );
269         shift += flat_part;
270         silk_apply_sine_window( x_windowed + shift, x_ptr + shift, 2, slope_part );
271
272         /* Update pointer: next LPC analysis block */
273         x_ptr += psEnc->sCmn.subfr_length;
274
275         if( psEnc->sCmn.warping_Q16 > 0 ) {
276             /* Calculate warped auto correlation */
277             silk_warped_autocorrelation_FIX( auto_corr, &scale, x_windowed, warping_Q16, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder );
278         } else {
279             /* Calculate regular auto correlation */
280             silk_autocorr( auto_corr, &scale, x_windowed, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder + 1 );
281         }
282
283         /* Add white noise, as a fraction of energy */
284         auto_corr[0] = silk_ADD32( auto_corr[0], silk_max_32( silk_SMULWB( silk_RSHIFT( auto_corr[ 0 ], 4 ),
285             SILK_FIX_CONST( SHAPE_WHITE_NOISE_FRACTION, 20 ) ), 1 ) );
286
287         /* Calculate the reflection coefficients using schur */
288         nrg = silk_schur64( refl_coef_Q16, auto_corr, psEnc->sCmn.shapingLPCOrder );
289         silk_assert( nrg >= 0 );
290
291         /* Convert reflection coefficients to prediction coefficients */
292         silk_k2a_Q16( AR2_Q24, refl_coef_Q16, psEnc->sCmn.shapingLPCOrder );
293
294         Qnrg = -scale;          /* range: -12...30*/
295         silk_assert( Qnrg >= -12 );
296         silk_assert( Qnrg <=  30 );
297
298         /* Make sure that Qnrg is an even number */
299         if( Qnrg & 1 ) {
300             Qnrg -= 1;
301             nrg >>= 1;
302         }
303
304         tmp32 = silk_SQRT_APPROX( nrg );
305         Qnrg >>= 1;             /* range: -6...15*/
306
307         sqrt_nrg[ k ] = tmp32;
308         Qnrg_vec[ k ] = Qnrg;
309
310         psEncCtrl->Gains_Q16[ k ] = silk_LSHIFT_SAT32( tmp32, 16 - Qnrg );
311
312         if( psEnc->sCmn.warping_Q16 > 0 ) {
313             /* Adjust gain for warping */
314             gain_mult_Q16 = warped_gain( AR2_Q24, warping_Q16, psEnc->sCmn.shapingLPCOrder );
315             silk_assert( psEncCtrl->Gains_Q16[ k ] >= 0 );
316             psEncCtrl->Gains_Q16[ k ] = silk_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 );
317             if( psEncCtrl->Gains_Q16[ k ] < 0 ) {
318                 psEncCtrl->Gains_Q16[ k ] = silk_int32_MAX;
319             }
320         }
321
322         /* Bandwidth expansion for synthesis filter shaping */
323         silk_bwexpander_32( AR2_Q24, psEnc->sCmn.shapingLPCOrder, BWExp2_Q16 );
324
325         /* Compute noise shaping filter coefficients */
326         silk_memcpy( AR1_Q24, AR2_Q24, psEnc->sCmn.shapingLPCOrder * sizeof( opus_int32 ) );
327
328         /* Bandwidth expansion for analysis filter shaping */
329         silk_assert( BWExp1_Q16 <= SILK_FIX_CONST( 1.0, 16 ) );
330         silk_bwexpander_32( AR1_Q24, psEnc->sCmn.shapingLPCOrder, BWExp1_Q16 );
331
332         /* Ratio of prediction gains, in energy domain */
333         silk_LPC_inverse_pred_gain_Q24( &pre_nrg_Q30, AR2_Q24, psEnc->sCmn.shapingLPCOrder );
334         silk_LPC_inverse_pred_gain_Q24( &nrg,         AR1_Q24, psEnc->sCmn.shapingLPCOrder );
335
336         /*psEncCtrl->GainsPre[ k ] = 1.0f - 0.7f * ( 1.0f - pre_nrg / nrg ) = 0.3f + 0.7f * pre_nrg / nrg;*/
337         pre_nrg_Q30 = silk_LSHIFT32( silk_SMULWB( pre_nrg_Q30, SILK_FIX_CONST( 0.7, 15 ) ), 1 );
338         psEncCtrl->GainsPre_Q14[ k ] = ( opus_int ) SILK_FIX_CONST( 0.3, 14 ) + silk_DIV32_varQ( pre_nrg_Q30, nrg, 14 );
339
340         /* Convert to monic warped prediction coefficients and limit absolute values */
341         limit_warped_coefs( AR2_Q24, AR1_Q24, warping_Q16, SILK_FIX_CONST( 3.999, 24 ), psEnc->sCmn.shapingLPCOrder );
342
343         /* Convert from Q24 to Q13 and store in int16 */
344         for( i = 0; i < psEnc->sCmn.shapingLPCOrder; i++ ) {
345             psEncCtrl->AR1_Q13[ k * MAX_SHAPE_LPC_ORDER + i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( AR1_Q24[ i ], 11 ) );
346             psEncCtrl->AR2_Q13[ k * MAX_SHAPE_LPC_ORDER + i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( AR2_Q24[ i ], 11 ) );
347         }
348     }
349
350     /*****************/
351     /* Gain tweaking */
352     /*****************/
353     /* Increase gains during low speech activity and put lower limit on gains */
354     gain_mult_Q16 = silk_log2lin( -silk_SMLAWB( -SILK_FIX_CONST( 16.0, 7 ), SNR_adj_dB_Q7, SILK_FIX_CONST( 0.16, 16 ) ) );
355     gain_add_Q16  = silk_log2lin(  silk_SMLAWB(  SILK_FIX_CONST( 16.0, 7 ), SILK_FIX_CONST( MIN_QGAIN_DB, 7 ), SILK_FIX_CONST( 0.16, 16 ) ) );
356     silk_assert( gain_mult_Q16 > 0 );
357     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
358         psEncCtrl->Gains_Q16[ k ] = silk_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 );
359         silk_assert( psEncCtrl->Gains_Q16[ k ] >= 0 );
360         psEncCtrl->Gains_Q16[ k ] = silk_ADD_POS_SAT32( psEncCtrl->Gains_Q16[ k ], gain_add_Q16 );
361     }
362
363     gain_mult_Q16 = SILK_FIX_CONST( 1.0, 16 ) + silk_RSHIFT_ROUND( silk_MLA( SILK_FIX_CONST( INPUT_TILT, 26 ),
364         psEncCtrl->coding_quality_Q14, SILK_FIX_CONST( HIGH_RATE_INPUT_TILT, 12 ) ), 10 );
365     for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
366         psEncCtrl->GainsPre_Q14[ k ] = silk_SMULWB( gain_mult_Q16, psEncCtrl->GainsPre_Q14[ k ] );
367     }
368
369     /************************************************/
370     /* Control low-frequency shaping and noise tilt */
371     /************************************************/
372     /* Less low frequency shaping for noisy inputs */
373     strength_Q16 = silk_MUL( SILK_FIX_CONST( LOW_FREQ_SHAPING, 4 ), silk_SMLAWB( SILK_FIX_CONST( 1.0, 12 ),
374         SILK_FIX_CONST( LOW_QUALITY_LOW_FREQ_SHAPING_DECR, 13 ), psEnc->sCmn.input_quality_bands_Q15[ 0 ] - SILK_FIX_CONST( 1.0, 15 ) ) );
375     strength_Q16 = silk_RSHIFT( silk_MUL( strength_Q16, psEnc->sCmn.speech_activity_Q8 ), 8 );
376     if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
377         /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */
378         /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/
379         opus_int fs_kHz_inv = silk_DIV32_16( SILK_FIX_CONST( 0.2, 14 ), psEnc->sCmn.fs_kHz );
380         for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
381             b_Q14 = fs_kHz_inv + silk_DIV32_16( SILK_FIX_CONST( 3.0, 14 ), psEncCtrl->pitchL[ k ] );
382             /* Pack two coefficients in one int32 */
383             psEncCtrl->LF_shp_Q14[ k ]  = silk_LSHIFT( SILK_FIX_CONST( 1.0, 14 ) - b_Q14 - silk_SMULWB( strength_Q16, b_Q14 ), 16 );
384             psEncCtrl->LF_shp_Q14[ k ] |= (opus_uint16)( b_Q14 - SILK_FIX_CONST( 1.0, 14 ) );
385         }
386         silk_assert( SILK_FIX_CONST( HARM_HP_NOISE_COEF, 24 ) < SILK_FIX_CONST( 0.5, 24 ) ); /* Guarantees that second argument to SMULWB() is within range of an opus_int16*/
387         Tilt_Q16 = - SILK_FIX_CONST( HP_NOISE_COEF, 16 ) -
388             silk_SMULWB( SILK_FIX_CONST( 1.0, 16 ) - SILK_FIX_CONST( HP_NOISE_COEF, 16 ),
389                 silk_SMULWB( SILK_FIX_CONST( HARM_HP_NOISE_COEF, 24 ), psEnc->sCmn.speech_activity_Q8 ) );
390     } else {
391         b_Q14 = silk_DIV32_16( 21299, psEnc->sCmn.fs_kHz ); /* 1.3_Q0 = 21299_Q14*/
392         /* Pack two coefficients in one int32 */
393         psEncCtrl->LF_shp_Q14[ 0 ]  = silk_LSHIFT( SILK_FIX_CONST( 1.0, 14 ) - b_Q14 -
394             silk_SMULWB( strength_Q16, silk_SMULWB( SILK_FIX_CONST( 0.6, 16 ), b_Q14 ) ), 16 );
395         psEncCtrl->LF_shp_Q14[ 0 ] |= (opus_uint16)( b_Q14 - SILK_FIX_CONST( 1.0, 14 ) );
396         for( k = 1; k < psEnc->sCmn.nb_subfr; k++ ) {
397             psEncCtrl->LF_shp_Q14[ k ] = psEncCtrl->LF_shp_Q14[ 0 ];
398         }
399         Tilt_Q16 = -SILK_FIX_CONST( HP_NOISE_COEF, 16 );
400     }
401
402     /****************************/
403     /* HARMONIC SHAPING CONTROL */
404     /****************************/
405     /* Control boosting of harmonic frequencies */
406     HarmBoost_Q16 = silk_SMULWB( silk_SMULWB( SILK_FIX_CONST( 1.0, 17 ) - silk_LSHIFT( psEncCtrl->coding_quality_Q14, 3 ),
407         psEnc->LTPCorr_Q15 ), SILK_FIX_CONST( LOW_RATE_HARMONIC_BOOST, 16 ) );
408
409     /* More harmonic boost for noisy input signals */
410     HarmBoost_Q16 = silk_SMLAWB( HarmBoost_Q16,
411         SILK_FIX_CONST( 1.0, 16 ) - silk_LSHIFT( psEncCtrl->input_quality_Q14, 2 ), SILK_FIX_CONST( LOW_INPUT_QUALITY_HARMONIC_BOOST, 16 ) );
412
413     if( USE_HARM_SHAPING && psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
414         /* More harmonic noise shaping for high bitrates or noisy input */
415         HarmShapeGain_Q16 = silk_SMLAWB( SILK_FIX_CONST( HARMONIC_SHAPING, 16 ),
416                 SILK_FIX_CONST( 1.0, 16 ) - silk_SMULWB( SILK_FIX_CONST( 1.0, 18 ) - silk_LSHIFT( psEncCtrl->coding_quality_Q14, 4 ),
417                 psEncCtrl->input_quality_Q14 ), SILK_FIX_CONST( HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING, 16 ) );
418
419         /* Less harmonic noise shaping for less periodic signals */
420         HarmShapeGain_Q16 = silk_SMULWB( silk_LSHIFT( HarmShapeGain_Q16, 1 ),
421             silk_SQRT_APPROX( silk_LSHIFT( psEnc->LTPCorr_Q15, 15 ) ) );
422     } else {
423         HarmShapeGain_Q16 = 0;
424     }
425
426     /*************************/
427     /* Smooth over subframes */
428     /*************************/
429     for( k = 0; k < MAX_NB_SUBFR; k++ ) {
430         psShapeSt->HarmBoost_smth_Q16 =
431             silk_SMLAWB( psShapeSt->HarmBoost_smth_Q16,     HarmBoost_Q16     - psShapeSt->HarmBoost_smth_Q16,     SILK_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );
432         psShapeSt->HarmShapeGain_smth_Q16 =
433             silk_SMLAWB( psShapeSt->HarmShapeGain_smth_Q16, HarmShapeGain_Q16 - psShapeSt->HarmShapeGain_smth_Q16, SILK_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );
434         psShapeSt->Tilt_smth_Q16 =
435             silk_SMLAWB( psShapeSt->Tilt_smth_Q16,          Tilt_Q16          - psShapeSt->Tilt_smth_Q16,          SILK_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );
436
437         psEncCtrl->HarmBoost_Q14[ k ]     = ( opus_int )silk_RSHIFT_ROUND( psShapeSt->HarmBoost_smth_Q16,     2 );
438         psEncCtrl->HarmShapeGain_Q14[ k ] = ( opus_int )silk_RSHIFT_ROUND( psShapeSt->HarmShapeGain_smth_Q16, 2 );
439         psEncCtrl->Tilt_Q14[ k ]          = ( opus_int )silk_RSHIFT_ROUND( psShapeSt->Tilt_smth_Q16,          2 );
440     }
441 }