]> git.sesse.net Git - kdenlive/blobdiff - src/renderer.cpp
* Cleanup monitor / MLT communication
[kdenlive] / src / renderer.cpp
index d38d63f561901970aabaeb98c21339a02fc40f89..8ca08514e914f847d2d8f7558d0831deff5ac1dc 100644 (file)
 
 #include <QTimer>
 #include <QDir>
+#include <QString>
 #include <QApplication>
 
-#include <stdlib.h>
+#include <cstdlib>
+#include <cstdarg>
 
 
 
@@ -49,6 +51,7 @@ static void kdenlive_callback(void* /*ptr*/, int level, const char* fmt, va_list
     if (level > MLT_LOG_ERROR) return;
     QString error;
     QApplication::postEvent(qApp->activeWindow() , new MltErrorEvent(error.vsprintf(fmt, vl).simplified()));
+    va_end(vl);
 }
 
 
@@ -60,14 +63,18 @@ static void consumer_frame_show(mlt_consumer, Render * self, mlt_frame frame_ptr
 #ifdef Q_WS_MAC
     self->showFrame(frame);
 #endif
+
+    self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
     if (frame.get_double("_speed") == 0.0) {
         self->emitConsumerStopped();
-    } else {
-        self->emitFrameNumber(mlt_frame_get_position(frame_ptr));
+    } else if (frame.get_double("_speed") < 0.0 && mlt_frame_get_position(frame_ptr) <= 0) {
+        self->pause();
+        self->emitConsumerStopped();
     }
 }
 
-Render::Render(const QString & rendererName, int winid, int /* extid */, QWidget *parent) :
+
+Render::Render(const QString & rendererName, int winid, int /* extid */, QString profile, QWidget *parent) :
         QObject(parent),
         m_isBlocked(0),
         m_name(rendererName),
@@ -84,14 +91,12 @@ Render::Render(const QString & rendererName, int winid, int /* extid */, QWidget
         , m_glWidget(0)
 #endif
 {
-    kDebug() << "//////////  USING PROFILE: " << (char*)KdenliveSettings::current_profile().toUtf8().data();
-
     /*if (rendererName == "project") m_monitorId = 10000;
     else m_monitorId = 10001;*/
     /*m_osdTimer = new QTimer(this);
     connect(m_osdTimer, SIGNAL(timeout()), this, SLOT(slotOsdTimeout()));*/
-
-    buildConsumer();
+    if (profile.isEmpty()) profile = KdenliveSettings::current_profile();
+    buildConsumer(profile);
 
     m_mltProducer = m_blackClip->cut(0, 50);
     m_mltConsumer->connect(*m_mltProducer);
@@ -150,11 +155,15 @@ void Render::closeMlt()
     //delete m_osdInfo;
 }
 
+void Render::slotSwitchFullscreen()
+{
+    if (m_mltConsumer) m_mltConsumer->set("full_screen", 1);
+}
 
-void Render::buildConsumer()
+void Render::buildConsumer(const QString profileName)
 {
     char *tmp;
-    m_activeProfile = KdenliveSettings::current_profile();
+    m_activeProfile = profileName;
     tmp = decodedString(m_activeProfile);
     setenv("MLT_PROFILE", tmp, 1);
     delete m_blackClip;
@@ -176,6 +185,7 @@ void Render::buildConsumer()
     }
     setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 1);
 
+    //m_mltConsumer->set("fullscreen", 1);
 #ifdef Q_WS_MAC
     m_mltConsumer = new Mlt::Consumer(*m_mltProfile , "sdl_audio");
     m_mltConsumer->set("preview_off", 1);
@@ -229,22 +239,27 @@ void Render::buildConsumer()
 
     m_blackClip = new Mlt::Producer(*m_mltProfile , "colour", "black");
     m_blackClip->set("id", "black");
+    m_blackClip->set("mlt_type", "producer");
 
 }
 
-int Render::resetProfile()
+int Render::resetProfile(const QString profileName)
 {
-    if (!m_mltConsumer) return 0;
-    if (m_activeProfile == KdenliveSettings::current_profile()) {
-        kDebug() << "reset to same profile, nothing to do";
-        return 1;
+    if (m_mltConsumer) {
+        QString videoDriver = KdenliveSettings::videodrivername();
+        QString currentDriver = m_mltConsumer->get("video_driver");
+        if (getenv("SDL_VIDEO_YUV_HWACCEL") != NULL && currentDriver == "x11") currentDriver = "x11_noaccel";
+        if (m_activeProfile == profileName && currentDriver == videoDriver) {
+            kDebug() << "reset to same profile, nothing to do";
+            return 1;
+        }
+
+        if (m_isSplitView) slotSplitView(false);
+        if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
+        m_mltConsumer->purge();
+        delete m_mltConsumer;
+        m_mltConsumer = NULL;
     }
-    kDebug() << "// RESETTING PROFILE FROM: " << m_activeProfile << " TO: " << KdenliveSettings::current_profile();
-    if (m_isSplitView) slotSplitView(false);
-    if (!m_mltConsumer->is_stopped()) m_mltConsumer->stop();
-    m_mltConsumer->purge();
-    delete m_mltConsumer;
-    m_mltConsumer = NULL;
     QString scene = sceneList();
     int pos = 0;
     double current_fps = m_mltProfile->fps();
@@ -270,7 +285,7 @@ int Render::resetProfile()
     }
     m_mltProducer = NULL;
 
-    buildConsumer();
+    buildConsumer(profileName);
     double new_fps = m_mltProfile->fps();
     if (current_fps != new_fps) {
         // fps changed, we must update the scenelist positions
@@ -576,7 +591,7 @@ void Render::slotSplitView(bool doit)
     }
 }
 
