OSDN Git Service

More code refactoring: Got rid of the "binaries" class + added a SourceFactory class...
[x264-launcher/x264-launcher.git] / src / thread_encode.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "thread_encode.h"
23
24 //Internal
25 #include "global.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"
31
32 //Encoders
33 #include "encoder_factory.h"
34
35 //Source
36 #include "source_factory.h"
37
38 //MUtils
39 #include <MUtils/OSSupport.h>
40 #include <MUtils/Version.h>
41
42 //Qt Framework
43 #include <QDate>
44 #include <QTime>
45 #include <QDateTime>
46 #include <QFileInfo>
47 #include <QDir>
48 #include <QProcess>
49 #include <QMutex>
50 #include <QTextCodec>
51 #include <QLocale>
52 #include <QCryptographicHash>
53
54 /*
55  * RAII execution state handler
56  */
57 class ExecutionStateHandler
58 {
59 public:
60         ExecutionStateHandler(void)
61         {
62                 x264_set_thread_execution_state(true);
63         }
64         ~ExecutionStateHandler(void)
65         {
66                 x264_set_thread_execution_state(false);
67         }
68 private:
69         //Disable copy constructor and assignment
70         ExecutionStateHandler(const ExecutionStateHandler &other) {}
71         ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
72
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*);
76 };
77
78 /*
79  * Macros
80  */
81 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
82 { \
83         if(ABORT_FLAG) \
84         { \
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); \
88                 return; \
89         } \
90         else if(!(OK_FLAG)) \
91         { \
92                 setStatus(JobStatus_Failed); \
93                 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
94                 return; \
95         } \
96 } \
97 while(0)
98
99 #define CONNECT(OBJ) do \
100 { \
101         if((OBJ)) \
102         { \
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); \
107         } \
108 } \
109 while(0)
110
111 ///////////////////////////////////////////////////////////////////////////////
112 // Constructor & Destructor
113 ///////////////////////////////////////////////////////////////////////////////
114
115 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
116 :
117         m_jobId(QUuid::createUuid()),
118         m_sourceFileName(sourceFileName),
119         m_outputFileName(outputFileName),
120         m_options(new OptionsModel(*options)),
121         m_sysinfo(sysinfo),
122         m_preferences(preferences),
123         m_jobObject(new JobObject),
124         m_semaphorePaused(0),
125         m_encoder(NULL),
126         m_pipedSource(NULL)
127 {
128         m_abort = false;
129         m_pause = false;
130
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);
133
134         //Create input handler object
135         switch(MediaInfo::analyze(m_sourceFileName))
136         {
137         case MediaInfo::FILETYPE_AVISYNTH:
138                 if(m_sysinfo->hasAvisynth())
139                 {
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);
141                 }
142                 break;
143         case MediaInfo::FILETYPE_VAPOURSYNTH:
144                 if(m_sysinfo->hasVapourSynth())
145                 {
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);
147                 }
148                 break;
149         }
150
151         //Establish connections
152         CONNECT(m_encoder);
153         CONNECT(m_pipedSource);
154 }
155
156 EncodeThread::~EncodeThread(void)
157 {
158         MUTILS_DELETE(m_encoder);
159         MUTILS_DELETE(m_jobObject);
160         MUTILS_DELETE(m_options);
161         MUTILS_DELETE(m_pipedSource);
162 }
163
164 ///////////////////////////////////////////////////////////////////////////////
165 // Thread entry point
166 ///////////////////////////////////////////////////////////////////////////////
167
168 void EncodeThread::run(void)
169 {
170 #if !defined(_DEBUG)
171         __try
172         {
173                 checkedRun();
174         }
175         __except(1)
176         {
177                 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
178         }
179 #else
180         checkedRun();
181 #endif
182
183         if(m_jobObject)
184         {
185                 m_jobObject->terminateJob(42);
186                 MUTILS_DELETE(m_jobObject);
187         }
188 }
189
190 void EncodeThread::checkedRun(void)
191 {
192         m_progress = 0;
193         m_status = JobStatus_Starting;
194
195         try
196         {
197                 try
198                 {
199                         ExecutionStateHandler executionStateHandler;
200                         encode();
201                 }
202                 catch(const std::exception &e)
203                 {
204                         log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(e.what())));
205                         setStatus(JobStatus_Failed);
206                 }
207                 catch(char *msg)
208                 {
209                         log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
210                         setStatus(JobStatus_Failed);
211                 }
212                 catch(...)
213                 {
214                         log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
215                         setStatus(JobStatus_Failed);
216                 }
217         }
218         catch(...)
219         {
220                 MUtils::OS::fatal_exit(L"Unhandeled exception error in encode thread!");
221         }
222 }
223
224 void EncodeThread::start(Priority priority)
225 {
226         qDebug("Thread starting...");
227
228         m_abort = false;
229         m_pause = false;
230
231         while(m_semaphorePaused.tryAcquire(1, 0));
232         QThread::start(priority);
233 }
234
235 ///////////////////////////////////////////////////////////////////////////////
236 // Encode functions
237 ///////////////////////////////////////////////////////////////////////////////
238
239 void EncodeThread::encode(void)
240 {
241         QDateTime startTime = QDateTime::currentDateTime();
242
243         // -----------------------------------------------------------------------------------
244         // Print Information
245         // -----------------------------------------------------------------------------------
246
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)));
252         
253         //Print system info
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")));
258
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()));
268         
269         bool ok = false;
270         unsigned int frames = 0;
271         
272         // -----------------------------------------------------------------------------------
273         // Check Versions
274         // -----------------------------------------------------------------------------------
275         
276         log(tr("\n--- CHECK VERSION ---\n"));
277
278         unsigned int encoderRevision = UINT_MAX, sourceRevision = UINT_MAX;
279         bool encoderModified = false, sourceModified = false;
280
281         log("Detect video encoder version:\n");
282
283         //Check encoder version
284         encoderRevision = m_encoder->checkVersion(encoderModified);
285         CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
286
287         //Is encoder version suppoprted?
288         CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
289
290         if(m_pipedSource)
291         {
292                 log("\nDetect video source version:\n");
293
294                 //Is source type available?
295                 CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
296
297                 //Checking source version
298                 sourceRevision = m_pipedSource->checkVersion(sourceModified);
299                 CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
300
301                 //Is source version supported?
302                 CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
303         }
304
305         //Print tool versions
306         log(QString("\n> %1").arg(m_encoder->printVersion(encoderRevision, encoderModified)));
307         if(m_pipedSource)
308         {
309                 log(QString("> %1").arg(m_pipedSource->printVersion(sourceRevision, sourceModified)));
310         }
311
312         // -----------------------------------------------------------------------------------
313         // Detect Source Info
314         // -----------------------------------------------------------------------------------
315
316         //Detect source info
317         if(m_pipedSource)
318         {
319                 log(tr("\n--- GET SOURCE INFO ---\n"));
320                 ok = m_pipedSource->checkSourceProperties(frames);
321                 CHECK_STATUS(m_abort, ok);
322         }
323
324         // -----------------------------------------------------------------------------------
325         // Encoding Passes
326         // -----------------------------------------------------------------------------------
327
328         //Run encoding passes
329         if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
330         {
331                 const QString passLogFile = getPasslogFile(m_outputFileName);
332                 
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);
336
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);
340         }
341         else
342         {
343                 log(tr("\n--- ENCODING VIDEO ---\n"));
344                 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames);
345                 CHECK_STATUS(m_abort, ok);
346         }
347
348         // -----------------------------------------------------------------------------------
349         // Encoding complete
350         // -----------------------------------------------------------------------------------
351
352         log(tr("\n--- COMPLETED ---\n"));
353
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);
357 }
358
359 ///////////////////////////////////////////////////////////////////////////////
360 // Misc functions
361 ///////////////////////////////////////////////////////////////////////////////
362
363 void EncodeThread::log(const QString &text)
364 {
365         emit messageLogged(m_jobId, text);
366 }
367
368 void EncodeThread::setStatus(const JobStatus &newStatus)
369 {
370         if(m_status != newStatus)
371         {
372                 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
373                 {
374                         if(m_status != JobStatus_Paused) setProgress(0);
375                 }
376                 if(newStatus == JobStatus_Failed)
377                 {
378                         setDetails("The job has failed. See log for details!");
379                 }
380                 if(newStatus == JobStatus_Aborted)
381                 {
382                         setDetails("The job was aborted by the user!");
383                 }
384                 m_status = newStatus;
385                 emit statusChanged(m_jobId, newStatus);
386         }
387 }
388
389 void EncodeThread::setProgress(const unsigned int &newProgress)
390 {
391         if(m_progress != newProgress)
392         {
393                 m_progress = newProgress;
394                 emit progressChanged(m_jobId, m_progress);
395         }
396 }
397
398 void EncodeThread::setDetails(const QString &text)
399 {
400         if((!text.isEmpty()) && (m_details.compare(text) != 0))
401         {
402                 emit detailsChanged(m_jobId, text);
403                 m_details = text;
404         }
405 }
406
407 QString EncodeThread::getPasslogFile(const QString &outputFile)
408 {
409         QFileInfo info(outputFile);
410         QString passLogFile = QString("%1/%2.stats").arg(info.absolutePath(), info.completeBaseName());
411         int counter = 1;
412
413         while(QFileInfo(passLogFile).exists())
414         {
415                 passLogFile = QString("%1/%2_%3.stats").arg(info.absolutePath(), info.completeBaseName(), QString::number(++counter));
416         }
417
418         return passLogFile;
419 }