10l: comparison of char* ptrs with string literals
[mplayer.git] / libfaad2 / syntax.c
blobf3d3c976f3acdc230683b25f1aaef141d4a13c8e
1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden.
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 $
26 **/
29 Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
32 #include "common.h"
33 #include "structs.h"
35 #include <stdlib.h>
36 #include <string.h>
38 #include "decoder.h"
39 #include "syntax.h"
40 #include "specrec.h"
41 #include "huffman.h"
42 #include "bits.h"
43 #include "pulse.h"
44 #include "analysis.h"
45 #include "drc.h"
46 #ifdef ERROR_RESILIENCE
47 #include "rvlc.h"
48 #endif
49 #ifdef SBR_DEC
50 #include "sbr_syntax.h"
51 #endif
54 /* static function declarations */
55 static void decode_sce_lfe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
56 uint8_t id_syn_ele);
57 static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
58 uint8_t id_syn_ele);
59 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
60 uint8_t channel, uint8_t *tag);
61 static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
62 uint8_t channel, uint8_t *tag);
63 #ifdef COUPLING_DEC
64 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld);
65 #endif
66 static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld);
67 static uint8_t program_config_element(program_config *pce, bitfile *ld);
68 static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
69 #ifdef SBR_DEC
70 ,uint8_t sbr_ele
71 #endif
73 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
74 bitfile *ld, ic_stream *ics, uint8_t scal_flag,
75 int16_t *spec_data);
76 static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
77 uint8_t common_window);
78 static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
79 static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
80 #ifdef SSR_DEC
81 static void gain_control_data(bitfile *ld, ic_stream *ics);
82 #endif
83 static uint8_t spectral_data(NeAACDecHandle 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 #ifdef LTP_DEC
89 static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
90 #endif
91 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
92 static void adts_variable_header(adts_header *adts, bitfile *ld);
93 static void adts_error_check(adts_header *adts, bitfile *ld);
94 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
95 static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
96 #ifdef SCALABLE_DEC
97 static int8_t aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
98 bitfile *ld, uint8_t this_layer_stereo);
99 #endif
102 /* Table 4.4.1 */
103 int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
104 program_config *pce_out)
106 program_config pce;
108 /* 1024 or 960 */
109 mp4ASC->frameLengthFlag = faad_get1bit(ld
110 DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
111 #ifndef ALLOW_SMALL_FRAMELENGTH
112 if (mp4ASC->frameLengthFlag == 1)
113 return -3;
114 #endif
116 mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
117 DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
118 if (mp4ASC->dependsOnCoreCoder == 1)
120 mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
121 DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
124 mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
125 if (mp4ASC->channelsConfiguration == 0)
127 if (program_config_element(&pce, ld))
128 return -3;
129 //mp4ASC->channelsConfiguration = pce.channels;
131 if (pce_out != NULL)
132 memcpy(pce_out, &pce, sizeof(program_config));
135 if (pce.num_valid_cc_elements)
136 return -3;
140 #ifdef ERROR_RESILIENCE
141 if (mp4ASC->extensionFlag == 1)
143 /* Error resilience not supported yet */
144 if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
146 mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
147 DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
148 mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
149 DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
150 mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
151 DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
153 /* 1 bit: extensionFlag3 */
156 #endif
158 return 0;
161 /* Table 4.4.2 */
162 /* An MPEG-4 Audio decoder is only required to follow the Program
163 Configuration Element in GASpecificConfig(). The decoder shall ignore
164 any Program Configuration Elements that may occur in raw data blocks.
165 PCEs transmitted in raw data blocks cannot be used to convey decoder
166 configuration information.
168 static uint8_t program_config_element(program_config *pce, bitfile *ld)
170 uint8_t i;
172 memset(pce, 0, sizeof(program_config));
174 pce->channels = 0;
176 pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
177 DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
179 pce->object_type = (uint8_t)faad_getbits(ld, 2
180 DEBUGVAR(1,11,"program_config_element(): object_type"));
181 pce->sf_index = (uint8_t)faad_getbits(ld, 4
182 DEBUGVAR(1,12,"program_config_element(): sf_index"));
183 pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
184 DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
185 pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
186 DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
187 pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
188 DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
189 pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
190 DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
191 pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
192 DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
193 pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
194 DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
196 pce->mono_mixdown_present = faad_get1bit(ld
197 DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
198 if (pce->mono_mixdown_present == 1)
200 pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
201 DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
204 pce->stereo_mixdown_present = faad_get1bit(ld
205 DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
206 if (pce->stereo_mixdown_present == 1)
208 pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
209 DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
212 pce->matrix_mixdown_idx_present = faad_get1bit(ld
213 DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
214 if (pce->matrix_mixdown_idx_present == 1)
216 pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
217 DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
218 pce->pseudo_surround_enable = faad_get1bit(ld
219 DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
222 for (i = 0; i < pce->num_front_channel_elements; i++)
224 pce->front_element_is_cpe[i] = faad_get1bit(ld
225 DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
226 pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
227 DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
229 if (pce->front_element_is_cpe[i] & 1)
231 pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
232 pce->num_front_channels += 2;
233 pce->channels += 2;
234 } else {
235 pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
236 pce->num_front_channels++;
237 pce->channels++;
241 for (i = 0; i < pce->num_side_channel_elements; i++)
243 pce->side_element_is_cpe[i] = faad_get1bit(ld
244 DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
245 pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
246 DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
248 if (pce->side_element_is_cpe[i] & 1)
250 pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
251 pce->num_side_channels += 2;
252 pce->channels += 2;
253 } else {
254 pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
255 pce->num_side_channels++;
256 pce->channels++;
260 for (i = 0; i < pce->num_back_channel_elements; i++)
262 pce->back_element_is_cpe[i] = faad_get1bit(ld
263 DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
264 pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
265 DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
267 if (pce->back_element_is_cpe[i] & 1)
269 pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
270 pce->channels += 2;
271 pce->num_back_channels += 2;
272 } else {
273 pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
274 pce->num_back_channels++;
275 pce->channels++;
279 for (i = 0; i < pce->num_lfe_channel_elements; i++)
281 pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
282 DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
284 pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
285 pce->num_lfe_channels++;
286 pce->channels++;
289 for (i = 0; i < pce->num_assoc_data_elements; i++)
290 pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
291 DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
293 for (i = 0; i < pce->num_valid_cc_elements; i++)
295 pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
296 DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
297 pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
298 DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
301 faad_byte_align(ld);
303 pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
304 DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
306 for (i = 0; i < pce->comment_field_bytes; i++)
308 pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
309 DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
311 pce->comment_field_data[i] = 0;
313 if (pce->channels > MAX_CHANNELS)
314 return 22;
316 return 0;
319 static void decode_sce_lfe(NeAACDecHandle hDecoder,
320 NeAACDecFrameInfo *hInfo, bitfile *ld,
321 uint8_t id_syn_ele)
323 uint8_t channels = hDecoder->fr_channels;
324 uint8_t tag = 0;
326 if (channels+1 > MAX_CHANNELS)
328 hInfo->error = 12;
329 return;
331 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
333 hInfo->error = 13;
334 return;
337 /* for SCE hDecoder->element_output_channels[] is not set here because this
338 can become 2 when some form of Parametric Stereo coding is used
341 /* save the syntax element id */
342 hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
344 /* decode the element */
345 hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
347 /* map output channels position to internal data channels */
348 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
350 /* this might be faulty when pce_set is true */
351 hDecoder->internal_channel[channels] = channels;
352 hDecoder->internal_channel[channels+1] = channels+1;
353 } else {
354 if (hDecoder->pce_set)
355 hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
356 else
357 hDecoder->internal_channel[channels] = channels;
360 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
361 hDecoder->fr_ch_ele++;
364 static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
365 uint8_t id_syn_ele)
367 uint8_t channels = hDecoder->fr_channels;
368 uint8_t tag = 0;
370 if (channels+2 > MAX_CHANNELS)
372 hInfo->error = 12;
373 return;
375 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
377 hInfo->error = 13;
378 return;
381 /* for CPE the number of output channels is always 2 */
382 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
384 /* element_output_channels not set yet */
385 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
386 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
387 /* element inconsistency */
388 hInfo->error = 21;
389 return;
392 /* save the syntax element id */
393 hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
395 /* decode the element */
396 hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
398 /* map output channel position to internal data channels */
399 if (hDecoder->pce_set)
401 hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
402 hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
403 } else {
404 hDecoder->internal_channel[channels] = channels;
405 hDecoder->internal_channel[channels+1] = channels+1;
408 hDecoder->fr_channels += 2;
409 hDecoder->fr_ch_ele++;
412 void raw_data_block(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
413 bitfile *ld, program_config *pce, drc_info *drc)
415 uint8_t id_syn_ele;
417 hDecoder->fr_channels = 0;
418 hDecoder->fr_ch_ele = 0;
419 hDecoder->first_syn_ele = 25;
420 hDecoder->has_lfe = 0;
422 #ifdef ERROR_RESILIENCE
423 if (hDecoder->object_type < ER_OBJECT_START)
425 #endif
426 /* Table 4.4.3: raw_data_block() */
427 while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID
428 DEBUGVAR(1,4,"NeAACDecDecode(): id_syn_ele"))) != ID_END)
430 switch (id_syn_ele) {
431 case ID_SCE:
432 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
433 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
434 if (hInfo->error > 0)
435 return;
436 break;
437 case ID_CPE:
438 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
439 decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
440 if (hInfo->error > 0)
441 return;
442 break;
443 case ID_LFE:
444 hDecoder->has_lfe++;
445 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
446 if (hInfo->error > 0)
447 return;
448 break;
449 case ID_CCE: /* not implemented yet, but skip the bits */
450 #ifdef COUPLING_DEC
451 hInfo->error = coupling_channel_element(hDecoder, ld);
452 #else
453 hInfo->error = 6;
454 #endif
455 if (hInfo->error > 0)
456 return;
457 break;
458 case ID_DSE:
459 data_stream_element(hDecoder, ld);
460 break;
461 case ID_PCE:
462 /* 14496-4: 5.6.4.1.2.1.3: */
463 /* program_configuration_element()'s in access units shall be ignored */
464 program_config_element(pce, ld);
465 //if ((hInfo->error = program_config_element(pce, ld)) > 0)
466 // return;
467 //hDecoder->pce_set = 1;
468 break;
469 case ID_FIL:
470 /* one sbr_info describes a channel_element not a channel! */
471 /* if we encounter SBR data here: error */
472 /* SBR data will be read directly in the SCE/LFE/CPE element */
473 if ((hInfo->error = fill_element(hDecoder, ld, drc
474 #ifdef SBR_DEC
475 , INVALID_SBR_ELEMENT
476 #endif
477 )) > 0)
478 return;
479 break;
482 #ifdef ERROR_RESILIENCE
483 } else {
484 /* Table 262: er_raw_data_block() */
485 switch (hDecoder->channelConfiguration)
487 case 1:
488 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
489 if (hInfo->error > 0)
490 return;
491 break;
492 case 2:
493 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
494 if (hInfo->error > 0)
495 return;
496 break;
497 case 3:
498 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
499 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
500 if (hInfo->error > 0)
501 return;
502 break;
503 case 4:
504 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
505 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
506 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
507 if (hInfo->error > 0)
508 return;
509 break;
510 case 5:
511 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
512 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
513 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
514 if (hInfo->error > 0)
515 return;
516 break;
517 case 6:
518 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
519 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
520 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
521 decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
522 if (hInfo->error > 0)
523 return;
524 break;
525 case 7: /* 8 channels */
526 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
527 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
528 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
529 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
530 decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
531 if (hInfo->error > 0)
532 return;
533 break;
534 default:
535 hInfo->error = 7;
536 return;
538 #if 0
539 cnt = bits_to_decode() / 8;
540 while (cnt >= 1)
542 cnt -= extension_payload(cnt);
544 #endif
546 #endif
548 /* new in corrigendum 14496-3:2002 */
549 #ifdef DRM
550 if (hDecoder->object_type != DRM_ER_LC)
551 #endif
553 faad_byte_align(ld);
556 return;
559 /* Table 4.4.4 and */
560 /* Table 4.4.9 */
561 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
562 uint8_t channel, uint8_t *tag)
564 uint8_t retval = 0;
565 element sce = {0};
566 ic_stream *ics = &(sce.ics1);
567 ALIGN int16_t spec_data[1024] = {0};
569 sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
570 DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
572 *tag = sce.element_instance_tag;
573 sce.channel = channel;
574 sce.paired_channel = -1;
576 retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
577 if (retval > 0)
578 return retval;
580 #ifdef SBR_DEC
581 /* check if next bitstream element is a fill element */
582 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
583 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
585 faad_flushbits(ld, LEN_SE_ID);
587 /* one sbr_info describes a channel_element not a channel! */
588 if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
590 return retval;
593 #endif
595 /* noiseless coding is done, spectral reconstruction is done now */
596 retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
597 if (retval > 0)
598 return retval;
600 return 0;
603 /* Table 4.4.5 */
604 static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
605 uint8_t channels, uint8_t *tag)
607 ALIGN int16_t spec_data1[1024] = {0};
608 ALIGN int16_t spec_data2[1024] = {0};
609 element cpe = {0};
610 ic_stream *ics1 = &(cpe.ics1);
611 ic_stream *ics2 = &(cpe.ics2);
612 uint8_t result;
614 cpe.channel = channels;
615 cpe.paired_channel = channels+1;
617 cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
618 DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
619 *tag = cpe.element_instance_tag;
621 if ((cpe.common_window = faad_get1bit(ld
622 DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
624 /* both channels have common ics information */
625 if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
626 return result;
628 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
629 DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
630 if (ics1->ms_mask_present == 1)
632 uint8_t g, sfb;
633 for (g = 0; g < ics1->num_window_groups; g++)
635 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
637 ics1->ms_used[g][sfb] = faad_get1bit(ld
638 DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
643 #ifdef ERROR_RESILIENCE
644 if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
646 if ((
647 #ifdef LTP_DEC
648 ics1->ltp.data_present =
649 #endif
650 faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
652 #ifdef LTP_DEC
653 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
655 return result;
657 #else
658 return 26;
659 #endif
662 #endif
664 memcpy(ics2, ics1, sizeof(ic_stream));
665 } else {
666 ics1->ms_mask_present = 0;
669 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
670 0, spec_data1)) > 0)
672 return result;
675 #ifdef ERROR_RESILIENCE
676 if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
677 (ics1->predictor_data_present))
679 if ((
680 #ifdef LTP_DEC
681 ics1->ltp2.data_present =
682 #endif
683 faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
685 #ifdef LTP_DEC
686 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
688 return result;
690 #else
691 return 26;
692 #endif
695 #endif
697 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
698 0, spec_data2)) > 0)
700 return result;
703 #ifdef SBR_DEC
704 /* check if next bitstream element is a fill element */
705 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
706 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
708 faad_flushbits(ld, LEN_SE_ID);
710 /* one sbr_info describes a channel_element not a channel! */
711 if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
713 return result;
716 #endif
718 /* noiseless coding is done, spectral reconstruction is done now */
719 if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
720 spec_data1, spec_data2)) > 0)
722 return result;
725 return 0;
728 /* Table 4.4.6 */
729 static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
730 uint8_t common_window)
732 uint8_t retval = 0;
734 /* ics->ics_reserved_bit = */ faad_get1bit(ld
735 DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
736 ics->window_sequence = (uint8_t)faad_getbits(ld, 2
737 DEBUGVAR(1,44,"ics_info(): window_sequence"));
738 ics->window_shape = faad_get1bit(ld
739 DEBUGVAR(1,45,"ics_info(): window_shape"));
741 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
743 ics->max_sfb = (uint8_t)faad_getbits(ld, 4
744 DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
745 ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
746 DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
747 } else {
748 ics->max_sfb = (uint8_t)faad_getbits(ld, 6
749 DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
752 /* get the grouping information */
753 if ((retval = window_grouping_info(hDecoder, ics)) > 0)
754 return retval;
756 /* should be an error */
757 /* check the range of max_sfb */
758 if (ics->max_sfb > ics->num_swb)
759 return 16;
761 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
763 if ((ics->predictor_data_present = faad_get1bit(ld
764 DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
766 if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
768 uint8_t sfb;
770 uint8_t limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
771 #ifdef MAIN_DEC
772 ics->pred.limit = limit;
773 #endif
775 if ((
776 #ifdef MAIN_DEC
777 ics->pred.predictor_reset =
778 #endif
779 faad_get1bit(ld DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
781 #ifdef MAIN_DEC
782 ics->pred.predictor_reset_group_number =
783 #endif
784 (uint8_t)faad_getbits(ld, 5 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
787 for (sfb = 0; sfb < limit; sfb++)
789 #ifdef MAIN_DEC
790 ics->pred.prediction_used[sfb] =
791 #endif
792 faad_get1bit(ld DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
795 #ifdef LTP_DEC
796 else { /* Long Term Prediction */
797 if (hDecoder->object_type < ER_OBJECT_START)
799 if ((ics->ltp.data_present = faad_get1bit(ld
800 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
802 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
804 return retval;
807 if (common_window)
809 if ((ics->ltp2.data_present = faad_get1bit(ld
810 DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
812 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
814 return retval;
819 #ifdef ERROR_RESILIENCE
820 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
822 if ((ics->ltp.data_present = faad_get1bit(ld
823 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
825 ltp_data(hDecoder, ics, &(ics->ltp), ld);
828 #endif
830 #endif
834 return retval;
837 /* Table 4.4.7 */
838 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
840 uint8_t i;
842 pul->number_pulse = (uint8_t)faad_getbits(ld, 2
843 DEBUGVAR(1,56,"pulse_data(): number_pulse"));
844 pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
845 DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
847 /* check the range of pulse_start_sfb */
848 if (pul->pulse_start_sfb > ics->num_swb)
849 return 16;
851 for (i = 0; i < pul->number_pulse+1; i++)
853 pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
854 DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
855 #if 0
856 printf("%d\n", pul->pulse_offset[i]);
857 #endif
858 pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
859 DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
860 #if 0
861 printf("%d\n", pul->pulse_amp[i]);
862 #endif
865 return 0;
868 #ifdef COUPLING_DEC
869 /* Table 4.4.8: Currently just for skipping the bits... */
870 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld)
872 uint8_t c, result = 0;
873 uint8_t ind_sw_cce_flag = 0;
874 uint8_t num_gain_element_lists = 0;
875 uint8_t num_coupled_elements = 0;
877 element el_empty = {0};
878 ic_stream ics_empty = {0};
879 int16_t sh_data[1024];
881 c = faad_getbits(ld, LEN_TAG
882 DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
884 ind_sw_cce_flag = faad_get1bit(ld
885 DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
886 num_coupled_elements = faad_getbits(ld, 3
887 DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
889 for (c = 0; c < num_coupled_elements + 1; c++)
891 uint8_t cc_target_is_cpe, cc_target_tag_select;
893 num_gain_element_lists++;
895 cc_target_is_cpe = faad_get1bit(ld
896 DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
897 cc_target_tag_select = faad_getbits(ld, 4
898 DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
900 if (cc_target_is_cpe)
902 uint8_t cc_l = faad_get1bit(ld
903 DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
904 uint8_t cc_r = faad_get1bit(ld
905 DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
907 if (cc_l && cc_r)
908 num_gain_element_lists++;
912 faad_get1bit(ld
913 DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
914 faad_get1bit(ld
915 DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
916 faad_getbits(ld, 2
917 DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
919 if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
920 0, sh_data)) > 0)
922 return result;
925 for (c = 1; c < num_gain_element_lists; c++)
927 uint8_t cge;
929 if (ind_sw_cce_flag)
931 cge = 1;
932 } else {
933 cge = faad_get1bit(ld
934 DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
937 if (cge)
939 huffman_scale_factor(ld);
940 } else {
941 uint8_t g, sfb;
943 for (g = 0; g < ics_empty.num_window_groups; g++)
945 for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
947 if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
948 huffman_scale_factor(ld);
954 return 0;
956 #endif
958 /* Table 4.4.10 */
959 static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld)
961 uint8_t byte_aligned;
962 uint16_t i, count;
964 /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
965 DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
966 byte_aligned = faad_get1bit(ld
967 DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
968 count = (uint16_t)faad_getbits(ld, 8
969 DEBUGVAR(1,62,"data_stream_element(): count"));
970 if (count == 255)
972 count += (uint16_t)faad_getbits(ld, 8
973 DEBUGVAR(1,63,"data_stream_element(): extra count"));
975 if (byte_aligned)
976 faad_byte_align(ld);
978 for (i = 0; i < count; i++)
980 faad_getbits(ld, LEN_BYTE
981 DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
984 return count;
987 /* Table 4.4.11 */
988 static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
989 #ifdef SBR_DEC
990 ,uint8_t sbr_ele
991 #endif
994 uint16_t count;
995 #ifdef SBR_DEC
996 uint8_t bs_extension_type;
997 #endif
999 count = (uint16_t)faad_getbits(ld, 4
1000 DEBUGVAR(1,65,"fill_element(): count"));
1001 if (count == 15)
1003 count += (uint16_t)faad_getbits(ld, 8
1004 DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
1007 if (count > 0)
1009 #ifdef SBR_DEC
1010 bs_extension_type = (uint8_t)faad_showbits(ld, 4);
1012 if ((bs_extension_type == EXT_SBR_DATA) ||
1013 (bs_extension_type == EXT_SBR_DATA_CRC))
1015 if (sbr_ele == INVALID_SBR_ELEMENT)
1016 return 24;
1018 if (!hDecoder->sbr[sbr_ele])
1020 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
1021 hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index),
1022 hDecoder->downSampledSBR
1023 #ifdef DRM
1025 #endif
1029 hDecoder->sbr_present_flag = 1;
1031 /* parse the SBR data */
1032 hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
1034 #if 0
1035 if (hDecoder->sbr[sbr_ele]->ret > 0)
1037 printf("%s\n", NeAACDecGetErrorMessage(hDecoder->sbr[sbr_ele]->ret));
1039 #endif
1041 #if (defined(PS_DEC) || defined(DRM_PS))
1042 if (hDecoder->sbr[sbr_ele]->ps_used)
1044 hDecoder->ps_used[sbr_ele] = 1;
1046 /* set element independent flag to 1 as well */
1047 hDecoder->ps_used_global = 1;
1049 #endif
1050 } else {
1051 #endif
1052 while (count > 0)
1054 count -= extension_payload(ld, drc, count);
1056 #ifdef SBR_DEC
1058 #endif
1061 return 0;
1064 /* Table 4.4.12 */
1065 #ifdef SSR_DEC
1066 static void gain_control_data(bitfile *ld, ic_stream *ics)
1068 uint8_t bd, wd, ad;
1069 ssr_info *ssr = &(ics->ssr);
1071 ssr->max_band = (uint8_t)faad_getbits(ld, 2
1072 DEBUGVAR(1,1000,"gain_control_data(): max_band"));
1074 if (ics->window_sequence == ONLY_LONG_SEQUENCE)
1076 for (bd = 1; bd <= ssr->max_band; bd++)
1078 for (wd = 0; wd < 1; wd++)
1080 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1081 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1083 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1085 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1086 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1087 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1088 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1092 } else if (ics->window_sequence == LONG_START_SEQUENCE) {
1093 for (bd = 1; bd <= ssr->max_band; bd++)
1095 for (wd = 0; wd < 2; wd++)
1097 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1098 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1100 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1102 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1103 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1104 if (wd == 0)
1106 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1107 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1108 } else {
1109 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1110 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1115 } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1116 for (bd = 1; bd <= ssr->max_band; bd++)
1118 for (wd = 0; wd < 8; wd++)
1120 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1121 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1123 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1125 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1126 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1127 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1128 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1132 } else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
1133 for (bd = 1; bd <= ssr->max_band; bd++)
1135 for (wd = 0; wd < 2; wd++)
1137 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1138 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1140 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1142 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1143 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1145 if (wd == 0)
1147 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1148 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1149 } else {
1150 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1151 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1158 #endif
1160 #ifdef SCALABLE_DEC
1161 /* Table 4.4.13 ASME */
1162 void aac_scalable_main_element(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
1163 bitfile *ld, program_config *pce, drc_info *drc)
1165 uint8_t retval = 0;
1166 uint8_t channels = hDecoder->fr_channels = 0;
1167 uint8_t ch;
1168 uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
1169 element cpe = {0};
1170 ic_stream *ics1 = &(cpe.ics1);
1171 ic_stream *ics2 = &(cpe.ics2);
1172 int16_t *spec_data;
1173 ALIGN int16_t spec_data1[1024] = {0};
1174 ALIGN int16_t spec_data2[1024] = {0};
1176 hDecoder->fr_ch_ele = 0;
1178 hInfo->error = aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
1179 if (hInfo->error > 0)
1180 return;
1182 cpe.common_window = 1;
1183 if (this_layer_stereo)
1185 hDecoder->element_id[0] = ID_CPE;
1186 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
1187 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
1188 } else {
1189 hDecoder->element_id[0] = ID_SCE;
1192 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1194 ic_stream *ics;
1195 if (ch == 0)
1197 ics = ics1;
1198 spec_data = spec_data1;
1199 } else {
1200 ics = ics2;
1201 spec_data = spec_data2;
1204 hInfo->error = individual_channel_stream(hDecoder, &cpe, ld, ics, 1, spec_data);
1205 if (hInfo->error > 0)
1206 return;
1209 #ifdef DRM
1210 #ifdef SBR_DEC
1211 /* In case of DRM we need to read the SBR info before channel reconstruction */
1212 if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
1214 bitfile ld_sbr = {0};
1215 uint32_t i;
1216 uint16_t count = 0;
1217 uint8_t *revbuffer;
1218 uint8_t *prevbufstart;
1219 uint8_t *pbufend;
1221 /* all forward bitreading should be finished at this point */
1222 uint32_t bitsconsumed = faad_get_processed_bits(ld);
1223 uint32_t buffer_size = faad_origbitbuffer_size(ld);
1224 uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
1226 if (bitsconsumed + 8 > buffer_size*8)
1228 hInfo->error = 14;
1229 return;
1232 if (!hDecoder->sbr[0])
1234 hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[0],
1235 2*get_sample_rate(hDecoder->sf_index), 0 /* ds SBR */, 1);
1238 /* Reverse bit reading of SBR data in DRM audio frame */
1239 revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
1240 prevbufstart = revbuffer;
1241 pbufend = &buffer[buffer_size - 1];
1242 for (i = 0; i < buffer_size; i++)
1243 *prevbufstart++ = tabFlipbits[*pbufend--];
1245 /* Set SBR data */
1246 /* consider 8 bits from AAC-CRC */
1247 count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
1248 faad_initbits(&ld_sbr, revbuffer, count);
1250 hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
1251 hDecoder->sbr[0]->sample_rate *= 2;
1253 faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
1255 hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
1256 #if (defined(PS_DEC) || defined(DRM_PS))
1257 if (hDecoder->sbr[0]->ps_used)
1259 hDecoder->ps_used[0] = 1;
1260 hDecoder->ps_used_global = 1;
1262 #endif
1264 /* check CRC */
1265 /* no need to check it if there was already an error */
1266 if (hDecoder->sbr[0]->ret == 0)
1267 hDecoder->sbr[0]->ret = (uint8_t)faad_check_CRC(&ld_sbr, (uint16_t)faad_get_processed_bits(&ld_sbr) - 8);
1269 /* SBR data was corrupted, disable it until the next header */
1270 if (hDecoder->sbr[0]->ret != 0)
1272 hDecoder->sbr[0]->header_count = 0;
1275 faad_endbits(&ld_sbr);
1277 if (revbuffer)
1278 faad_free(revbuffer);
1280 #endif
1281 #endif
1283 if (this_layer_stereo)
1285 hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
1286 if (hInfo->error > 0)
1287 return;
1288 } else {
1289 hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
1290 if (hInfo->error > 0)
1291 return;
1294 /* map output channels position to internal data channels */
1295 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
1297 /* this might be faulty when pce_set is true */
1298 hDecoder->internal_channel[channels] = channels;
1299 hDecoder->internal_channel[channels+1] = channels+1;
1300 } else {
1301 hDecoder->internal_channel[channels] = channels;
1304 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
1305 hDecoder->fr_ch_ele++;
1307 return;
1310 /* Table 4.4.15 */
1311 static int8_t aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
1312 bitfile *ld, uint8_t this_layer_stereo)
1314 uint8_t retval = 0;
1315 uint8_t ch;
1316 ic_stream *ics;
1318 /* ics1->ics_reserved_bit = */ faad_get1bit(ld
1319 DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
1320 ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
1321 DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
1322 ics1->window_shape = faad_get1bit(ld
1323 DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
1325 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
1327 ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
1328 DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
1329 ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
1330 DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
1331 } else {
1332 ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
1333 DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
1336 /* get the grouping information */
1337 if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
1338 return retval;
1340 /* should be an error */
1341 /* check the range of max_sfb */
1342 if (ics1->max_sfb > ics1->num_swb)
1343 return 16;
1345 if (this_layer_stereo)
1347 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
1348 DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
1349 if (ics1->ms_mask_present == 1)
1351 uint8_t g, sfb;
1352 for (g = 0; g < ics1->num_window_groups; g++)
1354 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
1356 ics1->ms_used[g][sfb] = faad_get1bit(ld
1357 DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
1362 memcpy(ics2, ics1, sizeof(ic_stream));
1363 } else {
1364 ics1->ms_mask_present = 0;
1367 if (0)
1369 faad_get1bit(ld
1370 DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer"));
1373 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1375 if (ch == 0)
1376 ics = ics1;
1377 else
1378 ics = ics2;
1380 if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/)
1382 if ((ics->tns_data_present = faad_get1bit(ld
1383 DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1)
1385 #ifdef DRM
1386 /* different order of data units in DRM */
1387 if (hDecoder->object_type != DRM_ER_LC)
1388 #endif
1390 tns_data(ics, &(ics->tns), ld);
1394 #if 0
1395 if (0 /*core_flag || tvq_layer_pesent*/)
1397 if ((ch==0) || ((ch==1) && (core_stereo || tvq_stereo))
1398 diff_control_data();
1399 if (mono_stereo_flag)
1400 diff_control_data_lr();
1401 } else {
1402 #endif
1403 if ((
1404 #ifdef LTP_DEC
1405 ics->ltp.data_present =
1406 #endif
1407 faad_get1bit(ld DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1)
1409 #ifdef LTP_DEC
1410 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
1412 return retval;
1414 #else
1415 return 26;
1416 #endif
1418 #if 0
1420 #endif
1423 return 0;
1425 #endif
1427 /* Table 4.4.24 */
1428 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
1429 bitfile *ld, ic_stream *ics, uint8_t scal_flag,
1430 int16_t *spec_data)
1432 uint8_t result;
1434 ics->global_gain = (uint8_t)faad_getbits(ld, 8
1435 DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
1437 if (!ele->common_window && !scal_flag)
1439 if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
1440 return result;
1443 if ((result = section_data(hDecoder, ics, ld)) > 0)
1444 return result;
1446 if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
1447 return result;
1449 if (!scal_flag)
1452 ** NOTE: It could be that pulse data is available in scalable AAC too,
1453 ** as said in Amendment 1, this could be only the case for ER AAC,
1454 ** though. (have to check this out later)
1456 /* get pulse data */
1457 if ((ics->pulse_data_present = faad_get1bit(ld
1458 DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
1460 if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
1461 return result;
1464 /* get tns data */
1465 if ((ics->tns_data_present = faad_get1bit(ld
1466 DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
1468 #ifdef ERROR_RESILIENCE
1469 if (hDecoder->object_type < ER_OBJECT_START)
1470 #endif
1471 tns_data(ics, &(ics->tns), ld);
1474 /* get gain control data */
1475 if ((ics->gain_control_data_present = faad_get1bit(ld
1476 DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
1478 #ifdef SSR_DEC
1479 if (hDecoder->object_type != SSR)
1480 return 1;
1481 else
1482 gain_control_data(ld, ics);
1483 #else
1484 return 1;
1485 #endif
1489 #ifdef ERROR_RESILIENCE
1490 if (hDecoder->aacSpectralDataResilienceFlag)
1492 ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
1493 DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
1495 if (hDecoder->channelConfiguration == 2)
1497 if (ics->length_of_reordered_spectral_data > 6144)
1498 ics->length_of_reordered_spectral_data = 6144;
1499 } else {
1500 if (ics->length_of_reordered_spectral_data > 12288)
1501 ics->length_of_reordered_spectral_data = 12288;
1504 ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6
1505 DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
1506 if (ics->length_of_longest_codeword >= 49)
1507 ics->length_of_longest_codeword = 49;
1510 /* RVLC spectral data is put here */
1511 if (hDecoder->aacScalefactorDataResilienceFlag)
1513 if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
1514 return result;
1517 if (hDecoder->object_type >= ER_OBJECT_START)
1519 if (ics->tns_data_present)
1520 tns_data(ics, &(ics->tns), ld);
1523 #ifdef DRM
1524 /* CRC check */
1525 if (hDecoder->object_type == DRM_ER_LC)
1526 if ((result = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
1527 return result;
1528 #endif
1530 if (hDecoder->aacSpectralDataResilienceFlag)
1532 /* error resilient spectral data decoding */
1533 if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1535 return result;
1537 } else {
1538 #endif
1539 /* decode the spectral data */
1540 if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1542 return result;
1544 #ifdef ERROR_RESILIENCE
1546 #endif
1548 /* pulse coding reconstruction */
1549 if (ics->pulse_data_present)
1551 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
1553 if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
1554 return result;
1555 } else {
1556 return 2; /* pulse coding not allowed for short blocks */
1560 return 0;
1563 /* Table 4.4.25 */
1564 static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1566 uint8_t g;
1567 uint8_t sect_esc_val, sect_bits;
1569 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1570 sect_bits = 3;
1571 else
1572 sect_bits = 5;
1573 sect_esc_val = (1<<sect_bits) - 1;
1575 #if 0
1576 printf("\ntotal sfb %d\n", ics->max_sfb);
1577 printf(" sect top cb\n");
1578 #endif
1580 for (g = 0; g < ics->num_window_groups; g++)
1582 uint8_t k = 0;
1583 uint8_t i = 0;
1585 while (k < ics->max_sfb)
1587 #ifdef ERROR_RESILIENCE
1588 uint8_t vcb11 = 0;
1589 #endif
1590 uint8_t sfb;
1591 uint8_t sect_len_incr;
1592 uint16_t sect_len = 0;
1593 uint8_t sect_cb_bits = 4;
1595 /* if "faad_getbits" detects error and returns "0", "k" is never
1596 incremented and we cannot leave the while loop */
1597 if ((ld->error != 0) || (ld->no_more_reading))
1598 return 14;
1600 #ifdef ERROR_RESILIENCE
1601 if (hDecoder->aacSectionDataResilienceFlag)
1602 sect_cb_bits = 5;
1603 #endif
1605 ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
1606 DEBUGVAR(1,71,"section_data(): sect_cb"));
1608 #if 0
1609 printf("%d\n", ics->sect_cb[g][i]);
1610 #endif
1612 if (ics->sect_cb[g][i] == NOISE_HCB)
1613 ics->noise_used = 1;
1615 #ifdef ERROR_RESILIENCE
1616 if (hDecoder->aacSectionDataResilienceFlag)
1618 if ((ics->sect_cb[g][i] == 11) ||
1619 ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
1621 vcb11 = 1;
1624 if (vcb11)
1626 sect_len_incr = 1;
1627 } else {
1628 #endif
1629 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1630 DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1631 #ifdef ERROR_RESILIENCE
1633 #endif
1634 while ((sect_len_incr == sect_esc_val) /* &&
1635 (k+sect_len < ics->max_sfb)*/)
1637 sect_len += sect_len_incr;
1638 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1639 DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1642 sect_len += sect_len_incr;
1644 ics->sect_start[g][i] = k;
1645 ics->sect_end[g][i] = k + sect_len;
1647 #if 0
1648 printf("%d\n", ics->sect_start[g][i]);
1649 #endif
1650 #if 0
1651 printf("%d\n", ics->sect_end[g][i]);
1652 #endif
1654 if (k + sect_len >= 8*15)
1655 return 15;
1656 if (i >= 8*15)
1657 return 15;
1659 for (sfb = k; sfb < k + sect_len; sfb++)
1661 ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
1662 #if 0
1663 printf("%d\n", ics->sfb_cb[g][sfb]);
1664 #endif
1667 #if 0
1668 printf(" %6d %6d %6d\n",
1670 ics->sect_end[g][i],
1671 ics->sect_cb[g][i]);
1672 #endif
1674 k += sect_len;
1675 i++;
1677 ics->num_sec[g] = i;
1678 #if 0
1679 printf("%d\n", ics->num_sec[g]);
1680 #endif
1683 #if 0
1684 printf("\n");
1685 #endif
1687 return 0;
1691 * decode_scale_factors()
1692 * decodes the scalefactors from the bitstream
1695 * All scalefactors (and also the stereo positions and pns energies) are
1696 * transmitted using Huffman coded DPCM relative to the previous active
1697 * scalefactor (respectively previous stereo position or previous pns energy,
1698 * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
1699 * differentially coded relative to the global gain.
1701 static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
1703 uint8_t g, sfb;
1704 int16_t t;
1705 int8_t noise_pcm_flag = 1;
1707 int16_t scale_factor = ics->global_gain;
1708 int16_t is_position = 0;
1709 int16_t noise_energy = ics->global_gain - 90;
1711 for (g = 0; g < ics->num_window_groups; g++)
1713 for (sfb = 0; sfb < ics->max_sfb; sfb++)
1715 switch (ics->sfb_cb[g][sfb])
1717 case ZERO_HCB: /* zero book */
1718 ics->scale_factors[g][sfb] = 0;
1719 //#define SF_PRINT
1720 #ifdef SF_PRINT
1721 printf("%d\n", ics->scale_factors[g][sfb]);
1722 #endif
1723 break;
1724 case INTENSITY_HCB: /* intensity books */
1725 case INTENSITY_HCB2:
1727 /* decode intensity position */
1728 t = huffman_scale_factor(ld);
1729 is_position += (t - 60);
1730 ics->scale_factors[g][sfb] = is_position;
1731 #ifdef SF_PRINT
1732 printf("%d\n", ics->scale_factors[g][sfb]);
1733 #endif
1735 break;
1736 case NOISE_HCB: /* noise books */
1738 /* decode noise energy */
1739 if (noise_pcm_flag)
1741 noise_pcm_flag = 0;
1742 t = (int16_t)faad_getbits(ld, 9
1743 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
1744 } else {
1745 t = huffman_scale_factor(ld);
1746 t -= 60;
1748 noise_energy += t;
1749 ics->scale_factors[g][sfb] = noise_energy;
1750 #ifdef SF_PRINT
1751 printf("%d\n", ics->scale_factors[g][sfb]);
1752 #endif
1754 break;
1755 default: /* spectral books */
1757 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
1759 ics->scale_factors[g][sfb] = 0;
1761 /* decode scale factor */
1762 t = huffman_scale_factor(ld);
1763 scale_factor += (t - 60);
1764 if (scale_factor < 0 || scale_factor > 255)
1765 return 4;
1766 ics->scale_factors[g][sfb] = scale_factor;
1767 #ifdef SF_PRINT
1768 printf("%d\n", ics->scale_factors[g][sfb]);
1769 #endif
1771 break;
1776 return 0;
1779 /* Table 4.4.26 */
1780 static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1782 uint8_t ret = 0;
1783 #ifdef PROFILE
1784 int64_t count = faad_get_ts();
1785 #endif
1787 #ifdef ERROR_RESILIENCE
1788 if (!hDecoder->aacScalefactorDataResilienceFlag)
1790 #endif
1791 ret = decode_scale_factors(ics, ld);
1792 #ifdef ERROR_RESILIENCE
1793 } else {
1794 /* In ER AAC the parameters for RVLC are seperated from the actual
1795 data that holds the scale_factors.
1796 Strangely enough, 2 parameters for HCR are put inbetween them.
1798 ret = rvlc_scale_factor_data(ics, ld);
1800 #endif
1802 #ifdef PROFILE
1803 count = faad_get_ts() - count;
1804 hDecoder->scalefac_cycles += count;
1805 #endif
1807 return ret;
1810 /* Table 4.4.27 */
1811 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
1813 uint8_t w, filt, i, start_coef_bits, coef_bits;
1814 uint8_t n_filt_bits = 2;
1815 uint8_t length_bits = 6;
1816 uint8_t order_bits = 5;
1818 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1820 n_filt_bits = 1;
1821 length_bits = 4;
1822 order_bits = 3;
1825 for (w = 0; w < ics->num_windows; w++)
1827 tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
1828 DEBUGVAR(1,74,"tns_data(): n_filt"));
1829 #if 0
1830 printf("%d\n", tns->n_filt[w]);
1831 #endif
1833 if (tns->n_filt[w])
1835 if ((tns->coef_res[w] = faad_get1bit(ld
1836 DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
1838 start_coef_bits = 4;
1839 } else {
1840 start_coef_bits = 3;
1842 #if 0
1843 printf("%d\n", tns->coef_res[w]);
1844 #endif
1847 for (filt = 0; filt < tns->n_filt[w]; filt++)
1849 tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits
1850 DEBUGVAR(1,76,"tns_data(): length"));
1851 #if 0
1852 printf("%d\n", tns->length[w][filt]);
1853 #endif
1854 tns->order[w][filt] = (uint8_t)faad_getbits(ld, order_bits
1855 DEBUGVAR(1,77,"tns_data(): order"));
1856 #if 0
1857 printf("%d\n", tns->order[w][filt]);
1858 #endif
1859 if (tns->order[w][filt])
1861 tns->direction[w][filt] = faad_get1bit(ld
1862 DEBUGVAR(1,78,"tns_data(): direction"));
1863 #if 0
1864 printf("%d\n", tns->direction[w][filt]);
1865 #endif
1866 tns->coef_compress[w][filt] = faad_get1bit(ld
1867 DEBUGVAR(1,79,"tns_data(): coef_compress"));
1868 #if 0
1869 printf("%d\n", tns->coef_compress[w][filt]);
1870 #endif
1872 coef_bits = start_coef_bits - tns->coef_compress[w][filt];
1873 for (i = 0; i < tns->order[w][filt]; i++)
1875 tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
1876 DEBUGVAR(1,80,"tns_data(): coef"));
1877 #if 0
1878 printf("%d\n", tns->coef[w][filt][i]);
1879 #endif
1886 #ifdef LTP_DEC
1887 /* Table 4.4.28 */
1888 static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
1890 uint8_t sfb, w;
1892 ltp->lag = 0;
1894 #ifdef LD_DEC
1895 if (hDecoder->object_type == LD)
1897 ltp->lag_update = (uint8_t)faad_getbits(ld, 1
1898 DEBUGVAR(1,142,"ltp_data(): lag_update"));
1900 if (ltp->lag_update)
1902 ltp->lag = (uint16_t)faad_getbits(ld, 10
1903 DEBUGVAR(1,81,"ltp_data(): lag"));
1905 } else {
1906 #endif
1907 ltp->lag = (uint16_t)faad_getbits(ld, 11
1908 DEBUGVAR(1,81,"ltp_data(): lag"));
1909 #ifdef LD_DEC
1911 #endif
1913 /* Check length of lag */
1914 if (ltp->lag > (hDecoder->frameLength << 1))
1915 return 18;
1917 ltp->coef = (uint8_t)faad_getbits(ld, 3
1918 DEBUGVAR(1,82,"ltp_data(): coef"));
1920 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1922 for (w = 0; w < ics->num_windows; w++)
1924 if ((ltp->short_used[w] = faad_get1bit(ld
1925 DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
1927 ltp->short_lag_present[w] = faad_get1bit(ld
1928 DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
1929 if (ltp->short_lag_present[w])
1931 ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
1932 DEBUGVAR(1,85,"ltp_data(): short_lag"));
1936 } else {
1937 ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1939 for (sfb = 0; sfb < ltp->last_band; sfb++)
1941 ltp->long_used[sfb] = faad_get1bit(ld
1942 DEBUGVAR(1,86,"ltp_data(): long_used"));
1946 return 0;
1948 #endif
1950 /* Table 4.4.29 */
1951 static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
1952 int16_t *spectral_data)
1954 int8_t i;
1955 uint8_t g;
1956 uint16_t inc, k, p = 0;
1957 uint8_t groups = 0;
1958 uint8_t sect_cb;
1959 uint8_t result;
1960 uint16_t nshort = hDecoder->frameLength/8;
1962 #ifdef PROFILE
1963 int64_t count = faad_get_ts();
1964 #endif
1966 for(g = 0; g < ics->num_window_groups; g++)
1968 p = groups*nshort;
1970 for (i = 0; i < ics->num_sec[g]; i++)
1972 sect_cb = ics->sect_cb[g][i];
1974 inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
1976 switch (sect_cb)
1978 case ZERO_HCB:
1979 case NOISE_HCB:
1980 case INTENSITY_HCB:
1981 case INTENSITY_HCB2:
1982 //#define SD_PRINT
1983 #ifdef SD_PRINT
1985 int j;
1986 for (j = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; j < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; j++)
1988 printf("%d\n", 0);
1991 #endif
1992 //#define SFBO_PRINT
1993 #ifdef SFBO_PRINT
1994 printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
1995 #endif
1996 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
1997 ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
1998 break;
1999 default:
2000 #ifdef SFBO_PRINT
2001 printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2002 #endif
2003 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
2004 k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
2006 if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
2007 return result;
2008 #ifdef SD_PRINT
2010 int j;
2011 for (j = p; j < p+inc; j++)
2013 printf("%d\n", spectral_data[j]);
2016 #endif
2017 p += inc;
2019 break;
2022 groups += ics->window_group_length[g];
2025 #ifdef PROFILE
2026 count = faad_get_ts() - count;
2027 hDecoder->spectral_cycles += count;
2028 #endif
2030 return 0;
2033 /* Table 4.4.30 */
2034 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
2036 uint16_t i, n, dataElementLength;
2037 uint8_t dataElementLengthPart;
2038 uint8_t align = 4, data_element_version, loopCounter;
2040 uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
2041 DEBUGVAR(1,87,"extension_payload(): extension_type"));
2043 switch (extension_type)
2045 case EXT_DYNAMIC_RANGE:
2046 drc->present = 1;
2047 n = dynamic_range_info(ld, drc);
2048 return n;
2049 case EXT_FILL_DATA:
2050 /* fill_nibble = */ faad_getbits(ld, 4
2051 DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be ‘0000’ */
2052 for (i = 0; i < count-1; i++)
2054 /* fill_byte[i] = */ faad_getbits(ld, 8
2055 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ‘10100101’ */
2057 return count;
2058 case EXT_DATA_ELEMENT:
2059 data_element_version = (uint8_t)faad_getbits(ld, 4
2060 DEBUGVAR(1,400,"extension_payload(): data_element_version"));
2061 switch (data_element_version)
2063 case ANC_DATA:
2064 loopCounter = 0;
2065 dataElementLength = 0;
2066 do {
2067 dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
2068 DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
2069 dataElementLength += dataElementLengthPart;
2070 loopCounter++;
2071 } while (dataElementLengthPart == 255);
2073 for (i = 0; i < dataElementLength; i++)
2075 /* data_element_byte[i] = */ faad_getbits(ld, 8
2076 DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
2077 return (dataElementLength+loopCounter+1);
2079 default:
2080 align = 0;
2082 case EXT_FIL:
2083 default:
2084 faad_getbits(ld, align
2085 DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
2086 for (i = 0; i < count-1; i++)
2088 /* other_bits[i] = */ faad_getbits(ld, 8
2089 DEBUGVAR(1,89,"extension_payload(): fill_bit"));
2091 return count;
2095 /* Table 4.4.31 */
2096 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc)
2098 uint8_t i, n = 1;
2099 uint8_t band_incr;
2101 drc->num_bands = 1;
2103 if (faad_get1bit(ld
2104 DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
2106 drc->pce_instance_tag = (uint8_t)faad_getbits(ld, 4
2107 DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag"));
2108 /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4
2109 DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits"));
2110 n++;
2113 drc->excluded_chns_present = faad_get1bit(ld
2114 DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present"));
2115 if (drc->excluded_chns_present == 1)
2117 n += excluded_channels(ld, drc);
2120 if (faad_get1bit(ld
2121 DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
2123 band_incr = (uint8_t)faad_getbits(ld, 4
2124 DEBUGVAR(1,95,"dynamic_range_info(): band_incr"));
2125 /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4
2126 DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits"));
2127 n++;
2128 drc->num_bands += band_incr;
2130 for (i = 0; i < drc->num_bands; i++);
2132 drc->band_top[i] = (uint8_t)faad_getbits(ld, 8
2133 DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
2134 n++;
2138 if (faad_get1bit(ld
2139 DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
2141 drc->prog_ref_level = (uint8_t)faad_getbits(ld, 7
2142 DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level"));
2143 /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
2144 DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits"));
2145 n++;
2148 for (i = 0; i < drc->num_bands; i++)
2150 drc->dyn_rng_sgn[i] = faad_get1bit(ld
2151 DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn"));
2152 drc->dyn_rng_ctl[i] = (uint8_t)faad_getbits(ld, 7
2153 DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl"));
2154 n++;
2157 return n;
2160 /* Table 4.4.32 */
2161 static uint8_t excluded_channels(bitfile *ld, drc_info *drc)
2163 uint8_t i, n = 0;
2164 uint8_t num_excl_chan = 7;
2166 for (i = 0; i < 7; i++)
2168 drc->exclude_mask[i] = faad_get1bit(ld
2169 DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
2171 n++;
2173 while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
2174 DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
2176 for (i = num_excl_chan; i < num_excl_chan+7; i++)
2178 drc->exclude_mask[i] = faad_get1bit(ld
2179 DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
2181 n++;
2182 num_excl_chan += 7;
2185 return n;
2188 /* Annex A: Audio Interchange Formats */
2190 /* Table 1.A.2 */
2191 void get_adif_header(adif_header *adif, bitfile *ld)
2193 uint8_t i;
2195 /* adif_id[0] = */ faad_getbits(ld, 8
2196 DEBUGVAR(1,106,"get_adif_header(): adif_id[0]"));
2197 /* adif_id[1] = */ faad_getbits(ld, 8
2198 DEBUGVAR(1,107,"get_adif_header(): adif_id[1]"));
2199 /* adif_id[2] = */ faad_getbits(ld, 8
2200 DEBUGVAR(1,108,"get_adif_header(): adif_id[2]"));
2201 /* adif_id[3] = */ faad_getbits(ld, 8
2202 DEBUGVAR(1,109,"get_adif_header(): adif_id[3]"));
2203 adif->copyright_id_present = faad_get1bit(ld
2204 DEBUGVAR(1,110,"get_adif_header(): copyright_id_present"));
2205 if(adif->copyright_id_present)
2207 for (i = 0; i < 72/8; i++)
2209 adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8
2210 DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
2212 adif->copyright_id[i] = 0;
2214 adif->original_copy = faad_get1bit(ld
2215 DEBUGVAR(1,112,"get_adif_header(): original_copy"));
2216 adif->home = faad_get1bit(ld
2217 DEBUGVAR(1,113,"get_adif_header(): home"));
2218 adif->bitstream_type = faad_get1bit(ld
2219 DEBUGVAR(1,114,"get_adif_header(): bitstream_type"));
2220 adif->bitrate = faad_getbits(ld, 23
2221 DEBUGVAR(1,115,"get_adif_header(): bitrate"));
2222 adif->num_program_config_elements = (uint8_t)faad_getbits(ld, 4
2223 DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements"));
2225 for (i = 0; i < adif->num_program_config_elements + 1; i++)
2227 if(adif->bitstream_type == 0)
2229 adif->adif_buffer_fullness = faad_getbits(ld, 20
2230 DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
2231 } else {
2232 adif->adif_buffer_fullness = 0;
2235 program_config_element(&adif->pce[i], ld);
2239 /* Table 1.A.5 */
2240 uint8_t adts_frame(adts_header *adts, bitfile *ld)
2242 /* faad_byte_align(ld); */
2243 if (adts_fixed_header(adts, ld))
2244 return 5;
2245 adts_variable_header(adts, ld);
2246 adts_error_check(adts, ld);
2248 return 0;
2251 /* Table 1.A.6 */
2252 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld)
2254 uint16_t i;
2255 uint8_t sync_err = 1;
2257 /* try to recover from sync errors */
2258 for (i = 0; i < 768; i++)
2260 adts->syncword = (uint16_t)faad_showbits(ld, 12);
2261 if (adts->syncword != 0xFFF)
2263 faad_getbits(ld, 8
2264 DEBUGVAR(0,0,""));
2265 } else {
2266 sync_err = 0;
2267 faad_getbits(ld, 12
2268 DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
2269 break;
2272 if (sync_err)
2273 return 5;
2275 adts->id = faad_get1bit(ld
2276 DEBUGVAR(1,119,"adts_fixed_header(): id"));
2277 adts->layer = (uint8_t)faad_getbits(ld, 2
2278 DEBUGVAR(1,120,"adts_fixed_header(): layer"));
2279 adts->protection_absent = faad_get1bit(ld
2280 DEBUGVAR(1,121,"adts_fixed_header(): protection_absent"));
2281 adts->profile = (uint8_t)faad_getbits(ld, 2
2282 DEBUGVAR(1,122,"adts_fixed_header(): profile"));
2283 adts->sf_index = (uint8_t)faad_getbits(ld, 4
2284 DEBUGVAR(1,123,"adts_fixed_header(): sf_index"));
2285 adts->private_bit = faad_get1bit(ld
2286 DEBUGVAR(1,124,"adts_fixed_header(): private_bit"));
2287 adts->channel_configuration = (uint8_t)faad_getbits(ld, 3
2288 DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration"));
2289 adts->original = faad_get1bit(ld
2290 DEBUGVAR(1,126,"adts_fixed_header(): original"));
2291 adts->home = faad_get1bit(ld
2292 DEBUGVAR(1,127,"adts_fixed_header(): home"));
2294 if (adts->old_format == 1)
2296 /* Removed in corrigendum 14496-3:2002 */
2297 if (adts->id == 0)
2299 adts->emphasis = (uint8_t)faad_getbits(ld, 2
2300 DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
2304 return 0;
2307 /* Table 1.A.7 */
2308 static void adts_variable_header(adts_header *adts, bitfile *ld)
2310 adts->copyright_identification_bit = faad_get1bit(ld
2311 DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit"));
2312 adts->copyright_identification_start = faad_get1bit(ld
2313 DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start"));
2314 adts->aac_frame_length = (uint16_t)faad_getbits(ld, 13
2315 DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length"));
2316 adts->adts_buffer_fullness = (uint16_t)faad_getbits(ld, 11
2317 DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness"));
2318 adts->no_raw_data_blocks_in_frame = (uint8_t)faad_getbits(ld, 2
2319 DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame"));
2322 /* Table 1.A.8 */
2323 static void adts_error_check(adts_header *adts, bitfile *ld)
2325 if (adts->protection_absent == 0)
2327 adts->crc_check = (uint16_t)faad_getbits(ld, 16
2328 DEBUGVAR(1,134,"adts_error_check(): crc_check"));