-void Render::getFileProperties(const QDomElement xml, const QString &clipId, bool replaceProducer)
+void Render::getFileProperties(const QDomElement xml, const QString &clipId, int imageHeight, bool replaceProducer)
 {
     KUrl url = KUrl(xml.attribute("resource", QString()));
     Mlt::Producer *producer = NULL;
@@ -625,6 +640,17 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, boo
         double aspect = xml.attribute("force_aspect_ratio").toDouble();
         if (aspect > 0) producer->set("force_aspect_ratio", aspect);
     }
+
+    if (xml.hasAttribute("force_fps")) {
+        double fps = xml.attribute("force_fps").toDouble();
+        if (fps > 0) producer->set("force_fps", fps);
+    }
+
+    if (xml.hasAttribute("force_progressive")) {
+        bool ok;
+        int progressive = xml.attribute("force_progressive").toInt(&ok);
+        if (ok) producer->set("force_progressive", progressive);
+    }
     if (xml.hasAttribute("threads")) {
         int threads = xml.attribute("threads").toInt();
         if (threads != 1) producer->set("threads", threads);
@@ -656,8 +682,7 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, boo
         return;
     }
 
-    int height = 50;
-    int width = (int)(height  * m_mltProfile->dar());
+    int width = (int)(imageHeight  * m_mltProfile->dar());
     QMap < QString, QString > filePropertyMap;
     QMap < QString, QString > metadataPropertyMap;
 
@@ -712,8 +737,8 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, boo
 
             mlt_image_format format = mlt_image_rgb24a;
             int frame_width = width;
-            int frame_height = height;
-            QPixmap pix(width, height);
+            int frame_height = imageHeight;
+            QPixmap pix(width, imageHeight);
             uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
             QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32);
 
@@ -725,7 +750,7 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, boo
             emit replyGetImage(clipId, pix);
 
         } else if (frame->get_int("test_audio") == 0) {
-            QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, height));
+            QPixmap pixmap = KIcon("audio-x-generic").pixmap(QSize(width, imageHeight));
             emit replyGetImage(clipId, pixmap);
             filePropertyMap["type"] = "audio";
         }
@@ -807,7 +832,7 @@ void Render::getFileProperties(const QDomElement xml, const QString &clipId, boo
         if (name.endsWith("markup") && !value.isEmpty())
             metadataPropertyMap[ name.section('.', 0, -2)] = value;
     }
-
+    producer->seek(0);
     emit replyGetFileProperties(clipId, producer, filePropertyMap, metadataPropertyMap, replaceProducer);
     kDebug() << "REquested fuile info for: " << url.path();
     delete frame;
@@ -888,6 +913,7 @@ int Render::setProducer(Mlt::Producer *producer, int position)
 
     m_fps = m_mltProducer->get_fps();
     int error = connectPlaylist();
