]> git.sesse.net Git - kdenlive/blob - src/projecttree/meltjob.cpp
Introduce MLT clip analysis, can now be used for getting auto normalize data in sox...
[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 <KLocale>
27
28 #include <mlt++/Mlt.h>
29
30
31 static void consumer_frame_render(mlt_consumer, MeltJob * self, mlt_frame /*frame_ptr*/)
32 {
33     // detect if the producer has finished playing. Is there a better way to do it?
34     self->emitFrameNumber();
35 }
36
37 MeltJob::MeltJob(CLIPTYPE cType, const QString &id, QStringList parameters) : AbstractClipJob(MLTJOB, cType, id, parameters), 
38     m_producer(NULL),
39     m_profile(NULL),
40     m_consumer(NULL),
41     m_length(0)
42 {
43     jobStatus = JOBWAITING;
44     m_params = parameters;
45     description = i18n("Process clip");
46 }
47
48 void MeltJob::setProducer(Mlt::Producer *producer)
49 {
50     m_producer = producer;
51 }
52
53 void MeltJob::startJob()
54 {
55     if (!m_producer) {
56         m_errorMessage.append(i18n("No producer for this clip."));
57         setStatus(JOBCRASHED);
58         return;
59     }
60     int in = m_params.takeFirst().toInt();
61     int out = m_params.takeFirst().toInt();
62     QString filter = m_params.takeFirst();
63     QString filterParams = m_params.takeFirst();
64     QString consumer = m_params.takeFirst();
65     QString consumerParams = m_params.takeFirst();
66     QString properties = m_params.takeFirst();
67     int startPos = m_params.takeFirst().toInt();
68     int track = m_params.takeFirst().toInt();
69     QString finalFilter;
70     if (!m_params.isEmpty()) finalFilter = m_params.takeFirst();
71     else finalFilter = filter;
72
73     if (out <= in) {
74         m_errorMessage.append(i18n("Clip zone undefined (%1 - %2).", in, out));
75         setStatus(JOBCRASHED);
76         return;
77     }
78     Mlt::Producer *prod = m_producer->cut(in, out);
79     m_profile = m_producer->profile();
80     m_consumer = new Mlt::Consumer(*m_profile, consumer.toUtf8().constData());
81     if (!m_consumer || !m_consumer->is_valid()) {
82         m_errorMessage.append(i18n("Cannot create consumer %1.", consumer));
83         setStatus(JOBCRASHED);
84         return;
85     }
86
87     m_consumer->set("terminate_on_pause", 1 );
88     m_consumer->set("eof", "pause" );
89     m_consumer->set("real_time", -1 );
90
91     QStringList list = consumerParams.split(' ', QString::SkipEmptyParts);
92     foreach(QString data, list) {
93         if (data.contains('=')) {
94             m_consumer->set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
95         }
96     }
97     
98     Mlt::Filter mltFilter(*m_profile, filter.toUtf8().data());
99     list = filterParams.split(' ', QString::SkipEmptyParts);
100     foreach(QString data, list) {
101         if (data.contains('=')) {
102             mltFilter.set(data.section('=', 0, 0).toUtf8().constData(), data.section('=', 1, 1).toUtf8().constData());
103         }
104     }
105     prod->attach(mltFilter);
106     m_length = prod->get_length();
107     m_consumer->connect(*prod);
108     prod->set_speed(0);
109     prod->seek(0);
110     m_showFrameEvent = m_consumer->listen("consumer-frame-show", this, (mlt_listener) consumer_frame_render);
111     m_consumer->start();
112     prod->set_speed(1);
113     while (jobStatus != JOBABORTED && !m_consumer->is_stopped()) {
114         
115     }
116     m_consumer->stop();
117     QStringList wanted = properties.split(',', QString::SkipEmptyParts);
118     stringMap jobResults;
119     foreach(const QString key, wanted) {
120         QString value = mltFilter.get(key.toUtf8().constData());
121         jobResults.insert(key, value);
122         kDebug()<<"RESULT: "<<key<<" = "<< value;
123     }
124     if (!jobResults.isEmpty()) emit gotFilterJobResults(m_clipId, startPos, track, finalFilter, jobResults);
125     setStatus(JOBDONE);
126     delete m_consumer;
127     delete prod;
128     return;
129 }
130
131
132 MeltJob::~MeltJob()
133 {
134 }
135
136 const QString MeltJob::destination() const
137 {
138     return QString();
139 }
140
141 stringMap MeltJob::cancelProperties()
142 {
143     QMap <QString, QString> props;
144     return props;
145 }
146
147 const QString MeltJob::statusMessage()
148 {
149     QString statusInfo;
150     switch (jobStatus) {
151         case JOBWORKING:
152             statusInfo = i18n("Processing clip");
153             break;
154         case JOBWAITING:
155             statusInfo = i18n("Waiting - process clip");
156             break;
157         default:
158             break;
159     }
160     return statusInfo;
161 }
162
163 void MeltJob::emitFrameNumber()
164 {
165     if (m_consumer && m_length > 0) {
166         emit jobProgress(m_clipId, (int) (100 * m_consumer->position() / m_length), jobType);
167     }
168 }