OSDN Git Service

Implemented multi-threading for file analyzer. Now multiple files can be analyzed...
[lamexp/LameXP.git] / src / Thread_Initialization.cpp
index 5b3fbdf..f037ce6 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@
 #include "Thread_Initialization.h"
 
 #include "LockedFile.h"
+#include "Tools.h"
 
 #include <QFileInfo>
 #include <QCoreApplication>
 #include <QLibrary>
 #include <QResource>
 #include <QTime>
+#include <QTextStream>
+#include <QRunnable>
+#include <QThreadPool>
+#include <QMutex>
+
+/* helper macros */
+#define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X)
+static const double g_allowedExtractDelay = 12.0;
 
 ////////////////////////////////////////////////////////////
-// TOOLS
+// ExtractorTask class
 ////////////////////////////////////////////////////////////
 
-#define CPU_TYPE_X86 0x000001 //x86-32
-#define CPU_TYPE_SSE 0x000002 //x86-32 + SSE2 (Intel only!)
-#define CPU_TYPE_X64 0x000004 //x86-64
+class ExtractorTask : public QRunnable
+{
+public:
+       ExtractorTask(const QDir &appDir, const QString &toolName, const QString &toolShortName, const QByteArray &toolHash, const unsigned int toolVersion)
+       :
+               QRunnable(), m_appDir(appDir), m_toolName(toolName), m_toolShortName(toolShortName), m_toolHash(toolHash), m_toolVersion(toolVersion)
+       {
+               /* Nothing to do */
+       }
 
-#define CPU_TYPE_GEN (CPU_TYPE_X86|CPU_TYPE_SSE)              //Use for all CPU's, except for x86-64
-#define CPU_TYPE_ALL (CPU_TYPE_X86|CPU_TYPE_SSE|CPU_TYPE_X64) //Use for all CPU's, x86-32 and x86-64
+       static void clearFlags(void)
+       {
+               s_bAbort = s_bCustom = false;
+               s_errMsg[0] = '\0';
+       }
 
-static const struct
-{
-       char *pcHash;
-       unsigned int uiCpuType;
-       char *pcName;
-       unsigned int uiVersion;
-}
-g_lamexp_tools[] =
-{
-       {"cd702b111e13e3b6ed71cb0b6e6e9fc6be994296c65ed367c22ba382ab1e147ba126b02b", CPU_TYPE_X86, "aften.i386.exe", 8},
-       {"aa81f531c7cf1be38bfce1656113cfd6d682106958ab269dae8dff70abbc3e00615fdf4e", CPU_TYPE_SSE, "aften.sse2.exe", 8},
-       {"29da0d3e810bc3e8d2cddb3db452325eefca0d0c1fff1379fa17806ad447752be1b88e2f", CPU_TYPE_X64, "aften.x64.exe",  8},
-       {"1cca303fabd889a18fc01c32a7fd861194cfcac60ba63740ea2d7c55d049dbf8f59259fa", CPU_TYPE_ALL, "alac.exe", 20},
-       {"72d7ef3ecb727d6eaf64a8cca8bda2b6cc91154178d7d1783144c4acf1a0c1c2547ca324", CPU_TYPE_ALL, "avs2wav.exe", 12},
-       {"c6f23fb7ba18a7972802e013b66ec6ce52088bf4fe783774eb1d33dd3401fa8aad76cc8b", CPU_TYPE_ALL, "elevator.exe", UINT_MAX},
-       {"b71736a34afbe3259304a5b5dda980dfdbc3bf4fbc06c62a98a3ee4742deda377020d691", CPU_TYPE_ALL, "faad.exe", 27},
-       {"446054f9a7f705f1aadc9053ca7b8a86a775499ef159978954ebdea92de056c34f8841f7", CPU_TYPE_ALL, "flac.exe", 121},
-       {"dd68d074c5e13a607580f3a24595c0f3882e37211d2ca628f46e6df20fabcc832dad488a", CPU_TYPE_ALL, "gpgv.exe", 1411},
-       {"b3fca757b3567dab75c042e62213c231de378ea0fdd7fe29b733417cd5d3d33558452f94", CPU_TYPE_ALL, "gpgv.gpg", UINT_MAX},
-       {"9c1c8024d5d75213edbc1dbad7aeaf2535000f57880b445c763ac45da365446b8cfd84c7", CPU_TYPE_ALL, "lame.exe", 3990},
-       {"67933924d68ce319795989648f29e7bd1abaac4ec09c26cbb0ff0d15a67a9df17e257933", CPU_TYPE_ALL, "mac.exe", 406},
-       {"2a4407cc8df3c1dfefad9238b93df9d526a5e171567e5920c7cbb271275e132693356685", CPU_TYPE_GEN, "mediainfo.i386.exe", 746},
-       {"f1fa9cdadd35188ce2ca25549abb3155a04faeaff52f96ce2856cd28f6c07df0b4febbf8", CPU_TYPE_X64, "mediainfo.x64.exe",  746},
-       {"a93ec86187025e66fb78026af35555bd3b4e30fe1a40e8d66f600cfd918f07f431f0b2f2", CPU_TYPE_ALL, "mpcdec.exe", 435},
-       {"5a89768010efb1ddfd88ccc378a89433ceaecb403a7b1f83f8716f6848d9a05b3d3c6d93", CPU_TYPE_ALL, "mpg123.exe", 1133},
-       {"0c781805dda931c529bd16069215f616a7a4c5e5c2dfb6b75fe85d52b20511830693e528", CPU_TYPE_ALL, "oggdec.exe", UINT_MAX},
-       {"0c019e13450dc664987e21f4e5489d182be7d6d0d81efbbaaf1c78693dfe3e38e0355b93", CPU_TYPE_X86, "oggenc2.i386.exe", 287603},
-       {"693dd6f779df70a047c15c2c79350855db38d5b0cd7e529b6877b7c821cfe6addfdd50a4", CPU_TYPE_SSE, "oggenc2.sse2.exe", 287603},
-       {"32cb0b2182488e5e9278ba6b9fc9141214c7546eec67ee02fa895810b0e56900368695be", CPU_TYPE_X64, "oggenc2.x64.exe",  287603},
-       {"58c2b8bcff8f27bfa8fab8172b80f5da731221d072c7dba4dd3a3d7d6423490a25dc6760", CPU_TYPE_ALL, "shorten.exe", 361},
-       {"abdf9b20a8031a09d0abca9cb10c31c8418f72403b5d1350fd69bfa34041591aca3060ab", CPU_TYPE_ALL, "sox.exe", 1432},
-       {"48e7f81c024cd17dac0eaeab253aad6b223e72dc80688f7576276b0563209514ff0bb9c8", CPU_TYPE_ALL, "speexdec.exe", 12},
-       {"9b50cf64747d4afbad5d8d9b5a0a2d41c5a58256f47ebdbd8cc920e7e576085dfe1b14ff", CPU_TYPE_ALL, "tta.exe", 21},
-       {"875871c942846f6ad163f9e4949bba2f4331bec678ca5aefe58c961b6825bd0d419a078b", CPU_TYPE_ALL, "valdec.exe", 31},
-       {"e657331e281840878a37eb4fb357cb79f33d528ddbd5f9b2e2f7d2194bed4720e1af8eaf", CPU_TYPE_ALL, "wget.exe", 1114},
-       {"42a541d0fbcbc7ee9777f3519ffeee77f516ed26a01beb891fcc7c66d2a3e238407e6e6b", CPU_TYPE_ALL, "wma2wav.exe", 20110726},
-       {"a258711f7a8a0c75528f3ed4d2c17513ff8598b7e0a9d7db13ca941a3140094ffc2ffb62", CPU_TYPE_ALL, "wupdate.exe", UINT_MAX},
-       {"6b053b37d47a9c8659ebf2de43ad19dcba17b9cd868b26974b9cc8c27b6167e8bf07a5a2", CPU_TYPE_ALL, "wvunpack.exe", 4601},
-       {NULL, NULL, NULL, NULL}
+       static bool getAbort(void) { return s_bAbort; }
+       static bool getCustom(void) { return s_bCustom; }
+       static char *const getError(void) { return s_errMsg; }
+
+protected:
+       void run(void)
+       {
+               try
+               {
+                       LockedFile *lockedFile = NULL;
+                       unsigned int version = m_toolVersion;
+                       
+                       if(!s_bAbort)
+                       {
+                               QFileInfo customTool(QString("%1/tools/%2/%3").arg(m_appDir.canonicalPath(), QString::number(lamexp_version_build()), m_toolShortName));
+                               if(customTool.exists() && customTool.isFile())
+                               {
+                                       qDebug("Setting up file: %s <- %s", m_toolShortName.toLatin1().constData(), m_appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
+                                       lockedFile = new LockedFile(customTool.canonicalFilePath()); version = UINT_MAX; s_bCustom = true;
+                               }
+                               else
+                               {
+                                       qDebug("Extracting file: %s -> %s", m_toolName.toLatin1().constData(), m_toolShortName.toLatin1().constData());
+                                       lockedFile = new LockedFile(QString(":/tools/%1").arg(m_toolName), QString("%1/lamexp_%2").arg(lamexp_temp_folder2(), m_toolShortName), m_toolHash);
+                               }
+
+                               if(lockedFile)
+                               {
+                                       QMutexLocker lock(&s_mutex);
+                                       lamexp_register_tool(m_toolShortName, lockedFile, version);
+                               }
+                       }
+               }
+               catch(char *errorMsg)
+               {
+                       qWarning("At least one of the required tools could not be initialized:\n%s", errorMsg);
+                       if(s_mutex.tryLock())
+                       {
+                               if(!s_bAbort) { s_bAbort = true; strncpy_s(s_errMsg, 1024, errorMsg, _TRUNCATE); }
+                               s_mutex.unlock();
+                       }
+               }
+       }
+
+private:
+       const QDir m_appDir;
+       const QString m_toolName;
+       const QString m_toolShortName;
+       const QByteArray m_toolHash;
+       const unsigned int m_toolVersion;
+
+       static volatile bool s_bAbort;
+       static volatile bool s_bCustom;
+       static QMutex s_mutex;
+       static char s_errMsg[1024];
 };
 
-static const double g_allowedExtractDelay = 10.0;
+volatile bool ExtractorTask::s_bAbort = false;
+volatile bool ExtractorTask::s_bCustom = false;
+char ExtractorTask::s_errMsg[1024] = {'\0'};
+QMutex ExtractorTask::s_mutex;
 
 ////////////////////////////////////////////////////////////
 // Constructor
@@ -109,12 +146,41 @@ InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
 void InitializationThread::run()
 {
        m_bSuccess = false;
-       bool bCustom = false;
        delay();
 
        //CPU type selection
-       unsigned int cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64 : ((m_cpuFeatures.intel && m_cpuFeatures.sse && m_cpuFeatures.sse2) ? CPU_TYPE_SSE : CPU_TYPE_X86);
-       
+       unsigned int cpuSupport = 0;
+       if(m_cpuFeatures.sse && m_cpuFeatures.sse2 && m_cpuFeatures.intel)
+       {
+               cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_SSE : CPU_TYPE_X86_SSE;
+       }
+       else
+       {
+               cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_GEN : CPU_TYPE_X86_GEN;
+       }
+
+       //Hack to disable x64 on Wine, as x64 binaries won't run under Wine (tested with Wine 1.4 under Ubuntu 12.04 x64)
+       if(cpuSupport & CPU_TYPE_X64_ALL)
+       {
+               //DWORD osVerNo = lamexp_get_os_version();
+               //if((HIWORD(osVerNo) == 6) && (LOWORD(osVerNo) == 2))
+               if(lamexp_detect_wine())
+               {
+                       qWarning("Running under Wine on a 64-Bit system. Going to disable all x64 support!\n");
+                       cpuSupport = (cpuSupport == CPU_TYPE_X64_SSE) ? CPU_TYPE_X86_SSE : CPU_TYPE_X86_GEN;
+               }
+       }
+
+       //Print selected CPU type
+       switch(cpuSupport)
+       {
+               PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
+               PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
+               PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
+               PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
+               default: throw "CPU support undefined!";
+       }
+
        //Allocate maps
        QMap<QString, QString> mapChecksum;
        QMap<QString, unsigned int> mapVersion;
@@ -144,6 +210,15 @@ void InitializationThread::run()
        QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
        QDir appDir = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
 
+       QThreadPool *pool = new QThreadPool();
+       int idealThreadCount = QThread::idealThreadCount();
+       if(idealThreadCount > 0)
+       {
+               pool->setMaxThreadCount(idealThreadCount * 2);
+       }
+       
+       ExtractorTask::clearFlags();
+
        QTime timer;
        timer.start();
        
@@ -167,20 +242,8 @@ void InitializationThread::run()
                        
                        if(toolCpuType & cpuSupport)
                        {
-                               QFileInfo customTool(QString("%1/tools/%2/%3").arg(appDir.canonicalPath(), QString::number(lamexp_version_build()), toolShortName));
-                               if(customTool.exists() && customTool.isFile())
-                               {
-                                       bCustom = true;
-                                       qDebug("Setting up file: %s <- %s", toolShortName.toLatin1().constData(), appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
-                                       LockedFile *lockedFile = new LockedFile(customTool.canonicalFilePath());
-                                       lamexp_register_tool(toolShortName, lockedFile, UINT_MAX);
-                               }
-                               else
-                               {
-                                       qDebug("Extracting file: %s -> %s", toolName.toLatin1().constData(),  toolShortName.toLatin1().constData());
-                                       LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName), toolHash);
-                                       lamexp_register_tool(toolShortName, lockedFile, toolVersion);
-                               }
+                               pool->start(new ExtractorTask(appDir, toolName, toolShortName, toolHash, toolVersion));
+                               QThread::yieldCurrentThread();
                        }
                }
                catch(char *errorMsg)
@@ -189,14 +252,25 @@ void InitializationThread::run()
                        return;
                }
        }
