~ubuntu-branches/debian/wheezy/vlc/wheezy

« back to all changes in this revision

Viewing changes to extras/faad2/libfaad/syntax.c

Tags: upstream-0.7.2.final
ImportĀ upstreamĀ versionĀ 0.7.2.final

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
 
3
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
 
4
**  
 
5
** This program is free software; you can redistribute it and/or modify
 
6
** it under the terms of the GNU General Public License as published by
 
7
** the Free Software Foundation; either version 2 of the License, or
 
8
** (at your option) any later version.
 
9
** 
 
10
** This program is distributed in the hope that it will be useful,
 
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
** GNU General Public License for more details.
 
14
** 
 
15
** You should have received a copy of the GNU General Public License
 
16
** along with this program; if not, write to the Free Software 
 
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
**
 
19
** Any non-GPL usage of this software or parts of this software is strictly
 
20
** forbidden.
 
21
**
 
22
** Commercial non-GPL licensing of this software is possible.
 
23
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 
24
**
 
25
** $Id: syntax.c,v 1.71 2004/02/04 19:55:03 menno Exp $
 
26
**/
 
27
 
 
28
/*
 
29
   Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
 
30
*/
 
31
 
 
32
#include "common.h"
 
33
#include "structs.h"
 
34
 
 
35
#include <stdlib.h>
 
36
#include <string.h>
 
37
 
 
38
#include "decoder.h"
 
39
#include "syntax.h"
 
40
#include "specrec.h"
 
41
#include "huffman.h"
 
42
#include "bits.h"
 
43
#include "pulse.h"
 
44
#include "analysis.h"
 
45
#include "drc.h"
 
46
#ifdef ERROR_RESILIENCE
 
47
#include "rvlc.h"
 
48
#endif
 
49
#ifdef SBR_DEC
 
50
#include "sbr_syntax.h"
 
51
#endif
 
52
 
 
53
 
 
54
/* static function declarations */
 
55
static void decode_sce_lfe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
 
56
                           uint8_t id_syn_ele);
 
57
static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
 
58
                       uint8_t id_syn_ele);
 
59
static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld,
 
60
                                          uint8_t channel, uint8_t *tag);
 
61
static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld,
 
62
                                    uint8_t channel, uint8_t *tag);
 
63
#ifdef COUPLING_DEC
 
64
static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld);
 
65
#endif
 
66
static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld);
 
67
static uint8_t program_config_element(program_config *pce, bitfile *ld);
 
68
static uint8_t fill_element(faacDecHandle hDecoder, bitfile *ld, drc_info *drc
 
69
#ifdef SBR_DEC
 
70
                            ,uint8_t sbr_ele
 
71
#endif
 
72
                            );
 
73
static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele,
 
74
                                         bitfile *ld, ic_stream *ics, uint8_t scal_flag,
 
75
                                         int16_t *spec_data);
 
76
static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
 
77
                        uint8_t common_window);
 
78
static uint8_t section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld);
 
79
static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld);
 
80
#ifdef SSR_DEC
 
81
static void gain_control_data(bitfile *ld, ic_stream *ics);
 
82
#endif
 
83
static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
 
84
                             int16_t *spectral_data);
 
85
static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count);
 
86
static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld);
 
87
static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld);
 
88
static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
 
89
static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
 
90
static void adts_variable_header(adts_header *adts, bitfile *ld);
 
91
static void adts_error_check(adts_header *adts, bitfile *ld);
 
92
static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
 
93
static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
 
94
#ifdef SCALABLE_DEC
 
95
static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
 
96
                                       bitfile *ld, uint8_t this_layer_stereo);
 
97
#endif
 
98
 
 
99
 
 
100
/* Table 4.4.1 */
 
101
int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
 
102
                        program_config *pce_out)
 
