Coverage Report

Created: 2026-07-24 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/specrec.c
Line
Count
Source
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: specrec.c,v 1.63 2010/06/04 20:47:56 menno Exp $
29
**/
30
31
/*
32
  Spectral reconstruction:
33
   - grouping/sectioning
34
   - inverse quantization
35
   - applying scalefactors
36
*/
37
38
#include "common.h"
39
#include "structs.h"
40
41
#include <stdlib.h>
42
#include "specrec.h"
43
#include "filtbank.h"
44
#include "syntax.h"
45
#include "iq_table.h"
46
#include "ms.h"
47
#include "is.h"
48
#include "pns.h"
49
#include "tns.h"
50
#include "drc.h"
51
#include "lt_predict.h"
52
#include "ic_predict.h"
53
#ifdef SSR_DEC
54
#include "ssr.h"
55
#include "ssr_fb.h"
56
#endif
57
58
59
/* static function declarations */
60
static uint8_t quant_to_spec(NeAACDecStruct *hDecoder,
61
                             ic_stream *ics, int16_t *quant_data,
62
                             real_t *spec_data, uint16_t frame_len);
63
64
65
#ifdef LD_DEC
66
ALIGN static const uint8_t num_swb_512_window[] =
67
{
68
    0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
69
};
70
ALIGN static const uint8_t num_swb_480_window[] =
71
{
72
    0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
73
};
74
#endif
75
76
ALIGN static const uint8_t num_swb_960_window[] =
77
{
78
    40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
79
};
80
81
ALIGN static const uint8_t num_swb_1024_window[] =
82
{
83
    41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
84
};
85
86
ALIGN static const uint8_t num_swb_128_window[] =
87
{
88
    12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
89
};
90
91
ALIGN static const uint16_t swb_offset_1024_96[] =
92
{
93
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
94
    64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240,
95
    276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
96
};
97
98
ALIGN static const uint16_t swb_offset_128_96[] =
99
{
100
    0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
101
};
102
103
ALIGN static const uint16_t swb_offset_1024_64[] =
104
{
105
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
106
    64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268,
107
    304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824,
108
    864, 904, 944, 984, 1024
109
};
110
111
ALIGN static const uint16_t swb_offset_128_64[] =
112
{
113
    0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
114
};
115
116
ALIGN static const uint16_t swb_offset_1024_48[] =
117
{
118
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
119
    80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
120
    320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
121
    768, 800, 832, 864, 896, 928, 1024
122
};
123
124
#ifdef LD_DEC
125
ALIGN static const uint16_t swb_offset_512_48[] =
126
{
127
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84,
128
    92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396,
129
    428, 460, 512
130
};
131
132
ALIGN static const uint16_t swb_offset_480_48[] =
133
{
134
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 ,80 ,88,
135
    96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400,
136
    432, 480
137
};
138
#endif
139
140
ALIGN static const uint16_t swb_offset_128_48[] =
141
{
142
    0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
143
};
144
145
ALIGN static const uint16_t swb_offset_1024_32[] =
146
{
147
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
148
    80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
149
    320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
150
    768, 800, 832, 864, 896, 928, 960, 992, 1024
151
};
152
153
#ifdef LD_DEC
154
ALIGN static const uint16_t swb_offset_512_32[] =
155
{
156
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80,
157
    88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352,
158
    384, 416, 448, 480, 512
159
};
160
161
ALIGN static const uint16_t swb_offset_480_32[] =
162
{
163
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80,
164
    88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352,
165
    384, 416, 448, 480
166
};
167
#endif
168
169
ALIGN static const uint16_t swb_offset_1024_24[] =
170
{
171
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
172
    76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220,
173
    240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704,
174
    768, 832, 896, 960, 1024
175
};
176
177
#ifdef LD_DEC
178
ALIGN static const uint16_t swb_offset_512_24[] =
179
{
180
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
181
    80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416,
182
    448, 480, 512
183
};
184
185
ALIGN static const uint16_t swb_offset_480_24[] =
186
{
187
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120,
188
    140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480
189
};
190
#endif
191
192
ALIGN static const uint16_t swb_offset_128_24[] =
193
{
194
    0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
195
};
196
197
ALIGN static const uint16_t swb_offset_1024_16[] =
198
{
199
    0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124,
200
    136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344,
201
    368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024
202
};
203
204
ALIGN static const uint16_t swb_offset_128_16[] =
205
{
206
    0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
207
};
208
209
ALIGN static const uint16_t swb_offset_1024_8[] =
210
{
211
    0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172,
212
    188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448,
213
    476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024
214
};
215
216
ALIGN static const uint16_t swb_offset_128_8[] =
217
{
218
    0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
219
};
220
221
ALIGN static const uint16_t *swb_offset_1024_window[] =
222
{
223
    swb_offset_1024_96,      /* 96000 */
224
    swb_offset_1024_96,      /* 88200 */
225
    swb_offset_1024_64,      /* 64000 */
226
    swb_offset_1024_48,      /* 48000 */
227
    swb_offset_1024_48,      /* 44100 */
228
    swb_offset_1024_32,      /* 32000 */
229
    swb_offset_1024_24,      /* 24000 */
230
    swb_offset_1024_24,      /* 22050 */
231
    swb_offset_1024_16,      /* 16000 */
232
    swb_offset_1024_16,      /* 12000 */
233
    swb_offset_1024_16,      /* 11025 */
234
    swb_offset_1024_8        /* 8000  */
235
};
236
237
#ifdef LD_DEC
238
ALIGN static const uint16_t *swb_offset_512_window[] =
239
{
240
    0,                       /* 96000 */
241
    0,                       /* 88200 */
242
    0,                       /* 64000 */
243
    swb_offset_512_48,       /* 48000 */
244
    swb_offset_512_48,       /* 44100 */
245
    swb_offset_512_32,       /* 32000 */
246
    swb_offset_512_24,       /* 24000 */
247
    swb_offset_512_24,       /* 22050 */
248
    0,                       /* 16000 */
249
    0,                       /* 12000 */
250
    0,                       /* 11025 */
251
    0                        /* 8000  */
252
};
253
254
ALIGN static const uint16_t *swb_offset_480_window[] =
255
{
256
    0,                       /* 96000 */
257
    0,                       /* 88200 */
258
    0,                       /* 64000 */
259
    swb_offset_480_48,       /* 48000 */
260
    swb_offset_480_48,       /* 44100 */
261
    swb_offset_480_32,       /* 32000 */
262
    swb_offset_480_24,       /* 24000 */
263
    swb_offset_480_24,       /* 22050 */
264
    0,                       /* 16000 */
265
    0,                       /* 12000 */
266
    0,                       /* 11025 */
267
    0                        /* 8000  */
268
};
269
#endif
270
271
ALIGN static const  uint16_t *swb_offset_128_window[] =
272
{
273
    swb_offset_128_96,       /* 96000 */
274
    swb_offset_128_96,       /* 88200 */
275
    swb_offset_128_64,       /* 64000 */
276
    swb_offset_128_48,       /* 48000 */
277
    swb_offset_128_48,       /* 44100 */
278
    swb_offset_128_48,       /* 32000 */
279
    swb_offset_128_24,       /* 24000 */
280
    swb_offset_128_24,       /* 22050 */
281
    swb_offset_128_16,       /* 16000 */
282
    swb_offset_128_16,       /* 12000 */
283
    swb_offset_128_16,       /* 11025 */
284
    swb_offset_128_8         /* 8000  */
285
};
286
287
125k
#define bit_set(A, B) ((A) & (1<<(B)))
288
289
/* 4.5.2.3.4 */
290
/*
291
  - determine the number of windows in a window_sequence named num_windows
292
  - determine the number of window_groups named num_window_groups
293
  - determine the number of windows in each group named window_group_length[g]
294
  - determine the total number of scalefactor window bands named num_swb for
295
    the actual window type
296
  - determine swb_offset[swb], the offset of the first coefficient in
297
    scalefactor window band named swb of the window actually used
298
  - determine sect_sfb_offset[g][section],the offset of the first coefficient
299
    in section named section. This offset depends on window_sequence and
300
    scale_factor_grouping and is needed to decode the spectral_data().
301
*/
302
uint8_t window_grouping_info(NeAACDecStruct *hDecoder, ic_stream *ics)
303
268k
{
304
268k
    uint8_t i, g;
305
306
268k
    uint8_t sf_index = hDecoder->sf_index;
307
308
268k
    if (sf_index >= 12)
309
3
        return 32;
310
311
268k
    switch (ics->window_sequence) {
312
236k
    case ONLY_LONG_SEQUENCE:
313
246k
    case LONG_START_SEQUENCE:
314
250k
    case LONG_STOP_SEQUENCE:
315
250k
        ics->num_windows = 1;
316
250k
        ics->num_window_groups = 1;
317
250k
        ics->window_group_length[ics->num_window_groups-1] = 1;
318
250k
#ifdef LD_DEC
319
250k
        if (hDecoder->object_type == LD)
320
2.73k
        {
321
2.73k
            if (hDecoder->frameLength == 512)
322
938
                ics->num_swb = num_swb_512_window[sf_index];
323
1.79k
            else /* if (hDecoder->frameLength == 480) */
324
1.79k
                ics->num_swb = num_swb_480_window[sf_index];
325
248k
        } else {
326
248k
#endif
327
248k
            if (hDecoder->frameLength == 1024)
328
215k
                ics->num_swb = num_swb_1024_window[sf_index];
329
32.6k
            else /* if (hDecoder->frameLength == 960) */
330
32.6k
                ics->num_swb = num_swb_960_window[sf_index];
331
248k
#ifdef LD_DEC
332
248k
        }
333
250k
#endif
334
335
250k
        if (ics->max_sfb > ics->num_swb)
336
89
        {
337
89
            return 32;
338
89
        }
339
340
        /* preparation of sect_sfb_offset for long blocks */
341
        /* also copy the last value! */
342
250k
#ifdef LD_DEC
343
250k
        if (hDecoder->object_type == LD)
344
2.72k
        {
345
2.72k
            if (hDecoder->frameLength == 512)
346
930
            {
347
23.7k
                for (i = 0; i < ics->num_swb; i++)
348
22.7k
                {
349
22.7k
                    ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i];
350
22.7k
                    ics->swb_offset[i] = swb_offset_512_window[sf_index][i];
351
22.7k
                }
352
1.79k
            } else /* if (hDecoder->frameLength == 480) */ {
353
59.5k
                for (i = 0; i < ics->num_swb; i++)
354
57.7k
                {
355
57.7k
                    ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i];
356
57.7k
                    ics->swb_offset[i] = swb_offset_480_window[sf_index][i];
357
57.7k
                }
358
1.79k
            }
359
2.72k
            ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
360
2.72k
            ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
361
2.72k
            ics->swb_offset_max = hDecoder->frameLength;
362
248k
        } else {
363
248k
#endif
364
10.6M
            for (i = 0; i < ics->num_swb; i++)
365
10.4M
            {
366
10.4M
                ics->sect_sfb_offset[0][i] = swb_offset_1024_window[sf_index][i];
367
10.4M
                ics->swb_offset[i] = swb_offset_1024_window[sf_index][i];
368
10.4M
            }
369
248k
            ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
370
248k
            ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
371
248k
            ics->swb_offset_max = hDecoder->frameLength;
372
248k
#ifdef LD_DEC
373
248k
        }