+
     if (position != -1) {
         m_mltProducer->seek(position);
         emit rendererPosition(position);
@@ -909,21 +935,20 @@ int Render::setSceneList(QString playlist, int position)
 {
     if (m_winid == -1) return -1;
     m_isBlocked = true;
-    int error;
+    int error = 0;
 
-    //kWarning() << "//////  RENDER, SET SCENE LIST: " << playlist;
+    kDebug() << "//////  RENDER, SET SCENE LIST: " << playlist;
 
-    if (m_mltConsumer == NULL) {
+    if (m_mltConsumer) {
+        if (!m_mltConsumer->is_stopped()) {
+            m_mltConsumer->stop();
+        }
+        m_mltConsumer->set("refresh", 0);
+    } else {
         kWarning() << "///////  ERROR, TRYING TO USE NULL MLT CONSUMER";
-        m_isBlocked = false;
-        return -1;
+        error = -1;
     }
 
-    if (!m_mltConsumer->is_stopped()) {
-        m_mltConsumer->stop();
-    }
-    m_mltConsumer->set("refresh", 0);
-
     if (m_mltProducer) {
         m_mltProducer->set_speed(0);
         //if (KdenliveSettings::osdtimecode() && m_osdInfo) m_mltProducer->detach(*m_osdInfo);
@@ -958,6 +983,7 @@ int Render::setSceneList(QString playlist, int position)
                 if (trackPlaylist.type() == playlist_type) trackPlaylist.clear();
                 trackNb--;
             }
+            delete field;
         }
         mlt_service_unlock(service.get_service());
 
@@ -976,6 +1002,7 @@ int Render::setSceneList(QString playlist, int position)
     if (!m_mltProducer || !m_mltProducer->is_valid()) {
         kDebug() << " WARNING - - - - -INVALID PLAYLIST: " << tmp;
         m_mltProducer = m_blackClip->cut(0, 50);
+        error = -1;
     }
     delete[] tmp;
 
@@ -1011,7 +1038,8 @@ int Render::setSceneList(QString playlist, int position)
     }
 
     kDebug() << "// NEW SCENE LIST DURATION SET TO: " << m_mltProducer->get_playtime();
-    error = connectPlaylist();
+    if (error == 0) error = connectPlaylist();
+    else connectPlaylist();
     fillSlowMotionProducers();
 
     m_isBlocked = false;
@@ -1112,9 +1140,8 @@ int Render::connectPlaylist()
     if (m_mltConsumer->start() == -1) {
         // ARGH CONSUMER BROKEN!!!!
         KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
-        emit blockMonitors();
-        delete m_mltProducer;
-        m_mltProducer = NULL;
+        delete m_mltConsumer;
+        m_mltConsumer = NULL;
         return -1;
     }
     emit durationChanged(m_mltProducer->get_playtime());
@@ -1181,10 +1208,8 @@ void Render::start()
     if (m_mltConsumer && m_mltConsumer->is_stopped()) {
         kDebug() << "-----  MONITOR: " << m_name << " WAS STOPPED";
         if (m_mltConsumer->start() == -1) {
-            KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
-            emit blockMonitors();
-            delete m_mltProducer;
-            m_mltProducer = NULL;
+            //KMessageBox::error(qApp->activeWindow(), i18n("Could not create the video preview window.\nThere is something wrong with your Kdenlive install or your driver settings, please fix it."));
+            kdWarning() << "/ / / / CANNOT START MONITOR";
         } else {
             kDebug() << "-----  MONITOR: " << m_name << " REFRESH";
             m_isBlocked = false;
@@ -1238,8 +1263,10 @@ void Render::pause()
     m_isBlocked = true;
     m_mltConsumer->set("refresh", 0);
     m_mltProducer->set_speed(0.0);
+    /*
+    The 2 lines below create a flicker loop
     emit rendererPosition(m_framePosition);
-    m_mltProducer->seek(m_framePosition);
+    m_mltProducer->seek(m_framePosition);*/
     m_mltConsumer->purge();
 }
 
@@ -1256,7 +1283,7 @@ void Render::switchPlay()
         m_isBlocked = true;
         m_mltConsumer->set("refresh", 0);
         m_mltProducer->set_speed(0.0);
-        emit rendererPosition(m_framePosition);
+        //emit rendererPosition(m_framePosition);
         m_mltProducer->seek(m_framePosition);
         m_mltConsumer->purge();
         //kDebug()<<" *********  RENDER PAUSE: "<<m_mltProducer->get_speed();
@@ -1379,9 +1406,7 @@ void Render::setDropFrames(bool show)
         m_mltConsumer->set("play.real_time", dropFrames);
 #endif
         if (m_mltConsumer->start() == -1) {
-            emit blockMonitors();
-            delete m_mltProducer;
-            m_mltProducer = NULL;
+            kdWarning() << "ERROR, Cannot start monitor";
         }
 
     }
@@ -1498,7 +1523,7 @@ void Render::mltCheckLength(Mlt::Tractor *tractor)
     while (trackNb > 1) {
         Mlt::Producer trackProducer(tractor->track(trackNb - 1));
         trackDuration = trackProducer.get_playtime() - 1;
-        //kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
+        // kDebug() << " / / /DURATON FOR TRACK " << trackNb - 1 << " = " << trackDuration;
         if (trackDuration > duration) duration = trackDuration;
         trackNb--;
     }
@@ -1515,17 +1540,17 @@ void Render::mltCheckLength(Mlt::Tractor *tractor)
 
         if (blackclip == NULL || blackTrackPlaylist.count() != 1) {
             blackTrackPlaylist.clear();
-            m_blackClip->set("length", duration);
-            m_blackClip->set("out", duration - 1);
-            blackclip = m_blackClip->cut(0, duration - 1);
+            m_blackClip->set("length", duration + 1);
+            m_blackClip->set("out", duration);
+            blackclip = m_blackClip->cut(0, duration);
             blackTrackPlaylist.insert_at(0, blackclip, 1);
         } else {
             if (duration > blackclip->parent().get_length()) {
-                blackclip->parent().set("length", duration);
-                blackclip->parent().set("out", duration - 1);
-                blackclip->set("length", duration);
+                blackclip->parent().set("length", duration + 1);
+                blackclip->parent().set("out", duration);
+                blackclip->set("length", duration + 1);
             }
-            blackTrackPlaylist.resize_clip(0, 0, duration - 1);
+            blackTrackPlaylist.resize_clip(0, 0, duration);
         }
 
         delete blackclip;
@@ -1534,12 +1559,16 @@ void Render::mltCheckLength(Mlt::Tractor *tractor)
     }
 }
 
-int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
+int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *prod, bool overwrite, bool push)
 {
     if (m_mltProducer == NULL) {
         kDebug() << "PLAYLIST NOT INITIALISED //////";
         return -1;
     }
+    if (prod == NULL) {
+        kDebug() << "Cannot insert clip without producer //////";
+        return -1;
+    }
     Mlt::Producer parentProd(m_mltProducer->parent());
     if (parentProd.get_producer() == NULL) {
         kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
@@ -1558,6 +1587,7 @@ int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *pro
     }
     mlt_service_lock(service.get_service());
     Mlt::Producer trackProducer(tractor.track(info.track));
+    int trackDuration = trackProducer.get_playtime() - 1;
     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
     //kDebug()<<"/// INSERT cLIP: "<<info.cropStart.frames(m_fps)<<", "<<info.startPos.frames(m_fps)<<"-"<<info.endPos.frames(m_fps);
 
@@ -1584,10 +1614,27 @@ int Render::mltInsertClip(ItemInfo info, QDomElement element, Mlt::Producer *pro
             m_slowmotionProducers.insert(url, slowprod);
         }
         prod = slowprod;
+        if (prod == NULL || !prod->is_valid()) {
+            mlt_service_unlock(service.get_service());
+            return -1;
+        }
     }
