Updated OggEnc binary to v2.87 using aoTuV Beta-6.03 (2011-05-04), compiled with...
[LameXP.git] / src / Thread_Initialization.cpp
blob12b7bddb124b146a600496e26fe124478c0c6ad7
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Thread_Initialization.h"
24 #include "LockedFile.h"
26 #include <QFileInfo>
27 #include <QCoreApplication>
28 #include <QProcess>
29 #include <QMap>
30 #include <QDir>
31 #include <QLibrary>
32 #include <QResource>
33 #include <QTime>
35 ////////////////////////////////////////////////////////////
36 // TOOLS
37 ////////////////////////////////////////////////////////////
39 #define CPU_TYPE_X86 0x000001 //x86-32
40 #define CPU_TYPE_SSE 0x000002 //x86-32 + SSE2 (Intel only!)
41 #define CPU_TYPE_X64 0x000004 //x86-64
43 #define CPU_TYPE_GEN (CPU_TYPE_X86|CPU_TYPE_SSE) //Use for all CPU's, except for x86-64
44 #define CPU_TYPE_ALL (CPU_TYPE_X86|CPU_TYPE_SSE|CPU_TYPE_X64) //Use for all CPU's, x86-32 and x86-64
46 static const struct
48 char *pcHash;
49 unsigned int uiCpuType;
50 char *pcName;
51 unsigned int uiVersion;
53 g_lamexp_tools[] =
55 {"0a6252606c1ceda7b8973e5935ef72d60b8fd64d", CPU_TYPE_X86, "aften.i386.exe", 8},
56 {"22253052acba92a0088bbf0aa82a8c505c07b854", CPU_TYPE_SSE, "aften.sse2.exe", 8},
57 {"2996a48b01b65a2c1806482654beeea7ffcf1f80", CPU_TYPE_X64, "aften.x64.exe", 8},
58 {"3b41f85dde8d4a5a0f4cd5f461099d0db24610ba", CPU_TYPE_ALL, "alac.exe", UINT_MAX},
59 {"fb74ac8b73ad8cba2c3b4e6e61f23401d630dc22", CPU_TYPE_ALL, "elevator.exe", UINT_MAX},
60 {"80e372d8b20be24102c18284286fcdf5fa14bd86", CPU_TYPE_ALL, "faad.exe", 27},
61 {"d33cd86f04bd4067e244d2804466583c7b90a4e2", CPU_TYPE_ALL, "flac.exe", 121},
62 {"9328a50e89b54ec065637496d9681a7e3eebf915", CPU_TYPE_ALL, "gpgv.exe", 1411},
63 {"d837bf6ee4dab557d8b02d46c75a24e58980fffa", CPU_TYPE_ALL, "gpgv.gpg", UINT_MAX},
64 {"d5b3b80220d85a9fd2f486e37c1fb6511f3c2d72", CPU_TYPE_ALL, "lame.exe", 3990},
65 {"a4e929cfaa42fa2e61a3d0c6434c77a06d45aef3", CPU_TYPE_ALL, "mac.exe", 406},
66 {"a9aa99209fb9ad6ceb97b7a46774fc97141d3dce", CPU_TYPE_GEN, "mediainfo.i386.exe", 745},
67 {"9f6b81378e4c408fe5ded8a26ddd0d6c705d81cb", CPU_TYPE_X64, "mediainfo.x64.exe", 745},
68 {"aa89763a5ba4d1a5986549b9ee53e005c51940c1", CPU_TYPE_ALL, "mpcdec.exe", 435},
69 {"c327400fcee268f581d8c03e2a5cbbe8031abb6f", CPU_TYPE_ALL, "mpg123.exe", 1133},
70 {"8dd7138714c3bcb39f5a3213413addba13d06f1e", CPU_TYPE_ALL, "oggdec.exe", UINT_MAX},
71 {"97fed9ab0657baa36b6c7764461d66318654dbe6", CPU_TYPE_X86, "oggenc2.i386.exe", 287603},
72 {"e6dc1f31ce822588fa9cf52a341cebffd526546c", CPU_TYPE_SSE, "oggenc2.sse2.exe", 287603},
73 {"79aa9da24b2a0728cdf0a6927be316b864595b83", CPU_TYPE_X64, "oggenc2.x64.exe", 287603},
74 {"0d9035bb62bdf46a2785261f8be5a4a0972abd15", CPU_TYPE_ALL, "shorten.exe", 361},
75 {"50ead3b852cbfc067a402e6c2d0d0d8879663dec", CPU_TYPE_ALL, "sox.exe", 1432},
76 {"8671e16497a2d217d3707d4aa418678d02b16bcc", CPU_TYPE_ALL, "speexdec.exe", 12},
77 {"093bfdec22872ca99e40183937c88785468be989", CPU_TYPE_ALL, "tta.exe", 21},
78 {"8c842eef65248b46fa6cb9a9e5714f575672d999", CPU_TYPE_ALL, "valdec.exe", 31},
79 {"62e2805d1b2eb2a4d86a5ca6e6ea58010d05d2a7", CPU_TYPE_ALL, "wget.exe", 1114},
80 {"a7e8aad52213e339ad985829722f35eab62be182", CPU_TYPE_ALL, "wupdate.exe", UINT_MAX},
81 {"b7d14b3540d24df13119a55d97623a61412de6e3", CPU_TYPE_ALL, "wvunpack.exe", 4601},
82 {NULL, NULL, NULL, NULL}
85 ////////////////////////////////////////////////////////////
86 // Constructor
87 ////////////////////////////////////////////////////////////
89 InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
91 m_bSuccess = false;
92 memset(&m_cpuFeatures, 0, sizeof(lamexp_cpu_t));
94 if(cpuFeatures)
96 memcpy(&m_cpuFeatures, cpuFeatures, sizeof(lamexp_cpu_t));
100 ////////////////////////////////////////////////////////////
101 // Thread Main
102 ////////////////////////////////////////////////////////////
104 void InitializationThread::run()
106 m_bSuccess = false;
107 bool bCustom = false;
108 delay();
110 //CPU type selection
111 unsigned int cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64 : ((m_cpuFeatures.intel && m_cpuFeatures.sse && m_cpuFeatures.sse2) ? CPU_TYPE_SSE : CPU_TYPE_X86);
113 //Allocate maps
114 QMap<QString, QString> mapChecksum;
115 QMap<QString, unsigned int> mapVersion;
116 QMap<QString, unsigned int> mapCpuType;
118 //Init properties
119 for(int i = 0; i < INT_MAX; i++)
121 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
123 break;
125 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
127 const QString currentTool = QString::fromLatin1(g_lamexp_tools[i].pcName);
128 mapChecksum.insert(currentTool, QString::fromLatin1(g_lamexp_tools[i].pcHash));
129 mapCpuType.insert(currentTool, g_lamexp_tools[i].uiCpuType);
130 mapVersion.insert(currentTool, g_lamexp_tools[i].uiVersion);
132 else
134 qFatal("Inconsistent checksum data detected. Take care!");
138 QDir toolsDir(":/tools/");
139 QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
140 QDir appDir = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
142 QTime timer;
143 timer.start();
145 //Extract all files
146 while(!toolsList.isEmpty())
150 QFileInfo currentTool = toolsList.takeFirst();
151 QString toolName = currentTool.fileName().toLower();
152 QString toolShortName = QString("%1.%2").arg(currentTool.baseName().toLower(), currentTool.suffix().toLower());
154 QByteArray toolHash = mapChecksum.take(toolName).toLatin1();
155 unsigned int toolCpuType = mapCpuType.take(toolName);
156 unsigned int toolVersion = mapVersion.take(toolName);
158 if(toolHash.size() != 40)
160 throw "The required checksum is missing, take care!";
163 if(toolCpuType & cpuSupport)
165 QFileInfo customTool(QString("%1/tools/%2/%3").arg(appDir.canonicalPath(), QString::number(lamexp_version_build()), toolShortName));
166 if(customTool.exists() && customTool.isFile())
168 bCustom = true;
169 qDebug("Setting up file: %s <- %s", toolShortName.toLatin1().constData(), appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
170 LockedFile *lockedFile = new LockedFile(customTool.canonicalFilePath());
171 lamexp_register_tool(toolShortName, lockedFile, UINT_MAX);
173 else
175 qDebug("Extracting file: %s -> %s", toolName.toLatin1().constData(), toolShortName.toLatin1().constData());
176 LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName), toolHash);
177 lamexp_register_tool(toolShortName, lockedFile, toolVersion);
181 catch(char *errorMsg)
183 qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg);
184 return;
188 //Make sure all files were extracted
189 if(!mapChecksum.isEmpty())
191 qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
192 return;
195 qDebug("All extracted.\n");
197 //Clean-up
198 mapChecksum.clear();
199 mapVersion.clear();
200 mapCpuType.clear();
202 //Using any custom tools?
203 if(bCustom)
205 qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
208 //Check delay
209 double delayExtract = static_cast<double>(timer.elapsed()) / 1000.0;
210 if(delayExtract > 8.0)
212 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract);
213 qWarning("Please report performance problems to your anti-virus developer !!!\n");
216 //Register all translations
217 initTranslations();
219 //Look for Nero encoder
220 initNeroAac();
222 //Look for WMA File decoder
223 initWmaDec();
225 delay();
226 m_bSuccess = true;
229 ////////////////////////////////////////////////////////////
230 // PUBLIC FUNCTIONS
231 ////////////////////////////////////////////////////////////
233 void InitializationThread::delay(void)
235 const char *temp = "|/-\\";
236 printf("Thread is doing something important... ?\b", temp[4]);
238 for(int i = 0; i < 20; i++)
240 printf("%c\b", temp[i%4]);
241 msleep(25);
244 printf("Done\n\n");
247 void InitializationThread::initTranslations(void)
249 //Search for language files
250 QStringList qmFiles = QDir(":/localization").entryList(QStringList() << "LameXP_??.qm", QDir::Files, QDir::Name);
252 //Make sure we found at least one translation
253 if(qmFiles.count() < 1)
255 qFatal("Could not find any translation files!");
256 return;
259 //Add all available translations
260 while(!qmFiles.isEmpty())
262 QString langId, langName;
263 unsigned int systemId = 0;
264 QString qmFile = qmFiles.takeFirst();
266 QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
267 if(langIdExp.indexIn(qmFile) >= 0)
269 langId = langIdExp.cap(1).toLower();
272 QResource langRes = (QString(":/localization/%1.txt").arg(qmFile));
273 if(langRes.isValid() && langRes.size() > 0)
275 QStringList langInfo = QString::fromUtf8(reinterpret_cast<const char*>(langRes.data()), langRes.size()).simplified().split(",", QString::SkipEmptyParts);
276 if(langInfo.count() == 2)
278 systemId = langInfo.at(0).toUInt();
279 langName = langInfo.at(1);
283 if(lamexp_translation_register(langId, qmFile, langName, systemId))
285 qDebug64("Registering translation: %1 = %2 (%3)", qmFile, langName, QString::number(systemId));
287 else
289 qWarning("Failed to register: %s", qmFile.toLatin1().constData());
293 qDebug("All registered.\n");
296 void InitializationThread::initNeroAac(void)
298 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
300 QFileInfo neroFileInfo[3];
301 neroFileInfo[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(appPath));
302 neroFileInfo[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(appPath));
303 neroFileInfo[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(appPath));
305 bool neroFilesFound = true;
306 for(int i = 0; i < 3; i++) { if(!neroFileInfo[i].exists()) neroFilesFound = false; }
308 //Lock the Nero binaries
309 if(!neroFilesFound)
311 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
312 return;
315 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo[0].canonicalFilePath().toUtf8().constData());
317 LockedFile *neroBin[3];
318 for(int i = 0; i < 3; i++) neroBin[i] = NULL;
322 for(int i = 0; i < 3; i++)
324 neroBin[i] = new LockedFile(neroFileInfo[i].canonicalFilePath());
327 catch(...)
329 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
330 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
331 return;
334 QProcess process;
335 process.setProcessChannelMode(QProcess::MergedChannels);
336 process.setReadChannel(QProcess::StandardOutput);
337 process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help");
339 if(!process.waitForStarted())
341 qWarning("Nero process failed to create!");
342 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
343 process.kill();
344 process.waitForFinished(-1);
345 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
346 return;
349 unsigned int neroVersion = 0;
351 while(process.state() != QProcess::NotRunning)
353 if(!process.waitForReadyRead())
355 if(process.state() == QProcess::Running)
357 qWarning("Nero process time out -> killing!");
358 process.kill();
359 process.waitForFinished(-1);
360 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
361 return;
365 while(process.canReadLine())
367 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
368 QStringList tokens = line.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
369 int index1 = tokens.indexOf("Package");
370 int index2 = tokens.indexOf("version:");
371 if(index1 >= 0 && index2 >= 0 && index1 + 1 == index2 && index2 < tokens.count() - 1)
373 QStringList versionTokens = tokens.at(index2 + 1).split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);
374 if(versionTokens.count() == 4)
376 neroVersion = 0;
377 neroVersion += min(9, max(0, versionTokens.at(3).toInt()));
378 neroVersion += min(9, max(0, versionTokens.at(2).toInt())) * 10;
379 neroVersion += min(9, max(0, versionTokens.at(1).toInt())) * 100;
380 neroVersion += min(9, max(0, versionTokens.at(0).toInt())) * 1000;
386 if(!(neroVersion > 0))
388 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
389 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
390 return;
393 for(int i = 0; i < 3; i++)
395 lamexp_register_tool(neroFileInfo[i].fileName(), neroBin[i], neroVersion);
400 void InitializationThread::initWmaDec(void)
402 static const char* wmaDecoderComponentPath = "NCH Software/Components/wmawav/wmawav.exe";
404 LockedFile *wmaFileBin = NULL;
405 QFileInfo wmaFileInfo = QFileInfo(QString("%1/%2").arg(lamexp_known_folder(lamexp_folder_programfiles), wmaDecoderComponentPath));
407 if(!wmaFileInfo.exists())
409 qDebug("WMA File Decoder not found -> WMA decoding support will be disabled!\n");
410 return;
415 wmaFileBin = new LockedFile(wmaFileInfo.canonicalFilePath());
417 catch(...)
419 qWarning("Failed to get excluive lock to WMA File Decoder binary -> WMA decoding support will be disabled!");
420 return;
423 QProcess process;
424 process.setProcessChannelMode(QProcess::MergedChannels);
425 process.setReadChannel(QProcess::StandardOutput);
426 process.start(wmaFileInfo.canonicalFilePath(), QStringList());
428 if(!process.waitForStarted())
430 qWarning("WmaWav process failed to create!");
431 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
432 process.kill();
433 process.waitForFinished(-1);
434 return;
437 bool b_wmaWavFound = false;
439 while(process.state() != QProcess::NotRunning)
441 if(!process.waitForReadyRead())
443 if(process.state() == QProcess::Running)
445 qWarning("WmaWav process time out -> killing!");
446 process.kill();
447 process.waitForFinished(-1);
448 return;
451 while(process.canReadLine())
453 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
454 if(line.contains("Usage: wmatowav.exe WMAFileSpec WAVFileSpec", Qt::CaseInsensitive))
456 b_wmaWavFound = true;
461 if(!b_wmaWavFound)
463 qWarning("WmaWav could not be identified -> WMA decoding support will be disabled!\n");
464 LAMEXP_DELETE(wmaFileBin);
465 return;
468 qDebug("Found WMA File Decoder binary:\n%s\n", wmaFileInfo.canonicalFilePath().toUtf8().constData());
470 if(wmaFileBin)
472 lamexp_register_tool(wmaFileInfo.fileName(), wmaFileBin);
476 ////////////////////////////////////////////////////////////
477 // EVENTS
478 ////////////////////////////////////////////////////////////
480 /*NONE*/