OSDN Git Service

Fixed VapourSynth input + some more code re-factoring.
[x264-launcher/x264-launcher.git] / src / source_vapoursynth.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_vapoursynth.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_VSPIPE_VER = 22;
35
36 VapoursynthSource::VapoursynthSource(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)
37 :
38         AbstractSource(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause, sourceFile),
39         m_sourceName("VapourSynth (vpy)"),
40         m_binaryFile(VPS_BINARY(m_sysinfo, m_preferences))
41 {
42         /*Nothing to do here*/
43 }
44
45 VapoursynthSource::~VapoursynthSource(void)
46 {
47         /*Nothing to do here*/
48 }
49
50 const QString &VapoursynthSource::getName(void)
51 {
52         return m_sourceName;
53 }
54
55 // ------------------------------------------------------------
56 // Check Version
57 // ------------------------------------------------------------
58
59 bool VapoursynthSource::isSourceAvailable()
60 {
61         if(!(m_sysinfo->hasVPSSupport() && (!m_sysinfo->getVPSPath().isEmpty()) && QFileInfo(VPS_BINARY(m_sysinfo, m_preferences)).isFile()))
62         {
63                 log(tr("\nVPY INPUT REQUIRES VAPOURSYNTH, BUT IT IS *NOT* AVAILABLE !!!"));
64                 return false;
65         }
66         return true;
67 }
68
69 void VapoursynthSource::checkVersion_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
70 {
71         cmdLine << "-version";
72         patterns << new QRegExp("\\bVapourSynth\\b", Qt::CaseInsensitive);
73         patterns << new QRegExp("\\bCore\\s+r(\\d+)\\b", Qt::CaseInsensitive);
74         patterns << new QRegExp("\\bAPI\\s+r(\\d+)\\b", Qt::CaseInsensitive);
75 }
76
77 void VapoursynthSource::checkVersion_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &coreVers, unsigned int &revision, bool &modified)
78 {
79         int offset = -1;
80
81         if((offset = patterns[1]->lastIndexIn(line)) >= 0)
82         {
83                 bool ok = false;
84                 unsigned int temp = patterns[1]->cap(1).toUInt(&ok);
85                 if(ok) revision = temp;
86         }
87         else if((offset = patterns[2]->lastIndexIn(line)) >= 0)
88         {
89                 bool ok = false;
90                 unsigned int temp = patterns[2]->cap(1).toUInt(&ok);
91                 if(ok) coreVers = temp;
92         }
93
94         if(!line.isEmpty())
95         {
96                 log(line);
97         }
98 }
99
100 void VapoursynthSource::printVersion(const unsigned int &revision, const bool &modified)
101 {
102         log(tr("\nVapourSynth version: r%1 (API r%2)").arg(QString::number(revision % REV_MULT), QString::number(revision / REV_MULT)));
103 }
104
105 bool VapoursynthSource::isVersionSupported(const unsigned int &revision, const bool &modified)
106 {
107         if((revision % REV_MULT) < VER_X264_VSPIPE_VER)
108         {
109                 log(tr("\nERROR: Your version of VapourSynth is unsupported (requires version r%1 or newer").arg(QString::number(VER_X264_VSPIPE_VER)));
110                 log(tr("You can find the latest VapourSynth version at: http://www.vapoursynth.com/"));
111                 return false;
112         }
113         return true;
114 }
115
116 // ------------------------------------------------------------
117 // Check Source Properties
118 // ------------------------------------------------------------
119
120 void VapoursynthSource::checkSourceProperties_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
121 {
122         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true));
123         cmdLine << "-" << "-info";
124
125         patterns << new QRegExp("\\bFrames:\\s+(\\d+)\\b");
126         patterns << new QRegExp("\\bWidth:\\s+(\\d+)\\b");
127         patterns << new QRegExp("\\bHeight:\\s+(\\d+)\\b");
128         patterns << new QRegExp("\\bFPS:\\s+(\\d+)\\b");
129         patterns << new QRegExp("\\bFPS:\\s+(\\d+)/(\\d+)\\b");
130 }
131
132 void VapoursynthSource::checkSourceProperties_parseLine(const QString &line, QList<QRegExp*> &patterns, unsigned int &frames, unsigned int &fSizeW, unsigned int &fSizeH, unsigned int &fpsNom, unsigned int &fpsDen)
133 {
134         int offset = -1;
135
136         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
137         {
138                 bool ok = false;
139                 unsigned int temp = patterns[0]->cap(1).toUInt(&ok);
140                 if(ok) frames = temp;
141         }
142         if((offset = patterns[1]->lastIndexIn(line)) >= 0)
143         {
144                 bool ok = false;
145                 unsigned int temp =patterns[1]->cap(1).toUInt(&ok);
146                 if(ok) fSizeW = temp;
147         }
148         if((offset = patterns[2]->lastIndexIn(line)) >= 0)
149         {
150                 bool ok = false;
151                 unsigned int temp = patterns[2]->cap(1).toUInt(&ok);
152                 if(ok) fSizeH = temp;
153         }
154         if((offset = patterns[3]->lastIndexIn(line)) >= 0)
155         {
156                 bool ok = false;
157                 unsigned int temp = patterns[3]->cap(1).toUInt(&ok);
158                 if(ok) fpsNom = temp;
159         }
160         if((offset = patterns[4]->lastIndexIn(line)) >= 0)
161         {
162                 bool ok1 = false, ok2 = false;
163                 unsigned int temp1 = patterns[4]->cap(1).toUInt(&ok1);
164                 unsigned int temp2 = patterns[4]->cap(2).toUInt(&ok2);
165                 if(ok1 && ok2)
166                 {
167                         fpsNom = temp1;
168                         fpsDen = temp2;
169                 }
170         }
171
172         if(!line.isEmpty())
173         {
174                 log(line);
175         }
176 }
177
178 // ------------------------------------------------------------
179 // Check Source Properties
180 // ------------------------------------------------------------
181
182 void VapoursynthSource::buildCommandLine(QStringList &cmdLine)
183 {
184         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true));
185         cmdLine << "-" << "-y4m";
186 }
187
188 void VapoursynthSource::flushProcess(QProcess &processInput)
189 {
190         while(processInput.bytesAvailable() > 0)
191         {
192                 log(tr("vpyp [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
193         }
194         
195         if(processInput.exitCode() != EXIT_SUCCESS)
196         {
197                 const int exitCode = processInput.exitCode();
198                 log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
199                 if((exitCode < 0) || (exitCode >= 32))
200                 {
201                         log(tr("\nIMPORTANT: The Vapoursynth process terminated abnormally. This means Vapoursynth or one of your Vapoursynth-Plugin's just crashed."));
202                 }
203         }
204 }