shithub: aacdec

Download patch

ref: ab56ea59d3a49486c15200f1db08a3d50b1df964
parent: d81e3555e2df1b812f83e267bbcf0644edf4174f
author: menno <menno>
date: Fri Sep 27 04:37:22 EDT 2002

Major cleanup of decoder.c
All bitstream functions are now in syntax.c

--- a/libfaad/common.c
+++ b/libfaad/common.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: common.c,v 1.2 2002/08/27 10:24:54 menno Exp $
+** $Id: common.c,v 1.3 2002/09/27 08:37:22 menno Exp $
 **/
 
 /* just some common functions that could be used anywhere */
@@ -23,9 +23,77 @@
 
 #include "common.h"
 
-#define LOG2 0.30102999566398
+#include "syntax.h"
 
+/* Returns the sample rate index based on the samplerate */
+uint8_t get_sr_index(uint32_t samplerate)
+{
+    if (92017 <= samplerate) return 0;
+    if (75132 <= samplerate) return 1;
+    if (55426 <= samplerate) return 2;
+    if (46009 <= samplerate) return 3;
+    if (37566 <= samplerate) return 4;
+    if (27713 <= samplerate) return 5;
+    if (23004 <= samplerate) return 6;
+    if (18783 <= samplerate) return 7;
+    if (13856 <= samplerate) return 8;
+    if (11502 <= samplerate) return 9;
+    if (9391 <= samplerate) return 10;
+
+    return 11;
+}
+
+/* Returns 0 if an object type is decodable, otherwise returns -1 */
+int8_t can_decode_ot(uint8_t object_type)
+{
+    switch (object_type)
+    {
+    case LC:
+        return 0;
+    case MAIN:
+#ifdef MAIN_DEC
+        return 0;
+#else
+        return -1;
+#endif
+    case SSR:
+        return -1;
+    case LTP:
+#ifdef LTP_DEC
+        return 0;
+#else
+        return -1;
+#endif
+
+    /* ER object types */
+#ifdef ERROR_RESILIENCE
+    case ER_LC:
+#ifdef DRM
+    case DRM_ER_LC:
+#endif
+        return 0;
+    case ER_LTP:
+#ifdef LTP_DEC
+        return 0;
+#else
+        return -1;
+#endif
+    case LD:
+#ifdef LD_DEC
+        return 0;
+#else
+        return -1;
+#endif
+#endif
+    }
+
+    return -1;
+}
+
 #if 0
+
+#define LOG2 0.30102999566398
+
 uint32_t int_log2(uint32_t val)
 {
     return (uint32_t)(log((real_t)val)/LOG2 + 0.5);
--- a/libfaad/common.h
+++ b/libfaad/common.h
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: common.h,v 1.22 2002/09/26 19:01:45 menno Exp $
+** $Id: common.h,v 1.23 2002/09/27 08:37:22 menno Exp $
 **/
 
 #ifndef __COMMON_H__
@@ -203,6 +203,8 @@
 
 /* common functions */
 uint32_t int_log2(uint32_t val);
+int8_t can_decode_ot(uint8_t object_type);
+uint8_t get_sr_index(uint32_t samplerate);
 
 #ifndef M_PI
 #define M_PI 3.14159265358979323846f
--- a/libfaad/decoder.c
+++ b/libfaad/decoder.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: decoder.c,v 1.35 2002/09/26 19:01:45 menno Exp $
+** $Id: decoder.c,v 1.36 2002/09/27 08:37:22 menno Exp $
 **/
 
 #include <stdlib.h>
@@ -62,9 +62,11 @@
     hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
     hDecoder->adts_header_present = 0;
     hDecoder->adif_header_present = 0;
+#ifdef ERROR_RESILIENCE
     hDecoder->aacSectionDataResilienceFlag = 0;
     hDecoder->aacScalefactorDataResilienceFlag = 0;
     hDecoder->aacSpectralDataResilienceFlag = 0;
+#endif
     hDecoder->frameLength = 1024;
 
     hDecoder->frame = 0;
@@ -120,71 +122,6 @@
     return 1;
 }
 
