1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "encoder_x265.h"
25 #include "model_options.h"
26 #include "model_status.h"
29 #include <QStringList>
34 static const unsigned int VERSION_X265_MINIMUM_VER = 9;
35 static const unsigned int VERSION_X265_MINIMUM_REV = 29;
37 // ------------------------------------------------------------
39 // ------------------------------------------------------------
41 #define X264_UPDATE_PROGRESS(X) do \
43 bool ok = false; qint64 size_estimate = 0; \
44 unsigned int progress = (X)->cap(1).toUInt(&ok); \
45 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
48 setProgress(progress); \
49 size_estimate = estimateSize(m_outputFile, progress); \
51 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(size_estimate))); \
55 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
57 if(ITER != LIST.end()) \
59 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
61 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
62 ITER = LIST.erase(ITER); \
63 if(ITER != LIST.end()) \
65 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
73 static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
75 const QString arch = (options->encArch() == OptionsModel::EncArch_x64) ? "x64" : "x86";
76 const QString vari = (options->encVariant() == OptionsModel::EncVariant_HiBit ) ? "16-Bit" : "8-Bit";
77 return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
80 // ------------------------------------------------------------
82 // ------------------------------------------------------------
84 class X265EncoderInfo : public AbstractEncoderInfo
87 virtual QString getVariantId(const int &variant) const
91 case OptionsModel::EncVariant_LoBit:
92 return QString::fromLatin1("8-Bit");
93 case OptionsModel::EncVariant_HiBit:
94 return QString::fromLatin1("16-Bit");
96 return QString::fromLatin1("N/A");
100 virtual QStringList getProfiles(const int &variant) const
102 return QStringList();
105 virtual QStringList supportedInputFormats(void) const
112 virtual QStringList supportedOutputFormats(void) const
119 virtual bool isRCModeSupported(const int rcMode) const
123 case OptionsModel::RCMode_CRF:
124 case OptionsModel::RCMode_CQ:
125 case OptionsModel::RCMode_ABR:
134 static const X265EncoderInfo s_x265EncoderInfo;
136 const AbstractEncoderInfo &X265Encoder::getEncoderInfo(void)
138 return s_x265EncoderInfo;
141 // ------------------------------------------------------------
142 // Constructor & Destructor
143 // ------------------------------------------------------------
145 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)
147 AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
148 m_encoderName(MAKE_NAME("x265 (H.265/HEVC)", m_options)),
149 m_binaryFile(ENC_BINARY(sysinfo, options))
151 if(options->encType() != OptionsModel::EncType_X265)
153 THROW("Invalid encoder type!");
157 X265Encoder::~X265Encoder(void)
159 /*Nothing to do here*/
162 const QString &X265Encoder::getName(void)
164 return m_encoderName;
167 // ------------------------------------------------------------
169 // ------------------------------------------------------------
171 void X265Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
173 cmdLine << "--version";
174 patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+0\\.(\\d+)\\+(\\d+)-[a-f0-9]+\\b", Qt::CaseInsensitive);
177 void X265Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
181 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
183 bool ok1 = false, ok2 = false;
184 unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
185 unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
186 if(ok1) coreVers = temp1;
187 if(ok2) revision = temp2;
196 QString X265Encoder::printVersion(const unsigned int &revision, const bool &modified)
198 return tr("x265 version: 0.%1+%2").arg(QString::number(revision / REV_MULT), QString::number(revision % REV_MULT));
201 bool X265Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
203 const unsigned int ver = (revision / REV_MULT);
204 const unsigned int rev = (revision % REV_MULT);
206 if((ver < VERSION_X265_MINIMUM_VER) || ((ver == VERSION_X265_MINIMUM_VER) && (rev < VERSION_X265_MINIMUM_REV)))
208 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)));
211 else if(ver > VERSION_X265_MINIMUM_VER)
213 log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
214 log(tr("This application works best with x265 version %1. Newer versions may work or not.").arg(QString::number(VERSION_X265_MINIMUM_VER)));
220 // ------------------------------------------------------------
221 // Encoding Functions
222 // ------------------------------------------------------------
224 void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
226 double crf_int = 0.0, crf_frc = 0.0;
228 switch(m_options->rcMode())
230 case OptionsModel::RCMode_CQ:
231 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
233 case OptionsModel::RCMode_CRF:
234 crf_frc = modf(m_options->quantizer(), &crf_int);
235 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
237 case OptionsModel::RCMode_2Pass:
238 case OptionsModel::RCMode_ABR:
239 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
242 THROW("Bad rate-control mode !!!");
246 if((pass == 1) || (pass == 2))
248 cmdLine << "--pass" << QString::number(pass);
249 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
252 cmdLine << "--preset" << m_options->preset().toLower();
254 if(m_options->tune().compare("none", Qt::CaseInsensitive))
256 cmdLine << "--tune" << m_options->tune().toLower();
259 if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
261 if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
263 cmdLine << "--profile" << m_options->profile().toLower();
267 if(!m_options->customEncParams().isEmpty())
269 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
272 QStringList::iterator i = customArgs.begin();
273 while(i != customArgs.end())
275 bool bModified = false;
276 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
277 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
281 cmdLine.append(customArgs);
284 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
288 if(frames < 1) THROW("Frames not set!");
289 cmdLine << "--frames" << QString::number(frames);
290 cmdLine << "--y4m" << "-";
294 cmdLine << QDir::toNativeSeparators(m_sourceFile);
298 void X265Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
300 patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames"); //regExpProgress
301 patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
302 patterns << new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
303 patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
306 void X265Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
309 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
311 X264_UPDATE_PROGRESS(patterns[0]);
313 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
316 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
317 setStatus(JobStatus_Indexing);
320 setProgress(progress);
322 setDetails(line.mid(offset).trimmed());
324 else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
326 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
327 setDetails(line.mid(offset).trimmed());
329 else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
331 X264_UPDATE_PROGRESS(patterns[3]);
333 else if(!line.isEmpty())