2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
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.
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.
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.
19
** Any non-GPL usage of this software or parts of this software is strictly
22
** Commercial non-GPL licensing of this software is possible.
23
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
25
** $Id: syntax.c,v 1.71 2004/02/04 19:55:03 menno Exp $
29
Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
46
#ifdef ERROR_RESILIENCE
50
#include "sbr_syntax.h"
54
/* static function declarations */
55
static void decode_sce_lfe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
57
static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
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);
64
static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld);
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
73
static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele,
74
bitfile *ld, ic_stream *ics, uint8_t scal_flag,
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);
81
static void gain_control_data(bitfile *ld, ic_stream *ics);
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);
95
static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
96
bitfile *ld, uint8_t this_layer_stereo);
101
int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
102
program_config *pce_out)
107
mp4ASC->frameLengthFlag = faad_get1bit(ld
108
DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
109
#ifndef ALLOW_SMALL_FRAMELENGTH
110
if (mp4ASC->frameLengthFlag == 1)
114
mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
115
DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
116
if (mp4ASC->dependsOnCoreCoder == 1)
118
mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
119
DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
122
mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
123
if (mp4ASC->channelsConfiguration == 0)
125
if (program_config_element(&pce, ld))
127
//mp4ASC->channelsConfiguration = pce.channels;
130
memcpy(pce_out, &pce, sizeof(program_config));
133
if (pce.num_valid_cc_elements)
138
#ifdef ERROR_RESILIENCE
139
if (mp4ASC->extensionFlag == 1)
141
/* Error resilience not supported yet */
142
if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
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"));
151
/* 1 bit: extensionFlag3 */
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.
166
static uint8_t program_config_element(program_config *pce, bitfile *ld)
170
memset(pce, 0, sizeof(program_config));
174
pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
175
DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
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"));
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)
198
pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
199
DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
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)
206
pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
207
DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
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)
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"));
220
for (i = 0; i < pce->num_front_channel_elements; i++)
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"));
227
if (pce->front_element_is_cpe[i] & 1)
229
pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
230
pce->num_front_channels += 2;
233
pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
234
pce->num_front_channels++;
239
for (i = 0; i < pce->num_side_channel_elements; i++)
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"));
246
if (pce->side_element_is_cpe[i] & 1)
248
pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
249
pce->num_side_channels += 2;
252
pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
253
pce->num_side_channels++;
258
for (i = 0; i < pce->num_back_channel_elements; i++)
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"));
265
if (pce->back_element_is_cpe[i] & 1)
267
pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
269
pce->num_back_channels += 2;
271
pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
272
pce->num_back_channels++;
277
for (i = 0; i < pce->num_lfe_channel_elements; i++)
279
pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
280
DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
282
pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
283
pce->num_lfe_channels++;
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"));
291
for (i = 0; i < pce->num_valid_cc_elements; i++)
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"));
301
pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
302
DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
304
for (i = 0; i < pce->comment_field_bytes; i++)
306
pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
307
DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
309
pce->comment_field_data[i] = 0;
311
if (pce->channels > MAX_CHANNELS)
317
static void decode_sce_lfe(faacDecHandle hDecoder,
318
faacDecFrameInfo *hInfo, bitfile *ld,
321
uint8_t channels = hDecoder->fr_channels;
324
if (channels+1 > MAX_CHANNELS)
329
if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
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
339
/* save the syntax element id */
340
hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
342
/* decode the element */
343
hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
345
/* map output channels position to internal data channels */
346
if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
348
/* this might be faulty when pce_set is true */
349
hDecoder->internal_channel[channels] = channels;
350
hDecoder->internal_channel[channels+1] = channels+1;
352
if (hDecoder->pce_set)
353
hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
355
hDecoder->internal_channel[channels] = channels;
358
hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
359
hDecoder->fr_ch_ele++;
362
static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
365
uint8_t channels = hDecoder->fr_channels;
368
if (channels+2 > MAX_CHANNELS)
373
if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
379
/* for CPE the number of output channels is always 2 */
380
if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
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 */
390
/* save the syntax element id */
391
hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
393
/* decode the element */
394
hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
396
/* map output channel position to internal data channels */
397
if (hDecoder->pce_set)
399
hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
400
hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
402
hDecoder->internal_channel[channels] = channels;
403
hDecoder->internal_channel[channels+1] = channels+1;
406
hDecoder->fr_channels += 2;
407
hDecoder->fr_ch_ele++;
410
void raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
411
bitfile *ld, program_config *pce, drc_info *drc)
415
hDecoder->fr_channels = 0;
416
hDecoder->fr_ch_ele = 0;
417
hDecoder->first_syn_ele = 25;
418
hDecoder->has_lfe = 0;
420
#ifdef ERROR_RESILIENCE
421
if (hDecoder->object_type < ER_OBJECT_START)
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)
428
switch (id_syn_ele) {
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)
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)
443
decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
444
if (hInfo->error > 0)
447
case ID_CCE: /* not implemented yet, but skip the bits */
449
hInfo->error = coupling_channel_element(hDecoder, ld);
453
if (hInfo->error > 0)
457
data_stream_element(hDecoder, ld);
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)
465
//hDecoder->pce_set = 1;
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
473
, INVALID_SBR_ELEMENT
480
#ifdef ERROR_RESILIENCE
482
/* Table 262: er_raw_data_block() */
483
switch (hDecoder->channelConfiguration)
486
decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
487
if (hInfo->error > 0)
491
decode_cpe(hDecoder, hInfo, ld, ID_CPE);
492
if (hInfo->error > 0)
496
decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
497
decode_cpe(hDecoder, hInfo, ld, ID_CPE);
498
if (hInfo->error > 0)
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)
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)
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)
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)
537
cnt = bits_to_decode() / 8;
540
cnt -= extension_payload(cnt);
546
/* new in corrigendum 14496-3:2002 */
548
if (hDecoder->object_type != DRM_ER_LC)
557
/* Table 4.4.4 and */
559
static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld,
560
uint8_t channel, uint8_t *tag)
564
ic_stream *ics = &(sce.ics1);
565
ALIGN int16_t spec_data[1024] = {0};
567
sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
568
DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
570
*tag = sce.element_instance_tag;
571
sce.channel = channel;
572
sce.paired_channel = -1;
574
retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
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)
583
faad_flushbits(ld, LEN_SE_ID);
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)
593
/* noiseless coding is done, spectral reconstruction is done now */
594
retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
602
static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld,
603
uint8_t channels, uint8_t *tag)
605
ALIGN int16_t spec_data1[1024] = {0};
606
ALIGN int16_t spec_data2[1024] = {0};
608
ic_stream *ics1 = &(cpe.ics1);
609
ic_stream *ics2 = &(cpe.ics2);
612
cpe.channel = channels;
613
cpe.paired_channel = channels+1;
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;
619
if ((cpe.common_window = faad_get1bit(ld
620
DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
622
/* both channels have common ics information */
623
if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
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)
631
for (g = 0; g < ics1->num_window_groups; g++)
633
for (sfb = 0; sfb < ics1->max_sfb; sfb++)
635
ics1->ms_used[g][sfb] = faad_get1bit(ld
636
DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
641
#ifdef ERROR_RESILIENCE
642
if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
644
if ((ics1->ltp.data_present = faad_get1bit(ld
645
DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
647
if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
655
memcpy(ics2, ics1, sizeof(ic_stream));
657
ics1->ms_mask_present = 0;
660
if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
666
#ifdef ERROR_RESILIENCE
667
if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
668
(ics1->predictor_data_present))
670
if ((ics1->ltp2.data_present = faad_get1bit(ld
671
DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
673
if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
681
if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
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)
692
faad_flushbits(ld, LEN_SE_ID);
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)
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)
713
static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
714
uint8_t common_window)
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"));
725
if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
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"));
732
ics->max_sfb = (uint8_t)faad_getbits(ld, 6
733
DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
736
/* get the grouping information */
737
if ((retval = window_grouping_info(hDecoder, ics)) > 0)
740
/* should be an error */
741
/* check the range of max_sfb */
742
if (ics->max_sfb > ics->num_swb)
745
if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
747
if ((ics->predictor_data_present = faad_get1bit(ld
748
DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
750
if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
754
ics->pred.limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
756
if ((ics->pred.predictor_reset = faad_get1bit(ld
757
DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
759
ics->pred.predictor_reset_group_number = (uint8_t)faad_getbits(ld, 5
760
DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
763
for (sfb = 0; sfb < ics->pred.limit; sfb++)
765
ics->pred.prediction_used[sfb] = faad_get1bit(ld
766
DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
770
else { /* Long Term Prediction */
771
if (hDecoder->object_type < ER_OBJECT_START)
773
if ((ics->ltp.data_present = faad_get1bit(ld
774
DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
776
if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
783
if ((ics->ltp2.data_present = faad_get1bit(ld
784
DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
786
if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
793
#ifdef ERROR_RESILIENCE
794
if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
796
if ((ics->ltp.data_present = faad_get1bit(ld
797
DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
799
ltp_data(hDecoder, ics, &(ics->ltp), ld);
812
static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
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"));
821
/* check the range of pulse_start_sfb */
822
if (pul->pulse_start_sfb > ics->num_swb)
825
for (i = 0; i < pul->number_pulse+1; i++)
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"));
837
/* Table 4.4.8: Currently just for skipping the bits... */
838
static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld)
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;
845
element el_empty = {0};
846
ic_stream ics_empty = {0};
847
int16_t sh_data[1024];
849
c = faad_getbits(ld, LEN_TAG
850
DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
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"));
857
for (c = 0; c < num_coupled_elements + 1; c++)
859
uint8_t cc_target_is_cpe, cc_target_tag_select;
861
num_gain_element_lists++;
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"));
868
if (cc_target_is_cpe)
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"));
876
num_gain_element_lists++;
881
DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
883
DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
885
DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
887
if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
893
for (c = 1; c < num_gain_element_lists; c++)
901
cge = faad_get1bit(ld
902
DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
907
huffman_scale_factor(ld);
911
for (g = 0; g < ics_empty.num_window_groups; g++)
913
for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
915
if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
916
huffman_scale_factor(ld);
927
static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld)
929
uint8_t byte_aligned;
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"));
940
count += (uint16_t)faad_getbits(ld, 8
941
DEBUGVAR(1,63,"data_stream_element(): extra count"));
946
for (i = 0; i < count; i++)
948
faad_getbits(ld, LEN_BYTE
949
DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
956
static uint8_t fill_element(faacDecHandle hDecoder, bitfile *ld, drc_info *drc
964
uint8_t bs_extension_type;
967
count = (uint16_t)faad_getbits(ld, 4
968
DEBUGVAR(1,65,"fill_element(): count"));
971
count += (uint16_t)faad_getbits(ld, 8
972
DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
978
bs_extension_type = (uint8_t)faad_showbits(ld, 4);
980
if ((bs_extension_type == EXT_SBR_DATA) ||
981
(bs_extension_type == EXT_SBR_DATA_CRC))
983
if (sbr_ele == INVALID_SBR_ELEMENT)
986
if (!hDecoder->sbr[sbr_ele])
988
hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
989
hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index)
996
hDecoder->sbr_present_flag = 1;
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)
1003
hDecoder->ps_used[sbr_ele] = 1;
1010
count -= extension_payload(ld, drc, count);
1022
static void gain_control_data(bitfile *ld, ic_stream *ics)
1025
ssr_info *ssr = &(ics->ssr);
1027
ssr->max_band = (uint8_t)faad_getbits(ld, 2
1028
DEBUGVAR(1,1000,"gain_control_data(): max_band"));
1030
if (ics->window_sequence == ONLY_LONG_SEQUENCE)
1032
for (bd = 1; bd <= ssr->max_band; bd++)
1034
for (wd = 0; wd < 1; wd++)
1036
ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1037
DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1039
for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
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"));
1048
} else if (ics->window_sequence == LONG_START_SEQUENCE) {
1049
for (bd = 1; bd <= ssr->max_band; bd++)
1051
for (wd = 0; wd < 2; wd++)
1053
ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1054
DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1056
for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1058
ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1059
DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1062
ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1063
DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1065
ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1066
DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1071
} else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1072
for (bd = 1; bd <= ssr->max_band; bd++)
1074
for (wd = 0; wd < 8; wd++)
1076
ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1077
DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1079
for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
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"));
1088
} else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
1089
for (bd = 1; bd <= ssr->max_band; bd++)
1091
for (wd = 0; wd < 2; wd++)
1093
ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1094
DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1096
for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1098
ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1099
DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1103
ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1104
DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1106
ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1107
DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
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)
1122
uint8_t channels = hDecoder->fr_channels = 0;
1124
uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
1126
ic_stream *ics1 = &(cpe.ics1);
1127
ic_stream *ics2 = &(cpe.ics2);
1129
ALIGN int16_t spec_data1[1024] = {0};
1130
ALIGN int16_t spec_data2[1024] = {0};
1132
hDecoder->fr_ch_ele = 0;
1134
hInfo->error = aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
1135
if (hInfo->error > 0)
1138
cpe.common_window = 1;
1139
if (this_layer_stereo)
1140
cpe.ele_id = ID_CPE;
1142
cpe.ele_id = ID_SCE;
1144
hDecoder->element_output_channels[hDecoder->fr_ch_ele] = (this_layer_stereo ? 2 : 0);
1146
for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1152
spec_data = spec_data1;
1155
spec_data = spec_data2;
1158
hInfo->error = individual_channel_stream(hDecoder, &cpe, ld, ics, 1, spec_data);
1159
if (hInfo->error > 0)
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))
1168
bitfile ld_sbr = {0};
1172
uint8_t *prevbufstart;
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);
1180
if (bitsconsumed + 8 > buffer_size*8)
1186
if (!hDecoder->sbr[0])
1188
hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, cpe.ele_id,
1189
2*get_sample_rate(hDecoder->sf_index), 1);
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--];
1200
/* consider 8 bits from AAC-CRC */
1201
count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
1202
faad_initbits(&ld_sbr, revbuffer, count);
1204
hDecoder->sbr[0]->lcstereo_flag = hDecoder->lcstereo_flag;
1206
hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
1207
hDecoder->sbr[0]->sample_rate *= 2;
1209
faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
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)
1215
hDecoder->ps_used[0] = 1;
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);
1224
faad_endbits(&ld_sbr);
1227
faad_free(revbuffer);
1232
if (this_layer_stereo)
1234
hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
1235
if (hInfo->error > 0)
1238
hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
1239
if (hInfo->error > 0)
1243
/* map output channels position to internal data channels */
1244
if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
1246
/* this might be faulty when pce_set is true */
1247
hDecoder->internal_channel[channels] = channels;
1248
hDecoder->internal_channel[channels+1] = channels+1;
1250
hDecoder->internal_channel[channels] = channels;
1253
hDecoder->element_id[hDecoder->fr_ch_ele] = cpe.ele_id;
1255
hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
1256
hDecoder->fr_ch_ele++;
1262
static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
1263
bitfile *ld, uint8_t this_layer_stereo)
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"));
1276
if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
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"));
1283
ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
1284
DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
1287
/* get the grouping information */
1288
if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
1291
/* should be an error */
1292
/* check the range of max_sfb */
1293
if (ics1->max_sfb > ics1->num_swb)
1296
if (this_layer_stereo)
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)
1303
for (g = 0; g < ics1->num_window_groups; g++)
1305
for (sfb = 0; sfb < ics1->max_sfb; sfb++)
1307
ics1->ms_used[g][sfb] = faad_get1bit(ld
1308
DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
1313
memcpy(ics2, ics1, sizeof(ic_stream));
1315
ics1->ms_mask_present = 0;
1321
DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer"));
1324
for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1331
if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/)
1333
if ((ics->tns_data_present = faad_get1bit(ld
1334
DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1)
1337
/* different order of data units in DRM */
1338
if (hDecoder->object_type != DRM_ER_LC)
1341
tns_data(ics, &(ics->tns), ld);
1346
if (0 /*core_flag || tvq_layer_pesent*/)
1348
if ((ch==0) || ((ch==1) && (core_stereo || tvq_stereo))
1349
diff_control_data();
1350
if (mono_stereo_flag)
1351
diff_control_data_lr();
1354
if ((ics->ltp.data_present = faad_get1bit(ld
1355
DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1)
1357
if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
1372
static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele,
1373
bitfile *ld, ic_stream *ics, uint8_t scal_flag,
1378
ics->global_gain = (uint8_t)faad_getbits(ld, 8
1379
DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
1381
if (!ele->common_window && !scal_flag)
1383
if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
1387
if ((result = section_data(hDecoder, ics, ld)) > 0)
1390
if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
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)
1400
/* get pulse data */
1401
if ((ics->pulse_data_present = faad_get1bit(ld
1402
DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
1404
if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
1409
if ((ics->tns_data_present = faad_get1bit(ld
1410
DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
1412
#ifdef ERROR_RESILIENCE
1413
if (hDecoder->object_type < ER_OBJECT_START)
1415
tns_data(ics, &(ics->tns), ld);
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)
1423
if (hDecoder->object_type != SSR)
1426
gain_control_data(ld, ics);
1433
#ifdef ERROR_RESILIENCE
1434
if (hDecoder->aacSpectralDataResilienceFlag)
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"));
1439
if (hDecoder->channelConfiguration == 2)
1441
if (ics->length_of_reordered_spectral_data > 6144)
1442
ics->length_of_reordered_spectral_data = 6144;
1444
if (ics->length_of_reordered_spectral_data > 12288)
1445
ics->length_of_reordered_spectral_data = 12288;
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;
1454
/* RVLC spectral data is put here */
1455
if (hDecoder->aacScalefactorDataResilienceFlag)
1457
if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
1461
if (hDecoder->object_type >= ER_OBJECT_START)
1463
if (ics->tns_data_present)
1464
tns_data(ics, &(ics->tns), ld);
1469
if (hDecoder->object_type == DRM_ER_LC)
1470
if ((result = faad_check_CRC(ld, faad_get_processed_bits(ld) - 8)) > 0)
1474
if (hDecoder->aacSpectralDataResilienceFlag)
1476
/* error resilient spectral data decoding */
1477
if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1483
/* decode the spectral data */
1484
if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1488
#ifdef ERROR_RESILIENCE
1492
/* pulse coding reconstruction */
1493
if (ics->pulse_data_present)
1495
if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
1497
if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
1500
return 2; /* pulse coding not allowed for short blocks */
1508
static uint8_t section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1511
uint8_t sect_esc_val, sect_bits;
1513
if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1517
sect_esc_val = (1<<sect_bits) - 1;
1520
printf("\ntotal sfb %d\n", ics->max_sfb);
1521
printf(" sect top cb\n");
1524
for (g = 0; g < ics->num_window_groups; g++)
1529
while (k < ics->max_sfb)
1531
#ifdef ERROR_RESILIENCE
1535
uint8_t sect_len_incr;
1536
uint16_t sect_len = 0;
1537
uint8_t sect_cb_bits = 4;
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))
1544
#ifdef ERROR_RESILIENCE
1545
if (hDecoder->aacSectionDataResilienceFlag)
1549
ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
1550
DEBUGVAR(1,71,"section_data(): sect_cb"));
1552
if (ics->sect_cb[g][i] == NOISE_HCB)
1553
ics->noise_used = 1;
1555
#ifdef ERROR_RESILIENCE
1556
if (hDecoder->aacSectionDataResilienceFlag)
1558
if ((ics->sect_cb[g][i] == 11) ||
1559
((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
1569
sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1570
DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1571
#ifdef ERROR_RESILIENCE
1574
while ((sect_len_incr == sect_esc_val) /* &&
1575
(k+sect_len < ics->max_sfb)*/)
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"));
1582
sect_len += sect_len_incr;
1584
ics->sect_start[g][i] = k;
1585
ics->sect_end[g][i] = k + sect_len;
1587
if (k + sect_len >= 8*15)
1592
for (sfb = k; sfb < k + sect_len; sfb++)
1593
ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
1596
printf(" %6d %6d %6d\n",
1598
ics->sect_end[g][i],
1599
ics->sect_cb[g][i]);
1605
ics->num_sec[g] = i;
1616
* decode_scale_factors()
1617
* decodes the scalefactors from the bitstream
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.
1626
static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
1630
int8_t noise_pcm_flag = 1;
1632
int16_t scale_factor = ics->global_gain;
1633
int16_t is_position = 0;
1634
int16_t noise_energy = ics->global_gain - 90;
1636
for (g = 0; g < ics->num_window_groups; g++)
1638
for (sfb = 0; sfb < ics->max_sfb; sfb++)
1640
switch (ics->sfb_cb[g][sfb])
1642
case ZERO_HCB: /* zero book */
1643
ics->scale_factors[g][sfb] = 0;
1645
case INTENSITY_HCB: /* intensity books */
1646
case INTENSITY_HCB2:
1648
/* decode intensity position */
1649
t = huffman_scale_factor(ld);
1650
is_position += (t - 60);
1651
ics->scale_factors[g][sfb] = is_position;
1654
case NOISE_HCB: /* noise books */
1656
/* decode noise energy */
1660
t = (int16_t)faad_getbits(ld, 9
1661
DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
1663
t = huffman_scale_factor(ld);
1667
ics->scale_factors[g][sfb] = noise_energy;
1670
default: /* spectral books */
1672
/* ics->scale_factors[g][sfb] must be between 0 and 255 */
1674
ics->scale_factors[g][sfb] = 0;
1676
/* decode scale factor */
1677
t = huffman_scale_factor(ld);
1678
scale_factor += (t - 60);
1679
if (scale_factor < 0 || scale_factor > 255)
1681
ics->scale_factors[g][sfb] = scale_factor;
1692
static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1696
int64_t count = faad_get_ts();
1699
#ifdef ERROR_RESILIENCE
1700
if (!hDecoder->aacScalefactorDataResilienceFlag)
1703
ret = decode_scale_factors(ics, ld);
1704
#ifdef ERROR_RESILIENCE
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.
1710
ret = rvlc_scale_factor_data(ics, ld);
1715
count = faad_get_ts() - count;
1716
hDecoder->scalefac_cycles += count;
1723
static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
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;
1730
if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1737
for (w = 0; w < ics->num_windows; w++)
1739
tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
1740
DEBUGVAR(1,74,"tns_data(): n_filt"));
1744
if ((tns->coef_res[w] = faad_get1bit(ld
1745
DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
1747
start_coef_bits = 4;
1749
start_coef_bits = 3;
1753
for (filt = 0; filt < tns->n_filt[w]; filt++)
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])
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"));
1766
coef_bits = start_coef_bits - tns->coef_compress[w][filt];
1767
for (i = 0; i < tns->order[w][filt]; i++)
1769
tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
1770
DEBUGVAR(1,80,"tns_data(): coef"));
1779
static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
1786
if (hDecoder->object_type == LD)
1788
ltp->lag_update = (uint8_t)faad_getbits(ld, 1
1789
DEBUGVAR(1,142,"ltp_data(): lag_update"));
1791
if (ltp->lag_update)
1793
ltp->lag = (uint16_t)faad_getbits(ld, 10
1794
DEBUGVAR(1,81,"ltp_data(): lag"));
1798
ltp->lag = (uint16_t)faad_getbits(ld, 11
1799
DEBUGVAR(1,81,"ltp_data(): lag"));
1804
/* Check length of lag */
1805
if (ltp->lag > (hDecoder->frameLength << 1))
1808
ltp->coef = (uint8_t)faad_getbits(ld, 3
1809
DEBUGVAR(1,82,"ltp_data(): coef"));
1811
if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1813
for (w = 0; w < ics->num_windows; w++)
1815
if ((ltp->short_used[w] = faad_get1bit(ld
1816
DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
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])
1822
ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
1823
DEBUGVAR(1,85,"ltp_data(): short_lag"));
1828
ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1830
for (sfb = 0; sfb < ltp->last_band; sfb++)
1832
ltp->long_used[sfb] = faad_get1bit(ld
1833
DEBUGVAR(1,86,"ltp_data(): long_used"));
1842
static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
1843
int16_t *spectral_data)
1847
uint16_t inc, k, p = 0;
1851
uint16_t nshort = hDecoder->frameLength/8;
1854
int64_t count = faad_get_ts();
1857
for(g = 0; g < ics->num_window_groups; g++)
1861
for (i = 0; i < ics->num_sec[g]; i++)
1863
sect_cb = ics->sect_cb[g][i];
1865
inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
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]]);
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)
1880
if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
1887
groups += ics->window_group_length[g];
1891
count = faad_get_ts() - count;
1892
hDecoder->spectral_cycles += count;
1899
static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
1901
uint16_t i, n, dataElementLength;
1902
uint8_t dataElementLengthPart;
1903
uint8_t align = 4, data_element_version, loopCounter;
1905
uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
1906
DEBUGVAR(1,87,"extension_payload(): extension_type"));
1908
switch (extension_type)
1910
case EXT_DYNAMIC_RANGE:
1912
n = dynamic_range_info(ld, drc);
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++)
1919
/* fill_byte[i] = */ faad_getbits(ld, 8
1920
DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ļæ½10100101ļæ½ */
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)
1930
dataElementLength = 0;
1932
dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
1933
DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
1934
dataElementLength += dataElementLengthPart;
1936
} while (dataElementLengthPart == 255);
1938
for (i = 0; i < dataElementLength; i++)
1940
/* data_element_byte[i] = */ faad_getbits(ld, 8
1941
DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
1942
return (dataElementLength+loopCounter+1);
1949
faad_getbits(ld, align
1950
DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
1951
for (i = 0; i < count-1; i++)
1953
/* other_bits[i] = */ faad_getbits(ld, 8
1954
DEBUGVAR(1,89,"extension_payload(): fill_bit"));
1961
static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc)
1969
DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
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"));
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)
1982
n += excluded_channels(ld, drc);
1986
DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
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"));
1993
drc->num_bands += band_incr;
1995
for (i = 0; i < drc->num_bands; i++);
1997
drc->band_top[i] = (uint8_t)faad_getbits(ld, 8
1998
DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
2004
DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
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"));
2013
for (i = 0; i < drc->num_bands; i++)
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"));
2026
static uint8_t excluded_channels(bitfile *ld, drc_info *drc)
2029
uint8_t num_excl_chan = 7;
2031
for (i = 0; i < 7; i++)
2033
drc->exclude_mask[i] = faad_get1bit(ld
2034
DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
2038
while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
2039
DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
2041
for (i = num_excl_chan; i < num_excl_chan+7; i++)
2043
drc->exclude_mask[i] = faad_get1bit(ld
2044
DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
2053
/* Annex A: Audio Interchange Formats */
2056
void get_adif_header(adif_header *adif, bitfile *ld)
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)
2072
for (i = 0; i < 72/8; i++)
2074
adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8
2075
DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
2077
adif->copyright_id[i] = 0;
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"));
2090
for (i = 0; i < adif->num_program_config_elements + 1; i++)
2092
if(adif->bitstream_type == 0)
2094
adif->adif_buffer_fullness = faad_getbits(ld, 20
2095
DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
2097
adif->adif_buffer_fullness = 0;
2100
program_config_element(&adif->pce[i], ld);
2105
uint8_t adts_frame(adts_header *adts, bitfile *ld)
2107
/* faad_byte_align(ld); */
2108
if (adts_fixed_header(adts, ld))
2110
adts_variable_header(adts, ld);
2111
adts_error_check(adts, ld);
2117
static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld)
2120
uint8_t sync_err = 1;
2122
/* try to recover from sync errors */
2123
for (i = 0; i < 768; i++)
2125
adts->syncword = (uint16_t)faad_showbits(ld, 12);
2126
if (adts->syncword != 0xFFF)
2133
DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
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"));
2159
if (adts->old_format == 1)
2161
/* Removed in corrigendum 14496-3:2002 */
2164
adts->emphasis = (uint8_t)faad_getbits(ld, 2
2165
DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
2173
static void adts_variable_header(adts_header *adts, bitfile *ld)
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"));
2188
static void adts_error_check(adts_header *adts, bitfile *ld)
2190
if (adts->protection_absent == 0)
2192
adts->crc_check = (uint16_t)faad_getbits(ld, 16
2193
DEBUGVAR(1,134,"adts_error_check(): crc_check"));