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.82 2004/09/04 14:56:29 menno Exp $
29 Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
46 #ifdef ERROR_RESILIENCE
50 #include "sbr_syntax.h"
55 /* static function declarations */
56 static void decode_sce_lfe(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
, bitfile
*ld
,
58 static void decode_cpe(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
, bitfile
*ld
,
60 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder
, bitfile
*ld
,
61 uint8_t channel
, uint8_t *tag
);
62 static uint8_t channel_pair_element(NeAACDecHandle hDecoder
, bitfile
*ld
,
63 uint8_t channel
, uint8_t *tag
);
65 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder
, bitfile
*ld
);
67 static uint16_t data_stream_element(NeAACDecHandle hDecoder
, bitfile
*ld
);
68 static uint8_t program_config_element(program_config
*pce
, bitfile
*ld
);
69 static uint8_t fill_element(NeAACDecHandle hDecoder
, bitfile
*ld
, drc_info
*drc
74 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder
, element
*ele
,
75 bitfile
*ld
, ic_stream
*ics
, uint8_t scal_flag
,
77 static uint8_t ics_info(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
,
78 uint8_t common_window
);
79 static uint8_t section_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
);
80 static uint8_t scale_factor_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
);
82 static void gain_control_data(bitfile
*ld
, ic_stream
*ics
);
84 static uint8_t spectral_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
,
85 int16_t *spectral_data
);
86 static uint16_t extension_payload(bitfile
*ld
, drc_info
*drc
, uint16_t count
);
87 static uint8_t pulse_data(ic_stream
*ics
, pulse_info
*pul
, bitfile
*ld
);
88 static void tns_data(ic_stream
*ics
, tns_info
*tns
, bitfile
*ld
);
90 static uint8_t ltp_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, ltp_info
*ltp
, bitfile
*ld
);
92 static uint8_t adts_fixed_header(adts_header
*adts
, bitfile
*ld
);
93 static void adts_variable_header(adts_header
*adts
, bitfile
*ld
);
94 static void adts_error_check(adts_header
*adts
, bitfile
*ld
);
95 static uint8_t dynamic_range_info(bitfile
*ld
, drc_info
*drc
);
96 static uint8_t excluded_channels(bitfile
*ld
, drc_info
*drc
);
98 static int8_t aac_scalable_main_header(NeAACDecHandle hDecoder
, ic_stream
*ics1
, ic_stream
*ics2
,
99 bitfile
*ld
, uint8_t this_layer_stereo
);
104 int8_t GASpecificConfig(bitfile
*ld
, mp4AudioSpecificConfig
*mp4ASC
,
105 program_config
*pce_out
)
110 mp4ASC
->frameLengthFlag
= faad_get1bit(ld
111 DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
112 #ifndef ALLOW_SMALL_FRAMELENGTH
113 if (mp4ASC
->frameLengthFlag
== 1)
117 mp4ASC
->dependsOnCoreCoder
= faad_get1bit(ld
118 DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
119 if (mp4ASC
->dependsOnCoreCoder
== 1)
121 mp4ASC
->coreCoderDelay
= (uint16_t)faad_getbits(ld
, 14
122 DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
125 mp4ASC
->extensionFlag
= faad_get1bit(ld
DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
126 if (mp4ASC
->channelsConfiguration
== 0)
128 if (program_config_element(&pce
, ld
))
130 //mp4ASC->channelsConfiguration = pce.channels;
133 memcpy(pce_out
, &pce
, sizeof(program_config
));
136 if (pce.num_valid_cc_elements)
141 #ifdef ERROR_RESILIENCE
142 if (mp4ASC
->extensionFlag
== 1)
144 /* Error resilience not supported yet */
145 if (mp4ASC
->objectTypeIndex
>= ER_OBJECT_START
)
147 mp4ASC
->aacSectionDataResilienceFlag
= faad_get1bit(ld
148 DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
149 mp4ASC
->aacScalefactorDataResilienceFlag
= faad_get1bit(ld
150 DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
151 mp4ASC
->aacSpectralDataResilienceFlag
= faad_get1bit(ld
152 DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
154 /* 1 bit: extensionFlag3 */
163 /* An MPEG-4 Audio decoder is only required to follow the Program
164 Configuration Element in GASpecificConfig(). The decoder shall ignore
165 any Program Configuration Elements that may occur in raw data blocks.
166 PCEs transmitted in raw data blocks cannot be used to convey decoder
167 configuration information.
169 static uint8_t program_config_element(program_config
*pce
, bitfile
*ld
)
173 memset(pce
, 0, sizeof(program_config
));
177 pce
->element_instance_tag
= (uint8_t)faad_getbits(ld
, 4
178 DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
180 pce
->object_type
= (uint8_t)faad_getbits(ld
, 2
181 DEBUGVAR(1,11,"program_config_element(): object_type"));
182 pce
->sf_index
= (uint8_t)faad_getbits(ld
, 4
183 DEBUGVAR(1,12,"program_config_element(): sf_index"));
184 pce
->num_front_channel_elements
= (uint8_t)faad_getbits(ld
, 4
185 DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
186 pce
->num_side_channel_elements
= (uint8_t)faad_getbits(ld
, 4
187 DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
188 pce
->num_back_channel_elements
= (uint8_t)faad_getbits(ld
, 4
189 DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
190 pce
->num_lfe_channel_elements
= (uint8_t)faad_getbits(ld
, 2
191 DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
192 pce
->num_assoc_data_elements
= (uint8_t)faad_getbits(ld
, 3
193 DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
194 pce
->num_valid_cc_elements
= (uint8_t)faad_getbits(ld
, 4
195 DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
197 pce
->mono_mixdown_present
= faad_get1bit(ld
198 DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
199 if (pce
->mono_mixdown_present
== 1)
201 pce
->mono_mixdown_element_number
= (uint8_t)faad_getbits(ld
, 4
202 DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
205 pce
->stereo_mixdown_present
= faad_get1bit(ld
206 DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
207 if (pce
->stereo_mixdown_present
== 1)
209 pce
->stereo_mixdown_element_number
= (uint8_t)faad_getbits(ld
, 4
210 DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
213 pce
->matrix_mixdown_idx_present
= faad_get1bit(ld
214 DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
215 if (pce
->matrix_mixdown_idx_present
== 1)
217 pce
->matrix_mixdown_idx
= (uint8_t)faad_getbits(ld
, 2
218 DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
219 pce
->pseudo_surround_enable
= faad_get1bit(ld
220 DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
223 for (i
= 0; i
< pce
->num_front_channel_elements
; i
++)
225 pce
->front_element_is_cpe
[i
] = faad_get1bit(ld
226 DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
227 pce
->front_element_tag_select
[i
] = (uint8_t)faad_getbits(ld
, 4
228 DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
230 if (pce
->front_element_is_cpe
[i
] & 1)
232 pce
->cpe_channel
[pce
->front_element_tag_select
[i
]] = pce
->channels
;
233 pce
->num_front_channels
+= 2;
236 pce
->sce_channel
[pce
->front_element_tag_select
[i
]] = pce
->channels
;
237 pce
->num_front_channels
++;
242 for (i
= 0; i
< pce
->num_side_channel_elements
; i
++)
244 pce
->side_element_is_cpe
[i
] = faad_get1bit(ld
245 DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
246 pce
->side_element_tag_select
[i
] = (uint8_t)faad_getbits(ld
, 4
247 DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
249 if (pce
->side_element_is_cpe
[i
] & 1)
251 pce
->cpe_channel
[pce
->side_element_tag_select
[i
]] = pce
->channels
;
252 pce
->num_side_channels
+= 2;
255 pce
->sce_channel
[pce
->side_element_tag_select
[i
]] = pce
->channels
;
256 pce
->num_side_channels
++;
261 for (i
= 0; i
< pce
->num_back_channel_elements
; i
++)
263 pce
->back_element_is_cpe
[i
] = faad_get1bit(ld
264 DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
265 pce
->back_element_tag_select
[i
] = (uint8_t)faad_getbits(ld
, 4
266 DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
268 if (pce
->back_element_is_cpe
[i
] & 1)
270 pce
->cpe_channel
[pce
->back_element_tag_select
[i
]] = pce
->channels
;
272 pce
->num_back_channels
+= 2;
274 pce
->sce_channel
[pce
->back_element_tag_select
[i
]] = pce
->channels
;
275 pce
->num_back_channels
++;
280 for (i
= 0; i
< pce
->num_lfe_channel_elements
; i
++)
282 pce
->lfe_element_tag_select
[i
] = (uint8_t)faad_getbits(ld
, 4
283 DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
285 pce
->sce_channel
[pce
->lfe_element_tag_select
[i
]] = pce
->channels
;
286 pce
->num_lfe_channels
++;
290 for (i
= 0; i
< pce
->num_assoc_data_elements
; i
++)
291 pce
->assoc_data_element_tag_select
[i
] = (uint8_t)faad_getbits(ld
, 4
292 DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
294 for (i
= 0; i
< pce
->num_valid_cc_elements
; i
++)
296 pce
->cc_element_is_ind_sw
[i
] = faad_get1bit(ld
297 DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
298 pce
->valid_cc_element_tag_select
[i
] = (uint8_t)faad_getbits(ld
, 4
299 DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
304 pce
->comment_field_bytes
= (uint8_t)faad_getbits(ld
, 8
305 DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
307 for (i
= 0; i
< pce
->comment_field_bytes
; i
++)
309 pce
->comment_field_data
[i
] = (uint8_t)faad_getbits(ld
, 8
310 DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
312 pce
->comment_field_data
[i
] = 0;
314 if (pce
->channels
> MAX_CHANNELS
)
320 static void decode_sce_lfe(NeAACDecHandle hDecoder
,
321 NeAACDecFrameInfo
*hInfo
, bitfile
*ld
,
324 uint8_t channels
= hDecoder
->fr_channels
;
327 if (channels
+1 > MAX_CHANNELS
)
332 if (hDecoder
->fr_ch_ele
+1 > MAX_SYNTAX_ELEMENTS
)
338 /* for SCE hDecoder->element_output_channels[] is not set here because this
339 can become 2 when some form of Parametric Stereo coding is used
342 /* save the syntax element id */
343 hDecoder
->element_id
[hDecoder
->fr_ch_ele
] = id_syn_ele
;
345 /* decode the element */
346 hInfo
->error
= single_lfe_channel_element(hDecoder
, ld
, channels
, &tag
);
348 /* map output channels position to internal data channels */
349 if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] == 2)
351 /* this might be faulty when pce_set is true */
352 hDecoder
->internal_channel
[channels
] = channels
;
353 hDecoder
->internal_channel
[channels
+1] = channels
+1;
355 if (hDecoder
->pce_set
)
356 hDecoder
->internal_channel
[hDecoder
->pce
.sce_channel
[tag
]] = channels
;
358 hDecoder
->internal_channel
[channels
] = channels
;
361 hDecoder
->fr_channels
+= hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
];
362 hDecoder
->fr_ch_ele
++;
365 static void decode_cpe(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
, bitfile
*ld
,
368 uint8_t channels
= hDecoder
->fr_channels
;
371 if (channels
+2 > MAX_CHANNELS
)
376 if (hDecoder
->fr_ch_ele
+1 > MAX_SYNTAX_ELEMENTS
)
382 /* for CPE the number of output channels is always 2 */
383 if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] == 0)
385 /* element_output_channels not set yet */
386 hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] = 2;
387 } else if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] != 2) {
388 /* element inconsistency */
393 /* save the syntax element id */
394 hDecoder
->element_id
[hDecoder
->fr_ch_ele
] = id_syn_ele
;
396 /* decode the element */
397 hInfo
->error
= channel_pair_element(hDecoder
, ld
, channels
, &tag
);
399 /* map output channel position to internal data channels */
400 if (hDecoder
->pce_set
)
402 hDecoder
->internal_channel
[hDecoder
->pce
.cpe_channel
[tag
]] = channels
;
403 hDecoder
->internal_channel
[hDecoder
->pce
.cpe_channel
[tag
]+1] = channels
+1;
405 hDecoder
->internal_channel
[channels
] = channels
;
406 hDecoder
->internal_channel
[channels
+1] = channels
+1;
409 hDecoder
->fr_channels
+= 2;
410 hDecoder
->fr_ch_ele
++;
413 void raw_data_block(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
414 bitfile
*ld
, program_config
*pce
, drc_info
*drc
)
418 hDecoder
->fr_channels
= 0;
419 hDecoder
->fr_ch_ele
= 0;
420 hDecoder
->first_syn_ele
= 25;
421 hDecoder
->has_lfe
= 0;
423 #ifdef ERROR_RESILIENCE
424 if (hDecoder
->object_type
< ER_OBJECT_START
)
427 /* Table 4.4.3: raw_data_block() */
428 while ((id_syn_ele
= (uint8_t)faad_getbits(ld
, LEN_SE_ID
429 DEBUGVAR(1,4,"NeAACDecDecode(): id_syn_ele"))) != ID_END
)
431 switch (id_syn_ele
) {
433 if (hDecoder
->first_syn_ele
== 25) hDecoder
->first_syn_ele
= id_syn_ele
;
434 decode_sce_lfe(hDecoder
, hInfo
, ld
, id_syn_ele
);
435 if (hInfo
->error
> 0)
439 if (hDecoder
->first_syn_ele
== 25) hDecoder
->first_syn_ele
= id_syn_ele
;
440 decode_cpe(hDecoder
, hInfo
, ld
, id_syn_ele
);
441 if (hInfo
->error
> 0)
446 decode_sce_lfe(hDecoder
, hInfo
, ld
, id_syn_ele
);
447 if (hInfo
->error
> 0)
450 case ID_CCE
: /* not implemented yet, but skip the bits */
452 hInfo
->error
= coupling_channel_element(hDecoder
, ld
);
456 if (hInfo
->error
> 0)
460 data_stream_element(hDecoder
, ld
);
463 /* 14496-4: 5.6.4.1.2.1.3: */
464 /* program_configuration_element()'s in access units shall be ignored */
465 program_config_element(pce
, ld
);
466 //if ((hInfo->error = program_config_element(pce, ld)) > 0)
468 //hDecoder->pce_set = 1;
471 /* one sbr_info describes a channel_element not a channel! */
472 /* if we encounter SBR data here: error */
473 /* SBR data will be read directly in the SCE/LFE/CPE element */
474 if ((hInfo
->error
= fill_element(hDecoder
, ld
, drc
476 , INVALID_SBR_ELEMENT
483 #ifdef ERROR_RESILIENCE
485 /* Table 262: er_raw_data_block() */
486 switch (hDecoder
->channelConfiguration
)
489 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
490 if (hInfo
->error
> 0)
494 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
495 if (hInfo
->error
> 0)
499 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
500 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
501 if (hInfo
->error
> 0)
505 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
506 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
507 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
508 if (hInfo
->error
> 0)
512 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
513 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
514 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
515 if (hInfo
->error
> 0)
519 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
520 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
521 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
522 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_LFE
);
523 if (hInfo
->error
> 0)
526 case 7: /* 8 channels */
527 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_SCE
);
528 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
529 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
530 decode_cpe(hDecoder
, hInfo
, ld
, ID_CPE
);
531 decode_sce_lfe(hDecoder
, hInfo
, ld
, ID_LFE
);
532 if (hInfo
->error
> 0)
540 cnt
= bits_to_decode() / 8;
543 cnt
-= extension_payload(cnt
);
549 /* new in corrigendum 14496-3:2002 */
551 if (hDecoder
->object_type
!= DRM_ER_LC
&& !hDecoder
->latm_header_present
)
560 /* Table 4.4.4 and */
562 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder
, bitfile
*ld
,
563 uint8_t channel
, uint8_t *tag
)
567 ic_stream
*ics
= &(sce
.ics1
);
568 ALIGN
int16_t spec_data
[1024] = {0};
570 sce
.element_instance_tag
= (uint8_t)faad_getbits(ld
, LEN_TAG
571 DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
573 *tag
= sce
.element_instance_tag
;
574 sce
.channel
= channel
;
575 sce
.paired_channel
= -1;
577 retval
= individual_channel_stream(hDecoder
, &sce
, ld
, ics
, 0, spec_data
);
582 /* check if next bitstream element is a fill element */
583 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
584 if (faad_showbits(ld
, LEN_SE_ID
) == ID_FIL
)
586 faad_flushbits(ld
, LEN_SE_ID
);
588 /* one sbr_info describes a channel_element not a channel! */
589 if ((retval
= fill_element(hDecoder
, ld
, hDecoder
->drc
, hDecoder
->fr_ch_ele
)) > 0)
596 /* noiseless coding is done, spectral reconstruction is done now */
597 retval
= reconstruct_single_channel(hDecoder
, ics
, &sce
, spec_data
);
605 static uint8_t channel_pair_element(NeAACDecHandle hDecoder
, bitfile
*ld
,
606 uint8_t channels
, uint8_t *tag
)
608 ALIGN
int16_t spec_data1
[1024] = {0};
609 ALIGN
int16_t spec_data2
[1024] = {0};
611 ic_stream
*ics1
= &(cpe
.ics1
);
612 ic_stream
*ics2
= &(cpe
.ics2
);
615 cpe
.channel
= channels
;
616 cpe
.paired_channel
= channels
+1;
618 cpe
.element_instance_tag
= (uint8_t)faad_getbits(ld
, LEN_TAG
619 DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
620 *tag
= cpe
.element_instance_tag
;
622 if ((cpe
.common_window
= faad_get1bit(ld
623 DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
625 /* both channels have common ics information */
626 if ((result
= ics_info(hDecoder
, ics1
, ld
, cpe
.common_window
)) > 0)
629 ics1
->ms_mask_present
= (uint8_t)faad_getbits(ld
, 2
630 DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
631 if (ics1
->ms_mask_present
== 1)
634 for (g
= 0; g
< ics1
->num_window_groups
; g
++)
636 for (sfb
= 0; sfb
< ics1
->max_sfb
; sfb
++)
638 ics1
->ms_used
[g
][sfb
] = faad_get1bit(ld
639 DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
644 #ifdef ERROR_RESILIENCE
645 if ((hDecoder
->object_type
>= ER_OBJECT_START
) && (ics1
->predictor_data_present
))
649 ics1
->ltp
.data_present
=
651 faad_get1bit(ld
DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
654 if ((result
= ltp_data(hDecoder
, ics1
, &(ics1
->ltp
), ld
)) > 0)
665 memcpy(ics2
, ics1
, sizeof(ic_stream
));
667 ics1
->ms_mask_present
= 0;
670 if ((result
= individual_channel_stream(hDecoder
, &cpe
, ld
, ics1
,
676 #ifdef ERROR_RESILIENCE
677 if (cpe
.common_window
&& (hDecoder
->object_type
>= ER_OBJECT_START
) &&
678 (ics1
->predictor_data_present
))
682 ics1
->ltp2
.data_present
=
684 faad_get1bit(ld
DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
687 if ((result
= ltp_data(hDecoder
, ics1
, &(ics1
->ltp2
), ld
)) > 0)
698 if ((result
= individual_channel_stream(hDecoder
, &cpe
, ld
, ics2
,
705 /* check if next bitstream element is a fill element */
706 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
707 if (faad_showbits(ld
, LEN_SE_ID
) == ID_FIL
)
709 faad_flushbits(ld
, LEN_SE_ID
);
711 /* one sbr_info describes a channel_element not a channel! */
712 if ((result
= fill_element(hDecoder
, ld
, hDecoder
->drc
, hDecoder
->fr_ch_ele
)) > 0)
719 /* noiseless coding is done, spectral reconstruction is done now */
720 if ((result
= reconstruct_channel_pair(hDecoder
, ics1
, ics2
, &cpe
,
721 spec_data1
, spec_data2
)) > 0)
730 static uint8_t ics_info(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
,
731 uint8_t common_window
)
735 /* ics->ics_reserved_bit = */ faad_get1bit(ld
736 DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
737 ics
->window_sequence
= (uint8_t)faad_getbits(ld
, 2
738 DEBUGVAR(1,44,"ics_info(): window_sequence"));
739 ics
->window_shape
= faad_get1bit(ld
740 DEBUGVAR(1,45,"ics_info(): window_shape"));
742 if (ics
->window_sequence
== EIGHT_SHORT_SEQUENCE
)
744 ics
->max_sfb
= (uint8_t)faad_getbits(ld
, 4
745 DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
746 ics
->scale_factor_grouping
= (uint8_t)faad_getbits(ld
, 7
747 DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
749 ics
->max_sfb
= (uint8_t)faad_getbits(ld
, 6
750 DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
753 /* get the grouping information */
754 if ((retval
= window_grouping_info(hDecoder
, ics
)) > 0)
757 /* should be an error */
758 /* check the range of max_sfb */
759 if (ics
->max_sfb
> ics
->num_swb
)
762 if (ics
->window_sequence
!= EIGHT_SHORT_SEQUENCE
)
764 if ((ics
->predictor_data_present
= faad_get1bit(ld
765 DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
767 if (hDecoder
->object_type
== MAIN
) /* MPEG2 style AAC predictor */
771 uint8_t limit
= min(ics
->max_sfb
, max_pred_sfb(hDecoder
->sf_index
));
773 ics
->pred
.limit
= limit
;
778 ics
->pred
.predictor_reset
=
780 faad_get1bit(ld
DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
783 ics
->pred
.predictor_reset_group_number
=
785 (uint8_t)faad_getbits(ld
, 5 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
788 for (sfb
= 0; sfb
< limit
; sfb
++)
791 ics
->pred
.prediction_used
[sfb
] =
793 faad_get1bit(ld
DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
797 else { /* Long Term Prediction */
798 if (hDecoder
->object_type
< ER_OBJECT_START
)
800 if ((ics
->ltp
.data_present
= faad_get1bit(ld
801 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
803 if ((retval
= ltp_data(hDecoder
, ics
, &(ics
->ltp
), ld
)) > 0)
810 if ((ics
->ltp2
.data_present
= faad_get1bit(ld
811 DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
813 if ((retval
= ltp_data(hDecoder
, ics
, &(ics
->ltp2
), ld
)) > 0)
820 #ifdef ERROR_RESILIENCE
821 if (!common_window
&& (hDecoder
->object_type
>= ER_OBJECT_START
))
823 if ((ics
->ltp
.data_present
= faad_get1bit(ld
824 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
826 ltp_data(hDecoder
, ics
, &(ics
->ltp
), ld
);
839 static uint8_t pulse_data(ic_stream
*ics
, pulse_info
*pul
, bitfile
*ld
)
843 pul
->number_pulse
= (uint8_t)faad_getbits(ld
, 2
844 DEBUGVAR(1,56,"pulse_data(): number_pulse"));
845 pul
->pulse_start_sfb
= (uint8_t)faad_getbits(ld
, 6
846 DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
848 /* check the range of pulse_start_sfb */
849 if (pul
->pulse_start_sfb
> ics
->num_swb
)
852 for (i
= 0; i
< pul
->number_pulse
+1; i
++)
854 pul
->pulse_offset
[i
] = (uint8_t)faad_getbits(ld
, 5
855 DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
857 printf("%d\n", pul
->pulse_offset
[i
]);
859 pul
->pulse_amp
[i
] = (uint8_t)faad_getbits(ld
, 4
860 DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
862 printf("%d\n", pul
->pulse_amp
[i
]);
870 /* Table 4.4.8: Currently just for skipping the bits... */
871 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder
, bitfile
*ld
)
873 uint8_t c
, result
= 0;
874 uint8_t ind_sw_cce_flag
= 0;
875 uint8_t num_gain_element_lists
= 0;
876 uint8_t num_coupled_elements
= 0;
878 element el_empty
= {0};
879 ic_stream ics_empty
= {0};
880 int16_t sh_data
[1024];
882 c
= faad_getbits(ld
, LEN_TAG
883 DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
885 ind_sw_cce_flag
= faad_get1bit(ld
886 DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
887 num_coupled_elements
= faad_getbits(ld
, 3
888 DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
890 for (c
= 0; c
< num_coupled_elements
+ 1; c
++)
892 uint8_t cc_target_is_cpe
, cc_target_tag_select
;
894 num_gain_element_lists
++;
896 cc_target_is_cpe
= faad_get1bit(ld
897 DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
898 cc_target_tag_select
= faad_getbits(ld
, 4
899 DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
901 if (cc_target_is_cpe
)
903 uint8_t cc_l
= faad_get1bit(ld
904 DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
905 uint8_t cc_r
= faad_get1bit(ld
906 DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
909 num_gain_element_lists
++;
914 DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
916 DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
918 DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
920 if ((result
= individual_channel_stream(hDecoder
, &el_empty
, ld
, &ics_empty
,
926 for (c
= 1; c
< num_gain_element_lists
; c
++)
934 cge
= faad_get1bit(ld
935 DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
940 huffman_scale_factor(ld
);
944 for (g
= 0; g
< ics_empty
.num_window_groups
; g
++)
946 for (sfb
= 0; sfb
< ics_empty
.max_sfb
; sfb
++)
948 if (ics_empty
.sfb_cb
[g
][sfb
] != ZERO_HCB
)
949 huffman_scale_factor(ld
);
960 static uint16_t data_stream_element(NeAACDecHandle hDecoder
, bitfile
*ld
)
962 uint8_t byte_aligned
;
965 /* element_instance_tag = */ faad_getbits(ld
, LEN_TAG
966 DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
967 byte_aligned
= faad_get1bit(ld
968 DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
969 count
= (uint16_t)faad_getbits(ld
, 8
970 DEBUGVAR(1,62,"data_stream_element(): count"));
973 count
+= (uint16_t)faad_getbits(ld
, 8
974 DEBUGVAR(1,63,"data_stream_element(): extra count"));
979 for (i
= 0; i
< count
; i
++)
981 faad_getbits(ld
, LEN_BYTE
982 DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
989 static uint8_t fill_element(NeAACDecHandle hDecoder
, bitfile
*ld
, drc_info
*drc
997 uint8_t bs_extension_type
;
1000 count
= (uint16_t)faad_getbits(ld
, 4
1001 DEBUGVAR(1,65,"fill_element(): count"));
1004 count
+= (uint16_t)faad_getbits(ld
, 8
1005 DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
1011 bs_extension_type
= (uint8_t)faad_showbits(ld
, 4);
1013 if ((bs_extension_type
== EXT_SBR_DATA
) ||
1014 (bs_extension_type
== EXT_SBR_DATA_CRC
))
1016 if (sbr_ele
== INVALID_SBR_ELEMENT
)
1019 if (!hDecoder
->sbr
[sbr_ele
])
1021 hDecoder
->sbr
[sbr_ele
] = sbrDecodeInit(hDecoder
->frameLength
,
1022 hDecoder
->element_id
[sbr_ele
], 2*get_sample_rate(hDecoder
->sf_index
),
1023 hDecoder
->downSampledSBR
1030 hDecoder
->sbr_present_flag
= 1;
1032 /* parse the SBR data */
1033 hDecoder
->sbr
[sbr_ele
]->ret
= sbr_extension_data(ld
, hDecoder
->sbr
[sbr_ele
], count
);
1036 if (hDecoder
->sbr
[sbr_ele
]->ret
> 0)
1038 printf("%s\n", NeAACDecGetErrorMessage(hDecoder
->sbr
[sbr_ele
]->ret
));
1042 #if (defined(PS_DEC) || defined(DRM_PS))
1043 if (hDecoder
->sbr
[sbr_ele
]->ps_used
)
1045 hDecoder
->ps_used
[sbr_ele
] = 1;
1047 /* set element independent flag to 1 as well */
1048 hDecoder
->ps_used_global
= 1;
1055 count
-= extension_payload(ld
, drc
, count
);
1067 static void gain_control_data(bitfile
*ld
, ic_stream
*ics
)
1070 ssr_info
*ssr
= &(ics
->ssr
);
1072 ssr
->max_band
= (uint8_t)faad_getbits(ld
, 2
1073 DEBUGVAR(1,1000,"gain_control_data(): max_band"));
1075 if (ics
->window_sequence
== ONLY_LONG_SEQUENCE
)
1077 for (bd
= 1; bd
<= ssr
->max_band
; bd
++)
1079 for (wd
= 0; wd
< 1; wd
++)
1081 ssr
->adjust_num
[bd
][wd
] = (uint8_t)faad_getbits(ld
, 3
1082 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1084 for (ad
= 0; ad
< ssr
->adjust_num
[bd
][wd
]; ad
++)
1086 ssr
->alevcode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 4
1087 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1088 ssr
->aloccode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 5
1089 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1093 } else if (ics
->window_sequence
== LONG_START_SEQUENCE
) {
1094 for (bd
= 1; bd
<= ssr
->max_band
; bd
++)
1096 for (wd
= 0; wd
< 2; wd
++)
1098 ssr
->adjust_num
[bd
][wd
] = (uint8_t)faad_getbits(ld
, 3
1099 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1101 for (ad
= 0; ad
< ssr
->adjust_num
[bd
][wd
]; ad
++)
1103 ssr
->alevcode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 4
1104 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1107 ssr
->aloccode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 4
1108 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1110 ssr
->aloccode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 2
1111 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1116 } else if (ics
->window_sequence
== EIGHT_SHORT_SEQUENCE
) {
1117 for (bd
= 1; bd
<= ssr
->max_band
; bd
++)
1119 for (wd
= 0; wd
< 8; wd
++)
1121 ssr
->adjust_num
[bd
][wd
] = (uint8_t)faad_getbits(ld
, 3
1122 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1124 for (ad
= 0; ad
< ssr
->adjust_num
[bd
][wd
]; ad
++)
1126 ssr
->alevcode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 4
1127 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1128 ssr
->aloccode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 2
1129 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1133 } else if (ics
->window_sequence
== LONG_STOP_SEQUENCE
) {
1134 for (bd
= 1; bd
<= ssr
->max_band
; bd
++)
1136 for (wd
= 0; wd
< 2; wd
++)
1138 ssr
->adjust_num
[bd
][wd
] = (uint8_t)faad_getbits(ld
, 3
1139 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1141 for (ad
= 0; ad
< ssr
->adjust_num
[bd
][wd
]; ad
++)
1143 ssr
->alevcode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 4
1144 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1148 ssr
->aloccode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 4
1149 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1151 ssr
->aloccode
[bd
][wd
][ad
] = (uint8_t)faad_getbits(ld
, 5
1152 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1162 /* Table 4.4.13 ASME */
1163 void aac_scalable_main_element(NeAACDecHandle hDecoder
, NeAACDecFrameInfo
*hInfo
,
1164 bitfile
*ld
, program_config
*pce
, drc_info
*drc
)
1167 uint8_t channels
= hDecoder
->fr_channels
= 0;
1169 uint8_t this_layer_stereo
= (hDecoder
->channelConfiguration
> 1) ? 1 : 0;
1171 ic_stream
*ics1
= &(cpe
.ics1
);
1172 ic_stream
*ics2
= &(cpe
.ics2
);
1174 ALIGN
int16_t spec_data1
[1024] = {0};
1175 ALIGN
int16_t spec_data2
[1024] = {0};
1177 hDecoder
->fr_ch_ele
= 0;
1179 hInfo
->error
= aac_scalable_main_header(hDecoder
, ics1
, ics2
, ld
, this_layer_stereo
);
1180 if (hInfo
->error
> 0)
1183 cpe
.common_window
= 1;
1184 if (this_layer_stereo
)
1186 hDecoder
->element_id
[0] = ID_CPE
;
1187 if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] == 0)
1188 hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] = 2;
1190 hDecoder
->element_id
[0] = ID_SCE
;
1193 for (ch
= 0; ch
< (this_layer_stereo
? 2 : 1); ch
++)
1199 spec_data
= spec_data1
;
1202 spec_data
= spec_data2
;
1205 hInfo
->error
= individual_channel_stream(hDecoder
, &cpe
, ld
, ics
, 1, spec_data
);
1206 if (hInfo
->error
> 0)
1212 /* In case of DRM we need to read the SBR info before channel reconstruction */
1213 if ((hDecoder
->sbr_present_flag
== 1) && (hDecoder
->object_type
== DRM_ER_LC
))
1215 bitfile ld_sbr
= {0};
1219 uint8_t *prevbufstart
;
1222 /* all forward bitreading should be finished at this point */
1223 uint32_t bitsconsumed
= faad_get_processed_bits(ld
);
1224 uint32_t buffer_size
= faad_origbitbuffer_size(ld
);
1225 uint8_t *buffer
= (uint8_t*)faad_origbitbuffer(ld
);
1227 if (bitsconsumed
+ 8 > buffer_size
*8)
1233 if (!hDecoder
->sbr
[0])
1235 hDecoder
->sbr
[0] = sbrDecodeInit(hDecoder
->frameLength
, hDecoder
->element_id
[0],
1236 2*get_sample_rate(hDecoder
->sf_index
), 0 /* ds SBR */, 1);
1239 /* Reverse bit reading of SBR data in DRM audio frame */
1240 revbuffer
= (uint8_t*)faad_malloc(buffer_size
*sizeof(uint8_t));
1241 prevbufstart
= revbuffer
;
1242 pbufend
= &buffer
[buffer_size
- 1];
1243 for (i
= 0; i
< buffer_size
; i
++)
1244 *prevbufstart
++ = tabFlipbits
[*pbufend
--];
1247 /* consider 8 bits from AAC-CRC */
1248 count
= (uint16_t)bit2byte(buffer_size
*8 - bitsconsumed
);
1249 faad_initbits(&ld_sbr
, revbuffer
, count
);
1251 hDecoder
->sbr
[0]->sample_rate
= get_sample_rate(hDecoder
->sf_index
);
1252 hDecoder
->sbr
[0]->sample_rate
*= 2;
1254 faad_getbits(&ld_sbr
, 8); /* Skip 8-bit CRC */
1256 hDecoder
->sbr
[0]->ret
= sbr_extension_data(&ld_sbr
, hDecoder
->sbr
[0], count
);
1257 #if (defined(PS_DEC) || defined(DRM_PS))
1258 if (hDecoder
->sbr
[0]->ps_used
)
1260 hDecoder
->ps_used
[0] = 1;
1261 hDecoder
->ps_used_global
= 1;
1266 /* no need to check it if there was already an error */
1267 if (hDecoder
->sbr
[0]->ret
== 0)
1268 hDecoder
->sbr
[0]->ret
= (uint8_t)faad_check_CRC(&ld_sbr
, (uint16_t)faad_get_processed_bits(&ld_sbr
) - 8);
1270 /* SBR data was corrupted, disable it until the next header */
1271 if (hDecoder
->sbr
[0]->ret
!= 0)
1273 hDecoder
->sbr
[0]->header_count
= 0;
1276 faad_endbits(&ld_sbr
);
1279 faad_free(revbuffer
);
1284 if (this_layer_stereo
)
1286 hInfo
->error
= reconstruct_channel_pair(hDecoder
, ics1
, ics2
, &cpe
, spec_data1
, spec_data2
);
1287 if (hInfo
->error
> 0)
1290 hInfo
->error
= reconstruct_single_channel(hDecoder
, ics1
, &cpe
, spec_data1
);
1291 if (hInfo
->error
> 0)
1295 /* map output channels position to internal data channels */
1296 if (hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
] == 2)
1298 /* this might be faulty when pce_set is true */
1299 hDecoder
->internal_channel
[channels
] = channels
;
1300 hDecoder
->internal_channel
[channels
+1] = channels
+1;
1302 hDecoder
->internal_channel
[channels
] = channels
;
1305 hDecoder
->fr_channels
+= hDecoder
->element_output_channels
[hDecoder
->fr_ch_ele
];
1306 hDecoder
->fr_ch_ele
++;
1312 static int8_t aac_scalable_main_header(NeAACDecHandle hDecoder
, ic_stream
*ics1
, ic_stream
*ics2
,
1313 bitfile
*ld
, uint8_t this_layer_stereo
)
1319 /* ics1->ics_reserved_bit = */ faad_get1bit(ld
1320 DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
1321 ics1
->window_sequence
= (uint8_t)faad_getbits(ld
, 2
1322 DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
1323 ics1
->window_shape
= faad_get1bit(ld
1324 DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
1326 if (ics1
->window_sequence
== EIGHT_SHORT_SEQUENCE
)
1328 ics1
->max_sfb
= (uint8_t)faad_getbits(ld
, 4
1329 DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
1330 ics1
->scale_factor_grouping
= (uint8_t)faad_getbits(ld
, 7
1331 DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
1333 ics1
->max_sfb
= (uint8_t)faad_getbits(ld
, 6
1334 DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
1337 /* get the grouping information */
1338 if ((retval
= window_grouping_info(hDecoder
, ics1
)) > 0)
1341 /* should be an error */
1342 /* check the range of max_sfb */
1343 if (ics1
->max_sfb
> ics1
->num_swb
)
1346 if (this_layer_stereo
)
1348 ics1
->ms_mask_present
= (uint8_t)faad_getbits(ld
, 2
1349 DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
1350 if (ics1
->ms_mask_present
== 1)
1353 for (g
= 0; g
< ics1
->num_window_groups
; g
++)
1355 for (sfb
= 0; sfb
< ics1
->max_sfb
; sfb
++)
1357 ics1
->ms_used
[g
][sfb
] = faad_get1bit(ld
1358 DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
1363 memcpy(ics2
, ics1
, sizeof(ic_stream
));
1365 ics1
->ms_mask_present
= 0;
1371 DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer"));
1374 for (ch
= 0; ch
< (this_layer_stereo
? 2 : 1); ch
++)
1381 if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/)
1383 if ((ics
->tns_data_present
= faad_get1bit(ld
1384 DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1)
1387 /* different order of data units in DRM */
1388 if (hDecoder
->object_type
!= DRM_ER_LC
)
1391 tns_data(ics
, &(ics
->tns
), ld
);
1396 if (0 /*core_flag || tvq_layer_pesent*/)
1398 if ((ch
==0) || ((ch
==1) && (core_stereo
|| tvq_stereo
))
1399 diff_control_data();
1400 if (mono_stereo_flag
)
1401 diff_control_data_lr();
1406 ics
->ltp
.data_present
=
1408 faad_get1bit(ld
DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1)
1411 if ((retval
= ltp_data(hDecoder
, ics
, &(ics
->ltp
), ld
)) > 0)
1429 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder
, element
*ele
,
1430 bitfile
*ld
, ic_stream
*ics
, uint8_t scal_flag
,
1435 ics
->global_gain
= (uint8_t)faad_getbits(ld
, 8
1436 DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
1438 if (!ele
->common_window
&& !scal_flag
)
1440 if ((result
= ics_info(hDecoder
, ics
, ld
, ele
->common_window
)) > 0)
1444 if ((result
= section_data(hDecoder
, ics
, ld
)) > 0)
1447 if ((result
= scale_factor_data(hDecoder
, ics
, ld
)) > 0)
1453 ** NOTE: It could be that pulse data is available in scalable AAC too,
1454 ** as said in Amendment 1, this could be only the case for ER AAC,
1455 ** though. (have to check this out later)
1457 /* get pulse data */
1458 if ((ics
->pulse_data_present
= faad_get1bit(ld
1459 DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
1461 if ((result
= pulse_data(ics
, &(ics
->pul
), ld
)) > 0)
1466 if ((ics
->tns_data_present
= faad_get1bit(ld
1467 DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
1469 #ifdef ERROR_RESILIENCE
1470 if (hDecoder
->object_type
< ER_OBJECT_START
)
1472 tns_data(ics
, &(ics
->tns
), ld
);
1475 /* get gain control data */
1476 if ((ics
->gain_control_data_present
= faad_get1bit(ld
1477 DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
1480 if (hDecoder
->object_type
!= SSR
)
1483 gain_control_data(ld
, ics
);
1490 #ifdef ERROR_RESILIENCE
1491 if (hDecoder
->aacSpectralDataResilienceFlag
)
1493 ics
->length_of_reordered_spectral_data
= (uint16_t)faad_getbits(ld
, 14
1494 DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
1496 if (hDecoder
->channelConfiguration
== 2)
1498 if (ics
->length_of_reordered_spectral_data
> 6144)
1499 ics
->length_of_reordered_spectral_data
= 6144;
1501 if (ics
->length_of_reordered_spectral_data
> 12288)
1502 ics
->length_of_reordered_spectral_data
= 12288;
1505 ics
->length_of_longest_codeword
= (uint8_t)faad_getbits(ld
, 6
1506 DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
1507 if (ics
->length_of_longest_codeword
>= 49)
1508 ics
->length_of_longest_codeword
= 49;
1511 /* RVLC spectral data is put here */
1512 if (hDecoder
->aacScalefactorDataResilienceFlag
)
1514 if ((result
= rvlc_decode_scale_factors(ics
, ld
)) > 0)
1518 if (hDecoder
->object_type
>= ER_OBJECT_START
)
1520 if (ics
->tns_data_present
)
1521 tns_data(ics
, &(ics
->tns
), ld
);
1526 if (hDecoder
->object_type
== DRM_ER_LC
)
1527 if ((result
= (uint8_t)faad_check_CRC(ld
, (uint16_t)faad_get_processed_bits(ld
) - 8)) > 0)
1531 if (hDecoder
->aacSpectralDataResilienceFlag
)
1533 /* error resilient spectral data decoding */
1534 if ((result
= reordered_spectral_data(hDecoder
, ics
, ld
, spec_data
)) > 0)
1540 /* decode the spectral data */
1541 if ((result
= spectral_data(hDecoder
, ics
, ld
, spec_data
)) > 0)
1545 #ifdef ERROR_RESILIENCE
1549 /* pulse coding reconstruction */
1550 if (ics
->pulse_data_present
)
1552 if (ics
->window_sequence
!= EIGHT_SHORT_SEQUENCE
)
1554 if ((result
= pulse_decode(ics
, spec_data
, hDecoder
->frameLength
)) > 0)
1557 return 2; /* pulse coding not allowed for short blocks */
1565 static uint8_t section_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
)
1568 uint8_t sect_esc_val
, sect_bits
;
1570 if (ics
->window_sequence
== EIGHT_SHORT_SEQUENCE
)
1574 sect_esc_val
= (1<<sect_bits
) - 1;
1577 printf("\ntotal sfb %d\n", ics
->max_sfb
);
1578 printf(" sect top cb\n");
1581 for (g
= 0; g
< ics
->num_window_groups
; g
++)
1586 while (k
< ics
->max_sfb
)
1588 #ifdef ERROR_RESILIENCE
1592 uint8_t sect_len_incr
;
1593 uint16_t sect_len
= 0;
1594 uint8_t sect_cb_bits
= 4;
1596 /* if "faad_getbits" detects error and returns "0", "k" is never
1597 incremented and we cannot leave the while loop */
1598 if ((ld
->error
!= 0) || (ld
->no_more_reading
))
1601 #ifdef ERROR_RESILIENCE
1602 if (hDecoder
->aacSectionDataResilienceFlag
)
1606 ics
->sect_cb
[g
][i
] = (uint8_t)faad_getbits(ld
, sect_cb_bits
1607 DEBUGVAR(1,71,"section_data(): sect_cb"));
1610 printf("%d\n", ics
->sect_cb
[g
][i
]);
1613 if (ics
->sect_cb
[g
][i
] == NOISE_HCB
)
1614 ics
->noise_used
= 1;
1616 #ifdef ERROR_RESILIENCE
1617 if (hDecoder
->aacSectionDataResilienceFlag
)
1619 if ((ics
->sect_cb
[g
][i
] == 11) ||
1620 ((ics
->sect_cb
[g
][i
] >= 16) && (ics
->sect_cb
[g
][i
] <= 32)))
1630 sect_len_incr
= (uint8_t)faad_getbits(ld
, sect_bits
1631 DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1632 #ifdef ERROR_RESILIENCE
1635 while ((sect_len_incr
== sect_esc_val
) /* &&
1636 (k+sect_len < ics->max_sfb)*/)
1638 sect_len
+= sect_len_incr
;
1639 sect_len_incr
= (uint8_t)faad_getbits(ld
, sect_bits
1640 DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1643 sect_len
+= sect_len_incr
;
1645 ics
->sect_start
[g
][i
] = k
;
1646 ics
->sect_end
[g
][i
] = k
+ sect_len
;
1649 printf("%d\n", ics
->sect_start
[g
][i
]);
1652 printf("%d\n", ics
->sect_end
[g
][i
]);
1655 if (k
+ sect_len
>= 8*15)
1660 for (sfb
= k
; sfb
< k
+ sect_len
; sfb
++)
1662 ics
->sfb_cb
[g
][sfb
] = ics
->sect_cb
[g
][i
];
1664 printf("%d\n", ics
->sfb_cb
[g
][sfb
]);
1669 printf(" %6d %6d %6d\n",
1671 ics
->sect_end
[g
][i
],
1672 ics
->sect_cb
[g
][i
]);
1678 ics
->num_sec
[g
] = i
;
1680 printf("%d\n", ics
->num_sec
[g
]);
1692 * decode_scale_factors()
1693 * decodes the scalefactors from the bitstream
1696 * All scalefactors (and also the stereo positions and pns energies) are
1697 * transmitted using Huffman coded DPCM relative to the previous active
1698 * scalefactor (respectively previous stereo position or previous pns energy,
1699 * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
1700 * differentially coded relative to the global gain.
1702 static uint8_t decode_scale_factors(ic_stream
*ics
, bitfile
*ld
)
1706 int8_t noise_pcm_flag
= 1;
1708 int16_t scale_factor
= ics
->global_gain
;
1709 int16_t is_position
= 0;
1710 int16_t noise_energy
= ics
->global_gain
- 90;
1712 for (g
= 0; g
< ics
->num_window_groups
; g
++)
1714 for (sfb
= 0; sfb
< ics
->max_sfb
; sfb
++)
1716 switch (ics
->sfb_cb
[g
][sfb
])
1718 case ZERO_HCB
: /* zero book */
1719 ics
->scale_factors
[g
][sfb
] = 0;
1722 printf("%d\n", ics
->scale_factors
[g
][sfb
]);
1725 case INTENSITY_HCB
: /* intensity books */
1726 case INTENSITY_HCB2
:
1728 /* decode intensity position */
1729 t
= huffman_scale_factor(ld
);
1730 is_position
+= (t
- 60);
1731 ics
->scale_factors
[g
][sfb
] = is_position
;
1733 printf("%d\n", ics
->scale_factors
[g
][sfb
]);
1737 case NOISE_HCB
: /* noise books */
1739 /* decode noise energy */
1743 t
= (int16_t)faad_getbits(ld
, 9
1744 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
1746 t
= huffman_scale_factor(ld
);
1750 ics
->scale_factors
[g
][sfb
] = noise_energy
;
1752 printf("%d\n", ics
->scale_factors
[g
][sfb
]);
1756 default: /* spectral books */
1758 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
1760 ics
->scale_factors
[g
][sfb
] = 0;
1762 /* decode scale factor */
1763 t
= huffman_scale_factor(ld
);
1764 scale_factor
+= (t
- 60);
1765 if (scale_factor
< 0 || scale_factor
> 255)
1767 ics
->scale_factors
[g
][sfb
] = scale_factor
;
1769 printf("%d\n", ics
->scale_factors
[g
][sfb
]);
1781 static uint8_t scale_factor_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
)
1785 int64_t count
= faad_get_ts();
1788 #ifdef ERROR_RESILIENCE
1789 if (!hDecoder
->aacScalefactorDataResilienceFlag
)
1792 ret
= decode_scale_factors(ics
, ld
);
1793 #ifdef ERROR_RESILIENCE
1795 /* In ER AAC the parameters for RVLC are seperated from the actual
1796 data that holds the scale_factors.
1797 Strangely enough, 2 parameters for HCR are put inbetween them.
1799 ret
= rvlc_scale_factor_data(ics
, ld
);
1804 count
= faad_get_ts() - count
;
1805 hDecoder
->scalefac_cycles
+= count
;
1812 static void tns_data(ic_stream
*ics
, tns_info
*tns
, bitfile
*ld
)
1814 uint8_t w
, filt
, i
, start_coef_bits
, coef_bits
;
1815 uint8_t n_filt_bits
= 2;
1816 uint8_t length_bits
= 6;
1817 uint8_t order_bits
= 5;
1819 if (ics
->window_sequence
== EIGHT_SHORT_SEQUENCE
)
1826 for (w
= 0; w
< ics
->num_windows
; w
++)
1828 tns
->n_filt
[w
] = (uint8_t)faad_getbits(ld
, n_filt_bits
1829 DEBUGVAR(1,74,"tns_data(): n_filt"));
1831 printf("%d\n", tns
->n_filt
[w
]);
1836 if ((tns
->coef_res
[w
] = faad_get1bit(ld
1837 DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
1839 start_coef_bits
= 4;
1841 start_coef_bits
= 3;
1844 printf("%d\n", tns
->coef_res
[w
]);
1848 for (filt
= 0; filt
< tns
->n_filt
[w
]; filt
++)
1850 tns
->length
[w
][filt
] = (uint8_t)faad_getbits(ld
, length_bits
1851 DEBUGVAR(1,76,"tns_data(): length"));
1853 printf("%d\n", tns
->length
[w
][filt
]);
1855 tns
->order
[w
][filt
] = (uint8_t)faad_getbits(ld
, order_bits
1856 DEBUGVAR(1,77,"tns_data(): order"));
1858 printf("%d\n", tns
->order
[w
][filt
]);
1860 if (tns
->order
[w
][filt
])
1862 tns
->direction
[w
][filt
] = faad_get1bit(ld
1863 DEBUGVAR(1,78,"tns_data(): direction"));
1865 printf("%d\n", tns
->direction
[w
][filt
]);
1867 tns
->coef_compress
[w
][filt
] = faad_get1bit(ld
1868 DEBUGVAR(1,79,"tns_data(): coef_compress"));
1870 printf("%d\n", tns
->coef_compress
[w
][filt
]);
1873 coef_bits
= start_coef_bits
- tns
->coef_compress
[w
][filt
];
1874 for (i
= 0; i
< tns
->order
[w
][filt
]; i
++)
1876 tns
->coef
[w
][filt
][i
] = (uint8_t)faad_getbits(ld
, coef_bits
1877 DEBUGVAR(1,80,"tns_data(): coef"));
1879 printf("%d\n", tns
->coef
[w
][filt
][i
]);
1889 static uint8_t ltp_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, ltp_info
*ltp
, bitfile
*ld
)
1896 if (hDecoder
->object_type
== LD
)
1898 ltp
->lag_update
= (uint8_t)faad_getbits(ld
, 1
1899 DEBUGVAR(1,142,"ltp_data(): lag_update"));
1901 if (ltp
->lag_update
)
1903 ltp
->lag
= (uint16_t)faad_getbits(ld
, 10
1904 DEBUGVAR(1,81,"ltp_data(): lag"));
1908 ltp
->lag
= (uint16_t)faad_getbits(ld
, 11
1909 DEBUGVAR(1,81,"ltp_data(): lag"));
1914 /* Check length of lag */
1915 if (ltp
->lag
> (hDecoder
->frameLength
<< 1))
1918 ltp
->coef
= (uint8_t)faad_getbits(ld
, 3
1919 DEBUGVAR(1,82,"ltp_data(): coef"));
1921 if (ics
->window_sequence
== EIGHT_SHORT_SEQUENCE
)
1923 for (w
= 0; w
< ics
->num_windows
; w
++)
1925 if ((ltp
->short_used
[w
] = faad_get1bit(ld
1926 DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
1928 ltp
->short_lag_present
[w
] = faad_get1bit(ld
1929 DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
1930 if (ltp
->short_lag_present
[w
])
1932 ltp
->short_lag
[w
] = (uint8_t)faad_getbits(ld
, 4
1933 DEBUGVAR(1,85,"ltp_data(): short_lag"));
1938 ltp
->last_band
= (ics
->max_sfb
< MAX_LTP_SFB
? ics
->max_sfb
: MAX_LTP_SFB
);
1940 for (sfb
= 0; sfb
< ltp
->last_band
; sfb
++)
1942 ltp
->long_used
[sfb
] = faad_get1bit(ld
1943 DEBUGVAR(1,86,"ltp_data(): long_used"));
1952 static uint8_t spectral_data(NeAACDecHandle hDecoder
, ic_stream
*ics
, bitfile
*ld
,
1953 int16_t *spectral_data
)
1957 uint16_t inc
, k
, p
= 0;
1961 uint16_t nshort
= hDecoder
->frameLength
/8;
1964 int64_t count
= faad_get_ts();
1967 for(g
= 0; g
< ics
->num_window_groups
; g
++)
1971 for (i
= 0; i
< ics
->num_sec
[g
]; i
++)
1973 sect_cb
= ics
->sect_cb
[g
][i
];
1975 inc
= (sect_cb
>= FIRST_PAIR_HCB
) ? 2 : 4;
1982 case INTENSITY_HCB2
:
1987 for (j
= ics
->sect_sfb_offset
[g
][ics
->sect_start
[g
][i
]]; j
< ics
->sect_sfb_offset
[g
][ics
->sect_end
[g
][i
]]; j
++)
1993 //#define SFBO_PRINT
1995 printf("%d\n", ics
->sect_sfb_offset
[g
][ics
->sect_start
[g
][i
]]);
1997 p
+= (ics
->sect_sfb_offset
[g
][ics
->sect_end
[g
][i
]] -
1998 ics
->sect_sfb_offset
[g
][ics
->sect_start
[g
][i
]]);
2002 printf("%d\n", ics
->sect_sfb_offset
[g
][ics
->sect_start
[g
][i
]]);
2004 for (k
= ics
->sect_sfb_offset
[g
][ics
->sect_start
[g
][i
]];
2005 k
< ics
->sect_sfb_offset
[g
][ics
->sect_end
[g
][i
]]; k
+= inc
)
2007 if ((result
= huffman_spectral_data(sect_cb
, ld
, &spectral_data
[p
])) > 0)
2012 for (j
= p
; j
< p
+inc
; j
++)
2014 printf("%d\n", spectral_data
[j
]);
2023 groups
+= ics
->window_group_length
[g
];
2027 count
= faad_get_ts() - count
;
2028 hDecoder
->spectral_cycles
+= count
;
2035 static uint16_t extension_payload(bitfile
*ld
, drc_info
*drc
, uint16_t count
)
2037 uint16_t i
, n
, dataElementLength
;
2038 uint8_t dataElementLengthPart
;
2039 uint8_t align
= 4, data_element_version
, loopCounter
;
2041 uint8_t extension_type
= (uint8_t)faad_getbits(ld
, 4
2042 DEBUGVAR(1,87,"extension_payload(): extension_type"));
2044 switch (extension_type
)
2046 case EXT_DYNAMIC_RANGE
:
2048 n
= dynamic_range_info(ld
, drc
);
2051 /* fill_nibble = */ faad_getbits(ld
, 4
2052 DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be ‘0000’ */
2053 for (i
= 0; i
< count
-1; i
++)
2055 /* fill_byte[i] = */ faad_getbits(ld
, 8
2056 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ‘10100101’ */
2059 case EXT_DATA_ELEMENT
:
2060 data_element_version
= (uint8_t)faad_getbits(ld
, 4
2061 DEBUGVAR(1,400,"extension_payload(): data_element_version"));
2062 switch (data_element_version
)
2066 dataElementLength
= 0;
2068 dataElementLengthPart
= (uint8_t)faad_getbits(ld
, 8
2069 DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
2070 dataElementLength
+= dataElementLengthPart
;
2072 } while (dataElementLengthPart
== 255);
2074 for (i
= 0; i
< dataElementLength
; i
++)
2076 /* data_element_byte[i] = */ faad_getbits(ld
, 8
2077 DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
2078 return (dataElementLength
+loopCounter
+1);
2085 faad_getbits(ld
, align
2086 DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
2087 for (i
= 0; i
< count
-1; i
++)
2089 /* other_bits[i] = */ faad_getbits(ld
, 8
2090 DEBUGVAR(1,89,"extension_payload(): fill_bit"));
2097 static uint8_t dynamic_range_info(bitfile
*ld
, drc_info
*drc
)
2105 DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
2107 drc
->pce_instance_tag
= (uint8_t)faad_getbits(ld
, 4
2108 DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag"));
2109 /* drc->drc_tag_reserved_bits = */ faad_getbits(ld
, 4
2110 DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits"));
2114 drc
->excluded_chns_present
= faad_get1bit(ld
2115 DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present"));
2116 if (drc
->excluded_chns_present
== 1)
2118 n
+= excluded_channels(ld
, drc
);
2122 DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
2124 band_incr
= (uint8_t)faad_getbits(ld
, 4
2125 DEBUGVAR(1,95,"dynamic_range_info(): band_incr"));
2126 /* drc->drc_bands_reserved_bits = */ faad_getbits(ld
, 4
2127 DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits"));
2129 drc
->num_bands
+= band_incr
;
2131 for (i
= 0; i
< drc
->num_bands
; i
++);
2133 drc
->band_top
[i
] = (uint8_t)faad_getbits(ld
, 8
2134 DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
2140 DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
2142 drc
->prog_ref_level
= (uint8_t)faad_getbits(ld
, 7
2143 DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level"));
2144 /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
2145 DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits"));
2149 for (i
= 0; i
< drc
->num_bands
; i
++)
2151 drc
->dyn_rng_sgn
[i
] = faad_get1bit(ld
2152 DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn"));
2153 drc
->dyn_rng_ctl
[i
] = (uint8_t)faad_getbits(ld
, 7
2154 DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl"));
2162 static uint8_t excluded_channels(bitfile
*ld
, drc_info
*drc
)
2165 uint8_t num_excl_chan
= 7;
2167 for (i
= 0; i
< 7; i
++)
2169 drc
->exclude_mask
[i
] = faad_get1bit(ld
2170 DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
2174 while ((drc
->additional_excluded_chns
[n
-1] = faad_get1bit(ld
2175 DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
2177 for (i
= num_excl_chan
; i
< num_excl_chan
+7; i
++)
2179 drc
->exclude_mask
[i
] = faad_get1bit(ld
2180 DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
2189 /* Annex A: Audio Interchange Formats */
2192 void get_adif_header(adif_header
*adif
, bitfile
*ld
)
2196 /* adif_id[0] = */ faad_getbits(ld
, 8
2197 DEBUGVAR(1,106,"get_adif_header(): adif_id[0]"));
2198 /* adif_id[1] = */ faad_getbits(ld
, 8
2199 DEBUGVAR(1,107,"get_adif_header(): adif_id[1]"));
2200 /* adif_id[2] = */ faad_getbits(ld
, 8
2201 DEBUGVAR(1,108,"get_adif_header(): adif_id[2]"));
2202 /* adif_id[3] = */ faad_getbits(ld
, 8
2203 DEBUGVAR(1,109,"get_adif_header(): adif_id[3]"));
2204 adif
->copyright_id_present
= faad_get1bit(ld
2205 DEBUGVAR(1,110,"get_adif_header(): copyright_id_present"));
2206 if(adif
->copyright_id_present
)
2208 for (i
= 0; i
< 72/8; i
++)
2210 adif
->copyright_id
[i
] = (int8_t)faad_getbits(ld
, 8
2211 DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
2213 adif
->copyright_id
[i
] = 0;
2215 adif
->original_copy
= faad_get1bit(ld
2216 DEBUGVAR(1,112,"get_adif_header(): original_copy"));
2217 adif
->home
= faad_get1bit(ld
2218 DEBUGVAR(1,113,"get_adif_header(): home"));
2219 adif
->bitstream_type
= faad_get1bit(ld
2220 DEBUGVAR(1,114,"get_adif_header(): bitstream_type"));
2221 adif
->bitrate
= faad_getbits(ld
, 23
2222 DEBUGVAR(1,115,"get_adif_header(): bitrate"));
2223 adif
->num_program_config_elements
= (uint8_t)faad_getbits(ld
, 4
2224 DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements"));
2226 for (i
= 0; i
< adif
->num_program_config_elements
+ 1; i
++)
2228 if(adif
->bitstream_type
== 0)
2230 adif
->adif_buffer_fullness
= faad_getbits(ld
, 20
2231 DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
2233 adif
->adif_buffer_fullness
= 0;
2236 program_config_element(&adif
->pce
[i
], ld
);
2241 uint8_t adts_frame(adts_header
*adts
, bitfile
*ld
)
2243 /* faad_byte_align(ld); */
2244 if (adts_fixed_header(adts
, ld
))
2246 adts_variable_header(adts
, ld
);
2247 adts_error_check(adts
, ld
);
2253 static uint8_t adts_fixed_header(adts_header
*adts
, bitfile
*ld
)
2256 uint8_t sync_err
= 1;
2258 /* try to recover from sync errors */
2259 for (i
= 0; i
< 768; i
++)
2261 adts
->syncword
= (uint16_t)faad_showbits(ld
, 12);
2262 if (adts
->syncword
!= 0xFFF)
2269 DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
2276 adts
->id
= faad_get1bit(ld
2277 DEBUGVAR(1,119,"adts_fixed_header(): id"));
2278 adts
->layer
= (uint8_t)faad_getbits(ld
, 2
2279 DEBUGVAR(1,120,"adts_fixed_header(): layer"));
2280 adts
->protection_absent
= faad_get1bit(ld
2281 DEBUGVAR(1,121,"adts_fixed_header(): protection_absent"));
2282 adts
->profile
= (uint8_t)faad_getbits(ld
, 2
2283 DEBUGVAR(1,122,"adts_fixed_header(): profile"));
2284 adts
->sf_index
= (uint8_t)faad_getbits(ld
, 4
2285 DEBUGVAR(1,123,"adts_fixed_header(): sf_index"));
2286 adts
->private_bit
= faad_get1bit(ld
2287 DEBUGVAR(1,124,"adts_fixed_header(): private_bit"));
2288 adts
->channel_configuration
= (uint8_t)faad_getbits(ld
, 3
2289 DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration"));
2290 adts
->original
= faad_get1bit(ld
2291 DEBUGVAR(1,126,"adts_fixed_header(): original"));
2292 adts
->home
= faad_get1bit(ld
2293 DEBUGVAR(1,127,"adts_fixed_header(): home"));
2295 if (adts
->old_format
== 1)
2297 /* Removed in corrigendum 14496-3:2002 */
2300 adts
->emphasis
= (uint8_t)faad_getbits(ld
, 2
2301 DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
2309 static void adts_variable_header(adts_header
*adts
, bitfile
*ld
)
2311 adts
->copyright_identification_bit
= faad_get1bit(ld
2312 DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit"));
2313 adts
->copyright_identification_start
= faad_get1bit(ld
2314 DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start"));
2315 adts
->aac_frame_length
= (uint16_t)faad_getbits(ld
, 13
2316 DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length"));
2317 adts
->adts_buffer_fullness
= (uint16_t)faad_getbits(ld
, 11
2318 DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness"));
2319 adts
->no_raw_data_blocks_in_frame
= (uint8_t)faad_getbits(ld
, 2
2320 DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame"));
2324 static void adts_error_check(adts_header
*adts
, bitfile
*ld
)
2326 if (adts
->protection_absent
== 0)
2328 adts
->crc_check
= (uint16_t)faad_getbits(ld
, 16
2329 DEBUGVAR(1,134,"adts_error_check(): crc_check"));
2333 /* LATM parsing functions */
2335 static uint32_t latm_get_value(bitfile
*ld
)
2338 uint8_t bytesForValue
;
2340 bytesForValue
= (uint8_t)faad_getbits(ld
, 2);
2342 for(l
=0; l
<bytesForValue
; l
++)
2343 value
= (value
<< 8) | (uint8_t)faad_getbits(ld
, 8);
2349 static uint32_t latmParsePayload(latm_header
*latm
, bitfile
*ld
)
2351 //assuming there's only one program with a single layer and 1 subFrame,
2352 //allStreamsSametimeframing is set,
2356 //this should be the payload length field for the current configuration
2358 if(latm
->framelen_type
==0)
2362 tmp
= (uint8_t)faad_getbits(ld
, 8);
2366 else if(latm
->framelen_type
==1)
2367 framelen
=latm
->frameLength
;
2373 static uint32_t latmAudioMuxElement(latm_header
*latm
, bitfile
*ld
)
2375 uint32_t ascLen
, asc_bits
=0;
2376 uint32_t x1
, y1
, m
, n
, i
;
2378 mp4AudioSpecificConfig mp4ASC
;
2380 latm
->useSameStreamMux
= (uint8_t)faad_getbits(ld
, 1);
2381 if(!latm
->useSameStreamMux
)
2383 //parseSameStreamMuxConfig
2384 latm
->version
= (uint8_t) faad_getbits(ld
, 1);
2386 latm
->versionA
= (uint8_t) faad_getbits(ld
, 1);
2389 //dunno the payload format for versionA
2390 fprintf(stderr
, "versionA not supported\n");
2393 if(latm
->version
) //read taraBufferFullness
2395 latm
->allStreamsSameTimeFraming
= (uint8_t)faad_getbits(ld
, 1);
2396 latm
->numSubFrames
= (uint8_t)faad_getbits(ld
, 6) + 1;
2397 latm
->numPrograms
= (uint8_t)faad_getbits(ld
, 4) + 1;
2398 latm
->numLayers
= faad_getbits(ld
, 3) + 1;
2399 if(latm
->numPrograms
>1 || !latm
->allStreamsSameTimeFraming
|| latm
->numSubFrames
>1 || latm
->numLayers
>1)
2401 fprintf(stderr
, "\r\nUnsupported LATM configuration: %d programs/ %d subframes, %d layers, allstreams: %d\n",
2402 latm
->numPrograms
, latm
->numSubFrames
, latm
->numLayers
, latm
->allStreamsSameTimeFraming
);
2407 ascLen
= latm_get_value(ld
);
2409 x1
= faad_get_processed_bits(ld
);
2410 if(AudioSpecificConfigFromBitfile(ld
, &mp4ASC
, &pce
, 0, 1) < 0)
2413 //horrid hack to unread the ASC bits and store them in latm->ASC
2414 //the correct code would rely on an ideal faad_ungetbits()
2415 y1
= faad_get_processed_bits(ld
);
2416 if((y1
-x1
) <= MAX_ASC_BYTES
*8)
2418 faad_rewindbits(ld
);
2423 faad_getbits(ld
, n
);
2428 m
= latm
->ASCbits
= y1
- x1
;
2432 latm
->ASC
[i
++] = (uint8_t) faad_getbits(ld
, n
);
2440 faad_getbits(ld
, ascLen
-asc_bits
);
2442 latm
->framelen_type
= (uint8_t) faad_getbits(ld
, 3);
2443 if(latm
->framelen_type
== 0)
2445 latm
->frameLength
= 0;
2446 faad_getbits(ld
, 8); //buffer fullness for frame_len_type==0, useless
2448 else if(latm
->framelen_type
== 1)
2450 latm
->frameLength
= faad_getbits(ld
, 9);
2451 if(latm
->frameLength
==0)
2453 fprintf(stderr
, "Invalid frameLength: 0\r\n");
2456 latm
->frameLength
= (latm
->frameLength
+20)*8;
2459 { //hellish CELP or HCVX stuff, discard
2460 fprintf(stderr
, "Unsupported CELP/HCVX framelentype: %d\n", latm
->framelen_type
);
2464 latm
->otherDataLenBits
= 0;
2465 if(faad_getbits(ld
, 1))
2466 { //other data present
2469 latm
->otherDataLenBits
= latm_get_value(ld
);
2472 esc
= faad_getbits(ld
, 1);
2473 tmp
= faad_getbits(ld
, 8);
2474 latm
->otherDataLenBits
= (latm
->otherDataLenBits
<< 8) + tmp
;
2477 if(faad_getbits(ld
, 1)) //crc
2478 faad_getbits(ld
, 8);
2484 return latmParsePayload(latm
, ld
);
2490 uint32_t faad_latm_frame(latm_header
*latm
, bitfile
*ld
)
2493 uint32_t initpos
, endpos
, firstpos
, ret
;
2495 firstpos
= faad_get_processed_bits(ld
);
2496 while(!ld
->error
&& !ld
->no_more_reading
)
2498 faad_byte_align(ld
);
2499 if(faad_showbits(ld
, 11) != 0x2B7)
2501 faad_getbits(ld
, 8);
2504 faad_getbits(ld
, 11);
2505 len
= faad_getbits(ld
, 13);
2508 initpos
= faad_get_processed_bits(ld
);
2509 ret
= latmAudioMuxElement(latm
, ld
);
2510 endpos
= faad_get_processed_bits(ld
);
2512 return (len
*8)-(endpos
-initpos
);
2513 //faad_getbits(ld, initpos-endpos); //go back to initpos, but is valid a getbits(-N) ?