shithub: aacdec

Download patch

ref: 7af9c20656f528fe2329d91197d4f83094d7715a
parent: 46d58fc082a8537f9de79342cd7d7d24974c984d
author: menno <menno>
date: Mon Aug 5 16:33:38 EDT 2002

RVLC bitstream reading working, what's left now is the decoding.

--- a/common/libsndfile/Win32/libsndfile.dsp
+++ b/common/libsndfile/Win32/libsndfile.dsp
@@ -283,6 +283,54 @@
 # Begin Group "Header Files"
 
 # PROP Default_Filter "h;hpp;hxx;hm;inl"
+# Begin Source File
+
+SOURCE=..\src\au.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\common.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\GSM610\config.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\config.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\float_cast.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\G72x\g72x.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\GSM610\gsm.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\G72x\private.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\GSM610\private.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\sfendian.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\sndfile.h
+# End Source File
+# Begin Source File
+
+SOURCE=..\src\wav_w64.h
+# End Source File
 # End Group
 # End Target
 # End Project
--- a/frontend/main.c
+++ b/frontend/main.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: main.c,v 1.16 2002/06/13 11:03:27 menno Exp $
+** $Id: main.c,v 1.17 2002/08/05 20:33:38 menno Exp $
 **/
 
 #ifdef _WIN32
@@ -302,13 +302,15 @@
         {
             unsigned char *buff = NULL;
             int buff_size = 0;
-            unsigned long dummy32; unsigned char dummy8;
+            unsigned long dummy1_32;
+            unsigned char dummy2_8, dummy3_8, dummy4_8, dummy5_8, dummy6_8,
+                dummy7_8, dummy8_8;
             MP4GetTrackESConfiguration(infile, trackId, &buff, &buff_size);
 
             if (buff)
             {
-                rc = AudioSpecificConfig(buff, &dummy32, &dummy8, &dummy8, &dummy8,
-                    &dummy8, &dummy8, &dummy8, &dummy8);
+                rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8, &dummy3_8,
+                    &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
                 free(buff);
 
                 if (rc < 0)
--- a/libfaad/bits.c
+++ b/libfaad/bits.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: bits.c,v 1.5 2002/02/18 10:01:05 menno Exp $
+** $Id: bits.c,v 1.6 2002/08/05 20:33:38 menno Exp $
 **/
 
 #include "common.h"
@@ -61,3 +61,21 @@
     }
     return 0;
 }
+
+void faad_getbitbuffer(bitfile *ld, void *buffer, uint16_t bits
+                       DEBUGDEC)
+{
+    uint16_t i, temp;
+    uint16_t bytes = bits / 8;
+    uint8_t remainder = bits % 8;
+    uint8_t *b_buffer = (uint8_t*)buffer;
+
+    for (i = 0; i < bytes; i++)
+    {
+        b_buffer[i] = faad_getbits(ld, 8 DEBUGVAR(print,var,dbg));
+    }
+
+    temp = faad_getbits(ld, remainder DEBUGVAR(print,var,dbg)) << (8-remainder);
+
+    b_buffer[bytes] = temp;
+}
\ No newline at end of file
--- a/libfaad/bits.h
+++ b/libfaad/bits.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: bits.h,v 1.5 2002/02/18 10:01:05 menno Exp $
+** $Id: bits.h,v 1.6 2002/08/05 20:33:38 menno Exp $
 **/
 
 #ifndef __BITS_H__
@@ -58,6 +58,8 @@
 void faad_initbits(bitfile *ld, void *buffer);
 uint8_t faad_byte_align(bitfile *ld);
 uint32_t faad_get_processed_bits(bitfile *ld);
+void faad_getbitbuffer(bitfile *ld, void *buffer, uint16_t bits
+                       DEBUGDEC);
 
 
 static INLINE uint32_t faad_showbits(bitfile *ld, uint8_t bits)
--- 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.17 2002/06/15 15:10:47 menno Exp $
+** $Id: decoder.c,v 1.18 2002/08/05 20:33:38 menno Exp $
 **/
 
 #include <stdlib.h>
@@ -331,6 +331,7 @@
     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, \
@@ -353,6 +354,7 @@
     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) \
