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"
28 #include <mlt++/Mlt.h>
31 static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame /*frame_ptr*/)
33 // detect if the producer has finished playing. Is there a better way to do it?
34 self->emitFrameNumber();
37 MeltJob::MeltJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(MLTJOB, cType, id, parameters),
43 jobStatus = JOBWAITING;
44 m_params = parameters;
45 description = i18n("Process clip");
46 QString consum = m_params.at(5);
47 if (consum.contains(":")) m_dest = consum.section(":", 1);
50 void MeltJob::setProducer(Mlt::Producer *producer)
52 m_producer = producer;
55 void MeltJob::startJob()
58 m_errorMessage.append(i18n("No producer for this clip."));
59 setStatus(JOBCRASHED);
62 int in = m_params.takeFirst().toInt();
63 int out = m_params.takeFirst().toInt();
64 QString producerParams =m_params.takeFirst();
65 QString filter = m_params.takeFirst();
66 QString filterParams = m_params.takeFirst();
67 QString consumer = m_params.takeFirst();
68 kDebug()<<"consumer: "<<consumer;
69 if (consumer.contains(":")) m_dest = consumer.section(":", 1);
70 QString consumerParams = m_params.takeFirst();
74 if (!m_params.isEmpty()) properties = m_params.takeFirst();
76 if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
78 if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
80 if (!m_params.isEmpty()) finalFilter = m_params.takeFirst();
81 else finalFilter = filter;
83 if (out != -1 && out <= in) {
84 m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
85 setStatus(JOBCRASHED);
89 m_profile = m_producer->profile();
93 QString url = QString::fromUtf8(m_producer->get("resource"));
94 prod = new Mlt::Producer(*m_profile, url.toUtf8().constData());
97 prod = m_producer->cut(in, out);
98 QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
99 foreach(QString data, list) {
100 if (data.contains('=')) {
101 prod->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
105 if (consumer.contains(":")) {
106 m_consumer = new Mlt::Consumer(*m_profile, consumer.section(":", 0, 0).toUtf8().constData(), consumer.section(":", 1).toUtf8().constData());
109 m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
111 if (!m_consumer || !m_consumer->is_valid()) {
112 m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
113 setStatus(JOBCRASHED);
117 //m_consumer->set("terminate_on_pause", 1 );
118 //m_consumer->set("eof", "pause" );
119 m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
121 list = consumerParams.split(' ', QString::SkipEmptyParts);
122 foreach(QString data, list) {
123 if (data.contains('=')) {
124 kDebug()<<"// filter con: "<<data;
125 m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
129 Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
130 list = filterParams.split(' ', QString::SkipEmptyParts);
131 foreach(QString data, list) {
132 if (data.contains('=')) {
133 kDebug()<<"// filter p: "<<data;
134 mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
137 Mlt::Playlist playlist;
138 playlist.append(*prod);
139 m_length = prod->get_length();
140 m_consumer->connect(playlist);
143 prod->attach(mltFilter);
144 m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
147 while (jobStatus != JOBABORTED && !m_consumer->is_stopped()) {
151 QStringList wanted = properties.split(',', QString::SkipEmptyParts);
152 stringMap jobResults;
153 foreach(const QString key, wanted) {
154 QString value = mltFilter.get(key.toUtf8().constData());
155 jobResults.insert(key, value);
156 kDebug()<<"RESULT: "<<key<<" = "<< value;
158 if (!jobResults.isEmpty()) emit gotFilterJobResults(m_clipId, startPos, track, finalFilter, jobResults);
170 const QString MeltJob::destination() const
175 stringMap MeltJob::cancelProperties()
177 QMap <QString, QString> props;
181 const QString MeltJob::statusMessage()
186 statusInfo = i18n("Processing clip");
189 statusInfo = i18n("Waiting - process clip");
197 void MeltJob::emitFrameNumber()
199 if (m_consumer && m_length > 0) {
200 emit jobProgress(m_clipId, (int) (100 * m_consumer->position() / m_length), jobType);