OSDN Git Service

Bump x265 version.
[x264-launcher/x264-launcher.git] / src / encoder_x265.cpp
index 5a624bb..7e0f7a2 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Simple x264 Launcher
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2016 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
 
 #include "encoder_x265.h"
 
+//Internal
+#include "global.h"
 #include "model_options.h"
 #include "model_status.h"
-#include "binaries.h"
-#include "binaries.h"
+#include "mediainfo.h"
+#include "model_sysinfo.h"
 
+//MUtils
+#include <MUtils/Exception.h>
+
+//Qt
 #include <QStringList>
 #include <QDir>
 #include <QRegExp>
 
 //x265 version info
-static const unsigned int X265_VERSION_X264_MINIMUM_VER = 9;
-static const unsigned int X265_VERSION_X264_MINIMUM_REV = 29;
+static const unsigned int VERSION_X265_MINIMUM_VER =  19;
+static const unsigned int VERSION_X265_MINIMUM_REV = 140;
 
 // ------------------------------------------------------------
 // Helper Macros
 // ------------------------------------------------------------
 
-#define X264_UPDATE_PROGRESS(X) do \
+#define X265_UPDATE_PROGRESS(X) do \
 { \
-       bool ok = false; qint64 size_estimate = 0; \
-       unsigned int progress = (X)->cap(1).toUInt(&ok); \
+       bool ok[2] = { false, false }; \
+       unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
+       unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
        setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
-       if(ok) \
+       if(ok[0] && ok[1]) \
        { \
-               setProgress(progress); \
-               size_estimate = estimateSize(m_outputFile, progress); \
+               const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
+               if(!qFuzzyCompare(progress, last_progress)) \
+               { \
+                       setProgress(floor(progress * 100.0)); \
+                       size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
+                       last_progress = progress; \
+               } \
        } \
-       setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(size_estimate))); \
+       setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
 } \
 while(0)
 
@@ -70,11 +82,112 @@ while(0)
 } \
 while(0)
 
-static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
+// ------------------------------------------------------------
+// Encoder Info
+// ------------------------------------------------------------
+
+class X265EncoderInfo : public AbstractEncoderInfo
+{
+public:
+       virtual QFlags<OptionsModel::EncVariant> getVariants(void) const
+       {
+               QFlags<OptionsModel::EncVariant> variants;
+               variants |= OptionsModel::EncVariant_8Bit;
+               variants |= OptionsModel::EncVariant_10Bit;
+               variants |= OptionsModel::EncVariant_12Bit;
+               return variants;
+       }
+
+       virtual QStringList getTunings(void) const
+       {
+               QStringList tunings;
+               tunings << "Grain" << "PSNR" << "SSIM" << "FastDecode" << "ZeroLatency";
+               return tunings;
+       }
+
+       virtual QStringList getPresets(void) const
+       {
+               QStringList presets;
+               presets << "ultrafast" << "superfast" << "veryfast" << "faster"   << "fast";
+               presets << "medium"    << "slow"      << "slower"   << "veryslow" << "placebo";
+               return presets;
+       }
+
+       virtual QStringList getProfiles(const OptionsModel::EncVariant &variant) const
+       {
+               QStringList profiles;
+               switch(variant)
+               {
+               case OptionsModel::EncVariant_8Bit:
+                       profiles << "main" << "main-intra" << "mainstillpicture" << "main444-8" << "main444-intra" << "main444-stillpicture";
+                       break;
+               case OptionsModel::EncVariant_10Bit:
+                       profiles << "main10" << "main10-intra" << "main422-10" << "main422-10-intra" << "main444-10" << "main444-10-intra";
+                       break;
+               case OptionsModel::EncVariant_12Bit:
+                       profiles << "main12" << "main12-intra" << "main422-12" << "main422-12-intra" << "main444-12" << "main444-12-intra";
+                       break;
+               }
+               return profiles;
+       }
+
+       virtual QStringList supportedOutputFormats(void) const
+       {
+               QStringList extLst;
+               extLst << "hevc";
+               return extLst;
+       }
+
+       virtual bool isRCModeSupported(const OptionsModel::RCMode &rcMode) const
+       {
+               switch(rcMode)
+               {
+               case OptionsModel::RCMode_CRF:
+               case OptionsModel::RCMode_CQ:
+               case OptionsModel::RCMode_2Pass:
+               case OptionsModel::RCMode_ABR:
+                       return true;
+               default:
+                       return false;
+               }
+       }
+
+       virtual bool isInputTypeSupported(const int format) const
+       {
+               switch(format)
+               {
+               case MediaInfo::FILETYPE_YUV4MPEG2:
+                       return true;
+               default:
+                       return false;
+               }
+       }
+
+       virtual QString getBinaryPath(const SysinfoModel *sysinfo, const OptionsModel::EncArch &encArch, const OptionsModel::EncVariant &encVariant) const
+       {
+               QString arch, variant;
+               switch(encArch)
+               {
+                       case OptionsModel::EncArch_x86_32: arch = "x86"; break;
+                       case OptionsModel::EncArch_x86_64: arch = "x64"; break;
+                       default: MUTILS_THROW("Unknown encoder arch!");
+               }
+               switch(encVariant)
+               {
+                       case OptionsModel::EncVariant_8Bit:  variant = "8bit";  break;
+                       case OptionsModel::EncVariant_10Bit: variant = "10bit"; break;
+                       case OptionsModel::EncVariant_12Bit: variant = "12bit"; break;
+                       default: MUTILS_THROW("Unknown encoder arch!");
+               }
+               return QString("%1/toolset/%2/x265_%3_%2.exe").arg(sysinfo->getAppPath(), arch, variant);
+       }
+};
+
+static const X265EncoderInfo s_x265EncoderInfo;
+
+const AbstractEncoderInfo &X265Encoder::getEncoderInfo(void)
 {
-       const QString arch = (options->encArch() == OptionsModel::EncArch_x64) ? "x64" : "x86";
-       const QString vari = (options->encVariant() == OptionsModel::EncVariant_HiBit ) ? "16-Bit" : "8-Bit";
-       return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
+       return s_x265EncoderInfo;
 }
 
 // ------------------------------------------------------------
