FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / libfaad / syntax.c
blobfa077d4e943da16e425bb16243173dd8c1fbf7af
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$
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 ALIGN int16_t spec_data[1024] = {0};
562 element sce;
563 static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
564 uint8_t channel, uint8_t *tag)
566 uint8_t retval = 0;
567 ic_stream *ics = &(sce.ics1);
569 memset(spec_data,0,sizeof(spec_data));
570 memset(&sce,0,sizeof(sce));
571 sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
572 DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
574 *tag = sce.element_instance_tag;
575 sce.channel = channel;
576 sce.paired_channel = -1;
578 retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
579 if (retval > 0)
580 return retval;
582 #ifdef SBR_DEC
583 /* check if next bitstream element is a fill element */
584 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
585 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
587 faad_flushbits(ld, LEN_SE_ID);
589 /* one sbr_info describes a channel_element not a channel! */
590 if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
592 return retval;
595 #endif
597 /* noiseless coding is done, spectral reconstruction is done now */
598 retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
599 if (retval > 0)
600 return retval;
602 return 0;
605 /* Table 4.4.5 */
606 ALIGN int16_t spec_data1[1024] IBSS_ATTR;
607 ALIGN int16_t spec_data2[1024] IBSS_ATTR;
608 element cpe;
609 static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
610 uint8_t channels, uint8_t *tag)
612 ic_stream *ics1 = &(cpe.ics1);
613 ic_stream *ics2 = &(cpe.ics2);
614 uint8_t result;
616 memset(spec_data1,0,sizeof(spec_data1));
617 memset(spec_data2,0,sizeof(spec_data2));
618 memset(&cpe,0,sizeof(cpe));
619 cpe.channel = channels;
620 cpe.paired_channel = channels+1;
622 cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
623 DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
624 *tag = cpe.element_instance_tag;
626 if ((cpe.common_window = faad_get1bit(ld
627 DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
629 /* both channels have common ics information */
630 if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
631 return result;
633 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
634 DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
635 if (ics1->ms_mask_present == 1)
637 uint8_t g, sfb;
638 for (g = 0; g < ics1->num_window_groups; g++)
640 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
642 ics1->ms_used[g][sfb] = faad_get1bit(ld
643 DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
648 #ifdef ERROR_RESILIENCE
649 if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
651 if ((
652 #ifdef LTP_DEC
653 ics1->ltp.data_present =
654 #endif
655 faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
657 #ifdef LTP_DEC
658 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
660 return result;
662 #else
663 return 26;
664 #endif
667 #endif
669 memcpy(ics2, ics1, sizeof(ic_stream));
670 } else {
671 ics1->ms_mask_present = 0;
674 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
675 0, spec_data1)) > 0)
677 return result;
680 #ifdef ERROR_RESILIENCE
681 if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
682 (ics1->predictor_data_present))
684 if ((
685 #ifdef LTP_DEC
686 ics1->ltp2.data_present =
687 #endif
688 faad_get1bit(ld DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
690 #ifdef LTP_DEC
691 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
693 return result;
695 #else
696 return 26;
697 #endif
700 #endif
702 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
703 0, spec_data2)) > 0)
705 return result;
708 #ifdef SBR_DEC
709 /* check if next bitstream element is a fill element */
710 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
711 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
713 faad_flushbits(ld, LEN_SE_ID);
715 /* one sbr_info describes a channel_element not a channel! */
716 if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
718 return result;
721 #endif
723 /* noiseless coding is done, spectral reconstruction is done now */
724 if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
725 spec_data1, spec_data2)) > 0)
727 return result;
730 return 0;
733 /* Table 4.4.6 */
734 static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
735 uint8_t common_window)
737 uint8_t retval = 0;
739 /* ics->ics_reserved_bit = */ faad_get1bit(ld
740 DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
741 ics->window_sequence = (uint8_t)faad_getbits(ld, 2
742 DEBUGVAR(1,44,"ics_info(): window_sequence"));
743 ics->window_shape = faad_get1bit(ld
744 DEBUGVAR(1,45,"ics_info(): window_shape"));
746 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
748 ics->max_sfb = (uint8_t)faad_getbits(ld, 4
749 DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
750 ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
751 DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
752 } else {
753 ics->max_sfb = (uint8_t)faad_getbits(ld, 6
754 DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
757 /* get the grouping information */
758 if ((retval = window_grouping_info(hDecoder, ics)) > 0)
759 return retval;
761 /* should be an error */
762 /* check the range of max_sfb */
763 if (ics->max_sfb > ics->num_swb)
764 return 16;
766 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
768 if ((ics->predictor_data_present = faad_get1bit(ld
769 DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
771 if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
773 uint8_t sfb;
775 uint8_t limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
776 #ifdef MAIN_DEC
777 ics->pred.limit = limit;
778 #endif
780 if ((
781 #ifdef MAIN_DEC
782 ics->pred.predictor_reset =
783 #endif
784 faad_get1bit(ld DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
786 #ifdef MAIN_DEC
787 ics->pred.predictor_reset_group_number =
788 (uint8_t)
789 #endif
790 faad_getbits(ld, 5 DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
793 for (sfb = 0; sfb < limit; sfb++)
795 #ifdef MAIN_DEC
796 ics->pred.prediction_used[sfb] =
797 #endif
798 faad_get1bit(ld DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
801 #ifdef LTP_DEC
802 else { /* Long Term Prediction */
803 if (hDecoder->object_type < ER_OBJECT_START)
805 if ((ics->ltp.data_present = faad_get1bit(ld
806 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
808 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
810 return retval;
813 if (common_window)
815 if ((ics->ltp2.data_present = faad_get1bit(ld
816 DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
818 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
820 return retval;
825 #ifdef ERROR_RESILIENCE
826 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
828 if ((ics->ltp.data_present = faad_get1bit(ld
829 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
831 ltp_data(hDecoder, ics, &(ics->ltp), ld);
834 #endif
836 #else
837 (void) common_window;
838 #endif
842 return retval;
845 /* Table 4.4.7 */
846 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
848 uint8_t i;
850 pul->number_pulse = (uint8_t)faad_getbits(ld, 2
851 DEBUGVAR(1,56,"pulse_data(): number_pulse"));
852 pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
853 DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
855 /* check the range of pulse_start_sfb */
856 if (pul->pulse_start_sfb > ics->num_swb)
857 return 16;
859 for (i = 0; i < pul->number_pulse+1; i++)
861 pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
862 DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
863 #if 0
864 printf("%d\n", pul->pulse_offset[i]);
865 #endif
866 pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
867 DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
868 #if 0
869 printf("%d\n", pul->pulse_amp[i]);
870 #endif
873 return 0;
876 #ifdef COUPLING_DEC
877 /* Table 4.4.8: Currently just for skipping the bits... */
878 static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld)
880 uint8_t c, result = 0;
881 uint8_t ind_sw_cce_flag = 0;
882 uint8_t num_gain_element_lists = 0;
883 uint8_t num_coupled_elements = 0;
885 element el_empty = {0};
886 ic_stream ics_empty = {0};
887 static int16_t sh_data[1024];
889 c = faad_getbits(ld, LEN_TAG
890 DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
892 ind_sw_cce_flag = faad_get1bit(ld
893 DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
894 num_coupled_elements = faad_getbits(ld, 3
895 DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
897 for (c = 0; c < num_coupled_elements + 1; c++)
899 uint8_t cc_target_is_cpe, cc_target_tag_select;
901 num_gain_element_lists++;
903 cc_target_is_cpe = faad_get1bit(ld
904 DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
905 cc_target_tag_select = faad_getbits(ld, 4
906 DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
908 if (cc_target_is_cpe)
910 uint8_t cc_l = faad_get1bit(ld
911 DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
912 uint8_t cc_r = faad_get1bit(ld
913 DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
915 if (cc_l && cc_r)
916 num_gain_element_lists++;
920 faad_get1bit(ld
921 DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
922 faad_get1bit(ld
923 DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
924 faad_getbits(ld, 2
925 DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
927 if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
928 0, sh_data)) > 0)
930 return result;
933 for (c = 1; c < num_gain_element_lists; c++)
935 uint8_t cge;
937 if (ind_sw_cce_flag)
939 cge = 1;
940 } else {
941 cge = faad_get1bit(ld
942 DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
945 if (cge)
947 huffman_scale_factor(ld);
948 } else {
949 uint8_t g, sfb;
951 for (g = 0; g < ics_empty.num_window_groups; g++)
953 for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
955 if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
956 huffman_scale_factor(ld);
962 return 0;
964 #endif
966 /* Table 4.4.10 */
967 static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld)
969 uint8_t byte_aligned;
970 uint16_t i, count;
972 (void)hDecoder;
974 /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
975 DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
976 byte_aligned = faad_get1bit(ld
977 DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
978 count = (uint16_t)faad_getbits(ld, 8
979 DEBUGVAR(1,62,"data_stream_element(): count"));
980 if (count == 255)
982 count += (uint16_t)faad_getbits(ld, 8
983 DEBUGVAR(1,63,"data_stream_element(): extra count"));
985 if (byte_aligned)
986 faad_byte_align(ld);
988 for (i = 0; i < count; i++)
990 faad_getbits(ld, LEN_BYTE
991 DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
994 return count;
997 /* Table 4.4.11 */
998 static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
999 #ifdef SBR_DEC
1000 ,uint8_t sbr_ele
1001 #endif
1004 uint16_t count;
1005 #ifdef SBR_DEC
1006 uint8_t bs_extension_type;
1007 #endif
1009 count = (uint16_t)faad_getbits(ld, 4
1010 DEBUGVAR(1,65,"fill_element(): count"));
1011 if (count == 15)
1013 count += (uint16_t)faad_getbits(ld, 8
1014 DEBUGVAR(1,66,"fill_element(): extra count")) - 1;
1017 if (count > 0)
1019 #ifdef SBR_DEC
1020 bs_extension_type = (uint8_t)faad_showbits(ld, 4);
1022 if ((bs_extension_type == EXT_SBR_DATA) ||
1023 (bs_extension_type == EXT_SBR_DATA_CRC))
1025 if (sbr_ele == INVALID_SBR_ELEMENT)
1026 return 24;
1028 if (!hDecoder->sbr[sbr_ele])
1030 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
1031 hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index),
1032 hDecoder->downSampledSBR
1033 #ifdef DRM
1035 #endif
1039 hDecoder->sbr_present_flag = 1;
1041 /* parse the SBR data */
1042 hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
1044 #if 0
1045 if (hDecoder->sbr[sbr_ele]->ret > 0)
1047 printf("%s\n", NeAACDecGetErrorMessage(hDecoder->sbr[sbr_ele]->ret));
1049 #endif
1051 #if (defined(PS_DEC) || defined(DRM_PS))
1052 if (hDecoder->sbr[sbr_ele]->ps_used)
1054 hDecoder->ps_used[sbr_ele] = 1;
1056 /* set element independent flag to 1 as well */
1057 hDecoder->ps_used_global = 1;
1059 #endif
1060 } else {
1061 #else
1062 (void)hDecoder;
1063 #endif
1064 while (count > 0)
1066 count -= extension_payload(ld, drc, count);
1068 #ifdef SBR_DEC
1070 #endif
1073 return 0;
1076 /* Table 4.4.12 */
1077 #ifdef SSR_DEC
1078 static void gain_control_data(bitfile *ld, ic_stream *ics)
1080 uint8_t bd, wd, ad;
1081 ssr_info *ssr = &(ics->ssr);
1083 ssr->max_band = (uint8_t)faad_getbits(ld, 2
1084 DEBUGVAR(1,1000,"gain_control_data(): max_band"));
1086 if (ics->window_sequence == ONLY_LONG_SEQUENCE)
1088 for (bd = 1; bd <= ssr->max_band; bd++)
1090 for (wd = 0; wd < 1; wd++)
1092 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1093 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1095 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1097 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1098 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1099 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1100 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1104 } else if (ics->window_sequence == LONG_START_SEQUENCE) {
1105 for (bd = 1; bd <= ssr->max_band; bd++)
1107 for (wd = 0; wd < 2; wd++)
1109 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1110 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1112 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1114 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1115 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1116 if (wd == 0)
1118 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1119 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1120 } else {
1121 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1122 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1127 } else if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
1128 for (bd = 1; bd <= ssr->max_band; bd++)
1130 for (wd = 0; wd < 8; wd++)
1132 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1133 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1135 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1137 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1138 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1139 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 2
1140 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1144 } else if (ics->window_sequence == LONG_STOP_SEQUENCE) {
1145 for (bd = 1; bd <= ssr->max_band; bd++)
1147 for (wd = 0; wd < 2; wd++)
1149 ssr->adjust_num[bd][wd] = (uint8_t)faad_getbits(ld, 3
1150 DEBUGVAR(1,1001,"gain_control_data(): adjust_num"));
1152 for (ad = 0; ad < ssr->adjust_num[bd][wd]; ad++)
1154 ssr->alevcode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1155 DEBUGVAR(1,1002,"gain_control_data(): alevcode"));
1157 if (wd == 0)
1159 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 4
1160 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1161 } else {
1162 ssr->aloccode[bd][wd][ad] = (uint8_t)faad_getbits(ld, 5
1163 DEBUGVAR(1,1003,"gain_control_data(): aloccode"));
1170 #endif
1172 #ifdef SCALABLE_DEC
1173 ALIGN int16_t spec_data1[1024];
1174 ALIGN int16_t spec_data2[1024];
1175 /* Table 4.4.13 ASME */
1176 void aac_scalable_main_element(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo,
1177 bitfile *ld, program_config *pce, drc_info *drc)
1179 uint8_t retval = 0;
1180 uint8_t channels = hDecoder->fr_channels = 0;
1181 uint8_t ch;
1182 uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
1183 element cpe = {0};
1184 ic_stream *ics1 = &(cpe.ics1);
1185 ic_stream *ics2 = &(cpe.ics2);
1186 int16_t *spec_data;
1188 memset(spec_data1,0,sizeof(spec_data1));
1189 memset(spec_data2,0,sizeof(spec_data2));
1190 hDecoder->fr_ch_ele = 0;
1192 hInfo->error = aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
1193 if (hInfo->error > 0)
1194 return;
1196 cpe.common_window = 1;
1197 if (this_layer_stereo)
1199 hDecoder->element_id[0] = ID_CPE;
1200 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
1201 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
1202 } else {
1203 hDecoder->element_id[0] = ID_SCE;
1206 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1208 ic_stream *ics;
1209 if (ch == 0)
1211 ics = ics1;
1212 spec_data = spec_data1;
1213 } else {
1214 ics = ics2;
1215 spec_data = spec_data2;
1218 hInfo->error = individual_channel_stream(hDecoder, &cpe, ld, ics, 1, spec_data);
1219 if (hInfo->error > 0)
1220 return;
1223 #ifdef DRM
1224 #ifdef SBR_DEC
1225 /* In case of DRM we need to read the SBR info before channel reconstruction */
1226 if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
1228 bitfile ld_sbr = {0};
1229 uint32_t i;
1230 uint16_t count = 0;
1231 uint8_t *revbuffer;
1232 uint8_t *prevbufstart;
1233 uint8_t *pbufend;
1235 /* all forward bitreading should be finished at this point */
1236 uint32_t bitsconsumed = faad_get_processed_bits(ld);
1237 uint32_t buffer_size = faad_origbitbuffer_size(ld);
1238 uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
1240 if (bitsconsumed + 8 > buffer_size*8)
1242 hInfo->error = 14;
1243 return;
1246 if (!hDecoder->sbr[0])
1248 hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, hDecoder->element_id[0],
1249 2*get_sample_rate(hDecoder->sf_index), 0 /* ds SBR */, 1);
1252 /* Reverse bit reading of SBR data in DRM audio frame */
1253 revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
1254 prevbufstart = revbuffer;
1255 pbufend = &buffer[buffer_size - 1];
1256 for (i = 0; i < buffer_size; i++)
1257 *prevbufstart++ = tabFlipbits[*pbufend--];
1259 /* Set SBR data */
1260 /* consider 8 bits from AAC-CRC */
1261 count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
1262 faad_initbits(&ld_sbr, revbuffer, count);
1264 hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
1265 hDecoder->sbr[0]->sample_rate *= 2;
1267 faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
1269 hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
1270 #if (defined(PS_DEC) || defined(DRM_PS))
1271 if (hDecoder->sbr[0]->ps_used)
1273 hDecoder->ps_used[0] = 1;
1274 hDecoder->ps_used_global = 1;
1276 #endif
1278 /* check CRC */
1279 /* no need to check it if there was already an error */
1280 if (hDecoder->sbr[0]->ret == 0)
1281 hDecoder->sbr[0]->ret = (uint8_t)faad_check_CRC(&ld_sbr, (uint16_t)faad_get_processed_bits(&ld_sbr) - 8);
1283 /* SBR data was corrupted, disable it until the next header */
1284 if (hDecoder->sbr[0]->ret != 0)
1286 hDecoder->sbr[0]->header_count = 0;
1289 faad_endbits(&ld_sbr);
1291 if (revbuffer)
1292 faad_free(revbuffer);
1294 #endif
1295 #endif
1297 if (this_layer_stereo)
1299 hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
1300 if (hInfo->error > 0)
1301 return;
1302 } else {
1303 hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
1304 if (hInfo->error > 0)
1305 return;
1308 /* map output channels position to internal data channels */
1309 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
1311 /* this might be faulty when pce_set is true */
1312 hDecoder->internal_channel[channels] = channels;
1313 hDecoder->internal_channel[channels+1] = channels+1;
1314 } else {
1315 hDecoder->internal_channel[channels] = channels;
1318 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
1319 hDecoder->fr_ch_ele++;
1321 return;
1324 /* Table 4.4.15 */
1325 static int8_t aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
1326 bitfile *ld, uint8_t this_layer_stereo)
1328 uint8_t retval = 0;
1329 uint8_t ch;
1330 ic_stream *ics;
1332 /* ics1->ics_reserved_bit = */ faad_get1bit(ld
1333 DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
1334 ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
1335 DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
1336 ics1->window_shape = faad_get1bit(ld
1337 DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
1339 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
1341 ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
1342 DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
1343 ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
1344 DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
1345 } else {
1346 ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
1347 DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
1350 /* get the grouping information */
1351 if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
1352 return retval;
1354 /* should be an error */
1355 /* check the range of max_sfb */
1356 if (ics1->max_sfb > ics1->num_swb)
1357 return 16;
1359 if (this_layer_stereo)
1361 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
1362 DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
1363 if (ics1->ms_mask_present == 1)
1365 uint8_t g, sfb;
1366 for (g = 0; g < ics1->num_window_groups; g++)
1368 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
1370 ics1->ms_used[g][sfb] = faad_get1bit(ld
1371 DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
1376 memcpy(ics2, ics1, sizeof(ic_stream));
1377 } else {
1378 ics1->ms_mask_present = 0;
1381 if (0)
1383 faad_get1bit(ld
1384 DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer"));
1387 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1389 if (ch == 0)
1390 ics = ics1;
1391 else
1392 ics = ics2;
1394 if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/)
1396 if ((ics->tns_data_present = faad_get1bit(ld
1397 DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1)
1399 #ifdef DRM
1400 /* different order of data units in DRM */
1401 if (hDecoder->object_type != DRM_ER_LC)
1402 #endif
1404 tns_data(ics, &(ics->tns), ld);
1408 #if 0
1409 if (0 /*core_flag || tvq_layer_pesent*/)
1411 if ((ch==0) || ((ch==1) && (core_stereo || tvq_stereo))
1412 diff_control_data();
1413 if (mono_stereo_flag)
1414 diff_control_data_lr();
1415 } else {
1416 #endif
1417 if ((
1418 #ifdef LTP_DEC
1419 ics->ltp.data_present =
1420 #endif
1421 faad_get1bit(ld DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1)
1423 #ifdef LTP_DEC
1424 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
1426 return retval;
1428 #else
1429 return 26;
1430 #endif
1432 #if 0
1434 #endif
1437 return 0;
1439 #endif
1441 /* Table 4.4.24 */
1442 static uint8_t individual_channel_stream(NeAACDecHandle hDecoder, element *ele,
1443 bitfile *ld, ic_stream *ics, uint8_t scal_flag,
1444 int16_t *spec_data)
1446 uint8_t result;
1448 ics->global_gain = (uint8_t)faad_getbits(ld, 8
1449 DEBUGVAR(1,67,"individual_channel_stream(): global_gain"));
1451 if (!ele->common_window && !scal_flag)
1453 if ((result = ics_info(hDecoder, ics, ld, ele->common_window)) > 0)
1454 return result;
1457 if ((result = section_data(hDecoder, ics, ld)) > 0)
1458 return result;
1460 if ((result = scale_factor_data(hDecoder, ics, ld)) > 0)
1461 return result;
1463 if (!scal_flag)
1466 ** NOTE: It could be that pulse data is available in scalable AAC too,
1467 ** as said in Amendment 1, this could be only the case for ER AAC,
1468 ** though. (have to check this out later)
1470 /* get pulse data */
1471 if ((ics->pulse_data_present = faad_get1bit(ld
1472 DEBUGVAR(1,68,"individual_channel_stream(): pulse_data_present"))) & 1)
1474 if ((result = pulse_data(ics, &(ics->pul), ld)) > 0)
1475 return result;
1478 /* get tns data */
1479 if ((ics->tns_data_present = faad_get1bit(ld
1480 DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
1482 #ifdef ERROR_RESILIENCE
1483 if (hDecoder->object_type < ER_OBJECT_START)
1484 #endif
1485 tns_data(ics, &(ics->tns), ld);
1488 /* get gain control data */
1489 if ((ics->gain_control_data_present = faad_get1bit(ld
1490 DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
1492 #ifdef SSR_DEC
1493 if (hDecoder->object_type != SSR)
1494 return 1;
1495 else
1496 gain_control_data(ld, ics);
1497 #else
1498 return 1;
1499 #endif
1503 #ifdef ERROR_RESILIENCE
1504 if (hDecoder->aacSpectralDataResilienceFlag)
1506 ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
1507 DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
1509 if (hDecoder->channelConfiguration == 2)
1511 if (ics->length_of_reordered_spectral_data > 6144)
1512 ics->length_of_reordered_spectral_data = 6144;
1513 } else {
1514 if (ics->length_of_reordered_spectral_data > 12288)
1515 ics->length_of_reordered_spectral_data = 12288;
1518 ics->length_of_longest_codeword = (uint8_t)faad_getbits(ld, 6
1519 DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
1520 if (ics->length_of_longest_codeword >= 49)
1521 ics->length_of_longest_codeword = 49;
1524 /* RVLC spectral data is put here */
1525 if (hDecoder->aacScalefactorDataResilienceFlag)
1527 if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
1528 return result;
1531 if (hDecoder->object_type >= ER_OBJECT_START)
1533 if (ics->tns_data_present)
1534 tns_data(ics, &(ics->tns), ld);
1537 #ifdef DRM
1538 /* CRC check */
1539 if (hDecoder->object_type == DRM_ER_LC)
1540 if ((result = (uint8_t)faad_check_CRC(ld, (uint16_t)faad_get_processed_bits(ld) - 8)) > 0)
1541 return result;
1542 #endif
1544 if (hDecoder->aacSpectralDataResilienceFlag)
1546 /* error resilient spectral data decoding */
1547 if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1549 return result;
1551 } else {
1552 #endif
1553 /* decode the spectral data */
1554 if ((result = spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1556 return result;
1558 #ifdef ERROR_RESILIENCE
1560 #endif
1562 /* pulse coding reconstruction */
1563 if (ics->pulse_data_present)
1565 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
1567 if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
1568 return result;
1569 } else {
1570 return 2; /* pulse coding not allowed for short blocks */
1574 return 0;
1577 /* Table 4.4.25 */
1578 static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1580 uint8_t g;
1581 uint8_t sect_esc_val, sect_bits;
1583 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1584 sect_bits = 3;
1585 else
1586 sect_bits = 5;
1587 sect_esc_val = (1<<sect_bits) - 1;
1589 #if 0
1590 printf("\ntotal sfb %d\n", ics->max_sfb);
1591 printf(" sect top cb\n");
1592 #endif
1594 for (g = 0; g < ics->num_window_groups; g++)
1596 uint8_t k = 0;
1597 uint8_t i = 0;
1599 while (k < ics->max_sfb)
1601 #ifdef ERROR_RESILIENCE
1602 uint8_t vcb11 = 0;
1603 #endif
1604 uint8_t sfb;
1605 uint8_t sect_len_incr;
1606 uint16_t sect_len = 0;
1607 uint8_t sect_cb_bits = 4;
1609 /* if "faad_getbits" detects error and returns "0", "k" is never
1610 incremented and we cannot leave the while loop */
1611 if ((ld->error != 0) || (ld->no_more_reading))
1612 return 14;
1614 #ifdef ERROR_RESILIENCE
1615 if (hDecoder->aacSectionDataResilienceFlag)
1616 sect_cb_bits = 5;
1617 #else
1618 (void) hDecoder;
1619 #endif
1621 ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
1622 DEBUGVAR(1,71,"section_data(): sect_cb"));
1624 #if 0
1625 printf("%d\n", ics->sect_cb[g][i]);
1626 #endif
1628 if (ics->sect_cb[g][i] == NOISE_HCB)
1629 ics->noise_used = 1;
1631 #ifdef ERROR_RESILIENCE
1632 if (hDecoder->aacSectionDataResilienceFlag)
1634 if ((ics->sect_cb[g][i] == 11) ||
1635 ((ics->sect_cb[g][i] >= 16) && (ics->sect_cb[g][i] <= 32)))
1637 vcb11 = 1;
1640 if (vcb11)
1642 sect_len_incr = 1;
1643 } else {
1644 #endif
1645 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1646 DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1647 #ifdef ERROR_RESILIENCE
1649 #endif
1650 while ((sect_len_incr == sect_esc_val) /* &&
1651 (k+sect_len < ics->max_sfb)*/)
1653 sect_len += sect_len_incr;
1654 sect_len_incr = (uint8_t)faad_getbits(ld, sect_bits
1655 DEBUGVAR(1,72,"section_data(): sect_len_incr"));
1658 sect_len += sect_len_incr;
1660 ics->sect_start[g][i] = k;
1661 ics->sect_end[g][i] = k + sect_len;
1663 #if 0
1664 printf("%d\n", ics->sect_start[g][i]);
1665 #endif
1666 #if 0
1667 printf("%d\n", ics->sect_end[g][i]);
1668 #endif
1670 if (k + sect_len >= 8*15)
1671 return 15;
1672 if (i >= 8*15)
1673 return 15;
1675 for (sfb = k; sfb < k + sect_len; sfb++)
1677 ics->sfb_cb[g][sfb] = ics->sect_cb[g][i];
1678 #if 0
1679 printf("%d\n", ics->sfb_cb[g][sfb]);
1680 #endif
1683 #if 0
1684 printf(" %6d %6d %6d\n",
1686 ics->sect_end[g][i],
1687 ics->sect_cb[g][i]);
1688 #endif
1690 k += sect_len;
1691 i++;
1693 ics->num_sec[g] = i;
1694 #if 0
1695 printf("%d\n", ics->num_sec[g]);
1696 #endif
1699 #if 0
1700 printf("\n");
1701 #endif
1703 return 0;
1707 * decode_scale_factors()
1708 * decodes the scalefactors from the bitstream
1711 * All scalefactors (and also the stereo positions and pns energies) are
1712 * transmitted using Huffman coded DPCM relative to the previous active
1713 * scalefactor (respectively previous stereo position or previous pns energy,
1714 * see subclause 4.6.2 and 4.6.3). The first active scalefactor is
1715 * differentially coded relative to the global gain.
1717 static uint8_t decode_scale_factors(ic_stream *ics, bitfile *ld)
1719 uint8_t g, sfb;
1720 int16_t t;
1721 int8_t noise_pcm_flag = 1;
1723 int16_t scale_factor = ics->global_gain;
1724 int16_t is_position = 0;
1725 int16_t noise_energy = ics->global_gain - 90;
1727 for (g = 0; g < ics->num_window_groups; g++)
1729 for (sfb = 0; sfb < ics->max_sfb; sfb++)
1731 switch (ics->sfb_cb[g][sfb])
1733 case ZERO_HCB: /* zero book */
1734 ics->scale_factors[g][sfb] = 0;
1735 //#define SF_PRINT
1736 #ifdef SF_PRINT
1737 printf("%d\n", ics->scale_factors[g][sfb]);
1738 #endif
1739 break;
1740 case INTENSITY_HCB: /* intensity books */
1741 case INTENSITY_HCB2:
1743 /* decode intensity position */
1744 t = huffman_scale_factor(ld);
1745 is_position += (t - 60);
1746 ics->scale_factors[g][sfb] = is_position;
1747 #ifdef SF_PRINT
1748 printf("%d\n", ics->scale_factors[g][sfb]);
1749 #endif
1751 break;
1752 case NOISE_HCB: /* noise books */
1754 /* decode noise energy */
1755 if (noise_pcm_flag)
1757 noise_pcm_flag = 0;
1758 t = (int16_t)faad_getbits(ld, 9
1759 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
1760 } else {
1761 t = huffman_scale_factor(ld);
1762 t -= 60;
1764 noise_energy += t;
1765 ics->scale_factors[g][sfb] = noise_energy;
1766 #ifdef SF_PRINT
1767 printf("%d\n", ics->scale_factors[g][sfb]);
1768 #endif
1770 break;
1771 default: /* spectral books */
1773 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
1775 ics->scale_factors[g][sfb] = 0;
1777 /* decode scale factor */
1778 t = huffman_scale_factor(ld);
1779 scale_factor += (t - 60);
1780 if (scale_factor < 0 || scale_factor > 255)
1781 return 4;
1782 ics->scale_factors[g][sfb] = scale_factor;
1783 #ifdef SF_PRINT
1784 printf("%d\n", ics->scale_factors[g][sfb]);
1785 #endif
1787 break;
1792 return 0;
1795 /* Table 4.4.26 */
1796 static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1798 uint8_t ret = 0;
1799 #ifdef PROFILE
1800 int64_t count = faad_get_ts();
1801 #endif
1803 #ifdef ERROR_RESILIENCE
1804 if (!hDecoder->aacScalefactorDataResilienceFlag)
1806 #else
1807 (void) hDecoder;
1808 #endif
1809 ret = decode_scale_factors(ics, ld);
1810 #ifdef ERROR_RESILIENCE
1811 } else {
1812 /* In ER AAC the parameters for RVLC are seperated from the actual
1813 data that holds the scale_factors.
1814 Strangely enough, 2 parameters for HCR are put inbetween them.
1816 ret = rvlc_scale_factor_data(ics, ld);
1818 #endif
1820 #ifdef PROFILE
1821 count = faad_get_ts() - count;
1822 hDecoder->scalefac_cycles += count;
1823 #endif
1825 return ret;
1828 /* Table 4.4.27 */
1829 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
1831 uint8_t w, filt, i, start_coef_bits = 0;
1832 int8_t coef_bits;
1833 uint8_t n_filt_bits = 2;
1834 uint8_t length_bits = 6;
1835 uint8_t order_bits = 5;
1837 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1839 n_filt_bits = 1;
1840 length_bits = 4;
1841 order_bits = 3;
1844 for (w = 0; w < ics->num_windows; w++)
1846 tns->n_filt[w] = (uint8_t)faad_getbits(ld, n_filt_bits
1847 DEBUGVAR(1,74,"tns_data(): n_filt"));
1848 #if 0
1849 printf("%d\n", tns->n_filt[w]);
1850 #endif
1852 if (tns->n_filt[w])
1854 if ((tns->coef_res[w] = faad_get1bit(ld
1855 DEBUGVAR(1,75,"tns_data(): coef_res"))) & 1)
1857 start_coef_bits = 4;
1858 } else {
1859 start_coef_bits = 3;
1861 #if 0
1862 printf("%d\n", tns->coef_res[w]);
1863 #endif
1866 for (filt = 0; filt < tns->n_filt[w]; filt++)
1868 tns->length[w][filt] = (uint8_t)faad_getbits(ld, length_bits
1869 DEBUGVAR(1,76,"tns_data(): length"));
1870 #if 0
1871 printf("%d\n", tns->length[w][filt]);
1872 #endif
1873 tns->order[w][filt] = (uint8_t)faad_getbits(ld, order_bits
1874 DEBUGVAR(1,77,"tns_data(): order"));
1875 #if 0
1876 printf("%d\n", tns->order[w][filt]);
1877 #endif
1878 if (tns->order[w][filt])
1880 tns->direction[w][filt] = faad_get1bit(ld
1881 DEBUGVAR(1,78,"tns_data(): direction"));
1882 #if 0
1883 printf("%d\n", tns->direction[w][filt]);
1884 #endif
1885 tns->coef_compress[w][filt] = faad_get1bit(ld
1886 DEBUGVAR(1,79,"tns_data(): coef_compress"));
1887 #if 0
1888 printf("%d\n", tns->coef_compress[w][filt]);
1889 #endif
1891 coef_bits = start_coef_bits - tns->coef_compress[w][filt];
1892 for (i = 0; i < tns->order[w][filt]; i++)
1894 tns->coef[w][filt][i] = (uint8_t)faad_getbits(ld, coef_bits
1895 DEBUGVAR(1,80,"tns_data(): coef"));
1896 #if 0
1897 printf("%d\n", tns->coef[w][filt][i]);
1898 #endif
1905 #ifdef LTP_DEC
1906 /* Table 4.4.28 */
1907 static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
1909 uint8_t sfb, w;
1911 ltp->lag = 0;
1913 #ifdef LD_DEC
1914 if (hDecoder->object_type == LD)
1916 ltp->lag_update = (uint8_t)faad_getbits(ld, 1
1917 DEBUGVAR(1,142,"ltp_data(): lag_update"));
1919 if (ltp->lag_update)
1921 ltp->lag = (uint16_t)faad_getbits(ld, 10
1922 DEBUGVAR(1,81,"ltp_data(): lag"));
1924 } else {
1925 #endif
1926 ltp->lag = (uint16_t)faad_getbits(ld, 11
1927 DEBUGVAR(1,81,"ltp_data(): lag"));
1928 #ifdef LD_DEC
1930 #endif
1932 /* Check length of lag */
1933 if (ltp->lag > (hDecoder->frameLength << 1))
1934 return 18;
1936 ltp->coef = (uint8_t)faad_getbits(ld, 3
1937 DEBUGVAR(1,82,"ltp_data(): coef"));
1939 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1941 for (w = 0; w < ics->num_windows; w++)
1943 if ((ltp->short_used[w] = faad_get1bit(ld
1944 DEBUGVAR(1,83,"ltp_data(): short_used"))) & 1)
1946 ltp->short_lag_present[w] = faad_get1bit(ld
1947 DEBUGVAR(1,84,"ltp_data(): short_lag_present"));
1948 if (ltp->short_lag_present[w])
1950 ltp->short_lag[w] = (uint8_t)faad_getbits(ld, 4
1951 DEBUGVAR(1,85,"ltp_data(): short_lag"));
1955 } else {
1956 ltp->last_band = (ics->max_sfb < MAX_LTP_SFB ? ics->max_sfb : MAX_LTP_SFB);
1958 for (sfb = 0; sfb < ltp->last_band; sfb++)
1960 ltp->long_used[sfb] = faad_get1bit(ld
1961 DEBUGVAR(1,86,"ltp_data(): long_used"));
1965 return 0;
1967 #endif
1969 /* Table 4.4.29 */
1970 static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
1971 int16_t *spectral_data)
1973 int8_t i;
1974 uint8_t g;
1975 uint16_t inc, k, p = 0;
1976 uint8_t groups = 0;
1977 uint8_t sect_cb;
1978 uint8_t result;
1979 uint16_t nshort = hDecoder->frameLength/8;
1981 #ifdef PROFILE
1982 int64_t count = faad_get_ts();
1983 #endif
1985 for(g = 0; g < ics->num_window_groups; g++)
1987 p = groups*nshort;
1989 for (i = 0; i < ics->num_sec[g]; i++)
1991 sect_cb = ics->sect_cb[g][i];
1993 inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
1995 switch (sect_cb)
1997 case ZERO_HCB:
1998 case NOISE_HCB:
1999 case INTENSITY_HCB:
2000 case INTENSITY_HCB2:
2001 //#define SD_PRINT
2002 #ifdef SD_PRINT
2004 int j;
2005 for (j = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; j < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; j++)
2007 printf("%d\n", 0);
2010 #endif
2011 //#define SFBO_PRINT
2012 #ifdef SFBO_PRINT
2013 printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2014 #endif
2015 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
2016 ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2017 break;
2018 default:
2019 #ifdef SFBO_PRINT
2020 printf("%d\n", ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
2021 #endif
2022 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
2023 k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
2025 if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
2026 return result;
2027 #ifdef SD_PRINT
2029 int j;
2030 for (j = p; j < p+inc; j++)
2032 printf("%d\n", spectral_data[j]);
2035 #endif
2036 p += inc;
2038 break;
2041 groups += ics->window_group_length[g];
2044 #ifdef PROFILE
2045 count = faad_get_ts() - count;
2046 hDecoder->spectral_cycles += count;
2047 #endif
2049 return 0;
2052 /* Table 4.4.30 */
2053 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count)
2055 uint16_t i, n, dataElementLength;
2056 uint8_t dataElementLengthPart;
2057 uint8_t align = 4, data_element_version, loopCounter;
2059 uint8_t extension_type = (uint8_t)faad_getbits(ld, 4
2060 DEBUGVAR(1,87,"extension_payload(): extension_type"));
2062 switch (extension_type)
2064 case EXT_DYNAMIC_RANGE:
2065 drc->present = 1;
2066 n = dynamic_range_info(ld, drc);
2067 return n;
2068 case EXT_FILL_DATA:
2069 /* fill_nibble = */ faad_getbits(ld, 4
2070 DEBUGVAR(1,136,"extension_payload(): fill_nibble")); /* must be "0000" */
2071 for (i = 0; i < count-1; i++)
2073 /* fill_byte[i] = */ faad_getbits(ld, 8
2074 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be "10100101" */
2076 return count;
2077 case EXT_DATA_ELEMENT:
2078 data_element_version = (uint8_t)faad_getbits(ld, 4
2079 DEBUGVAR(1,400,"extension_payload(): data_element_version"));
2080 switch (data_element_version)
2082 case ANC_DATA:
2083 loopCounter = 0;
2084 dataElementLength = 0;
2085 do {
2086 dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
2087 DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
2088 dataElementLength += dataElementLengthPart;
2089 loopCounter++;
2090 } while (dataElementLengthPart == 255);
2092 for (i = 0; i < dataElementLength; i++)
2094 /* data_element_byte[i] = */ faad_getbits(ld, 8
2095 DEBUGVAR(1,402,"extension_payload(): data_element_byte"));
2096 return (dataElementLength+loopCounter+1);
2098 default:
2099 align = 0;
2101 case EXT_FIL:
2102 default:
2103 faad_getbits(ld, align
2104 DEBUGVAR(1,88,"extension_payload(): fill_nibble"));
2105 for (i = 0; i < count-1; i++)
2107 /* other_bits[i] = */ faad_getbits(ld, 8
2108 DEBUGVAR(1,89,"extension_payload(): fill_bit"));
2110 return count;
2114 /* Table 4.4.31 */
2115 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc)
2117 uint8_t i, n = 1;
2118 uint8_t band_incr;
2120 drc->num_bands = 1;
2122 if (faad_get1bit(ld
2123 DEBUGVAR(1,90,"dynamic_range_info(): has instance_tag")) & 1)
2125 drc->pce_instance_tag = (uint8_t)faad_getbits(ld, 4
2126 DEBUGVAR(1,91,"dynamic_range_info(): pce_instance_tag"));
2127 /* drc->drc_tag_reserved_bits = */ faad_getbits(ld, 4
2128 DEBUGVAR(1,92,"dynamic_range_info(): drc_tag_reserved_bits"));
2129 n++;
2132 drc->excluded_chns_present = faad_get1bit(ld
2133 DEBUGVAR(1,93,"dynamic_range_info(): excluded_chns_present"));
2134 if (drc->excluded_chns_present == 1)
2136 n += excluded_channels(ld, drc);
2139 if (faad_get1bit(ld
2140 DEBUGVAR(1,94,"dynamic_range_info(): has bands data")) & 1)
2142 band_incr = (uint8_t)faad_getbits(ld, 4
2143 DEBUGVAR(1,95,"dynamic_range_info(): band_incr"));
2144 /* drc->drc_bands_reserved_bits = */ faad_getbits(ld, 4
2145 DEBUGVAR(1,96,"dynamic_range_info(): drc_bands_reserved_bits"));
2146 n++;
2147 drc->num_bands += band_incr;
2149 for (i = 0; i < drc->num_bands; i++);
2151 drc->band_top[i] = (uint8_t)faad_getbits(ld, 8
2152 DEBUGVAR(1,97,"dynamic_range_info(): band_top"));
2153 n++;
2157 if (faad_get1bit(ld
2158 DEBUGVAR(1,98,"dynamic_range_info(): has prog_ref_level")) & 1)
2160 drc->prog_ref_level = (uint8_t)faad_getbits(ld, 7
2161 DEBUGVAR(1,99,"dynamic_range_info(): prog_ref_level"));
2162 /* drc->prog_ref_level_reserved_bits = */ faad_get1bit(ld
2163 DEBUGVAR(1,100,"dynamic_range_info(): prog_ref_level_reserved_bits"));
2164 n++;
2167 for (i = 0; i < drc->num_bands; i++)
2169 drc->dyn_rng_sgn[i] = faad_get1bit(ld
2170 DEBUGVAR(1,101,"dynamic_range_info(): dyn_rng_sgn"));
2171 drc->dyn_rng_ctl[i] = (uint8_t)faad_getbits(ld, 7
2172 DEBUGVAR(1,102,"dynamic_range_info(): dyn_rng_ctl"));
2173 n++;
2176 return n;
2179 /* Table 4.4.32 */
2180 static uint8_t excluded_channels(bitfile *ld, drc_info *drc)
2182 uint8_t i, n = 0;
2183 uint8_t num_excl_chan = 7;
2185 for (i = 0; i < 7; i++)
2187 drc->exclude_mask[i] = faad_get1bit(ld
2188 DEBUGVAR(1,103,"excluded_channels(): exclude_mask"));
2190 n++;
2192 while ((drc->additional_excluded_chns[n-1] = faad_get1bit(ld
2193 DEBUGVAR(1,104,"excluded_channels(): additional_excluded_chns"))) == 1)
2195 for (i = num_excl_chan; i < num_excl_chan+7; i++)
2197 drc->exclude_mask[i] = faad_get1bit(ld
2198 DEBUGVAR(1,105,"excluded_channels(): exclude_mask"));
2200 n++;
2201 num_excl_chan += 7;
2204 return n;
2207 /* Annex A: Audio Interchange Formats */
2209 /* Table 1.A.2 */
2210 void get_adif_header(adif_header *adif, bitfile *ld)
2212 uint8_t i;
2214 /* adif_id[0] = */ faad_getbits(ld, 8
2215 DEBUGVAR(1,106,"get_adif_header(): adif_id[0]"));
2216 /* adif_id[1] = */ faad_getbits(ld, 8
2217 DEBUGVAR(1,107,"get_adif_header(): adif_id[1]"));
2218 /* adif_id[2] = */ faad_getbits(ld, 8
2219 DEBUGVAR(1,108,"get_adif_header(): adif_id[2]"));
2220 /* adif_id[3] = */ faad_getbits(ld, 8
2221 DEBUGVAR(1,109,"get_adif_header(): adif_id[3]"));
2222 adif->copyright_id_present = faad_get1bit(ld
2223 DEBUGVAR(1,110,"get_adif_header(): copyright_id_present"));
2224 if(adif->copyright_id_present)
2226 for (i = 0; i < 72/8; i++)
2228 adif->copyright_id[i] = (int8_t)faad_getbits(ld, 8
2229 DEBUGVAR(1,111,"get_adif_header(): copyright_id"));
2231 adif->copyright_id[i] = 0;
2233 adif->original_copy = faad_get1bit(ld
2234 DEBUGVAR(1,112,"get_adif_header(): original_copy"));
2235 adif->home = faad_get1bit(ld
2236 DEBUGVAR(1,113,"get_adif_header(): home"));
2237 adif->bitstream_type = faad_get1bit(ld
2238 DEBUGVAR(1,114,"get_adif_header(): bitstream_type"));
2239 adif->bitrate = faad_getbits(ld, 23
2240 DEBUGVAR(1,115,"get_adif_header(): bitrate"));
2241 adif->num_program_config_elements = (uint8_t)faad_getbits(ld, 4
2242 DEBUGVAR(1,116,"get_adif_header(): num_program_config_elements"));
2244 for (i = 0; i < adif->num_program_config_elements + 1; i++)
2246 if(adif->bitstream_type == 0)
2248 adif->adif_buffer_fullness = faad_getbits(ld, 20
2249 DEBUGVAR(1,117,"get_adif_header(): adif_buffer_fullness"));
2250 } else {
2251 adif->adif_buffer_fullness = 0;
2254 program_config_element(&adif->pce[i], ld);
2258 /* Table 1.A.5 */
2259 uint8_t adts_frame(adts_header *adts, bitfile *ld)
2261 /* faad_byte_align(ld); */
2262 if (adts_fixed_header(adts, ld))
2263 return 5;
2264 adts_variable_header(adts, ld);
2265 adts_error_check(adts, ld);
2267 return 0;
2270 /* Table 1.A.6 */
2271 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld)
2273 uint16_t i;
2274 uint8_t sync_err = 1;
2276 /* try to recover from sync errors */
2277 for (i = 0; i < 768; i++)
2279 adts->syncword = (uint16_t)faad_showbits(ld, 12);
2280 if (adts->syncword != 0xFFF)
2282 faad_getbits(ld, 8
2283 DEBUGVAR(0,0,""));
2284 } else {
2285 sync_err = 0;
2286 faad_getbits(ld, 12
2287 DEBUGVAR(1,118,"adts_fixed_header(): syncword"));
2288 break;
2291 if (sync_err)
2292 return 5;
2294 adts->id = faad_get1bit(ld
2295 DEBUGVAR(1,119,"adts_fixed_header(): id"));
2296 adts->layer = (uint8_t)faad_getbits(ld, 2
2297 DEBUGVAR(1,120,"adts_fixed_header(): layer"));
2298 adts->protection_absent = faad_get1bit(ld
2299 DEBUGVAR(1,121,"adts_fixed_header(): protection_absent"));
2300 adts->profile = (uint8_t)faad_getbits(ld, 2
2301 DEBUGVAR(1,122,"adts_fixed_header(): profile"));
2302 adts->sf_index = (uint8_t)faad_getbits(ld, 4
2303 DEBUGVAR(1,123,"adts_fixed_header(): sf_index"));
2304 adts->private_bit = faad_get1bit(ld
2305 DEBUGVAR(1,124,"adts_fixed_header(): private_bit"));
2306 adts->channel_configuration = (uint8_t)faad_getbits(ld, 3
2307 DEBUGVAR(1,125,"adts_fixed_header(): channel_configuration"));
2308 adts->original = faad_get1bit(ld
2309 DEBUGVAR(1,126,"adts_fixed_header(): original"));
2310 adts->home = faad_get1bit(ld
2311 DEBUGVAR(1,127,"adts_fixed_header(): home"));
2313 if (adts->old_format == 1)
2315 /* Removed in corrigendum 14496-3:2002 */
2316 if (adts->id == 0)
2318 adts->emphasis = (uint8_t)faad_getbits(ld, 2
2319 DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
2323 return 0;
2326 /* Table 1.A.7 */
2327 static void adts_variable_header(adts_header *adts, bitfile *ld)
2329 adts->copyright_identification_bit = faad_get1bit(ld
2330 DEBUGVAR(1,129,"adts_variable_header(): copyright_identification_bit"));
2331 adts->copyright_identification_start = faad_get1bit(ld
2332 DEBUGVAR(1,130,"adts_variable_header(): copyright_identification_start"));
2333 adts->aac_frame_length = (uint16_t)faad_getbits(ld, 13
2334 DEBUGVAR(1,131,"adts_variable_header(): aac_frame_length"));
2335 adts->adts_buffer_fullness = (uint16_t)faad_getbits(ld, 11
2336 DEBUGVAR(1,132,"adts_variable_header(): adts_buffer_fullness"));
2337 adts->no_raw_data_blocks_in_frame = (uint8_t)faad_getbits(ld, 2
2338 DEBUGVAR(1,133,"adts_variable_header(): no_raw_data_blocks_in_frame"));
2341 /* Table 1.A.8 */
2342 static void adts_error_check(adts_header *adts, bitfile *ld)
2344 if (adts->protection_absent == 0)
2346 adts->crc_check = (uint16_t)faad_getbits(ld, 16
2347 DEBUGVAR(1,134,"adts_error_check(): crc_check"));