OSDN Git Service

Updated copyright year.
[x264-launcher/x264-launcher.git] / src / encoder_abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2022 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 //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"
31
32 //MUtils
33 #include <MUtils/Global.h>
34 #include <MUtils/Exception.h>
35 #include <MUtils/OSSupport.h>
36
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>
46
47 // ------------------------------------------------------------
48 // Constructor & Destructor
49 // ------------------------------------------------------------
50
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)
52 :
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)))
57 {
58         /*Nothing to do here*/
59 }
60
61 AbstractEncoder::~AbstractEncoder(void)
62 {
63         /*Nothing to do here*/
64 }
65
66 // ------------------------------------------------------------
67 // Encoding Functions
68 // ------------------------------------------------------------
69
70 bool AbstractEncoder::runEncodingPass(AbstractSource* pipedSource, const QString outputFile, const ClipInfo &clipInfo, const int &pass, const QString &passLogFile)
71 {
72         QProcess processEncode, processInput;
73         
74         if(pipedSource)
75         {
76                 pipedSource->createProcess(processEncode, processInput);
77         }
78
79         QStringList cmdLine_Encode;
80         buildCommandLine(cmdLine_Encode, (pipedSource != NULL), clipInfo, m_indexFile, pass, passLogFile);
81
82         log("Creating encoder process:");
83         if(!startProcess(processEncode, getBinaryPath(), cmdLine_Encode, true, &getExtraPaths(), &getExtraEnv()))
84         {
85                 return false;
86         }
87
88         QList<QRegExp*> patterns;
89         runEncodingPass_init(patterns);
90         
91         double last_progress = 0.0;
92         double size_estimate = 0.0;
93         
94         bool bTimeout = false;
95         bool bAborted = false;
96
97         //Main processing loop
98         while(processEncode.state() != QProcess::NotRunning)
99         {
100                 unsigned int waitCounter = 0;
101
102                 //Wait until new output is available
103                 forever
104                 {
105                         if(*m_abort)
106                         {
107                                 processEncode.kill();
108                                 processInput.kill();
109                                 bAborted = true;
110                                 break;
111                         }
112                         if((*m_pause) && (processEncode.state() == QProcess::Running))
113                         {
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;
129                         }
130                         if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
131                         {
132                                 if(processEncode.state() == QProcess::Running)
133                                 {
134                                         if(++waitCounter > m_processTimeoutMaxCounter)
135                                         {
136                                                 if(m_preferences->getAbortOnTimeout())
137                                                 {
138                                                         processEncode.kill();
139                                                         qWarning("encoder process timed out <-- killing!");
140                                                         log("\nPROCESS TIMEOUT !!!");
141                                                         bTimeout = true;
142                                                         break;
143                                                 }
144                                         }
145                                         else if(waitCounter == m_processTimeoutWarning)
146                                         {
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)));
149                                         }
150                                         continue;
151                                 }
152                         }
153                         if((*m_abort) || ((*m_pause) && (processEncode.state() == QProcess::Running)))
154                         {
155                                 continue;
156                         }
157                         break;
158                 }
159                 
160                 //Exit main processing loop now?
161                 if(bAborted || bTimeout)
162                 {
163                         break;
164                 }
165
166                 //Process all output
167                 PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, clipInfo, pass, last_progress, size_estimate);
168         }
169         
170         if(!(bTimeout || bAborted))
171         {
172                 PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, clipInfo, pass, last_progress, size_estimate);
173         }
174
175         processEncode.waitForFinished(5000);
176         if(processEncode.state() != QProcess::NotRunning)
177         {
178                 qWarning("Encoder process still running, going to kill it!");
179                 processEncode.kill();
180                 processEncode.waitForFinished(-1);
181         }
182         
183         if(pipedSource)
184         {
185                 processInput.waitForFinished(5000);
186                 if(processInput.state() != QProcess::NotRunning)
187                 {
188                         qWarning("Input process still running, going to kill it!");
189                         processInput.kill();
190                         processInput.waitForFinished(-1);
191                 }
192                 if(!(bTimeout || bAborted))
193                 {
194                         pipedSource->flushProcess(processInput);
195                 }
196         }
197
198         while(!patterns.isEmpty())
199         {
200                 QRegExp *pattern = patterns.takeFirst();
201                 MUTILS_DELETE(pattern);
202         }
203
204         if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
205         {
206                 if(!(bTimeout || bAborted))
207                 {
208                         const int exitCode = processEncode.exitCode();
209                         if((exitCode < -1) || (exitCode >= 32))
210                         {
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."));
213                         }
214                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
215                 }
216                 processEncode.close();
217                 processInput.close();
218                 return false;
219         }
220
221         QThread::yieldCurrentThread();
222
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)));
226
227         switch(pass)
228         {
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;
241         }
242
243         setProgress(100);
244         processEncode.close();
245         processInput.close();
246         return true;
247 }
248
249 // ------------------------------------------------------------
250 // Utilities
251 // ------------------------------------------------------------
252
253 double AbstractEncoder::estimateSize(const QString &fileName, const double &progress)
254 {
255         double estimatedSize = 0.0;
256         if(progress >= 0.03)
257         {
258                 QFileInfo fileInfo(fileName);
259                 if(fileInfo.exists() && fileInfo.isFile())
260                 {
261                         const qint64 currentSize = QFileInfo(fileName).size();
262                         estimatedSize = static_cast<double>(currentSize) * (1.0 / qBound(0.0, progress, 1.0));
263                 }
264         }
265         return estimatedSize;
266 }
267
268 QString AbstractEncoder::sizeToString(qint64 size)
269 {
270         static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
271
272         if(size > 1024I64)
273         {
274                 qint64 estimatedSize = size;
275                 qint64 remainderSize = 0I64;
276
277                 int prefixIdx = 0;
278                 while((estimatedSize > 1024I64) && (prefixIdx < 4))
279                 {
280                         remainderSize = estimatedSize % 1024I64;
281                         estimatedSize = estimatedSize / 1024I64;
282                         prefixIdx++;
283                 }
284                         
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]);
287         }
288
289         return tr("N/A");
290 }
291
292 // ------------------------------------------------------------
293 // Encoder Info
294 // ------------------------------------------------------------
295
296 template <class T>
297 static T getElementAt(const QList<T> &list, const quint32 &index)
298 {
299         if (index >= quint32(list.count()))
300         {
301                 MUTILS_THROW("Index is out of bounds!");
302         }
303         return list[index];
304 }
305
306 QStringList AbstractEncoderInfo::getDependencies(const SysinfoModel *sysinfo, const quint32 &encArch, const quint32 &encVariant) const
307 {
308         return QStringList();
309 }
310
311 QString AbstractEncoderInfo::getFullName(const quint32 &encArch, const quint32 &encVariant) const
312 {
313         return QString("%1, %2, %3").arg(getName(), archToString(encArch), variantToString(encVariant));
314 }
315
316 QString AbstractEncoderInfo::archToString(const quint32 &index) const
317 {
318         return getElementAt(getArchitectures(), index).first;
319 }
320
321 AbstractEncoderInfo::ArchBit AbstractEncoderInfo::archToType(const quint32 &index) const
322 {
323         return getElementAt(getArchitectures(), index).second;
324 }
325
326 QString AbstractEncoderInfo::variantToString(const quint32 &index) const
327 {
328         return getElementAt(getVariants(), index);
329 }
330
331 QString AbstractEncoderInfo::rcModeToString(const quint32 &index) const
332 {
333         return getElementAt(getRCModes(), index).first;
334 }
335
336 AbstractEncoderInfo::RCType AbstractEncoderInfo::rcModeToType(const quint32 &index) const
337 {
338         return getElementAt(getRCModes(), index).second;
339 }