OSDN Git Service

Bump x264 minimum required version to API-#163 (r3049).
[x264-launcher/x264-launcher.git] / src / encoder_x264.cpp
index 474d4d7..e9f1a0d 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Simple x264 Launcher
-// Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2020 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_x264.h"
 
+//Internal
+#include "global.h"
 #include "model_options.h"
 #include "model_status.h"
-#include "binaries.h"
+#include "mediainfo.h"
+#include "model_sysinfo.h"
+#include "model_clipInfo.h"
 
+//MUtils
+#include <MUtils/Global.h>
+#include <MUtils/Exception.h>
+
+//Qt
 #include <QStringList>
 #include <QDir>
 #include <QRegExp>
+#include <QPair>
 
 //x264 version info
-static const unsigned int X264_VERSION_X264_MINIMUM_REV = 2397;
-static const unsigned int X264_VERSION_X264_CURRENT_API = 142;
+static const unsigned int VERSION_X264_MINIMUM_REV = 3049;
+static const unsigned int VERSION_X264_CURRENT_API =  163;
 
 // ------------------------------------------------------------
 // Helper Macros
@@ -57,25 +67,24 @@ while(0)
 
 #define X264_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)
 
-static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
-{
-       const QString arch = (options->encArch() == OptionsModel::EncArch_x64) ? "x64" : "x86";
-       const QString vari = (options->encVariant() == OptionsModel::EncVariant_HiBit ) ? "10-Bit" : "8-Bit";
-       return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
-}
-
 // ------------------------------------------------------------
 // Encoder Info
 // ------------------------------------------------------------
