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"));
77 if (xml.hasAttribute("cutzones")) {
78 QStringList cuts = xml.attribute("cutzones").split(';', QString::SkipEmptyParts);
79 for (int i = 0; i < cuts.count(); ++i) {
80 QString z = cuts.at(i);
81 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
85 if (xml.hasAttribute("analysisdata")) {
86 QStringList adata = xml.attribute("analysisdata").split('#', QString::SkipEmptyParts);
87 for (int i = 0; i < adata.count(); ++i)
88 m_analysisdata.insert(adata.at(i).section('?', 0, 0), adata.at(i).section('?', 1, 1));
91 KUrl url = KUrl(xml.attribute("resource"));
92 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
94 if (xml.hasAttribute("duration")) {
95 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
97 int out = xml.attribute("out").toInt();
98 int in = xml.attribute("in").toInt();
99 if (out > in) setDuration(GenTime(out - in + 1, KdenliveSettings::project_fps()));
102 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
104 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
106 // Setup timer to trigger audio thumbs creation
107 m_audioTimer.setSingleShot(true);
108 m_audioTimer.setInterval(800);
109 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
113 DocClipBase::~DocClipBase()
118 qDeleteAll(m_toDeleteProducers);
119 m_toDeleteProducers.clear();
120 qDeleteAll(m_baseTrackProducers);
121 m_baseTrackProducers.clear();
122 qDeleteAll(m_audioTrackProducers);
123 m_audioTrackProducers.clear();
124 qDeleteAll(m_videoTrackProducers);
125 m_videoTrackProducers.clear();
128 void DocClipBase::setZone(QPoint zone)
130 m_properties.insert("zone_in", QString::number(zone.x()));
131 m_properties.insert("zone_out", QString::number(zone.y()));
134 QPoint DocClipBase::zone() const
136 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
141 bool DocClipBase::hasAudioThumb() const
143 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
147 void DocClipBase::slotClearAudioCache()
149 audioFrameCache.clear();
150 m_audioThumbCreated = false;
153 /*void DocClipBase::getClipMainThumb() {
154 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
157 KThumb *DocClipBase::thumbProducer()
162 bool DocClipBase::audioThumbCreated() const
164 return m_audioThumbCreated;
167 const QString DocClipBase::name() const
170 return m_properties.value("name");
173 const QString &DocClipBase::getId() const
178 const CLIPTYPE & DocClipBase::clipType() const
183 void DocClipBase::setClipType(CLIPTYPE type)
186 m_properties.insert("type", QString::number((int) type));
189 KUrl DocClipBase::fileURL() const
191 QString res = m_properties.value("resource");
192 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
196 void DocClipBase::setClipThumbFrame(const uint &ix)
198 m_properties.insert("thumbnail", QString::number((int) ix));
201 uint DocClipBase::getClipThumbFrame() const
203 return (uint) m_properties.value("thumbnail").toInt();
206 const QString DocClipBase::description() const
208 return m_properties.value("description");
211 bool DocClipBase::isTransparent() const
213 return (m_properties.value("transparency") == "1");
216 const QString DocClipBase::getProperty(const QString &prop) const
218 return m_properties.value(prop);
221 void DocClipBase::setDuration(const GenTime &dur)
224 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
227 const GenTime &DocClipBase::duration() const
232 const GenTime DocClipBase::maxDuration() const
234 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
235 /*const GenTime dur(15000, KdenliveSettings::project_fps());
242 bool DocClipBase::hasFileSize() const
247 qulonglong DocClipBase::fileSize() const
249 return m_properties.value("file_size").toULongLong();
253 QDomElement DocClipBase::toXML(bool hideTemporaryProperties) const
256 QDomElement clip = doc.createElement("producer");
258 QMapIterator<QString, QString> i(m_properties);
259 while (i.hasNext()) {
261 if (hideTemporaryProperties && i.key().startsWith('_')) continue;
262 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
265 QMapIterator<QString, QStringList> j(m_metadata);
266 // Metadata name can have special chars so we cannot pass it as simple attribute
267 while (j.hasNext()) {
269 if (!j.value().isEmpty()) {
270 QDomElement property = doc.createElement("metaproperty");
271 property.setAttribute("name", "meta.attr." + j.key());
272 QStringList values = j.value();
273 QDomText value = doc.createTextNode(values.at(0));
274 if (values.count() > 1) property.setAttribute("tool", values.at(1));
275 property.appendChild(value);
276 clip.appendChild(property);
279 doc.appendChild(clip);
280 if (!m_cutZones.isEmpty()) {
282 for (int i = 0; i < m_cutZones.size(); ++i) {
283 CutZoneInfo info = m_cutZones.at(i);
284 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
286 clip.setAttribute("cutzones", cuts.join(";"));
289 if (!m_analysisdata.isEmpty()) {
290 QMapIterator<QString, QString> i(m_analysisdata);
291 while (i.hasNext()) {
293 //WARNING: a ? and # separator is not a good idea
294 adata.append(i.key() + "?" + i.value() + "#");
297 clip.setAttribute("analysisdata", adata);
298 //kDebug() << "/// CLIP XML: " << doc.toString();
299 return doc.documentElement();
302 const QString DocClipBase::shortInfo() const
306 if (m_clipType == AV || m_clipType == VIDEO || m_clipType == IMAGE || m_clipType == PLAYLIST) {
307 info = m_properties.value("frame_size") + " ";
308 if (m_properties.contains("fps")) {
309 info.append(i18n("%1 fps", m_properties.value("fps").left(5)));
311 if (!info.simplified().isEmpty()) info.prepend(" - ");
313 else if (m_clipType == AUDIO) {
314 info = " - " + m_properties.value("frequency") + i18n("Hz");
317 switch (m_clipType) {
319 tip.append(i18n("Audio clip") + "</b>" + info + "<br />" + fileURL().path());
322 tip.append(i18n("Mute video clip") + "</b>" + info + "<br />" + fileURL().path());
325 tip.append(i18n("Video clip") + "</b>" + info + "<br />" + fileURL().path());
328 tip.append(i18n("Color clip"));
331 tip.append(i18n("Image clip") + "</b>" + info + "<br />" + fileURL().path());
334 if (!fileURL().isEmpty() && getProperty("xmldata").isEmpty()) tip.append(i18n("Template text clip") + "</b><br />" + fileURL().path());
335 else tip.append(i18n("Text clip") + "</b><br />" + fileURL().path());
338 tip.append(i18n("Slideshow clip") + "</b><br />" + fileURL().directory());
341 tip.append(i18n("Virtual clip"));
344 tip.append(i18n("Playlist clip") + "</b>" + info + "<br />" + fileURL().path());
347 tip.append(i18n("Unknown clip"));
354 void DocClipBase::setAudioThumbCreated(bool isDone)
356 m_audioThumbCreated = isDone;
359 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
361 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
362 audioFrameCache = data;
363 m_audioThumbCreated = true;
367 QList < GenTime > DocClipBase::snapMarkers() const
369 QList < GenTime > markers;
370 for (int count = 0; count < m_snapMarkers.count(); ++count) {
371 markers.append(m_snapMarkers.at(count).time());
377 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
379 return m_snapMarkers;
383 void DocClipBase::addSnapMarker(const CommentedTime &marker)
385 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
386 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
387 if ((*it).time() >= marker.time())
391 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
392 (*it).setComment(marker.comment());
393 (*it).setMarkerType(marker.markerType());
394 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
396 m_snapMarkers.insert(it, marker);
400 void DocClipBase::editSnapMarker(const GenTime & time, const QString &comment)
402 QList < CommentedTime >::Iterator it;
403 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
404 if ((*it).time() == time)
407 if (it != m_snapMarkers.end()) {
408 (*it).setComment(comment);
410 kError() << "trying to edit Snap Marker that does not already exists";
414 QString DocClipBase::deleteSnapMarker(const GenTime & time)
416 QString result = i18n("Marker");
417 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
419 while (itt != m_snapMarkers.end()) {
420 if ((*itt).time() == time)
425 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
426 result = (*itt).comment();
427 m_snapMarkers.erase(itt);
433 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
435 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
437 while (itt != m_snapMarkers.end()) {
438 if ((*itt).time() == time)
446 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
449 for (it = 0; it < m_snapMarkers.count(); it++) {
450 if (m_snapMarkers.at(it).time() >= currTime)
453 if (it == 0) return GenTime();
454 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
455 return m_snapMarkers.at(it).time();
456 else return m_snapMarkers.at(it - 1).time();
459 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
462 for (it = 0; it < m_snapMarkers.count(); it++) {
463 if (m_snapMarkers.at(it).time() > currTime)
466 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
470 QString DocClipBase::markerComment(GenTime t) const
472 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
473 while (itt != m_snapMarkers.end()) {
474 if ((*itt).time() == t)
475 return (*itt).comment();
481 CommentedTime DocClipBase::markerAt(GenTime t) const
483 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
484 while (itt != m_snapMarkers.end()) {
485 if ((*itt).time() == t)
489 return CommentedTime();
492 void DocClipBase::clearThumbProducer()
494 if (m_thumbProd) m_thumbProd->clearProducer();
497 void DocClipBase::reloadThumbProducer()
499 if (m_thumbProd && !m_thumbProd->hasProducer())
500 m_thumbProd->setProducer(getProducer());
503 void DocClipBase::deleteProducers()
505 if (m_thumbProd) m_thumbProd->clearProducer();
507 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
508 // Clip is used in timeline, delay producers deletion
509 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
510 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
512 for (int i = 0; i < m_videoTrackProducers.count(); ++i) {
513 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
515 for (int i = 0; i < m_audioTrackProducers.count(); ++i) {
516 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
520 qDeleteAll(m_baseTrackProducers);
521 qDeleteAll(m_videoTrackProducers);
522 qDeleteAll(m_audioTrackProducers);
523 m_replaceMutex.unlock();
525 m_baseTrackProducers.clear();
526 m_videoTrackProducers.clear();
527 m_audioTrackProducers.clear();
530 void DocClipBase::cleanupProducers()
534 kDebug()<<"----------------------------------------------------------------------------------";
535 for (int i = 0; i < m_toDeleteProducers.count(); ++i) {
536 if (m_toDeleteProducers.at(i) != NULL) {
537 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
538 if (props.ref_count() > 2) {
539 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
547 qDeleteAll(m_toDeleteProducers);
548 m_toDeleteProducers.clear();
549 m_replaceMutex.unlock();
553 bool DocClipBase::isClean() const
555 return m_toDeleteProducers.isEmpty();
558 void DocClipBase::setValid()
560 m_placeHolder = false;
563 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
565 if (producer == NULL) return;
567 QMutexLocker locker(&m_producerMutex);
568 m_replaceMutex.lock();
571 QString id = producer->get("id");
572 if (m_placeHolder || !producer->is_valid()) {
573 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
574 producer->set("markup", tmp);
575 producer->set("bgcolour", "0xff0000ff");
576 producer->set("pad", "10");
579 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
580 if (m_clipType != AUDIO) {
581 if (!id.endsWith("_audio"))
582 m_thumbProd->setProducer(producer);
584 else m_thumbProd->setProducer(producer);
587 bool updated = false;
588 if (id.contains('_')) {
589 // this is a subtrack producer, insert it at correct place
590 id = id.section('_', 1);
591 if (id.endsWith("audio")) {
592 int pos = id.section('_', 0, 0).toInt();
593 if (pos >= m_audioTrackProducers.count()) {
594 while (m_audioTrackProducers.count() - 1 < pos) {
595 m_audioTrackProducers.append(NULL);
598 if (m_audioTrackProducers.at(pos) == NULL) {
599 m_audioTrackProducers[pos] = producer;
602 else delete producer;
604 } else if (id.endsWith("video")) {
606 // Keep compatibility with older projects where video only producers were not track specific
607 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
608 if (pos >= m_videoTrackProducers.count()) {
609 while (m_videoTrackProducers.count() - 1 < pos) {
610 m_videoTrackProducers.append(NULL);
613 if (m_videoTrackProducers.at(pos) == NULL) {
614 m_videoTrackProducers[pos] = producer;
617 else delete producer;
620 int pos = id.toInt();
621 if (pos >= m_baseTrackProducers.count()) {
622 while (m_baseTrackProducers.count() - 1 < pos) {
623 m_baseTrackProducers.append(NULL);
626 if (m_baseTrackProducers.at(pos) == NULL) {
627 m_baseTrackProducers[pos] = producer;
630 else delete producer;
632 if (m_baseTrackProducers.isEmpty()) {
633 m_baseTrackProducers.append(producer);
636 else if (m_baseTrackProducers.at(0) == NULL) {
637 m_baseTrackProducers[0] = producer;
640 else delete producer;
642 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
643 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
646 static double getPixelAspect(QMap<QString, QString>& props) {
647 int width = props.value("frame_size").section('x', 0, 0).toInt();
648 int height = props.value("frame_size").section('x', 1, 1).toInt();
649 int aspectNumerator = props.value("force_aspect_num").toInt();
650 int aspectDenominator = props.value("force_aspect_den").toInt();
651 if (aspectDenominator != 0 && width != 0)
652 return double(height) * aspectNumerator / aspectDenominator / width;
657 Mlt::Producer *DocClipBase::audioProducer(int track)
659 QMutexLocker locker(&m_producerMutex);
660 if (m_audioTrackProducers.count() <= track) {
661 while (m_audioTrackProducers.count() - 1 < track) {
662 m_audioTrackProducers.append(NULL);
665 if (m_audioTrackProducers.at(track) == NULL) {
667 for (i = 0; i < m_audioTrackProducers.count(); ++i)
668 if (m_audioTrackProducers.at(i) != NULL) break;
670 if (i >= m_audioTrackProducers.count()) {
671 // Could not find a valid producer for that clip
673 base = getProducer();
679 else base = m_audioTrackProducers.at(i);
680 m_audioTrackProducers[track] = cloneProducer(base);
681 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
683 return m_audioTrackProducers.at(track);
687 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
689 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
690 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
691 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
692 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
693 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
694 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
695 if (mute) prod->set("audio_index", -1);
696 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
697 if (blind) prod->set("video_index", -1);
698 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
699 prod->set("id", id.toUtf8().constData());
700 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
701 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
702 if (m_properties.contains("proxy_out")) {
703 // We have a proxy clip, make sure the proxy has same duration as original
704 prod->set("length", m_properties.value("duration").toInt());
705 prod->set("out", m_properties.value("proxy_out").toInt());
710 Mlt::Producer *DocClipBase::videoProducer(int track)
712 QMutexLocker locker(&m_producerMutex);
713 if (m_videoTrackProducers.count() <= track) {
714 while (m_videoTrackProducers.count() - 1 < track) {
715 m_videoTrackProducers.append(NULL);
718 if (m_videoTrackProducers.at(track) == NULL) {
720 for (i = 0; i < m_videoTrackProducers.count(); ++i)
721 if (m_videoTrackProducers.at(i) != NULL) break;
723 if (i >= m_videoTrackProducers.count()) {
724 // Could not find a valid producer for that clip
726 base = getProducer();
732 else base = m_videoTrackProducers.at(i);
733 m_videoTrackProducers[track] = cloneProducer(base);
734 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
736 return m_videoTrackProducers.at(track);
739 Mlt::Producer *DocClipBase::getCloneProducer()
741 Mlt::Producer *source = NULL;
742 Mlt::Producer *prod = NULL;
743 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
744 source = getProducer();
745 if (!source) return NULL;
747 if (m_clipType == COLOR) {
748 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
749 } else if (m_clipType == TEXT) {
750 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
751 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
752 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
756 QMutexLocker locker(&m_producerMutex);
757 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
758 if (m_baseTrackProducers.at(i) != NULL) {
759 source = m_baseTrackProducers.at(i);
763 if (!source) return NULL;
765 prod = cloneProducer(source);
768 adjustProducerProperties(prod, getId() + "_", false, false);
769 if (!m_properties.contains("proxy_out")) {
770 // Adjust length in case...
771 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
772 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
774 if (m_clipType == AUDIO) {
775 prod->set("_audioclip", 1);
782 Mlt::Producer *DocClipBase::getProducer(int track)
784 QMutexLocker locker(&m_producerMutex);
785 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
786 if (m_baseTrackProducers.count() == 0) {
789 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
790 if (m_baseTrackProducers.at(i) != NULL) {
791 return m_baseTrackProducers.at(i);
796 if (track >= m_baseTrackProducers.count()) {
797 while (m_baseTrackProducers.count() - 1 < track) {
798 m_baseTrackProducers.append(NULL);
801 if (m_baseTrackProducers.at(track) == NULL) {
803 for (i = 0; i < m_baseTrackProducers.count(); ++i)
804 if (m_baseTrackProducers.at(i) != NULL) break;
806 if (i >= m_baseTrackProducers.count()) {
807 // Could not find a valid producer for that clip, check in
810 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
811 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
812 m_baseTrackProducers[track] = prod;
814 return m_baseTrackProducers.at(track);
818 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
820 Mlt::Producer *result = NULL;
821 QString url = QString::fromUtf8(source->get("resource"));
822 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
823 // Xml producer sometimes loses the correct url
824 url = m_properties.value("resource");
826 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
827 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
829 if (result == NULL || !result->is_valid()) {
831 QString txt = "+" + i18n("Missing clip") + ".txt";
832 char *tmp = qstrdup(txt.toUtf8().constData());
833 result = new Mlt::Producer(*source->profile(), tmp);
835 if (result == NULL || !result->is_valid())
836 result = new Mlt::Producer(*(source->profile()), "colour:red");
838 result->set("bgcolour", "0xff0000ff");
839 result->set("pad", "10");
843 /*Mlt::Properties src_props(source->get_properties());
844 Mlt::Properties props(result->get_properties());
845 props.inherit(src_props);*/
849 void DocClipBase::setProducerProperty(const char *name, int data)
851 QMutexLocker locker(&m_producerMutex);
852 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
853 if (m_baseTrackProducers.at(i) != NULL)
854 m_baseTrackProducers[i]->set(name, data);
858 void DocClipBase::setProducerProperty(const char *name, double data)
860 QMutexLocker locker(&m_producerMutex);
861 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
862 if (m_baseTrackProducers.at(i) != NULL)
863 m_baseTrackProducers[i]->set(name, data);
867 void DocClipBase::setProducerProperty(const char *name, const char *data)
869 QMutexLocker locker(&m_producerMutex);
870 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
871 if (m_baseTrackProducers.at(i) != NULL)
872 m_baseTrackProducers[i]->set(name, data);
876 void DocClipBase::resetProducerProperty(const char *name)
878 QMutexLocker locker(&m_producerMutex);
879 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
880 if (m_baseTrackProducers.at(i) != NULL)
881 m_baseTrackProducers[i]->set(name, (const char*) NULL);
885 const char *DocClipBase::producerProperty(const char *name) const
887 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
888 if (m_baseTrackProducers.at(i) != NULL) {
889 return m_baseTrackProducers.at(i)->get(name);
896 void DocClipBase::slotRefreshProducer()
898 if (m_baseTrackProducers.count() == 0) return;
899 if (m_clipType == SLIDESHOW) {
900 setProducerProperty("ttl", getProperty("ttl").toInt());
901 //m_clipProducer->set("id", getProperty("id"));
902 if (!getProperty("animation").isEmpty()) {
903 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
905 Mlt::Filter *filter = clipService.filter(ct);
907 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
909 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
910 clipService.detach(*filter);
912 filter = clipService.filter(ct);
915 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
916 // filter does not exist, create it.
917 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
918 if (filter && filter->is_valid()) {
919 int cycle = getProperty("ttl").toInt();
920 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
921 if (!geometry.isEmpty()) {
922 if (getProperty("animation").contains("low-pass")) {
923 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
924 if (blur && blur->is_valid())
925 clipService.attach(*blur);
927 filter->set("transition.geometry", geometry.toUtf8().data());
928 filter->set("transition.cycle", cycle);
929 clipService.attach(*filter);
934 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
936 Mlt::Filter *filter = clipService.filter(0);
938 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
939 clipService.detach(*filter);
941 filter = clipService.filter(ct);
944 if (getProperty("fade") == "1") {
945 // we want a fade filter effect
946 kDebug() << "//////////// FADE WANTED";
947 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
949 Mlt::Filter *filter = clipService.filter(ct);
951 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
955 filter = clipService.filter(ct);
958 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
959 filter->set("cycle", getProperty("ttl").toInt());
960 filter->set("duration", getProperty("luma_duration").toInt());
961 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
962 if (!getProperty("softness").isEmpty()) {
963 int soft = getProperty("softness").toInt();
964 filter->set("luma.softness", (double) soft / 100.0);
967 // filter does not exist, create it...
968 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
969 filter->set("cycle", getProperty("ttl").toInt());
970 filter->set("duration", getProperty("luma_duration").toInt());
971 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
972 if (!getProperty("softness").isEmpty()) {
973 int soft = getProperty("softness").toInt();
974 filter->set("luma.softness", (double) soft / 100.0);
976 clipService.attach(*filter);
979 kDebug() << "//////////// FADE NOT WANTED!!!";
980 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
982 Mlt::Filter *filter = clipService.filter(0);
984 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
985 clipService.detach(*filter);
987 filter = clipService.filter(ct);
990 if (getProperty("crop") == "1") {
991 // we want a center crop filter effect
992 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
994 Mlt::Filter *filter = clipService.filter(ct);
996 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
1000 filter = clipService.filter(ct);
1003 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
1004 // filter does not exist, create it...
1005 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
1006 filter->set("center", 1);
1007 clipService.attach(*filter);
1010 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
1012 Mlt::Filter *filter = clipService.filter(0);
1014 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
1015 clipService.detach(*filter);
1017 filter = clipService.filter(ct);
1023 void DocClipBase::setProperties(QMap <QString, QString> properties)
1025 // changing clip type is not allowed
1026 properties.remove("type");
1027 QMapIterator<QString, QString> i(properties);
1028 bool refreshProducer = false;
1030 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
1031 QString oldProxy = m_properties.value("proxy");
1032 while (i.hasNext()) {
1034 setProperty(i.key(), i.value());
1035 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
1037 if (properties.contains("proxy")) {
1038 QString value = properties.value("proxy");
1039 // If value is "-", that means user manually disabled proxy on this clip
1040 if (value.isEmpty() || value == "-") {
1042 emit abortProxy(m_id, oldProxy);
1045 emit createProxy(m_id);
1048 if (refreshProducer) slotRefreshProducer();
1051 void DocClipBase::setMetadata(QMap <QString, QString> properties, QString tool)
1053 QMapIterator<QString, QString> i(properties);
1054 while (i.hasNext()) {
1056 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
1057 m_metadata.remove(i.key());
1059 m_metadata.insert(i.key(), QStringList() << i.value() << tool);
1064 QMap <QString, QStringList> DocClipBase::metadata() const
1069 void DocClipBase::clearProperty(const QString &key)
1071 m_properties.remove(key);
1074 void DocClipBase::getFileHash(const QString &url)
1076 if (m_clipType == SLIDESHOW) return;
1078 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1079 QByteArray fileData;
1080 QByteArray fileHash;
1081 //kDebug() << "SETTING HASH of" << value;
1082 m_properties.insert("file_size", QString::number(file.size()));
1084 * 1 MB = 1 second per 450 files (or faster)
1085 * 10 MB = 9 seconds per 450 files (or faster)
1087 if (file.size() > 1000000*2) {
1088 fileData = file.read(1000000);
1089 if (file.seek(file.size() - 1000000))
1090 fileData.append(file.readAll());
1092 fileData = file.readAll();
1094 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1095 m_properties.insert("file_hash", QString(fileHash.toHex()));
1099 bool DocClipBase::checkHash() const
1101 KUrl url = fileURL();
1102 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1106 QString DocClipBase::getClipHash() const
1109 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1110 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1111 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1113 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1114 if (hash.isEmpty()) hash = getHash(fileURL().path());
1120 void DocClipBase::setPlaceHolder(bool place)
1122 m_placeHolder = place;
1126 QString DocClipBase::getHash(const QString &path)
1129 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1130 QByteArray fileData;
1131 QByteArray fileHash;
1133 * 1 MB = 1 second per 450 files (or faster)
1134 * 10 MB = 9 seconds per 450 files (or faster)
1136 if (file.size() > 1000000*2) {
1137 fileData = file.read(1000000);
1138 if (file.seek(file.size() - 1000000))
1139 fileData.append(file.readAll());
1141 fileData = file.readAll();
1143 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1148 void DocClipBase::refreshThumbUrl()
1150 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1153 void DocClipBase::setProperty(const QString &key, const QString &value)
1155 m_properties.insert(key, value);
1156 if (key == "resource") {
1158 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1159 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1160 } else if (key == "out") {
1161 setDuration(GenTime(value.toInt() + 1, KdenliveSettings::project_fps()));
1163 else if (key == "colour") {
1164 setProducerProperty("colour", value.toUtf8().data());
1165 } else if (key == "templatetext") {
1166 setProducerProperty("templatetext", value.toUtf8().data());
1167 setProducerProperty("force_reload", 1);
1168 } else if (key == "xmldata") {
1169 setProducerProperty("xmldata", value.toUtf8().data());
1170 setProducerProperty("force_reload", 1);
1171 } else if (key == "force_aspect_num") {
1172 if (value.isEmpty()) {
1173 m_properties.remove("force_aspect_num");
1174 resetProducerProperty("force_aspect_ratio");
1175 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1176 } else if (key == "force_aspect_den") {
1177 if (value.isEmpty()) {
1178 m_properties.remove("force_aspect_den");
1179 resetProducerProperty("force_aspect_ratio");
1180 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1181 } else if (key == "force_fps") {
1182 if (value.isEmpty()) {
1183 m_properties.remove("force_fps");
1184 resetProducerProperty("force_fps");
1185 } else setProducerProperty("force_fps", value.toDouble());
1186 } else if (key == "force_progressive") {
1187 if (value.isEmpty()) {
1188 m_properties.remove("force_progressive");
1189 resetProducerProperty("force_progressive");
1190 } else setProducerProperty("force_progressive", value.toInt());
1191 } else if (key == "force_tff") {
1192 if (value.isEmpty()) {
1193 m_properties.remove("force_tff");
1194 resetProducerProperty("force_tff");
1195 } else setProducerProperty("force_tff", value.toInt());
1196 } else if (key == "threads") {
1197 if (value.isEmpty()) {
1198 m_properties.remove("threads");
1199 setProducerProperty("threads", 1);
1200 } else setProducerProperty("threads", value.toInt());
1201 } else if (key == "video_index") {
1202 if (value.isEmpty()) {
1203 m_properties.remove("video_index");
1204 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1205 } else setProducerProperty("video_index", value.toInt());
1206 } else if (key == "audio_index") {
1207 if (value.isEmpty()) {
1208 m_properties.remove("audio_index");
1209 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1210 } else setProducerProperty("audio_index", value.toInt());
1211 } else if (key == "force_colorspace") {
1212 if (value.isEmpty()) {
1213 m_properties.remove("force_colorspace");
1214 resetProducerProperty("force_colorspace");
1215 } else setProducerProperty("force_colorspace", value.toInt());
1216 } else if (key == "full_luma") {
1217 if (value.isEmpty()) {
1218 m_properties.remove("full_luma");
1219 resetProducerProperty("set.force_full_luma");
1220 } else setProducerProperty("set.force_full_luma", value.toInt());
1224 QMap <QString, QString> DocClipBase::properties() const
1226 return m_properties;
1229 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1231 QMap <QString, QString> currentProps;
1232 QMap<QString, QString>::const_iterator i = props.constBegin();
1233 while (i != props.constEnd()) {
1234 currentProps.insert(i.key(), m_properties.value(i.key()));
1237 return currentProps;
1240 bool DocClipBase::getAudioThumbs()
1242 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1243 if (m_audioThumbCreated) {
1246 m_audioTimer.start();
1250 bool DocClipBase::isPlaceHolder() const
1252 return m_placeHolder;
1255 void DocClipBase::addCutZone(int in, int out, QString desc)
1258 info.zone = QPoint(in, out);
1259 info.description = desc;
1260 for (int i = 0; i < m_cutZones.count(); ++i)
1261 if (m_cutZones.at(i).zone == info.zone) {
1264 m_cutZones.append(info);
1267 bool DocClipBase::hasCutZone(QPoint p) const
1269 for (int i = 0; i < m_cutZones.count(); ++i)
1270 if (m_cutZones.at(i).zone == p) return true;
1275 void DocClipBase::removeCutZone(int in, int out)
1278 for (int i = 0; i < m_cutZones.count(); ++i) {
1279 if (m_cutZones.at(i).zone == p) {
1280 m_cutZones.removeAt(i);
1286 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1288 QPoint old(oldin, oldout);
1289 for (int i = 0; i < m_cutZones.size(); ++i) {
1290 if (m_cutZones.at(i).zone == old) {
1292 info.zone = QPoint(in, out);
1293 info.description = desc;
1294 m_cutZones.replace(i, info);
1300 QList <CutZoneInfo> DocClipBase::cutZones() const
1305 bool DocClipBase::hasVideoCodec(const QString &codec) const
1307 Mlt::Producer *prod = NULL;
1308 if (m_baseTrackProducers.count() == 0) return false;
1309 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
1310 if (m_baseTrackProducers.at(i) != NULL) {
1311 prod = m_baseTrackProducers.at(i);
1316 if (!prod) return false;
1317 int default_video = prod->get_int("video_index");
1319 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1320 return prod->get(property) == codec;
1323 bool DocClipBase::hasAudioCodec(const QString &codec) const
1325 Mlt::Producer *prod = NULL;
1326 if (m_baseTrackProducers.count() == 0) return false;
1327 for (int i = 0; i < m_baseTrackProducers.count(); ++i) {
1328 if (m_baseTrackProducers.at(i) != NULL) {
1329 prod = m_baseTrackProducers.at(i);
1333 if (!prod) return false;
1334 int default_video = prod->get_int("audio_index");
1336 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1337 return prod->get(property) == codec;
1341 void DocClipBase::slotExtractImage(QList <int> frames)
1343 if (m_thumbProd == NULL) return;
1344 m_thumbProd->extractImage(frames);
1347 QImage DocClipBase::extractImage(int frame, int width, int height)
1349 if (m_thumbProd == NULL) return QImage();
1350 QMutexLocker locker(&m_producerMutex);
1351 return m_thumbProd->extractImage(frame, width, height);
1354 void DocClipBase::setAnalysisData(const QString &name, const QString &data, int offset)
1356 if (data.isEmpty()) m_analysisdata.remove(name);
1358 if (m_analysisdata.contains(name)) {
1359 if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip already contains analysis data %1", name), QString(), KGuiItem(i18n("Merge")), KGuiItem(i18n("Add"))) == KMessageBox::Yes) {
1361 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1362 Mlt::Geometry geometry(m_analysisdata.value(name).toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1363 Mlt::Geometry newGeometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1364 Mlt::GeometryItem item;
1366 while (!newGeometry.next_key(&item, pos)) {
1368 item.frame(pos + offset);
1370 geometry.insert(item);
1372 m_analysisdata.insert(name, geometry.serialise());
1375 // Add data with another name
1377 QString newname = name + " " + QString::number(i);
1378 while (m_analysisdata.contains(newname)) {
1380 newname = name + " " + QString::number(i);
1382 m_analysisdata.insert(newname, geometryWithOffset(data, offset));
1385 else m_analysisdata.insert(name, geometryWithOffset(data, offset));
1389 const QString DocClipBase::geometryWithOffset(QString data, int offset)
1391 if (offset == 0) return data;
1392 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1393 Mlt::Geometry geometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1394 Mlt::Geometry newgeometry(NULL, m_properties.value("duration").toInt(), profile->width(), profile->height());
1395 Mlt::GeometryItem item;
1397 while (!geometry.next_key(&item, pos)) {
1399 item.frame(pos + offset);
1401 newgeometry.insert(item);
1403 return newgeometry.serialise();
1406 QMap <QString, QString> DocClipBase::analysisData() const
1408 return m_analysisdata;
1412 #include "docclipbase.moc"