OSDN Git Service

Added support for Windows 7 Taskbar progress indicator + include version info in...
[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(2500))
349                         {
350                                 if(processEncode.state() == QProcess::Running)
351                                 {
352                                         if(waitCounter++ > m_processTimeoutCounter)
353                                         {
354                                                 processEncode.kill();
355                                                 qWarning("x264 process timed out <-- killing!");
356                                                 log("\nPROCESS TIMEOUT !!!");
357                                                 bTimeout = true;
358                                                 break;
359                                         }
360                                         continue;
361                                 }
362                         }
363                         if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
364                         {
365                                 continue;
366                         }
367                         break;
368                 }
369                 
370                 //Exit main processing loop now?
371                 if(bAborted || bTimeout)
372                 {
373                         break;
374                 }
375
376                 //Process all output
377                 while(processEncode.bytesAvailable() > 0)
378                 {
379                         QList<QByteArray> lines = processEncode.readLine().split('\r');
380                         while(!lines.isEmpty())
381                         {
382                                 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
383                                 int offset = -1;
384                                 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
385                                 {
386                                         bool ok = false;
387                                         unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
388                                         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
389                                         setDetails(text.mid(offset).trimmed());
390                                         if(ok) setProgress(progress);
391                                 }
392                                 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
393                                 {
394                                         bool ok = false;
395                                         unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
396                                         setStatus(JobStatus_Indexing);
397                                         setDetails(text.mid(offset).trimmed());
398                                         if(ok) setProgress(progress);
399                                 }
400                                 else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
401                                 {
402                                         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
403                                         setDetails(text.mid(offset).trimmed());
404                                 }
405                                 else if(!text.isEmpty())
406                                 {
407                                         log(text);
408                                 }
409                         }
410                 }
411         }
412
413         processEncode.waitForFinished(5000);
414         if(processEncode.state() != QProcess::NotRunning)
415         {
416                 qWarning("x264 process still running, going to kill it!");
417                 processEncode.kill();
418                 processEncode.waitForFinished(-1);
419         }
420         
421         processAvisynth.waitForFinished(5000);
422         if(processAvisynth.state() != QProcess::NotRunning)
423         {
424                 qWarning("Avisynth process still running, going to kill it!");
425                 processAvisynth.kill();
426                 processAvisynth.waitForFinished(-1);
427         }
428
429         if(!(bTimeout || bAborted))
430         {
431                 while(processAvisynth.bytesAvailable() > 0)
432                 {
433                         log(tr("av2y [info]: %1").arg(QString::fromUtf8(processAvisynth.readLine()).simplified()));
434                 }
435         }
436
437         if(usePipe && (processAvisynth.exitCode() != EXIT_SUCCESS))
438         {
439                 if(!(bTimeout || bAborted))
440                 {
441                         log(tr("\nWARNING: Avisynth process exited with error code: %1").arg(QString::number(processAvisynth.exitCode())));
442                 }
443         }
444
445         if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
446         {
447                 if(!(bTimeout || bAborted))
448                 {
449                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
450                 }
451                 processEncode.close();
452                 processAvisynth.close();
453                 return false;
454         }
455
456         switch(pass)
457         {
458         case 1:
459                 setStatus(JobStatus_Running_Pass1);
460                 setDetails(tr("First pass completed. Preparing for second pass..."));
461                 break;
462         case 2:
463                 setStatus(JobStatus_Running_Pass2);
464                 setDetails(tr("Second pass completed successfully."));
465                 break;
466         default:
467                 setStatus(JobStatus_Running);
468                 setDetails(tr("Encode completed successfully."));
469                 break;
470         }
471
472         setProgress(100);
473         processEncode.close();
474         processAvisynth.close();
475         return true;
476 }
477
478 QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
479 {
480         QStringList cmdLine;
481         double crf_int = 0.0, crf_frc = 0.0;
482
483         switch(m_options->rcMode())
484         {
485         case OptionsModel::RCMode_CQ:
486                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
487                 break;
488         case OptionsModel::RCMode_CRF:
489                 crf_frc = modf(m_options->quantizer(), &crf_int);
490                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
491                 break;
492         case OptionsModel::RCMode_2Pass:
493         case OptionsModel::RCMode_ABR:
494                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
495                 break;
496         default:
497                 throw "Bad rate-control mode !!!";
498                 break;
499         }
500         
501         if((pass == 1) || (pass == 2))
502         {
503                 cmdLine << "--pass" << QString::number(pass);
504                 cmdLine << "--stats" << pathToLocal(QDir::toNativeSeparators(passLogFile), true);
505         }
506
507         cmdLine << "--preset" << m_options->preset().toLower();
508
509         if(m_options->tune().compare("none", Qt::CaseInsensitive))
510         {
511                 cmdLine << "--tune" << m_options->tune().toLower();
512         }
513
514         if(m_options->profile().compare("auto", Qt::CaseInsensitive))
515         {
516                 cmdLine << "--profile" << m_options->profile().toLower();
517         }
518
519         if(!m_options->custom().isEmpty())
520         {
521                 cmdLine.append(splitParams(m_options->custom()));
522         }
523
524         cmdLine << "--output" << pathToLocal(QDir::toNativeSeparators(m_outputFileName), true);
525         
526         if(usePipe)
527         {
528                 if(frames < 1) throw "Frames not set!";
529                 cmdLine << "--frames" << QString::number(frames);
530                 cmdLine << "--demuxer" << "y4m";
531                 cmdLine << "--stdin" << "y4m" << "-";
532         }
533         else
534         {
535                 cmdLine << "--index" << pathToLocal(QDir::toNativeSeparators(indexFile), true, false);
536                 cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
537         }
538
539         return cmdLine;
540 }
541
542 unsigned int EncodeThread::checkVersionX264(bool x64, bool &modified)
543 {
544         QProcess process;
545         QStringList cmdLine = QStringList() << "--version";
546
547         log("Creating process:");
548         if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine))
549         {
550                 return false;;
551         }
552
553         QRegExp regExpVersion("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
554         QRegExp regExpVersionMod("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
555         
556         bool bTimeout = false;
557         bool bAborted = false;
558
559         unsigned int revision = UINT_MAX;
560         unsigned int coreVers = UINT_MAX;
561         modified = false;
562
563         while(process.state() != QProcess::NotRunning)
564         {
565                 if(m_abort)
566                 {
567                         process.kill();
568                         bAborted = true;
569                         break;
570                 }
571                 if(!process.waitForReadyRead())
572                 {
573                         if(process.state() == QProcess::Running)
574                         {
575                                 process.kill();
576                                 qWarning("x264 process timed out <-- killing!");
577                                 log("\nPROCESS TIMEOUT !!!");
578                                 bTimeout = true;
579                                 break;
580                         }
581                 }
582                 while(process.bytesAvailable() > 0)
583                 {
584                         QList<QByteArray> lines = process.readLine().split('\r');
585                         while(!lines.isEmpty())
586                         {
587                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
588                                 int offset = -1;
589                                 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
590                                 {
591                                         bool ok1 = false, ok2 = false;
592                                         unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
593                                         unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
594                                         if(ok1) coreVers = temp1;
595                                         if(ok2) revision = temp2;
596                                 }
597                                 else if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
598                                 {
599                                         bool ok1 = false, ok2 = false;
600                                         unsigned int temp1 = regExpVersionMod.cap(2).toUInt(&ok1);
601                                         unsigned int temp2 = regExpVersionMod.cap(3).toUInt(&ok2);
602                                         if(ok1) coreVers = temp1;
603                                         if(ok2) revision = temp2;
604                                         modified = true;
605                                 }
606                                 if(!text.isEmpty())
607                                 {
608                                         log(text);
609                                 }
610                         }
611                 }
612         }
613
614         process.waitForFinished();
615         if(process.state() != QProcess::NotRunning)
616         {
617                 process.kill();
618                 process.waitForFinished(-1);
619         }
620
621         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
622         {
623                 if(!(bTimeout || bAborted))
624                 {
625                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
626                 }
627                 return UINT_MAX;
628         }
629
630         if((revision == UINT_MAX) || (coreVers == UINT_MAX))
631         {
632                 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
633                 return UINT_MAX;
634         }
635         
636         return (coreVers * REV_MULT) + (revision % REV_MULT);
637 }
638
639 unsigned int EncodeThread::checkVersionAvs2yuv(bool x64)
640 {
641         QProcess process;
642
643         log("\nCreating process:");
644         if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "avs2yuv_x64" : "avs2yuv"), QStringList()))
645         {
646                 return false;;
647         }
648
649         QRegExp regExpVersionMod("\\bAvs2YUV (\\d+).(\\d+)bm(\\d)\\b", Qt::CaseInsensitive);
650         QRegExp regExpVersionOld("\\bAvs2YUV (\\d+).(\\d+)\\b", Qt::CaseInsensitive);
651         
652         bool bTimeout = false;
653         bool bAborted = false;
654
655         unsigned int ver_maj = UINT_MAX;
656         unsigned int ver_min = UINT_MAX;
657         unsigned int ver_mod = 0;
658
659         while(process.state() != QProcess::NotRunning)
660         {
661                 if(m_abort)
662                 {
663                         process.kill();
664                         bAborted = true;
665                         break;
666                 }
667                 if(!process.waitForReadyRead())
668                 {
669                         if(process.state() == QProcess::Running)
670                         {
671                                 process.kill();
672                                 qWarning("Avs2YUV process timed out <-- killing!");
673                                 log("\nPROCESS TIMEOUT !!!");
674                                 bTimeout = true;
675                                 break;
676                         }
677                 }
678                 while(process.bytesAvailable() > 0)
679                 {
680                         QList<QByteArray> lines = process.readLine().split('\r');
681                         while(!lines.isEmpty())
682                         {
683                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
684                                 int offset = -1;
685                                 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX) || (ver_mod == UINT_MAX))
686                                 {
687                                         if(!text.isEmpty())
688                                         {
689                                                 log(text);
690                                         }
691                                 }
692                                 if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
693                                 {
694                                         bool ok1 = false, ok2 = false, ok3 = false;
695                                         unsigned int temp1 = regExpVersionMod.cap(1).toUInt(&ok1);
696                                         unsigned int temp2 = regExpVersionMod.cap(2).toUInt(&ok2);
697                                         unsigned int temp3 = regExpVersionMod.cap(3).toUInt(&ok3);
698                                         if(ok1) ver_maj = temp1;
699                                         if(ok2) ver_min = temp2;
700                                         if(ok3) ver_mod = temp3;
701                                 }
702                                 else if((offset = regExpVersionOld.lastIndexIn(text)) >= 0)
703                                 {
704                                         bool ok1 = false, ok2 = false;
705                                         unsigned int temp1 = regExpVersionOld.cap(1).toUInt(&ok1);
706                                         unsigned int temp2 = regExpVersionOld.cap(2).toUInt(&ok2);
707                                         if(ok1) ver_maj = temp1;
708                                         if(ok2) ver_min = temp2;
709                                 }
710                         }
711                 }
712         }
713
714         process.waitForFinished();
715         if(process.state() != QProcess::NotRunning)
716         {
717                 process.kill();
718                 process.waitForFinished(-1);
719         }
720
721         if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 2)))
722         {
723                 if(!(bTimeout || bAborted))
724                 {
725                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
726                 }
727                 return UINT_MAX;
728         }
729
730         if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX))
731         {
732                 log(tr("\nFAILED TO DETERMINE AVS2YUV VERSION !!!"));
733                 return UINT_MAX;
734         }
735         
736         return (ver_maj * REV_MULT) + ((ver_min % REV_MULT) * 10) + (ver_mod % 10);
737 }
738
739 bool EncodeThread::checkProperties(bool x64, unsigned int &frames)
740 {
741         QProcess process;
742         
743         QStringList cmdLine = QStringList() << "-frames" << "1";
744         cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName)) << "NUL";
745
746         log("Creating process:");
747         if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "avs2yuv_x64" : "avs2yuv"), cmdLine))
748         {
749                 return false;;
750         }
751
752         QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
753         QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
754         
755         QTextCodec *localCodec = QTextCodec::codecForName("System");
756
757         bool bTimeout = false;
758         bool bAborted = false;
759
760         frames = 0;
761         
762         unsigned int fpsNom = 0;
763         unsigned int fpsDen = 0;
764         unsigned int fSizeW = 0;
765         unsigned int fSizeH = 0;
766         
767         while(process.state() != QProcess::NotRunning)
768         {
769                 if(m_abort)
770                 {
771                         process.kill();
772                         bAborted = true;
773                         break;
774                 }
775                 if(!process.waitForReadyRead())
776                 {
777                         if(process.state() == QProcess::Running)
778                         {
779                                 process.kill();
780                                 qWarning("Avs2YUV process timed out <-- killing!");
781                                 log("\nPROCESS TIMEOUT !!!");
782                                 bTimeout = true;
783                                 break;
784                         }
785                 }
786                 while(process.bytesAvailable() > 0)
787                 {
788                         QList<QByteArray> lines = process.readLine().split('\r');
789                         while(!lines.isEmpty())
790                         {
791                                 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
792                                 int offset = -1;
793                                 if((offset = regExpInt.lastIndexIn(text)) >= 0)
794                                 {
795                                         bool ok1 = false, ok2 = false;
796                                         bool ok3 = false, ok4 = false;
797                                         unsigned int temp1 = regExpInt.cap(1).toUInt(&ok1);
798                                         unsigned int temp2 = regExpInt.cap(2).toUInt(&ok2);
799                                         unsigned int temp3 = regExpInt.cap(3).toUInt(&ok3);
800                                         unsigned int temp4 = regExpInt.cap(4).toUInt(&ok4);
801                                         if(ok1) fSizeW = temp1;
802                                         if(ok2) fSizeH = temp2;
803                                         if(ok3) fpsNom = temp3;
804                                         if(ok4) frames = temp4;
805                                 }
806                                 else if((offset = regExpFrc.lastIndexIn(text)) >= 0)
807                                 {
808                                         bool ok1 = false, ok2 = false;
809                                         bool ok3 = false, ok4 = false, ok5 = false;
810                                         unsigned int temp1 = regExpFrc.cap(1).toUInt(&ok1);
811                                         unsigned int temp2 = regExpFrc.cap(2).toUInt(&ok2);
812                                         unsigned int temp3 = regExpFrc.cap(3).toUInt(&ok3);
813                                         unsigned int temp4 = regExpFrc.cap(4).toUInt(&ok4);
814                                         unsigned int temp5 = regExpFrc.cap(5).toUInt(&ok5);
815                                         if(ok1) fSizeW = temp1;
816                                         if(ok2) fSizeH = temp2;
817                                         if(ok3) fpsNom = temp3;
818                                         if(ok4) fpsDen = temp4;
819                                         if(ok5) frames = temp5;
820                                 }
821                                 if(!text.isEmpty())
822                                 {
823                                         log(text);
824                                 }
825                                 if(text.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
826                                 {
827                                         log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(x64 ? "64" : "32"));
828                                 }
829                         }
830                 }
831         }
832
833         process.waitForFinished();
834         if(process.state() != QProcess::NotRunning)
835         {
836                 process.kill();
837                 process.waitForFinished(-1);
838         }
839
840         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
841         {
842                 if(!(bTimeout || bAborted))
843                 {
844                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
845                 }
846                 return false;
847         }
848
849         if(frames == 0)
850         {
851                 log(tr("\nFAILED TO DETERMINE AVS PROPERTIES !!!"));
852                 return false;
853         }
854         
855         log("");
856
857         if((fSizeW > 0) && (fSizeH > 0))
858         {
859                 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
860         }
861         if((fpsNom > 0) && (fpsDen > 0))
862         {
863                 log(tr("Frame Rate: %1/%2").arg(QString::number(fpsNom), QString::number(fpsDen)));
864         }
865         if((fpsNom > 0) && (fpsDen == 0))
866         {
867                 log(tr("Frame Rate: %1").arg(QString::number(fpsNom)));
868         }
869         if(frames > 0)
870         {
871                 log(tr("No. Frames: %1").arg(QString::number(frames)));
872         }
873
874         return true;
875 }
876
877 ///////////////////////////////////////////////////////////////////////////////
878 // Misc functions
879 ///////////////////////////////////////////////////////////////////////////////
880
881 void EncodeThread::setStatus(JobStatus newStatus)
882 {
883         if(m_status != newStatus)
884         {
885                 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
886                 {
887                         if(m_status != JobStatus_Paused) setProgress(0);
888                 }
889                 if(newStatus == JobStatus_Failed)
890                 {
891                         setDetails("The job has failed. See log for details!");
892                 }
893                 if(newStatus == JobStatus_Aborted)
894                 {
895                         setDetails("The job was aborted by the user!");
896                 }
897                 m_status = newStatus;
898                 emit statusChanged(m_jobId, newStatus);
899         }
900 }
901
902 void EncodeThread::setProgress(unsigned int newProgress)
903 {
904         if(m_progress != newProgress)
905         {
906                 m_progress = newProgress;
907                 emit progressChanged(m_jobId, m_progress);
908         }
909 }
910
911 void EncodeThread::setDetails(const QString &text)
912 {
913         emit detailsChanged(m_jobId, text);
914 }
915
916 QString EncodeThread::pathToLocal(const QString &longPath, bool create, bool keep)
917 {
918         QTextCodec *localCodec = QTextCodec::codecForName("System");
919         
920         //Do NOT convert to short, if path can be represented in local Codepage
921         if(localCodec->toUnicode(localCodec->fromUnicode(longPath)).compare(longPath, Qt::CaseInsensitive) == 0)
922         {
923                 return longPath;
924         }
925         
926         //Create dummy file, if required (only existing files can have a short path!)
927         QFile tempFile;
928         if((!QFileInfo(longPath).exists()) && create)
929         {
930                 tempFile.setFileName(longPath);
931                 tempFile.open(QIODevice::WriteOnly);
932         }
933         
934         QString shortPath;
935         DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
936         
937         if(buffSize > 0)
938         {
939                 wchar_t *buffer = new wchar_t[buffSize];
940                 DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
941
942                 if(result > 0 && result < buffSize)
943                 {
944                         shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer));
945                 }
946
947                 delete[] buffer;
948                 buffer = NULL;
949         }
950
951         //Remove the dummy file now (FFMS2 fails, if index file does exist but is empty!)
952         if(tempFile.isOpen())
953         {
954                 if(!keep) tempFile.remove();
955                 tempFile.close();
956         }
957
958         if(shortPath.isEmpty())
959         {
960                 log(tr("Warning: Failed to convert path \"%1\" to short!\n").arg(longPath));
961         }
962
963         return (shortPath.isEmpty() ? longPath : shortPath);
964 }
965
966 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
967 {
968         QMutexLocker lock(&m_mutex_startProcess);
969         log(commandline2string(program, args) + "\n");
970
971         //Create a new job object, if not done yet
972         if(!m_handle_jobObject)
973         {
974                 m_handle_jobObject = CreateJobObject(NULL, NULL);
975                 if(m_handle_jobObject == INVALID_HANDLE_VALUE)
976                 {
977                         m_handle_jobObject = NULL;
978                 }
979                 if(m_handle_jobObject)
980                 {
981                         JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
982                         memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
983                         jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
984                         SetInformationJobObject(m_handle_jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
985                 }
986         }
987
988         if(mergeChannels)
989         {
990                 process.setProcessChannelMode(QProcess::MergedChannels);
991                 process.setReadChannel(QProcess::StandardOutput);
992         }
993         else
994         {
995                 process.setProcessChannelMode(QProcess::SeparateChannels);
996                 process.setReadChannel(QProcess::StandardError);
997         }
998
999         process.start(program, args);
1000         
1001         if(process.waitForStarted())
1002         {
1003                 Q_PID pid = process.pid();
1004                 AssignProcessToJobObject(m_handle_jobObject, process.pid()->hProcess);
1005                 if(pid != NULL)
1006                 {
1007                         if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
1008                         {
1009                                 SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
1010                         }
1011                 }
1012                 
1013                 lock.unlock();
1014                 return true;
1015         }
1016
1017         log("Process creation has failed :-(");
1018         QString errorMsg= process.errorString().trimmed();
1019         if(!errorMsg.isEmpty()) log(errorMsg);
1020
1021         process.kill();
1022         process.waitForFinished(-1);
1023         return false;
1024 }
1025
1026 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
1027 {
1028         QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
1029         
1030         for(int i = 0; i < arguments.count(); i++)
1031         {
1032                 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
1033         }
1034
1035         return commandline;
1036 }
1037
1038 QStringList EncodeThread::splitParams(const QString &params)
1039 {
1040         QStringList list; 
1041         bool ignoreWhitespaces = false;
1042         QString temp;
1043
1044         for(int i = 0; i < params.length(); i++)
1045         {
1046                 const QChar c = params.at(i);
1047
1048                 if(c == QChar::fromLatin1('"'))
1049                 {
1050                         ignoreWhitespaces = (!ignoreWhitespaces);
1051                         continue;
1052                 }
1053                 else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
1054                 {
1055                         APPEND_AND_CLEAR(list, temp);
1056                         continue;
1057                 }
1058                 
1059                 temp.append(c);
1060         }
1061         
1062         APPEND_AND_CLEAR(list, temp);
1063         return list;
1064 }