1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero 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 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24 **
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27 **
28 ** $Id: filtbank.c,v 1.46 2009/01/26 23:51:15 menno Exp $
29 **/
30
31 #include "common.h"
32 #include "structs.h"
33
34 #include <stdlib.h>
35 #include <string.h>
36 #ifdef _WIN32_WCE
37 #define assert(x)
38 #else
39 #include <assert.h>
40 #endif
41
42 #include "filtbank.h"
43 #include "syntax.h"
44 #include "kbd_win.h"
45 #include "sine_win.h"
46 #include "mdct.h"
47
48
filter_bank_init(uint16_t frame_len)49 fb_info *filter_bank_init(uint16_t frame_len)
50 {
51 uint16_t nshort = frame_len/8;
52 #ifdef LD_DEC
53 uint16_t frame_len_ld = frame_len/2;
54 #endif
55
56 fb_info *fb = (fb_info*)faad_malloc(sizeof(fb_info));
57 memset(fb, 0, sizeof(fb_info));
58
59 /* normal */
60 fb->mdct256 = faad_mdct_init(2*nshort);
61 fb->mdct2048 = faad_mdct_init(2*frame_len);
62 #ifdef LD_DEC
63 /* LD */
64 fb->mdct1024 = faad_mdct_init(2*frame_len_ld);
65 #endif
66
67 #ifdef ALLOW_SMALL_FRAMELENGTH
68 if (frame_len == 1024)
69 {
70 #endif
71 fb->long_window[0] = sine_long_1024;
72 fb->short_window[0] = sine_short_128;
73 fb->long_window[1] = kbd_long_1024;
74 fb->short_window[1] = kbd_short_128;
75 #ifdef LD_DEC
76 fb->ld_window[0] = sine_mid_512;
77 fb->ld_window[1] = ld_mid_512;
78 #endif
79 #ifdef ALLOW_SMALL_FRAMELENGTH
80 } else /* (frame_len == 960) */ {
81 fb->long_window[0] = sine_long_960;
82 fb->short_window[0] = sine_short_120;
83 fb->long_window[1] = kbd_long_960;
84 fb->short_window[1] = kbd_short_120;
85 #ifdef LD_DEC
86 fb->ld_window[0] = sine_mid_480;
87 fb->ld_window[1] = ld_mid_480;
88 #endif
89 }
90 #endif
91
92 return fb;
93 }
94
filter_bank_end(fb_info * fb)95 void filter_bank_end(fb_info *fb)
96 {
97 if (fb != NULL)
98 {
99 #ifdef PROFILE
100 printf("FB: %I64d cycles\n", fb->cycles);
101 #endif
102
103 faad_mdct_end(fb->mdct256);
104 faad_mdct_end(fb->mdct2048);
105 #ifdef LD_DEC
106 faad_mdct_end(fb->mdct1024);
107 #endif
108
109 faad_free(fb);
110 }
111 }
112
imdct_long(fb_info * fb,real_t * in_data,real_t * out_data,uint16_t len)113 static INLINE void imdct_long(fb_info *fb, real_t *in_data, real_t *out_data, uint16_t len)
114 {
115 #ifdef LD_DEC
116 mdct_info *mdct = NULL;
117
118 switch (len)
119 {
120 case 2048:
121 case 1920:
122 mdct = fb->mdct2048;
123 break;
124 case 1024:
125 case 960:
126 mdct = fb->mdct1024;
127 break;
128 }
129
130 faad_imdct(mdct, in_data, out_data);
131 #else
132 faad_imdct(fb->mdct2048, in_data, out_data);
133 #endif
134 }
135
136
137 #ifdef LTP_DEC
mdct(fb_info * fb,real_t * in_data,real_t * out_data,uint16_t len)138 static INLINE void mdct(fb_info *fb, real_t *in_data, real_t *out_data, uint16_t len)
139 {
140 mdct_info *mdct = NULL;
141
142 switch (len)
143 {
144 case 2048:
145 case 1920:
146 mdct = fb->mdct2048;
147 break;
148 case 256:
149 case 240:
150 mdct = fb->mdct256;
151 break;
152 #ifdef LD_DEC
153 case 1024:
154 case 960:
155 mdct = fb->mdct1024;
156 break;
157 #endif
158 }
159
160 faad_mdct(mdct, in_data, out_data);
161 }
162 #endif
163
ifilter_bank(fb_info * fb,uint8_t window_sequence,uint8_t window_shape,uint8_t window_shape_prev,real_t * freq_in,real_t * time_out,real_t * overlap,uint8_t object_type,uint16_t frame_len)164 void ifilter_bank(fb_info *fb, uint8_t window_sequence, uint8_t window_shape,
165 uint8_t window_shape_prev, real_t *freq_in,
166 real_t *time_out, real_t *overlap,
167 uint8_t object_type, uint16_t frame_len)
168 {
169 int16_t i;
170 ALIGN real_t transf_buf[2*1024] = {0};
171
172 const real_t *window_long = NULL;
173 const real_t *window_long_prev = NULL;
174 const real_t *window_short = NULL;
175 const real_t *window_short_prev = NULL;
176
177 uint16_t nlong = frame_len;
178 uint16_t nshort = frame_len/8;
179 uint16_t trans = nshort/2;
180
181 uint16_t nflat_ls = (nlong-nshort)/2;
182
183 #ifdef PROFILE
184 int64_t count = faad_get_ts();
185 #endif
186
187 /* select windows of current frame and previous frame (Sine or KBD) */
188 #ifdef LD_DEC
189 if (object_type == LD)
190 {
191 window_long = fb->ld_window[window_shape];
192 window_long_prev = fb->ld_window[window_shape_prev];
193 } else {
194 #endif
195 window_long = fb->long_window[window_shape];
196 window_long_prev = fb->long_window[window_shape_prev];
197 window_short = fb->short_window[window_shape];
198 window_short_prev = fb->short_window[window_shape_prev];
199 #ifdef LD_DEC
200 }
201 #endif
202
203 #if 0
204 for (i = 0; i < 1024; i++)
205 {
206 printf("%d\n", freq_in[i]);
207 }
208 #endif
209
210 #if 0
211 printf("%d %d\n", window_sequence, window_shape);
212 #endif
213
214 switch (window_sequence)
215 {
216 case ONLY_LONG_SEQUENCE:
217 /* perform iMDCT */
218 imdct_long(fb, freq_in, transf_buf, 2*nlong);
219
220 /* add second half output of previous frame to windowed output of current frame */
221 for (i = 0; i < nlong; i+=4)
222 {
223 time_out[i] = overlap[i] + MUL_F(transf_buf[i],window_long_prev[i]);
224 time_out[i+1] = overlap[i+1] + MUL_F(transf_buf[i+1],window_long_prev[i+1]);
225 time_out[i+2] = overlap[i+2] + MUL_F(transf_buf[i+2],window_long_prev[i+2]);
226 time_out[i+3] = overlap[i+3] + MUL_F(transf_buf[i+3],window_long_prev[i+3]);
227 }
228
229 /* window the second half and save as overlap for next frame */
230 for (i = 0; i < nlong; i+=4)
231 {
232 overlap[i] = MUL_F(transf_buf[nlong+i],window_long[nlong-1-i]);
233 overlap[i+1] = MUL_F(transf_buf[nlong+i+1],window_long[nlong-2-i]);
234 overlap[i+2] = MUL_F(transf_buf[nlong+i+2],window_long[nlong-3-i]);
235 overlap[i+3] = MUL_F(transf_buf[nlong+i+3],window_long[nlong-4-i]);
236 }
237 break;
238
239 case LONG_START_SEQUENCE:
240 /* perform iMDCT */
241 imdct_long(fb, freq_in, transf_buf, 2*nlong);
242
243 /* add second half output of previous frame to windowed output of current frame */
244 for (i = 0; i < nlong; i+=4)
245 {
246 time_out[i] = overlap[i] + MUL_F(transf_buf[i],window_long_prev[i]);
247 time_out[i+1] = overlap[i+1] + MUL_F(transf_buf[i+1],window_long_prev[i+1]);
248 time_out[i+2] = overlap[i+2] + MUL_F(transf_buf[i+2],window_long_prev[i+2]);
249 time_out[i+3] = overlap[i+3] + MUL_F(transf_buf[i+3],window_long_prev[i+3]);
250 }
251
252 /* window the second half and save as overlap for next frame */
253 /* construct second half window using padding with 1's and 0's */
254 for (i = 0; i < nflat_ls; i++)
255 overlap[i] = transf_buf[nlong+i];
256 for (i = 0; i < nshort; i++)
257 overlap[nflat_ls+i] = MUL_F(transf_buf[nlong+nflat_ls+i],window_short[nshort-i-1]);
258 for (i = 0; i < nflat_ls; i++)
259 overlap[nflat_ls+nshort+i] = 0;
260 break;
261
262 case EIGHT_SHORT_SEQUENCE:
263 /* perform iMDCT for each short block */
264 faad_imdct(fb->mdct256, freq_in+0*nshort, transf_buf+2*nshort*0);
265 faad_imdct(fb->mdct256, freq_in+1*nshort, transf_buf+2*nshort*1);
266 faad_imdct(fb->mdct256, freq_in+2*nshort, transf_buf+2*nshort*2);
267 faad_imdct(fb->mdct256, freq_in+3*nshort, transf_buf+2*nshort*3);
268 faad_imdct(fb->mdct256, freq_in+4*nshort, transf_buf+2*nshort*4);
269 faad_imdct(fb->mdct256, freq_in+5*nshort, transf_buf+2*nshort*5);
270 faad_imdct(fb->mdct256, freq_in+6*nshort, transf_buf+2*nshort*6);
271 faad_imdct(fb->mdct256, freq_in+7*nshort, transf_buf+2*nshort*7);
272
273 /* add second half output of previous frame to windowed output of current frame */
274 for (i = 0; i < nflat_ls; i++)
275 time_out[i] = overlap[i];
276 for(i = 0; i < nshort; i++)
277 {
278 time_out[nflat_ls+ i] = overlap[nflat_ls+ i] + MUL_F(transf_buf[nshort*0+i],window_short_prev[i]);
279 time_out[nflat_ls+1*nshort+i] = overlap[nflat_ls+nshort*1+i] + MUL_F(transf_buf[nshort*1+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*2+i],window_short[i]);
280 time_out[nflat_ls+2*nshort+i] = overlap[nflat_ls+nshort*2+i] + MUL_F(transf_buf[nshort*3+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*4+i],window_short[i]);
281 time_out[nflat_ls+3*nshort+i] = overlap[nflat_ls+nshort*3+i] + MUL_F(transf_buf[nshort*5+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*6+i],window_short[i]);
282 if (i < trans)
283 time_out[nflat_ls+4*nshort+i] = overlap[nflat_ls+nshort*4+i] + MUL_F(transf_buf[nshort*7+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*8+i],window_short[i]);
284 }
285
286 /* window the second half and save as overlap for next frame */
287 for(i = 0; i < nshort; i++)
288 {
289 if (i >= trans)
290 overlap[nflat_ls+4*nshort+i-nlong] = MUL_F(transf_buf[nshort*7+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*8+i],window_short[i]);
291 overlap[nflat_ls+5*nshort+i-nlong] = MUL_F(transf_buf[nshort*9+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*10+i],window_short[i]);
292 overlap[nflat_ls+6*nshort+i-nlong] = MUL_F(transf_buf[nshort*11+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*12+i],window_short[i]);
293 overlap[nflat_ls+7*nshort+i-nlong] = MUL_F(transf_buf[nshort*13+i],window_short[nshort-1-i]) + MUL_F(transf_buf[nshort*14+i],window_short[i]);
294 overlap[nflat_ls+8*nshort+i-nlong] = MUL_F(transf_buf[nshort*15+i],window_short[nshort-1-i]);
295 }
296 for (i = 0; i < nflat_ls; i++)
297 overlap[nflat_ls+nshort+i] = 0;
298 break;
299
300 case LONG_STOP_SEQUENCE:
301 /* perform iMDCT */
302 imdct_long(fb, freq_in, transf_buf, 2*nlong);
303
304 /* add second half output of previous frame to windowed output of current frame */
305 /* construct first half window using padding with 1's and 0's */
306 for (i = 0; i < nflat_ls; i++)
307 time_out[i] = overlap[i];
308 for (i = 0; i < nshort; i++)
309 time_out[nflat_ls+i] = overlap[nflat_ls+i] + MUL_F(transf_buf[nflat_ls+i],window_short_prev[i]);
310 for (i = 0; i < nflat_ls; i++)
311 time_out[nflat_ls+nshort+i] = overlap[nflat_ls+nshort+i] + transf_buf[nflat_ls+nshort+i];
312
313 /* window the second half and save as overlap for next frame */
314 for (i = 0; i < nlong; i++)
315 overlap[i] = MUL_F(transf_buf[nlong+i],window_long[nlong-1-i]);
316 break;
317 }
318
319 #if 0
320 for (i = 0; i < 1024; i++)
321 {
322 printf("%d\n", time_out[i]);
323 //printf("0x%.8X\n", time_out[i]);
324 }
325 #endif
326
327
328 #ifdef PROFILE
329 count = faad_get_ts() - count;
330 fb->cycles += count;
331 #endif
332 }
333
334
335 #ifdef LTP_DEC
336 /* only works for LTP -> no overlapping, no short blocks */
filter_bank_ltp(fb_info * fb,uint8_t window_sequence,uint8_t window_shape,uint8_t window_shape_prev,real_t * in_data,real_t * out_mdct,uint8_t object_type,uint16_t frame_len)337 void filter_bank_ltp(fb_info *fb, uint8_t window_sequence, uint8_t window_shape,
338 uint8_t window_shape_prev, real_t *in_data, real_t *out_mdct,
339 uint8_t object_type, uint16_t frame_len)
340 {
341 int16_t i;
342 ALIGN real_t windowed_buf[2*1024] = {0};
343
344 const real_t *window_long = NULL;
345 const real_t *window_long_prev = NULL;
346 const real_t *window_short = NULL;
347 const real_t *window_short_prev = NULL;
348
349 uint16_t nlong = frame_len;
350 uint16_t nshort = frame_len/8;
351 uint16_t nflat_ls = (nlong-nshort)/2;
352
353 assert(window_sequence != EIGHT_SHORT_SEQUENCE);
354
355 #ifdef LD_DEC
356 if (object_type == LD)
357 {
358 window_long = fb->ld_window[window_shape];
359 window_long_prev = fb->ld_window[window_shape_prev];
360 } else {
361 #endif
362 window_long = fb->long_window[window_shape];
363 window_long_prev = fb->long_window[window_shape_prev];
364 window_short = fb->short_window[window_shape];
365 window_short_prev = fb->short_window[window_shape_prev];
366 #ifdef LD_DEC
367 }
368 #endif
369
370 switch(window_sequence)
371 {
372 case ONLY_LONG_SEQUENCE:
373 for (i = nlong-1; i >= 0; i--)
374 {
375 windowed_buf[i] = MUL_F(in_data[i], window_long_prev[i]);
376 windowed_buf[i+nlong] = MUL_F(in_data[i+nlong], window_long[nlong-1-i]);
377 }
378 mdct(fb, windowed_buf, out_mdct, 2*nlong);
379 break;
380
381 case LONG_START_SEQUENCE:
382 for (i = 0; i < nlong; i++)
383 windowed_buf[i] = MUL_F(in_data[i], window_long_prev[i]);
384 for (i = 0; i < nflat_ls; i++)
385 windowed_buf[i+nlong] = in_data[i+nlong];
386 for (i = 0; i < nshort; i++)
387 windowed_buf[i+nlong+nflat_ls] = MUL_F(in_data[i+nlong+nflat_ls], window_short[nshort-1-i]);
388 for (i = 0; i < nflat_ls; i++)
389 windowed_buf[i+nlong+nflat_ls+nshort] = 0;
390 mdct(fb, windowed_buf, out_mdct, 2*nlong);
391 break;
392
393 case LONG_STOP_SEQUENCE:
394 for (i = 0; i < nflat_ls; i++)
395 windowed_buf[i] = 0;
396 for (i = 0; i < nshort; i++)
397 windowed_buf[i+nflat_ls] = MUL_F(in_data[i+nflat_ls], window_short_prev[i]);
398 for (i = 0; i < nflat_ls; i++)
399 windowed_buf[i+nflat_ls+nshort] = in_data[i+nflat_ls+nshort];
400 for (i = 0; i < nlong; i++)
401 windowed_buf[i+nlong] = MUL_F(in_data[i+nlong], window_long[nlong-1-i]);
402 mdct(fb, windowed_buf, out_mdct, 2*nlong);
403 break;
404 }
405 }
406 #endif
407