Refactored runEncodingPass() into AbstractEncoder class and refactored encoder-specific parts into the corresponding Encoder classes.

This commit is contained in:
LoRd_MuldeR 2014-02-24 19:51:53 +01:00
parent 0fa2a16e13
commit 6164e48604
11 changed files with 589 additions and 518 deletions

View File

@ -25,13 +25,34 @@
#include "model_options.h"
#include "model_preferences.h"
#include "model_sysinfo.h"
#include "model_status.h"
#include "binaries.h"
#include <QProcess>
#include <QDir>
#include <QTextCodec>
#include <QSemaphore>
#include <QDate>
#include <QTime>
#include <QThread>
#include <QLocale>
AbstractEncoder::AbstractEncoder(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort)
#define APPEND_AND_CLEAR(LIST, STR) do \
{ \
if(!((STR).isEmpty())) \
{ \
(LIST) << (STR); \
(STR).clear(); \
} \
} \
while(0)
AbstractEncoder::AbstractEncoder(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause, const QString &sourceFile, const QString &outputFile)
:
AbstractTool(jobId, jobObject, options, sysinfo, preferences, abort)
AbstractTool(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause),
m_sourceFile(sourceFile),
m_outputFile(outputFile),
m_indexFile(QString("%1/~%2.ffindex").arg(QDir::tempPath(), stringToHash(m_sourceFile)))
{
/*Nothing to do here*/
}
@ -133,3 +154,312 @@ unsigned int AbstractEncoder::checkVersion(bool &modified)
return (coreVers * REV_MULT) + (revision % REV_MULT);
}
bool AbstractEncoder::runEncodingPass(AbstractSource* source, const QString outputFile, const unsigned int &frames, const int &pass, const QString &passLogFile)
{
QProcess processEncode, processInput;
/*
if(inputType != INPUT_NATIVE)
{
QStringList cmdLine_Input;
processInput.setStandardOutputProcess(&processEncode);
switch(inputType)
{
case INPUT_AVISYN:
if(!m_options->customAvs2YUV().isEmpty())
{
cmdLine_Input.append(splitParams(m_options->customAvs2YUV()));
}
cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
cmdLine_Input << "-";
log("Creating Avisynth process:");
if(!startProcess(processInput, AVS_BINARY(m_sysinfo, m_preferences), cmdLine_Input, false))
{
return false;
}
break;
case INPUT_VAPOUR:
cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
cmdLine_Input << "-" << "-y4m";
log("Creating Vapoursynth process:");
if(!startProcess(processInput, VPS_BINARY(m_sysinfo, m_preferences), cmdLine_Input, false))
{
return false;
}
break;
default:
throw "Bad input type encontered!";
}
}
*/
QStringList cmdLine_Encode;
buildCommandLine(cmdLine_Encode, (source != NULL), frames, m_indexFile, pass, passLogFile);
log("Creating encoder process:");
if(!startProcess(processEncode, ENC_BINARY(m_sysinfo, m_options), cmdLine_Encode))
{
return false;
}
QList<QRegExp*> patterns;
runEncodingPass_init(patterns);
QTextCodec *localCodec = QTextCodec::codecForName("System");
bool bTimeout = false;
bool bAborted = false;
unsigned int last_progress = UINT_MAX;
unsigned int last_indexing = UINT_MAX;
qint64 size_estimate = 0I64;
//Main processing loop
while(processEncode.state() != QProcess::NotRunning)
{
unsigned int waitCounter = 0;
//Wait until new output is available
forever
{
if(m_abort)
{
processEncode.kill();
processInput.kill();
bAborted = true;
break;
}
if(m_pause && (processEncode.state() == QProcess::Running))
{
JobStatus previousStatus = m_jobStatus;
setStatus(JobStatus_Paused);
log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
bool ok[2] = {false, false};
QProcess *proc[2] = { &processEncode, &processInput };
ok[0] = x264_suspendProcess(proc[0], true);
ok[1] = x264_suspendProcess(proc[1], true);
while(m_pause) m_semaphorePause->tryAcquire(1, 5000);
while(m_semaphorePause->tryAcquire(1, 0));
ok[0] = x264_suspendProcess(proc[0], false);
ok[1] = x264_suspendProcess(proc[1], false);
if(!m_abort) setStatus(previousStatus);
log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
waitCounter = 0;
continue;
}
if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
{
if(processEncode.state() == QProcess::Running)
{
if(++waitCounter > m_processTimeoutMaxCounter)
{
if(m_preferences->getAbortOnTimeout())
{
processEncode.kill();
qWarning("encoder process timed out <-- killing!");
log("\nPROCESS TIMEOUT !!!");
bTimeout = true;
break;
}
}
else if(waitCounter == m_processTimeoutWarning)
{
unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
log(tr("Warning: encoder did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
}
continue;
}
}
if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
{
continue;
}
break;
}
//Exit main processing loop now?
if(bAborted || bTimeout)
{
break;
}
//Process all output
while(processEncode.bytesAvailable() > 0)
{
QList<QByteArray> lines = processEncode.readLine().split('\r');
while(!lines.isEmpty())
{
const QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
runEncodingPass_parseLine(text, patterns, pass);
}
}
}
processEncode.waitForFinished(5000);
if(processEncode.state() != QProcess::NotRunning)
{
qWarning("x264 process still running, going to kill it!");
processEncode.kill();
processEncode.waitForFinished(-1);
}
processInput.waitForFinished(5000);
if(processInput.state() != QProcess::NotRunning)
{
qWarning("Input process still running, going to kill it!");
processInput.kill();
processInput.waitForFinished(-1);
}
while(!patterns.isEmpty())
{
QRegExp *pattern = patterns.takeFirst();
X264_DELETE(pattern);
}
if(!(bTimeout || bAborted))
{
while(processInput.bytesAvailable() > 0)
{
/*
switch(inputType)
{
case INPUT_AVISYN:
log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
break;
case INPUT_VAPOUR:
log(tr("vpyp [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
break;
}
*/
}
}
/*
if((inputType != INPUT_NATIVE) && (processInput.exitCode() != EXIT_SUCCESS))
{
if(!(bTimeout || bAborted))
{
const int exitCode = processInput.exitCode();
log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
if((inputType == INPUT_AVISYN) && ((exitCode < 0) || (exitCode >= 32)))
{
log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
}
if((inputType == INPUT_VAPOUR) && ((exitCode < 0) || (exitCode >= 32)))
{
log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
}
}
}
*/
if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
{
if(!(bTimeout || bAborted))
{
log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
}
processEncode.close();
processInput.close();
return false;
}
QThread::yieldCurrentThread();
QFileInfo completedFileInfo(m_outputFile);
const qint64 finalSize = (completedFileInfo.exists() && completedFileInfo.isFile()) ? completedFileInfo.size() : 0;
QLocale locale(QLocale::English);
log(tr("Final file size is %1 bytes.").arg(sizeToString(finalSize)));
switch(pass)
{
case 1:
setStatus(JobStatus_Running_Pass1);
setDetails(tr("First pass completed. Preparing for second pass..."));
break;
case 2:
setStatus(JobStatus_Running_Pass2);
setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
break;
default:
setStatus(JobStatus_Running);
setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
break;
}
setProgress(100);
processEncode.close();
processInput.close();
return true;
}
QStringList AbstractEncoder::splitParams(const QString &params, const QString &sourceFile, const QString &outputFile)
{
QStringList list;
bool ignoreWhitespaces = false;
QString temp;
for(int i = 0; i < params.length(); i++)
{
const QChar c = params.at(i);
if(c == QChar::fromLatin1('"'))
{
ignoreWhitespaces = (!ignoreWhitespaces);
continue;
}
else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
{
APPEND_AND_CLEAR(list, temp);
continue;
}
temp.append(c);
}
APPEND_AND_CLEAR(list, temp);
list.replaceInStrings("$(INPUT)", QDir::toNativeSeparators(sourceFile), Qt::CaseInsensitive);
list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(outputFile), Qt::CaseInsensitive);
return list;
}
qint64 AbstractEncoder::estimateSize(const QString &fileName, const int &progress)
{
QFileInfo fileInfo(fileName);
if((progress >= 3) && fileInfo.exists() && fileInfo.isFile())
{
qint64 currentSize = QFileInfo(fileName).size();
qint64 estimatedSize = (currentSize * 100I64) / static_cast<qint64>(progress);
return estimatedSize;
}
return 0I64;
}
QString AbstractEncoder::sizeToString(qint64 size)
{
static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
if(size > 1024I64)
{
qint64 estimatedSize = size;
qint64 remainderSize = 0I64;
int prefixIdx = 0;
while((estimatedSize > 1024I64) && (prefixIdx < 4))
{
remainderSize = estimatedSize % 1024I64;
estimatedSize = estimatedSize / 1024I64;
prefixIdx++;
}
double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
}
return tr("N/A");
}

View File

@ -25,20 +25,36 @@
class QRegExp;
template<class T> class QList;
class AbstractSource;
class AbstractEncoder : public AbstractTool
{
public:
static const unsigned int REV_MULT = 10000;
AbstractEncoder(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort);
AbstractEncoder(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause, const QString &sourceFile, const QString &outputFile);
virtual ~AbstractEncoder(void);
virtual unsigned int checkVersion(bool &modified);
virtual bool isVersionSupported(const unsigned int &revision, const bool &modified) = 0;
virtual void printVersion(const unsigned int &revision, const bool &modified) = 0;
bool runEncodingPass(AbstractSource* source, const QString outputFile, const unsigned int &frames, const int &pass, const QString &passLogFile);
protected:
virtual void checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine) = 0;
virtual void checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified) = 0;
virtual void buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile) = 0;
virtual void runEncodingPass_init(QList<QRegExp*> &patterns) = 0;
virtual void runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass) = 0;
static QStringList splitParams(const QString &params, const QString &sourceFile, const QString &outputFile);
static qint64 estimateSize(const QString &fileName, const int &progress);
static QString sizeToString(qint64 size);
const QString &m_sourceFile;
const QString &m_outputFile;
const QString m_indexFile;
};

