shithub: aacdec

Download patch

ref: 7193768ac9ff3236ce7615b866f6d0e0ba53c5dc
parent: 2d236187d50077313e38fe0e7b0e178eb60f5218
author: menno <menno>
date: Fri Nov 1 06:19:36 EST 2002

Small changes to the decoder interface:
every function that has a buffer as input now also has a parameter to give the size of that buffer.

--- a/aacDECdrop/aacDECdrop/aacDECdrop.vcproj
+++ b/aacDECdrop/aacDECdrop/aacDECdrop.vcproj
@@ -30,6 +30,7 @@
 				OptimizeForWindowsApplication="TRUE"
 				AdditionalIncludeDirectories="..\..\include,..\..\common\libsndfile\src,..\..\common\mp4v2,..\..\common\libsndfile\win32"
 				PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
+				StringPooling="TRUE"
 				RuntimeLibrary="0"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\Release/aacDECdrop.pch"
@@ -48,7 +49,7 @@
 				OutputFile=".\Release/aacDECdrop.exe"
 				LinkIncremental="1"
 				SuppressStartupBanner="TRUE"
-				IgnoreDefaultLibraryNames="LIBCMT.lib"
+				IgnoreDefaultLibraryNames=""
 				ProgramDatabaseFile=".\Release/aacDECdrop.pdb"
 				SubSystem="2"/>
 			<Tool
--- a/aacDECdrop/decode.c
+++ b/aacDECdrop/decode.c
@@ -16,8 +16,8 @@
 ** along with this program; if not, write to the Free Software
 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **
-** $Id: decode.c,v 1.10 2002/08/15 17:32:51 menno Exp $
-** $Id: decode.c,v 1.10 2002/08/15 17:32:51 menno Exp $
+** $Id: decode.c,v 1.11 2002/11/01 11:19:34 menno Exp $
+** $Id: decode.c,v 1.11 2002/11/01 11:19:34 menno Exp $
 **/
 
 #ifdef _WIN32
@@ -50,7 +50,8 @@
 #define DEC_BUFF_VARS \
     int fileread, bytesconsumed, k; \
     int buffercount = 0, buffer_index = 0; \
-    unsigned char *buffer;
+    unsigned char *buffer; \
+    unsigned int bytes_in_buffer = 0;
 
 /* initialise buffering */
 #define INIT_BUFF(file) \
@@ -59,7 +60,7 @@
     fseek(file, 0, SEEK_SET); \
     buffer = (unsigned char*)malloc(FAAD_MIN_STREAMSIZE*MAX_CHANNELS); \
     memset(buffer, 0, FAAD_MIN_STREAMSIZE*MAX_CHANNELS); \
-    fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, file);
+    bytes_in_buffer = fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, file);
 
 /* skip bytes in buffer */
 #define UPDATE_BUFF_SKIP(bytes) \
@@ -66,7 +67,7 @@
     fseek(infile, bytes, SEEK_SET); \
     buffer_index += bytes; \
     buffercount = 0; \
-    fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, infile);
+    bytes_in_buffer = fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, infile);
 
 /* update buffer */
 #define UPDATE_BUFF_READ \
@@ -73,7 +74,7 @@
     if (bytesconsumed > 0) { \
         for (k = 0; k < (FAAD_MIN_STREAMSIZE*MAX_CHANNELS - bytesconsumed); k++) \
             buffer[k] = buffer[k + bytesconsumed]; \
-        fread(buffer + (FAAD_MIN_STREAMSIZE*MAX_CHANNELS) - bytesconsumed, 1, bytesconsumed, infile); \
+        bytes_in_buffer += fread(buffer + (FAAD_MIN_STREAMSIZE*MAX_CHANNELS) - bytesconsumed, 1, bytesconsumed, infile); \
         bytesconsumed = 0; \
     }
 
@@ -80,7 +81,8 @@
 /* update buffer indices after faacDecDecode */
 #define UPDATE_BUFF_IDX(frame) \
     bytesconsumed += frame.bytesconsumed; \
-    buffer_index += frame.bytesconsumed;
+    buffer_index += frame.bytesconsumed; \
+    bytes_in_buffer -= frame.bytesconsumed;
 
 /* true if decoding has to stop because of EOF */
 #define IS_FILE_END buffer_index >= fileread
@@ -169,7 +171,8 @@
 
     faacDecSetConfiguration(hDecoder, config);
 
-    if((bytesconsumed = faacDecInit(hDecoder, buffer, &samplerate, &channels)) < 0)
+    if ((bytesconsumed = faacDecInit(hDecoder, buffer, bytes_in_buffer,
+        &samplerate, &channels)) < 0)
     {
         /* If some error initializing occured, skip the file */
         error_handler("Error initializing decoder library.\n");
@@ -185,7 +188,7 @@
         /* update buffer */
         UPDATE_BUFF_READ
 
-        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer);
+        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer, bytes_in_buffer);
 
         /* update buffer indices */
         UPDATE_BUFF_IDX(frameInfo)
@@ -284,8 +287,8 @@
 
             if (buff)
             {
-                rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8, &dummy3_8,
-                    &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
+                rc = AudioSpecificConfig(buff, buff_size, &dummy1_32, &dummy2_8,
+                    &dummy3_8, &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
                 free(buff);
 
                 if (rc < 0)
@@ -376,7 +379,7 @@
             return 1;
         }
 
-        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer);
+        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer, buffer_size);
 
         if (buffer)
             free(buffer);
--- a/common/id3lib/libprj/id3lib.vcproj
+++ b/common/id3lib/libprj/id3lib.vcproj
@@ -70,7 +70,7 @@
 				AdditionalIncludeDirectories=".\,..\,..\include,..\include\id3,..\zlib\include,..\win32"
 				PreprocessorDefinitions="NDEBUG,WIN32,_LIB,_WINDOWS,HAVE_CONFIG_H,ID3LIB_COMPILATION"
 				StringPooling="TRUE"
-				RuntimeLibrary="4"
+				RuntimeLibrary="0"
 				EnableFunctionLevelLinking="TRUE"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\Release/id3lib.pch"
@@ -192,10 +192,10 @@
 				RelativePath="E:\Program Files\Microsoft Visual Studio\VC98\Include\BASETSD.H">
 			</File>
 			<File
