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 Wine, as x64 binaries won't run under Wine (tested with Wine 1.4 under Ubuntu 12.04 x64)
79 if(cpuSupport
& CPU_TYPE_X64_ALL
)
81 //DWORD osVerNo = lamexp_get_os_version();
82 //if((HIWORD(osVerNo) == 6) && (LOWORD(osVerNo) == 2))
83 if(lamexp_detect_wine())
85 qWarning("Running under Wine on a 64-Bit system. Going to disable all x64 support!\n");
86 cpuSupport
= (cpuSupport
== CPU_TYPE_X64_SSE
) ? CPU_TYPE_X86_SSE
: CPU_TYPE_X86_GEN
;
90 //Print selected CPU type
93 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN
); break;
94 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE
); break;
95 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN
); break;
96 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE
); break;
97 default: throw "CPU support undefined!";
101 QMap
<QString
, QString
> mapChecksum
;
102 QMap
<QString
, unsigned int> mapVersion
;
103 QMap
<QString
, unsigned int> mapCpuType
;
106 for(int i
= 0; i
< INT_MAX
; i
++)
108 if(!g_lamexp_tools
[i
].pcName
&& !g_lamexp_tools
[i
].pcHash
&& !g_lamexp_tools
[i
].uiVersion
)
112 else if(g_lamexp_tools
[i
].pcName
&& g_lamexp_tools
[i
].pcHash
&& g_lamexp_tools
[i
].uiVersion
)
114 const QString currentTool
= QString::fromLatin1(g_lamexp_tools
[i
].pcName
);
115 mapChecksum
.insert(currentTool
, QString::fromLatin1(g_lamexp_tools
[i
].pcHash
));
116 mapCpuType
.insert(currentTool
, g_lamexp_tools
[i
].uiCpuType
);
117 mapVersion
.insert(currentTool
, g_lamexp_tools
[i
].uiVersion
);
121 qFatal("Inconsistent checksum data detected. Take care!");
125 QDir
toolsDir(":/tools/");
126 QList
<QFileInfo
> toolsList
= toolsDir
.entryInfoList(QStringList("*.*"), QDir::Files
, QDir::Name
);
127 QDir appDir
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
133 while(!toolsList
.isEmpty())
137 QFileInfo currentTool
= toolsList
.takeFirst();
138 QString toolName
= currentTool
.fileName().toLower();
139 QString toolShortName
= QString("%1.%2").arg(currentTool
.baseName().toLower(), currentTool
.suffix().toLower());
141 QByteArray toolHash
= mapChecksum
.take(toolName
).toLatin1();
142 unsigned int toolCpuType
= mapCpuType
.take(toolName
);
143 unsigned int toolVersion
= mapVersion
.take(toolName
);
145 if(toolHash
.size() != 72)
147 throw "The required checksum is missing, take care!";
150 if(toolCpuType
& cpuSupport
)
152 QFileInfo
customTool(QString("%1/tools/%2/%3").arg(appDir
.canonicalPath(), QString::number(lamexp_version_build()), toolShortName
));
153 if(customTool
.exists() && customTool
.isFile())
156 qDebug("Setting up file: %s <- %s", toolShortName
.toLatin1().constData(), appDir
.relativeFilePath(customTool
.canonicalFilePath()).toLatin1().constData());
157 LockedFile
*lockedFile
= new LockedFile(customTool
.canonicalFilePath());
158 lamexp_register_tool(toolShortName
, lockedFile
, UINT_MAX
);
162 qDebug("Extracting file: %s -> %s", toolName
.toLatin1().constData(), toolShortName
.toLatin1().constData());
163 LockedFile
*lockedFile
= new LockedFile(QString(":/tools/%1").arg(toolName
), QString("%1/tool_%2").arg(lamexp_temp_folder2(), toolShortName
), toolHash
);
164 lamexp_register_tool(toolShortName
, lockedFile
, toolVersion
);
168 catch(char *errorMsg
)
170 qFatal("At least one of the required tools could not be initialized:\n%s", errorMsg
);
175 //Make sure all files were extracted
176 if(!mapChecksum
.isEmpty())
178 qFatal("At least one required tool could not be found:\n%s", toolsDir
.filePath(mapChecksum
.keys().first()).toLatin1().constData());
182 qDebug("All extracted.\n");
189 //Using any custom tools?
192 qWarning("Warning: Using custom tools, you might encounter unexpected problems!\n");
196 double delayExtract
= static_cast<double>(timer
.elapsed()) / 1000.0;
197 if(delayExtract
> g_allowedExtractDelay
)
199 m_slowIndicator
= true;
200 qWarning("Extracting tools took %.3f seconds -> probably slow realtime virus scanner.", delayExtract
);
201 qWarning("Please report performance problems to your anti-virus developer !!!\n");
204 //Register all translations
207 //Look for AAC encoders
216 ////////////////////////////////////////////////////////////
218 ////////////////////////////////////////////////////////////
220 void InitializationThread::delay(void)
222 const char *temp
= "|/-\\";
223 printf("Thread is doing something important... ?\b", temp
[4]);
225 for(int i
= 0; i
< 20; i
++)
227 printf("%c\b", temp
[i
%4]);
234 void InitializationThread::initTranslations(void)
236 //Search for language files
237 QStringList qmFiles
= QDir(":/localization").entryList(QStringList() << "LameXP_??.qm", QDir::Files
, QDir::Name
);
239 //Make sure we found at least one translation
240 if(qmFiles
.count() < 1)
242 qFatal("Could not find any translation files!");
246 //Add all available translations
247 while(!qmFiles
.isEmpty())
249 QString langId
, langName
;
250 unsigned int systemId
= 0, country
= 0;
251 QString qmFile
= qmFiles
.takeFirst();
253 QRegExp
langIdExp("LameXP_(\\w\\w)\\.qm", Qt::CaseInsensitive
);
254 if(langIdExp
.indexIn(qmFile
) >= 0)
256 langId
= langIdExp
.cap(1).toLower();
257 QResource langRes
= QResource(QString(":/localization/%1.txt").arg(qmFile
));
258 if(langRes
.isValid() && langRes
.size() > 0)
260 QByteArray data
= QByteArray::fromRawData(reinterpret_cast<const char*>(langRes
.data()), langRes
.size());
261 QTextStream
stream(&data
, QIODevice::ReadOnly
);
262 stream
.setAutoDetectUnicode(false); stream
.setCodec("UTF-8");
263 while(!stream
.atEnd())
265 QStringList langInfo
= stream
.readLine().simplified().split(",", QString::SkipEmptyParts
);
266 if(langInfo
.count() == 3)
268 systemId
= langInfo
.at(0).trimmed().toUInt();
269 country
= langInfo
.at(1).trimmed().toUInt();
270 langName
= langInfo
.at(2).trimmed();
277 if(!(langId
.isEmpty() || langName
.isEmpty() || systemId
== 0))
279 if(lamexp_translation_register(langId
, qmFile
, langName
, systemId
, country
))
281 qDebug("Registering translation: %s = %s (%u) [%u]", qmFile
.toUtf8().constData(), langName
.toUtf8().constData(), systemId
, country
);
285 qWarning("Failed to register: %s", qmFile
.toLatin1().constData());
290 qDebug("All registered.\n");
293 void InitializationThread::initNeroAac(void)
295 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
297 QFileInfo neroFileInfo
[3];
298 neroFileInfo
[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(appPath
));
299 neroFileInfo
[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(appPath
));
300 neroFileInfo
[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(appPath
));
302 bool neroFilesFound
= true;
303 for(int i
= 0; i
< 3; i
++) { if(!neroFileInfo
[i
].exists()) neroFilesFound
= false; }
305 //Lock the Nero binaries
308 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
312 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo
[0].canonicalFilePath().toUtf8().constData());
314 LockedFile
*neroBin
[3];
315 for(int i
= 0; i
< 3; i
++) neroBin
[i
] = NULL
;
319 for(int i
= 0; i
< 3; i
++)
321 neroBin
[i
] = new LockedFile(neroFileInfo
[i
].canonicalFilePath());
326 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
327 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
332 process
.setProcessChannelMode(QProcess::MergedChannels
);
333 process
.setReadChannel(QProcess::StandardOutput
);
334 process
.start(neroFileInfo
[0].canonicalFilePath(), QStringList() << "-help");
336 if(!process
.waitForStarted())
338 qWarning("Nero process failed to create!");
339 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
341 process
.waitForFinished(-1);
342 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
346 unsigned int neroVersion
= 0;
348 while(process
.state() != QProcess::NotRunning
)
350 if(!process
.waitForReadyRead())
352 if(process
.state() == QProcess::Running
)
354 qWarning("Nero process time out -> killing!");
356 process
.waitForFinished(-1);
357 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
362 while(process
.canReadLine())
364 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
365 QStringList tokens
= line
.split(" ", QString::SkipEmptyParts
, Qt::CaseInsensitive
);
366 int index1
= tokens
.indexOf("Package");
367 int index2
= tokens
.indexOf("version:");
368 if(index1
>= 0 && index2
>= 0 && index1
+ 1 == index2
&& index2
< tokens
.count() - 1)
370 QStringList versionTokens
= tokens
.at(index2
+ 1).split(".", QString::SkipEmptyParts
, Qt::CaseInsensitive
);
371 if(versionTokens
.count() == 4)
374 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(3).toInt()));
375 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(2).toInt())) * 10;
376 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(1).toInt())) * 100;
377 neroVersion
+= qMin(9, qMax(0, versionTokens
.at(0).toInt())) * 1000;
383 if(!(neroVersion
> 0))
385 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
386 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
390 for(int i
= 0; i
< 3; i
++)
392 lamexp_register_tool(neroFileInfo
[i
].fileName(), neroBin
[i
], neroVersion
);
396 void InitializationThread::initFhgAac(void)
398 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
400 QFileInfo fhgFileInfo
[4];
401 fhgFileInfo
[0] = QFileInfo(QString("%1/fhgaacenc.exe").arg(appPath
));
402 fhgFileInfo
[1] = QFileInfo(QString("%1/enc_fhgaac.dll").arg(appPath
));
403 fhgFileInfo
[2] = QFileInfo(QString("%1/nsutil.dll").arg(appPath
));
404 fhgFileInfo
[3] = QFileInfo(QString("%1/libmp4v2.dll").arg(appPath
));
406 bool fhgFilesFound
= true;
407 for(int i
= 0; i
< 4; i
++) { if(!fhgFileInfo
[i
].exists()) fhgFilesFound
= false; }
409 //Lock the FhgAacEnc binaries
412 qDebug("FhgAacEnc binaries not found -> FhgAacEnc support will be disabled!\n");
416 qDebug("Found FhgAacEnc cli_exe:\n%s\n", fhgFileInfo
[0].canonicalFilePath().toUtf8().constData());
417 qDebug("Found FhgAacEnc enc_dll:\n%s\n", fhgFileInfo
[1].canonicalFilePath().toUtf8().constData());
419 LockedFile
*fhgBin
[4];
420 for(int i
= 0; i
< 4; i
++) fhgBin
[i
] = NULL
;
424 for(int i
= 0; i
< 4; i
++)
426 fhgBin
[i
] = new LockedFile(fhgFileInfo
[i
].canonicalFilePath());
431 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
432 qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
437 process
.setProcessChannelMode(QProcess::MergedChannels
);
438 process
.setReadChannel(QProcess::StandardOutput
);
439 process
.start(fhgFileInfo
[0].canonicalFilePath(), QStringList() << "--version");
441 if(!process
.waitForStarted())
443 qWarning("FhgAacEnc process failed to create!");
444 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
446 process
.waitForFinished(-1);
447 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
451 QRegExp
fhgAacEncSig("fhgaacenc version (\\d+) by tmkk", Qt::CaseInsensitive
);
452 unsigned int fhgVersion
= 0;
454 while(process
.state() != QProcess::NotRunning
)
456 process
.waitForReadyRead();
457 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
459 qWarning("FhgAacEnc process time out -> killing!");
461 process
.waitForFinished(-1);
462 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
465 while(process
.bytesAvailable() > 0)
467 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
468 if(fhgAacEncSig
.lastIndexIn(line
) >= 0)
471 unsigned int temp
= fhgAacEncSig
.cap(1).toUInt(&ok
);
472 if(ok
) fhgVersion
= temp
;
477 if(!(fhgVersion
> 0))
479 qWarning("FhgAacEnc version couldn't be determined -> FhgAacEnc support will be disabled!");
480 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
483 else if(fhgVersion
< lamexp_toolver_fhgaacenc())
485 qWarning("FhgAacEnc version is too much outdated -> FhgAacEnc support will be disabled!");
486 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
490 for(int i
= 0; i
< 4; i
++)
492 lamexp_register_tool(fhgFileInfo
[i
].fileName(), fhgBin
[i
], fhgVersion
);
496 void InitializationThread::initQAac(void)
498 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
500 QFileInfo qaacFileInfo
[2];
501 qaacFileInfo
[0] = QFileInfo(QString("%1/qaac.exe").arg(appPath
));
502 qaacFileInfo
[1] = QFileInfo(QString("%1/libsoxrate.dll").arg(appPath
));
504 bool qaacFilesFound
= true;
505 for(int i
= 0; i
< 2; i
++) { if(!qaacFileInfo
[i
].exists()) qaacFilesFound
= false; }
507 //Lock the QAAC binaries
510 qDebug("QAAC binaries not found -> QAAC support will be disabled!\n");
514 qDebug("Found QAAC encoder:\n%s\n", qaacFileInfo
[0].canonicalFilePath().toUtf8().constData());
516 LockedFile
*qaacBin
[2];
517 for(int i
= 0; i
< 2; i
++) qaacBin
[i
] = NULL
;
521 for(int i
= 0; i
< 2; i
++)
523 qaacBin
[i
] = new LockedFile(qaacFileInfo
[i
].canonicalFilePath());
528 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
529 qWarning("Failed to get excluive lock to QAAC binary -> QAAC support will be disabled!");
534 process
.setProcessChannelMode(QProcess::MergedChannels
);
535 process
.setReadChannel(QProcess::StandardOutput
);
536 process
.start(qaacFileInfo
[0].canonicalFilePath(), QStringList() << "--check");
538 if(!process
.waitForStarted())
540 qWarning("QAAC process failed to create!");
541 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
543 process
.waitForFinished(-1);
544 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
548 QRegExp
qaacEncSig("qaac (\\d)\\.(\\d)(\\d)", Qt::CaseInsensitive
);
549 QRegExp
coreEncSig("CoreAudioToolbox (\\d)\\.(\\d)\\.(\\d)\\.(\\d)", Qt::CaseInsensitive
);
550 unsigned int qaacVersion
= 0;
551 unsigned int coreVersion
= 0;
553 while(process
.state() != QProcess::NotRunning
)
555 process
.waitForReadyRead();
556 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
558 qWarning("QAAC process time out -> killing!");
560 process
.waitForFinished(-1);
561 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
564 while(process
.bytesAvailable() > 0)
566 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
567 if(qaacEncSig
.lastIndexIn(line
) >= 0)
569 unsigned int tmp
[3] = {0, 0, 0};
570 bool ok
[3] = {false, false, false};
571 tmp
[0] = qaacEncSig
.cap(1).toUInt(&ok
[0]);
572 tmp
[1] = qaacEncSig
.cap(2).toUInt(&ok
[1]);
573 tmp
[2] = qaacEncSig
.cap(3).toUInt(&ok
[2]);
574 if(ok
[0] && ok
[1] && ok
[2])
576 qaacVersion
= (qBound(0U, tmp
[0], 9U) * 100) + (qBound(0U, tmp
[1], 9U) * 10) + qBound(0U, tmp
[2], 9U);
579 if(coreEncSig
.lastIndexIn(line
) >= 0)
581 unsigned int tmp
[4] = {0, 0, 0, 0};
582 bool ok
[4] = {false, false, false, false};
583 tmp
[0] = coreEncSig
.cap(1).toUInt(&ok
[0]);
584 tmp
[1] = coreEncSig
.cap(2).toUInt(&ok
[1]);
585 tmp
[2] = coreEncSig
.cap(3).toUInt(&ok
[2]);
586 tmp
[3] = coreEncSig
.cap(4).toUInt(&ok
[3]);
587 if(ok
[0] && ok
[1] && ok
[2] && ok
[3])
589 coreVersion
= (qBound(0U, tmp
[0], 9U) * 1000) + (qBound(0U, tmp
[1], 9U) * 100) + (qBound(0U, tmp
[2], 9U) * 10) + qBound(0U, tmp
[3], 9U);
595 //qDebug("qaac %d, CoreAudioToolbox %d", qaacVersion, coreVersion);
597 if(!(qaacVersion
> 0))
599 qWarning("QAAC version couldn't be determined -> QAAC support will be disabled!");
600 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
603 else if(qaacVersion
< lamexp_toolver_qaacenc())
605 qWarning("QAAC version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.??", qaacVersion
, "N/A").toLatin1().constData());
606 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
610 if(!(coreVersion
> 0))
612 qWarning("CoreAudioToolbox version couldn't be determined -> QAAC support will be disabled!");
613 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
616 else if(coreVersion
< lamexp_toolver_coreaudio())
618 qWarning("CoreAudioToolbox version is too much outdated (%s) -> QAAC support will be disabled!", lamexp_version2string("v?.?.?.?", coreVersion
, "N/A").toLatin1().constData());
619 for(int i
= 0; i
< 2; i
++) LAMEXP_DELETE(qaacBin
[i
]);
623 lamexp_register_tool(qaacFileInfo
[0].fileName(), qaacBin
[0], qaacVersion
);
624 lamexp_register_tool(qaacFileInfo
[1].fileName(), qaacBin
[1], qaacVersion
);
627 void InitializationThread::selfTest(void)
629 const unsigned int cpu
[4] = {CPU_TYPE_X86_GEN
, CPU_TYPE_X86_SSE
, CPU_TYPE_X64_GEN
, CPU_TYPE_X64_SSE
};
631 for(size_t k
= 0; k
< 4; k
++)
636 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN
); break;
637 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE
); break;
638 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN
); break;
639 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE
); break;
640 default: throw "CPU support undefined!";
643 for(int i
= 0; i
< INT_MAX
; i
++)
645 if(!g_lamexp_tools
[i
].pcName
&& !g_lamexp_tools
[i
].pcHash
&& !g_lamexp_tools
[i
].uiVersion
)
649 if(g_lamexp_tools
[i
].uiCpuType
& cpu
[k
])
651 qDebug("%02i -> %s", ++n
, g_lamexp_tools
[i
].pcName
);
656 qFatal("Tool count mismatch !!!");
662 ////////////////////////////////////////////////////////////
664 ////////////////////////////////////////////////////////////