OSDN Git Service

Added option to choose between 8-Bit and 10-Bit encoding at runtime. We now include...
[x264-launcher/x264-launcher.git] / src / thread_encode.cpp
index 6ec86cf..3595e6f 100644 (file)
@@ -32,6 +32,8 @@
 #include <QDir>
 #include <QProcess>
 #include <QMutex>
+#include <QTextCodec>
+#include <QLocale>
 
 /*
  * Static vars
@@ -47,15 +49,30 @@ QMutex EncodeThread::m_mutex_startProcess;
        { \
                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; \
        } \
 }
 
+#define APPEND_AND_CLEAR(LIST, STR) \
+{ \
+       if(!((STR).isEmpty())) \
+       { \
+               (LIST) << (STR); \
+               (STR).clear(); \
+       } \
+}
+
+#define X264_BINARY(BIN_DIR, IS_10BIT, IS_X64) QString("%1/x264_%2_%3.exe").arg((BIN_DIR), ((IS_10BIT) ? "10bit" : "8bit"), ((IS_X64) ? "x64" : "x86"))
+
 /*
  * Static vars
  */
@@ -65,14 +82,16 @@ static const unsigned int REV_MULT = 10000;
 // Constructor & Destructor
 ///////////////////////////////////////////////////////////////////////////////
 
-EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x64)
+EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x264_x64, bool x264_10bit, bool avs2yuv_x64)
 :
        m_jobId(QUuid::createUuid()),
        m_sourceFileName(sourceFileName),
        m_outputFileName(outputFileName),
        m_options(new OptionsModel(*options)),
        m_binDir(binDir),
-       m_x64(x64),
+       m_x264_x64(x264_x64),
+       m_x264_10bit(x264_10bit),
+       m_avs2yuv_x64(avs2yuv_x64),
        m_handle_jobObject(NULL),
        m_semaphorePaused(0)
 {
@@ -171,26 +190,27 @@ void EncodeThread::encode(void)
        log(tr("Preset:  %1").arg(m_options->preset()));
        log(tr("Tuning:  %1").arg(m_options->tune()));
        log(tr("Profile: %1").arg(m_options->profile()));
-       log(tr("Custom:  %1").arg(m_options->custom().isEmpty() ? tr("(None)") : m_options->custom()));
+       log(tr("Custom:  %1").arg(m_options->customX264().isEmpty() ? tr("(None)") : m_options->customX264()));
        
        bool ok = false;
        unsigned int frames = 0;
 
        //Use Avisynth?
        const bool usePipe = (QFileInfo(m_sourceFileName).suffix().compare("avs", Qt::CaseInsensitive) == 0);
+       const QString indexFile = QString("%1/%2.ffindex").arg(QDir::tempPath(), m_jobId.toString());
 
        //Checking x264 version
        log(tr("\n--- CHECK VERSION ---\n"));
        unsigned int revision_x264 = UINT_MAX;
        bool x264_modified = false;
-       ok = ((revision_x264 = checkVersionX264(m_x64, x264_modified)) != UINT_MAX);
+       ok = ((revision_x264 = checkVersionX264(m_x264_x64, m_x264_10bit, x264_modified)) != UINT_MAX);
        CHECK_STATUS(m_abort, ok);
        
        //Checking avs2yuv version
        unsigned int revision_avs2yuv = UINT_MAX;
        if(usePipe)
        {
-               ok = ((revision_avs2yuv = checkVersionAvs2yuv()) != UINT_MAX);
+               ok = ((revision_avs2yuv = checkVersionAvs2yuv(m_avs2yuv_x64)) != UINT_MAX);
                CHECK_STATUS(m_abort, ok);
        }
 
@@ -199,18 +219,18 @@ void EncodeThread::encode(void)
        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)));
 
        //Is x264 revision supported?
-       if((revision_x264 % REV_MULT) < VER_X264_MINIMUM_REV)
+       if((revision_x264 % REV_MULT) < (VER_X264_MINIMUM_REV))
        {
                log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %2)").arg(QString::number(VER_X264_MINIMUM_REV)));
                setStatus(JobStatus_Failed);
                return;
        }