103
{
 
104
    program_config pce;
 
105
 
 
106
    /* 1024 or 960 */
 
107
    mp4ASC->frameLengthFlag = faad_get1bit(ld
 
108
        DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
 
109
#ifndef ALLOW_SMALL_FRAMELENGTH
 
110
    if (mp4ASC->frameLengthFlag == 1)
 
111
        return -3;
 
112
#endif
 
113
 
 
114
    mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
 
115
        DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
 
116
    if (mp4ASC->dependsOnCoreCoder == 1)
 
117
    {
 
118
        mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
 
119
            DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
 
120
    }
 
121
 
 
122
    mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
 
123
    if (mp4ASC->channelsConfiguration == 0)
 
124
    {
 
125
        if (program_config_element(&pce, ld))
 
126
            return -3;
 
127
        //mp4ASC->channelsConfiguration = pce.channels;
 
128
 
 
129
        if (pce_out != NULL)
 
130
            memcpy(pce_out, &pce, sizeof(program_config));
 
131
 
 
132
        /*
 
133
        if (pce.num_valid_cc_elements)
 
134
            return -3;
 
135
        */
 
136
    }
 
137
 
 
138
#ifdef ERROR_RESILIENCE
 
139
    if (mp4ASC->extensionFlag == 1)
 
140
    {
 
141
        /* Error resilience not supported yet */
 
142
        if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
 
143
        {
 
144
            mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
 
145
                DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
 
146
            mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
 
147
                DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
 
148
            mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
 
149
                DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
 
150
 
 
151
            /* 1 bit: extensionFlag3 */
 
152
        }
 
153
    }
 
154
#endif
 
155
 
 
156
    return 0;
 
157
}
 
158
 
 
159
/* Table 4.4.2 */
 
160
/* An MPEG-4 Audio decoder is only required to follow the Program
 
161
   Configuration Element in GASpecificConfig(). The decoder shall ignore
 
162
   any Program Configuration Elements that may occur in raw data blocks.
 
163
   PCEs transmitted in raw data blocks cannot be used to convey decoder
 
164
   configuration information.
 
165
*/
 
166
static uint8_t program_config_element(program_config *pce, bitfile *ld)
 
167
{
 
168
    uint8_t i;
 
169
 
 
170
    memset(pce, 0, sizeof(program_config));
 
171
 
 
172
    pce->channels = 0;
 
173
 
 
174
    pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
 
175
        DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
 
176
 
 
177
    pce->object_type = (uint8_t)faad_getbits(ld, 2
 
178
        DEBUGVAR(1,11,"program_config_element(): object_type"));
 
179
    pce->sf_index = (uint8_t)faad_getbits(ld, 4
 
180
        DEBUGVAR(1,12,"program_config_element(): sf_index"));
 
181
    pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
 
182
        DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
 
183
    pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
 
184
        DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
 
185
    pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
 
186
        DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
 
187
    pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
 
188
        DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
 
189
    pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
 
190
        DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
 
191
    pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
 
192
        DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
 
193
 
 
194
    pce->mono_mixdown_present = faad_get1bit(ld
 
195
        DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
 
196
    if (pce->mono_mixdown_present == 1)
 
197
    {
 
198
        pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
 
199
            DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
 
200
    }
 
201
 
 
202
    pce->stereo_mixdown_present = faad_get1bit(ld
 
203
        DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
 
204
    if (pce->stereo_mixdown_present == 1)
 
205
    {
 
206
        pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
 
207
            DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
 
208
    }
 
209
 
 
210
    pce->matrix_mixdown_idx_present = faad_get1bit(ld
 
211
        DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
 
212
    if (pce->matrix_mixdown_idx_present == 1)
 
213
    {
 
214
        pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
 
215
            DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
 
216
        pce->pseudo_surround_enable = faad_get1bit(ld
 
217
            DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
 
218
    }
 
219
 
 
220
    for (i = 0; i < pce->num_front_channel_elements; i++)
 
221
    {
 
222
        pce->front_element_is_cpe[i] = faad_get1bit(ld
 
223
            DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
 
224
        pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
 
225
            DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
 
226
 
 
227
        if (pce->front_element_is_cpe[i] & 1)
 
228
        {
 
229
            pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
 
230
            pce->num_front_channels += 2;
 
231
            pce->channels += 2;
 
232
        } else {
 
233
            pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
 
234
            pce->num_front_channels++;
 
235
            pce->channels++;
 
236
        }
 
237
    }
 
238
 
 
239
    for (i = 0; i < pce->num_side_channel_elements; i++)
 
240
    {
 
241
        pce->side_element_is_cpe[i] = faad_get1bit(ld
 
242
            DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
 
243
        pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
 
244
            DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
 
245
 
 
246
        if (pce->side_element_is_cpe[i] & 1)
 
247
        {
 
248
            pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
 
249
            pce->num_side_channels += 2;
 
250
            pce->channels += 2;
 
251
        } else {
 
252
            pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
 
253
            pce->num_side_channels++;
 
254
            pce->channels++;
 
255
        }
 
256
    }
 
257
 
 
258
    for (i = 0; i < pce->num_back_channel_elements; i++)
 
259
    {
 
260
        pce->back_element_is_cpe[i] = faad_get1bit(ld
 
261
            DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
 
262
        pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
 
263
            DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
 
264
 
 
265
        if (pce->back_element_is_cpe[i] & 1)
 
266
        {
 
267
            pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
 
268
            pce->channels += 2;
 
269
            pce->num_back_channels += 2;
 
270
        } else {
 
271
            pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
 
272
            pce->num_back_channels++;
 
273
            pce->channels++;
 
274
        }
 
275
    }
 
276
 
 
277
    for (i = 0; i < pce->num_lfe_channel_elements; i++)
 
278
    {
 
279
        pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
 
280
            DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
 
281
 
 
282
        pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
 
283
        pce->num_lfe_channels++;
 
284
        pce->channels++;
 
285
    }
 
286
 
 
287
    for (i = 0; i < pce->num_assoc_data_elements; i++)
 
288
        pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
 
289
        DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
 
290
 
 
291
    for (i = 0; i < pce->num_valid_cc_elements; i++)
 
292
    {
 
293
        pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
 
294
            DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
 
295
        pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
 
296
            DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
 
297
    }
 
298
 
 
299
    faad_byte_align(ld);
 
300
 
 
301
    pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
 
302
        DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
 
303
 
 
304
    for (i = 0; i < pce->comment_field_bytes; i++)
 
305
    {
 
306
        pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
 
307
            DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
 
308
    }
 
309
    pce->comment_field_data[i] = 0;
 
310
 
 
311
    if (pce->channels > MAX_CHANNELS)
 
312
        return 22;
 
313
 
 
314
    return 0;
 
315
}
 
316
 
 
317
static void decode_sce_lfe(faacDecHandle hDecoder,
 
318
                           faacDecFrameInfo *hInfo, bitfile *ld,
 
319
                           uint8_t id_syn_ele)
 
320
{
 
321
    uint8_t channels = hDecoder->fr_channels;
 
322
    uint8_t tag = 0;
 
323
 
 
324
    if (channels+1 > MAX_CHANNELS)
 
325
    {
 
326
        hInfo->error = 12;
 
327
        return;
 
328
    }
 
329
    if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
 
330
    {
 
331
        hInfo->error = 13;
 
332
        return;
 
333
    }
 
334
 
 
335
    /* for SCE hDecoder->element_output_channels[] is not set here because this
 
336
       can become 2 when some form of Parametric Stereo coding is used
 
337
    */
 
338
 
 
339
    /* save the syntax element id */
 
340
    hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
 
341
 
 
342
    /* decode the element */
 
343
    hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
 
344
 
 
345
    /* map output channels position to internal data channels */
 
346
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
 
347
    {
 
348
        /* this might be faulty when pce_set is true */
 
349
        hDecoder->internal_channel[channels] = channels;
 
350
        hDecoder->internal_channel[channels+1] = channels+1;
 
351
    } else {
 
352
        if (hDecoder->pce_set)
 
353
            hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
 
354
        else
 
355
            hDecoder->internal_channel[channels] = channels;
 
356
    }
 
357
 
 
358
    hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
 
359
    hDecoder->fr_ch_ele++;
 
360
}
 
361
 
 
362
static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
 
363
                       uint8_t id_syn_ele)
 
364
{
 
365
    uint8_t channels = hDecoder->fr_channels;
 
366
    uint8_t tag = 0;
 
367
 
 
368
    if (channels+2 > MAX_CHANNELS)
 
369
    {
 
370
        hInfo->error = 12;
 
371
        return;
 
372
    }
 
373
    if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
 
374
    {
 
375
        hInfo->error = 13;
 
376
        return;
 
377
    }
 
378
 
 
379
    /* for CPE the number of output channels is always 2 */
 
380
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
 
381
    {
 
382
        /* element_output_channels not set yet */
 
383
        hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
 
384
    } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
 
385
        /* element inconsistency */
 
386
        hInfo->error = 21;
 
387
        return;
 
388
    }
 
389
 
 
390
    /* save the syntax element id */
 
391
    hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
 
392
 
 
393
    /* decode the element */
 
394
    hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
 
395
 
 
396
    /* map output channel position to internal data channels */
 
397
    if (hDecoder->pce_set)
 
398
    {
 
399
        hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
 
400
        hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
 
401
    } else {
 
402
        hDecoder->internal_channel[channels] = channels;
 
403
        hDecoder->internal_channel[channels+1] = channels+1;
 
404
    }
 
405
 
 
406
    hDecoder->fr_channels += 2;
 
407
    hDecoder->fr_ch_ele++;
 
408
}
 
409
 
 
410
void raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
 
411
                    bitfile *ld, program_config *pce, drc_info *drc)
 
412
{
 
413
    uint8_t id_syn_ele;
 
414
 
 
415
    hDecoder->fr_channels = 0;
 
416
    hDecoder->fr_ch_ele = 0;
 
417
    hDecoder->first_syn_ele = 25;
 
418
    hDecoder->has_lfe = 0;
 
419
 
 
420
#ifdef ERROR_RESILIENCE
 
421
    if (hDecoder->object_type < ER_OBJECT_START)
 
422
    {
 
423
#endif
 
424
        /* Table 4.4.3: raw_data_block() */
 
425
        while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID
 
426
            DEBUGVAR(1,4,"faacDecDecode(): id_syn_ele"))) != ID_END)
 
427
        {
 
428
            switch (id_syn_ele) {
 
429
            case ID_SCE:
 
430
                if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
 
431
                decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
 
432
                if (hInfo->error > 0)
 
433
                    return;
 
434
                break;
 
435
            case ID_CPE:
 
436
                if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
 
437
                decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
 
438
                if (hInfo->error > 0)
 
439
                    return;
 
440
                break;
 
441
            case ID_LFE:
 
442
                hDecoder->has_lfe++;
 
443
                decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
 
444
                if (hInfo->error > 0)
 
445
                    return;
 
446
                break;
 
447
            case ID_CCE: /* not implemented yet, but skip the bits */
 
448
#ifdef COUPLING_DEC
 
449
                hInfo->error = coupling_channel_element(hDecoder, ld);
 
450
#else
 
451
                hInfo->error = 6;
 
452
#endif
 
453
                if (hInfo->error > 0)
 
454
                    return;
 
455
                break;
 
456
            case ID_DSE:
 
457
                data_stream_element(hDecoder, ld);
 
458
                break;
 
459
            case ID_PCE:
 
460
                /* 14496-4: 5.6.4.1.2.1.3: */
 
461
                /* program_configuration_element()'s in access units shall be ignored */
 
462
                program_config_element(pce, ld);
 
463
                //if ((hInfo->error = program_config_element(pce, ld)) > 0)
 
464
                //    return;
 
465
                //hDecoder->pce_set = 1;
 
466
                break;
 
467
            case ID_FIL:
 
468
                /* one sbr_info describes a channel_element not a channel! */
 
469
                /* if we encounter SBR data here: error */
 
470
                /* SBR data will be read directly in the SCE/LFE/CPE element */
 
471
                if ((hInfo->error = fill_element(hDecoder, ld, drc
 
472
#ifdef SBR_DEC
 
473
                    , INVALID_SBR_ELEMENT
 
474
#endif
 
475
                    )) > 0)
 
476
                    return;
 
477
                break;
 
478
            }
 
479
        }
 
480
#ifdef ERROR_RESILIENCE
 
481
    } else {
 
482
        /* Table 262: er_raw_data_block() */
 
483
        switch (hDecoder->channelConfiguration)
 
484
        {
 
485
        case 1:
 
486
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
487
            if (hInfo->error > 0)
 
488
                return;
 
489
            break;
 
490
        case 2:
 
491
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
492
            if (hInfo->error > 0)
 
493
                return;
 
494
            break;
 
495
        case 3:
 
496
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
497
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
498
            if (hInfo->error > 0)
 
499
                return;
 
500
            break;
 
501
        case 4:
 
502
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
503
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
504
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
505
            if (hInfo->error > 0)
 
506
                return;
 
507
            break;
 
508
        case 5:
 
509
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
510
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
511
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
512
            if (hInfo->error > 0)
 
513
                return;
 
514
            break;
 
515
        case 6:
 
516
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
517
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
518
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
519
            decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
 
520
            if (hInfo->error > 0)
 
521
                return;
 
522
            break;
 
523
        case 7: /* 8 channels */
 
524
            decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
 
525
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
526
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
527
            decode_cpe(hDecoder, hInfo, ld, ID_CPE);
 
528
            decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
 
529
            if (hInfo->error > 0)
 
530
                return;
 
531
            break;
 
532
        default:
 
533
            hInfo->error = 7;
 
534
            return;
 
535
        }
 
536
#if 0
 
537
        cnt = bits_to_decode() / 8;
 
538
        while (cnt >= 1)
 
539
        {
 
540
            cnt -= extension_payload(cnt);
 
541
        }
 
542
#endif
 
543
    }
 
544
#endif
 
545
 
 
546
    /* new in corrigendum 14496-3:2002 */
 
547
#ifdef DRM
 
548
    if (hDecoder->object_type != DRM_ER_LC)
 
549
#endif
 
550
    {
 
551
        faad_byte_align(ld);
 
552
    }
 
553
 
 
554
    return;
 
555
}
 
556
 
 
557
/* Table 4.4.4 and */
 
558
/* Table 4.4.9 */
 
559
static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld,
 
560
                                          uint8_t channel, uint8_t *tag)
 
561
{
 
562
    uint8_t retval = 0;
 
563
    element sce = {0};
 
564
    ic_stream *ics = &(sce.ics1);
 
565
    ALIGN int16_t spec_data[1024] = {0};
 
566
 
 
567
    sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
 
568
        DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
 
569
 
 
570
    *tag = sce.element_instance_tag;
 
571
    sce.channel = channel;
 
572
    sce.paired_channel = -1;
 
573
 
 
574
    retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
 
575
    if (retval > 0)
 
576
        return retval;
 
577
 
 
578
#ifdef SBR_DEC
 
579
    /* check if next bitstream element is a fill element */
 
580
    /* if so, read it now so SBR decoding can be done in case of a file with SBR */
 
581
    if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
 
582
    {
 
583
        faad_flushbits(ld, LEN_SE_ID);
 
584
 
 
585
        /* one sbr_info describes a channel_element not a channel! */
 
586
        if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
 
587
        {
 
588
            return retval;
 
589
        }
 
590
    }
 
591
#endif
 
592
 
 
593
    /* noiseless coding is done, spectral reconstruction is done now */
 
594
    retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
 
595
    if (retval > 0)
 
596
        return retval;
 
597
 
 
598
    return 0;
 
599
}
 
600
 
 
601
/* Table 4.4.5 */
 
602
static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld,
 
603
                                    uint8_t channels, uint8_t *tag)
 