-/* Returns the sample rate index */
-static uint8_t get_sr_index(uint32_t samplerate)
-{
-    if (92017 <= samplerate) return 0;
-    if (75132 <= samplerate) return 1;
-    if (55426 <= samplerate) return 2;
-    if (46009 <= samplerate) return 3;
-    if (37566 <= samplerate) return 4;
-    if (27713 <= samplerate) return 5;
-    if (23004 <= samplerate) return 6;
-    if (18783 <= samplerate) return 7;
-    if (13856 <= samplerate) return 8;
-    if (11502 <= samplerate) return 9;
-    if (9391 <= samplerate) return 10;
-
-    return 11;
-}
-
-/* Returns 0 if an object type is decodable, otherwise returns -1 */
-static int8_t can_decode_ot(uint8_t object_type)
-{
-    switch (object_type)
-    {
-    case LC:
-        return 0;
-    case MAIN:
-#ifdef MAIN_DEC
-        return 0;
-#else
-        return -1;
-#endif
-    case SSR:
-        return -1;
-    case LTP:
-#ifdef LTP_DEC
-        return 0;
-#else
-        return -1;
-#endif
-
-    /* ER object types */
-#ifdef ERROR_RESILIENCE
-    case ER_LC:
-#ifdef DRM
-    case DRM_ER_LC:
-#endif
-        return 0;
-    case ER_LTP:
-#ifdef LTP_DEC
-        return 0;
-#else
-        return -1;
-#endif
-    case LD:
-#ifdef LD_DEC
-        return 0;
-#else
-        return -1;
-#endif
-#endif
-    }
-
-    return -1;
-}
-
 int32_t FAADAPI faacDecInit(faacDecHandle hDecoder, uint8_t *buffer,
                         uint32_t *samplerate, uint8_t *channels)
 {
@@ -278,9 +215,13 @@
 
     rc = AudioSpecificConfig(pBuffer, samplerate, channels,
         &hDecoder->sf_index, &hDecoder->object_type,
+#ifdef ERROR_RESILIENCE
         &hDecoder->aacSectionDataResilienceFlag,
         &hDecoder->aacScalefactorDataResilienceFlag,
         &hDecoder->aacSpectralDataResilienceFlag,
+#else
+        NULL, NULL, NULL,
+#endif
         &frameLengthFlag);
     if (hDecoder->object_type < 5)
         hDecoder->object_type--; /* For AAC differs from MPEG-4 */
@@ -338,110 +279,12 @@
     if (hDecoder) free(hDecoder);
 }
 
