OSDN Git Service

Added option to set the process priority of the encoder processes.
[x264-launcher/x264-launcher.git] / src / thread_encode.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2013 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 "win_preferences.h"
27 #include "version.h"
28
29 #include <QDate>
30 #include <QTime>
31 #include <QDateTime>
32 #include <QFileInfo>
33 #include <QDir>
34 #include <QProcess>
35 #include <QMutex>
36 #include <QTextCodec>
37 #include <QLocale>
38
39 /*
40  * Static vars
41  */
42 QMutex EncodeThread::m_mutex_startProcess;
43
44 /*
45  * Macros
46  */
47 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
48 { \
49         if(ABORT_FLAG) \
50         { \
51                 log("\nPROCESS ABORTED BY USER !!!"); \
52                 setStatus(JobStatus_Aborted); \
53                 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
54                 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
55                 return; \
56         } \
57         else if(!(OK_FLAG)) \
58         { \
59                 setStatus(JobStatus_Failed); \
60                 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
61                 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
62                 return; \
63         } \
64 } \
65 while(0)
66
67 #define APPEND_AND_CLEAR(LIST, STR) do \
68 { \
69         if(!((STR).isEmpty())) \
70         { \
71                 (LIST) << (STR); \
72                 (STR).clear(); \
73         } \
74 } \
75 while(0)
76
77 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
78 { \
79         if(ITER != LIST.end()) \
80         { \
81                 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
82                 { \
83                         log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
84                         ITER = LIST.erase(ITER); \
85                         if(ITER != LIST.end()) \
86                         { \
87                                 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
88                         } \
89                         FLAG = true; \
90                 } \
91         } \
92 } \
93 while(0)
94
95 #define AVS2_BINARY(BIN_DIR, IS_X64) QString("%1/%2/avs2yuv_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86"))
96 #define X264_BINARY(BIN_DIR, IS_10BIT, IS_X64) QString("%1/%2/x264_%3_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86"), ((IS_10BIT) ? "10bit" : "8bit"))
97
98 /*
99  * Static vars
100  */
101 static const unsigned int REV_MULT = 10000;
102
103 ///////////////////////////////////////////////////////////////////////////////
104 // Constructor & Destructor
105 ///////////////////////////////////////////////////////////////////////////////
106
107 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x264_x64, bool x264_10bit, bool avs2yuv_x64, unsigned int processPriroity)
108 :
109         m_jobId(QUuid::createUuid()),
110         m_sourceFileName(sourceFileName),
111         m_outputFileName(outputFileName),
112         m_options(new OptionsModel(*options)),
113         m_binDir(binDir),
114         m_x264_x64(x264_x64),
115         m_x264_10bit(x264_10bit),
116         m_avs2yuv_x64(avs2yuv_x64),
117         m_processPriority(processPriroity),
118         m_handle_jobObject(NULL),
119         m_semaphorePaused(0)
120 {
121         m_abort = false;
122         m_pause = false;
123 }
124
125 EncodeThread::~EncodeThread(void)
126 {
127         X264_DELETE(m_options);
128         
129         if(m_handle_jobObject)
130         {
131                 CloseHandle(m_handle_jobObject);
132                 m_handle_jobObject = NULL;
133         }
134 }
135
136 ///////////////////////////////////////////////////////////////////////////////
137 // Thread entry point
138 ///////////////////////////////////////////////////////////////////////////////
139
140 void EncodeThread::run(void)
141 {
142         __try
143         {
144                 checkedRun();
145         }
146         __except(1)
147         {
148                 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
149         }
150
151         if(m_handle_jobObject)
152         {
153                 TerminateJobObject(m_handle_jobObject, 42);
154                 m_handle_jobObject = NULL;
155         }
156 }
157
158 void EncodeThread::checkedRun(void)
159 {
160         m_progress = 0;
161         m_status = JobStatus_Starting;
162
163         try
164         {
165                 try
166                 {
167                         encode();
168                 }
169                 catch(char *msg)
170                 {
171                         log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
172                         setStatus(JobStatus_Failed);
173                 }
174                 catch(...)
175                 {
176                         log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
177                         setStatus(JobStatus_Failed);
178                 }
179         }
180         catch(...)
181         {
182                 RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 0, NULL);
183         }
184 }
185
186 void EncodeThread::start(Priority priority)
187 {
188         qDebug("Thread starting...");
189
190         m_abort = false;
191         m_pause = false;
192
193         while(m_semaphorePaused.tryAcquire(1, 0));
194         QThread::start(priority);
195 }
196
197 ///////////////////////////////////////////////////////////////////////////////
198 // Encode functions
199 ///////////////////////////////////////////////////////////////////////////////
200
201 void EncodeThread::encode(void)
202 {
203         QDateTime startTime = QDateTime::currentDateTime();
204
205         //Print some basic info
206         log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
207         log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
208         log(tr("Source file: %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
209         log(tr("Output file: %1").arg(QDir::toNativeSeparators(m_outputFileName)));
210         
211         //Print encoder settings
212         log(tr("\n--- SETTINGS ---\n"));
213         log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
214         log(tr("Preset:  %1").arg(m_options->preset()));
215         log(tr("Tuning:  %1").arg(m_options->tune()));
216         log(tr("Profile: %1").arg(m_options->profile()));
217         log(tr("Custom:  %1").arg(m_options->customX264().isEmpty() ? tr("(None)") : m_options->customX264()));
218         
219         bool ok = false;
220         unsigned int frames = 0;
221
222         //Use Avisynth?
223         const bool usePipe = (QFileInfo(m_sourceFileName).suffix().compare("avs", Qt::CaseInsensitive) == 0);
224         const QString indexFile = QString("%1/%2.ffindex").arg(QDir::tempPath(), m_jobId.toString());
225
226         //Checking x264 version
227         log(tr("\n--- CHECK VERSION ---\n"));
228         unsigned int revision_x264 = UINT_MAX;
229         bool x264_modified = false;
230         ok = ((revision_x264 = checkVersionX264(m_x264_x64, m_x264_10bit, x264_modified)) != UINT_MAX);
231         CHECK_STATUS(m_abort, ok);
232         
233         //Checking avs2yuv version
234         unsigned int revision_avs2yuv = UINT_MAX;
235         if(usePipe)
236         {
237                 ok = ((revision_avs2yuv = checkVersionAvs2yuv(m_avs2yuv_x64)) != UINT_MAX);
238                 CHECK_STATUS(m_abort, ok);
239         }
240
241         //Print versions
242         log(tr("\nx264 revision: %1 (core #%2)").arg(QString::number(revision_x264 % REV_MULT), QString::number(revision_x264 / REV_MULT)).append(x264_modified ? tr(" - with custom patches!") : QString()));
243         if(revision_avs2yuv != UINT_MAX) log(tr("Avs2YUV version: %1.%2.%3").arg(QString::number(revision_avs2yuv / REV_MULT), QString::number((revision_avs2yuv % REV_MULT) / 10),QString::number((revision_avs2yuv % REV_MULT) % 10)));
244
245         //Is x264 revision supported?
246         if((revision_x264 % REV_MULT) < (VER_X264_MINIMUM_REV))
247         {
248                 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(VER_X264_MINIMUM_REV)));
249                 setStatus(JobStatus_Failed);
250                 return;
251         }
252         if((revision_x264 / REV_MULT) != (VER_X264_CURRENT_API))
253         {
254                 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
255                 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(VER_X264_CURRENT_API)));
256         }
257         if((revision_avs2yuv != UINT_MAX) && ((revision_avs2yuv % REV_MULT) != (VER_X264_AVS2YUV_VER)))
258         {
259                 log(tr("\nERROR: Your version of avs2yuv is unsupported (Required version: v0.24 BugMaster's mod 2)"));
260                 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
261                 setStatus(JobStatus_Failed);
262                 return;
263         }
264
265         //Detect source info
266         if(usePipe)
267         {
268                 log(tr("\n--- AVS INFO ---\n"));
269                 ok = checkProperties(m_avs2yuv_x64, frames);
270                 CHECK_STATUS(m_abort, ok);
271         }
272
273         //Run encoding passes
274         if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
275         {
276                 QFileInfo info(m_outputFileName);
277                 QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
278
279                 if(QFileInfo(passLogFile).exists())
280                 {
281                         int n = 2;
282                         while(QFileInfo(passLogFile).exists())
283                         {
284                                 passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
285                         }
286                 }
287                 
288                 log(tr("\n--- PASS 1 ---\n"));
289                 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile, 1, passLogFile);
290                 CHECK_STATUS(m_abort, ok);
291
292                 log(tr("\n--- PASS 2 ---\n"));
293                 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile, 2, passLogFile);
294                 CHECK_STATUS(m_abort, ok);
295         }
296         else
297         {
298                 log(tr("\n--- ENCODING ---\n"));
299                 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile);
300                 CHECK_STATUS(m_abort, ok);
301         }
302
303         log(tr("\n--- DONE ---\n"));
304         if(QFileInfo(indexFile).exists()) QFile::remove(indexFile);
305         int timePassed = startTime.secsTo(QDateTime::currentDateTime());
306         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)));
307         setStatus(JobStatus_Completed);
308 }
309
310 bool EncodeThread::runEncodingPass(bool x264_x64, bool x264_10bit, bool avs2yuv_x64, bool usePipe, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
311 {
312         QProcess processEncode, processAvisynth;
313         
314         if(usePipe)
315         {
316                 QStringList cmdLine_Avisynth;
317                 if(!m_options->customAvs2YUV().isEmpty())
318                 {
319                         cmdLine_Avisynth.append(splitParams(m_options->customAvs2YUV()));
320                 }
321                 cmdLine_Avisynth << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
322                 cmdLine_Avisynth << "-";
323                 processAvisynth.setStandardOutputProcess(&processEncode);
324
325                 log("Creating Avisynth process:");
326                 if(!startProcess(processAvisynth, AVS2_BINARY(m_binDir, avs2yuv_x64), cmdLine_Avisynth, false))
327                 {
328                         return false;
329                 }
330         }
331
332         QStringList cmdLine_Encode = buildCommandLine(usePipe, x264_10bit, frames, indexFile, pass, passLogFile);
333
334         log("Creating x264 process:");
335         if(!startProcess(processEncode, X264_BINARY(m_binDir, x264_10bit, x264_x64), cmdLine_Encode))
336         {
337                 return false;
338         }
339
340         QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
341         QRegExp regExpProgress("\\[(\\d+)\\.(\\d+)%\\].+frames");
342         QRegExp regExpFrameCnt("^(\\d+) frames:");
343         
344         QTextCodec *localCodec = QTextCodec::codecForName("System");
345
346         bool bTimeout = false;
347         bool bAborted = false;
348
349         unsigned int last_progress = UINT_MAX;
350         unsigned int last_indexing = UINT_MAX;
351         qint64 size_estimate = 0I64;
352
353         //Main processing loop
354         while(processEncode.state() != QProcess::NotRunning)
355         {
356                 unsigned int waitCounter = 0;
357
358                 //Wait until new output is available
359                 forever
360                 {
361                         if(m_abort)
362                         {
363                                 processEncode.kill();
364                                 processAvisynth.kill();
365                                 bAborted = true;
366                                 break;
367                         }
368                         if(m_pause && (processEncode.state() == QProcess::Running))
369                         {
370                                 JobStatus previousStatus = m_status;
371                                 setStatus(JobStatus_Paused);
372                                 log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
373                                 bool ok[2] = {false, false};
374                                 Q_PID pid[2] = {processEncode.pid(), processAvisynth.pid()};
375                                 if(pid[0]) { ok[0] = (SuspendThread(pid[0]->hThread) != (DWORD)(-1)); }
376                                 if(pid[1]) { ok[1] = (SuspendThread(pid[1]->hThread) != (DWORD)(-1)); }
377                                 while(m_pause) m_semaphorePaused.tryAcquire(1, 5000);
378                                 while(m_semaphorePaused.tryAcquire(1, 0));
379                                 if(pid[0]) { if(ok[0]) ResumeThread(pid[0]->hThread); }
380                                 if(pid[1]) { if(ok[1]) ResumeThread(pid[1]->hThread); }
381                                 if(!m_abort) setStatus(previousStatus);
382                                 log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
383                                 waitCounter = 0;
384                                 continue;
385                         }
386                         if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
387                         {
388                                 if(processEncode.state() == QProcess::Running)
389                                 {
390                                         if(waitCounter++ > m_processTimeoutMaxCounter)
391                                         {
392                                                 processEncode.kill();
393                                                 qWarning("x264 process timed out <-- killing!");
394                                                 log("\nPROCESS TIMEOUT !!!");
395                                                 bTimeout = true;
396                                                 break;
397                                         }
398                                         else if(waitCounter == m_processTimeoutWarning)
399                                         {
400                                                 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
401                                                 log(tr("Warning: x264 did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
402                                         }
403                                         continue;
404                                 }
405                         }
406                         if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
407                         {
408                                 continue;
409                         }
410                         break;
411                 }
412                 
413                 //Exit main processing loop now?
414                 if(bAborted || bTimeout)
415                 {
416                         break;
417                 }
418
419                 //Process all output
420                 while(processEncode.bytesAvailable() > 0)
421                 {
422                         QList<QByteArray> lines = processEncode.readLine().split('\r');
423                         while(!lines.isEmpty())
424                         {
425                                 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
426                                 int offset = -1;
427                                 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
428                                 {
429                                         bool ok = false;
430                                         unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
431                                         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
432                                         if(ok && ((progress > last_progress) || (last_progress == UINT_MAX)))
433                                         {
434                                                 setProgress(progress);
435                                                 size_estimate = estimateSize(progress);
436                                                 last_progress = progress;
437                                         }
438                                         setDetails(tr("%1, est. file size %2").arg(text.mid(offset).trimmed(), sizeToString(size_estimate)));
439                                         last_indexing = UINT_MAX;
440                                 }
441                                 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
442                                 {
443                                         bool ok = false;
444                                         unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
445                                         setStatus(JobStatus_Indexing);
446                                         if(ok && ((progress > last_indexing) || (last_indexing == UINT_MAX)))
447                                         {
448                                                 setProgress(progress);
449                                                 last_indexing = progress;
450                                         }
451                                         setDetails(text.mid(offset).trimmed());
452                                         last_progress = UINT_MAX;
453                                 }
454                                 else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
455                                 {
456                                         last_progress = last_indexing = UINT_MAX;
457                                         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
458                                         setDetails(text.mid(offset).trimmed());
459                                 }
460                                 else if(!text.isEmpty())
461                                 {
462                                         last_progress = last_indexing = UINT_MAX;
463                                         log(text);
464                                 }
465                         }
466                 }
467         }
468
469         processEncode.waitForFinished(5000);
470         if(processEncode.state() != QProcess::NotRunning)
471         {
472                 qWarning("x264 process still running, going to kill it!");
473                 processEncode.kill();
474                 processEncode.waitForFinished(-1);
475         }
476         
477         processAvisynth.waitForFinished(5000);
478         if(processAvisynth.state() != QProcess::NotRunning)
479         {
480                 qWarning("Avisynth process still running, going to kill it!");
481                 processAvisynth.kill();
482                 processAvisynth.waitForFinished(-1);
483         }
484
485         if(!(bTimeout || bAborted))
486         {
487                 while(processAvisynth.bytesAvailable() > 0)
488                 {
489                         log(tr("av2y [info]: %1").arg(QString::fromUtf8(processAvisynth.readLine()).simplified()));
490                 }
491         }
492
493         if(usePipe && (processAvisynth.exitCode() != EXIT_SUCCESS))
494         {
495                 if(!(bTimeout || bAborted))
496                 {
497                         log(tr("\nWARNING: Avisynth process exited with error code: %1").arg(QString::number(processAvisynth.exitCode())));
498                 }
499         }
500
501         if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
502         {
503                 if(!(bTimeout || bAborted))
504                 {
505                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
506                 }
507                 processEncode.close();
508                 processAvisynth.close();
509                 return false;
510         }
511
512         QThread::yieldCurrentThread();
513
514         const qint64 finalSize = QFileInfo(m_outputFileName).size();
515         QLocale locale(QLocale::English);
516         log(tr("Final file size is %1 bytes.").arg(locale.toString(finalSize)));
517
518         switch(pass)
519         {
520         case 1:
521                 setStatus(JobStatus_Running_Pass1);
522                 setDetails(tr("First pass completed. Preparing for second pass..."));
523                 break;
524         case 2:
525                 setStatus(JobStatus_Running_Pass2);
526                 setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
527                 break;
528         default:
529                 setStatus(JobStatus_Running);
530                 setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
531                 break;
532         }
533
534         setProgress(100);
535         processEncode.close();
536         processAvisynth.close();
537         return true;
538 }
539
540 QStringList EncodeThread::buildCommandLine(bool usePipe, bool use10Bit, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
541 {
542         QStringList cmdLine;
543         double crf_int = 0.0, crf_frc = 0.0;
544
545         switch(m_options->rcMode())
546         {
547         case OptionsModel::RCMode_CQ:
548                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
549                 break;
550         case OptionsModel::RCMode_CRF:
551                 crf_frc = modf(m_options->quantizer(), &crf_int);
552                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
553                 break;
554         case OptionsModel::RCMode_2Pass:
555         case OptionsModel::RCMode_ABR:
556                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
557                 break;
558         default:
559                 throw "Bad rate-control mode !!!";
560                 break;
561         }
562         
563         if((pass == 1) || (pass == 2))
564         {
565                 cmdLine << "--pass" << QString::number(pass);
566                 cmdLine << "--stats" << pathToLocal(QDir::toNativeSeparators(passLogFile), true);
567         }
568
569         cmdLine << "--preset" << m_options->preset().toLower();
570
571         if(m_options->tune().compare("none", Qt::CaseInsensitive))
572         {
573                 cmdLine << "--tune" << m_options->tune().toLower();
574         }
575
576         if(m_options->profile().compare("auto", Qt::CaseInsensitive))
577         {
578                 if(use10Bit)
579                 {
580                         if(m_options->profile().compare("baseline", Qt::CaseInsensitive) || m_options->profile().compare("main", Qt::CaseInsensitive) || m_options->profile().compare("high", Qt::CaseInsensitive))
581                         {
582                                 log(tr("WARNING: Selected H.264 Profile not compatible with 10-Bit encoding. Ignoring!\n"));
583                         }
584                         else
585                         {
586                                 cmdLine << "--profile" << m_options->profile().toLower();
587                         }
588                 }
589                 else
590                 {
591                         cmdLine << "--profile" << m_options->profile().toLower();
592                 }
593         }
594
595         if(!m_options->customX264().isEmpty())
596         {
597                 QStringList customArgs = splitParams(m_options->customX264());
598                 if(usePipe)
599                 {
600                         QStringList::iterator i = customArgs.begin();
601                         while(i != customArgs.end())
602                         {
603                                 bool bModified = false;
604                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
605                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
606                                 if(!bModified) i++;
607                         }
608                 }
609                 cmdLine.append(customArgs);
610         }
611
612         cmdLine << "--output" << pathToLocal(QDir::toNativeSeparators(m_outputFileName), true);
613         
614         if(usePipe)
615         {
616                 if(frames < 1) throw "Frames not set!";
617                 cmdLine << "--frames" << QString::number(frames);
618                 cmdLine << "--demuxer" << "y4m";
619                 cmdLine << "--stdin" << "y4m" << "-";
620         }
621         else
622         {
623                 cmdLine << "--index" << pathToLocal(QDir::toNativeSeparators(indexFile), true, false);
624                 cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
625         }
626
627         return cmdLine;
628 }
629
630 unsigned int EncodeThread::checkVersionX264(bool use_x64, bool use_10bit, bool &modified)
631 {
632         QProcess process;
633         QStringList cmdLine = QStringList() << "--version";
634
635         log("Creating process:");
636         if(!startProcess(process, X264_BINARY(m_binDir, use_10bit, use_x64), cmdLine))
637         {
638                 return false;;
639         }
640
641         QRegExp regExpVersion("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
642         QRegExp regExpVersionMod("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
643         
644         bool bTimeout = false;
645         bool bAborted = false;
646
647         unsigned int revision = UINT_MAX;
648         unsigned int coreVers = UINT_MAX;
649         modified = false;
650
651         while(process.state() != QProcess::NotRunning)
652         {
653                 if(m_abort)
654                 {
655                         process.kill();
656                         bAborted = true;
657                         break;
658                 }
659                 if(!process.waitForReadyRead())
660                 {
661                         if(process.state() == QProcess::Running)
662                         {
663                                 process.kill();
664                                 qWarning("x264 process timed out <-- killing!");
665                                 log("\nPROCESS TIMEOUT !!!");
666                                 bTimeout = true;
667                                 break;
668                         }
669                 }
670                 while(process.bytesAvailable() > 0)
671                 {
672                         QList<QByteArray> lines = process.readLine().split('\r');
673                         while(!lines.isEmpty())
674                         {
675                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
676                                 int offset = -1;
677                                 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
678                                 {
679                                         bool ok1 = false, ok2 = false;
680                                         unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
681                                         unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
682                                         if(ok1) coreVers = temp1;
683                                         if(ok2) revision = temp2;
684                                 }
685                                 else if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
686                                 {
687                                         bool ok1 = false, ok2 = false;
688                                         unsigned int temp1 = regExpVersionMod.cap(2).toUInt(&ok1);
689                                         unsigned int temp2 = regExpVersionMod.cap(3).toUInt(&ok2);
690                                         if(ok1) coreVers = temp1;
691                                         if(ok2) revision = temp2;
692                                         modified = true;
693                                 }
694                                 if(!text.isEmpty())
695                                 {
696                                         log(text);
697                                 }
698                         }
699                 }
700         }
701
702         process.waitForFinished();
703         if(process.state() != QProcess::NotRunning)
704         {
705                 process.kill();
706                 process.waitForFinished(-1);
707         }
708
709         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
710         {
711                 if(!(bTimeout || bAborted))
712                 {
713                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
714                 }
715                 return UINT_MAX;
716         }
717
718         if((revision == UINT_MAX) || (coreVers == UINT_MAX))
719         {
720                 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
721                 return UINT_MAX;
722         }
723         
724         return (coreVers * REV_MULT) + (revision % REV_MULT);
725 }
726
727 unsigned int EncodeThread::checkVersionAvs2yuv(bool x64)
728 {
729         QProcess process;
730
731         log("\nCreating process:");
732         if(!startProcess(process, AVS2_BINARY(m_binDir, x64), QStringList()))
733         {
734                 return false;;
735         }
736
737         QRegExp regExpVersionMod("\\bAvs2YUV (\\d+).(\\d+)bm(\\d)\\b", Qt::CaseInsensitive);
738         QRegExp regExpVersionOld("\\bAvs2YUV (\\d+).(\\d+)\\b", Qt::CaseInsensitive);
739         
740         bool bTimeout = false;
741         bool bAborted = false;
742
743         unsigned int ver_maj = UINT_MAX;
744         unsigned int ver_min = UINT_MAX;
745         unsigned int ver_mod = 0;
746
747         while(process.state() != QProcess::NotRunning)
748         {
749                 if(m_abort)
750                 {
751                         process.kill();
752                         bAborted = true;
753                         break;
754                 }
755                 if(!process.waitForReadyRead())
756                 {
757                         if(process.state() == QProcess::Running)
758                         {
759                                 process.kill();
760                                 qWarning("Avs2YUV process timed out <-- killing!");
761                                 log("\nPROCESS TIMEOUT !!!");
762                                 bTimeout = true;
763                                 break;
764                         }
765                 }
766                 while(process.bytesAvailable() > 0)
767                 {
768                         QList<QByteArray> lines = process.readLine().split('\r');
769                         while(!lines.isEmpty())
770                         {
771                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
772                                 int offset = -1;
773                                 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX) || (ver_mod == UINT_MAX))
774                                 {
775                                         if(!text.isEmpty())
776                                         {
777                                                 log(text);
778                                         }
779                                 }
780                                 if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
781                                 {
782                                         bool ok1 = false, ok2 = false, ok3 = false;
783                                         unsigned int temp1 = regExpVersionMod.cap(1).toUInt(&ok1);
784                                         unsigned int temp2 = regExpVersionMod.cap(2).toUInt(&ok2);
785                                         unsigned int temp3 = regExpVersionMod.cap(3).toUInt(&ok3);
786                                         if(ok1) ver_maj = temp1;
787                                         if(ok2) ver_min = temp2;
788                                         if(ok3) ver_mod = temp3;
789                                 }
790                                 else if((offset = regExpVersionOld.lastIndexIn(text)) >= 0)
791                                 {
792                                         bool ok1 = false, ok2 = false;
793                                         unsigned int temp1 = regExpVersionOld.cap(1).toUInt(&ok1);
794                                         unsigned int temp2 = regExpVersionOld.cap(2).toUInt(&ok2);
795                                         if(ok1) ver_maj = temp1;
796                                         if(ok2) ver_min = temp2;
797                                 }
798                         }
799                 }
800         }
801
802         process.waitForFinished();
803         if(process.state() != QProcess::NotRunning)
804         {
805                 process.kill();
806                 process.waitForFinished(-1);
807         }
808
809         if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 2)))
810         {
811                 if(!(bTimeout || bAborted))
812                 {
813                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
814                 }
815                 return UINT_MAX;
816         }
817
818         if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX))
819         {
820                 log(tr("\nFAILED TO DETERMINE AVS2YUV VERSION !!!"));
821                 return UINT_MAX;
822         }
823         
824         return (ver_maj * REV_MULT) + ((ver_min % REV_MULT) * 10) + (ver_mod % 10);
825 }
826
827 bool EncodeThread::checkProperties(bool x64, unsigned int &frames)
828 {
829         QProcess process;
830         QStringList cmdLine;
831
832         if(!m_options->customAvs2YUV().isEmpty())
833         {
834                 cmdLine.append(splitParams(m_options->customAvs2YUV()));
835         }
836
837         cmdLine << "-frames" << "1";
838         cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName)) << "NUL";
839
840         log("Creating process:");
841         if(!startProcess(process, AVS2_BINARY(m_binDir, x64), cmdLine))
842         {
843                 return false;;
844         }
845
846         QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
847         QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
848         
849         QTextCodec *localCodec = QTextCodec::codecForName("System");
850
851         bool bTimeout = false;
852         bool bAborted = false;
853
854         frames = 0;
855         
856         unsigned int fpsNom = 0;
857         unsigned int fpsDen = 0;
858         unsigned int fSizeW = 0;
859         unsigned int fSizeH = 0;
860         
861         unsigned int waitCounter = 0;
862
863         while(process.state() != QProcess::NotRunning)
864         {
865                 if(m_abort)
866                 {
867                         process.kill();
868                         bAborted = true;
869                         break;
870                 }
871                 if(!process.waitForReadyRead(m_processTimeoutInterval))
872                 {
873                         if(process.state() == QProcess::Running)
874                         {
875                                 if(waitCounter++ > m_processTimeoutMaxCounter)
876                                 {
877                                         process.kill();
878                                         qWarning("Avs2YUV process timed out <-- killing!");
879                                         log("\nPROCESS TIMEOUT !!!");
880                                         log("\nAvisynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
881                                         bTimeout = true;
882                                         break;
883                                 }
884                                 else if(waitCounter == m_processTimeoutWarning)
885                                 {
886                                         unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
887                                         log(tr("Warning: Avisynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
888                                 }
889                         }
890                         continue;
891                 }
892                 
893                 waitCounter = 0;
894                 
895                 while(process.bytesAvailable() > 0)
896                 {
897                         QList<QByteArray> lines = process.readLine().split('\r');
898                         while(!lines.isEmpty())
899                         {
900                                 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
901                                 int offset = -1;
902                                 if((offset = regExpInt.lastIndexIn(text)) >= 0)
903                                 {
904                                         bool ok1 = false, ok2 = false;
905                                         bool ok3 = false, ok4 = false;
906                                         unsigned int temp1 = regExpInt.cap(1).toUInt(&ok1);
907                                         unsigned int temp2 = regExpInt.cap(2).toUInt(&ok2);
908                                         unsigned int temp3 = regExpInt.cap(3).toUInt(&ok3);
909                                         unsigned int temp4 = regExpInt.cap(4).toUInt(&ok4);
910                                         if(ok1) fSizeW = temp1;
911                                         if(ok2) fSizeH = temp2;
912                                         if(ok3) fpsNom = temp3;
913                                         if(ok4) frames = temp4;
914                                 }
915                                 else if((offset = regExpFrc.lastIndexIn(text)) >= 0)
916                                 {
917                                         bool ok1 = false, ok2 = false;
918                                         bool ok3 = false, ok4 = false, ok5 = false;
919                                         unsigned int temp1 = regExpFrc.cap(1).toUInt(&ok1);
920                                         unsigned int temp2 = regExpFrc.cap(2).toUInt(&ok2);
921                                         unsigned int temp3 = regExpFrc.cap(3).toUInt(&ok3);
922                                         unsigned int temp4 = regExpFrc.cap(4).toUInt(&ok4);
923                                         unsigned int temp5 = regExpFrc.cap(5).toUInt(&ok5);
924                                         if(ok1) fSizeW = temp1;
925                                         if(ok2) fSizeH = temp2;
926                                         if(ok3) fpsNom = temp3;
927                                         if(ok4) fpsDen = temp4;
928                                         if(ok5) frames = temp5;
929                                 }
930                                 if(!text.isEmpty())
931                                 {
932                                         log(text);
933                                 }
934                                 if(text.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
935                                 {
936                                         log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(x64 ? "64" : "32"));
937                                 }
938                                 if(text.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
939                                 {
940                                         log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
941                                 }
942                         }
943                 }
944         }
945
946         process.waitForFinished();
947         if(process.state() != QProcess::NotRunning)
948         {
949                 process.kill();
950                 process.waitForFinished(-1);
951         }
952
953         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
954         {
955                 if(!(bTimeout || bAborted))
956                 {
957                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
958                 }
959                 return false;
960         }
961
962         if(frames == 0)
963         {
964                 log(tr("\nFAILED TO DETERMINE AVS PROPERTIES !!!"));
965                 return false;
966         }
967         
968         log("");
969
970         if((fSizeW > 0) && (fSizeH > 0))
971         {
972                 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
973         }
974         if((fpsNom > 0) && (fpsDen > 0))
975         {
976                 log(tr("Frame Rate: %1/%2").arg(QString::number(fpsNom), QString::number(fpsDen)));
977         }
978         if((fpsNom > 0) && (fpsDen == 0))
979         {
980                 log(tr("Frame Rate: %1").arg(QString::number(fpsNom)));
981         }
982         if(frames > 0)
983         {
984                 log(tr("No. Frames: %1").arg(QString::number(frames)));
985         }
986
987         return true;
988 }
989
990 ///////////////////////////////////////////////////////////////////////////////
991 // Misc functions
992 ///////////////////////////////////////////////////////////////////////////////
993
994 void EncodeThread::setStatus(JobStatus newStatus)
995 {
996         if(m_status != newStatus)
997         {
998                 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
999                 {
1000                         if(m_status != JobStatus_Paused) setProgress(0);
1001                 }
1002                 if(newStatus == JobStatus_Failed)
1003                 {
1004                         setDetails("The job has failed. See log for details!");
1005                 }
1006                 if(newStatus == JobStatus_Aborted)
1007                 {
1008                         setDetails("The job was aborted by the user!");
1009                 }
1010                 m_status = newStatus;
1011                 emit statusChanged(m_jobId, newStatus);
1012         }
1013 }
1014
1015 void EncodeThread::setProgress(unsigned int newProgress)
1016 {
1017         if(m_progress != newProgress)
1018         {
1019                 m_progress = newProgress;
1020                 emit progressChanged(m_jobId, m_progress);
1021         }
1022 }
1023
1024 void EncodeThread::setDetails(const QString &text)
1025 {
1026         emit detailsChanged(m_jobId, text);
1027 }
1028
1029 QString EncodeThread::pathToLocal(const QString &longPath, bool create, bool keep)
1030 {
1031         QTextCodec *localCodec = QTextCodec::codecForName("System");
1032         
1033         //Do NOT convert to short, if path can be represented in local Codepage
1034         if(localCodec->toUnicode(localCodec->fromUnicode(longPath)).compare(longPath, Qt::CaseInsensitive) == 0)
1035         {
1036                 return longPath;
1037         }
1038         
1039         //Create dummy file, if required (only existing files can have a short path!)
1040         QFile tempFile;
1041         if((!QFileInfo(longPath).exists()) && create)
1042         {
1043                 tempFile.setFileName(longPath);
1044                 tempFile.open(QIODevice::WriteOnly);
1045         }
1046         
1047         QString shortPath;
1048         DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
1049         
1050         if(buffSize > 0)
1051         {
1052                 wchar_t *buffer = new wchar_t[buffSize];
1053                 DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
1054
1055                 if(result > 0 && result < buffSize)
1056                 {
1057                         shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer));
1058                 }
1059
1060                 delete[] buffer;
1061                 buffer = NULL;
1062         }
1063
1064         //Remove the dummy file now (FFMS2 fails, if index file does exist but is empty!)
1065         if(tempFile.isOpen())
1066         {
1067                 if(!keep) tempFile.remove();
1068                 tempFile.close();
1069         }
1070
1071         if(shortPath.isEmpty())
1072         {
1073                 log(tr("Warning: Failed to convert path \"%1\" to short!\n").arg(longPath));
1074         }
1075
1076         return (shortPath.isEmpty() ? longPath : shortPath);
1077 }
1078
1079 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
1080 {
1081         QMutexLocker lock(&m_mutex_startProcess);
1082         log(commandline2string(program, args) + "\n");
1083
1084         //Create a new job object, if not done yet
1085         if(!m_handle_jobObject)
1086         {
1087                 m_handle_jobObject = CreateJobObject(NULL, NULL);
1088                 if(m_handle_jobObject == INVALID_HANDLE_VALUE)
1089                 {
1090                         m_handle_jobObject = NULL;
1091                 }
1092                 if(m_handle_jobObject)
1093                 {
1094                         JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
1095                         memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
1096                         jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
1097                         SetInformationJobObject(m_handle_jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
1098                 }
1099         }
1100
1101         if(mergeChannels)
1102         {
1103                 process.setProcessChannelMode(QProcess::MergedChannels);
1104                 process.setReadChannel(QProcess::StandardOutput);
1105         }
1106         else
1107         {
1108                 process.setProcessChannelMode(QProcess::SeparateChannels);
1109                 process.setReadChannel(QProcess::StandardError);
1110         }
1111
1112         process.start(program, args);
1113         
1114         if(process.waitForStarted())
1115         {
1116                 Q_PID pid = process.pid();
1117                 AssignProcessToJobObject(m_handle_jobObject, process.pid()->hProcess);
1118                 if(pid != NULL)
1119                 {
1120                         switch(m_processPriority)
1121                         {
1122                         case PreferencesDialog::X264_PRIORITY_NORMAL:
1123                                 SetPriorityClass(process.pid()->hProcess, NORMAL_PRIORITY_CLASS);
1124                                 break;
1125                         case PreferencesDialog::X264_PRIORITY_BELOWNORMAL:
1126                                 if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
1127                                 {
1128                                         SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
1129                                 }
1130                                 break;
1131                         case PreferencesDialog::X264_PRIORITY_IDLE:
1132                                 SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
1133                                 break;
1134                         }
1135                 }
1136                 
1137                 lock.unlock();
1138                 return true;
1139         }
1140
1141         log("Process creation has failed :-(");
1142         QString errorMsg= process.errorString().trimmed();
1143         if(!errorMsg.isEmpty()) log(errorMsg);
1144
1145         process.kill();
1146         process.waitForFinished(-1);
1147         return false;
1148 }
1149
1150 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
1151 {
1152         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
1153         
1154         for(int i = 0; i < arguments.count(); i++)
1155         {
1156                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
1157         }
1158
1159         return commandline;
1160 }
1161
1162 QStringList EncodeThread::splitParams(const QString &params)
1163 {
1164         QStringList list; 
1165         bool ignoreWhitespaces = false;
1166         QString temp;
1167
1168         for(int i = 0; i < params.length(); i++)
1169         {
1170                 const QChar c = params.at(i);
1171
1172                 if(c == QChar::fromLatin1('"'))
1173                 {
1174                         ignoreWhitespaces = (!ignoreWhitespaces);
1175                         continue;
1176                 }
1177                 else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
1178                 {
1179                         APPEND_AND_CLEAR(list, temp);
1180                         continue;
1181                 }
1182                 
1183                 temp.append(c);
1184         }
1185         
1186         APPEND_AND_CLEAR(list, temp);
1187
1188         list.replaceInStrings("$(INPUT)", QDir::toNativeSeparators(m_sourceFileName), Qt::CaseInsensitive);
1189         list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(m_outputFileName), Qt::CaseInsensitive);
1190
1191         return list;
1192 }
1193
1194 qint64 EncodeThread::estimateSize(int progress)
1195 {
1196         if(progress >= 3)
1197         {
1198                 qint64 currentSize = QFileInfo(m_outputFileName).size();
1199                 qint64 estimatedSize = (currentSize * 100I64) / static_cast<qint64>(progress);
1200                 return estimatedSize;
1201         }
1202
1203         return 0I64;
1204 }
1205
1206 QString EncodeThread::sizeToString(qint64 size)
1207 {
1208         static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
1209
1210         if(size > 1024I64)
1211         {
1212                 qint64 estimatedSize = size;
1213                 qint64 remainderSize = 0I64;
1214
1215                 int prefixIdx = 0;
1216                 while((estimatedSize > 1024I64) && (prefixIdx < 4))
1217                 {
1218                         remainderSize = estimatedSize % 1024I64;
1219                         estimatedSize = estimatedSize / 1024I64;
1220                         prefixIdx++;
1221                 }
1222                         
1223                 double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
1224                 return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
1225         }
1226
1227         return tr("N/A");
1228 }