summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libfaad/specrec.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/specrec.c')
-rw-r--r--lib/rbcodec/codecs/libfaad/specrec.c1092
1 files changed, 1092 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/specrec.c b/lib/rbcodec/codecs/libfaad/specrec.c
new file mode 100644
index 0000000000..200239bddc
--- /dev/null
+++ b/lib/rbcodec/codecs/libfaad/specrec.c
@@ -0,0 +1,1092 @@
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.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program; if not, write to the Free Software
17** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
22** Commercial non-GPL licensing of this software is possible.
23** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24**
25** $Id$
26**/
27
28/*
29 Spectral reconstruction:
30 - grouping/sectioning
31 - inverse quantization
32 - applying scalefactors
33*/
34
35#include "common.h"
36#include "structs.h"
37
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
55
56/* static function declarations */
57static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
58 ic_stream *ics, int16_t *quant_data,
59 real_t *spec_data, uint16_t frame_len);
60
61
62#ifdef LD_DEC
63static const uint8_t num_swb_512_window[] =
64{
65 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
66};
67static const uint8_t num_swb_480_window[] =
68{
69 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
70};
71#endif
72
73static const uint8_t num_swb_960_window[] ICONST_ATTR =
74{
75 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
76};
77
78static const uint8_t num_swb_1024_window[] ICONST_ATTR =
79{
80 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
81};
82
83static const uint8_t num_swb_128_window[] ICONST_ATTR =
84{
85 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
86};
87
88static const uint16_t swb_offset_1024_96[] =
89{
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
93};
94
95static const uint16_t swb_offset_128_96[] =
96{
97 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
98};
99
100static const uint16_t swb_offset_1024_64[] =
101{
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
106};
107
108static const uint16_t swb_offset_128_64[] =
109{
110 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
111};
112
113static const uint16_t swb_offset_1024_48[] ICONST_ATTR =
114{
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
119};
120
121#ifdef LD_DEC
122static const uint16_t swb_offset_512_48[] =
123{
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
127};
128
129static const uint16_t swb_offset_480_48[] =
130{
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
134};
135#endif
136
137static const uint16_t swb_offset_128_48[] ICONST_ATTR =
138{
139 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
140};
141
142static const uint16_t swb_offset_1024_32[] ICONST_ATTR =
143{
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
148};
149
150#ifdef LD_DEC
151static const uint16_t swb_offset_512_32[] =
152{
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
156};
157
158static const uint16_t swb_offset_480_32[] =
159{
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
163};
164#endif
165
166static const uint16_t swb_offset_1024_24[] ICONST_ATTR =
167{
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
172};
173
174#ifdef LD_DEC
175static const uint16_t swb_offset_512_24[] =
176{
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
180};
181
182static const uint16_t swb_offset_480_24[] =
183{
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
186};
187#endif
188
189static const uint16_t swb_offset_128_24[] ICONST_ATTR =
190{
191 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
192};
193
194static const uint16_t swb_offset_1024_16[] ICONST_ATTR =
195{
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
199};
200
201static const uint16_t swb_offset_128_16[] ICONST_ATTR =
202{
203 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
204};
205
206static const uint16_t swb_offset_1024_8[] ICONST_ATTR =
207{
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
211};
212
213static const uint16_t swb_offset_128_8[] ICONST_ATTR =
214{
215 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
216};
217
218static const uint16_t *const swb_offset_1024_window[] ICONST_ATTR =
219{
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 */
232};
233
234#ifdef LD_DEC
235static const uint16_t *swb_offset_512_window[] =
236{
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 */
249};
250
251static const uint16_t *swb_offset_480_window[] =
252{
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 */
265};
266#endif
267
268static const uint16_t *const swb_offset_128_window[] ICONST_ATTR =
269{
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 */
282};
283
284#define bit_set(A, B) ((A) & (1<<(B)))
285
286/* static variables */
287/* used by reconstruct_single_channel() and reconstruct_channel_pair() */
288static real_t spec_coef1[FRAME_LEN] IBSS_ATTR MEM_ALIGN_ATTR;
289static real_t spec_coef2[FRAME_LEN] IBSS_ATTR MEM_ALIGN_ATTR;
290
291/* 4.5.2.3.4 */
292/*
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().
303*/
304uint8_t window_grouping_info(NeAACDecHandle hDecoder, ic_stream *ics)
305{
306 uint8_t i, g;
307
308 uint8_t sf_index = hDecoder->sf_index;
309
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)
319 {
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
331 }
332#endif
333
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)
338 {
339 if (hDecoder->frameLength == 512)
340 {
341 for (i = 0; i < ics->num_swb; i++)
342 {
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];
345 }
346 } else /* if (hDecoder->frameLength == 480) */ {
347 for (i = 0; i < ics->num_swb; i++)
348 {
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];
351 }
352 }
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++)
358 {
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];
361 }
362 ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
363 ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
364#ifdef LD_DEC
365 }
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];
373
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;
377
378 for (i = 0; i < ics->num_windows-1; i++) {
379 if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
380 {
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;
385 }
386 }
387
388 /* preparation of sect_sfb_offset for short blocks */
389 for (g = 0; g < ics->num_window_groups; g++)
390 {
391 uint16_t width;
392 uint8_t sect_sfb = 0;
393 uint16_t offset = 0;
394
395 for (i = 0; i < ics->num_swb; i++)
396 {
397 if (i+1 == ics->num_swb)
398 {
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];
403 }
404 width *= ics->window_group_length[g];
405 ics->sect_sfb_offset[g][sect_sfb++] = offset;
406 offset += width;
407 }
408 ics->sect_sfb_offset[g][sect_sfb] = offset;
409 }
410 return 0;
411 default:
412 return 1;
413 }
414}
415
416/* iquant() */
417/* output = sign(input)*abs(input)^(4/3) */
418/**/
419static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
420{
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
425 */
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)
430 };
431 real_t x1, x2;
432
433 int16_t sgn = 1;
434
435 if (q < 0)
436 {
437 q = -q;
438 sgn = -1;
439 }
440
441 if (q < IQ_TABLE_SIZE)
442 {
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];
449 }
450
451 if (q >= 8192)
452 {
453 *error = 17;
454 return 0;
455 }
456
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)
463 {
464 /* tab contains a value for all possible q [0,8192] */
465 if (LIKELY(-q < IQ_TABLE_SIZE))
466 return -tab[-q];
467
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];
474
475 *error = 17;
476 return 0;
477 }
478#endif
479}
480
481#ifndef FIXED_POINT
482static 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
504};
505#endif
506
507/* quant_to_spec: perform dequantisation and scaling
508 * and in case of short block it also does the deinterleaving
509 */
510/*
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.
529*/
530static const real_t pow2_table[] ICONST_ATTR =
531{
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 */
536};
537static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
538 ic_stream *ics, int16_t *quant_data,
539 real_t *spec_data, uint16_t frame_len)
540{
541 const real_t *tab = iq_table;
542
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;
548
549 k = 0;
550 gindex = 0;
551
552 for (g = 0; g < ics->num_window_groups; g++)
553 {
554 uint16_t j = 0;
555 uint16_t gincrease = 0;
556 uint16_t win_inc = ics->swb_offset[ics->num_swb];
557
558 for (sfb = 0; sfb < ics->num_swb; sfb++)
559 {
560 int32_t exp, frac;
561
562 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
563
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)
567 {
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;
575 }
576
577#ifdef FIXED_POINT
578 exp -= 25;
579 /* IMDCT pre-scaling */
580 if (hDecoder->object_type == LD)
581 {
582 exp -= 6 /*9*/;
583 } else {
584 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
585 exp -= 4 /*7*/;
586 else
587 exp -= 7 /*10*/;
588 }
589#endif
590
591 wa = gindex + j;
592
593#ifndef FIXED_POINT
594 scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
595#else
596 scf = pow2_table[frac];
597#endif
598
599 for (win = 0; win < ics->window_group_length[g]; win++)
600 {
601 for (bin = 0; bin < width; bin += 4)
602 {
603#ifndef FIXED_POINT
604 wb = wa + bin;
605
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;
610
611#else
612 wb = wa + bin;
613
614 if (exp>=0)
615 {
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);
625 }
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
638
639 gincrease += 4;
640 k += 4;
641 }
642 wa += win_inc;
643 }
644 j += width;
645 }
646 gindex += gincrease;
647 }
648
649 return error;
650}
651
652static uint8_t allocate_single_channel(NeAACDecHandle hDecoder, uint8_t channel,
653 uint8_t output_channels)
654{
655 (void)output_channels;
656 (void)hDecoder;
657 (void)channel;
658 return 0;
659}
660
661static uint8_t allocate_channel_pair(NeAACDecHandle hDecoder,
662 uint8_t channel, uint8_t paired_channel)
663{
664 (void)paired_channel;
665 (void)hDecoder;
666 (void)channel;
667 return 0;
668}
669
670uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder, ic_stream *ics,
671 element *sce, int16_t *spec_data)
672{
673 uint8_t retval, output_channels;
674
675#ifdef PROFILE
676 int64_t count = faad_get_ts();
677#endif
678
679
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
686
687 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
688 {
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;
694 }
695
696 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
697 {
698 retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
699 if (retval > 0)
700 return retval;
701
702 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
703 }
704
705
706 /* dequantisation and scaling */
707 retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef1, hDecoder->frameLength);
708 if (retval > 0)
709 return retval;
710
711#ifdef PROFILE
712 count = faad_get_ts() - count;
713 hDecoder->requant_cycles += count;
714#endif
715
716
717 /* pns decoding */
718 pns_decode(ics, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
719
720#ifdef MAIN_DEC
721 /* MAIN object type prediction */
722 if (hDecoder->object_type == MAIN)
723 {
724 /* intra channel prediction */
725 ic_prediction(ics, spec_coef1, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
726 hDecoder->sf_index);
727
728 /* In addition, for scalefactor bands coded by perceptual
729 noise substitution the predictors belonging to the
730 corresponding spectral coefficients are reset.
731 */
732 pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
733 }
734#endif
735
736#ifdef LTP_DEC
737 if (is_ltp_ot(hDecoder->object_type))
738 {
739#ifdef LD_DEC
740 if (hDecoder->object_type == LD)
741 {
742 if (ics->ltp.data_present)
743 {
744 if (ics->ltp.lag_update)
745 hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
746 }
747 ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
748 }
749#endif
750
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);
755 }
756#endif
757
758 /* tns decoding */
759 tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
760 spec_coef1, hDecoder->frameLength);
761
762 /* drc decoding */
763 if (hDecoder->drc->present)
764 {
765 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
766 drc_decode(hDecoder->drc, spec_coef1);
767 }
768
769 /* filter bank */
770#ifdef SSR_DEC
771 if (hDecoder->object_type != SSR)
772 {
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);
784 }
785#endif
786
787 /* save window shape for next frame */
788 hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
789
790#ifdef LTP_DEC
791 if (is_ltp_ot(hDecoder->object_type))
792 {
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);
795 }
796#endif
797
798#ifdef SBR_DEC
799 if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
800 && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
801 {
802 uint8_t ele = hDecoder->fr_ch_ele;
803 uint8_t ch = sce->channel;
804
805 /* following case can happen when forceUpSampling == 1 */
806 if (hDecoder->sbr[ele] == NULL)
807 {
808 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
809 hDecoder->element_id[ele], ele,
810 2*get_sample_rate(hDecoder->sf_index),
811 hDecoder->downSampledSBR, 0);
812#ifndef FAAD_STATIC_ALLOC
813 if (hDecoder->sbr[ele] == NULL)
814 {
815 /* could not allocate memory */
816 return 28;
817 }
818#endif
819 }
820
821 if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
822 hDecoder->sbr[ele]->maxAACLine = 8*sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
823 else
824 hDecoder->sbr[ele]->maxAACLine = sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
825
826 /* check if any of the PS tools is used */
827#if (defined(PS_DEC) || defined(DRM_PS))
828 if (hDecoder->ps_used[ele] == 0)
829 {
830#endif
831 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
832 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
833#if (defined(PS_DEC) || defined(DRM_PS))
834 } else {
835 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
836 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
837 hDecoder->downSampledSBR);
838 }
839#endif
840 if (retval > 0)
841 return retval;
842 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
843 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
844 {
845 return 23;
846 }
847#endif
848
849 /* copy L to R when no PS is used */
850#if (defined(PS_DEC) || defined(DRM_PS))
851 if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0))
852 {
853 uint8_t ele = hDecoder->fr_ch_ele;
854 uint8_t ch = sce->channel;
855 uint16_t frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
856 frame_size *= hDecoder->frameLength*sizeof(real_t);
857
858 memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
859 }
860#endif
861
862 return 0;
863}
864
865uint8_t reconstruct_channel_pair(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
866 element *cpe, int16_t *spec_data1, int16_t *spec_data2)
867{
868 uint8_t retval;
869
870#ifdef PROFILE
871 int64_t count = faad_get_ts();
872#endif
873 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
874 {
875 retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
876 if (retval > 0)
877 return retval;
878
879 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
880 }
881
882 /* dequantisation and scaling */
883 retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
884 if (retval > 0)
885 return retval;
886 retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
887 if (retval > 0)
888 return retval;
889
890#ifdef PROFILE
891 count = faad_get_ts() - count;
892 hDecoder->requant_cycles += count;
893#endif
894
895
896 /* pns decoding */
897 if (ics1->ms_mask_present)
898 {
899 pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type);
900 } else {
901 pns_decode(ics1, NULL, spec_coef1, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
902 pns_decode(ics2, NULL, spec_coef2, NULL, hDecoder->frameLength, 0, hDecoder->object_type);
903 }
904
905 /* mid/side decoding */
906 ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
907
908#if 0
909 {
910 int i;
911 for (i = 0; i < 1024; i++)
912 {
913 //printf("%d\n", spec_coef1[i]);
914 printf("0x%.8X\n", spec_coef1[i]);
915 }
916 for (i = 0; i < 1024; i++)
917 {
918 //printf("%d\n", spec_coef2[i]);
919 printf("0x%.8X\n", spec_coef2[i]);
920 }
921 }
922#endif
923
924 /* intensity stereo decoding */
925 is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
926
927#if 0
928 {
929 int i;
930 for (i = 0; i < 1024; i++)
931 {
932 printf("%d\n", spec_coef1[i]);
933 //printf("0x%.8X\n", spec_coef1[i]);
934 }
935 for (i = 0; i < 1024; i++)
936 {
937 printf("%d\n", spec_coef2[i]);
938 //printf("0x%.8X\n", spec_coef2[i]);
939 }
940 }
941#endif
942
943#ifdef MAIN_DEC
944 /* MAIN object type prediction */
945 if (hDecoder->object_type == MAIN)
946 {
947 /* intra channel prediction */
948 ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
949 hDecoder->sf_index);
950 ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
951 hDecoder->sf_index);
952
953 /* In addition, for scalefactor bands coded by perceptual
954 noise substitution the predictors belonging to the
955 corresponding spectral coefficients are reset.
956 */
957 pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
958 pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
959 }
960#endif
961
962#ifdef LTP_DEC
963 if (is_ltp_ot(hDecoder->object_type))
964 {
965 ltp_info *ltp1 = &(ics1->ltp);
966 ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp);
967#ifdef LD_DEC
968 if (hDecoder->object_type == LD)
969 {
970 if (ltp1->data_present)
971 {
972 if (ltp1->lag_update)
973 hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
974 }
975 ltp1->lag = hDecoder->ltp_lag[cpe->channel];
976 if (ltp2->data_present)
977 {
978 if (ltp2->lag_update)
979 hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
980 }
981 ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
982 }
983#endif
984
985 /* long term prediction */
986 lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
987 ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
988 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
989 lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
990 ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
991 hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
992 }
993#endif
994
995 /* tns decoding */
996 tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
997 spec_coef1, hDecoder->frameLength);
998 tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
999 spec_coef2, hDecoder->frameLength);
1000
1001 /* drc decoding */
1002 if (hDecoder->drc->present)
1003 {
1004 if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
1005 drc_decode(hDecoder->drc, spec_coef1);
1006 if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
1007 drc_decode(hDecoder->drc, spec_coef2);
1008 }
1009
1010 /* filter bank */
1011#ifdef SSR_DEC
1012 if (hDecoder->object_type != SSR)
1013 {
1014#endif
1015 ifilter_bank(ics1->window_sequence,ics1->window_shape,
1016 hDecoder->window_shape_prev[cpe->channel],spec_coef1,
1017 hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
1018 hDecoder->object_type, hDecoder->frameLength);
1019 ifilter_bank(ics2->window_sequence,ics2->window_shape,
1020 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1021 hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
1022 hDecoder->object_type, hDecoder->frameLength);
1023#ifdef SSR_DEC
1024 } else {
1025 ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1026 hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
1027 hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
1028 hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
1029 ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1030 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
1031 hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
1032 hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
1033 }
1034#endif
1035
1036 /* save window shape for next frame */
1037 hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
1038 hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
1039
1040#ifdef LTP_DEC
1041 if (is_ltp_ot(hDecoder->object_type))
1042 {
1043 lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
1044 hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type);
1045 lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
1046 hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type);
1047 }
1048#endif
1049
1050#ifdef SBR_DEC
1051 if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1052 && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1053 {
1054 uint8_t ele = hDecoder->fr_ch_ele;
1055 uint8_t ch0 = cpe->channel;
1056 uint8_t ch1 = cpe->paired_channel;
1057
1058 /* following case can happen when forceUpSampling == 1 */
1059 if (hDecoder->sbr[ele] == NULL)
1060 {
1061 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1062 hDecoder->element_id[ele], ele,
1063 2*get_sample_rate(hDecoder->sf_index),
1064 hDecoder->downSampledSBR, 0);
1065#ifndef FAAD_STATIC_ALLOC
1066 if (hDecoder->sbr[ele] == NULL)
1067 {
1068 /* could not allocate memory */
1069 return 28;
1070 }
1071#endif
1072 }
1073
1074 if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1075 hDecoder->sbr[ele]->maxAACLine = 8*cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
1076 else
1077 hDecoder->sbr[ele]->maxAACLine = cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
1078
1079 retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1080 hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1081 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1082 if (retval > 0)
1083 return retval;
1084 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1085 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1086 {
1087 return 23;
1088 }
1089#endif
1090
1091 return 0;
1092}