--- libfaad2.orig/bits.h	2006-03-16 20:15:04.000000000 +0100
+++ libfaad2/bits.h	2006-03-16 20:37:21.000000000 +0100
@@ -58,7 +61,7 @@
 
 #if defined (_WIN32) && !defined(_WIN32_WCE) && !defined(__MINGW32__)
 #define BSWAP(a) __asm mov eax,a __asm bswap eax __asm mov a, eax
-#elif defined(LINUX) || defined(DJGPP) || defined(__MINGW32__)
+#elif defined(LINUX) || defined(DJGPP)
 #define BSWAP(a) __asm__ ( "bswapl %0\n" : "=r" (a) : "0" (a) )
 #else
 #define BSWAP(a) \
--- libfaad2.orig/common.h	2006-03-16 20:15:04.000000000 +0100
+++ libfaad2/common.h	2006-03-16 20:56:56.000000000 +0100
@@ -32,8 +37,13 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "../config.h"
 #endif
+
+/* Allow build on Cygwin*/
+#if defined(__CYGWIN__)
+#define __STRICT_ANSI__
+#endif
 
 #define INLINE __inline
 #if 0 //defined(_WIN32) && !defined(_WIN32_WCE)
 #define ALIGN __declspec(align(16))
@@ -61,7 +69,7 @@
 /* Use if target platform has address generators with autoincrement */
 //#define PREFER_POINTERS
 
-#ifdef _WIN32_WCE
+#if defined(_WIN32_WCE) || defined(__arm__) || defined(__avr32__)
 #define FIXED_POINT
 #endif
 
@@ -117,6 +125,9 @@
 # ifdef MAIN_DEC
 #  undef MAIN_DEC
 # endif
+# ifdef SBR_DEC
+#  undef SBR_DEC
+# endif
 #endif // FIXED_POINT
 
 #ifdef DRM
@@ -151,6 +162,7 @@
 
 #include <stdlib.h>
 
+#if 0
 typedef unsigned __int64 uint64_t;
 typedef unsigned __int32 uint32_t;
 typedef unsigned __int16 uint16_t;
@@ -159,5 +171,9 @@
 typedef __int32 int32_t;
 typedef __int16 int16_t;
 typedef __int8  int8_t;
+#else
+#include <inttypes.h>
+#endif
+
 typedef float float32_t;
 
@@ -245,7 +245,7 @@
 
 #endif
 
-#ifdef WORDS_BIGENDIAN
+#if HAVE_BIGENDIAN
 #define ARCH_IS_BIG_ENDIAN
 #endif
 
@@ -317,7 +317,7 @@
         }
         return i;
     }
-  #elif (defined(__i386__) && defined(__GNUC__))
+  #elif (defined(__i386__) && defined(__GNUC__)) && !defined(__STDC_VERSION__)
     #define HAS_LRINTF
     // from http://www.stereopsis.com/FPU.html
     static INLINE int lrintf(float f)
@@ -330,5 +346,7 @@
 
   #else
 
+#include <math.h>
+
 #ifdef HAVE_LRINTF
 #  define HAS_LRINTF
@@ -338,8 +356,6 @@
 #  define __USE_ISOC99   1
 #endif
 
-    #include <math.h>
-
 #ifdef HAVE_SINF
 #  define sin sinf
 #error
--- libfaad2.orig/output.c	2006-03-16 20:15:04.000000000 +0100
+++ libfaad2/output.c	2006-04-18 19:50:26.000000000 +0200
@@ -463,9 +462,9 @@
     }
 }
 
-void* output_to_PCM(NeAACDecHandle hDecoder,
-                    real_t **input, void *sample_buffer, uint8_t channels,
-                    uint16_t frame_len, uint8_t format)
+static void* output_to_PCM_sux(NeAACDecHandle hDecoder,
+                               real_t **input, void *sample_buffer, uint8_t channels,
+                               uint16_t frame_len, uint8_t format)
 {
     uint8_t ch;
     uint16_t i;
@@ -554,4 +553,51 @@
     return sample_buffer;
 }
 
