1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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"
27 #include <QCoreApplication>
35 ////////////////////////////////////////////////////////////
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
49 unsigned int uiCpuType;
51 unsigned int uiVersion;
55 {"3b41f85dde8d4a5a0f4cd5f461099d0db24610ba", CPU_TYPE_ALL, "alac.exe", UINT_MAX},
56 {"fb74ac8b73ad8cba2c3b4e6e61f23401d630dc22", CPU_TYPE_ALL, "elevator.exe", UINT_MAX},
57 {"3c647950bccfcc75d0746c0772e7115684be4dc5", CPU_TYPE_ALL, "faad.exe", 27},
58 {"d33cd86f04bd4067e244d2804466583c7b90a4e2", CPU_TYPE_ALL, "flac.exe", 121},
59 {"9328a50e89b54ec065637496d9681a7e3eebf915", CPU_TYPE_ALL, "gpgv.exe", 1411},
60 {"d837bf6ee4dab557d8b02d46c75a24e58980fffa", CPU_TYPE_ALL, "gpgv.gpg", UINT_MAX},
61 {"b8800842893e2900eea7cda5c2556661333bc5db", CPU_TYPE_ALL, "lame.exe", 39916},
62 {"a4e929cfaa42fa2e61a3d0c6434c77a06d45aef3", CPU_TYPE_ALL, "mac.exe", 406},
63 {"9ee3074dc3232c61c0ffd6ae6755f65974695c51", CPU_TYPE_GEN, "mediainfo.i386.exe", 743},
64 {"3691da33a4efd7625d1bd01cbf5ce6ffd8a12d19", CPU_TYPE_X64, "mediainfo.x64.exe", 743},
65 {"aa89763a5ba4d1a5986549b9ee53e005c51940c1", CPU_TYPE_ALL, "mpcdec.exe", 435},
66 {"38f81efca6c1eeab0b9dc39d06c2ac750267217f", CPU_TYPE_ALL, "mpg123.exe", 1132},
67 {"8dd7138714c3bcb39f5a3213413addba13d06f1e", CPU_TYPE_ALL, "oggdec.exe", UINT_MAX},
68 {"14a99d3b1f0b166dbd68db45196da871e58e14ec", CPU_TYPE_X86, "oggenc2.i386.exe", 287602},
69 {"36f8d93ef3df6a420a73a9b5cf02dafdaf4321f0", CPU_TYPE_SSE, "oggenc2.sse2.exe", 287602},
70 {"87ad1af73e9b9db3da3db645e5c2253cb0c2a2ea", CPU_TYPE_X64, "oggenc2.x64.exe", 287602},
71 {"0d9035bb62bdf46a2785261f8be5a4a0972abd15", CPU_TYPE_ALL, "shorten.exe", 361},
72 {"50ead3b852cbfc067a402e6c2d0d0d8879663dec", CPU_TYPE_ALL, "sox.exe", 1432},
73 {"8671e16497a2d217d3707d4aa418678d02b16bcc", CPU_TYPE_ALL, "speexdec.exe", 12},
74 {"093bfdec22872ca99e40183937c88785468be989", CPU_TYPE_ALL, "tta.exe", 21},
75 {"8c842eef65248b46fa6cb9a9e5714f575672d999", CPU_TYPE_ALL, "valdec.exe", 31},
76 {"62e2805d1b2eb2a4d86a5ca6e6ea58010d05d2a7", CPU_TYPE_ALL, "wget.exe", 1114},
77 {"a7e8aad52213e339ad985829722f35eab62be182", CPU_TYPE_ALL, "wupdate.exe", UINT_MAX},
78 {"b7d14b3540d24df13119a55d97623a61412de6e3", CPU_TYPE_ALL, "wvunpack.exe", 4601},
79 {NULL, NULL, NULL, NULL}
82 ////////////////////////////////////////////////////////////
84 ////////////////////////////////////////////////////////////
86 InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
89 memset(&m_cpuFeatures, 0, sizeof(lamexp_cpu_t));
93 memcpy(&m_cpuFeatures, cpuFeatures, sizeof(lamexp_cpu_t));
97 ////////////////////////////////////////////////////////////
99 ////////////////////////////////////////////////////////////
101 void InitializationThread::run()
107 unsigned int cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64 : ((m_cpuFeatures.intel && m_cpuFeatures.sse && m_cpuFeatures.sse2) ? CPU_TYPE_SSE : CPU_TYPE_X86);
110 QMap<QString, QString> mapChecksum;
111 QMap<QString, unsigned int> mapVersion;
112 QMap<QString, unsigned int> mapCpuType;
115 for(int i = 0; i < INT_MAX; i++)
117 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
121 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
123 const QString currentTool = QString::fromLatin1(g_lamexp_tools[i].pcName);
124 mapChecksum.insert(currentTool, QString::fromLatin1(g_lamexp_tools[i].pcHash));
125 mapCpuType.insert(currentTool, g_lamexp_tools[i].uiCpuType);
126 mapVersion.insert(currentTool, g_lamexp_tools[i].uiVersion);
130 qFatal("Inconsistent checksum data detected. Take care!");
134 QDir toolsDir(":/tools/");
135 QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
141 while(!toolsList.isEmpty())
145 QFileInfo currentTool = toolsList.takeFirst();
146 QString toolName = currentTool.fileName().toLower();
147 QString toolShortName = QString("%1.%2").arg(currentTool.baseName().toLower(), currentTool.suffix().toLower());
149 QByteArray toolHash = mapChecksum.take(toolName).toLatin1();
150 unsigned int toolCpuType = mapCpuType.take(toolName);
151 unsigned int toolVersion = mapVersion.take(toolName);
153 if(toolHash.size() != 40)
155 throw "The required checksum is missing, take care!";
158 if(toolCpuType & cpuSupport)
160 qDebug("Extracting file: %s -> %s", toolName.toLatin1().constData(), toolShortName.toLatin1().constData());
161 LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName), toolHash);
162 lamexp_register_tool(toolShortName, lockedFile, toolVersion);
165 catch(char *errorMsg)
167 qFatal("At least one of the required tools could not be extracted:\n%s", errorMsg);
172 //Make sure all files were extracted
173 if(!mapChecksum.isEmpty())
175 qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
184 qDebug("All extracted.\n");
187 double delayExtract = static_cast<double>(timer.elapsed()) / 1000.0;
188 if(delayExtract > 8.0)
190 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract);
191 qWarning("Please report performance problems to your anti-virus developer !!!\n");
194 //Register all translations
197 //Look for Nero encoder
200 //Look for WMA File decoder
207 ////////////////////////////////////////////////////////////
209 ////////////////////////////////////////////////////////////
211 void InitializationThread::delay(void)
213 const char *temp = "|/-\\";
214 printf("Thread is doing something important... ?\b", temp[4]);
216 for(int i = 0; i < 20; i++)
218 printf("%c\b", temp[i%4]);
225 void InitializationThread::initTranslations(void)
227 //Search for language files
228 QStringList qmFiles = QDir(":/localization").entryList(QStringList() << "LameXP_??.qm", QDir::Files, QDir::Name);
230 //Make sure we found at least one translation
231 if(qmFiles.count() < 1)
233 qFatal("Could not find any translation files!");
237 //Add all available translations
238 while(!qmFiles.isEmpty())
240 QString langId, langName;
241 unsigned int systemId = 0;
242 QString qmFile = qmFiles.takeFirst();
244 QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
245 if(langIdExp.indexIn(qmFile) >= 0)
247 langId = langIdExp.cap(1).toLower();
250 QResource langRes = (QString(":/localization/%1.txt").arg(qmFile));
251 if(langRes.isValid() && langRes.size() > 0)
253 QStringList langInfo = QString::fromUtf8(reinterpret_cast<const char*>(langRes.data()), langRes.size()).simplified().split(",", QString::SkipEmptyParts);
254 if(langInfo.count() == 2)
256 systemId = langInfo.at(0).toUInt();
257 langName = langInfo.at(1);
261 if(lamexp_translation_register(langId, qmFile, langName, systemId))
263 qDebug64("Registering translation: %1 = %2 (%3)", qmFile, langName, QString::number(systemId));
267 qWarning("Failed to register: %s", qmFile.toLatin1().constData());
271 qDebug("All registered.\n");
274 void InitializationThread::initNeroAac(void)
276 QFileInfo neroFileInfo[3];
277 neroFileInfo[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(QCoreApplication::applicationDirPath()));
278 neroFileInfo[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(QCoreApplication::applicationDirPath()));
279 neroFileInfo[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(QCoreApplication::applicationDirPath()));
281 bool neroFilesFound = true;
282 for(int i = 0; i < 3; i++) { if(!neroFileInfo[i].exists()) neroFilesFound = false; }
284 //Lock the Nero binaries
287 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
291 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo[0].canonicalFilePath().toUtf8().constData());
293 LockedFile *neroBin[3];
294 for(int i = 0; i < 3; i++) neroBin[i] = NULL;
298 for(int i = 0; i < 3; i++)
300 neroBin[i] = new LockedFile(neroFileInfo[i].canonicalFilePath());
305 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
306 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
311 process.setProcessChannelMode(QProcess::MergedChannels);
312 process.setReadChannel(QProcess::StandardOutput);
313 process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help");
315 if(!process.waitForStarted())
317 qWarning("Nero process failed to create!");
318 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
320 process.waitForFinished(-1);
321 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
325 unsigned int neroVersion = 0;
327 while(process.state() != QProcess::NotRunning)
329 if(!process.waitForReadyRead())
331 if(process.state() == QProcess::Running)
333 qWarning("Nero process time out -> killing!");
335 process.waitForFinished(-1);
336 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
341 while(process.canReadLine())
343 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
344 QStringList tokens = line.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
345 int index1 = tokens.indexOf("Package");
346 int index2 = tokens.indexOf("version:");
347 if(index1 >= 0 && index2 >= 0 && index1 + 1 == index2 && index2 < tokens.count() - 1)
349 QStringList versionTokens = tokens.at(index2 + 1).split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);
350 if(versionTokens.count() == 4)
353 neroVersion += min(9, max(0, versionTokens.at(3).toInt()));
354 neroVersion += min(9, max(0, versionTokens.at(2).toInt())) * 10;
355 neroVersion += min(9, max(0, versionTokens.at(1).toInt())) * 100;
356 neroVersion += min(9, max(0, versionTokens.at(0).toInt())) * 1000;
362 if(!(neroVersion > 0))
364 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
365 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
369 for(int i = 0; i < 3; i++)
371 lamexp_register_tool(neroFileInfo[i].fileName(), neroBin[i], neroVersion);
376 void InitializationThread::initWmaDec(void)
378 static const char* wmaDecoderComponentPath = "NCH Software/Components/wmawav/wmawav.exe";
380 LockedFile *wmaFileBin = NULL;
381 QFileInfo wmaFileInfo = QFileInfo(QString("%1/%2").arg(lamexp_known_folder(lamexp_folder_programfiles), wmaDecoderComponentPath));
383 if(!wmaFileInfo.exists())
385 qDebug("WMA File Decoder not found -> WMA decoding support will be disabled!\n");
391 wmaFileBin = new LockedFile(wmaFileInfo.canonicalFilePath());
395 qWarning("Failed to get excluive lock to WMA File Decoder binary -> WMA decoding support will be disabled!");
400 process.setProcessChannelMode(QProcess::MergedChannels);
401 process.setReadChannel(QProcess::StandardOutput);
402 process.start(wmaFileInfo.canonicalFilePath(), QStringList());
404 if(!process.waitForStarted())
406 qWarning("WmaWav process failed to create!");
407 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
409 process.waitForFinished(-1);
413 bool b_wmaWavFound = false;
415 while(process.state() != QProcess::NotRunning)
417 if(!process.waitForReadyRead())
419 if(process.state() == QProcess::Running)
421 qWarning("WmaWav process time out -> killing!");
423 process.waitForFinished(-1);
427 while(process.canReadLine())
429 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
430 if(line.contains("Usage: wmatowav.exe WMAFileSpec WAVFileSpec", Qt::CaseInsensitive))
432 b_wmaWavFound = true;
439 qWarning("WmaWav could not be identified -> WMA decoding support will be disabled!\n");
440 LAMEXP_DELETE(wmaFileBin);
444 qDebug("Found WMA File Decoder binary:\n%s\n", wmaFileInfo.canonicalFilePath().toUtf8().constData());
448 lamexp_register_tool(wmaFileInfo.fileName(), wmaFileBin);
452 ////////////////////////////////////////////////////////////
454 ////////////////////////////////////////////////////////////