@@ -623,13 +625,9 @@
         for (i = 0; i < ch_ele; i++)
         {
             if (syntax_elements[i]->channel == ch)
-            {
                 ics = &(syntax_elements[i]->ics1);
-                break;
-            } else if (syntax_elements[i]->paired_channel == ch) {
+            else if (syntax_elements[i]->paired_channel == ch)
                 ics = &(syntax_elements[i]->ics2);
-                break;
-            }
         }
 
         /* inverse quantization */
@@ -664,12 +662,10 @@
                 ltp = &(ics->ltp);
                 pch = syntax_elements[i]->paired_channel;
                 right_channel = 0;
-                break;
             } else if (syntax_elements[i]->paired_channel == ch) {
                 ics = &(syntax_elements[i]->ics2);
                 ltp = &(ics->ltp2);
                 right_channel = 1;
-                break;
             }
         }
 
--- 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.10 2002/06/17 18:14:35 menno Exp $
+** $Id: filtbank.c,v 1.11 2002/08/05 20:33:38 menno Exp $
 **/
 
 #include "common.h"
@@ -322,7 +322,7 @@
             window_short_prev_ptr = window_short;
         }
 
-        vcopy(obuf_temp, o_buf + 448, nlong*2-nflat_ls);
+        vcopy(obuf_temp, o_buf + nflat_ls, nlong*2-nflat_ls);
         vzero(o_buf+2*nlong-1, nflat_ls);
 
         free(obuf_temp);
--- a/libfaad/libfaad.dsp
+++ b/libfaad/libfaad.dsp
@@ -209,6 +209,10 @@
 # End Source File
 # Begin Source File
 
+SOURCE=.\rvlc_scale_factors.c
+# End Source File
+# Begin Source File
+
 SOURCE=.\sbr_dec.c
 # End Source File
 # Begin Source File
@@ -330,6 +334,10 @@
 # Begin Source File
 
 SOURCE=.\pulse.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\rvlc_scale_factors.h
 # End Source File
 # Begin Source File
 
--- 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.8 2002/06/13 11:03:27 menno Exp $
+** $Id: mp4.c,v 1.9 2002/08/05 20:33:38 menno Exp $
 **/
 
 #include "common.h"
