]> git.sesse.net Git - kdenlive/blobdiff - src/renderer.cpp
Speedup: only convert displayed frame to QImage if necessary (for on monitor scene...
[kdenlive] / src / renderer.cpp
index f3002a87bfcb05817038026377fb0e6a7c92f157..1ec3d2a0dd439bb92663e63e4f6ea171a87af696 100644 (file)
@@ -27,6 +27,7 @@
 #include "kdenlivesettings.h"
 #include "kthumb.h"
 #include "definitions.h"
+#include "slideshowclip.h"
 
 #include <mlt++/Mlt.h>
 
@@ -65,8 +66,9 @@ static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr
 #endif
 
     self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
-    if (frame_ptr->convert_image)
+    if (self->sendFrameForAnalysis && frame_ptr->convert_image) {
         self->emitFrameUpdated(frame);
+    }
     if (frame.get_double("_speed") == 0.0) {
         self->emitConsumerStopped();
     } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
@@ -79,6 +81,7 @@ static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr
 Render::Render(const QString & rendererName, int winid, int /* extid */, QString profile, QWidget *parent) :
         QObject(parent),
         m_isBlocked(0),
+        sendFrameForAnalysis(false),
         m_name(rendererName),
         m_mltConsumer(NULL),
         m_mltProducer(NULL),
@@ -712,13 +715,31 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, int
     Mlt::Frame *frame = producer->get_frame();
 
     if (xml.attribute("type").toInt() == SLIDESHOW) {
-        if (xml.hasAttribute("ttl")) producer->set("ttl", xml.attribute("ttl").toInt());
+        int ttl = xml.hasAttribute("ttl") ? xml.attribute("ttl").toInt() : 0;
+        if (ttl) producer->set("ttl", ttl);
+        if (!xml.attribute("animation").isEmpty()) {
+            Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "affine");
+            if (filter && filter->is_valid()) {
+                int cycle = ttl;
+                QString geometry = SlideshowClip::animationToGeometry(xml.attribute("animation"), cycle);
+                if (!geometry.isEmpty()) {
+                    if (xml.attribute("animation").contains("low-pass")) {
+                        Mlt::Filter *blur = new Mlt::Filter(*m_mltProfile, "boxblur");
+                        if (blur && blur->is_valid())
+                            producer->attach(*blur);
+                    }
+                    filter->set("transition.geometry", geometry.toUtf8().data());
+                    filter->set("transition.cycle", cycle);
+                    producer->attach(*filter);
+                }
+            }
+        }
         if (xml.attribute("fade") == "1") {
             // user wants a fade effect to slideshow
             Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "luma");
             if (filter && filter->is_valid()) {
-                if (xml.hasAttribute("ttl")) filter->set("period", xml.attribute("ttl").toInt() - 1);
-                if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("luma.out", xml.attribute("luma_duration").toInt());
+                if (ttl) filter->set("cycle", ttl);
+                if (xml.hasAttribute("luma_duration") && !xml.attribute("luma_duration").isEmpty()) filter->set("duration", xml.attribute("luma_duration").toInt());
                 if (xml.hasAttribute("luma_file") && !xml.attribute("luma_file").isEmpty()) {
                     char *tmp = decodedString(xml.attribute("luma_file"));
                     filter->set("luma.resource", tmp);
@@ -728,8 +749,15 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, int
                         filter->set("luma.softness", (double) soft / 100.0);
                     }
                 }
-                Mlt::Service clipService(producer->get_service());
-                clipService.attach(*filter);
+                producer->attach(*filter);
+            }
+        }
+        if (xml.attribute("crop") == "1") {
+            // user wants to center crop the slides
+            Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, "crop");
+            if (filter && filter->is_valid()) {
+                filter->set("center", 1);
+                producer->attach(*filter);
             }
         }
     }
@@ -1519,6 +1547,7 @@ void Render::showFrame(Mlt::Frame& frame)
 }
 #endif
 
+
 /*
  * MLT playlist direct manipulation.
  */
