OSDN Git Service

Make "custom" Avs2YUV parameters work again.
[x264-launcher/x264-launcher.git] / src / source_avisynth.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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 #pragma once
23
24 #include "source_avisynth.h"
25
26 #include "global.h"
27
28 #include <QDir>
29 #include <QProcess>
30
31 static const unsigned int VER_X264_AVS2YUV_VER = 243;
32
33 // ------------------------------------------------------------
34 // Encoder Info
35 // ------------------------------------------------------------
36
37 class AvisynthSourceInfo : public AbstractSourceInfo
38 {
39 public:
40         virtual QString getBinaryPath(const SysinfoModel *sysinfo, const bool& x64) const
41         {
42                 return QString("%1/toolset/%2/avs2yuv_%2.exe").arg(sysinfo->getAppPath(), (x64 ? "x64": "x86"));
43         }
44 };
45
46 static const AvisynthSourceInfo s_avisynthEncoderInfo;
47
48 const AbstractSourceInfo &AvisynthSource::getSourceInfo(void)
49 {
50         return s_avisynthEncoderInfo;
51 }
52
53 // ------------------------------------------------------------
54 // Constructor & Destructor
55 // ------------------------------------------------------------
56
57 AvisynthSource::AvisynthSource(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)
58 :
59         AbstractSource(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile)
60 {
61         /*Nothing to do here*/
62 }
63
64 AvisynthSource::~AvisynthSource(void)
65 {
66         /*Nothing to do here*/
67 }
68
69 QString AvisynthSource::getName(void) const
70 {
71         return tr("Avisynth (avs)");
72 }
73
74 // ------------------------------------------------------------
75 // Check Version
76 // ------------------------------------------------------------
77
78 bool AvisynthSource::isSourceAvailable()
79 {
80         if(!(m_sysinfo->hasAvisynth()))
81         {
82                 log(tr("\nAVS INPUT REQUIRES AVISYNTH, BUT IT IS *NOT* AVAILABLE !!!"));
83                 return false;
84         }
85         return true;
86 }
87
88 void AvisynthSource::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
89 {
90         patterns << new QRegExp("\\bAvs2YUV (\\d+).(\\d+)\\b", Qt::CaseInsensitive);
91         patterns << new QRegExp("\\bAvs2YUV (\\d+).(\\d+)bm(\\d)\\b", Qt::CaseInsensitive);
92 }
93
94 void AvisynthSource::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &core, unsigned int &build, bool &modified)
95 {
96         int offset = -1;
97
98         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
99         {
100                 bool ok1 = false, ok2 = false;
101                 unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
102                 unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
103                 if(ok1 && ok2)
104                 {
105                         core  = temp1;
106                         build = temp2;
107                 }
108                 log(line);
109         }
110         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
111         {
112                 bool ok1 = false, ok2 = false, ok3 = false;
113                 unsigned int temp1 = patterns[1]->cap(1).toUInt(&ok1);
114                 unsigned int temp2 = patterns[1]->cap(2).toUInt(&ok2);
115                 unsigned int temp3 = patterns[1]->cap(3).toUInt(&ok3);
116                 if(ok1 && ok2 && ok3)
117                 {
118                         core  = temp1;
119                         build = (temp2 * 10) + (temp3 % 10);
120                 }
121                 modified = true;
122                 log(line);
123         }
124 }
125
126 bool AvisynthSource::checkVersion_succeeded(const int &exitCode)
127 {
128         return (exitCode == EXIT_SUCCESS) || (exitCode == 2);
129 }
130
131 QString AvisynthSource::printVersion(const unsigned int &revision, const bool &modified)
132 {
133         unsigned int core, build;
134         splitRevision(revision, core, build);
135
136         return tr("Avs2YUV version: %1.%2.%3").arg(QString::number(core), QString::number(build / 10),QString::number(build % 10));
137 }
138
139 bool AvisynthSource::isVersionSupported(const unsigned int &revision, const bool &modified)
140 {
141         unsigned int core, build;
142         splitRevision(revision, core, build);
143
144         if((revision != UINT_MAX) && (build < VER_X264_AVS2YUV_VER))
145         {
146                 log(tr("\nERROR: Your version of avs2yuv is unsupported (required version: v0.24 BugMaster's mod 2)"));
147                 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
148                 return false;
149         }
150         return true;
151 }
152
153 // ------------------------------------------------------------
154 // Check Source Properties
155 // ------------------------------------------------------------
156
157 void AvisynthSource::checkSourceProperties_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
158 {
159         if(!m_options->customAvs2YUV().isEmpty())
160         {
161                 cmdLine << splitParams(m_options->customAvs2YUV());
162         }
163
164         cmdLine << "-frames" << "1";
165         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true)) << "NUL";
166
167         patterns << new QRegExp(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
168         patterns << new QRegExp(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
169 }
170
171 void AvisynthSource::checkSourceProperties_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &frames, unsigned int &fSizeW, unsigned int &fSizeH, unsigned int &fpsNom, unsigned int &fpsDen)
172 {
173         int offset = -1;
174
175         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
176         {
177                 bool ok1 = false, ok2 = false;
178                 bool ok3 = false, ok4 = false;
179                 unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
180                 unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
181                 unsigned int temp3 = patterns[0]->cap(3).toUInt(&ok3);
182                 unsigned int temp4 = patterns[0]->cap(4).toUInt(&ok4);
183                 if(ok1) fSizeW = temp1;
184                 if(ok2) fSizeH = temp2;
185                 if(ok3) fpsNom = temp3;
186                 if(ok4) frames = temp4;
187         }
188         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
189         {
190                 bool ok1 = false, ok2 = false;
191                 bool ok3 = false, ok4 = false, ok5 = false;
192                 unsigned int temp1 = patterns[1]->cap(1).toUInt(&ok1);
193                 unsigned int temp2 = patterns[1]->cap(2).toUInt(&ok2);
194                 unsigned int temp3 = patterns[1]->cap(3).toUInt(&ok3);
195                 unsigned int temp4 = patterns[1]->cap(4).toUInt(&ok4);
196                 unsigned int temp5 = patterns[1]->cap(5).toUInt(&ok5);
197                 if(ok1) fSizeW = temp1;
198                 if(ok2) fSizeH = temp2;
199                 if(ok3) fpsNom = temp3;
200                 if(ok4) fpsDen = temp4;
201                 if(ok5) frames = temp5;
202         }
203
204         if(!line.isEmpty())
205         {
206                 log(line);
207         }
208
209         if(line.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
210         {
211                 log(tr("\nWarning: It seems that Avisynth is not currently installed/available !!!"));
212         }
213         if(line.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
214         {
215                 log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
216         }
217 }
218
219 // ------------------------------------------------------------
220 // Source Processing
221 // ------------------------------------------------------------
222
223 void AvisynthSource::buildCommandLine(QStringList &cmdLine)
224 {
225         if(!m_options->customAvs2YUV().isEmpty())
226         {
227                 cmdLine << splitParams(m_options->customAvs2YUV());
228         }
229
230         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true));
231         cmdLine << "-";
232 }
233
234 void AvisynthSource::flushProcess(QProcess &processInput)
235 {
236         while(processInput.bytesAvailable() > 0)
237         {
238                 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
239         }
240         
241         if(processInput.exitCode() != EXIT_SUCCESS)
242         {
243                 const int exitCode = processInput.exitCode();
244                 log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
245                 if((exitCode < 0) || (exitCode >= 32))
246                 {
247                         log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
248                         log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
249                 }
250         }
251 }