OSDN Git Service

Bump x264 minimum required version to API-#163 (r3049).
[x264-launcher/x264-launcher.git] / src / encoder_x264.cpp
index f0a6d8b..e9f1a0d 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // Simple x264 Launcher
-// Copyright (C) 2004-2015 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 "model_status.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 VERSION_X264_MINIMUM_REV = 2555;
-static const unsigned int VERSION_X264_CURRENT_API = 148;
+static const unsigned int VERSION_X264_MINIMUM_REV = 3049;
+static const unsigned int VERSION_X264_CURRENT_API =  163;
 
 // ------------------------------------------------------------
 // Helper Macros
@@ -89,67 +92,62 @@ while(0)
 class X264EncoderInfo : public AbstractEncoderInfo
 {
 public:
-       virtual QFlags<OptionsModel::EncVariant> getVariants(void) const
+       virtual QString getName(void) const
        {
-               QFlags<OptionsModel::EncVariant> variants;
-               variants |= OptionsModel::EncVariant_8Bit;
-               variants |= OptionsModel::EncVariant_10Bit;
-               return variants;
+               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
        {
-               QStringList tunings;
-               tunings << "Film"       << "Animation"   << "Grain";
-               tunings << "StillImage" << "PSNR"        << "SSIM";
-               tunings << "FastDecode" << "ZeroLatency" << "Touhou";
-               
-               return tunings;
+               return QStringList()
+               << "Film"       << "Animation"   << "Grain"
+               << "StillImage" << "PSNR"        << "SSIM"
+               << "FastDecode" << "ZeroLatency" << "Touhou";
        }
 
        virtual QStringList getPresets(void) const
        {
-               QStringList presets;
-               presets << "ultrafast" << "superfast" << "veryfast" << "faster"   << "fast";
-               presets << "medium"    << "slow"      << "slower"   << "veryslow" << "placebo";
-               return presets;
+               return QStringList()
+               << "ultrafast" << "superfast" << "veryfast" << "faster"   << "fast"
+               << "medium"    << "slow"      << "slower"   << "veryslow" << "placebo";
        }
 
-       virtual QStringList getProfiles(const OptionsModel::EncVariant &variant) const
+       virtual QStringList getProfiles(const quint32 &variant) const
        {
                QStringList profiles;
-
-               if(variant == OptionsModel::EncVariant_8Bit)
+               switch(variant)
                {
-                       profiles << "Baseline" << "Main" << "High";
+                       case 0: profiles << "Baseline" << "Main"    << "High";    break;
+                       case 1: profiles << "High10"   << "High422" << "High444"; break;
+                       default: MUTILS_THROW("Unknown encoder variant!");
                }
-               if((variant == OptionsModel::EncVariant_8Bit) || (variant == OptionsModel::EncVariant_10Bit))
-               {
-                       profiles << "High10" << "High422" << "High444";
-               }
-
                return profiles;
        }
 
        virtual QStringList supportedOutputFormats(void) const
        {
-               QStringList extLst;
-               extLst << "264" << "mkv" << "mp4";
-               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;
-               }
+               return QStringList() << "264" << "mkv" << "mp4";
        }
 
        virtual bool isInputTypeSupported(const int format) const
@@ -165,32 +163,40 @@ public:
                }
        }
 
-       virtual QString getBinaryPath(const SysinfoModel *sysinfo, const OptionsModel::EncArch &encArch, const OptionsModel::EncVariant &encVariant) const
+       virtual QString getBinaryPath(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
        {
                QString arch, variant;
                switch(encArch)
                {
-                       case OptionsModel::EncArch_x86_32: arch = "x86"; break;
-                       case OptionsModel::EncArch_x86_64: arch = "x64"; break;
+                       case 0: arch = "x86"; break;
+                       case 1: arch = "x64"; break;
                        default: MUTILS_THROW("Unknown encoder arch!");
                }
-               switch(encVariant)
+               if ((encVariant < 0) || (encVariant > 1))
                {
-                       case OptionsModel::EncVariant_8Bit:  variant = "8bit";  break;
-                       case OptionsModel::EncVariant_10Bit: variant = "10bit"; break;
-                       default: MUTILS_THROW("Unknown encoder arch!");
+                       MUTILS_THROW("Unknown encoder variant!");
                }
-               return QString("%1/toolset/%2/x264_%3_%2.exe").arg(sysinfo->getAppPath(), arch, 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
 // ------------------------------------------------------------
@@ -212,20 +218,7 @@ X264Encoder::~X264Encoder(void)
 
 QString X264Encoder::getName(void) const
 {
-       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;
-               default: MUTILS_THROW("Unknown encoder arch!");
-       }
-       return QString("x264 (H.264/AVC), %1, %2").arg(arch, variant);
+       return s_x264EncoderInfo.getFullName(m_options->encArch(), m_options->encVariant());
 }
 
 // ------------------------------------------------------------
@@ -239,30 +232,24 @@ void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdL
        patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
 }
 
-void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, 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 && (temp1 > 0) && (temp2 > 0))
+               unsigned int temp[3];
+               if(MUtils::regexp_parse_uint32(*patterns[0], temp, 3))
                {
-                       core  = temp1;
-                       build = 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 && (temp1 > 0) && (temp2 > 0))
+               unsigned int temp[3];
+               if (MUtils::regexp_parse_uint32(*patterns[1], temp, 3))
                {
-                       core  = temp1;
-                       build = temp2;
+                       core  = temp[1];
+                       build = temp[2];
                }
                modified = true;
        }
@@ -311,21 +298,34 @@ 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:
@@ -338,21 +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().simplified().isEmpty())
+       const QString tune = m_options->tune().simplified().toLower();
+       if(!tune.isEmpty())
        {
-               if(m_options->tune().simplified().compare(QString::fromLatin1(OptionsModel::TUNING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
+               if(tune.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
                {
-                       cmdLine << "--tune" << m_options->tune().simplified().toLower();
+                       cmdLine << "--tune" << tune;
                }
        }
 
-       if(!m_options->profile().simplified().isEmpty())
+       const QString profile = m_options->profile().simplified().toLower();
+       if(!profile.isEmpty())
        {
-               if(m_options->profile().simplified().compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
+               if(profile.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
                {
-                       cmdLine << "--profile" << m_options->profile().simplified().toLower();
+                       cmdLine << "--profile" << profile;
                }
        }
 
@@ -377,8 +386,11 @@ void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
        
        if(usePipe)
        {
-               if(frames < 1) MUTILS_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" << "-";
        }
@@ -397,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, double &last_progress, double &size_estimate)
+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)