-#ifdef ERROR_RESILIENCE
-#define decode_sce_lfe() \
-    spec_data[channels]   = (int16_t*)malloc(frame_len*sizeof(int16_t)); \
-    spec_coef[channels]   = (real_t*)malloc(frame_len*sizeof(real_t)); \
- \
-    syntax_elements[ch_ele] = (element*)malloc(sizeof(element)); \
-    memset(syntax_elements[ch_ele], 0, sizeof(element)); \
-    syntax_elements[ch_ele]->ele_id  = id_syn_ele; \
-    syntax_elements[ch_ele]->channel = channels; \
-    syntax_elements[ch_ele]->paired_channel = -1; \
- \
-    if ((hInfo->error = single_lfe_channel_element(syntax_elements[ch_ele], \
-        ld, spec_data[channels], sf_index, object_type, frame_len, \
-        aacSectionDataResilienceFlag, aacScalefactorDataResilienceFlag, \
-        aacSpectralDataResilienceFlag)) > 0) \
-    { \
-        /* to make sure everything gets deallocated */ \
-        channels++; ch_ele++; \
-        goto error; \
-    } \
- \
-    channels++; \
-    ch_ele++;
-#else
-#define decode_sce_lfe() \
-    spec_data[channels]   = (int16_t*)malloc(frame_len*sizeof(int16_t)); \
-    spec_coef[channels]   = (real_t*)malloc(frame_len*sizeof(real_t)); \
- \
-    syntax_elements[ch_ele] = (element*)malloc(sizeof(element)); \
-    memset(syntax_elements[ch_ele], 0, sizeof(element)); \
-    syntax_elements[ch_ele]->ele_id  = id_syn_ele; \
-    syntax_elements[ch_ele]->channel = channels; \
-    syntax_elements[ch_ele]->paired_channel = -1; \
- \
-    if ((hInfo->error = single_lfe_channel_element(syntax_elements[ch_ele], \
-        ld, spec_data[channels], sf_index, object_type, frame_len)) > 0) \
-    { \
-        /* to make sure everything gets deallocated */ \
-        channels++; ch_ele++; \
-        goto error; \
-    } \
- \
-    channels++; \
-    ch_ele++;
-#endif
-
-#ifdef ERROR_RESILIENCE
-#define decode_cpe() \
-    spec_data[channels]   = (int16_t*)malloc(frame_len*sizeof(int16_t)); \
-    spec_data[channels+1] = (int16_t*)malloc(frame_len*sizeof(int16_t)); \
-    spec_coef[channels]   = (real_t*)malloc(frame_len*sizeof(real_t)); \
-    spec_coef[channels+1] = (real_t*)malloc(frame_len*sizeof(real_t)); \
- \
-    syntax_elements[ch_ele] = (element*)malloc(sizeof(element)); \
-    memset(syntax_elements[ch_ele], 0, sizeof(element)); \
-    syntax_elements[ch_ele]->ele_id         = id_syn_ele; \
-    syntax_elements[ch_ele]->channel        = channels; \
-    syntax_elements[ch_ele]->paired_channel = channels+1; \
- \
-    if ((hInfo->error = channel_pair_element(syntax_elements[ch_ele], \
-        ld, spec_data[channels], spec_data[channels+1], \
-        sf_index, object_type, frame_len, \
-        aacSectionDataResilienceFlag, aacScalefactorDataResilienceFlag, \
-        aacSpectralDataResilienceFlag)) > 0) \
-    { \
-        /* to make sure everything gets deallocated */ \
-        channels+=2; ch_ele++; \
-        goto error; \
-    } \
- \
-    channels += 2; \
-    ch_ele++;
-#else
-#define decode_cpe() \
-    spec_data[channels]   = (int16_t*)malloc(frame_len*sizeof(int16_t)); \
-    spec_data[channels+1] = (int16_t*)malloc(frame_len*sizeof(int16_t)); \
-    spec_coef[channels]   = (real_t*)malloc(frame_len*sizeof(real_t)); \
-    spec_coef[channels+1] = (real_t*)malloc(frame_len*sizeof(real_t)); \
- \
-    syntax_elements[ch_ele] = (element*)malloc(sizeof(element)); \
-    memset(syntax_elements[ch_ele], 0, sizeof(element)); \
-    syntax_elements[ch_ele]->ele_id         = id_syn_ele; \
-    syntax_elements[ch_ele]->channel        = channels; \
-    syntax_elements[ch_ele]->paired_channel = channels+1; \
- \
-    if ((hInfo->error = channel_pair_element(syntax_elements[ch_ele], \
-        ld, spec_data[channels], spec_data[channels+1], \
-        sf_index, object_type, frame_len)) > 0) \
-    { \
-        /* to make sure everything gets deallocated */ \
-        channels+=2; ch_ele++; \
-        goto error; \
-    } \
- \
-    channels += 2; \
-    ch_ele++;
-#endif
-
 void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
                             faacDecFrameInfo *hInfo,
                             uint8_t *buffer)
 {
     int32_t i;
-    uint8_t id_syn_ele, ele, ch;
+    uint8_t ch;
     adts_header adts;
     uint8_t channels, ch_ele;
     bitfile *ld = (bitfile*)malloc(sizeof(bitfile));
@@ -479,6 +322,7 @@
 
     program_config pce;
     element *syntax_elements[MAX_SYNTAX_ELEMENTS];
+    element **elements;
     int16_t *spec_data[MAX_CHANNELS];
     real_t *spec_coef[MAX_CHANNELS];
 
@@ -487,10 +331,6 @@
 
     void *sample_buffer;
 
-    ele = 0;
-    channels = 0;
-    ch_ele = 0;
-
     memset(hInfo, 0, sizeof(faacDecFrameInfo));
 
     /* initialize the bitstream */
@@ -511,110 +351,17 @@
     dbg_count = 0;
 #endif
 
-#ifdef ERROR_RESILIENCE
-    if (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:
-            case ID_LFE:
-                decode_sce_lfe();
-                break;
-            case ID_CPE:
-                decode_cpe();
-                break;
-            case ID_CCE: /* not implemented yet */
-                hInfo->error = 6;
-                goto error;
-                break;
-            case ID_DSE:
-                data_stream_element(ld);
-                break;
-            case ID_PCE:
-                if ((hInfo->error = program_config_element(&pce, ld)) > 0)
-                    goto error;
-                break;
-            case ID_FIL:
-                if ((hInfo->error = fill_element(ld, drc)) > 0)
-                    goto error;
-                break;
-            }
-            ele++;
-        }
-#ifdef ERROR_RESILIENCE
-    } else {
-        /* Table 262: er_raw_data_block() */
-        switch (channelConfiguration)
-        {
-        case 1:
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            break;
-        case 2:
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            break;
-        case 3:
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            break;
-        case 4:
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            break;
-        case 5:
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            break;
-        case 6:
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_LFE;
-            decode_sce_lfe();
-            break;
-        case 7:
-            id_syn_ele = ID_SCE;
-            decode_sce_lfe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_CPE;
-            decode_cpe();
-            id_syn_ele = ID_LFE;
-            decode_sce_lfe();
-            break;
-        default:
-            hInfo->error = 7;
-            goto error;
-        }
-#if 0
-        cnt = bits_to_decode() / 8;
-        while (cnt >= 1)
-        {
-            cnt -= extension_payload(cnt);
-        }
-#endif
-    }
-#endif
+    channels = 0;
+    ch_ele = 0;
+    elements = syntax_elements;
+
+    /* decode the complete bitstream */
+    elements = raw_data_block(hDecoder, hInfo, ld, syntax_elements,
+        spec_data, spec_coef, &ch_ele, &channels, &pce, drc);
+
+    if (hInfo->error > 0)
+        goto error;
+
 
     /* no more bit reading after this */
     faad_byte_align(ld);
