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_qmf.c,v 1.5 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
|
|
34 #include <stdlib.h>
|
|
35 #include <string.h>
|
|
36 #include "sbr_dct.h"
|
|
37 #include "sbr_qmf.h"
|
|
38 #include "sbr_syntax.h"
|
|
39
|
|
40
|
|
41 qmfa_info *qmfa_init(uint8_t channels)
|
|
42 {
|
|
43 #if 0
|
|
44 int16_t n;
|
|
45 #endif
|
|
46 int size = 0;
|
|
47 qmfa_info *qmfa = (qmfa_info*)malloc(sizeof(qmfa_info));
|
|
48 qmfa->x = (real_t*)malloc(channels * 10 * sizeof(real_t));
|
|
49 memset(qmfa->x, 0, channels * 10 * sizeof(real_t));
|
|
50
|
|
51 qmfa->channels = channels;
|
|
52
|
|
53 if (channels == 32)
|
|
54 {
|
|
55 #if 0
|
|
56 for (n = 0; n < 32; n++)
|
|
57 {
|
|
58 qmfa->post_exp_re[n] = cos((M_PI/32.)*(0.75*n + 0.375));
|
|
59 qmfa->post_exp_im[n] = sin((M_PI/32.)*(0.75*n + 0.375));
|
|
60 }
|
|
61 #endif
|
|
62 } else if (channels == 64) {
|
|
63 #if 0
|
|
64 for (n = 0; n < 2*channels; n++)
|
|
65 {
|
|
66 qmfa->pre_exp_re[n] = cos(M_PI*n/(2.*channels));
|
|
67 qmfa->pre_exp_im[n] = sin(M_PI*n/(2.*channels));
|
|
68 }
|
|
69 for (n = 0; n < 64; n++)
|
|
70 {
|
|
71 qmfa->post_exp_re[n] = cos(M_PI*(2*n+1)/(2.*128.));
|
|
72 qmfa->post_exp_im[n] = sin(M_PI*(2*n+1)/(2.*128.));
|
|
73 }
|
|
74 #endif
|
|
75 }
|
|
76
|
|
77 return qmfa;
|
|
78 }
|
|
79
|
|
80 void qmfa_end(qmfa_info *qmfa)
|
|
81 {
|
|
82 if (qmfa)
|
|
83 {
|
|
84 if (qmfa->x) free(qmfa->x);
|
|
85 free(qmfa);
|
|
86 }
|
|
87 }
|
|
88
|
|
89 void sbr_qmf_analysis_32(qmfa_info *qmfa, const real_t *input,
|
|
90 qmf_t *X, uint8_t offset)
|
|
91 {
|
|
92 uint8_t l;
|
|
93 real_t u[64];
|
|
94 #ifndef SBR_LOW_POWER
|
|
95 real_t x[64], y[64];
|
|
96 #else
|
|
97 real_t y[32];
|
|
98 #endif
|
|
99 const real_t *inptr = input;
|
|
100
|
|
101 /* qmf subsample l */
|
|
102 for (l = 0; l < 32; l++)
|
|
103 {
|
|
104 int16_t n;
|
|
105
|
|
106 /* shift input buffer x */
|
|
107 memmove(qmfa->x + 32, qmfa->x, (320-32)*sizeof(real_t));
|
|
108
|
|
109 /* add new samples to input buffer x */
|
|
110 for (n = 32 - 1; n >= 0; n--)
|
|
111 {
|
|
112 #ifdef FIXED_POINT
|
|
113 qmfa->x[n] = (*inptr++) >> 5;
|
|
114 #else
|
|
115 qmfa->x[n] = *inptr++;
|
|
116 #endif
|
|
117 }
|
|
118
|
|
119 /* window and summation to create array u */
|
|
120 for (n = 0; n < 64; n++)
|
|
121 {
|
|
122 u[n] = MUL_R_C(qmfa->x[n], qmf_c_2[n]) +
|
|
123 MUL_R_C(qmfa->x[n + 64], qmf_c_2[n + 64]) +
|
|
124 MUL_R_C(qmfa->x[n + 128], qmf_c_2[n + 128]) +
|
|
125 MUL_R_C(qmfa->x[n + 192], qmf_c_2[n + 192]) +
|
|
126 MUL_R_C(qmfa->x[n + 256], qmf_c_2[n + 256]);
|
|
127 }
|
|
128
|
|
129 /* calculate 32 subband samples by introducing X */
|
|
130 #ifdef SBR_LOW_POWER
|
|
131 y[0] = u[48];
|
|
132 for (n = 1; n < 16; n++)
|
|
133 y[n] = u[n+48] + u[48-n];
|
|
134 for (n = 16; n < 32; n++)
|
|
135 y[n] = -u[n-16] + u[48-n];
|
|
136
|
|
137 DCT3_32_unscaled(u, y);
|
|
138
|
|
139 for (n = 0; n < 32; n++)
|
|
140 {
|
|
141 #ifdef FIXED_POINT
|
|
142 QMF_RE(X[((l + offset)<<5) + n]) = u[n] << 1;
|
|
143 #else
|
|
144 QMF_RE(X[((l + offset)<<5) + n]) = 2. * u[n];
|
|
145 #endif
|
|
146
|
|
147 #if 0
|
|
148 if (fabs(QMF_RE(X[((l + offset)<<5) + n])) > pow(2,20))
|
|
149 {
|
|
150 printf("%f\n", QMF_RE(X[((l + offset)<<5) + n]));
|
|
151 }
|
|
152 #endif
|
|
153 }
|
|
154 #else
|
|
155 x[0] = u[0];
|
|
156 x[63] = u[32];
|
|
157 for (n = 2; n < 64; n += 2)
|
|
158 {
|
|
159 x[n-1] = u[(n>>1)];
|
|
160 x[n] = -u[64-(n>>1)];
|
|
161 }
|
|
162
|
|
163 DCT4_64(y, x);
|
|
164
|
|
165 for (n = 0; n < 32; n++)
|
|
166 {
|
|
167 #ifdef FIXED_POINT
|
|
168 QMF_RE(X[((l + offset)<<5) + n]) = y[n] << 1;
|
|
169 QMF_IM(X[((l + offset)<<5) + n]) = -y[63-n] << 1;
|
|
170 #else
|
|
171 QMF_RE(X[((l + offset)<<5) + n]) = 2. * y[n];
|
|
172 QMF_IM(X[((l + offset)<<5) + n]) = -2. * y[63-n];
|
|
173 #endif
|
|
174
|
|
175 #if 0
|
|
176 if (fabs(QMF_RE(X[((l + offset)<<5) + n])) > pow(2,20))
|
|
177 {
|
|
178 printf("%f\n", QMF_RE(X[((l + offset)<<5) + n]));
|
|
179 }
|
|
180 if (fabs(QMF_IM(X[((l + offset)<<5) + n])) > pow(2,20))
|
|
181 {
|
|
182 printf("%f\n", QMF_IM(X[((l + offset)<<5) + n]));
|
|
183 }
|
|
184 #endif
|
|
185 }
|
|
186 #endif
|
|
187 }
|
|
188 }
|
|
189
|
|
190 qmfs_info *qmfs_init(uint8_t channels)
|
|
191 {
|
|
192 int size = 0;
|
|
193 qmfs_info *qmfs = (qmfs_info*)malloc(sizeof(qmfs_info));
|
|
194 qmfs->v = (real_t*)malloc(channels * 20 * sizeof(real_t));
|
|
195 memset(qmfs->v, 0, channels * 20 * sizeof(real_t));
|
|
196
|
|
197 qmfs->channels = channels;
|
|
198
|
|
199 return qmfs;
|
|
200 }
|
|
201
|
|
202 void qmfs_end(qmfs_info *qmfs)
|
|
203 {
|
|
204 if (qmfs)
|
|
205 {
|
|
206 if (qmfs->v) free(qmfs->v);
|
|
207 free(qmfs);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 #if 0
|
|
212 void sbr_qmf_synthesis_32(qmfs_info *qmfs, const complex_t *X,
|
|
213 real_t *output)
|
|
214 {
|
|
215 uint8_t l;
|
|
216 int16_t n, k;
|
|
217 real_t w[320];
|
|
218 complex_t x[128];
|
|
219 real_t *outptr = output;
|
|
220
|
|
221 /* qmf subsample l */
|
|
222 for (l = 0; l < 32; l++)
|
|
223 {
|
|
224 /* shift buffer */
|
|
225 for (n = 640 - 1; n >= 64; n--)
|
|
226 {
|
|
227 qmfs->v[n] = qmfs->v[n - 64];
|
|
228 }
|
|
229
|
|
230 /* calculate 64 samples */
|
|
231 memset(x, 0, 2*64*sizeof(real_t));
|
|
232
|
|
233 for (k = 0; k < 32; k++)
|
|
234 {
|
|
235 real_t er, ei, Xr, Xi;
|
|
236 er = qmfs->pre_exp_re[k];
|
|
237 ei = qmfs->pre_exp_im[k];
|
|
238
|
|
239 Xr = RE(X[l * 32 + k]);
|
|
240 Xi = IM(X[l * 32 + k]);
|
|
241 RE(x[k]) = Xr * er - Xi * ei;
|
|
242 IM(x[k]) = Xi * er + Xr * ei;
|
|
243 }
|
|
244
|
|
245 cfftb(qmfs->cffts, x);
|
|
246
|
|
247 for (n = 0; n < 64; n++)
|
|
248 {
|
|
249 real_t er, ei;
|
|
250 er = qmfs->post_exp_re[n];
|
|
251 ei = qmfs->post_exp_im[n];
|
|
252
|
|
253 qmfs->v[n] = RE(x[n]) * er - IM(x[n]) * ei;
|
|
254 }
|
|
255
|
|
256 for (n = 0; n < 5; n++)
|
|
257 {
|
|
258 for (k = 0; k < 32; k++)
|
|
259 {
|
|
260 w[64 * n + k] = qmfs->v[128 * n + k];
|
|
261 w[64 * n + 32 + k] = qmfs->v[128 * n + 96 + k];
|
|
262 }
|
|
263 }
|
|
264
|
|
265 /* window */
|
|
266 for (n = 0; n < 320; n++)
|
|
267 {
|
|
268 w[n] *= qmf_c_2[n];
|
|
269 }
|
|
270
|
|
271 /* calculate 32 output samples */
|
|
272 for (k = 0; k < 32; k++)
|
|
273 {
|
|
274 real_t sample = 0.0;
|
|
275
|
|
276 for (n = 0; n < 10; n++)
|
|
277 {
|
|
278 sample += w[32 * n + k];
|
|
279 }
|
|
280
|
|
281 *outptr++ = sample;
|
|
282 }
|
|
283 }
|
|
284 }
|
|
285 #endif
|
|
286
|
|
287 void sbr_qmf_synthesis_64(qmfs_info *qmfs, const qmf_t *X,
|
|
288 real_t *output)
|
|
289 {
|
|
290 uint8_t l;
|
|
291 int16_t n, k;
|
|
292 #ifdef SBR_LOW_POWER
|
|
293 real_t x[64];
|
|
294 #else
|
|
295 real_t x1[64], x2[64];
|
|
296 #endif
|
|
297 real_t *outptr = output;
|
|
298
|
|
299
|
|
300 /* qmf subsample l */
|
|
301 for (l = 0; l < 32; l++)
|
|
302 {
|
|
303 /* shift buffer */
|
|
304 memmove(qmfs->v + 128, qmfs->v, (1280-128)*sizeof(real_t));
|
|
305
|
|
306 /* calculate 128 samples */
|
|
307 #ifdef SBR_LOW_POWER
|
|
308 for (k = 0; k < 64; k++)
|
|
309 {
|
|
310 #ifdef FIXED_POINT
|
|
311 x[k] = QMF_RE(X[(l<<6) + k]);
|
|
312 #else
|
|
313 x[k] = QMF_RE(X[(l<<6) + k]) / 32.;
|
|
314 #endif
|
|
315 }
|
|
316
|
|
317 DCT2_64_unscaled(x, x);
|
|
318
|
|
319 for (n = 0; n < 64; n++)
|
|
320 {
|
|
321 qmfs->v[n+32] = x[n];
|
|
322 }
|
|
323 qmfs->v[0] = qmfs->v[64];
|
|
324 for (n = 1; n < 32; n++)
|
|
325 {
|
|
326 qmfs->v[32 - n] = qmfs->v[n + 32];
|
|
327 qmfs->v[n + 96] = -qmfs->v[96 - n];
|
|
328 }
|
|
329 #else
|
|
330 for (k = 0; k < 64; k++)
|
|
331 {
|
|
332 x1[k] = QMF_RE(X[(l<<6) + k])/64.;
|
|
333 x2[k] = QMF_IM(X[(l<<6) + k])/64.;
|
|
334 }
|
|
335
|
|
336 DCT4_64(x1, x1);
|
|
337 DST4_64(x2, x2);
|
|
338
|
|
339 for (n = 0; n < 64; n++)
|
|
340 {
|
|
341 qmfs->v[n] = x2[n] - x1[n];
|
|
342 qmfs->v[127-n] = x2[n] + x1[n];
|
|
343 }
|
|
344 #endif
|
|
345
|
|
346 /* calculate 64 output samples and window */
|
|
347 for (k = 0; k < 64; k++)
|
|
348 {
|
|
349 *outptr++ = MUL_R_C(qmfs->v[k], qmf_c[k]) +
|
|
350 MUL_R_C(qmfs->v[192 + k], qmf_c[64 + k]) +
|
|
351 MUL_R_C(qmfs->v[256 + k], qmf_c[128 + k]) +
|
|
352 MUL_R_C(qmfs->v[256 + 192 + k], qmf_c[128 + 64 + k]) +
|
|
353 MUL_R_C(qmfs->v[512 + k], qmf_c[256 + k]) +
|
|
354 MUL_R_C(qmfs->v[512 + 192 + k], qmf_c[256 + 64 + k]) +
|
|
355 MUL_R_C(qmfs->v[768 + k], qmf_c[384 + k]) +
|
|
356 MUL_R_C(qmfs->v[768 + 192 + k], qmf_c[384 + 64 + k]) +
|
|
357 MUL_R_C(qmfs->v[1024 + k], qmf_c[512 + k]) +
|
|
358 MUL_R_C(qmfs->v[1024 + 192 + k], qmf_c[512 + 64 + k]);
|
|
359 }
|
|
360 }
|
|
361 }
|
|
362
|
|
363 #endif
|