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), 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), e.firstChild().nodeValue());
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, QString> 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 QDomText value = doc.createTextNode(j.value());
274 property.appendChild(value);
275 clip.appendChild(property);
278 doc.appendChild(clip);
279 if (!m_cutZones.isEmpty()) {
281 for (int i = 0; i < m_cutZones.size(); i++) {
282 CutZoneInfo info = m_cutZones.at(i);
283 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
285 clip.setAttribute("cutzones", cuts.join(";"));
288 if (!m_analysisdata.isEmpty()) {
289 QMapIterator<QString, QString> i(m_analysisdata);
290 while (i.hasNext()) {
292 //WARNING: a ? and # separator is not a good idea
293 adata.append(i.key() + "?" + i.value() + "#");
296 clip.setAttribute("analysisdata", adata);
297 //kDebug() << "/// CLIP XML: " << doc.toString();
298 return doc.documentElement();
302 void DocClipBase::setAudioThumbCreated(bool isDone)
304 m_audioThumbCreated = isDone;
307 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
309 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
310 audioFrameCache = data;
311 m_audioThumbCreated = true;
315 QList < GenTime > DocClipBase::snapMarkers() const
317 QList < GenTime > markers;
318 for (int count = 0; count < m_snapMarkers.count(); ++count) {
319 markers.append(m_snapMarkers.at(count).time());
325 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
327 return m_snapMarkers;
331 void DocClipBase::addSnapMarker(const CommentedTime marker)
333 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
334 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
335 if ((*it).time() >= marker.time())
339 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
340 (*it).setComment(marker.comment());
341 (*it).setMarkerType(marker.markerType());
342 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
344 m_snapMarkers.insert(it, marker);
348 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
350 QList < CommentedTime >::Iterator it;
351 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
352 if ((*it).time() == time)
355 if (it != m_snapMarkers.end()) {
356 (*it).setComment(comment);
358 kError() << "trying to edit Snap Marker that does not already exists";
362 QString DocClipBase::deleteSnapMarker(const GenTime & time)
364 QString result = i18n("Marker");
365 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
367 while (itt != m_snapMarkers.end()) {
368 if ((*itt).time() == time)
373 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
374 result = (*itt).comment();
375 m_snapMarkers.erase(itt);
381 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
383 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
385 while (itt != m_snapMarkers.end()) {
386 if ((*itt).time() == time)
394 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
397 for (it = 0; it < m_snapMarkers.count(); it++) {
398 if (m_snapMarkers.at(it).time() >= currTime)
401 if (it == 0) return GenTime();
402 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
403 return m_snapMarkers.at(it).time();
404 else return m_snapMarkers.at(it - 1).time();
407 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
410 for (it = 0; it < m_snapMarkers.count(); it++) {
411 if (m_snapMarkers.at(it).time() > currTime)
414 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
418 QString DocClipBase::markerComment(GenTime t) const
420 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
421 while (itt != m_snapMarkers.end()) {
422 if ((*itt).time() == t)
423 return (*itt).comment();
429 CommentedTime DocClipBase::markerAt(GenTime t) const
431 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
432 while (itt != m_snapMarkers.end()) {
433 if ((*itt).time() == t)
437 return CommentedTime();
440 void DocClipBase::clearThumbProducer()
442 if (m_thumbProd) m_thumbProd->clearProducer();
445 void DocClipBase::reloadThumbProducer()
447 if (m_thumbProd && !m_thumbProd->hasProducer())
448 m_thumbProd->setProducer(getProducer());
451 void DocClipBase::deleteProducers()
453 if (m_thumbProd) m_thumbProd->clearProducer();
455 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
456 // Clip is used in timeline, delay producers deletion
457 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
458 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
460 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
461 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
463 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
464 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
468 qDeleteAll(m_baseTrackProducers);
469 qDeleteAll(m_videoTrackProducers);
470 qDeleteAll(m_audioTrackProducers);
471 m_replaceMutex.unlock();
473 m_baseTrackProducers.clear();
474 m_videoTrackProducers.clear();
475 m_audioTrackProducers.clear();
478 void DocClipBase::cleanupProducers()
482 kDebug()<<"----------------------------------------------------------------------------------";
483 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
484 if (m_toDeleteProducers.at(i) != NULL) {
485 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
486 if (props.ref_count() > 2) {
487 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
495 qDeleteAll(m_toDeleteProducers);
496 m_toDeleteProducers.clear();
497 m_replaceMutex.unlock();
501 bool DocClipBase::isClean() const
503 return m_toDeleteProducers.isEmpty();
506 void DocClipBase::setValid()
508 m_placeHolder = false;
511 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
513 if (producer == NULL) return;
515 QMutexLocker locker(&m_producerMutex);
516 m_replaceMutex.lock();
519 QString id = producer->get("id");
520 if (m_placeHolder || !producer->is_valid()) {
521 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
522 producer->set("markup", tmp);
523 producer->set("bgcolour", "0xff0000ff");
524 producer->set("pad", "10");
527 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
528 if (m_clipType != AUDIO) {
529 if (!id.endsWith("_audio"))
530 m_thumbProd->setProducer(producer);
532 else m_thumbProd->setProducer(producer);
535 bool updated = false;
536 if (id.contains('_')) {
537 // this is a subtrack producer, insert it at correct place
538 id = id.section('_', 1);
539 if (id.endsWith("audio")) {
540 int pos = id.section('_', 0, 0).toInt();
541 if (pos >= m_audioTrackProducers.count()) {
542 while (m_audioTrackProducers.count() - 1 < pos) {
543 m_audioTrackProducers.append(NULL);
546 if (m_audioTrackProducers.at(pos) == NULL) {
547 m_audioTrackProducers[pos] = producer;
550 else delete producer;
552 } else if (id.endsWith("video")) {
554 // Keep compatibility with older projects where video only producers were not track specific
555 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
556 if (pos >= m_videoTrackProducers.count()) {
557 while (m_videoTrackProducers.count() - 1 < pos) {
558 m_videoTrackProducers.append(NULL);
561 if (m_videoTrackProducers.at(pos) == NULL) {
562 m_videoTrackProducers[pos] = producer;
565 else delete producer;
568 int pos = id.toInt();
569 if (pos >= m_baseTrackProducers.count()) {
570 while (m_baseTrackProducers.count() - 1 < pos) {
571 m_baseTrackProducers.append(NULL);
574 if (m_baseTrackProducers.at(pos) == NULL) {
575 m_baseTrackProducers[pos] = producer;
578 else delete producer;
580 if (m_baseTrackProducers.isEmpty()) {
581 m_baseTrackProducers.append(producer);
584 else if (m_baseTrackProducers.at(0) == NULL) {
585 m_baseTrackProducers[0] = producer;
588 else delete producer;
590 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
591 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
594 static double getPixelAspect(QMap<QString, QString>& props) {
595 int width = props.value("frame_size").section('x', 0, 0).toInt();
596 int height = props.value("frame_size").section('x', 1, 1).toInt();
597 int aspectNumerator = props.value("force_aspect_num").toInt();
598 int aspectDenominator = props.value("force_aspect_den").toInt();
599 if (aspectDenominator != 0 && width != 0)
600 return double(height) * aspectNumerator / aspectDenominator / width;
605 Mlt::Producer *DocClipBase::audioProducer(int track)
607 QMutexLocker locker(&m_producerMutex);
608 if (m_audioTrackProducers.count() <= track) {
609 while (m_audioTrackProducers.count() - 1 < track) {
610 m_audioTrackProducers.append(NULL);
613 if (m_audioTrackProducers.at(track) == NULL) {
615 for (i = 0; i < m_audioTrackProducers.count(); i++)
616 if (m_audioTrackProducers.at(i) != NULL) break;
618 if (i >= m_audioTrackProducers.count()) {
619 // Could not find a valid producer for that clip
621 base = getProducer();
627 else base = m_audioTrackProducers.at(i);
628 m_audioTrackProducers[track] = cloneProducer(base);
629 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
631 return m_audioTrackProducers.at(track);
635 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
637 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
638 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
639 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
640 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
641 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
642 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
643 if (mute) prod->set("audio_index", -1);
644 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
645 if (blind) prod->set("video_index", -1);
646 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
647 prod->set("id", id.toUtf8().constData());
648 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
649 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
650 if (m_properties.contains("proxy_out")) {
651 // We have a proxy clip, make sure the proxy has same duration as original
652 prod->set("length", m_properties.value("duration").toInt());
653 prod->set("out", m_properties.value("proxy_out").toInt());
658 Mlt::Producer *DocClipBase::videoProducer(int track)
660 QMutexLocker locker(&m_producerMutex);
661 if (m_videoTrackProducers.count() <= track) {
662 while (m_videoTrackProducers.count() - 1 < track) {
663 m_videoTrackProducers.append(NULL);
666 if (m_videoTrackProducers.at(track) == NULL) {
668 for (i = 0; i < m_videoTrackProducers.count(); i++)
669 if (m_videoTrackProducers.at(i) != NULL) break;
671 if (i >= m_videoTrackProducers.count()) {
672 // Could not find a valid producer for that clip
674 base = getProducer();
680 else base = m_videoTrackProducers.at(i);
681 m_videoTrackProducers[track] = cloneProducer(base);
682 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
684 return m_videoTrackProducers.at(track);
687 Mlt::Producer *DocClipBase::getCloneProducer()
689 Mlt::Producer *source = NULL;
690 Mlt::Producer *prod = NULL;
691 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
692 source = getProducer();
693 if (!source) return NULL;
695 if (m_clipType == COLOR) {
696 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
697 } else if (m_clipType == TEXT) {
698 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
699 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
700 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
704 QMutexLocker locker(&m_producerMutex);
705 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
706 if (m_baseTrackProducers.at(i) != NULL) {
707 source = m_baseTrackProducers.at(i);
711 if (!source) return NULL;
713 prod = cloneProducer(source);
716 adjustProducerProperties(prod, getId() + "_", false, false);
717 if (!m_properties.contains("proxy_out")) {
718 // Adjust length in case...
719 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
720 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
722 if (m_clipType == AUDIO) {
723 prod->set("_audioclip", 1);
730 Mlt::Producer *DocClipBase::getProducer(int track)
732 QMutexLocker locker(&m_producerMutex);
733 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
734 if (m_baseTrackProducers.count() == 0) {
737 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
738 if (m_baseTrackProducers.at(i) != NULL) {
739 return m_baseTrackProducers.at(i);
744 if (track >= m_baseTrackProducers.count()) {
745 while (m_baseTrackProducers.count() - 1 < track) {
746 m_baseTrackProducers.append(NULL);
749 if (m_baseTrackProducers.at(track) == NULL) {
751 for (i = 0; i < m_baseTrackProducers.count(); i++)
752 if (m_baseTrackProducers.at(i) != NULL) break;
754 if (i >= m_baseTrackProducers.count()) {
755 // Could not find a valid producer for that clip, check in
758 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
759 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
760 m_baseTrackProducers[track] = prod;
762 return m_baseTrackProducers.at(track);
766 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
768 Mlt::Producer *result = NULL;
769 QString url = QString::fromUtf8(source->get("resource"));
770 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
771 // Xml producer sometimes loses the correct url
772 url = m_properties.value("resource");
774 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
775 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
777 if (result == NULL || !result->is_valid()) {
779 QString txt = "+" + i18n("Missing clip") + ".txt";
780 char *tmp = qstrdup(txt.toUtf8().constData());
781 result = new Mlt::Producer(*source->profile(), tmp);
783 if (result == NULL || !result->is_valid())
784 result = new Mlt::Producer(*(source->profile()), "colour:red");
786 result->set("bgcolour", "0xff0000ff");
787 result->set("pad", "10");
791 /*Mlt::Properties src_props(source->get_properties());
792 Mlt::Properties props(result->get_properties());
793 props.inherit(src_props);*/
797 void DocClipBase::setProducerProperty(const char *name, int data)
799 QMutexLocker locker(&m_producerMutex);
800 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
801 if (m_baseTrackProducers.at(i) != NULL)
802 m_baseTrackProducers[i]->set(name, data);
806 void DocClipBase::setProducerProperty(const char *name, double data)
808 QMutexLocker locker(&m_producerMutex);
809 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
810 if (m_baseTrackProducers.at(i) != NULL)
811 m_baseTrackProducers[i]->set(name, data);
815 void DocClipBase::setProducerProperty(const char *name, const char *data)
817 QMutexLocker locker(&m_producerMutex);
818 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
819 if (m_baseTrackProducers.at(i) != NULL)
820 m_baseTrackProducers[i]->set(name, data);
824 void DocClipBase::resetProducerProperty(const char *name)
826 QMutexLocker locker(&m_producerMutex);
827 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
828 if (m_baseTrackProducers.at(i) != NULL)
829 m_baseTrackProducers[i]->set(name, (const char*) NULL);
833 const char *DocClipBase::producerProperty(const char *name) const
835 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
836 if (m_baseTrackProducers.at(i) != NULL) {
837 return m_baseTrackProducers.at(i)->get(name);
844 void DocClipBase::slotRefreshProducer()
846 if (m_baseTrackProducers.count() == 0) return;
847 if (m_clipType == SLIDESHOW) {
848 setProducerProperty("ttl", getProperty("ttl").toInt());
849 //m_clipProducer->set("id", getProperty("id"));
850 if (!getProperty("animation").isEmpty()) {
851 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
853 Mlt::Filter *filter = clipService.filter(ct);
855 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
857 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
858 clipService.detach(*filter);
860 filter = clipService.filter(ct);
863 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
864 // filter does not exist, create it.
865 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
866 if (filter && filter->is_valid()) {
867 int cycle = getProperty("ttl").toInt();
868 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
869 if (!geometry.isEmpty()) {
870 if (getProperty("animation").contains("low-pass")) {
871 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
872 if (blur && blur->is_valid())
873 clipService.attach(*blur);
875 filter->set("transition.geometry", geometry.toUtf8().data());
876 filter->set("transition.cycle", cycle);
877 clipService.attach(*filter);
882 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
884 Mlt::Filter *filter = clipService.filter(0);
886 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
887 clipService.detach(*filter);
889 filter = clipService.filter(ct);
892 if (getProperty("fade") == "1") {
893 // we want a fade filter effect
894 kDebug() << "//////////// FADE WANTED";
895 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
897 Mlt::Filter *filter = clipService.filter(ct);
899 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
903 filter = clipService.filter(ct);
906 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
907 filter->set("cycle", getProperty("ttl").toInt());
908 filter->set("duration", getProperty("luma_duration").toInt());
909 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
910 if (!getProperty("softness").isEmpty()) {
911 int soft = getProperty("softness").toInt();
912 filter->set("luma.softness", (double) soft / 100.0);
915 // filter does not exist, create it...
916 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
917 filter->set("cycle", getProperty("ttl").toInt());
918 filter->set("duration", getProperty("luma_duration").toInt());
919 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
920 if (!getProperty("softness").isEmpty()) {
921 int soft = getProperty("softness").toInt();
922 filter->set("luma.softness", (double) soft / 100.0);
924 clipService.attach(*filter);
927 kDebug() << "//////////// FADE NOT WANTED!!!";
928 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
930 Mlt::Filter *filter = clipService.filter(0);
932 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
933 clipService.detach(*filter);
935 filter = clipService.filter(ct);
938 if (getProperty("crop") == "1") {
939 // we want a center crop filter effect
940 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
942 Mlt::Filter *filter = clipService.filter(ct);
944 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
948 filter = clipService.filter(ct);
951 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
952 // filter does not exist, create it...
953 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
954 filter->set("center", 1);
955 clipService.attach(*filter);
958 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
960 Mlt::Filter *filter = clipService.filter(0);
962 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
963 clipService.detach(*filter);
965 filter = clipService.filter(ct);
971 void DocClipBase::setProperties(QMap <QString, QString> properties)
973 // changing clip type is not allowed
974 properties.remove("type");
975 QMapIterator<QString, QString> i(properties);
976 bool refreshProducer = false;
978 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
979 QString oldProxy = m_properties.value("proxy");
980 while (i.hasNext()) {
982 setProperty(i.key(), i.value());
983 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
985 if (properties.contains("proxy")) {
986 QString value = properties.value("proxy");
987 // If value is "-", that means user manually disabled proxy on this clip
988 if (value.isEmpty() || value == "-") {
990 emit abortProxy(m_id, oldProxy);
993 emit createProxy(m_id);
996 if (refreshProducer) slotRefreshProducer();
999 void DocClipBase::setMetadata(QMap <QString, QString> properties)
1001 QMapIterator<QString, QString> i(properties);
1002 while (i.hasNext()) {
1004 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
1005 m_metadata.remove(i.key());
1007 m_metadata.insert(i.key(), i.value());
1012 QMap <QString, QString> DocClipBase::metadata() const
1017 void DocClipBase::clearProperty(const QString &key)
1019 m_properties.remove(key);
1022 void DocClipBase::getFileHash(const QString &url)
1024 if (m_clipType == SLIDESHOW) return;
1026 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1027 QByteArray fileData;
1028 QByteArray fileHash;
1029 //kDebug() << "SETTING HASH of" << value;
1030 m_properties.insert("file_size", QString::number(file.size()));
1032 * 1 MB = 1 second per 450 files (or faster)
1033 * 10 MB = 9 seconds per 450 files (or faster)
1035 if (file.size() > 1000000*2) {
1036 fileData = file.read(1000000);
1037 if (file.seek(file.size() - 1000000))
1038 fileData.append(file.readAll());
1040 fileData = file.readAll();
1042 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1043 m_properties.insert("file_hash", QString(fileHash.toHex()));
1047 bool DocClipBase::checkHash() const
1049 KUrl url = fileURL();
1050 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1054 QString DocClipBase::getClipHash() const
1057 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1058 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1059 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1061 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1062 if (hash.isEmpty()) hash = getHash(fileURL().path());
1068 void DocClipBase::setPlaceHolder(bool place)
1070 m_placeHolder = place;
1074 QString DocClipBase::getHash(const QString &path)
1077 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1078 QByteArray fileData;
1079 QByteArray fileHash;
1081 * 1 MB = 1 second per 450 files (or faster)
1082 * 10 MB = 9 seconds per 450 files (or faster)
1084 if (file.size() > 1000000*2) {
1085 fileData = file.read(1000000);
1086 if (file.seek(file.size() - 1000000))
1087 fileData.append(file.readAll());
1089 fileData = file.readAll();
1091 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1096 void DocClipBase::refreshThumbUrl()
1098 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1101 void DocClipBase::setProperty(const QString &key, const QString &value)
1103 m_properties.insert(key, value);
1104 if (key == "resource") {
1106 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1107 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1108 } else if (key == "out") {
1109 setDuration(GenTime(value.toInt() + 1, KdenliveSettings::project_fps()));
1111 else if (key == "colour") {
1112 setProducerProperty("colour", value.toUtf8().data());
1113 } else if (key == "templatetext") {
1114 setProducerProperty("templatetext", value.toUtf8().data());
1115 setProducerProperty("force_reload", 1);
1116 } else if (key == "xmldata") {
1117 setProducerProperty("xmldata", value.toUtf8().data());
1118 setProducerProperty("force_reload", 1);
1119 } else if (key == "force_aspect_num") {
1120 if (value.isEmpty()) {
1121 m_properties.remove("force_aspect_num");
1122 resetProducerProperty("force_aspect_ratio");
1123 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1124 } else if (key == "force_aspect_den") {
1125 if (value.isEmpty()) {
1126 m_properties.remove("force_aspect_den");
1127 resetProducerProperty("force_aspect_ratio");
1128 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1129 } else if (key == "force_fps") {
1130 if (value.isEmpty()) {
1131 m_properties.remove("force_fps");
1132 resetProducerProperty("force_fps");
1133 } else setProducerProperty("force_fps", value.toDouble());
1134 } else if (key == "force_progressive") {
1135 if (value.isEmpty()) {
1136 m_properties.remove("force_progressive");
1137 resetProducerProperty("force_progressive");
1138 } else setProducerProperty("force_progressive", value.toInt());
1139 } else if (key == "force_tff") {
1140 if (value.isEmpty()) {
1141 m_properties.remove("force_tff");
1142 resetProducerProperty("force_tff");
1143 } else setProducerProperty("force_tff", value.toInt());
1144 } else if (key == "threads") {
1145 if (value.isEmpty()) {
1146 m_properties.remove("threads");
1147 setProducerProperty("threads", 1);
1148 } else setProducerProperty("threads", value.toInt());
1149 } else if (key == "video_index") {
1150 if (value.isEmpty()) {
1151 m_properties.remove("video_index");
1152 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1153 } else setProducerProperty("video_index", value.toInt());
1154 } else if (key == "audio_index") {
1155 if (value.isEmpty()) {
1156 m_properties.remove("audio_index");
1157 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1158 } else setProducerProperty("audio_index", value.toInt());
1159 } else if (key == "force_colorspace") {
1160 if (value.isEmpty()) {
1161 m_properties.remove("force_colorspace");
1162 resetProducerProperty("force_colorspace");
1163 } else setProducerProperty("force_colorspace", value.toInt());
1164 } else if (key == "full_luma") {
1165 if (value.isEmpty()) {
1166 m_properties.remove("full_luma");
1167 resetProducerProperty("set.force_full_luma");
1168 } else setProducerProperty("set.force_full_luma", value.toInt());
1172 QMap <QString, QString> DocClipBase::properties() const
1174 return m_properties;
1177 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1179 QMap <QString, QString> currentProps;
1180 QMap<QString, QString>::const_iterator i = props.constBegin();
1181 while (i != props.constEnd()) {
1182 currentProps.insert(i.key(), m_properties.value(i.key()));
1185 return currentProps;
1188 bool DocClipBase::getAudioThumbs()
1190 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1191 if (m_audioThumbCreated) {
1194 m_audioTimer.start();
1198 bool DocClipBase::isPlaceHolder() const
1200 return m_placeHolder;
1203 void DocClipBase::addCutZone(int in, int out, QString desc)
1206 info.zone = QPoint(in, out);
1207 info.description = desc;
1208 for (int i = 0; i < m_cutZones.count(); i++)
1209 if (m_cutZones.at(i).zone == info.zone) {
1212 m_cutZones.append(info);
1215 bool DocClipBase::hasCutZone(QPoint p) const
1217 for (int i = 0; i < m_cutZones.count(); i++)
1218 if (m_cutZones.at(i).zone == p) return true;
1223 void DocClipBase::removeCutZone(int in, int out)
1226 for (int i = 0; i < m_cutZones.count(); i++) {
1227 if (m_cutZones.at(i).zone == p) {
1228 m_cutZones.removeAt(i);
1234 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1236 QPoint old(oldin, oldout);
1237 for (int i = 0; i < m_cutZones.size(); ++i) {
1238 if (m_cutZones.at(i).zone == old) {
1240 info.zone = QPoint(in, out);
1241 info.description = desc;
1242 m_cutZones.replace(i, info);
1248 QList <CutZoneInfo> DocClipBase::cutZones() const
1253 bool DocClipBase::hasVideoCodec(const QString &codec) const
1255 Mlt::Producer *prod = NULL;
1256 if (m_baseTrackProducers.count() == 0) return false;
1257 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1258 if (m_baseTrackProducers.at(i) != NULL) {
1259 prod = m_baseTrackProducers.at(i);
1264 if (!prod) return false;
1265 int default_video = prod->get_int("video_index");
1267 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1268 return prod->get(property) == codec;
1271 bool DocClipBase::hasAudioCodec(const QString &codec) const
1273 Mlt::Producer *prod = NULL;
1274 if (m_baseTrackProducers.count() == 0) return false;
1275 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1276 if (m_baseTrackProducers.at(i) != NULL) {
1277 prod = m_baseTrackProducers.at(i);
1281 if (!prod) return false;
1282 int default_video = prod->get_int("audio_index");
1284 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1285 return prod->get(property) == codec;
1289 void DocClipBase::slotExtractImage(QList <int> frames)
1291 if (m_thumbProd == NULL) return;
1292 m_thumbProd->extractImage(frames);
1295 QImage DocClipBase::extractImage(int frame, int width, int height)
1297 if (m_thumbProd == NULL) return QImage();
1298 QMutexLocker locker(&m_producerMutex);
1299 return m_thumbProd->extractImage(frame, width, height);
1302 void DocClipBase::setAnalysisData(const QString &name, const QString &data, int offset)
1304 if (data.isEmpty()) m_analysisdata.remove(name);
1306 if (m_analysisdata.contains(name)) {
1307 if (KMessageBox::questionYesNo(kapp->activeWindow(), i18n("Clip already contains analysis data %1", name), QString(), KGuiItem(i18n("Merge")), KGuiItem(i18n("Add"))) == KMessageBox::Yes) {
1309 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1310 Mlt::Geometry geometry(m_analysisdata.value(name).toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1311 Mlt::Geometry newGeometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1312 Mlt::GeometryItem item;
1314 while (!newGeometry.next_key(&item, pos)) {
1316 item.frame(pos + offset);
1318 geometry.insert(item);
1320 m_analysisdata.insert(name, geometry.serialise());
1323 // Add data with another name
1325 QString newname = name + " " + QString::number(i);
1326 while (m_analysisdata.contains(newname)) {
1328 newname = name + " " + QString::number(i);
1330 m_analysisdata.insert(newname, geometryWithOffset(data, offset));
1333 else m_analysisdata.insert(name, geometryWithOffset(data, offset));
1337 const QString DocClipBase::geometryWithOffset(QString data, int offset)
1339 if (offset == 0) return data;
1340 Mlt::Profile *profile = m_baseTrackProducers.at(0)->profile();
1341 Mlt::Geometry geometry(data.toUtf8().data(), m_properties.value("duration").toInt(), profile->width(), profile->height());
1342 Mlt::Geometry newgeometry(NULL, m_properties.value("duration").toInt(), profile->width(), profile->height());
1343 Mlt::GeometryItem item;
1345 while (!geometry.next_key(&item, pos)) {
1347 item.frame(pos + offset);
1349 newgeometry.insert(item);
1351 return newgeometry.serialise();
1354 QMap <QString, QString> DocClipBase::analysisData() const
1356 return m_analysisdata;