--- a/libfaad/decoder.h
+++ b/libfaad/decoder.h
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: decoder.h,v 1.15 2002/09/08 18:14:37 menno Exp $
+** $Id: decoder.h,v 1.16 2002/09/27 08:37:22 menno Exp $
 **/
 
 #ifndef __DECODER_H__
@@ -75,9 +75,11 @@
     uint8_t sf_index;
     uint8_t object_type;
     uint8_t channelConfiguration;
+#ifdef ERROR_RESILIENCE
     uint8_t aacSectionDataResilienceFlag;
     uint8_t aacScalefactorDataResilienceFlag;
     uint8_t aacSpectralDataResilienceFlag;
+#endif
     uint16_t frameLength;
 
     uint32_t frame;
@@ -136,6 +138,21 @@
 void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
                             faacDecFrameInfo *hInfo,
                             uint8_t *buffer);
+
+/* these functions are in syntax.c */
+element *decode_sce_lfe(faacDecHandle hDecoder,
+                        faacDecFrameInfo *hInfo, bitfile *ld,
+                        int16_t **spec_data, real_t **spec_coef,
+                        uint8_t channels, uint8_t id_syn_ele);
+element *decode_cpe(faacDecHandle hDecoder,
+                    faacDecFrameInfo *hInfo, bitfile *ld,
+                    int16_t **spec_data, real_t **spec_coef,
+                    uint8_t channels, uint8_t id_syn_ele);
+element **raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
+                         bitfile *ld, element **elements,
+                         int16_t **spec_data, real_t **spec_coef,
+                         uint8_t *out_ch_ele, uint8_t *out_channels,
+                         program_config *pce, drc_info *drc);
 
 #ifdef _WIN32
   #pragma pack(pop)