View File

@ -22,17 +22,51 @@
#include "encoder_x264.h"
#include "model_options.h"
#include "model_status.h"
#include <QStringList>
#include <QDir>
#include <QRegExp>
//x264 version info
static const unsigned int X264_VERSION_X264_MINIMUM_REV = 2380;
static const unsigned int X264_VERSION_X264_CURRENT_API = 142;
X264Encoder::X264Encoder(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort)
#define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
{ \
if(ITER != LIST.end()) \
{ \
if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
{ \
log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
ITER = LIST.erase(ITER); \
if(ITER != LIST.end()) \
{ \
if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
} \
FLAG = true; \
} \
} \
} \
while(0)
#define X264_UPDATE_PROGRESS(X) do \
{ \
bool ok = false; qint64 size_estimate = 0; \
unsigned int progress = (X)->cap(1).toUInt(&ok); \
setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
if(ok) \
{ \
setProgress(progress); \
size_estimate = estimateSize(m_outputFile, progress); \
} \
setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(size_estimate))); \
} \
while(0)
X264Encoder::X264Encoder(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause, const QString &sourceFile, const QString &outputFile)
:
AbstractEncoder(jobId, jobObject, options, sysinfo, preferences, abort)
AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
{
if(options->encType() != OptionsModel::EncType_X264)
{
@ -96,3 +130,120 @@ bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &m
return true;
}
void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
{
double crf_int = 0.0, crf_frc = 0.0;
switch(m_options->rcMode())
{
case OptionsModel::RCMode_CQ:
cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
break;
case OptionsModel::RCMode_CRF:
crf_frc = modf(m_options->quantizer(), &crf_int);
cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
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);
}
cmdLine << "--preset" << m_options->preset().toLower();
if(m_options->tune().compare("none", Qt::CaseInsensitive))
{
cmdLine << "--tune" << m_options->tune().toLower();
}
if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
{
if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
{
cmdLine << "--profile" << m_options->profile().toLower();
}
}
if(!m_options->customEncParams().isEmpty())
{
QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
if(usePipe)
{
QStringList::iterator i = customArgs.begin();
while(i != customArgs.end())
{
bool bModified = false;
REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
if(!bModified) i++;
}
}
cmdLine.append(customArgs);
}
cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
if(usePipe)
{
if(frames < 1) throw "Frames not set!";
cmdLine << "--frames" << QString::number(frames);
cmdLine << "--demuxer" << "y4m";
cmdLine << "--stdin" << "y4m" << "-";
}
else
{
cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
cmdLine << QDir::toNativeSeparators(m_sourceFile);
}
}
void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
{
patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames"); //regExpProgress
patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
patterns << new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
}
void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
{
int offset = -1;
if((offset = patterns[0]->lastIndexIn(line)) >= 0)
{
X264_UPDATE_PROGRESS(patterns[0]);
}
else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
{
bool ok = false;
unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
setStatus(JobStatus_Indexing);
if(ok)
{
setProgress(progress);
}
setDetails(line.mid(offset).trimmed());
}
else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
{
setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
setDetails(line.mid(offset).trimmed());
}
else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
{
X264_UPDATE_PROGRESS(patterns[3]);
}
else if(!line.isEmpty())
{
log(line);
}
}

