comparison Plugins/Input/aac/libfaad2/sbr_hfgen.c @ 1010:29feaace84d0 trunk

[svn] - synchronize audacious-faad with FAAD2 CVS.
author nenolod
date Mon, 08 May 2006 06:56:47 -0700
parents 0a2ad94e8607
children 1e6c0a3f2d15
comparison
equal deleted inserted replaced
1009:1008da26c12d 1010:29feaace84d0
20 ** forbidden. 20 ** forbidden.
21 ** 21 **
22 ** Commercial non-GPL licensing of this software is possible. 22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 ** 24 **
25 ** $Id: sbr_hfgen.c,v 1.22 2004/09/08 09:43:11 gcp Exp $ 25 ** $Id: sbr_hfgen.c,v 1.12 2004/01/05 14:05:12 menno Exp $
26 **/ 26 **/
27 27
28 /* High Frequency generation */ 28 /* High Frequency generation */
29 29
30 #include "common.h" 30 #include "common.h"
36 #include "sbr_hfgen.h" 36 #include "sbr_hfgen.h"
37 #include "sbr_fbt.h" 37 #include "sbr_fbt.h"
38 38
39 39
40 /* static function declarations */ 40 /* static function declarations */
41 #ifdef SBR_LOW_POWER 41 static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32],
42 static void calc_prediction_coef_lp(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64], 42 complex_t *alpha_0, complex_t *alpha_1
43 complex_t *alpha_0, complex_t *alpha_1, real_t *rxx); 43 #ifdef SBR_LOW_POWER
44 , real_t *rxx
45 #endif
46 );
47 #ifdef SBR_LOW_POWER
44 static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg); 48 static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg);
45 #else
46 static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64],
47 complex_t *alpha_0, complex_t *alpha_1, uint8_t k);
48 #endif 49 #endif
49 static void calc_chirp_factors(sbr_info *sbr, uint8_t ch); 50 static void calc_chirp_factors(sbr_info *sbr, uint8_t ch);
50 static void patch_construction(sbr_info *sbr); 51 static void patch_construction(sbr_info *sbr);
51 52
52 53
53 void hf_generation(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64], 54 void hf_generation(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32],
54 qmf_t Xhigh[MAX_NTSRHFG][64] 55 qmf_t Xhigh[MAX_NTSRHFG][64]
55 #ifdef SBR_LOW_POWER 56 #ifdef SBR_LOW_POWER
56 ,real_t *deg 57 ,real_t *deg
57 #endif 58 #endif
58 ,uint8_t ch) 59 ,uint8_t ch)
65 66
66 uint8_t offset = sbr->tHFAdj; 67 uint8_t offset = sbr->tHFAdj;
67 uint8_t first = sbr->t_E[ch][0]; 68 uint8_t first = sbr->t_E[ch][0];
68 uint8_t last = sbr->t_E[ch][sbr->L_E[ch]]; 69 uint8_t last = sbr->t_E[ch][sbr->L_E[ch]];
69 70
71 // printf("%d %d\n", first, last);
72
70 calc_chirp_factors(sbr, ch); 73 calc_chirp_factors(sbr, ch);
71 74
72 #ifdef SBR_LOW_POWER 75 for (i = first; i < last; i++)
73 memset(deg, 0, 64*sizeof(real_t)); 76 {
74 #endif 77 memset(Xhigh[i + offset], 0, 64 * sizeof(qmf_t));
78 }
75 79
76 if ((ch == 0) && (sbr->Reset)) 80 if ((ch == 0) && (sbr->Reset))
77 patch_construction(sbr); 81 patch_construction(sbr);
78 82
79 /* calculate the prediction coefficients */ 83 /* calculate the prediction coefficients */
80 #ifdef SBR_LOW_POWER 84 calc_prediction_coef(sbr, Xlow, alpha_0, alpha_1
81 calc_prediction_coef_lp(sbr, Xlow, alpha_0, alpha_1, rxx); 85 #ifdef SBR_LOW_POWER
86 , rxx
87 #endif
88 );
89
90 #ifdef SBR_LOW_POWER
82 calc_aliasing_degree(sbr, rxx, deg); 91 calc_aliasing_degree(sbr, rxx, deg);
83 #endif 92 #endif
84 93
85 /* actual HF generation */ 94 /* actual HF generation */
86 for (i = 0; i < sbr->noPatches; i++) 95 for (i = 0; i < sbr->noPatches; i++)
87 { 96 {
88 for (x = 0; x < sbr->patchNoSubbands[i]; x++) 97 for (x = 0; x < sbr->patchNoSubbands[i]; x++)
89 { 98 {
90 real_t a0_r, a0_i, a1_r, a1_i; 99 complex_t a0, a1;
91 real_t bw, bw2; 100 real_t bw, bw2;
92 uint8_t q, p, k, g; 101 uint8_t q, p, k, g;
93 102
94 /* find the low and high band for patching */ 103 /* find the low and high band for patching */
95 k = sbr->kx + x; 104 k = sbr->kx + x;
113 122
114 /* do the patching */ 123 /* do the patching */
115 /* with or without filtering */ 124 /* with or without filtering */
116 if (bw2 > 0) 125 if (bw2 > 0)
117 { 126 {
118 real_t temp1_r, temp2_r, temp3_r; 127 RE(a0) = MUL_C(RE(alpha_0[p]), bw);
128 RE(a1) = MUL_C(RE(alpha_1[p]), bw2);
119 #ifndef SBR_LOW_POWER 129 #ifndef SBR_LOW_POWER
120 real_t temp1_i, temp2_i, temp3_i; 130 IM(a0) = MUL_C(IM(alpha_0[p]), bw);
121 calc_prediction_coef(sbr, Xlow, alpha_0, alpha_1, p); 131 IM(a1) = MUL_C(IM(alpha_1[p]), bw2);
122 #endif 132 #endif
123 133
124 a0_r = MUL_C(RE(alpha_0[p]), bw);
125 a1_r = MUL_C(RE(alpha_1[p]), bw2);
126 #ifndef SBR_LOW_POWER
127 a0_i = MUL_C(IM(alpha_0[p]), bw);
128 a1_i = MUL_C(IM(alpha_1[p]), bw2);
129 #endif
130
131 temp2_r = QMF_RE(Xlow[first - 2 + offset][p]);
132 temp3_r = QMF_RE(Xlow[first - 1 + offset][p]);
133 #ifndef SBR_LOW_POWER
134 temp2_i = QMF_IM(Xlow[first - 2 + offset][p]);
135 temp3_i = QMF_IM(Xlow[first - 1 + offset][p]);
136 #endif
137 for (l = first; l < last; l++) 134 for (l = first; l < last; l++)
138 { 135 {
139 temp1_r = temp2_r; 136 QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]);
140 temp2_r = temp3_r;
141 temp3_r = QMF_RE(Xlow[l + offset][p]);
142 #ifndef SBR_LOW_POWER 137 #ifndef SBR_LOW_POWER
143 temp1_i = temp2_i; 138 QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]);
144 temp2_i = temp3_i; 139 #endif
145 temp3_i = QMF_IM(Xlow[l + offset][p]); 140
146 #endif 141 #ifdef SBR_LOW_POWER
147 142 QMF_RE(Xhigh[l + offset][k]) += (
148 #ifdef SBR_LOW_POWER 143 MUL_R(RE(a0), QMF_RE(Xlow[l - 1 + offset][p])) +
149 QMF_RE(Xhigh[l + offset][k]) = 144 MUL_R(RE(a1), QMF_RE(Xlow[l - 2 + offset][p])));
150 temp3_r
151 +(MUL_R(a0_r, temp2_r) +
152 MUL_R(a1_r, temp1_r));
153 #else 145 #else
154 QMF_RE(Xhigh[l + offset][k]) = 146 QMF_RE(Xhigh[l + offset][k]) += (
155 temp3_r 147 RE(a0) * QMF_RE(Xlow[l - 1 + offset][p]) -
156 +(MUL_R(a0_r, temp2_r) - 148 IM(a0) * QMF_IM(Xlow[l - 1 + offset][p]) +
157 MUL_R(a0_i, temp2_i) + 149 RE(a1) * QMF_RE(Xlow[l - 2 + offset][p]) -
158 MUL_R(a1_r, temp1_r) - 150 IM(a1) * QMF_IM(Xlow[l - 2 + offset][p]));
159 MUL_R(a1_i, temp1_i)); 151 QMF_IM(Xhigh[l + offset][k]) += (
160 QMF_IM(Xhigh[l + offset][k]) = 152 IM(a0) * QMF_RE(Xlow[l - 1 + offset][p]) +
161 temp3_i 153 RE(a0) * QMF_IM(Xlow[l - 1 + offset][p]) +
162 +(MUL_R(a0_i, temp2_r) + 154 IM(a1) * QMF_RE(Xlow[l - 2 + offset][p]) +
163 MUL_R(a0_r, temp2_i) + 155 RE(a1) * QMF_IM(Xlow[l - 2 + offset][p]));
164 MUL_R(a1_i, temp1_r) +
165 MUL_R(a1_r, temp1_i));
166 #endif 156 #endif
167 } 157 }
168 } else { 158 } else {
169 for (l = first; l < last; l++) 159 for (l = first; l < last; l++)
170 { 160 {
191 complex_t r12; 181 complex_t r12;
192 complex_t r22; 182 complex_t r22;
193 real_t det; 183 real_t det;
194 } acorr_coef; 184 } acorr_coef;
195 185
186 #define SBR_ABS(A) ((A) < 0) ? -(A) : (A)
187
196 #ifdef SBR_LOW_POWER 188 #ifdef SBR_LOW_POWER
197 static void auto_correlation(sbr_info *sbr, acorr_coef *ac, 189 static void auto_correlation(sbr_info *sbr, acorr_coef *ac,
198 qmf_t buffer[MAX_NTSRHFG][64], 190 qmf_t buffer[MAX_NTSRHFG][32],
199 uint8_t bd, uint8_t len) 191 uint8_t bd, uint8_t len)
200 { 192 {
201 real_t r01 = 0, r02 = 0, r11 = 0; 193 real_t r01 = 0, r02 = 0, r11 = 0;
202 int8_t j; 194 int8_t j;
203 uint8_t offset = sbr->tHFAdj; 195 uint8_t offset = sbr->tHFAdj;
204 #ifdef FIXED_POINT
205 const real_t rel = FRAC_CONST(0.999999); // 1 / (1 + 1e-6f);
206 uint32_t maxi = 0;
207 uint32_t pow2, exp;
208 #else
209 const real_t rel = 1 / (1 + 1e-6f); 196 const real_t rel = 1 / (1 + 1e-6f);
210 #endif 197
211 198
212
213 #ifdef FIXED_POINT
214 mask = 0;
215
216 for (j = (offset-2); j < (len + offset); j++)
217 {
218 real_t x;
219 x = QMF_RE(buffer[j][bd])>>REAL_BITS;
220 mask |= x ^ (x >> 31);
221 }
222
223 exp = wl_min_lzc(mask);
224
225 /* improves accuracy */
226 if (exp > 0)
227 exp -= 1;
228
229 for (j = offset; j < len + offset; j++)
230 {
231 real_t buf_j = ((QMF_RE(buffer[j][bd])+(1<<(exp-1)))>>exp);
232 real_t buf_j_1 = ((QMF_RE(buffer[j-1][bd])+(1<<(exp-1)))>>exp);
233 real_t buf_j_2 = ((QMF_RE(buffer[j-2][bd])+(1<<(exp-1)))>>exp);
234
235 /* normalisation with rounding */
236 r01 += MUL_R(buf_j, buf_j_1);
237 r02 += MUL_R(buf_j, buf_j_2);
238 r11 += MUL_R(buf_j_1, buf_j_1);
239 }
240 RE(ac->r12) = r01 -
241 MUL_R(((QMF_RE(buffer[len+offset-1][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[len+offset-2][bd])+(1<<(exp-1)))>>exp)) +
242 MUL_R(((QMF_RE(buffer[offset-1][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[offset-2][bd])+(1<<(exp-1)))>>exp));
243 RE(ac->r22) = r11 -
244 MUL_R(((QMF_RE(buffer[len+offset-2][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[len+offset-2][bd])+(1<<(exp-1)))>>exp)) +
245 MUL_R(((QMF_RE(buffer[offset-2][bd])+(1<<(exp-1)))>>exp), ((QMF_RE(buffer[offset-2][bd])+(1<<(exp-1)))>>exp));
246 #else
247 for (j = offset; j < len + offset; j++) 199 for (j = offset; j < len + offset; j++)
248 { 200 {
249 r01 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]); 201 r01 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]);
250 r02 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]); 202 r02 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]);
251 r11 += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]); 203 r11 += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]);
254 QMF_RE(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) + 206 QMF_RE(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) +
255 QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]); 207 QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]);
256 RE(ac->r22) = r11 - 208 RE(ac->r22) = r11 -
257 QMF_RE(buffer[len+offset-2][bd]) * QMF_RE(buffer[len+offset-2][bd]) + 209 QMF_RE(buffer[len+offset-2][bd]) * QMF_RE(buffer[len+offset-2][bd]) +
258 QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]); 210 QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]);
259 #endif
260 RE(ac->r01) = r01; 211 RE(ac->r01) = r01;
261 RE(ac->r02) = r02; 212 RE(ac->r02) = r02;
262 RE(ac->r11) = r11; 213 RE(ac->r11) = r11;
263 214
264 ac->det = MUL_R(RE(ac->r11), RE(ac->r22)) - MUL_F(MUL_R(RE(ac->r12), RE(ac->r12)), rel); 215 ac->det = MUL_R(RE(ac->r11), RE(ac->r22)) - MUL_C(MUL_R(RE(ac->r12), RE(ac->r12)), rel);
265 } 216 }
266 #else 217 #else
267 static void auto_correlation(sbr_info *sbr, acorr_coef *ac, qmf_t buffer[MAX_NTSRHFG][64], 218 static void auto_correlation(sbr_info *sbr, acorr_coef *ac, qmf_t buffer[MAX_NTSRHFG][32],
268 uint8_t bd, uint8_t len) 219 uint8_t bd, uint8_t len)
269 { 220 {
270 real_t r01r = 0, r01i = 0, r02r = 0, r02i = 0, r11r = 0; 221 real_t r01r = 0, r01i = 0, r02r = 0, r02i = 0, r11r = 0;
271 real_t temp1_r, temp1_i, temp2_r, temp2_i, temp3_r, temp3_i, temp4_r, temp4_i, temp5_r, temp5_i;
272 #ifdef FIXED_POINT
273 const real_t rel = FRAC_CONST(0.999999); // 1 / (1 + 1e-6f);
274 uint32_t mask, exp;
275 real_t pow2_to_exp;
276 #else
277 const real_t rel = 1 / (1 + 1e-6f); 222 const real_t rel = 1 / (1 + 1e-6f);
278 #endif
279 int8_t j; 223 int8_t j;
280 uint8_t offset = sbr->tHFAdj; 224 uint8_t offset = sbr->tHFAdj;
281 225
282 #ifdef FIXED_POINT
283 mask = 0;
284
285 for (j = (offset-2); j < (len + offset); j++)
286 {
287 real_t x;
288 x = QMF_RE(buffer[j][bd])>>REAL_BITS;
289 mask |= x ^ (x >> 31);
290 x = QMF_IM(buffer[j][bd])>>REAL_BITS;
291 mask |= x ^ (x >> 31);
292 }
293
294 exp = wl_min_lzc(mask);
295
296 /* improves accuracy */
297 if (exp > 0)
298 exp -= 1;
299
300 pow2_to_exp = 1<<(exp-1);
301
302 temp2_r = (QMF_RE(buffer[offset-2][bd]) + pow2_to_exp) >> exp;
303 temp2_i = (QMF_IM(buffer[offset-2][bd]) + pow2_to_exp) >> exp;
304 temp3_r = (QMF_RE(buffer[offset-1][bd]) + pow2_to_exp) >> exp;
305 temp3_i = (QMF_IM(buffer[offset-1][bd]) + pow2_to_exp) >> exp;
306 // Save these because they are needed after loop
307 temp4_r = temp2_r;
308 temp4_i = temp2_i;
309 temp5_r = temp3_r;
310 temp5_i = temp3_i;
311 226
312 for (j = offset; j < len + offset; j++) 227 for (j = offset; j < len + offset; j++)
313 { 228 {
314 temp1_r = temp2_r; // temp1_r = (QMF_RE(buffer[offset-2][bd] + (1<<(exp-1))) >> exp; 229 r01r += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]) +
315 temp1_i = temp2_i; // temp1_i = (QMF_IM(buffer[offset-2][bd] + (1<<(exp-1))) >> exp; 230 QMF_IM(buffer[j][bd]) * QMF_IM(buffer[j-1][bd]);
316 temp2_r = temp3_r; // temp2_r = (QMF_RE(buffer[offset-1][bd] + (1<<(exp-1))) >> exp; 231 r01i += QMF_IM(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]) -
317 temp2_i = temp3_i; // temp2_i = (QMF_IM(buffer[offset-1][bd] + (1<<(exp-1))) >> exp; 232 QMF_RE(buffer[j][bd]) * QMF_IM(buffer[j-1][bd]);
318 temp3_r = (QMF_RE(buffer[j][bd]) + pow2_to_exp) >> exp; 233 r02r += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]) +
319 temp3_i = (QMF_IM(buffer[j][bd]) + pow2_to_exp) >> exp; 234 QMF_IM(buffer[j][bd]) * QMF_IM(buffer[j-2][bd]);
320 r01r += MUL_R(temp3_r, temp2_r) + MUL_R(temp3_i, temp2_i); 235 r02i += QMF_IM(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]) -
321 r01i += MUL_R(temp3_i, temp2_r) - MUL_R(temp3_r, temp2_i); 236 QMF_RE(buffer[j][bd]) * QMF_IM(buffer[j-2][bd]);
322 r02r += MUL_R(temp3_r, temp1_r) + MUL_R(temp3_i, temp1_i); 237 r11r += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]) +
323 r02i += MUL_R(temp3_i, temp1_r) - MUL_R(temp3_r, temp1_i); 238 QMF_IM(buffer[j-1][bd]) * QMF_IM(buffer[j-1][bd]);
324 r11r += MUL_R(temp2_r, temp2_r) + MUL_R(temp2_i, temp2_i); 239 }
325 }
326
327 // These are actual values in temporary variable at this point
328 // temp1_r = (QMF_RE(buffer[len+offset-1-2][bd] + (1<<(exp-1))) >> exp;
329 // temp1_i = (QMF_IM(buffer[len+offset-1-2][bd] + (1<<(exp-1))) >> exp;
330 // temp2_r = (QMF_RE(buffer[len+offset-1-1][bd] + (1<<(exp-1))) >> exp;
331 // temp2_i = (QMF_IM(buffer[len+offset-1-1][bd] + (1<<(exp-1))) >> exp;
332 // temp3_r = (QMF_RE(buffer[len+offset-1][bd]) + (1<<(exp-1))) >> exp;
333 // temp3_i = (QMF_IM(buffer[len+offset-1][bd]) + (1<<(exp-1))) >> exp;
334 // temp4_r = (QMF_RE(buffer[offset-2][bd]) + (1<<(exp-1))) >> exp;
335 // temp4_i = (QMF_IM(buffer[offset-2][bd]) + (1<<(exp-1))) >> exp;
336 // temp5_r = (QMF_RE(buffer[offset-1][bd]) + (1<<(exp-1))) >> exp;
337 // temp5_i = (QMF_IM(buffer[offset-1][bd]) + (1<<(exp-1))) >> exp;
338
339 RE(ac->r12) = r01r -
340 (MUL_R(temp3_r, temp2_r) + MUL_R(temp3_i, temp2_i)) +
341 (MUL_R(temp5_r, temp4_r) + MUL_R(temp5_i, temp4_i));
342 IM(ac->r12) = r01i -
343 (MUL_R(temp3_i, temp2_r) - MUL_R(temp3_r, temp2_i)) +
344 (MUL_R(temp5_i, temp4_r) - MUL_R(temp5_r, temp4_i));
345 RE(ac->r22) = r11r -
346 (MUL_R(temp2_r, temp2_r) + MUL_R(temp2_i, temp2_i)) +
347 (MUL_R(temp4_r, temp4_r) + MUL_R(temp4_i, temp4_i));
348
349 #else
350
351 temp2_r = QMF_RE(buffer[offset-2][bd]);
352 temp2_i = QMF_IM(buffer[offset-2][bd]);
353 temp3_r = QMF_RE(buffer[offset-1][bd]);
354 temp3_i = QMF_IM(buffer[offset-1][bd]);
355 // Save these because they are needed after loop
356 temp4_r = temp2_r;
357 temp4_i = temp2_i;
358 temp5_r = temp3_r;
359 temp5_i = temp3_i;
360
361 for (j = offset; j < len + offset; j++)
362 {
363 temp1_r = temp2_r; // temp1_r = QMF_RE(buffer[j-2][bd];
364 temp1_i = temp2_i; // temp1_i = QMF_IM(buffer[j-2][bd];
365 temp2_r = temp3_r; // temp2_r = QMF_RE(buffer[j-1][bd];
366 temp2_i = temp3_i; // temp2_i = QMF_IM(buffer[j-1][bd];
367 temp3_r = QMF_RE(buffer[j][bd]);
368 temp3_i = QMF_IM(buffer[j][bd]);
369 r01r += temp3_r * temp2_r + temp3_i * temp2_i;
370 r01i += temp3_i * temp2_r - temp3_r * temp2_i;
371 r02r += temp3_r * temp1_r + temp3_i * temp1_i;
372 r02i += temp3_i * temp1_r - temp3_r * temp1_i;
373 r11r += temp2_r * temp2_r + temp2_i * temp2_i;
374 }
375
376 // These are actual values in temporary variable at this point
377 // temp1_r = QMF_RE(buffer[len+offset-1-2][bd];
378 // temp1_i = QMF_IM(buffer[len+offset-1-2][bd];
379 // temp2_r = QMF_RE(buffer[len+offset-1-1][bd];
380 // temp2_i = QMF_IM(buffer[len+offset-1-1][bd];
381 // temp3_r = QMF_RE(buffer[len+offset-1][bd]);
382 // temp3_i = QMF_IM(buffer[len+offset-1][bd]);
383 // temp4_r = QMF_RE(buffer[offset-2][bd]);
384 // temp4_i = QMF_IM(buffer[offset-2][bd]);
385 // temp5_r = QMF_RE(buffer[offset-1][bd]);
386 // temp5_i = QMF_IM(buffer[offset-1][bd]);
387
388 RE(ac->r12) = r01r -
389 (temp3_r * temp2_r + temp3_i * temp2_i) +
390 (temp5_r * temp4_r + temp5_i * temp4_i);
391 IM(ac->r12) = r01i -
392 (temp3_i * temp2_r - temp3_r * temp2_i) +
393 (temp5_i * temp4_r - temp5_r * temp4_i);
394 RE(ac->r22) = r11r -
395 (temp2_r * temp2_r + temp2_i * temp2_i) +
396 (temp4_r * temp4_r + temp4_i * temp4_i);
397
398 #endif
399 240
400 RE(ac->r01) = r01r; 241 RE(ac->r01) = r01r;
401 IM(ac->r01) = r01i; 242 IM(ac->r01) = r01i;
402 RE(ac->r02) = r02r; 243 RE(ac->r02) = r02r;
403 IM(ac->r02) = r02i; 244 IM(ac->r02) = r02i;
404 RE(ac->r11) = r11r; 245 RE(ac->r11) = r11r;
405 246
406 ac->det = MUL_R(RE(ac->r11), RE(ac->r22)) - MUL_F(rel, (MUL_R(RE(ac->r12), RE(ac->r12)) + MUL_R(IM(ac->r12), IM(ac->r12)))); 247 RE(ac->r12) = r01r -
248 (QMF_RE(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) + QMF_IM(buffer[len+offset-1][bd]) * QMF_IM(buffer[len+offset-2][bd])) +
249 (QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]) + QMF_IM(buffer[offset-1][bd]) * QMF_IM(buffer[offset-2][bd]));
250 IM(ac->r12) = r01i -
251 (QMF_IM(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) - QMF_RE(buffer[len+offset-1][bd]) * QMF_IM(buffer[len+offset-2][bd])) +
252 (QMF_IM(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]) - QMF_RE(buffer[offset-1][bd]) * QMF_IM(buffer[offset-2][bd]));
253 RE(ac->r22) = r11r -
254 (QMF_RE(buffer[len+offset-2][bd]) * QMF_RE(buffer[len+offset-2][bd]) + QMF_IM(buffer[len+offset-2][bd]) * QMF_IM(buffer[len+offset-2][bd])) +
255 (QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]) + QMF_IM(buffer[offset-2][bd]) * QMF_IM(buffer[offset-2][bd]));
256
257 ac->det = RE(ac->r11) * RE(ac->r22) - rel * (RE(ac->r12) * RE(ac->r12) + IM(ac->r12) * IM(ac->r12));
407 } 258 }
408 #endif 259 #endif
409 260
410 /* calculate linear prediction coefficients using the covariance method */ 261 /* calculate linear prediction coefficients using the covariance method */
411 #ifndef SBR_LOW_POWER 262 static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32],
412 static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64], 263 complex_t *alpha_0, complex_t *alpha_1
413 complex_t *alpha_0, complex_t *alpha_1, uint8_t k) 264 #ifdef SBR_LOW_POWER
414 { 265 , real_t *rxx
415 real_t tmp; 266 #endif
416 acorr_coef ac; 267 )
417
418 auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6);
419
420 if (ac.det == 0)
421 {
422 RE(alpha_1[k]) = 0;
423 IM(alpha_1[k]) = 0;
424 } else {
425 #ifdef FIXED_POINT
426 tmp = (MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(IM(ac.r01), IM(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11)));
427 RE(alpha_1[k]) = DIV_R(tmp, ac.det);
428 tmp = (MUL_R(IM(ac.r01), RE(ac.r12)) + MUL_R(RE(ac.r01), IM(ac.r12)) - MUL_R(IM(ac.r02), RE(ac.r11)));
429 IM(alpha_1[k]) = DIV_R(tmp, ac.det);
430 #else
431 tmp = REAL_CONST(1.0) / ac.det;
432 RE(alpha_1[k]) = (MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(IM(ac.r01), IM(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11))) * tmp;
433 IM(alpha_1[k]) = (MUL_R(IM(ac.r01), RE(ac.r12)) + MUL_R(RE(ac.r01), IM(ac.r12)) - MUL_R(IM(ac.r02), RE(ac.r11))) * tmp;
434 #endif
435 }
436
437 if (RE(ac.r11) == 0)
438 {
439 RE(alpha_0[k]) = 0;
440 IM(alpha_0[k]) = 0;
441 } else {
442 #ifdef FIXED_POINT
443 tmp = -(RE(ac.r01) + MUL_R(RE(alpha_1[k]), RE(ac.r12)) + MUL_R(IM(alpha_1[k]), IM(ac.r12)));
444 RE(alpha_0[k]) = DIV_R(tmp, RE(ac.r11));
445 tmp = -(IM(ac.r01) + MUL_R(IM(alpha_1[k]), RE(ac.r12)) - MUL_R(RE(alpha_1[k]), IM(ac.r12)));
446 IM(alpha_0[k]) = DIV_R(tmp, RE(ac.r11));
447 #else
448 tmp = 1.0f / RE(ac.r11);
449 RE(alpha_0[k]) = -(RE(ac.r01) + MUL_R(RE(alpha_1[k]), RE(ac.r12)) + MUL_R(IM(alpha_1[k]), IM(ac.r12))) * tmp;
450 IM(alpha_0[k]) = -(IM(ac.r01) + MUL_R(IM(alpha_1[k]), RE(ac.r12)) - MUL_R(RE(alpha_1[k]), IM(ac.r12))) * tmp;
451 #endif
452 }
453
454 if ((MUL_R(RE(alpha_0[k]),RE(alpha_0[k])) + MUL_R(IM(alpha_0[k]),IM(alpha_0[k])) >= REAL_CONST(16)) ||
455 (MUL_R(RE(alpha_1[k]),RE(alpha_1[k])) + MUL_R(IM(alpha_1[k]),IM(alpha_1[k])) >= REAL_CONST(16)))
456 {
457 RE(alpha_0[k]) = 0;
458 IM(alpha_0[k]) = 0;
459 RE(alpha_1[k]) = 0;
460 IM(alpha_1[k]) = 0;
461 }
462 }
463 #else
464 static void calc_prediction_coef_lp(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][64],
465 complex_t *alpha_0, complex_t *alpha_1, real_t *rxx)
466 { 268 {
467 uint8_t k; 269 uint8_t k;
468 real_t tmp; 270 real_t tmp;
469 acorr_coef ac; 271 acorr_coef ac;
470 272
471 for (k = 1; k < sbr->f_master[0]; k++) 273 for (k = 1; k < sbr->f_master[0]; k++)
472 { 274 {
473 auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6); 275 auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6);
474 276
277 #ifdef SBR_LOW_POWER
475 if (ac.det == 0) 278 if (ac.det == 0)
476 { 279 {
280 RE(alpha_1[k]) = 0;
281 } else {
282 tmp = MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11));
283 RE(alpha_1[k]) = SBR_DIV(tmp, ac.det);
284 }
285
286 if (RE(ac.r11) == 0)
287 {
477 RE(alpha_0[k]) = 0; 288 RE(alpha_0[k]) = 0;
478 RE(alpha_1[k]) = 0; 289 } else {
479 } else { 290 tmp = RE(ac.r01) + MUL_R(RE(alpha_1[k]), RE(ac.r12));
480 tmp = MUL_R(RE(ac.r01), RE(ac.r22)) - MUL_R(RE(ac.r12), RE(ac.r02)); 291 RE(alpha_0[k]) = -SBR_DIV(tmp, RE(ac.r11));
481 RE(alpha_0[k]) = DIV_R(tmp, (-ac.det));
482
483 tmp = MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11));
484 RE(alpha_1[k]) = DIV_R(tmp, ac.det);
485 } 292 }
486 293
487 if ((RE(alpha_0[k]) >= REAL_CONST(4)) || (RE(alpha_1[k]) >= REAL_CONST(4))) 294 if ((RE(alpha_0[k]) >= REAL_CONST(4)) || (RE(alpha_1[k]) >= REAL_CONST(4)))
488 { 295 {
489 RE(alpha_0[k]) = REAL_CONST(0); 296 RE(alpha_0[k]) = REAL_CONST(0);
491 } 298 }
492 299
493 /* reflection coefficient */ 300 /* reflection coefficient */
494 if (RE(ac.r11) == 0) 301 if (RE(ac.r11) == 0)
495 { 302 {
496 rxx[k] = COEF_CONST(0.0); 303 rxx[k] = REAL_CONST(0.0);
497 } else { 304 } else {
498 rxx[k] = DIV_C(RE(ac.r01), RE(ac.r11)); 305 rxx[k] = -SBR_DIV(RE(ac.r01), RE(ac.r11));
499 rxx[k] = -rxx[k]; 306 if (rxx[k] > REAL_CONST(1.0)) rxx[k] = REAL_CONST(1.0);
500 if (rxx[k] > COEF_CONST(1.0)) rxx[k] = COEF_CONST(1.0); 307 if (rxx[k] < REAL_CONST(-1.0)) rxx[k] = REAL_CONST(-1.0);
501 if (rxx[k] < COEF_CONST(-1.0)) rxx[k] = COEF_CONST(-1.0); 308 }
502 } 309 #else
503 } 310 if (ac.det == 0)
504 } 311 {
505 312 RE(alpha_1[k]) = 0;
313 IM(alpha_1[k]) = 0;
314 } else {
315 tmp = REAL_CONST(1.0) / ac.det;
316 RE(alpha_1[k]) = (RE(ac.r01) * RE(ac.r12) - IM(ac.r01) * IM(ac.r12) - RE(ac.r02) * RE(ac.r11)) * tmp;
317 IM(alpha_1[k]) = (IM(ac.r01) * RE(ac.r12) + RE(ac.r01) * IM(ac.r12) - IM(ac.r02) * RE(ac.r11)) * tmp;
318 }
319
320 if (RE(ac.r11) == 0)
321 {
322 RE(alpha_0[k]) = 0;
323 IM(alpha_0[k]) = 0;
324 } else {
325 tmp = 1.0f / RE(ac.r11);
326 RE(alpha_0[k]) = -(RE(ac.r01) + RE(alpha_1[k]) * RE(ac.r12) + IM(alpha_1[k]) * IM(ac.r12)) * tmp;
327 IM(alpha_0[k]) = -(IM(ac.r01) + IM(alpha_1[k]) * RE(ac.r12) - RE(alpha_1[k]) * IM(ac.r12)) * tmp;
328 }
329
330 if ((RE(alpha_0[k])*RE(alpha_0[k]) + IM(alpha_0[k])*IM(alpha_0[k]) >= 16) ||
331 (RE(alpha_1[k])*RE(alpha_1[k]) + IM(alpha_1[k])*IM(alpha_1[k]) >= 16))
332 {
333 RE(alpha_0[k]) = 0;
334 IM(alpha_0[k]) = 0;
335 RE(alpha_1[k]) = 0;
336 IM(alpha_1[k]) = 0;
337 }
338 #endif
339 }
340 }
341
342 #ifdef SBR_LOW_POWER
506 static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg) 343 static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg)
507 { 344 {
508 uint8_t k; 345 uint8_t k;
509 346
510 rxx[0] = COEF_CONST(0.0); 347 rxx[0] = REAL_CONST(0.0);
511 deg[1] = COEF_CONST(0.0); 348 deg[1] = REAL_CONST(0.0);
512 349
513 for (k = 2; k < sbr->k0; k++) 350 for (k = 2; k < sbr->k0; k++)
514 { 351 {
515 deg[k] = 0.0; 352 deg[k] = 0.0;
516 353
517 if ((k % 2 == 0) && (rxx[k] < COEF_CONST(0.0))) 354 if ((k % 2 == 0) && (rxx[k] < REAL_CONST(0.0)))
518 { 355 {
519 if (rxx[k-1] < 0.0) 356 if (rxx[k-1] < 0.0)
520 { 357 {
521 deg[k] = COEF_CONST(1.0); 358 deg[k] = REAL_CONST(1.0);
522 359
523 if (rxx[k-2] > COEF_CONST(0.0)) 360 if (rxx[k-2] > REAL_CONST(0.0))
524 { 361 {
525 deg[k-1] = COEF_CONST(1.0) - MUL_C(rxx[k-1], rxx[k-1]); 362 deg[k-1] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
526 } 363 }
527 } else if (rxx[k-2] > COEF_CONST(0.0)) { 364 } else if (rxx[k-2] > REAL_CONST(0.0)) {
528 deg[k] = COEF_CONST(1.0) - MUL_C(rxx[k-1], rxx[k-1]); 365 deg[k] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
529 } 366 }
530 } 367 }
531 368
532 if ((k % 2 == 1) && (rxx[k] > COEF_CONST(0.0))) 369 if ((k % 2 == 1) && (rxx[k] > REAL_CONST(0.0)))
533 { 370 {
534 if (rxx[k-1] > COEF_CONST(0.0)) 371 if (rxx[k-1] > REAL_CONST(0.0))
535 { 372 {
536 deg[k] = COEF_CONST(1.0); 373 deg[k] = REAL_CONST(1.0);
537 374
538 if (rxx[k-2] < COEF_CONST(0.0)) 375 if (rxx[k-2] < REAL_CONST(0.0))
539 { 376 {
540 deg[k-1] = COEF_CONST(1.0) - MUL_C(rxx[k-1], rxx[k-1]); 377 deg[k-1] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
541 } 378 }
542 } else if (rxx[k-2] < COEF_CONST(0.0)) { 379 } else if (rxx[k-2] < REAL_CONST(0.0)) {
543 deg[k] = COEF_CONST(1.0) - MUL_C(rxx[k-1], rxx[k-1]); 380 deg[k] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
544 } 381 }
545 } 382 }
546 } 383 }
547 } 384 }
548 #endif 385 #endif
601 { 438 {
602 uint8_t i, k; 439 uint8_t i, k;
603 uint8_t odd, sb; 440 uint8_t odd, sb;
604 uint8_t msb = sbr->k0; 441 uint8_t msb = sbr->k0;
605 uint8_t usb = sbr->kx; 442 uint8_t usb = sbr->kx;
606 uint8_t goalSbTab[] = { 21, 23, 32, 43, 46, 64, 85, 93, 128, 0, 0, 0 }; 443 uint8_t goalSbTab[] = { 21, 23, 43, 46, 64, 85, 93, 128, 0, 0, 0 };
607 /* (uint8_t)(2.048e6/sbr->sample_rate + 0.5); */ 444 /* (uint8_t)(2.048e6/sbr->sample_rate + 0.5); */
608 uint8_t goalSb = goalSbTab[get_sr_index(sbr->sample_rate)]; 445 uint8_t goalSb = goalSbTab[get_sr_index(sbr->sample_rate)];
609 446
610 sbr->noPatches = 0; 447 sbr->noPatches = 0;
611 448
615 k = i+1; 452 k = i+1;
616 } else { 453 } else {
617 k = sbr->N_master; 454 k = sbr->N_master;
618 } 455 }
619 456
620 if (sbr->N_master == 0)
621 {
622 sbr->noPatches = 0;
623 sbr->patchNoSubbands[0] = 0;
624 sbr->patchStartSubband[0] = 0;
625
626 return;
627 }
628
629 do 457 do
630 { 458 {
631 uint8_t j = k + 1; 459 uint8_t j = k + 1;
632 460
633 do 461 do