OSDN Git Service

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