]> git.sesse.net Git - kdenlive/blob - src/projecttree/meltjob.cpp
294ff94f12be34c8c5f912b719bf0d20db11479e
[kdenlive] / src / projecttree / meltjob.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 "meltjob.h"
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
24
25 #include <KDebug>
26 #include <KUrl>
27 #include <KLocalizedString>
28
29 #include <mlt++/Mlt.h>
30
31
32 static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame frame_ptr)
33 {
34     Mlt::Frame frame(frame_ptr);
35     self->emitFrameNumber((int) frame.get_position());
36 }
37
38 MeltJob::MeltJob(CLIPTYPE cType, const QString &id, const QStringList &parameters,  const QMap <QString, QString>&extraParams)
39     : AbstractClipJob(MLTJOB, cType, id, parameters),
40     addClipToProject(0),
41     m_consumer(NULL),
42     m_producer(NULL),
43     m_profile(NULL),
44     m_filter(NULL),
45     m_showFrameEvent(NULL),
46     m_length(0),
47     m_extra(extraParams)
48 {
49     m_jobStatus = JOBWAITING;
50     m_params = parameters;
51     description = i18n("Process clip");
52     QString consum = m_params.at(5);
53     if (consum.contains(':')) m_dest = consum.section(':', 1);
54 }
55
56 void MeltJob::setProducer(Mlt::Producer *producer, const KUrl &url)
57 {
58     m_url = QString::fromUtf8(producer->get("resource"));
59     if (m_url == "<playlist>" || m_url == "<tractor>" || m_url == "<producer>")
60         m_url == url.path();
61 }
62
63 void MeltJob::startJob()
64 {
65     if (m_url.isEmpty()) {
66         m_errorMessage.append(i18n("No producer for this clip."));
67         setStatus(JOBCRASHED);
68         return;
69     }
70     int in = m_params.takeFirst().toInt();
71     if (in > 0 && !m_extra.contains("offset")) m_extra.insert("offset", QString::number(in));
72     int out = m_params.takeFirst().toInt();
73     QString producerParams =m_params.takeFirst(); 
74     QString filter = m_params.takeFirst();
75     QString filterParams = m_params.takeFirst();
76     QString consumer = m_params.takeFirst();
77     if (consumer.contains(':')) m_dest = consumer.section(':', 1);
78     QString consumerParams = m_params.takeFirst();
79     
80     // optional params
81     int startPos = -1;
82     if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
83     int track = -1;
84     if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
85     if (!m_extra.contains("finalfilter")) m_extra.insert("finalfilter", filter);
86
87     if (out != -1 && out <= in) {
88         m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
89         setStatus(JOBCRASHED);
90         return;
91     }
92     if (m_extra.contains("producer_profile")) {
93         m_profile = new Mlt::Profile;
94         m_profile->set_explicit(false);
95     }
96     else {
97         m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
98     }
99     if (m_extra.contains("resize_profile")) {   
100         m_profile->set_height(m_extra.value("resize_profile").toInt());
101         m_profile->set_width(m_profile->height() * m_profile->sar());
102     }
103     if (out == -1) {
104         m_producer = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
105         if (m_producer) m_length = m_producer->get_length();
106     }
107     else {
108         Mlt::Producer *tmp = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
109         if (tmp) m_producer = tmp->cut(in, out);
110         delete tmp;
111         if (m_producer) m_length = m_producer->get_playtime();
112     }
113     if (!m_producer || !m_producer->is_valid()) {
114         // Clip was removed or something went wrong, Notify user?
115         //m_errorMessage.append(i18n("Invalid clip"));
116         setStatus(JOBCRASHED);
117         return;
118     }
119     if (m_extra.contains("producer_profile")) {
120         m_profile->from_producer(*m_producer);
121         m_profile->set_explicit(true);
122     }
123     QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
124     foreach(const QString &data, list) {
125         if (data.contains('=')) {
126             m_producer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
127         }
128     }
129     if (consumer.contains(":")) {
130         m_consumer = new Mlt::Consumer(*m_profile, consumer.section(':', 0, 0).toUtf8().constData(), consumer.section(':', 1).toUtf8().constData());
131     }
132     else {
133         m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
134     }
135     if (!m_consumer || !m_consumer->is_valid()) {
136         m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
137         setStatus(JOBCRASHED);
138         return;
139     }
140
141     //m_consumer->set("terminate_on_pause", 1 );
142     //m_consumer->set("eof", "pause" );
143     m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
144
145
146     list = consumerParams.split(' ', QString::SkipEmptyParts);
147     foreach(const QString &data, list) {
148         if (data.contains('=')) {
149             kDebug()<<"// filter con: "<<data;
150             m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
151         }
152     }
153     
154     m_filter = new Mlt::Filter(*m_profile, filter.toUtf8().data());
155     if (!m_filter || !m_filter->is_valid()) {
156         m_errorMessage = i18n("Filter %1 crashed", filter);
157         setStatus(JOBCRASHED);
158         return;
159     }
160     list = filterParams.split(' ', QString::SkipEmptyParts);
161     foreach(const QString &data, list) {
162         if (data.contains('=')) {
163             kDebug()<<"// filter p: "<<data;
164             m_filter->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
165         }
166     }
167     Mlt::Tractor tractor;
168     Mlt::Playlist playlist;
169     playlist.append(*m_producer);
170     tractor.set_track(playlist, 0);
171     m_consumer->connect(tractor);
172     m_producer->set_speed(0);
173     m_producer->seek(0);
174     m_producer->attach(*m_filter);
175     m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
176     m_producer->set_speed(1);
177     m_consumer->run();
178     
179     QMap <QString, QString> jobResults;
180     if (m_jobStatus != JOBABORTED && m_extra.contains("key")) {
181         QString result = m_filter->get(m_extra.value("key").toUtf8().constData());
182         jobResults.insert(m_extra.value("key"), result);
183     }
184     if (!jobResults.isEmpty() && m_jobStatus != JOBABORTED) {
185         emit gotFilterJobResults(m_clipId, startPos, track, jobResults, m_extra);
186     }
187     if (m_jobStatus == JOBABORTED || m_jobStatus == JOBWORKING) m_jobStatus = JOBDONE;
188 }
189
190
191 MeltJob::~MeltJob()
192 {
193     delete m_showFrameEvent;
194     delete m_filter;
195     delete m_producer;
196     delete m_consumer;
197     delete m_profile;
198 }
199
200 const QString MeltJob::destination() const
201 {
202     return m_dest;
203 }
204
205 stringMap MeltJob::cancelProperties()
206 {
207     QMap <QString, QString> props;
208     return props;
209 }
210
211 const QString MeltJob::statusMessage()
212 {
213     QString statusInfo;
214     switch (m_jobStatus) {
215         case JOBWORKING:
216             statusInfo = description;
217             break;
218         case JOBWAITING:
219             statusInfo = i18n("Waiting to process clip");
220             break;
221         default:
222             break;
223     }
224     return statusInfo;
225 }
226
227 void MeltJob::emitFrameNumber(int pos)
228 {
229     if (m_length > 0) {
230         emit jobProgress(m_clipId, (int) (100 * pos / m_length), jobType);
231     }
232 }
233
234 bool MeltJob::isProjectFilter() const
235 {
236     return m_extra.contains("projecttreefilter");
237 }
238
239 void MeltJob::setStatus(CLIPJOBSTATUS status)
240 {
241     m_jobStatus = status;
242     if (status == JOBABORTED && m_consumer) m_consumer->stop();
243 }
244
245
246 #include "meltjob.moc"