-				RelativePath="..\win32\config.h">
+				RelativePath="..\config.h">
 			</File>
 			<File
-				RelativePath="..\config.h">
+				RelativePath="..\win32\config.h">
 			</File>
 			<File
 				RelativePath="..\include\id3\error.h">
--- a/common/id3lib/zlib/prj/zlib.vcproj
+++ b/common/id3lib/zlib/prj/zlib.vcproj
@@ -112,7 +112,7 @@
 				AdditionalIncludeDirectories="..\include"
 				PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
 				StringPooling="TRUE"
-				RuntimeLibrary="4"
+				RuntimeLibrary="0"
 				EnableFunctionLevelLinking="TRUE"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\Release/zlib.pch"
--- a/common/mp4v2/libmp4v2_st60.vcproj
+++ b/common/mp4v2/libmp4v2_st60.vcproj
@@ -23,7 +23,7 @@
 				AdditionalIncludeDirectories=".\"
 				PreprocessorDefinitions="WIN32,NDEBUG,_WINDOWS"
 				StringPooling="TRUE"
-				RuntimeLibrary="4"
+				RuntimeLibrary="0"
 				EnableFunctionLevelLinking="TRUE"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\ST_Release/libmp4v2_st60.pch"
--- a/frontend/faad.vcproj
+++ b/frontend/faad.vcproj
@@ -24,7 +24,7 @@
 				AdditionalIncludeDirectories="../include,../common/mp4v2,../common/faad"
 				PreprocessorDefinitions="WIN32,_DEBUG,_CONSOLE"
 				BasicRuntimeChecks="3"
-				RuntimeLibrary="5"
+				RuntimeLibrary="1"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\Debug/faad.pch"
 				AssemblerListingLocation=".\Debug/"
--- 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.24 2002/09/24 09:23:55 menno Exp $
+** $Id: main.c,v 1.25 2002/11/01 11:19:35 menno Exp $
 **/
 
 #ifdef _WIN32
@@ -47,7 +47,8 @@
 #define DEC_BUFF_VARS \
     int fileread, bytesconsumed, k; \
     int buffercount = 0, buffer_index = 0; \
-    unsigned char *buffer;
+    unsigned char *buffer; \
+    unsigned int bytes_in_buffer = 0;
 
 /* initialise buffering */
 #define INIT_BUFF(file) \
@@ -56,7 +57,7 @@
     fseek(file, 0, SEEK_SET); \
     buffer = (unsigned char*)malloc(FAAD_MIN_STREAMSIZE*MAX_CHANNELS); \
     memset(buffer, 0, FAAD_MIN_STREAMSIZE*MAX_CHANNELS); \
-    fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, file);
+    bytes_in_buffer = fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, file);
 
 /* skip bytes in buffer */
 #define UPDATE_BUFF_SKIP(bytes) \
@@ -63,7 +64,7 @@
     fseek(infile, bytes, SEEK_SET); \
     buffer_index += bytes; \
     buffercount = 0; \
-    fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, infile);
+    bytes_in_buffer = fread(buffer, 1, FAAD_MIN_STREAMSIZE*MAX_CHANNELS, infile);
 
 /* update buffer */
 #define UPDATE_BUFF_READ \
@@ -70,7 +71,7 @@
     if (bytesconsumed > 0) { \
         for (k = 0; k < (FAAD_MIN_STREAMSIZE*MAX_CHANNELS - bytesconsumed); k++) \
             buffer[k] = buffer[k + bytesconsumed]; \
-        fread(buffer + (FAAD_MIN_STREAMSIZE*MAX_CHANNELS) - bytesconsumed, 1, bytesconsumed, infile); \
+        bytes_in_buffer += fread(buffer + (FAAD_MIN_STREAMSIZE*MAX_CHANNELS) - bytesconsumed, 1, bytesconsumed, infile); \
         bytesconsumed = 0; \
     }
 
@@ -77,7 +78,8 @@
 /* update buffer indices after faacDecDecode */
 #define UPDATE_BUFF_IDX(frame) \
     bytesconsumed += frame.bytesconsumed; \
-    buffer_index += frame.bytesconsumed;
+    buffer_index += frame.bytesconsumed; \
+    bytes_in_buffer -= frame.bytesconsumed;
 
 /* true if decoding has to stop because of EOF */
 #define IS_FILE_END buffer_index >= fileread
@@ -204,8 +206,8 @@
 
     faacDecSetConfiguration(hDecoder, config);
 
-    if((bytesconsumed = faacDecInit(hDecoder, buffer, &samplerate,
-        &channels)) < 0)
+    if ((bytesconsumed = faacDecInit(hDecoder, buffer, bytes_in_buffer,
+        &samplerate, &channels)) < 0)
     {
         /* If some error initializing occured, skip the file */
         fprintf(stderr, "Error initializing decoder library.\n");
@@ -221,7 +223,8 @@
         /* update buffer */
         UPDATE_BUFF_READ
 
-        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer);
+        sample_buffer = faacDecDecode(hDecoder, &frameInfo,
+            buffer, bytes_in_buffer);
 
         /* update buffer indices */
         UPDATE_BUFF_IDX(frameInfo)
@@ -309,8 +312,8 @@
 
             if (buff)
             {
-                rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8, &dummy3_8,
-                    &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
+                rc = AudioSpecificConfig(buff, buff_size, &dummy1_32, &dummy2_8,
+                    &dummy3_8, &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
                 free(buff);
 
                 if (rc < 0)
@@ -412,7 +415,7 @@
             return 1;
 		}
 
-        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer);
+        sample_buffer = faacDecDecode(hDecoder, &frameInfo, buffer, buffer_size);
 
         if (buffer) free(buffer);
 
--- a/include/faad.h
+++ b/include/faad.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: faad.h,v 1.15 2002/10/11 18:05:01 menno Exp $
+** $Id: faad.h,v 1.16 2002/11/01 11:19:35 menno Exp $
 **/
 
 #ifndef __AACDEC_H__
@@ -99,6 +99,7 @@
 /* Init the library based on info from the AAC file (ADTS/ADIF) */
 long FAADAPI faacDecInit(faacDecHandle hDecoder,
                         unsigned char *buffer,
+                        unsigned long buffer_size,
                         unsigned long *samplerate,
                         unsigned char *channels);
 
