2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
29 Spectral reconstruction:
31 - inverse quantization
32 - applying scalefactors
49 #include "lt_predict.h"
50 #include "ic_predict.h"
56 /* static function declarations */
57 static uint8_t quant_to_spec(NeAACDecHandle hDecoder
,
58 ic_stream
*ics
, int16_t *quant_data
,
59 real_t
*spec_data
, uint16_t frame_len
);
63 static const uint8_t num_swb_512_window
[] =
65 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
67 static const uint8_t num_swb_480_window
[] =
69 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
73 static const uint8_t num_swb_960_window
[] ICONST_ATTR
=
75 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
78 static const uint8_t num_swb_1024_window
[] ICONST_ATTR
=
80 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
83 static const uint8_t num_swb_128_window
[] ICONST_ATTR
=
85 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
88 static const uint16_t swb_offset_1024_96
[] =
90 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
91 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240,
92 276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
95 static const uint16_t swb_offset_128_96
[] =
97 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
100 static const uint16_t swb_offset_1024_64
[] =
102 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
103 64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268,
104 304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824,
105 864, 904, 944, 984, 1024
108 static const uint16_t swb_offset_128_64
[] =
110 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
113 static const uint16_t swb_offset_1024_48
[] ICONST_ATTR
=
115 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
116 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
117 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
118 768, 800, 832, 864, 896, 928, 1024
122 static const uint16_t swb_offset_512_48
[] =
124 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84,
125 92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396,
129 static const uint16_t swb_offset_480_48
[] =
131 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 ,80 ,88,
132 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400,
137 static const uint16_t swb_offset_128_48
[] ICONST_ATTR
=
139 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
142 static const uint16_t swb_offset_1024_32
[] ICONST_ATTR
=
144 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
145 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
146 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
147 768, 800, 832, 864, 896, 928, 960, 992, 1024
151 static const uint16_t swb_offset_512_32
[] =
153 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80,
154 88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352,
155 384, 416, 448, 480, 512
158 static const uint16_t swb_offset_480_32
[] =
160 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80,
161 88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352,
166 static const uint16_t swb_offset_1024_24
[] ICONST_ATTR
=
168 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
169 76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220,
170 240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704,
171 768, 832, 896, 960, 1024
175 static const uint16_t swb_offset_512_24
[] =
177 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
178 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416,
182 static const uint16_t swb_offset_480_24
[] =
184 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120,
185 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480
189 static const uint16_t swb_offset_128_24
[] ICONST_ATTR
=
191 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
194 static const uint16_t swb_offset_1024_16
[] ICONST_ATTR
=
196 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124,
197 136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344,
198 368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024
201 static const uint16_t swb_offset_128_16
[] ICONST_ATTR
=
203 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
206 static const uint16_t swb_offset_1024_8
[] ICONST_ATTR
=
208 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172,
209 188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448,
210 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024
213 static const uint16_t swb_offset_128_8
[] ICONST_ATTR
=
215 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
218 static const uint16_t *const swb_offset_1024_window
[] ICONST_ATTR
=
220 swb_offset_1024_96
, /* 96000 */
221 swb_offset_1024_96
, /* 88200 */
222 swb_offset_1024_64
, /* 64000 */
223 swb_offset_1024_48
, /* 48000 */
224 swb_offset_1024_48
, /* 44100 */
225 swb_offset_1024_32
, /* 32000 */
226 swb_offset_1024_24
, /* 24000 */
227 swb_offset_1024_24
, /* 22050 */
228 swb_offset_1024_16
, /* 16000 */
229 swb_offset_1024_16
, /* 12000 */
230 swb_offset_1024_16
, /* 11025 */
231 swb_offset_1024_8
/* 8000 */
235 static const uint16_t *swb_offset_512_window
[] =
240 swb_offset_512_48
, /* 48000 */
241 swb_offset_512_48
, /* 44100 */
242 swb_offset_512_32
, /* 32000 */
243 swb_offset_512_24
, /* 24000 */
244 swb_offset_512_24
, /* 22050 */
251 static const uint16_t *swb_offset_480_window
[] =
256 swb_offset_480_48
, /* 48000 */
257 swb_offset_480_48
, /* 44100 */
258 swb_offset_480_32
, /* 32000 */
259 swb_offset_480_24
, /* 24000 */
260 swb_offset_480_24
, /* 22050 */
268 static const uint16_t *const swb_offset_128_window
[] ICONST_ATTR
=
270 swb_offset_128_96
, /* 96000 */
271 swb_offset_128_96
, /* 88200 */
272 swb_offset_128_64
, /* 64000 */
273 swb_offset_128_48
, /* 48000 */
274 swb_offset_128_48
, /* 44100 */
275 swb_offset_128_48
, /* 32000 */
276 swb_offset_128_24
, /* 24000 */
277 swb_offset_128_24
, /* 22050 */
278 swb_offset_128_16
, /* 16000 */
279 swb_offset_128_16
, /* 12000 */
280 swb_offset_128_16
, /* 11025 */
281 swb_offset_128_8
/* 8000 */
284 #define bit_set(A, B) ((A) & (1<<(B)))
286 /* static variables */
287 /* used by reconstruct_single_channel() and reconstruct_channel_pair() */
288 static real_t spec_coef1
[FRAME_LEN
] IBSS_ATTR MEM_ALIGN_ATTR
;
289 static real_t spec_coef2
[FRAME_LEN
] IBSS_ATTR MEM_ALIGN_ATTR
;
293 - determine the number of windows in a window_sequence named num_windows
294 - determine the number of window_groups named num_window_groups
295 - determine the number of windows in each group named window_group_length[g]
296 - determine the total number of scalefactor window bands named num_swb for
297 the actual window type
298 - determine swb_offset[swb], the offset of the first coefficient in
299 scalefactor window band named swb of the window actually used
300 - determine sect_sfb_offset[g][section],the offset of the first coefficient
301 in section named section. This offset depends on window_sequence and
302 scale_factor_grouping and is needed to decode the spectral_data().
304 uint8_t window_grouping_info(NeAACDecHandle hDecoder
, ic_stream
*ics
)
308 uint8_t sf_index
= hDecoder
->sf_index
;
310 switch (ics
->window_sequence
) {
311 case ONLY_LONG_SEQUENCE
:
312 case LONG_START_SEQUENCE
:
313 case LONG_STOP_SEQUENCE
:
314 ics
->num_windows
= 1;
315 ics
->num_window_groups
= 1;
316 ics
->window_group_length
[ics
->num_window_groups
-1] = 1;
318 if (hDecoder
->object_type
== LD
)
320 if (hDecoder
->frameLength
== 512)
321 ics
->num_swb
= num_swb_512_window
[sf_index
];
322 else /* if (hDecoder->frameLength == 480) */
323 ics
->num_swb
= num_swb_480_window
[sf_index
];
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
];
334 /* preparation of sect_sfb_offset for long blocks */
335 /* also copy the last value! */
337 if (hDecoder
->object_type
== LD
)
339 if (hDecoder
->frameLength
== 512)
341 for (i
= 0; i
< ics
->num_swb
; i
++)
343 ics
->sect_sfb_offset
[0][i
] = swb_offset_512_window
[sf_index
][i
];
344 ics
->swb_offset
[i
] = swb_offset_512_window
[sf_index
][i
];
346 } else /* if (hDecoder->frameLength == 480) */ {
347 for (i
= 0; i
< ics
->num_swb
; i
++)
349 ics
->sect_sfb_offset
[0][i
] = swb_offset_480_window
[sf_index
][i
];
350 ics
->swb_offset
[i
] = swb_offset_480_window
[sf_index
][i
];
353 ics
->sect_sfb_offset
[0][ics
->num_swb
] = hDecoder
->frameLength
;
354 ics
->swb_offset
[ics
->num_swb
] = hDecoder
->frameLength
;
357 for (i
= 0; i
< ics
->num_swb
; i
++)
359 ics
->sect_sfb_offset
[0][i
] = swb_offset_1024_window
[sf_index
][i
];
360 ics
->swb_offset
[i
] = swb_offset_1024_window
[sf_index
][i
];
362 ics
->sect_sfb_offset
[0][ics
->num_swb
] = hDecoder
->frameLength
;
363 ics
->swb_offset
[ics
->num_swb
] = hDecoder
->frameLength
;
368 case EIGHT_SHORT_SEQUENCE
:
369 ics
->num_windows
= 8;
370 ics
->num_window_groups
= 1;
371 ics
->window_group_length
[ics
->num_window_groups
-1] = 1;
372 ics
->num_swb
= num_swb_128_window
[sf_index
];
374 for (i
= 0; i
< ics
->num_swb
; i
++)
375 ics
->swb_offset
[i
] = swb_offset_128_window
[sf_index
][i
];
376 ics
->swb_offset
[ics
->num_swb
] = hDecoder
->frameLength
/8;
378 for (i
= 0; i
< ics
->num_windows
-1; i
++) {
379 if (bit_set(ics
->scale_factor_grouping
, 6-i
) == 0)
381 ics
->num_window_groups
+= 1;
382 ics
->window_group_length
[ics
->num_window_groups
-1] = 1;
384 ics
->window_group_length
[ics
->num_window_groups
-1] += 1;
388 /* preparation of sect_sfb_offset for short blocks */
389 for (g
= 0; g
< ics
->num_window_groups
; g
++)
392 uint8_t sect_sfb
= 0;
395 for (i
= 0; i
< ics
->num_swb
; i
++)
397 if (i
+1 == ics
->num_swb
)
399 width
= (hDecoder
->frameLength
/8) - swb_offset_128_window
[sf_index
][i
];
401 width
= swb_offset_128_window
[sf_index
][i
+1] -
402 swb_offset_128_window
[sf_index
][i
];
404 width
*= ics
->window_group_length
[g
];
405 ics
->sect_sfb_offset
[g
][sect_sfb
++] = offset
;
408 ics
->sect_sfb_offset
[g
][sect_sfb
] = offset
;
417 /* output = sign(input)*abs(input)^(4/3) */
419 static INLINE real_t
iquant(int16_t q
, const real_t
*tab
, uint8_t *error
)
422 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
423 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
424 * defined a 1026 value table and interpolation will be used
426 static const real_t errcorr
[] = {
427 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
428 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
441 if (q
< IQ_TABLE_SIZE
)
443 //#define IQUANT_PRINT
445 //printf("0x%.8X\n", sgn * tab[q]);
446 printf("%d\n", sgn
* tab
[q
]);
457 /* linear interpolation */
459 x2
= tab
[(q
>>3) + 1];
460 return sgn
* 16 * (MUL_R(errcorr
[q
&7],(x2
-x1
)) + x1
);
461 #else /* #ifndef BIG_IQ_TABLE */
464 /* tab contains a value for all possible q [0,8192] */
465 if (LIKELY(-q
< IQ_TABLE_SIZE
))
471 /* tab contains a value for all possible q [0,8192] */
472 if (LIKELY(q
< IQ_TABLE_SIZE
))
482 static const real_t pow2sf_tab
[] = {
483 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
484 2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007,
485 1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006,
486 1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005,
487 0.0001220703125, 0.000244140625, 0.00048828125,
488 0.0009765625, 0.001953125, 0.00390625,
489 0.0078125, 0.015625, 0.03125,
492 4.0, 8.0, 16.0, 32.0,
494 512.0, 1024.0, 2048.0,
495 4096.0, 8192.0, 16384.0,
496 32768.0, 65536.0, 131072.0,
497 262144.0, 524288.0, 1048576.0,
498 2097152.0, 4194304.0, 8388608.0,
499 16777216.0, 33554432.0, 67108864.0,
500 134217728.0, 268435456.0, 536870912.0,
501 1073741824.0, 2147483648.0, 4294967296.0,
502 8589934592.0, 17179869184.0, 34359738368.0,
503 68719476736.0, 137438953472.0, 274877906944.0
507 /* quant_to_spec: perform dequantisation and scaling
508 * and in case of short block it also does the deinterleaving
511 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
512 window_group_length[0] = 1) the spectral data is in ascending spectral
514 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
515 grouping in the following manner:
516 - Groups are ordered sequentially
517 - Within a group, a scalefactor band consists of the spectral data of all
518 grouped SHORT_WINDOWs for the associated scalefactor window band. To
519 clarify via example, the length of a group is in the range of one to eight
521 - If there are eight groups each with length one (num_window_groups = 8,
522 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
523 each in ascending spectral order.
524 - If there is only one group with length eight (num_window_groups = 1,
525 window_group_length[0] = 8), the result is that spectral data of all eight
526 SHORT_WINDOWs is interleaved by scalefactor window bands.
527 - Within a scalefactor window band, the coefficients are in ascending
530 static const real_t pow2_table
[] ICONST_ATTR
=
533 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
534 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.50 */
535 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
537 static uint8_t quant_to_spec(NeAACDecHandle hDecoder
,
538 ic_stream
*ics
, int16_t *quant_data
,
539 real_t
*spec_data
, uint16_t frame_len
)
541 const real_t
*tab
= iq_table
;
545 uint16_t width
, bin
, k
, gindex
, wa
, wb
;
546 uint8_t error
= 0; /* Init error flag */
552 for (g
= 0; g
< ics
->num_window_groups
; g
++)
555 uint16_t gincrease
= 0;
556 uint16_t win_inc
= ics
->swb_offset
[ics
->num_swb
];
558 for (sfb
= 0; sfb
< ics
->num_swb
; sfb
++)
562 width
= ics
->swb_offset
[sfb
+1] - ics
->swb_offset
[sfb
];
564 /* this could be scalefactor for IS or PNS, those can be negative or bigger then 255 */
565 /* just ignore them */
566 if (ics
->scale_factors
[g
][sfb
] < 0 || ics
->scale_factors
[g
][sfb
] > 255)
571 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
572 exp
= (ics
->scale_factors
[g
][sfb
] /* - 100 */) >> 2;
573 /* frac must always be > 0 */
574 frac
= (ics
->scale_factors
[g
][sfb
] /* - 100 */) & 3;
579 /* IMDCT pre-scaling */
580 if (hDecoder
->object_type
== LD
)
584 if (ics
->window_sequence
== EIGHT_SHORT_SEQUENCE
)
594 scf
= pow2sf_tab
[exp
/*+25*/] * pow2_table
[frac
];
596 scf
= pow2_table
[frac
];
599 for (win
= 0; win
< ics
->window_group_length
[g
]; win
++)
601 for (bin
= 0; bin
< width
; bin
+= 4)
606 spec_data
[wb
+0] = iquant(quant_data
[k
+0], tab
, &error
) * scf
;
607 spec_data
[wb
+1] = iquant(quant_data
[k
+1], tab
, &error
) * scf
;
608 spec_data
[wb
+2] = iquant(quant_data
[k
+2], tab
, &error
) * scf
;
609 spec_data
[wb
+3] = iquant(quant_data
[k
+3], tab
, &error
) * scf
;
616 spec_data
[wb
+0] = MUL_C((iquant(quant_data
[k
+0], tab
, &error
)<< exp
), scf
);
617 spec_data
[wb
+1] = MUL_C((iquant(quant_data
[k
+1], tab
, &error
)<< exp
), scf
);
618 spec_data
[wb
+2] = MUL_C((iquant(quant_data
[k
+2], tab
, &error
)<< exp
), scf
);
619 spec_data
[wb
+3] = MUL_C((iquant(quant_data
[k
+3], tab
, &error
)<< exp
), scf
);
621 spec_data
[wb
+0] = MUL_C((iquant(quant_data
[k
+0], tab
, &error
)>>-exp
), scf
);
622 spec_data
[wb
+1] = MUL_C((iquant(quant_data
[k
+1], tab
, &error
)>>-exp
), scf
);
623 spec_data
[wb
+2] = MUL_C((iquant(quant_data
[k
+2], tab
, &error
)>>-exp
), scf
);
624 spec_data
[wb
+3] = MUL_C((iquant(quant_data
[k
+3], tab
, &error
)>>-exp
), scf
);
628 printf("%d\n", spec_data
[gindex
+(win
*win_inc
)+j
+bin
+0]);
629 printf("%d\n", spec_data
[gindex
+(win
*win_inc
)+j
+bin
+1]);
630 printf("%d\n", spec_data
[gindex
+(win
*win_inc
)+j
+bin
+2]);
631 printf("%d\n", spec_data
[gindex
+(win
*win_inc
)+j
+bin
+3]);
632 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
633 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
634 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
635 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
652 static uint8_t allocate_single_channel(NeAACDecHandle hDecoder
, uint8_t channel
,
653 uint8_t output_channels
)
655 (void)output_channels
;
661 static uint8_t allocate_channel_pair(NeAACDecHandle hDecoder
,
662 uint8_t channel
, uint8_t paired_channel
)
664 (void)paired_channel
;
670 uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder
, ic_stream
*ics
,
671 element
*sce
, int16_t *spec_data
)
673 uint8_t retval
, output_channels
;
676 int64_t count
= faad_get_ts();
680 /* always allocate 2 channels, PS can always "suddenly" turn up */
681 #if (defined(PS_DEC) || defined(DRM_PS))
687 if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] == 0)
689 /* element_output_channels not set yet */
690 hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] = output_channels
;
691 } else if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] != output_channels
) {
692 /* element inconsistency */
696 if (hDecoder
->element_alloced
[hDecoder
->fr_ch_ele
] == 0)
698 retval
= allocate_single_channel(hDecoder
, sce
->channel
, output_channels
);
702 hDecoder
->element_alloced
[hDecoder
->fr_ch_ele
] = 1;
706 /* dequantisation and scaling */
707 retval
= quant_to_spec(hDecoder
, ics
, spec_data
, spec_coef1
, hDecoder
->frameLength
);
712 count
= faad_get_ts() - count
;
713 hDecoder
->requant_cycles
+= count
;
718 pns_decode(ics
, NULL
, spec_coef1
, NULL
, hDecoder
->frameLength
, 0, hDecoder
->object_type
);
721 /* MAIN object type prediction */
722 if (hDecoder
->object_type
== MAIN
)
724 /* intra channel prediction */
725 ic_prediction(ics
, spec_coef1
, hDecoder
->pred_stat
[sce
->channel
], hDecoder
->frameLength
,
728 /* In addition, for scalefactor bands coded by perceptual
729 noise substitution the predictors belonging to the
730 corresponding spectral coefficients are reset.
732 pns_reset_pred_state(ics
, hDecoder
->pred_stat
[sce
->channel
]);
737 if (is_ltp_ot(hDecoder
->object_type
))
740 if (hDecoder
->object_type
== LD
)
742 if (ics
->ltp
.data_present
)
744 if (ics
->ltp
.lag_update
)
745 hDecoder
->ltp_lag
[sce
->channel
] = ics
->ltp
.lag
;
747 ics
->ltp
.lag
= hDecoder
->ltp_lag
[sce
->channel
];
751 /* long term prediction */
752 lt_prediction(ics
, &(ics
->ltp
), spec_coef1
, hDecoder
->lt_pred_stat
[sce
->channel
], hDecoder
->fb
,
753 ics
->window_shape
, hDecoder
->window_shape_prev
[sce
->channel
],
754 hDecoder
->sf_index
, hDecoder
->object_type
, hDecoder
->frameLength
);
759 tns_decode_frame(ics
, &(ics
->tns
), hDecoder
->sf_index
, hDecoder
->object_type
,
760 spec_coef1
, hDecoder
->frameLength
);
763 if (hDecoder
->drc
->present
)
765 if (!hDecoder
->drc
->exclude_mask
[sce
->channel
] || !hDecoder
->drc
->excluded_chns_present
)
766 drc_decode(hDecoder
->drc
, spec_coef1
);
771 if (hDecoder
->object_type
!= SSR
)
774 ifilter_bank(ics
->window_sequence
,ics
->window_shape
,
775 hDecoder
->window_shape_prev
[sce
->channel
],spec_coef1
,
776 hDecoder
->time_out
[sce
->channel
], hDecoder
->fb_intermed
[sce
->channel
],
777 hDecoder
->object_type
, hDecoder
->frameLength
);
780 ssr_decode(&(ics
->ssr
), hDecoder
->fb
, ics
->window_sequence
, ics
->window_shape
,
781 hDecoder
->window_shape_prev
[sce
->channel
], spec_coef1
, hDecoder
->time_out
[sce
->channel
],
782 hDecoder
->ssr_overlap
[sce
->channel
], hDecoder
->ipqf_buffer
[sce
->channel
], hDecoder
->prev_fmd
[sce
->channel
],
783 hDecoder
->frameLength
);
787 /* save window shape for next frame */
788 hDecoder
->window_shape_prev
[sce
->channel
] = ics
->window_shape
;
791 if (is_ltp_ot(hDecoder
->object_type
))
793 lt_update_state(hDecoder
->lt_pred_stat
[sce
->channel
], hDecoder
->time_out
[sce
->channel
],
794 hDecoder
->fb_intermed
[sce
->channel
], hDecoder
->frameLength
, hDecoder
->object_type
);
799 if (((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
800 && hDecoder
->sbr_alloced
[hDecoder
->fr_ch_ele
])
802 uint8_t ele
= hDecoder
->fr_ch_ele
;
803 uint8_t ch
= sce
->channel
;
805 /* following case can happen when forceUpSampling == 1 */
806 if (hDecoder
->sbr
[ele
] == NULL
)
808 hDecoder
->sbr
[ele
] = sbrDecodeInit(hDecoder
->frameLength
,
809 hDecoder
->element_id
[ele
], ele
,
810 2*get_sample_rate(hDecoder
->sf_index
),
811 hDecoder
->downSampledSBR
818 if (sce
->ics1
.window_sequence
== EIGHT_SHORT_SEQUENCE
)
819 hDecoder
->sbr
[ele
]->maxAACLine
= 8*sce
->ics1
.swb_offset
[max(sce
->ics1
.max_sfb
-1, 0)];
821 hDecoder
->sbr
[ele
]->maxAACLine
= sce
->ics1
.swb_offset
[max(sce
->ics1
.max_sfb
-1, 0)];
823 /* check if any of the PS tools is used */
824 #if (defined(PS_DEC) || defined(DRM_PS))
825 if (hDecoder
->ps_used
[ele
] == 0)
828 retval
= sbrDecodeSingleFrame(hDecoder
->sbr
[ele
], hDecoder
->time_out
[ch
],
829 hDecoder
->postSeekResetFlag
, hDecoder
->downSampledSBR
);
830 #if (defined(PS_DEC) || defined(DRM_PS))
832 retval
= sbrDecodeSingleFramePS(hDecoder
->sbr
[ele
], hDecoder
->time_out
[ch
],
833 hDecoder
->time_out
[ch
+1], hDecoder
->postSeekResetFlag
,
834 hDecoder
->downSampledSBR
);
839 } else if (((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
840 && !hDecoder
->sbr_alloced
[hDecoder
->fr_ch_ele
])
846 /* copy L to R when no PS is used */
847 #if (defined(PS_DEC) || defined(DRM_PS))
848 if ((hDecoder
->ps_used
[hDecoder
->fr_ch_ele
] == 0))
850 uint8_t ele
= hDecoder
->fr_ch_ele
;
851 uint8_t ch
= sce
->channel
;
852 uint16_t frame_size
= (hDecoder
->sbr_alloced
[ele
]) ? 2 : 1;
853 frame_size
*= hDecoder
->frameLength
*sizeof(real_t
);
855 memcpy(hDecoder
->time_out
[ch
+1], hDecoder
->time_out
[ch
], frame_size
);
862 uint8_t reconstruct_channel_pair(NeAACDecHandle hDecoder
, ic_stream
*ics1
, ic_stream
*ics2
,
863 element
*cpe
, int16_t *spec_data1
, int16_t *spec_data2
)
868 int64_t count
= faad_get_ts();
870 if (hDecoder
->element_alloced
[hDecoder
->fr_ch_ele
] == 0)
872 retval
= allocate_channel_pair(hDecoder
, cpe
->channel
, (uint8_t)cpe
->paired_channel
);
876 hDecoder
->element_alloced
[hDecoder
->fr_ch_ele
] = 1;
879 /* dequantisation and scaling */
880 retval
= quant_to_spec(hDecoder
, ics1
, spec_data1
, spec_coef1
, hDecoder
->frameLength
);
883 retval
= quant_to_spec(hDecoder
, ics2
, spec_data2
, spec_coef2
, hDecoder
->frameLength
);
888 count
= faad_get_ts() - count
;
889 hDecoder
->requant_cycles
+= count
;
894 if (ics1
->ms_mask_present
)
896 pns_decode(ics1
, ics2
, spec_coef1
, spec_coef2
, hDecoder
->frameLength
, 1, hDecoder
->object_type
);
898 pns_decode(ics1
, NULL
, spec_coef1
, NULL
, hDecoder
->frameLength
, 0, hDecoder
->object_type
);
899 pns_decode(ics2
, NULL
, spec_coef2
, NULL
, hDecoder
->frameLength
, 0, hDecoder
->object_type
);
902 /* mid/side decoding */
903 ms_decode(ics1
, ics2
, spec_coef1
, spec_coef2
, hDecoder
->frameLength
);
908 for (i
= 0; i
< 1024; i
++)
910 //printf("%d\n", spec_coef1[i]);
911 printf("0x%.8X\n", spec_coef1
[i
]);
913 for (i
= 0; i
< 1024; i
++)
915 //printf("%d\n", spec_coef2[i]);
916 printf("0x%.8X\n", spec_coef2
[i
]);
921 /* intensity stereo decoding */
922 is_decode(ics1
, ics2
, spec_coef1
, spec_coef2
, hDecoder
->frameLength
);
927 for (i
= 0; i
< 1024; i
++)
929 printf("%d\n", spec_coef1
[i
]);
930 //printf("0x%.8X\n", spec_coef1[i]);
932 for (i
= 0; i
< 1024; i
++)
934 printf("%d\n", spec_coef2
[i
]);
935 //printf("0x%.8X\n", spec_coef2[i]);
941 /* MAIN object type prediction */
942 if (hDecoder
->object_type
== MAIN
)
944 /* intra channel prediction */
945 ic_prediction(ics1
, spec_coef1
, hDecoder
->pred_stat
[cpe
->channel
], hDecoder
->frameLength
,
947 ic_prediction(ics2
, spec_coef2
, hDecoder
->pred_stat
[cpe
->paired_channel
], hDecoder
->frameLength
,
950 /* In addition, for scalefactor bands coded by perceptual
951 noise substitution the predictors belonging to the
952 corresponding spectral coefficients are reset.
954 pns_reset_pred_state(ics1
, hDecoder
->pred_stat
[cpe
->channel
]);
955 pns_reset_pred_state(ics2
, hDecoder
->pred_stat
[cpe
->paired_channel
]);
960 if (is_ltp_ot(hDecoder
->object_type
))
962 ltp_info
*ltp1
= &(ics1
->ltp
);
963 ltp_info
*ltp2
= (cpe
->common_window
) ? &(ics2
->ltp2
) : &(ics2
->ltp
);
965 if (hDecoder
->object_type
== LD
)
967 if (ltp1
->data_present
)
969 if (ltp1
->lag_update
)
970 hDecoder
->ltp_lag
[cpe
->channel
] = ltp1
->lag
;
972 ltp1
->lag
= hDecoder
->ltp_lag
[cpe
->channel
];
973 if (ltp2
->data_present
)
975 if (ltp2
->lag_update
)
976 hDecoder
->ltp_lag
[cpe
->paired_channel
] = ltp2
->lag
;
978 ltp2
->lag
= hDecoder
->ltp_lag
[cpe
->paired_channel
];
982 /* long term prediction */
983 lt_prediction(ics1
, ltp1
, spec_coef1
, hDecoder
->lt_pred_stat
[cpe
->channel
], hDecoder
->fb
,
984 ics1
->window_shape
, hDecoder
->window_shape_prev
[cpe
->channel
],
985 hDecoder
->sf_index
, hDecoder
->object_type
, hDecoder
->frameLength
);
986 lt_prediction(ics2
, ltp2
, spec_coef2
, hDecoder
->lt_pred_stat
[cpe
->paired_channel
], hDecoder
->fb
,
987 ics2
->window_shape
, hDecoder
->window_shape_prev
[cpe
->paired_channel
],
988 hDecoder
->sf_index
, hDecoder
->object_type
, hDecoder
->frameLength
);
993 tns_decode_frame(ics1
, &(ics1
->tns
), hDecoder
->sf_index
, hDecoder
->object_type
,
994 spec_coef1
, hDecoder
->frameLength
);
995 tns_decode_frame(ics2
, &(ics2
->tns
), hDecoder
->sf_index
, hDecoder
->object_type
,
996 spec_coef2
, hDecoder
->frameLength
);
999 if (hDecoder
->drc
->present
)
1001 if (!hDecoder
->drc
->exclude_mask
[cpe
->channel
] || !hDecoder
->drc
->excluded_chns_present
)
1002 drc_decode(hDecoder
->drc
, spec_coef1
);
1003 if (!hDecoder
->drc
->exclude_mask
[cpe
->paired_channel
] || !hDecoder
->drc
->excluded_chns_present
)
1004 drc_decode(hDecoder
->drc
, spec_coef2
);
1009 if (hDecoder
->object_type
!= SSR
)
1012 ifilter_bank(ics1
->window_sequence
,ics1
->window_shape
,
1013 hDecoder
->window_shape_prev
[cpe
->channel
],spec_coef1
,
1014 hDecoder
->time_out
[cpe
->channel
], hDecoder
->fb_intermed
[cpe
->channel
],
1015 hDecoder
->object_type
, hDecoder
->frameLength
);
1016 ifilter_bank(ics2
->window_sequence
,ics2
->window_shape
,
1017 hDecoder
->window_shape_prev
[cpe
->paired_channel
], spec_coef2
,
1018 hDecoder
->time_out
[cpe
->paired_channel
], hDecoder
->fb_intermed
[cpe
->paired_channel
],
1019 hDecoder
->object_type
, hDecoder
->frameLength
);
1022 ssr_decode(&(ics1
->ssr
), hDecoder
->fb
, ics1
->window_sequence
, ics1
->window_shape
,
1023 hDecoder
->window_shape_prev
[cpe
->channel
], spec_coef1
, hDecoder
->time_out
[cpe
->channel
],
1024 hDecoder
->ssr_overlap
[cpe
->channel
], hDecoder
->ipqf_buffer
[cpe
->channel
],
1025 hDecoder
->prev_fmd
[cpe
->channel
], hDecoder
->frameLength
);
1026 ssr_decode(&(ics2
->ssr
), hDecoder
->fb
, ics2
->window_sequence
, ics2
->window_shape
,
1027 hDecoder
->window_shape_prev
[cpe
->paired_channel
], spec_coef2
, hDecoder
->time_out
[cpe
->paired_channel
],
1028 hDecoder
->ssr_overlap
[cpe
->paired_channel
], hDecoder
->ipqf_buffer
[cpe
->paired_channel
],
1029 hDecoder
->prev_fmd
[cpe
->paired_channel
], hDecoder
->frameLength
);
1033 /* save window shape for next frame */
1034 hDecoder
->window_shape_prev
[cpe
->channel
] = ics1
->window_shape
;
1035 hDecoder
->window_shape_prev
[cpe
->paired_channel
] = ics2
->window_shape
;
1038 if (is_ltp_ot(hDecoder
->object_type
))
1040 lt_update_state(hDecoder
->lt_pred_stat
[cpe
->channel
], hDecoder
->time_out
[cpe
->channel
],
1041 hDecoder
->fb_intermed
[cpe
->channel
], hDecoder
->frameLength
, hDecoder
->object_type
);
1042 lt_update_state(hDecoder
->lt_pred_stat
[cpe
->paired_channel
], hDecoder
->time_out
[cpe
->paired_channel
],
1043 hDecoder
->fb_intermed
[cpe
->paired_channel
], hDecoder
->frameLength
, hDecoder
->object_type
);
1048 if (((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
1049 && hDecoder
->sbr_alloced
[hDecoder
->fr_ch_ele
])
1051 uint8_t ele
= hDecoder
->fr_ch_ele
;
1052 uint8_t ch0
= cpe
->channel
;
1053 uint8_t ch1
= cpe
->paired_channel
;
1055 /* following case can happen when forceUpSampling == 1 */
1056 if (hDecoder
->sbr
[ele
] == NULL
)
1058 hDecoder
->sbr
[ele
] = sbrDecodeInit(hDecoder
->frameLength
,
1059 hDecoder
->element_id
[ele
], ele
,
1060 2*get_sample_rate(hDecoder
->sf_index
),
1061 hDecoder
->downSampledSBR
1068 if (cpe
->ics1
.window_sequence
== EIGHT_SHORT_SEQUENCE
)
1069 hDecoder
->sbr
[ele
]->maxAACLine
= 8*cpe
->ics1
.swb_offset
[max(cpe
->ics1
.max_sfb
-1, 0)];
1071 hDecoder
->sbr
[ele
]->maxAACLine
= cpe
->ics1
.swb_offset
[max(cpe
->ics1
.max_sfb
-1, 0)];
1073 retval
= sbrDecodeCoupleFrame(hDecoder
->sbr
[ele
],
1074 hDecoder
->time_out
[ch0
], hDecoder
->time_out
[ch1
],
1075 hDecoder
->postSeekResetFlag
, hDecoder
->downSampledSBR
);
1078 } else if (((hDecoder
->sbr_present_flag
== 1) || (hDecoder
->forceUpSampling
== 1))
1079 && !hDecoder
->sbr_alloced
[hDecoder
->fr_ch_ele
])