OSDN Git Service

Refactored code to better manage the encoder binary paths: They are now handled by...
[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 #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 = 243;
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 QString AvisynthSource::getName(void) const
55 {
56         return m_sourceName;
57 }
58
59 // ------------------------------------------------------------
60 // Check Version
61 // ------------------------------------------------------------
62
63 bool AvisynthSource::isSourceAvailable()
64 {
65         if(!(m_sysinfo->hasAvisynth()))
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 &core, unsigned int &build, 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                         core  = temp1;
91                         build = temp2;
92                 }
93                 log(line);
94         }
95         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
96         {
97                 bool ok1 = false, ok2 = false, ok3 = false;
98                 unsigned int temp1 = patterns[1]->cap(1).toUInt(&ok1);
99                 unsigned int temp2 = patterns[1]->cap(2).toUInt(&ok2);
100                 unsigned int temp3 = patterns[1]->cap(3).toUInt(&ok3);
101                 if(ok1 && ok2 && ok3)
102                 {
103                         core  = temp1;
104                         build = (temp2 * 10) + (temp3 % 10);
105                 }
106                 modified = true;
107                 log(line);
108         }
109 }
110
111 bool AvisynthSource::checkVersion_succeeded(const int &exitCode)
112 {
113         return (exitCode == EXIT_SUCCESS) || (exitCode == 2);
114 }
115
116 QString AvisynthSource::printVersion(const unsigned int &revision, const bool &modified)
117 {
118         unsigned int core, build;
119         splitRevision(revision, core, build);
120
121         return tr("Avs2YUV version: %1.%2.%3").arg(QString::number(core), QString::number(build / 10),QString::number(build % 10));
122 }
123
124 bool AvisynthSource::isVersionSupported(const unsigned int &revision, const bool &modified)
125 {
126         unsigned int core, build;
127         splitRevision(revision, core, build);
128
129         if((revision != UINT_MAX) && (build < VER_X264_AVS2YUV_VER))
130         {
131                 log(tr("\nERROR: Your version of avs2yuv is unsupported (required version: v0.24 BugMaster's mod 2)"));
132                 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
133                 return false;
134         }
135         return true;
136 }
137
138 // ------------------------------------------------------------
139 // Check Source Properties
140 // ------------------------------------------------------------
141
142 void AvisynthSource::checkSourceProperties_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
143 {
144         cmdLine << "-frames" << "1";
145         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true)) << "NUL";
146
147         patterns << new QRegExp(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
148         patterns << new QRegExp(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
149 }
150
151 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)
152 {
153         int offset = -1;
154
155         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
156         {
157                 bool ok1 = false, ok2 = false;
158                 bool ok3 = false, ok4 = false;
159                 unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
160                 unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
161                 unsigned int temp3 = patterns[0]->cap(3).toUInt(&ok3);
162                 unsigned int temp4 = patterns[0]->cap(4).toUInt(&ok4);
163                 if(ok1) fSizeW = temp1;
164                 if(ok2) fSizeH = temp2;
165                 if(ok3) fpsNom = temp3;
166                 if(ok4) frames = temp4;
167         }
168         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
169         {
170                 bool ok1 = false, ok2 = false;
171                 bool ok3 = false, ok4 = false, ok5 = false;
172                 unsigned int temp1 = patterns[1]->cap(1).toUInt(&ok1);
173                 unsigned int temp2 = patterns[1]->cap(2).toUInt(&ok2);
174                 unsigned int temp3 = patterns[1]->cap(3).toUInt(&ok3);
175                 unsigned int temp4 = patterns[1]->cap(4).toUInt(&ok4);
176                 unsigned int temp5 = patterns[1]->cap(5).toUInt(&ok5);
177                 if(ok1) fSizeW = temp1;
178                 if(ok2) fSizeH = temp2;
179                 if(ok3) fpsNom = temp3;
180                 if(ok4) fpsDen = temp4;
181                 if(ok5) frames = temp5;
182         }
183
184         if(!line.isEmpty())
185         {
186                 log(line);
187         }
188
189         if(line.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
190         {
191                 log(tr("\nWarning: It seems that Avisynth is not currently installed/available !!!"));
192         }
193         if(line.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
194         {
195                 log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
196         }
197 }
198
199 // ------------------------------------------------------------
200 // Source Processing
201 // ------------------------------------------------------------
202
203 void AvisynthSource::buildCommandLine(QStringList &cmdLine)
204 {
205         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true));
206         cmdLine << "-";
207 }
208
209 void AvisynthSource::flushProcess(QProcess &processInput)
210 {
211         while(processInput.bytesAvailable() > 0)
212         {
213                 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
214         }
215         
216         if(processInput.exitCode() != EXIT_SUCCESS)
217         {
218                 const int exitCode = processInput.exitCode();
219                 log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
220                 if((exitCode < 0) || (exitCode >= 32))
221                 {
222                         log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
223                         log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
224                 }
225         }
226 }