OSDN Git Service

Bump x264 and x265 versions.
[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
31 //MUtils
32 #include <MUtils/Exception.h>
33
34 //Qt
35 #include <QStringList>
36 #include <QDir>
37 #include <QRegExp>
38
39 //x264 version info
40 static const unsigned int VERSION_X264_MINIMUM_REV = 2643;
41 static const unsigned int VERSION_X264_CURRENT_API = 148;
42
43 // ------------------------------------------------------------
44 // Helper Macros
45 // ------------------------------------------------------------
46
47 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
48 { \
49         if(ITER != LIST.end()) \
50         { \
51                 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
52                 { \
53                         log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
54                         ITER = LIST.erase(ITER); \
55                         if(ITER != LIST.end()) \
56                         { \
57                                 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
58                         } \
59                         FLAG = true; \
60                 } \
61         } \
62 } \
63 while(0)
64
65 #define X264_UPDATE_PROGRESS(X) do \
66 { \
67         bool ok[2] = { false, false }; \
68         unsigned int progressInt = (X)->cap(1).toUInt(&ok[0]); \
69         unsigned int progressFrc = (X)->cap(2).toUInt(&ok[1]); \
70         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
71         if(ok[0] && ok[1]) \
72         { \
73                 const double progress = (double(progressInt) / 100.0) + (double(progressFrc) / 1000.0); \
74                 if(!qFuzzyCompare(progress, last_progress)) \
75                 { \
76                         setProgress(floor(progress * 100.0)); \
77                         size_estimate = qFuzzyIsNull(size_estimate) ? estimateSize(m_outputFile, progress) : ((0.667 * size_estimate) + (0.333 * estimateSize(m_outputFile, progress))); \
78                         last_progress = progress; \
79                 } \
80         } \
81         setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(qRound64(size_estimate)))); \
82 } \
83 while(0)
84
85 // ------------------------------------------------------------
86 // Encoder Info
87 // ------------------------------------------------------------
88
89 class X264EncoderInfo : public AbstractEncoderInfo
90 {
91 public:
92         virtual QFlags<OptionsModel::EncVariant> getVariants(void) const
93         {
94                 QFlags<OptionsModel::EncVariant> variants;
95                 variants |= OptionsModel::EncVariant_8Bit;
96                 variants |= OptionsModel::EncVariant_10Bit;
97                 return variants;
98         }
99
100         virtual QStringList getTunings(void) const
101         {
102                 QStringList tunings;
103                 tunings << "Film"       << "Animation"   << "Grain";
104                 tunings << "StillImage" << "PSNR"        << "SSIM";
105                 tunings << "FastDecode" << "ZeroLatency" << "Touhou";
106                 
107                 return tunings;
108         }
109
110         virtual QStringList getPresets(void) const
111         {
112                 QStringList presets;
113                 presets << "ultrafast" << "superfast" << "veryfast" << "faster"   << "fast";
114                 presets << "medium"    << "slow"      << "slower"   << "veryslow" << "placebo";
115                 return presets;
116         }
117
118         virtual QStringList getProfiles(const OptionsModel::EncVariant &variant) const
119         {
120                 QStringList profiles;
121
122                 if(variant == OptionsModel::EncVariant_8Bit)
123                 {
124                         profiles << "Baseline" << "Main" << "High";
125                 }
126                 if((variant == OptionsModel::EncVariant_8Bit) || (variant == OptionsModel::EncVariant_10Bit))
127                 {
128                         profiles << "High10" << "High422" << "High444";
129                 }
130
131                 return profiles;
132         }
133
134         virtual QStringList supportedOutputFormats(void) const
135         {
136                 QStringList extLst;
137                 extLst << "264" << "mkv" << "mp4";
138                 return extLst;
139         }
140
141         virtual bool isRCModeSupported(const OptionsModel::RCMode &rcMode) const
142         {
143                 switch(rcMode)
144                 {
145                 case OptionsModel::RCMode_CRF:
146                 case OptionsModel::RCMode_CQ:
147                 case OptionsModel::RCMode_2Pass:
148                 case OptionsModel::RCMode_ABR:
149                         return true;
150                 default:
151                         return false;
152                 }
153         }
154
155         virtual bool isInputTypeSupported(const int format) const
156         {
157                 switch(format)
158                 {
159                 case MediaInfo::FILETYPE_AVISYNTH:
160                 case MediaInfo::FILETYPE_YUV4MPEG2:
161                 case MediaInfo::FILETYPE_UNKNOWN:
162                         return true;
163                 default:
164                         return false;
165                 }
166         }
167
168         virtual QString getBinaryPath(const SysinfoModel *sysinfo, const OptionsModel::EncArch &encArch, const OptionsModel::EncVariant &encVariant) const
169         {
170                 QString arch, variant;
171                 switch(encArch)
172                 {
173                         case OptionsModel::EncArch_x86_32: arch = "x86"; break;
174                         case OptionsModel::EncArch_x86_64: arch = "x64"; break;
175                         default: MUTILS_THROW("Unknown encoder arch!");
176                 }
177                 switch(encVariant)
178                 {
179                         case OptionsModel::EncVariant_8Bit:  variant = "8bit";  break;
180                         case OptionsModel::EncVariant_10Bit: variant = "10bit"; break;
181                         default: MUTILS_THROW("Unknown encoder arch!");
182                 }
183                 return QString("%1/toolset/%2/x264_%3_%2.exe").arg(sysinfo->getAppPath(), arch, variant);
184         }
185 };
186
187 static const X264EncoderInfo s_x264EncoderInfo;
188
189 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
190 {
191         return s_x264EncoderInfo;
192 }
193
194 // ------------------------------------------------------------
195 // Constructor & Destructor
196 // ------------------------------------------------------------
197
198 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)
199 :
200         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
201 {
202         if(options->encType() != OptionsModel::EncType_X264)
203         {
204                 MUTILS_THROW("Invalid encoder type!");
205         }
206 }
207
208 X264Encoder::~X264Encoder(void)
209 {
210         /*Nothing to do here*/
211 }
212
213 QString X264Encoder::getName(void) const
214 {
215         QString arch, variant;
216         switch(m_options->encArch())
217         {
218                 case OptionsModel::EncArch_x86_32: arch = "x86"; break;
219                 case OptionsModel::EncArch_x86_64: arch = "x64"; break;
220                 default: MUTILS_THROW("Unknown encoder arch!");
221         }
222         switch(m_options->encVariant())
223         {
224                 case OptionsModel::EncVariant_8Bit:  variant = "8-Bit";  break;
225                 case OptionsModel::EncVariant_10Bit: variant = "10-Bit"; break;
226                 default: MUTILS_THROW("Unknown encoder arch!");
227         }
228         return QString("x264 (H.264/AVC), %1, %2").arg(arch, variant);
229 }
230
231 // ------------------------------------------------------------
232 // Check Version
233 // ------------------------------------------------------------
234
235 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
236 {
237         cmdLine << "--version";
238         patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
239         patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
240 }
241
242 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
243 {
244         int offset = -1;
245
246         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
247         {
248                 bool ok1 = false, ok2 = false;
249                 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
250                 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
251                 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
252                 {
253                         core  = temp1;
254                         build = temp2;
255                 }
256         }
257         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
258         {
259                 bool ok1 = false, ok2 = false;
260                 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
261                 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
262                 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
263                 {
264                         core  = temp1;
265                         build = temp2;
266                 }
267                 modified = true;
268         }
269
270         if(!line.isEmpty())
271         {
272                 log(line);
273         }
274 }
275
276 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
277 {
278         unsigned int core, build;
279         splitRevision(revision, core, build);
280
281         QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
282         if(modified)
283         {
284                 versionStr.append(tr(" - with custom patches!"));
285         }
286
287         return versionStr;
288 }
289
290 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
291 {
292         unsigned int core, build;
293         splitRevision(revision, core, build);
294
295         if(build < VERSION_X264_MINIMUM_REV)
296         {
297                 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
298                 return false;
299         }
300         
301         if(core != VERSION_X264_CURRENT_API)
302         {
303                 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
304                 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)));
305         }
306
307         return true;
308 }
309
310 // ------------------------------------------------------------
311 // Encoding Functions
312 // ------------------------------------------------------------
313
314 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
315 {
316         double crf_int = 0.0, crf_frc = 0.0;
317
318         switch(m_options->rcMode())
319         {
320         case OptionsModel::RCMode_CQ:
321                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
322                 break;
323         case OptionsModel::RCMode_CRF:
324                 crf_frc = modf(m_options->quantizer(), &crf_int);
325                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
326                 break;
327         case OptionsModel::RCMode_2Pass:
328         case OptionsModel::RCMode_ABR:
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(frames < 1) MUTILS_THROW("Frames not set!");
390                 cmdLine << "--frames" << QString::number(frames);
391                 cmdLine << "--demuxer" << "y4m";
392                 cmdLine << "--stdin" << "y4m" << "-";
393         }
394         else
395         {
396                 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
397                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
398         }
399 }
400
401 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
402 {
403         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
404         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
405         patterns << new QRegExp("^(\\d+) frames:");
406         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
407 }
408
409 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass, double &last_progress, double &size_estimate)
410 {
411         int offset = -1;
412         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
413         {
414                 X264_UPDATE_PROGRESS(patterns[0]);
415         }
416         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
417         {
418                 bool ok = false;
419                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
420                 setStatus(JobStatus_Indexing);
421                 if(ok)
422                 {
423                         setProgress(progress);
424                 }
425                 setDetails(line.mid(offset).trimmed());
426         }
427         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
428         {
429                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
430                 setDetails(line.mid(offset).trimmed());
431         }
432         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
433         {
434                 X264_UPDATE_PROGRESS(patterns[3]);
435         }
436         else if(!line.isEmpty())
437         {
438                 log(line);
439         }
440 }