Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "SigProc_FIX.h" Chris@69: #include "define.h" Chris@69: Chris@69: #define QA 24 Chris@69: #define A_LIMIT SILK_FIX_CONST( 0.99975, QA ) Chris@69: Chris@69: #define MUL32_FRAC_Q(a32, b32, Q) ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q))) Chris@69: Chris@69: /* Compute inverse of LPC prediction gain, and */ Chris@69: /* test if LPC coefficients are stable (all poles within unit circle) */ Chris@69: static opus_int32 LPC_inverse_pred_gain_QA_c( /* O Returns inverse prediction gain in energy domain, Q30 */ Chris@69: opus_int32 A_QA[ SILK_MAX_ORDER_LPC ], /* I Prediction coefficients */ Chris@69: const opus_int order /* I Prediction order */ Chris@69: ) Chris@69: { Chris@69: opus_int k, n, mult2Q; Chris@69: opus_int32 invGain_Q30, rc_Q31, rc_mult1_Q30, rc_mult2, tmp1, tmp2; Chris@69: Chris@69: invGain_Q30 = SILK_FIX_CONST( 1, 30 ); Chris@69: for( k = order - 1; k > 0; k-- ) { Chris@69: /* Check for stability */ Chris@69: if( ( A_QA[ k ] > A_LIMIT ) || ( A_QA[ k ] < -A_LIMIT ) ) { Chris@69: return 0; Chris@69: } Chris@69: Chris@69: /* Set RC equal to negated AR coef */ Chris@69: rc_Q31 = -silk_LSHIFT( A_QA[ k ], 31 - QA ); Chris@69: Chris@69: /* rc_mult1_Q30 range: [ 1 : 2^30 ] */ Chris@69: rc_mult1_Q30 = silk_SUB32( SILK_FIX_CONST( 1, 30 ), silk_SMMUL( rc_Q31, rc_Q31 ) ); Chris@69: silk_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */ Chris@69: silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) ); Chris@69: Chris@69: /* Update inverse gain */ Chris@69: /* invGain_Q30 range: [ 0 : 2^30 ] */ Chris@69: invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 ); Chris@69: silk_assert( invGain_Q30 >= 0 ); Chris@69: silk_assert( invGain_Q30 <= ( 1 << 30 ) ); Chris@69: if( invGain_Q30 < SILK_FIX_CONST( 1.0f / MAX_PREDICTION_POWER_GAIN, 30 ) ) { Chris@69: return 0; Chris@69: } Chris@69: Chris@69: /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */ Chris@69: mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) ); Chris@69: rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 ); Chris@69: Chris@69: /* Update AR coefficient */ Chris@69: for( n = 0; n < (k + 1) >> 1; n++ ) { Chris@69: opus_int64 tmp64; Chris@69: tmp1 = A_QA[ n ]; Chris@69: tmp2 = A_QA[ k - n - 1 ]; Chris@69: tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( silk_SUB_SAT32(tmp1, Chris@69: MUL32_FRAC_Q( tmp2, rc_Q31, 31 ) ), rc_mult2 ), mult2Q); Chris@69: if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) { Chris@69: return 0; Chris@69: } Chris@69: A_QA[ n ] = ( opus_int32 )tmp64; Chris@69: tmp64 = silk_RSHIFT_ROUND64( silk_SMULL( silk_SUB_SAT32(tmp2, Chris@69: MUL32_FRAC_Q( tmp1, rc_Q31, 31 ) ), rc_mult2), mult2Q); Chris@69: if( tmp64 > silk_int32_MAX || tmp64 < silk_int32_MIN ) { Chris@69: return 0; Chris@69: } Chris@69: A_QA[ k - n - 1 ] = ( opus_int32 )tmp64; Chris@69: } Chris@69: } Chris@69: Chris@69: /* Check for stability */ Chris@69: if( ( A_QA[ k ] > A_LIMIT ) || ( A_QA[ k ] < -A_LIMIT ) ) { Chris@69: return 0; Chris@69: } Chris@69: Chris@69: /* Set RC equal to negated AR coef */ Chris@69: rc_Q31 = -silk_LSHIFT( A_QA[ 0 ], 31 - QA ); Chris@69: Chris@69: /* Range: [ 1 : 2^30 ] */ Chris@69: rc_mult1_Q30 = silk_SUB32( SILK_FIX_CONST( 1, 30 ), silk_SMMUL( rc_Q31, rc_Q31 ) ); Chris@69: Chris@69: /* Update inverse gain */ Chris@69: /* Range: [ 0 : 2^30 ] */ Chris@69: invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 ); Chris@69: silk_assert( invGain_Q30 >= 0 ); Chris@69: silk_assert( invGain_Q30 <= ( 1 << 30 ) ); Chris@69: if( invGain_Q30 < SILK_FIX_CONST( 1.0f / MAX_PREDICTION_POWER_GAIN, 30 ) ) { Chris@69: return 0; Chris@69: } Chris@69: Chris@69: return invGain_Q30; Chris@69: } Chris@69: Chris@69: /* For input in Q12 domain */ Chris@69: opus_int32 silk_LPC_inverse_pred_gain_c( /* O Returns inverse prediction gain in energy domain, Q30 */ Chris@69: const opus_int16 *A_Q12, /* I Prediction coefficients, Q12 [order] */ Chris@69: const opus_int order /* I Prediction order */ Chris@69: ) Chris@69: { Chris@69: opus_int k; Chris@69: opus_int32 Atmp_QA[ SILK_MAX_ORDER_LPC ]; Chris@69: opus_int32 DC_resp = 0; Chris@69: Chris@69: /* Increase Q domain of the AR coefficients */ Chris@69: for( k = 0; k < order; k++ ) { Chris@69: DC_resp += (opus_int32)A_Q12[ k ]; Chris@69: Atmp_QA[ k ] = silk_LSHIFT32( (opus_int32)A_Q12[ k ], QA - 12 ); Chris@69: } Chris@69: /* If the DC is unstable, we don't even need to do the full calculations */ Chris@69: if( DC_resp >= 4096 ) { Chris@69: return 0; Chris@69: } Chris@69: return LPC_inverse_pred_gain_QA_c( Atmp_QA, order ); Chris@69: }