Coverage Report

Created: 2025-07-11 06:39

/proc/self/cwd/libfaad/syntax.c
Line
Count
Source (jump to first uncovered line)
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: syntax.c,v 1.93 2009/01/26 23:51:15 menno Exp $
29
**/
30
31
/*
32
   Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
33
*/
34
35
#include "common.h"
36
#include "structs.h"
37
38
#include <stdlib.h>
39
#include <stdio.h>
40
41
#include "syntax.h"
42
#include "specrec.h"
43
#include "huffman.h"
44
#include "bits.h"
45
#include "pulse.h"
46
#include "analysis.h"
47
#include "drc.h"
48
#ifdef ERROR_RESILIENCE
49
#include "rvlc.h"
50
#endif
51
#ifdef SBR_DEC
52
#include "sbr_syntax.h"
53
#endif
54
#include "mp4.h"
55
56
57
/* static function declarations */
58
static void decode_sce_lfe(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
59
                           uint8_t id_syn_ele);
60
static void decode_cpe(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
61
                       uint8_t id_syn_ele);
62
static uint8_t single_lfe_channel_element(NeAACDecStruct *hDecoder, bitfile *ld,
63
                                          uint8_t channel, uint8_t *tag);
64
static uint8_t channel_pair_element(NeAACDecStruct *hDecoder, bitfile *ld,
65
                                    uint8_t channel, uint8_t *tag);
66
#ifdef COUPLING_DEC
67
static uint8_t coupling_channel_element(NeAACDecStruct *hDecoder, bitfile *ld);
68
#endif
69
static uint16_t data_stream_element(NeAACDecStruct *hDecoder, bitfile *ld);
70
static uint8_t program_config_element(program_config *pce, bitfile *ld);
71
static uint8_t fill_element(NeAACDecStruct *hDecoder, bitfile *ld, drc_info *drc
72
#ifdef SBR_DEC
73
                            ,uint8_t sbr_ele
74
#endif
75
                            );
76
static uint8_t individual_channel_stream(NeAACDecStruct *hDecoder, element *ele,
77
                                         bitfile *ld, ic_stream *ics, uint8_t scal_flag,
78
                                         int16_t *spec_data);
79
static uint8_t ics_info(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld,
80
                        uint8_t common_window);
81
static uint8_t section_data(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld);
82
static uint8_t scale_factor_data(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld);
83
#ifdef SSR_DEC
84
static void gain_control_data(bitfile *ld, ic_stream *ics);
85
#endif
86
static uint8_t spectral_data(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld,
87
                             int16_t *spectral_data);
88
static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld);
89
static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld);
90
#ifdef LTP_DEC
91
static uint8_t ltp_data(NeAACDecStruct *hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
92
#endif
93
static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
94
static void adts_variable_header(adts_header *adts, bitfile *ld);
95
static void adts_error_check(adts_header *adts, bitfile *ld);
96
static uint8_t side_info(NeAACDecStruct *hDecoder, element *ele,
97
                         bitfile *ld, ic_stream *ics, uint8_t scal_flag);
98
#ifdef DRM
99
static int8_t DRM_aac_scalable_main_header(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2,
100
                                           bitfile *ld, uint8_t this_layer_stereo);
101
#else
102
static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count);
103
static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
104
static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
105
#endif
106
107
108
/* Table 4.4.1 */
109
int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
110
                        program_config *pce_out)