-
-    Mlt::Producer *clip = prod->cut((int) info.cropStart.frames(m_fps), (int)(info.endPos - info.startPos + info.cropStart).frames(m_fps) - 1);
-    int newIndex = trackPlaylist.insert_at((int) info.startPos.frames(m_fps), clip, 1);
+    int cutPos = (int) info.cropStart.frames(m_fps);
+    int insertPos = (int) info.startPos.frames(m_fps);
+    int cutDuration = (int)(info.endPos - info.startPos).frames(m_fps) - 1;
+    Mlt::Producer *clip = prod->cut(cutPos, cutDuration + cutPos);
+    if (overwrite && (insertPos < trackDuration)) {
+        // Replace zone with blanks
+        //trackPlaylist.split_at(insertPos, true);
+        trackPlaylist.remove_region(insertPos, cutDuration + 1);
+        int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
+        trackPlaylist.insert_blank(clipIndex, cutDuration);
+    } else if (push) {
+        trackPlaylist.split_at(insertPos, true);
+        int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
+        trackPlaylist.insert_blank(clipIndex, cutDuration);
+    }
+    int newIndex = trackPlaylist.insert_at(insertPos, clip, 1);
     delete clip;
     /*if (QString(prod->get("transparency")).toInt() == 1)
         mltAddClipTransparency(info, info.track - 1, QString(prod->get("id")).toInt());*/
@@ -1683,13 +1730,17 @@ void Render::mltCutClip(int track, GenTime position)
     m_isBlocked = false;
 }
 
-void Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
+bool Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *prod)
 {
     // TODO: optimize
+    if (prod == NULL) {
+        kDebug() << "Cannot update clip with null producer //////";
+        return false;
+    }
     Mlt::Service service(m_mltProducer->parent().get_service());
     if (service.type() != tractor_type) {
         kWarning() << "// TRACTOR PROBLEM";
-        return;
+        return false;
     }
     Mlt::Tractor tractor(service);
     Mlt::Producer trackProducer(tractor.track(info.track));
@@ -1709,9 +1760,9 @@ void Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *pr
         }
         ct++;
         filter = sourceService.filter(ct);
-    }    
-    mltRemoveClip(info.track, info.startPos);
-    mltInsertClip(info, element, prod);
+    }
+    if (!mltRemoveClip(info.track, info.startPos)) return false;
+    if (mltInsertClip(info, element, prod) == -1) return false;
     if (!filtersList.isEmpty()) {
         clipIndex = trackPlaylist.get_clip_index_at(startPos);
         Mlt::Producer *destclip = trackPlaylist.get_clip(clipIndex);
@@ -1720,6 +1771,7 @@ void Render::mltUpdateClip(ItemInfo info, QDomElement element, Mlt::Producer *pr
         for (int i = 0; i < filtersList.count(); i++)
             destService.attach(*(filtersList.at(i)));
     }
+    return true;
 }
 
 
@@ -1778,12 +1830,12 @@ int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
 {
     if (!m_mltProducer) {
         kDebug() << "PLAYLIST NOT INITIALISED //////";
-        return -1;
+        return 0;
     }
     Mlt::Producer parentProd(m_mltProducer->parent());
     if (parentProd.get_producer() == NULL) {
         kDebug() << "PLAYLIST BROKEN, CANNOT INSERT CLIP //////";
-        return -1;
+        return 0;
     }
 
     Mlt::Service service(parentProd.get_service());
@@ -1793,7 +1845,11 @@ int Render::mltGetSpaceLength(const GenTime pos, int track, bool fromBlankStart)
     Mlt::Producer trackProducer(tractor.track(track));
     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
     int clipIndex = trackPlaylist.get_clip_index_at(insertPos);
-    if (!trackPlaylist.is_blank(clipIndex)) return -1;
+    if (clipIndex == trackPlaylist.count()) {
+        // We are after the end of the playlist
+        return -1;
+    }
+    if (!trackPlaylist.is_blank(clipIndex)) return 0;
     if (fromBlankStart) return trackPlaylist.clip_length(clipIndex);
     return trackPlaylist.clip_length(clipIndex) + trackPlaylist.clip_start(clipIndex) - insertPos;
 }
@@ -1979,7 +2035,7 @@ void Render::mltPasteEffects(Mlt::Producer *source, Mlt::Producer *dest)
     }
 }
 
-int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, int strobe, Mlt::Producer *prod)
+int Render::mltChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, double speed, double oldspeed, int strobe, Mlt::Producer *prod)
 {
     m_isBlocked = true;
     int newLength = 0;
@@ -2011,7 +2067,6 @@ int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, int
         delete original;
         return -1;
     }
-    delete original;
 
     QString serv = clipparent.get("mlt_service");
     QString id = clipparent.get("id");
@@ -2032,10 +2087,22 @@ int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, int
             tmp = decodedString(producerid);
             slowprod->set("id", tmp);
             delete[] tmp;
+            // copy producer props
+            double ar = original->parent().get_double("force_aspect_ratio");
+            if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
+            double fps = original->parent().get_double("force_fps");
+            if (fps != 0.0) slowprod->set("force_fps", fps);
+            int threads = original->parent().get_int("threads");
+            if (threads != 0) slowprod->set("threads", threads);
+            if (original->parent().get("force_progressive"))
+                slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
+            int ix = original->parent().get_int("video_index");
+            if (ix != 0) slowprod->set("video_index", ix);
             m_slowmotionProducers.insert(url, slowprod);
         }
         Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
         trackPlaylist.consolidate_blanks(0);
+
         // Check that the blank space is long enough for our new duration
         clipIndex = trackPlaylist.get_clip_index_at(startPos);
         int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
