OSDN Git Service

The list of supported profiles will now be loaded from the EncoderInfo object, depend...
[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 "global.h"
25 #include "model_options.h"
26 #include "model_status.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 QString getVariantId(const int &variant) const
88         {
89                 switch(variant)
90                 {
91                 case OptionsModel::EncVariant_LoBit:
92                         return QString::fromLatin1("8-Bit");
93                 case OptionsModel::EncVariant_HiBit:
94                         return QString::fromLatin1("16-Bit");
95                 default:
96                         return QString::fromLatin1("N/A");
97                 }
98         }
99
100         virtual QStringList getProfiles(const int &variant) const
101         {
102                 return QStringList();
103         }
104
105         virtual QStringList supportedInputFormats(void) const
106         {
107                 QStringList extLst;
108                 extLst << "y4m";
109                 return extLst;
110         }
111
112         virtual QStringList supportedOutputFormats(void) const
113         {
114                 QStringList extLst;
115                 extLst << "hevc";
116                 return extLst;
117         }
118
119         virtual bool isRCModeSupported(const int rcMode) const
120         {
121                 switch(rcMode)
122                 {
123                 case OptionsModel::RCMode_CRF:
124                 case OptionsModel::RCMode_CQ:
125                 case OptionsModel::RCMode_ABR:
126                         return true;
127                 default:
128                         return false;
129                 }
130         }
131
132 };
133
134 static const X265EncoderInfo s_x265EncoderInfo;
135
136 const AbstractEncoderInfo &X265Encoder::getEncoderInfo(void)
137 {
138         return s_x265EncoderInfo;
139 }
140
141 // ------------------------------------------------------------
142 // Constructor & Destructor
143 // ------------------------------------------------------------
144
145 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)
146 :
147         AbstractEncoder(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile, outputFile),
148         m_encoderName(MAKE_NAME("x265 (H.265/HEVC)", m_options)),
149         m_binaryFile(ENC_BINARY(sysinfo, options))
150 {
151         if(options->encType() != OptionsModel::EncType_X265)
152         {
153                 THROW("Invalid encoder type!");
154         }
155 }
156
157 X265Encoder::~X265Encoder(void)
158 {
159         /*Nothing to do here*/
160 }
161
162 const QString &X265Encoder::getName(void)
163 {
164         return m_encoderName;
165 }
166
167 // ------------------------------------------------------------
168 // Check Version
169 // ------------------------------------------------------------
170
171 void X265Encoder::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
172 {
173         cmdLine << "--version";
174         patterns << new QRegExp("\\bHEVC\\s+encoder\\s+version\\s+0\\.(\\d+)\\+(\\d+)-[a-f0-9]+\\b", Qt::CaseInsensitive);
175 }
176
177 void X265Encoder::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
178 {
179         int offset = -1;
180
181         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
182         {
183                 bool ok1 = false, ok2 = false;
184                 unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
185                 unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
186                 if(ok1) coreVers = temp1;
187                 if(ok2) revision = temp2;
188         }
189
190         if(!line.isEmpty())
191         {
192                 log(line);
193         }
194 }
195
196 QString X265Encoder::printVersion(const unsigned int &revision, const bool &modified)
197 {
198         return tr("x265 version: 0.%1+%2").arg(QString::number(revision / REV_MULT), QString::number(revision % REV_MULT));
199 }
200
201 bool X265Encoder::isVersionSupported(const unsigned int &revision, const bool &modified)
202 {
203         const unsigned int ver = (revision / REV_MULT);
204         const unsigned int rev = (revision % REV_MULT);
205
206         if((ver < VERSION_X265_MINIMUM_VER) || ((ver == VERSION_X265_MINIMUM_VER) && (rev < VERSION_X265_MINIMUM_REV)))
207         {
208                 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)));
209                 return false;
210         }
211         else if(ver > VERSION_X265_MINIMUM_VER)
212         {
213                 log(tr("\nWARNING: Your version of x265 is newer than the latest tested version, take care!"));
214                 log(tr("This application works best with x265 version %1. Newer versions may work or not.").arg(QString::number(VERSION_X265_MINIMUM_VER)));
215         }
216         
217         return true;
218 }
219
220 // ------------------------------------------------------------
221 // Encoding Functions
222 // ------------------------------------------------------------
223
224 void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, const unsigned int &frames, const QString &indexFile, const int &pass, const QString &passLogFile)
225 {
226         double crf_int = 0.0, crf_frc = 0.0;
227
228         switch(m_options->rcMode())
229         {
230         case OptionsModel::RCMode_CQ:
231                 cmdLine << "--qp" << QString::number(qRound(m_options->quantizer()));
232                 break;
233         case OptionsModel::RCMode_CRF:
234                 crf_frc = modf(m_options->quantizer(), &crf_int);
235                 cmdLine << "--crf" << QString("%1.%2").arg(QString::number(qRound(crf_int)), QString::number(qRound(crf_frc * 10.0)));
236                 break;
237         case OptionsModel::RCMode_2Pass:
238         case OptionsModel::RCMode_ABR:
239                 cmdLine << "--bitrate" << QString::number(m_options->bitrate());
240                 break;
241         default:
242                 THROW("Bad rate-control mode !!!");
243                 break;
244         }
245         
246         if((pass == 1) || (pass == 2))
247         {
248                 cmdLine << "--pass" << QString::number(pass);
249                 cmdLine << "--stats" << QDir::toNativeSeparators(passLogFile);
250         }
251
252         cmdLine << "--preset" << m_options->preset().toLower();
253
254         if(m_options->tune().compare("none", Qt::CaseInsensitive))
255         {
256                 cmdLine << "--tune" << m_options->tune().toLower();
257         }
258
259         if(m_options->profile().compare("auto", Qt::CaseInsensitive) != 0)
260         {
261                 if((m_options->encType() == OptionsModel::EncType_X264) && (m_options->encVariant() == OptionsModel::EncVariant_LoBit))
262                 {
263                         cmdLine << "--profile" << m_options->profile().toLower();
264                 }
265         }
266
267         if(!m_options->customEncParams().isEmpty())
268         {
269                 QStringList customArgs = splitParams(m_options->customEncParams(), m_sourceFile, m_outputFile);
270                 if(usePipe)
271                 {
272                         QStringList::iterator i = customArgs.begin();
273                         while(i != customArgs.end())
274                         {
275                                 bool bModified = false;
276                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--fps");
277                                 REMOVE_CUSTOM_ARG(customArgs, i, bModified, "--frames");
278                                 if(!bModified) i++;
279                         }
280                 }
281                 cmdLine.append(customArgs);
282         }
283
284         cmdLine << "--output" << QDir::toNativeSeparators(m_outputFile);
285         
286         if(usePipe)
287         {
288                 if(frames < 1) THROW("Frames not set!");
289                 cmdLine << "--frames" << QString::number(frames);
290                 cmdLine << "--y4m" << "-";
291         }
292         else
293         {
294                 cmdLine << QDir::toNativeSeparators(m_sourceFile);
295         }
296 }
297
298 void X265Encoder::runEncodingPass_init(QList<QRegExp*> &patterns)
299 {
300         patterns << new QRegExp("\\[(\\d+)\\.(\\d+)%\\].+frames");   //regExpProgress
301         patterns << new QRegExp("indexing.+\\[(\\d+)\\.(\\d+)%\\]"); //regExpIndexing
302         patterns << new QRegExp("^(\\d+) frames:"); //regExpFrameCnt
303         patterns << new QRegExp("\\[\\s*(\\d+)\\.(\\d+)%\\]\\s+(\\d+)/(\\d+)\\s(\\d+).(\\d+)\\s(\\d+).(\\d+)\\s+(\\d+):(\\d+):(\\d+)\\s+(\\d+):(\\d+):(\\d+)"); //regExpModified
304 }
305
306 void X265Encoder::runEncodingPass_parseLine(const QString &line, QList<QRegExp*> &patterns, const int &pass)
307 {
308         int offset = -1;
309         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
310         {
311                 X264_UPDATE_PROGRESS(patterns[0]);
312         }
313         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
314         {
315                 bool ok = false;
316                 unsigned int progress = patterns[1]->cap(1).toUInt(&ok);
317                 setStatus(JobStatus_Indexing);
318                 if(ok)
319                 {
320                         setProgress(progress);
321                 }
322                 setDetails(line.mid(offset).trimmed());
323         }
324         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
325         {
326                 setStatus((pass == 2) ? JobStatus_Running_Pass2 : ((pass == 1) ? JobStatus_Running_Pass1 : JobStatus_Running));
327                 setDetails(line.mid(offset).trimmed());
328         }
329         else if((offset = patterns[3]->lastIndexIn(line)) >= 0)
330         {
331                 X264_UPDATE_PROGRESS(patterns[3]);
332         }
333         else if(!line.isEmpty())
334         {
335                 log(line);
336         }
337 }