-       
+
+       //Wait for extrator threads to finish
+       pool->waitForDone();
+       LAMEXP_DELETE(pool);
+
+       //Make sure all files were extracted correctly
+       if(ExtractorTask::getAbort())
+       {
+               qFatal("At least one of the required tools could not be initialized:\n%s", ExtractorTask::getError());
+               return;
+       }
+
        //Make sure all files were extracted
        if(!mapChecksum.isEmpty())
        {
                qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
                return;
        }
-       
+
        qDebug("All extracted.\n");
 
        //Clean-up
@@ -205,7 +279,7 @@ void InitializationThread::run()
        mapCpuType.clear();
        
        //Using any custom tools?
-       if(bCustom)
+       if(ExtractorTask::getCustom())
        {
                qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
        }
@@ -222,8 +296,10 @@ void InitializationThread::run()
        //Register all translations
        initTranslations();
 
-       //Look for Nero encoder
+       //Look for AAC encoders
        initNeroAac();
+       initFhgAac();
+       initQAac();
 
        delay();
        m_bSuccess = true;
@@ -263,33 +339,43 @@ void InitializationThread::initTranslations(void)
        while(!qmFiles.isEmpty())
        {
                QString langId, langName;
-               unsigned int systemId = 0;
+               unsigned int systemId = 0, country = 0;
                QString qmFile = qmFiles.takeFirst();
                
                QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
                if(langIdExp.indexIn(qmFile) >= 0)
                {
                        langId = langIdExp.cap(1).toLower();
+                       QResource langRes = QResource(QString(":/localization/%1.txt").arg(qmFile));
+                       if(langRes.isValid() && langRes.size() > 0)
+                       {
+                               QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(langRes.data()), langRes.size());
+                               QTextStream stream(&data, QIODevice::ReadOnly);
+                               stream.setAutoDetectUnicode(false); stream.setCodec("UTF-8");
+                               while(!stream.atEnd())
+                               {
+                                       QStringList langInfo = stream.readLine().simplified().split(",", QString::SkipEmptyParts);
+                                       if(langInfo.count() == 3)
+                                       {
+                                               systemId = langInfo.at(0).trimmed().toUInt();
+                                               country  = langInfo.at(1).trimmed().toUInt();
+                                               langName = langInfo.at(2).trimmed();
+                                               break;
+                                       }
+                               }
+                       }
                }
 
