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>
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
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 QString qmFile
= qmFiles
.takeFirst();
250 unsigned int systemId
= 0;
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() == 2)
267 systemId
= langInfo
.at(0).trimmed().toUInt();
268 langName
= langInfo
.at(1).trimmed();
275 if(!(langId
.isEmpty() || langName
.isEmpty() || systemId
== 0))
277 if(lamexp_translation_register(langId
, qmFile
, langName
, systemId
))
279 qDebug("Registering translation: %s = %s (%u)", qmFile
.toUtf8().constData(), langName
.toUtf8().constData(), systemId
);
283 qWarning("Failed to register: %s", qmFile
.toLatin1().constData());
288 qDebug("All registered.\n");
291 void InitializationThread::initNeroAac(void)
293 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
295 QFileInfo neroFileInfo
[3];
296 neroFileInfo
[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(appPath
));
297 neroFileInfo
[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(appPath
));
298 neroFileInfo
[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(appPath
));
300 bool neroFilesFound
= true;
301 for(int i
= 0; i
< 3; i
++) { if(!neroFileInfo
[i
].exists()) neroFilesFound
= false; }
303 //Lock the Nero binaries
306 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
310 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo
[0].canonicalFilePath().toUtf8().constData());
312 LockedFile
*neroBin
[3];
313 for(int i
= 0; i
< 3; i
++) neroBin
[i
] = NULL
;
317 for(int i
= 0; i
< 3; i
++)
319 neroBin
[i
] = new LockedFile(neroFileInfo
[i
].canonicalFilePath());
324 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
325 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
330 process
.setProcessChannelMode(QProcess::MergedChannels
);
331 process
.setReadChannel(QProcess::StandardOutput
);
332 process
.start(neroFileInfo
[0].canonicalFilePath(), QStringList() << "-help");
334 if(!process
.waitForStarted())
336 qWarning("Nero process failed to create!");
337 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
339 process
.waitForFinished(-1);
340 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
344 unsigned int neroVersion
= 0;
346 while(process
.state() != QProcess::NotRunning
)
348 if(!process
.waitForReadyRead())
350 if(process
.state() == QProcess::Running
)
352 qWarning("Nero process time out -> killing!");
354 process
.waitForFinished(-1);
355 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
360 while(process
.canReadLine())
362 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
363 QStringList tokens
= line
.split(" ", QString::SkipEmptyParts
, Qt::CaseInsensitive
);
364 int index1
= tokens
.indexOf("Package");
365 int index2
= tokens
.indexOf("version:");
366 if(index1
>= 0 && index2
>= 0 && index1
+ 1 == index2
&& index2
< tokens
.count() - 1)
368 QStringList versionTokens
= tokens
.at(index2
+ 1).split(".", QString::SkipEmptyParts
, Qt::CaseInsensitive
);
369 if(versionTokens
.count() == 4)
372 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(3).toInt()));
373 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(2).toInt())) * 10;
374 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(1).toInt())) * 100;
375 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(0).toInt())) * 1000;
381 if(!(neroVersion
> 0))
383 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
384 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
388 for(int i
= 0; i
< 3; i
++)
390 lamexp_register_tool(neroFileInfo
[i
].fileName(), neroBin
[i
], neroVersion
);
394 void InitializationThread::initFhgAac(void)
396 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
398 QFileInfo fhgFileInfo
[4];
399 fhgFileInfo
[0] = QFileInfo(QString("%1/fhgaacenc.exe").arg(appPath
));
400 fhgFileInfo
[1] = QFileInfo(QString("%1/enc_fhgaac.dll").arg(appPath
));
401 fhgFileInfo
[2] = QFileInfo(QString("%1/nsutil.dll").arg(appPath
));
402 fhgFileInfo
[3] = QFileInfo(QString("%1/libmp4v2.dll").arg(appPath
));
404 bool fhgFilesFound
= true;
405 for(int i
= 0; i
< 4; i
++) { if(!fhgFileInfo
[i
].exists()) fhgFilesFound
= false; }
407 //Lock the FhgAacEnc binaries
410 qDebug("FhgAacEnc binaries not found -> FhgAacEnc support will be disabled!\n");
414 qDebug("Found FhgAacEnc cli_exe:\n%s\n", fhgFileInfo
[0].canonicalFilePath().toUtf8().constData());
415 qDebug("Found FhgAacEnc enc_dll:\n%s\n", fhgFileInfo
[1].canonicalFilePath().toUtf8().constData());
417 LockedFile
*fhgBin
[4];
418 for(int i
= 0; i
< 4; i
++) fhgBin
[i
] = NULL
;
422 for(int i
= 0; i
< 4; i
++)
424 fhgBin
[i
] = new LockedFile(fhgFileInfo
[i
].canonicalFilePath());
429 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
430 qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
435 process
.setProcessChannelMode(QProcess::MergedChannels
);
436 process
.setReadChannel(QProcess::StandardOutput
);
437 process
.start(fhgFileInfo
[0].canonicalFilePath(), QStringList() << "--version");
439 if(!process
.waitForStarted())
441 qWarning("FhgAacEnc process failed to create!");
442 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
444 process
.waitForFinished(-1);
445 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
449 QRegExp
fhgAacEncSig("fhgaacenc version (\\d+) by tmkk", Qt::CaseInsensitive
);
450 unsigned int fhgVersion
= 0;
452 while(process
.state() != QProcess::NotRunning
)
454 process
.waitForReadyRead();
455 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
457 qWarning("FhgAacEnc process time out -> killing!");
459 process
.waitForFinished(-1);
460 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
463 while(process
.bytesAvailable() > 0)
465 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
466 if(fhgAacEncSig
.lastIndexIn(line
) >= 0)
469 unsigned int temp
= fhgAacEncSig
.cap(1).toUInt(&ok
);
470 if(ok
) fhgVersion
= temp
;
475 if(!(fhgVersion
> 0))
477 qWarning("FhgAacEnc version couldn't be determined -> FhgAacEnc support will be disabled!");
478 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
481 else if(fhgVersion
< lamexp_toolver_fhgaacenc())
483 qWarning("FhgAacEnc version is too much outdated -> FhgAacEnc support will be disabled!");
484 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
488 for(int i
= 0; i
< 4; i
++)
490 lamexp_register_tool(fhgFileInfo
[i
].fileName(), fhgBin
[i
], fhgVersion
);
494 void InitializationThread::initQAac(void)
496 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
498 QFileInfo qaacFileInfo
[2];
499 qaacFileInfo
[0] = QFileInfo(QString("%1/qaac.exe").arg(appPath
));
500 qaacFileInfo
[1] = QFileInfo(QString("%1/libsoxrate.dll").arg(appPath
));
502 bool qaacFilesFound
= true;
503 for(int i
= 0; i
< 2; i
++) { if(!qaacFileInfo
[i
].exists()) qaacFilesFound
= false; }
505 //Lock the QAAC binaries
508 qDebug("QAAC binaries not found -> QAAC support will be disabled!\n");
512 qDebug("Found QAAC encoder:\n%s\n", qaacFileInfo
[0].canonicalFilePath().toUtf8().constData());
514 LockedFile
*qaacBin
[2];
515 for(int i
= 0; i
< 2; i
++) qaacBin
[i
] = NULL
;
519 for(int i
= 0; i
< 2; i
++)
521 qaacBin
[i
] = new LockedFile(qaacFileInfo
[i
].canonicalFilePath());
526 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
527 qWarning("Failed to get excluive lock to QAAC binary -> QAAC support will be disabled!");
532 process
.setProcessChannelMode(QProcess::MergedChannels
);
533 process
.setReadChannel(QProcess::StandardOutput
);
534 process
.start(qaacFileInfo
[0].canonicalFilePath(), QStringList() << "--check");
536 if(!process
.waitForStarted())
538 qWarning("QAAC process failed to create!");
539 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
541 process
.waitForFinished(-1);
542 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
546 QRegExp
qaacEncSig("qaac (\\d)\\.(\\d)(\\d)", Qt::CaseInsensitive
);
547 QRegExp
coreEncSig("CoreAudioToolbox (\\d)\\.(\\d)\\.(\\d)\\.(\\d)", Qt::CaseInsensitive
);
548 unsigned int qaacVersion
= 0;
549 unsigned int coreVersion
= 0;
551 while(process
.state() != QProcess::NotRunning
)
553 process
.waitForReadyRead();
554 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
556 qWarning("QAAC process time out -> killing!");
558 process
.waitForFinished(-1);
559 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
562 while(process
.bytesAvailable() > 0)
564 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
565 if(qaacEncSig
.lastIndexIn(line
) >= 0)
567 unsigned int tmp
[3] = {0, 0, 0};
568 bool ok
[3] = {false, false, false};
569 tmp
[0] = qaacEncSig
.cap(1).toUInt(&ok
[0]);
570 tmp
[1] = qaacEncSig
.cap(2).toUInt(&ok
[1]);
571 tmp
[2] = qaacEncSig
.cap(3).toUInt(&ok
[2]);
572 if(ok
[0] && ok
[1] && ok
[2])
574 qaacVersion
= (qBound(0U, tmp
[0], 9U) * 100) + (qBound(0U, tmp
[1], 9U) * 10) + qBound(0U, tmp
[2], 9U);
577 if(coreEncSig
.lastIndexIn(line
) >= 0)
579 unsigned int tmp
[4] = {0, 0, 0, 0};
580 bool ok
[4] = {false, false, false, false};
581 tmp
[0] = coreEncSig
.cap(1).toUInt(&ok
[0]);
582 tmp
[1] = coreEncSig
.cap(2).toUInt(&ok
[1]);
583 tmp
[2] = coreEncSig
.cap(3).toUInt(&ok
[2]);
584 tmp
[3] = coreEncSig
.cap(4).toUInt(&ok
[3]);
585 if(ok
[0] && ok
[1] && ok
[2] && ok
[3])
587 coreVersion
= (qBound(0U, tmp
[0], 9U) * 1000) + (qBound(0U, tmp
[1], 9U) * 100) + (qBound(0U, tmp
[2], 9U) * 10) + qBound(0U, tmp
[3], 9U);
593 //qDebug("qaac %d, CoreAudioToolbox %d", qaacVersion, coreVersion);
595 if(!(qaacVersion
> 0))
597 qWarning("QAAC version couldn't be determined -> QAAC support will be disabled!");
598 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
601 else if(qaacVersion
< lamexp_toolver_qaacenc())
603 qWarning("QAAC version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.??", qaacVersion
, "N/A").toLatin1().constData());
604 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
608 if(!(coreVersion
> 0))
610 qWarning("CoreAudioToolbox version couldn't be determined -> QAAC support will be disabled!");
611 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
614 else if(coreVersion
< lamexp_toolver_coreaudio())
616 qWarning("CoreAudioToolbox version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.?.?.?", coreVersion
, "N/A").toLatin1().constData());
617 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
621 lamexp_register_tool(qaacFileInfo
[0].fileName(), qaacBin
[0], qaacVersion
);
622 lamexp_register_tool(qaacFileInfo
[1].fileName(), qaacBin
[1], qaacVersion
);
625 void InitializationThread::selfTest(void)
627 const unsigned int cpu
[4] = {CPU_TYPE_X86_GEN
, CPU_TYPE_X86_SSE
, CPU_TYPE_X64_GEN
, CPU_TYPE_X64_SSE
};
629 for(size_t k
= 0; k
< 4; k
++)
634 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN
); break;
635 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE
); break;
636 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN
); break;
637 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE
); break;
638 default: throw "CPU support undefined!";
641 for(int i
= 0; i
< INT_MAX
; i
++)
643 if(!g_lamexp_tools
[i
].pcName
&& !g_lamexp_tools
[i
].pcHash
&& !g_lamexp_tools
[i
].uiVersion
)
647 if(g_lamexp_tools
[i
].uiCpuType
& cpu
[k
])
649 qDebug("%02i -> %s", ++n
, g_lamexp_tools
[i
].pcName
);
654 qFatal("Tool count mismatch !!!");
660 ////////////////////////////////////////////////////////////
662 ////////////////////////////////////////////////////////////