1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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_encode.h"
26 #include "model_options.h"
27 #include "model_preferences.h"
28 #include "model_sysinfo.h"
29 #include "job_object.h"
33 #include "encoder_x264.h"
34 #include "encoder_x265.h"
37 #include "source_avisynth.h"
38 #include "source_vapoursynth.h"
50 #include <QCryptographicHash>
53 * RAII execution state handler
55 class ExecutionStateHandler
58 ExecutionStateHandler(void)
60 x264_set_thread_execution_state(true);
62 ~ExecutionStateHandler(void)
64 x264_set_thread_execution_state(false);
67 //Disable copy constructor and assignment
68 ExecutionStateHandler(const ExecutionStateHandler &other) {}
69 ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
71 //Prevent object allocation on the heap
72 void *operator new(size_t); void *operator new[](size_t);
73 void operator delete(void *); void operator delete[](void*);
79 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
83 log("\nPROCESS ABORTED BY USER !!!"); \
84 setStatus(JobStatus_Aborted); \
85 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
90 setStatus(JobStatus_Failed); \
91 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
97 #define CONNECT(OBJ) do \
101 connect((OBJ), SIGNAL(statusChanged(JobStatus)), this, SLOT(setStatus(JobStatus)), Qt::DirectConnection); \
102 connect((OBJ), SIGNAL(progressChanged(unsigned int)), this, SLOT(setProgress(unsigned int)), Qt::DirectConnection); \
103 connect((OBJ), SIGNAL(detailsChanged(QString)), this, SLOT(setDetails(QString)), Qt::DirectConnection); \
104 connect((OBJ), SIGNAL(messageLogged(QString)), this, SLOT(log(QString)), Qt::DirectConnection); \
122 //static const char *VPS_TEST_FILE = "import vapoursynth as vs\ncore = vs.get_core()\nv = core.std.BlankClip()\nv.set_output()\n";
124 ///////////////////////////////////////////////////////////////////////////////
125 // Constructor & Destructor
126 ///////////////////////////////////////////////////////////////////////////////
128 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
130 m_jobId(QUuid::createUuid()),
131 m_sourceFileName(sourceFileName),
132 m_outputFileName(outputFileName),
133 m_options(new OptionsModel(*options)),
135 m_preferences(preferences),
136 m_jobObject(new JobObject),
137 m_semaphorePaused(0),
144 //Create encoder object
145 switch(options->encType())
147 case OptionsModel::EncType_X264:
148 m_encoder = new X264Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
150 case OptionsModel::EncType_X265:
151 m_encoder = new X265Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
154 throw "Unknown encoder type encountered!";
157 //Create input handler object
158 switch(getInputType(QFileInfo(m_sourceFileName).suffix()))
161 m_pipedSource = new AvisynthSource (m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
164 m_pipedSource = new VapoursynthSource(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
168 //Establish connections
170 CONNECT(m_pipedSource);
173 EncodeThread::~EncodeThread(void)
175 X264_DELETE(m_encoder);
176 X264_DELETE(m_jobObject);
177 X264_DELETE(m_options);
180 ///////////////////////////////////////////////////////////////////////////////
181 // Thread entry point
182 ///////////////////////////////////////////////////////////////////////////////
184 void EncodeThread::run(void)
193 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
201 m_jobObject->terminateJob(42);
202 X264_DELETE(m_jobObject);
206 void EncodeThread::checkedRun(void)
209 m_status = JobStatus_Starting;
215 ExecutionStateHandler executionStateHandler;
220 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
221 setStatus(JobStatus_Failed);
225 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
226 setStatus(JobStatus_Failed);
231 x264_fatal_exit(L"Unhandeled exception error in encode thread!");
235 void EncodeThread::start(Priority priority)
237 qDebug("Thread starting...");
242 while(m_semaphorePaused.tryAcquire(1, 0));
243 QThread::start(priority);
246 ///////////////////////////////////////////////////////////////////////////////
248 ///////////////////////////////////////////////////////////////////////////////
250 void EncodeThread::encode(void)
252 QDateTime startTime = QDateTime::currentDateTime();
254 // -----------------------------------------------------------------------------------
256 // -----------------------------------------------------------------------------------
258 //Print some basic info
259 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
260 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
261 log(tr("Source file : %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
262 log(tr("Output file : %1").arg(QDir::toNativeSeparators(m_outputFileName)));
265 log(tr("\n--- SYSTEMINFO ---\n"));
266 log(tr("Binary Path : %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
267 log(tr("Avisynth : %1").arg(m_sysinfo->hasAVSSupport() ? tr("Yes") : tr("No")));
268 log(tr("VapourSynth : %1").arg(m_sysinfo->hasVPSSupport() ? QDir::toNativeSeparators(m_sysinfo->getVPSPath()) : tr("N/A")));
270 //Print encoder settings
271 log(tr("\n--- SETTINGS ---\n"));
272 log(tr("Encoder : %1").arg(m_encoder->getName()));
273 log(tr("Source : %1").arg(m_pipedSource ? m_pipedSource->getName() : tr("Native")));
274 log(tr("RC Mode : %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
275 log(tr("Preset : %1").arg(m_options->preset()));
276 log(tr("Tuning : %1").arg(m_options->tune()));
277 log(tr("Profile : %1").arg(m_options->profile()));
278 log(tr("Custom : %1").arg(m_options->customEncParams().isEmpty() ? tr("(None)") : m_options->customEncParams()));
281 unsigned int frames = 0;
283 // -----------------------------------------------------------------------------------
285 // -----------------------------------------------------------------------------------
287 log(tr("\n--- CHECK VERSION ---\n"));
289 //Check encoder version
290 bool encoderModified = false;
291 const unsigned int encoderRevision = m_encoder->checkVersion(encoderModified);
292 CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
294 //Print source versions
295 m_encoder->printVersion(encoderRevision, encoderModified);
297 //Is encoder version suppoprted?
298 CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
302 //Is source type available?
303 CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
305 //Checking source version
306 bool sourceModified = false;
307 const unsigned int sourceRevision = m_pipedSource->checkVersion(sourceModified);
308 CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
310 //Print source versions
311 m_pipedSource->printVersion(sourceRevision, sourceModified);
313 //Is source version supported?
314 CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
317 // -----------------------------------------------------------------------------------
318 // Detect Source Info
319 // -----------------------------------------------------------------------------------
324 log(tr("\n--- GET SOURCE INFO ---\n"));
325 ok = m_pipedSource->checkSourceProperties(frames);
326 CHECK_STATUS(m_abort, ok);
329 // -----------------------------------------------------------------------------------
331 // -----------------------------------------------------------------------------------
333 //Run encoding passes
334 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
336 const QString passLogFile = getPasslogFile(m_outputFileName);
338 log(tr("\n--- ENCODING PASS #1 ---\n"));
339 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 1, passLogFile);
340 CHECK_STATUS(m_abort, ok);
342 log(tr("\n--- ENCODING PASS #2 ---\n"));
343 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 2, passLogFile);
344 CHECK_STATUS(m_abort, ok);
348 log(tr("\n--- ENCODING VIDEO ---\n"));
349 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames);
350 CHECK_STATUS(m_abort, ok);
353 // -----------------------------------------------------------------------------------
355 // -----------------------------------------------------------------------------------
357 log(tr("\n--- COMPLETED ---\n"));
359 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
360 log(tr("Job finished at %1, %2. Process took %3 minutes, %4 seconds.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString(Qt::ISODate), QString::number(timePassed / 60), QString::number(timePassed % 60)));
361 setStatus(JobStatus_Completed);
364 ///////////////////////////////////////////////////////////////////////////////
366 ///////////////////////////////////////////////////////////////////////////////
368 void EncodeThread::log(const QString &text)
370 emit messageLogged(m_jobId, text);
373 void EncodeThread::setStatus(const JobStatus &newStatus)
375 if(m_status != newStatus)
377 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
379 if(m_status != JobStatus_Paused) setProgress(0);
381 if(newStatus == JobStatus_Failed)
383 setDetails("The job has failed. See log for details!");
385 if(newStatus == JobStatus_Aborted)
387 setDetails("The job was aborted by the user!");
389 m_status = newStatus;
390 emit statusChanged(m_jobId, newStatus);
394 void EncodeThread::setProgress(const unsigned int &newProgress)
396 if(m_progress != newProgress)
398 m_progress = newProgress;
399 emit progressChanged(m_jobId, m_progress);
403 void EncodeThread::setDetails(const QString &text)
405 if((!text.isEmpty()) && (m_details.compare(text) != 0))
407 emit detailsChanged(m_jobId, text);
412 int EncodeThread::getInputType(const QString &fileExt)
414 int type = INPUT_NATIVE;
416 if(fileExt.compare("avs", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
417 if(fileExt.compare("avsi", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
418 if(fileExt.compare("vpy", Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
419 if(fileExt.compare("py", Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
424 QString EncodeThread::getPasslogFile(const QString &outputFile)
426 QFileInfo info(outputFile);
427 QString passLogFile = QString("%1/%2.stats").arg(info.absolutePath(), info.completeBaseName());
430 while(QFileInfo(passLogFile).exists())
432 passLogFile = QString("%1/%2_%3.stats").arg(info.absolutePath(), info.completeBaseName(), QString::number(++counter));