Bump version + updated changelog.
[simple-x264-launcher.git] / src / thread_encode.cpp
blobe3f539ed13277468c5a33f81f924aae715848b8e
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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"
24 //Internal
25 #include "global.h"
26 #include "model_options.h"
27 #include "model_preferences.h"
28 #include "model_sysinfo.h"
29 #include "model_clipInfo.h"
30 #include "job_object.h"
31 #include "mediainfo.h"
33 //Encoders
34 #include "encoder_factory.h"
36 //Source
37 #include "source_factory.h"
39 //MUtils
40 #include <MUtils/OSSupport.h>
41 #include <MUtils/Version.h>
43 //Qt Framework
44 #include <QDate>
45 #include <QTime>
46 #include <QDateTime>
47 #include <QFileInfo>
48 #include <QDir>
49 #include <QProcess>
50 #include <QMutex>
51 #include <QTextCodec>
52 #include <QLocale>
53 #include <QCryptographicHash>
56 * RAII execution state handler
58 class ExecutionStateHandler
60 public:
61 ExecutionStateHandler(void)
63 x264_set_thread_execution_state(true);
65 ~ExecutionStateHandler(void)
67 x264_set_thread_execution_state(false);
69 private:
70 //Disable copy constructor and assignment
71 ExecutionStateHandler(const ExecutionStateHandler &other) {}
72 ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
74 //Prevent object allocation on the heap
75 void *operator new(size_t); void *operator new[](size_t);
76 void operator delete(void *); void operator delete[](void*);
80 * Macros
82 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
83 { \
84 if(ABORT_FLAG) \
85 { \
86 log("\nPROCESS ABORTED BY USER !!!"); \
87 setStatus(JobStatus_Aborted); \
88 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
89 return; \
90 } \
91 else if(!(OK_FLAG)) \
92 { \
93 setStatus(JobStatus_Failed); \
94 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
95 return; \
96 } \
97 } \
98 while(0)
100 #define CONNECT(OBJ) do \
102 if((OBJ)) \
104 connect((OBJ), SIGNAL(statusChanged(JobStatus)), this, SLOT(setStatus(JobStatus)), Qt::DirectConnection); \
105 connect((OBJ), SIGNAL(progressChanged(unsigned int)), this, SLOT(setProgress(unsigned int)), Qt::DirectConnection); \
106 connect((OBJ), SIGNAL(detailsChanged(QString)), this, SLOT(setDetails(QString)), Qt::DirectConnection); \
107 connect((OBJ), SIGNAL(messageLogged(QString)), this, SLOT(log(QString)), Qt::DirectConnection); \
110 while(0)
112 ///////////////////////////////////////////////////////////////////////////////
113 // Constructor & Destructor
114 ///////////////////////////////////////////////////////////////////////////////
116 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
118 m_jobId(QUuid::createUuid()),
119 m_sourceFileName(sourceFileName),
120 m_outputFileName(outputFileName),
121 m_options(new OptionsModel(*options)),
122 m_sysinfo(sysinfo),
123 m_preferences(preferences),
124 m_jobObject(new JobObject),
125 m_semaphorePaused(0),
126 m_encoder(NULL),
127 m_pipedSource(NULL)
129 m_abort = false;
130 m_pause = false;
132 //Create encoder object
133 m_encoder = EncoderFactory::createEncoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
135 //Create input handler object
136 switch(MediaInfo::analyze(m_sourceFileName))
138 case MediaInfo::FILETYPE_AVISYNTH:
139 if(m_sysinfo->hasAvisynth())
141 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 break;
144 case MediaInfo::FILETYPE_VAPOURSYNTH:
145 if(m_sysinfo->hasVapourSynth())
147 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);
149 break;
152 //Establish connections
153 CONNECT(m_encoder);
154 CONNECT(m_pipedSource);
157 EncodeThread::~EncodeThread(void)
159 MUTILS_DELETE(m_encoder);
160 MUTILS_DELETE(m_jobObject);
161 MUTILS_DELETE(m_options);
162 MUTILS_DELETE(m_pipedSource);
165 ///////////////////////////////////////////////////////////////////////////////
166 // Thread entry point
167 ///////////////////////////////////////////////////////////////////////////////
169 void EncodeThread::run(void)
171 #if !defined(_DEBUG)
172 __try
174 checkedRun();
176 __except(1)
178 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
180 #else
181 checkedRun();
182 #endif
184 if(m_jobObject)
186 m_jobObject->terminateJob(42);
187 MUTILS_DELETE(m_jobObject);
191 void EncodeThread::checkedRun(void)
193 m_progress = 0;
194 m_status = JobStatus_Starting;
200 ExecutionStateHandler executionStateHandler;
201 encode();
203 catch(const std::exception &e)
205 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(e.what())));
206 setStatus(JobStatus_Failed);
208 catch(char *msg)
210 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
211 setStatus(JobStatus_Failed);
213 catch(...)
215 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
216 setStatus(JobStatus_Failed);
219 catch(...)
221 MUtils::OS::fatal_exit(L"Unhandeled exception error in encode thread!");
225 void EncodeThread::start(Priority priority)
227 qDebug("Thread starting...");
229 m_abort = false;
230 m_pause = false;
232 while(m_semaphorePaused.tryAcquire(1, 0));
233 QThread::start(priority);
236 ///////////////////////////////////////////////////////////////////////////////
237 // Encode functions
238 ///////////////////////////////////////////////////////////////////////////////
240 void EncodeThread::encode(void)
242 QDateTime startTime = QDateTime::currentDateTime();
244 // -----------------------------------------------------------------------------------
245 // Print Information
246 // -----------------------------------------------------------------------------------
248 //Print some basic info
249 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), MUtils::Version::app_build_date().toString(Qt::ISODate)));
250 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
251 log(tr("Source file : %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
252 log(tr("Output file : %1").arg(QDir::toNativeSeparators(m_outputFileName)));
254 //Print system info
255 log(tr("\n--- SYSTEMINFO ---\n"));
256 log(tr("Binary Path : %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
257 log(tr("Avisynth : %1").arg(m_sysinfo->hasAvisynth() ? tr("Yes") : tr("No")));
258 log(tr("VapourSynth : %1").arg(m_sysinfo->hasVapourSynth() ? tr("Yes") : tr("No")));
260 //Print encoder settings
261 log(tr("\n--- SETTINGS ---\n"));
262 log(tr("Encoder : %1").arg(m_encoder->getName()));
263 log(tr("Source : %1").arg(m_pipedSource ? m_pipedSource->getName() : tr("Native")));
264 log(tr("RC Mode : %1").arg(m_encoder->getEncoderInfo().rcModeToString(m_options->rcMode())));
265 log(tr("Preset : %1").arg(m_options->preset()));
266 log(tr("Tuning : %1").arg(m_options->tune()));
267 log(tr("Profile : %1").arg(m_options->profile()));
268 log(tr("Custom : %1").arg(m_options->customEncParams().isEmpty() ? tr("<None>") : m_options->customEncParams()));
270 bool ok = false;
271 ClipInfo clipInfo;
273 // -----------------------------------------------------------------------------------
274 // Check Versions
275 // -----------------------------------------------------------------------------------
277 log(tr("\n--- CHECK VERSION ---\n"));
279 unsigned int encoderRevision = UINT_MAX, sourceRevision = UINT_MAX;
280 bool encoderModified = false, sourceModified = false;
282 log("Detect video encoder version:\n");
284 //Check encoder version
285 encoderRevision = m_encoder->checkVersion(encoderModified);
286 CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
288 //Is encoder version suppoprted?
289 CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
291 if(m_pipedSource)
293 log("\nDetect video source version:\n");
295 //Is source type available?
296 CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
298 //Checking source version
299 sourceRevision = m_pipedSource->checkVersion(sourceModified);
300 CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
302 //Is source version supported?
303 CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
306 //Print tool versions
307 log(QString("\n> %1").arg(m_encoder->printVersion(encoderRevision, encoderModified)));
308 if(m_pipedSource)
310 log(QString("> %1").arg(m_pipedSource->printVersion(sourceRevision, sourceModified)));
313 // -----------------------------------------------------------------------------------
314 // Detect Source Info
315 // -----------------------------------------------------------------------------------
317 //Detect source info
318 if(m_pipedSource)
320 log(tr("\n--- GET SOURCE INFO ---\n"));
321 ok = m_pipedSource->checkSourceProperties(clipInfo);
322 CHECK_STATUS(m_abort, ok);
325 // -----------------------------------------------------------------------------------
326 // Encoding Passes
327 // -----------------------------------------------------------------------------------
329 //Run encoding passes
330 if(m_encoder->getEncoderInfo().rcModeToType(m_options->rcMode()) == AbstractEncoderInfo::RC_TYPE_MULTIPASS)
332 const QString passLogFile = getPasslogFile(m_outputFileName);
334 log(tr("\n--- ENCODING PASS #1 ---\n"));
335 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, clipInfo, 1, passLogFile);
336 CHECK_STATUS(m_abort, ok);
338 log(tr("\n--- ENCODING PASS #2 ---\n"));
339 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, clipInfo, 2, passLogFile);
340 CHECK_STATUS(m_abort, ok);
342 else
344 log(tr("\n--- ENCODING VIDEO ---\n"));
345 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, clipInfo);
346 CHECK_STATUS(m_abort, ok);
349 // -----------------------------------------------------------------------------------
350 // Encoding complete
351 // -----------------------------------------------------------------------------------
353 log(tr("\n--- COMPLETED ---\n"));
355 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
356 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)));
357 setStatus(JobStatus_Completed);
360 ///////////////////////////////////////////////////////////////////////////////
361 // Misc functions
362 ///////////////////////////////////////////////////////////////////////////////
364 void EncodeThread::log(const QString &text)
366 emit messageLogged(m_jobId, QDateTime::currentMSecsSinceEpoch(), text);
369 void EncodeThread::setStatus(const JobStatus &newStatus)
371 if(m_status != newStatus)
373 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
375 if(m_status != JobStatus_Paused) setProgress(0);
377 if(newStatus == JobStatus_Failed)
379 setDetails("The job has failed. See log for details!");
381 if(newStatus == JobStatus_Aborted)
383 setDetails("The job was aborted by the user!");
385 m_status = newStatus;
386 emit statusChanged(m_jobId, newStatus);
390 void EncodeThread::setProgress(const unsigned int &newProgress)
392 if(m_progress != newProgress)
394 m_progress = newProgress;
395 emit progressChanged(m_jobId, m_progress);
399 void EncodeThread::setDetails(const QString &text)
401 if((!text.isEmpty()) && (m_details.compare(text) != 0))
403 emit detailsChanged(m_jobId, text);
404 m_details = text;
408 QString EncodeThread::getPasslogFile(const QString &outputFile)
410 QFileInfo info(outputFile);
411 QString passLogFile = QString("%1/%2.stats").arg(info.absolutePath(), info.completeBaseName());
412 int counter = 1;
414 while(QFileInfo(passLogFile).exists())
416 passLogFile = QString("%1/%2_%3.stats").arg(info.absolutePath(), info.completeBaseName(), QString::number(++counter));
419 return passLogFile;