604
{
 
605
    ALIGN int16_t spec_data1[1024] = {0};
 
606
    ALIGN int16_t spec_data2[1024] = {0};
 
607
    element cpe = {0};
 
608
    ic_stream *ics1 = &(cpe.ics1);
 
609
    ic_stream *ics2 = &(cpe.ics2);
 
610
    uint8_t result;
 
611
 
 
612
    cpe.channel        = channels;
 
613
    cpe.paired_channel = channels+1;
 
614
 
 
615
    cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
 
616
        DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
 
617
    *tag = cpe.element_instance_tag;
 
618
 
 
619
    if ((cpe.common_window = faad_get1bit(ld
 
620
        DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
 
621
    {
 
622
        /* both channels have common ics information */
 
623
        if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
 
624
            return result;
 
625
 
 
626
        ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
 
627
            DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
 
628
        if (ics1->ms_mask_present == 1)
 
629
        {
 
630
            uint8_t g, sfb;
 
631
            for (g = 0; g < ics1->num_window_groups; g++)
 
632
            {
 
633
                for (sfb = 0; sfb < ics1->max_sfb; sfb++)
 
634
                {
 
635
                    ics1->ms_used[g][sfb] = faad_get1bit(ld
 
636
                        DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
 
637
                }
 
638
            }
 
639
        }
 
640
 
 
641
#ifdef ERROR_RESILIENCE
 
642
        if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
 
643
        {
 
644
            if ((ics1->ltp.data_present = faad_get1bit(ld
 
645
                DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
 
646
            {
 
647
                if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
 
648
                {
 
649
                    return result;
 
650
                }
 
651
            }
 
652
        }
 
653
#endif
 
654
 
 
655
        memcpy(ics2, ics1, sizeof(ic_stream));
 
656
    } else {
 
657
        ics1->ms_mask_present = 0;
 
658
    }
 
659
 
 
660
    if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
 
661
        0, spec_data1)) > 0)
 
662
    {
 
663
        return result;
 
664
    }
 
665
 
 
666
#ifdef ERROR_RESILIENCE
 
667
    if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
 
668
        (ics1->predictor_data_present))
 
669
    {
 
670
        if ((ics1->ltp2.data_present = faad_get1bit(ld
 
671
            DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
 
672
        {
 
673
            if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
 
674
            {
 
675
                return result;
 
676
            }
 
677
        }
 
678
    }
 
679
#endif
 
680
 
 
681
    if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
 
682
        0, spec_data2)) > 0)
 
683
    {
 
684
        return result;
 
685
    }
 
686
 
 
687
#ifdef SBR_DEC
 
688
    /* check if next bitstream element is a fill element */
 
689
    /* if so, read it now so SBR decoding can be done in case of a file with SBR */
 
690
    if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
 
691
    {
 
692
        faad_flushbits(ld, LEN_SE_ID);
 
693
 
 
694
        /* one sbr_info describes a channel_element not a channel! */
 
695
        if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
 
696
        {
 
697
            return result;
 
698
        }
 
699
    }
 
700
#endif
 
701
 
 
702
    /* noiseless coding is done, spectral reconstruction is done now */
 
703
    if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
 
704
        spec_data1, spec_data2)) > 0)
 
705
    {
 
706
        return result;
 
707
    }
 
708
 
 
709
    return 0;
 
710
}
 
711
 
 
712
/* Table 4.4.6 */
 
713
static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
 
714
                        uint8_t common_window)
 
