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_x264.h"
25 #include "model_options.h"
26 #include "model_status.h"
28 #include "mediainfo.h"
30 #include <QStringList>
35 static const unsigned int VERSION_X264_MINIMUM_REV = 2495;
36 static const unsigned int VERSION_X264_CURRENT_API = 144;
38 // ------------------------------------------------------------
40 // ------------------------------------------------------------
42 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
44 if(ITER != LIST.end()) \
46 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
48 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
49 ITER = LIST.erase(ITER); \
50 if(ITER != LIST.end()) \
52 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
60 #define X264_UPDATE_PROGRESS(X) do \
62 bool ok[2] = { false, false }; \
63 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
64 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
65 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
68 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
69 if(!qFuzzyCompare(progress, last_progress)) \
71 setProgress(floor(progress * 100.0)); \
72 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
73 last_progress = progress; \
76 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
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 ) ? "10-Bit" : "8-Bit";
84 return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
87 // ------------------------------------------------------------
89 // ------------------------------------------------------------
91 class X264EncoderInfo : 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("10-Bit");
103 return QString::fromLatin1("N/A");
107 virtual QStringList getTunings(void) const
111 tunings << "Film" << "Animation" << "Grain";
112 tunings << "StillImage" << "PSNR" << "SSIM";
113 tunings << "FastDecode" << "ZeroLatency" << "Touhou";
118 virtual QStringList getProfiles(const int &variant) const
120 QStringList profiles;
122 if(variant == OptionsModel::EncVariant_LoBit)
124 profiles << "Baseline" << "Main" << "High";
126 if((variant == OptionsModel::EncVariant_LoBit) || (variant == OptionsModel::EncVariant_HiBit))
128 profiles << "High10" << "High422" << "High444";
134 virtual QStringList supportedOutputFormats(void) const
137 extLst << "264" << "mkv" << "mp4";
141 virtual bool isRCModeSupported(const int 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:
169 static const X264EncoderInfo s_x264EncoderInfo;
171 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
173 return s_x264EncoderInfo;
176 // ------------------------------------------------------------
177 // Constructor & Destructor
178 // ------------------------------------------------------------
180 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)
182 AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
183 m_encoderName(MAKE_NAME("x264 (H.264/AVC)", m_options)),
184 m_binaryFile(ENC_BINARY(sysinfo, options))
186 if(options->encType() != OptionsModel::EncType_X264)
188 THROW("Invalid encoder type!");
192 X264Encoder::~X264Encoder(void)
194 /*Nothing to do here*/
197 const QString &X264Encoder::getName(void)
199 return m_encoderName;
202 // ------------------------------------------------------------
204 // ------------------------------------------------------------
206 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
208 cmdLine << "--version";
209 patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
210 patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
213 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
217 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
219 bool ok1 = false, ok2 = false;
220 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
221 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
222 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
228 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
230 bool ok1 = false, ok2 = false;
231 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
232 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
233 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
247 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
249 unsigned int core, build;
250 splitRevision(revision, core, build);
252 QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
255 versionStr.append(tr(" - with custom patches!"));
261 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
263 unsigned int core, build;
264 splitRevision(revision, core, build);
266 if(build < VERSION_X264_MINIMUM_REV)
268 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
272 if(core != VERSION_X264_CURRENT_API)
274 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
275 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)));
281 // ------------------------------------------------------------
282 // Encoding Functions
283 // ------------------------------------------------------------
285 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
287 double crf_int = 0.0, crf_frc = 0.0;
289 switch(m_options->rcMode())
291 case OptionsModel::RCMode_CQ:
292 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
294 case OptionsModel::RCMode_CRF:
295 crf_frc = modf(m_options->quantizer(), &crf_int);
296 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
298 case OptionsModel::RCMode_2Pass:
299 case OptionsModel::RCMode_ABR:
300 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
303 THROW("Bad rate-control mode !!!");
306 if((pass == 1) || (pass == 2))
308 cmdLine << "--pass" << QString::number(pass);
309 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
312 cmdLine << "--preset" << m_options->preset().toLower();
314 if(!m_options->tune().simplified().isEmpty())
316 if(m_options->tune().simplified().compare(QString::fromLatin1(OptionsModel::TUNING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
318 cmdLine << "--tune" << m_options->tune().simplified().toLower();
322 if(!m_options->profile().simplified().isEmpty())
324 if(m_options->profile().simplified().compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
326 cmdLine << "--profile" << m_options->profile().simplified().toLower();
330 if(!m_options->customEncParams().isEmpty())
332 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
335 QStringList::iterator i = customArgs.begin();
336 while(i != customArgs.end())
338 bool bModified = false;
339 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
340 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
344 cmdLine.append(customArgs);
347 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
351 if(frames < 1) THROW("Frames not set!");
352 cmdLine << "--frames" << QString::number(frames);
353 cmdLine << "--demuxer" << "y4m";
354 cmdLine << "--stdin" << "y4m" << "-";
358 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
359 cmdLine << QDir::toNativeSeparators(m_sourceFile);
363 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
365 patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
366 patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
367 patterns << new QRegExp("^(\\d+) frames:");
368 patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
371 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass, double &last_progress, double &size_estimate)
374 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
376 X264_UPDATE_PROGRESS(patterns[0]);
378 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
381 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
382 setStatus(JobStatus_Indexing);
385 setProgress(progress);
387 setDetails(line.mid(offset).trimmed());
389 else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
391 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
392 setDetails(line.mid(offset).trimmed());
394 else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
396 X264_UPDATE_PROGRESS(patterns[3]);
398 else if(!line.isEmpty())