@@ -2064,12 +2131,11 @@ int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, int
         int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
 
         Mlt::Producer *cut;
-        GenTime oldDuration = GenTime(clipLength, m_fps);
-        GenTime newDuration = oldDuration * oldspeed;
-        if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
+        int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps));
+        if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + speedIndependantInfo.cropDuration).frames(m_fps) > blankEnd) {
             GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
-            cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)(info.cropStart.frames(m_fps) + maxLength.frames(m_fps) - 1));
-        } else cut = prod->cut((int)(info.cropStart.frames(m_fps)), (int)((info.cropStart + newDuration).frames(m_fps)) - 1);
+            cut = prod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
+        } else cut = prod->cut(originalStart, (int)(originalStart + speedIndependantInfo.cropDuration.frames(m_fps)) - 1);
 
         // move all effects to the correct producer
         mltPasteEffects(clip, cut);
@@ -2098,23 +2164,34 @@ int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, int
             tmp = decodedString(producerid);
             slowprod->set("id", tmp);
             delete[] tmp;
+            // copy producer props
+            double ar = original->parent().get_double("force_aspect_ratio");
+            if (ar != 0.0) slowprod->set("force_aspect_ratio", ar);
+            double fps = original->parent().get_double("force_fps");
+            if (fps != 0.0) slowprod->set("force_fps", fps);
+            if (original->parent().get("force_progressive"))
+                slowprod->set("force_progressive", original->parent().get_int("force_progressive"));
+            int threads = original->parent().get_int("threads");
+            if (threads != 0) slowprod->set("threads", threads);
+            int ix = original->parent().get_int("video_index");
+            if (ix != 0) slowprod->set("video_index", ix);
             m_slowmotionProducers.insert(url, slowprod);
         }
         Mlt::Producer *clip = trackPlaylist.replace_with_blank(clipIndex);
         trackPlaylist.consolidate_blanks(0);
 
-        GenTime oldDuration = GenTime(clipLength, m_fps);
-        GenTime newDuration = oldDuration * (oldspeed / speed);
+        GenTime duration = speedIndependantInfo.cropDuration / speed;
+        int originalStart = (int)(speedIndependantInfo.cropStart.frames(m_fps) / speed);
 
         // Check that the blank space is long enough for our new duration
         clipIndex = trackPlaylist.get_clip_index_at(startPos);
         int blankEnd = trackPlaylist.clip_start(clipIndex) + trackPlaylist.clip_length(clipIndex);
 
         Mlt::Producer *cut;
-        if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + newDuration).frames(m_fps) > blankEnd) {
+        if (clipIndex + 1 < trackPlaylist.count() && (info.startPos + duration).frames(m_fps) > blankEnd) {
             GenTime maxLength = GenTime(blankEnd, m_fps) - info.startPos;
-            cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)(info.cropStart.frames(m_fps) / speed + maxLength.frames(m_fps) - 1));
-        } else cut = slowprod->cut((int)(info.cropStart.frames(m_fps) / speed), (int)((info.cropStart / speed + newDuration).frames(m_fps) - 1));
+            cut = slowprod->cut(originalStart, (int)(originalStart + maxLength.frames(m_fps) - 1));
+        } else cut = slowprod->cut(originalStart, (int)(originalStart + duration.frames(m_fps)) - 1);
 
         // move all effects to the correct producer
         mltPasteEffects(clip, cut);
@@ -2127,7 +2204,7 @@ int Render::mltChangeClipSpeed(ItemInfo info, double speed, double oldspeed, int
 
         mlt_service_unlock(service.get_service());
     }
-
+    delete original;
     if (clipIndex + 1 == trackPlaylist.count()) mltCheckLength(&tractor);
     m_isBlocked = false;
     return newLength;
@@ -2185,15 +2262,45 @@ bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList para
     Mlt::Service clipService(clip->get_service());
     m_isBlocked = true;
     int duration = clip->get_playtime();
+    bool updateIndex = false;
     delete clip;
-    // temporarily remove all effects after insert point
-    QList <Mlt::Filter *> filtersList;
+
     const int filter_ix = params.paramValue("kdenlive_ix").toInt();
     int ct = 0;
     Mlt::Filter *filter = clipService.filter(ct);
+    while (filter) {
+        if (QString(filter->get("kdenlive_ix")).toInt() == filter_ix) {
+            // A filter at that position already existed, so we will increase all indexes later
+            updateIndex = true;
+            break;
+        }
+        ct++;
+        filter = clipService.filter(ct);
+    }
+
+    if (params.paramValue("id") == "speed") {
+        // special case, speed effect is not really inserted, we just update the other effects index (kdenlive_ix)
+        ct = 0;
+        filter = clipService.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);
+            }
+            ct++;
+            filter = clipService.filter(ct);
+        }
+        m_isBlocked = false;
+        if (doRefresh) refresh();
+        return true;
+    }
+
+
+    // temporarily remove all effects after insert point
+    QList <Mlt::Filter *> filtersList;
+    ct = 0;
+    filter = clipService.filter(ct);
     while (filter) {
         if (QString(filter->get("kdenlive_ix")).toInt() >= filter_ix) {
-            filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() + 1);
             filtersList.append(filter);
             clipService.detach(*filter);
         } else ct++;
