1 /***************************************************************************
3 * Copyright (C) 2011 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
22 #include "kdenlivesettings.h"
23 #include "kdenlivedoc.h"
27 #include <KLocalizedString>
29 #include <mlt++/Mlt.h>
32 static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame frame_ptr)
34 Mlt::Frame frame(frame_ptr);
35 self->emitFrameNumber((int) frame.get_position());
38 MeltJob::MeltJob(ClipType cType, const QString &id, const QStringList ¶meters, const QMap <QString, QString>&extraParams)
39 : AbstractClipJob(MLTJOB, cType, id, parameters),
45 m_showFrameEvent(NULL),
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);
57 void MeltJob::setProducer(Mlt::Producer *producer, const KUrl &url)
59 m_url = QString::fromUtf8(producer->get("resource"));
60 if (m_url == QLatin1String("<playlist>") || m_url == QLatin1String("<tractor>") || m_url == QLatin1String("<producer>"))
64 void MeltJob::startJob()
66 if (m_url.isEmpty()) {
67 m_errorMessage.append(i18n("No producer for this clip."));
68 setStatus(JobCrashed);
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();
83 if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
85 if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
86 if (!m_extra.contains(QLatin1String("finalfilter")))
87 m_extra.insert(QLatin1String("finalfilter"), filter);
89 if (out != -1 && out <= in) {
90 m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
91 setStatus(JobCrashed);
94 if (m_extra.contains(QLatin1String("producer_profile"))) {
95 m_profile = new Mlt::Profile;
96 m_profile->set_explicit(false);
99 m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
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());
106 m_producer = new Mlt::Producer(*m_profile, m_url.toUtf8().constData());
107 if (m_producer) m_length = m_producer->get_length();
110 Mlt::Producer *tmp = new Mlt::Producer(*m_profile, m_url.toUtf8().constData());
111 if (tmp) m_producer = tmp->cut(in, out);
113 if (m_producer) m_length = m_producer->get_playtime();
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);
121 if (m_extra.contains(QLatin1String("producer_profile"))) {
122 m_profile->from_producer(*m_producer);
123 m_profile->set_explicit(true);
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());
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());
135 m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
137 if (!m_consumer || !m_consumer->is_valid()) {
138 m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
139 setStatus(JobCrashed);
143 //m_consumer->set("terminate_on_pause", 1 );
144 //m_consumer->set("eof", "pause" );
145 m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
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());
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);
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());
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);
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);
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);
186 if (!jobResults.isEmpty() && m_jobStatus != JobAborted) {
187 emit gotFilterJobResults(m_clipId, startPos, track, jobResults, m_extra);
189 if (m_jobStatus == JobAborted || m_jobStatus == JobWorking) m_jobStatus = JobDone;
195 delete m_showFrameEvent;
202 const QString MeltJob::destination() const
207 stringMap MeltJob::cancelProperties()
209 QMap <QString, QString> props;
213 const QString MeltJob::statusMessage()
216 switch (m_jobStatus) {
218 statusInfo = description;
221 statusInfo = i18n("Waiting to process clip");
229 void MeltJob::emitFrameNumber(int pos)
232 emit jobProgress(m_clipId, (int) (100 * pos / m_length), jobType);
236 bool MeltJob::isProjectFilter() const
238 return m_extra.contains(QLatin1String("projecttreefilter"));
241 void MeltJob::setStatus(ClipJobStatus status)
243 m_jobStatus = status;
244 if (status == JobAborted && m_consumer) m_consumer->stop();
248 #include "meltjob.moc"