--- a/libfaad/filtbank.c
+++ b/libfaad/filtbank.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: filtbank.c,v 1.19 2002/09/08 18:14:37 menno Exp $
+** $Id: filtbank.c,v 1.20 2002/09/27 08:37:22 menno Exp $
 **/
 
 #include "common.h"
@@ -25,6 +25,7 @@
 #include <memory.h>
 #include <assert.h>
 #include "filtbank.h"
+#include "decoder.h"
 #include "syntax.h"
 #include "kbd_win.h"
 #include "sine_win.h"
--- a/libfaad/huffman.h
+++ b/libfaad/huffman.h
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: huffman.h,v 1.11 2002/09/26 19:01:45 menno Exp $
+** $Id: huffman.h,v 1.12 2002/09/27 08:37:22 menno Exp $
 **/
 
 #ifndef __HUFFMAN_H__
@@ -311,9 +311,9 @@
     /* VCB11 uses codebook 11 */
     case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23:
     case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31:
-    {
         /* TODO: If ER is used, some extra error checking should be done */
 #endif
+    {
         uint8_t err = huffman_2step_pair_sign(11, ld, sp);
         sp[0] = huffman_getescape(ld, sp[0]);
         sp[1] = huffman_getescape(ld, sp[1]);
--- a/libfaad/mp4.c
+++ b/libfaad/mp4.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: mp4.c,v 1.10 2002/09/18 11:22:36 menno Exp $
+** $Id: mp4.c,v 1.11 2002/09/27 08:37:22 menno Exp $
 **/
 
 #include "common.h"
@@ -147,28 +147,25 @@
         ObjectTypeIndex == 6 || ObjectTypeIndex == 7)
     {
         return GASpecificConfig(&ld, channels, ObjectTypeIndex,
+#ifdef ERROR_RESILIENCE
             aacSectionDataResilienceFlag,
             aacScalefactorDataResilienceFlag,
             aacSpectralDataResilienceFlag,
+#endif
             frameLengthFlag);
 #ifdef ERROR_RESILIENCE
     } else if (ObjectTypeIndex >= ER_OBJECT_START) { /* ER */
         int8_t result = GASpecificConfig(&ld, channels, ObjectTypeIndex,
+#ifdef ERROR_RESILIENCE
             aacSectionDataResilienceFlag,
             aacScalefactorDataResilienceFlag,
             aacSpectralDataResilienceFlag,
+#endif
             frameLengthFlag);
         uint8_t ep_config = (uint8_t)faad_getbits(&ld, 2
             DEBUGVAR(1,143,"parse_audio_decoder_specific_info(): epConfig"));
         if (ep_config != 0)
             return -5;
-
-#if 0
-        printf("sect: %d scf: %d spec: %d\n",
-            *aacSectionDataResilienceFlag,
-            *aacScalefactorDataResilienceFlag,
-            *aacSpectralDataResilienceFlag);
-#endif
 
         return result;
 #endif
--- a/libfaad/pns.h
+++ b/libfaad/pns.h
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: pns.h,v 1.9 2002/09/08 18:14:37 menno Exp $
+** $Id: pns.h,v 1.10 2002/09/27 08:37:22 menno Exp $
 **/
 
 #ifndef __PNS_H__
@@ -26,8 +26,9 @@
 extern "C" {
 #endif
 
-#include "syntax.h"
 #include "common.h"
+
+#include "syntax.h"
 
 #define NOISE_OFFSET 90
 /* #define MEAN_NRG 1.537228e+18 */ /* (2^31)^2 / 3 */
--- a/libfaad/syntax.c
+++ b/libfaad/syntax.c
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: syntax.c,v 1.28 2002/09/26 19:01:45 menno Exp $
+** $Id: syntax.c,v 1.29 2002/09/27 08:37:22 menno Exp $
 **/
 
 /*
@@ -30,6 +30,7 @@
 #include <stdlib.h>
 #include <memory.h>
 #include "common.h"
+#include "decoder.h"
 #include "syntax.h"
 #include "specrec.h"
 #include "huffman.h"
@@ -48,9 +49,11 @@
 /* Table 4.4.1 */
 int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration,
                         uint8_t object_type,
+#ifdef ERROR_RESILIENCE
                         uint8_t *aacSectionDataResilienceFlag,
                         uint8_t *aacScalefactorDataResilienceFlag,
                         uint8_t *aacSpectralDataResilienceFlag,
+#endif
                         uint8_t *frameLengthFlag)
 {
     uint8_t dependsOnCoreCoder, extensionFlag;
@@ -79,6 +82,7 @@
             return -3;
     }
 
+#ifdef ERROR_RESILIENCE
     if (extensionFlag == 1)
     {
         /* Error resilience not supported yet */
@@ -94,6 +98,7 @@
             /* 1 bit: extensionFlag3 */
         }
     }
+#endif
 
     return 0;
 }
