shithub: aacenc

Download patch

ref: 695a501740c4271dce621bff17cc64dec8a480df
parent: 220dc0a03d5d592da832c081e8027062d16baae5
author: xfhobbes <xfhobbes>
date: Sun Mar 18 12:10:09 EST 2001

Rewrote DoProcessing() method to comply to rules of Hungarian Notation and to common C++ code styles.

--- a/wingui/EncoderJobProcessingManager.cpp
+++ b/wingui/EncoderJobProcessingManager.cpp
@@ -18,10 +18,6 @@
 
 //#define DUMMY_ENCODERJOB_PROCESSING
 
-// constants copied from reference implementation in VC project faacgui
-#define PCMBUFSIZE 1024
-#define BITBUFSIZE 8192
-
 //////////////////////////////////////////////////////////////////////
 // Construction/Destruction
 //////////////////////////////////////////////////////////////////////
@@ -166,6 +162,7 @@
 }
 
 #else
+
 bool CEncoderJobProcessingManager::DoProcessing()
 {
 	long lStartTimeMillis=::GetTickCount();
@@ -172,176 +169,22 @@
 	const CEncoderJob *poJob=m_poJobToProcess;
 	bool bInterrupted=false;
 
-	SNDFILE *infile;
-	SF_INFO sfinfo;
-
-	// open the input file
-	if ((infile = sf_open_read(poJob->GetFiles().GetCompleteSourceFilePath(), &sfinfo)) != NULL)
-	{
-		// determine input file parameters
-		unsigned int sampleRate = sfinfo.samplerate;
-		unsigned int numChannels = sfinfo.channels;
-
-		// open and setup the encoder
-		unsigned long inputSamples;
-		unsigned long maxOutputSize;
-		faacEncHandle hEncoder = faacEncOpen(sampleRate, numChannels, &inputSamples, &maxOutputSize);
-		if (hEncoder)
-		{
-			HANDLE hOutfile;
-
-			// set encoder configuration
-			faacEncConfigurationPtr config = faacEncGetCurrentConfiguration(hEncoder);
-
-			config->allowMidside = poJob->GetAllowMidside() ? 1 : 0;
-			config->useTns = poJob->GetUseTns() ? 1 : 0;
-			config->useLtp = poJob->GetUseLtp() ? 1 : 0;
-			config->useLfe = poJob->GetUseLfe() ? 1 : 0;
-			config->bitRate = poJob->GetBitRate();
-			config->bandWidth = poJob->GetBandwidth();
-			config->aacProfile = GetAacProfileConstant(poJob->GetAacProfile());
-
-			if (!faacEncSetConfiguration(hEncoder, config))
-			{
-				faacEncClose(hEncoder);
-				sf_close(infile);
-
-				AfxMessageBox("faacEncSetConfiguration failed!", MB_OK | MB_ICONSTOP);
-
-				return false;
-			}
-
-			// open the output file
-			hOutfile = CreateFile(poJob->GetFiles().GetCompleteTargetFilePath(), GENERIC_WRITE, 0, NULL,
-				CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-
-			if (hOutfile != INVALID_HANDLE_VALUE)
-			{
-				UINT startTime = GetTickCount(), lastUpdated = 0;
-				DWORD totalBytesRead = 0;
-
-				unsigned int bytesInput = 0, bytesConsumed = 0;
-				DWORD numberOfBytesWritten = 0;
-				short *pcmbuf;
-				unsigned char *bitbuf;
-
-				pcmbuf = (short*)malloc(PCMBUFSIZE*numChannels*sizeof(short));
-				bitbuf = (unsigned char*)malloc(BITBUFSIZE*sizeof(unsigned char));
-
-				while (true)
-				{
-					int bytesWritten;
-					int samplesToRead = PCMBUFSIZE;
-
-					bytesInput = sf_read_short(infile, pcmbuf, numChannels*PCMBUFSIZE) * sizeof(short);
-					
-					//SendDlgItemMessage (hWnd, IDC_PROGRESS, PBM_SETPOS, (unsigned long)((float)totalBytesRead * 1024.0f / (sfinfo.samples*2*numChannels)), 0);
-					
-					totalBytesRead += bytesInput;
-
-					// call the actual encoding routine
-					bytesWritten = faacEncEncode(hEncoder,
-						pcmbuf,
-						bytesInput/2,
-						bitbuf,
-						BITBUFSIZE);
-
-					switch (m_eCurrentWorkingState)
-					{
-					case eRunning:
-						{
-							// just report our current state and process waiting window messages
-							WriteProgress(lStartTimeMillis, (sfinfo.samples*2*numChannels), totalBytesRead);
-							m_poInfoTarget->ProcessUserMessages();
-							break;
-						}
-					case ePaused:
-						{
-							// must wait
-							while (m_eCurrentWorkingState==ePaused)
-							{
-								// be idle
-								m_poInfoTarget->ProcessUserMessages();
-								Sleep(200);
-							}
-							break;
-						}
-					case eStopped:
-						{
-							// must interrupt
-							bInterrupted=true;
-							break;
-						}
-					}
-					
-					if (bInterrupted) 
-					{
-						// Stop Pressed
-						break;
-					}
-
-					if (!bytesInput && !bytesWritten)
-					{
-						// all done, bail out
-						break;
-					}
-
-					if (bytesWritten < 0)
-					{
-						AfxMessageBox("faacEncEncodeFrame failed!", MB_OK | MB_ICONSTOP);
-						bInterrupted=true;
-						break;
-					}
-
-					WriteFile(hOutfile, bitbuf, bytesWritten, &numberOfBytesWritten, NULL);
-				}
-
-				CloseHandle(hOutfile);
-				if (pcmbuf) free(pcmbuf);
-				if (bitbuf) free(bitbuf);
-			}
-
-			faacEncClose(hEncoder);
-		}
-
-		sf_close(infile);
-		MessageBeep(1);
-	}
-	else
-	{
-		AfxMessageBox("Couldn't open input file!", MB_OK | MB_ICONSTOP);
-		bInterrupted=true;
-	}
-
-	return !bInterrupted;
-}
-
-
-// this is a version that I will try to put more the Hungarian Notation and C++ style
-/*bool CEncoderJobProcessingManager::DoProcessing()
-{
-	long lStartTimeMillis=::GetTickCount();
-	const CEncoderJob *poJob=m_poJobToProcess;
-	bool bInterrupted=false;
-
-	SNDFILE *phInfile;
+	SNDFILE *phInFile;
 	SF_INFO sctSfInfo;
 
 	// open the input file
-	if ((phInfile=sf_open_read(poJob->GetFiles().GetCompleteSourceFilePath(), &sctSfInfo)) != NULL)
+	if ((phInFile=sf_open_read(poJob->GetFiles().GetCompleteSourceFilePath(), &sctSfInfo)) != NULL)
 	{
 		// determine input file parameters
-		unsigned int uiSampleRate=sctSfInfo.samplerate;
-		unsigned int uiNumChannels=sctSfInfo.channels;
+		long lSampleRate=sctSfInfo.samplerate;
+		long lNumChannels=sctSfInfo.channels;
 
 		// open and setup the encoder
 		unsigned long ulInputSamplesPerLoopCycle;
-		unsigned long ulMaxLoopCycleCompressionOutputSize;
-		faacEncHandle hEncoder=faacEncOpen(uiSampleRate, uiNumChannels, &ulInputSamplesPerLoopCycle, &ulMaxLoopCycleCompressionOutputSize);
-		if (hEncoder)
+		unsigned long ulMaxLoopCycleOutputSize;
+		faacEncHandle hEncoder=faacEncOpen(lSampleRate, lNumChannels, &ulInputSamplesPerLoopCycle, &ulMaxLoopCycleOutputSize);
+		if (hEncoder!=0)
 		{
-			HANDLE hOutfile;
-
 			// set encoder configuration
 			faacEncConfigurationPtr pEncConfig=faacEncGetCurrentConfiguration(hEncoder);
 
@@ -356,7 +199,7 @@
 			if (!faacEncSetConfiguration(hEncoder, pEncConfig))
 			{
 				faacEncClose(hEncoder);
-				sf_close(phInfile);
+				sf_close(phInFile);
 
 				AfxMessageBox("faacEncSetConfiguration failed!", MB_OK | MB_ICONSTOP);
 
@@ -364,39 +207,37 @@
 			}
 
 			// open the output file
-			hOutfile=CreateFile(poJob->GetFiles().GetCompleteTargetFilePath(), GENERIC_WRITE, 0, NULL,
-				CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-
-			if (hOutfile != INVALID_HANDLE_VALUE)
+			CFile *poFile;
+			CArchive *poTargetFileOutputArchive;
+			if (OpenOutputFileArchive(poJob->GetFiles().GetCompleteTargetFilePath(), poFile, poTargetFileOutputArchive))
 			{
-				UINT startTime = GetTickCount(), lastUpdated = 0;
-				DWORD totalBytesRead = 0;
+				long lStartTime=GetTickCount();
+				long lLastUpdated=0;
+				long lTotalBytesRead = 0;
 
-				unsigned int bytesInput = 0, bytesConsumed = 0;
-				DWORD numberOfBytesWritten = 0;
-				short *pcmbuf;
-				unsigned char *bitbuf;
+				long lSamplesInput=0;
+				long lBytesConsumed=0;
+				short *parsPcmBuf;
+				char *parcBitBuf;
 
-				pcmbuf = (short*)malloc(PCMBUFSIZE*numChannels*sizeof(short));
-				bitbuf = (unsigned char*)malloc(BITBUFSIZE*sizeof(unsigned char));
+				parsPcmBuf=new short[ulInputSamplesPerLoopCycle];
+				parcBitBuf=new char[ulMaxLoopCycleOutputSize];
 
 				while (true)
 				{
-					int bytesWritten;
-					int samplesToRead = PCMBUFSIZE;
+					long lBytesWritten;
 
-					bytesInput = sf_read_short(infile, pcmbuf, numChannels*PCMBUFSIZE) * sizeof(short);
+					lSamplesInput=sf_read_short(phInFile, parsPcmBuf, ulInputSamplesPerLoopCycle);
 					
-					//SendDlgItemMessage (hWnd, IDC_PROGRESS, PBM_SETPOS, (unsigned long)((float)totalBytesRead * 1024.0f / (sfinfo.samples*2*numChannels)), 0);
-					
-					totalBytesRead += bytesInput;
+					lTotalBytesRead+=lSamplesInput*sizeof(short);
 
 					// call the actual encoding routine
-					bytesWritten = faacEncEncode(hEncoder,
-						pcmbuf,
-						bytesInput/2,
-						bitbuf,
-						BITBUFSIZE);
+					lBytesWritten=faacEncEncode(
+						hEncoder,
+						parsPcmBuf,
+						lSamplesInput,
+						parcBitBuf,
+						ulMaxLoopCycleOutputSize);
 
 					switch (m_eCurrentWorkingState)
 					{
@@ -403,7 +244,7 @@
 					case eRunning:
 						{
 							// just report our current state and process waiting window messages
-							WriteProgress(lStartTimeMillis, (sfinfo.samples*2*numChannels), totalBytesRead);
+							WriteProgress(lStartTimeMillis, (sctSfInfo.samples*sizeof(short)*lNumChannels), lTotalBytesRead);
 							m_poInfoTarget->ProcessUserMessages();
 							break;
 						}
@@ -432,13 +273,13 @@
 						break;
 					}
 
-					if (!bytesInput && !bytesWritten)
+					if (lSamplesInput==0 && lBytesWritten==0)
 					{
 						// all done, bail out
 						break;
 					}
 
-					if (bytesWritten < 0)
+					if (lBytesWritten < 0)
 					{
 						AfxMessageBox("faacEncEncodeFrame failed!", MB_OK | MB_ICONSTOP);
 						bInterrupted=true;
@@ -445,19 +286,21 @@
 						break;
 					}
 
-					WriteFile(hOutfile, bitbuf, bytesWritten, &numberOfBytesWritten, NULL);
+					poTargetFileOutputArchive->Write(parcBitBuf, lBytesWritten);
 				}
 
-				CloseHandle(hOutfile);
-				if (pcmbuf) free(pcmbuf);
-				if (bitbuf) free(bitbuf);
+				// close the target file
+				if (poTargetFileOutputArchive!=0) delete poTargetFileOutputArchive;
+				if (poFile!=0) delete poFile;
+				if (parsPcmBuf!=0) delete[] parsPcmBuf;
+				if (parcBitBuf!=0) delete[] parcBitBuf;
 			}
 
 			faacEncClose(hEncoder);
 		}
 
-		sf_close(infile);
-		MessageBeep(1);
+		sf_close(phInFile);
+		//MessageBeep(1);		// no more done here
 	}
 	else
 	{
@@ -466,7 +309,7 @@
 	}
 
 	return !bInterrupted;
-}*/
+}
 #endif
 
 void CEncoderJobProcessingManager::WriteProgress(long lOperationStartTickCount, long lMaxSteps, long lCurSteps)
