comparison libfaad2/syntax.c @ 12527:4a370c80fe5c

update to the 2.0 release of faad, patch by adland
author diego
date Wed, 02 Jun 2004 22:59:04 +0000
parents 3185f64f6350
children d81145997036
comparison
equal deleted inserted replaced
12526:e183ad37d24c 12527:4a370c80fe5c
1 /* 1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 ** 4 **
5 ** This program is free software; you can redistribute it and/or modify 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 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 7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version. 8 ** (at your option) any later version.
20 ** forbidden. 20 ** forbidden.
21 ** 21 **
22 ** Commercial non-GPL licensing of this software is possible. 22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 ** 24 **
25 ** $Id: syntax.c,v 1.56 2003/09/30 12:43:05 menno Exp $ 25 ** $Id: syntax.c,v 1.2 2003/10/03 22:22:27 alex Exp $
26 **/ 26 **/
27 27
28 /* 28 /*
29 Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio) 29 Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
30 */ 30 */
49 #ifdef SBR_DEC 49 #ifdef SBR_DEC
50 #include "sbr_syntax.h" 50 #include "sbr_syntax.h"
51 #endif 51 #endif
52 52
53 53
54 /* static function declarations */
55 static void decode_sce_lfe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
56 uint8_t id_syn_ele);
57 static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
58 uint8_t id_syn_ele);
59 static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld,
60 uint8_t channel, uint8_t *tag);
61 static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld,
62 uint8_t channel, uint8_t *tag);
63 #ifdef COUPLING_DEC
64 static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld);
65 #endif
66 static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld);
67 static uint8_t program_config_element(program_config *pce, bitfile *ld);
68 static uint8_t fill_element(faacDecHandle hDecoder, bitfile *ld, drc_info *drc
69 #ifdef SBR_DEC
70 ,uint8_t sbr_ele
71 #endif
72 );
73 static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele,
74 bitfile *ld, ic_stream *ics, uint8_t scal_flag,
75 int16_t *spec_data);
76 static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
77 uint8_t common_window);
78 static uint8_t section_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld);
79 static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld);
80 #ifdef SSR_DEC
81 static void gain_control_data(bitfile *ld, ic_stream *ics);
82 #endif
83 static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
84 int16_t *spectral_data);
85 static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count);
86 static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld);
87 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld);
88 static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
89 static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
90 static void adts_variable_header(adts_header *adts, bitfile *ld);
91 static void adts_error_check(adts_header *adts, bitfile *ld);
92 static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
93 static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
94 #ifdef SCALABLE_DEC
95 static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
96 bitfile *ld, uint8_t this_layer_stereo);
97 #endif
98
99
54 /* Table 4.4.1 */ 100 /* Table 4.4.1 */
55 int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC, 101 int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
56 program_config *pce_out) 102 program_config *pce_out)
57 { 103 {
58 program_config pce; 104 program_config pce;
59 105
60 /* 1024 or 960 */ 106 /* 1024 or 960 */
61 mp4ASC->frameLengthFlag = faad_get1bit(ld 107 mp4ASC->frameLengthFlag = faad_get1bit(ld
62 DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag")); 108 DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
109 #ifndef ALLOW_SMALL_FRAMELENGTH
110 if (mp4ASC->frameLengthFlag == 1)
111 return -3;
112 #endif
63 113
64 mp4ASC->dependsOnCoreCoder = faad_get1bit(ld 114 mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
65 DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder")); 115 DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
66 if (mp4ASC->dependsOnCoreCoder == 1) 116 if (mp4ASC->dependsOnCoreCoder == 1)
67 { 117 {
70 } 120 }
71 121
72 mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag")); 122 mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
73 if (mp4ASC->channelsConfiguration == 0) 123 if (mp4ASC->channelsConfiguration == 0)
74 { 124 {
75 program_config_element(&pce, ld); 125 if (program_config_element(&pce, ld))
126 return -3;
76 //mp4ASC->channelsConfiguration = pce.channels; 127 //mp4ASC->channelsConfiguration = pce.channels;
77 128
78 if (pce_out != NULL) 129 if (pce_out != NULL)
79 memcpy(pce_out, &pce, sizeof(program_config)); 130 memcpy(pce_out, &pce, sizeof(program_config));
80 131
110 Configuration Element in GASpecificConfig(). The decoder shall ignore 161 Configuration Element in GASpecificConfig(). The decoder shall ignore
111 any Program Configuration Elements that may occur in raw data blocks. 162 any Program Configuration Elements that may occur in raw data blocks.
112 PCEs transmitted in raw data blocks cannot be used to convey decoder 163 PCEs transmitted in raw data blocks cannot be used to convey decoder
113 configuration information. 164 configuration information.
114 */ 165 */
115 uint8_t program_config_element(program_config *pce, bitfile *ld) 166 static uint8_t program_config_element(program_config *pce, bitfile *ld)
116 { 167 {
117 uint8_t i; 168 uint8_t i;
118 169
119 memset(pce, 0, sizeof(program_config)); 170 memset(pce, 0, sizeof(program_config));
120 171
255 pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8 306 pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
256 DEBUGVAR(1,37,"program_config_element(): comment_field_data")); 307 DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
257 } 308 }
258 pce->comment_field_data[i] = 0; 309 pce->comment_field_data[i] = 0;
259 310
311 if (pce->channels > MAX_CHANNELS)
312 return 22;
313
260 return 0; 314 return 0;
261 } 315 }
262 316
263 element *decode_sce_lfe(faacDecHandle hDecoder, 317 static void decode_sce_lfe(faacDecHandle hDecoder,
264 faacDecFrameInfo *hInfo, bitfile *ld, 318 faacDecFrameInfo *hInfo, bitfile *ld,
265 real_t **spec_coef, uint8_t id_syn_ele) 319 uint8_t id_syn_ele)
266 { 320 {
267 element *ele;
268 uint8_t channels = hDecoder->fr_channels; 321 uint8_t channels = hDecoder->fr_channels;
322 uint8_t tag = 0;
269 323
270 if (channels+1 > MAX_CHANNELS) 324 if (channels+1 > MAX_CHANNELS)
271 { 325 {
272 hInfo->error = 12; 326 hInfo->error = 12;
273 return NULL; 327 return;
274 } 328 }
275 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS) 329 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
276 { 330 {
277 hInfo->error = 13; 331 hInfo->error = 13;
278 return NULL; 332 return;
279 } 333 }
280 334
281 spec_coef[channels] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t)); 335 /* for SCE hDecoder->element_output_channels[] is not set here because this
282 336 can become 2 when some form of Parametric Stereo coding is used
283 ele = (element*)malloc(sizeof(element)); 337 */
284 memset(ele, 0, sizeof(element)); 338
285 ele->ele_id = id_syn_ele; 339 /* save the syntax element id */
286 ele->channel = channels; 340 hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
287 ele->paired_channel = -1; 341
288 342 /* decode the element */
289 hInfo->error = single_lfe_channel_element(hDecoder, ele, 343 hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
290 ld, spec_coef[channels]); 344
291 345 /* map output channels position to internal data channels */
346 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
347 {
348 /* this might be faulty when pce_set is true */
349 hDecoder->internal_channel[channels] = channels;
350 hDecoder->internal_channel[channels+1] = channels+1;
351 } else {
352 if (hDecoder->pce_set)
353 hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
354 else
355 hDecoder->internal_channel[channels] = channels;
356 }
357
358 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
359 hDecoder->fr_ch_ele++;
360 }
361
362 static void decode_cpe(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, bitfile *ld,
363 uint8_t id_syn_ele)
364 {
365 uint8_t channels = hDecoder->fr_channels;
366 uint8_t tag = 0;
367
368 if (channels+2 > MAX_CHANNELS)
369 {
370 hInfo->error = 12;
371 return;
372 }
373 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
374 {
375 hInfo->error = 13;
376 return;
377 }
378
379 /* for CPE the number of output channels is always 2 */
380 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
381 {
382 /* element_output_channels not set yet */
383 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
384 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
385 /* element inconsistency */
386 hInfo->error = 21;
387 return;
388 }
389
390 /* save the syntax element id */
391 hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
392
393 /* decode the element */
394 hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
395
396 /* map output channel position to internal data channels */
292 if (hDecoder->pce_set) 397 if (hDecoder->pce_set)
293 hDecoder->internal_channel[hDecoder->pce.sce_channel[ele->element_instance_tag]] = channels; 398 {
294 else 399 hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
295 hDecoder->internal_channel[channels] = channels; 400 hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
296
297 if (id_syn_ele == ID_SCE)
298 hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
299 else /* LFE */
300 hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
301
302 hDecoder->fr_channels++;
303 hDecoder->fr_ch_ele++;
304
305 return ele;
306 }
307
308 element *decode_cpe(faacDecHandle hDecoder,
309 faacDecFrameInfo *hInfo, bitfile *ld,
310 real_t **spec_coef, uint8_t id_syn_ele)
311 {
312 element *ele;
313 uint8_t channels = hDecoder->fr_channels;
314
315 if (channels+2 > MAX_CHANNELS)
316 {
317 hInfo->error = 12;
318 return NULL;
319 }
320 if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
321 {
322 hInfo->error = 13;
323 return NULL;
324 }
325
326 spec_coef[channels] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t));
327 spec_coef[channels+1] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t));
328
329 ele = (element*)malloc(sizeof(element));
330 memset(ele, 0, sizeof(element));
331 ele->ele_id = id_syn_ele;
332 ele->channel = channels;
333 ele->paired_channel = channels+1;
334
335 hInfo->error = channel_pair_element(hDecoder, ele,
336 ld, spec_coef[channels], spec_coef[channels+1]);
337
338 if (hDecoder->pce_set)
339 {
340 hDecoder->internal_channel[hDecoder->pce.cpe_channel[ele->element_instance_tag]] = channels;
341 hDecoder->internal_channel[hDecoder->pce.cpe_channel[ele->element_instance_tag]+1] = channels+1;
342 } else { 401 } else {
343 hDecoder->internal_channel[channels] = channels; 402 hDecoder->internal_channel[channels] = channels;
344 hDecoder->internal_channel[channels+1] = channels+1; 403 hDecoder->internal_channel[channels+1] = channels+1;
345 } 404 }
346 405
347 hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
348 hDecoder->channel_element[channels+1] = hDecoder->fr_ch_ele;
349
350 hDecoder->fr_channels += 2; 406 hDecoder->fr_channels += 2;
351 hDecoder->fr_ch_ele++; 407 hDecoder->fr_ch_ele++;
352 408 }
353 return ele; 409
354 } 410 void raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
355 411 bitfile *ld, program_config *pce, drc_info *drc)
356 element **raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
357 bitfile *ld, element **elements,
358 real_t **spec_coef, program_config *pce, drc_info *drc)
359 { 412 {
360 uint8_t id_syn_ele; 413 uint8_t id_syn_ele;
361 uint8_t ch_ele = 0;
362 414
363 hDecoder->fr_channels = 0; 415 hDecoder->fr_channels = 0;
364 hDecoder->fr_ch_ele = 0; 416 hDecoder->fr_ch_ele = 0;
365 hDecoder->first_syn_ele = 25; 417 hDecoder->first_syn_ele = 25;
366 hDecoder->has_lfe = 0; 418 hDecoder->has_lfe = 0;
374 DEBUGVAR(1,4,"faacDecDecode(): id_syn_ele"))) != ID_END) 426 DEBUGVAR(1,4,"faacDecDecode(): id_syn_ele"))) != ID_END)
375 { 427 {
376 switch (id_syn_ele) { 428 switch (id_syn_ele) {
377 case ID_SCE: 429 case ID_SCE:
378 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele; 430 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
379 hDecoder->last_syn_ele = id_syn_ele; 431 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
380 elements[ch_ele++] = decode_sce_lfe(hDecoder,
381 hInfo, ld, spec_coef, id_syn_ele);
382 if (hInfo->error > 0) 432 if (hInfo->error > 0)
383 return elements; 433 return;
384 break; 434 break;
385 case ID_CPE: 435 case ID_CPE:
386 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele; 436 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
387 hDecoder->last_syn_ele = id_syn_ele; 437 decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
388 elements[ch_ele++] = decode_cpe(hDecoder,
389 hInfo, ld, spec_coef, id_syn_ele);
390 if (hInfo->error > 0) 438 if (hInfo->error > 0)
391 return elements; 439 return;
392 break; 440 break;
393 case ID_LFE: 441 case ID_LFE:
394 hDecoder->has_lfe++; 442 hDecoder->has_lfe++;
395 elements[ch_ele++] = decode_sce_lfe(hDecoder, 443 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
396 hInfo, ld, spec_coef, id_syn_ele);
397 if (hInfo->error > 0) 444 if (hInfo->error > 0)
398 return elements; 445 return;
399 break; 446 break;
400 case ID_CCE: /* not implemented yet, but skip the bits */ 447 case ID_CCE: /* not implemented yet, but skip the bits */
448 #ifdef COUPLING_DEC
401 hInfo->error = coupling_channel_element(hDecoder, ld); 449 hInfo->error = coupling_channel_element(hDecoder, ld);
450 #else
451 hInfo->error = 6;
452 #endif
402 if (hInfo->error > 0) 453 if (hInfo->error > 0)
403 return elements; 454 return;
404 break; 455 break;
405 case ID_DSE: 456 case ID_DSE:
406 data_stream_element(hDecoder, ld); 457 data_stream_element(hDecoder, ld);
407 break; 458 break;
408 case ID_PCE: 459 case ID_PCE:
409 if ((hInfo->error = program_config_element(pce, ld)) > 0) 460 /* 14496-4: 5.6.4.1.2.1.3: */
410 return elements; 461 /* program_configuration_element()'s in access units shall be ignored */
411 hDecoder->pce_set = 1; 462 program_config_element(pce, ld);
463 //if ((hInfo->error = program_config_element(pce, ld)) > 0)
464 // return;
465 //hDecoder->pce_set = 1;
412 break; 466 break;
413 case ID_FIL: 467 case ID_FIL:
414 /* one sbr_info describes a channel_element not a channel! */ 468 /* one sbr_info describes a channel_element not a channel! */
469 /* if we encounter SBR data here: error */
470 /* SBR data will be read directly in the SCE/LFE/CPE element */
415 if ((hInfo->error = fill_element(hDecoder, ld, drc 471 if ((hInfo->error = fill_element(hDecoder, ld, drc
416 #ifdef SBR_DEC 472 #ifdef SBR_DEC
417 , (ch_ele-1) 473 , INVALID_SBR_ELEMENT
418 #endif 474 #endif
419 )) > 0) 475 )) > 0)
420 return elements; 476 return;
421 #ifdef SBR_DEC
422 if (hDecoder->sbr_used[ch_ele-1])
423 {
424 hDecoder->sbr_present_flag = 1;
425 hDecoder->sbr[ch_ele-1]->sample_rate = get_sample_rate(hDecoder->sf_index);
426 hDecoder->sbr[ch_ele-1]->sample_rate *= 2;
427 }
428 #endif
429 break; 477 break;
430 } 478 }
431 } 479 }
432 #ifdef ERROR_RESILIENCE 480 #ifdef ERROR_RESILIENCE
433 } else { 481 } else {
434 /* Table 262: er_raw_data_block() */ 482 /* Table 262: er_raw_data_block() */
435 switch (hDecoder->channelConfiguration) 483 switch (hDecoder->channelConfiguration)
436 { 484 {
437 case 1: 485 case 1:
438 elements[ch_ele++] = decode_sce_lfe(hDecoder, 486 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
439 hInfo, ld, spec_coef, ID_SCE);
440 if (hInfo->error > 0) 487 if (hInfo->error > 0)
441 return elements; 488 return;
442 break; 489 break;
443 case 2: 490 case 2:
444 elements[ch_ele++] = decode_cpe(hDecoder, 491 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
445 hInfo, ld, spec_coef, ID_CPE);
446 if (hInfo->error > 0) 492 if (hInfo->error > 0)
447 return elements; 493 return;
448 break; 494 break;
449 case 3: 495 case 3:
450 elements[ch_ele++] = decode_sce_lfe(hDecoder, 496 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
451 hInfo, ld, spec_coef, ID_SCE); 497 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
452 elements[ch_ele++] = decode_cpe(hDecoder,
453 hInfo, ld, spec_coef, ID_CPE);
454 if (hInfo->error > 0) 498 if (hInfo->error > 0)
455 return elements; 499 return;
456 break; 500 break;
457 case 4: 501 case 4:
458 elements[ch_ele++] = decode_sce_lfe(hDecoder, 502 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
459 hInfo, ld, spec_coef, ID_SCE); 503 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
460 elements[ch_ele++] = decode_cpe(hDecoder, 504 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
461 hInfo, ld, spec_coef, ID_CPE);
462 elements[ch_ele++] = decode_sce_lfe(hDecoder,
463 hInfo, ld, spec_coef, ID_SCE);
464 if (hInfo->error > 0) 505 if (hInfo->error > 0)
465 return elements; 506 return;
466 break; 507 break;
467 case 5: 508 case 5:
468 elements[ch_ele++] = decode_sce_lfe(hDecoder, 509 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
469 hInfo, ld, spec_coef, ID_SCE); 510 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
470 elements[ch_ele++] = decode_cpe(hDecoder, 511 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
471 hInfo, ld, spec_coef, ID_CPE);
472 elements[ch_ele++] = decode_cpe(hDecoder,
473 hInfo, ld, spec_coef, ID_CPE);
474 if (hInfo->error > 0) 512 if (hInfo->error > 0)
475 return elements; 513 return;
476 break; 514 break;
477 case 6: 515 case 6:
478 elements[ch_ele++] = decode_sce_lfe(hDecoder, 516 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
479 hInfo, ld, spec_coef, ID_SCE); 517 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
480 elements[ch_ele++] = decode_cpe(hDecoder, 518 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
481 hInfo, ld, spec_coef, ID_CPE); 519 decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
482 elements[ch_ele++] = decode_cpe(hDecoder,
483 hInfo, ld, spec_coef, ID_CPE);
484 elements[ch_ele++] = decode_sce_lfe(hDecoder,
485 hInfo, ld, spec_coef, ID_LFE);
486 if (hInfo->error > 0) 520 if (hInfo->error > 0)
487 return elements; 521 return;
488 break; 522 break;
489 case 7: 523 case 7: /* 8 channels */
490 elements[ch_ele++] = decode_sce_lfe(hDecoder, 524 decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
491 hInfo, ld, spec_coef, ID_SCE); 525 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
492 elements[ch_ele++] = decode_cpe(hDecoder, 526 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
493 hInfo, ld, spec_coef, ID_CPE); 527 decode_cpe(hDecoder, hInfo, ld, ID_CPE);
494 elements[ch_ele++] = decode_cpe(hDecoder, 528 decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
495 hInfo, ld, spec_coef, ID_CPE);
496 elements[ch_ele++] = decode_cpe(hDecoder,
497 hInfo, ld, spec_coef, ID_CPE);
498 elements[ch_ele++] = decode_sce_lfe(hDecoder,
499 hInfo, ld, spec_coef, ID_LFE);
500 if (hInfo->error > 0) 529 if (hInfo->error > 0)
501 return elements; 530 return;
502 break; 531 break;
503 default: 532 default:
504 hInfo->error = 7; 533 hInfo->error = 7;
505 return elements; 534 return;
506 } 535 }
507 #if 0 536 #if 0
508 cnt = bits_to_decode() / 8; 537 cnt = bits_to_decode() / 8;
509 while (cnt >= 1) 538 while (cnt >= 1)
510 { 539 {
520 #endif 549 #endif
521 { 550 {
522 faad_byte_align(ld); 551 faad_byte_align(ld);
523 } 552 }
524 553
525 return elements; 554 return;
526 } 555 }
527 556
528 /* Table 4.4.4 and */ 557 /* Table 4.4.4 and */
529 /* Table 4.4.9 */ 558 /* Table 4.4.9 */
530 static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, 559 static uint8_t single_lfe_channel_element(faacDecHandle hDecoder, bitfile *ld,
531 element *sce, bitfile *ld, 560 uint8_t channel, uint8_t *tag)
532 real_t *spec_coef)
533 { 561 {
534 uint8_t retval = 0; 562 uint8_t retval = 0;
535 ic_stream *ics = &(sce->ics1); 563 element sce = {0};
536 int16_t spec_data[1024]; 564 ic_stream *ics = &(sce.ics1);
537 #ifdef DRM 565 ALIGN int16_t spec_data[1024] = {0};
566
567 sce.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
568 DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
569
570 *tag = sce.element_instance_tag;
571 sce.channel = channel;
572 sce.paired_channel = -1;
573
574 retval = individual_channel_stream(hDecoder, &sce, ld, ics, 0, spec_data);
575 if (retval > 0)
576 return retval;
577
578 #ifdef SBR_DEC
579 /* check if next bitstream element is a fill element */
580 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
581 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
582 {
583 faad_flushbits(ld, LEN_SE_ID);
584
585 /* one sbr_info describes a channel_element not a channel! */
586 if ((retval = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
587 {
588 return retval;
589 }
590 }
591 #endif
592
593 /* noiseless coding is done, spectral reconstruction is done now */
594 retval = reconstruct_single_channel(hDecoder, ics, &sce, spec_data);
595 if (retval > 0)
596 return retval;
597
598 return 0;
599 }
600
601 /* Table 4.4.5 */
602 static uint8_t channel_pair_element(faacDecHandle hDecoder, bitfile *ld,
603 uint8_t channels, uint8_t *tag)
604 {
605 ALIGN int16_t spec_data1[1024] = {0};
606 ALIGN int16_t spec_data2[1024] = {0};
607 element cpe = {0};
608 ic_stream *ics1 = &(cpe.ics1);
609 ic_stream *ics2 = &(cpe.ics2);
538 uint8_t result; 610 uint8_t result;
539 611
540 if (hDecoder->object_type != DRM_ER_LC) 612 cpe.channel = channels;
541 #endif 613 cpe.paired_channel = channels+1;
542 sce->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG 614
543 DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag")); 615 cpe.element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
544
545 #ifdef DRM
546 if (hDecoder->object_type == DRM_ER_LC)
547 {
548 individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data);
549
550 if (ics->tns_data_present)
551 tns_data(ics, &(ics->tns), ld);
552
553 if ((result = faad_check_CRC( ld, faad_get_processed_bits(ld) - 8 )) > 0)
554 return result;
555
556 /* error resilient spectral data decoding */
557 if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
558 return result;
559
560 /* pulse coding reconstruction */
561 if (ics->pulse_data_present)
562 {
563 if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
564 {
565 if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
566 return result;
567 } else {
568 return 2; /* pulse coding not allowed for short blocks */
569 }
570 }
571 } else
572 #endif
573 {
574 retval = individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data);
575 if (retval > 0)
576 return retval;
577 }
578
579
580 /* noiseless coding is done, spectral reconstruction is done now */
581
582 /* inverse quantization */
583 inverse_quantization(spec_coef, spec_data, hDecoder->frameLength);
584
585 /* apply scalefactors */
586 apply_scalefactors(hDecoder, ics, spec_coef, hDecoder->frameLength);
587
588 /* deinterleave short block grouping */
589 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
590 quant_to_spec(ics, spec_coef, hDecoder->frameLength);
591
592 return 0;
593 }
594
595 /* Table 4.4.5 */
596 static uint8_t channel_pair_element(faacDecHandle hDecoder, element *cpe,
597 bitfile *ld,
598 real_t *spec_coef1, real_t *spec_coef2)
599 {
600 uint8_t result;
601 ic_stream *ics1 = &(cpe->ics1);
602 ic_stream *ics2 = &(cpe->ics2);
603 int16_t spec_data1[1024];
604 int16_t spec_data2[1024];
605
606 #ifdef DRM
607 if (hDecoder->object_type != DRM_ER_LC)
608 #endif
609 cpe->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
610 DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag")); 616 DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
611 617 *tag = cpe.element_instance_tag;
612 if ((cpe->common_window = faad_get1bit(ld 618
619 if ((cpe.common_window = faad_get1bit(ld
613 DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1) 620 DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
614 { 621 {
615 /* both channels have common ics information */ 622 /* both channels have common ics information */
616 if ((result = ics_info(hDecoder, ics1, ld, cpe->common_window)) > 0) 623 if ((result = ics_info(hDecoder, ics1, ld, cpe.common_window)) > 0)
617 return result; 624 return result;
618 625
619 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2 626 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
620 DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present")); 627 DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
621 if (ics1->ms_mask_present == 1) 628 if (ics1->ms_mask_present == 1)
635 if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present)) 642 if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
636 { 643 {
637 if ((ics1->ltp.data_present = faad_get1bit(ld 644 if ((ics1->ltp.data_present = faad_get1bit(ld
638 DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1) 645 DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
639 { 646 {
640 ltp_data(hDecoder, ics1, &(ics1->ltp), ld); 647 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp), ld)) > 0)
648 {
649 return result;
650 }
641 } 651 }
642 } 652 }
643 #endif 653 #endif
644 654
645 memcpy(ics2, ics1, sizeof(ic_stream)); 655 memcpy(ics2, ics1, sizeof(ic_stream));
646 } else { 656 } else {
647 ics1->ms_mask_present = 0; 657 ics1->ms_mask_present = 0;
648 } 658 }
649 659
650 if ((result = individual_channel_stream(hDecoder, cpe, ld, ics1, 660 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics1,
651 0, spec_data1)) > 0) 661 0, spec_data1)) > 0)
652 { 662 {
653 return result; 663 return result;
654 } 664 }
655 665
656 #ifdef ERROR_RESILIENCE 666 #ifdef ERROR_RESILIENCE
657 if (cpe->common_window && (hDecoder->object_type >= ER_OBJECT_START) && 667 if (cpe.common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
658 (ics1->predictor_data_present)) 668 (ics1->predictor_data_present))
659 { 669 {
660 if ((ics1->ltp2.data_present = faad_get1bit(ld 670 if ((ics1->ltp2.data_present = faad_get1bit(ld
661 DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1) 671 DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
662 { 672 {
663 ltp_data(hDecoder, ics1, &(ics1->ltp2), ld); 673 if ((result = ltp_data(hDecoder, ics1, &(ics1->ltp2), ld)) > 0)
664 } 674 {
665 } 675 return result;
666 #endif 676 }
667 677 }
668 if ((result = individual_channel_stream(hDecoder, cpe, ld, ics2, 678 }
679 #endif
680
681 if ((result = individual_channel_stream(hDecoder, &cpe, ld, ics2,
669 0, spec_data2)) > 0) 682 0, spec_data2)) > 0)
670 { 683 {
671 return result; 684 return result;
672 } 685 }
673 686
674 #ifdef DRM 687 #ifdef SBR_DEC
675 if (hDecoder->object_type == DRM_ER_LC) 688 /* check if next bitstream element is a fill element */
676 { 689 /* if so, read it now so SBR decoding can be done in case of a file with SBR */
677 if (ics1->tns_data_present) 690 if (faad_showbits(ld, LEN_SE_ID) == ID_FIL)
678 tns_data(ics1, &(ics1->tns), ld); 691 {
679 692 faad_flushbits(ld, LEN_SE_ID);
680 if (ics1->tns_data_present) 693
681 tns_data(ics2, &(ics2->tns), ld); 694 /* one sbr_info describes a channel_element not a channel! */
682 695 if ((result = fill_element(hDecoder, ld, hDecoder->drc, hDecoder->fr_ch_ele)) > 0)
683 if ((result = faad_check_CRC( ld, faad_get_processed_bits(ld) - 8 )) > 0) 696 {
684 return result; 697 return result;
685
686 /* error resilient spectral data decoding */
687 if ((result = reordered_spectral_data(hDecoder, ics1, ld, spec_data1)) > 0)
688 return result;
689 if ((result = reordered_spectral_data(hDecoder, ics2, ld, spec_data2)) > 0)
690 return result;
691 /* pulse coding reconstruction */
692 if (ics1->pulse_data_present)
693 {
694 if (ics1->window_sequence != EIGHT_SHORT_SEQUENCE)
695 {
696 if ((result = pulse_decode(ics1, spec_data1, hDecoder->frameLength)) > 0)
697 return result;
698 } else {
699 return 2; /* pulse coding not allowed for short blocks */
700 }
701 }
702 if (ics2->pulse_data_present)
703 {
704 if (ics2->window_sequence != EIGHT_SHORT_SEQUENCE)
705 {
706 if ((result = pulse_decode(ics2, spec_data2, hDecoder->frameLength)) > 0)
707 return result;
708 } else {
709 return 2; /* pulse coding not allowed for short blocks */
710 }
711 } 698 }
712 } 699 }
713 #endif 700 #endif
714 701
715 /* noiseless coding is done, spectral reconstruction is done now */ 702 /* noiseless coding is done, spectral reconstruction is done now */
716 703 if ((result = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe,
717 /* inverse quantization */ 704 spec_data1, spec_data2)) > 0)
718 inverse_quantization(spec_coef1, spec_data1, hDecoder->frameLength); 705 {
719 inverse_quantization(spec_coef2, spec_data2, hDecoder->frameLength); 706 return result;
720 707 }
721 /* apply scalefactors */
722 apply_scalefactors(hDecoder, ics1, spec_coef1, hDecoder->frameLength);
723 apply_scalefactors(hDecoder, ics2, spec_coef2, hDecoder->frameLength);
724
725 /* deinterleave short block grouping */
726 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
727 quant_to_spec(ics1, spec_coef1, hDecoder->frameLength);
728 if (ics2->window_sequence == EIGHT_SHORT_SEQUENCE)
729 quant_to_spec(ics2, spec_coef2, hDecoder->frameLength);
730 708
731 return 0; 709 return 0;
732 } 710 }
733
734 static uint8_t pred_sfb_max[] =
735 {
736 33, /* 96000 */
737 33, /* 88200 */
738 38, /* 64000 */
739 40, /* 48000 */
740 40, /* 44100 */
741 40, /* 32000 */
742 41, /* 24000 */
743 41, /* 22050 */
744 37, /* 16000 */
745 37, /* 12000 */
746 37, /* 11025 */
747 34 /* 8000 */
748 };
749 711
750 /* Table 4.4.6 */ 712 /* Table 4.4.6 */
751 static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, 713 static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
752 uint8_t common_window) 714 uint8_t common_window)
753 { 715 {
787 { 749 {
788 if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */ 750 if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
789 { 751 {
790 uint8_t sfb; 752 uint8_t sfb;
791 753
792 ics->pred.limit = min(ics->max_sfb, pred_sfb_max[hDecoder->sf_index]); 754 ics->pred.limit = min(ics->max_sfb, max_pred_sfb(hDecoder->sf_index));
793 755
794 if ((ics->pred.predictor_reset = faad_get1bit(ld 756 if ((ics->pred.predictor_reset = faad_get1bit(ld
795 DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1) 757 DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
796 { 758 {
797 ics->pred.predictor_reset_group_number = (uint8_t)faad_getbits(ld, 5 759 ics->pred.predictor_reset_group_number = (uint8_t)faad_getbits(ld, 5
809 if (hDecoder->object_type < ER_OBJECT_START) 771 if (hDecoder->object_type < ER_OBJECT_START)
810 { 772 {
811 if ((ics->ltp.data_present = faad_get1bit(ld 773 if ((ics->ltp.data_present = faad_get1bit(ld
812 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1) 774 DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
813 { 775 {
814 ltp_data(hDecoder, ics, &(ics->ltp), ld); 776 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
777 {
778 return retval;
779 }
815 } 780 }
816 if (common_window) 781 if (common_window)
817 { 782 {
818 if ((ics->ltp2.data_present = faad_get1bit(ld 783 if ((ics->ltp2.data_present = faad_get1bit(ld
819 DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1) 784 DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
820 { 785 {
821 ltp_data(hDecoder, ics, &(ics->ltp2), ld); 786 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp2), ld)) > 0)
787 {
788 return retval;
789 }
822 } 790 }
823 } 791 }
824 } 792 }
825 #ifdef ERROR_RESILIENCE 793 #ifdef ERROR_RESILIENCE
826 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START)) 794 if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
863 } 831 }
864 832
865 return 0; 833 return 0;
866 } 834 }
867 835
836 #ifdef COUPLING_DEC
868 /* Table 4.4.8: Currently just for skipping the bits... */ 837 /* Table 4.4.8: Currently just for skipping the bits... */
869 static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld) 838 static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld)
870 { 839 {
871 uint8_t c, result = 0; 840 uint8_t c, result = 0;
872 uint8_t ind_sw_cce_flag = 0; 841 uint8_t ind_sw_cce_flag = 0;
873 uint8_t num_gain_element_lists = 0; 842 uint8_t num_gain_element_lists = 0;
874 uint8_t num_coupled_elements = 0; 843 uint8_t num_coupled_elements = 0;
875 844
876 element el_empty; 845 element el_empty = {0};
877 ic_stream ics_empty; 846 ic_stream ics_empty = {0};
878 int16_t sh_data[1024]; 847 int16_t sh_data[1024];
879
880 memset(&el_empty, 0, sizeof(element));
881 memset(&ics_empty, 0, sizeof(ic_stream));
882 848
883 c = faad_getbits(ld, LEN_TAG 849 c = faad_getbits(ld, LEN_TAG
884 DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag")); 850 DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
885 851
886 ind_sw_cce_flag = faad_get1bit(ld 852 ind_sw_cce_flag = faad_get1bit(ld
953 } 919 }
954 } 920 }
955 921
956 return 0; 922 return 0;
957 } 923 }
924 #endif
958 925
959 /* Table 4.4.10 */ 926 /* Table 4.4.10 */
960 static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld) 927 static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld)
961 { 928 {
962 uint8_t byte_aligned; 929 uint8_t byte_aligned;
976 if (byte_aligned) 943 if (byte_aligned)
977 faad_byte_align(ld); 944 faad_byte_align(ld);
978 945
979 for (i = 0; i < count; i++) 946 for (i = 0; i < count; i++)
980 { 947 {
981 uint8_t data = faad_getbits(ld, LEN_BYTE 948 faad_getbits(ld, LEN_BYTE
982 DEBUGVAR(1,64,"data_stream_element(): data_stream_byte")); 949 DEBUGVAR(1,64,"data_stream_element(): data_stream_byte"));
983 } 950 }
984 951
985 return count; 952 return count;
986 } 953 }
1006 } 973 }
1007 974
1008 if (count > 0) 975 if (count > 0)
1009 { 976 {
1010 #ifdef SBR_DEC 977 #ifdef SBR_DEC
1011 hDecoder->sbr_used[sbr_ele] = 0;
1012 bs_extension_type = (uint8_t)faad_showbits(ld, 4); 978 bs_extension_type = (uint8_t)faad_showbits(ld, 4);
1013 979
1014 if ((bs_extension_type == EXT_SBR_DATA) || 980 if ((bs_extension_type == EXT_SBR_DATA) ||
1015 (bs_extension_type == EXT_SBR_DATA_CRC)) 981 (bs_extension_type == EXT_SBR_DATA_CRC))
1016 { 982 {
1017 hDecoder->sbr_used[sbr_ele] = 1; 983 if (sbr_ele == INVALID_SBR_ELEMENT)
984 return 24;
1018 985
1019 if (!hDecoder->sbr[sbr_ele]) 986 if (!hDecoder->sbr[sbr_ele])
1020 { 987 {
1021 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength 988 hDecoder->sbr[sbr_ele] = sbrDecodeInit(hDecoder->frameLength,
989 hDecoder->element_id[sbr_ele], 2*get_sample_rate(hDecoder->sf_index)
1022 #ifdef DRM 990 #ifdef DRM
1023 , 0 991 , 0
1024 #endif 992 #endif
1025 ); 993 );
1026 } 994 }
1027 995
1028 /* read in all the SBR data for processing later on */ 996 hDecoder->sbr_present_flag = 1;
1029 hDecoder->sbr[sbr_ele]->data = (uint8_t*)faad_getbitbuffer(ld, count*8); 997
1030 hDecoder->sbr[sbr_ele]->data_size = count; 998 /* parse the SBR data */
999 hDecoder->sbr[sbr_ele]->ret = sbr_extension_data(ld, hDecoder->sbr[sbr_ele], count);
1000 #if (defined(PS_DEC) || defined(DRM_PS))
1001 if (hDecoder->sbr[sbr_ele]->ps_used)
1002 {
1003 hDecoder->ps_used[sbr_ele] = 1;
1004 }
1005 #endif
1031 } else { 1006 } else {
1032 hDecoder->sbr_used[sbr_ele] = 0;
1033 #endif 1007 #endif
1034 while (count > 0) 1008 while (count > 0)
1035 { 1009 {
1036 count -= extension_payload(ld, drc, count); 1010 count -= extension_payload(ld, drc, count);
1037 } 1011 }
1137 } 1111 }
1138 } 1112 }
1139 } 1113 }
1140 #endif 1114 #endif
1141 1115
1116 #ifdef SCALABLE_DEC
1117 /* Table 4.4.13 ASME */
1118 void aac_scalable_main_element(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
1119 bitfile *ld, program_config *pce, drc_info *drc)
1120 {
1121 uint8_t retval = 0;
1122 uint8_t channels = hDecoder->fr_channels = 0;
1123 uint8_t ch;
1124 uint8_t this_layer_stereo = (hDecoder->channelConfiguration > 1) ? 1 : 0;
1125 element cpe = {0};
1126 ic_stream *ics1 = &(cpe.ics1);
1127 ic_stream *ics2 = &(cpe.ics2);
1128 int16_t *spec_data;
1129 ALIGN int16_t spec_data1[1024] = {0};
1130 ALIGN int16_t spec_data2[1024] = {0};
1131
1132 hDecoder->fr_ch_ele = 0;
1133
1134 hInfo->error = aac_scalable_main_header(hDecoder, ics1, ics2, ld, this_layer_stereo);
1135 if (hInfo->error > 0)
1136 return;
1137
1138 cpe.common_window = 1;
1139 if (this_layer_stereo)
1140 cpe.ele_id = ID_CPE;
1141 else
1142 cpe.ele_id = ID_SCE;
1143
1144 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = (this_layer_stereo ? 2 : 0);
1145
1146 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1147 {
1148 ic_stream *ics;
1149 if (ch == 0)
1150 {
1151 ics = ics1;
1152 spec_data = spec_data1;
1153 } else {
1154 ics = ics2;
1155 spec_data = spec_data2;
1156 }
1157
1158 hInfo->error = individual_channel_stream(hDecoder, &cpe, ld, ics, 1, spec_data);
1159 if (hInfo->error > 0)
1160 return;
1161 }
1162
1163 #ifdef DRM
1164 #ifdef SBR_DEC
1165 /* In case of DRM we need to read the SBR info before channel reconstruction */
1166 if ((hDecoder->sbr_present_flag == 1) && (hDecoder->object_type == DRM_ER_LC))
1167 {
1168 bitfile ld_sbr = {0};
1169 uint32_t i;
1170 uint16_t count = 0;
1171 uint8_t *revbuffer;
1172 uint8_t *prevbufstart;
1173 uint8_t *pbufend;
1174
1175 /* all forward bitreading should be finished at this point */
1176 uint32_t bitsconsumed = faad_get_processed_bits(ld);
1177 uint32_t buffer_size = faad_origbitbuffer_size(ld);
1178 uint8_t *buffer = (uint8_t*)faad_origbitbuffer(ld);
1179
1180 if (bitsconsumed + 8 > buffer_size*8)
1181 {
1182 hInfo->error = 14;
1183 return;
1184 }
1185
1186 if (!hDecoder->sbr[0])
1187 {
1188 hDecoder->sbr[0] = sbrDecodeInit(hDecoder->frameLength, cpe.ele_id,
1189 2*get_sample_rate(hDecoder->sf_index), 1);
1190 }
1191
1192 /* Reverse bit reading of SBR data in DRM audio frame */
1193 revbuffer = (uint8_t*)faad_malloc(buffer_size*sizeof(uint8_t));
1194 prevbufstart = revbuffer;
1195 pbufend = &buffer[buffer_size - 1];
1196 for (i = 0; i < buffer_size; i++)
1197 *prevbufstart++ = tabFlipbits[*pbufend--];
1198
1199 /* Set SBR data */
1200 /* consider 8 bits from AAC-CRC */
1201 count = (uint16_t)bit2byte(buffer_size*8 - bitsconsumed);
1202 faad_initbits(&ld_sbr, revbuffer, count);
1203
1204 hDecoder->sbr[0]->lcstereo_flag = hDecoder->lcstereo_flag;
1205
1206 hDecoder->sbr[0]->sample_rate = get_sample_rate(hDecoder->sf_index);
1207 hDecoder->sbr[0]->sample_rate *= 2;
1208
1209 faad_getbits(&ld_sbr, 8); /* Skip 8-bit CRC */
1210
1211 hDecoder->sbr[0]->ret = sbr_extension_data(&ld_sbr, hDecoder->sbr[0], count);
1212 #if (defined(PS_DEC) || defined(DRM_PS))
1213 if (hDecoder->sbr[0]->ps_used)
1214 {
1215 hDecoder->ps_used[0] = 1;
1216 }
1217 #endif
1218
1219 /* check CRC */
1220 /* no need to check it if there was already an error */
1221 if (hDecoder->sbr[0]->ret == 0)
1222 hDecoder->sbr[0]->ret = faad_check_CRC(&ld_sbr, faad_get_processed_bits(&ld_sbr) - 8);
1223
1224 faad_endbits(&ld_sbr);
1225
1226 if (revbuffer)
1227 faad_free(revbuffer);
1228 }
1229 #endif
1230 #endif
1231
1232 if (this_layer_stereo)
1233 {
1234 hInfo->error = reconstruct_channel_pair(hDecoder, ics1, ics2, &cpe, spec_data1, spec_data2);
1235 if (hInfo->error > 0)
1236 return;
1237 } else {
1238 hInfo->error = reconstruct_single_channel(hDecoder, ics1, &cpe, spec_data1);
1239 if (hInfo->error > 0)
1240 return;
1241 }
1242
1243 /* map output channels position to internal data channels */
1244 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
1245 {
1246 /* this might be faulty when pce_set is true */
1247 hDecoder->internal_channel[channels] = channels;
1248 hDecoder->internal_channel[channels+1] = channels+1;
1249 } else {
1250 hDecoder->internal_channel[channels] = channels;
1251 }
1252
1253 hDecoder->element_id[hDecoder->fr_ch_ele] = cpe.ele_id;
1254
1255 hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
1256 hDecoder->fr_ch_ele++;
1257
1258 return;
1259 }
1260
1261 /* Table 4.4.15 */
1262 static int8_t aac_scalable_main_header(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
1263 bitfile *ld, uint8_t this_layer_stereo)
1264 {
1265 uint8_t retval = 0;
1266 uint8_t ch;
1267 ic_stream *ics;
1268
1269 /* ics1->ics_reserved_bit = */ faad_get1bit(ld
1270 DEBUGVAR(1,300,"aac_scalable_main_header(): ics_reserved_bits"));
1271 ics1->window_sequence = (uint8_t)faad_getbits(ld, 2
1272 DEBUGVAR(1,301,"aac_scalable_main_header(): window_sequence"));
1273 ics1->window_shape = faad_get1bit(ld
1274 DEBUGVAR(1,302,"aac_scalable_main_header(): window_shape"));
1275
1276 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
1277 {
1278 ics1->max_sfb = (uint8_t)faad_getbits(ld, 4
1279 DEBUGVAR(1,303,"aac_scalable_main_header(): max_sfb (short)"));
1280 ics1->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
1281 DEBUGVAR(1,304,"aac_scalable_main_header(): scale_factor_grouping"));
1282 } else {
1283 ics1->max_sfb = (uint8_t)faad_getbits(ld, 6
1284 DEBUGVAR(1,305,"aac_scalable_main_header(): max_sfb (long)"));
1285 }
1286
1287 /* get the grouping information */
1288 if ((retval = window_grouping_info(hDecoder, ics1)) > 0)
1289 return retval;
1290
1291 /* should be an error */
1292 /* check the range of max_sfb */
1293 if (ics1->max_sfb > ics1->num_swb)
1294 return 16;
1295
1296 if (this_layer_stereo)
1297 {
1298 ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
1299 DEBUGVAR(1,306,"aac_scalable_main_header(): ms_mask_present"));
1300 if (ics1->ms_mask_present == 1)
1301 {
1302 uint8_t g, sfb;
1303 for (g = 0; g < ics1->num_window_groups; g++)
1304 {
1305 for (sfb = 0; sfb < ics1->max_sfb; sfb++)
1306 {
1307 ics1->ms_used[g][sfb] = faad_get1bit(ld
1308 DEBUGVAR(1,307,"aac_scalable_main_header(): faad_get1bit"));
1309 }
1310 }
1311 }
1312
1313 memcpy(ics2, ics1, sizeof(ic_stream));
1314 } else {
1315 ics1->ms_mask_present = 0;
1316 }
1317
1318 if (0)
1319 {
1320 faad_get1bit(ld
1321 DEBUGVAR(1,308,"aac_scalable_main_header(): tns_channel_mono_layer"));
1322 }
1323
1324 for (ch = 0; ch < (this_layer_stereo ? 2 : 1); ch++)
1325 {
1326 if (ch == 0)
1327 ics = ics1;
1328 else
1329 ics = ics2;
1330
1331 if ( 1 /*!tvq_layer_pesent || (tns_aac_tvq_en[ch] == 1)*/)
1332 {
1333 if ((ics->tns_data_present = faad_get1bit(ld
1334 DEBUGVAR(1,309,"aac_scalable_main_header(): tns_data_present"))) & 1)
1335 {
1336 #ifdef DRM
1337 /* different order of data units in DRM */
1338 if (hDecoder->object_type != DRM_ER_LC)
1339 #endif
1340 {
1341 tns_data(ics, &(ics->tns), ld);
1342 }
1343 }
1344 }
1345 #if 0
1346 if (0 /*core_flag || tvq_layer_pesent*/)
1347 {
1348 if ((ch==0) || ((ch==1) && (core_stereo || tvq_stereo))
1349 diff_control_data();
1350 if (mono_stereo_flag)
1351 diff_control_data_lr();
1352 } else {
1353 #endif
1354 if ((ics->ltp.data_present = faad_get1bit(ld
1355 DEBUGVAR(1,310,"aac_scalable_main_header(): ltp.data_present"))) & 1)
1356 {
1357 if ((retval = ltp_data(hDecoder, ics, &(ics->ltp), ld)) > 0)
1358 {
1359 return retval;
1360 }
1361 }
1362 #if 0
1363 }
1364 #endif
1365 }
1366
1367 return 0;
1368 }
1369 #endif
1370
1142 /* Table 4.4.24 */ 1371 /* Table 4.4.24 */
1143 static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele, 1372 static uint8_t individual_channel_stream(faacDecHandle hDecoder, element *ele,
1144 bitfile *ld, ic_stream *ics, uint8_t scal_flag, 1373 bitfile *ld, ic_stream *ics, uint8_t scal_flag,
1145 int16_t *spec_data) 1374 int16_t *spec_data)
1146 { 1375 {
1227 { 1456 {
1228 if ((result = rvlc_decode_scale_factors(ics, ld)) > 0) 1457 if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
1229 return result; 1458 return result;
1230 } 1459 }
1231 1460
1232 #ifdef DRM
1233 if (hDecoder->object_type == DRM_ER_LC)
1234 return 0;
1235 #endif
1236
1237 if (hDecoder->object_type >= ER_OBJECT_START) 1461 if (hDecoder->object_type >= ER_OBJECT_START)
1238 { 1462 {
1239 if (ics->tns_data_present) 1463 if (ics->tns_data_present)
1240 tns_data(ics, &(ics->tns), ld); 1464 tns_data(ics, &(ics->tns), ld);
1241 } 1465 }
1466
1467 #ifdef DRM
1468 /* CRC check */
1469 if (hDecoder->object_type == DRM_ER_LC)
1470 if ((result = faad_check_CRC(ld, faad_get_processed_bits(ld) - 8)) > 0)
1471 return result;
1472 #endif
1242 1473
1243 if (hDecoder->aacSpectralDataResilienceFlag) 1474 if (hDecoder->aacSpectralDataResilienceFlag)
1244 { 1475 {
1245 /* error resilient spectral data decoding */ 1476 /* error resilient spectral data decoding */
1246 if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0) 1477 if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
1414 case INTENSITY_HCB: /* intensity books */ 1645 case INTENSITY_HCB: /* intensity books */
1415 case INTENSITY_HCB2: 1646 case INTENSITY_HCB2:
1416 1647
1417 /* decode intensity position */ 1648 /* decode intensity position */
1418 t = huffman_scale_factor(ld); 1649 t = huffman_scale_factor(ld);
1419 if (t < 0)
1420 return 9;
1421 is_position += (t - 60); 1650 is_position += (t - 60);
1422 ics->scale_factors[g][sfb] = is_position; 1651 ics->scale_factors[g][sfb] = is_position;
1423 1652
1424 break; 1653 break;
1425 case NOISE_HCB: /* noise books */ 1654 case NOISE_HCB: /* noise books */
1430 noise_pcm_flag = 0; 1659 noise_pcm_flag = 0;
1431 t = (int16_t)faad_getbits(ld, 9 1660 t = (int16_t)faad_getbits(ld, 9
1432 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256; 1661 DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
1433 } else { 1662 } else {
1434 t = huffman_scale_factor(ld); 1663 t = huffman_scale_factor(ld);
1435 if (t < 0)
1436 return 9;
1437 t -= 60; 1664 t -= 60;
1438 } 1665 }
1439 noise_energy += t; 1666 noise_energy += t;
1440 ics->scale_factors[g][sfb] = noise_energy; 1667 ics->scale_factors[g][sfb] = noise_energy;
1441 1668
1442 break; 1669 break;
1443 default: /* spectral books */ 1670 default: /* spectral books */
1444 1671
1672 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
1673
1674 ics->scale_factors[g][sfb] = 0;
1675
1445 /* decode scale factor */ 1676 /* decode scale factor */
1446 t = huffman_scale_factor(ld); 1677 t = huffman_scale_factor(ld);
1447 if (t < 0)
1448 return 9;
1449 scale_factor += (t - 60); 1678 scale_factor += (t - 60);
1450 if (scale_factor < 0) 1679 if (scale_factor < 0 || scale_factor > 255)
1451 return 4; 1680 return 4;
1452 ics->scale_factors[g][sfb] = scale_factor; 1681 ics->scale_factors[g][sfb] = scale_factor;
1453 1682
1454 break; 1683 break;
1455 } 1684 }
1460 } 1689 }
1461 1690
1462 /* Table 4.4.26 */ 1691 /* Table 4.4.26 */
1463 static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld) 1692 static uint8_t scale_factor_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld)
1464 { 1693 {
1694 uint8_t ret = 0;
1695 #ifdef PROFILE
1696 int64_t count = faad_get_ts();
1697 #endif
1698
1465 #ifdef ERROR_RESILIENCE 1699 #ifdef ERROR_RESILIENCE
1466 if (!hDecoder->aacScalefactorDataResilienceFlag) 1700 if (!hDecoder->aacScalefactorDataResilienceFlag)
1467 { 1701 {
1468 #endif 1702 #endif
1469 return decode_scale_factors(ics, ld); 1703 ret = decode_scale_factors(ics, ld);
1470 #ifdef ERROR_RESILIENCE 1704 #ifdef ERROR_RESILIENCE
1471 } else { 1705 } else {
1472 /* In ER AAC the parameters for RVLC are seperated from the actual 1706 /* In ER AAC the parameters for RVLC are seperated from the actual
1473 data that holds the scale_factors. 1707 data that holds the scale_factors.
1474 Strangely enough, 2 parameters for HCR are put inbetween them. 1708 Strangely enough, 2 parameters for HCR are put inbetween them.
1475 */ 1709 */
1476 return rvlc_scale_factor_data(ics, ld); 1710 ret = rvlc_scale_factor_data(ics, ld);
1477 } 1711 }
1478 #endif 1712 #endif
1713
1714 #ifdef PROFILE
1715 count = faad_get_ts() - count;
1716 hDecoder->scalefac_cycles += count;
1717 #endif
1718
1719 return ret;
1479 } 1720 }
1480 1721
1481 /* Table 4.4.27 */ 1722 /* Table 4.4.27 */
1482 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld) 1723 static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld)
1483 { 1724 {
1533 } 1774 }
1534 } 1775 }
1535 1776
1536 #ifdef LTP_DEC 1777 #ifdef LTP_DEC
1537 /* Table 4.4.28 */ 1778 /* Table 4.4.28 */
1538 static void ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld) 1779 static uint8_t ltp_data(faacDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld)
1539 { 1780 {
1540 uint8_t sfb, w; 1781 uint8_t sfb, w;
1782
1783 ltp->lag = 0;
1541 1784
1542 #ifdef LD_DEC 1785 #ifdef LD_DEC
1543 if (hDecoder->object_type == LD) 1786 if (hDecoder->object_type == LD)
1544 { 1787 {
1545 ltp->lag_update = (uint8_t)faad_getbits(ld, 1 1788 ltp->lag_update = (uint8_t)faad_getbits(ld, 1
1555 ltp->lag = (uint16_t)faad_getbits(ld, 11 1798 ltp->lag = (uint16_t)faad_getbits(ld, 11
1556 DEBUGVAR(1,81,"ltp_data(): lag")); 1799 DEBUGVAR(1,81,"ltp_data(): lag"));
1557 #ifdef LD_DEC 1800 #ifdef LD_DEC
1558 } 1801 }
1559 #endif 1802 #endif
1803
1804 /* Check length of lag */
1805 if (ltp->lag > (hDecoder->frameLength << 1))
1806 return 18;
1807
1560 ltp->coef = (uint8_t)faad_getbits(ld, 3 1808 ltp->coef = (uint8_t)faad_getbits(ld, 3
1561 DEBUGVAR(1,82,"ltp_data(): coef")); 1809 DEBUGVAR(1,82,"ltp_data(): coef"));
1562 1810
1563 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) 1811 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
1564 { 1812 {
1583 { 1831 {
1584 ltp->long_used[sfb] = faad_get1bit(ld 1832 ltp->long_used[sfb] = faad_get1bit(ld
1585 DEBUGVAR(1,86,"ltp_data(): long_used")); 1833 DEBUGVAR(1,86,"ltp_data(): long_used"));
1586 } 1834 }
1587 } 1835 }
1836
1837 return 0;
1588 } 1838 }
1589 #endif 1839 #endif
1590 1840
1591 /* Table 4.4.29 */ 1841 /* Table 4.4.29 */
1592 static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld, 1842 static uint8_t spectral_data(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
1593 int16_t *spectral_data) 1843 int16_t *spectral_data)
1594 { 1844 {
1595 int8_t i; 1845 int8_t i;
1596 uint8_t g; 1846 uint8_t g;
1597 int16_t *sp; 1847 uint16_t inc, k, p = 0;
1598 uint16_t k, p = 0;
1599 uint8_t groups = 0; 1848 uint8_t groups = 0;
1600 uint8_t sect_cb; 1849 uint8_t sect_cb;
1601 uint8_t result; 1850 uint8_t result;
1602 uint16_t nshort = hDecoder->frameLength/8; 1851 uint16_t nshort = hDecoder->frameLength/8;
1603 1852
1604 sp = spectral_data; 1853 #ifdef PROFILE
1605 memset(sp, 0, hDecoder->frameLength*sizeof(int16_t)); 1854 int64_t count = faad_get_ts();
1855 #endif
1606 1856
1607 for(g = 0; g < ics->num_window_groups; g++) 1857 for(g = 0; g < ics->num_window_groups; g++)
1608 { 1858 {
1609 p = groups*nshort; 1859 p = groups*nshort;
1610 1860
1611 for (i = 0; i < ics->num_sec[g]; i++) 1861 for (i = 0; i < ics->num_sec[g]; i++)
1612 { 1862 {
1613 sect_cb = ics->sect_cb[g][i]; 1863 sect_cb = ics->sect_cb[g][i];
1864
1865 inc = (sect_cb >= FIRST_PAIR_HCB) ? 2 : 4;
1614 1866
1615 switch (sect_cb) 1867 switch (sect_cb)
1616 { 1868 {
1617 case ZERO_HCB: 1869 case ZERO_HCB:
1618 case NOISE_HCB: 1870 case NOISE_HCB:
1621 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] - 1873 p += (ics->sect_sfb_offset[g][ics->sect_end[g][i]] -
1622 ics->sect_sfb_offset[g][ics->sect_start[g][i]]); 1874 ics->sect_sfb_offset[g][ics->sect_start[g][i]]);
1623 break; 1875 break;
1624 default: 1876 default:
1625 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]]; 1877 for (k = ics->sect_sfb_offset[g][ics->sect_start[g][i]];
1626 k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += 4) 1878 k < ics->sect_sfb_offset[g][ics->sect_end[g][i]]; k += inc)
1627 { 1879 {
1628 sp = spectral_data + p; 1880 if ((result = huffman_spectral_data(sect_cb, ld, &spectral_data[p])) > 0)
1629 1881 return result;
1630 if ((result = huffman_spectral_data(sect_cb, ld, sp)) > 0) 1882 p += inc;
1631 return result;
1632 if (sect_cb >= FIRST_PAIR_HCB)
1633 {
1634 if ((result = huffman_spectral_data(sect_cb, ld, sp+2)) > 0)
1635 return result;
1636 }
1637 p += 4;
1638 } 1883 }
1639 break; 1884 break;
1640 } 1885 }
1641 } 1886 }
1642 groups += ics->window_group_length[g]; 1887 groups += ics->window_group_length[g];
1643 } 1888 }
1889
1890 #ifdef PROFILE
1891 count = faad_get_ts() - count;
1892 hDecoder->spectral_cycles += count;
1893 #endif
1644 1894
1645 return 0; 1895 return 0;
1646 } 1896 }
1647 1897
1648 /* Table 4.4.30 */ 1898 /* Table 4.4.30 */
1669 /* fill_byte[i] = */ faad_getbits(ld, 8 1919 /* fill_byte[i] = */ faad_getbits(ld, 8
1670 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ‘10100101’ */ 1920 DEBUGVAR(1,88,"extension_payload(): fill_byte")); /* must be ‘10100101’ */
1671 } 1921 }
1672 return count; 1922 return count;
1673 case EXT_DATA_ELEMENT: 1923 case EXT_DATA_ELEMENT:
1674 data_element_version = faad_getbits(ld, 4 1924 data_element_version = (uint8_t)faad_getbits(ld, 4
1675 DEBUGVAR(1,400,"extension_payload(): data_element_version")); 1925 DEBUGVAR(1,400,"extension_payload(): data_element_version"));
1676 switch (data_element_version) 1926 switch (data_element_version)
1677 { 1927 {
1678 case ANC_DATA: 1928 case ANC_DATA:
1679 loopCounter = 0; 1929 loopCounter = 0;
1680 dataElementLength = 0; 1930 dataElementLength = 0;
1681 do { 1931 do {
1682 dataElementLengthPart = faad_getbits(ld, 8 1932 dataElementLengthPart = (uint8_t)faad_getbits(ld, 8
1683 DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart")); 1933 DEBUGVAR(1,401,"extension_payload(): dataElementLengthPart"));
1684 dataElementLength += dataElementLengthPart; 1934 dataElementLength += dataElementLengthPart;
1685 loopCounter++; 1935 loopCounter++;
1686 } while (dataElementLengthPart == 255); 1936 } while (dataElementLengthPart == 255);
1687 1937
1907 DEBUGVAR(1,127,"adts_fixed_header(): home")); 2157 DEBUGVAR(1,127,"adts_fixed_header(): home"));
1908 2158
1909 if (adts->old_format == 1) 2159 if (adts->old_format == 1)
1910 { 2160 {
1911 /* Removed in corrigendum 14496-3:2002 */ 2161 /* Removed in corrigendum 14496-3:2002 */
1912 if (adts->id == 0) 2162 if (adts->id == 0)
1913 { 2163 {
1914 adts->emphasis = (uint8_t)faad_getbits(ld, 2 2164 adts->emphasis = (uint8_t)faad_getbits(ld, 2
1915 DEBUGVAR(1,128,"adts_fixed_header(): emphasis")); 2165 DEBUGVAR(1,128,"adts_fixed_header(): emphasis"));
1916 } 2166 }
1917 } 2167 }
1918 2168
1919 return 0; 2169 return 0;
1920 } 2170 }
1921 2171