michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "main_FLP.h" michael@0: #include "tuning_parameters.h" michael@0: michael@0: /* Compute gain to make warped filter coefficients have a zero mean log frequency response on a */ michael@0: /* non-warped frequency scale. (So that it can be implemented with a minimum-phase monic filter.) */ michael@0: /* Note: A monic filter is one with the first coefficient equal to 1.0. In Silk we omit the first */ michael@0: /* coefficient in an array of coefficients, for monic filters. */ michael@0: static OPUS_INLINE silk_float warped_gain( michael@0: const silk_float *coefs, michael@0: silk_float lambda, michael@0: opus_int order michael@0: ) { michael@0: opus_int i; michael@0: silk_float gain; michael@0: michael@0: lambda = -lambda; michael@0: gain = coefs[ order - 1 ]; michael@0: for( i = order - 2; i >= 0; i-- ) { michael@0: gain = lambda * gain + coefs[ i ]; michael@0: } michael@0: return (silk_float)( 1.0f / ( 1.0f - lambda * gain ) ); michael@0: } michael@0: michael@0: /* Convert warped filter coefficients to monic pseudo-warped coefficients and limit maximum */ michael@0: /* amplitude of monic warped coefficients by using bandwidth expansion on the true coefficients */ michael@0: static OPUS_INLINE void warped_true2monic_coefs( michael@0: silk_float *coefs_syn, michael@0: silk_float *coefs_ana, michael@0: silk_float lambda, michael@0: silk_float limit, michael@0: opus_int order michael@0: ) { michael@0: opus_int i, iter, ind = 0; michael@0: silk_float tmp, maxabs, chirp, gain_syn, gain_ana; michael@0: michael@0: /* Convert to monic coefficients */ michael@0: for( i = order - 1; i > 0; i-- ) { michael@0: coefs_syn[ i - 1 ] -= lambda * coefs_syn[ i ]; michael@0: coefs_ana[ i - 1 ] -= lambda * coefs_ana[ i ]; michael@0: } michael@0: gain_syn = ( 1.0f - lambda * lambda ) / ( 1.0f + lambda * coefs_syn[ 0 ] ); michael@0: gain_ana = ( 1.0f - lambda * lambda ) / ( 1.0f + lambda * coefs_ana[ 0 ] ); michael@0: for( i = 0; i < order; i++ ) { michael@0: coefs_syn[ i ] *= gain_syn; michael@0: coefs_ana[ i ] *= gain_ana; michael@0: } michael@0: michael@0: /* Limit */ michael@0: for( iter = 0; iter < 10; iter++ ) { michael@0: /* Find maximum absolute value */ michael@0: maxabs = -1.0f; michael@0: for( i = 0; i < order; i++ ) { michael@0: tmp = silk_max( silk_abs_float( coefs_syn[ i ] ), silk_abs_float( coefs_ana[ i ] ) ); michael@0: if( tmp > maxabs ) { michael@0: maxabs = tmp; michael@0: ind = i; michael@0: } michael@0: } michael@0: if( maxabs <= limit ) { michael@0: /* Coefficients are within range - done */ michael@0: return; michael@0: } michael@0: michael@0: /* Convert back to true warped coefficients */ michael@0: for( i = 1; i < order; i++ ) { michael@0: coefs_syn[ i - 1 ] += lambda * coefs_syn[ i ]; michael@0: coefs_ana[ i - 1 ] += lambda * coefs_ana[ i ]; michael@0: } michael@0: gain_syn = 1.0f / gain_syn; michael@0: gain_ana = 1.0f / gain_ana; michael@0: for( i = 0; i < order; i++ ) { michael@0: coefs_syn[ i ] *= gain_syn; michael@0: coefs_ana[ i ] *= gain_ana; michael@0: } michael@0: michael@0: /* Apply bandwidth expansion */ michael@0: chirp = 0.99f - ( 0.8f + 0.1f * iter ) * ( maxabs - limit ) / ( maxabs * ( ind + 1 ) ); michael@0: silk_bwexpander_FLP( coefs_syn, order, chirp ); michael@0: silk_bwexpander_FLP( coefs_ana, order, chirp ); michael@0: michael@0: /* Convert to monic warped coefficients */ michael@0: for( i = order - 1; i > 0; i-- ) { michael@0: coefs_syn[ i - 1 ] -= lambda * coefs_syn[ i ]; michael@0: coefs_ana[ i - 1 ] -= lambda * coefs_ana[ i ]; michael@0: } michael@0: gain_syn = ( 1.0f - lambda * lambda ) / ( 1.0f + lambda * coefs_syn[ 0 ] ); michael@0: gain_ana = ( 1.0f - lambda * lambda ) / ( 1.0f + lambda * coefs_ana[ 0 ] ); michael@0: for( i = 0; i < order; i++ ) { michael@0: coefs_syn[ i ] *= gain_syn; michael@0: coefs_ana[ i ] *= gain_ana; michael@0: } michael@0: } michael@0: silk_assert( 0 ); michael@0: } michael@0: michael@0: /* Compute noise shaping coefficients and initial gain values */ michael@0: void silk_noise_shape_analysis_FLP( michael@0: silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ michael@0: silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ michael@0: const silk_float *pitch_res, /* I LPC residual from pitch analysis */ michael@0: const silk_float *x /* I Input signal [frame_length + la_shape] */ michael@0: ) michael@0: { michael@0: silk_shape_state_FLP *psShapeSt = &psEnc->sShape; michael@0: opus_int k, nSamples; michael@0: silk_float SNR_adj_dB, HarmBoost, HarmShapeGain, Tilt; michael@0: silk_float nrg, pre_nrg, log_energy, log_energy_prev, energy_variation; michael@0: silk_float delta, BWExp1, BWExp2, gain_mult, gain_add, strength, b, warping; michael@0: silk_float x_windowed[ SHAPE_LPC_WIN_MAX ]; michael@0: silk_float auto_corr[ MAX_SHAPE_LPC_ORDER + 1 ]; michael@0: const silk_float *x_ptr, *pitch_res_ptr; michael@0: michael@0: /* Point to start of first LPC analysis block */ michael@0: x_ptr = x - psEnc->sCmn.la_shape; michael@0: michael@0: /****************/ michael@0: /* GAIN CONTROL */ michael@0: /****************/ michael@0: SNR_adj_dB = psEnc->sCmn.SNR_dB_Q7 * ( 1 / 128.0f ); michael@0: michael@0: /* Input quality is the average of the quality in the lowest two VAD bands */ michael@0: psEncCtrl->input_quality = 0.5f * ( psEnc->sCmn.input_quality_bands_Q15[ 0 ] + psEnc->sCmn.input_quality_bands_Q15[ 1 ] ) * ( 1.0f / 32768.0f ); michael@0: michael@0: /* Coding quality level, between 0.0 and 1.0 */ michael@0: psEncCtrl->coding_quality = silk_sigmoid( 0.25f * ( SNR_adj_dB - 20.0f ) ); michael@0: michael@0: if( psEnc->sCmn.useCBR == 0 ) { michael@0: /* Reduce coding SNR during low speech activity */ michael@0: b = 1.0f - psEnc->sCmn.speech_activity_Q8 * ( 1.0f / 256.0f ); michael@0: SNR_adj_dB -= BG_SNR_DECR_dB * psEncCtrl->coding_quality * ( 0.5f + 0.5f * psEncCtrl->input_quality ) * b * b; michael@0: } michael@0: michael@0: if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) { michael@0: /* Reduce gains for periodic signals */ michael@0: SNR_adj_dB += HARM_SNR_INCR_dB * psEnc->LTPCorr; michael@0: } else { michael@0: /* For unvoiced signals and low-quality input, adjust the quality slower than SNR_dB setting */ michael@0: SNR_adj_dB += ( -0.4f * psEnc->sCmn.SNR_dB_Q7 * ( 1 / 128.0f ) + 6.0f ) * ( 1.0f - psEncCtrl->input_quality ); michael@0: } michael@0: michael@0: /*************************/ michael@0: /* SPARSENESS PROCESSING */ michael@0: /*************************/ michael@0: /* Set quantizer offset */ michael@0: if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) { michael@0: /* Initially set to 0; may be overruled in process_gains(..) */ michael@0: psEnc->sCmn.indices.quantOffsetType = 0; michael@0: psEncCtrl->sparseness = 0.0f; michael@0: } else { michael@0: /* Sparseness measure, based on relative fluctuations of energy per 2 milliseconds */ michael@0: nSamples = 2 * psEnc->sCmn.fs_kHz; michael@0: energy_variation = 0.0f; michael@0: log_energy_prev = 0.0f; michael@0: pitch_res_ptr = pitch_res; michael@0: for( k = 0; k < silk_SMULBB( SUB_FRAME_LENGTH_MS, psEnc->sCmn.nb_subfr ) / 2; k++ ) { michael@0: nrg = ( silk_float )nSamples + ( silk_float )silk_energy_FLP( pitch_res_ptr, nSamples ); michael@0: log_energy = silk_log2( nrg ); michael@0: if( k > 0 ) { michael@0: energy_variation += silk_abs_float( log_energy - log_energy_prev ); michael@0: } michael@0: log_energy_prev = log_energy; michael@0: pitch_res_ptr += nSamples; michael@0: } michael@0: psEncCtrl->sparseness = silk_sigmoid( 0.4f * ( energy_variation - 5.0f ) ); michael@0: michael@0: /* Set quantization offset depending on sparseness measure */ michael@0: if( psEncCtrl->sparseness > SPARSENESS_THRESHOLD_QNT_OFFSET ) { michael@0: psEnc->sCmn.indices.quantOffsetType = 0; michael@0: } else { michael@0: psEnc->sCmn.indices.quantOffsetType = 1; michael@0: } michael@0: michael@0: /* Increase coding SNR for sparse signals */ michael@0: SNR_adj_dB += SPARSE_SNR_INCR_dB * ( psEncCtrl->sparseness - 0.5f ); michael@0: } michael@0: michael@0: /*******************************/ michael@0: /* Control bandwidth expansion */ michael@0: /*******************************/ michael@0: /* More BWE for signals with high prediction gain */ michael@0: strength = FIND_PITCH_WHITE_NOISE_FRACTION * psEncCtrl->predGain; /* between 0.0 and 1.0 */ michael@0: BWExp1 = BWExp2 = BANDWIDTH_EXPANSION / ( 1.0f + strength * strength ); michael@0: delta = LOW_RATE_BANDWIDTH_EXPANSION_DELTA * ( 1.0f - 0.75f * psEncCtrl->coding_quality ); michael@0: BWExp1 -= delta; michael@0: BWExp2 += delta; michael@0: /* BWExp1 will be applied after BWExp2, so make it relative */ michael@0: BWExp1 /= BWExp2; michael@0: michael@0: if( psEnc->sCmn.warping_Q16 > 0 ) { michael@0: /* Slightly more warping in analysis will move quantization noise up in frequency, where it's better masked */ michael@0: warping = (silk_float)psEnc->sCmn.warping_Q16 / 65536.0f + 0.01f * psEncCtrl->coding_quality; michael@0: } else { michael@0: warping = 0.0f; michael@0: } michael@0: michael@0: /********************************************/ michael@0: /* Compute noise shaping AR coefs and gains */ michael@0: /********************************************/ michael@0: for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { michael@0: /* Apply window: sine slope followed by flat part followed by cosine slope */ michael@0: opus_int shift, slope_part, flat_part; michael@0: flat_part = psEnc->sCmn.fs_kHz * 3; michael@0: slope_part = ( psEnc->sCmn.shapeWinLength - flat_part ) / 2; michael@0: michael@0: silk_apply_sine_window_FLP( x_windowed, x_ptr, 1, slope_part ); michael@0: shift = slope_part; michael@0: silk_memcpy( x_windowed + shift, x_ptr + shift, flat_part * sizeof(silk_float) ); michael@0: shift += flat_part; michael@0: silk_apply_sine_window_FLP( x_windowed + shift, x_ptr + shift, 2, slope_part ); michael@0: michael@0: /* Update pointer: next LPC analysis block */ michael@0: x_ptr += psEnc->sCmn.subfr_length; michael@0: michael@0: if( psEnc->sCmn.warping_Q16 > 0 ) { michael@0: /* Calculate warped auto correlation */ michael@0: silk_warped_autocorrelation_FLP( auto_corr, x_windowed, warping, michael@0: psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder ); michael@0: } else { michael@0: /* Calculate regular auto correlation */ michael@0: silk_autocorrelation_FLP( auto_corr, x_windowed, psEnc->sCmn.shapeWinLength, psEnc->sCmn.shapingLPCOrder + 1 ); michael@0: } michael@0: michael@0: /* Add white noise, as a fraction of energy */ michael@0: auto_corr[ 0 ] += auto_corr[ 0 ] * SHAPE_WHITE_NOISE_FRACTION; michael@0: michael@0: /* Convert correlations to prediction coefficients, and compute residual energy */ michael@0: nrg = silk_levinsondurbin_FLP( &psEncCtrl->AR2[ k * MAX_SHAPE_LPC_ORDER ], auto_corr, psEnc->sCmn.shapingLPCOrder ); michael@0: psEncCtrl->Gains[ k ] = ( silk_float )sqrt( nrg ); michael@0: michael@0: if( psEnc->sCmn.warping_Q16 > 0 ) { michael@0: /* Adjust gain for warping */ michael@0: psEncCtrl->Gains[ k ] *= warped_gain( &psEncCtrl->AR2[ k * MAX_SHAPE_LPC_ORDER ], warping, psEnc->sCmn.shapingLPCOrder ); michael@0: } michael@0: michael@0: /* Bandwidth expansion for synthesis filter shaping */ michael@0: silk_bwexpander_FLP( &psEncCtrl->AR2[ k * MAX_SHAPE_LPC_ORDER ], psEnc->sCmn.shapingLPCOrder, BWExp2 ); michael@0: michael@0: /* Compute noise shaping filter coefficients */ michael@0: silk_memcpy( michael@0: &psEncCtrl->AR1[ k * MAX_SHAPE_LPC_ORDER ], michael@0: &psEncCtrl->AR2[ k * MAX_SHAPE_LPC_ORDER ], michael@0: psEnc->sCmn.shapingLPCOrder * sizeof( silk_float ) ); michael@0: michael@0: /* Bandwidth expansion for analysis filter shaping */ michael@0: silk_bwexpander_FLP( &psEncCtrl->AR1[ k * MAX_SHAPE_LPC_ORDER ], psEnc->sCmn.shapingLPCOrder, BWExp1 ); michael@0: michael@0: /* Ratio of prediction gains, in energy domain */ michael@0: pre_nrg = silk_LPC_inverse_pred_gain_FLP( &psEncCtrl->AR2[ k * MAX_SHAPE_LPC_ORDER ], psEnc->sCmn.shapingLPCOrder ); michael@0: nrg = silk_LPC_inverse_pred_gain_FLP( &psEncCtrl->AR1[ k * MAX_SHAPE_LPC_ORDER ], psEnc->sCmn.shapingLPCOrder ); michael@0: psEncCtrl->GainsPre[ k ] = 1.0f - 0.7f * ( 1.0f - pre_nrg / nrg ); michael@0: michael@0: /* Convert to monic warped prediction coefficients and limit absolute values */ michael@0: warped_true2monic_coefs( &psEncCtrl->AR2[ k * MAX_SHAPE_LPC_ORDER ], &psEncCtrl->AR1[ k * MAX_SHAPE_LPC_ORDER ], michael@0: warping, 3.999f, psEnc->sCmn.shapingLPCOrder ); michael@0: } michael@0: michael@0: /*****************/ michael@0: /* Gain tweaking */ michael@0: /*****************/ michael@0: /* Increase gains during low speech activity */ michael@0: gain_mult = (silk_float)pow( 2.0f, -0.16f * SNR_adj_dB ); michael@0: gain_add = (silk_float)pow( 2.0f, 0.16f * MIN_QGAIN_DB ); michael@0: for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { michael@0: psEncCtrl->Gains[ k ] *= gain_mult; michael@0: psEncCtrl->Gains[ k ] += gain_add; michael@0: } michael@0: michael@0: gain_mult = 1.0f + INPUT_TILT + psEncCtrl->coding_quality * HIGH_RATE_INPUT_TILT; michael@0: for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { michael@0: psEncCtrl->GainsPre[ k ] *= gain_mult; michael@0: } michael@0: michael@0: /************************************************/ michael@0: /* Control low-frequency shaping and noise tilt */ michael@0: /************************************************/ michael@0: /* Less low frequency shaping for noisy inputs */ michael@0: strength = LOW_FREQ_SHAPING * ( 1.0f + LOW_QUALITY_LOW_FREQ_SHAPING_DECR * ( psEnc->sCmn.input_quality_bands_Q15[ 0 ] * ( 1.0f / 32768.0f ) - 1.0f ) ); michael@0: strength *= psEnc->sCmn.speech_activity_Q8 * ( 1.0f / 256.0f ); michael@0: if( psEnc->sCmn.indices.signalType == TYPE_VOICED ) { michael@0: /* Reduce low frequencies quantization noise for periodic signals, depending on pitch lag */ michael@0: /*f = 400; freqz([1, -0.98 + 2e-4 * f], [1, -0.97 + 7e-4 * f], 2^12, Fs); axis([0, 1000, -10, 1])*/ michael@0: for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { michael@0: b = 0.2f / psEnc->sCmn.fs_kHz + 3.0f / psEncCtrl->pitchL[ k ]; michael@0: psEncCtrl->LF_MA_shp[ k ] = -1.0f + b; michael@0: psEncCtrl->LF_AR_shp[ k ] = 1.0f - b - b * strength; michael@0: } michael@0: Tilt = - HP_NOISE_COEF - michael@0: (1 - HP_NOISE_COEF) * HARM_HP_NOISE_COEF * psEnc->sCmn.speech_activity_Q8 * ( 1.0f / 256.0f ); michael@0: } else { michael@0: b = 1.3f / psEnc->sCmn.fs_kHz; michael@0: psEncCtrl->LF_MA_shp[ 0 ] = -1.0f + b; michael@0: psEncCtrl->LF_AR_shp[ 0 ] = 1.0f - b - b * strength * 0.6f; michael@0: for( k = 1; k < psEnc->sCmn.nb_subfr; k++ ) { michael@0: psEncCtrl->LF_MA_shp[ k ] = psEncCtrl->LF_MA_shp[ 0 ]; michael@0: psEncCtrl->LF_AR_shp[ k ] = psEncCtrl->LF_AR_shp[ 0 ]; michael@0: } michael@0: Tilt = -HP_NOISE_COEF; michael@0: } michael@0: michael@0: /****************************/ michael@0: /* HARMONIC SHAPING CONTROL */ michael@0: /****************************/ michael@0: /* Control boosting of harmonic frequencies */ michael@0: HarmBoost = LOW_RATE_HARMONIC_BOOST * ( 1.0f - psEncCtrl->coding_quality ) * psEnc->LTPCorr; michael@0: michael@0: /* More harmonic boost for noisy input signals */ michael@0: HarmBoost += LOW_INPUT_QUALITY_HARMONIC_BOOST * ( 1.0f - psEncCtrl->input_quality ); michael@0: michael@0: if( USE_HARM_SHAPING && psEnc->sCmn.indices.signalType == TYPE_VOICED ) { michael@0: /* Harmonic noise shaping */ michael@0: HarmShapeGain = HARMONIC_SHAPING; michael@0: michael@0: /* More harmonic noise shaping for high bitrates or noisy input */ michael@0: HarmShapeGain += HIGH_RATE_OR_LOW_QUALITY_HARMONIC_SHAPING * michael@0: ( 1.0f - ( 1.0f - psEncCtrl->coding_quality ) * psEncCtrl->input_quality ); michael@0: michael@0: /* Less harmonic noise shaping for less periodic signals */ michael@0: HarmShapeGain *= ( silk_float )sqrt( psEnc->LTPCorr ); michael@0: } else { michael@0: HarmShapeGain = 0.0f; michael@0: } michael@0: michael@0: /*************************/ michael@0: /* Smooth over subframes */ michael@0: /*************************/ michael@0: for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { michael@0: psShapeSt->HarmBoost_smth += SUBFR_SMTH_COEF * ( HarmBoost - psShapeSt->HarmBoost_smth ); michael@0: psEncCtrl->HarmBoost[ k ] = psShapeSt->HarmBoost_smth; michael@0: psShapeSt->HarmShapeGain_smth += SUBFR_SMTH_COEF * ( HarmShapeGain - psShapeSt->HarmShapeGain_smth ); michael@0: psEncCtrl->HarmShapeGain[ k ] = psShapeSt->HarmShapeGain_smth; michael@0: psShapeSt->Tilt_smth += SUBFR_SMTH_COEF * ( Tilt - psShapeSt->Tilt_smth ); michael@0: psEncCtrl->Tilt[ k ] = psShapeSt->Tilt_smth; michael@0: } michael@0: }