@@ -27,70 +27,70 @@
 
 /* defines if an object type can be decoded by this library or not */
 static uint8_t ObjectTypesTable[32] = {
-    0, /* NULL */
+    0, /*  0 NULL */
 #ifdef MAIN_DEC
-    1, /* AAC Main */
+    1, /*  1 AAC Main */
 #else
-    0, /* AAC Main */
+    0, /*  1 AAC Main */
 #endif
-    1, /* AAC LC */
-    0, /* AAC SSR */
+    1, /*  2 AAC LC */
+    0, /*  3 AAC SSR */
 #ifdef LTP_DEC
-    1, /* AAC LTP */
+    1, /*  4 AAC LTP */
 #else
-    0, /* AAC LTP */
+    0, /*  4 AAC LTP */
 #endif
-    0, /* Reserved */
-    0, /* AAC Scalable */
-    0, /* TwinVQ */
-    0, /* CELP */
-    0, /* HVXC */
-    0, /* Reserved */
-    0, /* Reserved */
-    0, /* TTSI */
-    0, /* Main synthetic */
-    0, /* Wavetable synthesis */
-    0, /* General MIDI */
-    0, /* Algorithmic Synthesis and Audio FX */
+    0, /*  5 Reserved */
+    0, /*  6 AAC Scalable */
+    0, /*  7 TwinVQ */
+    0, /*  8 CELP */
+    0, /*  9 HVXC */
+    0, /* 10 Reserved */
+    0, /* 11 Reserved */
+    0, /* 12 TTSI */
+    0, /* 13 Main synthetic */
+    0, /* 14 Wavetable synthesis */
+    0, /* 15 General MIDI */
+    0, /* 16 Algorithmic Synthesis and Audio FX */
 
     /* MPEG-4 Version 2 */
 #ifdef ERROR_RESILIENCE
-    1, /* ER AAC LC */
-    0, /* (Reserved) */
+    1, /* 17 ER AAC LC */
+    0, /* 18 (Reserved) */
 #ifdef LTP_DEC
-    1, /* ER AAC LTP */
+    1, /* 19 ER AAC LTP */
 #else
-    0, /* ER AAC LTP */
+    0, /* 19 ER AAC LTP */
 #endif
-    0, /* ER AAC scalable */
-    0, /* ER TwinVQ */
-    0, /* ER BSAC */
+    0, /* 20 ER AAC scalable */
+    0, /* 21 ER TwinVQ */
+    0, /* 22 ER BSAC */
 #ifdef LD_DEC
-    1, /* ER AAC LD */
+    1, /* 23 ER AAC LD */
 #else
-    0, /* ER AAC LD */
+    0, /* 23 ER AAC LD */
 #endif
-    0, /* ER CELP */
-    0, /* ER HVXC */
-    0, /* ER HILN */
-    0, /* ER Parametric */
+    0, /* 24 ER CELP */
+    0, /* 25 ER HVXC */
+    0, /* 26 ER HILN */
+    0, /* 27 ER Parametric */
 #else /* No ER defined */
-    0, /* ER AAC LC */
-    0, /* (Reserved) */
-    0, /* ER AAC LTP */
-    0, /* ER AAC scalable */
-    0, /* ER TwinVQ */
-    0, /* ER BSAC */
-    0, /* ER AAC LD */
-    0, /* ER CELP */
-    0, /* ER HVXC */
-    0, /* ER HILN */
-    0, /* ER Parametric */
+    0, /* 17 ER AAC LC */
+    0, /* 18 (Reserved) */
+    0, /* 19 ER AAC LTP */
+    0, /* 20 ER AAC scalable */
+    0, /* 21 ER TwinVQ */
+    0, /* 22 ER BSAC */
+    0, /* 23 ER AAC LD */
+    0, /* 24 ER CELP */
+    0, /* 25 ER HVXC */
+    0, /* 26 ER HILN */
+    0, /* 27 ER Parametric */
 #endif
-    0, /* (Reserved) */
-    0, /* (Reserved) */
-    0, /* (Reserved) */
-    0  /* (Reserved) */
+    0, /* 28 (Reserved) */
+    0, /* 29 (Reserved) */
+    0, /* 30 (Reserved) */
+    0  /* 31 (Reserved) */
 };
 
 /* Table 1.6.1 */
@@ -137,7 +137,7 @@
         return -2;
     }
 
-    if(ChannelsConfiguration > 7)
+    if (ChannelsConfiguration > 7)
     {
         return -3;
     }