374
250k
#endif
375
250k
        return 0;
376
17.9k
    case EIGHT_SHORT_SEQUENCE:
377
17.9k
        ics->num_windows = 8;
378
17.9k
        ics->num_window_groups = 1;
379
17.9k
        ics->window_group_length[ics->num_window_groups-1] = 1;
380
17.9k
        ics->num_swb = num_swb_128_window[sf_index];
381
382
17.9k
        if (ics->max_sfb > ics->num_swb)
383
7
        {
384
7
            return 32;
385
7
        }
386
387
260k
        for (i = 0; i < ics->num_swb; i++)
388
242k
            ics->swb_offset[i] = swb_offset_128_window[sf_index][i];
389
17.9k
        ics->swb_offset[ics->num_swb] = hDecoder->frameLength/8;
390
17.9k
        ics->swb_offset_max = hDecoder->frameLength/8;
391
392
143k
        for (i = 0; i < ics->num_windows-1; i++) {
393
125k
            if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
394
103k
            {
395
103k
                ics->num_window_groups += 1;
396
103k
                ics->window_group_length[ics->num_window_groups-1] = 1;
397
103k
            } else {
398
22.3k
                ics->window_group_length[ics->num_window_groups-1] += 1;
399
22.3k
            }
400
125k
        }
401
402
        /* preparation of sect_sfb_offset for short blocks */
403
139k
        for (g = 0; g < ics->num_window_groups; g++)