@@ -233,17 +238,285 @@
     return 0;
 }
 
+element *decode_sce_lfe(faacDecHandle hDecoder,
+                        faacDecFrameInfo *hInfo, bitfile *ld,
+                        int16_t **spec_data, real_t **spec_coef,
+                        uint8_t channels, uint8_t id_syn_ele)
+{
+    element *ele;
+
+    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(ele,
+        ld, spec_data[channels], hDecoder->sf_index,
+        hDecoder->object_type, hDecoder->frameLength
+#ifdef ERROR_RESILIENCE
+        ,hDecoder->aacSectionDataResilienceFlag,
+        hDecoder->aacScalefactorDataResilienceFlag,
+        hDecoder->aacSpectralDataResilienceFlag
+#endif
+        );
+
+    return ele;
+}
+
+element *decode_cpe(faacDecHandle hDecoder,
+                    faacDecFrameInfo *hInfo, bitfile *ld,
+                    int16_t **spec_data, real_t **spec_coef,
+                    uint8_t channels, uint8_t id_syn_ele)
+{
+    element *ele;
+
+    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(ele,
+        ld, spec_data[channels], spec_data[channels+1],
+        hDecoder->sf_index, hDecoder->object_type,
+        hDecoder->frameLength
+#ifdef ERROR_RESILIENCE
+        ,hDecoder->aacSectionDataResilienceFlag,
+        hDecoder->aacScalefactorDataResilienceFlag,
+        hDecoder->aacSpectralDataResilienceFlag
+#endif
+        );
+
+    return ele;
+}
+
+element **raw_data_block(faacDecHandle hDecoder, faacDecFrameInfo *hInfo,
+                         bitfile *ld, element **elements,
+                         int16_t **spec_data, real_t **spec_coef,
+                         uint8_t *out_ch_ele, uint8_t *out_channels,
+                         program_config *pce, drc_info *drc)
+{
+    uint8_t id_syn_ele, ch_ele, channels;
+
+    channels = 0;
+    ch_ele = 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:
+            case ID_LFE:
+                elements[ch_ele] = decode_sce_lfe(hDecoder,
+                    hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+                ch_ele++; channels++;
+                if (hInfo->error > 0)
+                    goto return_on_error;
+                break;
+            case ID_CPE:
+                elements[ch_ele] = decode_cpe(hDecoder,
+                    hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+                channels += 2; ch_ele++;
+                if (hInfo->error > 0)
+                    goto return_on_error;
+                break;
+            case ID_CCE: /* not implemented yet */
+                hInfo->error = 6;
+                goto return_on_error;
+                break;
+            case ID_DSE:
+                data_stream_element(ld);
+                break;
+            case ID_PCE:
+                if ((hInfo->error = program_config_element(pce, ld)) > 0)
+                    goto return_on_error;
+                break;
+            case ID_FIL:
+                if ((hInfo->error = fill_element(ld, drc)) > 0)
+                    goto return_on_error;
+                break;
+            }
+        }
+#ifdef ERROR_RESILIENCE
+    } else {
+        /* Table 262: er_raw_data_block() */
+        switch (hDecoder->channelConfiguration)
+        {
+        case 1:
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        case 2:
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        case 3:
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        case 4:
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        case 5:
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        case 6:
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_LFE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        case 7:
+            id_syn_ele = ID_SCE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_CPE;
+            elements[ch_ele] = decode_cpe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            channels += 2; ch_ele++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            id_syn_ele = ID_LFE;
+            elements[ch_ele] = decode_sce_lfe(hDecoder,
+                hInfo, ld, spec_data, spec_coef, channels, id_syn_ele);
+            ch_ele++; channels++;
+            if (hInfo->error > 0)
+                goto return_on_error;
+            break;
+        default:
+            hInfo->error = 7;
+            goto return_on_error;
+        }
+#if 0
+        cnt = bits_to_decode() / 8;
+        while (cnt >= 1)
+        {
+            cnt -= extension_payload(cnt);
+        }
+#endif
+    }
+#endif
+
+return_on_error:
+    *out_ch_ele = ch_ele;
+    *out_channels = channels;
+
+    return elements;
+}
+
 /* Table 4.4.4 and */
 /* Table 4.4.9 */
