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_encode.h"
26 #include "model_options.h"
27 #include "model_preferences.h"
28 #include "model_sysinfo.h"
29 #include "job_object.h"
31 #include "mediainfo.h"
34 #include "encoder_factory.h"
37 #include "source_avisynth.h"
38 #include "source_vapoursynth.h"
41 #include <MUtils/OSSupport.h>
42 #include <MUtils/Version.h>
54 #include <QCryptographicHash>
57 * RAII execution state handler
59 class ExecutionStateHandler
62 ExecutionStateHandler(void)
64 x264_set_thread_execution_state(true);
66 ~ExecutionStateHandler(void)
68 x264_set_thread_execution_state(false);
71 //Disable copy constructor and assignment
72 ExecutionStateHandler(const ExecutionStateHandler &other) {}
73 ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
75 //Prevent object allocation on the heap
76 void *operator new(size_t); void *operator new[](size_t);
77 void operator delete(void *); void operator delete[](void*);
83 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
87 log("\nPROCESS ABORTED BY USER !!!"); \
88 setStatus(JobStatus_Aborted); \
89 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
94 setStatus(JobStatus_Failed); \
95 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
101 #define CONNECT(OBJ) do \
105 connect((OBJ), SIGNAL(statusChanged(JobStatus)), this, SLOT(setStatus(JobStatus)), Qt::DirectConnection); \
106 connect((OBJ), SIGNAL(progressChanged(unsigned int)), this, SLOT(setProgress(unsigned int)), Qt::DirectConnection); \
107 connect((OBJ), SIGNAL(detailsChanged(QString)), this, SLOT(setDetails(QString)), Qt::DirectConnection); \
108 connect((OBJ), SIGNAL(messageLogged(QString)), this, SLOT(log(QString)), Qt::DirectConnection); \
113 ///////////////////////////////////////////////////////////////////////////////
114 // Constructor & Destructor
115 ///////////////////////////////////////////////////////////////////////////////
117 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
119 m_jobId(QUuid::createUuid()),
120 m_sourceFileName(sourceFileName),
121 m_outputFileName(outputFileName),
122 m_options(new OptionsModel(*options)),
124 m_preferences(preferences),
125 m_jobObject(new JobObject),
126 m_semaphorePaused(0),
133 //Create encoder object
134 m_encoder = EncoderFactory::createEncoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
136 //Create input handler object
137 switch(MediaInfo::analyze(m_sourceFileName))
139 case MediaInfo::FILETYPE_AVISYNTH:
140 if(m_sysinfo->hasAVSSupport())
142 m_pipedSource = new AvisynthSource (m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
145 case MediaInfo::FILETYPE_VAPOURSYNTH:
146 if(m_sysinfo->hasVPS32Support() || m_sysinfo->hasVPS64Support())
148 m_pipedSource = new VapoursynthSource(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
153 //Establish connections
155 CONNECT(m_pipedSource);
158 EncodeThread::~EncodeThread(void)
160 MUTILS_DELETE(m_encoder);
161 MUTILS_DELETE(m_jobObject);
162 MUTILS_DELETE(m_options);
163 MUTILS_DELETE(m_pipedSource);
166 ///////////////////////////////////////////////////////////////////////////////
167 // Thread entry point
168 ///////////////////////////////////////////////////////////////////////////////
170 void EncodeThread::run(void)
179 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
187 m_jobObject->terminateJob(42);
188 MUTILS_DELETE(m_jobObject);
192 void EncodeThread::checkedRun(void)
195 m_status = JobStatus_Starting;
201 ExecutionStateHandler executionStateHandler;
204 catch(const std::exception &e)
206 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(e.what())));
207 setStatus(JobStatus_Failed);
211 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
212 setStatus(JobStatus_Failed);
216 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
217 setStatus(JobStatus_Failed);
222 MUtils::OS::fatal_exit(L"Unhandeled exception error in encode thread!");
226 void EncodeThread::start(Priority priority)
228 qDebug("Thread starting...");
233 while(m_semaphorePaused.tryAcquire(1, 0));
234 QThread::start(priority);
237 ///////////////////////////////////////////////////////////////////////////////
239 ///////////////////////////////////////////////////////////////////////////////
241 void EncodeThread::encode(void)
243 QDateTime startTime = QDateTime::currentDateTime();
245 // -----------------------------------------------------------------------------------
247 // -----------------------------------------------------------------------------------
249 //Print some basic info
250 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), MUtils::Version::app_build_date().toString(Qt::ISODate)));
251 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
252 log(tr("Source file : %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
253 log(tr("Output file : %1").arg(QDir::toNativeSeparators(m_outputFileName)));
256 log(tr("\n--- SYSTEMINFO ---\n"));
257 log(tr("Binary Path : %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
258 log(tr("Avisynth : %1").arg(m_sysinfo->hasAVSSupport() ? tr("Yes") : tr("No")));
259 log(tr("VapourSynth : %1").arg((m_sysinfo->hasVPS32Support() || m_sysinfo->hasVPS64Support()) ? tr("Yes") : tr("No")));
261 //Print encoder settings
262 log(tr("\n--- SETTINGS ---\n"));
263 log(tr("Encoder : %1").arg(m_encoder->getName()));
264 log(tr("Source : %1").arg(m_pipedSource ? m_pipedSource->getName() : tr("Native")));
265 log(tr("RC Mode : %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
266 log(tr("Preset : %1").arg(m_options->preset()));
267 log(tr("Tuning : %1").arg(m_options->tune()));
268 log(tr("Profile : %1").arg(m_options->profile()));
269 log(tr("Custom : %1").arg(m_options->customEncParams().isEmpty() ? tr("<None>") : m_options->customEncParams()));
272 unsigned int frames = 0;
274 // -----------------------------------------------------------------------------------
276 // -----------------------------------------------------------------------------------
278 log(tr("\n--- CHECK VERSION ---\n"));
280 unsigned int encoderRevision = UINT_MAX, sourceRevision = UINT_MAX;
281 bool encoderModified = false, sourceModified = false;
283 log("Detect video encoder version:\n");
285 //Check encoder version
286 encoderRevision = m_encoder->checkVersion(encoderModified);
287 CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
289 //Is encoder version suppoprted?
290 CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
294 log("\nDetect video source version:\n");
296 //Is source type available?
297 CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
299 //Checking source version
300 sourceRevision = m_pipedSource->checkVersion(sourceModified);
301 CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
303 //Is source version supported?
304 CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
307 //Print tool versions
308 log(QString("\n> %1").arg(m_encoder->printVersion(encoderRevision, encoderModified)));
311 log(QString("> %1").arg(m_pipedSource->printVersion(sourceRevision, sourceModified)));
314 // -----------------------------------------------------------------------------------
315 // Detect Source Info
316 // -----------------------------------------------------------------------------------
321 log(tr("\n--- GET SOURCE INFO ---\n"));
322 ok = m_pipedSource->checkSourceProperties(frames);
323 CHECK_STATUS(m_abort, ok);
326 // -----------------------------------------------------------------------------------
328 // -----------------------------------------------------------------------------------
330 //Run encoding passes
331 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
333 const QString passLogFile = getPasslogFile(m_outputFileName);
335 log(tr("\n--- ENCODING PASS #1 ---\n"));
336 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 1, passLogFile);
337 CHECK_STATUS(m_abort, ok);
339 log(tr("\n--- ENCODING PASS #2 ---\n"));
340 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 2, passLogFile);
341 CHECK_STATUS(m_abort, ok);
345 log(tr("\n--- ENCODING VIDEO ---\n"));
346 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames);
347 CHECK_STATUS(m_abort, ok);
350 // -----------------------------------------------------------------------------------
352 // -----------------------------------------------------------------------------------
354 log(tr("\n--- COMPLETED ---\n"));
356 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
357 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)));
358 setStatus(JobStatus_Completed);
361 ///////////////////////////////////////////////////////////////////////////////
363 ///////////////////////////////////////////////////////////////////////////////
365 void EncodeThread::log(const QString &text)
367 emit messageLogged(m_jobId, text);
370 void EncodeThread::setStatus(const JobStatus &newStatus)
372 if(m_status != newStatus)
374 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
376 if(m_status != JobStatus_Paused) setProgress(0);
378 if(newStatus == JobStatus_Failed)
380 setDetails("The job has failed. See log for details!");
382 if(newStatus == JobStatus_Aborted)
384 setDetails("The job was aborted by the user!");
386 m_status = newStatus;
387 emit statusChanged(m_jobId, newStatus);
391 void EncodeThread::setProgress(const unsigned int &newProgress)
393 if(m_progress != newProgress)
395 m_progress = newProgress;
396 emit progressChanged(m_jobId, m_progress);
400 void EncodeThread::setDetails(const QString &text)
402 if((!text.isEmpty()) && (m_details.compare(text) != 0))
404 emit detailsChanged(m_jobId, text);
409 QString EncodeThread::getPasslogFile(const QString &outputFile)
411 QFileInfo info(outputFile);
412 QString passLogFile = QString("%1/%2.stats").arg(info.absolutePath(), info.completeBaseName());
415 while(QFileInfo(passLogFile).exists())
417 passLogFile = QString("%1/%2_%3.stats").arg(info.absolutePath(), info.completeBaseName(), QString::number(++counter));