2014-02-22 20:32:46 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Simple x264 Launcher
|
|
|
|
// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "encoder_abstract.h"
|
|
|
|
|
|
|
|
#include "global.h"
|
|
|
|
#include "model_options.h"
|
|
|
|
#include "model_preferences.h"
|
|
|
|
#include "model_sysinfo.h"
|
2014-02-24 19:51:53 +01:00
|
|
|
#include "model_status.h"
|
2014-02-24 23:13:42 +01:00
|
|
|
#include "source_abstract.h"
|
2014-02-22 20:32:46 +01:00
|
|
|
#include "binaries.h"
|
|
|
|
|
|
|
|
#include <QProcess>
|
2014-02-24 19:51:53 +01:00
|
|
|
#include <QDir>
|
|
|
|
#include <QTextCodec>
|
|
|
|
#include <QSemaphore>
|
|
|
|
#include <QDate>
|
|
|
|
#include <QTime>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QLocale>
|
2014-02-22 20:32:46 +01:00
|
|
|
|
2014-02-25 23:52:10 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Helper Macros
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-02-24 19:51:53 +01:00
|
|
|
#define APPEND_AND_CLEAR(LIST, STR) do \
|
|
|
|
{ \
|
|
|
|
if(!((STR).isEmpty())) \
|
|
|
|
{ \
|
|
|
|
(LIST) << (STR); \
|
|
|
|
(STR).clear(); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while(0)
|
|
|
|
|
2014-02-25 23:52:10 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Constructor & Destructor
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-02-24 19:51:53 +01:00
|
|
|
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)
|
2014-02-24 14:57:30 +01:00
|
|
|
:
|
2014-02-24 19:51:53 +01:00
|
|
|
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)))
|
2014-02-24 14:57:30 +01:00
|
|
|
{
|
|
|
|
/*Nothing to do here*/
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractEncoder::~AbstractEncoder(void)
|
|
|
|
{
|
|
|
|
/*Nothing to do here*/
|
|
|
|
}
|
|
|
|
|
2014-02-25 23:52:10 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Encoding Functions
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-02-24 23:13:42 +01:00
|
|
|
bool AbstractEncoder::runEncodingPass(AbstractSource* pipedSource, const QString outputFile, const unsigned int &frames, const int &pass, const QString &passLogFile)
|
2014-02-24 19:51:53 +01:00
|
|
|
{
|
|
|
|
QProcess processEncode, processInput;
|
|
|
|
|
2014-02-24 23:13:42 +01:00
|
|
|
if(pipedSource)
|
2014-02-24 19:51:53 +01:00
|
|
|
{
|
2014-02-24 23:13:42 +01:00
|
|
|
pipedSource->createProcess(processEncode, processInput);
|
2014-02-24 19:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList cmdLine_Encode;
|
2014-02-24 23:13:42 +01:00
|
|
|
buildCommandLine(cmdLine_Encode, (pipedSource != NULL), frames, m_indexFile, pass, passLogFile);
|
2014-02-24 19:51:53 +01:00
|
|
|
|
|
|
|
log("Creating encoder process:");
|
|
|
|
if(!startProcess(processEncode, ENC_BINARY(m_sysinfo, m_options), cmdLine_Encode))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QRegExp*> patterns;
|
|
|
|
runEncodingPass_init(patterns);
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2014-02-25 23:52:10 +01:00
|
|
|
if(*m_abort)
|
2014-02-24 19:51:53 +01:00
|
|
|
{
|
|
|
|
processEncode.kill();
|
|
|
|
processInput.kill();
|
|
|
|
bAborted = true;
|
|
|
|
break;
|
|
|
|
}
|
2014-02-25 23:52:10 +01:00
|
|
|
if((*m_pause) && (processEncode.state() == QProcess::Running))
|
2014-02-24 19:51:53 +01:00
|
|
|
{
|
|
|
|
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);
|
2014-02-25 23:52:10 +01:00
|
|
|
while(*m_pause) m_semaphorePause->tryAcquire(1, 5000);
|
2014-02-24 19:51:53 +01:00
|
|
|
while(m_semaphorePause->tryAcquire(1, 0));
|
|
|
|
ok[0] = x264_suspendProcess(proc[0], false);
|
|
|
|
ok[1] = x264_suspendProcess(proc[1], false);
|
2014-02-25 23:52:10 +01:00
|
|
|
if(!(*m_abort)) setStatus(previousStatus);
|
2014-02-24 19:51:53 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 23:52:10 +01:00
|
|
|
if((*m_abort) || ((*m_pause) && (processEncode.state() == QProcess::Running)))
|
2014-02-24 19:51:53 +01:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Exit main processing loop now?
|
|
|
|
if(bAborted || bTimeout)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Process all output
|
2014-02-25 23:52:10 +01:00
|
|
|
PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, pass);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(bTimeout || bAborted))
|
|
|
|
{
|
|
|
|
PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, pass);
|
2014-02-24 19:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
processEncode.waitForFinished(5000);
|
|
|
|
if(processEncode.state() != QProcess::NotRunning)
|
|
|
|
{
|
|
|
|
qWarning("x264 process still running, going to kill it!");
|
|
|
|
processEncode.kill();
|
|
|
|
processEncode.waitForFinished(-1);
|
|
|
|
}
|
|
|
|
|
2014-02-24 23:13:42 +01:00
|
|
|
if(pipedSource)
|
2014-02-24 19:51:53 +01:00
|
|
|
{
|
2014-02-24 23:13:42 +01:00
|
|
|
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))
|
|
|
|
{
|
|
|
|
pipedSource->flushProcess(processInput);
|
|
|
|
}
|
2014-02-24 19:51:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
while(!patterns.isEmpty())
|
|
|
|
{
|
|
|
|
QRegExp *pattern = patterns.takeFirst();
|
|
|
|
X264_DELETE(pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
|
|
|
|
{
|
|
|
|
if(!(bTimeout || bAborted))
|
|
|
|
{
|
2014-02-26 17:39:36 +01:00
|
|
|
const int exitCode = processEncode.exitCode();
|
|
|
|
if((exitCode < 0) || (exitCode >= 32))
|
|
|
|
{
|
|
|
|
log(tr("\nFATAL ERROR: The encoder process has crashed, your encode probably is *incomplete* !!!"));
|
|
|
|
}
|
|
|
|
log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
|
2014-02-24 19:51:53 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-25 23:52:10 +01:00
|
|
|
// ------------------------------------------------------------
|
|
|
|
// Utilities
|
|
|
|
// ------------------------------------------------------------
|
|
|
|
|
2014-02-24 19:51:53 +01:00
|
|
|
QStringList AbstractEncoder::splitParams(const QString ¶ms, 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");
|
|
|
|
}
|