404
121k
        {
405
121k
            uint16_t width;
406
121k
            uint8_t sect_sfb = 0;
407
121k
            uint16_t offset = 0;
408
409
1.75M
            for (i = 0; i < ics->num_swb; i++)
410
1.63M
            {
411
1.63M
                if (i+1 == ics->num_swb)
412
121k
                {
413
121k
                    width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
414
1.50M
                } else {
415
1.50M
                    width = swb_offset_128_window[sf_index][i+1] -
416
1.50M
                        swb_offset_128_window[sf_index][i];
417
1.50M
                }
418
1.63M
                width *= ics->window_group_length[g];
419
1.63M
                ics->sect_sfb_offset[g][sect_sfb++] = offset;
420
1.63M
                offset += width;
421
1.63M
            }
422
121k
            ics->sect_sfb_offset[g][sect_sfb] = offset;
423
121k
        }
424
17.9k
        return 0;
425
0
    default:
426
0
        return 32;
427
268k
    }
428
268k
}
429
430
/* iquant() * output = sign(input)*abs(input)^(4/3) */
431
static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
432
281M
{
433
281M
#ifdef FIXED_POINT
434
/* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
435
/* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
436
 * defined a 1026 value table and interpolation will be used
437
 */
438
281M
#ifndef BIG_IQ_TABLE
439
281M
    static const real_t errcorr[] = {
440
281M
        REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
441
281M
        REAL_CONST(4.0/8.0),  REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
442
281M
        REAL_CONST(0)
443
281M
    };
444
281M
    real_t x1, x2;
445
281M
#endif
446
281M
    int16_t sgn = 1;
447
    /* compute the magnitude in int: -q is evaluated as int and so does not
448
       wrap for q == -32768 the way an int16_t negation would, which keeps the
449
       comparison against IQ_TABLE_SIZE in range like the floating-point path */
450
281M
    int aq = q;
451
452
281M
    if (aq < 0)
453
91.7k
    {
454
91.7k
        aq = -aq;
455
91.7k
        sgn = -1;
456
91.7k
    }
457
458
281M
    if (aq < IQ_TABLE_SIZE)
459
281M
    {
460
//#define IQUANT_PRINT
461
#ifdef IQUANT_PRINT
462
        //printf("0x%.8X\n", sgn * tab[aq]);
463
        printf("%d\n", sgn * tab[aq]);
464
#endif
465
281M
        return sgn * tab[aq];
466
281M
    }
467
468
2.48k
#ifndef BIG_IQ_TABLE
469
2.48k
    if (aq >= 8192)
470
346
    {
471
346
        *error = 17;
472
346
        return 0;
473
346
    }
474
475
    /* linear interpolation */
476
2.13k
    x1 = tab[aq>>3];
477
2.13k
    x2 = tab[(aq>>3) + 1];
478
2.13k
    return sgn * 16 * (MUL_R(errcorr[aq&7],(x2-x1)) + x1);
479
#else
480
    *error = 17;
481
    return 0;
482
#endif
483
484
#else
485
    if (q < 0)
486
    {
487
        /* tab contains a value for all possible q [0,8192] */
488
        if (-q < IQ_TABLE_SIZE)
489
            return -tab[-q];
490
491
        *error = 17;
492
        return 0;
493
    } else {
494
        /* tab contains a value for all possible q [0,8192] */
495
        if (q < IQ_TABLE_SIZE)
496
            return tab[q];
497
498
        *error = 17;
499
        return 0;
500
    }
501
#endif
502
2.48k
}
503
504
#ifndef FIXED_POINT
505
ALIGN static const real_t pow2sf_tab[] = {
506
    2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
507
    2.384185791015625E-007, 4.76837158203125E-007, 9.5367431640625E-007,
508
    1.9073486328125E-006, 3.814697265625E-006, 7.62939453125E-006,
509
    1.52587890625E-005, 3.0517578125E-005, 6.103515625E-005,
510
    0.0001220703125, 0.000244140625, 0.00048828125,
511
    0.0009765625, 0.001953125, 0.00390625,
512
    0.0078125, 0.015625, 0.03125,
513
    0.0625, 0.125, 0.25,
514
    0.5, 1.0, 2.0,
515
    4.0, 8.0, 16.0, 32.0,
516
    64.0, 128.0, 256.0,
517
    512.0, 1024.0, 2048.0,
518
    4096.0, 8192.0, 16384.0,
519
    32768.0, 65536.0, 131072.0,
520
    262144.0, 524288.0, 1048576.0,
521
    2097152.0, 4194304.0, 8388608.0,
522
    16777216.0, 33554432.0, 67108864.0,
523
    134217728.0, 268435456.0, 536870912.0,
524
    1073741824.0, 2147483648.0, 4294967296.0,
525
    8589934592.0, 17179869184.0, 34359738368.0,
526
    68719476736.0, 137438953472.0, 274877906944.0
527
};
528
#endif
529
530
/* quant_to_spec: perform dequantisation and scaling
531
 * and in case of short block it also does the deinterleaving
532
 */
533
/*
534
  For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
535
  window_group_length[0] = 1) the spectral data is in ascending spectral
536
  order.
537
  For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
538
  grouping in the following manner:
539
  - Groups are ordered sequentially
540
  - Within a group, a scalefactor band consists of the spectral data of all
541
    grouped SHORT_WINDOWs for the associated scalefactor window band. To
542
    clarify via example, the length of a group is in the range of one to eight
543
    SHORT_WINDOWs.
544
  - If there are eight groups each with length one (num_window_groups = 8,
545
    window_group_length[0..7] = 1), the result is a sequence of eight spectra,
546
    each in ascending spectral order.
547
  - If there is only one group with length eight (num_window_groups = 1,
548
    window_group_length[0] = 8), the result is that spectral data of all eight
549
    SHORT_WINDOWs is interleaved by scalefactor window bands.
550
  - Within a scalefactor window band, the coefficients are in ascending
551
    spectral order.
552
*/
553
static uint8_t quant_to_spec(NeAACDecStruct *hDecoder,
554
                             ic_stream *ics, int16_t *quant_data,
555
                             real_t *spec_data, uint16_t frame_len)
