summaryrefslogtreecommitdiffstats
path: root/libfaad2/syntax.c
diff options
context:
space:
mode:
authorarpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2>2003-08-30 22:30:28 +0000
committerarpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2>2003-08-30 22:30:28 +0000
commit32063c433915b8dddd143a951ad90ae901ac1b38 (patch)
tree88aaee983b0885b5bb22d870476f7afdaa8a7010 /libfaad2/syntax.c
parent264633eec589baddfdcd79dde08fd7f1f47fba51 (diff)
downloadmpv-32063c433915b8dddd143a951ad90ae901ac1b38.tar.bz2
mpv-32063c433915b8dddd143a951ad90ae901ac1b38.tar.xz
libfaad2 v2.0rc1 imported
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@10726 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'libfaad2/syntax.c')
-rw-r--r--libfaad2/syntax.c1960
1 files changed, 1960 insertions, 0 deletions
diff --git a/libfaad2/syntax.c b/libfaad2/syntax.c
new file mode 100644
index 0000000000..33c3a2ba66
--- /dev/null
+++ b/libfaad2/syntax.c
@@ -0,0 +1,1960 @@
+/*
+** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
+** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation; either version 2 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+**
+** Any non-GPL usage of this software or parts of this software is strictly
+** forbidden.
+**
+** Commercial non-GPL licensing of this software is possible.
+** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
+**
+** $Id: syntax.c,v 1.51 2003/07/29 08:20:14 menno Exp $
+**/
+
+/*
+ Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
+*/
+
+#include "common.h"
+#include "structs.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "decoder.h"
+#include "syntax.h"
+#include "specrec.h"
+#include "huffman.h"
+#include "bits.h"
+#include "pulse.h"
+#include "analysis.h"
+#include "drc.h"
+#ifdef ERROR_RESILIENCE
+#include "rvlc.h"
+#endif
+#ifdef SBR_DEC
+#include "sbr_syntax.h"
+#endif
+
+
+/* Table 4.4.1 */
+int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
+ program_config *pce_out)
+{
+ program_config pce;
+
+ /* 1024 or 960 */
+ mp4ASC->frameLengthFlag = faad_get1bit(ld
+ DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
+
+ mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
+ DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
+ if (mp4ASC->dependsOnCoreCoder == 1)
+ {
+ mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
+ DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
+ }
+
+ mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
+ if (mp4ASC->channelsConfiguration == 0)
+ {
+ program_config_element(&pce, ld);
+ //mp4ASC->channelsConfiguration = pce.channels;
+
+ if (pce_out != NULL)
+ memcpy(pce_out, &pce, sizeof(program_config));
+
+ /*
+ if (pce.num_valid_cc_elements)
+ return -3;
+ */
+ }
+
+#ifdef ERROR_RESILIENCE
+ if (mp4ASC->extensionFlag == 1)
+ {
+ /* Error resilience not supported yet */
+ if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
+ {
+ mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
+ DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
+ mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
+ DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
+ mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
+ DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
+
+ /* 1 bit: extensionFlag3 */
+ }
+ }
+#endif
+
+ return 0;
+}
+
+/* Table 4.4.2 */
+/* An MPEG-4 Audio decoder is only required to follow the Program
+ Configuration Element in GASpecificConfig(). The decoder shall ignore
+ any Program Configuration Elements that may occur in raw data blocks.
+ PCEs transmitted in raw data blocks cannot be used to convey decoder
+ configuration information.
+*/
+uint8_t program_config_element(program_config *pce, bitfile *ld)
+{
+ uint8_t i;
+
+ memset(pce, 0, sizeof(program_config));
+
+ pce->channels = 0;
+
+ pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
+
+ pce->object_type = (uint8_t)faad_getbits(ld, 2
+ DEBUGVAR(1,11,"program_config_element(): object_type"));
+ pce->sf_index = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,12,"program_config_element(): sf_index"));
+ pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
+ pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
+ pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
+ pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
+ DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
+ pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
+ DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
+ pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
+
+ pce->mono_mixdown_present = faad_get1bit(ld
+ DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
+ if (pce->mono_mixdown_present == 1)
+ {
+ pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
+ }
+
+ pce->stereo_mixdown_present = faad_get1bit(ld
+ DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
+ if (pce->stereo_mixdown_present == 1)
+ {
+ pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
+ }
+
+ pce->matrix_mixdown_idx_present = faad_get1bit(ld
+ DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
+ if (pce->matrix_mixdown_idx_present == 1)
+ {
+ pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
+ DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
+ pce->pseudo_surround_enable = faad_get1bit(ld
+ DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
+ }
+
+ for (i = 0; i < pce->num_front_channel_elements; i++)
+ {
+ pce->front_element_is_cpe[i] = faad_get1bit(ld
+ DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
+ pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
+
+ if (pce->front_element_is_cpe[i] & 1)
+ {
+ pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
+ pce->num_front_channels += 2;
+ pce->channels += 2;
+ } else {
+ pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
+ pce->num_front_channels++;
+ pce->channels++;
+ }
+ }
+
+ for (i = 0; i < pce->num_side_channel_elements; i++)
+ {
+ pce->side_element_is_cpe[i] = faad_get1bit(ld
+ DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
+ pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
+
+ if (pce->side_element_is_cpe[i] & 1)
+ {
+ pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
+ pce->num_side_channels += 2;
+ pce->channels += 2;
+ } else {
+ pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
+ pce->num_side_channels++;
+ pce->channels++;
+ }
+ }
+
+ for (i = 0; i < pce->num_back_channel_elements; i++)
+ {
+ pce->back_element_is_cpe[i] = faad_get1bit(ld
+ DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
+ pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
+
+ if (pce->back_element_is_cpe[i] & 1)
+ {
+ pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
+ pce->channels += 2;
+ pce->num_back_channels += 2;
+ } else {
+ pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
+ pce->num_back_channels++;
+ pce->channels++;
+ }
+ }
+
+ for (i = 0; i < pce->num_lfe_channel_elements; i++)
+ {
+ pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
+
+ pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
+ pce->num_lfe_channels++;
+ pce->channels++;
+ }
+
+ for (i = 0; i < pce->num_assoc_data_elements; i++)
+ pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
+
+ for (i = 0; i < pce->num_valid_cc_elements; i++)
+ {
+ pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
+ DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
+ pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
+ }
+
+ faad_byte_align(ld);
+
+ pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
+ DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
+
+ for (i = 0; i < pce->comment_field_bytes; i++)
+ {
+ pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
+ DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
+ }
+ pce->comment_field_data[i] = 0;
+
+ return 0;
+}
+
+element *decode_sce_lfe(faacDecHandle hDecoder,
+ faacDecFrameInfo *hInfo, bitfile *ld,
+ int16_t **spec_data, real_t **spec_coef,
+ uint8_t id_syn_ele)
+{
+ element *ele;
+ uint8_t channels = hDecoder->fr_channels;
+
+ if (channels+1 > MAX_CHANNELS)
+ {
+ hInfo->error = 12;
+ return NULL;
+ }
+ if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
+ {
+ hInfo->error = 13;
+ return NULL;
+ }
+
+ spec_data[channels] = (int16_t*)malloc(hDecoder->frameLength*sizeof(int16_t));
+ spec_coef[channels] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t));
+
+ ele = (element*)malloc(sizeof(element));
+ memset(ele, 0, sizeof(element));
+ ele->ele_id = id_syn_ele;
+ ele->channel = channels;
+ ele->paired_channel = -1;
+
+ hInfo->error = single_lfe_channel_element(hDecoder, ele,
+ ld, spec_data[channels]);
+
+ if (hDecoder->pce_set)
+ hDecoder->internal_channel[hDecoder->pce.sce_channel[ele->element_instance_tag]] = channels;
+ else
+ hDecoder->internal_channel[channels] = channels;
+
+ if (id_syn_ele == ID_SCE)
+ hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
+ else /* LFE */
+ hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
+
+ hDecoder->fr_channels++;
+ hDecoder->fr_ch_ele++;
+
+ return ele;
+}
+
+element *decode_cpe(faacDecHandle hDecoder,
+ faacDecFrameInfo *hInfo, bitfile *ld,
+ int16_t **spec_data, real_t **spec_coef,
+ uint8_t id_syn_ele)
+{
+ element *ele;
+ uint8_t channels = hDecoder->fr_channels;
+
+ if (channels+2 > MAX_CHANNELS)
+ {
+ hInfo->error = 12;
+ return NULL;
+ }
+ if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
+ {
+ hInfo->error = 13;
+ return NULL;
+ }
+
+ spec_data[channels] = (int16_t*)malloc(hDecoder->frameLength*sizeof(int16_t));
+ spec_data[channels+1] = (int16_t*)malloc(hDecoder->frameLength*sizeof(int16_t));
+ spec_coef[channels] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t));
+ spec_coef[channels+1] = (real_t*)malloc(hDecoder->frameLength*sizeof(real_t));
+
+ ele = (element*)malloc(sizeof(element));
+ memset(ele, 0, sizeof(element));
+ ele->ele_id = id_syn_ele;
+ ele->channel = channels;
+ ele->paired_channel = channels+1;
+
+ hInfo->error = channel_pair_element(hDecoder, ele,
+ ld, spec_data[channels], spec_data[channels+1]);
+
+ if (hDecoder->pce_set)
+ {
+ hDecoder->internal_channel[hDecoder->pce.cpe_channel[ele->element_instance_tag]] = channels;
+ hDecoder->internal_channel[hDecoder->pce.cpe_channel[ele->element_instance_tag]+1] = channels+1;
+ } else {
+ hDecoder->internal_channel[channels] = channels;
+ hDecoder->internal_channel[channels+1] = channels+1;
+ }
+
+ hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
+ hDecoder->channel_element[channels+1] = hDecoder->fr_ch_ele;
+
+ hDecoder->fr_channels += 2;
+ hDecoder->fr_ch_ele++;
+
+ return ele;
+}
+
+element **raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
+ bitfile *ld, element **elements,
+ int16_t **spec_data, real_t **spec_coef,
+ program_config *pce, drc_info *drc)
+{
+ uint8_t id_syn_ele;
+ uint8_t ch_ele = 0;
+
+ hDecoder->fr_channels = 0;
+ hDecoder->fr_ch_ele = 0;
+ hDecoder->first_syn_ele = 25;
+ hDecoder->has_lfe = 0;
+
+#ifdef ERROR_RESILIENCE
+ if (hDecoder->object_type < ER_OBJECT_START)
+ {
+#endif
+ /* Table 4.4.3: raw_data_block() */
+ while ((id_syn_ele = (uint8_t)faad_getbits(ld, LEN_SE_ID
+ DEBUGVAR(1,4,"faacDecDecode(): id_syn_ele"))) != ID_END)
+ {
+ switch (id_syn_ele) {
+ case ID_SCE:
+ if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
+ hDecoder->last_syn_ele = id_syn_ele;
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, id_syn_ele);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case ID_CPE:
+ if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
+ hDecoder->last_syn_ele = id_syn_ele;
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, id_syn_ele);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case ID_LFE:
+ hDecoder->has_lfe++;
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, id_syn_ele);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case ID_CCE: /* not implemented yet, but skip the bits */
+ hInfo->error = coupling_channel_element(hDecoder, ld);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case ID_DSE:
+ data_stream_element(hDecoder, ld);
+ break;
+ case ID_PCE:
+ if ((hInfo->error = program_config_element(pce, ld)) > 0)
+ return elements;
+ hDecoder->pce_set = 1;
+ break;
+ case ID_FIL:
+ /* one sbr_info describes a channel_element not a channel! */
+ if ((hInfo->error = fill_element(hDecoder, ld, drc
+#ifdef SBR_DEC
+ , (ch_ele-1)
+#endif
+ )) > 0)
+ return elements;
+#ifdef SBR_DEC
+ if (hDecoder->sbr_used[ch_ele-1])
+ {
+ hDecoder->sbr_present_flag = 1;
+ hDecoder->sbr[ch_ele-1]->sample_rate = sample_rates[hDecoder->sf_index];
+ hDecoder->sbr[ch_ele-1]->sample_rate *= 2;
+ }
+#endif
+ break;
+ }
+ }
+#ifdef ERROR_RESILIENCE
+ } else {
+ /* Table 262: er_raw_data_block() */
+ switch (hDecoder->channelConfiguration)
+ {
+ case 1:
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case 2:
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case 3:
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case 4:
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case 5:
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case 6:
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_LFE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ case 7:
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_SCE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_cpe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_CPE);
+ elements[ch_ele++] = decode_sce_lfe(hDecoder,
+ hInfo, ld, spec_data, spec_coef, ID_LFE);
+ if (hInfo->error > 0)
+ return elements;
+ break;
+ default:
+ hInfo->error = 7;
+ return elements;
+ }
+#if 0
+ cnt = bits_to_decode() / 8;
+ while (cnt >= 1)
+ {
+ cnt -= extension_payload(cnt);
+ }
+#endif
+ }
+#endif
+
+ /* new in corrigendum 14496-3:2002 */
+ faad_byte_align(ld);
+
+ return elements;
+}
+
+#ifdef DRM
+static uint8_t faad_check_CRC(bitfile *ld)
+{
+ uint16_t len = faad_get_processed_bits(ld) - 8;
+ uint8_t CRC;
+ uint16_t r=255; /* Initialize to all ones */
+
+ /* CRC polynome used x^8 + x^4 + x^3 + x^2 +1 */
+#define GPOLY 0435
+
+ faad_rewindbits(ld);
+
+ CRC = ~faad_getbits(ld, 8
+ DEBUGVAR(1,999,"faad_check_CRC(): CRC")); /* CRC is stored inverted */
+
+ for (; len>0; len--)
+ {
+ r = ( (r << 1) ^ (( ( faad_get1bit(ld
+ DEBUGVAR(1,998,"")) & 1) ^ ((r >> 7) & 1)) * GPOLY )) & 0xFF;
+ }
+ if (r != CRC)
+ {
+ return 8;
+ } else {
+ return 0;
+ }
+}
+#endif
+
+/* Table 4.4.4 and */
+/* Table 4.4.9 */
+static uint8_t single_lfe_channel_element(faacDecHandle hDecoder,
+ element *sce, bitfile *ld,
+ int16_t *spec_data)
+{
+ ic_stream *ics = &(sce->ics1);
+#ifdef DRM
+ uint8_t result;
+
+ if (hDecoder->object_type != DRM_ER_LC)
+#endif
+ sce->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
+ DEBUGVAR(1,38,"single_lfe_channel_element(): element_instance_tag"));
+
+#ifdef DRM
+ if (hDecoder->object_type == DRM_ER_LC)
+ {
+ individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data);
+
+ if (ics->tns_data_present)
+ tns_data(ics, &(ics->tns), ld);
+
+ if ((result = faad_check_CRC( ld )) > 0)
+ return result;
+
+ /* error resilient spectral data decoding */
+ if ((result = reordered_spectral_data(hDecoder, ics, ld, spec_data)) > 0)
+ return result;
+
+ /* pulse coding reconstruction */
+ if (ics->pulse_data_present)
+ {
+ if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
+ {
+ if ((result = pulse_decode(ics, spec_data, hDecoder->frameLength)) > 0)
+ return result;
+ } else {
+ return 2; /* pulse coding not allowed for short blocks */
+ }
+ }
+ return 0;
+ } else
+#endif
+
+ return individual_channel_stream(hDecoder, sce, ld, ics, 0, spec_data);
+}
+
+/* Table 4.4.5 */
+static uint8_t channel_pair_element(faacDecHandle hDecoder, element *cpe,
+ bitfile *ld, int16_t *spec_data1,
+ int16_t *spec_data2)
+{
+ uint8_t result;
+ ic_stream *ics1 = &(cpe->ics1);
+ ic_stream *ics2 = &(cpe->ics2);
+
+#ifdef DRM
+ if (hDecoder->object_type != DRM_ER_LC)
+#endif
+ cpe->element_instance_tag = (uint8_t)faad_getbits(ld, LEN_TAG
+ DEBUGVAR(1,39,"channel_pair_element(): element_instance_tag"));
+
+ if ((cpe->common_window = faad_get1bit(ld
+ DEBUGVAR(1,40,"channel_pair_element(): common_window"))) & 1)
+ {
+ /* both channels have common ics information */
+ if ((result = ics_info(hDecoder, ics1, ld, cpe->common_window)) > 0)
+ return result;
+
+ ics1->ms_mask_present = (uint8_t)faad_getbits(ld, 2
+ DEBUGVAR(1,41,"channel_pair_element(): ms_mask_present"));
+ if (ics1->ms_mask_present == 1)
+ {
+ uint8_t g, sfb;
+ for (g = 0; g < ics1->num_window_groups; g++)
+ {
+ for (sfb = 0; sfb < ics1->max_sfb; sfb++)
+ {
+ ics1->ms_used[g][sfb] = faad_get1bit(ld
+ DEBUGVAR(1,42,"channel_pair_element(): faad_get1bit"));
+ }
+ }
+ }
+
+#ifdef ERROR_RESILIENCE
+ if ((hDecoder->object_type >= ER_OBJECT_START) && (ics1->predictor_data_present))
+ {
+ if ((ics1->ltp.data_present = faad_get1bit(ld
+ DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
+ {
+ ltp_data(hDecoder, ics1, &(ics1->ltp), ld);
+ }
+ }
+#endif
+
+ memcpy(ics2, ics1, sizeof(ic_stream));
+ } else {
+ ics1->ms_mask_present = 0;
+ }
+
+ if ((result = individual_channel_stream(hDecoder, cpe, ld, ics1,
+ 0, spec_data1)) > 0)
+ {
+ return result;
+ }
+
+#ifdef ERROR_RESILIENCE
+ if (cpe->common_window && (hDecoder->object_type >= ER_OBJECT_START) &&
+ (ics1->predictor_data_present))
+ {
+ if ((ics1->ltp2.data_present = faad_get1bit(ld
+ DEBUGVAR(1,50,"channel_pair_element(): ltp.data_present"))) & 1)
+ {
+ ltp_data(hDecoder, ics1, &(ics1->ltp2), ld);
+ }
+ }
+#endif
+
+ if ((result = individual_channel_stream(hDecoder, cpe, ld, ics2,
+ 0, spec_data2)) > 0)
+ {
+ return result;
+ }
+
+#ifdef DRM
+ if (hDecoder->object_type == DRM_ER_LC)
+ {
+ if (ics1->tns_data_present)
+ tns_data(ics1, &(ics1->tns), ld);
+
+ if (ics1->tns_data_present)
+ tns_data(ics2, &(ics2->tns), ld);
+
+ if ((result = faad_check_CRC( ld )) > 0)
+ {
+ return result;
+ }
+ /* error resilient spectral data decoding */
+ if ((result = reordered_spectral_data(hDecoder, ics1, ld, spec_data1)) > 0)
+ return result;
+ if ((result = reordered_spectral_data(hDecoder, ics2, ld, spec_data2)) > 0)
+ return result;
+ /* pulse coding reconstruction */
+ if (ics1->pulse_data_present)
+ {
+ if (ics1->window_sequence != EIGHT_SHORT_SEQUENCE)
+ {
+ if ((result = pulse_decode(ics1, spec_data1, hDecoder->frameLength)) > 0)
+ return result;
+ } else {
+ return 2; /* pulse coding not allowed for short blocks */
+ }
+ }
+ if (ics2->pulse_data_present)
+ {
+ if (ics2->window_sequence != EIGHT_SHORT_SEQUENCE)
+ {
+ if ((result = pulse_decode(ics2, spec_data2, hDecoder->frameLength)) > 0)
+ return result;
+ } else {
+ return 2; /* pulse coding not allowed for short blocks */
+ }
+ }
+ return 0;
+ } else
+#endif
+
+ return 0;
+}
+
+static uint8_t pred_sfb_max[] =
+{
+ 33, /* 96000 */
+ 33, /* 88200 */
+ 38, /* 64000 */
+ 40, /* 48000 */
+ 40, /* 44100 */
+ 40, /* 32000 */
+ 41, /* 24000 */
+ 41, /* 22050 */
+ 37, /* 16000 */
+ 37, /* 12000 */
+ 37, /* 11025 */
+ 34 /* 8000 */
+};
+
+/* Table 4.4.6 */
+static uint8_t ics_info(faacDecHandle hDecoder, ic_stream *ics, bitfile *ld,
+ uint8_t common_window)
+{
+ uint8_t retval = 0;
+
+ /* ics->ics_reserved_bit = */ faad_get1bit(ld
+ DEBUGVAR(1,43,"ics_info(): ics_reserved_bit"));
+ ics->window_sequence = (uint8_t)faad_getbits(ld, 2
+ DEBUGVAR(1,44,"ics_info(): window_sequence"));
+ ics->window_shape = faad_get1bit(ld
+ DEBUGVAR(1,45,"ics_info(): window_shape"));
+
+ if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
+ {
+ ics->max_sfb = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,46,"ics_info(): max_sfb (short)"));
+ ics->scale_factor_grouping = (uint8_t)faad_getbits(ld, 7
+ DEBUGVAR(1,47,"ics_info(): scale_factor_grouping"));
+ } else {
+ ics->max_sfb = (uint8_t)faad_getbits(ld, 6
+ DEBUGVAR(1,48,"ics_info(): max_sfb (long)"));
+ }
+
+ /* get the grouping information */
+ if ((retval = window_grouping_info(hDecoder, ics)) > 0)
+ return retval;
+
+ /* should be an error */
+ /* check the range of max_sfb */
+ if (ics->max_sfb > ics->num_swb)
+ return 16;
+
+ if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
+ {
+ if ((ics->predictor_data_present = faad_get1bit(ld
+ DEBUGVAR(1,49,"ics_info(): predictor_data_present"))) & 1)
+ {
+ if (hDecoder->object_type == MAIN) /* MPEG2 style AAC predictor */
+ {
+ uint8_t sfb;
+
+ ics->pred.limit = min(ics->max_sfb, pred_sfb_max[hDecoder->sf_index]);
+
+ if ((ics->pred.predictor_reset = faad_get1bit(ld
+ DEBUGVAR(1,53,"ics_info(): pred.predictor_reset"))) & 1)
+ {
+ ics->pred.predictor_reset_group_number = (uint8_t)faad_getbits(ld, 5
+ DEBUGVAR(1,54,"ics_info(): pred.predictor_reset_group_number"));
+ }
+
+ for (sfb = 0; sfb < ics->pred.limit; sfb++)
+ {
+ ics->pred.prediction_used[sfb] = faad_get1bit(ld
+ DEBUGVAR(1,55,"ics_info(): pred.prediction_used"));
+ }
+ }
+#ifdef LTP_DEC
+ else { /* Long Term Prediction */
+ if (hDecoder->object_type < ER_OBJECT_START)
+ {
+ if ((ics->ltp.data_present = faad_get1bit(ld
+ DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
+ {
+ ltp_data(hDecoder, ics, &(ics->ltp), ld);
+ }
+ if (common_window)
+ {
+ if ((ics->ltp2.data_present = faad_get1bit(ld
+ DEBUGVAR(1,51,"ics_info(): ltp2.data_present"))) & 1)
+ {
+ ltp_data(hDecoder, ics, &(ics->ltp2), ld);
+ }
+ }
+ }
+#ifdef ERROR_RESILIENCE
+ if (!common_window && (hDecoder->object_type >= ER_OBJECT_START))
+ {
+ if ((ics->ltp.data_present = faad_get1bit(ld
+ DEBUGVAR(1,50,"ics_info(): ltp.data_present"))) & 1)
+ {
+ ltp_data(hDecoder, ics, &(ics->ltp), ld);
+ }
+ }
+#endif
+ }
+#endif
+ }
+ }
+
+ return retval;
+}
+
+/* Table 4.4.7 */
+static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld)
+{
+ uint8_t i;
+
+ pul->number_pulse = (uint8_t)faad_getbits(ld, 2
+ DEBUGVAR(1,56,"pulse_data(): number_pulse"));
+ pul->pulse_start_sfb = (uint8_t)faad_getbits(ld, 6
+ DEBUGVAR(1,57,"pulse_data(): pulse_start_sfb"));
+
+ /* check the range of pulse_start_sfb */
+ if (pul->pulse_start_sfb > ics->num_swb)
+ return 16;
+
+ for (i = 0; i < pul->number_pulse+1; i++)
+ {
+ pul->pulse_offset[i] = (uint8_t)faad_getbits(ld, 5
+ DEBUGVAR(1,58,"pulse_data(): pulse_offset"));
+ pul->pulse_amp[i] = (uint8_t)faad_getbits(ld, 4
+ DEBUGVAR(1,59,"pulse_data(): pulse_amp"));
+ }
+
+ return 0;
+}
+
+/* Table 4.4.8: Currently just for skipping the bits... */
+static uint8_t coupling_channel_element(faacDecHandle hDecoder, bitfile *ld)
+{
+ uint8_t c, result = 0;
+ uint8_t ind_sw_cce_flag = 0;
+ uint8_t num_gain_element_lists = 0;
+ uint8_t num_coupled_elements = 0;
+
+ element el_empty;
+ ic_stream ics_empty;
+ int16_t sh_data[1024];
+
+ memset(&el_empty, 0, sizeof(element));
+ memset(&ics_empty, 0, sizeof(ic_stream));
+
+ c = faad_getbits(ld, LEN_TAG
+ DEBUGVAR(1,900,"coupling_channel_element(): element_instance_tag"));
+
+ ind_sw_cce_flag = faad_get1bit(ld
+ DEBUGVAR(1,901,"coupling_channel_element(): ind_sw_cce_flag"));
+ num_coupled_elements = faad_getbits(ld, 3
+ DEBUGVAR(1,902,"coupling_channel_element(): num_coupled_elements"));
+
+ for (c = 0; c < num_coupled_elements + 1; c++)
+ {
+ uint8_t cc_target_is_cpe, cc_target_tag_select;
+
+ num_gain_element_lists++;
+
+ cc_target_is_cpe = faad_get1bit(ld
+ DEBUGVAR(1,903,"coupling_channel_element(): cc_target_is_cpe"));
+ cc_target_tag_select = faad_getbits(ld, 4
+ DEBUGVAR(1,904,"coupling_channel_element(): cc_target_tag_select"));
+
+ if (cc_target_is_cpe)
+ {
+ uint8_t cc_l = faad_get1bit(ld
+ DEBUGVAR(1,905,"coupling_channel_element(): cc_l"));
+ uint8_t cc_r = faad_get1bit(ld
+ DEBUGVAR(1,906,"coupling_channel_element(): cc_r"));
+
+ if (cc_l && cc_r)
+ num_gain_element_lists++;
+ }
+ }
+
+ faad_get1bit(ld
+ DEBUGVAR(1,907,"coupling_channel_element(): cc_domain"));
+ faad_get1bit(ld
+ DEBUGVAR(1,908,"coupling_channel_element(): gain_element_sign"));
+ faad_getbits(ld, 2
+ DEBUGVAR(1,909,"coupling_channel_element(): gain_element_scale"));
+
+ if ((result = individual_channel_stream(hDecoder, &el_empty, ld, &ics_empty,
+ 0, sh_data)) > 0)
+ {
+ return result;
+ }
+
+ for (c = 1; c < num_gain_element_lists; c++)
+ {
+ uint8_t cge;
+
+ if (ind_sw_cce_flag)
+ {
+ cge = 1;
+ } else {
+ cge = faad_get1bit(ld
+ DEBUGVAR(1,910,"coupling_channel_element(): common_gain_element_present"));
+ }
+
+ if (cge)
+ {
+ huffman_scale_factor(ld);
+ } else {
+ uint8_t g, sfb;
+
+ for (g = 0; g < ics_empty.num_window_groups; g++)
+ {
+ for (sfb = 0; sfb < ics_empty.max_sfb; sfb++)
+ {
+ if (ics_empty.sfb_cb[g][sfb] != ZERO_HCB)
+ huffman_scale_factor(ld);
+ }
+ }
+ }
+ }
+
+ return 0;
+}
+
+/* Table 4.4.10 */
+static uint16_t data_stream_element(faacDecHandle hDecoder, bitfile *ld)
+{
+ uint8_t byte_aligned;
+ uint16_t i, count;
+
+ /* element_instance_tag = */ faad_getbits(ld, LEN_TAG
+ DEBUGVAR(1,60,"data_stream_element(): element_instance_tag"));
+ byte_aligned = faad_get1bit(ld
+ DEBUGVAR(1,61,"data_stream_element(): byte_aligned"));
+ count = (uint16_t)faad_getbits(ld, 8
+ DEBUGVAR(1,62,"data_stream_element(): count"));
+ if (count == 255)
+ {
+ count += (uint16_t)faad_getbits(ld, 8
+ DEBUGVAR(1,63,"data_stream_element(): extra count"));
+ }
+ if (byte_aligned)
+ faad_byte_align(ld);
+
+ for (i = 0; i < count; i++)
+ {
+ uint8_t data = faad_getbits(ld, LEN_BYTE
+ DEBUGVAR(1,64,"data_stream_element(): d