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 9a3c84a..3595e6f 100644 (file)
@@ -33,6 +33,7 @@
 #include <QProcess>
 #include <QMutex>
 #include <QTextCodec>
+#include <QLocale>
 
 /*
  * Static vars
@@ -49,12 +50,14 @@ 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; \
        } \
 }
@@ -68,6 +71,8 @@ QMutex EncodeThread::m_mutex_startProcess;
        } \
 }
 
+#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
  */
@@ -77,7 +82,7 @@ static const unsigned int REV_MULT = 10000;
 // Constructor & Destructor
 ///////////////////////////////////////////////////////////////////////////////
 
-EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputFileName, const OptionsModel *options, const QString &binDir, bool x264_x64, bool avs2yuv_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),
@@ -85,6 +90,7 @@ EncodeThread::EncodeThread(const QString &sourceFileName, const QString &outputF
        m_options(new OptionsModel(*options)),
        m_binDir(binDir),
        m_x264_x64(x264_x64),
+       m_x264_10bit(x264_10bit),
        m_avs2yuv_x64(avs2yuv_x64),
        m_handle_jobObject(NULL),
        m_semaphorePaused(0)
@@ -197,7 +203,7 @@ void EncodeThread::encode(void)
        log(tr("\n--- CHECK VERSION ---\n"));
        unsigned int revision_x264 = UINT_MAX;
        bool x264_modified = false;
-       ok = ((revision_x264 = checkVersionX264(m_x264_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
@@ -256,17 +262,17 @@ void EncodeThread::encode(void)
                }
                
                log(tr("\n--- PASS 1 ---\n"));
-               ok = runEncodingPass(m_x264_x64, m_avs2yuv_x64, usePipe, frames, indexFile, 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_x264_x64, m_avs2yuv_x64, usePipe, frames, indexFile, 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_x264_x64, m_avs2yuv_x64, usePipe, frames, indexFile);
+               ok = runEncodingPass(m_x264_x64, m_x264_10bit, m_avs2yuv_x64, usePipe, frames, indexFile);
                CHECK_STATUS(m_abort, ok);
        }
 
@@ -277,7 +283,7 @@ void EncodeThread::encode(void)
        setStatus(JobStatus_Completed);
 }
 
-bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe, unsigned int frames, const QString &indexFile, 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;
        
@@ -299,16 +305,16 @@ bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe
                }
        }
 
-       QStringList cmdLine_Encode = buildCommandLine(usePipe, frames, indexFile, 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, x264_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");
@@ -316,6 +322,10 @@ bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe
        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)
        {
@@ -395,24 +405,37 @@ bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe
                                        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);
                                }
                        }
@@ -462,6 +485,12 @@ bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe
                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:
@@ -470,11 +499,11 @@ bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe
                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;
        }
 
@@ -484,7 +513,7 @@ bool EncodeThread::runEncodingPass(bool x264_x64, bool avs2yuv_x64, bool usePipe
        return true;
 }
 
-QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, const QString &indexFile, 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;
@@ -522,7 +551,21 @@ QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, co
 
        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->customX264().isEmpty())
@@ -548,13 +591,13 @@ QStringList EncodeThread::buildCommandLine(bool usePipe, unsigned int frames, co
        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;;
        }
@@ -1094,5 +1137,45 @@ QStringList EncodeThread::splitParams(const QString &params)
        }
        
        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;
-}
\ No newline at end of file
+}
+
+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");
+}