@@ -2300,7 +2407,10 @@ bool Render::mltAddEffect(int track, GenTime position, EffectsParameterList para
 
     // re-add following filters
     for (int i = 0; i < filtersList.count(); i++) {
-        clipService.attach(*(filtersList.at(i)));
+        Mlt::Filter *filter = filtersList.at(i);
+        if (updateIndex)
+            filter->set("kdenlive_ix", QString(filter->get("kdenlive_ix")).toInt() + 1);
+        clipService.attach(*filter);
     }
     m_isBlocked = false;
     if (doRefresh) refresh();
@@ -2336,6 +2446,19 @@ bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList par
     m_isBlocked = true;
     int ct = 0;
     Mlt::Filter *filter = clipService.filter(ct);
+
+    /*
+    kDebug() << "EDITING FILTER: "<<index <<", "<<tag;
+    kDebug() << "EFFect stack: ++++++++++++++++++++++++++";
+    while (filter) {
+        kDebug() << "Filter: "<< filter->get("kdenlive_id") <<", IX: "<<filter->get("kdenlive_ix");
+        ct++;
+        filter = clipService.filter(ct);
+    }
+    kDebug() << "++++++++++++++++++++++++++";
+    */
+    ct = 0;
+    filter = clipService.filter(ct);
     while (filter) {
         if (filter->get("kdenlive_ix") == index) {
             break;
@@ -2345,24 +2468,10 @@ bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList par
     }
 
     if (!filter) {
-        kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT!!!!!";
+        kDebug() << "WARINIG, FILTER FOR EDITING NOT FOUND, ADDING IT! " << index << ", " << tag;
         // filter was not found, it was probably a disabled filter, so add it to the correct place...
-        int ct = 0;
-        filter = clipService.filter(ct);
-        QList <Mlt::Filter *> filtersList;
-        while (filter) {
-            if (QString(filter->get("kdenlive_ix")).toInt() > index.toInt()) {
-                filtersList.append(filter);
-                clipService.detach(*filter);
-            } else ct++;
-            filter = clipService.filter(ct);
-        }
-        bool success = mltAddEffect(track, position, params);
-
-        for (int i = 0; i < filtersList.count(); i++) {
-            clipService.attach(*(filtersList.at(i)));
-        }
 
+        bool success = mltAddEffect(track, position, params);
         m_isBlocked = false;
         return success;
     }
@@ -2381,6 +2490,38 @@ bool Render::mltEditEffect(int track, GenTime position, EffectsParameterList par
     return true;
 }
 
+void Render::mltUpdateEffectPosition(int track, GenTime position, int oldPos, int newPos)
+{
+
+    kDebug() << "MOVING EFFECT FROM " << oldPos << ", TO: " << newPos;
+    Mlt::Service service(m_mltProducer->parent().get_service());
+
+    Mlt::Tractor tractor(service);
+    Mlt::Producer trackProducer(tractor.track(track));
+    Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
+    //int clipIndex = trackPlaylist.get_clip_index_at(position.frames(m_fps));
+    Mlt::Producer *clip = trackPlaylist.get_clip_at((int) position.frames(m_fps));
+    if (!clip) {
+        kDebug() << "WARINIG, CANNOT FIND CLIP ON track: " << track << ", AT POS: " << position.frames(m_fps);
+        return;
+    }
+    Mlt::Service clipService(clip->get_service());
+    delete clip;
+    m_isBlocked = true;
+    int ct = 0;
+    Mlt::Filter *filter = clipService.filter(ct);
+    while (filter) {
+        int pos = QString(filter->get("kdenlive_ix")).toInt();
+        if (pos == oldPos) {
+            filter->set("kdenlive_ix", newPos);
+        } else ct++;
+        filter = clipService.filter(ct);
+    }
+
+    m_isBlocked = false;
+    refresh();
+}
+
 void Render::mltMoveEffect(int track, GenTime position, int oldPos, int newPos)
 {
 
@@ -2516,7 +2657,7 @@ bool Render::mltResizeClipEnd(ItemInfo info, GenTime clipDuration)
                 kDebug() << "/// RESIZE ERROR, NXT CLIP IS NOT BLK: " << clipIndex;
             }
         }
-    } else trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
+    } else if (clipIndex != trackPlaylist.count()) trackPlaylist.insert_blank(clipIndex, 0 - diff - 1);
     trackPlaylist.consolidate_blanks(0);
     mlt_service_unlock(service.get_service());
 
@@ -2640,17 +2781,17 @@ bool Render::mltResizeClipStart(ItemInfo info, GenTime diff)
     return true;
 }
 
-bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod)
+bool Render::mltMoveClip(int startTrack, int endTrack, GenTime moveStart, GenTime moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
 {
-    return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod);
+    return mltMoveClip(startTrack, endTrack, (int) moveStart.frames(m_fps), (int) moveEnd.frames(m_fps), prod, overwrite, insert);
 }
 
 
