Added support for detecting "portable" Avisynth.
[simple-x264-launcher.git] / src / thread_vapoursynth.cpp
blobeeed03d7bdaee568b0acca21019afc17fe0fa07b
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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_vapoursynth.h"
24 //Mutils
25 #include <MUtils/OSSupport.h>
26 #include <MUtils/Registry.h>
28 //Qt
29 #include <QLibrary>
30 #include <QEventLoop>
31 #include <QTimer>
32 #include <QMutexLocker>
33 #include <QApplication>
34 #include <QDir>
35 #include <QProcess>
37 //Internal
38 #include "global.h"
39 #include "model_sysinfo.h"
41 //CRT
42 #include <cassert>
44 //Const
45 static const bool ENABLE_PORTABLE_VPS = true;
47 //Static
48 QMutex VapourSynthCheckThread::m_vpsLock;
49 QScopedPointer<QFile> VapourSynthCheckThread::m_vpsExePath[2];
50 QScopedPointer<QFile> VapourSynthCheckThread::m_vpsDllPath[2];
52 #define VALID_DIR(STR) ((!(STR).isEmpty()) && QDir((STR)).exists())
53 #define BOOLIFY(X) ((X) ? '1' : '0')
54 #define VPS_BITNESS(X) (((X) + 1U) * 32U)
56 static inline QString &cleanDir(QString &path)
58 if(!path.isEmpty())
60 path = QDir::fromNativeSeparators(path);
61 while(path.endsWith('/'))
63 path.chop(1);
66 return path;
69 //-------------------------------------
70 // External API
71 //-------------------------------------
73 bool VapourSynthCheckThread::detect(SysinfoModel *sysinfo)
75 sysinfo->clearVapourSynth();
76 sysinfo->clearVPSPath();
78 QMutexLocker lock(&m_vpsLock);
80 QEventLoop loop;
81 VapourSynthCheckThread thread;
83 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
85 connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
86 connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
88 thread.start();
89 QTimer::singleShot(15000, &loop, SLOT(quit()));
91 qDebug("VapourSynth thread has been created, please wait...");
92 loop.exec(QEventLoop::ExcludeUserInputEvents);
93 qDebug("VapourSynth thread finished.");
95 QApplication::restoreOverrideCursor();
97 if(!thread.wait(1000))
99 qWarning("VapourSynth thread encountered timeout -> probably deadlock!");
100 thread.terminate();
101 thread.wait();
102 return false;
105 if(thread.getException())
107 qWarning("VapourSynth thread encountered an exception !!!");
108 return false;
111 if(thread.getSuccess())
113 sysinfo->setVapourSynth(SysinfoModel::VapourSynth_X86, thread.getSuccess() & VAPOURSYNTH_X86);
114 sysinfo->setVapourSynth(SysinfoModel::VapourSynth_X64, thread.getSuccess() & VAPOURSYNTH_X64);
115 sysinfo->setVPSPath(thread.getPath());
116 qDebug("VapourSynth support is officially enabled now! [x86=%c, x64=%c]", BOOLIFY(sysinfo->getVapourSynth(SysinfoModel::VapourSynth_X86)), BOOLIFY(sysinfo->getVapourSynth(SysinfoModel::VapourSynth_X64)));
118 else
120 qWarning("VapourSynth could not be found -> VapourSynth support disabled!");
123 return true;
126 //-------------------------------------
127 // Thread class
128 //-------------------------------------
130 VapourSynthCheckThread::VapourSynthCheckThread(void)
132 m_success &= 0;
133 m_exception = false;
134 m_vpsPath.clear();
137 VapourSynthCheckThread::~VapourSynthCheckThread(void)
141 void VapourSynthCheckThread::run(void)
143 m_success &= 0;
144 m_exception = false;
145 m_vpsPath.clear();
147 detectVapourSynthPath1(m_success, m_vpsPath, &m_exception);
150 void VapourSynthCheckThread::detectVapourSynthPath1(int &success, QString &path, volatile bool *exception)
152 __try
154 return detectVapourSynthPath2(success, path, exception);
156 __except(1)
158 *exception = true;
159 qWarning("Unhandled exception error in VapourSynth thread !!!");
163 void VapourSynthCheckThread::detectVapourSynthPath2(int &success, QString &path, volatile bool *exception)
167 return detectVapourSynthPath3(success, path);
169 catch(...)
171 *exception = true;
172 qWarning("VapourSynth initializdation raised an C++ exception!");
176 void VapourSynthCheckThread::detectVapourSynthPath3(int &success, QString &path)
178 success &= 0;
179 path.clear();
181 static const char *VPS_CORE_DIR[] =
183 "core32",
184 "core64",
185 NULL
187 static const int VPS_BIT_FLAG[] =
189 VAPOURSYNTH_X86,
190 VAPOURSYNTH_X64,
191 NULL
193 static const char *VPS_REG_KEYS[] =
195 "SOFTWARE\\VapourSynth",
196 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\VapourSynth_is1",
197 NULL
199 static const char *VPS_REG_NAME[] =
201 "Path",
202 "InstallLocation",
203 "Inno Setup: App Path",
204 NULL
206 static const MUtils::Registry::reg_scope_t REG_SCOPE[3] =
208 MUtils::Registry::scope_default,
209 MUtils::Registry::scope_wow_x32,
210 MUtils::Registry::scope_wow_x64
213 QString vapoursynthPath;
215 //Look for "portable" VapourSynth version
216 if (ENABLE_PORTABLE_VPS)
218 const QString vpsPortableDir = QString("%1/extra/VapourSynth").arg(QCoreApplication::applicationDirPath());
219 if (VALID_DIR(vpsPortableDir))
221 for (size_t i = 0; VPS_CORE_DIR[i]; i++)
223 const QFileInfo vpsPortableDll = QFileInfo(QString("%1/%2/VapourSynth.dll").arg(vpsPortableDir, QString::fromLatin1(VPS_CORE_DIR[i])));
224 if (vpsPortableDll.exists() && vpsPortableDll.isFile())
226 vapoursynthPath = vpsPortableDir;
227 break;
233 //Read VapourSynth path from registry
234 if (vapoursynthPath.isEmpty())
236 for (size_t i = 0; VPS_REG_KEYS[i]; i++)
238 for (size_t j = 0; VPS_REG_NAME[j]; j++)
240 for (size_t k = 0; k < 3; k++)
242 QString temp;
243 if (MUtils::Registry::reg_value_read(MUtils::Registry::root_machine, QString::fromLatin1(VPS_REG_KEYS[i]), QString::fromLatin1(VPS_REG_NAME[j]), temp, REG_SCOPE[k]))
245 temp = cleanDir(temp);
246 if (VALID_DIR(temp))
248 vapoursynthPath = temp;
249 break;
253 if (!vapoursynthPath.isEmpty())
255 break;
258 if (!vapoursynthPath.isEmpty())
260 break;
265 //Make sure VapourSynth directory does exist
266 if(vapoursynthPath.isEmpty())
268 qWarning("VapourSynth install path not found -> disable VapouSynth support!");
269 return;
272 //Validate the VapourSynth installation now!
273 qDebug("VapourSynth Dir: %s", vapoursynthPath.toUtf8().constData());
274 for (size_t i = 0; VPS_CORE_DIR[i]; i++)
276 QFile *vpsExeFile, *vpsDllFile;
277 if (isVapourSynthComplete(QString("%1/%2").arg(vapoursynthPath, QString::fromLatin1(VPS_CORE_DIR[i])), vpsExeFile, vpsDllFile))
279 if (vpsExeFile && checkVapourSynth(vpsExeFile->fileName()))
281 success |= VPS_BIT_FLAG[i];
282 qDebug("VapourSynth %u-Bit edition found!", VPS_BITNESS(i));
283 m_vpsExePath[i].reset(vpsExeFile);
284 m_vpsDllPath[i].reset(vpsDllFile);
286 else
288 qWarning("VapourSynth %u-Bit edition was found, but version check has failed!", VPS_BITNESS(i));
291 else
293 qDebug("VapourSynth %u-Bit edition *not* found!", VPS_BITNESS(i));
297 //Return VapourSynth path
298 if(success)
300 path = vapoursynthPath;
304 bool VapourSynthCheckThread::isVapourSynthComplete(const QString &vsCorePath, QFile *&vpsExeFile, QFile *&vpsDllFile)
306 bool complete = false;
307 vpsExeFile = vpsDllFile = NULL;
309 QFileInfo vpsExeInfo(QString("%1/vspipe.exe" ).arg(vsCorePath));
310 QFileInfo vpsDllInfo(QString("%1/vapoursynth.dll").arg(vsCorePath));
312 qDebug("VapourSynth EXE: %s", vpsExeInfo.absoluteFilePath().toUtf8().constData());
313 qDebug("VapourSynth DLL: %s", vpsDllInfo.absoluteFilePath().toUtf8().constData());
315 if(vpsExeInfo.exists() && vpsDllInfo.exists())
317 vpsExeFile = new QFile(vpsExeInfo.canonicalFilePath());
318 vpsDllFile = new QFile(vpsDllInfo.canonicalFilePath());
319 if(vpsExeFile->open(QIODevice::ReadOnly) && vpsDllFile->open(QIODevice::ReadOnly))
321 complete = MUtils::OS::is_executable_file(vpsExeFile->fileName());
325 if(!complete)
327 MUTILS_DELETE(vpsExeFile);
328 MUTILS_DELETE(vpsDllFile);
331 return complete;
334 bool VapourSynthCheckThread::checkVapourSynth(const QString &vspipePath)
336 QProcess process;
337 QStringList output;
339 //Setup process object
340 process.setWorkingDirectory(QDir::tempPath());
341 process.setProcessChannelMode(QProcess::MergedChannels);
342 process.setReadChannel(QProcess::StandardOutput);
344 //Try to start VSPIPE.EXE
345 process.start(vspipePath, QStringList() << "--version");
346 if(!process.waitForStarted())
348 qWarning("Failed to launch VSPIPE.EXE -> %s", process.errorString().toUtf8().constData());
349 return false;
352 //Wait for process to finish
353 while(process.state() != QProcess::NotRunning)
355 if(process.waitForReadyRead(12000))
357 while(process.canReadLine())
359 output << QString::fromUtf8(process.readLine()).simplified();
361 continue;
363 if(process.state() != QProcess::NotRunning)
365 qWarning("VSPIPE.EXE process encountered a deadlock -> aborting now!");
366 break;
370 //Make sure VSPIPE.EXE has terminated!
371 process.waitForFinished(2500);
372 if(process.state() != QProcess::NotRunning)
374 qWarning("VSPIPE.EXE process still running, going to kill it!");
375 process.kill();
376 process.waitForFinished(-1);
379 //Read pending lines
380 while(process.canReadLine())
382 output << QString::fromUtf8(process.readLine()).simplified();
385 //Check exit code
386 if(process.exitCode() != 0)
388 qWarning("VSPIPE.EXE failed with code 0x%08X -> disable Vapousynth support!", process.exitCode());
389 return false;
392 //Init regular expressions
393 QRegExp vpsLogo("VapourSynth\\s+Video\\s+Processing\\s+Library");
395 //Check for version info
396 bool vapoursynthLogo = false;
397 for(QStringList::ConstIterator iter = output.constBegin(); iter != output.constEnd(); iter++)
399 if(vpsLogo.lastIndexIn(*iter) >= 0)
401 vapoursynthLogo = true;
402 break;
406 //Minimum required version found?
407 if(vapoursynthLogo)
409 qDebug("VapourSynth version was detected successfully.");
410 return true;
413 //Failed to determine version
414 qWarning("Failed to determine VapourSynth version!");
415 return false;