556
278k
{
557
278k
    ALIGN static const real_t pow2_table[] =
558
278k
    {
559
278k
        COEF_CONST(1.0),
560
278k
        COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
561
278k
        COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
562
278k
        COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
563
278k
    };
564
278k
    const real_t *tab = iq_table;
565
566
278k
    uint8_t g, sfb, win;
567
278k
    uint16_t width, bin, k, gindex;
568
278k
    uint8_t error = 0; /* Init error flag */
569
#ifndef FIXED_POINT
570
    real_t scf;
571
#else
572
278k
    int32_t sat_shift_mask = 0;
573
278k
#endif
574
575
278k
    k = 0;
576
278k
    gindex = 0;
577
578
    /* In this case quant_to_spec is no-op and spec_data remains undefined.
579
     * Without peeking into AAC specification, there is no strong evidence if
580
     * such streams are invalid -> just calm down MSAN. */
581
278k
    if (ics->num_swb == 0)
582
448
        memset(spec_data, 0, frame_len * sizeof(real_t));
583
584
664k
    for (g = 0; g < ics->num_window_groups; g++)
585
385k
    {
586
385k
        uint16_t j = 0;
587
385k
        uint16_t gincrease = 0;
588
385k
        uint16_t win_inc = ics->swb_offset[ics->num_swb];
589
590
12.9M
        for (sfb = 0; sfb < ics->num_swb; sfb++)
591
12.5M
        {
592
12.5M
            int32_t exp, frac;
593
12.5M
            uint16_t wa = gindex + j;
594
12.5M
            int16_t scale_factor = ics->scale_factors[g][sfb];
595
596
12.5M
            width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
597
598
12.5M
#ifdef FIXED_POINT
599
12.5M
            scale_factor -= 100;
600
            /* IMDCT pre-scaling */
601
12.5M
            if (hDecoder->object_type == LD)
602
78.0k
            {
603
78.0k
                scale_factor -= 24 /*9*/;
604
12.4M
            } else {
605
12.4M
                if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
606
1.68M
                    scale_factor -= 16 /*7*/;
607
10.7M
                else
608
10.7M
                    scale_factor -= 28 /*10*/;
609
12.4M
            }
610
12.5M
            if (scale_factor > 120)
611
794
                scale_factor = 120;  /* => exp <= 30 */
612
#else
613
            (void)hDecoder;
614
#endif
615
616
            /* scale_factor for IS or PNS, has different meaning; fill with almost zeroes */
617
12.5M
            if (is_intensity(ics, g, sfb) || is_noise(ics, g, sfb))
618
47.1k
            {
619
47.1k
                scale_factor = 0;
620
47.1k
            }
621
622
            /* scale_factor must be between 0 and 255 */
623
12.5M
            exp = (scale_factor /* - 100 */) >> 2;
624
            /* frac must always be > 0 */
625
12.5M
            frac = (scale_factor /* - 100 */) & 3;
626
627
#ifndef FIXED_POINT
628
            scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
629
#else
630
12.5M
            if (exp > 0)
631
23.9k
                sat_shift_mask = SAT_SHIFT_MASK(exp);
632
12.5M
#endif
633
634
25.4M
            for (win = 0; win < ics->window_group_length[g]; win++)
635
12.8M
            {
636
83.2M
                for (bin = 0; bin < width; bin += 4)
637
70.3M
                {
638
70.3M
                    uint16_t wb = wa + bin;
639
#ifndef FIXED_POINT
640
                    spec_data[wb+0] = iquant(quant_data[k+0], tab, &error) * scf;
641
                    spec_data[wb+1] = iquant(quant_data[k+1], tab, &error) * scf;
642
                    spec_data[wb+2] = iquant(quant_data[k+2], tab, &error) * scf;
643
                    spec_data[wb+3] = iquant(quant_data[k+3], tab, &error) * scf;
644
#else
645
70.3M
                    real_t iq0 = iquant(quant_data[k+0], tab, &error);
646
70.3M
                    real_t iq1 = iquant(quant_data[k+1], tab, &error);
647
70.3M
                    real_t iq2 = iquant(quant_data[k+2], tab, &error);
648
70.3M
                    real_t iq3 = iquant(quant_data[k+3], tab, &error);
649
650
70.3M
                    if (exp == -32)
651
65.0M
                    {
652
65.0M
                        spec_data[wb+0] = 0;
653
65.0M
                        spec_data[wb+1] = 0;
654
65.0M
                        spec_data[wb+2] = 0;
655
65.0M
                        spec_data[wb+3] = 0;
656
65.0M
                    } else if (exp <= 0) {
657
5.15M
                        spec_data[wb+0] = iq0 >> -exp;
658
5.15M
                        spec_data[wb+1] = iq1 >> -exp;
659
5.15M
                        spec_data[wb+2] = iq2 >> -exp;
660
5.15M
                        spec_data[wb+3] = iq3 >> -exp;
661
5.15M
                    } else { /* exp > 0 */
662
124k
                        spec_data[wb+0] = SAT_SHIFT(iq0, exp, sat_shift_mask);
663
124k
                        spec_data[wb+1] = SAT_SHIFT(iq1, exp, sat_shift_mask);
664
124k
                        spec_data[wb+2] = SAT_SHIFT(iq2, exp, sat_shift_mask);
665
124k
                        spec_data[wb+3] = SAT_SHIFT(iq3, exp, sat_shift_mask);
666
124k
                    }
667
70.3M
                    if (frac != 0)
668
199k
                    {
669
199k
                        spec_data[wb+0] = MUL_C(spec_data[wb+0],pow2_table[frac]);
670
199k
                        spec_data[wb+1] = MUL_C(spec_data[wb+1],pow2_table[frac]);
671
199k
                        spec_data[wb+2] = MUL_C(spec_data[wb+2],pow2_table[frac]);
672
199k
                        spec_data[wb+3] = MUL_C(spec_data[wb+3],pow2_table[frac]);
673
199k
                    }
674
675
//#define SCFS_PRINT
676
#ifdef SCFS_PRINT
677
                    printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
678
                    printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
679
                    printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
680
                    printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
681
                    //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
682
                    //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
683
                    //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
684
                    //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
685
#endif
686
70.3M
#endif
687
688
70.3M
                    gincrease += 4;
689
70.3M
                    k += 4;
690
70.3M
                }
691
12.8M
                wa += win_inc;
692
12.8M
            }
693
12.5M
            j += width;
694
12.5M
        }
695
385k
        gindex += gincrease;
696
385k
    }
697
698
278k
    return error;
699
278k
}
700
701
static uint8_t allocate_single_channel(NeAACDecStruct *hDecoder, uint8_t channel,
702
                                       uint8_t output_channels)
