shithub: aacenc

Download patch

ref: 1a6bd6cbe1228ec224e35870cac5420de00903f0
parent: a929acdf788d7899bf2631e0f8b9a2acdae09ef7
author: menno <menno>
date: Sun May 26 14:29:59 EDT 2002

Fixes in cooledit plugin

--- a/plugins/cooledit/Faac.cpp
+++ b/plugins/cooledit/Faac.cpp
@@ -7,7 +7,7 @@
 
 
 
-#define PI_VER "v2.0 beta1"
+#define PI_VER "v2.0 beta2"
 #define REGISTRY_PROGRAM_NAME "SOFTWARE\\4N\\CoolEdit\\AAC-MPEG4"
 
 
@@ -14,18 +14,18 @@
 
 typedef struct output_tag  // any special vars associated with output file
 {
- FILE  *fFile;
- DWORD lSize;
- long  lSamprate;
- WORD  wBitsPerSample;
- WORD  wChannels;
- char  szNAME[256];
+FILE			*fFile;
+DWORD			lSize;
+long			lSamprate;
+WORD			wBitsPerSample;
+WORD			wChannels;
+char			szNAME[256];
 
- faacEncHandle hEncoder;
- unsigned char *bitbuf;
- DWORD maxBytesOutput;
- long  samplesInput;
- BYTE  bStopEnc;
+faacEncHandle	hEncoder;
+unsigned char	*bitbuf;
+DWORD			maxBytesOutput;
+long			samplesInput;
+BYTE			bStopEnc;
 } MYOUTPUT;
 
 
@@ -36,6 +36,12 @@
 faacEncConfiguration	EncCfg;
 } MYCFG;
 
+
+
+// *********************************************************************************************
+
+
+
 void RD_Cfg(MYCFG *cfg) 
 { 
 registryClass *reg=new registryClass();
@@ -273,9 +279,8 @@
  
 	return TRUE;
 } // End of DIALOGSMsgProc                                      
+// *********************************************************************************************
 
-
-
 __declspec(dllexport) DWORD FAR PASCAL FilterGetOptions(HWND hWnd, HINSTANCE hInst, long lSamprate, WORD wChannels, WORD wBitsPerSample, DWORD dwOptions) // return 0 if no options box
 {
 long nDialogReturn=0;
@@ -286,6 +291,7 @@
 
  return nDialogReturn;
 }
+// *********************************************************************************************
 
 __declspec(dllexport) DWORD FAR PASCAL FilterWriteFirstSpecialData(HANDLE hInput, 
 	SPECIALDATA * psp)
@@ -292,6 +298,7 @@
 {
  return 0;
 }
+// *********************************************************************************************
 
 __declspec(dllexport) DWORD FAR PASCAL FilterWriteNextSpecialData(HANDLE hInput, SPECIALDATA * psp)
 {	
@@ -300,6 +307,7 @@
 // as either a counter to know which item to retrieve next, or as a
 // structure with other state information in it.
 }
+// *********************************************************************************************
 
 __declspec(dllexport) DWORD FAR PASCAL FilterWriteSpecialData(HANDLE hOutput,
 	LPCSTR szListType, LPCSTR szType, char * pData,DWORD dwSize)
@@ -306,6 +314,7 @@
 {
  return 0;
 }
+// *********************************************************************************************
 
 __declspec(dllexport) void FAR PASCAL CloseFilterOutput(HANDLE hOutput)
 {
@@ -334,108 +343,103 @@
   GlobalFree(hOutput);
  }
 }              
+// *********************************************************************************************
 
+#define ERROR_OFO(msg) \
+{ \
+	if(msg) \
+		MessageBox(0, msg, "FAAC plugin", MB_OK); \
+	if(hOutput) \
+	{ \
+		GlobalUnlock(hOutput); \
+		CloseFilterOutput(hOutput); \
+	} \
+	return 0; \
+}
+
 __declspec(dllexport) HANDLE FAR PASCAL OpenFilterOutput(LPSTR lpstrFilename,long lSamprate,WORD wBitsPerSample,WORD wChannels,long lSize, long far *lpChunkSize, DWORD dwOptions)
 {
 HANDLE			hOutput;
-faacEncHandle	hEncoder;
-FILE			*outfile;
-unsigned char	*bitbuf;
+MYOUTPUT		*mo;
+MYCFG			cfg;
 DWORD			maxBytesOutput;
 DWORD			samplesInput;
 int				bytesEncoded;
+int				tmp;
 
-//    if(!((dwOptions>>23)&1))
-/*	{
-     config_init();
-     config_read(&dwOptions);
-	}
-*/
+    hOutput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,sizeof(MYOUTPUT));
+    if(!hOutput)
+		ERROR_OFO("Memory allocation error: hOutput");
+	mo=(MYOUTPUT *)GlobalLock(hOutput);
+	memset(mo,0,sizeof(MYOUTPUT));
+
 // open the aac output file 
