]> git.sesse.net Git - kdenlive/blobdiff - src/docclipbase.cpp
Try to fix audio mixing bug ( http://www.kdenlive.org:80/mantis/view.php?id=228 )
[kdenlive] / src / docclipbase.cpp
index 344d692df825c29012c0f1701b5c82fc6f34093a..bf26e183182901bf4cf9c6fd22b0092d980036d7 100644 (file)
 #include "kthumb.h"
 #include "clipmanager.h"
 
-DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, uint id):
-        m_xml(xml), m_id(id), m_description(""), m_refcount(0), m_projectThumbFrame(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL) {
+DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id):
+        m_id(id), m_description(QString()), m_refcount(0), m_audioThumbCreated(false), m_duration(GenTime()), m_thumbProd(NULL), m_audioTimer(NULL), m_properties(QMap <QString, QString> ()), audioFrameChache(QMap<int, QMap<int, QByteArray> > ()), m_baseTrackProducers(QList <Mlt::Producer *>())  {
     int type = xml.attribute("type").toInt();
     m_clipType = (CLIPTYPE) type;
     m_name = xml.attribute("name");
-    m_xml.setAttribute("id", QString::number(id));
+
+    QDomNamedNodeMap attributes = xml.attributes();
+    for (unsigned int i = 0; i < attributes.count(); i++) {
+        m_properties.insert(attributes.item(i).nodeName(), attributes.item(i).nodeValue());
+    }
+
     KUrl url = KUrl(xml.attribute("resource"));
     int out = xml.attribute("out").toInt();
-    if (out != 0) setDuration(GenTime(out, 25));
+    if (out != 0) {
+        setDuration(GenTime(out, KdenliveSettings::project_fps()));
+    } else {
+        out = xml.attribute("duration").toInt();
+        if (out != 0) setDuration(GenTime(out, KdenliveSettings::project_fps()));
+    }
     if (m_name.isEmpty()) m_name = url.fileName();
 
-    if (!url.isEmpty()) {
-        m_thumbProd = new KThumb(clipManager, url);
+    //if (!url.isEmpty() && QFile::exists(url.path()))
+    {
+        m_thumbProd = new KThumb(clipManager, url, m_id);
         if (m_clipType == AV || m_clipType == AUDIO) slotCreateAudioTimer();
     }
     //kDebug() << "type is video" << (m_clipType == AV) << " " << m_clipType;
 }
 
-
-
-DocClipBase::DocClipBase(const DocClipBase& clip) {
-    m_xml = clip.toXML();
-    m_id = clip.getId();
-    m_clipType = clip.clipType();
-    m_name = clip.name();
-    m_duration = clip.duration();
-    m_audioThumbCreated = clip.audioThumbCreated();
-}
-
-DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
+/*DocClipBase & DocClipBase::operator=(const DocClipBase & clip) {
     DocClipBase::operator=(clip);
-    m_xml = clip.toXML();
     m_id = clip.getId();
     m_clipType = clip.clipType();
     m_name = clip.name();
     m_duration = clip.duration();
     m_audioThumbCreated = clip.audioThumbCreated();
+    m_properties = clip.properties();
     return *this;
-}
+}*/
 
 DocClipBase::~DocClipBase() {
     if (m_thumbProd) delete m_thumbProd;
+    qDeleteAll(m_baseTrackProducers);
 }
 
 void DocClipBase::slotCreateAudioTimer() {
@@ -73,19 +74,20 @@ void DocClipBase::slotCreateAudioTimer() {
     connect(m_audioTimer, SIGNAL(timeout()), this, SLOT(slotGetAudioThumbs()));
 }
 
-void DocClipBase::slotRequestAudioThumbs() {
-    emit getAudioThumbs();
+void DocClipBase::askForAudioThumbs() {
+    if (m_thumbProd) m_thumbProd->askForAudioThumbs(getId());
 }
 
 void DocClipBase::slotClearAudioCache() {
+    if (m_thumbProd) m_thumbProd->stopAudioThumbs();
+    if (m_audioTimer != NULL) m_audioTimer->stop();
     audioFrameChache.clear();
     m_audioThumbCreated = false;
 }
 
-void DocClipBase::setGroup(const QString name, const QString id) {
-    m_xml.setAttribute("groupname", name);
-    m_xml.setAttribute("groupid", id);
-}
+/*void DocClipBase::getClipMainThumb() {
+    if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
+}*/
 
 KThumb *DocClipBase::thumbProducer() {
     return m_thumbProd;
@@ -104,11 +106,11 @@ const QString & DocClipBase::name() const {
     return m_name;
 }
 
-uint DocClipBase::getId() const {
+const QString &DocClipBase::getId() const {
     return m_id;
 }
 
-void DocClipBase::setId(const uint &newId) {
+void DocClipBase::setId(const QString &newId) {
     m_id = newId;
 }
 
@@ -118,40 +120,55 @@ const CLIPTYPE & DocClipBase::clipType() const {
 
 void DocClipBase::setClipType(CLIPTYPE type) {
     m_clipType = type;
-    if (m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
+
+    m_properties.insert("type", QString::number((int) type));
+    if (m_thumbProd && m_audioTimer == NULL && (m_clipType == AV || m_clipType == AUDIO))
         slotCreateAudioTimer();
 }
 
 KUrl DocClipBase::fileURL() const {
-    QString res = m_xml.attribute("resource");
+    QString res = m_properties.value("resource");
     if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
     return KUrl();
 }
 
-void DocClipBase::setProjectThumbFrame(const uint &ix) {
-    m_projectThumbFrame = ix;
+void DocClipBase::setClipThumbFrame(const uint &ix) {
+    m_properties.insert("thumbnail", QString::number((int) ix));
+}
+
+uint DocClipBase::getClipThumbFrame() const {
+    return (uint) m_properties.value("thumbnail").toInt();
 }
 
-uint DocClipBase::getProjectThumbFrame() const {
-    return m_projectThumbFrame;
+const QString DocClipBase::description() const {
+    return m_properties.value("description");
 }
 
-void DocClipBase::setDescription(const QString & description) {
-    m_description = description;
+bool DocClipBase::isTransparent() const {
+    return (m_properties.value("transparency") == "1");
 }
 
-const QString & DocClipBase::description() const {
-    return m_description;
+const QString DocClipBase::getProperty(const QString prop) const {
+    return m_properties.value(prop);
 }
 
 void DocClipBase::setDuration(GenTime dur) {
     m_duration = dur;
+    m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
 }
 
 const GenTime &DocClipBase::duration() const {
     return m_duration;
 }
 
+const GenTime &DocClipBase::maxDuration() const {
+    if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW &&  m_properties.value("loop") == "1")) {
+        const GenTime dur(10000, KdenliveSettings::project_fps());
+        return dur;
+    }
+    return m_duration;
+}
+
 bool DocClipBase::hasFileSize() const {
     return true;
 }
@@ -159,15 +176,18 @@ bool DocClipBase::hasFileSize() const {
 
 // virtual
 QDomElement DocClipBase::toXML() const {
-    /*
-        QDomDocument doc;
+    QDomDocument doc;
+
+    QDomElement clip = doc.createElement("producer");
 
-        QDomElement clip = doc.createElement("kdenliveclip");
-        QDomText text = doc.createTextNode(description());
-        clip.appendChild(text);
-        doc.appendChild(clip);
-    */
-    return m_xml;
+    QMapIterator<QString, QString> i(m_properties);
+    while (i.hasNext()) {
+        i.next();
+        if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
+    }
+    //doc.appendChild(clip);
+    //kDebug()<<"/// CLIP XML: "<<doc.toString();
+    return clip;
 }
 
 DocClipBase *DocClipBase::
@@ -178,8 +198,7 @@ createClip(KdenliveDoc *doc, const QDomElement & element) {
     node.normalize();
     if (element.tagName() != "kdenliveclip") {
         kWarning() <<
-        "DocClipBase::createClip() element has unknown tagName : " <<
-        element.tagName() << endl;
+        "DocClipBase::createClip() element has unknown tagName : " << element.tagName();
         return 0;
     }
 
@@ -204,11 +223,12 @@ createClip(KdenliveDoc *doc, const QDomElement & element) {
         n = n.nextSibling();
     }
     if (clip == 0) {
-        kWarning() << "DocClipBase::createClip() unable to create clip" <<
-        endl;
+        kWarning() << "DocClipBase::createClip() unable to create clip";
     } else {
         // setup DocClipBase specifics of the clip.
-        clip->setDescription(description);
+        QMap <QString, QString> props;
+        props.insert("description", description);
+        clip->setProperties(props);
         clip->setAudioThumbCreated(false);
     }
     return clip;
@@ -219,9 +239,6 @@ void DocClipBase::setAudioThumbCreated(bool isDone) {
 }
 
 
-QDomDocument DocClipBase::generateSceneList(bool, bool) const {
-}
-
 void DocClipBase::setThumbnail(const QPixmap & pixmap) {
     m_thumbnail = pixmap;
 }
@@ -263,9 +280,8 @@ void DocClipBase::addSnapMarker(const GenTime & time, QString comment) {
     }
 
     if ((it != m_snapMarkers.end()) && ((*it).time() == time)) {
-        kError() <<
-        "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo"
-        << endl;
+        (*it).setComment(comment);
+        //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
     } else {
         CommentedTime t(time, comment);
         m_snapMarkers.insert(it, t);
@@ -282,8 +298,7 @@ void DocClipBase::editSnapMarker(const GenTime & time, QString comment) {
     if (it != m_snapMarkers.end()) {
         (*it).setComment(comment);
     } else {
-        kError() <<
-        "trying to edit Snap Marker that does not already exists"  << endl;
+        kError() << "trying to edit Snap Marker that does not already exists";
     }
 }
 
@@ -350,26 +365,207 @@ QString DocClipBase::markerComment(GenTime t) {
     return QString::null;
 }
 
-void DocClipBase::setProperties(QMap <QString, QString> properties) {
-    m_properties = properties;
+void DocClipBase::setProducer(Mlt::Producer *producer) {
+    if (producer == NULL) return;
+    QString id = producer->get("id");
+    kDebug() << "// SET PRODUCER: " << id;
+    if (id.contains('_')) {
+        // this is a subtrack producer, insert it at correct place
+        int pos = id.section('_', 1, 1).toInt();
+        kDebug() << "// POS = " << pos << ", MAX: " << m_baseTrackProducers.count();
+        if (pos >= m_baseTrackProducers.count()) {
+            while (m_baseTrackProducers.count() - 1 < pos) {
+                m_baseTrackProducers.append(NULL);
+            }
+        }
+        kDebug() << "// POS = " << pos << ", NEW MAX: " << m_baseTrackProducers.count();
+        if (m_baseTrackProducers.at(pos) == NULL) m_baseTrackProducers[pos] = producer;
+    } else m_baseTrackProducers.append(producer);
+    //m_clipProducer = producer;
+    //m_clipProducer->set("transparency", m_properties.value("transparency").toInt());
+    if (m_thumbProd && !m_thumbProd->hasProducer()) m_thumbProd->setProducer(producer);
+}
+
+Mlt::Producer *DocClipBase::producer(int track) {
+    if (track == -1 || (m_clipType != AUDIO && m_clipType != AV)) {
+        if (m_baseTrackProducers.count() == 0) return NULL;
+        int i;
+        for (i = 0; i < m_baseTrackProducers.count(); i++)
+            if (m_baseTrackProducers.at(i) != NULL) break;
+        if (i < m_baseTrackProducers.count()) return m_baseTrackProducers.at(i);
+        return NULL;
+    }
+    if (track >= m_baseTrackProducers.count()) {
+        while (m_baseTrackProducers.count() - 1 < track) {
+            m_baseTrackProducers.append(NULL);
+        }
+    }
+    if (m_baseTrackProducers.at(track) == NULL) {
+        int i;
+        for (i = 0; i < m_baseTrackProducers.count(); i++)
+            if (m_baseTrackProducers.at(i) != NULL) break;
+        if (i >= m_baseTrackProducers.count()) return NULL;
+        m_baseTrackProducers[track] = new Mlt::Producer(*m_baseTrackProducers.at(i)->profile(), m_baseTrackProducers.at(i)->get("resource"));
+        if (m_properties.contains("force_aspect_ratio")) m_baseTrackProducers[track]->set("force_aspect_ratio", m_properties.value("force_aspect_ratio").toDouble());
+        if (m_properties.contains("threads")) m_baseTrackProducers[track]->set("threads", m_properties.value("threads").toInt());
+        if (m_properties.contains("video_index")) m_baseTrackProducers[track]->set("video_index", m_properties.value("video_index").toInt());
+        if (m_properties.contains("audio_index")) m_baseTrackProducers[track]->set("audio_index", m_properties.value("audio_index").toInt());
+        char *tmp = (char *) qstrdup(QString(getId() + '_' + QString::number(track)).toUtf8().data());
+        m_baseTrackProducers[track]->set("id", tmp);
+        delete[] tmp;
+    }
+    return m_baseTrackProducers.at(track);
 }
 
-QMap <QString, QString> DocClipBase::properties() {
-    return m_properties;
+void DocClipBase::setProducerProperty(const char *name, int data) {
+    for (int i = 0; i < m_baseTrackProducers.count(); i++) {
+        if (m_baseTrackProducers.at(i) != NULL)
+            m_baseTrackProducers[i]->set(name, data);
+    }
 }
 
+void DocClipBase::setProducerProperty(const char *name, const char *data) {
+    for (int i = 0; i < m_baseTrackProducers.count(); i++) {
+        if (m_baseTrackProducers.at(i) != NULL)
+            m_baseTrackProducers[i]->set(name, data);
+    }
+}
 
-void DocClipBase::slotGetAudioThumbs() {
+void DocClipBase::slotRefreshProducer() {
+    if (m_baseTrackProducers.count() == 0) return;
+    kDebug() << "////////////   REFRESH CLIP !!!!!!!!!!!!!!!!";
+    if (m_clipType == SLIDESHOW) {
+        /*char *tmp = (char *) qstrdup(getProperty("resource").toUtf8().data());
+               Mlt::Producer producer(*(m_clipProducer->profile()), tmp);
+               delete[] tmp;
+        delete m_clipProducer;
+        m_clipProducer = new Mlt::Producer(producer.get_producer());
+        if (!getProperty("out").isEmpty()) m_clipProducer->set_in_and_out(getProperty("in").toInt(), getProperty("out").toInt());*/
+        setProducerProperty("ttl", getProperty("ttl").toInt());
+        //m_clipProducer->set("id", getProperty("id"));
+        if (getProperty("fade") == "1") {
+            // we want a fade filter effect
+            kDebug() << "////////////   FADE WANTED";
+            Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
+            int ct = 0;
+            Mlt::Filter *filter = clipService.filter(ct);
+            while (filter) {
+                if (filter->get("mlt_service") == "luma") {
+                    break;
+                }
+                ct++;
+                filter = clipService.filter(ct);
+            }
 
+            if (filter && filter->get("mlt_service") == "luma") {
+                filter->set("period", getProperty("ttl").toInt() - 1);
+                filter->set("luma.out", getProperty("luma_duration").toInt());
+                QString resource = getProperty("luma_file");
+                char *tmp = (char *) qstrdup(resource.toUtf8().data());
+                filter->set("luma.resource", tmp);
+                delete[] tmp;
+                if (getProperty("softness") != QString()) {
+                    int soft = getProperty("softness").toInt();
+                    filter->set("luma.softness", (double) soft / 100.0);
+                }
+            } else {
+                // filter does not exist, create it...
+                Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
+                filter->set("period", getProperty("ttl").toInt() - 1);
+                filter->set("luma.out", getProperty("luma_duration").toInt());
+                QString resource = getProperty("luma_file");
+                char *tmp = (char *) qstrdup(resource.toUtf8().data());
+                filter->set("luma.resource", tmp);
+                delete[] tmp;
+                if (getProperty("softness") != QString()) {
+                    int soft = getProperty("softness").toInt();
+                    filter->set("luma.softness", (double) soft / 100.0);
+                }
+                clipService.attach(*filter);
+            }
+        } else {
+            kDebug() << "////////////   FADE NOT WANTED!!!";
+            Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
+            int ct = 0;
+            Mlt::Filter *filter = clipService.filter(0);
+            while (filter) {
+                if (filter->get("mlt_service") == "luma") {
+                    clipService.detach(*filter);
+                } else ct++;
+                filter = clipService.filter(ct);
+            }
+        }
+    }
+}
+
+void DocClipBase::setProperties(QMap <QString, QString> properties) {
+    // changing clip type is not allowed
+    properties.remove("type");
+    QMapIterator<QString, QString> i(properties);
+    bool refreshProducer = false;
+    QStringList keys;
+    keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness";
+    while (i.hasNext()) {
+        i.next();
+        setProperty(i.key(), i.value());
+        if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
+    }
+    if (refreshProducer) slotRefreshProducer();
+}
+
+void DocClipBase::clearProperty(const QString &key) {
+    m_properties.remove(key);
+}
+
+void DocClipBase::setProperty(const QString &key, const QString &value) {
+    m_properties.insert(key, value);
+    if (key == "resource") m_thumbProd->updateClipUrl(KUrl(value));
+    else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
+    //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
+    else if (key == "colour") {
+        char *tmp = (char *) qstrdup(value.toUtf8().data());
+        setProducerProperty("colour", tmp);
+        delete[] tmp;
+    } else if (key == "xmldata") {
+        setProducerProperty("force_reload", 1);
+    } else if (key == "force_aspect_ratio") {
+        if (value.isEmpty()) {
+            m_properties.remove("force_aspect_ratio");
+            setProducerProperty("force_aspect_ratio", 0);
+        } else setProducerProperty("force_aspect_ratio", value.toDouble());
+    } else if (key == "threads") {
+        if (value.isEmpty()) {
+            m_properties.remove("threads");
+            setProducerProperty("threads", 1);
+        } else setProducerProperty("threads", value.toInt());
+    } else if (key == "video_index") {
+        if (value.isEmpty()) {
+            m_properties.remove("video_index");
+            setProducerProperty("video_index", m_properties.value("default_video").toInt());
+        } else setProducerProperty("video_index", value.toInt());
+    } else if (key == "audio_index") {
+        if (value.isEmpty()) {
+            m_properties.remove("audio_index");
+            setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
+        } else setProducerProperty("audio_index", value.toInt());
+    }
+}
+
+QMap <QString, QString> DocClipBase::properties() const {
+    return m_properties;
+}
+
+bool DocClipBase::slotGetAudioThumbs() {
+    if (m_thumbProd == NULL) return false;
     if (m_audioThumbCreated) {
-        if (m_audioTimer != NULL)
-            m_audioTimer->stop();
-    } else {
-        if (m_audioTimer != NULL)
-            m_audioTimer->start(2000);
-        double lengthInFrames = duration().frames(/*framesPerSecond()*/25);
-        m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
+        if (m_audioTimer != NULL) m_audioTimer->stop();
+        return false;
     }
+    if (m_audioTimer != NULL)
+        m_audioTimer->start(1500);
+    double lengthInFrames = duration().frames(KdenliveSettings::project_fps());
+    m_thumbProd->getAudioThumbs(2, 0, lengthInFrames /*must be number of frames*/, 20);
+    return true;
 }