-       if((revision_x264 / REV_MULT) != VER_X264_CURRENT_API)
+       if((revision_x264 / REV_MULT) != (VER_X264_CURRENT_API))
        {
                log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
                log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(VER_X264_CURRENT_API)));
        }
-       if((revision_avs2yuv != UINT_MAX) && ((revision_avs2yuv % REV_MULT) != VER_x264_AVS2YUV_VER))
+       if((revision_avs2yuv != UINT_MAX) && ((revision_avs2yuv % REV_MULT) != (VER_X264_AVS2YUV_VER)))
        {
                log(tr("\nERROR: Your version of avs2yuv is unsupported (Required version: v0.24 BugMaster's mod 2)"));
                log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
@@ -222,7 +242,7 @@ void EncodeThread::encode(void)
        if(usePipe)
        {
                log(tr("\n--- AVS INFO ---\n"));
-               ok = checkProperties(frames);
+               ok = checkProperties(m_avs2yuv_x64, frames);
                CHECK_STATUS(m_abort, ok);
        }
 
@@ -242,59 +262,70 @@ void EncodeThread::encode(void)
                }
                
                log(tr("\n--- PASS 1 ---\n"));
-               ok = runEncodingPass(m_x64, usePipe, frames, 1, passLogFile);
+               ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile, 1, passLogFile);
                CHECK_STATUS(m_abort, ok);
 
                log(tr("\n--- PASS 2 ---\n"));
-               ok = runEncodingPass(m_x64, usePipe, frames, 2, passLogFile);
+               ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile, 2, passLogFile);
                CHECK_STATUS(m_abort, ok);
        }
        else
        {
                log(tr("\n--- ENCODING ---\n"));
-               ok = runEncodingPass(m_x64, usePipe, frames);
+               ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile);
                CHECK_STATUS(m_abort, ok);
        }
 
        log(tr("\n--- DONE ---\n"));
+       if(QFileInfo(indexFile).exists()) QFile::remove(indexFile);
        int timePassed = startTime.secsTo(QDateTime::currentDateTime());
        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)));
        setStatus(JobStatus_Completed);
 }
 
-bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames, int pass, const QString &passLogFile)
+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)
 {
        QProcess processEncode, processAvisynth;
        
        if(usePipe)
        {
                QStringList cmdLine_Avisynth;
-               cmdLine_Avisynth << QDir::toNativeSeparators(m_sourceFileName);
+               if(!m_options->customAvs2YUV().isEmpty())
+               {
+                       cmdLine_Avisynth.append(splitParams(m_options->customAvs2YUV()));
+               }
+               cmdLine_Avisynth << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
                cmdLine_Avisynth << "-";
                processAvisynth.setStandardOutputProcess(&processEncode);
 
                log("Creating Avisynth process:");
-               if(!startProcess(processAvisynth, QString("%1/avs2yuv.exe").arg(m_binDir), cmdLine_Avisynth, false))
+               if(!startProcess(processAvisynth, QString("%1/%2.exe").arg(m_binDir, avs2yuv_x64 ? "avs2yuv_x64" : "avs2yuv"), cmdLine_Avisynth, false))
                {
                        return false;
                }
        }
 
-       QStringList cmdLine_Encode = buildCommandLine(usePipe, frames, pass, passLogFile);
+       QStringList cmdLine_Encode = buildCommandLine(usePipe, x264_10bit, frames, indexFile, pass, passLogFile);
 
        log("Creating x264 process:");
-       if(!startProcess(processEncode, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine_Encode))
+       if(!startProcess(processEncode, X264_BINARY(m_binDir, x264_10bit, x264_x64), cmdLine_Encode))
        {
                return false;
        }
 
-       QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.\\d+%\\]");
-       QRegExp regExpProgress("\\[(\\d+)\\.\\d+%\\].+frames");
+       QRegExp regExpIndexing("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
+       QRegExp regExpProgress("\\[(\\d+)\\.(\\d+)%\\].+frames");
        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)
        {
@@ -328,11 +359,11 @@ bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames,
                                waitCounter = 0;
                                continue;
                        }
