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 "SigProc_FIX.h" michael@0: michael@0: #define QA 24 michael@0: #define A_LIMIT SILK_FIX_CONST( 0.99975, QA ) michael@0: michael@0: #define MUL32_FRAC_Q(a32, b32, Q) ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q))) michael@0: michael@0: /* Compute inverse of LPC prediction gain, and */ michael@0: /* test if LPC coefficients are stable (all poles within unit circle) */ michael@0: static opus_int32 LPC_inverse_pred_gain_QA( /* O Returns inverse prediction gain in energy domain, Q30 */ michael@0: opus_int32 A_QA[ 2 ][ SILK_MAX_ORDER_LPC ], /* I Prediction coefficients */ michael@0: const opus_int order /* I Prediction order */ michael@0: ) michael@0: { michael@0: opus_int k, n, mult2Q; michael@0: opus_int32 invGain_Q30, rc_Q31, rc_mult1_Q30, rc_mult2, tmp_QA; michael@0: opus_int32 *Aold_QA, *Anew_QA; michael@0: michael@0: Anew_QA = A_QA[ order & 1 ]; michael@0: michael@0: invGain_Q30 = (opus_int32)1 << 30; michael@0: for( k = order - 1; k > 0; k-- ) { michael@0: /* Check for stability */ michael@0: if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) { michael@0: return 0; michael@0: } michael@0: michael@0: /* Set RC equal to negated AR coef */ michael@0: rc_Q31 = -silk_LSHIFT( Anew_QA[ k ], 31 - QA ); michael@0: michael@0: /* rc_mult1_Q30 range: [ 1 : 2^30 ] */ michael@0: rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 ); michael@0: silk_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */ michael@0: silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) ); michael@0: michael@0: /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */ michael@0: mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) ); michael@0: rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 ); michael@0: michael@0: /* Update inverse gain */ michael@0: /* invGain_Q30 range: [ 0 : 2^30 ] */ michael@0: invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 ); michael@0: silk_assert( invGain_Q30 >= 0 ); michael@0: silk_assert( invGain_Q30 <= ( 1 << 30 ) ); michael@0: michael@0: /* Swap pointers */ michael@0: Aold_QA = Anew_QA; michael@0: Anew_QA = A_QA[ k & 1 ]; michael@0: michael@0: /* Update AR coefficient */ michael@0: for( n = 0; n < k; n++ ) { michael@0: tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 ); michael@0: Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q ); michael@0: } michael@0: } michael@0: michael@0: /* Check for stability */ michael@0: if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) { michael@0: return 0; michael@0: } michael@0: michael@0: /* Set RC equal to negated AR coef */ michael@0: rc_Q31 = -silk_LSHIFT( Anew_QA[ 0 ], 31 - QA ); michael@0: michael@0: /* Range: [ 1 : 2^30 ] */ michael@0: rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 ); michael@0: michael@0: /* Update inverse gain */ michael@0: /* Range: [ 0 : 2^30 ] */ michael@0: invGain_Q30 = silk_LSHIFT( silk_SMMUL( invGain_Q30, rc_mult1_Q30 ), 2 ); michael@0: silk_assert( invGain_Q30 >= 0 ); michael@0: silk_assert( invGain_Q30 <= 1<<30 ); michael@0: michael@0: return invGain_Q30; michael@0: } michael@0: michael@0: /* For input in Q12 domain */ michael@0: opus_int32 silk_LPC_inverse_pred_gain( /* O Returns inverse prediction gain in energy domain, Q30 */ michael@0: const opus_int16 *A_Q12, /* I Prediction coefficients, Q12 [order] */ michael@0: const opus_int order /* I Prediction order */ michael@0: ) michael@0: { michael@0: opus_int k; michael@0: opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ]; michael@0: opus_int32 *Anew_QA; michael@0: opus_int32 DC_resp = 0; michael@0: michael@0: Anew_QA = Atmp_QA[ order & 1 ]; michael@0: michael@0: /* Increase Q domain of the AR coefficients */ michael@0: for( k = 0; k < order; k++ ) { michael@0: DC_resp += (opus_int32)A_Q12[ k ]; michael@0: Anew_QA[ k ] = silk_LSHIFT32( (opus_int32)A_Q12[ k ], QA - 12 ); michael@0: } michael@0: /* If the DC is unstable, we don't even need to do the full calculations */ michael@0: if( DC_resp >= 4096 ) { michael@0: return 0; michael@0: } michael@0: return LPC_inverse_pred_gain_QA( Atmp_QA, order ); michael@0: } michael@0: michael@0: #ifdef FIXED_POINT michael@0: michael@0: /* For input in Q24 domain */ michael@0: opus_int32 silk_LPC_inverse_pred_gain_Q24( /* O Returns inverse prediction gain in energy domain, Q30 */ michael@0: const opus_int32 *A_Q24, /* I Prediction coefficients [order] */ michael@0: const opus_int order /* I Prediction order */ michael@0: ) michael@0: { michael@0: opus_int k; michael@0: opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ]; michael@0: opus_int32 *Anew_QA; michael@0: michael@0: Anew_QA = Atmp_QA[ order & 1 ]; michael@0: michael@0: /* Increase Q domain of the AR coefficients */ michael@0: for( k = 0; k < order; k++ ) { michael@0: Anew_QA[ k ] = silk_RSHIFT32( A_Q24[ k ], 24 - QA ); michael@0: } michael@0: michael@0: return LPC_inverse_pred_gain_QA( Atmp_QA, order ); michael@0: } michael@0: #endif