@@ -111,9 +112,11 @@
 
 void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
                             faacDecFrameInfo *hInfo,
-                            unsigned char *buffer);
+                            unsigned char *buffer,
+                            unsigned long buffer_size);
 
 char FAADAPI AudioSpecificConfig(unsigned char *pBuffer,
+                                 unsigned long buffer_size,
                                  unsigned long *samplerate,
                                  unsigned char *channels,
                                  unsigned char *sf_index,
--- a/libfaad/bits.c
+++ b/libfaad/bits.c
@@ -16,19 +16,20 @@
 ** 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.13 2002/10/26 11:43:11 menno Exp $
+** $Id: bits.c,v 1.14 2002/11/01 11:19:35 menno Exp $
 **/
 
 #include "common.h"
 #include <stdlib.h>
+#include <memory.h>
 #include "bits.h"
 
 /* initialize buffer, call once before first getbits or showbits */
-void faad_initbits(bitfile *ld, void *buffer)
+void faad_initbits(bitfile *ld, void *buffer, uint32_t buffer_size)
 {
     uint32_t tmp;
 
-    ld->start = (uint32_t*)buffer;
+    ld->buffer_size = buffer_size;
 
     tmp = *(uint32_t*)buffer;
 #ifndef ARCH_IS_BIG_ENDIAN
@@ -42,8 +43,10 @@
 #endif
     ld->bufb = tmp;
 
-    ld->bits_left = 32;
+    ld->start = (uint32_t*)buffer;
     ld->tail = ((uint32_t*)buffer + 2);
+
+    ld->bits_left = 32;
 }
 
 uint32_t faad_get_processed_bits(bitfile *ld)
@@ -94,6 +97,8 @@
 {
     uint32_t tmp;
     int32_t index;
+
+    ld->buffer_size = bit2byte(bits_in_buffer);
 
     index = (bits_in_buffer+31)/32 - 1;
 
--- 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.10 2002/09/28 21:57:37 menno Exp $
+** $Id: bits.h,v 1.11 2002/11/01 11:19:35 menno Exp $
 **/
 
 #ifndef __BITS_H__
@@ -40,6 +40,7 @@
     uint32_t bufa;
     uint32_t bufb;
     uint32_t bits_left;
+    uint32_t buffer_size; /* size of the buffer in bytes */
     uint32_t *tail;
     uint32_t *start;
 } bitfile;
@@ -62,7 +63,7 @@
     0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF
 };
 
-void faad_initbits(bitfile *ld, void *buffer);
+void faad_initbits(bitfile *ld, void *buffer, uint32_t buffer_size);
 void faad_initbits_rev(bitfile *ld, void *buffer,
                        uint32_t bits_in_buffer);
 uint8_t faad_byte_align(bitfile *ld);
--- 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.37 2002/10/16 19:18:09 menno Exp $
+** $Id: decoder.c,v 1.38 2002/11/01 11:19:35 menno Exp $
 **/
 
 #include <stdlib.h>
