Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: webrtc/common_audio/signal_processing/levinson_durbin.c

Issue 2500953003: Avoid left-shifting negative values in a number of places (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/common_audio/signal_processing/vector_scaling_operations.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 25 matching lines...) Expand all
36 int16_t tmp_hi, tmp_low; 36 int16_t tmp_hi, tmp_low;
37 int32_t temp1W32, temp2W32, temp3W32; 37 int32_t temp1W32, temp2W32, temp3W32;
38 int16_t norm; 38 int16_t norm;
39 39
40 // Normalize the autocorrelation R[0]...R[order+1] 40 // Normalize the autocorrelation R[0]...R[order+1]
41 41
42 norm = WebRtcSpl_NormW32(R[0]); 42 norm = WebRtcSpl_NormW32(R[0]);
43 43
44 for (i = 0; i <= order; ++i) 44 for (i = 0; i <= order; ++i)
45 { 45 {
46 temp1W32 = WEBRTC_SPL_LSHIFT_W32(R[i], norm); 46 temp1W32 = R[i] * (1 << norm);
47 // Put R in hi and low format 47 // Put R in hi and low format
48 R_hi[i] = (int16_t)(temp1W32 >> 16); 48 R_hi[i] = (int16_t)(temp1W32 >> 16);
49 R_low[i] = (int16_t)((temp1W32 - ((int32_t)R_hi[i] << 16)) >> 1); 49 R_low[i] = (int16_t)((temp1W32 - ((int32_t)R_hi[i] * 65536)) >> 1);
50 } 50 }
51 51
52 // K = A[1] = -R[1] / R[0] 52 // K = A[1] = -R[1] / R[0]
53 53
54 temp2W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)R_hi[1],16) 54 temp2W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)R_hi[1],16)
55 + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[1],1); // R[1] in Q31 55 + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[1],1); // R[1] in Q31
56 temp3W32 = WEBRTC_SPL_ABS_W32(temp2W32); // abs R[1] 56 temp3W32 = WEBRTC_SPL_ABS_W32(temp2W32); // abs R[1]
57 temp1W32 = WebRtcSpl_DivW32HiLow(temp3W32, R_hi[0], R_low[0]); // abs(R[1])/ R[0] in Q31 57 temp1W32 = WebRtcSpl_DivW32HiLow(temp3W32, R_hi[0], R_low[0]); // abs(R[1])/ R[0] in Q31
58 // Put back the sign on R[1] 58 // Put back the sign on R[1]
59 if (temp2W32 > 0) 59 if (temp2W32 > 0)
60 { 60 {
61 temp1W32 = -temp1W32; 61 temp1W32 = -temp1W32;
62 } 62 }
63 63
64 // Put K in hi and low format 64 // Put K in hi and low format
65 K_hi = (int16_t)(temp1W32 >> 16); 65 K_hi = (int16_t)(temp1W32 >> 16);
66 K_low = (int16_t)((temp1W32 - ((int32_t)K_hi << 16)) >> 1); 66 K_low = (int16_t)((temp1W32 - ((int32_t)K_hi * 65536)) >> 1);
67 67
68 // Store first reflection coefficient 68 // Store first reflection coefficient
69 K[0] = K_hi; 69 K[0] = K_hi;
70 70
71 temp1W32 >>= 4; // A[1] in Q27. 71 temp1W32 >>= 4; // A[1] in Q27.
72 72
73 // Put A[1] in hi and low format 73 // Put A[1] in hi and low format
74 A_hi[1] = (int16_t)(temp1W32 >> 16); 74 A_hi[1] = (int16_t)(temp1W32 >> 16);
75 A_low[1] = (int16_t)((temp1W32 - ((int32_t)A_hi[1] << 16)) >> 1); 75 A_low[1] = (int16_t)((temp1W32 - ((int32_t)A_hi[1] * 65536)) >> 1);
76 76
77 // Alpha = R[0] * (1-K^2) 77 // Alpha = R[0] * (1-K^2)
78 78
79 temp1W32 = ((K_hi * K_low >> 14) + K_hi * K_hi) << 1; // = k^2 in Q31 79 temp1W32 = ((K_hi * K_low >> 14) + K_hi * K_hi) << 1; // = k^2 in Q31
80 80
81 temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0 81 temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0
82 temp1W32 = (int32_t)0x7fffffffL - temp1W32; // temp1W32 = (1 - K[0]*K[0]) in Q31 82 temp1W32 = (int32_t)0x7fffffffL - temp1W32; // temp1W32 = (1 - K[0]*K[0]) in Q31
83 83
84 // Store temp1W32 = 1 - K[0]*K[0] on hi and low format 84 // Store temp1W32 = 1 - K[0]*K[0] on hi and low format
85 tmp_hi = (int16_t)(temp1W32 >> 16); 85 tmp_hi = (int16_t)(temp1W32 >> 16);
(...skipping 19 matching lines...) Expand all
105 / 105 /
106 ---- 106 ----
107 j=1..i-1 107 j=1..i-1
108 */ 108 */
109 109
110 temp1W32 = 0; 110 temp1W32 = 0;
111 111
112 for (j = 1; j < i; j++) 112 for (j = 1; j < i; j++)
113 { 113 {
114 // temp1W32 is in Q31 114 // temp1W32 is in Q31
115 temp1W32 += (R_hi[j] * A_hi[i - j] << 1) + 115 temp1W32 += (R_hi[j] * A_hi[i - j] * 2) +
116 (((R_hi[j] * A_low[i - j] >> 15) + 116 (((R_hi[j] * A_low[i - j] >> 15) +
117 (R_low[j] * A_hi[i - j] >> 15)) << 1); 117 (R_low[j] * A_hi[i - j] >> 15)) * 2);
118 } 118 }
119 119
120 temp1W32 = WEBRTC_SPL_LSHIFT_W32(temp1W32, 4); 120 temp1W32 = temp1W32 * 16;
121 temp1W32 += (WEBRTC_SPL_LSHIFT_W32((int32_t)R_hi[i], 16) 121 temp1W32 += ((int32_t)R_hi[i] * 65536)
122 + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[i], 1)); 122 + WEBRTC_SPL_LSHIFT_W32((int32_t)R_low[i], 1);
123 123
124 // K = -temp1W32 / Alpha 124 // K = -temp1W32 / Alpha
125 temp2W32 = WEBRTC_SPL_ABS_W32(temp1W32); // abs(temp1W32) 125 temp2W32 = WEBRTC_SPL_ABS_W32(temp1W32); // abs(temp1W32)
126 temp3W32 = WebRtcSpl_DivW32HiLow(temp2W32, Alpha_hi, Alpha_low); // abs( temp1W32)/Alpha 126 temp3W32 = WebRtcSpl_DivW32HiLow(temp2W32, Alpha_hi, Alpha_low); // abs( temp1W32)/Alpha
127 127
128 // Put the sign of temp1W32 back again 128 // Put the sign of temp1W32 back again
129 if (temp1W32 > 0) 129 if (temp1W32 > 0)
130 { 130 {
131 temp3W32 = -temp3W32; 131 temp3W32 = -temp3W32;
132 } 132 }
133 133
134 // Use the Alpha shifts from earlier to de-normalize 134 // Use the Alpha shifts from earlier to de-normalize
135 norm = WebRtcSpl_NormW32(temp3W32); 135 norm = WebRtcSpl_NormW32(temp3W32);
136 if ((Alpha_exp <= norm) || (temp3W32 == 0)) 136 if ((Alpha_exp <= norm) || (temp3W32 == 0))
137 { 137 {
138 temp3W32 = WEBRTC_SPL_LSHIFT_W32(temp3W32, Alpha_exp); 138 temp3W32 = temp3W32 * (1 << Alpha_exp);
139 } else 139 } else
140 { 140 {
141 if (temp3W32 > 0) 141 if (temp3W32 > 0)
142 { 142 {
143 temp3W32 = (int32_t)0x7fffffffL; 143 temp3W32 = (int32_t)0x7fffffffL;
144 } else 144 } else
145 { 145 {
146 temp3W32 = (int32_t)0x80000000L; 146 temp3W32 = (int32_t)0x80000000L;
147 } 147 }
148 } 148 }
149 149
150 // Put K on hi and low format 150 // Put K on hi and low format
151 K_hi = (int16_t)(temp3W32 >> 16); 151 K_hi = (int16_t)(temp3W32 >> 16);
152 K_low = (int16_t)((temp3W32 - ((int32_t)K_hi << 16)) >> 1); 152 K_low = (int16_t)((temp3W32 - ((int32_t)K_hi * 65536)) >> 1);
153 153
154 // Store Reflection coefficient in Q15 154 // Store Reflection coefficient in Q15
155 K[i - 1] = K_hi; 155 K[i - 1] = K_hi;
156 156
157 // Test for unstable filter. 157 // Test for unstable filter.
158 // If unstable return 0 and let the user decide what to do in that case 158 // If unstable return 0 and let the user decide what to do in that case
159 159
160 if ((int32_t)WEBRTC_SPL_ABS_W16(K_hi) > (int32_t)32750) 160 if ((int32_t)WEBRTC_SPL_ABS_W16(K_hi) > (int32_t)32750)
161 { 161 {
162 return 0; // Unstable filter 162 return 0; // Unstable filter
163 } 163 }
164 164
165 /* 165 /*
166 Compute updated LPC coefficient: Anew[i] 166 Compute updated LPC coefficient: Anew[i]
167 Anew[j]= A[j] + K*A[i-j] for j=1..i-1 167 Anew[j]= A[j] + K*A[i-j] for j=1..i-1
168 Anew[i]= K 168 Anew[i]= K
169 */ 169 */
170 170
171 for (j = 1; j < i; j++) 171 for (j = 1; j < i; j++)
172 { 172 {
173 // temp1W32 = A[j] in Q27 173 // temp1W32 = A[j] in Q27
174 temp1W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)A_hi[j],16) 174 temp1W32 = (int32_t)A_hi[j] * 65536
175 + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[j],1); 175 + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[j],1);
176 176
177 // temp1W32 += K*A[i-j] in Q27 177 // temp1W32 += K*A[i-j] in Q27
178 temp1W32 += (K_hi * A_hi[i - j] + (K_hi * A_low[i - j] >> 15) + 178 temp1W32 += (K_hi * A_hi[i - j] + (K_hi * A_low[i - j] >> 15) +
179 (K_low * A_hi[i - j] >> 15)) << 1; 179 (K_low * A_hi[i - j] >> 15)) * 2;
180 180
181 // Put Anew in hi and low format 181 // Put Anew in hi and low format
182 A_upd_hi[j] = (int16_t)(temp1W32 >> 16); 182 A_upd_hi[j] = (int16_t)(temp1W32 >> 16);
183 A_upd_low[j] = (int16_t)( 183 A_upd_low[j] = (int16_t)(
184 (temp1W32 - ((int32_t)A_upd_hi[j] << 16)) >> 1); 184 (temp1W32 - ((int32_t)A_upd_hi[j] * 65536)) >> 1);
185 } 185 }
186 186
187 // temp3W32 = K in Q27 (Convert from Q31 to Q27) 187 // temp3W32 = K in Q27 (Convert from Q31 to Q27)
188 temp3W32 >>= 4; 188 temp3W32 >>= 4;
189 189
190 // Store Anew in hi and low format 190 // Store Anew in hi and low format
191 A_upd_hi[i] = (int16_t)(temp3W32 >> 16); 191 A_upd_hi[i] = (int16_t)(temp3W32 >> 16);
192 A_upd_low[i] = (int16_t)( 192 A_upd_low[i] = (int16_t)(
193 (temp3W32 - ((int32_t)A_upd_hi[i] << 16)) >> 1); 193 (temp3W32 - ((int32_t)A_upd_hi[i] * 65536)) >> 1);
194 194
195 // Alpha = Alpha * (1-K^2) 195 // Alpha = Alpha * (1-K^2)
196 196
197 temp1W32 = ((K_hi * K_low >> 14) + K_hi * K_hi) << 1; // K*K in Q31 197 temp1W32 = ((K_hi * K_low >> 14) + K_hi * K_hi) << 1; // K*K in Q31
198 198
199 temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0 199 temp1W32 = WEBRTC_SPL_ABS_W32(temp1W32); // Guard against <0
200 temp1W32 = (int32_t)0x7fffffffL - temp1W32; // 1 - K*K in Q31 200 temp1W32 = (int32_t)0x7fffffffL - temp1W32; // 1 - K*K in Q31
201 201
202 // Convert 1- K^2 in hi and low format 202 // Convert 1- K^2 in hi and low format
203 tmp_hi = (int16_t)(temp1W32 >> 16); 203 tmp_hi = (int16_t)(temp1W32 >> 16);
(...skipping 26 matching lines...) Expand all
230 /* 230 /*
231 Set A[0] to 1.0 and store the A[i] i=1...order in Q12 231 Set A[0] to 1.0 and store the A[i] i=1...order in Q12
232 (Convert from Q27 and use rounding) 232 (Convert from Q27 and use rounding)
233 */ 233 */
234 234
235 A[0] = 4096; 235 A[0] = 4096;
236 236
237 for (i = 1; i <= order; i++) 237 for (i = 1; i <= order; i++)
238 { 238 {
239 // temp1W32 in Q27 239 // temp1W32 in Q27
240 temp1W32 = WEBRTC_SPL_LSHIFT_W32((int32_t)A_hi[i], 16) 240 temp1W32 = (int32_t)A_hi[i] * 65536
241 + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[i], 1); 241 + WEBRTC_SPL_LSHIFT_W32((int32_t)A_low[i], 1);
242 // Round and store upper word 242 // Round and store upper word
243 A[i] = (int16_t)(((temp1W32 << 1) + 32768) >> 16); 243 A[i] = (int16_t)(((temp1W32 * 2) + 32768) >> 16);
244 } 244 }
245 return 1; // Stable filters 245 return 1; // Stable filters
246 } 246 }
OLDNEW
« no previous file with comments | « no previous file | webrtc/common_audio/signal_processing/vector_scaling_operations.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698