]> git.sesse.net Git - kdenlive/blob - src/projecttree/meltjob.cpp
Cleanup & fix melt job (like video stab) not keeping original clip profile
[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     self->emitFrameNumber();
36 }
37
38 MeltJob::MeltJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(MLTJOB, cType, id, parameters),
39     addClipToProject(0),
40     m_producer(NULL),
41     m_profile(NULL),
42     m_consumer(NULL),
43     m_showFrameEvent(NULL),
44     m_length(0)
45 {
46     jobStatus = JOBWAITING;
47     m_params = parameters;
48     description = i18n("Process clip");
49     QString consum = m_params.at(5);
50     if (consum.contains(':')) m_dest = consum.section(':', 1);
51 }
52
53 void MeltJob::setProducer(Mlt::Producer *producer, KUrl url)
54 {
55     m_producer = producer;
56     m_url = QString::fromUtf8(m_producer->get("resource"));
57     if (m_url == "<playlist>" || m_url == "<tractor>" || m_url == "<producer>")
58         m_url == url.path();
59 }
60
61 void MeltJob::startJob()
62 {
63     if (!m_producer) {
64         m_errorMessage.append(i18n("No producer for this clip."));
65         setStatus(JOBCRASHED);
66         return;
67     }
68     int in = m_params.takeFirst().toInt();
69     int out = m_params.takeFirst().toInt();
70     QString producerParams =m_params.takeFirst(); 
71     QString filter = m_params.takeFirst();
72     QString filterParams = m_params.takeFirst();
73     QString consumer = m_params.takeFirst();
74     kDebug()<<"consumer: "<<consumer;
75     if (consumer.contains(':')) m_dest = consumer.section(':', 1);
76     QString consumerParams = m_params.takeFirst();
77     
78     // optional params
79     QString properties;
80     if (!m_params.isEmpty()) properties = m_params.takeFirst();
81     int startPos = -1;
82     if (!m_params.isEmpty()) startPos = m_params.takeFirst().toInt();
83     int track = -1;
84     if (!m_params.isEmpty()) track = m_params.takeFirst().toInt();
85     QString finalFilter;
86     if (!m_params.isEmpty()) finalFilter = m_params.takeFirst();
87     else 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     Mlt::Producer *prod ;
95     m_profile = new Mlt::Profile;
96     m_profile->set_explicit(false);
97     if (out == -1) {
98         prod = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
99     }
100     else {
101         Mlt::Producer *tmp = new Mlt::Producer(*m_profile,  m_url.toUtf8().constData());
102         prod = tmp->cut(in, out);
103         delete tmp;
104     }
105     m_profile->from_producer(*prod);
106     m_profile->set_explicit(true);
107     QStringList list = producerParams.split(' ', QString::SkipEmptyParts);
108     foreach(const QString &data, list) {
109         if (data.contains('=')) {
110             prod->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
111         }
112     }
113     if (consumer.contains(":")) {
114         m_consumer = new Mlt::Consumer(*m_profile, consumer.section(':', 0, 0).toUtf8().constData(), consumer.section(':', 1).toUtf8().constData());
115     }
116     else {
117         m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
118     }
119     if (!m_consumer || !m_consumer->is_valid()) {
120         m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
121         setStatus(JOBCRASHED);
122         return;
123     }
124
125     //m_consumer->set("terminate_on_pause", 1 );
126     //m_consumer->set("eof", "pause" );
127     m_consumer->set("real_time", -KdenliveSettings::mltthreads() );
128
129     list = consumerParams.split(' ', QString::SkipEmptyParts);
130     foreach(const QString &data, list) {
131         if (data.contains('=')) {
132             kDebug()<<"// filter con: "<<data;
133             m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
134         }
135     }
136     
137     Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
138     list = filterParams.split(' ', QString::SkipEmptyParts);
139     foreach(const QString &data, list) {
140         if (data.contains('=')) {
141             kDebug()<<"// filter p: "<<data;
142             mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
143         }
144     }
145     Mlt::Tractor tractor;
146     Mlt::Playlist playlist;
147     playlist.append(*prod);
148     tractor.set_track(playlist, 0);
149     m_length = prod->get_length();
150     m_consumer->connect(tractor);
151     prod->set_speed(0);
152     prod->seek(0);
153     prod->attach(mltFilter);
154     m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
155     m_consumer->start();
156     prod->set_speed(1);
157     while (jobStatus != JOBABORTED && !m_consumer->is_stopped()) {
158         
159     }
160     m_consumer->stop();
161     QStringList wanted = properties.split(',', QString::SkipEmptyParts);
162     stringMap jobResults;
163     foreach(const QString &key, wanted) {
164         QString value = mltFilter.get(key.toUtf8().constData());
165         jobResults.insert(key, value);
166     }
167     if (!jobResults.isEmpty()) emit gotFilterJobResults(m_clipId, startPos, track, finalFilter, jobResults);
168     setStatus(JOBDONE);
169     delete m_consumer;
170     delete prod;
171     return;
172 }
173
174
175 MeltJob::~MeltJob()
176 {
177 }
178
179 const QString MeltJob::destination() const
180 {
181     return m_dest;
182 }
183
184 stringMap MeltJob::cancelProperties()
185 {
186     QMap <QString, QString> props;
187     return props;
188 }
189
190 const QString MeltJob::statusMessage()
191 {
192     QString statusInfo;
193     switch (jobStatus) {
194         case JOBWORKING:
195             statusInfo = description;
196             break;
197         case JOBWAITING:
198             statusInfo = i18n("Waiting to process clip");
199             break;
200         default:
201             break;
202     }
203     return statusInfo;
204 }
205
206 void MeltJob::emitFrameNumber()
207 {
208     if (m_consumer && m_length > 0) {
209         emit jobProgress(m_clipId, (int) (100 * m_consumer->position() / m_length), jobType);
210     }
211 }
212