@@ -123,6 +123,7 @@
 }
 
 int32_t FAADAPI faacDecInit(faacDecHandle hDecoder, uint8_t *buffer,
+                            uint32_t buffer_size,
                         uint32_t *samplerate, uint8_t *channels)
 {
     uint32_t bits = 0;
@@ -137,7 +138,7 @@
 
     if (buffer != NULL)
     {
-        faad_initbits(&ld, buffer);
+        faad_initbits(&ld, buffer, buffer_size);
 
 #ifdef DRM
         if (hDecoder->object_type != DRM_ER_LC)
@@ -213,7 +214,8 @@
         return -1;
     }
 
-    rc = AudioSpecificConfig(pBuffer, samplerate, channels,
+    rc = AudioSpecificConfig(pBuffer, SizeOfDecoderSpecificInfo,
+        samplerate, channels,
         &hDecoder->sf_index, &hDecoder->object_type,
 #ifdef ERROR_RESILIENCE
         &hDecoder->aacSectionDataResilienceFlag,
@@ -281,7 +283,7 @@
 
 void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
                             faacDecFrameInfo *hInfo,
-                            uint8_t *buffer)
+                            uint8_t *buffer, uint32_t buffer_size)
 {
     int32_t i;
     uint8_t ch;
@@ -334,7 +336,7 @@
     memset(hInfo, 0, sizeof(faacDecFrameInfo));
 
     /* initialize the bitstream */
-    faad_initbits(ld, buffer);
+    faad_initbits(ld, buffer, buffer_size);
 
     if (hDecoder->adts_header_present)
     {
--- 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.16 2002/09/27 08:37:22 menno Exp $
+** $Id: decoder.h,v 1.17 2002/11/01 11:19:35 menno Exp $
 **/
 
 #ifndef __DECODER_H__
@@ -124,9 +124,10 @@
 
 /* Init the library based on info from the AAC file (ADTS/ADIF) */
 int32_t FAADAPI faacDecInit(faacDecHandle hDecoder,
-                        uint8_t *buffer,
-                        uint32_t *samplerate,
-                        uint8_t *channels);
+                            uint8_t *buffer,
+                            uint32_t buffer_size,
+                            uint32_t *samplerate,
+                            uint8_t *channels);
 
 /* Init the library using a DecoderSpecificInfo */
 int8_t FAADAPI faacDecInit2(faacDecHandle hDecoder, uint8_t *pBuffer,
@@ -137,7 +138,8 @@
 
 void* FAADAPI faacDecDecode(faacDecHandle hDecoder,
                             faacDecFrameInfo *hInfo,
-                            uint8_t *buffer);
+                            uint8_t *buffer,
+                            uint32_t buffer_size);
 
 /* these functions are in syntax.c */
 element *decode_sce_lfe(faacDecHandle hDecoder,
--- a/libfaad/libfaad.vcproj
+++ b/libfaad/libfaad.vcproj
@@ -70,7 +70,7 @@
 				AdditionalIncludeDirectories="fftw"
 				PreprocessorDefinitions="NDEBUG,WIN32,_LIB,FFTW_ENABLE_FLOAT"
 				StringPooling="TRUE"
-				RuntimeLibrary="4"
+				RuntimeLibrary="0"
 				EnableFunctionLevelLinking="TRUE"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\Release/libfaad.pch"
@@ -250,9 +250,6 @@
 				RelativePath=".\error.h">
 			</File>
 			<File
-				RelativePath=".\fftw\f77_func.h">
-			</File>
-			<File
 				RelativePath=".\filtbank.h">
 			</File>
 			<File
@@ -293,21 +290,6 @@
 			</File>
 			<File
 				RelativePath=".\rvlc_scale_factors.h">
-			</File>
-			<File
-				RelativePath=".\sbr_dec.h">
-			</File>
-			<File
-				RelativePath=".\sbr_huff.h">
-			</File>
-			<File
-				RelativePath=".\sbr_qmf.h">
-			</File>
-			<File
-				RelativePath=".\sbr_syntax.h">
-			</File>
-			<File
-				RelativePath=".\sbr_util.h">
 			</File>
 			<File
 				RelativePath=".\specrec.h">
--- 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.11 2002/09/27 08:37:22 menno Exp $
+** $Id: mp4.c,v 1.12 2002/11/01 11:19:35 menno Exp $
 **/
 
 #include "common.h"
@@ -95,6 +95,7 @@
 
 /* Table 1.6.1 */
 int8_t FAADAPI AudioSpecificConfig(uint8_t *pBuffer,
+                                   uint32_t buffer_size,
                                    uint32_t *samplerate,
                                    uint8_t *channels,
                                    uint8_t *sf_index,
@@ -107,7 +108,7 @@
     bitfile ld;
     uint8_t ObjectTypeIndex, SamplingFrequencyIndex, ChannelsConfiguration;
 
-    faad_initbits(&ld, pBuffer);
+    faad_initbits(&ld, pBuffer, buffer_size);
     faad_byte_align(&ld);
 
     ObjectTypeIndex = (uint8_t)faad_getbits(&ld, 5
--- a/libfaad/mp4.h
+++ b/libfaad/mp4.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: mp4.h,v 1.5 2002/06/13 11:03:27 menno Exp $
+** $Id: mp4.h,v 1.6 2002/11/01 11:19:36 menno Exp $
 **/
 
 #ifndef __MP4_H__
@@ -29,6 +29,7 @@
 #include "decoder.h"
 
 int8_t FAADAPI AudioSpecificConfig(uint8_t *pBuffer,
+                                   uint32_t buffer_size,
                                    uint32_t *samplerate,
                                    uint8_t *channels,
                                    uint8_t *sf_index,
--- a/libfaad/rvlc_scale_factors.c
+++ b/libfaad/rvlc_scale_factors.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: rvlc_scale_factors.c,v 1.5 2002/09/29 10:34:00 menno Exp $
+** $Id: rvlc_scale_factors.c,v 1.6 2002/11/01 11:19:36 menno Exp $
 **/
 
 /* RVLC scalefactor decoding
@@ -102,7 +102,7 @@
         rvlc_sf_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_sf
             DEBUGVAR(1,156,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_sf"));
 
-        faad_initbits(&ld_rvlc_sf, (void*)rvlc_sf_buffer);
+        faad_initbits(&ld_rvlc_sf, (void*)rvlc_sf_buffer, bit2byte(ics->length_of_rvlc_sf));
 //        faad_initbits_rev(&ld_rvlc_sf_rev, (void*)rvlc_sf_buffer,
 //            ics->length_of_rvlc_sf);
     }
@@ -115,7 +115,7 @@
         rvlc_esc_buffer = faad_getbitbuffer(ld, ics->length_of_rvlc_escapes
             DEBUGVAR(1,157,"rvlc_decode_scale_factors(): bitbuffer: length_of_rvlc_escapes"));
 
-        faad_initbits(&ld_rvlc_esc, (void*)rvlc_esc_buffer);
+        faad_initbits(&ld_rvlc_esc, (void*)rvlc_esc_buffer, bit2byte(ics->length_of_rvlc_escapes));
 //        faad_initbits_rev(&ld_rvlc_esc_rev, (void*)rvlc_esc_buffer,
 //            ics->length_of_rvlc_escapes);
     }
--- a/plugins/in_mp4/in_mp4.c
+++ b/plugins/in_mp4/in_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: in_mp4.c,v 1.19 2002/09/03 21:22:53 menno Exp $
+** $Id: in_mp4.c,v 1.20 2002/11/01 11:19:36 menno Exp $
 **/
 
 #define WIN32_LEAN_AND_MEAN
@@ -491,7 +491,8 @@
         }
 
         if ((mp4state.bytes_consumed = faacDecInit(mp4state.hDecoder,
-            mp4state.buffer+tagsize, &mp4state.samplerate, &mp4state.channels)) < 0)
+            mp4state.buffer+tagsize, mp4state.bytes_into_buffer,
+            &mp4state.samplerate, &mp4state.channels)) < 0)
         {
             show_error(module.hMainWindow, "Can't initialize library.");
             faacDecClose(mp4state.hDecoder);
@@ -832,7 +833,8 @@
                     sample_buffer = NULL;
                     frameInfo.samples = 0;
                 } else {
-                    sample_buffer = faacDecDecode(mp4state.hDecoder, &frameInfo, buffer);
+                    sample_buffer = faacDecDecode(mp4state.hDecoder, &frameInfo,
+                        buffer, buffer_size);
                 }
                 if (frameInfo.error > 0)
                 {
@@ -988,7 +990,8 @@
                         }
                     }
 
-                    sample_buffer = faacDecDecode(mp4state.hDecoder, &frameInfo, buffer);
+                    sample_buffer = faacDecDecode(mp4state.hDecoder, &frameInfo,
+                        buffer, mp4state.bytes_into_buffer);
 
                     mp4state.bytes_consumed += frameInfo.bytesconsumed;
                     mp4state.bytes_into_buffer -= mp4state.bytes_consumed;
--- a/plugins/in_mp4/utils.c
+++ b/plugins/in_mp4/utils.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: utils.c,v 1.3 2002/08/15 17:41:44 menno Exp $
+** $Id: utils.c,v 1.4 2002/11/01 11:19:36 menno Exp $
 **/
 
 #define WIN32_LEAN_AND_MEAN
@@ -63,8 +63,8 @@
 
             if (buff)
             {
-                rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8, &dummy3_8, &dummy4_8,
-                    &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
+                rc = AudioSpecificConfig(buff, buff_size, &dummy1_32, &dummy2_8,
+                    &dummy3_8, &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
                 free(buff);
 
                 if (rc < 0)
--- a/plugins/winamp/in_faad.c
+++ b/plugins/winamp/in_faad.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: in_faad.c,v 1.3 2002/02/18 10:01:05 menno Exp $
+** $Id: in_faad.c,v 1.4 2002/11/01 11:19:36 menno Exp $
 **/
 
 #define WIN32_LEAN_AND_MEAN
@@ -343,7 +343,8 @@
 	/* Copy the configuration dialog setting and use it as the default */
 	/* initialize the decoder, and get samplerate and channel info */
 
-    if((buffercount = faacDecInit(hDecoder, memmap_buffer + memmap_index, &samplerate, &channels)) < 0)
+    if ((buffercount = faacDecInit(hDecoder, memmap_buffer + memmap_index,
+        fileread - memmap_index - 1, &samplerate, &channels)) < 0)
     {
 		MessageBox(mod.hMainWindow, "Error opening input file\n", "FAAD Error", MB_OK);
 		return 1;
@@ -403,7 +404,7 @@
 	/* Copy the configuration dialog setting and use it as the default */
 	/* initialize the decoder, and get samplerate and channel info */
 
-    if((buffercount = faacDecInit(hDecoder, buffer, &samplerate, &channels)) < 0)
+    if((buffercount = faacDecInit(hDecoder, buffer, 768*2, &samplerate, &channels)) < 0)
     {
 		MessageBox(mod.hMainWindow, "Error opening input file\n", "FAAD Error", MB_OK);
 		return 1;
@@ -781,7 +782,8 @@
 
 int PlayThread_memmap()
 {
-    sample_buffer = (char*)faacDecDecode(hDecoder, &frameInfo, memmap_buffer + memmap_index);
+    sample_buffer = (char*)faacDecDecode(hDecoder, &frameInfo,
+        memmap_buffer + memmap_index, fileread - memmap_index - 1);
     if (frameInfo.error)
     {
         MessageBox(NULL, faacDecGetErrorMessage(frameInfo.error), "FAAD Error", MB_OK);
@@ -807,7 +809,7 @@
         buffercount = 0;
     }
 
-    sample_buffer = (char*)faacDecDecode(hDecoder, &frameInfo, buffer);
+    sample_buffer = (char*)faacDecDecode(hDecoder, &frameInfo, buffer, 768*2);
     if (frameInfo.error)
     {
         MessageBox(NULL, faacDecGetErrorMessage(frameInfo.error), "FAAD Error", MB_OK);
--- a/plugins/winamp/in_faad.vcproj
+++ b/plugins/winamp/in_faad.vcproj
@@ -84,7 +84,7 @@
 				AdditionalIncludeDirectories="..\..\include,..\..\common\faad,..\..\plugins\winamp,..\..\common\id3lib\include"
 				PreprocessorDefinitions="NDEBUG;WIN32;_WINDOWS;ID3LIB_COMPILATION"
 				StringPooling="TRUE"
-				RuntimeLibrary="4"
+				RuntimeLibrary="0"
 				EnableFunctionLevelLinking="TRUE"
 				UsePrecompiledHeader="2"
 				PrecompiledHeaderFile=".\Release/in_faad.pch"
--- a/plugins/winamp3/FAAD.cpp
+++ b/plugins/winamp3/FAAD.cpp
@@ -138,8 +138,8 @@
 
             if (buff)
             {
-                rc = AudioSpecificConfig(buff, &dummy1_32, &dummy2_8, &dummy3_8, &dummy4_8,
-                    &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
+                rc = AudioSpecificConfig(buff, buff_size,&dummy1_32, &dummy2_8,
+                    &dummy3_8, &dummy4_8, &dummy5_8, &dummy6_8, &dummy7_8, &dummy8_8);
                 free(buff);
 
                 if (rc < 0)
@@ -235,7 +235,8 @@
 		MP4GetTrackESConfiguration(mp4File, track, (unsigned __int8 **)&buffer, &buffer_size);
 		if(!buffer)
 			ERROR_getInfos("MP4GetTrackESConfiguration");
-		AudioSpecificConfig(buffer, &timeScale, &Channels, &sf, &type, &dummy8, &dummy8, &dummy8, &dummy8);
+		AudioSpecificConfig(buffer, buffer_size, &timeScale, &Channels, &sf, &type,
+            &dummy8, &dummy8, &dummy8, &dummy8);
 		if(faacDecInit2(hDecoder, buffer, buffer_size, &Samplerate, &Channels) < 0)
 			ERROR_getInfos("Error initializing decoder library");
 		FREE_ARRAY(buffer);
@@ -379,7 +380,8 @@
 			DWORD	Samples,
 					BytesConsumed;
 
-				if((bytes_consumed=faacDecInit(hDecoder, buffer, &Samplerate, &Channels))<0)
+				if((bytes_consumed=faacDecInit(hDecoder, buffer, bytes_into_buffer,
+                    &Samplerate, &Channels))<0)
 					ERROR_getInfos("Can't init library");
 				bytes_into_buffer-=bytes_consumed;
 				if(!processData(infos,0,0))
@@ -396,7 +398,7 @@
 			}
 		}
 
-		if((bytes_consumed=faacDecInit(hDecoder, buffer, &Samplerate, &Channels))<0)
+		if((bytes_consumed=faacDecInit(hDecoder, buffer, bytes_into_buffer, &Samplerate, &Channels))<0)
 			ERROR_getInfos("faacDecInit failed!")
 		bytes_into_buffer-=bytes_consumed;
 
@@ -472,7 +474,7 @@
 				ERROR_processData("MP4ReadSample")
 			}
 
-			bufout=(char *)faacDecDecode(hDecoder,&frameInfo,buffer);
+			bufout=(char *)faacDecDecode(hDecoder,&frameInfo,buffer,buffer_size);
 			BytesDecoded=frameInfo.samples*sizeof(short);
 			FREE_ARRAY(buffer);
 			// to update the slider
@@ -536,7 +538,7 @@
 				else
 					ERROR_processData(0);
 
-			bufout=(char *)faacDecDecode(hDecoder,&frameInfo,buffer);
+			bufout=(char *)faacDecDecode(hDecoder,&frameInfo,buffer,bytes_into_buffer);
 			BytesDecoded=frameInfo.samples*sizeof(short);
 			bytes_consumed+=frameInfo.bytesconsumed;
 			bytes_into_buffer-=bytes_consumed;
--- a/plugins/xmms/src/libaac.c
+++ b/plugins/xmms/src/libaac.c
@@ -1,14 +1,14 @@
 /*
-**			  AAC plugin for XMMS 1.2.7
-**				by ciberfred
-**		------------------------------------------------
+**            AAC plugin for XMMS 1.2.7
+**              by ciberfred
+**      ------------------------------------------------
 ** The version of the plugin match the version of XMMS
 ** for identifie different version use the date :)
 **
-**			version 1.2.7 (23 august 2002)
+**          version 1.2.7 (23 august 2002)
 **
 **
-**	       need faad2 package from http://www.audiocoding.com
+**         need faad2 package from http://www.audiocoding.com
 **
 */
 
@@ -25,10 +25,10 @@
 #include "xmms/configfile.h"
 #include "xmms/titlestring.h"
 
-#define AAC_DESCRIPTION	"MPEG2/4 AAC player - 1.2.7"
-#define AAC_VERSION	"AAC player - 23 Agust 2002"
-#define AAC_ABOUT	"Writen from scratch by ciberfred from France"
-#define BUFFER_SIZE	FAAD_MIN_STREAMSIZE*64
+#define AAC_DESCRIPTION "MPEG2/4 AAC player - 1.2.7"
+#define AAC_VERSION "AAC player - 23 Agust 2002"
+#define AAC_ABOUT   "Writen from scratch by ciberfred from France"
+#define BUFFER_SIZE FAAD_MIN_STREAMSIZE*64
 
 static void aac_init(void);
 static void aac_play(char*);
@@ -55,47 +55,47 @@
 
 InputPlugin aac_ip =
 {
-	0,		// handle
-	0,		// filename
-	AAC_DESCRIPTION,// description
-	aac_init,	// init_func
-	aac_about,	// aboutbox
-	0,		// configuration
-	aac_isFile,	// ???
-	0,		// scan dir
-	aac_play,	// when play button
-	aac_stop,	// when stop
-	aac_pause,	// when pause
-	aac_seek,	// when seek
-	0,		// set equalizer
-	aac_getTime,	// ???
-	0,		// get volume
-	0,		// set volume
-	aac_cleanup,	// the cleanup function :)
-	0,		// obsolete (???)
-	0,		// send visualisation data
-	0,		// set player window info
-	0,		// set song title text
-	0,	// get song title text to show on Playlist
-	aac_getSongInfo,// file info box
-	0		// pointer to outputPlugin
+    0,      // handle
+    0,      // filename
+    AAC_DESCRIPTION,// description
+    aac_init,   // init_func
+    aac_about,  // aboutbox
+    0,      // configuration
+    aac_isFile, // ???
+    0,      // scan dir
+    aac_play,   // when play button
+    aac_stop,   // when stop
+    aac_pause,  // when pause
+    aac_seek,   // when seek
+    0,      // set equalizer
+    aac_getTime,    // ???
+    0,      // get volume
+    0,      // set volume
+    aac_cleanup,    // the cleanup function :)
+    0,      // obsolete (???)
+    0,      // send visualisation data
+    0,      // set player window info
+    0,      // set song title text
+    0,  // get song title text to show on Playlist
+    aac_getSongInfo,// file info box
+    0       // pointer to outputPlugin
 };
-static gboolean 	bPlaying = FALSE;
-static gboolean	bOutputOpen = FALSE;
-static pthread_t	decodeThread;
-static gint		seek_pos = -1; // the track position
-static pthread_mutex_t	mutex = PTHREAD_MUTEX_INITIALIZER;
+static gboolean     bPlaying = FALSE;
+static gboolean bOutputOpen = FALSE;
+static pthread_t    decodeThread;
+static gint     seek_pos = -1; // the track position
+static pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /******************************************************************************/
 
-InputPlugin	*get_iplugin_info(void)
+InputPlugin *get_iplugin_info(void)
 {
-	return (&aac_ip);
+    return (&aac_ip);
 }
 
 static void aac_init(void)
 {
-	memset(&decodeThread, 0, sizeof(pthread_t));
+    memset(&decodeThread, 0, sizeof(pthread_t));
 }
 
 static void aac_cleanup(void)
@@ -104,208 +104,208 @@
 
 static void aac_play(char *filename)
 {
-	printf("play\n");
-	bPlaying = TRUE;
-	pthread_create(&decodeThread, 0, aac_decode, g_strdup(filename));
-	return;
+    printf("play\n");
+    bPlaying = TRUE;
+    pthread_create(&decodeThread, 0, aac_decode, g_strdup(filename));
+    return;
 }
 
 static void aac_stop(void)
 {
-	printf("stop\n");
-	if (bPlaying){
-		bPlaying = FALSE;
-		pthread_join(decodeThread, NULL);
-		memset(&decodeThread, 0, sizeof(pthread_t));
-		aac_ip.output->close_audio();
-		clearWindowDatas();
-	}
+    printf("stop\n");
+    if (bPlaying){
+        bPlaying = FALSE;
+        pthread_join(decodeThread, NULL);
+        memset(&decodeThread, 0, sizeof(pthread_t));
+        aac_ip.output->close_audio();
+        clearWindowDatas();
+    }
 }
 
 static void aac_pause(short paused)
 {
-	printf("pause\n");
-	if(bOutputOpen){
-		aac_ip.output->pause(paused);
-	}
+    printf("pause\n");
+    if(bOutputOpen){
+        aac_ip.output->pause(paused);
+    }
 }
 
 static int aac_getTime(void)
 {
-	if (!bPlaying){
-		return (-1);
-	}
-	else{
-		return (aac_ip.output->output_time());
-	}
+    if (!bPlaying){
+        return (-1);
+    }
+    else{
+        return (aac_ip.output->output_time());
+    }
 }
 
 
 static void aac_seek(int time)
 {
-	printf("seek\n");
+    printf("seek\n");
 }
 
 static void aac_getSongInfo(char *filename)
 {
-	infoBoxWindow = createDialogInfo();
-	gtk_widget_show(infoBoxWindow);
+    infoBoxWindow = createDialogInfo();
+    gtk_widget_show(infoBoxWindow);
 }
 
 static void *aac_decode(void *args)
 {
-	char 			*filename = args;
-	char			*xmmstitle=NULL;
-	FILE 			*file = NULL;
-	faacDecHandle		decoder = 0;
-	unsigned char		*buffer = 0;
-	unsigned long		bufferconsumed = 0;
-	unsigned long		samplerate = 0;
-	char			channels;
-	unsigned long		buffervalid = 0;
-	TitleInput		*input;
-	char 			*temp = g_strdup(filename);
-	char			*ext  = strrchr(temp, '.');
+    char            *filename = args;
+    char            *xmmstitle=NULL;
+    FILE            *file = NULL;
+    faacDecHandle       decoder = 0;
+    unsigned char       *buffer = 0;
+    unsigned long       bufferconsumed = 0;
+    unsigned long       samplerate = 0;
+    char            channels;
+    unsigned long       buffervalid = 0;
+    TitleInput      *input;
+    char            *temp = g_strdup(filename);
+    char            *ext  = strrchr(temp, '.');
 
-	printf("decoding...\n");
-	pthread_mutex_lock(&mutex);
-	clearWindowDatas();
-	if ((file = fopen(filename, "rb")) == 0){
-		printf("can't find file %s\n", filename);
-		pthread_mutex_unlock(&mutex);
-		pthread_exit(NULL);
-	}
-	if ((decoder = faacDecOpen()) == NULL){
-		printf("Open Decoder Error\n");
-		fclose(file);
-		pthread_mutex_unlock(&mutex);
-		pthread_exit(NULL);
-	}
-	if ((buffer = g_malloc(BUFFER_SIZE)) == NULL)
-		printf("error g_malloc\n");
-	buffervalid = fread(buffer, 1, BUFFER_SIZE, file);
+    printf("decoding...\n");
+    pthread_mutex_lock(&mutex);
+    clearWindowDatas();
+    if ((file = fopen(filename, "rb")) == 0){
+        printf("can't find file %s\n", filename);
+        pthread_mutex_unlock(&mutex);
+        pthread_exit(NULL);
+    }
+    if ((decoder = faacDecOpen()) == NULL){
+        printf("Open Decoder Error\n");
+        fclose(file);
+        pthread_mutex_unlock(&mutex);
+        pthread_exit(NULL);
+    }
+    if ((buffer = g_malloc(BUFFER_SIZE)) == NULL)
+        printf("error g_malloc\n");
+    buffervalid = fread(buffer, 1, BUFFER_SIZE, file);
 // check for ID3 tag
-		XMMS_NEW_TITLEINPUT(input);
-		input->file_name = g_basename(temp);
-		input->file_ext = ext ? ext+1 : NULL;
-		input->file_path = temp;
-	if (!strncmp(buffer, "ID3", 3)){
-		int size = 0;
+        XMMS_NEW_TITLEINPUT(input);
+        input->file_name = g_basename(temp);
+        input->file_ext = ext ? ext+1 : NULL;
+        input->file_path = temp;
+    if (!strncmp(buffer, "ID3", 3)){
+        int size = 0;
 
-		printf("Song with a ID3Tagv2\n");
-		readID3tag(filename);
-		if(title)
-		input->track_name = g_strdup(title);
-		if(artist)
-		input->performer = g_strdup(artist);
-		if(genre)
-		input->genre = g_strdup(genre);
-		if(track)
-		input->track_number = atoi(track);
-		fseek(file, 0, SEEK_SET);
+        printf("Song with a ID3Tagv2\n");
+        readID3tag(filename);
+        if(title)
+        input->track_name = g_strdup(title);
+        if(artist)
+        input->performer = g_strdup(artist);
+        if(genre)
+        input->genre = g_strdup(genre);
+        if(track)
+        input->track_number = atoi(track);
+        fseek(file, 0, SEEK_SET);
 /*
 ** hum .. horrible hack taken from the winamp plugin to jump
 ** the tag, is there any id3 function to do this ???? hum... seems not :(
 */
-		size = (buffer[6]<<21) | (buffer[7]<<14) | (buffer[8]<<7) | buffer[9];
-		size+=10;
-		fread(buffer, 1, size, file);
-		buffervalid = fread(buffer, 1, BUFFER_SIZE, file);
-	}
-	xmmstitle = xmms_get_titlestring(xmms_get_gentitle_format(), input);
-	if(xmmstitle == NULL)
-		xmmstitle = g_strdup(input->file_name);
-	g_free(temp);
-	g_free(input->performer);
-	g_free(input->album_name);
-	g_free(input->track_name);
-	g_free(input->genre);
-	g_free(input);
-	bufferconsumed = faacDecInit(decoder, buffer, &samplerate, &channels);
-//	printf("song with %d channels at %d Hz\n", channels, samplerate);
-	if((bOutputOpen = aac_ip.output->open_audio(FMT_S16_NE, samplerate, channels)) == FALSE){
-		printf("Output Error\n");
-		g_free(buffer);
-		buffer=0;
-		faacDecClose(decoder);
-		fclose(file);
-		aac_ip.output->close_audio();
-		pthread_mutex_unlock(&mutex);
-		pthread_exit(NULL);
-	}
-	aac_ip.set_info(xmmstitle, -1, -1, samplerate, channels);
-	aac_ip.output->flush(0);
+        size = (buffer[6]<<21) | (buffer[7]<<14) | (buffer[8]<<7) | buffer[9];
+        size+=10;
+        fread(buffer, 1, size, file);
+        buffervalid = fread(buffer, 1, BUFFER_SIZE, file);
+    }
+    xmmstitle = xmms_get_titlestring(xmms_get_gentitle_format(), input);
+    if(xmmstitle == NULL)
+        xmmstitle = g_strdup(input->file_name);
+    g_free(temp);
+    g_free(input->performer);
+    g_free(input->album_name);
+    g_free(input->track_name);
+    g_free(input->genre);
+    g_free(input);
+    bufferconsumed = faacDecInit(decoder, buffer, buffervalid, &samplerate, &channels);
+//  printf("song with %d channels at %d Hz\n", channels, samplerate);
+    if((bOutputOpen = aac_ip.output->open_audio(FMT_S16_NE, samplerate, channels)) == FALSE){
+        printf("Output Error\n");
+        g_free(buffer);
+        buffer=0;
+        faacDecClose(decoder);
+        fclose(file);
+        aac_ip.output->close_audio();
+        pthread_mutex_unlock(&mutex);
+        pthread_exit(NULL);
+    }
+    aac_ip.set_info(xmmstitle, -1, -1, samplerate, channels);
+    aac_ip.output->flush(0);
 
-	while(bPlaying && buffervalid > 0){
-		faacDecFrameInfo	finfo;
-		unsigned long		samplesdecoded;
-		char			*sample_buffer = NULL;
+    while(bPlaying && buffervalid > 0){
+        faacDecFrameInfo    finfo;
+        unsigned long       samplesdecoded;
+        char            *sample_buffer = NULL;
 
-		if(bufferconsumed > 0){
-		memmove(buffer, &buffer[bufferconsumed], buffervalid-bufferconsumed);
-			buffervalid -= bufferconsumed;
-			buffervalid += fread(&buffer[buffervalid], 1, BUFFER_SIZE-buffervalid, file);
-			bufferconsumed = 0;
-		}
-		sample_buffer = faacDecDecode(decoder, &finfo, buffer);
-		if(finfo.error){
-			buffervalid = 0;
-			printf("FAAD2 Error %s\n", faacDecGetErrorMessage(finfo.error));
-			printf("---Use Psystrip.exe on the file to avoid the ADTS error---\n");
-		}
-		bufferconsumed += finfo.bytesconsumed;
-		samplesdecoded = finfo.samples;
-		if((samplesdecoded<=0) && !sample_buffer){
-			printf("error\n");
-		}
-		while(bPlaying && aac_ip.output->buffer_free() < (samplesdecoded<<1)){
-			xmms_usleep(10000);
-		}
-		aac_ip.add_vis_pcm(aac_ip.output->written_time(), FMT_S16_LE, channels, samplesdecoded<<1, sample_buffer);
-		aac_ip.output->write_audio(sample_buffer, samplesdecoded<<1);
-	}
-	while(bPlaying && aac_ip.output->buffer_playing()){
-		xmms_usleep(10000);
-	}
-//	aac_ip.output->flush(0);
-	aac_ip.output->buffer_free();
-	aac_ip.output->close_audio();
-	bPlaying = FALSE;
-	bOutputOpen = FALSE;
-	g_free(buffer);
-	faacDecClose(decoder);
-	g_free(xmmstitle);
-	fclose(file);
-	printf("...ended\n");
-	seek_pos = -1;
-	pthread_mutex_unlock(&mutex);
-	pthread_exit(NULL);
+        if(bufferconsumed > 0){
+        memmove(buffer, &buffer[bufferconsumed], buffervalid-bufferconsumed);
+            buffervalid -= bufferconsumed;
+            buffervalid += fread(&buffer[buffervalid], 1, BUFFER_SIZE-buffervalid, file);
+            bufferconsumed = 0;
+        }
+        sample_buffer = faacDecDecode(decoder, &finfo, buffer, buffervalid);
+        if(finfo.error){
+            buffervalid = 0;
+            printf("FAAD2 Error %s\n", faacDecGetErrorMessage(finfo.error));
+            printf("---Use Psystrip.exe on the file to avoid the ADTS error---\n");
+        }
+        bufferconsumed += finfo.bytesconsumed;
+        samplesdecoded = finfo.samples;
+        if((samplesdecoded<=0) && !sample_buffer){
+            printf("error\n");
+        }
+        while(bPlaying && aac_ip.output->buffer_free() < (samplesdecoded<<1)){
+            xmms_usleep(10000);
+        }
+        aac_ip.add_vis_pcm(aac_ip.output->written_time(), FMT_S16_LE, channels, samplesdecoded<<1, sample_buffer);
+        aac_ip.output->write_audio(sample_buffer, samplesdecoded<<1);
+    }
+    while(bPlaying && aac_ip.output->buffer_playing()){
+        xmms_usleep(10000);
+    }
+//  aac_ip.output->flush(0);
+    aac_ip.output->buffer_free();
+    aac_ip.output->close_audio();
+    bPlaying = FALSE;
+    bOutputOpen = FALSE;
+    g_free(buffer);
+    faacDecClose(decoder);
+    g_free(xmmstitle);
+    fclose(file);
+    printf("...ended\n");
+    seek_pos = -1;
+    pthread_mutex_unlock(&mutex);
+    pthread_exit(NULL);
 }
 
 static int aac_isFile(char *filename)
 {
-	char *extention = strrchr(filename, '.');
-	if (extention && !strcasecmp(extention, ".aac")){
-		return (1);
-	}
-	return(0);
+    char *extention = strrchr(filename, '.');
+    if (extention && !strcasecmp(extention, ".aac")){
+        return (1);
+    }
+    return(0);
 }
 
 static void aac_about(void)
 {
-	GtkWidget *dialog, *button, *label, *label2;
+    GtkWidget *dialog, *button, *label, *label2;
 
-	dialog = gtk_dialog_new();
-	gtk_window_set_title(GTK_WINDOW(dialog), "About FAAD2 plugin");
-	label = gtk_label_new(AAC_ABOUT);
-	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
-	gtk_widget_show(label);
-	label2 = gtk_label_new(AAC_VERSION);
-	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label2, TRUE, TRUE, 0);
-	gtk_widget_show(label2);
-	button = gtk_button_new_with_label("close");
-	gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(dialog));
-	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
-	gtk_widget_show(button);
-	gtk_widget_show(dialog);
+    dialog = gtk_dialog_new();
+    gtk_window_set_title(GTK_WINDOW(dialog), "About FAAD2 plugin");
+    label = gtk_label_new(AAC_ABOUT);
+    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
+    gtk_widget_show(label);
+    label2 = gtk_label_new(AAC_VERSION);
+    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label2, TRUE, TRUE, 0);
+    gtk_widget_show(label2);
+    button = gtk_button_new_with_label("close");
+    gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(gtk_widget_destroy), GTK_OBJECT(dialog));
+    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
+    gtk_widget_show(button);
+    gtk_widget_show(dialog);
 }