1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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_avisynth.h"
27 #include <QMutexLocker>
28 #include <QApplication>
32 #include "3rd_party/avisynth_c.h"
35 #include <MUtils/Global.h>
37 QMutex AvisynthCheckThread::m_avsLock;
38 QLibrary *AvisynthCheckThread::m_avsLib = NULL;
40 //-------------------------------------
42 //-------------------------------------
44 int AvisynthCheckThread::detect(volatile double *version)
47 QMutexLocker lock(&m_avsLock);
50 AvisynthCheckThread thread;
52 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
54 connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
55 connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
58 QTimer::singleShot(15000, &loop, SLOT(quit()));
60 qDebug("Avisynth thread has been created, please wait...");
61 loop.exec(QEventLoop::ExcludeUserInputEvents);
62 qDebug("Avisynth thread finished.");
64 QApplication::restoreOverrideCursor();
66 if(!thread.wait(1000))
68 qWarning("Avisynth thread encountered timeout -> probably deadlock!");
74 if(thread.getException())
76 qWarning("Avisynth thread encountered an exception !!!");
80 if(thread.getSuccess())
82 *version = thread.getVersion();
83 qDebug("Version check completed: %.2f", *version);
87 qWarning("Avisynth thread failed to determine the version!");
91 void AvisynthCheckThread::unload(void)
93 QMutexLocker lock(&m_avsLock);
97 if(m_avsLib->isLoaded())
103 MUTILS_DELETE(m_avsLib);
106 //-------------------------------------
108 //-------------------------------------
110 AvisynthCheckThread::AvisynthCheckThread(void)
117 AvisynthCheckThread::~AvisynthCheckThread(void)
121 void AvisynthCheckThread::run(void)
123 m_exception = m_success = false;
124 m_success = detectAvisynthVersion1(&m_version, &m_exception);
127 bool AvisynthCheckThread::detectAvisynthVersion1(volatile double *version_number, volatile bool *exception)
131 return detectAvisynthVersion2(version_number, exception);
136 qWarning("Unhandled exception error in Avisynth thread !!!");
141 bool AvisynthCheckThread::detectAvisynthVersion2(volatile double *version_number, volatile bool *exception)
145 return detectAvisynthVersion3(version_number);
150 qWarning("Avisynth initializdation raised an C++ exception!");
155 bool AvisynthCheckThread::detectAvisynthVersion3(volatile double *version_number)
157 bool success = false;
158 *version_number = 0.0;
162 m_avsLib = new QLibrary("avisynth.dll");
165 if(m_avsLib->isLoaded() || m_avsLib->load())
167 avs_create_script_environment_func avs_create_script_environment_ptr = (avs_create_script_environment_func) m_avsLib->resolve("avs_create_script_environment");
168 avs_invoke_func avs_invoke_ptr = (avs_invoke_func) m_avsLib->resolve("avs_invoke");
169 avs_function_exists_func avs_function_exists_ptr = (avs_function_exists_func) m_avsLib->resolve("avs_function_exists");
170 avs_delete_script_environment_func avs_delete_script_environment_ptr = (avs_delete_script_environment_func) m_avsLib->resolve("avs_delete_script_environment");
171 avs_release_value_func avs_release_value_ptr = (avs_release_value_func) m_avsLib->resolve("avs_release_value");
173 if((avs_create_script_environment_ptr != NULL) && (avs_invoke_ptr != NULL) && (avs_function_exists_ptr != NULL))
175 qDebug("avs_create_script_environment_ptr(AVS_INTERFACE_25)");
176 AVS_ScriptEnvironment* avs_env = avs_create_script_environment_ptr(AVS_INTERFACE_25);
179 qDebug("avs_function_exists_ptr(avs_env, \"VersionNumber\")");
180 if(avs_function_exists_ptr(avs_env, "VersionNumber"))
182 qDebug("avs_invoke_ptr(avs_env, \"VersionNumber\", avs_new_value_array(NULL, 0), NULL)");
183 AVS_Value avs_version = avs_invoke_ptr(avs_env, "VersionNumber", avs_new_value_array(NULL, 0), NULL);
184 if(!avs_is_error(avs_version))
186 if(avs_is_float(avs_version))
188 qDebug("Avisynth version: v%.2f", avs_as_float(avs_version));
189 *version_number = avs_as_float(avs_version);
190 if(avs_release_value_ptr) avs_release_value_ptr(avs_version);
195 qWarning("Failed to determine version number, Avisynth didn't return a float!");
200 qWarning("Failed to determine version number, Avisynth returned an error!");
205 qWarning("The 'VersionNumber' function does not exist in your Avisynth DLL, can't determine version!");
207 if(avs_delete_script_environment_ptr != NULL)
209 avs_delete_script_environment_ptr(avs_env);
215 qWarning("The Avisynth DLL failed to create the script environment!");
220 qWarning("It seems the Avisynth DLL is missing required API functions!");
225 qWarning("Failed to load Avisynth.dll library!");