Coverage Report

Created: 2026-10-17 06:34

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