-uint8_t single_lfe_channel_element(element *sce, bitfile *ld, int16_t *spec_data,
-                                   uint8_t sf_index, uint8_t object_type,
-                                   uint16_t frame_len
+static uint8_t single_lfe_channel_element(element *sce, bitfile *ld,
+                                          int16_t *spec_data,
+                                          uint8_t sf_index, uint8_t object_type,
+                                          uint16_t frame_len
 #ifdef ERROR_RESILIENCE
-                                   ,uint8_t aacSectionDataResilienceFlag,
-                                   uint8_t aacScalefactorDataResilienceFlag,
-                                   uint8_t aacSpectralDataResilienceFlag
+                                          ,uint8_t aacSectionDataResilienceFlag,
+                                          uint8_t aacScalefactorDataResilienceFlag,
+                                          uint8_t aacSpectralDataResilienceFlag
 #endif
-                                   )
+                                          )
 {
     ic_stream *ics = &(sce->ics1);
 
@@ -264,15 +537,15 @@
 }
 
 /* Table 4.4.5 */
-uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1,
-                             int16_t *spec_data2, uint8_t sf_index, uint8_t object_type,
-                             uint16_t frame_len
+static uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1,
+                                    int16_t *spec_data2, uint8_t sf_index, uint8_t object_type,
+                                    uint16_t frame_len
 #ifdef ERROR_RESILIENCE
-                             ,uint8_t aacSectionDataResilienceFlag,
-                             uint8_t aacScalefactorDataResilienceFlag,
-                             uint8_t aacSpectralDataResilienceFlag
+                                    ,uint8_t aacSectionDataResilienceFlag,
+                                    uint8_t aacScalefactorDataResilienceFlag,
+                                    uint8_t aacSpectralDataResilienceFlag
 #endif
-                             )
+                                    )
 {
     uint8_t result;
     ic_stream *ics1 = &(cpe->ics1);
@@ -458,7 +731,7 @@
 }
 
 /* Table 4.4.10 */
