switch_ratio may not work with some filter chains
[mplayer/greg.git] / libfaad2 / sbr_e_nf.c
blobeb89267c13ff6e70096ae316baad2b3da5e3cebb
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_e_nf.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 #include "common.h"
31 #include "structs.h"
33 #ifdef SBR_DEC
35 #include <stdlib.h>
37 #include "sbr_syntax.h"
38 #include "sbr_e_nf.h"
40 ALIGN static const real_t pow2deq[] = {
41 REAL_CONST(2.9103830456733704E-011), REAL_CONST(5.8207660913467407E-011),
42 REAL_CONST(1.1641532182693481E-010), REAL_CONST(2.3283064365386963E-010),
43 REAL_CONST(4.6566128730773926E-010), REAL_CONST(9.3132257461547852E-010),
44 REAL_CONST(1.862645149230957E-009), REAL_CONST(3.7252902984619141E-009),
45 REAL_CONST(7.4505805969238281E-009), REAL_CONST(1.4901161193847656E-008),
46 REAL_CONST(2.9802322387695313E-008), REAL_CONST(5.9604644775390625E-008),
47 REAL_CONST(1.1920928955078125E-007), REAL_CONST(2.384185791015625E-007),
48 REAL_CONST(4.76837158203125E-007), REAL_CONST(9.5367431640625E-007),
49 REAL_CONST(1.9073486328125E-006), REAL_CONST(3.814697265625E-006),
50 REAL_CONST(7.62939453125E-006), REAL_CONST(1.52587890625E-005),
51 REAL_CONST(3.0517578125E-005), REAL_CONST(6.103515625E-005),
52 REAL_CONST(0.0001220703125), REAL_CONST(0.000244140625),
53 REAL_CONST(0.00048828125), REAL_CONST(0.0009765625),
54 REAL_CONST(0.001953125), REAL_CONST(0.00390625),
55 REAL_CONST(0.0078125), REAL_CONST(0.015625),
56 REAL_CONST(0.03125), REAL_CONST(0.0625),
57 REAL_CONST(0.125), REAL_CONST(0.25),
58 REAL_CONST(0.5), REAL_CONST(1.0),
59 REAL_CONST(2.0), REAL_CONST(4.0),
60 REAL_CONST(8.0), REAL_CONST(16.0),
61 REAL_CONST(32.0), REAL_CONST(64.0),
62 REAL_CONST(128.0), REAL_CONST(256.0),
63 REAL_CONST(512.0), REAL_CONST(1024.0),
64 REAL_CONST(2048.0), REAL_CONST(4096.0),
65 REAL_CONST(8192.0), REAL_CONST(16384.0),
66 REAL_CONST(32768.0), REAL_CONST(65536.0),
67 REAL_CONST(131072.0), REAL_CONST(262144.0),
68 REAL_CONST(524288.0), REAL_CONST(1048576.0),
69 REAL_CONST(2097152.0), REAL_CONST(4194304.0),
70 REAL_CONST(8388608.0), REAL_CONST(16777216.0),
71 REAL_CONST(33554432.0), REAL_CONST(67108864.0),
72 REAL_CONST(134217728.0), REAL_CONST(268435456.0),
73 REAL_CONST(536870912.0), REAL_CONST(1073741824.0),
74 REAL_CONST(2147483648.0), REAL_CONST(4294967296.0),
75 REAL_CONST(8589934592.0), REAL_CONST(17179869184.0),
76 REAL_CONST(34359738368.0), REAL_CONST(68719476736.0),
77 REAL_CONST(137438953472.0), REAL_CONST(274877906944.0),
78 REAL_CONST(549755813888.0), REAL_CONST(1099511627776.0),
79 REAL_CONST(2199023255552.0), REAL_CONST(4398046511104.0),
80 REAL_CONST(8796093022208.0), REAL_CONST(17592186044416.0),
81 REAL_CONST(35184372088832.0), REAL_CONST(70368744177664.0),
82 REAL_CONST(140737488355328.0), REAL_CONST(281474976710656.0),
83 REAL_CONST(562949953421312.0), REAL_CONST(1125899906842624.0),
84 REAL_CONST(2251799813685248.0), REAL_CONST(4503599627370496.0),
85 REAL_CONST(9007199254740992.0), REAL_CONST(18014398509481984.0),
86 REAL_CONST(36028797018963968.0), REAL_CONST(72057594037927936.0),
87 REAL_CONST(144115188075855870.0), REAL_CONST(288230376151711740.0),
88 REAL_CONST(576460752303423490.0), REAL_CONST(1152921504606847000.0),
89 REAL_CONST(2305843009213694000.0), REAL_CONST(4611686018427387900.0),
90 REAL_CONST(9223372036854775800.0), REAL_CONST(1.8446744073709552E+019),
91 REAL_CONST(3.6893488147419103E+019), REAL_CONST(7.3786976294838206E+019),
92 REAL_CONST(1.4757395258967641E+020), REAL_CONST(2.9514790517935283E+020),
93 REAL_CONST(5.9029581035870565E+020), REAL_CONST(1.1805916207174113E+021),
94 REAL_CONST(2.3611832414348226E+021), REAL_CONST(4.7223664828696452E+021),
95 REAL_CONST(9.4447329657392904E+021), REAL_CONST(1.8889465931478581E+022),
96 REAL_CONST(3.7778931862957162E+022), REAL_CONST(7.5557863725914323E+022),
97 REAL_CONST(1.5111572745182865E+023), REAL_CONST(3.0223145490365729E+023),
98 REAL_CONST(6.0446290980731459E+023), REAL_CONST(1.2089258196146292E+024),
99 REAL_CONST(2.4178516392292583E+024), REAL_CONST(4.8357032784585167E+024),
100 REAL_CONST(9.6714065569170334E+024), REAL_CONST(1.9342813113834067E+025),
101 REAL_CONST(3.8685626227668134E+025), REAL_CONST(7.7371252455336267E+025),
102 REAL_CONST(1.5474250491067253E+026), REAL_CONST(3.0948500982134507E+026),
103 REAL_CONST(6.1897001964269014E+026), REAL_CONST(1.2379400392853803E+027),
104 REAL_CONST(2.4758800785707605E+027)
107 /* 1.0 / (1.0 + pow(2.0, x - 12) */
108 ALIGN static const real_t pow2deq_rcp[] = {
109 FRAC_CONST(0.99975591896509641),
110 FRAC_CONST(0.99951195705222062),
111 FRAC_CONST(0.99902439024390244),
112 FRAC_CONST(0.99805068226120852),
113 FRAC_CONST(0.99610894941634243),
114 FRAC_CONST(0.99224806201550386),
115 FRAC_CONST(0.98461538461538467),
116 FRAC_CONST(0.96969696969696972),
117 FRAC_CONST(0.94117647058823528),
118 FRAC_CONST(0.88888888888888884),
119 FRAC_CONST(0.80000000000000004),
120 FRAC_CONST(0.66666666666666663),
121 FRAC_CONST(0.5),
122 FRAC_CONST(0.33333333333333331),
123 FRAC_CONST(0.20000000000000001),
124 FRAC_CONST(0.1111111111111111),
125 FRAC_CONST(0.058823529411764705),
126 FRAC_CONST(0.030303030303030304),
127 FRAC_CONST(0.015384615384615385),
128 FRAC_CONST(0.0077519379844961239),
129 FRAC_CONST(0.0038910505836575876),
130 FRAC_CONST(0.0019493177387914229),
131 FRAC_CONST(0.00097560975609756097),
132 FRAC_CONST(0.0004880429477794046),
133 FRAC_CONST(0.00024408103490358799),
134 FRAC_CONST(0.00012205541315757354),
135 FRAC_CONST(6.1031431187061336E-005),
136 FRAC_CONST(3.0516646830846227E-005),
137 FRAC_CONST(1.5258556235409006E-005),
138 FRAC_CONST(7.6293363240331724E-006),
139 FRAC_CONST(3.8146827137652828E-006),
140 FRAC_CONST(1.9073449948406318E-006),
141 FRAC_CONST(9.5367340691241559E-007)
144 void extract_envelope_data(sbr_info *sbr, uint8_t ch)
146 uint8_t l, k;
148 for (l = 0; l < sbr->L_E[ch]; l++)
150 if (sbr->bs_df_env[ch][l] == 0)
152 for (k = 1; k < sbr->n[sbr->f[ch][l]]; k++)
154 sbr->E[ch][k][l] = sbr->E[ch][k - 1][l] + sbr->E[ch][k][l];
157 } else { /* bs_df_env == 1 */
159 uint8_t g = (l == 0) ? sbr->f_prev[ch] : sbr->f[ch][l-1];
160 int16_t E_prev;
162 if (sbr->f[ch][l] == g)
164 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
166 if (l == 0)
167 E_prev = sbr->E_prev[ch][k];
168 else
169 E_prev = sbr->E[ch][k][l - 1];
171 sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
174 } else if ((g == 1) && (sbr->f[ch][l] == 0)) {
175 uint8_t i;
177 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
179 for (i = 0; i < sbr->N_high; i++)
181 if (sbr->f_table_res[HI_RES][i] == sbr->f_table_res[LO_RES][k])
183 if (l == 0)
184 E_prev = sbr->E_prev[ch][i];
185 else
186 E_prev = sbr->E[ch][i][l - 1];
188 sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
193 } else if ((g == 0) && (sbr->f[ch][l] == 1)) {
194 uint8_t i;
196 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
198 for (i = 0; i < sbr->N_low; i++)
200 if ((sbr->f_table_res[LO_RES][i] <= sbr->f_table_res[HI_RES][k]) &&
201 (sbr->f_table_res[HI_RES][k] < sbr->f_table_res[LO_RES][i + 1]))
203 if (l == 0)
204 E_prev = sbr->E_prev[ch][i];
205 else
206 E_prev = sbr->E[ch][i][l - 1];
208 sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
217 void extract_noise_floor_data(sbr_info *sbr, uint8_t ch)
219 uint8_t l, k;
221 for (l = 0; l < sbr->L_Q[ch]; l++)
223 if (sbr->bs_df_noise[ch][l] == 0)
225 for (k = 1; k < sbr->N_Q; k++)
227 sbr->Q[ch][k][l] = sbr->Q[ch][k][l] + sbr->Q[ch][k-1][l];
229 } else {
230 if (l == 0)
232 for (k = 0; k < sbr->N_Q; k++)
234 sbr->Q[ch][k][l] = sbr->Q_prev[ch][k] + sbr->Q[ch][k][0];
236 } else {
237 for (k = 0; k < sbr->N_Q; k++)
239 sbr->Q[ch][k][l] = sbr->Q[ch][k][l - 1] + sbr->Q[ch][k][l];
246 void envelope_noise_dequantisation(sbr_info *sbr, uint8_t ch)
248 if (sbr->bs_coupling == 0)
250 int16_t exp;
251 uint8_t l, k;
252 uint8_t amp = (sbr->amp_res[ch]) ? 0 : 1;
254 for (l = 0; l < sbr->L_E[ch]; l++)
256 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
258 /* +6 for the *64 and -10 for the /32 in the synthesis QMF
259 * since this is a energy value: (x/32)^2 = (x^2)/1024
261 exp = (sbr->E[ch][k][l] >> amp) + 6;
263 if ((exp < -P2_TABLE_OFFSET) || (exp > P2_TABLE_MAX))
265 sbr->E_orig[ch][k][l] = 0;
266 } else {
267 /* FIXED POINT TODO: E_orig: INTEGER!! */
268 sbr->E_orig[ch][k][l] = pow2deq[exp + P2_TABLE_OFFSET];
270 /* save half the table size at the cost of 1 multiply */
271 if (amp && (sbr->E[ch][k][l] & 1))
273 sbr->E_orig[ch][k][l] = MUL_R(sbr->E_orig[ch][k][l], REAL_CONST(1.414213562));
279 for (l = 0; l < sbr->L_Q[ch]; l++)
281 for (k = 0; k < sbr->N_Q; k++)
283 if (sbr->Q[ch][k][l] < 0 || sbr->Q[ch][k][l] > 30)
285 sbr->Q_orig[ch][k][l] = 0;
286 } else {
287 exp = NOISE_FLOOR_OFFSET - sbr->Q[ch][k][l];
288 sbr->Q_orig[ch][k][l] = pow2deq[exp + P2_TABLE_OFFSET];
295 void unmap_envelope_noise(sbr_info *sbr)
297 real_t tmp;
298 int16_t exp0, exp1;
299 uint8_t l, k;
300 uint8_t amp0 = (sbr->amp_res[0]) ? 0 : 1;
301 uint8_t amp1 = (sbr->amp_res[1]) ? 0 : 1;
303 for (l = 0; l < sbr->L_E[0]; l++)
305 for (k = 0; k < sbr->n[sbr->f[0][l]]; k++)
307 /* +6: * 64 ; +1: * 2 ; -10: /1024 QMF */
308 exp0 = (sbr->E[0][k][l] >> amp0) + 7;
310 /* UN_MAP removed: (x / 4096) same as (x >> 12) */
311 /* E[1] is always even so no need for compensating the divide by 2 with
312 * an extra multiplication
314 exp1 = (sbr->E[1][k][l] >> amp1) - 12;
316 if ((exp0 < -P2_TABLE_OFFSET) || (exp0 > P2_TABLE_MAX) ||
317 (exp1 < -P2_TABLE_RCP_OFFSET) || (exp1 > P2_TABLE_RCP_MAX))
319 sbr->E_orig[1][k][l] = 0;
320 sbr->E_orig[0][k][l] = 0;
321 } else {
322 tmp = pow2deq[exp0 + P2_TABLE_OFFSET];
323 if (amp0 && (sbr->E[0][k][l] & 1))
324 tmp = MUL_R(tmp, REAL_CONST(1.414213562));
326 /* FIXED POINT TODO: E_orig: INTEGER!! */
327 sbr->E_orig[1][k][l] = MUL_F(tmp, pow2deq_rcp[exp1 + P2_TABLE_RCP_OFFSET]);
328 sbr->E_orig[0][k][l] = MUL_R(sbr->E_orig[1][k][l], pow2deq[exp1 + P2_TABLE_OFFSET]);
332 for (l = 0; l < sbr->L_Q[0]; l++)
334 for (k = 0; k < sbr->N_Q; k++)
336 if ((sbr->Q[0][k][l] < 0 || sbr->Q[0][k][l] > 30) ||
337 (sbr->Q[1][k][l] < 0 || sbr->Q[1][k][l] > 24 /* 2*panOffset(1) */))
339 sbr->Q_orig[0][k][l] = 0;
340 sbr->Q_orig[1][k][l] = 0;
341 } else {
342 exp0 = NOISE_FLOOR_OFFSET - sbr->Q[0][k][l] + 1;
343 exp1 = sbr->Q[1][k][l] - 12;
345 sbr->Q_orig[1][k][l] = MUL_F(pow2deq[exp0 + P2_TABLE_OFFSET], pow2deq_rcp[exp1 + P2_TABLE_RCP_OFFSET]);
346 sbr->Q_orig[0][k][l] = MUL_R(sbr->Q_orig[1][k][l], pow2deq[exp1 + P2_TABLE_OFFSET]);
352 #endif