shithub: aacdec

Download patch

ref: 7c7d6306a2f7bbd4ea419277be5f35a35abe8167
parent: fcae268a4273f53783e4ca67de3896499a6c983f
author: menno <menno>
date: Wed Jan 28 14:17:26 EST 2004

new channel allocation

--- a/libfaad/bits.c
+++ b/libfaad/bits.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: bits.c,v 1.35 2004/01/16 20:20:32 menno Exp $
+** $Id: bits.c,v 1.39 2004/09/04 14:56:27 menno Exp $
 **/
 
 #include "common.h"
--- a/libfaad/bits.h
+++ b/libfaad/bits.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: bits.h,v 1.34 2004/01/16 20:20:32 menno Exp $
+** $Id: bits.h,v 1.35 2004/01/28 19:17:25 menno Exp $
 **/
 
 #ifndef __BITS_H__
--- a/libfaad/common.h
+++ b/libfaad/common.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: common.h,v 1.47 2004/01/20 18:42:51 menno Exp $
+** $Id: common.h,v 1.49 2004/01/29 11:31:11 menno Exp $
 **/
 
 #ifndef __COMMON_H__
--- a/libfaad/decoder.c
+++ b/libfaad/decoder.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: decoder.c,v 1.92 2004/01/16 20:20:32 menno Exp $
+** $Id: decoder.c,v 1.93 2004/01/28 19:17:25 menno Exp $
 **/
 
 #include "common.h"
--- a/libfaad/error.c
+++ b/libfaad/error.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: error.c,v 1.20 2004/01/16 20:20:32 menno Exp $
+** $Id: error.c,v 1.21 2004/01/28 19:17:25 menno Exp $
 **/
 
 #include "common.h"
--- a/libfaad/error.h
+++ b/libfaad/error.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: error.h,v 1.15 2004/01/16 20:20:32 menno Exp $
+** $Id: error.h,v 1.16 2004/01/28 19:17:25 menno Exp $
 **/
 
 #ifndef __ERROR_H__
--- a/libfaad/fixed.h
+++ b/libfaad/fixed.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: fixed.h,v 1.17 2004/01/05 14:05:11 menno Exp $
+** $Id: fixed.h,v 1.18 2004/01/28 19:17:25 menno Exp $
 **/
 
 #ifndef __FIXED_H__
--- a/libfaad/huffman.c
+++ b/libfaad/huffman.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: huffman.c,v 1.16 2004/01/20 18:42:51 menno Exp $
+** $Id: huffman.c,v 1.17 2004/01/28 19:17:25 menno Exp $
 **/
 
 #include "common.h"
@@ -62,7 +62,7 @@
         index = hcb_sf[index][bit];
     }
 
-    return index + 121;
+    return index + 61;
 }
 
 
--- a/libfaad/output.c
+++ b/libfaad/output.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: output.c,v 1.33 2003/12/23 18:41:42 menno Exp $
+** $Id: output.c,v 1.35 2004/02/26 09:29:27 menno Exp $
 **/
 
 #include "common.h"
@@ -36,7 +36,7 @@
 
 #define FLOAT_SCALE (1.0f/(1<<15))
 
-#define DM_MUL REAL_CONST(0.4142135623730950488) // 1/(1+sqrt(2))
+#define DM_MUL REAL_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
 #define RSQRT2 REAL_CONST(0.7071067811865475244) // 1/sqrt(2)
 
 
@@ -370,6 +370,29 @@
 
 #else
 
+#define DM_MUL FRAC_CONST(0.3203772410170407) // 1/(1+sqrt(2) + 1/sqrt(2))
+#define RSQRT2 FRAC_CONST(0.7071067811865475244) // 1/sqrt(2)
+
+static INLINE real_t get_sample(real_t **input, uint8_t channel, uint16_t sample,
+                                uint8_t down_matrix, uint8_t *internal_channel)
+{
+    if (!down_matrix)
+        return input[internal_channel[channel]][sample];
+
+    if (channel == 0)
+    {
+        real_t C   = MUL_F(input[internal_channel[0]][sample], RSQRT2);
+        real_t L_S = MUL_F(input[internal_channel[3]][sample], RSQRT2);
+        real_t cum = input[internal_channel[1]][sample] + C + L_S;
+        return MUL_F(cum, DM_MUL);
+    } else {
+        real_t C   = MUL_F(input[internal_channel[0]][sample], RSQRT2);
+        real_t R_S = MUL_F(input[internal_channel[4]][sample], RSQRT2);
+        real_t cum = input[internal_channel[2]][sample] + C + R_S;
+        return MUL_F(cum, DM_MUL);
+    }
+}
+
 void* output_to_PCM(faacDecHandle hDecoder,
                     real_t **input, void *sample_buffer, uint8_t channels,
                     uint16_t frame_len, uint8_t format)
