switch_ratio may not work with some filter chains
[mplayer/greg.git] / libfaad2 / sbr_hfgen.c
blob683b302934699666e29e0e032c75169c3a9305ce
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30
26 ** $Id: sbr_hfgen.c,v 1.3 2004/06/02 22:59:03 diego Exp $
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
28 **/
30 /* High Frequency generation */
32 #include "common.h"
33 #include "structs.h"
35 #ifdef SBR_DEC
37 #include "sbr_syntax.h"
38 #include "sbr_hfgen.h"
39 #include "sbr_fbt.h"
42 /* static function declarations */
43 static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32],
44 complex_t *alpha_0, complex_t *alpha_1
45 #ifdef SBR_LOW_POWER
46 , real_t *rxx
47 #endif
49 #ifdef SBR_LOW_POWER
50 static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg);
51 #endif
52 static void calc_chirp_factors(sbr_info *sbr, uint8_t ch);
53 static void patch_construction(sbr_info *sbr);
56 void hf_generation(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32],
57 qmf_t Xhigh[MAX_NTSRHFG][64]
58 #ifdef SBR_LOW_POWER
59 ,real_t *deg
60 #endif
61 ,uint8_t ch)
63 uint8_t l, i, x;
64 ALIGN complex_t alpha_0[64], alpha_1[64];
65 #ifdef SBR_LOW_POWER
66 ALIGN real_t rxx[64];
67 #endif
69 uint8_t offset = sbr->tHFAdj;
70 uint8_t first = sbr->t_E[ch][0];
71 uint8_t last = sbr->t_E[ch][sbr->L_E[ch]];
73 // printf("%d %d\n", first, last);
75 calc_chirp_factors(sbr, ch);
77 for (i = first; i < last; i++)
79 memset(Xhigh[i + offset], 0, 64 * sizeof(qmf_t));
82 if ((ch == 0) && (sbr->Reset))
83 patch_construction(sbr);
85 /* calculate the prediction coefficients */
86 calc_prediction_coef(sbr, Xlow, alpha_0, alpha_1
87 #ifdef SBR_LOW_POWER
88 , rxx
89 #endif
92 #ifdef SBR_LOW_POWER
93 calc_aliasing_degree(sbr, rxx, deg);
94 #endif
96 /* actual HF generation */
97 for (i = 0; i < sbr->noPatches; i++)
99 for (x = 0; x < sbr->patchNoSubbands[i]; x++)
101 complex_t a0, a1;
102 real_t bw, bw2;
103 uint8_t q, p, k, g;
105 /* find the low and high band for patching */
106 k = sbr->kx + x;
107 for (q = 0; q < i; q++)
109 k += sbr->patchNoSubbands[q];
111 p = sbr->patchStartSubband[i] + x;
113 #ifdef SBR_LOW_POWER
114 if (x != 0 /*x < sbr->patchNoSubbands[i]-1*/)
115 deg[k] = deg[p];
116 else
117 deg[k] = 0;
118 #endif
120 g = sbr->table_map_k_to_g[k];
122 bw = sbr->bwArray[ch][g];
123 bw2 = MUL_C(bw, bw);
125 /* do the patching */
126 /* with or without filtering */
127 if (bw2 > 0)
129 RE(a0) = MUL_C(RE(alpha_0[p]), bw);
130 RE(a1) = MUL_C(RE(alpha_1[p]), bw2);
131 #ifndef SBR_LOW_POWER
132 IM(a0) = MUL_C(IM(alpha_0[p]), bw);
133 IM(a1) = MUL_C(IM(alpha_1[p]), bw2);
134 #endif
136 for (l = first; l < last; l++)
138 QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]);
139 #ifndef SBR_LOW_POWER
140 QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]);
141 #endif
143 #ifdef SBR_LOW_POWER
144 QMF_RE(Xhigh[l + offset][k]) += (
145 MUL_R(RE(a0), QMF_RE(Xlow[l - 1 + offset][p])) +
146 MUL_R(RE(a1), QMF_RE(Xlow[l - 2 + offset][p])));
147 #else
148 QMF_RE(Xhigh[l + offset][k]) += (
149 RE(a0) * QMF_RE(Xlow[l - 1 + offset][p]) -
150 IM(a0) * QMF_IM(Xlow[l - 1 + offset][p]) +
151 RE(a1) * QMF_RE(Xlow[l - 2 + offset][p]) -
152 IM(a1) * QMF_IM(Xlow[l - 2 + offset][p]));
153 QMF_IM(Xhigh[l + offset][k]) += (
154 IM(a0) * QMF_RE(Xlow[l - 1 + offset][p]) +
155 RE(a0) * QMF_IM(Xlow[l - 1 + offset][p]) +
156 IM(a1) * QMF_RE(Xlow[l - 2 + offset][p]) +
157 RE(a1) * QMF_IM(Xlow[l - 2 + offset][p]));
158 #endif
160 } else {
161 for (l = first; l < last; l++)
163 QMF_RE(Xhigh[l + offset][k]) = QMF_RE(Xlow[l + offset][p]);
164 #ifndef SBR_LOW_POWER
165 QMF_IM(Xhigh[l + offset][k]) = QMF_IM(Xlow[l + offset][p]);
166 #endif
172 if (sbr->Reset)
174 limiter_frequency_table(sbr);
178 typedef struct
180 complex_t r01;
181 complex_t r02;
182 complex_t r11;
183 complex_t r12;
184 complex_t r22;
185 real_t det;
186 } acorr_coef;
188 #define SBR_ABS(A) ((A) < 0) ? -(A) : (A)
190 #ifdef SBR_LOW_POWER
191 static void auto_correlation(sbr_info *sbr, acorr_coef *ac,
192 qmf_t buffer[MAX_NTSRHFG][32],
193 uint8_t bd, uint8_t len)
195 real_t r01 = 0, r02 = 0, r11 = 0;
196 int8_t j;
197 uint8_t offset = sbr->tHFAdj;
198 const real_t rel = 1 / (1 + 1e-6f);
201 for (j = offset; j < len + offset; j++)
203 r01 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]);
204 r02 += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]);
205 r11 += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]);
207 RE(ac->r12) = r01 -
208 QMF_RE(buffer[len+offset-1][bd]) * QMF_RE(buffer[len+offset-2][bd]) +
209 QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]);
210 RE(ac->r22) = r11 -
211 QMF_RE(buffer[len+offset-2][bd]) * QMF_RE(buffer[len+offset-2][bd]) +
212 QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]);
213 RE(ac->r01) = r01;
214 RE(ac->r02) = r02;
215 RE(ac->r11) = r11;
217 ac->det = MUL_R(RE(ac->r11), RE(ac->r22)) - MUL_C(MUL_R(RE(ac->r12), RE(ac->r12)), rel);
219 #else
220 static void auto_correlation(sbr_info *sbr, acorr_coef *ac, qmf_t buffer[MAX_NTSRHFG][32],
221 uint8_t bd, uint8_t len)
223 real_t r01r = 0, r01i = 0, r02r = 0, r02i = 0, r11r = 0;
224 const real_t rel = 1 / (1 + 1e-6f);
225 int8_t j;
226 uint8_t offset = sbr->tHFAdj;
229 for (j = offset; j < len + offset; j++)
231 r01r += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]) +
232 QMF_IM(buffer[j][bd]) * QMF_IM(buffer[j-1][bd]);
233 r01i += QMF_IM(buffer[j][bd]) * QMF_RE(buffer[j-1][bd]) -
234 QMF_RE(buffer[j][bd]) * QMF_IM(buffer[j-1][bd]);
235 r02r += QMF_RE(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]) +
236 QMF_IM(buffer[j][bd]) * QMF_IM(buffer[j-2][bd]);
237 r02i += QMF_IM(buffer[j][bd]) * QMF_RE(buffer[j-2][bd]) -
238 QMF_RE(buffer[j][bd]) * QMF_IM(buffer[j-2][bd]);
239 r11r += QMF_RE(buffer[j-1][bd]) * QMF_RE(buffer[j-1][bd]) +
240 QMF_IM(buffer[j-1][bd]) * QMF_IM(buffer[j-1][bd]);
243 RE(ac->r01) = r01r;
244 IM(ac->r01) = r01i;
245 RE(ac->r02) = r02r;
246 IM(ac->r02) = r02i;
247 RE(ac->r11) = r11r;
249 RE(ac->r12) = r01r -
250 (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])) +
251 (QMF_RE(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]) + QMF_IM(buffer[offset-1][bd]) * QMF_IM(buffer[offset-2][bd]));
252 IM(ac->r12) = r01i -
253 (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])) +
254 (QMF_IM(buffer[offset-1][bd]) * QMF_RE(buffer[offset-2][bd]) - QMF_RE(buffer[offset-1][bd]) * QMF_IM(buffer[offset-2][bd]));
255 RE(ac->r22) = r11r -
256 (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])) +
257 (QMF_RE(buffer[offset-2][bd]) * QMF_RE(buffer[offset-2][bd]) + QMF_IM(buffer[offset-2][bd]) * QMF_IM(buffer[offset-2][bd]));
259 ac->det = RE(ac->r11) * RE(ac->r22) - rel * (RE(ac->r12) * RE(ac->r12) + IM(ac->r12) * IM(ac->r12));
261 #endif
263 /* calculate linear prediction coefficients using the covariance method */
264 static void calc_prediction_coef(sbr_info *sbr, qmf_t Xlow[MAX_NTSRHFG][32],
265 complex_t *alpha_0, complex_t *alpha_1
266 #ifdef SBR_LOW_POWER
267 , real_t *rxx
268 #endif
271 uint8_t k;
272 real_t tmp;
273 acorr_coef ac;
275 for (k = 1; k < sbr->f_master[0]; k++)
277 auto_correlation(sbr, &ac, Xlow, k, sbr->numTimeSlotsRate + 6);
279 #ifdef SBR_LOW_POWER
280 if (ac.det == 0)
282 RE(alpha_1[k]) = 0;
283 } else {
284 tmp = MUL_R(RE(ac.r01), RE(ac.r12)) - MUL_R(RE(ac.r02), RE(ac.r11));
285 RE(alpha_1[k]) = SBR_DIV(tmp, ac.det);
288 if (RE(ac.r11) == 0)
290 RE(alpha_0[k]) = 0;
291 } else {
292 tmp = RE(ac.r01) + MUL_R(RE(alpha_1[k]), RE(ac.r12));
293 RE(alpha_0[k]) = -SBR_DIV(tmp, RE(ac.r11));
296 if ((RE(alpha_0[k]) >= REAL_CONST(4)) || (RE(alpha_1[k]) >= REAL_CONST(4)))
298 RE(alpha_0[k]) = REAL_CONST(0);
299 RE(alpha_1[k]) = REAL_CONST(0);
302 /* reflection coefficient */
303 if (RE(ac.r11) == 0)
305 rxx[k] = REAL_CONST(0.0);
306 } else {
307 rxx[k] = -SBR_DIV(RE(ac.r01), RE(ac.r11));
308 if (rxx[k] > REAL_CONST(1.0)) rxx[k] = REAL_CONST(1.0);
309 if (rxx[k] < REAL_CONST(-1.0)) rxx[k] = REAL_CONST(-1.0);
311 #else
312 if (ac.det == 0)
314 RE(alpha_1[k]) = 0;
315 IM(alpha_1[k]) = 0;
316 } else {
317 tmp = REAL_CONST(1.0) / ac.det;
318 RE(alpha_1[k]) = (RE(ac.r01) * RE(ac.r12) - IM(ac.r01) * IM(ac.r12) - RE(ac.r02) * RE(ac.r11)) * tmp;
319 IM(alpha_1[k]) = (IM(ac.r01) * RE(ac.r12) + RE(ac.r01) * IM(ac.r12) - IM(ac.r02) * RE(ac.r11)) * tmp;
322 if (RE(ac.r11) == 0)
324 RE(alpha_0[k]) = 0;
325 IM(alpha_0[k]) = 0;
326 } else {
327 tmp = 1.0f / RE(ac.r11);
328 RE(alpha_0[k]) = -(RE(ac.r01) + RE(alpha_1[k]) * RE(ac.r12) + IM(alpha_1[k]) * IM(ac.r12)) * tmp;
329 IM(alpha_0[k]) = -(IM(ac.r01) + IM(alpha_1[k]) * RE(ac.r12) - RE(alpha_1[k]) * IM(ac.r12)) * tmp;
332 if ((RE(alpha_0[k])*RE(alpha_0[k]) + IM(alpha_0[k])*IM(alpha_0[k]) >= 16) ||
333 (RE(alpha_1[k])*RE(alpha_1[k]) + IM(alpha_1[k])*IM(alpha_1[k]) >= 16))
335 RE(alpha_0[k]) = 0;
336 IM(alpha_0[k]) = 0;
337 RE(alpha_1[k]) = 0;
338 IM(alpha_1[k]) = 0;
340 #endif
344 #ifdef SBR_LOW_POWER
345 static void calc_aliasing_degree(sbr_info *sbr, real_t *rxx, real_t *deg)
347 uint8_t k;
349 rxx[0] = REAL_CONST(0.0);
350 deg[1] = REAL_CONST(0.0);
352 for (k = 2; k < sbr->k0; k++)
354 deg[k] = 0.0;
356 if ((k % 2 == 0) && (rxx[k] < REAL_CONST(0.0)))
358 if (rxx[k-1] < 0.0)
360 deg[k] = REAL_CONST(1.0);
362 if (rxx[k-2] > REAL_CONST(0.0))
364 deg[k-1] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
366 } else if (rxx[k-2] > REAL_CONST(0.0)) {
367 deg[k] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
371 if ((k % 2 == 1) && (rxx[k] > REAL_CONST(0.0)))
373 if (rxx[k-1] > REAL_CONST(0.0))
375 deg[k] = REAL_CONST(1.0);
377 if (rxx[k-2] < REAL_CONST(0.0))
379 deg[k-1] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
381 } else if (rxx[k-2] < REAL_CONST(0.0)) {
382 deg[k] = REAL_CONST(1.0) - MUL_R(rxx[k-1], rxx[k-1]);
387 #endif
389 /* FIXED POINT: bwArray = COEF */
390 static real_t mapNewBw(uint8_t invf_mode, uint8_t invf_mode_prev)
392 switch (invf_mode)
394 case 1: /* LOW */
395 if (invf_mode_prev == 0) /* NONE */
396 return COEF_CONST(0.6);
397 else
398 return COEF_CONST(0.75);
400 case 2: /* MID */
401 return COEF_CONST(0.9);
403 case 3: /* HIGH */
404 return COEF_CONST(0.98);
406 default: /* NONE */
407 if (invf_mode_prev == 1) /* LOW */
408 return COEF_CONST(0.6);
409 else
410 return COEF_CONST(0.0);
414 /* FIXED POINT: bwArray = COEF */
415 static void calc_chirp_factors(sbr_info *sbr, uint8_t ch)
417 uint8_t i;
419 for (i = 0; i < sbr->N_Q; i++)
421 sbr->bwArray[ch][i] = mapNewBw(sbr->bs_invf_mode[ch][i], sbr->bs_invf_mode_prev[ch][i]);
423 if (sbr->bwArray[ch][i] < sbr->bwArray_prev[ch][i])
424 sbr->bwArray[ch][i] = MUL_F(sbr->bwArray[ch][i], FRAC_CONST(0.75)) + MUL_F(sbr->bwArray_prev[ch][i], FRAC_CONST(0.25));
425 else
426 sbr->bwArray[ch][i] = MUL_F(sbr->bwArray[ch][i], FRAC_CONST(0.90625)) + MUL_F(sbr->bwArray_prev[ch][i], FRAC_CONST(0.09375));
428 if (sbr->bwArray[ch][i] < COEF_CONST(0.015625))
429 sbr->bwArray[ch][i] = COEF_CONST(0.0);
431 if (sbr->bwArray[ch][i] >= COEF_CONST(0.99609375))
432 sbr->bwArray[ch][i] = COEF_CONST(0.99609375);
434 sbr->bwArray_prev[ch][i] = sbr->bwArray[ch][i];
435 sbr->bs_invf_mode_prev[ch][i] = sbr->bs_invf_mode[ch][i];
439 static void patch_construction(sbr_info *sbr)
441 uint8_t i, k;
442 uint8_t odd, sb;
443 uint8_t msb = sbr->k0;
444 uint8_t usb = sbr->kx;
445 uint8_t goalSbTab[] = { 21, 23, 43, 46, 64, 85, 93, 128, 0, 0, 0 };
446 /* (uint8_t)(2.048e6/sbr->sample_rate + 0.5); */
447 uint8_t goalSb = goalSbTab[get_sr_index(sbr->sample_rate)];
449 sbr->noPatches = 0;
451 if (goalSb < (sbr->kx + sbr->M))
453 for (i = 0, k = 0; sbr->f_master[i] < goalSb; i++)
454 k = i+1;
455 } else {
456 k = sbr->N_master;
461 uint8_t j = k + 1;
465 j--;
467 sb = sbr->f_master[j];
468 odd = (sb - 2 + sbr->k0) % 2;
469 } while (sb > (sbr->k0 - 1 + msb - odd));
471 sbr->patchNoSubbands[sbr->noPatches] = max(sb - usb, 0);
472 sbr->patchStartSubband[sbr->noPatches] = sbr->k0 - odd -
473 sbr->patchNoSubbands[sbr->noPatches];
475 if (sbr->patchNoSubbands[sbr->noPatches] > 0)
477 usb = sb;
478 msb = sb;
479 sbr->noPatches++;
480 } else {
481 msb = sbr->kx;
484 if (sbr->f_master[k] - sb < 3)
485 k = sbr->N_master;
486 } while (sb != (sbr->kx + sbr->M));
488 if ((sbr->patchNoSubbands[sbr->noPatches-1] < 3) && (sbr->noPatches > 1))
490 sbr->noPatches--;
493 sbr->noPatches = min(sbr->noPatches, 5);
496 #endif