1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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>
35 #include <QTextStream>
38 #define PRINT_CPU_TYPE(X) case X: qDebug("Selected CPU is: " #X)
39 static const double g_allowedExtractDelay = 10.0;
41 ////////////////////////////////////////////////////////////
43 ////////////////////////////////////////////////////////////
45 InitializationThread::InitializationThread(const lamexp_cpu_t *cpuFeatures)
48 memset(&m_cpuFeatures, 0, sizeof(lamexp_cpu_t));
49 m_slowIndicator = false;
53 memcpy(&m_cpuFeatures, cpuFeatures, sizeof(lamexp_cpu_t));
57 ////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////
61 void InitializationThread::run()
68 unsigned int cpuSupport = 0;
69 if(m_cpuFeatures.sse && m_cpuFeatures.sse2 && m_cpuFeatures.intel)
71 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_SSE : CPU_TYPE_X86_SSE;
75 cpuSupport = m_cpuFeatures.x64 ? CPU_TYPE_X64_GEN : CPU_TYPE_X86_GEN;
78 //Hack to disable x64 on the Windows 8 Developer Preview (not required anymore)
79 //if(cpuSupport & CPU_TYPE_X64_ALL)
81 // DWORD osVerNo = lamexp_get_os_version();
82 // if((HIWORD(osVerNo) == 6) && (LOWORD(osVerNo) == 2))
84 // qWarning("Windows 8 (x64) developer preview detected. Going to disable all x64 support!\n");
85 // cpuSupport = (cpuSupport == CPU_TYPE_X64_SSE) ? CPU_TYPE_X86_SSE : CPU_TYPE_X86_GEN;
89 //Print selected CPU type
92 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
93 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
94 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
95 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
96 default: throw "CPU support undefined!";
100 QMap<QString, QString> mapChecksum;
101 QMap<QString, unsigned int> mapVersion;
102 QMap<QString, unsigned int> mapCpuType;
105 for(int i = 0; i < INT_MAX; i++)
107 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
111 else if(g_lamexp_tools[i].pcName && g_lamexp_tools[i].pcHash && g_lamexp_tools[i].uiVersion)
113 const QString currentTool = QString::fromLatin1(g_lamexp_tools[i].pcName);
114 mapChecksum.insert(currentTool, QString::fromLatin1(g_lamexp_tools[i].pcHash));
115 mapCpuType.insert(currentTool, g_lamexp_tools[i].uiCpuType);
116 mapVersion.insert(currentTool, g_lamexp_tools[i].uiVersion);
120 qFatal("Inconsistent checksum data detected. Take care!");
124 QDir toolsDir(":/tools/");
125 QList<QFileInfo> toolsList = toolsDir.entryInfoList(QStringList("*.*"), QDir::Files, QDir::Name);
126 QDir appDir = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
132 while(!toolsList.isEmpty())
136 QFileInfo currentTool = toolsList.takeFirst();
137 QString toolName = currentTool.fileName().toLower();
138 QString toolShortName = QString("%1.%2").arg(currentTool.baseName().toLower(), currentTool.suffix().toLower());
140 QByteArray toolHash = mapChecksum.take(toolName).toLatin1();
141 unsigned int toolCpuType = mapCpuType.take(toolName);
142 unsigned int toolVersion = mapVersion.take(toolName);
144 if(toolHash.size() != 72)
146 throw "The required checksum is missing, take care!";
149 if(toolCpuType & cpuSupport)
151 QFileInfo customTool(QString("%1/tools/%2/%3").arg(appDir.canonicalPath(), QString::number(lamexp_version_build()), toolShortName));
152 if(customTool.exists() && customTool.isFile())
155 qDebug("Setting up file: %s <- %s", toolShortName.toLatin1().constData(), appDir.relativeFilePath(customTool.canonicalFilePath()).toLatin1().constData());
156 LockedFile *lockedFile = new LockedFile(customTool.canonicalFilePath());
157 lamexp_register_tool(toolShortName, lockedFile, UINT_MAX);
161 qDebug("Extracting file: %s -> %s", toolName.toLatin1().constData(), toolShortName.toLatin1().constData());
162 LockedFile *lockedFile = new LockedFile(QString(":/tools/%1").arg(toolName), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName), toolHash);
163 lamexp_register_tool(toolShortName, lockedFile, toolVersion);
167 catch(char *errorMsg)
169 qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg);
174 //Make sure all files were extracted
175 if(!mapChecksum.isEmpty())
177 qFatal("At least one required tool could not be found:\n%s", toolsDir.filePath(mapChecksum.keys().first()).toLatin1().constData());
181 qDebug("All extracted.\n");
188 //Using any custom tools?
191 qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
195 double delayExtract = static_cast<double>(timer.elapsed()) / 1000.0;
196 if(delayExtract > g_allowedExtractDelay)
198 m_slowIndicator = true;
199 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract);
200 qWarning("Please report performance problems to your anti-virus developer !!!\n");
203 //Register all translations
206 //Look for AAC encoders
215 ////////////////////////////////////////////////////////////
217 ////////////////////////////////////////////////////////////
219 void InitializationThread::delay(void)
221 const char *temp = "|/-\\";
222 printf("Thread is doing something important... ?\b", temp[4]);
224 for(int i = 0; i < 20; i++)
226 printf("%c\b", temp[i%4]);
233 void InitializationThread::initTranslations(void)
235 //Search for language files
236 QStringList qmFiles = QDir(":/localization").entryList(QStringList() << "LameXP_??.qm", QDir::Files, QDir::Name);
238 //Make sure we found at least one translation
239 if(qmFiles.count() < 1)
241 qFatal("Could not find any translation files!");
245 //Add all available translations
246 while(!qmFiles.isEmpty())
248 QString langId, langName;
249 unsigned int systemId = 0, country = 0;
250 QString qmFile = qmFiles.takeFirst();
252 QRegExp langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive);
253 if(langIdExp.indexIn(qmFile) >= 0)
255 langId = langIdExp.cap(1).toLower();
256 QResource langRes = QResource(QString(":/localization/%1.txt").arg(qmFile));
257 if(langRes.isValid() && langRes.size() > 0)
259 QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(langRes.data()), langRes.size());
260 QTextStream stream(&data, QIODevice::ReadOnly);
261 stream.setAutoDetectUnicode(false); stream.setCodec("UTF-8");
262 while(!stream.atEnd())
264 QStringList langInfo = stream.readLine().simplified().split(",", QString::SkipEmptyParts);
265 if(langInfo.count() == 3)
267 systemId = langInfo.at(0).trimmed().toUInt();
268 country = langInfo.at(1).trimmed().toUInt();
269 langName = langInfo.at(2).trimmed();
276 if(!(langId.isEmpty() || langName.isEmpty() || systemId == 0))
278 if(lamexp_translation_register(langId, qmFile, langName, systemId, country))
280 qDebug("Registering translation: %s = %s (%u) [%u]", qmFile.toUtf8().constData(), langName.toUtf8().constData(), systemId, country);
284 qWarning("Failed to register: %s", qmFile.toLatin1().constData());
289 qDebug("All registered.\n");
292 void InitializationThread::initNeroAac(void)
294 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
296 QFileInfo neroFileInfo[3];
297 neroFileInfo[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(appPath));
298 neroFileInfo[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(appPath));
299 neroFileInfo[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(appPath));
301 bool neroFilesFound = true;
302 for(int i = 0; i < 3; i++) { if(!neroFileInfo[i].exists()) neroFilesFound = false; }
304 //Lock the Nero binaries
307 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
311 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo[0].canonicalFilePath().toUtf8().constData());
313 LockedFile *neroBin[3];
314 for(int i = 0; i < 3; i++) neroBin[i] = NULL;
318 for(int i = 0; i < 3; i++)
320 neroBin[i] = new LockedFile(neroFileInfo[i].canonicalFilePath());
325 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
326 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
331 process.setProcessChannelMode(QProcess::MergedChannels);
332 process.setReadChannel(QProcess::StandardOutput);
333 process.start(neroFileInfo[0].canonicalFilePath(), QStringList() << "-help");
335 if(!process.waitForStarted())
337 qWarning("Nero process failed to create!");
338 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
340 process.waitForFinished(-1);
341 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
345 unsigned int neroVersion = 0;
347 while(process.state() != QProcess::NotRunning)
349 if(!process.waitForReadyRead())
351 if(process.state() == QProcess::Running)
353 qWarning("Nero process time out -> killing!");
355 process.waitForFinished(-1);
356 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
361 while(process.canReadLine())
363 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
364 QStringList tokens = line.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive);
365 int index1 = tokens.indexOf("Package");
366 int index2 = tokens.indexOf("version:");
367 if(index1 >= 0 && index2 >= 0 && index1 + 1 == index2 && index2 < tokens.count() - 1)
369 QStringList versionTokens = tokens.at(index2 + 1).split(".", QString::SkipEmptyParts, Qt::CaseInsensitive);
370 if(versionTokens.count() == 4)
373 neroVersion += qMin(9, qMax(0, versionTokens.at(3).toInt()));
374 neroVersion += qMin(9, qMax(0, versionTokens.at(2).toInt())) * 10;
375 neroVersion += qMin(9, qMax(0, versionTokens.at(1).toInt())) * 100;
376 neroVersion += qMin(9, qMax(0, versionTokens.at(0).toInt())) * 1000;
382 if(!(neroVersion > 0))
384 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
385 for(int i = 0; i < 3; i++) LAMEXP_DELETE(neroBin[i]);
389 for(int i = 0; i < 3; i++)
391 lamexp_register_tool(neroFileInfo[i].fileName(), neroBin[i], neroVersion);
395 void InitializationThread::initFhgAac(void)
397 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
399 QFileInfo fhgFileInfo[4];
400 fhgFileInfo[0] = QFileInfo(QString("%1/fhgaacenc.exe").arg(appPath));
401 fhgFileInfo[1] = QFileInfo(QString("%1/enc_fhgaac.dll").arg(appPath));
402 fhgFileInfo[2] = QFileInfo(QString("%1/nsutil.dll").arg(appPath));
403 fhgFileInfo[3] = QFileInfo(QString("%1/libmp4v2.dll").arg(appPath));
405 bool fhgFilesFound = true;
406 for(int i = 0; i < 4; i++) { if(!fhgFileInfo[i].exists()) fhgFilesFound = false; }
408 //Lock the FhgAacEnc binaries
411 qDebug("FhgAacEnc binaries not found -> FhgAacEnc support will be disabled!\n");
415 qDebug("Found FhgAacEnc cli_exe:\n%s\n", fhgFileInfo[0].canonicalFilePath().toUtf8().constData());
416 qDebug("Found FhgAacEnc enc_dll:\n%s\n", fhgFileInfo[1].canonicalFilePath().toUtf8().constData());
418 LockedFile *fhgBin[4];
419 for(int i = 0; i < 4; i++) fhgBin[i] = NULL;
423 for(int i = 0; i < 4; i++)
425 fhgBin[i] = new LockedFile(fhgFileInfo[i].canonicalFilePath());
430 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
431 qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
436 process.setProcessChannelMode(QProcess::MergedChannels);
437 process.setReadChannel(QProcess::StandardOutput);
438 process.start(fhgFileInfo[0].canonicalFilePath(), QStringList() << "--version");
440 if(!process.waitForStarted())
442 qWarning("FhgAacEnc process failed to create!");
443 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
445 process.waitForFinished(-1);
446 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
450 QRegExp fhgAacEncSig("fhgaacenc version (\\d+) by tmkk", Qt::CaseInsensitive);
451 unsigned int fhgVersion = 0;
453 while(process.state() != QProcess::NotRunning)
455 process.waitForReadyRead();
456 if(!process.bytesAvailable() && process.state() == QProcess::Running)
458 qWarning("FhgAacEnc process time out -> killing!");
460 process.waitForFinished(-1);
461 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
464 while(process.bytesAvailable() > 0)
466 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
467 if(fhgAacEncSig.lastIndexIn(line) >= 0)
470 unsigned int temp = fhgAacEncSig.cap(1).toUInt(&ok);
471 if(ok) fhgVersion = temp;
476 if(!(fhgVersion > 0))
478 qWarning("FhgAacEnc version couldn't be determined -> FhgAacEnc support will be disabled!");
479 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
482 else if(fhgVersion < lamexp_toolver_fhgaacenc())
484 qWarning("FhgAacEnc version is too much outdated -> FhgAacEnc support will be disabled!");
485 for(int i = 0; i < 4; i++) LAMEXP_DELETE(fhgBin[i]);
489 for(int i = 0; i < 4; i++)
491 lamexp_register_tool(fhgFileInfo[i].fileName(), fhgBin[i], fhgVersion);
495 void InitializationThread::initQAac(void)
497 const QString appPath = QDir(QCoreApplication::applicationDirPath()).canonicalPath();
499 QFileInfo qaacFileInfo[2];
500 qaacFileInfo[0] = QFileInfo(QString("%1/qaac.exe").arg(appPath));
501 qaacFileInfo[1] = QFileInfo(QString("%1/libsoxrate.dll").arg(appPath));
503 bool qaacFilesFound = true;
504 for(int i = 0; i < 2; i++) { if(!qaacFileInfo[i].exists()) qaacFilesFound = false; }
506 //Lock the QAAC binaries
509 qDebug("QAAC binaries not found -> QAAC support will be disabled!\n");
513 qDebug("Found QAAC encoder:\n%s\n", qaacFileInfo[0].canonicalFilePath().toUtf8().constData());
515 LockedFile *qaacBin[2];
516 for(int i = 0; i < 2; i++) qaacBin[i] = NULL;
520 for(int i = 0; i < 2; i++)
522 qaacBin[i] = new LockedFile(qaacFileInfo[i].canonicalFilePath());
527 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
528 qWarning("Failed to get excluive lock to QAAC binary -> QAAC support will be disabled!");
533 process.setProcessChannelMode(QProcess::MergedChannels);
534 process.setReadChannel(QProcess::StandardOutput);
535 process.start(qaacFileInfo[0].canonicalFilePath(), QStringList() << "--check");
537 if(!process.waitForStarted())
539 qWarning("QAAC process failed to create!");
540 qWarning("Error message: \"%s\"\n", process.errorString().toLatin1().constData());
542 process.waitForFinished(-1);
543 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
547 QRegExp qaacEncSig("qaac (\\d)\\.(\\d)(\\d)", Qt::CaseInsensitive);
548 QRegExp coreEncSig("CoreAudioToolbox (\\d)\\.(\\d)\\.(\\d)\\.(\\d)", Qt::CaseInsensitive);
549 unsigned int qaacVersion = 0;
550 unsigned int coreVersion = 0;
552 while(process.state() != QProcess::NotRunning)
554 process.waitForReadyRead();
555 if(!process.bytesAvailable() && process.state() == QProcess::Running)
557 qWarning("QAAC process time out -> killing!");
559 process.waitForFinished(-1);
560 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
563 while(process.bytesAvailable() > 0)
565 QString line = QString::fromUtf8(process.readLine().constData()).simplified();
566 if(qaacEncSig.lastIndexIn(line) >= 0)
568 unsigned int tmp[3] = {0, 0, 0};
569 bool ok[3] = {false, false, false};
570 tmp[0] = qaacEncSig.cap(1).toUInt(&ok[0]);
571 tmp[1] = qaacEncSig.cap(2).toUInt(&ok[1]);
572 tmp[2] = qaacEncSig.cap(3).toUInt(&ok[2]);
573 if(ok[0] && ok[1] && ok[2])
575 qaacVersion = (qBound(0U, tmp[0], 9U) * 100) + (qBound(0U, tmp[1], 9U) * 10) + qBound(0U, tmp[2], 9U);
578 if(coreEncSig.lastIndexIn(line) >= 0)
580 unsigned int tmp[4] = {0, 0, 0, 0};
581 bool ok[4] = {false, false, false, false};
582 tmp[0] = coreEncSig.cap(1).toUInt(&ok[0]);
583 tmp[1] = coreEncSig.cap(2).toUInt(&ok[1]);
584 tmp[2] = coreEncSig.cap(3).toUInt(&ok[2]);
585 tmp[3] = coreEncSig.cap(4).toUInt(&ok[3]);
586 if(ok[0] && ok[1] && ok[2] && ok[3])
588 coreVersion = (qBound(0U, tmp[0], 9U) * 1000) + (qBound(0U, tmp[1], 9U) * 100) + (qBound(0U, tmp[2], 9U) * 10) + qBound(0U, tmp[3], 9U);
594 //qDebug("qaac %d, CoreAudioToolbox %d", qaacVersion, coreVersion);
596 if(!(qaacVersion > 0))
598 qWarning("QAAC version couldn't be determined -> QAAC support will be disabled!");
599 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
602 else if(qaacVersion < lamexp_toolver_qaacenc())
604 qWarning("QAAC version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.??", qaacVersion, "N/A").toLatin1().constData());
605 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
609 if(!(coreVersion > 0))
611 qWarning("CoreAudioToolbox version couldn't be determined -> QAAC support will be disabled!");
612 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
615 else if(coreVersion < lamexp_toolver_coreaudio())
617 qWarning("CoreAudioToolbox version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.?.?.?", coreVersion, "N/A").toLatin1().constData());
618 for(int i = 0; i < 2; i++) LAMEXP_DELETE(qaacBin[i]);
622 lamexp_register_tool(qaacFileInfo[0].fileName(), qaacBin[0], qaacVersion);
623 lamexp_register_tool(qaacFileInfo[1].fileName(), qaacBin[1], qaacVersion);
626 void InitializationThread::selfTest(void)
628 const unsigned int cpu[4] = {CPU_TYPE_X86_GEN, CPU_TYPE_X86_SSE, CPU_TYPE_X64_GEN, CPU_TYPE_X64_SSE};
630 for(size_t k = 0; k < 4; k++)
635 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN); break;
636 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE); break;
637 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN); break;
638 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE); break;
639 default: throw "CPU support undefined!";
642 for(int i = 0; i < INT_MAX; i++)
644 if(!g_lamexp_tools[i].pcName && !g_lamexp_tools[i].pcHash && !g_lamexp_tools[i].uiVersion)
648 if(g_lamexp_tools[i].uiCpuType & cpu[k])
650 qDebug("%02i -> %s", ++n, g_lamexp_tools[i].pcName);
655 qFatal("Tool count mismatch !!!");
661 ////////////////////////////////////////////////////////////
663 ////////////////////////////////////////////////////////////