OSDN Git Service

Check x264 version + added simple build script.
[x264-launcher/x264-launcher.git] / src / thread_encode.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 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 #include "global.h"
25 #include "model_options.h"
26 #include "version.h"
27
28 #include <QDate>
29 #include <QTime>
30 #include <QFileInfo>
31 #include <QDir>
32 #include <QProcess>
33 #include <QMutex>
34 #include <QLibrary>
35
36 /*
37  * Win32 API definitions
38  */
39 typedef HANDLE (WINAPI *CreateJobObjectFun)(__in_opt LPSECURITY_ATTRIBUTES lpJobAttributes, __in_opt LPCSTR lpName);
40 typedef BOOL (WINAPI *SetInformationJobObjectFun)(__in HANDLE hJob, __in JOBOBJECTINFOCLASS JobObjectInformationClass, __in_bcount(cbJobObjectInformationLength) LPVOID lpJobObjectInformation, __in DWORD cbJobObjectInformationLength);
41 typedef BOOL (WINAPI *AssignProcessToJobObjectFun)(__in HANDLE hJob, __in HANDLE hProcess);
42
43 /*
44  * Static vars
45  */
46 QMutex EncodeThread::m_mutex_startProcess;
47 HANDLE EncodeThread::m_handle_jobObject = NULL;
48
49 /*
50  * Macros
51  */
52 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) \
53 { \
54         if(ABORT_FLAG) \
55         { \
56                 log("\nPROCESS ABORTED BY USER !!!"); \
57                 setStatus(JobStatus_Aborted); \
58                 return; \
59         } \
60         else if(!(OK_FLAG)) \
61         { \
62                 setStatus(JobStatus_Failed); \
63                 return; \
64         } \
65 }
66
67 /*
68  * Static vars
69  */
70 static const unsigned int REV_MULT = 10000;
71
72 ///////////////////////////////////////////////////////////////////////////////
73 // Constructor & Destructor
74 ///////////////////////////////////////////////////////////////////////////////
75
76 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x64)
77 :
78         m_jobId(QUuid::createUuid()),
79         m_sourceFileName(sourceFileName),
80         m_outputFileName(outputFileName),
81         m_options(new OptionsModel(*options)),
82         m_binDir(binDir),
83         m_x64(x64)
84 {
85         m_abort = false;
86 }
87
88 EncodeThread::~EncodeThread(void)
89 {
90         X264_DELETE(m_options);
91 }
92
93 ///////////////////////////////////////////////////////////////////////////////
94 // Thread entry point
95 ///////////////////////////////////////////////////////////////////////////////
96
97 void EncodeThread::run(void)
98 {
99         try
100         {
101                 m_progress = 0;
102                 m_status = JobStatus_Starting;
103                 encode();
104         }
105         catch(char *msg)
106         {
107                 log(tr("EXCEPTION ERROR: ").append(QString::fromLatin1(msg)));
108         }
109         catch(...)
110         {
111                 log(tr("EXCEPTION ERROR !!!"));
112         }
113 }
114
115 ///////////////////////////////////////////////////////////////////////////////
116 // Encode functions
117 ///////////////////////////////////////////////////////////////////////////////
118
119 void EncodeThread::encode(void)
120 {
121         Sleep(500);
122
123         //Print some basic info
124         log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
125         log(tr("Source file: %1").arg(m_sourceFileName));
126         log(tr("Output file: %1").arg(m_outputFileName));
127         log(tr("\n[Encoder Options]"));
128         log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
129         log(tr("Preset: %1").arg(m_options->preset()));
130         log(tr("Tuning: %1").arg(m_options->tune()));
131         log(tr("Profile: %1").arg(m_options->profile()));
132         log(tr("Custom: %1").arg(m_options->custom().isEmpty() ? tr("(None)") : m_options->custom()));
133         
134         //Detect source info
135         log(tr("\n[Input Properties]"));
136         log(tr("Not implemented yet, sorry ;-)\n"));
137
138         bool ok = false;
139
140         //Checking version
141         log(tr("--- VERSION ---\n"));
142         unsigned int revision;
143         ok = ((revision = checkVersion(m_x64)) != UINT_MAX);
144         CHECK_STATUS(m_abort, ok);
145
146         //Is revision supported?
147         log(tr("\nx264 revision: %1 (core #%2)").arg(QString::number(revision % REV_MULT), QString::number(revision / REV_MULT)));
148         if((revision % REV_MULT) < VER_X264_MINIMUM_REV)
149         {
150                 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(VER_X264_MINIMUM_REV)));
151                 setStatus(JobStatus_Failed);
152                 return;
153         }
154         if((revision / REV_MULT) != VER_X264_CURRENT_API)
155         {
156                 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
157                 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(VER_X264_CURRENT_API)));
158         }
159
160         //Run encoding passes
161         if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
162         {
163                 QFileInfo info(m_outputFileName);
164                 QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
165
166                 if(QFileInfo(passLogFile).exists())
167                 {
168                         int n = 2;
169                         while(QFileInfo(passLogFile).exists())
170                         {
171                                 passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
172                         }
173                 }
174                 
175                 log(tr("\n--- PASS 1 ---\n"));
176                 ok = runEncodingPass(m_x64, 1, passLogFile);
177                 CHECK_STATUS(m_abort, ok);
178
179                 log(tr("\n--- PASS 2 ---\n"));
180                 ok = runEncodingPass(m_x64,2, passLogFile);
181                 CHECK_STATUS(m_abort, ok);
182         }
183         else
184         {
185                 log(tr("\n--- ENCODING ---\n"));
186                 ok = runEncodingPass(m_x64);
187                 CHECK_STATUS(m_abort, ok);
188         }
189
190         log(tr("\n--- DONE ---\n"));
191         log(tr("Job finished at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
192         setStatus(JobStatus_Completed);
193 }
194
195 bool EncodeThread::runEncodingPass(bool x64, int pass, const QString &passLogFile)
196 {
197         QProcess process;
198         QStringList cmdLine = buildCommandLine(pass, passLogFile);
199
200         log("Creating process:");
201         if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine))
202         {
203                 return false;;
204         }
205
206         QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.\\d+%\\]");
207         QRegExp regExpProgress("\\[(\\d+)\\.\\d+%\\].+frames");
208         
209         bool bTimeout = false;
210         bool bAborted = false;
211
212         while(process.state() != QProcess::NotRunning)
213         {
214                 if(m_abort)
215                 {
216                         process.kill();
217                         bAborted = true;
218                         break;
219                 }
220                 if(!process.waitForReadyRead(m_processTimeoutInterval))
221                 {
222                         if(process.state() == QProcess::Running)
223                         {
224                                 process.kill();
225                                 qWarning("x264 process timed out <-- killing!");
226                                 log("\nPROCESS TIMEOUT !!!");
227                                 bTimeout = true;
228                                 break;
229                         }
230                 }
231                 while(process.bytesAvailable() > 0)
232                 {
233                         QList<QByteArray> lines = process.readLine().split('\r');
234                         while(!lines.isEmpty())
235                         {
236                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
237                                 int offset = -1;
238                                 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
239                                 {
240                                         bool ok = false;
241                                         unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
242                                         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
243                                         setDetails(text.mid(offset).trimmed());
244                                         if(ok) setProgress(progress);
245                                 }
246                                 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
247                                 {
248                                         bool ok = false;
249                                         unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
250                                         setStatus(JobStatus_Indexing);
251                                         setDetails(text.mid(offset).trimmed());
252                                         if(ok) setProgress(progress);
253                                 }
254                                 else if(!text.isEmpty())
255                                 {
256                                         log(text);
257                                 }
258                         }
259                 }
260         }
261
262         process.waitForFinished();
263         if(process.state() != QProcess::NotRunning)
264         {
265                 process.kill();
266                 process.waitForFinished(-1);
267         }
268
269         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
270         {
271                 return false;
272         }
273         
274         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
275         setProgress(100);
276         return true;
277 }
278
279 QStringList EncodeThread::buildCommandLine(int pass, const QString &passLogFile)
280 {
281         QStringList cmdLine;
282
283         switch(m_options->rcMode())
284         {
285         case OptionsModel::RCMode_CRF:
286                 cmdLine << "--crf" << QString::number(m_options->quantizer());
287                 break;
288         case OptionsModel::RCMode_CQ:
289                 cmdLine << "--qp" << QString::number(m_options->quantizer());
290                 break;
291         case OptionsModel::RCMode_2Pass:
292         case OptionsModel::RCMode_ABR:
293                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
294                 break;
295         default:
296                 throw "Bad rate-control mode !!!";
297                 break;
298         }
299         
300         if((pass == 1) || (pass == 2))
301         {
302                 cmdLine << "--pass" << QString::number(pass);
303                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
304         }
305
306         if(m_options->tune().compare("none", Qt::CaseInsensitive))
307         {
308                 cmdLine << "--tune" << m_options->tune().toLower();
309         }
310         
311         cmdLine << "--preset" << m_options->preset().toLower();
312
313         if(!m_options->custom().isEmpty())
314         {
315                 //FIXME: Handle custom parameters that contain spaces!
316                 cmdLine.append(m_options->custom().split(" "));
317         }
318
319         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
320         cmdLine << m_sourceFileName;
321
322         return cmdLine;
323 }
324
325 unsigned int EncodeThread::checkVersion(bool x64)
326 {
327         QProcess process;
328         QStringList cmdLine = QStringList() << "--version";
329
330         log("Creating process:");
331         if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine))
332         {
333                 return false;;
334         }
335
336         QRegExp regExpVersion("x264 (\\d)\\.(\\d+)\\.(\\d+) ([0-9A-Fa-f]{7})");
337         
338         bool bTimeout = false;
339         bool bAborted = false;
340
341         unsigned int revision = UINT_MAX;
342         unsigned int coreVers = UINT_MAX;
343
344         while(process.state() != QProcess::NotRunning)
345         {
346                 if(m_abort)
347                 {
348                         process.kill();
349                         bAborted = true;
350                         break;
351                 }
352                 if(!process.waitForReadyRead(m_processTimeoutInterval))
353                 {
354                         if(process.state() == QProcess::Running)
355                         {
356                                 process.kill();
357                                 qWarning("x264 process timed out <-- killing!");
358                                 log("\nPROCESS TIMEOUT !!!");
359                                 bTimeout = true;
360                                 break;
361                         }
362                 }
363                 while(process.bytesAvailable() > 0)
364                 {
365                         QList<QByteArray> lines = process.readLine().split('\r');
366                         while(!lines.isEmpty())
367                         {
368                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
369                                 int offset = -1;
370                                 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
371                                 {
372                                         bool ok1 = false, ok2 = false;
373                                         unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
374                                         unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
375                                         if(ok1) coreVers = temp1;
376                                         if(ok2) revision = temp2;
377                                 }
378                                 if(!text.isEmpty())
379                                 {
380                                         log(text);
381                                 }
382                         }
383                 }
384         }
385
386         process.waitForFinished();
387         if(process.state() != QProcess::NotRunning)
388         {
389                 process.kill();
390                 process.waitForFinished(-1);
391         }
392
393         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
394         {
395                 return UINT_MAX;
396         }
397
398         if((revision == UINT_MAX) || (coreVers == UINT_MAX))
399         {
400                 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
401                 return UINT_MAX;
402         }
403         
404         return (coreVers * REV_MULT) + revision;
405 }
406
407 ///////////////////////////////////////////////////////////////////////////////
408 // Misc functions
409 ///////////////////////////////////////////////////////////////////////////////
410
411 void EncodeThread::setStatus(JobStatus newStatus)
412 {
413         if(m_status != newStatus)
414         {
415                 m_status = newStatus;
416                 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted))
417                 {
418                         setProgress(0);
419                 }
420                 if(newStatus == JobStatus_Failed)
421                 {
422                         setDetails("The job has failed. See log for details!");
423                 }
424                 if(newStatus == JobStatus_Aborted)
425                 {
426                         setDetails("The job was aborted by the user!");
427                 }
428                 emit statusChanged(m_jobId, newStatus);
429         }
430 }
431
432 void EncodeThread::setProgress(unsigned int newProgress)
433 {
434         if(m_progress != newProgress)
435         {
436                 m_progress = newProgress;
437                 emit progressChanged(m_jobId, m_progress);
438         }
439 }
440
441 void EncodeThread::setDetails(const QString &text)
442 {
443         emit detailsChanged(m_jobId, text);
444 }
445
446 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args)
447 {
448         static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL;
449         
450         QMutexLocker lock(&m_mutex_startProcess);
451         log(commandline2string(program, args) + "\n");
452         
453         if(!AssignProcessToJobObjectPtr)
454         {
455                 QLibrary Kernel32Lib("kernel32.dll");
456                 AssignProcessToJobObjectPtr = (AssignProcessToJobObjectFun) Kernel32Lib.resolve("AssignProcessToJobObject");
457         }
458         
459         process.setProcessChannelMode(QProcess::MergedChannels);
460         process.setReadChannel(QProcess::StandardOutput);
461         process.start(program, args);
462         
463         if(process.waitForStarted())
464         {
465                 
466                 if(AssignProcessToJobObjectPtr)
467                 {
468                         AssignProcessToJobObjectPtr(m_handle_jobObject, process.pid()->hProcess);
469                 }
470                 if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
471                 {
472                         SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
473                 }
474                 
475                 lock.unlock();
476                 return true;
477         }
478
479         log("Process creation has failed :-(");
480         QString errorMsg= process.errorString().trimmed();
481         if(!errorMsg.isEmpty()) log(errorMsg);
482
483         process.kill();
484         process.waitForFinished(-1);
485         return false;
486 }
487
488 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
489 {
490         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
491         
492         for(int i = 0; i < arguments.count(); i++)
493         {
494                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
495         }
496
497         return commandline;
498 }