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"
26 #include "model_options.h"
27 #include "model_status.h"
29 #include "mediainfo.h"
32 #include <MUtils/Exception.h>
35 #include <QStringList>
40 static const unsigned int VERSION_X264_MINIMUM_REV = 2533;
41 static const unsigned int VERSION_X264_CURRENT_API = 146;
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 static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
87 const QString arch = (options->encArch() == OptionsModel::EncArch_x64) ? "x64" : "x86";
88 const QString vari = (options->encVariant() == OptionsModel::EncVariant_HiBit ) ? "10-Bit" : "8-Bit";
89 return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
92 // ------------------------------------------------------------
94 // ------------------------------------------------------------
96 class X264EncoderInfo : public AbstractEncoderInfo
99 virtual QString getVariantId(const int &variant) const
103 case OptionsModel::EncVariant_LoBit:
104 return QString::fromLatin1("8-Bit");
105 case OptionsModel::EncVariant_HiBit:
106 return QString::fromLatin1("10-Bit");
108 return QString::fromLatin1("N/A");
112 virtual QStringList getTunings(void) const
116 tunings << "Film" << "Animation" << "Grain";
117 tunings << "StillImage" << "PSNR" << "SSIM";
118 tunings << "FastDecode" << "ZeroLatency" << "Touhou";
123 virtual QStringList getProfiles(const int &variant) const
125 QStringList profiles;
127 if(variant == OptionsModel::EncVariant_LoBit)
129 profiles << "Baseline" << "Main" << "High";
131 if((variant == OptionsModel::EncVariant_LoBit) || (variant == OptionsModel::EncVariant_HiBit))
133 profiles << "High10" << "High422" << "High444";
139 virtual QStringList supportedOutputFormats(void) const
142 extLst << "264" << "mkv" << "mp4";
146 virtual bool isRCModeSupported(const int rcMode) const
150 case OptionsModel::RCMode_CRF:
151 case OptionsModel::RCMode_CQ:
152 case OptionsModel::RCMode_2Pass:
153 case OptionsModel::RCMode_ABR:
160 virtual bool isInputTypeSupported(const int format) const
164 case MediaInfo::FILETYPE_AVISYNTH:
165 case MediaInfo::FILETYPE_YUV4MPEG2:
166 case MediaInfo::FILETYPE_UNKNOWN:
174 static const X264EncoderInfo s_x264EncoderInfo;
176 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
178 return s_x264EncoderInfo;
181 // ------------------------------------------------------------
182 // Constructor & Destructor
183 // ------------------------------------------------------------
185 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)
187 AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
188 m_encoderName(MAKE_NAME("x264 (H.264/AVC)", m_options)),
189 m_binaryFile(ENC_BINARY(sysinfo, options))
191 if(options->encType() != OptionsModel::EncType_X264)
193 MUTILS_THROW("Invalid encoder type!");
197 X264Encoder::~X264Encoder(void)
199 /*Nothing to do here*/
202 const QString &X264Encoder::getName(void)
204 return m_encoderName;
207 // ------------------------------------------------------------
209 // ------------------------------------------------------------
211 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
213 cmdLine << "--version";
214 patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
215 patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
218 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
222 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
224 bool ok1 = false, ok2 = false;
225 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
226 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
227 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
233 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
235 bool ok1 = false, ok2 = false;
236 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
237 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
238 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
252 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
254 unsigned int core, build;
255 splitRevision(revision, core, build);
257 QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
260 versionStr.append(tr(" - with custom patches!"));
266 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
268 unsigned int core, build;
269 splitRevision(revision, core, build);
271 if(build < VERSION_X264_MINIMUM_REV)
273 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
277 if(core != VERSION_X264_CURRENT_API)
279 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
280 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)));
286 // ------------------------------------------------------------
287 // Encoding Functions
288 // ------------------------------------------------------------
290 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
292 double crf_int = 0.0, crf_frc = 0.0;
294 switch(m_options->rcMode())
296 case OptionsModel::RCMode_CQ:
297 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
299 case OptionsModel::RCMode_CRF:
300 crf_frc = modf(m_options->quantizer(), &crf_int);
301 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
303 case OptionsModel::RCMode_2Pass:
304 case OptionsModel::RCMode_ABR:
305 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
308 MUTILS_THROW("Bad rate-control mode !!!");
311 if((pass == 1) || (pass == 2))
313 cmdLine << "--pass" << QString::number(pass);
314 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
317 cmdLine << "--preset" << m_options->preset().toLower();
319 if(!m_options->tune().simplified().isEmpty())
321 if(m_options->tune().simplified().compare(QString::fromLatin1(OptionsModel::TUNING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
323 cmdLine << "--tune" << m_options->tune().simplified().toLower();
327 if(!m_options->profile().simplified().isEmpty())
329 if(m_options->profile().simplified().compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
331 cmdLine << "--profile" << m_options->profile().simplified().toLower();
335 if(!m_options->customEncParams().isEmpty())
337 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
340 QStringList::iterator i = customArgs.begin();
341 while(i != customArgs.end())
343 bool bModified = false;
344 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
345 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
349 cmdLine.append(customArgs);
352 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
356 if(frames < 1) MUTILS_THROW("Frames not set!");
357 cmdLine << "--frames" << QString::number(frames);
358 cmdLine << "--demuxer" << "y4m";
359 cmdLine << "--stdin" << "y4m" << "-";
363 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
364 cmdLine << QDir::toNativeSeparators(m_sourceFile);
368 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
370 patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
371 patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
372 patterns << new QRegExp("^(\\d+) frames:");
373 patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
376 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass, double &last_progress, double &size_estimate)
379 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
381 X264_UPDATE_PROGRESS(patterns[0]);
383 else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
386 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
387 setStatus(JobStatus_Indexing);
390 setProgress(progress);
392 setDetails(line.mid(offset).trimmed());
394 else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
396 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
397 setDetails(line.mid(offset).trimmed());
399 else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
401 X264_UPDATE_PROGRESS(patterns[3]);
403 else if(!line.isEmpty())