-uint16_t data_stream_element(bitfile *ld)
+static uint16_t data_stream_element(bitfile *ld)
 {
     uint8_t byte_aligned;
     uint16_t i, count;
@@ -487,7 +760,7 @@
 }
 
 /* Table 4.4.11 */
-uint8_t fill_element(bitfile *ld, drc_info *drc
+static uint8_t fill_element(bitfile *ld, drc_info *drc
 #ifdef SBR
                      ,uint8_t next_ele_id
 #endif
--- a/libfaad/syntax.h
+++ b/libfaad/syntax.h
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software 
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: syntax.h,v 1.19 2002/09/04 10:22:18 menno Exp $
+** $Id: syntax.h,v 1.20 2002/09/27 08:37:22 menno Exp $
 **/
 
 #ifndef __SYNTAX_H__
@@ -283,40 +283,44 @@
 
 int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration,
                         uint8_t object_type,
+#ifdef ERROR_RESILIENCE
                         uint8_t *aacSectionDataResilienceFlag,
                         uint8_t *aacScalefactorDataResilienceFlag,
                         uint8_t *aacSpectralDataResilienceFlag,
+#endif
                         uint8_t *frameLengthFlag);
-uint8_t single_lfe_channel_element(element *sce, bitfile *ld, int16_t *spec_data,
-                                   uint8_t sf_index, uint8_t object_type,
-                                   uint16_t frame_len
+
+uint8_t adts_frame(adts_header *adts, bitfile *ld);
+void get_adif_header(adif_header *adif, bitfile *ld);
+
+
+/* static functions */
+static uint8_t single_lfe_channel_element(element *sce, bitfile *ld,
+                                          int16_t *spec_data,
+                                          uint8_t sf_index, uint8_t object_type,
+                                          uint16_t frame_len
 #ifdef ERROR_RESILIENCE
-                                   ,uint8_t aacSectionDataResilienceFlag,
-                                   uint8_t aacScalefactorDataResilienceFlag,
-                                   uint8_t aacSpectralDataResilienceFlag
+                                          ,uint8_t aacSectionDataResilienceFlag,
+                                          uint8_t aacScalefactorDataResilienceFlag,
+                                          uint8_t aacSpectralDataResilienceFlag
 #endif
-                                   );
-uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1,
-                             int16_t *spec_data2, uint8_t sf_index, uint8_t object_type,
-                             uint16_t frame_len
+                                          );
+static uint8_t channel_pair_element(element *cpe, bitfile *ld, int16_t *spec_data1,
+                                    int16_t *spec_data2, uint8_t sf_index, uint8_t object_type,
+                                    uint16_t frame_len
 #ifdef ERROR_RESILIENCE
-                             ,uint8_t aacSectionDataResilienceFlag,
-                             uint8_t aacScalefactorDataResilienceFlag,
-                             uint8_t aacSpectralDataResilienceFlag
+                                    ,uint8_t aacSectionDataResilienceFlag,
+                                    uint8_t aacScalefactorDataResilienceFlag,
+                                    uint8_t aacSpectralDataResilienceFlag
 #endif
-                             );
-uint16_t data_stream_element(bitfile *ld);
-uint8_t program_config_element(program_config *pce, bitfile *ld);
-uint8_t fill_element(bitfile *ld, drc_info *drc
+                                    );
+static uint16_t data_stream_element(bitfile *ld);
+static uint8_t program_config_element(program_config *pce, bitfile *ld);
+static uint8_t fill_element(bitfile *ld, drc_info *drc
 #ifdef SBR
-                     ,uint8_t next_ele_id
+                            ,uint8_t next_ele_id
 #endif
-                     );
-uint8_t adts_frame(adts_header *adts, bitfile *ld);
-void get_adif_header(adif_header *adif, bitfile *ld);
-
-
-/* static functions */
+                            );
 static uint8_t individual_channel_stream(element *ele, bitfile *ld,
                                      ic_stream *ics, uint8_t scal_flag,
                                      int16_t *spec_data, uint8_t sf_index,