-void Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
+bool Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
 {
     if (prod == NULL || !prod->is_valid()) {
         kDebug() << "// Warning, CLIP on track " << track << ", at: " << pos << " is invalid, cannot update it!!!";
-        return;
+        return false;
     }
     kDebug() << "NEW PROD ID: " << prod->get("id");
     m_isBlocked++;
@@ -2658,24 +2799,24 @@ void Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
     Mlt::Service service(m_mltProducer->parent().get_service());
     if (service.type() != tractor_type) {
         kWarning() << "// TRACTOR PROBLEM";
-        return;
+        return false;
     }
     mlt_service_lock(m_mltConsumer->get_service());
 
     Mlt::Tractor tractor(service);
     Mlt::Producer trackProducer(tractor.track(track));
     Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
-    int clipIndex = trackPlaylist.get_clip_index_at(pos + 1);
+    int clipIndex = trackPlaylist.get_clip_index_at(pos);
     Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
-    if (clipProducer->is_blank()) {
+    if (clipProducer == NULL || clipProducer->is_blank()) {
         kDebug() << "// ERROR UPDATING CLIP PROD";
         delete clipProducer;
         mlt_service_unlock(m_mltConsumer->get_service());
         m_isBlocked--;
-        return;
+        return false;
     }
     Mlt::Producer *clip = prod->cut(clipProducer->get_in(), clipProducer->get_out());
-
+    if (!clip) return false;
     // move all effects to the correct producer
     mltPasteEffects(clipProducer, clip);
     trackPlaylist.insert_at(pos, clip, 1);
@@ -2683,9 +2824,10 @@ void Render::mltUpdateClipProducer(int track, int pos, Mlt::Producer *prod)
     delete clipProducer;
     mlt_service_unlock(m_mltConsumer->get_service());
     m_isBlocked--;
+    return true;
 }
 
-bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod)
+bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEnd, Mlt::Producer *prod, bool overwrite, bool insert)
 {
     m_isBlocked++;
 
@@ -2704,10 +2846,10 @@ bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEn
     bool checkLength = false;
     if (endTrack == startTrack) {
         Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
-        if (!trackPlaylist.is_blank_at(moveEnd) || clipProducer->is_blank()) {
+        if (!overwrite && (!trackPlaylist.is_blank_at(moveEnd) || !clipProducer || clipProducer->is_blank())) {
             // error, destination is not empty
             if (!trackPlaylist.is_blank_at(moveEnd)) trackPlaylist.insert_at(moveStart, clipProducer, 1);
-            delete clipProducer;
+            if (clipProducer) delete clipProducer;
             //int ix = trackPlaylist.get_clip_index_at(moveEnd);
             kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
             mlt_service_unlock(service.get_service());
@@ -2715,7 +2857,13 @@ bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEn
             return false;
         } else {
             trackPlaylist.consolidate_blanks(0);
+            if (overwrite) {
+                trackPlaylist.remove_region(moveEnd, clipProducer->get_playtime());
+                int clipIndex = trackPlaylist.get_clip_index_at(moveEnd);
+                trackPlaylist.insert_blank(clipIndex, clipProducer->get_playtime() - 1);
+            }
             int newIndex = trackPlaylist.insert_at(moveEnd, clipProducer, 1);
+            trackPlaylist.consolidate_blanks(1);
             delete clipProducer;
             /*if (QString(clipProducer.parent().get("transparency")).toInt() == 1) {
             mltMoveTransparency(moveStart, moveEnd, startTrack, endTrack, QString(clipProducer.parent().get("id")).toInt());
@@ -2726,17 +2874,17 @@ bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEn
     } else {
         Mlt::Producer destTrackProducer(tractor.track(endTrack));
         Mlt::Playlist destTrackPlaylist((mlt_playlist) destTrackProducer.get_service());
-        if (!destTrackPlaylist.is_blank_at(moveEnd)) {
+        if (!overwrite && !destTrackPlaylist.is_blank_at(moveEnd)) {
             // error, destination is not empty
             mlt_service_unlock(service.get_service());
             m_isBlocked--;
             return false;
         } else {
             Mlt::Producer *clipProducer = trackPlaylist.replace_with_blank(clipIndex);
-            if (clipProducer->is_blank()) {
+            if (!clipProducer || clipProducer->is_blank()) {
                 // error, destination is not empty
                 //int ix = trackPlaylist.get_clip_index_at(moveEnd);
-                delete clipProducer;
+                if (clipProducer) delete clipProducer;
                 kDebug() << "// ERROR MOVING CLIP TO : " << moveEnd;
                 mlt_service_unlock(service.get_service());
                 m_isBlocked--;
@@ -2762,9 +2910,15 @@ bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEn
 
             // move all effects to the correct producer
             mltPasteEffects(clipProducer, clip);
-            
+
+            if (overwrite) {
+                destTrackPlaylist.remove_region(moveEnd, clip->get_playtime());
+                int clipIndex = destTrackPlaylist.get_clip_index_at(moveEnd);
+                destTrackPlaylist.insert_blank(clipIndex, clip->get_playtime() - 1);
+            }
+
             int newIndex = destTrackPlaylist.insert_at(moveEnd, clip, 1);
-            
+
             if (clip == clipProducer) {
                 delete clip;
                 clip = NULL;
@@ -2789,40 +2943,71 @@ bool Render::mltMoveClip(int startTrack, int endTrack, int moveStart, int moveEn
     return true;
 }
 
+
+QList <int> Render::checkTrackSequence(int track)
+{
+    QList <int> list;
+    Mlt::Service service(m_mltProducer->parent().get_service());
+    if (service.type() != tractor_type) {
+        kWarning() << "// TRACTOR PROBLEM";
+        return list;
+    }
+    Mlt::Tractor tractor(service);
+    mlt_service_lock(service.get_service());
+    Mlt::Producer trackProducer(tractor.track(track));
+    Mlt::Playlist trackPlaylist((mlt_playlist) trackProducer.get_service());
+    int clipNb = trackPlaylist.count();
+    //kDebug() << "// PARSING SCENE TRACK: " << t << ", CLIPS: " << clipNb;
+    for (int i = 0; i < clipNb; i++) {
+        Mlt::Producer *c = trackPlaylist.get_clip(i);
+        int pos = trackPlaylist.clip_start(i);
+        if (!list.contains(pos)) list.append(pos);
+        pos += c->get_playtime();
+        if (!list.contains(pos)) list.append(pos);
+        delete c;
+    }
+    return list;
+}
+
 bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int newTransitionTrack, GenTime oldIn, GenTime oldOut, GenTime newIn, GenTime newOut)
 {
     int new_in = (int)newIn.frames(m_fps);
     int new_out = (int)newOut.frames(m_fps) - 1;
     if (new_in >= new_out) return false;
 
-    mlt_service serv = m_mltProducer->parent().get_service();
-    m_isBlocked++;
-    mlt_service_lock(serv);
-    //m_mltConsumer->set("refresh", 0);
+    Mlt::Service service(m_mltProducer->parent().get_service());
+    Mlt::Tractor tractor(service);
+    Mlt::Field *field = tractor.field();
 
+    m_isBlocked++;
+    mlt_service_lock(service.get_service());
 
-    mlt_service nextservice = mlt_service_get_producer(serv);
+    mlt_service nextservice = mlt_service_get_producer(service.get_service());
     mlt_properties properties = MLT_SERVICE_PROPERTIES(nextservice);
     QString mlt_type = mlt_properties_get(properties, "mlt_type");
     QString resource = mlt_properties_get(properties, "mlt_service");
     int old_pos = (int)(oldIn.frames(m_fps) + oldOut.frames(m_fps)) / 2;
+    bool found = false;
 
     while (mlt_type == "transition") {
-        mlt_transition tr = (mlt_transition) nextservice;
-        int currentTrack = mlt_transition_get_b_track(tr);
-        int currentIn = (int) mlt_transition_get_in(tr);
-        int currentOut = (int) mlt_transition_get_out(tr);
+        Mlt::Transition transition((mlt_transition) nextservice);
+        int currentTrack = transition.get_b_track();
+        int currentIn = (int) transition.get_in();
+        int currentOut = (int) transition.get_out();
 
         if (resource == type && startTrack == currentTrack && currentIn <= old_pos && currentOut >= old_pos) {
-            mlt_transition_set_in_and_out(tr, new_in, new_out);
+            found = true;
             if (newTrack - startTrack != 0) {
-                //kDebug() << "///// TRANSITION CHANGE TRACK. CUrrent (b): " << currentTrack << 'x' << mlt_transition_get_a_track(tr) << ", NEw: " << newTrack << 'x' << newTransitionTrack;
-
-                mlt_properties properties = MLT_TRANSITION_PROPERTIES(tr);
-                mlt_properties_set_int(properties, "a_track", newTransitionTrack);
-                mlt_properties_set_int(properties, "b_track", newTrack);
-                //kDebug() << "set new start & end :" << new_in << new_out<< "TR OFFSET: "<<trackOffset<<", TRACKS: "<<mlt_transition_get_a_track(tr)<<'x'<<mlt_transition_get_b_track(tr);
-            }
+                Mlt::Properties trans_props(transition.get_properties());
+                char *tmp = decodedString(transition.get("mlt_service"));
+                Mlt::Transition new_transition(*m_mltProfile, tmp);
+                delete[] tmp;
+                Mlt::Properties new_trans_props(new_transition.get_properties());
+                new_trans_props.inherit(trans_props);
+                new_transition.set_in_and_out(new_in, new_out);
+                field->disconnect_service(transition);
+                field->plant_transition(new_transition, newTransitionTrack, newTrack);
+            } else transition.set_in_and_out(new_in, new_out);
             break;
         }
         nextservice = mlt_service_producer(nextservice);
@@ -2831,11 +3016,11 @@ bool Render::mltMoveTransition(QString type, int startTrack, int newTrack, int n
         mlt_type = mlt_properties_get(properties, "mlt_type");
         resource = mlt_properties_get(properties, "mlt_service");
     }
-    mlt_service_unlock(serv);
+    mlt_service_unlock(service.get_service());
     m_isBlocked--;
     refresh();
     //if (m_isBlocked == 0) m_mltConsumer->set("refresh", 1);
-    return true;
+    return found;
 }
 
 void Render::mltUpdateTransition(QString oldTag, QString tag, int a_track, int b_track, GenTime in, GenTime out, QDomElement xml)
@@ -2877,6 +3062,7 @@ void Render::mltUpdateTransitionParams(QString type, int a_track, int b_track, G
             QString key;
             mlt_properties transproperties = MLT_TRANSITION_PROPERTIES(tr);
             mlt_properties_set_int(transproperties, "force_track", xml.attribute("force_track").toInt());
+            mlt_properties_set_int(transproperties, "automatic", xml.attribute("automatic", "0").toInt());
             // update the transition id in case it uses the same MLT service but different Kdenlive id
             char *tmp = decodedString(xml.attribute("id"));
             mlt_properties_set(transproperties, "kdenlive_id", tmp);
@@ -3349,7 +3535,13 @@ void Render::mltDeleteTrack(int ix)
             int a_track = mappedProps.value("a_track").toInt();
             int b_track = mappedProps.value("b_track").toInt();
             if (a_track > 0 && a_track >= ix) a_track --;
-            if (b_track > 0 && b_track >= ix) b_track --;
+            if (b_track > 0 && b_track > ix) b_track --;
+            if (b_track == ix) {
+                // transition was on the deleted track, so remove it
+                tractor.removeChild(transitions.at(i));
+                i--;
+                continue;
+            }
             for (int j = 0; j < props.count(); j++) {
                 QDomElement f = props.at(j).toElement();
                 if (f.attribute("name") == "a_track") f.firstChild().setNodeValue(QString::number(a_track));