+void* output_to_PCM(NeAACDecHandle hDecoder,
+                    real_t **input, void *sample_buffer, uint8_t channels,
+                    uint16_t frame_len, uint8_t format)
+{
+    int ch;
+    int i;
+    int16_t *short_sample_buffer = (int16_t*)sample_buffer;
+    real_t *ch0 = input[hDecoder->internal_channel[0]];
+    real_t *ch1 = input[hDecoder->internal_channel[1]];
+    real_t *ch2 = input[hDecoder->internal_channel[2]];
+    real_t *ch3 = input[hDecoder->internal_channel[3]];
+    real_t *ch4 = input[hDecoder->internal_channel[4]];
+
+    if (format != FAAD_FMT_16BIT)
+        return output_to_PCM_sux(hDecoder, input, sample_buffer, channels, frame_len, format);
+
+    if (hDecoder->downMatrix) {
+        for(i = 0; i < frame_len; i++)
+        {
+	    int32_t tmp;
+	    tmp = (ch1[i] + ((ch0[i]+ch3[i])>>1) + ((ch0[i]+ch3[i])>>2) + (1<<(REAL_BITS))) >> (REAL_BITS+1);
+	    if ((tmp+0x8000) & ~0xffff) tmp = ~(tmp>>31)-0x8000;
+            short_sample_buffer[0] = tmp;
+	    tmp = (ch2[i] + ((ch0[i]+ch4[i])>>1) + ((ch0[i]+ch4[i])>>2) + (1<<(REAL_BITS))) >> (REAL_BITS+1);
+	    if ((tmp+0x8000) & ~0xffff) tmp = ~(tmp>>31)-0x8000;
+            short_sample_buffer[1] = tmp;
+	    short_sample_buffer += channels;
+        }
+        return sample_buffer;
+    }
+
+    /* Copy output to a standard PCM buffer */
+    for(i = 0; i < frame_len; i++)
+    {
+        for (ch = 0; ch < channels; ch++)
+        {
+            int32_t tmp = input[hDecoder->internal_channel[ch]][i];
+            tmp += (1 << (REAL_BITS-1));
+            tmp >>= REAL_BITS;
+	    if ((tmp+0x8000) & ~0xffff) tmp = ~(tmp>>31)-0x8000;
+            *(short_sample_buffer++) = tmp;
+        }
+    }
+
+    return sample_buffer;
+}
+
 #endif
--- libfaad2.orig/ps_dec.c	2006-03-16 20:15:04.000000000 +0100
+++ libfaad2/ps_dec.c	2006-04-18 20:29:38.000000000 +0200
@@ -159,7 +162,7 @@
 
 /* static function declarations */
 static void ps_data_decode(ps_info *ps);
-static hyb_info *hybrid_init();
+static hyb_info *hybrid_init(void);
 static void channel_filter2(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
                             qmf_t *buffer, qmf_t **X_hybrid);
 static void INLINE DCT3_4_unscaled(real_t *y, real_t *x);
@@ -189,7 +192,7 @@
 /*  */
 
 
