Rework of libfaad in several areas. Allow removal of malloc with a new define FAAD_ST...
[kugel-rb.git] / apps / codecs / libfaad / specrec.c
blobb5c830523703cd1d3253fd93e637233e1ad03ab3
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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.
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
20 ** forbidden.
22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25 ** $Id$
26 **/
29 Spectral reconstruction:
30 - grouping/sectioning
31 - inverse quantization
32 - applying scalefactors
35 #include "common.h"
36 #include "structs.h"
38 #include <string.h>
39 #include <stdlib.h>
40 #include "specrec.h"
41 #include "filtbank.h"
42 #include "syntax.h"
43 #include "iq_table.h"
44 #include "ms.h"
45 #include "is.h"
46 #include "pns.h"
47 #include "tns.h"
48 #include "drc.h"
49 #include "lt_predict.h"
50 #include "ic_predict.h"
51 #ifdef SSR_DEC
52 #include "ssr.h"
53 #include "ssr_fb.h"
54 #endif
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);
62 #ifdef LD_DEC
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
71 #endif
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
121 #ifdef LD_DEC
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,
126 428, 460, 512
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,
133 432, 480
135 #endif
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
150 #ifdef LD_DEC
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,
162 384, 416, 448, 480
164 #endif
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
174 #ifdef LD_DEC
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,
179 448, 480, 512
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
187 #endif
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 */
234 #ifdef LD_DEC
235 static const uint16_t *swb_offset_512_window[] =
237 0, /* 96000 */
238 0, /* 88200 */
239 0, /* 64000 */
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 */
245 0, /* 16000 */
246 0, /* 12000 */
247 0, /* 11025 */
248 0 /* 8000 */
251 static const uint16_t *swb_offset_480_window[] =
253 0, /* 96000 */
254 0, /* 88200 */
255 0, /* 64000 */
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 */
261 0, /* 16000 */
262 0, /* 12000 */
263 0, /* 11025 */
264 0 /* 8000 */
266 #endif
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;
291 /* 4.5.2.3.4 */
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)
306 uint8_t i, g;
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;
317 #ifdef LD_DEC
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];
324 } else {
325 #endif
326 if (hDecoder->frameLength == 1024)
327 ics->num_swb = num_swb_1024_window[sf_index];
328 else /* if (hDecoder->frameLength == 960) */
329 ics->num_swb = num_swb_960_window[sf_index];
330 #ifdef LD_DEC
332 #endif
334 /* preparation of sect_sfb_offset for long blocks */
335 /* also copy the last value! */
336 #ifdef LD_DEC
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;
355 } else {
356 #endif
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;
364 #ifdef LD_DEC
366 #endif
367 return 0;
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;
383 } else {
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++)
391 uint16_t width;
392 uint8_t sect_sfb = 0;
393 uint16_t offset = 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];
400 } else {
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;
406 offset += width;
408 ics->sect_sfb_offset[g][sect_sfb] = offset;
410 return 0;
411 default:
412 return 1;
416 /* iquant() */
417 /* output = sign(input)*abs(input)^(4/3) */
418 /**/
419 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
421 #ifndef BIG_IQ_TABLE
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),
429 REAL_CONST(0)
431 real_t x1, x2;
433 int16_t sgn = 1;
435 if (q < 0)
437 q = -q;
438 sgn = -1;
441 if (q < IQ_TABLE_SIZE)
443 //#define IQUANT_PRINT
444 #ifdef IQUANT_PRINT
445 //printf("0x%.8X\n", sgn * tab[q]);
446 printf("%d\n", sgn * tab[q]);
447 #endif
448 return sgn * tab[q];
451 if (q >= 8192)
453 *error = 17;
454 return 0;
457 /* linear interpolation */
458 x1 = tab[q>>3];
459 x2 = tab[(q>>3) + 1];
460 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
461 #else /* #ifndef BIG_IQ_TABLE */
462 if (q < 0)
464 /* tab contains a value for all possible q [0,8192] */
465 if (LIKELY(-q < IQ_TABLE_SIZE))
466 return -tab[-q];
468 *error = 17;
469 return 0;
470 } else {
471 /* tab contains a value for all possible q [0,8192] */
472 if (LIKELY(q < IQ_TABLE_SIZE))
473 return tab[q];
475 *error = 17;
476 return 0;
478 #endif
481 #ifndef FIXED_POINT
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,
490 0.0625, 0.125, 0.25,
491 0.5, 1.0, 2.0,
492 4.0, 8.0, 16.0, 32.0,
493 64.0, 128.0, 256.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
505 #endif
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
513 order.
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
520 SHORT_WINDOWs.
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
528 spectral order.
530 static const real_t pow2_table[] ICONST_ATTR =
532 COEF_CONST(1.0),
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;
543 (void)frame_len;
544 uint8_t g, sfb, win;
545 uint16_t width, bin, k, gindex, wa, wb;
546 uint8_t error = 0; /* Init error flag */
547 real_t scf;
549 k = 0;
550 gindex = 0;
552 for (g = 0; g < ics->num_window_groups; g++)
554 uint16_t j = 0;
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++)
560 int32_t exp, frac;
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)
568 exp = 0;
569 frac = 0;
570 } else {
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;
577 #ifdef FIXED_POINT
578 exp -= 25;
579 /* IMDCT pre-scaling */
580 if (hDecoder->object_type == LD)
582 exp -= 6 /*9*/;
583 } else {
584 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
585 exp -= 4 /*7*/;
586 else
587 exp -= 7 /*10*/;
589 #endif
591 wa = gindex + j;
593 #ifndef FIXED_POINT
594 scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
595 #else
596 scf = pow2_table[frac];
597 #endif
599 for (win = 0; win < ics->window_group_length[g]; win++)
601 for (bin = 0; bin < width; bin += 4)
603 #ifndef FIXED_POINT
604 wb = wa + bin;
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;
611 #else
612 wb = wa + bin;
614 if (exp>=0)
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);
620 } else {
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);
626 //#define SCFS_PRINT
627 #ifdef SCFS_PRINT
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]);
636 #endif
637 #endif
639 gincrease += 4;
640 k += 4;
642 wa += win_inc;
644 j += width;
646 gindex += gincrease;
649 return error;
652 static uint8_t allocate_single_channel(NeAACDecHandle hDecoder, uint8_t channel,
653 uint8_t output_channels)
655 (void)output_channels;
656 (void)hDecoder;
657 (void)channel;
658 return 0;
661 static uint8_t allocate_channel_pair(NeAACDecHandle hDecoder,
662 uint8_t channel, uint8_t paired_channel)
664 (void)paired_channel;
665 (void)hDecoder;
666 (void)channel;
667 return 0;
670 uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder, ic_stream *ics,
671 element *sce, int16_t *spec_data)
673 uint8_t retval, output_channels;
675 #ifdef PROFILE
676 int64_t count = faad_get_ts();
677 #endif
680 /* always allocate 2 channels, PS can always "suddenly" turn up */
681 #if (defined(PS_DEC) || defined(DRM_PS))
682 output_channels = 2;
683 #else
684 output_channels = 1;
685 #endif
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 */
693 return 21;
696 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
698 retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
699 if (retval > 0)
700 return retval;
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);
708 if (retval > 0)
709 return retval;
711 #ifdef PROFILE
712 count = faad_get_ts() - count;
713 hDecoder->requant_cycles += count;
714 #endif
717 /* pns decoding */
718 pns_decode(ics, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
720 #ifdef MAIN_DEC
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,
726 hDecoder->sf_index);
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]);
734 #endif
736 #ifdef LTP_DEC
737 if (is_ltp_ot(hDecoder->object_type))
739 #ifdef LD_DEC
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];
749 #endif
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);
756 #endif
758 /* tns decoding */
759 tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
760 spec_coef1, hDecoder->frameLength);
762 /* drc decoding */
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);
769 /* filter bank */
770 #ifdef SSR_DEC
771 if (hDecoder->object_type != SSR)
773 #endif
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);
778 #ifdef SSR_DEC
779 } else {
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);
785 #endif
787 /* save window shape for next frame */
788 hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
790 #ifdef LTP_DEC
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);
796 #endif
798 #ifdef SBR_DEC
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
812 #ifdef DRM
814 #endif
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)];
820 else
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)
827 #endif
828 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
829 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
830 #if (defined(PS_DEC) || defined(DRM_PS))
831 } else {
832 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
833 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
834 hDecoder->downSampledSBR);
836 #endif
837 if (retval > 0)
838 return retval;
839 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
840 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
842 return 23;
844 #endif
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);
857 #endif
859 return 0;
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)
865 uint8_t retval;
867 #ifdef PROFILE
868 int64_t count = faad_get_ts();
869 #endif
870 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
872 retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
873 if (retval > 0)
874 return retval;
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);
881 if (retval > 0)
882 return retval;
883 retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
884 if (retval > 0)
885 return retval;
887 #ifdef PROFILE
888 count = faad_get_ts() - count;
889 hDecoder->requant_cycles += count;
890 #endif
893 /* pns decoding */
894 if (ics1->ms_mask_present)
896 pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type);
897 } else {
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);
905 #if 0
907 int i;
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]);
919 #endif
921 /* intensity stereo decoding */
922 is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
924 #if 0
926 int i;
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]);
938 #endif
940 #ifdef MAIN_DEC
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,
946 hDecoder->sf_index);
947 ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
948 hDecoder->sf_index);
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]);
957 #endif
959 #ifdef LTP_DEC
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);
964 #ifdef LD_DEC
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];
980 #endif
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);
990 #endif
992 /* tns decoding */
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);
998 /* drc decoding */
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);
1007 /* filter bank */
1008 #ifdef SSR_DEC
1009 if (hDecoder->object_type != SSR)
1011 #endif
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);
1020 #ifdef SSR_DEC
1021 } else {
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);
1031 #endif
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;
1037 #ifdef LTP_DEC
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);
1045 #endif
1047 #ifdef SBR_DEC
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
1062 #ifdef DRM
1064 #endif
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)];
1070 else
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);
1076 if (retval > 0)
1077 return retval;
1078 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1079 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1081 return 23;
1083 #endif
1085 return 0;