comparison libfaad2/syntax.c @ 10725:e989150f8216

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