|
@@ -0,0 +1,152 @@
|
|
1
|
+#include "SKP_Silk_SigProc_FIX.h"
|
|
2
|
+#define QA 16
|
|
3
|
+#define A_LIMIT 65520
|
|
4
|
+
|
|
5
|
+SKP_int SKP_Silk_LPC_inverse_pred_gain( /* O: Returns 1 if unstable, otherwise 0 */
|
|
6
|
+ SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */
|
|
7
|
+ const SKP_int16 *A_Q12, /* I: Prediction coefficients, Q12 [order] */
|
|
8
|
+ const SKP_int order /* I: Prediction order */
|
|
9
|
+)
|
|
10
|
+{
|
|
11
|
+ SKP_int k, n, headrm;
|
|
12
|
+ SKP_int32 rc_Q31, rc_mult1_Q30, rc_mult2_Q16;
|
|
13
|
+ SKP_int32 Atmp_QA[ 2 ][ SigProc_MAX_ORDER_LPC ], tmp_QA;
|
|
14
|
+ SKP_int32 *Aold_QA, *Anew_QA;
|
|
15
|
+
|
|
16
|
+ Anew_QA = Atmp_QA[ order & 1 ];
|
|
17
|
+ /* Increase Q domain of the AR coefficients */
|
|
18
|
+ for( k = 0; k < order; k++ ) {
|
|
19
|
+ Anew_QA[ k ] = SKP_LSHIFT( (SKP_int32)A_Q12[ k ], QA - 12 );
|
|
20
|
+ }
|
|
21
|
+
|
|
22
|
+ *invGain_Q30 = ( 1 << 30 );
|
|
23
|
+ for( k = order - 1; k > 0; k-- ) {
|
|
24
|
+ /* Check for stability */
|
|
25
|
+ if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) {
|
|
26
|
+ return 1;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ /* Set RC equal to negated AR coef */
|
|
30
|
+ rc_Q31 = -SKP_LSHIFT( Anew_QA[ k ], 31 - QA );
|
|
31
|
+
|
|
32
|
+ /* rc_mult1_Q30 range: [ 1 : 2^30-1 ] */
|
|
33
|
+ rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 );
|
|
34
|
+ SKP_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */
|
|
35
|
+ SKP_assert( rc_mult1_Q30 < ( 1 << 30 ) );
|
|
36
|
+
|
|
37
|
+ /* rc_mult2_Q16 range: [ 2^16 : SKP_int32_MAX ] */
|
|
38
|
+ rc_mult2_Q16 = SKP_INVERSE32_varQ( rc_mult1_Q30, 46 ); /* 16 = 46 - 30 */
|
|
39
|
+
|
|
40
|
+ /* Update inverse gain */
|
|
41
|
+ /* invGain_Q30 range: [ 0 : 2^30 ] */
|
|
42
|
+ *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 );
|
|
43
|
+ SKP_assert( *invGain_Q30 >= 0 );
|
|
44
|
+ SKP_assert( *invGain_Q30 <= ( 1 << 30 ) );
|
|
45
|
+
|
|
46
|
+ /* Swap pointers */
|
|
47
|
+ Aold_QA = Anew_QA;
|
|
48
|
+ Anew_QA = Atmp_QA[ k & 1 ];
|
|
49
|
+
|
|
50
|
+ /* Update AR coefficient */
|
|
51
|
+ headrm = SKP_Silk_CLZ32( rc_mult2_Q16 ) - 1;
|
|
52
|
+ rc_mult2_Q16 = SKP_LSHIFT( rc_mult2_Q16, headrm ); /* Q: 16 + headrm */
|
|
53
|
+ for( n = 0; n < k; n++ ) {
|
|
54
|
+ tmp_QA = Aold_QA[ n ] - SKP_LSHIFT( SKP_SMMUL( Aold_QA[ k - n - 1 ], rc_Q31 ), 1 );
|
|
55
|
+ Anew_QA[ n ] = SKP_LSHIFT( SKP_SMMUL( tmp_QA, rc_mult2_Q16 ), 16 - headrm );
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ /* Check for stability */
|
|
60
|
+ if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) {
|
|
61
|
+ return 1;
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ /* Set RC equal to negated AR coef */
|
|
65
|
+ rc_Q31 = -SKP_LSHIFT( Anew_QA[ 0 ], 31 - QA );
|
|
66
|
+
|
|
67
|
+ /* Range: [ 1 : 2^30 ] */
|
|
68
|
+ rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 );
|
|
69
|
+
|
|
70
|
+ /* Update inverse gain */
|
|
71
|
+ /* Range: [ 0 : 2^30 ] */
|
|
72
|
+ *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 );
|
|
73
|
+ SKP_assert( *invGain_Q30 >= 0 );
|
|
74
|
+ SKP_assert( *invGain_Q30 <= 1<<30 );
|
|
75
|
+
|
|
76
|
+ return 0;
|
|
77
|
+}
|
|
78
|
+
|
|
79
|
+/* For input in Q13 domain */
|
|
80
|
+SKP_int SKP_Silk_LPC_inverse_pred_gain_Q13( /* O: Returns 1 if unstable, otherwise 0 */
|
|
81
|
+ SKP_int32 *invGain_Q30, /* O: Inverse prediction gain, Q30 energy domain */
|
|
82
|
+ const SKP_int16 *A_Q13, /* I: Prediction coefficients, Q13 [order] */
|
|
83
|
+ const SKP_int order /* I: Prediction order */
|
|
84
|
+)
|
|
85
|
+{
|
|
86
|
+ SKP_int k, n, headrm;
|
|
87
|
+ SKP_int32 rc_Q31, rc_mult1_Q30, rc_mult2_Q16;
|
|
88
|
+ SKP_int32 Atmp_QA[ 2 ][ SigProc_MAX_ORDER_LPC ], tmp_QA;
|
|
89
|
+ SKP_int32 *Aold_QA, *Anew_QA;
|
|
90
|
+
|
|
91
|
+ Anew_QA = Atmp_QA[ order & 1 ];
|
|
92
|
+ /* Increase Q domain of the AR coefficients */
|
|
93
|
+ for( k = 0; k < order; k++ ) {
|
|
94
|
+ Anew_QA[ k ] = SKP_LSHIFT( (SKP_int32)A_Q13[ k ], QA - 13 );
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ *invGain_Q30 = ( 1 << 30 );
|
|
98
|
+ for( k = order - 1; k > 0; k-- ) {
|
|
99
|
+ /* Check for stability */
|
|
100
|
+ if( ( Anew_QA[ k ] > A_LIMIT ) || ( Anew_QA[ k ] < -A_LIMIT ) ) {
|
|
101
|
+ return 1;
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ /* Set RC equal to negated AR coef */
|
|
105
|
+ rc_Q31 = -SKP_LSHIFT( Anew_QA[ k ], 31 - QA );
|
|
106
|
+
|
|
107
|
+ /* rc_mult1_Q30 range: [ 1 : 2^30-1 ] */
|
|
108
|
+ rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 );
|
|
109
|
+ SKP_assert( rc_mult1_Q30 > ( 1 << 15 ) ); /* reduce A_LIMIT if fails */
|
|
110
|
+ SKP_assert( rc_mult1_Q30 < ( 1 << 30 ) );
|
|
111
|
+
|
|
112
|
+ /* rc_mult2_Q16 range: [ 2^16 : SKP_int32_MAX ] */
|
|
113
|
+ rc_mult2_Q16 = SKP_INVERSE32_varQ( rc_mult1_Q30, 46 ); /* 16 = 46 - 30 */
|
|
114
|
+
|
|
115
|
+ /* Update inverse gain */
|
|
116
|
+ /* invGain_Q30 range: [ 0 : 2^30 ] */
|
|
117
|
+ *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 );
|
|
118
|
+ SKP_assert( *invGain_Q30 >= 0 );
|
|
119
|
+ SKP_assert( *invGain_Q30 <= 1<<30 );
|
|
120
|
+
|
|
121
|
+ /* Swap pointers */
|
|
122
|
+ Aold_QA = Anew_QA;
|
|
123
|
+ Anew_QA = Atmp_QA[ k & 1 ];
|
|
124
|
+
|
|
125
|
+ /* Update AR coefficient */
|
|
126
|
+ headrm = SKP_Silk_CLZ32( rc_mult2_Q16 ) - 1;
|
|
127
|
+ rc_mult2_Q16 = SKP_LSHIFT( rc_mult2_Q16, headrm ); /* Q: 16 + headrm */
|
|
128
|
+ for( n = 0; n < k; n++ ) {
|
|
129
|
+ tmp_QA = Aold_QA[ n ] - SKP_LSHIFT( SKP_SMMUL( Aold_QA[ k - n - 1 ], rc_Q31 ), 1 );
|
|
130
|
+ Anew_QA[ n ] = SKP_LSHIFT( SKP_SMMUL( tmp_QA, rc_mult2_Q16 ), 16 - headrm );
|
|
131
|
+ }
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ /* Check for stability */
|
|
135
|
+ if( ( Anew_QA[ 0 ] > A_LIMIT ) || ( Anew_QA[ 0 ] < -A_LIMIT ) ) {
|
|
136
|
+ return 1;
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ /* Set RC equal to negated AR coef */
|
|
140
|
+ rc_Q31 = -SKP_LSHIFT( Anew_QA[ 0 ], 31 - QA );
|
|
141
|
+
|
|
142
|
+ /* Range: [ 1 : 2^30 ] */
|
|
143
|
+ rc_mult1_Q30 = ( SKP_int32_MAX >> 1 ) - SKP_SMMUL( rc_Q31, rc_Q31 );
|
|
144
|
+
|
|
145
|
+ /* Update inverse gain */
|
|
146
|
+ /* Range: [ 0 : 2^30 ] */
|
|
147
|
+ *invGain_Q30 = SKP_LSHIFT( SKP_SMMUL( *invGain_Q30, rc_mult1_Q30 ), 2 );
|
|
148
|
+ SKP_assert( *invGain_Q30 >= 0 );
|
|
149
|
+ SKP_assert( *invGain_Q30 <= 1<<30 );
|
|
150
|
+
|
|
151
|
+ return 0;
|
|
152
|
+}
|