]> git.sesse.net Git - kdenlive/commitdiff
Create new (interpolated) keyframes at the in/out points after resize
authorTill Theato <root@ttill.de>
Wed, 6 Apr 2011 16:01:29 +0000 (16:01 +0000)
committerTill Theato <root@ttill.de>
Wed, 6 Apr 2011 16:01:29 +0000 (16:01 +0000)
svn path=/trunk/kdenlive/; revision=5536

src/clipitem.cpp
src/clipitem.h

index c225c7476de8d4e1a552679223ca897536176b48..5e4cf6f6656cbf19707ff17cb229e1547009c983 100644 (file)
@@ -1814,7 +1814,7 @@ QMap<int, QDomElement> ClipItem::adjustEffectsToDuration(int width, int height,
             } else if (type == "simplekeyframe" || type == "keyframe") {
                 if (!effects.contains(i))
                     effects[i] = effect.cloneNode().toElement();
-                updateNormalKeyframes(param, previous, current, fromStart);
+                updateNormalKeyframes(param);
 #ifdef QJSON
             } else if (type == "roto-spline") {
                 if (!effects.contains(i))
@@ -1829,44 +1829,78 @@ QMap<int, QDomElement> ClipItem::adjustEffectsToDuration(int width, int height,
     return effects;
 }
 
-bool ClipItem::updateNormalKeyframes(QDomElement parameter, const int previous, const int current, bool fromStart)
+bool ClipItem::updateNormalKeyframes(QDomElement parameter)
 {
-    // TODO: add interpolated keyframes at clip in/out point
+    int in = cropStart().frames(m_fps);
+    int out = (cropStart() + cropDuration()).frames(m_fps) - 1;
 
-    bool modified = false;
+    const QStringList data = parameter.attribute("keyframes").split(';', QString::SkipEmptyParts);
+    QMap <int, double> keyframes;
+    foreach (QString keyframe, data)
+        keyframes[keyframe.section(':', 0, 0).toInt()] = keyframe.section(':', 1, 1).toDouble();
 
-    // parse keyframes and adjust values
-    const QStringList keyframes = parameter.attribute("keyframes").split(';', QString::SkipEmptyParts);
-    QMap <int, double> kfr;
-    int pos;
-    double val;
-    foreach(const QString &str, keyframes) {
-        pos = str.section(':', 0, 0).toInt();
-        val = str.section(':', 1, 1).toDouble();
-        if (pos == previous) {
-            // first or last keyframe
-            kfr[current] = val;
-            modified = true;
-        } else {
-            if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
-                // only keyframes in range
-                kfr[pos] = val;
-                modified = true;
+
+    QMap<int, double>::iterator i = keyframes.end();
+    int lastPos = -1;
+    double lastValue = 0;
+    qreal relPos;
+
+    /*
+     * Take care of resize from start
+     */
+    bool startFound = false;
+    while (i-- != keyframes.begin()) {
+        if (i.key() < in && !startFound) {
+            startFound = true;
+            if (lastPos < 0) {
+                keyframes[in] = i.value();
+            } else {
+                relPos = (in - i.key()) / (qreal)(lastPos - i.key() + 1);
+                keyframes[in] = i.value() + (lastValue - i.value()) * relPos;
             }
         }
+        lastPos = i.key();
+        lastValue = i.value();
+        if (startFound)
+            i = keyframes.erase(i);
+    }
+
+    /*
+     * Take care of resize from end
+     */
+    i = keyframes.begin();
+    lastPos = -1;
+    bool endFound = false;
+    while (i != keyframes.end()) {
+        if (i.key() > out && !endFound) {
+            endFound = true;
+            if (lastPos < 0) {
+                keyframes[out] = i.value();
+            } else {
+                relPos = (out - lastPos) / (qreal)(i.key() - lastPos + 1);
+                keyframes[out] = lastValue + (i.value() - lastValue) * relPos;
+            }
+         }
+        lastPos = i.key();
+        lastValue = i.value();
+        if (endFound)
+            i = keyframes.erase(i);
+        else
+            ++i;
     }
 
-    if (modified) {
+    if (startFound || endFound) {
         QString newkfr;
-        QMap<int, double>::const_iterator k = kfr.constBegin();
-        while (k != kfr.constEnd()) {
-            newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
+        QMap<int, double>::const_iterator k = keyframes.constBegin();
+        while (k != keyframes.constEnd()) {
+            newkfr.append(QString::number(k.key()) + ':' + QString::number(qRound(k.value())) + ';');
             ++k;
         }
         parameter.setAttribute("keyframes", newkfr);
+        return true;
     }
 
-    return modified;
+    return false;
 }
 
 void ClipItem::updateGeometryKeyframes(QDomElement effect, int paramIndex, int width, int height, int cut)
index 77708af77071dc66203ec3a5a86caaee30eb8443..fddbd007e8556c87667b317dab72bf52c540d84e 100644 (file)
@@ -168,8 +168,10 @@ public:
      * Can be used for all effects using mlt_geometry with keyframes, but at the moment Pan & Zoom is the only one. */
     QList <int> updatePanZoom(int width, int height, int cut = 0);
     void updateGeometryKeyframes(QDomElement effect, int paramIndex, int width, int height, int cut = 0);
-    bool updateNormalKeyframes(QDomElement parameter, const int previous, const int current, bool fromStart);
-    QMap<int, QDomElement> adjustEffectsToDuration(int width, int height, int previous, int current,  bool fromStart);
+    bool updateNormalKeyframes(QDomElement parameter);
+
+    /** @brief Adjusts effects after a clip duration change. */
+    QMap<int, QDomElement> adjustEffectsToDuration(int width, int height, int previous, int current, bool fromStart);
 
     /** Returns the necessary (audio, video, general) producer.
      * @param track Track of the requested producer