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"
30 #include "model_clipInfo.h"
33 #include <MUtils/Exception.h>
36 #include <QStringList>
42 static const unsigned int VERSION_X264_MINIMUM_REV
= 2708;
43 static const unsigned int VERSION_X264_CURRENT_API
= 148;
45 // ------------------------------------------------------------
47 // ------------------------------------------------------------
49 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
51 if(ITER != LIST.end()) \
53 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
55 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
56 ITER = LIST.erase(ITER); \
57 if(ITER != LIST.end()) \
59 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
67 #define X264_UPDATE_PROGRESS(X) do \
69 bool ok[2] = { false, false }; \
70 unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
71 unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
72 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
75 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
76 if(!qFuzzyCompare(progress, last_progress)) \
78 setProgress(floor(progress * 100.0)); \
79 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
80 last_progress = progress; \
83 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
87 // ------------------------------------------------------------
89 // ------------------------------------------------------------
91 class X264EncoderInfo
: public AbstractEncoderInfo
94 virtual QString
getName(void) const
96 return "x264 (AVC/H.264)";
99 virtual QList
<ArchId
> getArchitectures(void) const
101 return QList
<ArchId
>()
102 << qMakePair(QString("32-Bit (x86)"), ARCH_TYPE_X86
)
103 << qMakePair(QString("64-Bit (x64)"), ARCH_TYPE_X64
);
106 virtual QStringList
getVariants(void) const
108 return QStringList() << "8-Bit" << "10-Bit";
111 virtual QList
<RCMode
> getRCModes(void) const
113 return QList
<RCMode
>()
114 << qMakePair(QString("CRF"), RC_TYPE_QUANTIZER
)
115 << qMakePair(QString("CQ"), RC_TYPE_QUANTIZER
)
116 << qMakePair(QString("2-Pass"), RC_TYPE_MULTIPASS
)
117 << qMakePair(QString("ABR"), RC_TYPE_RATE_KBPS
);
120 virtual QStringList
getTunings(void) const
123 << "Film" << "Animation" << "Grain"
124 << "StillImage" << "PSNR" << "SSIM"
125 << "FastDecode" << "ZeroLatency" << "Touhou";
128 virtual QStringList
getPresets(void) const
131 << "ultrafast" << "superfast" << "veryfast" << "faster" << "fast"
132 << "medium" << "slow" << "slower" << "veryslow" << "placebo";
135 virtual QStringList
getProfiles(const quint32
&variant
) const
137 QStringList profiles
;
140 case 0: profiles
<< "Baseline" << "Main" << "High"; break;
141 case 1: profiles
<< "High10" << "High422" << "High444"; break;
142 default: MUTILS_THROW("Unknown encoder variant!");
147 virtual QStringList
supportedOutputFormats(void) const
149 return QStringList() << "264" << "mkv" << "mp4";
152 virtual bool isInputTypeSupported(const int format
) const
156 case MediaInfo::FILETYPE_AVISYNTH
:
157 case MediaInfo::FILETYPE_YUV4MPEG2
:
158 case MediaInfo::FILETYPE_UNKNOWN
:
165 virtual QString
getBinaryPath(const SysinfoModel
*sysinfo
, const quint32
&encArch
, const quint32
&encVariant
) const
167 QString arch
, variant
;
170 case 0: arch
= "x86"; break;
171 case 1: arch
= "x64"; break;
172 default: MUTILS_THROW("Unknown encoder arch!");
176 case 0: variant
= "8bit"; break;
177 case 1: variant
= "10bit"; break;
178 default: MUTILS_THROW("Unknown encoder variant!");
180 return QString("%1/toolset/%2/x264_%3_%2.exe").arg(sysinfo
->getAppPath(), arch
, variant
);
183 virtual QString
getHelpCommand(void) const
189 static const X264EncoderInfo s_x264EncoderInfo
;
191 const AbstractEncoderInfo
& X264Encoder::encoderInfo(void)
193 return s_x264EncoderInfo
;
196 const AbstractEncoderInfo
&X264Encoder::getEncoderInfo(void) const
198 return encoderInfo();
201 // ------------------------------------------------------------
202 // Constructor & Destructor
203 // ------------------------------------------------------------
205 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
)
207 AbstractEncoder(jobObject
, options
, sysinfo
, preferences
, jobStatus
, abort
, pause
, semaphorePause
, sourceFile
, outputFile
)
209 if(options
->encType() != OptionsModel::EncType_X264
)
211 MUTILS_THROW("Invalid encoder type!");
215 X264Encoder::~X264Encoder(void)
217 /*Nothing to do here*/
220 QString
X264Encoder::getName(void) const
222 return s_x264EncoderInfo
.getFullName(m_options
->encArch(), m_options
->encVariant());
225 // ------------------------------------------------------------
227 // ------------------------------------------------------------
229 void X264Encoder::checkVersion_init(QList
<QRegExp
*> &patterns
, QStringList
&cmdLine
)
231 cmdLine
<< "--version";
232 patterns
<< new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive
);
233 patterns
<< new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive
);
236 void X264Encoder::checkVersion_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, unsigned int &core
, unsigned int &build
, bool &modified
)
240 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
242 bool ok1
= false, ok2
= false;
243 unsigned int temp1
= patterns
[0]->cap(2).toUInt(&ok1
);
244 unsigned int temp2
= patterns
[0]->cap(3).toUInt(&ok2
);
245 if(ok1
&& ok2
&& (temp1
> 0) && (temp2
> 0))
251 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
253 bool ok1
= false, ok2
= false;
254 unsigned int temp1
= patterns
[1]->cap(2).toUInt(&ok1
);
255 unsigned int temp2
= patterns
[1]->cap(3).toUInt(&ok2
);
256 if(ok1
&& ok2
&& (temp1
> 0) && (temp2
> 0))
270 QString
X264Encoder::printVersion(const unsigned int &revision
, const bool &modified
)
272 unsigned int core
, build
;
273 splitRevision(revision
, core
, build
);
275 QString versionStr
= tr("x264 revision: %1 (core #%2)").arg(QString::number(build
), QString::number(core
));
278 versionStr
.append(tr(" - with custom patches!"));
284 bool X264Encoder::isVersionSupported(const unsigned int &revision
, const bool &modified
)
286 unsigned int core
, build
;
287 splitRevision(revision
, core
, build
);
289 if(build
< VERSION_X264_MINIMUM_REV
)
291 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV
)));
295 if(core
!= VERSION_X264_CURRENT_API
)
297 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
298 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
)));
304 // ------------------------------------------------------------
305 // Encoding Functions
306 // ------------------------------------------------------------
308 void X264Encoder::buildCommandLine(QStringList
&cmdLine
, const bool &usePipe
, const ClipInfo
&clipInfo
, const QString
&indexFile
, const int &pass
, const QString
&passLogFile
)
310 double crf_int
= 0.0, crf_frc
= 0.0;
312 switch(m_options
->rcMode())
315 crf_frc
= modf(m_options
->quantizer(), &crf_int
);
316 cmdLine
<< "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int
)), QString::number(qRound(crf_frc
* 10.0)));
319 cmdLine
<< "--qp" << QString::number(qRound(m_options
->quantizer()));
323 cmdLine
<< "--bitrate" << QString::number(m_options
->bitrate());
326 MUTILS_THROW("Bad rate-control mode !!!");
329 if((pass
== 1) || (pass
== 2))
331 cmdLine
<< "--pass" << QString::number(pass
);
332 cmdLine
<< "--stats" << QDir::toNativeSeparators(passLogFile
);
335 const QString preset
= m_options
->preset().simplified().toLower();
336 if(!preset
.isEmpty())
338 if(preset
.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
340 cmdLine
<< "--preset" << preset
;
344 const QString tune
= m_options
->tune().simplified().toLower();
347 if(tune
.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED
), Qt::CaseInsensitive
) != 0)
349 cmdLine
<< "--tune" << tune
;
353 const QString profile
= m_options
->profile().simplified().toLower();
354 if(!profile
.isEmpty())
356 if(profile
.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED
), Qt::CaseInsensitive
) != 0)
358 cmdLine
<< "--profile" << profile
;
362 if(!m_options
->customEncParams().isEmpty())
364 QStringList customArgs
= splitParams(m_options
->customEncParams(), m_sourceFile
, m_outputFile
);
367 QStringList::iterator i
= customArgs
.begin();
368 while(i
!= customArgs
.end())
370 bool bModified
= false;
371 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--fps");
372 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--frames");
376 cmdLine
.append(customArgs
);
379 cmdLine
<< "--output" << QDir::toNativeSeparators(m_outputFile
);
383 if (clipInfo
.getFrameCount() < 1)
385 MUTILS_THROW("Frames not set!");
387 cmdLine
<< "--frames" << QString::number(clipInfo
.getFrameCount());
388 cmdLine
<< "--demuxer" << "y4m";
389 cmdLine
<< "--stdin" << "y4m" << "-";
393 cmdLine
<< "--index" << QDir::toNativeSeparators(indexFile
);
394 cmdLine
<< QDir::toNativeSeparators(m_sourceFile
);
398 void X264Encoder::runEncodingPass_init(QList
<QRegExp
*> &patterns
)
400 patterns
<< new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
401 patterns
<< new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
402 patterns
<< new QRegExp("^(\\d+) frames:");
403 patterns
<< new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
406 void X264Encoder::runEncodingPass_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, const ClipInfo
&clipInfo
, const int &pass
, double &last_progress
, double &size_estimate
)
409 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
411 X264_UPDATE_PROGRESS(patterns
[0]);
413 else if((offset
= patterns
[1]->lastIndexIn(line
)) >= 0)
416 unsigned int progress
= patterns
[1]->cap(1).toUInt(&ok
);
417 setStatus(JobStatus_Indexing
);
420 setProgress(progress
);
422 setDetails(line
.mid(offset
).trimmed());
424 else if((offset
= patterns
[2]->lastIndexIn(line
)) >= 0)
426 setStatus((pass
== 2) ? JobStatus_Running_Pass2
: ((pass
== 1) ? JobStatus_Running_Pass1
: JobStatus_Running
));
427 setDetails(line
.mid(offset
).trimmed());
429 else if((offset
= patterns
[3]->lastIndexIn(line
)) >= 0)
431 X264_UPDATE_PROGRESS(patterns
[3]);
433 else if(!line
.isEmpty())