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>
37 #include <QCryptographicHash>
38 #include <QtConcurrentRun>
42 DocClipBase::DocClipBase(ClipManager *clipManager, QDomElement xml, const QString &id) :
46 m_baseTrackProducers(),
47 m_videoTrackProducers(),
48 m_audioTrackProducers(),
49 m_snapMarkers(QList < CommentedTime >()),
52 m_audioThumbCreated(false),
54 m_placeHolder(xml.hasAttribute("placeholder")),
57 int type = xml.attribute("type").toInt();
58 m_clipType = (CLIPTYPE) type;
59 if (m_placeHolder) xml.removeAttribute("placeholder");
60 QDomNamedNodeMap attributes = xml.attributes();
61 for (int i = 0; i < attributes.count(); i++) {
62 QString name = attributes.item(i).nodeName();
63 if (name.startsWith("meta.attr.")) {
64 m_metadata.insert(name.section('.', 2, 3), attributes.item(i).nodeValue());
65 } else m_properties.insert(name, attributes.item(i).nodeValue());
68 if (xml.hasAttribute("cutzones")) {
69 QStringList cuts = xml.attribute("cutzones").split(';', QString::SkipEmptyParts);
70 for (int i = 0; i < cuts.count(); i++) {
71 QString z = cuts.at(i);
72 addCutZone(z.section('-', 0, 0).toInt(), z.section('-', 1, 1).toInt(), z.section('-', 2, 2));
76 if (xml.hasAttribute("analysisdata")) {
77 QStringList adata = xml.attribute("analysisdata").split('#', QString::SkipEmptyParts);
78 for (int i = 0; i < adata.count(); i++)
79 m_analysisdata.insert(adata.at(i).section('?', 0, 0), adata.at(i).section('?', 1, 1));
82 KUrl url = KUrl(xml.attribute("resource"));
83 if (!m_properties.contains("file_hash") && !url.isEmpty()) getFileHash(url.path());
85 if (xml.hasAttribute("duration")) {
86 setDuration(GenTime(xml.attribute("duration").toInt(), KdenliveSettings::project_fps()));
88 int out = xml.attribute("out").toInt();
89 int in = xml.attribute("in").toInt();
90 setDuration(GenTime(out - in, KdenliveSettings::project_fps()));
93 if (!m_properties.contains("name")) m_properties.insert("name", url.fileName());
95 m_thumbProd = new KThumb(clipManager, url, m_id, m_properties.value("file_hash"));
97 // Setup timer to trigger audio thumbs creation
98 m_audioTimer.setSingleShot(true);
99 m_audioTimer.setInterval(800);
100 connect(&m_audioTimer, SIGNAL(timeout()), m_thumbProd, SLOT(slotCreateAudioThumbs()));
104 DocClipBase::~DocClipBase()
109 qDeleteAll(m_toDeleteProducers);
110 m_toDeleteProducers.clear();
111 qDeleteAll(m_baseTrackProducers);
112 m_baseTrackProducers.clear();
113 qDeleteAll(m_audioTrackProducers);
114 m_audioTrackProducers.clear();
115 qDeleteAll(m_videoTrackProducers);
116 m_videoTrackProducers.clear();
119 void DocClipBase::setZone(QPoint zone)
121 m_properties.insert("zone_in", QString::number(zone.x()));
122 m_properties.insert("zone_out", QString::number(zone.y()));
125 QPoint DocClipBase::zone() const
127 QPoint zone(m_properties.value("zone_in", "0").toInt(), m_properties.value("zone_out", "50").toInt());
132 bool DocClipBase::hasAudioThumb() const
134 if (m_clipType == AUDIO || m_clipType == AV || m_clipType == PLAYLIST) return true;
138 void DocClipBase::slotClearAudioCache()
140 m_audioFrameCache.clear();
141 m_audioThumbCreated = false;
144 /*void DocClipBase::getClipMainThumb() {
145 if (m_thumbProd) m_thumbProd->getMainThumb(m_properties.value("thumbnail").toInt());
148 KThumb *DocClipBase::thumbProducer()
153 bool DocClipBase::audioThumbCreated() const
155 return m_audioThumbCreated;
158 const QString DocClipBase::name() const
161 return m_properties.value("name");
164 const QString &DocClipBase::getId() const
169 const CLIPTYPE & DocClipBase::clipType() const
174 void DocClipBase::setClipType(CLIPTYPE type)
177 m_properties.insert("type", QString::number((int) type));
180 KUrl DocClipBase::fileURL() const
182 QString res = m_properties.value("resource");
183 if (m_clipType != COLOR && !res.isEmpty()) return KUrl(res);
187 void DocClipBase::setClipThumbFrame(const uint &ix)
189 m_properties.insert("thumbnail", QString::number((int) ix));
192 uint DocClipBase::getClipThumbFrame() const
194 return (uint) m_properties.value("thumbnail").toInt();
197 const QString DocClipBase::description() const
199 return m_properties.value("description");
202 bool DocClipBase::isTransparent() const
204 return (m_properties.value("transparency") == "1");
207 const QString DocClipBase::getProperty(const QString &prop) const
209 return m_properties.value(prop);
212 void DocClipBase::setDuration(GenTime dur)
215 m_properties.insert("duration", QString::number((int) dur.frames(KdenliveSettings::project_fps())));
218 const GenTime &DocClipBase::duration() const
223 const GenTime DocClipBase::maxDuration() const
225 if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == TEXT || (m_clipType == SLIDESHOW && m_properties.value("loop") == "1")) {
226 /*const GenTime dur(15000, KdenliveSettings::project_fps());
233 bool DocClipBase::hasFileSize() const
238 qulonglong DocClipBase::fileSize() const
240 return m_properties.value("file_size").toULongLong();
244 QDomElement DocClipBase::toXML(bool hideTemporaryProperties) const
247 QDomElement clip = doc.createElement("producer");
249 QMapIterator<QString, QString> i(m_properties);
250 while (i.hasNext()) {
252 if (hideTemporaryProperties && i.key().startsWith('_')) continue;
253 if (!i.value().isEmpty()) clip.setAttribute(i.key(), i.value());
255 doc.appendChild(clip);
256 if (!m_cutZones.isEmpty()) {
258 for (int i = 0; i < m_cutZones.size(); i++) {
259 CutZoneInfo info = m_cutZones.at(i);
260 cuts << QString::number(info.zone.x()) + "-" + QString::number(info.zone.y()) + "-" + info.description;
262 clip.setAttribute("cutzones", cuts.join(";"));
265 if (!m_analysisdata.isEmpty()) {
266 QMapIterator<QString, QString> i(m_analysisdata);
267 while (i.hasNext()) {
269 //WARNING: a ? and # separator is not a good idea
270 adata.append(i.key() + "?" + i.value() + "#");
273 clip.setAttribute("analysisdata", adata);
274 //kDebug() << "/// CLIP XML: " << doc.toString();
275 return doc.documentElement();
279 void DocClipBase::setAudioThumbCreated(bool isDone)
281 m_audioThumbCreated = isDone;
284 void DocClipBase::updateAudioThumbnail(const audioByteArray& data)
286 //kDebug() << "CLIPBASE RECIEDVED AUDIO DATA*********************************************";
287 m_audioFrameCache = data;
288 m_audioThumbCreated = true;
292 QList < GenTime > DocClipBase::snapMarkers() const
294 QList < GenTime > markers;
296 for (int count = 0; count < m_snapMarkers.count(); ++count) {
297 markers.append(m_snapMarkers.at(count).time());
303 QList < CommentedTime > DocClipBase::commentedSnapMarkers() const
305 return m_snapMarkers;
309 void DocClipBase::addSnapMarker(const CommentedTime marker)
311 QList < CommentedTime >::Iterator it = m_snapMarkers.begin();
312 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
313 if ((*it).time() >= marker.time())
317 if ((it != m_snapMarkers.end()) && ((*it).time() == marker.time())) {
318 (*it).setComment(marker.comment());
319 (*it).setMarkerType(marker.markerType());
320 //kError() << "trying to add Snap Marker that already exists, this will cause inconsistancies with undo/redo";
322 m_snapMarkers.insert(it, marker);
326 void DocClipBase::editSnapMarker(const GenTime & time, QString comment)
328 QList < CommentedTime >::Iterator it;
329 for (it = m_snapMarkers.begin(); it != m_snapMarkers.end(); ++it) {
330 if ((*it).time() == time)
333 if (it != m_snapMarkers.end()) {
334 (*it).setComment(comment);
336 kError() << "trying to edit Snap Marker that does not already exists";
340 QString DocClipBase::deleteSnapMarker(const GenTime & time)
342 QString result = i18n("Marker");
343 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
345 while (itt != m_snapMarkers.end()) {
346 if ((*itt).time() == time)
351 if ((itt != m_snapMarkers.end()) && ((*itt).time() == time)) {
352 result = (*itt).comment();
353 m_snapMarkers.erase(itt);
359 GenTime DocClipBase::hasSnapMarkers(const GenTime & time)
361 QList < CommentedTime >::Iterator itt = m_snapMarkers.begin();
363 while (itt != m_snapMarkers.end()) {
364 if ((*itt).time() == time)
372 GenTime DocClipBase::findPreviousSnapMarker(const GenTime & currTime)
375 for (it = 0; it < m_snapMarkers.count(); it++) {
376 if (m_snapMarkers.at(it).time() >= currTime)
379 if (it == 0) return GenTime();
380 else if (it == m_snapMarkers.count() - 1 && m_snapMarkers.at(it).time() < currTime)
381 return m_snapMarkers.at(it).time();
382 else return m_snapMarkers.at(it - 1).time();
385 GenTime DocClipBase::findNextSnapMarker(const GenTime & currTime)
388 for (it = 0; it < m_snapMarkers.count(); it++) {
389 if (m_snapMarkers.at(it).time() > currTime)
392 if (it < m_snapMarkers.count() && m_snapMarkers.at(it).time() > currTime) return m_snapMarkers.at(it).time();
396 QString DocClipBase::markerComment(GenTime t) const
398 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
399 while (itt != m_snapMarkers.end()) {
400 if ((*itt).time() == t)
401 return (*itt).comment();
407 CommentedTime DocClipBase::markerAt(GenTime t) const
409 QList < CommentedTime >::ConstIterator itt = m_snapMarkers.begin();
410 while (itt != m_snapMarkers.end()) {
411 if ((*itt).time() == t)
415 return CommentedTime();
418 void DocClipBase::clearThumbProducer()
420 if (m_thumbProd) m_thumbProd->clearProducer();
423 void DocClipBase::reloadThumbProducer()
425 if (m_thumbProd && !m_thumbProd->hasProducer())
426 m_thumbProd->setProducer(getProducer());
429 void DocClipBase::deleteProducers()
431 if (m_thumbProd) m_thumbProd->clearProducer();
433 if (numReferences() > 0 && (!m_baseTrackProducers.isEmpty() || !m_videoTrackProducers.isEmpty() || !m_audioTrackProducers.isEmpty())) {
434 // Clip is used in timeline, delay producers deletion
435 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
436 m_toDeleteProducers.append(m_baseTrackProducers.at(i));
438 for (int i = 0; i < m_videoTrackProducers.count(); i++) {
439 m_toDeleteProducers.append(m_videoTrackProducers.at(i));
441 for (int i = 0; i < m_audioTrackProducers.count(); i++) {
442 m_toDeleteProducers.append(m_audioTrackProducers.at(i));
446 qDeleteAll(m_baseTrackProducers);
447 qDeleteAll(m_videoTrackProducers);
448 qDeleteAll(m_audioTrackProducers);
449 m_replaceMutex.unlock();
451 m_baseTrackProducers.clear();
452 m_videoTrackProducers.clear();
453 m_audioTrackProducers.clear();
456 void DocClipBase::cleanupProducers()
460 kDebug()<<"----------------------------------------------------------------------------------";
461 for (int i = 0; i < m_toDeleteProducers.count(); i++) {
462 if (m_toDeleteProducers.at(i) != NULL) {
463 Mlt::Properties props(m_toDeleteProducers.at(i)->get_properties());
464 if (props.ref_count() > 2) {
465 kDebug()<<"PRODUCER: "<<i<<", COUNTS: "<<props.ref_count();
473 qDeleteAll(m_toDeleteProducers);
474 m_toDeleteProducers.clear();
475 m_replaceMutex.unlock();
479 bool DocClipBase::isClean() const
481 return m_toDeleteProducers.isEmpty();
484 void DocClipBase::setValid()
486 m_placeHolder = false;
489 void DocClipBase::setProducer(Mlt::Producer *producer, bool reset, bool readPropertiesFromProducer)
491 if (producer == NULL) return;
493 QMutexLocker locker(&m_producerMutex);
494 m_replaceMutex.lock();
497 QString id = producer->get("id");
498 if (m_placeHolder || !producer->is_valid()) {
499 char *tmp = qstrdup(i18n("Missing clip").toUtf8().constData());
500 producer->set("markup", tmp);
501 producer->set("bgcolour", "0xff0000ff");
502 producer->set("pad", "10");
505 else if (m_thumbProd && !m_thumbProd->hasProducer()) {
506 if (m_clipType != AUDIO) {
507 if (!id.endsWith("_audio"))
508 m_thumbProd->setProducer(producer);
510 else m_thumbProd->setProducer(producer);
513 bool updated = false;
514 if (id.contains('_')) {
515 // this is a subtrack producer, insert it at correct place
516 id = id.section('_', 1);
517 if (id.endsWith("audio")) {
518 int pos = id.section('_', 0, 0).toInt();
519 if (pos >= m_audioTrackProducers.count()) {
520 while (m_audioTrackProducers.count() - 1 < pos) {
521 m_audioTrackProducers.append(NULL);
524 if (m_audioTrackProducers.at(pos) == NULL) {
525 m_audioTrackProducers[pos] = producer;
528 else delete producer;
530 } else if (id.endsWith("video")) {
532 // Keep compatibility with older projects where video only producers were not track specific
533 if (id.contains('_')) pos = id.section('_', 0, 0).toInt();
534 if (pos >= m_videoTrackProducers.count()) {
535 while (m_videoTrackProducers.count() - 1 < pos) {
536 m_videoTrackProducers.append(NULL);
539 if (m_videoTrackProducers.at(pos) == NULL) {
540 m_videoTrackProducers[pos] = producer;
543 else delete producer;
546 int pos = id.toInt();
547 if (pos >= m_baseTrackProducers.count()) {
548 while (m_baseTrackProducers.count() - 1 < pos) {
549 m_baseTrackProducers.append(NULL);
552 if (m_baseTrackProducers.at(pos) == NULL) {
553 m_baseTrackProducers[pos] = producer;
556 else delete producer;
558 if (m_baseTrackProducers.isEmpty()) {
559 m_baseTrackProducers.append(producer);
562 else if (m_baseTrackProducers.at(0) == NULL) {
563 m_baseTrackProducers[0] = producer;
566 else delete producer;
568 if (updated && readPropertiesFromProducer && (m_clipType != COLOR && m_clipType != IMAGE && m_clipType != TEXT))
569 setDuration(GenTime(producer->get_length(), KdenliveSettings::project_fps()));
572 static double getPixelAspect(QMap<QString, QString>& props) {
573 int width = props.value("frame_size").section('x', 0, 0).toInt();
574 int height = props.value("frame_size").section('x', 1, 1).toInt();
575 int aspectNumerator = props.value("force_aspect_num").toInt();
576 int aspectDenominator = props.value("force_aspect_den").toInt();
577 if (aspectDenominator != 0 && width != 0)
578 return double(height) * aspectNumerator / aspectDenominator / width;
583 Mlt::Producer *DocClipBase::audioProducer(int track)
585 QMutexLocker locker(&m_producerMutex);
586 if (m_audioTrackProducers.count() <= track) {
587 while (m_audioTrackProducers.count() - 1 < track) {
588 m_audioTrackProducers.append(NULL);
591 if (m_audioTrackProducers.at(track) == NULL) {
593 for (i = 0; i < m_audioTrackProducers.count(); i++)
594 if (m_audioTrackProducers.at(i) != NULL) break;
596 if (i >= m_audioTrackProducers.count()) {
597 // Could not find a valid producer for that clip
599 base = getProducer();
605 else base = m_audioTrackProducers.at(i);
606 m_audioTrackProducers[track] = cloneProducer(base);
607 adjustProducerProperties(m_audioTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_audio"), false, true);
609 return m_audioTrackProducers.at(track);
613 void DocClipBase::adjustProducerProperties(Mlt::Producer *prod, const QString &id, bool mute, bool blind)
615 if (m_properties.contains("force_aspect_num") && m_properties.contains("force_aspect_den") && m_properties.contains("frame_size"))
616 prod->set("force_aspect_ratio", getPixelAspect(m_properties));
617 if (m_properties.contains("force_fps")) prod->set("force_fps", m_properties.value("force_fps").toDouble());
618 if (m_properties.contains("force_progressive")) prod->set("force_progressive", m_properties.value("force_progressive").toInt());
619 if (m_properties.contains("force_tff")) prod->set("force_tff", m_properties.value("force_tff").toInt());
620 if (m_properties.contains("threads")) prod->set("threads", m_properties.value("threads").toInt());
621 if (mute) prod->set("audio_index", -1);
622 else if (m_properties.contains("audio_index")) prod->set("audio_index", m_properties.value("audio_index").toInt());
623 if (blind) prod->set("video_index", -1);
624 else if (m_properties.contains("video_index")) prod->set("video_index", m_properties.value("video_index").toInt());
625 prod->set("id", id.toUtf8().constData());
626 if (m_properties.contains("force_colorspace")) prod->set("force_colorspace", m_properties.value("force_colorspace").toInt());
627 if (m_properties.contains("full_luma")) prod->set("set.force_full_luma", m_properties.value("full_luma").toInt());
628 if (m_properties.contains("proxy_out")) {
629 // We have a proxy clip, make sure the proxy has same duration as original
630 prod->set("length", m_properties.value("duration").toInt());
631 prod->set("out", m_properties.value("proxy_out").toInt());
636 Mlt::Producer *DocClipBase::videoProducer(int track)
638 QMutexLocker locker(&m_producerMutex);
639 if (m_videoTrackProducers.count() <= track) {
640 while (m_videoTrackProducers.count() - 1 < track) {
641 m_videoTrackProducers.append(NULL);
644 if (m_videoTrackProducers.at(track) == NULL) {
646 for (i = 0; i < m_videoTrackProducers.count(); i++)
647 if (m_videoTrackProducers.at(i) != NULL) break;
649 if (i >= m_videoTrackProducers.count()) {
650 // Could not find a valid producer for that clip
652 base = getProducer();
658 else base = m_videoTrackProducers.at(i);
659 m_videoTrackProducers[track] = cloneProducer(base);
660 adjustProducerProperties(m_videoTrackProducers.at(track), QString(getId() + '_' + QString::number(track) + "_video"), true, false);
662 return m_videoTrackProducers.at(track);
665 Mlt::Producer *DocClipBase::getCloneProducer()
667 Mlt::Producer *source = NULL;
668 Mlt::Producer *prod = NULL;
669 if (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST) {
670 source = getProducer();
671 if (!source) return NULL;
673 if (m_clipType == COLOR) {
674 prod = new Mlt::Producer(*(source->profile()), 0, QString("colour:" + QString(source->get("resource"))).toUtf8().constData());
675 } else if (m_clipType == TEXT) {
676 prod = new Mlt::Producer(*(source->profile()), 0, QString("kdenlivetitle:" + QString(source->get("resource"))).toUtf8().constData());
677 if (prod && prod->is_valid() && m_properties.contains("xmldata"))
678 prod->set("xmldata", m_properties.value("xmldata").toUtf8().constData());
682 QMutexLocker locker(&m_producerMutex);
683 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
684 if (m_baseTrackProducers.at(i) != NULL) {
685 source = m_baseTrackProducers.at(i);
689 if (!source) return NULL;
691 prod = cloneProducer(source);
694 adjustProducerProperties(prod, getId() + "_", false, false);
695 if (!m_properties.contains("proxy_out")) {
696 // Adjust length in case...
697 if (m_properties.contains("duration")) prod->set("length", m_properties.value("duration").toInt());
698 if (m_properties.contains("out"))prod->set("out", m_properties.value("out").toInt());
700 if (m_clipType == AUDIO) {
701 prod->set("_audioclip", 1);
708 Mlt::Producer *DocClipBase::getProducer(int track)
710 QMutexLocker locker(&m_producerMutex);
711 if (track == -1 || (m_clipType != AUDIO && m_clipType != AV && m_clipType != PLAYLIST)) {
712 if (m_baseTrackProducers.count() == 0) {
715 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
716 if (m_baseTrackProducers.at(i) != NULL) {
717 return m_baseTrackProducers.at(i);
722 if (track >= m_baseTrackProducers.count()) {
723 while (m_baseTrackProducers.count() - 1 < track) {
724 m_baseTrackProducers.append(NULL);
727 if (m_baseTrackProducers.at(track) == NULL) {
729 for (i = 0; i < m_baseTrackProducers.count(); i++)
730 if (m_baseTrackProducers.at(i) != NULL) break;
732 if (i >= m_baseTrackProducers.count()) {
733 // Could not find a valid producer for that clip, check in
736 Mlt::Producer *prod = cloneProducer(m_baseTrackProducers.at(i));
737 adjustProducerProperties(prod, QString(getId() + '_' + QString::number(track)), false, false);
738 m_baseTrackProducers[track] = prod;
740 return m_baseTrackProducers.at(track);
744 Mlt::Producer *DocClipBase::cloneProducer(Mlt::Producer *source)
746 Mlt::Producer *result = NULL;
747 QString url = QString::fromUtf8(source->get("resource"));
748 if (url == "<playlist>" || url == "<tractor>" || url == "<producer>") {
749 // Xml producer sometimes loses the correct url
750 url = m_properties.value("resource");
752 if (m_clipType == SLIDESHOW || KIO::NetAccess::exists(KUrl(url), KIO::NetAccess::SourceSide, 0)) {
753 result = new Mlt::Producer(*(source->profile()), url.toUtf8().constData());
755 if (result == NULL || !result->is_valid()) {
757 QString txt = "+" + i18n("Missing clip") + ".txt";
758 char *tmp = qstrdup(txt.toUtf8().constData());
759 result = new Mlt::Producer(*source->profile(), tmp);
761 if (result == NULL || !result->is_valid())
762 result = new Mlt::Producer(*(source->profile()), "colour:red");
764 result->set("bgcolour", "0xff0000ff");
765 result->set("pad", "10");
769 /*Mlt::Properties src_props(source->get_properties());
770 Mlt::Properties props(result->get_properties());
771 props.inherit(src_props);*/
775 void DocClipBase::setProducerProperty(const char *name, int data)
777 QMutexLocker locker(&m_producerMutex);
778 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
779 if (m_baseTrackProducers.at(i) != NULL)
780 m_baseTrackProducers[i]->set(name, data);
784 void DocClipBase::setProducerProperty(const char *name, double data)
786 QMutexLocker locker(&m_producerMutex);
787 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
788 if (m_baseTrackProducers.at(i) != NULL)
789 m_baseTrackProducers[i]->set(name, data);
793 void DocClipBase::setProducerProperty(const char *name, const char *data)
795 QMutexLocker locker(&m_producerMutex);
796 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
797 if (m_baseTrackProducers.at(i) != NULL)
798 m_baseTrackProducers[i]->set(name, data);
802 void DocClipBase::resetProducerProperty(const char *name)
804 QMutexLocker locker(&m_producerMutex);
805 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
806 if (m_baseTrackProducers.at(i) != NULL)
807 m_baseTrackProducers[i]->set(name, (const char*) NULL);
811 const char *DocClipBase::producerProperty(const char *name) const
813 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
814 if (m_baseTrackProducers.at(i) != NULL) {
815 return m_baseTrackProducers.at(i)->get(name);
822 void DocClipBase::slotRefreshProducer()
824 if (m_baseTrackProducers.count() == 0) return;
825 if (m_clipType == SLIDESHOW) {
826 setProducerProperty("ttl", getProperty("ttl").toInt());
827 //m_clipProducer->set("id", getProperty("id"));
828 if (!getProperty("animation").isEmpty()) {
829 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
831 Mlt::Filter *filter = clipService.filter(ct);
833 if (strcmp(filter->get("mlt_service"), "affine") == 0) {
835 } else if (strcmp(filter->get("mlt_service"), "boxblur") == 0) {
836 clipService.detach(*filter);
838 filter = clipService.filter(ct);
841 if (!filter || strcmp(filter->get("mlt_service"), "affine")) {
842 // filter does not exist, create it.
843 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "affine");
844 if (filter && filter->is_valid()) {
845 int cycle = getProperty("ttl").toInt();
846 QString geometry = SlideshowClip::animationToGeometry(getProperty("animation"), cycle);
847 if (!geometry.isEmpty()) {
848 if (getProperty("animation").contains("low-pass")) {
849 Mlt::Filter *blur = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "boxblur");
850 if (blur && blur->is_valid())
851 clipService.attach(*blur);
853 filter->set("transition.geometry", geometry.toUtf8().data());
854 filter->set("transition.cycle", cycle);
855 clipService.attach(*filter);
860 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
862 Mlt::Filter *filter = clipService.filter(0);
864 if (strcmp(filter->get("mlt_service"), "affine") == 0 || strcmp(filter->get("mlt_service"), "boxblur") == 0) {
865 clipService.detach(*filter);
867 filter = clipService.filter(ct);
870 if (getProperty("fade") == "1") {
871 // we want a fade filter effect
872 kDebug() << "//////////// FADE WANTED";
873 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
875 Mlt::Filter *filter = clipService.filter(ct);
877 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
881 filter = clipService.filter(ct);
884 if (filter && strcmp(filter->get("mlt_service"), "luma") == 0) {
885 filter->set("cycle", getProperty("ttl").toInt());
886 filter->set("duration", getProperty("luma_duration").toInt());
887 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
888 if (!getProperty("softness").isEmpty()) {
889 int soft = getProperty("softness").toInt();
890 filter->set("luma.softness", (double) soft / 100.0);
893 // filter does not exist, create it...
894 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "luma");
895 filter->set("cycle", getProperty("ttl").toInt());
896 filter->set("duration", getProperty("luma_duration").toInt());
897 filter->set("luma.resource", getProperty("luma_file").toUtf8().data());
898 if (!getProperty("softness").isEmpty()) {
899 int soft = getProperty("softness").toInt();
900 filter->set("luma.softness", (double) soft / 100.0);
902 clipService.attach(*filter);
905 kDebug() << "//////////// FADE NOT WANTED!!!";
906 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
908 Mlt::Filter *filter = clipService.filter(0);
910 if (strcmp(filter->get("mlt_service"), "luma") == 0) {
911 clipService.detach(*filter);
913 filter = clipService.filter(ct);
916 if (getProperty("crop") == "1") {
917 // we want a center crop filter effect
918 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
920 Mlt::Filter *filter = clipService.filter(ct);
922 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
926 filter = clipService.filter(ct);
929 if (!filter || strcmp(filter->get("mlt_service"), "crop")) {
930 // filter does not exist, create it...
931 Mlt::Filter *filter = new Mlt::Filter(*(m_baseTrackProducers.at(0)->profile()), "crop");
932 filter->set("center", 1);
933 clipService.attach(*filter);
936 Mlt::Service clipService(m_baseTrackProducers.at(0)->get_service());
938 Mlt::Filter *filter = clipService.filter(0);
940 if (strcmp(filter->get("mlt_service"), "crop") == 0) {
941 clipService.detach(*filter);
943 filter = clipService.filter(ct);
949 void DocClipBase::setProperties(QMap <QString, QString> properties)
951 // changing clip type is not allowed
952 properties.remove("type");
953 QMapIterator<QString, QString> i(properties);
954 bool refreshProducer = false;
956 keys << "luma_duration" << "luma_file" << "fade" << "ttl" << "softness" << "crop" << "animation";
957 QString oldProxy = m_properties.value("proxy");
958 while (i.hasNext()) {
960 setProperty(i.key(), i.value());
961 if (m_clipType == SLIDESHOW && keys.contains(i.key())) refreshProducer = true;
963 if (properties.contains("proxy")) {
964 QString value = properties.value("proxy");
965 // If value is "-", that means user manually disabled proxy on this clip
966 if (value.isEmpty() || value == "-") {
968 emit abortProxy(m_id, oldProxy);
971 emit createProxy(m_id);
974 if (refreshProducer) slotRefreshProducer();
977 void DocClipBase::setMetadata(QMap <QString, QString> properties)
979 QMapIterator<QString, QString> i(properties);
980 while (i.hasNext()) {
982 if (i.value().isEmpty() && m_metadata.contains(i.key())) {
983 m_metadata.remove(i.key());
985 m_metadata.insert(i.key(), i.value());
990 QMap <QString, QString> DocClipBase::metadata() const
995 void DocClipBase::clearProperty(const QString &key)
997 m_properties.remove(key);
1000 void DocClipBase::getFileHash(const QString &url)
1002 if (m_clipType == SLIDESHOW) return;
1004 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1005 QByteArray fileData;
1006 QByteArray fileHash;
1007 //kDebug() << "SETTING HASH of" << value;
1008 m_properties.insert("file_size", QString::number(file.size()));
1010 * 1 MB = 1 second per 450 files (or faster)
1011 * 10 MB = 9 seconds per 450 files (or faster)
1013 if (file.size() > 1000000*2) {
1014 fileData = file.read(1000000);
1015 if (file.seek(file.size() - 1000000))
1016 fileData.append(file.readAll());
1018 fileData = file.readAll();
1020 fileHash = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
1021 m_properties.insert("file_hash", QString(fileHash.toHex()));
1025 bool DocClipBase::checkHash() const
1027 KUrl url = fileURL();
1028 if (!url.isEmpty() && getClipHash() != getHash(url.path())) return false;
1032 QString DocClipBase::getClipHash() const
1035 if (m_clipType == SLIDESHOW) hash = QCryptographicHash::hash(m_properties.value("resource").toAscii().data(), QCryptographicHash::Md5).toHex();
1036 else if (m_clipType == COLOR) hash = QCryptographicHash::hash(m_properties.value("colour").toAscii().data(), QCryptographicHash::Md5).toHex();
1037 else if (m_clipType == TEXT) hash = QCryptographicHash::hash(QString("title" + getId() + m_properties.value("xmldata")).toUtf8().data(), QCryptographicHash::Md5).toHex();
1039 if (m_properties.contains("file_hash")) hash = m_properties.value("file_hash");
1040 if (hash.isEmpty()) hash = getHash(fileURL().path());
1046 void DocClipBase::setPlaceHolder(bool place)
1048 m_placeHolder = place;
1052 QString DocClipBase::getHash(const QString &path)
1055 if (file.open(QIODevice::ReadOnly)) { // write size and hash only if resource points to a file
1056 QByteArray fileData;
1057 QByteArray fileHash;
1059 * 1 MB = 1 second per 450 files (or faster)
1060 * 10 MB = 9 seconds per 450 files (or faster)
1062 if (file.size() > 1000000*2) {
1063 fileData = file.read(1000000);
1064 if (file.seek(file.size() - 1000000))
1065 fileData.append(file.readAll());
1067 fileData = file.readAll();
1069 return QCryptographicHash::hash(fileData, QCryptographicHash::Md5).toHex();
1074 void DocClipBase::refreshThumbUrl()
1076 if (m_thumbProd) m_thumbProd->updateThumbUrl(m_properties.value("file_hash"));
1079 void DocClipBase::setProperty(const QString &key, const QString &value)
1081 m_properties.insert(key, value);
1082 if (key == "resource") {
1084 if (m_thumbProd) m_thumbProd->updateClipUrl(KUrl(value), m_properties.value("file_hash"));
1085 } else if (key == "out") setDuration(GenTime(value.toInt(), KdenliveSettings::project_fps()));
1086 //else if (key == "transparency") m_clipProducer->set("transparency", value.toInt());
1087 else if (key == "colour") {
1088 setProducerProperty("colour", value.toUtf8().data());
1089 } else if (key == "templatetext") {
1090 setProducerProperty("templatetext", value.toUtf8().data());
1091 setProducerProperty("force_reload", 1);
1092 } else if (key == "xmldata") {
1093 setProducerProperty("xmldata", value.toUtf8().data());
1094 setProducerProperty("force_reload", 1);
1095 } else if (key == "force_aspect_num") {
1096 if (value.isEmpty()) {
1097 m_properties.remove("force_aspect_num");
1098 resetProducerProperty("force_aspect_ratio");
1099 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1100 } else if (key == "force_aspect_den") {
1101 if (value.isEmpty()) {
1102 m_properties.remove("force_aspect_den");
1103 resetProducerProperty("force_aspect_ratio");
1104 } else setProducerProperty("force_aspect_ratio", getPixelAspect(m_properties));
1105 } else if (key == "force_fps") {
1106 if (value.isEmpty()) {
1107 m_properties.remove("force_fps");
1108 resetProducerProperty("force_fps");
1109 } else setProducerProperty("force_fps", value.toDouble());
1110 } else if (key == "force_progressive") {
1111 if (value.isEmpty()) {
1112 m_properties.remove("force_progressive");
1113 resetProducerProperty("force_progressive");
1114 } else setProducerProperty("force_progressive", value.toInt());
1115 } else if (key == "force_tff") {
1116 if (value.isEmpty()) {
1117 m_properties.remove("force_tff");
1118 resetProducerProperty("force_tff");
1119 } else setProducerProperty("force_tff", value.toInt());
1120 } else if (key == "threads") {
1121 if (value.isEmpty()) {
1122 m_properties.remove("threads");
1123 setProducerProperty("threads", 1);
1124 } else setProducerProperty("threads", value.toInt());
1125 } else if (key == "video_index") {
1126 if (value.isEmpty()) {
1127 m_properties.remove("video_index");
1128 setProducerProperty("video_index", m_properties.value("default_video").toInt());
1129 } else setProducerProperty("video_index", value.toInt());
1130 } else if (key == "audio_index") {
1131 if (value.isEmpty()) {
1132 m_properties.remove("audio_index");
1133 setProducerProperty("audio_index", m_properties.value("default_audio").toInt());
1134 } else setProducerProperty("audio_index", value.toInt());
1135 } else if (key == "force_colorspace") {
1136 if (value.isEmpty()) {
1137 m_properties.remove("force_colorspace");
1138 resetProducerProperty("force_colorspace");
1139 } else setProducerProperty("force_colorspace", value.toInt());
1140 } else if (key == "full_luma") {
1141 if (value.isEmpty()) {
1142 m_properties.remove("full_luma");
1143 resetProducerProperty("set.force_full_luma");
1144 } else setProducerProperty("set.force_full_luma", value.toInt());
1148 QMap <QString, QString> DocClipBase::properties() const
1150 return m_properties;
1153 QMap <QString, QString> DocClipBase::currentProperties(QMap <QString, QString> props)
1155 QMap <QString, QString> currentProps;
1156 QMap<QString, QString>::const_iterator i = props.constBegin();
1157 while (i != props.constEnd()) {
1158 currentProps.insert(i.key(), m_properties.value(i.key()));
1161 return currentProps;
1164 bool DocClipBase::getAudioThumbs()
1166 if (m_thumbProd == NULL || isPlaceHolder() || !KdenliveSettings::audiothumbnails()) return false;
1167 if (m_audioThumbCreated) {
1170 m_audioTimer.start();
1174 bool DocClipBase::isPlaceHolder() const
1176 return m_placeHolder;
1179 void DocClipBase::addCutZone(int in, int out, QString desc)
1182 info.zone = QPoint(in, out);
1183 info.description = desc;
1184 for (int i = 0; i < m_cutZones.count(); i++)
1185 if (m_cutZones.at(i).zone == info.zone) {
1188 m_cutZones.append(info);
1191 bool DocClipBase::hasCutZone(QPoint p) const
1193 for (int i = 0; i < m_cutZones.count(); i++)
1194 if (m_cutZones.at(i).zone == p) return true;
1199 void DocClipBase::removeCutZone(int in, int out)
1202 for (int i = 0; i < m_cutZones.count(); i++) {
1203 if (m_cutZones.at(i).zone == p) {
1204 m_cutZones.removeAt(i);
1210 void DocClipBase::updateCutZone(int oldin, int oldout, int in, int out, QString desc)
1212 QPoint old(oldin, oldout);
1213 for (int i = 0; i < m_cutZones.size(); ++i) {
1214 if (m_cutZones.at(i).zone == old) {
1216 info.zone = QPoint(in, out);
1217 info.description = desc;
1218 m_cutZones.replace(i, info);
1224 QList <CutZoneInfo> DocClipBase::cutZones() const
1229 bool DocClipBase::hasVideoCodec(const QString &codec) const
1231 Mlt::Producer *prod = NULL;
1232 if (m_baseTrackProducers.count() == 0) return false;
1233 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1234 if (m_baseTrackProducers.at(i) != NULL) {
1235 prod = m_baseTrackProducers.at(i);
1240 if (!prod) return false;
1241 int default_video = prod->get_int("video_index");
1243 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1244 return prod->get(property) == codec;
1247 bool DocClipBase::hasAudioCodec(const QString &codec) const
1249 Mlt::Producer *prod = NULL;
1250 if (m_baseTrackProducers.count() == 0) return false;
1251 for (int i = 0; i < m_baseTrackProducers.count(); i++) {
1252 if (m_baseTrackProducers.at(i) != NULL) {
1253 prod = m_baseTrackProducers.at(i);
1257 if (!prod) return false;
1258 int default_video = prod->get_int("audio_index");
1260 snprintf(property, sizeof(property), "meta.media.%d.codec.name", default_video);
1261 return prod->get(property) == codec;
1265 void DocClipBase::slotExtractImage(QList <int> frames)
1267 if (m_thumbProd == NULL) return;
1268 m_thumbProd->extractImage(frames);
1271 QImage DocClipBase::extractImage(int frame, int width, int height)
1273 if (m_thumbProd == NULL) return QImage();
1274 QMutexLocker locker(&m_producerMutex);
1275 return m_thumbProd->extractImage(frame, width, height);
1278 void DocClipBase::setAnalysisData(const QString &name, const QString &data)
1280 if (data.isEmpty()) m_analysisdata.remove(name);
1281 else m_analysisdata.insert(name, data);
1284 QMap <QString, QString> DocClipBase::analysisData() const
1286 return m_analysisdata;