1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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"
29 #include "mediainfo.h"
30 #include "model_sysinfo.h"
33 #include <MUtils/Exception.h>
36 #include <QStringList>
41 static const unsigned int VERSION_X265_MINIMUM_VER
= 17;
42 static const unsigned int VERSION_X265_MINIMUM_REV
= 382;
44 // ------------------------------------------------------------
46 // ------------------------------------------------------------
48 #define X265_UPDATE_PROGRESS(X) do \
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)); \
56 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
57 if(!qFuzzyCompare(progress, last_progress)) \
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; \
64 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
68 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
70 if(ITER != LIST.end()) \
72 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
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()) \
78 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
86 // ------------------------------------------------------------
88 // ------------------------------------------------------------
90 class X265EncoderInfo
: public AbstractEncoderInfo
93 virtual QFlags
<OptionsModel::EncVariant
> getVariants(void) const
95 QFlags
<OptionsModel::EncVariant
> variants
;
96 variants
|= OptionsModel::EncVariant_8Bit
;
97 variants
|= OptionsModel::EncVariant_10Bit
;
98 variants
|= OptionsModel::EncVariant_12Bit
;
102 virtual QStringList
getTunings(void) const
105 tunings
<< "Grain" << "PSNR" << "SSIM" << "FastDecode" << "ZeroLatency";
109 virtual QStringList
getProfiles(const int &variant
) const
111 QStringList profiles
;
114 case OptionsModel::EncVariant_8Bit
:
115 profiles
<< "main" << "main-intra" << "mainstillpicture" << "main444-8" << "main444-intra" << "main444-stillpicture";
117 case OptionsModel::EncVariant_10Bit
:
118 profiles
<< "main10" << "main10-intra" << "main422-10" << "main422-10-intra" << "main444-10" << "main444-10-intra";
120 case OptionsModel::EncVariant_12Bit
:
121 profiles
<< "main12" << "main12-intra" << "main422-12" << "main422-12-intra" << "main444-12" << "main444-12-intra";
127 virtual QStringList
supportedOutputFormats(void) const
134 virtual bool isRCModeSupported(const int rcMode
) const
138 case OptionsModel::RCMode_CRF
:
139 case OptionsModel::RCMode_CQ
:
140 case OptionsModel::RCMode_2Pass
:
141 case OptionsModel::RCMode_ABR
:
148 virtual bool isInputTypeSupported(const int format
) const
152 case MediaInfo::FILETYPE_YUV4MPEG2
:
159 virtual QString
getBinaryPath(const SysinfoModel
*sysinfo
, const OptionsModel::EncArch
&encArch
, const OptionsModel::EncVariant
&encVariant
) const
161 QString arch
, variant
;
164 case OptionsModel::EncArch_x86_32
: arch
= "x86"; break;
165 case OptionsModel::EncArch_x86_64
: arch
= "x64"; break;
166 default: MUTILS_THROW("Unknown encoder arch!");
170 case OptionsModel::EncVariant_8Bit
: variant
= "8bit"; break;
171 case OptionsModel::EncVariant_10Bit
: variant
= "10bit"; break;
172 case OptionsModel::EncVariant_12Bit
: 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::getEncoderInfo(void)
183 return s_x265EncoderInfo
;
186 // ------------------------------------------------------------
187 // Constructor & Destructor
188 // ------------------------------------------------------------
190 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
)
192 AbstractEncoder(jobObject
, options
, sysinfo
, preferences
, jobStatus
, abort
, pause
, semaphorePause
, sourceFile
, outputFile
)
194 if(options
->encType() != OptionsModel::EncType_X265
)
196 MUTILS_THROW("Invalid encoder type!");
200 X265Encoder::~X265Encoder(void)
202 /*Nothing to do here*/
205 QString
X265Encoder::getName(void) const
207 QString arch
, variant
;
208 switch(m_options
->encArch())
210 case OptionsModel::EncArch_x86_32
: arch
= "x86"; break;
211 case OptionsModel::EncArch_x86_64
: arch
= "x64"; break;
212 default: MUTILS_THROW("Unknown encoder arch!");
214 switch(m_options
->encVariant())
216 case OptionsModel::EncVariant_8Bit
: variant
= "8-Bit"; break;
217 case OptionsModel::EncVariant_10Bit
: variant
= "10-Bit"; break;
218 case OptionsModel::EncVariant_12Bit
: variant
= "12-Bit"; break;
219 default: MUTILS_THROW("Unknown encoder arch!");
221 return QString("x265 (H.265/HEVC), %1, %2").arg(arch
, variant
);
224 // ------------------------------------------------------------
226 // ------------------------------------------------------------
228 void X265Encoder::checkVersion_init(QList
<QRegExp
*> &patterns
, QStringList
&cmdLine
)
230 cmdLine
<< "--version";
231 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\+(\\d+)\\b", Qt::CaseInsensitive
);
232 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\b", Qt::CaseInsensitive
);
235 void X265Encoder::checkVersion_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, unsigned int &core
, unsigned int &build
, bool &modified
)
239 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
241 bool ok
[3] = { false, false, false };
242 unsigned int temp
[3];
243 temp
[0] = patterns
[0]->cap(1).toUInt(&ok
[0]);
244 temp
[1] = patterns
[0]->cap(2).toUInt(&ok
[1]);
245 temp
[2] = patterns
[0]->cap(3).toUInt(&ok
[2]);
248 core
= (10 * temp
[0]) + temp
[1];
250 if(ok
[2]) build
= temp
[2];
252 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
254 bool ok
[2] = { false, false };
255 unsigned int temp
[2];
256 temp
[0] = patterns
[1]->cap(1).toUInt(&ok
[0]);
257 temp
[1] = patterns
[1]->cap(2).toUInt(&ok
[1]);
260 core
= (10 * temp
[0]) + temp
[1];
271 QString
X265Encoder::printVersion(const unsigned int &revision
, const bool &modified
)
273 unsigned int core
, build
;
274 splitRevision(revision
, core
, build
);
276 return tr("x265 version: %1.%2+%3").arg(QString::number(core
/ 10), QString::number(core
% 10), QString::number(build
));
279 bool X265Encoder::isVersionSupported(const unsigned int &revision
, const bool &modified
)
281 unsigned int core
, build
;
282 splitRevision(revision
, core
, build
);
284 if((core
< VERSION_X265_MINIMUM_VER
) || ((core
== VERSION_X265_MINIMUM_VER
) && (build
< VERSION_X265_MINIMUM_REV
)))
286 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
)));
289 else if(core
> VERSION_X265_MINIMUM_VER
)
291 log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
292 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)));
298 // ------------------------------------------------------------
299 // Encoding Functions
300 // ------------------------------------------------------------
302 void X265Encoder::buildCommandLine(QStringList
&cmdLine
, const bool &usePipe
, const unsigned int &frames
, const QString
&indexFile
, const int &pass
, const QString
&passLogFile
)
304 double crf_int
= 0.0, crf_frc
= 0.0;
306 switch(m_options
->rcMode())
308 case OptionsModel::RCMode_CQ
:
309 cmdLine
<< "--qp" << QString::number(qRound(m_options
->quantizer()));
311 case OptionsModel::RCMode_CRF
:
312 crf_frc
= modf(m_options
->quantizer(), &crf_int
);
313 cmdLine
<< "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int
)), QString::number(qRound(crf_frc
* 10.0)));
315 case OptionsModel::RCMode_2Pass
:
316 case OptionsModel::RCMode_ABR
:
317 cmdLine
<< "--bitrate" << QString::number(m_options
->bitrate());
320 MUTILS_THROW("Bad rate-control mode !!!");
324 if((pass
== 1) || (pass
== 2))
326 cmdLine
<< "--pass" << QString::number(pass
);
327 cmdLine
<< "--stats" << QDir::toNativeSeparators(passLogFile
);
330 cmdLine
<< "--preset" << m_options
->preset().toLower();
332 if(!m_options
->tune().simplified().isEmpty())
334 if(m_options
->tune().simplified().compare(QString::fromLatin1(OptionsModel::TUNING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
336 cmdLine
<< "--tune" << m_options
->tune().simplified().toLower();
340 if(!m_options
->profile().simplified().isEmpty())
342 if(m_options
->profile().simplified().compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED
), Qt::CaseInsensitive
) != 0)
344 cmdLine
<< "--profile" << m_options
->profile().simplified().toLower();
348 if(!m_options
->customEncParams().isEmpty())
350 QStringList customArgs
= splitParams(m_options
->customEncParams(), m_sourceFile
, m_outputFile
);
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");
362 cmdLine
.append(customArgs
);
365 cmdLine
<< "--output" << QDir::toNativeSeparators(m_outputFile
);
369 if(frames
< 1) MUTILS_THROW("Frames not set!");
370 cmdLine
<< "--frames" << QString::number(frames
);
371 cmdLine
<< "--y4m" << "-";
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 int &pass
, double &last_progress
, double &size_estimate
)
390 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
392 X265_UPDATE_PROGRESS(patterns
[0]);
394 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
397 unsigned int progress
= patterns
[1]->cap(1).toUInt(&ok
);
398 setStatus(JobStatus_Indexing
);
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())