-static hyb_info *hybrid_init()
+static hyb_info *hybrid_init(void)
 {
     uint8_t i;
 
@@ -1935,8 +1938,8 @@
 /* main Parametric Stereo decoding function */
 uint8_t ps_decode(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64])
 {
-    qmf_t X_hybrid_left[32][32] = {{0}};
-    qmf_t X_hybrid_right[32][32] = {{0}};
+    qmf_t X_hybrid_left[32][32] = {{{0}}};
+    qmf_t X_hybrid_right[32][32] = {{{0}}};
 
     /* delta decoding of the bitstream data */
     ps_data_decode(ps);
--- libfaad2.orig/sbr_dec.c	2006-03-16 20:15:04.000000000 +0100
+++ libfaad2/sbr_dec.c	2006-04-18 20:33:57.000000000 +0200
@@ -526,8 +529,8 @@
     uint8_t l, k;
     uint8_t dont_process = 0;
     uint8_t ret = 0;
-    ALIGN qmf_t X_left[38][64] = {{0}};
-    ALIGN qmf_t X_right[38][64] = {{0}}; /* must set this to 0 */
+    ALIGN qmf_t X_left[38][64] = {{{0}}};
+    ALIGN qmf_t X_right[38][64] = {{{0}}}; /* must set this to 0 */
 
     if (sbr == NULL)
         return 20;
--- libfaad2.orig/specrec.c	2006-03-16 20:15:04.000000000 +0100
+++ libfaad2/specrec.c	2006-04-18 20:38:09.000000000 +0200
@@ -673,29 +673,19 @@
     /* 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));
+            hDecoder->pred_stat[channel] = (pred_state*)realloc(hDecoder->pred_stat[channel], 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));
+            hDecoder->lt_pred_stat[channel] = (int16_t*)realloc(hDecoder->lt_pred_stat[channel], 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;
@@ -706,41 +696,28 @@
             hDecoder->sbr_alloced[hDecoder->fr_ch_ele] = 1;
         }
 #endif
-        hDecoder->time_out[channel] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
+        hDecoder->time_out[channel] = (real_t*)realloc(hDecoder->time_out[channel], mul*hDecoder->frameLength*sizeof(real_t));
         memset(hDecoder->time_out[channel], 0, mul*hDecoder->frameLength*sizeof(real_t));
-    }
 #if (defined(PS_DEC) || defined(DRM_PS))
     if (output_channels == 2)
     {
-        if (hDecoder->time_out[channel+1] == NULL)
-        {
-            hDecoder->time_out[channel+1] = (real_t*)faad_malloc(mul*hDecoder->frameLength*sizeof(real_t));
+            hDecoder->time_out[channel+1] = (real_t*)realloc(hDecoder->time_out[channel+1], mul*hDecoder->frameLength*sizeof(real_t));
             memset(hDecoder->time_out[channel+1], 0, mul*hDecoder->frameLength*sizeof(real_t));
-        }
     }
 #endif
 
-    if (hDecoder->fb_intermed[channel] == NULL)
-    {
-        hDecoder->fb_intermed[channel] = (real_t*)faad_malloc(hDecoder->frameLength*sizeof(real_t));
+        hDecoder->fb_intermed[channel] = (real_t*)realloc(hDecoder->fb_intermed[channel], 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));
+            hDecoder->ssr_overlap[channel] = (real_t*)realloc(hDecoder->ssr_overlap[channel], 2*hDecoder->frameLength*sizeof(real_t));
+            memset(hDecoder->ssr_overlap[channel], 0, 2*hDecoder->frameLength*sizeof(real_t));
+            hDecoder->prev_fmd[channel] = (real_t*)realloc(hDecoder->prev_fmd[channel], 2*hDecoder->frameLength*sizeof(real_t));
             for (k = 0; k < 2*hDecoder->frameLength; k++)
                 hDecoder->prev_fmd[channel][k] = REAL_CONST(-1);
-        }
     }
 #endif
 
@@ -865,22 +842,14 @@
 
     /* always allocate 2 channels, PS can always "suddenly" turn up */
 #if (defined(PS_DEC) || defined(DRM_PS))
-    output_channels = 2;
+    output_channels = hDecoder->ps_used[hDecoder->fr_ch_ele] ? 2 : 1;
 #else
     output_channels = 1;
 #endif
 
-    if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
-    {
-        /* element_output_channels not set yet */
+    if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0 ||
+        hDecoder->element_output_channels[hDecoder->fr_ch_ele] < output_channels) {
         hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
-    } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
-        /* element inconsistency */
-        return 21;
-    }
-
-    if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
-    {
         retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
         if (retval > 0)
             return retval;
@@ -1026,11 +995,10 @@
     {
         return 23;
     }
-#endif
 
     /* copy L to R when no PS is used */
 #if (defined(PS_DEC) || defined(DRM_PS))
-    if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0))
+    if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0) && (output_channels == 2))
     {
         uint8_t ele = hDecoder->fr_ch_ele;
         uint8_t ch = sce->channel;
@@ -1040,6 +1008,7 @@
         memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
     }
 #endif
+#endif
 
     return 0;
 }
