OSDN Git Service

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