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"
28 #include "mediainfo.h"
30 #include <QStringList>
35 static const unsigned int VERSION_X265_MINIMUM_VER
= 14;
36 static const unsigned int VERSION_X265_MINIMUM_REV
= 0;
38 // ------------------------------------------------------------
40 // ------------------------------------------------------------
42 #define X265_UPDATE_PROGRESS(X) do \
44 bool ok[2] = { false, false }; \
45 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
46 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
47 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
50 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
51 if(!qFuzzyCompare(progress, last_progress)) \
53 setProgress(floor(progress * 100.0)); \
54 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
55 last_progress = progress; \
58 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
62 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
64 if(ITER != LIST.end()) \
66 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
68 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
69 ITER = LIST.erase(ITER); \
70 if(ITER != LIST.end()) \
72 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
80 static QString
MAKE_NAME(const char *baseName
, const OptionsModel
*options
)
82 const QString arch
= (options
->encArch() == OptionsModel::EncArch_x64
) ? "x64" : "x86";
83 const QString vari
= (options
->encVariant() == OptionsModel::EncVariant_HiBit
) ? "16-Bit" : "8-Bit";
84 return QString("%1, %2, %3").arg(QString::fromLatin1(baseName
), arch
, vari
);
87 // ------------------------------------------------------------
89 // ------------------------------------------------------------
91 class X265EncoderInfo
: public AbstractEncoderInfo
94 virtual QString
getVariantId(const int &variant
) const
98 case OptionsModel::EncVariant_LoBit
:
99 return QString::fromLatin1("8-Bit");
100 case OptionsModel::EncVariant_HiBit
:
101 return QString::fromLatin1("16-Bit");
103 return QString::fromLatin1("N/A");
107 virtual QStringList
getTunings(void) const
110 tunings
<< "Grain" << "PSNR" << "SSIM" << "FastDecode" << "ZeroLatency";
114 virtual QStringList
getProfiles(const int &variant
) const
116 return QStringList();
119 virtual QStringList
supportedOutputFormats(void) const
126 virtual bool isRCModeSupported(const int rcMode
) const
130 case OptionsModel::RCMode_CRF
:
131 case OptionsModel::RCMode_CQ
:
132 case OptionsModel::RCMode_2Pass
:
133 case OptionsModel::RCMode_ABR
:
140 virtual bool isInputTypeSupported(const int format
) const
144 case MediaInfo::FILETYPE_YUV4MPEG2
:
152 static const X265EncoderInfo s_x265EncoderInfo
;
154 const AbstractEncoderInfo
&X265Encoder::getEncoderInfo(void)
156 return s_x265EncoderInfo
;
159 // ------------------------------------------------------------
160 // Constructor & Destructor
161 // ------------------------------------------------------------
163 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
)
165 AbstractEncoder(jobObject
, options
, sysinfo
, preferences
, jobStatus
, abort
, pause
, semaphorePause
, sourceFile
, outputFile
),
166 m_encoderName(MAKE_NAME("x265 (H.265/HEVC)", m_options
)),
167 m_binaryFile(ENC_BINARY(sysinfo
, options
))
169 if(options
->encType() != OptionsModel::EncType_X265
)
171 THROW("Invalid encoder type!");
175 X265Encoder::~X265Encoder(void)
177 /*Nothing to do here*/
180 const QString
&X265Encoder::getName(void)
182 return m_encoderName
;
185 // ------------------------------------------------------------
187 // ------------------------------------------------------------
189 void X265Encoder::checkVersion_init(QList
<QRegExp
*> &patterns
, QStringList
&cmdLine
)
191 cmdLine
<< "--version";
192 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\+(\\d+)\\b", Qt::CaseInsensitive
);
193 patterns
<< new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+(\\d)\\.(\\d+)\\b", Qt::CaseInsensitive
);
196 void X265Encoder::checkVersion_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, unsigned int &core
, unsigned int &build
, bool &modified
)
200 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
202 bool ok
[3] = { false, false, false };
203 unsigned int temp
[3];
204 temp
[0] = patterns
[0]->cap(1).toUInt(&ok
[0]);
205 temp
[1] = patterns
[0]->cap(2).toUInt(&ok
[1]);
206 temp
[2] = patterns
[0]->cap(3).toUInt(&ok
[2]);
209 core
= (10 * temp
[0]) + temp
[1];
211 if(ok
[2]) build
= temp
[2];
213 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
215 bool ok
[2] = { false, false };
216 unsigned int temp
[2];
217 temp
[0] = patterns
[1]->cap(1).toUInt(&ok
[0]);
218 temp
[1] = patterns
[1]->cap(2).toUInt(&ok
[1]);
221 core
= (10 * temp
[0]) + temp
[1];
232 QString
X265Encoder::printVersion(const unsigned int &revision
, const bool &modified
)
234 unsigned int core
, build
;
235 splitRevision(revision
, core
, build
);
237 return tr("x265 version: %1.%2+%3").arg(QString::number(core
/ 10), QString::number(core
% 10), QString::number(build
));
240 bool X265Encoder::isVersionSupported(const unsigned int &revision
, const bool &modified
)
242 unsigned int core
, build
;
243 splitRevision(revision
, core
, build
);
245 if((core
< VERSION_X265_MINIMUM_VER
) || ((core
== VERSION_X265_MINIMUM_VER
) && (build
< VERSION_X265_MINIMUM_REV
)))
247 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
)));
250 else if(core
> VERSION_X265_MINIMUM_VER
)
252 log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
253 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)));
259 // ------------------------------------------------------------
260 // Encoding Functions
261 // ------------------------------------------------------------
263 void X265Encoder::buildCommandLine(QStringList
&cmdLine
, const bool &usePipe
, const unsigned int &frames
, const QString
&indexFile
, const int &pass
, const QString
&passLogFile
)
265 double crf_int
= 0.0, crf_frc
= 0.0;
267 switch(m_options
->rcMode())
269 case OptionsModel::RCMode_CQ
:
270 cmdLine
<< "--qp" << QString::number(qRound(m_options
->quantizer()));
272 case OptionsModel::RCMode_CRF
:
273 crf_frc
= modf(m_options
->quantizer(), &crf_int
);
274 cmdLine
<< "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int
)), QString::number(qRound(crf_frc
* 10.0)));
276 case OptionsModel::RCMode_2Pass
:
277 case OptionsModel::RCMode_ABR
:
278 cmdLine
<< "--bitrate" << QString::number(m_options
->bitrate());
281 THROW("Bad rate-control mode !!!");
285 if((pass
== 1) || (pass
== 2))
287 cmdLine
<< "--pass" << QString::number(pass
);
288 cmdLine
<< "--stats" << QDir::toNativeSeparators(passLogFile
);
291 cmdLine
<< "--preset" << m_options
->preset().toLower();
293 if(!m_options
->tune().simplified().isEmpty())
295 if(m_options
->tune().simplified().compare(QString::fromLatin1(OptionsModel::TUNING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
297 cmdLine
<< "--tune" << m_options
->tune().simplified().toLower();
301 if(m_options
->profile().compare("auto", Qt::CaseInsensitive
) != 0)
303 if((m_options
->encType() == OptionsModel::EncType_X264
) && (m_options
->encVariant() == OptionsModel::EncVariant_LoBit
))
305 cmdLine
<< "--profile" << m_options
->profile().toLower();
309 if(!m_options
->customEncParams().isEmpty())
311 QStringList customArgs
= splitParams(m_options
->customEncParams(), m_sourceFile
, m_outputFile
);
314 QStringList::iterator i
= customArgs
.begin();
315 while(i
!= customArgs
.end())
317 bool bModified
= false;
318 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--fps");
319 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--frames");
323 cmdLine
.append(customArgs
);
326 cmdLine
<< "--output" << QDir::toNativeSeparators(m_outputFile
);
330 if(frames
< 1) THROW("Frames not set!");
331 cmdLine
<< "--frames" << QString::number(frames
);
332 cmdLine
<< "--y4m" << "-";
336 cmdLine
<< QDir::toNativeSeparators(m_sourceFile
);
340 void X265Encoder::runEncodingPass_init(QList
<QRegExp
*> &patterns
)
342 patterns
<< new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames"); //regExpProgress
343 patterns
<< new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
344 patterns
<< new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
345 patterns
<< new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
348 void X265Encoder::runEncodingPass_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, const int &pass
, double &last_progress
, double &size_estimate
)
351 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
353 X265_UPDATE_PROGRESS(patterns
[0]);
355 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
358 unsigned int progress
= patterns
[1]->cap(1).toUInt(&ok
);
359 setStatus(JobStatus_Indexing
);
362 setProgress(progress
);
364 setDetails(line
.mid(offset
).trimmed());
366 else if((offset
= patterns
[2]->lastIndexIn(line
)) >= 0)
368 setStatus((pass
== 2) ? JobStatus_Running_Pass2
: ((pass
== 1) ? JobStatus_Running_Pass1
: JobStatus_Running
));
369 setDetails(line
.mid(offset
).trimmed());
371 else if((offset
= patterns
[3]->lastIndexIn(line
)) >= 0)
373 X265_UPDATE_PROGRESS(patterns
[3]);
375 else if(!line
.isEmpty())