10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 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.
|
|
18 **
|
|
19 ** Any non-GPL usage of this software or parts of this software is strictly
|
|
20 ** forbidden.
|
|
21 **
|
|
22 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
|
25 ** $Id: sbr_e_nf.c,v 1.1 2003/07/29 08:20:13 menno Exp $
|
|
26 **/
|
|
27
|
|
28 #include "common.h"
|
|
29 #include "structs.h"
|
|
30
|
|
31 #ifdef SBR_DEC
|
|
32
|
|
33 #include <stdlib.h>
|
|
34
|
|
35 #include "sbr_syntax.h"
|
|
36 #include "sbr_e_nf.h"
|
|
37
|
|
38 void extract_envelope_data(sbr_info *sbr, uint8_t ch)
|
|
39 {
|
|
40 uint8_t l, k;
|
|
41
|
|
42 #if 0
|
|
43 if (sbr->frame == 19)
|
|
44 {
|
|
45 sbr->frame = 19;
|
|
46 }
|
|
47 #endif
|
|
48
|
|
49 for (l = 0; l < sbr->L_E[ch]; l++)
|
|
50 {
|
|
51 if (sbr->bs_df_env[ch][l] == 0)
|
|
52 {
|
|
53 for (k = 1; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
54 {
|
|
55 sbr->E[ch][k][l] = sbr->E[ch][k - 1][l] + sbr->E[ch][k][l];
|
|
56 }
|
|
57
|
|
58 } else { /* bs_df_env == 1 */
|
|
59
|
|
60 uint8_t g = (l == 0) ? sbr->f_prev[ch] : sbr->f[ch][l-1];
|
|
61 int16_t E_prev;
|
|
62
|
|
63 if (sbr->f[ch][l] == g)
|
|
64 {
|
|
65 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
66 {
|
|
67 if (l == 0)
|
|
68 E_prev = sbr->E_prev[ch][k];
|
|
69 else
|
|
70 E_prev = sbr->E[ch][k][l - 1];
|
|
71
|
|
72 sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
|
|
73 }
|
|
74
|
|
75 } else if ((g == 1) && (sbr->f[ch][l] == 0)) {
|
|
76 uint8_t i;
|
|
77
|
|
78 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
79 {
|
|
80 for (i = 0; i < sbr->N_high; i++)
|
|
81 {
|
|
82 if (sbr->f_table_res[HI_RES][i] == sbr->f_table_res[LO_RES][k])
|
|
83 {
|
|
84 if (l == 0)
|
|
85 E_prev = sbr->E_prev[ch][i];
|
|
86 else
|
|
87 E_prev = sbr->E[ch][i][l - 1];
|
|
88
|
|
89 sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
|
|
90 }
|
|
91 }
|
|
92 }
|
|
93
|
|
94 } else if ((g == 0) && (sbr->f[ch][l] == 1)) {
|
|
95 uint8_t i;
|
|
96
|
|
97 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
98 {
|
|
99 for (i = 0; i < sbr->N_low; i++)
|
|
100 {
|
|
101 if ((sbr->f_table_res[LO_RES][i] <= sbr->f_table_res[HI_RES][k]) &&
|
|
102 (sbr->f_table_res[HI_RES][k] < sbr->f_table_res[LO_RES][i + 1]))
|
|
103 {
|
|
104 if (l == 0)
|
|
105 E_prev = sbr->E_prev[ch][i];
|
|
106 else
|
|
107 E_prev = sbr->E[ch][i][l - 1];
|
|
108
|
|
109 sbr->E[ch][k][l] = E_prev + sbr->E[ch][k][l];
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 }
|
|
114 }
|
|
115 }
|
|
116
|
|
117 #if 0
|
|
118 if (sbr->frame == 23)
|
|
119 {
|
|
120 int l, k;
|
|
121
|
|
122 for (l = 0; l < sbr->L_E[ch]; l++)
|
|
123 {
|
|
124 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
125 {
|
|
126 //printf("l:%d k:%d E:%d\n",l,k, sbr->E[ch][k][l]);
|
|
127 printf("%d\n", sbr->E[ch][k][l]);
|
|
128 }
|
|
129 }
|
|
130 printf("\n");
|
|
131 }
|
|
132 #endif
|
|
133 }
|
|
134
|
|
135 void extract_noise_floor_data(sbr_info *sbr, uint8_t ch)
|
|
136 {
|
|
137 uint8_t l, k;
|
|
138
|
|
139 for (l = 0; l < sbr->L_Q[ch]; l++)
|
|
140 {
|
|
141 if (sbr->bs_df_noise[ch][l] == 0)
|
|
142 {
|
|
143 for (k = 1; k < sbr->N_Q; k++)
|
|
144 {
|
|
145 sbr->Q[ch][k][l] = sbr->Q[ch][k][l] + sbr->Q[ch][k-1][l];
|
|
146 }
|
|
147 } else {
|
|
148 if (l == 0)
|
|
149 {
|
|
150 for (k = 0; k < sbr->N_Q; k++)
|
|
151 {
|
|
152 sbr->Q[ch][k][l] = sbr->Q_prev[ch][k] + sbr->Q[ch][k][0];
|
|
153 }
|
|
154 } else {
|
|
155 for (k = 0; k < sbr->N_Q; k++)
|
|
156 {
|
|
157 sbr->Q[ch][k][l] = sbr->Q[ch][k][l - 1] + sbr->Q[ch][k][l];
|
|
158 }
|
|
159 }
|
|
160 }
|
|
161 }
|
|
162
|
|
163 #if 0
|
|
164 if (sbr->frame == 23)
|
|
165 {
|
|
166 int l, k;
|
|
167
|
|
168 for (l = 0; l < sbr->L_Q[ch]; l++)
|
|
169 {
|
|
170 for (k = 0; k < sbr->N_Q; k++)
|
|
171 {
|
|
172 //printf("l:%d k:%d E:%d\n",l,k, sbr->E[ch][k][l]);
|
|
173 printf("%d\n", sbr->Q[ch][k][l]);
|
|
174 }
|
|
175 }
|
|
176 printf("\n");
|
|
177 }
|
|
178 #endif
|
|
179 }
|
|
180
|
|
181 /* FIXME: pow() not needed */
|
|
182 void envelope_noise_dequantisation(sbr_info *sbr, uint8_t ch)
|
|
183 {
|
|
184 if (sbr->bs_coupling == 0)
|
|
185 {
|
|
186 uint8_t l, k;
|
|
187 #ifdef FIXED_POINT
|
|
188 uint8_t amp = (sbr->amp_res[ch]) ? 0 : 1;
|
|
189 #else
|
|
190 real_t amp = (sbr->amp_res[ch]) ? 1.0 : 0.5;
|
|
191 #endif
|
|
192
|
|
193 for (l = 0; l < sbr->L_E[ch]; l++)
|
|
194 {
|
|
195 for (k = 0; k < sbr->n[sbr->f[ch][l]]; k++)
|
|
196 {
|
|
197 #ifndef FIXED_POINT
|
|
198 /* +6 for the *64 */
|
|
199 sbr->E_orig[ch][k][l] = pow(2, sbr->E[ch][k][l]*amp + 6);
|
|
200 #else
|
|
201 int8_t exp;
|
|
202
|
|
203 /* +6 for the *64 and -10 for the /32 in the synthesis QMF
|
|
204 * since this is a energy value: (x/32)^2 = (x^2)/1024
|
|
205 */
|
|
206 exp = (sbr->E[ch][k][l] >> amp) + 6 - 10;
|
|
207
|
|
208 // printf("%d\n", exp);
|
|
209
|
|
210 if (exp < 0)
|
|
211 sbr->E_orig[ch][k][l] = 0; //REAL_CONST(1) >> -exp;
|
|
212 else
|
|
213 sbr->E_orig[ch][k][l] = 1 << exp; //REAL_CONST(1) << exp;
|
|
214
|
|
215 if (amp && (sbr->E[ch][k][l] & 1))
|
|
216 sbr->E_orig[ch][k][l] = MUL(sbr->E_orig[ch][k][l], REAL_CONST(1.414213562));
|
|
217 #endif
|
|
218 }
|
|
219 }
|
|
220
|
|
221 for (l = 0; l < sbr->L_Q[ch]; l++)
|
|
222 {
|
|
223 for (k = 0; k < sbr->N_Q; k++)
|
|
224 {
|
|
225 if (sbr->Q[ch][k][l] < 0 || sbr->Q[ch][k][l] > 30)
|
|
226 sbr->Q_orig[ch][k][l] = 0;
|
|
227 else {
|
|
228 #ifndef FIXED_POINT
|
|
229 sbr->Q_orig[ch][k][l] = pow(2, NOISE_FLOOR_OFFSET - sbr->Q[ch][k][l]);
|
|
230 #else
|
|
231 int8_t exp = NOISE_FLOOR_OFFSET - sbr->Q[ch][k][l];
|
|
232 if (exp < 0)
|
|
233 sbr->Q_orig[ch][k][l] = REAL_CONST(1) >> -exp;
|
|
234 else
|
|
235 sbr->Q_orig[ch][k][l] = REAL_CONST(1) << exp;
|
|
236 #endif
|
|
237 }
|
|
238 }
|
|
239 }
|
|
240 }
|
|
241 }
|
|
242
|
|
243 void unmap_envelope_noise(sbr_info *sbr)
|
|
244 {
|
|
245 uint8_t l, k;
|
|
246 #ifdef FIXED_POINT
|
|
247 uint8_t amp0 = (sbr->amp_res[0]) ? 0 : 1;
|
|
248 uint8_t amp1 = (sbr->amp_res[1]) ? 0 : 1;
|
|
249 #else
|
|
250 real_t amp0 = (sbr->amp_res[0]) ? 1.0 : 0.5;
|
|
251 real_t amp1 = (sbr->amp_res[1]) ? 1.0 : 0.5;
|
|
252 #endif
|
|
253
|
|
254 for (l = 0; l < sbr->L_E[0]; l++)
|
|
255 {
|
|
256 for (k = 0; k < sbr->n[sbr->f[0][l]]; k++)
|
|
257 {
|
|
258 real_t l_temp, r_temp;
|
|
259
|
|
260 #ifdef FIXED_POINT
|
|
261 int8_t exp;
|
|
262
|
|
263 /* +6: * 64 ; +1: * 2 ; -10: /1024 QMF */
|
|
264 exp = (sbr->E[0][k][l] >> amp0) - 3;
|
|
265
|
|
266 // printf("%d\n", exp);
|
|
267
|
|
268 if (exp < 0)
|
|
269 l_temp = REAL_CONST(1) >> -exp;
|
|
270 else
|
|
271 l_temp = REAL_CONST(1) << exp;
|
|
272
|
|
273 if (amp0 && (sbr->E[0][k][l] & 1))
|
|
274 l_temp = MUL(l_temp, REAL_CONST(1.414213562373095));
|
|
275
|
|
276 /* UN_MAP removed: (x / 4096) same as (x >> 12) */
|
|
277 exp = (sbr->E[1][k][l] >> amp1) - 12;
|
|
278
|
|
279 // printf("%d\n", exp);
|
|
280
|
|
281 if (exp < 0)
|
|
282 r_temp = REAL_CONST(1) >> -exp;
|
|
283 else
|
|
284 r_temp = REAL_CONST(1) << exp;
|
|
285
|
|
286 if (amp1 && (sbr->E[1][k][l] & 1))
|
|
287 r_temp = MUL(r_temp, REAL_CONST(1.414213562373095));
|
|
288 #else
|
|
289 /* +6: * 64 ; +1: * 2 */
|
|
290 l_temp = pow(2, sbr->E[0][k][l]*amp0 + 7);
|
|
291 /* UN_MAP removed: (x / 4096) same as (x >> 12) */
|
|
292 r_temp = pow(2, sbr->E[1][k][l]*amp1 - 12);
|
|
293 #endif
|
|
294
|
|
295
|
|
296 #ifdef FIXED_POINT
|
|
297 {
|
|
298 real_t tmp = REAL_CONST(1.0) + r_temp;
|
|
299 sbr->E_orig[1][k][l] = SBR_DIV(l_temp, tmp);
|
|
300 }
|
|
301 #else
|
|
302 sbr->E_orig[1][k][l] = l_temp / (1.0 + r_temp);
|
|
303 #endif
|
|
304 sbr->E_orig[0][k][l] = MUL(r_temp, sbr->E_orig[1][k][l]);
|
|
305
|
|
306 #ifdef FIXED_POINT
|
|
307 sbr->E_orig[0][k][l] >>= REAL_BITS;
|
|
308 sbr->E_orig[1][k][l] >>= REAL_BITS;
|
|
309 #endif
|
|
310
|
|
311 //printf("%f\t%f\n", sbr->E_orig[0][k][l] /(float)(1<<REAL_BITS), sbr->E_orig[1][k][l] /(float)(1<<REAL_BITS));
|
|
312 //printf("%f\t%f\n", sbr->E_orig[0][k][l]/1024., sbr->E_orig[1][k][l]/1024.);
|
|
313 }
|
|
314 }
|
|
315 for (l = 0; l < sbr->L_Q[0]; l++)
|
|
316 {
|
|
317 for (k = 0; k < sbr->N_Q; k++)
|
|
318 {
|
|
319 if ((sbr->Q[0][k][l] < 0 || sbr->Q[0][k][l] > 30) ||
|
|
320 (sbr->Q[1][k][l] < 0 || sbr->Q[1][k][l] > 30))
|
|
321 {
|
|
322 sbr->Q_orig[0][k][l] = 0;
|
|
323 sbr->Q_orig[1][k][l] = 0;
|
|
324 } else {
|
|
325 real_t l_temp, r_temp;
|
|
326
|
|
327 #ifndef FIXED_POINT
|
|
328 l_temp = pow(2.0, NOISE_FLOOR_OFFSET - sbr->Q[0][k][l] + 1);
|
|
329 r_temp = pow(2.0, sbr->Q[1][k][l] - 12);
|
|
330 #else
|
|
331 int8_t exp;
|
|
332
|
|
333 exp = NOISE_FLOOR_OFFSET - sbr->Q[0][k][l] + 1;
|
|
334 if (exp < 0)
|
|
335 l_temp = REAL_CONST(1) >> -exp;
|
|
336 else
|
|
337 l_temp = REAL_CONST(1) << exp;
|
|
338
|
|
339 exp = sbr->Q[1][k][l] - 12;
|
|
340 if (exp < 0)
|
|
341 r_temp = REAL_CONST(1) >> -exp;
|
|
342 else
|
|
343 r_temp = REAL_CONST(1) << exp;
|
|
344 #endif
|
|
345
|
|
346 #ifdef FIXED_POINT
|
|
347 sbr->Q_orig[1][k][l] = SBR_DIV(l_temp, (REAL_CONST(1.0) + r_temp));
|
|
348 #else
|
|
349 sbr->Q_orig[1][k][l] = l_temp / (1.0 + r_temp);
|
|
350 #endif
|
|
351 sbr->Q_orig[0][k][l] = MUL(r_temp, sbr->Q_orig[1][k][l]);
|
|
352 }
|
|
353 }
|
|
354 }
|
|
355 }
|
|
356
|
|
357 #endif
|