OSDN Git Service

Fixed VapourSynth input + some more code re-factoring.
[x264-launcher/x264-launcher.git] / src / thread_encode.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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
32 //Encoders
33 #include "encoder_x264.h"
34 #include "encoder_x265.h"
35
36 //Source
37 #include "source_avisynth.h"
38 #include "source_vapoursynth.h"
39
40 //Qt Framework
41 #include <QDate>
42 #include <QTime>
43 #include <QDateTime>
44 #include <QFileInfo>
45 #include <QDir>
46 #include <QProcess>
47 #include <QMutex>
48 #include <QTextCodec>
49 #include <QLocale>
50 #include <QCryptographicHash>
51
52 /*
53  * RAII execution state handler
54  */
55 class ExecutionStateHandler
56 {
57 public:
58         ExecutionStateHandler(void)
59         {
60                 x264_set_thread_execution_state(true);
61         }
62         ~ExecutionStateHandler(void)
63         {
64                 x264_set_thread_execution_state(false);
65         }
66 private:
67         //Disable copy constructor and assignment
68         ExecutionStateHandler(const ExecutionStateHandler &other) {}
69         ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
70
71         //Prevent object allocation on the heap
72         void *operator new(size_t);   void *operator new[](size_t);
73         void operator delete(void *); void operator delete[](void*);
74 };
75
76 /*
77  * Macros
78  */
79 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
80 { \
81         if(ABORT_FLAG) \
82         { \
83                 log("\nPROCESS ABORTED BY USER !!!"); \
84                 setStatus(JobStatus_Aborted); \
85                 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
86                 return; \
87         } \
88         else if(!(OK_FLAG)) \
89         { \
90                 setStatus(JobStatus_Failed); \
91                 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
92                 return; \
93         } \
94 } \
95 while(0)
96
97 #define CONNECT(OBJ) do \
98 { \
99         if((OBJ)) \
100         { \
101                 connect((OBJ), SIGNAL(statusChanged(JobStatus)),      this, SLOT(setStatus(JobStatus)),      Qt::DirectConnection); \
102                 connect((OBJ), SIGNAL(progressChanged(unsigned int)), this, SLOT(setProgress(unsigned int)), Qt::DirectConnection); \
103                 connect((OBJ), SIGNAL(detailsChanged(QString)),       this, SLOT(setDetails(QString)),       Qt::DirectConnection); \
104                 connect((OBJ), SIGNAL(messageLogged(QString)),        this, SLOT(log(QString)),              Qt::DirectConnection); \
105         } \
106 } \
107 while(0)
108
109 /*
110  * Input types
111  */
112 typedef enum
113 {
114         INPUT_NATIVE = 0,
115         INPUT_AVISYN = 1,
116         INPUT_VAPOUR = 2
117 };
118
119 /*
120  * Static vars
121  */
122 //static const char *VPS_TEST_FILE = "import vapoursynth as vs\ncore = vs.get_core()\nv = core.std.BlankClip()\nv.set_output()\n";
123
124 ///////////////////////////////////////////////////////////////////////////////
125 // Constructor & Destructor
126 ///////////////////////////////////////////////////////////////////////////////
127
128 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
129 :
130         m_jobId(QUuid::createUuid()),
131         m_sourceFileName(sourceFileName),
132         m_outputFileName(outputFileName),
133         m_options(new OptionsModel(*options)),
134         m_sysinfo(sysinfo),
135         m_preferences(preferences),
136         m_jobObject(new JobObject),
137         m_semaphorePaused(0),
138         m_encoder(NULL),
139         m_pipedSource(NULL)
140 {
141         m_abort = false;
142         m_pause = false;
143
144         //Create encoder object
145         switch(options->encType())
146         {
147         case OptionsModel::EncType_X264:
148                 m_encoder = new X264Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
149                 break;
150         case OptionsModel::EncType_X265:
151                 m_encoder = new X265Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
152                 break;
153         default:
154                 throw "Unknown encoder type encountered!";
155         }
156
157         //Create input handler object
158         switch(getInputType(QFileInfo(m_sourceFileName).suffix()))
159         {
160         case INPUT_AVISYN:
161                 m_pipedSource = new AvisynthSource   (m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
162                 break;
163         case INPUT_VAPOUR:
164                 m_pipedSource = new VapoursynthSource(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName);
165                 break;
166         }
167
168         //Establish connections
169         CONNECT(m_encoder);
170         CONNECT(m_pipedSource);
171 }
172
173 EncodeThread::~EncodeThread(void)
174 {
175         X264_DELETE(m_encoder);
176         X264_DELETE(m_jobObject);
177         X264_DELETE(m_options);
178 }
179
180 ///////////////////////////////////////////////////////////////////////////////
181 // Thread entry point
182 ///////////////////////////////////////////////////////////////////////////////
183
184 void EncodeThread::run(void)
185 {
186 #if !defined(_DEBUG)
187         __try
188         {
189                 checkedRun();
190         }
191         __except(1)
192         {
193                 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
194         }
195 #else
196         checkedRun();
197 #endif
198
199         if(m_jobObject)
200         {
201                 m_jobObject->terminateJob(42);
202                 X264_DELETE(m_jobObject);
203         }
204 }
205
206 void EncodeThread::checkedRun(void)
207 {
208         m_progress = 0;
209         m_status = JobStatus_Starting;
210
211         try
212         {
213                 try
214                 {
215                         ExecutionStateHandler executionStateHandler;
216                         encode();
217                 }
218                 catch(char *msg)
219                 {
220                         log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
221                         setStatus(JobStatus_Failed);
222                 }
223                 catch(...)
224                 {
225                         log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
226                         setStatus(JobStatus_Failed);
227                 }
228         }
229         catch(...)
230         {
231                 x264_fatal_exit(L"Unhandeled exception error in encode thread!");
232         }
233 }
234
235 void EncodeThread::start(Priority priority)
236 {
237         qDebug("Thread starting...");
238
239         m_abort = false;
240         m_pause = false;
241
242         while(m_semaphorePaused.tryAcquire(1, 0));
243         QThread::start(priority);
244 }
245
246 ///////////////////////////////////////////////////////////////////////////////
247 // Encode functions
248 ///////////////////////////////////////////////////////////////////////////////
249
250 void EncodeThread::encode(void)
251 {
252         QDateTime startTime = QDateTime::currentDateTime();
253
254         // -----------------------------------------------------------------------------------
255         // Print Information
256         // -----------------------------------------------------------------------------------
257
258         //Print some basic info
259         log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
260         log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
261         log(tr("Source file : %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
262         log(tr("Output file : %1").arg(QDir::toNativeSeparators(m_outputFileName)));
263         
264         //Print system info
265         log(tr("\n--- SYSTEMINFO ---\n"));
266         log(tr("Binary Path : %1").arg(QDir::toNativeSeparators(m_sysinfo->getAppPath())));
267         log(tr("Avisynth    : %1").arg(m_sysinfo->hasAVSSupport() ? tr("Yes") : tr("No")));
268         log(tr("VapourSynth : %1").arg(m_sysinfo->hasVPSSupport() ? QDir::toNativeSeparators(m_sysinfo->getVPSPath()) : tr("N/A")));
269
270         //Print encoder settings
271         log(tr("\n--- SETTINGS ---\n"));
272         log(tr("Encoder : %1").arg(m_encoder->getName()));
273         log(tr("Source  : %1").arg(m_pipedSource ? m_pipedSource->getName() : tr("Native")));
274         log(tr("RC Mode : %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
275         log(tr("Preset  : %1").arg(m_options->preset()));
276         log(tr("Tuning  : %1").arg(m_options->tune()));
277         log(tr("Profile : %1").arg(m_options->profile()));
278         log(tr("Custom  : %1").arg(m_options->customEncParams().isEmpty() ? tr("(None)") : m_options->customEncParams()));
279         
280         bool ok = false;
281         unsigned int frames = 0;
282         
283         // -----------------------------------------------------------------------------------
284         // Check Versions
285         // -----------------------------------------------------------------------------------
286         
287         log(tr("\n--- CHECK VERSION ---\n"));
288
289         //Check encoder version
290         bool encoderModified = false;
291         const unsigned int encoderRevision = m_encoder->checkVersion(encoderModified);
292         CHECK_STATUS(m_abort, (ok = (encoderRevision != UINT_MAX)));
293         
294         //Print source versions
295         m_encoder->printVersion(encoderRevision, encoderModified);
296
297         //Is encoder version suppoprted?
298         CHECK_STATUS(m_abort, (ok = m_encoder->isVersionSupported(encoderRevision, encoderModified)));
299
300         if(m_pipedSource)
301         {
302                 //Is source type available?
303                 CHECK_STATUS(m_abort, (ok = m_pipedSource->isSourceAvailable()));
304
305                 //Checking source version
306                 bool sourceModified = false;
307                 const unsigned int sourceRevision = m_pipedSource->checkVersion(sourceModified);
308                 CHECK_STATUS(m_abort, (ok = (sourceRevision != UINT_MAX)));
309
310                 //Print source versions
311                 m_pipedSource->printVersion(sourceRevision, sourceModified);
312
313                 //Is source version supported?
314                 CHECK_STATUS(m_abort, (ok = m_pipedSource->isVersionSupported(sourceRevision, sourceModified)));
315         }
316
317         // -----------------------------------------------------------------------------------
318         // Detect Source Info
319         // -----------------------------------------------------------------------------------
320
321         //Detect source info
322         if(m_pipedSource)
323         {
324                 log(tr("\n--- GET SOURCE INFO ---\n"));
325                 ok = m_pipedSource->checkSourceProperties(frames);
326                 CHECK_STATUS(m_abort, ok);
327         }
328
329         // -----------------------------------------------------------------------------------
330         // Encoding Passes
331         // -----------------------------------------------------------------------------------
332
333         //Run encoding passes
334         if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
335         {
336                 const QString passLogFile = getPasslogFile(m_outputFileName);
337                 
338                 log(tr("\n--- ENCODING PASS #1 ---\n"));
339                 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 1, passLogFile);
340                 CHECK_STATUS(m_abort, ok);
341
342                 log(tr("\n--- ENCODING PASS #2 ---\n"));
343                 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames, 2, passLogFile);
344                 CHECK_STATUS(m_abort, ok);
345         }
346         else
347         {
348                 log(tr("\n--- ENCODING VIDEO ---\n"));
349                 ok = m_encoder->runEncodingPass(m_pipedSource, m_outputFileName, frames);
350                 CHECK_STATUS(m_abort, ok);
351         }
352
353         // -----------------------------------------------------------------------------------
354         // Encoding complete
355         // -----------------------------------------------------------------------------------
356
357         log(tr("\n--- COMPLETED ---\n"));
358
359         int timePassed = startTime.secsTo(QDateTime::currentDateTime());
360         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)));
361         setStatus(JobStatus_Completed);
362 }
363
364 ///////////////////////////////////////////////////////////////////////////////
365 // Misc functions
366 ///////////////////////////////////////////////////////////////////////////////
367
368 void EncodeThread::log(const QString &text)
369 {
370         emit messageLogged(m_jobId, text);
371 }
372
373 void EncodeThread::setStatus(const JobStatus &newStatus)
374 {
375         if(m_status != newStatus)
376         {
377                 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
378                 {
379                         if(m_status != JobStatus_Paused) setProgress(0);
380                 }
381                 if(newStatus == JobStatus_Failed)
382                 {
383                         setDetails("The job has failed. See log for details!");
384                 }
385                 if(newStatus == JobStatus_Aborted)
386                 {
387                         setDetails("The job was aborted by the user!");
388                 }
389                 m_status = newStatus;
390                 emit statusChanged(m_jobId, newStatus);
391         }
392 }
393
394 void EncodeThread::setProgress(const unsigned int &newProgress)
395 {
396         if(m_progress != newProgress)
397         {
398                 m_progress = newProgress;
399                 emit progressChanged(m_jobId, m_progress);
400         }
401 }
402
403 void EncodeThread::setDetails(const QString &text)
404 {
405         if((!text.isEmpty()) && (m_details.compare(text) != 0))
406         {
407                 emit detailsChanged(m_jobId, text);
408                 m_details = text;
409         }
410 }
411
412 int EncodeThread::getInputType(const QString &fileExt)
413 {
414         int type = INPUT_NATIVE;
415
416         if(fileExt.compare("avs",  Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
417         if(fileExt.compare("avsi", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
418         if(fileExt.compare("vpy",  Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
419         if(fileExt.compare("py",   Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
420
421         return type;
422 }
423
424 QString EncodeThread::getPasslogFile(const QString &outputFile)
425 {
426         QFileInfo info(outputFile);
427         QString passLogFile = QString("%1/%2.stats").arg(info.absolutePath(), info.completeBaseName());
428         int counter = 1;
429
430         while(QFileInfo(passLogFile).exists())
431         {
432                 passLogFile = QString("%1/%2_%3.stats").arg(info.absolutePath(), info.completeBaseName(), QString::number(++counter));
433         }
434
435         return passLogFile;
436 }