OSDN Git Service

Fixed the use of the "m_abort" and "m_pause" flags. Also various fixes to encoder...
[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 = 2380;
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 // ------------------------------------------------------------
73 // Constructor & Destructor
74 // ------------------------------------------------------------
75
76 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)
77 :
78         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
79         m_binaryFile(ENC_BINARY(sysinfo, options))
80 {
81         if(options->encType() != OptionsModel::EncType_X264)
82         {
83                 throw "Invalid encoder type!";
84         }
85 }
86
87 X264Encoder::~X264Encoder(void)
88 {
89         /*Nothing to do here*/
90 }
91
92 // ------------------------------------------------------------
93 // Check Version
94 // ------------------------------------------------------------
95
96 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
97 {
98         cmdLine << "--version";
99         patterns << new QRegExp("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
100         patterns << new QRegExp("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
101 }
102
103 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
104 {
105         int offset = -1;
106         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
107         {
108                 bool ok1 = false, ok2 = false;
109                 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
110                 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
111                 if(ok1) coreVers = temp1;
112                 if(ok2) revision = temp2;
113         }
114         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
115         {
116                 bool ok1 = false, ok2 = false;
117                 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
118                 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
119                 if(ok1) coreVers = temp1;
120                 if(ok2) revision = temp2;
121                 modified = true;
122         }
123 }
124
125 void X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
126 {
127         log(tr("\nx264 revision: %1 (core #%2)").arg(QString::number(revision % REV_MULT), QString::number(revision / REV_MULT)).append(modified ? tr(" - with custom patches!") : QString()));
128 }
129
130 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
131 {
132         if((revision % REV_MULT) < X264_VERSION_X264_MINIMUM_REV)
133         {
134                 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %1)").arg(QString::number(X264_VERSION_X264_MINIMUM_REV)));
135                 return false;
136         }
137         
138         if((revision / REV_MULT) != X264_VERSION_X264_CURRENT_API)
139         {
140                 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
141                 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(X264_VERSION_X264_CURRENT_API)));
142         }
143
144         return true;
145 }
146
147 // ------------------------------------------------------------
148 // Encoding Functions
149 // ------------------------------------------------------------
150
151 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
152 {
153         double crf_int = 0.0, crf_frc = 0.0;
154
155         switch(m_options->rcMode())
156         {
157         case OptionsModel::RCMode_CQ:
158                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
159                 break;
160         case OptionsModel::RCMode_CRF:
161                 crf_frc = modf(m_options->quantizer(), &crf_int);
162                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
163                 break;
164         case OptionsModel::RCMode_2Pass:
165         case OptionsModel::RCMode_ABR:
166                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
167                 break;
168         default:
169                 throw "Bad rate-control mode !!!";
170                 break;
171         }
172         
173         if((pass == 1) || (pass == 2))
174         {
175                 cmdLine << "--pass" << QString::number(pass);
176                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
177         }
178
179         cmdLine << "--preset" << m_options->preset().toLower();
180
181         if(m_options->tune().compare("none", Qt::CaseInsensitive))
182         {
183                 cmdLine << "--tune" << m_options->tune().toLower();
184         }
185
186         if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
187         {
188                 if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
189                 {
190                         cmdLine << "--profile" << m_options->profile().toLower();
191                 }
192         }
193
194         if(!m_options->customEncParams().isEmpty())
195         {
196                 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
197                 if(usePipe)
198                 {
199                         QStringList::iterator i = customArgs.begin();
200                         while(i != customArgs.end())
201                         {
202                                 bool bModified = false;
203                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
204                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
205                                 if(!bModified) i++;
206                         }
207                 }
208                 cmdLine.append(customArgs);
209         }
210
211         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
212         
213         if(usePipe)
214         {
215                 if(frames < 1) throw "Frames not set!";
216                 cmdLine << "--frames" << QString::number(frames);
217                 cmdLine << "--demuxer" << "y4m";
218                 cmdLine << "--stdin" << "y4m" << "-";
219         }
220         else
221         {
222                 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
223                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
224         }
225 }
226
227 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
228 {
229         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");
230         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]");
231         patterns << new QRegExp("^(\\d+) frames:");
232         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
233 }
234
235 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
236 {
237         log(tr("PARSE: \"%1\"").arg(line));
238
239         int offset = -1;
240         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
241         {
242                 X264_UPDATE_PROGRESS(patterns[0]);
243         }
244         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
245         {
246                 bool ok = false;
247                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
248                 setStatus(JobStatus_Indexing);
249                 if(ok)
250                 {
251                         setProgress(progress);
252                 }
253                 setDetails(line.mid(offset).trimmed());
254         }
255         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
256         {
257                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
258                 setDetails(line.mid(offset).trimmed());
259         }
260         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
261         {
262                 X264_UPDATE_PROGRESS(patterns[3]);
263         }
264         else if(!line.isEmpty())
265         {
266                 log(line);
267         }
268 }