OSDN Git Service

Refactored runEncodingPass() into AbstractEncoder class and refactored encoder-specif...
[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
27 #include <QStringList>
28 #include <QDir>
29 #include <QRegExp>
30
31 //x264 version info
32 static const unsigned int X264_VERSION_X264_MINIMUM_REV = 2380;
33 static const unsigned int X264_VERSION_X264_CURRENT_API = 142;
34
35 #define REMOVE_CUSTOM_ARG(LIST, ITER, FLAG, PARAM) do \
36 { \
37         if(ITER != LIST.end()) \
38         { \
39                 if((*ITER).compare(PARAM, Qt::CaseInsensitive) == 0) \
40                 { \
41                         log(tr("WARNING: Custom parameter \"" PARAM "\" will be ignored in Pipe'd mode!\n")); \
42                         ITER = LIST.erase(ITER); \
43                         if(ITER != LIST.end()) \
44                         { \
45                                 if(!((*ITER).startsWith("--", Qt::CaseInsensitive))) ITER = LIST.erase(ITER); \
46                         } \
47                         FLAG = true; \
48                 } \
49         } \
50 } \
51 while(0)
52
53 #define X264_UPDATE_PROGRESS(X) do \
54 { \
55         bool ok = false; qint64 size_estimate = 0; \
56         unsigned int progress = (X)->cap(1).toUInt(&ok); \
57         setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running)); \
58         if(ok) \
59         { \
60                 setProgress(progress); \
61                 size_estimate = estimateSize(m_outputFile, progress); \
62         } \
63         setDetails(tr("%1, est. file size %2").arg(line.mid(offset).trimmed(), sizeToString(size_estimate))); \
64 } \
65 while(0)
66
67 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)
68 :
69         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile)
70 {
71         if(options->encType() != OptionsModel::EncType_X264)
72         {
73                 throw "Invalid encoder type!";
74         }
75
76 }
77
78 X264Encoder::~X264Encoder(void)
79 {
80         /*Nothing to do here*/
81 }
82
83 void X264Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
84 {
85         cmdLine << "--version";
86         patterns << new QRegExp("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})", Qt::CaseInsensitive);
87         patterns << new QRegExp("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
88 }
89
90 void X264Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
91 {
92         int offset = -1;
93         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
94         {
95                 bool ok1 = false, ok2 = false;
96                 unsigned int temp1 = patterns[0]->cap(2).toUInt(&ok1);
97                 unsigned int temp2 = patterns[0]->cap(3).toUInt(&ok2);
98                 if(ok1) coreVers = temp1;
99                 if(ok2) revision = temp2;
100         }
101         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
102         {
103                 bool ok1 = false, ok2 = false;
104                 unsigned int temp1 = patterns[1]->cap(2).toUInt(&ok1);
105                 unsigned int temp2 = patterns[1]->cap(3).toUInt(&ok2);
106                 if(ok1) coreVers = temp1;
107                 if(ok2) revision = temp2;
108                 modified = true;
109         }
110 }
111
112 void X264Encoder::printVersion(const unsigned int &revision, const bool &modified)
113 {
114         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()));
115 }
116
117 bool X264Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
118 {
119         if((revision % REV_MULT) < X264_VERSION_X264_MINIMUM_REV)
120         {
121                 log(tr("\nERROR: Your revision of x264 is too old! (Minimum required revision is %1)").arg(QString::number(X264_VERSION_X264_MINIMUM_REV)));
122                 return false;
123         }
124         
125         if((revision / REV_MULT) != X264_VERSION_X264_CURRENT_API)
126         {
127                 log(tr("\nWARNING: Your revision of x264 uses an unsupported core (API) version, take care!"));
128                 log(tr("This application works best with x264 core (API) version %2.").arg(QString::number(X264_VERSION_X264_CURRENT_API)));
129         }
130
131         return true;
132 }
133
134 void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
135 {
136         double crf_int = 0.0, crf_frc = 0.0;
137
138         switch(m_options->rcMode())
139         {
140         case OptionsModel::RCMode_CQ:
141                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
142                 break;
143         case OptionsModel::RCMode_CRF:
144                 crf_frc = modf(m_options->quantizer(), &crf_int);
145                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
146                 break;
147         case OptionsModel::RCMode_2Pass:
148         case OptionsModel::RCMode_ABR:
149                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
150                 break;
151         default:
152                 throw "Bad rate-control mode !!!";
153                 break;
154         }
155         
156         if((pass == 1) || (pass == 2))
157         {
158                 cmdLine << "--pass" << QString::number(pass);
159                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
160         }
161
162         cmdLine << "--preset" << m_options->preset().toLower();
163
164         if(m_options->tune().compare("none", Qt::CaseInsensitive))
165         {
166                 cmdLine << "--tune" << m_options->tune().toLower();
167         }
168
169         if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
170         {
171                 if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
172                 {
173                         cmdLine << "--profile" << m_options->profile().toLower();
174                 }
175         }
176
177         if(!m_options->customEncParams().isEmpty())
178         {
179                 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
180                 if(usePipe)
181                 {
182                         QStringList::iterator i = customArgs.begin();
183                         while(i != customArgs.end())
184                         {
185                                 bool bModified = false;
186                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
187                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
188                                 if(!bModified) i++;
189                         }
190                 }
191                 cmdLine.append(customArgs);
192         }
193
194         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
195         
196         if(usePipe)
197         {
198                 if(frames < 1) throw "Frames not set!";
199                 cmdLine << "--frames" << QString::number(frames);
200                 cmdLine << "--demuxer" << "y4m";
201                 cmdLine << "--stdin" << "y4m" << "-";
202         }
203         else
204         {
205                 cmdLine << "--index" << QDir::toNativeSeparators(indexFile);
206                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
207         }
208 }
209
210 void X264Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
211 {
212         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");   //regExpProgress
213         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
214         patterns << new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
215         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
216 }
217
218 void X264Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
219 {
220         int offset = -1;
221         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
222         {
223                 X264_UPDATE_PROGRESS(patterns[0]);
224         }
225         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
226         {
227                 bool ok = false;
228                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
229                 setStatus(JobStatus_Indexing);
230                 if(ok)
231                 {
232                         setProgress(progress);
233                 }
234                 setDetails(line.mid(offset).trimmed());
235         }
236         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
237         {
238                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
239                 setDetails(line.mid(offset).trimmed());
240         }
241         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
242         {
243                 X264_UPDATE_PROGRESS(patterns[3]);
244         }
245         else if(!line.isEmpty())
246         {
247                 log(line);
248         }
249 }