1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "thread_encode.h"
25 #include "model_options.h"
26 #include "model_preferences.h"
27 #include "job_object.h"
38 #include <QCryptographicHash>
43 QMutex EncodeThread::m_mutex_startProcess;
48 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
52 log("\nPROCESS ABORTED BY USER !!!"); \
53 setStatus(JobStatus_Aborted); \
54 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
55 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
60 setStatus(JobStatus_Failed); \
61 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
62 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
68 #define APPEND_AND_CLEAR(LIST, STR) do \
70 if(!((STR).isEmpty())) \
78 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
80 if(ITER != LIST.end()) \
82 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
84 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
85 ITER = LIST.erase(ITER); \
86 if(ITER != LIST.end()) \
88 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
96 #define AVS2_BINARY(BIN_DIR, IS_X64) (QString("%1/%2/avs2yuv_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86")))
97 #define X264_BINARY(BIN_DIR, IS_10BIT, IS_X64) (QString("%1/%2/x264_%3_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86"), ((IS_10BIT) ? "10bit" : "8bit")))
98 #define VPSP_BINARY(VPS_DIR) (QString("%1/core/vspipe.exe").arg((VPS_DIR)))
103 static const unsigned int REV_MULT = 10000;
104 static const char *VPS_TEST_FILE = "import vapoursynth as vs\ncore = vs.get_core()\nv = core.std.BlankClip()\nv.set_output()\n";
106 ///////////////////////////////////////////////////////////////////////////////
107 // Constructor & Destructor
108 ///////////////////////////////////////////////////////////////////////////////
110 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, const QString &vpsDir, const bool &x264_x64, const bool &x264_10bit, const bool &avs2yuv_x64, const bool &skipVersionTest, const int &processPriroity, const bool &abortOnTimeout)
112 m_jobId(QUuid::createUuid()),
113 m_sourceFileName(sourceFileName),
114 m_outputFileName(outputFileName),
115 m_options(new OptionsModel(*options)),
118 m_x264_x64(x264_x64),
119 m_x264_10bit(x264_10bit),
120 m_avs2yuv_x64(avs2yuv_x64),
121 m_skipVersionTest(skipVersionTest),
122 m_processPriority(processPriroity),
123 m_abortOnTimeout(abortOnTimeout),
124 m_jobObject(new JobObject),
131 EncodeThread::~EncodeThread(void)
133 X264_DELETE(m_options);
134 X264_DELETE(m_jobObject);
137 ///////////////////////////////////////////////////////////////////////////////
138 // Thread entry point
139 ///////////////////////////////////////////////////////////////////////////////
141 void EncodeThread::run(void)
150 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
158 m_jobObject->terminateJob(42);
159 X264_DELETE(m_jobObject);
163 void EncodeThread::checkedRun(void)
166 m_status = JobStatus_Starting;
176 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
177 setStatus(JobStatus_Failed);
181 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
182 setStatus(JobStatus_Failed);
187 x264_fatal_exit(L"Unhandeled exception error in encode thread!");
191 void EncodeThread::start(Priority priority)
193 qDebug("Thread starting...");
198 while(m_semaphorePaused.tryAcquire(1, 0));
199 QThread::start(priority);
202 ///////////////////////////////////////////////////////////////////////////////
204 ///////////////////////////////////////////////////////////////////////////////
206 void EncodeThread::encode(void)
208 QDateTime startTime = QDateTime::currentDateTime();
210 //Print some basic info
211 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
212 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
213 log(tr("Source file: %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
214 log(tr("Output file: %1").arg(QDir::toNativeSeparators(m_outputFileName)));
216 if(!m_vpsDir.isEmpty())
218 log(tr("\nVapourSynth: %1").arg(m_vpsDir));
221 //Print encoder settings
222 log(tr("\n--- SETTINGS ---\n"));
223 log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
224 log(tr("Preset: %1").arg(m_options->preset()));
225 log(tr("Tuning: %1").arg(m_options->tune()));
226 log(tr("Profile: %1").arg(m_options->profile()));
227 log(tr("Custom: %1").arg(m_options->customX264().isEmpty() ? tr("(None)") : m_options->customX264()));
232 unsigned int frames = 0;
234 //Seletct type of input
235 const int inputType = getInputType(QFileInfo(m_sourceFileName).suffix());
236 const QString indexFile = QString("%1/x264_%2.ffindex").arg(QDir::tempPath(), stringToHash(m_sourceFileName));
238 //Checking x264 version
239 log(tr("\n--- CHECK VERSION ---\n"));
240 unsigned int revision_x264 = UINT_MAX;
241 bool x264_modified = false;
242 ok = ((revision_x264 = checkVersionX264(m_x264_x64, m_x264_10bit, x264_modified)) != UINT_MAX);
243 CHECK_STATUS(m_abort, ok);
245 //Checking avs2yuv version
246 unsigned int revision_avs2yuv = UINT_MAX;
250 ok = ((revision_avs2yuv = checkVersionAvs2yuv(m_avs2yuv_x64)) != UINT_MAX);
251 CHECK_STATUS(m_abort, ok);
254 ok = checkVersionVapoursynth();
255 CHECK_STATUS(m_abort, ok);
260 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()));
261 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)));
263 //Is x264 revision supported?
264 if((revision_x264 % REV_MULT) < x264_version_x264_minimum_rev())
266 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(x264_version_x264_minimum_rev())));
267 setStatus(JobStatus_Failed);
270 if((revision_x264 / REV_MULT) != x264_version_x264_current_api())
272 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
273 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(x264_version_x264_current_api())));
275 if((revision_avs2yuv != UINT_MAX) && ((revision_avs2yuv % REV_MULT) != x264_version_x264_avs2yuv_ver()))
277 log(tr("\nERROR: Your version of avs2yuv is unsupported (Required version: v0.24 BugMaster's mod 2)"));
278 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
279 setStatus(JobStatus_Failed);
284 if(inputType != INPUT_NATIVE)
286 log(tr("\n--- SOURCE INFO ---\n"));
290 ok = checkPropertiesAvisynth(m_avs2yuv_x64, frames);
291 CHECK_STATUS(m_abort, ok);
294 ok = checkPropertiesVapoursynth(frames);
295 CHECK_STATUS(m_abort, ok);
300 //Run encoding passes
301 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
303 QFileInfo info(m_outputFileName);
304 QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
306 if(QFileInfo(passLogFile).exists())
309 while(QFileInfo(passLogFile).exists())
311 passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
315 log(tr("\n--- PASS 1 ---\n"));
316 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, inputType, frames, indexFile, 1, passLogFile);
317 CHECK_STATUS(m_abort, ok);
319 log(tr("\n--- PASS 2 ---\n"));
320 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, inputType, frames, indexFile, 2, passLogFile);
321 CHECK_STATUS(m_abort, ok);
325 log(tr("\n--- ENCODING ---\n"));
326 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, inputType, frames, indexFile);
327 CHECK_STATUS(m_abort, ok);
330 log(tr("\n--- DONE ---\n"));
331 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
332 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)));
333 setStatus(JobStatus_Completed);
336 #define X264_UPDATE_PROGRESS(X) do \
339 unsigned int progress = (X).cap(1).toUInt(&ok); \
340 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
341 if(ok && ((progress > last_progress) || (last_progress == UINT_MAX))) \
343 setProgress(progress); \
344 size_estimate = estimateSize(progress); \
345 last_progress = progress; \
347 setDetails(tr("%1, est. file size %2").arg(text.mid(offset).trimmed(), sizeToString(size_estimate))); \
348 last_indexing = UINT_MAX; \
352 bool EncodeThread::runEncodingPass(bool x264_x64, bool x264_10bit, bool avs2yuv_x64, int inputType, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
354 QProcess processEncode, processInput;
356 if(inputType != INPUT_NATIVE)
358 QStringList cmdLine_Input;
359 processInput.setStandardOutputProcess(&processEncode);
363 if(!m_options->customAvs2YUV().isEmpty())
365 cmdLine_Input.append(splitParams(m_options->customAvs2YUV()));
367 cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
368 cmdLine_Input << "-";
369 log("Creating Avisynth process:");
370 if(!startProcess(processInput, AVS2_BINARY(m_binDir, avs2yuv_x64), cmdLine_Input, false))
376 cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
377 cmdLine_Input << "-" << "-y4m";
378 log("Creating Vapoursynth process:");
379 if(!startProcess(processInput, VPSP_BINARY(m_vpsDir), cmdLine_Input, false))
385 throw "Bad input type encontered!";
389 QStringList cmdLine_Encode = buildCommandLine((inputType != INPUT_NATIVE), x264_10bit, frames, indexFile, pass, passLogFile);
391 log("Creating x264 process:");
392 if(!startProcess(processEncode, X264_BINARY(m_binDir, x264_10bit, x264_x64), cmdLine_Encode))
397 QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
398 QRegExp regExpProgress("\\[(\\d+)\\.(\\d+)%\\].+frames");
399 QRegExp regExpModified("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)");
400 QRegExp regExpFrameCnt("^(\\d+) frames:");
402 QTextCodec *localCodec = QTextCodec::codecForName("System");
404 bool bTimeout = false;
405 bool bAborted = false;
407 unsigned int last_progress = UINT_MAX;
408 unsigned int last_indexing = UINT_MAX;
409 qint64 size_estimate = 0I64;
411 //Main processing loop
412 while(processEncode.state() != QProcess::NotRunning)
414 unsigned int waitCounter = 0;
416 //Wait until new output is available
421 processEncode.kill();
426 if(m_pause && (processEncode.state() == QProcess::Running))
428 JobStatus previousStatus = m_status;
429 setStatus(JobStatus_Paused);
430 log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
431 bool ok[2] = {false, false};
432 QProcess *proc[2] = { &processEncode, &processInput };
433 ok[0] = x264_suspendProcess(proc[0], true);
434 ok[1] = x264_suspendProcess(proc[1], true);
435 while(m_pause) m_semaphorePaused.tryAcquire(1, 5000);
436 while(m_semaphorePaused.tryAcquire(1, 0));
437 ok[0] = x264_suspendProcess(proc[0], false);
438 ok[1] = x264_suspendProcess(proc[1], false);
439 if(!m_abort) setStatus(previousStatus);
440 log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
444 if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
446 if(processEncode.state() == QProcess::Running)
448 if(++waitCounter > m_processTimeoutMaxCounter)
452 processEncode.kill();
453 qWarning("x264 process timed out <-- killing!");
454 log("\nPROCESS TIMEOUT !!!");
459 else if(waitCounter == m_processTimeoutWarning)
461 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
462 log(tr("Warning: x264 did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
467 if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
474 //Exit main processing loop now?
475 if(bAborted || bTimeout)
481 while(processEncode.bytesAvailable() > 0)
483 QList<QByteArray> lines = processEncode.readLine().split('\r');
484 while(!lines.isEmpty())
486 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
488 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
490 X264_UPDATE_PROGRESS(regExpProgress);
492 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
495 unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
496 setStatus(JobStatus_Indexing);
497 if(ok && ((progress > last_indexing) || (last_indexing == UINT_MAX)))
499 setProgress(progress);
500 last_indexing = progress;
502 setDetails(text.mid(offset).trimmed());
503 last_progress = UINT_MAX;
505 else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
507 last_progress = last_indexing = UINT_MAX;
508 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
509 setDetails(text.mid(offset).trimmed());
511 else if((offset = regExpModified.lastIndexIn(text)) >= 0)
513 X264_UPDATE_PROGRESS(regExpModified);
515 else if(!text.isEmpty())
517 last_progress = last_indexing = UINT_MAX;
524 processEncode.waitForFinished(5000);
525 if(processEncode.state() != QProcess::NotRunning)
527 qWarning("x264 process still running, going to kill it!");
528 processEncode.kill();
529 processEncode.waitForFinished(-1);
532 processInput.waitForFinished(5000);
533 if(processInput.state() != QProcess::NotRunning)
535 qWarning("Input process still running, going to kill it!");
537 processInput.waitForFinished(-1);
540 if(!(bTimeout || bAborted))
542 while(processInput.bytesAvailable() > 0)
547 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
550 log(tr("vpyp [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
556 if((inputType != INPUT_NATIVE) && (processInput.exitCode() != EXIT_SUCCESS))
558 if(!(bTimeout || bAborted))
560 const int exitCode = processInput.exitCode();
561 log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
562 if((inputType == INPUT_AVISYN) && ((exitCode < 0) || (exitCode > 2)))
564 log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
565 log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
567 if((inputType == INPUT_VAPOUR) && ((exitCode < 0) || (exitCode > 1)))
569 log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
574 if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
576 if(!(bTimeout || bAborted))
578 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
580 processEncode.close();
581 processInput.close();
585 QThread::yieldCurrentThread();
587 const qint64 finalSize = QFileInfo(m_outputFileName).size();
588 QLocale locale(QLocale::English);
589 log(tr("Final file size is %1 bytes.").arg(locale.toString(finalSize)));
594 setStatus(JobStatus_Running_Pass1);
595 setDetails(tr("First pass completed. Preparing for second pass..."));
598 setStatus(JobStatus_Running_Pass2);
599 setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
602 setStatus(JobStatus_Running);
603 setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
608 processEncode.close();
609 processInput.close();
613 QStringList EncodeThread::buildCommandLine(bool usePipe, bool use10Bit, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
616 double crf_int = 0.0, crf_frc = 0.0;
618 switch(m_options->rcMode())
620 case OptionsModel::RCMode_CQ:
621 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
623 case OptionsModel::RCMode_CRF:
624 crf_frc = modf(m_options->quantizer(), &crf_int);
625 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
627 case OptionsModel::RCMode_2Pass:
628 case OptionsModel::RCMode_ABR:
629 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
632 throw "Bad rate-control mode !!!";
636 if((pass == 1) || (pass == 2))
638 cmdLine << "--pass" << QString::number(pass);
639 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
642 cmdLine << "--preset" << m_options->preset().toLower();
644 if(m_options->tune().compare("none", Qt::CaseInsensitive))
646 cmdLine << "--tune" << m_options->tune().toLower();
649 if(m_options->profile().compare("auto", Qt::CaseInsensitive))
653 if(m_options->profile().compare("baseline", Qt::CaseInsensitive) || m_options->profile().compare("main", Qt::CaseInsensitive) || m_options->profile().compare("high", Qt::CaseInsensitive))
655 log(tr("WARNING: Selected H.264 Profile not compatible with 10-Bit encoding. Ignoring!\n"));
659 cmdLine << "--profile" << m_options->profile().toLower();
664 cmdLine << "--profile" << m_options->profile().toLower();
668 if(!m_options->customX264().isEmpty())
670 QStringList customArgs = splitParams(m_options->customX264());
673 QStringList::iterator i = customArgs.begin();
674 while(i != customArgs.end())
676 bool bModified = false;
677 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
678 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
682 cmdLine.append(customArgs);
685 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
689 if(frames < 1) throw "Frames not set!";
690 cmdLine << "--frames" << QString::number(frames);
691 cmdLine << "--demuxer" << "y4m";
692 cmdLine << "--stdin" << "y4m" << "-";
696 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
697 cmdLine << QDir::toNativeSeparators(m_sourceFileName);
703 unsigned int EncodeThread::checkVersionX264(bool use_x64, bool use_10bit, bool &modified)
705 if(m_skipVersionTest)
707 log("Warning: Skipping x264 version check this time!");
708 return (999 * REV_MULT) + (9999 % REV_MULT);
712 QStringList cmdLine = QStringList() << "--version";
714 log("Creating process:");
715 if(!startProcess(process, X264_BINARY(m_binDir, use_10bit, use_x64), cmdLine))
720 QRegExp regExpVersion("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
721 QRegExp regExpVersionMod("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
723 bool bTimeout = false;
724 bool bAborted = false;
726 unsigned int revision = UINT_MAX;
727 unsigned int coreVers = UINT_MAX;
730 while(process.state() != QProcess::NotRunning)
738 if(!process.waitForReadyRead())
740 if(process.state() == QProcess::Running)
743 qWarning("x264 process timed out <-- killing!");
744 log("\nPROCESS TIMEOUT !!!");
749 while(process.bytesAvailable() > 0)
751 QList<QByteArray> lines = process.readLine().split('\r');
752 while(!lines.isEmpty())
754 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
756 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
758 bool ok1 = false, ok2 = false;
759 unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
760 unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
761 if(ok1) coreVers = temp1;
762 if(ok2) revision = temp2;
764 else if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
766 bool ok1 = false, ok2 = false;
767 unsigned int temp1 = regExpVersionMod.cap(2).toUInt(&ok1);
768 unsigned int temp2 = regExpVersionMod.cap(3).toUInt(&ok2);
769 if(ok1) coreVers = temp1;
770 if(ok2) revision = temp2;
781 process.waitForFinished();
782 if(process.state() != QProcess::NotRunning)
785 process.waitForFinished(-1);
788 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
790 if(!(bTimeout || bAborted))
792 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
797 if((revision == UINT_MAX) || (coreVers == UINT_MAX))
799 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
803 return (coreVers * REV_MULT) + (revision % REV_MULT);
806 unsigned int EncodeThread::checkVersionAvs2yuv(bool x64)
810 log("\nCreating process:");
811 if(!startProcess(process, AVS2_BINARY(m_binDir, x64), QStringList()))
816 QRegExp regExpVersionMod("\\bAvs2YUV (\\d+).(\\d+)bm(\\d)\\b", Qt::CaseInsensitive);
817 QRegExp regExpVersionOld("\\bAvs2YUV (\\d+).(\\d+)\\b", Qt::CaseInsensitive);
819 bool bTimeout = false;
820 bool bAborted = false;
822 unsigned int ver_maj = UINT_MAX;
823 unsigned int ver_min = UINT_MAX;
824 unsigned int ver_mod = 0;
826 while(process.state() != QProcess::NotRunning)
834 if(!process.waitForReadyRead())
836 if(process.state() == QProcess::Running)
839 qWarning("Avs2YUV process timed out <-- killing!");
840 log("\nPROCESS TIMEOUT !!!");
845 while(process.bytesAvailable() > 0)
847 QList<QByteArray> lines = process.readLine().split('\r');
848 while(!lines.isEmpty())
850 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
852 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX) || (ver_mod == UINT_MAX))
859 if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
861 bool ok1 = false, ok2 = false, ok3 = false;
862 unsigned int temp1 = regExpVersionMod.cap(1).toUInt(&ok1);
863 unsigned int temp2 = regExpVersionMod.cap(2).toUInt(&ok2);
864 unsigned int temp3 = regExpVersionMod.cap(3).toUInt(&ok3);
865 if(ok1) ver_maj = temp1;
866 if(ok2) ver_min = temp2;
867 if(ok3) ver_mod = temp3;
869 else if((offset = regExpVersionOld.lastIndexIn(text)) >= 0)
871 bool ok1 = false, ok2 = false;
872 unsigned int temp1 = regExpVersionOld.cap(1).toUInt(&ok1);
873 unsigned int temp2 = regExpVersionOld.cap(2).toUInt(&ok2);
874 if(ok1) ver_maj = temp1;
875 if(ok2) ver_min = temp2;
881 process.waitForFinished();
882 if(process.state() != QProcess::NotRunning)
885 process.waitForFinished(-1);
888 if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 2)))
890 if(!(bTimeout || bAborted))
892 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
897 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX))
899 log(tr("\nFAILED TO DETERMINE AVS2YUV VERSION !!!"));
903 return (ver_maj * REV_MULT) + ((ver_min % REV_MULT) * 10) + (ver_mod % 10);
906 bool EncodeThread::checkVersionVapoursynth(/*const QString &vspipePath*/)
908 //Is VapourSynth available at all?
909 if(m_vpsDir.isEmpty() || (!QFileInfo(VPSP_BINARY(m_vpsDir)).isFile()))
911 log(tr("\nVPY INPUT REQUIRES VAPOURSYNTH, BUT IT IS *NOT* AVAILABLE !!!"));
917 log("\nCreating process:");
918 if(!startProcess(process, VPSP_BINARY(m_vpsDir), QStringList()))
923 QRegExp regExpSignature("\\bVSPipe\\s+usage\\b", Qt::CaseInsensitive);
925 bool bTimeout = false;
926 bool bAborted = false;
928 bool vspipeSignature = false;
930 while(process.state() != QProcess::NotRunning)
938 if(!process.waitForReadyRead())
940 if(process.state() == QProcess::Running)
943 qWarning("VSPipe process timed out <-- killing!");
944 log("\nPROCESS TIMEOUT !!!");
949 while(process.bytesAvailable() > 0)
951 QList<QByteArray> lines = process.readLine().split('\r');
952 while(!lines.isEmpty())
954 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
955 if(regExpSignature.lastIndexIn(text) >= 0)
957 vspipeSignature = true;
967 process.waitForFinished();
968 if(process.state() != QProcess::NotRunning)
971 process.waitForFinished(-1);
974 if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 1)))
976 if(!(bTimeout || bAborted))
978 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
985 log(tr("\nFAILED TO DETECT VSPIPE SIGNATURE !!!"));
989 return vspipeSignature;
992 bool EncodeThread::checkPropertiesAvisynth(bool x64, unsigned int &frames)
997 if(!m_options->customAvs2YUV().isEmpty())
999 cmdLine.append(splitParams(m_options->customAvs2YUV()));
1002 cmdLine << "-frames" << "1";
1003 cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true)) << "NUL";
1005 log("Creating process:");
1006 if(!startProcess(process, AVS2_BINARY(m_binDir, x64), cmdLine))
1011 QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
1012 QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
1014 QTextCodec *localCodec = QTextCodec::codecForName("System");
1016 bool bTimeout = false;
1017 bool bAborted = false;
1021 unsigned int fpsNom = 0;
1022 unsigned int fpsDen = 0;
1023 unsigned int fSizeW = 0;
1024 unsigned int fSizeH = 0;
1026 unsigned int waitCounter = 0;
1028 while(process.state() != QProcess::NotRunning)
1036 if(!process.waitForReadyRead(m_processTimeoutInterval))
1038 if(process.state() == QProcess::Running)
1040 if(++waitCounter > m_processTimeoutMaxCounter)
1042 if(m_abortOnTimeout)
1045 qWarning("Avs2YUV process timed out <-- killing!");
1046 log("\nPROCESS TIMEOUT !!!");
1047 log("\nAvisynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
1052 else if(waitCounter == m_processTimeoutWarning)
1054 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
1055 log(tr("Warning: Avisynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
1063 while(process.bytesAvailable() > 0)
1065 QList<QByteArray> lines = process.readLine().split('\r');
1066 while(!lines.isEmpty())
1068 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
1070 if((offset = regExpInt.lastIndexIn(text)) >= 0)
1072 bool ok1 = false, ok2 = false;
1073 bool ok3 = false, ok4 = false;
1074 unsigned int temp1 = regExpInt.cap(1).toUInt(&ok1);
1075 unsigned int temp2 = regExpInt.cap(2).toUInt(&ok2);
1076 unsigned int temp3 = regExpInt.cap(3).toUInt(&ok3);
1077 unsigned int temp4 = regExpInt.cap(4).toUInt(&ok4);
1078 if(ok1) fSizeW = temp1;
1079 if(ok2) fSizeH = temp2;
1080 if(ok3) fpsNom = temp3;
1081 if(ok4) frames = temp4;
1083 else if((offset = regExpFrc.lastIndexIn(text)) >= 0)
1085 bool ok1 = false, ok2 = false;
1086 bool ok3 = false, ok4 = false, ok5 = false;
1087 unsigned int temp1 = regExpFrc.cap(1).toUInt(&ok1);
1088 unsigned int temp2 = regExpFrc.cap(2).toUInt(&ok2);
1089 unsigned int temp3 = regExpFrc.cap(3).toUInt(&ok3);
1090 unsigned int temp4 = regExpFrc.cap(4).toUInt(&ok4);
1091 unsigned int temp5 = regExpFrc.cap(5).toUInt(&ok5);
1092 if(ok1) fSizeW = temp1;
1093 if(ok2) fSizeH = temp2;
1094 if(ok3) fpsNom = temp3;
1095 if(ok4) fpsDen = temp4;
1096 if(ok5) frames = temp5;
1102 if(text.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
1104 log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(x64 ? "64" : "32"));
1106 if(text.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
1108 log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
1114 process.waitForFinished();
1115 if(process.state() != QProcess::NotRunning)
1118 process.waitForFinished(-1);
1121 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
1123 if(!(bTimeout || bAborted))
1125 const int exitCode = process.exitCode();
1126 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
1127 if((exitCode < 0) || (exitCode > 2))
1129 log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
1130 log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
1138 log(tr("\nFAILED TO DETERMINE AVS PROPERTIES !!!"));
1144 if((fSizeW > 0) && (fSizeH > 0))
1146 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
1148 if((fpsNom > 0) && (fpsDen > 0))
1150 log(tr("Frame Rate: %1/%2").arg(QString::number(fpsNom), QString::number(fpsDen)));
1152 if((fpsNom > 0) && (fpsDen == 0))
1154 log(tr("Frame Rate: %1").arg(QString::number(fpsNom)));
1158 log(tr("No. Frames: %1").arg(QString::number(frames)));
1164 bool EncodeThread::checkPropertiesVapoursynth(/*const QString &vspipePath,*/ unsigned int &frames)
1167 QStringList cmdLine;
1169 cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
1170 cmdLine << "-" << "-info";
1172 log("Creating process:");
1173 if(!startProcess(process, VPSP_BINARY(m_vpsDir), cmdLine))
1178 QRegExp regExpFrm("\\bFrames:\\s+(\\d+)\\b");
1179 QRegExp regExpSzW("\\bWidth:\\s+(\\d+)\\b");
1180 QRegExp regExpSzH("\\bHeight:\\s+(\\d+)\\b");
1182 QTextCodec *localCodec = QTextCodec::codecForName("System");
1184 bool bTimeout = false;
1185 bool bAborted = false;
1189 unsigned int fSizeW = 0;
1190 unsigned int fSizeH = 0;
1192 unsigned int waitCounter = 0;
1194 while(process.state() != QProcess::NotRunning)
1202 if(!process.waitForReadyRead(m_processTimeoutInterval))
1204 if(process.state() == QProcess::Running)
1206 if(++waitCounter > m_processTimeoutMaxCounter)
1208 if(m_abortOnTimeout)
1211 qWarning("VSPipe process timed out <-- killing!");
1212 log("\nPROCESS TIMEOUT !!!");
1213 log("\nVapoursynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
1218 else if(waitCounter == m_processTimeoutWarning)
1220 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
1221 log(tr("Warning: nVapoursynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
1229 while(process.bytesAvailable() > 0)
1231 QList<QByteArray> lines = process.readLine().split('\r');
1232 while(!lines.isEmpty())
1234 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
1236 if((offset = regExpFrm.lastIndexIn(text)) >= 0)
1239 unsigned int temp = regExpFrm.cap(1).toUInt(&ok);
1240 if(ok) frames = temp;
1242 if((offset = regExpSzW.lastIndexIn(text)) >= 0)
1245 unsigned int temp = regExpSzW.cap(1).toUInt(&ok);
1246 if(ok) fSizeW = temp;
1248 if((offset = regExpSzH.lastIndexIn(text)) >= 0)
1251 unsigned int temp = regExpSzH.cap(1).toUInt(&ok);
1252 if(ok) fSizeH = temp;
1262 process.waitForFinished();
1263 if(process.state() != QProcess::NotRunning)
1266 process.waitForFinished(-1);
1269 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
1271 if(!(bTimeout || bAborted))
1273 const int exitCode = process.exitCode();
1274 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
1275 if((exitCode < 0) || (exitCode > 1))
1277 log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
1285 log(tr("\nFAILED TO DETERMINE VPY PROPERTIES !!!"));
1291 if((fSizeW > 0) && (fSizeH > 0))
1293 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
1297 log(tr("No. Frames: %1").arg(QString::number(frames)));
1303 ///////////////////////////////////////////////////////////////////////////////
1305 ///////////////////////////////////////////////////////////////////////////////
1307 void EncodeThread::setStatus(JobStatus newStatus)
1309 if(m_status != newStatus)
1311 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
1313 if(m_status != JobStatus_Paused) setProgress(0);
1315 if(newStatus == JobStatus_Failed)
1317 setDetails("The job has failed. See log for details!");
1319 if(newStatus == JobStatus_Aborted)
1321 setDetails("The job was aborted by the user!");
1323 m_status = newStatus;
1324 emit statusChanged(m_jobId, newStatus);
1328 void EncodeThread::setProgress(unsigned int newProgress)
1330 if(m_progress != newProgress)
1332 m_progress = newProgress;
1333 emit progressChanged(m_jobId, m_progress);
1337 void EncodeThread::setDetails(const QString &text)
1339 emit detailsChanged(m_jobId, text);
1342 QString EncodeThread::stringToHash(const QString &string)
1344 QByteArray result(10, char(0));
1345 const QByteArray hash = QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::Sha1);
1347 if((hash.size() == 20) && (result.size() == 10))
1349 unsigned char *out = reinterpret_cast<unsigned char*>(result.data());
1350 const unsigned char *in = reinterpret_cast<const unsigned char*>(hash.constData());
1351 for(int i = 0; i < 10; i++)
1353 out[i] = (in[i] ^ in[10+i]);
1357 return QString::fromLatin1(result.toHex().constData());
1360 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
1362 QMutexLocker lock(&m_mutex_startProcess);
1363 log(commandline2string(program, args) + "\n");
1365 process.setWorkingDirectory(QDir::tempPath());
1369 process.setProcessChannelMode(QProcess::MergedChannels);
1370 process.setReadChannel(QProcess::StandardOutput);
1374 process.setProcessChannelMode(QProcess::SeparateChannels);
1375 process.setReadChannel(QProcess::StandardError);
1378 process.start(program, args);
1380 if(process.waitForStarted())
1382 m_jobObject->addProcessToJob(&process);
1383 x264_change_process_priority(&process, m_processPriority);
1388 log("Process creation has failed :-(");
1389 QString errorMsg= process.errorString().trimmed();
1390 if(!errorMsg.isEmpty()) log(errorMsg);
1393 process.waitForFinished(-1);
1397 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
1399 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
1401 for(int i = 0; i < arguments.count(); i++)
1403 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
1409 QStringList EncodeThread::splitParams(const QString ¶ms)
1412 bool ignoreWhitespaces = false;
1415 for(int i = 0; i < params.length(); i++)
1417 const QChar c = params.at(i);
1419 if(c == QChar::fromLatin1('"'))
1421 ignoreWhitespaces = (!ignoreWhitespaces);
1424 else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
1426 APPEND_AND_CLEAR(list, temp);
1433 APPEND_AND_CLEAR(list, temp);
1435 list.replaceInStrings("$(INPUT)", QDir::toNativeSeparators(m_sourceFileName), Qt::CaseInsensitive);
1436 list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(m_outputFileName), Qt::CaseInsensitive);
1441 qint64 EncodeThread::estimateSize(int progress)
1445 qint64 currentSize = QFileInfo(m_outputFileName).size();
1446 qint64 estimatedSize = (currentSize * 100I64) / static_cast<qint64>(progress);
1447 return estimatedSize;
1453 QString EncodeThread::sizeToString(qint64 size)
1455 static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
1459 qint64 estimatedSize = size;
1460 qint64 remainderSize = 0I64;
1463 while((estimatedSize > 1024I64) && (prefixIdx < 4))
1465 remainderSize = estimatedSize % 1024I64;
1466 estimatedSize = estimatedSize / 1024I64;
1470 double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
1471 return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
1477 int EncodeThread::getInputType(const QString &fileExt)
1479 int type = INPUT_NATIVE;
1480 if(fileExt.compare("avs", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
1481 else if(fileExt.compare("avsi", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
1482 else if(fileExt.compare("vpy", Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
1483 else if(fileExt.compare("py", Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;