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