OSDN Git Service

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