OSDN Git Service

Actually make encoding with x265 work, from y4m sources as well as from STDIN sources...
[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                 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                         coreVers = temp1;
104                         revision = (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         return tr("Avs2YUV version: %1.%2.%3").arg(QString::number(revision / REV_MULT), QString::number((revision % REV_MULT) / 10),QString::number((revision % REV_MULT) % 10));
119 }
120
121 bool AvisynthSource::isVersionSupported(const unsigned int &revision, const bool &modified)
122 {
123         if((revision != UINT_MAX) && ((revision % REV_MULT) < VER_X264_AVS2YUV_VER))
124         {
125                 log(tr("\nERROR: Your version of avs2yuv is unsupported (required version: v0.24 BugMaster's mod 2)"));
126                 log(tr("You can find the required version at: http://komisar.gin.by/tools/avs2yuv/"));
127                 return false;
128         }
129         return true;
130 }
131
132 // ------------------------------------------------------------
133 // Check Source Properties
134 // ------------------------------------------------------------
135
136 void AvisynthSource::checkSourceProperties_init(QList<QRegExp*> &patterns, QStringList &cmdLine)
137 {
138         cmdLine << "-frames" << "1";
139         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true)) << "NUL";
140
141         patterns << new QRegExp(": (\\d+)x(\\d+), (\\d+) fps, (\\d+) frames");
142         patterns << new QRegExp(": (\\d+)x(\\d+), (\\d+)/(\\d+) fps, (\\d+) frames");
143 }
144
145 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)
146 {
147         qWarning("parseLine \"%1\"", line.toUtf8().constData());
148
149         int offset = -1;
150
151         if((offset = patterns[0]->lastIndexIn(line)) >= 0)
152         {
153                 bool ok1 = false, ok2 = false;
154                 bool ok3 = false, ok4 = false;
155                 unsigned int temp1 = patterns[0]->cap(1).toUInt(&ok1);
156                 unsigned int temp2 = patterns[0]->cap(2).toUInt(&ok2);
157                 unsigned int temp3 = patterns[0]->cap(3).toUInt(&ok3);
158                 unsigned int temp4 = patterns[0]->cap(4).toUInt(&ok4);
159                 if(ok1) fSizeW = temp1;
160                 if(ok2) fSizeH = temp2;
161                 if(ok3) fpsNom = temp3;
162                 if(ok4) frames = temp4;
163         }
164         else if((offset = patterns[1]->lastIndexIn(line)) >= 0)
165         {
166                 bool ok1 = false, ok2 = false;
167                 bool ok3 = false, ok4 = false, ok5 = false;
168                 unsigned int temp1 = patterns[1]->cap(1).toUInt(&ok1);
169                 unsigned int temp2 = patterns[1]->cap(2).toUInt(&ok2);
170                 unsigned int temp3 = patterns[1]->cap(3).toUInt(&ok3);
171                 unsigned int temp4 = patterns[1]->cap(4).toUInt(&ok4);
172                 unsigned int temp5 = patterns[1]->cap(5).toUInt(&ok5);
173                 if(ok1) fSizeW = temp1;
174                 if(ok2) fSizeH = temp2;
175                 if(ok3) fpsNom = temp3;
176                 if(ok4) fpsDen = temp4;
177                 if(ok5) frames = temp5;
178         }
179
180         if(!line.isEmpty())
181         {
182                 log(line);
183         }
184
185         if(line.contains("failed to load avisynth.dll", Qt::CaseInsensitive))
186         {
187                 log(tr("\nWarning: It seems that %1-Bit Avisynth is not currently installed !!!").arg(m_preferences->getUseAvisyth64Bit() ? "64" : "32"));
188         }
189         if(line.contains(QRegExp("couldn't convert input clip to (YV16|YV24)", Qt::CaseInsensitive)))
190         {
191                 log(tr("\nWarning: YV16 (4:2:2) and YV24 (4:4:4) color-spaces only supported in Avisynth 2.6 !!!"));
192         }
193 }
194
195 // ------------------------------------------------------------
196 // Source Processing
197 // ------------------------------------------------------------
198
199 void AvisynthSource::buildCommandLine(QStringList &cmdLine)
200 {
201         cmdLine << QDir::toNativeSeparators(x264_path2ansi(m_sourceFile, true));
202         cmdLine << "-";
203 }
204
205 void AvisynthSource::flushProcess(QProcess &processInput)
206 {
207         while(processInput.bytesAvailable() > 0)
208         {
209                 log(tr("av2y [info]: %1").arg(QString::fromUtf8(processInput.readLine()).simplified()));
210         }
211         
212         if(processInput.exitCode() != EXIT_SUCCESS)
213         {
214                 const int exitCode = processInput.exitCode();
215                 log(tr("\nWARNING: Input process exited with error (code: %1), your encode might be *incomplete* !!!").arg(QString::number(exitCode)));
216                 if((exitCode < 0) || (exitCode >= 32))
217                 {
218                         log(tr("\nIMPORTANT: The Avs2YUV process terminated abnormally. This means Avisynth or one of your Avisynth-Plugin's just crashed."));
219                         log(tr("IMPORTANT: Please fix your Avisynth script and try again! If you use Avisynth-MT, try using a *stable* Avisynth instead!"));
220                 }
221         }
222 }