2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero 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 ** 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"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: specrec.c,v 1.62 2009/01/26 23:51:15 menno Exp $
32 Spectral reconstruction:
34 - inverse quantization
35 - applying scalefactors
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);
67 ALIGN static const uint8_t num_swb_512_window[] =
69 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
71 ALIGN static const uint8_t num_swb_480_window[] =
73 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
77 ALIGN static const uint8_t num_swb_960_window[] =
79 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
82 ALIGN static const uint8_t num_swb_1024_window[] =
84 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
87 ALIGN static const uint8_t num_swb_128_window[] =
89 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
92 ALIGN static const uint16_t swb_offset_1024_96[] =
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
99 ALIGN static const uint16_t swb_offset_128_96[] =
101 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
104 ALIGN static const uint16_t swb_offset_1024_64[] =
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
112 ALIGN static const uint16_t swb_offset_128_64[] =
114 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
117 ALIGN static const uint16_t swb_offset_1024_48[] =
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
126 ALIGN static const uint16_t swb_offset_512_48[] =
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,
133 ALIGN static const uint16_t swb_offset_480_48[] =
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,
141 ALIGN static const uint16_t swb_offset_128_48[] =
143 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
146 ALIGN static const uint16_t swb_offset_1024_32[] =
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
155 ALIGN static const uint16_t swb_offset_512_32[] =
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
162 ALIGN static const uint16_t swb_offset_480_32[] =
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,
170 ALIGN static const uint16_t swb_offset_1024_24[] =
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
179 ALIGN static const uint16_t swb_offset_512_24[] =
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,
186 ALIGN static const uint16_t swb_offset_480_24[] =
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
193 ALIGN static const uint16_t swb_offset_128_24[] =
195 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
198 ALIGN static const uint16_t swb_offset_1024_16[] =
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
205 ALIGN static const uint16_t swb_offset_128_16[] =
207 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
210 ALIGN static const uint16_t swb_offset_1024_8[] =
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
217 ALIGN static const uint16_t swb_offset_128_8[] =
219 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
222 ALIGN static const uint16_t *swb_offset_1024_window[] =
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 */
239 ALIGN static const uint16_t *swb_offset_512_window[] =
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 */
255 ALIGN static const uint16_t *swb_offset_480_window[] =
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 */
272 ALIGN static const uint16_t *swb_offset_128_window[] =
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 */
288 #define bit_set(A, B) ((A) & (1<<(B)))
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().
303 uint8_t window_grouping_info(NeAACDecStruct *hDecoder, ic_stream *ics)
307 uint8_t sf_index = hDecoder->sf_index;
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;
317 if (hDecoder->object_type == LD)
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];
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];
333 if (ics->max_sfb > ics->num_swb)
338 /* preparation of sect_sfb_offset for long blocks */
339 /* also copy the last value! */
341 if (hDecoder->object_type == LD)
343 if (hDecoder->frameLength == 512)
345 for (i = 0; i < ics->num_swb; i++)
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];
350 } else /* if (hDecoder->frameLength == 480) */ {
351 for (i = 0; i < ics->num_swb; i++)
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];
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;
362 for (i = 0; i < ics->num_swb; i++)
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];
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;
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];
380 if (ics->max_sfb > ics->num_swb)
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;
390 for (i = 0; i < ics->num_windows-1; i++) {
391 if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
393 ics->num_window_groups += 1;
394 ics->window_group_length[ics->num_window_groups-1] = 1;
396 ics->window_group_length[ics->num_window_groups-1] += 1;
400 /* preparation of sect_sfb_offset for short blocks */
401 for (g = 0; g < ics->num_window_groups; g++)
404 uint8_t sect_sfb = 0;
407 for (i = 0; i < ics->num_swb; i++)
409 if (i+1 == ics->num_swb)
411 width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
413 width = swb_offset_128_window[sf_index][i+1] -
414 swb_offset_128_window[sf_index][i];
416 width *= ics->window_group_length[g];
417 ics->sect_sfb_offset[g][sect_sfb++] = offset;
420 ics->sect_sfb_offset[g][sect_sfb] = offset;
429 * output = sign(input)*abs(input)^(4/3) */
430 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
433 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
434 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
435 * defined a 1026 value table and interpolation will be used
438 static const real_t errcorr[] = {
439 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
440 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
453 if (q < IQ_TABLE_SIZE)
455 //#define IQUANT_PRINT
457 //printf("0x%.8X\n", sgn * tab[q]);
458 printf("%d\n", sgn * tab[q]);
470 /* linear interpolation */
472 x2 = tab[(q>>3) + 1];
473 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
482 /* tab contains a value for all possible q [0,8192] */
483 if (-q < IQ_TABLE_SIZE)
489 /* tab contains a value for all possible q [0,8192] */
490 if (q < IQ_TABLE_SIZE)
500 ALIGN static const real_t pow2sf_tab[] = {
501 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
502 2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007,
503 1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006,
504 1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005,
505 0.0001220703125, 0.000244140625, 0.00048828125,
506 0.0009765625, 0.001953125, 0.00390625,
507 0.0078125, 0.015625, 0.03125,
510 4.0, 8.0, 16.0, 32.0,
512 512.0, 1024.0, 2048.0,
513 4096.0, 8192.0, 16384.0,
514 32768.0, 65536.0, 131072.0,
515 262144.0, 524288.0, 1048576.0,
516 2097152.0, 4194304.0, 8388608.0,
517 16777216.0, 33554432.0, 67108864.0,
518 134217728.0, 268435456.0, 536870912.0,
519 1073741824.0, 2147483648.0, 4294967296.0,
520 8589934592.0, 17179869184.0, 34359738368.0,
521 68719476736.0, 137438953472.0, 274877906944.0
525 /* quant_to_spec: perform dequantisation and scaling
526 * and in case of short block it also does the deinterleaving
529 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
530 window_group_length[0] = 1) the spectral data is in ascending spectral
532 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
533 grouping in the following manner:
534 - Groups are ordered sequentially
535 - Within a group, a scalefactor band consists of the spectral data of all
536 grouped SHORT_WINDOWs for the associated scalefactor window band. To
537 clarify via example, the length of a group is in the range of one to eight
539 - If there are eight groups each with length one (num_window_groups = 8,
540 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
541 each in ascending spectral order.
542 - If there is only one group with length eight (num_window_groups = 1,
543 window_group_length[0] = 8), the result is that spectral data of all eight
544 SHORT_WINDOWs is interleaved by scalefactor window bands.
545 - Within a scalefactor window band, the coefficients are in ascending
548 static uint8_t quant_to_spec(NeAACDecStruct *hDecoder,
549 ic_stream *ics, int16_t *quant_data,
550 real_t *spec_data, uint16_t frame_len)
552 ALIGN static const real_t pow2_table[] =
555 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
556 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
557 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
559 const real_t *tab = iq_table;
562 uint16_t width, bin, k, gindex, wa, wb;
563 uint8_t error = 0; /* Init error flag */
571 for (g = 0; g < ics->num_window_groups; g++)
574 uint16_t gincrease = 0;
575 uint16_t win_inc = ics->swb_offset[ics->num_swb];
577 for (sfb = 0; sfb < ics->num_swb; sfb++)
581 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
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)
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;
598 /* IMDCT pre-scaling */
599 if (hDecoder->object_type == LD)
603 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
613 scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
616 for (win = 0; win < ics->window_group_length[g]; win++)
618 for (bin = 0; bin < width; bin += 4)
623 spec_data[wb+0] = iquant(quant_data[k+0], tab, &error) * scf;
624 spec_data[wb+1] = iquant(quant_data[k+1], tab, &error) * scf;
625 spec_data[wb+2] = iquant(quant_data[k+2], tab, &error) * scf;
626 spec_data[wb+3] = iquant(quant_data[k+3], tab, &error) * scf;
629 real_t iq0 = iquant(quant_data[k+0], tab, &error);
630 real_t iq1 = iquant(quant_data[k+1], tab, &error);
631 real_t iq2 = iquant(quant_data[k+2], tab, &error);
632 real_t iq3 = iquant(quant_data[k+3], tab, &error);
638 spec_data[wb+0] = iq0 >>= -exp;
639 spec_data[wb+1] = iq1 >>= -exp;
640 spec_data[wb+2] = iq2 >>= -exp;
641 spec_data[wb+3] = iq3 >>= -exp;
643 spec_data[wb+0] = iq0 <<= exp;
644 spec_data[wb+1] = iq1 <<= exp;
645 spec_data[wb+2] = iq2 <<= exp;
646 spec_data[wb+3] = iq3 <<= exp;
650 spec_data[wb+0] = MUL_C(spec_data[wb+0],pow2_table[frac]);
651 spec_data[wb+1] = MUL_C(spec_data[wb+1],pow2_table[frac]);
652 spec_data[wb+2] = MUL_C(spec_data[wb+2],pow2_table[frac]);
653 spec_data[wb+3] = MUL_C(spec_data[wb+3],pow2_table[frac]);
658 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
659 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
660 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
661 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
662 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
663 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
664 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
665 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
682 static uint8_t allocate_single_channel(NeAACDecStruct *hDecoder, uint8_t channel,
683 uint8_t output_channels)
688 /* MAIN object type prediction */
689 if (hDecoder->object_type == MAIN)
691 /* allocate the state only when needed */
692 if (hDecoder->pred_stat[channel] != NULL)
694 faad_free(hDecoder->pred_stat[channel]);
695 hDecoder->pred_stat[channel] = NULL;
698 hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
699 reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
704 if (is_ltp_ot(hDecoder->object_type))
706 /* allocate the state only when needed */
707 if (hDecoder->lt_pred_stat[channel] != NULL)
709 faad_free(hDecoder->lt_pred_stat[channel]);
710 hDecoder->lt_pred_stat[channel] = NULL;
713 hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
714 memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
718 if (hDecoder->time_out[channel] != NULL)
720 faad_free(hDecoder->time_out[channel]);
721 hDecoder->time_out[channel] = NULL;
727 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
728 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
730 /* SBR requires 2 times as much output data */
732 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
735 hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
736 memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
739 #if (defined(PS_DEC) || defined(DRM_PS))
740 if (output_channels == 2)
742 if (hDecoder->time_out[channel+1] != NULL)
744 faad_free(hDecoder->time_out[channel+1]);
745 hDecoder->time_out[channel+1] = NULL;
748 hDecoder->time_out[channel+1] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
749 memset(hDecoder->time_out[channel+1], 0, mul*hDecoder->frameLength*sizeof(real_t));
753 if (hDecoder->fb_intermed[channel] != NULL)
755 faad_free(hDecoder->fb_intermed[channel]);
756 hDecoder->fb_intermed[channel] = NULL;
759 hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
760 memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
763 if (hDecoder->object_type == SSR)
765 if (hDecoder->ssr_overlap[channel] == NULL)
767 hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
768 memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
770 if (hDecoder->prev_fmd[channel] == NULL)
773 hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
774 for (k = 0; k < 2*hDecoder->frameLength; k++)
775 hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
783 static uint8_t allocate_channel_pair(NeAACDecStruct *hDecoder,
784 uint8_t channel, uint8_t paired_channel)
789 /* MAIN object type prediction */
790 if (hDecoder->object_type == MAIN)
792 /* allocate the state only when needed */
793 if (hDecoder->pred_stat[channel] == NULL)
795 hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
796 reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
798 if (hDecoder->pred_stat[paired_channel] == NULL)
800 hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
801 reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength);
807 if (is_ltp_ot(hDecoder->object_type))
809 /* allocate the state only when needed */
810 if (hDecoder->lt_pred_stat[channel] == NULL)
812 hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
813 memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
815 if (hDecoder->lt_pred_stat[paired_channel] == NULL)
817 hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
818 memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
823 if (hDecoder->time_out[channel] == NULL)
827 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
828 if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
830 /* SBR requires 2 times as much output data */
832 hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
835 hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
836 memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
838 if (hDecoder->time_out[paired_channel] == NULL)
840 hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
841 memset(hDecoder->time_out[paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
844 if (hDecoder->fb_intermed[channel] == NULL)
846 hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
847 memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
849 if (hDecoder->fb_intermed[paired_channel] == NULL)
851 hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
852 memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
856 if (hDecoder->object_type == SSR)
858 if (hDecoder->ssr_overlap[cpe->channel] == NULL)
860 hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
861 memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
863 if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
865 hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
866 memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
868 if (hDecoder->prev_fmd[cpe->channel] == NULL)
871 hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
872 for (k = 0; k < 2*hDecoder->frameLength; k++)
873 hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
875 if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
878 hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
879 for (k = 0; k < 2*hDecoder->frameLength; k++)
880 hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
888 uint8_t reconstruct_single_channel(NeAACDecStruct *hDecoder, ic_stream *ics,
889 element *sce, int16_t *spec_data)
893 ALIGN real_t spec_coef[1024];
896 int64_t count = faad_get_ts();
900 /* always allocate 2 channels, PS can always "suddenly" turn up */
901 #if ( (defined(DRM) && defined(DRM_PS)) )
903 #elif defined(PS_DEC)
904 if (hDecoder->ps_used[hDecoder->fr_ch_ele])
912 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
914 /* element_output_channels not set yet */
915 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
916 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
917 /* element inconsistency */
919 /* this only happens if PS is actually found but not in the first frame
920 * this means that there is only 1 bitstream element!
923 /* reset the allocation */
924 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 0;
926 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
931 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
933 retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
937 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
941 /* dequantisation and scaling */
942 retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
947 count = faad_get_ts() - count;
948 hDecoder->requant_cycles += count;
953 pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
954 &(hDecoder->__r1), &(hDecoder->__r2));
957 /* MAIN object type prediction */
958 if (hDecoder->object_type == MAIN)
960 if (!hDecoder->pred_stat[sce->channel])
963 /* intra channel prediction */
964 ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
967 /* In addition, for scalefactor bands coded by perceptual
968 noise substitution the predictors belonging to the
969 corresponding spectral coefficients are reset.
971 pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
976 if (is_ltp_ot(hDecoder->object_type))
979 if (hDecoder->object_type == LD)
981 if (ics->ltp.data_present)
983 if (ics->ltp.lag_update)
984 hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
986 ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
990 /* long term prediction */
991 lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
992 ics->window_shape, hDecoder->window_shape_prev[sce->channel],
993 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
998 tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
999 spec_coef, hDecoder->frameLength);
1002 if (hDecoder->drc->present)
1004 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
1005 drc_decode(hDecoder->drc, spec_coef);
1010 if (hDecoder->object_type != SSR)
1013 ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
1014 hDecoder->window_shape_prev[sce->channel], spec_coef,
1015 hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel],
1016 hDecoder->object_type, hDecoder->frameLength);
1019 ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
1020 hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
1021 hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
1022 hDecoder->frameLength);
1026 /* save window shape for next frame */
1027 hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
1030 if (is_ltp_ot(hDecoder->object_type))
1032 lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel],
1033 hDecoder->fb_intermed[sce->channel], hDecoder->frameLength, hDecoder->object_type);
1038 if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1039 && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1041 int ele = hDecoder->fr_ch_ele;
1042 int ch = sce->channel;
1044 /* following case can happen when forceUpSampling == 1 */
1045 if (hDecoder->sbr[ele] == NULL)
1047 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1048 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1049 hDecoder->downSampledSBR
1056 if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1057 hDecoder->sbr[ele]->maxAACLine = 8*min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1059 hDecoder->sbr[ele]->maxAACLine = min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1061 /* check if any of the PS tools is used */
1062 #if (defined(PS_DEC) || defined(DRM_PS))
1063 if (hDecoder->ps_used[ele] == 0)
1066 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
1067 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1068 #if (defined(PS_DEC) || defined(DRM_PS))
1070 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
1071 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
1072 hDecoder->downSampledSBR);
1077 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1078 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1084 /* copy L to R when no PS is used */
1085 #if (defined(PS_DEC) || defined(DRM_PS))
1086 if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) &&
1087 (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2))
1089 int ele = hDecoder->fr_ch_ele;
1090 int ch = sce->channel;
1091 int frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
1092 frame_size *= hDecoder->frameLength*sizeof(real_t);
1094 memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
1101 uint8_t reconstruct_channel_pair(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2,
1102 element *cpe, int16_t *spec_data1, int16_t *spec_data2)
1105 ALIGN real_t spec_coef1[1024];
1106 ALIGN real_t spec_coef2[1024];
1109 int64_t count = faad_get_ts();
1111 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
1113 retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
1117 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
1120 /* dequantisation and scaling */
1121 retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
1124 retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
1129 count = faad_get_ts() - count;
1130 hDecoder->requant_cycles += count;
1135 if (ics1->ms_mask_present)
1137 pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type,
1138 &(hDecoder->__r1), &(hDecoder->__r2));
1140 pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
1141 &(hDecoder->__r1), &(hDecoder->__r2));
1142 pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
1143 &(hDecoder->__r1), &(hDecoder->__r2));
1146 /* mid/side decoding */
1147 ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1152 for (i = 0; i < 1024; i++)
1154 //printf("%d\n", spec_coef1[i]);
1155 printf("0x%.8X\n", spec_coef1[i]);
1157 for (i = 0; i < 1024; i++)
1159 //printf("%d\n", spec_coef2[i]);
1160 printf("0x%.8X\n", spec_coef2[i]);
1165 /* intensity stereo decoding */
1166 is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1171 for (i = 0; i < 1024; i++)
1173 printf("%d\n", spec_coef1[i]);
1174 //printf("0x%.8X\n", spec_coef1[i]);
1176 for (i = 0; i < 1024; i++)
1178 printf("%d\n", spec_coef2[i]);
1179 //printf("0x%.8X\n", spec_coef2[i]);
1185 /* MAIN object type prediction */
1186 if (hDecoder->object_type == MAIN)
1188 /* intra channel prediction */
1189 ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
1190 hDecoder->sf_index);
1191 ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
1192 hDecoder->sf_index);
1194 /* In addition, for scalefactor bands coded by perceptual
1195 noise substitution the predictors belonging to the
1196 corresponding spectral coefficients are reset.
1198 pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
1199 pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
1204 if (is_ltp_ot(hDecoder->object_type))
1206 ltp_info *ltp1 = &(ics1->ltp);
1207 ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp);
1209 if (hDecoder->object_type == LD)
1211 if (ltp1->data_present)
1213 if (ltp1->lag_update)
1214 hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
1216 ltp1->lag = hDecoder->ltp_lag[cpe->channel];
1217 if (ltp2->data_present)
1219 if (ltp2->lag_update)
1220 hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
1222 ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
1226 /* long term prediction */
1227 lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
1228 ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
1229 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1230 lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
1231 ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
1232 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1237 tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
1238 spec_coef1, hDecoder->frameLength);
1239 tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
1240 spec_coef2, hDecoder->frameLength);
1243 if (hDecoder->drc->present)
1245 if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
1246 drc_decode(hDecoder->drc, spec_coef1);
1247 if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
1248 drc_decode(hDecoder->drc, spec_coef2);
1253 if (hDecoder->object_type != SSR)
1256 ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1257 hDecoder->window_shape_prev[cpe->channel], spec_coef1,
1258 hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
1259 hDecoder->object_type, hDecoder->frameLength);
1260 ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1261 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1262 hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
1263 hDecoder->object_type, hDecoder->frameLength);
1266 ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1267 hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
1268 hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
1269 hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
1270 ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1271 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
1272 hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
1273 hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
1277 /* save window shape for next frame */
1278 hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
1279 hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
1282 if (is_ltp_ot(hDecoder->object_type))
1284 lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
1285 hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type);
1286 lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
1287 hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type);
1292 if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1293 && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1295 int ele = hDecoder->fr_ch_ele;
1296 int ch0 = cpe->channel;
1297 int ch1 = cpe->paired_channel;
1299 /* following case can happen when forceUpSampling == 1 */
1300 if (hDecoder->sbr[ele] == NULL)
1302 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1303 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1304 hDecoder->downSampledSBR
1311 if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1312 hDecoder->sbr[ele]->maxAACLine = 8*min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1314 hDecoder->sbr[ele]->maxAACLine = min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1316 retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1317 hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1318 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1321 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1322 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])