-                       if(!processEncode.waitForReadyRead(2500))
+                       if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
                        {
                                if(processEncode.state() == QProcess::Running)
                                {
-                                       if(waitCounter++ > m_processTimeoutCounter)
+                                       if(waitCounter++ > m_processTimeoutMaxCounter)
                                        {
                                                processEncode.kill();
                                                qWarning("x264 process timed out <-- killing!");
@@ -340,6 +371,11 @@ bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames,
                                                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;
                                }
                        }
@@ -362,31 +398,44 @@ bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames,
                        QList<QByteArray> lines = processEncode.readLine().split('\r');
                        while(!lines.isEmpty())
                        {
-                               QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
+                               QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
                                int offset = -1;
                                if((offset = regExpProgress.lastIndexIn(text)) >= 0)
                                {
                                        bool ok = false;
                                        unsigned int progress = regExpProgress.cap(1).toUInt(&ok);
                                        setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
-                                       setDetails(text.mid(offset).trimmed());
-                                       if(ok) setProgress(progress);
+                                       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;
                                }
                                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());
-                                       if(ok) setProgress(progress);
+                                       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(!text.isEmpty())
                                {
+                                       last_progress = last_indexing = UINT_MAX;
                                        log(text);
                                }
                        }
@@ -436,6 +485,12 @@ bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames,
                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:
@@ -444,11 +499,11 @@ bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames,
                break;
        case 2:
                setStatus(JobStatus_Running_Pass2);
-               setDetails(tr("Second pass completed successfully."));
+               setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
                break;
        default:
                setStatus(JobStatus_Running);
-               setDetails(tr("Encode completed successfully."));
+               setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
                break;
        }
 
@@ -458,17 +513,19 @@ bool EncodeThread::runEncodingPass(bool x64, bool usePipe, unsigned int frames,
        return true;
 }
 
-QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, int pass, const QString &passLogFile)
+QStringList EncodeThread::buildCommandLine(bool usePipe, bool use10Bit, unsigned int frames, const QString &indexFile, int pass, const QString &passLogFile)
 {
        QStringList cmdLine;
+       double crf_int = 0.0, crf_frc = 0.0;
 
        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());
+               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:
@@ -482,7 +539,7 @@ QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, in
        if((pass == 1) || (pass == 2))
        {
                cmdLine << "--pass" << QString::number(pass);
-               cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
+               cmdLine << "--stats" << pathToLocal(QDir::toNativeSeparators(passLogFile), true);
        }
 
        cmdLine << "--preset" << m_options->preset().toLower();
@@ -494,16 +551,29 @@ QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, in
 
        if(m_options->profile().compare("auto", Qt::CaseInsensitive))
        {
-               cmdLine << "--profile" << m_options->profile().toLower();
+               if(use10Bit)
+               {
+                       if(m_options->profile().compare("baseline", Qt::CaseInsensitive) || m_options->profile().compare("main", Qt::CaseInsensitive) || m_options->profile().compare("high", Qt::CaseInsensitive))
+                       {
+                               log(tr("WARNING: Selected H.264 Profile not compatible with 10-Bit encoding. Ignoring!\n"));
+                       }
+                       else
+                       {
+                               cmdLine << "--profile" << m_options->profile().toLower();
+                       }
+               }
+               else
+               {
+                       cmdLine << "--profile" << m_options->profile().toLower();
+               }
        }
 
-       if(!m_options->custom().isEmpty())
+       if(!m_options->customX264().isEmpty())
        {
-               //FIXME: Handle custom parameters that contain spaces!
-               cmdLine.append(m_options->custom().split(" "));
+               cmdLine.append(splitParams(m_options->customX264()));
        }
 
-       cmdLine << "--output" << QDir::toNativeSeparators(m_outputFileName);
+       cmdLine << "--output" << pathToLocal(QDir::toNativeSeparators(m_outputFileName), true);
        
        if(usePipe)
        {
@@ -514,19 +584,20 @@ QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, in
        }
        else
        {
-               cmdLine << QDir::toNativeSeparators(m_sourceFileName);
+               cmdLine << "--index" << pathToLocal(QDir::toNativeSeparators(indexFile), true, false);
+               cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName));
        }
 
        return cmdLine;
 }
 
