]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
Moving widget files into widgets folder.
[kdenlive] / src / rotoscoping / rotowidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Till Theato (root@ttill.de)                     *
3  *   This file is part of Kdenlive (www.kdenlive.org).                     *
4  *                                                                         *
5  *   Kdenlive 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  *   Kdenlive 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 Kdenlive.  If not, see <http://www.gnu.org/licenses/>.     *
17  ***************************************************************************/
18
19 #include "rotowidget.h"
20 #include "monitor.h"
21 #include "renderer.h"
22 #include "monitorscene.h"
23 #include "widgets/monitoreditwidget.h"
24 #include "onmonitoritems/rotoscoping/bpointitem.h"
25 #include "onmonitoritems/rotoscoping/splineitem.h"
26 #include "simplekeyframes/simplekeyframewidget.h"
27 #include "kdenlivesettings.h"
28
29 #include <mlt++/Mlt.h>
30
31 #include <math.h>
32
33 #include <qjson/parser.h>
34 #include <qjson/serializer.h>
35
36 #include <QVBoxLayout>
37
38 /** @brief Listener for "tracking-finished" event in MLT rotoscoping filter. */
39 void tracking_finished(mlt_service *owner, RotoWidget *self, char *data)
40 {
41     Q_UNUSED(owner)
42
43     if (self)
44         self->setSpline(QString(data));
45 }
46
47 RotoWidget::RotoWidget(const QString &data, Monitor *monitor, const ItemInfo &info, const Timecode &t, QWidget* parent) :
48         QWidget(parent),
49         m_monitor(monitor),
50         m_showScene(true),
51         m_in(info.cropStart.frames(KdenliveSettings::project_fps())),
52         m_out((info.cropStart + info.cropDuration).frames(KdenliveSettings::project_fps()) - 1),
53         m_filter(NULL)
54 {
55     QVBoxLayout *l = new QVBoxLayout(this);
56     m_keyframeWidget = new SimpleKeyframeWidget(t, m_out - m_in, this);
57     l->addWidget(m_keyframeWidget);
58
59     MonitorEditWidget *edit = monitor->getEffectEdit();
60     m_scene = edit->getScene();
61     m_scene->cleanup();
62
63     m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
64
65     connect(m_item, SIGNAL(changed(bool)), this, SLOT(slotUpdateData(bool)));
66     connect(m_keyframeWidget, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
67     connect(m_keyframeWidget, SIGNAL(keyframeAdded(int)), this, SLOT(slotAddKeyframe(int)));
68     connect(m_keyframeWidget, SIGNAL(keyframeRemoved(int)), this, SLOT(slotRemoveKeyframe(int)));
69     connect(m_keyframeWidget, SIGNAL(keyframeMoved(int,int)), this, SLOT(slotMoveKeyframe(int,int)));
70     connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
71
72     setSpline(data, false);
73     setupTrackingListen(info);
74     m_scene->centerView();
75 }
76
77 RotoWidget::~RotoWidget()
78 {
79     if (m_filter)
80         mlt_events_disconnect(m_filter->get_properties(), this);
81
82     delete m_keyframeWidget;
83
84     m_scene->removeItem(m_item);
85     delete m_item;
86
87     if (m_monitor) {
88         MonitorEditWidget *edit = m_monitor->getEffectEdit();
89         edit->removeCustomControls();
90         m_monitor->slotShowEffectScene(false);
91     }
92 }
93
94 void RotoWidget::slotSyncPosition(int relTimelinePos)
95 {
96     relTimelinePos = qBound(0, relTimelinePos, m_out);
97     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
98     slotPositionChanged(relTimelinePos, false);
99 }
100
101 void RotoWidget::slotUpdateData(int pos, bool editing)
102 {
103     Q_UNUSED(editing)
104
105     int width = m_monitor->render->frameRenderWidth();
106     int height = m_monitor->render->renderHeight();
107
108     /*
109      * use the position of the on-monitor points to create a storable list
110      */
111     QList <BPoint> spline = m_item->getPoints();
112     QList <QVariant> vlist;
113     foreach (const BPoint &point, spline) {
114         QList <QVariant> pl;
115         for (int i = 0; i < 3; ++i)
116             pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
117         vlist << QVariant(pl);
118     }
119
120     if (m_data.canConvert(QVariant::Map)) {
121         QMap <QString, QVariant> map = m_data.toMap();
122         // replace or insert at position
123         // we have to fill with 0s to maintain the correct order
124         map[QString::number((pos < 0 ? m_keyframeWidget->getPosition() : pos) + m_in).rightJustified(log10((double)m_out) + 1, '0')] = QVariant(vlist);
125         m_data = QVariant(map);
126     } else {
127         // timeline update is only required if the first keyframe did not exist yet
128         bool update = m_data.isNull();
129         m_data = QVariant(vlist);
130         if (update) {
131             keyframeTimelineFullUpdate();
132         }
133     }
134
135     emit valueChanged();
136 }
137
138 void RotoWidget::slotUpdateData(bool editing)
139 {
140     slotUpdateData(-1, editing);
141 }
142
143 QString RotoWidget::getSpline()
144 {
145     QJson::Serializer serializer;
146     return QString(serializer.serialize(m_data));
147 }
148
149 void RotoWidget::slotPositionChanged(int pos, bool seek)
150 {
151     // do not update while the spline is being edited (points are being dragged)
152     if (m_item->editing())
153         return;
154
155     m_keyframeWidget->slotSetPosition(pos, false);
156
157     pos += m_in;
158
159     QList <BPoint> p;
160
161     if (m_data.canConvert(QVariant::Map)) {
162         QMap <QString, QVariant> map = m_data.toMap();
163         QMap <QString, QVariant>::const_iterator i = map.constBegin();
164         int keyframe1, keyframe2;
165         keyframe1 = keyframe2 = i.key().toInt();
166         // find keyframes next to pos
167         while (i.key().toInt() < pos && ++i != map.constEnd()) {
168             keyframe1 = keyframe2;
169             keyframe2 = i.key().toInt();
170         }
171
172         if (keyframe1 != keyframe2 && pos < keyframe2) {
173             /*
174              * in between two keyframes
175              * -> interpolate
176              */
177             QList <BPoint> p1 = getPoints(keyframe1);
178             QList <BPoint> p2 = getPoints(keyframe2);
179             qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
180
181             // additionaly points are ignored (same behavior as MLT filter)
182             int count = qMin(p1.count(), p2.count());
183             for (int i = 0; i < count; ++i) {
184                 BPoint bp;
185                 for (int j = 0; j < 3; ++j) {
186                     if (p1.at(i)[j] != p2.at(i)[j])
187                         bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
188                     else
189                         bp[j] = p1.at(i)[j];
190                 }
191                 p.append(bp);
192             }
193
194             m_item->setPoints(p);
195             m_item->setEnabled(false);
196             m_scene->setEnabled(false);
197         } else {
198             p = getPoints(keyframe2);
199             // only update if necessary to preserve the current point selection
200             if (p != m_item->getPoints())
201                 m_item->setPoints(p);
202             m_item->setEnabled(pos == keyframe2);
203             m_scene->setEnabled(pos == keyframe2);
204         }
205     } else {
206         p = getPoints(-1);
207         // only update if necessary to preserve the current point selection
208         if (p != m_item->getPoints())
209             m_item->setPoints(p);
210         m_item->setEnabled(true);
211         m_scene->setEnabled(true);
212     }
213
214     if (seek)
215         emit seekToPos(pos - m_in);
216 }
217
218 QList <BPoint> RotoWidget::getPoints(int keyframe)
219 {
220     int width = m_monitor->render->frameRenderWidth();
221     int height = m_monitor->render->renderHeight();
222     QList <BPoint> points;
223     QList <QVariant> data;
224     if (keyframe >= 0)
225         data = m_data.toMap()[QString::number(keyframe).rightJustified(log10((double)m_out) + 1, '0')].toList();
226     else
227         data = m_data.toList();
228
229     // skip tracking flag
230     if (data.count() && data.at(0).canConvert(QVariant::String))
231         data.removeFirst();
232
233     foreach (const QVariant &bpoint, data) {
234         QList <QVariant> l = bpoint.toList();
235         BPoint p;
236         for (int i = 0; i < 3; ++i)
237             p[i] = QPointF(l.at(i).toList().at(0).toDouble() * width, l.at(i).toList().at(1).toDouble() * height);
238         points << p;
239     }
240     return points;
241 }
242
243 void RotoWidget::slotAddKeyframe(int pos)
244 {
245     if (!m_data.canConvert(QVariant::Map)) {
246         QVariant data = m_data;
247         QMap<QString, QVariant> map;
248         map[QString::number(m_in).rightJustified(log10((double)m_out) + 1, '0')] = data;
249         m_data = QVariant(map);
250     }
251
252     if (pos < 0)
253         m_keyframeWidget->addKeyframe();
254
255     slotUpdateData(pos);
256     m_item->setEnabled(true);
257     m_scene->setEnabled(true);
258 }
259
260 void RotoWidget::slotRemoveKeyframe(int pos)
261 {
262     if (pos < 0)
263         pos = m_keyframeWidget->getPosition();
264
265     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
266         return;
267
268     QMap<QString, QVariant> map = m_data.toMap();
269     map.remove(QString::number(pos + m_in).rightJustified(log10((double)m_out) + 1, '0'));
270     m_data = QVariant(map);
271
272     if (m_data.toMap().count() == 1) {
273         // only one keyframe -> switch from map to list again
274         m_data = m_data.toMap().begin().value();
275     }
276
277     slotPositionChanged(m_keyframeWidget->getPosition(), false);
278     emit valueChanged();
279 }
280
281 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
282 {
283     if (m_data.canConvert(QVariant::Map)) {
284         QMap<QString, QVariant> map = m_data.toMap();
285         map[QString::number(newPos + m_in).rightJustified(log10((double)m_out) + 1, '0')] = map.take(QString::number(oldPos + m_in).rightJustified(log10((double)m_out) + 1, '0'));
286         m_data = QVariant(map);
287     }
288
289     slotPositionChanged(m_keyframeWidget->getPosition(), false);
290     emit valueChanged();
291 }
292
293 void RotoWidget::updateTimecodeFormat()
294 {
295     m_keyframeWidget->updateTimecodeFormat();
296 }
297
298 void RotoWidget::keyframeTimelineFullUpdate()
299 {
300     if (m_data.canConvert(QVariant::Map)) {
301         QList <int> keyframes;
302         QMap <QString, QVariant> map = m_data.toMap();
303         QMap <QString, QVariant>::const_iterator i = map.constBegin();
304         while (i != map.constEnd()) {
305             keyframes.append(i.key().toInt() - m_in);
306             ++i;
307         }
308         m_keyframeWidget->setKeyframes(keyframes);
309
310         /*for (int j = 0; j < keyframes.count(); ++j) {
311             // key might already be justified
312             if (map.contains(QString::number(keyframes.at(j) + m_in))) {
313                 QVariant value = map.take(QString::number(keyframes.at(j) + m_in));
314                 map[QString::number(keyframes.at(j) + m_in).rightJustified(log10((double)m_out) + 1, '0')] = value;
315             }
316         }
317         m_data = QVariant(map);*/
318     } else {
319         // static (only one keyframe)
320         // make sure the first keyframe was already created
321         if (m_data.isValid()) {
322             m_keyframeWidget->setKeyframes(QList <int>() << 0);
323         }
324     }
325 }
326
327 void RotoWidget::setupTrackingListen(const ItemInfo &info)
328 {
329     if (info.startPos < GenTime()) {
330         // TODO: track effects
331         return;
332     }
333
334     Mlt::Service service(m_monitor->render->getProducer()->parent().get_service());
335     Mlt::Tractor tractor(service);
336     Mlt::Producer trackProducer(tractor.track(tractor.count() - info.track - 1));
337     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
338
339     Mlt::Producer *clip = trackPlaylist.get_clip_at((int)info.startPos.frames(KdenliveSettings::project_fps()));
340     if (!clip) {
341         return;
342     }
343
344     int i = 0;
345     Mlt::Filter *filter = clip->filter(0);
346     while (filter) {
347         if (strcmp(filter->get("kdenlive_id"), "rotoscoping") == 0) {
348             m_filter = filter;
349             filter->listen("tracking-finished", this, (mlt_listener)tracking_finished);
350             break;
351         }
352         filter = clip->filter(++i);
353     }
354
355     delete clip;
356 }
357
358 void RotoWidget::setSpline(const QString &spline, bool notify)
359 {
360     QJson::Parser parser;
361     bool ok;
362     m_data = parser.parse(spline.simplified().toUtf8(), &ok);
363     if (!ok) {
364         // :(
365     }
366     keyframeTimelineFullUpdate();
367     slotPositionChanged(m_keyframeWidget->getPosition(), false);
368     if (notify)
369         emit valueChanged();
370 }
371
372
373 static QVariant interpolate(int position, int in, int out, QVariant *splineIn, QVariant *splineOut)
374 {
375     qreal relPos = (position - in) / (qreal)(out - in + 1);
376     QList<QVariant> keyframe1 = splineIn->toList();
377     QList<QVariant> keyframe2 = splineOut->toList();
378     QList<QVariant> keyframe;
379     if (keyframe1.count() && keyframe1.at(0).canConvert(QVariant::String))
380         keyframe1.removeFirst();
381     if (keyframe2.count() && keyframe2.at(0).canConvert(QVariant::String))
382         keyframe2.removeFirst();
383     int max = qMin(keyframe1.count(), keyframe2.count());
384         
385     for (int i = 0; i < max; ++i) {
386         QList<QVariant> p1 = keyframe1.at(i).toList();
387         QList<QVariant> p2 = keyframe2.at(i).toList();
388         QList<QVariant> p;
389         for (int j = 0; j < 3; ++j) {
390             QPointF middle = QLineF(QPointF(p1.at(j).toList().at(0).toDouble(), p1.at(j).toList().at(1).toDouble()),
391                                     QPointF(p2.at(j).toList().at(0).toDouble(), p2.at(j).toList().at(1).toDouble())).pointAt(relPos);
392             p.append(QVariant(QList<QVariant>() << QVariant(middle.x()) << QVariant(middle.y())));
393         }
394         keyframe.append(QVariant(p));
395     }
396     return QVariant(keyframe);
397 }
398
399 bool adjustRotoDuration(QString* data, int in, int out)
400 {
401     QJson::Parser parser;
402     bool ok;
403     QVariant splines = parser.parse(data->toUtf8(), &ok);
404     if (!ok) {
405         *data = QString();
406         return true;
407     }
408
409     if (!splines.canConvert(QVariant::Map))
410         return false;
411
412     QMap<QString, QVariant> newMap;
413     QMap<QString, QVariant> map = splines.toMap();
414     QMap<QString, QVariant>::iterator i = map.end();
415     int lastPos = -1;
416     QVariant last = QVariant();
417
418     /*
419      * Take care of resize from start
420      */
421     bool startFound = false;
422     while (i-- != map.begin()) {
423         if (!startFound && i.key().toInt() < in) {
424             startFound = true;
425             if (lastPos < 0)
426                 newMap[QString::number(in).rightJustified(log10((double)out) + 1, '0')] = i.value();
427             else
428                 newMap[QString::number(in).rightJustified(log10((double)out) + 1, '0')] = interpolate(in, i.key().toInt(), lastPos, &i.value(), &last);
429         }
430         lastPos = i.key().toInt();
431         last = i.value();
432         if (startFound)
433             i = map.erase(i);
434     }
435
436     /*
437      * Take care of resize from end
438      */
439     i = map.begin();
440     lastPos = -1;
441     bool endFound = false;
442     while (i != map.end()) {
443         if (!endFound && i.key().toInt() > out) {
444             endFound = true;
445             if (lastPos < 0)
446                 newMap[QString::number(out)] = i.value();
447             else
448                 newMap[QString::number(out)] = interpolate(out, lastPos, i.key().toInt(), &last, &i.value());
449         }
450         lastPos = i.key().toInt();
451         last = i.value();
452         if (endFound)
453             i = map.erase(i);
454         else
455             ++i;
456     }
457
458     /*
459      * Update key lengths to prevent sorting issues
460      */
461     i = map.begin();
462     while (i != map.end()) {
463         newMap[i.key().rightJustified(log10((double)out) + 1, '0', true)] = i.value();
464         ++i;
465     }
466
467     QJson::Serializer serializer;
468     *data = QString(serializer.serialize(QVariant(newMap)));
469
470     if (startFound || endFound)
471         return true;
472     return false;
473 }
474
475 #include "rotowidget.moc"