@@ -512,7 +355,30 @@
 	}
 }
 
-CArchive* CEncoderJobProcessingManager::GetOutputFileArchive(const CString &oFileName)
+bool CEncoderJobProcessingManager::OpenOutputFileArchive(const CString &oFileName, CFile* &poFile, CArchive* &poArchive)
 {
-	return 0;
+	try
+	{
+		poFile=0;
+		poArchive=0;
+
+		// open the file
+		poFile=new CFile(oFileName, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
+		poArchive=new CArchive(poFile, CArchive::store);
+
+		return true;
+	}
+	catch (...)
+	{
+		// error opening the file for exclusive writing
+		if (poArchive!=0)
+		{
+			delete poArchive;
+		}
+		if (poFile!=0)
+		{
+			delete poFile;
+		}
+		return false;
+	}
 }
\ No newline at end of file
--- a/wingui/EncoderJobProcessingManager.h
+++ b/wingui/EncoderJobProcessingManager.h
@@ -48,7 +48,14 @@
 
 	static int GetAacProfileConstant(CEncoderJob::EAacProfile eAacProfile);
 
-	static CArchive* GetOutputFileArchive(const CString &oFileName);
+	// opens an archive that writes in the specified file, 0 in
+	// case of errors;
+	// the caller must specify two pointers that are both initialized
+	// by this method; the caller must delete these two objects when
+	// he's finished - first the archive, then the file;
+	// this method returns false in case of errors; then neither of
+	// the two pointers must be tried to be deleted
+	static bool OpenOutputFileArchive(const CString &oFileName, CFile* &poFile, CArchive* &poArchive);
 };
 
 #endif // !defined(AFX_ENCODERJOBPROCESSINGMANAGER_H__A1444E93_1546_11D5_8402_0080C88C25BD__INCLUDED_)