1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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_vapoursynth.h"
25 #include <MUtils/OSSupport.h>
26 #include <MUtils/Registry.h>
32 #include <QMutexLocker>
33 #include <QApplication>
39 #include "model_sysinfo.h"
45 static const bool ENABLE_PORTABLE_VPS
= true;
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
)
60 path
= QDir::fromNativeSeparators(path
);
61 while(path
.endsWith('/'))
69 //-------------------------------------
71 //-------------------------------------
73 bool VapourSynthCheckThread::detect(SysinfoModel
*sysinfo
)
75 sysinfo
->clearVapourSynth();
76 sysinfo
->clearVPSPath();
78 QMutexLocker
lock(&m_vpsLock
);
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()));
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!");
105 if(thread
.getException())
107 qWarning("VapourSynth thread encountered an exception !!!");
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
)));
120 qWarning("VapourSynth could not be found -> VapourSynth support disabled!");
126 //-------------------------------------
128 //-------------------------------------
130 VapourSynthCheckThread::VapourSynthCheckThread(void)
137 VapourSynthCheckThread::~VapourSynthCheckThread(void)
141 void VapourSynthCheckThread::run(void)
147 detectVapourSynthPath1(m_success
, m_vpsPath
, &m_exception
);
150 void VapourSynthCheckThread::detectVapourSynthPath1(int &success
, QString
&path
, volatile bool *exception
)
154 return detectVapourSynthPath2(success
, path
, exception
);
159 qWarning("Unhandled exception error in VapourSynth thread !!!");
163 void VapourSynthCheckThread::detectVapourSynthPath2(int &success
, QString
&path
, volatile bool *exception
)
167 return detectVapourSynthPath3(success
, path
);
172 qWarning("VapourSynth initializdation raised an C++ exception!");
176 void VapourSynthCheckThread::detectVapourSynthPath3(int &success
, QString
&path
)
181 static const char *VPS_CORE_DIR
[] =
187 static const int VPS_BIT_FLAG
[] =
193 static const char *VPS_REG_KEYS
[] =
195 "SOFTWARE\\VapourSynth",
196 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\VapourSynth_is1",
199 static const char *VPS_REG_NAME
[] =
203 "Inno Setup: App Path",
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
;
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
++)
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
);
248 vapoursynthPath
= temp
;
253 if (!vapoursynthPath
.isEmpty())
258 if (!vapoursynthPath
.isEmpty())
265 //Make sure VapourSynth directory does exist
266 if(vapoursynthPath
.isEmpty())
268 qWarning("VapourSynth install path not found -> disable VapouSynth support!");
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
);
288 qWarning("VapourSynth %u-Bit edition was found, but version check has failed!", VPS_BITNESS(i
));
293 qDebug("VapourSynth %u-Bit edition *not* found!", VPS_BITNESS(i
));
297 //Return VapourSynth path
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());
327 MUTILS_DELETE(vpsExeFile
);
328 MUTILS_DELETE(vpsDllFile
);
334 bool VapourSynthCheckThread::checkVapourSynth(const QString
&vspipePath
)
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());
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();
363 if(process
.state() != QProcess::NotRunning
)
365 qWarning("VSPIPE.EXE process encountered a deadlock -> aborting now!");
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!");
376 process
.waitForFinished(-1);
380 while(process
.canReadLine())
382 output
<< QString::fromUtf8(process
.readLine()).simplified();
386 if(process
.exitCode() != 0)
388 qWarning("VSPIPE.EXE failed with code 0x%08X -> disable Vapousynth support!", process
.exitCode());
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;
406 //Minimum required version found?
409 qDebug("VapourSynth version was detected successfully.");
413 //Failed to determine version
414 qWarning("Failed to determine VapourSynth version!");