@@ -1781,7 +1810,7 @@ bool Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *pr
     int ct = 0;
     Mlt::Filter *filter = sourceService.filter(ct);
     while (filter) {
-        if (filter->get("kdenlive_ix") != 0) {
+        if (filter->get_int("kdenlive_ix") != 0) {
             filtersList.append(filter);
         }
         ct++;
@@ -2064,7 +2093,7 @@ void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
     int ct = 0;
     Mlt::Filter *filter = sourceService.filter(ct);
     while (filter) {
-        if (filter->get("kdenlive_ix") != 0) {
+        if (filter->get_int("kdenlive_ix") != 0) {
             sourceService.detach(*filter);
             destService.attach(*filter);
         } else ct++;
@@ -2250,7 +2279,6 @@ int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, dou
 
 bool Render::mltRemoveTrackEffect(int track, QString index, bool updateIndex)
 {
-    kDebug() << "REMOVE TK EFF: " << track << ", IX: " << index;
     Mlt::Service service(m_mltProducer->parent().get_service());
     bool success = false;
     Mlt::Tractor tractor(service);
@@ -2259,19 +2287,21 @@ bool Render::mltRemoveTrackEffect(int track, QString index, bool updateIndex)
     Mlt::Service clipService(trackPlaylist.get_service());
 
     m_isBlocked = true;
+    mlt_service_lock(service.get_service());
     int ct = 0;
     Mlt::Filter *filter = clipService.filter(ct);
     while (filter) {
-        if ((index == "-1" && strcmp(filter->get("kdenlive_id"), ""))  || filter->get("kdenlive_ix") == index) {
+        if ((index == "-1" && strcmp(filter->get("kdenlive_id"), ""))  || filter->get_int("kdenlive_ix") == index.toInt()) {
             if (clipService.detach(*filter) == 0) success = true;
         } else if (updateIndex) {
             // Adjust the other effects index
-            if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
+            if (filter->get_int("kdenlive_ix") > index.toInt()) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
             ct++;
         } else ct++;
         filter = clipService.filter(ct);
     }
     m_isBlocked = false;
+    mlt_service_unlock(service.get_service());
     refresh();
     return success;
 }
@@ -2306,20 +2336,22 @@ bool Render::mltRemoveEffect(int track, GenTime position, QString index, bool up
 
 //    if (tag.startsWith("ladspa")) tag = "ladspa";
     m_isBlocked = true;
+    mlt_service_lock(service.get_service());
     int ct = 0;
     Mlt::Filter *filter = clipService.filter(ct);
     while (filter) {
-        if ((index == "-1" && strcmp(filter->get("kdenlive_id"), ""))  || filter->get("kdenlive_ix") == index) {// && filter->get("kdenlive_id") == id) {
+        if ((index == "-1" && strcmp(filter->get("kdenlive_id"), ""))  || filter->get_int("kdenlive_ix") == index.toInt()) {// && filter->get("kdenlive_id") == id) {
             if (clipService.detach(*filter) == 0) success = true;
             //kDebug()<<"Deleted filter id:"<<filter->get("kdenlive_id")<<", ix:"<<filter->get("kdenlive_ix")<<", SERVICE:"<<filter->get("mlt_service");
         } else if (updateIndex) {
             // Adjust the other effects index
-            if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
+            if (filter->get_int("kdenlive_ix") > index.toInt()) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
             ct++;
         } else ct++;
         filter = clipService.filter(ct);
     }
     m_isBlocked = false;
+    mlt_service_unlock(service.get_service());
     if (doRefresh) refresh();
     return success;
 }
@@ -2351,7 +2383,6 @@ bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList para
     }
 
     Mlt::Service clipService(clip->get_service());
-    m_isBlocked = true;
     int duration = clip->get_playtime();
     if (doRefresh) {
         // Check if clip is visible in monitor
@@ -2368,9 +2399,12 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
     const int filter_ix = params.paramValue("kdenlive_ix").toInt();
     const QString region =  params.paramValue("region");
     int ct = 0;
+    m_isBlocked = true;
+    mlt_service_lock(service.get_service());
+
     Mlt::Filter *filter = service.filter(ct);
     while (filter) {
-        if (QString(filter->get("kdenlive_ix")).toInt() == filter_ix) {
+        if (filter->get_int("kdenlive_ix") == filter_ix) {
             // A filter at that position already existed, so we will increase all indexes later
             updateIndex = true;
             break;
@@ -2384,13 +2418,14 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
         ct = 0;
         filter = service.filter(ct);
         while (filter) {
-            if (QString(filter->get("kdenlive_ix")).toInt() >= filter_ix) {
-                if (updateIndex) filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() + 1);
+            if (filter->get_int("kdenlive_ix") >= filter_ix) {
+                if (updateIndex) filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
             }
             ct++;
             filter = service.filter(ct);
         }
         m_isBlocked = false;
+        mlt_service_unlock(service.get_service());
         if (doRefresh) refresh();
         return true;
     }
@@ -2401,7 +2436,7 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
     ct = 0;
     filter = service.filter(ct);
     while (filter) {
-        if (QString(filter->get("kdenlive_ix")).toInt() >= filter_ix) {
+        if (filter->get_int("kdenlive_ix") >= filter_ix) {
             filtersList.append(filter);
             service.detach(*filter);
         } else ct++;
@@ -2433,16 +2468,13 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
         params.removeParam("max");
         params.removeParam("factor");
         int offset = 0;
-        for (int i = 0; i < keyFrames.size() - 1; ++i) {
+        // Special case, only one keyframe, means we want a constant value
+        if (keyFrames.count() == 1) {
             Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
             if (filter && filter->is_valid()) {
                 filter->set("kdenlive_id", filterId);
-                int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
-                double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
-                int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
-                double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
-                if (x2 == -1) x2 = duration;
-
+                int x1 = keyFrames.at(0).section(':', 0, 0).toInt();
+                double y1 = keyFrames.at(0).section(':', 1, 1).toDouble();
                 for (int j = 0; j < params.count(); j++) {
                     char *name = decodedString(params.at(j).name());
                     char *value = decodedString(params.at(j).value());
@@ -2450,16 +2482,38 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
                     delete[] name;
                     delete[] value;
                 }
-
                 filter->set("in", x1);
-                filter->set("out", x2);
                 //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
                 filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
-                filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
                 service.attach(*filter);
-                offset = 1;
             }
-        }
+        } else for (int i = 0; i < keyFrames.size() - 1; ++i) {
+                Mlt::Filter *filter = new Mlt::Filter(*m_mltProfile, filterTag);
+                if (filter && filter->is_valid()) {
+                    filter->set("kdenlive_id", filterId);
+                    int x1 = keyFrames.at(i).section(':', 0, 0).toInt() + offset;
+                    double y1 = keyFrames.at(i).section(':', 1, 1).toDouble();
+                    int x2 = keyFrames.at(i + 1).section(':', 0, 0).toInt();
+                    double y2 = keyFrames.at(i + 1).section(':', 1, 1).toDouble();
+                    if (x2 == -1) x2 = duration;
+
+                    for (int j = 0; j < params.count(); j++) {
+                        char *name = decodedString(params.at(j).name());
+                        char *value = decodedString(params.at(j).value());
+                        filter->set(name, value);
+                        delete[] name;
+                        delete[] value;
+                    }
+
+                    filter->set("in", x1);
+                    filter->set("out", x2);
+                    //kDebug() << "// ADDING KEYFRAME vals: " << min<<" / "<<max<<", "<<y1<<", factor: "<<factor;
+                    filter->set(starttag, QString::number((min + y1) / factor).toUtf8().data());
+                    filter->set(endtag, QString::number((min + y2) / factor).toUtf8().data());
+                    service.attach(*filter);
+                    offset = 1;
+                }
+            }
         delete[] starttag;
         delete[] endtag;
     } else {
@@ -2485,6 +2539,7 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
         } else {
             kDebug() << "filter is NULL";
             m_isBlocked = false;
+            mlt_service_unlock(service.get_service());
             return false;
         }
         params.removeParam("kdenlive_id");
@@ -2515,6 +2570,9 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
             delete[] value;
         }
 
+        if (params.paramValue("id") == "pan_zoom") {
+            filter->set_in_and_out(service.get_int("in"), service.get_int("out") + 1);
+        }
 
         // attach filter to the clip
         service.attach(*filter);
@@ -2526,17 +2584,17 @@ bool Render::mltAddEffect(Mlt::Service service, EffectsParameterList params, int
     for (int i = 0; i < filtersList.count(); i++) {
         Mlt::Filter *filter = filtersList.at(i);
         if (updateIndex)
-            filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() + 1);
+            filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") + 1);
         service.attach(*filter);
     }
     m_isBlocked = false;
+    mlt_service_unlock(service.get_service());
     if (doRefresh) refresh();
     return true;
 }
 
 bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
 {
-    kDebug() << "EDIT TK, FILTER: " << track;
     Mlt::Service service(m_mltProducer->parent().get_service());
     Mlt::Tractor tractor(service);
     Mlt::Producer trackProducer(tractor.track(track));
@@ -2549,7 +2607,7 @@ bool Render::mltEditTrackEffect(int track, EffectsParameterList params)
 
     Mlt::Filter *filter = clipService.filter(ct);
     while (filter) {
-        if (filter->get("kdenlive_ix") == index) {
+        if (filter->get_int("kdenlive_ix") == index.toInt()) {
             break;
         }
         ct++;
@@ -2590,8 +2648,11 @@ bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList par
 
     if (!params.paramValue("keyframes").isEmpty() || /*it.key().startsWith("#") || */tag.startsWith("ladspa") || tag == "sox" || tag == "autotrack_rectangle" || params.hasParam("region")) {
         // This is a keyframe effect, to edit it, we remove it and re-add it.
-        mltRemoveEffect(track, position, index, false);
-        bool success = mltAddEffect(track, position, params);
+        bool success = mltRemoveEffect(track, position, index, false);
+        if (!success) kDebug() << "// ERROR Removing effect : " << index;
+        if (position < GenTime()) success = mltAddTrackEffect(track, params);
+        else success = mltAddEffect(track, position, params);
+        if (!success) kDebug() << "// ERROR Adding effect : " << index;
         return success;
     }
     if (position < GenTime()) {
@@ -2632,7 +2693,7 @@ bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList par
 
     Mlt::Filter *filter = clipService.filter(ct);
     while (filter) {
-        if (filter->get("kdenlive_ix") == index) {
+        if (filter->get_int("kdenlive_ix") == index.toInt()) {
             break;
         }
         ct++;
@@ -2691,7 +2752,7 @@ void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, in
     int ct = 0;
     Mlt::Filter *filter = clipService.filter(ct);
     while (filter) {
-        int pos = QString(filter->get("kdenlive_ix")).toInt();
+        int pos = filter->get_int("kdenlive_ix");
         if (pos == oldPos) {
             filter->set("kdenlive_ix", newPos);
         } else ct++;
@@ -2735,19 +2796,19 @@ void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
     bool found = false;
     if (newPos > oldPos) {
         while (filter) {
-            if (!found && QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
+            if (!found && filter->get_int("kdenlive_ix") == oldPos) {
                 filter->set("kdenlive_ix", newPos);
                 filtersList.append(filter);
                 clipService.detach(*filter);
                 filter = clipService.filter(ct);
-                while (filter && QString(filter->get("kdenlive_ix")).toInt() <= newPos) {
-                    filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
+                while (filter && filter->get_int("kdenlive_ix") <= newPos) {
+                    filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
                     ct++;
                     filter = clipService.filter(ct);
                 }
                 found = true;
             }
-            if (filter && QString(filter->get("kdenlive_ix")).toInt() > newPos) {
+            if (filter && filter->get_int("kdenlive_ix") > newPos) {
                 filtersList.append(filter);
                 clipService.detach(*filter);
             } else ct++;
@@ -2755,7 +2816,7 @@ void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
         }
     } else {
         while (filter) {
-            if (QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
+            if (filter->get_int("kdenlive_ix") == oldPos) {
                 filter->set("kdenlive_ix", newPos);
                 filtersList.append(filter);
                 clipService.detach(*filter);
@@ -2766,7 +2827,7 @@ void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
         ct = 0;
         filter = clipService.filter(ct);
         while (filter) {
-            int pos = QString(filter->get("kdenlive_ix")).toInt();
+            int pos = filter->get_int("kdenlive_ix");
             if (pos >= newPos) {
                 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
                 filtersList.append(filter);
@@ -2800,19 +2861,19 @@ void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
     bool found = false;
     if (newPos > oldPos) {
         while (filter) {
-            if (!found && QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
+            if (!found && filter->get_int("kdenlive_ix") == oldPos) {
                 filter->set("kdenlive_ix", newPos);
                 filtersList.append(filter);
                 clipService.detach(*filter);
                 filter = clipService.filter(ct);
-                while (filter && QString(filter->get("kdenlive_ix")).toInt() <= newPos) {
-                    filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() - 1);
+                while (filter && filter->get_int("kdenlive_ix") <= newPos) {
+                    filter->set("kdenlive_ix", filter->get_int("kdenlive_ix") - 1);
                     ct++;
                     filter = clipService.filter(ct);
                 }
                 found = true;
             }
-            if (filter && QString(filter->get("kdenlive_ix")).toInt() > newPos) {
+            if (filter && filter->get_int("kdenlive_ix") > newPos) {
                 filtersList.append(filter);
                 clipService.detach(*filter);
             } else ct++;
@@ -2820,7 +2881,7 @@ void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
         }
     } else {
         while (filter) {
-            if (QString(filter->get("kdenlive_ix")).toInt() == oldPos) {
+            if (filter->get_int("kdenlive_ix") == oldPos) {
                 filter->set("kdenlive_ix", newPos);
                 filtersList.append(filter);
                 clipService.detach(*filter);
@@ -2831,7 +2892,7 @@ void Render::mltMoveTrackEffect(int track, int oldPos, int newPos)
         ct = 0;
         filter = clipService.filter(ct);
         while (filter) {
-            int pos = QString(filter->get("kdenlive_ix")).toInt();
+            int pos = filter->get_int("kdenlive_ix");
             if (pos >= newPos) {
                 if (pos < oldPos) filter->set("kdenlive_ix", pos + 1);
                 filtersList.append(filter);
@@ -2877,16 +2938,17 @@ bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
     Mlt::Producer *clip = trackPlaylist.get_clip(clipIndex);
     int previousStart = clip->get_in();
     int newDuration = (int) clipDuration.frames(m_fps) - 1;
+    int currentOut = (int)(info.cropStart + info.cropDuration).frames(m_fps) - 1;
     int diff = newDuration - (trackPlaylist.clip_length(clipIndex) - 1);
-    if (newDuration > clip->get_length()) {
-        clip->parent().set("length", newDuration + 1);
-        clip->parent().set("out", newDuration);
-        clip->set("length", newDuration + 1);
+    if (currentOut > clip->get_length()) {
+        clip->parent().set("length", currentOut + 1);
+        clip->parent().set("out", currentOut);
+        clip->set("length", currentOut + 1);
     }
-    if (newDuration > clip->get_out()) {
+    /*if (newDuration > clip->get_out()) {
         clip->parent().set_in_and_out(0, newDuration + 1);
         clip->set_in_and_out(0, newDuration + 1);
-    }
+    }*/
     delete clip;
     trackPlaylist.resize_clip(clipIndex, previousStart, newDuration + previousStart);
     trackPlaylist.consolidate_blanks(0);
@@ -4022,5 +4084,16 @@ QString Render::updateSceneListFps(double current_fps, double new_fps, QString s
     return doc.toString();
 }
 
+
+void Render::sendFrameUpdate()
+{
+    if (m_mltProducer) {
+        Mlt::Frame * frame = m_mltProducer->get_frame();
+        emitFrameUpdated(*frame);
+        delete frame;
+    }
+}
+
+
 #include "renderer.moc"