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