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