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_nvenc.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_NVENCC_MINIMUM_VER
= 206;
41 static const unsigned int VERSION_NVENCC_MINIMUM_API
= 60;
43 // ------------------------------------------------------------
45 // ------------------------------------------------------------
47 #define NVENCC_UPDATE_PROGRESS(X) do \
50 unsigned int progressFrames = (X)->cap(1).toUInt(&ok); \
51 setStatus(JobStatus_Running); \
52 if(ok && (totalFrames > 0) && (totalFrames != UINT_MAX)) \
54 const double progress = (double(progressFrames) / double(totalFrames)); \
55 if(!qFuzzyCompare(progress, last_progress)) \
57 setProgress(floor(progress * 100.0)); \
58 size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
59 last_progress = progress; \
62 setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
66 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
68 if(ITER != LIST.end()) \
70 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
72 log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
73 ITER = LIST.erase(ITER); \
74 if(ITER != LIST.end()) \
76 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
84 // ------------------------------------------------------------
86 // ------------------------------------------------------------
88 class NVEncEncoderInfo
: public AbstractEncoderInfo
91 virtual QFlags
<OptionsModel::EncVariant
> getVariants(void) const
93 QFlags
<OptionsModel::EncVariant
> variants
;
94 variants
|= OptionsModel::EncVariant_8Bit
;
98 virtual QStringList
getTunings(void) const
100 return QStringList();
103 virtual QStringList
getPresets(void) const
105 return QStringList();
108 virtual QStringList
getProfiles(const OptionsModel::EncVariant
&variant
) const
110 QStringList profiles
;
113 case OptionsModel::EncVariant_8Bit
:
114 profiles
<< "baseline" << "main" << "high";
120 virtual QStringList
supportedOutputFormats(void) const
127 virtual bool isRCModeSupported(const OptionsModel::RCMode
&rcMode
) const
131 case OptionsModel::RCMode_CQ
:
132 case OptionsModel::RCMode_ABR
:
139 virtual bool isInputTypeSupported(const int format
) const
143 case MediaInfo::FILETYPE_YUV4MPEG2
:
150 virtual QString
getBinaryPath(const SysinfoModel
*sysinfo
, const OptionsModel::EncArch
&encArch
, const OptionsModel::EncVariant
&encVariant
) const
152 QString arch
, variant
;
155 case OptionsModel::EncArch_x86_32
: arch
= "x86"; break;
156 case OptionsModel::EncArch_x86_64
: arch
= "x64"; break;
157 default: MUTILS_THROW("Unknown encoder arch!");
161 case OptionsModel::EncVariant_8Bit
: variant
= "8bit"; break;
162 default: MUTILS_THROW("Unknown encoder arch!");
164 return QString("%1/toolset/%2/nvencc_%2.exe").arg(sysinfo
->getAppPath(), arch
);
167 virtual QStringList
getDependencies(const SysinfoModel
*sysinfo
, const OptionsModel::EncArch
&encArch
, const OptionsModel::EncVariant
&encVariant
) const
169 QString arch
, variant
;
172 case OptionsModel::EncArch_x86_32
: arch
= "x86"; break;
173 case OptionsModel::EncArch_x86_64
: arch
= "x64"; break;
174 default: MUTILS_THROW("Unknown encoder arch!");
178 case OptionsModel::EncVariant_8Bit
: variant
= "8bit"; break;
179 default: MUTILS_THROW("Unknown encoder arch!");
181 QStringList dependencies
;
182 dependencies
<< QString("%1/toolset/%2/avcodec-57.dll" ).arg(sysinfo
->getAppPath(), arch
);
183 dependencies
<< QString("%1/toolset/%2/avfilter-6.dll" ).arg(sysinfo
->getAppPath(), arch
);
184 dependencies
<< QString("%1/toolset/%2/avformat-57.dll" ).arg(sysinfo
->getAppPath(), arch
);
185 dependencies
<< QString("%1/toolset/%2/avutil-55.dll" ).arg(sysinfo
->getAppPath(), arch
);
186 dependencies
<< QString("%1/toolset/%2/swresample-2.dll").arg(sysinfo
->getAppPath(), arch
);
191 static const NVEncEncoderInfo s_x265EncoderInfo
;
193 const AbstractEncoderInfo
&NVEncEncoder::getEncoderInfo(void)
195 return s_x265EncoderInfo
;
198 // ------------------------------------------------------------
199 // Constructor & Destructor
200 // ------------------------------------------------------------
202 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
)
204 AbstractEncoder(jobObject
, options
, sysinfo
, preferences
, jobStatus
, abort
, pause
, semaphorePause
, sourceFile
, outputFile
)
206 if(options
->encType() != OptionsModel::EncType_NVEnc
)
208 MUTILS_THROW("Invalid encoder type!");
212 NVEncEncoder::~NVEncEncoder(void)
214 /*Nothing to do here*/
217 QString
NVEncEncoder::getName(void) const
219 QString arch
, variant
;
220 switch(m_options
->encArch())
222 case OptionsModel::EncArch_x86_32
: arch
= "x86"; break;
223 case OptionsModel::EncArch_x86_64
: arch
= "x64"; break;
224 default: MUTILS_THROW("Unknown encoder arch!");
226 switch(m_options
->encVariant())
228 case OptionsModel::EncVariant_8Bit
: variant
= "8-Bit"; break;
229 default: MUTILS_THROW("Unknown encoder arch!");
231 return QString("NVEncC, %1, %2").arg(arch
, variant
);
234 // ------------------------------------------------------------
236 // ------------------------------------------------------------
238 void NVEncEncoder::checkVersion_init(QList
<QRegExp
*> &patterns
, QStringList
&cmdLine
)
240 cmdLine
<< "--version";
241 patterns
<< new QRegExp("\\bNVEncC\\s+\\(\\w+\\)\\s+(\\d)\\.(\\d+)\\s+by\\s+rigaya\\s+\\[NVENC\\s+API\\s+v(\\d+)\\.(\\d+)\\]", Qt::CaseInsensitive
);
244 void NVEncEncoder::checkVersion_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, unsigned int &core
, unsigned int &build
, bool &modified
)
248 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
250 bool ok
[4] = { false, false, false, false };
251 unsigned int temp
[4];
252 temp
[0] = patterns
[0]->cap(1).toUInt(&ok
[0]);
253 temp
[1] = patterns
[0]->cap(2).toUInt(&ok
[1]);
254 temp
[2] = patterns
[0]->cap(2).toUInt(&ok
[2]);
255 temp
[3] = patterns
[0]->cap(2).toUInt(&ok
[3]);
258 core
= (100 * temp
[0]) + temp
[1];
262 build
= (10 * temp
[2]) + temp
[3];
272 bool NVEncEncoder::checkVersion_succeeded(const int &exitCode
)
274 return (exitCode
== 0) || (exitCode
== 1);
277 QString
NVEncEncoder::printVersion(const unsigned int &revision
, const bool &modified
)
279 unsigned int core
, build
;
280 splitRevision(revision
, core
, build
);
282 return tr("NVEncC version: %1.%2").arg(QString::number(core
/ 100), QString::number(core
% 100).leftJustified(2, QLatin1Char('0')));
285 bool NVEncEncoder::isVersionSupported(const unsigned int &revision
, const bool &modified
)
287 unsigned int core
, build
;
288 splitRevision(revision
, core
, build
);
290 if(core
< VERSION_NVENCC_MINIMUM_VER
)
292 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)));
295 else if(core
> VERSION_NVENCC_MINIMUM_VER
)
297 log(tr("\nWARNING: Your version of NVEncC is newer than the latest tested version, take care!"));
298 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)));
301 if (build
< VERSION_NVENCC_MINIMUM_API
)
303 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)));
310 // ------------------------------------------------------------
311 // Encoding Functions
312 // ------------------------------------------------------------
314 void NVEncEncoder::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_ABR
:
321 cmdLine
<< "--vbr" << QString::number(m_options
->bitrate());
323 case OptionsModel::RCMode_CQ
:
324 cmdLine
<< "--cqp" << QString::number(qRound(m_options
->quantizer()));
327 MUTILS_THROW("Bad rate-control mode !!!");
331 const QString profile
= m_options
->profile().simplified().toLower();
332 if(!profile
.isEmpty())
334 if(profile
.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED
), Qt::CaseInsensitive
) != 0)
336 cmdLine
<< "--profile" << profile
;
340 if(!m_options
->customEncParams().isEmpty())
342 QStringList customArgs
= splitParams(m_options
->customEncParams(), m_sourceFile
, m_outputFile
);
345 QStringList::iterator i
= customArgs
.begin();
346 while(i
!= customArgs
.end())
348 bool bModified
= false;
349 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--fps");
350 REMOVE_CUSTOM_ARG(customArgs
, i
, bModified
, "--frames");
354 cmdLine
.append(customArgs
);
357 cmdLine
<< "--output" << QDir::toNativeSeparators(m_outputFile
);
361 cmdLine
<< "--y4m" << "--input" << "-";
365 cmdLine
<< "--input" << QDir::toNativeSeparators(m_sourceFile
);
369 void NVEncEncoder::runEncodingPass_init(QList
<QRegExp
*> &patterns
)
371 patterns
<< new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
374 void NVEncEncoder::runEncodingPass_parseLine(const QString
&line
, QList
<QRegExp
*> &patterns
, const unsigned int &totalFrames
, const int &pass
, double &last_progress
, double &size_estimate
)
377 if((offset
= patterns
[0]->lastIndexIn(line
)) >= 0)
379 NVENCC_UPDATE_PROGRESS(patterns
[0]);
381 else if(!line
.isEmpty())