1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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 "model_sysinfo.h"
28 #include "job_object.h"
40 #include <QCryptographicHash>
45 QMutex EncodeThread::m_mutex_startProcess;
48 * RAII execution state handler
50 class ExecutionStateHandler
53 ExecutionStateHandler(void)
55 x264_set_thread_execution_state(true);
57 ~ExecutionStateHandler(void)
59 x264_set_thread_execution_state(false);
62 //Disable copy constructor and assignment
63 ExecutionStateHandler(const ExecutionStateHandler &other) {}
64 ExecutionStateHandler &operator=(const ExecutionStateHandler &) {}
66 //Prevent object allocation on the heap
67 void *operator new(size_t); void *operator new[](size_t);
68 void operator delete(void *); void operator delete[](void*);
74 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
78 log("\nPROCESS ABORTED BY USER !!!"); \
79 setStatus(JobStatus_Aborted); \
80 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
81 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
86 setStatus(JobStatus_Failed); \
87 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
88 if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
94 #define APPEND_AND_CLEAR(LIST, STR) do \
96 if(!((STR).isEmpty())) \
104 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
106 if(ITER != LIST.end()) \
108 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
110 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
111 ITER = LIST.erase(ITER); \
112 if(ITER != LIST.end()) \
114 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
125 static const unsigned int REV_MULT = 10000;
126 static const char *VPS_TEST_FILE = "import vapoursynth as vs\ncore = vs.get_core()\nv = core.std.BlankClip()\nv.set_output()\n";
128 ///////////////////////////////////////////////////////////////////////////////
129 // Constructor & Destructor
130 ///////////////////////////////////////////////////////////////////////////////
132 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences)
134 m_jobId(QUuid::createUuid()),
135 m_sourceFileName(sourceFileName),
136 m_outputFileName(outputFileName),
137 m_options(new OptionsModel(*options)),
139 m_preferences(preferences),
140 m_jobObject(new JobObject),
147 EncodeThread::~EncodeThread(void)
149 X264_DELETE(m_options);
150 X264_DELETE(m_jobObject);
153 ///////////////////////////////////////////////////////////////////////////////
154 // Thread entry point
155 ///////////////////////////////////////////////////////////////////////////////
157 void EncodeThread::run(void)
166 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
174 m_jobObject->terminateJob(42);
175 X264_DELETE(m_jobObject);
179 void EncodeThread::checkedRun(void)
182 m_status = JobStatus_Starting;
188 ExecutionStateHandler executionStateHandler;
193 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
194 setStatus(JobStatus_Failed);
198 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
199 setStatus(JobStatus_Failed);
204 x264_fatal_exit(L"Unhandeled exception error in encode thread!");
208 void EncodeThread::start(Priority priority)
210 qDebug("Thread starting...");
215 while(m_semaphorePaused.tryAcquire(1, 0));
216 QThread::start(priority);
219 ///////////////////////////////////////////////////////////////////////////////
221 ///////////////////////////////////////////////////////////////////////////////
223 void EncodeThread::encode(void)
225 QDateTime startTime = QDateTime::currentDateTime();
227 //Print some basic info
228 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
229 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
230 log(tr("Source file: %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
231 log(tr("Output file: %1").arg(QDir::toNativeSeparators(m_outputFileName)));
233 if(!m_sysinfo->getVPSPath().isEmpty())
235 log(tr("\nVapourSynth: %1").arg(QDir::toNativeSeparators(m_sysinfo->getVPSPath())));
238 //Print encoder settings
239 log(tr("\n--- SETTINGS ---\n"));
240 log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
241 log(tr("Preset: %1").arg(m_options->preset()));
242 log(tr("Tuning: %1").arg(m_options->tune()));
243 log(tr("Profile: %1").arg(m_options->profile()));
244 log(tr("Custom: %1").arg(m_options->customEncParams().isEmpty() ? tr("(None)") : m_options->customEncParams()));
246 log(m_sysinfo->getAppPath());
249 unsigned int frames = 0;
251 //Seletct type of input
252 const int inputType = getInputType(QFileInfo(m_sourceFileName).suffix());
253 const QString indexFile = QString("%1/x264_%2.ffindex").arg(QDir::tempPath(), stringToHash(m_sourceFileName));
255 //Checking x264 version
256 log(tr("\n--- CHECK VERSION ---\n"));
257 unsigned int revision_x264 = UINT_MAX;
258 bool x264_modified = false;
259 ok = ((revision_x264 = checkVersionX264(x264_modified)) != UINT_MAX);
260 CHECK_STATUS(m_abort, ok);
262 //Checking avs2yuv version
263 unsigned int revision_avs2yuv = UINT_MAX;
267 ok = ((revision_avs2yuv = checkVersionAvs2yuv(m_avs2yuv_x64)) != UINT_MAX);
268 CHECK_STATUS(m_abort, ok);
271 ok = checkVersionVapoursynth();
272 CHECK_STATUS(m_abort, ok);
277 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()));
278 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)));
280 //Is x264 revision supported?
281 if((revision_x264 % REV_MULT) < x264_version_x264_minimum_rev())
283 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(x264_version_x264_minimum_rev())));
284 setStatus(JobStatus_Failed);
287 if((revision_x264 / REV_MULT) != x264_version_x264_current_api())
289 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
290 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(x264_version_x264_current_api())));
292 if((revision_avs2yuv != UINT_MAX) && ((revision_avs2yuv % REV_MULT) != x264_version_x264_avs2yuv_ver()))
294 log(tr("\nERROR: Your version of avs2yuv is unsupported (Required version: v0.24 BugMaster's mod 2)"));
295 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
296 setStatus(JobStatus_Failed);
301 if(inputType != INPUT_NATIVE)
303 log(tr("\n--- SOURCE INFO ---\n"));
307 ok = checkPropertiesAvisynth(m_avs2yuv_x64, frames);
308 CHECK_STATUS(m_abort, ok);
311 ok = checkPropertiesVapoursynth(frames);
312 CHECK_STATUS(m_abort, ok);
317 //Run encoding passes
318 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
320 QFileInfo info(m_outputFileName);
321 QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
323 if(QFileInfo(passLogFile).exists())
326 while(QFileInfo(passLogFile).exists())
328 passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
332 log(tr("\n--- PASS 1 ---\n"));
333 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, inputType, frames, indexFile, 1, passLogFile);
334 CHECK_STATUS(m_abort, ok);
336 log(tr("\n--- PASS 2 ---\n"));
337 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, inputType, frames, indexFile, 2, passLogFile);
338 CHECK_STATUS(m_abort, ok);
342 log(tr("\n--- ENCODING ---\n"));
343 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, inputType, frames, indexFile);
344 CHECK_STATUS(m_abort, ok);
347 log(tr("\n--- DONE ---\n"));
348 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
349 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)));
350 setStatus(JobStatus_Completed);
353 #define X264_UPDATE_PROGRESS(X) do \
356 unsigned int progress = (X).cap(1).toUInt(&ok); \
357 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
358 if(ok && ((progress > last_progress) || (last_progress == UINT_MAX))) \
360 setProgress(progress); \
361 size_estimate = estimateSize(progress); \
362 last_progress = progress; \
364 setDetails(tr("%1, est. file size %2").arg(text.mid(offset).trimmed(), sizeToString(size_estimate))); \
365 last_indexing = UINT_MAX; \
369 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)
371 QProcess processEncode, processInput;
373 if(inputType != INPUT_NATIVE)
375 QStringList cmdLine_Input;
376 processInput.setStandardOutputProcess(&processEncode);
380 if(!m_options->customAvs2YUV().isEmpty())
382 cmdLine_Input.append(splitParams(m_options->customAvs2YUV()));
384 cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
385 cmdLine_Input << "-";
386 log("Creating Avisynth process:");
387 if(!startProcess(processInput, AVS2_BINARY(m_binDir, avs2yuv_x64), cmdLine_Input, false))
393 cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
394 cmdLine_Input << "-" << "-y4m";
395 log("Creating Vapoursynth process:");
396 if(!startProcess(processInput, VPSP_BINARY(m_vpsDir), cmdLine_Input, false))
402 throw "Bad input type encontered!";
406 QStringList cmdLine_Encode = buildCommandLine((inputType != INPUT_NATIVE), x264_10bit, frames, indexFile, pass, passLogFile);
408 log("Creating x264 process:");
409 if(!startProcess(processEncode, X264_BINARY(m_binDir, x264_10bit, x264_x64), cmdLine_Encode))
414 QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
415 QRegExp regExpProgress("\\[(\\d+)\\.(\\d+)%\\].+frames");
416 QRegExp regExpModified("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)");
417 QRegExp regExpFrameCnt("^(\\d+) frames:");
419 QTextCodec *localCodec = QTextCodec::codecForName("System");
421 bool bTimeout = false;
422 bool bAborted = false;
424 unsigned int last_progress = UINT_MAX;
425 unsigned int last_indexing = UINT_MAX;
426 qint64 size_estimate = 0I64;
428 //Main processing loop
429 while(processEncode.state() != QProcess::NotRunning)
431 unsigned int waitCounter = 0;
433 //Wait until new output is available
438 processEncode.kill();
443 if(m_pause && (processEncode.state() == QProcess::Running))
445 JobStatus previousStatus = m_status;
446 setStatus(JobStatus_Paused);
447 log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
448 bool ok[2] = {false, false};
449 QProcess *proc[2] = { &processEncode, &processInput };
450 ok[0] = x264_suspendProcess(proc[0], true);
451 ok[1] = x264_suspendProcess(proc[1], true);
452 while(m_pause) m_semaphorePaused.tryAcquire(1, 5000);
453 while(m_semaphorePaused.tryAcquire(1, 0));
454 ok[0] = x264_suspendProcess(proc[0], false);
455 ok[1] = x264_suspendProcess(proc[1], false);
456 if(!m_abort) setStatus(previousStatus);
457 log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
461 if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
463 if(processEncode.state() == QProcess::Running)
465 if(++waitCounter > m_processTimeoutMaxCounter)
469 processEncode.kill();
470 qWarning("x264 process timed out <-- killing!");
471 log("\nPROCESS TIMEOUT !!!");
476 else if(waitCounter == m_processTimeoutWarning)
478 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
479 log(tr("Warning: x264 did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
484 if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
491 //Exit main processing loop now?
492 if(bAborted || bTimeout)
498 while(processEncode.bytesAvailable() > 0)
500 QList<QByteArray> lines = processEncode.readLine().split('\r');
501 while(!lines.isEmpty())
503 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
505 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
507 X264_UPDATE_PROGRESS(regExpProgress);
509 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
512 unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
513 setStatus(JobStatus_Indexing);
514 if(ok && ((progress > last_indexing) || (last_indexing == UINT_MAX)))
516 setProgress(progress);
517 last_indexing = progress;
519 setDetails(text.mid(offset).trimmed());
520 last_progress = UINT_MAX;
522 else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
524 last_progress = last_indexing = UINT_MAX;
525 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
526 setDetails(text.mid(offset).trimmed());
528 else if((offset = regExpModified.lastIndexIn(text)) >= 0)
530 X264_UPDATE_PROGRESS(regExpModified);
532 else if(!text.isEmpty())
534 last_progress = last_indexing = UINT_MAX;
541 processEncode.waitForFinished(5000);
542 if(processEncode.state() != QProcess::NotRunning)
544 qWarning("x264 process still running, going to kill it!");
545 processEncode.kill();
546 processEncode.waitForFinished(-1);
549 processInput.waitForFinished(5000);
550 if(processInput.state() != QProcess::NotRunning)
552 qWarning("Input process still running, going to kill it!");
554 processInput.waitForFinished(-1);
557 if(!(bTimeout || bAborted))
559 while(processInput.bytesAvailable() > 0)
564 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
567 log(tr("vpyp [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
573 if((inputType != INPUT_NATIVE) && (processInput.exitCode() != EXIT_SUCCESS))
575 if(!(bTimeout || bAborted))
577 const int exitCode = processInput.exitCode();
578 log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
579 if((inputType == INPUT_AVISYN) && ((exitCode < 0) || (exitCode >= 32)))
581 log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
582 log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
584 if((inputType == INPUT_VAPOUR) && ((exitCode < 0) || (exitCode >= 32)))
586 log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
591 if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
593 if(!(bTimeout || bAborted))
595 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
597 processEncode.close();
598 processInput.close();
602 QThread::yieldCurrentThread();
604 const qint64 finalSize = QFileInfo(m_outputFileName).size();
605 QLocale locale(QLocale::English);
606 log(tr("Final file size is %1 bytes.").arg(locale.toString(finalSize)));
611 setStatus(JobStatus_Running_Pass1);
612 setDetails(tr("First pass completed. Preparing for second pass..."));
615 setStatus(JobStatus_Running_Pass2);
616 setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
619 setStatus(JobStatus_Running);
620 setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
625 processEncode.close();
626 processInput.close();
630 QStringList EncodeThread::buildCommandLine(bool usePipe, bool use10Bit, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
633 double crf_int = 0.0, crf_frc = 0.0;
635 switch(m_options->rcMode())
637 case OptionsModel::RCMode_CQ:
638 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
640 case OptionsModel::RCMode_CRF:
641 crf_frc = modf(m_options->quantizer(), &crf_int);
642 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
644 case OptionsModel::RCMode_2Pass:
645 case OptionsModel::RCMode_ABR:
646 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
649 throw "Bad rate-control mode !!!";
653 if((pass == 1) || (pass == 2))
655 cmdLine << "--pass" << QString::number(pass);
656 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
659 cmdLine << "--preset" << m_options->preset().toLower();
661 if(m_options->tune().compare("none", Qt::CaseInsensitive))
663 cmdLine << "--tune" << m_options->tune().toLower();
666 if(m_options->profile().compare("auto", Qt::CaseInsensitive))
670 if(m_options->profile().compare("baseline", Qt::CaseInsensitive) || m_options->profile().compare("main", Qt::CaseInsensitive) || m_options->profile().compare("high", Qt::CaseInsensitive))
672 log(tr("WARNING: Selected H.264 Profile not compatible with 10-Bit encoding. Ignoring!\n"));
676 cmdLine << "--profile" << m_options->profile().toLower();
681 cmdLine << "--profile" << m_options->profile().toLower();
685 if(!m_options->customEncParams().isEmpty())
687 QStringList customArgs = splitParams(m_options->customEncParams());
690 QStringList::iterator i = customArgs.begin();
691 while(i != customArgs.end())
693 bool bModified = false;
694 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
695 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
699 cmdLine.append(customArgs);
702 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
706 if(frames < 1) throw "Frames not set!";
707 cmdLine << "--frames" << QString::number(frames);
708 cmdLine << "--demuxer" << "y4m";
709 cmdLine << "--stdin" << "y4m" << "-";
713 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
714 cmdLine << QDir::toNativeSeparators(m_sourceFileName);
720 unsigned int EncodeThread::checkVersionX264(bool &modified)
722 if(m_skipVersionTest)
724 log("Warning: Skipping x264 version check this time!");
725 return (999 * REV_MULT) + (9999 % REV_MULT);
729 QStringList cmdLine = QStringList() << "--version";
731 log("Creating process:");
732 if(!startProcess(process, ENC_BINARY(m_sysinfo, m_options), cmdLine))
737 QRegExp regExpVersion("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
738 QRegExp regExpVersionMod("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
740 bool bTimeout = false;
741 bool bAborted = false;
743 unsigned int revision = UINT_MAX;
744 unsigned int coreVers = UINT_MAX;
747 while(process.state() != QProcess::NotRunning)
755 if(!process.waitForReadyRead())
757 if(process.state() == QProcess::Running)
760 qWarning("x264 process timed out <-- killing!");
761 log("\nPROCESS TIMEOUT !!!");
766 while(process.bytesAvailable() > 0)
768 QList<QByteArray> lines = process.readLine().split('\r');
769 while(!lines.isEmpty())
771 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
773 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
775 bool ok1 = false, ok2 = false;
776 unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
777 unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
778 if(ok1) coreVers = temp1;
779 if(ok2) revision = temp2;
781 else if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
783 bool ok1 = false, ok2 = false;
784 unsigned int temp1 = regExpVersionMod.cap(2).toUInt(&ok1);
785 unsigned int temp2 = regExpVersionMod.cap(3).toUInt(&ok2);
786 if(ok1) coreVers = temp1;
787 if(ok2) revision = temp2;
798 process.waitForFinished();
799 if(process.state() != QProcess::NotRunning)
802 process.waitForFinished(-1);
805 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
807 if(!(bTimeout || bAborted))
809 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
814 if((revision == UINT_MAX) || (coreVers == UINT_MAX))
816 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
820 return (coreVers * REV_MULT) + (revision % REV_MULT);
823 unsigned int EncodeThread::checkVersionAvs2yuv(bool x64)
827 log("\nCreating process:");
828 if(!startProcess(process, AVS2_BINARY(m_binDir, x64), QStringList()))
833 QRegExp regExpVersionMod("\\bAvs2YUV (\\d+).(\\d+)bm(\\d)\\b", Qt::CaseInsensitive);
834 QRegExp regExpVersionOld("\\bAvs2YUV (\\d+).(\\d+)\\b", Qt::CaseInsensitive);
836 bool bTimeout = false;
837 bool bAborted = false;
839 unsigned int ver_maj = UINT_MAX;
840 unsigned int ver_min = UINT_MAX;
841 unsigned int ver_mod = 0;
843 while(process.state() != QProcess::NotRunning)
851 if(!process.waitForReadyRead())
853 if(process.state() == QProcess::Running)
856 qWarning("Avs2YUV process timed out <-- killing!");
857 log("\nPROCESS TIMEOUT !!!");
862 while(process.bytesAvailable() > 0)
864 QList<QByteArray> lines = process.readLine().split('\r');
865 while(!lines.isEmpty())
867 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
869 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX) || (ver_mod == UINT_MAX))
876 if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
878 bool ok1 = false, ok2 = false, ok3 = false;
879 unsigned int temp1 = regExpVersionMod.cap(1).toUInt(&ok1);
880 unsigned int temp2 = regExpVersionMod.cap(2).toUInt(&ok2);
881 unsigned int temp3 = regExpVersionMod.cap(3).toUInt(&ok3);
882 if(ok1) ver_maj = temp1;
883 if(ok2) ver_min = temp2;
884 if(ok3) ver_mod = temp3;
886 else if((offset = regExpVersionOld.lastIndexIn(text)) >= 0)
888 bool ok1 = false, ok2 = false;
889 unsigned int temp1 = regExpVersionOld.cap(1).toUInt(&ok1);
890 unsigned int temp2 = regExpVersionOld.cap(2).toUInt(&ok2);
891 if(ok1) ver_maj = temp1;
892 if(ok2) ver_min = temp2;
898 process.waitForFinished();
899 if(process.state() != QProcess::NotRunning)
902 process.waitForFinished(-1);
905 if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 2)))
907 if(!(bTimeout || bAborted))
909 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
914 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX))
916 log(tr("\nFAILED TO DETERMINE AVS2YUV VERSION !!!"));
920 return (ver_maj * REV_MULT) + ((ver_min % REV_MULT) * 10) + (ver_mod % 10);
923 bool EncodeThread::checkVersionVapoursynth(/*const QString &vspipePath*/)
925 //Is VapourSynth available at all?
926 if(m_vpsDir.isEmpty() || (!QFileInfo(VPSP_BINARY(m_vpsDir)).isFile()))
928 log(tr("\nVPY INPUT REQUIRES VAPOURSYNTH, BUT IT IS *NOT* AVAILABLE !!!"));
934 log("\nCreating process:");
935 if(!startProcess(process, VPSP_BINARY(m_vpsDir), QStringList()))
940 QRegExp regExpSignature("\\bVSPipe\\s+usage\\b", Qt::CaseInsensitive);
942 bool bTimeout = false;
943 bool bAborted = false;
945 bool vspipeSignature = false;
947 while(process.state() != QProcess::NotRunning)
955 if(!process.waitForReadyRead())
957 if(process.state() == QProcess::Running)
960 qWarning("VSPipe process timed out <-- killing!");
961 log("\nPROCESS TIMEOUT !!!");
966 while(process.bytesAvailable() > 0)
968 QList<QByteArray> lines = process.readLine().split('\r');
969 while(!lines.isEmpty())
971 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
972 if(regExpSignature.lastIndexIn(text) >= 0)
974 vspipeSignature = true;
984 process.waitForFinished();
985 if(process.state() != QProcess::NotRunning)
988 process.waitForFinished(-1);
991 if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 1)))
993 if(!(bTimeout || bAborted))
995 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
1000 if(!vspipeSignature)
1002 log(tr("\nFAILED TO DETECT VSPIPE SIGNATURE !!!"));
1006 return vspipeSignature;
1009 bool EncodeThread::checkPropertiesAvisynth(bool x64, unsigned int &frames)
1012 QStringList cmdLine;
1014 if(!m_options->customAvs2YUV().isEmpty())
1016 cmdLine.append(splitParams(m_options->customAvs2YUV()));
1019 cmdLine << "-frames" << "1";
1020 cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true)) << "NUL";
1022 log("Creating process:");
1023 if(!startProcess(process, AVS2_BINARY(m_binDir, x64), cmdLine))
1028 QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
1029 QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
1031 QTextCodec *localCodec = QTextCodec::codecForName("System");
1033 bool bTimeout = false;
1034 bool bAborted = false;
1038 unsigned int fpsNom = 0;
1039 unsigned int fpsDen = 0;
1040 unsigned int fSizeW = 0;
1041 unsigned int fSizeH = 0;
1043 unsigned int waitCounter = 0;
1045 while(process.state() != QProcess::NotRunning)
1053 if(!process.waitForReadyRead(m_processTimeoutInterval))
1055 if(process.state() == QProcess::Running)
1057 if(++waitCounter > m_processTimeoutMaxCounter)
1059 if(m_abortOnTimeout)
1062 qWarning("Avs2YUV process timed out <-- killing!");
1063 log("\nPROCESS TIMEOUT !!!");
1064 log("\nAvisynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
1069 else if(waitCounter == m_processTimeoutWarning)
1071 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
1072 log(tr("Warning: Avisynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
1080 while(process.bytesAvailable() > 0)
1082 QList<QByteArray> lines = process.readLine().split('\r');
1083 while(!lines.isEmpty())
1085 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
1087 if((offset = regExpInt.lastIndexIn(text)) >= 0)
1089 bool ok1 = false, ok2 = false;
1090 bool ok3 = false, ok4 = false;
1091 unsigned int temp1 = regExpInt.cap(1).toUInt(&ok1);
1092 unsigned int temp2 = regExpInt.cap(2).toUInt(&ok2);
1093 unsigned int temp3 = regExpInt.cap(3).toUInt(&ok3);
1094 unsigned int temp4 = regExpInt.cap(4).toUInt(&ok4);
1095 if(ok1) fSizeW = temp1;
1096 if(ok2) fSizeH = temp2;
1097 if(ok3) fpsNom = temp3;
1098 if(ok4) frames = temp4;
1100 else if((offset = regExpFrc.lastIndexIn(text)) >= 0)
1102 bool ok1 = false, ok2 = false;
1103 bool ok3 = false, ok4 = false, ok5 = false;
1104 unsigned int temp1 = regExpFrc.cap(1).toUInt(&ok1);
1105 unsigned int temp2 = regExpFrc.cap(2).toUInt(&ok2);
1106 unsigned int temp3 = regExpFrc.cap(3).toUInt(&ok3);
1107 unsigned int temp4 = regExpFrc.cap(4).toUInt(&ok4);
1108 unsigned int temp5 = regExpFrc.cap(5).toUInt(&ok5);
1109 if(ok1) fSizeW = temp1;
1110 if(ok2) fSizeH = temp2;
1111 if(ok3) fpsNom = temp3;
1112 if(ok4) fpsDen = temp4;
1113 if(ok5) frames = temp5;
1119 if(text.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
1121 log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(x64 ? "64" : "32"));
1123 if(text.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
1125 log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
1131 process.waitForFinished();
1132 if(process.state() != QProcess::NotRunning)
1135 process.waitForFinished(-1);
1138 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
1140 if(!(bTimeout || bAborted))
1142 const int exitCode = process.exitCode();
1143 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
1144 if((exitCode < 0) || (exitCode >= 32))
1146 log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
1147 log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
1155 log(tr("\nFAILED TO DETERMINE AVS PROPERTIES !!!"));
1161 if((fSizeW > 0) && (fSizeH > 0))
1163 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
1165 if((fpsNom > 0) && (fpsDen > 0))
1167 log(tr("Frame Rate: %1/%2").arg(QString::number(fpsNom), QString::number(fpsDen)));
1169 if((fpsNom > 0) && (fpsDen == 0))
1171 log(tr("Frame Rate: %1").arg(QString::number(fpsNom)));
1175 log(tr("No. Frames: %1").arg(QString::number(frames)));
1181 bool EncodeThread::checkPropertiesVapoursynth(/*const QString &vspipePath,*/ unsigned int &frames)
1184 QStringList cmdLine;
1186 cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
1187 cmdLine << "-" << "-info";
1189 log("Creating process:");
1190 if(!startProcess(process, VPSP_BINARY(m_vpsDir), cmdLine))
1195 QRegExp regExpFrm("\\bFrames:\\s+(\\d+)\\b");
1196 QRegExp regExpSzW("\\bWidth:\\s+(\\d+)\\b");
1197 QRegExp regExpSzH("\\bHeight:\\s+(\\d+)\\b");
1199 QTextCodec *localCodec = QTextCodec::codecForName("System");
1201 bool bTimeout = false;
1202 bool bAborted = false;
1206 unsigned int fSizeW = 0;
1207 unsigned int fSizeH = 0;
1209 unsigned int waitCounter = 0;
1211 while(process.state() != QProcess::NotRunning)
1219 if(!process.waitForReadyRead(m_processTimeoutInterval))
1221 if(process.state() == QProcess::Running)
1223 if(++waitCounter > m_processTimeoutMaxCounter)
1225 if(m_abortOnTimeout)
1228 qWarning("VSPipe process timed out <-- killing!");
1229 log("\nPROCESS TIMEOUT !!!");
1230 log("\nVapoursynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
1235 else if(waitCounter == m_processTimeoutWarning)
1237 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
1238 log(tr("Warning: nVapoursynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
1246 while(process.bytesAvailable() > 0)
1248 QList<QByteArray> lines = process.readLine().split('\r');
1249 while(!lines.isEmpty())
1251 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
1253 if((offset = regExpFrm.lastIndexIn(text)) >= 0)
1256 unsigned int temp = regExpFrm.cap(1).toUInt(&ok);
1257 if(ok) frames = temp;
1259 if((offset = regExpSzW.lastIndexIn(text)) >= 0)
1262 unsigned int temp = regExpSzW.cap(1).toUInt(&ok);
1263 if(ok) fSizeW = temp;
1265 if((offset = regExpSzH.lastIndexIn(text)) >= 0)
1268 unsigned int temp = regExpSzH.cap(1).toUInt(&ok);
1269 if(ok) fSizeH = temp;
1279 process.waitForFinished();
1280 if(process.state() != QProcess::NotRunning)
1283 process.waitForFinished(-1);
1286 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
1288 if(!(bTimeout || bAborted))
1290 const int exitCode = process.exitCode();
1291 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
1292 if((exitCode < 0) || (exitCode >= 32))
1294 log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
1302 log(tr("\nFAILED TO DETERMINE VPY PROPERTIES !!!"));
1308 if((fSizeW > 0) && (fSizeH > 0))
1310 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
1314 log(tr("No. Frames: %1").arg(QString::number(frames)));
1320 ///////////////////////////////////////////////////////////////////////////////
1322 ///////////////////////////////////////////////////////////////////////////////
1324 void EncodeThread::setStatus(JobStatus newStatus)
1326 if(m_status != newStatus)
1328 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
1330 if(m_status != JobStatus_Paused) setProgress(0);
1332 if(newStatus == JobStatus_Failed)
1334 setDetails("The job has failed. See log for details!");
1336 if(newStatus == JobStatus_Aborted)
1338 setDetails("The job was aborted by the user!");
1340 m_status = newStatus;
1341 emit statusChanged(m_jobId, newStatus);
1345 void EncodeThread::setProgress(unsigned int newProgress)
1347 if(m_progress != newProgress)
1349 m_progress = newProgress;
1350 emit progressChanged(m_jobId, m_progress);
1354 void EncodeThread::setDetails(const QString &text)
1356 emit detailsChanged(m_jobId, text);
1359 QString EncodeThread::stringToHash(const QString &string)
1361 QByteArray result(10, char(0));
1362 const QByteArray hash = QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::Sha1);
1364 if((hash.size() == 20) && (result.size() == 10))
1366 unsigned char *out = reinterpret_cast<unsigned char*>(result.data());
1367 const unsigned char *in = reinterpret_cast<const unsigned char*>(hash.constData());
1368 for(int i = 0; i < 10; i++)
1370 out[i] = (in[i] ^ in[10+i]);
1374 return QString::fromLatin1(result.toHex().constData());
1377 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
1379 QMutexLocker lock(&m_mutex_startProcess);
1380 log(commandline2string(program, args) + "\n");
1382 process.setWorkingDirectory(QDir::tempPath());
1386 process.setProcessChannelMode(QProcess::MergedChannels);
1387 process.setReadChannel(QProcess::StandardOutput);
1391 process.setProcessChannelMode(QProcess::SeparateChannels);
1392 process.setReadChannel(QProcess::StandardError);
1395 process.start(program, args);
1397 if(process.waitForStarted())
1399 m_jobObject->addProcessToJob(&process);
1400 x264_change_process_priority(&process, m_processPriority);
1405 log("Process creation has failed :-(");
1406 QString errorMsg= process.errorString().trimmed();
1407 if(!errorMsg.isEmpty()) log(errorMsg);
1410 process.waitForFinished(-1);
1414 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
1416 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
1418 for(int i = 0; i < arguments.count(); i++)
1420 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
1426 QStringList EncodeThread::splitParams(const QString ¶ms)
1429 bool ignoreWhitespaces = false;
1432 for(int i = 0; i < params.length(); i++)
1434 const QChar c = params.at(i);
1436 if(c == QChar::fromLatin1('"'))
1438 ignoreWhitespaces = (!ignoreWhitespaces);
1441 else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
1443 APPEND_AND_CLEAR(list, temp);
1450 APPEND_AND_CLEAR(list, temp);
1452 list.replaceInStrings("$(INPUT)", QDir::toNativeSeparators(m_sourceFileName), Qt::CaseInsensitive);
1453 list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(m_outputFileName), Qt::CaseInsensitive);
1458 qint64 EncodeThread::estimateSize(int progress)
1462 qint64 currentSize = QFileInfo(m_outputFileName).size();
1463 qint64 estimatedSize = (currentSize * 100I64) / static_cast<qint64>(progress);
1464 return estimatedSize;
1470 QString EncodeThread::sizeToString(qint64 size)
1472 static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
1476 qint64 estimatedSize = size;
1477 qint64 remainderSize = 0I64;
1480 while((estimatedSize > 1024I64) && (prefixIdx < 4))
1482 remainderSize = estimatedSize % 1024I64;
1483 estimatedSize = estimatedSize / 1024I64;
1487 double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
1488 return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
1494 int EncodeThread::getInputType(const QString &fileExt)
1496 int type = INPUT_NATIVE;
1497 if(fileExt.compare("avs", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
1498 else if(fileExt.compare("avsi", Qt::CaseInsensitive) == 0) type = INPUT_AVISYN;
1499 else if(fileExt.compare("vpy", Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;
1500 else if(fileExt.compare("py", Qt::CaseInsensitive) == 0) type = INPUT_VAPOUR;