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>
40 static const unsigned int VERSION_X264_MINIMUM_REV = 2643;
41 static const unsigned int VERSION_X264_CURRENT_API = 148;
43 // ------------------------------------------------------------
45 // ------------------------------------------------------------
47 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
49 if(ITER != LIST.end()) \
51 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
53 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
54 ITER = LIST.erase(ITER); \
55 if(ITER != LIST.end()) \
57 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
65 #define X264_UPDATE_PROGRESS(X) do \
67 bool ok[2] = { false, false }; \
68 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
69 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
70 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
73 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
74 if(!qFuzzyCompare(progress, last_progress)) \
76 setProgress(floor(progress * 100.0)); \
77 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
78 last_progress = progress; \
81 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
85 // ------------------------------------------------------------
87 // ------------------------------------------------------------
89 class X264EncoderInfo : public AbstractEncoderInfo
92 virtual QFlags<OptionsModel::EncVariant> getVariants(void) const
94 QFlags<OptionsModel::EncVariant> variants;
95 variants |= OptionsModel::EncVariant_8Bit;
96 variants |= OptionsModel::EncVariant_10Bit;
100 virtual QStringList getTunings(void) const
103 tunings << "Film" << "Animation" << "Grain";
104 tunings << "StillImage" << "PSNR" << "SSIM";
105 tunings << "FastDecode" << "ZeroLatency" << "Touhou";
110 virtual QStringList getPresets(void) const
113 presets << "ultrafast" << "superfast" << "veryfast" << "faster" << "fast";
114 presets << "medium" << "slow" << "slower" << "veryslow" << "placebo";
118 virtual QStringList getProfiles(const OptionsModel::EncVariant &variant) const
120 QStringList profiles;
122 if(variant == OptionsModel::EncVariant_8Bit)
124 profiles << "Baseline" << "Main" << "High";
126 if((variant == OptionsModel::EncVariant_8Bit) || (variant == OptionsModel::EncVariant_10Bit))
128 profiles << "High10" << "High422" << "High444";
134 virtual QStringList supportedOutputFormats(void) const
137 extLst << "264" << "mkv" << "mp4";
141 virtual bool isRCModeSupported(const OptionsModel::RCMode &rcMode) const
145 case OptionsModel::RCMode_CRF:
146 case OptionsModel::RCMode_CQ:
147 case OptionsModel::RCMode_2Pass:
148 case OptionsModel::RCMode_ABR:
155 virtual bool isInputTypeSupported(const int format) const
159 case MediaInfo::FILETYPE_AVISYNTH:
160 case MediaInfo::FILETYPE_YUV4MPEG2:
161 case MediaInfo::FILETYPE_UNKNOWN:
168 virtual QString getBinaryPath(const SysinfoModel *sysinfo, const OptionsModel::EncArch &encArch, const OptionsModel::EncVariant &encVariant) const
170 QString arch, variant;
173 case OptionsModel::EncArch_x86_32: arch = "x86"; break;
174 case OptionsModel::EncArch_x86_64: arch = "x64"; break;
175 default: MUTILS_THROW("Unknown encoder arch!");
179 case OptionsModel::EncVariant_8Bit: variant = "8bit"; break;
180 case OptionsModel::EncVariant_10Bit: variant = "10bit"; break;
181 default: MUTILS_THROW("Unknown encoder arch!");
183 return QString("%1/toolset/%2/x264_%3_%2.exe").arg(sysinfo->getAppPath(), arch, variant);
187 static const X264EncoderInfo s_x264EncoderInfo;
189 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
191 return s_x264EncoderInfo;
194 // ------------------------------------------------------------
195 // Constructor & Destructor
196 // ------------------------------------------------------------
198 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)
200 AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
202 if(options->encType() != OptionsModel::EncType_X264)
204 MUTILS_THROW("Invalid encoder type!");
208 X264Encoder::~X264Encoder(void)
210 /*Nothing to do here*/
213 QString X264Encoder::getName(void) const
215 QString arch, variant;
216 switch(m_options->encArch())
218 case OptionsModel::EncArch_x86_32: arch = "x86"; break;
219 case OptionsModel::EncArch_x86_64: arch = "x64"; break;
220 default: MUTILS_THROW("Unknown encoder arch!");
222 switch(m_options->encVariant())
224 case OptionsModel::EncVariant_8Bit: variant = "8-Bit"; break;
225 case OptionsModel::EncVariant_10Bit: variant = "10-Bit"; break;
226 default: MUTILS_THROW("Unknown encoder arch!");
228 return QString("x264 (H.264/AVC), %1, %2").arg(arch, variant);
231 // ------------------------------------------------------------
233 // ------------------------------------------------------------
235 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
237 cmdLine << "--version";
238 patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
239 patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
242 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
246 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
248 bool ok1 = false, ok2 = false;
249 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
250 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
251 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
257 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
259 bool ok1 = false, ok2 = false;
260 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
261 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
262 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
276 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
278 unsigned int core, build;
279 splitRevision(revision, core, build);
281 QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
284 versionStr.append(tr(" - with custom patches!"));
290 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
292 unsigned int core, build;
293 splitRevision(revision, core, build);
295 if(build < VERSION_X264_MINIMUM_REV)
297 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
301 if(core != VERSION_X264_CURRENT_API)
303 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
304 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)));
310 // ------------------------------------------------------------
311 // Encoding Functions
312 // ------------------------------------------------------------
314 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
316 double crf_int = 0.0, crf_frc = 0.0;
318 switch(m_options->rcMode())
320 case OptionsModel::RCMode_CQ:
321 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
323 case OptionsModel::RCMode_CRF:
324 crf_frc = modf(m_options->quantizer(), &crf_int);
325 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
327 case OptionsModel::RCMode_2Pass:
328 case OptionsModel::RCMode_ABR:
329 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
332 MUTILS_THROW("Bad rate-control mode !!!");
335 if((pass == 1) || (pass == 2))
337 cmdLine << "--pass" << QString::number(pass);
338 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
341 const QString preset = m_options->preset().simplified().toLower();
342 if(!preset.isEmpty())
344 if(preset.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
346 cmdLine << "--preset" << preset;
350 const QString tune = m_options->tune().simplified().toLower();
353 if(tune.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
355 cmdLine << "--tune" << tune;
359 const QString profile = m_options->profile().simplified().toLower();
360 if(!profile.isEmpty())
362 if(profile.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
364 cmdLine << "--profile" << profile;
368 if(!m_options->customEncParams().isEmpty())
370 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
373 QStringList::iterator i = customArgs.begin();
374 while(i != customArgs.end())
376 bool bModified = false;
377 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
378 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
382 cmdLine.append(customArgs);
385 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
389 if(frames < 1) MUTILS_THROW("Frames not set!");
390 cmdLine << "--frames" << QString::number(frames);
391 cmdLine << "--demuxer" << "y4m";
392 cmdLine << "--stdin" << "y4m" << "-";
396 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
397 cmdLine << QDir::toNativeSeparators(m_sourceFile);
401 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
403 patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
404 patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
405 patterns << new QRegExp("^(\\d+) frames:");
406 patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
409 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass, double &last_progress, double &size_estimate)
412 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
414 X264_UPDATE_PROGRESS(patterns[0]);
416 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
419 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
420 setStatus(JobStatus_Indexing);
423 setProgress(progress);
425 setDetails(line.mid(offset).trimmed());
427 else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
429 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
430 setDetails(line.mid(offset).trimmed());
432 else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
434 X264_UPDATE_PROGRESS(patterns[3]);
436 else if(!line.isEmpty())