1 /***************************************************************************
2 * DocClipBase.cpp - description *
3 * ------------------- *
4 * begin : Fri Apr 12 2002 *
5 * Copyright (C) 2002 by Jason Wood (jasonwood@blueyonder.co.uk) *
6 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
27 #include "docclipbase.h"
28 #include "kdenlivesettings.h"
30 #include "clipmanager.h"
31 #include "slideshowclip.h"
33 #include <KIO/NetAccess>
34 #include <KStandardDirs>
35 #include <KApplication>
38 #include <QCryptographicHash>
39 #include <QtConcurrentRun>
42 #include <kmessagebox.h>
44 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
49 m_baseTrackProducers(),
50 m_videoTrackProducers(),
51 m_audioTrackProducers(),
52 m_snapMarkers(QList < CommentedTime >()),
55 m_audioThumbCreated(false),
57 m_placeHolder(xml.hasAttribute("placeholder")),
60 int type = xml.attribute("type").toInt();
61 m_clipType = (CLIPTYPE) type;
62 if (m_placeHolder) xml.removeAttribute("placeholder");
63 QDomNamedNodeMap attributes = xml.attributes();
64 for (int i = 0; i < attributes.count(); i++) {
65 QString name = attributes.item(i).nodeName();
66 if (name.startsWith("meta.attr.")) {
67 m_metadata.insert(name.section('.', 2), QStringList() << attributes.item(i).nodeValue());
68 } else m_properties.insert(name, attributes.item(i).nodeValue());
70 QDomNodeList metas = xml.elementsByTagName("metaproperty");
71 for (int i = 0; i < metas.count(); i++) {
72 QDomElement e = metas.item(i).toElement();
74 m_metadata.insert(e.attribute("name").section('.', 2), QStringList() << e.firstChild().nodeValue() << e.attribute("tool"));
78 if (xml.hasAttribute("cutzones")) {
79 QStringList cuts = xml.attribute("cutzones").split(';', QString::SkipEmptyParts);
80 for (int i = 0; i < cuts.count(); i++) {
81 QString z = cuts.at(i);
82 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
86 if (xml.hasAttribute("analysisdata")) {
87 QStringList adata = xml.attribute("analysisdata").split('#', QString::SkipEmptyParts);
88 for (int i = 0; i < adata.count(); i++)
89 m_analysisdata.insert(adata.at(i).section('?', 0, 0), adata.at(i).section('?', 1, 1));
92 KUrl url = KUrl(xml.attribute("resource"));
93 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
95 if (xml.hasAttribute("duration")) {
96 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
98 int out = xml.attribute("out").toInt();
99 int in = xml.attribute("in").toInt();
100 if (out > in) setDuration(GenTime(out - in + 1, KdenliveSettings::project_fps()));
103 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
105 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
107 // Setup timer to trigger audio thumbs creation
108 m_audioTimer.setSingleShot(true);
109 m_audioTimer.setInterval(800);
110 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
114 DocClipBase::~DocClipBase()
119 qDeleteAll(m_toDeleteProducers);
120 m_toDeleteProducers.clear();
121 qDeleteAll(m_baseTrackProducers);
122 m_baseTrackProducers.clear();
123 qDeleteAll(m_audioTrackProducers);
124 m_audioTrackProducers.clear();
125 qDeleteAll(m_videoTrackProducers);
126 m_videoTrackProducers.clear();
129 void DocClipBase::setZone(QPoint zone)
131 m_properties.insert("zone_in", QString::number(zone.x()));
132 m_properties.insert("zone_out", QString::number(zone.y()));
135 QPoint DocClipBase::zone() const
137 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
142 bool DocClipBase::hasAudioThumb() const
144 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
148 void DocClipBase::slotClearAudioCache()
150 audioFrameCache.clear();
151 m_audioThumbCreated = false;
154 /*void DocClipBase::getClipMainThumb() {
155 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
158 KThumb *DocClipBase::thumbProducer()
163 bool DocClipBase::audioThumbCreated() const
165 return m_audioThumbCreated;
168 const QString DocClipBase::name() const
171 return m_properties.value("name");
174 const QString &DocClipBase::getId() const
179 const CLIPTYPE & DocClipBase::clipType() const
184 void DocClipBase::setClipType(CLIPTYPE type)
187 m_properties.insert("type", QString::number((int) type));
190 KUrl DocClipBase::fileURL() const
192 QString res = m_properties.value("resource");
193 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
197 void DocClipBase::setClipThumbFrame(const uint &ix)
199 m_properties.insert("thumbnail", QString::number((int) ix));
202 uint DocClipBase::getClipThumbFrame() const
204 return (uint) m_properties.value("thumbnail").toInt();
207 const QString DocClipBase::description() const
209 return m_properties.value("description");
212 bool DocClipBase::isTransparent() const
214 return (m_properties.value("transparency") == "1");
217 const QString DocClipBase::getProperty(const QString &prop) const
219 return m_properties.value(prop);
222 void DocClipBase::setDuration(GenTime dur)
225 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
228 const GenTime &DocClipBase::duration() const
233 const GenTime DocClipBase::maxDuration() const
235 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
236 /*const GenTime dur(15000, KdenliveSettings::project_fps());
243 bool DocClipBase::hasFileSize() const
248 qulonglong DocClipBase::fileSize() const
250 return m_properties.value("file_size").toULongLong();
254 QDomElement DocClipBase::toXML(bool hideTemporaryProperties) const
257 QDomElement clip = doc.createElement("producer");
259 QMapIterator<QString, QString> i(m_properties);
260 while (i.hasNext()) {
262 if (hideTemporaryProperties && i.key().startsWith('_')) continue;
263 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
266 QMapIterator<QString, QStringList> j(m_metadata);
267 // Metadata name can have special chars so we cannot pass it as simple attribute
268 while (j.hasNext()) {
270 if (!j.value().isEmpty()) {
271 QDomElement property = doc.createElement("metaproperty");
272 property.setAttribute("name", "meta.attr." + j.key());
273 QStringList values = j.value();
274 QDomText value = doc.createTextNode(values.at(0));
275 if (values.count() > 1) property.setAttribute("tool", values.at(1));
276 property.appendChild(value);
277 clip.appendChild(property);
280 doc.appendChild(clip);
281 if (!m_cutZones.isEmpty()) {
283 for (int i = 0; i < m_cutZones.size(); i++) {
284 CutZoneInfo info = m_cutZones.at(i);
285 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
287 clip.setAttribute("cutzones", cuts.join(";"));
290 if (!m_analysisdata.isEmpty()) {
291 QMapIterator<QString, QString> i(m_analysisdata);
292 while (i.hasNext()) {
294 //WARNING: a ? and # separator is not a good idea
295 adata.append(i.key() + "?" + i.value() + "#");
298 clip.setAttribute("analysisdata", adata);
299 //kDebug() << "/// CLIP XML: " << doc.toString();
300 return doc.documentElement();
304 void DocClipBase::setAudioThumbCreated(bool isDone)
306 m_audioThumbCreated = isDone;
309 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
311 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
312 audioFrameCache = data;
313 m_audioThumbCreated = true;
317 QList < GenTime > DocClipBase::snapMarkers() const
319 QList < GenTime > markers;
320 for (int count = 0; count < m_snapMarkers.count(); ++count) {
321 markers.append(m_snapMarkers.at(count).time());
327 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
329 return m_snapMarkers;
333 void DocClipBase::addSnapMarker(const CommentedTime marker)
335 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
336 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
337 if ((*it).time() >= marker.time())
341 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
342 (*it).setComment(marker.comment());
343 (*it).setMarkerType(marker.markerType());
344 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
346 m_snapMarkers.insert(it, marker);
350 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
352 QList < CommentedTime >::Iterator it;
353 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
354 if ((*it).time() == time)
357 if (it != m_snapMarkers.end()) {
358 (*it).setComment(comment);
360 kError() << "trying to edit Snap Marker that does not already exists";
364 QString DocClipBase::deleteSnapMarker(const GenTime & time)
366 QString result = i18n("Marker");
367 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
369 while (itt != m_snapMarkers.end()) {
370 if ((*itt).time() == time)
375 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
376 result = (*itt).comment();
377 m_snapMarkers.erase(itt);
383 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
385 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
387 while (itt != m_snapMarkers.end()) {
388 if ((*itt).time() == time)
396 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
399 for (it = 0; it < m_snapMarkers.count(); it++) {
400 if (m_snapMarkers.at(it).time() >= currTime)
403 if (it == 0) return GenTime();
404 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
405 return m_snapMarkers.at(it).time();
406 else return m_snapMarkers.at(it - 1).time();
409 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
412 for (it = 0; it < m_snapMarkers.count(); it++) {
413 if (m_snapMarkers.at(it).time() > currTime)
416 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
420 QString DocClipBase::markerComment(GenTime t) const
422 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
423 while (itt != m_snapMarkers.end()) {
424 if ((*itt).time() == t)
425 return (*itt).comment();
431 CommentedTime DocClipBase::markerAt(GenTime t) const
433 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
434 while (itt != m_snapMarkers.end()) {
435 if ((*itt).time() == t)
439 return CommentedTime();
442 void DocClipBase::clearThumbProducer()
444 if (m_thumbProd) m_thumbProd->clearProducer();
447 void DocClipBase::reloadThumbProducer()
449 if (m_thumbProd && !m_thumbProd->hasProducer())
450 m_thumbProd->setProducer(getProducer());
453 void DocClipBase::deleteProducers()
455 if (m_thumbProd) m_thumbProd->clearProducer();
457 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
458 // Clip is used in timeline, delay producers deletion
459 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
460 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
462 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
463 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
465 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
466 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
470 qDeleteAll(m_baseTrackProducers);
471 qDeleteAll(m_videoTrackProducers);
472 qDeleteAll(m_audioTrackProducers);
473 m_replaceMutex.unlock();
475 m_baseTrackProducers.clear();
476 m_videoTrackProducers.clear();
477 m_audioTrackProducers.clear();
480 void DocClipBase::cleanupProducers()
484 kDebug()<<"----------------------------------------------------------------------------------";
485 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
486 if (m_toDeleteProducers.at(i) != NULL) {
487 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
488 if (props.ref_count() > 2) {
489 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
497 qDeleteAll(m_toDeleteProducers);
498 m_toDeleteProducers.clear();
499 m_replaceMutex.unlock();
503 bool DocClipBase::isClean() const
505 return m_toDeleteProducers.isEmpty();
508 void DocClipBase::setValid()
510 m_placeHolder = false;
513 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
515 if (producer == NULL) return;
517 QMutexLocker locker(&m_producerMutex);
518 m_replaceMutex.lock();
521 QString id = producer->get("id");
522 if (m_placeHolder || !producer->is_valid()) {
523 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
524 producer->set("markup", tmp);
525 producer->set("bgcolour", "0xff0000ff");
526 producer->set("pad", "10");
529 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
530 if (m_clipType != AUDIO) {
531 if (!id.endsWith("_audio"))
532 m_thumbProd->setProducer(producer);
534 else m_thumbProd->setProducer(producer);
537 bool updated = false;
538 if (id.contains('_')) {
539 // this is a subtrack producer, insert it at correct place
540 id = id.section('_', 1);
541 if (id.endsWith("audio")) {
542 int pos = id.section('_', 0, 0).toInt();
543 if (pos >= m_audioTrackProducers.count()) {
544 while (m_audioTrackProducers.count() - 1 < pos) {
545 m_audioTrackProducers.append(NULL);
548 if (m_audioTrackProducers.at(pos) == NULL) {
549 m_audioTrackProducers[pos] = producer;
552 else delete producer;
554 } else if (id.endsWith("video")) {
556 // Keep compatibility with older projects where video only producers were not track specific
557 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
558 if (pos >= m_videoTrackProducers.count()) {
559 while (m_videoTrackProducers.count() - 1 < pos) {
560 m_videoTrackProducers.append(NULL);
563 if (m_videoTrackProducers.at(pos) == NULL) {
564 m_videoTrackProducers[pos] = producer;
567 else delete producer;
570 int pos = id.toInt();
571 if (pos >= m_baseTrackProducers.count()) {
572 while (m_baseTrackProducers.count() - 1 < pos) {
573 m_baseTrackProducers.append(NULL);
576 if (m_baseTrackProducers.at(pos) == NULL) {
577 m_baseTrackProducers[pos] = producer;
580 else delete producer;
582 if (m_baseTrackProducers.isEmpty()) {
583 m_baseTrackProducers.append(producer);
586 else if (m_baseTrackProducers.at(0) == NULL) {
587 m_baseTrackProducers[0] = producer;
590 else delete producer;
592 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
593 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
596 static double getPixelAspect(QMap<QString, QString>& props) {
597 int width = props.value("frame_size").section('x', 0, 0).toInt();
598 int height = props.value("frame_size").section('x', 1, 1).toInt();
599 int aspectNumerator = props.value("force_aspect_num").toInt();
600 int aspectDenominator = props.value("force_aspect_den").toInt();
601 if (aspectDenominator != 0 && width != 0)
602 return double(height) * aspectNumerator / aspectDenominator / width;
607 Mlt::Producer *DocClipBase::audioProducer(int track)
609 QMutexLocker locker(&m_producerMutex);
610 if (m_audioTrackProducers.count() <= track) {
611 while (m_audioTrackProducers.count() - 1 < track) {
612 m_audioTrackProducers.append(NULL);
615 if (m_audioTrackProducers.at(track) == NULL) {
617 for (i = 0; i < m_audioTrackProducers.count(); i++)
618 if (m_audioTrackProducers.at(i) != NULL) break;
620 if (i >= m_audioTrackProducers.count()) {
621 // Could not find a valid producer for that clip
623 base = getProducer();
629 else base = m_audioTrackProducers.at(i);
630 m_audioTrackProducers[track] = cloneProducer(base);
631 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
633 return m_audioTrackProducers.at(track);
637 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
639 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
640 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
641 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
642 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
643 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
644 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
645 if (mute) prod->set("audio_index", -1);
646 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
647 if (blind) prod->set("video_index", -1);
648 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
649 prod->set("id", id.toUtf8().constData());
650 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
651 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
652 if (m_properties.contains("proxy_out")) {
653 // We have a proxy clip, make sure the proxy has same duration as original
654 prod->set("length", m_properties.value("duration").toInt());
655 prod->set("out", m_properties.value("proxy_out").toInt());
660 Mlt::Producer *DocClipBase::videoProducer(int track)
662 QMutexLocker locker(&m_producerMutex);
663 if (m_videoTrackProducers.count() <= track) {
664 while (m_videoTrackProducers.count() - 1 < track) {
665 m_videoTrackProducers.append(NULL);
668 if (m_videoTrackProducers.at(track) == NULL) {
670 for (i = 0; i < m_videoTrackProducers.count(); i++)
671 if (m_videoTrackProducers.at(i) != NULL) break;
673 if (i >= m_videoTrackProducers.count()) {
674 // Could not find a valid producer for that clip
676 base = getProducer();
682 else base = m_videoTrackProducers.at(i);
683 m_videoTrackProducers[track] = cloneProducer(base);
684 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
686 return m_videoTrackProducers.at(track);
689 Mlt::Producer *DocClipBase::getCloneProducer()
691 Mlt::Producer *source = NULL;
692 Mlt::Producer *prod = NULL;
693 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
694 source = getProducer();
695 if (!source) return NULL;
697 if (m_clipType == COLOR) {
698 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
699 } else if (m_clipType == TEXT) {
700 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
701 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
702 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
706 QMutexLocker locker(&m_producerMutex);
707 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
708 if (m_baseTrackProducers.at(i) != NULL) {
709 source = m_baseTrackProducers.at(i);
713 if (!source) return NULL;
715 prod = cloneProducer(source);
718 adjustProducerProperties(prod, getId() + "_", false, false);
719 if (!m_properties.contains("proxy_out")) {
720 // Adjust length in case...
721 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
722 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
724 if (m_clipType == AUDIO) {
725 prod->set("_audioclip", 1);
732 Mlt::Producer *DocClipBase::getProducer(int track)
734 QMutexLocker locker(&m_producerMutex);
735 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
736 if (m_baseTrackProducers.count() == 0) {
739 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
740 if (m_baseTrackProducers.at(i) != NULL) {
741 return m_baseTrackProducers.at(i);
746 if (track >= m_baseTrackProducers.count()) {
747 while (m_baseTrackProducers.count() - 1 < track) {
748 m_baseTrackProducers.append(NULL);
751 if (m_baseTrackProducers.at(track) == NULL) {
753 for (i = 0; i < m_baseTrackProducers.count(); i++)
754 if (m_baseTrackProducers.at(i) != NULL) break;
756 if (i >= m_baseTrackProducers.count()) {
757 // Could not find a valid producer for that clip, check in
760 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
761 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
762 m_baseTrackProducers[track] = prod;
764 return m_baseTrackProducers.at(track);
768 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
770 Mlt::Producer *result = NULL;
771 QString url = QString::fromUtf8(source->get("resource"));
772 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
773 // Xml producer sometimes loses the correct url
774 url = m_properties.value("resource");
776 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
777 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
779 if (result == NULL || !result->is_valid()) {
781 QString txt = "+" + i18n("Missing clip") + ".txt";
782 char *tmp = qstrdup(txt.toUtf8().constData());
783 result = new Mlt::Producer(*source->profile(), tmp);
785 if (result == NULL || !result->is_valid())
786 result = new Mlt::Producer(*(source->profile()), "colour:red");
788 result->set("bgcolour", "0xff0000ff");
789 result->set("pad", "10");
793 /*Mlt::Properties src_props(source->get_properties());
794 Mlt::Properties props(result->get_properties());
795 props.inherit(src_props);*/
799 void DocClipBase::setProducerProperty(const char *name, int data)
801 QMutexLocker locker(&m_producerMutex);
802 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
803 if (m_baseTrackProducers.at(i) != NULL)
804 m_baseTrackProducers[i]->set(name, data);
808 void DocClipBase::setProducerProperty(const char *name, double data)
810 QMutexLocker locker(&m_producerMutex);
811 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
812 if (m_baseTrackProducers.at(i) != NULL)
813 m_baseTrackProducers[i]->set(name, data);
817 void DocClipBase::setProducerProperty(const char *name, const char *data)
819 QMutexLocker locker(&m_producerMutex);
820 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
821 if (m_baseTrackProducers.at(i) != NULL)
822 m_baseTrackProducers[i]->set(name, data);
826 void DocClipBase::resetProducerProperty(const char *name)
828 QMutexLocker locker(&m_producerMutex);
829 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
830 if (m_baseTrackProducers.at(i) != NULL)
831 m_baseTrackProducers[i]->set(name, (const char*) NULL);
835 const char *DocClipBase::producerProperty(const char *name) const
837 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
838 if (m_baseTrackProducers.at(i) != NULL) {
839 return m_baseTrackProducers.at(i)->get(name);
846 void DocClipBase::slotRefreshProducer()
848 if (m_baseTrackProducers.count() == 0) return;
849 if (m_clipType == SLIDESHOW) {
850 setProducerProperty("ttl", getProperty("ttl").toInt());
851 //m_clipProducer->set("id", getProperty("id"));
852 if (!getProperty("animation").isEmpty()) {
853 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
855 Mlt::Filter *filter = clipService.filter(ct);
857 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
859 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
860 clipService.detach(*filter);
862 filter = clipService.filter(ct);
865 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
866 // filter does not exist, create it.
867 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
868 if (filter && filter->is_valid()) {
869 int cycle = getProperty("ttl").toInt();
870 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
871 if (!geometry.isEmpty()) {
872 if (getProperty("animation").contains("low-pass")) {
873 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
874 if (blur && blur->is_valid())
875 clipService.attach(*blur);
877 filter->set("transition.geometry", geometry.toUtf8().data());
878 filter->set("transition.cycle", cycle);
879 clipService.attach(*filter);
884 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
886 Mlt::Filter *filter = clipService.filter(0);
888 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
889 clipService.detach(*filter);
891 filter = clipService.filter(ct);
894 if (getProperty("fade") == "1") {
895 // we want a fade filter effect
896 kDebug() << "//////////// FADE WANTED";
897 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
899 Mlt::Filter *filter = clipService.filter(ct);
901 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
905 filter = clipService.filter(ct);
908 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
909 filter->set("cycle", getProperty("ttl").toInt());
910 filter->set("duration", getProperty("luma_duration").toInt());
911 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
912 if (!getProperty("softness").isEmpty()) {
913 int soft = getProperty("softness").toInt();
914 filter->set("luma.softness", (double) soft / 100.0);
917 // filter does not exist, create it...
918 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
919 filter->set("cycle", getProperty("ttl").toInt());
920 filter->set("duration", getProperty("luma_duration").toInt());
921 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
922 if (!getProperty("softness").isEmpty()) {
923 int soft = getProperty("softness").toInt();
924 filter->set("luma.softness", (double) soft / 100.0);
926 clipService.attach(*filter);
929 kDebug() << "//////////// FADE NOT WANTED!!!";
930 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
932 Mlt::Filter *filter = clipService.filter(0);
934 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
935 clipService.detach(*filter);
937 filter = clipService.filter(ct);
940 if (getProperty("crop") == "1") {
941 // we want a center crop filter effect
942 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
944 Mlt::Filter *filter = clipService.filter(ct);
946 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
950 filter = clipService.filter(ct);
953 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
954 // filter does not exist, create it...
955 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
956 filter->set("center", 1);
957 clipService.attach(*filter);
960 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
962 Mlt::Filter *filter = clipService.filter(0);
964 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
965 clipService.detach(*filter);
967 filter = clipService.filter(ct);
973 void DocClipBase::setProperties(QMap <QString, QString> properties)
975 // changing clip type is not allowed
976 properties.remove("type");
977 QMapIterator<QString, QString> i(properties);
978 bool refreshProducer = false;
980 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
981 QString oldProxy = m_properties.value("proxy");
982 while (i.hasNext()) {
984 setProperty(i.key(), i.value());
985 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
987 if (properties.contains("proxy")) {
988 QString value = properties.value("proxy");
989 // If value is "-", that means user manually disabled proxy on this clip
990 if (value.isEmpty() || value == "-") {
992 emit abortProxy(m_id, oldProxy);
995 emit createProxy(m_id);
998 if (refreshProducer) slotRefreshProducer();
1001 void DocClipBase::setMetadata(QMap <QString, QString> properties, QString tool)
1003 QMapIterator<QString, QString> i(properties);
1004 while (i.hasNext()) {
1006 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
1007 m_metadata.remove(i.key());
1009 m_metadata.insert(i.key(), QStringList() << i.value() << tool);
1014 QMap <QString, QStringList> DocClipBase::metadata() const
1019 void DocClipBase::clearProperty(const QString &key)
1021 m_properties.remove(key);
1024 void DocClipBase::getFileHash(const QString &url)
1026 if (m_clipType == SLIDESHOW) return;
1028 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1029 QByteArray fileData;
1030 QByteArray fileHash;
1031 //kDebug() << "SETTING HASH of" << value;
1032 m_properties.insert("file_size", QString::number(file.size()));
1034 * 1 MB = 1 second per 450 files (or faster)
1035 * 10 MB = 9 seconds per 450 files (or faster)
1037 if (file.size() > 1000000*2) {
1038 fileData = file.read(1000000);
1039 if (file.seek(file.size() - 1000000))
1040 fileData.append(file.readAll());
1042 fileData = file.readAll();
1044 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1045 m_properties.insert("file_hash", QString(fileHash.toHex()));
1049 bool DocClipBase::checkHash() const
1051 KUrl url = fileURL();
1052 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1056 QString DocClipBase::getClipHash() const
1059 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1060 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1061 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1063 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1064 if (hash.isEmpty()) hash = getHash(fileURL().path());
1070 void DocClipBase::setPlaceHolder(bool place)
1072 m_placeHolder = place;
1076 QString DocClipBase::getHash(const QString &path)
1079 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1080 QByteArray fileData;
1081 QByteArray fileHash;
1083 * 1 MB = 1 second per 450 files (or faster)
1084 * 10 MB = 9 seconds per 450 files (or faster)
1086 if (file.size() > 1000000*2) {
1087 fileData = file.read(1000000);
1088 if (file.seek(file.size() - 1000000))
1089 fileData.append(file.readAll());
1091 fileData = file.readAll();
1093 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1098 void DocClipBase::refreshThumbUrl()
1100 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1103 void DocClipBase::setProperty(const QString &key, const QString &value)
1105 m_properties.insert(key, value);
1106 if (key == "resource") {
1108 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1109 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1110 } else if (key == "out") {
1111 setDuration(GenTime(value.toInt() + 1, KdenliveSettings::project_fps()));
1113 else if (key == "colour") {
1114 setProducerProperty("colour", value.toUtf8().data());
1115 } else if (key == "templatetext") {
1116 setProducerProperty("templatetext", value.toUtf8().data());
1117 setProducerProperty("force_reload", 1);
1118 } else if (key == "xmldata") {
1119 setProducerProperty("xmldata", value.toUtf8().data());
1120 setProducerProperty("force_reload", 1);
1121 } else if (key == "force_aspect_num") {
1122 if (value.isEmpty()) {
1123 m_properties.remove("force_aspect_num");
1124 resetProducerProperty("force_aspect_ratio");
1125 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1126 } else if (key == "force_aspect_den") {
1127 if (value.isEmpty()) {
1128 m_properties.remove("force_aspect_den");
1129 resetProducerProperty("force_aspect_ratio");
1130 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1131 } else if (key == "force_fps") {
1132 if (value.isEmpty()) {
1133 m_properties.remove("force_fps");
1134 resetProducerProperty("force_fps");
1135 } else setProducerProperty("force_fps", value.toDouble());
1136 } else if (key == "force_progressive") {
1137 if (value.isEmpty()) {
1138 m_properties.remove("force_progressive");
1139 resetProducerProperty("force_progressive");
1140 } else setProducerProperty("force_progressive", value.toInt());
1141 } else if (key == "force_tff") {
1142 if (value.isEmpty()) {
1143 m_properties.remove("force_tff");
1144 resetProducerProperty("force_tff");
1145 } else setProducerProperty("force_tff", value.toInt());
1146 } else if (key == "threads") {
1147 if (value.isEmpty()) {
1148 m_properties.remove("threads");
1149 setProducerProperty("threads", 1);
1150 } else setProducerProperty("threads", value.toInt());
1151 } else if (key == "video_index") {
1152 if (value.isEmpty()) {
1153 m_properties.remove("video_index");
1154 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1155 } else setProducerProperty("video_index", value.toInt());
1156 } else if (key == "audio_index") {
1157 if (value.isEmpty()) {
1158 m_properties.remove("audio_index");
1159 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1160 } else setProducerProperty("audio_index", value.toInt());
1161 } else if (key == "force_colorspace") {
1162 if (value.isEmpty()) {
1163 m_properties.remove("force_colorspace");
1164 resetProducerProperty("force_colorspace");
1165 } else setProducerProperty("force_colorspace", value.toInt());
1166 } else if (key == "full_luma") {
1167 if (value.isEmpty()) {
1168 m_properties.remove("full_luma");
1169 resetProducerProperty("set.force_full_luma");
1170 } else setProducerProperty("set.force_full_luma", value.toInt());
1174 QMap <QString, QString> DocClipBase::properties() const
1176 return m_properties;
1179 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1181 QMap <QString, QString> currentProps;
1182 QMap<QString, QString>::const_iterator i = props.constBegin();
1183 while (i != props.constEnd()) {
1184 currentProps.insert(i.key(), m_properties.value(i.key()));
1187 return currentProps;
1190 bool DocClipBase::getAudioThumbs()
1192 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1193 if (m_audioThumbCreated) {
1196 m_audioTimer.start();
1200 bool DocClipBase::isPlaceHolder() const
1202 return m_placeHolder;
1205 void DocClipBase::addCutZone(int in, int out, QString desc)
1208 info.zone = QPoint(in, out);
1209 info.description = desc;
1210 for (int i = 0; i < m_cutZones.count(); i++)
1211 if (m_cutZones.at(i).zone == info.zone) {
1214 m_cutZones.append(info);
1217 bool DocClipBase::hasCutZone(QPoint p) const
1219 for (int i = 0; i < m_cutZones.count(); i++)
1220 if (m_cutZones.at(i).zone == p) return true;
1225 void DocClipBase::removeCutZone(int in, int out)
1228 for (int i = 0; i < m_cutZones.count(); i++) {
1229 if (m_cutZones.at(i).zone == p) {
1230 m_cutZones.removeAt(i);
1236 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1238 QPoint old(oldin, oldout);
1239 for (int i = 0; i < m_cutZones.size(); ++i) {
1240 if (m_cutZones.at(i).zone == old) {
1242 info.zone = QPoint(in, out);
1243 info.description = desc;
1244 m_cutZones.replace(i, info);
1250 QList <CutZoneInfo> DocClipBase::cutZones() const
1255 bool DocClipBase::hasVideoCodec(const QString &codec) const
1257 Mlt::Producer *prod = NULL;
1258 if (m_baseTrackProducers.count() == 0) return false;
1259 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1260 if (m_baseTrackProducers.at(i) != NULL) {
1261 prod = m_baseTrackProducers.at(i);
1266 if (!prod) return false;
1267 int default_video = prod->get_int("video_index");
1269 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1270 return prod->get(property) == codec;
1273 bool DocClipBase::hasAudioCodec(const QString &codec) const
1275 Mlt::Producer *prod = NULL;
1276 if (m_baseTrackProducers.count() == 0) return false;
1277 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1278 if (m_baseTrackProducers.at(i) != NULL) {
1279 prod = m_baseTrackProducers.at(i);
1283 if (!prod) return false;
1284 int default_video = prod->get_int("audio_index");
1286 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1287 return prod->get(property) == codec;
1291 void DocClipBase::slotExtractImage(QList <int> frames)
1293 if (m_thumbProd == NULL) return;
1294 m_thumbProd->extractImage(frames);
1297 QImage DocClipBase::extractImage(int frame, int width, int height)
1299 if (m_thumbProd == NULL) return QImage();
1300 QMutexLocker locker(&m_producerMutex);
1301 return m_thumbProd->extractImage(frame, width, height);
1304 void DocClipBase::setAnalysisData(const QString &name, const QString &data, int offset)
1306 if (data.isEmpty()) m_analysisdata.remove(name);
1308 if (m_analysisdata.contains(name)) {
1309 if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip already contains analysis data %1", name), QString(), KGuiItem(i18n("Merge")), KGuiItem(i18n("Add"))) == KMessageBox::Yes) {
1311 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1312 Mlt::Geometry geometry(m_analysisdata.value(name).toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1313 Mlt::Geometry newGeometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1314 Mlt::GeometryItem item;
1316 while (!newGeometry.next_key(&item, pos)) {
1318 item.frame(pos + offset);
1320 geometry.insert(item);
1322 m_analysisdata.insert(name, geometry.serialise());
1325 // Add data with another name
1327 QString newname = name + " " + QString::number(i);
1328 while (m_analysisdata.contains(newname)) {
1330 newname = name + " " + QString::number(i);
1332 m_analysisdata.insert(newname, geometryWithOffset(data, offset));
1335 else m_analysisdata.insert(name, geometryWithOffset(data, offset));
1339 const QString DocClipBase::geometryWithOffset(QString data, int offset)
1341 if (offset == 0) return data;
1342 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1343 Mlt::Geometry geometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1344 Mlt::Geometry newgeometry(NULL, m_properties.value("duration").toInt(), profile->width(), profile->height());
1345 Mlt::GeometryItem item;
1347 while (!geometry.next_key(&item, pos)) {
1349 item.frame(pos + offset);
1351 newgeometry.insert(item);
1353 return newgeometry.serialise();
1356 QMap <QString, QString> DocClipBase::analysisData() const
1358 return m_analysisdata;