1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2013 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>
31 #include "avisynth_c.h"
33 QMutex AvisynthCheckThread::m_avsLock;
34 QLibrary *AvisynthCheckThread::m_avsLib = NULL;
36 //-------------------------------------
38 //-------------------------------------
40 int AvisynthCheckThread::detect(volatile double *version)
43 QMutexLocker lock(&m_avsLock);
46 AvisynthCheckThread thread;
49 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
51 connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
52 connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
53 connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
58 qDebug("Avisynth thread has been created, please wait...");
59 loop.exec(QEventLoop::ExcludeUserInputEvents);
60 qDebug("Avisynth thread finished.");
62 QApplication::restoreOverrideCursor();
64 if(!thread.wait(1000))
66 qWarning("Avisynth thread encountered timeout -> probably deadlock!");
72 if(thread.getException())
74 qWarning("Avisynth thread encountered an exception !!!");
78 if(thread.getSuccess())
80 *version = thread.getVersion();
81 qDebug("Version check completed: %.2f", *version);
85 qWarning("Avisynth thread failed to determine the version!");
89 void AvisynthCheckThread::unload(void)
91 QMutexLocker lock(&m_avsLock);
95 if(m_avsLib->isLoaded())
101 X264_DELETE(m_avsLib);
104 //-------------------------------------
106 //-------------------------------------
108 AvisynthCheckThread::AvisynthCheckThread(void)
115 AvisynthCheckThread::~AvisynthCheckThread(void)
119 void AvisynthCheckThread::run(void)
121 m_exception = m_success = false;
122 m_success = detectAvisynthVersion1(&m_version, &m_exception);
125 bool AvisynthCheckThread::detectAvisynthVersion1(volatile double *version_number, volatile bool *exception)
129 return detectAvisynthVersion2(version_number, exception);
134 qWarning("Unhandled exception error in Avisynth thread !!!");
139 bool AvisynthCheckThread::detectAvisynthVersion2(volatile double *version_number, volatile bool *exception)
143 return detectAvisynthVersion3(version_number);
148 qWarning("Avisynth initializdation raised an C++ exception!");
153 bool AvisynthCheckThread::detectAvisynthVersion3(volatile double *version_number)
155 bool success = false;
156 *version_number = 0.0;
160 m_avsLib = new QLibrary("avisynth.dll");
163 if(m_avsLib->isLoaded() || m_avsLib->load())
165 avs_create_script_environment_func avs_create_script_environment_ptr = (avs_create_script_environment_func) m_avsLib->resolve("avs_create_script_environment");
166 avs_invoke_func avs_invoke_ptr = (avs_invoke_func) m_avsLib->resolve("avs_invoke");
167 avs_function_exists_func avs_function_exists_ptr = (avs_function_exists_func) m_avsLib->resolve("avs_function_exists");
168 avs_delete_script_environment_func avs_delete_script_environment_ptr = (avs_delete_script_environment_func) m_avsLib->resolve("avs_delete_script_environment");
169 avs_release_value_func avs_release_value_ptr = (avs_release_value_func) m_avsLib->resolve("avs_release_value");
171 if((avs_create_script_environment_ptr != NULL) && (avs_invoke_ptr != NULL) && (avs_function_exists_ptr != NULL))
173 qDebug("avs_create_script_environment_ptr(AVS_INTERFACE_25)");
174 AVS_ScriptEnvironment* avs_env = avs_create_script_environment_ptr(AVS_INTERFACE_25);
177 qDebug("avs_function_exists_ptr(avs_env, \"VersionNumber\")");
178 if(avs_function_exists_ptr(avs_env, "VersionNumber"))
180 qDebug("avs_invoke_ptr(avs_env, \"VersionNumber\", avs_new_value_array(NULL, 0), NULL)");
181 AVS_Value avs_version = avs_invoke_ptr(avs_env, "VersionNumber", avs_new_value_array(NULL, 0), NULL);
182 if(!avs_is_error(avs_version))
184 if(avs_is_float(avs_version))
186 qDebug("Avisynth version: v%.2f", avs_as_float(avs_version));
187 *version_number = avs_as_float(avs_version);
188 if(avs_release_value_ptr) avs_release_value_ptr(avs_version);
193 qWarning("Failed to determine version number, Avisynth didn't return a float!");
198 qWarning("Failed to determine version number, Avisynth returned an error!");
203 qWarning("The 'VersionNumber' function does not exist in your Avisynth DLL, can't determine version!");
205 if(avs_delete_script_environment_ptr != NULL)
207 avs_delete_script_environment_ptr(avs_env);
213 qWarning("The Avisynth DLL failed to create the script environment!");
218 qWarning("It seems the Avisynth DLL is missing required API functions!");
223 qWarning("Failed to load Avisynth.dll library!");