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"
28 #include <QCoreApplication>
37 #define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X)
38 static const double g_allowedExtractDelay = 10.0;
40 ////////////////////////////////////////////////////////////
42 ////////////////////////////////////////////////////////////
44 InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
47 memset(&m_cpuFeatures, 0, sizeof(lamexp_cpu_t));
48 m_slowIndicator = false;
52 memcpy(&m_cpuFeatures, cpuFeatures, sizeof(lamexp_cpu_t));
56 ////////////////////////////////////////////////////////////
58 ////////////////////////////////////////////////////////////
60 void InitializationThread::run()
67 unsigned int cpuSupport = 0;
68 if(m_cpuFeatures.sse && m_cpuFeatures.sse2 && m_cpuFeatures.intel)
70 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_SSE : CPU_TYPE_X86_SSE;
74 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_GEN : CPU_TYPE_X86_GEN;
77 //Hack to disable x64 on the Windows 8 Developer Preview
78 if(cpuSupport & CPU_TYPE_X64_ALL)
80 DWORD osVerNo = lamexp_get_os_version();
81 if((HIWORD(osVerNo) == 6) && (LOWORD(osVerNo) == 2))
83 qWarning("Windows 8 (x64) developer preview detected. Going to disable all x64 support!\n");
84 cpuSupport = (cpuSupport == CPU_TYPE_X64_SSE) ? CPU_TYPE_X86_SSE : CPU_TYPE_X86_GEN;
88 //Print selected CPU type
91 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
92 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
93 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
94 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
95 default: throw "CPU support undefined!";
99 QMap<QString, QString> mapChecksum;
100 QMap<QString, unsigned int> mapVersion;
101 QMap<QString, unsigned int> mapCpuType;
104 for(int i = 0; i < INT_MAX; i++)
106 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
110 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
112 const QString currentTool = QString::fromLatin1(g_lamexp_tools[i].pcName);
113 mapChecksum.insert(currentTool, QString::fromLatin1(g_lamexp_tools[i].pcHash));
114 mapCpuType.insert(currentTool, g_lamexp_tools[i].uiCpuType);
115 mapVersion.insert(currentTool, g_lamexp_tools[i].uiVersion);
119 qFatal("Inconsistent checksum data detected. Take care!");
123 QDir toolsDir(":/tools/");
124 QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
125 QDir appDir = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
131 while(!toolsList.isEmpty())
135 QFileInfo currentTool = toolsList.takeFirst();
136 QString toolName = currentTool.fileName().toLower();
137 QString toolShortName = QString("%1.%2").arg(currentTool.baseName().toLower(), currentTool.suffix().toLower());
139 QByteArray toolHash = mapChecksum.take(toolName).toLatin1();
140 unsigned int toolCpuType = mapCpuType.take(toolName);
141 unsigned int toolVersion = mapVersion.take(toolName);
143 if(toolHash.size() != 72)
145 throw "The required checksum is missing, take care!";
148 if(toolCpuType & cpuSupport)
150 QFileInfo customTool(QString("%1/tools/%2/%3").arg(appDir.canonicalPath(), QString::number(lamexp_version_build()), toolShortName));
151 if(customTool.exists() && customTool.isFile())
154 qDebug("Setting up file: %s <- %s", toolShortName.toLatin1().constData(), appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
155 LockedFile *lockedFile = new LockedFile(customTool.canonicalFilePath());
156 lamexp_register_tool(toolShortName, lockedFile, UINT_MAX);
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);
166 catch(char *errorMsg)
168 qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg);
173 //Make sure all files were extracted
174 if(!mapChecksum.isEmpty())
176 qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
180 qDebug("All extracted.\n");
187 //Using any custom tools?
190 qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
194 double delayExtract = static_cast<double>(timer.elapsed()) / 1000.0;
195 if(delayExtract > g_allowedExtractDelay)
197 m_slowIndicator = true;
198 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract);
199 qWarning("Please report performance problems to your anti-virus developer !!!\n");
202 //Register all translations
205 //Look for AAC encoders
214 ////////////////////////////////////////////////////////////
216 ////////////////////////////////////////////////////////////
218 void InitializationThread::delay(void)
220 const char *temp = "|/-\\";
221 printf("Thread is doing something important... ?\b", temp[4]);
223 for(int i = 0; i < 20; i++)
225 printf("%c\b", temp[i%4]);
232 void InitializationThread::initTranslations(void)
234 //Search for language files
235 QStringList qmFiles = QDir(":/localization").entryList(QStringList() << "LameXP_??.qm", QDir::Files, QDir::Name);
237 //Make sure we found at least one translation
238 if(qmFiles.count() < 1)
240 qFatal("Could not find any translation files!");
244 //Add all available translations
245 while(!qmFiles.isEmpty())
247 QString langId, langName;
248 unsigned int systemId = 0;
249 QString qmFile = qmFiles.takeFirst();
251 QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
252 if(langIdExp.indexIn(qmFile) >= 0)
254 langId = langIdExp.cap(1).toLower();
257 QResource langRes = (QString(":/localization/%1.txt").arg(qmFile));
258 if(langRes.isValid() && langRes.size() > 0)
260 QStringList langInfo = QString::fromUtf8(reinterpret_cast<const char*>(langRes.data()), langRes.size()).simplified().split(",", QString::SkipEmptyParts);
261 if(langInfo.count() == 2)
263 systemId = langInfo.at(0).toUInt();
264 langName = langInfo.at(1);
268 if(lamexp_translation_register(langId, qmFile, langName, systemId))
270 qDebug("Registering translation: %s = %s (%u)", qmFile.toUtf8().constData(), langName.toUtf8().constData(), systemId);
274 qWarning("Failed to register: %s", qmFile.toLatin1().constData());
278 qDebug("All registered.\n");
281 void InitializationThread::initNeroAac(void)
283 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
285 QFileInfo neroFileInfo[3];
286 neroFileInfo[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(appPath));
287 neroFileInfo[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(appPath));
288 neroFileInfo[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(appPath));
290 bool neroFilesFound = true;
291 for(int i = 0; i < 3; i++) { if(!neroFileInfo[i].exists()) neroFilesFound = false; }
293 //Lock the Nero binaries
296 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
300 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo[0].canonicalFilePath().toUtf8().constData());
302 LockedFile *neroBin[3];
303 for(int i = 0; i < 3; i++) neroBin[i] = NULL;
307 for(int i = 0; i < 3; i++)
309 neroBin[i] = new LockedFile(neroFileInfo[i].canonicalFilePath());
314 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
315 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
320 process.setProcessChannelMode(QProcess::MergedChannels);
321 process.setReadChannel(QProcess::StandardOutput);
322 process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help");
324 if(!process.waitForStarted())
326 qWarning("Nero process failed to create!");
327 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
329 process.waitForFinished(-1);
330 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
334 unsigned int neroVersion = 0;
336 while(process.state() != QProcess::NotRunning)
338 if(!process.waitForReadyRead())
340 if(process.state() == QProcess::Running)
342 qWarning("Nero process time out -> killing!");
344 process.waitForFinished(-1);
345 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
350 while(process.canReadLine())
352 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
353 QStringList tokens = line.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
354 int index1 = tokens.indexOf("Package");
355 int index2 = tokens.indexOf("version:");
356 if(index1 >= 0 && index2 >= 0 && index1 + 1 == index2 && index2 < tokens.count() - 1)
358 QStringList versionTokens = tokens.at(index2 + 1).split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);
359 if(versionTokens.count() == 4)
362 neroVersion += qMin(9, qMax(0, versionTokens.at(3).toInt()));
363 neroVersion += qMin(9, qMax(0, versionTokens.at(2).toInt())) * 10;
364 neroVersion += qMin(9, qMax(0, versionTokens.at(1).toInt())) * 100;
365 neroVersion += qMin(9, qMax(0, versionTokens.at(0).toInt())) * 1000;
371 if(!(neroVersion > 0))
373 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
374 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
378 for(int i = 0; i < 3; i++)
380 lamexp_register_tool(neroFileInfo[i].fileName(), neroBin[i], neroVersion);
384 void InitializationThread::initFhgAac(void)
386 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
388 QFileInfo fhgFileInfo[4];
389 fhgFileInfo[0] = QFileInfo(QString("%1/fhgaacenc.exe").arg(appPath));
390 fhgFileInfo[1] = QFileInfo(QString("%1/enc_fhgaac.dll").arg(appPath));
391 fhgFileInfo[2] = QFileInfo(QString("%1/nsutil.dll").arg(appPath));
392 fhgFileInfo[3] = QFileInfo(QString("%1/libmp4v2.dll").arg(appPath));
394 bool fhgFilesFound = true;
395 for(int i = 0; i < 4; i++) { if(!fhgFileInfo[i].exists()) fhgFilesFound = false; }
397 //Lock the FhgAacEnc binaries
400 qDebug("FhgAacEnc binaries not found -> FhgAacEnc support will be disabled!\n");
404 qDebug("Found FhgAacEnc cli_exe:\n%s\n", fhgFileInfo[0].canonicalFilePath().toUtf8().constData());
405 qDebug("Found FhgAacEnc enc_dll:\n%s\n", fhgFileInfo[1].canonicalFilePath().toUtf8().constData());
407 LockedFile *fhgBin[4];
408 for(int i = 0; i < 4; i++) fhgBin[i] = NULL;
412 for(int i = 0; i < 4; i++)
414 fhgBin[i] = new LockedFile(fhgFileInfo[i].canonicalFilePath());
419 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
420 qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
425 process.setProcessChannelMode(QProcess::MergedChannels);
426 process.setReadChannel(QProcess::StandardOutput);
427 process.start(fhgFileInfo[0].canonicalFilePath(), QStringList() << "--version");
429 if(!process.waitForStarted())
431 qWarning("FhgAacEnc process failed to create!");
432 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
434 process.waitForFinished(-1);
435 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
439 QRegExp fhgAacEncSig("fhgaacenc version (\\d+) by tmkk", Qt::CaseInsensitive);
440 unsigned int fhgVersion = 0;
442 while(process.state() != QProcess::NotRunning)
444 process.waitForReadyRead();
445 if(!process.bytesAvailable() && process.state() == QProcess::Running)
447 qWarning("FhgAacEnc process time out -> killing!");
449 process.waitForFinished(-1);
450 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
453 while(process.bytesAvailable() > 0)
455 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
456 if(fhgAacEncSig.lastIndexIn(line) >= 0)
459 unsigned int temp = fhgAacEncSig.cap(1).toUInt(&ok);
460 if(ok) fhgVersion = temp;
465 if(!(fhgVersion > 0))
467 qWarning("FhgAacEnc version couldn't be determined -> FhgAacEnc support will be disabled!");
468 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
471 else if(fhgVersion < lamexp_toolver_fhgaacenc())
473 qWarning("FhgAacEnc version is too much outdated -> FhgAacEnc support will be disabled!");
474 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
478 for(int i = 0; i < 4; i++)
480 lamexp_register_tool(fhgFileInfo[i].fileName(), fhgBin[i], fhgVersion);
484 void InitializationThread::initQAac(void)
486 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
488 QFileInfo qaacFileInfo[2];
489 qaacFileInfo[0] = QFileInfo(QString("%1/qaac.exe").arg(appPath));
490 qaacFileInfo[1] = QFileInfo(QString("%1/libsoxrate.dll").arg(appPath));
492 bool qaacFilesFound = true;
493 for(int i = 0; i < 2; i++) { if(!qaacFileInfo[i].exists()) qaacFilesFound = false; }
495 //Lock the QAAC binaries
498 qDebug("QAAC binaries not found -> QAAC support will be disabled!\n");
502 qDebug("Found QAAC encoder:\n%s\n", qaacFileInfo[0].canonicalFilePath().toUtf8().constData());
504 LockedFile *qaacBin[2];
505 for(int i = 0; i < 2; i++) qaacBin[i] = NULL;
509 for(int i = 0; i < 2; i++)
511 qaacBin[i] = new LockedFile(qaacFileInfo[i].canonicalFilePath());
516 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
517 qWarning("Failed to get excluive lock to QAAC binary -> QAAC support will be disabled!");
522 process.setProcessChannelMode(QProcess::MergedChannels);
523 process.setReadChannel(QProcess::StandardOutput);
524 process.start(qaacFileInfo[0].canonicalFilePath(), QStringList() << "--check");
526 if(!process.waitForStarted())
528 qWarning("QAAC process failed to create!");
529 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
531 process.waitForFinished(-1);
532 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
536 QRegExp qaacEncSig("qaac (\\d)\\.(\\d)(\\d)", Qt::CaseInsensitive);
537 QRegExp coreEncSig("CoreAudioToolbox (\\d)\\.(\\d)\\.(\\d)\\.(\\d)", Qt::CaseInsensitive);
538 unsigned int qaacVersion = 0;
539 unsigned int coreVersion = 0;
541 while(process.state() != QProcess::NotRunning)
543 process.waitForReadyRead();
544 if(!process.bytesAvailable() && process.state() == QProcess::Running)
546 qWarning("QAAC process time out -> killing!");
548 process.waitForFinished(-1);
549 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
552 while(process.bytesAvailable() > 0)
554 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
555 if(qaacEncSig.lastIndexIn(line) >= 0)
557 unsigned int tmp[3] = {0, 0, 0};
558 bool ok[3] = {false, false, false};
559 tmp[0] = qaacEncSig.cap(1).toUInt(&ok[0]);
560 tmp[1] = qaacEncSig.cap(2).toUInt(&ok[1]);
561 tmp[2] = qaacEncSig.cap(3).toUInt(&ok[2]);
562 if(ok[0] && ok[1] && ok[2])
564 qaacVersion = (qBound(0U, tmp[0], 9U) * 100) + (qBound(0U, tmp[1], 9U) * 10) + qBound(0U, tmp[2], 9U);
567 if(coreEncSig.lastIndexIn(line) >= 0)
569 unsigned int tmp[4] = {0, 0, 0, 0};
570 bool ok[4] = {false, false, false, false};
571 tmp[0] = coreEncSig.cap(1).toUInt(&ok[0]);
572 tmp[1] = coreEncSig.cap(2).toUInt(&ok[1]);
573 tmp[2] = coreEncSig.cap(3).toUInt(&ok[2]);
574 tmp[3] = coreEncSig.cap(4).toUInt(&ok[3]);
575 if(ok[0] && ok[1] && ok[2] && ok[3])
577 coreVersion = (qBound(0U, tmp[0], 9U) * 1000) + (qBound(0U, tmp[1], 9U) * 100) + (qBound(0U, tmp[2], 9U) * 10) + qBound(0U, tmp[3], 9U);
583 //qDebug("qaac %d, CoreAudioToolbox %d", qaacVersion, coreVersion);
585 if(!(qaacVersion > 0))
587 qWarning("QAAC version couldn't be determined -> QAAC support will be disabled!");
588 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
591 else if(qaacVersion < lamexp_toolver_qaacenc())
593 qWarning("QAAC version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.??", qaacVersion, "N/A").toLatin1().constData());
594 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
598 if(!(coreVersion > 0))
600 qWarning("CoreAudioToolbox version couldn't be determined -> QAAC support will be disabled!");
601 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
604 else if(coreVersion < lamexp_toolver_coreaudio())
606 qWarning("CoreAudioToolbox version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.?.?.?", coreVersion, "N/A").toLatin1().constData());
607 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
611 lamexp_register_tool(qaacFileInfo[0].fileName(), qaacBin[0], qaacVersion);
612 lamexp_register_tool(qaacFileInfo[1].fileName(), qaacBin[1], qaacVersion);
615 void InitializationThread::selfTest(void)
617 const unsigned int cpu[4] = {CPU_TYPE_X86_GEN, CPU_TYPE_X86_SSE, CPU_TYPE_X64_GEN, CPU_TYPE_X64_SSE};
619 for(size_t k = 0; k < 4; k++)
624 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
625 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
626 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
627 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
628 default: throw "CPU support undefined!";
631 for(int i = 0; i < INT_MAX; i++)
633 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
637 if(g_lamexp_tools[i].uiCpuType & cpu[k])
639 qDebug("%02i -> %s", ++n, g_lamexp_tools[i].pcName);
644 qFatal("Tool count mismatch !!!");
650 ////////////////////////////////////////////////////////////
652 ////////////////////////////////////////////////////////////