OSDN Git Service

Happy new year 2015 !!!
[x264-launcher/x264-launcher.git] / src / encoder_abstract.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
3 // Copyright (C) 2004-2015 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 "model_status.h"
29 #include "source_abstract.h"
30 #include "binaries.h"
31
32 #include <QProcess>
33 #include <QDir>
34 #include <QTextCodec>
35 #include <QSemaphore>
36 #include <QDate>
37 #include <QTime>
38 #include <QThread>
39 #include <QLocale>
40
41 // ------------------------------------------------------------
42 // Helper Macros
43 // ------------------------------------------------------------
44
45 #define APPEND_AND_CLEAR(LIST, STR) do \
46 { \
47         if(!((STR).isEmpty())) \
48         { \
49                 (LIST) << (STR); \
50                 (STR).clear(); \
51         } \
52 } \
53 while(0)
54
55 // ------------------------------------------------------------
56 // Constructor & Destructor
57 // ------------------------------------------------------------
58
59 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)
60 :
61         AbstractTool(jobObject, options, sysinfo, preferences, jobStatus, abort, pause, semaphorePause),
62         m_sourceFile(sourceFile),
63         m_outputFile(outputFile),
64         m_indexFile(QString("%1/~%2.ffindex").arg(QDir::tempPath(), stringToHash(m_sourceFile)))
65 {
66         /*Nothing to do here*/
67 }
68
69 AbstractEncoder::~AbstractEncoder(void)
70 {
71         /*Nothing to do here*/
72 }
73
74 // ------------------------------------------------------------
75 // Encoding Functions
76 // ------------------------------------------------------------
77
78 bool AbstractEncoder::runEncodingPass(AbstractSource* pipedSource, const QString outputFile, const unsigned int &frames, const int &pass, const QString &passLogFile)
79 {
80         QProcess processEncode, processInput;
81         
82         if(pipedSource)
83         {
84                 pipedSource->createProcess(processEncode, processInput);
85         }
86
87         QStringList cmdLine_Encode;
88         buildCommandLine(cmdLine_Encode, (pipedSource != NULL), frames, m_indexFile, pass, passLogFile);
89
90         log("Creating encoder process:");
91         if(!startProcess(processEncode, ENC_BINARY(m_sysinfo, m_options), cmdLine_Encode))
92         {
93                 return false;
94         }
95
96         QList<QRegExp*> patterns;
97         runEncodingPass_init(patterns);
98         
99         double last_progress = 0.0;
100         double size_estimate = 0.0;
101         
102         bool bTimeout = false;
103         bool bAborted = false;
104
105         //Main processing loop
106         while(processEncode.state() != QProcess::NotRunning)
107         {
108                 unsigned int waitCounter = 0;
109
110                 //Wait until new output is available
111                 forever
112                 {
113                         if(*m_abort)
114                         {
115                                 processEncode.kill();
116                                 processInput.kill();
117                                 bAborted = true;
118                                 break;
119                         }
120                         if((*m_pause) && (processEncode.state() == QProcess::Running))
121                         {
122                                 JobStatus previousStatus = m_jobStatus;
123                                 setStatus(JobStatus_Paused);
124                                 log(tr("Job paused by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
125                                 bool ok[2] = {false, false};
126                                 QProcess *proc[2] = { &processEncode, &processInput };
127                                 ok[0] = x264_suspendProcess(proc[0], true);
128                                 ok[1] = x264_suspendProcess(proc[1], true);
129                                 while(*m_pause) m_semaphorePause->tryAcquire(1, 5000);
130                                 while(m_semaphorePause->tryAcquire(1, 0));
131                                 ok[0] = x264_suspendProcess(proc[0], false);
132                                 ok[1] = x264_suspendProcess(proc[1], false);
133                                 if(!(*m_abort)) setStatus(previousStatus);
134                                 log(tr("Job resumed by user at %1, %2.").arg(QDate::currentDate().toString(Qt::ISODate), QTime::currentTime().toString( Qt::ISODate)));
135                                 waitCounter = 0;
136                                 continue;
137                         }
138                         if(!processEncode.waitForReadyRead(m_processTimeoutInterval))
139                         {
140                                 if(processEncode.state() == QProcess::Running)
141                                 {
142                                         if(++waitCounter > m_processTimeoutMaxCounter)
143                                         {
144                                                 if(m_preferences->getAbortOnTimeout())
145                                                 {
146                                                         processEncode.kill();
147                                                         qWarning("encoder process timed out <-- killing!");
148                                                         log("\nPROCESS TIMEOUT !!!");
149                                                         bTimeout = true;
150                                                         break;
151                                                 }
152                                         }
153                                         else if(waitCounter == m_processTimeoutWarning)
154                                         {
155                                                 unsigned int timeOut = (waitCounter * m_processTimeoutInterval) / 1000U;
156                                                 log(tr("Warning: encoder did not respond for %1 seconds, potential deadlock...").arg(QString::number(timeOut)));
157                                         }
158                                         continue;
159                                 }
160                         }
161                         if((*m_abort) || ((*m_pause) && (processEncode.state() == QProcess::Running)))
162                         {
163                                 continue;
164                         }
165                         break;
166                 }
167                 
168                 //Exit main processing loop now?
169                 if(bAborted || bTimeout)
170                 {
171                         break;
172                 }
173
174                 //Process all output
175                 PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, pass, last_progress, size_estimate);
176         }
177         
178         if(!(bTimeout || bAborted))
179         {
180                 PROCESS_PENDING_LINES(processEncode, runEncodingPass_parseLine, patterns, pass, last_progress, size_estimate);
181         }
182
183         processEncode.waitForFinished(5000);
184         if(processEncode.state() != QProcess::NotRunning)
185         {
186                 qWarning("Encoder process still running, going to kill it!");
187                 processEncode.kill();
188                 processEncode.waitForFinished(-1);
189         }
190         
191         if(pipedSource)
192         {
193                 processInput.waitForFinished(5000);
194                 if(processInput.state() != QProcess::NotRunning)
195                 {
196                         qWarning("Input process still running, going to kill it!");
197                         processInput.kill();
198                         processInput.waitForFinished(-1);
199                 }
200                 if(!(bTimeout || bAborted))
201                 {
202                         pipedSource->flushProcess(processInput);
203                 }
204         }
205
206         while(!patterns.isEmpty())
207         {
208                 QRegExp *pattern = patterns.takeFirst();
209                 X264_DELETE(pattern);
210         }
211
212         if(bTimeout || bAborted || processEncode.exitCode() != EXIT_SUCCESS)
213         {
214                 if(!(bTimeout || bAborted))
215                 {
216                         const int exitCode = processEncode.exitCode();
217                         if((exitCode < -1) || (exitCode >= 32))
218                         {
219                                 log(tr("\nFATAL ERROR: The encoder process has *crashed* -> your encode probably is *incomplete* !!!"));
220                                 log(tr("Note that this indicates a bug in the current encoder, *not* in Simple x264/x265 Launcher."));
221                         }
222                         log(tr("\nPROCESS EXITED WITH ERROR CODE: %1").arg(QString::number(exitCode)));
223                 }
224                 processEncode.close();
225                 processInput.close();
226                 return false;
227         }
228
229         QThread::yieldCurrentThread();
230
231         QFileInfo completedFileInfo(m_outputFile);
232         const qint64 finalSize = (completedFileInfo.exists() && completedFileInfo.isFile()) ? completedFileInfo.size() : 0;
233         log(tr("Final file size is %1 bytes.").arg(sizeToString(finalSize)));
234
235         switch(pass)
236         {
237         case 1:
238                 setStatus(JobStatus_Running_Pass1);
239                 setDetails(tr("First pass completed. Preparing for second pass..."));
240                 break;
241         case 2:
242                 setStatus(JobStatus_Running_Pass2);
243                 setDetails(tr("Second pass completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
244                 break;
245         default:
246                 setStatus(JobStatus_Running);
247                 setDetails(tr("Encode completed successfully. Final size is %1.").arg(sizeToString(finalSize)));
248                 break;
249         }
250
251         setProgress(100);
252         processEncode.close();
253         processInput.close();
254         return true;
255 }
256
257 // ------------------------------------------------------------
258 // Utilities
259 // ------------------------------------------------------------
260
261 QStringList AbstractEncoder::splitParams(const QString &params, const QString &sourceFile, const QString &outputFile)
262 {
263         QStringList list; 
264         bool ignoreWhitespaces = false;
265         QString temp;
266
267         for(int i = 0; i < params.length(); i++)
268         {
269                 const QChar c = params.at(i);
270
271                 if(c == QChar::fromLatin1('"'))
272                 {
273                         ignoreWhitespaces = (!ignoreWhitespaces);
274                         continue;
275                 }
276                 else if((!ignoreWhitespaces) && (c == QChar::fromLatin1(' ')))
277                 {
278                         APPEND_AND_CLEAR(list, temp);
279                         continue;
280                 }
281                 
282                 temp.append(c);
283         }
284         
285         APPEND_AND_CLEAR(list, temp);
286
287         list.replaceInStrings("$(INPUT)",  QDir::toNativeSeparators(sourceFile), Qt::CaseInsensitive);
288         list.replaceInStrings("$(OUTPUT)", QDir::toNativeSeparators(outputFile), Qt::CaseInsensitive);
289
290         return list;
291 }
292
293 double AbstractEncoder::estimateSize(const QString &fileName, const double &progress)
294 {
295         double estimatedSize = 0.0;
296         if(progress >= 0.03)
297         {
298                 QFileInfo fileInfo(fileName);
299                 if(fileInfo.exists() && fileInfo.isFile())
300                 {
301                         const qint64 currentSize = QFileInfo(fileName).size();
302                         estimatedSize = static_cast<double>(currentSize) * (1.0 / qBound(0.0, progress, 1.0));
303                 }
304         }
305         return estimatedSize;
306 }
307
308 QString AbstractEncoder::sizeToString(qint64 size)
309 {
310         static char *prefix[5] = {"Byte", "KB", "MB", "GB", "TB"};
311
312         if(size > 1024I64)
313         {
314                 qint64 estimatedSize = size;
315                 qint64 remainderSize = 0I64;
316
317                 int prefixIdx = 0;
318                 while((estimatedSize > 1024I64) && (prefixIdx < 4))
319                 {
320                         remainderSize = estimatedSize % 1024I64;
321                         estimatedSize = estimatedSize / 1024I64;
322                         prefixIdx++;
323                 }
324                         
325                 double value = static_cast<double>(estimatedSize) + (static_cast<double>(remainderSize) / 1024.0);
326                 return QString().sprintf((value < 10.0) ? "%.2f %s" : "%.1f %s", value, prefix[prefixIdx]);
327         }
328
329         return tr("N/A");
330 }
331
332 // ------------------------------------------------------------
333 // Encoder Info
334 // ------------------------------------------------------------
335
336 const AbstractEncoderInfo& AbstractEncoder::getEncoderInfo(void)
337 {
338         THROW("[getEncoderInfo] This function must be overwritten in sub-classes!");
339 }