-unsigned int EncodeThread::checkVersionX264(bool x64, bool &modified)
+unsigned int EncodeThread::checkVersionX264(bool use_x64, bool use_10bit, bool &modified)
 {
        QProcess process;
        QStringList cmdLine = QStringList() << "--version";
 
        log("Creating process:");
-       if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "x264_x64" : "x264"), cmdLine))
+       if(!startProcess(process, X264_BINARY(m_binDir, use_10bit, use_x64), cmdLine))
        {
                return false;;
        }
@@ -617,12 +688,12 @@ unsigned int EncodeThread::checkVersionX264(bool x64, bool &modified)
        return (coreVers * REV_MULT) + (revision % REV_MULT);
 }
 
-unsigned int EncodeThread::checkVersionAvs2yuv(void)
+unsigned int EncodeThread::checkVersionAvs2yuv(bool x64)
 {
        QProcess process;
 
        log("\nCreating process:");
-       if(!startProcess(process, QString("%1/avs2yuv.exe").arg(m_binDir), QStringList()))
+       if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "avs2yuv_x64" : "avs2yuv"), QStringList()))
        {
                return false;;
        }
@@ -717,15 +788,21 @@ unsigned int EncodeThread::checkVersionAvs2yuv(void)
        return (ver_maj * REV_MULT) + ((ver_min % REV_MULT) * 10) + (ver_mod % 10);
 }
 
