]> 4ch.mooo.com Git - 16.git/blob - src/lib/doslib/ext/faad/specrec.c
f259f6babd2b12d93caea0ebdb22d2ded309ab4b
[16.git] / src / lib / doslib / ext / faad / specrec.c
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: specrec.c,v 1.62 2009/01/26 23:51:15 menno Exp $
29 **/
30
31 /*
32   Spectral reconstruction:
33    - grouping/sectioning
34    - inverse quantization
35    - applying scalefactors
36 */
37
38 #include "common.h"
39 #include "structs.h"
40
41 #include <string.h>
42 #include <stdlib.h>
43 #include "specrec.h"
44 #include "filtbank.h"
45 #include "syntax.h"
46 #include "iq_table.h"
47 #include "ms.h"
48 #include "is.h"
49 #include "pns.h"
50 #include "tns.h"
51 #include "drc.h"
52 #include "lt_pred.h"
53 #include "ic_pred.h"
54 #ifdef SSR_DEC
55 #include "ssr.h"
56 #include "ssr_fb.h"
57 #endif
58
59
60 /* static function declarations */
61 static uint8_t quant_to_spec(NeAACDecStruct *hDecoder,
62                              ic_stream *ics, int16_t *quant_data,
63                              real_t *spec_data, uint16_t frame_len);
64
65
66 #ifdef LD_DEC
67 ALIGN static const uint8_t num_swb_512_window[] =
68 {
69     0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
70 };
71 ALIGN static const uint8_t num_swb_480_window[] =
72 {
73     0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
74 };
75 #endif
76
77 ALIGN static const uint8_t num_swb_960_window[] =
78 {
79     40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
80 };
81
82 ALIGN static const uint8_t num_swb_1024_window[] =
83 {
84     41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
85 };
86
87 ALIGN static const uint8_t num_swb_128_window[] =
88 {
89     12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
90 };
91
92 ALIGN static const uint16_t swb_offset_1024_96[] =
93 {
94     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
95     64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240,
96     276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
97 };
98
99 ALIGN static const uint16_t swb_offset_128_96[] =
100 {
101     0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
102 };
103
104 ALIGN static const uint16_t swb_offset_1024_64[] =
105 {
106     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
107     64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268,
108     304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824,
109     864, 904, 944, 984, 1024
110 };
111
112 ALIGN static const uint16_t swb_offset_128_64[] =
113 {
114     0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
115 };
116
117 ALIGN static const uint16_t swb_offset_1024_48[] =
118 {
119     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
120     80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
121     320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
122     768, 800, 832, 864, 896, 928, 1024
123 };
124
125 #ifdef LD_DEC
126 ALIGN static const uint16_t swb_offset_512_48[] =
127 {
128     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84,
129     92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396,
130     428, 460, 512
131 };
132
133 ALIGN static const uint16_t swb_offset_480_48[] =
134 {
135     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 ,80 ,88,
136     96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400,
137     432, 480
138 };
139 #endif
140
141 ALIGN static const uint16_t swb_offset_128_48[] =
142 {
143     0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
144 };
145
146 ALIGN static const uint16_t swb_offset_1024_32[] =
147 {
148     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
149     80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
150     320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
151     768, 800, 832, 864, 896, 928, 960, 992, 1024
152 };
153
154 #ifdef LD_DEC
155 ALIGN static const uint16_t swb_offset_512_32[] =
156 {
157     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80,
158     88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352,
159     384, 416, 448, 480, 512
160 };
161
162 ALIGN static const uint16_t swb_offset_480_32[] =
163 {
164     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80,
165     88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352,
166     384, 416, 448, 480
167 };
168 #endif
169
170 ALIGN static const uint16_t swb_offset_1024_24[] =
171 {
172     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
173     76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220,
174     240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704,
175     768, 832, 896, 960, 1024
176 };
177
178 #ifdef LD_DEC
179 ALIGN static const uint16_t swb_offset_512_24[] =
180 {
181     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
182     80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416,
183     448, 480, 512
184 };
185
186 ALIGN static const uint16_t swb_offset_480_24[] =
187 {
188     0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120,
189     140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480
190 };
191 #endif
192
193 ALIGN static const uint16_t swb_offset_128_24[] =
194 {
195     0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
196 };
197
198 ALIGN static const uint16_t swb_offset_1024_16[] =
199 {
200     0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124,
201     136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344,
202     368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024
203 };
204
205 ALIGN static const uint16_t swb_offset_128_16[] =
206 {
207     0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
208 };
209
210 ALIGN static const uint16_t swb_offset_1024_8[] =
211 {
212     0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172,
213     188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448,
214     476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024
215 };
216
217 ALIGN static const uint16_t swb_offset_128_8[] =
218 {
219     0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
220 };
221
222 ALIGN static const uint16_t *swb_offset_1024_window[] =
223 {
224     swb_offset_1024_96,      /* 96000 */
225     swb_offset_1024_96,      /* 88200 */
226     swb_offset_1024_64,      /* 64000 */
227     swb_offset_1024_48,      /* 48000 */
228     swb_offset_1024_48,      /* 44100 */
229     swb_offset_1024_32,      /* 32000 */
230     swb_offset_1024_24,      /* 24000 */
231     swb_offset_1024_24,      /* 22050 */
232     swb_offset_1024_16,      /* 16000 */
233     swb_offset_1024_16,      /* 12000 */
234     swb_offset_1024_16,      /* 11025 */
235     swb_offset_1024_8        /* 8000  */
236 };
237
238 #ifdef LD_DEC
239 ALIGN static const uint16_t *swb_offset_512_window[] =
240 {
241     0,                       /* 96000 */
242     0,                       /* 88200 */
243     0,                       /* 64000 */
244     swb_offset_512_48,       /* 48000 */
245     swb_offset_512_48,       /* 44100 */
246     swb_offset_512_32,       /* 32000 */
247     swb_offset_512_24,       /* 24000 */
248     swb_offset_512_24,       /* 22050 */
249     0,                       /* 16000 */
250     0,                       /* 12000 */
251     0,                       /* 11025 */
252     0                        /* 8000  */
253 };
254
255 ALIGN static const uint16_t *swb_offset_480_window[] =
256 {
257     0,                       /* 96000 */
258     0,                       /* 88200 */
259     0,                       /* 64000 */
260     swb_offset_480_48,       /* 48000 */
261     swb_offset_480_48,       /* 44100 */
262     swb_offset_480_32,       /* 32000 */
263     swb_offset_480_24,       /* 24000 */
264     swb_offset_480_24,       /* 22050 */
265     0,                       /* 16000 */
266     0,                       /* 12000 */
267     0,                       /* 11025 */
268     0                        /* 8000  */
269 };
270 #endif
271
272 ALIGN static const  uint16_t *swb_offset_128_window[] =
273 {
274     swb_offset_128_96,       /* 96000 */
275     swb_offset_128_96,       /* 88200 */
276     swb_offset_128_64,       /* 64000 */
277     swb_offset_128_48,       /* 48000 */
278     swb_offset_128_48,       /* 44100 */
279     swb_offset_128_48,       /* 32000 */
280     swb_offset_128_24,       /* 24000 */
281     swb_offset_128_24,       /* 22050 */
282     swb_offset_128_16,       /* 16000 */
283     swb_offset_128_16,       /* 12000 */
284     swb_offset_128_16,       /* 11025 */
285     swb_offset_128_8         /* 8000  */
286 };
287
288 #define bit_set(A, B) ((A) & (1<<(B)))
289
290 /* 4.5.2.3.4 */
291 /*
292   - determine the number of windows in a window_sequence named num_windows
293   - determine the number of window_groups named num_window_groups
294   - determine the number of windows in each group named window_group_length[g]
295   - determine the total number of scalefactor window bands named num_swb for
296     the actual window type
297   - determine swb_offset[swb], the offset of the first coefficient in
298     scalefactor window band named swb of the window actually used
299   - determine sect_sfb_offset[g][section],the offset of the first coefficient
300     in section named section. This offset depends on window_sequence and
301     scale_factor_grouping and is needed to decode the spectral_data().
302 */
303 uint8_t window_grouping_info(NeAACDecStruct *hDecoder, ic_stream *ics)
304 {
305     uint8_t i, g;
306
307     uint8_t sf_index = hDecoder->sf_index;
308
309     switch (ics->window_sequence) {
310     case ONLY_LONG_SEQUENCE:
311     case LONG_START_SEQUENCE:
312     case LONG_STOP_SEQUENCE:
313         ics->num_windows = 1;
314         ics->num_window_groups = 1;
315         ics->window_group_length[ics->num_window_groups-1] = 1;
316 #ifdef LD_DEC
317         if (hDecoder->object_type == LD)
318         {
319             if (hDecoder->frameLength == 512)
320                 ics->num_swb = num_swb_512_window[sf_index];
321             else /* if (hDecoder->frameLength == 480) */
322                 ics->num_swb = num_swb_480_window[sf_index];
323         } else {
324 #endif
325             if (hDecoder->frameLength == 1024)
326                 ics->num_swb = num_swb_1024_window[sf_index];
327             else /* if (hDecoder->frameLength == 960) */
328                 ics->num_swb = num_swb_960_window[sf_index];
329 #ifdef LD_DEC
330         }
331 #endif
332
333         if (ics->max_sfb > ics->num_swb)
334         {
335             return 32;
336         }
337
338         /* preparation of sect_sfb_offset for long blocks */
339         /* also copy the last value! */
340 #ifdef LD_DEC
341         if (hDecoder->object_type == LD)
342         {
343             if (hDecoder->frameLength == 512)
344             {
345                 for (i = 0; i < ics->num_swb; i++)
346                 {
347                     ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i];
348                     ics->swb_offset[i] = swb_offset_512_window[sf_index][i];
349                 }
350             } else /* if (hDecoder->frameLength == 480) */ {
351                 for (i = 0; i < ics->num_swb; i++)
352                 {
353                     ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i];
354                     ics->swb_offset[i] = swb_offset_480_window[sf_index][i];
355                 }
356             }
357             ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
358             ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
359             ics->swb_offset_max = hDecoder->frameLength;
360         } else {
361 #endif
362             for (i = 0; i < ics->num_swb; i++)
363             {
364                 ics->sect_sfb_offset[0][i] = swb_offset_1024_window[sf_index][i];
365                 ics->swb_offset[i] = swb_offset_1024_window[sf_index][i];
366             }
367             ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
368             ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
369             ics->swb_offset_max = hDecoder->frameLength;
370 #ifdef LD_DEC
371         }
372 #endif
373         return 0;
374     case EIGHT_SHORT_SEQUENCE:
375         ics->num_windows = 8;
376         ics->num_window_groups = 1;
377         ics->window_group_length[ics->num_window_groups-1] = 1;
378         ics->num_swb = num_swb_128_window[sf_index];
379
380         if (ics->max_sfb > ics->num_swb)
381         {
382             return 32;
383         }
384
385         for (i = 0; i < ics->num_swb; i++)
386             ics->swb_offset[i] = swb_offset_128_window[sf_index][i];
387         ics->swb_offset[ics->num_swb] = hDecoder->frameLength/8;
388         ics->swb_offset_max = hDecoder->frameLength/8;
389
390         for (i = 0; i < ics->num_windows-1; i++) {
391             if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
392             {
393                 ics->num_window_groups += 1;
394                 ics->window_group_length[ics->num_window_groups-1] = 1;
395             } else {
396                 ics->window_group_length[ics->num_window_groups-1] += 1;
397             }
398         }
399
400         /* preparation of sect_sfb_offset for short blocks */
401         for (g = 0; g < ics->num_window_groups; g++)
402         {
403             uint16_t width;
404             uint8_t sect_sfb = 0;
405             uint16_t offset = 0;
406
407             for (i = 0; i < ics->num_swb; i++)
408             {
409                 if (i+1 == ics->num_swb)
410                 {
411                     width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
412                 } else {
413                     width = swb_offset_128_window[sf_index][i+1] -
414                         swb_offset_128_window[sf_index][i];
415                 }
416                 width *= ics->window_group_length[g];
417                 ics->sect_sfb_offset[g][sect_sfb++] = offset;
418                 offset += width;
419             }
420             ics->sect_sfb_offset[g][sect_sfb] = offset;
421         }
422         return 0;
423     default:
424         return 32;
425     }
426 }
427
428 /* iquant() *
429  * output = sign(input)*abs(input)^(4/3) */
430 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
431 {
432 #ifdef FIXED_POINT
433 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
434 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
435  * defined a 1026 value table and interpolation will be used
436  */
437 #ifndef BIG_IQ_TABLE
438     static const real_t errcorr[] = {
439         REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
440         REAL_CONST(4.0/8.0),  REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
441         REAL_CONST(0)
442     };
443     real_t x1, x2;
444 #endif
445     int16_t sgn = 1;
446
447     if (q < 0)
448     {
449         q = -q;
450         sgn = -1;
451     }
452
453     if (q < IQ_TABLE_SIZE)
454     {
455 //#define IQUANT_PRINT
456 #ifdef IQUANT_PRINT
457         //printf("0x%.8X\n", sgn * tab[q]);
458         printf("%d\n", sgn * tab[q]);
459 #endif
460         return sgn * tab[q];
461     }
462
463 #ifndef BIG_IQ_TABLE
464     if (q >= 8192)
465     {
466         *error = 17;
467         return 0;
468     }
469
470     /* linear interpolation */
471     x1 = tab[q>>3];
472     x2 = tab[(q>>3) + 1];
473     return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
474 #else
475     *error = 17;
476     return 0;
477 #endif
478
479 #else
480     if (q < 0)
481     {
482         /* tab contains a value for all possible q [0,8192] */
483         if (-q < IQ_TABLE_SIZE)
484             return -tab[-q];
485
486         *error = 17;
487         return 0;
488     } else {
489         /* tab contains a value for all possible q [0,8192] */
490         if (q < IQ_TABLE_SIZE)
491             return tab[q];
492
493         *error = 17;
494         return 0;
495     }
496 #endif
497 }
498
499 #ifndef FIXED_POINT
500 ALIGN static const real_t pow2sf_tab[] = {
501     2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
502     2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007,
503     1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006,
504     1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005,
505     0.0001220703125, 0.000244140625, 0.00048828125,
506     0.0009765625, 0.001953125, 0.00390625,
507     0.0078125, 0.015625, 0.03125,
508     0.0625, 0.125, 0.25,
509     0.5, 1.0, 2.0,
510     4.0, 8.0, 16.0, 32.0,
511     64.0, 128.0, 256.0,
512     512.0, 1024.0, 2048.0,
513     4096.0, 8192.0, 16384.0,
514     32768.0, 65536.0, 131072.0,
515     262144.0, 524288.0, 1048576.0,
516     2097152.0, 4194304.0, 8388608.0,
517     16777216.0, 33554432.0, 67108864.0,
518     134217728.0, 268435456.0, 536870912.0,
519     1073741824.0, 2147483648.0, 4294967296.0,
520     8589934592.0, 17179869184.0, 34359738368.0,
521     68719476736.0, 137438953472.0, 274877906944.0
522 };
523 #endif
524
525 /* quant_to_spec: perform dequantisation and scaling
526  * and in case of short block it also does the deinterleaving
527  */
528 /*
529   For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
530   window_group_length[0] = 1) the spectral data is in ascending spectral
531   order.
532   For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
533   grouping in the following manner:
534   - Groups are ordered sequentially
535   - Within a group, a scalefactor band consists of the spectral data of all
536     grouped SHORT_WINDOWs for the associated scalefactor window band. To
537     clarify via example, the length of a group is in the range of one to eight
538     SHORT_WINDOWs.
539   - If there are eight groups each with length one (num_window_groups = 8,
540     window_group_length[0..7] = 1), the result is a sequence of eight spectra,
541     each in ascending spectral order.
542   - If there is only one group with length eight (num_window_groups = 1,
543     window_group_length[0] = 8), the result is that spectral data of all eight
544     SHORT_WINDOWs is interleaved by scalefactor window bands.
545   - Within a scalefactor window band, the coefficients are in ascending
546     spectral order.
547 */
548 static uint8_t quant_to_spec(NeAACDecStruct *hDecoder,
549                              ic_stream *ics, int16_t *quant_data,
550                              real_t *spec_data, uint16_t frame_len)
551 {
552     ALIGN static const real_t pow2_table[] =
553     {
554         COEF_CONST(1.0),
555         COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
556         COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
557         COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
558     };
559     const real_t *tab = iq_table;
560
561     uint8_t g, sfb, win;
562     uint16_t width, bin, k, gindex, wa, wb;
563     uint8_t error = 0; /* Init error flag */
564 #ifndef FIXED_POINT
565     real_t scf;
566 #endif
567
568     k = 0;
569     gindex = 0;
570
571     for (g = 0; g < ics->num_window_groups; g++)
572     {
573         uint16_t j = 0;
574         uint16_t gincrease = 0;
575         uint16_t win_inc = ics->swb_offset[ics->num_swb];
576
577         for (sfb = 0; sfb < ics->num_swb; sfb++)
578         {
579             int32_t exp, frac;
580
581             width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
582
583             /* this could be scalefactor for IS or PNS, those can be negative or bigger then 255 */
584             /* just ignore them */
585             if (ics->scale_factors[g][sfb] < 0 || ics->scale_factors[g][sfb] > 255)
586             {
587                 exp = 0;
588                 frac = 0;
589             } else {
590                 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
591                 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
592                 /* frac must always be > 0 */
593                 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
594             }
595
596 #ifdef FIXED_POINT
597             exp -= 25;
598             /* IMDCT pre-scaling */
599             if (hDecoder->object_type == LD)
600             {
601                 exp -= 6 /*9*/;
602             } else {
603                 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
604                     exp -= 4 /*7*/;
605                 else
606                     exp -= 7 /*10*/;
607             }
608 #endif
609
610             wa = gindex + j;
611
612 #ifndef FIXED_POINT
613             scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
614 #endif
615
616             for (win = 0; win < ics->window_group_length[g]; win++)
617             {
618                 for (bin = 0; bin < width; bin += 4)
619                 {
620 #ifndef FIXED_POINT
621                     wb = wa + bin;
622
623                     spec_data[wb+0] = iquant(quant_data[k+0], tab, &error) * scf;
624                     spec_data[wb+1] = iquant(quant_data[k+1], tab, &error) * scf;                        
625                     spec_data[wb+2] = iquant(quant_data[k+2], tab, &error) * scf;                        
626                     spec_data[wb+3] = iquant(quant_data[k+3], tab, &error) * scf;
627                         
628 #else
629                     real_t iq0 = iquant(quant_data[k+0], tab, &error);
630                     real_t iq1 = iquant(quant_data[k+1], tab, &error);
631                     real_t iq2 = iquant(quant_data[k+2], tab, &error);
632                     real_t iq3 = iquant(quant_data[k+3], tab, &error);
633
634                     wb = wa + bin;
635
636                     if (exp < 0)
637                     {
638                         spec_data[wb+0] = iq0 >>= -exp;
639                         spec_data[wb+1] = iq1 >>= -exp;
640                         spec_data[wb+2] = iq2 >>= -exp;
641                         spec_data[wb+3] = iq3 >>= -exp;
642                     } else {
643                         spec_data[wb+0] = iq0 <<= exp;
644                         spec_data[wb+1] = iq1 <<= exp;
645                         spec_data[wb+2] = iq2 <<= exp;
646                         spec_data[wb+3] = iq3 <<= exp;
647                     }
648                     if (frac != 0)
649                     {
650                         spec_data[wb+0] = MUL_C(spec_data[wb+0],pow2_table[frac]);
651                         spec_data[wb+1] = MUL_C(spec_data[wb+1],pow2_table[frac]);
652                         spec_data[wb+2] = MUL_C(spec_data[wb+2],pow2_table[frac]);
653                         spec_data[wb+3] = MUL_C(spec_data[wb+3],pow2_table[frac]);
654                     }
655
656 //#define SCFS_PRINT
657 #ifdef SCFS_PRINT
658                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
659                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
660                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
661                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
662                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
663                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
664                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
665                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
666 #endif
667 #endif
668
669                     gincrease += 4;
670                     k += 4;
671                 }
672                 wa += win_inc;
673             }
674             j += width;
675         }
676         gindex += gincrease;
677     }
678
679     return error;
680 }
681
682 static uint8_t allocate_single_channel(NeAACDecStruct *hDecoder, uint8_t channel,
683                                        uint8_t output_channels)
684 {
685     int mul = 1;
686
687 #ifdef MAIN_DEC
688     /* MAIN object type prediction */
689     if (hDecoder->object_type == MAIN)
690     {
691         /* allocate the state only when needed */
692         if (hDecoder->pred_stat[channel] != NULL)
693         {
694             faad_free(hDecoder->pred_stat[channel]);
695             hDecoder->pred_stat[channel] = NULL;
696         }
697
698         hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
699         reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
700     }
701 #endif
702
703 #ifdef LTP_DEC
704     if (is_ltp_ot(hDecoder->object_type))
705     {
706         /* allocate the state only when needed */
707         if (hDecoder->lt_pred_stat[channel] != NULL)
708         {
709             faad_free(hDecoder->lt_pred_stat[channel]);
710             hDecoder->lt_pred_stat[channel] = NULL;
711         }
712
713         hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
714         memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
715     }
716 #endif
717
718     if (hDecoder->time_out[channel] != NULL)
719     {
720         faad_free(hDecoder->time_out[channel]);
721         hDecoder->time_out[channel] = NULL;
722     }
723
724     {
725         mul = 1;
726 #ifdef SBR_DEC
727         hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
728         if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
729         {
730             /* SBR requires 2 times as much output data */
731             mul = 2;
732             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
733         }
734 #endif
735         hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
736         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
737     }
738
739 #if (defined(PS_DEC) || defined(DRM_PS))
740     if (output_channels == 2)
741     {
742         if (hDecoder->time_out[channel+1] != NULL)
743         {
744             faad_free(hDecoder->time_out[channel+1]);
745             hDecoder->time_out[channel+1] = NULL;
746         }
747
748         hDecoder->time_out[channel+1] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
749         memset(hDecoder->time_out[channel+1], 0, mul*hDecoder->frameLength*sizeof(real_t));
750     }
751 #endif
752
753     if (hDecoder->fb_intermed[channel] != NULL)
754     {
755         faad_free(hDecoder->fb_intermed[channel]);
756         hDecoder->fb_intermed[channel] = NULL;
757     }
758
759     hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
760     memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
761
762 #ifdef SSR_DEC
763     if (hDecoder->object_type == SSR)
764     {
765         if (hDecoder->ssr_overlap[channel] == NULL)
766         {
767             hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
768             memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
769         }
770         if (hDecoder->prev_fmd[channel] == NULL)
771         {
772             uint16_t k;
773             hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
774             for (k = 0; k < 2*hDecoder->frameLength; k++)
775                 hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
776         }
777     }
778 #endif
779
780     return 0;
781 }
782
783 static uint8_t allocate_channel_pair(NeAACDecStruct *hDecoder,
784                                      uint8_t channel, uint8_t paired_channel)
785 {
786     int mul = 1;
787
788 #ifdef MAIN_DEC
789     /* MAIN object type prediction */
790     if (hDecoder->object_type == MAIN)
791     {
792         /* allocate the state only when needed */
793         if (hDecoder->pred_stat[channel] == NULL)
794         {
795             hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
796             reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
797         }
798         if (hDecoder->pred_stat[paired_channel] == NULL)
799         {
800             hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
801             reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength);
802         }
803     }
804 #endif
805
806 #ifdef LTP_DEC
807     if (is_ltp_ot(hDecoder->object_type))
808     {
809         /* allocate the state only when needed */
810         if (hDecoder->lt_pred_stat[channel] == NULL)
811         {
812             hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
813             memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
814         }
815         if (hDecoder->lt_pred_stat[paired_channel] == NULL)
816         {
817             hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
818             memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
819         }
820     }
821 #endif
822
823     if (hDecoder->time_out[channel] == NULL)
824     {
825         mul = 1;
826 #ifdef SBR_DEC
827         hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
828         if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
829         {
830             /* SBR requires 2 times as much output data */
831             mul = 2;
832             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
833         }
834 #endif
835         hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
836         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
837     }
838     if (hDecoder->time_out[paired_channel] == NULL)
839     {
840         hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
841         memset(hDecoder->time_out[paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
842     }
843
844     if (hDecoder->fb_intermed[channel] == NULL)
845     {
846         hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
847         memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
848     }
849     if (hDecoder->fb_intermed[paired_channel] == NULL)
850     {
851         hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
852         memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
853     }
854
855 #ifdef SSR_DEC
856     if (hDecoder->object_type == SSR)
857     {
858         if (hDecoder->ssr_overlap[cpe->channel] == NULL)
859         {
860             hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
861             memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
862         }
863         if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
864         {
865             hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
866             memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
867         }
868         if (hDecoder->prev_fmd[cpe->channel] == NULL)
869         {
870             uint16_t k;
871             hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
872             for (k = 0; k < 2*hDecoder->frameLength; k++)
873                 hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
874         }
875         if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
876         {
877             uint16_t k;
878             hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
879             for (k = 0; k < 2*hDecoder->frameLength; k++)
880                 hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
881         }
882     }
883 #endif
884
885     return 0;
886 }
887
888 uint8_t reconstruct_single_channel(NeAACDecStruct *hDecoder, ic_stream *ics,
889                                    element *sce, int16_t *spec_data)
890 {
891     uint8_t retval;
892     int output_channels;
893     ALIGN real_t spec_coef[1024];
894
895 #ifdef PROFILE
896     int64_t count = faad_get_ts();
897 #endif
898
899
900     /* always allocate 2 channels, PS can always "suddenly" turn up */
901 #if ( (defined(DRM) && defined(DRM_PS)) )
902     output_channels = 2;
903 #elif defined(PS_DEC)
904     if (hDecoder->ps_used[hDecoder->fr_ch_ele])
905         output_channels = 2;
906     else
907         output_channels = 1;
908 #else
909     output_channels = 1;
910 #endif
911
912     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
913     {
914         /* element_output_channels not set yet */
915         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
916     } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
917         /* element inconsistency */
918
919         /* this only happens if PS is actually found but not in the first frame
920          * this means that there is only 1 bitstream element!
921          */
922
923         /* reset the allocation */
924         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 0;
925
926         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
927
928         //return 21;
929     }
930
931     if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
932     {
933         retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
934         if (retval > 0)
935             return retval;
936
937         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
938     }
939
940
941     /* dequantisation and scaling */
942     retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
943     if (retval > 0)
944         return retval;
945
946 #ifdef PROFILE
947     count = faad_get_ts() - count;
948     hDecoder->requant_cycles += count;
949 #endif
950
951
952     /* pns decoding */
953     pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
954         &(hDecoder->__r1), &(hDecoder->__r2));
955
956 #ifdef MAIN_DEC
957     /* MAIN object type prediction */
958     if (hDecoder->object_type == MAIN)
959     {
960                 if (!hDecoder->pred_stat[sce->channel])
961                         return 33;
962
963         /* intra channel prediction */
964         ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
965             hDecoder->sf_index);
966
967         /* In addition, for scalefactor bands coded by perceptual
968            noise substitution the predictors belonging to the
969            corresponding spectral coefficients are reset.
970         */
971         pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
972     }
973 #endif
974
975 #ifdef LTP_DEC
976     if (is_ltp_ot(hDecoder->object_type))
977     {
978 #ifdef LD_DEC
979         if (hDecoder->object_type == LD)
980         {
981             if (ics->ltp.data_present)
982             {
983                 if (ics->ltp.lag_update)
984                     hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
985             }
986             ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
987         }
988 #endif
989
990         /* long term prediction */
991         lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
992             ics->window_shape, hDecoder->window_shape_prev[sce->channel],
993             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
994     }
995 #endif
996
997     /* tns decoding */
998     tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
999         spec_coef, hDecoder->frameLength);
1000
1001     /* drc decoding */
1002     if (hDecoder->drc->present)
1003     {
1004         if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
1005             drc_decode(hDecoder->drc, spec_coef);
1006     }
1007
1008     /* filter bank */
1009 #ifdef SSR_DEC
1010     if (hDecoder->object_type != SSR)
1011     {
1012 #endif
1013         ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
1014             hDecoder->window_shape_prev[sce->channel], spec_coef,
1015             hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel],
1016             hDecoder->object_type, hDecoder->frameLength);
1017 #ifdef SSR_DEC
1018     } else {
1019         ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
1020             hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
1021             hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
1022             hDecoder->frameLength);
1023     }
1024 #endif
1025
1026     /* save window shape for next frame */
1027     hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
1028
1029 #ifdef LTP_DEC
1030     if (is_ltp_ot(hDecoder->object_type))
1031     {
1032         lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel],
1033             hDecoder->fb_intermed[sce->channel], hDecoder->frameLength, hDecoder->object_type);
1034     }
1035 #endif
1036
1037 #ifdef SBR_DEC
1038     if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1039         && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1040     {
1041         int ele = hDecoder->fr_ch_ele;
1042         int ch = sce->channel;
1043
1044         /* following case can happen when forceUpSampling == 1 */
1045         if (hDecoder->sbr[ele] == NULL)
1046         {
1047             hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1048                 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1049                 hDecoder->downSampledSBR
1050 #ifdef DRM
1051                 , 0
1052 #endif
1053                 );
1054         }
1055
1056         if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1057             hDecoder->sbr[ele]->maxAACLine = 8*min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1058         else
1059             hDecoder->sbr[ele]->maxAACLine = min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1060
1061         /* check if any of the PS tools is used */
1062 #if (defined(PS_DEC) || defined(DRM_PS))
1063         if (hDecoder->ps_used[ele] == 0)
1064         {
1065 #endif
1066             retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
1067                 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1068 #if (defined(PS_DEC) || defined(DRM_PS))
1069         } else {
1070             retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
1071                 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
1072                 hDecoder->downSampledSBR);
1073         }
1074 #endif
1075         if (retval > 0)
1076             return retval;
1077     } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1078         && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1079     {
1080         return 23;
1081     }
1082 #endif
1083
1084     /* copy L to R when no PS is used */
1085 #if (defined(PS_DEC) || defined(DRM_PS))
1086     if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) &&
1087         (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2))
1088     {
1089         int ele = hDecoder->fr_ch_ele;
1090         int ch = sce->channel;
1091         int frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
1092         frame_size *= hDecoder->frameLength*sizeof(real_t);
1093
1094         memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
1095     }
1096 #endif
1097
1098     return 0;
1099 }
1100
1101 uint8_t reconstruct_channel_pair(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2,
1102                                  element *cpe, int16_t *spec_data1, int16_t *spec_data2)
1103 {
1104     uint8_t retval;
1105     ALIGN real_t spec_coef1[1024];
1106     ALIGN real_t spec_coef2[1024];
1107
1108 #ifdef PROFILE
1109     int64_t count = faad_get_ts();
1110 #endif
1111     if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
1112     {
1113         retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
1114         if (retval > 0)
1115             return retval;
1116
1117         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
1118     }
1119
1120     /* dequantisation and scaling */
1121     retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
1122     if (retval > 0)
1123         return retval;
1124     retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
1125     if (retval > 0)
1126         return retval;
1127
1128 #ifdef PROFILE
1129     count = faad_get_ts() - count;
1130     hDecoder->requant_cycles += count;
1131 #endif
1132
1133
1134     /* pns decoding */
1135     if (ics1->ms_mask_present)
1136     {
1137         pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type,
1138             &(hDecoder->__r1), &(hDecoder->__r2));
1139     } else {
1140         pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
1141             &(hDecoder->__r1), &(hDecoder->__r2));
1142         pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
1143             &(hDecoder->__r1), &(hDecoder->__r2));
1144     }
1145
1146     /* mid/side decoding */
1147     ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1148
1149 #if 0
1150     {
1151         int i;
1152         for (i = 0; i < 1024; i++)
1153         {
1154             //printf("%d\n", spec_coef1[i]);
1155             printf("0x%.8X\n", spec_coef1[i]);
1156         }
1157         for (i = 0; i < 1024; i++)
1158         {
1159             //printf("%d\n", spec_coef2[i]);
1160             printf("0x%.8X\n", spec_coef2[i]);
1161         }
1162     }
1163 #endif
1164
1165     /* intensity stereo decoding */
1166     is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1167
1168 #if 0
1169     {
1170         int i;
1171         for (i = 0; i < 1024; i++)
1172         {
1173             printf("%d\n", spec_coef1[i]);
1174             //printf("0x%.8X\n", spec_coef1[i]);
1175         }
1176         for (i = 0; i < 1024; i++)
1177         {
1178             printf("%d\n", spec_coef2[i]);
1179             //printf("0x%.8X\n", spec_coef2[i]);
1180         }
1181     }
1182 #endif
1183
1184 #ifdef MAIN_DEC
1185     /* MAIN object type prediction */
1186     if (hDecoder->object_type == MAIN)
1187     {
1188         /* intra channel prediction */
1189         ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
1190             hDecoder->sf_index);
1191         ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
1192             hDecoder->sf_index);
1193
1194         /* In addition, for scalefactor bands coded by perceptual
1195            noise substitution the predictors belonging to the
1196            corresponding spectral coefficients are reset.
1197         */
1198         pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
1199         pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
1200     }
1201 #endif
1202
1203 #ifdef LTP_DEC
1204     if (is_ltp_ot(hDecoder->object_type))
1205     {
1206         ltp_info *ltp1 = &(ics1->ltp);
1207         ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp);
1208 #ifdef LD_DEC
1209         if (hDecoder->object_type == LD)
1210         {
1211             if (ltp1->data_present)
1212             {
1213                 if (ltp1->lag_update)
1214                     hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
1215             }
1216             ltp1->lag = hDecoder->ltp_lag[cpe->channel];
1217             if (ltp2->data_present)
1218             {
1219                 if (ltp2->lag_update)
1220                     hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
1221             }
1222             ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
1223         }
1224 #endif
1225
1226         /* long term prediction */
1227         lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
1228             ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
1229             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1230         lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
1231             ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
1232             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1233     }
1234 #endif
1235
1236     /* tns decoding */
1237     tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
1238         spec_coef1, hDecoder->frameLength);
1239     tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
1240         spec_coef2, hDecoder->frameLength);
1241
1242     /* drc decoding */
1243     if (hDecoder->drc->present)
1244     {
1245         if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
1246             drc_decode(hDecoder->drc, spec_coef1);
1247         if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
1248             drc_decode(hDecoder->drc, spec_coef2);
1249     }
1250
1251     /* filter bank */
1252 #ifdef SSR_DEC
1253     if (hDecoder->object_type != SSR)
1254     {
1255 #endif
1256         ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1257             hDecoder->window_shape_prev[cpe->channel], spec_coef1,
1258             hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
1259             hDecoder->object_type, hDecoder->frameLength);
1260         ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1261             hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1262             hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
1263             hDecoder->object_type, hDecoder->frameLength);
1264 #ifdef SSR_DEC
1265     } else {
1266         ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1267             hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
1268             hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
1269             hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
1270         ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1271             hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
1272             hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
1273             hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
1274     }
1275 #endif
1276
1277     /* save window shape for next frame */
1278     hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
1279     hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
1280
1281 #ifdef LTP_DEC
1282     if (is_ltp_ot(hDecoder->object_type))
1283     {
1284         lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
1285             hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type);
1286         lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
1287             hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type);
1288     }
1289 #endif
1290
1291 #ifdef SBR_DEC
1292     if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1293         && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1294     {
1295         int ele = hDecoder->fr_ch_ele;
1296         int ch0 = cpe->channel;
1297         int ch1 = cpe->paired_channel;
1298
1299         /* following case can happen when forceUpSampling == 1 */
1300         if (hDecoder->sbr[ele] == NULL)
1301         {
1302             hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1303                 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1304                 hDecoder->downSampledSBR
1305 #ifdef DRM
1306                 , 0
1307 #endif
1308                 );
1309         }
1310
1311         if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1312             hDecoder->sbr[ele]->maxAACLine = 8*min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1313         else
1314             hDecoder->sbr[ele]->maxAACLine = min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1315
1316         retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1317             hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1318             hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1319         if (retval > 0)
1320             return retval;
1321     } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1322         && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1323     {
1324         return 23;
1325     }
1326 #endif
1327
1328     return 0;
1329 }