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_predict.h"
53 #include "ic_predict.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 */
window_grouping_info(NeAACDecStruct * hDecoder,ic_stream * ics)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         {
326             if (hDecoder->frameLength == 1024)
327                 ics->num_swb = num_swb_1024_window[sf_index];
328             else /* if (hDecoder->frameLength == 960) */
329                 ics->num_swb = num_swb_960_window[sf_index];
330         }
331 
332         if (ics->max_sfb > ics->num_swb)
333         {
334             return 32;
335         }
336 
337         /* preparation of sect_sfb_offset for long blocks */
338         /* also copy the last value! */
339 #ifdef LD_DEC
340         if (hDecoder->object_type == LD)
341         {
342             if (hDecoder->frameLength == 512)
343             {
344                 for (i = 0; i < ics->num_swb; i++)
345                 {
346                     ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i];
347                     ics->swb_offset[i] = swb_offset_512_window[sf_index][i];
348                 }
349             } else /* if (hDecoder->frameLength == 480) */ {
350                 for (i = 0; i < ics->num_swb; i++)
351                 {
352                     ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i];
353                     ics->swb_offset[i] = swb_offset_480_window[sf_index][i];
354                 }
355             }
356             ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
357             ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
358             ics->swb_offset_max = hDecoder->frameLength;
359         } else
360 #endif
361         {
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         }
371         return 0;
372     case EIGHT_SHORT_SEQUENCE:
373         ics->num_windows = 8;
374         ics->num_window_groups = 1;
375         ics->window_group_length[ics->num_window_groups-1] = 1;
376         ics->num_swb = num_swb_128_window[sf_index];
377 
378         if (ics->max_sfb > ics->num_swb)
379         {
380             return 32;
381         }
382 
383         for (i = 0; i < ics->num_swb; i++)
384             ics->swb_offset[i] = swb_offset_128_window[sf_index][i];
385         ics->swb_offset[ics->num_swb] = hDecoder->frameLength/8;
386         ics->swb_offset_max = hDecoder->frameLength/8;
387 
388         for (i = 0; i < ics->num_windows-1; i++) {
389             if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
390             {
391                 ics->num_window_groups += 1;
392                 ics->window_group_length[ics->num_window_groups-1] = 1;
393             } else {
394                 ics->window_group_length[ics->num_window_groups-1] += 1;
395             }
396         }
397 
398         /* preparation of sect_sfb_offset for short blocks */
399         for (g = 0; g < ics->num_window_groups; g++)
400         {
401             uint16_t width;
402             uint8_t sect_sfb = 0;
403             uint16_t offset = 0;
404 
405             for (i = 0; i < ics->num_swb; i++)
406             {
407                 if (i+1 == ics->num_swb)
408                 {
409                     width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
410                 } else {
411                     width = swb_offset_128_window[sf_index][i+1] -
412                         swb_offset_128_window[sf_index][i];
413                 }
414                 width *= ics->window_group_length[g];
415                 ics->sect_sfb_offset[g][sect_sfb++] = offset;
416                 offset += width;
417             }
418             ics->sect_sfb_offset[g][sect_sfb] = offset;
419         }
420         return 0;
421     default:
422         return 32;
423     }
424 }
425 
426 /* iquant() */
427 /* output = sign(input)*abs(input)^(4/3) */
428 /**/
iquant(int16_t q,const real_t * tab,uint8_t * error)429 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
430 {
431 #ifdef FIXED_POINT
432 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
433 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
434  * defined a 1026 value table and interpolation will be used
435  */
436 #ifndef BIG_IQ_TABLE
437     static const real_t errcorr[] = {
438         REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
439         REAL_CONST(4.0/8.0),  REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
440         REAL_CONST(0)
441     };
442     real_t x1, x2;
443 #endif
444     int16_t sgn = 1;
445 
446     if (q < 0)
447     {
448         q = -q;
449         sgn = -1;
450     }
451 
452     if (q < IQ_TABLE_SIZE)
453     {
454 //#define IQUANT_PRINT
455 #ifdef IQUANT_PRINT
456         //printf("0x%.8X\n", sgn * tab[q]);
457         printf("%d\n", sgn * tab[q]);
458 #endif
459         return sgn * tab[q];
460     }
461 
462 #ifndef BIG_IQ_TABLE
463     if (q >= 8192)
464     {
465         *error = 17;
466         return 0;
467     }
468 
469     /* linear interpolation */
470     x1 = tab[q>>3];
471     x2 = tab[(q>>3) + 1];
472     return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
473 #else
474     *error = 17;
475     return 0;
476 #endif
477 
478 #else
479     if (q < 0)
480     {
481         /* tab contains a value for all possible q [0,8192] */
482         if (-q < IQ_TABLE_SIZE)
483             return -tab[-q];
484 
485         *error = 17;
486         return 0;
487     } else {
488         /* tab contains a value for all possible q [0,8192] */
489         if (q < IQ_TABLE_SIZE)
490             return tab[q];
491 
492         *error = 17;
493         return 0;
494     }
495 #endif
496 }
497 
498 #ifndef FIXED_POINT
499 ALIGN static const real_t pow2sf_tab[] = {
500     2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
501     2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007,
502     1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006,
503     1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005,
504     0.0001220703125, 0.000244140625, 0.00048828125,
505     0.0009765625, 0.001953125, 0.00390625,
506     0.0078125, 0.015625, 0.03125,
507     0.0625, 0.125, 0.25,
508     0.5, 1.0, 2.0,
509     4.0, 8.0, 16.0, 32.0,
510     64.0, 128.0, 256.0,
511     512.0, 1024.0, 2048.0,
512     4096.0, 8192.0, 16384.0,
513     32768.0, 65536.0, 131072.0,
514     262144.0, 524288.0, 1048576.0,
515     2097152.0, 4194304.0, 8388608.0,
516     16777216.0, 33554432.0, 67108864.0,
517     134217728.0, 268435456.0, 536870912.0,
518     1073741824.0, 2147483648.0, 4294967296.0,
519     8589934592.0, 17179869184.0, 34359738368.0,
520     68719476736.0, 137438953472.0, 274877906944.0
521 };
522 #endif
523 
524 /* quant_to_spec: perform dequantisation and scaling
525  * and in case of short block it also does the deinterleaving
526  */
527 /*
528   For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
529   window_group_length[0] = 1) the spectral data is in ascending spectral
530   order.
531   For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
532   grouping in the following manner:
533   - Groups are ordered sequentially
534   - Within a group, a scalefactor band consists of the spectral data of all
535     grouped SHORT_WINDOWs for the associated scalefactor window band. To
536     clarify via example, the length of a group is in the range of one to eight
537     SHORT_WINDOWs.
538   - If there are eight groups each with length one (num_window_groups = 8,
539     window_group_length[0..7] = 1), the result is a sequence of eight spectra,
540     each in ascending spectral order.
541   - If there is only one group with length eight (num_window_groups = 1,
542     window_group_length[0] = 8), the result is that spectral data of all eight
543     SHORT_WINDOWs is interleaved by scalefactor window bands.
544   - Within a scalefactor window band, the coefficients are in ascending
545     spectral order.
546 */
quant_to_spec(NeAACDecStruct * hDecoder,ic_stream * ics,int16_t * quant_data,real_t * spec_data,uint16_t frame_len)547 static uint8_t quant_to_spec(NeAACDecStruct *hDecoder,
548                              ic_stream *ics, int16_t *quant_data,
549                              real_t *spec_data, uint16_t frame_len)
550 {
551     ALIGN static const real_t pow2_table[] =
552     {
553         COEF_CONST(1.0),
554         COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
555         COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
556         COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
557     };
558     const real_t *tab = iq_table;
559 
560     uint8_t g, sfb, win;
561     uint16_t width, bin, k, gindex, wa, wb;
562     uint8_t error = 0; /* Init error flag */
563 #ifndef FIXED_POINT
564     real_t scf;
565 #endif
566 
567     (void)frame_len; /* FIXME: check */
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 #else
609             (void)hDecoder;
610 #endif
611 
612             wa = gindex + j;
613 
614 #ifndef FIXED_POINT
615             scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
616 #endif
617 
618             for (win = 0; win < ics->window_group_length[g]; win++)
619             {
620                 for (bin = 0; bin < width; bin += 4)
621                 {
622 #ifndef FIXED_POINT
623                     wb = wa + bin;
624 
625                     spec_data[wb+0] = iquant(quant_data[k+0], tab, &error) * scf;
626                     spec_data[wb+1] = iquant(quant_data[k+1], tab, &error) * scf;
627                     spec_data[wb+2] = iquant(quant_data[k+2], tab, &error) * scf;
628                     spec_data[wb+3] = iquant(quant_data[k+3], tab, &error) * scf;
629 
630 #else
631                     real_t iq0 = iquant(quant_data[k+0], tab, &error);
632                     real_t iq1 = iquant(quant_data[k+1], tab, &error);
633                     real_t iq2 = iquant(quant_data[k+2], tab, &error);
634                     real_t iq3 = iquant(quant_data[k+3], tab, &error);
635 
636                     wb = wa + bin;
637 
638                     if (exp < 0)
639                     {
640                         spec_data[wb+0] = iq0 >>= -exp;
641                         spec_data[wb+1] = iq1 >>= -exp;
642                         spec_data[wb+2] = iq2 >>= -exp;
643                         spec_data[wb+3] = iq3 >>= -exp;
644                     } else {
645                         spec_data[wb+0] = iq0 <<= exp;
646                         spec_data[wb+1] = iq1 <<= exp;
647                         spec_data[wb+2] = iq2 <<= exp;
648                         spec_data[wb+3] = iq3 <<= exp;
649                     }
650                     if (frac != 0)
651                     {
652                         spec_data[wb+0] = MUL_C(spec_data[wb+0],pow2_table[frac]);
653                         spec_data[wb+1] = MUL_C(spec_data[wb+1],pow2_table[frac]);
654                         spec_data[wb+2] = MUL_C(spec_data[wb+2],pow2_table[frac]);
655                         spec_data[wb+3] = MUL_C(spec_data[wb+3],pow2_table[frac]);
656                     }
657 
658 //#define SCFS_PRINT
659 #ifdef SCFS_PRINT
660                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
661                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
662                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
663                     printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
664                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
665                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
666                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
667                     //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
668 #endif
669 #endif
670 
671                     gincrease += 4;
672                     k += 4;
673                 }
674                 wa += win_inc;
675             }
676             j += width;
677         }
678         gindex += gincrease;
679     }
680 
681     return error;
682 }
683 
allocate_single_channel(NeAACDecStruct * hDecoder,uint8_t channel,uint8_t output_channels)684 static uint8_t allocate_single_channel(NeAACDecStruct *hDecoder, uint8_t channel,
685                                        uint8_t output_channels)
686 {
687     int mul = 1;
688 
689 #ifdef MAIN_DEC
690     /* MAIN object type prediction */
691     if (hDecoder->object_type == MAIN)
692     {
693         /* allocate the state only when needed */
694         if (hDecoder->pred_stat[channel] != NULL)
695         {
696             faad_free(hDecoder->pred_stat[channel]);
697             hDecoder->pred_stat[channel] = NULL;
698         }
699 
700         hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
701         reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
702     }
703 #endif
704 
705 #ifdef LTP_DEC
706     if (is_ltp_ot(hDecoder->object_type))
707     {
708         /* allocate the state only when needed */
709         if (hDecoder->lt_pred_stat[channel] != NULL)
710         {
711             faad_free(hDecoder->lt_pred_stat[channel]);
712             hDecoder->lt_pred_stat[channel] = NULL;
713         }
714 
715         hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
716         memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
717     }
718 #endif
719 
720     if (hDecoder->time_out[channel] != NULL)
721     {
722         faad_free(hDecoder->time_out[channel]);
723         hDecoder->time_out[channel] = NULL;
724     }
725 
726     {
727         mul = 1;
728 #ifdef SBR_DEC
729         hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
730         if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
731         {
732             /* SBR requires 2 times as much output data */
733             mul = 2;
734             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
735         }
736 #endif
737         hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
738         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
739     }
740 
741 #if (defined(PS_DEC) || defined(DRM_PS))
742     if (output_channels == 2)
743     {
744         if (hDecoder->time_out[channel+1] != NULL)
745         {
746             faad_free(hDecoder->time_out[channel+1]);
747             hDecoder->time_out[channel+1] = NULL;
748         }
749 
750         hDecoder->time_out[channel+1] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
751         memset(hDecoder->time_out[channel+1], 0, mul*hDecoder->frameLength*sizeof(real_t));
752     }
753 #endif
754 
755     if (hDecoder->fb_intermed[channel] != NULL)
756     {
757         faad_free(hDecoder->fb_intermed[channel]);
758         hDecoder->fb_intermed[channel] = NULL;
759     }
760 
761     hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
762     memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
763 
764 #ifdef SSR_DEC
765     if (hDecoder->object_type == SSR)
766     {
767         if (hDecoder->ssr_overlap[channel] == NULL)
768         {
769             hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
770             memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
771         }
772         if (hDecoder->prev_fmd[channel] == NULL)
773         {
774             uint16_t k;
775             hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
776             for (k = 0; k < 2*hDecoder->frameLength; k++)
777                 hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
778         }
779     }
780 #endif
781 
782     return 0;
783 }
784 
allocate_channel_pair(NeAACDecStruct * hDecoder,uint8_t channel,uint8_t paired_channel)785 static uint8_t allocate_channel_pair(NeAACDecStruct *hDecoder,
786                                      uint8_t channel, uint8_t paired_channel)
787 {
788     int mul = 1;
789 
790 #ifdef MAIN_DEC
791     /* MAIN object type prediction */
792     if (hDecoder->object_type == MAIN)
793     {
794         /* allocate the state only when needed */
795         if (hDecoder->pred_stat[channel] == NULL)
796         {
797             hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
798             reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
799         }
800         if (hDecoder->pred_stat[paired_channel] == NULL)
801         {
802             hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
803             reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength);
804         }
805     }
806 #endif
807 
808 #ifdef LTP_DEC
809     if (is_ltp_ot(hDecoder->object_type))
810     {
811         /* allocate the state only when needed */
812         if (hDecoder->lt_pred_stat[channel] == NULL)
813         {
814             hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
815             memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
816         }
817         if (hDecoder->lt_pred_stat[paired_channel] == NULL)
818         {
819             hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
820             memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
821         }
822     }
823 #endif
824 
825     if (hDecoder->time_out[channel] == NULL)
826     {
827         mul = 1;
828 #ifdef SBR_DEC
829         hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
830         if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
831         {
832             /* SBR requires 2 times as much output data */
833             mul = 2;
834             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
835         }
836 #endif
837         hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
838         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
839     }
840     if (hDecoder->time_out[paired_channel] == NULL)
841     {
842         hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
843         memset(hDecoder->time_out[paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
844     }
845 
846     if (hDecoder->fb_intermed[channel] == NULL)
847     {
848         hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
849         memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
850     }
851     if (hDecoder->fb_intermed[paired_channel] == NULL)
852     {
853         hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
854         memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
855     }
856 
857 #ifdef SSR_DEC
858     if (hDecoder->object_type == SSR)
859     {
860         if (hDecoder->ssr_overlap[cpe->channel] == NULL)
861         {
862             hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
863             memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
864         }
865         if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
866         {
867             hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
868             memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
869         }
870         if (hDecoder->prev_fmd[cpe->channel] == NULL)
871         {
872             uint16_t k;
873             hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
874             for (k = 0; k < 2*hDecoder->frameLength; k++)
875                 hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
876         }
877         if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
878         {
879             uint16_t k;
880             hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
881             for (k = 0; k < 2*hDecoder->frameLength; k++)
882                 hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
883         }
884     }
885 #endif
886 
887     return 0;
888 }
889 
reconstruct_single_channel(NeAACDecStruct * hDecoder,ic_stream * ics,element * sce,int16_t * spec_data)890 uint8_t reconstruct_single_channel(NeAACDecStruct *hDecoder, ic_stream *ics,
891                                    element *sce, int16_t *spec_data)
892 {
893     uint8_t retval;
894     int output_channels;
895     ALIGN real_t spec_coef[1024];
896 
897 #ifdef PROFILE
898     int64_t count = faad_get_ts();
899 #endif
900 
901 
902     /* always allocate 2 channels, PS can always "suddenly" turn up */
903 #if ( (defined(DRM) && defined(DRM_PS)) )
904     output_channels = 2;
905 #elif defined(PS_DEC)
906     if (hDecoder->ps_used[hDecoder->fr_ch_ele])
907         output_channels = 2;
908     else
909         output_channels = 1;
910 #else
911     output_channels = 1;
912 #endif
913 
914     if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
915     {
916         /* element_output_channels not set yet */
917         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
918     } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
919         /* element inconsistency */
920 
921         /* this only happens if PS is actually found but not in the first frame
922          * this means that there is only 1 bitstream element!
923          */
924 
925         /* reset the allocation */
926         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 0;
927 
928         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
929 
930         //return 21;
931     }
932 
933     if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
934     {
935         retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
936         if (retval > 0)
937             return retval;
938 
939         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
940     }
941 
942 
943     /* dequantisation and scaling */
944     retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
945     if (retval > 0)
946         return retval;
947 
948 #ifdef PROFILE
949     count = faad_get_ts() - count;
950     hDecoder->requant_cycles += count;
951 #endif
952 
953 
954     /* pns decoding */
955     pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
956         &(hDecoder->__r1), &(hDecoder->__r2));
957 
958 #ifdef MAIN_DEC
959     /* MAIN object type prediction */
960     if (hDecoder->object_type == MAIN)
961     {
962 		if (!hDecoder->pred_stat[sce->channel])
963 			return 33;
964 
965         /* intra channel prediction */
966         ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
967             hDecoder->sf_index);
968 
969         /* In addition, for scalefactor bands coded by perceptual
970            noise substitution the predictors belonging to the
971            corresponding spectral coefficients are reset.
972         */
973         pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
974     }
975 #endif
976 
977 #ifdef LTP_DEC
978     if (is_ltp_ot(hDecoder->object_type))
979     {
980 #ifdef LD_DEC
981         if (hDecoder->object_type == LD)
982         {
983             if (ics->ltp.data_present)
984             {
985                 if (ics->ltp.lag_update)
986                     hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
987             }
988             ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
989         }
990 #endif
991 
992         /* long term prediction */
993         lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
994             ics->window_shape, hDecoder->window_shape_prev[sce->channel],
995             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
996     }
997 #endif
998 
999     /* tns decoding */
1000     tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
1001         spec_coef, hDecoder->frameLength);
1002 
1003     /* drc decoding */
1004     if (hDecoder->drc->present)
1005     {
1006         if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
1007             drc_decode(hDecoder->drc, spec_coef);
1008     }
1009 
1010     /* filter bank */
1011 #ifdef SSR_DEC
1012     if (hDecoder->object_type != SSR)
1013 #endif
1014     {
1015         ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
1016             hDecoder->window_shape_prev[sce->channel], spec_coef,
1017             hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel],
1018             hDecoder->object_type, hDecoder->frameLength);
1019     }
1020 #ifdef SSR_DEC
1021     else {
1022         ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
1023             hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
1024             hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
1025             hDecoder->frameLength);
1026     }
1027 #endif
1028 
1029     /* save window shape for next frame */
1030     hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
1031 
1032 #ifdef LTP_DEC
1033     if (is_ltp_ot(hDecoder->object_type))
1034     {
1035         lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel],
1036             hDecoder->fb_intermed[sce->channel], hDecoder->frameLength, hDecoder->object_type);
1037     }
1038 #endif
1039 
1040 #ifdef SBR_DEC
1041     if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1042         && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1043     {
1044         int ele = hDecoder->fr_ch_ele;
1045         int ch = sce->channel;
1046 
1047         /* following case can happen when forceUpSampling == 1 */
1048         if (hDecoder->sbr[ele] == NULL)
1049         {
1050             hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1051                 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1052                 hDecoder->downSampledSBR
1053 #ifdef DRM
1054                 , 0
1055 #endif
1056                 );
1057         }
1058 
1059         if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1060             hDecoder->sbr[ele]->maxAACLine = 8*min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1061         else
1062             hDecoder->sbr[ele]->maxAACLine = min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1063 
1064         /* check if any of the PS tools is used */
1065 #if (defined(PS_DEC) || defined(DRM_PS))
1066         if (hDecoder->ps_used[ele] == 0)
1067 #endif
1068         {
1069             retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
1070                 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1071         }
1072 #if (defined(PS_DEC) || defined(DRM_PS))
1073         else {
1074             retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
1075                 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
1076                 hDecoder->downSampledSBR);
1077         }
1078 #endif
1079         if (retval > 0)
1080             return retval;
1081     } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1082         && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1083     {
1084         return 23;
1085     }
1086 #endif
1087 
1088     /* copy L to R when no PS is used */
1089 #if (defined(PS_DEC) || defined(DRM_PS))
1090     if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) &&
1091         (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2))
1092     {
1093         int ele = hDecoder->fr_ch_ele;
1094         int ch = sce->channel;
1095         int frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
1096         frame_size *= hDecoder->frameLength*sizeof(real_t);
1097 
1098         memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
1099     }
1100 #endif
1101 
1102     return 0;
1103 }
1104 
reconstruct_channel_pair(NeAACDecStruct * hDecoder,ic_stream * ics1,ic_stream * ics2,element * cpe,int16_t * spec_data1,int16_t * spec_data2)1105 uint8_t reconstruct_channel_pair(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2,
1106                                  element *cpe, int16_t *spec_data1, int16_t *spec_data2)
1107 {
1108     uint8_t retval;
1109     ALIGN real_t spec_coef1[1024];
1110     ALIGN real_t spec_coef2[1024];
1111 
1112 #ifdef PROFILE
1113     int64_t count = faad_get_ts();
1114 #endif
1115     if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
1116     {
1117         retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
1118         if (retval > 0)
1119             return retval;
1120 
1121         hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
1122     }
1123 
1124     /* dequantisation and scaling */
1125     retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
1126     if (retval > 0)
1127         return retval;
1128     retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
1129     if (retval > 0)
1130         return retval;
1131 
1132 #ifdef PROFILE
1133     count = faad_get_ts() - count;
1134     hDecoder->requant_cycles += count;
1135 #endif
1136 
1137 
1138     /* pns decoding */
1139     if (ics1->ms_mask_present)
1140     {
1141         pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type,
1142             &(hDecoder->__r1), &(hDecoder->__r2));
1143     } else {
1144         pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
1145             &(hDecoder->__r1), &(hDecoder->__r2));
1146         pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
1147             &(hDecoder->__r1), &(hDecoder->__r2));
1148     }
1149 
1150     /* mid/side decoding */
1151     ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1152 
1153 #if 0
1154     {
1155         int i;
1156         for (i = 0; i < 1024; i++)
1157         {
1158             //printf("%d\n", spec_coef1[i]);
1159             printf("0x%.8X\n", spec_coef1[i]);
1160         }
1161         for (i = 0; i < 1024; i++)
1162         {
1163             //printf("%d\n", spec_coef2[i]);
1164             printf("0x%.8X\n", spec_coef2[i]);
1165         }
1166     }
1167 #endif
1168 
1169     /* intensity stereo decoding */
1170     is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1171 
1172 #if 0
1173     {
1174         int i;
1175         for (i = 0; i < 1024; i++)
1176         {
1177             printf("%d\n", spec_coef1[i]);
1178             //printf("0x%.8X\n", spec_coef1[i]);
1179         }
1180         for (i = 0; i < 1024; i++)
1181         {
1182             printf("%d\n", spec_coef2[i]);
1183             //printf("0x%.8X\n", spec_coef2[i]);
1184         }
1185     }
1186 #endif
1187 
1188 #ifdef MAIN_DEC
1189     /* MAIN object type prediction */
1190     if (hDecoder->object_type == MAIN)
1191     {
1192         /* intra channel prediction */
1193         ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
1194             hDecoder->sf_index);
1195         ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
1196             hDecoder->sf_index);
1197 
1198         /* In addition, for scalefactor bands coded by perceptual
1199            noise substitution the predictors belonging to the
1200            corresponding spectral coefficients are reset.
1201         */
1202         pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
1203         pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
1204     }
1205 #endif
1206 
1207 #ifdef LTP_DEC
1208     if (is_ltp_ot(hDecoder->object_type))
1209     {
1210         ltp_info *ltp1 = &(ics1->ltp);
1211         ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp);
1212 #ifdef LD_DEC
1213         if (hDecoder->object_type == LD)
1214         {
1215             if (ltp1->data_present)
1216             {
1217                 if (ltp1->lag_update)
1218                     hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
1219             }
1220             ltp1->lag = hDecoder->ltp_lag[cpe->channel];
1221             if (ltp2->data_present)
1222             {
1223                 if (ltp2->lag_update)
1224                     hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
1225             }
1226             ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
1227         }
1228 #endif
1229 
1230         /* long term prediction */
1231         lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
1232             ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
1233             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1234         lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
1235             ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
1236             hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1237     }
1238 #endif
1239 
1240     /* tns decoding */
1241     tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
1242         spec_coef1, hDecoder->frameLength);
1243     tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
1244         spec_coef2, hDecoder->frameLength);
1245 
1246     /* drc decoding */
1247     if (hDecoder->drc->present)
1248     {
1249         if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
1250             drc_decode(hDecoder->drc, spec_coef1);
1251         if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
1252             drc_decode(hDecoder->drc, spec_coef2);
1253     }
1254 
1255     /* filter bank */
1256 #ifdef SSR_DEC
1257     if (hDecoder->object_type != SSR)
1258 #endif
1259     {
1260         ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1261             hDecoder->window_shape_prev[cpe->channel], spec_coef1,
1262             hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
1263             hDecoder->object_type, hDecoder->frameLength);
1264         ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1265             hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1266             hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
1267             hDecoder->object_type, hDecoder->frameLength);
1268     }
1269 #ifdef SSR_DEC
1270     else {
1271         ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1272             hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
1273             hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
1274             hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
1275         ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1276             hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
1277             hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
1278             hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
1279     }
1280 #endif
1281 
1282     /* save window shape for next frame */
1283     hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
1284     hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
1285 
1286 #ifdef LTP_DEC
1287     if (is_ltp_ot(hDecoder->object_type))
1288     {
1289         lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
1290             hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type);
1291         lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
1292             hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type);
1293     }
1294 #endif
1295 
1296 #ifdef SBR_DEC
1297     if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1298         && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1299     {
1300         int ele = hDecoder->fr_ch_ele;
1301         int ch0 = cpe->channel;
1302         int ch1 = cpe->paired_channel;
1303 
1304         /* following case can happen when forceUpSampling == 1 */
1305         if (hDecoder->sbr[ele] == NULL)
1306         {
1307             hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1308                 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1309                 hDecoder->downSampledSBR
1310 #ifdef DRM
1311                 , 0
1312 #endif
1313                 );
1314         }
1315 
1316         if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1317             hDecoder->sbr[ele]->maxAACLine = 8*min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1318         else
1319             hDecoder->sbr[ele]->maxAACLine = min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1320 
1321         retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1322             hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1323             hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1324         if (retval > 0)
1325             return retval;
1326     } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1327         && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1328     {
1329         return 23;
1330     }
1331 #endif
1332 
1333     return 0;
1334 }
1335