Updated AddJob dialog for the recent AbstractEncoderInfo changes + various fixes.
[simple-x264-launcher.git] / src / encoder_x265.cpp
blob8e36364ac39c05f16825e39d9c9193e5ff326e8b
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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"
24 //Internal
25 #include "global.h"
26 #include "model_options.h"
27 #include "model_status.h"
28 #include "mediainfo.h"
29 #include "model_sysinfo.h"
31 //MUtils
32 #include <MUtils/Exception.h>
34 //Qt
35 #include <QStringList>
36 #include <QDir>
37 #include <QRegExp>
38 #include <QPair>
40 //x265 version info
41 static const unsigned int VERSION_X265_MINIMUM_VER = 19;
42 static const unsigned int VERSION_X265_MINIMUM_REV = 140;
44 // ------------------------------------------------------------
45 // Helper Macros
46 // ------------------------------------------------------------
48 #define X265_UPDATE_PROGRESS(X) do \
49 { \
50 bool ok[2] = { false, false }; \
51 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
52 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
53 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
54 if(ok[0] && ok[1]) \
55 { \
56 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
57 if(!qFuzzyCompare(progress, last_progress)) \
58 { \
59 setProgress(floor(progress * 100.0)); \
60 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
61 last_progress = progress; \
62 } \
63 } \
64 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
65 } \
66 while(0)
68 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
69 { \
70 if(ITER != LIST.end()) \
71 { \
72 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
73 { \
74 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
75 ITER = LIST.erase(ITER); \
76 if(ITER != LIST.end()) \
77 { \
78 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
79 } \
80 FLAG = true; \
81 } \
82 } \
83 } \
84 while(0)
86 // ------------------------------------------------------------
87 // Encoder Info
88 // ------------------------------------------------------------
90 class X265EncoderInfo : public AbstractEncoderInfo
92 public:
93 virtual QString getName(void) const
95 return "x265 (HEVC/H.265)";
98 virtual QList<ArchId> getArchitectures(void) const
100 return QList<ArchId>()
101 << qMakePair(QString("32-Bit (x86)"), ARCH_TYPE_X86)
102 << qMakePair(QString("64-Bit (x64)"), ARCH_TYPE_X64);
105 virtual QStringList getVariants(void) const
107 return QStringList() << "8-Bit" << "10-Bit" << "12-Bit";
109 virtual QList<RCMode> getRCModes(void) const
111 return QList<RCMode>()
112 << qMakePair(QString("CRF"), RC_TYPE_QUANTIZER)
113 << qMakePair(QString("CQ"), RC_TYPE_QUANTIZER)
114 << qMakePair(QString("2-Pass"), RC_TYPE_MULTIPASS)
115 << qMakePair(QString("ABR"), RC_TYPE_RATE_KBPS);
118 virtual QStringList getTunings(void) const
120 return QStringList() << "Grain" << "PSNR" << "SSIM" << "FastDecode" << "ZeroLatency";
123 virtual QStringList getPresets(void) const
125 return QStringList()
126 << "ultrafast" << "superfast" << "veryfast" << "faster" << "fast"
127 << "medium" << "slow" << "slower" << "veryslow" << "placebo";
130 virtual QStringList getProfiles(const quint32 &variant) const
132 QStringList profiles;
133 switch(variant)
135 case 0: profiles << "main" << "main-intra" << "mainstillpicture" << "main444-8" << "main444-intra" << "main444-stillpicture"; break;
136 case 1: profiles << "main10" << "main10-intra" << "main422-10" << "main422-10-intra" << "main444-10" << "main444-10-intra"; break;
137 case 2: profiles << "main12" << "main12-intra" << "main422-12" << "main422-12-intra" << "main444-12" << "main444-12-intra"; break;
138 default: MUTILS_THROW("Unknown encoder variant!");
140 return profiles;
143 virtual QStringList supportedOutputFormats(void) const
145 return QStringList() << "hevc";
148 virtual bool isInputTypeSupported(const int format) const
150 switch(format)
152 case MediaInfo::FILETYPE_YUV4MPEG2:
153 return true;
154 default:
155 return false;
159 virtual QString getBinaryPath(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
161 QString arch, variant;
162 switch(encArch)
164 case 0: arch = "x86"; break;
165 case 1: arch = "x64"; break;
166 default: MUTILS_THROW("Unknown encoder arch!");
168 switch(encVariant)
170 case 0: variant = "8bit"; break;
171 case 1: variant = "10bit"; break;
172 case 2: variant = "12bit"; break;
173 default: MUTILS_THROW("Unknown encoder arch!");
175 return QString("%1/toolset/%2/x265_%3_%2.exe").arg(sysinfo->getAppPath(), arch, variant);
179 static const X265EncoderInfo s_x265EncoderInfo;
181 const AbstractEncoderInfo& X265Encoder::encoderInfo(void)
183 return s_x265EncoderInfo;
186 const AbstractEncoderInfo &X265Encoder::getEncoderInfo(void) const
188 return encoderInfo();
191 // ------------------------------------------------------------
192 // Constructor & Destructor
193 // ------------------------------------------------------------
195 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)
197 AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
199 if(options->encType() != OptionsModel::EncType_X265)
201 MUTILS_THROW("Invalid encoder type!");
205 X265Encoder::~X265Encoder(void)
207 /*Nothing to do here*/
210 QString X265Encoder::getName(void) const
212 return s_x265EncoderInfo.getFullName(m_options->encArch(), m_options->encVariant());
215 // ------------------------------------------------------------
216 // Check Version
217 // ------------------------------------------------------------
219 void X265Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
221 cmdLine << "--version";
222 patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\+(\\d+)\\b", Qt::CaseInsensitive);
223 patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\b", Qt::CaseInsensitive);
226 void X265Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
228 int offset = -1;
230 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
232 bool ok[3] = { false, false, false };
233 unsigned int temp[3];
234 temp[0] = patterns[0]->cap(1).toUInt(&ok[0]);
235 temp[1] = patterns[0]->cap(2).toUInt(&ok[1]);
236 temp[2] = patterns[0]->cap(3).toUInt(&ok[2]);
237 if(ok[0] && ok[1])
239 core = (10 * temp[0]) + temp[1];
241 if(ok[2]) build = temp[2];
243 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
245 bool ok[2] = { false, false };
246 unsigned int temp[2];
247 temp[0] = patterns[1]->cap(1).toUInt(&ok[0]);
248 temp[1] = patterns[1]->cap(2).toUInt(&ok[1]);
249 if(ok[0] && ok[1])
251 core = (10 * temp[0]) + temp[1];
253 build = 0;
256 if(!line.isEmpty())
258 log(line);
262 QString X265Encoder::printVersion(const unsigned int &revision, const bool &modified)
264 unsigned int core, build;
265 splitRevision(revision, core, build);
267 return tr("x265 version: %1.%2+%3").arg(QString::number(core / 10), QString::number(core % 10), QString::number(build));
270 bool X265Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
272 unsigned int core, build;
273 splitRevision(revision, core, build);
275 if((core < VERSION_X265_MINIMUM_VER) || ((core == VERSION_X265_MINIMUM_VER) && (build < VERSION_X265_MINIMUM_REV)))
277 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)));
278 return false;
280 else if(core > VERSION_X265_MINIMUM_VER)
282 log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
283 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)));
286 return true;
289 // ------------------------------------------------------------
290 // Encoding Functions
291 // ------------------------------------------------------------
293 void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
295 double crf_int = 0.0, crf_frc = 0.0;
297 switch(m_options->rcMode())
299 case 0:
300 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
301 break;
302 case 1:
303 crf_frc = modf(m_options->quantizer(), &crf_int);
304 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
305 break;
306 case 2:
307 case 3:
308 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
309 break;
310 default:
311 MUTILS_THROW("Bad rate-control mode !!!");
312 break;
315 if((pass == 1) || (pass == 2))
317 cmdLine << "--pass" << QString::number(pass);
318 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
321 const QString preset = m_options->preset().simplified().toLower();
322 if(!preset.isEmpty())
324 if(preset.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
326 cmdLine << "--preset" << preset;
330 const QString tune = m_options->tune().simplified().toLower();
331 if(!tune.isEmpty())
333 if(tune.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
335 cmdLine << "--tune" << tune;
339 const QString profile = m_options->profile().simplified().toLower();
340 if(!profile.isEmpty())
342 if(profile.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
344 cmdLine << "--profile" << profile;
348 if(!m_options->customEncParams().isEmpty())
350 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
351 if(usePipe)
353 QStringList::iterator i = customArgs.begin();
354 while(i != customArgs.end())
356 bool bModified = false;
357 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
358 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
359 if(!bModified) i++;
362 cmdLine.append(customArgs);
365 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
367 if(usePipe)
369 if(frames < 1) MUTILS_THROW("Frames not set!");
370 cmdLine << "--frames" << QString::number(frames);
371 cmdLine << "--y4m" << "-";
373 else
375 cmdLine << QDir::toNativeSeparators(m_sourceFile);
379 void X265Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
381 patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames"); //regExpProgress
382 patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
383 patterns << new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
384 patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
387 void X265Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const unsigned int &totalFrames, const int &pass, double &last_progress, double &size_estimate)
389 int offset = -1;
390 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
392 X265_UPDATE_PROGRESS(patterns[0]);
394 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
396 bool ok = false;
397 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
398 setStatus(JobStatus_Indexing);
399 if(ok)
401 setProgress(progress);
403 setDetails(line.mid(offset).trimmed());
405 else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
407 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
408 setDetails(line.mid(offset).trimmed());
410 else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
412 X265_UPDATE_PROGRESS(patterns[3]);
414 else if(!line.isEmpty())
416 log(line);