View File

@ -26,13 +26,17 @@
class X264Encoder : public AbstractEncoder
{
public:
X264Encoder(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort);
X264Encoder(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause, const QString &sourceFile, const QString &outputFile);
virtual ~X264Encoder(void);
virtual void printVersion(const unsigned int &revision, const bool &modified);
virtual bool isVersionSupported(const unsigned int &revision, const bool &modified);
virtual void buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile);
protected:
virtual void checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine);
virtual void checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified);
virtual void runEncodingPass_init(QList<QRegExp*> &patterns);
virtual void runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass);
};

View File

@ -30,9 +30,9 @@
static const unsigned int X265_VERSION_X264_MINIMUM_VER = 7;
static const unsigned int X265_VERSION_X264_MINIMUM_REV = 167;
X265Encoder::X265Encoder(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort)
X265Encoder::X265Encoder(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause, const QString &sourceFile, const QString &outputFile)
:
AbstractEncoder(jobId, jobObject, options, sysinfo, preferences, abort)
AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
{
if(options->encType() != OptionsModel::EncType_X265)
{

View File

@ -26,13 +26,17 @@
class X265Encoder : public AbstractEncoder
{
public:
X265Encoder(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort);
X265Encoder(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause, const QString &sourceFile, const QString &outputFile);
virtual ~X265Encoder(void);
virtual void printVersion(const unsigned int &revision, const bool &modified);
virtual bool isVersionSupported(const unsigned int &revision, const bool &modified);
virtual void buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile);
protected:
virtual void checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine);
virtual void checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified);
virtual void runEncodingPass_init(QList<QRegExp*> &patterns);
virtual void runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass);
};