703
215k
{
704
215k
    int mul = 1;
705
706
#ifdef MAIN_DEC
707
    /* MAIN object type prediction */
708
    if (hDecoder->object_type == MAIN)
709
    {
710
        /* allocate the state only when needed */
711
        if (hDecoder->pred_stat[channel] != NULL)
712
        {
713
            faad_free(hDecoder->pred_stat[channel]);
714
            hDecoder->pred_stat[channel] = NULL;
715
        }
716
717
        hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
718
        reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
719
    }
720
#endif
721
722
215k
#ifdef LTP_DEC
723
215k
    if (is_ltp_ot(hDecoder->object_type))
724
133k
    {
725
        /* allocate the state only when needed */
726
133k
        if (hDecoder->lt_pred_stat[channel] != NULL)
727
625
        {
728
625
            faad_free(hDecoder->lt_pred_stat[channel]);
729
625
            hDecoder->lt_pred_stat[channel] = NULL;
730
625
        }
731
732
133k
        hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
733
133k
        memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
734
133k
    }
735
215k
#endif
736
737
215k
    if (hDecoder->time_out[channel] != NULL)
738
4.17k
    {
739
4.17k
        faad_free(hDecoder->time_out[channel]);
740
4.17k
        hDecoder->time_out[channel] = NULL;
741
4.17k
    }
742
743
215k
    {
744
215k
        mul = 1;
745
215k
#ifdef SBR_DEC
746
215k
        hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
747
215k
        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
748
163k
        {
749
            /* SBR requires 2 times as much output data */
750
163k
            mul = 2;
751
163k
            hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
752
163k
        }
753
215k
#endif
754
215k
        hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
755
215k
        memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
756
215k
    }
757
758
215k
#if (defined(PS_DEC) || defined(DRM_PS))
759
215k
    if (output_channels == 2)
760
4.80k
    {
761
4.80k
        if (hDecoder->time_out[channel+1] != NULL)
762
1.41k
        {
763
1.41k
            faad_free(hDecoder->time_out[channel+1]);
764
1.41k
            hDecoder->time_out[channel+1] = NULL;
765
1.41k
        }
766
767
4.80k
        hDecoder->time_out[channel+1] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
768
4.80k
        memset(hDecoder->time_out[channel+1], 0, mul*hDecoder->frameLength*sizeof(real_t));
769
4.80k
    }
770
215k
#endif
771
772
215k
    if (hDecoder->fb_intermed[channel] != NULL)
773
3.50k
    {
774
3.50k
        faad_free(hDecoder->fb_intermed[channel]);
775
3.50k
        hDecoder->fb_intermed[channel] = NULL;
776
3.50k
    }
777
778
215k
    hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
779
215k
    memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
780
781
#ifdef SSR_DEC
782
    if (hDecoder->object_type == SSR)
783
    {
784
        if (hDecoder->ssr_overlap[channel] == NULL)
785
        {
786
            hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
787
            memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
788
        }
789
        if (hDecoder->prev_fmd[channel] == NULL)
790
        {
791
            uint16_t k;
792
            hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
793
            for (k = 0; k < 2*hDecoder->frameLength; k++)
794
                hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
795
        }
796
    }
797
#endif
798
799
215k
    return 0;
800
215k
}
801
802
static uint8_t allocate_channel_pair(NeAACDecStruct *hDecoder,
803
                                     uint8_t channel, uint8_t paired_channel)
804
18.6k
{
805
18.6k
    int mul = 1;
806
807
#ifdef MAIN_DEC
808
    /* MAIN object type prediction */
809
    if (hDecoder->object_type == MAIN)
810
    {
811
        /* allocate the state only when needed */
812
        if (hDecoder->pred_stat[channel] == NULL)
813
        {
814
            hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
815
            reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
816
        }
817
        if (hDecoder->pred_stat[paired_channel] == NULL)
818
        {
819
            hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
820
            reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength);
821
        }
822
    }
823
#endif
824
825
18.6k
#ifdef LTP_DEC
826
18.6k
    if (is_ltp_ot(hDecoder->object_type))
827
8.02k
    {
828
        /* allocate the state only when needed */
829
8.02k
        if (hDecoder->lt_pred_stat[channel] == NULL)
830
7.99k
        {
831
7.99k
            hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
832
7.99k
            memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
833
7.99k
        }
834
8.02k
        if (hDecoder->lt_pred_stat[paired_channel] == NULL)
835
7.99k
        {
836
7.99k
            hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
837
7.99k
            memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
838
7.99k
        }
839
8.02k
    }
840
18.6k
#endif
841
842
18.6k
    {
843
18.6k
        mul = 1;
844
18.6k
#ifdef SBR_DEC
845
18.6k
        hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
846
18.6k
        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
847
15.6k
        {
848
            /* SBR requires 2 times as much output data */
849
15.6k
            mul = 2;
850
15.6k
            hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
851
15.6k
        }
852
18.6k
#endif
853
18.6k
    }
854
18.6k
    if (hDecoder->time_out[channel] != NULL)
855
39
    {
856
39
        faad_free(hDecoder->time_out[channel]);
857
39
        hDecoder->time_out[channel] = NULL;
858
39
    }
859
18.6k
    if (hDecoder->time_out[paired_channel] != NULL)
860
38
    {
861
38
        faad_free(hDecoder->time_out[paired_channel]);
862
38
        hDecoder->time_out[paired_channel] = NULL;
863
38
    }
864
18.6k
    hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
865
18.6k
    memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
866
18.6k
    hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