-	if(!(outfile=fopen(lpstrFilename, "wb")))
-	{
-     MessageBox(0, "Can't create file", "FAAC interface", MB_OK);
-	 return 0;
-	}
+	if(!(mo->fFile=fopen(lpstrFilename, "wb")))
+		ERROR_OFO("Can't create file");
 
 // open the encoder library
-	if(!(hEncoder=faacEncOpen(lSamprate, wChannels, &samplesInput, &maxBytesOutput)))
-	{
-	 MessageBox(0, "Can't init library", "FAAC interface", MB_OK);
-	 fclose(outfile);
-	 return 0;
-	}
+	if(!(mo->hEncoder=faacEncOpen(lSamprate, wChannels, &samplesInput, &maxBytesOutput)))
+		ERROR_OFO("Can't init library");
 
-	if(!(bitbuf=(unsigned char*)malloc(maxBytesOutput*sizeof(unsigned char))))
-	{
-	 MessageBox(0, "Memory allocation error: output buffer", "FAAC interface", MB_OK);
-     faacEncClose(hEncoder);
-	 fclose(outfile);
-	 return 0;
-	}
+	if(!(mo->bitbuf=(unsigned char*)malloc(maxBytesOutput*sizeof(unsigned char))))
+		ERROR_OFO("Memory allocation error: output buffer");
 
 	*lpChunkSize=samplesInput*2;
 
-    hOutput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,sizeof(MYOUTPUT));
-    if(hOutput)
-    {
-	MYCFG cfg;
-	MYOUTPUT *mo;
-	 mo=(MYOUTPUT *)GlobalLock(hOutput);
 
-	 RD_Cfg(&cfg);
-	 if(!cfg.AutoCfg)
-	 {
-     faacEncConfigurationPtr myFormat=&cfg.EncCfg;
-//     myFormat=faacEncGetCurrentConfiguration(hEncoder);
+	RD_Cfg(&cfg);
+	if(!cfg.AutoCfg)
+	{
+    faacEncConfigurationPtr myFormat=&cfg.EncCfg;
+//     myFormat=faacEncGetCurrentConfiguration(mo->hEncoder);
 
-      if(!myFormat->bandWidth)
-	   myFormat->bandWidth=lSamprate/2;
+		if(!myFormat->bandWidth)
+			myFormat->bandWidth=lSamprate/2;
 
-	  if(!faacEncSetConfiguration(hEncoder, myFormat))
-	  {
-       MessageBox(0, "Unsupported parameters", "FAAC interface", MB_OK);
-       faacEncClose(hEncoder);
-	   fclose(outfile);
-	   free(bitbuf);
-       GlobalFree(hOutput);
-	   return 0;
-	  }
+		if(!faacEncSetConfiguration(mo->hEncoder, myFormat))
+			ERROR_OFO("Unsupported parameters");
 	 }
 
-	 mo->fFile=outfile;
-	 mo->lSize=lSize;
-	 mo->lSamprate=lSamprate;
-	 mo->wBitsPerSample=wBitsPerSample;
-	 mo->wChannels=wChannels;
-	 strcpy(mo->szNAME,lpstrFilename);
+	mo->fFile=mo->fFile;
+	mo->lSize=lSize;
+	mo->lSamprate=lSamprate;
+	mo->wBitsPerSample=wBitsPerSample;
+	mo->wChannels=wChannels;
+	strcpy(mo->szNAME,lpstrFilename);
 
-	 mo->hEncoder=hEncoder;
-     mo->bitbuf=bitbuf;
-	 mo->maxBytesOutput=maxBytesOutput;
-	 mo->samplesInput=samplesInput;
-	 mo->bStopEnc=0;
+	mo->hEncoder=mo->hEncoder;
+    mo->bitbuf=mo->bitbuf;
+	mo->maxBytesOutput=maxBytesOutput;
+	mo->samplesInput=samplesInput;
+	mo->bStopEnc=0;
 
-	 GlobalUnlock(hOutput);
-    }
-	else
+// init flushing process
+    bytesEncoded=faacEncEncode(mo->hEncoder, 0, 0, mo->bitbuf, maxBytesOutput); // initializes the flushing process
+    if(bytesEncoded>0)
 	{
-	 MessageBox(0, "hOutput=NULL", "FAAC interface", MB_OK);
-     faacEncClose(hEncoder);
-	 fclose(outfile);
-	 free(bitbuf);
-	 return 0;
+		tmp=fwrite(mo->bitbuf, 1, bytesEncoded, mo->fFile);
+		if(tmp!=bytesEncoded)
+			ERROR_OFO("fwrite");
 	}
 
+	GlobalUnlock(hOutput);
 
-// init flushing process
-    bytesEncoded=faacEncEncode(hEncoder, 0, 0, bitbuf, maxBytesOutput); // initializes the flushing process
-    if(bytesEncoded>0)
-	 fwrite(bitbuf, 1, bytesEncoded, outfile);
-
     return hOutput;
 }
