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