View File

@ -41,11 +41,6 @@
#include <QLocale>
#include <QCryptographicHash>
/*
* Static vars
*/
QMutex EncodeThread::m_mutex_startProcess;
/*
* RAII execution state handler
*/
@ -79,48 +74,18 @@ private:
{ \
log("\nPROCESS ABORTED BY USER !!!"); \
setStatus(JobStatus_Aborted); \
if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
return; \
} \
else if(!(OK_FLAG)) \
{ \
setStatus(JobStatus_Failed); \
if(QFileInfo(indexFile).exists()) QFile::remove(indexFile); \
if(QFileInfo(m_outputFileName).exists() && (QFileInfo(m_outputFileName).size() == 0)) QFile::remove(m_outputFileName); \
return; \
} \
} \
while(0)
#define APPEND_AND_CLEAR(LIST, STR) do \
{ \
if(!((STR).isEmpty())) \
{ \
(LIST) << (STR); \
(STR).clear(); \
} \
} \
while(0)
#define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
{ \
if(ITER != LIST.end()) \
{ \
if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
{ \
log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
ITER = LIST.erase(ITER); \
if(ITER != LIST.end()) \
{ \
if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
} \
FLAG = true; \
} \
} \
} \
while(0)
/*
* Static vars
*/
@ -148,15 +113,20 @@ EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputF
switch(options->encType())
{
case OptionsModel::EncType_X264:
m_encoder = new X264Encoder(&m_jobId, m_jobObject, m_options, m_sysinfo, m_preferences, &m_abort);
m_encoder = new X264Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
break;
case OptionsModel::EncType_X265:
m_encoder = new X265Encoder(&m_jobId, m_jobObject, m_options, m_sysinfo, m_preferences, &m_abort);
m_encoder = new X265Encoder(m_jobObject, m_options, m_sysinfo, m_preferences, m_status, &m_abort, &m_pause, &m_semaphorePaused, m_sourceFileName, m_outputFileName);
break;
default:
throw "Unknown encoder type encountered!";
}
//Establish connections
connect(m_encoder, SIGNAL(statusChanged(JobStatus)), this, SIGNAL(setStatus(QString)), Qt::DirectConnection);
connect(m_encoder, SIGNAL(progressChanged(unsigned int)), this, SIGNAL(setProgress(QString)), Qt::DirectConnection);
connect(m_encoder, SIGNAL(messageLogged(QString)), this, SIGNAL(log(QString)), Qt::DirectConnection);
connect(m_encoder, SIGNAL(detailsChanged(QString)), this, SIGNAL(setDetails(QString)), Qt::DirectConnection);
}
EncodeThread::~EncodeThread(void)
@ -269,7 +239,6 @@ void EncodeThread::encode(void)
//Seletct type of input
const int inputType = getInputType(QFileInfo(m_sourceFileName).suffix());
const QString indexFile = QString("%1/x264_%2.ffindex").arg(QDir::tempPath(), stringToHash(m_sourceFileName));
// -----------------------------------------------------------------------------------
// Check Versions
@ -373,362 +342,6 @@ void EncodeThread::encode(void)
setStatus(JobStatus_Completed);
}
#define X264_UPDATE_PROGRESS(X) do \
{ \
bool ok = false; \
unsigned int progress = (X).cap(1).toUInt(&ok); \
setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
if(ok && ((progress > last_progress) || (last_progress == UINT_MAX))) \
{ \
setProgress(progress); \
size_estimate = estimateSize(progress); \
last_progress = progress; \
} \
setDetails(tr("%1, est. file size %2").arg(text.mid(offset).trimmed(), sizeToString(size_estimate))); \
last_indexing = UINT_MAX; \
} \
while(0)
bool EncodeThread::runEncodingPass(const int &inputType, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
{
QProcess processEncode, processInput;
if(inputType != INPUT_NATIVE)
{
QStringList cmdLine_Input;
processInput.setStandardOutputProcess(&processEncode);
switch(inputType)
{
case INPUT_AVISYN:
if(!m_options->customAvs2YUV().isEmpty())
{
cmdLine_Input.append(splitParams(m_options->customAvs2YUV()));
}
cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
cmdLine_Input << "-";
log("Creating Avisynth process:");
if(!startProcess(processInput, AVS_BINARY(m_sysinfo, m_preferences), cmdLine_Input, false))
{
return false;
}
break;
case INPUT_VAPOUR:
cmdLine_Input << QDir::toNativeSeparators(x264_path2ansi(m_sourceFileName, true));
cmdLine_Input << "-" << "-y4m";
log("Creating Vapoursynth process:");
if(!startProcess(processInput, VPS_BINARY(m_sysinfo, m_preferences), cmdLine_Input, false))
{
return false;
}
break;
default:
throw "Bad input type encontered!";
}
}
QStringList cmdLine_Encode = buildCommandLine((inputType != INPUT_NATIVE), frames, indexFile, pass, passLogFile);
log("Creating x264 process:");
if(!startProcess(processEncode, ENC_BINARY(m_sysinfo, m_options), cmdLine_Encode))
{
return false;
}
QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
QRegExp regExpProgress("\\[(\\d+)\\.(\\d+)%\\].+frames");
QRegExp regExpModified("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)");
QRegExp regExpFrameCnt("^(\\d+) frames:");
QTextCodec *localCodec = QTextCodec::codecForName("System");
bool bTimeout = false;
bool bAborted = false;
unsigned int last_progress = UINT_MAX;
unsigned int last_indexing = UINT_MAX;
qint64 size_estimate = 0I64;
//Main processing loop
while(processEncode.state() != QProcess::NotRunning)
{
unsigned int waitCounter = 0;
//Wait until new output is available
forever
{
if(m_abort)
{
processEncode.kill();
processInput.kill();
bAborted = true;
break;
}
if(m_pause && (processEncode.state() == QProcess::Running))
{
JobStatus previousStatus = m_status;
setStatus(JobStatus_Paused);
log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
bool ok[2] = {false, false};
QProcess *proc[2] = { &processEncode, &processInput };
ok[0] = x264_suspendProcess(proc[0], true);
ok[1] = x264_suspendProcess(proc[1], true);
while(m_pause) m_semaphorePaused.tryAcquire(1, 5000);
while(m_semaphorePaused.tryAcquire(1, 0));
ok[0] = x264_suspendProcess(proc[0], false);
ok[1] = x264_suspendProcess(proc[1], false);
if(!m_abort) setStatus(previousStatus);
log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
waitCounter = 0;
continue;
}
if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
{
if(processEncode.state() == QProcess::Running)
{
if(++waitCounter > m_processTimeoutMaxCounter)
{
if(m_preferences->getAbortOnTimeout())
{
processEncode.kill();
qWarning("x264 process timed out <-- killing!");
log("\nPROCESS TIMEOUT !!!");
bTimeout = true;
break;
}
}
else if(waitCounter == m_processTimeoutWarning)
{
unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
log(tr("Warning: x264 did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
}
continue;
}
}
if(m_abort || (m_pause && (processEncode.state() == QProcess::Running)))
{
continue;
}
break;
}
//Exit main processing loop now?
if(bAborted || bTimeout)
{
break;
}
//Process all output
while(processEncode.bytesAvailable() > 0)
{
QList<QByteArray> lines = processEncode.readLine().split('\r');
while(!lines.isEmpty())
{
QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
int offset = -1;
if((offset = regExpProgress.lastIndexIn(text)) >= 0)
{
X264_UPDATE_PROGRESS(regExpProgress);
}
else if((offset = regExpIndexing.lastIndexIn(text)) >= 0)
{
bool ok = false;
unsigned int progress = regExpIndexing.cap(1).toUInt(&ok);
setStatus(JobStatus_Indexing);
if(ok && ((progress > last_indexing) || (last_indexing == UINT_MAX)))
{
setProgress(progress);
last_indexing = progress;
}
setDetails(text.mid(offset).trimmed());
last_progress = UINT_MAX;
}
else if((offset = regExpFrameCnt.lastIndexIn(text)) >= 0)
{
last_progress = last_indexing = UINT_MAX;
setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
setDetails(text.mid(offset).trimmed());
}
else if((offset = regExpModified.lastIndexIn(text)) >= 0)
{
X264_UPDATE_PROGRESS(regExpModified);
}
else if(!text.isEmpty())
{
last_progress = last_indexing = UINT_MAX;
log(text);
}
}
}
}
processEncode.waitForFinished(5000);
if(processEncode.state() != QProcess::NotRunning)
{
qWarning("x264 process still running, going to kill it!");
processEncode.kill();
processEncode.waitForFinished(-1);
}
processInput.waitForFinished(5000);
if(processInput.state() != QProcess::NotRunning)
{
qWarning("Input process still running, going to kill it!");
processInput.kill();
processInput.waitForFinished(-1);
}
if(!(bTimeout || bAborted))
{
while(processInput.bytesAvailable() > 0)
{
switch(inputType)
{
case INPUT_AVISYN:
log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
break;
case INPUT_VAPOUR:
log(tr("vpyp [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
break;
}
}
}
if((inputType != INPUT_NATIVE) && (processInput.exitCode() != EXIT_SUCCESS))
{
if(!(bTimeout || bAborted))
{
const int exitCode = processInput.exitCode();
log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
if((inputType == INPUT_AVISYN) && ((exitCode < 0) || (exitCode >= 32)))
{
log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
}
if((inputType == INPUT_VAPOUR) && ((exitCode < 0) || (exitCode >= 32)))
{
log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
}
}
}
if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
{
if(!(bTimeout || bAborted))
{
log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(processEncode.exitCode())));
}
processEncode.close();
processInput.close();
return false;
}
QThread::yieldCurrentThread();
const qint64 finalSize = QFileInfo(m_outputFileName).size();
QLocale locale(QLocale::English);
log(tr("Final file size is %1 bytes.").arg(locale.toString(finalSize)));
switch(pass)
{
case 1:
setStatus(JobStatus_Running_Pass1);
setDetails(tr("First pass completed. Preparing for second pass..."));
break;
case 2:
setStatus(JobStatus_Running_Pass2);
setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
break;
default:
setStatus(JobStatus_Running);
setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
break;
}
setProgress(100);
processEncode.close();
processInput.close();
return true;
}
QStringList EncodeThread::buildCommandLine(const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
{
QStringList cmdLine;
double crf_int = 0.0, crf_frc = 0.0;
switch(m_options->rcMode())
{
case OptionsModel::RCMode_CQ:
cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
break;
case OptionsModel::RCMode_CRF:
crf_frc = modf(m_options->quantizer(), &crf_int);
cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
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);
}
cmdLine << "--preset" << m_options->preset().toLower();
if(m_options->tune().compare("none", Qt::CaseInsensitive))
{
cmdLine << "--tune" << m_options->tune().toLower();
}
if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
{
if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
{
cmdLine << "--profile" << m_options->profile().toLower();
}
}
if(!m_options->customEncParams().isEmpty())
{
QStringList customArgs = splitParams(m_options->customEncParams());
if(usePipe)
{
QStringList::iterator i = customArgs.begin();
while(i != customArgs.end())
{
bool bModified = false;
REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
if(!bModified) i++;
}
}
cmdLine.append(customArgs);
}
cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
if(usePipe)
{
if(frames < 1) throw "Frames not set!";
cmdLine << "--frames" << QString::number(frames);
cmdLine << "--demuxer" << "y4m";
cmdLine << "--stdin" << "y4m" << "-";
}
else
{
cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
cmdLine << QDir::toNativeSeparators(m_sourceFileName);
}
return cmdLine;
}
unsigned int EncodeThread::checkVersionAvs2yuv(void)
{
if(!m_sysinfo->hasAVSSupport())
@ -1236,7 +849,12 @@ bool EncodeThread::checkPropertiesVPS(unsigned int &frames)
// Misc functions
///////////////////////////////////////////////////////////////////////////////
void EncodeThread::setStatus(JobStatus newStatus)
void EncodeThread::log(const QString &text)
{
emit messageLogged(m_jobId, text);
}
void EncodeThread::setStatus(const JobStatus &newStatus)
{
if(m_status != newStatus)
{
@ -1257,7 +875,7 @@ void EncodeThread::setStatus(JobStatus newStatus)
}
}
void EncodeThread::setProgress(unsigned int newProgress)
void EncodeThread::setProgress(const unsigned int &newProgress)
{
if(m_progress != newProgress)
{
@ -1271,92 +889,6 @@ void EncodeThread::setDetails(const QString &text)
emit detailsChanged(m_jobId, text);
}
QString EncodeThread::stringToHash(const QString &string)
{
QByteArray result(10, char(0));
const QByteArray hash = QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::Sha1);
if((hash.size() == 20) && (result.size() == 10))
{
unsigned char *out = reinterpret_cast<unsigned char*>(result.data());
const unsigned char *in = reinterpret_cast<const unsigned char*>(hash.constData());
for(int i = 0; i < 10; i++)
{
out[i] = (in[i] ^ in[10+i]);
}
}
return QString::fromLatin1(result.toHex().constData());
}
QStringList EncodeThread::splitParams(const QString &params)
{
QStringList list;
bool ignoreWhitespaces = false;
QString temp;
for(int i = 0; i < params.length(); i++)
{
const QChar c = params.at(i);
if(c == QChar::fromLatin1('"'))
{
ignoreWhitespaces = (!ignoreWhitespaces);
continue;
}
else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
{
APPEND_AND_CLEAR(list, temp);
continue;
}
temp.append(c);
}
APPEND_AND_CLEAR(list, temp);
list.replaceInStrings("$(INPUT)", QDir::toNativeSeparators(m_sourceFileName), Qt::CaseInsensitive);
list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(m_outputFileName), Qt::CaseInsensitive);
return list;
}
qint64 EncodeThread::estimateSize(int progress)
{
if(progress >= 3)
{
qint64 currentSize = QFileInfo(m_outputFileName).size();
qint64 estimatedSize = (currentSize * 100I64) / static_cast<qint64>(progress);
return estimatedSize;
}
return 0I64;
}
QString EncodeThread::sizeToString(qint64 size)
{
static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
if(size > 1024I64)
{
qint64 estimatedSize = size;
qint64 remainderSize = 0I64;
int prefixIdx = 0;
while((estimatedSize > 1024I64) && (prefixIdx < 4))
{
remainderSize = estimatedSize % 1024I64;
estimatedSize = estimatedSize / 1024I64;
prefixIdx++;
}
double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
}
return tr("N/A");
}
int EncodeThread::getInputType(const QString &fileExt)
{
int type = INPUT_NATIVE;

View File

@ -53,11 +53,13 @@ public:
{
m_pause = true;
}
void resumeJob(void)
{
m_pause = false;
m_semaphorePaused.release();
}
void abortJob(void)
{
m_abort = true;
@ -66,11 +68,6 @@ public:
}
protected:
static QMutex m_mutex_startProcess;
static const unsigned int m_processTimeoutInterval = 2500;
static const unsigned int m_processTimeoutMaxCounter = 120;
static const unsigned int m_processTimeoutWarning = 24;
//Globals
const SysinfoModel *const m_sysinfo;
const PreferencesModel *const m_preferences;
@ -120,25 +117,24 @@ protected:
bool checkPropertiesVPS(unsigned int &frames);
//Auxiallary Stuff
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);
QStringList splitParams(const QString &params);
qint64 estimateSize(int progress);
//QStringList splitParams(const QString &params);
//Static functions
static QString sizeToString(qint64 size);
static int getInputType(const QString &fileExt);
static QString stringToHash(const QString &string);
signals:
void statusChanged(const QUuid &jobId, JobStatus newStatus);
void progressChanged(const QUuid &jobId, unsigned int newProgress);
void statusChanged(const QUuid &jobId, const JobStatus &newStatus);
void progressChanged(const QUuid &jobId, const unsigned int &newProgress);
void messageLogged(const QUuid &jobId, const QString &text);
void detailsChanged(const QUuid &jobId, const QString &details);
private slots:
void log(const QString &text);
void setStatus(const JobStatus &newStatus);
void setProgress(const unsigned int &newProgress);
void setDetails(const QString &text);
public slots:
void start(Priority priority = InheritPriority);
};

View File

@ -31,17 +31,20 @@
#include <QProcess>
#include <QMutexLocker>
#include <QDir>
#include <QCryptographicHash>
QMutex AbstractTool::s_mutexStartProcess;
AbstractTool::AbstractTool(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort)
AbstractTool::AbstractTool(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause)
:
m_jobId(jobId),
m_jobObject(jobObject),
m_options(options),
m_sysinfo(sysinfo),
m_preferences(preferences),
m_abort(abort)
m_jobStatus(jobStatus),
m_abort(abort),
m_pause(pause),
m_semaphorePause(semaphorePause)
{
/*nothing to do here*/
}
@ -94,3 +97,21 @@ QString AbstractTool::commandline2string(const QString &program, const QStringLi
return commandline;
}
QString AbstractTool::stringToHash(const QString &string)
{
QByteArray result(10, char(0));
const QByteArray hash = QCryptographicHash::hash(string.toUtf8(), QCryptographicHash::Sha1);
if((hash.size() == 20) && (result.size() == 10))
{
unsigned char *out = reinterpret_cast<unsigned char*>(result.data());
const unsigned char *in = reinterpret_cast<const unsigned char*>(hash.constData());
for(int i = 0; i < 10; i++)
{
out[i] = (in[i] ^ in[10+i]);
}
}
return QString::fromLatin1(result.toHex().constData());
}

View File

@ -30,29 +30,46 @@ class SysinfoModel;
class PreferencesModel;
class JobObject;
class QProcess;
class QSemaphore;
enum JobStatus;
class AbstractTool : QObject
class AbstractTool : public QObject
{
Q_OBJECT
public:
AbstractTool(const QUuid *jobId, JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, volatile bool *abort);
AbstractTool(JobObject *jobObject, const OptionsModel *options, const SysinfoModel *const sysinfo, const PreferencesModel *const preferences, JobStatus &jobStatus, volatile bool *abort, volatile bool *pause, QSemaphore *semaphorePause);
virtual ~AbstractTool(void) {/*NOP*/}
signals:
void messageLogged(const QUuid &jobId, const QString &text);
void statusChanged(const JobStatus &newStatus);
void progressChanged(unsigned int newProgress);
void messageLogged(const QString &text);
void detailsChanged(const QString &details);
protected:
inline void log(const QString &text) { emit messageLogged((*m_jobId), text); }
bool startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels = true);
static QString commandline2string(const QString &program, const QStringList &arguments);
static const unsigned int m_processTimeoutInterval = 2500;
static const unsigned int m_processTimeoutMaxCounter = 120;
static const unsigned int m_processTimeoutWarning = 24;
void log(const QString &text) { emit messageLogged(text); }
void setStatus(const JobStatus &newStatus) { emit statusChanged(newStatus); }
void setProgress(unsigned int newProgress) { emit progressChanged(newProgress); }
void setDetails(const QString &text) { emit detailsChanged(text); }
bool startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels = true);
const QUuid *const m_jobId;
JobObject *const m_jobObject;
const OptionsModel *const m_options;
const SysinfoModel *const m_sysinfo;
const PreferencesModel *const m_preferences;
JobStatus &m_jobStatus;
volatile bool *const m_abort;
volatile bool *const m_pause;
QSemaphore *const m_semaphorePause;
static QString commandline2string(const QString &program, const QStringList &arguments);
static QString stringToHash(const QString &string);
static QMutex s_mutexStartProcess;
};

View File

@ -26,7 +26,7 @@
#define VER_X264_MAJOR 2
#define VER_X264_MINOR 3
#define VER_X264_PATCH 2
#define VER_X264_BUILD 782
#define VER_X264_BUILD 786
#define VER_X264_AVS2YUV_VER 242