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