-               QResource langRes = (QString(":/localization/%1.txt").arg(qmFile));
-               if(langRes.isValid() && langRes.size() > 0)
+               if(!(langId.isEmpty() || langName.isEmpty() || systemId == 0))
                {
-                       QStringList langInfo = QString::fromUtf8(reinterpret_cast<const char*>(langRes.data()), langRes.size()).simplified().split(",", QString::SkipEmptyParts);
-                       if(langInfo.count() == 2)
+                       if(lamexp_translation_register(langId, qmFile, langName, systemId, country))
                        {
-                               systemId = langInfo.at(0).toUInt();
-                               langName = langInfo.at(1);
+                               qDebug("Registering translation: %s = %s (%u) [%u]", qmFile.toUtf8().constData(), langName.toUtf8().constData(), systemId, country);
+                       }
+                       else
+                       {
+                               qWarning("Failed to register: %s", qmFile.toLatin1().constData());
                        }
-               }
-               
-               if(lamexp_translation_register(langId, qmFile, langName, systemId))
-               {
-                       qDebug64("Registering translation: %1 = %2 (%3)", qmFile, langName, QString::number(systemId));
-               }
-               else
-               {
-                       qWarning("Failed to register: %s", qmFile.toLatin1().constData());
                }
        }
 
@@ -377,10 +463,10 @@ void InitializationThread::initNeroAac(void)
                                if(versionTokens.count() == 4)
                                {
                                        neroVersion = 0;
-                                       neroVersion += min(9, max(0, versionTokens.at(3).toInt()));
-                                       neroVersion += min(9, max(0, versionTokens.at(2).toInt())) * 10;
-                                       neroVersion += min(9, max(0, versionTokens.at(1).toInt())) * 100;
-                                       neroVersion += min(9, max(0, versionTokens.at(0).toInt())) * 1000;
+                                       neroVersion += qMin(9, qMax(0, versionTokens.at(3).toInt()));
+                                       neroVersion += qMin(9, qMax(0, versionTokens.at(2).toInt())) * 10;
+                                       neroVersion += qMin(9, qMax(0, versionTokens.at(1).toInt())) * 100;
+                                       neroVersion += qMin(9, qMax(0, versionTokens.at(0).toInt())) * 1000;
                                }
                        }
                }
