1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2012 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"
38 * Win32 API definitions
40 typedef HANDLE (WINAPI *CreateJobObjectFun)(__in_opt LPSECURITY_ATTRIBUTES lpJobAttributes, __in_opt LPCSTR lpName);
41 typedef BOOL (WINAPI *SetInformationJobObjectFun)(__in HANDLE hJob, __in JOBOBJECTINFOCLASS JobObjectInformationClass, __in_bcount(cbJobObjectInformationLength) LPVOID lpJobObjectInformation, __in DWORD cbJobObjectInformationLength);
42 typedef BOOL (WINAPI *AssignProcessToJobObjectFun)(__in HANDLE hJob, __in HANDLE hProcess);
47 QMutex EncodeThread::m_mutex_startProcess;
52 #define CHECK_STATUS(ABORT_FLAG, OK_FLAG) \
56 log("\nPROCESS ABORTED BY USER !!!"); \
57 setStatus(JobStatus_Aborted); \
62 setStatus(JobStatus_Failed); \
70 static const unsigned int REV_MULT = 10000;
72 ///////////////////////////////////////////////////////////////////////////////
73 // Constructor & Destructor
74 ///////////////////////////////////////////////////////////////////////////////
76 EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x64)
78 m_jobId(QUuid::createUuid()),
79 m_sourceFileName(sourceFileName),
80 m_outputFileName(outputFileName),
81 m_options(new OptionsModel(*options)),
84 m_handle_jobObject(NULL)
89 EncodeThread::~EncodeThread(void)
91 X264_DELETE(m_options);
93 if(m_handle_jobObject)
95 CloseHandle(m_handle_jobObject);
96 m_handle_jobObject = NULL;
100 ///////////////////////////////////////////////////////////////////////////////
101 // Thread entry point
102 ///////////////////////////////////////////////////////////////////////////////
104 void EncodeThread::run(void)
107 m_status = JobStatus_Starting;
115 log(tr("EXCEPTION ERROR: ").append(QString::fromLatin1(msg)));
119 log(tr("EXCEPTION ERROR !!!"));
122 if(m_handle_jobObject)
124 CloseHandle(m_handle_jobObject);
125 m_handle_jobObject = NULL;
129 ///////////////////////////////////////////////////////////////////////////////
131 ///////////////////////////////////////////////////////////////////////////////
133 void EncodeThread::encode(void)
135 QDateTime startTime = QDateTime::currentDateTime();
137 //Print some basic info
138 log(tr("Job started at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
139 log(tr("Source file: %1").arg(m_sourceFileName));
140 log(tr("Output file: %1").arg(m_outputFileName));
142 //Print encoder settings
143 log(tr("\n--- SETTINGS ---\n"));
144 log(tr("RC Mode: %1").arg(OptionsModel::rcMode2String(m_options->rcMode())));
145 log(tr("Preset: %1").arg(m_options->preset()));
146 log(tr("Tuning: %1").arg(m_options->tune()));
147 log(tr("Profile: %1").arg(m_options->profile()));
148 log(tr("Custom: %1").arg(m_options->custom().isEmpty() ? tr("(None)") : m_options->custom()));
151 unsigned int frames = 0;
154 bool usePipe = true; //m_x64 && (QFileInfo(m_sourceFileName).suffix().compare("avs", Qt::CaseInsensitive) == 0);
157 log(tr("\n--- AVS INFO ---\n"));
158 ok = checkProperties(frames);
159 CHECK_STATUS(m_abort, ok);
163 log(tr("\n--- X264 VERSION ---\n"));
164 unsigned int revision;
165 ok = ((revision = checkVersion(m_x64)) != UINT_MAX);
166 CHECK_STATUS(m_abort, ok);
168 //Is revision supported?
169 log(tr("\nx264 revision: %1 (core #%2)").arg(QString::number(revision % REV_MULT), QString::number(revision / REV_MULT)));
170 if((revision % REV_MULT) < VER_X264_MINIMUM_REV)
172 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(VER_X264_MINIMUM_REV)));
173 setStatus(JobStatus_Failed);
176 if((revision / REV_MULT) != VER_X264_CURRENT_API)
178 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
179 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(VER_X264_CURRENT_API)));
182 //Run encoding passes
183 if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
185 QFileInfo info(m_outputFileName);
186 QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
188 if(QFileInfo(passLogFile).exists())
191 while(QFileInfo(passLogFile).exists())
193 passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
197 log(tr("\n--- PASS 1 ---\n"));
198 ok = runEncodingPass(m_x64, usePipe, frames, 1, passLogFile);
199 CHECK_STATUS(m_abort, ok);
201 log(tr("\n--- PASS 2 ---\n"));
202 ok = runEncodingPass(m_x64, usePipe, frames, 2, passLogFile);
203 CHECK_STATUS(m_abort, ok);
207 log(tr("\n--- ENCODING ---\n"));
208 ok = runEncodingPass(m_x64, usePipe, frames);
209 CHECK_STATUS(m_abort, ok);
212 log(tr("\n--- DONE ---\n"));
213 int timePassed = startTime.secsTo(QDateTime::currentDateTime());
214 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)));
215 setStatus(JobStatus_Completed);
218 bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames, int pass, const QString &passLogFile)
220 QProcess processEncode, processAvisynth;
224 QStringList cmdLine_Avisynth;
225 cmdLine_Avisynth << QDir::toNativeSeparators(m_sourceFileName);
226 cmdLine_Avisynth << "-";
227 processAvisynth.setStandardOutputProcess(&processEncode);
229 log("Creating Avisynth process:");
230 if(!startProcess(processAvisynth, QString("%1/avs2yuv.exe").arg(m_binDir), cmdLine_Avisynth, false))
236 QStringList cmdLine_Encode = buildCommandLine(usePipe, frames, pass, passLogFile);
238 log("Creating x264 process:");
239 if(!startProcess(processEncode, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine_Encode))
244 QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.\\d+%\\]");
245 QRegExp regExpProgress("\\[(\\d+)\\.\\d+%\\].+frames");
246 QRegExp regExpFrameCnt("^(\\d+) frames:");
248 bool bTimeout = false;
249 bool bAborted = false;
251 while(processEncode.state() != QProcess::NotRunning)
255 processEncode.kill();
256 processAvisynth.kill();
260 if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
262 if(processEncode.state() == QProcess::Running)
264 processEncode.kill();
265 qWarning("x264 process timed out <-- killing!");
266 log("\nPROCESS TIMEOUT !!!");
271 while(processEncode.bytesAvailable() > 0)
273 QList<QByteArray> lines = processEncode.readLine().split('\r');
274 while(!lines.isEmpty())
276 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
278 if((offset = regExpProgress.lastIndexIn(text)) >= 0)
281 unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
282 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
283 setDetails(text.mid(offset).trimmed());
284 if(ok) setProgress(progress);
286 else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
289 unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
290 setStatus(JobStatus_Indexing);
291 setDetails(text.mid(offset).trimmed());
292 if(ok) setProgress(progress);
294 else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
296 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
297 setDetails(text.mid(offset).trimmed());
299 else if(!text.isEmpty())
307 processEncode.waitForFinished(5000);
308 if(processEncode.state() != QProcess::NotRunning)
310 qWarning("x264 process still running, going to kill it!");
311 processEncode.kill();
312 processEncode.waitForFinished(-1);
315 processAvisynth.waitForFinished(5000);
316 if(processAvisynth.state() != QProcess::NotRunning)
318 qWarning("Avisynth process still running, going to kill it!");
319 processAvisynth.kill();
320 processAvisynth.waitForFinished(-1);
323 while(processAvisynth.bytesAvailable() > 0)
325 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processAvisynth.readLine()).simplified()));
328 if(usePipe && (processAvisynth.exitCode() != EXIT_SUCCESS))
330 if(!(bTimeout || bAborted))
332 log(tr("\nWARNING: Avisynth process exited with error code: %1").arg(QString::number(processAvisynth.exitCode())));
336 if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
338 if(!(bTimeout || bAborted))
340 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
342 processEncode.close();
343 processAvisynth.close();
347 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
349 processEncode.close();
350 processAvisynth.close();
354 QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, int pass, const QString &passLogFile)
358 switch(m_options->rcMode())
360 case OptionsModel::RCMode_CRF:
361 cmdLine << "--crf" << QString::number(m_options->quantizer());
363 case OptionsModel::RCMode_CQ:
364 cmdLine << "--qp" << QString::number(m_options->quantizer());
366 case OptionsModel::RCMode_2Pass:
367 case OptionsModel::RCMode_ABR:
368 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
371 throw "Bad rate-control mode !!!";
375 if((pass == 1) || (pass == 2))
377 cmdLine << "--pass" << QString::number(pass);
378 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
381 if(m_options->tune().compare("none", Qt::CaseInsensitive))
383 cmdLine << "--tune" << m_options->tune().toLower();
386 cmdLine << "--preset" << m_options->preset().toLower();
388 if(!m_options->custom().isEmpty())
390 //FIXME: Handle custom parameters that contain spaces!
391 cmdLine.append(m_options->custom().split(" "));
394 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
398 if(frames < 1) throw "Frames not set!";
399 cmdLine << "--frames" << QString::number(frames);
400 cmdLine << "--demuxer" << "y4m";
401 cmdLine << "--stdin" << "y4m" << "-";
405 cmdLine << QDir::toNativeSeparators(m_sourceFileName);
411 unsigned int EncodeThread::checkVersion(bool x64)
414 QStringList cmdLine = QStringList() << "--version";
416 log("Creating process:");
417 if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine))
422 QRegExp regExpVersion("x264 (\\d)\\.(\\d+)\\.(\\d+) ([0-9A-Fa-f]{7})");
424 bool bTimeout = false;
425 bool bAborted = false;
427 unsigned int revision = UINT_MAX;
428 unsigned int coreVers = UINT_MAX;
430 while(process.state() != QProcess::NotRunning)
438 if(!process.waitForReadyRead(m_processTimeoutInterval))
440 if(process.state() == QProcess::Running)
443 qWarning("x264 process timed out <-- killing!");
444 log("\nPROCESS TIMEOUT !!!");
449 while(process.bytesAvailable() > 0)
451 QList<QByteArray> lines = process.readLine().split('\r');
452 while(!lines.isEmpty())
454 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
456 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
458 bool ok1 = false, ok2 = false;
459 unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
460 unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
461 if(ok1) coreVers = temp1;
462 if(ok2) revision = temp2;
472 process.waitForFinished();
473 if(process.state() != QProcess::NotRunning)
476 process.waitForFinished(-1);
479 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
481 if(!(bTimeout || bAborted))
483 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
488 if((revision == UINT_MAX) || (coreVers == UINT_MAX))
490 log(tr("\nFAILED TO DETERMINE X264 VERSION !!!"));
494 return (coreVers * REV_MULT) + revision;
497 bool EncodeThread::checkProperties(unsigned int &frames)
501 QStringList cmdLine = QStringList() << "-frames" << "1";
502 cmdLine << QDir::toNativeSeparators(m_sourceFileName) << "NUL";
504 log("Creating process:");
505 if(!startProcess(process, QString("%1/avs2yuv.exe").arg(m_binDir), cmdLine))
510 QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
511 QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
513 bool bTimeout = false;
514 bool bAborted = false;
518 unsigned int fpsNom = 0;
519 unsigned int fpsDen = 0;
520 unsigned int fSizeW = 0;
521 unsigned int fSizeH = 0;
523 while(process.state() != QProcess::NotRunning)
531 if(!process.waitForReadyRead(m_processTimeoutInterval))
533 if(process.state() == QProcess::Running)
536 qWarning("x264 process timed out <-- killing!");
537 log("\nPROCESS TIMEOUT !!!");
542 while(process.bytesAvailable() > 0)
544 QList<QByteArray> lines = process.readLine().split('\r');
545 while(!lines.isEmpty())
547 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
549 if((offset = regExpInt.lastIndexIn(text)) >= 0)
551 bool ok1 = false, ok2 = false;
552 bool ok3 = false, ok4 = false;
553 unsigned int temp1 = regExpInt.cap(1).toUInt(&ok1);
554 unsigned int temp2 = regExpInt.cap(2).toUInt(&ok2);
555 unsigned int temp3 = regExpInt.cap(3).toUInt(&ok3);
556 unsigned int temp4 = regExpInt.cap(4).toUInt(&ok4);
557 if(ok1) fSizeW = temp1;
558 if(ok2) fSizeH = temp2;
559 if(ok3) fpsNom = temp3;
560 if(ok4) frames = temp4;
562 else if((offset = regExpFrc.lastIndexIn(text)) >= 0)
564 bool ok1 = false, ok2 = false;
565 bool ok3 = false, ok4 = false, ok5 = false;
566 unsigned int temp1 = regExpFrc.cap(1).toUInt(&ok1);
567 unsigned int temp2 = regExpFrc.cap(2).toUInt(&ok2);
568 unsigned int temp3 = regExpFrc.cap(3).toUInt(&ok3);
569 unsigned int temp4 = regExpFrc.cap(4).toUInt(&ok4);
570 unsigned int temp5 = regExpFrc.cap(5).toUInt(&ok5);
571 if(ok1) fSizeW = temp1;
572 if(ok2) fSizeH = temp2;
573 if(ok3) fpsNom = temp3;
574 if(ok4) fpsDen = temp4;
575 if(ok5) frames = temp5;
585 process.waitForFinished();
586 if(process.state() != QProcess::NotRunning)
589 process.waitForFinished(-1);
592 if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
594 if(!(bTimeout || bAborted))
596 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
603 log(tr("\nFAILED TO DETERMINE AVS PROPERTIES !!!"));
609 if((fSizeW > 0) && (fSizeH > 0))
611 log(tr("Resolution: %1x%2").arg(QString::number(fSizeW), QString::number(fSizeH)));
613 if((fpsNom > 0) && (fpsDen > 0))
615 log(tr("Frame Rate: %1/%2").arg(QString::number(fpsNom), QString::number(fpsDen)));
617 if((fpsNom > 0) && (fpsDen == 0))
619 log(tr("Frame Rate: %1").arg(QString::number(fpsNom)));
623 log(tr("No. Frames: %1").arg(QString::number(frames)));
629 ///////////////////////////////////////////////////////////////////////////////
631 ///////////////////////////////////////////////////////////////////////////////
633 void EncodeThread::setStatus(JobStatus newStatus)
635 if(m_status != newStatus)
637 m_status = newStatus;
638 if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted))
642 if(newStatus == JobStatus_Failed)
644 setDetails("The job has failed. See log for details!");
646 if(newStatus == JobStatus_Aborted)
648 setDetails("The job was aborted by the user!");
650 emit statusChanged(m_jobId, newStatus);
654 void EncodeThread::setProgress(unsigned int newProgress)
656 if(m_progress != newProgress)
658 m_progress = newProgress;
659 emit progressChanged(m_jobId, m_progress);
663 void EncodeThread::setDetails(const QString &text)
665 emit detailsChanged(m_jobId, text);
668 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
670 static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL;
671 static CreateJobObjectFun CreateJobObjectPtr = NULL;
672 static SetInformationJobObjectFun SetInformationJobObjectPtr = NULL;
674 QMutexLocker lock(&m_mutex_startProcess);
675 log(commandline2string(program, args) + "\n");
677 //Create a new job object, if not done yet
678 if(!m_handle_jobObject)
680 if(!CreateJobObjectPtr || !SetInformationJobObjectPtr)
682 QLibrary Kernel32Lib("kernel32.dll");
683 CreateJobObjectPtr = (CreateJobObjectFun) Kernel32Lib.resolve("CreateJobObjectA");
684 SetInformationJobObjectPtr = (SetInformationJobObjectFun) Kernel32Lib.resolve("SetInformationJobObject");
686 if(CreateJobObjectPtr && SetInformationJobObjectPtr)
688 m_handle_jobObject = CreateJobObjectPtr(NULL, NULL);
689 if(m_handle_jobObject == INVALID_HANDLE_VALUE)
691 m_handle_jobObject = NULL;
693 if(m_handle_jobObject)
695 JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo;
696 memset(&jobExtendedLimitInfo, 0, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
697 jobExtendedLimitInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_DIE_ON_UNHANDLED_EXCEPTION;
698 SetInformationJobObjectPtr(m_handle_jobObject, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
703 //Initialize AssignProcessToJobObject function
704 if(!AssignProcessToJobObjectPtr)
706 QLibrary Kernel32Lib("kernel32.dll");
707 AssignProcessToJobObjectPtr = (AssignProcessToJobObjectFun) Kernel32Lib.resolve("AssignProcessToJobObject");
712 process.setProcessChannelMode(QProcess::MergedChannels);
713 process.setReadChannel(QProcess::StandardOutput);
717 process.setProcessChannelMode(QProcess::SeparateChannels);
718 process.setReadChannel(QProcess::StandardError);
721 process.start(program, args);
723 if(process.waitForStarted())
725 if(AssignProcessToJobObjectPtr)
727 AssignProcessToJobObjectPtr(m_handle_jobObject, process.pid()->hProcess);
729 if(!SetPriorityClass(process.pid()->hProcess, BELOW_NORMAL_PRIORITY_CLASS))
731 SetPriorityClass(process.pid()->hProcess, IDLE_PRIORITY_CLASS);
738 log("Process creation has failed :-(");
739 QString errorMsg= process.errorString().trimmed();
740 if(!errorMsg.isEmpty()) log(errorMsg);
743 process.waitForFinished(-1);
747 QString EncodeThread::commandline2string(const QString &program, const QStringList &arguments)
749 QString commandline = (program.contains(' ') ? QString("\"%1\"").arg(program) : program);
751 for(int i = 0; i < arguments.count(); i++)
753 commandline += (arguments.at(i).contains(' ') ? QString(" \"%1\"").arg(arguments.at(i)) : QString(" %1").arg(arguments.at(i)));