OSDN Git Service

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