1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2023 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"
26 #include "model_options.h"
27 #include "model_status.h"
28 #include "mediainfo.h"
29 #include "model_sysinfo.h"
30 #include "model_clipInfo.h"
33 #include <MUtils/Global.h>
34 #include <MUtils/Exception.h>
37 #include <QStringList>
43 static const unsigned int VERSION_X265_MINIMUM_VER
= 35;
44 static const unsigned int VERSION_X265_MINIMUM_REV
= 20;
46 // ------------------------------------------------------------
48 // ------------------------------------------------------------
50 #define X265_UPDATE_PROGRESS(X) do \
52 bool ok[2] = { false, false }; \
53 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
54 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
55 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
58 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
59 if(!qFuzzyCompare(progress, last_progress)) \
61 setProgress(floor(progress * 100.0)); \
62 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
63 last_progress = progress; \
66 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
70 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
72 if(ITER != LIST.end()) \
74 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
76 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
77 ITER = LIST.erase(ITER); \
78 if(ITER != LIST.end()) \
80 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
88 // ------------------------------------------------------------
90 // ------------------------------------------------------------
92 class X265EncoderInfo
: public AbstractEncoderInfo
95 virtual QString
getName(void) const
97 return "x265 (HEVC/H.265)";
100 virtual QList
<ArchId
> getArchitectures(void) const
102 return QList
<ArchId
>()
103 << qMakePair(QString("32-Bit (x86)"), ARCH_TYPE_X86
)
104 << qMakePair(QString("64-Bit (x64)"), ARCH_TYPE_X64
);
107 virtual QStringList
getVariants(void) const
109 return QStringList() << "8-Bit" << "10-Bit" << "12-Bit";
111 virtual QList
<RCMode
> getRCModes(void) const
113 return QList
<RCMode
>()
114 << qMakePair(QString("CRF"), RC_TYPE_QUANTIZER
)
115 << qMakePair(QString("CQ"), RC_TYPE_QUANTIZER
)
116 << qMakePair(QString("2-Pass"), RC_TYPE_MULTIPASS
)
117 << qMakePair(QString("ABR"), RC_TYPE_RATE_KBPS
);
120 virtual QStringList
getTunings(void) const
122 return QStringList() << "Grain" << "PSNR" << "SSIM" << "FastDecode" << "ZeroLatency" << "Animation";
125 virtual QStringList
getPresets(void) const
128 << "ultrafast" << "superfast" << "veryfast" << "faster" << "fast"
129 << "medium" << "slow" << "slower" << "veryslow" << "placebo";
132 virtual QStringList
getProfiles(const quint32
&variant
) const
134 QStringList profiles
;
137 case 0: profiles
<< "main" << "main-intra" << "mainstillpicture" << "main444-8" << "main444-intra" << "main444-stillpicture"; break;
138 case 1: profiles
<< "main10" << "main10-intra" << "main422-10" << "main422-10-intra" << "main444-10" << "main444-10-intra"; break;
139 case 2: profiles
<< "main12" << "main12-intra" << "main422-12" << "main422-12-intra" << "main444-12" << "main444-12-intra"; break;
140 default: MUTILS_THROW("Unknown encoder variant!");
145 virtual QStringList
supportedOutputFormats(void) const
147 return QStringList() << "hevc";
150 virtual bool isInputTypeSupported(const int format
) const
154 case MediaInfo::FILETYPE_YUV4MPEG2
:
161 virtual QString
getBinaryPath(const SysinfoModel
*sysinfo
, const quint32
&encArch
, const quint32
&encVariant
) const
166 case 0: arch
= "x86"; break;
167 case 1: arch
= "x64"; break;
168 default: MUTILS_THROW("Unknown encoder arch!");
170 if((encVariant
< 0) || (encVariant
> 2))
172 MUTILS_THROW("Unknown encoder variant!");
174 return QString("%1/toolset/%2/x265_%2.exe").arg(sysinfo
->getAppPath(), arch
);
177 virtual QString
getHelpCommand(void) const
183 static const X265EncoderInfo s_x265EncoderInfo
;
185 const AbstractEncoderInfo
& X265Encoder::encoderInfo(void)
187 return s_x265EncoderInfo
;
190 const AbstractEncoderInfo
&X265Encoder::getEncoderInfo(void) const
192 return encoderInfo();
195 // ------------------------------------------------------------
196 // Constructor & Destructor
197 // ------------------------------------------------------------
199 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
)
201 AbstractEncoder(jobObject
, options
, sysinfo
, preferences
, jobStatus
, abort
, pause
, semaphorePause
, sourceFile
, outputFile
)
203 if(options
->encType() != OptionsModel::EncType_X265
)
205 MUTILS_THROW("Invalid encoder type!");
209 X265Encoder::~X265Encoder(void)
211 /*Nothing to do here*/
214 QString
X265Encoder::getName(void) const
216 return s_x265EncoderInfo
.getFullName(m_options
->encArch(), m_options
->encVariant());
219 // ------------------------------------------------------------
221 // ------------------------------------------------------------
223 void X265Encoder::checkVersion_init(QList
<QRegExp
*> &patterns
, QStringList
&cmdLine
)
225 cmdLine
<< "--version";
226 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\+(\\d+)\\b", Qt::CaseInsensitive
);
227 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\_Au\\+(\\d+)\\b", Qt::CaseInsensitive
);
228 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\b", Qt::CaseInsensitive
);
231 void X265Encoder::checkVersion_parseLine(const QString
&line
, const QList
<QRegExp
*> &patterns
, unsigned int &core
, unsigned int &build
, bool &modified
)
235 for (size_t q
= 0; q
< 2; ++q
)
237 if ((offset
= patterns
[q
]->lastIndexIn(line
)) >= 0)
239 unsigned int temp
[3];
240 if (MUtils::regexp_parse_uint32(*patterns
[q
], temp
, 3))
242 core
= (10 * temp
[0]) + temp
[1];
249 if ((!core
) || (core
== UINT_MAX
))
251 if ((offset
= patterns
[2]->lastIndexIn(line
)) >= 0)
253 unsigned int temp
[2];
254 if (MUtils::regexp_parse_uint32(*patterns
[2], temp
, 2))
256 core
= (10 * temp
[0]) + temp
[1];
268 QString
X265Encoder::printVersion(const unsigned int &revision
, const bool &modified
)
270 unsigned int core
, build
;
271 splitRevision(revision
, core
, build
);
273 return tr("x265 version: %1.%2+%3").arg(QString::number(core
/ 10), QString::number(core
% 10), QString::number(build
));
276 bool X265Encoder::isVersionSupported(const unsigned int &revision
, const bool &modified
)
278 unsigned int core
, build
;
279 splitRevision(revision
, core
, build
);
281 if((core
< VERSION_X265_MINIMUM_VER
) || ((core
== VERSION_X265_MINIMUM_VER
) && (build
< VERSION_X265_MINIMUM_REV
)))
283 log(tr("\nERROR: Your version of x265 is too old! (Minimum required revision is %1.%2+%3)").arg(QString::number(VERSION_X265_MINIMUM_VER
/ 10), QString::number(VERSION_X265_MINIMUM_VER
% 10), QString::number(VERSION_X265_MINIMUM_REV
)));
286 else if(core
> VERSION_X265_MINIMUM_VER
)
288 log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
289 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)));
295 // ------------------------------------------------------------
296 // Encoding Functions
297 // ------------------------------------------------------------
299 void X265Encoder::buildCommandLine(QStringList
&cmdLine
, const bool &usePipe
, const ClipInfo
&clipInfo
, const QString
&indexFile
, const int &pass
, const QString
&passLogFile
)
301 double crf_int
= 0.0, crf_frc
= 0.0;
304 switch (m_options
->encVariant())
307 cmdLine
<< QString::number(8);
310 cmdLine
<< QString::number(10);
313 cmdLine
<< QString::number(12);
316 MUTILS_THROW("Unknown encoder variant!");
319 switch(m_options
->rcMode())
322 crf_frc
= modf(m_options
->quantizer(), &crf_int
);
323 cmdLine
<< "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int
)), QString::number(qRound(crf_frc
* 10.0)));
326 cmdLine
<< "--qp" << QString::number(qRound(m_options
->quantizer()));
330 cmdLine
<< "--bitrate" << QString::number(m_options
->bitrate());
333 MUTILS_THROW("Bad rate-control mode !!!");
337 if((pass
== 1) || (pass
== 2))
339 cmdLine
<< "--pass" << QString::number(pass
);
340 cmdLine
<< "--stats" << QDir::toNativeSeparators(passLogFile
);
343 const QString preset
= m_options
->preset().simplified().toLower();
344 if(!preset
.isEmpty())
346 if(preset
.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
348 cmdLine
<< "--preset" << preset
;
352 const QString tune
= m_options
->tune().simplified().toLower();
355 if(tune
.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
357 cmdLine
<< "--tune" << tune
;
361 const QString profile
= m_options
->profile().simplified().toLower();
362 if(!profile
.isEmpty())
364 if(profile
.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED
), Qt::CaseInsensitive
) != 0)
366 cmdLine
<< "--profile" << profile
;
370 if(!m_options
->customEncParams().isEmpty())
372 QStringList customArgs
= splitParams(m_options
->customEncParams(), m_sourceFile
, m_outputFile
);
375 QStringList::iterator i
= customArgs
.begin();
376 while(i
!= customArgs
.end())
378 bool bModified
= false;
379 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--fps");
380 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--frames");
384 cmdLine
.append(customArgs
);
387 cmdLine
<< "--output" << QDir::toNativeSeparators(m_outputFile
);
391 if (clipInfo
.getFrameCount() < 1)
393 MUTILS_THROW("Frames not set!");
395 cmdLine
<< "--frames" << QString::number(clipInfo
.getFrameCount());
396 cmdLine
<< "--y4m" << "-";
400 cmdLine
<< QDir::toNativeSeparators(m_sourceFile
);
404 void X265Encoder::runEncodingPass_init(QList
<QRegExp
*> &patterns
)
406 patterns
<< new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames"); //regExpProgress
407 patterns
<< new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
408 patterns
<< new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
409 patterns
<< new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
412 void X265Encoder::runEncodingPass_parseLine(const QString
&line
, const QList
<QRegExp
*> &patterns
, const ClipInfo
&clipInfo
, const int &pass
, double &last_progress
, double &size_estimate
)
415 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
417 X265_UPDATE_PROGRESS(patterns
[0]);
419 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
422 unsigned int progress
= patterns
[1]->cap(1).toUInt(&ok
);
423 setStatus(JobStatus_Indexing
);
426 setProgress(progress
);
428 setDetails(line
.mid(offset
).trimmed());
430 else if((offset
= patterns
[2]->lastIndexIn(line
)) >= 0)
432 setStatus((pass
== 2) ? JobStatus_Running_Pass2
: ((pass
== 1) ? JobStatus_Running_Pass1
: JobStatus_Running
));
433 setDetails(line
.mid(offset
).trimmed());
435 else if((offset
= patterns
[3]->lastIndexIn(line
)) >= 0)
437 X265_UPDATE_PROGRESS(patterns
[3]);
439 else if(!line
.isEmpty())