-bool EncodeThread::checkProperties(unsigned int &frames)
+bool EncodeThread::checkProperties(bool x64, unsigned int &frames)
 {
        QProcess process;
-       
-       QStringList cmdLine = QStringList() << "-frames" << "1";
-       cmdLine << QDir::toNativeSeparators(m_sourceFileName) << "NUL";
+       QStringList cmdLine;
+
+       if(!m_options->customAvs2YUV().isEmpty())
+       {
+               cmdLine.append(splitParams(m_options->customAvs2YUV()));
+       }
+
+       cmdLine << "-frames" << "1";
+       cmdLine << pathToLocal(QDir::toNativeSeparators(m_sourceFileName)) << "NUL";
 
        log("Creating process:");
-       if(!startProcess(process, QString("%1/avs2yuv.exe").arg(m_binDir), cmdLine))
+       if(!startProcess(process, QString("%1/%2.exe").arg(m_binDir, x64 ? "avs2yuv_x64" : "avs2yuv"), cmdLine))
        {
                return false;;
        }
@@ -733,6 +810,8 @@ bool EncodeThread::checkProperties(unsigned int &frames)
        QRegExp regExpInt(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
        QRegExp regExpFrc(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
        
+       QTextCodec *localCodec = QTextCodec::codecForName("System");
+
        bool bTimeout = false;
        bool bAborted = false;
 
@@ -743,6 +822,8 @@ bool EncodeThread::checkProperties(unsigned int &frames)
        unsigned int fSizeW = 0;
        unsigned int fSizeH = 0;
        
+       unsigned int waitCounter = 0;
+
        while(process.state() != QProcess::NotRunning)
        {
                if(m_abort)
@@ -751,23 +832,36 @@ bool EncodeThread::checkProperties(unsigned int &frames)
                        bAborted = true;
                        break;
                }
-               if(!process.waitForReadyRead())
+               if(!process.waitForReadyRead(m_processTimeoutInterval))
                {
                        if(process.state() == QProcess::Running)
                        {
-                               process.kill();
-                               qWarning("Avs2YUV process timed out <-- killing!");
-                               log("\nPROCESS TIMEOUT !!!");
-                               bTimeout = true;
-                               break;
+                               if(waitCounter++ > m_processTimeoutMaxCounter)
+                               {
+                                       process.kill();
+                                       qWarning("Avs2YUV process timed out <-- killing!");
+                                       log("\nPROCESS TIMEOUT !!!");
+                                       log("\nAvisynth has encountered a deadlock or your script takes EXTREMELY long to initialize!");
+                                       bTimeout = true;
+                                       break;
+                               }
+                               else if(waitCounter == m_processTimeoutWarning)
+                               {
+                                       unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
+                                       log(tr("Warning: Avisynth did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
+                               }
                        }
+                       continue;
                }
+               
+               waitCounter = 0;
+               
                while(process.bytesAvailable() > 0)
                {
                        QList<QByteArray> lines = process.readLine().split('\r');
                        while(!lines.isEmpty())
                        {
-                               QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
+                               QString text = localCodec->toUnicode(lines.takeFirst().constData()).simplified();
                                int offset = -1;
                                if((offset = regExpInt.lastIndexIn(text)) >= 0)
                                {
@@ -801,6 +895,14 @@ bool EncodeThread::checkProperties(unsigned int &frames)
                                {
                                        log(text);
                                }
+                               if(text.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
+                               {
+                                       log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(x64 ? "64" : "32"));
+                               }
+                               if(text.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
+                               {
+                                       log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
+                               }
                        }
                }
        }
@@ -857,10 +959,9 @@ void EncodeThread::setStatus(JobStatus newStatus)
 {
        if(m_status != newStatus)
        {
-               m_status = newStatus;
                if((newStatus != JobStatus_Completed) && (newStatus != JobStatus_Failed) && (newStatus != JobStatus_Aborted) && (newStatus != JobStatus_Paused))
                {
-                       setProgress(0);
+                       if(m_status != JobStatus_Paused) setProgress(0);
                }
                if(newStatus == JobStatus_Failed)
                {
@@ -870,6 +971,7 @@ void EncodeThread::setStatus(JobStatus newStatus)
                {
                        setDetails("The job was aborted by the user!");
                }
+               m_status = newStatus;
                emit statusChanged(m_jobId, newStatus);
        }
 }
@@ -888,6 +990,56 @@ void EncodeThread::setDetails(const QString &text)
        emit detailsChanged(m_jobId, text);
 }
 
+QString EncodeThread::pathToLocal(const QString &longPath, bool create, bool keep)
+{
+       QTextCodec *localCodec = QTextCodec::codecForName("System");
+       
+       //Do NOT convert to short, if path can be represented in local Codepage
+       if(localCodec->toUnicode(localCodec->fromUnicode(longPath)).compare(longPath, Qt::CaseInsensitive) == 0)
+       {
+               return longPath;
+       }
+       
+       //Create dummy file, if required (only existing files can have a short path!)
+       QFile tempFile;
+       if((!QFileInfo(longPath).exists()) && create)
+       {
+               tempFile.setFileName(longPath);
+               tempFile.open(QIODevice::WriteOnly);
+       }
+       
+       QString shortPath;
+       DWORD buffSize = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), NULL, NULL);
+       
+       if(buffSize > 0)
+       {
+               wchar_t *buffer = new wchar_t[buffSize];
+               DWORD result = GetShortPathNameW(reinterpret_cast<const wchar_t*>(longPath.utf16()), buffer, buffSize);
+
+               if(result > 0 && result < buffSize)
+               {
+                       shortPath = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buffer));
+               }
+
+               delete[] buffer;
+               buffer = NULL;
+       }
+
+       //Remove the dummy file now (FFMS2 fails, if index file does exist but is empty!)
+       if(tempFile.isOpen())
+       {
+               if(!keep) tempFile.remove();
+               tempFile.close();
+       }
+
+       if(shortPath.isEmpty())
+       {
+               log(tr("Warning: Failed to convert path \"%1\" to short!\n").arg(longPath));
+       }
+
+       return (shortPath.isEmpty() ? longPath : shortPath);
+}
+
 bool EncodeThread::startProcess(QProcess &process, const QString &program, const QStringList &args, bool mergeChannels)
 {
        QMutexLocker lock(&m_mutex_startProcess);
@@ -959,3 +1111,71 @@ QString EncodeThread::commandline2string(const QString &program, const QStringLi
 
        return commandline;
 }
+
+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");
+}