OSDN Git Service

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