@@ -83,13 +196,11 @@ static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
 
 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(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
-       m_encoderName(MAKE_NAME("x265 (H.265/HEVC)", m_options)),
-       m_binaryFile(ENC_BINARY(sysinfo, options))
+       AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
 {
        if(options->encType() != OptionsModel::EncType_X265)
        {
-               throw "Invalid encoder type!";
+               MUTILS_THROW("Invalid encoder type!");
        }
 }
 
@@ -98,9 +209,23 @@ X265Encoder::~X265Encoder(void)
        /*Nothing to do here*/
 }
 
-const QString &X265Encoder::getName(void)
+QString X265Encoder::getName(void) const
 {
-       return m_encoderName;
+       QString arch, variant;
+       switch(m_options->encArch())
+       {
+               case OptionsModel::EncArch_x86_32: arch = "x86"; break;
+               case OptionsModel::EncArch_x86_64: arch = "x64"; break;
+               default: MUTILS_THROW("Unknown encoder arch!");
+       }
+       switch(m_options->encVariant())
+       {
+               case OptionsModel::EncVariant_8Bit:  variant = "8-Bit";  break;
+               case OptionsModel::EncVariant_10Bit: variant = "10-Bit"; break;
+               case OptionsModel::EncVariant_12Bit: variant = "12-Bit"; break;
+               default: MUTILS_THROW("Unknown encoder arch!");
+       }
+       return QString("x265 (H.265/HEVC), %1, %2").arg(arch, variant);
 }
 
 // ------------------------------------------------------------
@@ -110,20 +235,38 @@ const QString &X265Encoder::getName(void)
 void X265Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
 {
        cmdLine << "--version";
-       patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+0\\.(\\d+)\\+(\\d+)-[a-f0-9]+\\b", Qt::CaseInsensitive);
+       patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\+(\\d+)\\b", Qt::CaseInsensitive);
+       patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\b", Qt::CaseInsensitive);
 }
 
