]> git.sesse.net Git - kdenlive/blob - src/projecttree/proxyclipjob.cpp
Fix crash on scene cut analysis: http://kdenlive.org/mantis/view.php?id=2873
[kdenlive] / src / projecttree / proxyclipjob.cpp
1 /***************************************************************************
2  *                                                                         *
3  *   Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
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     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
19  ***************************************************************************/
20
21 #include "proxyclipjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
24
25 #include <KDebug>
26 #include <KLocale>
27
28 ProxyJob::ProxyJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(PROXYJOB, cType, id, parameters),
29     m_jobDuration(0),
30     m_isFfmpegJob(true)
31 {
32     m_jobStatus = JOBWAITING;
33     description = i18n("proxy");
34     m_dest = parameters.at(0);
35     m_src = parameters.at(1);
36     m_exif = parameters.at(2).toInt();
37     m_proxyParams = parameters.at(3);
38     m_renderWidth = parameters.at(4).toInt();
39     m_renderHeight = parameters.at(5).toInt();
40     replaceClip = true;
41 }
42
43 void ProxyJob::startJob()
44 {
45     // Special case: playlist clips (.mlt or .kdenlive project files)
46     m_jobDuration = 0;
47     if (clipType == PLAYLIST) {
48         // change FFmpeg params to MLT format
49         m_isFfmpegJob = false;
50         QStringList mltParameters;
51         mltParameters << m_src;
52         mltParameters << "-consumer" << "avformat:" + m_dest;
53         QStringList params = m_proxyParams.split('-', QString::SkipEmptyParts);
54                 
55         foreach(const QString &s, params) {
56             QString t = s.simplified();
57             if (t.count(' ') == 0) {
58                 t.append("=1");
59             }
60             else t.replace(' ', '=');
61             mltParameters << t;
62         }
63         
64         mltParameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
65
66         //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
67         double display_ratio = KdenliveDoc::getDisplayRatio(m_src);
68         mltParameters << "aspect=" + QString::number(display_ratio);
69             
70         // Ask for progress reporting
71         mltParameters << "progress=1";
72
73         m_jobProcess = new QProcess;
74         m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
75         m_jobProcess->start(KdenliveSettings::rendererpath(), mltParameters);
76         m_jobProcess->waitForStarted();
77     }
78     else if (clipType == IMAGE) {
79         m_isFfmpegJob = false;
80         // Image proxy
81         QImage i(m_src);
82         if (i.isNull()) {
83             m_errorMessage.append(i18n("Cannot load image %1.", m_src));
84             setStatus(JOBCRASHED);
85             return;
86         }
87         
88         QImage proxy;
89         // Images are scaled to profile size. 
90         //TODO: Make it be configurable?
91         if (i.width() > i.height()) proxy = i.scaledToWidth(m_renderWidth);
92         else proxy = i.scaledToHeight(m_renderHeight);
93         if (m_exif > 1) {
94             // Rotate image according to exif data
95             QImage processed;
96             QMatrix matrix;
97
98             switch ( m_exif ) {
99                 case 2:
100                     matrix.scale( -1, 1 );
101                     break;
102                 case 3:
103                     matrix.rotate( 180 );
104                     break;
105                 case 4:
106                     matrix.scale( 1, -1 );
107                     break;
108                 case 5:
109                     matrix.rotate( 270 );
110                     matrix.scale( -1, 1 );
111                     break;
112                 case 6:
113                     matrix.rotate( 90 );
114                     break;
115                 case 7:
116                     matrix.rotate( 90 );
117                     matrix.scale( -1, 1 );
118                     break;
119                 case 8:
120                     matrix.rotate( 270 );
121                     break;
122             }
123             processed = proxy.transformed( matrix );
124             processed.save(m_dest);
125         }
126         else proxy.save(m_dest);
127         setStatus(JOBDONE);
128         return;
129     }
130     else {
131         m_isFfmpegJob = true;
132         QStringList parameters;
133         parameters << "-i" << m_src;
134         QString params = m_proxyParams;
135         foreach(const QString &s, params.split(' '))
136             parameters << s;
137
138         // Make sure we don't block when proxy file already exists
139         parameters << "-y";
140         parameters << m_dest;
141         m_jobProcess = new QProcess;
142         m_jobProcess->setProcessChannelMode(QProcess::MergedChannels);
143         m_jobProcess->start(KdenliveSettings::ffmpegpath(), parameters, QIODevice::ReadOnly);
144         m_jobProcess->waitForStarted();
145     }
146     while (m_jobProcess->state() != QProcess::NotRunning) {
147         processLogInfo();
148         if (m_jobStatus == JOBABORTED) {
149             emit cancelRunningJob(m_clipId, cancelProperties());
150             m_jobProcess->close();
151             m_jobProcess->waitForFinished();
152             QFile::remove(m_dest);
153         }
154         m_jobProcess->waitForFinished(400);
155     }
156     
157     if (m_jobStatus != JOBABORTED) {
158         int result = m_jobProcess->exitStatus();
159         if (result == QProcess::NormalExit) {
160             if (QFileInfo(m_dest).size() == 0) {
161                 // File was not created
162                 processLogInfo();
163                 m_errorMessage.append(i18n("Failed to create proxy clip."));
164                 setStatus(JOBCRASHED);
165             }
166             else setStatus(JOBDONE);
167         }
168         else if (result == QProcess::CrashExit) {
169             // Proxy process crashed
170             QFile::remove(m_dest);
171             setStatus(JOBCRASHED);
172         }
173     }
174     
175     delete m_jobProcess;
176     return;
177 }
178
179 void ProxyJob::processLogInfo()
180 {
181     if (!m_jobProcess || m_jobStatus == JOBABORTED) return;
182     QString log = m_jobProcess->readAll();
183     if (!log.isEmpty()) m_logDetails.append(log + '\n');
184     else return;
185     int progress;
186     if (m_isFfmpegJob) {
187         // Parse FFmpeg output
188         if (m_jobDuration == 0) {
189             if (log.contains("Duration:")) {
190                 QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
191                 QStringList numbers = data.split(':');
192                 m_jobDuration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
193             }
194         }
195         else if (log.contains("time=")) {
196             QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
197             if (time.contains(':')) {
198                 QStringList numbers = time.split(':');
199                 progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
200             }
201             else progress = (int) time.toDouble();
202             emit jobProgress(m_clipId, (int) (100.0 * progress / m_jobDuration), jobType);
203         }
204     }
205     else {
206         // Parse MLT output
207         if (log.contains("percentage:")) {
208             progress = log.section(':', -1).simplified().toInt();
209             emit jobProgress(m_clipId, progress, jobType);
210         }
211     }
212 }
213
214 ProxyJob::~ProxyJob()
215 {
216 }
217
218 const QString ProxyJob::destination() const
219 {
220     return m_dest;
221 }
222
223 stringMap ProxyJob::cancelProperties()
224 {
225     QMap <QString, QString> props;
226     props.insert("proxy", "-");
227     return props;
228 }
229
230 const QString ProxyJob::statusMessage()
231 {
232     QString statusInfo;
233     switch (m_jobStatus) {
234         case JOBWORKING:
235             statusInfo = i18n("Creating proxy");
236             break;
237         case JOBWAITING:
238             statusInfo = i18n("Waiting - proxy");
239             break;
240         default:
241             break;
242     }
243     return statusInfo;
244 }
245