+// *********************************************************************************************
 
+#define ERROR_WFO(msg) \
+{ \
+	if(msg) \
+		MessageBox(0, msg, "FAAC plugin", MB_OK); \
+	if(hOutput) \
+	{ \
+		mo->bStopEnc=1; \
+		GlobalUnlock(hOutput); \
+	} \
+	return 0; \
+}
+
 __declspec(dllexport) DWORD FAR PASCAL WriteFilterOutput(HANDLE hOutput, unsigned char far *buf, long lBytes)
 {
 int bytesWritten;
@@ -446,7 +450,7 @@
  MYOUTPUT far *mo;
   mo=(MYOUTPUT far *)GlobalLock(hOutput);
 
-  if(!mo->bStopEnc)
+  if(!mo->bStopEnc) // Is this case possible?
   {
 // call the actual encoding routine
    bytesEncoded=faacEncEncode(mo->hEncoder, (short *)buf, mo->samplesInput, mo->bitbuf, mo->maxBytesOutput);
@@ -453,10 +457,7 @@
    if(bytesEncoded<1) // end of flushing process
    {
     if(bytesEncoded<0)
-	{
-     MessageBox(0, "faacEncEncode() failed", "FAAC interface", MB_OK);
-     mo->bStopEnc=1;
-	}
+		ERROR_WFO("faacEncEncode");
 	bytesWritten=lBytes ? 1 : 0; // bytesWritten==0 stops CoolEdit...
     GlobalUnlock(hOutput);
     return bytesWritten;
@@ -464,12 +465,7 @@
 // write bitstream to aac file 
    bytesWritten=fwrite(mo->bitbuf, 1, bytesEncoded, mo->fFile);
    if(bytesWritten!=bytesEncoded)
-   {
-    MessageBox(0, "bytesWritten and bytesEncoded are different", "FAAC interface", MB_OK);
-    mo->bStopEnc=1;
-    GlobalUnlock(hOutput);
-    return 0;
-   }
+		ERROR_WFO("fwrite");
 
    GlobalUnlock(hOutput);
   }
--- a/plugins/cooledit/Faad.cpp
+++ b/plugins/cooledit/Faad.cpp
@@ -8,18 +8,17 @@
 
 
 #define MAX_CHANNELS 2
-#define QWORD __int32
 
 
 typedef struct input_tag // any special vars associated with input file
 {
 //AAC
- FILE	*fFile;
- DWORD	lSize;    
- DWORD	tagsize;
- DWORD	bytes_read;		// from file
- DWORD	bytes_consumed;	// by faadDecDecode
- long	bytes_into_buffer;
+ FILE			*aacFile;
+ DWORD			lSize;    
+ DWORD			tagsize;
+ DWORD			bytes_read;		// from file
+ DWORD			bytes_consumed;	// by faadDecDecode
+ long			bytes_into_buffer;
  unsigned char	*buffer;
 
 //MP4
@@ -26,18 +25,18 @@
  MP4FileHandle	mp4File;
  MP4SampleId	sampleId, numSamples;
  int			track;
- unsigned char  type;
+ DWORD			type;
 
 // GENERAL
  faacDecHandle	hDecoder;
  faadAACInfo	file_info;
- QWORD	len_ms;
- WORD	wChannels;
- DWORD	dwSamprate;
- WORD	wBitsPerSample;
- char	szName[256];
- DWORD	full_size;		// size of decoded file needed to set the length of progress bar
- bool	IsAAC;
+ __int32		len_ms;
+ WORD			wChannels;
+ DWORD			dwSamprate;
+ WORD			wBitsPerSample;
+ char			szName[256];
+ DWORD			full_size;		// size of decoded file needed to set the length of progress bar
+ bool			IsAAC;
 } MYINPUT;
 
 static const char* mpeg4AudioNames[]=
@@ -93,12 +92,11 @@
             unsigned char *buff = NULL;
             unsigned __int32 buff_size = 0;
 			DWORD dummy;
-            unsigned char ch_dummy;
             MP4GetTrackESConfiguration(infile, trackId, &buff, &buff_size);
 
             if (buff)
             {
-                rc = AudioSpecificConfig(buff, &dummy, &ch_dummy, &ch_dummy, &ch_dummy);
+                rc = AudioSpecificConfig(buff, &dummy, &dummy, &dummy, &dummy);
                 free(buff);
 
                 if (rc < 0)
@@ -127,6 +125,7 @@
   return TRUE;
  return FALSE;
 }
+// *********************************************************************************************
 
 __declspec(dllexport) long FAR PASCAL FilterGetFileSize(HANDLE hInput)
 {
@@ -143,8 +142,8 @@
 
  return full_size;
 }
+// *********************************************************************************************
 
-
 __declspec(dllexport) DWORD FAR PASCAL FilterOptionsString(HANDLE hInput, LPSTR szString)
 {
 char buf[20];
@@ -164,7 +163,7 @@
   sprintf(buf,"%lu bps\n",mi->file_info.bitrate);
   lstrcat(szString,buf);
  
-  if(mi->IsAAC)
+  if(mi->IsAAC)  // AAC file --------------------------------------------------------------------
   {
 	switch(mi->file_info.headertype)
 	{
@@ -195,7 +194,7 @@
 		break;
 	}
   }
-  else
+  else  // MP4 file -----------------------------------------------------------------------------
 	lstrcat(szString,mpeg4AudioNames[mi->type]);
 
   GlobalUnlock(hInput);
@@ -202,6 +201,7 @@
  }
  return 1;
 }
+// *********************************************************************************************
 
 __declspec(dllexport) DWORD FAR PASCAL FilterGetFirstSpecialData(HANDLE hInput, 
 	SPECIALDATA * psp)
@@ -208,6 +208,7 @@
 {
 return 0L;
 }
+// *********************************************************************************************
     
 __declspec(dllexport) DWORD FAR PASCAL FilterGetNextSpecialData(HANDLE hInput, SPECIALDATA * psp)
 {	return 0; // only has 1 special data!  Otherwise we would use psp->hSpecialData
@@ -214,6 +215,7 @@
 			  // as either a counter to know which item to retrieve next, or as a
 			  // structure with other state information in it.
 }
+// *********************************************************************************************
 
 __declspec(dllexport) void FAR PASCAL CloseFilterInput(HANDLE hInput)
 {
@@ -220,240 +222,234 @@
  if(hInput)
  {
  MYINPUT far *mi;
-  mi=(MYINPUT far *)GlobalLock(hInput);
+	mi=(MYINPUT far *)GlobalLock(hInput);
 
-  if(mi->fFile)
-   fclose(mi->fFile);
+// AAC file ---------------------------------------------------------------------
+	if(mi->aacFile)
+		fclose(mi->aacFile);
   
-  if(mi->buffer)
-   free(mi->buffer);
+	if(mi->buffer)
+		free(mi->buffer);
 
-  if(mi->hDecoder)
-   faacDecClose(mi->hDecoder);
+// MP4 file ---------------------------------------------------------------------
+	if(mi->mp4File)
+		MP4Close(mi->mp4File);
 
-  GlobalUnlock(hInput);
-  GlobalFree(hInput);
+	if(mi->hDecoder)
+		faacDecClose(mi->hDecoder);
+
+	GlobalUnlock(hInput);
+	GlobalFree(hInput);
  }
 }
+// *********************************************************************************************
 
-
-#define ERROR_ON_OPEN_MP4(msg) \
+#define ERROR_OFI(msg) \
 { \
-	MessageBox(0, msg, "FAAD interface", MB_OK); \
-	if(infile) \
-		MP4Close(infile); \
-	if(hDecoder) \
-		faacDecClose(hDecoder); \
-	GlobalUnlock(hInput); \
+	if(msg) \
+		MessageBox(0, msg, "FAAD plugin", MB_OK); \
+	if(hInput) \
+	{ \
+		GlobalUnlock(hInput); \
+		CloseFilterInput(hInput); \
+	} \
 	return 0; \
 }
-#define ERROR_ON_OPEN_AAC(msg) \
-{ \
-	MessageBox(0, msg, "FAAD interface", MB_OK); \
-	fclose(infile); \
-	if(buffer) \
-		free(buffer); \
-	if(hDecoder) \
-		faacDecClose(hDecoder); \
-	GlobalUnlock(hInput); \
-	return 0; \
-}
 
 // return handle that will be passed in to close, and write routines
-__declspec(dllexport) HANDLE FAR PASCAL OpenFilterInput( LPSTR lpstrFilename,
-											long far *lSamprate,
-											WORD far *wBitsPerSample,
-											WORD far *wChannels,
-											HWND hWnd,
-											long far *lChunkSize)
+__declspec(dllexport) HANDLE FAR PASCAL OpenFilterInput(LPSTR lpstrFilename, long far *lSamprate, WORD far *wBitsPerSample, WORD far *wChannels, HWND hWnd, long far *lChunkSize)
 {
-HANDLE hInput;
-MYINPUT *mi;
-faacDecHandle hDecoder=0;
-faacDecConfigurationPtr config;
-DWORD  tmp;
-DWORD  samplerate;
-unsigned char channels;
-unsigned char *buffer=0;
+HANDLE					hInput;
+MYINPUT					*mi;
+faacDecConfigurationPtr	config;
+DWORD					samplerate, channels;
+DWORD					tmp;
+BYTE					BitsPerSample=16;
 
- hInput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE|GMEM_ZEROINIT,sizeof(MYINPUT));
- if(!hInput)
-  return 0;
- mi=(MYINPUT *)GlobalLock(hInput);
- memset(mi,0,sizeof(MYINPUT));
+	hInput=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE|GMEM_ZEROINIT,sizeof(MYINPUT));
+	if(!hInput)
+		ERROR_OFI("Memory allocation error: hInput");
+	mi=(MYINPUT *)GlobalLock(hInput);
+	memset(mi,0,sizeof(MYINPUT));
 
 
- mi->IsAAC=strcmpi(lpstrFilename+lstrlen(lpstrFilename)-4,".aac")==0;
+	mi->IsAAC=strcmpi(lpstrFilename+lstrlen(lpstrFilename)-4,".aac")==0;
 
