]> git.sesse.net Git - kdenlive/blob - src/rotoscoping/rotowidget.cpp
rotoscoping: fix keyframe sorting issues after duration change
[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 <math.h>
30
31 #include <qjson/parser.h>
32 #include <qjson/serializer.h>
33
34 #include <QVBoxLayout>
35
36
37 RotoWidget::RotoWidget(QString data, Monitor *monitor, int in, int out, Timecode t, QWidget* parent) :
38         QWidget(parent),
39         m_monitor(monitor),
40         m_showScene(true),
41         m_in(in),
42         m_out(out)
43 {
44     QVBoxLayout *l = new QVBoxLayout(this);
45     m_keyframeWidget = new SimpleKeyframeWidget(t, m_out - m_in, this);
46     l->addWidget(m_keyframeWidget);
47
48     MonitorEditWidget *edit = monitor->getEffectEdit();
49     edit->showVisibilityButton(true);
50     m_scene = edit->getScene();
51
52     QJson::Parser parser;
53     bool ok;
54     m_data = parser.parse(data.toUtf8(), &ok);
55     if (!ok) {
56         // :(
57     }
58
59     
60     if (m_data.canConvert(QVariant::Map)) {
61         /*
62          * pass keyframe data to keyframe timeline
63          */
64         QList <int> keyframes;
65         QMap <QString, QVariant> map = m_data.toMap();
66         QMap <QString, QVariant>::const_iterator i = map.constBegin();
67         while (i != map.constEnd()) {
68             keyframes.append(i.key().toInt() - m_in);
69             ++i;
70         }
71         m_keyframeWidget->setKeyframes(keyframes);
72
73         for (int j = 0; j < keyframes.count(); ++j) {
74             // key might already be justified
75             if (map.contains(QString::number(keyframes.at(j) + m_in))) {
76                 QVariant value = map.take(QString::number(keyframes.at(j) + m_in));
77                 map[QString::number(keyframes.at(j) + m_in).rightJustified(log10((double)m_out) + 1, '0')] = value;
78             }
79         }
80         m_data = QVariant(map);
81     } else {
82         // static (only one keyframe)
83         m_keyframeWidget->setKeyframes(QList <int>() << 0);
84     }
85
86     m_item = new SplineItem(QList <BPoint>(), NULL, m_scene);
87
88     connect(m_item, SIGNAL(changed(bool)), this, SLOT(slotUpdateData(bool)));
89     connect(edit, SIGNAL(showEdit(bool)), this, SLOT(slotShowScene(bool)));
90     connect(m_monitor, SIGNAL(renderPosition(int)), this, SLOT(slotCheckMonitorPosition(int)));
91     connect(m_keyframeWidget, SIGNAL(positionChanged(int)), this, SLOT(slotPositionChanged(int)));
92     connect(m_keyframeWidget, SIGNAL(keyframeAdded(int)), this, SLOT(slotAddKeyframe(int)));
93     connect(m_keyframeWidget, SIGNAL(keyframeRemoved(int)), this, SLOT(slotRemoveKeyframe(int)));
94     connect(m_keyframeWidget, SIGNAL(keyframeMoved(int,int)), this, SLOT(slotMoveKeyframe(int,int)));
95     connect(m_scene, SIGNAL(addKeyframe()), this, SLOT(slotAddKeyframe()));
96
97     slotPositionChanged(0, false);
98 }
99
100 RotoWidget::~RotoWidget()
101 {
102     delete m_keyframeWidget;
103
104     m_scene->removeItem(m_item);
105     delete m_item;
106
107     if (m_monitor) {
108         MonitorEditWidget *edit = m_monitor->getEffectEdit();
109         edit->showVisibilityButton(false);
110         edit->removeCustomControls();
111         m_monitor->slotEffectScene(false);
112     }
113 }
114
115 void RotoWidget::slotCheckMonitorPosition(int renderPos)
116 {
117     if (m_showScene)
118         emit checkMonitorPosition(renderPos);
119 }
120
121 void RotoWidget::slotSyncPosition(int relTimelinePos)
122 {
123     relTimelinePos = qBound(0, relTimelinePos, m_out);
124     m_keyframeWidget->slotSetPosition(relTimelinePos, false);
125     slotPositionChanged(relTimelinePos, false);
126 }
127
128 void RotoWidget::slotShowScene(bool show)
129 {
130     m_showScene = show;
131     if (!m_showScene)
132         m_monitor->slotEffectScene(false);
133     else
134         slotCheckMonitorPosition(m_monitor->render->seekFramePosition());
135 }
136
137 void RotoWidget::slotUpdateData(int pos, bool editing)
138 {
139     Q_UNUSED(editing)
140
141     int width = m_monitor->render->frameRenderWidth();
142     int height = m_monitor->render->renderHeight();
143
144     /*
145      * use the position of the on-monitor points to create a storable list
146      */
147     QList <BPoint> spline = m_item->getPoints();
148     QList <QVariant> vlist;
149     foreach (const BPoint &point, spline) {
150         QList <QVariant> pl;
151         for (int i = 0; i < 3; ++i)
152             pl << QVariant(QList <QVariant>() << QVariant(point[i].x() / width) << QVariant(point[i].y() / height));
153         vlist << QVariant(pl);
154     }
155
156     if (m_data.canConvert(QVariant::Map)) {
157         QMap <QString, QVariant> map = m_data.toMap();
158         // replace or insert at position
159         // we have to fill with 0s to maintain the correct order
160         map[QString::number((pos < 0 ? m_keyframeWidget->getPosition() : pos) + m_in).rightJustified(log10((double)m_out) + 1, '0')] = QVariant(vlist);
161         m_data = QVariant(map);
162     } else {
163         m_data = QVariant(vlist);
164     }
165
166     emit valueChanged();
167 }
168
169 void RotoWidget::slotUpdateData(bool editing)
170 {
171     slotUpdateData(-1, editing);
172 }
173
174 QString RotoWidget::getSpline()
175 {
176     QJson::Serializer serializer;
177     return QString(serializer.serialize(m_data));
178 }
179
180 void RotoWidget::slotPositionChanged(int pos, bool seek)
181 {
182     // do not update while the spline is being edited (points are being dragged)
183     if (m_item->editing())
184         return;
185
186     m_keyframeWidget->slotSetPosition(pos, false);
187
188     pos += m_in;
189
190     QList <BPoint> p;
191
192     if (m_data.canConvert(QVariant::Map)) {
193         QMap <QString, QVariant> map = m_data.toMap();
194         QMap <QString, QVariant>::const_iterator i = map.constBegin();
195         int keyframe1, keyframe2;
196         keyframe1 = keyframe2 = i.key().toInt();
197         // find keyframes next to pos
198         while (i.key().toInt() < pos && ++i != map.constEnd()) {
199             keyframe1 = keyframe2;
200             keyframe2 = i.key().toInt();
201         }
202
203         if (keyframe1 != keyframe2 && pos < keyframe2) {
204             /*
205              * in between two keyframes
206              * -> interpolate
207              */
208             QList <BPoint> p1 = getPoints(keyframe1);
209             QList <BPoint> p2 = getPoints(keyframe2);
210             qreal relPos = (pos - keyframe1) / (qreal)(keyframe2 - keyframe1 + 1);
211
212             // additionaly points are ignored (same behavior as MLT filter)
213             int count = qMin(p1.count(), p2.count());
214             for (int i = 0; i < count; ++i) {
215                 BPoint bp;
216                 for (int j = 0; j < 3; ++j) {
217                     if (p1.at(i)[j] != p2.at(i)[j])
218                         bp[j] = QLineF(p1.at(i)[j], p2.at(i)[j]).pointAt(relPos);
219                     else
220                         bp[j] = p1.at(i)[j];
221                 }
222                 p.append(bp);
223             }
224
225             m_item->setPoints(p);
226             m_item->setEnabled(false);
227             m_scene->setEnabled(false);
228         } else {
229             p = getPoints(keyframe2);
230             // only update if necessary to preserve the current point selection
231             if (p != m_item->getPoints())
232                 m_item->setPoints(p);
233             m_item->setEnabled(pos == keyframe2);
234             m_scene->setEnabled(pos == keyframe2);
235         }
236     } else {
237         p = getPoints(-1);
238         // only update if necessary to preserve the current point selection
239         if (p != m_item->getPoints())
240             m_item->setPoints(p);
241         m_item->setEnabled(true);
242         m_scene->setEnabled(true);
243     }
244
245     if (seek)
246         emit seekToPos(pos - m_in);
247 }
248
249 QList <BPoint> RotoWidget::getPoints(int keyframe)
250 {
251     int width = m_monitor->render->frameRenderWidth();
252     int height = m_monitor->render->renderHeight();
253     QList <BPoint> points;
254     QList <QVariant> data;
255     if (keyframe >= 0)
256         data = m_data.toMap()[QString::number(keyframe).rightJustified(log10((double)m_out) + 1, '0')].toList();
257     else
258         data = m_data.toList();
259     foreach (const QVariant &bpoint, data) {
260         QList <QVariant> l = bpoint.toList();
261         BPoint p;
262         for (int i = 0; i < 3; ++i)
263             p[i] = QPointF(l.at(i).toList().at(0).toDouble() * width, l.at(i).toList().at(1).toDouble() * height);
264         points << p;
265     }
266     return points;
267 }
268
269 void RotoWidget::slotAddKeyframe(int pos)
270 {
271     if (!m_data.canConvert(QVariant::Map)) {
272         QVariant data = m_data;
273         QMap<QString, QVariant> map;
274         map[QString::number(m_in).rightJustified(log10((double)m_out) + 1, '0')] = data;
275         m_data = QVariant(map);
276     }
277
278     if (pos < 0)
279         m_keyframeWidget->addKeyframe();
280
281     slotUpdateData(pos);
282     m_item->setEnabled(true);
283     m_scene->setEnabled(true);
284 }
285
286 void RotoWidget::slotRemoveKeyframe(int pos)
287 {
288     if (pos < 0)
289         pos = m_keyframeWidget->getPosition();
290
291     if (!m_data.canConvert(QVariant::Map) || m_data.toMap().count() < 2)
292         return;
293
294     QMap<QString, QVariant> map = m_data.toMap();
295     map.remove(QString::number(pos + m_in).rightJustified(log10((double)m_out) + 1, '0'));
296     m_data = QVariant(map);
297
298     if (m_data.toMap().count() == 1) {
299         // only one keyframe -> switch from map to list again
300         m_data = m_data.toMap().begin().value();
301     }
302
303     slotPositionChanged(m_keyframeWidget->getPosition(), false);
304     emit valueChanged();
305 }
306
307 void RotoWidget::slotMoveKeyframe(int oldPos, int newPos)
308 {
309     if (m_data.canConvert(QVariant::Map)) {
310         QMap<QString, QVariant> map = m_data.toMap();
311         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'));
312         m_data = QVariant(map);
313     }
314
315     slotPositionChanged(m_keyframeWidget->getPosition(), false);
316     emit valueChanged();
317 }
318
319 void RotoWidget::updateTimecodeFormat()
320 {
321     m_keyframeWidget->updateTimecodeFormat();
322 }
323
324
325
326 static QVariant interpolate(int position, int in, int out, QVariant *splineIn, QVariant *splineOut)
327 {
328     qreal relPos = (position - in) / (qreal)(out - in + 1);
329     QList<QVariant> keyframe1 = splineIn->toList();
330     QList<QVariant> keyframe2 = splineOut->toList();
331     QList<QVariant> keyframe;
332     int max = qMin(keyframe1.count(), keyframe2.count());
333     for (int i = 0; i < max; ++i) {
334         QList<QVariant> p1 = keyframe1.at(i).toList();
335         QList<QVariant> p2 = keyframe2.at(i).toList();
336         QList<QVariant> p;
337         for (int j = 0; j < 3; ++j) {
338             QPointF middle = QLineF(QPointF(p1.at(j).toList().at(0).toDouble(), p1.at(j).toList().at(1).toDouble()),
339                                     QPointF(p2.at(j).toList().at(0).toDouble(), p2.at(j).toList().at(1).toDouble())).pointAt(relPos);
340             p.append(QVariant(QList<QVariant>() << QVariant(middle.x()) << QVariant(middle.y())));
341         }
342         keyframe.append(QVariant(p));
343     }
344     return QVariant(keyframe);
345 }
346
347 bool adjustRotoDuration(QString* data, int in, int out)
348 {
349     QJson::Parser parser;
350     bool ok;
351     QVariant splines = parser.parse(data->toUtf8(), &ok);
352     if (!ok) {
353         *data = QString();
354         return true;
355     }
356
357     if (!splines.canConvert(QVariant::Map))
358         return false;
359
360     QMap<QString, QVariant> newMap;
361     QMap<QString, QVariant> map = splines.toMap();
362     QMap<QString, QVariant>::iterator i = map.end();
363     int lastPos = -1;
364     QVariant last = QVariant();
365
366     /*
367      * Take care of resize from start
368      */
369     bool startFound = false;
370     while (i-- != map.begin()) {
371         if (!startFound && i.key().toInt() < in) {
372             startFound = true;
373             if (lastPos < 0)
374                 newMap[QString::number(in).rightJustified(log10((double)out) + 1, '0')] = i.value();
375             else
376                 newMap[QString::number(in).rightJustified(log10((double)out) + 1, '0')] = interpolate(in, i.key().toInt(), lastPos, &i.value(), &last);
377         }
378         lastPos = i.key().toInt();
379         last = i.value();
380         if (startFound)
381             i = map.erase(i);
382     }
383
384     /*
385      * Take care of resize from end
386      */
387     i = map.begin();
388     lastPos = -1;
389     bool endFound = false;
390     while (i != map.end()) {
391         if (!endFound && i.key().toInt() > out) {
392             endFound = true;
393             if (lastPos < 0)
394                 newMap[QString::number(out)] = i.value();
395             else
396                 newMap[QString::number(out)] = interpolate(out, lastPos, i.key().toInt(), &last, &i.value());
397         }
398         lastPos = i.key().toInt();
399         last = i.value();
400         if (endFound)
401             i = map.erase(i);
402         else
403             ++i;
404     }
405
406     /*
407      * Update key lengths to prevent sorting issues
408      */
409     i = map.begin();
410     while (i != map.end()) {
411         newMap[i.key().rightJustified(log10((double)out) + 1, '0', true)] = i.value();
412         ++i;
413     }
414
415     QJson::Serializer serializer;
416     *data = QString(serializer.serialize(QVariant(newMap)));
417
418     if (startFound || endFound)
419         return true;
420     return false;
421 }
422
423 #include "rotowidget.moc"