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 Nero AAC encoder
208 //Look for FHG AAC encoder
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;
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();
258 QResource langRes
= (QString(":/localization/%1.txt").arg(qmFile
));
259 if(langRes
.isValid() && langRes
.size() > 0)
261 QStringList langInfo
= QString::fromUtf8(reinterpret_cast<const char*>(langRes
.data()), langRes
.size()).simplified().split(",", QString::SkipEmptyParts
);
262 if(langInfo
.count() == 2)
264 systemId
= langInfo
.at(0).toUInt();
265 langName
= langInfo
.at(1);
269 if(lamexp_translation_register(langId
, qmFile
, langName
, systemId
))
271 qDebug("Registering translation: %s = %s (%u)", qmFile
.toUtf8().constData(), langName
.toUtf8().constData(), systemId
);
275 qWarning("Failed to register: %s", qmFile
.toLatin1().constData());
279 qDebug("All registered.\n");
282 void InitializationThread::initNeroAac(void)
284 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
286 QFileInfo neroFileInfo
[3];
287 neroFileInfo
[0] = QFileInfo(QString("%1/neroAacEnc.exe").arg(appPath
));
288 neroFileInfo
[1] = QFileInfo(QString("%1/neroAacDec.exe").arg(appPath
));
289 neroFileInfo
[2] = QFileInfo(QString("%1/neroAacTag.exe").arg(appPath
));
291 bool neroFilesFound
= true;
292 for(int i
= 0; i
< 3; i
++) { if(!neroFileInfo
[i
].exists()) neroFilesFound
= false; }
294 //Lock the Nero binaries
297 qDebug("Nero encoder binaries not found -> AAC encoding support will be disabled!\n");
301 qDebug("Found Nero AAC encoder binary:\n%s\n", neroFileInfo
[0].canonicalFilePath().toUtf8().constData());
303 LockedFile
*neroBin
[3];
304 for(int i
= 0; i
< 3; i
++) neroBin
[i
] = NULL
;
308 for(int i
= 0; i
< 3; i
++)
310 neroBin
[i
] = new LockedFile(neroFileInfo
[i
].canonicalFilePath());
315 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
316 qWarning("Failed to get excluive lock to Nero encoder binary -> AAC encoding support will be disabled!");
321 process
.setProcessChannelMode(QProcess::MergedChannels
);
322 process
.setReadChannel(QProcess::StandardOutput
);
323 process
.start(neroFileInfo
[0].canonicalFilePath(), QStringList() << "-help");
325 if(!process
.waitForStarted())
327 qWarning("Nero process failed to create!");
328 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
330 process
.waitForFinished(-1);
331 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
335 unsigned int neroVersion
= 0;
337 while(process
.state() != QProcess::NotRunning
)
339 if(!process
.waitForReadyRead())
341 if(process
.state() == QProcess::Running
)
343 qWarning("Nero process time out -> killing!");
345 process
.waitForFinished(-1);
346 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
351 while(process
.canReadLine())
353 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
354 QStringList tokens
= line
.split(" ", QString::SkipEmptyParts
, Qt::CaseInsensitive
);
355 int index1
= tokens
.indexOf("Package");
356 int index2
= tokens
.indexOf("version:");
357 if(index1
>= 0 && index2
>= 0 && index1
+ 1 == index2
&& index2
< tokens
.count() - 1)
359 QStringList versionTokens
= tokens
.at(index2
+ 1).split(".", QString::SkipEmptyParts
, Qt::CaseInsensitive
);
360 if(versionTokens
.count() == 4)
363 neroVersion
+= min(9, max(0, versionTokens
.at(3).toInt()));
364 neroVersion
+= min(9, max(0, versionTokens
.at(2).toInt())) * 10;
365 neroVersion
+= min(9, max(0, versionTokens
.at(1).toInt())) * 100;
366 neroVersion
+= min(9, max(0, versionTokens
.at(0).toInt())) * 1000;
372 if(!(neroVersion
> 0))
374 qWarning("Nero AAC version could not be determined -> AAC encoding support will be disabled!");
375 for(int i
= 0; i
< 3; i
++) LAMEXP_DELETE(neroBin
[i
]);
379 for(int i
= 0; i
< 3; i
++)
381 lamexp_register_tool(neroFileInfo
[i
].fileName(), neroBin
[i
], neroVersion
);
385 void InitializationThread::initFhgAac(void)
387 const QString appPath
= QDir(QCoreApplication::applicationDirPath()).canonicalPath();
389 QFileInfo fhgFileInfo
[4];
390 fhgFileInfo
[0] = QFileInfo(QString("%1/fhgaacenc.exe").arg(appPath
));
391 fhgFileInfo
[1] = QFileInfo(QString("%1/enc_fhgaac.dll").arg(appPath
));
392 fhgFileInfo
[2] = QFileInfo(QString("%1/nsutil.dll").arg(appPath
));
393 fhgFileInfo
[3] = QFileInfo(QString("%1/libmp4v2.dll").arg(appPath
));
395 bool fhgFilesFound
= true;
396 for(int i
= 0; i
< 4; i
++) { if(!fhgFileInfo
[i
].exists()) fhgFilesFound
= false; }
398 //Lock the FhgAacEnc binaries
401 qDebug("FhgAacEnc binaries not found -> FhgAacEnc support will be disabled!\n");
405 qDebug("Found FhgAacEnc cli_exe:\n%s\n", fhgFileInfo
[0].canonicalFilePath().toUtf8().constData());
406 qDebug("Found FhgAacEnc enc_dll:\n%s\n", fhgFileInfo
[1].canonicalFilePath().toUtf8().constData());
408 LockedFile
*fhgBin
[4];
409 for(int i
= 0; i
< 4; i
++) fhgBin
[i
] = NULL
;
413 for(int i
= 0; i
< 4; i
++)
415 fhgBin
[i
] = new LockedFile(fhgFileInfo
[i
].canonicalFilePath());
420 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
421 qWarning("Failed to get excluive lock to FhgAacEnc binary -> FhgAacEnc support will be disabled!");
426 process
.setProcessChannelMode(QProcess::MergedChannels
);
427 process
.setReadChannel(QProcess::StandardOutput
);
428 process
.start(fhgFileInfo
[0].canonicalFilePath(), QStringList() << "--version");
430 if(!process
.waitForStarted())
432 qWarning("FhgAacEnc process failed to create!");
433 qWarning("Error message: \"%s\"\n", process
.errorString().toLatin1().constData());
435 process
.waitForFinished(-1);
436 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
440 QRegExp
fhgAacEncSig("fhgaacenc version (\\d+) by tmkk", Qt::CaseInsensitive
);
441 unsigned int fhgVersion
= 0;
443 while(process
.state() != QProcess::NotRunning
)
445 process
.waitForReadyRead();
446 if(!process
.bytesAvailable() && process
.state() == QProcess::Running
)
448 qWarning("FhgAacEnc process time out -> killing!");
450 process
.waitForFinished(-1);
451 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
454 while(process
.bytesAvailable() > 0)
456 QString line
= QString::fromUtf8(process
.readLine().constData()).simplified();
457 if(fhgAacEncSig
.lastIndexIn(line
) >= 0)
460 unsigned int temp
= fhgAacEncSig
.cap(1).toUInt(&ok
);
461 if(ok
) fhgVersion
= temp
;
466 if(!(fhgVersion
> 0))
468 qWarning("FhgAacEnc version couldn't be determined -> FhgAacEnc support will be disabled!");
469 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
472 else if(fhgVersion
< lamexp_toolver_fhgaacenc())
474 qWarning("FhgAacEnc version is too much outdated -> FhgAacEnc support will be disabled!");
475 for(int i
= 0; i
< 4; i
++) LAMEXP_DELETE(fhgBin
[i
]);
479 for(int i
= 0; i
< 4; i
++)
481 lamexp_register_tool(fhgFileInfo
[i
].fileName(), fhgBin
[i
], fhgVersion
);
485 void InitializationThread::selfTest(void)
487 const unsigned int cpu
[4] = {CPU_TYPE_X86_GEN
, CPU_TYPE_X86_SSE
, CPU_TYPE_X64_GEN
, CPU_TYPE_X64_SSE
};
489 for(size_t k
= 0; k
< 4; k
++)
494 PRINT_CPU_TYPE(CPU_TYPE_X86_GEN
); break;
495 PRINT_CPU_TYPE(CPU_TYPE_X86_SSE
); break;
496 PRINT_CPU_TYPE(CPU_TYPE_X64_GEN
); break;
497 PRINT_CPU_TYPE(CPU_TYPE_X64_SSE
); break;
498 default: throw "CPU support undefined!";
501 for(int i
= 0; i
< INT_MAX
; i
++)
503 if(!g_lamexp_tools
[i
].pcName
&& !g_lamexp_tools
[i
].pcHash
&& !g_lamexp_tools
[i
].uiVersion
)
507 if(g_lamexp_tools
[i
].uiCpuType
& cpu
[k
])
509 qDebug("%02i -> %s", ++n
, g_lamexp_tools
[i
].pcName
);
514 qFatal("Tool count mismatch !!!");
520 ////////////////////////////////////////////////////////////
522 ////////////////////////////////////////////////////////////