Bump version + updated changelog.
[simple-x264-launcher.git] / src / encoder_abstract.cpp
blob31c1f7df862fb48df2bb9214df43ad4b9a58d82b
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2018 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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "encoder_abstract.h"
24 //Internal
25 #include "global.h"
26 #include "model_options.h"
27 #include "model_preferences.h"
28 #include "model_sysinfo.h"
29 #include "model_status.h"
30 #include "source_abstract.h"
32 //MUtils
33 #include <MUtils/Global.h>
34 #include <MUtils/Exception.h>
35 #include <MUtils/OSSupport.h>
37 //Qt
38 #include <QProcess>
39 #include <QDir>
40 #include <QTextCodec>
41 #include <QSemaphore>
42 #include <QDate>
43 #include <QTime>
44 #include <QThread>
45 #include <QLocale>
47 // ------------------------------------------------------------
48 // Constructor & Destructor
49 // ------------------------------------------------------------
51 AbstractEncoder::AbstractEncoder(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, const QString &outputFile)
53 AbstractTool(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause),
54 m_sourceFile(sourceFile),
55 m_outputFile(outputFile),
56 m_indexFile(QString("%1/~%2.ffindex").arg(QDir::tempPath(), stringToHash(m_sourceFile)))
58 /*Nothing to do here*/
61 AbstractEncoder::~AbstractEncoder(void)
63 /*Nothing to do here*/
66 // ------------------------------------------------------------
67 // Encoding Functions
68 // ------------------------------------------------------------
70 bool AbstractEncoder::runEncodingPass(AbstractSource* pipedSource, const QString outputFile, const ClipInfo &clipInfo, const int &pass, const QString &passLogFile)
72 QProcess processEncode, processInput;
74 if(pipedSource)
76 pipedSource->createProcess(processEncode, processInput);
79 QStringList cmdLine_Encode;
80 buildCommandLine(cmdLine_Encode, (pipedSource != NULL), clipInfo, m_indexFile, pass, passLogFile);
82 log("Creating encoder process:");
83 if(!startProcess(processEncode, getBinaryPath(), cmdLine_Encode, true, &getExtraPaths()))
85 return false;
88 QList<QRegExp*> patterns;
89 runEncodingPass_init(patterns);
91 double last_progress = 0.0;
92 double size_estimate = 0.0;
94 bool bTimeout = false;
95 bool bAborted = false;
97 //Main processing loop
98 while(processEncode.state() != QProcess::NotRunning)
100 unsigned int waitCounter = 0;
102 //Wait until new output is available
103 forever
105 if(*m_abort)
107 processEncode.kill();
108 processInput.kill();
109 bAborted = true;
110 break;
112 if((*m_pause) && (processEncode.state() == QProcess::Running))
114 JobStatus previousStatus = m_jobStatus;
115 setStatus(JobStatus_Paused);
116 log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
117 bool ok[2] = {false, false};
118 QProcess *proc[2] = { &processEncode, &processInput };
119 ok[0] = MUtils::OS::suspend_process(proc[0], true);
120 ok[1] = MUtils::OS::suspend_process(proc[1], true);
121 while(*m_pause) m_semaphorePause->tryAcquire(1, 5000);
122 while(m_semaphorePause->tryAcquire(1, 0));
123 ok[0] = MUtils::OS::suspend_process(proc[0], false);
124 ok[1] = MUtils::OS::suspend_process(proc[1], false);
125 if(!(*m_abort)) setStatus(previousStatus);
126 log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
127 waitCounter = 0;
128 continue;
130 if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
132 if(processEncode.state() == QProcess::Running)
134 if(++waitCounter > m_processTimeoutMaxCounter)
136 if(m_preferences->getAbortOnTimeout())
138 processEncode.kill();
139 qWarning("encoder process timed out <-- killing!");
140 log("\nPROCESS TIMEOUT !!!");
141 bTimeout = true;
142 break;
145 else if(waitCounter == m_processTimeoutWarning)
147 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
148 log(tr("Warning: encoder did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
150 continue;
153 if((*m_abort) || ((*m_pause) && (processEncode.state() == QProcess::Running)))
155 continue;
157 break;
160 //Exit main processing loop now?
161 if(bAborted || bTimeout)
163 break;
166 //Process all output
167 PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, clipInfo, pass, last_progress, size_estimate);
170 if(!(bTimeout || bAborted))
172 PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, clipInfo, pass, last_progress, size_estimate);
175 processEncode.waitForFinished(5000);
176 if(processEncode.state() != QProcess::NotRunning)
178 qWarning("Encoder process still running, going to kill it!");
179 processEncode.kill();
180 processEncode.waitForFinished(-1);
183 if(pipedSource)
185 processInput.waitForFinished(5000);
186 if(processInput.state() != QProcess::NotRunning)
188 qWarning("Input process still running, going to kill it!");
189 processInput.kill();
190 processInput.waitForFinished(-1);
192 if(!(bTimeout || bAborted))
194 pipedSource->flushProcess(processInput);
198 while(!patterns.isEmpty())
200 QRegExp *pattern = patterns.takeFirst();
201 MUTILS_DELETE(pattern);
204 if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
206 if(!(bTimeout || bAborted))
208 const int exitCode = processEncode.exitCode();
209 if((exitCode < -1) || (exitCode >= 32))
211 log(tr("\nFATAL ERROR: The encoder process has *crashed* -> your encode probably is *incomplete* !!!"));
212 log(tr("Note that this indicates a bug in the current encoder, *not* in Simple x264/x265 Launcher."));
214 log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
216 processEncode.close();
217 processInput.close();
218 return false;
221 QThread::yieldCurrentThread();
223 QFileInfo completedFileInfo(m_outputFile);
224 const qint64 finalSize = (completedFileInfo.exists() && completedFileInfo.isFile()) ? completedFileInfo.size() : 0;
225 log(tr("Final file size is %1 bytes.").arg(sizeToString(finalSize)));
227 switch(pass)
229 case 1:
230 setStatus(JobStatus_Running_Pass1);
231 setDetails(tr("First pass completed. Preparing for second pass..."));
232 break;
233 case 2:
234 setStatus(JobStatus_Running_Pass2);
235 setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
236 break;
237 default:
238 setStatus(JobStatus_Running);
239 setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
240 break;
243 setProgress(100);
244 processEncode.close();
245 processInput.close();
246 return true;
249 // ------------------------------------------------------------
250 // Utilities
251 // ------------------------------------------------------------
253 double AbstractEncoder::estimateSize(const QString &fileName, const double &progress)
255 double estimatedSize = 0.0;
256 if(progress >= 0.03)
258 QFileInfo fileInfo(fileName);
259 if(fileInfo.exists() && fileInfo.isFile())
261 const qint64 currentSize = QFileInfo(fileName).size();
262 estimatedSize = static_cast<double>(currentSize) * (1.0 / qBound(0.0, progress, 1.0));
265 return estimatedSize;
268 QString AbstractEncoder::sizeToString(qint64 size)
270 static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
272 if(size > 1024I64)
274 qint64 estimatedSize = size;
275 qint64 remainderSize = 0I64;
277 int prefixIdx = 0;
278 while((estimatedSize > 1024I64) && (prefixIdx < 4))
280 remainderSize = estimatedSize % 1024I64;
281 estimatedSize = estimatedSize / 1024I64;
282 prefixIdx++;
285 double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
286 return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
289 return tr("N/A");
292 // ------------------------------------------------------------
293 // Encoder Info
294 // ------------------------------------------------------------
296 template <class T>
297 static T getElementAt(const QList<T> &list, const quint32 &index)
299 if (index >= quint32(list.count()))
301 MUTILS_THROW("Index is out of bounds!");
303 return list[index];
306 QStringList AbstractEncoderInfo::getDependencies(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
308 return QStringList();
311 QString AbstractEncoderInfo::getFullName(const quint32 &encArch, const quint32 &encVariant) const
313 return QString("%1, %2, %3").arg(getName(), archToString(encArch), variantToString(encVariant));
316 QString AbstractEncoderInfo::archToString(const quint32 &index) const
318 return getElementAt(getArchitectures(), index).first;
321 AbstractEncoderInfo::ArchBit AbstractEncoderInfo::archToType(const quint32 &index) const
323 return getElementAt(getArchitectures(), index).second;
326 QString AbstractEncoderInfo::variantToString(const quint32 &index) const
328 return getElementAt(getVariants(), index);
331 QString AbstractEncoderInfo::rcModeToString(const quint32 &index) const
333 return getElementAt(getRCModes(), index).first;
336 AbstractEncoderInfo::RCType AbstractEncoderInfo::rcModeToType(const quint32 &index) const
338 return getElementAt(getRCModes(), index).second;