867
18.6k
    memset(hDecoder->time_out[paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
868
869
18.6k
    if (hDecoder->fb_intermed[channel] != NULL)
870
39
    {
871
39
        faad_free(hDecoder->fb_intermed[channel]);
872
39
        hDecoder->fb_intermed[channel] = NULL;
873
39
    }
874
18.6k
    if (hDecoder->fb_intermed[paired_channel] != NULL)
875
38
    {
876
38
        faad_free(hDecoder->fb_intermed[paired_channel]);
877
38
        hDecoder->fb_intermed[paired_channel] = NULL;
878
38
    }
879
18.6k
    hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
880
18.6k
    memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
881
18.6k
    hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
882
18.6k
    memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
883
884
#ifdef SSR_DEC
885
    if (hDecoder->object_type == SSR)
886
    {
887
        if (hDecoder->ssr_overlap[cpe->channel] == NULL)
888
        {
889
            hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
890
            memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
891
        }
892
        if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
893
        {
894
            hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
895
            memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
896
        }
897
        if (hDecoder->prev_fmd[cpe->channel] == NULL)
898
        {
899
            uint16_t k;
900
            hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
901
            for (k = 0; k < 2*hDecoder->frameLength; k++)
902
                hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
903
        }
904
        if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
905
        {
906
            uint16_t k;
907
            hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
908
            for (k = 0; k < 2*hDecoder->frameLength; k++)
909
                hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
910
        }
911
    }
912
#endif
913
914
18.6k
    return 0;
915
18.6k
}
916
917
uint8_t reconstruct_single_channel(NeAACDecStruct *hDecoder, ic_stream *ics,
918
                                   element *sce, int16_t *spec_data)
919
233k
{
920
233k
    uint8_t retval;
921
233k
    uint8_t output_channels;
922
233k
    ALIGN real_t spec_coef[1024];
923
924
#ifdef PROFILE
925
    int64_t count = faad_get_ts();
926
#endif
927
928
929
    /* always allocate 2 channels, PS can always "suddenly" turn up */
930
#if ( (defined(DRM) && defined(DRM_PS)) )
931
    output_channels = 2;
932
#elif defined(PS_DEC)
933
233k
    if (hDecoder->ps_used[hDecoder->fr_ch_ele])
934
5.88k
        output_channels = 2;
935
228k
    else
936
228k
        output_channels = 1;
937
#else
938
    output_channels = 1;
939
#endif
940
941
233k
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
942
210k
    {
943
        /* element_output_channels not set yet */
944
210k
        hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
945
210k
    } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
946
        /* element inconsistency */
947
948
        /* this only happens if PS is actually found but not in the first frame
949
         * this means that there is only 1 bitstream element!
950
         */
951
952
        /* The simplest way to fix the accounting,
953
         * is to reallocate this and all the following channels.
954
         */
955
652
        memset(&hDecoder->element_alloced[hDecoder->fr_ch_ele], 0,
956
652
            sizeof(uint8_t) * (MAX_SYNTAX_ELEMENTS - hDecoder->fr_ch_ele));
957
958
652
        hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
959
960
        //return 21;
961
652
    }
962
963
233k
    if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
964
215k
    {
965
215k
        retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
966
215k
        if (retval > 0)
967
0
            return retval;
968
969
215k
        hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
970
215k
    }
971
972
    /* sanity check, CVE-2018-20199, CVE-2018-20360 */
973
233k
    if(!hDecoder->time_out[sce->channel])
974
0
        return 15;
975
233k
    if(output_channels > 1 && !hDecoder->time_out[sce->channel+1])
976
0
        return 15;
977
233k
    if(!hDecoder->fb_intermed[sce->channel])
978
0
        return 15;
979
980
    /* dequantisation and scaling */
981
233k
    retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
982
233k
    if (retval > 0)
983
42
        return retval;
984
985
#ifdef PROFILE
986
    count = faad_get_ts() - count;
987
    hDecoder->requant_cycles += count;
988
#endif
989
990
991
    /* pns decoding */
992
233k
    pns_decode(ics, NULL, spec_coef, NULL, hDecoder->frameLength, 0, hDecoder->object_type,
993
233k
        &(hDecoder->__r1), &(hDecoder->__r2));
994
995
#ifdef MAIN_DEC
996
    /* MAIN object type prediction */
997
    if (hDecoder->object_type == MAIN)
998
    {
999
    if (!hDecoder->pred_stat[sce->channel])
1000
      return 33;
1001
1002
        /* intra channel prediction */
1003
        ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
1004
            hDecoder->sf_index);
1005
1006
        /* In addition, for scalefactor bands coded by perceptual
1007
           noise substitution the predictors belonging to the
1008
           corresponding spectral coefficients are reset.
1009
        */
1010
        pns_reset_pred_state(ics, hDecoder->pred_stat[sce->channel]);
1011
    }
1012
#endif
1013
1014
233k
#ifdef LTP_DEC
1015
233k
    if (is_ltp_ot(hDecoder->object_type))
1016
142k
    {
1017
142k
#ifdef LD_DEC
1018
142k
        if (hDecoder->object_type == LD)
1019
618
        {
1020
618
            if (ics->ltp.data_present)
1021
66
            {
1022
66
                if (ics->ltp.lag_update)
1023
26
                    hDecoder->ltp_lag[sce->channel] = ics->ltp.lag;
1024
66
            }
1025
618
            ics->ltp.lag = hDecoder->ltp_lag[sce->channel];
1026
618
        }
1027
142k
#endif
1028
1029
        /* long term prediction */
1030
142k
        lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
1031
142k
            ics->window_shape, hDecoder->window_shape_prev[sce->channel],
1032
142k
            hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1033
142k
    }
1034
233k
#endif
1035
1036
    /* tns decoding */
1037
233k
    tns_decode_frame(ics, &(ics->tns), hDecoder->sf_index, hDecoder->object_type,
1038
233k
        spec_coef, hDecoder->frameLength);
1039
1040
    /* drc decoding */
1041
233k
#ifdef APPLY_DRC
1042
233k
    if (hDecoder->drc->present)
1043
14.3k
    {
1044
14.3k
        if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
1045
12.6k
            drc_decode(hDecoder->drc, spec_coef);
1046
14.3k
    }
1047
233k
#endif
1048
    /* filter bank */
1049
#ifdef SSR_DEC
1050
    if (hDecoder->object_type != SSR)
1051
    {
1052
#endif
1053
233k
        ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
1054
233k
            hDecoder->window_shape_prev[sce->channel], spec_coef,
1055
233k
            hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel],
1056
233k
            hDecoder->object_type, hDecoder->frameLength);
1057
#ifdef SSR_DEC
1058
    } else {
1059
        ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
1060
            hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
1061
            hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
1062
            hDecoder->frameLength);
1063
    }
1064
#endif
1065
1066
    /* save window shape for next frame */
1067
233k
    hDecoder->window_shape_prev[sce->channel] = ics->window_shape;
1068
1069
233k
#ifdef LTP_DEC
1070
233k
    if (is_ltp_ot(hDecoder->object_type))
1071
142k
    {
1072
142k
        lt_update_state(hDecoder->lt_pred_stat[sce->channel], hDecoder->time_out[sce->channel],
1073
142k
            hDecoder->fb_intermed[sce->channel], hDecoder->frameLength, hDecoder->object_type);
1074
142k
    }
