1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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"
30 #include "mediainfo.h"
33 #include "encoder_factory.h"
36 #include "source_factory.h"
39 #include <MUtils/OSSupport.h>
40 #include <MUtils/Version.h>
52 #include <QCryptographicHash>
55 * RAII execution state handler
57 class ExecutionStateHandler
60 ExecutionStateHandler(void)
62 x264_set_thread_execution_state(true);
64 ~ExecutionStateHandler(void)
66 x264_set_thread_execution_state(false);
69 //Disable copy constructor and assignment
70 ExecutionStateHandler(const ExecutionStateHandler &other) {}
71 ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
73 //Prevent object allocation on the heap
74 void *operator new(size_t); void *operator new[](size_t);
75 void operator delete(void *); void operator delete[](void*);
81 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
85 log("\nPROCESS ABORTED BY USER !!!"); \
86 setStatus(JobStatus_Aborted); \
87 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
92 setStatus(JobStatus_Failed); \
93 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
99 #define CONNECT(OBJ) do \
103 connect((OBJ), SIGNAL(statusChanged(JobStatus)), this, SLOT(setStatus(JobStatus)), Qt::DirectConnection); \
104 connect((OBJ), SIGNAL(progressChanged(unsigned int)), this, SLOT(setProgress(unsigned int)), Qt::DirectConnection); \
105 connect((OBJ), SIGNAL(detailsChanged(QString)), this, SLOT(setDetails(QString)), Qt::DirectConnection); \
106 connect((OBJ), SIGNAL(messageLogged(QString)), this, SLOT(log(QString)), Qt::DirectConnection); \
111 ///////////////////////////////////////////////////////////////////////////////
112 // Constructor & Destructor
113 ///////////////////////////////////////////////////////////////////////////////
115 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
117 m_jobId(QUuid::createUuid()),
118 m_sourceFileName(sourceFileName),
119 m_outputFileName(outputFileName),
120 m_options(new OptionsModel(*options)),
122 m_preferences(preferences),
123 m_jobObject(new JobObject),
124 m_semaphorePaused(0),
131 //Create encoder object
132 m_encoder = EncoderFactory::createEncoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
134 //Create input handler object
135 switch(MediaInfo::analyze(m_sourceFileName))
137 case MediaInfo::FILETYPE_AVISYNTH:
138 if(m_sysinfo->hasAvisynth())
140 m_pipedSource = SourceFactory::createSource(SourceFactory::SourceType_AVS, m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
143 case MediaInfo::FILETYPE_VAPOURSYNTH:
144 if(m_sysinfo->hasVapourSynth())
146 m_pipedSource = SourceFactory::createSource(SourceFactory::SourceType_VPS, m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
151 //Establish connections
153 CONNECT(m_pipedSource);
156 EncodeThread::~EncodeThread(void)
158 MUTILS_DELETE(m_encoder);
159 MUTILS_DELETE(m_jobObject);
160 MUTILS_DELETE(m_options);
161 MUTILS_DELETE(m_pipedSource);
164 ///////////////////////////////////////////////////////////////////////////////
165 // Thread entry point
166 ///////////////////////////////////////////////////////////////////////////////
168 void EncodeThread::run(void)
177 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
185 m_jobObject->terminateJob(42);
186 MUTILS_DELETE(m_jobObject);
190 void EncodeThread::checkedRun(void)
193 m_status = JobStatus_Starting;
199 ExecutionStateHandler executionStateHandler;
202 catch(const std::exception &e)
204 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(e.what())));
205 setStatus(JobStatus_Failed);
209 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
210 setStatus(JobStatus_Failed);
214 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
215 setStatus(JobStatus_Failed);
220 MUtils::OS::fatal_exit(L"Unhandeled exception error in encode thread!");
224 void EncodeThread::start(Priority priority)
226 qDebug("Thread starting...");
231 while(m_semaphorePaused.tryAcquire(1, 0));
232 QThread::start(priority);
235 ///////////////////////////////////////////////////////////////////////////////
237 ///////////////////////////////////////////////////////////////////////////////
239 void EncodeThread::encode(void)
241 QDateTime startTime = QDateTime::currentDateTime();
243 // -----------------------------------------------------------------------------------
245 // -----------------------------------------------------------------------------------
247 //Print some basic info
248 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), MUtils::Version::app_build_date().toString(Qt::ISODate)));
249 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
250 log(tr("Source file : %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
251 log(tr("Output file : %1").arg(QDir::toNativeSeparators(m_outputFileName)));
254 log(tr("\n--- SYSTEMINFO ---\n"));
255 log(tr("Binary Path : %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
256 log(tr("Avisynth : %1").arg(m_sysinfo->hasAvisynth() ? tr("Yes") : tr("No")));
257 log(tr("VapourSynth : %1").arg(m_sysinfo->hasVapourSynth() ? tr("Yes") : tr("No")));
259 //Print encoder settings
260 log(tr("\n--- SETTINGS ---\n"));
261 log(tr("Encoder : %1").arg(m_encoder->getName()));
262 log(tr("Source : %1").arg(m_pipedSource ? m_pipedSource->getName() : tr("Native")));
263 log(tr("RC Mode : %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
264 log(tr("Preset : %1").arg(m_options->preset()));
265 log(tr("Tuning : %1").arg(m_options->tune()));
266 log(tr("Profile : %1").arg(m_options->profile()));
267 log(tr("Custom : %1").arg(m_options->customEncParams().isEmpty() ? tr("<None>") : m_options->customEncParams()));
270 unsigned int frames = 0;
272 // -----------------------------------------------------------------------------------
274 // -----------------------------------------------------------------------------------
276 log(tr("\n--- CHECK VERSION ---\n"));
278 unsigned int encoderRevision = UINT_MAX, sourceRevision = UINT_MAX;
279 bool encoderModified = false, sourceModified = false;
281 log("Detect video encoder version:\n");
283 //Check encoder version
284 encoderRevision = m_encoder->checkVersion(encoderModified);
285 CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
287 //Is encoder version suppoprted?
288 CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
292 log("\nDetect video source version:\n");
294 //Is source type available?
295 CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
297 //Checking source version
298 sourceRevision = m_pipedSource->checkVersion(sourceModified);
299 CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
301 //Is source version supported?
302 CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
305 //Print tool versions
306 log(QString("\n> %1").arg(m_encoder->printVersion(encoderRevision, encoderModified)));
309 log(QString("> %1").arg(m_pipedSource->printVersion(sourceRevision, sourceModified)));
312 // -----------------------------------------------------------------------------------
313 // Detect Source Info
314 // -----------------------------------------------------------------------------------
319 log(tr("\n--- GET SOURCE INFO ---\n"));
320 ok = m_pipedSource->checkSourceProperties(frames);
321 CHECK_STATUS(m_abort, ok);
324 // -----------------------------------------------------------------------------------
326 // -----------------------------------------------------------------------------------
328 //Run encoding passes
329 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
331 const QString passLogFile = getPasslogFile(m_outputFileName);
333 log(tr("\n--- ENCODING PASS #1 ---\n"));
334 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 1, passLogFile);
335 CHECK_STATUS(m_abort, ok);
337 log(tr("\n--- ENCODING PASS #2 ---\n"));
338 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 2, passLogFile);
339 CHECK_STATUS(m_abort, ok);
343 log(tr("\n--- ENCODING VIDEO ---\n"));
344 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames);
345 CHECK_STATUS(m_abort, ok);
348 // -----------------------------------------------------------------------------------
350 // -----------------------------------------------------------------------------------
352 log(tr("\n--- COMPLETED ---\n"));
354 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
355 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)));
356 setStatus(JobStatus_Completed);
359 ///////////////////////////////////////////////////////////////////////////////
361 ///////////////////////////////////////////////////////////////////////////////
363 void EncodeThread::log(const QString &text)
365 emit messageLogged(m_jobId, text);
368 void EncodeThread::setStatus(const JobStatus &newStatus)
370 if(m_status != newStatus)
372 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
374 if(m_status != JobStatus_Paused) setProgress(0);
376 if(newStatus == JobStatus_Failed)
378 setDetails("The job has failed. See log for details!");
380 if(newStatus == JobStatus_Aborted)
382 setDetails("The job was aborted by the user!");
384 m_status = newStatus;
385 emit statusChanged(m_jobId, newStatus);
389 void EncodeThread::setProgress(const unsigned int &newProgress)
391 if(m_progress != newProgress)
393 m_progress = newProgress;
394 emit progressChanged(m_jobId, m_progress);
398 void EncodeThread::setDetails(const QString &text)
400 if((!text.isEmpty()) && (m_details.compare(text) != 0))
402 emit detailsChanged(m_jobId, text);
407 QString EncodeThread::getPasslogFile(const QString &outputFile)
409 QFileInfo info(outputFile);
410 QString passLogFile = QString("%1/%2.stats").arg(info.absolutePath(), info.completeBaseName());
413 while(QFileInfo(passLogFile).exists())
415 passLogFile = QString("%1/%2_%3.stats").arg(info.absolutePath(), info.completeBaseName(), QString::number(++counter));