111
2.17k
{
112
2.17k
    program_config pce;
113
114
    /* 1024 or 960 */
115
2.17k
    mp4ASC->frameLengthFlag = faad_get1bit(ld
116
2.17k
        DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
117
#ifndef ALLOW_SMALL_FRAMELENGTH
118
    if (mp4ASC->frameLengthFlag == 1)
119
        return -3;
120
#endif
121
122
2.17k
    mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
123
2.17k
        DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
124
2.17k
    if (mp4ASC->dependsOnCoreCoder == 1)
125
574
    {
126
574
        mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
127
574
            DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
128
574
    }
129
130
2.17k
    mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
131
2.17k
    if (mp4ASC->channelsConfiguration == 0)
132
402
    {
133
402
        if (program_config_element(&pce, ld))
134
2
            return -3;
135
        //mp4ASC->channelsConfiguration = pce.channels;
136
137
400
        if (pce_out != NULL)
138
400
            memcpy(pce_out, &pce, sizeof(program_config));
139
140
        /*
141
        if (pce.num_valid_cc_elements)
142
            return -3;
143
        */
144
400
    }
145
146
2.17k
#ifdef ERROR_RESILIENCE
147
2.17k
    if (mp4ASC->extensionFlag == 1)
148
1.47k
    {
149
        /* Error resilience not supported yet */
150
1.47k
        if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
151
1.25k
        {
152
1.25k
            mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
153
1.25k
                DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
154
1.25k
            mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
155
1.25k
                DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
156
1.25k
            mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
157
1.25k
                DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
158
1.25k
        }
159
        /* 1 bit: extensionFlag3 */
160
1.47k
        faad_getbits(ld, 1);
161
1.47k
  }
162
2.17k
#endif
163
164
2.17k
    return 0;
165
2.17k
}
166
167
/* Table 4.4.2 */
168
/* An MPEG-4 Audio decoder is only required to follow the Program
169
   Configuration Element in GASpecificConfig(). The decoder shall ignore
170
   any Program Configuration Elements that may occur in raw data blocks.
171
   PCEs transmitted in raw data blocks cannot be used to convey decoder
172
   configuration information.
173
*/
174
static uint8_t program_config_element(program_config *pce, bitfile *ld)
175
1.99k
{
176
1.99k
    uint8_t i;
177
178
1.99k
    memset(pce, 0, sizeof(program_config));
179
180
1.99k
    pce->channels = 0;
181
182
1.99k
    pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
183
1.99k
        DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
184
185
1.99k
    pce->object_type = (uint8_t)faad_getbits(ld, 2
186
1.99k
        DEBUGVAR(1,11,"program_config_element(): object_type"));
187
1.99k
    pce->sf_index = (uint8_t)faad_getbits(ld, 4
188
1.99k
        DEBUGVAR(1,12,"program_config_element(): sf_index"));
189
1.99k
    pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
190
1.99k
        DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
191
1.99k
    pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
192
1.99k
        DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
193
1.99k
    pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
194
1.99k
        DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
195
1.99k
    pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
196
1.99k
        DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
197
1.99k
    pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
198
1.99k
        DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
199
1.99k
    pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
200
1.99k
        DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
201
202
1.99k
    pce->mono_mixdown_present = faad_get1bit(ld
203
1.99k
        DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
204
1.99k
    if (pce->mono_mixdown_present == 1)
205
458
    {
206
458
        pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
207
458
            DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
208
458
    }
209
210
1.99k
    pce->stereo_mixdown_present = faad_get1bit(ld
211
1.99k
        DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
212
1.99k
    if (pce->stereo_mixdown_present == 1)
213
332
    {
214
332
        pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
215
332
            DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
216
332
    }
217
218
1.99k
    pce->matrix_mixdown_idx_present = faad_get1bit(ld
219
1.99k
        DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
220
1.99k
    if (pce->matrix_mixdown_idx_present == 1)
221
311
    {
222
311
        pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
223
311
            DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
224
311
        pce->pseudo_surround_enable = faad_get1bit(ld
225
311
            DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
226
311
    }
227
228
8.16k
    for (i = 0; i < pce->num_front_channel_elements; i++)
229
6.17k
    {
230
6.17k
        pce->front_element_is_cpe[i] = faad_get1bit(ld
231
6.17k
            DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
232
6.17k
        pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
233
6.17k
            DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
234
235
6.17k
        if (pce->front_element_is_cpe[i] & 1)
236
1.85k
        {
237
1.85k
            pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
238
1.85k
            pce->num_front_channels += 2;
239
1.85k
            pce->channels += 2;
240
4.32k
        } else {
241
4.32k
            pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
242
4.32k
            pce->num_front_channels++;
243
4.32k
            pce->channels++;
244
4.32k
        }
245
6.17k
    }
246
247
7.46k
    for (i = 0; i < pce->num_side_channel_elements; i++)
248
5.47k
    {
249
5.47k
        pce->side_element_is_cpe[i] = faad_get1bit(ld
250
5.47k
            DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
251
5.47k
        pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
252
5.47k
            DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
253
254
5.47k
        if (pce->side_element_is_cpe[i] & 1)
255
1.38k
        {
256
1.38k
            pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
257
1.38k
            pce->num_side_channels += 2;
258
1.38k
            pce->channels += 2;
259
4.08k
        } else {
260
4.08k
            pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
261
4.08k
            pce->num_side_channels++;
262
4.08k
            pce->channels++;
263
4.08k
        }
264
5.47k
    }
265
266
7.86k
    for (i = 0; i < pce->num_back_channel_elements; i++)
267
5.87k
    {
268
5.87k
        pce->back_element_is_cpe[i] = faad_get1bit(ld
269
5.87k
            DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
270
5.87k
        pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
271
5.87k
            DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
272
273
5.87k
        if (pce->back_element_is_cpe[i] & 1)
274
1.47k
        {
275
1.47k
            pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
276
1.47k
            pce->channels += 2;
277
1.47k
            pce->num_back_channels += 2;
278
4.40k
        } else {
279
4.40k
            pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
280
4.40k
            pce->num_back_channels++;
281
4.40k
            pce->channels++;
282
4.40k
        }
283
5.87k
    }
284
285
3.41k
    for (i = 0; i < pce->num_lfe_channel_elements; i++)
286
1.41k
    {
287
1.41k
        pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
288
1.41k
            DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
289
290
1.41k
        pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
291
1.41k
        pce->num_lfe_channels++;
292
1.41k
        pce->channels++;
293
1.41k
    }
294
295
3.93k
    for (i = 0; i < pce->num_assoc_data_elements; i++)
296
1.94k
        pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
297
1.94k
        DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
298
299
6.61k
    for (i = 0; i < pce->num_valid_cc_elements; i++)
300
4.62k
    {
301
4.62k
        pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
302
4.62k
            DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
303
4.62k
        pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
304
4.62k
            DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
305
4.62k
    }
306
307
1.99k
    faad_byte_align(ld);
308
309
1.99k
    pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
310
1.99k
        DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
311
312
20.5k
    for (i = 0; i < pce->comment_field_bytes; i++)
313
18.5k
    {
314
18.5k
        pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
315
18.5k
            DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
316
18.5k
    }
317
1.99k
    pce->comment_field_data[i] = 0;
318
319
1.99k
    if (pce->channels > MAX_CHANNELS)
320
74
        return 22;
321
322
1.91k
    return 0;
323
1.99k
}
324
325
static void decode_sce_lfe(NeAACDecStruct *hDecoder,
326
                           NeAACDecFrameInfo *hInfo, bitfile *ld,
327
                           uint8_t id_syn_ele)
328
107k
{
329
107k
    uint8_t channels = hDecoder->fr_channels;
330
107k
    uint8_t tag = 0;
331
332
    /* One or two channels are used;
333
       exact number will be known after single_lfe_channel_element
334
    */
335
107k
    if (channels+2 > MAX_CHANNELS)
336
3.16k
    {
337
3.16k
        hInfo->error = 12;
338
3.16k
        return;
339
3.16k
    }
340
104k
    if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
341
0
    {
342
0
        hInfo->error = 13;
343
0
        return;
344
0
    }
345
346
    /* for SCE hDecoder->element_output_channels[] is not set here because this
347
       can become 2 when some form of Parametric Stereo coding is used
348
    */
349
350
104k
    if (hDecoder->element_id[hDecoder->fr_ch_ele] != INVALID_ELEMENT_ID &&
351
104k
        hDecoder->element_id[hDecoder->fr_ch_ele] != id_syn_ele)
352
81
    {
353
        /* element inconsistency */
354
81
        hInfo->error = 21;
355
81
        return;
356
81
    }
357
358
    /* save the syntax element id */
359
104k
    hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
360
361
    /* decode the element */
362
104k
    hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
363
364
    /* map output channels position to internal data channels */
365
104k
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
366
103k
    {
367
        /* this might be faulty when pce_set is true */
368
103k
        hDecoder->internal_channel[channels] = channels;
369
103k
        hDecoder->internal_channel[channels+1] = channels+1;
370
103k
    } else {
371
577
        if (hDecoder->pce_set)
372
38
        {
373
38
            if (hDecoder->pce.channels > MAX_CHANNELS)
374
3
            {
375
3
                hInfo->error = 22;
376
3
                return;
377
3
            }
378
35
            hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
379
539
        } else {
380
539
            hDecoder->internal_channel[channels] = channels;
381
539
        }
382
577
    }
383
384
104k
    hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
385
104k
    hDecoder->fr_ch_ele++;
386
104k
}
387
388
static void decode_cpe(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
389
                       uint8_t id_syn_ele)
390
20.8k
{
391
20.8k
    uint8_t channels = hDecoder->fr_channels;
392
20.8k
    uint8_t tag = 0;
393
394
20.8k
    if (channels+2 > MAX_CHANNELS)
395
151
    {
396
151
        hInfo->error = 12;
397
151
        return;
398
151
    }
399
20.7k
    if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
400
0
    {
401
0
        hInfo->error = 13;
402
0
        return;
403
0
    }
404
20.7k
    if (hDecoder->pce_set && (hDecoder->pce.channels > MAX_CHANNELS))
405
3
    {
406
3
        hInfo->error = 22;
407
3
        return;
408
3
    }
409
410
    /* for CPE the number of output channels is always 2 */
411
20.7k
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
412
17.3k
    {
413
        /* element_output_channels not set yet */
414
17.3k
        hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
415
17.3k
    } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
416
        /* element inconsistency */
417
0
        hInfo->error = 21;
418
0
        return;
419
0
    }
420
421
20.7k
    if (hDecoder->element_id[hDecoder->fr_ch_ele] != INVALID_ELEMENT_ID &&
422
20.7k
        hDecoder->element_id[hDecoder->fr_ch_ele] != id_syn_ele)
423
44
    {
424
        /* element inconsistency */
425
44
        hInfo->error = 21;
426
44
        return;
427
44
    }
428
429
    /* save the syntax element id */
430
20.6k
    hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
431
432
    /* decode the element */
433
20.6k
    hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
434
435
    /* map output channel position to internal data channels */
436
20.6k
    if (hDecoder->pce_set)
437
1.29k
    {
438
1.29k
        hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
439
1.29k
        hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
440
19.3k
    } else {
441
19.3k
        hDecoder->internal_channel[channels] = channels;
442
19.3k
        hDecoder->internal_channel[channels+1] = channels+1;
443
19.3k
    }
444
445
20.6k
    hDecoder->fr_channels += 2;
446
20.6k
    hDecoder->fr_ch_ele++;
447
20.6k
}
448
449
void raw_data_block(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo,
450
                    bitfile *ld, program_config *pce, drc_info *drc)
451
7.84k
{
452
7.84k
    uint8_t id_syn_ele;
453
7.84k
    uint8_t ele_this_frame = 0;
454
455
7.84k
    hDecoder->fr_channels = 0;
456
7.84k
    hDecoder->fr_ch_ele = 0;
457
7.84k
    hDecoder->first_syn_ele = 25;
458
7.84k
    hDecoder->has_lfe = 0;
459
460
7.84k
#ifdef ERROR_RESILIENCE
461
7.84k
    if (hDecoder->object_type < ER_OBJECT_START)
462
5.29k
    {
463
5.29k
#endif
464
        /* Table 4.4.3: raw_data_block() */
465
538k
        while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID
466
538k
            DEBUGVAR(1,4,"NeAACDecDecode(): id_syn_ele"))) != ID_END)
467
538k
        {
468
538k
            switch (id_syn_ele) {
469
105k
            case ID_SCE:
470
105k
                ele_this_frame++;
471
105k
                if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
472
105k
                decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
473
105k
                if (hInfo->error > 0)
474
3.71k
                    return;
475
101k
                break;
476
101k
            case ID_CPE:
477
17.3k
                ele_this_frame++;
478
17.3k
                if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
479
17.3k
                decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
480
17.3k
                if (hInfo->error > 0)
481
611
                    return;
482
16.7k
                break;
483
16.7k
            case ID_LFE:
484
79
#ifdef DRM
485
79
                hInfo->error = 32;
486
#else
487
                ele_this_frame++;
488
                hDecoder->has_lfe++;
489
                decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
490
#endif
491
79
                if (hInfo->error > 0)
492
79
                    return;
493
0
                break;
494
131
            case ID_CCE: /* not implemented yet, but skip the bits */
495
131
#ifdef DRM
496
131
                hInfo->error = 32;
497
#else
498
                ele_this_frame++;
499
#ifdef COUPLING_DEC
500
                hInfo->error = coupling_channel_element(hDecoder, ld);
501
#else
502
                hInfo->error = 6;
503
#endif
504
#endif
505
131
                if (hInfo->error > 0)
506
131
                    return;
507
0
                break;
508
413k
            case ID_DSE:
509
413k
                ele_this_frame++;
510
413k
                data_stream_element(hDecoder, ld);
511
413k
                break;
512
326
            case ID_PCE:
513
326
                if (ele_this_frame != 0)
514
84
                {
515
84
                    hInfo->error = 31;
516
84
                    return;
517
84
                }
518
242
                ele_this_frame++;
519
                /* 14496-4: 5.6.4.1.2.1.3: */
520
                /* program_configuration_element()'s in access units shall be ignored */
521
242
                program_config_element(pce, ld);
522
                //if ((hInfo->error = program_config_element(pce, ld)) > 0)
523
                //    return;
524
                //hDecoder->pce_set = 1;
525
242
                break;
526
1.90k
            case ID_FIL:
527
1.90k
                ele_this_frame++;
528
                /* one sbr_info describes a channel_element not a channel! */
529
                /* if we encounter SBR data here: error */
530
                /* SBR data will be read directly in the SCE/LFE/CPE element */
531
1.90k
                if ((hInfo->error = fill_element(hDecoder, ld, drc
532
1.90k
#ifdef SBR_DEC
533
1.90k
                    , INVALID_SBR_ELEMENT
534
1.90k
#endif
535
1.90k
                    )) > 0)
536
104
                    return;
537
1.79k
                break;
538
538k
            }
539
533k
            if (ld->error != 0)
540
0
            {
541
0
                hInfo->error = 32;
542
0
                return;
543
0
            }
544
533k
        }
545
5.29k
#ifdef ERROR_RESILIENCE
546
5.29k
    } else {
547
        /* Table 262: er_raw_data_block() */
548
2.55k
        switch (hDecoder->channelConfiguration)
549
2.55k
        {
550
0
        case 1:
551
0
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
552
0
            if (hInfo->error > 0)
553
0
                return;
554
0
            break;
555
943
        case 2:
556
943
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
557
943
            if (hInfo->error > 0)
558
146
                return;
559
797
            break;
560
797
        case 3:
561
159
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
562
159
            if (hInfo->error > 0)
563
30
                return;
564
129
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
565
129
            if (hInfo->error > 0)
566
7
                return;
567
122
            break;
568
312
        case 4:
569
312
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
570
312
            if (hInfo->error > 0)
571
44
                return;
572
268
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
573
268
            if (hInfo->error > 0)
574
12
                return;
575
256
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
576
256
            if (hInfo->error > 0)
577
4
                return;
578
252
            break;
579
644
        case 5:
580
644
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
581
644
            if (hInfo->error > 0)
582
112
                return;
583
532
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
584
532
            if (hInfo->error > 0)
585
19
                return;
586
513
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
587
513
            if (hInfo->error > 0)
588
8
                return;
589
505
            break;
590
505
        case 6:
591
168
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
592
168
            if (hInfo->error > 0)
593
28
                return;
594
140
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
595
140
            if (hInfo->error > 0)
596
12
                return;
597
128
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
598
128
            if (hInfo->error > 0)
599
4
                return;
600
124
            decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
601
124
            if (hInfo->error > 0)
602
15
                return;
603
109
            break;
604
323
        case 7: /* 8 channels */
605
323
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
606
323
            if (hInfo->error > 0)
607
23
                return;
608
300
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
609
300
            if (hInfo->error > 0)
610
22
                return;
611
278
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
612
278
            if (hInfo->error > 0)
613
4
                return;
614
274
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
615
274
            if (hInfo->error > 0)
616
36
                return;
617
238
            decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
618
238
            if (hInfo->error > 0)
619
22
                return;
620
216
            break;
621
216
        default:
622
5
            hInfo->error = 7;
623
5
            return;
624
2.55k
        }
625
#if 0
626
        cnt = bits_to_decode() / 8;
627
        while (cnt >= 1)
628
        {
629
            cnt -= extension_payload(cnt);
630
        }
631
#endif
632
2.55k
    }
633
2.57k
#endif
634
635
    /* new in corrigendum 14496-3:2002 */
636
2.57k
#ifdef DRM
637
2.57k
    if (hDecoder->object_type != DRM_ER_LC
638
#if 0
639
        && !hDecoder->latm_header_present
640
#endif
641
2.57k
        )
642
2.57k
#endif
643
2.57k
    {
644
2.57k
        faad_byte_align(ld);
645
2.57k
    }
646
647
2.57k
    return;
648
7.84k
}
649
650
/* Table 4.4.4 and */
651
/* Table 4.4.9 */
652
static uint8_t single_lfe_channel_element(NeAACDecStruct *hDecoder, bitfile *ld,
653
                                          uint8_t channel, uint8_t *tag)
654
104k
{
655
104k
    uint8_t retval = 0;
656
104k
    element sce = {0};
657
104k
    ic_stream *ics = &(sce.ics1);
658
104k
    ALIGN int16_t spec_data[1024] = {0};
659
660
104k
    sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
661
104k
        DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
662
663
104k
    *tag = sce.element_instance_tag;
664
104k
    sce.channel = channel;
665
104k
    sce.paired_channel = -1;
666
667
104k
    retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
668
104k
    if (retval > 0)
669
532
        return retval;
670
671
    /* IS not allowed in single channel */
672
103k
    if (ics->is_used)
673
79
        return 32;
674
675
103k
#ifdef SBR_DEC
676
    /* check if next bitstream element is a fill element */
677
    /* if so, read it now so SBR decoding can be done in case of a file with SBR */
678
103k
    if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
679
12.7k
    {
680
12.7k
        faad_flushbits(ld, LEN_SE_ID);
681
682
        /* one sbr_info describes a channel_element not a channel! */
683
12.7k
        if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
684
27
        {
685
27
            return retval;
686
27
        }
687
12.7k
    }
688
103k
#endif
689
690
    /* noiseless coding is done, spectral reconstruction is done now */
691
103k
    retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
692
103k
    if (retval > 0)
693
103
        return retval;
694
695
103k
    return 0;
696
103k
}
697
698
/* Table 4.4.5 */
699
static uint8_t channel_pair_element(NeAACDecStruct *hDecoder, bitfile *ld,
700
                                    uint8_t channels, uint8_t *tag)
701
20.6k
{
702
20.6k
    ALIGN int16_t spec_data1[1024] = {0};
703
20.6k
    ALIGN int16_t spec_data2[1024] = {0};
704
20.6k
    element cpe = {0};
705
20.6k
    ic_stream *ics1 = &(cpe.ics1);
706
20.6k
    ic_stream *ics2 = &(cpe.ics2);
707
20.6k
    uint8_t result;
708
709
20.6k
    cpe.channel        = channels;
710
20.6k
    cpe.paired_channel = channels+1;
711
712
20.6k
    cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
713
20.6k
        DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
714
20.6k
    *tag = cpe.element_instance_tag;
715
716
20.6k
    if ((cpe.common_window = faad_get1bit(ld
717
20.6k
        DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
718
12.1k
    {
719
        /* both channels have common ics information */
720
12.1k
        if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
721
88
            return result;
722
723
12.0k
        ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
724
12.0k
            DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
725
12.0k
        if (ics1->ms_mask_present == 3)
726
26
        {
727
            /* bitstream error */
728
26
            return 32;
729
26
        }
730
12.0k
        if (ics1->ms_mask_present == 1)
731
2.88k
        {
732
2.88k
            uint8_t g, sfb;
733
8.64k
            for (g = 0; g < ics1->num_window_groups; g++)
734
5.76k
            {
735
9.88k
                for (sfb = 0; sfb < ics1->max_sfb; sfb++)
736
4.11k
                {
737
4.11k
                    ics1->ms_used[g][sfb] = faad_get1bit(ld
738
4.11k
                        DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
739
4.11k
                }
740
5.76k
            }
741
2.88k
        }
742
743
12.0k
#ifdef ERROR_RESILIENCE
744
12.0k
        if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
745
57
        {
746
57
            if ((
747
#ifdef LTP_DEC
748
                ics1->ltp.data_present =
749
#endif
750
57
                faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
751
4
            {
752
#ifdef LTP_DEC
753
                if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
754
                {
755
                    return result;
756
                }
757
#else
758
4
                return 26;
759
4
#endif
760
4
            }
761
57
        }
762
12.0k
#endif
763
764
12.0k
        memcpy(ics2, ics1, sizeof(ic_stream));
765
12.0k
    } else {
766
8.54k
        ics1->ms_mask_present = 0;
767
8.54k
    }
768
769
20.5k
    if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
770
20.5k
        0, spec_data1)) > 0)
771
343
    {
772
343
        return result;
773
343
    }
774
775
20.2k
#ifdef ERROR_RESILIENCE
776
20.2k
    if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
777
20.2k
        (ics1->predictor_data_present))
778
35
    {
779
35
        if ((
780
#ifdef LTP_DEC
781
            ics1->ltp2.data_present =
782
#endif
783
35
            faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
784
3
        {
785
#ifdef LTP_DEC
786
            if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
787
            {
788
                return result;
789
            }
790
#else
791
3
            return 26;
792
3
#endif
793
3
        }
794
35
    }
795
20.2k
#endif
796
797
20.2k
    if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
798
20.2k
        0, spec_data2)) > 0)
799
159
    {
800
159
        return result;
801
159
    }
802
803
20.0k
#ifdef SBR_DEC
804
    /* check if next bitstream element is a fill element */
805
    /* if so, read it now so SBR decoding can be done in case of a file with SBR */
806
20.0k
    if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
807
12.2k
    {
808
12.2k
        faad_flushbits(ld, LEN_SE_ID);
809
810
        /* one sbr_info describes a channel_element not a channel! */
811
12.2k
        if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
812
22
        {
813
22
            return result;
814
22
        }
815
12.2k
    }
816
20.0k
#endif
817
818
    /* noiseless coding is done, spectral reconstruction is done now */
819
20.0k
    if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
820
20.0k
        spec_data1, spec_data2)) > 0)
821
38
    {
822
38
        return result;
823
38
    }
824
825
19.9k
    return 0;
826
20.0k
}
827
828
/* Table 4.4.6 */
829
static uint8_t ics_info(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld,
830
                        uint8_t common_window)
831
133k
{
832
133k
    uint8_t retval = 0;
833
133k
    uint8_t ics_reserved_bit;
834
835
133k
    ics_reserved_bit = faad_get1bit(ld
836
133k
        DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
837
133k
    if (ics_reserved_bit != 0)
838
256
        return 32;
839
133k
    ics->window_sequence = (uint8_t)faad_getbits(ld, 2
840
133k
        DEBUGVAR(1,44,"ics_info(): window_sequence"));
841
133k
    ics->window_shape = faad_get1bit(ld
842
133k
        DEBUGVAR(1,45,"ics_info(): window_shape"));
843
844
#ifdef LD_DEC
845
    /* No block switching in LD */
846
    if ((hDecoder->object_type == LD) && (ics->window_sequence != ONLY_LONG_SEQUENCE))
847
        return 32;
848
#endif
849
850
133k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
851
14.7k
    {
852
14.7k
        ics->max_sfb = (uint8_t)faad_getbits(ld, 4
853
14.7k
            DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
854
14.7k
        ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
855
14.7k
            DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
856
118k
    } else {
857
118k
        ics->max_sfb = (uint8_t)faad_getbits(ld, 6
858
118k
            DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
859
118k
    }
860
861
    /* get the grouping information */
862
133k
    if ((retval = window_grouping_info(hDecoder, ics)) > 0)
863
70
        return retval;
864
865
866
    /* should be an error */
867
    /* check the range of max_sfb */
868
133k
    if (ics->max_sfb > ics->num_swb)
869
0
        return 16;
870
871
133k
    if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
872
118k
    {
873
118k
        if ((ics->predictor_data_present = faad_get1bit(ld
874
118k
            DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
875
8.56k
        {
876
8.56k
            if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
877
2.55k
            {
878
2.55k
                uint8_t sfb;
879
2.55k
                uint8_t predictor_reset, predictor_reset_group_number, prediction_used;
880
2.55k
                uint8_t limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
881
882
2.55k
                predictor_reset = faad_get1bit(ld DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"));
883
2.55k
                if (predictor_reset)
884
2.40k
                {
885
2.40k
                    predictor_reset_group_number =
886
2.40k
                        (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
887
2.40k
                } else {
888
150
                    predictor_reset_group_number = 0;
889
150
                }
890
891
14.0k
                for (sfb = 0; sfb < limit; sfb++)
892
11.4k
                {
893
11.4k
                    prediction_used = faad_get1bit(ld DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
894
#ifdef MAIN_DEC
895
                    ics->pred.prediction_used[sfb] = prediction_used;
896
#endif
897
11.4k
                }
898
#ifdef MAIN_DEC
899
                ics->pred.limit = limit;
900
                ics->pred.predictor_reset = predictor_reset;
901
                ics->pred.predictor_reset_group_number = predictor_reset_group_number;
902
#else
903
2.55k
                (void)predictor_reset_group_number;
904
2.55k
                (void)prediction_used;
905
2.55k
#endif
906
2.55k
            }
907
#ifdef LTP_DEC
908
            else { /* Long Term Prediction */
909
                if (hDecoder->object_type < ER_OBJECT_START)
910
                {
911
                    if ((ics->ltp.data_present = faad_get1bit(ld
912
                        DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
913
                    {
914
                        if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
915
                        {
916
                            return retval;
917
                        }
918
                    }
919
                    if (common_window)
920
                    {
921
                        if ((ics->ltp2.data_present = faad_get1bit(ld
922
                            DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
923
                        {
924
                            if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
925
                            {
926
                                return retval;
927
                            }
928
                        }
929
                    }
930
                }
931
#ifdef ERROR_RESILIENCE
932
                if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
933
                {
934
                    if ((ics->ltp.data_present = faad_get1bit(ld
935
                        DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
936
                    {
937
                        if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
938
                        {
939
                            return retval;
940
                        }
941
                    }
942
                }
943
#endif  /* ERROR_RESILIENCE */
944
            }
945
#else  /* LTP_DEC */
946
8.56k
            (void)common_window;
947
8.56k
#endif  /* LTP_DEC */
948
8.56k
        }
949
118k
    }
950
951
133k
    return retval;
952
133k
}
953
954
/* Table 4.4.7 */
955
static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
956
5.81k
{
957
5.81k
    uint8_t i;
958
959
5.81k
    pul->number_pulse = (uint8_t)faad_getbits(ld, 2
960
5.81k
        DEBUGVAR(1,56,"pulse_data(): number_pulse"));
961
5.81k
    pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
962
5.81k
        DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
963
964
    /* check the range of pulse_start_sfb */
965
5.81k
    if (pul->pulse_start_sfb > ics->num_swb)
966
34
        return 16;
967
968
14.1k
    for (i = 0; i < pul->number_pulse+1; i++)
969
8.40k
    {
970
8.40k
        pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
971
8.40k
            DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
972
#if 0
973
        printf("%d\n", pul->pulse_offset[i]);
974
#endif
975
8.40k
        pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
976
8.40k
            DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
977
#if 0
978
        printf("%d\n", pul->pulse_amp[i]);
979
#endif
980
8.40k
    }
981
982
5.78k
    return 0;
983
5.81k
}
984
985
#ifdef COUPLING_DEC
986
/* Table 4.4.8: Currently just for skipping the bits... */
987
static uint8_t coupling_channel_element(NeAACDecStruct *hDecoder, bitfile *ld)
988
{
989
    uint8_t c, result = 0;
990
    uint8_t ind_sw_cce_flag = 0;
991
    uint8_t num_gain_element_lists = 0;
992
    uint8_t num_coupled_elements = 0;
993
994
    element el_empty = {0};
995
    ic_stream ics_empty = {0};
996
    int16_t sh_data[1024];
997
998
    c = faad_getbits(ld, LEN_TAG
999
        DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
1000
1001
    ind_sw_cce_flag = faad_get1bit(ld
1002
        DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
1003
    num_coupled_elements = faad_getbits(ld, 3
1004
        DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
1005
1006
    for (c = 0; c < num_coupled_elements + 1; c++)
1007
    {
1008
        uint8_t cc_target_is_cpe, cc_target_tag_select;
1009
1010
        num_gain_element_lists++;
1011
1012
        cc_target_is_cpe = faad_get1bit(ld
1013
            DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
1014
        cc_target_tag_select = faad_getbits(ld, 4
1015
            DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
1016
1017
        if (cc_target_is_cpe)
1018
        {
1019
            uint8_t cc_l = faad_get1bit(ld
1020
                DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
1021
            uint8_t cc_r = faad_get1bit(ld
1022
                DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
1023
1024
            if (cc_l && cc_r)
1025
                num_gain_element_lists++;
1026
        }
1027
    }
1028
1029
    faad_get1bit(ld
1030
        DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
1031
    faad_get1bit(ld
1032
        DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
1033
    faad_getbits(ld, 2
1034
        DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
1035
1036
    if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
1037
        0, sh_data)) > 0)
1038
    {
1039
        return result;
1040
    }
1041
1042
    /* IS not allowed in single channel */
1043
    if (ics->is_used)
1044
        return 32;
1045
1046
    for (c = 1; c < num_gain_element_lists; c++)
1047
    {
1048
        uint8_t cge;
1049
1050
        if (ind_sw_cce_flag)
1051
        {
1052
            cge = 1;
1053
        } else {
1054
            cge = faad_get1bit(ld
1055
                DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
1056
        }
1057
1058
        if (cge)
1059
        {
1060
            huffman_scale_factor(ld);
1061
        } else {
1062
            uint8_t g, sfb;
1063
1064
            for (g = 0; g < ics_empty.num_window_groups; g++)
1065
            {
1066
                for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
1067
                {
1068
                    if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
1069
                        huffman_scale_factor(ld);
1070
                }
1071
            }
1072
        }
1073
    }
1074
1075
    return 0;
1076
}
1077
#endif
1078
1079
/* Table 4.4.10 */
1080
static uint16_t data_stream_element(NeAACDecStruct *hDecoder, bitfile *ld)
1081
413k
{
1082
413k
    uint8_t byte_aligned;
1083
413k
    uint16_t i, count;
1084
413k
    (void)hDecoder;  /* TODO: remove parameter; rename method; why result is unused? */
1085
1086
413k
    /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
1087
413k
        DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
1088
413k
    byte_aligned = faad_get1bit(ld
1089
413k
        DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
1090
413k
    count = (uint16_t)faad_getbits(ld, 8
1091
413k
        DEBUGVAR(1,62,"data_stream_element(): count"));
1092
413k
    if (count == 255)
1093
1.47k
    {
1094
1.47k
        count += (uint16_t)faad_getbits(ld, 8
1095
1.47k
            DEBUGVAR(1,63,"data_stream_element(): extra count"));
1096
1.47k
    }
1097
413k
    if (byte_aligned)
1098
66.9k
        faad_byte_align(ld);
1099
1100
19.4M
    for (i = 0; i < count; i++)
1101
19.0M
    {
1102
19.0M
        faad_getbits(ld, LEN_BYTE
1103
19.0M
            DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
1104
19.0M
    }
1105
1106
413k
    return count;
1107
413k
}
1108
1109
/* Table 4.4.11 */
1110
static uint8_t fill_element(NeAACDecStruct *hDecoder, bitfile *ld, drc_info *drc
1111
#ifdef SBR_DEC
1112
                            ,uint8_t sbr_ele
1113
#endif
1114
                            )
1115
26.8k
{
1116
26.8k
    uint16_t count;
1117
26.8k
#ifdef SBR_DEC
1118
26.8k
    uint8_t bs_extension_type;
1119
26.8k
#endif
1120
1121
26.8k
    count = (uint16_t)faad_getbits(ld, 4
1122
26.8k
        DEBUGVAR(1,65,"fill_element(): count"));
1123
26.8k
    if (count == 15)
1124
2.97k
    {
1125
2.97k
        count += (uint16_t)faad_getbits(ld, 8
1126
2.97k
            DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
1127
2.97k
    }
1128
1129
26.8k
    if (count > 0)
1130
24.9k
    {
1131
24.9k
#ifdef SBR_DEC
1132
24.9k
        bs_extension_type = (uint8_t)faad_showbits(ld, 4);
1133
1134
24.9k
        if ((bs_extension_type == EXT_SBR_DATA) ||
1135
24.9k
            (bs_extension_type == EXT_SBR_DATA_CRC))
1136
24.7k
        {
1137
24.7k
            if (sbr_ele == INVALID_SBR_ELEMENT)
1138
35
                return 24;
1139
1140
24.7k
            if (!hDecoder->sbr[sbr_ele])
1141
21.2k
            {
1142
21.2k
                hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
1143
21.2k
                    hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index),
1144
21.2k
                    hDecoder->downSampledSBR
1145
21.2k
#ifdef DRM
1146
21.2k
                    , 0
1147
21.2k
#endif
1148
21.2k
                    );
1149
21.2k
            }
1150
24.7k
            if (!hDecoder->sbr[sbr_ele])
1151
0
                return 19;
1152
1153
24.7k
            hDecoder->sbr_present_flag = 1;
1154
1155
            /* parse the SBR data */
1156
24.7k
            hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count,
1157
24.7k
                hDecoder->postSeekResetFlag);
1158
1159
#if 0
1160
            if (hDecoder->sbr[sbr_ele]->ret > 0)
1161
            {
1162
                printf("%s\n", NeAACDecGetErrorMessage(hDecoder->sbr[sbr_ele]->ret));
1163
            }
1164
#endif
1165
1166
24.7k
#if (defined(PS_DEC) || defined(DRM_PS))
1167
24.7k
            if (hDecoder->sbr[sbr_ele]->ps_used)
1168
3.84k
            {
1169
3.84k
                hDecoder->ps_used[sbr_ele] = 1;
1170
1171
                /* set element independent flag to 1 as well */
1172
3.84k
                hDecoder->ps_used_global = 1;
1173
3.84k
            }
1174
24.7k
#endif
1175
24.7k
        } else {
1176
118
#endif
1177
#ifndef DRM
1178
            while (count > 0)
1179
            {
1180
                uint16_t payload_bytes = extension_payload(ld, drc, count);
1181
                if (payload_bytes <= count) {
1182
                    count -= payload_bytes;
1183
                } else {
1184
                    count = 0;
1185
                }
1186
            }
1187
#else
1188
118
            (void)drc;
1189
118
            return 30;
1190
118
#endif
1191
118
#ifdef SBR_DEC
1192
118
        }
1193
24.9k
#endif
1194
24.9k
    }
1195
1196
26.7k
    return 0;
1197
26.8k
}
1198
1199
/* Table 4.4.12 */
1200
#ifdef SSR_DEC
1201
static void gain_control_data(bitfile *ld, ic_stream *ics)
1202
{
1203
    uint8_t bd, wd, ad;
1204
    ssr_info *ssr = &(ics->ssr);
1205
1206
    ssr->max_band = (uint8_t)faad_getbits(ld, 2
1207
        DEBUGVAR(1,1000,"gain_control_data(): max_band"));
1208
1209
    if (ics->window_sequence == ONLY_LONG_SEQUENCE)
1210
    {
1211
        for (bd = 1; bd <= ssr->max_band; bd++)
1212
        {
1213
            for (wd = 0; wd < 1; wd++)
1214
            {
1215
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1216
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1217
1218
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1219
                {
1220
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1221
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1222
                    ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1223
                        DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1224
                }
1225
            }
1226
        }
1227
    } else if (ics->window_sequence == LONG_START_SEQUENCE) {
1228
        for (bd = 1; bd <= ssr->max_band; bd++)
1229
        {
1230
            for (wd = 0; wd < 2; wd++)
1231
            {
1232
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1233
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1234
1235
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1236
                {
1237
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1238
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1239
                    if (wd == 0)
1240
                    {
1241
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1242
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1243
                    } else {
1244
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1245
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1246
                    }
1247
                }
1248
            }
1249
        }
1250
    } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1251
        for (bd = 1; bd <= ssr->max_band; bd++)
1252
        {
1253
            for (wd = 0; wd < 8; wd++)
1254
            {
1255
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1256
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1257
1258
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1259
                {
1260
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1261
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1262
                    ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1263
                        DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1264
                }
1265
            }
1266
        }
1267
    } else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
1268
        for (bd = 1; bd <= ssr->max_band; bd++)
1269
        {
1270
            for (wd = 0; wd < 2; wd++)
1271
            {
1272
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1273
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1274
1275
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1276
                {
1277
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1278
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1279
1280
                    if (wd == 0)
1281
                    {
1282
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1283
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1284
                    } else {
1285
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1286
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1287
                    }
1288
                }
1289
            }
1290
        }
1291
    }
1292
}
1293
#endif
1294
1295
#ifdef DRM
1296
/* Table 4.4.13 ASME */
1297
void DRM_aac_scalable_main_element(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo,
1298
                                   bitfile *ld, program_config *pce, drc_info *drc)
1299
785
{
1300
785
    uint8_t channels = hDecoder->fr_channels = 0;
1301
785
    uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
1302
785
    element cpe = {0};
1303
785
    ic_stream *ics1 = &(cpe.ics1);
1304
785
    ic_stream *ics2 = &(cpe.ics2);
1305
785
    ALIGN int16_t spec_data1[1024] = {0};
1306
785
    ALIGN int16_t spec_data2[1024] = {0};
1307
1308
785
    (void)drc;  /* TODO: remove unused parameter? */
1309
785
    (void)pce;  /* TODO: remove unused parameter? */
1310
1311
785
    hDecoder->fr_ch_ele = 0;
1312
1313
785
    hInfo->error = DRM_aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
1314
785
    if (hInfo->error > 0)
1315
13
        return;
1316
1317
772
    cpe.common_window = 1;
1318
772
    if (this_layer_stereo)
1319
228
    {
1320
228
        hDecoder->element_id[0] = ID_CPE;
1321
228
        if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
1322
168
            hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
1323
544
    } else {
1324
544
        hDecoder->element_id[0] = ID_SCE;
1325
544
    }
1326
1327
772
    if (this_layer_stereo)
1328
228
    {
1329
228
        cpe.channel        = 0;
1330
228
        cpe.paired_channel = 1;
1331
228
    }
1332
1333
1334
    /* Stereo2 / Mono1 */
1335
772
    ics1->tns_data_present = faad_get1bit(ld);
1336
1337
#if defined(LTP_DEC)
1338
    ics1->ltp.data_present = faad_get1bit(ld);
1339
#elif defined (DRM)
1340
772
    if(faad_get1bit(ld)) {
1341
7
         hInfo->error = 26;
1342
7
         return;
1343
7
    }
1344
#else
1345
    faad_get1bit(ld);
1346
#endif
1347
1348
765
    hInfo->error = side_info(hDecoder, &cpe, ld, ics1, 1);
1349
765
    if (hInfo->error > 0)
1350
80
        return;
1351
685
    if (this_layer_stereo)
1352
188
    {
1353
        /* Stereo3 */
1354
188
        ics2->tns_data_present = faad_get1bit(ld);
1355
#ifdef LTP_DEC
1356
        ics1->ltp.data_present =
1357
#endif
1358
188
            faad_get1bit(ld);
1359
188
        hInfo->error = side_info(hDecoder, &cpe, ld, ics2, 1);
1360
188
        if (hInfo->error > 0)
1361
22
            return;
1362
188
    }
1363
    /* Stereo4 / Mono2 */
1364
663
    if (ics1->tns_data_present)
1365
110
        tns_data(ics1, &(ics1->tns), ld);
1366
663
    if (this_layer_stereo)
1367
166
    {
1368
        /* Stereo5 */
1369
166
        if (ics2->tns_data_present)
1370
60
            tns_data(ics2, &(ics2->tns), ld);
1371
166
    }
1372
1373
663
#ifdef DRM
1374
    /* CRC check */
1375
663
    if (hDecoder->object_type == DRM_ER_LC)
1376
663
    {
1377
663
        if ((hInfo->error = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
1378
170
            return;
1379
663
    }
1380
493
#endif
1381
1382
    /* Stereo6 / Mono3 */
1383
    /* error resilient spectral data decoding */
1384
493
    if ((hInfo->error = reordered_spectral_data(hDecoder, ics1, ld, spec_data1)) > 0)
1385
24
    {
1386
24
        return;
1387
24
    }
1388
469
    if (this_layer_stereo)
1389
55
    {
1390
        /* Stereo7 */
1391
        /* error resilient spectral data decoding */
1392
55
        if ((hInfo->error = reordered_spectral_data(hDecoder, ics2, ld, spec_data2)) > 0)
1393
3
        {
1394
3
            return;
1395
3
        }
1396
55
    }
1397
1398
1399
466
#ifdef DRM
1400
466
#ifdef SBR_DEC
1401
    /* In case of DRM we need to read the SBR info before channel reconstruction */
1402
466
    if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
1403
423
    {
1404
423
        bitfile ld_sbr = {0};
1405
423
        uint32_t i;
1406
423
        uint16_t count = 0;
1407
423
        uint8_t *revbuffer;
1408
423
        uint8_t *prevbufstart;
1409
423
        uint8_t *pbufend;
1410
1411
        /* all forward bitreading should be finished at this point */
1412
423
        uint32_t bitsconsumed = faad_get_processed_bits(ld);
1413
423
        uint32_t buffer_size = faad_origbitbuffer_size(ld);
1414
423
        uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
1415
1416
423
        if (bitsconsumed + 8 > buffer_size*8)
1417
86
        {
1418
86
            hInfo->error = 14;
1419
86
            return;
1420
86
        }
1421
1422
337
        if (!hDecoder->sbr[0])
1423
248
        {
1424
248
            hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[0],
1425
248
                2*get_sample_rate(hDecoder->sf_index), 0 /* ds SBR */, 1);
1426
248
        }
1427
337
        if (!hDecoder->sbr[0])
1428
0
        {
1429
0
            hInfo->error = 19;
1430
0
            return;
1431
0
        }
1432
1433
        /* Reverse bit reading of SBR data in DRM audio frame */
1434
337
        revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
1435
337
        prevbufstart = revbuffer;
1436
337
        pbufend = &buffer[buffer_size - 1];
1437
4.23M
        for (i = 0; i < buffer_size; i++)
1438
4.23M
            *prevbufstart++ = reverse_byte(*pbufend--);
1439
1440
        /* Set SBR data */
1441
        /* consider 8 bits from AAC-CRC */
1442
        /* SBR buffer size is original buffer size minus AAC buffer size */
1443
337
        count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
1444
337
        faad_initbits(&ld_sbr, revbuffer, count);
1445
1446
337
        hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
1447
337
        hDecoder->sbr[0]->sample_rate *= 2;
1448
1449
337
        faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
1450
1451
337
        hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count, hDecoder->postSeekResetFlag);
1452
337
#if (defined(PS_DEC) || defined(DRM_PS))
1453
337
        if (hDecoder->sbr[0]->ps_used)
1454
215
        {
1455
215
            hDecoder->ps_used[0] = 1;
1456
215
            hDecoder->ps_used_global = 1;
1457
215
        }
1458
337
#endif
1459
1460
337
        if (ld_sbr.error)
1461
5
        {
1462
5
            hDecoder->sbr[0]->ret = 1;
1463
5
        }
1464
1465
        /* check CRC */
1466
        /* no need to check it if there was already an error */
1467
337
        if (hDecoder->sbr[0]->ret == 0)
1468
169
            hDecoder->sbr[0]->ret = (uint8_t)faad_check_CRC(&ld_sbr, (uint16_t)faad_get_processed_bits(&ld_sbr) - 8);
1469
1470
        /* SBR data was corrupted, disable it until the next header */
1471
337
        if (hDecoder->sbr[0]->ret != 0)
1472
204
        {
1473
204
            hDecoder->sbr[0]->header_count = 0;
1474
204
        }
1475
1476
337
        faad_endbits(&ld_sbr);
1477
1478
337
        if (revbuffer)
1479
337
            faad_free(revbuffer);
1480
337
    }
1481
380
#endif
1482
380
#endif
1483
1484
380
    if (this_layer_stereo)
1485
50
    {
1486
50
        hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
1487
50
        if (hInfo->error > 0)
1488
8
            return;
1489
330
    } else {
1490
330
        hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
1491
330
        if (hInfo->error > 0)
1492
3
            return;
1493
330
    }
1494
1495
    /* map output channels position to internal data channels */
1496
369
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
1497
369
    {
1498
        /* this might be faulty when pce_set is true */
1499
369
        hDecoder->internal_channel[channels] = channels;
1500
369
        hDecoder->internal_channel[channels+1] = channels+1;
1501
369
    } else {
1502
0
        hDecoder->internal_channel[channels] = channels;
1503
0
    }
1504
1505
369
    hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
1506
369
    hDecoder->fr_ch_ele++;
1507
1508
369
    return;
1509
380
}
1510
1511
/* Table 4.4.15 */
1512
static int8_t DRM_aac_scalable_main_header(NeAACDecStruct *hDecoder, ic_stream *ics1, ic_stream *ics2,
1513
                                           bitfile *ld, uint8_t this_layer_stereo)
1514
785
{
1515
785
    uint8_t retval = 0;
1516
785
    uint8_t ics_reserved_bit;
1517
1518
785
    ics_reserved_bit = faad_get1bit(ld
1519
785
        DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
1520
785
    if (ics_reserved_bit != 0)
1521
5
        return 32;
1522
780
    ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
1523
780
        DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
1524
780
    ics1->window_shape = faad_get1bit(ld
1525
780
        DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
1526
1527
780
    if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
1528
118
    {
1529
118
        ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
1530
118
            DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
1531
118
        ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
1532
118
            DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
1533
662
    } else {
1534
662
        ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
1535
662
            DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
1536
662
    }
1537
1538
    /* get the grouping information */
1539
780
    if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
1540
5
        return retval;
1541
1542
    /* should be an error */
1543
    /* check the range of max_sfb */
1544
775
    if (ics1->max_sfb > ics1->num_swb)
1545
0
        return 16;
1546
1547
775
    if (this_layer_stereo)
1548
231
    {
1549
231
        ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
1550
231
            DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
1551
231
        if (ics1->ms_mask_present == 3)
1552
3
        {
1553
            /* bitstream error */
1554
3
            return 32;
1555
3
        }
1556
228
        if (ics1->ms_mask_present == 1)
1557
70
        {
1558
70
            uint8_t g, sfb;
1559
283
            for (g = 0; g < ics1->num_window_groups; g++)
1560
213
            {
1561
1.65k
                for (sfb = 0; sfb < ics1->max_sfb; sfb++)
1562
1.43k
                {
1563
1.43k
                    ics1->ms_used[g][sfb] = faad_get1bit(ld
1564
1.43k
                        DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
1565
1.43k
                }
1566
213
            }
1567
70
        }
1568
1569
228
        memcpy(ics2, ics1, sizeof(ic_stream));
1570
544
    } else {
1571
544
        ics1->ms_mask_present = 0;
1572
544
    }
1573
1574
772
    return 0;
1575
775
}
1576
#endif
1577
1578
static uint8_t side_info(NeAACDecStruct *hDecoder, element *ele,
1579
                         bitfile *ld, ic_stream *ics, uint8_t scal_flag)
1580
145k
{
1581
145k
    uint8_t result;
1582
1583
145k
    ics->global_gain = (uint8_t)faad_getbits(ld, 8
1584
145k
        DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
1585
1586
145k
    if (!ele->common_window && !scal_flag)
1587
121k
    {
1588
121k
        if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
1589
238
            return result;
1590
121k
    }
1591
1592
145k
    if ((result = section_data(hDecoder, ics, ld)) > 0)
1593
624
        return result;
1594
1595
145k
    if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
1596
14
        return result;
1597
1598
145k
    if (!scal_flag)
1599
144k
    {
1600
        /**
1601
         **  NOTE: It could be that pulse data is available in scalable AAC too,
1602
         **        as said in Amendment 1, this could be only the case for ER AAC,
1603
         **        though. (have to check this out later)
1604
         **/
1605
        /* get pulse data */
1606
144k
        if ((ics->pulse_data_present = faad_get1bit(ld
1607
144k
            DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
1608
5.81k
        {
1609
5.81k
            if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
1610
34
                return result;
1611
5.81k
        }
1612
1613
        /* get tns data */
1614
144k
        if ((ics->tns_data_present = faad_get1bit(ld
1615
144k
            DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
1616
15.0k
        {
1617
15.0k
#ifdef ERROR_RESILIENCE
1618
15.0k
            if (hDecoder->object_type < ER_OBJECT_START)
1619
14.7k
#endif
1620
14.7k
                tns_data(ics, &(ics->tns), ld);
1621
15.0k
        }
1622
1623
        /* get gain control data */
1624
144k
        if ((ics->gain_control_data_present = faad_get1bit(ld
1625
144k
            DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
1626
57
        {
1627
#ifdef SSR_DEC
1628
            if (hDecoder->object_type != SSR)
1629
                return 1;
1630
            else
1631
                gain_control_data(ld, ics);
1632
#else
1633
57
            return 1;
1634
57
#endif
1635
57
        }
1636
144k
    }
1637
1638
144k
#ifdef ERROR_RESILIENCE
1639
144k
    if (hDecoder->aacSpectralDataResilienceFlag)
1640
4.28k
    {
1641
4.28k
        ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
1642
4.28k
            DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
1643
1644
4.28k
        if (hDecoder->channelConfiguration == 2)
1645
421
        {
1646
421
            if (ics->length_of_reordered_spectral_data > 6144)
1647
68
                ics->length_of_reordered_spectral_data = 6144;
1648
3.86k
        } else {
1649
3.86k
            if (ics->length_of_reordered_spectral_data > 12288)
1650
80
                ics->length_of_reordered_spectral_data = 12288;
1651
3.86k
        }
1652
1653
4.28k
        ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6
1654
4.28k
            DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
1655
4.28k
        if (ics->length_of_longest_codeword >= 49)
1656
251
            ics->length_of_longest_codeword = 49;
1657
4.28k
    }
1658
1659
    /* RVLC spectral data is put here */
1660
144k
    if (hDecoder->aacScalefactorDataResilienceFlag)
1661
6.49k
    {
1662
6.49k
        if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
1663
32
            return result;
1664
6.49k
    }
1665
144k
#endif
1666
1667
144k
    return 0;
1668
144k
}
1669
1670
/* Table 4.4.24 */
1671
static uint8_t individual_channel_stream(NeAACDecStruct *hDecoder, element *ele,
1672
                                         bitfile *ld, ic_stream *ics, uint8_t scal_flag,
1673
                                         int16_t *spec_data)
1674
145k
{
1675
145k
    uint8_t result;
1676
1677
145k
    result = side_info(hDecoder, ele, ld, ics, scal_flag);
1678
145k
    if (result > 0)
1679
897
        return result;
1680
1681
144k
    if (hDecoder->object_type >= ER_OBJECT_START)
1682
8.75k
    {
1683
8.75k
        if (ics->tns_data_present)
1684
320
            tns_data(ics, &(ics->tns), ld);
1685
8.75k
    }
1686
1687
144k
#ifdef DRM
1688
    /* CRC check */
1689
144k
    if (hDecoder->object_type == DRM_ER_LC)
1690
0
    {
1691
0
        if ((result = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
1692
0
            return result;
1693
0
    }
1694
144k
#endif
1695
1696
144k
#ifdef ERROR_RESILIENCE
1697
144k
    if (hDecoder->aacSpectralDataResilienceFlag)
1698
3.59k
    {
1699
        /* error resilient spectral data decoding */
1700
3.59k
        if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1701
75
        {
1702
75
            return result;
1703
75
        }
1704
140k
    } else {
1705
140k
#endif
1706
        /* decode the spectral data */
1707
140k
        if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1708
42
        {
1709
42
            return result;
1710
42
        }
1711
140k
#ifdef ERROR_RESILIENCE
1712
140k
    }
1713
143k
#endif
1714
1715
    /* pulse coding reconstruction */
1716
143k
    if (ics->pulse_data_present)
1717
5.76k
    {
1718
5.76k
        if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
1719
5.75k
        {
1720
5.75k
            if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
1721
4
                return result;
1722
5.75k
        } else {
1723
16
            return 2; /* pulse coding not allowed for short blocks */
1724
16
        }
1725
5.76k
    }
1726
1727
143k
    return 0;
1728
143k
}
1729
1730
/* Table 4.4.25 */
1731
static uint8_t section_data(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld)
1732
145k
{
1733
145k
    uint8_t g;
1734
145k
    uint8_t sect_esc_val, sect_bits;
1735
145k
    uint8_t sect_lim; /* 51 or 120, anyways less than 127. */
1736
1737
145k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1738
16.4k
        sect_bits = 3;
1739
16.4k
        sect_lim = 8 * 15;
1740
129k
    } else {
1741
129k
        sect_bits = 5;
1742
129k
        sect_lim = MAX_SFB;
1743
129k
    }
1744
145k
    sect_esc_val = (1u<<sect_bits) - 1;
1745
1746
#if 0
1747
    printf("\ntotal sfb %d\n", ics->max_sfb);
1748
    printf("   sect    top     cb\n");
1749
#endif
1750
1751
380k
    for (g = 0; g < ics->num_window_groups; g++)
1752
235k
    {
1753
235k
        uint8_t k = 0;
1754
235k
        uint8_t i = 0;
1755
1756
304k
        while (k < ics->max_sfb)
1757
69.1k
        {
1758
69.1k
#ifdef ERROR_RESILIENCE
1759
69.1k
            uint8_t vcb11 = 0;
1760
69.1k
#endif
1761
69.1k
            uint8_t sfb;
1762
69.1k
            uint8_t sect_len_incr;
1763
69.1k
            uint8_t sect_len = 0;
1764
69.1k
            uint8_t sect_cb_bits = 4;
1765
1766
            /* if "faad_getbits" detects error and returns "0", "k" is never
1767
               incremented and we cannot leave the while loop */
1768
69.1k
            if (ld->error != 0)
1769
0
                return 14;
1770
69.1k
            if (i >= sect_lim)
1771
381
                return 15;
1772
1773
68.7k
#ifdef ERROR_RESILIENCE
1774
68.7k
            if (hDecoder->aacSectionDataResilienceFlag)
1775
16.7k
                sect_cb_bits = 5;
1776
68.7k
#endif
1777
1778
68.7k
            ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
1779
68.7k
                DEBUGVAR(1,71,"section_data(): sect_cb"));
1780
1781
68.7k
            if (ics->sect_cb[g][i] == 12)
1782
68
                return 32;
1783
1784
#if 0
1785
            printf("%d\n", ics->sect_cb[g][i]);
1786
#endif
1787
1788
#ifndef DRM
1789
            if (ics->sect_cb[g][i] == NOISE_HCB)
1790
                ics->noise_used = 1;
1791
#else
1792
            /* PNS not allowed in DRM */
1793
68.7k
            if (ics->sect_cb[g][i] == NOISE_HCB)
1794
16
                return 29;
1795
68.6k
#endif
1796
68.6k
            if (ics->sect_cb[g][i] == INTENSITY_HCB2 || ics->sect_cb[g][i] == INTENSITY_HCB)
1797
1.76k
                ics->is_used = 1;
1798
1799
68.6k
#ifdef ERROR_RESILIENCE
1800
68.6k
            if (hDecoder->aacSectionDataResilienceFlag)
1801
16.7k
            {
1802
16.7k
                if ((ics->sect_cb[g][i] == 11) ||
1803
16.7k
                    ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
1804
4.05k
                {
1805
4.05k
                    vcb11 = 1;
1806
4.05k
                }
1807
16.7k
            }
1808
68.6k
            if (vcb11)
1809
4.05k
            {
1810
4.05k
                sect_len_incr = 1;
1811
64.6k
            } else {
1812
64.6k
#endif
1813
64.6k
                sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1814
64.6k
                    DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1815
64.6k
#ifdef ERROR_RESILIENCE
1816
64.6k
            }
1817
68.6k
#endif
1818
69.2k
            while (sect_len_incr == sect_esc_val /* &&
1819
68.6k
                (k+sect_len < ics->max_sfb)*/)
1820
574
            {
1821
574
                sect_len += sect_len_incr;
1822
574
                if (sect_len > sect_lim)
1823
16
                    return 15;
1824
558
                sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1825
558
                    DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1826
558
            }
1827
1828
68.6k
            sect_len += sect_len_incr;
1829
1830
68.6k
            ics->sect_start[g][i] = k;
1831
68.6k
            ics->sect_end[g][i] = k + sect_len;
1832
1833
#if 0
1834
            printf("%d\n", ics->sect_start[g][i]);
1835
#endif
1836
#if 0
1837
            printf("%d\n", ics->sect_end[g][i]);
1838
#endif
1839
1840
68.6k
            if (sect_len > sect_lim)
1841
7
                return 15;
1842
68.6k
            if (k + sect_len > sect_lim)
1843
10
                return 15;
1844
1845
213k
            for (sfb = k; sfb < k + sect_len; sfb++)
1846
145k
            {
1847
145k
                ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
1848
#if 0
1849
                printf("%d\n", ics->sfb_cb[g][sfb]);
1850
#endif
1851
145k
            }
1852
1853
#if 0
1854
            printf(" %6d %6d %6d\n",
1855
                i,
1856
                ics->sect_end[g][i],
1857
                ics->sect_cb[g][i]);
1858
#endif
1859
1860
68.6k
            k += sect_len; /* k <= sect_lim */
1861
68.6k
            i++;
1862
68.6k
        }
1863
235k
        ics->num_sec[g] = i;
1864
1865
        /* the sum of all sect_len_incr elements for a given window
1866
         * group shall equal max_sfb */
1867
235k
        if (k != ics->max_sfb)
1868
126
        {
1869
126
            return 32;
1870
126
        }
1871
#if 0
1872
        printf("%d\n", ics->num_sec[g]);
1873
#endif
1874
235k
    }
1875
1876
#if 0
1877
    printf("\n");
1878
#endif
1879
1880
145k
    return 0;
1881
145k
}
1882
1883
/*
1884
 *  decode_scale_factors()
1885
 *   decodes the scalefactors from the bitstream
1886
 */
1887
/*
1888
 * All scalefactors (and also the stereo positions and pns energies) are
1889
 * transmitted using Huffman coded DPCM relative to the previous active
1890
 * scalefactor (respectively previous stereo position or previous pns energy,
1891
 * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
1892
 * differentially coded relative to the global gain.
1893
 */
1894
static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
1895
138k
{
1896
138k
    uint8_t g, sfb;
1897
138k
    int16_t t;
1898
1899
138k
    int16_t scale_factor = ics->global_gain;
1900
138k
    int16_t is_position = 0;
1901
138k
    int16_t scale_factor_max = 255;
1902
#ifdef FIXED_POINT
1903
    /* TODO: consider rolling out to regular build. */
1904
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1905
    /* The value is inexact, adjusted to current fuzzer findings. */
1906
    scale_factor_max = 165;
1907
#endif  // FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1908
#endif  // FIXED_POINT
1909
#ifndef DRM
1910
    int8_t noise_pcm_flag = 1;
1911
    int16_t noise_energy = ics->global_gain - 90;
1912
#endif
1913
1914
364k
    for (g = 0; g < ics->num_window_groups; g++)
1915
226k
    {
1916
350k
        for (sfb = 0; sfb < ics->max_sfb; sfb++)
1917
124k
        {
1918
124k
            switch (ics->sfb_cb[g][sfb])
1919
124k
            {
1920
88.3k
            case ZERO_HCB: /* zero book */
1921
88.3k
                ics->scale_factors[g][sfb] = 0;
1922
//#define SF_PRINT
1923
#ifdef SF_PRINT
1924
                printf("%d\n", ics->scale_factors[g][sfb]);
1925
#endif
1926
88.3k
                break;
1927
4.41k
            case INTENSITY_HCB: /* intensity books */
1928
6.58k
            case INTENSITY_HCB2:
1929
1930
                /* decode intensity position */
1931
6.58k
                t = huffman_scale_factor(ld);
1932
6.58k
                is_position += (t - 60);
1933
6.58k
                ics->scale_factors[g][sfb] = is_position;
1934
#ifdef SF_PRINT
1935
                printf("%d\n", ics->scale_factors[g][sfb]);
1936
#endif
1937
1938
6.58k
                break;
1939
0
            case NOISE_HCB: /* noise books */
1940
1941
#ifndef DRM
1942
                /* decode noise energy */
1943
                if (noise_pcm_flag)
1944
                {
1945
                    noise_pcm_flag = 0;
1946
                    t = (int16_t)faad_getbits(ld, 9
1947
                        DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
1948
                } else {
1949
                    t = huffman_scale_factor(ld);
1950
                    t -= 60;
1951
                }
1952
                noise_energy += t;
1953
                ics->scale_factors[g][sfb] = noise_energy;
1954
#ifdef SF_PRINT
1955
                printf("%d\n", ics->scale_factors[g][sfb]);
1956
#endif
1957
#else
1958
                /* PNS not allowed in DRM */
1959
0
                return 29;
1960
0
#endif
1961
1962
0
                break;
1963
29.1k
            default: /* spectral books */
1964
1965
                /* ics->scale_factors[g][sfb] must be between 0 and 255 */
1966
1967
29.1k
                ics->scale_factors[g][sfb] = 0;
1968
1969
                /* decode scale factor */
1970
29.1k
                t = huffman_scale_factor(ld);
1971
29.1k
                scale_factor += (t - 60);
1972
29.1k
                if (scale_factor < 0 || scale_factor > 255)
1973
14
                    return 4;
1974
29.0k
                ics->scale_factors[g][sfb] = min(scale_factor, scale_factor_max);
1975
#ifdef SF_PRINT
1976
                printf("%d\n", ics->scale_factors[g][sfb]);
1977
#endif
1978
1979
29.0k
                break;
1980
124k
            }
1981
124k
        }
1982
226k
    }
1983
1984
138k
    return 0;
1985
138k
}
1986
1987
/* Table 4.4.26 */
1988
static uint8_t scale_factor_data(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld)
1989
145k
{
1990
145k
    uint8_t ret = 0;
1991
#ifdef PROFILE
1992
    int64_t count = faad_get_ts();
1993
#endif
1994
1995
145k
#ifdef ERROR_RESILIENCE
1996
145k
    if (!hDecoder->aacScalefactorDataResilienceFlag)
1997
138k
    {
1998
138k
#endif
1999
138k
        ret = decode_scale_factors(ics, ld);
2000
138k
#ifdef ERROR_RESILIENCE
2001
138k
    } else {
2002
        /* In ER AAC the parameters for RVLC are seperated from the actual
2003
           data that holds the scale_factors.
2004
           Strangely enough, 2 parameters for HCR are put inbetween them.
2005
        */
2006
6.49k
        ret = rvlc_scale_factor_data(ics, ld);
2007
6.49k
    }
2008
145k
#endif
2009
2010
#ifdef PROFILE
2011
    count = faad_get_ts() - count;
2012
    hDecoder->scalefac_cycles += count;
2013
#endif
2014
2015
145k
    return ret;
2016
145k
}
2017
2018
/* Table 4.4.27 */
2019
static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
2020
15.2k
{
2021
15.2k
    uint8_t w, filt, i, coef_bits;
2022
15.2k
    uint8_t n_filt_bits = 2;
2023
15.2k
    uint8_t length_bits = 6;
2024
15.2k
    uint8_t order_bits = 5;
2025
2026
15.2k
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
2027
1.28k
    {
2028
1.28k
        n_filt_bits = 1;
2029
1.28k
        length_bits = 4;
2030
1.28k
        order_bits = 3;
2031
1.28k
    }
2032
2033
39.4k
    for (w = 0; w < ics->num_windows; w++)
2034
24.2k
    {
2035
24.2k
        uint8_t start_coef_bits = 3;
2036
24.2k
        tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
2037
24.2k
            DEBUGVAR(1,74,"tns_data(): n_filt"));
2038
#if 0
2039
        printf("%d\n", tns->n_filt[w]);
2040
#endif
2041
2042
24.2k
        if (tns->n_filt[w])
2043
8.71k
        {
2044
8.71k
            if ((tns->coef_res[w] = faad_get1bit(ld
2045
8.71k
                DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
2046
2.78k
                start_coef_bits = 4;
2047
#if 0
2048
            printf("%d\n", tns->coef_res[w]);
2049
#endif
2050
8.71k
        }
2051
2052
36.4k
        for (filt = 0; filt < tns->n_filt[w]; filt++)
2053
12.2k
        {
2054
12.2k
            tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits
2055
12.2k
                DEBUGVAR(1,76,"tns_data(): length"));
2056
#if 0
2057
            printf("%d\n", tns->length[w][filt]);
2058
#endif
2059
12.2k
            tns->order[w][filt]  = (uint8_t)faad_getbits(ld, order_bits
2060
12.2k
                DEBUGVAR(1,77,"tns_data(): order"));
2061
#if 0
2062
            printf("%d\n", tns->order[w][filt]);
2063
#endif
2064
12.2k
            if (tns->order[w][filt])
2065
6.50k
            {
2066
6.50k
                tns->direction[w][filt] = faad_get1bit(ld
2067
6.50k
                    DEBUGVAR(1,78,"tns_data(): direction"));
2068
#if 0
2069
                printf("%d\n", tns->direction[w][filt]);
2070
#endif
2071
6.50k
                tns->coef_compress[w][filt] = faad_get1bit(ld
2072
6.50k
                    DEBUGVAR(1,79,"tns_data(): coef_compress"));
2073
#if 0
2074
                printf("%d\n", tns->coef_compress[w][filt]);
2075
#endif
2076
2077
6.50k
                coef_bits = start_coef_bits - tns->coef_compress[w][filt];
2078
33.0k
                for (i = 0; i < tns->order[w][filt]; i++)
2079
26.5k
                {
2080
26.5k
                    tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
2081
26.5k
                        DEBUGVAR(1,80,"tns_data(): coef"));
2082
#if 0
2083
                    printf("%d\n", tns->coef[w][filt][i]);
2084
#endif
2085
26.5k
                }
2086
6.50k
            }
2087
12.2k
        }
2088
24.2k
    }
2089
15.2k
}
2090
2091
#ifdef LTP_DEC
2092
/* Table 4.4.28 */
2093
static uint8_t ltp_data(NeAACDecStruct *hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
2094
{
2095
    uint8_t sfb, w;
2096
2097
    ltp->lag = 0;
2098
2099
#ifdef LD_DEC
2100
    if (hDecoder->object_type == LD)
2101
    {
2102
        ltp->lag_update = (uint8_t)faad_getbits(ld, 1
2103
            DEBUGVAR(1,142,"ltp_data(): lag_update"));
2104
2105
        if (ltp->lag_update)
2106
        {
2107
            ltp->lag = (uint16_t)faad_getbits(ld, 10
2108
                DEBUGVAR(1,81,"ltp_data(): lag"));
2109
        }
2110
    } else {
2111
#endif
2112
        ltp->lag = (uint16_t)faad_getbits(ld, 11
2113
            DEBUGVAR(1,81,"ltp_data(): lag"));
2114
#ifdef LD_DEC
2115
    }
2116
#endif
2117
2118
    /* Check length of lag */
2119
    if (ltp->lag > (hDecoder->frameLength << 1))
2120
        return 18;
2121
2122
    ltp->coef = (uint8_t)faad_getbits(ld, 3
2123
        DEBUGVAR(1,82,"ltp_data(): coef"));
2124
2125
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
2126
    {
2127
        for (w = 0; w < ics->num_windows; w++)
2128
        {
2129
            if ((ltp->short_used[w] = faad_get1bit(ld
2130
                DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
2131
            {
2132
                ltp->short_lag_present[w] = faad_get1bit(ld
2133
                    DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
2134
                if (ltp->short_lag_present[w])
2135
                {
2136
                    ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
2137
                        DEBUGVAR(1,85,"ltp_data(): short_lag"));
2138
                }
2139
            }
2140
        }
2141
    } else {
2142
        ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
2143
2144
        for (sfb = 0; sfb < ltp->last_band; sfb++)
2145
        {
2146
            ltp->long_used[sfb] = faad_get1bit(ld
2147
                DEBUGVAR(1,86,"ltp_data(): long_used"));
2148
        }
2149
    }
2150
2151
    return 0;
2152
}
2153
#endif
2154
2155
/* Table 4.4.29 */
2156
static uint8_t spectral_data(NeAACDecStruct *hDecoder, ic_stream *ics, bitfile *ld,
2157
                             int16_t *spectral_data)
2158
140k
{
2159
140k
    int8_t i;
2160
140k
    uint8_t g;
2161
140k
    uint16_t inc, k, p = 0;
2162
140k
    uint8_t groups = 0;
2163
140k
    uint8_t sect_cb;
2164
140k
    uint8_t result;
2165
140k
    uint16_t nshort = hDecoder->frameLength/8;
2166
2167
#ifdef PROFILE
2168
    int64_t count = faad_get_ts();
2169
#endif
2170
2171
369k
    for(g = 0; g < ics->num_window_groups; g++)
2172
229k
    {
2173
229k
        p = groups*nshort;
2174
2175
263k
        for (i = 0; i < ics->num_sec[g]; i++)
2176
34.4k
        {
2177
34.4k
            sect_cb = ics->sect_cb[g][i];
2178
2179
34.4k
            inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
2180
2181
34.4k
            switch (sect_cb)
2182
34.4k
            {
2183
12.3k
            case ZERO_HCB:
2184
12.3k
            case NOISE_HCB:
2185
13.1k
            case INTENSITY_HCB:
2186
13.4k
            case INTENSITY_HCB2:
2187
//#define SD_PRINT
2188
#ifdef SD_PRINT
2189
                {
2190
                    int j;
2191
                    for (j = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; j < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; j++)
2192
                    {
2193
                        printf("%d\n", 0);
2194
                    }
2195
                }
2196
#endif
2197
//#define SFBO_PRINT
2198
#ifdef SFBO_PRINT
2199
                printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2200
#endif
2201
13.4k
                p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
2202
13.4k
                    ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2203
13.4k
                break;
2204
20.9k
            default:
2205
#ifdef SFBO_PRINT
2206
                printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2207
#endif
2208
20.9k
                for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
2209
202k
                     k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
2210
181k
                {
2211
181k
                    if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
2212
42
                        return result;
2213
#ifdef SD_PRINT
2214
                    {
2215
                        int j;
2216
                        for (j = p; j < p+inc; j++)
2217
                        {
2218
                            printf("%d\n", spectral_data[j]);
2219
                        }
2220
                    }
2221
#endif
2222
181k
                    p += inc;
2223
181k
                }
2224
20.9k
                break;
2225
34.4k
            }
2226
34.4k
        }
2227
229k
        groups += ics->window_group_length[g];
2228
229k
    }
2229
2230
#ifdef PROFILE
2231
    count = faad_get_ts() - count;
2232
    hDecoder->spectral_cycles += count;
2233
#endif
2234
2235
140k
    return 0;
2236
140k
}
2237
2238
#ifndef DRM
2239
/* Table 4.4.30 */
2240
static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
2241
{
2242
    uint16_t i, n, dataElementLength;
2243
    uint8_t dataElementLengthPart;
2244
    uint8_t align = 4, data_element_version, loopCounter;
2245
2246
    uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
2247
        DEBUGVAR(1,87,"extension_payload(): extension_type"));
2248
2249
    switch (extension_type)
2250
    {
2251
    case EXT_DYNAMIC_RANGE:
2252
        drc->present = 1;
2253
        n = dynamic_range_info(ld, drc);
2254
        return n;
2255
    case EXT_FILL_DATA:
2256
        /* fill_nibble = */ faad_getbits(ld, 4
2257
            DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be '0000' */
2258
        for (i = 0; i < count-1; i++)
2259
        {
2260
            /* fill_byte[i] = */ faad_getbits(ld, 8
2261
                DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be '10100101' */
2262
        }
2263
        return count;
2264
    case EXT_DATA_ELEMENT:
2265
        data_element_version = (uint8_t)faad_getbits(ld, 4
2266
            DEBUGVAR(1,400,"extension_payload(): data_element_version"));
2267
        switch (data_element_version)
2268
        {
2269
        case ANC_DATA:
2270
            loopCounter = 0;
2271
            dataElementLength = 0;
2272
            do {
2273
                dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
2274
                    DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
2275
                dataElementLength += dataElementLengthPart;
2276
                loopCounter++;
2277
            } while (dataElementLengthPart == 255);
2278
2279
            for (i = 0; i < dataElementLength; i++)
2280
            {
2281
                /* data_element_byte[i] = */ faad_getbits(ld, 8
2282
                    DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
2283
                return (dataElementLength+loopCounter+1);
2284
            }
2285
        default:
2286
            align = 0;
2287
        }
2288
    case EXT_FIL:
2289
    default:
2290
        faad_getbits(ld, align
2291
            DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
2292
        for (i = 0; i < count-1; i++)
2293
        {
2294
            /* other_bits[i] = */ faad_getbits(ld, 8
2295
               DEBUGVAR(1,89,"extension_payload(): fill_bit"));
2296
        }
2297
        return count;
2298
    }
2299
}
2300
2301
/* Table 4.4.31 */
2302
static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc)
2303
{
2304
    uint8_t i, n = 1;
2305
    uint8_t band_incr;
2306
2307
    drc->num_bands = 1;
2308
2309
    if (faad_get1bit(ld
2310
        DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
2311
    {
2312
        drc->pce_instance_tag = (uint8_t)faad_getbits(ld, 4
2313
            DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag"));
2314
        /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4
2315
            DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits"));
2316
        n++;
2317
    }
2318
2319
    drc->excluded_chns_present = faad_get1bit(ld
2320
        DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present"));
2321
    if (drc->excluded_chns_present == 1)
2322
    {
2323
        n += excluded_channels(ld, drc);
2324
    }
2325
2326
    if (faad_get1bit(ld
2327
        DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
2328
    {
2329
        band_incr = (uint8_t)faad_getbits(ld, 4
2330
            DEBUGVAR(1,95,"dynamic_range_info(): band_incr"));
2331
        /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4
2332
            DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits"));
2333
        n++;
2334
        drc->num_bands += band_incr;
2335
2336
        for (i = 0; i < drc->num_bands; i++)
2337
        {
2338
            drc->band_top[i] = (uint8_t)faad_getbits(ld, 8
2339
                DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
2340
            n++;
2341
        }
2342
    }
2343
2344
    if (faad_get1bit(ld
2345
        DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
2346
    {
2347
        drc->prog_ref_level = (uint8_t)faad_getbits(ld, 7
2348
            DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level"));
2349
        /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
2350
            DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits"));
2351
        n++;
2352
    }
2353
2354
    for (i = 0; i < drc->num_bands; i++)
2355
    {
2356
        drc->dyn_rng_sgn[i] = faad_get1bit(ld
2357
            DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn"));
2358
        drc->dyn_rng_ctl[i] = (uint8_t)faad_getbits(ld, 7
2359
            DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl"));
2360
        n++;
2361
    }
2362
2363
    return n;
2364
}
2365
2366
/* Table 4.4.32 */
2367
static uint8_t excluded_channels(bitfile *ld, drc_info *drc)
2368
{
2369
    uint8_t i, n = 0;
2370
    uint8_t num_excl_chan = 7;
2371
2372
    for (i = 0; i < 7; i++)
2373
    {
2374
        drc->exclude_mask[i] = faad_get1bit(ld
2375
            DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
2376
    }
2377
    n++;
2378
2379
    while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
2380
        DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
2381
    {
2382
        if (i >= MAX_CHANNELS - num_excl_chan - 7)
2383
            return n;
2384
        for (i = num_excl_chan; i < num_excl_chan+7; i++)
2385
        {
2386
            drc->exclude_mask[i] = faad_get1bit(ld
2387
                DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
2388
        }
2389
        n++;
2390
        num_excl_chan += 7;
2391
    }
2392
2393
    return n;
2394
}
2395
#endif
2396
2397
/* Annex A: Audio Interchange Formats */
2398
2399
/* Table 1.A.2 */
2400
void get_adif_header(adif_header *adif, bitfile *ld)
2401
113
{
2402
113
    uint8_t i;
2403
2404
    /* adif_id[0] = */ faad_getbits(ld, 8
2405
113
        DEBUGVAR(1,106,"get_adif_header(): adif_id[0]"));
2406
113
    /* adif_id[1] = */ faad_getbits(ld, 8
2407
113
        DEBUGVAR(1,107,"get_adif_header(): adif_id[1]"));
2408
113
    /* adif_id[2] = */ faad_getbits(ld, 8
2409
113
        DEBUGVAR(1,108,"get_adif_header(): adif_id[2]"));
2410
113
    /* adif_id[3] = */ faad_getbits(ld, 8
2411
113
        DEBUGVAR(1,109,"get_adif_header(): adif_id[3]"));
2412
113
    adif->copyright_id_present = faad_get1bit(ld
2413
113
        DEBUGVAR(1,110,"get_adif_header(): copyright_id_present"));
2414
113
    if(adif->copyright_id_present)
2415
15
    {
2416
150
        for (i = 0; i < 72/8; i++)
2417
135
        {
2418
135
            adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8
2419
135
                DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
2420
135
        }
2421
15
        adif->copyright_id[i] = 0;
2422
15
    }
2423
113
    adif->original_copy  = faad_get1bit(ld
2424
113
        DEBUGVAR(1,112,"get_adif_header(): original_copy"));
2425
113
    adif->home = faad_get1bit(ld
2426
113
        DEBUGVAR(1,113,"get_adif_header(): home"));
2427
113
    adif->bitstream_type = faad_get1bit(ld
2428
113
        DEBUGVAR(1,114,"get_adif_header(): bitstream_type"));
2429
113
    adif->bitrate = faad_getbits(ld, 23
2430
113
        DEBUGVAR(1,115,"get_adif_header(): bitrate"));
2431
113
    adif->num_program_config_elements = (uint8_t)faad_getbits(ld, 4
2432
113
        DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements"));
2433
2434
1.46k
    for (i = 0; i < adif->num_program_config_elements + 1; i++)
2435
1.34k
    {
2436
1.34k
        if(adif->bitstream_type == 0)
2437
462
        {
2438
462
            adif->adif_buffer_fullness = faad_getbits(ld, 20
2439
462
                DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
2440
885
        } else {
2441
885
            adif->adif_buffer_fullness = 0;
2442
885
        }
2443
2444
1.34k
        program_config_element(&adif->pce[i], ld);
2445
1.34k
    }
2446
113
}
2447
2448
/* Table 1.A.5 */
2449
uint8_t adts_frame(adts_header *adts, bitfile *ld)
2450
5.25k
{
2451
    /* faad_byte_align(ld); */
2452
5.25k
    if (adts_fixed_header(adts, ld))
2453
5.09k
        return 5;
2454
158
    adts_variable_header(adts, ld);
2455
158
    adts_error_check(adts, ld);
2456
2457
158
    return 0;
2458
5.25k
}
2459
2460
/* Table 1.A.6 */
2461
static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld)
2462
5.25k
{
2463
5.25k
    uint16_t i;
2464
5.25k
    uint8_t sync_err = 1;
2465
2466
    /* try to recover from sync errors */
2467
3.91M
    for (i = 0; i < 768; i++)
2468
3.91M
    {
2469
3.91M
        adts->syncword = (uint16_t)faad_showbits(ld, 12);
2470
3.91M
        if (adts->syncword != 0xFFF)
2471
3.91M
        {
2472
3.91M
            faad_getbits(ld, 8
2473
3.91M
                DEBUGVAR(0,0,""));
2474
3.91M
        } else {
2475
158
            sync_err = 0;
2476
158
            faad_getbits(ld, 12
2477
158
                DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
2478
158
            break;
2479
158
        }
2480
3.91M
    }
2481
5.25k
    if (sync_err)
2482
5.09k
        return 5;
2483
2484
158
    adts->id = faad_get1bit(ld
2485
158
        DEBUGVAR(1,119,"adts_fixed_header(): id"));
2486
158
    adts->layer = (uint8_t)faad_getbits(ld, 2
2487
158
        DEBUGVAR(1,120,"adts_fixed_header(): layer"));
2488
158
    adts->protection_absent = faad_get1bit(ld
2489
158
        DEBUGVAR(1,121,"adts_fixed_header(): protection_absent"));
2490
158
    adts->profile = (uint8_t)faad_getbits(ld, 2
2491
158
        DEBUGVAR(1,122,"adts_fixed_header(): profile"));
2492
158
    adts->sf_index = (uint8_t)faad_getbits(ld, 4
2493
158
        DEBUGVAR(1,123,"adts_fixed_header(): sf_index"));
2494
158
    adts->private_bit = faad_get1bit(ld
2495
158
        DEBUGVAR(1,124,"adts_fixed_header(): private_bit"));
2496
158
    adts->channel_configuration = (uint8_t)faad_getbits(ld, 3
2497
158
        DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration"));
2498
158
    adts->original = faad_get1bit(ld
2499
158
        DEBUGVAR(1,126,"adts_fixed_header(): original"));
2500
158
    adts->home = faad_get1bit(ld
2501
158
        DEBUGVAR(1,127,"adts_fixed_header(): home"));
2502
2503
158
    if (adts->old_format == 1)
2504
0
    {
2505
        /* Removed in corrigendum 14496-3:2002 */
2506
0
        if (adts->id == 0)
2507
0
        {
2508
0
            adts->emphasis = (uint8_t)faad_getbits(ld, 2
2509
0
                DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
2510
0
        }
2511
0
    }
2512
2513
158
    return 0;
2514
5.25k
}
2515
2516
/* Table 1.A.7 */
2517
static void adts_variable_header(adts_header *adts, bitfile *ld)
2518
158
{
2519
158
    adts->copyright_identification_bit = faad_get1bit(ld
2520
158
        DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit"));
2521
158
    adts->copyright_identification_start = faad_get1bit(ld
2522
158
        DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start"));
2523
158
    adts->aac_frame_length = (uint16_t)faad_getbits(ld, 13
2524
158
        DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length"));
2525
158
    adts->adts_buffer_fullness = (uint16_t)faad_getbits(ld, 11
2526
158
        DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness"));
2527
158
    adts->no_raw_data_blocks_in_frame = (uint8_t)faad_getbits(ld, 2
2528
158
        DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame"));
2529
158
}
2530
2531
/* Table 1.A.8 */
2532
static void adts_error_check(adts_header *adts, bitfile *ld)
2533
158
{
2534
158
    if (adts->protection_absent == 0)
2535
32
    {
2536
32
        adts->crc_check = (uint16_t)faad_getbits(ld, 16
2537
32
            DEBUGVAR(1,134,"adts_error_check(): crc_check"));
2538
32
    }
2539
158
}
2540
2541
/* LATM parsing functions */
2542
#if 0
2543
2544
static uint32_t latm_get_value(bitfile *ld)
2545
{
2546
    uint32_t l, value;
2547
    uint8_t bytesForValue;
2548
2549
    bytesForValue = (uint8_t)faad_getbits(ld, 2);
2550
    value = 0;
2551
    for(l=0; l<bytesForValue; l++)
2552
        value = (value << 8) | (uint8_t)faad_getbits(ld, 8);
2553
2554
    return value;
2555
}
2556
2557
static uint32_t latmParsePayload(latm_header *latm, bitfile *ld)
2558
{
2559
    //assuming there's only one program with a single layer and 1 subFrame,
2560
    //allStreamsSametimeframing is set,
2561
    uint32_t framelen;
2562
    uint8_t tmp;
2563
2564
    //this should be the payload length field for the current configuration
2565
    framelen = 0;
2566
    if(latm->framelen_type==0)
2567
    {
2568
        do
2569
        {
2570
            tmp = (uint8_t)faad_getbits(ld, 8);
2571
            framelen += tmp;
2572
        } while(tmp==0xff);
2573
    }
2574
    else if(latm->framelen_type==1)
2575
        framelen=latm->frameLength;
2576
2577
    return framelen;
2578
}
2579
2580
static uint32_t latmAudioMuxElement(latm_header *latm, bitfile *ld)
2581
{
2582
    uint32_t ascLen, asc_bits=0;
2583
    uint32_t x1, y1, m, n, i;
2584
    program_config pce;
2585
    mp4AudioSpecificConfig mp4ASC;
2586
2587
    latm->useSameStreamMux = (uint8_t)faad_getbits(ld, 1);
2588
    if(!latm->useSameStreamMux)
2589
    {
2590
        //parseSameStreamMuxConfig
2591
        latm->version = (uint8_t) faad_getbits(ld, 1);
2592
        if(latm->version)
2593
            latm->versionA = (uint8_t) faad_getbits(ld, 1);
2594
        if(latm->versionA)
2595
        {
2596
            //dunno the payload format for versionA
2597
            fprintf(stderr, "versionA not supported\n");
2598
            return 0;
2599
        }
2600
        if(latm->version) //read taraBufferFullness
2601
            latm_get_value(ld);
2602
        latm->allStreamsSameTimeFraming = (uint8_t)faad_getbits(ld, 1);
2603
        latm->numSubFrames = (uint8_t)faad_getbits(ld, 6) + 1;
2604
        latm->numPrograms = (uint8_t)faad_getbits(ld, 4) + 1;
2605
        latm->numLayers = faad_getbits(ld, 3) + 1;
2606
        if(latm->numPrograms>1 || !latm->allStreamsSameTimeFraming || latm->numSubFrames>1 || latm->numLayers>1)
2607
        {
2608
            fprintf(stderr, "\r\nUnsupported LATM configuration: %d programs/ %d subframes, %d layers, allstreams: %d\n",
2609
                latm->numPrograms, latm->numSubFrames, latm->numLayers, latm->allStreamsSameTimeFraming);
2610
            return 0;
2611
        }
2612
        ascLen = 0;
2613
        if(latm->version)
2614
            ascLen = latm_get_value(ld);
2615
2616
        x1 = faad_get_processed_bits(ld);
2617
        if(AudioSpecificConfigFromBitfile(ld, &mp4ASC, &pce, 0, 1) < 0)
2618
            return 0;
2619
2620
        //horrid hack to unread the ASC bits and store them in latm->ASC
2621
        //the correct code would rely on an ideal faad_ungetbits()
2622
        y1 = faad_get_processed_bits(ld);
2623
        if((y1-x1) <= MAX_ASC_BYTES*8)
2624
        {
2625
            faad_rewindbits(ld);
2626
            m = x1;
2627
            while(m>0)
2628
            {
2629
                n = min(m, 32);
2630
                faad_getbits(ld, n);
2631
                m -= n;
2632
            }
2633
2634
            i = 0;
2635
            m = latm->ASCbits = y1 - x1;
2636
            while(m > 0)
2637
            {
2638
                n = min(m, 8);
2639
                latm->ASC[i++] = (uint8_t) faad_getbits(ld, n);
2640
                m -= n;
2641
            }
2642
        }
2643
2644
        asc_bits = y1-x1;
2645
2646
        if(ascLen>asc_bits)
2647
            faad_getbits(ld, ascLen-asc_bits);
2648
2649
        latm->framelen_type = (uint8_t) faad_getbits(ld, 3);
2650
        if(latm->framelen_type == 0)
2651
        {
2652
            latm->frameLength = 0;
2653
            faad_getbits(ld, 8); //buffer fullness for frame_len_type==0, useless
2654
        }
2655
        else if(latm->framelen_type == 1)
2656
        {
2657
            latm->frameLength = faad_getbits(ld, 9);
2658
            if(latm->frameLength==0)
2659
            {
2660
                fprintf(stderr, "Invalid frameLength: 0\r\n");
2661
                return 0;
2662
            }
2663
            latm->frameLength = (latm->frameLength+20)*8;
2664
        }
2665
        else
2666
        {   //hellish CELP or HCVX stuff, discard
2667
            fprintf(stderr, "Unsupported CELP/HCVX framelentype: %d\n", latm->framelen_type);
2668
            return 0;
2669
        }
2670
2671
        latm->otherDataLenBits = 0;
2672
        if(faad_getbits(ld, 1))
2673
        {   //other data present
2674
            int esc, tmp;
2675
            if(latm->version)
2676
                latm->otherDataLenBits = latm_get_value(ld);
2677
            else do
2678
            {
2679
                esc = faad_getbits(ld, 1);
2680
                tmp = faad_getbits(ld, 8);
2681
                latm->otherDataLenBits = (latm->otherDataLenBits << 8) + tmp;
2682
            } while(esc);
2683
        }
2684
        if(faad_getbits(ld, 1)) //crc
2685
            faad_getbits(ld, 8);
2686
        latm->inited = 1;
2687
      }
2688
2689
      //read payload
2690
      if(latm->inited)
2691
          return latmParsePayload(latm, ld);
2692
      else
2693
          return 0;
2694
}
2695
2696
uint32_t faad_latm_frame(latm_header *latm, bitfile *ld)
2697
{
2698
    uint16_t len;
2699
    uint32_t initpos, endpos, firstpos, ret;
2700
2701
    firstpos = faad_get_processed_bits(ld);
2702
    while (ld->bytes_left)
2703
    {
2704
        faad_byte_align(ld);
2705
        if(faad_showbits(ld, 11) != 0x2B7)
2706
        {
2707
            faad_getbits(ld, 8);
2708
            continue;
2709
        }
2710
        faad_getbits(ld, 11);
2711
        len = faad_getbits(ld, 13);
2712
        if(!len)
2713
            continue;
2714
        initpos = faad_get_processed_bits(ld);
2715
        ret = latmAudioMuxElement(latm, ld);
2716
        endpos = faad_get_processed_bits(ld);
2717
        if(ret>0)
2718
            return (len*8)-(endpos-initpos);
2719
        //faad_getbits(ld, initpos-endpos); //go back to initpos, but is valid a getbits(-N) ?
2720
    }
2721
    return 0xFFFFFFFF;
2722
}
2723
#endif