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