@@ -154,7 +154,7 @@
             frameLengthFlag);
 #ifdef ERROR_RESILIENCE
     } else if (ObjectTypeIndex >= ER_OBJECT_START) { /* ER */
-        uint8_t result = GASpecificConfig(&ld, channels, ObjectTypeIndex,
+        int8_t result = GASpecificConfig(&ld, channels, ObjectTypeIndex,
             aacSectionDataResilienceFlag,
             aacScalefactorDataResilienceFlag,
             aacSpectralDataResilienceFlag,
--- /dev/null
+++ b/libfaad/rvlc_scale_factors.c
@@ -1,0 +1,182 @@
+/*
+** FAAD - Freeware Advanced Audio Decoder
+** Copyright (C) 2002 M. Bakker
+**  
+** 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.
+**
+** $Id: rvlc_scale_factors.c,v 1.1 2002/08/05 20:33:38 menno Exp $
+**/
+
+#include "common.h"
+#include <stdlib.h>
+#include "syntax.h"
+#include "bits.h"
+#include "rvlc_scale_factors.h"
+
+
+#ifdef ERROR_RESILIENCE
+
+#if 0
+        uint32_t bits_used, length_of_rvlc_sf;
+        uint8_t bits = 11;
+
+        sf_concealment = faad_get1bit(ld
+            DEBUGVAR(1,149,"scale_factor_data(): sf_concealment"));
+        rev_global_gain = faad_getbits(ld, 8
+            DEBUGVAR(1,150,"scale_factor_data(): rev_global_gain"));
+
+        if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
+            bits = 9;
+
+        /* the number of bits used for the huffman codewords */
+        length_of_rvlc_sf = faad_getbits(ld, bits
+            DEBUGVAR(1,151,"scale_factor_data(): length_of_rvlc_sf"));
+
+        /* check how many bits are used in decoding the scalefactors
+           A better solution would be to read length_of_rvlc_sf ahead
+           in a buffer and use that to decode the scale factors
+        */
+        bits_used = faad_get_processed_bits(ld);
+        decode_scale_factors(ics, ld);
+        bits_used = faad_get_processed_bits(ld) - bits_used;
+
+        /* return an error if the number of decoded bits is not correct
+           FAAD should be able to recover from this, for example by
+           setting all scalefactors to 0 (e.g. muting the frame)
+        */
+        if (bits_used != length_of_rvlc_sf)
+            return 8;
+
+        sf_escapes_present; 1 uimsbf
+
+        if (sf_escapes_present)
+        {
+            length_of_rvlc_escapes; 8 uimsbf
+
+            for (g = 0; g < num_window_groups; g++)
+            {
+                for (sfb = 0; sfb < max_sfb; sfb++)
+                {
+                    if (sect_cb[g][sfb] != ZERO_HCB)
+                    {
+                        if (is_intensity(g, sfb) &&
+                            dpcm_is_position[g][sfb] == ESC_FLAG)
+                        {
+                            rvlc_esc_sf[dpcm_is_position[g][sfb]]; 2..20 vlclbf
+                        } else {
+                            if (is_noise(g, sfb) &&
+                                dpcm_noise_nrg[g][sfb] == ESC_FLAG)
+                            {
+                                rvlc_esc_sf[dpcm_noise_nrg[g][sfb]]; 2..20 vlclbf
+                            } else {
+                                if (dpcm_sf[g][sfb] == ESC_FLAG)
+                                {
+                                    rvlc_esc_sf[dpcm_sf[g][sfb]]; 2..20 vlclbf
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+            if (intensity_used &&
+                dpcm_is_position[g][sfb] == ESC_FLAG)
+            {
+                rvlc_esc_sf[dpcm_is_last_position]; 2..20 vlclbf
+            }
+        }
+
+        if (noise_used)
+        {
+            dpcm_noise_last_position; 9 uimsbf
+        }
+#endif
+
+uint8_t rvlc_scale_factor_data(ic_stream *ics, bitfile *ld)
+{
+    uint8_t bits = 9;
+
+    ics->sf_concealment = faad_get1bit(ld
+        DEBUGVAR(1,149,"rvlc_scale_factor_data(): sf_concealment"));
+    ics->rev_global_gain = faad_getbits(ld, 8
+        DEBUGVAR(1,150,"rvlc_scale_factor_data(): rev_global_gain"));
+
+    if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
+        bits = 11;
+
+    /* the number of bits used for the huffman codewords */
+    ics->length_of_rvlc_sf = faad_getbits(ld, bits
+        DEBUGVAR(1,151,"rvlc_scale_factor_data(): length_of_rvlc_sf"));
+
+    if (ics->noise_used)
+    {
+        ics->dpcm_noise_nrg = faad_getbits(ld, 9
+            DEBUGVAR(1,152,"rvlc_scale_factor_data(): dpcm_noise_nrg"));
+
+        ics->length_of_rvlc_sf -= 9;
+    }
+
+    ics->sf_escapes_present = faad_get1bit(ld
+        DEBUGVAR(1,153,"rvlc_scale_factor_data(): sf_escapes_present"));
+
+    if (ics->sf_escapes_present)
+    {
+        ics->length_of_rvlc_escapes = faad_getbits(ld, 8
+            DEBUGVAR(1,154,"rvlc_scale_factor_data(): length_of_rvlc_escapes"));
+    }
+
+    if (ics->noise_used)
+    {
+        ics->dpcm_noise_last_position = faad_getbits(ld, 9
+            DEBUGVAR(1,155,"rvlc_scale_factor_data(): dpcm_noise_last_position"));
+    }
+
+    return 0;
+}
+
+uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld)
+{
+    void *rvlc_buffer = NULL;
+    void *rvlc_esc_buffer = NULL;
+
+    if (ics->length_of_rvlc_sf > 0)
+    {
+        rvlc_buffer = malloc((bit2byte(ics->length_of_rvlc_sf)+1)*sizeof(uint8_t));
+
+        /* We read length_of_rvlc_sf bits here to put it in a
+           seperate bitfile.
+        */
+        faad_getbitbuffer(ld, rvlc_buffer, ics->length_of_rvlc_sf
+            DEBUGVAR(1,156,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_sf"));
+    }
+
+    if (ics->sf_escapes_present)
+    {
+        rvlc_esc_buffer = malloc((bit2byte(ics->length_of_rvlc_escapes)+1)*sizeof(uint8_t));
+
+        /* We read length_of_rvlc_escapes bits here to put it in a
+           seperate bitfile.
+        */
+        faad_getbitbuffer(ld, rvlc_esc_buffer, ics->length_of_rvlc_escapes
+            DEBUGVAR(1,157,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_escapes"));
+    }
+
+    if (rvlc_esc_buffer) free(rvlc_esc_buffer);
+    if (rvlc_buffer) free(rvlc_buffer);
+
+    return 0;
+}
+
+#endif
\ No newline at end of file
--- /dev/null
+++ b/libfaad/rvlc_scale_factors.h
@@ -1,0 +1,37 @@
+/*
+** FAAD - Freeware Advanced Audio Decoder
+** Copyright (C) 2002 M. Bakker
+**  
+** 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.
+**
+** $Id: rvlc_scale_factors.h,v 1.1 2002/08/05 20:33:38 menno Exp $
+**/
+
+#ifndef __RVLC_SCF_H__
+#define __RVLC_SCF_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+uint8_t rvlc_scale_factor_data(ic_stream *ics, bitfile *ld);
+uint8_t rvlc_decode_scale_factors(ic_stream *ics, bitfile *ld);
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif
--- 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.20 2002/06/15 15:38:22 menno Exp $
+** $Id: syntax.c,v 1.21 2002/08/05 20:33:38 menno Exp $
 **/
 
 /*
@@ -40,15 +40,18 @@
 #ifdef SBR
 #include "sbr_syntax.h"
 #endif
+#ifdef ERROR_RESILIENCE
+#include "rvlc_scale_factors.h"
+#endif
 
 
 /* Table 4.4.1 */
-uint8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration,
-                         uint8_t object_type,
-                         uint8_t *aacSectionDataResilienceFlag,
-                         uint8_t *aacScalefactorDataResilienceFlag,
-                         uint8_t *aacSpectralDataResilienceFlag,
-                         uint8_t *frameLengthFlag)
+int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration,
+                        uint8_t object_type,
+                        uint8_t *aacSectionDataResilienceFlag,
+                        uint8_t *aacScalefactorDataResilienceFlag,
+                        uint8_t *aacSpectralDataResilienceFlag,
+                        uint8_t *frameLengthFlag)
 {
     uint8_t dependsOnCoreCoder, extensionFlag;
     uint16_t coreCoderDelay;
@@ -657,13 +660,8 @@
             DEBUGVAR(1,69,"individual_channel_stream(): tns_data_present"))) & 1)
         {
 #ifdef ERROR_RESILIENCE
-            /* TODO I don't understand this, but the "rewrite" software moves tns_data away */
-            if ((object_type != ER_LC) && (object_type != ER_LTP)
-#ifdef DRM
-                && (object_type != DRM_ER_LC)
+            if (object_type < ER_OBJECT_START)
 #endif
-                )
-#endif
                 tns_data(ics, &(ics->tns), ld);
         }
 
@@ -671,7 +669,7 @@
         if ((ics->gain_control_data_present = faad_get1bit(ld
             DEBUGVAR(1,70,"individual_channel_stream(): gain_control_data_present"))) & 1)
         {
-#if 0
+#if 1
             return 1;
 #else
             gain_control_data(ld, ics);
@@ -680,26 +678,8 @@
     }
 
 #ifdef ERROR_RESILIENCE
-    if (!aacSpectralDataResilienceFlag)
+    if (aacSpectralDataResilienceFlag)
     {
-        /* TODO I don't understand this, but the "rewrite" software
-                moves tns_data before spectral_data */
-        if ( (object_type == ER_LC) || (object_type == ER_LTP) 
-#ifdef DRM
-            && (object_type != DRM_ER_LC)
-#endif
-            )
-        {
-            if (ics->tns_data_present)
-                tns_data(ics, &(ics->tns), ld);
-        }
-#endif
-
-        /* decode the spectral data */
-        if ((result = spectral_data(ics, ld, spec_data, frame_len)) > 0)
-            return result;
-#ifdef ERROR_RESILIENCE
-    } else {
         ics->length_of_reordered_spectral_data = (uint16_t)faad_getbits(ld, 14
             DEBUGVAR(1,147,"individual_channel_stream(): length_of_reordered_spectral_data"));
         /* TODO: test for >6144/12288, see page 143 */
@@ -707,13 +687,27 @@
             DEBUGVAR(1,148,"individual_channel_stream(): length_of_longest_codeword"));
         if (ics->length_of_longest_codeword >= 49)
             ics->length_of_longest_codeword = 49;
+    }
 
-        /* TODO I don't understand this, but the "rewrite" software
-                moves tns_data before spectral_data */
+    /* RVLC spectral data is put here */
+    if (aacScalefactorDataResilienceFlag)
+    {
+        if ((result = rvlc_decode_scale_factors(ics, ld)) > 0)
+            return result;
+    }
 
+    if ((object_type >= ER_OBJECT_START) 
+#ifdef DRM
+        && (object_type != DRM_ER_LC)
+#endif
+        )
+    {
         if (ics->tns_data_present)
             tns_data(ics, &(ics->tns), ld);
+    }
 
+    if (aacSpectralDataResilienceFlag)
+    {
         /* error resilient spectral data decoding */
         if ((result = reordered_spectral_data(ics, ld, spec_data, frame_len,
             aacSectionDataResilienceFlag)) > 0)
@@ -720,6 +714,15 @@
         {
             return result;
         }
+    } else {
+#endif
+
+#ifdef ERROR_RESILIENCE
+        /* decode the spectral data */
+        if ((result = spectral_data(ics, ld, spec_data, frame_len)) > 0)
+        {
+            return result;
+        }
     }
 #endif
 
@@ -729,7 +732,7 @@
         if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
             pulse_decode(ics, spec_data);
         else
-            return 2; /* pulse coding not allowed for long blocks */
+            return 2; /* pulse coding not allowed for short blocks */
     }
 
     return 0;
@@ -771,6 +774,9 @@
             ics->sect_cb[g][i] = (uint8_t)faad_getbits(ld, sect_cb_bits
                 DEBUGVAR(1,71,"section_data(): sect_cb"));
 
+            if (ics->sect_cb[g][i] == NOISE_HCB)
+                ics->noise_used = 1;
+
 #ifdef ERROR_RESILIENCE
             if (!aacSectionDataResilienceFlag ||
                 (ics->sect_cb[g][i] < 11) ||
@@ -897,81 +903,11 @@
         return decode_scale_factors(ics, ld);
 #ifdef ERROR_RESILIENCE
     } else {
-#if 0
-        uint32_t bits_used, length_of_rvlc_sf;
-        uint8_t bits = 11;
-
-        sf_concealment = faad_get1bit(ld
-            DEBUGVAR(1,149,"scale_factor_data(): sf_concealment"));
-        rev_global_gain = faad_getbits(ld, 8
-            DEBUGVAR(1,150,"scale_factor_data(): rev_global_gain"));
-
-        if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
-            bits = 9;
-
-        /* the number of bits used for the huffman codewords */
-        length_of_rvlc_sf = faad_getbits(ld, bits
-            DEBUGVAR(1,151,"scale_factor_data(): length_of_rvlc_sf"));
-
-        /* check how many bits are used in decoding the scalefactors
-           A better solution would be to read length_of_rvlc_sf ahead
-           in a buffer and use that to decode the scale factors
+        /* In ER AAC the parameters for RVLC are seperated from the actual
+           data that holds the scale_factors.
+           Strangely enough, 2 parameters for HCR are put inbetween them.
         */
-        bits_used = faad_get_processed_bits(ld);
-        decode_scale_factors(ics, ld);
-        bits_used = faad_get_processed_bits(ld) - bits_used;
-
-        /* return an error if the number of decoded bits is not correct
-           FAAD should be able to recover from this, for example by
-           setting all scalefactors to 0 (e.g. muting the frame)
-        */
-        if (bits_used != length_of_rvlc_sf)
-            return 8;
-
-        sf_escapes_present; 1 uimsbf
-
-        if (sf_escapes_present)
-        {
-            length_of_rvlc_escapes; 8 uimsbf
-
-            for (g = 0; g < num_window_groups; g++)
-            {
-                for (sfb = 0; sfb < max_sfb; sfb++)
-                {
-                    if (sect_cb[g][sfb] != ZERO_HCB)
-                    {
-                        if (is_intensity(g, sfb) &&
-                            dpcm_is_position[g][sfb] == ESC_FLAG)
-                        {
-                            rvlc_esc_sf[dpcm_is_position[g][sfb]]; 2..20 vlclbf
-                        } else {
-                            if (is_noise(g, sfb) &&
-                                dpcm_noise_nrg[g][sfb] == ESC_FLAG)
-                            {
-                                rvlc_esc_sf[dpcm_noise_nrg[g][sfb]]; 2..20 vlclbf
-                            } else {
-                                if (dpcm_sf[g][sfb] == ESC_FLAG)
-                                {
-                                    rvlc_esc_sf[dpcm_sf[g][sfb]]; 2..20 vlclbf
-                                }
-                            }
-                        }
-                    }
-                }
-            }
-
-            if (intensity_used &&
-                dpcm_is_position[g][sfb] == ESC_FLAG)
-            {
-                rvlc_esc_sf[dpcm_is_last_position]; 2..20 vlclbf
-            }
-        }
-
-        if (noise_used)
-        {
-            dpcm_noise_last_position; 9 uimsbf
-        }
-#endif
+        return rvlc_scale_factor_data(ics, ld);
     }
 #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.15 2002/07/24 11:41:01 menno Exp $
+** $Id: syntax.h,v 1.16 2002/08/05 20:33:38 menno Exp $
 **/
 
 #ifndef __SYNTAX_H__
@@ -238,6 +238,8 @@
     uint8_t ms_mask_present;
     uint8_t ms_used[MAX_WINDOW_GROUPS][MAX_SFB];
 
+    uint8_t noise_used;
+
     uint8_t pulse_data_present;
     uint8_t tns_data_present;
     uint8_t gain_control_data_present;
@@ -250,9 +252,17 @@
     ltp_info ltp2;
 
 #ifdef ERROR_RESILIENCE
-    /* ER data */
+    /* ER HCR data */
     uint16_t length_of_reordered_spectral_data;
     uint8_t length_of_longest_codeword;
+    /* ER RLVC data */
+    uint8_t sf_concealment;
+    uint8_t rev_global_gain;
+    uint16_t length_of_rvlc_sf;
+    uint16_t dpcm_noise_nrg;
+    uint8_t sf_escapes_present;
+    uint8_t length_of_rvlc_escapes;
+    uint16_t dpcm_noise_last_position;
 #endif
 } ic_stream; /* individual channel stream */
 
@@ -271,17 +281,12 @@
 } element; /* syntax element (SCE, CPE, LFE) */
 
 
-uint8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration,
-                         uint8_t object_type,
-                         uint8_t *aacSectionDataResilienceFlag,
-                         uint8_t *aacScalefactorDataResilienceFlag,
-                         uint8_t *aacSpectralDataResilienceFlag,
-                         uint8_t *frameLengthFlag);
-uint8_t raw_data_block(bitfile *ld, int16_t ***spec_data, real_t ***spec_coef,
-                       element ***syntax_elements,
-                       uint8_t *channels, uint8_t *ele, uint8_t *ch_ele,
-                       uint16_t frame_len, uint8_t sf_index, uint8_t object_type,
-                       drc_info *drc);
+int8_t GASpecificConfig(bitfile *ld, uint8_t *channelConfiguration,
+                        uint8_t object_type,
+                        uint8_t *aacSectionDataResilienceFlag,
+                        uint8_t *aacScalefactorDataResilienceFlag,
+                        uint8_t *aacSpectralDataResilienceFlag,
+                        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
--