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