comparison Plugins/Input/aac/libfaad2/specrec.c @ 61:fa848bd484d8 trunk

[svn] Move plugins to Plugins/
author nenolod
date Fri, 28 Oct 2005 22:58:11 -0700
parents
children 0a2ad94e8607
comparison
equal deleted inserted replaced
60:1771f253e1b2 61:fa848bd484d8
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: specrec.c,v 1.33 2003/11/12 20:47:59 menno Exp $
26 **/
27
28 /*
29 Spectral reconstruction:
30 - grouping/sectioning
31 - inverse quantization
32 - applying scalefactors
33 */
34
35 #include "common.h"
36 #include "structs.h"
37
38 #include <string.h>
39 #include "specrec.h"
40 #include "syntax.h"
41 #include "iq_table.h"
42 #include "ms.h"
43 #include "is.h"
44 #include "pns.h"
45 #include "tns.h"
46 #include "lt_predict.h"
47 #include "ic_predict.h"
48 #ifdef SSR_DEC
49 #include "ssr.h"
50 #include "ssr_fb.h"
51 #endif
52
53 #ifdef LD_DEC
54 static uint8_t num_swb_512_window[] =
55 {
56 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
57 };
58 static uint8_t num_swb_480_window[] =
59 {
60 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
61 };
62 #endif
63
64 static uint8_t num_swb_960_window[] =
65 {
66 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
67 };
68
69 static uint8_t num_swb_1024_window[] =
70 {
71 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
72 };
73
74 static uint8_t num_swb_128_window[] =
75 {
76 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
77 };
78
79 static uint16_t swb_offset_1024_96[] =
80 {
81 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
82 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240,
83 276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
84 };
85
86 static uint16_t swb_offset_128_96[] =
87 {
88 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
89 };
90
91 static uint16_t swb_offset_1024_64[] =
92 {
93 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
94 64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268,
95 304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824,
96 864, 904, 944, 984, 1024
97 };
98
99 static uint16_t swb_offset_128_64[] =
100 {
101 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
102 };
103
104
105 static uint16_t swb_offset_1024_48[] =
106 {
107 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
108 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
109 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
110 768, 800, 832, 864, 896, 928, 1024
111 };
112
113 #ifdef LD_DEC
114 static uint16_t swb_offset_512_48[] =
115 {
116 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84,
117 92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396,
118 428, 460, 512
119 };
120
121 static uint16_t swb_offset_480_48[] =
122 {
123 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 ,80 ,88,
124 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400,
125 432, 480
126 };
127 #endif
128
129 static uint16_t swb_offset_128_48[] =
130 {
131 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
132 };
133
134 static uint16_t swb_offset_1024_32[] =
135 {
136 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
137 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
138 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
139 768, 800, 832, 864, 896, 928, 960, 992, 1024
140 };
141
142 #ifdef LD_DEC
143 static uint16_t swb_offset_512_32[] =
144 {
145 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80,
146 88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352,
147 384, 416, 448, 480, 512
148 };
149
150 static uint16_t swb_offset_480_32[] =
151 {
152 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80,
153 88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352,
154 384, 416, 448, 480
155 };
156 #endif
157
158 static uint16_t swb_offset_1024_24[] =
159 {
160 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
161 76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220,
162 240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704,
163 768, 832, 896, 960, 1024
164 };
165
166 #ifdef LD_DEC
167 static uint16_t swb_offset_512_24[] =
168 {
169 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
170 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416,
171 448, 480, 512
172 };
173
174 static uint16_t swb_offset_480_24[] =
175 {
176 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120,
177 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480
178 };
179 #endif
180
181 static uint16_t swb_offset_128_24[] =
182 {
183 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
184 };
185
186 static uint16_t swb_offset_1024_16[] =
187 {
188 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124,
189 136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344,
190 368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024
191 };
192
193 static uint16_t swb_offset_128_16[] =
194 {
195 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
196 };
197
198 static uint16_t swb_offset_1024_8[] =
199 {
200 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172,
201 188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448,
202 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024
203 };
204
205 static uint16_t swb_offset_128_8[] =
206 {
207 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
208 };
209
210 static uint16_t *swb_offset_1024_window[] =
211 {
212 swb_offset_1024_96, /* 96000 */
213 swb_offset_1024_96, /* 88200 */
214 swb_offset_1024_64, /* 64000 */
215 swb_offset_1024_48, /* 48000 */
216 swb_offset_1024_48, /* 44100 */
217 swb_offset_1024_32, /* 32000 */
218 swb_offset_1024_24, /* 24000 */
219 swb_offset_1024_24, /* 22050 */
220 swb_offset_1024_16, /* 16000 */
221 swb_offset_1024_16, /* 12000 */
222 swb_offset_1024_16, /* 11025 */
223 swb_offset_1024_8 /* 8000 */
224 };
225
226 #ifdef LD_DEC
227 static uint16_t *swb_offset_512_window[] =
228 {
229 0, /* 96000 */
230 0, /* 88200 */
231 0, /* 64000 */
232 swb_offset_512_48, /* 48000 */
233 swb_offset_512_48, /* 44100 */
234 swb_offset_512_32, /* 32000 */
235 swb_offset_512_24, /* 24000 */
236 swb_offset_512_24, /* 22050 */
237 0, /* 16000 */
238 0, /* 12000 */
239 0, /* 11025 */
240 0 /* 8000 */
241 };
242
243 static uint16_t *swb_offset_480_window[] =
244 {
245 0, /* 96000 */
246 0, /* 88200 */
247 0, /* 64000 */
248 swb_offset_480_48, /* 48000 */
249 swb_offset_480_48, /* 44100 */
250 swb_offset_480_32, /* 32000 */
251 swb_offset_480_24, /* 24000 */
252 swb_offset_480_24, /* 22050 */
253 0, /* 16000 */
254 0, /* 12000 */
255 0, /* 11025 */
256 0 /* 8000 */
257 };
258 #endif
259
260 static uint16_t *swb_offset_128_window[] =
261 {
262 swb_offset_128_96, /* 96000 */
263 swb_offset_128_96, /* 88200 */
264 swb_offset_128_64, /* 64000 */
265 swb_offset_128_48, /* 48000 */
266 swb_offset_128_48, /* 44100 */
267 swb_offset_128_48, /* 32000 */
268 swb_offset_128_24, /* 24000 */
269 swb_offset_128_24, /* 22050 */
270 swb_offset_128_16, /* 16000 */
271 swb_offset_128_16, /* 12000 */
272 swb_offset_128_16, /* 11025 */
273 swb_offset_128_8 /* 8000 */
274 };
275
276 #define bit_set(A, B) ((A) & (1<<(B)))
277
278 /* 4.5.2.3.4 */
279 /*
280 - determine the number of windows in a window_sequence named num_windows
281 - determine the number of window_groups named num_window_groups
282 - determine the number of windows in each group named window_group_length[g]
283 - determine the total number of scalefactor window bands named num_swb for
284 the actual window type
285 - determine swb_offset[swb], the offset of the first coefficient in
286 scalefactor window band named swb of the window actually used
287 - determine sect_sfb_offset[g][section],the offset of the first coefficient
288 in section named section. This offset depends on window_sequence and
289 scale_factor_grouping and is needed to decode the spectral_data().
290 */
291 uint8_t window_grouping_info(faacDecHandle hDecoder, ic_stream *ics)
292 {
293 uint8_t i, g;
294
295 uint8_t sf_index = hDecoder->sf_index;
296
297 switch (ics->window_sequence) {
298 case ONLY_LONG_SEQUENCE:
299 case LONG_START_SEQUENCE:
300 case LONG_STOP_SEQUENCE:
301 ics->num_windows = 1;
302 ics->num_window_groups = 1;
303 ics->window_group_length[ics->num_window_groups-1] = 1;
304 #ifdef LD_DEC
305 if (hDecoder->object_type == LD)
306 {
307 if (hDecoder->frameLength == 512)
308 ics->num_swb = num_swb_512_window[sf_index];
309 else /* if (hDecoder->frameLength == 480) */
310 ics->num_swb = num_swb_480_window[sf_index];
311 } else {
312 #endif
313 if (hDecoder->frameLength == 1024)
314 ics->num_swb = num_swb_1024_window[sf_index];
315 else /* if (hDecoder->frameLength == 960) */
316 ics->num_swb = num_swb_960_window[sf_index];
317 #ifdef LD_DEC
318 }
319 #endif
320
321 /* preparation of sect_sfb_offset for long blocks */
322 /* also copy the last value! */
323 #ifdef LD_DEC
324 if (hDecoder->object_type == LD)
325 {
326 if (hDecoder->frameLength == 512)
327 {
328 for (i = 0; i < ics->num_swb; i++)
329 {
330 ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i];
331 ics->swb_offset[i] = swb_offset_512_window[sf_index][i];
332 }
333 } else /* if (hDecoder->frameLength == 480) */ {
334 for (i = 0; i < ics->num_swb; i++)
335 {
336 ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i];
337 ics->swb_offset[i] = swb_offset_480_window[sf_index][i];
338 }
339 }
340 ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
341 ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
342 } else {
343 #endif
344 for (i = 0; i < ics->num_swb; i++)
345 {
346 ics->sect_sfb_offset[0][i] = swb_offset_1024_window[sf_index][i];
347 ics->swb_offset[i] = swb_offset_1024_window[sf_index][i];
348 }
349 ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
350 ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
351 #ifdef LD_DEC
352 }
353 #endif
354 return 0;
355 case EIGHT_SHORT_SEQUENCE:
356 ics->num_windows = 8;
357 ics->num_window_groups = 1;
358 ics->window_group_length[ics->num_window_groups-1] = 1;
359 ics->num_swb = num_swb_128_window[sf_index];
360
361 for (i = 0; i < ics->num_swb; i++)
362 ics->swb_offset[i] = swb_offset_128_window[sf_index][i];
363 ics->swb_offset[ics->num_swb] = hDecoder->frameLength/8;
364
365 for (i = 0; i < ics->num_windows-1; i++) {
366 if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
367 {
368 ics->num_window_groups += 1;
369 ics->window_group_length[ics->num_window_groups-1] = 1;
370 } else {
371 ics->window_group_length[ics->num_window_groups-1] += 1;
372 }
373 }
374
375 /* preparation of sect_sfb_offset for short blocks */
376 for (g = 0; g < ics->num_window_groups; g++)
377 {
378 uint16_t width;
379 uint8_t sect_sfb = 0;
380 uint16_t offset = 0;
381
382 for (i = 0; i < ics->num_swb; i++)
383 {
384 if (i+1 == ics->num_swb)
385 {
386 width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
387 } else {
388 width = swb_offset_128_window[sf_index][i+1] -
389 swb_offset_128_window[sf_index][i];
390 }
391 width *= ics->window_group_length[g];
392 ics->sect_sfb_offset[g][sect_sfb++] = offset;
393 offset += width;
394 }
395 ics->sect_sfb_offset[g][sect_sfb] = offset;
396 }
397 return 0;
398 default:
399 return 1;
400 }
401 }
402
403 /*
404 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
405 window_group_length[0] = 1) the spectral data is in ascending spectral
406 order.
407 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
408 grouping in the following manner:
409 - Groups are ordered sequentially
410 - Within a group, a scalefactor band consists of the spectral data of all
411 grouped SHORT_WINDOWs for the associated scalefactor window band. To
412 clarify via example, the length of a group is in the range of one to eight
413 SHORT_WINDOWs.
414 - If there are eight groups each with length one (num_window_groups = 8,
415 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
416 each in ascending spectral order.
417 - If there is only one group with length eight (num_window_groups = 1,
418 window_group_length[0] = 8), the result is that spectral data of all eight
419 SHORT_WINDOWs is interleaved by scalefactor window bands.
420 - Within a scalefactor window band, the coefficients are in ascending
421 spectral order.
422 */
423 static void quant_to_spec(ic_stream *ics, real_t *spec_data, uint16_t frame_len)
424 {
425 uint8_t g, sfb, win;
426 uint16_t width, bin, k, gindex;
427
428 real_t tmp_spec[1024] = {0};
429
430 k = 0;
431 gindex = 0;
432
433 for (g = 0; g < ics->num_window_groups; g++)
434 {
435 uint16_t j = 0;
436 uint16_t gincrease = 0;
437 uint16_t win_inc = ics->swb_offset[ics->num_swb];
438
439 for (sfb = 0; sfb < ics->num_swb; sfb++)
440 {
441 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
442
443 for (win = 0; win < ics->window_group_length[g]; win++)
444 {
445 for (bin = 0; bin < width; bin += 4)
446 {
447 tmp_spec[gindex+(win*win_inc)+j+bin+0] = spec_data[k+0];
448 tmp_spec[gindex+(win*win_inc)+j+bin+1] = spec_data[k+1];
449 tmp_spec[gindex+(win*win_inc)+j+bin+2] = spec_data[k+2];
450 tmp_spec[gindex+(win*win_inc)+j+bin+3] = spec_data[k+3];
451 gincrease += 4;
452 k += 4;
453 }
454 }
455 j += width;
456 }
457 gindex += gincrease;
458 }
459
460 memcpy(spec_data, tmp_spec, frame_len*sizeof(real_t));
461 }
462
463 #ifndef FIXED_POINT
464 void build_tables(real_t *pow2_table)
465 {
466 uint16_t i;
467
468 /* build pow(2, 0.25*x) table for scalefactors */
469 for(i = 0; i < POW_TABLE_SIZE; i++)
470 {
471 pow2_table[i] = REAL_CONST(pow(2.0, 0.25 * (i-100)));
472 }
473 }
474 #endif
475
476 static INLINE real_t iquant(int16_t q, real_t *tab)
477 {
478 #ifdef FIXED_POINT
479 static const real_t errcorr[] = {
480 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
481 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
482 REAL_CONST(0)
483 };
484 real_t x1, x2;
485 int16_t sgn = 1;
486
487 if (q < 0)
488 {
489 q = -q;
490 sgn = -1;
491 }
492
493 if (q < IQ_TABLE_SIZE)
494 return sgn * tab[q];
495
496 /* linear interpolation */
497 x1 = tab[q>>3];
498 x2 = tab[(q>>3) + 1];
499 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
500 #else
501 real_t sgn = REAL_CONST(1.0);
502
503 if (q < 0)
504 {
505 q = -q;
506 sgn = REAL_CONST(-1.0);
507 }
508
509 if (q < IQ_TABLE_SIZE)
510 return sgn * tab[q];
511
512 return sgn * (real_t)pow(q, 4.0/3.0);
513 #endif
514 }
515
516 static void inverse_quantization(real_t *x_invquant, int16_t *x_quant, uint16_t frame_len)
517 {
518 int16_t i;
519 real_t *tab = iq_table;
520
521 for(i = 0; i < frame_len; i+=4)
522 {
523 x_invquant[i] = iquant(x_quant[i], tab);
524 x_invquant[i+1] = iquant(x_quant[i+1], tab);
525 x_invquant[i+2] = iquant(x_quant[i+2], tab);
526 x_invquant[i+3] = iquant(x_quant[i+3], tab);
527 }
528 }
529
530 #ifndef FIXED_POINT
531 static INLINE real_t get_scale_factor_gain(uint16_t scale_factor, real_t *pow2_table)
532 {
533 if (scale_factor < POW_TABLE_SIZE)
534 return pow2_table[scale_factor];
535 else
536 return REAL_CONST(pow(2.0, 0.25 * (scale_factor - 100)));
537 }
538 #else
539 static real_t pow2_table[] =
540 {
541 COEF_CONST(0.59460355750136053335874998528024), /* 2^-0.75 */
542 COEF_CONST(0.70710678118654752440084436210485), /* 2^-0.5 */
543 COEF_CONST(0.84089641525371454303112547623321), /* 2^-0.25 */
544 COEF_CONST(1.0),
545 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
546 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
547 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
548 };
549 #endif
550
551 static void apply_scalefactors(faacDecHandle hDecoder, ic_stream *ics,
552 real_t *x_invquant, uint16_t frame_len)
553 {
554 uint8_t g, sfb;
555 uint16_t top;
556 #ifndef FIXED_POINT
557 real_t scale;
558 #else
559 int32_t exp, frac;
560 #endif
561 uint8_t groups = 0;
562 uint16_t nshort = frame_len/8;
563
564 for (g = 0; g < ics->num_window_groups; g++)
565 {
566 uint16_t k = 0;
567
568 /* using this nshort*groups doesn't hurt long blocks, because
569 long blocks only have 1 group, so that means 'groups' is
570 always 0 for long blocks
571 */
572 for (sfb = 0; sfb < ics->max_sfb; sfb++)
573 {
574 top = ics->sect_sfb_offset[g][sfb+1];
575
576 #ifndef FIXED_POINT
577 scale = get_scale_factor_gain(ics->scale_factors[g][sfb], hDecoder->pow2_table);
578 #else
579 exp = (ics->scale_factors[g][sfb] - 100) / 4;
580 frac = (ics->scale_factors[g][sfb] - 100) % 4;
581
582 /* IMDCT pre-scaling */
583 if (hDecoder->object_type == LD)
584 {
585 exp -= 6 /*9*/;
586 } else {
587 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
588 exp -= 4 /*7*/;
589 else
590 exp -= 7 /*10*/;
591 }
592 #if (REAL_BITS == 16)
593 exp--;
594 #endif
595 #endif
596
597 /* minimum size of a sf band is 4 and always a multiple of 4 */
598 for ( ; k < top; k += 4)
599 {
600 #ifndef FIXED_POINT
601 x_invquant[k+(groups*nshort)] = x_invquant[k+(groups*nshort)] * scale;
602 x_invquant[k+(groups*nshort)+1] = x_invquant[k+(groups*nshort)+1] * scale;
603 x_invquant[k+(groups*nshort)+2] = x_invquant[k+(groups*nshort)+2] * scale;
604 x_invquant[k+(groups*nshort)+3] = x_invquant[k+(groups*nshort)+3] * scale;
605 #else
606 if (exp < 0)
607 {
608 x_invquant[k+(groups*nshort)] >>= -exp;
609 x_invquant[k+(groups*nshort)+1] >>= -exp;
610 x_invquant[k+(groups*nshort)+2] >>= -exp;
611 x_invquant[k+(groups*nshort)+3] >>= -exp;
612 } else {
613 x_invquant[k+(groups*nshort)] <<= exp;
614 x_invquant[k+(groups*nshort)+1] <<= exp;
615 x_invquant[k+(groups*nshort)+2] <<= exp;
616 x_invquant[k+(groups*nshort)+3] <<= exp;
617 }
618
619 if (frac)
620 {
621 x_invquant[k+(groups*nshort)] = MUL_C(x_invquant[k+(groups*nshort)],pow2_table[frac + 3]);
622 x_invquant[k+(groups*nshort)+1] = MUL_C(x_invquant[k+(groups*nshort)+1],pow2_table[frac + 3]);
623 x_invquant[k+(groups*nshort)+2] = MUL_C(x_invquant[k+(groups*nshort)+2],pow2_table[frac + 3]);
624 x_invquant[k+(groups*nshort)+3] = MUL_C(x_invquant[k+(groups*nshort)+3],pow2_table[frac + 3]);
625 }
626 #endif
627 }
628 }
629 groups += ics->window_group_length[g];
630 }
631 }
632
633 void reconstruct_single_channel(faacDecHandle hDecoder, ic_stream *ics,
634 element *sce, int16_t *spec_data)
635 {
636 real_t spec_coef[1024];
637
638 /* inverse quantization */
639 inverse_quantization(spec_coef, spec_data, hDecoder->frameLength);
640
641 /* apply scalefactors */
642 apply_scalefactors(hDecoder, ics, spec_coef, hDecoder->frameLength);
643
644 /* deinterleave short block grouping */
645 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
646 quant_to_spec(ics, spec_coef, hDecoder->frameLength);
647
648
649 /* pns decoding */
650 pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
651
652 #ifdef MAIN_DEC
653 /* MAIN object type prediction */
654 if (hDecoder->object_type == MAIN)
655 {
656 /* allocate the state only when needed */
657 if (hDecoder->pred_stat[sce->channel] == NULL)
658 {
659 hDecoder->pred_stat[sce->channel] = (pred_state*)malloc(hDecoder->frameLength * sizeof(pred_state));
660 reset_all_predictors(hDecoder->pred_stat[sce->channel], hDecoder->frameLength);
661 }
662
663 /* intra channel prediction */
664 ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
665 hDecoder->sf_index);
666
667 /* In addition, for scalefactor bands coded by perceptual
668 noise substitution the predictors belonging to the
669 corresponding spectral coefficients are reset.
670 */
671 pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
672 }
673 #endif
674
675 #ifdef LTP_DEC
676 if (is_ltp_ot(hDecoder->object_type))
677 {
678 #ifdef LD_DEC
679 if (hDecoder->object_type == LD)
680 {
681 if (ics->ltp.data_present)
682 {
683 if (ics->ltp.lag_update)
684 hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
685 }
686 ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
687 }
688 #endif
689
690 /* allocate the state only when needed */
691 if (hDecoder->lt_pred_stat[sce->channel] == NULL)
692 {
693 hDecoder->lt_pred_stat[sce->channel] = (int16_t*)malloc(hDecoder->frameLength*4 * sizeof(int16_t));
694 memset(hDecoder->lt_pred_stat[sce->channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
695 }
696
697 /* long term prediction */
698 lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
699 ics->window_shape, hDecoder->window_shape_prev[sce->channel],
700 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
701 }
702 #endif
703
704 /* tns decoding */
705 tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
706 spec_coef, hDecoder->frameLength);
707
708 /* drc decoding */
709 if (hDecoder->drc->present)
710 {
711 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
712 drc_decode(hDecoder->drc, spec_coef);
713 }
714
715 if (hDecoder->time_out[sce->channel] == NULL)
716 {
717 hDecoder->time_out[sce->channel] = (real_t*)malloc(hDecoder->frameLength*2*sizeof(real_t));
718 memset(hDecoder->time_out[sce->channel], 0, hDecoder->frameLength*2*sizeof(real_t));
719 }
720
721 /* filter bank */
722 #ifdef SSR_DEC
723 if (hDecoder->object_type != SSR)
724 {
725 #endif
726 ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
727 hDecoder->window_shape_prev[sce->channel], spec_coef,
728 hDecoder->time_out[sce->channel], hDecoder->object_type, hDecoder->frameLength);
729 #ifdef SSR_DEC
730 } else {
731 if (hDecoder->ssr_overlap[sce->channel] == NULL)
732 {
733 hDecoder->ssr_overlap[sce->channel] = (real_t*)malloc(2*hDecoder->frameLength*sizeof(real_t));
734 memset(hDecoder->ssr_overlap[sce->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
735 }
736 if (hDecoder->prev_fmd[sce->channel] == NULL)
737 {
738 uint16_t k;
739 hDecoder->prev_fmd[sce->channel] = (real_t*)malloc(2*hDecoder->frameLength*sizeof(real_t));
740 for (k = 0; k < 2*hDecoder->frameLength; k++)
741 hDecoder->prev_fmd[sce->channel][k] = REAL_CONST(-1);
742 }
743
744 ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
745 hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
746 hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
747 hDecoder->frameLength);
748 }
749 #endif
750
751 /* save window shape for next frame */
752 hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
753
754 #ifdef LTP_DEC
755 if (is_ltp_ot(hDecoder->object_type))
756 {
757 lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel],
758 hDecoder->time_out[sce->channel]+hDecoder->frameLength, hDecoder->frameLength, hDecoder->object_type);
759 }
760 #endif
761 }
762
763 void reconstruct_channel_pair(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
764 element *cpe, int16_t *spec_data1, int16_t *spec_data2)
765 {
766 real_t spec_coef1[1024];
767 real_t spec_coef2[1024];
768
769 /* inverse quantization */
770 inverse_quantization(spec_coef1, spec_data1, hDecoder->frameLength);
771 inverse_quantization(spec_coef2, spec_data2, hDecoder->frameLength);
772
773 /* apply scalefactors */
774 apply_scalefactors(hDecoder, ics1, spec_coef1, hDecoder->frameLength);
775 apply_scalefactors(hDecoder, ics2, spec_coef2, hDecoder->frameLength);
776
777 /* deinterleave short block grouping */
778 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
779 quant_to_spec(ics1, spec_coef1, hDecoder->frameLength);
780 if (ics2->window_sequence == EIGHT_SHORT_SEQUENCE)
781 quant_to_spec(ics2, spec_coef2, hDecoder->frameLength);
782
783
784 /* pns decoding */
785 if (ics1->ms_mask_present)
786 {
787 pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type);
788 } else {
789 pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
790 pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
791 }
792
793 /* mid/side decoding */
794 ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
795
796 /* intensity stereo decoding */
797 is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
798
799 #ifdef MAIN_DEC
800 /* MAIN object type prediction */
801 if (hDecoder->object_type == MAIN)
802 {
803 /* allocate the state only when needed */
804 if (hDecoder->pred_stat[cpe->channel] == NULL)
805 {
806 hDecoder->pred_stat[cpe->channel] = (pred_state*)malloc(hDecoder->frameLength * sizeof(pred_state));
807 reset_all_predictors(hDecoder->pred_stat[cpe->channel], hDecoder->frameLength);
808 }
809 if (hDecoder->pred_stat[cpe->paired_channel] == NULL)
810 {
811 hDecoder->pred_stat[cpe->paired_channel] = (pred_state*)malloc(hDecoder->frameLength * sizeof(pred_state));
812 reset_all_predictors(hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength);
813 }
814
815 /* intra channel prediction */
816 ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
817 hDecoder->sf_index);
818 ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
819 hDecoder->sf_index);
820
821 /* In addition, for scalefactor bands coded by perceptual
822 noise substitution the predictors belonging to the
823 corresponding spectral coefficients are reset.
824 */
825 pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
826 pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
827 }
828 #endif
829
830 #ifdef LTP_DEC
831 if (is_ltp_ot(hDecoder->object_type))
832 {
833 ltp_info *ltp1 = &(ics1->ltp);
834 ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp) ;
835 #ifdef LD_DEC
836 if (hDecoder->object_type == LD)
837 {
838 if (ltp1->data_present)
839 {
840 if (ltp1->lag_update)
841 hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
842 }
843 ltp1->lag = hDecoder->ltp_lag[cpe->channel];
844 if (ltp2->data_present)
845 {
846 if (ltp2->lag_update)
847 hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
848 }
849 ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
850 }
851 #endif
852
853 /* allocate the state only when needed */
854 if (hDecoder->lt_pred_stat[cpe->channel] == NULL)
855 {
856 hDecoder->lt_pred_stat[cpe->channel] = (int16_t*)malloc(hDecoder->frameLength*4 * sizeof(int16_t));
857 memset(hDecoder->lt_pred_stat[cpe->channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
858 }
859 if (hDecoder->lt_pred_stat[cpe->paired_channel] == NULL)
860 {
861 hDecoder->lt_pred_stat[cpe->paired_channel] = (int16_t*)malloc(hDecoder->frameLength*4 * sizeof(int16_t));
862 memset(hDecoder->lt_pred_stat[cpe->paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
863 }
864
865 /* long term prediction */
866 lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
867 ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
868 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
869 lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
870 ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
871 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
872 }
873 #endif
874
875 /* tns decoding */
876 tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
877 spec_coef1, hDecoder->frameLength);
878 tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
879 spec_coef2, hDecoder->frameLength);
880
881 /* drc decoding */
882 if (hDecoder->drc->present)
883 {
884 if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
885 drc_decode(hDecoder->drc, spec_coef1);
886 if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
887 drc_decode(hDecoder->drc, spec_coef2);
888 }
889
890 if (hDecoder->time_out[cpe->channel] == NULL)
891 {
892 hDecoder->time_out[cpe->channel] = (real_t*)malloc(hDecoder->frameLength*2*sizeof(real_t));
893 memset(hDecoder->time_out[cpe->channel], 0, hDecoder->frameLength*2*sizeof(real_t));
894 }
895 if (hDecoder->time_out[cpe->paired_channel] == NULL)
896 {
897 hDecoder->time_out[cpe->paired_channel] = (real_t*)malloc(hDecoder->frameLength*2*sizeof(real_t));
898 memset(hDecoder->time_out[cpe->paired_channel], 0, hDecoder->frameLength*2*sizeof(real_t));
899 }
900
901 /* filter bank */
902 #ifdef SSR_DEC
903 if (hDecoder->object_type != SSR)
904 {
905 #endif
906 ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
907 hDecoder->window_shape_prev[cpe->channel], spec_coef1,
908 hDecoder->time_out[cpe->channel], hDecoder->object_type, hDecoder->frameLength);
909 ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
910 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
911 hDecoder->time_out[cpe->paired_channel], hDecoder->object_type, hDecoder->frameLength);
912 #ifdef SSR_DEC
913 } else {
914 if (hDecoder->ssr_overlap[cpe->channel] == NULL)
915 {
916 hDecoder->ssr_overlap[cpe->channel] = (real_t*)malloc(2*hDecoder->frameLength*sizeof(real_t));
917 memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
918 }
919 if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
920 {
921 hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)malloc(2*hDecoder->frameLength*sizeof(real_t));
922 memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
923 }
924 if (hDecoder->prev_fmd[cpe->channel] == NULL)
925 {
926 uint16_t k;
927 hDecoder->prev_fmd[cpe->channel] = (real_t*)malloc(2*hDecoder->frameLength*sizeof(real_t));
928 for (k = 0; k < 2*hDecoder->frameLength; k++)
929 hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
930 }
931 if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
932 {
933 uint16_t k;
934 hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)malloc(2*hDecoder->frameLength*sizeof(real_t));
935 for (k = 0; k < 2*hDecoder->frameLength; k++)
936 hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
937 }
938
939 ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
940 hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
941 hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
942 hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
943 ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
944 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
945 hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
946 hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
947 }
948 #endif
949
950 /* save window shape for next frame */
951 hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
952 hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
953
954 #ifdef LTP_DEC
955 if (is_ltp_ot(hDecoder->object_type))
956 {
957 lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
958 hDecoder->time_out[cpe->channel]+hDecoder->frameLength, hDecoder->frameLength, hDecoder->object_type);
959 lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
960 hDecoder->time_out[cpe->paired_channel]+hDecoder->frameLength, hDecoder->frameLength,
961 hDecoder->object_type);
962 }
963 #endif
964 }