- if(!mi->IsAAC) // MP4 file ---------------------------------------------------------------------
- {
-// faacDecFrameInfo frameInfo;
- MP4FileHandle infile;
- MP4SampleId numSamples;
- MP4Duration length;
- int fileType=FAAD_FMT_16BIT; // default
- int track;
- unsigned __int32 buffer_size;
- unsigned long timeScale;
- unsigned char sf;
+	if(!mi->IsAAC) // MP4 file ---------------------------------------------------------------------
+	{
+	MP4Duration			length;
+	int					track;
+	unsigned __int32	buffer_size;
+	unsigned long		timeScale, sf;
 
-	if(!(infile = MP4Read(lpstrFilename, 0)))
-	    ERROR_ON_OPEN_MP4("Error opening file");
-	mi->mp4File=infile;
+		if(!(mi->mp4File = MP4Read(lpstrFilename, 0)))
+		    ERROR_OFI("Error opening file");
 
-    if ((track = GetAACTrack(infile)) < 0)
-		ERROR_ON_OPEN_MP4("Unable to find correct AAC sound track in the MP4 file");
+		if ((track = GetAACTrack(mi->mp4File)) < 0)
+			ERROR_OFI("Unable to find correct AAC sound track in the MP4 file");
 
-    length = MP4GetTrackDuration(infile, track);
-	mi->len_ms=(DWORD) MP4ConvertFromTrackDuration(infile, track, length, MP4_MSECS_TIME_SCALE);
+		if(!(mi->hDecoder=faacDecOpen()))
+			ERROR_OFI("Can't init library");
 
-    if(!(hDecoder=faacDecOpen()))
-		ERROR_ON_OPEN_MP4("Can't init library");
-    buffer = NULL;
-    buffer_size = 0;
-    MP4GetTrackESConfiguration(infile, track, &buffer, &buffer_size);
-    if(buffer)
-		AudioSpecificConfig(buffer, &timeScale, &channels, &sf, &mi->type);
-    mi->file_info.bitrate=(int)MP4GetTrackIntegerProperty(infile, track, "mdia.minf.stbl.stsd.mp4a.esds.decConfigDescr.avgBitrate");
-    numSamples = MP4GetTrackNumberOfSamples(infile, track);
+		mi->buffer = NULL;
+		buffer_size = 0;
+		MP4GetTrackESConfiguration(mi->mp4File, track, &mi->buffer, &buffer_size);
+	    if(!mi->buffer)
+			ERROR_OFI("MP4GetTrackESConfiguration");
 
-    if(faacDecInit2(hDecoder, buffer, buffer_size, &samplerate, &channels) < 0)
-		ERROR_ON_OPEN_MP4("Error initializing decoder library");
+		AudioSpecificConfig(mi->buffer, &timeScale, &channels, &sf, &mi->type);
+		if(memcmp(mpeg4AudioNames[mi->type],"AAC",3))
+			ERROR_OFI(0);
+		if(faacDecInit2(mi->hDecoder, mi->buffer, buffer_size, &samplerate, &channels) < 0)
+			ERROR_OFI("Error initializing decoder library");
 
-    if (buffer) free(buffer);
+	    free(mi->buffer);
 
-   mi->numSamples=numSamples;
-   mi->track=track;
-   mi->sampleId=1;
- }
- else // AAC file ------------------------------------------------------------------------------
- {   
- FILE   *infile;
- DWORD  pos; // into the file. Needed to obtain length of file
- DWORD  read;
- int    *seek_table;
- long tagsize;
+		length = MP4GetTrackDuration(mi->mp4File, track);
+		mi->len_ms=(DWORD) MP4ConvertFromTrackDuration(mi->mp4File, track, length, MP4_MSECS_TIME_SCALE);
+		mi->file_info.bitrate=(int)MP4GetTrackIntegerProperty(mi->mp4File, track, "mdia.minf.stbl.stsd.mp4a.esds.decConfigDescr.avgBitrate");
+		mi->numSamples = MP4GetTrackNumberOfSamples(mi->mp4File, track);
 
-  if(!(infile=fopen(lpstrFilename,"rb")))
-		ERROR_ON_OPEN_AAC("Error opening file"); 
+		mi->track=track;
+		mi->sampleId=1;
+	}
+	else // AAC file ------------------------------------------------------------------------------
+	{   
+	DWORD	pos; // into the file. Needed to obtain length of file
+	DWORD	read;
+	int		*seek_table;
+	long	tagsize;
 
-  mi->fFile=infile;
-  pos=ftell(infile);
-  fseek(infile, 0, SEEK_END);
-  mi->lSize=ftell(infile);
-  fseek(infile, pos, SEEK_SET);
+		if(!(mi->aacFile=fopen(lpstrFilename,"rb")))
+			ERROR_OFI("Error opening file"); 
 
-  if(!(buffer=(BYTE *)malloc(768*MAX_CHANNELS)))
-  {
-   MessageBox(0, "Memory allocation error: buffer", "FAAD interface", MB_OK);
-   GlobalUnlock(hInput);
-   return 0;
-  }
-  mi->buffer=buffer;
-  memset(buffer, 0, 768*MAX_CHANNELS);
+		pos=ftell(mi->aacFile);
+		fseek(mi->aacFile, 0, SEEK_END);
+		mi->lSize=ftell(mi->aacFile);
+		fseek(mi->aacFile, pos, SEEK_SET);
 
-  if(mi->lSize<768*MAX_CHANNELS)
-   tmp=mi->lSize;
-  else
-   tmp=768*MAX_CHANNELS;
-  read=fread(buffer, 1, tmp, infile);
-  if(read==tmp)
-  {
-   mi->bytes_read=read;
-   mi->bytes_into_buffer=read;
-  }
-  else
-	ERROR_ON_OPEN_AAC("fread");
+		if(!(mi->buffer=(BYTE *)malloc(768*MAX_CHANNELS)))
+			ERROR_OFI("Memory allocation error"); 
+		memset(mi->buffer, 0, 768*MAX_CHANNELS);
 
-  tagsize=id3v2_tag(buffer);
-  if(tagsize)
-  {
-   memcpy(buffer,buffer+tagsize,768*MAX_CHANNELS - tagsize);
+		if(mi->lSize<768*MAX_CHANNELS)
+			tmp=mi->lSize;
+		else
+			tmp=768*MAX_CHANNELS;
+		read=fread(mi->buffer, 1, tmp, mi->aacFile);
+		if(read==tmp)
+		{
+			mi->bytes_read=read;
+			mi->bytes_into_buffer=read;
+		}
+		else
+			ERROR_OFI("fread");
 
-   if(mi->bytes_read+tagsize<mi->lSize)
-	tmp=tagsize;
-   else
-    tmp=mi->lSize-mi->bytes_read;
-   read=fread(buffer+mi->bytes_into_buffer, 1, tmp, mi->fFile);
-   if(read==tmp)
-   {
-    mi->bytes_read+=read;
-    mi->bytes_into_buffer+=read;
-   }
-   else
-	ERROR_ON_OPEN_AAC("fread");
-  }
-  mi->tagsize=tagsize;
+		tagsize=id3v2_tag(mi->buffer);
+		if(tagsize)
+		{
+			memcpy(mi->buffer,mi->buffer+tagsize,768*MAX_CHANNELS - tagsize);
 
-  if(!(hDecoder=faacDecOpen()))
-	ERROR_ON_OPEN_AAC("Can't init library");
+			if(mi->bytes_read+tagsize<mi->lSize)
+				tmp=tagsize;
+			else
+				tmp=mi->lSize-mi->bytes_read;
+			read=fread(mi->buffer+mi->bytes_into_buffer, 1, tmp, mi->aacFile);
+			if(read==tmp)
+			{
+				mi->bytes_read+=read;
+				mi->bytes_into_buffer+=read;
+			}
+			else
+				ERROR_OFI("fread");
+		}
+		mi->tagsize=tagsize;
 
-  config = faacDecGetCurrentConfiguration(hDecoder);
-//  config->defObjectType = MAIN;
-  config->defSampleRate = 44100;
-  config->outputFormat=FAAD_FMT_16BIT;
-  faacDecSetConfiguration(hDecoder, config);
+		if(!(mi->hDecoder=faacDecOpen()))
+			ERROR_OFI("Can't open library");
 
-  if((mi->bytes_consumed=faacDecInit(hDecoder, buffer, &samplerate, &channels)) < 0)
-   ERROR_ON_OPEN_AAC("Error retrieving information form input file");
-  mi->bytes_into_buffer-=mi->bytes_consumed;
-// if(mi->bytes_consumed>0) 
-// faacDecInit reports there is an header to skip
+		if(seek_table=(int *)malloc(sizeof(int)*10800))
+		{
+			if(get_AAC_format(lpstrFilename, &(mi->file_info), seek_table)<0)
+				ERROR_OFI("Error retrieving information form input file");
+			free(seek_table);
+		}
+		if(mi->file_info.headertype==0)
+		{
+			config = faacDecGetCurrentConfiguration(mi->hDecoder);
+			config->defObjectType = mi->file_info.object_type;
+			config->defSampleRate = mi->file_info.sampling_rate;
+			config->outputFormat=FAAD_FMT_16BIT;
+			faacDecSetConfiguration(mi->hDecoder, config);
+		}
+
+		if((mi->bytes_consumed=faacDecInit(mi->hDecoder, mi->buffer, &samplerate, &channels)) < 0)
+			ERROR_OFI("Can't init library");
+		mi->bytes_into_buffer-=mi->bytes_consumed;
+// if(mi->bytes_consumed>0) faacDecInit reports there is an header to skip
 // this operation will be done in ReadFilterInput
 
+		mi->len_ms=(DWORD)((1000*((float)mi->lSize*8))/mi->file_info.bitrate);
+	} // END AAC file -----------------------------------------------------------------------------
 
-  if(seek_table=(int *)malloc(sizeof(int)*10800))
-  {
-   get_AAC_format(lpstrFilename, &(mi->file_info), seek_table);
-   free(seek_table);
-  }
-  if(!mi->file_info.version)
-   ERROR_ON_OPEN_AAC("Error retrieving information form input file");
+	config = faacDecGetCurrentConfiguration(mi->hDecoder);
+	switch(config->outputFormat)
+	{
+	case FAAD_FMT_16BIT:
+		BitsPerSample=16;
+		break;
+	case FAAD_FMT_24BIT:
+		BitsPerSample=24;
+		break;
+	case FAAD_FMT_32BIT:
+		BitsPerSample=32;
+		break;
+	default:
+		ERROR_OFI("Invalid format");
+	}
 
-  mi->len_ms=(DWORD)((1000*((float)mi->lSize*8))/mi->file_info.bitrate);
- } // END AAC file -----------------------------------------------------------------------------
+	if(mi->len_ms)
+		mi->full_size=(DWORD)(mi->len_ms*((float)samplerate/1000)*channels*(BitsPerSample/8));
+	else
+		mi->full_size=mi->lSize; // corrupted stream?
 
