annotate src/opus-1.3/silk/fixed/noise_shape_analysis_FIX.c @ 79:91c729825bca pa_catalina

Update build for AUDIO_COMPONENT_FIX
author Chris Cannam
date Wed, 30 Oct 2019 12:40:34 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 #include "main_FIX.h"
Chris@69 33 #include "stack_alloc.h"
Chris@69 34 #include "tuning_parameters.h"
Chris@69 35
Chris@69 36 /* Compute gain to make warped filter coefficients have a zero mean log frequency response on a */
Chris@69 37 /* non-warped frequency scale. (So that it can be implemented with a minimum-phase monic filter.) */
Chris@69 38 /* Note: A monic filter is one with the first coefficient equal to 1.0. In Silk we omit the first */
Chris@69 39 /* coefficient in an array of coefficients, for monic filters. */
Chris@69 40 static OPUS_INLINE opus_int32 warped_gain( /* gain in Q16*/
Chris@69 41 const opus_int32 *coefs_Q24,
Chris@69 42 opus_int lambda_Q16,
Chris@69 43 opus_int order
Chris@69 44 ) {
Chris@69 45 opus_int i;
Chris@69 46 opus_int32 gain_Q24;
Chris@69 47
Chris@69 48 lambda_Q16 = -lambda_Q16;
Chris@69 49 gain_Q24 = coefs_Q24[ order - 1 ];
Chris@69 50 for( i = order - 2; i >= 0; i-- ) {
Chris@69 51 gain_Q24 = silk_SMLAWB( coefs_Q24[ i ], gain_Q24, lambda_Q16 );
Chris@69 52 }
Chris@69 53 gain_Q24 = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), gain_Q24, -lambda_Q16 );
Chris@69 54 return silk_INVERSE32_varQ( gain_Q24, 40 );
Chris@69 55 }
Chris@69 56
Chris@69 57 /* Convert warped filter coefficients to monic pseudo-warped coefficients and limit maximum */
Chris@69 58 /* amplitude of monic warped coefficients by using bandwidth expansion on the true coefficients */
Chris@69 59 static OPUS_INLINE void limit_warped_coefs(
Chris@69 60 opus_int32 *coefs_Q24,
Chris@69 61 opus_int lambda_Q16,
Chris@69 62 opus_int32 limit_Q24,
Chris@69 63 opus_int order
Chris@69 64 ) {
Chris@69 65 opus_int i, iter, ind = 0;
Chris@69 66 opus_int32 tmp, maxabs_Q24, chirp_Q16, gain_Q16;
Chris@69 67 opus_int32 nom_Q16, den_Q24;
Chris@69 68 opus_int32 limit_Q20, maxabs_Q20;
Chris@69 69
Chris@69 70 /* Convert to monic coefficients */
Chris@69 71 lambda_Q16 = -lambda_Q16;
Chris@69 72 for( i = order - 1; i > 0; i-- ) {
Chris@69 73 coefs_Q24[ i - 1 ] = silk_SMLAWB( coefs_Q24[ i - 1 ], coefs_Q24[ i ], lambda_Q16 );
Chris@69 74 }
Chris@69 75 lambda_Q16 = -lambda_Q16;
Chris@69 76 nom_Q16 = silk_SMLAWB( SILK_FIX_CONST( 1.0, 16 ), -(opus_int32)lambda_Q16, lambda_Q16 );
Chris@69 77 den_Q24 = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), coefs_Q24[ 0 ], lambda_Q16 );
Chris@69 78 gain_Q16 = silk_DIV32_varQ( nom_Q16, den_Q24, 24 );
Chris@69 79 for( i = 0; i < order; i++ ) {
Chris@69 80 coefs_Q24[ i ] = silk_SMULWW( gain_Q16, coefs_Q24[ i ] );
Chris@69 81 }
Chris@69 82 limit_Q20 = silk_RSHIFT(limit_Q24, 4);
Chris@69 83 for( iter = 0; iter < 10; iter++ ) {
Chris@69 84 /* Find maximum absolute value */
Chris@69 85 maxabs_Q24 = -1;
Chris@69 86 for( i = 0; i < order; i++ ) {
Chris@69 87 tmp = silk_abs_int32( coefs_Q24[ i ] );
Chris@69 88 if( tmp > maxabs_Q24 ) {
Chris@69 89 maxabs_Q24 = tmp;
Chris@69 90 ind = i;
Chris@69 91 }
Chris@69 92 }
Chris@69 93 /* Use Q20 to avoid any overflow when multiplying by (ind + 1) later. */
Chris@69 94 maxabs_Q20 = silk_RSHIFT(maxabs_Q24, 4);
Chris@69 95 if( maxabs_Q20 <= limit_Q20 ) {
Chris@69 96 /* Coefficients are within range - done */
Chris@69 97 return;
Chris@69 98 }
Chris@69 99
Chris@69 100 /* Convert back to true warped coefficients */
Chris@69 101 for( i = 1; i < order; i++ ) {
Chris@69 102 coefs_Q24[ i - 1 ] = silk_SMLAWB( coefs_Q24[ i - 1 ], coefs_Q24[ i ], lambda_Q16 );
Chris@69 103 }
Chris@69 104 gain_Q16 = silk_INVERSE32_varQ( gain_Q16, 32 );
Chris@69 105 for( i = 0; i < order; i++ ) {
Chris@69 106 coefs_Q24[ i ] = silk_SMULWW( gain_Q16, coefs_Q24[ i ] );
Chris@69 107 }
Chris@69 108
Chris@69 109 /* Apply bandwidth expansion */
Chris@69 110 chirp_Q16 = SILK_FIX_CONST( 0.99, 16 ) - silk_DIV32_varQ(
Chris@69 111 silk_SMULWB( maxabs_Q20 - limit_Q20, silk_SMLABB( SILK_FIX_CONST( 0.8, 10 ), SILK_FIX_CONST( 0.1, 10 ), iter ) ),
Chris@69 112 silk_MUL( maxabs_Q20, ind + 1 ), 22 );
Chris@69 113 silk_bwexpander_32( coefs_Q24, order, chirp_Q16 );
Chris@69 114
Chris@69 115 /* Convert to monic warped coefficients */
Chris@69 116 lambda_Q16 = -lambda_Q16;
Chris@69 117 for( i = order - 1; i > 0; i-- ) {
Chris@69 118 coefs_Q24[ i - 1 ] = silk_SMLAWB( coefs_Q24[ i - 1 ], coefs_Q24[ i ], lambda_Q16 );
Chris@69 119 }
Chris@69 120 lambda_Q16 = -lambda_Q16;
Chris@69 121 nom_Q16 = silk_SMLAWB( SILK_FIX_CONST( 1.0, 16 ), -(opus_int32)lambda_Q16, lambda_Q16 );
Chris@69 122 den_Q24 = silk_SMLAWB( SILK_FIX_CONST( 1.0, 24 ), coefs_Q24[ 0 ], lambda_Q16 );
Chris@69 123 gain_Q16 = silk_DIV32_varQ( nom_Q16, den_Q24, 24 );
Chris@69 124 for( i = 0; i < order; i++ ) {
Chris@69 125 coefs_Q24[ i ] = silk_SMULWW( gain_Q16, coefs_Q24[ i ] );
Chris@69 126 }
Chris@69 127 }
Chris@69 128 silk_assert( 0 );
Chris@69 129 }
Chris@69 130
Chris@69 131 /* Disable MIPS version until it's updated. */
Chris@69 132 #if 0 && defined(MIPSr1_ASM)
Chris@69 133 #include "mips/noise_shape_analysis_FIX_mipsr1.h"
Chris@69 134 #endif
Chris@69 135
Chris@69 136 /**************************************************************/
Chris@69 137 /* Compute noise shaping coefficients and initial gain values */
Chris@69 138 /**************************************************************/
Chris@69 139 #ifndef OVERRIDE_silk_noise_shape_analysis_FIX
Chris@69 140 void silk_noise_shape_analysis_FIX(
Chris@69 141 silk_encoder_state_FIX *psEnc, /* I/O Encoder state FIX */
Chris@69 142 silk_encoder_control_FIX *psEncCtrl, /* I/O Encoder control FIX */
Chris@69 143 const opus_int16 *pitch_res, /* I LPC residual from pitch analysis */
Chris@69 144 const opus_int16 *x, /* I Input signal [ frame_length + la_shape ] */
Chris@69 145 int arch /* I Run-time architecture */
Chris@69 146 )
Chris@69 147 {
Chris@69 148 silk_shape_state_FIX *psShapeSt = &psEnc->sShape;
Chris@69 149 opus_int k, i, nSamples, nSegs, Qnrg, b_Q14, warping_Q16, scale = 0;
Chris@69 150 opus_int32 SNR_adj_dB_Q7, HarmShapeGain_Q16, Tilt_Q16, tmp32;
Chris@69 151 opus_int32 nrg, log_energy_Q7, log_energy_prev_Q7, energy_variation_Q7;
Chris@69 152 opus_int32 BWExp_Q16, gain_mult_Q16, gain_add_Q16, strength_Q16, b_Q8;
Chris@69 153 opus_int32 auto_corr[ MAX_SHAPE_LPC_ORDER + 1 ];
Chris@69 154 opus_int32 refl_coef_Q16[ MAX_SHAPE_LPC_ORDER ];
Chris@69 155 opus_int32 AR_Q24[ MAX_SHAPE_LPC_ORDER ];
Chris@69 156 VARDECL( opus_int16, x_windowed );
Chris@69 157 const opus_int16 *x_ptr, *pitch_res_ptr;
Chris@69 158 SAVE_STACK;
Chris@69 159
Chris@69 160 /* Point to start of first LPC analysis block */
Chris@69 161 x_ptr = x - psEnc->sCmn.la_shape;
Chris@69 162
Chris@69 163 /****************/
Chris@69 164 /* GAIN CONTROL */
Chris@69 165 /****************/
Chris@69 166 SNR_adj_dB_Q7 = psEnc->sCmn.SNR_dB_Q7;
Chris@69 167
Chris@69 168 /* Input quality is the average of the quality in the lowest two VAD bands */
Chris@69 169 psEncCtrl->input_quality_Q14 = ( opus_int )silk_RSHIFT( (opus_int32)psEnc->sCmn.input_quality_bands_Q15[ 0 ]
Chris@69 170 + psEnc->sCmn.input_quality_bands_Q15[ 1 ], 2 );
Chris@69 171
Chris@69 172 /* Coding quality level, between 0.0_Q0 and 1.0_Q0, but in Q14 */
Chris@69 173 psEncCtrl->coding_quality_Q14 = silk_RSHIFT( silk_sigm_Q15( silk_RSHIFT_ROUND( SNR_adj_dB_Q7 -
Chris@69 174 SILK_FIX_CONST( 20.0, 7 ), 4 ) ), 1 );
Chris@69 175
Chris@69 176 /* Reduce coding SNR during low speech activity */
Chris@69 177 if( psEnc->sCmn.useCBR == 0 ) {
Chris@69 178 b_Q8 = SILK_FIX_CONST( 1.0, 8 ) - psEnc->sCmn.speech_activity_Q8;
Chris@69 179 b_Q8 = silk_SMULWB( silk_LSHIFT( b_Q8, 8 ), b_Q8 );
Chris@69 180 SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7,
Chris@69 181 silk_SMULBB( SILK_FIX_CONST( -BG_SNR_DECR_dB, 7 ) >> ( 4 + 1 ), b_Q8 ), /* Q11*/
Chris@69 182 silk_SMULWB( SILK_FIX_CONST( 1.0, 14 ) + psEncCtrl->input_quality_Q14, psEncCtrl->coding_quality_Q14 ) ); /* Q12*/
Chris@69 183 }
Chris@69 184
Chris@69 185 if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
Chris@69 186 /* Reduce gains for periodic signals */
Chris@69 187 SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7, SILK_FIX_CONST( HARM_SNR_INCR_dB, 8 ), psEnc->LTPCorr_Q15 );
Chris@69 188 } else {
Chris@69 189 /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */
Chris@69 190 SNR_adj_dB_Q7 = silk_SMLAWB( SNR_adj_dB_Q7,
Chris@69 191 silk_SMLAWB( SILK_FIX_CONST( 6.0, 9 ), -SILK_FIX_CONST( 0.4, 18 ), psEnc->sCmn.SNR_dB_Q7 ),
Chris@69 192 SILK_FIX_CONST( 1.0, 14 ) - psEncCtrl->input_quality_Q14 );
Chris@69 193 }
Chris@69 194
Chris@69 195 /*************************/
Chris@69 196 /* SPARSENESS PROCESSING */
Chris@69 197 /*************************/
Chris@69 198 /* Set quantizer offset */
Chris@69 199 if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
Chris@69 200 /* Initially set to 0; may be overruled in process_gains(..) */
Chris@69 201 psEnc->sCmn.indices.quantOffsetType = 0;
Chris@69 202 } else {
Chris@69 203 /* Sparseness measure, based on relative fluctuations of energy per 2 milliseconds */
Chris@69 204 nSamples = silk_LSHIFT( psEnc->sCmn.fs_kHz, 1 );
Chris@69 205 energy_variation_Q7 = 0;
Chris@69 206 log_energy_prev_Q7 = 0;
Chris@69 207 pitch_res_ptr = pitch_res;
Chris@69 208 nSegs = silk_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) / 2;
Chris@69 209 for( k = 0; k < nSegs; k++ ) {
Chris@69 210 silk_sum_sqr_shift( &nrg, &scale, pitch_res_ptr, nSamples );
Chris@69 211 nrg += silk_RSHIFT( nSamples, scale ); /* Q(-scale)*/
Chris@69 212
Chris@69 213 log_energy_Q7 = silk_lin2log( nrg );
Chris@69 214 if( k > 0 ) {
Chris@69 215 energy_variation_Q7 += silk_abs( log_energy_Q7 - log_energy_prev_Q7 );
Chris@69 216 }
Chris@69 217 log_energy_prev_Q7 = log_energy_Q7;
Chris@69 218 pitch_res_ptr += nSamples;
Chris@69 219 }
Chris@69 220
Chris@69 221 /* Set quantization offset depending on sparseness measure */
Chris@69 222 if( energy_variation_Q7 > SILK_FIX_CONST( ENERGY_VARIATION_THRESHOLD_QNT_OFFSET, 7 ) * (nSegs-1) ) {
Chris@69 223 psEnc->sCmn.indices.quantOffsetType = 0;
Chris@69 224 } else {
Chris@69 225 psEnc->sCmn.indices.quantOffsetType = 1;
Chris@69 226 }
Chris@69 227 }
Chris@69 228
Chris@69 229 /*******************************/
Chris@69 230 /* Control bandwidth expansion */
Chris@69 231 /*******************************/
Chris@69 232 /* More BWE for signals with high prediction gain */
Chris@69 233 strength_Q16 = silk_SMULWB( psEncCtrl->predGain_Q16, SILK_FIX_CONST( FIND_PITCH_WHITE_NOISE_FRACTION, 16 ) );
Chris@69 234 BWExp_Q16 = silk_DIV32_varQ( SILK_FIX_CONST( BANDWIDTH_EXPANSION, 16 ),
Chris@69 235 silk_SMLAWW( SILK_FIX_CONST( 1.0, 16 ), strength_Q16, strength_Q16 ), 16 );
Chris@69 236
Chris@69 237 if( psEnc->sCmn.warping_Q16 > 0 ) {
Chris@69 238 /* Slightly more warping in analysis will move quantization noise up in frequency, where it's better masked */
Chris@69 239 warping_Q16 = silk_SMLAWB( psEnc->sCmn.warping_Q16, (opus_int32)psEncCtrl->coding_quality_Q14, SILK_FIX_CONST( 0.01, 18 ) );
Chris@69 240 } else {
Chris@69 241 warping_Q16 = 0;
Chris@69 242 }
Chris@69 243
Chris@69 244 /********************************************/
Chris@69 245 /* Compute noise shaping AR coefs and gains */
Chris@69 246 /********************************************/
Chris@69 247 ALLOC( x_windowed, psEnc->sCmn.shapeWinLength, opus_int16 );
Chris@69 248 for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
Chris@69 249 /* Apply window: sine slope followed by flat part followed by cosine slope */
Chris@69 250 opus_int shift, slope_part, flat_part;
Chris@69 251 flat_part = psEnc->sCmn.fs_kHz * 3;
Chris@69 252 slope_part = silk_RSHIFT( psEnc->sCmn.shapeWinLength - flat_part, 1 );
Chris@69 253
Chris@69 254 silk_apply_sine_window( x_windowed, x_ptr, 1, slope_part );
Chris@69 255 shift = slope_part;
Chris@69 256 silk_memcpy( x_windowed + shift, x_ptr + shift, flat_part * sizeof(opus_int16) );
Chris@69 257 shift += flat_part;
Chris@69 258 silk_apply_sine_window( x_windowed + shift, x_ptr + shift, 2, slope_part );
Chris@69 259
Chris@69 260 /* Update pointer: next LPC analysis block */
Chris@69 261 x_ptr += psEnc->sCmn.subfr_length;
Chris@69 262
Chris@69 263 if( psEnc->sCmn.warping_Q16 > 0 ) {
Chris@69 264 /* Calculate warped auto correlation */
Chris@69 265 silk_warped_autocorrelation_FIX( auto_corr, &scale, x_windowed, warping_Q16, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder, arch );
Chris@69 266 } else {
Chris@69 267 /* Calculate regular auto correlation */
Chris@69 268 silk_autocorr( auto_corr, &scale, x_windowed, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder + 1, arch );
Chris@69 269 }
Chris@69 270
Chris@69 271 /* Add white noise, as a fraction of energy */
Chris@69 272 auto_corr[0] = silk_ADD32( auto_corr[0], silk_max_32( silk_SMULWB( silk_RSHIFT( auto_corr[ 0 ], 4 ),
Chris@69 273 SILK_FIX_CONST( SHAPE_WHITE_NOISE_FRACTION, 20 ) ), 1 ) );
Chris@69 274
Chris@69 275 /* Calculate the reflection coefficients using schur */
Chris@69 276 nrg = silk_schur64( refl_coef_Q16, auto_corr, psEnc->sCmn.shapingLPCOrder );
Chris@69 277 silk_assert( nrg >= 0 );
Chris@69 278
Chris@69 279 /* Convert reflection coefficients to prediction coefficients */
Chris@69 280 silk_k2a_Q16( AR_Q24, refl_coef_Q16, psEnc->sCmn.shapingLPCOrder );
Chris@69 281
Chris@69 282 Qnrg = -scale; /* range: -12...30*/
Chris@69 283 silk_assert( Qnrg >= -12 );
Chris@69 284 silk_assert( Qnrg <= 30 );
Chris@69 285
Chris@69 286 /* Make sure that Qnrg is an even number */
Chris@69 287 if( Qnrg & 1 ) {
Chris@69 288 Qnrg -= 1;
Chris@69 289 nrg >>= 1;
Chris@69 290 }
Chris@69 291
Chris@69 292 tmp32 = silk_SQRT_APPROX( nrg );
Chris@69 293 Qnrg >>= 1; /* range: -6...15*/
Chris@69 294
Chris@69 295 psEncCtrl->Gains_Q16[ k ] = silk_LSHIFT_SAT32( tmp32, 16 - Qnrg );
Chris@69 296
Chris@69 297 if( psEnc->sCmn.warping_Q16 > 0 ) {
Chris@69 298 /* Adjust gain for warping */
Chris@69 299 gain_mult_Q16 = warped_gain( AR_Q24, warping_Q16, psEnc->sCmn.shapingLPCOrder );
Chris@69 300 silk_assert( psEncCtrl->Gains_Q16[ k ] > 0 );
Chris@69 301 if( psEncCtrl->Gains_Q16[ k ] < SILK_FIX_CONST( 0.25, 16 ) ) {
Chris@69 302 psEncCtrl->Gains_Q16[ k ] = silk_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 );
Chris@69 303 } else {
Chris@69 304 psEncCtrl->Gains_Q16[ k ] = silk_SMULWW( silk_RSHIFT_ROUND( psEncCtrl->Gains_Q16[ k ], 1 ), gain_mult_Q16 );
Chris@69 305 if ( psEncCtrl->Gains_Q16[ k ] >= ( silk_int32_MAX >> 1 ) ) {
Chris@69 306 psEncCtrl->Gains_Q16[ k ] = silk_int32_MAX;
Chris@69 307 } else {
Chris@69 308 psEncCtrl->Gains_Q16[ k ] = silk_LSHIFT32( psEncCtrl->Gains_Q16[ k ], 1 );
Chris@69 309 }
Chris@69 310 }
Chris@69 311 silk_assert( psEncCtrl->Gains_Q16[ k ] > 0 );
Chris@69 312 }
Chris@69 313
Chris@69 314 /* Bandwidth expansion */
Chris@69 315 silk_bwexpander_32( AR_Q24, psEnc->sCmn.shapingLPCOrder, BWExp_Q16 );
Chris@69 316
Chris@69 317 if( psEnc->sCmn.warping_Q16 > 0 ) {
Chris@69 318 /* Convert to monic warped prediction coefficients and limit absolute values */
Chris@69 319 limit_warped_coefs( AR_Q24, warping_Q16, SILK_FIX_CONST( 3.999, 24 ), psEnc->sCmn.shapingLPCOrder );
Chris@69 320
Chris@69 321 /* Convert from Q24 to Q13 and store in int16 */
Chris@69 322 for( i = 0; i < psEnc->sCmn.shapingLPCOrder; i++ ) {
Chris@69 323 psEncCtrl->AR_Q13[ k * MAX_SHAPE_LPC_ORDER + i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( AR_Q24[ i ], 11 ) );
Chris@69 324 }
Chris@69 325 } else {
Chris@69 326 silk_LPC_fit( &psEncCtrl->AR_Q13[ k * MAX_SHAPE_LPC_ORDER ], AR_Q24, 13, 24, psEnc->sCmn.shapingLPCOrder );
Chris@69 327 }
Chris@69 328 }
Chris@69 329
Chris@69 330 /*****************/
Chris@69 331 /* Gain tweaking */
Chris@69 332 /*****************/
Chris@69 333 /* Increase gains during low speech activity and put lower limit on gains */
Chris@69 334 gain_mult_Q16 = silk_log2lin( -silk_SMLAWB( -SILK_FIX_CONST( 16.0, 7 ), SNR_adj_dB_Q7, SILK_FIX_CONST( 0.16, 16 ) ) );
Chris@69 335 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 ) ) );
Chris@69 336 silk_assert( gain_mult_Q16 > 0 );
Chris@69 337 for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
Chris@69 338 psEncCtrl->Gains_Q16[ k ] = silk_SMULWW( psEncCtrl->Gains_Q16[ k ], gain_mult_Q16 );
Chris@69 339 silk_assert( psEncCtrl->Gains_Q16[ k ] >= 0 );
Chris@69 340 psEncCtrl->Gains_Q16[ k ] = silk_ADD_POS_SAT32( psEncCtrl->Gains_Q16[ k ], gain_add_Q16 );
Chris@69 341 }
Chris@69 342
Chris@69 343
Chris@69 344 /************************************************/
Chris@69 345 /* Control low-frequency shaping and noise tilt */
Chris@69 346 /************************************************/
Chris@69 347 /* Less low frequency shaping for noisy inputs */
Chris@69 348 strength_Q16 = silk_MUL( SILK_FIX_CONST( LOW_FREQ_SHAPING, 4 ), silk_SMLAWB( SILK_FIX_CONST( 1.0, 12 ),
Chris@69 349 SILK_FIX_CONST( LOW_QUALITY_LOW_FREQ_SHAPING_DECR, 13 ), psEnc->sCmn.input_quality_bands_Q15[ 0 ] - SILK_FIX_CONST( 1.0, 15 ) ) );
Chris@69 350 strength_Q16 = silk_RSHIFT( silk_MUL( strength_Q16, psEnc->sCmn.speech_activity_Q8 ), 8 );
Chris@69 351 if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
Chris@69 352 /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */
Chris@69 353 /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/
Chris@69 354 opus_int fs_kHz_inv = silk_DIV32_16( SILK_FIX_CONST( 0.2, 14 ), psEnc->sCmn.fs_kHz );
Chris@69 355 for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) {
Chris@69 356 b_Q14 = fs_kHz_inv + silk_DIV32_16( SILK_FIX_CONST( 3.0, 14 ), psEncCtrl->pitchL[ k ] );
Chris@69 357 /* Pack two coefficients in one int32 */
Chris@69 358 psEncCtrl->LF_shp_Q14[ k ] = silk_LSHIFT( SILK_FIX_CONST( 1.0, 14 ) - b_Q14 - silk_SMULWB( strength_Q16, b_Q14 ), 16 );
Chris@69 359 psEncCtrl->LF_shp_Q14[ k ] |= (opus_uint16)( b_Q14 - SILK_FIX_CONST( 1.0, 14 ) );
Chris@69 360 }
Chris@69 361 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*/
Chris@69 362 Tilt_Q16 = - SILK_FIX_CONST( HP_NOISE_COEF, 16 ) -
Chris@69 363 silk_SMULWB( SILK_FIX_CONST( 1.0, 16 ) - SILK_FIX_CONST( HP_NOISE_COEF, 16 ),
Chris@69 364 silk_SMULWB( SILK_FIX_CONST( HARM_HP_NOISE_COEF, 24 ), psEnc->sCmn.speech_activity_Q8 ) );
Chris@69 365 } else {
Chris@69 366 b_Q14 = silk_DIV32_16( 21299, psEnc->sCmn.fs_kHz ); /* 1.3_Q0 = 21299_Q14*/
Chris@69 367 /* Pack two coefficients in one int32 */
Chris@69 368 psEncCtrl->LF_shp_Q14[ 0 ] = silk_LSHIFT( SILK_FIX_CONST( 1.0, 14 ) - b_Q14 -
Chris@69 369 silk_SMULWB( strength_Q16, silk_SMULWB( SILK_FIX_CONST( 0.6, 16 ), b_Q14 ) ), 16 );
Chris@69 370 psEncCtrl->LF_shp_Q14[ 0 ] |= (opus_uint16)( b_Q14 - SILK_FIX_CONST( 1.0, 14 ) );
Chris@69 371 for( k = 1; k < psEnc->sCmn.nb_subfr; k++ ) {
Chris@69 372 psEncCtrl->LF_shp_Q14[ k ] = psEncCtrl->LF_shp_Q14[ 0 ];
Chris@69 373 }
Chris@69 374 Tilt_Q16 = -SILK_FIX_CONST( HP_NOISE_COEF, 16 );
Chris@69 375 }
Chris@69 376
Chris@69 377 /****************************/
Chris@69 378 /* HARMONIC SHAPING CONTROL */
Chris@69 379 /****************************/
Chris@69 380 if( USE_HARM_SHAPING && psEnc->sCmn.indices.signalType == TYPE_VOICED ) {
Chris@69 381 /* More harmonic noise shaping for high bitrates or noisy input */
Chris@69 382 HarmShapeGain_Q16 = silk_SMLAWB( SILK_FIX_CONST( HARMONIC_SHAPING, 16 ),
Chris@69 383 SILK_FIX_CONST( 1.0, 16 ) - silk_SMULWB( SILK_FIX_CONST( 1.0, 18 ) - silk_LSHIFT( psEncCtrl->coding_quality_Q14, 4 ),
Chris@69 384 psEncCtrl->input_quality_Q14 ), SILK_FIX_CONST( HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING, 16 ) );
Chris@69 385
Chris@69 386 /* Less harmonic noise shaping for less periodic signals */
Chris@69 387 HarmShapeGain_Q16 = silk_SMULWB( silk_LSHIFT( HarmShapeGain_Q16, 1 ),
Chris@69 388 silk_SQRT_APPROX( silk_LSHIFT( psEnc->LTPCorr_Q15, 15 ) ) );
Chris@69 389 } else {
Chris@69 390 HarmShapeGain_Q16 = 0;
Chris@69 391 }
Chris@69 392
Chris@69 393 /*************************/
Chris@69 394 /* Smooth over subframes */
Chris@69 395 /*************************/
Chris@69 396 for( k = 0; k < MAX_NB_SUBFR; k++ ) {
Chris@69 397 psShapeSt->HarmShapeGain_smth_Q16 =
Chris@69 398 silk_SMLAWB( psShapeSt->HarmShapeGain_smth_Q16, HarmShapeGain_Q16 - psShapeSt->HarmShapeGain_smth_Q16, SILK_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );
Chris@69 399 psShapeSt->Tilt_smth_Q16 =
Chris@69 400 silk_SMLAWB( psShapeSt->Tilt_smth_Q16, Tilt_Q16 - psShapeSt->Tilt_smth_Q16, SILK_FIX_CONST( SUBFR_SMTH_COEF, 16 ) );
Chris@69 401
Chris@69 402 psEncCtrl->HarmShapeGain_Q14[ k ] = ( opus_int )silk_RSHIFT_ROUND( psShapeSt->HarmShapeGain_smth_Q16, 2 );
Chris@69 403 psEncCtrl->Tilt_Q14[ k ] = ( opus_int )silk_RSHIFT_ROUND( psShapeSt->Tilt_smth_Q16, 2 );
Chris@69 404 }
Chris@69 405 RESTORE_STACK;
Chris@69 406 }
Chris@69 407 #endif /* OVERRIDE_silk_noise_shape_analysis_FIX */