@@ -399,86 +485,271 @@ void InitializationThread::initNeroAac(void)
        }
 }
 
+void InitializationThread::initFhgAac(void)
+{
+       const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
+       
+       QFileInfo fhgFileInfo[4];
+       fhgFileInfo[0] = QFileInfo(QString("%1/fhgaacenc.exe").arg(appPath));
+       fhgFileInfo[1] = QFileInfo(QString("%1/enc_fhgaac.dll").arg(appPath));
+       fhgFileInfo[2] = QFileInfo(QString("%1/nsutil.dll").arg(appPath));
+       fhgFileInfo[3] = QFileInfo(QString("%1/libmp4v2.dll").arg(appPath));
+       
+       bool fhgFilesFound = true;
+       for(int i = 0; i < 4; i++)      { if(!fhgFileInfo[i].exists()) fhgFilesFound = false; }
 
-//void InitializationThread::initWmaDec(void)
-//{
-//     static const char* wmaDecoderComponentPath = "NCH Software/Components/wmawav/wmawav.exe";
-//
-//     LockedFile *wmaFileBin = NULL;
-//     QFileInfo wmaFileInfo = QFileInfo(QString("%1/%2").arg(lamexp_known_folder(lamexp_folder_programfiles), wmaDecoderComponentPath));
-//
-//     if(!(wmaFileInfo.exists() && wmaFileInfo.isFile()))
-//     {
-//             wmaFileInfo.setFile(QString("%1/%2").arg(QDir(QCoreApplication::applicationDirPath()).canonicalPath(), "wmawav.exe"));
-//     }
-//     if(!(wmaFileInfo.exists() && wmaFileInfo.isFile()))
-//     {
-//             qDebug("WMA File Decoder not found -> WMA decoding support will be disabled!\n");
-//             return;
-//     }
-//
-//     try
-//     {
-//             wmaFileBin = new LockedFile(wmaFileInfo.canonicalFilePath());
-//     }
-//     catch(...)
-//     {
-//             qWarning("Failed to get excluive lock to WMA File Decoder binary -> WMA decoding support will be disabled!");
-//             return;
-//     }
-//
-//     QProcess process;
-//     process.setProcessChannelMode(QProcess::MergedChannels);
-//     process.setReadChannel(QProcess::StandardOutput);
-//     process.start(wmaFileInfo.canonicalFilePath(), QStringList());
-//
-//     if(!process.waitForStarted())
-//     {
-//             qWarning("WmaWav process failed to create!");
-//             qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
-//             process.kill();
-//             process.waitForFinished(-1);
-//             return;
-//     }
-//
-//     bool b_wmaWavFound = false;
-//
-//     while(process.state() != QProcess::NotRunning)
-//     {
-//             if(!process.waitForReadyRead())
-//             {
-//                     if(process.state() == QProcess::Running)
-//                     {
-//                             qWarning("WmaWav process time out -> killing!");
-//                             process.kill();
-//                             process.waitForFinished(-1);
-//                             return;
-//                     }
-//             }
-//             while(process.canReadLine())
-//             {
-//                     QString line = QString::fromUtf8(process.readLine().constData()).simplified();
-//                     if(line.contains("Usage: wmatowav.exe WMAFileSpec WAVFileSpec", Qt::CaseInsensitive))
-//                     {
-//                             b_wmaWavFound = true;
-//                     }
-//             }
-//     }
-//
-//     if(!b_wmaWavFound)
-//     {
-//             qWarning("WmaWav could not be identified -> WMA decoding support will be disabled!\n");
-//             LAMEXP_DELETE(wmaFileBin);
-//             return;
-//     }
-//
-//     qDebug("Found WMA File Decoder binary:\n%s\n", wmaFileInfo.canonicalFilePath().toUtf8().constData());
-//
-//     if(wmaFileBin)
-//     {
-//             lamexp_register_tool(wmaFileInfo.fileName(), wmaFileBin);
-//     }
-//}
+       //Lock the FhgAacEnc binaries
+       if(!fhgFilesFound)
+       {
+               qDebug("FhgAacEnc binaries not found -> FhgAacEnc support will be disabled!\n");
+               return;
+       }
+
+       qDebug("Found FhgAacEnc cli_exe:\n%s\n", fhgFileInfo[0].canonicalFilePath().toUtf8().constData());
+       qDebug("Found FhgAacEnc enc_dll:\n%s\n", fhgFileInfo[1].canonicalFilePath().toUtf8().constData());
+
+       LockedFile *fhgBin[4];
+       for(int i = 0; i < 4; i++) fhgBin[i] = NULL;
+
+       try
+       {
+               for(int i = 0; i < 4; i++)
+               {
+                       fhgBin[i] = new LockedFile(fhgFileInfo[i].canonicalFilePath());
+               }
+       }
+       catch(...)
+       {
+               for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
+               qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
+               return;
+       }
+
+       QProcess process;
+       process.setProcessChannelMode(QProcess::MergedChannels);
+       process.setReadChannel(QProcess::StandardOutput);
+       process.start(fhgFileInfo[0].canonicalFilePath(), QStringList() << "--version");
+
+       if(!process.waitForStarted())
+       {
+               qWarning("FhgAacEnc process failed to create!");
+               qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
+               process.kill();
+               process.waitForFinished(-1);
+               for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
+               return;
+       }
+
+       QRegExp fhgAacEncSig("fhgaacenc version (\\d+) by tmkk", Qt::CaseInsensitive);
+       unsigned int fhgVersion = 0;
+
+       while(process.state() != QProcess::NotRunning)
+       {
+               process.waitForReadyRead();
+               if(!process.bytesAvailable() && process.state() == QProcess::Running)
+               {
+                       qWarning("FhgAacEnc process time out -> killing!");
+                       process.kill();
+                       process.waitForFinished(-1);
+                       for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
+                       return;
+               }
+               while(process.bytesAvailable() > 0)
+               {
+                       QString line = QString::fromUtf8(process.readLine().constData()).simplified();
+                       if(fhgAacEncSig.lastIndexIn(line) >= 0)
+                       {
+                               bool ok = false;
+                               unsigned int temp = fhgAacEncSig.cap(1).toUInt(&ok);
+                               if(ok) fhgVersion = temp;
+                       }
+               }
+       }
+
+       if(!(fhgVersion > 0))
+       {
+               qWarning("FhgAacEnc version couldn't be determined -> FhgAacEnc support will be disabled!");
+               for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
+               return;
+       }
+       else if(fhgVersion < lamexp_toolver_fhgaacenc())
+       {
+               qWarning("FhgAacEnc version is too much outdated -> FhgAacEnc support will be disabled!");
+               for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
+               return;
+       }
+       
+       for(int i = 0; i < 4; i++)
+       {
+               lamexp_register_tool(fhgFileInfo[i].fileName(), fhgBin[i], fhgVersion);
+       }
+}
+
+void InitializationThread::initQAac(void)
+{
+       const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
+
+       QFileInfo qaacFileInfo[2];
+       qaacFileInfo[0] = QFileInfo(QString("%1/qaac.exe").arg(appPath));
+       qaacFileInfo[1] = QFileInfo(QString("%1/libsoxrate.dll").arg(appPath));
+       
+       bool qaacFilesFound = true;
+       for(int i = 0; i < 2; i++)      { if(!qaacFileInfo[i].exists()) qaacFilesFound = false; }
+
+       //Lock the QAAC binaries
+       if(!qaacFilesFound)
+       {
+               qDebug("QAAC binaries not found -> QAAC support will be disabled!\n");
+               return;
+       }
+
+       qDebug("Found QAAC encoder:\n%s\n", qaacFileInfo[0].canonicalFilePath().toUtf8().constData());
+
+       LockedFile *qaacBin[2];
+       for(int i = 0; i < 2; i++) qaacBin[i] = NULL;
+
+       try
+       {
+               for(int i = 0; i < 2; i++)
+               {
+                       qaacBin[i] = new LockedFile(qaacFileInfo[i].canonicalFilePath());
+               }
+       }
+       catch(...)
+       {
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               qWarning("Failed to get excluive lock to QAAC binary -> QAAC support will be disabled!");
+               return;
+       }
+
+       QProcess process;
+       process.setProcessChannelMode(QProcess::MergedChannels);
+       process.setReadChannel(QProcess::StandardOutput);
+       process.start(qaacFileInfo[0].canonicalFilePath(), QStringList() << "--check");
+
+       if(!process.waitForStarted())
+       {
+               qWarning("QAAC process failed to create!");
+               qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
+               process.kill();
+               process.waitForFinished(-1);
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
+       }
+
+       QRegExp qaacEncSig("qaac (\\d)\\.(\\d)(\\d)", Qt::CaseInsensitive);
+       QRegExp coreEncSig("CoreAudioToolbox (\\d)\\.(\\d)\\.(\\d)\\.(\\d)", Qt::CaseInsensitive);
+       unsigned int qaacVersion = 0;
+       unsigned int coreVersion = 0;
+
+       while(process.state() != QProcess::NotRunning)
+       {
+               process.waitForReadyRead();
+               if(!process.bytesAvailable() && process.state() == QProcess::Running)
+               {
+                       qWarning("QAAC process time out -> killing!");
+                       process.kill();
+                       process.waitForFinished(-1);
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+                       return;
+               }
+               while(process.bytesAvailable() > 0)
+               {
+                       QString line = QString::fromUtf8(process.readLine().constData()).simplified();
+                       if(qaacEncSig.lastIndexIn(line) >= 0)
+                       {
+                               unsigned int tmp[3] = {0, 0, 0};
+                               bool ok[3] = {false, false, false};
+                               tmp[0] = qaacEncSig.cap(1).toUInt(&ok[0]);
+                               tmp[1] = qaacEncSig.cap(2).toUInt(&ok[1]);
+                               tmp[2] = qaacEncSig.cap(3).toUInt(&ok[2]);
+                               if(ok[0] && ok[1] && ok[2])
+                               {
+                                       qaacVersion = (qBound(0U, tmp[0], 9U) * 100) + (qBound(0U, tmp[1], 9U) * 10) + qBound(0U, tmp[2], 9U);
+                               }
+                       }
+                       if(coreEncSig.lastIndexIn(line) >= 0)
+                       {
+                               unsigned int tmp[4] = {0, 0, 0, 0};
+                               bool ok[4] = {false, false, false, false};
+                               tmp[0] = coreEncSig.cap(1).toUInt(&ok[0]);
+                               tmp[1] = coreEncSig.cap(2).toUInt(&ok[1]);
+                               tmp[2] = coreEncSig.cap(3).toUInt(&ok[2]);
+                               tmp[3] = coreEncSig.cap(4).toUInt(&ok[3]);
+                               if(ok[0] && ok[1] && ok[2] && ok[3])
+                               {
+                                       coreVersion = (qBound(0U, tmp[0], 9U) * 1000) + (qBound(0U, tmp[1], 9U) * 100) + (qBound(0U, tmp[2], 9U) * 10) + qBound(0U, tmp[3], 9U);
+                               }
+                       }
+               }
+       }
+
+       //qDebug("qaac %d, CoreAudioToolbox %d", qaacVersion, coreVersion);
+
+       if(!(qaacVersion > 0))
+       {
+               qWarning("QAAC version couldn't be determined -> QAAC support will be disabled!");
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
+       }
+       else if(qaacVersion < lamexp_toolver_qaacenc())
+       {
+               qWarning("QAAC version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.??", qaacVersion, "N/A").toLatin1().constData());
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
+       }
+
+       if(!(coreVersion > 0))
+       {
+               qWarning("CoreAudioToolbox version couldn't be determined -> QAAC support will be disabled!");
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
+       }
+       else if(coreVersion < lamexp_toolver_coreaudio())
+       {
+               qWarning("CoreAudioToolbox version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.?.?.?", coreVersion, "N/A").toLatin1().constData());
+               for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
+               return;
+       }
+
+       lamexp_register_tool(qaacFileInfo[0].fileName(), qaacBin[0], qaacVersion);
+       lamexp_register_tool(qaacFileInfo[1].fileName(), qaacBin[1], qaacVersion);
+}
+
+void InitializationThread::selfTest(void)
+{
+       const unsigned int cpu[4] = {CPU_TYPE_X86_GEN, CPU_TYPE_X86_SSE, CPU_TYPE_X64_GEN, CPU_TYPE_X64_SSE};
+
+       for(size_t k = 0; k < 4; k++)
+       {
+               qDebug("[TEST]");
+               switch(cpu[k])
+               {
+                       PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
+                       PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
+                       PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
+                       PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
+                       default: throw "CPU support undefined!";
+               }
+               int n = 0;
+               for(int i = 0; i < INT_MAX; i++)
+               {
+                       if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
+                       {
+                               break;
+                       }
+                       if(g_lamexp_tools[i].uiCpuType & cpu[k])
+                       {
+                               qDebug("%02i -> %s", ++n, g_lamexp_tools[i].pcName);
+                       }
+               }
+               if(n != 25)
+               {
+                       qFatal("Tool count mismatch !!!");
+               }
+               qDebug("Done.\n");
+       }
+}
 
 ////////////////////////////////////////////////////////////
 // EVENTS