Small fix.
[simple-x264-launcher.git] / src / encoder_nvenc.cpp
blob11d39c0f85965f739ee79224854768db12da8d97
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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.
9 //
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_nvenc.h"
24 //Internal
25 #include "global.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"
32 //MUtils
33 #include <MUtils/Exception.h>
35 //Qt
36 #include <QStringList>
37 #include <QDir>
38 #include <QRegExp>
39 #include <QPair>
41 //x265 version info
42 static const unsigned int VERSION_NVENCC_MINIMUM_VER = 206;
43 static const unsigned int VERSION_NVENCC_MINIMUM_API = 60;
45 // ------------------------------------------------------------
46 // Helper Macros
47 // ------------------------------------------------------------
49 #define NVENCC_UPDATE_PROGRESS(X) do \
50 { \
51 bool ok = false; \
52 unsigned int progressFrames = (X)->cap(1).toUInt(&ok); \
53 double progress = 0.0; \
54 setStatus(JobStatus_Running); \
55 if(ok && (clipInfo.getFrameCount() > 0)) \
56 { \
57 progress = (double(progressFrames) / double(clipInfo.getFrameCount())); \
58 if(!qFuzzyCompare(progress, last_progress)) \
59 { \
60 setProgress(floor(progress * 100.0)); \
61 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
62 last_progress = progress; \
63 } \
64 } \
65 setDetails(tr("[%1] %2, est. file size %3").arg(QString().sprintf("%.1f%%", 100.0 * progress), line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
66 } \
67 while(0)
69 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
70 { \
71 if(ITER != LIST.end()) \
72 { \
73 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
74 { \
75 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
76 ITER = LIST.erase(ITER); \
77 if(ITER != LIST.end()) \
78 { \
79 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
80 } \
81 FLAG = true; \
82 } \
83 } \
84 } \
85 while(0)
87 // ------------------------------------------------------------
88 // Encoder Info
89 // ------------------------------------------------------------
91 class NVEncEncoderInfo : public AbstractEncoderInfo
93 public:
94 virtual QString getName(void) const
96 return "NVEncC";
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() << "AVC" << "HEVC";
111 virtual QList<RCMode> getRCModes(void) const
113 return QList<RCMode>()
114 << qMakePair(QString("CQP"), RC_TYPE_QUANTIZER)
115 << qMakePair(QString("VBR"), RC_TYPE_RATE_KBPS)
116 << qMakePair(QString("VBR2"), RC_TYPE_RATE_KBPS)
117 << qMakePair(QString("CBR"), RC_TYPE_RATE_KBPS);
120 virtual QStringList getTunings(void) const
122 return QStringList();
125 virtual QStringList getPresets(void) const
127 return QStringList();
130 virtual QStringList getProfiles(const quint32 &variant) const
132 QStringList profiles;
133 switch(variant)
135 case 0: profiles << "baseline" << "main" << "high"; break;
136 case 1: profiles << "main"; break;
137 default: MUTILS_THROW("Unknown encoder variant!");
139 return profiles;
142 virtual QStringList supportedOutputFormats(void) const
144 QStringList extLst;
145 extLst << "mp4" << "264" << "hevc";
146 return extLst;
149 virtual bool isInputTypeSupported(const int format) const
151 switch(format)
153 case MediaInfo::FILETYPE_YUV4MPEG2:
154 return true;
155 default:
156 return false;
160 virtual QString getBinaryPath(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
162 QString arch;
163 switch(encArch)
165 case 0: arch = "x86"; break;
166 case 1: arch = "x64"; break;
167 default: MUTILS_THROW("Unknown encoder arch!");
169 switch(encVariant)
171 case 0: break;
172 case 1: break;
173 default: MUTILS_THROW("Unknown encoder variant!");
175 return QString("%1/toolset/%2/nvencc_%2.exe").arg(sysinfo->getAppPath(), arch);
178 virtual QStringList getDependencies(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
180 QString arch;
181 switch (encArch)
183 case 0: arch = "x86"; break;
184 case 1: arch = "x64"; break;
185 default: MUTILS_THROW("Unknown encoder arch!");
187 switch (encVariant)
189 case 0: break;
190 case 1: break;
191 default: MUTILS_THROW("Unknown encoder variant!");
194 return QStringList()
195 << QString("%1/toolset/%2/avcodec-57.dll" ).arg(sysinfo->getAppPath(), arch)
196 << QString("%1/toolset/%2/avfilter-6.dll" ).arg(sysinfo->getAppPath(), arch)
197 << QString("%1/toolset/%2/avformat-57.dll" ).arg(sysinfo->getAppPath(), arch)
198 << QString("%1/toolset/%2/avutil-55.dll" ).arg(sysinfo->getAppPath(), arch)
199 << QString("%1/toolset/%2/swresample-2.dll").arg(sysinfo->getAppPath(), arch);
203 static const NVEncEncoderInfo s_nvencEncoderInfo;
205 const AbstractEncoderInfo& NVEncEncoder::encoderInfo(void)
207 return s_nvencEncoderInfo;
210 const AbstractEncoderInfo &NVEncEncoder::getEncoderInfo(void) const
212 return encoderInfo();
215 // ------------------------------------------------------------
216 // Constructor & Destructor
217 // ------------------------------------------------------------
219 NVEncEncoder::NVEncEncoder(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)
221 AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
223 if(options->encType() != OptionsModel::EncType_NVEnc)
225 MUTILS_THROW("Invalid encoder type!");
229 NVEncEncoder::~NVEncEncoder(void)
231 /*Nothing to do here*/
234 QString NVEncEncoder::getName(void) const
236 return s_nvencEncoderInfo.getFullName(m_options->encArch(), m_options->encVariant());
239 // ------------------------------------------------------------
240 // Check Version
241 // ------------------------------------------------------------
243 void NVEncEncoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
245 cmdLine << "--version";
246 patterns << new QRegExp("\\bNVEncC\\s+\\(\\w+\\)\\s+(\\d)\\.(\\d+)\\s+by\\s+rigaya\\s+\\[NVENC\\s+API\\s+v(\\d+)\\.(\\d+)\\]", Qt::CaseInsensitive);
249 void NVEncEncoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
251 int offset = -1;
253 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
255 bool ok[4] = { false, false, false, false };
256 unsigned int temp[4];
257 temp[0] = patterns[0]->cap(1).toUInt(&ok[0]);
258 temp[1] = patterns[0]->cap(2).toUInt(&ok[1]);
259 temp[2] = patterns[0]->cap(2).toUInt(&ok[2]);
260 temp[3] = patterns[0]->cap(2).toUInt(&ok[3]);
261 if(ok[0] && ok[1])
263 core = (100 * temp[0]) + temp[1];
265 if (ok[2] && ok[3])
267 build = (10 * temp[2]) + temp[3];
271 if(!line.isEmpty())
273 log(line);
277 bool NVEncEncoder::checkVersion_succeeded(const int &exitCode)
279 return (exitCode == 0) || (exitCode == 1);
282 QString NVEncEncoder::printVersion(const unsigned int &revision, const bool &modified)
284 unsigned int core, build;
285 splitRevision(revision, core, build);
287 return tr("NVEncC version: %1.%2").arg(QString::number(core / 100), QString::number(core % 100).leftJustified(2, QLatin1Char('0')));
290 bool NVEncEncoder::isVersionSupported(const unsigned int &revision, const bool &modified)
292 unsigned int core, build;
293 splitRevision(revision, core, build);
295 if(core < VERSION_NVENCC_MINIMUM_VER)
297 log(tr("\nERROR: Your version of NVEncC is too old! (Minimum required version is %1.%2)").arg(QString::number(VERSION_NVENCC_MINIMUM_VER / 100), QString::number(VERSION_NVENCC_MINIMUM_VER % 100)));
298 return false;
300 else if(core > VERSION_NVENCC_MINIMUM_VER)
302 log(tr("\nWARNING: Your version of NVEncC is newer than the latest tested version, take care!"));
303 log(tr("This application works best with NVEncC version %1.%2. Newer versions may work or not.").arg(QString::number(VERSION_NVENCC_MINIMUM_VER / 100), QString::number(VERSION_NVENCC_MINIMUM_VER % 100)));
306 if (build < VERSION_NVENCC_MINIMUM_API)
308 log(tr("\nERROR: Your version of NVENC API is too old! (Minimum required version is %1.%2)").arg(QString::number(VERSION_NVENCC_MINIMUM_API / 10), QString::number(VERSION_NVENCC_MINIMUM_API % 10)));
309 return false;
312 return true;
315 // ------------------------------------------------------------
316 // Encoding Functions
317 // ------------------------------------------------------------
319 void NVEncEncoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const ClipInfo &clipInfo, const QString &indexFile, const int &pass, const QString &passLogFile)
321 switch (m_options->encVariant())
323 case 0:
324 cmdLine << "--codec" << "avc";
325 break;
326 case 1:
327 cmdLine << "--codec" << "hevc";
328 break;
329 default:
330 MUTILS_THROW("Bad encoder variant !!!");
333 switch(m_options->rcMode())
335 case 0:
336 cmdLine << "--cqp" << QString::number(qRound(m_options->quantizer()));
337 break;
338 case 1:
339 cmdLine << "--vbr" << QString::number(m_options->bitrate());
340 break;
341 case 2:
342 cmdLine << "--vbr2" << QString::number(m_options->bitrate());
343 break;
344 case 3:
345 cmdLine << "--cbr" << QString::number(m_options->bitrate());
346 break;
347 default:
348 MUTILS_THROW("Bad rate-control mode !!!");
351 const QString profile = m_options->profile().simplified().toLower();
352 if(!profile.isEmpty())
354 if(profile.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
356 cmdLine << "--profile" << profile;
360 if(!m_options->customEncParams().isEmpty())
362 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
363 if(usePipe)
365 QStringList::iterator i = customArgs.begin();
366 while(i != customArgs.end())
368 bool bModified = false;
369 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
370 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
371 if(!bModified) i++;
374 cmdLine.append(customArgs);
377 cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
379 if(usePipe)
381 cmdLine << "--y4m" << "--input" << "-";
383 else
385 cmdLine << "--input" << QDir::toNativeSeparators(m_sourceFile);
389 void NVEncEncoder::runEncodingPass_init(QList<QRegExp*> &patterns)
391 patterns << new QRegExp("^(\\d+) frames:");
392 patterns << new QRegExp("Selected\\s+codec\\s+is\\s+not\\s+supported", Qt::CaseInsensitive);
395 void NVEncEncoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const ClipInfo &clipInfo, const int &pass, double &last_progress, double &size_estimate)
397 int offset = -1;
398 if((offset = patterns[0]->lastIndexIn(line)) >= 0)
400 NVENCC_UPDATE_PROGRESS(patterns[0]);
402 else if ((offset = patterns[1]->lastIndexIn(line)) >= 0)
404 log(QString("YOUR HARDWARE DOES *NOT* SUPPORT THE '%1' CODEC !!!\n").arg(s_nvencEncoderInfo.variantToString(m_options->encVariant())));
406 else if(!line.isEmpty())
408 log(line);