715
{
 
716
    uint8_t retval = 0;
 
717
 
 
718
    /* ics->ics_reserved_bit = */ faad_get1bit(ld
 
719
        DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
 
720
    ics->window_sequence = (uint8_t)faad_getbits(ld, 2
 
721
        DEBUGVAR(1,44,"ics_info(): window_sequence"));
 
722
    ics->window_shape = faad_get1bit(ld
 
723
        DEBUGVAR(1,45,"ics_info(): window_shape"));
 
724
 
 
725
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
 
726
    {
 
727
        ics->max_sfb = (uint8_t)faad_getbits(ld, 4
 
728
            DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
 
729
        ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
 
730
            DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
 
731
    } else {
 
732
        ics->max_sfb = (uint8_t)faad_getbits(ld, 6
 
733
            DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
 
734
    }
 
735
 
 
736
    /* get the grouping information */
 
737
    if ((retval = window_grouping_info(hDecoder, ics)) > 0)
 
738
        return retval;
 
739
 
 
740
    /* should be an error */
 
741
    /* check the range of max_sfb */
 
742
    if (ics->max_sfb > ics->num_swb)
 
743
        return 16;
 
744
 
 
745
    if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
 
746
    {
 
747
        if ((ics->predictor_data_present = faad_get1bit(ld
 
748
            DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
 
749
        {
 
750
            if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
 
751
            {
 
752
                uint8_t sfb;
 
753
 
 
754
                ics->pred.limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
 
755
 
 
756
                if ((ics->pred.predictor_reset = faad_get1bit(ld
 
757
                    DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
 
758
                {
 
759
                    ics->pred.predictor_reset_group_number = (uint8_t)faad_getbits(ld, 5
 
760
                        DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
 
761
                }
 
762
 
 
763
                for (sfb = 0; sfb < ics->pred.limit; sfb++)
 
764
                {
 
765
                    ics->pred.prediction_used[sfb] = faad_get1bit(ld
 
766
                        DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
 
767
                }
 
768
            }
 
769
#ifdef LTP_DEC
 
770
            else { /* Long Term Prediction */
 
771
                if (hDecoder->object_type < ER_OBJECT_START)
 
772
                {
 
773
                    if ((ics->ltp.data_present = faad_get1bit(ld
 
774
                        DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
 
775
                    {
 
776
                        if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
 
777
                        {
 
778
                            return retval;
 
779
                        }
 
780
                    }
 
781
                    if (common_window)
 
782
                    {
 
783
                        if ((ics->ltp2.data_present = faad_get1bit(ld
 
784
                            DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
 
785
                        {
 
786
                            if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
 
787
                            {
 
788
                                return retval;
 
789
                            }
 
790
                        }
 
791
                    }
 
792
                }
 
793
#ifdef ERROR_RESILIENCE
 
794
                if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
 
795
                {
 
796
                    if ((ics->ltp.data_present = faad_get1bit(ld
 
797
                        DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
 
798
                    {
 
799
                        ltp_data(hDecoder, ics, &(ics->ltp), ld);
 
800
                    }
 
801
                }
 
802
#endif
 
803
            }
 
804
#endif
 
805
        }
 
806
    }
 
807
 
 
808
    return retval;
 
809
}
 
810
 
 
811
/* Table 4.4.7 */
 
812
static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
 
813
{
 
814
    uint8_t i;
 
815
 
 
816
    pul->number_pulse = (uint8_t)faad_getbits(ld, 2
 
817
        DEBUGVAR(1,56,"pulse_data(): number_pulse"));
 
818
    pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
 
819
        DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
 
820
 
 
821
    /* check the range of pulse_start_sfb */
 
822
    if (pul->pulse_start_sfb > ics->num_swb)
 
823
        return 16;
 
824
 
 
825
    for (i = 0; i < pul->number_pulse+1; i++)
 
826
    {
 
827
        pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
 
828
            DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
 
829
        pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
 
830
            DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
 
831
    }
 
832
 
 
833
    return 0;
 
834
}
 
835
 
 
836
#ifdef COUPLING_DEC
 
837
/* Table 4.4.8: Currently just for skipping the bits... */
 
838
static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld)
 
839
{
 
840
    uint8_t c, result = 0;
 
841
    uint8_t ind_sw_cce_flag = 0;
 
842
    uint8_t num_gain_element_lists = 0;
 
843
    uint8_t num_coupled_elements = 0;
 
844
 
 
845
    element el_empty = {0};
 
846
    ic_stream ics_empty = {0};
 
847
    int16_t sh_data[1024];
 
848
 
 
849
    c = faad_getbits(ld, LEN_TAG
 
850
        DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
 
851
 
 
852
    ind_sw_cce_flag = faad_get1bit(ld
 
853
        DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
 
854
    num_coupled_elements = faad_getbits(ld, 3
 
855
        DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
 
856
 
 
857
    for (c = 0; c < num_coupled_elements + 1; c++)
 
858
    {
 
859
        uint8_t cc_target_is_cpe, cc_target_tag_select;
 
860
 
 
861
        num_gain_element_lists++;
 
862
 
 
863
        cc_target_is_cpe = faad_get1bit(ld
 
864
            DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
 
865
        cc_target_tag_select = faad_getbits(ld, 4
 
866
            DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
 
867
 
 
868
        if (cc_target_is_cpe)
 
869
        {
 
870
            uint8_t cc_l = faad_get1bit(ld
 
871
                DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
 
872
            uint8_t cc_r = faad_get1bit(ld
 
873
                DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
 
874
 
 
875
            if (cc_l && cc_r)
 
876
                num_gain_element_lists++;
 
877
        }
 
878
    }
 
879
 
 
880
    faad_get1bit(ld
 
881
        DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
 
882
    faad_get1bit(ld
 
883
        DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
 
884
    faad_getbits(ld, 2
 
885
        DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
 
886
 
 
887
    if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
 
888
        0, sh_data)) > 0)
 
889
    {
 
890
        return result;
 
891
    }
 
892
 
 
893
    for (c = 1; c < num_gain_element_lists; c++)
 
894
    {
 
895
        uint8_t cge;
 
896
 
 
897
        if (ind_sw_cce_flag)
 
898
        {
 
899
            cge = 1;
 
900
        } else {
 
901
            cge = faad_get1bit(ld
 
902
                DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
 
903
        }
 
904
 
 
905
        if (cge)
 
906
        {
 
907
            huffman_scale_factor(ld);
 
908
        } else {
 
909
            uint8_t g, sfb;
 
910
 
 
911
            for (g = 0; g < ics_empty.num_window_groups; g++)
 
912
            {
 
913
                for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
 
914
                {
 
915
                    if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
 
916
                        huffman_scale_factor(ld);
 
917
                }
 
918
            }
 
919
        }
 
920
    }
 
921
 
 
922
    return 0;
 
923
}
 
924
#endif
 
925
 
 
926
/* Table 4.4.10 */
 
927
static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld)
 
928
{
 
929
    uint8_t byte_aligned;
 
930
    uint16_t i, count;
 
931
 
 
932
    /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
 
933
        DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
 
934
    byte_aligned = faad_get1bit(ld
 
935
        DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
 
936
    count = (uint16_t)faad_getbits(ld, 8
 
937
        DEBUGVAR(1,62,"data_stream_element(): count"));
 
938
    if (count == 255)
 
939
    {
 
940
        count += (uint16_t)faad_getbits(ld, 8
 
941
            DEBUGVAR(1,63,"data_stream_element(): extra count"));
 
942
    }
 
943
    if (byte_aligned)
 
944
        faad_byte_align(ld);
 
945
 
 
946
    for (i = 0; i < count; i++)
 
947
    {
 
948
        faad_getbits(ld, LEN_BYTE
 
949
            DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
 
950
    }
 
951
 
 
952
    return count;
 
953
}
 
954
 
 
955
/* Table 4.4.11 */
 
956
static uint8_t fill_element(faacDecHandle hDecoder, bitfile *ld, drc_info *drc
 
957
#ifdef SBR_DEC
 
958
                            ,uint8_t sbr_ele
 
959
#endif
 
960
                            )
 
961
{
 
962
    uint16_t count;
 
963
#ifdef SBR_DEC
 
964
    uint8_t bs_extension_type;
 
965
#endif
 
966
 
 
967
    count = (uint16_t)faad_getbits(ld, 4
 
968
        DEBUGVAR(1,65,"fill_element(): count"));
 
969
    if (count == 15)
 
970
    {
 
971
        count += (uint16_t)faad_getbits(ld, 8
 
972
            DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
 
973
    }
 
974
 
 
975
    if (count > 0)
 
976
    {
 
977
#ifdef SBR_DEC
 
978
        bs_extension_type = (uint8_t)faad_showbits(ld, 4);
 
979
 
 
980
        if ((bs_extension_type == EXT_SBR_DATA) ||
 
981
            (bs_extension_type == EXT_SBR_DATA_CRC))
 
982
        {
 
983
            if (sbr_ele == INVALID_SBR_ELEMENT)
 
984
                return 24;
 
985
 
 
986
            if (!hDecoder->sbr[sbr_ele])
 
987
            {
 
988
                hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
 
989
                    hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index)
 
990
#ifdef DRM
 
991
                    , 0
 
992
#endif
 
993
                    );
 
994
            }
 
995
 
 
996
            hDecoder->sbr_present_flag = 1;
 
997
 
 
998
            /* parse the SBR data */
 
999
            hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
 
1000
#if (defined(PS_DEC) || defined(DRM_PS))
 
1001
            if (hDecoder->sbr[sbr_ele]->ps_used)
 
1002
            {
 
1003
                hDecoder->ps_used[sbr_ele] = 1;
 
1004
            }
 
1005
#endif
 
1006
        } else {
 
1007
#endif
 
1008
            while (count > 0)
 
1009
            {
 
1010
                count -= extension_payload(ld, drc, count);
 
1011
            }
 
1012
#ifdef SBR_DEC
 
1013
        }
 
1014
#endif
 
1015
    }
 
1016
 
 
1017
    return 0;
 
1018
}
 
1019
 
 
1020
/* Table 4.4.12 */
 
1021
#ifdef SSR_DEC
 
1022
static void gain_control_data(bitfile *ld, ic_stream *ics)
 
1023
{
 
1024
    uint8_t bd, wd, ad;
 
1025
    ssr_info *ssr = &(ics->ssr);
 
1026
 
 
1027
    ssr->max_band = (uint8_t)faad_getbits(ld, 2
 
1028
        DEBUGVAR(1,1000,"gain_control_data(): max_band"));
 
1029
 
 
1030
    if (ics->window_sequence == ONLY_LONG_SEQUENCE)
 
1031
    {
 
1032
        for (bd = 1; bd <= ssr->max_band; bd++)
 
1033
        {
 
1034
            for (wd = 0; wd < 1; wd++)
 
1035
            {
 
1036
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
 
1037
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
 
1038
 
 
1039
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
 
1040
                {
 
1041
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
 
1042
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
 
1043
                    ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
 
1044
                        DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
 
1045
                }
 
1046
            }
 
1047
        }
 
1048
    } else if (ics->window_sequence == LONG_START_SEQUENCE) {
 
1049
        for (bd = 1; bd <= ssr->max_band; bd++)
 
1050
        {
 
1051
            for (wd = 0; wd < 2; wd++)
 
1052
            {
 
1053
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
 
1054
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
 
1055
 
 
1056
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
 
1057
                {
 
1058
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
 
1059
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
 
1060
                    if (wd == 0)
 
1061
                    {
 
1062
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
 
1063
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
 
1064
                    } else {
 
1065
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
 
1066
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
 
1067
                    }
 
1068
                }
 
1069
            }
 
1070
        }
 
1071
    } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
 
1072
        for (bd = 1; bd <= ssr->max_band; bd++)
 
1073
        {
 
1074
            for (wd = 0; wd < 8; wd++)
 
1075
            {
 
1076
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
 
1077
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
 
1078
 
 
1079
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
 
1080
                {
 
1081
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
 
1082
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
 
1083
                    ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
 
1084
                        DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
 
1085
                }
 
1086
            }
 
1087
        }
 
1088
    } else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
 
1089
        for (bd = 1; bd <= ssr->max_band; bd++)
 
1090
        {
 
1091
            for (wd = 0; wd < 2; wd++)
 
1092
            {
 
1093
                ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
 
1094
                    DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
 
1095
 
 
1096
                for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
 
1097
                {
 
1098
                    ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
 
1099
                        DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
 
1100
 
 
1101
                    if (wd == 0)
 
1102
                    {
 
1103
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
 
1104
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
 
1105
                    } else {
 
1106
                        ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
 
1107
                            DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
 
1108
                    }
 
1109
                }
 
1110
            }
 
1111
        }
 
1112
    }
 
1113
}
 
1114
#endif
 
1115
 
 
1116
#ifdef SCALABLE_DEC
 
1117
/* Table 4.4.13 ASME */
 
1118
void aac_scalable_main_element(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
 
1119
                               bitfile *ld, program_config *pce, drc_info *drc)
 
1120
{
 
1121
    uint8_t retval = 0;
 
1122
    uint8_t channels = hDecoder->fr_channels = 0;
 
1123
    uint8_t ch;
 
1124
    uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
 
1125
    element cpe = {0};
 
1126
    ic_stream *ics1 = &(cpe.ics1);
 
1127
    ic_stream *ics2 = &(cpe.ics2);
 
1128
    int16_t *spec_data;
 
1129
    ALIGN int16_t spec_data1[1024] = {0};
 
1130
    ALIGN int16_t spec_data2[1024] = {0};
 
1131
 
 
1132
    hDecoder->fr_ch_ele = 0;
 
1133
 
 
1134
    hInfo->error = aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
 
1135
    if (hInfo->error > 0)
 
1136
        return;
 
1137
 
 
1138
    cpe.common_window = 1;
 
1139
    if (this_layer_stereo)
 
1140
        cpe.ele_id = ID_CPE;
 
1141
    else
 
1142
        cpe.ele_id = ID_SCE;
 
1143
 
 
1144
    hDecoder->element_output_channels[hDecoder->fr_ch_ele] = (this_layer_stereo ? 2 : 0);
 
1145
 
 
1146
    for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
 
1147
    {
 
1148
        ic_stream *ics;
 
1149
        if (ch == 0)
 
1150
        {
 
1151
            ics = ics1;
 
1152
            spec_data = spec_data1;
 
1153
        } else {
 
1154
            ics = ics2;
 
1155
            spec_data = spec_data2;
 
1156
        }
 
1157
 
 
1158
        hInfo->error = individual_channel_stream(hDecoder, &cpe, ld, ics, 1, spec_data);
 
1159
        if (hInfo->error > 0)
 
1160
            return;
 
1161
    }
 
1162
 
 
1163
#ifdef DRM
 
1164
#ifdef SBR_DEC
 
1165
    /* In case of DRM we need to read the SBR info before channel reconstruction */
 
1166
    if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
 
1167
    {
 
1168
        bitfile ld_sbr = {0};
 
1169
        uint32_t i;
 
1170
        uint16_t count = 0;
 
1171
        uint8_t *revbuffer;
 
1172
        uint8_t *prevbufstart;
 
1173
        uint8_t *pbufend;
 
1174
 
 
1175
        /* all forward bitreading should be finished at this point */
 
1176
        uint32_t bitsconsumed = faad_get_processed_bits(ld);
 
1177
        uint32_t buffer_size = faad_origbitbuffer_size(ld);
 
1178
        uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
 
1179
 
 
1180
        if (bitsconsumed + 8 > buffer_size*8)
 
1181
        {
 
1182
            hInfo->error = 14;
 
1183
            return;
 
1184
        }
 
1185
 
 
1186
        if (!hDecoder->sbr[0])
 
1187
        {
 
1188
            hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, cpe.ele_id,
 
1189
                2*get_sample_rate(hDecoder->sf_index), 1);
 
1190
        }
 
1191
 
 
1192
        /* Reverse bit reading of SBR data in DRM audio frame */
 
1193
        revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
 
1194
        prevbufstart = revbuffer;
 
1195
        pbufend = &buffer[buffer_size - 1];
 
1196
        for (i = 0; i < buffer_size; i++)
 
1197
            *prevbufstart++ = tabFlipbits[*pbufend--];
 
1198
 
 
1199
        /* Set SBR data */
 
1200
        /* consider 8 bits from AAC-CRC */
 
1201
        count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
 
1202
        faad_initbits(&ld_sbr, revbuffer, count);
 
1203
 
 
1204
        hDecoder->sbr[0]->lcstereo_flag = hDecoder->lcstereo_flag;
 
1205
 
 
1206
        hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
 
1207
        hDecoder->sbr[0]->sample_rate *= 2;
 
1208
 
 
1209
        faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
 
1210
 
 
1211
        hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
 
1212
#if (defined(PS_DEC) || defined(DRM_PS))
 
1213
        if (hDecoder->sbr[0]->ps_used)
 
1214
        {
 
1215
            hDecoder->ps_used[0] = 1;
 
1216
        }
 
1217
#endif
 
1218
 
 
1219
        /* check CRC */
 
1220
        /* no need to check it if there was already an error */
 
1221
        if (hDecoder->sbr[0]->ret == 0)
 
1222
            hDecoder->sbr[0]->ret = faad_check_CRC(&ld_sbr, faad_get_processed_bits(&ld_sbr) - 8);
 
1223
 
 
1224
        faad_endbits(&ld_sbr);
 
1225
 
 
1226
        if (revbuffer)
 
1227
            faad_free(revbuffer);
 
1228
    }
 
1229
#endif
 
1230
#endif
 
1231
 
 
1232
    if (this_layer_stereo)
 
1233
    {
 
1234
        hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
 
1235
        if (hInfo->error > 0)
 
1236
            return;
 
1237
    } else {
 
1238
        hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
 
1239
        if (hInfo->error > 0)
 
1240
            return;
 
1241
    }
 
1242
 
 
1243
    /* map output channels position to internal data channels */
 
1244
    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
 
1245
    {
 
1246
        /* this might be faulty when pce_set is true */
 
1247
        hDecoder->internal_channel[channels] = channels;
 
1248
        hDecoder->internal_channel[channels+1] = channels+1;
 
1249
    } else {
 
1250
        hDecoder->internal_channel[channels] = channels;
 
1251
    }
 
1252
 
 
1253
    hDecoder->element_id[hDecoder->fr_ch_ele] = cpe.ele_id;
 
1254
 
 
1255
    hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
 
1256
    hDecoder->fr_ch_ele++;
 
1257
 
 
1258
    return;
 
1259
}
 
1260
 
 
1261
/* Table 4.4.15 */
 
1262
static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
 
1263
                                       bitfile *ld, uint8_t this_layer_stereo)
 
1264
{
 
1265
    uint8_t retval = 0;
 
1266
    uint8_t ch;
 
1267
    ic_stream *ics;
 
1268
 
 
1269
    /* ics1->ics_reserved_bit = */ faad_get1bit(ld
 
1270
        DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
 
1271
    ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
 
1272
        DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
 
1273
    ics1->window_shape = faad_get1bit(ld
 
1274
        DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
 
1275
 
 
1276
    if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
 
1277
    {
 
1278
        ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
 
1279
            DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
 
1280
        ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
 
1281
            DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
 
1282
    } else {
 
1283
        ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
 
1284
            DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
 
1285
    }
 
1286
 
 
1287
    /* get the grouping information */
 
1288
    if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
 
1289
        return retval;
 
1290
 
 
1291
    /* should be an error */
 
1292
    /* check the range of max_sfb */
 
1293
    if (ics1->max_sfb > ics1->num_swb)
 
1294
        return 16;
 
1295
 
 
1296
    if (this_layer_stereo)
 
1297
    {
 
1298
        ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
 
1299
            DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
 
1300
        if (ics1->ms_mask_present == 1)
 
1301
        {
 
1302
            uint8_t g, sfb;
 
1303
            for (g = 0; g < ics1->num_window_groups; g++)
 
1304
            {
 
1305
                for (sfb = 0; sfb < ics1->max_sfb; sfb++)
 
1306
                {
 
1307
                    ics1->ms_used[g][sfb] = faad_get1bit(ld
 
1308
                        DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
 
1309
                }
 
1310
            }
 
1311
        }
 
1312
 
 
1313
        memcpy(ics2, ics1, sizeof(ic_stream));
 
1314
    } else {
 
1315
        ics1->ms_mask_present = 0;
 
1316
    }
 
1317
 
 
1318
    if (0)
 
1319
    {
 
1320
        faad_get1bit(ld
 
1321
            DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer"));
 
1322
    }
 
1323
 
 
1324
    for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
 
1325
    {
 
1326
        if (ch == 0)
 
1327
            ics = ics1;
 
1328
        else
 
1329
            ics = ics2;
 
1330
 
 
1331
        if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/)
 
1332
        {
 
1333
            if ((ics->tns_data_present = faad_get1bit(ld
 
1334
                DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1)
 
1335
            {
 
1336
#ifdef DRM
 
1337
                /* different order of data units in DRM */
 
1338
                if (hDecoder->object_type != DRM_ER_LC)
 
1339
#endif
 
1340
                {
 
1341
                    tns_data(ics, &(ics->tns), ld);
 
1342
                }
 
1343
            }
 
1344
        }
 
1345
#if 0
 
1346
        if (0 /*core_flag || tvq_layer_pesent*/)
 
1347
        {
 
1348
            if ((ch==0) || ((ch==1) && (core_stereo || tvq_stereo))
 
1349
                diff_control_data();
 
1350
            if (mono_stereo_flag)
 
1351
                diff_control_data_lr();
 
1352
        } else {
 
1353
#endif
 
1354
            if ((ics->ltp.data_present = faad_get1bit(ld
 
1355
                DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1)
 
1356
            {
 
1357
                if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
 
1358
                {
 
1359
                    return retval;
 
1360
                }
 
1361
            }
 
1362
#if 0
 
1363
        }
 
1364
#endif
 
1365
    }
 
1366
 
 
1367
    return 0;
 
1368
}
 
1369
#endif
 
1370
 
 
1371
/* Table 4.4.24 */
 
1372
static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele,
 
1373
                                         bitfile *ld, ic_stream *ics, uint8_t scal_flag,
 
1374
                                         int16_t *spec_data)
 
1375
{
 
1376
    uint8_t result;
 
1377
 
 
1378
    ics->global_gain = (uint8_t)faad_getbits(ld, 8
 
1379
        DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
 
1380
 
 
1381
    if (!ele->common_window && !scal_flag)
 
1382
    {
 
1383
        if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
 
1384
            return result;
 
1385
    }
 
1386
 
 
1387
    if ((result = section_data(hDecoder, ics, ld)) > 0)
 
1388
        return result;
 
1389
 
 
1390
    if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
 
1391
        return result;
 
1392
 
 
1393
    if (!scal_flag)
 
1394
    {
 
1395
        /**
 
1396
         **  NOTE: It could be that pulse data is available in scalable AAC too,
 
1397
         **        as said in Amendment 1, this could be only the case for ER AAC,
 
1398
         **        though. (have to check this out later)
 
1399
         **/
 
1400
        /* get pulse data */
 
1401
        if ((ics->pulse_data_present = faad_get1bit(ld
 
1402
            DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
 
1403
        {
 
1404
            if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
 
1405
                return result;
 
1406
        }
 
1407
 
 
1408
        /* get tns data */
 
1409
        if ((ics->tns_data_present = faad_get1bit(ld
 
1410
            DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
 
1411
        {
 
1412
#ifdef ERROR_RESILIENCE
 
1413
            if (hDecoder->object_type < ER_OBJECT_START)
 
1414
#endif
 
1415
                tns_data(ics, &(ics->tns), ld);
 
1416
        }
 
1417
 
 
1418
        /* get gain control data */
 
1419
        if ((ics->gain_control_data_present = faad_get1bit(ld
 
1420
            DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
 
1421
        {
 
1422
#ifdef SSR_DEC
 
1423
            if (hDecoder->object_type != SSR)
 
1424
                return 1;
 
1425
            else
 
1426
                gain_control_data(ld, ics);
 
1427
#else
 
1428
            return 1;
 
1429
#endif
 
1430
        }
 
1431
    }
 
1432
 
 
1433
#ifdef ERROR_RESILIENCE
 
1434
    if (hDecoder->aacSpectralDataResilienceFlag)
 
1435
    {
 
1436
        ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
 
1437
            DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
 
1438
 
 
1439
        if (hDecoder->channelConfiguration == 2)
 
1440
        {
 
1441
            if (ics->length_of_reordered_spectral_data > 6144)
 
1442
                ics->length_of_reordered_spectral_data = 6144;
 
1443
        } else {
 
1444
            if (ics->length_of_reordered_spectral_data > 12288)
 
1445
                ics->length_of_reordered_spectral_data = 12288;
 
1446
        }
 
1447
 
 
1448
        ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6
 
1449
            DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
 
1450
        if (ics->length_of_longest_codeword >= 49)
 
1451
            ics->length_of_longest_codeword = 49;
 
1452
    }
 
1453
 
 
1454
    /* RVLC spectral data is put here */
 
1455
    if (hDecoder->aacScalefactorDataResilienceFlag)
 
1456
    {
 
1457
        if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
 
1458
            return result;
 
1459
    }
 
1460
 
 
1461
    if (hDecoder->object_type >= ER_OBJECT_START) 
 
1462
    {
 
1463
        if (ics->tns_data_present)
 
1464
            tns_data(ics, &(ics->tns), ld);
 
1465
    }
 
1466
 
 
1467
#ifdef DRM
 
1468
    /* CRC check */
 
1469
    if (hDecoder->object_type == DRM_ER_LC)
 
1470
        if ((result = faad_check_CRC(ld, faad_get_processed_bits(ld) - 8)) > 0)
 
1471
            return result;
 
1472
#endif
 
1473
 
 
1474
    if (hDecoder->aacSpectralDataResilienceFlag)
 
1475
    {
 
1476
        /* error resilient spectral data decoding */
 
1477
        if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
 
1478
        {
 
1479
            return result;
 
1480
        }
 
1481
    } else {
 
1482
#endif
 
1483
        /* decode the spectral data */
 
1484
        if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
 
1485
        {
 
1486
            return result;
 
1487
        }
 
1488
#ifdef ERROR_RESILIENCE
 
1489
    }
 
1490
#endif
 
1491
 
 
1492
    /* pulse coding reconstruction */
 
1493
    if (ics->pulse_data_present)
 
1494
    {
 
1495
        if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
 
1496
        {
 
1497
            if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
 
1498
                return result;
 
1499
        } else {
 
1500
            return 2; /* pulse coding not allowed for short blocks */
 
1501
        }
 
1502
    }
 
1503
 
 
1504
    return 0;
 
1505
}
 
1506
 
 
1507
/* Table 4.4.25 */
 
1508
static uint8_t section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld)
 
1509
{
 
1510
    uint8_t g;
 
1511
    uint8_t sect_esc_val, sect_bits;
 
1512
 
 
1513
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
 
1514
        sect_bits = 3;
 
1515
    else
 
1516
        sect_bits = 5;
 
1517
    sect_esc_val = (1<<sect_bits) - 1;
 
1518
 
 
1519
#if 0
 
1520
    printf("\ntotal sfb %d\n", ics->max_sfb);
 
1521
    printf("   sect    top     cb\n");
 
1522
#endif
 
1523
 
 
1524
    for (g = 0; g < ics->num_window_groups; g++)
 
1525
    {
 
1526
        uint8_t k = 0;
 
1527
        uint8_t i = 0;
 
1528
 
 
1529
        while (k < ics->max_sfb)
 
1530
        {
 
1531
#ifdef ERROR_RESILIENCE
 
1532
            uint8_t vcb11 = 0;
 
1533
#endif
 
1534
            uint8_t sfb;
 
1535
            uint8_t sect_len_incr;
 
1536
            uint16_t sect_len = 0;
 
1537
            uint8_t sect_cb_bits = 4;
 
1538
 
 
1539
            /* if "faad_getbits" detects error and returns "0", "k" is never
 
1540
               incremented and we cannot leave the while loop */
 
1541
            if ((ld->error != 0) || (ld->no_more_reading))
 
1542
                return 14;
 
1543
 
 
1544
#ifdef ERROR_RESILIENCE
 
1545
            if (hDecoder->aacSectionDataResilienceFlag)
 
1546
                sect_cb_bits = 5;
 
1547
#endif
 
1548
 
 
1549
            ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
 
1550
                DEBUGVAR(1,71,"section_data(): sect_cb"));
 
1551
 
 
1552
            if (ics->sect_cb[g][i] == NOISE_HCB)
 
1553
                ics->noise_used = 1;
 
1554
 
 
1555
#ifdef ERROR_RESILIENCE
 
1556
            if (hDecoder->aacSectionDataResilienceFlag)
 
1557
            {
 
1558
                if ((ics->sect_cb[g][i] == 11) ||
 
1559
                    ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
 
1560
                {
 
1561
                    vcb11 = 1;
 
1562
                }
 
1563
            }
 
1564
            if (vcb11)
 
1565
            {
 
1566
                sect_len_incr = 1;
 
1567
            } else {
 
1568
#endif
 
1569
                sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
 
1570
                    DEBUGVAR(1,72,"section_data(): sect_len_incr"));
 
1571
#ifdef ERROR_RESILIENCE
 
1572
            }
 
1573
#endif
 
1574
            while ((sect_len_incr == sect_esc_val) /* &&
 
1575
                (k+sect_len < ics->max_sfb)*/)
 
1576
            {
 
1577
                sect_len += sect_len_incr;
 
1578
                sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
 
1579
                    DEBUGVAR(1,72,"section_data(): sect_len_incr"));
 
1580
            }
 
1581
 
 
1582
            sect_len += sect_len_incr;
 
1583
 
 
1584
            ics->sect_start[g][i] = k;
 
1585
            ics->sect_end[g][i] = k + sect_len;
 
1586
 
 
1587
            if (k + sect_len >= 8*15)
 
1588
                return 15;
 
1589
            if (i >= 8*15)
 
1590
                return 15;
 
1591
 
 
1592
            for (sfb = k; sfb < k + sect_len; sfb++)
 
1593
                ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
 
1594
 
 
1595
#if 0
 
1596
            printf(" %6d %6d %6d\n",
 
1597
                i,
 
1598
                ics->sect_end[g][i],
 
1599
                ics->sect_cb[g][i]);
 
1600
#endif
 
1601
 
 
1602
            k += sect_len;
 
1603
            i++;
 
1604
        }
 
1605
        ics->num_sec[g] = i;
 
1606
    }
 
1607
 
 
1608
#if 0
 
1609
    printf("\n");
 
1610
#endif
 
1611
 
 
1612
    return 0;
 
1613
}
 
1614
 
 
1615
/*
 
1616
 *  decode_scale_factors()
 
1617
 *   decodes the scalefactors from the bitstream
 
1618
 */
 
1619
/*
 
1620
 * All scalefactors (and also the stereo positions and pns energies) are
 
1621
 * transmitted using Huffman coded DPCM relative to the previous active
 
1622
 * scalefactor (respectively previous stereo position or previous pns energy,
 
1623
 * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
 
1624
 * differentially coded relative to the global gain.
 
1625
 */
 
1626
static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
 
1627
{
 
1628
    uint8_t g, sfb;
 
1629
    int16_t t;
 
1630
    int8_t noise_pcm_flag = 1;
 
1631
 
 
1632
    int16_t scale_factor = ics->global_gain;
 
1633
    int16_t is_position = 0;
 
1634
    int16_t noise_energy = ics->global_gain - 90;
 
1635
 
 
1636
    for (g = 0; g < ics->num_window_groups; g++)
 
1637
    {
 
1638
        for (sfb = 0; sfb < ics->max_sfb; sfb++)
 
1639
        {
 
1640
            switch (ics->sfb_cb[g][sfb])
 
1641
            {
 
1642
            case ZERO_HCB: /* zero book */
 
1643
                ics->scale_factors[g][sfb] = 0;
 
1644
                break;
 
1645
            case INTENSITY_HCB: /* intensity books */
 
1646
            case INTENSITY_HCB2:
 
1647
 
 
1648
                /* decode intensity position */
 
1649
                t = huffman_scale_factor(ld);
 
1650
                is_position += (t - 60);
 
1651
                ics->scale_factors[g][sfb] = is_position;
 
1652
 
 
1653
                break;
 
1654
            case NOISE_HCB: /* noise books */
 
1655
 
 
1656
                /* decode noise energy */
 
1657
                if (noise_pcm_flag)
 
1658
                {
 
1659
                    noise_pcm_flag = 0;
 
1660
                    t = (int16_t)faad_getbits(ld, 9
 
1661
                        DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
 
1662
                } else {
 
1663
                    t = huffman_scale_factor(ld);
 
1664
                    t -= 60;
 
1665
                }
 
1666
                noise_energy += t;
 
1667
                ics->scale_factors[g][sfb] = noise_energy;
 
1668
 
 
1669
                break;
 
1670
            default: /* spectral books */
 
1671
 
 
1672
                /* ics->scale_factors[g][sfb] must be between 0 and 255 */
 
1673
 
 
1674
                ics->scale_factors[g][sfb] = 0;
 
1675
 
 
1676
                /* decode scale factor */
 
1677
                t = huffman_scale_factor(ld);
 
1678
                scale_factor += (t - 60);
 
1679
                if (scale_factor < 0 || scale_factor > 255)
 
1680
                    return 4;
 
1681
                ics->scale_factors[g][sfb] = scale_factor;
 
1682
 
 
1683
                break;
 
1684
            }
 
1685
        }
 
1686
    }
 
1687
 
 
1688
    return 0;
 
1689
}
 
1690
 
 
1691
/* Table 4.4.26 */
 
1692
static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld)
 
1693
{
 
1694
    uint8_t ret = 0;
 
1695
#ifdef PROFILE
 
1696
    int64_t count = faad_get_ts();
 
1697
#endif
 
1698
 
 
1699
#ifdef ERROR_RESILIENCE
 
1700
    if (!hDecoder->aacScalefactorDataResilienceFlag)
 
1701
    {
 
1702
#endif
 
1703
        ret = decode_scale_factors(ics, ld);
 
1704
#ifdef ERROR_RESILIENCE
 
1705
    } else {
 
1706
        /* In ER AAC the parameters for RVLC are seperated from the actual
 
1707
           data that holds the scale_factors.
 
1708
           Strangely enough, 2 parameters for HCR are put inbetween them.
 
1709
        */
 
1710
        ret = rvlc_scale_factor_data(ics, ld);
 
1711
    }
 
1712
#endif
 
1713
 
 
1714
#ifdef PROFILE
 
1715
    count = faad_get_ts() - count;
 
1716
    hDecoder->scalefac_cycles += count;
 
1717
#endif
 
1718
 
 
1719
    return ret;
 
1720
}
 
1721
 
 
1722
/* Table 4.4.27 */
 
1723
static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
 
1724
{
 
1725
    uint8_t w, filt, i, start_coef_bits, coef_bits;
 
1726
    uint8_t n_filt_bits = 2;
 
1727
    uint8_t length_bits = 6;
 
1728
    uint8_t order_bits = 5;
 
1729
 
 
1730
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
 
1731
    {
 
1732
        n_filt_bits = 1;
 
1733
        length_bits = 4;
 
1734
        order_bits = 3;
 
1735
    }
 
1736
 
 
1737
    for (w = 0; w < ics->num_windows; w++)
 
1738
    {
 
1739
        tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
 
1740
            DEBUGVAR(1,74,"tns_data(): n_filt"));
 
1741
 
 
1742
        if (tns->n_filt[w])
 
1743
        {
 
1744
            if ((tns->coef_res[w] = faad_get1bit(ld
 
1745
                DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
 
1746
            {
 
1747
                start_coef_bits = 4;
 
1748
            } else {
 
1749
                start_coef_bits = 3;
 
1750
            }
 
1751
        }
 
1752
 
 
1753
        for (filt = 0; filt < tns->n_filt[w]; filt++)
 
1754
        {
 
1755
            tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits
 
1756
                DEBUGVAR(1,76,"tns_data(): length"));
 
1757
            tns->order[w][filt]  = (uint8_t)faad_getbits(ld, order_bits
 
1758
                DEBUGVAR(1,77,"tns_data(): order"));
 
1759
            if (tns->order[w][filt])
 
1760
            {
 
1761
                tns->direction[w][filt] = faad_get1bit(ld
 
1762
                    DEBUGVAR(1,78,"tns_data(): direction"));
 
1763
                tns->coef_compress[w][filt] = faad_get1bit(ld
 
1764
                    DEBUGVAR(1,79,"tns_data(): coef_compress"));
 
1765
 
 
1766
                coef_bits = start_coef_bits - tns->coef_compress[w][filt];
 
1767
                for (i = 0; i < tns->order[w][filt]; i++)
 
1768
                {
 
1769
                    tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
 
1770
                        DEBUGVAR(1,80,"tns_data(): coef"));
 
1771
                }
 
1772
            }
 
1773
        }
 
1774
    }
 
1775
}
 
1776
 
 
1777
#ifdef LTP_DEC
 
1778
/* Table 4.4.28 */
 
1779
static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
 
1780
{
 
1781
    uint8_t sfb, w;
 
1782
 
 
1783
    ltp->lag = 0;
 
1784
 
 
1785
#ifdef LD_DEC
 
1786
    if (hDecoder->object_type == LD)
 
1787
    {
 
1788
        ltp->lag_update = (uint8_t)faad_getbits(ld, 1
 
1789
            DEBUGVAR(1,142,"ltp_data(): lag_update"));
 
1790
 
 
1791
        if (ltp->lag_update)
 
1792
        {
 
1793
            ltp->lag = (uint16_t)faad_getbits(ld, 10
 
1794
                DEBUGVAR(1,81,"ltp_data(): lag"));
 
1795
        }
 
1796
    } else {
 
1797
#endif
 
1798
        ltp->lag = (uint16_t)faad_getbits(ld, 11
 
1799
            DEBUGVAR(1,81,"ltp_data(): lag"));
 
1800
#ifdef LD_DEC
 
1801
    }
 
1802
#endif
 
1803
 
 
1804
    /* Check length of lag */
 
1805
    if (ltp->lag > (hDecoder->frameLength << 1))
 
1806
        return 18;
 
1807
 
 
1808
    ltp->coef = (uint8_t)faad_getbits(ld, 3
 
1809
        DEBUGVAR(1,82,"ltp_data(): coef"));
 
1810
 
 
1811
    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
 
1812
    {
 
1813
        for (w = 0; w < ics->num_windows; w++)
 
1814
        {
 
1815
            if ((ltp->short_used[w] = faad_get1bit(ld
 
1816
                DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
 
1817
            {
 
1818
                ltp->short_lag_present[w] = faad_get1bit(ld
 
1819
                    DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
 
1820
                if (ltp->short_lag_present[w])
 
1821
                {
 
1822
                    ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
 
1823
                        DEBUGVAR(1,85,"ltp_data(): short_lag"));
 
1824
                }
 
1825
            }
 
1826
        }
 
1827
    } else {
 
1828
        ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
 
1829
 
 
1830
        for (sfb = 0; sfb < ltp->last_band; sfb++)
 
1831
        {
 
1832
            ltp->long_used[sfb] = faad_get1bit(ld
 
1833
                DEBUGVAR(1,86,"ltp_data(): long_used"));
 
1834
        }
 
1835
    }
 
1836
 
 
1837
    return 0;
 
1838
}
 
1839
#endif
 
1840
 
 
1841
/* Table 4.4.29 */
 
1842
static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
 
1843
                             int16_t *spectral_data)
 
1844
{
 
1845
    int8_t i;
 
1846
    uint8_t g;
 
1847
    uint16_t inc, k, p = 0;
 
1848
    uint8_t groups = 0;
 
1849
    uint8_t sect_cb;
 
1850
    uint8_t result;
 
1851
    uint16_t nshort = hDecoder->frameLength/8;
 
1852
 
 
1853
#ifdef PROFILE
 
1854
    int64_t count = faad_get_ts();
 
1855
#endif
 
1856
 
 
1857
    for(g = 0; g < ics->num_window_groups; g++)
 
1858
    {
 
1859
        p = groups*nshort;
 
1860
 
 
1861
        for (i = 0; i < ics->num_sec[g]; i++)
 
1862
        {
 
1863
            sect_cb = ics->sect_cb[g][i];
 
1864
 
 
1865
            inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
 
1866
 
 
1867
            switch (sect_cb)
 
1868
            {
 
1869
            case ZERO_HCB:
 
1870
            case NOISE_HCB:
 
1871
            case INTENSITY_HCB:
 
1872
            case INTENSITY_HCB2:
 
1873
                p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
 
1874
                    ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
 
1875
                break;
 
1876
            default:
 
1877
                for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
 
1878
                     k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
 
1879
                {
 
1880
                    if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
 
1881
                        return result;
 
1882
                    p += inc;
 
1883
                }
 
1884
                break;
 
1885
            }
 
1886
        }
 
1887
        groups += ics->window_group_length[g];
 
1888
    }
 
1889
 
 
1890
#ifdef PROFILE
 
1891
    count = faad_get_ts() - count;
 
1892
    hDecoder->spectral_cycles += count;
 
1893
#endif
 
1894
 
 
1895
    return 0;
 
1896
}
 
1897
 
 
1898
/* Table 4.4.30 */
 
1899
static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
 
1900
{
 
1901
    uint16_t i, n, dataElementLength;
 
1902
    uint8_t dataElementLengthPart;
 
1903
    uint8_t align = 4, data_element_version, loopCounter;
 
1904
 
 
1905
    uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
 
1906
        DEBUGVAR(1,87,"extension_payload(): extension_type"));
 
1907
 
 
1908
    switch (extension_type)
 
1909
    {
 
1910
    case EXT_DYNAMIC_RANGE:
 
1911
        drc->present = 1;
 
1912
        n = dynamic_range_info(ld, drc);
 
1913
        return n;
 
1914
    case EXT_FILL_DATA:
 
1915
        /* fill_nibble = */ faad_getbits(ld, 4
 
1916
            DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be ļæ½0000ļæ½ */
 
1917
        for (i = 0; i < count-1; i++)
 
1918
        {
 
1919
            /* fill_byte[i] = */ faad_getbits(ld, 8
 
1920
                DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ļæ½10100101ļæ½ */
 
1921
        }
 
1922
        return count;
 
1923
    case EXT_DATA_ELEMENT:
 
1924
        data_element_version = (uint8_t)faad_getbits(ld, 4
 
1925
            DEBUGVAR(1,400,"extension_payload(): data_element_version"));
 
1926
        switch (data_element_version)
 
1927
        {
 
1928
        case ANC_DATA:
 
1929
            loopCounter = 0;
 
1930
            dataElementLength = 0;
 
1931
            do {
 
1932
                dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
 
1933
                    DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
 
1934
                dataElementLength += dataElementLengthPart;
 
1935
                loopCounter++;
 
1936
            } while (dataElementLengthPart == 255);
 
1937
 
 
1938
            for (i = 0; i < dataElementLength; i++)
 
1939
            {
 
1940
                /* data_element_byte[i] = */ faad_getbits(ld, 8
 
1941
                    DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
 
1942
                return (dataElementLength+loopCounter+1);
 
1943
            }
 
1944
        default:
 
1945
            align = 0;
 
1946
        }
 
1947
    case EXT_FIL:
 
1948
    default:
 
1949
        faad_getbits(ld, align
 
1950
            DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
 
1951
        for (i = 0; i < count-1; i++)
 
1952
        {
 
1953
            /* other_bits[i] = */ faad_getbits(ld, 8
 
1954
               DEBUGVAR(1,89,"extension_payload(): fill_bit"));
 
1955
        }
 
1956
        return count;
 
1957
    }
 
1958
}
 
1959
 
 
1960
/* Table 4.4.31 */
 
1961
static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc)
 
1962
{
 
1963
    uint8_t i, n = 1;
 
1964
    uint8_t band_incr;
 
1965
 
 
1966
    drc->num_bands = 1;
 
1967
 
 
1968
    if (faad_get1bit(ld
 
1969
        DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
 
1970
    {
 
1971
        drc->pce_instance_tag = (uint8_t)faad_getbits(ld, 4
 
1972
            DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag"));
 
1973
        /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4
 
1974
            DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits"));
 
1975
        n++;
 
1976
    }
 
1977
 
 
1978
    drc->excluded_chns_present = faad_get1bit(ld
 
1979
        DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present"));
 
1980
    if (drc->excluded_chns_present == 1)
 
1981
    {
 
1982
        n += excluded_channels(ld, drc);
 
1983
    }
 
1984
 
 
1985
    if (faad_get1bit(ld
 
1986
        DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
 
1987
    {
 
1988
        band_incr = (uint8_t)faad_getbits(ld, 4
 
1989
            DEBUGVAR(1,95,"dynamic_range_info(): band_incr"));
 
1990
        /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4
 
1991
            DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits"));
 
1992
        n++;
 
1993
        drc->num_bands += band_incr;
 
1994
 
 
1995
        for (i = 0; i < drc->num_bands; i++);
 
1996
        {
 
1997
            drc->band_top[i] = (uint8_t)faad_getbits(ld, 8
 
1998
                DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
 
1999
            n++;
 
2000
        }
 
2001
    }
 
2002
 
 
2003
    if (faad_get1bit(ld
 
2004
        DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
 
2005
    {
 
2006
        drc->prog_ref_level = (uint8_t)faad_getbits(ld, 7
 
2007
            DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level"));
 
2008
        /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
 
2009
            DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits"));
 
2010
        n++;
 
2011
    }
 
2012
 
 
2013
    for (i = 0; i < drc->num_bands; i++)
 
2014
    {
 
2015
        drc->dyn_rng_sgn[i] = faad_get1bit(ld
 
2016
            DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn"));
 
2017
        drc->dyn_rng_ctl[i] = (uint8_t)faad_getbits(ld, 7
 
2018
            DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl"));
 
2019
        n++;
 
2020
    }
 
2021
 
 
2022
    return n;
 
2023
}
 
2024
 
 
2025
/* Table 4.4.32 */
 
2026
static uint8_t excluded_channels(bitfile *ld, drc_info *drc)
 
2027
{
 
2028
    uint8_t i, n = 0;
 
2029
    uint8_t num_excl_chan = 7;
 
2030
 
 
2031
    for (i = 0; i < 7; i++)
 
2032
    {
 
2033
        drc->exclude_mask[i] = faad_get1bit(ld
 
2034
            DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
 
2035
    }
 
2036
    n++;
 
2037
 
 
2038
    while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
 
2039
        DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
 
2040
    {
 
2041
        for (i = num_excl_chan; i < num_excl_chan+7; i++)
 
2042
        {
 
2043
            drc->exclude_mask[i] = faad_get1bit(ld
 
2044
                DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
 
2045
        }
 
2046
        n++;
 
2047
        num_excl_chan += 7;
 
2048
    }
 
2049
 
 
2050
    return n;
 
2051
}
 
2052
 
 
2053
/* Annex A: Audio Interchange Formats */
 
2054
 
 
2055
/* Table 1.A.2 */
 
2056
void get_adif_header(adif_header *adif, bitfile *ld)
 
2057
{
 
2058
    uint8_t i;
 
2059
 
 
2060
    /* adif_id[0] = */ faad_getbits(ld, 8
 
2061
        DEBUGVAR(1,106,"get_adif_header(): adif_id[0]"));
 
2062
    /* adif_id[1] = */ faad_getbits(ld, 8
 
2063
        DEBUGVAR(1,107,"get_adif_header(): adif_id[1]"));
 
2064
    /* adif_id[2] = */ faad_getbits(ld, 8
 
2065
        DEBUGVAR(1,108,"get_adif_header(): adif_id[2]"));
 
2066
    /* adif_id[3] = */ faad_getbits(ld, 8
 
2067
        DEBUGVAR(1,109,"get_adif_header(): adif_id[3]"));
 
2068
    adif->copyright_id_present = faad_get1bit(ld
 
2069
        DEBUGVAR(1,110,"get_adif_header(): copyright_id_present"));
 
2070
    if(adif->copyright_id_present)
 
2071
    {
 
2072
        for (i = 0; i < 72/8; i++)
 
2073
        {
 
2074
            adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8
 
2075
                DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
 
2076
        }
 
2077
        adif->copyright_id[i] = 0;
 
2078
    }
 
2079
    adif->original_copy  = faad_get1bit(ld
 
2080
        DEBUGVAR(1,112,"get_adif_header(): original_copy"));
 
2081
    adif->home = faad_get1bit(ld
 
2082
        DEBUGVAR(1,113,"get_adif_header(): home"));
 
2083
    adif->bitstream_type = faad_get1bit(ld
 
2084
        DEBUGVAR(1,114,"get_adif_header(): bitstream_type"));
 
2085
    adif->bitrate = faad_getbits(ld, 23
 
2086
        DEBUGVAR(1,115,"get_adif_header(): bitrate"));
 
2087
    adif->num_program_config_elements = (uint8_t)faad_getbits(ld, 4
 
2088
        DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements"));
 
2089
 
 
2090
    for (i = 0; i < adif->num_program_config_elements + 1; i++)
 
2091
    {
 
2092
        if(adif->bitstream_type == 0)
 
2093
        {
 
2094
            adif->adif_buffer_fullness = faad_getbits(ld, 20
 
2095
                DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
 
2096
        } else {
 
2097
            adif->adif_buffer_fullness = 0;
 
2098
        }
 
2099
 
 
2100
        program_config_element(&adif->pce[i], ld);
 
2101
    }
 
2102
}
 
2103
 
 
2104
/* Table 1.A.5 */
 
2105
uint8_t adts_frame(adts_header *adts, bitfile *ld)
 
2106
{
 
2107
    /* faad_byte_align(ld); */
 
2108
    if (adts_fixed_header(adts, ld))
 
2109
        return 5;
 
2110
    adts_variable_header(adts, ld);
 
2111
    adts_error_check(adts, ld);
 
2112
 
 
2113
    return 0;
 
2114
}
 
2115
 
 
2116
/* Table 1.A.6 */
 
2117
static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld)
 
2118
{
 
2119
    uint16_t i;
 
2120
    uint8_t sync_err = 1;
 
2121
 
 
2122
    /* try to recover from sync errors */
 
2123
    for (i = 0; i < 768; i++)
 
2124
    {
 
2125
        adts->syncword = (uint16_t)faad_showbits(ld, 12);
 
2126
        if (adts->syncword != 0xFFF)
 
2127
        {
 
2128
            faad_getbits(ld, 8
 
2129
                DEBUGVAR(0,0,""));
 
2130
        } else {
 
2131
            sync_err = 0;
 
2132
            faad_getbits(ld, 12
 
2133
                DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
 
2134
            break;
 
2135
        }
 
2136
    }
 
2137
    if (sync_err)
 
2138
        return 5;
 
2139
 
 
2140
    adts->id = faad_get1bit(ld
 
2141
        DEBUGVAR(1,119,"adts_fixed_header(): id"));
 
2142
    adts->layer = (uint8_t)faad_getbits(ld, 2
 
2143
        DEBUGVAR(1,120,"adts_fixed_header(): layer"));
 
2144
    adts->protection_absent = faad_get1bit(ld
 
2145
        DEBUGVAR(1,121,"adts_fixed_header(): protection_absent"));
 
2146
    adts->profile = (uint8_t)faad_getbits(ld, 2
 
2147
        DEBUGVAR(1,122,"adts_fixed_header(): profile"));
 
2148
    adts->sf_index = (uint8_t)faad_getbits(ld, 4
 
2149
        DEBUGVAR(1,123,"adts_fixed_header(): sf_index"));
 
2150
    adts->private_bit = faad_get1bit(ld
 
2151
        DEBUGVAR(1,124,"adts_fixed_header(): private_bit"));
 
2152
    adts->channel_configuration = (uint8_t)faad_getbits(ld, 3
 
2153
        DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration"));
 
2154
    adts->original = faad_get1bit(ld
 
2155
        DEBUGVAR(1,126,"adts_fixed_header(): original"));
 
2156
    adts->home = faad_get1bit(ld
 
2157
        DEBUGVAR(1,127,"adts_fixed_header(): home"));
 
2158
 
 
2159
    if (adts->old_format == 1)
 
2160
    {
 
2161
        /* Removed in corrigendum 14496-3:2002 */
 
2162
        if (adts->id == 0)
 
2163
        {
 
2164
            adts->emphasis = (uint8_t)faad_getbits(ld, 2
 
2165
                DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
 
2166
        }
 
2167
    }
 
2168
 
 
2169
    return 0;
 
2170
}
 
2171
 
 
2172
/* Table 1.A.7 */
 
2173
static void adts_variable_header(adts_header *adts, bitfile *ld)
 
2174
{
 
2175
    adts->copyright_identification_bit = faad_get1bit(ld
 
2176
        DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit"));
 
2177
    adts->copyright_identification_start = faad_get1bit(ld
 
2178
        DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start"));
 
2179
    adts->aac_frame_length = (uint16_t)faad_getbits(ld, 13
 
2180
        DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length"));
 
2181
    adts->adts_buffer_fullness = (uint16_t)faad_getbits(ld, 11
 
2182
        DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness"));
 
2183
    adts->no_raw_data_blocks_in_frame = (uint8_t)faad_getbits(ld, 2
 
2184
        DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame"));
 
2185
}
 
2186
 
 
2187
/* Table 1.A.8 */
 
2188
static void adts_error_check(adts_header *adts, bitfile *ld)
 
2189
{
 
2190
    if (adts->protection_absent == 0)
 
2191
    {
 
2192
        adts->crc_check = (uint16_t)faad_getbits(ld, 16
 
2193
            DEBUGVAR(1,134,"adts_error_check(): crc_check"));
 
2194
    }
 
2195
}