OSDN Git Service

Bump x264 minimum required version to API-#163 (r3049).
[x264-launcher/x264-launcher.git] / src / encoder_x264.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2020 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "encoder_x264.h"
23
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"
31
32 //MUtils
33 #include <MUtils/Global.h>
34 #include <MUtils/Exception.h>
35
36 //Qt
37 #include <QStringList>
38 #include <QDir>
39 #include <QRegExp>
40 #include <QPair>
41
42 //x264 version info
43 static const unsigned int VERSION_X264_MINIMUM_REV = 3049;
44 static const unsigned int VERSION_X264_CURRENT_API =  163;
45
46 // ------------------------------------------------------------
47 // Helper Macros
48 // ------------------------------------------------------------
49
50 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
51 { \
52         if(ITER != LIST.end()) \
53         { \
54                 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
55                 { \
56                         log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
57                         ITER = LIST.erase(ITER); \
58                         if(ITER != LIST.end()) \
59                         { \
60                                 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
61                         } \
62                         FLAG = true; \
63                 } \
64         } \
65 } \
66 while(0)
67
68 #define X264_UPDATE_PROGRESS(X) do \
69 { \
70         bool ok[2] = { false, false }; \
71         unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
72         unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
73         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
74         if(ok[0] && ok[1]) \
75         { \
76                 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
77                 if(!qFuzzyCompare(progress, last_progress)) \
78                 { \
79                         setProgress(floor(progress * 100.0)); \
80                         size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
81                         last_progress = progress; \
82                 } \
83         } \
84         setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
85 } \
86 while(0)
87
88 // ------------------------------------------------------------
89 // Encoder Info
90 // ------------------------------------------------------------
91
92 class X264EncoderInfo : public AbstractEncoderInfo
93 {
94 public:
95         virtual QString getName(void) const
96         {
97                 return "x264 (AVC/H.264)";
98         }
99
100         virtual QList<ArchId> getArchitectures(void) const
101         {
102                 return QList<ArchId>()
103                 << qMakePair(QString("32-Bit (x86)"), ARCH_TYPE_X86)
104                 << qMakePair(QString("64-Bit (x64)"), ARCH_TYPE_X64);
105         }
106
107         virtual QStringList getVariants(void) const
108         {
109                 return QStringList() << "8-Bit" << "10-Bit";
110         }
111
112         virtual QList<RCMode> getRCModes(void) const
113         {
114                 return QList<RCMode>()
115                 << qMakePair(QString("CRF"),    RC_TYPE_QUANTIZER)
116                 << qMakePair(QString("CQ"),     RC_TYPE_QUANTIZER)
117                 << qMakePair(QString("2-Pass"), RC_TYPE_MULTIPASS)
118                 << qMakePair(QString("ABR"),    RC_TYPE_RATE_KBPS);
119         }
120
121         virtual QStringList getTunings(void) const
122         {
123                 return QStringList()
124                 << "Film"       << "Animation"   << "Grain"
125                 << "StillImage" << "PSNR"        << "SSIM"
126                 << "FastDecode" << "ZeroLatency" << "Touhou";
127         }
128
129         virtual QStringList getPresets(void) const
130         {
131                 return QStringList()
132                 << "ultrafast" << "superfast" << "veryfast" << "faster"   << "fast"
133                 << "medium"    << "slow"      << "slower"   << "veryslow" << "placebo";
134         }
135
136         virtual QStringList getProfiles(const quint32 &variant) const
137         {
138                 QStringList profiles;
139                 switch(variant)
140                 {
141                         case 0: profiles << "Baseline" << "Main"    << "High";    break;
142                         case 1: profiles << "High10"   << "High422" << "High444"; break;
143                         default: MUTILS_THROW("Unknown encoder variant!");
144                 }
145                 return profiles;
146         }
147
148         virtual QStringList supportedOutputFormats(void) const
149         {
150                 return QStringList() << "264" << "mkv" << "mp4";
151         }
152
153         virtual bool isInputTypeSupported(const int format) const
154         {
155                 switch(format)
156                 {
157                 case MediaInfo::FILETYPE_AVISYNTH:
158                 case MediaInfo::FILETYPE_YUV4MPEG2:
159                 case MediaInfo::FILETYPE_UNKNOWN:
160                         return true;
161                 default:
162                         return false;
163                 }
164         }
165
166         virtual QString getBinaryPath(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
167         {
168                 QString arch, variant;
169                 switch(encArch)
170                 {
171                         case 0: arch = "x86"; break;
172                         case 1: arch = "x64"; break;
173                         default: MUTILS_THROW("Unknown encoder arch!");
174                 }
175                 if ((encVariant < 0) || (encVariant > 1))
176                 {
177                         MUTILS_THROW("Unknown encoder variant!");
178                 }
179                 return QString("%1/toolset/%2/x264_%2.exe").arg(sysinfo->getAppPath(), arch);
180         }
181
182         virtual QString getHelpCommand(void) const
183         {
184                 return "--fullhelp";
185         }
186 };
187
188 static const X264EncoderInfo s_x264EncoderInfo;
189
190 const AbstractEncoderInfo& X264Encoder::encoderInfo(void)
191 {
192         return s_x264EncoderInfo;
193 }
194
195 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void) const
196 {
197         return encoderInfo();
198 }
199
200 // ------------------------------------------------------------
201 // Constructor & Destructor
202 // ------------------------------------------------------------
203
204 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)
205 :
206         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
207 {
208         if(options->encType() != OptionsModel::EncType_X264)
209         {
210                 MUTILS_THROW("Invalid encoder type!");
211         }
212 }
213
214 X264Encoder::~X264Encoder(void)
215 {
216         /*Nothing to do here*/
217 }
218
219 QString X264Encoder::getName(void) const
220 {
221         return s_x264EncoderInfo.getFullName(m_options->encArch(), m_options->encVariant());
222 }
223
224 // ------------------------------------------------------------
225 // Check Version
226 // ------------------------------------------------------------
227
228 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
229 {
230         cmdLine << "--version";
231         patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
232         patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
233 }
234
235 void X264Encoder::checkVersion_parseLine(const QString &line, const QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
236 {
237         if(patterns[0]->lastIndexIn(line) >= 0)
238         {
239                 unsigned int temp[3];
240                 if(MUtils::regexp_parse_uint32(*patterns[0], temp, 3))
241                 {
242                         core  = temp[1];
243                         build = temp[2];
244                 }
245         }
246         else if(patterns[1]->lastIndexIn(line) >= 0)
247         {
248                 unsigned int temp[3];
249                 if (MUtils::regexp_parse_uint32(*patterns[1], temp, 3))
250                 {
251                         core  = temp[1];
252                         build = temp[2];
253                 }
254                 modified = true;
255         }
256
257         if(!line.isEmpty())
258         {
259                 log(line);
260         }
261 }
262
263 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
264 {
265         unsigned int core, build;
266         splitRevision(revision, core, build);
267
268         QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
269         if(modified)
270         {
271                 versionStr.append(tr(" - with custom patches!"));
272         }
273
274         return versionStr;
275 }
276
277 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
278 {
279         unsigned int core, build;
280         splitRevision(revision, core, build);
281
282         if(build < VERSION_X264_MINIMUM_REV)
283         {
284                 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
285                 return false;
286         }
287         
288         if(core != VERSION_X264_CURRENT_API)
289         {
290                 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
291                 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)));
292         }
293
294         return true;
295 }
296
297 // ------------------------------------------------------------
298 // Encoding Functions
299 // ------------------------------------------------------------
300
301 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const ClipInfo &clipInfo, const QString &indexFile, const int &pass, const QString &passLogFile)
302 {
303         double crf_int = 0.0, crf_frc = 0.0;
304
305         cmdLine << "--output-depth";
306         switch (m_options->encVariant())
307         {
308         case 0:
309                 cmdLine << QString::number(8);
310                 break;
311         case 1:
312                 cmdLine << QString::number(10);
313                 break;
314         default:
315                 MUTILS_THROW("Unknown encoder variant!");
316         }
317
318         switch(m_options->rcMode())
319         {
320         case 0:
321                 crf_frc = modf(m_options->quantizer(), &crf_int);
322                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
323                 break;
324         case 1:
325                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
326                 break;
327         case 2:
328         case 3:
329                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
330                 break;
331         default:
332                 MUTILS_THROW("Bad rate-control mode !!!");
333         }
334         
335         if((pass == 1) || (pass == 2))
336         {
337                 cmdLine << "--pass" << QString::number(pass);
338                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
339         }
340
341         const QString preset = m_options->preset().simplified().toLower();
342         if(!preset.isEmpty())
343         {
344                 if(preset.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
345                 {
346                         cmdLine << "--preset" << preset;
347                 }
348         }
349
350         const QString tune = m_options->tune().simplified().toLower();
351         if(!tune.isEmpty())
352         {
353                 if(tune.compare(QString::fromLatin1(OptionsModel::SETTING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
354                 {
355                         cmdLine << "--tune" << tune;
356                 }
357         }
358
359         const QString profile = m_options->profile().simplified().toLower();
360         if(!profile.isEmpty())
361         {
362                 if(profile.compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
363                 {
364                         cmdLine << "--profile" << profile;
365                 }
366         }
367
368         if(!m_options->customEncParams().isEmpty())
369         {
370                 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
371                 if(usePipe)
372                 {
373                         QStringList::iterator i = customArgs.begin();
374                         while(i != customArgs.end())
375                         {
376                                 bool bModified = false;
377                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
378                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
379                                 if(!bModified) i++;
380                         }
381                 }
382                 cmdLine.append(customArgs);
383         }
384
385         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
386         
387         if(usePipe)
388         {
389                 if (clipInfo.getFrameCount() < 1)
390                 {
391                         MUTILS_THROW("Frames not set!");
392                 }
393                 cmdLine << "--frames" << QString::number(clipInfo.getFrameCount());
394                 cmdLine << "--demuxer" << "y4m";
395                 cmdLine << "--stdin" << "y4m" << "-";
396         }
397         else
398         {
399                 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
400                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
401         }
402 }
403
404 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
405 {
406         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
407         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
408         patterns << new QRegExp("^(\\d+) frames:");
409         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
410 }
411
412 void X264Encoder::runEncodingPass_parseLine(const QString &line, const QList<QRegExp*> &patterns, const ClipInfo &clipInfo, const int &pass, double &last_progress, double &size_estimate)
413 {
414         int offset = -1;
415         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
416         {
417                 X264_UPDATE_PROGRESS(patterns[0]);
418         }
419         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
420         {
421                 bool ok = false;
422                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
423                 setStatus(JobStatus_Indexing);
424                 if(ok)
425                 {
426                         setProgress(progress);
427                 }
428                 setDetails(line.mid(offset).trimmed());
429         }
430         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
431         {
432                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
433                 setDetails(line.mid(offset).trimmed());
434         }
435         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
436         {
437                 X264_UPDATE_PROGRESS(patterns[3]);
438         }
439         else if(!line.isEmpty())
440         {
441                 log(line);
442         }
443 }