OSDN Git Service

99b377e7d7965e273a57c024edcc0e614e06c85c
[x264-launcher/x264-launcher.git] / src / encoder_x264.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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 "binaries.h"
29 #include "mediainfo.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 = 2533;
41 static const unsigned int VERSION_X264_CURRENT_API = 146;
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 static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
86 {
87         const QString arch = (options->encArch() == OptionsModel::EncArch_x64) ? "x64" : "x86";
88         const QString vari = (options->encVariant() == OptionsModel::EncVariant_HiBit ) ? "10-Bit" : "8-Bit";
89         return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
90 }
91
92 // ------------------------------------------------------------
93 // Encoder Info
94 // ------------------------------------------------------------
95
96 class X264EncoderInfo : public AbstractEncoderInfo
97 {
98 public:
99         virtual QString getVariantId(const int &variant) const
100         {
101                 switch(variant)
102                 {
103                 case OptionsModel::EncVariant_LoBit:
104                         return QString::fromLatin1("8-Bit");
105                 case OptionsModel::EncVariant_HiBit:
106                         return QString::fromLatin1("10-Bit");
107                 default:
108                         return QString::fromLatin1("N/A");
109                 }
110         }
111
112         virtual QStringList getTunings(void) const
113         {
114                 QStringList tunings;
115
116                 tunings << "Film"       << "Animation"   << "Grain";
117                 tunings << "StillImage" << "PSNR"        << "SSIM";
118                 tunings << "FastDecode" << "ZeroLatency" << "Touhou";
119                 
120                 return tunings;
121         }
122
123         virtual QStringList getProfiles(const int &variant) const
124         {
125                 QStringList profiles;
126
127                 if(variant == OptionsModel::EncVariant_LoBit)
128                 {
129                         profiles << "Baseline" << "Main" << "High";
130                 }
131                 if((variant == OptionsModel::EncVariant_LoBit) || (variant == OptionsModel::EncVariant_HiBit))
132                 {
133                         profiles << "High10" << "High422" << "High444";
134                 }
135
136                 return profiles;
137         }
138
139         virtual QStringList supportedOutputFormats(void) const
140         {
141                 QStringList extLst;
142                 extLst << "264" << "mkv" << "mp4";
143                 return extLst;
144         }
145
146         virtual bool isRCModeSupported(const int rcMode) const
147         {
148                 switch(rcMode)
149                 {
150                 case OptionsModel::RCMode_CRF:
151                 case OptionsModel::RCMode_CQ:
152                 case OptionsModel::RCMode_2Pass:
153                 case OptionsModel::RCMode_ABR:
154                         return true;
155                 default:
156                         return false;
157                 }
158         }
159
160         virtual bool isInputTypeSupported(const int format) const
161         {
162                 switch(format)
163                 {
164                 case MediaInfo::FILETYPE_AVISYNTH:
165                 case MediaInfo::FILETYPE_YUV4MPEG2:
166                 case MediaInfo::FILETYPE_UNKNOWN:
167                         return true;
168                 default:
169                         return false;
170                 }
171         }
172 };
173
174 static const X264EncoderInfo s_x264EncoderInfo;
175
176 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
177 {
178         return s_x264EncoderInfo;
179 }
180
181 // ------------------------------------------------------------
182 // Constructor & Destructor
183 // ------------------------------------------------------------
184
185 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)
186 :
187         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
188         m_encoderName(MAKE_NAME("x264 (H.264/AVC)", m_options)),
189         m_binaryFile(ENC_BINARY(sysinfo, options))
190 {
191         if(options->encType() != OptionsModel::EncType_X264)
192         {
193                 MUTILS_THROW("Invalid encoder type!");
194         }
195 }
196
197 X264Encoder::~X264Encoder(void)
198 {
199         /*Nothing to do here*/
200 }
201
202 const QString &X264Encoder::getName(void)
203 {
204         return m_encoderName;
205 }
206
207 // ------------------------------------------------------------
208 // Check Version
209 // ------------------------------------------------------------
210
211 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
212 {
213         cmdLine << "--version";
214         patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)\\s+([a-f0-9]{7})", Qt::CaseInsensitive);
215         patterns << new QRegExp("\\bx264\\s+(\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
216 }
217
218 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
219 {
220         int offset = -1;
221
222         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
223         {
224                 bool ok1 = false, ok2 = false;
225                 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
226                 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
227                 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
228                 {
229                         core  = temp1;
230                         build = temp2;
231                 }
232         }
233         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
234         {
235                 bool ok1 = false, ok2 = false;
236                 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
237                 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
238                 if(ok1 && ok2 && (temp1 > 0) && (temp2 > 0))
239                 {
240                         core  = temp1;
241                         build = temp2;
242                 }
243                 modified = true;
244         }
245
246         if(!line.isEmpty())
247         {
248                 log(line);
249         }
250 }
251
252 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
253 {
254         unsigned int core, build;
255         splitRevision(revision, core, build);
256
257         QString versionStr = tr("x264 revision: %1 (core #%2)").arg(QString::number(build), QString::number(core));
258         if(modified)
259         {
260                 versionStr.append(tr(" - with custom patches!"));
261         }
262
263         return versionStr;
264 }
265
266 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
267 {
268         unsigned int core, build;
269         splitRevision(revision, core, build);
270
271         if(build < VERSION_X264_MINIMUM_REV)
272         {
273                 log(tr("\nERROR: Your revision of x264 is too old! Minimum required revision is %1.").arg(QString::number(VERSION_X264_MINIMUM_REV)));
274                 return false;
275         }
276         
277         if(core != VERSION_X264_CURRENT_API)
278         {
279                 log(tr("\nWARNING: Your x264 binary uses an untested core (API) version, take care!"));
280                 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)));
281         }
282
283         return true;
284 }
285
286 // ------------------------------------------------------------
287 // Encoding Functions
288 // ------------------------------------------------------------
289
290 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
291 {
292         double crf_int = 0.0, crf_frc = 0.0;
293
294         switch(m_options->rcMode())
295         {
296         case OptionsModel::RCMode_CQ:
297                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
298                 break;
299         case OptionsModel::RCMode_CRF:
300                 crf_frc = modf(m_options->quantizer(), &crf_int);
301                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
302                 break;
303         case OptionsModel::RCMode_2Pass:
304         case OptionsModel::RCMode_ABR:
305                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
306                 break;
307         default:
308                 MUTILS_THROW("Bad rate-control mode !!!");
309         }
310         
311         if((pass == 1) || (pass == 2))
312         {
313                 cmdLine << "--pass" << QString::number(pass);
314                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
315         }
316
317         cmdLine << "--preset" << m_options->preset().toLower();
318
319         if(!m_options->tune().simplified().isEmpty())
320         {
321                 if(m_options->tune().simplified().compare(QString::fromLatin1(OptionsModel::TUNING_UNSPECIFIED), Qt::CaseInsensitive) != 0)
322                 {
323                         cmdLine << "--tune" << m_options->tune().simplified().toLower();
324                 }
325         }
326
327         if(!m_options->profile().simplified().isEmpty())
328         {
329                 if(m_options->profile().simplified().compare(QString::fromLatin1(OptionsModel::PROFILE_UNRESTRICTED), Qt::CaseInsensitive) != 0)
330                 {
331                         cmdLine << "--profile" << m_options->profile().simplified().toLower();
332                 }
333         }
334
335         if(!m_options->customEncParams().isEmpty())
336         {
337                 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
338                 if(usePipe)
339                 {
340                         QStringList::iterator i = customArgs.begin();
341                         while(i != customArgs.end())
342                         {
343                                 bool bModified = false;
344                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
345                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
346                                 if(!bModified) i++;
347                         }
348                 }
349                 cmdLine.append(customArgs);
350         }
351
352         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
353         
354         if(usePipe)
355         {
356                 if(frames < 1) MUTILS_THROW("Frames not set!");
357                 cmdLine << "--frames" << QString::number(frames);
358                 cmdLine << "--demuxer" << "y4m";
359                 cmdLine << "--stdin" << "y4m" << "-";
360         }
361         else
362         {
363                 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
364                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
365         }
366 }
367
368 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
369 {
370         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
371         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
372         patterns << new QRegExp("^(\\d+) frames:");
373         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
374 }
375
376 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass, double &last_progress, double &size_estimate)
377 {
378         int offset = -1;
379         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
380         {
381                 X264_UPDATE_PROGRESS(patterns[0]);
382         }
383         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
384         {
385                 bool ok = false;
386                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
387                 setStatus(JobStatus_Indexing);
388                 if(ok)
389                 {
390                         setProgress(progress);
391                 }
392                 setDetails(line.mid(offset).trimmed());
393         }
394         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
395         {
396                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
397                 setDetails(line.mid(offset).trimmed());
398         }
399         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
400         {
401                 X264_UPDATE_PROGRESS(patterns[3]);
402         }
403         else if(!line.isEmpty())
404         {
405                 log(line);
406         }
407 }