OSDN Git Service

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