OSDN Git Service

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