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"
41 QMutex EncodeThread::m_mutex_startProcess;
46 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) do \
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); \
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); \
66 #define APPEND_AND_CLEAR(LIST, STR) do \
68 if(!((STR).isEmpty())) \
76 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
78 if(ITER != LIST.end()) \
80 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
82 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
83 ITER = LIST.erase(ITER); \
84 if(ITER != LIST.end()) \
86 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
94 #define AVS2_BINARY(BIN_DIR, IS_X64) QString("%1/%2/avs2yuv_%2.exe").arg((BIN_DIR), ((IS_X64) ? "x64" : "x86"))
95 #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"))
100 static const unsigned int REV_MULT = 10000;
102 ///////////////////////////////////////////////////////////////////////////////
103 // Constructor & Destructor
104 ///////////////////////////////////////////////////////////////////////////////
106 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x264_x64, bool x264_10bit, bool avs2yuv_x64)
108 m_jobId(QUuid::createUuid()),
109 m_sourceFileName(sourceFileName),
110 m_outputFileName(outputFileName),
111 m_options(new OptionsModel(*options)),
113 m_x264_x64(x264_x64),
114 m_x264_10bit(x264_10bit),
115 m_avs2yuv_x64(avs2yuv_x64),
116 m_handle_jobObject(NULL),
123 EncodeThread::~EncodeThread(void)
125 X264_DELETE(m_options);
127 if(m_handle_jobObject)
129 CloseHandle(m_handle_jobObject);
130 m_handle_jobObject = NULL;
134 ///////////////////////////////////////////////////////////////////////////////
135 // Thread entry point
136 ///////////////////////////////////////////////////////////////////////////////
138 void EncodeThread::run(void)
146 qWarning("STRUCTURED EXCEPTION ERROR IN ENCODE THREAD !!!");
149 if(m_handle_jobObject)
151 TerminateJobObject(m_handle_jobObject, 42);
152 m_handle_jobObject = NULL;
156 void EncodeThread::checkedRun(void)
159 m_status = JobStatus_Starting;
169 log(tr("EXCEPTION ERROR IN THREAD: ").append(QString::fromLatin1(msg)));
170 setStatus(JobStatus_Failed);
174 log(tr("UNHANDLED EXCEPTION ERROR IN THREAD !!!"));
175 setStatus(JobStatus_Failed);
180 RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 0, NULL);
184 void EncodeThread::start(Priority priority)
186 qDebug("Thread starting...");
191 while(m_semaphorePaused.tryAcquire(1, 0));
192 QThread::start(priority);
195 ///////////////////////////////////////////////////////////////////////////////
197 ///////////////////////////////////////////////////////////////////////////////
199 void EncodeThread::encode(void)
201 QDateTime startTime = QDateTime::currentDateTime();
203 //Print some basic info
204 log(tr("Simple x264 Launcher (Build #%1), built %2\n").arg(QString::number(x264_version_build()), x264_version_date().toString(Qt::ISODate)));
205 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
206 log(tr("Source file: %1").arg(QDir::toNativeSeparators(m_sourceFileName)));
207 log(tr("Output file: %1").arg(QDir::toNativeSeparators(m_outputFileName)));
209 //Print encoder settings
210 log(tr("\n--- SETTINGS ---\n"));
211 log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
212 log(tr("Preset: %1").arg(m_options->preset()));
213 log(tr("Tuning: %1").arg(m_options->tune()));
214 log(tr("Profile: %1").arg(m_options->profile()));
215 log(tr("Custom: %1").arg(m_options->customX264().isEmpty() ? tr("(None)") : m_options->customX264()));
218 unsigned int frames = 0;
221 const bool usePipe = (QFileInfo(m_sourceFileName).suffix().compare("avs", Qt::CaseInsensitive) == 0);
222 const QString indexFile = QString("%1/%2.ffindex").arg(QDir::tempPath(), m_jobId.toString());
224 //Checking x264 version
225 log(tr("\n--- CHECK VERSION ---\n"));
226 unsigned int revision_x264 = UINT_MAX;
227 bool x264_modified = false;
228 ok = ((revision_x264 = checkVersionX264(m_x264_x64, m_x264_10bit, x264_modified)) != UINT_MAX);
229 CHECK_STATUS(m_abort, ok);
231 //Checking avs2yuv version
232 unsigned int revision_avs2yuv = UINT_MAX;
235 ok = ((revision_avs2yuv = checkVersionAvs2yuv(m_avs2yuv_x64)) != UINT_MAX);
236 CHECK_STATUS(m_abort, ok);
240 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()));
241 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)));
243 //Is x264 revision supported?
244 if((revision_x264 % REV_MULT) < (VER_X264_MINIMUM_REV))
246 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(VER_X264_MINIMUM_REV)));
247 setStatus(JobStatus_Failed);
250 if((revision_x264 / REV_MULT) != (VER_X264_CURRENT_API))
252 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
253 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(VER_X264_CURRENT_API)));
255 if((revision_avs2yuv != UINT_MAX) && ((revision_avs2yuv % REV_MULT) != (VER_X264_AVS2YUV_VER)))
257 log(tr("\nERROR: Your version of avs2yuv is unsupported (Required version: v0.24 BugMaster's mod 2)"));
258 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
259 setStatus(JobStatus_Failed);
266 log(tr("\n--- AVS INFO ---\n"));
267 ok = checkProperties(m_avs2yuv_x64, frames);
268 CHECK_STATUS(m_abort, ok);
271 //Run encoding passes
272 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
274 QFileInfo info(m_outputFileName);
275 QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
277 if(QFileInfo(passLogFile).exists())
280 while(QFileInfo(passLogFile).exists())
282 passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
286 log(tr("\n--- PASS 1 ---\n"));
287 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile, 1, passLogFile);
288 CHECK_STATUS(m_abort, ok);
290 log(tr("\n--- PASS 2 ---\n"));
291 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile, 2, passLogFile);
292 CHECK_STATUS(m_abort, ok);
296 log(tr("\n--- ENCODING ---\n"));
297 ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile);
298 CHECK_STATUS(m_abort, ok);
301 log(tr("\n--- DONE ---\n"));
302 if(QFileInfo(indexFile).exists()) QFile::remove(indexFile);
303 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
304 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)));
305 setStatus(JobStatus_Completed);
308 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)
310 QProcess processEncode, processAvisynth;
314 QStringList cmdLine_Avisynth;
315 if(!m_options->customAvs2YUV().isEmpty())
317 cmdLine_Avisynth.append(splitParams(m_options->customAvs2YUV()));
319 cmdLine_Avisynth << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
320 cmdLine_Avisynth << "-";
321 processAvisynth.setStandardOutputProcess(&processEncode);
323 log("Creating Avisynth process:");
324 if(!startProcess(processAvisynth, AVS2_BINARY(m_binDir, avs2yuv_x64), cmdLine_Avisynth, false))
330 QStringList cmdLine_Encode = buildCommandLine(usePipe, x264_10bit, frames, indexFile, pass, passLogFile);
332 log("Creating x264 process:");
333 if(!startProcess(processEncode, X264_BINARY(m_binDir, x264_10bit, x264_x64), cmdLine_Encode))
338 QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
339 QRegExp regExpProgress("\\[(\\d+)\\.(\\d+)%\\].+frames");
340 QRegExp regExpFrameCnt("^(\\d+) frames:");
342 QTextCodec *localCodec = QTextCodec::codecForName("System");
344 bool bTimeout = false;
345 bool bAborted = false;
347 unsigned int last_progress = UINT_MAX;
348 unsigned int last_indexing = UINT_MAX;
349 qint64 size_estimate = 0I64;
351 //Main processing loop
352 while(processEncode.state() != QProcess::NotRunning)
354 unsigned int waitCounter = 0;
356 //Wait until new output is available
361 processEncode.kill();
362 processAvisynth.kill();
366 if(m_pause && (processEncode.state() == QProcess::Running))
368 JobStatus previousStatus = m_status;
369 setStatus(JobStatus_Paused);
370 log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
371 bool ok[2] = {false, false};
372 Q_PID pid[2] = {processEncode.pid(), processAvisynth.pid()};
373 if(pid[0]) { ok[0] = (SuspendThread(pid[0]->hThread) != (DWORD)(-1)); }
374 if(pid[1]) { ok[1] = (SuspendThread(pid[1]->hThread) != (DWORD)(-1)); }
375 while(m_pause) m_semaphorePaused.tryAcquire(1, 5000);
376 while(m_semaphorePaused.tryAcquire(1, 0));
377 if(pid[0]) { if(ok[0]) ResumeThread(pid[0]->hThread); }
378 if(pid[1]) { if(ok[1]) ResumeThread(pid[1]->hThread); }
379 if(!m_abort) setStatus(previousStatus);
380 log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
384 if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
386 if(processEncode.state() == QProcess::Running)
388 if(waitCounter++ > m_processTimeoutMaxCounter)
390 processEncode.kill();
391 qWarning("x264 process timed out <-- killing!");
392 log("\nPROCESS TIMEOUT !!!");
396 else if(waitCounter == m_processTimeoutWarning)
398 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
399 log(tr("Warning: x264 did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
404 if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
411 //Exit main processing loop now?
412 if(bAborted || bTimeout)
418 while(processEncode.bytesAvailable() > 0)
420 QList<QByteArray> lines = processEncode.readLine().split('\r');
421 while(!lines.isEmpty())
423 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
425 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
428 unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
429 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
430 if(ok && ((progress > last_progress) || (last_progress == UINT_MAX)))
432 setProgress(progress);
433 size_estimate = estimateSize(progress);
434 last_progress = progress;
436 setDetails(tr("%1, est. file size %2").arg(text.mid(offset).trimmed(), sizeToString(size_estimate)));
437 last_indexing = UINT_MAX;
439 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
442 unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
443 setStatus(JobStatus_Indexing);
444 if(ok && ((progress > last_indexing) || (last_indexing == UINT_MAX)))
446 setProgress(progress);
447 last_indexing = progress;
449 setDetails(text.mid(offset).trimmed());
450 last_progress = UINT_MAX;
452 else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
454 last_progress = last_indexing = UINT_MAX;
455 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
456 setDetails(text.mid(offset).trimmed());
458 else if(!text.isEmpty())
460 last_progress = last_indexing = UINT_MAX;
467 processEncode.waitForFinished(5000);
468 if(processEncode.state() != QProcess::NotRunning)
470 qWarning("x264 process still running, going to kill it!");
471 processEncode.kill();
472 processEncode.waitForFinished(-1);
475 processAvisynth.waitForFinished(5000);
476 if(processAvisynth.state() != QProcess::NotRunning)
478 qWarning("Avisynth process still running, going to kill it!");
479 processAvisynth.kill();
480 processAvisynth.waitForFinished(-1);
483 if(!(bTimeout || bAborted))
485 while(processAvisynth.bytesAvailable() > 0)
487 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processAvisynth.readLine()).simplified()));
491 if(usePipe && (processAvisynth.exitCode() != EXIT_SUCCESS))
493 if(!(bTimeout || bAborted))
495 log(tr("\nWARNING: Avisynth process exited with error code: %1").arg(QString::number(processAvisynth.exitCode())));
499 if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
501 if(!(bTimeout || bAborted))
503 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
505 processEncode.close();
506 processAvisynth.close();
510 QThread::yieldCurrentThread();
512 const qint64 finalSize = QFileInfo(m_outputFileName).size();
513 QLocale locale(QLocale::English);
514 log(tr("Final file size is %1 bytes.").arg(locale.toString(finalSize)));
519 setStatus(JobStatus_Running_Pass1);
520 setDetails(tr("First pass completed. Preparing for second pass..."));
523 setStatus(JobStatus_Running_Pass2);
524 setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
527 setStatus(JobStatus_Running);
528 setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
533 processEncode.close();
534 processAvisynth.close();
538 QStringList EncodeThread::buildCommandLine(bool usePipe, bool use10Bit, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
541 double crf_int = 0.0, crf_frc = 0.0;
543 switch(m_options->rcMode())
545 case OptionsModel::RCMode_CQ:
546 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
548 case OptionsModel::RCMode_CRF:
549 crf_frc = modf(m_options->quantizer(), &crf_int);
550 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
552 case OptionsModel::RCMode_2Pass:
553 case OptionsModel::RCMode_ABR:
554 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
557 throw "Bad rate-control mode !!!";
561 if((pass == 1) || (pass == 2))
563 cmdLine << "--pass" << QString::number(pass);
564 cmdLine << "--stats" << pathToLocal(QDir::toNativeSeparators(passLogFile), true);
567 cmdLine << "--preset" << m_options->preset().toLower();
569 if(m_options->tune().compare("none", Qt::CaseInsensitive))
571 cmdLine << "--tune" << m_options->tune().toLower();
574 if(m_options->profile().compare("auto", Qt::CaseInsensitive))
578 if(m_options->profile().compare("baseline", Qt::CaseInsensitive) || m_options->profile().compare("main", Qt::CaseInsensitive) || m_options->profile().compare("high", Qt::CaseInsensitive))
580 log(tr("WARNING: Selected H.264 Profile not compatible with 10-Bit encoding. Ignoring!\n"));
584 cmdLine << "--profile" << m_options->profile().toLower();
589 cmdLine << "--profile" << m_options->profile().toLower();
593 if(!m_options->customX264().isEmpty())
595 QStringList customArgs = splitParams(m_options->customX264());
598 QStringList::iterator i = customArgs.begin();
599 while(i != customArgs.end())
601 bool bModified = false;
602 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
603 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
607 cmdLine.append(customArgs);
610 cmdLine << "--output" << pathToLocal(QDir::toNativeSeparators(m_outputFileName), true);
614 if(frames < 1) throw "Frames not set!";
615 cmdLine << "--frames" << QString::number(frames);
616 cmdLine << "--demuxer" << "y4m";
617 cmdLine << "--stdin" << "y4m" << "-";
621 cmdLine << "--index" << pathToLocal(QDir::toNativeSeparators(indexFile), true, false);
622 cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
628 unsigned int EncodeThread::checkVersionX264(bool use_x64, bool use_10bit, bool &modified)
631 QStringList cmdLine = QStringList() << "--version";
633 log("Creating process:");
634 if(!startProcess(process, X264_BINARY(m_binDir, use_10bit, use_x64), cmdLine))
639 QRegExp regExpVersion("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
640 QRegExp regExpVersionMod("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
642 bool bTimeout = false;
643 bool bAborted = false;
645 unsigned int revision = UINT_MAX;
646 unsigned int coreVers = UINT_MAX;
649 while(process.state() != QProcess::NotRunning)
657 if(!process.waitForReadyRead())
659 if(process.state() == QProcess::Running)
662 qWarning("x264 process timed out <-- killing!");
663 log("\nPROCESS TIMEOUT !!!");
668 while(process.bytesAvailable() > 0)
670 QList<QByteArray> lines = process.readLine().split('\r');
671 while(!lines.isEmpty())
673 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
675 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
677 bool ok1 = false, ok2 = false;
678 unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
679 unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
680 if(ok1) coreVers = temp1;
681 if(ok2) revision = temp2;
683 else if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
685 bool ok1 = false, ok2 = false;
686 unsigned int temp1 = regExpVersionMod.cap(2).toUInt(&ok1);
687 unsigned int temp2 = regExpVersionMod.cap(3).toUInt(&ok2);
688 if(ok1) coreVers = temp1;
689 if(ok2) revision = temp2;
700 process.waitForFinished();
701 if(process.state() != QProcess::NotRunning)
704 process.waitForFinished(-1);
707 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
709 if(!(bTimeout || bAborted))
711 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
716 if((revision == UINT_MAX) || (coreVers == UINT_MAX))
718 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
722 return (coreVers * REV_MULT) + (revision % REV_MULT);
725 unsigned int EncodeThread::checkVersionAvs2yuv(bool x64)
729 log("\nCreating process:");
730 if(!startProcess(process, AVS2_BINARY(m_binDir, x64), QStringList()))
735 QRegExp regExpVersionMod("\\bAvs2YUV (\\d+).(\\d+)bm(\\d)\\b", Qt::CaseInsensitive);
736 QRegExp regExpVersionOld("\\bAvs2YUV (\\d+).(\\d+)\\b", Qt::CaseInsensitive);
738 bool bTimeout = false;
739 bool bAborted = false;
741 unsigned int ver_maj = UINT_MAX;
742 unsigned int ver_min = UINT_MAX;
743 unsigned int ver_mod = 0;
745 while(process.state() != QProcess::NotRunning)
753 if(!process.waitForReadyRead())
755 if(process.state() == QProcess::Running)
758 qWarning("Avs2YUV process timed out <-- killing!");
759 log("\nPROCESS TIMEOUT !!!");
764 while(process.bytesAvailable() > 0)
766 QList<QByteArray> lines = process.readLine().split('\r');
767 while(!lines.isEmpty())
769 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
771 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX) || (ver_mod == UINT_MAX))
778 if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
780 bool ok1 = false, ok2 = false, ok3 = false;
781 unsigned int temp1 = regExpVersionMod.cap(1).toUInt(&ok1);
782 unsigned int temp2 = regExpVersionMod.cap(2).toUInt(&ok2);
783 unsigned int temp3 = regExpVersionMod.cap(3).toUInt(&ok3);
784 if(ok1) ver_maj = temp1;
785 if(ok2) ver_min = temp2;
786 if(ok3) ver_mod = temp3;
788 else if((offset = regExpVersionOld.lastIndexIn(text)) >= 0)
790 bool ok1 = false, ok2 = false;
791 unsigned int temp1 = regExpVersionOld.cap(1).toUInt(&ok1);
792 unsigned int temp2 = regExpVersionOld.cap(2).toUInt(&ok2);
793 if(ok1) ver_maj = temp1;
794 if(ok2) ver_min = temp2;
800 process.waitForFinished();
801 if(process.state() != QProcess::NotRunning)
804 process.waitForFinished(-1);
807 if(bTimeout || bAborted || ((process.exitCode() != EXIT_SUCCESS) && (process.exitCode() != 2)))
809 if(!(bTimeout || bAborted))
811 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
816 if((ver_maj == UINT_MAX) || (ver_min == UINT_MAX))
818 log(tr("\nFAILED TO DETERMINE AVS2YUV VERSION !!!"));
822 return (ver_maj * REV_MULT) + ((ver_min % REV_MULT) * 10) + (ver_mod % 10);
825 bool EncodeThread::checkProperties(bool x64, unsigned int &frames)
830 if(!m_options->customAvs2YUV().isEmpty())
832 cmdLine.append(splitParams(m_options->customAvs2YUV()));
835 cmdLine << "-frames" << "1";
836 cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName)) << "NUL";
838 log("Creating process:");
839 if(!startProcess(process, AVS2_BINARY(m_binDir, x64), cmdLine))
844 QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
845 QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
847 QTextCodec *localCodec = QTextCodec::codecForName("System");
849 bool bTimeout = false;
850 bool bAborted = false;
854 unsigned int fpsNom = 0;
855 unsigned int fpsDen = 0;
856 unsigned int fSizeW = 0;
857 unsigned int fSizeH = 0;
859 unsigned int waitCounter = 0;
861 while(process.state() != QProcess::NotRunning)
869 if(!process.waitForReadyRead(m_processTimeoutInterval))
871 if(process.state() == QProcess::Running)
873 if(waitCounter++ > m_processTimeoutMaxCounter)
876 qWarning("Avs2YUV process timed out <-- killing!");
877 log("\nPROCESS TIMEOUT !!!");
878 log("\nAvisynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
882 else if(waitCounter == m_processTimeoutWarning)
884 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
885 log(tr("Warning: Avisynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
893 while(process.bytesAvailable() > 0)
895 QList<QByteArray> lines = process.readLine().split('\r');
896 while(!lines.isEmpty())
898 QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
900 if((offset = regExpInt.lastIndexIn(text)) >= 0)
902 bool ok1 = false, ok2 = false;
903 bool ok3 = false, ok4 = false;
904 unsigned int temp1 = regExpInt.cap(1).toUInt(&ok1);
905 unsigned int temp2 = regExpInt.cap(2).toUInt(&ok2);
906 unsigned int temp3 = regExpInt.cap(3).toUInt(&ok3);
907 unsigned int temp4 = regExpInt.cap(4).toUInt(&ok4);
908 if(ok1) fSizeW = temp1;
909 if(ok2) fSizeH = temp2;
910 if(ok3) fpsNom = temp3;
911 if(ok4) frames = temp4;
913 else if((offset = regExpFrc.lastIndexIn(text)) >= 0)
915 bool ok1 = false, ok2 = false;
916 bool ok3 = false, ok4 = false, ok5 = false;
917 unsigned int temp1 = regExpFrc.cap(1).toUInt(&ok1);
918 unsigned int temp2 = regExpFrc.cap(2).toUInt(&ok2);
919 unsigned int temp3 = regExpFrc.cap(3).toUInt(&ok3);
920 unsigned int temp4 = regExpFrc.cap(4).toUInt(&ok4);
921 unsigned int temp5 = regExpFrc.cap(5).toUInt(&ok5);
922 if(ok1) fSizeW = temp1;
923 if(ok2) fSizeH = temp2;
924 if(ok3) fpsNom = temp3;
925 if(ok4) fpsDen = temp4;
926 if(ok5) frames = temp5;
932 if(text.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
934 log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(x64 ? "64" : "32"));
936 if(text.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
938 log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
944 process.waitForFinished();
945 if(process.state() != QProcess::NotRunning)
948 process.waitForFinished(-1);
951 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
953 if(!(bTimeout || bAborted))
955 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
962 log(tr("\nFAILED TO DETERMINE AVS PROPERTIES !!!"));
968 if((fSizeW > 0) && (fSizeH > 0))
970 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
972 if((fpsNom > 0) && (fpsDen > 0))
974 log(tr("Frame Rate: %1/%2").arg(QString::number(fpsNom), QString::number(fpsDen)));
976 if((fpsNom > 0) && (fpsDen == 0))
978 log(tr("Frame Rate: %1").arg(QString::number(fpsNom)));
982 log(tr("No. Frames: %1").arg(QString::number(frames)));
988 ///////////////////////////////////////////////////////////////////////////////
990 ///////////////////////////////////////////////////////////////////////////////
992 void EncodeThread::setStatus(JobStatus newStatus)
994 if(m_status != newStatus)
996 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
998 if(m_status != JobStatus_Paused) setProgress(0);
1000 if(newStatus == JobStatus_Failed)
1002 setDetails("The job has failed. See log for details!");
1004 if(newStatus == JobStatus_Aborted)
1006 setDetails("The job was aborted by the user!");
1008 m_status = newStatus;
1009 emit statusChanged(m_jobId, newStatus);
1013 void EncodeThread::setProgress(unsigned int newProgress)
1015 if(m_progress != newProgress)
1017 m_progress = newProgress;
1018 emit progressChanged(m_jobId, m_progress);
1022 void EncodeThread::setDetails(const QString &text)
1024 emit detailsChanged(m_jobId, text);
1027 QString EncodeThread::pathToLocal(const QString &longPath, bool create, bool keep)
1029 QTextCodec *localCodec = QTextCodec::codecForName("System");
1031 //Do NOT convert to short, if path can be represented in local Codepage
1032 if(localCodec->toUnicode(localCodec->fromUnicode(longPath)).compare(longPath, Qt::CaseInsensitive) == 0)
1037 //Create dummy file, if required (only existing files can have a short path!)
1039 if((!QFileInfo(longPath).exists()) && create)
1041 tempFile.setFileName(longPath);
1042 tempFile.open(QIODevice::WriteOnly);
1046 DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
1050 wchar_t *buffer = new wchar_t[buffSize];
1051 DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
1053 if(result > 0 && result < buffSize)
1055 shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer));
1062 //Remove the dummy file now (FFMS2 fails, if index file does exist but is empty!)
1063 if(tempFile.isOpen())
1065 if(!keep) tempFile.remove();
1069 if(shortPath.isEmpty())
1071 log(tr("Warning: Failed to convert path \"%1\" to short!\n").arg(longPath));
1074 return (shortPath.isEmpty() ? longPath : shortPath);
1077 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
1079 QMutexLocker lock(&m_mutex_startProcess);
1080 log(commandline2string(program, args) + "\n");
1082 //Create a new job object, if not done yet
1083 if(!m_handle_jobObject)
1085 m_handle_jobObject = CreateJobObject(NULL, NULL);
1086 if(m_handle_jobObject == INVALID_HANDLE_VALUE)
1088 m_handle_jobObject = NULL;
1090 if(m_handle_jobObject)
1092 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
1093 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
1094 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
1095 SetInformationJobObject(m_handle_jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
1101 process.setProcessChannelMode(QProcess::MergedChannels);
1102 process.setReadChannel(QProcess::StandardOutput);
1106 process.setProcessChannelMode(QProcess::SeparateChannels);
1107 process.setReadChannel(QProcess::StandardError);
1110 process.start(program, args);
1112 if(process.waitForStarted())
1114 Q_PID pid = process.pid();
1115 AssignProcessToJobObject(m_handle_jobObject, process.pid()->hProcess);
1118 if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
1120 SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
1128 log("Process creation has failed :-(");
1129 QString errorMsg= process.errorString().trimmed();
1130 if(!errorMsg.isEmpty()) log(errorMsg);
1133 process.waitForFinished(-1);
1137 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
1139 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
1141 for(int i = 0; i < arguments.count(); i++)
1143 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));
1149 QStringList EncodeThread::splitParams(const QString ¶ms)
1152 bool ignoreWhitespaces = false;
1155 for(int i = 0; i < params.length(); i++)
1157 const QChar c = params.at(i);
1159 if(c == QChar::fromLatin1('"'))
1161 ignoreWhitespaces = (!ignoreWhitespaces);
1164 else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
1166 APPEND_AND_CLEAR(list, temp);
1173 APPEND_AND_CLEAR(list, temp);
1175 list.replaceInStrings("$(INPUT)", QDir::toNativeSeparators(m_sourceFileName), Qt::CaseInsensitive);
1176 list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(m_outputFileName), Qt::CaseInsensitive);
1181 qint64 EncodeThread::estimateSize(int progress)
1185 qint64 currentSize = QFileInfo(m_outputFileName).size();
1186 qint64 estimatedSize = (currentSize * 100I64) / static_cast<qint64>(progress);
1187 return estimatedSize;
1193 QString EncodeThread::sizeToString(qint64 size)
1195 static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
1199 qint64 estimatedSize = size;
1200 qint64 remainderSize = 0I64;
1203 while((estimatedSize > 1024I64) && (prefixIdx < 4))
1205 remainderSize = estimatedSize % 1024I64;
1206 estimatedSize = estimatedSize / 1024I64;
1210 double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
1211 return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);