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>
31 #include "3rd_party/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;
48 QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
50 connect(&thread, SIGNAL(finished()), &loop, SLOT(quit()));
51 connect(&thread, SIGNAL(terminated()), &loop, SLOT(quit()));
54 QTimer::singleShot(15000, &loop, SLOT(quit()));
56 qDebug("Avisynth thread has been created, please wait...");
57 loop.exec(QEventLoop::ExcludeUserInputEvents);
58 qDebug("Avisynth thread finished.");
60 QApplication::restoreOverrideCursor();
62 if(!thread.wait(1000))
64 qWarning("Avisynth thread encountered timeout -> probably deadlock!");
70 if(thread.getException())
72 qWarning("Avisynth thread encountered an exception !!!");
76 if(thread.getSuccess())
78 *version = thread.getVersion();
79 qDebug("Version check completed: %.2f", *version);
83 qWarning("Avisynth thread failed to determine the version!");
87 void AvisynthCheckThread::unload(void)
89 QMutexLocker lock(&m_avsLock);
93 if(m_avsLib->isLoaded())
99 X264_DELETE(m_avsLib);
102 //-------------------------------------
104 //-------------------------------------
106 AvisynthCheckThread::AvisynthCheckThread(void)
113 AvisynthCheckThread::~AvisynthCheckThread(void)
117 void AvisynthCheckThread::run(void)
119 m_exception = m_success = false;
120 m_success = detectAvisynthVersion1(&m_version, &m_exception);
123 bool AvisynthCheckThread::detectAvisynthVersion1(volatile double *version_number, volatile bool *exception)
127 return detectAvisynthVersion2(version_number, exception);
132 qWarning("Unhandled exception error in Avisynth thread !!!");
137 bool AvisynthCheckThread::detectAvisynthVersion2(volatile double *version_number, volatile bool *exception)
141 return detectAvisynthVersion3(version_number);
146 qWarning("Avisynth initializdation raised an C++ exception!");
151 bool AvisynthCheckThread::detectAvisynthVersion3(volatile double *version_number)
153 bool success = false;
154 *version_number = 0.0;
158 m_avsLib = new QLibrary("avisynth.dll");
161 if(m_avsLib->isLoaded() || m_avsLib->load())
163 avs_create_script_environment_func avs_create_script_environment_ptr = (avs_create_script_environment_func) m_avsLib->resolve("avs_create_script_environment");
164 avs_invoke_func avs_invoke_ptr = (avs_invoke_func) m_avsLib->resolve("avs_invoke");
165 avs_function_exists_func avs_function_exists_ptr = (avs_function_exists_func) m_avsLib->resolve("avs_function_exists");
166 avs_delete_script_environment_func avs_delete_script_environment_ptr = (avs_delete_script_environment_func) m_avsLib->resolve("avs_delete_script_environment");
167 avs_release_value_func avs_release_value_ptr = (avs_release_value_func) m_avsLib->resolve("avs_release_value");
169 if((avs_create_script_environment_ptr != NULL) && (avs_invoke_ptr != NULL) && (avs_function_exists_ptr != NULL))
171 qDebug("avs_create_script_environment_ptr(AVS_INTERFACE_25)");
172 AVS_ScriptEnvironment* avs_env = avs_create_script_environment_ptr(AVS_INTERFACE_25);
175 qDebug("avs_function_exists_ptr(avs_env, \"VersionNumber\")");
176 if(avs_function_exists_ptr(avs_env, "VersionNumber"))
178 qDebug("avs_invoke_ptr(avs_env, \"VersionNumber\", avs_new_value_array(NULL, 0), NULL)");
179 AVS_Value avs_version = avs_invoke_ptr(avs_env, "VersionNumber", avs_new_value_array(NULL, 0), NULL);
180 if(!avs_is_error(avs_version))
182 if(avs_is_float(avs_version))
184 qDebug("Avisynth version: v%.2f", avs_as_float(avs_version));
185 *version_number = avs_as_float(avs_version);
186 if(avs_release_value_ptr) avs_release_value_ptr(avs_version);
191 qWarning("Failed to determine version number, Avisynth didn't return a float!");
196 qWarning("Failed to determine version number, Avisynth returned an error!");
201 qWarning("The 'VersionNumber' function does not exist in your Avisynth DLL, can't determine version!");
203 if(avs_delete_script_environment_ptr != NULL)
205 avs_delete_script_environment_ptr(avs_env);
211 qWarning("The Avisynth DLL failed to create the script environment!");
216 qWarning("It seems the Avisynth DLL is missing required API functions!");
221 qWarning("Failed to load Avisynth.dll library!");