@@ -387,7 +410,8 @@
         case FAAD_FMT_16BIT:
             for(i = 0; i < frame_len; i++)
             {
-                int32_t tmp = input[ch][i];
+                //int32_t tmp = input[ch][i];
+                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
                 if (tmp >= 0)
                 {
                     tmp += (1 << (REAL_BITS-1));
@@ -409,7 +433,8 @@
         case FAAD_FMT_24BIT:
             for(i = 0; i < frame_len; i++)
             {
-                int32_t tmp = input[ch][i];
+                //int32_t tmp = input[ch][i];
+                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
                 if (tmp >= 0)
                 {
                     tmp += (1 << (REAL_BITS-9));
@@ -432,7 +457,8 @@
         case FAAD_FMT_32BIT:
             for(i = 0; i < frame_len; i++)
             {
-                int32_t tmp = input[ch][i];
+                //int32_t tmp = input[ch][i];
+                int32_t tmp = get_sample(input, ch, i, hDecoder->downMatrix, hDecoder->internal_channel);
                 if (tmp >= 0)
                 {
                     tmp += (1 << (16-REAL_BITS-1));
--- a/libfaad/specrec.c
+++ b/libfaad/specrec.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: specrec.c,v 1.41 2004/01/16 20:20:32 menno Exp $
+** $Id: specrec.c,v 1.42 2004/01/28 19:17:26 menno Exp $
 **/
 
 /*
@@ -688,10 +688,187 @@
 }
 #endif
 
+static uint8_t allocate_single_channel(faacDecHandle hDecoder, uint8_t channel)
+{
+    uint8_t mul = 1;
+
+#ifdef MAIN_DEC
+    /* MAIN object type prediction */
+    if (hDecoder->object_type == MAIN)
+    {
+        /* allocate the state only when needed */
+        if (hDecoder->pred_stat[channel] == NULL)
+        {
+            hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
+            reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
+        }
+    }
+#endif
+
+#ifdef LTP_DEC
+    if (is_ltp_ot(hDecoder->object_type))
+    {
+        /* allocate the state only when needed */
+        if (hDecoder->lt_pred_stat[channel] == NULL)
+        {
+            hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
+            memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
+        }
+    }
+#endif
+
+    if (hDecoder->time_out[channel] == NULL)
+    {
+        mul = 1;
+#ifdef SBR_DEC
+        hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
+        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+        {
+            /* SBR requires 2 times as much output data */
+            mul = 2;
+            hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
+        }
+#endif
+        hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
+        memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
+    }
+
+    if (hDecoder->fb_intermed[channel] == NULL)
+    {
+        hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
+        memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
+    }
+
+#ifdef SSR_DEC
+    if (hDecoder->object_type == SSR)
+    {
+        if (hDecoder->ssr_overlap[channel] == NULL)
+        {
+            hDecoder->ssr_overlap[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
+            memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
+        }
+        if (hDecoder->prev_fmd[channel] == NULL)
+        {
+            uint16_t k;
+            hDecoder->prev_fmd[channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
+            for (k = 0; k < 2*hDecoder->frameLength; k++)
+                hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
+        }
+    }
+#endif
+
+    return 0;
+}
+
+static uint8_t allocate_channel_pair(faacDecHandle hDecoder,
+                                     uint8_t channel, uint8_t paired_channel)
+{
+    uint8_t mul = 1;
+
+#ifdef MAIN_DEC
+    /* MAIN object type prediction */
+    if (hDecoder->object_type == MAIN)
+    {
+        /* allocate the state only when needed */
+        if (hDecoder->pred_stat[channel] == NULL)
+        {
+            hDecoder->pred_stat[channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
+            reset_all_predictors(hDecoder->pred_stat[channel], hDecoder->frameLength);
+        }
+        if (hDecoder->pred_stat[paired_channel] == NULL)
+        {
+            hDecoder->pred_stat[paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
+            reset_all_predictors(hDecoder->pred_stat[paired_channel], hDecoder->frameLength);
+        }
+    }
+#endif
+
+#ifdef LTP_DEC
+    if (is_ltp_ot(hDecoder->object_type))
+    {
+        /* allocate the state only when needed */
+        if (hDecoder->lt_pred_stat[channel] == NULL)
+        {
+            hDecoder->lt_pred_stat[channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
+            memset(hDecoder->lt_pred_stat[channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
+        }
+        if (hDecoder->lt_pred_stat[paired_channel] == NULL)
+        {
+            hDecoder->lt_pred_stat[paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
+            memset(hDecoder->lt_pred_stat[paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
+        }
+    }
+#endif
+
+    if (hDecoder->time_out[channel] == NULL)
+    {
+        mul = 1;
+#ifdef SBR_DEC
+        hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
+        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
+        {
+            /* SBR requires 2 times as much output data */
+            mul = 2;
+            hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
+        }
+#endif
+        hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
+        memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
+    }
+    if (hDecoder->time_out[paired_channel] == NULL)
+    {
+        hDecoder->time_out[paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
+        memset(hDecoder->time_out[paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
+    }
+
+    if (hDecoder->fb_intermed[channel] == NULL)
+    {
+        hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
+        memset(hDecoder->fb_intermed[channel], 0, hDecoder->frameLength*sizeof(real_t));
+    }
+    if (hDecoder->fb_intermed[paired_channel] == NULL)
+    {
+        hDecoder->fb_intermed[paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
+        memset(hDecoder->fb_intermed[paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
+    }
+
+#ifdef SSR_DEC
+    if (hDecoder->object_type == SSR)
+    {
+        if (hDecoder->ssr_overlap[cpe->channel] == NULL)
+        {
+            hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
+            memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
+        }
+        if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
+        {
+            hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
+            memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
+        }
+        if (hDecoder->prev_fmd[cpe->channel] == NULL)
+        {
+            uint16_t k;
+            hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
+            for (k = 0; k < 2*hDecoder->frameLength; k++)
+                hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
+        }
+        if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
+        {
+            uint16_t k;
+            hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
+            for (k = 0; k < 2*hDecoder->frameLength; k++)
+                hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
+        }
+    }
+#endif
+
+    return 0;
+}
+
 uint8_t reconstruct_single_channel(faacDecHandle hDecoder, ic_stream *ics,
                                    element *sce, int16_t *spec_data)
 {
-    uint8_t retval, mul;
+    uint8_t retval;
     ALIGN real_t spec_coef[1024];
 
 #ifdef PROFILE
@@ -698,6 +875,16 @@
     int64_t count = faad_get_ts();
 #endif
 
+    if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
+    {
+        retval = allocate_single_channel(hDecoder, sce->channel);
+        if (retval > 0)
+            return retval;
+
+        hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
+    }
+
+
     /* inverse quantization */
     retval = inverse_quantization(spec_coef, spec_data, hDecoder->frameLength);
     if (retval > 0)
@@ -727,13 +914,6 @@
     /* MAIN object type prediction */
     if (hDecoder->object_type == MAIN)
     {
-        /* allocate the state only when needed */
-        if (hDecoder->pred_stat[sce->channel] == NULL)
-        {
-            hDecoder->pred_stat[sce->channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
-            reset_all_predictors(hDecoder->pred_stat[sce->channel], hDecoder->frameLength);
-        }
-
         /* intra channel prediction */
         ic_prediction(ics, spec_coef, hDecoder->pred_stat[sce->channel], hDecoder->frameLength,
             hDecoder->sf_index);
@@ -761,13 +941,6 @@
         }
 #endif
 
-        /* allocate the state only when needed */
-        if (hDecoder->lt_pred_stat[sce->channel] == NULL)
-        {
-            hDecoder->lt_pred_stat[sce->channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
-            memset(hDecoder->lt_pred_stat[sce->channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
-        }
-
         /* long term prediction */
         lt_prediction(ics, &(ics->ltp), spec_coef, hDecoder->lt_pred_stat[sce->channel], hDecoder->fb,
             ics->window_shape, hDecoder->window_shape_prev[sce->channel],
@@ -787,28 +960,6 @@
     }
 
 
-    if (hDecoder->time_out[sce->channel] == NULL)
-    {
-        mul = 1;
-#ifdef SBR_DEC
-        hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
-        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
-        {
-            /* SBR requires 2 times as much output data */
-            mul = 2;
-            hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
-        }
-#endif
-        hDecoder->time_out[sce->channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
-        memset(hDecoder->time_out[sce->channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
-    }
-
-    if (hDecoder->fb_intermed[sce->channel] == NULL)
-    {
-        hDecoder->fb_intermed[sce->channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
-        memset(hDecoder->fb_intermed[sce->channel], 0, hDecoder->frameLength*sizeof(real_t));
-    }
-
     /* filter bank */
 #ifdef SSR_DEC
     if (hDecoder->object_type != SSR)
@@ -826,19 +977,6 @@
 #endif
 #ifdef SSR_DEC
     } else {
-        if (hDecoder->ssr_overlap[sce->channel] == NULL)
-        {
-            hDecoder->ssr_overlap[sce->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
-            memset(hDecoder->ssr_overlap[sce->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
-        }
-        if (hDecoder->prev_fmd[sce->channel] == NULL)
-        {
-            uint16_t k;
-            hDecoder->prev_fmd[sce->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
-            for (k = 0; k < 2*hDecoder->frameLength; k++)
-                hDecoder->prev_fmd[sce->channel][k] = REAL_CONST(-1);
-        }
-
         ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
             hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
             hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
@@ -892,7 +1030,7 @@
 uint8_t reconstruct_channel_pair(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
                                  element *cpe, int16_t *spec_data1, int16_t *spec_data2)
 {
-    uint8_t retval, mul;
+    uint8_t retval;
     ALIGN real_t spec_coef1[1024];
     ALIGN real_t spec_coef2[1024];
 
@@ -899,7 +1037,15 @@
 #ifdef PROFILE
     int64_t count = faad_get_ts();
 #endif
+    if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
+    {
+        retval = allocate_channel_pair(hDecoder, cpe->channel, cpe->paired_channel);
+        if (retval > 0)
+            return retval;
 
+        hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
+    }
+
     /* inverse quantization */
     retval = inverse_quantization(spec_coef1, spec_data1, hDecoder->frameLength);
     if (retval > 0)
@@ -949,18 +1095,6 @@
     /* MAIN object type prediction */
     if (hDecoder->object_type == MAIN)
     {
-        /* allocate the state only when needed */
-        if (hDecoder->pred_stat[cpe->channel] == NULL)
-        {
-            hDecoder->pred_stat[cpe->channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
-            reset_all_predictors(hDecoder->pred_stat[cpe->channel], hDecoder->frameLength);
-        }
-        if (hDecoder->pred_stat[cpe->paired_channel] == NULL)
-        {
-            hDecoder->pred_stat[cpe->paired_channel] = (pred_state*)faad_malloc(hDecoder->frameLength * sizeof(pred_state));
-            reset_all_predictors(hDecoder->pred_stat[cpe->paired_channel], hDecoder->frameLength);
-        }
-
         /* intra channel prediction */
         ic_prediction(ics1, spec_coef1, hDecoder->pred_stat[cpe->channel], hDecoder->frameLength,
             hDecoder->sf_index);
@@ -999,18 +1133,6 @@
         }
 #endif
 
-        /* allocate the state only when needed */
-        if (hDecoder->lt_pred_stat[cpe->channel] == NULL)
-        {
-            hDecoder->lt_pred_stat[cpe->channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
-            memset(hDecoder->lt_pred_stat[cpe->channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
-        }
-        if (hDecoder->lt_pred_stat[cpe->paired_channel] == NULL)
-        {
-            hDecoder->lt_pred_stat[cpe->paired_channel] = (int16_t*)faad_malloc(hDecoder->frameLength*4 * sizeof(int16_t));
-            memset(hDecoder->lt_pred_stat[cpe->paired_channel], 0, hDecoder->frameLength*4 * sizeof(int16_t));
-        }
-
         /* long term prediction */
         lt_prediction(ics1, ltp1, spec_coef1, hDecoder->lt_pred_stat[cpe->channel], hDecoder->fb,
             ics1->window_shape, hDecoder->window_shape_prev[cpe->channel],
@@ -1036,38 +1158,6 @@
             drc_decode(hDecoder->drc, spec_coef2);
     }
 
-    if (hDecoder->time_out[cpe->channel] == NULL)
-    {
-        mul = 1;
-#ifdef SBR_DEC
-        hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 0;
-        if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
-        {
-            /* SBR requires 2 times as much output data */
-            mul = 2;
-            hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
-        }
-#endif
-        hDecoder->time_out[cpe->channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
-        memset(hDecoder->time_out[cpe->channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
-    }
-    if (hDecoder->time_out[cpe->paired_channel] == NULL)
-    {
-        hDecoder->time_out[cpe->paired_channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
-        memset(hDecoder->time_out[cpe->paired_channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
-    }
-
-    if (hDecoder->fb_intermed[cpe->channel] == NULL)
-    {
-        hDecoder->fb_intermed[cpe->channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
-        memset(hDecoder->fb_intermed[cpe->channel], 0, hDecoder->frameLength*sizeof(real_t));
-    }
-    if (hDecoder->fb_intermed[cpe->paired_channel] == NULL)
-    {
-        hDecoder->fb_intermed[cpe->paired_channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
-        memset(hDecoder->fb_intermed[cpe->paired_channel], 0, hDecoder->frameLength*sizeof(real_t));
-    }
-
     /* filter bank */
 #ifdef SSR_DEC
     if (hDecoder->object_type != SSR)
@@ -1092,31 +1182,6 @@
 #endif
 #ifdef SSR_DEC
     } else {
-        if (hDecoder->ssr_overlap[cpe->channel] == NULL)
-        {
-            hDecoder->ssr_overlap[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
-            memset(hDecoder->ssr_overlap[cpe->channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
-        }
-        if (hDecoder->ssr_overlap[cpe->paired_channel] == NULL)
-        {
-            hDecoder->ssr_overlap[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
-            memset(hDecoder->ssr_overlap[cpe->paired_channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
-        }
-        if (hDecoder->prev_fmd[cpe->channel] == NULL)
-        {
-            uint16_t k;
-            hDecoder->prev_fmd[cpe->channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
-            for (k = 0; k < 2*hDecoder->frameLength; k++)
-                hDecoder->prev_fmd[cpe->channel][k] = REAL_CONST(-1);
-        }
-        if (hDecoder->prev_fmd[cpe->paired_channel] == NULL)
-        {
-            uint16_t k;
-            hDecoder->prev_fmd[cpe->paired_channel] = (real_t*)faad_malloc(2*hDecoder->frameLength*sizeof(real_t));
-            for (k = 0; k < 2*hDecoder->frameLength; k++)
-                hDecoder->prev_fmd[cpe->paired_channel][k] = REAL_CONST(-1);
-        }
-
         ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
             hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
             hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
--- a/libfaad/structs.h
+++ b/libfaad/structs.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: structs.h,v 1.29 2004/01/16 20:20:32 menno Exp $
+** $Id: structs.h,v 1.30 2004/01/28 19:17:26 menno Exp $
 **/
 
 #ifndef __STRUCTS_H__
@@ -386,6 +386,7 @@
 
     void *sample_buffer;
     uint8_t alloced_channels;
+    uint8_t element_alloced[MAX_SYNTAX_ELEMENTS];
 
     uint8_t window_shape_prev[MAX_CHANNELS];
 #ifdef LTP_DEC
--- a/libfaad/syntax.c
+++ b/libfaad/syntax.c
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: syntax.c,v 1.68 2004/01/16 20:20:32 menno Exp $
+** $Id: syntax.c,v 1.69 2004/01/28 19:17:26 menno Exp $
 **/
 
 /*
@@ -328,11 +328,6 @@
         return;
     }
 
-    if (hDecoder->pce_set)
-        hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
-    else
-        hDecoder->internal_channel[channels] = channels;
-
     if (id_syn_ele == ID_SCE)
         hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
     else /* LFE */
@@ -341,6 +336,11 @@
 
     hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
 
+    if (hDecoder->pce_set)
+        hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
+    else
+        hDecoder->internal_channel[channels] = channels;
+
     hDecoder->fr_channels++;
     hDecoder->fr_ch_ele++;
 }
@@ -362,6 +362,12 @@
         return;
     }
 
+    hDecoder->channel_element[channels] = hDecoder->fr_ch_ele;
+    hDecoder->channel_element[channels+1] = hDecoder->fr_ch_ele;
+    hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
+
+    hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
+
     if (hDecoder->pce_set)
     {
         hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
@@ -371,12 +377,6 @@
         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->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
-
-    hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
-
     hDecoder->fr_channels += 2;
     hDecoder->fr_ch_ele++;
 }
@@ -385,7 +385,6 @@
                     bitfile *ld, 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;
@@ -404,7 +403,6 @@
             case ID_SCE:
                 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
                 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
-                ch_ele++;
                 if (hInfo->error > 0)
                     return;
                 break;
@@ -411,7 +409,6 @@
             case ID_CPE:
                 if (hDecoder->first_syn_ele == 25) hDecoder->first_syn_ele = id_syn_ele;
                 decode_cpe(hDecoder, hInfo, ld, id_syn_ele);
-                ch_ele++;
                 if (hInfo->error > 0)
                     return;
                 break;
@@ -418,7 +415,6 @@
             case ID_LFE:
                 hDecoder->has_lfe++;
                 decode_sce_lfe(hDecoder, hInfo, ld, id_syn_ele);
-                ch_ele++;
                 if (hInfo->error > 0)
                     return;
                 break;
@@ -448,7 +444,6 @@
                 /* SBR data will be read directly in the SCE/LFE/CPE element */
                 if ((hInfo->error = fill_element(hDecoder, ld, drc
 #ifdef SBR_DEC
-                    //, (ch_ele == 0) ? INVALID_SBR_ELEMENT : (ch_ele-1)
                     , INVALID_SBR_ELEMENT
 #endif
                     )) > 0)
@@ -463,67 +458,48 @@
         {
         case 1:
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
         case 2:
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
         case 3:
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
         case 4:
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
         case 5:
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
         case 6:
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
-        case 7:
+        case 7: /* 8 channels */
             decode_sce_lfe(hDecoder, hInfo, ld, ID_SCE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_cpe(hDecoder, hInfo, ld, ID_CPE);
-            ch_ele++;
             decode_sce_lfe(hDecoder, hInfo, ld, ID_LFE);
-            ch_ele++;
             if (hInfo->error > 0)
                 return;
             break;
@@ -1624,9 +1600,7 @@
 
                 /* decode intensity position */
                 t = huffman_scale_factor(ld);
-                if (t < 0)
-                    return 9;
-                is_position += (t - 60);
+                is_position += t;
                 ics->scale_factors[g][sfb] = is_position;
 
                 break;
@@ -1640,9 +1614,6 @@
                         DEBUGVAR(1,73,"scale_factor_data(): first noise")) - 256;
                 } else {
                     t = huffman_scale_factor(ld);
-                    if (t < 0)
-                        return 9;
-                    t -= 60;
                 }
                 noise_energy += t;
                 ics->scale_factors[g][sfb] = noise_energy;
@@ -1656,9 +1627,7 @@
 
                 /* decode scale factor */
                 t = huffman_scale_factor(ld);
-                if (t < 0)
-                    return 9;
-                scale_factor += (t - 60);
+                scale_factor += t;
                 if (scale_factor < 0 || scale_factor > 255)
                     return 4;
                 ics->scale_factors[g][sfb] = scale_factor;
--- a/libfaad/syntax.h
+++ b/libfaad/syntax.h
@@ -22,7 +22,7 @@
 ** Commercial non-GPL licensing of this software is possible.
 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
 **
-** $Id: syntax.h,v 1.46 2004/01/16 20:20:32 menno Exp $
+** $Id: syntax.h,v 1.47 2004/01/28 19:17:26 menno Exp $
 **/
 
 #ifndef __SYNTAX_H__