1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 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_x264.h"
26 #include "model_options.h"
27 #include "model_status.h"
28 #include "mediainfo.h"
29 #include "model_sysinfo.h"
32 #include <MUtils/Exception.h>
35 #include <QStringList>
41 static const unsigned int VERSION_X264_MINIMUM_REV
= 2668;
42 static const unsigned int VERSION_X264_CURRENT_API
= 148;
44 // ------------------------------------------------------------
46 // ------------------------------------------------------------
48 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
50 if(ITER != LIST.end()) \
52 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
54 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
55 ITER = LIST.erase(ITER); \
56 if(ITER != LIST.end()) \
58 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
66 #define X264_UPDATE_PROGRESS(X) do \
68 bool ok[2] = { false, false }; \
69 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
70 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
71 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
74 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
75 if(!qFuzzyCompare(progress, last_progress)) \
77 setProgress(floor(progress * 100.0)); \
78 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
79 last_progress = progress; \
82 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
86 // ------------------------------------------------------------
88 // ------------------------------------------------------------
90 class X264EncoderInfo
: public AbstractEncoderInfo
93 virtual QString
getName(void) const
95 return "x264 (AVC/H.264)";
98 virtual QStringList
getArchitectures(void) const
100 return QStringList() << "32-Bit (x86)" << "64-Bit (x64)";
103 virtual QStringList
getVariants(void) const
105 return QStringList() << "8-Bit" << "10-Bit";
108 virtual QList
<RCMode
> getRCModes(void) const
110 return QList
<RCMode
>()
111 << qMakePair(QString("CRF"), RC_TYPE_QUANTIZER
)
112 << qMakePair(QString("CQ"), RC_TYPE_QUANTIZER
)
113 << qMakePair(QString("2-Pass"), RC_TYPE_MULTIPASS
)
114 << qMakePair(QString("ABR"), RC_TYPE_RATE_KBPS
);
117 virtual QStringList
getTunings(void) const
120 << "Film" << "Animation" << "Grain"
121 << "StillImage" << "PSNR" << "SSIM"
122 << "FastDecode" << "ZeroLatency" << "Touhou";
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
<< "Baseline" << "Main" << "High"; break;
138 case 1: profiles
<< "High10" << "High422" << "High444"; break;
139 default: MUTILS_THROW("Unknown encoder variant!");
144 virtual QStringList
supportedOutputFormats(void) const
146 return QStringList() << "264" << "mkv" << "mp4";
149 virtual bool isInputTypeSupported(const int format
) const
153 case MediaInfo::FILETYPE_AVISYNTH
:
154 case MediaInfo::FILETYPE_YUV4MPEG2
:
155 case MediaInfo::FILETYPE_UNKNOWN
:
162 virtual QString
getBinaryPath(const SysinfoModel
*sysinfo
, const quint32
&encArch
, const quint32
&encVariant
) const
164 QString arch
, variant
;
167 case 0: arch
= "x86"; break;
168 case 1: arch
= "x64"; break;
169 default: MUTILS_THROW("Unknown encoder arch!");
173 case 0: variant
= "8bit"; break;
174 case 1: variant
= "10bit"; break;
175 default: MUTILS_THROW("Unknown encoder variant!");
177 return QString("%1/toolset/%2/x264_%3_%2.exe").arg(sysinfo
->getAppPath(), arch
, variant
);
181 static const X264EncoderInfo s_x264EncoderInfo
;
183 const AbstractEncoderInfo
&X264Encoder::getEncoderInfo(void)
185 return s_x264EncoderInfo
;
188 // ------------------------------------------------------------
189 // Constructor & Destructor
190 // ------------------------------------------------------------
192 X264Encoder::X264Encoder(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
)
194 AbstractEncoder(jobObject
, options
, sysinfo
, preferences
, jobStatus
, abort
, pause
, semaphorePause
, sourceFile
, outputFile
)
196 if(options
->encType() != OptionsModel::EncType_X264
)
198 MUTILS_THROW("Invalid encoder type!");
202 X264Encoder::~X264Encoder(void)
204 /*Nothing to do here*/
207 QString
X264Encoder::getName(void) const
209 return s_x264EncoderInfo
.getFullName(m_options
->encArch(), m_options
->encVariant());
212 // ------------------------------------------------------------
214 // ------------------------------------------------------------
216 void X264Encoder::checkVersion_init(QList
<QRegExp
*> &patterns
, QStringList
&cmdLine
)
218 cmdLine
<< "--version";
219 patterns
<< new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive
);
220 patterns
<< new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive
);
223 void X264Encoder::checkVersion_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, unsigned int &core
, unsigned int &build
, bool &modified
)
227 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
229 bool ok1
= false, ok2
= false;
230 unsigned int temp1
= patterns
[0]->cap(2).toUInt(&ok1
);
231 unsigned int temp2
= patterns
[0]->cap(3).toUInt(&ok2
);
232 if(ok1
&& ok2
&& (temp1
> 0) && (temp2
> 0))
238 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
240 bool ok1
= false, ok2
= false;
241 unsigned int temp1
= patterns
[1]->cap(2).toUInt(&ok1
);
242 unsigned int temp2
= patterns
[1]->cap(3).toUInt(&ok2
);
243 if(ok1
&& ok2
&& (temp1
> 0) && (temp2
> 0))
257 QString
X264Encoder::printVersion(const unsigned int &revision
, const bool &modified
)
259 unsigned int core
, build
;
260 splitRevision(revision
, core
, build
);
262 QString versionStr
= tr("x264 revision: %1 (core #%2)").arg(QString::number(build
), QString::number(core
));
265 versionStr
.append(tr(" - with custom patches!"));
271 bool X264Encoder::isVersionSupported(const unsigned int &revision
, const bool &modified
)
273 unsigned int core
, build
;
274 splitRevision(revision
, core
, build
);
276 if(build
< VERSION_X264_MINIMUM_REV
)
278 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV
)));
282 if(core
!= VERSION_X264_CURRENT_API
)
284 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
285 log(tr("This application works best with x264 core (API) version %1. Newer versions may work or not.").arg(QString::number(VERSION_X264_CURRENT_API
)));
291 // ------------------------------------------------------------
292 // Encoding Functions
293 // ------------------------------------------------------------
295 void X264Encoder::buildCommandLine(QStringList
&cmdLine
, const bool &usePipe
, const unsigned int &frames
, const QString
&indexFile
, const int &pass
, const QString
&passLogFile
)
297 double crf_int
= 0.0, crf_frc
= 0.0;
299 switch(m_options
->rcMode())
302 cmdLine
<< "--qp" << QString::number(qRound(m_options
->quantizer()));
305 crf_frc
= modf(m_options
->quantizer(), &crf_int
);
306 cmdLine
<< "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int
)), QString::number(qRound(crf_frc
* 10.0)));
310 cmdLine
<< "--bitrate" << QString::number(m_options
->bitrate());
313 MUTILS_THROW("Bad rate-control mode !!!");
316 if((pass
== 1) || (pass
== 2))
318 cmdLine
<< "--pass" << QString::number(pass
);
319 cmdLine
<< "--stats" << QDir::toNativeSeparators(passLogFile
);
322 const QString preset
= m_options
->preset().simplified().toLower();
323 if(!preset
.isEmpty())
325 if(preset
.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
327 cmdLine
<< "--preset" << preset
;
331 const QString tune
= m_options
->tune().simplified().toLower();
334 if(tune
.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
336 cmdLine
<< "--tune" << tune
;
340 const QString profile
= m_options
->profile().simplified().toLower();
341 if(!profile
.isEmpty())
343 if(profile
.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED
), Qt::CaseInsensitive
) != 0)
345 cmdLine
<< "--profile" << profile
;
349 if(!m_options
->customEncParams().isEmpty())
351 QStringList customArgs
= splitParams(m_options
->customEncParams(), m_sourceFile
, m_outputFile
);
354 QStringList::iterator i
= customArgs
.begin();
355 while(i
!= customArgs
.end())
357 bool bModified
= false;
358 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--fps");
359 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--frames");
363 cmdLine
.append(customArgs
);
366 cmdLine
<< "--output" << QDir::toNativeSeparators(m_outputFile
);
370 if(frames
< 1) MUTILS_THROW("Frames not set!");
371 cmdLine
<< "--frames" << QString::number(frames
);
372 cmdLine
<< "--demuxer" << "y4m";
373 cmdLine
<< "--stdin" << "y4m" << "-";
377 cmdLine
<< "--index" << QDir::toNativeSeparators(indexFile
);
378 cmdLine
<< QDir::toNativeSeparators(m_sourceFile
);
382 void X264Encoder::runEncodingPass_init(QList
<QRegExp
*> &patterns
)
384 patterns
<< new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
385 patterns
<< new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
386 patterns
<< new QRegExp("^(\\d+) frames:");
387 patterns
<< new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
390 void X264Encoder::runEncodingPass_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, const unsigned int &totalFrames
, const int &pass
, double &last_progress
, double &size_estimate
)
393 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
395 X264_UPDATE_PROGRESS(patterns
[0]);
397 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
400 unsigned int progress
= patterns
[1]->cap(1).toUInt(&ok
);
401 setStatus(JobStatus_Indexing
);
404 setProgress(progress
);
406 setDetails(line
.mid(offset
).trimmed());
408 else if((offset
= patterns
[2]->lastIndexIn(line
)) >= 0)
410 setStatus((pass
== 2) ? JobStatus_Running_Pass2
: ((pass
== 1) ? JobStatus_Running_Pass1
: JobStatus_Running
));
411 setDetails(line
.mid(offset
).trimmed());
413 else if((offset
= patterns
[3]->lastIndexIn(line
)) >= 0)
415 X264_UPDATE_PROGRESS(patterns
[3]);
417 else if(!line
.isEmpty())