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