Implemented 2-Pass support + various fixes.

This commit is contained in:
LoRd_MuldeR 2012-01-30 04:58:42 +01:00
parent 65c7aba23a
commit 8ebfc46240
5 changed files with 191 additions and 39 deletions

View File

@ -20,6 +20,9 @@
<property name="orientation"> <property name="orientation">
<enum>Qt::Vertical</enum> <enum>Qt::Vertical</enum>
</property> </property>
<property name="opaqueResize">
<bool>false</bool>
</property>
<property name="handleWidth"> <property name="handleWidth">
<number>8</number> <number>8</number>
</property> </property>

View File

@ -250,7 +250,8 @@ bool JobListModel::abortJob(const QModelIndex &index)
if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count()) if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
{ {
QUuid id = m_jobs.at(index.row()); QUuid id = m_jobs.at(index.row());
if(m_status.value(id) == EncodeThread::JobStatus_Indexing || m_status.value(id) == EncodeThread::JobStatus_Running) if(m_status.value(id) == EncodeThread::JobStatus_Indexing || m_status.value(id) == EncodeThread::JobStatus_Running ||
m_status.value(id) == EncodeThread::JobStatus_Running_Pass1 || EncodeThread::JobStatus_Running_Pass2)
{ {
updateStatus(id, EncodeThread::JobStatus_Aborting); updateStatus(id, EncodeThread::JobStatus_Aborting);
m_threads.value(id)->abortJob(); m_threads.value(id)->abortJob();

View File

@ -26,6 +26,8 @@
#include <QDate> #include <QDate>
#include <QTime> #include <QTime>
#include <QFileInfo>
#include <QDir>
#include <QProcess> #include <QProcess>
#include <QMutex> #include <QMutex>
#include <QLibrary> #include <QLibrary>
@ -71,15 +73,17 @@ void EncodeThread::run(void)
{ {
try try
{ {
m_progress = 0;
m_status = JobStatus_Starting;
encode(); encode();
} }
catch(char *msg) catch(char *msg)
{ {
emit messageLogged(m_jobId, QString("EXCEPTION ERROR: ").append(QString::fromLatin1(msg))); log(tr("EXCEPTION ERROR: ").append(QString::fromLatin1(msg)));
} }
catch(...) catch(...)
{ {
emit messageLogged(m_jobId, QString("EXCEPTION ERROR !!!")); log(tr("EXCEPTION ERROR !!!"));
} }
} }
@ -106,20 +110,88 @@ void EncodeThread::encode(void)
log(tr("\n[Input Properties]")); log(tr("\n[Input Properties]"));
log(tr("Not implemented yet, sorry ;-)\n")); log(tr("Not implemented yet, sorry ;-)\n"));
QStringList cmdLine; bool ok = false;
QProcess process;
cmdLine = buildCommandLine(); //Run encoding passes
if(m_options->rcMode() == OptionsModel::RCMode_2Pass)
{
QFileInfo info(m_outputFileName);
QString passLogFile = QString("%1/%2.stats").arg(info.path(), info.completeBaseName());
if(QFileInfo(passLogFile).exists())
{
int n = 2;
while(QFileInfo(passLogFile).exists())
{
passLogFile = QString("%1/%2.%3.stats").arg(info.path(), info.completeBaseName(), QString::number(n++));
}
}
log("--- PASS 1 ---\n");
ok = runEncodingPass(1, passLogFile);
if(m_abort)
{
log("\nPROCESS ABORTED BY USER !!!");
setStatus(JobStatus_Aborted);
return;
}
else if(!ok)
{
setStatus(JobStatus_Failed);
return;
}
log("\n--- PASS 2 ---\n");
ok = runEncodingPass(2, passLogFile);
if(m_abort)
{
log("\nPROCESS ABORTED BY USER !!!");
setStatus(JobStatus_Aborted);
return;
}
else if(!ok)
{
setStatus(JobStatus_Failed);
return;
}
}
else
{
log("--- ENCODING ---\n");
ok = runEncodingPass();
if(m_abort)
{
log("\nPROCESS ABORTED BY USER !!!");
setStatus(JobStatus_Aborted);
return;
}
else if(!ok)
{
setStatus(JobStatus_Failed);
return;
}
}
log(tr("\nJob finished at %1, %2.\n").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
setStatus(JobStatus_Completed);
}
bool EncodeThread::runEncodingPass(int pass, const QString &passLogFile)
{
QProcess process;
QStringList cmdLine = buildCommandLine(pass, passLogFile);
log("Creating process:"); log("Creating process:");
if(!startProcess(process, QString("%1/x264.exe").arg(m_binDir), cmdLine)) if(!startProcess(process, QString("%1/x264.exe").arg(m_binDir), cmdLine))
{ {
emit statusChanged(m_jobId, JobStatus_Failed); return false;;
return;
} }
emit statusChanged(m_jobId, JobStatus_Running); QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.\\d+%\\]");
QRegExp regExp("\\[(\\d+)\\.\\d+%\\].+frames"); QRegExp regExpProgress("\\[(\\d+)\\.\\d+%\\].+frames");
bool bTimeout = false; bool bTimeout = false;
bool bAborted = false; bool bAborted = false;
@ -130,35 +202,46 @@ void EncodeThread::encode(void)
{ {
process.kill(); process.kill();
bAborted = true; bAborted = true;
log("\nABORTED BY USER !!!");
break; break;
} }
process.waitForReadyRead(m_processTimeoutInterval); if(!process.waitForReadyRead(m_processTimeoutInterval))
if(!process.bytesAvailable() && process.state() == QProcess::Running)
{ {
process.kill(); if(process.state() == QProcess::Running)
qWarning("x264 process timed out <-- killing!"); {
log("\nPROCESS TIMEOUT !!!"); process.kill();
bTimeout = true; qWarning("x264 process timed out <-- killing!");
break; log("\nPROCESS TIMEOUT !!!");
bTimeout = true;
break;
}
} }
while(process.bytesAvailable() > 0) while(process.bytesAvailable() > 0)
{ {
QByteArray line = process.readLine(); QList<QByteArray> lines = process.readLine().split('\r');
QString text = QString::fromUtf8(line.constData()).simplified(); while(!lines.isEmpty())
if(regExp.lastIndexIn(text) >= 0)
{ {
bool ok = false; QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
unsigned int progress = regExp.cap(1).toUInt(&ok); int offset = -1;
if(ok) if((offset = regExpProgress.lastIndexIn(text)) >= 0)
{ {
emit progressChanged(m_jobId, progress); bool ok = false;
emit detailsChanged(m_jobId, line); unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
if(ok) setProgress(progress);
setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
setDetails(text.mid(offset).trimmed());
}
else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
{
bool ok = false;
unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
if(ok) setProgress(progress);
setStatus(JobStatus_Indexing);
setDetails(text.mid(offset).trimmed());
}
else if(!text.isEmpty())
{
log(text);
} }
}
else if(!text.isEmpty())
{
log(text);
} }
} }
} }
@ -172,27 +255,55 @@ void EncodeThread::encode(void)
if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS) if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
{ {
emit statusChanged(m_jobId, JobStatus_Failed); return false;
return;
} }
emit progressChanged(m_jobId, 100); setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
emit statusChanged(m_jobId, JobStatus_Completed); setProgress(100);
return true;
} }
QStringList EncodeThread::buildCommandLine(void) QStringList EncodeThread::buildCommandLine(int pass, const QString &passLogFile)
{ {
QStringList cmdLine; QStringList cmdLine;
cmdLine << "--crf" << QString::number(m_options->quantizer()); switch(m_options->rcMode())
{
case OptionsModel::RCMode_CRF:
cmdLine << "--crf" << QString::number(m_options->quantizer());
break;
case OptionsModel::RCMode_CQ:
cmdLine << "--qp" << QString::number(m_options->quantizer());
break;
case OptionsModel::RCMode_2Pass:
case OptionsModel::RCMode_ABR:
cmdLine << "--bitrate" << QString::number(m_options->bitrate());
break;
default:
throw "Bad rate-control mode !!!";
break;
}
if((pass == 1) || (pass == 2))
{
cmdLine << "--pass" << QString::number(pass);
cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
}
if(m_options->tune().compare("none", Qt::CaseInsensitive)) if(m_options->tune().compare("none", Qt::CaseInsensitive))
{ {
cmdLine << "--tune" << m_options->tune().toLower(); cmdLine << "--tune" << m_options->tune().toLower();
} }
cmdLine << "--preset" << m_options->preset().toLower(); cmdLine << "--preset" << m_options->preset().toLower();
cmdLine << "--output" << m_outputFileName;
if(!m_options->custom().isEmpty())
{
//FIXME: Handle custom parameters that contain spaces!
cmdLine.append(m_options->custom().split(" "));
}
cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
cmdLine << m_sourceFileName; cmdLine << m_sourceFileName;
return cmdLine; return cmdLine;
@ -202,6 +313,30 @@ QStringList EncodeThread::buildCommandLine(void)
// Misc functions // Misc functions
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void EncodeThread::setStatus(JobStatus newStatus)
{
if(m_status != newStatus)
{
m_status = newStatus;
emit statusChanged(m_jobId, newStatus);
setProgress(0);
}
}
void EncodeThread::setProgress(unsigned int newProgress)
{
if(m_progress != newProgress)
{
m_progress = newProgress;
emit progressChanged(m_jobId, m_progress);
}
}
void EncodeThread::setDetails(const QString &text)
{
emit detailsChanged(m_jobId, text);
}
bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args) bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args)
{ {
static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL; static AssignProcessToJobObjectFun AssignProcessToJobObjectPtr = NULL;

View File

@ -61,23 +61,35 @@ protected:
static QMutex m_mutex_startProcess; static QMutex m_mutex_startProcess;
static void *m_handle_jobObject; static void *m_handle_jobObject;
static const int m_processTimeoutInterval = 600000; static const int m_processTimeoutInterval = 60000;
//Constants
const QUuid m_jobId; const QUuid m_jobId;
const QString m_sourceFileName; const QString m_sourceFileName;
const QString m_outputFileName; const QString m_outputFileName;
const OptionsModel *m_options; const OptionsModel *m_options;
const QString m_binDir; const QString m_binDir;
//Flags
volatile bool m_abort; volatile bool m_abort;
//Internal status values
JobStatus m_status;
unsigned int m_progress;
//Entry point
virtual void run(void); virtual void run(void);
//Encode functions //Encode functions
void encode(void); void encode(void);
QStringList buildCommandLine(void); bool runEncodingPass(int pass = 0, const QString &passLogFile = QString());
QStringList buildCommandLine(int pass = 0, const QString &passLogFile = QString());
//Auxiallary Stuff //Auxiallary Stuff
void log(const QString &text) { emit messageLogged(m_jobId, text); } void log(const QString &text) { emit messageLogged(m_jobId, text); }
inline void setStatus(JobStatus newStatus);
inline void setProgress(unsigned int newProgress);
inline void setDetails(const QString &text);
bool startProcess(QProcess &process, const QString &program, const QStringList &args); bool startProcess(QProcess &process, const QString &program, const QStringList &args);
static QString commandline2string(const QString &program, const QStringList &arguments); static QString commandline2string(const QString &program, const QStringList &arguments);

View File

@ -56,6 +56,7 @@ MainWindow::MainWindow(bool x64supported)
//Freeze minimum size //Freeze minimum size
setMinimumSize(size()); setMinimumSize(size());
splitter->setSizes(QList<int>() << 200 << SHRT_MAX);
//Update title //Update title
labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time()))); labelBuildDate->setText(tr("Built on %1 at %2").arg(x264_version_date().toString(Qt::ISODate), QString::fromLatin1(x264_version_time())));