OSDN Git Service

Some refactoring to allow supporting multiple encoders in the encode thread (far...
[x264-launcher/x264-launcher.git] / src / encoder_abstract.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 #include "encoder_abstract.h"
23
24 #include "global.h"
25 #include "model_options.h"
26 #include "model_preferences.h"
27 #include "model_sysinfo.h"
28 #include "binaries.h"
29
30 #include <QProcess>
31
32 unsigned int AbstractEncoder::checkVersion(bool &modified)
33 {
34         if(m_preferences->getSkipVersionTest())
35         {
36                 log("Warning: Skipping encoder version check this time!");
37                 return (999 * REV_MULT) + (REV_MULT-1);
38         }
39
40         QProcess process;
41         QStringList cmdLine = QStringList() << "--version";
42
43         log("Creating process:");
44         if(!startProcess(process, ENC_BINARY(m_sysinfo, m_options), cmdLine))
45         {
46                 return false;;
47         }
48
49         QRegExp regExpVersion("", Qt::CaseInsensitive);
50         QRegExp regExpVersionMod("\\bx264 (\\d)\\.(\\d+)\\.(\\d+)", Qt::CaseInsensitive);
51         
52         switch(m_options->encType())
53         {
54                 case OptionsModel::EncType_X264: regExpVersion.setPattern("\\bx264\\s(\\d)\\.(\\d+)\\.(\\d+)\\s([a-f0-9]{7})");
55                 case OptionsModel::EncType_X265: regExpVersion.setPattern("\\bHEVC\\s+encoder\\s+version\\s+0\\.(\\d+)\\+(\\d+)-[a-f0-9]+\\b");
56                 default: throw "Invalid encoder type!";
57         }
58
59         bool bTimeout = false;
60         bool bAborted = false;
61
62         unsigned int revision = UINT_MAX;
63         unsigned int coreVers = UINT_MAX;
64         modified = false;
65
66         while(process.state() != QProcess::NotRunning)
67         {
68                 if(m_abort)
69                 {
70                         process.kill();
71                         bAborted = true;
72                         break;
73                 }
74                 if(!process.waitForReadyRead())
75                 {
76                         if(process.state() == QProcess::Running)
77                         {
78                                 process.kill();
79                                 qWarning("encoder process timed out <-- killing!");
80                                 log("\nPROCESS TIMEOUT !!!");
81                                 bTimeout = true;
82                                 break;
83                         }
84                 }
85                 while(process.bytesAvailable() > 0)
86                 {
87                         QList<QByteArray> lines = process.readLine().split('\r');
88                         while(!lines.isEmpty())
89                         {
90                                 QString text = QString::fromUtf8(lines.takeFirst().constData()).simplified();
91                                 int offset = -1;
92                                 if((offset = regExpVersion.lastIndexIn(text)) >= 0)
93                                 {
94                                         bool ok1 = false, ok2 = false;
95                                         unsigned int temp1 = regExpVersion.cap(2).toUInt(&ok1);
96                                         unsigned int temp2 = regExpVersion.cap(3).toUInt(&ok2);
97                                         if(ok1) coreVers = temp1;
98                                         if(ok2) revision = temp2;
99                                 }
100                                 else if((offset = regExpVersionMod.lastIndexIn(text)) >= 0)
101                                 {
102                                         bool ok1 = false, ok2 = false;
103                                         unsigned int temp1 = regExpVersionMod.cap(2).toUInt(&ok1);
104                                         unsigned int temp2 = regExpVersionMod.cap(3).toUInt(&ok2);
105                                         if(ok1) coreVers = temp1;
106                                         if(ok2) revision = temp2;
107                                         modified = true;
108                                 }
109                                 if(!text.isEmpty())
110                                 {
111                                         log(text);
112                                 }
113                         }
114                 }
115         }
116
117         process.waitForFinished();
118         if(process.state() != QProcess::NotRunning)
119         {
120                 process.kill();
121                 process.waitForFinished(-1);
122         }
123
124         if(bTimeout || bAborted || process.exitCode() != EXIT_SUCCESS)
125         {
126                 if(!(bTimeout || bAborted))
127                 {
128                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(process.exitCode())));
129                 }
130                 return UINT_MAX;
131         }
132
133         if((revision == UINT_MAX) || (coreVers == UINT_MAX))
134         {
135                 log(tr("\nFAILED TO DETERMINE ENCODER VERSION !!!"));
136                 return UINT_MAX;
137         }
138         
139         return (coreVers * REV_MULT) + (revision % REV_MULT);
140 }