1075
233k
#endif
1076
1077
233k
#ifdef SBR_DEC
1078
233k
    if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1079
179k
        && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1080
179k
    {
1081
179k
        int ele = hDecoder->fr_ch_ele;
1082
179k
        int ch = sce->channel;
1083
1084
        /* following case can happen when forceUpSampling == 1 */
1085
179k
        if (hDecoder->sbr[ele] == NULL)
1086
147k
        {
1087
147k
            hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1088
147k
                hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1089
147k
                hDecoder->downSampledSBR
1090
#ifdef DRM
1091
                , 0
1092
#endif
1093
147k
                );
1094
147k
        }
1095
179k
        if (!hDecoder->sbr[ele])
1096
69
            return 19;
1097
1098
179k
        if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1099
14.1k
            hDecoder->sbr[ele]->maxAACLine = 8*min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1100
165k
        else
1101
165k
            hDecoder->sbr[ele]->maxAACLine = min(sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)], sce->ics1.swb_offset_max);
1102
1103
        /* check if any of the PS tools is used */
1104
179k
#if (defined(PS_DEC) || defined(DRM_PS))
1105
179k
        if (hDecoder->ps_used[ele] == 0)
1106
173k
        {
1107
173k
#endif
1108
173k
            retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
1109
173k
                hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1110
173k
#if (defined(PS_DEC) || defined(DRM_PS))
1111
173k
        } else {
1112
5.88k
            retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
1113
5.88k
                hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
1114
5.88k
                hDecoder->downSampledSBR);
1115
5.88k
        }
1116
179k
#endif
1117
179k
        if (retval > 0)
1118
31
            return retval;
1119
179k
    } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1120
6
        && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1121
6
    {
1122
6
        return 23;
1123
6
    }
1124
233k
#endif
1125
1126
    /* copy L to R when no PS is used */
1127
233k
#if (defined(PS_DEC) || defined(DRM_PS))
1128
233k
    if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) &&
1129
227k
        (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2))
1130
0
    {
1131
0
        int ele = hDecoder->fr_ch_ele;
1132
0
        int ch = sce->channel;
1133
0
        int frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
1134
0
        frame_size *= hDecoder->frameLength*sizeof(real_t);
1135
1136
0
        memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
1137
0
    }
1138
233k
#endif
1139
1140
233k
    return 0;
1141
233k
}
1142
1143
uint8_t reconstruct_channel_pair(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2,
1144
                                 element *cpe, int16_t *spec_data1, int16_t *spec_data2)
1145
22.3k
{
1146
22.3k
    uint8_t retval;
1147
22.3k
    ALIGN real_t spec_coef1[1024];
1148
22.3k
    ALIGN real_t spec_coef2[1024];
1149
1150
#ifdef PROFILE
1151
    int64_t count = faad_get_ts();
1152
#endif
1153
22.3k
    if (hDecoder->element_alloced[hDecoder->fr_ch_ele] != 2)
1154
18.6k
    {
1155
18.6k
        retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
1156
18.6k
        if (retval > 0)
1157
0
            return retval;
1158
1159
18.6k
        hDecoder->element_alloced[hDecoder->fr_ch_ele] = 2;
1160
18.6k
    }
1161
1162
    /* sanity check, CVE-2018-20199, CVE-2018-20360 */
1163
22.3k
    if(!hDecoder->time_out[cpe->channel] || !hDecoder->time_out[cpe->paired_channel])
1164
0
        return 15;
1165
22.3k
    if(!hDecoder->fb_intermed[cpe->channel] || !hDecoder->fb_intermed[cpe->paired_channel])
1166
0
        return 15;
1167
1168
    /* dequantisation and scaling */
1169
22.3k
    retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
1170
22.3k
    if (retval > 0)
1171
15
        return retval;
1172
22.3k
    retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
1173
22.3k
    if (retval > 0)
1174
3
        return retval;
1175
1176
#ifdef PROFILE
1177
    count = faad_get_ts() - count;
1178
    hDecoder->requant_cycles += count;
1179
#endif
1180
1181
    /* pns decoding */
1182
22.3k
    if (ics1->ms_mask_present)
1183
5.65k
    {
1184
5.65k
        pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 1, hDecoder->object_type,
1185
5.65k
            &(hDecoder->__r1), &(hDecoder->__r2));
1186
16.7k
    } else {
1187
16.7k
        pns_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength, 0, hDecoder->object_type,
1188
16.7k
            &(hDecoder->__r1), &(hDecoder->__r2));
1189
16.7k
    }
1190
1191
    /* mid/side decoding */
1192
22.3k
    ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1193
1194
#if 0
1195
    {
1196
        int i;
1197
        for (i = 0; i < 1024; i++)
1198
        {
1199
            //printf("%d\n", spec_coef1[i]);
1200
            printf("0x%.8X\n", spec_coef1[i]);
1201
        }
1202
        for (i = 0; i < 1024; i++)
1203
        {
1204
            //printf("%d\n", spec_coef2[i]);
1205
            printf("0x%.8X\n", spec_coef2[i]);
1206
        }
1207
    }
1208
#endif
1209
1210
    /* intensity stereo decoding */
1211
22.3k
    is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1212
1213
#if 0
1214
    {
1215
        int i;
1216
        for (i = 0; i < 1024; i++)
1217
        {
1218
            printf("%d\n", spec_coef1[i]);
1219
            //printf("0x%.8X\n", spec_coef1[i]);
1220
        }
1221
        for (i = 0; i < 1024; i++)
1222
        {
1223
            printf("%d\n", spec_coef2[i]);
1224
            //printf("0x%.8X\n", spec_coef2[i]);
1225
        }
1226
    }
1227
#endif
1228
1229
#ifdef MAIN_DEC
1230
    /* MAIN object type prediction */
1231
    if (hDecoder->object_type == MAIN)
1232
    {
1233
        /* intra channel prediction */
1234
        ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
1235
            hDecoder->sf_index);
1236
        ic_prediction(ics2, spec_coef2, hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength,
1237
            hDecoder->sf_index);
1238
1239
        /* In addition, for scalefactor bands coded by perceptual
1240
           noise substitution the predictors belonging to the
1241
           corresponding spectral coefficients are reset.
1242
        */
1243
        pns_reset_pred_state(ics1, hDecoder->pred_stat[cpe->channel]);
1244
        pns_reset_pred_state(ics2, hDecoder->pred_stat[cpe->paired_channel]);
1245
    }
