OSDN Git Service

474d4d754a6c366d218c880e967348f6033704de
[x264-launcher/x264-launcher.git] / src / encoder_x264.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2014 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 #include "model_options.h"
25 #include "model_status.h"
26 #include "binaries.h"
27
28 #include <QStringList>
29 #include <QDir>
30 #include <QRegExp>
31
32 //x264 version info
33 static const unsigned int X264_VERSION_X264_MINIMUM_REV = 2397;
34 static const unsigned int X264_VERSION_X264_CURRENT_API = 142;
35
36 // ------------------------------------------------------------
37 // Helper Macros
38 // ------------------------------------------------------------
39
40 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
41 { \
42         if(ITER != LIST.end()) \
43         { \
44                 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
45                 { \
46                         log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
47                         ITER = LIST.erase(ITER); \
48                         if(ITER != LIST.end()) \
49                         { \
50                                 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
51                         } \
52                         FLAG = true; \
53                 } \
54         } \
55 } \
56 while(0)
57
58 #define X264_UPDATE_PROGRESS(X) do \
59 { \
60         bool ok = false; qint64 size_estimate = 0; \
61         unsigned int progress = (X)->cap(1).toUInt(&ok); \
62         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
63         if(ok) \
64         { \
65                 setProgress(progress); \
66                 size_estimate = estimateSize(m_outputFile, progress); \
67         } \
68         setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(size_estimate))); \
69 } \
70 while(0)
71
72 static QString MAKE_NAME(const char *baseName, const OptionsModel *options)
73 {
74         const QString arch = (options->encArch() == OptionsModel::EncArch_x64) ? "x64" : "x86";
75         const QString vari = (options->encVariant() == OptionsModel::EncVariant_HiBit ) ? "10-Bit" : "8-Bit";
76         return QString("%1, %2, %3").arg(QString::fromLatin1(baseName), arch, vari);
77 }
78
79 // ------------------------------------------------------------
80 // Encoder Info
81 // ------------------------------------------------------------
82
83 class X264EncoderInfo : public AbstractEncoderInfo
84 {
85 public:
86         virtual QStringList supportedInputFormats(void) const
87         {
88                 QStringList extLst;
89                 extLst << "avi";
90                 extLst << "mp4";
91                 extLst << "mkv";
92                 extLst << "flv";
93                 extLst << "mpg";
94                 extLst << "m2v";
95                 extLst << "m2ts";
96                 extLst << "ts";
97                 extLst << "wmv";
98                 extLst << "ogm";
99                 extLst << "vob";
100                 extLst << "y4m";
101                 return extLst;
102         }
103
104         virtual QStringList supportedOutputFormats(void) const
105         {
106                 QStringList extLst;
107                 extLst << "264";
108                 extLst << "mkv";
109                 extLst << "mp4";
110                 return extLst;
111         }
112 };
113
114 static const X264EncoderInfo s_x264EncoderInfo;
115
116 const AbstractEncoderInfo &X264Encoder::getEncoderInfo(void)
117 {
118         return s_x264EncoderInfo;
119 }
120
121 // ------------------------------------------------------------
122 // Constructor & Destructor
123 // ------------------------------------------------------------
124
125 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)
126 :
127         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
128         m_encoderName(MAKE_NAME("x264 (H.264/AVC)", m_options)),
129         m_binaryFile(ENC_BINARY(sysinfo, options))
130 {
131         if(options->encType() != OptionsModel::EncType_X264)
132         {
133                 throw "Invalid encoder type!";
134         }
135 }
136
137 X264Encoder::~X264Encoder(void)
138 {
139         /*Nothing to do here*/
140 }
141
142 const QString &X264Encoder::getName(void)
143 {
144         return m_encoderName;
145 }
146
147 // ------------------------------------------------------------
148 // Check Version
149 // ------------------------------------------------------------
150
151 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
152 {
153         cmdLine << "--version";
154         patterns << new QRegExp("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
155         patterns << new QRegExp("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
156 }
157
158 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
159 {
160         int offset = -1;
161
162         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
163         {
164                 bool ok1 = false, ok2 = false;
165                 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
166                 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
167                 if(ok1 && ok2)
168                 {
169                         coreVers = temp1;
170                         revision = temp2;
171                 }
172         }
173         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
174         {
175                 bool ok1 = false, ok2 = false;
176                 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
177                 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
178                 if(ok1 && ok2)
179                 {
180                         coreVers = temp1;
181                         revision = temp2;
182                 }
183                 modified = true;
184         }
185
186         if(!line.isEmpty())
187         {
188                 log(line);
189         }
190 }
191
192 QString X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
193 {
194         return tr("x264 revision: %1 (core #%2)").arg(QString::number(revision % REV_MULT), QString::number(revision / REV_MULT)).append(modified ? tr(" - with custom patches!") : QString());
195 }
196
197 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
198 {
199         if((revision % REV_MULT) < X264_VERSION_X264_MINIMUM_REV)
200         {
201                 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %1)").arg(QString::number(X264_VERSION_X264_MINIMUM_REV)));
202                 return false;
203         }
204         
205         if((revision / REV_MULT) != X264_VERSION_X264_CURRENT_API)
206         {
207                 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
208                 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(X264_VERSION_X264_CURRENT_API)));
209         }
210
211         return true;
212 }
213
214 // ------------------------------------------------------------
215 // Encoding Functions
216 // ------------------------------------------------------------
217
218 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
219 {
220         double crf_int = 0.0, crf_frc = 0.0;
221
222         switch(m_options->rcMode())
223         {
224         case OptionsModel::RCMode_CQ:
225                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
226                 break;
227         case OptionsModel::RCMode_CRF:
228                 crf_frc = modf(m_options->quantizer(), &crf_int);
229                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
230                 break;
231         case OptionsModel::RCMode_2Pass:
232         case OptionsModel::RCMode_ABR:
233                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
234                 break;
235         default:
236                 throw "Bad rate-control mode !!!";
237                 break;
238         }
239         
240         if((pass == 1) || (pass == 2))
241         {
242                 cmdLine << "--pass" << QString::number(pass);
243                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
244         }
245
246         cmdLine << "--preset" << m_options->preset().toLower();
247
248         if(m_options->tune().compare("none", Qt::CaseInsensitive))
249         {
250                 cmdLine << "--tune" << m_options->tune().toLower();
251         }
252
253         if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
254         {
255                 if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
256                 {
257                         cmdLine << "--profile" << m_options->profile().toLower();
258                 }
259         }
260
261         if(!m_options->customEncParams().isEmpty())
262         {
263                 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
264                 if(usePipe)
265                 {
266                         QStringList::iterator i = customArgs.begin();
267                         while(i != customArgs.end())
268                         {
269                                 bool bModified = false;
270                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
271                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
272                                 if(!bModified) i++;
273                         }
274                 }
275                 cmdLine.append(customArgs);
276         }
277
278         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
279         
280         if(usePipe)
281         {
282                 if(frames < 1) throw "Frames not set!";
283                 cmdLine << "--frames" << QString::number(frames);
284                 cmdLine << "--demuxer" << "y4m";
285                 cmdLine << "--stdin" << "y4m" << "-";
286         }
287         else
288         {
289                 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
290                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
291         }
292 }
293
294 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
295 {
296         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
297         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
298         patterns << new QRegExp("^(\\d+) frames:");
299         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
300 }
301
302 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
303 {
304         int offset = -1;
305         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
306         {
307                 X264_UPDATE_PROGRESS(patterns[0]);
308         }
309         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
310         {
311                 bool ok = false;
312                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
313                 setStatus(JobStatus_Indexing);
314                 if(ok)
315                 {
316                         setProgress(progress);
317                 }
318                 setDetails(line.mid(offset).trimmed());
319         }
320         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
321         {
322                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
323                 setDetails(line.mid(offset).trimmed());
324         }
325         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
326         {
327                 X264_UPDATE_PROGRESS(patterns[3]);
328         }
329         else if(!line.isEmpty())
330         {
331                 log(line);
332         }
333 }