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 ***********************************************************************/
32 #include "SigProc_FIX.h"
35 #define A_LIMIT SILK_FIX_CONST( 0.99975, QA )
37 #define MUL32_FRAC_Q(a32, b32, Q) ((opus_int32)(silk_RSHIFT_ROUND64(silk_SMULL(a32, b32), Q)))
39 /* Compute inverse of LPC prediction gain, and */
40 /* test if LPC coefficients are stable (all poles within unit circle) */
41 static opus_int LPC_inverse_pred_gain_QA( /* O Returns 1 if unstable, otherwise 0 */
42 opus_int32 *invGain_Q30, /* O Inverse prediction gain, Q30 energy domain */
43 opus_int32 A_QA[ 2 ][ SILK_MAX_ORDER_LPC ], /* I Prediction coefficients */
44 const opus_int order /* I Prediction order */
47 opus_int k, n, mult2Q;
48 opus_int32 rc_Q31, rc_mult1_Q30, rc_mult2, tmp_QA;
49 opus_int32 *Aold_QA, *Anew_QA;
51 Anew_QA = A_QA[ order & 1 ];
53 *invGain_Q30 = ( 1 << 30 );
54 for( k = order - 1; k > 0; k-- ) {
55 /* Check for stability */
56 if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) {
60 /* Set RC equal to negated AR coef */
61 rc_Q31 = -silk_LSHIFT( Anew_QA[ k ], 31 - QA );
63 /* rc_mult1_Q30 range: [ 1 : 2^30 ] */
64 rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
65 silk_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */
66 silk_assert( rc_mult1_Q30 <= ( 1 << 30 ) );
68 /* rc_mult2 range: [ 2^30 : silk_int32_MAX ] */
69 mult2Q = 32 - silk_CLZ32( silk_abs( rc_mult1_Q30 ) );
70 rc_mult2 = silk_INVERSE32_varQ( rc_mult1_Q30, mult2Q + 30 );
72 /* Update inverse gain */
73 /* invGain_Q30 range: [ 0 : 2^30 ] */
74 *invGain_Q30 = silk_LSHIFT( silk_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 );
75 silk_assert( *invGain_Q30 >= 0 );
76 silk_assert( *invGain_Q30 <= ( 1 << 30 ) );
80 Anew_QA = A_QA[ k & 1 ];
82 /* Update AR coefficient */
83 for( n = 0; n < k; n++ ) {
84 tmp_QA = Aold_QA[ n ] - MUL32_FRAC_Q( Aold_QA[ k - n - 1 ], rc_Q31, 31 );
85 Anew_QA[ n ] = MUL32_FRAC_Q( tmp_QA, rc_mult2 , mult2Q );
89 /* Check for stability */
90 if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) {
94 /* Set RC equal to negated AR coef */
95 rc_Q31 = -silk_LSHIFT( Anew_QA[ 0 ], 31 - QA );
97 /* Range: [ 1 : 2^30 ] */
98 rc_mult1_Q30 = ( (opus_int32)1 << 30 ) - silk_SMMUL( rc_Q31, rc_Q31 );
100 /* Update inverse gain */
101 /* Range: [ 0 : 2^30 ] */
102 *invGain_Q30 = silk_LSHIFT( silk_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 );
103 silk_assert( *invGain_Q30 >= 0 );
104 silk_assert( *invGain_Q30 <= 1<<30 );
109 /* For input in Q12 domain */
110 opus_int silk_LPC_inverse_pred_gain( /* O Returns 1 if unstable, otherwise 0 */
111 opus_int32 *invGain_Q30, /* O Inverse prediction gain, Q30 energy domain */
112 const opus_int16 *A_Q12, /* I Prediction coefficients, Q12 [order] */
113 const opus_int order /* I Prediction order */
117 opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
119 opus_int32 DC_resp=0;
121 Anew_QA = Atmp_QA[ order & 1 ];
123 /* Increase Q domain of the AR coefficients */
124 for( k = 0; k < order; k++ ) {
125 DC_resp += (opus_int32)A_Q12[ k ];
126 Anew_QA[ k ] = silk_LSHIFT( (opus_int32)A_Q12[ k ], QA - 12 );
128 /* If the DC is unstable, we don't even need to do the full calculations */
129 if( DC_resp >= 4096 ) {
132 return LPC_inverse_pred_gain_QA( invGain_Q30, Atmp_QA, order );
135 /* For input in Q24 domain */
136 opus_int silk_LPC_inverse_pred_gain_Q24( /* O Returns 1 if unstable, otherwise 0 */
137 opus_int32 *invGain_Q30, /* O Inverse prediction gain, Q30 energy domain */
138 const opus_int32 *A_Q24, /* I Prediction coefficients, Q24 [order] */
139 const opus_int order /* I Prediction order */
143 opus_int32 Atmp_QA[ 2 ][ SILK_MAX_ORDER_LPC ];
146 Anew_QA = Atmp_QA[ order & 1 ];
148 /* Increase Q domain of the AR coefficients */
149 for( k = 0; k < order; k++ ) {
150 Anew_QA[ k ] = silk_RSHIFT( A_Q24[ k ], 24 - QA );
153 return LPC_inverse_pred_gain_QA( invGain_Q30, Atmp_QA, order );