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"
29 #include <mlt++/Mlt.h>
32 static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame /*frame_ptr*/)
34 // detect if the producer has finished playing. Is there a better way to do it?
35 self->emitFrameNumber();
38 MeltJob::MeltJob(CLIPTYPE cType, const QString &id, QStringList parameters, QStringList extraParams) : AbstractClipJob(MLTJOB, cType, id, parameters),
43 m_showFrameEvent(NULL),
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);
54 void MeltJob::setProducer(Mlt::Producer *producer, KUrl url)
56 m_producer = producer;
57 m_url = QString::fromUtf8(m_producer->get("resource"));
58 if (m_url == "<playlist>" || m_url == "<tractor>" || m_url == "<producer>")
62 void MeltJob::startJob()
65 m_errorMessage.append(i18n("No producer for this clip."));
66 setStatus(JOBCRASHED);
69 int in = m_params.takeFirst().toInt();
70 int out = m_params.takeFirst().toInt();
71 QString producerParams =m_params.takeFirst();
72 QString filter = m_params.takeFirst();
73 QString filterParams = m_params.takeFirst();
74 QString consumer = m_params.takeFirst();
75 if (consumer.contains(':')) m_dest = consumer.section(':', 1);
76 QString consumerParams = m_params.takeFirst();
80 if (!m_params.isEmpty()) properties = m_params.takeFirst();
82 if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
84 if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
86 if (!m_params.isEmpty()) finalFilter = m_params.takeFirst();
87 else finalFilter = filter;
90 if (out != -1 && out <= in) {
91 m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
92 setStatus(JOBCRASHED);
96 if (m_extra.contains("project_profile")) {
97 m_profile = new Mlt::Profile(KdenliveSettings::current_profile().toUtf8().constData());
100 m_profile = new Mlt::Profile;
101 m_profile->set_explicit(false);
104 prod = new Mlt::Producer(*m_profile, m_url.toUtf8().constData());
107 Mlt::Producer *tmp = new Mlt::Producer(*m_profile, m_url.toUtf8().constData());
108 prod = tmp->cut(in, out);
111 if (!m_extra.contains("project_profile")) {
112 m_profile->from_producer(*prod);
113 m_profile->set_explicit(true);
115 QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
116 foreach(const QString &data, list) {
117 if (data.contains('=')) {
118 prod->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
121 if (consumer.contains(":")) {
122 m_consumer = new Mlt::Consumer(*m_profile, consumer.section(':', 0, 0).toUtf8().constData(), consumer.section(':', 1).toUtf8().constData());
125 m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
127 if (!m_consumer || !m_consumer->is_valid()) {
128 m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
129 setStatus(JOBCRASHED);
133 //m_consumer->set("terminate_on_pause", 1 );
134 //m_consumer->set("eof", "pause" );
135 m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
138 list = consumerParams.split(' ', QString::SkipEmptyParts);
139 foreach(const QString &data, list) {
140 if (data.contains('=')) {
141 kDebug()<<"// filter con: "<<data;
142 m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
146 Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
147 list = filterParams.split(' ', QString::SkipEmptyParts);
148 foreach(const QString &data, list) {
149 if (data.contains('=')) {
150 kDebug()<<"// filter p: "<<data;
151 mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
154 Mlt::Tractor tractor;
155 Mlt::Playlist playlist;
156 playlist.append(*prod);
157 tractor.set_track(playlist, 0);
158 m_length = prod->get_length();
159 m_consumer->connect(tractor);
162 prod->attach(mltFilter);
163 m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
166 while (jobStatus != JOBABORTED && !m_consumer->is_stopped()) {
170 QStringList wanted = properties.split(',', QString::SkipEmptyParts);
171 stringMap jobResults;
172 foreach(const QString &key, wanted) {
173 QString value = mltFilter.get(key.toUtf8().constData());
174 jobResults.insert(key, value);
176 if (!jobResults.isEmpty()) emit gotFilterJobResults(m_clipId, startPos, track, finalFilter, jobResults);
188 const QString MeltJob::destination() const
193 stringMap MeltJob::cancelProperties()
195 QMap <QString, QString> props;
199 const QString MeltJob::statusMessage()
204 statusInfo = description;
207 statusInfo = i18n("Waiting to process clip");
215 void MeltJob::emitFrameNumber()
217 if (m_consumer && m_length > 0) {
218 emit jobProgress(m_clipId, (int) (100 * m_consumer->position() / m_length), jobType);
222 bool MeltJob::isProjectFilter() const
224 return m_extra.contains("projecttreefilter");