-  if(mi->len_ms)
-   mi->full_size=(DWORD)(mi->len_ms*((float)samplerate/1000)*channels*(16/8));
-  else
-   mi->full_size=mi->lSize; // corrupted stream?
+	*lSamprate=samplerate;
+	*wBitsPerSample=BitsPerSample;
+	*wChannels=(WORD)channels;
+	*lChunkSize=(BitsPerSample/8)*1024*channels;
 
-  mi->hDecoder=hDecoder;
-  *lSamprate=samplerate;
-  *wBitsPerSample=16;
-  *wChannels=(WORD)channels;
-  *lChunkSize=sizeof(short)*1024*channels;
+	mi->wChannels=(WORD)channels;
+	mi->dwSamprate=samplerate;
+	mi->wBitsPerSample=*wBitsPerSample;
+	strcpy(mi->szName,lpstrFilename);
 
-  mi->wChannels=(WORD)channels;
-  mi->dwSamprate=samplerate;
-  mi->wBitsPerSample=*wBitsPerSample;
-  strcpy(mi->szName,lpstrFilename);
+	GlobalUnlock(hInput);
 
-  GlobalUnlock(hInput);
-
- return hInput;
+	return hInput;
 }
+// *********************************************************************************************
 
-#define ERROR_ReadFilterInput(msg) \
-	{ \
-		if(msg) \
-			MessageBox(0, msg, "FAAD interface", MB_OK); \
+#define ERROR_RFI(msg) \
+{ \
+	if(msg) \
+		MessageBox(0, msg, "FAAD plugin", MB_OK); \
+	if(hInput) \
 		GlobalUnlock(hInput); \
-		return 0; \
-	} \
+	return 0; \
+}
 
 __declspec(dllexport) DWORD FAR PASCAL ReadFilterInput(HANDLE hInput, unsigned char far *bufout, long lBytes)
 {
-DWORD	read,
-		tmp,
-		shorts_decoded=0;
-unsigned char *buffer=0;
-faacDecFrameInfo frameInfo;
-char *sample_buffer=0;
-MYINPUT *mi;
+DWORD				read,
+					tmp,
+					shorts_decoded=0;
+unsigned char		*buffer=0;
+faacDecFrameInfo	frameInfo;
+char				*sample_buffer=0;
+MYINPUT				*mi;
 
 	if(!hInput)
-		return 0;
+		ERROR_RFI("Memory allocation error: hInput");
 	mi=(MYINPUT *)GlobalLock(hInput);
 
 	if(!mi->IsAAC) // MP4 file --------------------------------------------------------------------------
@@ -465,13 +461,13 @@
 		{
 			buffer=NULL;
 			if(mi->sampleId>=mi->numSamples)
-				ERROR_ReadFilterInput(0);
+				ERROR_RFI(0);
 
 			rc=MP4ReadSample(mi->mp4File, mi->track, mi->sampleId++, &buffer, &buffer_size, NULL, NULL, NULL, NULL);
 			if(rc==0 || buffer==NULL)
 			{
 				if(buffer) free(buffer);
-				ERROR_ReadFilterInput("MP4ReadSample")
+				ERROR_RFI("MP4ReadSample")
 			}
 
 			sample_buffer=(char *)faacDecDecode(mi->hDecoder,&frameInfo,buffer);
@@ -496,7 +492,7 @@
 						tmp=mi->bytes_consumed;
 					else
 						tmp=mi->lSize-mi->bytes_read;
-					read=fread(buffer+mi->bytes_into_buffer, 1, tmp, mi->fFile);
+					read=fread(buffer+mi->bytes_into_buffer, 1, tmp, mi->aacFile);
 					if(read==tmp)
 					{
 						mi->bytes_read+=read;
@@ -512,7 +508,7 @@
 
 			if(mi->bytes_into_buffer<1)
 				if(mi->bytes_read<mi->lSize)
-					ERROR_ReadFilterInput("ReadFilterInput: buffer empty!")
+					ERROR_RFI("ReadFilterInput: buffer empty!")
 				else
 					return 0;
 
@@ -527,7 +523,7 @@
 	GlobalUnlock(hInput);
 
 	if(frameInfo.error)
-		ERROR_ReadFilterInput(faacDecGetErrorMessage(frameInfo.error));
+		ERROR_RFI(faacDecGetErrorMessage(frameInfo.error));
 
 	return shorts_decoded;
 }
--- a/plugins/cooledit/Main.cpp
+++ b/plugins/cooledit/Main.cpp
@@ -69,16 +69,16 @@
 	lstrcpy(cq->szExt2,"MP4"); 
 	cq->lChunkSize=16384; 
 	cq->dwFlags=QF_RATEADJUSTABLE|QF_CANLOAD|QF_CANSAVE|QF_HASOPTIONSBOX;
- 	cq->Stereo8=0xFF; // supports all rates ???
- 	cq->Stereo16=0xFF;
- 	cq->Stereo24=0xFF;
- 	cq->Stereo32=0xFF;
  	cq->Mono8=0xFF;
  	cq->Mono16=0xFF;
  	cq->Mono24=0xFF;
  	cq->Mono32=0xFF;
- 	cq->Quad32=0xFF;
- 	cq->Quad16=0xFF;
+ 	cq->Stereo8=0xFF; // supports all rates ???
+ 	cq->Stereo16=0xFF;
+ 	cq->Stereo24=0xFF;
+ 	cq->Stereo32=0xFF;
  	cq->Quad8=0xFF;
+ 	cq->Quad16=0xFF;
+ 	cq->Quad32=0xFF;
  	return C_VALIDLIBRARY;
 }
--- a/plugins/cooledit/resource.h
+++ b/plugins/cooledit/resource.h
@@ -25,9 +25,9 @@
 // 
 #ifdef APSTUDIO_INVOKED
 #ifndef APSTUDIO_READONLY_SYMBOLS
-#define _APS_NEXT_RESOURCE_VALUE        106
+#define _APS_NEXT_RESOURCE_VALUE        105
 #define _APS_NEXT_COMMAND_VALUE         40001
-#define _APS_NEXT_CONTROL_VALUE         1014
+#define _APS_NEXT_CONTROL_VALUE         1015
 #define _APS_NEXT_SYMED_VALUE           101
 #endif
 #endif