1246
#endif
1247
1248
22.3k
#ifdef LTP_DEC
1249
22.3k
    if (is_ltp_ot(hDecoder->object_type))
1250
9.81k
    {
1251
9.81k
        ltp_info *ltp1 = &(ics1->ltp);
1252
9.81k
        ltp_info *ltp2 = (cpe->common_window) ? &(ics2->ltp2) : &(ics2->ltp);
1253
9.81k
#ifdef LD_DEC
1254
9.81k
        if (hDecoder->object_type == LD)
1255
1.02k
        {
1256
1.02k
            if (ltp1->data_present)
1257
90
            {
1258
90
                if (ltp1->lag_update)
1259
30
                    hDecoder->ltp_lag[cpe->channel] = ltp1->lag;
1260
90
            }
1261
1.02k
            ltp1->lag = hDecoder->ltp_lag[cpe->channel];
1262
1.02k
            if (ltp2->data_present)
1263
65
            {
1264
65
                if (ltp2->lag_update)
1265
37
                    hDecoder->ltp_lag[cpe->paired_channel] = ltp2->lag;
1266
65
            }
1267
1.02k
            ltp2->lag = hDecoder->ltp_lag[cpe->paired_channel];
1268
1.02k
        }
1269
9.81k
#endif
1270
1271
        /* long term prediction */
1272
9.81k
        lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
1273
9.81k
            ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
1274
9.81k
            hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1275
9.81k
        lt_prediction(ics2, ltp2, spec_coef2, hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->fb,
1276
9.81k
            ics2->window_shape, hDecoder->window_shape_prev[cpe->paired_channel],
1277
9.81k
            hDecoder->sf_index, hDecoder->object_type, hDecoder->frameLength);
1278
9.81k
    }
1279
22.3k
#endif
1280
1281
    /* tns decoding */
1282
22.3k
    tns_decode_frame(ics1, &(ics1->tns), hDecoder->sf_index, hDecoder->object_type,
1283
22.3k
        spec_coef1, hDecoder->frameLength);
1284
22.3k
    tns_decode_frame(ics2, &(ics2->tns), hDecoder->sf_index, hDecoder->object_type,
1285
22.3k
        spec_coef2, hDecoder->frameLength);
1286
1287
    /* drc decoding */
1288
22.3k
#if APPLY_DRC
1289
22.3k
    if (hDecoder->drc->present)
1290
864
    {
1291
864
        if (!hDecoder->drc->exclude_mask[cpe->channel] || !hDecoder->drc->excluded_chns_present)
1292
769
            drc_decode(hDecoder->drc, spec_coef1);
1293
864
        if (!hDecoder->drc->exclude_mask[cpe->paired_channel] || !hDecoder->drc->excluded_chns_present)
1294
772
            drc_decode(hDecoder->drc, spec_coef2);
1295
864
    }
1296
22.3k
#endif
1297
    /* filter bank */
1298
#ifdef SSR_DEC
1299
    if (hDecoder->object_type != SSR)
1300
    {
1301
#endif
1302
22.3k
        ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1303
22.3k
            hDecoder->window_shape_prev[cpe->channel], spec_coef1,
1304
22.3k
            hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
1305
22.3k
            hDecoder->object_type, hDecoder->frameLength);
1306
22.3k
        ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1307
22.3k
            hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1308
22.3k
            hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
1309
22.3k
            hDecoder->object_type, hDecoder->frameLength);
1310
#ifdef SSR_DEC
1311
    } else {
1312
        ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1313
            hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
1314
            hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
1315
            hDecoder->prev_fmd[cpe->channel], hDecoder->frameLength);
1316
        ssr_decode(&(ics2->ssr), hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1317
            hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, hDecoder->time_out[cpe->paired_channel],
1318
            hDecoder->ssr_overlap[cpe->paired_channel], hDecoder->ipqf_buffer[cpe->paired_channel],
1319
            hDecoder->prev_fmd[cpe->paired_channel], hDecoder->frameLength);
1320
    }
1321
#endif
1322
1323
    /* save window shape for next frame */
1324
22.3k
    hDecoder->window_shape_prev[cpe->channel] = ics1->window_shape;
1325
22.3k
    hDecoder->window_shape_prev[cpe->paired_channel] = ics2->window_shape;
1326
1327
22.3k
#ifdef LTP_DEC
1328
22.3k
    if (is_ltp_ot(hDecoder->object_type))
1329
9.81k
    {
1330
9.81k
        lt_update_state(hDecoder->lt_pred_stat[cpe->channel], hDecoder->time_out[cpe->channel],
1331
9.81k
            hDecoder->fb_intermed[cpe->channel], hDecoder->frameLength, hDecoder->object_type);
1332
9.81k
        lt_update_state(hDecoder->lt_pred_stat[cpe->paired_channel], hDecoder->time_out[cpe->paired_channel],
1333
9.81k
            hDecoder->fb_intermed[cpe->paired_channel], hDecoder->frameLength, hDecoder->object_type);
1334
9.81k
    }
1335
22.3k
#endif
1336
1337
22.3k
#ifdef SBR_DEC
1338
22.3k
    if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1339
18.8k
        && hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1340
18.8k
    {
1341
18.8k
        int ele = hDecoder->fr_ch_ele;
1342
18.8k
        int ch0 = cpe->channel;
1343
18.8k
        int ch1 = cpe->paired_channel;
1344
1345
        /* following case can happen when forceUpSampling == 1 */
1346
18.8k
        if (hDecoder->sbr[ele] == NULL)
1347
6.93k
        {
1348
6.93k
            hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1349
6.93k
                hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1350
6.93k
                hDecoder->downSampledSBR
1351
#ifdef DRM
1352
                , 0
1353
#endif
1354
6.93k
                );
1355
6.93k
        }
1356
18.8k
        if (!hDecoder->sbr[ele])
1357
45
            return 19;
1358
1359
18.8k
        if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1360
857
            hDecoder->sbr[ele]->maxAACLine = 8*min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1361
17.9k
        else
1362
17.9k
            hDecoder->sbr[ele]->maxAACLine = min(cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)], cpe->ics1.swb_offset_max);
1363
1364
18.8k
        retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1365
18.8k
            hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1366
18.8k
            hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1367
18.8k
        if (retval > 0)
1368
0
            return retval;
1369
18.8k
    } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1370
1
        && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1371
1
    {
1372
1
        return 23;
1373
1
    }
1374
22.3k
#endif
1375
1376
22.3k
    return 0;
1377
22.3k
}