-void X265Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
+void X265Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
 {
        int offset = -1;
 
        if((offset = patterns[0]->lastIndexIn(line)) >= 0)
        {
-               bool ok1 = false, ok2 = false;
-               unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
-               unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
-               if(ok1) coreVers = temp1;
-               if(ok2) revision = temp2;
+               bool ok[3] = { false, false, false };
+               unsigned int temp[3];
+               temp[0] = patterns[0]->cap(1).toUInt(&ok[0]);
+               temp[1] = patterns[0]->cap(2).toUInt(&ok[1]);
+               temp[2] = patterns[0]->cap(3).toUInt(&ok[2]);
+               if(ok[0] && ok[1])
+               {
+                       core = (10 * temp[0]) + temp[1];
+               }
+               if(ok[2]) build = temp[2];
+       }
+       else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
+       {
+               bool ok[2] = { false, false };
+               unsigned int temp[2];
+               temp[0] = patterns[1]->cap(1).toUInt(&ok[0]);
+               temp[1] = patterns[1]->cap(2).toUInt(&ok[1]);
+               if(ok[0] && ok[1])
+               {
+                       core = (10 * temp[0]) + temp[1];
+               }
+               build = 0;
        }
 
        if(!line.isEmpty())
@@ -134,19 +277,27 @@ void X265Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &p
 
 QString X265Encoder::printVersion(const unsigned int &revision, const bool &modified)
 {
-       return tr("x265 version: 0.%1+%2").arg(QString::number(revision / REV_MULT), QString::number(revision % REV_MULT));
+       unsigned int core, build;
+       splitRevision(revision, core, build);
+
+       return tr("x265 version: %1.%2+%3").arg(QString::number(core / 10), QString::number(core % 10), QString::number(build));
 }
 
 bool X265Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
 {
-       const unsigned int ver = (revision / REV_MULT);
-       const unsigned int rev = (revision % REV_MULT);
+       unsigned int core, build;
+       splitRevision(revision, core, build);
 
-       if((ver < X265_VERSION_X264_MINIMUM_VER) || (rev < X265_VERSION_X264_MINIMUM_REV))
+       if((core < VERSION_X265_MINIMUM_VER) || ((core == VERSION_X265_MINIMUM_VER) && (build < VERSION_X265_MINIMUM_REV)))
        {
-               log(tr("\nERROR: Your version of x265 is too old! (Minimum required revision is 0.%1+%2)").arg(QString::number(X265_VERSION_X264_MINIMUM_VER), QString::number(X265_VERSION_X264_MINIMUM_REV)));
+               log(tr("\nERROR: Your version of x265 is too old! (Minimum required revision is 0.%1+%2)").arg(QString::number(VERSION_X265_MINIMUM_VER), QString::number(VERSION_X265_MINIMUM_REV)));
                return false;
        }
+       else if(core > VERSION_X265_MINIMUM_VER)
+       {
+               log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
+               log(tr("This application works best with x265 version %1.%2. Newer versions may work or not.").arg(QString::number(VERSION_X265_MINIMUM_VER / 10), QString::number(VERSION_X265_MINIMUM_VER % 10)));
+       }
        
        return true;
 }
@@ -173,7 +324,7 @@ void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
                cmdLine << "--bitrate" << QString::number(m_options->bitrate());
                break;
        default:
-               throw "Bad rate-control mode !!!";
+               MUTILS_THROW("Bad rate-control mode !!!");
                break;
        }
        
@@ -183,18 +334,30 @@ void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
                cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
        }
 
-       cmdLine << "--preset" << m_options->preset().toLower();
+       const QString preset = m_options->preset().simplified().toLower();
+       if(!preset.isEmpty())
+       {
+               if(preset.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
+               {
+                       cmdLine << "--preset" << preset;
+               }
+       }
 
-       if(m_options->tune().compare("none", Qt::CaseInsensitive))
+       const QString tune = m_options->tune().simplified().toLower();
+       if(!tune.isEmpty())
        {
-               cmdLine << "--tune" << m_options->tune().toLower();
+               if(tune.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
+               {
+                       cmdLine << "--tune" << tune;
+               }
        }
 
-       if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
+       const QString profile = m_options->profile().simplified().toLower();
+       if(!profile.isEmpty())
        {
-               if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
+               if(profile.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
                {
-                       cmdLine << "--profile" << m_options->profile().toLower();
+                       cmdLine << "--profile" << profile;
                }
        }
 
@@ -219,7 +382,7 @@ void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
        
        if(usePipe)
        {
-               if(frames < 1) throw "Frames not set!";
+               if(frames < 1) MUTILS_THROW("Frames not set!");
                cmdLine << "--frames" << QString::number(frames);
                cmdLine << "--y4m" << "-";
        }
@@ -237,12 +400,12 @@ void X265Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
        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 X265Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
+void X265Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass, double &last_progress, double &size_estimate)
 {
        int offset = -1;
        if((offset = patterns[0]->lastIndexIn(line)) >= 0)
        {
-               X264_UPDATE_PROGRESS(patterns[0]);
+               X265_UPDATE_PROGRESS(patterns[0]);
        }
        else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
        {
@@ -262,7 +425,7 @@ void X265Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*>
        }
        else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
        {
-               X264_UPDATE_PROGRESS(patterns[3]);
+               X265_UPDATE_PROGRESS(patterns[3]);
        }
        else if(!line.isEmpty())
        {