@@ -83,54 +92,122 @@ static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
 class X264EncoderInfo : public AbstractEncoderInfo
 {
 public:
-       virtual QStringList supportedInputFormats(void) const
-       {
-               QStringList extLst;
-               extLst << "avi";
-               extLst << "mp4";
-               extLst << "mkv";
-               extLst << "flv";
-               extLst << "mpg";
-               extLst << "m2v";
-               extLst << "m2ts";
-               extLst << "ts";
-               extLst << "wmv";
-               extLst << "ogm";
-               extLst << "vob";
-               extLst << "y4m";
-               return extLst;
+       virtual QString getName(void) const
+       {
+               return "x264 (AVC/H.264)";
+       }
+
+       virtual QList<ArchId> getArchitectures(void) const
+       {
+               return QList<ArchId>()
+               << qMakePair(QString("32-Bit (x86)"), ARCH_TYPE_X86)
+               << qMakePair(QString("64-Bit (x64)"), ARCH_TYPE_X64);
+       }
+
+       virtual QStringList getVariants(void) const
+       {
+               return QStringList() << "8-Bit" << "10-Bit";
+       }
+
+       virtual QList<RCMode> getRCModes(void) const
+       {
+               return QList<RCMode>()
+               << qMakePair(QString("CRF"),    RC_TYPE_QUANTIZER)
+               << qMakePair(QString("CQ"),     RC_TYPE_QUANTIZER)
+               << qMakePair(QString("2-Pass"), RC_TYPE_MULTIPASS)
+               << qMakePair(QString("ABR"),    RC_TYPE_RATE_KBPS);
+       }
+
+       virtual QStringList getTunings(void) const
+       {
+               return QStringList()
+               << "Film"       << "Animation"   << "Grain"
+               << "StillImage" << "PSNR"        << "SSIM"
+               << "FastDecode" << "ZeroLatency" << "Touhou";
+       }
+
+       virtual QStringList getPresets(void) const
+       {
+               return QStringList()
+               << "ultrafast" << "superfast" << "veryfast" << "faster"   << "fast"
+               << "medium"    << "slow"      << "slower"   << "veryslow" << "placebo";
+       }
+
+       virtual QStringList getProfiles(const quint32 &variant) const
+       {
+               QStringList profiles;
+               switch(variant)
+               {
+                       case 0: profiles << "Baseline" << "Main"    << "High";    break;
+                       case 1: profiles << "High10"   << "High422" << "High444"; break;
+                       default: MUTILS_THROW("Unknown encoder variant!");
+               }
+               return profiles;
        }
 
        virtual QStringList supportedOutputFormats(void) const
        {
-               QStringList extLst;
-               extLst << "264";
-               extLst << "mkv";
-               extLst << "mp4";
-               return extLst;
+               return QStringList() << "264" << "mkv" << "mp4";
+       }
+
+       virtual bool isInputTypeSupported(const int format) const
+       {
+               switch(format)
+               {
+               case MediaInfo::FILETYPE_AVISYNTH:
+               case MediaInfo::FILETYPE_YUV4MPEG2:
+               case MediaInfo::FILETYPE_UNKNOWN:
+                       return true;
+               default:
+                       return false;
+               }
+       }
+
+       virtual QString getBinaryPath(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
+       {
+               QString arch, variant;
+               switch(encArch)
+               {
+                       case 0: arch = "x86"; break;
+                       case 1: arch = "x64"; break;
+                       default: MUTILS_THROW("Unknown encoder arch!");
+               }
+               if ((encVariant < 0) || (encVariant > 1))
+               {
+                       MUTILS_THROW("Unknown encoder variant!");
+               }
+               return QString("%1/toolset/%2/x264_%2.exe").arg(sysinfo->getAppPath(), arch);
+       }
+
+       virtual QString getHelpCommand(void) const
+       {
+               return "--fullhelp";
        }
 };
 
 static const X264EncoderInfo s_x264EncoderInfo;
 
-const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
+const AbstractEncoderInfo& X264Encoder::encoderInfo(void)
 {
        return s_x264EncoderInfo;
 }
 
+const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void) const
+{
+       return encoderInfo();
+}
+
 // ------------------------------------------------------------
 // Constructor & Destructor
 // ------------------------------------------------------------
 
 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(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
-       m_encoderName(MAKE_NAME("x264 (H.264/AVC)", m_options)),
-       m_binaryFile(ENC_BINARY(sysinfo, options))
+       AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
 {
        if(options->encType() != OptionsModel::EncType_X264)
        {
-               throw "Invalid encoder type!";
+               MUTILS_THROW("Invalid encoder type!");
        }
 }
 
@@ -139,9 +216,9 @@ X264Encoder::~X264Encoder(void)
        /*Nothing to do here*/
 }
 
-const QString &X264Encoder::getName(void)
+QString X264Encoder::getName(void) const
 {
-       return m_encoderName;
+       return s_x264EncoderInfo.getFullName(m_options->encArch(), m_options->encVariant());
 }
 
 // ------------------------------------------------------------
@@ -151,34 +228,28 @@ const QString &X264Encoder::getName(void)
 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
 {
        cmdLine << "--version";
-       patterns << new QRegExp("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
-       patterns << new QRegExp("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
+       patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
+       patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
 }
 
-void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
+void X264Encoder::checkVersion_parseLine(const QString &line, const QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
 {
-       int offset = -1;
-
-       if((offset = patterns[0]->lastIndexIn(line)) >= 0)
+       if(patterns[0]->lastIndexIn(line) >= 0)
        {
-               bool ok1 = false, ok2 = false;
-               unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
-               unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
-               if(ok1 && ok2)
+               unsigned int temp[3];
+               if(MUtils::regexp_parse_uint32(*patterns[0], temp, 3))
                {
-                       coreVers = temp1;
-                       revision = temp2;
+                       core  = temp[1];
+                       build = temp[2];
                }
        }
-       else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
+       else if(patterns[1]->lastIndexIn(line) >= 0)
        {
-               bool ok1 = false, ok2 = false;
-               unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
-               unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
-               if(ok1 && ok2)
+               unsigned int temp[3];
+               if (MUtils::regexp_parse_uint32(*patterns[1], temp, 3))
                {
-                       coreVers = temp1;
-                       revision = temp2;
+                       core  = temp[1];
+                       build = temp[2];
                }
                modified = true;
        }
@@ -191,21 +262,33 @@ void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &p
 
 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
 {
-       return tr("x264 revision: %1 (core #%2)").arg(QString::number(revision % REV_MULT), QString::number(revision / REV_MULT)).append(modified ? tr(" - with custom patches!") : QString());
+       unsigned int core, build;
+       splitRevision(revision, core, build);
+
+       QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
+       if(modified)
+       {
+               versionStr.append(tr(" - with custom patches!"));
+       }
+
+       return versionStr;
 }
 
 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
 {
-       if((revision % REV_MULT) < X264_VERSION_X264_MINIMUM_REV)
+       unsigned int core, build;
+       splitRevision(revision, core, build);
+
+       if(build < VERSION_X264_MINIMUM_REV)
        {
-               log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %1)").arg(QString::number(X264_VERSION_X264_MINIMUM_REV)));
+               log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
                return false;
        }
        
-       if((revision / REV_MULT) != X264_VERSION_X264_CURRENT_API)
+       if(core != VERSION_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(X264_VERSION_X264_CURRENT_API)));
+               log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
+               log(tr("This application works best with x264 core (API) version %1. Newer versions may work or not.").arg(QString::number(VERSION_X264_CURRENT_API)));
        }
 
        return true;
@@ -215,26 +298,38 @@ bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &m
 // Encoding Functions
 // ------------------------------------------------------------
 
-void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
+void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const ClipInfo &clipInfo, const QString &indexFile, const int &pass, const QString &passLogFile)
 {
        double crf_int = 0.0, crf_frc = 0.0;
 
-       switch(m_options->rcMode())
+       cmdLine << "--output-depth";
+       switch (m_options->encVariant())
        {
-       case OptionsModel::RCMode_CQ:
-               cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
+       case 0:
+               cmdLine << QString::number(8);
+               break;
+       case 1:
+               cmdLine << QString::number(10);
                break;
-       case OptionsModel::RCMode_CRF:
+       default:
+               MUTILS_THROW("Unknown encoder variant!");
+       }
+
+       switch(m_options->rcMode())
+       {
+       case 0:
                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:
+       case 1:
+               cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
+               break;
+       case 2:
+       case 3:
                cmdLine << "--bitrate" << QString::number(m_options->bitrate());
                break;
        default:
-               throw "Bad rate-control mode !!!";
-               break;
+               MUTILS_THROW("Bad rate-control mode !!!");
        }
        
        if((pass == 1) || (pass == 2))
@@ -243,18 +338,30 @@ void X264Encoder::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;
                }
        }
 
@@ -279,8 +386,11 @@ void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
        
        if(usePipe)
        {
-               if(frames < 1) throw "Frames not set!";
-               cmdLine << "--frames" << QString::number(frames);
+               if (clipInfo.getFrameCount() < 1)
+               {
+                       MUTILS_THROW("Frames not set!");
+               }
+               cmdLine << "--frames" << QString::number(clipInfo.getFrameCount());
                cmdLine << "--demuxer" << "y4m";
                cmdLine << "--stdin" << "y4m" << "-";
        }
@@ -299,7 +409,7 @@ void X264Encoder::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 X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
+void X264Encoder::runEncodingPass_parseLine(const QString &line, const QList<QRegExp*> &patterns, const ClipInfo &clipInfo, const int &pass, double &last_progress, double &size_estimate)
 {
        int offset = -1;
        if((offset = patterns[0]->lastIndexIn(line)) >= 0)